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
|= info
->wscale
;
159 if (opts
->options
& XT_SYNPROXY_OPT_SACK_PERM
)
160 opts
->tsval
|= 1 << 4;
162 if (opts
->options
& XT_SYNPROXY_OPT_ECN
)
163 opts
->tsval
|= 1 << 5;
165 EXPORT_SYMBOL_GPL(synproxy_init_timestamp_cookie
);
167 void synproxy_check_timestamp_cookie(struct synproxy_options
*opts
)
169 opts
->wscale
= opts
->tsecr
& 0xf;
170 if (opts
->wscale
!= 0xf)
171 opts
->options
|= XT_SYNPROXY_OPT_WSCALE
;
173 opts
->options
|= opts
->tsecr
& (1 << 4) ? XT_SYNPROXY_OPT_SACK_PERM
: 0;
175 opts
->options
|= opts
->tsecr
& (1 << 5) ? XT_SYNPROXY_OPT_ECN
: 0;
177 EXPORT_SYMBOL_GPL(synproxy_check_timestamp_cookie
);
179 unsigned int synproxy_tstamp_adjust(struct sk_buff
*skb
,
180 unsigned int protoff
,
183 enum ip_conntrack_info ctinfo
,
184 const struct nf_conn_synproxy
*synproxy
)
186 unsigned int optoff
, optend
;
189 if (synproxy
->tsoff
== 0)
192 optoff
= protoff
+ sizeof(struct tcphdr
);
193 optend
= protoff
+ th
->doff
* 4;
195 if (!skb_make_writable(skb
, optend
))
198 while (optoff
< optend
) {
199 unsigned char *op
= skb
->data
+ optoff
;
208 if (optoff
+ 1 == optend
||
209 optoff
+ op
[1] > optend
||
212 if (op
[0] == TCPOPT_TIMESTAMP
&&
213 op
[1] == TCPOLEN_TIMESTAMP
) {
214 if (CTINFO2DIR(ctinfo
) == IP_CT_DIR_REPLY
) {
217 *ptr
= htonl(ntohl(*ptr
) -
222 *ptr
= htonl(ntohl(*ptr
) +
225 inet_proto_csum_replace4(&th
->check
, skb
,
234 EXPORT_SYMBOL_GPL(synproxy_tstamp_adjust
);
236 static struct nf_ct_ext_type nf_ct_synproxy_extend __read_mostly
= {
237 .len
= sizeof(struct nf_conn_synproxy
),
238 .align
= __alignof__(struct nf_conn_synproxy
),
239 .id
= NF_CT_EXT_SYNPROXY
,
242 #ifdef CONFIG_PROC_FS
243 static void *synproxy_cpu_seq_start(struct seq_file
*seq
, loff_t
*pos
)
245 struct synproxy_net
*snet
= synproxy_pernet(seq_file_net(seq
));
249 return SEQ_START_TOKEN
;
251 for (cpu
= *pos
- 1; cpu
< nr_cpu_ids
; cpu
++) {
252 if (!cpu_possible(cpu
))
255 return per_cpu_ptr(snet
->stats
, cpu
);
261 static void *synproxy_cpu_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
263 struct synproxy_net
*snet
= synproxy_pernet(seq_file_net(seq
));
266 for (cpu
= *pos
; cpu
< nr_cpu_ids
; cpu
++) {
267 if (!cpu_possible(cpu
))
270 return per_cpu_ptr(snet
->stats
, cpu
);
276 static void synproxy_cpu_seq_stop(struct seq_file
*seq
, void *v
)
281 static int synproxy_cpu_seq_show(struct seq_file
*seq
, void *v
)
283 struct synproxy_stats
*stats
= v
;
285 if (v
== SEQ_START_TOKEN
) {
286 seq_printf(seq
, "entries\t\tsyn_received\t"
287 "cookie_invalid\tcookie_valid\t"
288 "cookie_retrans\tconn_reopened\n");
292 seq_printf(seq
, "%08x\t%08x\t%08x\t%08x\t%08x\t%08x\n", 0,
294 stats
->cookie_invalid
,
296 stats
->cookie_retrans
,
297 stats
->conn_reopened
);
302 static const struct seq_operations synproxy_cpu_seq_ops
= {
303 .start
= synproxy_cpu_seq_start
,
304 .next
= synproxy_cpu_seq_next
,
305 .stop
= synproxy_cpu_seq_stop
,
306 .show
= synproxy_cpu_seq_show
,
309 static int synproxy_cpu_seq_open(struct inode
*inode
, struct file
*file
)
311 return seq_open_net(inode
, file
, &synproxy_cpu_seq_ops
,
312 sizeof(struct seq_net_private
));
315 static const struct file_operations synproxy_cpu_seq_fops
= {
316 .owner
= THIS_MODULE
,
317 .open
= synproxy_cpu_seq_open
,
320 .release
= seq_release_net
,
323 static int __net_init
synproxy_proc_init(struct net
*net
)
325 if (!proc_create("synproxy", S_IRUGO
, net
->proc_net_stat
,
326 &synproxy_cpu_seq_fops
))
331 static void __net_exit
synproxy_proc_exit(struct net
*net
)
333 remove_proc_entry("synproxy", net
->proc_net_stat
);
336 static int __net_init
synproxy_proc_init(struct net
*net
)
341 static void __net_exit
synproxy_proc_exit(struct net
*net
)
345 #endif /* CONFIG_PROC_FS */
347 static int __net_init
synproxy_net_init(struct net
*net
)
349 struct synproxy_net
*snet
= synproxy_pernet(net
);
350 struct nf_conntrack_tuple t
;
354 memset(&t
, 0, sizeof(t
));
355 ct
= nf_conntrack_alloc(net
, 0, &t
, &t
, GFP_KERNEL
);
361 if (!nfct_seqadj_ext_add(ct
))
363 if (!nfct_synproxy_ext_add(ct
))
365 __set_bit(IPS_TEMPLATE_BIT
, &ct
->status
);
366 __set_bit(IPS_CONFIRMED_BIT
, &ct
->status
);
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_conntrack_free(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>");