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> 452efdaaafSHangbin Liu #include <linux/tcp.h> 462efdaaafSHangbin Liu #include <linux/udp.h> 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds #include <net/sock.h> 491da177e4SLinus Torvalds #include <net/snmp.h> 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds #include <net/ipv6.h> 52a11d206dSYOSHIFUJI Hideaki #include <net/ip6_route.h> 531da177e4SLinus Torvalds #include <net/protocol.h> 541da177e4SLinus Torvalds #include <net/transp_v6.h> 551da177e4SLinus Torvalds #include <net/rawv6.h> 561da177e4SLinus Torvalds #include <net/ndisc.h> 571da177e4SLinus Torvalds #include <net/addrconf.h> 5870b095c8SFlorian Westphal #include <net/ipv6_frag.h> 59eec2e618SHannes Frederic Sowa #include <net/inet_ecn.h> 601da177e4SLinus Torvalds 61d4ad4d22SNikolay Aleksandrov static const char ip6_frag_cache_name[] = "ip6-frags"; 62d4ad4d22SNikolay Aleksandrov 63fc08c258SFabian Frederick static u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h) 64eec2e618SHannes Frederic Sowa { 65eec2e618SHannes Frederic Sowa return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK); 66eec2e618SHannes Frederic Sowa } 671da177e4SLinus Torvalds 687eb95156SPavel Emelyanov static struct inet_frags ip6_frags; 691da177e4SLinus Torvalds 70d4289fccSPeter Oskolkov static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb, 71d4289fccSPeter Oskolkov struct sk_buff *prev_tail, struct net_device *dev); 72f61944efSHerbert Xu 7378802011SKees Cook static void ip6_frag_expire(struct timer_list *t) 74b836c99fSAmerigo Wang { 7578802011SKees Cook struct inet_frag_queue *frag = from_timer(frag, t, timer); 76b836c99fSAmerigo Wang struct frag_queue *fq; 77b836c99fSAmerigo Wang 7878802011SKees Cook fq = container_of(frag, struct frag_queue, q); 79b836c99fSAmerigo Wang 80a39aca67SEric Dumazet ip6frag_expire_frag_queue(fq->q.fqdir->net, fq); 811da177e4SLinus Torvalds } 821da177e4SLinus Torvalds 83fc08c258SFabian Frederick static struct frag_queue * 84648700f7SEric Dumazet fq_find(struct net *net, __be32 id, const struct ipv6hdr *hdr, int iif) 851da177e4SLinus Torvalds { 86648700f7SEric Dumazet struct frag_v6_compare_key key = { 87648700f7SEric Dumazet .id = id, 88648700f7SEric Dumazet .saddr = hdr->saddr, 89648700f7SEric Dumazet .daddr = hdr->daddr, 90648700f7SEric Dumazet .user = IP6_DEFRAG_LOCAL_DELIVER, 91648700f7SEric Dumazet .iif = iif, 92648700f7SEric Dumazet }; 93c6fda282SPavel Emelyanov struct inet_frag_queue *q; 941da177e4SLinus Torvalds 95648700f7SEric Dumazet if (!(ipv6_addr_type(&hdr->daddr) & (IPV6_ADDR_MULTICAST | 96648700f7SEric Dumazet IPV6_ADDR_LINKLOCAL))) 97648700f7SEric Dumazet key.iif = 0; 989a375803SPavel Emelyanov 994907abc6SEric Dumazet q = inet_frag_find(net->ipv6.fqdir, &key); 1002d44ed22SEric Dumazet if (!q) 1019546377cSShan Wei return NULL; 1022d44ed22SEric Dumazet 103c6fda282SPavel Emelyanov return container_of(q, struct frag_queue, q); 1041da177e4SLinus Torvalds } 1051da177e4SLinus Torvalds 106f61944efSHerbert Xu static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb, 107415787d7SEric Dumazet struct frag_hdr *fhdr, int nhoff, 108415787d7SEric Dumazet u32 *prob_offset) 1091da177e4SLinus Torvalds { 110adf30907SEric Dumazet struct net *net = dev_net(skb_dst(skb)->dev); 111d4289fccSPeter Oskolkov int offset, end, fragsize; 112d4289fccSPeter Oskolkov struct sk_buff *prev_tail; 113d4289fccSPeter Oskolkov struct net_device *dev; 114d4289fccSPeter Oskolkov int err = -ENOENT; 115eec2e618SHannes Frederic Sowa u8 ecn; 1161da177e4SLinus Torvalds 11706aa8b8aSNikolay Aleksandrov if (fq->q.flags & INET_FRAG_COMPLETE) 1181da177e4SLinus Torvalds goto err; 1191da177e4SLinus Torvalds 120d4289fccSPeter Oskolkov err = -EINVAL; 1211da177e4SLinus Torvalds offset = ntohs(fhdr->frag_off) & ~0x7; 1220660e03fSArnaldo Carvalho de Melo end = offset + (ntohs(ipv6_hdr(skb)->payload_len) - 1230660e03fSArnaldo Carvalho de Melo ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1))); 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds if ((unsigned int)end > IPV6_MAXPLEN) { 126415787d7SEric Dumazet *prob_offset = (u8 *)&fhdr->frag_off - skb_network_header(skb); 127d4289fccSPeter Oskolkov /* note that if prob_offset is set, the skb is freed elsewhere, 128d4289fccSPeter Oskolkov * we do not free it here. 129d4289fccSPeter Oskolkov */ 130f61944efSHerbert Xu return -1; 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds 133eec2e618SHannes Frederic Sowa ecn = ip6_frag_ecn(ipv6_hdr(skb)); 134eec2e618SHannes Frederic Sowa 135d56f90a7SArnaldo Carvalho de Melo if (skb->ip_summed == CHECKSUM_COMPLETE) { 136d56f90a7SArnaldo Carvalho de Melo const unsigned char *nh = skb_network_header(skb); 1371da177e4SLinus Torvalds skb->csum = csum_sub(skb->csum, 138d56f90a7SArnaldo Carvalho de Melo csum_partial(nh, (u8 *)(fhdr + 1) - nh, 139d56f90a7SArnaldo Carvalho de Melo 0)); 140d56f90a7SArnaldo Carvalho de Melo } 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds /* Is this the final fragment? */ 1431da177e4SLinus Torvalds if (!(fhdr->frag_off & htons(IP6_MF))) { 1441da177e4SLinus Torvalds /* If we already have some bits beyond end 1451da177e4SLinus Torvalds * or have different end, the segment is corrupted. 1461da177e4SLinus Torvalds */ 1475ab11c98SPavel Emelyanov if (end < fq->q.len || 14806aa8b8aSNikolay Aleksandrov ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len)) 1492475f59cSPeter Oskolkov goto discard_fq; 15006aa8b8aSNikolay Aleksandrov fq->q.flags |= INET_FRAG_LAST_IN; 1515ab11c98SPavel Emelyanov fq->q.len = end; 1521da177e4SLinus Torvalds } else { 1531da177e4SLinus Torvalds /* Check if the fragment is rounded to 8 bytes. 1541da177e4SLinus Torvalds * Required by the RFC. 1551da177e4SLinus Torvalds */ 1561da177e4SLinus Torvalds if (end & 0x7) { 1571da177e4SLinus Torvalds /* RFC2460 says always send parameter problem in 1581da177e4SLinus Torvalds * this case. -DaveM 1591da177e4SLinus Torvalds */ 160415787d7SEric Dumazet *prob_offset = offsetof(struct ipv6hdr, payload_len); 161f61944efSHerbert Xu return -1; 1621da177e4SLinus Torvalds } 1635ab11c98SPavel Emelyanov if (end > fq->q.len) { 1641da177e4SLinus Torvalds /* Some bits beyond end -> corruption. */ 16506aa8b8aSNikolay Aleksandrov if (fq->q.flags & INET_FRAG_LAST_IN) 1662475f59cSPeter Oskolkov goto discard_fq; 1675ab11c98SPavel Emelyanov fq->q.len = end; 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds } 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds if (end == offset) 1722475f59cSPeter Oskolkov goto discard_fq; 1731da177e4SLinus Torvalds 174d4289fccSPeter Oskolkov err = -ENOMEM; 1751da177e4SLinus Torvalds /* Point into the IP datagram 'data' part. */ 1761da177e4SLinus Torvalds if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) 1772475f59cSPeter Oskolkov goto discard_fq; 17842ca89c1SStephen Hemminger 179d4289fccSPeter Oskolkov err = pskb_trim_rcsum(skb, end - offset); 180d4289fccSPeter Oskolkov if (err) 1812475f59cSPeter Oskolkov goto discard_fq; 1821da177e4SLinus Torvalds 183d4289fccSPeter Oskolkov /* Note : skb->rbnode and skb->dev share the same location. */ 184219badfaSEric Dumazet dev = skb->dev; 185219badfaSEric Dumazet /* Makes sure compiler wont do silly aliasing games */ 186219badfaSEric Dumazet barrier(); 1871da177e4SLinus Torvalds 188d4289fccSPeter Oskolkov prev_tail = fq->q.fragments_tail; 189d4289fccSPeter Oskolkov err = inet_frag_queue_insert(&fq->q, skb, offset, end); 190d4289fccSPeter Oskolkov if (err) 191d4289fccSPeter Oskolkov goto insert_error; 192d4289fccSPeter Oskolkov 193d4289fccSPeter Oskolkov if (dev) 194d4289fccSPeter Oskolkov fq->iif = dev->ifindex; 1951da177e4SLinus Torvalds 1965ab11c98SPavel Emelyanov fq->q.stamp = skb->tstamp; 1975ab11c98SPavel Emelyanov fq->q.meat += skb->len; 198eec2e618SHannes Frederic Sowa fq->ecn |= ecn; 1996ce3b4dcSEric Dumazet add_frag_mem_limit(fq->q.fqdir, skb->truesize); 2001da177e4SLinus Torvalds 201dbd1759eSWillem de Bruijn fragsize = -skb_network_offset(skb) + skb->len; 202dbd1759eSWillem de Bruijn if (fragsize > fq->q.max_size) 203dbd1759eSWillem de Bruijn fq->q.max_size = fragsize; 204dbd1759eSWillem de Bruijn 2051da177e4SLinus Torvalds /* The first fragment. 2061da177e4SLinus Torvalds * nhoffset is obtained from the first fragment, of course. 2071da177e4SLinus Torvalds */ 2081da177e4SLinus Torvalds if (offset == 0) { 2091da177e4SLinus Torvalds fq->nhoffset = nhoff; 21006aa8b8aSNikolay Aleksandrov fq->q.flags |= INET_FRAG_FIRST_IN; 2111da177e4SLinus Torvalds } 212f61944efSHerbert Xu 21306aa8b8aSNikolay Aleksandrov if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) && 21497599dc7SEric Dumazet fq->q.meat == fq->q.len) { 21597599dc7SEric Dumazet unsigned long orefdst = skb->_skb_refdst; 216f61944efSHerbert Xu 21797599dc7SEric Dumazet skb->_skb_refdst = 0UL; 218d4289fccSPeter Oskolkov err = ip6_frag_reasm(fq, skb, prev_tail, dev); 21997599dc7SEric Dumazet skb->_skb_refdst = orefdst; 220d4289fccSPeter Oskolkov return err; 22197599dc7SEric Dumazet } 22297599dc7SEric Dumazet 22397599dc7SEric Dumazet skb_dst_drop(skb); 224d4289fccSPeter Oskolkov return -EINPROGRESS; 2251da177e4SLinus Torvalds 226d4289fccSPeter Oskolkov insert_error: 227d4289fccSPeter Oskolkov if (err == IPFRAG_DUP) { 228d4289fccSPeter Oskolkov kfree_skb(skb); 229d4289fccSPeter Oskolkov return -EINVAL; 230d4289fccSPeter Oskolkov } 231d4289fccSPeter Oskolkov err = -EINVAL; 232d4289fccSPeter Oskolkov __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), 233d4289fccSPeter Oskolkov IPSTATS_MIB_REASM_OVERLAPS); 23470789d70SNicolas Dichtel discard_fq: 235093ba729SEric Dumazet inet_frag_kill(&fq->q); 2361d015503SEric Dumazet __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), 2373bd653c8SDenis V. Lunev IPSTATS_MIB_REASMFAILS); 238d4289fccSPeter Oskolkov err: 2391da177e4SLinus Torvalds kfree_skb(skb); 240d4289fccSPeter Oskolkov return err; 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds /* 2441da177e4SLinus Torvalds * Check if this packet is complete. 2451da177e4SLinus Torvalds * 2461da177e4SLinus Torvalds * It is called with locked fq, and caller must check that 2471da177e4SLinus Torvalds * queue is eligible for reassembly i.e. it is not COMPLETE, 2481da177e4SLinus Torvalds * the last and the first frames arrived and all the bits are here. 2491da177e4SLinus Torvalds */ 250d4289fccSPeter Oskolkov static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb, 251d4289fccSPeter Oskolkov struct sk_buff *prev_tail, struct net_device *dev) 2521da177e4SLinus Torvalds { 253a39aca67SEric Dumazet struct net *net = fq->q.fqdir->net; 2541da177e4SLinus Torvalds unsigned int nhoff; 255d4289fccSPeter Oskolkov void *reasm_data; 256d4289fccSPeter Oskolkov int payload_len; 257eec2e618SHannes Frederic Sowa u8 ecn; 2581da177e4SLinus Torvalds 259093ba729SEric Dumazet inet_frag_kill(&fq->q); 2601da177e4SLinus Torvalds 261eec2e618SHannes Frederic Sowa ecn = ip_frag_ecn_table[fq->ecn]; 262eec2e618SHannes Frederic Sowa if (unlikely(ecn == 0xff)) 263eec2e618SHannes Frederic Sowa goto out_fail; 264eec2e618SHannes Frederic Sowa 265d4289fccSPeter Oskolkov reasm_data = inet_frag_reasm_prepare(&fq->q, skb, prev_tail); 266d4289fccSPeter Oskolkov if (!reasm_data) 267f61944efSHerbert Xu goto out_oom; 268f61944efSHerbert Xu 269d4289fccSPeter Oskolkov payload_len = ((skb->data - skb_network_header(skb)) - 2705ab11c98SPavel Emelyanov sizeof(struct ipv6hdr) + fq->q.len - 271d56f90a7SArnaldo Carvalho de Melo sizeof(struct frag_hdr)); 2721da177e4SLinus Torvalds if (payload_len > IPV6_MAXPLEN) 2731da177e4SLinus Torvalds goto out_oversize; 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds /* We have to remove fragment header from datagram and to relocate 2761da177e4SLinus Torvalds * header in order to calculate ICV correctly. */ 2771da177e4SLinus Torvalds nhoff = fq->nhoffset; 278d4289fccSPeter Oskolkov skb_network_header(skb)[nhoff] = skb_transport_header(skb)[0]; 279d4289fccSPeter Oskolkov memmove(skb->head + sizeof(struct frag_hdr), skb->head, 280d4289fccSPeter Oskolkov (skb->data - skb->head) - sizeof(struct frag_hdr)); 281d4289fccSPeter Oskolkov if (skb_mac_header_was_set(skb)) 282d4289fccSPeter Oskolkov skb->mac_header += sizeof(struct frag_hdr); 283d4289fccSPeter Oskolkov skb->network_header += sizeof(struct frag_hdr); 2841da177e4SLinus Torvalds 285d4289fccSPeter Oskolkov skb_reset_transport_header(skb); 2861da177e4SLinus Torvalds 287891584f4SGuillaume Nault inet_frag_reasm_finish(&fq->q, skb, reasm_data, true); 288ec16439eSEric Dumazet 289d4289fccSPeter Oskolkov skb->dev = dev; 290d4289fccSPeter Oskolkov ipv6_hdr(skb)->payload_len = htons(payload_len); 291d4289fccSPeter Oskolkov ipv6_change_dsfield(ipv6_hdr(skb), 0xff, ecn); 292d4289fccSPeter Oskolkov IP6CB(skb)->nhoff = nhoff; 293d4289fccSPeter Oskolkov IP6CB(skb)->flags |= IP6SKB_FRAGMENTED; 294d4289fccSPeter Oskolkov IP6CB(skb)->frag_max_size = fq->q.max_size; 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds /* Yes, and fold redundant checksum back. 8) */ 297d4289fccSPeter Oskolkov skb_postpush_rcsum(skb, skb_network_header(skb), 298d4289fccSPeter Oskolkov skb_network_header_len(skb)); 2991da177e4SLinus Torvalds 300a11d206dSYOSHIFUJI Hideaki rcu_read_lock(); 301e1ae5c2eSStephen Suryaputra __IP6_INC_STATS(net, __in6_dev_stats_get(dev, skb), IPSTATS_MIB_REASMOKS); 302a11d206dSYOSHIFUJI Hideaki rcu_read_unlock(); 303fa0f5273SPeter Oskolkov fq->q.rb_fragments = RB_ROOT; 304d6bebca9SChangli Gao fq->q.fragments_tail = NULL; 305d4289fccSPeter Oskolkov fq->q.last_run_head = NULL; 3061da177e4SLinus Torvalds return 1; 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds out_oversize: 309e87cc472SJoe Perches net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len); 3101da177e4SLinus Torvalds goto out_fail; 3111da177e4SLinus Torvalds out_oom: 312e87cc472SJoe Perches net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n"); 3131da177e4SLinus Torvalds out_fail: 314a11d206dSYOSHIFUJI Hideaki rcu_read_lock(); 315e1ae5c2eSStephen Suryaputra __IP6_INC_STATS(net, __in6_dev_stats_get(dev, skb), IPSTATS_MIB_REASMFAILS); 316a11d206dSYOSHIFUJI Hideaki rcu_read_unlock(); 3172475f59cSPeter Oskolkov inet_frag_kill(&fq->q); 3181da177e4SLinus Torvalds return -1; 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds 3219d9e937bSGeorg Kohmann /* Check if the upper layer header is truncated in the first fragment. */ 3229d9e937bSGeorg Kohmann bool ipv6_frag_thdr_truncated(struct sk_buff *skb, int start, u8 *nexthdrp) 3239d9e937bSGeorg Kohmann { 3249d9e937bSGeorg Kohmann u8 nexthdr = *nexthdrp; 3259d9e937bSGeorg Kohmann __be16 frag_off; 3269d9e937bSGeorg Kohmann int offset; 3279d9e937bSGeorg Kohmann 3289d9e937bSGeorg Kohmann offset = ipv6_skip_exthdr(skb, start, &nexthdr, &frag_off); 3299d9e937bSGeorg Kohmann if (offset < 0 || (frag_off & htons(IP6_OFFSET))) 3309d9e937bSGeorg Kohmann return false; 3319d9e937bSGeorg Kohmann switch (nexthdr) { 3329d9e937bSGeorg Kohmann case NEXTHDR_TCP: 3339d9e937bSGeorg Kohmann offset += sizeof(struct tcphdr); 3349d9e937bSGeorg Kohmann break; 3359d9e937bSGeorg Kohmann case NEXTHDR_UDP: 3369d9e937bSGeorg Kohmann offset += sizeof(struct udphdr); 3379d9e937bSGeorg Kohmann break; 3389d9e937bSGeorg Kohmann case NEXTHDR_ICMP: 3399d9e937bSGeorg Kohmann offset += sizeof(struct icmp6hdr); 3409d9e937bSGeorg Kohmann break; 3419d9e937bSGeorg Kohmann default: 3429d9e937bSGeorg Kohmann offset += 1; 3439d9e937bSGeorg Kohmann } 3449d9e937bSGeorg Kohmann if (offset > skb->len) 3459d9e937bSGeorg Kohmann return true; 3469d9e937bSGeorg Kohmann return false; 3479d9e937bSGeorg Kohmann } 3489d9e937bSGeorg Kohmann EXPORT_SYMBOL(ipv6_frag_thdr_truncated); 3499d9e937bSGeorg Kohmann 350e5bbef20SHerbert Xu static int ipv6_frag_rcv(struct sk_buff *skb) 3511da177e4SLinus Torvalds { 3521da177e4SLinus Torvalds struct frag_hdr *fhdr; 3531da177e4SLinus Torvalds struct frag_queue *fq; 354b71d1d42SEric Dumazet const struct ipv6hdr *hdr = ipv6_hdr(skb); 355adf30907SEric Dumazet struct net *net = dev_net(skb_dst(skb)->dev); 3562efdaaafSHangbin Liu u8 nexthdr; 3579d9e937bSGeorg Kohmann int iif; 3581da177e4SLinus Torvalds 359f46078cfSHannes Frederic Sowa if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED) 360f46078cfSHannes Frederic Sowa goto fail_hdr; 361f46078cfSHannes Frederic Sowa 3621d015503SEric Dumazet __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS); 3631da177e4SLinus Torvalds 3641da177e4SLinus Torvalds /* Jumbo payload inhibits frag. header */ 36598b3377cSDenis V. Lunev if (hdr->payload_len == 0) 36698b3377cSDenis V. Lunev goto fail_hdr; 36798b3377cSDenis V. Lunev 368ea2ae17dSArnaldo Carvalho de Melo if (!pskb_may_pull(skb, (skb_transport_offset(skb) + 36998b3377cSDenis V. Lunev sizeof(struct frag_hdr)))) 37098b3377cSDenis V. Lunev goto fail_hdr; 3711da177e4SLinus Torvalds 3720660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 3739c70220bSArnaldo Carvalho de Melo fhdr = (struct frag_hdr *)skb_transport_header(skb); 3741da177e4SLinus Torvalds 3751da177e4SLinus Torvalds if (!(fhdr->frag_off & htons(0xFFF9))) { 3761da177e4SLinus Torvalds /* It is not a fragmented frame */ 377b0e380b1SArnaldo Carvalho de Melo skb->transport_header += sizeof(struct frag_hdr); 3781d015503SEric Dumazet __IP6_INC_STATS(net, 379adf30907SEric Dumazet ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS); 3801da177e4SLinus Torvalds 381d56f90a7SArnaldo Carvalho de Melo IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb); 382f46078cfSHannes Frederic Sowa IP6CB(skb)->flags |= IP6SKB_FRAGMENTED; 3831da177e4SLinus Torvalds return 1; 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds 3862efdaaafSHangbin Liu /* RFC 8200, Section 4.5 Fragment Header: 3872efdaaafSHangbin Liu * If the first fragment does not include all headers through an 3882efdaaafSHangbin Liu * Upper-Layer header, then that fragment should be discarded and 3892efdaaafSHangbin Liu * an ICMP Parameter Problem, Code 3, message should be sent to 3902efdaaafSHangbin Liu * the source of the fragment, with the Pointer field set to zero. 3912efdaaafSHangbin Liu */ 3922efdaaafSHangbin Liu nexthdr = hdr->nexthdr; 3939d9e937bSGeorg Kohmann if (ipv6_frag_thdr_truncated(skb, skb_transport_offset(skb), &nexthdr)) { 3942efdaaafSHangbin Liu __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev), 3952efdaaafSHangbin Liu IPSTATS_MIB_INHDRERRORS); 3962efdaaafSHangbin Liu icmpv6_param_prob(skb, ICMPV6_HDR_INCOMP, 0); 3972efdaaafSHangbin Liu return -1; 3982efdaaafSHangbin Liu } 3992efdaaafSHangbin Liu 400648700f7SEric Dumazet iif = skb->dev ? skb->dev->ifindex : 0; 401648700f7SEric Dumazet fq = fq_find(net, fhdr->identification, hdr, iif); 40253b24b8fSIan Morris if (fq) { 403415787d7SEric Dumazet u32 prob_offset = 0; 404f61944efSHerbert Xu int ret; 4051da177e4SLinus Torvalds 4065ab11c98SPavel Emelyanov spin_lock(&fq->q.lock); 4071da177e4SLinus Torvalds 408648700f7SEric Dumazet fq->iif = iif; 409415787d7SEric Dumazet ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff, 410415787d7SEric Dumazet &prob_offset); 4111da177e4SLinus Torvalds 4125ab11c98SPavel Emelyanov spin_unlock(&fq->q.lock); 413093ba729SEric Dumazet inet_frag_put(&fq->q); 414415787d7SEric Dumazet if (prob_offset) { 415415787d7SEric Dumazet __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev), 416415787d7SEric Dumazet IPSTATS_MIB_INHDRERRORS); 417d4289fccSPeter Oskolkov /* icmpv6_param_prob() calls kfree_skb(skb) */ 418415787d7SEric Dumazet icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, prob_offset); 419415787d7SEric Dumazet } 4201da177e4SLinus Torvalds return ret; 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds 4231d015503SEric Dumazet __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS); 4241da177e4SLinus Torvalds kfree_skb(skb); 4251da177e4SLinus Torvalds return -1; 42698b3377cSDenis V. Lunev 42798b3377cSDenis V. Lunev fail_hdr: 428bdb7cc64SStephen Suryaputra __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev), 429d2373862SNikolay Aleksandrov IPSTATS_MIB_INHDRERRORS); 43098b3377cSDenis V. Lunev icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb)); 43198b3377cSDenis V. Lunev return -1; 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds 434cc24becaSIan Morris static const struct inet6_protocol frag_protocol = { 4351da177e4SLinus Torvalds .handler = ipv6_frag_rcv, 4361da177e4SLinus Torvalds .flags = INET6_PROTO_NOPOLICY, 4371da177e4SLinus Torvalds }; 4381da177e4SLinus Torvalds 4398d8354d2SPavel Emelyanov #ifdef CONFIG_SYSCTL 4401bab4c75SNikolay Aleksandrov 4410a64b4b8SPavel Emelyanov static struct ctl_table ip6_frags_ns_ctl_table[] = { 442e71e0349SDaniel Lezcano { 4438d8354d2SPavel Emelyanov .procname = "ip6frag_high_thresh", 4443e67f106SEric Dumazet .maxlen = sizeof(unsigned long), 4458d8354d2SPavel Emelyanov .mode = 0644, 4463e67f106SEric Dumazet .proc_handler = proc_doulongvec_minmax, 4478d8354d2SPavel Emelyanov }, 4488d8354d2SPavel Emelyanov { 4498d8354d2SPavel Emelyanov .procname = "ip6frag_low_thresh", 4503e67f106SEric Dumazet .maxlen = sizeof(unsigned long), 4518d8354d2SPavel Emelyanov .mode = 0644, 4526e00f7ddSEric Dumazet .proc_handler = proc_doulongvec_minmax, 4538d8354d2SPavel Emelyanov }, 4548d8354d2SPavel Emelyanov { 4558d8354d2SPavel Emelyanov .procname = "ip6frag_time", 4568d8354d2SPavel Emelyanov .maxlen = sizeof(int), 4578d8354d2SPavel Emelyanov .mode = 0644, 4586d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 4598d8354d2SPavel Emelyanov }, 4607d291ebbSPavel Emelyanov { } 4617d291ebbSPavel Emelyanov }; 4627d291ebbSPavel Emelyanov 463e3a57d18SFlorian Westphal /* secret interval has been deprecated */ 464e3a57d18SFlorian Westphal static int ip6_frags_secret_interval_unused; 4657d291ebbSPavel Emelyanov static struct ctl_table ip6_frags_ctl_table[] = { 4668d8354d2SPavel Emelyanov { 4678d8354d2SPavel Emelyanov .procname = "ip6frag_secret_interval", 468e3a57d18SFlorian Westphal .data = &ip6_frags_secret_interval_unused, 4698d8354d2SPavel Emelyanov .maxlen = sizeof(int), 4708d8354d2SPavel Emelyanov .mode = 0644, 4716d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 4728d8354d2SPavel Emelyanov }, 4738d8354d2SPavel Emelyanov { } 4748d8354d2SPavel Emelyanov }; 4757d460db9SDaniel Lezcano 4762c8c1e72SAlexey Dobriyan static int __net_init ip6_frags_ns_sysctl_register(struct net *net) 4778d8354d2SPavel Emelyanov { 478e4a2d5c2SPavel Emelyanov struct ctl_table *table; 4798d8354d2SPavel Emelyanov struct ctl_table_header *hdr; 4808d8354d2SPavel Emelyanov 4810a64b4b8SPavel Emelyanov table = ip6_frags_ns_ctl_table; 48209ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) { 4830a64b4b8SPavel Emelyanov table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL); 48463159f29SIan Morris if (!table) 485e4a2d5c2SPavel Emelyanov goto err_alloc; 486e4a2d5c2SPavel Emelyanov 4878668d0e2SEric Dumazet } 4884907abc6SEric Dumazet table[0].data = &net->ipv6.fqdir->high_thresh; 4894907abc6SEric Dumazet table[0].extra1 = &net->ipv6.fqdir->low_thresh; 4904907abc6SEric Dumazet table[1].data = &net->ipv6.fqdir->low_thresh; 4914907abc6SEric Dumazet table[1].extra2 = &net->ipv6.fqdir->high_thresh; 4924907abc6SEric Dumazet table[2].data = &net->ipv6.fqdir->timeout; 493e4a2d5c2SPavel Emelyanov 494ec8f23ceSEric W. Biederman hdr = register_net_sysctl(net, "net/ipv6", table); 49563159f29SIan Morris if (!hdr) 496e4a2d5c2SPavel Emelyanov goto err_reg; 497e4a2d5c2SPavel Emelyanov 498e4a2d5c2SPavel Emelyanov net->ipv6.sysctl.frags_hdr = hdr; 499e4a2d5c2SPavel Emelyanov return 0; 500e4a2d5c2SPavel Emelyanov 501e4a2d5c2SPavel Emelyanov err_reg: 50209ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) 503e4a2d5c2SPavel Emelyanov kfree(table); 504e4a2d5c2SPavel Emelyanov err_alloc: 505e4a2d5c2SPavel Emelyanov return -ENOMEM; 506e4a2d5c2SPavel Emelyanov } 507e4a2d5c2SPavel Emelyanov 5082c8c1e72SAlexey Dobriyan static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net) 509e4a2d5c2SPavel Emelyanov { 510e4a2d5c2SPavel Emelyanov struct ctl_table *table; 511e4a2d5c2SPavel Emelyanov 512e4a2d5c2SPavel Emelyanov table = net->ipv6.sysctl.frags_hdr->ctl_table_arg; 513e4a2d5c2SPavel Emelyanov unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr); 5143705e11aSYang Hongyang if (!net_eq(net, &init_net)) 515e4a2d5c2SPavel Emelyanov kfree(table); 5168d8354d2SPavel Emelyanov } 5177d291ebbSPavel Emelyanov 5187d291ebbSPavel Emelyanov static struct ctl_table_header *ip6_ctl_header; 5197d291ebbSPavel Emelyanov 5207d291ebbSPavel Emelyanov static int ip6_frags_sysctl_register(void) 5217d291ebbSPavel Emelyanov { 52243444757SEric W. Biederman ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6", 5237d291ebbSPavel Emelyanov ip6_frags_ctl_table); 5247d291ebbSPavel Emelyanov return ip6_ctl_header == NULL ? -ENOMEM : 0; 5257d291ebbSPavel Emelyanov } 5267d291ebbSPavel Emelyanov 5277d291ebbSPavel Emelyanov static void ip6_frags_sysctl_unregister(void) 5287d291ebbSPavel Emelyanov { 5297d291ebbSPavel Emelyanov unregister_net_sysctl_table(ip6_ctl_header); 5307d291ebbSPavel Emelyanov } 5318d8354d2SPavel Emelyanov #else 532fc08c258SFabian Frederick static int ip6_frags_ns_sysctl_register(struct net *net) 5338d8354d2SPavel Emelyanov { 5348d8354d2SPavel Emelyanov return 0; 5358d8354d2SPavel Emelyanov } 536e4a2d5c2SPavel Emelyanov 537fc08c258SFabian Frederick static void ip6_frags_ns_sysctl_unregister(struct net *net) 538e4a2d5c2SPavel Emelyanov { 539e4a2d5c2SPavel Emelyanov } 5407d291ebbSPavel Emelyanov 541fc08c258SFabian Frederick static int ip6_frags_sysctl_register(void) 5427d291ebbSPavel Emelyanov { 5437d291ebbSPavel Emelyanov return 0; 5447d291ebbSPavel Emelyanov } 5457d291ebbSPavel Emelyanov 546fc08c258SFabian Frederick static void ip6_frags_sysctl_unregister(void) 5477d291ebbSPavel Emelyanov { 5487d291ebbSPavel Emelyanov } 5498d8354d2SPavel Emelyanov #endif 5508d8354d2SPavel Emelyanov 5512c8c1e72SAlexey Dobriyan static int __net_init ipv6_frags_init_net(struct net *net) 5528d8354d2SPavel Emelyanov { 553787bea77SEric Dumazet int res; 554787bea77SEric Dumazet 555a39aca67SEric Dumazet res = fqdir_init(&net->ipv6.fqdir, &ip6_frags, net); 556787bea77SEric Dumazet if (res < 0) 557787bea77SEric Dumazet return res; 5585a63643eSJesper Dangaard Brouer 5594907abc6SEric Dumazet net->ipv6.fqdir->high_thresh = IPV6_FRAG_HIGH_THRESH; 5604907abc6SEric Dumazet net->ipv6.fqdir->low_thresh = IPV6_FRAG_LOW_THRESH; 5614907abc6SEric Dumazet net->ipv6.fqdir->timeout = IPV6_FRAG_TIMEOUT; 5624907abc6SEric Dumazet 563787bea77SEric Dumazet res = ip6_frags_ns_sysctl_register(net); 564787bea77SEric Dumazet if (res < 0) 5654907abc6SEric Dumazet fqdir_exit(net->ipv6.fqdir); 566787bea77SEric Dumazet return res; 567e71e0349SDaniel Lezcano } 568e71e0349SDaniel Lezcano 569d5dd8879SEric Dumazet static void __net_exit ipv6_frags_pre_exit_net(struct net *net) 570d5dd8879SEric Dumazet { 571d5dd8879SEric Dumazet fqdir_pre_exit(net->ipv6.fqdir); 572d5dd8879SEric Dumazet } 573d5dd8879SEric Dumazet 5742c8c1e72SAlexey Dobriyan static void __net_exit ipv6_frags_exit_net(struct net *net) 57581566e83SPavel Emelyanov { 5760a64b4b8SPavel Emelyanov ip6_frags_ns_sysctl_unregister(net); 5774907abc6SEric Dumazet fqdir_exit(net->ipv6.fqdir); 57881566e83SPavel Emelyanov } 57981566e83SPavel Emelyanov 58081566e83SPavel Emelyanov static struct pernet_operations ip6_frags_ops = { 58181566e83SPavel Emelyanov .init = ipv6_frags_init_net, 582d5dd8879SEric Dumazet .pre_exit = ipv6_frags_pre_exit_net, 58381566e83SPavel Emelyanov .exit = ipv6_frags_exit_net, 58481566e83SPavel Emelyanov }; 58581566e83SPavel Emelyanov 58670b095c8SFlorian Westphal static const struct rhashtable_params ip6_rhash_params = { 587648700f7SEric Dumazet .head_offset = offsetof(struct inet_frag_queue, node), 58870b095c8SFlorian Westphal .hashfn = ip6frag_key_hashfn, 58970b095c8SFlorian Westphal .obj_hashfn = ip6frag_obj_hashfn, 59070b095c8SFlorian Westphal .obj_cmpfn = ip6frag_obj_cmpfn, 591648700f7SEric Dumazet .automatic_shrinking = true, 592648700f7SEric Dumazet }; 593648700f7SEric Dumazet 594853cbbaaSDaniel Lezcano int __init ipv6_frag_init(void) 5951da177e4SLinus Torvalds { 596853cbbaaSDaniel Lezcano int ret; 5971da177e4SLinus Torvalds 59870b095c8SFlorian Westphal ip6_frags.constructor = ip6frag_init; 599c9547709SPavel Emelyanov ip6_frags.destructor = NULL; 6001e4b8287SPavel Emelyanov ip6_frags.qsize = sizeof(struct frag_queue); 601e521db9dSPavel Emelyanov ip6_frags.frag_expire = ip6_frag_expire; 602d4ad4d22SNikolay Aleksandrov ip6_frags.frags_cache_name = ip6_frag_cache_name; 603648700f7SEric Dumazet ip6_frags.rhash_params = ip6_rhash_params; 604d4ad4d22SNikolay Aleksandrov ret = inet_frags_init(&ip6_frags); 605d4ad4d22SNikolay Aleksandrov if (ret) 6065b975babSEric Dumazet goto out; 6075b975babSEric Dumazet 6085b975babSEric Dumazet ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT); 6095b975babSEric Dumazet if (ret) 6105b975babSEric Dumazet goto err_protocol; 6115b975babSEric Dumazet 6125b975babSEric Dumazet ret = ip6_frags_sysctl_register(); 6135b975babSEric Dumazet if (ret) 6145b975babSEric Dumazet goto err_sysctl; 6155b975babSEric Dumazet 6165b975babSEric Dumazet ret = register_pernet_subsys(&ip6_frags_ops); 6175b975babSEric Dumazet if (ret) 618d4ad4d22SNikolay Aleksandrov goto err_pernet; 6195b975babSEric Dumazet 620853cbbaaSDaniel Lezcano out: 621853cbbaaSDaniel Lezcano return ret; 6220002c630SPavel Emelyanov 6230002c630SPavel Emelyanov err_pernet: 6247d291ebbSPavel Emelyanov ip6_frags_sysctl_unregister(); 6257d291ebbSPavel Emelyanov err_sysctl: 6260002c630SPavel Emelyanov inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT); 6275b975babSEric Dumazet err_protocol: 6285b975babSEric Dumazet inet_frags_fini(&ip6_frags); 6290002c630SPavel Emelyanov goto out; 630853cbbaaSDaniel Lezcano } 631853cbbaaSDaniel Lezcano 632853cbbaaSDaniel Lezcano void ipv6_frag_exit(void) 633853cbbaaSDaniel Lezcano { 6347d291ebbSPavel Emelyanov ip6_frags_sysctl_unregister(); 63581566e83SPavel Emelyanov unregister_pernet_subsys(&ip6_frags_ops); 636853cbbaaSDaniel Lezcano inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT); 637ae7352d3SEric Dumazet inet_frags_fini(&ip6_frags); 6381da177e4SLinus Torvalds } 639