xref: /openbmc/linux/net/ipv6/reassembly.c (revision 42ca89c1)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	IPv6 fragment reassembly
31da177e4SLinus Torvalds  *	Linux INET6 implementation
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	Authors:
61da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *	$Id: reassembly.c,v 1.26 2001/03/07 22:00:57 davem Exp $
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *	Based on: net/ipv4/ip_fragment.c
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
131da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
141da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
151da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds /*
191da177e4SLinus Torvalds  *	Fixes:
201da177e4SLinus Torvalds  *	Andi Kleen	Make it work with multiple hosts.
211da177e4SLinus Torvalds  *			More RFC compliance.
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  *      Horst von Brand Add missing #include <linux/string.h>
241da177e4SLinus Torvalds  *	Alexey Kuznetsov	SMP races, threading, cleanup.
251da177e4SLinus Torvalds  *	Patrick McHardy		LRU queue of frag heads for evictor.
261da177e4SLinus Torvalds  *	Mitsuru KANDA @USAGI	Register inet6_protocol{}.
271da177e4SLinus Torvalds  *	David Stevens and
281da177e4SLinus Torvalds  *	YOSHIFUJI,H. @USAGI	Always remove fragment header to
291da177e4SLinus Torvalds  *				calculate ICV correctly.
301da177e4SLinus Torvalds  */
311da177e4SLinus Torvalds #include <linux/config.h>
321da177e4SLinus Torvalds #include <linux/errno.h>
331da177e4SLinus Torvalds #include <linux/types.h>
341da177e4SLinus Torvalds #include <linux/string.h>
351da177e4SLinus Torvalds #include <linux/socket.h>
361da177e4SLinus Torvalds #include <linux/sockios.h>
371da177e4SLinus Torvalds #include <linux/jiffies.h>
381da177e4SLinus Torvalds #include <linux/net.h>
391da177e4SLinus Torvalds #include <linux/list.h>
401da177e4SLinus Torvalds #include <linux/netdevice.h>
411da177e4SLinus Torvalds #include <linux/in6.h>
421da177e4SLinus Torvalds #include <linux/ipv6.h>
431da177e4SLinus Torvalds #include <linux/icmpv6.h>
441da177e4SLinus Torvalds #include <linux/random.h>
451da177e4SLinus Torvalds #include <linux/jhash.h>
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #include <net/sock.h>
481da177e4SLinus Torvalds #include <net/snmp.h>
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #include <net/ipv6.h>
511da177e4SLinus Torvalds #include <net/protocol.h>
521da177e4SLinus Torvalds #include <net/transp_v6.h>
531da177e4SLinus Torvalds #include <net/rawv6.h>
541da177e4SLinus Torvalds #include <net/ndisc.h>
551da177e4SLinus Torvalds #include <net/addrconf.h>
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds int sysctl_ip6frag_high_thresh = 256*1024;
581da177e4SLinus Torvalds int sysctl_ip6frag_low_thresh = 192*1024;
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds int sysctl_ip6frag_time = IPV6_FRAG_TIMEOUT;
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds struct ip6frag_skb_cb
631da177e4SLinus Torvalds {
641da177e4SLinus Torvalds 	struct inet6_skb_parm	h;
651da177e4SLinus Torvalds 	int			offset;
661da177e4SLinus Torvalds };
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds #define FRAG6_CB(skb)	((struct ip6frag_skb_cb*)((skb)->cb))
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds /*
721da177e4SLinus Torvalds  *	Equivalent of ipv4 struct ipq
731da177e4SLinus Torvalds  */
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds struct frag_queue
761da177e4SLinus Torvalds {
771da177e4SLinus Torvalds 	struct frag_queue	*next;
781da177e4SLinus Torvalds 	struct list_head lru_list;		/* lru list member	*/
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	__u32			id;		/* fragment id		*/
811da177e4SLinus Torvalds 	struct in6_addr		saddr;
821da177e4SLinus Torvalds 	struct in6_addr		daddr;
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 	spinlock_t		lock;
851da177e4SLinus Torvalds 	atomic_t		refcnt;
861da177e4SLinus Torvalds 	struct timer_list	timer;		/* expire timer		*/
871da177e4SLinus Torvalds 	struct sk_buff		*fragments;
881da177e4SLinus Torvalds 	int			len;
891da177e4SLinus Torvalds 	int			meat;
901da177e4SLinus Torvalds 	int			iif;
911da177e4SLinus Torvalds 	struct timeval		stamp;
921da177e4SLinus Torvalds 	unsigned int		csum;
931da177e4SLinus Torvalds 	__u8			last_in;	/* has first/last segment arrived? */
941da177e4SLinus Torvalds #define COMPLETE		4
951da177e4SLinus Torvalds #define FIRST_IN		2
961da177e4SLinus Torvalds #define LAST_IN			1
971da177e4SLinus Torvalds 	__u16			nhoffset;
981da177e4SLinus Torvalds 	struct frag_queue	**pprev;
991da177e4SLinus Torvalds };
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds /* Hash table. */
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds #define IP6Q_HASHSZ	64
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds static struct frag_queue *ip6_frag_hash[IP6Q_HASHSZ];
1061da177e4SLinus Torvalds static DEFINE_RWLOCK(ip6_frag_lock);
1071da177e4SLinus Torvalds static u32 ip6_frag_hash_rnd;
1081da177e4SLinus Torvalds static LIST_HEAD(ip6_frag_lru_list);
1091da177e4SLinus Torvalds int ip6_frag_nqueues = 0;
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds static __inline__ void __fq_unlink(struct frag_queue *fq)
1121da177e4SLinus Torvalds {
1131da177e4SLinus Torvalds 	if(fq->next)
1141da177e4SLinus Torvalds 		fq->next->pprev = fq->pprev;
1151da177e4SLinus Torvalds 	*fq->pprev = fq->next;
1161da177e4SLinus Torvalds 	list_del(&fq->lru_list);
1171da177e4SLinus Torvalds 	ip6_frag_nqueues--;
1181da177e4SLinus Torvalds }
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds static __inline__ void fq_unlink(struct frag_queue *fq)
1211da177e4SLinus Torvalds {
1221da177e4SLinus Torvalds 	write_lock(&ip6_frag_lock);
1231da177e4SLinus Torvalds 	__fq_unlink(fq);
1241da177e4SLinus Torvalds 	write_unlock(&ip6_frag_lock);
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds static unsigned int ip6qhashfn(u32 id, struct in6_addr *saddr,
1281da177e4SLinus Torvalds 			       struct in6_addr *daddr)
1291da177e4SLinus Torvalds {
1301da177e4SLinus Torvalds 	u32 a, b, c;
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	a = saddr->s6_addr32[0];
1331da177e4SLinus Torvalds 	b = saddr->s6_addr32[1];
1341da177e4SLinus Torvalds 	c = saddr->s6_addr32[2];
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	a += JHASH_GOLDEN_RATIO;
1371da177e4SLinus Torvalds 	b += JHASH_GOLDEN_RATIO;
1381da177e4SLinus Torvalds 	c += ip6_frag_hash_rnd;
1391da177e4SLinus Torvalds 	__jhash_mix(a, b, c);
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 	a += saddr->s6_addr32[3];
1421da177e4SLinus Torvalds 	b += daddr->s6_addr32[0];
1431da177e4SLinus Torvalds 	c += daddr->s6_addr32[1];
1441da177e4SLinus Torvalds 	__jhash_mix(a, b, c);
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	a += daddr->s6_addr32[2];
1471da177e4SLinus Torvalds 	b += daddr->s6_addr32[3];
1481da177e4SLinus Torvalds 	c += id;
1491da177e4SLinus Torvalds 	__jhash_mix(a, b, c);
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	return c & (IP6Q_HASHSZ - 1);
1521da177e4SLinus Torvalds }
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds static struct timer_list ip6_frag_secret_timer;
1551da177e4SLinus Torvalds int sysctl_ip6frag_secret_interval = 10 * 60 * HZ;
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds static void ip6_frag_secret_rebuild(unsigned long dummy)
1581da177e4SLinus Torvalds {
1591da177e4SLinus Torvalds 	unsigned long now = jiffies;
1601da177e4SLinus Torvalds 	int i;
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	write_lock(&ip6_frag_lock);
1631da177e4SLinus Torvalds 	get_random_bytes(&ip6_frag_hash_rnd, sizeof(u32));
1641da177e4SLinus Torvalds 	for (i = 0; i < IP6Q_HASHSZ; i++) {
1651da177e4SLinus Torvalds 		struct frag_queue *q;
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 		q = ip6_frag_hash[i];
1681da177e4SLinus Torvalds 		while (q) {
1691da177e4SLinus Torvalds 			struct frag_queue *next = q->next;
1701da177e4SLinus Torvalds 			unsigned int hval = ip6qhashfn(q->id,
1711da177e4SLinus Torvalds 						       &q->saddr,
1721da177e4SLinus Torvalds 						       &q->daddr);
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 			if (hval != i) {
1751da177e4SLinus Torvalds 				/* Unlink. */
1761da177e4SLinus Torvalds 				if (q->next)
1771da177e4SLinus Torvalds 					q->next->pprev = q->pprev;
1781da177e4SLinus Torvalds 				*q->pprev = q->next;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 				/* Relink to new hash chain. */
1811da177e4SLinus Torvalds 				if ((q->next = ip6_frag_hash[hval]) != NULL)
1821da177e4SLinus Torvalds 					q->next->pprev = &q->next;
1831da177e4SLinus Torvalds 				ip6_frag_hash[hval] = q;
1841da177e4SLinus Torvalds 				q->pprev = &ip6_frag_hash[hval];
1851da177e4SLinus Torvalds 			}
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 			q = next;
1881da177e4SLinus Torvalds 		}
1891da177e4SLinus Torvalds 	}
1901da177e4SLinus Torvalds 	write_unlock(&ip6_frag_lock);
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	mod_timer(&ip6_frag_secret_timer, now + sysctl_ip6frag_secret_interval);
1931da177e4SLinus Torvalds }
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds atomic_t ip6_frag_mem = ATOMIC_INIT(0);
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds /* Memory Tracking Functions. */
1981da177e4SLinus Torvalds static inline void frag_kfree_skb(struct sk_buff *skb, int *work)
1991da177e4SLinus Torvalds {
2001da177e4SLinus Torvalds 	if (work)
2011da177e4SLinus Torvalds 		*work -= skb->truesize;
2021da177e4SLinus Torvalds 	atomic_sub(skb->truesize, &ip6_frag_mem);
2031da177e4SLinus Torvalds 	kfree_skb(skb);
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds static inline void frag_free_queue(struct frag_queue *fq, int *work)
2071da177e4SLinus Torvalds {
2081da177e4SLinus Torvalds 	if (work)
2091da177e4SLinus Torvalds 		*work -= sizeof(struct frag_queue);
2101da177e4SLinus Torvalds 	atomic_sub(sizeof(struct frag_queue), &ip6_frag_mem);
2111da177e4SLinus Torvalds 	kfree(fq);
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds static inline struct frag_queue *frag_alloc_queue(void)
2151da177e4SLinus Torvalds {
2161da177e4SLinus Torvalds 	struct frag_queue *fq = kmalloc(sizeof(struct frag_queue), GFP_ATOMIC);
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	if(!fq)
2191da177e4SLinus Torvalds 		return NULL;
2201da177e4SLinus Torvalds 	atomic_add(sizeof(struct frag_queue), &ip6_frag_mem);
2211da177e4SLinus Torvalds 	return fq;
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds /* Destruction primitives. */
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds /* Complete destruction of fq. */
2271da177e4SLinus Torvalds static void ip6_frag_destroy(struct frag_queue *fq, int *work)
2281da177e4SLinus Torvalds {
2291da177e4SLinus Torvalds 	struct sk_buff *fp;
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 	BUG_TRAP(fq->last_in&COMPLETE);
2321da177e4SLinus Torvalds 	BUG_TRAP(del_timer(&fq->timer) == 0);
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	/* Release all fragment data. */
2351da177e4SLinus Torvalds 	fp = fq->fragments;
2361da177e4SLinus Torvalds 	while (fp) {
2371da177e4SLinus Torvalds 		struct sk_buff *xp = fp->next;
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 		frag_kfree_skb(fp, work);
2401da177e4SLinus Torvalds 		fp = xp;
2411da177e4SLinus Torvalds 	}
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	frag_free_queue(fq, work);
2441da177e4SLinus Torvalds }
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds static __inline__ void fq_put(struct frag_queue *fq, int *work)
2471da177e4SLinus Torvalds {
2481da177e4SLinus Torvalds 	if (atomic_dec_and_test(&fq->refcnt))
2491da177e4SLinus Torvalds 		ip6_frag_destroy(fq, work);
2501da177e4SLinus Torvalds }
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds /* Kill fq entry. It is not destroyed immediately,
2531da177e4SLinus Torvalds  * because caller (and someone more) holds reference count.
2541da177e4SLinus Torvalds  */
2551da177e4SLinus Torvalds static __inline__ void fq_kill(struct frag_queue *fq)
2561da177e4SLinus Torvalds {
2571da177e4SLinus Torvalds 	if (del_timer(&fq->timer))
2581da177e4SLinus Torvalds 		atomic_dec(&fq->refcnt);
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	if (!(fq->last_in & COMPLETE)) {
2611da177e4SLinus Torvalds 		fq_unlink(fq);
2621da177e4SLinus Torvalds 		atomic_dec(&fq->refcnt);
2631da177e4SLinus Torvalds 		fq->last_in |= COMPLETE;
2641da177e4SLinus Torvalds 	}
2651da177e4SLinus Torvalds }
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds static void ip6_evictor(void)
2681da177e4SLinus Torvalds {
2691da177e4SLinus Torvalds 	struct frag_queue *fq;
2701da177e4SLinus Torvalds 	struct list_head *tmp;
2711da177e4SLinus Torvalds 	int work;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	work = atomic_read(&ip6_frag_mem) - sysctl_ip6frag_low_thresh;
2741da177e4SLinus Torvalds 	if (work <= 0)
2751da177e4SLinus Torvalds 		return;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	while(work > 0) {
2781da177e4SLinus Torvalds 		read_lock(&ip6_frag_lock);
2791da177e4SLinus Torvalds 		if (list_empty(&ip6_frag_lru_list)) {
2801da177e4SLinus Torvalds 			read_unlock(&ip6_frag_lock);
2811da177e4SLinus Torvalds 			return;
2821da177e4SLinus Torvalds 		}
2831da177e4SLinus Torvalds 		tmp = ip6_frag_lru_list.next;
2841da177e4SLinus Torvalds 		fq = list_entry(tmp, struct frag_queue, lru_list);
2851da177e4SLinus Torvalds 		atomic_inc(&fq->refcnt);
2861da177e4SLinus Torvalds 		read_unlock(&ip6_frag_lock);
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 		spin_lock(&fq->lock);
2891da177e4SLinus Torvalds 		if (!(fq->last_in&COMPLETE))
2901da177e4SLinus Torvalds 			fq_kill(fq);
2911da177e4SLinus Torvalds 		spin_unlock(&fq->lock);
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 		fq_put(fq, &work);
2941da177e4SLinus Torvalds 		IP6_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
2951da177e4SLinus Torvalds 	}
2961da177e4SLinus Torvalds }
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds static void ip6_frag_expire(unsigned long data)
2991da177e4SLinus Torvalds {
3001da177e4SLinus Torvalds 	struct frag_queue *fq = (struct frag_queue *) data;
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	spin_lock(&fq->lock);
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	if (fq->last_in & COMPLETE)
3051da177e4SLinus Torvalds 		goto out;
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	fq_kill(fq);
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	IP6_INC_STATS_BH(IPSTATS_MIB_REASMTIMEOUT);
3101da177e4SLinus Torvalds 	IP6_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	/* Send error only if the first segment arrived. */
3131da177e4SLinus Torvalds 	if (fq->last_in&FIRST_IN && fq->fragments) {
3141da177e4SLinus Torvalds 		struct net_device *dev = dev_get_by_index(fq->iif);
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 		/*
3171da177e4SLinus Torvalds 		   But use as source device on which LAST ARRIVED
3181da177e4SLinus Torvalds 		   segment was received. And do not use fq->dev
3191da177e4SLinus Torvalds 		   pointer directly, device might already disappeared.
3201da177e4SLinus Torvalds 		 */
3211da177e4SLinus Torvalds 		if (dev) {
3221da177e4SLinus Torvalds 			fq->fragments->dev = dev;
3231da177e4SLinus Torvalds 			icmpv6_send(fq->fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0,
3241da177e4SLinus Torvalds 				    dev);
3251da177e4SLinus Torvalds 			dev_put(dev);
3261da177e4SLinus Torvalds 		}
3271da177e4SLinus Torvalds 	}
3281da177e4SLinus Torvalds out:
3291da177e4SLinus Torvalds 	spin_unlock(&fq->lock);
3301da177e4SLinus Torvalds 	fq_put(fq, NULL);
3311da177e4SLinus Torvalds }
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds /* Creation primitives. */
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds static struct frag_queue *ip6_frag_intern(unsigned int hash,
3371da177e4SLinus Torvalds 					  struct frag_queue *fq_in)
3381da177e4SLinus Torvalds {
3391da177e4SLinus Torvalds 	struct frag_queue *fq;
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	write_lock(&ip6_frag_lock);
3421da177e4SLinus Torvalds #ifdef CONFIG_SMP
3431da177e4SLinus Torvalds 	for (fq = ip6_frag_hash[hash]; fq; fq = fq->next) {
3441da177e4SLinus Torvalds 		if (fq->id == fq_in->id &&
3451da177e4SLinus Torvalds 		    ipv6_addr_equal(&fq_in->saddr, &fq->saddr) &&
3461da177e4SLinus Torvalds 		    ipv6_addr_equal(&fq_in->daddr, &fq->daddr)) {
3471da177e4SLinus Torvalds 			atomic_inc(&fq->refcnt);
3481da177e4SLinus Torvalds 			write_unlock(&ip6_frag_lock);
3491da177e4SLinus Torvalds 			fq_in->last_in |= COMPLETE;
3501da177e4SLinus Torvalds 			fq_put(fq_in, NULL);
3511da177e4SLinus Torvalds 			return fq;
3521da177e4SLinus Torvalds 		}
3531da177e4SLinus Torvalds 	}
3541da177e4SLinus Torvalds #endif
3551da177e4SLinus Torvalds 	fq = fq_in;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	if (!mod_timer(&fq->timer, jiffies + sysctl_ip6frag_time))
3581da177e4SLinus Torvalds 		atomic_inc(&fq->refcnt);
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	atomic_inc(&fq->refcnt);
3611da177e4SLinus Torvalds 	if((fq->next = ip6_frag_hash[hash]) != NULL)
3621da177e4SLinus Torvalds 		fq->next->pprev = &fq->next;
3631da177e4SLinus Torvalds 	ip6_frag_hash[hash] = fq;
3641da177e4SLinus Torvalds 	fq->pprev = &ip6_frag_hash[hash];
3651da177e4SLinus Torvalds 	INIT_LIST_HEAD(&fq->lru_list);
3661da177e4SLinus Torvalds 	list_add_tail(&fq->lru_list, &ip6_frag_lru_list);
3671da177e4SLinus Torvalds 	ip6_frag_nqueues++;
3681da177e4SLinus Torvalds 	write_unlock(&ip6_frag_lock);
3691da177e4SLinus Torvalds 	return fq;
3701da177e4SLinus Torvalds }
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds static struct frag_queue *
3741da177e4SLinus Torvalds ip6_frag_create(unsigned int hash, u32 id, struct in6_addr *src, struct in6_addr *dst)
3751da177e4SLinus Torvalds {
3761da177e4SLinus Torvalds 	struct frag_queue *fq;
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	if ((fq = frag_alloc_queue()) == NULL)
3791da177e4SLinus Torvalds 		goto oom;
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	memset(fq, 0, sizeof(struct frag_queue));
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	fq->id = id;
3841da177e4SLinus Torvalds 	ipv6_addr_copy(&fq->saddr, src);
3851da177e4SLinus Torvalds 	ipv6_addr_copy(&fq->daddr, dst);
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	init_timer(&fq->timer);
3881da177e4SLinus Torvalds 	fq->timer.function = ip6_frag_expire;
3891da177e4SLinus Torvalds 	fq->timer.data = (long) fq;
3901da177e4SLinus Torvalds 	spin_lock_init(&fq->lock);
3911da177e4SLinus Torvalds 	atomic_set(&fq->refcnt, 1);
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	return ip6_frag_intern(hash, fq);
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds oom:
3961da177e4SLinus Torvalds 	IP6_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
3971da177e4SLinus Torvalds 	return NULL;
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds static __inline__ struct frag_queue *
4011da177e4SLinus Torvalds fq_find(u32 id, struct in6_addr *src, struct in6_addr *dst)
4021da177e4SLinus Torvalds {
4031da177e4SLinus Torvalds 	struct frag_queue *fq;
4041da177e4SLinus Torvalds 	unsigned int hash = ip6qhashfn(id, src, dst);
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	read_lock(&ip6_frag_lock);
4071da177e4SLinus Torvalds 	for(fq = ip6_frag_hash[hash]; fq; fq = fq->next) {
4081da177e4SLinus Torvalds 		if (fq->id == id &&
4091da177e4SLinus Torvalds 		    ipv6_addr_equal(src, &fq->saddr) &&
4101da177e4SLinus Torvalds 		    ipv6_addr_equal(dst, &fq->daddr)) {
4111da177e4SLinus Torvalds 			atomic_inc(&fq->refcnt);
4121da177e4SLinus Torvalds 			read_unlock(&ip6_frag_lock);
4131da177e4SLinus Torvalds 			return fq;
4141da177e4SLinus Torvalds 		}
4151da177e4SLinus Torvalds 	}
4161da177e4SLinus Torvalds 	read_unlock(&ip6_frag_lock);
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	return ip6_frag_create(hash, id, src, dst);
4191da177e4SLinus Torvalds }
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds static void ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
4231da177e4SLinus Torvalds 			   struct frag_hdr *fhdr, int nhoff)
4241da177e4SLinus Torvalds {
4251da177e4SLinus Torvalds 	struct sk_buff *prev, *next;
4261da177e4SLinus Torvalds 	int offset, end;
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	if (fq->last_in & COMPLETE)
4291da177e4SLinus Torvalds 		goto err;
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	offset = ntohs(fhdr->frag_off) & ~0x7;
4321da177e4SLinus Torvalds 	end = offset + (ntohs(skb->nh.ipv6h->payload_len) -
4331da177e4SLinus Torvalds 			((u8 *) (fhdr + 1) - (u8 *) (skb->nh.ipv6h + 1)));
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 	if ((unsigned int)end > IPV6_MAXPLEN) {
4361da177e4SLinus Torvalds 		IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
4371da177e4SLinus Torvalds  		icmpv6_param_prob(skb,ICMPV6_HDR_FIELD, (u8*)&fhdr->frag_off - skb->nh.raw);
4381da177e4SLinus Torvalds  		return;
4391da177e4SLinus Torvalds 	}
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds  	if (skb->ip_summed == CHECKSUM_HW)
4421da177e4SLinus Torvalds  		skb->csum = csum_sub(skb->csum,
4431da177e4SLinus Torvalds  				     csum_partial(skb->nh.raw, (u8*)(fhdr+1)-skb->nh.raw, 0));
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	/* Is this the final fragment? */
4461da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(IP6_MF))) {
4471da177e4SLinus Torvalds 		/* If we already have some bits beyond end
4481da177e4SLinus Torvalds 		 * or have different end, the segment is corrupted.
4491da177e4SLinus Torvalds 		 */
4501da177e4SLinus Torvalds 		if (end < fq->len ||
4511da177e4SLinus Torvalds 		    ((fq->last_in & LAST_IN) && end != fq->len))
4521da177e4SLinus Torvalds 			goto err;
4531da177e4SLinus Torvalds 		fq->last_in |= LAST_IN;
4541da177e4SLinus Torvalds 		fq->len = end;
4551da177e4SLinus Torvalds 	} else {
4561da177e4SLinus Torvalds 		/* Check if the fragment is rounded to 8 bytes.
4571da177e4SLinus Torvalds 		 * Required by the RFC.
4581da177e4SLinus Torvalds 		 */
4591da177e4SLinus Torvalds 		if (end & 0x7) {
4601da177e4SLinus Torvalds 			/* RFC2460 says always send parameter problem in
4611da177e4SLinus Torvalds 			 * this case. -DaveM
4621da177e4SLinus Torvalds 			 */
4631da177e4SLinus Torvalds 			IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
4641da177e4SLinus Torvalds 			icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
4651da177e4SLinus Torvalds 					  offsetof(struct ipv6hdr, payload_len));
4661da177e4SLinus Torvalds 			return;
4671da177e4SLinus Torvalds 		}
4681da177e4SLinus Torvalds 		if (end > fq->len) {
4691da177e4SLinus Torvalds 			/* Some bits beyond end -> corruption. */
4701da177e4SLinus Torvalds 			if (fq->last_in & LAST_IN)
4711da177e4SLinus Torvalds 				goto err;
4721da177e4SLinus Torvalds 			fq->len = end;
4731da177e4SLinus Torvalds 		}
4741da177e4SLinus Torvalds 	}
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	if (end == offset)
4771da177e4SLinus Torvalds 		goto err;
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 	/* Point into the IP datagram 'data' part. */
4801da177e4SLinus Torvalds 	if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
4811da177e4SLinus Torvalds 		goto err;
48242ca89c1SStephen Hemminger 
48342ca89c1SStephen Hemminger 	if (pskb_trim_rcsum(skb, end - offset))
4841da177e4SLinus Torvalds 		goto err;
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	/* Find out which fragments are in front and at the back of us
4871da177e4SLinus Torvalds 	 * in the chain of fragments so far.  We must know where to put
4881da177e4SLinus Torvalds 	 * this fragment, right?
4891da177e4SLinus Torvalds 	 */
4901da177e4SLinus Torvalds 	prev = NULL;
4911da177e4SLinus Torvalds 	for(next = fq->fragments; next != NULL; next = next->next) {
4921da177e4SLinus Torvalds 		if (FRAG6_CB(next)->offset >= offset)
4931da177e4SLinus Torvalds 			break;	/* bingo! */
4941da177e4SLinus Torvalds 		prev = next;
4951da177e4SLinus Torvalds 	}
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	/* We found where to put this one.  Check for overlap with
4981da177e4SLinus Torvalds 	 * preceding fragment, and, if needed, align things so that
4991da177e4SLinus Torvalds 	 * any overlaps are eliminated.
5001da177e4SLinus Torvalds 	 */
5011da177e4SLinus Torvalds 	if (prev) {
5021da177e4SLinus Torvalds 		int i = (FRAG6_CB(prev)->offset + prev->len) - offset;
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 		if (i > 0) {
5051da177e4SLinus Torvalds 			offset += i;
5061da177e4SLinus Torvalds 			if (end <= offset)
5071da177e4SLinus Torvalds 				goto err;
5081da177e4SLinus Torvalds 			if (!pskb_pull(skb, i))
5091da177e4SLinus Torvalds 				goto err;
5101da177e4SLinus Torvalds 			if (skb->ip_summed != CHECKSUM_UNNECESSARY)
5111da177e4SLinus Torvalds 				skb->ip_summed = CHECKSUM_NONE;
5121da177e4SLinus Torvalds 		}
5131da177e4SLinus Torvalds 	}
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	/* Look for overlap with succeeding segments.
5161da177e4SLinus Torvalds 	 * If we can merge fragments, do it.
5171da177e4SLinus Torvalds 	 */
5181da177e4SLinus Torvalds 	while (next && FRAG6_CB(next)->offset < end) {
5191da177e4SLinus Torvalds 		int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 		if (i < next->len) {
5221da177e4SLinus Torvalds 			/* Eat head of the next overlapped fragment
5231da177e4SLinus Torvalds 			 * and leave the loop. The next ones cannot overlap.
5241da177e4SLinus Torvalds 			 */
5251da177e4SLinus Torvalds 			if (!pskb_pull(next, i))
5261da177e4SLinus Torvalds 				goto err;
5271da177e4SLinus Torvalds 			FRAG6_CB(next)->offset += i;	/* next fragment */
5281da177e4SLinus Torvalds 			fq->meat -= i;
5291da177e4SLinus Torvalds 			if (next->ip_summed != CHECKSUM_UNNECESSARY)
5301da177e4SLinus Torvalds 				next->ip_summed = CHECKSUM_NONE;
5311da177e4SLinus Torvalds 			break;
5321da177e4SLinus Torvalds 		} else {
5331da177e4SLinus Torvalds 			struct sk_buff *free_it = next;
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds 			/* Old fragment is completely overridden with
5361da177e4SLinus Torvalds 			 * new one drop it.
5371da177e4SLinus Torvalds 			 */
5381da177e4SLinus Torvalds 			next = next->next;
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 			if (prev)
5411da177e4SLinus Torvalds 				prev->next = next;
5421da177e4SLinus Torvalds 			else
5431da177e4SLinus Torvalds 				fq->fragments = next;
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 			fq->meat -= free_it->len;
5461da177e4SLinus Torvalds 			frag_kfree_skb(free_it, NULL);
5471da177e4SLinus Torvalds 		}
5481da177e4SLinus Torvalds 	}
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 	FRAG6_CB(skb)->offset = offset;
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 	/* Insert this fragment in the chain of fragments. */
5531da177e4SLinus Torvalds 	skb->next = next;
5541da177e4SLinus Torvalds 	if (prev)
5551da177e4SLinus Torvalds 		prev->next = skb;
5561da177e4SLinus Torvalds 	else
5571da177e4SLinus Torvalds 		fq->fragments = skb;
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	if (skb->dev)
5601da177e4SLinus Torvalds 		fq->iif = skb->dev->ifindex;
5611da177e4SLinus Torvalds 	skb->dev = NULL;
562a61bbcf2SPatrick McHardy 	skb_get_timestamp(skb, &fq->stamp);
5631da177e4SLinus Torvalds 	fq->meat += skb->len;
5641da177e4SLinus Torvalds 	atomic_add(skb->truesize, &ip6_frag_mem);
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	/* The first fragment.
5671da177e4SLinus Torvalds 	 * nhoffset is obtained from the first fragment, of course.
5681da177e4SLinus Torvalds 	 */
5691da177e4SLinus Torvalds 	if (offset == 0) {
5701da177e4SLinus Torvalds 		fq->nhoffset = nhoff;
5711da177e4SLinus Torvalds 		fq->last_in |= FIRST_IN;
5721da177e4SLinus Torvalds 	}
5731da177e4SLinus Torvalds 	write_lock(&ip6_frag_lock);
5741da177e4SLinus Torvalds 	list_move_tail(&fq->lru_list, &ip6_frag_lru_list);
5751da177e4SLinus Torvalds 	write_unlock(&ip6_frag_lock);
5761da177e4SLinus Torvalds 	return;
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds err:
5791da177e4SLinus Torvalds 	IP6_INC_STATS(IPSTATS_MIB_REASMFAILS);
5801da177e4SLinus Torvalds 	kfree_skb(skb);
5811da177e4SLinus Torvalds }
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds /*
5841da177e4SLinus Torvalds  *	Check if this packet is complete.
5851da177e4SLinus Torvalds  *	Returns NULL on failure by any reason, and pointer
5861da177e4SLinus Torvalds  *	to current nexthdr field in reassembled frame.
5871da177e4SLinus Torvalds  *
5881da177e4SLinus Torvalds  *	It is called with locked fq, and caller must check that
5891da177e4SLinus Torvalds  *	queue is eligible for reassembly i.e. it is not COMPLETE,
5901da177e4SLinus Torvalds  *	the last and the first frames arrived and all the bits are here.
5911da177e4SLinus Torvalds  */
5921da177e4SLinus Torvalds static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff **skb_in,
5931da177e4SLinus Torvalds 			  unsigned int *nhoffp,
5941da177e4SLinus Torvalds 			  struct net_device *dev)
5951da177e4SLinus Torvalds {
5961da177e4SLinus Torvalds 	struct sk_buff *fp, *head = fq->fragments;
5971da177e4SLinus Torvalds 	int    payload_len;
5981da177e4SLinus Torvalds 	unsigned int nhoff;
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds 	fq_kill(fq);
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	BUG_TRAP(head != NULL);
6031da177e4SLinus Torvalds 	BUG_TRAP(FRAG6_CB(head)->offset == 0);
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	/* Unfragmented part is taken from the first segment. */
6061da177e4SLinus Torvalds 	payload_len = (head->data - head->nh.raw) - sizeof(struct ipv6hdr) + fq->len - sizeof(struct frag_hdr);
6071da177e4SLinus Torvalds 	if (payload_len > IPV6_MAXPLEN)
6081da177e4SLinus Torvalds 		goto out_oversize;
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	/* Head of list must not be cloned. */
6111da177e4SLinus Torvalds 	if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
6121da177e4SLinus Torvalds 		goto out_oom;
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 	/* If the first fragment is fragmented itself, we split
6151da177e4SLinus Torvalds 	 * it to two chunks: the first with data and paged part
6161da177e4SLinus Torvalds 	 * and the second, holding only fragments. */
6171da177e4SLinus Torvalds 	if (skb_shinfo(head)->frag_list) {
6181da177e4SLinus Torvalds 		struct sk_buff *clone;
6191da177e4SLinus Torvalds 		int i, plen = 0;
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 		if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
6221da177e4SLinus Torvalds 			goto out_oom;
6231da177e4SLinus Torvalds 		clone->next = head->next;
6241da177e4SLinus Torvalds 		head->next = clone;
6251da177e4SLinus Torvalds 		skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
6261da177e4SLinus Torvalds 		skb_shinfo(head)->frag_list = NULL;
6271da177e4SLinus Torvalds 		for (i=0; i<skb_shinfo(head)->nr_frags; i++)
6281da177e4SLinus Torvalds 			plen += skb_shinfo(head)->frags[i].size;
6291da177e4SLinus Torvalds 		clone->len = clone->data_len = head->data_len - plen;
6301da177e4SLinus Torvalds 		head->data_len -= clone->len;
6311da177e4SLinus Torvalds 		head->len -= clone->len;
6321da177e4SLinus Torvalds 		clone->csum = 0;
6331da177e4SLinus Torvalds 		clone->ip_summed = head->ip_summed;
6341da177e4SLinus Torvalds 		atomic_add(clone->truesize, &ip6_frag_mem);
6351da177e4SLinus Torvalds 	}
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	/* We have to remove fragment header from datagram and to relocate
6381da177e4SLinus Torvalds 	 * header in order to calculate ICV correctly. */
6391da177e4SLinus Torvalds 	nhoff = fq->nhoffset;
6401da177e4SLinus Torvalds 	head->nh.raw[nhoff] = head->h.raw[0];
6411da177e4SLinus Torvalds 	memmove(head->head + sizeof(struct frag_hdr), head->head,
6421da177e4SLinus Torvalds 		(head->data - head->head) - sizeof(struct frag_hdr));
6431da177e4SLinus Torvalds 	head->mac.raw += sizeof(struct frag_hdr);
6441da177e4SLinus Torvalds 	head->nh.raw += sizeof(struct frag_hdr);
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	skb_shinfo(head)->frag_list = head->next;
6471da177e4SLinus Torvalds 	head->h.raw = head->data;
6481da177e4SLinus Torvalds 	skb_push(head, head->data - head->nh.raw);
6491da177e4SLinus Torvalds 	atomic_sub(head->truesize, &ip6_frag_mem);
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	for (fp=head->next; fp; fp = fp->next) {
6521da177e4SLinus Torvalds 		head->data_len += fp->len;
6531da177e4SLinus Torvalds 		head->len += fp->len;
6541da177e4SLinus Torvalds 		if (head->ip_summed != fp->ip_summed)
6551da177e4SLinus Torvalds 			head->ip_summed = CHECKSUM_NONE;
6561da177e4SLinus Torvalds 		else if (head->ip_summed == CHECKSUM_HW)
6571da177e4SLinus Torvalds 			head->csum = csum_add(head->csum, fp->csum);
6581da177e4SLinus Torvalds 		head->truesize += fp->truesize;
6591da177e4SLinus Torvalds 		atomic_sub(fp->truesize, &ip6_frag_mem);
6601da177e4SLinus Torvalds 	}
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds 	head->next = NULL;
6631da177e4SLinus Torvalds 	head->dev = dev;
664a61bbcf2SPatrick McHardy 	skb_set_timestamp(head, &fq->stamp);
6651da177e4SLinus Torvalds 	head->nh.ipv6h->payload_len = htons(payload_len);
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 	*skb_in = head;
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 	/* Yes, and fold redundant checksum back. 8) */
6701da177e4SLinus Torvalds 	if (head->ip_summed == CHECKSUM_HW)
6711da177e4SLinus Torvalds 		head->csum = csum_partial(head->nh.raw, head->h.raw-head->nh.raw, head->csum);
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 	IP6_INC_STATS_BH(IPSTATS_MIB_REASMOKS);
6741da177e4SLinus Torvalds 	fq->fragments = NULL;
6751da177e4SLinus Torvalds 	*nhoffp = nhoff;
6761da177e4SLinus Torvalds 	return 1;
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds out_oversize:
6791da177e4SLinus Torvalds 	if (net_ratelimit())
6801da177e4SLinus Torvalds 		printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len);
6811da177e4SLinus Torvalds 	goto out_fail;
6821da177e4SLinus Torvalds out_oom:
6831da177e4SLinus Torvalds 	if (net_ratelimit())
6841da177e4SLinus Torvalds 		printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n");
6851da177e4SLinus Torvalds out_fail:
6861da177e4SLinus Torvalds 	IP6_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
6871da177e4SLinus Torvalds 	return -1;
6881da177e4SLinus Torvalds }
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds static int ipv6_frag_rcv(struct sk_buff **skbp, unsigned int *nhoffp)
6911da177e4SLinus Torvalds {
6921da177e4SLinus Torvalds 	struct sk_buff *skb = *skbp;
6931da177e4SLinus Torvalds 	struct net_device *dev = skb->dev;
6941da177e4SLinus Torvalds 	struct frag_hdr *fhdr;
6951da177e4SLinus Torvalds 	struct frag_queue *fq;
6961da177e4SLinus Torvalds 	struct ipv6hdr *hdr;
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	hdr = skb->nh.ipv6h;
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 	IP6_INC_STATS_BH(IPSTATS_MIB_REASMREQDS);
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 	/* Jumbo payload inhibits frag. header */
7031da177e4SLinus Torvalds 	if (hdr->payload_len==0) {
7041da177e4SLinus Torvalds 		IP6_INC_STATS(IPSTATS_MIB_INHDRERRORS);
7051da177e4SLinus Torvalds 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb->h.raw-skb->nh.raw);
7061da177e4SLinus Torvalds 		return -1;
7071da177e4SLinus Torvalds 	}
7081da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, (skb->h.raw-skb->data)+sizeof(struct frag_hdr))) {
7091da177e4SLinus Torvalds 		IP6_INC_STATS(IPSTATS_MIB_INHDRERRORS);
7101da177e4SLinus Torvalds 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb->h.raw-skb->nh.raw);
7111da177e4SLinus Torvalds 		return -1;
7121da177e4SLinus Torvalds 	}
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 	hdr = skb->nh.ipv6h;
7151da177e4SLinus Torvalds 	fhdr = (struct frag_hdr *)skb->h.raw;
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(0xFFF9))) {
7181da177e4SLinus Torvalds 		/* It is not a fragmented frame */
7191da177e4SLinus Torvalds 		skb->h.raw += sizeof(struct frag_hdr);
7201da177e4SLinus Torvalds 		IP6_INC_STATS_BH(IPSTATS_MIB_REASMOKS);
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 		*nhoffp = (u8*)fhdr - skb->nh.raw;
7231da177e4SLinus Torvalds 		return 1;
7241da177e4SLinus Torvalds 	}
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	if (atomic_read(&ip6_frag_mem) > sysctl_ip6frag_high_thresh)
7271da177e4SLinus Torvalds 		ip6_evictor();
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 	if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr)) != NULL) {
7301da177e4SLinus Torvalds 		int ret = -1;
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 		spin_lock(&fq->lock);
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds 		ip6_frag_queue(fq, skb, fhdr, *nhoffp);
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds 		if (fq->last_in == (FIRST_IN|LAST_IN) &&
7371da177e4SLinus Torvalds 		    fq->meat == fq->len)
7381da177e4SLinus Torvalds 			ret = ip6_frag_reasm(fq, skbp, nhoffp, dev);
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 		spin_unlock(&fq->lock);
7411da177e4SLinus Torvalds 		fq_put(fq, NULL);
7421da177e4SLinus Torvalds 		return ret;
7431da177e4SLinus Torvalds 	}
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 	IP6_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
7461da177e4SLinus Torvalds 	kfree_skb(skb);
7471da177e4SLinus Torvalds 	return -1;
7481da177e4SLinus Torvalds }
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds static struct inet6_protocol frag_protocol =
7511da177e4SLinus Torvalds {
7521da177e4SLinus Torvalds 	.handler	=	ipv6_frag_rcv,
7531da177e4SLinus Torvalds 	.flags		=	INET6_PROTO_NOPOLICY,
7541da177e4SLinus Torvalds };
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds void __init ipv6_frag_init(void)
7571da177e4SLinus Torvalds {
7581da177e4SLinus Torvalds 	if (inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT) < 0)
7591da177e4SLinus Torvalds 		printk(KERN_ERR "ipv6_frag_init: Could not register protocol\n");
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	ip6_frag_hash_rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
7621da177e4SLinus Torvalds 				   (jiffies ^ (jiffies >> 6)));
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds 	init_timer(&ip6_frag_secret_timer);
7651da177e4SLinus Torvalds 	ip6_frag_secret_timer.function = ip6_frag_secret_rebuild;
7661da177e4SLinus Torvalds 	ip6_frag_secret_timer.expires = jiffies + sysctl_ip6frag_secret_interval;
7671da177e4SLinus Torvalds 	add_timer(&ip6_frag_secret_timer);
7681da177e4SLinus Torvalds }
769