xref: /openbmc/linux/net/ipv6/reassembly.c (revision 219badfa)
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  *	Based on: net/ipv4/ip_fragment.c
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
111da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
121da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
131da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds /*
171da177e4SLinus Torvalds  *	Fixes:
181da177e4SLinus Torvalds  *	Andi Kleen	Make it work with multiple hosts.
191da177e4SLinus Torvalds  *			More RFC compliance.
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  *      Horst von Brand Add missing #include <linux/string.h>
221da177e4SLinus Torvalds  *	Alexey Kuznetsov	SMP races, threading, cleanup.
231da177e4SLinus Torvalds  *	Patrick McHardy		LRU queue of frag heads for evictor.
241da177e4SLinus Torvalds  *	Mitsuru KANDA @USAGI	Register inet6_protocol{}.
251da177e4SLinus Torvalds  *	David Stevens and
261da177e4SLinus Torvalds  *	YOSHIFUJI,H. @USAGI	Always remove fragment header to
271da177e4SLinus Torvalds  *				calculate ICV correctly.
281da177e4SLinus Torvalds  */
295a3da1feSHannes Frederic Sowa 
305a3da1feSHannes Frederic Sowa #define pr_fmt(fmt) "IPv6: " fmt
315a3da1feSHannes Frederic Sowa 
321da177e4SLinus Torvalds #include <linux/errno.h>
331da177e4SLinus Torvalds #include <linux/types.h>
341da177e4SLinus Torvalds #include <linux/string.h>
351da177e4SLinus Torvalds #include <linux/socket.h>
361da177e4SLinus Torvalds #include <linux/sockios.h>
371da177e4SLinus Torvalds #include <linux/jiffies.h>
381da177e4SLinus Torvalds #include <linux/net.h>
391da177e4SLinus Torvalds #include <linux/list.h>
401da177e4SLinus Torvalds #include <linux/netdevice.h>
411da177e4SLinus Torvalds #include <linux/in6.h>
421da177e4SLinus Torvalds #include <linux/ipv6.h>
431da177e4SLinus Torvalds #include <linux/icmpv6.h>
441da177e4SLinus Torvalds #include <linux/random.h>
451da177e4SLinus Torvalds #include <linux/jhash.h>
46f61944efSHerbert Xu #include <linux/skbuff.h>
475a0e3ad6STejun Heo #include <linux/slab.h>
48bc3b2d7fSPaul Gortmaker #include <linux/export.h>
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #include <net/sock.h>
511da177e4SLinus Torvalds #include <net/snmp.h>
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds #include <net/ipv6.h>
54a11d206dSYOSHIFUJI Hideaki #include <net/ip6_route.h>
551da177e4SLinus Torvalds #include <net/protocol.h>
561da177e4SLinus Torvalds #include <net/transp_v6.h>
571da177e4SLinus Torvalds #include <net/rawv6.h>
581da177e4SLinus Torvalds #include <net/ndisc.h>
591da177e4SLinus Torvalds #include <net/addrconf.h>
605ab11c98SPavel Emelyanov #include <net/inet_frag.h>
61eec2e618SHannes Frederic Sowa #include <net/inet_ecn.h>
621da177e4SLinus Torvalds 
63d4ad4d22SNikolay Aleksandrov static const char ip6_frag_cache_name[] = "ip6-frags";
64d4ad4d22SNikolay Aleksandrov 
65fc08c258SFabian Frederick static u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
66eec2e618SHannes Frederic Sowa {
67eec2e618SHannes Frederic Sowa 	return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
68eec2e618SHannes Frederic Sowa }
691da177e4SLinus Torvalds 
707eb95156SPavel Emelyanov static struct inet_frags ip6_frags;
711da177e4SLinus Torvalds 
72f61944efSHerbert Xu static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
73f61944efSHerbert Xu 			  struct net_device *dev);
74f61944efSHerbert Xu 
7536c77782SFlorian Westphal void ip6_frag_init(struct inet_frag_queue *q, const void *a)
76c6fda282SPavel Emelyanov {
77c6fda282SPavel Emelyanov 	struct frag_queue *fq = container_of(q, struct frag_queue, q);
78648700f7SEric Dumazet 	const struct frag_v6_compare_key *key = a;
79c6fda282SPavel Emelyanov 
80648700f7SEric Dumazet 	q->key.v6 = *key;
81648700f7SEric Dumazet 	fq->ecn = 0;
82c6fda282SPavel Emelyanov }
83c6fda282SPavel Emelyanov EXPORT_SYMBOL(ip6_frag_init);
84c6fda282SPavel Emelyanov 
85093ba729SEric Dumazet void ip6_expire_frag_queue(struct net *net, struct frag_queue *fq)
861da177e4SLinus Torvalds {
87a11d206dSYOSHIFUJI Hideaki 	struct net_device *dev = NULL;
8805c0b86bSEric Dumazet 	struct sk_buff *head;
89e521db9dSPavel Emelyanov 
9005c0b86bSEric Dumazet 	rcu_read_lock();
915ab11c98SPavel Emelyanov 	spin_lock(&fq->q.lock);
921da177e4SLinus Torvalds 
9306aa8b8aSNikolay Aleksandrov 	if (fq->q.flags & INET_FRAG_COMPLETE)
941da177e4SLinus Torvalds 		goto out;
951da177e4SLinus Torvalds 
96093ba729SEric Dumazet 	inet_frag_kill(&fq->q);
971da177e4SLinus Torvalds 
9869df9d59SEric Dumazet 	dev = dev_get_by_index_rcu(net, fq->iif);
9969df9d59SEric Dumazet 	if (!dev)
10005c0b86bSEric Dumazet 		goto out;
10169df9d59SEric Dumazet 
1021d015503SEric Dumazet 	__IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
1031d015503SEric Dumazet 	__IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
1042e404f63SNikolay Aleksandrov 
10578c784c4SIngo Oeser 	/* Don't send error if the first segment did not arrive. */
10605c0b86bSEric Dumazet 	head = fq->q.fragments;
10705c0b86bSEric Dumazet 	if (!(fq->q.flags & INET_FRAG_FIRST_IN) || !head)
10805c0b86bSEric Dumazet 		goto out;
10978c784c4SIngo Oeser 
1102e404f63SNikolay Aleksandrov 	/* But use as source device on which LAST ARRIVED
1112e404f63SNikolay Aleksandrov 	 * segment was received. And do not use fq->dev
1122e404f63SNikolay Aleksandrov 	 * pointer directly, device might already disappeared.
1131da177e4SLinus Torvalds 	 */
11405c0b86bSEric Dumazet 	head->dev = dev;
11505c0b86bSEric Dumazet 	skb_get(head);
11605c0b86bSEric Dumazet 	spin_unlock(&fq->q.lock);
11705c0b86bSEric Dumazet 
11805c0b86bSEric Dumazet 	icmpv6_send(head, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0);
11905c0b86bSEric Dumazet 	kfree_skb(head);
12005c0b86bSEric Dumazet 	goto out_rcu_unlock;
12105c0b86bSEric Dumazet 
1221da177e4SLinus Torvalds out:
1235ab11c98SPavel Emelyanov 	spin_unlock(&fq->q.lock);
12405c0b86bSEric Dumazet out_rcu_unlock:
12505c0b86bSEric Dumazet 	rcu_read_unlock();
126093ba729SEric Dumazet 	inet_frag_put(&fq->q);
127b836c99fSAmerigo Wang }
128b836c99fSAmerigo Wang EXPORT_SYMBOL(ip6_expire_frag_queue);
129b836c99fSAmerigo Wang 
13078802011SKees Cook static void ip6_frag_expire(struct timer_list *t)
131b836c99fSAmerigo Wang {
13278802011SKees Cook 	struct inet_frag_queue *frag = from_timer(frag, t, timer);
133b836c99fSAmerigo Wang 	struct frag_queue *fq;
134b836c99fSAmerigo Wang 	struct net *net;
135b836c99fSAmerigo Wang 
13678802011SKees Cook 	fq = container_of(frag, struct frag_queue, q);
137b836c99fSAmerigo Wang 	net = container_of(fq->q.net, struct net, ipv6.frags);
138b836c99fSAmerigo Wang 
139093ba729SEric Dumazet 	ip6_expire_frag_queue(net, fq);
1401da177e4SLinus Torvalds }
1411da177e4SLinus Torvalds 
142fc08c258SFabian Frederick static struct frag_queue *
143648700f7SEric Dumazet fq_find(struct net *net, __be32 id, const struct ipv6hdr *hdr, int iif)
1441da177e4SLinus Torvalds {
145648700f7SEric Dumazet 	struct frag_v6_compare_key key = {
146648700f7SEric Dumazet 		.id = id,
147648700f7SEric Dumazet 		.saddr = hdr->saddr,
148648700f7SEric Dumazet 		.daddr = hdr->daddr,
149648700f7SEric Dumazet 		.user = IP6_DEFRAG_LOCAL_DELIVER,
150648700f7SEric Dumazet 		.iif = iif,
151648700f7SEric Dumazet 	};
152c6fda282SPavel Emelyanov 	struct inet_frag_queue *q;
1531da177e4SLinus Torvalds 
154648700f7SEric Dumazet 	if (!(ipv6_addr_type(&hdr->daddr) & (IPV6_ADDR_MULTICAST |
155648700f7SEric Dumazet 					    IPV6_ADDR_LINKLOCAL)))
156648700f7SEric Dumazet 		key.iif = 0;
1579a375803SPavel Emelyanov 
158648700f7SEric Dumazet 	q = inet_frag_find(&net->ipv6.frags, &key);
1592d44ed22SEric Dumazet 	if (!q)
1609546377cSShan Wei 		return NULL;
1612d44ed22SEric Dumazet 
162c6fda282SPavel Emelyanov 	return container_of(q, struct frag_queue, q);
1631da177e4SLinus Torvalds }
1641da177e4SLinus Torvalds 
165f61944efSHerbert Xu static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
1661da177e4SLinus Torvalds 			   struct frag_hdr *fhdr, int nhoff)
1671da177e4SLinus Torvalds {
1681da177e4SLinus Torvalds 	struct sk_buff *prev, *next;
169f61944efSHerbert Xu 	struct net_device *dev;
170dbd1759eSWillem de Bruijn 	int offset, end, fragsize;
171adf30907SEric Dumazet 	struct net *net = dev_net(skb_dst(skb)->dev);
172eec2e618SHannes Frederic Sowa 	u8 ecn;
1731da177e4SLinus Torvalds 
17406aa8b8aSNikolay Aleksandrov 	if (fq->q.flags & INET_FRAG_COMPLETE)
1751da177e4SLinus Torvalds 		goto err;
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	offset = ntohs(fhdr->frag_off) & ~0x7;
1780660e03fSArnaldo Carvalho de Melo 	end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
1790660e03fSArnaldo Carvalho de Melo 			((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 	if ((unsigned int)end > IPV6_MAXPLEN) {
1821d015503SEric Dumazet 		__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
183a11d206dSYOSHIFUJI Hideaki 				IPSTATS_MIB_INHDRERRORS);
184d56f90a7SArnaldo Carvalho de Melo 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
185d56f90a7SArnaldo Carvalho de Melo 				  ((u8 *)&fhdr->frag_off -
186d56f90a7SArnaldo Carvalho de Melo 				   skb_network_header(skb)));
187f61944efSHerbert Xu 		return -1;
1881da177e4SLinus Torvalds 	}
1891da177e4SLinus Torvalds 
190eec2e618SHannes Frederic Sowa 	ecn = ip6_frag_ecn(ipv6_hdr(skb));
191eec2e618SHannes Frederic Sowa 
192d56f90a7SArnaldo Carvalho de Melo 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
193d56f90a7SArnaldo Carvalho de Melo 		const unsigned char *nh = skb_network_header(skb);
1941da177e4SLinus Torvalds 		skb->csum = csum_sub(skb->csum,
195d56f90a7SArnaldo Carvalho de Melo 				     csum_partial(nh, (u8 *)(fhdr + 1) - nh,
196d56f90a7SArnaldo Carvalho de Melo 						  0));
197d56f90a7SArnaldo Carvalho de Melo 	}
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	/* Is this the final fragment? */
2001da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(IP6_MF))) {
2011da177e4SLinus Torvalds 		/* If we already have some bits beyond end
2021da177e4SLinus Torvalds 		 * or have different end, the segment is corrupted.
2031da177e4SLinus Torvalds 		 */
2045ab11c98SPavel Emelyanov 		if (end < fq->q.len ||
20506aa8b8aSNikolay Aleksandrov 		    ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len))
2061da177e4SLinus Torvalds 			goto err;
20706aa8b8aSNikolay Aleksandrov 		fq->q.flags |= INET_FRAG_LAST_IN;
2085ab11c98SPavel Emelyanov 		fq->q.len = end;
2091da177e4SLinus Torvalds 	} else {
2101da177e4SLinus Torvalds 		/* Check if the fragment is rounded to 8 bytes.
2111da177e4SLinus Torvalds 		 * Required by the RFC.
2121da177e4SLinus Torvalds 		 */
2131da177e4SLinus Torvalds 		if (end & 0x7) {
2141da177e4SLinus Torvalds 			/* RFC2460 says always send parameter problem in
2151da177e4SLinus Torvalds 			 * this case. -DaveM
2161da177e4SLinus Torvalds 			 */
2171d015503SEric Dumazet 			__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
218a11d206dSYOSHIFUJI Hideaki 					IPSTATS_MIB_INHDRERRORS);
2191da177e4SLinus Torvalds 			icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
2201da177e4SLinus Torvalds 					  offsetof(struct ipv6hdr, payload_len));
221f61944efSHerbert Xu 			return -1;
2221da177e4SLinus Torvalds 		}
2235ab11c98SPavel Emelyanov 		if (end > fq->q.len) {
2241da177e4SLinus Torvalds 			/* Some bits beyond end -> corruption. */
22506aa8b8aSNikolay Aleksandrov 			if (fq->q.flags & INET_FRAG_LAST_IN)
2261da177e4SLinus Torvalds 				goto err;
2275ab11c98SPavel Emelyanov 			fq->q.len = end;
2281da177e4SLinus Torvalds 		}
2291da177e4SLinus Torvalds 	}
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 	if (end == offset)
2321da177e4SLinus Torvalds 		goto err;
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	/* Point into the IP datagram 'data' part. */
2351da177e4SLinus Torvalds 	if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
2361da177e4SLinus Torvalds 		goto err;
23742ca89c1SStephen Hemminger 
23842ca89c1SStephen Hemminger 	if (pskb_trim_rcsum(skb, end - offset))
2391da177e4SLinus Torvalds 		goto err;
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	/* Find out which fragments are in front and at the back of us
2421da177e4SLinus Torvalds 	 * in the chain of fragments so far.  We must know where to put
2431da177e4SLinus Torvalds 	 * this fragment, right?
2441da177e4SLinus Torvalds 	 */
245d6bebca9SChangli Gao 	prev = fq->q.fragments_tail;
246219badfaSEric Dumazet 	if (!prev || prev->ip_defrag_offset < offset) {
247d6bebca9SChangli Gao 		next = NULL;
248d6bebca9SChangli Gao 		goto found;
249d6bebca9SChangli Gao 	}
2501da177e4SLinus Torvalds 	prev = NULL;
2515ab11c98SPavel Emelyanov 	for (next = fq->q.fragments; next != NULL; next = next->next) {
252219badfaSEric Dumazet 		if (next->ip_defrag_offset >= offset)
2531da177e4SLinus Torvalds 			break;	/* bingo! */
2541da177e4SLinus Torvalds 		prev = next;
2551da177e4SLinus Torvalds 	}
2561da177e4SLinus Torvalds 
257d6bebca9SChangli Gao found:
2585de658f8SEric Dumazet 	/* RFC5722, Section 4, amended by Errata ID : 3089
25970789d70SNicolas Dichtel 	 *                          When reassembling an IPv6 datagram, if
26070789d70SNicolas Dichtel 	 *   one or more its constituent fragments is determined to be an
26170789d70SNicolas Dichtel 	 *   overlapping fragment, the entire datagram (and any constituent
2625de658f8SEric Dumazet 	 *   fragments) MUST be silently discarded.
2631da177e4SLinus Torvalds 	 */
2641da177e4SLinus Torvalds 
26570789d70SNicolas Dichtel 	/* Check for overlap with preceding fragment. */
26670789d70SNicolas Dichtel 	if (prev &&
267219badfaSEric Dumazet 	    (prev->ip_defrag_offset + prev->len) > offset)
26870789d70SNicolas Dichtel 		goto discard_fq;
2691da177e4SLinus Torvalds 
27070789d70SNicolas Dichtel 	/* Look for overlap with succeeding segment. */
271219badfaSEric Dumazet 	if (next && next->ip_defrag_offset < end)
27270789d70SNicolas Dichtel 		goto discard_fq;
2731da177e4SLinus Torvalds 
274219badfaSEric Dumazet 	/* Note : skb->ip_defrag_offset and skb->dev share the same location */
275219badfaSEric Dumazet 	dev = skb->dev;
276219badfaSEric Dumazet 	if (dev)
277219badfaSEric Dumazet 		fq->iif = dev->ifindex;
278219badfaSEric Dumazet 	/* Makes sure compiler wont do silly aliasing games */
279219badfaSEric Dumazet 	barrier();
280219badfaSEric Dumazet 	skb->ip_defrag_offset = offset;
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	/* Insert this fragment in the chain of fragments. */
2831da177e4SLinus Torvalds 	skb->next = next;
284d6bebca9SChangli Gao 	if (!next)
285d6bebca9SChangli Gao 		fq->q.fragments_tail = skb;
2861da177e4SLinus Torvalds 	if (prev)
2871da177e4SLinus Torvalds 		prev->next = skb;
2881da177e4SLinus Torvalds 	else
2895ab11c98SPavel Emelyanov 		fq->q.fragments = skb;
2901da177e4SLinus Torvalds 
2915ab11c98SPavel Emelyanov 	fq->q.stamp = skb->tstamp;
2925ab11c98SPavel Emelyanov 	fq->q.meat += skb->len;
293eec2e618SHannes Frederic Sowa 	fq->ecn |= ecn;
2940e60d245SFlorian Westphal 	add_frag_mem_limit(fq->q.net, skb->truesize);
2951da177e4SLinus Torvalds 
296dbd1759eSWillem de Bruijn 	fragsize = -skb_network_offset(skb) + skb->len;
297dbd1759eSWillem de Bruijn 	if (fragsize > fq->q.max_size)
298dbd1759eSWillem de Bruijn 		fq->q.max_size = fragsize;
299dbd1759eSWillem de Bruijn 
3001da177e4SLinus Torvalds 	/* The first fragment.
3011da177e4SLinus Torvalds 	 * nhoffset is obtained from the first fragment, of course.
3021da177e4SLinus Torvalds 	 */
3031da177e4SLinus Torvalds 	if (offset == 0) {
3041da177e4SLinus Torvalds 		fq->nhoffset = nhoff;
30506aa8b8aSNikolay Aleksandrov 		fq->q.flags |= INET_FRAG_FIRST_IN;
3061da177e4SLinus Torvalds 	}
307f61944efSHerbert Xu 
30806aa8b8aSNikolay Aleksandrov 	if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
30997599dc7SEric Dumazet 	    fq->q.meat == fq->q.len) {
31097599dc7SEric Dumazet 		int res;
31197599dc7SEric Dumazet 		unsigned long orefdst = skb->_skb_refdst;
312f61944efSHerbert Xu 
31397599dc7SEric Dumazet 		skb->_skb_refdst = 0UL;
31497599dc7SEric Dumazet 		res = ip6_frag_reasm(fq, prev, dev);
31597599dc7SEric Dumazet 		skb->_skb_refdst = orefdst;
31697599dc7SEric Dumazet 		return res;
31797599dc7SEric Dumazet 	}
31897599dc7SEric Dumazet 
31997599dc7SEric Dumazet 	skb_dst_drop(skb);
320f61944efSHerbert Xu 	return -1;
3211da177e4SLinus Torvalds 
32270789d70SNicolas Dichtel discard_fq:
323093ba729SEric Dumazet 	inet_frag_kill(&fq->q);
3241da177e4SLinus Torvalds err:
3251d015503SEric Dumazet 	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
3263bd653c8SDenis V. Lunev 			IPSTATS_MIB_REASMFAILS);
3271da177e4SLinus Torvalds 	kfree_skb(skb);
328f61944efSHerbert Xu 	return -1;
3291da177e4SLinus Torvalds }
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds /*
3321da177e4SLinus Torvalds  *	Check if this packet is complete.
3331da177e4SLinus Torvalds  *	Returns NULL on failure by any reason, and pointer
3341da177e4SLinus Torvalds  *	to current nexthdr field in reassembled frame.
3351da177e4SLinus Torvalds  *
3361da177e4SLinus Torvalds  *	It is called with locked fq, and caller must check that
3371da177e4SLinus Torvalds  *	queue is eligible for reassembly i.e. it is not COMPLETE,
3381da177e4SLinus Torvalds  *	the last and the first frames arrived and all the bits are here.
3391da177e4SLinus Torvalds  */
340f61944efSHerbert Xu static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
3411da177e4SLinus Torvalds 			  struct net_device *dev)
3421da177e4SLinus Torvalds {
3432bad35b7SJorge Boncompte [DTI2] 	struct net *net = container_of(fq->q.net, struct net, ipv6.frags);
3445ab11c98SPavel Emelyanov 	struct sk_buff *fp, *head = fq->q.fragments;
3451da177e4SLinus Torvalds 	int    payload_len;
3461da177e4SLinus Torvalds 	unsigned int nhoff;
347ec16439eSEric Dumazet 	int sum_truesize;
348eec2e618SHannes Frederic Sowa 	u8 ecn;
3491da177e4SLinus Torvalds 
350093ba729SEric Dumazet 	inet_frag_kill(&fq->q);
3511da177e4SLinus Torvalds 
352eec2e618SHannes Frederic Sowa 	ecn = ip_frag_ecn_table[fq->ecn];
353eec2e618SHannes Frederic Sowa 	if (unlikely(ecn == 0xff))
354eec2e618SHannes Frederic Sowa 		goto out_fail;
355eec2e618SHannes Frederic Sowa 
356f61944efSHerbert Xu 	/* Make the one we just received the head. */
357f61944efSHerbert Xu 	if (prev) {
358f61944efSHerbert Xu 		head = prev->next;
359f61944efSHerbert Xu 		fp = skb_clone(head, GFP_ATOMIC);
360f61944efSHerbert Xu 
361f61944efSHerbert Xu 		if (!fp)
362f61944efSHerbert Xu 			goto out_oom;
363f61944efSHerbert Xu 
364f61944efSHerbert Xu 		fp->next = head->next;
365d6bebca9SChangli Gao 		if (!fp->next)
366d6bebca9SChangli Gao 			fq->q.fragments_tail = fp;
367f61944efSHerbert Xu 		prev->next = fp;
368f61944efSHerbert Xu 
3695ab11c98SPavel Emelyanov 		skb_morph(head, fq->q.fragments);
3705ab11c98SPavel Emelyanov 		head->next = fq->q.fragments->next;
371f61944efSHerbert Xu 
372808db80aSEric Dumazet 		consume_skb(fq->q.fragments);
3735ab11c98SPavel Emelyanov 		fq->q.fragments = head;
374f61944efSHerbert Xu 	}
375f61944efSHerbert Xu 
376547b792cSIlpo Järvinen 	WARN_ON(head == NULL);
377219badfaSEric Dumazet 	WARN_ON(head->ip_defrag_offset != 0);
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	/* Unfragmented part is taken from the first segment. */
380d56f90a7SArnaldo Carvalho de Melo 	payload_len = ((head->data - skb_network_header(head)) -
3815ab11c98SPavel Emelyanov 		       sizeof(struct ipv6hdr) + fq->q.len -
382d56f90a7SArnaldo Carvalho de Melo 		       sizeof(struct frag_hdr));
3831da177e4SLinus Torvalds 	if (payload_len > IPV6_MAXPLEN)
3841da177e4SLinus Torvalds 		goto out_oversize;
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	/* Head of list must not be cloned. */
38714bbd6a5SPravin B Shelar 	if (skb_unclone(head, GFP_ATOMIC))
3881da177e4SLinus Torvalds 		goto out_oom;
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	/* If the first fragment is fragmented itself, we split
3911da177e4SLinus Torvalds 	 * it to two chunks: the first with data and paged part
3921da177e4SLinus Torvalds 	 * and the second, holding only fragments. */
39321dc3301SDavid S. Miller 	if (skb_has_frag_list(head)) {
3941da177e4SLinus Torvalds 		struct sk_buff *clone;
3951da177e4SLinus Torvalds 		int i, plen = 0;
3961da177e4SLinus Torvalds 
397e5d08d71SIan Morris 		clone = alloc_skb(0, GFP_ATOMIC);
39863159f29SIan Morris 		if (!clone)
3991da177e4SLinus Torvalds 			goto out_oom;
4001da177e4SLinus Torvalds 		clone->next = head->next;
4011da177e4SLinus Torvalds 		head->next = clone;
4021da177e4SLinus Torvalds 		skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
4034d9092bbSDavid S. Miller 		skb_frag_list_init(head);
4041da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
4059e903e08SEric Dumazet 			plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
4061da177e4SLinus Torvalds 		clone->len = clone->data_len = head->data_len - plen;
4071da177e4SLinus Torvalds 		head->data_len -= clone->len;
4081da177e4SLinus Torvalds 		head->len -= clone->len;
4091da177e4SLinus Torvalds 		clone->csum = 0;
4101da177e4SLinus Torvalds 		clone->ip_summed = head->ip_summed;
4110e60d245SFlorian Westphal 		add_frag_mem_limit(fq->q.net, clone->truesize);
4121da177e4SLinus Torvalds 	}
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	/* We have to remove fragment header from datagram and to relocate
4151da177e4SLinus Torvalds 	 * header in order to calculate ICV correctly. */
4161da177e4SLinus Torvalds 	nhoff = fq->nhoffset;
417b0e380b1SArnaldo Carvalho de Melo 	skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
4181da177e4SLinus Torvalds 	memmove(head->head + sizeof(struct frag_hdr), head->head,
4191da177e4SLinus Torvalds 		(head->data - head->head) - sizeof(struct frag_hdr));
420b678aa57SJason A. Donenfeld 	if (skb_mac_header_was_set(head))
421b0e380b1SArnaldo Carvalho de Melo 		head->mac_header += sizeof(struct frag_hdr);
422b0e380b1SArnaldo Carvalho de Melo 	head->network_header += sizeof(struct frag_hdr);
4231da177e4SLinus Torvalds 
424badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(head);
425d56f90a7SArnaldo Carvalho de Melo 	skb_push(head, head->data - skb_network_header(head));
4261da177e4SLinus Torvalds 
427ec16439eSEric Dumazet 	sum_truesize = head->truesize;
428ec16439eSEric Dumazet 	for (fp = head->next; fp;) {
429ec16439eSEric Dumazet 		bool headstolen;
430ec16439eSEric Dumazet 		int delta;
431ec16439eSEric Dumazet 		struct sk_buff *next = fp->next;
432ec16439eSEric Dumazet 
433ec16439eSEric Dumazet 		sum_truesize += fp->truesize;
4341da177e4SLinus Torvalds 		if (head->ip_summed != fp->ip_summed)
4351da177e4SLinus Torvalds 			head->ip_summed = CHECKSUM_NONE;
43684fa7933SPatrick McHardy 		else if (head->ip_summed == CHECKSUM_COMPLETE)
4371da177e4SLinus Torvalds 			head->csum = csum_add(head->csum, fp->csum);
438ec16439eSEric Dumazet 
439ec16439eSEric Dumazet 		if (skb_try_coalesce(head, fp, &headstolen, &delta)) {
440ec16439eSEric Dumazet 			kfree_skb_partial(fp, headstolen);
441ec16439eSEric Dumazet 		} else {
442ec16439eSEric Dumazet 			if (!skb_shinfo(head)->frag_list)
443ec16439eSEric Dumazet 				skb_shinfo(head)->frag_list = fp;
444ec16439eSEric Dumazet 			head->data_len += fp->len;
445ec16439eSEric Dumazet 			head->len += fp->len;
4461da177e4SLinus Torvalds 			head->truesize += fp->truesize;
4471da177e4SLinus Torvalds 		}
448ec16439eSEric Dumazet 		fp = next;
449ec16439eSEric Dumazet 	}
4500e60d245SFlorian Westphal 	sub_frag_mem_limit(fq->q.net, sum_truesize);
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	head->next = NULL;
4531da177e4SLinus Torvalds 	head->dev = dev;
4545ab11c98SPavel Emelyanov 	head->tstamp = fq->q.stamp;
4550660e03fSArnaldo Carvalho de Melo 	ipv6_hdr(head)->payload_len = htons(payload_len);
456eec2e618SHannes Frederic Sowa 	ipv6_change_dsfield(ipv6_hdr(head), 0xff, ecn);
457951dbc8aSPatrick McHardy 	IP6CB(head)->nhoff = nhoff;
458f46078cfSHannes Frederic Sowa 	IP6CB(head)->flags |= IP6SKB_FRAGMENTED;
459dbd1759eSWillem de Bruijn 	IP6CB(head)->frag_max_size = fq->q.max_size;
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 	/* Yes, and fold redundant checksum back. 8) */
4626b83d28aSDaniel Borkmann 	skb_postpush_rcsum(head, skb_network_header(head),
4636b83d28aSDaniel Borkmann 			   skb_network_header_len(head));
4641da177e4SLinus Torvalds 
465a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
4661d015503SEric Dumazet 	__IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
467a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
4685ab11c98SPavel Emelyanov 	fq->q.fragments = NULL;
469d6bebca9SChangli Gao 	fq->q.fragments_tail = NULL;
4701da177e4SLinus Torvalds 	return 1;
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds out_oversize:
473e87cc472SJoe Perches 	net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len);
4741da177e4SLinus Torvalds 	goto out_fail;
4751da177e4SLinus Torvalds out_oom:
476e87cc472SJoe Perches 	net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n");
4771da177e4SLinus Torvalds out_fail:
478a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
4791d015503SEric Dumazet 	__IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
480a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
4811da177e4SLinus Torvalds 	return -1;
4821da177e4SLinus Torvalds }
4831da177e4SLinus Torvalds 
484e5bbef20SHerbert Xu static int ipv6_frag_rcv(struct sk_buff *skb)
4851da177e4SLinus Torvalds {
4861da177e4SLinus Torvalds 	struct frag_hdr *fhdr;
4871da177e4SLinus Torvalds 	struct frag_queue *fq;
488b71d1d42SEric Dumazet 	const struct ipv6hdr *hdr = ipv6_hdr(skb);
489adf30907SEric Dumazet 	struct net *net = dev_net(skb_dst(skb)->dev);
490648700f7SEric Dumazet 	int iif;
4911da177e4SLinus Torvalds 
492f46078cfSHannes Frederic Sowa 	if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED)
493f46078cfSHannes Frederic Sowa 		goto fail_hdr;
494f46078cfSHannes Frederic Sowa 
4951d015503SEric Dumazet 	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS);
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	/* Jumbo payload inhibits frag. header */
49898b3377cSDenis V. Lunev 	if (hdr->payload_len == 0)
49998b3377cSDenis V. Lunev 		goto fail_hdr;
50098b3377cSDenis V. Lunev 
501ea2ae17dSArnaldo Carvalho de Melo 	if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
50298b3377cSDenis V. Lunev 				 sizeof(struct frag_hdr))))
50398b3377cSDenis V. Lunev 		goto fail_hdr;
5041da177e4SLinus Torvalds 
5050660e03fSArnaldo Carvalho de Melo 	hdr = ipv6_hdr(skb);
5069c70220bSArnaldo Carvalho de Melo 	fhdr = (struct frag_hdr *)skb_transport_header(skb);
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(0xFFF9))) {
5091da177e4SLinus Torvalds 		/* It is not a fragmented frame */
510b0e380b1SArnaldo Carvalho de Melo 		skb->transport_header += sizeof(struct frag_hdr);
5111d015503SEric Dumazet 		__IP6_INC_STATS(net,
512adf30907SEric Dumazet 				ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS);
5131da177e4SLinus Torvalds 
514d56f90a7SArnaldo Carvalho de Melo 		IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
515f46078cfSHannes Frederic Sowa 		IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
5161da177e4SLinus Torvalds 		return 1;
5171da177e4SLinus Torvalds 	}
5181da177e4SLinus Torvalds 
519648700f7SEric Dumazet 	iif = skb->dev ? skb->dev->ifindex : 0;
520648700f7SEric Dumazet 	fq = fq_find(net, fhdr->identification, hdr, iif);
52153b24b8fSIan Morris 	if (fq) {
522f61944efSHerbert Xu 		int ret;
5231da177e4SLinus Torvalds 
5245ab11c98SPavel Emelyanov 		spin_lock(&fq->q.lock);
5251da177e4SLinus Torvalds 
526648700f7SEric Dumazet 		fq->iif = iif;
527f61944efSHerbert Xu 		ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
5281da177e4SLinus Torvalds 
5295ab11c98SPavel Emelyanov 		spin_unlock(&fq->q.lock);
530093ba729SEric Dumazet 		inet_frag_put(&fq->q);
5311da177e4SLinus Torvalds 		return ret;
5321da177e4SLinus Torvalds 	}
5331da177e4SLinus Torvalds 
5341d015503SEric Dumazet 	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS);
5351da177e4SLinus Torvalds 	kfree_skb(skb);
5361da177e4SLinus Torvalds 	return -1;
53798b3377cSDenis V. Lunev 
53898b3377cSDenis V. Lunev fail_hdr:
5391d015503SEric Dumazet 	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
540d2373862SNikolay Aleksandrov 			IPSTATS_MIB_INHDRERRORS);
54198b3377cSDenis V. Lunev 	icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
54298b3377cSDenis V. Lunev 	return -1;
5431da177e4SLinus Torvalds }
5441da177e4SLinus Torvalds 
545cc24becaSIan Morris static const struct inet6_protocol frag_protocol = {
5461da177e4SLinus Torvalds 	.handler	=	ipv6_frag_rcv,
5471da177e4SLinus Torvalds 	.flags		=	INET6_PROTO_NOPOLICY,
5481da177e4SLinus Torvalds };
5491da177e4SLinus Torvalds 
5508d8354d2SPavel Emelyanov #ifdef CONFIG_SYSCTL
5511bab4c75SNikolay Aleksandrov static int zero;
5521bab4c75SNikolay Aleksandrov 
5530a64b4b8SPavel Emelyanov static struct ctl_table ip6_frags_ns_ctl_table[] = {
554e71e0349SDaniel Lezcano 	{
5558d8354d2SPavel Emelyanov 		.procname	= "ip6frag_high_thresh",
556e31e0bdcSPavel Emelyanov 		.data		= &init_net.ipv6.frags.high_thresh,
5573e67f106SEric Dumazet 		.maxlen		= sizeof(unsigned long),
5588d8354d2SPavel Emelyanov 		.mode		= 0644,
5593e67f106SEric Dumazet 		.proc_handler	= proc_doulongvec_minmax,
5601bab4c75SNikolay Aleksandrov 		.extra1		= &init_net.ipv6.frags.low_thresh
5618d8354d2SPavel Emelyanov 	},
5628d8354d2SPavel Emelyanov 	{
5638d8354d2SPavel Emelyanov 		.procname	= "ip6frag_low_thresh",
564e31e0bdcSPavel Emelyanov 		.data		= &init_net.ipv6.frags.low_thresh,
5653e67f106SEric Dumazet 		.maxlen		= sizeof(unsigned long),
5668d8354d2SPavel Emelyanov 		.mode		= 0644,
5671bab4c75SNikolay Aleksandrov 		.proc_handler	= proc_dointvec_minmax,
5681bab4c75SNikolay Aleksandrov 		.extra1		= &zero,
5691bab4c75SNikolay Aleksandrov 		.extra2		= &init_net.ipv6.frags.high_thresh
5708d8354d2SPavel Emelyanov 	},
5718d8354d2SPavel Emelyanov 	{
5728d8354d2SPavel Emelyanov 		.procname	= "ip6frag_time",
573b2fd5321SPavel Emelyanov 		.data		= &init_net.ipv6.frags.timeout,
5748d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
5758d8354d2SPavel Emelyanov 		.mode		= 0644,
5766d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
5778d8354d2SPavel Emelyanov 	},
5787d291ebbSPavel Emelyanov 	{ }
5797d291ebbSPavel Emelyanov };
5807d291ebbSPavel Emelyanov 
581e3a57d18SFlorian Westphal /* secret interval has been deprecated */
582e3a57d18SFlorian Westphal static int ip6_frags_secret_interval_unused;
5837d291ebbSPavel Emelyanov static struct ctl_table ip6_frags_ctl_table[] = {
5848d8354d2SPavel Emelyanov 	{
5858d8354d2SPavel Emelyanov 		.procname	= "ip6frag_secret_interval",
586e3a57d18SFlorian Westphal 		.data		= &ip6_frags_secret_interval_unused,
5878d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
5888d8354d2SPavel Emelyanov 		.mode		= 0644,
5896d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
5908d8354d2SPavel Emelyanov 	},
5918d8354d2SPavel Emelyanov 	{ }
5928d8354d2SPavel Emelyanov };
5937d460db9SDaniel Lezcano 
5942c8c1e72SAlexey Dobriyan static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
5958d8354d2SPavel Emelyanov {
596e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
5978d8354d2SPavel Emelyanov 	struct ctl_table_header *hdr;
5988d8354d2SPavel Emelyanov 
5990a64b4b8SPavel Emelyanov 	table = ip6_frags_ns_ctl_table;
60009ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net)) {
6010a64b4b8SPavel Emelyanov 		table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
60263159f29SIan Morris 		if (!table)
603e4a2d5c2SPavel Emelyanov 			goto err_alloc;
604e4a2d5c2SPavel Emelyanov 
605e31e0bdcSPavel Emelyanov 		table[0].data = &net->ipv6.frags.high_thresh;
6061bab4c75SNikolay Aleksandrov 		table[0].extra1 = &net->ipv6.frags.low_thresh;
6071bab4c75SNikolay Aleksandrov 		table[0].extra2 = &init_net.ipv6.frags.high_thresh;
608e31e0bdcSPavel Emelyanov 		table[1].data = &net->ipv6.frags.low_thresh;
6091bab4c75SNikolay Aleksandrov 		table[1].extra2 = &net->ipv6.frags.high_thresh;
610b2fd5321SPavel Emelyanov 		table[2].data = &net->ipv6.frags.timeout;
611e4a2d5c2SPavel Emelyanov 	}
612e4a2d5c2SPavel Emelyanov 
613ec8f23ceSEric W. Biederman 	hdr = register_net_sysctl(net, "net/ipv6", table);
61463159f29SIan Morris 	if (!hdr)
615e4a2d5c2SPavel Emelyanov 		goto err_reg;
616e4a2d5c2SPavel Emelyanov 
617e4a2d5c2SPavel Emelyanov 	net->ipv6.sysctl.frags_hdr = hdr;
618e4a2d5c2SPavel Emelyanov 	return 0;
619e4a2d5c2SPavel Emelyanov 
620e4a2d5c2SPavel Emelyanov err_reg:
62109ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net))
622e4a2d5c2SPavel Emelyanov 		kfree(table);
623e4a2d5c2SPavel Emelyanov err_alloc:
624e4a2d5c2SPavel Emelyanov 	return -ENOMEM;
625e4a2d5c2SPavel Emelyanov }
626e4a2d5c2SPavel Emelyanov 
6272c8c1e72SAlexey Dobriyan static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
628e4a2d5c2SPavel Emelyanov {
629e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
630e4a2d5c2SPavel Emelyanov 
631e4a2d5c2SPavel Emelyanov 	table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
632e4a2d5c2SPavel Emelyanov 	unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
6333705e11aSYang Hongyang 	if (!net_eq(net, &init_net))
634e4a2d5c2SPavel Emelyanov 		kfree(table);
6358d8354d2SPavel Emelyanov }
6367d291ebbSPavel Emelyanov 
6377d291ebbSPavel Emelyanov static struct ctl_table_header *ip6_ctl_header;
6387d291ebbSPavel Emelyanov 
6397d291ebbSPavel Emelyanov static int ip6_frags_sysctl_register(void)
6407d291ebbSPavel Emelyanov {
64143444757SEric W. Biederman 	ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6",
6427d291ebbSPavel Emelyanov 			ip6_frags_ctl_table);
6437d291ebbSPavel Emelyanov 	return ip6_ctl_header == NULL ? -ENOMEM : 0;
6447d291ebbSPavel Emelyanov }
6457d291ebbSPavel Emelyanov 
6467d291ebbSPavel Emelyanov static void ip6_frags_sysctl_unregister(void)
6477d291ebbSPavel Emelyanov {
6487d291ebbSPavel Emelyanov 	unregister_net_sysctl_table(ip6_ctl_header);
6497d291ebbSPavel Emelyanov }
6508d8354d2SPavel Emelyanov #else
651fc08c258SFabian Frederick static int ip6_frags_ns_sysctl_register(struct net *net)
6528d8354d2SPavel Emelyanov {
6538d8354d2SPavel Emelyanov 	return 0;
6548d8354d2SPavel Emelyanov }
655e4a2d5c2SPavel Emelyanov 
656fc08c258SFabian Frederick static void ip6_frags_ns_sysctl_unregister(struct net *net)
657e4a2d5c2SPavel Emelyanov {
658e4a2d5c2SPavel Emelyanov }
6597d291ebbSPavel Emelyanov 
660fc08c258SFabian Frederick static int ip6_frags_sysctl_register(void)
6617d291ebbSPavel Emelyanov {
6627d291ebbSPavel Emelyanov 	return 0;
6637d291ebbSPavel Emelyanov }
6647d291ebbSPavel Emelyanov 
665fc08c258SFabian Frederick static void ip6_frags_sysctl_unregister(void)
6667d291ebbSPavel Emelyanov {
6677d291ebbSPavel Emelyanov }
6688d8354d2SPavel Emelyanov #endif
6698d8354d2SPavel Emelyanov 
6702c8c1e72SAlexey Dobriyan static int __net_init ipv6_frags_init_net(struct net *net)
6718d8354d2SPavel Emelyanov {
672787bea77SEric Dumazet 	int res;
673787bea77SEric Dumazet 
6747c070aa9SShan Wei 	net->ipv6.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
6757c070aa9SShan Wei 	net->ipv6.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
676b2fd5321SPavel Emelyanov 	net->ipv6.frags.timeout = IPV6_FRAG_TIMEOUT;
677093ba729SEric Dumazet 	net->ipv6.frags.f = &ip6_frags;
6788d8354d2SPavel Emelyanov 
679787bea77SEric Dumazet 	res = inet_frags_init_net(&net->ipv6.frags);
680787bea77SEric Dumazet 	if (res < 0)
681787bea77SEric Dumazet 		return res;
6825a63643eSJesper Dangaard Brouer 
683787bea77SEric Dumazet 	res = ip6_frags_ns_sysctl_register(net);
684787bea77SEric Dumazet 	if (res < 0)
685093ba729SEric Dumazet 		inet_frags_exit_net(&net->ipv6.frags);
686787bea77SEric Dumazet 	return res;
687e71e0349SDaniel Lezcano }
688e71e0349SDaniel Lezcano 
6892c8c1e72SAlexey Dobriyan static void __net_exit ipv6_frags_exit_net(struct net *net)
69081566e83SPavel Emelyanov {
6910a64b4b8SPavel Emelyanov 	ip6_frags_ns_sysctl_unregister(net);
692093ba729SEric Dumazet 	inet_frags_exit_net(&net->ipv6.frags);
69381566e83SPavel Emelyanov }
69481566e83SPavel Emelyanov 
69581566e83SPavel Emelyanov static struct pernet_operations ip6_frags_ops = {
69681566e83SPavel Emelyanov 	.init = ipv6_frags_init_net,
69781566e83SPavel Emelyanov 	.exit = ipv6_frags_exit_net,
69881566e83SPavel Emelyanov };
69981566e83SPavel Emelyanov 
700648700f7SEric Dumazet static u32 ip6_key_hashfn(const void *data, u32 len, u32 seed)
701648700f7SEric Dumazet {
702648700f7SEric Dumazet 	return jhash2(data,
703648700f7SEric Dumazet 		      sizeof(struct frag_v6_compare_key) / sizeof(u32), seed);
704648700f7SEric Dumazet }
705648700f7SEric Dumazet 
706648700f7SEric Dumazet static u32 ip6_obj_hashfn(const void *data, u32 len, u32 seed)
707648700f7SEric Dumazet {
708648700f7SEric Dumazet 	const struct inet_frag_queue *fq = data;
709648700f7SEric Dumazet 
710648700f7SEric Dumazet 	return jhash2((const u32 *)&fq->key.v6,
711648700f7SEric Dumazet 		      sizeof(struct frag_v6_compare_key) / sizeof(u32), seed);
712648700f7SEric Dumazet }
713648700f7SEric Dumazet 
714648700f7SEric Dumazet static int ip6_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
715648700f7SEric Dumazet {
716648700f7SEric Dumazet 	const struct frag_v6_compare_key *key = arg->key;
717648700f7SEric Dumazet 	const struct inet_frag_queue *fq = ptr;
718648700f7SEric Dumazet 
719648700f7SEric Dumazet 	return !!memcmp(&fq->key, key, sizeof(*key));
720648700f7SEric Dumazet }
721648700f7SEric Dumazet 
722648700f7SEric Dumazet const struct rhashtable_params ip6_rhash_params = {
723648700f7SEric Dumazet 	.head_offset		= offsetof(struct inet_frag_queue, node),
724648700f7SEric Dumazet 	.hashfn			= ip6_key_hashfn,
725648700f7SEric Dumazet 	.obj_hashfn		= ip6_obj_hashfn,
726648700f7SEric Dumazet 	.obj_cmpfn		= ip6_obj_cmpfn,
727648700f7SEric Dumazet 	.automatic_shrinking	= true,
728648700f7SEric Dumazet };
729648700f7SEric Dumazet EXPORT_SYMBOL(ip6_rhash_params);
730648700f7SEric Dumazet 
731853cbbaaSDaniel Lezcano int __init ipv6_frag_init(void)
7321da177e4SLinus Torvalds {
733853cbbaaSDaniel Lezcano 	int ret;
7341da177e4SLinus Torvalds 
735c6fda282SPavel Emelyanov 	ip6_frags.constructor = ip6_frag_init;
736c9547709SPavel Emelyanov 	ip6_frags.destructor = NULL;
7371e4b8287SPavel Emelyanov 	ip6_frags.qsize = sizeof(struct frag_queue);
738e521db9dSPavel Emelyanov 	ip6_frags.frag_expire = ip6_frag_expire;
739d4ad4d22SNikolay Aleksandrov 	ip6_frags.frags_cache_name = ip6_frag_cache_name;
740648700f7SEric Dumazet 	ip6_frags.rhash_params = ip6_rhash_params;
741d4ad4d22SNikolay Aleksandrov 	ret = inet_frags_init(&ip6_frags);
742d4ad4d22SNikolay Aleksandrov 	if (ret)
7435b975babSEric Dumazet 		goto out;
7445b975babSEric Dumazet 
7455b975babSEric Dumazet 	ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
7465b975babSEric Dumazet 	if (ret)
7475b975babSEric Dumazet 		goto err_protocol;
7485b975babSEric Dumazet 
7495b975babSEric Dumazet 	ret = ip6_frags_sysctl_register();
7505b975babSEric Dumazet 	if (ret)
7515b975babSEric Dumazet 		goto err_sysctl;
7525b975babSEric Dumazet 
7535b975babSEric Dumazet 	ret = register_pernet_subsys(&ip6_frags_ops);
7545b975babSEric Dumazet 	if (ret)
755d4ad4d22SNikolay Aleksandrov 		goto err_pernet;
7565b975babSEric Dumazet 
757853cbbaaSDaniel Lezcano out:
758853cbbaaSDaniel Lezcano 	return ret;
7590002c630SPavel Emelyanov 
7600002c630SPavel Emelyanov err_pernet:
7617d291ebbSPavel Emelyanov 	ip6_frags_sysctl_unregister();
7627d291ebbSPavel Emelyanov err_sysctl:
7630002c630SPavel Emelyanov 	inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
7645b975babSEric Dumazet err_protocol:
7655b975babSEric Dumazet 	inet_frags_fini(&ip6_frags);
7660002c630SPavel Emelyanov 	goto out;
767853cbbaaSDaniel Lezcano }
768853cbbaaSDaniel Lezcano 
769853cbbaaSDaniel Lezcano void ipv6_frag_exit(void)
770853cbbaaSDaniel Lezcano {
771853cbbaaSDaniel Lezcano 	inet_frags_fini(&ip6_frags);
7727d291ebbSPavel Emelyanov 	ip6_frags_sysctl_unregister();
77381566e83SPavel Emelyanov 	unregister_pernet_subsys(&ip6_frags_ops);
774853cbbaaSDaniel Lezcano 	inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
7751da177e4SLinus Torvalds }
776