xref: /openbmc/linux/net/ipv6/reassembly.c (revision e7c8a41e817f381ac5c2a59ecc81b483bd68a7df)
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 {
77*e7c8a41eSYasuyuki Kozakai 	struct hlist_node	list;
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 };
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds /* Hash table. */
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds #define IP6Q_HASHSZ	64
1031da177e4SLinus Torvalds 
104*e7c8a41eSYasuyuki Kozakai static struct hlist_head ip6_frag_hash[IP6Q_HASHSZ];
1051da177e4SLinus Torvalds static DEFINE_RWLOCK(ip6_frag_lock);
1061da177e4SLinus Torvalds static u32 ip6_frag_hash_rnd;
1071da177e4SLinus Torvalds static LIST_HEAD(ip6_frag_lru_list);
1081da177e4SLinus Torvalds int ip6_frag_nqueues = 0;
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds static __inline__ void __fq_unlink(struct frag_queue *fq)
1111da177e4SLinus Torvalds {
112*e7c8a41eSYasuyuki Kozakai 	hlist_del(&fq->list);
1131da177e4SLinus Torvalds 	list_del(&fq->lru_list);
1141da177e4SLinus Torvalds 	ip6_frag_nqueues--;
1151da177e4SLinus Torvalds }
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds static __inline__ void fq_unlink(struct frag_queue *fq)
1181da177e4SLinus Torvalds {
1191da177e4SLinus Torvalds 	write_lock(&ip6_frag_lock);
1201da177e4SLinus Torvalds 	__fq_unlink(fq);
1211da177e4SLinus Torvalds 	write_unlock(&ip6_frag_lock);
1221da177e4SLinus Torvalds }
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds static unsigned int ip6qhashfn(u32 id, struct in6_addr *saddr,
1251da177e4SLinus Torvalds 			       struct in6_addr *daddr)
1261da177e4SLinus Torvalds {
1271da177e4SLinus Torvalds 	u32 a, b, c;
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	a = saddr->s6_addr32[0];
1301da177e4SLinus Torvalds 	b = saddr->s6_addr32[1];
1311da177e4SLinus Torvalds 	c = saddr->s6_addr32[2];
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	a += JHASH_GOLDEN_RATIO;
1341da177e4SLinus Torvalds 	b += JHASH_GOLDEN_RATIO;
1351da177e4SLinus Torvalds 	c += ip6_frag_hash_rnd;
1361da177e4SLinus Torvalds 	__jhash_mix(a, b, c);
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	a += saddr->s6_addr32[3];
1391da177e4SLinus Torvalds 	b += daddr->s6_addr32[0];
1401da177e4SLinus Torvalds 	c += daddr->s6_addr32[1];
1411da177e4SLinus Torvalds 	__jhash_mix(a, b, c);
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	a += daddr->s6_addr32[2];
1441da177e4SLinus Torvalds 	b += daddr->s6_addr32[3];
1451da177e4SLinus Torvalds 	c += id;
1461da177e4SLinus Torvalds 	__jhash_mix(a, b, c);
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 	return c & (IP6Q_HASHSZ - 1);
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds static struct timer_list ip6_frag_secret_timer;
1521da177e4SLinus Torvalds int sysctl_ip6frag_secret_interval = 10 * 60 * HZ;
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds static void ip6_frag_secret_rebuild(unsigned long dummy)
1551da177e4SLinus Torvalds {
1561da177e4SLinus Torvalds 	unsigned long now = jiffies;
1571da177e4SLinus Torvalds 	int i;
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	write_lock(&ip6_frag_lock);
1601da177e4SLinus Torvalds 	get_random_bytes(&ip6_frag_hash_rnd, sizeof(u32));
1611da177e4SLinus Torvalds 	for (i = 0; i < IP6Q_HASHSZ; i++) {
1621da177e4SLinus Torvalds 		struct frag_queue *q;
163*e7c8a41eSYasuyuki Kozakai 		struct hlist_node *p, *n;
1641da177e4SLinus Torvalds 
165*e7c8a41eSYasuyuki Kozakai 		hlist_for_each_entry_safe(q, p, n, &ip6_frag_hash[i], list) {
1661da177e4SLinus Torvalds 			unsigned int hval = ip6qhashfn(q->id,
1671da177e4SLinus Torvalds 						       &q->saddr,
1681da177e4SLinus Torvalds 						       &q->daddr);
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 			if (hval != i) {
171*e7c8a41eSYasuyuki Kozakai 				hlist_del(&q->list);
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 				/* Relink to new hash chain. */
174*e7c8a41eSYasuyuki Kozakai 				hlist_add_head(&q->list,
175*e7c8a41eSYasuyuki Kozakai 					       &ip6_frag_hash[hval]);
1761da177e4SLinus Torvalds 
177*e7c8a41eSYasuyuki Kozakai 			}
1781da177e4SLinus Torvalds 		}
1791da177e4SLinus Torvalds 	}
1801da177e4SLinus Torvalds 	write_unlock(&ip6_frag_lock);
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	mod_timer(&ip6_frag_secret_timer, now + sysctl_ip6frag_secret_interval);
1831da177e4SLinus Torvalds }
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds atomic_t ip6_frag_mem = ATOMIC_INIT(0);
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds /* Memory Tracking Functions. */
1881da177e4SLinus Torvalds static inline void frag_kfree_skb(struct sk_buff *skb, int *work)
1891da177e4SLinus Torvalds {
1901da177e4SLinus Torvalds 	if (work)
1911da177e4SLinus Torvalds 		*work -= skb->truesize;
1921da177e4SLinus Torvalds 	atomic_sub(skb->truesize, &ip6_frag_mem);
1931da177e4SLinus Torvalds 	kfree_skb(skb);
1941da177e4SLinus Torvalds }
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds static inline void frag_free_queue(struct frag_queue *fq, int *work)
1971da177e4SLinus Torvalds {
1981da177e4SLinus Torvalds 	if (work)
1991da177e4SLinus Torvalds 		*work -= sizeof(struct frag_queue);
2001da177e4SLinus Torvalds 	atomic_sub(sizeof(struct frag_queue), &ip6_frag_mem);
2011da177e4SLinus Torvalds 	kfree(fq);
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds static inline struct frag_queue *frag_alloc_queue(void)
2051da177e4SLinus Torvalds {
2061da177e4SLinus Torvalds 	struct frag_queue *fq = kmalloc(sizeof(struct frag_queue), GFP_ATOMIC);
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	if(!fq)
2091da177e4SLinus Torvalds 		return NULL;
2101da177e4SLinus Torvalds 	atomic_add(sizeof(struct frag_queue), &ip6_frag_mem);
2111da177e4SLinus Torvalds 	return fq;
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds /* Destruction primitives. */
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds /* Complete destruction of fq. */
2171da177e4SLinus Torvalds static void ip6_frag_destroy(struct frag_queue *fq, int *work)
2181da177e4SLinus Torvalds {
2191da177e4SLinus Torvalds 	struct sk_buff *fp;
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	BUG_TRAP(fq->last_in&COMPLETE);
2221da177e4SLinus Torvalds 	BUG_TRAP(del_timer(&fq->timer) == 0);
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	/* Release all fragment data. */
2251da177e4SLinus Torvalds 	fp = fq->fragments;
2261da177e4SLinus Torvalds 	while (fp) {
2271da177e4SLinus Torvalds 		struct sk_buff *xp = fp->next;
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 		frag_kfree_skb(fp, work);
2301da177e4SLinus Torvalds 		fp = xp;
2311da177e4SLinus Torvalds 	}
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	frag_free_queue(fq, work);
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds static __inline__ void fq_put(struct frag_queue *fq, int *work)
2371da177e4SLinus Torvalds {
2381da177e4SLinus Torvalds 	if (atomic_dec_and_test(&fq->refcnt))
2391da177e4SLinus Torvalds 		ip6_frag_destroy(fq, work);
2401da177e4SLinus Torvalds }
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds /* Kill fq entry. It is not destroyed immediately,
2431da177e4SLinus Torvalds  * because caller (and someone more) holds reference count.
2441da177e4SLinus Torvalds  */
2451da177e4SLinus Torvalds static __inline__ void fq_kill(struct frag_queue *fq)
2461da177e4SLinus Torvalds {
2471da177e4SLinus Torvalds 	if (del_timer(&fq->timer))
2481da177e4SLinus Torvalds 		atomic_dec(&fq->refcnt);
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	if (!(fq->last_in & COMPLETE)) {
2511da177e4SLinus Torvalds 		fq_unlink(fq);
2521da177e4SLinus Torvalds 		atomic_dec(&fq->refcnt);
2531da177e4SLinus Torvalds 		fq->last_in |= COMPLETE;
2541da177e4SLinus Torvalds 	}
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds static void ip6_evictor(void)
2581da177e4SLinus Torvalds {
2591da177e4SLinus Torvalds 	struct frag_queue *fq;
2601da177e4SLinus Torvalds 	struct list_head *tmp;
2611da177e4SLinus Torvalds 	int work;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	work = atomic_read(&ip6_frag_mem) - sysctl_ip6frag_low_thresh;
2641da177e4SLinus Torvalds 	if (work <= 0)
2651da177e4SLinus Torvalds 		return;
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 	while(work > 0) {
2681da177e4SLinus Torvalds 		read_lock(&ip6_frag_lock);
2691da177e4SLinus Torvalds 		if (list_empty(&ip6_frag_lru_list)) {
2701da177e4SLinus Torvalds 			read_unlock(&ip6_frag_lock);
2711da177e4SLinus Torvalds 			return;
2721da177e4SLinus Torvalds 		}
2731da177e4SLinus Torvalds 		tmp = ip6_frag_lru_list.next;
2741da177e4SLinus Torvalds 		fq = list_entry(tmp, struct frag_queue, lru_list);
2751da177e4SLinus Torvalds 		atomic_inc(&fq->refcnt);
2761da177e4SLinus Torvalds 		read_unlock(&ip6_frag_lock);
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 		spin_lock(&fq->lock);
2791da177e4SLinus Torvalds 		if (!(fq->last_in&COMPLETE))
2801da177e4SLinus Torvalds 			fq_kill(fq);
2811da177e4SLinus Torvalds 		spin_unlock(&fq->lock);
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 		fq_put(fq, &work);
2841da177e4SLinus Torvalds 		IP6_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
2851da177e4SLinus Torvalds 	}
2861da177e4SLinus Torvalds }
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds static void ip6_frag_expire(unsigned long data)
2891da177e4SLinus Torvalds {
2901da177e4SLinus Torvalds 	struct frag_queue *fq = (struct frag_queue *) data;
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	spin_lock(&fq->lock);
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	if (fq->last_in & COMPLETE)
2951da177e4SLinus Torvalds 		goto out;
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	fq_kill(fq);
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	IP6_INC_STATS_BH(IPSTATS_MIB_REASMTIMEOUT);
3001da177e4SLinus Torvalds 	IP6_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	/* Send error only if the first segment arrived. */
3031da177e4SLinus Torvalds 	if (fq->last_in&FIRST_IN && fq->fragments) {
3041da177e4SLinus Torvalds 		struct net_device *dev = dev_get_by_index(fq->iif);
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 		/*
3071da177e4SLinus Torvalds 		   But use as source device on which LAST ARRIVED
3081da177e4SLinus Torvalds 		   segment was received. And do not use fq->dev
3091da177e4SLinus Torvalds 		   pointer directly, device might already disappeared.
3101da177e4SLinus Torvalds 		 */
3111da177e4SLinus Torvalds 		if (dev) {
3121da177e4SLinus Torvalds 			fq->fragments->dev = dev;
3131da177e4SLinus Torvalds 			icmpv6_send(fq->fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0,
3141da177e4SLinus Torvalds 				    dev);
3151da177e4SLinus Torvalds 			dev_put(dev);
3161da177e4SLinus Torvalds 		}
3171da177e4SLinus Torvalds 	}
3181da177e4SLinus Torvalds out:
3191da177e4SLinus Torvalds 	spin_unlock(&fq->lock);
3201da177e4SLinus Torvalds 	fq_put(fq, NULL);
3211da177e4SLinus Torvalds }
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds /* Creation primitives. */
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds static struct frag_queue *ip6_frag_intern(unsigned int hash,
3271da177e4SLinus Torvalds 					  struct frag_queue *fq_in)
3281da177e4SLinus Torvalds {
3291da177e4SLinus Torvalds 	struct frag_queue *fq;
330*e7c8a41eSYasuyuki Kozakai #ifdef CONFIG_SMP
331*e7c8a41eSYasuyuki Kozakai 	struct hlist_node *n;
332*e7c8a41eSYasuyuki Kozakai #endif
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	write_lock(&ip6_frag_lock);
3351da177e4SLinus Torvalds #ifdef CONFIG_SMP
336*e7c8a41eSYasuyuki Kozakai 	hlist_for_each_entry(fq, n, &ip6_frag_hash[hash], list) {
3371da177e4SLinus Torvalds 		if (fq->id == fq_in->id &&
3381da177e4SLinus Torvalds 		    ipv6_addr_equal(&fq_in->saddr, &fq->saddr) &&
3391da177e4SLinus Torvalds 		    ipv6_addr_equal(&fq_in->daddr, &fq->daddr)) {
3401da177e4SLinus Torvalds 			atomic_inc(&fq->refcnt);
3411da177e4SLinus Torvalds 			write_unlock(&ip6_frag_lock);
3421da177e4SLinus Torvalds 			fq_in->last_in |= COMPLETE;
3431da177e4SLinus Torvalds 			fq_put(fq_in, NULL);
3441da177e4SLinus Torvalds 			return fq;
3451da177e4SLinus Torvalds 		}
3461da177e4SLinus Torvalds 	}
3471da177e4SLinus Torvalds #endif
3481da177e4SLinus Torvalds 	fq = fq_in;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	if (!mod_timer(&fq->timer, jiffies + sysctl_ip6frag_time))
3511da177e4SLinus Torvalds 		atomic_inc(&fq->refcnt);
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	atomic_inc(&fq->refcnt);
354*e7c8a41eSYasuyuki Kozakai 	hlist_add_head(&fq->list, &ip6_frag_hash[hash]);
3551da177e4SLinus Torvalds 	INIT_LIST_HEAD(&fq->lru_list);
3561da177e4SLinus Torvalds 	list_add_tail(&fq->lru_list, &ip6_frag_lru_list);
3571da177e4SLinus Torvalds 	ip6_frag_nqueues++;
3581da177e4SLinus Torvalds 	write_unlock(&ip6_frag_lock);
3591da177e4SLinus Torvalds 	return fq;
3601da177e4SLinus Torvalds }
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds static struct frag_queue *
3641da177e4SLinus Torvalds ip6_frag_create(unsigned int hash, u32 id, struct in6_addr *src, struct in6_addr *dst)
3651da177e4SLinus Torvalds {
3661da177e4SLinus Torvalds 	struct frag_queue *fq;
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	if ((fq = frag_alloc_queue()) == NULL)
3691da177e4SLinus Torvalds 		goto oom;
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 	memset(fq, 0, sizeof(struct frag_queue));
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	fq->id = id;
3741da177e4SLinus Torvalds 	ipv6_addr_copy(&fq->saddr, src);
3751da177e4SLinus Torvalds 	ipv6_addr_copy(&fq->daddr, dst);
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	init_timer(&fq->timer);
3781da177e4SLinus Torvalds 	fq->timer.function = ip6_frag_expire;
3791da177e4SLinus Torvalds 	fq->timer.data = (long) fq;
3801da177e4SLinus Torvalds 	spin_lock_init(&fq->lock);
3811da177e4SLinus Torvalds 	atomic_set(&fq->refcnt, 1);
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	return ip6_frag_intern(hash, fq);
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds oom:
3861da177e4SLinus Torvalds 	IP6_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
3871da177e4SLinus Torvalds 	return NULL;
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds static __inline__ struct frag_queue *
3911da177e4SLinus Torvalds fq_find(u32 id, struct in6_addr *src, struct in6_addr *dst)
3921da177e4SLinus Torvalds {
3931da177e4SLinus Torvalds 	struct frag_queue *fq;
394*e7c8a41eSYasuyuki Kozakai 	struct hlist_node *n;
3951da177e4SLinus Torvalds 	unsigned int hash = ip6qhashfn(id, src, dst);
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	read_lock(&ip6_frag_lock);
398*e7c8a41eSYasuyuki Kozakai 	hlist_for_each_entry(fq, n, &ip6_frag_hash[hash], list) {
3991da177e4SLinus Torvalds 		if (fq->id == id &&
4001da177e4SLinus Torvalds 		    ipv6_addr_equal(src, &fq->saddr) &&
4011da177e4SLinus Torvalds 		    ipv6_addr_equal(dst, &fq->daddr)) {
4021da177e4SLinus Torvalds 			atomic_inc(&fq->refcnt);
4031da177e4SLinus Torvalds 			read_unlock(&ip6_frag_lock);
4041da177e4SLinus Torvalds 			return fq;
4051da177e4SLinus Torvalds 		}
4061da177e4SLinus Torvalds 	}
4071da177e4SLinus Torvalds 	read_unlock(&ip6_frag_lock);
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 	return ip6_frag_create(hash, id, src, dst);
4101da177e4SLinus Torvalds }
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds static void ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
4141da177e4SLinus Torvalds 			   struct frag_hdr *fhdr, int nhoff)
4151da177e4SLinus Torvalds {
4161da177e4SLinus Torvalds 	struct sk_buff *prev, *next;
4171da177e4SLinus Torvalds 	int offset, end;
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	if (fq->last_in & COMPLETE)
4201da177e4SLinus Torvalds 		goto err;
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 	offset = ntohs(fhdr->frag_off) & ~0x7;
4231da177e4SLinus Torvalds 	end = offset + (ntohs(skb->nh.ipv6h->payload_len) -
4241da177e4SLinus Torvalds 			((u8 *) (fhdr + 1) - (u8 *) (skb->nh.ipv6h + 1)));
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	if ((unsigned int)end > IPV6_MAXPLEN) {
4271da177e4SLinus Torvalds 		IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
4281da177e4SLinus Torvalds  		icmpv6_param_prob(skb,ICMPV6_HDR_FIELD, (u8*)&fhdr->frag_off - skb->nh.raw);
4291da177e4SLinus Torvalds  		return;
4301da177e4SLinus Torvalds 	}
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds  	if (skb->ip_summed == CHECKSUM_HW)
4331da177e4SLinus Torvalds  		skb->csum = csum_sub(skb->csum,
4341da177e4SLinus Torvalds  				     csum_partial(skb->nh.raw, (u8*)(fhdr+1)-skb->nh.raw, 0));
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	/* Is this the final fragment? */
4371da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(IP6_MF))) {
4381da177e4SLinus Torvalds 		/* If we already have some bits beyond end
4391da177e4SLinus Torvalds 		 * or have different end, the segment is corrupted.
4401da177e4SLinus Torvalds 		 */
4411da177e4SLinus Torvalds 		if (end < fq->len ||
4421da177e4SLinus Torvalds 		    ((fq->last_in & LAST_IN) && end != fq->len))
4431da177e4SLinus Torvalds 			goto err;
4441da177e4SLinus Torvalds 		fq->last_in |= LAST_IN;
4451da177e4SLinus Torvalds 		fq->len = end;
4461da177e4SLinus Torvalds 	} else {
4471da177e4SLinus Torvalds 		/* Check if the fragment is rounded to 8 bytes.
4481da177e4SLinus Torvalds 		 * Required by the RFC.
4491da177e4SLinus Torvalds 		 */
4501da177e4SLinus Torvalds 		if (end & 0x7) {
4511da177e4SLinus Torvalds 			/* RFC2460 says always send parameter problem in
4521da177e4SLinus Torvalds 			 * this case. -DaveM
4531da177e4SLinus Torvalds 			 */
4541da177e4SLinus Torvalds 			IP6_INC_STATS_BH(IPSTATS_MIB_INHDRERRORS);
4551da177e4SLinus Torvalds 			icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
4561da177e4SLinus Torvalds 					  offsetof(struct ipv6hdr, payload_len));
4571da177e4SLinus Torvalds 			return;
4581da177e4SLinus Torvalds 		}
4591da177e4SLinus Torvalds 		if (end > fq->len) {
4601da177e4SLinus Torvalds 			/* Some bits beyond end -> corruption. */
4611da177e4SLinus Torvalds 			if (fq->last_in & LAST_IN)
4621da177e4SLinus Torvalds 				goto err;
4631da177e4SLinus Torvalds 			fq->len = end;
4641da177e4SLinus Torvalds 		}
4651da177e4SLinus Torvalds 	}
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 	if (end == offset)
4681da177e4SLinus Torvalds 		goto err;
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	/* Point into the IP datagram 'data' part. */
4711da177e4SLinus Torvalds 	if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
4721da177e4SLinus Torvalds 		goto err;
47342ca89c1SStephen Hemminger 
47442ca89c1SStephen Hemminger 	if (pskb_trim_rcsum(skb, end - offset))
4751da177e4SLinus Torvalds 		goto err;
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	/* Find out which fragments are in front and at the back of us
4781da177e4SLinus Torvalds 	 * in the chain of fragments so far.  We must know where to put
4791da177e4SLinus Torvalds 	 * this fragment, right?
4801da177e4SLinus Torvalds 	 */
4811da177e4SLinus Torvalds 	prev = NULL;
4821da177e4SLinus Torvalds 	for(next = fq->fragments; next != NULL; next = next->next) {
4831da177e4SLinus Torvalds 		if (FRAG6_CB(next)->offset >= offset)
4841da177e4SLinus Torvalds 			break;	/* bingo! */
4851da177e4SLinus Torvalds 		prev = next;
4861da177e4SLinus Torvalds 	}
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	/* We found where to put this one.  Check for overlap with
4891da177e4SLinus Torvalds 	 * preceding fragment, and, if needed, align things so that
4901da177e4SLinus Torvalds 	 * any overlaps are eliminated.
4911da177e4SLinus Torvalds 	 */
4921da177e4SLinus Torvalds 	if (prev) {
4931da177e4SLinus Torvalds 		int i = (FRAG6_CB(prev)->offset + prev->len) - offset;
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 		if (i > 0) {
4961da177e4SLinus Torvalds 			offset += i;
4971da177e4SLinus Torvalds 			if (end <= offset)
4981da177e4SLinus Torvalds 				goto err;
4991da177e4SLinus Torvalds 			if (!pskb_pull(skb, i))
5001da177e4SLinus Torvalds 				goto err;
5011da177e4SLinus Torvalds 			if (skb->ip_summed != CHECKSUM_UNNECESSARY)
5021da177e4SLinus Torvalds 				skb->ip_summed = CHECKSUM_NONE;
5031da177e4SLinus Torvalds 		}
5041da177e4SLinus Torvalds 	}
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 	/* Look for overlap with succeeding segments.
5071da177e4SLinus Torvalds 	 * If we can merge fragments, do it.
5081da177e4SLinus Torvalds 	 */
5091da177e4SLinus Torvalds 	while (next && FRAG6_CB(next)->offset < end) {
5101da177e4SLinus Torvalds 		int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 		if (i < next->len) {
5131da177e4SLinus Torvalds 			/* Eat head of the next overlapped fragment
5141da177e4SLinus Torvalds 			 * and leave the loop. The next ones cannot overlap.
5151da177e4SLinus Torvalds 			 */
5161da177e4SLinus Torvalds 			if (!pskb_pull(next, i))
5171da177e4SLinus Torvalds 				goto err;
5181da177e4SLinus Torvalds 			FRAG6_CB(next)->offset += i;	/* next fragment */
5191da177e4SLinus Torvalds 			fq->meat -= i;
5201da177e4SLinus Torvalds 			if (next->ip_summed != CHECKSUM_UNNECESSARY)
5211da177e4SLinus Torvalds 				next->ip_summed = CHECKSUM_NONE;
5221da177e4SLinus Torvalds 			break;
5231da177e4SLinus Torvalds 		} else {
5241da177e4SLinus Torvalds 			struct sk_buff *free_it = next;
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 			/* Old fragment is completely overridden with
5271da177e4SLinus Torvalds 			 * new one drop it.
5281da177e4SLinus Torvalds 			 */
5291da177e4SLinus Torvalds 			next = next->next;
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 			if (prev)
5321da177e4SLinus Torvalds 				prev->next = next;
5331da177e4SLinus Torvalds 			else
5341da177e4SLinus Torvalds 				fq->fragments = next;
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 			fq->meat -= free_it->len;
5371da177e4SLinus Torvalds 			frag_kfree_skb(free_it, NULL);
5381da177e4SLinus Torvalds 		}
5391da177e4SLinus Torvalds 	}
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 	FRAG6_CB(skb)->offset = offset;
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	/* Insert this fragment in the chain of fragments. */
5441da177e4SLinus Torvalds 	skb->next = next;
5451da177e4SLinus Torvalds 	if (prev)
5461da177e4SLinus Torvalds 		prev->next = skb;
5471da177e4SLinus Torvalds 	else
5481da177e4SLinus Torvalds 		fq->fragments = skb;
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 	if (skb->dev)
5511da177e4SLinus Torvalds 		fq->iif = skb->dev->ifindex;
5521da177e4SLinus Torvalds 	skb->dev = NULL;
553a61bbcf2SPatrick McHardy 	skb_get_timestamp(skb, &fq->stamp);
5541da177e4SLinus Torvalds 	fq->meat += skb->len;
5551da177e4SLinus Torvalds 	atomic_add(skb->truesize, &ip6_frag_mem);
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	/* The first fragment.
5581da177e4SLinus Torvalds 	 * nhoffset is obtained from the first fragment, of course.
5591da177e4SLinus Torvalds 	 */
5601da177e4SLinus Torvalds 	if (offset == 0) {
5611da177e4SLinus Torvalds 		fq->nhoffset = nhoff;
5621da177e4SLinus Torvalds 		fq->last_in |= FIRST_IN;
5631da177e4SLinus Torvalds 	}
5641da177e4SLinus Torvalds 	write_lock(&ip6_frag_lock);
5651da177e4SLinus Torvalds 	list_move_tail(&fq->lru_list, &ip6_frag_lru_list);
5661da177e4SLinus Torvalds 	write_unlock(&ip6_frag_lock);
5671da177e4SLinus Torvalds 	return;
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds err:
5701da177e4SLinus Torvalds 	IP6_INC_STATS(IPSTATS_MIB_REASMFAILS);
5711da177e4SLinus Torvalds 	kfree_skb(skb);
5721da177e4SLinus Torvalds }
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds /*
5751da177e4SLinus Torvalds  *	Check if this packet is complete.
5761da177e4SLinus Torvalds  *	Returns NULL on failure by any reason, and pointer
5771da177e4SLinus Torvalds  *	to current nexthdr field in reassembled frame.
5781da177e4SLinus Torvalds  *
5791da177e4SLinus Torvalds  *	It is called with locked fq, and caller must check that
5801da177e4SLinus Torvalds  *	queue is eligible for reassembly i.e. it is not COMPLETE,
5811da177e4SLinus Torvalds  *	the last and the first frames arrived and all the bits are here.
5821da177e4SLinus Torvalds  */
5831da177e4SLinus Torvalds static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff **skb_in,
5841da177e4SLinus Torvalds 			  unsigned int *nhoffp,
5851da177e4SLinus Torvalds 			  struct net_device *dev)
5861da177e4SLinus Torvalds {
5871da177e4SLinus Torvalds 	struct sk_buff *fp, *head = fq->fragments;
5881da177e4SLinus Torvalds 	int    payload_len;
5891da177e4SLinus Torvalds 	unsigned int nhoff;
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	fq_kill(fq);
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds 	BUG_TRAP(head != NULL);
5941da177e4SLinus Torvalds 	BUG_TRAP(FRAG6_CB(head)->offset == 0);
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	/* Unfragmented part is taken from the first segment. */
5971da177e4SLinus Torvalds 	payload_len = (head->data - head->nh.raw) - sizeof(struct ipv6hdr) + fq->len - sizeof(struct frag_hdr);
5981da177e4SLinus Torvalds 	if (payload_len > IPV6_MAXPLEN)
5991da177e4SLinus Torvalds 		goto out_oversize;
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 	/* Head of list must not be cloned. */
6021da177e4SLinus Torvalds 	if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
6031da177e4SLinus Torvalds 		goto out_oom;
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	/* If the first fragment is fragmented itself, we split
6061da177e4SLinus Torvalds 	 * it to two chunks: the first with data and paged part
6071da177e4SLinus Torvalds 	 * and the second, holding only fragments. */
6081da177e4SLinus Torvalds 	if (skb_shinfo(head)->frag_list) {
6091da177e4SLinus Torvalds 		struct sk_buff *clone;
6101da177e4SLinus Torvalds 		int i, plen = 0;
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 		if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
6131da177e4SLinus Torvalds 			goto out_oom;
6141da177e4SLinus Torvalds 		clone->next = head->next;
6151da177e4SLinus Torvalds 		head->next = clone;
6161da177e4SLinus Torvalds 		skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
6171da177e4SLinus Torvalds 		skb_shinfo(head)->frag_list = NULL;
6181da177e4SLinus Torvalds 		for (i=0; i<skb_shinfo(head)->nr_frags; i++)
6191da177e4SLinus Torvalds 			plen += skb_shinfo(head)->frags[i].size;
6201da177e4SLinus Torvalds 		clone->len = clone->data_len = head->data_len - plen;
6211da177e4SLinus Torvalds 		head->data_len -= clone->len;
6221da177e4SLinus Torvalds 		head->len -= clone->len;
6231da177e4SLinus Torvalds 		clone->csum = 0;
6241da177e4SLinus Torvalds 		clone->ip_summed = head->ip_summed;
6251da177e4SLinus Torvalds 		atomic_add(clone->truesize, &ip6_frag_mem);
6261da177e4SLinus Torvalds 	}
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	/* We have to remove fragment header from datagram and to relocate
6291da177e4SLinus Torvalds 	 * header in order to calculate ICV correctly. */
6301da177e4SLinus Torvalds 	nhoff = fq->nhoffset;
6311da177e4SLinus Torvalds 	head->nh.raw[nhoff] = head->h.raw[0];
6321da177e4SLinus Torvalds 	memmove(head->head + sizeof(struct frag_hdr), head->head,
6331da177e4SLinus Torvalds 		(head->data - head->head) - sizeof(struct frag_hdr));
6341da177e4SLinus Torvalds 	head->mac.raw += sizeof(struct frag_hdr);
6351da177e4SLinus Torvalds 	head->nh.raw += sizeof(struct frag_hdr);
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	skb_shinfo(head)->frag_list = head->next;
6381da177e4SLinus Torvalds 	head->h.raw = head->data;
6391da177e4SLinus Torvalds 	skb_push(head, head->data - head->nh.raw);
6401da177e4SLinus Torvalds 	atomic_sub(head->truesize, &ip6_frag_mem);
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	for (fp=head->next; fp; fp = fp->next) {
6431da177e4SLinus Torvalds 		head->data_len += fp->len;
6441da177e4SLinus Torvalds 		head->len += fp->len;
6451da177e4SLinus Torvalds 		if (head->ip_summed != fp->ip_summed)
6461da177e4SLinus Torvalds 			head->ip_summed = CHECKSUM_NONE;
6471da177e4SLinus Torvalds 		else if (head->ip_summed == CHECKSUM_HW)
6481da177e4SLinus Torvalds 			head->csum = csum_add(head->csum, fp->csum);
6491da177e4SLinus Torvalds 		head->truesize += fp->truesize;
6501da177e4SLinus Torvalds 		atomic_sub(fp->truesize, &ip6_frag_mem);
6511da177e4SLinus Torvalds 	}
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 	head->next = NULL;
6541da177e4SLinus Torvalds 	head->dev = dev;
655a61bbcf2SPatrick McHardy 	skb_set_timestamp(head, &fq->stamp);
6561da177e4SLinus Torvalds 	head->nh.ipv6h->payload_len = htons(payload_len);
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 	*skb_in = head;
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 	/* Yes, and fold redundant checksum back. 8) */
6611da177e4SLinus Torvalds 	if (head->ip_summed == CHECKSUM_HW)
6621da177e4SLinus Torvalds 		head->csum = csum_partial(head->nh.raw, head->h.raw-head->nh.raw, head->csum);
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	IP6_INC_STATS_BH(IPSTATS_MIB_REASMOKS);
6651da177e4SLinus Torvalds 	fq->fragments = NULL;
6661da177e4SLinus Torvalds 	*nhoffp = nhoff;
6671da177e4SLinus Torvalds 	return 1;
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds out_oversize:
6701da177e4SLinus Torvalds 	if (net_ratelimit())
6711da177e4SLinus Torvalds 		printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len);
6721da177e4SLinus Torvalds 	goto out_fail;
6731da177e4SLinus Torvalds out_oom:
6741da177e4SLinus Torvalds 	if (net_ratelimit())
6751da177e4SLinus Torvalds 		printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n");
6761da177e4SLinus Torvalds out_fail:
6771da177e4SLinus Torvalds 	IP6_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
6781da177e4SLinus Torvalds 	return -1;
6791da177e4SLinus Torvalds }
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds static int ipv6_frag_rcv(struct sk_buff **skbp, unsigned int *nhoffp)
6821da177e4SLinus Torvalds {
6831da177e4SLinus Torvalds 	struct sk_buff *skb = *skbp;
6841da177e4SLinus Torvalds 	struct net_device *dev = skb->dev;
6851da177e4SLinus Torvalds 	struct frag_hdr *fhdr;
6861da177e4SLinus Torvalds 	struct frag_queue *fq;
6871da177e4SLinus Torvalds 	struct ipv6hdr *hdr;
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 	hdr = skb->nh.ipv6h;
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 	IP6_INC_STATS_BH(IPSTATS_MIB_REASMREQDS);
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 	/* Jumbo payload inhibits frag. header */
6941da177e4SLinus Torvalds 	if (hdr->payload_len==0) {
6951da177e4SLinus Torvalds 		IP6_INC_STATS(IPSTATS_MIB_INHDRERRORS);
6961da177e4SLinus Torvalds 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb->h.raw-skb->nh.raw);
6971da177e4SLinus Torvalds 		return -1;
6981da177e4SLinus Torvalds 	}
6991da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, (skb->h.raw-skb->data)+sizeof(struct frag_hdr))) {
7001da177e4SLinus Torvalds 		IP6_INC_STATS(IPSTATS_MIB_INHDRERRORS);
7011da177e4SLinus Torvalds 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb->h.raw-skb->nh.raw);
7021da177e4SLinus Torvalds 		return -1;
7031da177e4SLinus Torvalds 	}
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 	hdr = skb->nh.ipv6h;
7061da177e4SLinus Torvalds 	fhdr = (struct frag_hdr *)skb->h.raw;
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(0xFFF9))) {
7091da177e4SLinus Torvalds 		/* It is not a fragmented frame */
7101da177e4SLinus Torvalds 		skb->h.raw += sizeof(struct frag_hdr);
7111da177e4SLinus Torvalds 		IP6_INC_STATS_BH(IPSTATS_MIB_REASMOKS);
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 		*nhoffp = (u8*)fhdr - skb->nh.raw;
7141da177e4SLinus Torvalds 		return 1;
7151da177e4SLinus Torvalds 	}
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	if (atomic_read(&ip6_frag_mem) > sysctl_ip6frag_high_thresh)
7181da177e4SLinus Torvalds 		ip6_evictor();
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr)) != NULL) {
7211da177e4SLinus Torvalds 		int ret = -1;
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 		spin_lock(&fq->lock);
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 		ip6_frag_queue(fq, skb, fhdr, *nhoffp);
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 		if (fq->last_in == (FIRST_IN|LAST_IN) &&
7281da177e4SLinus Torvalds 		    fq->meat == fq->len)
7291da177e4SLinus Torvalds 			ret = ip6_frag_reasm(fq, skbp, nhoffp, dev);
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds 		spin_unlock(&fq->lock);
7321da177e4SLinus Torvalds 		fq_put(fq, NULL);
7331da177e4SLinus Torvalds 		return ret;
7341da177e4SLinus Torvalds 	}
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds 	IP6_INC_STATS_BH(IPSTATS_MIB_REASMFAILS);
7371da177e4SLinus Torvalds 	kfree_skb(skb);
7381da177e4SLinus Torvalds 	return -1;
7391da177e4SLinus Torvalds }
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds static struct inet6_protocol frag_protocol =
7421da177e4SLinus Torvalds {
7431da177e4SLinus Torvalds 	.handler	=	ipv6_frag_rcv,
7441da177e4SLinus Torvalds 	.flags		=	INET6_PROTO_NOPOLICY,
7451da177e4SLinus Torvalds };
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds void __init ipv6_frag_init(void)
7481da177e4SLinus Torvalds {
7491da177e4SLinus Torvalds 	if (inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT) < 0)
7501da177e4SLinus Torvalds 		printk(KERN_ERR "ipv6_frag_init: Could not register protocol\n");
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 	ip6_frag_hash_rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
7531da177e4SLinus Torvalds 				   (jiffies ^ (jiffies >> 6)));
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	init_timer(&ip6_frag_secret_timer);
7561da177e4SLinus Torvalds 	ip6_frag_secret_timer.function = ip6_frag_secret_rebuild;
7571da177e4SLinus Torvalds 	ip6_frag_secret_timer.expires = jiffies + sysctl_ip6frag_secret_interval;
7581da177e4SLinus Torvalds 	add_timer(&ip6_frag_secret_timer);
7591da177e4SLinus Torvalds }
760