xref: /openbmc/linux/net/ipv6/reassembly.c (revision 5a3da1fe)
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>
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds struct ip6frag_skb_cb
631da177e4SLinus Torvalds {
641da177e4SLinus Torvalds 	struct inet6_skb_parm	h;
651da177e4SLinus Torvalds 	int			offset;
661da177e4SLinus Torvalds };
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds #define FRAG6_CB(skb)	((struct ip6frag_skb_cb*)((skb)->cb))
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds 
717eb95156SPavel Emelyanov static struct inet_frags ip6_frags;
721da177e4SLinus Torvalds 
73f61944efSHerbert Xu static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
74f61944efSHerbert Xu 			  struct net_device *dev);
75f61944efSHerbert Xu 
76f6596f9dSZach Brown /*
77f6596f9dSZach Brown  * callers should be careful not to use the hash value outside the ipfrag_lock
78f6596f9dSZach Brown  * as doing so could race with ipfrag_hash_rnd being recalculated.
79f6596f9dSZach Brown  */
8093c8b90fSIlpo Järvinen unsigned int inet6_hash_frag(__be32 id, const struct in6_addr *saddr,
8193c8b90fSIlpo Järvinen 			     const struct in6_addr *daddr, u32 rnd)
821da177e4SLinus Torvalds {
8382a39eb6SJozsef Kadlecsik 	u32 c;
841da177e4SLinus Torvalds 
858064b3cfSEric Dumazet 	c = jhash_3words(ipv6_addr_hash(saddr), ipv6_addr_hash(daddr),
868064b3cfSEric Dumazet 			 (__force u32)id, rnd);
871da177e4SLinus Torvalds 
887eb95156SPavel Emelyanov 	return c & (INETFRAGS_HASHSZ - 1);
891da177e4SLinus Torvalds }
9093c8b90fSIlpo Järvinen EXPORT_SYMBOL_GPL(inet6_hash_frag);
911da177e4SLinus Torvalds 
92321a3a99SPavel Emelyanov static unsigned int ip6_hashfn(struct inet_frag_queue *q)
931da177e4SLinus Torvalds {
94321a3a99SPavel Emelyanov 	struct frag_queue *fq;
951da177e4SLinus Torvalds 
96321a3a99SPavel Emelyanov 	fq = container_of(q, struct frag_queue, q);
9793c8b90fSIlpo Järvinen 	return inet6_hash_frag(fq->id, &fq->saddr, &fq->daddr, ip6_frags.rnd);
981da177e4SLinus Torvalds }
991da177e4SLinus Torvalds 
100cbc264caSEric Dumazet bool ip6_frag_match(struct inet_frag_queue *q, void *a)
101abd6523dSPavel Emelyanov {
102abd6523dSPavel Emelyanov 	struct frag_queue *fq;
103abd6523dSPavel Emelyanov 	struct ip6_create_arg *arg = a;
104abd6523dSPavel Emelyanov 
105abd6523dSPavel Emelyanov 	fq = container_of(q, struct frag_queue, q);
106cbc264caSEric Dumazet 	return	fq->id == arg->id &&
107cbc264caSEric Dumazet 		fq->user == arg->user &&
108abd6523dSPavel Emelyanov 		ipv6_addr_equal(&fq->saddr, arg->src) &&
109cbc264caSEric Dumazet 		ipv6_addr_equal(&fq->daddr, arg->dst);
110abd6523dSPavel Emelyanov }
111abd6523dSPavel Emelyanov EXPORT_SYMBOL(ip6_frag_match);
112abd6523dSPavel Emelyanov 
113c6fda282SPavel Emelyanov void ip6_frag_init(struct inet_frag_queue *q, void *a)
114c6fda282SPavel Emelyanov {
115c6fda282SPavel Emelyanov 	struct frag_queue *fq = container_of(q, struct frag_queue, q);
116c6fda282SPavel Emelyanov 	struct ip6_create_arg *arg = a;
117c6fda282SPavel Emelyanov 
118c6fda282SPavel Emelyanov 	fq->id = arg->id;
1190b5ccb2eSPatrick McHardy 	fq->user = arg->user;
1204e3fd7a0SAlexey Dobriyan 	fq->saddr = *arg->src;
1214e3fd7a0SAlexey Dobriyan 	fq->daddr = *arg->dst;
122c6fda282SPavel Emelyanov }
123c6fda282SPavel Emelyanov EXPORT_SYMBOL(ip6_frag_init);
124c6fda282SPavel Emelyanov 
125b836c99fSAmerigo Wang void ip6_expire_frag_queue(struct net *net, struct frag_queue *fq,
126b836c99fSAmerigo Wang 			   struct inet_frags *frags)
1271da177e4SLinus Torvalds {
128a11d206dSYOSHIFUJI Hideaki 	struct net_device *dev = NULL;
129e521db9dSPavel Emelyanov 
1305ab11c98SPavel Emelyanov 	spin_lock(&fq->q.lock);
1311da177e4SLinus Torvalds 
132bc578a54SJoe Perches 	if (fq->q.last_in & INET_FRAG_COMPLETE)
1331da177e4SLinus Torvalds 		goto out;
1341da177e4SLinus Torvalds 
135b836c99fSAmerigo Wang 	inet_frag_kill(&fq->q, frags);
1361da177e4SLinus Torvalds 
137a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
13869df9d59SEric Dumazet 	dev = dev_get_by_index_rcu(net, fq->iif);
13969df9d59SEric Dumazet 	if (!dev)
14069df9d59SEric Dumazet 		goto out_rcu_unlock;
14169df9d59SEric Dumazet 
142483a47d2SDenis V. Lunev 	IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
143483a47d2SDenis V. Lunev 	IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
1441da177e4SLinus Torvalds 
14578c784c4SIngo Oeser 	/* Don't send error if the first segment did not arrive. */
146bc578a54SJoe Perches 	if (!(fq->q.last_in & INET_FRAG_FIRST_IN) || !fq->q.fragments)
14769df9d59SEric Dumazet 		goto out_rcu_unlock;
14878c784c4SIngo Oeser 
1491da177e4SLinus Torvalds 	/*
1501da177e4SLinus Torvalds 	   But use as source device on which LAST ARRIVED
1511da177e4SLinus Torvalds 	   segment was received. And do not use fq->dev
1521da177e4SLinus Torvalds 	   pointer directly, device might already disappeared.
1531da177e4SLinus Torvalds 	 */
1545ab11c98SPavel Emelyanov 	fq->q.fragments->dev = dev;
1553ffe533cSAlexey Dobriyan 	icmpv6_send(fq->q.fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0);
15669df9d59SEric Dumazet out_rcu_unlock:
15769df9d59SEric Dumazet 	rcu_read_unlock();
1581da177e4SLinus Torvalds out:
1595ab11c98SPavel Emelyanov 	spin_unlock(&fq->q.lock);
160b836c99fSAmerigo Wang 	inet_frag_put(&fq->q, frags);
161b836c99fSAmerigo Wang }
162b836c99fSAmerigo Wang EXPORT_SYMBOL(ip6_expire_frag_queue);
163b836c99fSAmerigo Wang 
164b836c99fSAmerigo Wang static void ip6_frag_expire(unsigned long data)
165b836c99fSAmerigo Wang {
166b836c99fSAmerigo Wang 	struct frag_queue *fq;
167b836c99fSAmerigo Wang 	struct net *net;
168b836c99fSAmerigo Wang 
169b836c99fSAmerigo Wang 	fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q);
170b836c99fSAmerigo Wang 	net = container_of(fq->q.net, struct net, ipv6.frags);
171b836c99fSAmerigo Wang 
172b836c99fSAmerigo Wang 	ip6_expire_frag_queue(net, fq, &ip6_frags);
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds 
175abd6523dSPavel Emelyanov static __inline__ struct frag_queue *
176b71d1d42SEric Dumazet fq_find(struct net *net, __be32 id, const struct in6_addr *src, const struct in6_addr *dst)
1771da177e4SLinus Torvalds {
178c6fda282SPavel Emelyanov 	struct inet_frag_queue *q;
179c6fda282SPavel Emelyanov 	struct ip6_create_arg arg;
180abd6523dSPavel Emelyanov 	unsigned int hash;
1811da177e4SLinus Torvalds 
182c6fda282SPavel Emelyanov 	arg.id = id;
1830b5ccb2eSPatrick McHardy 	arg.user = IP6_DEFRAG_LOCAL_DELIVER;
184c6fda282SPavel Emelyanov 	arg.src = src;
185c6fda282SPavel Emelyanov 	arg.dst = dst;
1869a375803SPavel Emelyanov 
1879a375803SPavel Emelyanov 	read_lock(&ip6_frags.lock);
18893c8b90fSIlpo Järvinen 	hash = inet6_hash_frag(id, src, dst, ip6_frags.rnd);
189c6fda282SPavel Emelyanov 
190ac18e750SPavel Emelyanov 	q = inet_frag_find(&net->ipv6.frags, &ip6_frags, &arg, hash);
1915a3da1feSHannes Frederic Sowa 	if (IS_ERR_OR_NULL(q)) {
1925a3da1feSHannes Frederic Sowa 		inet_frag_maybe_warn_overflow(q, pr_fmt());
1939546377cSShan Wei 		return NULL;
1945a3da1feSHannes Frederic Sowa 	}
195c6fda282SPavel Emelyanov 	return container_of(q, struct frag_queue, q);
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
198f61944efSHerbert Xu static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
1991da177e4SLinus Torvalds 			   struct frag_hdr *fhdr, int nhoff)
2001da177e4SLinus Torvalds {
2011da177e4SLinus Torvalds 	struct sk_buff *prev, *next;
202f61944efSHerbert Xu 	struct net_device *dev;
2031da177e4SLinus Torvalds 	int offset, end;
204adf30907SEric Dumazet 	struct net *net = dev_net(skb_dst(skb)->dev);
2051da177e4SLinus Torvalds 
206bc578a54SJoe Perches 	if (fq->q.last_in & INET_FRAG_COMPLETE)
2071da177e4SLinus Torvalds 		goto err;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	offset = ntohs(fhdr->frag_off) & ~0x7;
2100660e03fSArnaldo Carvalho de Melo 	end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
2110660e03fSArnaldo Carvalho de Melo 			((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 	if ((unsigned int)end > IPV6_MAXPLEN) {
214adf30907SEric Dumazet 		IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
215a11d206dSYOSHIFUJI Hideaki 				 IPSTATS_MIB_INHDRERRORS);
216d56f90a7SArnaldo Carvalho de Melo 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
217d56f90a7SArnaldo Carvalho de Melo 				  ((u8 *)&fhdr->frag_off -
218d56f90a7SArnaldo Carvalho de Melo 				   skb_network_header(skb)));
219f61944efSHerbert Xu 		return -1;
2201da177e4SLinus Torvalds 	}
2211da177e4SLinus Torvalds 
222d56f90a7SArnaldo Carvalho de Melo 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
223d56f90a7SArnaldo Carvalho de Melo 		const unsigned char *nh = skb_network_header(skb);
2241da177e4SLinus Torvalds 		skb->csum = csum_sub(skb->csum,
225d56f90a7SArnaldo Carvalho de Melo 				     csum_partial(nh, (u8 *)(fhdr + 1) - nh,
226d56f90a7SArnaldo Carvalho de Melo 						  0));
227d56f90a7SArnaldo Carvalho de Melo 	}
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	/* Is this the final fragment? */
2301da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(IP6_MF))) {
2311da177e4SLinus Torvalds 		/* If we already have some bits beyond end
2321da177e4SLinus Torvalds 		 * or have different end, the segment is corrupted.
2331da177e4SLinus Torvalds 		 */
2345ab11c98SPavel Emelyanov 		if (end < fq->q.len ||
235bc578a54SJoe Perches 		    ((fq->q.last_in & INET_FRAG_LAST_IN) && end != fq->q.len))
2361da177e4SLinus Torvalds 			goto err;
237bc578a54SJoe Perches 		fq->q.last_in |= INET_FRAG_LAST_IN;
2385ab11c98SPavel Emelyanov 		fq->q.len = end;
2391da177e4SLinus Torvalds 	} else {
2401da177e4SLinus Torvalds 		/* Check if the fragment is rounded to 8 bytes.
2411da177e4SLinus Torvalds 		 * Required by the RFC.
2421da177e4SLinus Torvalds 		 */
2431da177e4SLinus Torvalds 		if (end & 0x7) {
2441da177e4SLinus Torvalds 			/* RFC2460 says always send parameter problem in
2451da177e4SLinus Torvalds 			 * this case. -DaveM
2461da177e4SLinus Torvalds 			 */
247adf30907SEric Dumazet 			IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
248a11d206dSYOSHIFUJI Hideaki 					 IPSTATS_MIB_INHDRERRORS);
2491da177e4SLinus Torvalds 			icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
2501da177e4SLinus Torvalds 					  offsetof(struct ipv6hdr, payload_len));
251f61944efSHerbert Xu 			return -1;
2521da177e4SLinus Torvalds 		}
2535ab11c98SPavel Emelyanov 		if (end > fq->q.len) {
2541da177e4SLinus Torvalds 			/* Some bits beyond end -> corruption. */
255bc578a54SJoe Perches 			if (fq->q.last_in & INET_FRAG_LAST_IN)
2561da177e4SLinus Torvalds 				goto err;
2575ab11c98SPavel Emelyanov 			fq->q.len = end;
2581da177e4SLinus Torvalds 		}
2591da177e4SLinus Torvalds 	}
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	if (end == offset)
2621da177e4SLinus Torvalds 		goto err;
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	/* Point into the IP datagram 'data' part. */
2651da177e4SLinus Torvalds 	if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
2661da177e4SLinus Torvalds 		goto err;
26742ca89c1SStephen Hemminger 
26842ca89c1SStephen Hemminger 	if (pskb_trim_rcsum(skb, end - offset))
2691da177e4SLinus Torvalds 		goto err;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	/* Find out which fragments are in front and at the back of us
2721da177e4SLinus Torvalds 	 * in the chain of fragments so far.  We must know where to put
2731da177e4SLinus Torvalds 	 * this fragment, right?
2741da177e4SLinus Torvalds 	 */
275d6bebca9SChangli Gao 	prev = fq->q.fragments_tail;
276d6bebca9SChangli Gao 	if (!prev || FRAG6_CB(prev)->offset < offset) {
277d6bebca9SChangli Gao 		next = NULL;
278d6bebca9SChangli Gao 		goto found;
279d6bebca9SChangli Gao 	}
2801da177e4SLinus Torvalds 	prev = NULL;
2815ab11c98SPavel Emelyanov 	for(next = fq->q.fragments; next != NULL; next = next->next) {
2821da177e4SLinus Torvalds 		if (FRAG6_CB(next)->offset >= offset)
2831da177e4SLinus Torvalds 			break;	/* bingo! */
2841da177e4SLinus Torvalds 		prev = next;
2851da177e4SLinus Torvalds 	}
2861da177e4SLinus Torvalds 
287d6bebca9SChangli Gao found:
2885de658f8SEric Dumazet 	/* RFC5722, Section 4, amended by Errata ID : 3089
28970789d70SNicolas Dichtel 	 *                          When reassembling an IPv6 datagram, if
29070789d70SNicolas Dichtel 	 *   one or more its constituent fragments is determined to be an
29170789d70SNicolas Dichtel 	 *   overlapping fragment, the entire datagram (and any constituent
2925de658f8SEric Dumazet 	 *   fragments) MUST be silently discarded.
2931da177e4SLinus Torvalds 	 */
2941da177e4SLinus Torvalds 
29570789d70SNicolas Dichtel 	/* Check for overlap with preceding fragment. */
29670789d70SNicolas Dichtel 	if (prev &&
297f4642141SShan Wei 	    (FRAG6_CB(prev)->offset + prev->len) > offset)
29870789d70SNicolas Dichtel 		goto discard_fq;
2991da177e4SLinus Torvalds 
30070789d70SNicolas Dichtel 	/* Look for overlap with succeeding segment. */
30170789d70SNicolas Dichtel 	if (next && FRAG6_CB(next)->offset < end)
30270789d70SNicolas Dichtel 		goto discard_fq;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	FRAG6_CB(skb)->offset = offset;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	/* Insert this fragment in the chain of fragments. */
3071da177e4SLinus Torvalds 	skb->next = next;
308d6bebca9SChangli Gao 	if (!next)
309d6bebca9SChangli Gao 		fq->q.fragments_tail = skb;
3101da177e4SLinus Torvalds 	if (prev)
3111da177e4SLinus Torvalds 		prev->next = skb;
3121da177e4SLinus Torvalds 	else
3135ab11c98SPavel Emelyanov 		fq->q.fragments = skb;
3141da177e4SLinus Torvalds 
315f61944efSHerbert Xu 	dev = skb->dev;
316f61944efSHerbert Xu 	if (dev) {
317f61944efSHerbert Xu 		fq->iif = dev->ifindex;
3181da177e4SLinus Torvalds 		skb->dev = NULL;
319f61944efSHerbert Xu 	}
3205ab11c98SPavel Emelyanov 	fq->q.stamp = skb->tstamp;
3215ab11c98SPavel Emelyanov 	fq->q.meat += skb->len;
322d433673eSJesper Dangaard Brouer 	add_frag_mem_limit(&fq->q, skb->truesize);
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	/* The first fragment.
3251da177e4SLinus Torvalds 	 * nhoffset is obtained from the first fragment, of course.
3261da177e4SLinus Torvalds 	 */
3271da177e4SLinus Torvalds 	if (offset == 0) {
3281da177e4SLinus Torvalds 		fq->nhoffset = nhoff;
329bc578a54SJoe Perches 		fq->q.last_in |= INET_FRAG_FIRST_IN;
3301da177e4SLinus Torvalds 	}
331f61944efSHerbert Xu 
332bc578a54SJoe Perches 	if (fq->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
333bc578a54SJoe Perches 	    fq->q.meat == fq->q.len)
334f61944efSHerbert Xu 		return ip6_frag_reasm(fq, prev, dev);
335f61944efSHerbert Xu 
3363ef0eb0dSJesper Dangaard Brouer 	inet_frag_lru_move(&fq->q);
337f61944efSHerbert Xu 	return -1;
3381da177e4SLinus Torvalds 
33970789d70SNicolas Dichtel discard_fq:
340b836c99fSAmerigo Wang 	inet_frag_kill(&fq->q, &ip6_frags);
3411da177e4SLinus Torvalds err:
342adf30907SEric Dumazet 	IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
3433bd653c8SDenis V. Lunev 		      IPSTATS_MIB_REASMFAILS);
3441da177e4SLinus Torvalds 	kfree_skb(skb);
345f61944efSHerbert Xu 	return -1;
3461da177e4SLinus Torvalds }
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds /*
3491da177e4SLinus Torvalds  *	Check if this packet is complete.
3501da177e4SLinus Torvalds  *	Returns NULL on failure by any reason, and pointer
3511da177e4SLinus Torvalds  *	to current nexthdr field in reassembled frame.
3521da177e4SLinus Torvalds  *
3531da177e4SLinus Torvalds  *	It is called with locked fq, and caller must check that
3541da177e4SLinus Torvalds  *	queue is eligible for reassembly i.e. it is not COMPLETE,
3551da177e4SLinus Torvalds  *	the last and the first frames arrived and all the bits are here.
3561da177e4SLinus Torvalds  */
357f61944efSHerbert Xu static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
3581da177e4SLinus Torvalds 			  struct net_device *dev)
3591da177e4SLinus Torvalds {
3602bad35b7SJorge Boncompte [DTI2] 	struct net *net = container_of(fq->q.net, struct net, ipv6.frags);
3615ab11c98SPavel Emelyanov 	struct sk_buff *fp, *head = fq->q.fragments;
3621da177e4SLinus Torvalds 	int    payload_len;
3631da177e4SLinus Torvalds 	unsigned int nhoff;
364ec16439eSEric Dumazet 	int sum_truesize;
3651da177e4SLinus Torvalds 
366b836c99fSAmerigo Wang 	inet_frag_kill(&fq->q, &ip6_frags);
3671da177e4SLinus Torvalds 
368f61944efSHerbert Xu 	/* Make the one we just received the head. */
369f61944efSHerbert Xu 	if (prev) {
370f61944efSHerbert Xu 		head = prev->next;
371f61944efSHerbert Xu 		fp = skb_clone(head, GFP_ATOMIC);
372f61944efSHerbert Xu 
373f61944efSHerbert Xu 		if (!fp)
374f61944efSHerbert Xu 			goto out_oom;
375f61944efSHerbert Xu 
376f61944efSHerbert Xu 		fp->next = head->next;
377d6bebca9SChangli Gao 		if (!fp->next)
378d6bebca9SChangli Gao 			fq->q.fragments_tail = fp;
379f61944efSHerbert Xu 		prev->next = fp;
380f61944efSHerbert Xu 
3815ab11c98SPavel Emelyanov 		skb_morph(head, fq->q.fragments);
3825ab11c98SPavel Emelyanov 		head->next = fq->q.fragments->next;
383f61944efSHerbert Xu 
384808db80aSEric Dumazet 		consume_skb(fq->q.fragments);
3855ab11c98SPavel Emelyanov 		fq->q.fragments = head;
386f61944efSHerbert Xu 	}
387f61944efSHerbert Xu 
388547b792cSIlpo Järvinen 	WARN_ON(head == NULL);
389547b792cSIlpo Järvinen 	WARN_ON(FRAG6_CB(head)->offset != 0);
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	/* Unfragmented part is taken from the first segment. */
392d56f90a7SArnaldo Carvalho de Melo 	payload_len = ((head->data - skb_network_header(head)) -
3935ab11c98SPavel Emelyanov 		       sizeof(struct ipv6hdr) + fq->q.len -
394d56f90a7SArnaldo Carvalho de Melo 		       sizeof(struct frag_hdr));
3951da177e4SLinus Torvalds 	if (payload_len > IPV6_MAXPLEN)
3961da177e4SLinus Torvalds 		goto out_oversize;
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	/* Head of list must not be cloned. */
39914bbd6a5SPravin B Shelar 	if (skb_unclone(head, GFP_ATOMIC))
4001da177e4SLinus Torvalds 		goto out_oom;
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 	/* If the first fragment is fragmented itself, we split
4031da177e4SLinus Torvalds 	 * it to two chunks: the first with data and paged part
4041da177e4SLinus Torvalds 	 * and the second, holding only fragments. */
40521dc3301SDavid S. Miller 	if (skb_has_frag_list(head)) {
4061da177e4SLinus Torvalds 		struct sk_buff *clone;
4071da177e4SLinus Torvalds 		int i, plen = 0;
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 		if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
4101da177e4SLinus Torvalds 			goto out_oom;
4111da177e4SLinus Torvalds 		clone->next = head->next;
4121da177e4SLinus Torvalds 		head->next = clone;
4131da177e4SLinus Torvalds 		skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
4144d9092bbSDavid S. Miller 		skb_frag_list_init(head);
4151da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
4169e903e08SEric Dumazet 			plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
4171da177e4SLinus Torvalds 		clone->len = clone->data_len = head->data_len - plen;
4181da177e4SLinus Torvalds 		head->data_len -= clone->len;
4191da177e4SLinus Torvalds 		head->len -= clone->len;
4201da177e4SLinus Torvalds 		clone->csum = 0;
4211da177e4SLinus Torvalds 		clone->ip_summed = head->ip_summed;
422d433673eSJesper Dangaard Brouer 		add_frag_mem_limit(&fq->q, clone->truesize);
4231da177e4SLinus Torvalds 	}
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	/* We have to remove fragment header from datagram and to relocate
4261da177e4SLinus Torvalds 	 * header in order to calculate ICV correctly. */
4271da177e4SLinus Torvalds 	nhoff = fq->nhoffset;
428b0e380b1SArnaldo Carvalho de Melo 	skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
4291da177e4SLinus Torvalds 	memmove(head->head + sizeof(struct frag_hdr), head->head,
4301da177e4SLinus Torvalds 		(head->data - head->head) - sizeof(struct frag_hdr));
431b0e380b1SArnaldo Carvalho de Melo 	head->mac_header += sizeof(struct frag_hdr);
432b0e380b1SArnaldo Carvalho de Melo 	head->network_header += sizeof(struct frag_hdr);
4331da177e4SLinus Torvalds 
434badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(head);
435d56f90a7SArnaldo Carvalho de Melo 	skb_push(head, head->data - skb_network_header(head));
4361da177e4SLinus Torvalds 
437ec16439eSEric Dumazet 	sum_truesize = head->truesize;
438ec16439eSEric Dumazet 	for (fp = head->next; fp;) {
439ec16439eSEric Dumazet 		bool headstolen;
440ec16439eSEric Dumazet 		int delta;
441ec16439eSEric Dumazet 		struct sk_buff *next = fp->next;
442ec16439eSEric Dumazet 
443ec16439eSEric Dumazet 		sum_truesize += fp->truesize;
4441da177e4SLinus Torvalds 		if (head->ip_summed != fp->ip_summed)
4451da177e4SLinus Torvalds 			head->ip_summed = CHECKSUM_NONE;
44684fa7933SPatrick McHardy 		else if (head->ip_summed == CHECKSUM_COMPLETE)
4471da177e4SLinus Torvalds 			head->csum = csum_add(head->csum, fp->csum);
448ec16439eSEric Dumazet 
449ec16439eSEric Dumazet 		if (skb_try_coalesce(head, fp, &headstolen, &delta)) {
450ec16439eSEric Dumazet 			kfree_skb_partial(fp, headstolen);
451ec16439eSEric Dumazet 		} else {
452ec16439eSEric Dumazet 			if (!skb_shinfo(head)->frag_list)
453ec16439eSEric Dumazet 				skb_shinfo(head)->frag_list = fp;
454ec16439eSEric Dumazet 			head->data_len += fp->len;
455ec16439eSEric Dumazet 			head->len += fp->len;
4561da177e4SLinus Torvalds 			head->truesize += fp->truesize;
4571da177e4SLinus Torvalds 		}
458ec16439eSEric Dumazet 		fp = next;
459ec16439eSEric Dumazet 	}
460d433673eSJesper Dangaard Brouer 	sub_frag_mem_limit(&fq->q, sum_truesize);
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	head->next = NULL;
4631da177e4SLinus Torvalds 	head->dev = dev;
4645ab11c98SPavel Emelyanov 	head->tstamp = fq->q.stamp;
4650660e03fSArnaldo Carvalho de Melo 	ipv6_hdr(head)->payload_len = htons(payload_len);
466951dbc8aSPatrick McHardy 	IP6CB(head)->nhoff = nhoff;
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	/* Yes, and fold redundant checksum back. 8) */
46984fa7933SPatrick McHardy 	if (head->ip_summed == CHECKSUM_COMPLETE)
470d56f90a7SArnaldo Carvalho de Melo 		head->csum = csum_partial(skb_network_header(head),
471cfe1fc77SArnaldo Carvalho de Melo 					  skb_network_header_len(head),
472d56f90a7SArnaldo Carvalho de Melo 					  head->csum);
4731da177e4SLinus Torvalds 
474a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
4752bad35b7SJorge Boncompte [DTI2] 	IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
476a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
4775ab11c98SPavel Emelyanov 	fq->q.fragments = NULL;
478d6bebca9SChangli Gao 	fq->q.fragments_tail = NULL;
4791da177e4SLinus Torvalds 	return 1;
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds out_oversize:
482e87cc472SJoe Perches 	net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len);
4831da177e4SLinus Torvalds 	goto out_fail;
4841da177e4SLinus Torvalds out_oom:
485e87cc472SJoe Perches 	net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n");
4861da177e4SLinus Torvalds out_fail:
487a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
4882bad35b7SJorge Boncompte [DTI2] 	IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
489a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
4901da177e4SLinus Torvalds 	return -1;
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds 
493e5bbef20SHerbert Xu static int ipv6_frag_rcv(struct sk_buff *skb)
4941da177e4SLinus Torvalds {
4951da177e4SLinus Torvalds 	struct frag_hdr *fhdr;
4961da177e4SLinus Torvalds 	struct frag_queue *fq;
497b71d1d42SEric Dumazet 	const struct ipv6hdr *hdr = ipv6_hdr(skb);
498adf30907SEric Dumazet 	struct net *net = dev_net(skb_dst(skb)->dev);
4996b102865SAmerigo Wang 	int evicted;
5001da177e4SLinus Torvalds 
501adf30907SEric Dumazet 	IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS);
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 	/* Jumbo payload inhibits frag. header */
50498b3377cSDenis V. Lunev 	if (hdr->payload_len==0)
50598b3377cSDenis V. Lunev 		goto fail_hdr;
50698b3377cSDenis V. Lunev 
507ea2ae17dSArnaldo Carvalho de Melo 	if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
50898b3377cSDenis V. Lunev 				 sizeof(struct frag_hdr))))
50998b3377cSDenis V. Lunev 		goto fail_hdr;
5101da177e4SLinus Torvalds 
5110660e03fSArnaldo Carvalho de Melo 	hdr = ipv6_hdr(skb);
5129c70220bSArnaldo Carvalho de Melo 	fhdr = (struct frag_hdr *)skb_transport_header(skb);
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(0xFFF9))) {
5151da177e4SLinus Torvalds 		/* It is not a fragmented frame */
516b0e380b1SArnaldo Carvalho de Melo 		skb->transport_header += sizeof(struct frag_hdr);
517483a47d2SDenis V. Lunev 		IP6_INC_STATS_BH(net,
518adf30907SEric Dumazet 				 ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS);
5191da177e4SLinus Torvalds 
520d56f90a7SArnaldo Carvalho de Melo 		IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
5211da177e4SLinus Torvalds 		return 1;
5221da177e4SLinus Torvalds 	}
5231da177e4SLinus Torvalds 
5246b102865SAmerigo Wang 	evicted = inet_frag_evictor(&net->ipv6.frags, &ip6_frags, false);
5256b102865SAmerigo Wang 	if (evicted)
5266b102865SAmerigo Wang 		IP6_ADD_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
5276b102865SAmerigo Wang 				 IPSTATS_MIB_REASMFAILS, evicted);
5281da177e4SLinus Torvalds 
5299546377cSShan Wei 	fq = fq_find(net, fhdr->identification, &hdr->saddr, &hdr->daddr);
5309546377cSShan Wei 	if (fq != NULL) {
531f61944efSHerbert Xu 		int ret;
5321da177e4SLinus Torvalds 
5335ab11c98SPavel Emelyanov 		spin_lock(&fq->q.lock);
5341da177e4SLinus Torvalds 
535f61944efSHerbert Xu 		ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
5361da177e4SLinus Torvalds 
5375ab11c98SPavel Emelyanov 		spin_unlock(&fq->q.lock);
538b836c99fSAmerigo Wang 		inet_frag_put(&fq->q, &ip6_frags);
5391da177e4SLinus Torvalds 		return ret;
5401da177e4SLinus Torvalds 	}
5411da177e4SLinus Torvalds 
542adf30907SEric Dumazet 	IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS);
5431da177e4SLinus Torvalds 	kfree_skb(skb);
5441da177e4SLinus Torvalds 	return -1;
54598b3377cSDenis V. Lunev 
54698b3377cSDenis V. Lunev fail_hdr:
547adf30907SEric Dumazet 	IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS);
54898b3377cSDenis V. Lunev 	icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
54998b3377cSDenis V. Lunev 	return -1;
5501da177e4SLinus Torvalds }
5511da177e4SLinus Torvalds 
55241135cc8SAlexey Dobriyan static const struct inet6_protocol frag_protocol =
5531da177e4SLinus Torvalds {
5541da177e4SLinus Torvalds 	.handler	=	ipv6_frag_rcv,
5551da177e4SLinus Torvalds 	.flags		=	INET6_PROTO_NOPOLICY,
5561da177e4SLinus Torvalds };
5571da177e4SLinus Torvalds 
5588d8354d2SPavel Emelyanov #ifdef CONFIG_SYSCTL
5590a64b4b8SPavel Emelyanov static struct ctl_table ip6_frags_ns_ctl_table[] = {
560e71e0349SDaniel Lezcano 	{
5618d8354d2SPavel Emelyanov 		.procname	= "ip6frag_high_thresh",
562e31e0bdcSPavel Emelyanov 		.data		= &init_net.ipv6.frags.high_thresh,
5638d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
5648d8354d2SPavel Emelyanov 		.mode		= 0644,
5656d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec
5668d8354d2SPavel Emelyanov 	},
5678d8354d2SPavel Emelyanov 	{
5688d8354d2SPavel Emelyanov 		.procname	= "ip6frag_low_thresh",
569e31e0bdcSPavel Emelyanov 		.data		= &init_net.ipv6.frags.low_thresh,
5708d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
5718d8354d2SPavel Emelyanov 		.mode		= 0644,
5726d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec
5738d8354d2SPavel Emelyanov 	},
5748d8354d2SPavel Emelyanov 	{
5758d8354d2SPavel Emelyanov 		.procname	= "ip6frag_time",
576b2fd5321SPavel Emelyanov 		.data		= &init_net.ipv6.frags.timeout,
5778d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
5788d8354d2SPavel Emelyanov 		.mode		= 0644,
5796d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
5808d8354d2SPavel Emelyanov 	},
5817d291ebbSPavel Emelyanov 	{ }
5827d291ebbSPavel Emelyanov };
5837d291ebbSPavel Emelyanov 
5847d291ebbSPavel Emelyanov static struct ctl_table ip6_frags_ctl_table[] = {
5858d8354d2SPavel Emelyanov 	{
5868d8354d2SPavel Emelyanov 		.procname	= "ip6frag_secret_interval",
5873b4bc4a2SPavel Emelyanov 		.data		= &ip6_frags.secret_interval,
5888d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
5898d8354d2SPavel Emelyanov 		.mode		= 0644,
5906d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
5918d8354d2SPavel Emelyanov 	},
5928d8354d2SPavel Emelyanov 	{ }
5938d8354d2SPavel Emelyanov };
5947d460db9SDaniel Lezcano 
5952c8c1e72SAlexey Dobriyan static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
5968d8354d2SPavel Emelyanov {
597e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
5988d8354d2SPavel Emelyanov 	struct ctl_table_header *hdr;
5998d8354d2SPavel Emelyanov 
6000a64b4b8SPavel Emelyanov 	table = ip6_frags_ns_ctl_table;
60109ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net)) {
6020a64b4b8SPavel Emelyanov 		table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
603e4a2d5c2SPavel Emelyanov 		if (table == NULL)
604e4a2d5c2SPavel Emelyanov 			goto err_alloc;
605e4a2d5c2SPavel Emelyanov 
606e31e0bdcSPavel Emelyanov 		table[0].data = &net->ipv6.frags.high_thresh;
607e31e0bdcSPavel Emelyanov 		table[1].data = &net->ipv6.frags.low_thresh;
608b2fd5321SPavel Emelyanov 		table[2].data = &net->ipv6.frags.timeout;
609464dc801SEric W. Biederman 
610464dc801SEric W. Biederman 		/* Don't export sysctls to unprivileged users */
611464dc801SEric W. Biederman 		if (net->user_ns != &init_user_ns)
612464dc801SEric W. Biederman 			table[0].procname = NULL;
613e4a2d5c2SPavel Emelyanov 	}
614e4a2d5c2SPavel Emelyanov 
615ec8f23ceSEric W. Biederman 	hdr = register_net_sysctl(net, "net/ipv6", table);
616e4a2d5c2SPavel Emelyanov 	if (hdr == NULL)
617e4a2d5c2SPavel Emelyanov 		goto err_reg;
618e4a2d5c2SPavel Emelyanov 
619e4a2d5c2SPavel Emelyanov 	net->ipv6.sysctl.frags_hdr = hdr;
620e4a2d5c2SPavel Emelyanov 	return 0;
621e4a2d5c2SPavel Emelyanov 
622e4a2d5c2SPavel Emelyanov err_reg:
62309ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net))
624e4a2d5c2SPavel Emelyanov 		kfree(table);
625e4a2d5c2SPavel Emelyanov err_alloc:
626e4a2d5c2SPavel Emelyanov 	return -ENOMEM;
627e4a2d5c2SPavel Emelyanov }
628e4a2d5c2SPavel Emelyanov 
6292c8c1e72SAlexey Dobriyan static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
630e4a2d5c2SPavel Emelyanov {
631e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
632e4a2d5c2SPavel Emelyanov 
633e4a2d5c2SPavel Emelyanov 	table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
634e4a2d5c2SPavel Emelyanov 	unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
6353705e11aSYang Hongyang 	if (!net_eq(net, &init_net))
636e4a2d5c2SPavel Emelyanov 		kfree(table);
6378d8354d2SPavel Emelyanov }
6387d291ebbSPavel Emelyanov 
6397d291ebbSPavel Emelyanov static struct ctl_table_header *ip6_ctl_header;
6407d291ebbSPavel Emelyanov 
6417d291ebbSPavel Emelyanov static int ip6_frags_sysctl_register(void)
6427d291ebbSPavel Emelyanov {
64343444757SEric W. Biederman 	ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6",
6447d291ebbSPavel Emelyanov 			ip6_frags_ctl_table);
6457d291ebbSPavel Emelyanov 	return ip6_ctl_header == NULL ? -ENOMEM : 0;
6467d291ebbSPavel Emelyanov }
6477d291ebbSPavel Emelyanov 
6487d291ebbSPavel Emelyanov static void ip6_frags_sysctl_unregister(void)
6497d291ebbSPavel Emelyanov {
6507d291ebbSPavel Emelyanov 	unregister_net_sysctl_table(ip6_ctl_header);
6517d291ebbSPavel Emelyanov }
6528d8354d2SPavel Emelyanov #else
6530a64b4b8SPavel Emelyanov static inline int ip6_frags_ns_sysctl_register(struct net *net)
6548d8354d2SPavel Emelyanov {
6558d8354d2SPavel Emelyanov 	return 0;
6568d8354d2SPavel Emelyanov }
657e4a2d5c2SPavel Emelyanov 
6580a64b4b8SPavel Emelyanov static inline void ip6_frags_ns_sysctl_unregister(struct net *net)
659e4a2d5c2SPavel Emelyanov {
660e4a2d5c2SPavel Emelyanov }
6617d291ebbSPavel Emelyanov 
6627d291ebbSPavel Emelyanov static inline int ip6_frags_sysctl_register(void)
6637d291ebbSPavel Emelyanov {
6647d291ebbSPavel Emelyanov 	return 0;
6657d291ebbSPavel Emelyanov }
6667d291ebbSPavel Emelyanov 
6677d291ebbSPavel Emelyanov static inline void ip6_frags_sysctl_unregister(void)
6687d291ebbSPavel Emelyanov {
6697d291ebbSPavel Emelyanov }
6708d8354d2SPavel Emelyanov #endif
6718d8354d2SPavel Emelyanov 
6722c8c1e72SAlexey Dobriyan static int __net_init ipv6_frags_init_net(struct net *net)
6738d8354d2SPavel Emelyanov {
6747c070aa9SShan Wei 	net->ipv6.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
6757c070aa9SShan Wei 	net->ipv6.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
676b2fd5321SPavel Emelyanov 	net->ipv6.frags.timeout = IPV6_FRAG_TIMEOUT;
6778d8354d2SPavel Emelyanov 
678e5a2bb84SPavel Emelyanov 	inet_frags_init_net(&net->ipv6.frags);
679e5a2bb84SPavel Emelyanov 
6800a64b4b8SPavel Emelyanov 	return ip6_frags_ns_sysctl_register(net);
681e71e0349SDaniel Lezcano }
682e71e0349SDaniel Lezcano 
6832c8c1e72SAlexey Dobriyan static void __net_exit ipv6_frags_exit_net(struct net *net)
68481566e83SPavel Emelyanov {
6850a64b4b8SPavel Emelyanov 	ip6_frags_ns_sysctl_unregister(net);
68681566e83SPavel Emelyanov 	inet_frags_exit_net(&net->ipv6.frags, &ip6_frags);
68781566e83SPavel Emelyanov }
68881566e83SPavel Emelyanov 
68981566e83SPavel Emelyanov static struct pernet_operations ip6_frags_ops = {
69081566e83SPavel Emelyanov 	.init = ipv6_frags_init_net,
69181566e83SPavel Emelyanov 	.exit = ipv6_frags_exit_net,
69281566e83SPavel Emelyanov };
69381566e83SPavel Emelyanov 
694853cbbaaSDaniel Lezcano int __init ipv6_frag_init(void)
6951da177e4SLinus Torvalds {
696853cbbaaSDaniel Lezcano 	int ret;
6971da177e4SLinus Torvalds 
698853cbbaaSDaniel Lezcano 	ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
699853cbbaaSDaniel Lezcano 	if (ret)
700853cbbaaSDaniel Lezcano 		goto out;
701e71e0349SDaniel Lezcano 
7027d291ebbSPavel Emelyanov 	ret = ip6_frags_sysctl_register();
7037d291ebbSPavel Emelyanov 	if (ret)
7047d291ebbSPavel Emelyanov 		goto err_sysctl;
7057d291ebbSPavel Emelyanov 
7060002c630SPavel Emelyanov 	ret = register_pernet_subsys(&ip6_frags_ops);
7070002c630SPavel Emelyanov 	if (ret)
7080002c630SPavel Emelyanov 		goto err_pernet;
7098d8354d2SPavel Emelyanov 
710321a3a99SPavel Emelyanov 	ip6_frags.hashfn = ip6_hashfn;
711c6fda282SPavel Emelyanov 	ip6_frags.constructor = ip6_frag_init;
712c9547709SPavel Emelyanov 	ip6_frags.destructor = NULL;
7131e4b8287SPavel Emelyanov 	ip6_frags.skb_free = NULL;
7141e4b8287SPavel Emelyanov 	ip6_frags.qsize = sizeof(struct frag_queue);
715abd6523dSPavel Emelyanov 	ip6_frags.match = ip6_frag_match;
716e521db9dSPavel Emelyanov 	ip6_frags.frag_expire = ip6_frag_expire;
7173b4bc4a2SPavel Emelyanov 	ip6_frags.secret_interval = 10 * 60 * HZ;
7187eb95156SPavel Emelyanov 	inet_frags_init(&ip6_frags);
719853cbbaaSDaniel Lezcano out:
720853cbbaaSDaniel Lezcano 	return ret;
7210002c630SPavel Emelyanov 
7220002c630SPavel Emelyanov err_pernet:
7237d291ebbSPavel Emelyanov 	ip6_frags_sysctl_unregister();
7247d291ebbSPavel Emelyanov err_sysctl:
7250002c630SPavel Emelyanov 	inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
7260002c630SPavel Emelyanov 	goto out;
727853cbbaaSDaniel Lezcano }
728853cbbaaSDaniel Lezcano 
729853cbbaaSDaniel Lezcano void ipv6_frag_exit(void)
730853cbbaaSDaniel Lezcano {
731853cbbaaSDaniel Lezcano 	inet_frags_fini(&ip6_frags);
7327d291ebbSPavel Emelyanov 	ip6_frags_sysctl_unregister();
73381566e83SPavel Emelyanov 	unregister_pernet_subsys(&ip6_frags_ops);
734853cbbaaSDaniel Lezcano 	inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
7351da177e4SLinus Torvalds }
736