xref: /openbmc/linux/net/ipv6/reassembly.c (revision 2d44ed22)
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 
65cc24becaSIan Morris struct ip6frag_skb_cb {
661da177e4SLinus Torvalds 	struct inet6_skb_parm	h;
671da177e4SLinus Torvalds 	int			offset;
681da177e4SLinus Torvalds };
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds #define FRAG6_CB(skb)	((struct ip6frag_skb_cb *)((skb)->cb))
711da177e4SLinus Torvalds 
72fc08c258SFabian Frederick static u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
73eec2e618SHannes Frederic Sowa {
74eec2e618SHannes Frederic Sowa 	return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
75eec2e618SHannes Frederic Sowa }
761da177e4SLinus Torvalds 
777eb95156SPavel Emelyanov static struct inet_frags ip6_frags;
781da177e4SLinus Torvalds 
79f61944efSHerbert Xu static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
80f61944efSHerbert Xu 			  struct net_device *dev);
81f61944efSHerbert Xu 
8236c77782SFlorian Westphal void ip6_frag_init(struct inet_frag_queue *q, const void *a)
83c6fda282SPavel Emelyanov {
84c6fda282SPavel Emelyanov 	struct frag_queue *fq = container_of(q, struct frag_queue, q);
85648700f7SEric Dumazet 	const struct frag_v6_compare_key *key = a;
86c6fda282SPavel Emelyanov 
87648700f7SEric Dumazet 	q->key.v6 = *key;
88648700f7SEric Dumazet 	fq->ecn = 0;
89c6fda282SPavel Emelyanov }
90c6fda282SPavel Emelyanov EXPORT_SYMBOL(ip6_frag_init);
91c6fda282SPavel Emelyanov 
92093ba729SEric Dumazet void ip6_expire_frag_queue(struct net *net, struct frag_queue *fq)
931da177e4SLinus Torvalds {
94a11d206dSYOSHIFUJI Hideaki 	struct net_device *dev = NULL;
95e521db9dSPavel Emelyanov 
965ab11c98SPavel Emelyanov 	spin_lock(&fq->q.lock);
971da177e4SLinus Torvalds 
9806aa8b8aSNikolay Aleksandrov 	if (fq->q.flags & INET_FRAG_COMPLETE)
991da177e4SLinus Torvalds 		goto out;
1001da177e4SLinus Torvalds 
101093ba729SEric Dumazet 	inet_frag_kill(&fq->q);
1021da177e4SLinus Torvalds 
103a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
10469df9d59SEric Dumazet 	dev = dev_get_by_index_rcu(net, fq->iif);
10569df9d59SEric Dumazet 	if (!dev)
10669df9d59SEric Dumazet 		goto out_rcu_unlock;
10769df9d59SEric Dumazet 
1081d015503SEric Dumazet 	__IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
1091d015503SEric Dumazet 	__IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
1102e404f63SNikolay Aleksandrov 
11178c784c4SIngo Oeser 	/* Don't send error if the first segment did not arrive. */
11206aa8b8aSNikolay Aleksandrov 	if (!(fq->q.flags & INET_FRAG_FIRST_IN) || !fq->q.fragments)
11369df9d59SEric Dumazet 		goto out_rcu_unlock;
11478c784c4SIngo Oeser 
1152e404f63SNikolay Aleksandrov 	/* But use as source device on which LAST ARRIVED
1162e404f63SNikolay Aleksandrov 	 * segment was received. And do not use fq->dev
1172e404f63SNikolay Aleksandrov 	 * pointer directly, device might already disappeared.
1181da177e4SLinus Torvalds 	 */
1195ab11c98SPavel Emelyanov 	fq->q.fragments->dev = dev;
1203ffe533cSAlexey Dobriyan 	icmpv6_send(fq->q.fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0);
12169df9d59SEric Dumazet out_rcu_unlock:
12269df9d59SEric Dumazet 	rcu_read_unlock();
1231da177e4SLinus Torvalds out:
1245ab11c98SPavel Emelyanov 	spin_unlock(&fq->q.lock);
125093ba729SEric Dumazet 	inet_frag_put(&fq->q);
126b836c99fSAmerigo Wang }
127b836c99fSAmerigo Wang EXPORT_SYMBOL(ip6_expire_frag_queue);
128b836c99fSAmerigo Wang 
12978802011SKees Cook static void ip6_frag_expire(struct timer_list *t)
130b836c99fSAmerigo Wang {
13178802011SKees Cook 	struct inet_frag_queue *frag = from_timer(frag, t, timer);
132b836c99fSAmerigo Wang 	struct frag_queue *fq;
133b836c99fSAmerigo Wang 	struct net *net;
134b836c99fSAmerigo Wang 
13578802011SKees Cook 	fq = container_of(frag, struct frag_queue, q);
136b836c99fSAmerigo Wang 	net = container_of(fq->q.net, struct net, ipv6.frags);
137b836c99fSAmerigo Wang 
138093ba729SEric Dumazet 	ip6_expire_frag_queue(net, fq);
1391da177e4SLinus Torvalds }
1401da177e4SLinus Torvalds 
141fc08c258SFabian Frederick static struct frag_queue *
142648700f7SEric Dumazet fq_find(struct net *net, __be32 id, const struct ipv6hdr *hdr, int iif)
1431da177e4SLinus Torvalds {
144648700f7SEric Dumazet 	struct frag_v6_compare_key key = {
145648700f7SEric Dumazet 		.id = id,
146648700f7SEric Dumazet 		.saddr = hdr->saddr,
147648700f7SEric Dumazet 		.daddr = hdr->daddr,
148648700f7SEric Dumazet 		.user = IP6_DEFRAG_LOCAL_DELIVER,
149648700f7SEric Dumazet 		.iif = iif,
150648700f7SEric Dumazet 	};
151c6fda282SPavel Emelyanov 	struct inet_frag_queue *q;
1521da177e4SLinus Torvalds 
153648700f7SEric Dumazet 	if (!(ipv6_addr_type(&hdr->daddr) & (IPV6_ADDR_MULTICAST |
154648700f7SEric Dumazet 					    IPV6_ADDR_LINKLOCAL)))
155648700f7SEric Dumazet 		key.iif = 0;
1569a375803SPavel Emelyanov 
157648700f7SEric Dumazet 	q = inet_frag_find(&net->ipv6.frags, &key);
1582d44ed22SEric Dumazet 	if (!q)
1599546377cSShan Wei 		return NULL;
1602d44ed22SEric Dumazet 
161c6fda282SPavel Emelyanov 	return container_of(q, struct frag_queue, q);
1621da177e4SLinus Torvalds }
1631da177e4SLinus Torvalds 
164f61944efSHerbert Xu static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
1651da177e4SLinus Torvalds 			   struct frag_hdr *fhdr, int nhoff)
1661da177e4SLinus Torvalds {
1671da177e4SLinus Torvalds 	struct sk_buff *prev, *next;
168f61944efSHerbert Xu 	struct net_device *dev;
169dbd1759eSWillem de Bruijn 	int offset, end, fragsize;
170adf30907SEric Dumazet 	struct net *net = dev_net(skb_dst(skb)->dev);
171eec2e618SHannes Frederic Sowa 	u8 ecn;
1721da177e4SLinus Torvalds 
17306aa8b8aSNikolay Aleksandrov 	if (fq->q.flags & INET_FRAG_COMPLETE)
1741da177e4SLinus Torvalds 		goto err;
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	offset = ntohs(fhdr->frag_off) & ~0x7;
1770660e03fSArnaldo Carvalho de Melo 	end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
1780660e03fSArnaldo Carvalho de Melo 			((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	if ((unsigned int)end > IPV6_MAXPLEN) {
1811d015503SEric Dumazet 		__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
182a11d206dSYOSHIFUJI Hideaki 				IPSTATS_MIB_INHDRERRORS);
183d56f90a7SArnaldo Carvalho de Melo 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
184d56f90a7SArnaldo Carvalho de Melo 				  ((u8 *)&fhdr->frag_off -
185d56f90a7SArnaldo Carvalho de Melo 				   skb_network_header(skb)));
186f61944efSHerbert Xu 		return -1;
1871da177e4SLinus Torvalds 	}
1881da177e4SLinus Torvalds 
189eec2e618SHannes Frederic Sowa 	ecn = ip6_frag_ecn(ipv6_hdr(skb));
190eec2e618SHannes Frederic Sowa 
191d56f90a7SArnaldo Carvalho de Melo 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
192d56f90a7SArnaldo Carvalho de Melo 		const unsigned char *nh = skb_network_header(skb);
1931da177e4SLinus Torvalds 		skb->csum = csum_sub(skb->csum,
194d56f90a7SArnaldo Carvalho de Melo 				     csum_partial(nh, (u8 *)(fhdr + 1) - nh,
195d56f90a7SArnaldo Carvalho de Melo 						  0));
196d56f90a7SArnaldo Carvalho de Melo 	}
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	/* Is this the final fragment? */
1991da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(IP6_MF))) {
2001da177e4SLinus Torvalds 		/* If we already have some bits beyond end
2011da177e4SLinus Torvalds 		 * or have different end, the segment is corrupted.
2021da177e4SLinus Torvalds 		 */
2035ab11c98SPavel Emelyanov 		if (end < fq->q.len ||
20406aa8b8aSNikolay Aleksandrov 		    ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len))
2051da177e4SLinus Torvalds 			goto err;
20606aa8b8aSNikolay Aleksandrov 		fq->q.flags |= INET_FRAG_LAST_IN;
2075ab11c98SPavel Emelyanov 		fq->q.len = end;
2081da177e4SLinus Torvalds 	} else {
2091da177e4SLinus Torvalds 		/* Check if the fragment is rounded to 8 bytes.
2101da177e4SLinus Torvalds 		 * Required by the RFC.
2111da177e4SLinus Torvalds 		 */
2121da177e4SLinus Torvalds 		if (end & 0x7) {
2131da177e4SLinus Torvalds 			/* RFC2460 says always send parameter problem in
2141da177e4SLinus Torvalds 			 * this case. -DaveM
2151da177e4SLinus Torvalds 			 */
2161d015503SEric Dumazet 			__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
217a11d206dSYOSHIFUJI Hideaki 					IPSTATS_MIB_INHDRERRORS);
2181da177e4SLinus Torvalds 			icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
2191da177e4SLinus Torvalds 					  offsetof(struct ipv6hdr, payload_len));
220f61944efSHerbert Xu 			return -1;
2211da177e4SLinus Torvalds 		}
2225ab11c98SPavel Emelyanov 		if (end > fq->q.len) {
2231da177e4SLinus Torvalds 			/* Some bits beyond end -> corruption. */
22406aa8b8aSNikolay Aleksandrov 			if (fq->q.flags & INET_FRAG_LAST_IN)
2251da177e4SLinus Torvalds 				goto err;
2265ab11c98SPavel Emelyanov 			fq->q.len = end;
2271da177e4SLinus Torvalds 		}
2281da177e4SLinus Torvalds 	}
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	if (end == offset)
2311da177e4SLinus Torvalds 		goto err;
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	/* Point into the IP datagram 'data' part. */
2341da177e4SLinus Torvalds 	if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
2351da177e4SLinus Torvalds 		goto err;
23642ca89c1SStephen Hemminger 
23742ca89c1SStephen Hemminger 	if (pskb_trim_rcsum(skb, end - offset))
2381da177e4SLinus Torvalds 		goto err;
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	/* Find out which fragments are in front and at the back of us
2411da177e4SLinus Torvalds 	 * in the chain of fragments so far.  We must know where to put
2421da177e4SLinus Torvalds 	 * this fragment, right?
2431da177e4SLinus Torvalds 	 */
244d6bebca9SChangli Gao 	prev = fq->q.fragments_tail;
245d6bebca9SChangli Gao 	if (!prev || FRAG6_CB(prev)->offset < offset) {
246d6bebca9SChangli Gao 		next = NULL;
247d6bebca9SChangli Gao 		goto found;
248d6bebca9SChangli Gao 	}
2491da177e4SLinus Torvalds 	prev = NULL;
2505ab11c98SPavel Emelyanov 	for (next = fq->q.fragments; next != NULL; next = next->next) {
2511da177e4SLinus Torvalds 		if (FRAG6_CB(next)->offset >= offset)
2521da177e4SLinus Torvalds 			break;	/* bingo! */
2531da177e4SLinus Torvalds 		prev = next;
2541da177e4SLinus Torvalds 	}
2551da177e4SLinus Torvalds 
256d6bebca9SChangli Gao found:
2575de658f8SEric Dumazet 	/* RFC5722, Section 4, amended by Errata ID : 3089
25870789d70SNicolas Dichtel 	 *                          When reassembling an IPv6 datagram, if
25970789d70SNicolas Dichtel 	 *   one or more its constituent fragments is determined to be an
26070789d70SNicolas Dichtel 	 *   overlapping fragment, the entire datagram (and any constituent
2615de658f8SEric Dumazet 	 *   fragments) MUST be silently discarded.
2621da177e4SLinus Torvalds 	 */
2631da177e4SLinus Torvalds 
26470789d70SNicolas Dichtel 	/* Check for overlap with preceding fragment. */
26570789d70SNicolas Dichtel 	if (prev &&
266f4642141SShan Wei 	    (FRAG6_CB(prev)->offset + prev->len) > offset)
26770789d70SNicolas Dichtel 		goto discard_fq;
2681da177e4SLinus Torvalds 
26970789d70SNicolas Dichtel 	/* Look for overlap with succeeding segment. */
27070789d70SNicolas Dichtel 	if (next && FRAG6_CB(next)->offset < end)
27170789d70SNicolas Dichtel 		goto discard_fq;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	FRAG6_CB(skb)->offset = offset;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	/* Insert this fragment in the chain of fragments. */
2761da177e4SLinus Torvalds 	skb->next = next;
277d6bebca9SChangli Gao 	if (!next)
278d6bebca9SChangli Gao 		fq->q.fragments_tail = skb;
2791da177e4SLinus Torvalds 	if (prev)
2801da177e4SLinus Torvalds 		prev->next = skb;
2811da177e4SLinus Torvalds 	else
2825ab11c98SPavel Emelyanov 		fq->q.fragments = skb;
2831da177e4SLinus Torvalds 
284f61944efSHerbert Xu 	dev = skb->dev;
285f61944efSHerbert Xu 	if (dev) {
286f61944efSHerbert Xu 		fq->iif = dev->ifindex;
2871da177e4SLinus Torvalds 		skb->dev = NULL;
288f61944efSHerbert Xu 	}
2895ab11c98SPavel Emelyanov 	fq->q.stamp = skb->tstamp;
2905ab11c98SPavel Emelyanov 	fq->q.meat += skb->len;
291eec2e618SHannes Frederic Sowa 	fq->ecn |= ecn;
2920e60d245SFlorian Westphal 	add_frag_mem_limit(fq->q.net, skb->truesize);
2931da177e4SLinus Torvalds 
294dbd1759eSWillem de Bruijn 	fragsize = -skb_network_offset(skb) + skb->len;
295dbd1759eSWillem de Bruijn 	if (fragsize > fq->q.max_size)
296dbd1759eSWillem de Bruijn 		fq->q.max_size = fragsize;
297dbd1759eSWillem de Bruijn 
2981da177e4SLinus Torvalds 	/* The first fragment.
2991da177e4SLinus Torvalds 	 * nhoffset is obtained from the first fragment, of course.
3001da177e4SLinus Torvalds 	 */
3011da177e4SLinus Torvalds 	if (offset == 0) {
3021da177e4SLinus Torvalds 		fq->nhoffset = nhoff;
30306aa8b8aSNikolay Aleksandrov 		fq->q.flags |= INET_FRAG_FIRST_IN;
3041da177e4SLinus Torvalds 	}
305f61944efSHerbert Xu 
30606aa8b8aSNikolay Aleksandrov 	if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
30797599dc7SEric Dumazet 	    fq->q.meat == fq->q.len) {
30897599dc7SEric Dumazet 		int res;
30997599dc7SEric Dumazet 		unsigned long orefdst = skb->_skb_refdst;
310f61944efSHerbert Xu 
31197599dc7SEric Dumazet 		skb->_skb_refdst = 0UL;
31297599dc7SEric Dumazet 		res = ip6_frag_reasm(fq, prev, dev);
31397599dc7SEric Dumazet 		skb->_skb_refdst = orefdst;
31497599dc7SEric Dumazet 		return res;
31597599dc7SEric Dumazet 	}
31697599dc7SEric Dumazet 
31797599dc7SEric Dumazet 	skb_dst_drop(skb);
318f61944efSHerbert Xu 	return -1;
3191da177e4SLinus Torvalds 
32070789d70SNicolas Dichtel discard_fq:
321093ba729SEric Dumazet 	inet_frag_kill(&fq->q);
3221da177e4SLinus Torvalds err:
3231d015503SEric Dumazet 	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
3243bd653c8SDenis V. Lunev 			IPSTATS_MIB_REASMFAILS);
3251da177e4SLinus Torvalds 	kfree_skb(skb);
326f61944efSHerbert Xu 	return -1;
3271da177e4SLinus Torvalds }
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds /*
3301da177e4SLinus Torvalds  *	Check if this packet is complete.
3311da177e4SLinus Torvalds  *	Returns NULL on failure by any reason, and pointer
3321da177e4SLinus Torvalds  *	to current nexthdr field in reassembled frame.
3331da177e4SLinus Torvalds  *
3341da177e4SLinus Torvalds  *	It is called with locked fq, and caller must check that
3351da177e4SLinus Torvalds  *	queue is eligible for reassembly i.e. it is not COMPLETE,
3361da177e4SLinus Torvalds  *	the last and the first frames arrived and all the bits are here.
3371da177e4SLinus Torvalds  */
338f61944efSHerbert Xu static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
3391da177e4SLinus Torvalds 			  struct net_device *dev)
3401da177e4SLinus Torvalds {
3412bad35b7SJorge Boncompte [DTI2] 	struct net *net = container_of(fq->q.net, struct net, ipv6.frags);
3425ab11c98SPavel Emelyanov 	struct sk_buff *fp, *head = fq->q.fragments;
3431da177e4SLinus Torvalds 	int    payload_len;
3441da177e4SLinus Torvalds 	unsigned int nhoff;
345ec16439eSEric Dumazet 	int sum_truesize;
346eec2e618SHannes Frederic Sowa 	u8 ecn;
3471da177e4SLinus Torvalds 
348093ba729SEric Dumazet 	inet_frag_kill(&fq->q);
3491da177e4SLinus Torvalds 
350eec2e618SHannes Frederic Sowa 	ecn = ip_frag_ecn_table[fq->ecn];
351eec2e618SHannes Frederic Sowa 	if (unlikely(ecn == 0xff))
352eec2e618SHannes Frederic Sowa 		goto out_fail;
353eec2e618SHannes Frederic Sowa 
354f61944efSHerbert Xu 	/* Make the one we just received the head. */
355f61944efSHerbert Xu 	if (prev) {
356f61944efSHerbert Xu 		head = prev->next;
357f61944efSHerbert Xu 		fp = skb_clone(head, GFP_ATOMIC);
358f61944efSHerbert Xu 
359f61944efSHerbert Xu 		if (!fp)
360f61944efSHerbert Xu 			goto out_oom;
361f61944efSHerbert Xu 
362f61944efSHerbert Xu 		fp->next = head->next;
363d6bebca9SChangli Gao 		if (!fp->next)
364d6bebca9SChangli Gao 			fq->q.fragments_tail = fp;
365f61944efSHerbert Xu 		prev->next = fp;
366f61944efSHerbert Xu 
3675ab11c98SPavel Emelyanov 		skb_morph(head, fq->q.fragments);
3685ab11c98SPavel Emelyanov 		head->next = fq->q.fragments->next;
369f61944efSHerbert Xu 
370808db80aSEric Dumazet 		consume_skb(fq->q.fragments);
3715ab11c98SPavel Emelyanov 		fq->q.fragments = head;
372f61944efSHerbert Xu 	}
373f61944efSHerbert Xu 
374547b792cSIlpo Järvinen 	WARN_ON(head == NULL);
375547b792cSIlpo Järvinen 	WARN_ON(FRAG6_CB(head)->offset != 0);
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	/* Unfragmented part is taken from the first segment. */
378d56f90a7SArnaldo Carvalho de Melo 	payload_len = ((head->data - skb_network_header(head)) -
3795ab11c98SPavel Emelyanov 		       sizeof(struct ipv6hdr) + fq->q.len -
380d56f90a7SArnaldo Carvalho de Melo 		       sizeof(struct frag_hdr));
3811da177e4SLinus Torvalds 	if (payload_len > IPV6_MAXPLEN)
3821da177e4SLinus Torvalds 		goto out_oversize;
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	/* Head of list must not be cloned. */
38514bbd6a5SPravin B Shelar 	if (skb_unclone(head, GFP_ATOMIC))
3861da177e4SLinus Torvalds 		goto out_oom;
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 	/* If the first fragment is fragmented itself, we split
3891da177e4SLinus Torvalds 	 * it to two chunks: the first with data and paged part
3901da177e4SLinus Torvalds 	 * and the second, holding only fragments. */
39121dc3301SDavid S. Miller 	if (skb_has_frag_list(head)) {
3921da177e4SLinus Torvalds 		struct sk_buff *clone;
3931da177e4SLinus Torvalds 		int i, plen = 0;
3941da177e4SLinus Torvalds 
395e5d08d71SIan Morris 		clone = alloc_skb(0, GFP_ATOMIC);
39663159f29SIan Morris 		if (!clone)
3971da177e4SLinus Torvalds 			goto out_oom;
3981da177e4SLinus Torvalds 		clone->next = head->next;
3991da177e4SLinus Torvalds 		head->next = clone;
4001da177e4SLinus Torvalds 		skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
4014d9092bbSDavid S. Miller 		skb_frag_list_init(head);
4021da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
4039e903e08SEric Dumazet 			plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
4041da177e4SLinus Torvalds 		clone->len = clone->data_len = head->data_len - plen;
4051da177e4SLinus Torvalds 		head->data_len -= clone->len;
4061da177e4SLinus Torvalds 		head->len -= clone->len;
4071da177e4SLinus Torvalds 		clone->csum = 0;
4081da177e4SLinus Torvalds 		clone->ip_summed = head->ip_summed;
4090e60d245SFlorian Westphal 		add_frag_mem_limit(fq->q.net, clone->truesize);
4101da177e4SLinus Torvalds 	}
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	/* We have to remove fragment header from datagram and to relocate
4131da177e4SLinus Torvalds 	 * header in order to calculate ICV correctly. */
4141da177e4SLinus Torvalds 	nhoff = fq->nhoffset;
415b0e380b1SArnaldo Carvalho de Melo 	skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
4161da177e4SLinus Torvalds 	memmove(head->head + sizeof(struct frag_hdr), head->head,
4171da177e4SLinus Torvalds 		(head->data - head->head) - sizeof(struct frag_hdr));
418b678aa57SJason A. Donenfeld 	if (skb_mac_header_was_set(head))
419b0e380b1SArnaldo Carvalho de Melo 		head->mac_header += sizeof(struct frag_hdr);
420b0e380b1SArnaldo Carvalho de Melo 	head->network_header += sizeof(struct frag_hdr);
4211da177e4SLinus Torvalds 
422badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(head);
423d56f90a7SArnaldo Carvalho de Melo 	skb_push(head, head->data - skb_network_header(head));
4241da177e4SLinus Torvalds 
425ec16439eSEric Dumazet 	sum_truesize = head->truesize;
426ec16439eSEric Dumazet 	for (fp = head->next; fp;) {
427ec16439eSEric Dumazet 		bool headstolen;
428ec16439eSEric Dumazet 		int delta;
429ec16439eSEric Dumazet 		struct sk_buff *next = fp->next;
430ec16439eSEric Dumazet 
431ec16439eSEric Dumazet 		sum_truesize += fp->truesize;
4321da177e4SLinus Torvalds 		if (head->ip_summed != fp->ip_summed)
4331da177e4SLinus Torvalds 			head->ip_summed = CHECKSUM_NONE;
43484fa7933SPatrick McHardy 		else if (head->ip_summed == CHECKSUM_COMPLETE)
4351da177e4SLinus Torvalds 			head->csum = csum_add(head->csum, fp->csum);
436ec16439eSEric Dumazet 
437ec16439eSEric Dumazet 		if (skb_try_coalesce(head, fp, &headstolen, &delta)) {
438ec16439eSEric Dumazet 			kfree_skb_partial(fp, headstolen);
439ec16439eSEric Dumazet 		} else {
440ec16439eSEric Dumazet 			if (!skb_shinfo(head)->frag_list)
441ec16439eSEric Dumazet 				skb_shinfo(head)->frag_list = fp;
442ec16439eSEric Dumazet 			head->data_len += fp->len;
443ec16439eSEric Dumazet 			head->len += fp->len;
4441da177e4SLinus Torvalds 			head->truesize += fp->truesize;
4451da177e4SLinus Torvalds 		}
446ec16439eSEric Dumazet 		fp = next;
447ec16439eSEric Dumazet 	}
4480e60d245SFlorian Westphal 	sub_frag_mem_limit(fq->q.net, sum_truesize);
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	head->next = NULL;
4511da177e4SLinus Torvalds 	head->dev = dev;
4525ab11c98SPavel Emelyanov 	head->tstamp = fq->q.stamp;
4530660e03fSArnaldo Carvalho de Melo 	ipv6_hdr(head)->payload_len = htons(payload_len);
454eec2e618SHannes Frederic Sowa 	ipv6_change_dsfield(ipv6_hdr(head), 0xff, ecn);
455951dbc8aSPatrick McHardy 	IP6CB(head)->nhoff = nhoff;
456f46078cfSHannes Frederic Sowa 	IP6CB(head)->flags |= IP6SKB_FRAGMENTED;
457dbd1759eSWillem de Bruijn 	IP6CB(head)->frag_max_size = fq->q.max_size;
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 	/* Yes, and fold redundant checksum back. 8) */
4606b83d28aSDaniel Borkmann 	skb_postpush_rcsum(head, skb_network_header(head),
4616b83d28aSDaniel Borkmann 			   skb_network_header_len(head));
4621da177e4SLinus Torvalds 
463a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
4641d015503SEric Dumazet 	__IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
465a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
4665ab11c98SPavel Emelyanov 	fq->q.fragments = NULL;
467d6bebca9SChangli Gao 	fq->q.fragments_tail = NULL;
4681da177e4SLinus Torvalds 	return 1;
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds out_oversize:
471e87cc472SJoe Perches 	net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len);
4721da177e4SLinus Torvalds 	goto out_fail;
4731da177e4SLinus Torvalds out_oom:
474e87cc472SJoe Perches 	net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n");
4751da177e4SLinus Torvalds out_fail:
476a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
4771d015503SEric Dumazet 	__IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
478a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
4791da177e4SLinus Torvalds 	return -1;
4801da177e4SLinus Torvalds }
4811da177e4SLinus Torvalds 
482e5bbef20SHerbert Xu static int ipv6_frag_rcv(struct sk_buff *skb)
4831da177e4SLinus Torvalds {
4841da177e4SLinus Torvalds 	struct frag_hdr *fhdr;
4851da177e4SLinus Torvalds 	struct frag_queue *fq;
486b71d1d42SEric Dumazet 	const struct ipv6hdr *hdr = ipv6_hdr(skb);
487adf30907SEric Dumazet 	struct net *net = dev_net(skb_dst(skb)->dev);
488648700f7SEric Dumazet 	int iif;
4891da177e4SLinus Torvalds 
490f46078cfSHannes Frederic Sowa 	if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED)
491f46078cfSHannes Frederic Sowa 		goto fail_hdr;
492f46078cfSHannes Frederic Sowa 
4931d015503SEric Dumazet 	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS);
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 	/* Jumbo payload inhibits frag. header */
49698b3377cSDenis V. Lunev 	if (hdr->payload_len == 0)
49798b3377cSDenis V. Lunev 		goto fail_hdr;
49898b3377cSDenis V. Lunev 
499ea2ae17dSArnaldo Carvalho de Melo 	if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
50098b3377cSDenis V. Lunev 				 sizeof(struct frag_hdr))))
50198b3377cSDenis V. Lunev 		goto fail_hdr;
5021da177e4SLinus Torvalds 
5030660e03fSArnaldo Carvalho de Melo 	hdr = ipv6_hdr(skb);
5049c70220bSArnaldo Carvalho de Melo 	fhdr = (struct frag_hdr *)skb_transport_header(skb);
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(0xFFF9))) {
5071da177e4SLinus Torvalds 		/* It is not a fragmented frame */
508b0e380b1SArnaldo Carvalho de Melo 		skb->transport_header += sizeof(struct frag_hdr);
5091d015503SEric Dumazet 		__IP6_INC_STATS(net,
510adf30907SEric Dumazet 				ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS);
5111da177e4SLinus Torvalds 
512d56f90a7SArnaldo Carvalho de Melo 		IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
513f46078cfSHannes Frederic Sowa 		IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
5141da177e4SLinus Torvalds 		return 1;
5151da177e4SLinus Torvalds 	}
5161da177e4SLinus Torvalds 
517648700f7SEric Dumazet 	iif = skb->dev ? skb->dev->ifindex : 0;
518648700f7SEric Dumazet 	fq = fq_find(net, fhdr->identification, hdr, iif);
51953b24b8fSIan Morris 	if (fq) {
520f61944efSHerbert Xu 		int ret;
5211da177e4SLinus Torvalds 
5225ab11c98SPavel Emelyanov 		spin_lock(&fq->q.lock);
5231da177e4SLinus Torvalds 
524648700f7SEric Dumazet 		fq->iif = iif;
525f61944efSHerbert Xu 		ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
5261da177e4SLinus Torvalds 
5275ab11c98SPavel Emelyanov 		spin_unlock(&fq->q.lock);
528093ba729SEric Dumazet 		inet_frag_put(&fq->q);
5291da177e4SLinus Torvalds 		return ret;
5301da177e4SLinus Torvalds 	}
5311da177e4SLinus Torvalds 
5321d015503SEric Dumazet 	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS);
5331da177e4SLinus Torvalds 	kfree_skb(skb);
5341da177e4SLinus Torvalds 	return -1;
53598b3377cSDenis V. Lunev 
53698b3377cSDenis V. Lunev fail_hdr:
5371d015503SEric Dumazet 	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
538d2373862SNikolay Aleksandrov 			IPSTATS_MIB_INHDRERRORS);
53998b3377cSDenis V. Lunev 	icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
54098b3377cSDenis V. Lunev 	return -1;
5411da177e4SLinus Torvalds }
5421da177e4SLinus Torvalds 
543cc24becaSIan Morris static const struct inet6_protocol frag_protocol = {
5441da177e4SLinus Torvalds 	.handler	=	ipv6_frag_rcv,
5451da177e4SLinus Torvalds 	.flags		=	INET6_PROTO_NOPOLICY,
5461da177e4SLinus Torvalds };
5471da177e4SLinus Torvalds 
5488d8354d2SPavel Emelyanov #ifdef CONFIG_SYSCTL
5491bab4c75SNikolay Aleksandrov static int zero;
5501bab4c75SNikolay Aleksandrov 
5510a64b4b8SPavel Emelyanov static struct ctl_table ip6_frags_ns_ctl_table[] = {
552e71e0349SDaniel Lezcano 	{
5538d8354d2SPavel Emelyanov 		.procname	= "ip6frag_high_thresh",
554e31e0bdcSPavel Emelyanov 		.data		= &init_net.ipv6.frags.high_thresh,
5558d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
5568d8354d2SPavel Emelyanov 		.mode		= 0644,
5571bab4c75SNikolay Aleksandrov 		.proc_handler	= proc_dointvec_minmax,
5581bab4c75SNikolay Aleksandrov 		.extra1		= &init_net.ipv6.frags.low_thresh
5598d8354d2SPavel Emelyanov 	},
5608d8354d2SPavel Emelyanov 	{
5618d8354d2SPavel Emelyanov 		.procname	= "ip6frag_low_thresh",
562e31e0bdcSPavel Emelyanov 		.data		= &init_net.ipv6.frags.low_thresh,
5638d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
5648d8354d2SPavel Emelyanov 		.mode		= 0644,
5651bab4c75SNikolay Aleksandrov 		.proc_handler	= proc_dointvec_minmax,
5661bab4c75SNikolay Aleksandrov 		.extra1		= &zero,
5671bab4c75SNikolay Aleksandrov 		.extra2		= &init_net.ipv6.frags.high_thresh
5688d8354d2SPavel Emelyanov 	},
5698d8354d2SPavel Emelyanov 	{
5708d8354d2SPavel Emelyanov 		.procname	= "ip6frag_time",
571b2fd5321SPavel Emelyanov 		.data		= &init_net.ipv6.frags.timeout,
5728d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
5738d8354d2SPavel Emelyanov 		.mode		= 0644,
5746d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
5758d8354d2SPavel Emelyanov 	},
5767d291ebbSPavel Emelyanov 	{ }
5777d291ebbSPavel Emelyanov };
5787d291ebbSPavel Emelyanov 
579e3a57d18SFlorian Westphal /* secret interval has been deprecated */
580e3a57d18SFlorian Westphal static int ip6_frags_secret_interval_unused;
5817d291ebbSPavel Emelyanov static struct ctl_table ip6_frags_ctl_table[] = {
5828d8354d2SPavel Emelyanov 	{
5838d8354d2SPavel Emelyanov 		.procname	= "ip6frag_secret_interval",
584e3a57d18SFlorian Westphal 		.data		= &ip6_frags_secret_interval_unused,
5858d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
5868d8354d2SPavel Emelyanov 		.mode		= 0644,
5876d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
5888d8354d2SPavel Emelyanov 	},
5898d8354d2SPavel Emelyanov 	{ }
5908d8354d2SPavel Emelyanov };
5917d460db9SDaniel Lezcano 
5922c8c1e72SAlexey Dobriyan static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
5938d8354d2SPavel Emelyanov {
594e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
5958d8354d2SPavel Emelyanov 	struct ctl_table_header *hdr;
5968d8354d2SPavel Emelyanov 
5970a64b4b8SPavel Emelyanov 	table = ip6_frags_ns_ctl_table;
59809ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net)) {
5990a64b4b8SPavel Emelyanov 		table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
60063159f29SIan Morris 		if (!table)
601e4a2d5c2SPavel Emelyanov 			goto err_alloc;
602e4a2d5c2SPavel Emelyanov 
603e31e0bdcSPavel Emelyanov 		table[0].data = &net->ipv6.frags.high_thresh;
6041bab4c75SNikolay Aleksandrov 		table[0].extra1 = &net->ipv6.frags.low_thresh;
6051bab4c75SNikolay Aleksandrov 		table[0].extra2 = &init_net.ipv6.frags.high_thresh;
606e31e0bdcSPavel Emelyanov 		table[1].data = &net->ipv6.frags.low_thresh;
6071bab4c75SNikolay Aleksandrov 		table[1].extra2 = &net->ipv6.frags.high_thresh;
608b2fd5321SPavel Emelyanov 		table[2].data = &net->ipv6.frags.timeout;
609e4a2d5c2SPavel Emelyanov 	}
610e4a2d5c2SPavel Emelyanov 
611ec8f23ceSEric W. Biederman 	hdr = register_net_sysctl(net, "net/ipv6", table);
61263159f29SIan Morris 	if (!hdr)
613e4a2d5c2SPavel Emelyanov 		goto err_reg;
614e4a2d5c2SPavel Emelyanov 
615e4a2d5c2SPavel Emelyanov 	net->ipv6.sysctl.frags_hdr = hdr;
616e4a2d5c2SPavel Emelyanov 	return 0;
617e4a2d5c2SPavel Emelyanov 
618e4a2d5c2SPavel Emelyanov err_reg:
61909ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net))
620e4a2d5c2SPavel Emelyanov 		kfree(table);
621e4a2d5c2SPavel Emelyanov err_alloc:
622e4a2d5c2SPavel Emelyanov 	return -ENOMEM;
623e4a2d5c2SPavel Emelyanov }
624e4a2d5c2SPavel Emelyanov 
6252c8c1e72SAlexey Dobriyan static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
626e4a2d5c2SPavel Emelyanov {
627e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
628e4a2d5c2SPavel Emelyanov 
629e4a2d5c2SPavel Emelyanov 	table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
630e4a2d5c2SPavel Emelyanov 	unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
6313705e11aSYang Hongyang 	if (!net_eq(net, &init_net))
632e4a2d5c2SPavel Emelyanov 		kfree(table);
6338d8354d2SPavel Emelyanov }
6347d291ebbSPavel Emelyanov 
6357d291ebbSPavel Emelyanov static struct ctl_table_header *ip6_ctl_header;
6367d291ebbSPavel Emelyanov 
6377d291ebbSPavel Emelyanov static int ip6_frags_sysctl_register(void)
6387d291ebbSPavel Emelyanov {
63943444757SEric W. Biederman 	ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6",
6407d291ebbSPavel Emelyanov 			ip6_frags_ctl_table);
6417d291ebbSPavel Emelyanov 	return ip6_ctl_header == NULL ? -ENOMEM : 0;
6427d291ebbSPavel Emelyanov }
6437d291ebbSPavel Emelyanov 
6447d291ebbSPavel Emelyanov static void ip6_frags_sysctl_unregister(void)
6457d291ebbSPavel Emelyanov {
6467d291ebbSPavel Emelyanov 	unregister_net_sysctl_table(ip6_ctl_header);
6477d291ebbSPavel Emelyanov }
6488d8354d2SPavel Emelyanov #else
649fc08c258SFabian Frederick static int ip6_frags_ns_sysctl_register(struct net *net)
6508d8354d2SPavel Emelyanov {
6518d8354d2SPavel Emelyanov 	return 0;
6528d8354d2SPavel Emelyanov }
653e4a2d5c2SPavel Emelyanov 
654fc08c258SFabian Frederick static void ip6_frags_ns_sysctl_unregister(struct net *net)
655e4a2d5c2SPavel Emelyanov {
656e4a2d5c2SPavel Emelyanov }
6577d291ebbSPavel Emelyanov 
658fc08c258SFabian Frederick static int ip6_frags_sysctl_register(void)
6597d291ebbSPavel Emelyanov {
6607d291ebbSPavel Emelyanov 	return 0;
6617d291ebbSPavel Emelyanov }
6627d291ebbSPavel Emelyanov 
663fc08c258SFabian Frederick static void ip6_frags_sysctl_unregister(void)
6647d291ebbSPavel Emelyanov {
6657d291ebbSPavel Emelyanov }
6668d8354d2SPavel Emelyanov #endif
6678d8354d2SPavel Emelyanov 
6682c8c1e72SAlexey Dobriyan static int __net_init ipv6_frags_init_net(struct net *net)
6698d8354d2SPavel Emelyanov {
670787bea77SEric Dumazet 	int res;
671787bea77SEric Dumazet 
6727c070aa9SShan Wei 	net->ipv6.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
6737c070aa9SShan Wei 	net->ipv6.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
674b2fd5321SPavel Emelyanov 	net->ipv6.frags.timeout = IPV6_FRAG_TIMEOUT;
675093ba729SEric Dumazet 	net->ipv6.frags.f = &ip6_frags;
6768d8354d2SPavel Emelyanov 
677787bea77SEric Dumazet 	res = inet_frags_init_net(&net->ipv6.frags);
678787bea77SEric Dumazet 	if (res < 0)
679787bea77SEric Dumazet 		return res;
6805a63643eSJesper Dangaard Brouer 
681787bea77SEric Dumazet 	res = ip6_frags_ns_sysctl_register(net);
682787bea77SEric Dumazet 	if (res < 0)
683093ba729SEric Dumazet 		inet_frags_exit_net(&net->ipv6.frags);
684787bea77SEric Dumazet 	return res;
685e71e0349SDaniel Lezcano }
686e71e0349SDaniel Lezcano 
6872c8c1e72SAlexey Dobriyan static void __net_exit ipv6_frags_exit_net(struct net *net)
68881566e83SPavel Emelyanov {
6890a64b4b8SPavel Emelyanov 	ip6_frags_ns_sysctl_unregister(net);
690093ba729SEric Dumazet 	inet_frags_exit_net(&net->ipv6.frags);
69181566e83SPavel Emelyanov }
69281566e83SPavel Emelyanov 
69381566e83SPavel Emelyanov static struct pernet_operations ip6_frags_ops = {
69481566e83SPavel Emelyanov 	.init = ipv6_frags_init_net,
69581566e83SPavel Emelyanov 	.exit = ipv6_frags_exit_net,
69681566e83SPavel Emelyanov };
69781566e83SPavel Emelyanov 
698648700f7SEric Dumazet static u32 ip6_key_hashfn(const void *data, u32 len, u32 seed)
699648700f7SEric Dumazet {
700648700f7SEric Dumazet 	return jhash2(data,
701648700f7SEric Dumazet 		      sizeof(struct frag_v6_compare_key) / sizeof(u32), seed);
702648700f7SEric Dumazet }
703648700f7SEric Dumazet 
704648700f7SEric Dumazet static u32 ip6_obj_hashfn(const void *data, u32 len, u32 seed)
705648700f7SEric Dumazet {
706648700f7SEric Dumazet 	const struct inet_frag_queue *fq = data;
707648700f7SEric Dumazet 
708648700f7SEric Dumazet 	return jhash2((const u32 *)&fq->key.v6,
709648700f7SEric Dumazet 		      sizeof(struct frag_v6_compare_key) / sizeof(u32), seed);
710648700f7SEric Dumazet }
711648700f7SEric Dumazet 
712648700f7SEric Dumazet static int ip6_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
713648700f7SEric Dumazet {
714648700f7SEric Dumazet 	const struct frag_v6_compare_key *key = arg->key;
715648700f7SEric Dumazet 	const struct inet_frag_queue *fq = ptr;
716648700f7SEric Dumazet 
717648700f7SEric Dumazet 	return !!memcmp(&fq->key, key, sizeof(*key));
718648700f7SEric Dumazet }
719648700f7SEric Dumazet 
720648700f7SEric Dumazet const struct rhashtable_params ip6_rhash_params = {
721648700f7SEric Dumazet 	.head_offset		= offsetof(struct inet_frag_queue, node),
722648700f7SEric Dumazet 	.hashfn			= ip6_key_hashfn,
723648700f7SEric Dumazet 	.obj_hashfn		= ip6_obj_hashfn,
724648700f7SEric Dumazet 	.obj_cmpfn		= ip6_obj_cmpfn,
725648700f7SEric Dumazet 	.automatic_shrinking	= true,
726648700f7SEric Dumazet };
727648700f7SEric Dumazet EXPORT_SYMBOL(ip6_rhash_params);
728648700f7SEric Dumazet 
729853cbbaaSDaniel Lezcano int __init ipv6_frag_init(void)
7301da177e4SLinus Torvalds {
731853cbbaaSDaniel Lezcano 	int ret;
7321da177e4SLinus Torvalds 
733c6fda282SPavel Emelyanov 	ip6_frags.constructor = ip6_frag_init;
734c9547709SPavel Emelyanov 	ip6_frags.destructor = NULL;
7351e4b8287SPavel Emelyanov 	ip6_frags.qsize = sizeof(struct frag_queue);
736e521db9dSPavel Emelyanov 	ip6_frags.frag_expire = ip6_frag_expire;
737d4ad4d22SNikolay Aleksandrov 	ip6_frags.frags_cache_name = ip6_frag_cache_name;
738648700f7SEric Dumazet 	ip6_frags.rhash_params = ip6_rhash_params;
739d4ad4d22SNikolay Aleksandrov 	ret = inet_frags_init(&ip6_frags);
740d4ad4d22SNikolay Aleksandrov 	if (ret)
7415b975babSEric Dumazet 		goto out;
7425b975babSEric Dumazet 
7435b975babSEric Dumazet 	ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
7445b975babSEric Dumazet 	if (ret)
7455b975babSEric Dumazet 		goto err_protocol;
7465b975babSEric Dumazet 
7475b975babSEric Dumazet 	ret = ip6_frags_sysctl_register();
7485b975babSEric Dumazet 	if (ret)
7495b975babSEric Dumazet 		goto err_sysctl;
7505b975babSEric Dumazet 
7515b975babSEric Dumazet 	ret = register_pernet_subsys(&ip6_frags_ops);
7525b975babSEric Dumazet 	if (ret)
753d4ad4d22SNikolay Aleksandrov 		goto err_pernet;
7545b975babSEric Dumazet 
755853cbbaaSDaniel Lezcano out:
756853cbbaaSDaniel Lezcano 	return ret;
7570002c630SPavel Emelyanov 
7580002c630SPavel Emelyanov err_pernet:
7597d291ebbSPavel Emelyanov 	ip6_frags_sysctl_unregister();
7607d291ebbSPavel Emelyanov err_sysctl:
7610002c630SPavel Emelyanov 	inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
7625b975babSEric Dumazet err_protocol:
7635b975babSEric Dumazet 	inet_frags_fini(&ip6_frags);
7640002c630SPavel Emelyanov 	goto out;
765853cbbaaSDaniel Lezcano }
766853cbbaaSDaniel Lezcano 
767853cbbaaSDaniel Lezcano void ipv6_frag_exit(void)
768853cbbaaSDaniel Lezcano {
769853cbbaaSDaniel Lezcano 	inet_frags_fini(&ip6_frags);
7707d291ebbSPavel Emelyanov 	ip6_frags_sysctl_unregister();
77181566e83SPavel Emelyanov 	unregister_pernet_subsys(&ip6_frags_ops);
772853cbbaaSDaniel Lezcano 	inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
7731da177e4SLinus Torvalds }
774