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