xref: /openbmc/linux/net/ipv6/reassembly.c (revision a11d206d)
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/errno.h>
321da177e4SLinus Torvalds #include <linux/types.h>
331da177e4SLinus Torvalds #include <linux/string.h>
341da177e4SLinus Torvalds #include <linux/socket.h>
351da177e4SLinus Torvalds #include <linux/sockios.h>
361da177e4SLinus Torvalds #include <linux/jiffies.h>
371da177e4SLinus Torvalds #include <linux/net.h>
381da177e4SLinus Torvalds #include <linux/list.h>
391da177e4SLinus Torvalds #include <linux/netdevice.h>
401da177e4SLinus Torvalds #include <linux/in6.h>
411da177e4SLinus Torvalds #include <linux/ipv6.h>
421da177e4SLinus Torvalds #include <linux/icmpv6.h>
431da177e4SLinus Torvalds #include <linux/random.h>
441da177e4SLinus Torvalds #include <linux/jhash.h>
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #include <net/sock.h>
471da177e4SLinus Torvalds #include <net/snmp.h>
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds #include <net/ipv6.h>
50a11d206dSYOSHIFUJI Hideaki #include <net/ip6_route.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 
57ab32ea5dSBrian Haley int sysctl_ip6frag_high_thresh __read_mostly = 256*1024;
58ab32ea5dSBrian Haley int sysctl_ip6frag_low_thresh __read_mostly = 192*1024;
591da177e4SLinus Torvalds 
60ab32ea5dSBrian Haley int sysctl_ip6frag_time __read_mostly = 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 {
77e7c8a41eSYasuyuki 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 
104e7c8a41eSYasuyuki 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 {
112e7c8a41eSYasuyuki 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 
124f6596f9dSZach Brown /*
125f6596f9dSZach Brown  * callers should be careful not to use the hash value outside the ipfrag_lock
126f6596f9dSZach Brown  * as doing so could race with ipfrag_hash_rnd being recalculated.
127f6596f9dSZach Brown  */
1281da177e4SLinus Torvalds static unsigned int ip6qhashfn(u32 id, struct in6_addr *saddr,
1291da177e4SLinus Torvalds 			       struct in6_addr *daddr)
1301da177e4SLinus Torvalds {
1311da177e4SLinus Torvalds 	u32 a, b, c;
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	a = saddr->s6_addr32[0];
1341da177e4SLinus Torvalds 	b = saddr->s6_addr32[1];
1351da177e4SLinus Torvalds 	c = saddr->s6_addr32[2];
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	a += JHASH_GOLDEN_RATIO;
1381da177e4SLinus Torvalds 	b += JHASH_GOLDEN_RATIO;
1391da177e4SLinus Torvalds 	c += ip6_frag_hash_rnd;
1401da177e4SLinus Torvalds 	__jhash_mix(a, b, c);
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	a += saddr->s6_addr32[3];
1431da177e4SLinus Torvalds 	b += daddr->s6_addr32[0];
1441da177e4SLinus Torvalds 	c += daddr->s6_addr32[1];
1451da177e4SLinus Torvalds 	__jhash_mix(a, b, c);
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 	a += daddr->s6_addr32[2];
1481da177e4SLinus Torvalds 	b += daddr->s6_addr32[3];
1491da177e4SLinus Torvalds 	c += id;
1501da177e4SLinus Torvalds 	__jhash_mix(a, b, c);
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	return c & (IP6Q_HASHSZ - 1);
1531da177e4SLinus Torvalds }
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds static struct timer_list ip6_frag_secret_timer;
156ab32ea5dSBrian Haley int sysctl_ip6frag_secret_interval __read_mostly = 10 * 60 * HZ;
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds static void ip6_frag_secret_rebuild(unsigned long dummy)
1591da177e4SLinus Torvalds {
1601da177e4SLinus Torvalds 	unsigned long now = jiffies;
1611da177e4SLinus Torvalds 	int i;
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 	write_lock(&ip6_frag_lock);
1641da177e4SLinus Torvalds 	get_random_bytes(&ip6_frag_hash_rnd, sizeof(u32));
1651da177e4SLinus Torvalds 	for (i = 0; i < IP6Q_HASHSZ; i++) {
1661da177e4SLinus Torvalds 		struct frag_queue *q;
167e7c8a41eSYasuyuki Kozakai 		struct hlist_node *p, *n;
1681da177e4SLinus Torvalds 
169e7c8a41eSYasuyuki Kozakai 		hlist_for_each_entry_safe(q, p, n, &ip6_frag_hash[i], list) {
1701da177e4SLinus Torvalds 			unsigned int hval = ip6qhashfn(q->id,
1711da177e4SLinus Torvalds 						       &q->saddr,
1721da177e4SLinus Torvalds 						       &q->daddr);
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 			if (hval != i) {
175e7c8a41eSYasuyuki Kozakai 				hlist_del(&q->list);
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 				/* Relink to new hash chain. */
178e7c8a41eSYasuyuki Kozakai 				hlist_add_head(&q->list,
179e7c8a41eSYasuyuki Kozakai 					       &ip6_frag_hash[hval]);
1801da177e4SLinus Torvalds 
181e7c8a41eSYasuyuki Kozakai 			}
1821da177e4SLinus Torvalds 		}
1831da177e4SLinus Torvalds 	}
1841da177e4SLinus Torvalds 	write_unlock(&ip6_frag_lock);
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 	mod_timer(&ip6_frag_secret_timer, now + sysctl_ip6frag_secret_interval);
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds atomic_t ip6_frag_mem = ATOMIC_INIT(0);
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds /* Memory Tracking Functions. */
1921da177e4SLinus Torvalds static inline void frag_kfree_skb(struct sk_buff *skb, int *work)
1931da177e4SLinus Torvalds {
1941da177e4SLinus Torvalds 	if (work)
1951da177e4SLinus Torvalds 		*work -= skb->truesize;
1961da177e4SLinus Torvalds 	atomic_sub(skb->truesize, &ip6_frag_mem);
1971da177e4SLinus Torvalds 	kfree_skb(skb);
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds static inline void frag_free_queue(struct frag_queue *fq, int *work)
2011da177e4SLinus Torvalds {
2021da177e4SLinus Torvalds 	if (work)
2031da177e4SLinus Torvalds 		*work -= sizeof(struct frag_queue);
2041da177e4SLinus Torvalds 	atomic_sub(sizeof(struct frag_queue), &ip6_frag_mem);
2051da177e4SLinus Torvalds 	kfree(fq);
2061da177e4SLinus Torvalds }
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds static inline struct frag_queue *frag_alloc_queue(void)
2091da177e4SLinus Torvalds {
21078c784c4SIngo Oeser 	struct frag_queue *fq = kzalloc(sizeof(struct frag_queue), GFP_ATOMIC);
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	if(!fq)
2131da177e4SLinus Torvalds 		return NULL;
2141da177e4SLinus Torvalds 	atomic_add(sizeof(struct frag_queue), &ip6_frag_mem);
2151da177e4SLinus Torvalds 	return fq;
2161da177e4SLinus Torvalds }
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds /* Destruction primitives. */
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds /* Complete destruction of fq. */
2211da177e4SLinus Torvalds static void ip6_frag_destroy(struct frag_queue *fq, int *work)
2221da177e4SLinus Torvalds {
2231da177e4SLinus Torvalds 	struct sk_buff *fp;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	BUG_TRAP(fq->last_in&COMPLETE);
2261da177e4SLinus Torvalds 	BUG_TRAP(del_timer(&fq->timer) == 0);
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	/* Release all fragment data. */
2291da177e4SLinus Torvalds 	fp = fq->fragments;
2301da177e4SLinus Torvalds 	while (fp) {
2311da177e4SLinus Torvalds 		struct sk_buff *xp = fp->next;
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 		frag_kfree_skb(fp, work);
2341da177e4SLinus Torvalds 		fp = xp;
2351da177e4SLinus Torvalds 	}
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	frag_free_queue(fq, work);
2381da177e4SLinus Torvalds }
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds static __inline__ void fq_put(struct frag_queue *fq, int *work)
2411da177e4SLinus Torvalds {
2421da177e4SLinus Torvalds 	if (atomic_dec_and_test(&fq->refcnt))
2431da177e4SLinus Torvalds 		ip6_frag_destroy(fq, work);
2441da177e4SLinus Torvalds }
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds /* Kill fq entry. It is not destroyed immediately,
2471da177e4SLinus Torvalds  * because caller (and someone more) holds reference count.
2481da177e4SLinus Torvalds  */
2491da177e4SLinus Torvalds static __inline__ void fq_kill(struct frag_queue *fq)
2501da177e4SLinus Torvalds {
2511da177e4SLinus Torvalds 	if (del_timer(&fq->timer))
2521da177e4SLinus Torvalds 		atomic_dec(&fq->refcnt);
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	if (!(fq->last_in & COMPLETE)) {
2551da177e4SLinus Torvalds 		fq_unlink(fq);
2561da177e4SLinus Torvalds 		atomic_dec(&fq->refcnt);
2571da177e4SLinus Torvalds 		fq->last_in |= COMPLETE;
2581da177e4SLinus Torvalds 	}
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds 
261a11d206dSYOSHIFUJI Hideaki static void ip6_evictor(struct inet6_dev *idev)
2621da177e4SLinus Torvalds {
2631da177e4SLinus Torvalds 	struct frag_queue *fq;
2641da177e4SLinus Torvalds 	struct list_head *tmp;
2651da177e4SLinus Torvalds 	int work;
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 	work = atomic_read(&ip6_frag_mem) - sysctl_ip6frag_low_thresh;
2681da177e4SLinus Torvalds 	if (work <= 0)
2691da177e4SLinus Torvalds 		return;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	while(work > 0) {
2721da177e4SLinus Torvalds 		read_lock(&ip6_frag_lock);
2731da177e4SLinus Torvalds 		if (list_empty(&ip6_frag_lru_list)) {
2741da177e4SLinus Torvalds 			read_unlock(&ip6_frag_lock);
2751da177e4SLinus Torvalds 			return;
2761da177e4SLinus Torvalds 		}
2771da177e4SLinus Torvalds 		tmp = ip6_frag_lru_list.next;
2781da177e4SLinus Torvalds 		fq = list_entry(tmp, struct frag_queue, lru_list);
2791da177e4SLinus Torvalds 		atomic_inc(&fq->refcnt);
2801da177e4SLinus Torvalds 		read_unlock(&ip6_frag_lock);
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 		spin_lock(&fq->lock);
2831da177e4SLinus Torvalds 		if (!(fq->last_in&COMPLETE))
2841da177e4SLinus Torvalds 			fq_kill(fq);
2851da177e4SLinus Torvalds 		spin_unlock(&fq->lock);
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 		fq_put(fq, &work);
288a11d206dSYOSHIFUJI Hideaki 		IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
2891da177e4SLinus Torvalds 	}
2901da177e4SLinus Torvalds }
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds static void ip6_frag_expire(unsigned long data)
2931da177e4SLinus Torvalds {
2941da177e4SLinus Torvalds 	struct frag_queue *fq = (struct frag_queue *) data;
295a11d206dSYOSHIFUJI Hideaki 	struct net_device *dev = NULL;
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	spin_lock(&fq->lock);
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	if (fq->last_in & COMPLETE)
3001da177e4SLinus Torvalds 		goto out;
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	fq_kill(fq);
3031da177e4SLinus Torvalds 
304a11d206dSYOSHIFUJI Hideaki 	dev = dev_get_by_index(fq->iif);
305a11d206dSYOSHIFUJI Hideaki 	if (!dev)
306a11d206dSYOSHIFUJI Hideaki 		goto out;
307a11d206dSYOSHIFUJI Hideaki 
308a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
309a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
310a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
311a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
3121da177e4SLinus Torvalds 
31378c784c4SIngo Oeser 	/* Don't send error if the first segment did not arrive. */
31478c784c4SIngo Oeser 	if (!(fq->last_in&FIRST_IN) || !fq->fragments)
31578c784c4SIngo Oeser 		goto out;
31678c784c4SIngo Oeser 
3171da177e4SLinus Torvalds 	/*
3181da177e4SLinus Torvalds 	   But use as source device on which LAST ARRIVED
3191da177e4SLinus Torvalds 	   segment was received. And do not use fq->dev
3201da177e4SLinus Torvalds 	   pointer directly, device might already disappeared.
3211da177e4SLinus Torvalds 	 */
3221da177e4SLinus Torvalds 	fq->fragments->dev = dev;
32378c784c4SIngo Oeser 	icmpv6_send(fq->fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0, dev);
3241da177e4SLinus Torvalds out:
325a11d206dSYOSHIFUJI Hideaki 	if (dev)
326a11d206dSYOSHIFUJI Hideaki 		dev_put(dev);
3271da177e4SLinus Torvalds 	spin_unlock(&fq->lock);
3281da177e4SLinus Torvalds 	fq_put(fq, NULL);
3291da177e4SLinus Torvalds }
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds /* Creation primitives. */
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 
334f6596f9dSZach Brown static struct frag_queue *ip6_frag_intern(struct frag_queue *fq_in)
3351da177e4SLinus Torvalds {
3361da177e4SLinus Torvalds 	struct frag_queue *fq;
337f6596f9dSZach Brown 	unsigned int hash;
338e7c8a41eSYasuyuki Kozakai #ifdef CONFIG_SMP
339e7c8a41eSYasuyuki Kozakai 	struct hlist_node *n;
340e7c8a41eSYasuyuki Kozakai #endif
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	write_lock(&ip6_frag_lock);
343f6596f9dSZach Brown 	hash = ip6qhashfn(fq_in->id, &fq_in->saddr, &fq_in->daddr);
3441da177e4SLinus Torvalds #ifdef CONFIG_SMP
345e7c8a41eSYasuyuki Kozakai 	hlist_for_each_entry(fq, n, &ip6_frag_hash[hash], list) {
3461da177e4SLinus Torvalds 		if (fq->id == fq_in->id &&
3471da177e4SLinus Torvalds 		    ipv6_addr_equal(&fq_in->saddr, &fq->saddr) &&
3481da177e4SLinus Torvalds 		    ipv6_addr_equal(&fq_in->daddr, &fq->daddr)) {
3491da177e4SLinus Torvalds 			atomic_inc(&fq->refcnt);
3501da177e4SLinus Torvalds 			write_unlock(&ip6_frag_lock);
3511da177e4SLinus Torvalds 			fq_in->last_in |= COMPLETE;
3521da177e4SLinus Torvalds 			fq_put(fq_in, NULL);
3531da177e4SLinus Torvalds 			return fq;
3541da177e4SLinus Torvalds 		}
3551da177e4SLinus Torvalds 	}
3561da177e4SLinus Torvalds #endif
3571da177e4SLinus Torvalds 	fq = fq_in;
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	if (!mod_timer(&fq->timer, jiffies + sysctl_ip6frag_time))
3601da177e4SLinus Torvalds 		atomic_inc(&fq->refcnt);
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	atomic_inc(&fq->refcnt);
363e7c8a41eSYasuyuki Kozakai 	hlist_add_head(&fq->list, &ip6_frag_hash[hash]);
3641da177e4SLinus Torvalds 	INIT_LIST_HEAD(&fq->lru_list);
3651da177e4SLinus Torvalds 	list_add_tail(&fq->lru_list, &ip6_frag_lru_list);
3661da177e4SLinus Torvalds 	ip6_frag_nqueues++;
3671da177e4SLinus Torvalds 	write_unlock(&ip6_frag_lock);
3681da177e4SLinus Torvalds 	return fq;
3691da177e4SLinus Torvalds }
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds static struct frag_queue *
373a11d206dSYOSHIFUJI Hideaki ip6_frag_create(u32 id, struct in6_addr *src, struct in6_addr *dst,
374a11d206dSYOSHIFUJI Hideaki 		struct inet6_dev *idev)
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 	fq->id = id;
3821da177e4SLinus Torvalds 	ipv6_addr_copy(&fq->saddr, src);
3831da177e4SLinus Torvalds 	ipv6_addr_copy(&fq->daddr, dst);
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 	init_timer(&fq->timer);
3861da177e4SLinus Torvalds 	fq->timer.function = ip6_frag_expire;
3871da177e4SLinus Torvalds 	fq->timer.data = (long) fq;
3881da177e4SLinus Torvalds 	spin_lock_init(&fq->lock);
3891da177e4SLinus Torvalds 	atomic_set(&fq->refcnt, 1);
3901da177e4SLinus Torvalds 
391f6596f9dSZach Brown 	return ip6_frag_intern(fq);
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds oom:
394a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
3951da177e4SLinus Torvalds 	return NULL;
3961da177e4SLinus Torvalds }
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds static __inline__ struct frag_queue *
399a11d206dSYOSHIFUJI Hideaki fq_find(u32 id, struct in6_addr *src, struct in6_addr *dst,
400a11d206dSYOSHIFUJI Hideaki 	struct inet6_dev *idev)
4011da177e4SLinus Torvalds {
4021da177e4SLinus Torvalds 	struct frag_queue *fq;
403e7c8a41eSYasuyuki Kozakai 	struct hlist_node *n;
404f6596f9dSZach Brown 	unsigned int hash;
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	read_lock(&ip6_frag_lock);
407f6596f9dSZach Brown 	hash = ip6qhashfn(id, src, dst);
408e7c8a41eSYasuyuki Kozakai 	hlist_for_each_entry(fq, n, &ip6_frag_hash[hash], list) {
4091da177e4SLinus Torvalds 		if (fq->id == id &&
4101da177e4SLinus Torvalds 		    ipv6_addr_equal(src, &fq->saddr) &&
4111da177e4SLinus Torvalds 		    ipv6_addr_equal(dst, &fq->daddr)) {
4121da177e4SLinus Torvalds 			atomic_inc(&fq->refcnt);
4131da177e4SLinus Torvalds 			read_unlock(&ip6_frag_lock);
4141da177e4SLinus Torvalds 			return fq;
4151da177e4SLinus Torvalds 		}
4161da177e4SLinus Torvalds 	}
4171da177e4SLinus Torvalds 	read_unlock(&ip6_frag_lock);
4181da177e4SLinus Torvalds 
419a11d206dSYOSHIFUJI Hideaki 	return ip6_frag_create(id, src, dst, idev);
4201da177e4SLinus Torvalds }
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds static void ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
4241da177e4SLinus Torvalds 			   struct frag_hdr *fhdr, int nhoff)
4251da177e4SLinus Torvalds {
4261da177e4SLinus Torvalds 	struct sk_buff *prev, *next;
4271da177e4SLinus Torvalds 	int offset, end;
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	if (fq->last_in & COMPLETE)
4301da177e4SLinus Torvalds 		goto err;
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	offset = ntohs(fhdr->frag_off) & ~0x7;
4331da177e4SLinus Torvalds 	end = offset + (ntohs(skb->nh.ipv6h->payload_len) -
4341da177e4SLinus Torvalds 			((u8 *) (fhdr + 1) - (u8 *) (skb->nh.ipv6h + 1)));
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	if ((unsigned int)end > IPV6_MAXPLEN) {
437a11d206dSYOSHIFUJI Hideaki 		IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
438a11d206dSYOSHIFUJI Hideaki 				 IPSTATS_MIB_INHDRERRORS);
4391da177e4SLinus Torvalds  		icmpv6_param_prob(skb,ICMPV6_HDR_FIELD, (u8*)&fhdr->frag_off - skb->nh.raw);
4401da177e4SLinus Torvalds  		return;
4411da177e4SLinus Torvalds 	}
4421da177e4SLinus Torvalds 
44384fa7933SPatrick McHardy  	if (skb->ip_summed == CHECKSUM_COMPLETE)
4441da177e4SLinus Torvalds  		skb->csum = csum_sub(skb->csum,
4451da177e4SLinus Torvalds  				     csum_partial(skb->nh.raw, (u8*)(fhdr+1)-skb->nh.raw, 0));
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	/* Is this the final fragment? */
4481da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(IP6_MF))) {
4491da177e4SLinus Torvalds 		/* If we already have some bits beyond end
4501da177e4SLinus Torvalds 		 * or have different end, the segment is corrupted.
4511da177e4SLinus Torvalds 		 */
4521da177e4SLinus Torvalds 		if (end < fq->len ||
4531da177e4SLinus Torvalds 		    ((fq->last_in & LAST_IN) && end != fq->len))
4541da177e4SLinus Torvalds 			goto err;
4551da177e4SLinus Torvalds 		fq->last_in |= LAST_IN;
4561da177e4SLinus Torvalds 		fq->len = end;
4571da177e4SLinus Torvalds 	} else {
4581da177e4SLinus Torvalds 		/* Check if the fragment is rounded to 8 bytes.
4591da177e4SLinus Torvalds 		 * Required by the RFC.
4601da177e4SLinus Torvalds 		 */
4611da177e4SLinus Torvalds 		if (end & 0x7) {
4621da177e4SLinus Torvalds 			/* RFC2460 says always send parameter problem in
4631da177e4SLinus Torvalds 			 * this case. -DaveM
4641da177e4SLinus Torvalds 			 */
465a11d206dSYOSHIFUJI Hideaki 			IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
466a11d206dSYOSHIFUJI Hideaki 					 IPSTATS_MIB_INHDRERRORS);
4671da177e4SLinus Torvalds 			icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
4681da177e4SLinus Torvalds 					  offsetof(struct ipv6hdr, payload_len));
4691da177e4SLinus Torvalds 			return;
4701da177e4SLinus Torvalds 		}
4711da177e4SLinus Torvalds 		if (end > fq->len) {
4721da177e4SLinus Torvalds 			/* Some bits beyond end -> corruption. */
4731da177e4SLinus Torvalds 			if (fq->last_in & LAST_IN)
4741da177e4SLinus Torvalds 				goto err;
4751da177e4SLinus Torvalds 			fq->len = end;
4761da177e4SLinus Torvalds 		}
4771da177e4SLinus Torvalds 	}
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 	if (end == offset)
4801da177e4SLinus Torvalds 		goto err;
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 	/* Point into the IP datagram 'data' part. */
4831da177e4SLinus Torvalds 	if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
4841da177e4SLinus Torvalds 		goto err;
48542ca89c1SStephen Hemminger 
48642ca89c1SStephen Hemminger 	if (pskb_trim_rcsum(skb, end - offset))
4871da177e4SLinus Torvalds 		goto err;
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	/* Find out which fragments are in front and at the back of us
4901da177e4SLinus Torvalds 	 * in the chain of fragments so far.  We must know where to put
4911da177e4SLinus Torvalds 	 * this fragment, right?
4921da177e4SLinus Torvalds 	 */
4931da177e4SLinus Torvalds 	prev = NULL;
4941da177e4SLinus Torvalds 	for(next = fq->fragments; next != NULL; next = next->next) {
4951da177e4SLinus Torvalds 		if (FRAG6_CB(next)->offset >= offset)
4961da177e4SLinus Torvalds 			break;	/* bingo! */
4971da177e4SLinus Torvalds 		prev = next;
4981da177e4SLinus Torvalds 	}
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds 	/* We found where to put this one.  Check for overlap with
5011da177e4SLinus Torvalds 	 * preceding fragment, and, if needed, align things so that
5021da177e4SLinus Torvalds 	 * any overlaps are eliminated.
5031da177e4SLinus Torvalds 	 */
5041da177e4SLinus Torvalds 	if (prev) {
5051da177e4SLinus Torvalds 		int i = (FRAG6_CB(prev)->offset + prev->len) - offset;
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 		if (i > 0) {
5081da177e4SLinus Torvalds 			offset += i;
5091da177e4SLinus Torvalds 			if (end <= offset)
5101da177e4SLinus Torvalds 				goto err;
5111da177e4SLinus Torvalds 			if (!pskb_pull(skb, i))
5121da177e4SLinus Torvalds 				goto err;
5131da177e4SLinus Torvalds 			if (skb->ip_summed != CHECKSUM_UNNECESSARY)
5141da177e4SLinus Torvalds 				skb->ip_summed = CHECKSUM_NONE;
5151da177e4SLinus Torvalds 		}
5161da177e4SLinus Torvalds 	}
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	/* Look for overlap with succeeding segments.
5191da177e4SLinus Torvalds 	 * If we can merge fragments, do it.
5201da177e4SLinus Torvalds 	 */
5211da177e4SLinus Torvalds 	while (next && FRAG6_CB(next)->offset < end) {
5221da177e4SLinus Torvalds 		int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds 		if (i < next->len) {
5251da177e4SLinus Torvalds 			/* Eat head of the next overlapped fragment
5261da177e4SLinus Torvalds 			 * and leave the loop. The next ones cannot overlap.
5271da177e4SLinus Torvalds 			 */
5281da177e4SLinus Torvalds 			if (!pskb_pull(next, i))
5291da177e4SLinus Torvalds 				goto err;
5301da177e4SLinus Torvalds 			FRAG6_CB(next)->offset += i;	/* next fragment */
5311da177e4SLinus Torvalds 			fq->meat -= i;
5321da177e4SLinus Torvalds 			if (next->ip_summed != CHECKSUM_UNNECESSARY)
5331da177e4SLinus Torvalds 				next->ip_summed = CHECKSUM_NONE;
5341da177e4SLinus Torvalds 			break;
5351da177e4SLinus Torvalds 		} else {
5361da177e4SLinus Torvalds 			struct sk_buff *free_it = next;
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 			/* Old fragment is completely overridden with
5391da177e4SLinus Torvalds 			 * new one drop it.
5401da177e4SLinus Torvalds 			 */
5411da177e4SLinus Torvalds 			next = next->next;
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 			if (prev)
5441da177e4SLinus Torvalds 				prev->next = next;
5451da177e4SLinus Torvalds 			else
5461da177e4SLinus Torvalds 				fq->fragments = next;
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 			fq->meat -= free_it->len;
5491da177e4SLinus Torvalds 			frag_kfree_skb(free_it, NULL);
5501da177e4SLinus Torvalds 		}
5511da177e4SLinus Torvalds 	}
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	FRAG6_CB(skb)->offset = offset;
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 	/* Insert this fragment in the chain of fragments. */
5561da177e4SLinus Torvalds 	skb->next = next;
5571da177e4SLinus Torvalds 	if (prev)
5581da177e4SLinus Torvalds 		prev->next = skb;
5591da177e4SLinus Torvalds 	else
5601da177e4SLinus Torvalds 		fq->fragments = skb;
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	if (skb->dev)
5631da177e4SLinus Torvalds 		fq->iif = skb->dev->ifindex;
5641da177e4SLinus Torvalds 	skb->dev = NULL;
565a61bbcf2SPatrick McHardy 	skb_get_timestamp(skb, &fq->stamp);
5661da177e4SLinus Torvalds 	fq->meat += skb->len;
5671da177e4SLinus Torvalds 	atomic_add(skb->truesize, &ip6_frag_mem);
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 	/* The first fragment.
5701da177e4SLinus Torvalds 	 * nhoffset is obtained from the first fragment, of course.
5711da177e4SLinus Torvalds 	 */
5721da177e4SLinus Torvalds 	if (offset == 0) {
5731da177e4SLinus Torvalds 		fq->nhoffset = nhoff;
5741da177e4SLinus Torvalds 		fq->last_in |= FIRST_IN;
5751da177e4SLinus Torvalds 	}
5761da177e4SLinus Torvalds 	write_lock(&ip6_frag_lock);
5771da177e4SLinus Torvalds 	list_move_tail(&fq->lru_list, &ip6_frag_lru_list);
5781da177e4SLinus Torvalds 	write_unlock(&ip6_frag_lock);
5791da177e4SLinus Torvalds 	return;
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds err:
582a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
5831da177e4SLinus Torvalds 	kfree_skb(skb);
5841da177e4SLinus Torvalds }
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds /*
5871da177e4SLinus Torvalds  *	Check if this packet is complete.
5881da177e4SLinus Torvalds  *	Returns NULL on failure by any reason, and pointer
5891da177e4SLinus Torvalds  *	to current nexthdr field in reassembled frame.
5901da177e4SLinus Torvalds  *
5911da177e4SLinus Torvalds  *	It is called with locked fq, and caller must check that
5921da177e4SLinus Torvalds  *	queue is eligible for reassembly i.e. it is not COMPLETE,
5931da177e4SLinus Torvalds  *	the last and the first frames arrived and all the bits are here.
5941da177e4SLinus Torvalds  */
5951da177e4SLinus Torvalds static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff **skb_in,
5961da177e4SLinus Torvalds 			  struct net_device *dev)
5971da177e4SLinus Torvalds {
5981da177e4SLinus Torvalds 	struct sk_buff *fp, *head = fq->fragments;
5991da177e4SLinus Torvalds 	int    payload_len;
6001da177e4SLinus Torvalds 	unsigned int nhoff;
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	fq_kill(fq);
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	BUG_TRAP(head != NULL);
6051da177e4SLinus Torvalds 	BUG_TRAP(FRAG6_CB(head)->offset == 0);
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	/* Unfragmented part is taken from the first segment. */
6081da177e4SLinus Torvalds 	payload_len = (head->data - head->nh.raw) - sizeof(struct ipv6hdr) + fq->len - sizeof(struct frag_hdr);
6091da177e4SLinus Torvalds 	if (payload_len > IPV6_MAXPLEN)
6101da177e4SLinus Torvalds 		goto out_oversize;
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 	/* Head of list must not be cloned. */
6131da177e4SLinus Torvalds 	if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
6141da177e4SLinus Torvalds 		goto out_oom;
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	/* If the first fragment is fragmented itself, we split
6171da177e4SLinus Torvalds 	 * it to two chunks: the first with data and paged part
6181da177e4SLinus Torvalds 	 * and the second, holding only fragments. */
6191da177e4SLinus Torvalds 	if (skb_shinfo(head)->frag_list) {
6201da177e4SLinus Torvalds 		struct sk_buff *clone;
6211da177e4SLinus Torvalds 		int i, plen = 0;
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 		if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
6241da177e4SLinus Torvalds 			goto out_oom;
6251da177e4SLinus Torvalds 		clone->next = head->next;
6261da177e4SLinus Torvalds 		head->next = clone;
6271da177e4SLinus Torvalds 		skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
6281da177e4SLinus Torvalds 		skb_shinfo(head)->frag_list = NULL;
6291da177e4SLinus Torvalds 		for (i=0; i<skb_shinfo(head)->nr_frags; i++)
6301da177e4SLinus Torvalds 			plen += skb_shinfo(head)->frags[i].size;
6311da177e4SLinus Torvalds 		clone->len = clone->data_len = head->data_len - plen;
6321da177e4SLinus Torvalds 		head->data_len -= clone->len;
6331da177e4SLinus Torvalds 		head->len -= clone->len;
6341da177e4SLinus Torvalds 		clone->csum = 0;
6351da177e4SLinus Torvalds 		clone->ip_summed = head->ip_summed;
6361da177e4SLinus Torvalds 		atomic_add(clone->truesize, &ip6_frag_mem);
6371da177e4SLinus Torvalds 	}
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	/* We have to remove fragment header from datagram and to relocate
6401da177e4SLinus Torvalds 	 * header in order to calculate ICV correctly. */
6411da177e4SLinus Torvalds 	nhoff = fq->nhoffset;
6421da177e4SLinus Torvalds 	head->nh.raw[nhoff] = head->h.raw[0];
6431da177e4SLinus Torvalds 	memmove(head->head + sizeof(struct frag_hdr), head->head,
6441da177e4SLinus Torvalds 		(head->data - head->head) - sizeof(struct frag_hdr));
6451da177e4SLinus Torvalds 	head->mac.raw += sizeof(struct frag_hdr);
6461da177e4SLinus Torvalds 	head->nh.raw += sizeof(struct frag_hdr);
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	skb_shinfo(head)->frag_list = head->next;
6491da177e4SLinus Torvalds 	head->h.raw = head->data;
6501da177e4SLinus Torvalds 	skb_push(head, head->data - head->nh.raw);
6511da177e4SLinus Torvalds 	atomic_sub(head->truesize, &ip6_frag_mem);
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 	for (fp=head->next; fp; fp = fp->next) {
6541da177e4SLinus Torvalds 		head->data_len += fp->len;
6551da177e4SLinus Torvalds 		head->len += fp->len;
6561da177e4SLinus Torvalds 		if (head->ip_summed != fp->ip_summed)
6571da177e4SLinus Torvalds 			head->ip_summed = CHECKSUM_NONE;
65884fa7933SPatrick McHardy 		else if (head->ip_summed == CHECKSUM_COMPLETE)
6591da177e4SLinus Torvalds 			head->csum = csum_add(head->csum, fp->csum);
6601da177e4SLinus Torvalds 		head->truesize += fp->truesize;
6611da177e4SLinus Torvalds 		atomic_sub(fp->truesize, &ip6_frag_mem);
6621da177e4SLinus Torvalds 	}
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	head->next = NULL;
6651da177e4SLinus Torvalds 	head->dev = dev;
666a61bbcf2SPatrick McHardy 	skb_set_timestamp(head, &fq->stamp);
6671da177e4SLinus Torvalds 	head->nh.ipv6h->payload_len = htons(payload_len);
668951dbc8aSPatrick McHardy 	IP6CB(head)->nhoff = nhoff;
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 	*skb_in = head;
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	/* Yes, and fold redundant checksum back. 8) */
67384fa7933SPatrick McHardy 	if (head->ip_summed == CHECKSUM_COMPLETE)
6741da177e4SLinus Torvalds 		head->csum = csum_partial(head->nh.raw, head->h.raw-head->nh.raw, head->csum);
6751da177e4SLinus Torvalds 
676a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
677a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
678a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
6791da177e4SLinus Torvalds 	fq->fragments = NULL;
6801da177e4SLinus Torvalds 	return 1;
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds out_oversize:
6831da177e4SLinus Torvalds 	if (net_ratelimit())
6841da177e4SLinus Torvalds 		printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len);
6851da177e4SLinus Torvalds 	goto out_fail;
6861da177e4SLinus Torvalds out_oom:
6871da177e4SLinus Torvalds 	if (net_ratelimit())
6881da177e4SLinus Torvalds 		printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n");
6891da177e4SLinus Torvalds out_fail:
690a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
691a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
692a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
6931da177e4SLinus Torvalds 	return -1;
6941da177e4SLinus Torvalds }
6951da177e4SLinus Torvalds 
696951dbc8aSPatrick McHardy static int ipv6_frag_rcv(struct sk_buff **skbp)
6971da177e4SLinus Torvalds {
6981da177e4SLinus Torvalds 	struct sk_buff *skb = *skbp;
6991da177e4SLinus Torvalds 	struct net_device *dev = skb->dev;
7001da177e4SLinus Torvalds 	struct frag_hdr *fhdr;
7011da177e4SLinus Torvalds 	struct frag_queue *fq;
7021da177e4SLinus Torvalds 	struct ipv6hdr *hdr;
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 	hdr = skb->nh.ipv6h;
7051da177e4SLinus Torvalds 
706a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMREQDS);
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds 	/* Jumbo payload inhibits frag. header */
7091da177e4SLinus Torvalds 	if (hdr->payload_len==0) {
710a11d206dSYOSHIFUJI Hideaki 		IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
7111da177e4SLinus Torvalds 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb->h.raw-skb->nh.raw);
7121da177e4SLinus Torvalds 		return -1;
7131da177e4SLinus Torvalds 	}
7141da177e4SLinus Torvalds 	if (!pskb_may_pull(skb, (skb->h.raw-skb->data)+sizeof(struct frag_hdr))) {
715a11d206dSYOSHIFUJI Hideaki 		IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
7161da177e4SLinus Torvalds 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb->h.raw-skb->nh.raw);
7171da177e4SLinus Torvalds 		return -1;
7181da177e4SLinus Torvalds 	}
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	hdr = skb->nh.ipv6h;
7211da177e4SLinus Torvalds 	fhdr = (struct frag_hdr *)skb->h.raw;
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(0xFFF9))) {
7241da177e4SLinus Torvalds 		/* It is not a fragmented frame */
7251da177e4SLinus Torvalds 		skb->h.raw += sizeof(struct frag_hdr);
726a11d206dSYOSHIFUJI Hideaki 		IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMOKS);
7271da177e4SLinus Torvalds 
728951dbc8aSPatrick McHardy 		IP6CB(skb)->nhoff = (u8*)fhdr - skb->nh.raw;
7291da177e4SLinus Torvalds 		return 1;
7301da177e4SLinus Torvalds 	}
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 	if (atomic_read(&ip6_frag_mem) > sysctl_ip6frag_high_thresh)
733a11d206dSYOSHIFUJI Hideaki 		ip6_evictor(ip6_dst_idev(skb->dst));
7341da177e4SLinus Torvalds 
735a11d206dSYOSHIFUJI Hideaki 	if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr,
736a11d206dSYOSHIFUJI Hideaki 			  ip6_dst_idev(skb->dst))) != NULL) {
7371da177e4SLinus Torvalds 		int ret = -1;
7381da177e4SLinus Torvalds 
7391da177e4SLinus Torvalds 		spin_lock(&fq->lock);
7401da177e4SLinus Torvalds 
741951dbc8aSPatrick McHardy 		ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds 		if (fq->last_in == (FIRST_IN|LAST_IN) &&
7441da177e4SLinus Torvalds 		    fq->meat == fq->len)
745951dbc8aSPatrick McHardy 			ret = ip6_frag_reasm(fq, skbp, dev);
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 		spin_unlock(&fq->lock);
7481da177e4SLinus Torvalds 		fq_put(fq, NULL);
7491da177e4SLinus Torvalds 		return ret;
7501da177e4SLinus Torvalds 	}
7511da177e4SLinus Torvalds 
752a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
7531da177e4SLinus Torvalds 	kfree_skb(skb);
7541da177e4SLinus Torvalds 	return -1;
7551da177e4SLinus Torvalds }
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds static struct inet6_protocol frag_protocol =
7581da177e4SLinus Torvalds {
7591da177e4SLinus Torvalds 	.handler	=	ipv6_frag_rcv,
7601da177e4SLinus Torvalds 	.flags		=	INET6_PROTO_NOPOLICY,
7611da177e4SLinus Torvalds };
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds void __init ipv6_frag_init(void)
7641da177e4SLinus Torvalds {
7651da177e4SLinus Torvalds 	if (inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT) < 0)
7661da177e4SLinus Torvalds 		printk(KERN_ERR "ipv6_frag_init: Could not register protocol\n");
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 	ip6_frag_hash_rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
7691da177e4SLinus Torvalds 				   (jiffies ^ (jiffies >> 6)));
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds 	init_timer(&ip6_frag_secret_timer);
7721da177e4SLinus Torvalds 	ip6_frag_secret_timer.function = ip6_frag_secret_rebuild;
7731da177e4SLinus Torvalds 	ip6_frag_secret_timer.expires = jiffies + sysctl_ip6frag_secret_interval;
7741da177e4SLinus Torvalds 	add_timer(&ip6_frag_secret_timer);
7751da177e4SLinus Torvalds }
776