11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * IPv6 fragment reassembly 31da177e4SLinus Torvalds * Linux INET6 implementation 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Authors: 61da177e4SLinus Torvalds * Pedro Roque <roque@di.fc.ul.pt> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Based on: net/ipv4/ip_fragment.c 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 111da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 121da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 131da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 141da177e4SLinus Torvalds */ 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds /* 171da177e4SLinus Torvalds * Fixes: 181da177e4SLinus Torvalds * Andi Kleen Make it work with multiple hosts. 191da177e4SLinus Torvalds * More RFC compliance. 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * Horst von Brand Add missing #include <linux/string.h> 221da177e4SLinus Torvalds * Alexey Kuznetsov SMP races, threading, cleanup. 231da177e4SLinus Torvalds * Patrick McHardy LRU queue of frag heads for evictor. 241da177e4SLinus Torvalds * Mitsuru KANDA @USAGI Register inet6_protocol{}. 251da177e4SLinus Torvalds * David Stevens and 261da177e4SLinus Torvalds * YOSHIFUJI,H. @USAGI Always remove fragment header to 271da177e4SLinus Torvalds * calculate ICV correctly. 281da177e4SLinus Torvalds */ 295a3da1feSHannes Frederic Sowa 305a3da1feSHannes Frederic Sowa #define pr_fmt(fmt) "IPv6: " fmt 315a3da1feSHannes Frederic Sowa 321da177e4SLinus Torvalds #include <linux/errno.h> 331da177e4SLinus Torvalds #include <linux/types.h> 341da177e4SLinus Torvalds #include <linux/string.h> 351da177e4SLinus Torvalds #include <linux/socket.h> 361da177e4SLinus Torvalds #include <linux/sockios.h> 371da177e4SLinus Torvalds #include <linux/jiffies.h> 381da177e4SLinus Torvalds #include <linux/net.h> 391da177e4SLinus Torvalds #include <linux/list.h> 401da177e4SLinus Torvalds #include <linux/netdevice.h> 411da177e4SLinus Torvalds #include <linux/in6.h> 421da177e4SLinus Torvalds #include <linux/ipv6.h> 431da177e4SLinus Torvalds #include <linux/icmpv6.h> 441da177e4SLinus Torvalds #include <linux/random.h> 451da177e4SLinus Torvalds #include <linux/jhash.h> 46f61944efSHerbert Xu #include <linux/skbuff.h> 475a0e3ad6STejun Heo #include <linux/slab.h> 48bc3b2d7fSPaul Gortmaker #include <linux/export.h> 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds #include <net/sock.h> 511da177e4SLinus Torvalds #include <net/snmp.h> 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds #include <net/ipv6.h> 54a11d206dSYOSHIFUJI Hideaki #include <net/ip6_route.h> 551da177e4SLinus Torvalds #include <net/protocol.h> 561da177e4SLinus Torvalds #include <net/transp_v6.h> 571da177e4SLinus Torvalds #include <net/rawv6.h> 581da177e4SLinus Torvalds #include <net/ndisc.h> 591da177e4SLinus Torvalds #include <net/addrconf.h> 6070b095c8SFlorian Westphal #include <net/ipv6_frag.h> 61eec2e618SHannes Frederic Sowa #include <net/inet_ecn.h> 621da177e4SLinus Torvalds 63d4ad4d22SNikolay Aleksandrov static const char ip6_frag_cache_name[] = "ip6-frags"; 64d4ad4d22SNikolay Aleksandrov 65fc08c258SFabian Frederick static u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h) 66eec2e618SHannes Frederic Sowa { 67eec2e618SHannes Frederic Sowa return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK); 68eec2e618SHannes Frederic Sowa } 691da177e4SLinus Torvalds 707eb95156SPavel Emelyanov static struct inet_frags ip6_frags; 711da177e4SLinus Torvalds 72d4289fccSPeter Oskolkov static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb, 73d4289fccSPeter Oskolkov struct sk_buff *prev_tail, struct net_device *dev); 74f61944efSHerbert Xu 7578802011SKees Cook static void ip6_frag_expire(struct timer_list *t) 76b836c99fSAmerigo Wang { 7778802011SKees Cook struct inet_frag_queue *frag = from_timer(frag, t, timer); 78b836c99fSAmerigo Wang struct frag_queue *fq; 79b836c99fSAmerigo Wang struct net *net; 80b836c99fSAmerigo Wang 8178802011SKees Cook fq = container_of(frag, struct frag_queue, q); 82803fdd99SEric Dumazet net = container_of(fq->q.fqdir, struct net, ipv6.fqdir); 83b836c99fSAmerigo Wang 8470b095c8SFlorian Westphal ip6frag_expire_frag_queue(net, fq); 851da177e4SLinus Torvalds } 861da177e4SLinus Torvalds 87fc08c258SFabian Frederick static struct frag_queue * 88648700f7SEric Dumazet fq_find(struct net *net, __be32 id, const struct ipv6hdr *hdr, int iif) 891da177e4SLinus Torvalds { 90648700f7SEric Dumazet struct frag_v6_compare_key key = { 91648700f7SEric Dumazet .id = id, 92648700f7SEric Dumazet .saddr = hdr->saddr, 93648700f7SEric Dumazet .daddr = hdr->daddr, 94648700f7SEric Dumazet .user = IP6_DEFRAG_LOCAL_DELIVER, 95648700f7SEric Dumazet .iif = iif, 96648700f7SEric Dumazet }; 97c6fda282SPavel Emelyanov struct inet_frag_queue *q; 981da177e4SLinus Torvalds 99648700f7SEric Dumazet if (!(ipv6_addr_type(&hdr->daddr) & (IPV6_ADDR_MULTICAST | 100648700f7SEric Dumazet IPV6_ADDR_LINKLOCAL))) 101648700f7SEric Dumazet key.iif = 0; 1029a375803SPavel Emelyanov 103803fdd99SEric Dumazet q = inet_frag_find(&net->ipv6.fqdir, &key); 1042d44ed22SEric Dumazet if (!q) 1059546377cSShan Wei return NULL; 1062d44ed22SEric Dumazet 107c6fda282SPavel Emelyanov return container_of(q, struct frag_queue, q); 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds 110f61944efSHerbert Xu static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb, 111415787d7SEric Dumazet struct frag_hdr *fhdr, int nhoff, 112415787d7SEric Dumazet u32 *prob_offset) 1131da177e4SLinus Torvalds { 114adf30907SEric Dumazet struct net *net = dev_net(skb_dst(skb)->dev); 115d4289fccSPeter Oskolkov int offset, end, fragsize; 116d4289fccSPeter Oskolkov struct sk_buff *prev_tail; 117d4289fccSPeter Oskolkov struct net_device *dev; 118d4289fccSPeter Oskolkov int err = -ENOENT; 119eec2e618SHannes Frederic Sowa u8 ecn; 1201da177e4SLinus Torvalds 12106aa8b8aSNikolay Aleksandrov if (fq->q.flags & INET_FRAG_COMPLETE) 1221da177e4SLinus Torvalds goto err; 1231da177e4SLinus Torvalds 124d4289fccSPeter Oskolkov err = -EINVAL; 1251da177e4SLinus Torvalds offset = ntohs(fhdr->frag_off) & ~0x7; 1260660e03fSArnaldo Carvalho de Melo end = offset + (ntohs(ipv6_hdr(skb)->payload_len) - 1270660e03fSArnaldo Carvalho de Melo ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1))); 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds if ((unsigned int)end > IPV6_MAXPLEN) { 130415787d7SEric Dumazet *prob_offset = (u8 *)&fhdr->frag_off - skb_network_header(skb); 131d4289fccSPeter Oskolkov /* note that if prob_offset is set, the skb is freed elsewhere, 132d4289fccSPeter Oskolkov * we do not free it here. 133d4289fccSPeter Oskolkov */ 134f61944efSHerbert Xu return -1; 1351da177e4SLinus Torvalds } 1361da177e4SLinus Torvalds 137eec2e618SHannes Frederic Sowa ecn = ip6_frag_ecn(ipv6_hdr(skb)); 138eec2e618SHannes Frederic Sowa 139d56f90a7SArnaldo Carvalho de Melo if (skb->ip_summed == CHECKSUM_COMPLETE) { 140d56f90a7SArnaldo Carvalho de Melo const unsigned char *nh = skb_network_header(skb); 1411da177e4SLinus Torvalds skb->csum = csum_sub(skb->csum, 142d56f90a7SArnaldo Carvalho de Melo csum_partial(nh, (u8 *)(fhdr + 1) - nh, 143d56f90a7SArnaldo Carvalho de Melo 0)); 144d56f90a7SArnaldo Carvalho de Melo } 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds /* Is this the final fragment? */ 1471da177e4SLinus Torvalds if (!(fhdr->frag_off & htons(IP6_MF))) { 1481da177e4SLinus Torvalds /* If we already have some bits beyond end 1491da177e4SLinus Torvalds * or have different end, the segment is corrupted. 1501da177e4SLinus Torvalds */ 1515ab11c98SPavel Emelyanov if (end < fq->q.len || 15206aa8b8aSNikolay Aleksandrov ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len)) 1532475f59cSPeter Oskolkov goto discard_fq; 15406aa8b8aSNikolay Aleksandrov fq->q.flags |= INET_FRAG_LAST_IN; 1555ab11c98SPavel Emelyanov fq->q.len = end; 1561da177e4SLinus Torvalds } else { 1571da177e4SLinus Torvalds /* Check if the fragment is rounded to 8 bytes. 1581da177e4SLinus Torvalds * Required by the RFC. 1591da177e4SLinus Torvalds */ 1601da177e4SLinus Torvalds if (end & 0x7) { 1611da177e4SLinus Torvalds /* RFC2460 says always send parameter problem in 1621da177e4SLinus Torvalds * this case. -DaveM 1631da177e4SLinus Torvalds */ 164415787d7SEric Dumazet *prob_offset = offsetof(struct ipv6hdr, payload_len); 165f61944efSHerbert Xu return -1; 1661da177e4SLinus Torvalds } 1675ab11c98SPavel Emelyanov if (end > fq->q.len) { 1681da177e4SLinus Torvalds /* Some bits beyond end -> corruption. */ 16906aa8b8aSNikolay Aleksandrov if (fq->q.flags & INET_FRAG_LAST_IN) 1702475f59cSPeter Oskolkov goto discard_fq; 1715ab11c98SPavel Emelyanov fq->q.len = end; 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds if (end == offset) 1762475f59cSPeter Oskolkov goto discard_fq; 1771da177e4SLinus Torvalds 178d4289fccSPeter Oskolkov err = -ENOMEM; 1791da177e4SLinus Torvalds /* Point into the IP datagram 'data' part. */ 1801da177e4SLinus Torvalds if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) 1812475f59cSPeter Oskolkov goto discard_fq; 18242ca89c1SStephen Hemminger 183d4289fccSPeter Oskolkov err = pskb_trim_rcsum(skb, end - offset); 184d4289fccSPeter Oskolkov if (err) 1852475f59cSPeter Oskolkov goto discard_fq; 1861da177e4SLinus Torvalds 187d4289fccSPeter Oskolkov /* Note : skb->rbnode and skb->dev share the same location. */ 188219badfaSEric Dumazet dev = skb->dev; 189219badfaSEric Dumazet /* Makes sure compiler wont do silly aliasing games */ 190219badfaSEric Dumazet barrier(); 1911da177e4SLinus Torvalds 192d4289fccSPeter Oskolkov prev_tail = fq->q.fragments_tail; 193d4289fccSPeter Oskolkov err = inet_frag_queue_insert(&fq->q, skb, offset, end); 194d4289fccSPeter Oskolkov if (err) 195d4289fccSPeter Oskolkov goto insert_error; 196d4289fccSPeter Oskolkov 197d4289fccSPeter Oskolkov if (dev) 198d4289fccSPeter Oskolkov fq->iif = dev->ifindex; 1991da177e4SLinus Torvalds 2005ab11c98SPavel Emelyanov fq->q.stamp = skb->tstamp; 2015ab11c98SPavel Emelyanov fq->q.meat += skb->len; 202eec2e618SHannes Frederic Sowa fq->ecn |= ecn; 2036ce3b4dcSEric Dumazet add_frag_mem_limit(fq->q.fqdir, skb->truesize); 2041da177e4SLinus Torvalds 205dbd1759eSWillem de Bruijn fragsize = -skb_network_offset(skb) + skb->len; 206dbd1759eSWillem de Bruijn if (fragsize > fq->q.max_size) 207dbd1759eSWillem de Bruijn fq->q.max_size = fragsize; 208dbd1759eSWillem de Bruijn 2091da177e4SLinus Torvalds /* The first fragment. 2101da177e4SLinus Torvalds * nhoffset is obtained from the first fragment, of course. 2111da177e4SLinus Torvalds */ 2121da177e4SLinus Torvalds if (offset == 0) { 2131da177e4SLinus Torvalds fq->nhoffset = nhoff; 21406aa8b8aSNikolay Aleksandrov fq->q.flags |= INET_FRAG_FIRST_IN; 2151da177e4SLinus Torvalds } 216f61944efSHerbert Xu 21706aa8b8aSNikolay Aleksandrov if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) && 21897599dc7SEric Dumazet fq->q.meat == fq->q.len) { 21997599dc7SEric Dumazet unsigned long orefdst = skb->_skb_refdst; 220f61944efSHerbert Xu 22197599dc7SEric Dumazet skb->_skb_refdst = 0UL; 222d4289fccSPeter Oskolkov err = ip6_frag_reasm(fq, skb, prev_tail, dev); 22397599dc7SEric Dumazet skb->_skb_refdst = orefdst; 224d4289fccSPeter Oskolkov return err; 22597599dc7SEric Dumazet } 22697599dc7SEric Dumazet 22797599dc7SEric Dumazet skb_dst_drop(skb); 228d4289fccSPeter Oskolkov return -EINPROGRESS; 2291da177e4SLinus Torvalds 230d4289fccSPeter Oskolkov insert_error: 231d4289fccSPeter Oskolkov if (err == IPFRAG_DUP) { 232d4289fccSPeter Oskolkov kfree_skb(skb); 233d4289fccSPeter Oskolkov return -EINVAL; 234d4289fccSPeter Oskolkov } 235d4289fccSPeter Oskolkov err = -EINVAL; 236d4289fccSPeter Oskolkov __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), 237d4289fccSPeter Oskolkov IPSTATS_MIB_REASM_OVERLAPS); 23870789d70SNicolas Dichtel discard_fq: 239093ba729SEric Dumazet inet_frag_kill(&fq->q); 2401d015503SEric Dumazet __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), 2413bd653c8SDenis V. Lunev IPSTATS_MIB_REASMFAILS); 242d4289fccSPeter Oskolkov err: 2431da177e4SLinus Torvalds kfree_skb(skb); 244d4289fccSPeter Oskolkov return err; 2451da177e4SLinus Torvalds } 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds /* 2481da177e4SLinus Torvalds * Check if this packet is complete. 2491da177e4SLinus Torvalds * 2501da177e4SLinus Torvalds * It is called with locked fq, and caller must check that 2511da177e4SLinus Torvalds * queue is eligible for reassembly i.e. it is not COMPLETE, 2521da177e4SLinus Torvalds * the last and the first frames arrived and all the bits are here. 2531da177e4SLinus Torvalds */ 254d4289fccSPeter Oskolkov static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *skb, 255d4289fccSPeter Oskolkov struct sk_buff *prev_tail, struct net_device *dev) 2561da177e4SLinus Torvalds { 257803fdd99SEric Dumazet struct net *net = container_of(fq->q.fqdir, struct net, ipv6.fqdir); 2581da177e4SLinus Torvalds unsigned int nhoff; 259d4289fccSPeter Oskolkov void *reasm_data; 260d4289fccSPeter Oskolkov int payload_len; 261eec2e618SHannes Frederic Sowa u8 ecn; 2621da177e4SLinus Torvalds 263093ba729SEric Dumazet inet_frag_kill(&fq->q); 2641da177e4SLinus Torvalds 265eec2e618SHannes Frederic Sowa ecn = ip_frag_ecn_table[fq->ecn]; 266eec2e618SHannes Frederic Sowa if (unlikely(ecn == 0xff)) 267eec2e618SHannes Frederic Sowa goto out_fail; 268eec2e618SHannes Frederic Sowa 269d4289fccSPeter Oskolkov reasm_data = inet_frag_reasm_prepare(&fq->q, skb, prev_tail); 270d4289fccSPeter Oskolkov if (!reasm_data) 271f61944efSHerbert Xu goto out_oom; 272f61944efSHerbert Xu 273d4289fccSPeter Oskolkov payload_len = ((skb->data - skb_network_header(skb)) - 2745ab11c98SPavel Emelyanov sizeof(struct ipv6hdr) + fq->q.len - 275d56f90a7SArnaldo Carvalho de Melo sizeof(struct frag_hdr)); 2761da177e4SLinus Torvalds if (payload_len > IPV6_MAXPLEN) 2771da177e4SLinus Torvalds goto out_oversize; 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds /* We have to remove fragment header from datagram and to relocate 2801da177e4SLinus Torvalds * header in order to calculate ICV correctly. */ 2811da177e4SLinus Torvalds nhoff = fq->nhoffset; 282d4289fccSPeter Oskolkov skb_network_header(skb)[nhoff] = skb_transport_header(skb)[0]; 283d4289fccSPeter Oskolkov memmove(skb->head + sizeof(struct frag_hdr), skb->head, 284d4289fccSPeter Oskolkov (skb->data - skb->head) - sizeof(struct frag_hdr)); 285d4289fccSPeter Oskolkov if (skb_mac_header_was_set(skb)) 286d4289fccSPeter Oskolkov skb->mac_header += sizeof(struct frag_hdr); 287d4289fccSPeter Oskolkov skb->network_header += sizeof(struct frag_hdr); 2881da177e4SLinus Torvalds 289d4289fccSPeter Oskolkov skb_reset_transport_header(skb); 2901da177e4SLinus Torvalds 291d4289fccSPeter Oskolkov inet_frag_reasm_finish(&fq->q, skb, reasm_data); 292ec16439eSEric Dumazet 293d4289fccSPeter Oskolkov skb->dev = dev; 294d4289fccSPeter Oskolkov ipv6_hdr(skb)->payload_len = htons(payload_len); 295d4289fccSPeter Oskolkov ipv6_change_dsfield(ipv6_hdr(skb), 0xff, ecn); 296d4289fccSPeter Oskolkov IP6CB(skb)->nhoff = nhoff; 297d4289fccSPeter Oskolkov IP6CB(skb)->flags |= IP6SKB_FRAGMENTED; 298d4289fccSPeter Oskolkov IP6CB(skb)->frag_max_size = fq->q.max_size; 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds /* Yes, and fold redundant checksum back. 8) */ 301d4289fccSPeter Oskolkov skb_postpush_rcsum(skb, skb_network_header(skb), 302d4289fccSPeter Oskolkov skb_network_header_len(skb)); 3031da177e4SLinus Torvalds 304a11d206dSYOSHIFUJI Hideaki rcu_read_lock(); 3051d015503SEric Dumazet __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS); 306a11d206dSYOSHIFUJI Hideaki rcu_read_unlock(); 307fa0f5273SPeter Oskolkov fq->q.rb_fragments = RB_ROOT; 308d6bebca9SChangli Gao fq->q.fragments_tail = NULL; 309d4289fccSPeter Oskolkov fq->q.last_run_head = NULL; 3101da177e4SLinus Torvalds return 1; 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds out_oversize: 313e87cc472SJoe Perches net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len); 3141da177e4SLinus Torvalds goto out_fail; 3151da177e4SLinus Torvalds out_oom: 316e87cc472SJoe Perches net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n"); 3171da177e4SLinus Torvalds out_fail: 318a11d206dSYOSHIFUJI Hideaki rcu_read_lock(); 3191d015503SEric Dumazet __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS); 320a11d206dSYOSHIFUJI Hideaki rcu_read_unlock(); 3212475f59cSPeter Oskolkov inet_frag_kill(&fq->q); 3221da177e4SLinus Torvalds return -1; 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds 325e5bbef20SHerbert Xu static int ipv6_frag_rcv(struct sk_buff *skb) 3261da177e4SLinus Torvalds { 3271da177e4SLinus Torvalds struct frag_hdr *fhdr; 3281da177e4SLinus Torvalds struct frag_queue *fq; 329b71d1d42SEric Dumazet const struct ipv6hdr *hdr = ipv6_hdr(skb); 330adf30907SEric Dumazet struct net *net = dev_net(skb_dst(skb)->dev); 331648700f7SEric Dumazet int iif; 3321da177e4SLinus Torvalds 333f46078cfSHannes Frederic Sowa if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED) 334f46078cfSHannes Frederic Sowa goto fail_hdr; 335f46078cfSHannes Frederic Sowa 3361d015503SEric Dumazet __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS); 3371da177e4SLinus Torvalds 3381da177e4SLinus Torvalds /* Jumbo payload inhibits frag. header */ 33998b3377cSDenis V. Lunev if (hdr->payload_len == 0) 34098b3377cSDenis V. Lunev goto fail_hdr; 34198b3377cSDenis V. Lunev 342ea2ae17dSArnaldo Carvalho de Melo if (!pskb_may_pull(skb, (skb_transport_offset(skb) + 34398b3377cSDenis V. Lunev sizeof(struct frag_hdr)))) 34498b3377cSDenis V. Lunev goto fail_hdr; 3451da177e4SLinus Torvalds 3460660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 3479c70220bSArnaldo Carvalho de Melo fhdr = (struct frag_hdr *)skb_transport_header(skb); 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds if (!(fhdr->frag_off & htons(0xFFF9))) { 3501da177e4SLinus Torvalds /* It is not a fragmented frame */ 351b0e380b1SArnaldo Carvalho de Melo skb->transport_header += sizeof(struct frag_hdr); 3521d015503SEric Dumazet __IP6_INC_STATS(net, 353adf30907SEric Dumazet ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS); 3541da177e4SLinus Torvalds 355d56f90a7SArnaldo Carvalho de Melo IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb); 356f46078cfSHannes Frederic Sowa IP6CB(skb)->flags |= IP6SKB_FRAGMENTED; 3571da177e4SLinus Torvalds return 1; 3581da177e4SLinus Torvalds } 3591da177e4SLinus Torvalds 360648700f7SEric Dumazet iif = skb->dev ? skb->dev->ifindex : 0; 361648700f7SEric Dumazet fq = fq_find(net, fhdr->identification, hdr, iif); 36253b24b8fSIan Morris if (fq) { 363415787d7SEric Dumazet u32 prob_offset = 0; 364f61944efSHerbert Xu int ret; 3651da177e4SLinus Torvalds 3665ab11c98SPavel Emelyanov spin_lock(&fq->q.lock); 3671da177e4SLinus Torvalds 368648700f7SEric Dumazet fq->iif = iif; 369415787d7SEric Dumazet ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff, 370415787d7SEric Dumazet &prob_offset); 3711da177e4SLinus Torvalds 3725ab11c98SPavel Emelyanov spin_unlock(&fq->q.lock); 373093ba729SEric Dumazet inet_frag_put(&fq->q); 374415787d7SEric Dumazet if (prob_offset) { 375415787d7SEric Dumazet __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev), 376415787d7SEric Dumazet IPSTATS_MIB_INHDRERRORS); 377d4289fccSPeter Oskolkov /* icmpv6_param_prob() calls kfree_skb(skb) */ 378415787d7SEric Dumazet icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, prob_offset); 379415787d7SEric Dumazet } 3801da177e4SLinus Torvalds return ret; 3811da177e4SLinus Torvalds } 3821da177e4SLinus Torvalds 3831d015503SEric Dumazet __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS); 3841da177e4SLinus Torvalds kfree_skb(skb); 3851da177e4SLinus Torvalds return -1; 38698b3377cSDenis V. Lunev 38798b3377cSDenis V. Lunev fail_hdr: 388bdb7cc64SStephen Suryaputra __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev), 389d2373862SNikolay Aleksandrov IPSTATS_MIB_INHDRERRORS); 39098b3377cSDenis V. Lunev icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb)); 39198b3377cSDenis V. Lunev return -1; 3921da177e4SLinus Torvalds } 3931da177e4SLinus Torvalds 394cc24becaSIan Morris static const struct inet6_protocol frag_protocol = { 3951da177e4SLinus Torvalds .handler = ipv6_frag_rcv, 3961da177e4SLinus Torvalds .flags = INET6_PROTO_NOPOLICY, 3971da177e4SLinus Torvalds }; 3981da177e4SLinus Torvalds 3998d8354d2SPavel Emelyanov #ifdef CONFIG_SYSCTL 4001bab4c75SNikolay Aleksandrov 4010a64b4b8SPavel Emelyanov static struct ctl_table ip6_frags_ns_ctl_table[] = { 402e71e0349SDaniel Lezcano { 4038d8354d2SPavel Emelyanov .procname = "ip6frag_high_thresh", 4043e67f106SEric Dumazet .maxlen = sizeof(unsigned long), 4058d8354d2SPavel Emelyanov .mode = 0644, 4063e67f106SEric Dumazet .proc_handler = proc_doulongvec_minmax, 4078d8354d2SPavel Emelyanov }, 4088d8354d2SPavel Emelyanov { 4098d8354d2SPavel Emelyanov .procname = "ip6frag_low_thresh", 4103e67f106SEric Dumazet .maxlen = sizeof(unsigned long), 4118d8354d2SPavel Emelyanov .mode = 0644, 4126e00f7ddSEric Dumazet .proc_handler = proc_doulongvec_minmax, 4138d8354d2SPavel Emelyanov }, 4148d8354d2SPavel Emelyanov { 4158d8354d2SPavel Emelyanov .procname = "ip6frag_time", 4168d8354d2SPavel Emelyanov .maxlen = sizeof(int), 4178d8354d2SPavel Emelyanov .mode = 0644, 4186d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 4198d8354d2SPavel Emelyanov }, 4207d291ebbSPavel Emelyanov { } 4217d291ebbSPavel Emelyanov }; 4227d291ebbSPavel Emelyanov 423e3a57d18SFlorian Westphal /* secret interval has been deprecated */ 424e3a57d18SFlorian Westphal static int ip6_frags_secret_interval_unused; 4257d291ebbSPavel Emelyanov static struct ctl_table ip6_frags_ctl_table[] = { 4268d8354d2SPavel Emelyanov { 4278d8354d2SPavel Emelyanov .procname = "ip6frag_secret_interval", 428e3a57d18SFlorian Westphal .data = &ip6_frags_secret_interval_unused, 4298d8354d2SPavel Emelyanov .maxlen = sizeof(int), 4308d8354d2SPavel Emelyanov .mode = 0644, 4316d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 4328d8354d2SPavel Emelyanov }, 4338d8354d2SPavel Emelyanov { } 4348d8354d2SPavel Emelyanov }; 4357d460db9SDaniel Lezcano 4362c8c1e72SAlexey Dobriyan static int __net_init ip6_frags_ns_sysctl_register(struct net *net) 4378d8354d2SPavel Emelyanov { 438e4a2d5c2SPavel Emelyanov struct ctl_table *table; 4398d8354d2SPavel Emelyanov struct ctl_table_header *hdr; 4408d8354d2SPavel Emelyanov 4410a64b4b8SPavel Emelyanov table = ip6_frags_ns_ctl_table; 44209ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) { 4430a64b4b8SPavel Emelyanov table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL); 44463159f29SIan Morris if (!table) 445e4a2d5c2SPavel Emelyanov goto err_alloc; 446e4a2d5c2SPavel Emelyanov 4478668d0e2SEric Dumazet } 448803fdd99SEric Dumazet table[0].data = &net->ipv6.fqdir.high_thresh; 449803fdd99SEric Dumazet table[0].extra1 = &net->ipv6.fqdir.low_thresh; 450803fdd99SEric Dumazet table[1].data = &net->ipv6.fqdir.low_thresh; 451803fdd99SEric Dumazet table[1].extra2 = &net->ipv6.fqdir.high_thresh; 452803fdd99SEric Dumazet table[2].data = &net->ipv6.fqdir.timeout; 453e4a2d5c2SPavel Emelyanov 454ec8f23ceSEric W. Biederman hdr = register_net_sysctl(net, "net/ipv6", table); 45563159f29SIan Morris if (!hdr) 456e4a2d5c2SPavel Emelyanov goto err_reg; 457e4a2d5c2SPavel Emelyanov 458e4a2d5c2SPavel Emelyanov net->ipv6.sysctl.frags_hdr = hdr; 459e4a2d5c2SPavel Emelyanov return 0; 460e4a2d5c2SPavel Emelyanov 461e4a2d5c2SPavel Emelyanov err_reg: 46209ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) 463e4a2d5c2SPavel Emelyanov kfree(table); 464e4a2d5c2SPavel Emelyanov err_alloc: 465e4a2d5c2SPavel Emelyanov return -ENOMEM; 466e4a2d5c2SPavel Emelyanov } 467e4a2d5c2SPavel Emelyanov 4682c8c1e72SAlexey Dobriyan static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net) 469e4a2d5c2SPavel Emelyanov { 470e4a2d5c2SPavel Emelyanov struct ctl_table *table; 471e4a2d5c2SPavel Emelyanov 472e4a2d5c2SPavel Emelyanov table = net->ipv6.sysctl.frags_hdr->ctl_table_arg; 473e4a2d5c2SPavel Emelyanov unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr); 4743705e11aSYang Hongyang if (!net_eq(net, &init_net)) 475e4a2d5c2SPavel Emelyanov kfree(table); 4768d8354d2SPavel Emelyanov } 4777d291ebbSPavel Emelyanov 4787d291ebbSPavel Emelyanov static struct ctl_table_header *ip6_ctl_header; 4797d291ebbSPavel Emelyanov 4807d291ebbSPavel Emelyanov static int ip6_frags_sysctl_register(void) 4817d291ebbSPavel Emelyanov { 48243444757SEric W. Biederman ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6", 4837d291ebbSPavel Emelyanov ip6_frags_ctl_table); 4847d291ebbSPavel Emelyanov return ip6_ctl_header == NULL ? -ENOMEM : 0; 4857d291ebbSPavel Emelyanov } 4867d291ebbSPavel Emelyanov 4877d291ebbSPavel Emelyanov static void ip6_frags_sysctl_unregister(void) 4887d291ebbSPavel Emelyanov { 4897d291ebbSPavel Emelyanov unregister_net_sysctl_table(ip6_ctl_header); 4907d291ebbSPavel Emelyanov } 4918d8354d2SPavel Emelyanov #else 492fc08c258SFabian Frederick static int ip6_frags_ns_sysctl_register(struct net *net) 4938d8354d2SPavel Emelyanov { 4948d8354d2SPavel Emelyanov return 0; 4958d8354d2SPavel Emelyanov } 496e4a2d5c2SPavel Emelyanov 497fc08c258SFabian Frederick static void ip6_frags_ns_sysctl_unregister(struct net *net) 498e4a2d5c2SPavel Emelyanov { 499e4a2d5c2SPavel Emelyanov } 5007d291ebbSPavel Emelyanov 501fc08c258SFabian Frederick static int ip6_frags_sysctl_register(void) 5027d291ebbSPavel Emelyanov { 5037d291ebbSPavel Emelyanov return 0; 5047d291ebbSPavel Emelyanov } 5057d291ebbSPavel Emelyanov 506fc08c258SFabian Frederick static void ip6_frags_sysctl_unregister(void) 5077d291ebbSPavel Emelyanov { 5087d291ebbSPavel Emelyanov } 5098d8354d2SPavel Emelyanov #endif 5108d8354d2SPavel Emelyanov 5112c8c1e72SAlexey Dobriyan static int __net_init ipv6_frags_init_net(struct net *net) 5128d8354d2SPavel Emelyanov { 513787bea77SEric Dumazet int res; 514787bea77SEric Dumazet 515803fdd99SEric Dumazet net->ipv6.fqdir.high_thresh = IPV6_FRAG_HIGH_THRESH; 516803fdd99SEric Dumazet net->ipv6.fqdir.low_thresh = IPV6_FRAG_LOW_THRESH; 517803fdd99SEric Dumazet net->ipv6.fqdir.timeout = IPV6_FRAG_TIMEOUT; 518803fdd99SEric Dumazet net->ipv6.fqdir.f = &ip6_frags; 5198d8354d2SPavel Emelyanov 520803fdd99SEric Dumazet res = inet_frags_init_net(&net->ipv6.fqdir); 521787bea77SEric Dumazet if (res < 0) 522787bea77SEric Dumazet return res; 5235a63643eSJesper Dangaard Brouer 524787bea77SEric Dumazet res = ip6_frags_ns_sysctl_register(net); 525787bea77SEric Dumazet if (res < 0) 526803fdd99SEric Dumazet fqdir_exit(&net->ipv6.fqdir); 527787bea77SEric Dumazet return res; 528e71e0349SDaniel Lezcano } 529e71e0349SDaniel Lezcano 5302c8c1e72SAlexey Dobriyan static void __net_exit ipv6_frags_exit_net(struct net *net) 53181566e83SPavel Emelyanov { 5320a64b4b8SPavel Emelyanov ip6_frags_ns_sysctl_unregister(net); 533803fdd99SEric Dumazet fqdir_exit(&net->ipv6.fqdir); 53481566e83SPavel Emelyanov } 53581566e83SPavel Emelyanov 53681566e83SPavel Emelyanov static struct pernet_operations ip6_frags_ops = { 53781566e83SPavel Emelyanov .init = ipv6_frags_init_net, 53881566e83SPavel Emelyanov .exit = ipv6_frags_exit_net, 53981566e83SPavel Emelyanov }; 54081566e83SPavel Emelyanov 54170b095c8SFlorian Westphal static const struct rhashtable_params ip6_rhash_params = { 542648700f7SEric Dumazet .head_offset = offsetof(struct inet_frag_queue, node), 54370b095c8SFlorian Westphal .hashfn = ip6frag_key_hashfn, 54470b095c8SFlorian Westphal .obj_hashfn = ip6frag_obj_hashfn, 54570b095c8SFlorian Westphal .obj_cmpfn = ip6frag_obj_cmpfn, 546648700f7SEric Dumazet .automatic_shrinking = true, 547648700f7SEric Dumazet }; 548648700f7SEric Dumazet 549853cbbaaSDaniel Lezcano int __init ipv6_frag_init(void) 5501da177e4SLinus Torvalds { 551853cbbaaSDaniel Lezcano int ret; 5521da177e4SLinus Torvalds 55370b095c8SFlorian Westphal ip6_frags.constructor = ip6frag_init; 554c9547709SPavel Emelyanov ip6_frags.destructor = NULL; 5551e4b8287SPavel Emelyanov ip6_frags.qsize = sizeof(struct frag_queue); 556e521db9dSPavel Emelyanov ip6_frags.frag_expire = ip6_frag_expire; 557d4ad4d22SNikolay Aleksandrov ip6_frags.frags_cache_name = ip6_frag_cache_name; 558648700f7SEric Dumazet ip6_frags.rhash_params = ip6_rhash_params; 559d4ad4d22SNikolay Aleksandrov ret = inet_frags_init(&ip6_frags); 560d4ad4d22SNikolay Aleksandrov if (ret) 5615b975babSEric Dumazet goto out; 5625b975babSEric Dumazet 5635b975babSEric Dumazet ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT); 5645b975babSEric Dumazet if (ret) 5655b975babSEric Dumazet goto err_protocol; 5665b975babSEric Dumazet 5675b975babSEric Dumazet ret = ip6_frags_sysctl_register(); 5685b975babSEric Dumazet if (ret) 5695b975babSEric Dumazet goto err_sysctl; 5705b975babSEric Dumazet 5715b975babSEric Dumazet ret = register_pernet_subsys(&ip6_frags_ops); 5725b975babSEric Dumazet if (ret) 573d4ad4d22SNikolay Aleksandrov goto err_pernet; 5745b975babSEric Dumazet 575853cbbaaSDaniel Lezcano out: 576853cbbaaSDaniel Lezcano return ret; 5770002c630SPavel Emelyanov 5780002c630SPavel Emelyanov err_pernet: 5797d291ebbSPavel Emelyanov ip6_frags_sysctl_unregister(); 5807d291ebbSPavel Emelyanov err_sysctl: 5810002c630SPavel Emelyanov inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT); 5825b975babSEric Dumazet err_protocol: 5835b975babSEric Dumazet inet_frags_fini(&ip6_frags); 5840002c630SPavel Emelyanov goto out; 585853cbbaaSDaniel Lezcano } 586853cbbaaSDaniel Lezcano 587853cbbaaSDaniel Lezcano void ipv6_frag_exit(void) 588853cbbaaSDaniel Lezcano { 589853cbbaaSDaniel Lezcano inet_frags_fini(&ip6_frags); 5907d291ebbSPavel Emelyanov ip6_frags_sysctl_unregister(); 59181566e83SPavel Emelyanov unregister_pernet_subsys(&ip6_frags_ops); 592853cbbaaSDaniel Lezcano inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT); 5931da177e4SLinus Torvalds } 594