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