Merge tag 'v3.10.95' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / ipv6 / netfilter / nf_conntrack_reasm.c
1 /*
2 * IPv6 fragment reassembly for connection tracking
3 *
4 * Copyright (C)2004 USAGI/WIDE Project
5 *
6 * Author:
7 * Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
8 *
9 * Based on: net/ipv6/reassembly.c
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 */
16
17 #define pr_fmt(fmt) "IPv6-nf: " fmt
18
19 #include <linux/errno.h>
20 #include <linux/types.h>
21 #include <linux/string.h>
22 #include <linux/socket.h>
23 #include <linux/sockios.h>
24 #include <linux/jiffies.h>
25 #include <linux/net.h>
26 #include <linux/list.h>
27 #include <linux/netdevice.h>
28 #include <linux/in6.h>
29 #include <linux/ipv6.h>
30 #include <linux/icmpv6.h>
31 #include <linux/random.h>
32 #include <linux/slab.h>
33
34 #include <net/sock.h>
35 #include <net/snmp.h>
36 #include <net/inet_frag.h>
37
38 #include <net/ipv6.h>
39 #include <net/protocol.h>
40 #include <net/transp_v6.h>
41 #include <net/rawv6.h>
42 #include <net/ndisc.h>
43 #include <net/addrconf.h>
44 #include <net/inet_ecn.h>
45 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
46 #include <linux/sysctl.h>
47 #include <linux/netfilter.h>
48 #include <linux/netfilter_ipv6.h>
49 #include <linux/kernel.h>
50 #include <linux/module.h>
51 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
52
53
54 struct nf_ct_frag6_skb_cb
55 {
56 struct inet6_skb_parm h;
57 int offset;
58 struct sk_buff *orig;
59 };
60
61 #define NFCT_FRAG6_CB(skb) ((struct nf_ct_frag6_skb_cb*)((skb)->cb))
62
63 static struct inet_frags nf_frags;
64
65 #ifdef CONFIG_SYSCTL
66 static struct ctl_table nf_ct_frag6_sysctl_table[] = {
67 {
68 .procname = "nf_conntrack_frag6_timeout",
69 .data = &init_net.nf_frag.frags.timeout,
70 .maxlen = sizeof(unsigned int),
71 .mode = 0644,
72 .proc_handler = proc_dointvec_jiffies,
73 },
74 {
75 .procname = "nf_conntrack_frag6_low_thresh",
76 .data = &init_net.nf_frag.frags.low_thresh,
77 .maxlen = sizeof(unsigned int),
78 .mode = 0644,
79 .proc_handler = proc_dointvec,
80 },
81 {
82 .procname = "nf_conntrack_frag6_high_thresh",
83 .data = &init_net.nf_frag.frags.high_thresh,
84 .maxlen = sizeof(unsigned int),
85 .mode = 0644,
86 .proc_handler = proc_dointvec,
87 },
88 { }
89 };
90
91 static int nf_ct_frag6_sysctl_register(struct net *net)
92 {
93 struct ctl_table *table;
94 struct ctl_table_header *hdr;
95
96 table = nf_ct_frag6_sysctl_table;
97 if (!net_eq(net, &init_net)) {
98 table = kmemdup(table, sizeof(nf_ct_frag6_sysctl_table),
99 GFP_KERNEL);
100 if (table == NULL)
101 goto err_alloc;
102
103 table[0].data = &net->nf_frag.frags.timeout;
104 table[1].data = &net->nf_frag.frags.low_thresh;
105 table[2].data = &net->nf_frag.frags.high_thresh;
106 }
107
108 hdr = register_net_sysctl(net, "net/netfilter", table);
109 if (hdr == NULL)
110 goto err_reg;
111
112 net->nf_frag.sysctl.frags_hdr = hdr;
113 return 0;
114
115 err_reg:
116 if (!net_eq(net, &init_net))
117 kfree(table);
118 err_alloc:
119 return -ENOMEM;
120 }
121
122 static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net)
123 {
124 struct ctl_table *table;
125
126 table = net->nf_frag.sysctl.frags_hdr->ctl_table_arg;
127 unregister_net_sysctl_table(net->nf_frag.sysctl.frags_hdr);
128 if (!net_eq(net, &init_net))
129 kfree(table);
130 }
131
132 #else
133 static int nf_ct_frag6_sysctl_register(struct net *net)
134 {
135 return 0;
136 }
137 static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net)
138 {
139 }
140 #endif
141
142 static inline u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
143 {
144 return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
145 }
146
147 static unsigned int nf_hashfn(struct inet_frag_queue *q)
148 {
149 const struct frag_queue *nq;
150
151 nq = container_of(q, struct frag_queue, q);
152 return inet6_hash_frag(nq->id, &nq->saddr, &nq->daddr, nf_frags.rnd);
153 }
154
155 static void nf_skb_free(struct sk_buff *skb)
156 {
157 if (NFCT_FRAG6_CB(skb)->orig)
158 kfree_skb(NFCT_FRAG6_CB(skb)->orig);
159 }
160
161 static void nf_ct_frag6_expire(unsigned long data)
162 {
163 struct frag_queue *fq;
164 struct net *net;
165
166 fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q);
167 net = container_of(fq->q.net, struct net, nf_frag.frags);
168
169 ip6_expire_frag_queue(net, fq, &nf_frags);
170 }
171
172 /* Creation primitives. */
173 static inline struct frag_queue *fq_find(struct net *net, __be32 id,
174 u32 user, struct in6_addr *src,
175 struct in6_addr *dst, int iif, u8 ecn)
176 {
177 struct inet_frag_queue *q;
178 struct ip6_create_arg arg;
179 unsigned int hash;
180
181 arg.id = id;
182 arg.user = user;
183 arg.src = src;
184 arg.dst = dst;
185 arg.iif = iif;
186 arg.ecn = ecn;
187
188 read_lock_bh(&nf_frags.lock);
189 hash = inet6_hash_frag(id, src, dst, nf_frags.rnd);
190
191 q = inet_frag_find(&net->nf_frag.frags, &nf_frags, &arg, hash);
192 local_bh_enable();
193 if (IS_ERR_OR_NULL(q)) {
194 inet_frag_maybe_warn_overflow(q, pr_fmt());
195 return NULL;
196 }
197 return container_of(q, struct frag_queue, q);
198 }
199
200
201 static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb,
202 const struct frag_hdr *fhdr, int nhoff)
203 {
204 struct sk_buff *prev, *next;
205 unsigned int payload_len;
206 int offset, end;
207 u8 ecn;
208
209 if (fq->q.last_in & INET_FRAG_COMPLETE) {
210 pr_debug("Already completed\n");
211 goto err;
212 }
213
214 payload_len = ntohs(ipv6_hdr(skb)->payload_len);
215
216 offset = ntohs(fhdr->frag_off) & ~0x7;
217 end = offset + (payload_len -
218 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
219
220 if ((unsigned int)end > IPV6_MAXPLEN) {
221 pr_debug("offset is too large.\n");
222 return -1;
223 }
224
225 ecn = ip6_frag_ecn(ipv6_hdr(skb));
226
227 if (skb->ip_summed == CHECKSUM_COMPLETE) {
228 const unsigned char *nh = skb_network_header(skb);
229 skb->csum = csum_sub(skb->csum,
230 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
231 0));
232 }
233
234 /* Is this the final fragment? */
235 if (!(fhdr->frag_off & htons(IP6_MF))) {
236 /* If we already have some bits beyond end
237 * or have different end, the segment is corrupted.
238 */
239 if (end < fq->q.len ||
240 ((fq->q.last_in & INET_FRAG_LAST_IN) && end != fq->q.len)) {
241 pr_debug("already received last fragment\n");
242 goto err;
243 }
244 fq->q.last_in |= INET_FRAG_LAST_IN;
245 fq->q.len = end;
246 } else {
247 /* Check if the fragment is rounded to 8 bytes.
248 * Required by the RFC.
249 */
250 if (end & 0x7) {
251 /* RFC2460 says always send parameter problem in
252 * this case. -DaveM
253 */
254 pr_debug("end of fragment not rounded to 8 bytes.\n");
255 return -1;
256 }
257 if (end > fq->q.len) {
258 /* Some bits beyond end -> corruption. */
259 if (fq->q.last_in & INET_FRAG_LAST_IN) {
260 pr_debug("last packet already reached.\n");
261 goto err;
262 }
263 fq->q.len = end;
264 }
265 }
266
267 if (end == offset)
268 goto err;
269
270 /* Point into the IP datagram 'data' part. */
271 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
272 pr_debug("queue: message is too short.\n");
273 goto err;
274 }
275 if (pskb_trim_rcsum(skb, end - offset)) {
276 pr_debug("Can't trim\n");
277 goto err;
278 }
279
280 /* Find out which fragments are in front and at the back of us
281 * in the chain of fragments so far. We must know where to put
282 * this fragment, right?
283 */
284 prev = fq->q.fragments_tail;
285 if (!prev || NFCT_FRAG6_CB(prev)->offset < offset) {
286 next = NULL;
287 goto found;
288 }
289 prev = NULL;
290 for (next = fq->q.fragments; next != NULL; next = next->next) {
291 if (NFCT_FRAG6_CB(next)->offset >= offset)
292 break; /* bingo! */
293 prev = next;
294 }
295
296 found:
297 /* RFC5722, Section 4:
298 * When reassembling an IPv6 datagram, if
299 * one or more its constituent fragments is determined to be an
300 * overlapping fragment, the entire datagram (and any constituent
301 * fragments, including those not yet received) MUST be silently
302 * discarded.
303 */
304
305 /* Check for overlap with preceding fragment. */
306 if (prev &&
307 (NFCT_FRAG6_CB(prev)->offset + prev->len) > offset)
308 goto discard_fq;
309
310 /* Look for overlap with succeeding segment. */
311 if (next && NFCT_FRAG6_CB(next)->offset < end)
312 goto discard_fq;
313
314 NFCT_FRAG6_CB(skb)->offset = offset;
315
316 /* Insert this fragment in the chain of fragments. */
317 skb->next = next;
318 if (!next)
319 fq->q.fragments_tail = skb;
320 if (prev)
321 prev->next = skb;
322 else
323 fq->q.fragments = skb;
324
325 if (skb->dev) {
326 fq->iif = skb->dev->ifindex;
327 skb->dev = NULL;
328 }
329 fq->q.stamp = skb->tstamp;
330 fq->q.meat += skb->len;
331 fq->ecn |= ecn;
332 if (payload_len > fq->q.max_size)
333 fq->q.max_size = payload_len;
334 add_frag_mem_limit(&fq->q, skb->truesize);
335
336 /* The first fragment.
337 * nhoffset is obtained from the first fragment, of course.
338 */
339 if (offset == 0) {
340 fq->nhoffset = nhoff;
341 fq->q.last_in |= INET_FRAG_FIRST_IN;
342 }
343
344 inet_frag_lru_move(&fq->q);
345 return 0;
346
347 discard_fq:
348 inet_frag_kill(&fq->q, &nf_frags);
349 err:
350 return -1;
351 }
352
353 /*
354 * Check if this packet is complete.
355 * Returns NULL on failure by any reason, and pointer
356 * to current nexthdr field in reassembled frame.
357 *
358 * It is called with locked fq, and caller must check that
359 * queue is eligible for reassembly i.e. it is not COMPLETE,
360 * the last and the first frames arrived and all the bits are here.
361 */
362 static struct sk_buff *
363 nf_ct_frag6_reasm(struct frag_queue *fq, struct net_device *dev)
364 {
365 struct sk_buff *fp, *op, *head = fq->q.fragments;
366 int payload_len;
367 u8 ecn;
368
369 inet_frag_kill(&fq->q, &nf_frags);
370
371 WARN_ON(head == NULL);
372 WARN_ON(NFCT_FRAG6_CB(head)->offset != 0);
373
374 ecn = ip_frag_ecn_table[fq->ecn];
375 if (unlikely(ecn == 0xff))
376 goto out_fail;
377
378 /* Unfragmented part is taken from the first segment. */
379 payload_len = ((head->data - skb_network_header(head)) -
380 sizeof(struct ipv6hdr) + fq->q.len -
381 sizeof(struct frag_hdr));
382 if (payload_len > IPV6_MAXPLEN) {
383 pr_debug("payload len is too large.\n");
384 goto out_oversize;
385 }
386
387 /* Head of list must not be cloned. */
388 if (skb_unclone(head, GFP_ATOMIC)) {
389 pr_debug("skb is cloned but can't expand head");
390 goto out_oom;
391 }
392
393 /* If the first fragment is fragmented itself, we split
394 * it to two chunks: the first with data and paged part
395 * and the second, holding only fragments. */
396 if (skb_has_frag_list(head)) {
397 struct sk_buff *clone;
398 int i, plen = 0;
399
400 clone = alloc_skb(0, GFP_ATOMIC);
401 if (clone == NULL)
402 goto out_oom;
403
404 clone->next = head->next;
405 head->next = clone;
406 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
407 skb_frag_list_init(head);
408 for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
409 plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
410 clone->len = clone->data_len = head->data_len - plen;
411 head->data_len -= clone->len;
412 head->len -= clone->len;
413 clone->csum = 0;
414 clone->ip_summed = head->ip_summed;
415
416 NFCT_FRAG6_CB(clone)->orig = NULL;
417 add_frag_mem_limit(&fq->q, clone->truesize);
418 }
419
420 /* We have to remove fragment header from datagram and to relocate
421 * header in order to calculate ICV correctly. */
422 skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0];
423 memmove(head->head + sizeof(struct frag_hdr), head->head,
424 (head->data - head->head) - sizeof(struct frag_hdr));
425 head->mac_header += sizeof(struct frag_hdr);
426 head->network_header += sizeof(struct frag_hdr);
427
428 skb_shinfo(head)->frag_list = head->next;
429 skb_reset_transport_header(head);
430 skb_push(head, head->data - skb_network_header(head));
431
432 for (fp=head->next; fp; fp = fp->next) {
433 head->data_len += fp->len;
434 head->len += fp->len;
435 if (head->ip_summed != fp->ip_summed)
436 head->ip_summed = CHECKSUM_NONE;
437 else if (head->ip_summed == CHECKSUM_COMPLETE)
438 head->csum = csum_add(head->csum, fp->csum);
439 head->truesize += fp->truesize;
440 }
441 sub_frag_mem_limit(&fq->q, head->truesize);
442
443 head->local_df = 1;
444 head->next = NULL;
445 head->dev = dev;
446 head->tstamp = fq->q.stamp;
447 ipv6_hdr(head)->payload_len = htons(payload_len);
448 ipv6_change_dsfield(ipv6_hdr(head), 0xff, ecn);
449 IP6CB(head)->frag_max_size = sizeof(struct ipv6hdr) + fq->q.max_size;
450
451 /* Yes, and fold redundant checksum back. 8) */
452 if (head->ip_summed == CHECKSUM_COMPLETE)
453 head->csum = csum_partial(skb_network_header(head),
454 skb_network_header_len(head),
455 head->csum);
456
457 fq->q.fragments = NULL;
458 fq->q.fragments_tail = NULL;
459
460 /* all original skbs are linked into the NFCT_FRAG6_CB(head).orig */
461 fp = skb_shinfo(head)->frag_list;
462 if (fp && NFCT_FRAG6_CB(fp)->orig == NULL)
463 /* at above code, head skb is divided into two skbs. */
464 fp = fp->next;
465
466 op = NFCT_FRAG6_CB(head)->orig;
467 for (; fp; fp = fp->next) {
468 struct sk_buff *orig = NFCT_FRAG6_CB(fp)->orig;
469
470 op->next = orig;
471 op = orig;
472 NFCT_FRAG6_CB(fp)->orig = NULL;
473 }
474
475 return head;
476
477 out_oversize:
478 net_dbg_ratelimited("nf_ct_frag6_reasm: payload len = %d\n",
479 payload_len);
480 goto out_fail;
481 out_oom:
482 net_dbg_ratelimited("nf_ct_frag6_reasm: no memory for reassembly\n");
483 out_fail:
484 return NULL;
485 }
486
487 /*
488 * find the header just before Fragment Header.
489 *
490 * if success return 0 and set ...
491 * (*prevhdrp): the value of "Next Header Field" in the header
492 * just before Fragment Header.
493 * (*prevhoff): the offset of "Next Header Field" in the header
494 * just before Fragment Header.
495 * (*fhoff) : the offset of Fragment Header.
496 *
497 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
498 *
499 */
500 static int
501 find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)
502 {
503 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
504 const int netoff = skb_network_offset(skb);
505 u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr);
506 int start = netoff + sizeof(struct ipv6hdr);
507 int len = skb->len - start;
508 u8 prevhdr = NEXTHDR_IPV6;
509
510 while (nexthdr != NEXTHDR_FRAGMENT) {
511 struct ipv6_opt_hdr hdr;
512 int hdrlen;
513
514 if (!ipv6_ext_hdr(nexthdr)) {
515 return -1;
516 }
517 if (nexthdr == NEXTHDR_NONE) {
518 pr_debug("next header is none\n");
519 return -1;
520 }
521 if (len < (int)sizeof(struct ipv6_opt_hdr)) {
522 pr_debug("too short\n");
523 return -1;
524 }
525 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
526 BUG();
527 if (nexthdr == NEXTHDR_AUTH)
528 hdrlen = (hdr.hdrlen+2)<<2;
529 else
530 hdrlen = ipv6_optlen(&hdr);
531
532 prevhdr = nexthdr;
533 prev_nhoff = start;
534
535 nexthdr = hdr.nexthdr;
536 len -= hdrlen;
537 start += hdrlen;
538 }
539
540 if (len < 0)
541 return -1;
542
543 *prevhdrp = prevhdr;
544 *prevhoff = prev_nhoff;
545 *fhoff = start;
546
547 return 0;
548 }
549
550 struct sk_buff *nf_ct_frag6_gather(struct sk_buff *skb, u32 user)
551 {
552 struct sk_buff *clone;
553 struct net_device *dev = skb->dev;
554 struct net *net = skb_dst(skb) ? dev_net(skb_dst(skb)->dev)
555 : dev_net(skb->dev);
556 struct frag_hdr *fhdr;
557 struct frag_queue *fq;
558 struct ipv6hdr *hdr;
559 int fhoff, nhoff;
560 u8 prevhdr;
561 struct sk_buff *ret_skb = NULL;
562
563 /* Jumbo payload inhibits frag. header */
564 if (ipv6_hdr(skb)->payload_len == 0) {
565 pr_debug("payload len = 0\n");
566 return skb;
567 }
568
569 if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
570 return skb;
571
572 clone = skb_clone(skb, GFP_ATOMIC);
573 if (clone == NULL) {
574 pr_debug("Can't clone skb\n");
575 return skb;
576 }
577
578 NFCT_FRAG6_CB(clone)->orig = skb;
579
580 if (!pskb_may_pull(clone, fhoff + sizeof(*fhdr))) {
581 pr_debug("message is too short.\n");
582 goto ret_orig;
583 }
584
585 skb_set_transport_header(clone, fhoff);
586 hdr = ipv6_hdr(clone);
587 fhdr = (struct frag_hdr *)skb_transport_header(clone);
588
589 local_bh_disable();
590 inet_frag_evictor(&net->nf_frag.frags, &nf_frags, false);
591 local_bh_enable();
592
593 fq = fq_find(net, fhdr->identification, user, &hdr->saddr, &hdr->daddr,
594 skb->dev ? skb->dev->ifindex : 0, ip6_frag_ecn(hdr));
595 if (fq == NULL) {
596 pr_debug("Can't find and can't create new queue\n");
597 goto ret_orig;
598 }
599
600 spin_lock_bh(&fq->q.lock);
601
602 if (nf_ct_frag6_queue(fq, clone, fhdr, nhoff) < 0) {
603 spin_unlock_bh(&fq->q.lock);
604 pr_debug("Can't insert skb to queue\n");
605 inet_frag_put(&fq->q, &nf_frags);
606 goto ret_orig;
607 }
608
609 if (fq->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
610 fq->q.meat == fq->q.len) {
611 ret_skb = nf_ct_frag6_reasm(fq, dev);
612 if (ret_skb == NULL)
613 pr_debug("Can't reassemble fragmented packets\n");
614 }
615 spin_unlock_bh(&fq->q.lock);
616
617 inet_frag_put(&fq->q, &nf_frags);
618 return ret_skb;
619
620 ret_orig:
621 kfree_skb(clone);
622 return skb;
623 }
624
625 void nf_ct_frag6_consume_orig(struct sk_buff *skb)
626 {
627 struct sk_buff *s, *s2;
628
629 for (s = NFCT_FRAG6_CB(skb)->orig; s;) {
630 s2 = s->next;
631 s->next = NULL;
632 consume_skb(s);
633 s = s2;
634 }
635 }
636
637 static int nf_ct_net_init(struct net *net)
638 {
639 net->nf_frag.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
640 net->nf_frag.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
641 net->nf_frag.frags.timeout = IPV6_FRAG_TIMEOUT;
642 inet_frags_init_net(&net->nf_frag.frags);
643
644 return nf_ct_frag6_sysctl_register(net);
645 }
646
647 static void nf_ct_net_exit(struct net *net)
648 {
649 nf_ct_frags6_sysctl_unregister(net);
650 inet_frags_exit_net(&net->nf_frag.frags, &nf_frags);
651 }
652
653 static struct pernet_operations nf_ct_net_ops = {
654 .init = nf_ct_net_init,
655 .exit = nf_ct_net_exit,
656 };
657
658 int nf_ct_frag6_init(void)
659 {
660 int ret = 0;
661
662 nf_frags.hashfn = nf_hashfn;
663 nf_frags.constructor = ip6_frag_init;
664 nf_frags.destructor = NULL;
665 nf_frags.skb_free = nf_skb_free;
666 nf_frags.qsize = sizeof(struct frag_queue);
667 nf_frags.match = ip6_frag_match;
668 nf_frags.frag_expire = nf_ct_frag6_expire;
669 nf_frags.secret_interval = 10 * 60 * HZ;
670 inet_frags_init(&nf_frags);
671
672 ret = register_pernet_subsys(&nf_ct_net_ops);
673 if (ret)
674 inet_frags_fini(&nf_frags);
675
676 return ret;
677 }
678
679 void nf_ct_frag6_cleanup(void)
680 {
681 unregister_pernet_subsys(&nf_ct_net_ops);
682 inet_frags_fini(&nf_frags);
683 }