xref: /openbmc/linux/net/ipv6/reassembly.c (revision d27f9b35)
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>
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #include <net/sock.h>
471da177e4SLinus Torvalds #include <net/snmp.h>
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds #include <net/ipv6.h>
50a11d206dSYOSHIFUJI Hideaki #include <net/ip6_route.h>
511da177e4SLinus Torvalds #include <net/protocol.h>
521da177e4SLinus Torvalds #include <net/transp_v6.h>
531da177e4SLinus Torvalds #include <net/rawv6.h>
541da177e4SLinus Torvalds #include <net/ndisc.h>
551da177e4SLinus Torvalds #include <net/addrconf.h>
565ab11c98SPavel Emelyanov #include <net/inet_frag.h>
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds struct ip6frag_skb_cb
591da177e4SLinus Torvalds {
601da177e4SLinus Torvalds 	struct inet6_skb_parm	h;
611da177e4SLinus Torvalds 	int			offset;
621da177e4SLinus Torvalds };
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds #define FRAG6_CB(skb)	((struct ip6frag_skb_cb*)((skb)->cb))
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /*
681da177e4SLinus Torvalds  *	Equivalent of ipv4 struct ipq
691da177e4SLinus Torvalds  */
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds struct frag_queue
721da177e4SLinus Torvalds {
735ab11c98SPavel Emelyanov 	struct inet_frag_queue	q;
741da177e4SLinus Torvalds 
75e69a4adcSAl Viro 	__be32			id;		/* fragment id		*/
760b5ccb2eSPatrick McHardy 	u32			user;
771da177e4SLinus Torvalds 	struct in6_addr		saddr;
781da177e4SLinus Torvalds 	struct in6_addr		daddr;
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	int			iif;
811da177e4SLinus Torvalds 	unsigned int		csum;
821da177e4SLinus Torvalds 	__u16			nhoffset;
831da177e4SLinus Torvalds };
841da177e4SLinus Torvalds 
857eb95156SPavel Emelyanov static struct inet_frags ip6_frags;
861da177e4SLinus Torvalds 
87e5a2bb84SPavel Emelyanov int ip6_frag_nqueues(struct net *net)
887eb95156SPavel Emelyanov {
89e5a2bb84SPavel Emelyanov 	return net->ipv6.frags.nqueues;
907eb95156SPavel Emelyanov }
911da177e4SLinus Torvalds 
926ddc0822SPavel Emelyanov int ip6_frag_mem(struct net *net)
937eb95156SPavel Emelyanov {
946ddc0822SPavel Emelyanov 	return atomic_read(&net->ipv6.frags.mem);
957eb95156SPavel Emelyanov }
961da177e4SLinus Torvalds 
97f61944efSHerbert Xu static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
98f61944efSHerbert Xu 			  struct net_device *dev);
99f61944efSHerbert Xu 
100f6596f9dSZach Brown /*
101f6596f9dSZach Brown  * callers should be careful not to use the hash value outside the ipfrag_lock
102f6596f9dSZach Brown  * as doing so could race with ipfrag_hash_rnd being recalculated.
103f6596f9dSZach Brown  */
10493c8b90fSIlpo Järvinen unsigned int inet6_hash_frag(__be32 id, const struct in6_addr *saddr,
10593c8b90fSIlpo Järvinen 			     const struct in6_addr *daddr, u32 rnd)
1061da177e4SLinus Torvalds {
1071da177e4SLinus Torvalds 	u32 a, b, c;
1081da177e4SLinus Torvalds 
109e69a4adcSAl Viro 	a = (__force u32)saddr->s6_addr32[0];
110e69a4adcSAl Viro 	b = (__force u32)saddr->s6_addr32[1];
111e69a4adcSAl Viro 	c = (__force u32)saddr->s6_addr32[2];
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 	a += JHASH_GOLDEN_RATIO;
1141da177e4SLinus Torvalds 	b += JHASH_GOLDEN_RATIO;
11593c8b90fSIlpo Järvinen 	c += rnd;
1161da177e4SLinus Torvalds 	__jhash_mix(a, b, c);
1171da177e4SLinus Torvalds 
118e69a4adcSAl Viro 	a += (__force u32)saddr->s6_addr32[3];
119e69a4adcSAl Viro 	b += (__force u32)daddr->s6_addr32[0];
120e69a4adcSAl Viro 	c += (__force u32)daddr->s6_addr32[1];
1211da177e4SLinus Torvalds 	__jhash_mix(a, b, c);
1221da177e4SLinus Torvalds 
123e69a4adcSAl Viro 	a += (__force u32)daddr->s6_addr32[2];
124e69a4adcSAl Viro 	b += (__force u32)daddr->s6_addr32[3];
125e69a4adcSAl Viro 	c += (__force u32)id;
1261da177e4SLinus Torvalds 	__jhash_mix(a, b, c);
1271da177e4SLinus Torvalds 
1287eb95156SPavel Emelyanov 	return c & (INETFRAGS_HASHSZ - 1);
1291da177e4SLinus Torvalds }
13093c8b90fSIlpo Järvinen EXPORT_SYMBOL_GPL(inet6_hash_frag);
1311da177e4SLinus Torvalds 
132321a3a99SPavel Emelyanov static unsigned int ip6_hashfn(struct inet_frag_queue *q)
1331da177e4SLinus Torvalds {
134321a3a99SPavel Emelyanov 	struct frag_queue *fq;
1351da177e4SLinus Torvalds 
136321a3a99SPavel Emelyanov 	fq = container_of(q, struct frag_queue, q);
13793c8b90fSIlpo Järvinen 	return inet6_hash_frag(fq->id, &fq->saddr, &fq->daddr, ip6_frags.rnd);
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
140abd6523dSPavel Emelyanov int ip6_frag_match(struct inet_frag_queue *q, void *a)
141abd6523dSPavel Emelyanov {
142abd6523dSPavel Emelyanov 	struct frag_queue *fq;
143abd6523dSPavel Emelyanov 	struct ip6_create_arg *arg = a;
144abd6523dSPavel Emelyanov 
145abd6523dSPavel Emelyanov 	fq = container_of(q, struct frag_queue, q);
1460b5ccb2eSPatrick McHardy 	return (fq->id == arg->id && fq->user == arg->user &&
147abd6523dSPavel Emelyanov 			ipv6_addr_equal(&fq->saddr, arg->src) &&
148abd6523dSPavel Emelyanov 			ipv6_addr_equal(&fq->daddr, arg->dst));
149abd6523dSPavel Emelyanov }
150abd6523dSPavel Emelyanov EXPORT_SYMBOL(ip6_frag_match);
151abd6523dSPavel Emelyanov 
1521da177e4SLinus Torvalds /* Memory Tracking Functions. */
1536ddc0822SPavel Emelyanov static inline void frag_kfree_skb(struct netns_frags *nf,
1546ddc0822SPavel Emelyanov 		struct sk_buff *skb, int *work)
1551da177e4SLinus Torvalds {
1561da177e4SLinus Torvalds 	if (work)
1571da177e4SLinus Torvalds 		*work -= skb->truesize;
1586ddc0822SPavel Emelyanov 	atomic_sub(skb->truesize, &nf->mem);
1591da177e4SLinus Torvalds 	kfree_skb(skb);
1601da177e4SLinus Torvalds }
1611da177e4SLinus Torvalds 
162c6fda282SPavel Emelyanov void ip6_frag_init(struct inet_frag_queue *q, void *a)
163c6fda282SPavel Emelyanov {
164c6fda282SPavel Emelyanov 	struct frag_queue *fq = container_of(q, struct frag_queue, q);
165c6fda282SPavel Emelyanov 	struct ip6_create_arg *arg = a;
166c6fda282SPavel Emelyanov 
167c6fda282SPavel Emelyanov 	fq->id = arg->id;
1680b5ccb2eSPatrick McHardy 	fq->user = arg->user;
169c6fda282SPavel Emelyanov 	ipv6_addr_copy(&fq->saddr, arg->src);
170c6fda282SPavel Emelyanov 	ipv6_addr_copy(&fq->daddr, arg->dst);
171c6fda282SPavel Emelyanov }
172c6fda282SPavel Emelyanov EXPORT_SYMBOL(ip6_frag_init);
173c6fda282SPavel Emelyanov 
1741da177e4SLinus Torvalds /* Destruction primitives. */
1751da177e4SLinus Torvalds 
1764b6cb5d8SPavel Emelyanov static __inline__ void fq_put(struct frag_queue *fq)
1771da177e4SLinus Torvalds {
178762cc408SPavel Emelyanov 	inet_frag_put(&fq->q, &ip6_frags);
1791da177e4SLinus Torvalds }
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds /* Kill fq entry. It is not destroyed immediately,
1821da177e4SLinus Torvalds  * because caller (and someone more) holds reference count.
1831da177e4SLinus Torvalds  */
1841da177e4SLinus Torvalds static __inline__ void fq_kill(struct frag_queue *fq)
1851da177e4SLinus Torvalds {
186277e650dSPavel Emelyanov 	inet_frag_kill(&fq->q, &ip6_frags);
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
1896ddc0822SPavel Emelyanov static void ip6_evictor(struct net *net, struct inet6_dev *idev)
1901da177e4SLinus Torvalds {
1918e7999c4SPavel Emelyanov 	int evicted;
1921da177e4SLinus Torvalds 
1936ddc0822SPavel Emelyanov 	evicted = inet_frag_evictor(&net->ipv6.frags, &ip6_frags);
1948e7999c4SPavel Emelyanov 	if (evicted)
195821d5777SDenis V. Lunev 		IP6_ADD_STATS_BH(net, idev, IPSTATS_MIB_REASMFAILS, evicted);
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds static void ip6_frag_expire(unsigned long data)
1991da177e4SLinus Torvalds {
200e521db9dSPavel Emelyanov 	struct frag_queue *fq;
201a11d206dSYOSHIFUJI Hideaki 	struct net_device *dev = NULL;
2024ac2ccd0SDaniel Lezcano 	struct net *net;
2031da177e4SLinus Torvalds 
204e521db9dSPavel Emelyanov 	fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q);
205e521db9dSPavel Emelyanov 
2065ab11c98SPavel Emelyanov 	spin_lock(&fq->q.lock);
2071da177e4SLinus Torvalds 
208bc578a54SJoe Perches 	if (fq->q.last_in & INET_FRAG_COMPLETE)
2091da177e4SLinus Torvalds 		goto out;
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	fq_kill(fq);
2121da177e4SLinus Torvalds 
2134ac2ccd0SDaniel Lezcano 	net = container_of(fq->q.net, struct net, ipv6.frags);
214a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
21569df9d59SEric Dumazet 	dev = dev_get_by_index_rcu(net, fq->iif);
21669df9d59SEric Dumazet 	if (!dev)
21769df9d59SEric Dumazet 		goto out_rcu_unlock;
21869df9d59SEric Dumazet 
219483a47d2SDenis V. Lunev 	IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT);
220483a47d2SDenis V. Lunev 	IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
2211da177e4SLinus Torvalds 
22278c784c4SIngo Oeser 	/* Don't send error if the first segment did not arrive. */
223bc578a54SJoe Perches 	if (!(fq->q.last_in & INET_FRAG_FIRST_IN) || !fq->q.fragments)
22469df9d59SEric Dumazet 		goto out_rcu_unlock;
22578c784c4SIngo Oeser 
2261da177e4SLinus Torvalds 	/*
2271da177e4SLinus Torvalds 	   But use as source device on which LAST ARRIVED
2281da177e4SLinus Torvalds 	   segment was received. And do not use fq->dev
2291da177e4SLinus Torvalds 	   pointer directly, device might already disappeared.
2301da177e4SLinus Torvalds 	 */
2315ab11c98SPavel Emelyanov 	fq->q.fragments->dev = dev;
2323ffe533cSAlexey Dobriyan 	icmpv6_send(fq->q.fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0);
23369df9d59SEric Dumazet out_rcu_unlock:
23469df9d59SEric Dumazet 	rcu_read_unlock();
2351da177e4SLinus Torvalds out:
2365ab11c98SPavel Emelyanov 	spin_unlock(&fq->q.lock);
2374b6cb5d8SPavel Emelyanov 	fq_put(fq);
2381da177e4SLinus Torvalds }
2391da177e4SLinus Torvalds 
240abd6523dSPavel Emelyanov static __inline__ struct frag_queue *
2419546377cSShan Wei fq_find(struct net *net, __be32 id, struct in6_addr *src, struct in6_addr *dst)
2421da177e4SLinus Torvalds {
243c6fda282SPavel Emelyanov 	struct inet_frag_queue *q;
244c6fda282SPavel Emelyanov 	struct ip6_create_arg arg;
245abd6523dSPavel Emelyanov 	unsigned int hash;
2461da177e4SLinus Torvalds 
247c6fda282SPavel Emelyanov 	arg.id = id;
2480b5ccb2eSPatrick McHardy 	arg.user = IP6_DEFRAG_LOCAL_DELIVER;
249c6fda282SPavel Emelyanov 	arg.src = src;
250c6fda282SPavel Emelyanov 	arg.dst = dst;
2519a375803SPavel Emelyanov 
2529a375803SPavel Emelyanov 	read_lock(&ip6_frags.lock);
25393c8b90fSIlpo Järvinen 	hash = inet6_hash_frag(id, src, dst, ip6_frags.rnd);
254c6fda282SPavel Emelyanov 
255ac18e750SPavel Emelyanov 	q = inet_frag_find(&net->ipv6.frags, &ip6_frags, &arg, hash);
256c6fda282SPavel Emelyanov 	if (q == NULL)
2579546377cSShan Wei 		return NULL;
2581da177e4SLinus Torvalds 
259c6fda282SPavel Emelyanov 	return container_of(q, struct frag_queue, q);
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds 
262f61944efSHerbert Xu static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
2631da177e4SLinus Torvalds 			   struct frag_hdr *fhdr, int nhoff)
2641da177e4SLinus Torvalds {
2651da177e4SLinus Torvalds 	struct sk_buff *prev, *next;
266f61944efSHerbert Xu 	struct net_device *dev;
2671da177e4SLinus Torvalds 	int offset, end;
268adf30907SEric Dumazet 	struct net *net = dev_net(skb_dst(skb)->dev);
2691da177e4SLinus Torvalds 
270bc578a54SJoe Perches 	if (fq->q.last_in & INET_FRAG_COMPLETE)
2711da177e4SLinus Torvalds 		goto err;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	offset = ntohs(fhdr->frag_off) & ~0x7;
2740660e03fSArnaldo Carvalho de Melo 	end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
2750660e03fSArnaldo Carvalho de Melo 			((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	if ((unsigned int)end > IPV6_MAXPLEN) {
278adf30907SEric Dumazet 		IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
279a11d206dSYOSHIFUJI Hideaki 				 IPSTATS_MIB_INHDRERRORS);
280d56f90a7SArnaldo Carvalho de Melo 		icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
281d56f90a7SArnaldo Carvalho de Melo 				  ((u8 *)&fhdr->frag_off -
282d56f90a7SArnaldo Carvalho de Melo 				   skb_network_header(skb)));
283f61944efSHerbert Xu 		return -1;
2841da177e4SLinus Torvalds 	}
2851da177e4SLinus Torvalds 
286d56f90a7SArnaldo Carvalho de Melo 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
287d56f90a7SArnaldo Carvalho de Melo 		const unsigned char *nh = skb_network_header(skb);
2881da177e4SLinus Torvalds 		skb->csum = csum_sub(skb->csum,
289d56f90a7SArnaldo Carvalho de Melo 				     csum_partial(nh, (u8 *)(fhdr + 1) - nh,
290d56f90a7SArnaldo Carvalho de Melo 						  0));
291d56f90a7SArnaldo Carvalho de Melo 	}
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	/* Is this the final fragment? */
2941da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(IP6_MF))) {
2951da177e4SLinus Torvalds 		/* If we already have some bits beyond end
2961da177e4SLinus Torvalds 		 * or have different end, the segment is corrupted.
2971da177e4SLinus Torvalds 		 */
2985ab11c98SPavel Emelyanov 		if (end < fq->q.len ||
299bc578a54SJoe Perches 		    ((fq->q.last_in & INET_FRAG_LAST_IN) && end != fq->q.len))
3001da177e4SLinus Torvalds 			goto err;
301bc578a54SJoe Perches 		fq->q.last_in |= INET_FRAG_LAST_IN;
3025ab11c98SPavel Emelyanov 		fq->q.len = end;
3031da177e4SLinus Torvalds 	} else {
3041da177e4SLinus Torvalds 		/* Check if the fragment is rounded to 8 bytes.
3051da177e4SLinus Torvalds 		 * Required by the RFC.
3061da177e4SLinus Torvalds 		 */
3071da177e4SLinus Torvalds 		if (end & 0x7) {
3081da177e4SLinus Torvalds 			/* RFC2460 says always send parameter problem in
3091da177e4SLinus Torvalds 			 * this case. -DaveM
3101da177e4SLinus Torvalds 			 */
311adf30907SEric Dumazet 			IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)),
312a11d206dSYOSHIFUJI Hideaki 					 IPSTATS_MIB_INHDRERRORS);
3131da177e4SLinus Torvalds 			icmpv6_param_prob(skb, ICMPV6_HDR_FIELD,
3141da177e4SLinus Torvalds 					  offsetof(struct ipv6hdr, payload_len));
315f61944efSHerbert Xu 			return -1;
3161da177e4SLinus Torvalds 		}
3175ab11c98SPavel Emelyanov 		if (end > fq->q.len) {
3181da177e4SLinus Torvalds 			/* Some bits beyond end -> corruption. */
319bc578a54SJoe Perches 			if (fq->q.last_in & INET_FRAG_LAST_IN)
3201da177e4SLinus Torvalds 				goto err;
3215ab11c98SPavel Emelyanov 			fq->q.len = end;
3221da177e4SLinus Torvalds 		}
3231da177e4SLinus Torvalds 	}
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 	if (end == offset)
3261da177e4SLinus Torvalds 		goto err;
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	/* Point into the IP datagram 'data' part. */
3291da177e4SLinus Torvalds 	if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
3301da177e4SLinus Torvalds 		goto err;
33142ca89c1SStephen Hemminger 
33242ca89c1SStephen Hemminger 	if (pskb_trim_rcsum(skb, end - offset))
3331da177e4SLinus Torvalds 		goto err;
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	/* Find out which fragments are in front and at the back of us
3361da177e4SLinus Torvalds 	 * in the chain of fragments so far.  We must know where to put
3371da177e4SLinus Torvalds 	 * this fragment, right?
3381da177e4SLinus Torvalds 	 */
3391da177e4SLinus Torvalds 	prev = NULL;
3405ab11c98SPavel Emelyanov 	for(next = fq->q.fragments; next != NULL; next = next->next) {
3411da177e4SLinus Torvalds 		if (FRAG6_CB(next)->offset >= offset)
3421da177e4SLinus Torvalds 			break;	/* bingo! */
3431da177e4SLinus Torvalds 		prev = next;
3441da177e4SLinus Torvalds 	}
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	/* We found where to put this one.  Check for overlap with
3471da177e4SLinus Torvalds 	 * preceding fragment, and, if needed, align things so that
3481da177e4SLinus Torvalds 	 * any overlaps are eliminated.
3491da177e4SLinus Torvalds 	 */
3501da177e4SLinus Torvalds 	if (prev) {
3511da177e4SLinus Torvalds 		int i = (FRAG6_CB(prev)->offset + prev->len) - offset;
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 		if (i > 0) {
3541da177e4SLinus Torvalds 			offset += i;
3551da177e4SLinus Torvalds 			if (end <= offset)
3561da177e4SLinus Torvalds 				goto err;
3571da177e4SLinus Torvalds 			if (!pskb_pull(skb, i))
3581da177e4SLinus Torvalds 				goto err;
3591da177e4SLinus Torvalds 			if (skb->ip_summed != CHECKSUM_UNNECESSARY)
3601da177e4SLinus Torvalds 				skb->ip_summed = CHECKSUM_NONE;
3611da177e4SLinus Torvalds 		}
3621da177e4SLinus Torvalds 	}
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	/* Look for overlap with succeeding segments.
3651da177e4SLinus Torvalds 	 * If we can merge fragments, do it.
3661da177e4SLinus Torvalds 	 */
3671da177e4SLinus Torvalds 	while (next && FRAG6_CB(next)->offset < end) {
3681da177e4SLinus Torvalds 		int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 		if (i < next->len) {
3711da177e4SLinus Torvalds 			/* Eat head of the next overlapped fragment
3721da177e4SLinus Torvalds 			 * and leave the loop. The next ones cannot overlap.
3731da177e4SLinus Torvalds 			 */
3741da177e4SLinus Torvalds 			if (!pskb_pull(next, i))
3751da177e4SLinus Torvalds 				goto err;
3761da177e4SLinus Torvalds 			FRAG6_CB(next)->offset += i;	/* next fragment */
3775ab11c98SPavel Emelyanov 			fq->q.meat -= i;
3781da177e4SLinus Torvalds 			if (next->ip_summed != CHECKSUM_UNNECESSARY)
3791da177e4SLinus Torvalds 				next->ip_summed = CHECKSUM_NONE;
3801da177e4SLinus Torvalds 			break;
3811da177e4SLinus Torvalds 		} else {
3821da177e4SLinus Torvalds 			struct sk_buff *free_it = next;
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 			/* Old fragment is completely overridden with
3851da177e4SLinus Torvalds 			 * new one drop it.
3861da177e4SLinus Torvalds 			 */
3871da177e4SLinus Torvalds 			next = next->next;
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 			if (prev)
3901da177e4SLinus Torvalds 				prev->next = next;
3911da177e4SLinus Torvalds 			else
3925ab11c98SPavel Emelyanov 				fq->q.fragments = next;
3931da177e4SLinus Torvalds 
3945ab11c98SPavel Emelyanov 			fq->q.meat -= free_it->len;
3956ddc0822SPavel Emelyanov 			frag_kfree_skb(fq->q.net, free_it, NULL);
3961da177e4SLinus Torvalds 		}
3971da177e4SLinus Torvalds 	}
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	FRAG6_CB(skb)->offset = offset;
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	/* Insert this fragment in the chain of fragments. */
4021da177e4SLinus Torvalds 	skb->next = next;
4031da177e4SLinus Torvalds 	if (prev)
4041da177e4SLinus Torvalds 		prev->next = skb;
4051da177e4SLinus Torvalds 	else
4065ab11c98SPavel Emelyanov 		fq->q.fragments = skb;
4071da177e4SLinus Torvalds 
408f61944efSHerbert Xu 	dev = skb->dev;
409f61944efSHerbert Xu 	if (dev) {
410f61944efSHerbert Xu 		fq->iif = dev->ifindex;
4111da177e4SLinus Torvalds 		skb->dev = NULL;
412f61944efSHerbert Xu 	}
4135ab11c98SPavel Emelyanov 	fq->q.stamp = skb->tstamp;
4145ab11c98SPavel Emelyanov 	fq->q.meat += skb->len;
4156ddc0822SPavel Emelyanov 	atomic_add(skb->truesize, &fq->q.net->mem);
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	/* The first fragment.
4181da177e4SLinus Torvalds 	 * nhoffset is obtained from the first fragment, of course.
4191da177e4SLinus Torvalds 	 */
4201da177e4SLinus Torvalds 	if (offset == 0) {
4211da177e4SLinus Torvalds 		fq->nhoffset = nhoff;
422bc578a54SJoe Perches 		fq->q.last_in |= INET_FRAG_FIRST_IN;
4231da177e4SLinus Torvalds 	}
424f61944efSHerbert Xu 
425bc578a54SJoe Perches 	if (fq->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
426bc578a54SJoe Perches 	    fq->q.meat == fq->q.len)
427f61944efSHerbert Xu 		return ip6_frag_reasm(fq, prev, dev);
428f61944efSHerbert Xu 
4297eb95156SPavel Emelyanov 	write_lock(&ip6_frags.lock);
4303140c25cSPavel Emelyanov 	list_move_tail(&fq->q.lru_list, &fq->q.net->lru_list);
4317eb95156SPavel Emelyanov 	write_unlock(&ip6_frags.lock);
432f61944efSHerbert Xu 	return -1;
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds err:
435adf30907SEric Dumazet 	IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
4363bd653c8SDenis V. Lunev 		      IPSTATS_MIB_REASMFAILS);
4371da177e4SLinus Torvalds 	kfree_skb(skb);
438f61944efSHerbert Xu 	return -1;
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds /*
4421da177e4SLinus Torvalds  *	Check if this packet is complete.
4431da177e4SLinus Torvalds  *	Returns NULL on failure by any reason, and pointer
4441da177e4SLinus Torvalds  *	to current nexthdr field in reassembled frame.
4451da177e4SLinus Torvalds  *
4461da177e4SLinus Torvalds  *	It is called with locked fq, and caller must check that
4471da177e4SLinus Torvalds  *	queue is eligible for reassembly i.e. it is not COMPLETE,
4481da177e4SLinus Torvalds  *	the last and the first frames arrived and all the bits are here.
4491da177e4SLinus Torvalds  */
450f61944efSHerbert Xu static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
4511da177e4SLinus Torvalds 			  struct net_device *dev)
4521da177e4SLinus Torvalds {
4532bad35b7SJorge Boncompte [DTI2] 	struct net *net = container_of(fq->q.net, struct net, ipv6.frags);
4545ab11c98SPavel Emelyanov 	struct sk_buff *fp, *head = fq->q.fragments;
4551da177e4SLinus Torvalds 	int    payload_len;
4561da177e4SLinus Torvalds 	unsigned int nhoff;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	fq_kill(fq);
4591da177e4SLinus Torvalds 
460f61944efSHerbert Xu 	/* Make the one we just received the head. */
461f61944efSHerbert Xu 	if (prev) {
462f61944efSHerbert Xu 		head = prev->next;
463f61944efSHerbert Xu 		fp = skb_clone(head, GFP_ATOMIC);
464f61944efSHerbert Xu 
465f61944efSHerbert Xu 		if (!fp)
466f61944efSHerbert Xu 			goto out_oom;
467f61944efSHerbert Xu 
468f61944efSHerbert Xu 		fp->next = head->next;
469f61944efSHerbert Xu 		prev->next = fp;
470f61944efSHerbert Xu 
4715ab11c98SPavel Emelyanov 		skb_morph(head, fq->q.fragments);
4725ab11c98SPavel Emelyanov 		head->next = fq->q.fragments->next;
473f61944efSHerbert Xu 
4745ab11c98SPavel Emelyanov 		kfree_skb(fq->q.fragments);
4755ab11c98SPavel Emelyanov 		fq->q.fragments = head;
476f61944efSHerbert Xu 	}
477f61944efSHerbert Xu 
478547b792cSIlpo Järvinen 	WARN_ON(head == NULL);
479547b792cSIlpo Järvinen 	WARN_ON(FRAG6_CB(head)->offset != 0);
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 	/* Unfragmented part is taken from the first segment. */
482d56f90a7SArnaldo Carvalho de Melo 	payload_len = ((head->data - skb_network_header(head)) -
4835ab11c98SPavel Emelyanov 		       sizeof(struct ipv6hdr) + fq->q.len -
484d56f90a7SArnaldo Carvalho de Melo 		       sizeof(struct frag_hdr));
4851da177e4SLinus Torvalds 	if (payload_len > IPV6_MAXPLEN)
4861da177e4SLinus Torvalds 		goto out_oversize;
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds 	/* Head of list must not be cloned. */
4891da177e4SLinus Torvalds 	if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC))
4901da177e4SLinus Torvalds 		goto out_oom;
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	/* If the first fragment is fragmented itself, we split
4931da177e4SLinus Torvalds 	 * it to two chunks: the first with data and paged part
4941da177e4SLinus Torvalds 	 * and the second, holding only fragments. */
4954d9092bbSDavid S. Miller 	if (skb_has_frags(head)) {
4961da177e4SLinus Torvalds 		struct sk_buff *clone;
4971da177e4SLinus Torvalds 		int i, plen = 0;
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 		if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL)
5001da177e4SLinus Torvalds 			goto out_oom;
5011da177e4SLinus Torvalds 		clone->next = head->next;
5021da177e4SLinus Torvalds 		head->next = clone;
5031da177e4SLinus Torvalds 		skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list;
5044d9092bbSDavid S. Miller 		skb_frag_list_init(head);
5051da177e4SLinus Torvalds 		for (i=0; i<skb_shinfo(head)->nr_frags; i++)
5061da177e4SLinus Torvalds 			plen += skb_shinfo(head)->frags[i].size;
5071da177e4SLinus Torvalds 		clone->len = clone->data_len = head->data_len - plen;
5081da177e4SLinus Torvalds 		head->data_len -= clone->len;
5091da177e4SLinus Torvalds 		head->len -= clone->len;
5101da177e4SLinus Torvalds 		clone->csum = 0;
5111da177e4SLinus Torvalds 		clone->ip_summed = head->ip_summed;
5126ddc0822SPavel Emelyanov 		atomic_add(clone->truesize, &fq->q.net->mem);
5131da177e4SLinus Torvalds 	}
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	/* We have to remove fragment header from datagram and to relocate
5161da177e4SLinus Torvalds 	 * header in order to calculate ICV correctly. */
5171da177e4SLinus Torvalds 	nhoff = fq->nhoffset;
518b0e380b1SArnaldo Carvalho de Melo 	skb_network_header(head)[nhoff] = skb_transport_header(head)[0];
5191da177e4SLinus Torvalds 	memmove(head->head + sizeof(struct frag_hdr), head->head,
5201da177e4SLinus Torvalds 		(head->data - head->head) - sizeof(struct frag_hdr));
521b0e380b1SArnaldo Carvalho de Melo 	head->mac_header += sizeof(struct frag_hdr);
522b0e380b1SArnaldo Carvalho de Melo 	head->network_header += sizeof(struct frag_hdr);
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds 	skb_shinfo(head)->frag_list = head->next;
525badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(head);
526d56f90a7SArnaldo Carvalho de Melo 	skb_push(head, head->data - skb_network_header(head));
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	for (fp=head->next; fp; fp = fp->next) {
5291da177e4SLinus Torvalds 		head->data_len += fp->len;
5301da177e4SLinus Torvalds 		head->len += fp->len;
5311da177e4SLinus Torvalds 		if (head->ip_summed != fp->ip_summed)
5321da177e4SLinus Torvalds 			head->ip_summed = CHECKSUM_NONE;
53384fa7933SPatrick McHardy 		else if (head->ip_summed == CHECKSUM_COMPLETE)
5341da177e4SLinus Torvalds 			head->csum = csum_add(head->csum, fp->csum);
5351da177e4SLinus Torvalds 		head->truesize += fp->truesize;
5361da177e4SLinus Torvalds 	}
537d27f9b35SEric Dumazet 	atomic_sub(head->truesize, &fq->q.net->mem);
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	head->next = NULL;
5401da177e4SLinus Torvalds 	head->dev = dev;
5415ab11c98SPavel Emelyanov 	head->tstamp = fq->q.stamp;
5420660e03fSArnaldo Carvalho de Melo 	ipv6_hdr(head)->payload_len = htons(payload_len);
543951dbc8aSPatrick McHardy 	IP6CB(head)->nhoff = nhoff;
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	/* Yes, and fold redundant checksum back. 8) */
54684fa7933SPatrick McHardy 	if (head->ip_summed == CHECKSUM_COMPLETE)
547d56f90a7SArnaldo Carvalho de Melo 		head->csum = csum_partial(skb_network_header(head),
548cfe1fc77SArnaldo Carvalho de Melo 					  skb_network_header_len(head),
549d56f90a7SArnaldo Carvalho de Melo 					  head->csum);
5501da177e4SLinus Torvalds 
551a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
5522bad35b7SJorge Boncompte [DTI2] 	IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
553a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
5545ab11c98SPavel Emelyanov 	fq->q.fragments = NULL;
5551da177e4SLinus Torvalds 	return 1;
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds out_oversize:
5581da177e4SLinus Torvalds 	if (net_ratelimit())
5591da177e4SLinus Torvalds 		printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len);
5601da177e4SLinus Torvalds 	goto out_fail;
5611da177e4SLinus Torvalds out_oom:
5621da177e4SLinus Torvalds 	if (net_ratelimit())
5631da177e4SLinus Torvalds 		printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n");
5641da177e4SLinus Torvalds out_fail:
565a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
5662bad35b7SJorge Boncompte [DTI2] 	IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
567a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
5681da177e4SLinus Torvalds 	return -1;
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds 
571e5bbef20SHerbert Xu static int ipv6_frag_rcv(struct sk_buff *skb)
5721da177e4SLinus Torvalds {
5731da177e4SLinus Torvalds 	struct frag_hdr *fhdr;
5741da177e4SLinus Torvalds 	struct frag_queue *fq;
5750660e03fSArnaldo Carvalho de Melo 	struct ipv6hdr *hdr = ipv6_hdr(skb);
576adf30907SEric Dumazet 	struct net *net = dev_net(skb_dst(skb)->dev);
5771da177e4SLinus Torvalds 
578adf30907SEric Dumazet 	IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS);
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	/* Jumbo payload inhibits frag. header */
58198b3377cSDenis V. Lunev 	if (hdr->payload_len==0)
58298b3377cSDenis V. Lunev 		goto fail_hdr;
58398b3377cSDenis V. Lunev 
584ea2ae17dSArnaldo Carvalho de Melo 	if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
58598b3377cSDenis V. Lunev 				 sizeof(struct frag_hdr))))
58698b3377cSDenis V. Lunev 		goto fail_hdr;
5871da177e4SLinus Torvalds 
5880660e03fSArnaldo Carvalho de Melo 	hdr = ipv6_hdr(skb);
5899c70220bSArnaldo Carvalho de Melo 	fhdr = (struct frag_hdr *)skb_transport_header(skb);
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(0xFFF9))) {
5921da177e4SLinus Torvalds 		/* It is not a fragmented frame */
593b0e380b1SArnaldo Carvalho de Melo 		skb->transport_header += sizeof(struct frag_hdr);
594483a47d2SDenis V. Lunev 		IP6_INC_STATS_BH(net,
595adf30907SEric Dumazet 				 ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS);
5961da177e4SLinus Torvalds 
597d56f90a7SArnaldo Carvalho de Melo 		IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
5981da177e4SLinus Torvalds 		return 1;
5991da177e4SLinus Torvalds 	}
6001da177e4SLinus Torvalds 
601e31e0bdcSPavel Emelyanov 	if (atomic_read(&net->ipv6.frags.mem) > net->ipv6.frags.high_thresh)
602adf30907SEric Dumazet 		ip6_evictor(net, ip6_dst_idev(skb_dst(skb)));
6031da177e4SLinus Torvalds 
6049546377cSShan Wei 	fq = fq_find(net, fhdr->identification, &hdr->saddr, &hdr->daddr);
6059546377cSShan Wei 	if (fq != NULL) {
606f61944efSHerbert Xu 		int ret;
6071da177e4SLinus Torvalds 
6085ab11c98SPavel Emelyanov 		spin_lock(&fq->q.lock);
6091da177e4SLinus Torvalds 
610f61944efSHerbert Xu 		ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff);
6111da177e4SLinus Torvalds 
6125ab11c98SPavel Emelyanov 		spin_unlock(&fq->q.lock);
6134b6cb5d8SPavel Emelyanov 		fq_put(fq);
6141da177e4SLinus Torvalds 		return ret;
6151da177e4SLinus Torvalds 	}
6161da177e4SLinus Torvalds 
617adf30907SEric Dumazet 	IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS);
6181da177e4SLinus Torvalds 	kfree_skb(skb);
6191da177e4SLinus Torvalds 	return -1;
62098b3377cSDenis V. Lunev 
62198b3377cSDenis V. Lunev fail_hdr:
622adf30907SEric Dumazet 	IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS);
62398b3377cSDenis V. Lunev 	icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
62498b3377cSDenis V. Lunev 	return -1;
6251da177e4SLinus Torvalds }
6261da177e4SLinus Torvalds 
62741135cc8SAlexey Dobriyan static const struct inet6_protocol frag_protocol =
6281da177e4SLinus Torvalds {
6291da177e4SLinus Torvalds 	.handler	=	ipv6_frag_rcv,
6301da177e4SLinus Torvalds 	.flags		=	INET6_PROTO_NOPOLICY,
6311da177e4SLinus Torvalds };
6321da177e4SLinus Torvalds 
6338d8354d2SPavel Emelyanov #ifdef CONFIG_SYSCTL
6340a64b4b8SPavel Emelyanov static struct ctl_table ip6_frags_ns_ctl_table[] = {
635e71e0349SDaniel Lezcano 	{
6368d8354d2SPavel Emelyanov 		.procname	= "ip6frag_high_thresh",
637e31e0bdcSPavel Emelyanov 		.data		= &init_net.ipv6.frags.high_thresh,
6388d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6398d8354d2SPavel Emelyanov 		.mode		= 0644,
6406d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec
6418d8354d2SPavel Emelyanov 	},
6428d8354d2SPavel Emelyanov 	{
6438d8354d2SPavel Emelyanov 		.procname	= "ip6frag_low_thresh",
644e31e0bdcSPavel Emelyanov 		.data		= &init_net.ipv6.frags.low_thresh,
6458d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6468d8354d2SPavel Emelyanov 		.mode		= 0644,
6476d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec
6488d8354d2SPavel Emelyanov 	},
6498d8354d2SPavel Emelyanov 	{
6508d8354d2SPavel Emelyanov 		.procname	= "ip6frag_time",
651b2fd5321SPavel Emelyanov 		.data		= &init_net.ipv6.frags.timeout,
6528d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6538d8354d2SPavel Emelyanov 		.mode		= 0644,
6546d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
6558d8354d2SPavel Emelyanov 	},
6567d291ebbSPavel Emelyanov 	{ }
6577d291ebbSPavel Emelyanov };
6587d291ebbSPavel Emelyanov 
6597d291ebbSPavel Emelyanov static struct ctl_table ip6_frags_ctl_table[] = {
6608d8354d2SPavel Emelyanov 	{
6618d8354d2SPavel Emelyanov 		.procname	= "ip6frag_secret_interval",
6623b4bc4a2SPavel Emelyanov 		.data		= &ip6_frags.secret_interval,
6638d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
6648d8354d2SPavel Emelyanov 		.mode		= 0644,
6656d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
6668d8354d2SPavel Emelyanov 	},
6678d8354d2SPavel Emelyanov 	{ }
6688d8354d2SPavel Emelyanov };
6697d460db9SDaniel Lezcano 
6702c8c1e72SAlexey Dobriyan static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
6718d8354d2SPavel Emelyanov {
672e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
6738d8354d2SPavel Emelyanov 	struct ctl_table_header *hdr;
6748d8354d2SPavel Emelyanov 
6750a64b4b8SPavel Emelyanov 	table = ip6_frags_ns_ctl_table;
67609ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net)) {
6770a64b4b8SPavel Emelyanov 		table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
678e4a2d5c2SPavel Emelyanov 		if (table == NULL)
679e4a2d5c2SPavel Emelyanov 			goto err_alloc;
680e4a2d5c2SPavel Emelyanov 
681e31e0bdcSPavel Emelyanov 		table[0].data = &net->ipv6.frags.high_thresh;
682e31e0bdcSPavel Emelyanov 		table[1].data = &net->ipv6.frags.low_thresh;
683b2fd5321SPavel Emelyanov 		table[2].data = &net->ipv6.frags.timeout;
684e4a2d5c2SPavel Emelyanov 	}
685e4a2d5c2SPavel Emelyanov 
686e4a2d5c2SPavel Emelyanov 	hdr = register_net_sysctl_table(net, net_ipv6_ctl_path, table);
687e4a2d5c2SPavel Emelyanov 	if (hdr == NULL)
688e4a2d5c2SPavel Emelyanov 		goto err_reg;
689e4a2d5c2SPavel Emelyanov 
690e4a2d5c2SPavel Emelyanov 	net->ipv6.sysctl.frags_hdr = hdr;
691e4a2d5c2SPavel Emelyanov 	return 0;
692e4a2d5c2SPavel Emelyanov 
693e4a2d5c2SPavel Emelyanov err_reg:
69409ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net))
695e4a2d5c2SPavel Emelyanov 		kfree(table);
696e4a2d5c2SPavel Emelyanov err_alloc:
697e4a2d5c2SPavel Emelyanov 	return -ENOMEM;
698e4a2d5c2SPavel Emelyanov }
699e4a2d5c2SPavel Emelyanov 
7002c8c1e72SAlexey Dobriyan static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
701e4a2d5c2SPavel Emelyanov {
702e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
703e4a2d5c2SPavel Emelyanov 
704e4a2d5c2SPavel Emelyanov 	table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
705e4a2d5c2SPavel Emelyanov 	unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
7063705e11aSYang Hongyang 	if (!net_eq(net, &init_net))
707e4a2d5c2SPavel Emelyanov 		kfree(table);
7088d8354d2SPavel Emelyanov }
7097d291ebbSPavel Emelyanov 
7107d291ebbSPavel Emelyanov static struct ctl_table_header *ip6_ctl_header;
7117d291ebbSPavel Emelyanov 
7127d291ebbSPavel Emelyanov static int ip6_frags_sysctl_register(void)
7137d291ebbSPavel Emelyanov {
7147d291ebbSPavel Emelyanov 	ip6_ctl_header = register_net_sysctl_rotable(net_ipv6_ctl_path,
7157d291ebbSPavel Emelyanov 			ip6_frags_ctl_table);
7167d291ebbSPavel Emelyanov 	return ip6_ctl_header == NULL ? -ENOMEM : 0;
7177d291ebbSPavel Emelyanov }
7187d291ebbSPavel Emelyanov 
7197d291ebbSPavel Emelyanov static void ip6_frags_sysctl_unregister(void)
7207d291ebbSPavel Emelyanov {
7217d291ebbSPavel Emelyanov 	unregister_net_sysctl_table(ip6_ctl_header);
7227d291ebbSPavel Emelyanov }
7238d8354d2SPavel Emelyanov #else
7240a64b4b8SPavel Emelyanov static inline int ip6_frags_ns_sysctl_register(struct net *net)
7258d8354d2SPavel Emelyanov {
7268d8354d2SPavel Emelyanov 	return 0;
7278d8354d2SPavel Emelyanov }
728e4a2d5c2SPavel Emelyanov 
7290a64b4b8SPavel Emelyanov static inline void ip6_frags_ns_sysctl_unregister(struct net *net)
730e4a2d5c2SPavel Emelyanov {
731e4a2d5c2SPavel Emelyanov }
7327d291ebbSPavel Emelyanov 
7337d291ebbSPavel Emelyanov static inline int ip6_frags_sysctl_register(void)
7347d291ebbSPavel Emelyanov {
7357d291ebbSPavel Emelyanov 	return 0;
7367d291ebbSPavel Emelyanov }
7377d291ebbSPavel Emelyanov 
7387d291ebbSPavel Emelyanov static inline void ip6_frags_sysctl_unregister(void)
7397d291ebbSPavel Emelyanov {
7407d291ebbSPavel Emelyanov }
7418d8354d2SPavel Emelyanov #endif
7428d8354d2SPavel Emelyanov 
7432c8c1e72SAlexey Dobriyan static int __net_init ipv6_frags_init_net(struct net *net)
7448d8354d2SPavel Emelyanov {
7457c070aa9SShan Wei 	net->ipv6.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
7467c070aa9SShan Wei 	net->ipv6.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
747b2fd5321SPavel Emelyanov 	net->ipv6.frags.timeout = IPV6_FRAG_TIMEOUT;
7488d8354d2SPavel Emelyanov 
749e5a2bb84SPavel Emelyanov 	inet_frags_init_net(&net->ipv6.frags);
750e5a2bb84SPavel Emelyanov 
7510a64b4b8SPavel Emelyanov 	return ip6_frags_ns_sysctl_register(net);
752e71e0349SDaniel Lezcano }
753e71e0349SDaniel Lezcano 
7542c8c1e72SAlexey Dobriyan static void __net_exit ipv6_frags_exit_net(struct net *net)
75581566e83SPavel Emelyanov {
7560a64b4b8SPavel Emelyanov 	ip6_frags_ns_sysctl_unregister(net);
75781566e83SPavel Emelyanov 	inet_frags_exit_net(&net->ipv6.frags, &ip6_frags);
75881566e83SPavel Emelyanov }
75981566e83SPavel Emelyanov 
76081566e83SPavel Emelyanov static struct pernet_operations ip6_frags_ops = {
76181566e83SPavel Emelyanov 	.init = ipv6_frags_init_net,
76281566e83SPavel Emelyanov 	.exit = ipv6_frags_exit_net,
76381566e83SPavel Emelyanov };
76481566e83SPavel Emelyanov 
765853cbbaaSDaniel Lezcano int __init ipv6_frag_init(void)
7661da177e4SLinus Torvalds {
767853cbbaaSDaniel Lezcano 	int ret;
7681da177e4SLinus Torvalds 
769853cbbaaSDaniel Lezcano 	ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
770853cbbaaSDaniel Lezcano 	if (ret)
771853cbbaaSDaniel Lezcano 		goto out;
772e71e0349SDaniel Lezcano 
7737d291ebbSPavel Emelyanov 	ret = ip6_frags_sysctl_register();
7747d291ebbSPavel Emelyanov 	if (ret)
7757d291ebbSPavel Emelyanov 		goto err_sysctl;
7767d291ebbSPavel Emelyanov 
7770002c630SPavel Emelyanov 	ret = register_pernet_subsys(&ip6_frags_ops);
7780002c630SPavel Emelyanov 	if (ret)
7790002c630SPavel Emelyanov 		goto err_pernet;
7808d8354d2SPavel Emelyanov 
781321a3a99SPavel Emelyanov 	ip6_frags.hashfn = ip6_hashfn;
782c6fda282SPavel Emelyanov 	ip6_frags.constructor = ip6_frag_init;
783c9547709SPavel Emelyanov 	ip6_frags.destructor = NULL;
7841e4b8287SPavel Emelyanov 	ip6_frags.skb_free = NULL;
7851e4b8287SPavel Emelyanov 	ip6_frags.qsize = sizeof(struct frag_queue);
786abd6523dSPavel Emelyanov 	ip6_frags.match = ip6_frag_match;
787e521db9dSPavel Emelyanov 	ip6_frags.frag_expire = ip6_frag_expire;
7883b4bc4a2SPavel Emelyanov 	ip6_frags.secret_interval = 10 * 60 * HZ;
7897eb95156SPavel Emelyanov 	inet_frags_init(&ip6_frags);
790853cbbaaSDaniel Lezcano out:
791853cbbaaSDaniel Lezcano 	return ret;
7920002c630SPavel Emelyanov 
7930002c630SPavel Emelyanov err_pernet:
7947d291ebbSPavel Emelyanov 	ip6_frags_sysctl_unregister();
7957d291ebbSPavel Emelyanov err_sysctl:
7960002c630SPavel Emelyanov 	inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
7970002c630SPavel Emelyanov 	goto out;
798853cbbaaSDaniel Lezcano }
799853cbbaaSDaniel Lezcano 
800853cbbaaSDaniel Lezcano void ipv6_frag_exit(void)
801853cbbaaSDaniel Lezcano {
802853cbbaaSDaniel Lezcano 	inet_frags_fini(&ip6_frags);
8037d291ebbSPavel Emelyanov 	ip6_frags_sysctl_unregister();
80481566e83SPavel Emelyanov 	unregister_pernet_subsys(&ip6_frags_ops);
805853cbbaaSDaniel Lezcano 	inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
8061da177e4SLinus Torvalds }
807