xref: /openbmc/linux/net/ipv6/reassembly.c (revision ae7352d3)
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>
6070b095c8SFlorian Westphal #include <net/ipv6_frag.h>
61eec2e618SHannes Frederic Sowa #include <net/inet_ecn.h>
621da177e4SLinus Torvalds 
63d4ad4d22SNikolay Aleksandrov static const char ip6_frag_cache_name[] = "ip6-frags";
64d4ad4d22SNikolay Aleksandrov 
65fc08c258SFabian Frederick static u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
66eec2e618SHannes Frederic Sowa {
67eec2e618SHannes Frederic Sowa 	return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
68eec2e618SHannes Frederic Sowa }
691da177e4SLinus Torvalds 
707eb95156SPavel Emelyanov static struct inet_frags ip6_frags;
711da177e4SLinus Torvalds 
72d4289fccSPeter Oskolkov static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb,
73d4289fccSPeter Oskolkov 			  struct sk_buff *prev_tail, struct net_device *dev);
74f61944efSHerbert Xu 
7578802011SKees Cook static void ip6_frag_expire(struct timer_list *t)
76b836c99fSAmerigo Wang {
7778802011SKees Cook 	struct inet_frag_queue *frag = from_timer(frag, t, timer);
78b836c99fSAmerigo Wang 	struct frag_queue *fq;
79b836c99fSAmerigo Wang 
8078802011SKees Cook 	fq = container_of(frag, struct frag_queue, q);
81b836c99fSAmerigo Wang 
82a39aca67SEric Dumazet 	ip6frag_expire_frag_queue(fq->q.fqdir->net, fq);
831da177e4SLinus Torvalds }
841da177e4SLinus Torvalds 
85fc08c258SFabian Frederick static struct frag_queue *
86648700f7SEric Dumazet fq_find(struct net *net, __be32 id, const struct ipv6hdr *hdr, int iif)
871da177e4SLinus Torvalds {
88648700f7SEric Dumazet 	struct frag_v6_compare_key key = {
89648700f7SEric Dumazet 		.id = id,
90648700f7SEric Dumazet 		.saddr = hdr->saddr,
91648700f7SEric Dumazet 		.daddr = hdr->daddr,
92648700f7SEric Dumazet 		.user = IP6_DEFRAG_LOCAL_DELIVER,
93648700f7SEric Dumazet 		.iif = iif,
94648700f7SEric Dumazet 	};
95c6fda282SPavel Emelyanov 	struct inet_frag_queue *q;
961da177e4SLinus Torvalds 
97648700f7SEric Dumazet 	if (!(ipv6_addr_type(&hdr->daddr) & (IPV6_ADDR_MULTICAST |
98648700f7SEric Dumazet 					    IPV6_ADDR_LINKLOCAL)))
99648700f7SEric Dumazet 		key.iif = 0;
1009a375803SPavel Emelyanov 
1014907abc6SEric Dumazet 	q = inet_frag_find(net->ipv6.fqdir, &key);
1022d44ed22SEric Dumazet 	if (!q)
1039546377cSShan Wei 		return NULL;
1042d44ed22SEric Dumazet 
105c6fda282SPavel Emelyanov 	return container_of(q, struct frag_queue, q);
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds 
108f61944efSHerbert Xu static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
109415787d7SEric Dumazet 			  struct frag_hdr *fhdr, int nhoff,
110415787d7SEric Dumazet 			  u32 *prob_offset)
1111da177e4SLinus Torvalds {
112adf30907SEric Dumazet 	struct net *net = dev_net(skb_dst(skb)->dev);
113d4289fccSPeter Oskolkov 	int offset, end, fragsize;
114d4289fccSPeter Oskolkov 	struct sk_buff *prev_tail;
115d4289fccSPeter Oskolkov 	struct net_device *dev;
116d4289fccSPeter Oskolkov 	int err = -ENOENT;
117eec2e618SHannes Frederic Sowa 	u8 ecn;
1181da177e4SLinus Torvalds 
11906aa8b8aSNikolay Aleksandrov 	if (fq->q.flags & INET_FRAG_COMPLETE)
1201da177e4SLinus Torvalds 		goto err;
1211da177e4SLinus Torvalds 
122d4289fccSPeter Oskolkov 	err = -EINVAL;
1231da177e4SLinus Torvalds 	offset = ntohs(fhdr->frag_off) & ~0x7;
1240660e03fSArnaldo Carvalho de Melo 	end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
1250660e03fSArnaldo Carvalho de Melo 			((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	if ((unsigned int)end > IPV6_MAXPLEN) {
128415787d7SEric Dumazet 		*prob_offset = (u8 *)&fhdr->frag_off - skb_network_header(skb);
129d4289fccSPeter Oskolkov 		/* note that if prob_offset is set, the skb is freed elsewhere,
130d4289fccSPeter Oskolkov 		 * we do not free it here.
131d4289fccSPeter Oskolkov 		 */
132f61944efSHerbert Xu 		return -1;
1331da177e4SLinus Torvalds 	}
1341da177e4SLinus Torvalds 
135eec2e618SHannes Frederic Sowa 	ecn = ip6_frag_ecn(ipv6_hdr(skb));
136eec2e618SHannes Frederic Sowa 
137d56f90a7SArnaldo Carvalho de Melo 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
138d56f90a7SArnaldo Carvalho de Melo 		const unsigned char *nh = skb_network_header(skb);
1391da177e4SLinus Torvalds 		skb->csum = csum_sub(skb->csum,
140d56f90a7SArnaldo Carvalho de Melo 				     csum_partial(nh, (u8 *)(fhdr + 1) - nh,
141d56f90a7SArnaldo Carvalho de Melo 						  0));
142d56f90a7SArnaldo Carvalho de Melo 	}
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	/* Is this the final fragment? */
1451da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(IP6_MF))) {
1461da177e4SLinus Torvalds 		/* If we already have some bits beyond end
1471da177e4SLinus Torvalds 		 * or have different end, the segment is corrupted.
1481da177e4SLinus Torvalds 		 */
1495ab11c98SPavel Emelyanov 		if (end < fq->q.len ||
15006aa8b8aSNikolay Aleksandrov 		    ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len))
1512475f59cSPeter Oskolkov 			goto discard_fq;
15206aa8b8aSNikolay Aleksandrov 		fq->q.flags |= INET_FRAG_LAST_IN;
1535ab11c98SPavel Emelyanov 		fq->q.len = end;
1541da177e4SLinus Torvalds 	} else {
1551da177e4SLinus Torvalds 		/* Check if the fragment is rounded to 8 bytes.
1561da177e4SLinus Torvalds 		 * Required by the RFC.
1571da177e4SLinus Torvalds 		 */
1581da177e4SLinus Torvalds 		if (end & 0x7) {
1591da177e4SLinus Torvalds 			/* RFC2460 says always send parameter problem in
1601da177e4SLinus Torvalds 			 * this case. -DaveM
1611da177e4SLinus Torvalds 			 */
162415787d7SEric Dumazet 			*prob_offset = offsetof(struct ipv6hdr, payload_len);
163f61944efSHerbert Xu 			return -1;
1641da177e4SLinus Torvalds 		}
1655ab11c98SPavel Emelyanov 		if (end > fq->q.len) {
1661da177e4SLinus Torvalds 			/* Some bits beyond end -> corruption. */
16706aa8b8aSNikolay Aleksandrov 			if (fq->q.flags & INET_FRAG_LAST_IN)
1682475f59cSPeter Oskolkov 				goto discard_fq;
1695ab11c98SPavel Emelyanov 			fq->q.len = end;
1701da177e4SLinus Torvalds 		}
1711da177e4SLinus Torvalds 	}
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	if (end == offset)
1742475f59cSPeter Oskolkov 		goto discard_fq;
1751da177e4SLinus Torvalds 
176d4289fccSPeter Oskolkov 	err = -ENOMEM;
1771da177e4SLinus Torvalds 	/* Point into the IP datagram 'data' part. */
1781da177e4SLinus Torvalds 	if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
1792475f59cSPeter Oskolkov 		goto discard_fq;
18042ca89c1SStephen Hemminger 
181d4289fccSPeter Oskolkov 	err = pskb_trim_rcsum(skb, end - offset);
182d4289fccSPeter Oskolkov 	if (err)
1832475f59cSPeter Oskolkov 		goto discard_fq;
1841da177e4SLinus Torvalds 
185d4289fccSPeter Oskolkov 	/* Note : skb->rbnode and skb->dev share the same location. */
186219badfaSEric Dumazet 	dev = skb->dev;
187219badfaSEric Dumazet 	/* Makes sure compiler wont do silly aliasing games */
188219badfaSEric Dumazet 	barrier();
1891da177e4SLinus Torvalds 
190d4289fccSPeter Oskolkov 	prev_tail = fq->q.fragments_tail;
191d4289fccSPeter Oskolkov 	err = inet_frag_queue_insert(&fq->q, skb, offset, end);
192d4289fccSPeter Oskolkov 	if (err)
193d4289fccSPeter Oskolkov 		goto insert_error;
194d4289fccSPeter Oskolkov 
195d4289fccSPeter Oskolkov 	if (dev)
196d4289fccSPeter Oskolkov 		fq->iif = dev->ifindex;
1971da177e4SLinus Torvalds 
1985ab11c98SPavel Emelyanov 	fq->q.stamp = skb->tstamp;
1995ab11c98SPavel Emelyanov 	fq->q.meat += skb->len;
200eec2e618SHannes Frederic Sowa 	fq->ecn |= ecn;
2016ce3b4dcSEric Dumazet 	add_frag_mem_limit(fq->q.fqdir, skb->truesize);
2021da177e4SLinus Torvalds 
203dbd1759eSWillem de Bruijn 	fragsize = -skb_network_offset(skb) + skb->len;
204dbd1759eSWillem de Bruijn 	if (fragsize > fq->q.max_size)
205dbd1759eSWillem de Bruijn 		fq->q.max_size = fragsize;
206dbd1759eSWillem de Bruijn 
2071da177e4SLinus Torvalds 	/* The first fragment.
2081da177e4SLinus Torvalds 	 * nhoffset is obtained from the first fragment, of course.
2091da177e4SLinus Torvalds 	 */
2101da177e4SLinus Torvalds 	if (offset == 0) {
2111da177e4SLinus Torvalds 		fq->nhoffset = nhoff;
21206aa8b8aSNikolay Aleksandrov 		fq->q.flags |= INET_FRAG_FIRST_IN;
2131da177e4SLinus Torvalds 	}
214f61944efSHerbert Xu 
21506aa8b8aSNikolay Aleksandrov 	if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
21697599dc7SEric Dumazet 	    fq->q.meat == fq->q.len) {
21797599dc7SEric Dumazet 		unsigned long orefdst = skb->_skb_refdst;
218f61944efSHerbert Xu 
21997599dc7SEric Dumazet 		skb->_skb_refdst = 0UL;
220d4289fccSPeter Oskolkov 		err = ip6_frag_reasm(fq, skb, prev_tail, dev);
22197599dc7SEric Dumazet 		skb->_skb_refdst = orefdst;
222d4289fccSPeter Oskolkov 		return err;
22397599dc7SEric Dumazet 	}
22497599dc7SEric Dumazet 
22597599dc7SEric Dumazet 	skb_dst_drop(skb);
226d4289fccSPeter Oskolkov 	return -EINPROGRESS;
2271da177e4SLinus Torvalds 
228d4289fccSPeter Oskolkov insert_error:
229d4289fccSPeter Oskolkov 	if (err == IPFRAG_DUP) {
230d4289fccSPeter Oskolkov 		kfree_skb(skb);
231d4289fccSPeter Oskolkov 		return -EINVAL;
232d4289fccSPeter Oskolkov 	}
233d4289fccSPeter Oskolkov 	err = -EINVAL;
234d4289fccSPeter Oskolkov 	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
235d4289fccSPeter Oskolkov 			IPSTATS_MIB_REASM_OVERLAPS);
23670789d70SNicolas Dichtel discard_fq:
237093ba729SEric Dumazet 	inet_frag_kill(&fq->q);
2381d015503SEric Dumazet 	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
2393bd653c8SDenis V. Lunev 			IPSTATS_MIB_REASMFAILS);
240d4289fccSPeter Oskolkov err:
2411da177e4SLinus Torvalds 	kfree_skb(skb);
242d4289fccSPeter Oskolkov 	return err;
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds /*
2461da177e4SLinus Torvalds  *	Check if this packet is complete.
2471da177e4SLinus Torvalds  *
2481da177e4SLinus Torvalds  *	It is called with locked fq, and caller must check that
2491da177e4SLinus Torvalds  *	queue is eligible for reassembly i.e. it is not COMPLETE,
2501da177e4SLinus Torvalds  *	the last and the first frames arrived and all the bits are here.
2511da177e4SLinus Torvalds  */
252d4289fccSPeter Oskolkov static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb,
253d4289fccSPeter Oskolkov 			  struct sk_buff *prev_tail, struct net_device *dev)
2541da177e4SLinus Torvalds {
255a39aca67SEric Dumazet 	struct net *net = fq->q.fqdir->net;
2561da177e4SLinus Torvalds 	unsigned int nhoff;
257d4289fccSPeter Oskolkov 	void *reasm_data;
258d4289fccSPeter Oskolkov 	int payload_len;
259eec2e618SHannes Frederic Sowa 	u8 ecn;
2601da177e4SLinus Torvalds 
261093ba729SEric Dumazet 	inet_frag_kill(&fq->q);
2621da177e4SLinus Torvalds 
263eec2e618SHannes Frederic Sowa 	ecn = ip_frag_ecn_table[fq->ecn];
264eec2e618SHannes Frederic Sowa 	if (unlikely(ecn == 0xff))
265eec2e618SHannes Frederic Sowa 		goto out_fail;
266eec2e618SHannes Frederic Sowa 
267d4289fccSPeter Oskolkov 	reasm_data = inet_frag_reasm_prepare(&fq->q, skb, prev_tail);
268d4289fccSPeter Oskolkov 	if (!reasm_data)
269f61944efSHerbert Xu 		goto out_oom;
270f61944efSHerbert Xu 
271d4289fccSPeter Oskolkov 	payload_len = ((skb->data - skb_network_header(skb)) -
2725ab11c98SPavel Emelyanov 		       sizeof(struct ipv6hdr) + fq->q.len -
273d56f90a7SArnaldo Carvalho de Melo 		       sizeof(struct frag_hdr));
2741da177e4SLinus Torvalds 	if (payload_len > IPV6_MAXPLEN)
2751da177e4SLinus Torvalds 		goto out_oversize;
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	/* We have to remove fragment header from datagram and to relocate
2781da177e4SLinus Torvalds 	 * header in order to calculate ICV correctly. */
2791da177e4SLinus Torvalds 	nhoff = fq->nhoffset;
280d4289fccSPeter Oskolkov 	skb_network_header(skb)[nhoff] = skb_transport_header(skb)[0];
281d4289fccSPeter Oskolkov 	memmove(skb->head + sizeof(struct frag_hdr), skb->head,
282d4289fccSPeter Oskolkov 		(skb->data - skb->head) - sizeof(struct frag_hdr));
283d4289fccSPeter Oskolkov 	if (skb_mac_header_was_set(skb))
284d4289fccSPeter Oskolkov 		skb->mac_header += sizeof(struct frag_hdr);
285d4289fccSPeter Oskolkov 	skb->network_header += sizeof(struct frag_hdr);
2861da177e4SLinus Torvalds 
287d4289fccSPeter Oskolkov 	skb_reset_transport_header(skb);
2881da177e4SLinus Torvalds 
289d4289fccSPeter Oskolkov 	inet_frag_reasm_finish(&fq->q, skb, reasm_data);
290ec16439eSEric Dumazet 
291d4289fccSPeter Oskolkov 	skb->dev = dev;
292d4289fccSPeter Oskolkov 	ipv6_hdr(skb)->payload_len = htons(payload_len);
293d4289fccSPeter Oskolkov 	ipv6_change_dsfield(ipv6_hdr(skb), 0xff, ecn);
294d4289fccSPeter Oskolkov 	IP6CB(skb)->nhoff = nhoff;
295d4289fccSPeter Oskolkov 	IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
296d4289fccSPeter Oskolkov 	IP6CB(skb)->frag_max_size = fq->q.max_size;
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	/* Yes, and fold redundant checksum back. 8) */
299d4289fccSPeter Oskolkov 	skb_postpush_rcsum(skb, skb_network_header(skb),
300d4289fccSPeter Oskolkov 			   skb_network_header_len(skb));
3011da177e4SLinus Torvalds 
302a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
3031d015503SEric Dumazet 	__IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS);
304a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
305fa0f5273SPeter Oskolkov 	fq->q.rb_fragments = RB_ROOT;
306d6bebca9SChangli Gao 	fq->q.fragments_tail = NULL;
307d4289fccSPeter Oskolkov 	fq->q.last_run_head = NULL;
3081da177e4SLinus Torvalds 	return 1;
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds out_oversize:
311e87cc472SJoe Perches 	net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len);
3121da177e4SLinus Torvalds 	goto out_fail;
3131da177e4SLinus Torvalds out_oom:
314e87cc472SJoe Perches 	net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n");
3151da177e4SLinus Torvalds out_fail:
316a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
3171d015503SEric Dumazet 	__IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS);
318a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
3192475f59cSPeter Oskolkov 	inet_frag_kill(&fq->q);
3201da177e4SLinus Torvalds 	return -1;
3211da177e4SLinus Torvalds }
3221da177e4SLinus Torvalds 
323e5bbef20SHerbert Xu static int ipv6_frag_rcv(struct sk_buff *skb)
3241da177e4SLinus Torvalds {
3251da177e4SLinus Torvalds 	struct frag_hdr *fhdr;
3261da177e4SLinus Torvalds 	struct frag_queue *fq;
327b71d1d42SEric Dumazet 	const struct ipv6hdr *hdr = ipv6_hdr(skb);
328adf30907SEric Dumazet 	struct net *net = dev_net(skb_dst(skb)->dev);
329648700f7SEric Dumazet 	int iif;
3301da177e4SLinus Torvalds 
331f46078cfSHannes Frederic Sowa 	if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED)
332f46078cfSHannes Frederic Sowa 		goto fail_hdr;
333f46078cfSHannes Frederic Sowa 
3341d015503SEric Dumazet 	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS);
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	/* Jumbo payload inhibits frag. header */
33798b3377cSDenis V. Lunev 	if (hdr->payload_len == 0)
33898b3377cSDenis V. Lunev 		goto fail_hdr;
33998b3377cSDenis V. Lunev 
340ea2ae17dSArnaldo Carvalho de Melo 	if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
34198b3377cSDenis V. Lunev 				 sizeof(struct frag_hdr))))
34298b3377cSDenis V. Lunev 		goto fail_hdr;
3431da177e4SLinus Torvalds 
3440660e03fSArnaldo Carvalho de Melo 	hdr = ipv6_hdr(skb);
3459c70220bSArnaldo Carvalho de Melo 	fhdr = (struct frag_hdr *)skb_transport_header(skb);
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(0xFFF9))) {
3481da177e4SLinus Torvalds 		/* It is not a fragmented frame */
349b0e380b1SArnaldo Carvalho de Melo 		skb->transport_header += sizeof(struct frag_hdr);
3501d015503SEric Dumazet 		__IP6_INC_STATS(net,
351adf30907SEric Dumazet 				ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS);
3521da177e4SLinus Torvalds 
353d56f90a7SArnaldo Carvalho de Melo 		IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
354f46078cfSHannes Frederic Sowa 		IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
3551da177e4SLinus Torvalds 		return 1;
3561da177e4SLinus Torvalds 	}
3571da177e4SLinus Torvalds 
358648700f7SEric Dumazet 	iif = skb->dev ? skb->dev->ifindex : 0;
359648700f7SEric Dumazet 	fq = fq_find(net, fhdr->identification, hdr, iif);
36053b24b8fSIan Morris 	if (fq) {
361415787d7SEric Dumazet 		u32 prob_offset = 0;
362f61944efSHerbert Xu 		int ret;
3631da177e4SLinus Torvalds 
3645ab11c98SPavel Emelyanov 		spin_lock(&fq->q.lock);
3651da177e4SLinus Torvalds 
366648700f7SEric Dumazet 		fq->iif = iif;
367415787d7SEric Dumazet 		ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff,
368415787d7SEric Dumazet 				     &prob_offset);
3691da177e4SLinus Torvalds 
3705ab11c98SPavel Emelyanov 		spin_unlock(&fq->q.lock);
371093ba729SEric Dumazet 		inet_frag_put(&fq->q);
372415787d7SEric Dumazet 		if (prob_offset) {
373415787d7SEric Dumazet 			__IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev),
374415787d7SEric Dumazet 					IPSTATS_MIB_INHDRERRORS);
375d4289fccSPeter Oskolkov 			/* icmpv6_param_prob() calls kfree_skb(skb) */
376415787d7SEric Dumazet 			icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, prob_offset);
377415787d7SEric Dumazet 		}
3781da177e4SLinus Torvalds 		return ret;
3791da177e4SLinus Torvalds 	}
3801da177e4SLinus Torvalds 
3811d015503SEric Dumazet 	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS);
3821da177e4SLinus Torvalds 	kfree_skb(skb);
3831da177e4SLinus Torvalds 	return -1;
38498b3377cSDenis V. Lunev 
38598b3377cSDenis V. Lunev fail_hdr:
386bdb7cc64SStephen Suryaputra 	__IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev),
387d2373862SNikolay Aleksandrov 			IPSTATS_MIB_INHDRERRORS);
38898b3377cSDenis V. Lunev 	icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
38998b3377cSDenis V. Lunev 	return -1;
3901da177e4SLinus Torvalds }
3911da177e4SLinus Torvalds 
392cc24becaSIan Morris static const struct inet6_protocol frag_protocol = {
3931da177e4SLinus Torvalds 	.handler	=	ipv6_frag_rcv,
3941da177e4SLinus Torvalds 	.flags		=	INET6_PROTO_NOPOLICY,
3951da177e4SLinus Torvalds };
3961da177e4SLinus Torvalds 
3978d8354d2SPavel Emelyanov #ifdef CONFIG_SYSCTL
3981bab4c75SNikolay Aleksandrov 
3990a64b4b8SPavel Emelyanov static struct ctl_table ip6_frags_ns_ctl_table[] = {
400e71e0349SDaniel Lezcano 	{
4018d8354d2SPavel Emelyanov 		.procname	= "ip6frag_high_thresh",
4023e67f106SEric Dumazet 		.maxlen		= sizeof(unsigned long),
4038d8354d2SPavel Emelyanov 		.mode		= 0644,
4043e67f106SEric Dumazet 		.proc_handler	= proc_doulongvec_minmax,
4058d8354d2SPavel Emelyanov 	},
4068d8354d2SPavel Emelyanov 	{
4078d8354d2SPavel Emelyanov 		.procname	= "ip6frag_low_thresh",
4083e67f106SEric Dumazet 		.maxlen		= sizeof(unsigned long),
4098d8354d2SPavel Emelyanov 		.mode		= 0644,
4106e00f7ddSEric Dumazet 		.proc_handler	= proc_doulongvec_minmax,
4118d8354d2SPavel Emelyanov 	},
4128d8354d2SPavel Emelyanov 	{
4138d8354d2SPavel Emelyanov 		.procname	= "ip6frag_time",
4148d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
4158d8354d2SPavel Emelyanov 		.mode		= 0644,
4166d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
4178d8354d2SPavel Emelyanov 	},
4187d291ebbSPavel Emelyanov 	{ }
4197d291ebbSPavel Emelyanov };
4207d291ebbSPavel Emelyanov 
421e3a57d18SFlorian Westphal /* secret interval has been deprecated */
422e3a57d18SFlorian Westphal static int ip6_frags_secret_interval_unused;
4237d291ebbSPavel Emelyanov static struct ctl_table ip6_frags_ctl_table[] = {
4248d8354d2SPavel Emelyanov 	{
4258d8354d2SPavel Emelyanov 		.procname	= "ip6frag_secret_interval",
426e3a57d18SFlorian Westphal 		.data		= &ip6_frags_secret_interval_unused,
4278d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
4288d8354d2SPavel Emelyanov 		.mode		= 0644,
4296d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
4308d8354d2SPavel Emelyanov 	},
4318d8354d2SPavel Emelyanov 	{ }
4328d8354d2SPavel Emelyanov };
4337d460db9SDaniel Lezcano 
4342c8c1e72SAlexey Dobriyan static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
4358d8354d2SPavel Emelyanov {
436e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
4378d8354d2SPavel Emelyanov 	struct ctl_table_header *hdr;
4388d8354d2SPavel Emelyanov 
4390a64b4b8SPavel Emelyanov 	table = ip6_frags_ns_ctl_table;
44009ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net)) {
4410a64b4b8SPavel Emelyanov 		table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
44263159f29SIan Morris 		if (!table)
443e4a2d5c2SPavel Emelyanov 			goto err_alloc;
444e4a2d5c2SPavel Emelyanov 
4458668d0e2SEric Dumazet 	}
4464907abc6SEric Dumazet 	table[0].data	= &net->ipv6.fqdir->high_thresh;
4474907abc6SEric Dumazet 	table[0].extra1	= &net->ipv6.fqdir->low_thresh;
4484907abc6SEric Dumazet 	table[1].data	= &net->ipv6.fqdir->low_thresh;
4494907abc6SEric Dumazet 	table[1].extra2	= &net->ipv6.fqdir->high_thresh;
4504907abc6SEric Dumazet 	table[2].data	= &net->ipv6.fqdir->timeout;
451e4a2d5c2SPavel Emelyanov 
452ec8f23ceSEric W. Biederman 	hdr = register_net_sysctl(net, "net/ipv6", table);
45363159f29SIan Morris 	if (!hdr)
454e4a2d5c2SPavel Emelyanov 		goto err_reg;
455e4a2d5c2SPavel Emelyanov 
456e4a2d5c2SPavel Emelyanov 	net->ipv6.sysctl.frags_hdr = hdr;
457e4a2d5c2SPavel Emelyanov 	return 0;
458e4a2d5c2SPavel Emelyanov 
459e4a2d5c2SPavel Emelyanov err_reg:
46009ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net))
461e4a2d5c2SPavel Emelyanov 		kfree(table);
462e4a2d5c2SPavel Emelyanov err_alloc:
463e4a2d5c2SPavel Emelyanov 	return -ENOMEM;
464e4a2d5c2SPavel Emelyanov }
465e4a2d5c2SPavel Emelyanov 
4662c8c1e72SAlexey Dobriyan static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
467e4a2d5c2SPavel Emelyanov {
468e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
469e4a2d5c2SPavel Emelyanov 
470e4a2d5c2SPavel Emelyanov 	table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
471e4a2d5c2SPavel Emelyanov 	unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
4723705e11aSYang Hongyang 	if (!net_eq(net, &init_net))
473e4a2d5c2SPavel Emelyanov 		kfree(table);
4748d8354d2SPavel Emelyanov }
4757d291ebbSPavel Emelyanov 
4767d291ebbSPavel Emelyanov static struct ctl_table_header *ip6_ctl_header;
4777d291ebbSPavel Emelyanov 
4787d291ebbSPavel Emelyanov static int ip6_frags_sysctl_register(void)
4797d291ebbSPavel Emelyanov {
48043444757SEric W. Biederman 	ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6",
4817d291ebbSPavel Emelyanov 			ip6_frags_ctl_table);
4827d291ebbSPavel Emelyanov 	return ip6_ctl_header == NULL ? -ENOMEM : 0;
4837d291ebbSPavel Emelyanov }
4847d291ebbSPavel Emelyanov 
4857d291ebbSPavel Emelyanov static void ip6_frags_sysctl_unregister(void)
4867d291ebbSPavel Emelyanov {
4877d291ebbSPavel Emelyanov 	unregister_net_sysctl_table(ip6_ctl_header);
4887d291ebbSPavel Emelyanov }
4898d8354d2SPavel Emelyanov #else
490fc08c258SFabian Frederick static int ip6_frags_ns_sysctl_register(struct net *net)
4918d8354d2SPavel Emelyanov {
4928d8354d2SPavel Emelyanov 	return 0;
4938d8354d2SPavel Emelyanov }
494e4a2d5c2SPavel Emelyanov 
495fc08c258SFabian Frederick static void ip6_frags_ns_sysctl_unregister(struct net *net)
496e4a2d5c2SPavel Emelyanov {
497e4a2d5c2SPavel Emelyanov }
4987d291ebbSPavel Emelyanov 
499fc08c258SFabian Frederick static int ip6_frags_sysctl_register(void)
5007d291ebbSPavel Emelyanov {
5017d291ebbSPavel Emelyanov 	return 0;
5027d291ebbSPavel Emelyanov }
5037d291ebbSPavel Emelyanov 
504fc08c258SFabian Frederick static void ip6_frags_sysctl_unregister(void)
5057d291ebbSPavel Emelyanov {
5067d291ebbSPavel Emelyanov }
5078d8354d2SPavel Emelyanov #endif
5088d8354d2SPavel Emelyanov 
5092c8c1e72SAlexey Dobriyan static int __net_init ipv6_frags_init_net(struct net *net)
5108d8354d2SPavel Emelyanov {
511787bea77SEric Dumazet 	int res;
512787bea77SEric Dumazet 
513a39aca67SEric Dumazet 	res = fqdir_init(&net->ipv6.fqdir, &ip6_frags, net);
514787bea77SEric Dumazet 	if (res < 0)
515787bea77SEric Dumazet 		return res;
5165a63643eSJesper Dangaard Brouer 
5174907abc6SEric Dumazet 	net->ipv6.fqdir->high_thresh = IPV6_FRAG_HIGH_THRESH;
5184907abc6SEric Dumazet 	net->ipv6.fqdir->low_thresh = IPV6_FRAG_LOW_THRESH;
5194907abc6SEric Dumazet 	net->ipv6.fqdir->timeout = IPV6_FRAG_TIMEOUT;
5204907abc6SEric Dumazet 
521787bea77SEric Dumazet 	res = ip6_frags_ns_sysctl_register(net);
522787bea77SEric Dumazet 	if (res < 0)
5234907abc6SEric Dumazet 		fqdir_exit(net->ipv6.fqdir);
524787bea77SEric Dumazet 	return res;
525e71e0349SDaniel Lezcano }
526e71e0349SDaniel Lezcano 
5272c8c1e72SAlexey Dobriyan static void __net_exit ipv6_frags_exit_net(struct net *net)
52881566e83SPavel Emelyanov {
5290a64b4b8SPavel Emelyanov 	ip6_frags_ns_sysctl_unregister(net);
5304907abc6SEric Dumazet 	fqdir_exit(net->ipv6.fqdir);
53181566e83SPavel Emelyanov }
53281566e83SPavel Emelyanov 
53381566e83SPavel Emelyanov static struct pernet_operations ip6_frags_ops = {
53481566e83SPavel Emelyanov 	.init = ipv6_frags_init_net,
53581566e83SPavel Emelyanov 	.exit = ipv6_frags_exit_net,
53681566e83SPavel Emelyanov };
53781566e83SPavel Emelyanov 
53870b095c8SFlorian Westphal static const struct rhashtable_params ip6_rhash_params = {
539648700f7SEric Dumazet 	.head_offset		= offsetof(struct inet_frag_queue, node),
54070b095c8SFlorian Westphal 	.hashfn			= ip6frag_key_hashfn,
54170b095c8SFlorian Westphal 	.obj_hashfn		= ip6frag_obj_hashfn,
54270b095c8SFlorian Westphal 	.obj_cmpfn		= ip6frag_obj_cmpfn,
543648700f7SEric Dumazet 	.automatic_shrinking	= true,
544648700f7SEric Dumazet };
545648700f7SEric Dumazet 
546853cbbaaSDaniel Lezcano int __init ipv6_frag_init(void)
5471da177e4SLinus Torvalds {
548853cbbaaSDaniel Lezcano 	int ret;
5491da177e4SLinus Torvalds 
55070b095c8SFlorian Westphal 	ip6_frags.constructor = ip6frag_init;
551c9547709SPavel Emelyanov 	ip6_frags.destructor = NULL;
5521e4b8287SPavel Emelyanov 	ip6_frags.qsize = sizeof(struct frag_queue);
553e521db9dSPavel Emelyanov 	ip6_frags.frag_expire = ip6_frag_expire;
554d4ad4d22SNikolay Aleksandrov 	ip6_frags.frags_cache_name = ip6_frag_cache_name;
555648700f7SEric Dumazet 	ip6_frags.rhash_params = ip6_rhash_params;
556d4ad4d22SNikolay Aleksandrov 	ret = inet_frags_init(&ip6_frags);
557d4ad4d22SNikolay Aleksandrov 	if (ret)
5585b975babSEric Dumazet 		goto out;
5595b975babSEric Dumazet 
5605b975babSEric Dumazet 	ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
5615b975babSEric Dumazet 	if (ret)
5625b975babSEric Dumazet 		goto err_protocol;
5635b975babSEric Dumazet 
5645b975babSEric Dumazet 	ret = ip6_frags_sysctl_register();
5655b975babSEric Dumazet 	if (ret)
5665b975babSEric Dumazet 		goto err_sysctl;
5675b975babSEric Dumazet 
5685b975babSEric Dumazet 	ret = register_pernet_subsys(&ip6_frags_ops);
5695b975babSEric Dumazet 	if (ret)
570d4ad4d22SNikolay Aleksandrov 		goto err_pernet;
5715b975babSEric Dumazet 
572853cbbaaSDaniel Lezcano out:
573853cbbaaSDaniel Lezcano 	return ret;
5740002c630SPavel Emelyanov 
5750002c630SPavel Emelyanov err_pernet:
5767d291ebbSPavel Emelyanov 	ip6_frags_sysctl_unregister();
5777d291ebbSPavel Emelyanov err_sysctl:
5780002c630SPavel Emelyanov 	inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
5795b975babSEric Dumazet err_protocol:
5805b975babSEric Dumazet 	inet_frags_fini(&ip6_frags);
5810002c630SPavel Emelyanov 	goto out;
582853cbbaaSDaniel Lezcano }
583853cbbaaSDaniel Lezcano 
584853cbbaaSDaniel Lezcano void ipv6_frag_exit(void)
585853cbbaaSDaniel Lezcano {
5867d291ebbSPavel Emelyanov 	ip6_frags_sysctl_unregister();
58781566e83SPavel Emelyanov 	unregister_pernet_subsys(&ip6_frags_ops);
588853cbbaaSDaniel Lezcano 	inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
589ae7352d3SEric Dumazet 	inet_frags_fini(&ip6_frags);
5901da177e4SLinus Torvalds }
591