]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - net/ipv6/reassembly.c
[INET]: Collect frag queues management objects together
[linux-2.6-omap-h63xx.git] / 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 int sysctl_ip6frag_high_thresh __read_mostly = 256*1024;
60 int sysctl_ip6frag_low_thresh __read_mostly = 192*1024;
61
62 int sysctl_ip6frag_time __read_mostly = IPV6_FRAG_TIMEOUT;
63
64 struct ip6frag_skb_cb
65 {
66         struct inet6_skb_parm   h;
67         int                     offset;
68 };
69
70 #define FRAG6_CB(skb)   ((struct ip6frag_skb_cb*)((skb)->cb))
71
72
73 /*
74  *      Equivalent of ipv4 struct ipq
75  */
76
77 struct frag_queue
78 {
79         struct inet_frag_queue  q;
80
81         __be32                  id;             /* fragment id          */
82         struct in6_addr         saddr;
83         struct in6_addr         daddr;
84
85         int                     iif;
86         unsigned int            csum;
87         __u16                   nhoffset;
88 };
89
90 static struct inet_frags ip6_frags;
91
92 int ip6_frag_nqueues(void)
93 {
94         return ip6_frags.nqueues;
95 }
96
97 int ip6_frag_mem(void)
98 {
99         return atomic_read(&ip6_frags.mem);
100 }
101
102 static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
103                           struct net_device *dev);
104
105 static __inline__ void __fq_unlink(struct frag_queue *fq)
106 {
107         hlist_del(&fq->q.list);
108         list_del(&fq->q.lru_list);
109         ip6_frags.nqueues--;
110 }
111
112 static __inline__ void fq_unlink(struct frag_queue *fq)
113 {
114         write_lock(&ip6_frags.lock);
115         __fq_unlink(fq);
116         write_unlock(&ip6_frags.lock);
117 }
118
119 /*
120  * callers should be careful not to use the hash value outside the ipfrag_lock
121  * as doing so could race with ipfrag_hash_rnd being recalculated.
122  */
123 static unsigned int ip6qhashfn(__be32 id, struct in6_addr *saddr,
124                                struct in6_addr *daddr)
125 {
126         u32 a, b, c;
127
128         a = (__force u32)saddr->s6_addr32[0];
129         b = (__force u32)saddr->s6_addr32[1];
130         c = (__force u32)saddr->s6_addr32[2];
131
132         a += JHASH_GOLDEN_RATIO;
133         b += JHASH_GOLDEN_RATIO;
134         c += ip6_frags.rnd;
135         __jhash_mix(a, b, c);
136
137         a += (__force u32)saddr->s6_addr32[3];
138         b += (__force u32)daddr->s6_addr32[0];
139         c += (__force u32)daddr->s6_addr32[1];
140         __jhash_mix(a, b, c);
141
142         a += (__force u32)daddr->s6_addr32[2];
143         b += (__force u32)daddr->s6_addr32[3];
144         c += (__force u32)id;
145         __jhash_mix(a, b, c);
146
147         return c & (INETFRAGS_HASHSZ - 1);
148 }
149
150 int sysctl_ip6frag_secret_interval __read_mostly = 10 * 60 * HZ;
151
152 static void ip6_frag_secret_rebuild(unsigned long dummy)
153 {
154         unsigned long now = jiffies;
155         int i;
156
157         write_lock(&ip6_frags.lock);
158         get_random_bytes(&ip6_frags.rnd, sizeof(u32));
159         for (i = 0; i < INETFRAGS_HASHSZ; i++) {
160                 struct frag_queue *q;
161                 struct hlist_node *p, *n;
162
163                 hlist_for_each_entry_safe(q, p, n, &ip6_frags.hash[i], q.list) {
164                         unsigned int hval = ip6qhashfn(q->id,
165                                                        &q->saddr,
166                                                        &q->daddr);
167
168                         if (hval != i) {
169                                 hlist_del(&q->q.list);
170
171                                 /* Relink to new hash chain. */
172                                 hlist_add_head(&q->q.list,
173                                                &ip6_frags.hash[hval]);
174
175                         }
176                 }
177         }
178         write_unlock(&ip6_frags.lock);
179
180         mod_timer(&ip6_frags.secret_timer, now + sysctl_ip6frag_secret_interval);
181 }
182
183 /* Memory Tracking Functions. */
184 static inline void frag_kfree_skb(struct sk_buff *skb, int *work)
185 {
186         if (work)
187                 *work -= skb->truesize;
188         atomic_sub(skb->truesize, &ip6_frags.mem);
189         kfree_skb(skb);
190 }
191
192 static inline void frag_free_queue(struct frag_queue *fq, int *work)
193 {
194         if (work)
195                 *work -= sizeof(struct frag_queue);
196         atomic_sub(sizeof(struct frag_queue), &ip6_frags.mem);
197         kfree(fq);
198 }
199
200 static inline struct frag_queue *frag_alloc_queue(void)
201 {
202         struct frag_queue *fq = kzalloc(sizeof(struct frag_queue), GFP_ATOMIC);
203
204         if(!fq)
205                 return NULL;
206         atomic_add(sizeof(struct frag_queue), &ip6_frags.mem);
207         return fq;
208 }
209
210 /* Destruction primitives. */
211
212 /* Complete destruction of fq. */
213 static void ip6_frag_destroy(struct frag_queue *fq, int *work)
214 {
215         struct sk_buff *fp;
216
217         BUG_TRAP(fq->q.last_in&COMPLETE);
218         BUG_TRAP(del_timer(&fq->q.timer) == 0);
219
220         /* Release all fragment data. */
221         fp = fq->q.fragments;
222         while (fp) {
223                 struct sk_buff *xp = fp->next;
224
225                 frag_kfree_skb(fp, work);
226                 fp = xp;
227         }
228
229         frag_free_queue(fq, work);
230 }
231
232 static __inline__ void fq_put(struct frag_queue *fq, int *work)
233 {
234         if (atomic_dec_and_test(&fq->q.refcnt))
235                 ip6_frag_destroy(fq, work);
236 }
237
238 /* Kill fq entry. It is not destroyed immediately,
239  * because caller (and someone more) holds reference count.
240  */
241 static __inline__ void fq_kill(struct frag_queue *fq)
242 {
243         if (del_timer(&fq->q.timer))
244                 atomic_dec(&fq->q.refcnt);
245
246         if (!(fq->q.last_in & COMPLETE)) {
247                 fq_unlink(fq);
248                 atomic_dec(&fq->q.refcnt);
249                 fq->q.last_in |= COMPLETE;
250         }
251 }
252
253 static void ip6_evictor(struct inet6_dev *idev)
254 {
255         struct frag_queue *fq;
256         struct list_head *tmp;
257         int work;
258
259         work = atomic_read(&ip6_frags.mem) - sysctl_ip6frag_low_thresh;
260         if (work <= 0)
261                 return;
262
263         while(work > 0) {
264                 read_lock(&ip6_frags.lock);
265                 if (list_empty(&ip6_frags.lru_list)) {
266                         read_unlock(&ip6_frags.lock);
267                         return;
268                 }
269                 tmp = ip6_frags.lru_list.next;
270                 fq = list_entry(tmp, struct frag_queue, q.lru_list);
271                 atomic_inc(&fq->q.refcnt);
272                 read_unlock(&ip6_frags.lock);
273
274                 spin_lock(&fq->q.lock);
275                 if (!(fq->q.last_in&COMPLETE))
276                         fq_kill(fq);
277                 spin_unlock(&fq->q.lock);
278
279                 fq_put(fq, &work);
280                 IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
281         }
282 }
283
284 static void ip6_frag_expire(unsigned long data)
285 {
286         struct frag_queue *fq = (struct frag_queue *) data;
287         struct net_device *dev = NULL;
288
289         spin_lock(&fq->q.lock);
290
291         if (fq->q.last_in & COMPLETE)
292                 goto out;
293
294         fq_kill(fq);
295
296         dev = dev_get_by_index(&init_net, fq->iif);
297         if (!dev)
298                 goto out;
299
300         rcu_read_lock();
301         IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
302         IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
303         rcu_read_unlock();
304
305         /* Don't send error if the first segment did not arrive. */
306         if (!(fq->q.last_in&FIRST_IN) || !fq->q.fragments)
307                 goto out;
308
309         /*
310            But use as source device on which LAST ARRIVED
311            segment was received. And do not use fq->dev
312            pointer directly, device might already disappeared.
313          */
314         fq->q.fragments->dev = dev;
315         icmpv6_send(fq->q.fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0, dev);
316 out:
317         if (dev)
318                 dev_put(dev);
319         spin_unlock(&fq->q.lock);
320         fq_put(fq, NULL);
321 }
322
323 /* Creation primitives. */
324
325
326 static struct frag_queue *ip6_frag_intern(struct frag_queue *fq_in)
327 {
328         struct frag_queue *fq;
329         unsigned int hash;
330 #ifdef CONFIG_SMP
331         struct hlist_node *n;
332 #endif
333
334         write_lock(&ip6_frags.lock);
335         hash = ip6qhashfn(fq_in->id, &fq_in->saddr, &fq_in->daddr);
336 #ifdef CONFIG_SMP
337         hlist_for_each_entry(fq, n, &ip6_frags.hash[hash], q.list) {
338                 if (fq->id == fq_in->id &&
339                     ipv6_addr_equal(&fq_in->saddr, &fq->saddr) &&
340                     ipv6_addr_equal(&fq_in->daddr, &fq->daddr)) {
341                         atomic_inc(&fq->q.refcnt);
342                         write_unlock(&ip6_frags.lock);
343                         fq_in->q.last_in |= COMPLETE;
344                         fq_put(fq_in, NULL);
345                         return fq;
346                 }
347         }
348 #endif
349         fq = fq_in;
350
351         if (!mod_timer(&fq->q.timer, jiffies + sysctl_ip6frag_time))
352                 atomic_inc(&fq->q.refcnt);
353
354         atomic_inc(&fq->q.refcnt);
355         hlist_add_head(&fq->q.list, &ip6_frags.hash[hash]);
356         INIT_LIST_HEAD(&fq->q.lru_list);
357         list_add_tail(&fq->q.lru_list, &ip6_frags.lru_list);
358         ip6_frags.nqueues++;
359         write_unlock(&ip6_frags.lock);
360         return fq;
361 }
362
363
364 static struct frag_queue *
365 ip6_frag_create(__be32 id, struct in6_addr *src, struct in6_addr *dst,
366                 struct inet6_dev *idev)
367 {
368         struct frag_queue *fq;
369
370         if ((fq = frag_alloc_queue()) == NULL)
371                 goto oom;
372
373         fq->id = id;
374         ipv6_addr_copy(&fq->saddr, src);
375         ipv6_addr_copy(&fq->daddr, dst);
376
377         init_timer(&fq->q.timer);
378         fq->q.timer.function = ip6_frag_expire;
379         fq->q.timer.data = (long) fq;
380         spin_lock_init(&fq->q.lock);
381         atomic_set(&fq->q.refcnt, 1);
382
383         return ip6_frag_intern(fq);
384
385 oom:
386         IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
387         return NULL;
388 }
389
390 static __inline__ struct frag_queue *
391 fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst,
392         struct inet6_dev *idev)
393 {
394         struct frag_queue *fq;
395         struct hlist_node *n;
396         unsigned int hash;
397
398         read_lock(&ip6_frags.lock);
399         hash = ip6qhashfn(id, src, dst);
400         hlist_for_each_entry(fq, n, &ip6_frags.hash[hash], q.list) {
401                 if (fq->id == id &&
402                     ipv6_addr_equal(src, &fq->saddr) &&
403                     ipv6_addr_equal(dst, &fq->daddr)) {
404                         atomic_inc(&fq->q.refcnt);
405                         read_unlock(&ip6_frags.lock);
406                         return fq;
407                 }
408         }
409         read_unlock(&ip6_frags.lock);
410
411         return ip6_frag_create(id, src, dst, idev);
412 }
413
414
415 static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
416                            struct frag_hdr *fhdr, int nhoff)
417 {
418         struct sk_buff *prev, *next;
419         struct net_device *dev;
420         int offset, end;
421
422         if (fq->q.last_in & COMPLETE)
423                 goto err;
424
425         offset = ntohs(fhdr->frag_off) & ~0x7;
426         end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
427                         ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
428
429         if ((unsigned int)end > IPV6_MAXPLEN) {
430                 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
431                                  IPSTATS_MIB_INHDRERRORS);
432                 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
433                                   ((u8 *)&fhdr->frag_off -
434                                    skb_network_header(skb)));
435                 return -1;
436         }
437
438         if (skb->ip_summed == CHECKSUM_COMPLETE) {
439                 const unsigned char *nh = skb_network_header(skb);
440                 skb->csum = csum_sub(skb->csum,
441                                      csum_partial(nh, (u8 *)(fhdr + 1) - nh,
442                                                   0));
443         }
444
445         /* Is this the final fragment? */
446         if (!(fhdr->frag_off & htons(IP6_MF))) {
447                 /* If we already have some bits beyond end
448                  * or have different end, the segment is corrupted.
449                  */
450                 if (end < fq->q.len ||
451                     ((fq->q.last_in & LAST_IN) && end != fq->q.len))
452                         goto err;
453                 fq->q.last_in |= LAST_IN;
454                 fq->q.len = end;
455         } else {
456                 /* Check if the fragment is rounded to 8 bytes.
457                  * Required by the RFC.
458                  */
459                 if (end & 0x7) {
460                         /* RFC2460 says always send parameter problem in
461                          * this case. -DaveM
462                          */
463                         IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
464                                          IPSTATS_MIB_INHDRERRORS);
465                         icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
466                                           offsetof(struct ipv6hdr, payload_len));
467                         return -1;
468                 }
469                 if (end > fq->q.len) {
470                         /* Some bits beyond end -> corruption. */
471                         if (fq->q.last_in & LAST_IN)
472                                 goto err;
473                         fq->q.len = end;
474                 }
475         }
476
477         if (end == offset)
478                 goto err;
479
480         /* Point into the IP datagram 'data' part. */
481         if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
482                 goto err;
483
484         if (pskb_trim_rcsum(skb, end - offset))
485                 goto err;
486
487         /* Find out which fragments are in front and at the back of us
488          * in the chain of fragments so far.  We must know where to put
489          * this fragment, right?
490          */
491         prev = NULL;
492         for(next = fq->q.fragments; next != NULL; next = next->next) {
493                 if (FRAG6_CB(next)->offset >= offset)
494                         break;  /* bingo! */
495                 prev = next;
496         }
497
498         /* We found where to put this one.  Check for overlap with
499          * preceding fragment, and, if needed, align things so that
500          * any overlaps are eliminated.
501          */
502         if (prev) {
503                 int i = (FRAG6_CB(prev)->offset + prev->len) - offset;
504
505                 if (i > 0) {
506                         offset += i;
507                         if (end <= offset)
508                                 goto err;
509                         if (!pskb_pull(skb, i))
510                                 goto err;
511                         if (skb->ip_summed != CHECKSUM_UNNECESSARY)
512                                 skb->ip_summed = CHECKSUM_NONE;
513                 }
514         }
515
516         /* Look for overlap with succeeding segments.
517          * If we can merge fragments, do it.
518          */
519         while (next && FRAG6_CB(next)->offset < end) {
520                 int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */
521
522                 if (i < next->len) {
523                         /* Eat head of the next overlapped fragment
524                          * and leave the loop. The next ones cannot overlap.
525                          */
526                         if (!pskb_pull(next, i))
527                                 goto err;
528                         FRAG6_CB(next)->offset += i;    /* next fragment */
529                         fq->q.meat -= i;
530                         if (next->ip_summed != CHECKSUM_UNNECESSARY)
531                                 next->ip_summed = CHECKSUM_NONE;
532                         break;
533                 } else {
534                         struct sk_buff *free_it = next;
535
536                         /* Old fragment is completely overridden with
537                          * new one drop it.
538                          */
539                         next = next->next;
540
541                         if (prev)
542                                 prev->next = next;
543                         else
544                                 fq->q.fragments = next;
545
546                         fq->q.meat -= free_it->len;
547                         frag_kfree_skb(free_it, NULL);
548                 }
549         }
550
551         FRAG6_CB(skb)->offset = offset;
552
553         /* Insert this fragment in the chain of fragments. */
554         skb->next = next;
555         if (prev)
556                 prev->next = skb;
557         else
558                 fq->q.fragments = skb;
559
560         dev = skb->dev;
561         if (dev) {
562                 fq->iif = dev->ifindex;
563                 skb->dev = NULL;
564         }
565         fq->q.stamp = skb->tstamp;
566         fq->q.meat += skb->len;
567         atomic_add(skb->truesize, &ip6_frags.mem);
568
569         /* The first fragment.
570          * nhoffset is obtained from the first fragment, of course.
571          */
572         if (offset == 0) {
573                 fq->nhoffset = nhoff;
574                 fq->q.last_in |= FIRST_IN;
575         }
576
577         if (fq->q.last_in == (FIRST_IN | LAST_IN) && fq->q.meat == fq->q.len)
578                 return ip6_frag_reasm(fq, prev, dev);
579
580         write_lock(&ip6_frags.lock);
581         list_move_tail(&fq->q.lru_list, &ip6_frags.lru_list);
582         write_unlock(&ip6_frags.lock);
583         return -1;
584
585 err:
586         IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
587         kfree_skb(skb);
588         return -1;
589 }
590
591 /*
592  *      Check if this packet is complete.
593  *      Returns NULL on failure by any reason, and pointer
594  *      to current nexthdr field in reassembled frame.
595  *
596  *      It is called with locked fq, and caller must check that
597  *      queue is eligible for reassembly i.e. it is not COMPLETE,
598  *      the last and the first frames arrived and all the bits are here.
599  */
600 static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
601                           struct net_device *dev)
602 {
603         struct sk_buff *fp, *head = fq->q.fragments;
604         int    payload_len;
605         unsigned int nhoff;
606
607         fq_kill(fq);
608
609         /* Make the one we just received the head. */
610         if (prev) {
611                 head = prev->next;
612                 fp = skb_clone(head, GFP_ATOMIC);
613
614                 if (!fp)
615                         goto out_oom;
616
617                 fp->next = head->next;
618                 prev->next = fp;
619
620                 skb_morph(head, fq->q.fragments);
621                 head->next = fq->q.fragments->next;
622
623                 kfree_skb(fq->q.fragments);
624                 fq->q.fragments = head;
625         }
626
627         BUG_TRAP(head != NULL);
628         BUG_TRAP(FRAG6_CB(head)->offset == 0);
629
630         /* Unfragmented part is taken from the first segment. */
631         payload_len = ((head->data - skb_network_header(head)) -
632                        sizeof(struct ipv6hdr) + fq->q.len -
633                        sizeof(struct frag_hdr));
634         if (payload_len > IPV6_MAXPLEN)
635                 goto out_oversize;
636
637         /* Head of list must not be cloned. */
638         if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
639                 goto out_oom;
640
641         /* If the first fragment is fragmented itself, we split
642          * it to two chunks: the first with data and paged part
643          * and the second, holding only fragments. */
644         if (skb_shinfo(head)->frag_list) {
645                 struct sk_buff *clone;
646                 int i, plen = 0;
647
648                 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
649                         goto out_oom;
650                 clone->next = head->next;
651                 head->next = clone;
652                 skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
653                 skb_shinfo(head)->frag_list = NULL;
654                 for (i=0; i<skb_shinfo(head)->nr_frags; i++)
655                         plen += skb_shinfo(head)->frags[i].size;
656                 clone->len = clone->data_len = head->data_len - plen;
657                 head->data_len -= clone->len;
658                 head->len -= clone->len;
659                 clone->csum = 0;
660                 clone->ip_summed = head->ip_summed;
661                 atomic_add(clone->truesize, &ip6_frags.mem);
662         }
663
664         /* We have to remove fragment header from datagram and to relocate
665          * header in order to calculate ICV correctly. */
666         nhoff = fq->nhoffset;
667         skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
668         memmove(head->head + sizeof(struct frag_hdr), head->head,
669                 (head->data - head->head) - sizeof(struct frag_hdr));
670         head->mac_header += sizeof(struct frag_hdr);
671         head->network_header += sizeof(struct frag_hdr);
672
673         skb_shinfo(head)->frag_list = head->next;
674         skb_reset_transport_header(head);
675         skb_push(head, head->data - skb_network_header(head));
676         atomic_sub(head->truesize, &ip6_frags.mem);
677
678         for (fp=head->next; fp; fp = fp->next) {
679                 head->data_len += fp->len;
680                 head->len += fp->len;
681                 if (head->ip_summed != fp->ip_summed)
682                         head->ip_summed = CHECKSUM_NONE;
683                 else if (head->ip_summed == CHECKSUM_COMPLETE)
684                         head->csum = csum_add(head->csum, fp->csum);
685                 head->truesize += fp->truesize;
686                 atomic_sub(fp->truesize, &ip6_frags.mem);
687         }
688
689         head->next = NULL;
690         head->dev = dev;
691         head->tstamp = fq->q.stamp;
692         ipv6_hdr(head)->payload_len = htons(payload_len);
693         IP6CB(head)->nhoff = nhoff;
694
695         /* Yes, and fold redundant checksum back. 8) */
696         if (head->ip_summed == CHECKSUM_COMPLETE)
697                 head->csum = csum_partial(skb_network_header(head),
698                                           skb_network_header_len(head),
699                                           head->csum);
700
701         rcu_read_lock();
702         IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
703         rcu_read_unlock();
704         fq->q.fragments = NULL;
705         return 1;
706
707 out_oversize:
708         if (net_ratelimit())
709                 printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len);
710         goto out_fail;
711 out_oom:
712         if (net_ratelimit())
713                 printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n");
714 out_fail:
715         rcu_read_lock();
716         IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
717         rcu_read_unlock();
718         return -1;
719 }
720
721 static int ipv6_frag_rcv(struct sk_buff **skbp)
722 {
723         struct sk_buff *skb = *skbp;
724         struct frag_hdr *fhdr;
725         struct frag_queue *fq;
726         struct ipv6hdr *hdr = ipv6_hdr(skb);
727
728         IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMREQDS);
729
730         /* Jumbo payload inhibits frag. header */
731         if (hdr->payload_len==0) {
732                 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
733                 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
734                                   skb_network_header_len(skb));
735                 return -1;
736         }
737         if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
738                                  sizeof(struct frag_hdr)))) {
739                 IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
740                 icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
741                                   skb_network_header_len(skb));
742                 return -1;
743         }
744
745         hdr = ipv6_hdr(skb);
746         fhdr = (struct frag_hdr *)skb_transport_header(skb);
747
748         if (!(fhdr->frag_off & htons(0xFFF9))) {
749                 /* It is not a fragmented frame */
750                 skb->transport_header += sizeof(struct frag_hdr);
751                 IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMOKS);
752
753                 IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
754                 return 1;
755         }
756
757         if (atomic_read(&ip6_frags.mem) > sysctl_ip6frag_high_thresh)
758                 ip6_evictor(ip6_dst_idev(skb->dst));
759
760         if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr,
761                           ip6_dst_idev(skb->dst))) != NULL) {
762                 int ret;
763
764                 spin_lock(&fq->q.lock);
765
766                 ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
767
768                 spin_unlock(&fq->q.lock);
769                 fq_put(fq, NULL);
770                 return ret;
771         }
772
773         IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
774         kfree_skb(skb);
775         return -1;
776 }
777
778 static struct inet6_protocol frag_protocol =
779 {
780         .handler        =       ipv6_frag_rcv,
781         .flags          =       INET6_PROTO_NOPOLICY,
782 };
783
784 void __init ipv6_frag_init(void)
785 {
786         if (inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT) < 0)
787                 printk(KERN_ERR "ipv6_frag_init: Could not register protocol\n");
788
789         init_timer(&ip6_frags.secret_timer);
790         ip6_frags.secret_timer.function = ip6_frag_secret_rebuild;
791         ip6_frags.secret_timer.expires = jiffies + sysctl_ip6frag_secret_interval;
792         add_timer(&ip6_frags.secret_timer);
793
794         inet_frags_init(&ip6_frags);
795 }