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