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