xref: /openbmc/linux/net/ipv6/reassembly.c (revision f61944ef)
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>
571da177e4SLinus Torvalds 
58ab32ea5dSBrian Haley int sysctl_ip6frag_high_thresh __read_mostly = 256*1024;
59ab32ea5dSBrian Haley int sysctl_ip6frag_low_thresh __read_mostly = 192*1024;
601da177e4SLinus Torvalds 
61ab32ea5dSBrian Haley int sysctl_ip6frag_time __read_mostly = IPV6_FRAG_TIMEOUT;
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds struct ip6frag_skb_cb
641da177e4SLinus Torvalds {
651da177e4SLinus Torvalds 	struct inet6_skb_parm	h;
661da177e4SLinus Torvalds 	int			offset;
671da177e4SLinus Torvalds };
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #define FRAG6_CB(skb)	((struct ip6frag_skb_cb*)((skb)->cb))
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds /*
731da177e4SLinus Torvalds  *	Equivalent of ipv4 struct ipq
741da177e4SLinus Torvalds  */
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds struct frag_queue
771da177e4SLinus Torvalds {
78e7c8a41eSYasuyuki Kozakai 	struct hlist_node	list;
791da177e4SLinus Torvalds 	struct list_head lru_list;		/* lru list member	*/
801da177e4SLinus Torvalds 
81e69a4adcSAl Viro 	__be32			id;		/* fragment id		*/
821da177e4SLinus Torvalds 	struct in6_addr		saddr;
831da177e4SLinus Torvalds 	struct in6_addr		daddr;
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds 	spinlock_t		lock;
861da177e4SLinus Torvalds 	atomic_t		refcnt;
871da177e4SLinus Torvalds 	struct timer_list	timer;		/* expire timer		*/
881da177e4SLinus Torvalds 	struct sk_buff		*fragments;
891da177e4SLinus Torvalds 	int			len;
901da177e4SLinus Torvalds 	int			meat;
911da177e4SLinus Torvalds 	int			iif;
92b7aa0bf7SEric Dumazet 	ktime_t			stamp;
931da177e4SLinus Torvalds 	unsigned int		csum;
941da177e4SLinus Torvalds 	__u8			last_in;	/* has first/last segment arrived? */
951da177e4SLinus Torvalds #define COMPLETE		4
961da177e4SLinus Torvalds #define FIRST_IN		2
971da177e4SLinus Torvalds #define LAST_IN			1
981da177e4SLinus Torvalds 	__u16			nhoffset;
991da177e4SLinus Torvalds };
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds /* Hash table. */
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds #define IP6Q_HASHSZ	64
1041da177e4SLinus Torvalds 
105e7c8a41eSYasuyuki Kozakai static struct hlist_head ip6_frag_hash[IP6Q_HASHSZ];
1061da177e4SLinus Torvalds static DEFINE_RWLOCK(ip6_frag_lock);
1071da177e4SLinus Torvalds static u32 ip6_frag_hash_rnd;
1081da177e4SLinus Torvalds static LIST_HEAD(ip6_frag_lru_list);
1091da177e4SLinus Torvalds int ip6_frag_nqueues = 0;
1101da177e4SLinus Torvalds 
111f61944efSHerbert Xu static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
112f61944efSHerbert Xu 			  struct net_device *dev);
113f61944efSHerbert Xu 
1141da177e4SLinus Torvalds static __inline__ void __fq_unlink(struct frag_queue *fq)
1151da177e4SLinus Torvalds {
116e7c8a41eSYasuyuki Kozakai 	hlist_del(&fq->list);
1171da177e4SLinus Torvalds 	list_del(&fq->lru_list);
1181da177e4SLinus Torvalds 	ip6_frag_nqueues--;
1191da177e4SLinus Torvalds }
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds static __inline__ void fq_unlink(struct frag_queue *fq)
1221da177e4SLinus Torvalds {
1231da177e4SLinus Torvalds 	write_lock(&ip6_frag_lock);
1241da177e4SLinus Torvalds 	__fq_unlink(fq);
1251da177e4SLinus Torvalds 	write_unlock(&ip6_frag_lock);
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds 
128f6596f9dSZach Brown /*
129f6596f9dSZach Brown  * callers should be careful not to use the hash value outside the ipfrag_lock
130f6596f9dSZach Brown  * as doing so could race with ipfrag_hash_rnd being recalculated.
131f6596f9dSZach Brown  */
132e69a4adcSAl Viro static unsigned int ip6qhashfn(__be32 id, struct in6_addr *saddr,
1331da177e4SLinus Torvalds 			       struct in6_addr *daddr)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	u32 a, b, c;
1361da177e4SLinus Torvalds 
137e69a4adcSAl Viro 	a = (__force u32)saddr->s6_addr32[0];
138e69a4adcSAl Viro 	b = (__force u32)saddr->s6_addr32[1];
139e69a4adcSAl Viro 	c = (__force u32)saddr->s6_addr32[2];
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 	a += JHASH_GOLDEN_RATIO;
1421da177e4SLinus Torvalds 	b += JHASH_GOLDEN_RATIO;
1431da177e4SLinus Torvalds 	c += ip6_frag_hash_rnd;
1441da177e4SLinus Torvalds 	__jhash_mix(a, b, c);
1451da177e4SLinus Torvalds 
146e69a4adcSAl Viro 	a += (__force u32)saddr->s6_addr32[3];
147e69a4adcSAl Viro 	b += (__force u32)daddr->s6_addr32[0];
148e69a4adcSAl Viro 	c += (__force u32)daddr->s6_addr32[1];
1491da177e4SLinus Torvalds 	__jhash_mix(a, b, c);
1501da177e4SLinus Torvalds 
151e69a4adcSAl Viro 	a += (__force u32)daddr->s6_addr32[2];
152e69a4adcSAl Viro 	b += (__force u32)daddr->s6_addr32[3];
153e69a4adcSAl Viro 	c += (__force u32)id;
1541da177e4SLinus Torvalds 	__jhash_mix(a, b, c);
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	return c & (IP6Q_HASHSZ - 1);
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds static struct timer_list ip6_frag_secret_timer;
160ab32ea5dSBrian Haley int sysctl_ip6frag_secret_interval __read_mostly = 10 * 60 * HZ;
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds static void ip6_frag_secret_rebuild(unsigned long dummy)
1631da177e4SLinus Torvalds {
1641da177e4SLinus Torvalds 	unsigned long now = jiffies;
1651da177e4SLinus Torvalds 	int i;
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	write_lock(&ip6_frag_lock);
1681da177e4SLinus Torvalds 	get_random_bytes(&ip6_frag_hash_rnd, sizeof(u32));
1691da177e4SLinus Torvalds 	for (i = 0; i < IP6Q_HASHSZ; i++) {
1701da177e4SLinus Torvalds 		struct frag_queue *q;
171e7c8a41eSYasuyuki Kozakai 		struct hlist_node *p, *n;
1721da177e4SLinus Torvalds 
173e7c8a41eSYasuyuki Kozakai 		hlist_for_each_entry_safe(q, p, n, &ip6_frag_hash[i], list) {
1741da177e4SLinus Torvalds 			unsigned int hval = ip6qhashfn(q->id,
1751da177e4SLinus Torvalds 						       &q->saddr,
1761da177e4SLinus Torvalds 						       &q->daddr);
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 			if (hval != i) {
179e7c8a41eSYasuyuki Kozakai 				hlist_del(&q->list);
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 				/* Relink to new hash chain. */
182e7c8a41eSYasuyuki Kozakai 				hlist_add_head(&q->list,
183e7c8a41eSYasuyuki Kozakai 					       &ip6_frag_hash[hval]);
1841da177e4SLinus Torvalds 
185e7c8a41eSYasuyuki Kozakai 			}
1861da177e4SLinus Torvalds 		}
1871da177e4SLinus Torvalds 	}
1881da177e4SLinus Torvalds 	write_unlock(&ip6_frag_lock);
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 	mod_timer(&ip6_frag_secret_timer, now + sysctl_ip6frag_secret_interval);
1911da177e4SLinus Torvalds }
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds atomic_t ip6_frag_mem = ATOMIC_INIT(0);
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds /* Memory Tracking Functions. */
1961da177e4SLinus Torvalds static inline void frag_kfree_skb(struct sk_buff *skb, int *work)
1971da177e4SLinus Torvalds {
1981da177e4SLinus Torvalds 	if (work)
1991da177e4SLinus Torvalds 		*work -= skb->truesize;
2001da177e4SLinus Torvalds 	atomic_sub(skb->truesize, &ip6_frag_mem);
2011da177e4SLinus Torvalds 	kfree_skb(skb);
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds static inline void frag_free_queue(struct frag_queue *fq, int *work)
2051da177e4SLinus Torvalds {
2061da177e4SLinus Torvalds 	if (work)
2071da177e4SLinus Torvalds 		*work -= sizeof(struct frag_queue);
2081da177e4SLinus Torvalds 	atomic_sub(sizeof(struct frag_queue), &ip6_frag_mem);
2091da177e4SLinus Torvalds 	kfree(fq);
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds static inline struct frag_queue *frag_alloc_queue(void)
2131da177e4SLinus Torvalds {
21478c784c4SIngo Oeser 	struct frag_queue *fq = kzalloc(sizeof(struct frag_queue), GFP_ATOMIC);
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	if(!fq)
2171da177e4SLinus Torvalds 		return NULL;
2181da177e4SLinus Torvalds 	atomic_add(sizeof(struct frag_queue), &ip6_frag_mem);
2191da177e4SLinus Torvalds 	return fq;
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds /* Destruction primitives. */
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds /* Complete destruction of fq. */
2251da177e4SLinus Torvalds static void ip6_frag_destroy(struct frag_queue *fq, int *work)
2261da177e4SLinus Torvalds {
2271da177e4SLinus Torvalds 	struct sk_buff *fp;
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	BUG_TRAP(fq->last_in&COMPLETE);
2301da177e4SLinus Torvalds 	BUG_TRAP(del_timer(&fq->timer) == 0);
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	/* Release all fragment data. */
2331da177e4SLinus Torvalds 	fp = fq->fragments;
2341da177e4SLinus Torvalds 	while (fp) {
2351da177e4SLinus Torvalds 		struct sk_buff *xp = fp->next;
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 		frag_kfree_skb(fp, work);
2381da177e4SLinus Torvalds 		fp = xp;
2391da177e4SLinus Torvalds 	}
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	frag_free_queue(fq, work);
2421da177e4SLinus Torvalds }
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds static __inline__ void fq_put(struct frag_queue *fq, int *work)
2451da177e4SLinus Torvalds {
2461da177e4SLinus Torvalds 	if (atomic_dec_and_test(&fq->refcnt))
2471da177e4SLinus Torvalds 		ip6_frag_destroy(fq, work);
2481da177e4SLinus Torvalds }
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds /* Kill fq entry. It is not destroyed immediately,
2511da177e4SLinus Torvalds  * because caller (and someone more) holds reference count.
2521da177e4SLinus Torvalds  */
2531da177e4SLinus Torvalds static __inline__ void fq_kill(struct frag_queue *fq)
2541da177e4SLinus Torvalds {
2551da177e4SLinus Torvalds 	if (del_timer(&fq->timer))
2561da177e4SLinus Torvalds 		atomic_dec(&fq->refcnt);
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	if (!(fq->last_in & COMPLETE)) {
2591da177e4SLinus Torvalds 		fq_unlink(fq);
2601da177e4SLinus Torvalds 		atomic_dec(&fq->refcnt);
2611da177e4SLinus Torvalds 		fq->last_in |= COMPLETE;
2621da177e4SLinus Torvalds 	}
2631da177e4SLinus Torvalds }
2641da177e4SLinus Torvalds 
265a11d206dSYOSHIFUJI Hideaki static void ip6_evictor(struct inet6_dev *idev)
2661da177e4SLinus Torvalds {
2671da177e4SLinus Torvalds 	struct frag_queue *fq;
2681da177e4SLinus Torvalds 	struct list_head *tmp;
2691da177e4SLinus Torvalds 	int work;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	work = atomic_read(&ip6_frag_mem) - sysctl_ip6frag_low_thresh;
2721da177e4SLinus Torvalds 	if (work <= 0)
2731da177e4SLinus Torvalds 		return;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	while(work > 0) {
2761da177e4SLinus Torvalds 		read_lock(&ip6_frag_lock);
2771da177e4SLinus Torvalds 		if (list_empty(&ip6_frag_lru_list)) {
2781da177e4SLinus Torvalds 			read_unlock(&ip6_frag_lock);
2791da177e4SLinus Torvalds 			return;
2801da177e4SLinus Torvalds 		}
2811da177e4SLinus Torvalds 		tmp = ip6_frag_lru_list.next;
2821da177e4SLinus Torvalds 		fq = list_entry(tmp, struct frag_queue, lru_list);
2831da177e4SLinus Torvalds 		atomic_inc(&fq->refcnt);
2841da177e4SLinus Torvalds 		read_unlock(&ip6_frag_lock);
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 		spin_lock(&fq->lock);
2871da177e4SLinus Torvalds 		if (!(fq->last_in&COMPLETE))
2881da177e4SLinus Torvalds 			fq_kill(fq);
2891da177e4SLinus Torvalds 		spin_unlock(&fq->lock);
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 		fq_put(fq, &work);
292a11d206dSYOSHIFUJI Hideaki 		IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
2931da177e4SLinus Torvalds 	}
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds static void ip6_frag_expire(unsigned long data)
2971da177e4SLinus Torvalds {
2981da177e4SLinus Torvalds 	struct frag_queue *fq = (struct frag_queue *) data;
299a11d206dSYOSHIFUJI Hideaki 	struct net_device *dev = NULL;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	spin_lock(&fq->lock);
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	if (fq->last_in & COMPLETE)
3041da177e4SLinus Torvalds 		goto out;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	fq_kill(fq);
3071da177e4SLinus Torvalds 
308881d966bSEric W. Biederman 	dev = dev_get_by_index(&init_net, fq->iif);
309a11d206dSYOSHIFUJI Hideaki 	if (!dev)
310a11d206dSYOSHIFUJI Hideaki 		goto out;
311a11d206dSYOSHIFUJI Hideaki 
312a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
313a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
314a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
315a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
3161da177e4SLinus Torvalds 
31778c784c4SIngo Oeser 	/* Don't send error if the first segment did not arrive. */
31878c784c4SIngo Oeser 	if (!(fq->last_in&FIRST_IN) || !fq->fragments)
31978c784c4SIngo Oeser 		goto out;
32078c784c4SIngo Oeser 
3211da177e4SLinus Torvalds 	/*
3221da177e4SLinus Torvalds 	   But use as source device on which LAST ARRIVED
3231da177e4SLinus Torvalds 	   segment was received. And do not use fq->dev
3241da177e4SLinus Torvalds 	   pointer directly, device might already disappeared.
3251da177e4SLinus Torvalds 	 */
3261da177e4SLinus Torvalds 	fq->fragments->dev = dev;
32778c784c4SIngo Oeser 	icmpv6_send(fq->fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0, dev);
3281da177e4SLinus Torvalds out:
329a11d206dSYOSHIFUJI Hideaki 	if (dev)
330a11d206dSYOSHIFUJI Hideaki 		dev_put(dev);
3311da177e4SLinus Torvalds 	spin_unlock(&fq->lock);
3321da177e4SLinus Torvalds 	fq_put(fq, NULL);
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds /* Creation primitives. */
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 
338f6596f9dSZach Brown static struct frag_queue *ip6_frag_intern(struct frag_queue *fq_in)
3391da177e4SLinus Torvalds {
3401da177e4SLinus Torvalds 	struct frag_queue *fq;
341f6596f9dSZach Brown 	unsigned int hash;
342e7c8a41eSYasuyuki Kozakai #ifdef CONFIG_SMP
343e7c8a41eSYasuyuki Kozakai 	struct hlist_node *n;
344e7c8a41eSYasuyuki Kozakai #endif
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	write_lock(&ip6_frag_lock);
347f6596f9dSZach Brown 	hash = ip6qhashfn(fq_in->id, &fq_in->saddr, &fq_in->daddr);
3481da177e4SLinus Torvalds #ifdef CONFIG_SMP
349e7c8a41eSYasuyuki Kozakai 	hlist_for_each_entry(fq, n, &ip6_frag_hash[hash], list) {
3501da177e4SLinus Torvalds 		if (fq->id == fq_in->id &&
3511da177e4SLinus Torvalds 		    ipv6_addr_equal(&fq_in->saddr, &fq->saddr) &&
3521da177e4SLinus Torvalds 		    ipv6_addr_equal(&fq_in->daddr, &fq->daddr)) {
3531da177e4SLinus Torvalds 			atomic_inc(&fq->refcnt);
3541da177e4SLinus Torvalds 			write_unlock(&ip6_frag_lock);
3551da177e4SLinus Torvalds 			fq_in->last_in |= COMPLETE;
3561da177e4SLinus Torvalds 			fq_put(fq_in, NULL);
3571da177e4SLinus Torvalds 			return fq;
3581da177e4SLinus Torvalds 		}
3591da177e4SLinus Torvalds 	}
3601da177e4SLinus Torvalds #endif
3611da177e4SLinus Torvalds 	fq = fq_in;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	if (!mod_timer(&fq->timer, jiffies + sysctl_ip6frag_time))
3641da177e4SLinus Torvalds 		atomic_inc(&fq->refcnt);
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 	atomic_inc(&fq->refcnt);
367e7c8a41eSYasuyuki Kozakai 	hlist_add_head(&fq->list, &ip6_frag_hash[hash]);
3681da177e4SLinus Torvalds 	INIT_LIST_HEAD(&fq->lru_list);
3691da177e4SLinus Torvalds 	list_add_tail(&fq->lru_list, &ip6_frag_lru_list);
3701da177e4SLinus Torvalds 	ip6_frag_nqueues++;
3711da177e4SLinus Torvalds 	write_unlock(&ip6_frag_lock);
3721da177e4SLinus Torvalds 	return fq;
3731da177e4SLinus Torvalds }
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds static struct frag_queue *
377e69a4adcSAl Viro ip6_frag_create(__be32 id, struct in6_addr *src, struct in6_addr *dst,
378a11d206dSYOSHIFUJI Hideaki 		struct inet6_dev *idev)
3791da177e4SLinus Torvalds {
3801da177e4SLinus Torvalds 	struct frag_queue *fq;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	if ((fq = frag_alloc_queue()) == NULL)
3831da177e4SLinus Torvalds 		goto oom;
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 	fq->id = id;
3861da177e4SLinus Torvalds 	ipv6_addr_copy(&fq->saddr, src);
3871da177e4SLinus Torvalds 	ipv6_addr_copy(&fq->daddr, dst);
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	init_timer(&fq->timer);
3901da177e4SLinus Torvalds 	fq->timer.function = ip6_frag_expire;
3911da177e4SLinus Torvalds 	fq->timer.data = (long) fq;
3921da177e4SLinus Torvalds 	spin_lock_init(&fq->lock);
3931da177e4SLinus Torvalds 	atomic_set(&fq->refcnt, 1);
3941da177e4SLinus Torvalds 
395f6596f9dSZach Brown 	return ip6_frag_intern(fq);
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds oom:
398a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS);
3991da177e4SLinus Torvalds 	return NULL;
4001da177e4SLinus Torvalds }
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds static __inline__ struct frag_queue *
403e69a4adcSAl Viro fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst,
404a11d206dSYOSHIFUJI Hideaki 	struct inet6_dev *idev)
4051da177e4SLinus Torvalds {
4061da177e4SLinus Torvalds 	struct frag_queue *fq;
407e7c8a41eSYasuyuki Kozakai 	struct hlist_node *n;
408f6596f9dSZach Brown 	unsigned int hash;
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	read_lock(&ip6_frag_lock);
411f6596f9dSZach Brown 	hash = ip6qhashfn(id, src, dst);
412e7c8a41eSYasuyuki Kozakai 	hlist_for_each_entry(fq, n, &ip6_frag_hash[hash], list) {
4131da177e4SLinus Torvalds 		if (fq->id == id &&
4141da177e4SLinus Torvalds 		    ipv6_addr_equal(src, &fq->saddr) &&
4151da177e4SLinus Torvalds 		    ipv6_addr_equal(dst, &fq->daddr)) {
4161da177e4SLinus Torvalds 			atomic_inc(&fq->refcnt);
4171da177e4SLinus Torvalds 			read_unlock(&ip6_frag_lock);
4181da177e4SLinus Torvalds 			return fq;
4191da177e4SLinus Torvalds 		}
4201da177e4SLinus Torvalds 	}
4211da177e4SLinus Torvalds 	read_unlock(&ip6_frag_lock);
4221da177e4SLinus Torvalds 
423a11d206dSYOSHIFUJI Hideaki 	return ip6_frag_create(id, src, dst, idev);
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 
427f61944efSHerbert Xu static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
4281da177e4SLinus Torvalds 			   struct frag_hdr *fhdr, int nhoff)
4291da177e4SLinus Torvalds {
4301da177e4SLinus Torvalds 	struct sk_buff *prev, *next;
431f61944efSHerbert Xu 	struct net_device *dev;
4321da177e4SLinus Torvalds 	int offset, end;
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 	if (fq->last_in & COMPLETE)
4351da177e4SLinus Torvalds 		goto err;
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 	offset = ntohs(fhdr->frag_off) & ~0x7;
4380660e03fSArnaldo Carvalho de Melo 	end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
4390660e03fSArnaldo Carvalho de Melo 			((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	if ((unsigned int)end > IPV6_MAXPLEN) {
442a11d206dSYOSHIFUJI Hideaki 		IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
443a11d206dSYOSHIFUJI Hideaki 				 IPSTATS_MIB_INHDRERRORS);
444d56f90a7SArnaldo Carvalho de Melo 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
445d56f90a7SArnaldo Carvalho de Melo 				  ((u8 *)&fhdr->frag_off -
446d56f90a7SArnaldo Carvalho de Melo 				   skb_network_header(skb)));
447f61944efSHerbert Xu 		return -1;
4481da177e4SLinus Torvalds 	}
4491da177e4SLinus Torvalds 
450d56f90a7SArnaldo Carvalho de Melo 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
451d56f90a7SArnaldo Carvalho de Melo 		const unsigned char *nh = skb_network_header(skb);
4521da177e4SLinus Torvalds 		skb->csum = csum_sub(skb->csum,
453d56f90a7SArnaldo Carvalho de Melo 				     csum_partial(nh, (u8 *)(fhdr + 1) - nh,
454d56f90a7SArnaldo Carvalho de Melo 						  0));
455d56f90a7SArnaldo Carvalho de Melo 	}
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	/* Is this the final fragment? */
4581da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(IP6_MF))) {
4591da177e4SLinus Torvalds 		/* If we already have some bits beyond end
4601da177e4SLinus Torvalds 		 * or have different end, the segment is corrupted.
4611da177e4SLinus Torvalds 		 */
4621da177e4SLinus Torvalds 		if (end < fq->len ||
4631da177e4SLinus Torvalds 		    ((fq->last_in & LAST_IN) && end != fq->len))
4641da177e4SLinus Torvalds 			goto err;
4651da177e4SLinus Torvalds 		fq->last_in |= LAST_IN;
4661da177e4SLinus Torvalds 		fq->len = end;
4671da177e4SLinus Torvalds 	} else {
4681da177e4SLinus Torvalds 		/* Check if the fragment is rounded to 8 bytes.
4691da177e4SLinus Torvalds 		 * Required by the RFC.
4701da177e4SLinus Torvalds 		 */
4711da177e4SLinus Torvalds 		if (end & 0x7) {
4721da177e4SLinus Torvalds 			/* RFC2460 says always send parameter problem in
4731da177e4SLinus Torvalds 			 * this case. -DaveM
4741da177e4SLinus Torvalds 			 */
475a11d206dSYOSHIFUJI Hideaki 			IP6_INC_STATS_BH(ip6_dst_idev(skb->dst),
476a11d206dSYOSHIFUJI Hideaki 					 IPSTATS_MIB_INHDRERRORS);
4771da177e4SLinus Torvalds 			icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
4781da177e4SLinus Torvalds 					  offsetof(struct ipv6hdr, payload_len));
479f61944efSHerbert Xu 			return -1;
4801da177e4SLinus Torvalds 		}
4811da177e4SLinus Torvalds 		if (end > fq->len) {
4821da177e4SLinus Torvalds 			/* Some bits beyond end -> corruption. */
4831da177e4SLinus Torvalds 			if (fq->last_in & LAST_IN)
4841da177e4SLinus Torvalds 				goto err;
4851da177e4SLinus Torvalds 			fq->len = end;
4861da177e4SLinus Torvalds 		}
4871da177e4SLinus Torvalds 	}
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	if (end == offset)
4901da177e4SLinus Torvalds 		goto err;
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	/* Point into the IP datagram 'data' part. */
4931da177e4SLinus Torvalds 	if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
4941da177e4SLinus Torvalds 		goto err;
49542ca89c1SStephen Hemminger 
49642ca89c1SStephen Hemminger 	if (pskb_trim_rcsum(skb, end - offset))
4971da177e4SLinus Torvalds 		goto err;
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	/* Find out which fragments are in front and at the back of us
5001da177e4SLinus Torvalds 	 * in the chain of fragments so far.  We must know where to put
5011da177e4SLinus Torvalds 	 * this fragment, right?
5021da177e4SLinus Torvalds 	 */
5031da177e4SLinus Torvalds 	prev = NULL;
5041da177e4SLinus Torvalds 	for(next = fq->fragments; next != NULL; next = next->next) {
5051da177e4SLinus Torvalds 		if (FRAG6_CB(next)->offset >= offset)
5061da177e4SLinus Torvalds 			break;	/* bingo! */
5071da177e4SLinus Torvalds 		prev = next;
5081da177e4SLinus Torvalds 	}
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	/* We found where to put this one.  Check for overlap with
5111da177e4SLinus Torvalds 	 * preceding fragment, and, if needed, align things so that
5121da177e4SLinus Torvalds 	 * any overlaps are eliminated.
5131da177e4SLinus Torvalds 	 */
5141da177e4SLinus Torvalds 	if (prev) {
5151da177e4SLinus Torvalds 		int i = (FRAG6_CB(prev)->offset + prev->len) - offset;
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 		if (i > 0) {
5181da177e4SLinus Torvalds 			offset += i;
5191da177e4SLinus Torvalds 			if (end <= offset)
5201da177e4SLinus Torvalds 				goto err;
5211da177e4SLinus Torvalds 			if (!pskb_pull(skb, i))
5221da177e4SLinus Torvalds 				goto err;
5231da177e4SLinus Torvalds 			if (skb->ip_summed != CHECKSUM_UNNECESSARY)
5241da177e4SLinus Torvalds 				skb->ip_summed = CHECKSUM_NONE;
5251da177e4SLinus Torvalds 		}
5261da177e4SLinus Torvalds 	}
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	/* Look for overlap with succeeding segments.
5291da177e4SLinus Torvalds 	 * If we can merge fragments, do it.
5301da177e4SLinus Torvalds 	 */
5311da177e4SLinus Torvalds 	while (next && FRAG6_CB(next)->offset < end) {
5321da177e4SLinus Torvalds 		int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 		if (i < next->len) {
5351da177e4SLinus Torvalds 			/* Eat head of the next overlapped fragment
5361da177e4SLinus Torvalds 			 * and leave the loop. The next ones cannot overlap.
5371da177e4SLinus Torvalds 			 */
5381da177e4SLinus Torvalds 			if (!pskb_pull(next, i))
5391da177e4SLinus Torvalds 				goto err;
5401da177e4SLinus Torvalds 			FRAG6_CB(next)->offset += i;	/* next fragment */
5411da177e4SLinus Torvalds 			fq->meat -= i;
5421da177e4SLinus Torvalds 			if (next->ip_summed != CHECKSUM_UNNECESSARY)
5431da177e4SLinus Torvalds 				next->ip_summed = CHECKSUM_NONE;
5441da177e4SLinus Torvalds 			break;
5451da177e4SLinus Torvalds 		} else {
5461da177e4SLinus Torvalds 			struct sk_buff *free_it = next;
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 			/* Old fragment is completely overridden with
5491da177e4SLinus Torvalds 			 * new one drop it.
5501da177e4SLinus Torvalds 			 */
5511da177e4SLinus Torvalds 			next = next->next;
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 			if (prev)
5541da177e4SLinus Torvalds 				prev->next = next;
5551da177e4SLinus Torvalds 			else
5561da177e4SLinus Torvalds 				fq->fragments = next;
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds 			fq->meat -= free_it->len;
5591da177e4SLinus Torvalds 			frag_kfree_skb(free_it, NULL);
5601da177e4SLinus Torvalds 		}
5611da177e4SLinus Torvalds 	}
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	FRAG6_CB(skb)->offset = offset;
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	/* Insert this fragment in the chain of fragments. */
5661da177e4SLinus Torvalds 	skb->next = next;
5671da177e4SLinus Torvalds 	if (prev)
5681da177e4SLinus Torvalds 		prev->next = skb;
5691da177e4SLinus Torvalds 	else
5701da177e4SLinus Torvalds 		fq->fragments = skb;
5711da177e4SLinus Torvalds 
572f61944efSHerbert Xu 	dev = skb->dev;
573f61944efSHerbert Xu 	if (dev) {
574f61944efSHerbert Xu 		fq->iif = dev->ifindex;
5751da177e4SLinus Torvalds 		skb->dev = NULL;
576f61944efSHerbert Xu 	}
577b7aa0bf7SEric Dumazet 	fq->stamp = skb->tstamp;
5781da177e4SLinus Torvalds 	fq->meat += skb->len;
5791da177e4SLinus Torvalds 	atomic_add(skb->truesize, &ip6_frag_mem);
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	/* The first fragment.
5821da177e4SLinus Torvalds 	 * nhoffset is obtained from the first fragment, of course.
5831da177e4SLinus Torvalds 	 */
5841da177e4SLinus Torvalds 	if (offset == 0) {
5851da177e4SLinus Torvalds 		fq->nhoffset = nhoff;
5861da177e4SLinus Torvalds 		fq->last_in |= FIRST_IN;
5871da177e4SLinus Torvalds 	}
588f61944efSHerbert Xu 
589f61944efSHerbert Xu 	if (fq->last_in == (FIRST_IN | LAST_IN) && fq->meat == fq->len)
590f61944efSHerbert Xu 		return ip6_frag_reasm(fq, prev, dev);
591f61944efSHerbert Xu 
5921da177e4SLinus Torvalds 	write_lock(&ip6_frag_lock);
5931da177e4SLinus Torvalds 	list_move_tail(&fq->lru_list, &ip6_frag_lru_list);
5941da177e4SLinus Torvalds 	write_unlock(&ip6_frag_lock);
595f61944efSHerbert Xu 	return -1;
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds err:
598a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
5991da177e4SLinus Torvalds 	kfree_skb(skb);
600f61944efSHerbert Xu 	return -1;
6011da177e4SLinus Torvalds }
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds /*
6041da177e4SLinus Torvalds  *	Check if this packet is complete.
6051da177e4SLinus Torvalds  *	Returns NULL on failure by any reason, and pointer
6061da177e4SLinus Torvalds  *	to current nexthdr field in reassembled frame.
6071da177e4SLinus Torvalds  *
6081da177e4SLinus Torvalds  *	It is called with locked fq, and caller must check that
6091da177e4SLinus Torvalds  *	queue is eligible for reassembly i.e. it is not COMPLETE,
6101da177e4SLinus Torvalds  *	the last and the first frames arrived and all the bits are here.
6111da177e4SLinus Torvalds  */
612f61944efSHerbert Xu static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
6131da177e4SLinus Torvalds 			  struct net_device *dev)
6141da177e4SLinus Torvalds {
6151da177e4SLinus Torvalds 	struct sk_buff *fp, *head = fq->fragments;
6161da177e4SLinus Torvalds 	int    payload_len;
6171da177e4SLinus Torvalds 	unsigned int nhoff;
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	fq_kill(fq);
6201da177e4SLinus Torvalds 
621f61944efSHerbert Xu 	/* Make the one we just received the head. */
622f61944efSHerbert Xu 	if (prev) {
623f61944efSHerbert Xu 		head = prev->next;
624f61944efSHerbert Xu 		fp = skb_clone(head, GFP_ATOMIC);
625f61944efSHerbert Xu 
626f61944efSHerbert Xu 		if (!fp)
627f61944efSHerbert Xu 			goto out_oom;
628f61944efSHerbert Xu 
629f61944efSHerbert Xu 		fp->next = head->next;
630f61944efSHerbert Xu 		prev->next = fp;
631f61944efSHerbert Xu 
632f61944efSHerbert Xu 		skb_morph(head, fq->fragments);
633f61944efSHerbert Xu 		head->next = fq->fragments->next;
634f61944efSHerbert Xu 
635f61944efSHerbert Xu 		kfree_skb(fq->fragments);
636f61944efSHerbert Xu 		fq->fragments = head;
637f61944efSHerbert Xu 	}
638f61944efSHerbert Xu 
6391da177e4SLinus Torvalds 	BUG_TRAP(head != NULL);
6401da177e4SLinus Torvalds 	BUG_TRAP(FRAG6_CB(head)->offset == 0);
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	/* Unfragmented part is taken from the first segment. */
643d56f90a7SArnaldo Carvalho de Melo 	payload_len = ((head->data - skb_network_header(head)) -
644d56f90a7SArnaldo Carvalho de Melo 		       sizeof(struct ipv6hdr) + fq->len -
645d56f90a7SArnaldo Carvalho de Melo 		       sizeof(struct frag_hdr));
6461da177e4SLinus Torvalds 	if (payload_len > IPV6_MAXPLEN)
6471da177e4SLinus Torvalds 		goto out_oversize;
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	/* Head of list must not be cloned. */
6501da177e4SLinus Torvalds 	if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
6511da177e4SLinus Torvalds 		goto out_oom;
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 	/* If the first fragment is fragmented itself, we split
6541da177e4SLinus Torvalds 	 * it to two chunks: the first with data and paged part
6551da177e4SLinus Torvalds 	 * and the second, holding only fragments. */
6561da177e4SLinus Torvalds 	if (skb_shinfo(head)->frag_list) {
6571da177e4SLinus Torvalds 		struct sk_buff *clone;
6581da177e4SLinus Torvalds 		int i, plen = 0;
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 		if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
6611da177e4SLinus Torvalds 			goto out_oom;
6621da177e4SLinus Torvalds 		clone->next = head->next;
6631da177e4SLinus Torvalds 		head->next = clone;
6641da177e4SLinus Torvalds 		skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
6651da177e4SLinus Torvalds 		skb_shinfo(head)->frag_list = NULL;
6661da177e4SLinus Torvalds 		for (i=0; i<skb_shinfo(head)->nr_frags; i++)
6671da177e4SLinus Torvalds 			plen += skb_shinfo(head)->frags[i].size;
6681da177e4SLinus Torvalds 		clone->len = clone->data_len = head->data_len - plen;
6691da177e4SLinus Torvalds 		head->data_len -= clone->len;
6701da177e4SLinus Torvalds 		head->len -= clone->len;
6711da177e4SLinus Torvalds 		clone->csum = 0;
6721da177e4SLinus Torvalds 		clone->ip_summed = head->ip_summed;
6731da177e4SLinus Torvalds 		atomic_add(clone->truesize, &ip6_frag_mem);
6741da177e4SLinus Torvalds 	}
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds 	/* We have to remove fragment header from datagram and to relocate
6771da177e4SLinus Torvalds 	 * header in order to calculate ICV correctly. */
6781da177e4SLinus Torvalds 	nhoff = fq->nhoffset;
679b0e380b1SArnaldo Carvalho de Melo 	skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
6801da177e4SLinus Torvalds 	memmove(head->head + sizeof(struct frag_hdr), head->head,
6811da177e4SLinus Torvalds 		(head->data - head->head) - sizeof(struct frag_hdr));
682b0e380b1SArnaldo Carvalho de Melo 	head->mac_header += sizeof(struct frag_hdr);
683b0e380b1SArnaldo Carvalho de Melo 	head->network_header += sizeof(struct frag_hdr);
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 	skb_shinfo(head)->frag_list = head->next;
686badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(head);
687d56f90a7SArnaldo Carvalho de Melo 	skb_push(head, head->data - skb_network_header(head));
6881da177e4SLinus Torvalds 	atomic_sub(head->truesize, &ip6_frag_mem);
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 	for (fp=head->next; fp; fp = fp->next) {
6911da177e4SLinus Torvalds 		head->data_len += fp->len;
6921da177e4SLinus Torvalds 		head->len += fp->len;
6931da177e4SLinus Torvalds 		if (head->ip_summed != fp->ip_summed)
6941da177e4SLinus Torvalds 			head->ip_summed = CHECKSUM_NONE;
69584fa7933SPatrick McHardy 		else if (head->ip_summed == CHECKSUM_COMPLETE)
6961da177e4SLinus Torvalds 			head->csum = csum_add(head->csum, fp->csum);
6971da177e4SLinus Torvalds 		head->truesize += fp->truesize;
6981da177e4SLinus Torvalds 		atomic_sub(fp->truesize, &ip6_frag_mem);
6991da177e4SLinus Torvalds 	}
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	head->next = NULL;
7021da177e4SLinus Torvalds 	head->dev = dev;
703b7aa0bf7SEric Dumazet 	head->tstamp = fq->stamp;
7040660e03fSArnaldo Carvalho de Melo 	ipv6_hdr(head)->payload_len = htons(payload_len);
705951dbc8aSPatrick McHardy 	IP6CB(head)->nhoff = nhoff;
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	/* Yes, and fold redundant checksum back. 8) */
70884fa7933SPatrick McHardy 	if (head->ip_summed == CHECKSUM_COMPLETE)
709d56f90a7SArnaldo Carvalho de Melo 		head->csum = csum_partial(skb_network_header(head),
710cfe1fc77SArnaldo Carvalho de Melo 					  skb_network_header_len(head),
711d56f90a7SArnaldo Carvalho de Melo 					  head->csum);
7121da177e4SLinus Torvalds 
713a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
714a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
715a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
7161da177e4SLinus Torvalds 	fq->fragments = NULL;
7171da177e4SLinus Torvalds 	return 1;
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds out_oversize:
7201da177e4SLinus Torvalds 	if (net_ratelimit())
7211da177e4SLinus Torvalds 		printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len);
7221da177e4SLinus Torvalds 	goto out_fail;
7231da177e4SLinus Torvalds out_oom:
7241da177e4SLinus Torvalds 	if (net_ratelimit())
7251da177e4SLinus Torvalds 		printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n");
7261da177e4SLinus Torvalds out_fail:
727a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
728a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
729a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
7301da177e4SLinus Torvalds 	return -1;
7311da177e4SLinus Torvalds }
7321da177e4SLinus Torvalds 
733951dbc8aSPatrick McHardy static int ipv6_frag_rcv(struct sk_buff **skbp)
7341da177e4SLinus Torvalds {
7351da177e4SLinus Torvalds 	struct sk_buff *skb = *skbp;
7361da177e4SLinus Torvalds 	struct frag_hdr *fhdr;
7371da177e4SLinus Torvalds 	struct frag_queue *fq;
7380660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *hdr = ipv6_hdr(skb);
7391da177e4SLinus Torvalds 
740a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMREQDS);
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	/* Jumbo payload inhibits frag. header */
7431da177e4SLinus Torvalds 	if (hdr->payload_len==0) {
744a11d206dSYOSHIFUJI Hideaki 		IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
745cfe1fc77SArnaldo Carvalho de Melo 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
746cfe1fc77SArnaldo Carvalho de Melo 				  skb_network_header_len(skb));
7471da177e4SLinus Torvalds 		return -1;
7481da177e4SLinus Torvalds 	}
749ea2ae17dSArnaldo Carvalho de Melo 	if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
750ea2ae17dSArnaldo Carvalho de Melo 				 sizeof(struct frag_hdr)))) {
751a11d206dSYOSHIFUJI Hideaki 		IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS);
752cfe1fc77SArnaldo Carvalho de Melo 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
753cfe1fc77SArnaldo Carvalho de Melo 				  skb_network_header_len(skb));
7541da177e4SLinus Torvalds 		return -1;
7551da177e4SLinus Torvalds 	}
7561da177e4SLinus Torvalds 
7570660e03fSArnaldo Carvalho de Melo 	hdr = ipv6_hdr(skb);
7589c70220bSArnaldo Carvalho de Melo 	fhdr = (struct frag_hdr *)skb_transport_header(skb);
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(0xFFF9))) {
7611da177e4SLinus Torvalds 		/* It is not a fragmented frame */
762b0e380b1SArnaldo Carvalho de Melo 		skb->transport_header += sizeof(struct frag_hdr);
763a11d206dSYOSHIFUJI Hideaki 		IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMOKS);
7641da177e4SLinus Torvalds 
765d56f90a7SArnaldo Carvalho de Melo 		IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
7661da177e4SLinus Torvalds 		return 1;
7671da177e4SLinus Torvalds 	}
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	if (atomic_read(&ip6_frag_mem) > sysctl_ip6frag_high_thresh)
770a11d206dSYOSHIFUJI Hideaki 		ip6_evictor(ip6_dst_idev(skb->dst));
7711da177e4SLinus Torvalds 
772a11d206dSYOSHIFUJI Hideaki 	if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr,
773a11d206dSYOSHIFUJI Hideaki 			  ip6_dst_idev(skb->dst))) != NULL) {
774f61944efSHerbert Xu 		int ret;
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 		spin_lock(&fq->lock);
7771da177e4SLinus Torvalds 
778f61944efSHerbert Xu 		ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 		spin_unlock(&fq->lock);
7811da177e4SLinus Torvalds 		fq_put(fq, NULL);
7821da177e4SLinus Torvalds 		return ret;
7831da177e4SLinus Torvalds 	}
7841da177e4SLinus Torvalds 
785a11d206dSYOSHIFUJI Hideaki 	IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS);
7861da177e4SLinus Torvalds 	kfree_skb(skb);
7871da177e4SLinus Torvalds 	return -1;
7881da177e4SLinus Torvalds }
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds static struct inet6_protocol frag_protocol =
7911da177e4SLinus Torvalds {
7921da177e4SLinus Torvalds 	.handler	=	ipv6_frag_rcv,
7931da177e4SLinus Torvalds 	.flags		=	INET6_PROTO_NOPOLICY,
7941da177e4SLinus Torvalds };
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds void __init ipv6_frag_init(void)
7971da177e4SLinus Torvalds {
7981da177e4SLinus Torvalds 	if (inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT) < 0)
7991da177e4SLinus Torvalds 		printk(KERN_ERR "ipv6_frag_init: Could not register protocol\n");
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 	ip6_frag_hash_rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^
8021da177e4SLinus Torvalds 				   (jiffies ^ (jiffies >> 6)));
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 	init_timer(&ip6_frag_secret_timer);
8051da177e4SLinus Torvalds 	ip6_frag_secret_timer.function = ip6_frag_secret_rebuild;
8061da177e4SLinus Torvalds 	ip6_frag_secret_timer.expires = jiffies + sysctl_ip6frag_secret_interval;
8071da177e4SLinus Torvalds 	add_timer(&ip6_frag_secret_timer);
8081da177e4SLinus Torvalds }
809