xref: /openbmc/linux/net/ipv6/reassembly.c (revision 7eb95156)
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>
45f61944efSHerbert Xu #include <linux/skbuff.h>
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #include <net/sock.h>
481da177e4SLinus Torvalds #include <net/snmp.h>
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #include <net/ipv6.h>
51a11d206dSYOSHIFUJI Hideaki #include <net/ip6_route.h>
521da177e4SLinus Torvalds #include <net/protocol.h>
531da177e4SLinus Torvalds #include <net/transp_v6.h>
541da177e4SLinus Torvalds #include <net/rawv6.h>
551da177e4SLinus Torvalds #include <net/ndisc.h>
561da177e4SLinus Torvalds #include <net/addrconf.h>
575ab11c98SPavel Emelyanov #include <net/inet_frag.h>
581da177e4SLinus Torvalds 
59ab32ea5dSBrian Haley int sysctl_ip6frag_high_thresh __read_mostly = 256*1024;
60ab32ea5dSBrian Haley int sysctl_ip6frag_low_thresh __read_mostly = 192*1024;
611da177e4SLinus Torvalds 
62ab32ea5dSBrian Haley int sysctl_ip6frag_time __read_mostly = IPV6_FRAG_TIMEOUT;
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds struct ip6frag_skb_cb
651da177e4SLinus Torvalds {
661da177e4SLinus Torvalds 	struct inet6_skb_parm	h;
671da177e4SLinus Torvalds 	int			offset;
681da177e4SLinus Torvalds };
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds #define FRAG6_CB(skb)	((struct ip6frag_skb_cb*)((skb)->cb))
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /*
741da177e4SLinus Torvalds  *	Equivalent of ipv4 struct ipq
751da177e4SLinus Torvalds  */
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds struct frag_queue
781da177e4SLinus Torvalds {
795ab11c98SPavel Emelyanov 	struct inet_frag_queue	q;
801da177e4SLinus Torvalds 
81e69a4adcSAl Viro 	__be32			id;		/* fragment id		*/
821da177e4SLinus Torvalds 	struct in6_addr		saddr;
831da177e4SLinus Torvalds 	struct in6_addr		daddr;
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds 	int			iif;
861da177e4SLinus Torvalds 	unsigned int		csum;
871da177e4SLinus Torvalds 	__u16			nhoffset;
881da177e4SLinus Torvalds };
891da177e4SLinus Torvalds 
907eb95156SPavel Emelyanov static struct inet_frags ip6_frags;
911da177e4SLinus Torvalds 
927eb95156SPavel Emelyanov int ip6_frag_nqueues(void)
937eb95156SPavel Emelyanov {
947eb95156SPavel Emelyanov 	return ip6_frags.nqueues;
957eb95156SPavel Emelyanov }
961da177e4SLinus Torvalds 
977eb95156SPavel Emelyanov int ip6_frag_mem(void)
987eb95156SPavel Emelyanov {
997eb95156SPavel Emelyanov 	return atomic_read(&ip6_frags.mem);
1007eb95156SPavel Emelyanov }
1011da177e4SLinus Torvalds 
102f61944efSHerbert Xu static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
103f61944efSHerbert Xu 			  struct net_device *dev);
104f61944efSHerbert Xu 
1051da177e4SLinus Torvalds static __inline__ void __fq_unlink(struct frag_queue *fq)
1061da177e4SLinus Torvalds {
1075ab11c98SPavel Emelyanov 	hlist_del(&fq->q.list);
1085ab11c98SPavel Emelyanov 	list_del(&fq->q.lru_list);
1097eb95156SPavel Emelyanov 	ip6_frags.nqueues--;
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds static __inline__ void fq_unlink(struct frag_queue *fq)
1131da177e4SLinus Torvalds {
1147eb95156SPavel Emelyanov 	write_lock(&ip6_frags.lock);
1151da177e4SLinus Torvalds 	__fq_unlink(fq);
1167eb95156SPavel Emelyanov 	write_unlock(&ip6_frags.lock);
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds 
119f6596f9dSZach Brown /*
120f6596f9dSZach Brown  * callers should be careful not to use the hash value outside the ipfrag_lock
121f6596f9dSZach Brown  * as doing so could race with ipfrag_hash_rnd being recalculated.
122f6596f9dSZach Brown  */
123e69a4adcSAl Viro static unsigned int ip6qhashfn(__be32 id, struct in6_addr *saddr,
1241da177e4SLinus Torvalds 			       struct in6_addr *daddr)
1251da177e4SLinus Torvalds {
1261da177e4SLinus Torvalds 	u32 a, b, c;
1271da177e4SLinus Torvalds 
128e69a4adcSAl Viro 	a = (__force u32)saddr->s6_addr32[0];
129e69a4adcSAl Viro 	b = (__force u32)saddr->s6_addr32[1];
130e69a4adcSAl Viro 	c = (__force u32)saddr->s6_addr32[2];
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	a += JHASH_GOLDEN_RATIO;
1331da177e4SLinus Torvalds 	b += JHASH_GOLDEN_RATIO;
1347eb95156SPavel Emelyanov 	c += ip6_frags.rnd;
1351da177e4SLinus Torvalds 	__jhash_mix(a, b, c);
1361da177e4SLinus Torvalds 
137e69a4adcSAl Viro 	a += (__force u32)saddr->s6_addr32[3];
138e69a4adcSAl Viro 	b += (__force u32)daddr->s6_addr32[0];
139e69a4adcSAl Viro 	c += (__force u32)daddr->s6_addr32[1];
1401da177e4SLinus Torvalds 	__jhash_mix(a, b, c);
1411da177e4SLinus Torvalds 
142e69a4adcSAl Viro 	a += (__force u32)daddr->s6_addr32[2];
143e69a4adcSAl Viro 	b += (__force u32)daddr->s6_addr32[3];
144e69a4adcSAl Viro 	c += (__force u32)id;
1451da177e4SLinus Torvalds 	__jhash_mix(a, b, c);
1461da177e4SLinus Torvalds 
1477eb95156SPavel Emelyanov 	return c & (INETFRAGS_HASHSZ - 1);
1481da177e4SLinus Torvalds }
1491da177e4SLinus Torvalds 
150ab32ea5dSBrian Haley int sysctl_ip6frag_secret_interval __read_mostly = 10 * 60 * HZ;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds static void ip6_frag_secret_rebuild(unsigned long dummy)
1531da177e4SLinus Torvalds {
1541da177e4SLinus Torvalds 	unsigned long now = jiffies;
1551da177e4SLinus Torvalds 	int i;
1561da177e4SLinus Torvalds 
1577eb95156SPavel Emelyanov 	write_lock(&ip6_frags.lock);
1587eb95156SPavel Emelyanov 	get_random_bytes(&ip6_frags.rnd, sizeof(u32));
1597eb95156SPavel Emelyanov 	for (i = 0; i < INETFRAGS_HASHSZ; i++) {
1601da177e4SLinus Torvalds 		struct frag_queue *q;
161e7c8a41eSYasuyuki Kozakai 		struct hlist_node *p, *n;
1621da177e4SLinus Torvalds 
1637eb95156SPavel Emelyanov 		hlist_for_each_entry_safe(q, p, n, &ip6_frags.hash[i], q.list) {
1641da177e4SLinus Torvalds 			unsigned int hval = ip6qhashfn(q->id,
1651da177e4SLinus Torvalds 						       &q->saddr,
1661da177e4SLinus Torvalds 						       &q->daddr);
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 			if (hval != i) {
1695ab11c98SPavel Emelyanov 				hlist_del(&q->q.list);
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 				/* Relink to new hash chain. */
1725ab11c98SPavel Emelyanov 				hlist_add_head(&q->q.list,
1737eb95156SPavel Emelyanov 					       &ip6_frags.hash[hval]);
1741da177e4SLinus Torvalds 
175e7c8a41eSYasuyuki Kozakai 			}
1761da177e4SLinus Torvalds 		}
1771da177e4SLinus Torvalds 	}
1787eb95156SPavel Emelyanov 	write_unlock(&ip6_frags.lock);
1791da177e4SLinus Torvalds 
1807eb95156SPavel Emelyanov 	mod_timer(&ip6_frags.secret_timer, now + sysctl_ip6frag_secret_interval);
1811da177e4SLinus Torvalds }
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds /* Memory Tracking Functions. */
1841da177e4SLinus Torvalds static inline void frag_kfree_skb(struct sk_buff *skb, int *work)
1851da177e4SLinus Torvalds {
1861da177e4SLinus Torvalds 	if (work)
1871da177e4SLinus Torvalds 		*work -= skb->truesize;
1887eb95156SPavel Emelyanov 	atomic_sub(skb->truesize, &ip6_frags.mem);
1891da177e4SLinus Torvalds 	kfree_skb(skb);
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds static inline void frag_free_queue(struct frag_queue *fq, int *work)
1931da177e4SLinus Torvalds {
1941da177e4SLinus Torvalds 	if (work)
1951da177e4SLinus Torvalds 		*work -= sizeof(struct frag_queue);
1967eb95156SPavel Emelyanov 	atomic_sub(sizeof(struct frag_queue), &ip6_frags.mem);
1971da177e4SLinus Torvalds 	kfree(fq);
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds static inline struct frag_queue *frag_alloc_queue(void)
2011da177e4SLinus Torvalds {
20278c784c4SIngo Oeser 	struct frag_queue *fq = kzalloc(sizeof(struct frag_queue), GFP_ATOMIC);
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	if(!fq)
2051da177e4SLinus Torvalds 		return NULL;
2067eb95156SPavel Emelyanov 	atomic_add(sizeof(struct frag_queue), &ip6_frags.mem);
2071da177e4SLinus Torvalds 	return fq;
2081da177e4SLinus Torvalds }
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds /* Destruction primitives. */
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds /* Complete destruction of fq. */
2131da177e4SLinus Torvalds static void ip6_frag_destroy(struct frag_queue *fq, int *work)
2141da177e4SLinus Torvalds {
2151da177e4SLinus Torvalds 	struct sk_buff *fp;
2161da177e4SLinus Torvalds 
2175ab11c98SPavel Emelyanov 	BUG_TRAP(fq->q.last_in&COMPLETE);
2185ab11c98SPavel Emelyanov 	BUG_TRAP(del_timer(&fq->q.timer) == 0);
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	/* Release all fragment data. */
2215ab11c98SPavel Emelyanov 	fp = fq->q.fragments;
2221da177e4SLinus Torvalds 	while (fp) {
2231da177e4SLinus Torvalds 		struct sk_buff *xp = fp->next;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 		frag_kfree_skb(fp, work);
2261da177e4SLinus Torvalds 		fp = xp;
2271da177e4SLinus Torvalds 	}
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	frag_free_queue(fq, work);
2301da177e4SLinus Torvalds }
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds static __inline__ void fq_put(struct frag_queue *fq, int *work)
2331da177e4SLinus Torvalds {
2345ab11c98SPavel Emelyanov 	if (atomic_dec_and_test(&fq->q.refcnt))
2351da177e4SLinus Torvalds 		ip6_frag_destroy(fq, work);
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds /* Kill fq entry. It is not destroyed immediately,
2391da177e4SLinus Torvalds  * because caller (and someone more) holds reference count.
2401da177e4SLinus Torvalds  */
2411da177e4SLinus Torvalds static __inline__ void fq_kill(struct frag_queue *fq)
2421da177e4SLinus Torvalds {
2435ab11c98SPavel Emelyanov 	if (del_timer(&fq->q.timer))
2445ab11c98SPavel Emelyanov 		atomic_dec(&fq->q.refcnt);
2451da177e4SLinus Torvalds 
2465ab11c98SPavel Emelyanov 	if (!(fq->q.last_in & COMPLETE)) {
2471da177e4SLinus Torvalds 		fq_unlink(fq);
2485ab11c98SPavel Emelyanov 		atomic_dec(&fq->q.refcnt);
2495ab11c98SPavel Emelyanov 		fq->q.last_in |= COMPLETE;
2501da177e4SLinus Torvalds 	}
2511da177e4SLinus Torvalds }
2521da177e4SLinus Torvalds 
253a11d206dSYOSHIFUJI Hideaki static void ip6_evictor(struct inet6_dev *idev)
2541da177e4SLinus Torvalds {
2551da177e4SLinus Torvalds 	struct frag_queue *fq;
2561da177e4SLinus Torvalds 	struct list_head *tmp;
2571da177e4SLinus Torvalds 	int work;
2581da177e4SLinus Torvalds 
2597eb95156SPavel Emelyanov 	work = atomic_read(&ip6_frags.mem) - sysctl_ip6frag_low_thresh;
2601da177e4SLinus Torvalds 	if (work <= 0)
2611da177e4SLinus Torvalds 		return;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 	while(work > 0) {
2647eb95156SPavel Emelyanov 		read_lock(&ip6_frags.lock);
2657eb95156SPavel Emelyanov 		if (list_empty(&ip6_frags.lru_list)) {
2667eb95156SPavel Emelyanov 			read_unlock(&ip6_frags.lock);
2671da177e4SLinus Torvalds 			return;
2681da177e4SLinus Torvalds 		}
2697eb95156SPavel Emelyanov 		tmp = ip6_frags.lru_list.next;
2705ab11c98SPavel Emelyanov 		fq = list_entry(tmp, struct frag_queue, q.lru_list);
2715ab11c98SPavel Emelyanov 		atomic_inc(&fq->q.refcnt);
2727eb95156SPavel Emelyanov 		read_unlock(&ip6_frags.lock);
2731da177e4SLinus Torvalds 
2745ab11c98SPavel Emelyanov 		spin_lock(&fq->q.lock);
2755ab11c98SPavel Emelyanov 		if (!(fq->q.last_in&COMPLETE))
2761da177e4SLinus Torvalds 			fq_kill(fq);
2775ab11c98SPavel Emelyanov 		spin_unlock(&fq->q.lock);
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 		fq_put(fq, &work);
280a11d206dSYOSHIFUJI Hideaki 		IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
2811da177e4SLinus Torvalds 	}
2821da177e4SLinus Torvalds }
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds static void ip6_frag_expire(unsigned long data)
2851da177e4SLinus Torvalds {
2861da177e4SLinus Torvalds 	struct frag_queue *fq = (struct frag_queue *) data;
287a11d206dSYOSHIFUJI Hideaki 	struct net_device *dev = NULL;
2881da177e4SLinus Torvalds 
2895ab11c98SPavel Emelyanov 	spin_lock(&fq->q.lock);
2901da177e4SLinus Torvalds 
2915ab11c98SPavel Emelyanov 	if (fq->q.last_in & COMPLETE)
2921da177e4SLinus Torvalds 		goto out;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	fq_kill(fq);
2951da177e4SLinus Torvalds 
296881d966bSEric W. Biederman 	dev = dev_get_by_index(&init_net, fq->iif);
297a11d206dSYOSHIFUJI Hideaki 	if (!dev)
298a11d206dSYOSHIFUJI Hideaki 		goto out;
299a11d206dSYOSHIFUJI Hideaki 
300a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
301a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
302a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
303a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
3041da177e4SLinus Torvalds 
30578c784c4SIngo Oeser 	/* Don't send error if the first segment did not arrive. */
3065ab11c98SPavel Emelyanov 	if (!(fq->q.last_in&FIRST_IN) || !fq->q.fragments)
30778c784c4SIngo Oeser 		goto out;
30878c784c4SIngo Oeser 
3091da177e4SLinus Torvalds 	/*
3101da177e4SLinus Torvalds 	   But use as source device on which LAST ARRIVED
3111da177e4SLinus Torvalds 	   segment was received. And do not use fq->dev
3121da177e4SLinus Torvalds 	   pointer directly, device might already disappeared.
3131da177e4SLinus Torvalds 	 */
3145ab11c98SPavel Emelyanov 	fq->q.fragments->dev = dev;
3155ab11c98SPavel Emelyanov 	icmpv6_send(fq->q.fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0, dev);
3161da177e4SLinus Torvalds out:
317a11d206dSYOSHIFUJI Hideaki 	if (dev)
318a11d206dSYOSHIFUJI Hideaki 		dev_put(dev);
3195ab11c98SPavel Emelyanov 	spin_unlock(&fq->q.lock);
3201da177e4SLinus Torvalds 	fq_put(fq, NULL);
3211da177e4SLinus Torvalds }
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds /* Creation primitives. */
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 
326f6596f9dSZach Brown static struct frag_queue *ip6_frag_intern(struct frag_queue *fq_in)
3271da177e4SLinus Torvalds {
3281da177e4SLinus Torvalds 	struct frag_queue *fq;
329f6596f9dSZach Brown 	unsigned int hash;
330e7c8a41eSYasuyuki Kozakai #ifdef CONFIG_SMP
331e7c8a41eSYasuyuki Kozakai 	struct hlist_node *n;
332e7c8a41eSYasuyuki Kozakai #endif
3331da177e4SLinus Torvalds 
3347eb95156SPavel Emelyanov 	write_lock(&ip6_frags.lock);
335f6596f9dSZach Brown 	hash = ip6qhashfn(fq_in->id, &fq_in->saddr, &fq_in->daddr);
3361da177e4SLinus Torvalds #ifdef CONFIG_SMP
3377eb95156SPavel Emelyanov 	hlist_for_each_entry(fq, n, &ip6_frags.hash[hash], q.list) {
3381da177e4SLinus Torvalds 		if (fq->id == fq_in->id &&
3391da177e4SLinus Torvalds 		    ipv6_addr_equal(&fq_in->saddr, &fq->saddr) &&
3401da177e4SLinus Torvalds 		    ipv6_addr_equal(&fq_in->daddr, &fq->daddr)) {
3415ab11c98SPavel Emelyanov 			atomic_inc(&fq->q.refcnt);
3427eb95156SPavel Emelyanov 			write_unlock(&ip6_frags.lock);
3435ab11c98SPavel Emelyanov 			fq_in->q.last_in |= COMPLETE;
3441da177e4SLinus Torvalds 			fq_put(fq_in, NULL);
3451da177e4SLinus Torvalds 			return fq;
3461da177e4SLinus Torvalds 		}
3471da177e4SLinus Torvalds 	}
3481da177e4SLinus Torvalds #endif
3491da177e4SLinus Torvalds 	fq = fq_in;
3501da177e4SLinus Torvalds 
3515ab11c98SPavel Emelyanov 	if (!mod_timer(&fq->q.timer, jiffies + sysctl_ip6frag_time))
3525ab11c98SPavel Emelyanov 		atomic_inc(&fq->q.refcnt);
3531da177e4SLinus Torvalds 
3545ab11c98SPavel Emelyanov 	atomic_inc(&fq->q.refcnt);
3557eb95156SPavel Emelyanov 	hlist_add_head(&fq->q.list, &ip6_frags.hash[hash]);
3565ab11c98SPavel Emelyanov 	INIT_LIST_HEAD(&fq->q.lru_list);
3577eb95156SPavel Emelyanov 	list_add_tail(&fq->q.lru_list, &ip6_frags.lru_list);
3587eb95156SPavel Emelyanov 	ip6_frags.nqueues++;
3597eb95156SPavel Emelyanov 	write_unlock(&ip6_frags.lock);
3601da177e4SLinus Torvalds 	return fq;
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds static struct frag_queue *
365e69a4adcSAl Viro ip6_frag_create(__be32 id, struct in6_addr *src, struct in6_addr *dst,
366a11d206dSYOSHIFUJI Hideaki 		struct inet6_dev *idev)
3671da177e4SLinus Torvalds {
3681da177e4SLinus Torvalds 	struct frag_queue *fq;
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	if ((fq = frag_alloc_queue()) == NULL)
3711da177e4SLinus Torvalds 		goto oom;
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 
3775ab11c98SPavel Emelyanov 	init_timer(&fq->q.timer);
3785ab11c98SPavel Emelyanov 	fq->q.timer.function = ip6_frag_expire;
3795ab11c98SPavel Emelyanov 	fq->q.timer.data = (long) fq;
3805ab11c98SPavel Emelyanov 	spin_lock_init(&fq->q.lock);
3815ab11c98SPavel Emelyanov 	atomic_set(&fq->q.refcnt, 1);
3821da177e4SLinus Torvalds 
383f6596f9dSZach Brown 	return ip6_frag_intern(fq);
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds oom:
386a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
3871da177e4SLinus Torvalds 	return NULL;
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds static __inline__ struct frag_queue *
391e69a4adcSAl Viro fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst,
392a11d206dSYOSHIFUJI Hideaki 	struct inet6_dev *idev)
3931da177e4SLinus Torvalds {
3941da177e4SLinus Torvalds 	struct frag_queue *fq;
395e7c8a41eSYasuyuki Kozakai 	struct hlist_node *n;
396f6596f9dSZach Brown 	unsigned int hash;
3971da177e4SLinus Torvalds 
3987eb95156SPavel Emelyanov 	read_lock(&ip6_frags.lock);
399f6596f9dSZach Brown 	hash = ip6qhashfn(id, src, dst);
4007eb95156SPavel Emelyanov 	hlist_for_each_entry(fq, n, &ip6_frags.hash[hash], q.list) {
4011da177e4SLinus Torvalds 		if (fq->id == id &&
4021da177e4SLinus Torvalds 		    ipv6_addr_equal(src, &fq->saddr) &&
4031da177e4SLinus Torvalds 		    ipv6_addr_equal(dst, &fq->daddr)) {
4045ab11c98SPavel Emelyanov 			atomic_inc(&fq->q.refcnt);
4057eb95156SPavel Emelyanov 			read_unlock(&ip6_frags.lock);
4061da177e4SLinus Torvalds 			return fq;
4071da177e4SLinus Torvalds 		}
4081da177e4SLinus Torvalds 	}
4097eb95156SPavel Emelyanov 	read_unlock(&ip6_frags.lock);
4101da177e4SLinus Torvalds 
411a11d206dSYOSHIFUJI Hideaki 	return ip6_frag_create(id, src, dst, idev);
4121da177e4SLinus Torvalds }
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 
415f61944efSHerbert Xu static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
4161da177e4SLinus Torvalds 			   struct frag_hdr *fhdr, int nhoff)
4171da177e4SLinus Torvalds {
4181da177e4SLinus Torvalds 	struct sk_buff *prev, *next;
419f61944efSHerbert Xu 	struct net_device *dev;
4201da177e4SLinus Torvalds 	int offset, end;
4211da177e4SLinus Torvalds 
4225ab11c98SPavel Emelyanov 	if (fq->q.last_in & COMPLETE)
4231da177e4SLinus Torvalds 		goto err;
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	offset = ntohs(fhdr->frag_off) & ~0x7;
4260660e03fSArnaldo Carvalho de Melo 	end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
4270660e03fSArnaldo Carvalho de Melo 			((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	if ((unsigned int)end > IPV6_MAXPLEN) {
430a11d206dSYOSHIFUJI Hideaki 		IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
431a11d206dSYOSHIFUJI Hideaki 				 IPSTATS_MIB_INHDRERRORS);
432d56f90a7SArnaldo Carvalho de Melo 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
433d56f90a7SArnaldo Carvalho de Melo 				  ((u8 *)&fhdr->frag_off -
434d56f90a7SArnaldo Carvalho de Melo 				   skb_network_header(skb)));
435f61944efSHerbert Xu 		return -1;
4361da177e4SLinus Torvalds 	}
4371da177e4SLinus Torvalds 
438d56f90a7SArnaldo Carvalho de Melo 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
439d56f90a7SArnaldo Carvalho de Melo 		const unsigned char *nh = skb_network_header(skb);
4401da177e4SLinus Torvalds 		skb->csum = csum_sub(skb->csum,
441d56f90a7SArnaldo Carvalho de Melo 				     csum_partial(nh, (u8 *)(fhdr + 1) - nh,
442d56f90a7SArnaldo Carvalho de Melo 						  0));
443d56f90a7SArnaldo Carvalho de Melo 	}
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 		 */
4505ab11c98SPavel Emelyanov 		if (end < fq->q.len ||
4515ab11c98SPavel Emelyanov 		    ((fq->q.last_in & LAST_IN) && end != fq->q.len))
4521da177e4SLinus Torvalds 			goto err;
4535ab11c98SPavel Emelyanov 		fq->q.last_in |= LAST_IN;
4545ab11c98SPavel Emelyanov 		fq->q.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 			 */
463a11d206dSYOSHIFUJI Hideaki 			IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
464a11d206dSYOSHIFUJI Hideaki 					 IPSTATS_MIB_INHDRERRORS);
4651da177e4SLinus Torvalds 			icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
4661da177e4SLinus Torvalds 					  offsetof(struct ipv6hdr, payload_len));
467f61944efSHerbert Xu 			return -1;
4681da177e4SLinus Torvalds 		}
4695ab11c98SPavel Emelyanov 		if (end > fq->q.len) {
4701da177e4SLinus Torvalds 			/* Some bits beyond end -> corruption. */
4715ab11c98SPavel Emelyanov 			if (fq->q.last_in & LAST_IN)
4721da177e4SLinus Torvalds 				goto err;
4735ab11c98SPavel Emelyanov 			fq->q.len = end;
4741da177e4SLinus Torvalds 		}
4751da177e4SLinus Torvalds 	}
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	if (end == offset)
4781da177e4SLinus Torvalds 		goto err;
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	/* Point into the IP datagram 'data' part. */
4811da177e4SLinus Torvalds 	if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
4821da177e4SLinus Torvalds 		goto err;
48342ca89c1SStephen Hemminger 
48442ca89c1SStephen Hemminger 	if (pskb_trim_rcsum(skb, end - offset))
4851da177e4SLinus Torvalds 		goto err;
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	/* Find out which fragments are in front and at the back of us
4881da177e4SLinus Torvalds 	 * in the chain of fragments so far.  We must know where to put
4891da177e4SLinus Torvalds 	 * this fragment, right?
4901da177e4SLinus Torvalds 	 */
4911da177e4SLinus Torvalds 	prev = NULL;
4925ab11c98SPavel Emelyanov 	for(next = fq->q.fragments; next != NULL; next = next->next) {
4931da177e4SLinus Torvalds 		if (FRAG6_CB(next)->offset >= offset)
4941da177e4SLinus Torvalds 			break;	/* bingo! */
4951da177e4SLinus Torvalds 		prev = next;
4961da177e4SLinus Torvalds 	}
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 	/* We found where to put this one.  Check for overlap with
4991da177e4SLinus Torvalds 	 * preceding fragment, and, if needed, align things so that
5001da177e4SLinus Torvalds 	 * any overlaps are eliminated.
5011da177e4SLinus Torvalds 	 */
5021da177e4SLinus Torvalds 	if (prev) {
5031da177e4SLinus Torvalds 		int i = (FRAG6_CB(prev)->offset + prev->len) - offset;
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 		if (i > 0) {
5061da177e4SLinus Torvalds 			offset += i;
5071da177e4SLinus Torvalds 			if (end <= offset)
5081da177e4SLinus Torvalds 				goto err;
5091da177e4SLinus Torvalds 			if (!pskb_pull(skb, i))
5101da177e4SLinus Torvalds 				goto err;
5111da177e4SLinus Torvalds 			if (skb->ip_summed != CHECKSUM_UNNECESSARY)
5121da177e4SLinus Torvalds 				skb->ip_summed = CHECKSUM_NONE;
5131da177e4SLinus Torvalds 		}
5141da177e4SLinus Torvalds 	}
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	/* Look for overlap with succeeding segments.
5171da177e4SLinus Torvalds 	 * If we can merge fragments, do it.
5181da177e4SLinus Torvalds 	 */
5191da177e4SLinus Torvalds 	while (next && FRAG6_CB(next)->offset < end) {
5201da177e4SLinus Torvalds 		int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 		if (i < next->len) {
5231da177e4SLinus Torvalds 			/* Eat head of the next overlapped fragment
5241da177e4SLinus Torvalds 			 * and leave the loop. The next ones cannot overlap.
5251da177e4SLinus Torvalds 			 */
5261da177e4SLinus Torvalds 			if (!pskb_pull(next, i))
5271da177e4SLinus Torvalds 				goto err;
5281da177e4SLinus Torvalds 			FRAG6_CB(next)->offset += i;	/* next fragment */
5295ab11c98SPavel Emelyanov 			fq->q.meat -= i;
5301da177e4SLinus Torvalds 			if (next->ip_summed != CHECKSUM_UNNECESSARY)
5311da177e4SLinus Torvalds 				next->ip_summed = CHECKSUM_NONE;
5321da177e4SLinus Torvalds 			break;
5331da177e4SLinus Torvalds 		} else {
5341da177e4SLinus Torvalds 			struct sk_buff *free_it = next;
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 			/* Old fragment is completely overridden with
5371da177e4SLinus Torvalds 			 * new one drop it.
5381da177e4SLinus Torvalds 			 */
5391da177e4SLinus Torvalds 			next = next->next;
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 			if (prev)
5421da177e4SLinus Torvalds 				prev->next = next;
5431da177e4SLinus Torvalds 			else
5445ab11c98SPavel Emelyanov 				fq->q.fragments = next;
5451da177e4SLinus Torvalds 
5465ab11c98SPavel Emelyanov 			fq->q.meat -= free_it->len;
5471da177e4SLinus Torvalds 			frag_kfree_skb(free_it, NULL);
5481da177e4SLinus Torvalds 		}
5491da177e4SLinus Torvalds 	}
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	FRAG6_CB(skb)->offset = offset;
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	/* Insert this fragment in the chain of fragments. */
5541da177e4SLinus Torvalds 	skb->next = next;
5551da177e4SLinus Torvalds 	if (prev)
5561da177e4SLinus Torvalds 		prev->next = skb;
5571da177e4SLinus Torvalds 	else
5585ab11c98SPavel Emelyanov 		fq->q.fragments = skb;
5591da177e4SLinus Torvalds 
560f61944efSHerbert Xu 	dev = skb->dev;
561f61944efSHerbert Xu 	if (dev) {
562f61944efSHerbert Xu 		fq->iif = dev->ifindex;
5631da177e4SLinus Torvalds 		skb->dev = NULL;
564f61944efSHerbert Xu 	}
5655ab11c98SPavel Emelyanov 	fq->q.stamp = skb->tstamp;
5665ab11c98SPavel Emelyanov 	fq->q.meat += skb->len;
5677eb95156SPavel Emelyanov 	atomic_add(skb->truesize, &ip6_frags.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;
5745ab11c98SPavel Emelyanov 		fq->q.last_in |= FIRST_IN;
5751da177e4SLinus Torvalds 	}
576f61944efSHerbert Xu 
5775ab11c98SPavel Emelyanov 	if (fq->q.last_in == (FIRST_IN | LAST_IN) && fq->q.meat == fq->q.len)
578f61944efSHerbert Xu 		return ip6_frag_reasm(fq, prev, dev);
579f61944efSHerbert Xu 
5807eb95156SPavel Emelyanov 	write_lock(&ip6_frags.lock);
5817eb95156SPavel Emelyanov 	list_move_tail(&fq->q.lru_list, &ip6_frags.lru_list);
5827eb95156SPavel Emelyanov 	write_unlock(&ip6_frags.lock);
583f61944efSHerbert Xu 	return -1;
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds err:
586a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
5871da177e4SLinus Torvalds 	kfree_skb(skb);
588f61944efSHerbert Xu 	return -1;
5891da177e4SLinus Torvalds }
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds /*
5921da177e4SLinus Torvalds  *	Check if this packet is complete.
5931da177e4SLinus Torvalds  *	Returns NULL on failure by any reason, and pointer
5941da177e4SLinus Torvalds  *	to current nexthdr field in reassembled frame.
5951da177e4SLinus Torvalds  *
5961da177e4SLinus Torvalds  *	It is called with locked fq, and caller must check that
5971da177e4SLinus Torvalds  *	queue is eligible for reassembly i.e. it is not COMPLETE,
5981da177e4SLinus Torvalds  *	the last and the first frames arrived and all the bits are here.
5991da177e4SLinus Torvalds  */
600f61944efSHerbert Xu static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
6011da177e4SLinus Torvalds 			  struct net_device *dev)
6021da177e4SLinus Torvalds {
6035ab11c98SPavel Emelyanov 	struct sk_buff *fp, *head = fq->q.fragments;
6041da177e4SLinus Torvalds 	int    payload_len;
6051da177e4SLinus Torvalds 	unsigned int nhoff;
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	fq_kill(fq);
6081da177e4SLinus Torvalds 
609f61944efSHerbert Xu 	/* Make the one we just received the head. */
610f61944efSHerbert Xu 	if (prev) {
611f61944efSHerbert Xu 		head = prev->next;
612f61944efSHerbert Xu 		fp = skb_clone(head, GFP_ATOMIC);
613f61944efSHerbert Xu 
614f61944efSHerbert Xu 		if (!fp)
615f61944efSHerbert Xu 			goto out_oom;
616f61944efSHerbert Xu 
617f61944efSHerbert Xu 		fp->next = head->next;
618f61944efSHerbert Xu 		prev->next = fp;
619f61944efSHerbert Xu 
6205ab11c98SPavel Emelyanov 		skb_morph(head, fq->q.fragments);
6215ab11c98SPavel Emelyanov 		head->next = fq->q.fragments->next;
622f61944efSHerbert Xu 
6235ab11c98SPavel Emelyanov 		kfree_skb(fq->q.fragments);
6245ab11c98SPavel Emelyanov 		fq->q.fragments = head;
625f61944efSHerbert Xu 	}
626f61944efSHerbert Xu 
6271da177e4SLinus Torvalds 	BUG_TRAP(head != NULL);
6281da177e4SLinus Torvalds 	BUG_TRAP(FRAG6_CB(head)->offset == 0);
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds 	/* Unfragmented part is taken from the first segment. */
631d56f90a7SArnaldo Carvalho de Melo 	payload_len = ((head->data - skb_network_header(head)) -
6325ab11c98SPavel Emelyanov 		       sizeof(struct ipv6hdr) + fq->q.len -
633d56f90a7SArnaldo Carvalho de Melo 		       sizeof(struct frag_hdr));
6341da177e4SLinus Torvalds 	if (payload_len > IPV6_MAXPLEN)
6351da177e4SLinus Torvalds 		goto out_oversize;
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	/* Head of list must not be cloned. */
6381da177e4SLinus Torvalds 	if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
6391da177e4SLinus Torvalds 		goto out_oom;
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	/* If the first fragment is fragmented itself, we split
6421da177e4SLinus Torvalds 	 * it to two chunks: the first with data and paged part
6431da177e4SLinus Torvalds 	 * and the second, holding only fragments. */
6441da177e4SLinus Torvalds 	if (skb_shinfo(head)->frag_list) {
6451da177e4SLinus Torvalds 		struct sk_buff *clone;
6461da177e4SLinus Torvalds 		int i, plen = 0;
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 		if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
6491da177e4SLinus Torvalds 			goto out_oom;
6501da177e4SLinus Torvalds 		clone->next = head->next;
6511da177e4SLinus Torvalds 		head->next = clone;
6521da177e4SLinus Torvalds 		skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
6531da177e4SLinus Torvalds 		skb_shinfo(head)->frag_list = NULL;
6541da177e4SLinus Torvalds 		for (i=0; i<skb_shinfo(head)->nr_frags; i++)
6551da177e4SLinus Torvalds 			plen += skb_shinfo(head)->frags[i].size;
6561da177e4SLinus Torvalds 		clone->len = clone->data_len = head->data_len - plen;
6571da177e4SLinus Torvalds 		head->data_len -= clone->len;
6581da177e4SLinus Torvalds 		head->len -= clone->len;
6591da177e4SLinus Torvalds 		clone->csum = 0;
6601da177e4SLinus Torvalds 		clone->ip_summed = head->ip_summed;
6617eb95156SPavel Emelyanov 		atomic_add(clone->truesize, &ip6_frags.mem);
6621da177e4SLinus Torvalds 	}
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	/* We have to remove fragment header from datagram and to relocate
6651da177e4SLinus Torvalds 	 * header in order to calculate ICV correctly. */
6661da177e4SLinus Torvalds 	nhoff = fq->nhoffset;
667b0e380b1SArnaldo Carvalho de Melo 	skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
6681da177e4SLinus Torvalds 	memmove(head->head + sizeof(struct frag_hdr), head->head,
6691da177e4SLinus Torvalds 		(head->data - head->head) - sizeof(struct frag_hdr));
670b0e380b1SArnaldo Carvalho de Melo 	head->mac_header += sizeof(struct frag_hdr);
671b0e380b1SArnaldo Carvalho de Melo 	head->network_header += sizeof(struct frag_hdr);
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 	skb_shinfo(head)->frag_list = head->next;
674badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(head);
675d56f90a7SArnaldo Carvalho de Melo 	skb_push(head, head->data - skb_network_header(head));
6767eb95156SPavel Emelyanov 	atomic_sub(head->truesize, &ip6_frags.mem);
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 	for (fp=head->next; fp; fp = fp->next) {
6791da177e4SLinus Torvalds 		head->data_len += fp->len;
6801da177e4SLinus Torvalds 		head->len += fp->len;
6811da177e4SLinus Torvalds 		if (head->ip_summed != fp->ip_summed)
6821da177e4SLinus Torvalds 			head->ip_summed = CHECKSUM_NONE;
68384fa7933SPatrick McHardy 		else if (head->ip_summed == CHECKSUM_COMPLETE)
6841da177e4SLinus Torvalds 			head->csum = csum_add(head->csum, fp->csum);
6851da177e4SLinus Torvalds 		head->truesize += fp->truesize;
6867eb95156SPavel Emelyanov 		atomic_sub(fp->truesize, &ip6_frags.mem);
6871da177e4SLinus Torvalds 	}
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 	head->next = NULL;
6901da177e4SLinus Torvalds 	head->dev = dev;
6915ab11c98SPavel Emelyanov 	head->tstamp = fq->q.stamp;
6920660e03fSArnaldo Carvalho de Melo 	ipv6_hdr(head)->payload_len = htons(payload_len);
693951dbc8aSPatrick McHardy 	IP6CB(head)->nhoff = nhoff;
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	/* Yes, and fold redundant checksum back. 8) */
69684fa7933SPatrick McHardy 	if (head->ip_summed == CHECKSUM_COMPLETE)
697d56f90a7SArnaldo Carvalho de Melo 		head->csum = csum_partial(skb_network_header(head),
698cfe1fc77SArnaldo Carvalho de Melo 					  skb_network_header_len(head),
699d56f90a7SArnaldo Carvalho de Melo 					  head->csum);
7001da177e4SLinus Torvalds 
701a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
702a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
703a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
7045ab11c98SPavel Emelyanov 	fq->q.fragments = NULL;
7051da177e4SLinus Torvalds 	return 1;
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds out_oversize:
7081da177e4SLinus Torvalds 	if (net_ratelimit())
7091da177e4SLinus Torvalds 		printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len);
7101da177e4SLinus Torvalds 	goto out_fail;
7111da177e4SLinus Torvalds out_oom:
7121da177e4SLinus Torvalds 	if (net_ratelimit())
7131da177e4SLinus Torvalds 		printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n");
7141da177e4SLinus Torvalds out_fail:
715a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
716a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
717a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
7181da177e4SLinus Torvalds 	return -1;
7191da177e4SLinus Torvalds }
7201da177e4SLinus Torvalds 
721951dbc8aSPatrick McHardy static int ipv6_frag_rcv(struct sk_buff **skbp)
7221da177e4SLinus Torvalds {
7231da177e4SLinus Torvalds 	struct sk_buff *skb = *skbp;
7241da177e4SLinus Torvalds 	struct frag_hdr *fhdr;
7251da177e4SLinus Torvalds 	struct frag_queue *fq;
7260660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *hdr = ipv6_hdr(skb);
7271da177e4SLinus Torvalds 
728a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMREQDS);
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 	/* Jumbo payload inhibits frag. header */
7311da177e4SLinus Torvalds 	if (hdr->payload_len==0) {
732a11d206dSYOSHIFUJI Hideaki 		IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
733cfe1fc77SArnaldo Carvalho de Melo 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
734cfe1fc77SArnaldo Carvalho de Melo 				  skb_network_header_len(skb));
7351da177e4SLinus Torvalds 		return -1;
7361da177e4SLinus Torvalds 	}
737ea2ae17dSArnaldo Carvalho de Melo 	if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
738ea2ae17dSArnaldo Carvalho de Melo 				 sizeof(struct frag_hdr)))) {
739a11d206dSYOSHIFUJI Hideaki 		IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
740cfe1fc77SArnaldo Carvalho de Melo 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
741cfe1fc77SArnaldo Carvalho de Melo 				  skb_network_header_len(skb));
7421da177e4SLinus Torvalds 		return -1;
7431da177e4SLinus Torvalds 	}
7441da177e4SLinus Torvalds 
7450660e03fSArnaldo Carvalho de Melo 	hdr = ipv6_hdr(skb);
7469c70220bSArnaldo Carvalho de Melo 	fhdr = (struct frag_hdr *)skb_transport_header(skb);
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(0xFFF9))) {
7491da177e4SLinus Torvalds 		/* It is not a fragmented frame */
750b0e380b1SArnaldo Carvalho de Melo 		skb->transport_header += sizeof(struct frag_hdr);
751a11d206dSYOSHIFUJI Hideaki 		IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMOKS);
7521da177e4SLinus Torvalds 
753d56f90a7SArnaldo Carvalho de Melo 		IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
7541da177e4SLinus Torvalds 		return 1;
7551da177e4SLinus Torvalds 	}
7561da177e4SLinus Torvalds 
7577eb95156SPavel Emelyanov 	if (atomic_read(&ip6_frags.mem) > sysctl_ip6frag_high_thresh)
758a11d206dSYOSHIFUJI Hideaki 		ip6_evictor(ip6_dst_idev(skb->dst));
7591da177e4SLinus Torvalds 
760a11d206dSYOSHIFUJI Hideaki 	if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr,
761a11d206dSYOSHIFUJI Hideaki 			  ip6_dst_idev(skb->dst))) != NULL) {
762f61944efSHerbert Xu 		int ret;
7631da177e4SLinus Torvalds 
7645ab11c98SPavel Emelyanov 		spin_lock(&fq->q.lock);
7651da177e4SLinus Torvalds 
766f61944efSHerbert Xu 		ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
7671da177e4SLinus Torvalds 
7685ab11c98SPavel Emelyanov 		spin_unlock(&fq->q.lock);
7691da177e4SLinus Torvalds 		fq_put(fq, NULL);
7701da177e4SLinus Torvalds 		return ret;
7711da177e4SLinus Torvalds 	}
7721da177e4SLinus Torvalds 
773a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
7741da177e4SLinus Torvalds 	kfree_skb(skb);
7751da177e4SLinus Torvalds 	return -1;
7761da177e4SLinus Torvalds }
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds static struct inet6_protocol frag_protocol =
7791da177e4SLinus Torvalds {
7801da177e4SLinus Torvalds 	.handler	=	ipv6_frag_rcv,
7811da177e4SLinus Torvalds 	.flags		=	INET6_PROTO_NOPOLICY,
7821da177e4SLinus Torvalds };
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds void __init ipv6_frag_init(void)
7851da177e4SLinus Torvalds {
7861da177e4SLinus Torvalds 	if (inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT) < 0)
7871da177e4SLinus Torvalds 		printk(KERN_ERR "ipv6_frag_init: Could not register protocol\n");
7881da177e4SLinus Torvalds 
7897eb95156SPavel Emelyanov 	init_timer(&ip6_frags.secret_timer);
7907eb95156SPavel Emelyanov 	ip6_frags.secret_timer.function = ip6_frag_secret_rebuild;
7917eb95156SPavel Emelyanov 	ip6_frags.secret_timer.expires = jiffies + sysctl_ip6frag_secret_interval;
7927eb95156SPavel Emelyanov 	add_timer(&ip6_frags.secret_timer);
7931da177e4SLinus Torvalds 
7947eb95156SPavel Emelyanov 	inet_frags_init(&ip6_frags);
7951da177e4SLinus Torvalds }
796