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 */ 291da177e4SLinus Torvalds #include <linux/errno.h> 301da177e4SLinus Torvalds #include <linux/types.h> 311da177e4SLinus Torvalds #include <linux/string.h> 321da177e4SLinus Torvalds #include <linux/socket.h> 331da177e4SLinus Torvalds #include <linux/sockios.h> 341da177e4SLinus Torvalds #include <linux/jiffies.h> 351da177e4SLinus Torvalds #include <linux/net.h> 361da177e4SLinus Torvalds #include <linux/list.h> 371da177e4SLinus Torvalds #include <linux/netdevice.h> 381da177e4SLinus Torvalds #include <linux/in6.h> 391da177e4SLinus Torvalds #include <linux/ipv6.h> 401da177e4SLinus Torvalds #include <linux/icmpv6.h> 411da177e4SLinus Torvalds #include <linux/random.h> 421da177e4SLinus Torvalds #include <linux/jhash.h> 43f61944efSHerbert Xu #include <linux/skbuff.h> 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds #include <net/sock.h> 461da177e4SLinus Torvalds #include <net/snmp.h> 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds #include <net/ipv6.h> 49a11d206dSYOSHIFUJI Hideaki #include <net/ip6_route.h> 501da177e4SLinus Torvalds #include <net/protocol.h> 511da177e4SLinus Torvalds #include <net/transp_v6.h> 521da177e4SLinus Torvalds #include <net/rawv6.h> 531da177e4SLinus Torvalds #include <net/ndisc.h> 541da177e4SLinus Torvalds #include <net/addrconf.h> 555ab11c98SPavel Emelyanov #include <net/inet_frag.h> 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds struct ip6frag_skb_cb 581da177e4SLinus Torvalds { 591da177e4SLinus Torvalds struct inet6_skb_parm h; 601da177e4SLinus Torvalds int offset; 611da177e4SLinus Torvalds }; 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds #define FRAG6_CB(skb) ((struct ip6frag_skb_cb*)((skb)->cb)) 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds /* 671da177e4SLinus Torvalds * Equivalent of ipv4 struct ipq 681da177e4SLinus Torvalds */ 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds struct frag_queue 711da177e4SLinus Torvalds { 725ab11c98SPavel Emelyanov struct inet_frag_queue q; 731da177e4SLinus Torvalds 74e69a4adcSAl Viro __be32 id; /* fragment id */ 751da177e4SLinus Torvalds struct in6_addr saddr; 761da177e4SLinus Torvalds struct in6_addr daddr; 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds int iif; 791da177e4SLinus Torvalds unsigned int csum; 801da177e4SLinus Torvalds __u16 nhoffset; 811da177e4SLinus Torvalds }; 821da177e4SLinus Torvalds 837eb95156SPavel Emelyanov static struct inet_frags ip6_frags; 841da177e4SLinus Torvalds 85e5a2bb84SPavel Emelyanov int ip6_frag_nqueues(struct net *net) 867eb95156SPavel Emelyanov { 87e5a2bb84SPavel Emelyanov return net->ipv6.frags.nqueues; 887eb95156SPavel Emelyanov } 891da177e4SLinus Torvalds 906ddc0822SPavel Emelyanov int ip6_frag_mem(struct net *net) 917eb95156SPavel Emelyanov { 926ddc0822SPavel Emelyanov return atomic_read(&net->ipv6.frags.mem); 937eb95156SPavel Emelyanov } 941da177e4SLinus Torvalds 95f61944efSHerbert Xu static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev, 96f61944efSHerbert Xu struct net_device *dev); 97f61944efSHerbert Xu 98f6596f9dSZach Brown /* 99f6596f9dSZach Brown * callers should be careful not to use the hash value outside the ipfrag_lock 100f6596f9dSZach Brown * as doing so could race with ipfrag_hash_rnd being recalculated. 101f6596f9dSZach Brown */ 10293c8b90fSIlpo Järvinen unsigned int inet6_hash_frag(__be32 id, const struct in6_addr *saddr, 10393c8b90fSIlpo Järvinen const struct in6_addr *daddr, u32 rnd) 1041da177e4SLinus Torvalds { 1051da177e4SLinus Torvalds u32 a, b, c; 1061da177e4SLinus Torvalds 107e69a4adcSAl Viro a = (__force u32)saddr->s6_addr32[0]; 108e69a4adcSAl Viro b = (__force u32)saddr->s6_addr32[1]; 109e69a4adcSAl Viro c = (__force u32)saddr->s6_addr32[2]; 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds a += JHASH_GOLDEN_RATIO; 1121da177e4SLinus Torvalds b += JHASH_GOLDEN_RATIO; 11393c8b90fSIlpo Järvinen c += rnd; 1141da177e4SLinus Torvalds __jhash_mix(a, b, c); 1151da177e4SLinus Torvalds 116e69a4adcSAl Viro a += (__force u32)saddr->s6_addr32[3]; 117e69a4adcSAl Viro b += (__force u32)daddr->s6_addr32[0]; 118e69a4adcSAl Viro c += (__force u32)daddr->s6_addr32[1]; 1191da177e4SLinus Torvalds __jhash_mix(a, b, c); 1201da177e4SLinus Torvalds 121e69a4adcSAl Viro a += (__force u32)daddr->s6_addr32[2]; 122e69a4adcSAl Viro b += (__force u32)daddr->s6_addr32[3]; 123e69a4adcSAl Viro c += (__force u32)id; 1241da177e4SLinus Torvalds __jhash_mix(a, b, c); 1251da177e4SLinus Torvalds 1267eb95156SPavel Emelyanov return c & (INETFRAGS_HASHSZ - 1); 1271da177e4SLinus Torvalds } 12893c8b90fSIlpo Järvinen EXPORT_SYMBOL_GPL(inet6_hash_frag); 1291da177e4SLinus Torvalds 130321a3a99SPavel Emelyanov static unsigned int ip6_hashfn(struct inet_frag_queue *q) 1311da177e4SLinus Torvalds { 132321a3a99SPavel Emelyanov struct frag_queue *fq; 1331da177e4SLinus Torvalds 134321a3a99SPavel Emelyanov fq = container_of(q, struct frag_queue, q); 13593c8b90fSIlpo Järvinen return inet6_hash_frag(fq->id, &fq->saddr, &fq->daddr, ip6_frags.rnd); 1361da177e4SLinus Torvalds } 1371da177e4SLinus Torvalds 138abd6523dSPavel Emelyanov int ip6_frag_match(struct inet_frag_queue *q, void *a) 139abd6523dSPavel Emelyanov { 140abd6523dSPavel Emelyanov struct frag_queue *fq; 141abd6523dSPavel Emelyanov struct ip6_create_arg *arg = a; 142abd6523dSPavel Emelyanov 143abd6523dSPavel Emelyanov fq = container_of(q, struct frag_queue, q); 144abd6523dSPavel Emelyanov return (fq->id == arg->id && 145abd6523dSPavel Emelyanov ipv6_addr_equal(&fq->saddr, arg->src) && 146abd6523dSPavel Emelyanov ipv6_addr_equal(&fq->daddr, arg->dst)); 147abd6523dSPavel Emelyanov } 148abd6523dSPavel Emelyanov EXPORT_SYMBOL(ip6_frag_match); 149abd6523dSPavel Emelyanov 1501da177e4SLinus Torvalds /* Memory Tracking Functions. */ 1516ddc0822SPavel Emelyanov static inline void frag_kfree_skb(struct netns_frags *nf, 1526ddc0822SPavel Emelyanov struct sk_buff *skb, int *work) 1531da177e4SLinus Torvalds { 1541da177e4SLinus Torvalds if (work) 1551da177e4SLinus Torvalds *work -= skb->truesize; 1566ddc0822SPavel Emelyanov atomic_sub(skb->truesize, &nf->mem); 1571da177e4SLinus Torvalds kfree_skb(skb); 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds 160c6fda282SPavel Emelyanov void ip6_frag_init(struct inet_frag_queue *q, void *a) 161c6fda282SPavel Emelyanov { 162c6fda282SPavel Emelyanov struct frag_queue *fq = container_of(q, struct frag_queue, q); 163c6fda282SPavel Emelyanov struct ip6_create_arg *arg = a; 164c6fda282SPavel Emelyanov 165c6fda282SPavel Emelyanov fq->id = arg->id; 166c6fda282SPavel Emelyanov ipv6_addr_copy(&fq->saddr, arg->src); 167c6fda282SPavel Emelyanov ipv6_addr_copy(&fq->daddr, arg->dst); 168c6fda282SPavel Emelyanov } 169c6fda282SPavel Emelyanov EXPORT_SYMBOL(ip6_frag_init); 170c6fda282SPavel Emelyanov 1711da177e4SLinus Torvalds /* Destruction primitives. */ 1721da177e4SLinus Torvalds 1734b6cb5d8SPavel Emelyanov static __inline__ void fq_put(struct frag_queue *fq) 1741da177e4SLinus Torvalds { 175762cc408SPavel Emelyanov inet_frag_put(&fq->q, &ip6_frags); 1761da177e4SLinus Torvalds } 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds /* Kill fq entry. It is not destroyed immediately, 1791da177e4SLinus Torvalds * because caller (and someone more) holds reference count. 1801da177e4SLinus Torvalds */ 1811da177e4SLinus Torvalds static __inline__ void fq_kill(struct frag_queue *fq) 1821da177e4SLinus Torvalds { 183277e650dSPavel Emelyanov inet_frag_kill(&fq->q, &ip6_frags); 1841da177e4SLinus Torvalds } 1851da177e4SLinus Torvalds 1866ddc0822SPavel Emelyanov static void ip6_evictor(struct net *net, struct inet6_dev *idev) 1871da177e4SLinus Torvalds { 1888e7999c4SPavel Emelyanov int evicted; 1891da177e4SLinus Torvalds 1906ddc0822SPavel Emelyanov evicted = inet_frag_evictor(&net->ipv6.frags, &ip6_frags); 1918e7999c4SPavel Emelyanov if (evicted) 192821d5777SDenis V. Lunev IP6_ADD_STATS_BH(net, idev, IPSTATS_MIB_REASMFAILS, evicted); 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds static void ip6_frag_expire(unsigned long data) 1961da177e4SLinus Torvalds { 197e521db9dSPavel Emelyanov struct frag_queue *fq; 198a11d206dSYOSHIFUJI Hideaki struct net_device *dev = NULL; 1994ac2ccd0SDaniel Lezcano struct net *net; 2001da177e4SLinus Torvalds 201e521db9dSPavel Emelyanov fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q); 202e521db9dSPavel Emelyanov 2035ab11c98SPavel Emelyanov spin_lock(&fq->q.lock); 2041da177e4SLinus Torvalds 205bc578a54SJoe Perches if (fq->q.last_in & INET_FRAG_COMPLETE) 2061da177e4SLinus Torvalds goto out; 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds fq_kill(fq); 2091da177e4SLinus Torvalds 2104ac2ccd0SDaniel Lezcano net = container_of(fq->q.net, struct net, ipv6.frags); 2114ac2ccd0SDaniel Lezcano dev = dev_get_by_index(net, fq->iif); 212a11d206dSYOSHIFUJI Hideaki if (!dev) 213a11d206dSYOSHIFUJI Hideaki goto out; 214a11d206dSYOSHIFUJI Hideaki 215a11d206dSYOSHIFUJI Hideaki rcu_read_lock(); 216483a47d2SDenis V. Lunev IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT); 217483a47d2SDenis V. Lunev IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS); 218a11d206dSYOSHIFUJI Hideaki rcu_read_unlock(); 2191da177e4SLinus Torvalds 22078c784c4SIngo Oeser /* Don't send error if the first segment did not arrive. */ 221bc578a54SJoe Perches if (!(fq->q.last_in & INET_FRAG_FIRST_IN) || !fq->q.fragments) 22278c784c4SIngo Oeser goto out; 22378c784c4SIngo Oeser 2241da177e4SLinus Torvalds /* 2251da177e4SLinus Torvalds But use as source device on which LAST ARRIVED 2261da177e4SLinus Torvalds segment was received. And do not use fq->dev 2271da177e4SLinus Torvalds pointer directly, device might already disappeared. 2281da177e4SLinus Torvalds */ 2295ab11c98SPavel Emelyanov fq->q.fragments->dev = dev; 2305ab11c98SPavel Emelyanov icmpv6_send(fq->q.fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0, dev); 2311da177e4SLinus Torvalds out: 232a11d206dSYOSHIFUJI Hideaki if (dev) 233a11d206dSYOSHIFUJI Hideaki dev_put(dev); 2345ab11c98SPavel Emelyanov spin_unlock(&fq->q.lock); 2354b6cb5d8SPavel Emelyanov fq_put(fq); 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds 238abd6523dSPavel Emelyanov static __inline__ struct frag_queue * 239ac18e750SPavel Emelyanov fq_find(struct net *net, __be32 id, struct in6_addr *src, struct in6_addr *dst, 240abd6523dSPavel Emelyanov struct inet6_dev *idev) 2411da177e4SLinus Torvalds { 242c6fda282SPavel Emelyanov struct inet_frag_queue *q; 243c6fda282SPavel Emelyanov struct ip6_create_arg arg; 244abd6523dSPavel Emelyanov unsigned int hash; 2451da177e4SLinus Torvalds 246c6fda282SPavel Emelyanov arg.id = id; 247c6fda282SPavel Emelyanov arg.src = src; 248c6fda282SPavel Emelyanov arg.dst = dst; 2499a375803SPavel Emelyanov 2509a375803SPavel Emelyanov read_lock(&ip6_frags.lock); 25193c8b90fSIlpo Järvinen hash = inet6_hash_frag(id, src, dst, ip6_frags.rnd); 252c6fda282SPavel Emelyanov 253ac18e750SPavel Emelyanov q = inet_frag_find(&net->ipv6.frags, &ip6_frags, &arg, hash); 254c6fda282SPavel Emelyanov if (q == NULL) 2551da177e4SLinus Torvalds goto oom; 2561da177e4SLinus Torvalds 257c6fda282SPavel Emelyanov return container_of(q, struct frag_queue, q); 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds oom: 260483a47d2SDenis V. Lunev IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_REASMFAILS); 2611da177e4SLinus Torvalds return NULL; 2621da177e4SLinus Torvalds } 2631da177e4SLinus Torvalds 264f61944efSHerbert Xu static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb, 2651da177e4SLinus Torvalds struct frag_hdr *fhdr, int nhoff) 2661da177e4SLinus Torvalds { 2671da177e4SLinus Torvalds struct sk_buff *prev, *next; 268f61944efSHerbert Xu struct net_device *dev; 2691da177e4SLinus Torvalds int offset, end; 270adf30907SEric Dumazet struct net *net = dev_net(skb_dst(skb)->dev); 2711da177e4SLinus Torvalds 272bc578a54SJoe Perches if (fq->q.last_in & INET_FRAG_COMPLETE) 2731da177e4SLinus Torvalds goto err; 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds offset = ntohs(fhdr->frag_off) & ~0x7; 2760660e03fSArnaldo Carvalho de Melo end = offset + (ntohs(ipv6_hdr(skb)->payload_len) - 2770660e03fSArnaldo Carvalho de Melo ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1))); 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds if ((unsigned int)end > IPV6_MAXPLEN) { 280adf30907SEric Dumazet IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), 281a11d206dSYOSHIFUJI Hideaki IPSTATS_MIB_INHDRERRORS); 282d56f90a7SArnaldo Carvalho de Melo icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, 283d56f90a7SArnaldo Carvalho de Melo ((u8 *)&fhdr->frag_off - 284d56f90a7SArnaldo Carvalho de Melo skb_network_header(skb))); 285f61944efSHerbert Xu return -1; 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds 288d56f90a7SArnaldo Carvalho de Melo if (skb->ip_summed == CHECKSUM_COMPLETE) { 289d56f90a7SArnaldo Carvalho de Melo const unsigned char *nh = skb_network_header(skb); 2901da177e4SLinus Torvalds skb->csum = csum_sub(skb->csum, 291d56f90a7SArnaldo Carvalho de Melo csum_partial(nh, (u8 *)(fhdr + 1) - nh, 292d56f90a7SArnaldo Carvalho de Melo 0)); 293d56f90a7SArnaldo Carvalho de Melo } 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds /* Is this the final fragment? */ 2961da177e4SLinus Torvalds if (!(fhdr->frag_off & htons(IP6_MF))) { 2971da177e4SLinus Torvalds /* If we already have some bits beyond end 2981da177e4SLinus Torvalds * or have different end, the segment is corrupted. 2991da177e4SLinus Torvalds */ 3005ab11c98SPavel Emelyanov if (end < fq->q.len || 301bc578a54SJoe Perches ((fq->q.last_in & INET_FRAG_LAST_IN) && end != fq->q.len)) 3021da177e4SLinus Torvalds goto err; 303bc578a54SJoe Perches fq->q.last_in |= INET_FRAG_LAST_IN; 3045ab11c98SPavel Emelyanov fq->q.len = end; 3051da177e4SLinus Torvalds } else { 3061da177e4SLinus Torvalds /* Check if the fragment is rounded to 8 bytes. 3071da177e4SLinus Torvalds * Required by the RFC. 3081da177e4SLinus Torvalds */ 3091da177e4SLinus Torvalds if (end & 0x7) { 3101da177e4SLinus Torvalds /* RFC2460 says always send parameter problem in 3111da177e4SLinus Torvalds * this case. -DaveM 3121da177e4SLinus Torvalds */ 313adf30907SEric Dumazet IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), 314a11d206dSYOSHIFUJI Hideaki IPSTATS_MIB_INHDRERRORS); 3151da177e4SLinus Torvalds icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, 3161da177e4SLinus Torvalds offsetof(struct ipv6hdr, payload_len)); 317f61944efSHerbert Xu return -1; 3181da177e4SLinus Torvalds } 3195ab11c98SPavel Emelyanov if (end > fq->q.len) { 3201da177e4SLinus Torvalds /* Some bits beyond end -> corruption. */ 321bc578a54SJoe Perches if (fq->q.last_in & INET_FRAG_LAST_IN) 3221da177e4SLinus Torvalds goto err; 3235ab11c98SPavel Emelyanov fq->q.len = end; 3241da177e4SLinus Torvalds } 3251da177e4SLinus Torvalds } 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds if (end == offset) 3281da177e4SLinus Torvalds goto err; 3291da177e4SLinus Torvalds 3301da177e4SLinus Torvalds /* Point into the IP datagram 'data' part. */ 3311da177e4SLinus Torvalds if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) 3321da177e4SLinus Torvalds goto err; 33342ca89c1SStephen Hemminger 33442ca89c1SStephen Hemminger if (pskb_trim_rcsum(skb, end - offset)) 3351da177e4SLinus Torvalds goto err; 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds /* Find out which fragments are in front and at the back of us 3381da177e4SLinus Torvalds * in the chain of fragments so far. We must know where to put 3391da177e4SLinus Torvalds * this fragment, right? 3401da177e4SLinus Torvalds */ 3411da177e4SLinus Torvalds prev = NULL; 3425ab11c98SPavel Emelyanov for(next = fq->q.fragments; next != NULL; next = next->next) { 3431da177e4SLinus Torvalds if (FRAG6_CB(next)->offset >= offset) 3441da177e4SLinus Torvalds break; /* bingo! */ 3451da177e4SLinus Torvalds prev = next; 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds /* We found where to put this one. Check for overlap with 3491da177e4SLinus Torvalds * preceding fragment, and, if needed, align things so that 3501da177e4SLinus Torvalds * any overlaps are eliminated. 3511da177e4SLinus Torvalds */ 3521da177e4SLinus Torvalds if (prev) { 3531da177e4SLinus Torvalds int i = (FRAG6_CB(prev)->offset + prev->len) - offset; 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds if (i > 0) { 3561da177e4SLinus Torvalds offset += i; 3571da177e4SLinus Torvalds if (end <= offset) 3581da177e4SLinus Torvalds goto err; 3591da177e4SLinus Torvalds if (!pskb_pull(skb, i)) 3601da177e4SLinus Torvalds goto err; 3611da177e4SLinus Torvalds if (skb->ip_summed != CHECKSUM_UNNECESSARY) 3621da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 3631da177e4SLinus Torvalds } 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds /* Look for overlap with succeeding segments. 3671da177e4SLinus Torvalds * If we can merge fragments, do it. 3681da177e4SLinus Torvalds */ 3691da177e4SLinus Torvalds while (next && FRAG6_CB(next)->offset < end) { 3701da177e4SLinus Torvalds int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */ 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds if (i < next->len) { 3731da177e4SLinus Torvalds /* Eat head of the next overlapped fragment 3741da177e4SLinus Torvalds * and leave the loop. The next ones cannot overlap. 3751da177e4SLinus Torvalds */ 3761da177e4SLinus Torvalds if (!pskb_pull(next, i)) 3771da177e4SLinus Torvalds goto err; 3781da177e4SLinus Torvalds FRAG6_CB(next)->offset += i; /* next fragment */ 3795ab11c98SPavel Emelyanov fq->q.meat -= i; 3801da177e4SLinus Torvalds if (next->ip_summed != CHECKSUM_UNNECESSARY) 3811da177e4SLinus Torvalds next->ip_summed = CHECKSUM_NONE; 3821da177e4SLinus Torvalds break; 3831da177e4SLinus Torvalds } else { 3841da177e4SLinus Torvalds struct sk_buff *free_it = next; 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds /* Old fragment is completely overridden with 3871da177e4SLinus Torvalds * new one drop it. 3881da177e4SLinus Torvalds */ 3891da177e4SLinus Torvalds next = next->next; 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds if (prev) 3921da177e4SLinus Torvalds prev->next = next; 3931da177e4SLinus Torvalds else 3945ab11c98SPavel Emelyanov fq->q.fragments = next; 3951da177e4SLinus Torvalds 3965ab11c98SPavel Emelyanov fq->q.meat -= free_it->len; 3976ddc0822SPavel Emelyanov frag_kfree_skb(fq->q.net, free_it, NULL); 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds } 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds FRAG6_CB(skb)->offset = offset; 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds /* Insert this fragment in the chain of fragments. */ 4041da177e4SLinus Torvalds skb->next = next; 4051da177e4SLinus Torvalds if (prev) 4061da177e4SLinus Torvalds prev->next = skb; 4071da177e4SLinus Torvalds else 4085ab11c98SPavel Emelyanov fq->q.fragments = skb; 4091da177e4SLinus Torvalds 410f61944efSHerbert Xu dev = skb->dev; 411f61944efSHerbert Xu if (dev) { 412f61944efSHerbert Xu fq->iif = dev->ifindex; 4131da177e4SLinus Torvalds skb->dev = NULL; 414f61944efSHerbert Xu } 4155ab11c98SPavel Emelyanov fq->q.stamp = skb->tstamp; 4165ab11c98SPavel Emelyanov fq->q.meat += skb->len; 4176ddc0822SPavel Emelyanov atomic_add(skb->truesize, &fq->q.net->mem); 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds /* The first fragment. 4201da177e4SLinus Torvalds * nhoffset is obtained from the first fragment, of course. 4211da177e4SLinus Torvalds */ 4221da177e4SLinus Torvalds if (offset == 0) { 4231da177e4SLinus Torvalds fq->nhoffset = nhoff; 424bc578a54SJoe Perches fq->q.last_in |= INET_FRAG_FIRST_IN; 4251da177e4SLinus Torvalds } 426f61944efSHerbert Xu 427bc578a54SJoe Perches if (fq->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) && 428bc578a54SJoe Perches fq->q.meat == fq->q.len) 429f61944efSHerbert Xu return ip6_frag_reasm(fq, prev, dev); 430f61944efSHerbert Xu 4317eb95156SPavel Emelyanov write_lock(&ip6_frags.lock); 4323140c25cSPavel Emelyanov list_move_tail(&fq->q.lru_list, &fq->q.net->lru_list); 4337eb95156SPavel Emelyanov write_unlock(&ip6_frags.lock); 434f61944efSHerbert Xu return -1; 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds err: 437adf30907SEric Dumazet IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), 4383bd653c8SDenis V. Lunev IPSTATS_MIB_REASMFAILS); 4391da177e4SLinus Torvalds kfree_skb(skb); 440f61944efSHerbert Xu return -1; 4411da177e4SLinus Torvalds } 4421da177e4SLinus Torvalds 4431da177e4SLinus Torvalds /* 4441da177e4SLinus Torvalds * Check if this packet is complete. 4451da177e4SLinus Torvalds * Returns NULL on failure by any reason, and pointer 4461da177e4SLinus Torvalds * to current nexthdr field in reassembled frame. 4471da177e4SLinus Torvalds * 4481da177e4SLinus Torvalds * It is called with locked fq, and caller must check that 4491da177e4SLinus Torvalds * queue is eligible for reassembly i.e. it is not COMPLETE, 4501da177e4SLinus Torvalds * the last and the first frames arrived and all the bits are here. 4511da177e4SLinus Torvalds */ 452f61944efSHerbert Xu static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev, 4531da177e4SLinus Torvalds struct net_device *dev) 4541da177e4SLinus Torvalds { 4552bad35b7SJorge Boncompte [DTI2] struct net *net = container_of(fq->q.net, struct net, ipv6.frags); 4565ab11c98SPavel Emelyanov struct sk_buff *fp, *head = fq->q.fragments; 4571da177e4SLinus Torvalds int payload_len; 4581da177e4SLinus Torvalds unsigned int nhoff; 4591da177e4SLinus Torvalds 4601da177e4SLinus Torvalds fq_kill(fq); 4611da177e4SLinus Torvalds 462f61944efSHerbert Xu /* Make the one we just received the head. */ 463f61944efSHerbert Xu if (prev) { 464f61944efSHerbert Xu head = prev->next; 465f61944efSHerbert Xu fp = skb_clone(head, GFP_ATOMIC); 466f61944efSHerbert Xu 467f61944efSHerbert Xu if (!fp) 468f61944efSHerbert Xu goto out_oom; 469f61944efSHerbert Xu 470f61944efSHerbert Xu fp->next = head->next; 471f61944efSHerbert Xu prev->next = fp; 472f61944efSHerbert Xu 4735ab11c98SPavel Emelyanov skb_morph(head, fq->q.fragments); 4745ab11c98SPavel Emelyanov head->next = fq->q.fragments->next; 475f61944efSHerbert Xu 4765ab11c98SPavel Emelyanov kfree_skb(fq->q.fragments); 4775ab11c98SPavel Emelyanov fq->q.fragments = head; 478f61944efSHerbert Xu } 479f61944efSHerbert Xu 480547b792cSIlpo Järvinen WARN_ON(head == NULL); 481547b792cSIlpo Järvinen WARN_ON(FRAG6_CB(head)->offset != 0); 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds /* Unfragmented part is taken from the first segment. */ 484d56f90a7SArnaldo Carvalho de Melo payload_len = ((head->data - skb_network_header(head)) - 4855ab11c98SPavel Emelyanov sizeof(struct ipv6hdr) + fq->q.len - 486d56f90a7SArnaldo Carvalho de Melo sizeof(struct frag_hdr)); 4871da177e4SLinus Torvalds if (payload_len > IPV6_MAXPLEN) 4881da177e4SLinus Torvalds goto out_oversize; 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds /* Head of list must not be cloned. */ 4911da177e4SLinus Torvalds if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC)) 4921da177e4SLinus Torvalds goto out_oom; 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds /* If the first fragment is fragmented itself, we split 4951da177e4SLinus Torvalds * it to two chunks: the first with data and paged part 4961da177e4SLinus Torvalds * and the second, holding only fragments. */ 4971da177e4SLinus Torvalds if (skb_shinfo(head)->frag_list) { 4981da177e4SLinus Torvalds struct sk_buff *clone; 4991da177e4SLinus Torvalds int i, plen = 0; 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL) 5021da177e4SLinus Torvalds goto out_oom; 5031da177e4SLinus Torvalds clone->next = head->next; 5041da177e4SLinus Torvalds head->next = clone; 5051da177e4SLinus Torvalds skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list; 5061da177e4SLinus Torvalds skb_shinfo(head)->frag_list = NULL; 5071da177e4SLinus Torvalds for (i=0; i<skb_shinfo(head)->nr_frags; i++) 5081da177e4SLinus Torvalds plen += skb_shinfo(head)->frags[i].size; 5091da177e4SLinus Torvalds clone->len = clone->data_len = head->data_len - plen; 5101da177e4SLinus Torvalds head->data_len -= clone->len; 5111da177e4SLinus Torvalds head->len -= clone->len; 5121da177e4SLinus Torvalds clone->csum = 0; 5131da177e4SLinus Torvalds clone->ip_summed = head->ip_summed; 5146ddc0822SPavel Emelyanov atomic_add(clone->truesize, &fq->q.net->mem); 5151da177e4SLinus Torvalds } 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds /* We have to remove fragment header from datagram and to relocate 5181da177e4SLinus Torvalds * header in order to calculate ICV correctly. */ 5191da177e4SLinus Torvalds nhoff = fq->nhoffset; 520b0e380b1SArnaldo Carvalho de Melo skb_network_header(head)[nhoff] = skb_transport_header(head)[0]; 5211da177e4SLinus Torvalds memmove(head->head + sizeof(struct frag_hdr), head->head, 5221da177e4SLinus Torvalds (head->data - head->head) - sizeof(struct frag_hdr)); 523b0e380b1SArnaldo Carvalho de Melo head->mac_header += sizeof(struct frag_hdr); 524b0e380b1SArnaldo Carvalho de Melo head->network_header += sizeof(struct frag_hdr); 5251da177e4SLinus Torvalds 5261da177e4SLinus Torvalds skb_shinfo(head)->frag_list = head->next; 527badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(head); 528d56f90a7SArnaldo Carvalho de Melo skb_push(head, head->data - skb_network_header(head)); 5296ddc0822SPavel Emelyanov atomic_sub(head->truesize, &fq->q.net->mem); 5301da177e4SLinus Torvalds 5311da177e4SLinus Torvalds for (fp=head->next; fp; fp = fp->next) { 5321da177e4SLinus Torvalds head->data_len += fp->len; 5331da177e4SLinus Torvalds head->len += fp->len; 5341da177e4SLinus Torvalds if (head->ip_summed != fp->ip_summed) 5351da177e4SLinus Torvalds head->ip_summed = CHECKSUM_NONE; 53684fa7933SPatrick McHardy else if (head->ip_summed == CHECKSUM_COMPLETE) 5371da177e4SLinus Torvalds head->csum = csum_add(head->csum, fp->csum); 5381da177e4SLinus Torvalds head->truesize += fp->truesize; 5396ddc0822SPavel Emelyanov atomic_sub(fp->truesize, &fq->q.net->mem); 5401da177e4SLinus Torvalds } 5411da177e4SLinus Torvalds 5421da177e4SLinus Torvalds head->next = NULL; 5431da177e4SLinus Torvalds head->dev = dev; 5445ab11c98SPavel Emelyanov head->tstamp = fq->q.stamp; 5450660e03fSArnaldo Carvalho de Melo ipv6_hdr(head)->payload_len = htons(payload_len); 546951dbc8aSPatrick McHardy IP6CB(head)->nhoff = nhoff; 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds /* Yes, and fold redundant checksum back. 8) */ 54984fa7933SPatrick McHardy if (head->ip_summed == CHECKSUM_COMPLETE) 550d56f90a7SArnaldo Carvalho de Melo head->csum = csum_partial(skb_network_header(head), 551cfe1fc77SArnaldo Carvalho de Melo skb_network_header_len(head), 552d56f90a7SArnaldo Carvalho de Melo head->csum); 5531da177e4SLinus Torvalds 554a11d206dSYOSHIFUJI Hideaki rcu_read_lock(); 5552bad35b7SJorge Boncompte [DTI2] IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMOKS); 556a11d206dSYOSHIFUJI Hideaki rcu_read_unlock(); 5575ab11c98SPavel Emelyanov fq->q.fragments = NULL; 5581da177e4SLinus Torvalds return 1; 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds out_oversize: 5611da177e4SLinus Torvalds if (net_ratelimit()) 5621da177e4SLinus Torvalds printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len); 5631da177e4SLinus Torvalds goto out_fail; 5641da177e4SLinus Torvalds out_oom: 5651da177e4SLinus Torvalds if (net_ratelimit()) 5661da177e4SLinus Torvalds printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n"); 5671da177e4SLinus Torvalds out_fail: 568a11d206dSYOSHIFUJI Hideaki rcu_read_lock(); 5692bad35b7SJorge Boncompte [DTI2] IP6_INC_STATS_BH(net, __in6_dev_get(dev), IPSTATS_MIB_REASMFAILS); 570a11d206dSYOSHIFUJI Hideaki rcu_read_unlock(); 5711da177e4SLinus Torvalds return -1; 5721da177e4SLinus Torvalds } 5731da177e4SLinus Torvalds 574e5bbef20SHerbert Xu static int ipv6_frag_rcv(struct sk_buff *skb) 5751da177e4SLinus Torvalds { 5761da177e4SLinus Torvalds struct frag_hdr *fhdr; 5771da177e4SLinus Torvalds struct frag_queue *fq; 5780660e03fSArnaldo Carvalho de Melo struct ipv6hdr *hdr = ipv6_hdr(skb); 579adf30907SEric Dumazet struct net *net = dev_net(skb_dst(skb)->dev); 5801da177e4SLinus Torvalds 581adf30907SEric Dumazet IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMREQDS); 5821da177e4SLinus Torvalds 5831da177e4SLinus Torvalds /* Jumbo payload inhibits frag. header */ 58498b3377cSDenis V. Lunev if (hdr->payload_len==0) 58598b3377cSDenis V. Lunev goto fail_hdr; 58698b3377cSDenis V. Lunev 587ea2ae17dSArnaldo Carvalho de Melo if (!pskb_may_pull(skb, (skb_transport_offset(skb) + 58898b3377cSDenis V. Lunev sizeof(struct frag_hdr)))) 58998b3377cSDenis V. Lunev goto fail_hdr; 5901da177e4SLinus Torvalds 5910660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 5929c70220bSArnaldo Carvalho de Melo fhdr = (struct frag_hdr *)skb_transport_header(skb); 5931da177e4SLinus Torvalds 5941da177e4SLinus Torvalds if (!(fhdr->frag_off & htons(0xFFF9))) { 5951da177e4SLinus Torvalds /* It is not a fragmented frame */ 596b0e380b1SArnaldo Carvalho de Melo skb->transport_header += sizeof(struct frag_hdr); 597483a47d2SDenis V. Lunev IP6_INC_STATS_BH(net, 598adf30907SEric Dumazet ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMOKS); 5991da177e4SLinus Torvalds 600d56f90a7SArnaldo Carvalho de Melo IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb); 6011da177e4SLinus Torvalds return 1; 6021da177e4SLinus Torvalds } 6031da177e4SLinus Torvalds 604e31e0bdcSPavel Emelyanov if (atomic_read(&net->ipv6.frags.mem) > net->ipv6.frags.high_thresh) 605adf30907SEric Dumazet ip6_evictor(net, ip6_dst_idev(skb_dst(skb))); 6061da177e4SLinus Torvalds 607ac18e750SPavel Emelyanov if ((fq = fq_find(net, fhdr->identification, &hdr->saddr, &hdr->daddr, 608adf30907SEric Dumazet ip6_dst_idev(skb_dst(skb)))) != NULL) { 609f61944efSHerbert Xu int ret; 6101da177e4SLinus Torvalds 6115ab11c98SPavel Emelyanov spin_lock(&fq->q.lock); 6121da177e4SLinus Torvalds 613f61944efSHerbert Xu ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff); 6141da177e4SLinus Torvalds 6155ab11c98SPavel Emelyanov spin_unlock(&fq->q.lock); 6164b6cb5d8SPavel Emelyanov fq_put(fq); 6171da177e4SLinus Torvalds return ret; 6181da177e4SLinus Torvalds } 6191da177e4SLinus Torvalds 620adf30907SEric Dumazet IP6_INC_STATS_BH(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_REASMFAILS); 6211da177e4SLinus Torvalds kfree_skb(skb); 6221da177e4SLinus Torvalds return -1; 62398b3377cSDenis V. Lunev 62498b3377cSDenis V. Lunev fail_hdr: 625adf30907SEric Dumazet IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS); 62698b3377cSDenis V. Lunev icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, skb_network_header_len(skb)); 62798b3377cSDenis V. Lunev return -1; 6281da177e4SLinus Torvalds } 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds static struct inet6_protocol frag_protocol = 6311da177e4SLinus Torvalds { 6321da177e4SLinus Torvalds .handler = ipv6_frag_rcv, 6331da177e4SLinus Torvalds .flags = INET6_PROTO_NOPOLICY, 6341da177e4SLinus Torvalds }; 6351da177e4SLinus Torvalds 6368d8354d2SPavel Emelyanov #ifdef CONFIG_SYSCTL 6370a64b4b8SPavel Emelyanov static struct ctl_table ip6_frags_ns_ctl_table[] = { 638e71e0349SDaniel Lezcano { 6398d8354d2SPavel Emelyanov .ctl_name = NET_IPV6_IP6FRAG_HIGH_THRESH, 6408d8354d2SPavel Emelyanov .procname = "ip6frag_high_thresh", 641e31e0bdcSPavel Emelyanov .data = &init_net.ipv6.frags.high_thresh, 6428d8354d2SPavel Emelyanov .maxlen = sizeof(int), 6438d8354d2SPavel Emelyanov .mode = 0644, 6446d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec 6458d8354d2SPavel Emelyanov }, 6468d8354d2SPavel Emelyanov { 6478d8354d2SPavel Emelyanov .ctl_name = NET_IPV6_IP6FRAG_LOW_THRESH, 6488d8354d2SPavel Emelyanov .procname = "ip6frag_low_thresh", 649e31e0bdcSPavel Emelyanov .data = &init_net.ipv6.frags.low_thresh, 6508d8354d2SPavel Emelyanov .maxlen = sizeof(int), 6518d8354d2SPavel Emelyanov .mode = 0644, 6526d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec 6538d8354d2SPavel Emelyanov }, 6548d8354d2SPavel Emelyanov { 6558d8354d2SPavel Emelyanov .ctl_name = NET_IPV6_IP6FRAG_TIME, 6568d8354d2SPavel Emelyanov .procname = "ip6frag_time", 657b2fd5321SPavel Emelyanov .data = &init_net.ipv6.frags.timeout, 6588d8354d2SPavel Emelyanov .maxlen = sizeof(int), 6598d8354d2SPavel Emelyanov .mode = 0644, 6606d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 6616d9f239aSAlexey Dobriyan .strategy = sysctl_jiffies, 6628d8354d2SPavel Emelyanov }, 6637d291ebbSPavel Emelyanov { } 6647d291ebbSPavel Emelyanov }; 6657d291ebbSPavel Emelyanov 6667d291ebbSPavel Emelyanov static struct ctl_table ip6_frags_ctl_table[] = { 6678d8354d2SPavel Emelyanov { 6688d8354d2SPavel Emelyanov .ctl_name = NET_IPV6_IP6FRAG_SECRET_INTERVAL, 6698d8354d2SPavel Emelyanov .procname = "ip6frag_secret_interval", 6703b4bc4a2SPavel Emelyanov .data = &ip6_frags.secret_interval, 6718d8354d2SPavel Emelyanov .maxlen = sizeof(int), 6728d8354d2SPavel Emelyanov .mode = 0644, 6736d9f239aSAlexey Dobriyan .proc_handler = proc_dointvec_jiffies, 6746d9f239aSAlexey Dobriyan .strategy = sysctl_jiffies 6758d8354d2SPavel Emelyanov }, 6768d8354d2SPavel Emelyanov { } 6778d8354d2SPavel Emelyanov }; 6787d460db9SDaniel Lezcano 6790a64b4b8SPavel Emelyanov static int ip6_frags_ns_sysctl_register(struct net *net) 6808d8354d2SPavel Emelyanov { 681e4a2d5c2SPavel Emelyanov struct ctl_table *table; 6828d8354d2SPavel Emelyanov struct ctl_table_header *hdr; 6838d8354d2SPavel Emelyanov 6840a64b4b8SPavel Emelyanov table = ip6_frags_ns_ctl_table; 685e4a2d5c2SPavel Emelyanov if (net != &init_net) { 6860a64b4b8SPavel Emelyanov table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL); 687e4a2d5c2SPavel Emelyanov if (table == NULL) 688e4a2d5c2SPavel Emelyanov goto err_alloc; 689e4a2d5c2SPavel Emelyanov 690e31e0bdcSPavel Emelyanov table[0].data = &net->ipv6.frags.high_thresh; 691e31e0bdcSPavel Emelyanov table[1].data = &net->ipv6.frags.low_thresh; 692b2fd5321SPavel Emelyanov table[2].data = &net->ipv6.frags.timeout; 693e4a2d5c2SPavel Emelyanov } 694e4a2d5c2SPavel Emelyanov 695e4a2d5c2SPavel Emelyanov hdr = register_net_sysctl_table(net, net_ipv6_ctl_path, table); 696e4a2d5c2SPavel Emelyanov if (hdr == NULL) 697e4a2d5c2SPavel Emelyanov goto err_reg; 698e4a2d5c2SPavel Emelyanov 699e4a2d5c2SPavel Emelyanov net->ipv6.sysctl.frags_hdr = hdr; 700e4a2d5c2SPavel Emelyanov return 0; 701e4a2d5c2SPavel Emelyanov 702e4a2d5c2SPavel Emelyanov err_reg: 703e4a2d5c2SPavel Emelyanov if (net != &init_net) 704e4a2d5c2SPavel Emelyanov kfree(table); 705e4a2d5c2SPavel Emelyanov err_alloc: 706e4a2d5c2SPavel Emelyanov return -ENOMEM; 707e4a2d5c2SPavel Emelyanov } 708e4a2d5c2SPavel Emelyanov 7090a64b4b8SPavel Emelyanov static void ip6_frags_ns_sysctl_unregister(struct net *net) 710e4a2d5c2SPavel Emelyanov { 711e4a2d5c2SPavel Emelyanov struct ctl_table *table; 712e4a2d5c2SPavel Emelyanov 713e4a2d5c2SPavel Emelyanov table = net->ipv6.sysctl.frags_hdr->ctl_table_arg; 714e4a2d5c2SPavel Emelyanov unregister_net_sysctl_table(net->ipv6.sysctl.frags_hdr); 715e4a2d5c2SPavel Emelyanov kfree(table); 7168d8354d2SPavel Emelyanov } 7177d291ebbSPavel Emelyanov 7187d291ebbSPavel Emelyanov static struct ctl_table_header *ip6_ctl_header; 7197d291ebbSPavel Emelyanov 7207d291ebbSPavel Emelyanov static int ip6_frags_sysctl_register(void) 7217d291ebbSPavel Emelyanov { 7227d291ebbSPavel Emelyanov ip6_ctl_header = register_net_sysctl_rotable(net_ipv6_ctl_path, 7237d291ebbSPavel Emelyanov ip6_frags_ctl_table); 7247d291ebbSPavel Emelyanov return ip6_ctl_header == NULL ? -ENOMEM : 0; 7257d291ebbSPavel Emelyanov } 7267d291ebbSPavel Emelyanov 7277d291ebbSPavel Emelyanov static void ip6_frags_sysctl_unregister(void) 7287d291ebbSPavel Emelyanov { 7297d291ebbSPavel Emelyanov unregister_net_sysctl_table(ip6_ctl_header); 7307d291ebbSPavel Emelyanov } 7318d8354d2SPavel Emelyanov #else 7320a64b4b8SPavel Emelyanov static inline int ip6_frags_ns_sysctl_register(struct net *net) 7338d8354d2SPavel Emelyanov { 7348d8354d2SPavel Emelyanov return 0; 7358d8354d2SPavel Emelyanov } 736e4a2d5c2SPavel Emelyanov 7370a64b4b8SPavel Emelyanov static inline void ip6_frags_ns_sysctl_unregister(struct net *net) 738e4a2d5c2SPavel Emelyanov { 739e4a2d5c2SPavel Emelyanov } 7407d291ebbSPavel Emelyanov 7417d291ebbSPavel Emelyanov static inline int ip6_frags_sysctl_register(void) 7427d291ebbSPavel Emelyanov { 7437d291ebbSPavel Emelyanov return 0; 7447d291ebbSPavel Emelyanov } 7457d291ebbSPavel Emelyanov 7467d291ebbSPavel Emelyanov static inline void ip6_frags_sysctl_unregister(void) 7477d291ebbSPavel Emelyanov { 7487d291ebbSPavel Emelyanov } 7498d8354d2SPavel Emelyanov #endif 7508d8354d2SPavel Emelyanov 7518d8354d2SPavel Emelyanov static int ipv6_frags_init_net(struct net *net) 7528d8354d2SPavel Emelyanov { 753e31e0bdcSPavel Emelyanov net->ipv6.frags.high_thresh = 256 * 1024; 754e31e0bdcSPavel Emelyanov net->ipv6.frags.low_thresh = 192 * 1024; 755b2fd5321SPavel Emelyanov net->ipv6.frags.timeout = IPV6_FRAG_TIMEOUT; 7568d8354d2SPavel Emelyanov 757e5a2bb84SPavel Emelyanov inet_frags_init_net(&net->ipv6.frags); 758e5a2bb84SPavel Emelyanov 7590a64b4b8SPavel Emelyanov return ip6_frags_ns_sysctl_register(net); 760e71e0349SDaniel Lezcano } 761e71e0349SDaniel Lezcano 76281566e83SPavel Emelyanov static void ipv6_frags_exit_net(struct net *net) 76381566e83SPavel Emelyanov { 7640a64b4b8SPavel Emelyanov ip6_frags_ns_sysctl_unregister(net); 76581566e83SPavel Emelyanov inet_frags_exit_net(&net->ipv6.frags, &ip6_frags); 76681566e83SPavel Emelyanov } 76781566e83SPavel Emelyanov 76881566e83SPavel Emelyanov static struct pernet_operations ip6_frags_ops = { 76981566e83SPavel Emelyanov .init = ipv6_frags_init_net, 77081566e83SPavel Emelyanov .exit = ipv6_frags_exit_net, 77181566e83SPavel Emelyanov }; 77281566e83SPavel Emelyanov 773853cbbaaSDaniel Lezcano int __init ipv6_frag_init(void) 7741da177e4SLinus Torvalds { 775853cbbaaSDaniel Lezcano int ret; 7761da177e4SLinus Torvalds 777853cbbaaSDaniel Lezcano ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT); 778853cbbaaSDaniel Lezcano if (ret) 779853cbbaaSDaniel Lezcano goto out; 780e71e0349SDaniel Lezcano 7817d291ebbSPavel Emelyanov ret = ip6_frags_sysctl_register(); 7827d291ebbSPavel Emelyanov if (ret) 7837d291ebbSPavel Emelyanov goto err_sysctl; 7847d291ebbSPavel Emelyanov 7850002c630SPavel Emelyanov ret = register_pernet_subsys(&ip6_frags_ops); 7860002c630SPavel Emelyanov if (ret) 7870002c630SPavel Emelyanov goto err_pernet; 7888d8354d2SPavel Emelyanov 789321a3a99SPavel Emelyanov ip6_frags.hashfn = ip6_hashfn; 790c6fda282SPavel Emelyanov ip6_frags.constructor = ip6_frag_init; 791c9547709SPavel Emelyanov ip6_frags.destructor = NULL; 7921e4b8287SPavel Emelyanov ip6_frags.skb_free = NULL; 7931e4b8287SPavel Emelyanov ip6_frags.qsize = sizeof(struct frag_queue); 794abd6523dSPavel Emelyanov ip6_frags.match = ip6_frag_match; 795e521db9dSPavel Emelyanov ip6_frags.frag_expire = ip6_frag_expire; 7963b4bc4a2SPavel Emelyanov ip6_frags.secret_interval = 10 * 60 * HZ; 7977eb95156SPavel Emelyanov inet_frags_init(&ip6_frags); 798853cbbaaSDaniel Lezcano out: 799853cbbaaSDaniel Lezcano return ret; 8000002c630SPavel Emelyanov 8010002c630SPavel Emelyanov err_pernet: 8027d291ebbSPavel Emelyanov ip6_frags_sysctl_unregister(); 8037d291ebbSPavel Emelyanov err_sysctl: 8040002c630SPavel Emelyanov inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT); 8050002c630SPavel Emelyanov goto out; 806853cbbaaSDaniel Lezcano } 807853cbbaaSDaniel Lezcano 808853cbbaaSDaniel Lezcano void ipv6_frag_exit(void) 809853cbbaaSDaniel Lezcano { 810853cbbaaSDaniel Lezcano inet_frags_fini(&ip6_frags); 8117d291ebbSPavel Emelyanov ip6_frags_sysctl_unregister(); 81281566e83SPavel Emelyanov unregister_pernet_subsys(&ip6_frags_ops); 813853cbbaaSDaniel Lezcano inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT); 8141da177e4SLinus Torvalds } 815