xref: /openbmc/linux/net/ipv6/reassembly.c (revision 415787d7)
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,
166415787d7SEric Dumazet 			  struct frag_hdr *fhdr, int nhoff,
167415787d7SEric Dumazet 			  u32 *prob_offset)
1681da177e4SLinus Torvalds {
1691da177e4SLinus Torvalds 	struct sk_buff *prev, *next;
170f61944efSHerbert Xu 	struct net_device *dev;
171dbd1759eSWillem de Bruijn 	int offset, end, fragsize;
172adf30907SEric Dumazet 	struct net *net = dev_net(skb_dst(skb)->dev);
173eec2e618SHannes Frederic Sowa 	u8 ecn;
1741da177e4SLinus Torvalds 
17506aa8b8aSNikolay Aleksandrov 	if (fq->q.flags & INET_FRAG_COMPLETE)
1761da177e4SLinus Torvalds 		goto err;
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	offset = ntohs(fhdr->frag_off) & ~0x7;
1790660e03fSArnaldo Carvalho de Melo 	end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
1800660e03fSArnaldo Carvalho de Melo 			((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	if ((unsigned int)end > IPV6_MAXPLEN) {
183415787d7SEric Dumazet 		*prob_offset = (u8 *)&fhdr->frag_off - skb_network_header(skb);
184f61944efSHerbert Xu 		return -1;
1851da177e4SLinus Torvalds 	}
1861da177e4SLinus Torvalds 
187eec2e618SHannes Frederic Sowa 	ecn = ip6_frag_ecn(ipv6_hdr(skb));
188eec2e618SHannes Frederic Sowa 
189d56f90a7SArnaldo Carvalho de Melo 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
190d56f90a7SArnaldo Carvalho de Melo 		const unsigned char *nh = skb_network_header(skb);
1911da177e4SLinus Torvalds 		skb->csum = csum_sub(skb->csum,
192d56f90a7SArnaldo Carvalho de Melo 				     csum_partial(nh, (u8 *)(fhdr + 1) - nh,
193d56f90a7SArnaldo Carvalho de Melo 						  0));
194d56f90a7SArnaldo Carvalho de Melo 	}
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	/* Is this the final fragment? */
1971da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(IP6_MF))) {
1981da177e4SLinus Torvalds 		/* If we already have some bits beyond end
1991da177e4SLinus Torvalds 		 * or have different end, the segment is corrupted.
2001da177e4SLinus Torvalds 		 */
2015ab11c98SPavel Emelyanov 		if (end < fq->q.len ||
20206aa8b8aSNikolay Aleksandrov 		    ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len))
2031da177e4SLinus Torvalds 			goto err;
20406aa8b8aSNikolay Aleksandrov 		fq->q.flags |= INET_FRAG_LAST_IN;
2055ab11c98SPavel Emelyanov 		fq->q.len = end;
2061da177e4SLinus Torvalds 	} else {
2071da177e4SLinus Torvalds 		/* Check if the fragment is rounded to 8 bytes.
2081da177e4SLinus Torvalds 		 * Required by the RFC.
2091da177e4SLinus Torvalds 		 */
2101da177e4SLinus Torvalds 		if (end & 0x7) {
2111da177e4SLinus Torvalds 			/* RFC2460 says always send parameter problem in
2121da177e4SLinus Torvalds 			 * this case. -DaveM
2131da177e4SLinus Torvalds 			 */
214415787d7SEric Dumazet 			*prob_offset = offsetof(struct ipv6hdr, payload_len);
215f61944efSHerbert Xu 			return -1;
2161da177e4SLinus Torvalds 		}
2175ab11c98SPavel Emelyanov 		if (end > fq->q.len) {
2181da177e4SLinus Torvalds 			/* Some bits beyond end -> corruption. */
21906aa8b8aSNikolay Aleksandrov 			if (fq->q.flags & INET_FRAG_LAST_IN)
2201da177e4SLinus Torvalds 				goto err;
2215ab11c98SPavel Emelyanov 			fq->q.len = end;
2221da177e4SLinus Torvalds 		}
2231da177e4SLinus Torvalds 	}
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	if (end == offset)
2261da177e4SLinus Torvalds 		goto err;
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	/* Point into the IP datagram 'data' part. */
2291da177e4SLinus Torvalds 	if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
2301da177e4SLinus Torvalds 		goto err;
23142ca89c1SStephen Hemminger 
23242ca89c1SStephen Hemminger 	if (pskb_trim_rcsum(skb, end - offset))
2331da177e4SLinus Torvalds 		goto err;
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	/* Find out which fragments are in front and at the back of us
2361da177e4SLinus Torvalds 	 * in the chain of fragments so far.  We must know where to put
2371da177e4SLinus Torvalds 	 * this fragment, right?
2381da177e4SLinus Torvalds 	 */
239d6bebca9SChangli Gao 	prev = fq->q.fragments_tail;
240219badfaSEric Dumazet 	if (!prev || prev->ip_defrag_offset < offset) {
241d6bebca9SChangli Gao 		next = NULL;
242d6bebca9SChangli Gao 		goto found;
243d6bebca9SChangli Gao 	}
2441da177e4SLinus Torvalds 	prev = NULL;
2455ab11c98SPavel Emelyanov 	for (next = fq->q.fragments; next != NULL; next = next->next) {
246219badfaSEric Dumazet 		if (next->ip_defrag_offset >= offset)
2471da177e4SLinus Torvalds 			break;	/* bingo! */
2481da177e4SLinus Torvalds 		prev = next;
2491da177e4SLinus Torvalds 	}
2501da177e4SLinus Torvalds 
251d6bebca9SChangli Gao found:
2525de658f8SEric Dumazet 	/* RFC5722, Section 4, amended by Errata ID : 3089
25370789d70SNicolas Dichtel 	 *                          When reassembling an IPv6 datagram, if
25470789d70SNicolas Dichtel 	 *   one or more its constituent fragments is determined to be an
25570789d70SNicolas Dichtel 	 *   overlapping fragment, the entire datagram (and any constituent
2565de658f8SEric Dumazet 	 *   fragments) MUST be silently discarded.
2571da177e4SLinus Torvalds 	 */
2581da177e4SLinus Torvalds 
25970789d70SNicolas Dichtel 	/* Check for overlap with preceding fragment. */
26070789d70SNicolas Dichtel 	if (prev &&
261219badfaSEric Dumazet 	    (prev->ip_defrag_offset + prev->len) > offset)
26270789d70SNicolas Dichtel 		goto discard_fq;
2631da177e4SLinus Torvalds 
26470789d70SNicolas Dichtel 	/* Look for overlap with succeeding segment. */
265219badfaSEric Dumazet 	if (next && next->ip_defrag_offset < end)
26670789d70SNicolas Dichtel 		goto discard_fq;
2671da177e4SLinus Torvalds 
268219badfaSEric Dumazet 	/* Note : skb->ip_defrag_offset and skb->dev share the same location */
269219badfaSEric Dumazet 	dev = skb->dev;
270219badfaSEric Dumazet 	if (dev)
271219badfaSEric Dumazet 		fq->iif = dev->ifindex;
272219badfaSEric Dumazet 	/* Makes sure compiler wont do silly aliasing games */
273219badfaSEric Dumazet 	barrier();
274219badfaSEric Dumazet 	skb->ip_defrag_offset = offset;
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 	/* Insert this fragment in the chain of fragments. */
2771da177e4SLinus Torvalds 	skb->next = next;
278d6bebca9SChangli Gao 	if (!next)
279d6bebca9SChangli Gao 		fq->q.fragments_tail = skb;
2801da177e4SLinus Torvalds 	if (prev)
2811da177e4SLinus Torvalds 		prev->next = skb;
2821da177e4SLinus Torvalds 	else
2835ab11c98SPavel Emelyanov 		fq->q.fragments = skb;
2841da177e4SLinus Torvalds 
2855ab11c98SPavel Emelyanov 	fq->q.stamp = skb->tstamp;
2865ab11c98SPavel Emelyanov 	fq->q.meat += skb->len;
287eec2e618SHannes Frederic Sowa 	fq->ecn |= ecn;
2880e60d245SFlorian Westphal 	add_frag_mem_limit(fq->q.net, skb->truesize);
2891da177e4SLinus Torvalds 
290dbd1759eSWillem de Bruijn 	fragsize = -skb_network_offset(skb) + skb->len;
291dbd1759eSWillem de Bruijn 	if (fragsize > fq->q.max_size)
292dbd1759eSWillem de Bruijn 		fq->q.max_size = fragsize;
293dbd1759eSWillem de Bruijn 
2941da177e4SLinus Torvalds 	/* The first fragment.
2951da177e4SLinus Torvalds 	 * nhoffset is obtained from the first fragment, of course.
2961da177e4SLinus Torvalds 	 */
2971da177e4SLinus Torvalds 	if (offset == 0) {
2981da177e4SLinus Torvalds 		fq->nhoffset = nhoff;
29906aa8b8aSNikolay Aleksandrov 		fq->q.flags |= INET_FRAG_FIRST_IN;
3001da177e4SLinus Torvalds 	}
301f61944efSHerbert Xu 
30206aa8b8aSNikolay Aleksandrov 	if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
30397599dc7SEric Dumazet 	    fq->q.meat == fq->q.len) {
30497599dc7SEric Dumazet 		int res;
30597599dc7SEric Dumazet 		unsigned long orefdst = skb->_skb_refdst;
306f61944efSHerbert Xu 
30797599dc7SEric Dumazet 		skb->_skb_refdst = 0UL;
30897599dc7SEric Dumazet 		res = ip6_frag_reasm(fq, prev, dev);
30997599dc7SEric Dumazet 		skb->_skb_refdst = orefdst;
31097599dc7SEric Dumazet 		return res;
31197599dc7SEric Dumazet 	}
31297599dc7SEric Dumazet 
31397599dc7SEric Dumazet 	skb_dst_drop(skb);
314f61944efSHerbert Xu 	return -1;
3151da177e4SLinus Torvalds 
31670789d70SNicolas Dichtel discard_fq:
317093ba729SEric Dumazet 	inet_frag_kill(&fq->q);
3181da177e4SLinus Torvalds err:
3191d015503SEric Dumazet 	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
3203bd653c8SDenis V. Lunev 			IPSTATS_MIB_REASMFAILS);
3211da177e4SLinus Torvalds 	kfree_skb(skb);
322f61944efSHerbert Xu 	return -1;
3231da177e4SLinus Torvalds }
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds /*
3261da177e4SLinus Torvalds  *	Check if this packet is complete.
3271da177e4SLinus Torvalds  *	Returns NULL on failure by any reason, and pointer
3281da177e4SLinus Torvalds  *	to current nexthdr field in reassembled frame.
3291da177e4SLinus Torvalds  *
3301da177e4SLinus Torvalds  *	It is called with locked fq, and caller must check that
3311da177e4SLinus Torvalds  *	queue is eligible for reassembly i.e. it is not COMPLETE,
3321da177e4SLinus Torvalds  *	the last and the first frames arrived and all the bits are here.
3331da177e4SLinus Torvalds  */
334f61944efSHerbert Xu static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
3351da177e4SLinus Torvalds 			  struct net_device *dev)
3361da177e4SLinus Torvalds {
3372bad35b7SJorge Boncompte [DTI2] 	struct net *net = container_of(fq->q.net, struct net, ipv6.frags);
3385ab11c98SPavel Emelyanov 	struct sk_buff *fp, *head = fq->q.fragments;
3391da177e4SLinus Torvalds 	int    payload_len;
3401da177e4SLinus Torvalds 	unsigned int nhoff;
341ec16439eSEric Dumazet 	int sum_truesize;
342eec2e618SHannes Frederic Sowa 	u8 ecn;
3431da177e4SLinus Torvalds 
344093ba729SEric Dumazet 	inet_frag_kill(&fq->q);
3451da177e4SLinus Torvalds 
346eec2e618SHannes Frederic Sowa 	ecn = ip_frag_ecn_table[fq->ecn];
347eec2e618SHannes Frederic Sowa 	if (unlikely(ecn == 0xff))
348eec2e618SHannes Frederic Sowa 		goto out_fail;
349eec2e618SHannes Frederic Sowa 
350f61944efSHerbert Xu 	/* Make the one we just received the head. */
351f61944efSHerbert Xu 	if (prev) {
352f61944efSHerbert Xu 		head = prev->next;
353f61944efSHerbert Xu 		fp = skb_clone(head, GFP_ATOMIC);
354f61944efSHerbert Xu 
355f61944efSHerbert Xu 		if (!fp)
356f61944efSHerbert Xu 			goto out_oom;
357f61944efSHerbert Xu 
358f61944efSHerbert Xu 		fp->next = head->next;
359d6bebca9SChangli Gao 		if (!fp->next)
360d6bebca9SChangli Gao 			fq->q.fragments_tail = fp;
361f61944efSHerbert Xu 		prev->next = fp;
362f61944efSHerbert Xu 
3635ab11c98SPavel Emelyanov 		skb_morph(head, fq->q.fragments);
3645ab11c98SPavel Emelyanov 		head->next = fq->q.fragments->next;
365f61944efSHerbert Xu 
366808db80aSEric Dumazet 		consume_skb(fq->q.fragments);
3675ab11c98SPavel Emelyanov 		fq->q.fragments = head;
368f61944efSHerbert Xu 	}
369f61944efSHerbert Xu 
370547b792cSIlpo Järvinen 	WARN_ON(head == NULL);
371219badfaSEric Dumazet 	WARN_ON(head->ip_defrag_offset != 0);
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	/* Unfragmented part is taken from the first segment. */
374d56f90a7SArnaldo Carvalho de Melo 	payload_len = ((head->data - skb_network_header(head)) -
3755ab11c98SPavel Emelyanov 		       sizeof(struct ipv6hdr) + fq->q.len -
376d56f90a7SArnaldo Carvalho de Melo 		       sizeof(struct frag_hdr));
3771da177e4SLinus Torvalds 	if (payload_len > IPV6_MAXPLEN)
3781da177e4SLinus Torvalds 		goto out_oversize;
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	/* Head of list must not be cloned. */
38114bbd6a5SPravin B Shelar 	if (skb_unclone(head, GFP_ATOMIC))
3821da177e4SLinus Torvalds 		goto out_oom;
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	/* If the first fragment is fragmented itself, we split
3851da177e4SLinus Torvalds 	 * it to two chunks: the first with data and paged part
3861da177e4SLinus Torvalds 	 * and the second, holding only fragments. */
38721dc3301SDavid S. Miller 	if (skb_has_frag_list(head)) {
3881da177e4SLinus Torvalds 		struct sk_buff *clone;
3891da177e4SLinus Torvalds 		int i, plen = 0;
3901da177e4SLinus Torvalds 
391e5d08d71SIan Morris 		clone = alloc_skb(0, GFP_ATOMIC);
39263159f29SIan Morris 		if (!clone)
3931da177e4SLinus Torvalds 			goto out_oom;
3941da177e4SLinus Torvalds 		clone->next = head->next;
3951da177e4SLinus Torvalds 		head->next = clone;
3961da177e4SLinus Torvalds 		skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
3974d9092bbSDavid S. Miller 		skb_frag_list_init(head);
3981da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
3999e903e08SEric Dumazet 			plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
4001da177e4SLinus Torvalds 		clone->len = clone->data_len = head->data_len - plen;
4011da177e4SLinus Torvalds 		head->data_len -= clone->len;
4021da177e4SLinus Torvalds 		head->len -= clone->len;
4031da177e4SLinus Torvalds 		clone->csum = 0;
4041da177e4SLinus Torvalds 		clone->ip_summed = head->ip_summed;
4050e60d245SFlorian Westphal 		add_frag_mem_limit(fq->q.net, clone->truesize);
4061da177e4SLinus Torvalds 	}
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	/* We have to remove fragment header from datagram and to relocate
4091da177e4SLinus Torvalds 	 * header in order to calculate ICV correctly. */
4101da177e4SLinus Torvalds 	nhoff = fq->nhoffset;
411b0e380b1SArnaldo Carvalho de Melo 	skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
4121da177e4SLinus Torvalds 	memmove(head->head + sizeof(struct frag_hdr), head->head,
4131da177e4SLinus Torvalds 		(head->data - head->head) - sizeof(struct frag_hdr));
414b678aa57SJason A. Donenfeld 	if (skb_mac_header_was_set(head))
415b0e380b1SArnaldo Carvalho de Melo 		head->mac_header += sizeof(struct frag_hdr);
416b0e380b1SArnaldo Carvalho de Melo 	head->network_header += sizeof(struct frag_hdr);
4171da177e4SLinus Torvalds 
418badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(head);
419d56f90a7SArnaldo Carvalho de Melo 	skb_push(head, head->data - skb_network_header(head));
4201da177e4SLinus Torvalds 
421ec16439eSEric Dumazet 	sum_truesize = head->truesize;
422ec16439eSEric Dumazet 	for (fp = head->next; fp;) {
423ec16439eSEric Dumazet 		bool headstolen;
424ec16439eSEric Dumazet 		int delta;
425ec16439eSEric Dumazet 		struct sk_buff *next = fp->next;
426ec16439eSEric Dumazet 
427ec16439eSEric Dumazet 		sum_truesize += fp->truesize;
4281da177e4SLinus Torvalds 		if (head->ip_summed != fp->ip_summed)
4291da177e4SLinus Torvalds 			head->ip_summed = CHECKSUM_NONE;
43084fa7933SPatrick McHardy 		else if (head->ip_summed == CHECKSUM_COMPLETE)
4311da177e4SLinus Torvalds 			head->csum = csum_add(head->csum, fp->csum);
432ec16439eSEric Dumazet 
433ec16439eSEric Dumazet 		if (skb_try_coalesce(head, fp, &headstolen, &delta)) {
434ec16439eSEric Dumazet 			kfree_skb_partial(fp, headstolen);
435ec16439eSEric Dumazet 		} else {
436ec16439eSEric Dumazet 			if (!skb_shinfo(head)->frag_list)
437ec16439eSEric Dumazet 				skb_shinfo(head)->frag_list = fp;
438ec16439eSEric Dumazet 			head->data_len += fp->len;
439ec16439eSEric Dumazet 			head->len += fp->len;
4401da177e4SLinus Torvalds 			head->truesize += fp->truesize;
4411da177e4SLinus Torvalds 		}
442ec16439eSEric Dumazet 		fp = next;
443ec16439eSEric Dumazet 	}
4440e60d245SFlorian Westphal 	sub_frag_mem_limit(fq->q.net, sum_truesize);
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	head->next = NULL;
4471da177e4SLinus Torvalds 	head->dev = dev;
4485ab11c98SPavel Emelyanov 	head->tstamp = fq->q.stamp;
4490660e03fSArnaldo Carvalho de Melo 	ipv6_hdr(head)->payload_len = htons(payload_len);
450eec2e618SHannes Frederic Sowa 	ipv6_change_dsfield(ipv6_hdr(head), 0xff, ecn);
451951dbc8aSPatrick McHardy 	IP6CB(head)->nhoff = nhoff;
452f46078cfSHannes Frederic Sowa 	IP6CB(head)->flags |= IP6SKB_FRAGMENTED;
453dbd1759eSWillem de Bruijn 	IP6CB(head)->frag_max_size = fq->q.max_size;
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 	/* Yes, and fold redundant checksum back. 8) */
4566b83d28aSDaniel Borkmann 	skb_postpush_rcsum(head, skb_network_header(head),
4576b83d28aSDaniel Borkmann 			   skb_network_header_len(head));
4581da177e4SLinus Torvalds 
459a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
4601d015503SEric Dumazet 	__IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
461a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
4625ab11c98SPavel Emelyanov 	fq->q.fragments = NULL;
463d6bebca9SChangli Gao 	fq->q.fragments_tail = NULL;
4641da177e4SLinus Torvalds 	return 1;
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds out_oversize:
467e87cc472SJoe Perches 	net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len);
4681da177e4SLinus Torvalds 	goto out_fail;
4691da177e4SLinus Torvalds out_oom:
470e87cc472SJoe Perches 	net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n");
4711da177e4SLinus Torvalds out_fail:
472a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
4731d015503SEric Dumazet 	__IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
474a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
4751da177e4SLinus Torvalds 	return -1;
4761da177e4SLinus Torvalds }
4771da177e4SLinus Torvalds 
478e5bbef20SHerbert Xu static int ipv6_frag_rcv(struct sk_buff *skb)
4791da177e4SLinus Torvalds {
4801da177e4SLinus Torvalds 	struct frag_hdr *fhdr;
4811da177e4SLinus Torvalds 	struct frag_queue *fq;
482b71d1d42SEric Dumazet 	const struct ipv6hdr *hdr = ipv6_hdr(skb);
483adf30907SEric Dumazet 	struct net *net = dev_net(skb_dst(skb)->dev);
484648700f7SEric Dumazet 	int iif;
4851da177e4SLinus Torvalds 
486f46078cfSHannes Frederic Sowa 	if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED)
487f46078cfSHannes Frederic Sowa 		goto fail_hdr;
488f46078cfSHannes Frederic Sowa 
4891d015503SEric Dumazet 	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS);
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	/* Jumbo payload inhibits frag. header */
49298b3377cSDenis V. Lunev 	if (hdr->payload_len == 0)
49398b3377cSDenis V. Lunev 		goto fail_hdr;
49498b3377cSDenis V. Lunev 
495ea2ae17dSArnaldo Carvalho de Melo 	if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
49698b3377cSDenis V. Lunev 				 sizeof(struct frag_hdr))))
49798b3377cSDenis V. Lunev 		goto fail_hdr;
4981da177e4SLinus Torvalds 
4990660e03fSArnaldo Carvalho de Melo 	hdr = ipv6_hdr(skb);
5009c70220bSArnaldo Carvalho de Melo 	fhdr = (struct frag_hdr *)skb_transport_header(skb);
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(0xFFF9))) {
5031da177e4SLinus Torvalds 		/* It is not a fragmented frame */
504b0e380b1SArnaldo Carvalho de Melo 		skb->transport_header += sizeof(struct frag_hdr);
5051d015503SEric Dumazet 		__IP6_INC_STATS(net,
506adf30907SEric Dumazet 				ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS);
5071da177e4SLinus Torvalds 
508d56f90a7SArnaldo Carvalho de Melo 		IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
509f46078cfSHannes Frederic Sowa 		IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
5101da177e4SLinus Torvalds 		return 1;
5111da177e4SLinus Torvalds 	}
5121da177e4SLinus Torvalds 
513648700f7SEric Dumazet 	iif = skb->dev ? skb->dev->ifindex : 0;
514648700f7SEric Dumazet 	fq = fq_find(net, fhdr->identification, hdr, iif);
51553b24b8fSIan Morris 	if (fq) {
516415787d7SEric Dumazet 		u32 prob_offset = 0;
517f61944efSHerbert Xu 		int ret;
5181da177e4SLinus Torvalds 
5195ab11c98SPavel Emelyanov 		spin_lock(&fq->q.lock);
5201da177e4SLinus Torvalds 
521648700f7SEric Dumazet 		fq->iif = iif;
522415787d7SEric Dumazet 		ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff,
523415787d7SEric Dumazet 				     &prob_offset);
5241da177e4SLinus Torvalds 
5255ab11c98SPavel Emelyanov 		spin_unlock(&fq->q.lock);
526093ba729SEric Dumazet 		inet_frag_put(&fq->q);
527415787d7SEric Dumazet 		if (prob_offset) {
528415787d7SEric Dumazet 			__IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev),
529415787d7SEric Dumazet 					IPSTATS_MIB_INHDRERRORS);
530415787d7SEric Dumazet 			icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, prob_offset);
531415787d7SEric Dumazet 		}
5321da177e4SLinus Torvalds 		return ret;
5331da177e4SLinus Torvalds 	}
5341da177e4SLinus Torvalds 
5351d015503SEric Dumazet 	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS);
5361da177e4SLinus Torvalds 	kfree_skb(skb);
5371da177e4SLinus Torvalds 	return -1;
53898b3377cSDenis V. Lunev 
53998b3377cSDenis V. Lunev fail_hdr:
540bdb7cc64SStephen Suryaputra 	__IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev),
541d2373862SNikolay Aleksandrov 			IPSTATS_MIB_INHDRERRORS);
54298b3377cSDenis V. Lunev 	icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
54398b3377cSDenis V. Lunev 	return -1;
5441da177e4SLinus Torvalds }
5451da177e4SLinus Torvalds 
546cc24becaSIan Morris static const struct inet6_protocol frag_protocol = {
5471da177e4SLinus Torvalds 	.handler	=	ipv6_frag_rcv,
5481da177e4SLinus Torvalds 	.flags		=	INET6_PROTO_NOPOLICY,
5491da177e4SLinus Torvalds };
5501da177e4SLinus Torvalds 
5518d8354d2SPavel Emelyanov #ifdef CONFIG_SYSCTL
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,
5676e00f7ddSEric Dumazet 		.proc_handler	= proc_doulongvec_minmax,
5681bab4c75SNikolay Aleksandrov 		.extra2		= &init_net.ipv6.frags.high_thresh
5698d8354d2SPavel Emelyanov 	},
5708d8354d2SPavel Emelyanov 	{
5718d8354d2SPavel Emelyanov 		.procname	= "ip6frag_time",
572b2fd5321SPavel Emelyanov 		.data		= &init_net.ipv6.frags.timeout,
5738d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
5748d8354d2SPavel Emelyanov 		.mode		= 0644,
5756d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
5768d8354d2SPavel Emelyanov 	},
5777d291ebbSPavel Emelyanov 	{ }
5787d291ebbSPavel Emelyanov };
5797d291ebbSPavel Emelyanov 
580e3a57d18SFlorian Westphal /* secret interval has been deprecated */
581e3a57d18SFlorian Westphal static int ip6_frags_secret_interval_unused;
5827d291ebbSPavel Emelyanov static struct ctl_table ip6_frags_ctl_table[] = {
5838d8354d2SPavel Emelyanov 	{
5848d8354d2SPavel Emelyanov 		.procname	= "ip6frag_secret_interval",
585e3a57d18SFlorian Westphal 		.data		= &ip6_frags_secret_interval_unused,
5868d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
5878d8354d2SPavel Emelyanov 		.mode		= 0644,
5886d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
5898d8354d2SPavel Emelyanov 	},
5908d8354d2SPavel Emelyanov 	{ }
5918d8354d2SPavel Emelyanov };
5927d460db9SDaniel Lezcano 
5932c8c1e72SAlexey Dobriyan static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
5948d8354d2SPavel Emelyanov {
595e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
5968d8354d2SPavel Emelyanov 	struct ctl_table_header *hdr;
5978d8354d2SPavel Emelyanov 
5980a64b4b8SPavel Emelyanov 	table = ip6_frags_ns_ctl_table;
59909ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net)) {
6000a64b4b8SPavel Emelyanov 		table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
60163159f29SIan Morris 		if (!table)
602e4a2d5c2SPavel Emelyanov 			goto err_alloc;
603e4a2d5c2SPavel Emelyanov 
604e31e0bdcSPavel Emelyanov 		table[0].data = &net->ipv6.frags.high_thresh;
6051bab4c75SNikolay Aleksandrov 		table[0].extra1 = &net->ipv6.frags.low_thresh;
6061bab4c75SNikolay Aleksandrov 		table[0].extra2 = &init_net.ipv6.frags.high_thresh;
607e31e0bdcSPavel Emelyanov 		table[1].data = &net->ipv6.frags.low_thresh;
6081bab4c75SNikolay Aleksandrov 		table[1].extra2 = &net->ipv6.frags.high_thresh;
609b2fd5321SPavel Emelyanov 		table[2].data = &net->ipv6.frags.timeout;
610e4a2d5c2SPavel Emelyanov 	}
611e4a2d5c2SPavel Emelyanov 
612ec8f23ceSEric W. Biederman 	hdr = register_net_sysctl(net, "net/ipv6", table);
61363159f29SIan Morris 	if (!hdr)
614e4a2d5c2SPavel Emelyanov 		goto err_reg;
615e4a2d5c2SPavel Emelyanov 
616e4a2d5c2SPavel Emelyanov 	net->ipv6.sysctl.frags_hdr = hdr;
617e4a2d5c2SPavel Emelyanov 	return 0;
618e4a2d5c2SPavel Emelyanov 
619e4a2d5c2SPavel Emelyanov err_reg:
62009ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net))
621e4a2d5c2SPavel Emelyanov 		kfree(table);
622e4a2d5c2SPavel Emelyanov err_alloc:
623e4a2d5c2SPavel Emelyanov 	return -ENOMEM;
624e4a2d5c2SPavel Emelyanov }
625e4a2d5c2SPavel Emelyanov 
6262c8c1e72SAlexey Dobriyan static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
627e4a2d5c2SPavel Emelyanov {
628e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
629e4a2d5c2SPavel Emelyanov 
630e4a2d5c2SPavel Emelyanov 	table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
631e4a2d5c2SPavel Emelyanov 	unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
6323705e11aSYang Hongyang 	if (!net_eq(net, &init_net))
633e4a2d5c2SPavel Emelyanov 		kfree(table);
6348d8354d2SPavel Emelyanov }
6357d291ebbSPavel Emelyanov 
6367d291ebbSPavel Emelyanov static struct ctl_table_header *ip6_ctl_header;
6377d291ebbSPavel Emelyanov 
6387d291ebbSPavel Emelyanov static int ip6_frags_sysctl_register(void)
6397d291ebbSPavel Emelyanov {
64043444757SEric W. Biederman 	ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6",
6417d291ebbSPavel Emelyanov 			ip6_frags_ctl_table);
6427d291ebbSPavel Emelyanov 	return ip6_ctl_header == NULL ? -ENOMEM : 0;
6437d291ebbSPavel Emelyanov }
6447d291ebbSPavel Emelyanov 
6457d291ebbSPavel Emelyanov static void ip6_frags_sysctl_unregister(void)
6467d291ebbSPavel Emelyanov {
6477d291ebbSPavel Emelyanov 	unregister_net_sysctl_table(ip6_ctl_header);
6487d291ebbSPavel Emelyanov }
6498d8354d2SPavel Emelyanov #else
650fc08c258SFabian Frederick static int ip6_frags_ns_sysctl_register(struct net *net)
6518d8354d2SPavel Emelyanov {
6528d8354d2SPavel Emelyanov 	return 0;
6538d8354d2SPavel Emelyanov }
654e4a2d5c2SPavel Emelyanov 
655fc08c258SFabian Frederick static void ip6_frags_ns_sysctl_unregister(struct net *net)
656e4a2d5c2SPavel Emelyanov {
657e4a2d5c2SPavel Emelyanov }
6587d291ebbSPavel Emelyanov 
659fc08c258SFabian Frederick static int ip6_frags_sysctl_register(void)
6607d291ebbSPavel Emelyanov {
6617d291ebbSPavel Emelyanov 	return 0;
6627d291ebbSPavel Emelyanov }
6637d291ebbSPavel Emelyanov 
664fc08c258SFabian Frederick static void ip6_frags_sysctl_unregister(void)
6657d291ebbSPavel Emelyanov {
6667d291ebbSPavel Emelyanov }
6678d8354d2SPavel Emelyanov #endif
6688d8354d2SPavel Emelyanov 
6692c8c1e72SAlexey Dobriyan static int __net_init ipv6_frags_init_net(struct net *net)
6708d8354d2SPavel Emelyanov {
671787bea77SEric Dumazet 	int res;
672787bea77SEric Dumazet 
6737c070aa9SShan Wei 	net->ipv6.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
6747c070aa9SShan Wei 	net->ipv6.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
675b2fd5321SPavel Emelyanov 	net->ipv6.frags.timeout = IPV6_FRAG_TIMEOUT;
676093ba729SEric Dumazet 	net->ipv6.frags.f = &ip6_frags;
6778d8354d2SPavel Emelyanov 
678787bea77SEric Dumazet 	res = inet_frags_init_net(&net->ipv6.frags);
679787bea77SEric Dumazet 	if (res < 0)
680787bea77SEric Dumazet 		return res;
6815a63643eSJesper Dangaard Brouer 
682787bea77SEric Dumazet 	res = ip6_frags_ns_sysctl_register(net);
683787bea77SEric Dumazet 	if (res < 0)
684093ba729SEric Dumazet 		inet_frags_exit_net(&net->ipv6.frags);
685787bea77SEric Dumazet 	return res;
686e71e0349SDaniel Lezcano }
687e71e0349SDaniel Lezcano 
6882c8c1e72SAlexey Dobriyan static void __net_exit ipv6_frags_exit_net(struct net *net)
68981566e83SPavel Emelyanov {
6900a64b4b8SPavel Emelyanov 	ip6_frags_ns_sysctl_unregister(net);
691093ba729SEric Dumazet 	inet_frags_exit_net(&net->ipv6.frags);
69281566e83SPavel Emelyanov }
69381566e83SPavel Emelyanov 
69481566e83SPavel Emelyanov static struct pernet_operations ip6_frags_ops = {
69581566e83SPavel Emelyanov 	.init = ipv6_frags_init_net,
69681566e83SPavel Emelyanov 	.exit = ipv6_frags_exit_net,
69781566e83SPavel Emelyanov };
69881566e83SPavel Emelyanov 
699648700f7SEric Dumazet static u32 ip6_key_hashfn(const void *data, u32 len, u32 seed)
700648700f7SEric Dumazet {
701648700f7SEric Dumazet 	return jhash2(data,
702648700f7SEric Dumazet 		      sizeof(struct frag_v6_compare_key) / sizeof(u32), seed);
703648700f7SEric Dumazet }
704648700f7SEric Dumazet 
705648700f7SEric Dumazet static u32 ip6_obj_hashfn(const void *data, u32 len, u32 seed)
706648700f7SEric Dumazet {
707648700f7SEric Dumazet 	const struct inet_frag_queue *fq = data;
708648700f7SEric Dumazet 
709648700f7SEric Dumazet 	return jhash2((const u32 *)&fq->key.v6,
710648700f7SEric Dumazet 		      sizeof(struct frag_v6_compare_key) / sizeof(u32), seed);
711648700f7SEric Dumazet }
712648700f7SEric Dumazet 
713648700f7SEric Dumazet static int ip6_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
714648700f7SEric Dumazet {
715648700f7SEric Dumazet 	const struct frag_v6_compare_key *key = arg->key;
716648700f7SEric Dumazet 	const struct inet_frag_queue *fq = ptr;
717648700f7SEric Dumazet 
718648700f7SEric Dumazet 	return !!memcmp(&fq->key, key, sizeof(*key));
719648700f7SEric Dumazet }
720648700f7SEric Dumazet 
721648700f7SEric Dumazet const struct rhashtable_params ip6_rhash_params = {
722648700f7SEric Dumazet 	.head_offset		= offsetof(struct inet_frag_queue, node),
723648700f7SEric Dumazet 	.hashfn			= ip6_key_hashfn,
724648700f7SEric Dumazet 	.obj_hashfn		= ip6_obj_hashfn,
725648700f7SEric Dumazet 	.obj_cmpfn		= ip6_obj_cmpfn,
726648700f7SEric Dumazet 	.automatic_shrinking	= true,
727648700f7SEric Dumazet };
728648700f7SEric Dumazet EXPORT_SYMBOL(ip6_rhash_params);
729648700f7SEric Dumazet 
730853cbbaaSDaniel Lezcano int __init ipv6_frag_init(void)
7311da177e4SLinus Torvalds {
732853cbbaaSDaniel Lezcano 	int ret;
7331da177e4SLinus Torvalds 
734c6fda282SPavel Emelyanov 	ip6_frags.constructor = ip6_frag_init;
735c9547709SPavel Emelyanov 	ip6_frags.destructor = NULL;
7361e4b8287SPavel Emelyanov 	ip6_frags.qsize = sizeof(struct frag_queue);
737e521db9dSPavel Emelyanov 	ip6_frags.frag_expire = ip6_frag_expire;
738d4ad4d22SNikolay Aleksandrov 	ip6_frags.frags_cache_name = ip6_frag_cache_name;
739648700f7SEric Dumazet 	ip6_frags.rhash_params = ip6_rhash_params;
740d4ad4d22SNikolay Aleksandrov 	ret = inet_frags_init(&ip6_frags);
741d4ad4d22SNikolay Aleksandrov 	if (ret)
7425b975babSEric Dumazet 		goto out;
7435b975babSEric Dumazet 
7445b975babSEric Dumazet 	ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
7455b975babSEric Dumazet 	if (ret)
7465b975babSEric Dumazet 		goto err_protocol;
7475b975babSEric Dumazet 
7485b975babSEric Dumazet 	ret = ip6_frags_sysctl_register();
7495b975babSEric Dumazet 	if (ret)
7505b975babSEric Dumazet 		goto err_sysctl;
7515b975babSEric Dumazet 
7525b975babSEric Dumazet 	ret = register_pernet_subsys(&ip6_frags_ops);
7535b975babSEric Dumazet 	if (ret)
754d4ad4d22SNikolay Aleksandrov 		goto err_pernet;
7555b975babSEric Dumazet 
756853cbbaaSDaniel Lezcano out:
757853cbbaaSDaniel Lezcano 	return ret;
7580002c630SPavel Emelyanov 
7590002c630SPavel Emelyanov err_pernet:
7607d291ebbSPavel Emelyanov 	ip6_frags_sysctl_unregister();
7617d291ebbSPavel Emelyanov err_sysctl:
7620002c630SPavel Emelyanov 	inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
7635b975babSEric Dumazet err_protocol:
7645b975babSEric Dumazet 	inet_frags_fini(&ip6_frags);
7650002c630SPavel Emelyanov 	goto out;
766853cbbaaSDaniel Lezcano }
767853cbbaaSDaniel Lezcano 
768853cbbaaSDaniel Lezcano void ipv6_frag_exit(void)
769853cbbaaSDaniel Lezcano {
770853cbbaaSDaniel Lezcano 	inet_frags_fini(&ip6_frags);
7717d291ebbSPavel Emelyanov 	ip6_frags_sysctl_unregister();
77281566e83SPavel Emelyanov 	unregister_pernet_subsys(&ip6_frags_ops);
773853cbbaaSDaniel Lezcano 	inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
7741da177e4SLinus Torvalds }
775