]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/ipv6/netfilter/ip6t_SYNPROXY.c
Merge branch 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[mirror_ubuntu-artful-kernel.git] / net / ipv6 / netfilter / ip6t_SYNPROXY.c
1 /*
2 * Copyright (c) 2013 Patrick McHardy <kaber@trash.net>
3 *
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.
7 */
8
9 #include <linux/module.h>
10 #include <linux/skbuff.h>
11 #include <net/ip6_checksum.h>
12 #include <net/ip6_route.h>
13 #include <net/tcp.h>
14
15 #include <linux/netfilter_ipv6/ip6_tables.h>
16 #include <linux/netfilter/x_tables.h>
17 #include <linux/netfilter/xt_SYNPROXY.h>
18 #include <net/netfilter/nf_conntrack.h>
19 #include <net/netfilter/nf_conntrack_seqadj.h>
20 #include <net/netfilter/nf_conntrack_synproxy.h>
21
22 static struct ipv6hdr *
23 synproxy_build_ip(struct net *net, struct sk_buff *skb,
24 const struct in6_addr *saddr,
25 const struct in6_addr *daddr)
26 {
27 struct ipv6hdr *iph;
28
29 skb_reset_network_header(skb);
30 iph = (struct ipv6hdr *)skb_put(skb, sizeof(*iph));
31 ip6_flow_hdr(iph, 0, 0);
32 iph->hop_limit = net->ipv6.devconf_all->hop_limit;
33 iph->nexthdr = IPPROTO_TCP;
34 iph->saddr = *saddr;
35 iph->daddr = *daddr;
36
37 return iph;
38 }
39
40 static void
41 synproxy_send_tcp(struct net *net,
42 const struct sk_buff *skb, struct sk_buff *nskb,
43 struct nf_conntrack *nfct, enum ip_conntrack_info ctinfo,
44 struct ipv6hdr *niph, struct tcphdr *nth,
45 unsigned int tcp_hdr_size)
46 {
47 struct dst_entry *dst;
48 struct flowi6 fl6;
49
50 nth->check = ~tcp_v6_check(tcp_hdr_size, &niph->saddr, &niph->daddr, 0);
51 nskb->ip_summed = CHECKSUM_PARTIAL;
52 nskb->csum_start = (unsigned char *)nth - nskb->head;
53 nskb->csum_offset = offsetof(struct tcphdr, check);
54
55 memset(&fl6, 0, sizeof(fl6));
56 fl6.flowi6_proto = IPPROTO_TCP;
57 fl6.saddr = niph->saddr;
58 fl6.daddr = niph->daddr;
59 fl6.fl6_sport = nth->source;
60 fl6.fl6_dport = nth->dest;
61 security_skb_classify_flow((struct sk_buff *)skb, flowi6_to_flowi(&fl6));
62 dst = ip6_route_output(net, NULL, &fl6);
63 if (dst->error) {
64 dst_release(dst);
65 goto free_nskb;
66 }
67 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
68 if (IS_ERR(dst))
69 goto free_nskb;
70
71 skb_dst_set(nskb, dst);
72
73 if (nfct) {
74 nskb->nfct = nfct;
75 nskb->nfctinfo = ctinfo;
76 nf_conntrack_get(nfct);
77 }
78
79 ip6_local_out(net, nskb->sk, nskb);
80 return;
81
82 free_nskb:
83 kfree_skb(nskb);
84 }
85
86 static void
87 synproxy_send_client_synack(struct net *net,
88 const struct sk_buff *skb, const struct tcphdr *th,
89 const struct synproxy_options *opts)
90 {
91 struct sk_buff *nskb;
92 struct ipv6hdr *iph, *niph;
93 struct tcphdr *nth;
94 unsigned int tcp_hdr_size;
95 u16 mss = opts->mss;
96
97 iph = ipv6_hdr(skb);
98
99 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
100 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
101 GFP_ATOMIC);
102 if (nskb == NULL)
103 return;
104 skb_reserve(nskb, MAX_TCP_HEADER);
105
106 niph = synproxy_build_ip(net, nskb, &iph->daddr, &iph->saddr);
107
108 skb_reset_transport_header(nskb);
109 nth = (struct tcphdr *)skb_put(nskb, tcp_hdr_size);
110 nth->source = th->dest;
111 nth->dest = th->source;
112 nth->seq = htonl(__cookie_v6_init_sequence(iph, th, &mss));
113 nth->ack_seq = htonl(ntohl(th->seq) + 1);
114 tcp_flag_word(nth) = TCP_FLAG_SYN | TCP_FLAG_ACK;
115 if (opts->options & XT_SYNPROXY_OPT_ECN)
116 tcp_flag_word(nth) |= TCP_FLAG_ECE;
117 nth->doff = tcp_hdr_size / 4;
118 nth->window = 0;
119 nth->check = 0;
120 nth->urg_ptr = 0;
121
122 synproxy_build_options(nth, opts);
123
124 synproxy_send_tcp(net, skb, nskb, skb->nfct, IP_CT_ESTABLISHED_REPLY,
125 niph, nth, tcp_hdr_size);
126 }
127
128 static void
129 synproxy_send_server_syn(struct net *net,
130 const struct sk_buff *skb, const struct tcphdr *th,
131 const struct synproxy_options *opts, u32 recv_seq)
132 {
133 struct synproxy_net *snet = synproxy_pernet(net);
134 struct sk_buff *nskb;
135 struct ipv6hdr *iph, *niph;
136 struct tcphdr *nth;
137 unsigned int tcp_hdr_size;
138
139 iph = ipv6_hdr(skb);
140
141 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
142 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
143 GFP_ATOMIC);
144 if (nskb == NULL)
145 return;
146 skb_reserve(nskb, MAX_TCP_HEADER);
147
148 niph = synproxy_build_ip(net, nskb, &iph->saddr, &iph->daddr);
149
150 skb_reset_transport_header(nskb);
151 nth = (struct tcphdr *)skb_put(nskb, tcp_hdr_size);
152 nth->source = th->source;
153 nth->dest = th->dest;
154 nth->seq = htonl(recv_seq - 1);
155 /* ack_seq is used to relay our ISN to the synproxy hook to initialize
156 * sequence number translation once a connection tracking entry exists.
157 */
158 nth->ack_seq = htonl(ntohl(th->ack_seq) - 1);
159 tcp_flag_word(nth) = TCP_FLAG_SYN;
160 if (opts->options & XT_SYNPROXY_OPT_ECN)
161 tcp_flag_word(nth) |= TCP_FLAG_ECE | TCP_FLAG_CWR;
162 nth->doff = tcp_hdr_size / 4;
163 nth->window = th->window;
164 nth->check = 0;
165 nth->urg_ptr = 0;
166
167 synproxy_build_options(nth, opts);
168
169 synproxy_send_tcp(net, skb, nskb, &snet->tmpl->ct_general, IP_CT_NEW,
170 niph, nth, tcp_hdr_size);
171 }
172
173 static void
174 synproxy_send_server_ack(struct net *net,
175 const struct ip_ct_tcp *state,
176 const struct sk_buff *skb, const struct tcphdr *th,
177 const struct synproxy_options *opts)
178 {
179 struct sk_buff *nskb;
180 struct ipv6hdr *iph, *niph;
181 struct tcphdr *nth;
182 unsigned int tcp_hdr_size;
183
184 iph = ipv6_hdr(skb);
185
186 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
187 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
188 GFP_ATOMIC);
189 if (nskb == NULL)
190 return;
191 skb_reserve(nskb, MAX_TCP_HEADER);
192
193 niph = synproxy_build_ip(net, nskb, &iph->daddr, &iph->saddr);
194
195 skb_reset_transport_header(nskb);
196 nth = (struct tcphdr *)skb_put(nskb, tcp_hdr_size);
197 nth->source = th->dest;
198 nth->dest = th->source;
199 nth->seq = htonl(ntohl(th->ack_seq));
200 nth->ack_seq = htonl(ntohl(th->seq) + 1);
201 tcp_flag_word(nth) = TCP_FLAG_ACK;
202 nth->doff = tcp_hdr_size / 4;
203 nth->window = htons(state->seen[IP_CT_DIR_ORIGINAL].td_maxwin);
204 nth->check = 0;
205 nth->urg_ptr = 0;
206
207 synproxy_build_options(nth, opts);
208
209 synproxy_send_tcp(net, skb, nskb, NULL, 0, niph, nth, tcp_hdr_size);
210 }
211
212 static void
213 synproxy_send_client_ack(struct net *net,
214 const struct sk_buff *skb, const struct tcphdr *th,
215 const struct synproxy_options *opts)
216 {
217 struct sk_buff *nskb;
218 struct ipv6hdr *iph, *niph;
219 struct tcphdr *nth;
220 unsigned int tcp_hdr_size;
221
222 iph = ipv6_hdr(skb);
223
224 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
225 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
226 GFP_ATOMIC);
227 if (nskb == NULL)
228 return;
229 skb_reserve(nskb, MAX_TCP_HEADER);
230
231 niph = synproxy_build_ip(net, nskb, &iph->saddr, &iph->daddr);
232
233 skb_reset_transport_header(nskb);
234 nth = (struct tcphdr *)skb_put(nskb, tcp_hdr_size);
235 nth->source = th->source;
236 nth->dest = th->dest;
237 nth->seq = htonl(ntohl(th->seq) + 1);
238 nth->ack_seq = th->ack_seq;
239 tcp_flag_word(nth) = TCP_FLAG_ACK;
240 nth->doff = tcp_hdr_size / 4;
241 nth->window = htons(ntohs(th->window) >> opts->wscale);
242 nth->check = 0;
243 nth->urg_ptr = 0;
244
245 synproxy_build_options(nth, opts);
246
247 synproxy_send_tcp(net, skb, nskb, skb->nfct, IP_CT_ESTABLISHED_REPLY,
248 niph, nth, tcp_hdr_size);
249 }
250
251 static bool
252 synproxy_recv_client_ack(struct net *net,
253 const struct sk_buff *skb, const struct tcphdr *th,
254 struct synproxy_options *opts, u32 recv_seq)
255 {
256 struct synproxy_net *snet = synproxy_pernet(net);
257 int mss;
258
259 mss = __cookie_v6_check(ipv6_hdr(skb), th, ntohl(th->ack_seq) - 1);
260 if (mss == 0) {
261 this_cpu_inc(snet->stats->cookie_invalid);
262 return false;
263 }
264
265 this_cpu_inc(snet->stats->cookie_valid);
266 opts->mss = mss;
267 opts->options |= XT_SYNPROXY_OPT_MSS;
268
269 if (opts->options & XT_SYNPROXY_OPT_TIMESTAMP)
270 synproxy_check_timestamp_cookie(opts);
271
272 synproxy_send_server_syn(net, skb, th, opts, recv_seq);
273 return true;
274 }
275
276 static unsigned int
277 synproxy_tg6(struct sk_buff *skb, const struct xt_action_param *par)
278 {
279 const struct xt_synproxy_info *info = par->targinfo;
280 struct net *net = xt_net(par);
281 struct synproxy_net *snet = synproxy_pernet(net);
282 struct synproxy_options opts = {};
283 struct tcphdr *th, _th;
284
285 if (nf_ip6_checksum(skb, xt_hooknum(par), par->thoff, IPPROTO_TCP))
286 return NF_DROP;
287
288 th = skb_header_pointer(skb, par->thoff, sizeof(_th), &_th);
289 if (th == NULL)
290 return NF_DROP;
291
292 if (!synproxy_parse_options(skb, par->thoff, th, &opts))
293 return NF_DROP;
294
295 if (th->syn && !(th->ack || th->fin || th->rst)) {
296 /* Initial SYN from client */
297 this_cpu_inc(snet->stats->syn_received);
298
299 if (th->ece && th->cwr)
300 opts.options |= XT_SYNPROXY_OPT_ECN;
301
302 opts.options &= info->options;
303 if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP)
304 synproxy_init_timestamp_cookie(info, &opts);
305 else
306 opts.options &= ~(XT_SYNPROXY_OPT_WSCALE |
307 XT_SYNPROXY_OPT_SACK_PERM |
308 XT_SYNPROXY_OPT_ECN);
309
310 synproxy_send_client_synack(net, skb, th, &opts);
311 return NF_DROP;
312
313 } else if (th->ack && !(th->fin || th->rst || th->syn)) {
314 /* ACK from client */
315 synproxy_recv_client_ack(net, skb, th, &opts, ntohl(th->seq));
316 return NF_DROP;
317 }
318
319 return XT_CONTINUE;
320 }
321
322 static unsigned int ipv6_synproxy_hook(void *priv,
323 struct sk_buff *skb,
324 const struct nf_hook_state *nhs)
325 {
326 struct net *net = nhs->net;
327 struct synproxy_net *snet = synproxy_pernet(net);
328 enum ip_conntrack_info ctinfo;
329 struct nf_conn *ct;
330 struct nf_conn_synproxy *synproxy;
331 struct synproxy_options opts = {};
332 const struct ip_ct_tcp *state;
333 struct tcphdr *th, _th;
334 __be16 frag_off;
335 u8 nexthdr;
336 int thoff;
337
338 ct = nf_ct_get(skb, &ctinfo);
339 if (ct == NULL)
340 return NF_ACCEPT;
341
342 synproxy = nfct_synproxy(ct);
343 if (synproxy == NULL)
344 return NF_ACCEPT;
345
346 if (nf_is_loopback_packet(skb))
347 return NF_ACCEPT;
348
349 nexthdr = ipv6_hdr(skb)->nexthdr;
350 thoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
351 &frag_off);
352 if (thoff < 0)
353 return NF_ACCEPT;
354
355 th = skb_header_pointer(skb, thoff, sizeof(_th), &_th);
356 if (th == NULL)
357 return NF_DROP;
358
359 state = &ct->proto.tcp;
360 switch (state->state) {
361 case TCP_CONNTRACK_CLOSE:
362 if (th->rst && !test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
363 nf_ct_seqadj_init(ct, ctinfo, synproxy->isn -
364 ntohl(th->seq) + 1);
365 break;
366 }
367
368 if (!th->syn || th->ack ||
369 CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
370 break;
371
372 /* Reopened connection - reset the sequence number and timestamp
373 * adjustments, they will get initialized once the connection is
374 * reestablished.
375 */
376 nf_ct_seqadj_init(ct, ctinfo, 0);
377 synproxy->tsoff = 0;
378 this_cpu_inc(snet->stats->conn_reopened);
379
380 /* fall through */
381 case TCP_CONNTRACK_SYN_SENT:
382 if (!synproxy_parse_options(skb, thoff, th, &opts))
383 return NF_DROP;
384
385 if (!th->syn && th->ack &&
386 CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) {
387 /* Keep-Alives are sent with SEG.SEQ = SND.NXT-1,
388 * therefore we need to add 1 to make the SYN sequence
389 * number match the one of first SYN.
390 */
391 if (synproxy_recv_client_ack(net, skb, th, &opts,
392 ntohl(th->seq) + 1))
393 this_cpu_inc(snet->stats->cookie_retrans);
394
395 return NF_DROP;
396 }
397
398 synproxy->isn = ntohl(th->ack_seq);
399 if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP)
400 synproxy->its = opts.tsecr;
401 break;
402 case TCP_CONNTRACK_SYN_RECV:
403 if (!th->syn || !th->ack)
404 break;
405
406 if (!synproxy_parse_options(skb, thoff, th, &opts))
407 return NF_DROP;
408
409 if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP)
410 synproxy->tsoff = opts.tsval - synproxy->its;
411
412 opts.options &= ~(XT_SYNPROXY_OPT_MSS |
413 XT_SYNPROXY_OPT_WSCALE |
414 XT_SYNPROXY_OPT_SACK_PERM);
415
416 swap(opts.tsval, opts.tsecr);
417 synproxy_send_server_ack(net, state, skb, th, &opts);
418
419 nf_ct_seqadj_init(ct, ctinfo, synproxy->isn - ntohl(th->seq));
420
421 swap(opts.tsval, opts.tsecr);
422 synproxy_send_client_ack(net, skb, th, &opts);
423
424 consume_skb(skb);
425 return NF_STOLEN;
426 default:
427 break;
428 }
429
430 synproxy_tstamp_adjust(skb, thoff, th, ct, ctinfo, synproxy);
431 return NF_ACCEPT;
432 }
433
434 static int synproxy_tg6_check(const struct xt_tgchk_param *par)
435 {
436 const struct ip6t_entry *e = par->entryinfo;
437
438 if (!(e->ipv6.flags & IP6T_F_PROTO) ||
439 e->ipv6.proto != IPPROTO_TCP ||
440 e->ipv6.invflags & XT_INV_PROTO)
441 return -EINVAL;
442
443 return nf_ct_netns_get(par->net, par->family);
444 }
445
446 static void synproxy_tg6_destroy(const struct xt_tgdtor_param *par)
447 {
448 nf_ct_netns_put(par->net, par->family);
449 }
450
451 static struct xt_target synproxy_tg6_reg __read_mostly = {
452 .name = "SYNPROXY",
453 .family = NFPROTO_IPV6,
454 .hooks = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD),
455 .target = synproxy_tg6,
456 .targetsize = sizeof(struct xt_synproxy_info),
457 .checkentry = synproxy_tg6_check,
458 .destroy = synproxy_tg6_destroy,
459 .me = THIS_MODULE,
460 };
461
462 static struct nf_hook_ops ipv6_synproxy_ops[] __read_mostly = {
463 {
464 .hook = ipv6_synproxy_hook,
465 .pf = NFPROTO_IPV6,
466 .hooknum = NF_INET_LOCAL_IN,
467 .priority = NF_IP_PRI_CONNTRACK_CONFIRM - 1,
468 },
469 {
470 .hook = ipv6_synproxy_hook,
471 .pf = NFPROTO_IPV6,
472 .hooknum = NF_INET_POST_ROUTING,
473 .priority = NF_IP_PRI_CONNTRACK_CONFIRM - 1,
474 },
475 };
476
477 static int __init synproxy_tg6_init(void)
478 {
479 int err;
480
481 err = nf_register_hooks(ipv6_synproxy_ops,
482 ARRAY_SIZE(ipv6_synproxy_ops));
483 if (err < 0)
484 goto err1;
485
486 err = xt_register_target(&synproxy_tg6_reg);
487 if (err < 0)
488 goto err2;
489
490 return 0;
491
492 err2:
493 nf_unregister_hooks(ipv6_synproxy_ops, ARRAY_SIZE(ipv6_synproxy_ops));
494 err1:
495 return err;
496 }
497
498 static void __exit synproxy_tg6_exit(void)
499 {
500 xt_unregister_target(&synproxy_tg6_reg);
501 nf_unregister_hooks(ipv6_synproxy_ops, ARRAY_SIZE(ipv6_synproxy_ops));
502 }
503
504 module_init(synproxy_tg6_init);
505 module_exit(synproxy_tg6_exit);
506
507 MODULE_LICENSE("GPL");
508 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");