xref: /openbmc/linux/net/ipv6/reassembly.c (revision d5dd8879)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *	IPv6 fragment reassembly
41da177e4SLinus Torvalds  *	Linux INET6 implementation
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *	Authors:
71da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *	Based on: net/ipv4/ip_fragment.c
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds /*
131da177e4SLinus Torvalds  *	Fixes:
141da177e4SLinus Torvalds  *	Andi Kleen	Make it work with multiple hosts.
151da177e4SLinus Torvalds  *			More RFC compliance.
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  *      Horst von Brand Add missing #include <linux/string.h>
181da177e4SLinus Torvalds  *	Alexey Kuznetsov	SMP races, threading, cleanup.
191da177e4SLinus Torvalds  *	Patrick McHardy		LRU queue of frag heads for evictor.
201da177e4SLinus Torvalds  *	Mitsuru KANDA @USAGI	Register inet6_protocol{}.
211da177e4SLinus Torvalds  *	David Stevens and
221da177e4SLinus Torvalds  *	YOSHIFUJI,H. @USAGI	Always remove fragment header to
231da177e4SLinus Torvalds  *				calculate ICV correctly.
241da177e4SLinus Torvalds  */
255a3da1feSHannes Frederic Sowa 
265a3da1feSHannes Frederic Sowa #define pr_fmt(fmt) "IPv6: " fmt
275a3da1feSHannes Frederic Sowa 
281da177e4SLinus Torvalds #include <linux/errno.h>
291da177e4SLinus Torvalds #include <linux/types.h>
301da177e4SLinus Torvalds #include <linux/string.h>
311da177e4SLinus Torvalds #include <linux/socket.h>
321da177e4SLinus Torvalds #include <linux/sockios.h>
331da177e4SLinus Torvalds #include <linux/jiffies.h>
341da177e4SLinus Torvalds #include <linux/net.h>
351da177e4SLinus Torvalds #include <linux/list.h>
361da177e4SLinus Torvalds #include <linux/netdevice.h>
371da177e4SLinus Torvalds #include <linux/in6.h>
381da177e4SLinus Torvalds #include <linux/ipv6.h>
391da177e4SLinus Torvalds #include <linux/icmpv6.h>
401da177e4SLinus Torvalds #include <linux/random.h>
411da177e4SLinus Torvalds #include <linux/jhash.h>
42f61944efSHerbert Xu #include <linux/skbuff.h>
435a0e3ad6STejun Heo #include <linux/slab.h>
44bc3b2d7fSPaul Gortmaker #include <linux/export.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>
5670b095c8SFlorian Westphal #include <net/ipv6_frag.h>
57eec2e618SHannes Frederic Sowa #include <net/inet_ecn.h>
581da177e4SLinus Torvalds 
59d4ad4d22SNikolay Aleksandrov static const char ip6_frag_cache_name[] = "ip6-frags";
60d4ad4d22SNikolay Aleksandrov 
61fc08c258SFabian Frederick static u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
62eec2e618SHannes Frederic Sowa {
63eec2e618SHannes Frederic Sowa 	return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
64eec2e618SHannes Frederic Sowa }
651da177e4SLinus Torvalds 
667eb95156SPavel Emelyanov static struct inet_frags ip6_frags;
671da177e4SLinus Torvalds 
68d4289fccSPeter Oskolkov static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb,
69d4289fccSPeter Oskolkov 			  struct sk_buff *prev_tail, struct net_device *dev);
70f61944efSHerbert Xu 
7178802011SKees Cook static void ip6_frag_expire(struct timer_list *t)
72b836c99fSAmerigo Wang {
7378802011SKees Cook 	struct inet_frag_queue *frag = from_timer(frag, t, timer);
74b836c99fSAmerigo Wang 	struct frag_queue *fq;
75b836c99fSAmerigo Wang 
7678802011SKees Cook 	fq = container_of(frag, struct frag_queue, q);
77b836c99fSAmerigo Wang 
78a39aca67SEric Dumazet 	ip6frag_expire_frag_queue(fq->q.fqdir->net, fq);
791da177e4SLinus Torvalds }
801da177e4SLinus Torvalds 
81fc08c258SFabian Frederick static struct frag_queue *
82648700f7SEric Dumazet fq_find(struct net *net, __be32 id, const struct ipv6hdr *hdr, int iif)
831da177e4SLinus Torvalds {
84648700f7SEric Dumazet 	struct frag_v6_compare_key key = {
85648700f7SEric Dumazet 		.id = id,
86648700f7SEric Dumazet 		.saddr = hdr->saddr,
87648700f7SEric Dumazet 		.daddr = hdr->daddr,
88648700f7SEric Dumazet 		.user = IP6_DEFRAG_LOCAL_DELIVER,
89648700f7SEric Dumazet 		.iif = iif,
90648700f7SEric Dumazet 	};
91c6fda282SPavel Emelyanov 	struct inet_frag_queue *q;
921da177e4SLinus Torvalds 
93648700f7SEric Dumazet 	if (!(ipv6_addr_type(&hdr->daddr) & (IPV6_ADDR_MULTICAST |
94648700f7SEric Dumazet 					    IPV6_ADDR_LINKLOCAL)))
95648700f7SEric Dumazet 		key.iif = 0;
969a375803SPavel Emelyanov 
974907abc6SEric Dumazet 	q = inet_frag_find(net->ipv6.fqdir, &key);
982d44ed22SEric Dumazet 	if (!q)
999546377cSShan Wei 		return NULL;
1002d44ed22SEric Dumazet 
101c6fda282SPavel Emelyanov 	return container_of(q, struct frag_queue, q);
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds 
104f61944efSHerbert Xu static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
105415787d7SEric Dumazet 			  struct frag_hdr *fhdr, int nhoff,
106415787d7SEric Dumazet 			  u32 *prob_offset)
1071da177e4SLinus Torvalds {
108adf30907SEric Dumazet 	struct net *net = dev_net(skb_dst(skb)->dev);
109d4289fccSPeter Oskolkov 	int offset, end, fragsize;
110d4289fccSPeter Oskolkov 	struct sk_buff *prev_tail;
111d4289fccSPeter Oskolkov 	struct net_device *dev;
112d4289fccSPeter Oskolkov 	int err = -ENOENT;
113eec2e618SHannes Frederic Sowa 	u8 ecn;
1141da177e4SLinus Torvalds 
11506aa8b8aSNikolay Aleksandrov 	if (fq->q.flags & INET_FRAG_COMPLETE)
1161da177e4SLinus Torvalds 		goto err;
1171da177e4SLinus Torvalds 
118d4289fccSPeter Oskolkov 	err = -EINVAL;
1191da177e4SLinus Torvalds 	offset = ntohs(fhdr->frag_off) & ~0x7;
1200660e03fSArnaldo Carvalho de Melo 	end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
1210660e03fSArnaldo Carvalho de Melo 			((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	if ((unsigned int)end > IPV6_MAXPLEN) {
124415787d7SEric Dumazet 		*prob_offset = (u8 *)&fhdr->frag_off - skb_network_header(skb);
125d4289fccSPeter Oskolkov 		/* note that if prob_offset is set, the skb is freed elsewhere,
126d4289fccSPeter Oskolkov 		 * we do not free it here.
127d4289fccSPeter Oskolkov 		 */
128f61944efSHerbert Xu 		return -1;
1291da177e4SLinus Torvalds 	}
1301da177e4SLinus Torvalds 
131eec2e618SHannes Frederic Sowa 	ecn = ip6_frag_ecn(ipv6_hdr(skb));
132eec2e618SHannes Frederic Sowa 
133d56f90a7SArnaldo Carvalho de Melo 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
134d56f90a7SArnaldo Carvalho de Melo 		const unsigned char *nh = skb_network_header(skb);
1351da177e4SLinus Torvalds 		skb->csum = csum_sub(skb->csum,
136d56f90a7SArnaldo Carvalho de Melo 				     csum_partial(nh, (u8 *)(fhdr + 1) - nh,
137d56f90a7SArnaldo Carvalho de Melo 						  0));
138d56f90a7SArnaldo Carvalho de Melo 	}
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds 	/* Is this the final fragment? */
1411da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(IP6_MF))) {
1421da177e4SLinus Torvalds 		/* If we already have some bits beyond end
1431da177e4SLinus Torvalds 		 * or have different end, the segment is corrupted.
1441da177e4SLinus Torvalds 		 */
1455ab11c98SPavel Emelyanov 		if (end < fq->q.len ||
14606aa8b8aSNikolay Aleksandrov 		    ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len))
1472475f59cSPeter Oskolkov 			goto discard_fq;
14806aa8b8aSNikolay Aleksandrov 		fq->q.flags |= INET_FRAG_LAST_IN;
1495ab11c98SPavel Emelyanov 		fq->q.len = end;
1501da177e4SLinus Torvalds 	} else {
1511da177e4SLinus Torvalds 		/* Check if the fragment is rounded to 8 bytes.
1521da177e4SLinus Torvalds 		 * Required by the RFC.
1531da177e4SLinus Torvalds 		 */
1541da177e4SLinus Torvalds 		if (end & 0x7) {
1551da177e4SLinus Torvalds 			/* RFC2460 says always send parameter problem in
1561da177e4SLinus Torvalds 			 * this case. -DaveM
1571da177e4SLinus Torvalds 			 */
158415787d7SEric Dumazet 			*prob_offset = offsetof(struct ipv6hdr, payload_len);
159f61944efSHerbert Xu 			return -1;
1601da177e4SLinus Torvalds 		}
1615ab11c98SPavel Emelyanov 		if (end > fq->q.len) {
1621da177e4SLinus Torvalds 			/* Some bits beyond end -> corruption. */
16306aa8b8aSNikolay Aleksandrov 			if (fq->q.flags & INET_FRAG_LAST_IN)
1642475f59cSPeter Oskolkov 				goto discard_fq;
1655ab11c98SPavel Emelyanov 			fq->q.len = end;
1661da177e4SLinus Torvalds 		}
1671da177e4SLinus Torvalds 	}
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	if (end == offset)
1702475f59cSPeter Oskolkov 		goto discard_fq;
1711da177e4SLinus Torvalds 
172d4289fccSPeter Oskolkov 	err = -ENOMEM;
1731da177e4SLinus Torvalds 	/* Point into the IP datagram 'data' part. */
1741da177e4SLinus Torvalds 	if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
1752475f59cSPeter Oskolkov 		goto discard_fq;
17642ca89c1SStephen Hemminger 
177d4289fccSPeter Oskolkov 	err = pskb_trim_rcsum(skb, end - offset);
178d4289fccSPeter Oskolkov 	if (err)
1792475f59cSPeter Oskolkov 		goto discard_fq;
1801da177e4SLinus Torvalds 
181d4289fccSPeter Oskolkov 	/* Note : skb->rbnode and skb->dev share the same location. */
182219badfaSEric Dumazet 	dev = skb->dev;
183219badfaSEric Dumazet 	/* Makes sure compiler wont do silly aliasing games */
184219badfaSEric Dumazet 	barrier();
1851da177e4SLinus Torvalds 
186d4289fccSPeter Oskolkov 	prev_tail = fq->q.fragments_tail;
187d4289fccSPeter Oskolkov 	err = inet_frag_queue_insert(&fq->q, skb, offset, end);
188d4289fccSPeter Oskolkov 	if (err)
189d4289fccSPeter Oskolkov 		goto insert_error;
190d4289fccSPeter Oskolkov 
191d4289fccSPeter Oskolkov 	if (dev)
192d4289fccSPeter Oskolkov 		fq->iif = dev->ifindex;
1931da177e4SLinus Torvalds 
1945ab11c98SPavel Emelyanov 	fq->q.stamp = skb->tstamp;
1955ab11c98SPavel Emelyanov 	fq->q.meat += skb->len;
196eec2e618SHannes Frederic Sowa 	fq->ecn |= ecn;
1976ce3b4dcSEric Dumazet 	add_frag_mem_limit(fq->q.fqdir, skb->truesize);
1981da177e4SLinus Torvalds 
199dbd1759eSWillem de Bruijn 	fragsize = -skb_network_offset(skb) + skb->len;
200dbd1759eSWillem de Bruijn 	if (fragsize > fq->q.max_size)
201dbd1759eSWillem de Bruijn 		fq->q.max_size = fragsize;
202dbd1759eSWillem de Bruijn 
2031da177e4SLinus Torvalds 	/* The first fragment.
2041da177e4SLinus Torvalds 	 * nhoffset is obtained from the first fragment, of course.
2051da177e4SLinus Torvalds 	 */
2061da177e4SLinus Torvalds 	if (offset == 0) {
2071da177e4SLinus Torvalds 		fq->nhoffset = nhoff;
20806aa8b8aSNikolay Aleksandrov 		fq->q.flags |= INET_FRAG_FIRST_IN;
2091da177e4SLinus Torvalds 	}
210f61944efSHerbert Xu 
21106aa8b8aSNikolay Aleksandrov 	if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
21297599dc7SEric Dumazet 	    fq->q.meat == fq->q.len) {
21397599dc7SEric Dumazet 		unsigned long orefdst = skb->_skb_refdst;
214f61944efSHerbert Xu 
21597599dc7SEric Dumazet 		skb->_skb_refdst = 0UL;
216d4289fccSPeter Oskolkov 		err = ip6_frag_reasm(fq, skb, prev_tail, dev);
21797599dc7SEric Dumazet 		skb->_skb_refdst = orefdst;
218d4289fccSPeter Oskolkov 		return err;
21997599dc7SEric Dumazet 	}
22097599dc7SEric Dumazet 
22197599dc7SEric Dumazet 	skb_dst_drop(skb);
222d4289fccSPeter Oskolkov 	return -EINPROGRESS;
2231da177e4SLinus Torvalds 
224d4289fccSPeter Oskolkov insert_error:
225d4289fccSPeter Oskolkov 	if (err == IPFRAG_DUP) {
226d4289fccSPeter Oskolkov 		kfree_skb(skb);
227d4289fccSPeter Oskolkov 		return -EINVAL;
228d4289fccSPeter Oskolkov 	}
229d4289fccSPeter Oskolkov 	err = -EINVAL;
230d4289fccSPeter Oskolkov 	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
231d4289fccSPeter Oskolkov 			IPSTATS_MIB_REASM_OVERLAPS);
23270789d70SNicolas Dichtel discard_fq:
233093ba729SEric Dumazet 	inet_frag_kill(&fq->q);
2341d015503SEric Dumazet 	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
2353bd653c8SDenis V. Lunev 			IPSTATS_MIB_REASMFAILS);
236d4289fccSPeter Oskolkov err:
2371da177e4SLinus Torvalds 	kfree_skb(skb);
238d4289fccSPeter Oskolkov 	return err;
2391da177e4SLinus Torvalds }
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds /*
2421da177e4SLinus Torvalds  *	Check if this packet is complete.
2431da177e4SLinus Torvalds  *
2441da177e4SLinus Torvalds  *	It is called with locked fq, and caller must check that
2451da177e4SLinus Torvalds  *	queue is eligible for reassembly i.e. it is not COMPLETE,
2461da177e4SLinus Torvalds  *	the last and the first frames arrived and all the bits are here.
2471da177e4SLinus Torvalds  */
248d4289fccSPeter Oskolkov static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb,
249d4289fccSPeter Oskolkov 			  struct sk_buff *prev_tail, struct net_device *dev)
2501da177e4SLinus Torvalds {
251a39aca67SEric Dumazet 	struct net *net = fq->q.fqdir->net;
2521da177e4SLinus Torvalds 	unsigned int nhoff;
253d4289fccSPeter Oskolkov 	void *reasm_data;
254d4289fccSPeter Oskolkov 	int payload_len;
255eec2e618SHannes Frederic Sowa 	u8 ecn;
2561da177e4SLinus Torvalds 
257093ba729SEric Dumazet 	inet_frag_kill(&fq->q);
2581da177e4SLinus Torvalds 
259eec2e618SHannes Frederic Sowa 	ecn = ip_frag_ecn_table[fq->ecn];
260eec2e618SHannes Frederic Sowa 	if (unlikely(ecn == 0xff))
261eec2e618SHannes Frederic Sowa 		goto out_fail;
262eec2e618SHannes Frederic Sowa 
263d4289fccSPeter Oskolkov 	reasm_data = inet_frag_reasm_prepare(&fq->q, skb, prev_tail);
264d4289fccSPeter Oskolkov 	if (!reasm_data)
265f61944efSHerbert Xu 		goto out_oom;
266f61944efSHerbert Xu 
267d4289fccSPeter Oskolkov 	payload_len = ((skb->data - skb_network_header(skb)) -
2685ab11c98SPavel Emelyanov 		       sizeof(struct ipv6hdr) + fq->q.len -
269d56f90a7SArnaldo Carvalho de Melo 		       sizeof(struct frag_hdr));
2701da177e4SLinus Torvalds 	if (payload_len > IPV6_MAXPLEN)
2711da177e4SLinus Torvalds 		goto out_oversize;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	/* We have to remove fragment header from datagram and to relocate
2741da177e4SLinus Torvalds 	 * header in order to calculate ICV correctly. */
2751da177e4SLinus Torvalds 	nhoff = fq->nhoffset;
276d4289fccSPeter Oskolkov 	skb_network_header(skb)[nhoff] = skb_transport_header(skb)[0];
277d4289fccSPeter Oskolkov 	memmove(skb->head + sizeof(struct frag_hdr), skb->head,
278d4289fccSPeter Oskolkov 		(skb->data - skb->head) - sizeof(struct frag_hdr));
279d4289fccSPeter Oskolkov 	if (skb_mac_header_was_set(skb))
280d4289fccSPeter Oskolkov 		skb->mac_header += sizeof(struct frag_hdr);
281d4289fccSPeter Oskolkov 	skb->network_header += sizeof(struct frag_hdr);
2821da177e4SLinus Torvalds 
283d4289fccSPeter Oskolkov 	skb_reset_transport_header(skb);
2841da177e4SLinus Torvalds 
285d4289fccSPeter Oskolkov 	inet_frag_reasm_finish(&fq->q, skb, reasm_data);
286ec16439eSEric Dumazet 
287d4289fccSPeter Oskolkov 	skb->dev = dev;
288d4289fccSPeter Oskolkov 	ipv6_hdr(skb)->payload_len = htons(payload_len);
289d4289fccSPeter Oskolkov 	ipv6_change_dsfield(ipv6_hdr(skb), 0xff, ecn);
290d4289fccSPeter Oskolkov 	IP6CB(skb)->nhoff = nhoff;
291d4289fccSPeter Oskolkov 	IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
292d4289fccSPeter Oskolkov 	IP6CB(skb)->frag_max_size = fq->q.max_size;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	/* Yes, and fold redundant checksum back. 8) */
295d4289fccSPeter Oskolkov 	skb_postpush_rcsum(skb, skb_network_header(skb),
296d4289fccSPeter Oskolkov 			   skb_network_header_len(skb));
2971da177e4SLinus Torvalds 
298a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
299e1ae5c2eSStephen Suryaputra 	__IP6_INC_STATS(net, __in6_dev_stats_get(dev, skb), IPSTATS_MIB_REASMOKS);
300a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
301fa0f5273SPeter Oskolkov 	fq->q.rb_fragments = RB_ROOT;
302d6bebca9SChangli Gao 	fq->q.fragments_tail = NULL;
303d4289fccSPeter Oskolkov 	fq->q.last_run_head = NULL;
3041da177e4SLinus Torvalds 	return 1;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds out_oversize:
307e87cc472SJoe Perches 	net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len);
3081da177e4SLinus Torvalds 	goto out_fail;
3091da177e4SLinus Torvalds out_oom:
310e87cc472SJoe Perches 	net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n");
3111da177e4SLinus Torvalds out_fail:
312a11d206dSYOSHIFUJI Hideaki 	rcu_read_lock();
313e1ae5c2eSStephen Suryaputra 	__IP6_INC_STATS(net, __in6_dev_stats_get(dev, skb), IPSTATS_MIB_REASMFAILS);
314a11d206dSYOSHIFUJI Hideaki 	rcu_read_unlock();
3152475f59cSPeter Oskolkov 	inet_frag_kill(&fq->q);
3161da177e4SLinus Torvalds 	return -1;
3171da177e4SLinus Torvalds }
3181da177e4SLinus Torvalds 
319e5bbef20SHerbert Xu static int ipv6_frag_rcv(struct sk_buff *skb)
3201da177e4SLinus Torvalds {
3211da177e4SLinus Torvalds 	struct frag_hdr *fhdr;
3221da177e4SLinus Torvalds 	struct frag_queue *fq;
323b71d1d42SEric Dumazet 	const struct ipv6hdr *hdr = ipv6_hdr(skb);
324adf30907SEric Dumazet 	struct net *net = dev_net(skb_dst(skb)->dev);
325648700f7SEric Dumazet 	int iif;
3261da177e4SLinus Torvalds 
327f46078cfSHannes Frederic Sowa 	if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED)
328f46078cfSHannes Frederic Sowa 		goto fail_hdr;
329f46078cfSHannes Frederic Sowa 
3301d015503SEric Dumazet 	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS);
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	/* Jumbo payload inhibits frag. header */
33398b3377cSDenis V. Lunev 	if (hdr->payload_len == 0)
33498b3377cSDenis V. Lunev 		goto fail_hdr;
33598b3377cSDenis V. Lunev 
336ea2ae17dSArnaldo Carvalho de Melo 	if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
33798b3377cSDenis V. Lunev 				 sizeof(struct frag_hdr))))
33898b3377cSDenis V. Lunev 		goto fail_hdr;
3391da177e4SLinus Torvalds 
3400660e03fSArnaldo Carvalho de Melo 	hdr = ipv6_hdr(skb);
3419c70220bSArnaldo Carvalho de Melo 	fhdr = (struct frag_hdr *)skb_transport_header(skb);
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	if (!(fhdr->frag_off & htons(0xFFF9))) {
3441da177e4SLinus Torvalds 		/* It is not a fragmented frame */
345b0e380b1SArnaldo Carvalho de Melo 		skb->transport_header += sizeof(struct frag_hdr);
3461d015503SEric Dumazet 		__IP6_INC_STATS(net,
347adf30907SEric Dumazet 				ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS);
3481da177e4SLinus Torvalds 
349d56f90a7SArnaldo Carvalho de Melo 		IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
350f46078cfSHannes Frederic Sowa 		IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
3511da177e4SLinus Torvalds 		return 1;
3521da177e4SLinus Torvalds 	}
3531da177e4SLinus Torvalds 
354648700f7SEric Dumazet 	iif = skb->dev ? skb->dev->ifindex : 0;
355648700f7SEric Dumazet 	fq = fq_find(net, fhdr->identification, hdr, iif);
35653b24b8fSIan Morris 	if (fq) {
357415787d7SEric Dumazet 		u32 prob_offset = 0;
358f61944efSHerbert Xu 		int ret;
3591da177e4SLinus Torvalds 
3605ab11c98SPavel Emelyanov 		spin_lock(&fq->q.lock);
3611da177e4SLinus Torvalds 
362648700f7SEric Dumazet 		fq->iif = iif;
363415787d7SEric Dumazet 		ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff,
364415787d7SEric Dumazet 				     &prob_offset);
3651da177e4SLinus Torvalds 
3665ab11c98SPavel Emelyanov 		spin_unlock(&fq->q.lock);
367093ba729SEric Dumazet 		inet_frag_put(&fq->q);
368415787d7SEric Dumazet 		if (prob_offset) {
369415787d7SEric Dumazet 			__IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev),
370415787d7SEric Dumazet 					IPSTATS_MIB_INHDRERRORS);
371d4289fccSPeter Oskolkov 			/* icmpv6_param_prob() calls kfree_skb(skb) */
372415787d7SEric Dumazet 			icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, prob_offset);
373415787d7SEric Dumazet 		}
3741da177e4SLinus Torvalds 		return ret;
3751da177e4SLinus Torvalds 	}
3761da177e4SLinus Torvalds 
3771d015503SEric Dumazet 	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS);
3781da177e4SLinus Torvalds 	kfree_skb(skb);
3791da177e4SLinus Torvalds 	return -1;
38098b3377cSDenis V. Lunev 
38198b3377cSDenis V. Lunev fail_hdr:
382bdb7cc64SStephen Suryaputra 	__IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev),
383d2373862SNikolay Aleksandrov 			IPSTATS_MIB_INHDRERRORS);
38498b3377cSDenis V. Lunev 	icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
38598b3377cSDenis V. Lunev 	return -1;
3861da177e4SLinus Torvalds }
3871da177e4SLinus Torvalds 
388cc24becaSIan Morris static const struct inet6_protocol frag_protocol = {
3891da177e4SLinus Torvalds 	.handler	=	ipv6_frag_rcv,
3901da177e4SLinus Torvalds 	.flags		=	INET6_PROTO_NOPOLICY,
3911da177e4SLinus Torvalds };
3921da177e4SLinus Torvalds 
3938d8354d2SPavel Emelyanov #ifdef CONFIG_SYSCTL
3941bab4c75SNikolay Aleksandrov 
3950a64b4b8SPavel Emelyanov static struct ctl_table ip6_frags_ns_ctl_table[] = {
396e71e0349SDaniel Lezcano 	{
3978d8354d2SPavel Emelyanov 		.procname	= "ip6frag_high_thresh",
3983e67f106SEric Dumazet 		.maxlen		= sizeof(unsigned long),
3998d8354d2SPavel Emelyanov 		.mode		= 0644,
4003e67f106SEric Dumazet 		.proc_handler	= proc_doulongvec_minmax,
4018d8354d2SPavel Emelyanov 	},
4028d8354d2SPavel Emelyanov 	{
4038d8354d2SPavel Emelyanov 		.procname	= "ip6frag_low_thresh",
4043e67f106SEric Dumazet 		.maxlen		= sizeof(unsigned long),
4058d8354d2SPavel Emelyanov 		.mode		= 0644,
4066e00f7ddSEric Dumazet 		.proc_handler	= proc_doulongvec_minmax,
4078d8354d2SPavel Emelyanov 	},
4088d8354d2SPavel Emelyanov 	{
4098d8354d2SPavel Emelyanov 		.procname	= "ip6frag_time",
4108d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
4118d8354d2SPavel Emelyanov 		.mode		= 0644,
4126d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
4138d8354d2SPavel Emelyanov 	},
4147d291ebbSPavel Emelyanov 	{ }
4157d291ebbSPavel Emelyanov };
4167d291ebbSPavel Emelyanov 
417e3a57d18SFlorian Westphal /* secret interval has been deprecated */
418e3a57d18SFlorian Westphal static int ip6_frags_secret_interval_unused;
4197d291ebbSPavel Emelyanov static struct ctl_table ip6_frags_ctl_table[] = {
4208d8354d2SPavel Emelyanov 	{
4218d8354d2SPavel Emelyanov 		.procname	= "ip6frag_secret_interval",
422e3a57d18SFlorian Westphal 		.data		= &ip6_frags_secret_interval_unused,
4238d8354d2SPavel Emelyanov 		.maxlen		= sizeof(int),
4248d8354d2SPavel Emelyanov 		.mode		= 0644,
4256d9f239aSAlexey Dobriyan 		.proc_handler	= proc_dointvec_jiffies,
4268d8354d2SPavel Emelyanov 	},
4278d8354d2SPavel Emelyanov 	{ }
4288d8354d2SPavel Emelyanov };
4297d460db9SDaniel Lezcano 
4302c8c1e72SAlexey Dobriyan static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
4318d8354d2SPavel Emelyanov {
432e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
4338d8354d2SPavel Emelyanov 	struct ctl_table_header *hdr;
4348d8354d2SPavel Emelyanov 
4350a64b4b8SPavel Emelyanov 	table = ip6_frags_ns_ctl_table;
43609ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net)) {
4370a64b4b8SPavel Emelyanov 		table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
43863159f29SIan Morris 		if (!table)
439e4a2d5c2SPavel Emelyanov 			goto err_alloc;
440e4a2d5c2SPavel Emelyanov 
4418668d0e2SEric Dumazet 	}
4424907abc6SEric Dumazet 	table[0].data	= &net->ipv6.fqdir->high_thresh;
4434907abc6SEric Dumazet 	table[0].extra1	= &net->ipv6.fqdir->low_thresh;
4444907abc6SEric Dumazet 	table[1].data	= &net->ipv6.fqdir->low_thresh;
4454907abc6SEric Dumazet 	table[1].extra2	= &net->ipv6.fqdir->high_thresh;
4464907abc6SEric Dumazet 	table[2].data	= &net->ipv6.fqdir->timeout;
447e4a2d5c2SPavel Emelyanov 
448ec8f23ceSEric W. Biederman 	hdr = register_net_sysctl(net, "net/ipv6", table);
44963159f29SIan Morris 	if (!hdr)
450e4a2d5c2SPavel Emelyanov 		goto err_reg;
451e4a2d5c2SPavel Emelyanov 
452e4a2d5c2SPavel Emelyanov 	net->ipv6.sysctl.frags_hdr = hdr;
453e4a2d5c2SPavel Emelyanov 	return 0;
454e4a2d5c2SPavel Emelyanov 
455e4a2d5c2SPavel Emelyanov err_reg:
45609ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net))
457e4a2d5c2SPavel Emelyanov 		kfree(table);
458e4a2d5c2SPavel Emelyanov err_alloc:
459e4a2d5c2SPavel Emelyanov 	return -ENOMEM;
460e4a2d5c2SPavel Emelyanov }
461e4a2d5c2SPavel Emelyanov 
4622c8c1e72SAlexey Dobriyan static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
463e4a2d5c2SPavel Emelyanov {
464e4a2d5c2SPavel Emelyanov 	struct ctl_table *table;
465e4a2d5c2SPavel Emelyanov 
466e4a2d5c2SPavel Emelyanov 	table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
467e4a2d5c2SPavel Emelyanov 	unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
4683705e11aSYang Hongyang 	if (!net_eq(net, &init_net))
469e4a2d5c2SPavel Emelyanov 		kfree(table);
4708d8354d2SPavel Emelyanov }
4717d291ebbSPavel Emelyanov 
4727d291ebbSPavel Emelyanov static struct ctl_table_header *ip6_ctl_header;
4737d291ebbSPavel Emelyanov 
4747d291ebbSPavel Emelyanov static int ip6_frags_sysctl_register(void)
4757d291ebbSPavel Emelyanov {
47643444757SEric W. Biederman 	ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6",
4777d291ebbSPavel Emelyanov 			ip6_frags_ctl_table);
4787d291ebbSPavel Emelyanov 	return ip6_ctl_header == NULL ? -ENOMEM : 0;
4797d291ebbSPavel Emelyanov }
4807d291ebbSPavel Emelyanov 
4817d291ebbSPavel Emelyanov static void ip6_frags_sysctl_unregister(void)
4827d291ebbSPavel Emelyanov {
4837d291ebbSPavel Emelyanov 	unregister_net_sysctl_table(ip6_ctl_header);
4847d291ebbSPavel Emelyanov }
4858d8354d2SPavel Emelyanov #else
486fc08c258SFabian Frederick static int ip6_frags_ns_sysctl_register(struct net *net)
4878d8354d2SPavel Emelyanov {
4888d8354d2SPavel Emelyanov 	return 0;
4898d8354d2SPavel Emelyanov }
490e4a2d5c2SPavel Emelyanov 
491fc08c258SFabian Frederick static void ip6_frags_ns_sysctl_unregister(struct net *net)
492e4a2d5c2SPavel Emelyanov {
493e4a2d5c2SPavel Emelyanov }
4947d291ebbSPavel Emelyanov 
495fc08c258SFabian Frederick static int ip6_frags_sysctl_register(void)
4967d291ebbSPavel Emelyanov {
4977d291ebbSPavel Emelyanov 	return 0;
4987d291ebbSPavel Emelyanov }
4997d291ebbSPavel Emelyanov 
500fc08c258SFabian Frederick static void ip6_frags_sysctl_unregister(void)
5017d291ebbSPavel Emelyanov {
5027d291ebbSPavel Emelyanov }
5038d8354d2SPavel Emelyanov #endif
5048d8354d2SPavel Emelyanov 
5052c8c1e72SAlexey Dobriyan static int __net_init ipv6_frags_init_net(struct net *net)
5068d8354d2SPavel Emelyanov {
507787bea77SEric Dumazet 	int res;
508787bea77SEric Dumazet 
509a39aca67SEric Dumazet 	res = fqdir_init(&net->ipv6.fqdir, &ip6_frags, net);
510787bea77SEric Dumazet 	if (res < 0)
511787bea77SEric Dumazet 		return res;
5125a63643eSJesper Dangaard Brouer 
5134907abc6SEric Dumazet 	net->ipv6.fqdir->high_thresh = IPV6_FRAG_HIGH_THRESH;
5144907abc6SEric Dumazet 	net->ipv6.fqdir->low_thresh = IPV6_FRAG_LOW_THRESH;
5154907abc6SEric Dumazet 	net->ipv6.fqdir->timeout = IPV6_FRAG_TIMEOUT;
5164907abc6SEric Dumazet 
517787bea77SEric Dumazet 	res = ip6_frags_ns_sysctl_register(net);
518787bea77SEric Dumazet 	if (res < 0)
5194907abc6SEric Dumazet 		fqdir_exit(net->ipv6.fqdir);
520787bea77SEric Dumazet 	return res;
521e71e0349SDaniel Lezcano }
522e71e0349SDaniel Lezcano 
523d5dd8879SEric Dumazet static void __net_exit ipv6_frags_pre_exit_net(struct net *net)
524d5dd8879SEric Dumazet {
525d5dd8879SEric Dumazet 	fqdir_pre_exit(net->ipv6.fqdir);
526d5dd8879SEric Dumazet }
527d5dd8879SEric Dumazet 
5282c8c1e72SAlexey Dobriyan static void __net_exit ipv6_frags_exit_net(struct net *net)
52981566e83SPavel Emelyanov {
5300a64b4b8SPavel Emelyanov 	ip6_frags_ns_sysctl_unregister(net);
5314907abc6SEric Dumazet 	fqdir_exit(net->ipv6.fqdir);
53281566e83SPavel Emelyanov }
53381566e83SPavel Emelyanov 
53481566e83SPavel Emelyanov static struct pernet_operations ip6_frags_ops = {
53581566e83SPavel Emelyanov 	.init		= ipv6_frags_init_net,
536d5dd8879SEric Dumazet 	.pre_exit	= ipv6_frags_pre_exit_net,
53781566e83SPavel Emelyanov 	.exit		= ipv6_frags_exit_net,
53881566e83SPavel Emelyanov };
53981566e83SPavel Emelyanov 
54070b095c8SFlorian Westphal static const struct rhashtable_params ip6_rhash_params = {
541648700f7SEric Dumazet 	.head_offset		= offsetof(struct inet_frag_queue, node),
54270b095c8SFlorian Westphal 	.hashfn			= ip6frag_key_hashfn,
54370b095c8SFlorian Westphal 	.obj_hashfn		= ip6frag_obj_hashfn,
54470b095c8SFlorian Westphal 	.obj_cmpfn		= ip6frag_obj_cmpfn,
545648700f7SEric Dumazet 	.automatic_shrinking	= true,
546648700f7SEric Dumazet };
547648700f7SEric Dumazet 
548853cbbaaSDaniel Lezcano int __init ipv6_frag_init(void)
5491da177e4SLinus Torvalds {
550853cbbaaSDaniel Lezcano 	int ret;
5511da177e4SLinus Torvalds 
55270b095c8SFlorian Westphal 	ip6_frags.constructor = ip6frag_init;
553c9547709SPavel Emelyanov 	ip6_frags.destructor = NULL;
5541e4b8287SPavel Emelyanov 	ip6_frags.qsize = sizeof(struct frag_queue);
555e521db9dSPavel Emelyanov 	ip6_frags.frag_expire = ip6_frag_expire;
556d4ad4d22SNikolay Aleksandrov 	ip6_frags.frags_cache_name = ip6_frag_cache_name;
557648700f7SEric Dumazet 	ip6_frags.rhash_params = ip6_rhash_params;
558d4ad4d22SNikolay Aleksandrov 	ret = inet_frags_init(&ip6_frags);
559d4ad4d22SNikolay Aleksandrov 	if (ret)
5605b975babSEric Dumazet 		goto out;
5615b975babSEric Dumazet 
5625b975babSEric Dumazet 	ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
5635b975babSEric Dumazet 	if (ret)
5645b975babSEric Dumazet 		goto err_protocol;
5655b975babSEric Dumazet 
5665b975babSEric Dumazet 	ret = ip6_frags_sysctl_register();
5675b975babSEric Dumazet 	if (ret)
5685b975babSEric Dumazet 		goto err_sysctl;
5695b975babSEric Dumazet 
5705b975babSEric Dumazet 	ret = register_pernet_subsys(&ip6_frags_ops);
5715b975babSEric Dumazet 	if (ret)
572d4ad4d22SNikolay Aleksandrov 		goto err_pernet;
5735b975babSEric Dumazet 
574853cbbaaSDaniel Lezcano out:
575853cbbaaSDaniel Lezcano 	return ret;
5760002c630SPavel Emelyanov 
5770002c630SPavel Emelyanov err_pernet:
5787d291ebbSPavel Emelyanov 	ip6_frags_sysctl_unregister();
5797d291ebbSPavel Emelyanov err_sysctl:
5800002c630SPavel Emelyanov 	inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
5815b975babSEric Dumazet err_protocol:
5825b975babSEric Dumazet 	inet_frags_fini(&ip6_frags);
5830002c630SPavel Emelyanov 	goto out;
584853cbbaaSDaniel Lezcano }
585853cbbaaSDaniel Lezcano 
586853cbbaaSDaniel Lezcano void ipv6_frag_exit(void)
587853cbbaaSDaniel Lezcano {
5887d291ebbSPavel Emelyanov 	ip6_frags_sysctl_unregister();
58981566e83SPavel Emelyanov 	unregister_pernet_subsys(&ip6_frags_ops);
590853cbbaaSDaniel Lezcano 	inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
591ae7352d3SEric Dumazet 	inet_frags_fini(&ip6_frags);
5921da177e4SLinus Torvalds }
593