xref: /openbmc/linux/net/ipv6/reassembly.c (revision 1d015503)
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 
82f6596f9dSZach Brown /*
83f6596f9dSZach Brown  * callers should be careful not to use the hash value outside the ipfrag_lock
84f6596f9dSZach Brown  * as doing so could race with ipfrag_hash_rnd being recalculated.
85f6596f9dSZach Brown  */
86b1190570SHannes Frederic Sowa static unsigned int inet6_hash_frag(__be32 id, const struct in6_addr *saddr,
87b1190570SHannes Frederic Sowa 				    const struct in6_addr *daddr)
881da177e4SLinus Torvalds {
89b1190570SHannes Frederic Sowa 	net_get_random_once(&ip6_frags.rnd, sizeof(ip6_frags.rnd));
90fb3cfe6eSFlorian Westphal 	return jhash_3words(ipv6_addr_hash(saddr), ipv6_addr_hash(daddr),
91b1190570SHannes Frederic Sowa 			    (__force u32)id, ip6_frags.rnd);
921da177e4SLinus Torvalds }
931da177e4SLinus Torvalds 
9436c77782SFlorian Westphal static unsigned int ip6_hashfn(const struct inet_frag_queue *q)
951da177e4SLinus Torvalds {
9636c77782SFlorian Westphal 	const struct frag_queue *fq;
971da177e4SLinus Torvalds 
98321a3a99SPavel Emelyanov 	fq = container_of(q, struct frag_queue, q);
99b1190570SHannes Frederic Sowa 	return inet6_hash_frag(fq->id, &fq->saddr, &fq->daddr);
1001da177e4SLinus Torvalds }
1011da177e4SLinus Torvalds 
10236c77782SFlorian Westphal bool ip6_frag_match(const struct inet_frag_queue *q, const void *a)
103abd6523dSPavel Emelyanov {
10436c77782SFlorian Westphal 	const struct frag_queue *fq;
10536c77782SFlorian Westphal 	const struct ip6_create_arg *arg = a;
106abd6523dSPavel Emelyanov 
107abd6523dSPavel Emelyanov 	fq = container_of(q, struct frag_queue, q);
108cbc264caSEric Dumazet 	return	fq->id == arg->id &&
109cbc264caSEric Dumazet 		fq->user == arg->user &&
110abd6523dSPavel Emelyanov 		ipv6_addr_equal(&fq->saddr, arg->src) &&
111264640fcSMichal Kubeček 		ipv6_addr_equal(&fq->daddr, arg->dst) &&
112264640fcSMichal Kubeček 		(arg->iif == fq->iif ||
113264640fcSMichal Kubeček 		 !(ipv6_addr_type(arg->dst) & (IPV6_ADDR_MULTICAST |
114264640fcSMichal Kubeček 					       IPV6_ADDR_LINKLOCAL)));
115abd6523dSPavel Emelyanov }
116abd6523dSPavel Emelyanov EXPORT_SYMBOL(ip6_frag_match);
117abd6523dSPavel Emelyanov 
11836c77782SFlorian Westphal void ip6_frag_init(struct inet_frag_queue *q, const void *a)
119c6fda282SPavel Emelyanov {
120c6fda282SPavel Emelyanov 	struct frag_queue *fq = container_of(q, struct frag_queue, q);
12136c77782SFlorian Westphal 	const struct ip6_create_arg *arg = a;
122c6fda282SPavel Emelyanov 
123c6fda282SPavel Emelyanov 	fq->id = arg->id;
1240b5ccb2eSPatrick McHardy 	fq->user = arg->user;
1254e3fd7a0SAlexey Dobriyan 	fq->saddr = *arg->src;
1264e3fd7a0SAlexey Dobriyan 	fq->daddr = *arg->dst;
127eec2e618SHannes Frederic Sowa 	fq->ecn = arg->ecn;
128c6fda282SPavel Emelyanov }
129c6fda282SPavel Emelyanov EXPORT_SYMBOL(ip6_frag_init);
130c6fda282SPavel Emelyanov 
131b836c99fSAmerigo Wang void ip6_expire_frag_queue(struct net *net, struct frag_queue *fq,
132b836c99fSAmerigo Wang 			   struct inet_frags *frags)
1331da177e4SLinus Torvalds {
134a11d206dSYOSHIFUJI Hideaki 	struct net_device *dev = NULL;
135e521db9dSPavel Emelyanov 
1365ab11c98SPavel Emelyanov 	spin_lock(&fq->q.lock);
1371da177e4SLinus Torvalds 
13806aa8b8aSNikolay Aleksandrov 	if (fq->q.flags & INET_FRAG_COMPLETE)
1391da177e4SLinus Torvalds 		goto out;
1401da177e4SLinus Torvalds 
141b836c99fSAmerigo Wang 	inet_frag_kill(&fq->q, frags);
1421da177e4SLinus Torvalds 
143a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
14469df9d59SEric Dumazet 	dev = dev_get_by_index_rcu(net, fq->iif);
14569df9d59SEric Dumazet 	if (!dev)
14669df9d59SEric Dumazet 		goto out_rcu_unlock;
14769df9d59SEric Dumazet 
1481d015503SEric Dumazet 	__IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
1491da177e4SLinus Torvalds 
150caaecdd3SNikolay Aleksandrov 	if (inet_frag_evicting(&fq->q))
1512e404f63SNikolay Aleksandrov 		goto out_rcu_unlock;
1522e404f63SNikolay Aleksandrov 
1531d015503SEric Dumazet 	__IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
1542e404f63SNikolay Aleksandrov 
15578c784c4SIngo Oeser 	/* Don't send error if the first segment did not arrive. */
15606aa8b8aSNikolay Aleksandrov 	if (!(fq->q.flags & INET_FRAG_FIRST_IN) || !fq->q.fragments)
15769df9d59SEric Dumazet 		goto out_rcu_unlock;
15878c784c4SIngo Oeser 
1592e404f63SNikolay Aleksandrov 	/* But use as source device on which LAST ARRIVED
1602e404f63SNikolay Aleksandrov 	 * segment was received. And do not use fq->dev
1612e404f63SNikolay Aleksandrov 	 * pointer directly, device might already disappeared.
1621da177e4SLinus Torvalds 	 */
1635ab11c98SPavel Emelyanov 	fq->q.fragments->dev = dev;
1643ffe533cSAlexey Dobriyan 	icmpv6_send(fq->q.fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0);
16569df9d59SEric Dumazet out_rcu_unlock:
16669df9d59SEric Dumazet 	rcu_read_unlock();
1671da177e4SLinus Torvalds out:
1685ab11c98SPavel Emelyanov 	spin_unlock(&fq->q.lock);
169b836c99fSAmerigo Wang 	inet_frag_put(&fq->q, frags);
170b836c99fSAmerigo Wang }
171b836c99fSAmerigo Wang EXPORT_SYMBOL(ip6_expire_frag_queue);
172b836c99fSAmerigo Wang 
173b836c99fSAmerigo Wang static void ip6_frag_expire(unsigned long data)
174b836c99fSAmerigo Wang {
175b836c99fSAmerigo Wang 	struct frag_queue *fq;
176b836c99fSAmerigo Wang 	struct net *net;
177b836c99fSAmerigo Wang 
178b836c99fSAmerigo Wang 	fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q);
179b836c99fSAmerigo Wang 	net = container_of(fq->q.net, struct net, ipv6.frags);
180b836c99fSAmerigo Wang 
181b836c99fSAmerigo Wang 	ip6_expire_frag_queue(net, fq, &ip6_frags);
1821da177e4SLinus Torvalds }
1831da177e4SLinus Torvalds 
184fc08c258SFabian Frederick static struct frag_queue *
185eec2e618SHannes Frederic Sowa fq_find(struct net *net, __be32 id, const struct in6_addr *src,
186264640fcSMichal Kubeček 	const struct in6_addr *dst, int iif, u8 ecn)
1871da177e4SLinus Torvalds {
188c6fda282SPavel Emelyanov 	struct inet_frag_queue *q;
189c6fda282SPavel Emelyanov 	struct ip6_create_arg arg;
190abd6523dSPavel Emelyanov 	unsigned int hash;
1911da177e4SLinus Torvalds 
192c6fda282SPavel Emelyanov 	arg.id = id;
1930b5ccb2eSPatrick McHardy 	arg.user = IP6_DEFRAG_LOCAL_DELIVER;
194c6fda282SPavel Emelyanov 	arg.src = src;
195c6fda282SPavel Emelyanov 	arg.dst = dst;
196264640fcSMichal Kubeček 	arg.iif = iif;
197eec2e618SHannes Frederic Sowa 	arg.ecn = ecn;
1989a375803SPavel Emelyanov 
199b1190570SHannes Frederic Sowa 	hash = inet6_hash_frag(id, src, dst);
200c6fda282SPavel Emelyanov 
201ac18e750SPavel Emelyanov 	q = inet_frag_find(&net->ipv6.frags, &ip6_frags, &arg, hash);
2025a3da1feSHannes Frederic Sowa 	if (IS_ERR_OR_NULL(q)) {
2035a3da1feSHannes Frederic Sowa 		inet_frag_maybe_warn_overflow(q, pr_fmt());
2049546377cSShan Wei 		return NULL;
2055a3da1feSHannes Frederic Sowa 	}
206c6fda282SPavel Emelyanov 	return container_of(q, struct frag_queue, q);
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds 
209f61944efSHerbert Xu static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
2101da177e4SLinus Torvalds 			   struct frag_hdr *fhdr, int nhoff)
2111da177e4SLinus Torvalds {
2121da177e4SLinus Torvalds 	struct sk_buff *prev, *next;
213f61944efSHerbert Xu 	struct net_device *dev;
2141da177e4SLinus Torvalds 	int offset, end;
215adf30907SEric Dumazet 	struct net *net = dev_net(skb_dst(skb)->dev);
216eec2e618SHannes Frederic Sowa 	u8 ecn;
2171da177e4SLinus Torvalds 
21806aa8b8aSNikolay Aleksandrov 	if (fq->q.flags & INET_FRAG_COMPLETE)
2191da177e4SLinus Torvalds 		goto err;
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	offset = ntohs(fhdr->frag_off) & ~0x7;
2220660e03fSArnaldo Carvalho de Melo 	end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
2230660e03fSArnaldo Carvalho de Melo 			((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	if ((unsigned int)end > IPV6_MAXPLEN) {
2261d015503SEric Dumazet 		__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
227a11d206dSYOSHIFUJI Hideaki 				IPSTATS_MIB_INHDRERRORS);
228d56f90a7SArnaldo Carvalho de Melo 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
229d56f90a7SArnaldo Carvalho de Melo 				  ((u8 *)&fhdr->frag_off -
230d56f90a7SArnaldo Carvalho de Melo 				   skb_network_header(skb)));
231f61944efSHerbert Xu 		return -1;
2321da177e4SLinus Torvalds 	}
2331da177e4SLinus Torvalds 
234eec2e618SHannes Frederic Sowa 	ecn = ip6_frag_ecn(ipv6_hdr(skb));
235eec2e618SHannes Frederic Sowa 
236d56f90a7SArnaldo Carvalho de Melo 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
237d56f90a7SArnaldo Carvalho de Melo 		const unsigned char *nh = skb_network_header(skb);
2381da177e4SLinus Torvalds 		skb->csum = csum_sub(skb->csum,
239d56f90a7SArnaldo Carvalho de Melo 				     csum_partial(nh, (u8 *)(fhdr + 1) - nh,
240d56f90a7SArnaldo Carvalho de Melo 						  0));
241d56f90a7SArnaldo Carvalho de Melo 	}
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	/* Is this the final fragment? */
2441da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(IP6_MF))) {
2451da177e4SLinus Torvalds 		/* If we already have some bits beyond end
2461da177e4SLinus Torvalds 		 * or have different end, the segment is corrupted.
2471da177e4SLinus Torvalds 		 */
2485ab11c98SPavel Emelyanov 		if (end < fq->q.len ||
24906aa8b8aSNikolay Aleksandrov 		    ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len))
2501da177e4SLinus Torvalds 			goto err;
25106aa8b8aSNikolay Aleksandrov 		fq->q.flags |= INET_FRAG_LAST_IN;
2525ab11c98SPavel Emelyanov 		fq->q.len = end;
2531da177e4SLinus Torvalds 	} else {
2541da177e4SLinus Torvalds 		/* Check if the fragment is rounded to 8 bytes.
2551da177e4SLinus Torvalds 		 * Required by the RFC.
2561da177e4SLinus Torvalds 		 */
2571da177e4SLinus Torvalds 		if (end & 0x7) {
2581da177e4SLinus Torvalds 			/* RFC2460 says always send parameter problem in
2591da177e4SLinus Torvalds 			 * this case. -DaveM
2601da177e4SLinus Torvalds 			 */
2611d015503SEric Dumazet 			__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
262a11d206dSYOSHIFUJI Hideaki 					IPSTATS_MIB_INHDRERRORS);
2631da177e4SLinus Torvalds 			icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
2641da177e4SLinus Torvalds 					  offsetof(struct ipv6hdr, payload_len));
265f61944efSHerbert Xu 			return -1;
2661da177e4SLinus Torvalds 		}
2675ab11c98SPavel Emelyanov 		if (end > fq->q.len) {
2681da177e4SLinus Torvalds 			/* Some bits beyond end -> corruption. */
26906aa8b8aSNikolay Aleksandrov 			if (fq->q.flags & INET_FRAG_LAST_IN)
2701da177e4SLinus Torvalds 				goto err;
2715ab11c98SPavel Emelyanov 			fq->q.len = end;
2721da177e4SLinus Torvalds 		}
2731da177e4SLinus Torvalds 	}
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	if (end == offset)
2761da177e4SLinus Torvalds 		goto err;
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	/* Point into the IP datagram 'data' part. */
2791da177e4SLinus Torvalds 	if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
2801da177e4SLinus Torvalds 		goto err;
28142ca89c1SStephen Hemminger 
28242ca89c1SStephen Hemminger 	if (pskb_trim_rcsum(skb, end - offset))
2831da177e4SLinus Torvalds 		goto err;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	/* Find out which fragments are in front and at the back of us
2861da177e4SLinus Torvalds 	 * in the chain of fragments so far.  We must know where to put
2871da177e4SLinus Torvalds 	 * this fragment, right?
2881da177e4SLinus Torvalds 	 */
289d6bebca9SChangli Gao 	prev = fq->q.fragments_tail;
290d6bebca9SChangli Gao 	if (!prev || FRAG6_CB(prev)->offset < offset) {
291d6bebca9SChangli Gao 		next = NULL;
292d6bebca9SChangli Gao 		goto found;
293d6bebca9SChangli Gao 	}
2941da177e4SLinus Torvalds 	prev = NULL;
2955ab11c98SPavel Emelyanov 	for (next = fq->q.fragments; next != NULL; next = next->next) {
2961da177e4SLinus Torvalds 		if (FRAG6_CB(next)->offset >= offset)
2971da177e4SLinus Torvalds 			break;	/* bingo! */
2981da177e4SLinus Torvalds 		prev = next;
2991da177e4SLinus Torvalds 	}
3001da177e4SLinus Torvalds 
301d6bebca9SChangli Gao found:
3025de658f8SEric Dumazet 	/* RFC5722, Section 4, amended by Errata ID : 3089
30370789d70SNicolas Dichtel 	 *                          When reassembling an IPv6 datagram, if
30470789d70SNicolas Dichtel 	 *   one or more its constituent fragments is determined to be an
30570789d70SNicolas Dichtel 	 *   overlapping fragment, the entire datagram (and any constituent
3065de658f8SEric Dumazet 	 *   fragments) MUST be silently discarded.
3071da177e4SLinus Torvalds 	 */
3081da177e4SLinus Torvalds 
30970789d70SNicolas Dichtel 	/* Check for overlap with preceding fragment. */
31070789d70SNicolas Dichtel 	if (prev &&
311f4642141SShan Wei 	    (FRAG6_CB(prev)->offset + prev->len) > offset)
31270789d70SNicolas Dichtel 		goto discard_fq;
3131da177e4SLinus Torvalds 
31470789d70SNicolas Dichtel 	/* Look for overlap with succeeding segment. */
31570789d70SNicolas Dichtel 	if (next && FRAG6_CB(next)->offset < end)
31670789d70SNicolas Dichtel 		goto discard_fq;
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 	FRAG6_CB(skb)->offset = offset;
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	/* Insert this fragment in the chain of fragments. */
3211da177e4SLinus Torvalds 	skb->next = next;
322d6bebca9SChangli Gao 	if (!next)
323d6bebca9SChangli Gao 		fq->q.fragments_tail = skb;
3241da177e4SLinus Torvalds 	if (prev)
3251da177e4SLinus Torvalds 		prev->next = skb;
3261da177e4SLinus Torvalds 	else
3275ab11c98SPavel Emelyanov 		fq->q.fragments = skb;
3281da177e4SLinus Torvalds 
329f61944efSHerbert Xu 	dev = skb->dev;
330f61944efSHerbert Xu 	if (dev) {
331f61944efSHerbert Xu 		fq->iif = dev->ifindex;
3321da177e4SLinus Torvalds 		skb->dev = NULL;
333f61944efSHerbert Xu 	}
3345ab11c98SPavel Emelyanov 	fq->q.stamp = skb->tstamp;
3355ab11c98SPavel Emelyanov 	fq->q.meat += skb->len;
336eec2e618SHannes Frederic Sowa 	fq->ecn |= ecn;
3370e60d245SFlorian Westphal 	add_frag_mem_limit(fq->q.net, skb->truesize);
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	/* The first fragment.
3401da177e4SLinus Torvalds 	 * nhoffset is obtained from the first fragment, of course.
3411da177e4SLinus Torvalds 	 */
3421da177e4SLinus Torvalds 	if (offset == 0) {
3431da177e4SLinus Torvalds 		fq->nhoffset = nhoff;
34406aa8b8aSNikolay Aleksandrov 		fq->q.flags |= INET_FRAG_FIRST_IN;
3451da177e4SLinus Torvalds 	}
346f61944efSHerbert Xu 
34706aa8b8aSNikolay Aleksandrov 	if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
34897599dc7SEric Dumazet 	    fq->q.meat == fq->q.len) {
34997599dc7SEric Dumazet 		int res;
35097599dc7SEric Dumazet 		unsigned long orefdst = skb->_skb_refdst;
351f61944efSHerbert Xu 
35297599dc7SEric Dumazet 		skb->_skb_refdst = 0UL;
35397599dc7SEric Dumazet 		res = ip6_frag_reasm(fq, prev, dev);
35497599dc7SEric Dumazet 		skb->_skb_refdst = orefdst;
35597599dc7SEric Dumazet 		return res;
35697599dc7SEric Dumazet 	}
35797599dc7SEric Dumazet 
35897599dc7SEric Dumazet 	skb_dst_drop(skb);
359f61944efSHerbert Xu 	return -1;
3601da177e4SLinus Torvalds 
36170789d70SNicolas Dichtel discard_fq:
362b836c99fSAmerigo Wang 	inet_frag_kill(&fq->q, &ip6_frags);
3631da177e4SLinus Torvalds err:
3641d015503SEric Dumazet 	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
3653bd653c8SDenis V. Lunev 			IPSTATS_MIB_REASMFAILS);
3661da177e4SLinus Torvalds 	kfree_skb(skb);
367f61944efSHerbert Xu 	return -1;
3681da177e4SLinus Torvalds }
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds /*
3711da177e4SLinus Torvalds  *	Check if this packet is complete.
3721da177e4SLinus Torvalds  *	Returns NULL on failure by any reason, and pointer
3731da177e4SLinus Torvalds  *	to current nexthdr field in reassembled frame.
3741da177e4SLinus Torvalds  *
3751da177e4SLinus Torvalds  *	It is called with locked fq, and caller must check that
3761da177e4SLinus Torvalds  *	queue is eligible for reassembly i.e. it is not COMPLETE,
3771da177e4SLinus Torvalds  *	the last and the first frames arrived and all the bits are here.
3781da177e4SLinus Torvalds  */
379f61944efSHerbert Xu static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
3801da177e4SLinus Torvalds 			  struct net_device *dev)
3811da177e4SLinus Torvalds {
3822bad35b7SJorge Boncompte [DTI2] 	struct net *net = container_of(fq->q.net, struct net, ipv6.frags);
3835ab11c98SPavel Emelyanov 	struct sk_buff *fp, *head = fq->q.fragments;
3841da177e4SLinus Torvalds 	int    payload_len;
3851da177e4SLinus Torvalds 	unsigned int nhoff;
386ec16439eSEric Dumazet 	int sum_truesize;
387eec2e618SHannes Frederic Sowa 	u8 ecn;
3881da177e4SLinus Torvalds 
389b836c99fSAmerigo Wang 	inet_frag_kill(&fq->q, &ip6_frags);
3901da177e4SLinus Torvalds 
391eec2e618SHannes Frederic Sowa 	ecn = ip_frag_ecn_table[fq->ecn];
392eec2e618SHannes Frederic Sowa 	if (unlikely(ecn == 0xff))
393eec2e618SHannes Frederic Sowa 		goto out_fail;
394eec2e618SHannes Frederic Sowa 
395f61944efSHerbert Xu 	/* Make the one we just received the head. */
396f61944efSHerbert Xu 	if (prev) {
397f61944efSHerbert Xu 		head = prev->next;
398f61944efSHerbert Xu 		fp = skb_clone(head, GFP_ATOMIC);
399f61944efSHerbert Xu 
400f61944efSHerbert Xu 		if (!fp)
401f61944efSHerbert Xu 			goto out_oom;
402f61944efSHerbert Xu 
403f61944efSHerbert Xu 		fp->next = head->next;
404d6bebca9SChangli Gao 		if (!fp->next)
405d6bebca9SChangli Gao 			fq->q.fragments_tail = fp;
406f61944efSHerbert Xu 		prev->next = fp;
407f61944efSHerbert Xu 
4085ab11c98SPavel Emelyanov 		skb_morph(head, fq->q.fragments);
4095ab11c98SPavel Emelyanov 		head->next = fq->q.fragments->next;
410f61944efSHerbert Xu 
411808db80aSEric Dumazet 		consume_skb(fq->q.fragments);
4125ab11c98SPavel Emelyanov 		fq->q.fragments = head;
413f61944efSHerbert Xu 	}
414f61944efSHerbert Xu 
415547b792cSIlpo Järvinen 	WARN_ON(head == NULL);
416547b792cSIlpo Järvinen 	WARN_ON(FRAG6_CB(head)->offset != 0);
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	/* Unfragmented part is taken from the first segment. */
419d56f90a7SArnaldo Carvalho de Melo 	payload_len = ((head->data - skb_network_header(head)) -
4205ab11c98SPavel Emelyanov 		       sizeof(struct ipv6hdr) + fq->q.len -
421d56f90a7SArnaldo Carvalho de Melo 		       sizeof(struct frag_hdr));
4221da177e4SLinus Torvalds 	if (payload_len > IPV6_MAXPLEN)
4231da177e4SLinus Torvalds 		goto out_oversize;
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	/* Head of list must not be cloned. */
42614bbd6a5SPravin B Shelar 	if (skb_unclone(head, GFP_ATOMIC))
4271da177e4SLinus Torvalds 		goto out_oom;
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	/* If the first fragment is fragmented itself, we split
4301da177e4SLinus Torvalds 	 * it to two chunks: the first with data and paged part
4311da177e4SLinus Torvalds 	 * and the second, holding only fragments. */
43221dc3301SDavid S. Miller 	if (skb_has_frag_list(head)) {
4331da177e4SLinus Torvalds 		struct sk_buff *clone;
4341da177e4SLinus Torvalds 		int i, plen = 0;
4351da177e4SLinus Torvalds 
436e5d08d71SIan Morris 		clone = alloc_skb(0, GFP_ATOMIC);
43763159f29SIan Morris 		if (!clone)
4381da177e4SLinus Torvalds 			goto out_oom;
4391da177e4SLinus Torvalds 		clone->next = head->next;
4401da177e4SLinus Torvalds 		head->next = clone;
4411da177e4SLinus Torvalds 		skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
4424d9092bbSDavid S. Miller 		skb_frag_list_init(head);
4431da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
4449e903e08SEric Dumazet 			plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
4451da177e4SLinus Torvalds 		clone->len = clone->data_len = head->data_len - plen;
4461da177e4SLinus Torvalds 		head->data_len -= clone->len;
4471da177e4SLinus Torvalds 		head->len -= clone->len;
4481da177e4SLinus Torvalds 		clone->csum = 0;
4491da177e4SLinus Torvalds 		clone->ip_summed = head->ip_summed;
4500e60d245SFlorian Westphal 		add_frag_mem_limit(fq->q.net, clone->truesize);
4511da177e4SLinus Torvalds 	}
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	/* We have to remove fragment header from datagram and to relocate
4541da177e4SLinus Torvalds 	 * header in order to calculate ICV correctly. */
4551da177e4SLinus Torvalds 	nhoff = fq->nhoffset;
456b0e380b1SArnaldo Carvalho de Melo 	skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
4571da177e4SLinus Torvalds 	memmove(head->head + sizeof(struct frag_hdr), head->head,
4581da177e4SLinus Torvalds 		(head->data - head->head) - sizeof(struct frag_hdr));
459b0e380b1SArnaldo Carvalho de Melo 	head->mac_header += sizeof(struct frag_hdr);
460b0e380b1SArnaldo Carvalho de Melo 	head->network_header += sizeof(struct frag_hdr);
4611da177e4SLinus Torvalds 
462badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(head);
463d56f90a7SArnaldo Carvalho de Melo 	skb_push(head, head->data - skb_network_header(head));
4641da177e4SLinus Torvalds 
465ec16439eSEric Dumazet 	sum_truesize = head->truesize;
466ec16439eSEric Dumazet 	for (fp = head->next; fp;) {
467ec16439eSEric Dumazet 		bool headstolen;
468ec16439eSEric Dumazet 		int delta;
469ec16439eSEric Dumazet 		struct sk_buff *next = fp->next;
470ec16439eSEric Dumazet 
471ec16439eSEric Dumazet 		sum_truesize += fp->truesize;
4721da177e4SLinus Torvalds 		if (head->ip_summed != fp->ip_summed)
4731da177e4SLinus Torvalds 			head->ip_summed = CHECKSUM_NONE;
47484fa7933SPatrick McHardy 		else if (head->ip_summed == CHECKSUM_COMPLETE)
4751da177e4SLinus Torvalds 			head->csum = csum_add(head->csum, fp->csum);
476ec16439eSEric Dumazet 
477ec16439eSEric Dumazet 		if (skb_try_coalesce(head, fp, &headstolen, &delta)) {
478ec16439eSEric Dumazet 			kfree_skb_partial(fp, headstolen);
479ec16439eSEric Dumazet 		} else {
480ec16439eSEric Dumazet 			if (!skb_shinfo(head)->frag_list)
481ec16439eSEric Dumazet 				skb_shinfo(head)->frag_list = fp;
482ec16439eSEric Dumazet 			head->data_len += fp->len;
483ec16439eSEric Dumazet 			head->len += fp->len;
4841da177e4SLinus Torvalds 			head->truesize += fp->truesize;
4851da177e4SLinus Torvalds 		}
486ec16439eSEric Dumazet 		fp = next;
487ec16439eSEric Dumazet 	}
4880e60d245SFlorian Westphal 	sub_frag_mem_limit(fq->q.net, sum_truesize);
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 	head->next = NULL;
4911da177e4SLinus Torvalds 	head->dev = dev;
4925ab11c98SPavel Emelyanov 	head->tstamp = fq->q.stamp;
4930660e03fSArnaldo Carvalho de Melo 	ipv6_hdr(head)->payload_len = htons(payload_len);
494eec2e618SHannes Frederic Sowa 	ipv6_change_dsfield(ipv6_hdr(head), 0xff, ecn);
495951dbc8aSPatrick McHardy 	IP6CB(head)->nhoff = nhoff;
496f46078cfSHannes Frederic Sowa 	IP6CB(head)->flags |= IP6SKB_FRAGMENTED;
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 	/* Yes, and fold redundant checksum back. 8) */
4996b83d28aSDaniel Borkmann 	skb_postpush_rcsum(head, skb_network_header(head),
5006b83d28aSDaniel Borkmann 			   skb_network_header_len(head));
5011da177e4SLinus Torvalds 
502a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
5031d015503SEric Dumazet 	__IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
504a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
5055ab11c98SPavel Emelyanov 	fq->q.fragments = NULL;
506d6bebca9SChangli Gao 	fq->q.fragments_tail = NULL;
5071da177e4SLinus Torvalds 	return 1;
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds out_oversize:
510e87cc472SJoe Perches 	net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len);
5111da177e4SLinus Torvalds 	goto out_fail;
5121da177e4SLinus Torvalds out_oom:
513e87cc472SJoe Perches 	net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n");
5141da177e4SLinus Torvalds out_fail:
515a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
5161d015503SEric Dumazet 	__IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
517a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
5181da177e4SLinus Torvalds 	return -1;
5191da177e4SLinus Torvalds }
5201da177e4SLinus Torvalds 
521e5bbef20SHerbert Xu static int ipv6_frag_rcv(struct sk_buff *skb)
5221da177e4SLinus Torvalds {
5231da177e4SLinus Torvalds 	struct frag_hdr *fhdr;
5241da177e4SLinus Torvalds 	struct frag_queue *fq;
525b71d1d42SEric Dumazet 	const struct ipv6hdr *hdr = ipv6_hdr(skb);
526adf30907SEric Dumazet 	struct net *net = dev_net(skb_dst(skb)->dev);
5271da177e4SLinus Torvalds 
528f46078cfSHannes Frederic Sowa 	if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED)
529f46078cfSHannes Frederic Sowa 		goto fail_hdr;
530f46078cfSHannes Frederic Sowa 
5311d015503SEric Dumazet 	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS);
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 	/* Jumbo payload inhibits frag. header */
53498b3377cSDenis V. Lunev 	if (hdr->payload_len == 0)
53598b3377cSDenis V. Lunev 		goto fail_hdr;
53698b3377cSDenis V. Lunev 
537ea2ae17dSArnaldo Carvalho de Melo 	if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
53898b3377cSDenis V. Lunev 				 sizeof(struct frag_hdr))))
53998b3377cSDenis V. Lunev 		goto fail_hdr;
5401da177e4SLinus Torvalds 
5410660e03fSArnaldo Carvalho de Melo 	hdr = ipv6_hdr(skb);
5429c70220bSArnaldo Carvalho de Melo 	fhdr = (struct frag_hdr *)skb_transport_header(skb);
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(0xFFF9))) {
5451da177e4SLinus Torvalds 		/* It is not a fragmented frame */
546b0e380b1SArnaldo Carvalho de Melo 		skb->transport_header += sizeof(struct frag_hdr);
5471d015503SEric Dumazet 		__IP6_INC_STATS(net,
548adf30907SEric Dumazet 				ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS);
5491da177e4SLinus Torvalds 
550d56f90a7SArnaldo Carvalho de Melo 		IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
551f46078cfSHannes Frederic Sowa 		IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
5521da177e4SLinus Torvalds 		return 1;
5531da177e4SLinus Torvalds 	}
5541da177e4SLinus Torvalds 
555eec2e618SHannes Frederic Sowa 	fq = fq_find(net, fhdr->identification, &hdr->saddr, &hdr->daddr,
556264640fcSMichal Kubeček 		     skb->dev ? skb->dev->ifindex : 0, ip6_frag_ecn(hdr));
55753b24b8fSIan Morris 	if (fq) {
558f61944efSHerbert Xu 		int ret;
5591da177e4SLinus Torvalds 
5605ab11c98SPavel Emelyanov 		spin_lock(&fq->q.lock);
5611da177e4SLinus Torvalds 
562f61944efSHerbert Xu 		ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
5631da177e4SLinus Torvalds 
5645ab11c98SPavel Emelyanov 		spin_unlock(&fq->q.lock);
565b836c99fSAmerigo Wang 		inet_frag_put(&fq->q, &ip6_frags);
5661da177e4SLinus Torvalds 		return ret;
5671da177e4SLinus Torvalds 	}
5681da177e4SLinus Torvalds 
5691d015503SEric Dumazet 	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS);
5701da177e4SLinus Torvalds 	kfree_skb(skb);
5711da177e4SLinus Torvalds 	return -1;
57298b3377cSDenis V. Lunev 
57398b3377cSDenis V. Lunev fail_hdr:
5741d015503SEric Dumazet 	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
575d2373862SNikolay Aleksandrov 			IPSTATS_MIB_INHDRERRORS);
57698b3377cSDenis V. Lunev 	icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
57798b3377cSDenis V. Lunev 	return -1;
5781da177e4SLinus Torvalds }
5791da177e4SLinus Torvalds 
580cc24becaSIan Morris static const struct inet6_protocol frag_protocol = {
5811da177e4SLinus Torvalds 	.handler	=	ipv6_frag_rcv,
5821da177e4SLinus Torvalds 	.flags		=	INET6_PROTO_NOPOLICY,
5831da177e4SLinus Torvalds };
5841da177e4SLinus Torvalds 
5858d8354d2SPavel Emelyanov #ifdef CONFIG_SYSCTL
5861bab4c75SNikolay Aleksandrov static int zero;
5871bab4c75SNikolay Aleksandrov 
5880a64b4b8SPavel Emelyanov static struct ctl_table ip6_frags_ns_ctl_table[] = {
589e71e0349SDaniel Lezcano 	{
5908d8354d2SPavel Emelyanov 		.procname	= "ip6frag_high_thresh",
591e31e0bdcSPavel Emelyanov 		.data		= &init_net.ipv6.frags.high_thresh,
5928d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
5938d8354d2SPavel Emelyanov 		.mode		= 0644,
5941bab4c75SNikolay Aleksandrov 		.proc_handler	= proc_dointvec_minmax,
5951bab4c75SNikolay Aleksandrov 		.extra1		= &init_net.ipv6.frags.low_thresh
5968d8354d2SPavel Emelyanov 	},
5978d8354d2SPavel Emelyanov 	{
5988d8354d2SPavel Emelyanov 		.procname	= "ip6frag_low_thresh",
599e31e0bdcSPavel Emelyanov 		.data		= &init_net.ipv6.frags.low_thresh,
6008d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6018d8354d2SPavel Emelyanov 		.mode		= 0644,
6021bab4c75SNikolay Aleksandrov 		.proc_handler	= proc_dointvec_minmax,
6031bab4c75SNikolay Aleksandrov 		.extra1		= &zero,
6041bab4c75SNikolay Aleksandrov 		.extra2		= &init_net.ipv6.frags.high_thresh
6058d8354d2SPavel Emelyanov 	},
6068d8354d2SPavel Emelyanov 	{
6078d8354d2SPavel Emelyanov 		.procname	= "ip6frag_time",
608b2fd5321SPavel Emelyanov 		.data		= &init_net.ipv6.frags.timeout,
6098d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6108d8354d2SPavel Emelyanov 		.mode		= 0644,
6116d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
6128d8354d2SPavel Emelyanov 	},
6137d291ebbSPavel Emelyanov 	{ }
6147d291ebbSPavel Emelyanov };
6157d291ebbSPavel Emelyanov 
616e3a57d18SFlorian Westphal /* secret interval has been deprecated */
617e3a57d18SFlorian Westphal static int ip6_frags_secret_interval_unused;
6187d291ebbSPavel Emelyanov static struct ctl_table ip6_frags_ctl_table[] = {
6198d8354d2SPavel Emelyanov 	{
6208d8354d2SPavel Emelyanov 		.procname	= "ip6frag_secret_interval",
621e3a57d18SFlorian Westphal 		.data		= &ip6_frags_secret_interval_unused,
6228d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6238d8354d2SPavel Emelyanov 		.mode		= 0644,
6246d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
6258d8354d2SPavel Emelyanov 	},
6268d8354d2SPavel Emelyanov 	{ }
6278d8354d2SPavel Emelyanov };
6287d460db9SDaniel Lezcano 
6292c8c1e72SAlexey Dobriyan static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
6308d8354d2SPavel Emelyanov {
631e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
6328d8354d2SPavel Emelyanov 	struct ctl_table_header *hdr;
6338d8354d2SPavel Emelyanov 
6340a64b4b8SPavel Emelyanov 	table = ip6_frags_ns_ctl_table;
63509ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net)) {
6360a64b4b8SPavel Emelyanov 		table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
63763159f29SIan Morris 		if (!table)
638e4a2d5c2SPavel Emelyanov 			goto err_alloc;
639e4a2d5c2SPavel Emelyanov 
640e31e0bdcSPavel Emelyanov 		table[0].data = &net->ipv6.frags.high_thresh;
6411bab4c75SNikolay Aleksandrov 		table[0].extra1 = &net->ipv6.frags.low_thresh;
6421bab4c75SNikolay Aleksandrov 		table[0].extra2 = &init_net.ipv6.frags.high_thresh;
643e31e0bdcSPavel Emelyanov 		table[1].data = &net->ipv6.frags.low_thresh;
6441bab4c75SNikolay Aleksandrov 		table[1].extra2 = &net->ipv6.frags.high_thresh;
645b2fd5321SPavel Emelyanov 		table[2].data = &net->ipv6.frags.timeout;
646464dc801SEric W. Biederman 
647464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
648464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
649464dc801SEric W. Biederman 			table[0].procname = NULL;
650e4a2d5c2SPavel Emelyanov 	}
651e4a2d5c2SPavel Emelyanov 
652ec8f23ceSEric W. Biederman 	hdr = register_net_sysctl(net, "net/ipv6", table);
65363159f29SIan Morris 	if (!hdr)
654e4a2d5c2SPavel Emelyanov 		goto err_reg;
655e4a2d5c2SPavel Emelyanov 
656e4a2d5c2SPavel Emelyanov 	net->ipv6.sysctl.frags_hdr = hdr;
657e4a2d5c2SPavel Emelyanov 	return 0;
658e4a2d5c2SPavel Emelyanov 
659e4a2d5c2SPavel Emelyanov err_reg:
66009ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net))
661e4a2d5c2SPavel Emelyanov 		kfree(table);
662e4a2d5c2SPavel Emelyanov err_alloc:
663e4a2d5c2SPavel Emelyanov 	return -ENOMEM;
664e4a2d5c2SPavel Emelyanov }
665e4a2d5c2SPavel Emelyanov 
6662c8c1e72SAlexey Dobriyan static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
667e4a2d5c2SPavel Emelyanov {
668e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
669e4a2d5c2SPavel Emelyanov 
670e4a2d5c2SPavel Emelyanov 	table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
671e4a2d5c2SPavel Emelyanov 	unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
6723705e11aSYang Hongyang 	if (!net_eq(net, &init_net))
673e4a2d5c2SPavel Emelyanov 		kfree(table);
6748d8354d2SPavel Emelyanov }
6757d291ebbSPavel Emelyanov 
6767d291ebbSPavel Emelyanov static struct ctl_table_header *ip6_ctl_header;
6777d291ebbSPavel Emelyanov 
6787d291ebbSPavel Emelyanov static int ip6_frags_sysctl_register(void)
6797d291ebbSPavel Emelyanov {
68043444757SEric W. Biederman 	ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6",
6817d291ebbSPavel Emelyanov 			ip6_frags_ctl_table);
6827d291ebbSPavel Emelyanov 	return ip6_ctl_header == NULL ? -ENOMEM : 0;
6837d291ebbSPavel Emelyanov }
6847d291ebbSPavel Emelyanov 
6857d291ebbSPavel Emelyanov static void ip6_frags_sysctl_unregister(void)
6867d291ebbSPavel Emelyanov {
6877d291ebbSPavel Emelyanov 	unregister_net_sysctl_table(ip6_ctl_header);
6887d291ebbSPavel Emelyanov }
6898d8354d2SPavel Emelyanov #else
690fc08c258SFabian Frederick static int ip6_frags_ns_sysctl_register(struct net *net)
6918d8354d2SPavel Emelyanov {
6928d8354d2SPavel Emelyanov 	return 0;
6938d8354d2SPavel Emelyanov }
694e4a2d5c2SPavel Emelyanov 
695fc08c258SFabian Frederick static void ip6_frags_ns_sysctl_unregister(struct net *net)
696e4a2d5c2SPavel Emelyanov {
697e4a2d5c2SPavel Emelyanov }
6987d291ebbSPavel Emelyanov 
699fc08c258SFabian Frederick static int ip6_frags_sysctl_register(void)
7007d291ebbSPavel Emelyanov {
7017d291ebbSPavel Emelyanov 	return 0;
7027d291ebbSPavel Emelyanov }
7037d291ebbSPavel Emelyanov 
704fc08c258SFabian Frederick static void ip6_frags_sysctl_unregister(void)
7057d291ebbSPavel Emelyanov {
7067d291ebbSPavel Emelyanov }
7078d8354d2SPavel Emelyanov #endif
7088d8354d2SPavel Emelyanov 
7092c8c1e72SAlexey Dobriyan static int __net_init ipv6_frags_init_net(struct net *net)
7108d8354d2SPavel Emelyanov {
7111d6119baSEric Dumazet 	int res;
7121d6119baSEric Dumazet 
7137c070aa9SShan Wei 	net->ipv6.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
7147c070aa9SShan Wei 	net->ipv6.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
715b2fd5321SPavel Emelyanov 	net->ipv6.frags.timeout = IPV6_FRAG_TIMEOUT;
7168d8354d2SPavel Emelyanov 
7171d6119baSEric Dumazet 	res = inet_frags_init_net(&net->ipv6.frags);
7181d6119baSEric Dumazet 	if (res)
7191d6119baSEric Dumazet 		return res;
7201d6119baSEric Dumazet 	res = ip6_frags_ns_sysctl_register(net);
7211d6119baSEric Dumazet 	if (res)
7221d6119baSEric Dumazet 		inet_frags_uninit_net(&net->ipv6.frags);
7231d6119baSEric Dumazet 	return res;
724e71e0349SDaniel Lezcano }
725e71e0349SDaniel Lezcano 
7262c8c1e72SAlexey Dobriyan static void __net_exit ipv6_frags_exit_net(struct net *net)
72781566e83SPavel Emelyanov {
7280a64b4b8SPavel Emelyanov 	ip6_frags_ns_sysctl_unregister(net);
72981566e83SPavel Emelyanov 	inet_frags_exit_net(&net->ipv6.frags, &ip6_frags);
73081566e83SPavel Emelyanov }
73181566e83SPavel Emelyanov 
73281566e83SPavel Emelyanov static struct pernet_operations ip6_frags_ops = {
73381566e83SPavel Emelyanov 	.init = ipv6_frags_init_net,
73481566e83SPavel Emelyanov 	.exit = ipv6_frags_exit_net,
73581566e83SPavel Emelyanov };
73681566e83SPavel Emelyanov 
737853cbbaaSDaniel Lezcano int __init ipv6_frag_init(void)
7381da177e4SLinus Torvalds {
739853cbbaaSDaniel Lezcano 	int ret;
7401da177e4SLinus Torvalds 
741853cbbaaSDaniel Lezcano 	ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
742853cbbaaSDaniel Lezcano 	if (ret)
743853cbbaaSDaniel Lezcano 		goto out;
744e71e0349SDaniel Lezcano 
7457d291ebbSPavel Emelyanov 	ret = ip6_frags_sysctl_register();
7467d291ebbSPavel Emelyanov 	if (ret)
7477d291ebbSPavel Emelyanov 		goto err_sysctl;
7487d291ebbSPavel Emelyanov 
7490002c630SPavel Emelyanov 	ret = register_pernet_subsys(&ip6_frags_ops);
7500002c630SPavel Emelyanov 	if (ret)
7510002c630SPavel Emelyanov 		goto err_pernet;
7528d8354d2SPavel Emelyanov 
753321a3a99SPavel Emelyanov 	ip6_frags.hashfn = ip6_hashfn;
754c6fda282SPavel Emelyanov 	ip6_frags.constructor = ip6_frag_init;
755c9547709SPavel Emelyanov 	ip6_frags.destructor = NULL;
7561e4b8287SPavel Emelyanov 	ip6_frags.qsize = sizeof(struct frag_queue);
757abd6523dSPavel Emelyanov 	ip6_frags.match = ip6_frag_match;
758e521db9dSPavel Emelyanov 	ip6_frags.frag_expire = ip6_frag_expire;
759d4ad4d22SNikolay Aleksandrov 	ip6_frags.frags_cache_name = ip6_frag_cache_name;
760d4ad4d22SNikolay Aleksandrov 	ret = inet_frags_init(&ip6_frags);
761d4ad4d22SNikolay Aleksandrov 	if (ret)
762d4ad4d22SNikolay Aleksandrov 		goto err_pernet;
763853cbbaaSDaniel Lezcano out:
764853cbbaaSDaniel Lezcano 	return ret;
7650002c630SPavel Emelyanov 
7660002c630SPavel Emelyanov err_pernet:
7677d291ebbSPavel Emelyanov 	ip6_frags_sysctl_unregister();
7687d291ebbSPavel Emelyanov err_sysctl:
7690002c630SPavel Emelyanov 	inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
7700002c630SPavel Emelyanov 	goto out;
771853cbbaaSDaniel Lezcano }
772853cbbaaSDaniel Lezcano 
773853cbbaaSDaniel Lezcano void ipv6_frag_exit(void)
774853cbbaaSDaniel Lezcano {
775853cbbaaSDaniel Lezcano 	inet_frags_fini(&ip6_frags);
7767d291ebbSPavel Emelyanov 	ip6_frags_sysctl_unregister();
77781566e83SPavel Emelyanov 	unregister_pernet_subsys(&ip6_frags_ops);
778853cbbaaSDaniel Lezcano 	inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
7791da177e4SLinus Torvalds }
780