18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *	IPv6 fragment reassembly
48c2ecf20Sopenharmony_ci *	Linux INET6 implementation
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci *	Authors:
78c2ecf20Sopenharmony_ci *	Pedro Roque		<roque@di.fc.ul.pt>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci *	Based on: net/ipv4/ip_fragment.c
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/*
138c2ecf20Sopenharmony_ci *	Fixes:
148c2ecf20Sopenharmony_ci *	Andi Kleen	Make it work with multiple hosts.
158c2ecf20Sopenharmony_ci *			More RFC compliance.
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci *      Horst von Brand Add missing #include <linux/string.h>
188c2ecf20Sopenharmony_ci *	Alexey Kuznetsov	SMP races, threading, cleanup.
198c2ecf20Sopenharmony_ci *	Patrick McHardy		LRU queue of frag heads for evictor.
208c2ecf20Sopenharmony_ci *	Mitsuru KANDA @USAGI	Register inet6_protocol{}.
218c2ecf20Sopenharmony_ci *	David Stevens and
228c2ecf20Sopenharmony_ci *	YOSHIFUJI,H. @USAGI	Always remove fragment header to
238c2ecf20Sopenharmony_ci *				calculate ICV correctly.
248c2ecf20Sopenharmony_ci */
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "IPv6: " fmt
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include <linux/errno.h>
298c2ecf20Sopenharmony_ci#include <linux/types.h>
308c2ecf20Sopenharmony_ci#include <linux/string.h>
318c2ecf20Sopenharmony_ci#include <linux/socket.h>
328c2ecf20Sopenharmony_ci#include <linux/sockios.h>
338c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
348c2ecf20Sopenharmony_ci#include <linux/net.h>
358c2ecf20Sopenharmony_ci#include <linux/list.h>
368c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
378c2ecf20Sopenharmony_ci#include <linux/in6.h>
388c2ecf20Sopenharmony_ci#include <linux/ipv6.h>
398c2ecf20Sopenharmony_ci#include <linux/icmpv6.h>
408c2ecf20Sopenharmony_ci#include <linux/random.h>
418c2ecf20Sopenharmony_ci#include <linux/jhash.h>
428c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
438c2ecf20Sopenharmony_ci#include <linux/slab.h>
448c2ecf20Sopenharmony_ci#include <linux/export.h>
458c2ecf20Sopenharmony_ci#include <linux/tcp.h>
468c2ecf20Sopenharmony_ci#include <linux/udp.h>
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#include <net/sock.h>
498c2ecf20Sopenharmony_ci#include <net/snmp.h>
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#include <net/ipv6.h>
528c2ecf20Sopenharmony_ci#include <net/ip6_route.h>
538c2ecf20Sopenharmony_ci#include <net/protocol.h>
548c2ecf20Sopenharmony_ci#include <net/transp_v6.h>
558c2ecf20Sopenharmony_ci#include <net/rawv6.h>
568c2ecf20Sopenharmony_ci#include <net/ndisc.h>
578c2ecf20Sopenharmony_ci#include <net/addrconf.h>
588c2ecf20Sopenharmony_ci#include <net/ipv6_frag.h>
598c2ecf20Sopenharmony_ci#include <net/inet_ecn.h>
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic const char ip6_frag_cache_name[] = "ip6-frags";
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic struct inet_frags ip6_frags;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb,
718c2ecf20Sopenharmony_ci			  struct sk_buff *prev_tail, struct net_device *dev);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic void ip6_frag_expire(struct timer_list *t)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	struct inet_frag_queue *frag = from_timer(frag, t, timer);
768c2ecf20Sopenharmony_ci	struct frag_queue *fq;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	fq = container_of(frag, struct frag_queue, q);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	ip6frag_expire_frag_queue(fq->q.fqdir->net, fq);
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic struct frag_queue *
848c2ecf20Sopenharmony_cifq_find(struct net *net, __be32 id, const struct ipv6hdr *hdr, int iif)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	struct frag_v6_compare_key key = {
878c2ecf20Sopenharmony_ci		.id = id,
888c2ecf20Sopenharmony_ci		.saddr = hdr->saddr,
898c2ecf20Sopenharmony_ci		.daddr = hdr->daddr,
908c2ecf20Sopenharmony_ci		.user = IP6_DEFRAG_LOCAL_DELIVER,
918c2ecf20Sopenharmony_ci		.iif = iif,
928c2ecf20Sopenharmony_ci	};
938c2ecf20Sopenharmony_ci	struct inet_frag_queue *q;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	if (!(ipv6_addr_type(&hdr->daddr) & (IPV6_ADDR_MULTICAST |
968c2ecf20Sopenharmony_ci					    IPV6_ADDR_LINKLOCAL)))
978c2ecf20Sopenharmony_ci		key.iif = 0;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	q = inet_frag_find(net->ipv6.fqdir, &key);
1008c2ecf20Sopenharmony_ci	if (!q)
1018c2ecf20Sopenharmony_ci		return NULL;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	return container_of(q, struct frag_queue, q);
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cistatic int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb,
1078c2ecf20Sopenharmony_ci			  struct frag_hdr *fhdr, int nhoff,
1088c2ecf20Sopenharmony_ci			  u32 *prob_offset)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	struct net *net = dev_net(skb_dst(skb)->dev);
1118c2ecf20Sopenharmony_ci	int offset, end, fragsize;
1128c2ecf20Sopenharmony_ci	struct sk_buff *prev_tail;
1138c2ecf20Sopenharmony_ci	struct net_device *dev;
1148c2ecf20Sopenharmony_ci	int err = -ENOENT;
1158c2ecf20Sopenharmony_ci	u8 ecn;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	if (fq->q.flags & INET_FRAG_COMPLETE)
1188c2ecf20Sopenharmony_ci		goto err;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	err = -EINVAL;
1218c2ecf20Sopenharmony_ci	offset = ntohs(fhdr->frag_off) & ~0x7;
1228c2ecf20Sopenharmony_ci	end = offset + (ntohs(ipv6_hdr(skb)->payload_len) -
1238c2ecf20Sopenharmony_ci			((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	if ((unsigned int)end > IPV6_MAXPLEN) {
1268c2ecf20Sopenharmony_ci		*prob_offset = (u8 *)&fhdr->frag_off - skb_network_header(skb);
1278c2ecf20Sopenharmony_ci		/* note that if prob_offset is set, the skb is freed elsewhere,
1288c2ecf20Sopenharmony_ci		 * we do not free it here.
1298c2ecf20Sopenharmony_ci		 */
1308c2ecf20Sopenharmony_ci		return -1;
1318c2ecf20Sopenharmony_ci	}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	ecn = ip6_frag_ecn(ipv6_hdr(skb));
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	if (skb->ip_summed == CHECKSUM_COMPLETE) {
1368c2ecf20Sopenharmony_ci		const unsigned char *nh = skb_network_header(skb);
1378c2ecf20Sopenharmony_ci		skb->csum = csum_sub(skb->csum,
1388c2ecf20Sopenharmony_ci				     csum_partial(nh, (u8 *)(fhdr + 1) - nh,
1398c2ecf20Sopenharmony_ci						  0));
1408c2ecf20Sopenharmony_ci	}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	/* Is this the final fragment? */
1438c2ecf20Sopenharmony_ci	if (!(fhdr->frag_off & htons(IP6_MF))) {
1448c2ecf20Sopenharmony_ci		/* If we already have some bits beyond end
1458c2ecf20Sopenharmony_ci		 * or have different end, the segment is corrupted.
1468c2ecf20Sopenharmony_ci		 */
1478c2ecf20Sopenharmony_ci		if (end < fq->q.len ||
1488c2ecf20Sopenharmony_ci		    ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len))
1498c2ecf20Sopenharmony_ci			goto discard_fq;
1508c2ecf20Sopenharmony_ci		fq->q.flags |= INET_FRAG_LAST_IN;
1518c2ecf20Sopenharmony_ci		fq->q.len = end;
1528c2ecf20Sopenharmony_ci	} else {
1538c2ecf20Sopenharmony_ci		/* Check if the fragment is rounded to 8 bytes.
1548c2ecf20Sopenharmony_ci		 * Required by the RFC.
1558c2ecf20Sopenharmony_ci		 */
1568c2ecf20Sopenharmony_ci		if (end & 0x7) {
1578c2ecf20Sopenharmony_ci			/* RFC2460 says always send parameter problem in
1588c2ecf20Sopenharmony_ci			 * this case. -DaveM
1598c2ecf20Sopenharmony_ci			 */
1608c2ecf20Sopenharmony_ci			*prob_offset = offsetof(struct ipv6hdr, payload_len);
1618c2ecf20Sopenharmony_ci			return -1;
1628c2ecf20Sopenharmony_ci		}
1638c2ecf20Sopenharmony_ci		if (end > fq->q.len) {
1648c2ecf20Sopenharmony_ci			/* Some bits beyond end -> corruption. */
1658c2ecf20Sopenharmony_ci			if (fq->q.flags & INET_FRAG_LAST_IN)
1668c2ecf20Sopenharmony_ci				goto discard_fq;
1678c2ecf20Sopenharmony_ci			fq->q.len = end;
1688c2ecf20Sopenharmony_ci		}
1698c2ecf20Sopenharmony_ci	}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	if (end == offset)
1728c2ecf20Sopenharmony_ci		goto discard_fq;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	err = -ENOMEM;
1758c2ecf20Sopenharmony_ci	/* Point into the IP datagram 'data' part. */
1768c2ecf20Sopenharmony_ci	if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data))
1778c2ecf20Sopenharmony_ci		goto discard_fq;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	err = pskb_trim_rcsum(skb, end - offset);
1808c2ecf20Sopenharmony_ci	if (err)
1818c2ecf20Sopenharmony_ci		goto discard_fq;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	/* Note : skb->rbnode and skb->dev share the same location. */
1848c2ecf20Sopenharmony_ci	dev = skb->dev;
1858c2ecf20Sopenharmony_ci	/* Makes sure compiler wont do silly aliasing games */
1868c2ecf20Sopenharmony_ci	barrier();
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	prev_tail = fq->q.fragments_tail;
1898c2ecf20Sopenharmony_ci	err = inet_frag_queue_insert(&fq->q, skb, offset, end);
1908c2ecf20Sopenharmony_ci	if (err)
1918c2ecf20Sopenharmony_ci		goto insert_error;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	if (dev)
1948c2ecf20Sopenharmony_ci		fq->iif = dev->ifindex;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	fq->q.stamp = skb->tstamp;
1978c2ecf20Sopenharmony_ci	fq->q.meat += skb->len;
1988c2ecf20Sopenharmony_ci	fq->ecn |= ecn;
1998c2ecf20Sopenharmony_ci	add_frag_mem_limit(fq->q.fqdir, skb->truesize);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	fragsize = -skb_network_offset(skb) + skb->len;
2028c2ecf20Sopenharmony_ci	if (fragsize > fq->q.max_size)
2038c2ecf20Sopenharmony_ci		fq->q.max_size = fragsize;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	/* The first fragment.
2068c2ecf20Sopenharmony_ci	 * nhoffset is obtained from the first fragment, of course.
2078c2ecf20Sopenharmony_ci	 */
2088c2ecf20Sopenharmony_ci	if (offset == 0) {
2098c2ecf20Sopenharmony_ci		fq->nhoffset = nhoff;
2108c2ecf20Sopenharmony_ci		fq->q.flags |= INET_FRAG_FIRST_IN;
2118c2ecf20Sopenharmony_ci	}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
2148c2ecf20Sopenharmony_ci	    fq->q.meat == fq->q.len) {
2158c2ecf20Sopenharmony_ci		unsigned long orefdst = skb->_skb_refdst;
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci		skb->_skb_refdst = 0UL;
2188c2ecf20Sopenharmony_ci		err = ip6_frag_reasm(fq, skb, prev_tail, dev);
2198c2ecf20Sopenharmony_ci		skb->_skb_refdst = orefdst;
2208c2ecf20Sopenharmony_ci		return err;
2218c2ecf20Sopenharmony_ci	}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	skb_dst_drop(skb);
2248c2ecf20Sopenharmony_ci	return -EINPROGRESS;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ciinsert_error:
2278c2ecf20Sopenharmony_ci	if (err == IPFRAG_DUP) {
2288c2ecf20Sopenharmony_ci		kfree_skb(skb);
2298c2ecf20Sopenharmony_ci		return -EINVAL;
2308c2ecf20Sopenharmony_ci	}
2318c2ecf20Sopenharmony_ci	err = -EINVAL;
2328c2ecf20Sopenharmony_ci	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
2338c2ecf20Sopenharmony_ci			IPSTATS_MIB_REASM_OVERLAPS);
2348c2ecf20Sopenharmony_cidiscard_fq:
2358c2ecf20Sopenharmony_ci	inet_frag_kill(&fq->q);
2368c2ecf20Sopenharmony_ci	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
2378c2ecf20Sopenharmony_ci			IPSTATS_MIB_REASMFAILS);
2388c2ecf20Sopenharmony_cierr:
2398c2ecf20Sopenharmony_ci	kfree_skb(skb);
2408c2ecf20Sopenharmony_ci	return err;
2418c2ecf20Sopenharmony_ci}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci/*
2448c2ecf20Sopenharmony_ci *	Check if this packet is complete.
2458c2ecf20Sopenharmony_ci *
2468c2ecf20Sopenharmony_ci *	It is called with locked fq, and caller must check that
2478c2ecf20Sopenharmony_ci *	queue is eligible for reassembly i.e. it is not COMPLETE,
2488c2ecf20Sopenharmony_ci *	the last and the first frames arrived and all the bits are here.
2498c2ecf20Sopenharmony_ci */
2508c2ecf20Sopenharmony_cistatic int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb,
2518c2ecf20Sopenharmony_ci			  struct sk_buff *prev_tail, struct net_device *dev)
2528c2ecf20Sopenharmony_ci{
2538c2ecf20Sopenharmony_ci	struct net *net = fq->q.fqdir->net;
2548c2ecf20Sopenharmony_ci	unsigned int nhoff;
2558c2ecf20Sopenharmony_ci	void *reasm_data;
2568c2ecf20Sopenharmony_ci	int payload_len;
2578c2ecf20Sopenharmony_ci	u8 ecn;
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	inet_frag_kill(&fq->q);
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	ecn = ip_frag_ecn_table[fq->ecn];
2628c2ecf20Sopenharmony_ci	if (unlikely(ecn == 0xff))
2638c2ecf20Sopenharmony_ci		goto out_fail;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	reasm_data = inet_frag_reasm_prepare(&fq->q, skb, prev_tail);
2668c2ecf20Sopenharmony_ci	if (!reasm_data)
2678c2ecf20Sopenharmony_ci		goto out_oom;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	payload_len = ((skb->data - skb_network_header(skb)) -
2708c2ecf20Sopenharmony_ci		       sizeof(struct ipv6hdr) + fq->q.len -
2718c2ecf20Sopenharmony_ci		       sizeof(struct frag_hdr));
2728c2ecf20Sopenharmony_ci	if (payload_len > IPV6_MAXPLEN)
2738c2ecf20Sopenharmony_ci		goto out_oversize;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	/* We have to remove fragment header from datagram and to relocate
2768c2ecf20Sopenharmony_ci	 * header in order to calculate ICV correctly. */
2778c2ecf20Sopenharmony_ci	nhoff = fq->nhoffset;
2788c2ecf20Sopenharmony_ci	skb_network_header(skb)[nhoff] = skb_transport_header(skb)[0];
2798c2ecf20Sopenharmony_ci	memmove(skb->head + sizeof(struct frag_hdr), skb->head,
2808c2ecf20Sopenharmony_ci		(skb->data - skb->head) - sizeof(struct frag_hdr));
2818c2ecf20Sopenharmony_ci	if (skb_mac_header_was_set(skb))
2828c2ecf20Sopenharmony_ci		skb->mac_header += sizeof(struct frag_hdr);
2838c2ecf20Sopenharmony_ci	skb->network_header += sizeof(struct frag_hdr);
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	skb_reset_transport_header(skb);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	inet_frag_reasm_finish(&fq->q, skb, reasm_data, true);
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	skb->dev = dev;
2908c2ecf20Sopenharmony_ci	ipv6_hdr(skb)->payload_len = htons(payload_len);
2918c2ecf20Sopenharmony_ci	ipv6_change_dsfield(ipv6_hdr(skb), 0xff, ecn);
2928c2ecf20Sopenharmony_ci	IP6CB(skb)->nhoff = nhoff;
2938c2ecf20Sopenharmony_ci	IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
2948c2ecf20Sopenharmony_ci	IP6CB(skb)->frag_max_size = fq->q.max_size;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	/* Yes, and fold redundant checksum back. 8) */
2978c2ecf20Sopenharmony_ci	skb_postpush_rcsum(skb, skb_network_header(skb),
2988c2ecf20Sopenharmony_ci			   skb_network_header_len(skb));
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	rcu_read_lock();
3018c2ecf20Sopenharmony_ci	__IP6_INC_STATS(net, __in6_dev_stats_get(dev, skb), IPSTATS_MIB_REASMOKS);
3028c2ecf20Sopenharmony_ci	rcu_read_unlock();
3038c2ecf20Sopenharmony_ci	fq->q.rb_fragments = RB_ROOT;
3048c2ecf20Sopenharmony_ci	fq->q.fragments_tail = NULL;
3058c2ecf20Sopenharmony_ci	fq->q.last_run_head = NULL;
3068c2ecf20Sopenharmony_ci	return 1;
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ciout_oversize:
3098c2ecf20Sopenharmony_ci	net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len);
3108c2ecf20Sopenharmony_ci	goto out_fail;
3118c2ecf20Sopenharmony_ciout_oom:
3128c2ecf20Sopenharmony_ci	net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n");
3138c2ecf20Sopenharmony_ciout_fail:
3148c2ecf20Sopenharmony_ci	rcu_read_lock();
3158c2ecf20Sopenharmony_ci	__IP6_INC_STATS(net, __in6_dev_stats_get(dev, skb), IPSTATS_MIB_REASMFAILS);
3168c2ecf20Sopenharmony_ci	rcu_read_unlock();
3178c2ecf20Sopenharmony_ci	inet_frag_kill(&fq->q);
3188c2ecf20Sopenharmony_ci	return -1;
3198c2ecf20Sopenharmony_ci}
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_cistatic int ipv6_frag_rcv(struct sk_buff *skb)
3228c2ecf20Sopenharmony_ci{
3238c2ecf20Sopenharmony_ci	struct frag_hdr *fhdr;
3248c2ecf20Sopenharmony_ci	struct frag_queue *fq;
3258c2ecf20Sopenharmony_ci	const struct ipv6hdr *hdr = ipv6_hdr(skb);
3268c2ecf20Sopenharmony_ci	struct net *net = dev_net(skb_dst(skb)->dev);
3278c2ecf20Sopenharmony_ci	u8 nexthdr;
3288c2ecf20Sopenharmony_ci	int iif;
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED)
3318c2ecf20Sopenharmony_ci		goto fail_hdr;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS);
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	/* Jumbo payload inhibits frag. header */
3368c2ecf20Sopenharmony_ci	if (hdr->payload_len == 0)
3378c2ecf20Sopenharmony_ci		goto fail_hdr;
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	if (!pskb_may_pull(skb, (skb_transport_offset(skb) +
3408c2ecf20Sopenharmony_ci				 sizeof(struct frag_hdr))))
3418c2ecf20Sopenharmony_ci		goto fail_hdr;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	hdr = ipv6_hdr(skb);
3448c2ecf20Sopenharmony_ci	fhdr = (struct frag_hdr *)skb_transport_header(skb);
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	if (!(fhdr->frag_off & htons(IP6_OFFSET | IP6_MF))) {
3478c2ecf20Sopenharmony_ci		/* It is not a fragmented frame */
3488c2ecf20Sopenharmony_ci		skb->transport_header += sizeof(struct frag_hdr);
3498c2ecf20Sopenharmony_ci		__IP6_INC_STATS(net,
3508c2ecf20Sopenharmony_ci				ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS);
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci		IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb);
3538c2ecf20Sopenharmony_ci		IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
3548c2ecf20Sopenharmony_ci		IP6CB(skb)->frag_max_size = ntohs(hdr->payload_len) +
3558c2ecf20Sopenharmony_ci					    sizeof(struct ipv6hdr);
3568c2ecf20Sopenharmony_ci		return 1;
3578c2ecf20Sopenharmony_ci	}
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	/* RFC 8200, Section 4.5 Fragment Header:
3608c2ecf20Sopenharmony_ci	 * If the first fragment does not include all headers through an
3618c2ecf20Sopenharmony_ci	 * Upper-Layer header, then that fragment should be discarded and
3628c2ecf20Sopenharmony_ci	 * an ICMP Parameter Problem, Code 3, message should be sent to
3638c2ecf20Sopenharmony_ci	 * the source of the fragment, with the Pointer field set to zero.
3648c2ecf20Sopenharmony_ci	 */
3658c2ecf20Sopenharmony_ci	nexthdr = hdr->nexthdr;
3668c2ecf20Sopenharmony_ci	if (ipv6frag_thdr_truncated(skb, skb_transport_offset(skb), &nexthdr)) {
3678c2ecf20Sopenharmony_ci		__IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev),
3688c2ecf20Sopenharmony_ci				IPSTATS_MIB_INHDRERRORS);
3698c2ecf20Sopenharmony_ci		icmpv6_param_prob(skb, ICMPV6_HDR_INCOMP, 0);
3708c2ecf20Sopenharmony_ci		return -1;
3718c2ecf20Sopenharmony_ci	}
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	iif = skb->dev ? skb->dev->ifindex : 0;
3748c2ecf20Sopenharmony_ci	fq = fq_find(net, fhdr->identification, hdr, iif);
3758c2ecf20Sopenharmony_ci	if (fq) {
3768c2ecf20Sopenharmony_ci		u32 prob_offset = 0;
3778c2ecf20Sopenharmony_ci		int ret;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci		spin_lock(&fq->q.lock);
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci		fq->iif = iif;
3828c2ecf20Sopenharmony_ci		ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff,
3838c2ecf20Sopenharmony_ci				     &prob_offset);
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci		spin_unlock(&fq->q.lock);
3868c2ecf20Sopenharmony_ci		inet_frag_put(&fq->q);
3878c2ecf20Sopenharmony_ci		if (prob_offset) {
3888c2ecf20Sopenharmony_ci			__IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev),
3898c2ecf20Sopenharmony_ci					IPSTATS_MIB_INHDRERRORS);
3908c2ecf20Sopenharmony_ci			/* icmpv6_param_prob() calls kfree_skb(skb) */
3918c2ecf20Sopenharmony_ci			icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, prob_offset);
3928c2ecf20Sopenharmony_ci		}
3938c2ecf20Sopenharmony_ci		return ret;
3948c2ecf20Sopenharmony_ci	}
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	__IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS);
3978c2ecf20Sopenharmony_ci	kfree_skb(skb);
3988c2ecf20Sopenharmony_ci	return -1;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_cifail_hdr:
4018c2ecf20Sopenharmony_ci	__IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev),
4028c2ecf20Sopenharmony_ci			IPSTATS_MIB_INHDRERRORS);
4038c2ecf20Sopenharmony_ci	icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb));
4048c2ecf20Sopenharmony_ci	return -1;
4058c2ecf20Sopenharmony_ci}
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_cistatic const struct inet6_protocol frag_protocol = {
4088c2ecf20Sopenharmony_ci	.handler	=	ipv6_frag_rcv,
4098c2ecf20Sopenharmony_ci	.flags		=	INET6_PROTO_NOPOLICY,
4108c2ecf20Sopenharmony_ci};
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci#ifdef CONFIG_SYSCTL
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_cistatic struct ctl_table ip6_frags_ns_ctl_table[] = {
4158c2ecf20Sopenharmony_ci	{
4168c2ecf20Sopenharmony_ci		.procname	= "ip6frag_high_thresh",
4178c2ecf20Sopenharmony_ci		.maxlen		= sizeof(unsigned long),
4188c2ecf20Sopenharmony_ci		.mode		= 0644,
4198c2ecf20Sopenharmony_ci		.proc_handler	= proc_doulongvec_minmax,
4208c2ecf20Sopenharmony_ci	},
4218c2ecf20Sopenharmony_ci	{
4228c2ecf20Sopenharmony_ci		.procname	= "ip6frag_low_thresh",
4238c2ecf20Sopenharmony_ci		.maxlen		= sizeof(unsigned long),
4248c2ecf20Sopenharmony_ci		.mode		= 0644,
4258c2ecf20Sopenharmony_ci		.proc_handler	= proc_doulongvec_minmax,
4268c2ecf20Sopenharmony_ci	},
4278c2ecf20Sopenharmony_ci	{
4288c2ecf20Sopenharmony_ci		.procname	= "ip6frag_time",
4298c2ecf20Sopenharmony_ci		.maxlen		= sizeof(int),
4308c2ecf20Sopenharmony_ci		.mode		= 0644,
4318c2ecf20Sopenharmony_ci		.proc_handler	= proc_dointvec_jiffies,
4328c2ecf20Sopenharmony_ci	},
4338c2ecf20Sopenharmony_ci	{ }
4348c2ecf20Sopenharmony_ci};
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci/* secret interval has been deprecated */
4378c2ecf20Sopenharmony_cistatic int ip6_frags_secret_interval_unused;
4388c2ecf20Sopenharmony_cistatic struct ctl_table ip6_frags_ctl_table[] = {
4398c2ecf20Sopenharmony_ci	{
4408c2ecf20Sopenharmony_ci		.procname	= "ip6frag_secret_interval",
4418c2ecf20Sopenharmony_ci		.data		= &ip6_frags_secret_interval_unused,
4428c2ecf20Sopenharmony_ci		.maxlen		= sizeof(int),
4438c2ecf20Sopenharmony_ci		.mode		= 0644,
4448c2ecf20Sopenharmony_ci		.proc_handler	= proc_dointvec_jiffies,
4458c2ecf20Sopenharmony_ci	},
4468c2ecf20Sopenharmony_ci	{ }
4478c2ecf20Sopenharmony_ci};
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_cistatic int __net_init ip6_frags_ns_sysctl_register(struct net *net)
4508c2ecf20Sopenharmony_ci{
4518c2ecf20Sopenharmony_ci	struct ctl_table *table;
4528c2ecf20Sopenharmony_ci	struct ctl_table_header *hdr;
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	table = ip6_frags_ns_ctl_table;
4558c2ecf20Sopenharmony_ci	if (!net_eq(net, &init_net)) {
4568c2ecf20Sopenharmony_ci		table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
4578c2ecf20Sopenharmony_ci		if (!table)
4588c2ecf20Sopenharmony_ci			goto err_alloc;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	}
4618c2ecf20Sopenharmony_ci	table[0].data	= &net->ipv6.fqdir->high_thresh;
4628c2ecf20Sopenharmony_ci	table[0].extra1	= &net->ipv6.fqdir->low_thresh;
4638c2ecf20Sopenharmony_ci	table[1].data	= &net->ipv6.fqdir->low_thresh;
4648c2ecf20Sopenharmony_ci	table[1].extra2	= &net->ipv6.fqdir->high_thresh;
4658c2ecf20Sopenharmony_ci	table[2].data	= &net->ipv6.fqdir->timeout;
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	hdr = register_net_sysctl(net, "net/ipv6", table);
4688c2ecf20Sopenharmony_ci	if (!hdr)
4698c2ecf20Sopenharmony_ci		goto err_reg;
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	net->ipv6.sysctl.frags_hdr = hdr;
4728c2ecf20Sopenharmony_ci	return 0;
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_cierr_reg:
4758c2ecf20Sopenharmony_ci	if (!net_eq(net, &init_net))
4768c2ecf20Sopenharmony_ci		kfree(table);
4778c2ecf20Sopenharmony_cierr_alloc:
4788c2ecf20Sopenharmony_ci	return -ENOMEM;
4798c2ecf20Sopenharmony_ci}
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_cistatic void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net)
4828c2ecf20Sopenharmony_ci{
4838c2ecf20Sopenharmony_ci	struct ctl_table *table;
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	table = net->ipv6.sysctl.frags_hdr->ctl_table_arg;
4868c2ecf20Sopenharmony_ci	unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr);
4878c2ecf20Sopenharmony_ci	if (!net_eq(net, &init_net))
4888c2ecf20Sopenharmony_ci		kfree(table);
4898c2ecf20Sopenharmony_ci}
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_cistatic struct ctl_table_header *ip6_ctl_header;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_cistatic int ip6_frags_sysctl_register(void)
4948c2ecf20Sopenharmony_ci{
4958c2ecf20Sopenharmony_ci	ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6",
4968c2ecf20Sopenharmony_ci			ip6_frags_ctl_table);
4978c2ecf20Sopenharmony_ci	return ip6_ctl_header == NULL ? -ENOMEM : 0;
4988c2ecf20Sopenharmony_ci}
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_cistatic void ip6_frags_sysctl_unregister(void)
5018c2ecf20Sopenharmony_ci{
5028c2ecf20Sopenharmony_ci	unregister_net_sysctl_table(ip6_ctl_header);
5038c2ecf20Sopenharmony_ci}
5048c2ecf20Sopenharmony_ci#else
5058c2ecf20Sopenharmony_cistatic int ip6_frags_ns_sysctl_register(struct net *net)
5068c2ecf20Sopenharmony_ci{
5078c2ecf20Sopenharmony_ci	return 0;
5088c2ecf20Sopenharmony_ci}
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_cistatic void ip6_frags_ns_sysctl_unregister(struct net *net)
5118c2ecf20Sopenharmony_ci{
5128c2ecf20Sopenharmony_ci}
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_cistatic int ip6_frags_sysctl_register(void)
5158c2ecf20Sopenharmony_ci{
5168c2ecf20Sopenharmony_ci	return 0;
5178c2ecf20Sopenharmony_ci}
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_cistatic void ip6_frags_sysctl_unregister(void)
5208c2ecf20Sopenharmony_ci{
5218c2ecf20Sopenharmony_ci}
5228c2ecf20Sopenharmony_ci#endif
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_cistatic int __net_init ipv6_frags_init_net(struct net *net)
5258c2ecf20Sopenharmony_ci{
5268c2ecf20Sopenharmony_ci	int res;
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci	res = fqdir_init(&net->ipv6.fqdir, &ip6_frags, net);
5298c2ecf20Sopenharmony_ci	if (res < 0)
5308c2ecf20Sopenharmony_ci		return res;
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	net->ipv6.fqdir->high_thresh = IPV6_FRAG_HIGH_THRESH;
5338c2ecf20Sopenharmony_ci	net->ipv6.fqdir->low_thresh = IPV6_FRAG_LOW_THRESH;
5348c2ecf20Sopenharmony_ci	net->ipv6.fqdir->timeout = IPV6_FRAG_TIMEOUT;
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	res = ip6_frags_ns_sysctl_register(net);
5378c2ecf20Sopenharmony_ci	if (res < 0)
5388c2ecf20Sopenharmony_ci		fqdir_exit(net->ipv6.fqdir);
5398c2ecf20Sopenharmony_ci	return res;
5408c2ecf20Sopenharmony_ci}
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_cistatic void __net_exit ipv6_frags_pre_exit_net(struct net *net)
5438c2ecf20Sopenharmony_ci{
5448c2ecf20Sopenharmony_ci	fqdir_pre_exit(net->ipv6.fqdir);
5458c2ecf20Sopenharmony_ci}
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_cistatic void __net_exit ipv6_frags_exit_net(struct net *net)
5488c2ecf20Sopenharmony_ci{
5498c2ecf20Sopenharmony_ci	ip6_frags_ns_sysctl_unregister(net);
5508c2ecf20Sopenharmony_ci	fqdir_exit(net->ipv6.fqdir);
5518c2ecf20Sopenharmony_ci}
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_cistatic struct pernet_operations ip6_frags_ops = {
5548c2ecf20Sopenharmony_ci	.init		= ipv6_frags_init_net,
5558c2ecf20Sopenharmony_ci	.pre_exit	= ipv6_frags_pre_exit_net,
5568c2ecf20Sopenharmony_ci	.exit		= ipv6_frags_exit_net,
5578c2ecf20Sopenharmony_ci};
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_cistatic const struct rhashtable_params ip6_rhash_params = {
5608c2ecf20Sopenharmony_ci	.head_offset		= offsetof(struct inet_frag_queue, node),
5618c2ecf20Sopenharmony_ci	.hashfn			= ip6frag_key_hashfn,
5628c2ecf20Sopenharmony_ci	.obj_hashfn		= ip6frag_obj_hashfn,
5638c2ecf20Sopenharmony_ci	.obj_cmpfn		= ip6frag_obj_cmpfn,
5648c2ecf20Sopenharmony_ci	.automatic_shrinking	= true,
5658c2ecf20Sopenharmony_ci};
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ciint __init ipv6_frag_init(void)
5688c2ecf20Sopenharmony_ci{
5698c2ecf20Sopenharmony_ci	int ret;
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	ip6_frags.constructor = ip6frag_init;
5728c2ecf20Sopenharmony_ci	ip6_frags.destructor = NULL;
5738c2ecf20Sopenharmony_ci	ip6_frags.qsize = sizeof(struct frag_queue);
5748c2ecf20Sopenharmony_ci	ip6_frags.frag_expire = ip6_frag_expire;
5758c2ecf20Sopenharmony_ci	ip6_frags.frags_cache_name = ip6_frag_cache_name;
5768c2ecf20Sopenharmony_ci	ip6_frags.rhash_params = ip6_rhash_params;
5778c2ecf20Sopenharmony_ci	ret = inet_frags_init(&ip6_frags);
5788c2ecf20Sopenharmony_ci	if (ret)
5798c2ecf20Sopenharmony_ci		goto out;
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT);
5828c2ecf20Sopenharmony_ci	if (ret)
5838c2ecf20Sopenharmony_ci		goto err_protocol;
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	ret = ip6_frags_sysctl_register();
5868c2ecf20Sopenharmony_ci	if (ret)
5878c2ecf20Sopenharmony_ci		goto err_sysctl;
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	ret = register_pernet_subsys(&ip6_frags_ops);
5908c2ecf20Sopenharmony_ci	if (ret)
5918c2ecf20Sopenharmony_ci		goto err_pernet;
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ciout:
5948c2ecf20Sopenharmony_ci	return ret;
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_cierr_pernet:
5978c2ecf20Sopenharmony_ci	ip6_frags_sysctl_unregister();
5988c2ecf20Sopenharmony_cierr_sysctl:
5998c2ecf20Sopenharmony_ci	inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
6008c2ecf20Sopenharmony_cierr_protocol:
6018c2ecf20Sopenharmony_ci	inet_frags_fini(&ip6_frags);
6028c2ecf20Sopenharmony_ci	goto out;
6038c2ecf20Sopenharmony_ci}
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_civoid ipv6_frag_exit(void)
6068c2ecf20Sopenharmony_ci{
6078c2ecf20Sopenharmony_ci	ip6_frags_sysctl_unregister();
6088c2ecf20Sopenharmony_ci	unregister_pernet_subsys(&ip6_frags_ops);
6098c2ecf20Sopenharmony_ci	inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT);
6108c2ecf20Sopenharmony_ci	inet_frags_fini(&ip6_frags);
6118c2ecf20Sopenharmony_ci}
612