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