2 * Copyright (c) 2013 Patrick McHardy <kaber@trash.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 #include <linux/module.h>
10 #include <linux/skbuff.h>
11 #include <asm/unaligned.h>
13 #include <net/netns/generic.h>
15 #include <linux/netfilter_ipv4/ip_tables.h>
16 #include <linux/netfilter/x_tables.h>
17 #include <linux/netfilter/xt_tcpudp.h>
18 #include <linux/netfilter/xt_SYNPROXY.h>
19 #include <net/netfilter/nf_conntrack.h>
20 #include <net/netfilter/nf_conntrack_extend.h>
21 #include <net/netfilter/nf_conntrack_seqadj.h>
22 #include <net/netfilter/nf_conntrack_synproxy.h>
25 EXPORT_SYMBOL_GPL(synproxy_net_id
);
28 synproxy_parse_options(const struct sk_buff
*skb
, unsigned int doff
,
29 const struct tcphdr
*th
, struct synproxy_options
*opts
)
31 int length
= (th
->doff
* 4) - sizeof(*th
);
34 ptr
= skb_header_pointer(skb
, doff
+ sizeof(*th
), length
, buf
);
57 if (opsize
== TCPOLEN_MSS
) {
58 opts
->mss
= get_unaligned_be16(ptr
);
59 opts
->options
|= XT_SYNPROXY_OPT_MSS
;
63 if (opsize
== TCPOLEN_WINDOW
) {
65 if (opts
->wscale
> 14)
67 opts
->options
|= XT_SYNPROXY_OPT_WSCALE
;
70 case TCPOPT_TIMESTAMP
:
71 if (opsize
== TCPOLEN_TIMESTAMP
) {
72 opts
->tsval
= get_unaligned_be32(ptr
);
73 opts
->tsecr
= get_unaligned_be32(ptr
+ 4);
74 opts
->options
|= XT_SYNPROXY_OPT_TIMESTAMP
;
77 case TCPOPT_SACK_PERM
:
78 if (opsize
== TCPOLEN_SACK_PERM
)
79 opts
->options
|= XT_SYNPROXY_OPT_SACK_PERM
;
88 EXPORT_SYMBOL_GPL(synproxy_parse_options
);
90 unsigned int synproxy_options_size(const struct synproxy_options
*opts
)
92 unsigned int size
= 0;
94 if (opts
->options
& XT_SYNPROXY_OPT_MSS
)
95 size
+= TCPOLEN_MSS_ALIGNED
;
96 if (opts
->options
& XT_SYNPROXY_OPT_TIMESTAMP
)
97 size
+= TCPOLEN_TSTAMP_ALIGNED
;
98 else if (opts
->options
& XT_SYNPROXY_OPT_SACK_PERM
)
99 size
+= TCPOLEN_SACKPERM_ALIGNED
;
100 if (opts
->options
& XT_SYNPROXY_OPT_WSCALE
)
101 size
+= TCPOLEN_WSCALE_ALIGNED
;
105 EXPORT_SYMBOL_GPL(synproxy_options_size
);
108 synproxy_build_options(struct tcphdr
*th
, const struct synproxy_options
*opts
)
110 __be32
*ptr
= (__be32
*)(th
+ 1);
111 u8 options
= opts
->options
;
113 if (options
& XT_SYNPROXY_OPT_MSS
)
114 *ptr
++ = htonl((TCPOPT_MSS
<< 24) |
115 (TCPOLEN_MSS
<< 16) |
118 if (options
& XT_SYNPROXY_OPT_TIMESTAMP
) {
119 if (options
& XT_SYNPROXY_OPT_SACK_PERM
)
120 *ptr
++ = htonl((TCPOPT_SACK_PERM
<< 24) |
121 (TCPOLEN_SACK_PERM
<< 16) |
122 (TCPOPT_TIMESTAMP
<< 8) |
125 *ptr
++ = htonl((TCPOPT_NOP
<< 24) |
127 (TCPOPT_TIMESTAMP
<< 8) |
130 *ptr
++ = htonl(opts
->tsval
);
131 *ptr
++ = htonl(opts
->tsecr
);
132 } else if (options
& XT_SYNPROXY_OPT_SACK_PERM
)
133 *ptr
++ = htonl((TCPOPT_NOP
<< 24) |
135 (TCPOPT_SACK_PERM
<< 8) |
138 if (options
& XT_SYNPROXY_OPT_WSCALE
)
139 *ptr
++ = htonl((TCPOPT_NOP
<< 24) |
140 (TCPOPT_WINDOW
<< 16) |
141 (TCPOLEN_WINDOW
<< 8) |
144 EXPORT_SYMBOL_GPL(synproxy_build_options
);
146 void synproxy_init_timestamp_cookie(const struct xt_synproxy_info
*info
,
147 struct synproxy_options
*opts
)
149 opts
->tsecr
= opts
->tsval
;
150 opts
->tsval
= tcp_time_stamp
& ~0x3f;
152 if (opts
->options
& XT_SYNPROXY_OPT_WSCALE
)
153 opts
->tsval
|= info
->wscale
;
157 if (opts
->options
& XT_SYNPROXY_OPT_SACK_PERM
)
158 opts
->tsval
|= 1 << 4;
160 if (opts
->options
& XT_SYNPROXY_OPT_ECN
)
161 opts
->tsval
|= 1 << 5;
163 EXPORT_SYMBOL_GPL(synproxy_init_timestamp_cookie
);
165 void synproxy_check_timestamp_cookie(struct synproxy_options
*opts
)
167 opts
->wscale
= opts
->tsecr
& 0xf;
168 if (opts
->wscale
!= 0xf)
169 opts
->options
|= XT_SYNPROXY_OPT_WSCALE
;
171 opts
->options
|= opts
->tsecr
& (1 << 4) ? XT_SYNPROXY_OPT_SACK_PERM
: 0;
173 opts
->options
|= opts
->tsecr
& (1 << 5) ? XT_SYNPROXY_OPT_ECN
: 0;
175 EXPORT_SYMBOL_GPL(synproxy_check_timestamp_cookie
);
177 unsigned int synproxy_tstamp_adjust(struct sk_buff
*skb
,
178 unsigned int protoff
,
181 enum ip_conntrack_info ctinfo
,
182 const struct nf_conn_synproxy
*synproxy
)
184 unsigned int optoff
, optend
;
187 if (synproxy
->tsoff
== 0)
190 optoff
= protoff
+ sizeof(struct tcphdr
);
191 optend
= protoff
+ th
->doff
* 4;
193 if (!skb_make_writable(skb
, optend
))
196 while (optoff
< optend
) {
197 unsigned char *op
= skb
->data
+ optoff
;
206 if (optoff
+ 1 == optend
||
207 optoff
+ op
[1] > optend
||
210 if (op
[0] == TCPOPT_TIMESTAMP
&&
211 op
[1] == TCPOLEN_TIMESTAMP
) {
212 if (CTINFO2DIR(ctinfo
) == IP_CT_DIR_REPLY
) {
215 *ptr
= htonl(ntohl(*ptr
) -
220 *ptr
= htonl(ntohl(*ptr
) +
223 inet_proto_csum_replace4(&th
->check
, skb
,
232 EXPORT_SYMBOL_GPL(synproxy_tstamp_adjust
);
234 static struct nf_ct_ext_type nf_ct_synproxy_extend __read_mostly
= {
235 .len
= sizeof(struct nf_conn_synproxy
),
236 .align
= __alignof__(struct nf_conn_synproxy
),
237 .id
= NF_CT_EXT_SYNPROXY
,
240 #ifdef CONFIG_PROC_FS
241 static void *synproxy_cpu_seq_start(struct seq_file
*seq
, loff_t
*pos
)
243 struct synproxy_net
*snet
= synproxy_pernet(seq_file_net(seq
));
247 return SEQ_START_TOKEN
;
249 for (cpu
= *pos
- 1; cpu
< nr_cpu_ids
; cpu
++) {
250 if (!cpu_possible(cpu
))
253 return per_cpu_ptr(snet
->stats
, cpu
);
259 static void *synproxy_cpu_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
261 struct synproxy_net
*snet
= synproxy_pernet(seq_file_net(seq
));
264 for (cpu
= *pos
; cpu
< nr_cpu_ids
; cpu
++) {
265 if (!cpu_possible(cpu
))
268 return per_cpu_ptr(snet
->stats
, cpu
);
274 static void synproxy_cpu_seq_stop(struct seq_file
*seq
, void *v
)
279 static int synproxy_cpu_seq_show(struct seq_file
*seq
, void *v
)
281 struct synproxy_stats
*stats
= v
;
283 if (v
== SEQ_START_TOKEN
) {
284 seq_printf(seq
, "entries\t\tsyn_received\t"
285 "cookie_invalid\tcookie_valid\t"
286 "cookie_retrans\tconn_reopened\n");
290 seq_printf(seq
, "%08x\t%08x\t%08x\t%08x\t%08x\t%08x\n", 0,
292 stats
->cookie_invalid
,
294 stats
->cookie_retrans
,
295 stats
->conn_reopened
);
300 static const struct seq_operations synproxy_cpu_seq_ops
= {
301 .start
= synproxy_cpu_seq_start
,
302 .next
= synproxy_cpu_seq_next
,
303 .stop
= synproxy_cpu_seq_stop
,
304 .show
= synproxy_cpu_seq_show
,
307 static int synproxy_cpu_seq_open(struct inode
*inode
, struct file
*file
)
309 return seq_open_net(inode
, file
, &synproxy_cpu_seq_ops
,
310 sizeof(struct seq_net_private
));
313 static const struct file_operations synproxy_cpu_seq_fops
= {
314 .owner
= THIS_MODULE
,
315 .open
= synproxy_cpu_seq_open
,
318 .release
= seq_release_net
,
321 static int __net_init
synproxy_proc_init(struct net
*net
)
323 if (!proc_create("synproxy", S_IRUGO
, net
->proc_net_stat
,
324 &synproxy_cpu_seq_fops
))
329 static void __net_exit
synproxy_proc_exit(struct net
*net
)
331 remove_proc_entry("synproxy", net
->proc_net_stat
);
334 static int __net_init
synproxy_proc_init(struct net
*net
)
339 static void __net_exit
synproxy_proc_exit(struct net
*net
)
343 #endif /* CONFIG_PROC_FS */
345 static int __net_init
synproxy_net_init(struct net
*net
)
347 struct synproxy_net
*snet
= synproxy_pernet(net
);
348 struct nf_conntrack_tuple t
;
352 memset(&t
, 0, sizeof(t
));
353 ct
= nf_conntrack_alloc(net
, 0, &t
, &t
, GFP_KERNEL
);
359 if (!nfct_seqadj_ext_add(ct
))
361 if (!nfct_synproxy_ext_add(ct
))
363 __set_bit(IPS_TEMPLATE_BIT
, &ct
->status
);
364 __set_bit(IPS_CONFIRMED_BIT
, &ct
->status
);
368 snet
->stats
= alloc_percpu(struct synproxy_stats
);
369 if (snet
->stats
== NULL
)
372 err
= synproxy_proc_init(net
);
379 free_percpu(snet
->stats
);
381 nf_conntrack_free(ct
);
386 static void __net_exit
synproxy_net_exit(struct net
*net
)
388 struct synproxy_net
*snet
= synproxy_pernet(net
);
390 nf_conntrack_free(snet
->tmpl
);
391 synproxy_proc_exit(net
);
392 free_percpu(snet
->stats
);
395 static struct pernet_operations synproxy_net_ops
= {
396 .init
= synproxy_net_init
,
397 .exit
= synproxy_net_exit
,
398 .id
= &synproxy_net_id
,
399 .size
= sizeof(struct synproxy_net
),
402 static int __init
synproxy_core_init(void)
406 err
= nf_ct_extend_register(&nf_ct_synproxy_extend
);
410 err
= register_pernet_subsys(&synproxy_net_ops
);
417 nf_ct_extend_unregister(&nf_ct_synproxy_extend
);
422 static void __exit
synproxy_core_exit(void)
424 unregister_pernet_subsys(&synproxy_net_ops
);
425 nf_ct_extend_unregister(&nf_ct_synproxy_extend
);
428 module_init(synproxy_core_init
);
429 module_exit(synproxy_core_exit
);
431 MODULE_LICENSE("GPL");
432 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");