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>
14 #include <linux/proc_fs.h>
16 #include <linux/netfilter_ipv4/ip_tables.h>
17 #include <linux/netfilter/x_tables.h>
18 #include <linux/netfilter/xt_tcpudp.h>
19 #include <linux/netfilter/xt_SYNPROXY.h>
20 #include <net/netfilter/nf_conntrack.h>
21 #include <net/netfilter/nf_conntrack_extend.h>
22 #include <net/netfilter/nf_conntrack_seqadj.h>
23 #include <net/netfilter/nf_conntrack_synproxy.h>
26 EXPORT_SYMBOL_GPL(synproxy_net_id
);
29 synproxy_parse_options(const struct sk_buff
*skb
, unsigned int doff
,
30 const struct tcphdr
*th
, struct synproxy_options
*opts
)
32 int length
= (th
->doff
* 4) - sizeof(*th
);
35 ptr
= skb_header_pointer(skb
, doff
+ sizeof(*th
), length
, buf
);
59 if (opsize
== TCPOLEN_MSS
) {
60 opts
->mss
= get_unaligned_be16(ptr
);
61 opts
->options
|= XT_SYNPROXY_OPT_MSS
;
65 if (opsize
== TCPOLEN_WINDOW
) {
67 if (opts
->wscale
> 14)
69 opts
->options
|= XT_SYNPROXY_OPT_WSCALE
;
72 case TCPOPT_TIMESTAMP
:
73 if (opsize
== TCPOLEN_TIMESTAMP
) {
74 opts
->tsval
= get_unaligned_be32(ptr
);
75 opts
->tsecr
= get_unaligned_be32(ptr
+ 4);
76 opts
->options
|= XT_SYNPROXY_OPT_TIMESTAMP
;
79 case TCPOPT_SACK_PERM
:
80 if (opsize
== TCPOLEN_SACK_PERM
)
81 opts
->options
|= XT_SYNPROXY_OPT_SACK_PERM
;
91 EXPORT_SYMBOL_GPL(synproxy_parse_options
);
93 unsigned int synproxy_options_size(const struct synproxy_options
*opts
)
95 unsigned int size
= 0;
97 if (opts
->options
& XT_SYNPROXY_OPT_MSS
)
98 size
+= TCPOLEN_MSS_ALIGNED
;
99 if (opts
->options
& XT_SYNPROXY_OPT_TIMESTAMP
)
100 size
+= TCPOLEN_TSTAMP_ALIGNED
;
101 else if (opts
->options
& XT_SYNPROXY_OPT_SACK_PERM
)
102 size
+= TCPOLEN_SACKPERM_ALIGNED
;
103 if (opts
->options
& XT_SYNPROXY_OPT_WSCALE
)
104 size
+= TCPOLEN_WSCALE_ALIGNED
;
108 EXPORT_SYMBOL_GPL(synproxy_options_size
);
111 synproxy_build_options(struct tcphdr
*th
, const struct synproxy_options
*opts
)
113 __be32
*ptr
= (__be32
*)(th
+ 1);
114 u8 options
= opts
->options
;
116 if (options
& XT_SYNPROXY_OPT_MSS
)
117 *ptr
++ = htonl((TCPOPT_MSS
<< 24) |
118 (TCPOLEN_MSS
<< 16) |
121 if (options
& XT_SYNPROXY_OPT_TIMESTAMP
) {
122 if (options
& XT_SYNPROXY_OPT_SACK_PERM
)
123 *ptr
++ = htonl((TCPOPT_SACK_PERM
<< 24) |
124 (TCPOLEN_SACK_PERM
<< 16) |
125 (TCPOPT_TIMESTAMP
<< 8) |
128 *ptr
++ = htonl((TCPOPT_NOP
<< 24) |
130 (TCPOPT_TIMESTAMP
<< 8) |
133 *ptr
++ = htonl(opts
->tsval
);
134 *ptr
++ = htonl(opts
->tsecr
);
135 } else if (options
& XT_SYNPROXY_OPT_SACK_PERM
)
136 *ptr
++ = htonl((TCPOPT_NOP
<< 24) |
138 (TCPOPT_SACK_PERM
<< 8) |
141 if (options
& XT_SYNPROXY_OPT_WSCALE
)
142 *ptr
++ = htonl((TCPOPT_NOP
<< 24) |
143 (TCPOPT_WINDOW
<< 16) |
144 (TCPOLEN_WINDOW
<< 8) |
147 EXPORT_SYMBOL_GPL(synproxy_build_options
);
149 void synproxy_init_timestamp_cookie(const struct xt_synproxy_info
*info
,
150 struct synproxy_options
*opts
)
152 opts
->tsecr
= opts
->tsval
;
153 opts
->tsval
= tcp_time_stamp
& ~0x3f;
155 if (opts
->options
& XT_SYNPROXY_OPT_WSCALE
) {
156 opts
->tsval
|= opts
->wscale
;
157 opts
->wscale
= info
->wscale
;
161 if (opts
->options
& XT_SYNPROXY_OPT_SACK_PERM
)
162 opts
->tsval
|= 1 << 4;
164 if (opts
->options
& XT_SYNPROXY_OPT_ECN
)
165 opts
->tsval
|= 1 << 5;
167 EXPORT_SYMBOL_GPL(synproxy_init_timestamp_cookie
);
169 void synproxy_check_timestamp_cookie(struct synproxy_options
*opts
)
171 opts
->wscale
= opts
->tsecr
& 0xf;
172 if (opts
->wscale
!= 0xf)
173 opts
->options
|= XT_SYNPROXY_OPT_WSCALE
;
175 opts
->options
|= opts
->tsecr
& (1 << 4) ? XT_SYNPROXY_OPT_SACK_PERM
: 0;
177 opts
->options
|= opts
->tsecr
& (1 << 5) ? XT_SYNPROXY_OPT_ECN
: 0;
179 EXPORT_SYMBOL_GPL(synproxy_check_timestamp_cookie
);
181 unsigned int synproxy_tstamp_adjust(struct sk_buff
*skb
,
182 unsigned int protoff
,
185 enum ip_conntrack_info ctinfo
,
186 const struct nf_conn_synproxy
*synproxy
)
188 unsigned int optoff
, optend
;
191 if (synproxy
->tsoff
== 0)
194 optoff
= protoff
+ sizeof(struct tcphdr
);
195 optend
= protoff
+ th
->doff
* 4;
197 if (!skb_make_writable(skb
, optend
))
200 while (optoff
< optend
) {
201 unsigned char *op
= skb
->data
+ optoff
;
210 if (optoff
+ 1 == optend
||
211 optoff
+ op
[1] > optend
||
214 if (op
[0] == TCPOPT_TIMESTAMP
&&
215 op
[1] == TCPOLEN_TIMESTAMP
) {
216 if (CTINFO2DIR(ctinfo
) == IP_CT_DIR_REPLY
) {
219 *ptr
= htonl(ntohl(*ptr
) -
224 *ptr
= htonl(ntohl(*ptr
) +
227 inet_proto_csum_replace4(&th
->check
, skb
,
236 EXPORT_SYMBOL_GPL(synproxy_tstamp_adjust
);
238 static struct nf_ct_ext_type nf_ct_synproxy_extend __read_mostly
= {
239 .len
= sizeof(struct nf_conn_synproxy
),
240 .align
= __alignof__(struct nf_conn_synproxy
),
241 .id
= NF_CT_EXT_SYNPROXY
,
244 #ifdef CONFIG_PROC_FS
245 static void *synproxy_cpu_seq_start(struct seq_file
*seq
, loff_t
*pos
)
247 struct synproxy_net
*snet
= synproxy_pernet(seq_file_net(seq
));
251 return SEQ_START_TOKEN
;
253 for (cpu
= *pos
- 1; cpu
< nr_cpu_ids
; cpu
++) {
254 if (!cpu_possible(cpu
))
257 return per_cpu_ptr(snet
->stats
, cpu
);
263 static void *synproxy_cpu_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
265 struct synproxy_net
*snet
= synproxy_pernet(seq_file_net(seq
));
268 for (cpu
= *pos
; cpu
< nr_cpu_ids
; cpu
++) {
269 if (!cpu_possible(cpu
))
272 return per_cpu_ptr(snet
->stats
, cpu
);
278 static void synproxy_cpu_seq_stop(struct seq_file
*seq
, void *v
)
283 static int synproxy_cpu_seq_show(struct seq_file
*seq
, void *v
)
285 struct synproxy_stats
*stats
= v
;
287 if (v
== SEQ_START_TOKEN
) {
288 seq_printf(seq
, "entries\t\tsyn_received\t"
289 "cookie_invalid\tcookie_valid\t"
290 "cookie_retrans\tconn_reopened\n");
294 seq_printf(seq
, "%08x\t%08x\t%08x\t%08x\t%08x\t%08x\n", 0,
296 stats
->cookie_invalid
,
298 stats
->cookie_retrans
,
299 stats
->conn_reopened
);
304 static const struct seq_operations synproxy_cpu_seq_ops
= {
305 .start
= synproxy_cpu_seq_start
,
306 .next
= synproxy_cpu_seq_next
,
307 .stop
= synproxy_cpu_seq_stop
,
308 .show
= synproxy_cpu_seq_show
,
311 static int synproxy_cpu_seq_open(struct inode
*inode
, struct file
*file
)
313 return seq_open_net(inode
, file
, &synproxy_cpu_seq_ops
,
314 sizeof(struct seq_net_private
));
317 static const struct file_operations synproxy_cpu_seq_fops
= {
318 .owner
= THIS_MODULE
,
319 .open
= synproxy_cpu_seq_open
,
322 .release
= seq_release_net
,
325 static int __net_init
synproxy_proc_init(struct net
*net
)
327 if (!proc_create("synproxy", S_IRUGO
, net
->proc_net_stat
,
328 &synproxy_cpu_seq_fops
))
333 static void __net_exit
synproxy_proc_exit(struct net
*net
)
335 remove_proc_entry("synproxy", net
->proc_net_stat
);
338 static int __net_init
synproxy_proc_init(struct net
*net
)
343 static void __net_exit
synproxy_proc_exit(struct net
*net
)
347 #endif /* CONFIG_PROC_FS */
349 static int __net_init
synproxy_net_init(struct net
*net
)
351 struct synproxy_net
*snet
= synproxy_pernet(net
);
355 ct
= nf_ct_tmpl_alloc(net
, 0, GFP_KERNEL
);
359 if (!nfct_seqadj_ext_add(ct
))
361 if (!nfct_synproxy_ext_add(ct
))
364 __set_bit(IPS_CONFIRMED_BIT
, &ct
->status
);
365 nf_conntrack_get(&ct
->ct_general
);
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_ct_put(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>");