12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * IPv6 fragment reassembly 41da177e4SLinus Torvalds * Linux INET6 implementation 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Authors: 71da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Based on: net/ipv4/ip_fragment.c 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds /* 131da177e4SLinus Torvalds * Fixes: 141da177e4SLinus Torvalds * Andi Kleen Make it work with multiple hosts. 151da177e4SLinus Torvalds * More RFC compliance. 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * Horst von Brand Add missing #include <linux/string.h> 181da177e4SLinus Torvalds * Alexey Kuznetsov SMP races, threading, cleanup. 191da177e4SLinus Torvalds * Patrick McHardy LRU queue of frag heads for evictor. 201da177e4SLinus Torvalds * Mitsuru KANDA @USAGI Register inet6_protocol{}. 211da177e4SLinus Torvalds * David Stevens and 221da177e4SLinus Torvalds * YOSHIFUJI,H. @USAGI Always remove fragment header to 231da177e4SLinus Torvalds * calculate ICV correctly. 241da177e4SLinus Torvalds */ 255a3da1feSHannes Frederic Sowa 265a3da1feSHannes Frederic Sowa #define pr_fmt(fmt) "IPv6: " fmt 275a3da1feSHannes Frederic Sowa 281da177e4SLinus Torvalds #include <linux/errno.h> 291da177e4SLinus Torvalds #include <linux/types.h> 301da177e4SLinus Torvalds #include <linux/string.h> 311da177e4SLinus Torvalds #include <linux/socket.h> 321da177e4SLinus Torvalds #include <linux/sockios.h> 331da177e4SLinus Torvalds #include <linux/jiffies.h> 341da177e4SLinus Torvalds #include <linux/net.h> 351da177e4SLinus Torvalds #include <linux/list.h> 361da177e4SLinus Torvalds #include <linux/netdevice.h> 371da177e4SLinus Torvalds #include <linux/in6.h> 381da177e4SLinus Torvalds #include <linux/ipv6.h> 391da177e4SLinus Torvalds #include <linux/icmpv6.h> 401da177e4SLinus Torvalds #include <linux/random.h> 411da177e4SLinus Torvalds #include <linux/jhash.h> 42f61944efSHerbert Xu #include <linux/skbuff.h> 435a0e3ad6STejun Heo #include <linux/slab.h> 44bc3b2d7fSPaul Gortmaker #include <linux/export.h> 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds #include <net/sock.h> 471da177e4SLinus Torvalds #include <net/snmp.h> 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds #include <net/ipv6.h> 50a11d206dSYOSHIFUJI Hideaki #include <net/ip6_route.h> 511da177e4SLinus Torvalds #include <net/protocol.h> 521da177e4SLinus Torvalds #include <net/transp_v6.h> 531da177e4SLinus Torvalds #include <net/rawv6.h> 541da177e4SLinus Torvalds #include <net/ndisc.h> 551da177e4SLinus Torvalds #include <net/addrconf.h> 5670b095c8SFlorian Westphal #include <net/ipv6_frag.h> 57eec2e618SHannes Frederic Sowa #include <net/inet_ecn.h> 581da177e4SLinus Torvalds 59d4ad4d22SNikolay Aleksandrov static const char ip6_frag_cache_name[] = "ip6-frags"; 60d4ad4d22SNikolay Aleksandrov 61fc08c258SFabian Frederick static u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h) 62eec2e618SHannes Frederic Sowa { 63eec2e618SHannes Frederic Sowa return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK); 64eec2e618SHannes Frederic Sowa } 651da177e4SLinus Torvalds 667eb95156SPavel Emelyanov static struct inet_frags ip6_frags; 671da177e4SLinus Torvalds 68d4289fccSPeter Oskolkov static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb, 69d4289fccSPeter Oskolkov struct sk_buff *prev_tail, struct net_device *dev); 70f61944efSHerbert Xu 7178802011SKees Cook static void ip6_frag_expire(struct timer_list *t) 72b836c99fSAmerigo Wang { 7378802011SKees Cook struct inet_frag_queue *frag = from_timer(frag, t, timer); 74b836c99fSAmerigo Wang struct frag_queue *fq; 75b836c99fSAmerigo Wang struct net *net; 76b836c99fSAmerigo Wang 7778802011SKees Cook fq = container_of(frag, struct frag_queue, q); 78b836c99fSAmerigo Wang net = container_of(fq->q.net, struct net, ipv6.frags); 79b836c99fSAmerigo Wang 8070b095c8SFlorian Westphal ip6frag_expire_frag_queue(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 99648700f7SEric Dumazet q = inet_frag_find(&net->ipv6.frags, &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; 1990e60d245SFlorian Westphal add_frag_mem_limit(fq->q.net, 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 { 2532bad35b7SJorge Boncompte [DTI2] struct net *net = container_of(fq->q.net, struct net, ipv6.frags); 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 287d4289fccSPeter Oskolkov inet_frag_reasm_finish(&fq->q, skb, reasm_data); 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(); 3011d015503SEric Dumazet __IP6_INC_STATS(net, __in6_dev_get(dev), 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(); 3151d015503SEric Dumazet __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS); 316a11d206dSYOSHIFUJI Hideaki rcu_read_unlock(); 3172475f59cSPeter Oskolkov inet_frag_kill(&fq->q); 3181da177e4SLinus Torvalds return -1; 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds 321e5bbef20SHerbert Xu static int ipv6_frag_rcv(struct sk_buff *skb) 3221da177e4SLinus Torvalds { 3231da177e4SLinus Torvalds struct frag_hdr *fhdr; 3241da177e4SLinus Torvalds struct frag_queue *fq; 325b71d1d42SEric Dumazet const struct ipv6hdr *hdr = ipv6_hdr(skb); 326adf30907SEric Dumazet struct net *net = dev_net(skb_dst(skb)->dev); 327648700f7SEric Dumazet int iif; 3281da177e4SLinus Torvalds 329f46078cfSHannes Frederic Sowa if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED) 330f46078cfSHannes Frederic Sowa goto fail_hdr; 331f46078cfSHannes Frederic Sowa 3321d015503SEric Dumazet __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS); 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds /* Jumbo payload inhibits frag. header */ 33598b3377cSDenis V. Lunev if (hdr->payload_len == 0) 33698b3377cSDenis V. Lunev goto fail_hdr; 33798b3377cSDenis V. Lunev 338ea2ae17dSArnaldo Carvalho de Melo if (!pskb_may_pull(skb, (skb_transport_offset(skb) + 33998b3377cSDenis V. Lunev sizeof(struct frag_hdr)))) 34098b3377cSDenis V. Lunev goto fail_hdr; 3411da177e4SLinus Torvalds 3420660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 3439c70220bSArnaldo Carvalho de Melo fhdr = (struct frag_hdr *)skb_transport_header(skb); 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds if (!(fhdr->frag_off & htons(0xFFF9))) { 3461da177e4SLinus Torvalds /* It is not a fragmented frame */ 347b0e380b1SArnaldo Carvalho de Melo skb->transport_header += sizeof(struct frag_hdr); 3481d015503SEric Dumazet __IP6_INC_STATS(net, 349adf30907SEric Dumazet ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS); 3501da177e4SLinus Torvalds 351d56f90a7SArnaldo Carvalho de Melo IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb); 352f46078cfSHannes Frederic Sowa IP6CB(skb)->flags |= IP6SKB_FRAGMENTED; 3531da177e4SLinus Torvalds return 1; 3541da177e4SLinus Torvalds } 3551da177e4SLinus Torvalds 356648700f7SEric Dumazet iif = skb->dev ? skb->dev->ifindex : 0; 357648700f7SEric Dumazet fq = fq_find(net, fhdr->identification, hdr, iif); 35853b24b8fSIan Morris if (fq) { 359415787d7SEric Dumazet u32 prob_offset = 0; 360f61944efSHerbert Xu int ret; 3611da177e4SLinus Torvalds 3625ab11c98SPavel Emelyanov spin_lock(&fq->q.lock); 3631da177e4SLinus Torvalds 364648700f7SEric Dumazet fq->iif = iif; 365415787d7SEric Dumazet ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff, 366415787d7SEric Dumazet &prob_offset); 3671da177e4SLinus Torvalds 3685ab11c98SPavel Emelyanov spin_unlock(&fq->q.lock); 369093ba729SEric Dumazet inet_frag_put(&fq->q); 370415787d7SEric Dumazet if (prob_offset) { 371415787d7SEric Dumazet __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev), 372415787d7SEric Dumazet IPSTATS_MIB_INHDRERRORS); 373d4289fccSPeter Oskolkov /* icmpv6_param_prob() calls kfree_skb(skb) */ 374415787d7SEric Dumazet icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, prob_offset); 375415787d7SEric Dumazet } 3761da177e4SLinus Torvalds return ret; 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds 3791d015503SEric Dumazet __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS); 3801da177e4SLinus Torvalds kfree_skb(skb); 3811da177e4SLinus Torvalds return -1; 38298b3377cSDenis V. Lunev 38398b3377cSDenis V. Lunev fail_hdr: 384bdb7cc64SStephen Suryaputra __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev), 385d2373862SNikolay Aleksandrov IPSTATS_MIB_INHDRERRORS); 38698b3377cSDenis V. Lunev icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb)); 38798b3377cSDenis V. Lunev return -1; 3881da177e4SLinus Torvalds } 3891da177e4SLinus Torvalds 390cc24becaSIan Morris static const struct inet6_protocol frag_protocol = { 3911da177e4SLinus Torvalds .handler = ipv6_frag_rcv, 3921da177e4SLinus Torvalds .flags = INET6_PROTO_NOPOLICY, 3931da177e4SLinus Torvalds }; 3941da177e4SLinus Torvalds 3958d8354d2SPavel Emelyanov #ifdef CONFIG_SYSCTL 3961bab4c75SNikolay Aleksandrov 3970a64b4b8SPavel Emelyanov static struct ctl_table ip6_frags_ns_ctl_table[] = { 398e71e0349SDaniel Lezcano { 3998d8354d2SPavel Emelyanov .procname = "ip6frag_high_thresh", 400e31e0bdcSPavel Emelyanov .data = &init_net.ipv6.frags.high_thresh, 4013e67f106SEric Dumazet .maxlen = sizeof(unsigned long), 4028d8354d2SPavel Emelyanov .mode = 0644, 4033e67f106SEric Dumazet .proc_handler = proc_doulongvec_minmax, 4041bab4c75SNikolay Aleksandrov .extra1 = &init_net.ipv6.frags.low_thresh 4058d8354d2SPavel Emelyanov }, 4068d8354d2SPavel Emelyanov { 4078d8354d2SPavel Emelyanov .procname = "ip6frag_low_thresh", 408e31e0bdcSPavel Emelyanov .data = &init_net.ipv6.frags.low_thresh, 4093e67f106SEric Dumazet .maxlen = sizeof(unsigned long), 4108d8354d2SPavel Emelyanov .mode = 0644, 4116e00f7ddSEric Dumazet .proc_handler = proc_doulongvec_minmax, 4121bab4c75SNikolay Aleksandrov .extra2 = &init_net.ipv6.frags.high_thresh 4138d8354d2SPavel Emelyanov }, 4148d8354d2SPavel Emelyanov { 4158d8354d2SPavel Emelyanov .procname = "ip6frag_time", 416b2fd5321SPavel Emelyanov .data = &init_net.ipv6.frags.timeout, 4178d8354d2SPavel Emelyanov .maxlen = sizeof(int), 4188d8354d2SPavel Emelyanov .mode = 0644, 4196d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 4208d8354d2SPavel Emelyanov }, 4217d291ebbSPavel Emelyanov { } 4227d291ebbSPavel Emelyanov }; 4237d291ebbSPavel Emelyanov 424e3a57d18SFlorian Westphal /* secret interval has been deprecated */ 425e3a57d18SFlorian Westphal static int ip6_frags_secret_interval_unused; 4267d291ebbSPavel Emelyanov static struct ctl_table ip6_frags_ctl_table[] = { 4278d8354d2SPavel Emelyanov { 4288d8354d2SPavel Emelyanov .procname = "ip6frag_secret_interval", 429e3a57d18SFlorian Westphal .data = &ip6_frags_secret_interval_unused, 4308d8354d2SPavel Emelyanov .maxlen = sizeof(int), 4318d8354d2SPavel Emelyanov .mode = 0644, 4326d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 4338d8354d2SPavel Emelyanov }, 4348d8354d2SPavel Emelyanov { } 4358d8354d2SPavel Emelyanov }; 4367d460db9SDaniel Lezcano 4372c8c1e72SAlexey Dobriyan static int __net_init ip6_frags_ns_sysctl_register(struct net *net) 4388d8354d2SPavel Emelyanov { 439e4a2d5c2SPavel Emelyanov struct ctl_table *table; 4408d8354d2SPavel Emelyanov struct ctl_table_header *hdr; 4418d8354d2SPavel Emelyanov 4420a64b4b8SPavel Emelyanov table = ip6_frags_ns_ctl_table; 44309ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) { 4440a64b4b8SPavel Emelyanov table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL); 44563159f29SIan Morris if (!table) 446e4a2d5c2SPavel Emelyanov goto err_alloc; 447e4a2d5c2SPavel Emelyanov 448e31e0bdcSPavel Emelyanov table[0].data = &net->ipv6.frags.high_thresh; 4491bab4c75SNikolay Aleksandrov table[0].extra1 = &net->ipv6.frags.low_thresh; 450e31e0bdcSPavel Emelyanov table[1].data = &net->ipv6.frags.low_thresh; 4511bab4c75SNikolay Aleksandrov table[1].extra2 = &net->ipv6.frags.high_thresh; 452b2fd5321SPavel Emelyanov table[2].data = &net->ipv6.frags.timeout; 453e4a2d5c2SPavel Emelyanov } 454e4a2d5c2SPavel Emelyanov 455ec8f23ceSEric W. Biederman hdr = register_net_sysctl(net, "net/ipv6", table); 45663159f29SIan Morris if (!hdr) 457e4a2d5c2SPavel Emelyanov goto err_reg; 458e4a2d5c2SPavel Emelyanov 459e4a2d5c2SPavel Emelyanov net->ipv6.sysctl.frags_hdr = hdr; 460e4a2d5c2SPavel Emelyanov return 0; 461e4a2d5c2SPavel Emelyanov 462e4a2d5c2SPavel Emelyanov err_reg: 46309ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) 464e4a2d5c2SPavel Emelyanov kfree(table); 465e4a2d5c2SPavel Emelyanov err_alloc: 466e4a2d5c2SPavel Emelyanov return -ENOMEM; 467e4a2d5c2SPavel Emelyanov } 468e4a2d5c2SPavel Emelyanov 4692c8c1e72SAlexey Dobriyan static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net) 470e4a2d5c2SPavel Emelyanov { 471e4a2d5c2SPavel Emelyanov struct ctl_table *table; 472e4a2d5c2SPavel Emelyanov 473e4a2d5c2SPavel Emelyanov table = net->ipv6.sysctl.frags_hdr->ctl_table_arg; 474e4a2d5c2SPavel Emelyanov unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr); 4753705e11aSYang Hongyang if (!net_eq(net, &init_net)) 476e4a2d5c2SPavel Emelyanov kfree(table); 4778d8354d2SPavel Emelyanov } 4787d291ebbSPavel Emelyanov 4797d291ebbSPavel Emelyanov static struct ctl_table_header *ip6_ctl_header; 4807d291ebbSPavel Emelyanov 4817d291ebbSPavel Emelyanov static int ip6_frags_sysctl_register(void) 4827d291ebbSPavel Emelyanov { 48343444757SEric W. Biederman ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6", 4847d291ebbSPavel Emelyanov ip6_frags_ctl_table); 4857d291ebbSPavel Emelyanov return ip6_ctl_header == NULL ? -ENOMEM : 0; 4867d291ebbSPavel Emelyanov } 4877d291ebbSPavel Emelyanov 4887d291ebbSPavel Emelyanov static void ip6_frags_sysctl_unregister(void) 4897d291ebbSPavel Emelyanov { 4907d291ebbSPavel Emelyanov unregister_net_sysctl_table(ip6_ctl_header); 4917d291ebbSPavel Emelyanov } 4928d8354d2SPavel Emelyanov #else 493fc08c258SFabian Frederick static int ip6_frags_ns_sysctl_register(struct net *net) 4948d8354d2SPavel Emelyanov { 4958d8354d2SPavel Emelyanov return 0; 4968d8354d2SPavel Emelyanov } 497e4a2d5c2SPavel Emelyanov 498fc08c258SFabian Frederick static void ip6_frags_ns_sysctl_unregister(struct net *net) 499e4a2d5c2SPavel Emelyanov { 500e4a2d5c2SPavel Emelyanov } 5017d291ebbSPavel Emelyanov 502fc08c258SFabian Frederick static int ip6_frags_sysctl_register(void) 5037d291ebbSPavel Emelyanov { 5047d291ebbSPavel Emelyanov return 0; 5057d291ebbSPavel Emelyanov } 5067d291ebbSPavel Emelyanov 507fc08c258SFabian Frederick static void ip6_frags_sysctl_unregister(void) 5087d291ebbSPavel Emelyanov { 5097d291ebbSPavel Emelyanov } 5108d8354d2SPavel Emelyanov #endif 5118d8354d2SPavel Emelyanov 5122c8c1e72SAlexey Dobriyan static int __net_init ipv6_frags_init_net(struct net *net) 5138d8354d2SPavel Emelyanov { 514787bea77SEric Dumazet int res; 515787bea77SEric Dumazet 5167c070aa9SShan Wei net->ipv6.frags.high_thresh = IPV6_FRAG_HIGH_THRESH; 5177c070aa9SShan Wei net->ipv6.frags.low_thresh = IPV6_FRAG_LOW_THRESH; 518b2fd5321SPavel Emelyanov net->ipv6.frags.timeout = IPV6_FRAG_TIMEOUT; 519093ba729SEric Dumazet net->ipv6.frags.f = &ip6_frags; 5208d8354d2SPavel Emelyanov 521787bea77SEric Dumazet res = inet_frags_init_net(&net->ipv6.frags); 522787bea77SEric Dumazet if (res < 0) 523787bea77SEric Dumazet return res; 5245a63643eSJesper Dangaard Brouer 525787bea77SEric Dumazet res = ip6_frags_ns_sysctl_register(net); 526787bea77SEric Dumazet if (res < 0) 527093ba729SEric Dumazet inet_frags_exit_net(&net->ipv6.frags); 528787bea77SEric Dumazet return res; 529e71e0349SDaniel Lezcano } 530e71e0349SDaniel Lezcano 5312c8c1e72SAlexey Dobriyan static void __net_exit ipv6_frags_exit_net(struct net *net) 53281566e83SPavel Emelyanov { 5330a64b4b8SPavel Emelyanov ip6_frags_ns_sysctl_unregister(net); 534093ba729SEric Dumazet inet_frags_exit_net(&net->ipv6.frags); 53581566e83SPavel Emelyanov } 53681566e83SPavel Emelyanov 53781566e83SPavel Emelyanov static struct pernet_operations ip6_frags_ops = { 53881566e83SPavel Emelyanov .init = ipv6_frags_init_net, 53981566e83SPavel Emelyanov .exit = ipv6_frags_exit_net, 54081566e83SPavel Emelyanov }; 54181566e83SPavel Emelyanov 54270b095c8SFlorian Westphal static const struct rhashtable_params ip6_rhash_params = { 543648700f7SEric Dumazet .head_offset = offsetof(struct inet_frag_queue, node), 54470b095c8SFlorian Westphal .hashfn = ip6frag_key_hashfn, 54570b095c8SFlorian Westphal .obj_hashfn = ip6frag_obj_hashfn, 54670b095c8SFlorian Westphal .obj_cmpfn = ip6frag_obj_cmpfn, 547648700f7SEric Dumazet .automatic_shrinking = true, 548648700f7SEric Dumazet }; 549648700f7SEric Dumazet 550853cbbaaSDaniel Lezcano int __init ipv6_frag_init(void) 5511da177e4SLinus Torvalds { 552853cbbaaSDaniel Lezcano int ret; 5531da177e4SLinus Torvalds 55470b095c8SFlorian Westphal ip6_frags.constructor = ip6frag_init; 555c9547709SPavel Emelyanov ip6_frags.destructor = NULL; 5561e4b8287SPavel Emelyanov ip6_frags.qsize = sizeof(struct frag_queue); 557e521db9dSPavel Emelyanov ip6_frags.frag_expire = ip6_frag_expire; 558d4ad4d22SNikolay Aleksandrov ip6_frags.frags_cache_name = ip6_frag_cache_name; 559648700f7SEric Dumazet ip6_frags.rhash_params = ip6_rhash_params; 560d4ad4d22SNikolay Aleksandrov ret = inet_frags_init(&ip6_frags); 561d4ad4d22SNikolay Aleksandrov if (ret) 5625b975babSEric Dumazet goto out; 5635b975babSEric Dumazet 5645b975babSEric Dumazet ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT); 5655b975babSEric Dumazet if (ret) 5665b975babSEric Dumazet goto err_protocol; 5675b975babSEric Dumazet 5685b975babSEric Dumazet ret = ip6_frags_sysctl_register(); 5695b975babSEric Dumazet if (ret) 5705b975babSEric Dumazet goto err_sysctl; 5715b975babSEric Dumazet 5725b975babSEric Dumazet ret = register_pernet_subsys(&ip6_frags_ops); 5735b975babSEric Dumazet if (ret) 574d4ad4d22SNikolay Aleksandrov goto err_pernet; 5755b975babSEric Dumazet 576853cbbaaSDaniel Lezcano out: 577853cbbaaSDaniel Lezcano return ret; 5780002c630SPavel Emelyanov 5790002c630SPavel Emelyanov err_pernet: 5807d291ebbSPavel Emelyanov ip6_frags_sysctl_unregister(); 5817d291ebbSPavel Emelyanov err_sysctl: 5820002c630SPavel Emelyanov inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT); 5835b975babSEric Dumazet err_protocol: 5845b975babSEric Dumazet inet_frags_fini(&ip6_frags); 5850002c630SPavel Emelyanov goto out; 586853cbbaaSDaniel Lezcano } 587853cbbaaSDaniel Lezcano 588853cbbaaSDaniel Lezcano void ipv6_frag_exit(void) 589853cbbaaSDaniel Lezcano { 590853cbbaaSDaniel Lezcano inet_frags_fini(&ip6_frags); 5917d291ebbSPavel Emelyanov ip6_frags_sysctl_unregister(); 59281566e83SPavel Emelyanov unregister_pernet_subsys(&ip6_frags_ops); 593853cbbaaSDaniel Lezcano inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT); 5941da177e4SLinus Torvalds } 595