162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * IPv6 fragment reassembly 462306a36Sopenharmony_ci * Linux INET6 implementation 562306a36Sopenharmony_ci * 662306a36Sopenharmony_ci * Authors: 762306a36Sopenharmony_ci * Pedro Roque <roque@di.fc.ul.pt> 862306a36Sopenharmony_ci * 962306a36Sopenharmony_ci * Based on: net/ipv4/ip_fragment.c 1062306a36Sopenharmony_ci */ 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ci/* 1362306a36Sopenharmony_ci * Fixes: 1462306a36Sopenharmony_ci * Andi Kleen Make it work with multiple hosts. 1562306a36Sopenharmony_ci * More RFC compliance. 1662306a36Sopenharmony_ci * 1762306a36Sopenharmony_ci * Horst von Brand Add missing #include <linux/string.h> 1862306a36Sopenharmony_ci * Alexey Kuznetsov SMP races, threading, cleanup. 1962306a36Sopenharmony_ci * Patrick McHardy LRU queue of frag heads for evictor. 2062306a36Sopenharmony_ci * Mitsuru KANDA @USAGI Register inet6_protocol{}. 2162306a36Sopenharmony_ci * David Stevens and 2262306a36Sopenharmony_ci * YOSHIFUJI,H. @USAGI Always remove fragment header to 2362306a36Sopenharmony_ci * calculate ICV correctly. 2462306a36Sopenharmony_ci */ 2562306a36Sopenharmony_ci 2662306a36Sopenharmony_ci#define pr_fmt(fmt) "IPv6: " fmt 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci#include <linux/errno.h> 2962306a36Sopenharmony_ci#include <linux/types.h> 3062306a36Sopenharmony_ci#include <linux/string.h> 3162306a36Sopenharmony_ci#include <linux/socket.h> 3262306a36Sopenharmony_ci#include <linux/sockios.h> 3362306a36Sopenharmony_ci#include <linux/jiffies.h> 3462306a36Sopenharmony_ci#include <linux/net.h> 3562306a36Sopenharmony_ci#include <linux/list.h> 3662306a36Sopenharmony_ci#include <linux/netdevice.h> 3762306a36Sopenharmony_ci#include <linux/in6.h> 3862306a36Sopenharmony_ci#include <linux/ipv6.h> 3962306a36Sopenharmony_ci#include <linux/icmpv6.h> 4062306a36Sopenharmony_ci#include <linux/random.h> 4162306a36Sopenharmony_ci#include <linux/jhash.h> 4262306a36Sopenharmony_ci#include <linux/skbuff.h> 4362306a36Sopenharmony_ci#include <linux/slab.h> 4462306a36Sopenharmony_ci#include <linux/export.h> 4562306a36Sopenharmony_ci#include <linux/tcp.h> 4662306a36Sopenharmony_ci#include <linux/udp.h> 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ci#include <net/sock.h> 4962306a36Sopenharmony_ci#include <net/snmp.h> 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ci#include <net/ipv6.h> 5262306a36Sopenharmony_ci#include <net/ip6_route.h> 5362306a36Sopenharmony_ci#include <net/protocol.h> 5462306a36Sopenharmony_ci#include <net/transp_v6.h> 5562306a36Sopenharmony_ci#include <net/rawv6.h> 5662306a36Sopenharmony_ci#include <net/ndisc.h> 5762306a36Sopenharmony_ci#include <net/addrconf.h> 5862306a36Sopenharmony_ci#include <net/ipv6_frag.h> 5962306a36Sopenharmony_ci#include <net/inet_ecn.h> 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_cistatic const char ip6_frag_cache_name[] = "ip6-frags"; 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_cistatic u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h) 6462306a36Sopenharmony_ci{ 6562306a36Sopenharmony_ci return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK); 6662306a36Sopenharmony_ci} 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_cistatic struct inet_frags ip6_frags; 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_cistatic int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb, 7162306a36Sopenharmony_ci struct sk_buff *prev_tail, struct net_device *dev); 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_cistatic void ip6_frag_expire(struct timer_list *t) 7462306a36Sopenharmony_ci{ 7562306a36Sopenharmony_ci struct inet_frag_queue *frag = from_timer(frag, t, timer); 7662306a36Sopenharmony_ci struct frag_queue *fq; 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ci fq = container_of(frag, struct frag_queue, q); 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci ip6frag_expire_frag_queue(fq->q.fqdir->net, fq); 8162306a36Sopenharmony_ci} 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_cistatic struct frag_queue * 8462306a36Sopenharmony_cifq_find(struct net *net, __be32 id, const struct ipv6hdr *hdr, int iif) 8562306a36Sopenharmony_ci{ 8662306a36Sopenharmony_ci struct frag_v6_compare_key key = { 8762306a36Sopenharmony_ci .id = id, 8862306a36Sopenharmony_ci .saddr = hdr->saddr, 8962306a36Sopenharmony_ci .daddr = hdr->daddr, 9062306a36Sopenharmony_ci .user = IP6_DEFRAG_LOCAL_DELIVER, 9162306a36Sopenharmony_ci .iif = iif, 9262306a36Sopenharmony_ci }; 9362306a36Sopenharmony_ci struct inet_frag_queue *q; 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci if (!(ipv6_addr_type(&hdr->daddr) & (IPV6_ADDR_MULTICAST | 9662306a36Sopenharmony_ci IPV6_ADDR_LINKLOCAL))) 9762306a36Sopenharmony_ci key.iif = 0; 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_ci q = inet_frag_find(net->ipv6.fqdir, &key); 10062306a36Sopenharmony_ci if (!q) 10162306a36Sopenharmony_ci return NULL; 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ci return container_of(q, struct frag_queue, q); 10462306a36Sopenharmony_ci} 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_cistatic int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb, 10762306a36Sopenharmony_ci struct frag_hdr *fhdr, int nhoff, 10862306a36Sopenharmony_ci u32 *prob_offset) 10962306a36Sopenharmony_ci{ 11062306a36Sopenharmony_ci struct net *net = dev_net(skb_dst(skb)->dev); 11162306a36Sopenharmony_ci int offset, end, fragsize; 11262306a36Sopenharmony_ci struct sk_buff *prev_tail; 11362306a36Sopenharmony_ci struct net_device *dev; 11462306a36Sopenharmony_ci int err = -ENOENT; 11562306a36Sopenharmony_ci SKB_DR(reason); 11662306a36Sopenharmony_ci u8 ecn; 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci /* If reassembly is already done, @skb must be a duplicate frag. */ 11962306a36Sopenharmony_ci if (fq->q.flags & INET_FRAG_COMPLETE) { 12062306a36Sopenharmony_ci SKB_DR_SET(reason, DUP_FRAG); 12162306a36Sopenharmony_ci goto err; 12262306a36Sopenharmony_ci } 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_ci err = -EINVAL; 12562306a36Sopenharmony_ci offset = ntohs(fhdr->frag_off) & ~0x7; 12662306a36Sopenharmony_ci end = offset + (ntohs(ipv6_hdr(skb)->payload_len) - 12762306a36Sopenharmony_ci ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1))); 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_ci if ((unsigned int)end > IPV6_MAXPLEN) { 13062306a36Sopenharmony_ci *prob_offset = (u8 *)&fhdr->frag_off - skb_network_header(skb); 13162306a36Sopenharmony_ci /* note that if prob_offset is set, the skb is freed elsewhere, 13262306a36Sopenharmony_ci * we do not free it here. 13362306a36Sopenharmony_ci */ 13462306a36Sopenharmony_ci return -1; 13562306a36Sopenharmony_ci } 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ci ecn = ip6_frag_ecn(ipv6_hdr(skb)); 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ci if (skb->ip_summed == CHECKSUM_COMPLETE) { 14062306a36Sopenharmony_ci const unsigned char *nh = skb_network_header(skb); 14162306a36Sopenharmony_ci skb->csum = csum_sub(skb->csum, 14262306a36Sopenharmony_ci csum_partial(nh, (u8 *)(fhdr + 1) - nh, 14362306a36Sopenharmony_ci 0)); 14462306a36Sopenharmony_ci } 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_ci /* Is this the final fragment? */ 14762306a36Sopenharmony_ci if (!(fhdr->frag_off & htons(IP6_MF))) { 14862306a36Sopenharmony_ci /* If we already have some bits beyond end 14962306a36Sopenharmony_ci * or have different end, the segment is corrupted. 15062306a36Sopenharmony_ci */ 15162306a36Sopenharmony_ci if (end < fq->q.len || 15262306a36Sopenharmony_ci ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len)) 15362306a36Sopenharmony_ci goto discard_fq; 15462306a36Sopenharmony_ci fq->q.flags |= INET_FRAG_LAST_IN; 15562306a36Sopenharmony_ci fq->q.len = end; 15662306a36Sopenharmony_ci } else { 15762306a36Sopenharmony_ci /* Check if the fragment is rounded to 8 bytes. 15862306a36Sopenharmony_ci * Required by the RFC. 15962306a36Sopenharmony_ci */ 16062306a36Sopenharmony_ci if (end & 0x7) { 16162306a36Sopenharmony_ci /* RFC2460 says always send parameter problem in 16262306a36Sopenharmony_ci * this case. -DaveM 16362306a36Sopenharmony_ci */ 16462306a36Sopenharmony_ci *prob_offset = offsetof(struct ipv6hdr, payload_len); 16562306a36Sopenharmony_ci return -1; 16662306a36Sopenharmony_ci } 16762306a36Sopenharmony_ci if (end > fq->q.len) { 16862306a36Sopenharmony_ci /* Some bits beyond end -> corruption. */ 16962306a36Sopenharmony_ci if (fq->q.flags & INET_FRAG_LAST_IN) 17062306a36Sopenharmony_ci goto discard_fq; 17162306a36Sopenharmony_ci fq->q.len = end; 17262306a36Sopenharmony_ci } 17362306a36Sopenharmony_ci } 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_ci if (end == offset) 17662306a36Sopenharmony_ci goto discard_fq; 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_ci err = -ENOMEM; 17962306a36Sopenharmony_ci /* Point into the IP datagram 'data' part. */ 18062306a36Sopenharmony_ci if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) 18162306a36Sopenharmony_ci goto discard_fq; 18262306a36Sopenharmony_ci 18362306a36Sopenharmony_ci err = pskb_trim_rcsum(skb, end - offset); 18462306a36Sopenharmony_ci if (err) 18562306a36Sopenharmony_ci goto discard_fq; 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_ci /* Note : skb->rbnode and skb->dev share the same location. */ 18862306a36Sopenharmony_ci dev = skb->dev; 18962306a36Sopenharmony_ci /* Makes sure compiler wont do silly aliasing games */ 19062306a36Sopenharmony_ci barrier(); 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ci prev_tail = fq->q.fragments_tail; 19362306a36Sopenharmony_ci err = inet_frag_queue_insert(&fq->q, skb, offset, end); 19462306a36Sopenharmony_ci if (err) 19562306a36Sopenharmony_ci goto insert_error; 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_ci if (dev) 19862306a36Sopenharmony_ci fq->iif = dev->ifindex; 19962306a36Sopenharmony_ci 20062306a36Sopenharmony_ci fq->q.stamp = skb->tstamp; 20162306a36Sopenharmony_ci fq->q.mono_delivery_time = skb->mono_delivery_time; 20262306a36Sopenharmony_ci fq->q.meat += skb->len; 20362306a36Sopenharmony_ci fq->ecn |= ecn; 20462306a36Sopenharmony_ci add_frag_mem_limit(fq->q.fqdir, skb->truesize); 20562306a36Sopenharmony_ci 20662306a36Sopenharmony_ci fragsize = -skb_network_offset(skb) + skb->len; 20762306a36Sopenharmony_ci if (fragsize > fq->q.max_size) 20862306a36Sopenharmony_ci fq->q.max_size = fragsize; 20962306a36Sopenharmony_ci 21062306a36Sopenharmony_ci /* The first fragment. 21162306a36Sopenharmony_ci * nhoffset is obtained from the first fragment, of course. 21262306a36Sopenharmony_ci */ 21362306a36Sopenharmony_ci if (offset == 0) { 21462306a36Sopenharmony_ci fq->nhoffset = nhoff; 21562306a36Sopenharmony_ci fq->q.flags |= INET_FRAG_FIRST_IN; 21662306a36Sopenharmony_ci } 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_ci if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) && 21962306a36Sopenharmony_ci fq->q.meat == fq->q.len) { 22062306a36Sopenharmony_ci unsigned long orefdst = skb->_skb_refdst; 22162306a36Sopenharmony_ci 22262306a36Sopenharmony_ci skb->_skb_refdst = 0UL; 22362306a36Sopenharmony_ci err = ip6_frag_reasm(fq, skb, prev_tail, dev); 22462306a36Sopenharmony_ci skb->_skb_refdst = orefdst; 22562306a36Sopenharmony_ci return err; 22662306a36Sopenharmony_ci } 22762306a36Sopenharmony_ci 22862306a36Sopenharmony_ci skb_dst_drop(skb); 22962306a36Sopenharmony_ci return -EINPROGRESS; 23062306a36Sopenharmony_ci 23162306a36Sopenharmony_ciinsert_error: 23262306a36Sopenharmony_ci if (err == IPFRAG_DUP) { 23362306a36Sopenharmony_ci SKB_DR_SET(reason, DUP_FRAG); 23462306a36Sopenharmony_ci err = -EINVAL; 23562306a36Sopenharmony_ci goto err; 23662306a36Sopenharmony_ci } 23762306a36Sopenharmony_ci err = -EINVAL; 23862306a36Sopenharmony_ci __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), 23962306a36Sopenharmony_ci IPSTATS_MIB_REASM_OVERLAPS); 24062306a36Sopenharmony_cidiscard_fq: 24162306a36Sopenharmony_ci inet_frag_kill(&fq->q); 24262306a36Sopenharmony_ci __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), 24362306a36Sopenharmony_ci IPSTATS_MIB_REASMFAILS); 24462306a36Sopenharmony_cierr: 24562306a36Sopenharmony_ci kfree_skb_reason(skb, reason); 24662306a36Sopenharmony_ci return err; 24762306a36Sopenharmony_ci} 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_ci/* 25062306a36Sopenharmony_ci * Check if this packet is complete. 25162306a36Sopenharmony_ci * 25262306a36Sopenharmony_ci * It is called with locked fq, and caller must check that 25362306a36Sopenharmony_ci * queue is eligible for reassembly i.e. it is not COMPLETE, 25462306a36Sopenharmony_ci * the last and the first frames arrived and all the bits are here. 25562306a36Sopenharmony_ci */ 25662306a36Sopenharmony_cistatic int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb, 25762306a36Sopenharmony_ci struct sk_buff *prev_tail, struct net_device *dev) 25862306a36Sopenharmony_ci{ 25962306a36Sopenharmony_ci struct net *net = fq->q.fqdir->net; 26062306a36Sopenharmony_ci unsigned int nhoff; 26162306a36Sopenharmony_ci void *reasm_data; 26262306a36Sopenharmony_ci int payload_len; 26362306a36Sopenharmony_ci u8 ecn; 26462306a36Sopenharmony_ci 26562306a36Sopenharmony_ci inet_frag_kill(&fq->q); 26662306a36Sopenharmony_ci 26762306a36Sopenharmony_ci ecn = ip_frag_ecn_table[fq->ecn]; 26862306a36Sopenharmony_ci if (unlikely(ecn == 0xff)) 26962306a36Sopenharmony_ci goto out_fail; 27062306a36Sopenharmony_ci 27162306a36Sopenharmony_ci reasm_data = inet_frag_reasm_prepare(&fq->q, skb, prev_tail); 27262306a36Sopenharmony_ci if (!reasm_data) 27362306a36Sopenharmony_ci goto out_oom; 27462306a36Sopenharmony_ci 27562306a36Sopenharmony_ci payload_len = ((skb->data - skb_network_header(skb)) - 27662306a36Sopenharmony_ci sizeof(struct ipv6hdr) + fq->q.len - 27762306a36Sopenharmony_ci sizeof(struct frag_hdr)); 27862306a36Sopenharmony_ci if (payload_len > IPV6_MAXPLEN) 27962306a36Sopenharmony_ci goto out_oversize; 28062306a36Sopenharmony_ci 28162306a36Sopenharmony_ci /* We have to remove fragment header from datagram and to relocate 28262306a36Sopenharmony_ci * header in order to calculate ICV correctly. */ 28362306a36Sopenharmony_ci nhoff = fq->nhoffset; 28462306a36Sopenharmony_ci skb_network_header(skb)[nhoff] = skb_transport_header(skb)[0]; 28562306a36Sopenharmony_ci memmove(skb->head + sizeof(struct frag_hdr), skb->head, 28662306a36Sopenharmony_ci (skb->data - skb->head) - sizeof(struct frag_hdr)); 28762306a36Sopenharmony_ci if (skb_mac_header_was_set(skb)) 28862306a36Sopenharmony_ci skb->mac_header += sizeof(struct frag_hdr); 28962306a36Sopenharmony_ci skb->network_header += sizeof(struct frag_hdr); 29062306a36Sopenharmony_ci 29162306a36Sopenharmony_ci skb_reset_transport_header(skb); 29262306a36Sopenharmony_ci 29362306a36Sopenharmony_ci inet_frag_reasm_finish(&fq->q, skb, reasm_data, true); 29462306a36Sopenharmony_ci 29562306a36Sopenharmony_ci skb->dev = dev; 29662306a36Sopenharmony_ci ipv6_hdr(skb)->payload_len = htons(payload_len); 29762306a36Sopenharmony_ci ipv6_change_dsfield(ipv6_hdr(skb), 0xff, ecn); 29862306a36Sopenharmony_ci IP6CB(skb)->nhoff = nhoff; 29962306a36Sopenharmony_ci IP6CB(skb)->flags |= IP6SKB_FRAGMENTED; 30062306a36Sopenharmony_ci IP6CB(skb)->frag_max_size = fq->q.max_size; 30162306a36Sopenharmony_ci 30262306a36Sopenharmony_ci /* Yes, and fold redundant checksum back. 8) */ 30362306a36Sopenharmony_ci skb_postpush_rcsum(skb, skb_network_header(skb), 30462306a36Sopenharmony_ci skb_network_header_len(skb)); 30562306a36Sopenharmony_ci 30662306a36Sopenharmony_ci rcu_read_lock(); 30762306a36Sopenharmony_ci __IP6_INC_STATS(net, __in6_dev_stats_get(dev, skb), IPSTATS_MIB_REASMOKS); 30862306a36Sopenharmony_ci rcu_read_unlock(); 30962306a36Sopenharmony_ci fq->q.rb_fragments = RB_ROOT; 31062306a36Sopenharmony_ci fq->q.fragments_tail = NULL; 31162306a36Sopenharmony_ci fq->q.last_run_head = NULL; 31262306a36Sopenharmony_ci return 1; 31362306a36Sopenharmony_ci 31462306a36Sopenharmony_ciout_oversize: 31562306a36Sopenharmony_ci net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len); 31662306a36Sopenharmony_ci goto out_fail; 31762306a36Sopenharmony_ciout_oom: 31862306a36Sopenharmony_ci net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n"); 31962306a36Sopenharmony_ciout_fail: 32062306a36Sopenharmony_ci rcu_read_lock(); 32162306a36Sopenharmony_ci __IP6_INC_STATS(net, __in6_dev_stats_get(dev, skb), IPSTATS_MIB_REASMFAILS); 32262306a36Sopenharmony_ci rcu_read_unlock(); 32362306a36Sopenharmony_ci inet_frag_kill(&fq->q); 32462306a36Sopenharmony_ci return -1; 32562306a36Sopenharmony_ci} 32662306a36Sopenharmony_ci 32762306a36Sopenharmony_cistatic int ipv6_frag_rcv(struct sk_buff *skb) 32862306a36Sopenharmony_ci{ 32962306a36Sopenharmony_ci struct frag_hdr *fhdr; 33062306a36Sopenharmony_ci struct frag_queue *fq; 33162306a36Sopenharmony_ci const struct ipv6hdr *hdr = ipv6_hdr(skb); 33262306a36Sopenharmony_ci struct net *net = dev_net(skb_dst(skb)->dev); 33362306a36Sopenharmony_ci u8 nexthdr; 33462306a36Sopenharmony_ci int iif; 33562306a36Sopenharmony_ci 33662306a36Sopenharmony_ci if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED) 33762306a36Sopenharmony_ci goto fail_hdr; 33862306a36Sopenharmony_ci 33962306a36Sopenharmony_ci __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS); 34062306a36Sopenharmony_ci 34162306a36Sopenharmony_ci /* Jumbo payload inhibits frag. header */ 34262306a36Sopenharmony_ci if (hdr->payload_len == 0) 34362306a36Sopenharmony_ci goto fail_hdr; 34462306a36Sopenharmony_ci 34562306a36Sopenharmony_ci if (!pskb_may_pull(skb, (skb_transport_offset(skb) + 34662306a36Sopenharmony_ci sizeof(struct frag_hdr)))) 34762306a36Sopenharmony_ci goto fail_hdr; 34862306a36Sopenharmony_ci 34962306a36Sopenharmony_ci hdr = ipv6_hdr(skb); 35062306a36Sopenharmony_ci fhdr = (struct frag_hdr *)skb_transport_header(skb); 35162306a36Sopenharmony_ci 35262306a36Sopenharmony_ci if (!(fhdr->frag_off & htons(IP6_OFFSET | IP6_MF))) { 35362306a36Sopenharmony_ci /* It is not a fragmented frame */ 35462306a36Sopenharmony_ci skb->transport_header += sizeof(struct frag_hdr); 35562306a36Sopenharmony_ci __IP6_INC_STATS(net, 35662306a36Sopenharmony_ci ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS); 35762306a36Sopenharmony_ci 35862306a36Sopenharmony_ci IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb); 35962306a36Sopenharmony_ci IP6CB(skb)->flags |= IP6SKB_FRAGMENTED; 36062306a36Sopenharmony_ci IP6CB(skb)->frag_max_size = ntohs(hdr->payload_len) + 36162306a36Sopenharmony_ci sizeof(struct ipv6hdr); 36262306a36Sopenharmony_ci return 1; 36362306a36Sopenharmony_ci } 36462306a36Sopenharmony_ci 36562306a36Sopenharmony_ci /* RFC 8200, Section 4.5 Fragment Header: 36662306a36Sopenharmony_ci * If the first fragment does not include all headers through an 36762306a36Sopenharmony_ci * Upper-Layer header, then that fragment should be discarded and 36862306a36Sopenharmony_ci * an ICMP Parameter Problem, Code 3, message should be sent to 36962306a36Sopenharmony_ci * the source of the fragment, with the Pointer field set to zero. 37062306a36Sopenharmony_ci */ 37162306a36Sopenharmony_ci nexthdr = hdr->nexthdr; 37262306a36Sopenharmony_ci if (ipv6frag_thdr_truncated(skb, skb_transport_offset(skb), &nexthdr)) { 37362306a36Sopenharmony_ci __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev), 37462306a36Sopenharmony_ci IPSTATS_MIB_INHDRERRORS); 37562306a36Sopenharmony_ci icmpv6_param_prob(skb, ICMPV6_HDR_INCOMP, 0); 37662306a36Sopenharmony_ci return -1; 37762306a36Sopenharmony_ci } 37862306a36Sopenharmony_ci 37962306a36Sopenharmony_ci iif = skb->dev ? skb->dev->ifindex : 0; 38062306a36Sopenharmony_ci fq = fq_find(net, fhdr->identification, hdr, iif); 38162306a36Sopenharmony_ci if (fq) { 38262306a36Sopenharmony_ci u32 prob_offset = 0; 38362306a36Sopenharmony_ci int ret; 38462306a36Sopenharmony_ci 38562306a36Sopenharmony_ci spin_lock(&fq->q.lock); 38662306a36Sopenharmony_ci 38762306a36Sopenharmony_ci fq->iif = iif; 38862306a36Sopenharmony_ci ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff, 38962306a36Sopenharmony_ci &prob_offset); 39062306a36Sopenharmony_ci 39162306a36Sopenharmony_ci spin_unlock(&fq->q.lock); 39262306a36Sopenharmony_ci inet_frag_put(&fq->q); 39362306a36Sopenharmony_ci if (prob_offset) { 39462306a36Sopenharmony_ci __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev), 39562306a36Sopenharmony_ci IPSTATS_MIB_INHDRERRORS); 39662306a36Sopenharmony_ci /* icmpv6_param_prob() calls kfree_skb(skb) */ 39762306a36Sopenharmony_ci icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, prob_offset); 39862306a36Sopenharmony_ci } 39962306a36Sopenharmony_ci return ret; 40062306a36Sopenharmony_ci } 40162306a36Sopenharmony_ci 40262306a36Sopenharmony_ci __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS); 40362306a36Sopenharmony_ci kfree_skb(skb); 40462306a36Sopenharmony_ci return -1; 40562306a36Sopenharmony_ci 40662306a36Sopenharmony_cifail_hdr: 40762306a36Sopenharmony_ci __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev), 40862306a36Sopenharmony_ci IPSTATS_MIB_INHDRERRORS); 40962306a36Sopenharmony_ci icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb)); 41062306a36Sopenharmony_ci return -1; 41162306a36Sopenharmony_ci} 41262306a36Sopenharmony_ci 41362306a36Sopenharmony_cistatic const struct inet6_protocol frag_protocol = { 41462306a36Sopenharmony_ci .handler = ipv6_frag_rcv, 41562306a36Sopenharmony_ci .flags = INET6_PROTO_NOPOLICY, 41662306a36Sopenharmony_ci}; 41762306a36Sopenharmony_ci 41862306a36Sopenharmony_ci#ifdef CONFIG_SYSCTL 41962306a36Sopenharmony_ci 42062306a36Sopenharmony_cistatic struct ctl_table ip6_frags_ns_ctl_table[] = { 42162306a36Sopenharmony_ci { 42262306a36Sopenharmony_ci .procname = "ip6frag_high_thresh", 42362306a36Sopenharmony_ci .maxlen = sizeof(unsigned long), 42462306a36Sopenharmony_ci .mode = 0644, 42562306a36Sopenharmony_ci .proc_handler = proc_doulongvec_minmax, 42662306a36Sopenharmony_ci }, 42762306a36Sopenharmony_ci { 42862306a36Sopenharmony_ci .procname = "ip6frag_low_thresh", 42962306a36Sopenharmony_ci .maxlen = sizeof(unsigned long), 43062306a36Sopenharmony_ci .mode = 0644, 43162306a36Sopenharmony_ci .proc_handler = proc_doulongvec_minmax, 43262306a36Sopenharmony_ci }, 43362306a36Sopenharmony_ci { 43462306a36Sopenharmony_ci .procname = "ip6frag_time", 43562306a36Sopenharmony_ci .maxlen = sizeof(int), 43662306a36Sopenharmony_ci .mode = 0644, 43762306a36Sopenharmony_ci .proc_handler = proc_dointvec_jiffies, 43862306a36Sopenharmony_ci }, 43962306a36Sopenharmony_ci { } 44062306a36Sopenharmony_ci}; 44162306a36Sopenharmony_ci 44262306a36Sopenharmony_ci/* secret interval has been deprecated */ 44362306a36Sopenharmony_cistatic int ip6_frags_secret_interval_unused; 44462306a36Sopenharmony_cistatic struct ctl_table ip6_frags_ctl_table[] = { 44562306a36Sopenharmony_ci { 44662306a36Sopenharmony_ci .procname = "ip6frag_secret_interval", 44762306a36Sopenharmony_ci .data = &ip6_frags_secret_interval_unused, 44862306a36Sopenharmony_ci .maxlen = sizeof(int), 44962306a36Sopenharmony_ci .mode = 0644, 45062306a36Sopenharmony_ci .proc_handler = proc_dointvec_jiffies, 45162306a36Sopenharmony_ci }, 45262306a36Sopenharmony_ci { } 45362306a36Sopenharmony_ci}; 45462306a36Sopenharmony_ci 45562306a36Sopenharmony_cistatic int __net_init ip6_frags_ns_sysctl_register(struct net *net) 45662306a36Sopenharmony_ci{ 45762306a36Sopenharmony_ci struct ctl_table *table; 45862306a36Sopenharmony_ci struct ctl_table_header *hdr; 45962306a36Sopenharmony_ci 46062306a36Sopenharmony_ci table = ip6_frags_ns_ctl_table; 46162306a36Sopenharmony_ci if (!net_eq(net, &init_net)) { 46262306a36Sopenharmony_ci table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL); 46362306a36Sopenharmony_ci if (!table) 46462306a36Sopenharmony_ci goto err_alloc; 46562306a36Sopenharmony_ci 46662306a36Sopenharmony_ci } 46762306a36Sopenharmony_ci table[0].data = &net->ipv6.fqdir->high_thresh; 46862306a36Sopenharmony_ci table[0].extra1 = &net->ipv6.fqdir->low_thresh; 46962306a36Sopenharmony_ci table[1].data = &net->ipv6.fqdir->low_thresh; 47062306a36Sopenharmony_ci table[1].extra2 = &net->ipv6.fqdir->high_thresh; 47162306a36Sopenharmony_ci table[2].data = &net->ipv6.fqdir->timeout; 47262306a36Sopenharmony_ci 47362306a36Sopenharmony_ci hdr = register_net_sysctl_sz(net, "net/ipv6", table, 47462306a36Sopenharmony_ci ARRAY_SIZE(ip6_frags_ns_ctl_table)); 47562306a36Sopenharmony_ci if (!hdr) 47662306a36Sopenharmony_ci goto err_reg; 47762306a36Sopenharmony_ci 47862306a36Sopenharmony_ci net->ipv6.sysctl.frags_hdr = hdr; 47962306a36Sopenharmony_ci return 0; 48062306a36Sopenharmony_ci 48162306a36Sopenharmony_cierr_reg: 48262306a36Sopenharmony_ci if (!net_eq(net, &init_net)) 48362306a36Sopenharmony_ci kfree(table); 48462306a36Sopenharmony_cierr_alloc: 48562306a36Sopenharmony_ci return -ENOMEM; 48662306a36Sopenharmony_ci} 48762306a36Sopenharmony_ci 48862306a36Sopenharmony_cistatic void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net) 48962306a36Sopenharmony_ci{ 49062306a36Sopenharmony_ci struct ctl_table *table; 49162306a36Sopenharmony_ci 49262306a36Sopenharmony_ci table = net->ipv6.sysctl.frags_hdr->ctl_table_arg; 49362306a36Sopenharmony_ci unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr); 49462306a36Sopenharmony_ci if (!net_eq(net, &init_net)) 49562306a36Sopenharmony_ci kfree(table); 49662306a36Sopenharmony_ci} 49762306a36Sopenharmony_ci 49862306a36Sopenharmony_cistatic struct ctl_table_header *ip6_ctl_header; 49962306a36Sopenharmony_ci 50062306a36Sopenharmony_cistatic int ip6_frags_sysctl_register(void) 50162306a36Sopenharmony_ci{ 50262306a36Sopenharmony_ci ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6", 50362306a36Sopenharmony_ci ip6_frags_ctl_table); 50462306a36Sopenharmony_ci return ip6_ctl_header == NULL ? -ENOMEM : 0; 50562306a36Sopenharmony_ci} 50662306a36Sopenharmony_ci 50762306a36Sopenharmony_cistatic void ip6_frags_sysctl_unregister(void) 50862306a36Sopenharmony_ci{ 50962306a36Sopenharmony_ci unregister_net_sysctl_table(ip6_ctl_header); 51062306a36Sopenharmony_ci} 51162306a36Sopenharmony_ci#else 51262306a36Sopenharmony_cistatic int ip6_frags_ns_sysctl_register(struct net *net) 51362306a36Sopenharmony_ci{ 51462306a36Sopenharmony_ci return 0; 51562306a36Sopenharmony_ci} 51662306a36Sopenharmony_ci 51762306a36Sopenharmony_cistatic void ip6_frags_ns_sysctl_unregister(struct net *net) 51862306a36Sopenharmony_ci{ 51962306a36Sopenharmony_ci} 52062306a36Sopenharmony_ci 52162306a36Sopenharmony_cistatic int ip6_frags_sysctl_register(void) 52262306a36Sopenharmony_ci{ 52362306a36Sopenharmony_ci return 0; 52462306a36Sopenharmony_ci} 52562306a36Sopenharmony_ci 52662306a36Sopenharmony_cistatic void ip6_frags_sysctl_unregister(void) 52762306a36Sopenharmony_ci{ 52862306a36Sopenharmony_ci} 52962306a36Sopenharmony_ci#endif 53062306a36Sopenharmony_ci 53162306a36Sopenharmony_cistatic int __net_init ipv6_frags_init_net(struct net *net) 53262306a36Sopenharmony_ci{ 53362306a36Sopenharmony_ci int res; 53462306a36Sopenharmony_ci 53562306a36Sopenharmony_ci res = fqdir_init(&net->ipv6.fqdir, &ip6_frags, net); 53662306a36Sopenharmony_ci if (res < 0) 53762306a36Sopenharmony_ci return res; 53862306a36Sopenharmony_ci 53962306a36Sopenharmony_ci net->ipv6.fqdir->high_thresh = IPV6_FRAG_HIGH_THRESH; 54062306a36Sopenharmony_ci net->ipv6.fqdir->low_thresh = IPV6_FRAG_LOW_THRESH; 54162306a36Sopenharmony_ci net->ipv6.fqdir->timeout = IPV6_FRAG_TIMEOUT; 54262306a36Sopenharmony_ci 54362306a36Sopenharmony_ci res = ip6_frags_ns_sysctl_register(net); 54462306a36Sopenharmony_ci if (res < 0) 54562306a36Sopenharmony_ci fqdir_exit(net->ipv6.fqdir); 54662306a36Sopenharmony_ci return res; 54762306a36Sopenharmony_ci} 54862306a36Sopenharmony_ci 54962306a36Sopenharmony_cistatic void __net_exit ipv6_frags_pre_exit_net(struct net *net) 55062306a36Sopenharmony_ci{ 55162306a36Sopenharmony_ci fqdir_pre_exit(net->ipv6.fqdir); 55262306a36Sopenharmony_ci} 55362306a36Sopenharmony_ci 55462306a36Sopenharmony_cistatic void __net_exit ipv6_frags_exit_net(struct net *net) 55562306a36Sopenharmony_ci{ 55662306a36Sopenharmony_ci ip6_frags_ns_sysctl_unregister(net); 55762306a36Sopenharmony_ci fqdir_exit(net->ipv6.fqdir); 55862306a36Sopenharmony_ci} 55962306a36Sopenharmony_ci 56062306a36Sopenharmony_cistatic struct pernet_operations ip6_frags_ops = { 56162306a36Sopenharmony_ci .init = ipv6_frags_init_net, 56262306a36Sopenharmony_ci .pre_exit = ipv6_frags_pre_exit_net, 56362306a36Sopenharmony_ci .exit = ipv6_frags_exit_net, 56462306a36Sopenharmony_ci}; 56562306a36Sopenharmony_ci 56662306a36Sopenharmony_cistatic const struct rhashtable_params ip6_rhash_params = { 56762306a36Sopenharmony_ci .head_offset = offsetof(struct inet_frag_queue, node), 56862306a36Sopenharmony_ci .hashfn = ip6frag_key_hashfn, 56962306a36Sopenharmony_ci .obj_hashfn = ip6frag_obj_hashfn, 57062306a36Sopenharmony_ci .obj_cmpfn = ip6frag_obj_cmpfn, 57162306a36Sopenharmony_ci .automatic_shrinking = true, 57262306a36Sopenharmony_ci}; 57362306a36Sopenharmony_ci 57462306a36Sopenharmony_ciint __init ipv6_frag_init(void) 57562306a36Sopenharmony_ci{ 57662306a36Sopenharmony_ci int ret; 57762306a36Sopenharmony_ci 57862306a36Sopenharmony_ci ip6_frags.constructor = ip6frag_init; 57962306a36Sopenharmony_ci ip6_frags.destructor = NULL; 58062306a36Sopenharmony_ci ip6_frags.qsize = sizeof(struct frag_queue); 58162306a36Sopenharmony_ci ip6_frags.frag_expire = ip6_frag_expire; 58262306a36Sopenharmony_ci ip6_frags.frags_cache_name = ip6_frag_cache_name; 58362306a36Sopenharmony_ci ip6_frags.rhash_params = ip6_rhash_params; 58462306a36Sopenharmony_ci ret = inet_frags_init(&ip6_frags); 58562306a36Sopenharmony_ci if (ret) 58662306a36Sopenharmony_ci goto out; 58762306a36Sopenharmony_ci 58862306a36Sopenharmony_ci ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT); 58962306a36Sopenharmony_ci if (ret) 59062306a36Sopenharmony_ci goto err_protocol; 59162306a36Sopenharmony_ci 59262306a36Sopenharmony_ci ret = ip6_frags_sysctl_register(); 59362306a36Sopenharmony_ci if (ret) 59462306a36Sopenharmony_ci goto err_sysctl; 59562306a36Sopenharmony_ci 59662306a36Sopenharmony_ci ret = register_pernet_subsys(&ip6_frags_ops); 59762306a36Sopenharmony_ci if (ret) 59862306a36Sopenharmony_ci goto err_pernet; 59962306a36Sopenharmony_ci 60062306a36Sopenharmony_ciout: 60162306a36Sopenharmony_ci return ret; 60262306a36Sopenharmony_ci 60362306a36Sopenharmony_cierr_pernet: 60462306a36Sopenharmony_ci ip6_frags_sysctl_unregister(); 60562306a36Sopenharmony_cierr_sysctl: 60662306a36Sopenharmony_ci inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT); 60762306a36Sopenharmony_cierr_protocol: 60862306a36Sopenharmony_ci inet_frags_fini(&ip6_frags); 60962306a36Sopenharmony_ci goto out; 61062306a36Sopenharmony_ci} 61162306a36Sopenharmony_ci 61262306a36Sopenharmony_civoid ipv6_frag_exit(void) 61362306a36Sopenharmony_ci{ 61462306a36Sopenharmony_ci ip6_frags_sysctl_unregister(); 61562306a36Sopenharmony_ci unregister_pernet_subsys(&ip6_frags_ops); 61662306a36Sopenharmony_ci inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT); 61762306a36Sopenharmony_ci inet_frags_fini(&ip6_frags); 61862306a36Sopenharmony_ci} 619