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 72f61944efSHerbert Xu static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev, 73f61944efSHerbert Xu 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); 82b836c99fSAmerigo Wang net = container_of(fq->q.net, struct net, ipv6.frags); 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 103648700f7SEric Dumazet q = inet_frag_find(&net->ipv6.frags, &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 { 1141da177e4SLinus Torvalds struct sk_buff *prev, *next; 115f61944efSHerbert Xu struct net_device *dev; 116dbd1759eSWillem de Bruijn int offset, end, fragsize; 117adf30907SEric Dumazet struct net *net = dev_net(skb_dst(skb)->dev); 118eec2e618SHannes Frederic Sowa u8 ecn; 1191da177e4SLinus Torvalds 12006aa8b8aSNikolay Aleksandrov if (fq->q.flags & INET_FRAG_COMPLETE) 1211da177e4SLinus Torvalds goto err; 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds offset = ntohs(fhdr->frag_off) & ~0x7; 1240660e03fSArnaldo Carvalho de Melo end = offset + (ntohs(ipv6_hdr(skb)->payload_len) - 1250660e03fSArnaldo Carvalho de Melo ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1))); 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds if ((unsigned int)end > IPV6_MAXPLEN) { 128415787d7SEric Dumazet *prob_offset = (u8 *)&fhdr->frag_off - skb_network_header(skb); 129f61944efSHerbert Xu return -1; 1301da177e4SLinus Torvalds } 1311da177e4SLinus Torvalds 132eec2e618SHannes Frederic Sowa ecn = ip6_frag_ecn(ipv6_hdr(skb)); 133eec2e618SHannes Frederic Sowa 134d56f90a7SArnaldo Carvalho de Melo if (skb->ip_summed == CHECKSUM_COMPLETE) { 135d56f90a7SArnaldo Carvalho de Melo const unsigned char *nh = skb_network_header(skb); 1361da177e4SLinus Torvalds skb->csum = csum_sub(skb->csum, 137d56f90a7SArnaldo Carvalho de Melo csum_partial(nh, (u8 *)(fhdr + 1) - nh, 138d56f90a7SArnaldo Carvalho de Melo 0)); 139d56f90a7SArnaldo Carvalho de Melo } 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds /* Is this the final fragment? */ 1421da177e4SLinus Torvalds if (!(fhdr->frag_off & htons(IP6_MF))) { 1431da177e4SLinus Torvalds /* If we already have some bits beyond end 1441da177e4SLinus Torvalds * or have different end, the segment is corrupted. 1451da177e4SLinus Torvalds */ 1465ab11c98SPavel Emelyanov if (end < fq->q.len || 14706aa8b8aSNikolay Aleksandrov ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len)) 1481da177e4SLinus Torvalds goto err; 14906aa8b8aSNikolay Aleksandrov fq->q.flags |= INET_FRAG_LAST_IN; 1505ab11c98SPavel Emelyanov fq->q.len = end; 1511da177e4SLinus Torvalds } else { 1521da177e4SLinus Torvalds /* Check if the fragment is rounded to 8 bytes. 1531da177e4SLinus Torvalds * Required by the RFC. 1541da177e4SLinus Torvalds */ 1551da177e4SLinus Torvalds if (end & 0x7) { 1561da177e4SLinus Torvalds /* RFC2460 says always send parameter problem in 1571da177e4SLinus Torvalds * this case. -DaveM 1581da177e4SLinus Torvalds */ 159415787d7SEric Dumazet *prob_offset = offsetof(struct ipv6hdr, payload_len); 160f61944efSHerbert Xu return -1; 1611da177e4SLinus Torvalds } 1625ab11c98SPavel Emelyanov if (end > fq->q.len) { 1631da177e4SLinus Torvalds /* Some bits beyond end -> corruption. */ 16406aa8b8aSNikolay Aleksandrov if (fq->q.flags & INET_FRAG_LAST_IN) 1651da177e4SLinus Torvalds goto err; 1665ab11c98SPavel Emelyanov fq->q.len = end; 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds if (end == offset) 1711da177e4SLinus Torvalds goto err; 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds /* Point into the IP datagram 'data' part. */ 1741da177e4SLinus Torvalds if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) 1751da177e4SLinus Torvalds goto err; 17642ca89c1SStephen Hemminger 17742ca89c1SStephen Hemminger if (pskb_trim_rcsum(skb, end - offset)) 1781da177e4SLinus Torvalds goto err; 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds /* Find out which fragments are in front and at the back of us 1811da177e4SLinus Torvalds * in the chain of fragments so far. We must know where to put 1821da177e4SLinus Torvalds * this fragment, right? 1831da177e4SLinus Torvalds */ 184d6bebca9SChangli Gao prev = fq->q.fragments_tail; 185219badfaSEric Dumazet if (!prev || prev->ip_defrag_offset < offset) { 186d6bebca9SChangli Gao next = NULL; 187d6bebca9SChangli Gao goto found; 188d6bebca9SChangli Gao } 1891da177e4SLinus Torvalds prev = NULL; 1905ab11c98SPavel Emelyanov for (next = fq->q.fragments; next != NULL; next = next->next) { 191219badfaSEric Dumazet if (next->ip_defrag_offset >= offset) 1921da177e4SLinus Torvalds break; /* bingo! */ 1931da177e4SLinus Torvalds prev = next; 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds 196d6bebca9SChangli Gao found: 1975de658f8SEric Dumazet /* RFC5722, Section 4, amended by Errata ID : 3089 19870789d70SNicolas Dichtel * When reassembling an IPv6 datagram, if 19970789d70SNicolas Dichtel * one or more its constituent fragments is determined to be an 20070789d70SNicolas Dichtel * overlapping fragment, the entire datagram (and any constituent 2015de658f8SEric Dumazet * fragments) MUST be silently discarded. 2021da177e4SLinus Torvalds */ 2031da177e4SLinus Torvalds 20470789d70SNicolas Dichtel /* Check for overlap with preceding fragment. */ 20570789d70SNicolas Dichtel if (prev && 206219badfaSEric Dumazet (prev->ip_defrag_offset + prev->len) > offset) 20770789d70SNicolas Dichtel goto discard_fq; 2081da177e4SLinus Torvalds 20970789d70SNicolas Dichtel /* Look for overlap with succeeding segment. */ 210219badfaSEric Dumazet if (next && next->ip_defrag_offset < end) 21170789d70SNicolas Dichtel goto discard_fq; 2121da177e4SLinus Torvalds 213219badfaSEric Dumazet /* Note : skb->ip_defrag_offset and skb->dev share the same location */ 214219badfaSEric Dumazet dev = skb->dev; 215219badfaSEric Dumazet if (dev) 216219badfaSEric Dumazet fq->iif = dev->ifindex; 217219badfaSEric Dumazet /* Makes sure compiler wont do silly aliasing games */ 218219badfaSEric Dumazet barrier(); 219219badfaSEric Dumazet skb->ip_defrag_offset = offset; 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds /* Insert this fragment in the chain of fragments. */ 2221da177e4SLinus Torvalds skb->next = next; 223d6bebca9SChangli Gao if (!next) 224d6bebca9SChangli Gao fq->q.fragments_tail = skb; 2251da177e4SLinus Torvalds if (prev) 2261da177e4SLinus Torvalds prev->next = skb; 2271da177e4SLinus Torvalds else 2285ab11c98SPavel Emelyanov fq->q.fragments = skb; 2291da177e4SLinus Torvalds 2305ab11c98SPavel Emelyanov fq->q.stamp = skb->tstamp; 2315ab11c98SPavel Emelyanov fq->q.meat += skb->len; 232eec2e618SHannes Frederic Sowa fq->ecn |= ecn; 2330e60d245SFlorian Westphal add_frag_mem_limit(fq->q.net, skb->truesize); 2341da177e4SLinus Torvalds 235dbd1759eSWillem de Bruijn fragsize = -skb_network_offset(skb) + skb->len; 236dbd1759eSWillem de Bruijn if (fragsize > fq->q.max_size) 237dbd1759eSWillem de Bruijn fq->q.max_size = fragsize; 238dbd1759eSWillem de Bruijn 2391da177e4SLinus Torvalds /* The first fragment. 2401da177e4SLinus Torvalds * nhoffset is obtained from the first fragment, of course. 2411da177e4SLinus Torvalds */ 2421da177e4SLinus Torvalds if (offset == 0) { 2431da177e4SLinus Torvalds fq->nhoffset = nhoff; 24406aa8b8aSNikolay Aleksandrov fq->q.flags |= INET_FRAG_FIRST_IN; 2451da177e4SLinus Torvalds } 246f61944efSHerbert Xu 24706aa8b8aSNikolay Aleksandrov if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) && 24897599dc7SEric Dumazet fq->q.meat == fq->q.len) { 24997599dc7SEric Dumazet int res; 25097599dc7SEric Dumazet unsigned long orefdst = skb->_skb_refdst; 251f61944efSHerbert Xu 25297599dc7SEric Dumazet skb->_skb_refdst = 0UL; 25397599dc7SEric Dumazet res = ip6_frag_reasm(fq, prev, dev); 25497599dc7SEric Dumazet skb->_skb_refdst = orefdst; 25597599dc7SEric Dumazet return res; 25697599dc7SEric Dumazet } 25797599dc7SEric Dumazet 25897599dc7SEric Dumazet skb_dst_drop(skb); 259f61944efSHerbert Xu return -1; 2601da177e4SLinus Torvalds 26170789d70SNicolas Dichtel discard_fq: 262093ba729SEric Dumazet inet_frag_kill(&fq->q); 2631da177e4SLinus Torvalds err: 2641d015503SEric Dumazet __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), 2653bd653c8SDenis V. Lunev IPSTATS_MIB_REASMFAILS); 2661da177e4SLinus Torvalds kfree_skb(skb); 267f61944efSHerbert Xu return -1; 2681da177e4SLinus Torvalds } 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds /* 2711da177e4SLinus Torvalds * Check if this packet is complete. 2721da177e4SLinus Torvalds * Returns NULL on failure by any reason, and pointer 2731da177e4SLinus Torvalds * to current nexthdr field in reassembled frame. 2741da177e4SLinus Torvalds * 2751da177e4SLinus Torvalds * It is called with locked fq, and caller must check that 2761da177e4SLinus Torvalds * queue is eligible for reassembly i.e. it is not COMPLETE, 2771da177e4SLinus Torvalds * the last and the first frames arrived and all the bits are here. 2781da177e4SLinus Torvalds */ 279f61944efSHerbert Xu static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev, 2801da177e4SLinus Torvalds struct net_device *dev) 2811da177e4SLinus Torvalds { 2822bad35b7SJorge Boncompte [DTI2] struct net *net = container_of(fq->q.net, struct net, ipv6.frags); 2835ab11c98SPavel Emelyanov struct sk_buff *fp, *head = fq->q.fragments; 2841da177e4SLinus Torvalds int payload_len; 2851da177e4SLinus Torvalds unsigned int nhoff; 286ec16439eSEric Dumazet int sum_truesize; 287eec2e618SHannes Frederic Sowa u8 ecn; 2881da177e4SLinus Torvalds 289093ba729SEric Dumazet inet_frag_kill(&fq->q); 2901da177e4SLinus Torvalds 291eec2e618SHannes Frederic Sowa ecn = ip_frag_ecn_table[fq->ecn]; 292eec2e618SHannes Frederic Sowa if (unlikely(ecn == 0xff)) 293eec2e618SHannes Frederic Sowa goto out_fail; 294eec2e618SHannes Frederic Sowa 295f61944efSHerbert Xu /* Make the one we just received the head. */ 296f61944efSHerbert Xu if (prev) { 297f61944efSHerbert Xu head = prev->next; 298f61944efSHerbert Xu fp = skb_clone(head, GFP_ATOMIC); 299f61944efSHerbert Xu 300f61944efSHerbert Xu if (!fp) 301f61944efSHerbert Xu goto out_oom; 302f61944efSHerbert Xu 303f61944efSHerbert Xu fp->next = head->next; 304d6bebca9SChangli Gao if (!fp->next) 305d6bebca9SChangli Gao fq->q.fragments_tail = fp; 306f61944efSHerbert Xu prev->next = fp; 307f61944efSHerbert Xu 3085ab11c98SPavel Emelyanov skb_morph(head, fq->q.fragments); 3095ab11c98SPavel Emelyanov head->next = fq->q.fragments->next; 310f61944efSHerbert Xu 311808db80aSEric Dumazet consume_skb(fq->q.fragments); 3125ab11c98SPavel Emelyanov fq->q.fragments = head; 313f61944efSHerbert Xu } 314f61944efSHerbert Xu 315547b792cSIlpo Järvinen WARN_ON(head == NULL); 316219badfaSEric Dumazet WARN_ON(head->ip_defrag_offset != 0); 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds /* Unfragmented part is taken from the first segment. */ 319d56f90a7SArnaldo Carvalho de Melo payload_len = ((head->data - skb_network_header(head)) - 3205ab11c98SPavel Emelyanov sizeof(struct ipv6hdr) + fq->q.len - 321d56f90a7SArnaldo Carvalho de Melo sizeof(struct frag_hdr)); 3221da177e4SLinus Torvalds if (payload_len > IPV6_MAXPLEN) 3231da177e4SLinus Torvalds goto out_oversize; 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds /* Head of list must not be cloned. */ 32614bbd6a5SPravin B Shelar if (skb_unclone(head, GFP_ATOMIC)) 3271da177e4SLinus Torvalds goto out_oom; 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds /* If the first fragment is fragmented itself, we split 3301da177e4SLinus Torvalds * it to two chunks: the first with data and paged part 3311da177e4SLinus Torvalds * and the second, holding only fragments. */ 33221dc3301SDavid S. Miller if (skb_has_frag_list(head)) { 3331da177e4SLinus Torvalds struct sk_buff *clone; 3341da177e4SLinus Torvalds int i, plen = 0; 3351da177e4SLinus Torvalds 336e5d08d71SIan Morris clone = alloc_skb(0, GFP_ATOMIC); 33763159f29SIan Morris if (!clone) 3381da177e4SLinus Torvalds goto out_oom; 3391da177e4SLinus Torvalds clone->next = head->next; 3401da177e4SLinus Torvalds head->next = clone; 3411da177e4SLinus Torvalds skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list; 3424d9092bbSDavid S. Miller skb_frag_list_init(head); 3431da177e4SLinus Torvalds for (i = 0; i < skb_shinfo(head)->nr_frags; i++) 3449e903e08SEric Dumazet plen += skb_frag_size(&skb_shinfo(head)->frags[i]); 3451da177e4SLinus Torvalds clone->len = clone->data_len = head->data_len - plen; 3461da177e4SLinus Torvalds head->data_len -= clone->len; 3471da177e4SLinus Torvalds head->len -= clone->len; 3481da177e4SLinus Torvalds clone->csum = 0; 3491da177e4SLinus Torvalds clone->ip_summed = head->ip_summed; 3500e60d245SFlorian Westphal add_frag_mem_limit(fq->q.net, clone->truesize); 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds /* We have to remove fragment header from datagram and to relocate 3541da177e4SLinus Torvalds * header in order to calculate ICV correctly. */ 3551da177e4SLinus Torvalds nhoff = fq->nhoffset; 356b0e380b1SArnaldo Carvalho de Melo skb_network_header(head)[nhoff] = skb_transport_header(head)[0]; 3571da177e4SLinus Torvalds memmove(head->head + sizeof(struct frag_hdr), head->head, 3581da177e4SLinus Torvalds (head->data - head->head) - sizeof(struct frag_hdr)); 359b678aa57SJason A. Donenfeld if (skb_mac_header_was_set(head)) 360b0e380b1SArnaldo Carvalho de Melo head->mac_header += sizeof(struct frag_hdr); 361b0e380b1SArnaldo Carvalho de Melo head->network_header += sizeof(struct frag_hdr); 3621da177e4SLinus Torvalds 363badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(head); 364d56f90a7SArnaldo Carvalho de Melo skb_push(head, head->data - skb_network_header(head)); 3651da177e4SLinus Torvalds 366ec16439eSEric Dumazet sum_truesize = head->truesize; 367ec16439eSEric Dumazet for (fp = head->next; fp;) { 368ec16439eSEric Dumazet bool headstolen; 369ec16439eSEric Dumazet int delta; 370ec16439eSEric Dumazet struct sk_buff *next = fp->next; 371ec16439eSEric Dumazet 372ec16439eSEric Dumazet sum_truesize += fp->truesize; 3731da177e4SLinus Torvalds if (head->ip_summed != fp->ip_summed) 3741da177e4SLinus Torvalds head->ip_summed = CHECKSUM_NONE; 37584fa7933SPatrick McHardy else if (head->ip_summed == CHECKSUM_COMPLETE) 3761da177e4SLinus Torvalds head->csum = csum_add(head->csum, fp->csum); 377ec16439eSEric Dumazet 378ec16439eSEric Dumazet if (skb_try_coalesce(head, fp, &headstolen, &delta)) { 379ec16439eSEric Dumazet kfree_skb_partial(fp, headstolen); 380ec16439eSEric Dumazet } else { 381ec16439eSEric Dumazet if (!skb_shinfo(head)->frag_list) 382ec16439eSEric Dumazet skb_shinfo(head)->frag_list = fp; 383ec16439eSEric Dumazet head->data_len += fp->len; 384ec16439eSEric Dumazet head->len += fp->len; 3851da177e4SLinus Torvalds head->truesize += fp->truesize; 3861da177e4SLinus Torvalds } 387ec16439eSEric Dumazet fp = next; 388ec16439eSEric Dumazet } 3890e60d245SFlorian Westphal sub_frag_mem_limit(fq->q.net, sum_truesize); 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds head->next = NULL; 3921da177e4SLinus Torvalds head->dev = dev; 3935ab11c98SPavel Emelyanov head->tstamp = fq->q.stamp; 3940660e03fSArnaldo Carvalho de Melo ipv6_hdr(head)->payload_len = htons(payload_len); 395eec2e618SHannes Frederic Sowa ipv6_change_dsfield(ipv6_hdr(head), 0xff, ecn); 396951dbc8aSPatrick McHardy IP6CB(head)->nhoff = nhoff; 397f46078cfSHannes Frederic Sowa IP6CB(head)->flags |= IP6SKB_FRAGMENTED; 398dbd1759eSWillem de Bruijn IP6CB(head)->frag_max_size = fq->q.max_size; 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds /* Yes, and fold redundant checksum back. 8) */ 4016b83d28aSDaniel Borkmann skb_postpush_rcsum(head, skb_network_header(head), 4026b83d28aSDaniel Borkmann skb_network_header_len(head)); 4031da177e4SLinus Torvalds 404a11d206dSYOSHIFUJI Hideaki rcu_read_lock(); 4051d015503SEric Dumazet __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS); 406a11d206dSYOSHIFUJI Hideaki rcu_read_unlock(); 4075ab11c98SPavel Emelyanov fq->q.fragments = NULL; 408d6bebca9SChangli Gao fq->q.fragments_tail = NULL; 4091da177e4SLinus Torvalds return 1; 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds out_oversize: 412e87cc472SJoe Perches net_dbg_ratelimited("ip6_frag_reasm: payload len = %d\n", payload_len); 4131da177e4SLinus Torvalds goto out_fail; 4141da177e4SLinus Torvalds out_oom: 415e87cc472SJoe Perches net_dbg_ratelimited("ip6_frag_reasm: no memory for reassembly\n"); 4161da177e4SLinus Torvalds out_fail: 417a11d206dSYOSHIFUJI Hideaki rcu_read_lock(); 4181d015503SEric Dumazet __IP6_INC_STATS(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS); 419a11d206dSYOSHIFUJI Hideaki rcu_read_unlock(); 4201da177e4SLinus Torvalds return -1; 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds 423e5bbef20SHerbert Xu static int ipv6_frag_rcv(struct sk_buff *skb) 4241da177e4SLinus Torvalds { 4251da177e4SLinus Torvalds struct frag_hdr *fhdr; 4261da177e4SLinus Torvalds struct frag_queue *fq; 427b71d1d42SEric Dumazet const struct ipv6hdr *hdr = ipv6_hdr(skb); 428adf30907SEric Dumazet struct net *net = dev_net(skb_dst(skb)->dev); 429648700f7SEric Dumazet int iif; 4301da177e4SLinus Torvalds 431f46078cfSHannes Frederic Sowa if (IP6CB(skb)->flags & IP6SKB_FRAGMENTED) 432f46078cfSHannes Frederic Sowa goto fail_hdr; 433f46078cfSHannes Frederic Sowa 4341d015503SEric Dumazet __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS); 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds /* Jumbo payload inhibits frag. header */ 43798b3377cSDenis V. Lunev if (hdr->payload_len == 0) 43898b3377cSDenis V. Lunev goto fail_hdr; 43998b3377cSDenis V. Lunev 440ea2ae17dSArnaldo Carvalho de Melo if (!pskb_may_pull(skb, (skb_transport_offset(skb) + 44198b3377cSDenis V. Lunev sizeof(struct frag_hdr)))) 44298b3377cSDenis V. Lunev goto fail_hdr; 4431da177e4SLinus Torvalds 4440660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 4459c70220bSArnaldo Carvalho de Melo fhdr = (struct frag_hdr *)skb_transport_header(skb); 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds if (!(fhdr->frag_off & htons(0xFFF9))) { 4481da177e4SLinus Torvalds /* It is not a fragmented frame */ 449b0e380b1SArnaldo Carvalho de Melo skb->transport_header += sizeof(struct frag_hdr); 4501d015503SEric Dumazet __IP6_INC_STATS(net, 451adf30907SEric Dumazet ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS); 4521da177e4SLinus Torvalds 453d56f90a7SArnaldo Carvalho de Melo IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb); 454f46078cfSHannes Frederic Sowa IP6CB(skb)->flags |= IP6SKB_FRAGMENTED; 4551da177e4SLinus Torvalds return 1; 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds 458648700f7SEric Dumazet iif = skb->dev ? skb->dev->ifindex : 0; 459648700f7SEric Dumazet fq = fq_find(net, fhdr->identification, hdr, iif); 46053b24b8fSIan Morris if (fq) { 461415787d7SEric Dumazet u32 prob_offset = 0; 462f61944efSHerbert Xu int ret; 4631da177e4SLinus Torvalds 4645ab11c98SPavel Emelyanov spin_lock(&fq->q.lock); 4651da177e4SLinus Torvalds 466648700f7SEric Dumazet fq->iif = iif; 467415787d7SEric Dumazet ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff, 468415787d7SEric Dumazet &prob_offset); 4691da177e4SLinus Torvalds 4705ab11c98SPavel Emelyanov spin_unlock(&fq->q.lock); 471093ba729SEric Dumazet inet_frag_put(&fq->q); 472415787d7SEric Dumazet if (prob_offset) { 473415787d7SEric Dumazet __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev), 474415787d7SEric Dumazet IPSTATS_MIB_INHDRERRORS); 475415787d7SEric Dumazet icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, prob_offset); 476415787d7SEric Dumazet } 4771da177e4SLinus Torvalds return ret; 4781da177e4SLinus Torvalds } 4791da177e4SLinus Torvalds 4801d015503SEric Dumazet __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS); 4811da177e4SLinus Torvalds kfree_skb(skb); 4821da177e4SLinus Torvalds return -1; 48398b3377cSDenis V. Lunev 48498b3377cSDenis V. Lunev fail_hdr: 485bdb7cc64SStephen Suryaputra __IP6_INC_STATS(net, __in6_dev_get_safely(skb->dev), 486d2373862SNikolay Aleksandrov IPSTATS_MIB_INHDRERRORS); 48798b3377cSDenis V. Lunev icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb)); 48898b3377cSDenis V. Lunev return -1; 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds 491cc24becaSIan Morris static const struct inet6_protocol frag_protocol = { 4921da177e4SLinus Torvalds .handler = ipv6_frag_rcv, 4931da177e4SLinus Torvalds .flags = INET6_PROTO_NOPOLICY, 4941da177e4SLinus Torvalds }; 4951da177e4SLinus Torvalds 4968d8354d2SPavel Emelyanov #ifdef CONFIG_SYSCTL 4971bab4c75SNikolay Aleksandrov 4980a64b4b8SPavel Emelyanov static struct ctl_table ip6_frags_ns_ctl_table[] = { 499e71e0349SDaniel Lezcano { 5008d8354d2SPavel Emelyanov .procname = "ip6frag_high_thresh", 501e31e0bdcSPavel Emelyanov .data = &init_net.ipv6.frags.high_thresh, 5023e67f106SEric Dumazet .maxlen = sizeof(unsigned long), 5038d8354d2SPavel Emelyanov .mode = 0644, 5043e67f106SEric Dumazet .proc_handler = proc_doulongvec_minmax, 5051bab4c75SNikolay Aleksandrov .extra1 = &init_net.ipv6.frags.low_thresh 5068d8354d2SPavel Emelyanov }, 5078d8354d2SPavel Emelyanov { 5088d8354d2SPavel Emelyanov .procname = "ip6frag_low_thresh", 509e31e0bdcSPavel Emelyanov .data = &init_net.ipv6.frags.low_thresh, 5103e67f106SEric Dumazet .maxlen = sizeof(unsigned long), 5118d8354d2SPavel Emelyanov .mode = 0644, 5126e00f7ddSEric Dumazet .proc_handler = proc_doulongvec_minmax, 5131bab4c75SNikolay Aleksandrov .extra2 = &init_net.ipv6.frags.high_thresh 5148d8354d2SPavel Emelyanov }, 5158d8354d2SPavel Emelyanov { 5168d8354d2SPavel Emelyanov .procname = "ip6frag_time", 517b2fd5321SPavel Emelyanov .data = &init_net.ipv6.frags.timeout, 5188d8354d2SPavel Emelyanov .maxlen = sizeof(int), 5198d8354d2SPavel Emelyanov .mode = 0644, 5206d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 5218d8354d2SPavel Emelyanov }, 5227d291ebbSPavel Emelyanov { } 5237d291ebbSPavel Emelyanov }; 5247d291ebbSPavel Emelyanov 525e3a57d18SFlorian Westphal /* secret interval has been deprecated */ 526e3a57d18SFlorian Westphal static int ip6_frags_secret_interval_unused; 5277d291ebbSPavel Emelyanov static struct ctl_table ip6_frags_ctl_table[] = { 5288d8354d2SPavel Emelyanov { 5298d8354d2SPavel Emelyanov .procname = "ip6frag_secret_interval", 530e3a57d18SFlorian Westphal .data = &ip6_frags_secret_interval_unused, 5318d8354d2SPavel Emelyanov .maxlen = sizeof(int), 5328d8354d2SPavel Emelyanov .mode = 0644, 5336d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 5348d8354d2SPavel Emelyanov }, 5358d8354d2SPavel Emelyanov { } 5368d8354d2SPavel Emelyanov }; 5377d460db9SDaniel Lezcano 5382c8c1e72SAlexey Dobriyan static int __net_init ip6_frags_ns_sysctl_register(struct net *net) 5398d8354d2SPavel Emelyanov { 540e4a2d5c2SPavel Emelyanov struct ctl_table *table; 5418d8354d2SPavel Emelyanov struct ctl_table_header *hdr; 5428d8354d2SPavel Emelyanov 5430a64b4b8SPavel Emelyanov table = ip6_frags_ns_ctl_table; 54409ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) { 5450a64b4b8SPavel Emelyanov table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL); 54663159f29SIan Morris if (!table) 547e4a2d5c2SPavel Emelyanov goto err_alloc; 548e4a2d5c2SPavel Emelyanov 549e31e0bdcSPavel Emelyanov table[0].data = &net->ipv6.frags.high_thresh; 5501bab4c75SNikolay Aleksandrov table[0].extra1 = &net->ipv6.frags.low_thresh; 5511bab4c75SNikolay Aleksandrov table[0].extra2 = &init_net.ipv6.frags.high_thresh; 552e31e0bdcSPavel Emelyanov table[1].data = &net->ipv6.frags.low_thresh; 5531bab4c75SNikolay Aleksandrov table[1].extra2 = &net->ipv6.frags.high_thresh; 554b2fd5321SPavel Emelyanov table[2].data = &net->ipv6.frags.timeout; 555e4a2d5c2SPavel Emelyanov } 556e4a2d5c2SPavel Emelyanov 557ec8f23ceSEric W. Biederman hdr = register_net_sysctl(net, "net/ipv6", table); 55863159f29SIan Morris if (!hdr) 559e4a2d5c2SPavel Emelyanov goto err_reg; 560e4a2d5c2SPavel Emelyanov 561e4a2d5c2SPavel Emelyanov net->ipv6.sysctl.frags_hdr = hdr; 562e4a2d5c2SPavel Emelyanov return 0; 563e4a2d5c2SPavel Emelyanov 564e4a2d5c2SPavel Emelyanov err_reg: 56509ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) 566e4a2d5c2SPavel Emelyanov kfree(table); 567e4a2d5c2SPavel Emelyanov err_alloc: 568e4a2d5c2SPavel Emelyanov return -ENOMEM; 569e4a2d5c2SPavel Emelyanov } 570e4a2d5c2SPavel Emelyanov 5712c8c1e72SAlexey Dobriyan static void __net_exit ip6_frags_ns_sysctl_unregister(struct net *net) 572e4a2d5c2SPavel Emelyanov { 573e4a2d5c2SPavel Emelyanov struct ctl_table *table; 574e4a2d5c2SPavel Emelyanov 575e4a2d5c2SPavel Emelyanov table = net->ipv6.sysctl.frags_hdr->ctl_table_arg; 576e4a2d5c2SPavel Emelyanov unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr); 5773705e11aSYang Hongyang if (!net_eq(net, &init_net)) 578e4a2d5c2SPavel Emelyanov kfree(table); 5798d8354d2SPavel Emelyanov } 5807d291ebbSPavel Emelyanov 5817d291ebbSPavel Emelyanov static struct ctl_table_header *ip6_ctl_header; 5827d291ebbSPavel Emelyanov 5837d291ebbSPavel Emelyanov static int ip6_frags_sysctl_register(void) 5847d291ebbSPavel Emelyanov { 58543444757SEric W. Biederman ip6_ctl_header = register_net_sysctl(&init_net, "net/ipv6", 5867d291ebbSPavel Emelyanov ip6_frags_ctl_table); 5877d291ebbSPavel Emelyanov return ip6_ctl_header == NULL ? -ENOMEM : 0; 5887d291ebbSPavel Emelyanov } 5897d291ebbSPavel Emelyanov 5907d291ebbSPavel Emelyanov static void ip6_frags_sysctl_unregister(void) 5917d291ebbSPavel Emelyanov { 5927d291ebbSPavel Emelyanov unregister_net_sysctl_table(ip6_ctl_header); 5937d291ebbSPavel Emelyanov } 5948d8354d2SPavel Emelyanov #else 595fc08c258SFabian Frederick static int ip6_frags_ns_sysctl_register(struct net *net) 5968d8354d2SPavel Emelyanov { 5978d8354d2SPavel Emelyanov return 0; 5988d8354d2SPavel Emelyanov } 599e4a2d5c2SPavel Emelyanov 600fc08c258SFabian Frederick static void ip6_frags_ns_sysctl_unregister(struct net *net) 601e4a2d5c2SPavel Emelyanov { 602e4a2d5c2SPavel Emelyanov } 6037d291ebbSPavel Emelyanov 604fc08c258SFabian Frederick static int ip6_frags_sysctl_register(void) 6057d291ebbSPavel Emelyanov { 6067d291ebbSPavel Emelyanov return 0; 6077d291ebbSPavel Emelyanov } 6087d291ebbSPavel Emelyanov 609fc08c258SFabian Frederick static void ip6_frags_sysctl_unregister(void) 6107d291ebbSPavel Emelyanov { 6117d291ebbSPavel Emelyanov } 6128d8354d2SPavel Emelyanov #endif 6138d8354d2SPavel Emelyanov 6142c8c1e72SAlexey Dobriyan static int __net_init ipv6_frags_init_net(struct net *net) 6158d8354d2SPavel Emelyanov { 616787bea77SEric Dumazet int res; 617787bea77SEric Dumazet 6187c070aa9SShan Wei net->ipv6.frags.high_thresh = IPV6_FRAG_HIGH_THRESH; 6197c070aa9SShan Wei net->ipv6.frags.low_thresh = IPV6_FRAG_LOW_THRESH; 620b2fd5321SPavel Emelyanov net->ipv6.frags.timeout = IPV6_FRAG_TIMEOUT; 621093ba729SEric Dumazet net->ipv6.frags.f = &ip6_frags; 6228d8354d2SPavel Emelyanov 623787bea77SEric Dumazet res = inet_frags_init_net(&net->ipv6.frags); 624787bea77SEric Dumazet if (res < 0) 625787bea77SEric Dumazet return res; 6265a63643eSJesper Dangaard Brouer 627787bea77SEric Dumazet res = ip6_frags_ns_sysctl_register(net); 628787bea77SEric Dumazet if (res < 0) 629093ba729SEric Dumazet inet_frags_exit_net(&net->ipv6.frags); 630787bea77SEric Dumazet return res; 631e71e0349SDaniel Lezcano } 632e71e0349SDaniel Lezcano 6332c8c1e72SAlexey Dobriyan static void __net_exit ipv6_frags_exit_net(struct net *net) 63481566e83SPavel Emelyanov { 6350a64b4b8SPavel Emelyanov ip6_frags_ns_sysctl_unregister(net); 636093ba729SEric Dumazet inet_frags_exit_net(&net->ipv6.frags); 63781566e83SPavel Emelyanov } 63881566e83SPavel Emelyanov 63981566e83SPavel Emelyanov static struct pernet_operations ip6_frags_ops = { 64081566e83SPavel Emelyanov .init = ipv6_frags_init_net, 64181566e83SPavel Emelyanov .exit = ipv6_frags_exit_net, 64281566e83SPavel Emelyanov }; 64381566e83SPavel Emelyanov 64470b095c8SFlorian Westphal static const struct rhashtable_params ip6_rhash_params = { 645648700f7SEric Dumazet .head_offset = offsetof(struct inet_frag_queue, node), 64670b095c8SFlorian Westphal .hashfn = ip6frag_key_hashfn, 64770b095c8SFlorian Westphal .obj_hashfn = ip6frag_obj_hashfn, 64870b095c8SFlorian Westphal .obj_cmpfn = ip6frag_obj_cmpfn, 649648700f7SEric Dumazet .automatic_shrinking = true, 650648700f7SEric Dumazet }; 651648700f7SEric Dumazet 652853cbbaaSDaniel Lezcano int __init ipv6_frag_init(void) 6531da177e4SLinus Torvalds { 654853cbbaaSDaniel Lezcano int ret; 6551da177e4SLinus Torvalds 65670b095c8SFlorian Westphal ip6_frags.constructor = ip6frag_init; 657c9547709SPavel Emelyanov ip6_frags.destructor = NULL; 6581e4b8287SPavel Emelyanov ip6_frags.qsize = sizeof(struct frag_queue); 659e521db9dSPavel Emelyanov ip6_frags.frag_expire = ip6_frag_expire; 660d4ad4d22SNikolay Aleksandrov ip6_frags.frags_cache_name = ip6_frag_cache_name; 661648700f7SEric Dumazet ip6_frags.rhash_params = ip6_rhash_params; 662d4ad4d22SNikolay Aleksandrov ret = inet_frags_init(&ip6_frags); 663d4ad4d22SNikolay Aleksandrov if (ret) 6645b975babSEric Dumazet goto out; 6655b975babSEric Dumazet 6665b975babSEric Dumazet ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT); 6675b975babSEric Dumazet if (ret) 6685b975babSEric Dumazet goto err_protocol; 6695b975babSEric Dumazet 6705b975babSEric Dumazet ret = ip6_frags_sysctl_register(); 6715b975babSEric Dumazet if (ret) 6725b975babSEric Dumazet goto err_sysctl; 6735b975babSEric Dumazet 6745b975babSEric Dumazet ret = register_pernet_subsys(&ip6_frags_ops); 6755b975babSEric Dumazet if (ret) 676d4ad4d22SNikolay Aleksandrov goto err_pernet; 6775b975babSEric Dumazet 678853cbbaaSDaniel Lezcano out: 679853cbbaaSDaniel Lezcano return ret; 6800002c630SPavel Emelyanov 6810002c630SPavel Emelyanov err_pernet: 6827d291ebbSPavel Emelyanov ip6_frags_sysctl_unregister(); 6837d291ebbSPavel Emelyanov err_sysctl: 6840002c630SPavel Emelyanov inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT); 6855b975babSEric Dumazet err_protocol: 6865b975babSEric Dumazet inet_frags_fini(&ip6_frags); 6870002c630SPavel Emelyanov goto out; 688853cbbaaSDaniel Lezcano } 689853cbbaaSDaniel Lezcano 690853cbbaaSDaniel Lezcano void ipv6_frag_exit(void) 691853cbbaaSDaniel Lezcano { 692853cbbaaSDaniel Lezcano inet_frags_fini(&ip6_frags); 6937d291ebbSPavel Emelyanov ip6_frags_sysctl_unregister(); 69481566e83SPavel Emelyanov unregister_pernet_subsys(&ip6_frags_ops); 695853cbbaaSDaniel Lezcano inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT); 6961da177e4SLinus Torvalds } 697