xref: /openbmc/linux/net/ipv6/reassembly.c (revision 43444757)
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  */
291da177e4SLinus Torvalds #include <linux/errno.h>
301da177e4SLinus Torvalds #include <linux/types.h>
311da177e4SLinus Torvalds #include <linux/string.h>
321da177e4SLinus Torvalds #include <linux/socket.h>
331da177e4SLinus Torvalds #include <linux/sockios.h>
341da177e4SLinus Torvalds #include <linux/jiffies.h>
351da177e4SLinus Torvalds #include <linux/net.h>
361da177e4SLinus Torvalds #include <linux/list.h>
371da177e4SLinus Torvalds #include <linux/netdevice.h>
381da177e4SLinus Torvalds #include <linux/in6.h>
391da177e4SLinus Torvalds #include <linux/ipv6.h>
401da177e4SLinus Torvalds #include <linux/icmpv6.h>
411da177e4SLinus Torvalds #include <linux/random.h>
421da177e4SLinus Torvalds #include <linux/jhash.h>
43f61944efSHerbert Xu #include <linux/skbuff.h>
445a0e3ad6STejun Heo #include <linux/slab.h>
45bc3b2d7fSPaul Gortmaker #include <linux/export.h>
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #include <net/sock.h>
481da177e4SLinus Torvalds #include <net/snmp.h>
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #include <net/ipv6.h>
51a11d206dSYOSHIFUJI Hideaki #include <net/ip6_route.h>
521da177e4SLinus Torvalds #include <net/protocol.h>
531da177e4SLinus Torvalds #include <net/transp_v6.h>
541da177e4SLinus Torvalds #include <net/rawv6.h>
551da177e4SLinus Torvalds #include <net/ndisc.h>
561da177e4SLinus Torvalds #include <net/addrconf.h>
575ab11c98SPavel Emelyanov #include <net/inet_frag.h>
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds struct ip6frag_skb_cb
601da177e4SLinus Torvalds {
611da177e4SLinus Torvalds 	struct inet6_skb_parm	h;
621da177e4SLinus Torvalds 	int			offset;
631da177e4SLinus Torvalds };
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds #define FRAG6_CB(skb)	((struct ip6frag_skb_cb*)((skb)->cb))
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /*
691da177e4SLinus Torvalds  *	Equivalent of ipv4 struct ipq
701da177e4SLinus Torvalds  */
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds struct frag_queue
731da177e4SLinus Torvalds {
745ab11c98SPavel Emelyanov 	struct inet_frag_queue	q;
751da177e4SLinus Torvalds 
76e69a4adcSAl Viro 	__be32			id;		/* fragment id		*/
770b5ccb2eSPatrick McHardy 	u32			user;
781da177e4SLinus Torvalds 	struct in6_addr		saddr;
791da177e4SLinus Torvalds 	struct in6_addr		daddr;
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds 	int			iif;
821da177e4SLinus Torvalds 	unsigned int		csum;
831da177e4SLinus Torvalds 	__u16			nhoffset;
841da177e4SLinus Torvalds };
851da177e4SLinus Torvalds 
867eb95156SPavel Emelyanov static struct inet_frags ip6_frags;
871da177e4SLinus Torvalds 
88e5a2bb84SPavel Emelyanov int ip6_frag_nqueues(struct net *net)
897eb95156SPavel Emelyanov {
90e5a2bb84SPavel Emelyanov 	return net->ipv6.frags.nqueues;
917eb95156SPavel Emelyanov }
921da177e4SLinus Torvalds 
936ddc0822SPavel Emelyanov int ip6_frag_mem(struct net *net)
947eb95156SPavel Emelyanov {
956ddc0822SPavel Emelyanov 	return atomic_read(&net->ipv6.frags.mem);
967eb95156SPavel Emelyanov }
971da177e4SLinus Torvalds 
98f61944efSHerbert Xu static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
99f61944efSHerbert Xu 			  struct net_device *dev);
100f61944efSHerbert Xu 
101f6596f9dSZach Brown /*
102f6596f9dSZach Brown  * callers should be careful not to use the hash value outside the ipfrag_lock
103f6596f9dSZach Brown  * as doing so could race with ipfrag_hash_rnd being recalculated.
104f6596f9dSZach Brown  */
10593c8b90fSIlpo Järvinen unsigned int inet6_hash_frag(__be32 id, const struct in6_addr *saddr,
10693c8b90fSIlpo Järvinen 			     const struct in6_addr *daddr, u32 rnd)
1071da177e4SLinus Torvalds {
10882a39eb6SJozsef Kadlecsik 	u32 c;
1091da177e4SLinus Torvalds 
11082a39eb6SJozsef Kadlecsik 	c = jhash_3words((__force u32)saddr->s6_addr32[0],
11182a39eb6SJozsef Kadlecsik 			 (__force u32)saddr->s6_addr32[1],
11282a39eb6SJozsef Kadlecsik 			 (__force u32)saddr->s6_addr32[2],
11382a39eb6SJozsef Kadlecsik 			 rnd);
1141da177e4SLinus Torvalds 
11582a39eb6SJozsef Kadlecsik 	c = jhash_3words((__force u32)saddr->s6_addr32[3],
11682a39eb6SJozsef Kadlecsik 			 (__force u32)daddr->s6_addr32[0],
11782a39eb6SJozsef Kadlecsik 			 (__force u32)daddr->s6_addr32[1],
11882a39eb6SJozsef Kadlecsik 			 c);
1191da177e4SLinus Torvalds 
12082a39eb6SJozsef Kadlecsik 	c =  jhash_3words((__force u32)daddr->s6_addr32[2],
12182a39eb6SJozsef Kadlecsik 			  (__force u32)daddr->s6_addr32[3],
12282a39eb6SJozsef Kadlecsik 			  (__force u32)id,
12382a39eb6SJozsef Kadlecsik 			  c);
1241da177e4SLinus Torvalds 
1257eb95156SPavel Emelyanov 	return c & (INETFRAGS_HASHSZ - 1);
1261da177e4SLinus Torvalds }
12793c8b90fSIlpo Järvinen EXPORT_SYMBOL_GPL(inet6_hash_frag);
1281da177e4SLinus Torvalds 
129321a3a99SPavel Emelyanov static unsigned int ip6_hashfn(struct inet_frag_queue *q)
1301da177e4SLinus Torvalds {
131321a3a99SPavel Emelyanov 	struct frag_queue *fq;
1321da177e4SLinus Torvalds 
133321a3a99SPavel Emelyanov 	fq = container_of(q, struct frag_queue, q);
13493c8b90fSIlpo Järvinen 	return inet6_hash_frag(fq->id, &fq->saddr, &fq->daddr, ip6_frags.rnd);
1351da177e4SLinus Torvalds }
1361da177e4SLinus Torvalds 
137abd6523dSPavel Emelyanov int ip6_frag_match(struct inet_frag_queue *q, void *a)
138abd6523dSPavel Emelyanov {
139abd6523dSPavel Emelyanov 	struct frag_queue *fq;
140abd6523dSPavel Emelyanov 	struct ip6_create_arg *arg = a;
141abd6523dSPavel Emelyanov 
142abd6523dSPavel Emelyanov 	fq = container_of(q, struct frag_queue, q);
1430b5ccb2eSPatrick McHardy 	return (fq->id == arg->id && fq->user == arg->user &&
144abd6523dSPavel Emelyanov 			ipv6_addr_equal(&fq->saddr, arg->src) &&
145abd6523dSPavel Emelyanov 			ipv6_addr_equal(&fq->daddr, arg->dst));
146abd6523dSPavel Emelyanov }
147abd6523dSPavel Emelyanov EXPORT_SYMBOL(ip6_frag_match);
148abd6523dSPavel Emelyanov 
149c6fda282SPavel Emelyanov void ip6_frag_init(struct inet_frag_queue *q, void *a)
150c6fda282SPavel Emelyanov {
151c6fda282SPavel Emelyanov 	struct frag_queue *fq = container_of(q, struct frag_queue, q);
152c6fda282SPavel Emelyanov 	struct ip6_create_arg *arg = a;
153c6fda282SPavel Emelyanov 
154c6fda282SPavel Emelyanov 	fq->id = arg->id;
1550b5ccb2eSPatrick McHardy 	fq->user = arg->user;
1564e3fd7a0SAlexey Dobriyan 	fq->saddr = *arg->src;
1574e3fd7a0SAlexey Dobriyan 	fq->daddr = *arg->dst;
158c6fda282SPavel Emelyanov }
159c6fda282SPavel Emelyanov EXPORT_SYMBOL(ip6_frag_init);
160c6fda282SPavel Emelyanov 
1611da177e4SLinus Torvalds /* Destruction primitives. */
1621da177e4SLinus Torvalds 
1634b6cb5d8SPavel Emelyanov static __inline__ void fq_put(struct frag_queue *fq)
1641da177e4SLinus Torvalds {
165762cc408SPavel Emelyanov 	inet_frag_put(&fq->q, &ip6_frags);
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds /* Kill fq entry. It is not destroyed immediately,
1691da177e4SLinus Torvalds  * because caller (and someone more) holds reference count.
1701da177e4SLinus Torvalds  */
1711da177e4SLinus Torvalds static __inline__ void fq_kill(struct frag_queue *fq)
1721da177e4SLinus Torvalds {
173277e650dSPavel Emelyanov 	inet_frag_kill(&fq->q, &ip6_frags);
1741da177e4SLinus Torvalds }
1751da177e4SLinus Torvalds 
1766ddc0822SPavel Emelyanov static void ip6_evictor(struct net *net, struct inet6_dev *idev)
1771da177e4SLinus Torvalds {
1788e7999c4SPavel Emelyanov 	int evicted;
1791da177e4SLinus Torvalds 
1806ddc0822SPavel Emelyanov 	evicted = inet_frag_evictor(&net->ipv6.frags, &ip6_frags);
1818e7999c4SPavel Emelyanov 	if (evicted)
182821d5777SDenis V. Lunev 		IP6_ADD_STATS_BH(net, idev, IPSTATS_MIB_REASMFAILS, evicted);
1831da177e4SLinus Torvalds }
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds static void ip6_frag_expire(unsigned long data)
1861da177e4SLinus Torvalds {
187e521db9dSPavel Emelyanov 	struct frag_queue *fq;
188a11d206dSYOSHIFUJI Hideaki 	struct net_device *dev = NULL;
1894ac2ccd0SDaniel Lezcano 	struct net *net;
1901da177e4SLinus Torvalds 
191e521db9dSPavel Emelyanov 	fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q);
192e521db9dSPavel Emelyanov 
1935ab11c98SPavel Emelyanov 	spin_lock(&fq->q.lock);
1941da177e4SLinus Torvalds 
195bc578a54SJoe Perches 	if (fq->q.last_in & INET_FRAG_COMPLETE)
1961da177e4SLinus Torvalds 		goto out;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	fq_kill(fq);
1991da177e4SLinus Torvalds 
2004ac2ccd0SDaniel Lezcano 	net = container_of(fq->q.net, struct net, ipv6.frags);
201a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
20269df9d59SEric Dumazet 	dev = dev_get_by_index_rcu(net, fq->iif);
20369df9d59SEric Dumazet 	if (!dev)
20469df9d59SEric Dumazet 		goto out_rcu_unlock;
20569df9d59SEric Dumazet 
206483a47d2SDenis V. Lunev 	IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
207483a47d2SDenis V. Lunev 	IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
2081da177e4SLinus Torvalds 
20978c784c4SIngo Oeser 	/* Don't send error if the first segment did not arrive. */
210bc578a54SJoe Perches 	if (!(fq->q.last_in & INET_FRAG_FIRST_IN) || !fq->q.fragments)
21169df9d59SEric Dumazet 		goto out_rcu_unlock;
21278c784c4SIngo Oeser 
2131da177e4SLinus Torvalds 	/*
2141da177e4SLinus Torvalds 	   But use as source device on which LAST ARRIVED
2151da177e4SLinus Torvalds 	   segment was received. And do not use fq->dev
2161da177e4SLinus Torvalds 	   pointer directly, device might already disappeared.
2171da177e4SLinus Torvalds 	 */
2185ab11c98SPavel Emelyanov 	fq->q.fragments->dev = dev;
2193ffe533cSAlexey Dobriyan 	icmpv6_send(fq->q.fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0);
22069df9d59SEric Dumazet out_rcu_unlock:
22169df9d59SEric Dumazet 	rcu_read_unlock();
2221da177e4SLinus Torvalds out:
2235ab11c98SPavel Emelyanov 	spin_unlock(&fq->q.lock);
2244b6cb5d8SPavel Emelyanov 	fq_put(fq);
2251da177e4SLinus Torvalds }
2261da177e4SLinus Torvalds 
227abd6523dSPavel Emelyanov static __inline__ struct frag_queue *
228b71d1d42SEric Dumazet fq_find(struct net *net, __be32 id, const struct in6_addr *src, const struct in6_addr *dst)
2291da177e4SLinus Torvalds {
230c6fda282SPavel Emelyanov 	struct inet_frag_queue *q;
231c6fda282SPavel Emelyanov 	struct ip6_create_arg arg;
232abd6523dSPavel Emelyanov 	unsigned int hash;
2331da177e4SLinus Torvalds 
234c6fda282SPavel Emelyanov 	arg.id = id;
2350b5ccb2eSPatrick McHardy 	arg.user = IP6_DEFRAG_LOCAL_DELIVER;
236c6fda282SPavel Emelyanov 	arg.src = src;
237c6fda282SPavel Emelyanov 	arg.dst = dst;
2389a375803SPavel Emelyanov 
2399a375803SPavel Emelyanov 	read_lock(&ip6_frags.lock);
24093c8b90fSIlpo Järvinen 	hash = inet6_hash_frag(id, src, dst, ip6_frags.rnd);
241c6fda282SPavel Emelyanov 
242ac18e750SPavel Emelyanov 	q = inet_frag_find(&net->ipv6.frags, &ip6_frags, &arg, hash);
243c6fda282SPavel Emelyanov 	if (q == NULL)
2449546377cSShan Wei 		return NULL;
2451da177e4SLinus Torvalds 
246c6fda282SPavel Emelyanov 	return container_of(q, struct frag_queue, q);
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
249f61944efSHerbert Xu static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
2501da177e4SLinus Torvalds 			   struct frag_hdr *fhdr, int nhoff)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds 	struct sk_buff *prev, *next;
253f61944efSHerbert Xu 	struct net_device *dev;
2541da177e4SLinus Torvalds 	int offset, end;
255adf30907SEric Dumazet 	struct net *net = dev_net(skb_dst(skb)->dev);
2561da177e4SLinus Torvalds 
257bc578a54SJoe Perches 	if (fq->q.last_in & INET_FRAG_COMPLETE)
2581da177e4SLinus Torvalds 		goto err;
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	offset = ntohs(fhdr->frag_off) & ~0x7;
2610660e03fSArnaldo Carvalho de Melo 	end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
2620660e03fSArnaldo Carvalho de Melo 			((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	if ((unsigned int)end > IPV6_MAXPLEN) {
265adf30907SEric Dumazet 		IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
266a11d206dSYOSHIFUJI Hideaki 				 IPSTATS_MIB_INHDRERRORS);
267d56f90a7SArnaldo Carvalho de Melo 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
268d56f90a7SArnaldo Carvalho de Melo 				  ((u8 *)&fhdr->frag_off -
269d56f90a7SArnaldo Carvalho de Melo 				   skb_network_header(skb)));
270f61944efSHerbert Xu 		return -1;
2711da177e4SLinus Torvalds 	}
2721da177e4SLinus Torvalds 
273d56f90a7SArnaldo Carvalho de Melo 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
274d56f90a7SArnaldo Carvalho de Melo 		const unsigned char *nh = skb_network_header(skb);
2751da177e4SLinus Torvalds 		skb->csum = csum_sub(skb->csum,
276d56f90a7SArnaldo Carvalho de Melo 				     csum_partial(nh, (u8 *)(fhdr + 1) - nh,
277d56f90a7SArnaldo Carvalho de Melo 						  0));
278d56f90a7SArnaldo Carvalho de Melo 	}
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	/* Is this the final fragment? */
2811da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(IP6_MF))) {
2821da177e4SLinus Torvalds 		/* If we already have some bits beyond end
2831da177e4SLinus Torvalds 		 * or have different end, the segment is corrupted.
2841da177e4SLinus Torvalds 		 */
2855ab11c98SPavel Emelyanov 		if (end < fq->q.len ||
286bc578a54SJoe Perches 		    ((fq->q.last_in & INET_FRAG_LAST_IN) && end != fq->q.len))
2871da177e4SLinus Torvalds 			goto err;
288bc578a54SJoe Perches 		fq->q.last_in |= INET_FRAG_LAST_IN;
2895ab11c98SPavel Emelyanov 		fq->q.len = end;
2901da177e4SLinus Torvalds 	} else {
2911da177e4SLinus Torvalds 		/* Check if the fragment is rounded to 8 bytes.
2921da177e4SLinus Torvalds 		 * Required by the RFC.
2931da177e4SLinus Torvalds 		 */
2941da177e4SLinus Torvalds 		if (end & 0x7) {
2951da177e4SLinus Torvalds 			/* RFC2460 says always send parameter problem in
2961da177e4SLinus Torvalds 			 * this case. -DaveM
2971da177e4SLinus Torvalds 			 */
298adf30907SEric Dumazet 			IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
299a11d206dSYOSHIFUJI Hideaki 					 IPSTATS_MIB_INHDRERRORS);
3001da177e4SLinus Torvalds 			icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
3011da177e4SLinus Torvalds 					  offsetof(struct ipv6hdr, payload_len));
302f61944efSHerbert Xu 			return -1;
3031da177e4SLinus Torvalds 		}
3045ab11c98SPavel Emelyanov 		if (end > fq->q.len) {
3051da177e4SLinus Torvalds 			/* Some bits beyond end -> corruption. */
306bc578a54SJoe Perches 			if (fq->q.last_in & INET_FRAG_LAST_IN)
3071da177e4SLinus Torvalds 				goto err;
3085ab11c98SPavel Emelyanov 			fq->q.len = end;
3091da177e4SLinus Torvalds 		}
3101da177e4SLinus Torvalds 	}
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	if (end == offset)
3131da177e4SLinus Torvalds 		goto err;
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	/* Point into the IP datagram 'data' part. */
3161da177e4SLinus Torvalds 	if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
3171da177e4SLinus Torvalds 		goto err;
31842ca89c1SStephen Hemminger 
31942ca89c1SStephen Hemminger 	if (pskb_trim_rcsum(skb, end - offset))
3201da177e4SLinus Torvalds 		goto err;
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	/* Find out which fragments are in front and at the back of us
3231da177e4SLinus Torvalds 	 * in the chain of fragments so far.  We must know where to put
3241da177e4SLinus Torvalds 	 * this fragment, right?
3251da177e4SLinus Torvalds 	 */
326d6bebca9SChangli Gao 	prev = fq->q.fragments_tail;
327d6bebca9SChangli Gao 	if (!prev || FRAG6_CB(prev)->offset < offset) {
328d6bebca9SChangli Gao 		next = NULL;
329d6bebca9SChangli Gao 		goto found;
330d6bebca9SChangli Gao 	}
3311da177e4SLinus Torvalds 	prev = NULL;
3325ab11c98SPavel Emelyanov 	for(next = fq->q.fragments; next != NULL; next = next->next) {
3331da177e4SLinus Torvalds 		if (FRAG6_CB(next)->offset >= offset)
3341da177e4SLinus Torvalds 			break;	/* bingo! */
3351da177e4SLinus Torvalds 		prev = next;
3361da177e4SLinus Torvalds 	}
3371da177e4SLinus Torvalds 
338d6bebca9SChangli Gao found:
3395de658f8SEric Dumazet 	/* RFC5722, Section 4, amended by Errata ID : 3089
34070789d70SNicolas Dichtel 	 *                          When reassembling an IPv6 datagram, if
34170789d70SNicolas Dichtel 	 *   one or more its constituent fragments is determined to be an
34270789d70SNicolas Dichtel 	 *   overlapping fragment, the entire datagram (and any constituent
3435de658f8SEric Dumazet 	 *   fragments) MUST be silently discarded.
3441da177e4SLinus Torvalds 	 */
3451da177e4SLinus Torvalds 
34670789d70SNicolas Dichtel 	/* Check for overlap with preceding fragment. */
34770789d70SNicolas Dichtel 	if (prev &&
348f4642141SShan Wei 	    (FRAG6_CB(prev)->offset + prev->len) > offset)
34970789d70SNicolas Dichtel 		goto discard_fq;
3501da177e4SLinus Torvalds 
35170789d70SNicolas Dichtel 	/* Look for overlap with succeeding segment. */
35270789d70SNicolas Dichtel 	if (next && FRAG6_CB(next)->offset < end)
35370789d70SNicolas Dichtel 		goto discard_fq;
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	FRAG6_CB(skb)->offset = offset;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	/* Insert this fragment in the chain of fragments. */
3581da177e4SLinus Torvalds 	skb->next = next;
359d6bebca9SChangli Gao 	if (!next)
360d6bebca9SChangli Gao 		fq->q.fragments_tail = skb;
3611da177e4SLinus Torvalds 	if (prev)
3621da177e4SLinus Torvalds 		prev->next = skb;
3631da177e4SLinus Torvalds 	else
3645ab11c98SPavel Emelyanov 		fq->q.fragments = skb;
3651da177e4SLinus Torvalds 
366f61944efSHerbert Xu 	dev = skb->dev;
367f61944efSHerbert Xu 	if (dev) {
368f61944efSHerbert Xu 		fq->iif = dev->ifindex;
3691da177e4SLinus Torvalds 		skb->dev = NULL;
370f61944efSHerbert Xu 	}
3715ab11c98SPavel Emelyanov 	fq->q.stamp = skb->tstamp;
3725ab11c98SPavel Emelyanov 	fq->q.meat += skb->len;
3736ddc0822SPavel Emelyanov 	atomic_add(skb->truesize, &fq->q.net->mem);
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	/* The first fragment.
3761da177e4SLinus Torvalds 	 * nhoffset is obtained from the first fragment, of course.
3771da177e4SLinus Torvalds 	 */
3781da177e4SLinus Torvalds 	if (offset == 0) {
3791da177e4SLinus Torvalds 		fq->nhoffset = nhoff;
380bc578a54SJoe Perches 		fq->q.last_in |= INET_FRAG_FIRST_IN;
3811da177e4SLinus Torvalds 	}
382f61944efSHerbert Xu 
383bc578a54SJoe Perches 	if (fq->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
384bc578a54SJoe Perches 	    fq->q.meat == fq->q.len)
385f61944efSHerbert Xu 		return ip6_frag_reasm(fq, prev, dev);
386f61944efSHerbert Xu 
3877eb95156SPavel Emelyanov 	write_lock(&ip6_frags.lock);
3883140c25cSPavel Emelyanov 	list_move_tail(&fq->q.lru_list, &fq->q.net->lru_list);
3897eb95156SPavel Emelyanov 	write_unlock(&ip6_frags.lock);
390f61944efSHerbert Xu 	return -1;
3911da177e4SLinus Torvalds 
39270789d70SNicolas Dichtel discard_fq:
39370789d70SNicolas Dichtel 	fq_kill(fq);
3941da177e4SLinus Torvalds err:
395adf30907SEric Dumazet 	IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
3963bd653c8SDenis V. Lunev 		      IPSTATS_MIB_REASMFAILS);
3971da177e4SLinus Torvalds 	kfree_skb(skb);
398f61944efSHerbert Xu 	return -1;
3991da177e4SLinus Torvalds }
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds /*
4021da177e4SLinus Torvalds  *	Check if this packet is complete.
4031da177e4SLinus Torvalds  *	Returns NULL on failure by any reason, and pointer
4041da177e4SLinus Torvalds  *	to current nexthdr field in reassembled frame.
4051da177e4SLinus Torvalds  *
4061da177e4SLinus Torvalds  *	It is called with locked fq, and caller must check that
4071da177e4SLinus Torvalds  *	queue is eligible for reassembly i.e. it is not COMPLETE,
4081da177e4SLinus Torvalds  *	the last and the first frames arrived and all the bits are here.
4091da177e4SLinus Torvalds  */
410f61944efSHerbert Xu static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
4111da177e4SLinus Torvalds 			  struct net_device *dev)
4121da177e4SLinus Torvalds {
4132bad35b7SJorge Boncompte [DTI2] 	struct net *net = container_of(fq->q.net, struct net, ipv6.frags);
4145ab11c98SPavel Emelyanov 	struct sk_buff *fp, *head = fq->q.fragments;
4151da177e4SLinus Torvalds 	int    payload_len;
4161da177e4SLinus Torvalds 	unsigned int nhoff;
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	fq_kill(fq);
4191da177e4SLinus Torvalds 
420f61944efSHerbert Xu 	/* Make the one we just received the head. */
421f61944efSHerbert Xu 	if (prev) {
422f61944efSHerbert Xu 		head = prev->next;
423f61944efSHerbert Xu 		fp = skb_clone(head, GFP_ATOMIC);
424f61944efSHerbert Xu 
425f61944efSHerbert Xu 		if (!fp)
426f61944efSHerbert Xu 			goto out_oom;
427f61944efSHerbert Xu 
428f61944efSHerbert Xu 		fp->next = head->next;
429d6bebca9SChangli Gao 		if (!fp->next)
430d6bebca9SChangli Gao 			fq->q.fragments_tail = fp;
431f61944efSHerbert Xu 		prev->next = fp;
432f61944efSHerbert Xu 
4335ab11c98SPavel Emelyanov 		skb_morph(head, fq->q.fragments);
4345ab11c98SPavel Emelyanov 		head->next = fq->q.fragments->next;
435f61944efSHerbert Xu 
4365ab11c98SPavel Emelyanov 		kfree_skb(fq->q.fragments);
4375ab11c98SPavel Emelyanov 		fq->q.fragments = head;
438f61944efSHerbert Xu 	}
439f61944efSHerbert Xu 
440547b792cSIlpo Järvinen 	WARN_ON(head == NULL);
441547b792cSIlpo Järvinen 	WARN_ON(FRAG6_CB(head)->offset != 0);
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	/* Unfragmented part is taken from the first segment. */
444d56f90a7SArnaldo Carvalho de Melo 	payload_len = ((head->data - skb_network_header(head)) -
4455ab11c98SPavel Emelyanov 		       sizeof(struct ipv6hdr) + fq->q.len -
446d56f90a7SArnaldo Carvalho de Melo 		       sizeof(struct frag_hdr));
4471da177e4SLinus Torvalds 	if (payload_len > IPV6_MAXPLEN)
4481da177e4SLinus Torvalds 		goto out_oversize;
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	/* Head of list must not be cloned. */
4511da177e4SLinus Torvalds 	if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
4521da177e4SLinus Torvalds 		goto out_oom;
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	/* If the first fragment is fragmented itself, we split
4551da177e4SLinus Torvalds 	 * it to two chunks: the first with data and paged part
4561da177e4SLinus Torvalds 	 * and the second, holding only fragments. */
45721dc3301SDavid S. Miller 	if (skb_has_frag_list(head)) {
4581da177e4SLinus Torvalds 		struct sk_buff *clone;
4591da177e4SLinus Torvalds 		int i, plen = 0;
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 		if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
4621da177e4SLinus Torvalds 			goto out_oom;
4631da177e4SLinus Torvalds 		clone->next = head->next;
4641da177e4SLinus Torvalds 		head->next = clone;
4651da177e4SLinus Torvalds 		skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
4664d9092bbSDavid S. Miller 		skb_frag_list_init(head);
4671da177e4SLinus Torvalds 		for (i = 0; i < skb_shinfo(head)->nr_frags; i++)
4689e903e08SEric Dumazet 			plen += skb_frag_size(&skb_shinfo(head)->frags[i]);
4691da177e4SLinus Torvalds 		clone->len = clone->data_len = head->data_len - plen;
4701da177e4SLinus Torvalds 		head->data_len -= clone->len;
4711da177e4SLinus Torvalds 		head->len -= clone->len;
4721da177e4SLinus Torvalds 		clone->csum = 0;
4731da177e4SLinus Torvalds 		clone->ip_summed = head->ip_summed;
4746ddc0822SPavel Emelyanov 		atomic_add(clone->truesize, &fq->q.net->mem);
4751da177e4SLinus Torvalds 	}
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	/* We have to remove fragment header from datagram and to relocate
4781da177e4SLinus Torvalds 	 * header in order to calculate ICV correctly. */
4791da177e4SLinus Torvalds 	nhoff = fq->nhoffset;
480b0e380b1SArnaldo Carvalho de Melo 	skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
4811da177e4SLinus Torvalds 	memmove(head->head + sizeof(struct frag_hdr), head->head,
4821da177e4SLinus Torvalds 		(head->data - head->head) - sizeof(struct frag_hdr));
483b0e380b1SArnaldo Carvalho de Melo 	head->mac_header += sizeof(struct frag_hdr);
484b0e380b1SArnaldo Carvalho de Melo 	head->network_header += sizeof(struct frag_hdr);
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	skb_shinfo(head)->frag_list = head->next;
487badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(head);
488d56f90a7SArnaldo Carvalho de Melo 	skb_push(head, head->data - skb_network_header(head));
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 	for (fp=head->next; fp; fp = fp->next) {
4911da177e4SLinus Torvalds 		head->data_len += fp->len;
4921da177e4SLinus Torvalds 		head->len += fp->len;
4931da177e4SLinus Torvalds 		if (head->ip_summed != fp->ip_summed)
4941da177e4SLinus Torvalds 			head->ip_summed = CHECKSUM_NONE;
49584fa7933SPatrick McHardy 		else if (head->ip_summed == CHECKSUM_COMPLETE)
4961da177e4SLinus Torvalds 			head->csum = csum_add(head->csum, fp->csum);
4971da177e4SLinus Torvalds 		head->truesize += fp->truesize;
4981da177e4SLinus Torvalds 	}
499d27f9b35SEric Dumazet 	atomic_sub(head->truesize, &fq->q.net->mem);
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 	head->next = NULL;
5021da177e4SLinus Torvalds 	head->dev = dev;
5035ab11c98SPavel Emelyanov 	head->tstamp = fq->q.stamp;
5040660e03fSArnaldo Carvalho de Melo 	ipv6_hdr(head)->payload_len = htons(payload_len);
505951dbc8aSPatrick McHardy 	IP6CB(head)->nhoff = nhoff;
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	/* Yes, and fold redundant checksum back. 8) */
50884fa7933SPatrick McHardy 	if (head->ip_summed == CHECKSUM_COMPLETE)
509d56f90a7SArnaldo Carvalho de Melo 		head->csum = csum_partial(skb_network_header(head),
510cfe1fc77SArnaldo Carvalho de Melo 					  skb_network_header_len(head),
511d56f90a7SArnaldo Carvalho de Melo 					  head->csum);
5121da177e4SLinus Torvalds 
513a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
5142bad35b7SJorge Boncompte [DTI2] 	IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
515a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
5165ab11c98SPavel Emelyanov 	fq->q.fragments = NULL;
517d6bebca9SChangli Gao 	fq->q.fragments_tail = NULL;
5181da177e4SLinus Torvalds 	return 1;
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds out_oversize:
5211da177e4SLinus Torvalds 	if (net_ratelimit())
5221da177e4SLinus Torvalds 		printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len);
5231da177e4SLinus Torvalds 	goto out_fail;
5241da177e4SLinus Torvalds out_oom:
5251da177e4SLinus Torvalds 	if (net_ratelimit())
5261da177e4SLinus Torvalds 		printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n");
5271da177e4SLinus Torvalds out_fail:
528a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
5292bad35b7SJorge Boncompte [DTI2] 	IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
530a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
5311da177e4SLinus Torvalds 	return -1;
5321da177e4SLinus Torvalds }
5331da177e4SLinus Torvalds 
534e5bbef20SHerbert Xu static int ipv6_frag_rcv(struct sk_buff *skb)
5351da177e4SLinus Torvalds {
5361da177e4SLinus Torvalds 	struct frag_hdr *fhdr;
5371da177e4SLinus Torvalds 	struct frag_queue *fq;
538b71d1d42SEric Dumazet 	const struct ipv6hdr *hdr = ipv6_hdr(skb);
539adf30907SEric Dumazet 	struct net *net = dev_net(skb_dst(skb)->dev);
5401da177e4SLinus Torvalds 
541adf30907SEric Dumazet 	IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS);
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	/* Jumbo payload inhibits frag. header */
54498b3377cSDenis V. Lunev 	if (hdr->payload_len==0)
54598b3377cSDenis V. Lunev 		goto fail_hdr;
54698b3377cSDenis V. Lunev 
547ea2ae17dSArnaldo Carvalho de Melo 	if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
54898b3377cSDenis V. Lunev 				 sizeof(struct frag_hdr))))
54998b3377cSDenis V. Lunev 		goto fail_hdr;
5501da177e4SLinus Torvalds 
5510660e03fSArnaldo Carvalho de Melo 	hdr = ipv6_hdr(skb);
5529c70220bSArnaldo Carvalho de Melo 	fhdr = (struct frag_hdr *)skb_transport_header(skb);
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(0xFFF9))) {
5551da177e4SLinus Torvalds 		/* It is not a fragmented frame */
556b0e380b1SArnaldo Carvalho de Melo 		skb->transport_header += sizeof(struct frag_hdr);
557483a47d2SDenis V. Lunev 		IP6_INC_STATS_BH(net,
558adf30907SEric Dumazet 				 ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS);
5591da177e4SLinus Torvalds 
560d56f90a7SArnaldo Carvalho de Melo 		IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
5611da177e4SLinus Torvalds 		return 1;
5621da177e4SLinus Torvalds 	}
5631da177e4SLinus Torvalds 
564e31e0bdcSPavel Emelyanov 	if (atomic_read(&net->ipv6.frags.mem) > net->ipv6.frags.high_thresh)
565adf30907SEric Dumazet 		ip6_evictor(net, ip6_dst_idev(skb_dst(skb)));
5661da177e4SLinus Torvalds 
5679546377cSShan Wei 	fq = fq_find(net, fhdr->identification, &hdr->saddr, &hdr->daddr);
5689546377cSShan Wei 	if (fq != NULL) {
569f61944efSHerbert Xu 		int ret;
5701da177e4SLinus Torvalds 
5715ab11c98SPavel Emelyanov 		spin_lock(&fq->q.lock);
5721da177e4SLinus Torvalds 
573f61944efSHerbert Xu 		ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
5741da177e4SLinus Torvalds 
5755ab11c98SPavel Emelyanov 		spin_unlock(&fq->q.lock);
5764b6cb5d8SPavel Emelyanov 		fq_put(fq);
5771da177e4SLinus Torvalds 		return ret;
5781da177e4SLinus Torvalds 	}
5791da177e4SLinus Torvalds 
580adf30907SEric Dumazet 	IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS);
5811da177e4SLinus Torvalds 	kfree_skb(skb);
5821da177e4SLinus Torvalds 	return -1;
58398b3377cSDenis V. Lunev 
58498b3377cSDenis V. Lunev fail_hdr:
585adf30907SEric Dumazet 	IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS);
58698b3377cSDenis V. Lunev 	icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
58798b3377cSDenis V. Lunev 	return -1;
5881da177e4SLinus Torvalds }
5891da177e4SLinus Torvalds 
59041135cc8SAlexey Dobriyan static const struct inet6_protocol frag_protocol =
5911da177e4SLinus Torvalds {
5921da177e4SLinus Torvalds 	.handler	=	ipv6_frag_rcv,
5931da177e4SLinus Torvalds 	.flags		=	INET6_PROTO_NOPOLICY,
5941da177e4SLinus Torvalds };
5951da177e4SLinus Torvalds 
5968d8354d2SPavel Emelyanov #ifdef CONFIG_SYSCTL
5970a64b4b8SPavel Emelyanov static struct ctl_table ip6_frags_ns_ctl_table[] = {
598e71e0349SDaniel Lezcano 	{
5998d8354d2SPavel Emelyanov 		.procname	= "ip6frag_high_thresh",
600e31e0bdcSPavel Emelyanov 		.data		= &init_net.ipv6.frags.high_thresh,
6018d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6028d8354d2SPavel Emelyanov 		.mode		= 0644,
6036d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec
6048d8354d2SPavel Emelyanov 	},
6058d8354d2SPavel Emelyanov 	{
6068d8354d2SPavel Emelyanov 		.procname	= "ip6frag_low_thresh",
607e31e0bdcSPavel Emelyanov 		.data		= &init_net.ipv6.frags.low_thresh,
6088d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6098d8354d2SPavel Emelyanov 		.mode		= 0644,
6106d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec
6118d8354d2SPavel Emelyanov 	},
6128d8354d2SPavel Emelyanov 	{
6138d8354d2SPavel Emelyanov 		.procname	= "ip6frag_time",
614b2fd5321SPavel Emelyanov 		.data		= &init_net.ipv6.frags.timeout,
6158d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6168d8354d2SPavel Emelyanov 		.mode		= 0644,
6176d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
6188d8354d2SPavel Emelyanov 	},
6197d291ebbSPavel Emelyanov 	{ }
6207d291ebbSPavel Emelyanov };
6217d291ebbSPavel Emelyanov 
6227d291ebbSPavel Emelyanov static struct ctl_table ip6_frags_ctl_table[] = {
6238d8354d2SPavel Emelyanov 	{
6248d8354d2SPavel Emelyanov 		.procname	= "ip6frag_secret_interval",
6253b4bc4a2SPavel Emelyanov 		.data		= &ip6_frags.secret_interval,
6268d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6278d8354d2SPavel Emelyanov 		.mode		= 0644,
6286d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
6298d8354d2SPavel Emelyanov 	},
6308d8354d2SPavel Emelyanov 	{ }
6318d8354d2SPavel Emelyanov };
6327d460db9SDaniel Lezcano 
6332c8c1e72SAlexey Dobriyan static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
6348d8354d2SPavel Emelyanov {
635e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
6368d8354d2SPavel Emelyanov 	struct ctl_table_header *hdr;
6378d8354d2SPavel Emelyanov 
6380a64b4b8SPavel Emelyanov 	table = ip6_frags_ns_ctl_table;
63909ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net)) {
6400a64b4b8SPavel Emelyanov 		table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
641e4a2d5c2SPavel Emelyanov 		if (table == NULL)
642e4a2d5c2SPavel Emelyanov 			goto err_alloc;
643e4a2d5c2SPavel Emelyanov 
644e31e0bdcSPavel Emelyanov 		table[0].data = &net->ipv6.frags.high_thresh;
645e31e0bdcSPavel Emelyanov 		table[1].data = &net->ipv6.frags.low_thresh;
646b2fd5321SPavel Emelyanov 		table[2].data = &net->ipv6.frags.timeout;
647e4a2d5c2SPavel Emelyanov 	}
648e4a2d5c2SPavel Emelyanov 
649e4a2d5c2SPavel Emelyanov 	hdr = register_net_sysctl_table(net, net_ipv6_ctl_path, table);
650e4a2d5c2SPavel Emelyanov 	if (hdr == NULL)
651e4a2d5c2SPavel Emelyanov 		goto err_reg;
652e4a2d5c2SPavel Emelyanov 
653e4a2d5c2SPavel Emelyanov 	net->ipv6.sysctl.frags_hdr = hdr;
654e4a2d5c2SPavel Emelyanov 	return 0;
655e4a2d5c2SPavel Emelyanov 
656e4a2d5c2SPavel Emelyanov err_reg:
65709ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net))
658e4a2d5c2SPavel Emelyanov 		kfree(table);
659e4a2d5c2SPavel Emelyanov err_alloc:
660e4a2d5c2SPavel Emelyanov 	return -ENOMEM;
661e4a2d5c2SPavel Emelyanov }
662e4a2d5c2SPavel Emelyanov 
6632c8c1e72SAlexey Dobriyan static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
664e4a2d5c2SPavel Emelyanov {
665e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
666e4a2d5c2SPavel Emelyanov 
667e4a2d5c2SPavel Emelyanov 	table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
668e4a2d5c2SPavel Emelyanov 	unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
6693705e11aSYang Hongyang 	if (!net_eq(net, &init_net))
670e4a2d5c2SPavel Emelyanov 		kfree(table);
6718d8354d2SPavel Emelyanov }
6727d291ebbSPavel Emelyanov 
6737d291ebbSPavel Emelyanov static struct ctl_table_header *ip6_ctl_header;
6747d291ebbSPavel Emelyanov 
6757d291ebbSPavel Emelyanov static int ip6_frags_sysctl_register(void)
6767d291ebbSPavel Emelyanov {
67743444757SEric W. Biederman 	ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6",
6787d291ebbSPavel Emelyanov 			ip6_frags_ctl_table);
6797d291ebbSPavel Emelyanov 	return ip6_ctl_header == NULL ? -ENOMEM : 0;
6807d291ebbSPavel Emelyanov }
6817d291ebbSPavel Emelyanov 
6827d291ebbSPavel Emelyanov static void ip6_frags_sysctl_unregister(void)
6837d291ebbSPavel Emelyanov {
6847d291ebbSPavel Emelyanov 	unregister_net_sysctl_table(ip6_ctl_header);
6857d291ebbSPavel Emelyanov }
6868d8354d2SPavel Emelyanov #else
6870a64b4b8SPavel Emelyanov static inline int ip6_frags_ns_sysctl_register(struct net *net)
6888d8354d2SPavel Emelyanov {
6898d8354d2SPavel Emelyanov 	return 0;
6908d8354d2SPavel Emelyanov }
691e4a2d5c2SPavel Emelyanov 
6920a64b4b8SPavel Emelyanov static inline void ip6_frags_ns_sysctl_unregister(struct net *net)
693e4a2d5c2SPavel Emelyanov {
694e4a2d5c2SPavel Emelyanov }
6957d291ebbSPavel Emelyanov 
6967d291ebbSPavel Emelyanov static inline int ip6_frags_sysctl_register(void)
6977d291ebbSPavel Emelyanov {
6987d291ebbSPavel Emelyanov 	return 0;
6997d291ebbSPavel Emelyanov }
7007d291ebbSPavel Emelyanov 
7017d291ebbSPavel Emelyanov static inline void ip6_frags_sysctl_unregister(void)
7027d291ebbSPavel Emelyanov {
7037d291ebbSPavel Emelyanov }
7048d8354d2SPavel Emelyanov #endif
7058d8354d2SPavel Emelyanov 
7062c8c1e72SAlexey Dobriyan static int __net_init ipv6_frags_init_net(struct net *net)
7078d8354d2SPavel Emelyanov {
7087c070aa9SShan Wei 	net->ipv6.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
7097c070aa9SShan Wei 	net->ipv6.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
710b2fd5321SPavel Emelyanov 	net->ipv6.frags.timeout = IPV6_FRAG_TIMEOUT;
7118d8354d2SPavel Emelyanov 
712e5a2bb84SPavel Emelyanov 	inet_frags_init_net(&net->ipv6.frags);
713e5a2bb84SPavel Emelyanov 
7140a64b4b8SPavel Emelyanov 	return ip6_frags_ns_sysctl_register(net);
715e71e0349SDaniel Lezcano }
716e71e0349SDaniel Lezcano 
7172c8c1e72SAlexey Dobriyan static void __net_exit ipv6_frags_exit_net(struct net *net)
71881566e83SPavel Emelyanov {
7190a64b4b8SPavel Emelyanov 	ip6_frags_ns_sysctl_unregister(net);
72081566e83SPavel Emelyanov 	inet_frags_exit_net(&net->ipv6.frags, &ip6_frags);
72181566e83SPavel Emelyanov }
72281566e83SPavel Emelyanov 
72381566e83SPavel Emelyanov static struct pernet_operations ip6_frags_ops = {
72481566e83SPavel Emelyanov 	.init = ipv6_frags_init_net,
72581566e83SPavel Emelyanov 	.exit = ipv6_frags_exit_net,
72681566e83SPavel Emelyanov };
72781566e83SPavel Emelyanov 
728853cbbaaSDaniel Lezcano int __init ipv6_frag_init(void)
7291da177e4SLinus Torvalds {
730853cbbaaSDaniel Lezcano 	int ret;
7311da177e4SLinus Torvalds 
732853cbbaaSDaniel Lezcano 	ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
733853cbbaaSDaniel Lezcano 	if (ret)
734853cbbaaSDaniel Lezcano 		goto out;
735e71e0349SDaniel Lezcano 
7367d291ebbSPavel Emelyanov 	ret = ip6_frags_sysctl_register();
7377d291ebbSPavel Emelyanov 	if (ret)
7387d291ebbSPavel Emelyanov 		goto err_sysctl;
7397d291ebbSPavel Emelyanov 
7400002c630SPavel Emelyanov 	ret = register_pernet_subsys(&ip6_frags_ops);
7410002c630SPavel Emelyanov 	if (ret)
7420002c630SPavel Emelyanov 		goto err_pernet;
7438d8354d2SPavel Emelyanov 
744321a3a99SPavel Emelyanov 	ip6_frags.hashfn = ip6_hashfn;
745c6fda282SPavel Emelyanov 	ip6_frags.constructor = ip6_frag_init;
746c9547709SPavel Emelyanov 	ip6_frags.destructor = NULL;
7471e4b8287SPavel Emelyanov 	ip6_frags.skb_free = NULL;
7481e4b8287SPavel Emelyanov 	ip6_frags.qsize = sizeof(struct frag_queue);
749abd6523dSPavel Emelyanov 	ip6_frags.match = ip6_frag_match;
750e521db9dSPavel Emelyanov 	ip6_frags.frag_expire = ip6_frag_expire;
7513b4bc4a2SPavel Emelyanov 	ip6_frags.secret_interval = 10 * 60 * HZ;
7527eb95156SPavel Emelyanov 	inet_frags_init(&ip6_frags);
753853cbbaaSDaniel Lezcano out:
754853cbbaaSDaniel Lezcano 	return ret;
7550002c630SPavel Emelyanov 
7560002c630SPavel Emelyanov err_pernet:
7577d291ebbSPavel Emelyanov 	ip6_frags_sysctl_unregister();
7587d291ebbSPavel Emelyanov err_sysctl:
7590002c630SPavel Emelyanov 	inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
7600002c630SPavel Emelyanov 	goto out;
761853cbbaaSDaniel Lezcano }
762853cbbaaSDaniel Lezcano 
763853cbbaaSDaniel Lezcano void ipv6_frag_exit(void)
764853cbbaaSDaniel Lezcano {
765853cbbaaSDaniel Lezcano 	inet_frags_fini(&ip6_frags);
7667d291ebbSPavel Emelyanov 	ip6_frags_sysctl_unregister();
76781566e83SPavel Emelyanov 	unregister_pernet_subsys(&ip6_frags_ops);
768853cbbaaSDaniel Lezcano 	inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
7691da177e4SLinus Torvalds }
770