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
);
58 if (opsize
== TCPOLEN_MSS
) {
59 opts
->mss
= get_unaligned_be16(ptr
);
60 opts
->options
|= XT_SYNPROXY_OPT_MSS
;
64 if (opsize
== TCPOLEN_WINDOW
) {
66 if (opts
->wscale
> 14)
68 opts
->options
|= XT_SYNPROXY_OPT_WSCALE
;
71 case TCPOPT_TIMESTAMP
:
72 if (opsize
== TCPOLEN_TIMESTAMP
) {
73 opts
->tsval
= get_unaligned_be32(ptr
);
74 opts
->tsecr
= get_unaligned_be32(ptr
+ 4);
75 opts
->options
|= XT_SYNPROXY_OPT_TIMESTAMP
;
78 case TCPOPT_SACK_PERM
:
79 if (opsize
== TCPOLEN_SACK_PERM
)
80 opts
->options
|= XT_SYNPROXY_OPT_SACK_PERM
;
90 EXPORT_SYMBOL_GPL(synproxy_parse_options
);
92 unsigned int synproxy_options_size(const struct synproxy_options
*opts
)
94 unsigned int size
= 0;
96 if (opts
->options
& XT_SYNPROXY_OPT_MSS
)
97 size
+= TCPOLEN_MSS_ALIGNED
;
98 if (opts
->options
& XT_SYNPROXY_OPT_TIMESTAMP
)
99 size
+= TCPOLEN_TSTAMP_ALIGNED
;
100 else if (opts
->options
& XT_SYNPROXY_OPT_SACK_PERM
)
101 size
+= TCPOLEN_SACKPERM_ALIGNED
;
102 if (opts
->options
& XT_SYNPROXY_OPT_WSCALE
)
103 size
+= TCPOLEN_WSCALE_ALIGNED
;
107 EXPORT_SYMBOL_GPL(synproxy_options_size
);
110 synproxy_build_options(struct tcphdr
*th
, const struct synproxy_options
*opts
)
112 __be32
*ptr
= (__be32
*)(th
+ 1);
113 u8 options
= opts
->options
;
115 if (options
& XT_SYNPROXY_OPT_MSS
)
116 *ptr
++ = htonl((TCPOPT_MSS
<< 24) |
117 (TCPOLEN_MSS
<< 16) |
120 if (options
& XT_SYNPROXY_OPT_TIMESTAMP
) {
121 if (options
& XT_SYNPROXY_OPT_SACK_PERM
)
122 *ptr
++ = htonl((TCPOPT_SACK_PERM
<< 24) |
123 (TCPOLEN_SACK_PERM
<< 16) |
124 (TCPOPT_TIMESTAMP
<< 8) |
127 *ptr
++ = htonl((TCPOPT_NOP
<< 24) |
129 (TCPOPT_TIMESTAMP
<< 8) |
132 *ptr
++ = htonl(opts
->tsval
);
133 *ptr
++ = htonl(opts
->tsecr
);
134 } else if (options
& XT_SYNPROXY_OPT_SACK_PERM
)
135 *ptr
++ = htonl((TCPOPT_NOP
<< 24) |
137 (TCPOPT_SACK_PERM
<< 8) |
140 if (options
& XT_SYNPROXY_OPT_WSCALE
)
141 *ptr
++ = htonl((TCPOPT_NOP
<< 24) |
142 (TCPOPT_WINDOW
<< 16) |
143 (TCPOLEN_WINDOW
<< 8) |
146 EXPORT_SYMBOL_GPL(synproxy_build_options
);
148 void synproxy_init_timestamp_cookie(const struct xt_synproxy_info
*info
,
149 struct synproxy_options
*opts
)
151 opts
->tsecr
= opts
->tsval
;
152 opts
->tsval
= tcp_time_stamp
& ~0x3f;
154 if (opts
->options
& XT_SYNPROXY_OPT_WSCALE
) {
155 opts
->tsval
|= opts
->wscale
;
156 opts
->wscale
= info
->wscale
;
160 if (opts
->options
& XT_SYNPROXY_OPT_SACK_PERM
)
161 opts
->tsval
|= 1 << 4;
163 if (opts
->options
& XT_SYNPROXY_OPT_ECN
)
164 opts
->tsval
|= 1 << 5;
166 EXPORT_SYMBOL_GPL(synproxy_init_timestamp_cookie
);
168 void synproxy_check_timestamp_cookie(struct synproxy_options
*opts
)
170 opts
->wscale
= opts
->tsecr
& 0xf;
171 if (opts
->wscale
!= 0xf)
172 opts
->options
|= XT_SYNPROXY_OPT_WSCALE
;
174 opts
->options
|= opts
->tsecr
& (1 << 4) ? XT_SYNPROXY_OPT_SACK_PERM
: 0;
176 opts
->options
|= opts
->tsecr
& (1 << 5) ? XT_SYNPROXY_OPT_ECN
: 0;
178 EXPORT_SYMBOL_GPL(synproxy_check_timestamp_cookie
);
180 unsigned int synproxy_tstamp_adjust(struct sk_buff
*skb
,
181 unsigned int protoff
,
184 enum ip_conntrack_info ctinfo
,
185 const struct nf_conn_synproxy
*synproxy
)
187 unsigned int optoff
, optend
;
190 if (synproxy
->tsoff
== 0)
193 optoff
= protoff
+ sizeof(struct tcphdr
);
194 optend
= protoff
+ th
->doff
* 4;
196 if (!skb_make_writable(skb
, optend
))
199 while (optoff
< optend
) {
200 unsigned char *op
= skb
->data
+ optoff
;
209 if (optoff
+ 1 == optend
||
210 optoff
+ op
[1] > optend
||
213 if (op
[0] == TCPOPT_TIMESTAMP
&&
214 op
[1] == TCPOLEN_TIMESTAMP
) {
215 if (CTINFO2DIR(ctinfo
) == IP_CT_DIR_REPLY
) {
218 *ptr
= htonl(ntohl(*ptr
) -
223 *ptr
= htonl(ntohl(*ptr
) +
226 inet_proto_csum_replace4(&th
->check
, skb
,
235 EXPORT_SYMBOL_GPL(synproxy_tstamp_adjust
);
237 static struct nf_ct_ext_type nf_ct_synproxy_extend __read_mostly
= {
238 .len
= sizeof(struct nf_conn_synproxy
),
239 .align
= __alignof__(struct nf_conn_synproxy
),
240 .id
= NF_CT_EXT_SYNPROXY
,
243 #ifdef CONFIG_PROC_FS
244 static void *synproxy_cpu_seq_start(struct seq_file
*seq
, loff_t
*pos
)
246 struct synproxy_net
*snet
= synproxy_pernet(seq_file_net(seq
));
250 return SEQ_START_TOKEN
;
252 for (cpu
= *pos
- 1; cpu
< nr_cpu_ids
; cpu
++) {
253 if (!cpu_possible(cpu
))
256 return per_cpu_ptr(snet
->stats
, cpu
);
262 static void *synproxy_cpu_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
264 struct synproxy_net
*snet
= synproxy_pernet(seq_file_net(seq
));
267 for (cpu
= *pos
; cpu
< nr_cpu_ids
; cpu
++) {
268 if (!cpu_possible(cpu
))
271 return per_cpu_ptr(snet
->stats
, cpu
);
277 static void synproxy_cpu_seq_stop(struct seq_file
*seq
, void *v
)
282 static int synproxy_cpu_seq_show(struct seq_file
*seq
, void *v
)
284 struct synproxy_stats
*stats
= v
;
286 if (v
== SEQ_START_TOKEN
) {
287 seq_printf(seq
, "entries\t\tsyn_received\t"
288 "cookie_invalid\tcookie_valid\t"
289 "cookie_retrans\tconn_reopened\n");
293 seq_printf(seq
, "%08x\t%08x\t%08x\t%08x\t%08x\t%08x\n", 0,
295 stats
->cookie_invalid
,
297 stats
->cookie_retrans
,
298 stats
->conn_reopened
);
303 static const struct seq_operations synproxy_cpu_seq_ops
= {
304 .start
= synproxy_cpu_seq_start
,
305 .next
= synproxy_cpu_seq_next
,
306 .stop
= synproxy_cpu_seq_stop
,
307 .show
= synproxy_cpu_seq_show
,
310 static int synproxy_cpu_seq_open(struct inode
*inode
, struct file
*file
)
312 return seq_open_net(inode
, file
, &synproxy_cpu_seq_ops
,
313 sizeof(struct seq_net_private
));
316 static const struct file_operations synproxy_cpu_seq_fops
= {
317 .owner
= THIS_MODULE
,
318 .open
= synproxy_cpu_seq_open
,
321 .release
= seq_release_net
,
324 static int __net_init
synproxy_proc_init(struct net
*net
)
326 if (!proc_create("synproxy", S_IRUGO
, net
->proc_net_stat
,
327 &synproxy_cpu_seq_fops
))
332 static void __net_exit
synproxy_proc_exit(struct net
*net
)
334 remove_proc_entry("synproxy", net
->proc_net_stat
);
337 static int __net_init
synproxy_proc_init(struct net
*net
)
342 static void __net_exit
synproxy_proc_exit(struct net
*net
)
346 #endif /* CONFIG_PROC_FS */
348 static int __net_init
synproxy_net_init(struct net
*net
)
350 struct synproxy_net
*snet
= synproxy_pernet(net
);
351 struct nf_conntrack_tuple t
;
355 memset(&t
, 0, sizeof(t
));
356 ct
= nf_conntrack_alloc(net
, 0, &t
, &t
, GFP_KERNEL
);
362 if (!nfct_seqadj_ext_add(ct
))
364 if (!nfct_synproxy_ext_add(ct
))
367 nf_conntrack_tmpl_insert(net
, ct
);
370 snet
->stats
= alloc_percpu(struct synproxy_stats
);
371 if (snet
->stats
== NULL
)
374 err
= synproxy_proc_init(net
);
381 free_percpu(snet
->stats
);
383 nf_conntrack_free(ct
);
388 static void __net_exit
synproxy_net_exit(struct net
*net
)
390 struct synproxy_net
*snet
= synproxy_pernet(net
);
392 nf_ct_put(snet
->tmpl
);
393 synproxy_proc_exit(net
);
394 free_percpu(snet
->stats
);
397 static struct pernet_operations synproxy_net_ops
= {
398 .init
= synproxy_net_init
,
399 .exit
= synproxy_net_exit
,
400 .id
= &synproxy_net_id
,
401 .size
= sizeof(struct synproxy_net
),
404 static int __init
synproxy_core_init(void)
408 err
= nf_ct_extend_register(&nf_ct_synproxy_extend
);
412 err
= register_pernet_subsys(&synproxy_net_ops
);
419 nf_ct_extend_unregister(&nf_ct_synproxy_extend
);
424 static void __exit
synproxy_core_exit(void)
426 unregister_pernet_subsys(&synproxy_net_ops
);
427 nf_ct_extend_unregister(&nf_ct_synproxy_extend
);
430 module_init(synproxy_core_init
);
431 module_exit(synproxy_core_exit
);
433 MODULE_LICENSE("GPL");
434 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");