[NETNS][IPV6]: Make ip6_frags per namespace.
[safe/jmp/linux-2.6] / net / ipv6 / reassembly.c
1 /*
2  *      IPv6 fragment reassembly
3  *      Linux INET6 implementation
4  *
5  *      Authors:
6  *      Pedro Roque             <roque@di.fc.ul.pt>
7  *
8  *      $Id: reassembly.c,v 1.26 2001/03/07 22:00:57 davem Exp $
9  *
10  *      Based on: net/ipv4/ip_fragment.c
11  *
12  *      This program is free software; you can redistribute it and/or
13  *      modify it under the terms of the GNU General Public License
14  *      as published by the Free Software Foundation; either version
15  *      2 of the License, or (at your option) any later version.
16  */
17
18 /*
19  *      Fixes:
20  *      Andi Kleen      Make it work with multiple hosts.
21  *                      More RFC compliance.
22  *
23  *      Horst von Brand Add missing #include <linux/string.h>
24  *      Alexey Kuznetsov        SMP races, threading, cleanup.
25  *      Patrick McHardy         LRU queue of frag heads for evictor.
26  *      Mitsuru KANDA @USAGI    Register inet6_protocol{}.
27  *      David Stevens and
28  *      YOSHIFUJI,H. @USAGI     Always remove fragment header to
29  *                              calculate ICV correctly.
30  */
31 #include <linux/errno.h>
32 #include <linux/types.h>
33 #include <linux/string.h>
34 #include <linux/socket.h>
35 #include <linux/sockios.h>
36 #include <linux/jiffies.h>
37 #include <linux/net.h>
38 #include <linux/list.h>
39 #include <linux/netdevice.h>
40 #include <linux/in6.h>
41 #include <linux/ipv6.h>
42 #include <linux/icmpv6.h>
43 #include <linux/random.h>
44 #include <linux/jhash.h>
45 #include <linux/skbuff.h>
46
47 #include <net/sock.h>
48 #include <net/snmp.h>
49
50 #include <net/ipv6.h>
51 #include <net/ip6_route.h>
52 #include <net/protocol.h>
53 #include <net/transp_v6.h>
54 #include <net/rawv6.h>
55 #include <net/ndisc.h>
56 #include <net/addrconf.h>
57 #include <net/inet_frag.h>
58
59 struct ip6frag_skb_cb
60 {
61         struct inet6_skb_parm   h;
62         int                     offset;
63 };
64
65 #define FRAG6_CB(skb)   ((struct ip6frag_skb_cb*)((skb)->cb))
66
67
68 /*
69  *      Equivalent of ipv4 struct ipq
70  */
71
72 struct frag_queue
73 {
74         struct inet_frag_queue  q;
75
76         __be32                  id;             /* fragment id          */
77         struct in6_addr         saddr;
78         struct in6_addr         daddr;
79
80         int                     iif;
81         unsigned int            csum;
82         __u16                   nhoffset;
83 };
84
85 static struct inet_frags ip6_frags;
86
87 int ip6_frag_nqueues(void)
88 {
89         return ip6_frags.nqueues;
90 }
91
92 int ip6_frag_mem(void)
93 {
94         return atomic_read(&ip6_frags.mem);
95 }
96
97 static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
98                           struct net_device *dev);
99
100 /*
101  * callers should be careful not to use the hash value outside the ipfrag_lock
102  * as doing so could race with ipfrag_hash_rnd being recalculated.
103  */
104 static unsigned int ip6qhashfn(__be32 id, struct in6_addr *saddr,
105                                struct in6_addr *daddr)
106 {
107         u32 a, b, c;
108
109         a = (__force u32)saddr->s6_addr32[0];
110         b = (__force u32)saddr->s6_addr32[1];
111         c = (__force u32)saddr->s6_addr32[2];
112
113         a += JHASH_GOLDEN_RATIO;
114         b += JHASH_GOLDEN_RATIO;
115         c += ip6_frags.rnd;
116         __jhash_mix(a, b, c);
117
118         a += (__force u32)saddr->s6_addr32[3];
119         b += (__force u32)daddr->s6_addr32[0];
120         c += (__force u32)daddr->s6_addr32[1];
121         __jhash_mix(a, b, c);
122
123         a += (__force u32)daddr->s6_addr32[2];
124         b += (__force u32)daddr->s6_addr32[3];
125         c += (__force u32)id;
126         __jhash_mix(a, b, c);
127
128         return c & (INETFRAGS_HASHSZ - 1);
129 }
130
131 static unsigned int ip6_hashfn(struct inet_frag_queue *q)
132 {
133         struct frag_queue *fq;
134
135         fq = container_of(q, struct frag_queue, q);
136         return ip6qhashfn(fq->id, &fq->saddr, &fq->daddr);
137 }
138
139 int ip6_frag_match(struct inet_frag_queue *q, void *a)
140 {
141         struct frag_queue *fq;
142         struct ip6_create_arg *arg = a;
143
144         fq = container_of(q, struct frag_queue, q);
145         return (fq->id == arg->id &&
146                         ipv6_addr_equal(&fq->saddr, arg->src) &&
147                         ipv6_addr_equal(&fq->daddr, arg->dst));
148 }
149 EXPORT_SYMBOL(ip6_frag_match);
150
151 /* Memory Tracking Functions. */
152 static inline void frag_kfree_skb(struct sk_buff *skb, int *work)
153 {
154         if (work)
155                 *work -= skb->truesize;
156         atomic_sub(skb->truesize, &ip6_frags.mem);
157         kfree_skb(skb);
158 }
159
160 void ip6_frag_init(struct inet_frag_queue *q, void *a)
161 {
162         struct frag_queue *fq = container_of(q, struct frag_queue, q);
163         struct ip6_create_arg *arg = a;
164
165         fq->id = arg->id;
166         ipv6_addr_copy(&fq->saddr, arg->src);
167         ipv6_addr_copy(&fq->daddr, arg->dst);
168 }
169 EXPORT_SYMBOL(ip6_frag_init);
170
171 /* Destruction primitives. */
172
173 static __inline__ void fq_put(struct frag_queue *fq)
174 {
175         inet_frag_put(&fq->q, &ip6_frags);
176 }
177
178 /* Kill fq entry. It is not destroyed immediately,
179  * because caller (and someone more) holds reference count.
180  */
181 static __inline__ void fq_kill(struct frag_queue *fq)
182 {
183         inet_frag_kill(&fq->q, &ip6_frags);
184 }
185
186 static void ip6_evictor(struct inet6_dev *idev)
187 {
188         int evicted;
189
190         evicted = inet_frag_evictor(&ip6_frags);
191         if (evicted)
192                 IP6_ADD_STATS_BH(idev, IPSTATS_MIB_REASMFAILS, evicted);
193 }
194
195 static void ip6_frag_expire(unsigned long data)
196 {
197         struct frag_queue *fq;
198         struct net_device *dev = NULL;
199
200         fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q);
201
202         spin_lock(&fq->q.lock);
203
204         if (fq->q.last_in & COMPLETE)
205                 goto out;
206
207         fq_kill(fq);
208
209         dev = dev_get_by_index(&init_net, fq->iif);
210         if (!dev)
211                 goto out;
212
213         rcu_read_lock();
214         IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
215         IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
216         rcu_read_unlock();
217
218         /* Don't send error if the first segment did not arrive. */
219         if (!(fq->q.last_in&FIRST_IN) || !fq->q.fragments)
220                 goto out;
221
222         /*
223            But use as source device on which LAST ARRIVED
224            segment was received. And do not use fq->dev
225            pointer directly, device might already disappeared.
226          */
227         fq->q.fragments->dev = dev;
228         icmpv6_send(fq->q.fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0, dev);
229 out:
230         if (dev)
231                 dev_put(dev);
232         spin_unlock(&fq->q.lock);
233         fq_put(fq);
234 }
235
236 static __inline__ struct frag_queue *
237 fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst,
238         struct inet6_dev *idev)
239 {
240         struct inet_frag_queue *q;
241         struct ip6_create_arg arg;
242         unsigned int hash;
243
244         arg.id = id;
245         arg.src = src;
246         arg.dst = dst;
247         hash = ip6qhashfn(id, src, dst);
248
249         q = inet_frag_find(&ip6_frags, &arg, hash);
250         if (q == NULL)
251                 goto oom;
252
253         return container_of(q, struct frag_queue, q);
254
255 oom:
256         IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
257         return NULL;
258 }
259
260 static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
261                            struct frag_hdr *fhdr, int nhoff)
262 {
263         struct sk_buff *prev, *next;
264         struct net_device *dev;
265         int offset, end;
266
267         if (fq->q.last_in & COMPLETE)
268                 goto err;
269
270         offset = ntohs(fhdr->frag_off) & ~0x7;
271         end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
272                         ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
273
274         if ((unsigned int)end > IPV6_MAXPLEN) {
275                 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
276                                  IPSTATS_MIB_INHDRERRORS);
277                 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
278                                   ((u8 *)&fhdr->frag_off -
279                                    skb_network_header(skb)));
280                 return -1;
281         }
282
283         if (skb->ip_summed == CHECKSUM_COMPLETE) {
284                 const unsigned char *nh = skb_network_header(skb);
285                 skb->csum = csum_sub(skb->csum,
286                                      csum_partial(nh, (u8 *)(fhdr + 1) - nh,
287                                                   0));
288         }
289
290         /* Is this the final fragment? */
291         if (!(fhdr->frag_off & htons(IP6_MF))) {
292                 /* If we already have some bits beyond end
293                  * or have different end, the segment is corrupted.
294                  */
295                 if (end < fq->q.len ||
296                     ((fq->q.last_in & LAST_IN) && end != fq->q.len))
297                         goto err;
298                 fq->q.last_in |= LAST_IN;
299                 fq->q.len = end;
300         } else {
301                 /* Check if the fragment is rounded to 8 bytes.
302                  * Required by the RFC.
303                  */
304                 if (end & 0x7) {
305                         /* RFC2460 says always send parameter problem in
306                          * this case. -DaveM
307                          */
308                         IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
309                                          IPSTATS_MIB_INHDRERRORS);
310                         icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
311                                           offsetof(struct ipv6hdr, payload_len));
312                         return -1;
313                 }
314                 if (end > fq->q.len) {
315                         /* Some bits beyond end -> corruption. */
316                         if (fq->q.last_in & LAST_IN)
317                                 goto err;
318                         fq->q.len = end;
319                 }
320         }
321
322         if (end == offset)
323                 goto err;
324
325         /* Point into the IP datagram 'data' part. */
326         if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
327                 goto err;
328
329         if (pskb_trim_rcsum(skb, end - offset))
330                 goto err;
331
332         /* Find out which fragments are in front and at the back of us
333          * in the chain of fragments so far.  We must know where to put
334          * this fragment, right?
335          */
336         prev = NULL;
337         for(next = fq->q.fragments; next != NULL; next = next->next) {
338                 if (FRAG6_CB(next)->offset >= offset)
339                         break;  /* bingo! */
340                 prev = next;
341         }
342
343         /* We found where to put this one.  Check for overlap with
344          * preceding fragment, and, if needed, align things so that
345          * any overlaps are eliminated.
346          */
347         if (prev) {
348                 int i = (FRAG6_CB(prev)->offset + prev->len) - offset;
349
350                 if (i > 0) {
351                         offset += i;
352                         if (end <= offset)
353                                 goto err;
354                         if (!pskb_pull(skb, i))
355                                 goto err;
356                         if (skb->ip_summed != CHECKSUM_UNNECESSARY)
357                                 skb->ip_summed = CHECKSUM_NONE;
358                 }
359         }
360
361         /* Look for overlap with succeeding segments.
362          * If we can merge fragments, do it.
363          */
364         while (next && FRAG6_CB(next)->offset < end) {
365                 int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */
366
367                 if (i < next->len) {
368                         /* Eat head of the next overlapped fragment
369                          * and leave the loop. The next ones cannot overlap.
370                          */
371                         if (!pskb_pull(next, i))
372                                 goto err;
373                         FRAG6_CB(next)->offset += i;    /* next fragment */
374                         fq->q.meat -= i;
375                         if (next->ip_summed != CHECKSUM_UNNECESSARY)
376                                 next->ip_summed = CHECKSUM_NONE;
377                         break;
378                 } else {
379                         struct sk_buff *free_it = next;
380
381                         /* Old fragment is completely overridden with
382                          * new one drop it.
383                          */
384                         next = next->next;
385
386                         if (prev)
387                                 prev->next = next;
388                         else
389                                 fq->q.fragments = next;
390
391                         fq->q.meat -= free_it->len;
392                         frag_kfree_skb(free_it, NULL);
393                 }
394         }
395
396         FRAG6_CB(skb)->offset = offset;
397
398         /* Insert this fragment in the chain of fragments. */
399         skb->next = next;
400         if (prev)
401                 prev->next = skb;
402         else
403                 fq->q.fragments = skb;
404
405         dev = skb->dev;
406         if (dev) {
407                 fq->iif = dev->ifindex;
408                 skb->dev = NULL;
409         }
410         fq->q.stamp = skb->tstamp;
411         fq->q.meat += skb->len;
412         atomic_add(skb->truesize, &ip6_frags.mem);
413
414         /* The first fragment.
415          * nhoffset is obtained from the first fragment, of course.
416          */
417         if (offset == 0) {
418                 fq->nhoffset = nhoff;
419                 fq->q.last_in |= FIRST_IN;
420         }
421
422         if (fq->q.last_in == (FIRST_IN | LAST_IN) && fq->q.meat == fq->q.len)
423                 return ip6_frag_reasm(fq, prev, dev);
424
425         write_lock(&ip6_frags.lock);
426         list_move_tail(&fq->q.lru_list, &ip6_frags.lru_list);
427         write_unlock(&ip6_frags.lock);
428         return -1;
429
430 err:
431         IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
432         kfree_skb(skb);
433         return -1;
434 }
435
436 /*
437  *      Check if this packet is complete.
438  *      Returns NULL on failure by any reason, and pointer
439  *      to current nexthdr field in reassembled frame.
440  *
441  *      It is called with locked fq, and caller must check that
442  *      queue is eligible for reassembly i.e. it is not COMPLETE,
443  *      the last and the first frames arrived and all the bits are here.
444  */
445 static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
446                           struct net_device *dev)
447 {
448         struct sk_buff *fp, *head = fq->q.fragments;
449         int    payload_len;
450         unsigned int nhoff;
451
452         fq_kill(fq);
453
454         /* Make the one we just received the head. */
455         if (prev) {
456                 head = prev->next;
457                 fp = skb_clone(head, GFP_ATOMIC);
458
459                 if (!fp)
460                         goto out_oom;
461
462                 fp->next = head->next;
463                 prev->next = fp;
464
465                 skb_morph(head, fq->q.fragments);
466                 head->next = fq->q.fragments->next;
467
468                 kfree_skb(fq->q.fragments);
469                 fq->q.fragments = head;
470         }
471
472         BUG_TRAP(head != NULL);
473         BUG_TRAP(FRAG6_CB(head)->offset == 0);
474
475         /* Unfragmented part is taken from the first segment. */
476         payload_len = ((head->data - skb_network_header(head)) -
477                        sizeof(struct ipv6hdr) + fq->q.len -
478                        sizeof(struct frag_hdr));
479         if (payload_len > IPV6_MAXPLEN)
480                 goto out_oversize;
481
482         /* Head of list must not be cloned. */
483         if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
484                 goto out_oom;
485
486         /* If the first fragment is fragmented itself, we split
487          * it to two chunks: the first with data and paged part
488          * and the second, holding only fragments. */
489         if (skb_shinfo(head)->frag_list) {
490                 struct sk_buff *clone;
491                 int i, plen = 0;
492
493                 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
494                         goto out_oom;
495                 clone->next = head->next;
496                 head->next = clone;
497                 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
498                 skb_shinfo(head)->frag_list = NULL;
499                 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
500                         plen += skb_shinfo(head)->frags[i].size;
501                 clone->len = clone->data_len = head->data_len - plen;
502                 head->data_len -= clone->len;
503                 head->len -= clone->len;
504                 clone->csum = 0;
505                 clone->ip_summed = head->ip_summed;
506                 atomic_add(clone->truesize, &ip6_frags.mem);
507         }
508
509         /* We have to remove fragment header from datagram and to relocate
510          * header in order to calculate ICV correctly. */
511         nhoff = fq->nhoffset;
512         skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
513         memmove(head->head + sizeof(struct frag_hdr), head->head,
514                 (head->data - head->head) - sizeof(struct frag_hdr));
515         head->mac_header += sizeof(struct frag_hdr);
516         head->network_header += sizeof(struct frag_hdr);
517
518         skb_shinfo(head)->frag_list = head->next;
519         skb_reset_transport_header(head);
520         skb_push(head, head->data - skb_network_header(head));
521         atomic_sub(head->truesize, &ip6_frags.mem);
522
523         for (fp=head->next; fp; fp = fp->next) {
524                 head->data_len += fp->len;
525                 head->len += fp->len;
526                 if (head->ip_summed != fp->ip_summed)
527                         head->ip_summed = CHECKSUM_NONE;
528                 else if (head->ip_summed == CHECKSUM_COMPLETE)
529                         head->csum = csum_add(head->csum, fp->csum);
530                 head->truesize += fp->truesize;
531                 atomic_sub(fp->truesize, &ip6_frags.mem);
532         }
533
534         head->next = NULL;
535         head->dev = dev;
536         head->tstamp = fq->q.stamp;
537         ipv6_hdr(head)->payload_len = htons(payload_len);
538         IP6CB(head)->nhoff = nhoff;
539
540         /* Yes, and fold redundant checksum back. 8) */
541         if (head->ip_summed == CHECKSUM_COMPLETE)
542                 head->csum = csum_partial(skb_network_header(head),
543                                           skb_network_header_len(head),
544                                           head->csum);
545
546         rcu_read_lock();
547         IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
548         rcu_read_unlock();
549         fq->q.fragments = NULL;
550         return 1;
551
552 out_oversize:
553         if (net_ratelimit())
554                 printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len);
555         goto out_fail;
556 out_oom:
557         if (net_ratelimit())
558                 printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n");
559 out_fail:
560         rcu_read_lock();
561         IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
562         rcu_read_unlock();
563         return -1;
564 }
565
566 static int ipv6_frag_rcv(struct sk_buff *skb)
567 {
568         struct frag_hdr *fhdr;
569         struct frag_queue *fq;
570         struct ipv6hdr *hdr = ipv6_hdr(skb);
571
572         IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMREQDS);
573
574         /* Jumbo payload inhibits frag. header */
575         if (hdr->payload_len==0) {
576                 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
577                 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
578                                   skb_network_header_len(skb));
579                 return -1;
580         }
581         if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
582                                  sizeof(struct frag_hdr)))) {
583                 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
584                 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
585                                   skb_network_header_len(skb));
586                 return -1;
587         }
588
589         hdr = ipv6_hdr(skb);
590         fhdr = (struct frag_hdr *)skb_transport_header(skb);
591
592         if (!(fhdr->frag_off & htons(0xFFF9))) {
593                 /* It is not a fragmented frame */
594                 skb->transport_header += sizeof(struct frag_hdr);
595                 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMOKS);
596
597                 IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
598                 return 1;
599         }
600
601         if (atomic_read(&ip6_frags.mem) > init_net.ipv6.sysctl.frags.high_thresh)
602                 ip6_evictor(ip6_dst_idev(skb->dst));
603
604         if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr,
605                           ip6_dst_idev(skb->dst))) != NULL) {
606                 int ret;
607
608                 spin_lock(&fq->q.lock);
609
610                 ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
611
612                 spin_unlock(&fq->q.lock);
613                 fq_put(fq);
614                 return ret;
615         }
616
617         IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
618         kfree_skb(skb);
619         return -1;
620 }
621
622 static struct inet6_protocol frag_protocol =
623 {
624         .handler        =       ipv6_frag_rcv,
625         .flags          =       INET6_PROTO_NOPOLICY,
626 };
627
628 void ipv6_frag_sysctl_init(struct net *net)
629 {
630         ip6_frags.ctl = &net->ipv6.sysctl.frags;
631 }
632
633 int __init ipv6_frag_init(void)
634 {
635         int ret;
636
637         ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
638         if (ret)
639                 goto out;
640
641         ip6_frags.hashfn = ip6_hashfn;
642         ip6_frags.constructor = ip6_frag_init;
643         ip6_frags.destructor = NULL;
644         ip6_frags.skb_free = NULL;
645         ip6_frags.qsize = sizeof(struct frag_queue);
646         ip6_frags.match = ip6_frag_match;
647         ip6_frags.frag_expire = ip6_frag_expire;
648         inet_frags_init(&ip6_frags);
649 out:
650         return ret;
651 }
652
653 void ipv6_frag_exit(void)
654 {
655         inet_frags_fini(&ip6_frags);
656         inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
657 }