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 * $Id: reassembly.c,v 1.26 2001/03/07 22:00:57 davem Exp $ 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * Based on: net/ipv4/ip_fragment.c 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 131da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 141da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 151da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 161da177e4SLinus Torvalds */ 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds /* 191da177e4SLinus Torvalds * Fixes: 201da177e4SLinus Torvalds * Andi Kleen Make it work with multiple hosts. 211da177e4SLinus Torvalds * More RFC compliance. 221da177e4SLinus Torvalds * 231da177e4SLinus Torvalds * Horst von Brand Add missing #include <linux/string.h> 241da177e4SLinus Torvalds * Alexey Kuznetsov SMP races, threading, cleanup. 251da177e4SLinus Torvalds * Patrick McHardy LRU queue of frag heads for evictor. 261da177e4SLinus Torvalds * Mitsuru KANDA @USAGI Register inet6_protocol{}. 271da177e4SLinus Torvalds * David Stevens and 281da177e4SLinus Torvalds * YOSHIFUJI,H. @USAGI Always remove fragment header to 291da177e4SLinus Torvalds * calculate ICV correctly. 301da177e4SLinus Torvalds */ 311da177e4SLinus Torvalds #include <linux/errno.h> 321da177e4SLinus Torvalds #include <linux/types.h> 331da177e4SLinus Torvalds #include <linux/string.h> 341da177e4SLinus Torvalds #include <linux/socket.h> 351da177e4SLinus Torvalds #include <linux/sockios.h> 361da177e4SLinus Torvalds #include <linux/jiffies.h> 371da177e4SLinus Torvalds #include <linux/net.h> 381da177e4SLinus Torvalds #include <linux/list.h> 391da177e4SLinus Torvalds #include <linux/netdevice.h> 401da177e4SLinus Torvalds #include <linux/in6.h> 411da177e4SLinus Torvalds #include <linux/ipv6.h> 421da177e4SLinus Torvalds #include <linux/icmpv6.h> 431da177e4SLinus Torvalds #include <linux/random.h> 441da177e4SLinus Torvalds #include <linux/jhash.h> 45f61944efSHerbert Xu #include <linux/skbuff.h> 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds #include <net/sock.h> 481da177e4SLinus Torvalds #include <net/snmp.h> 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds #include <net/ipv6.h> 51a11d206dSYOSHIFUJI Hideaki #include <net/ip6_route.h> 521da177e4SLinus Torvalds #include <net/protocol.h> 531da177e4SLinus Torvalds #include <net/transp_v6.h> 541da177e4SLinus Torvalds #include <net/rawv6.h> 551da177e4SLinus Torvalds #include <net/ndisc.h> 561da177e4SLinus Torvalds #include <net/addrconf.h> 575ab11c98SPavel Emelyanov #include <net/inet_frag.h> 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds struct ip6frag_skb_cb 601da177e4SLinus Torvalds { 611da177e4SLinus Torvalds struct inet6_skb_parm h; 621da177e4SLinus Torvalds int offset; 631da177e4SLinus Torvalds }; 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds #define FRAG6_CB(skb) ((struct ip6frag_skb_cb*)((skb)->cb)) 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds /* 691da177e4SLinus Torvalds * Equivalent of ipv4 struct ipq 701da177e4SLinus Torvalds */ 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds struct frag_queue 731da177e4SLinus Torvalds { 745ab11c98SPavel Emelyanov struct inet_frag_queue q; 751da177e4SLinus Torvalds 76e69a4adcSAl Viro __be32 id; /* fragment id */ 771da177e4SLinus Torvalds struct in6_addr saddr; 781da177e4SLinus Torvalds struct in6_addr daddr; 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds int iif; 811da177e4SLinus Torvalds unsigned int csum; 821da177e4SLinus Torvalds __u16 nhoffset; 831da177e4SLinus Torvalds }; 841da177e4SLinus Torvalds 8504128f23SPavel Emelyanov struct inet_frags_ctl ip6_frags_ctl __read_mostly = { 8604128f23SPavel Emelyanov .high_thresh = 256 * 1024, 8704128f23SPavel Emelyanov .low_thresh = 192 * 1024, 8804128f23SPavel Emelyanov .timeout = IPV6_FRAG_TIMEOUT, 8904128f23SPavel Emelyanov .secret_interval = 10 * 60 * HZ, 9004128f23SPavel Emelyanov }; 9104128f23SPavel Emelyanov 927eb95156SPavel Emelyanov static struct inet_frags ip6_frags; 931da177e4SLinus Torvalds 947eb95156SPavel Emelyanov int ip6_frag_nqueues(void) 957eb95156SPavel Emelyanov { 967eb95156SPavel Emelyanov return ip6_frags.nqueues; 977eb95156SPavel Emelyanov } 981da177e4SLinus Torvalds 997eb95156SPavel Emelyanov int ip6_frag_mem(void) 1007eb95156SPavel Emelyanov { 1017eb95156SPavel Emelyanov return atomic_read(&ip6_frags.mem); 1027eb95156SPavel Emelyanov } 1031da177e4SLinus Torvalds 104f61944efSHerbert Xu static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev, 105f61944efSHerbert Xu struct net_device *dev); 106f61944efSHerbert Xu 107f6596f9dSZach Brown /* 108f6596f9dSZach Brown * callers should be careful not to use the hash value outside the ipfrag_lock 109f6596f9dSZach Brown * as doing so could race with ipfrag_hash_rnd being recalculated. 110f6596f9dSZach Brown */ 111e69a4adcSAl Viro static unsigned int ip6qhashfn(__be32 id, struct in6_addr *saddr, 1121da177e4SLinus Torvalds struct in6_addr *daddr) 1131da177e4SLinus Torvalds { 1141da177e4SLinus Torvalds u32 a, b, c; 1151da177e4SLinus Torvalds 116e69a4adcSAl Viro a = (__force u32)saddr->s6_addr32[0]; 117e69a4adcSAl Viro b = (__force u32)saddr->s6_addr32[1]; 118e69a4adcSAl Viro c = (__force u32)saddr->s6_addr32[2]; 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds a += JHASH_GOLDEN_RATIO; 1211da177e4SLinus Torvalds b += JHASH_GOLDEN_RATIO; 1227eb95156SPavel Emelyanov c += ip6_frags.rnd; 1231da177e4SLinus Torvalds __jhash_mix(a, b, c); 1241da177e4SLinus Torvalds 125e69a4adcSAl Viro a += (__force u32)saddr->s6_addr32[3]; 126e69a4adcSAl Viro b += (__force u32)daddr->s6_addr32[0]; 127e69a4adcSAl Viro c += (__force u32)daddr->s6_addr32[1]; 1281da177e4SLinus Torvalds __jhash_mix(a, b, c); 1291da177e4SLinus Torvalds 130e69a4adcSAl Viro a += (__force u32)daddr->s6_addr32[2]; 131e69a4adcSAl Viro b += (__force u32)daddr->s6_addr32[3]; 132e69a4adcSAl Viro c += (__force u32)id; 1331da177e4SLinus Torvalds __jhash_mix(a, b, c); 1341da177e4SLinus Torvalds 1357eb95156SPavel Emelyanov return c & (INETFRAGS_HASHSZ - 1); 1361da177e4SLinus Torvalds } 1371da177e4SLinus Torvalds 138321a3a99SPavel Emelyanov static unsigned int ip6_hashfn(struct inet_frag_queue *q) 1391da177e4SLinus Torvalds { 140321a3a99SPavel Emelyanov struct frag_queue *fq; 1411da177e4SLinus Torvalds 142321a3a99SPavel Emelyanov fq = container_of(q, struct frag_queue, q); 143321a3a99SPavel Emelyanov return ip6qhashfn(fq->id, &fq->saddr, &fq->daddr); 1441da177e4SLinus Torvalds } 1451da177e4SLinus Torvalds 146abd6523dSPavel Emelyanov int ip6_frag_match(struct inet_frag_queue *q, void *a) 147abd6523dSPavel Emelyanov { 148abd6523dSPavel Emelyanov struct frag_queue *fq; 149abd6523dSPavel Emelyanov struct ip6_create_arg *arg = a; 150abd6523dSPavel Emelyanov 151abd6523dSPavel Emelyanov fq = container_of(q, struct frag_queue, q); 152abd6523dSPavel Emelyanov return (fq->id == arg->id && 153abd6523dSPavel Emelyanov ipv6_addr_equal(&fq->saddr, arg->src) && 154abd6523dSPavel Emelyanov ipv6_addr_equal(&fq->daddr, arg->dst)); 155abd6523dSPavel Emelyanov } 156abd6523dSPavel Emelyanov EXPORT_SYMBOL(ip6_frag_match); 157abd6523dSPavel Emelyanov 1581da177e4SLinus Torvalds /* Memory Tracking Functions. */ 1591da177e4SLinus Torvalds static inline void frag_kfree_skb(struct sk_buff *skb, int *work) 1601da177e4SLinus Torvalds { 1611da177e4SLinus Torvalds if (work) 1621da177e4SLinus Torvalds *work -= skb->truesize; 1637eb95156SPavel Emelyanov atomic_sub(skb->truesize, &ip6_frags.mem); 1641da177e4SLinus Torvalds kfree_skb(skb); 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds 167c6fda282SPavel Emelyanov void ip6_frag_init(struct inet_frag_queue *q, void *a) 168c6fda282SPavel Emelyanov { 169c6fda282SPavel Emelyanov struct frag_queue *fq = container_of(q, struct frag_queue, q); 170c6fda282SPavel Emelyanov struct ip6_create_arg *arg = a; 171c6fda282SPavel Emelyanov 172c6fda282SPavel Emelyanov fq->id = arg->id; 173c6fda282SPavel Emelyanov ipv6_addr_copy(&fq->saddr, arg->src); 174c6fda282SPavel Emelyanov ipv6_addr_copy(&fq->daddr, arg->dst); 175c6fda282SPavel Emelyanov } 176c6fda282SPavel Emelyanov EXPORT_SYMBOL(ip6_frag_init); 177c6fda282SPavel Emelyanov 1781da177e4SLinus Torvalds /* Destruction primitives. */ 1791da177e4SLinus Torvalds 1804b6cb5d8SPavel Emelyanov static __inline__ void fq_put(struct frag_queue *fq) 1811da177e4SLinus Torvalds { 182762cc408SPavel Emelyanov inet_frag_put(&fq->q, &ip6_frags); 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds /* Kill fq entry. It is not destroyed immediately, 1861da177e4SLinus Torvalds * because caller (and someone more) holds reference count. 1871da177e4SLinus Torvalds */ 1881da177e4SLinus Torvalds static __inline__ void fq_kill(struct frag_queue *fq) 1891da177e4SLinus Torvalds { 190277e650dSPavel Emelyanov inet_frag_kill(&fq->q, &ip6_frags); 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds 193a11d206dSYOSHIFUJI Hideaki static void ip6_evictor(struct inet6_dev *idev) 1941da177e4SLinus Torvalds { 1958e7999c4SPavel Emelyanov int evicted; 1961da177e4SLinus Torvalds 1978e7999c4SPavel Emelyanov evicted = inet_frag_evictor(&ip6_frags); 1988e7999c4SPavel Emelyanov if (evicted) 1998e7999c4SPavel Emelyanov IP6_ADD_STATS_BH(idev, IPSTATS_MIB_REASMFAILS, evicted); 2001da177e4SLinus Torvalds } 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds static void ip6_frag_expire(unsigned long data) 2031da177e4SLinus Torvalds { 204e521db9dSPavel Emelyanov struct frag_queue *fq; 205a11d206dSYOSHIFUJI Hideaki struct net_device *dev = NULL; 2061da177e4SLinus Torvalds 207e521db9dSPavel Emelyanov fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q); 208e521db9dSPavel Emelyanov 2095ab11c98SPavel Emelyanov spin_lock(&fq->q.lock); 2101da177e4SLinus Torvalds 2115ab11c98SPavel Emelyanov if (fq->q.last_in & COMPLETE) 2121da177e4SLinus Torvalds goto out; 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds fq_kill(fq); 2151da177e4SLinus Torvalds 216881d966bSEric W. Biederman dev = dev_get_by_index(&init_net, fq->iif); 217a11d206dSYOSHIFUJI Hideaki if (!dev) 218a11d206dSYOSHIFUJI Hideaki goto out; 219a11d206dSYOSHIFUJI Hideaki 220a11d206dSYOSHIFUJI Hideaki rcu_read_lock(); 221a11d206dSYOSHIFUJI Hideaki IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT); 222a11d206dSYOSHIFUJI Hideaki IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS); 223a11d206dSYOSHIFUJI Hideaki rcu_read_unlock(); 2241da177e4SLinus Torvalds 22578c784c4SIngo Oeser /* Don't send error if the first segment did not arrive. */ 2265ab11c98SPavel Emelyanov if (!(fq->q.last_in&FIRST_IN) || !fq->q.fragments) 22778c784c4SIngo Oeser goto out; 22878c784c4SIngo Oeser 2291da177e4SLinus Torvalds /* 2301da177e4SLinus Torvalds But use as source device on which LAST ARRIVED 2311da177e4SLinus Torvalds segment was received. And do not use fq->dev 2321da177e4SLinus Torvalds pointer directly, device might already disappeared. 2331da177e4SLinus Torvalds */ 2345ab11c98SPavel Emelyanov fq->q.fragments->dev = dev; 2355ab11c98SPavel Emelyanov icmpv6_send(fq->q.fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0, dev); 2361da177e4SLinus Torvalds out: 237a11d206dSYOSHIFUJI Hideaki if (dev) 238a11d206dSYOSHIFUJI Hideaki dev_put(dev); 2395ab11c98SPavel Emelyanov spin_unlock(&fq->q.lock); 2404b6cb5d8SPavel Emelyanov fq_put(fq); 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds 243abd6523dSPavel Emelyanov static __inline__ struct frag_queue * 244abd6523dSPavel Emelyanov fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst, 245abd6523dSPavel Emelyanov struct inet6_dev *idev) 2461da177e4SLinus Torvalds { 247c6fda282SPavel Emelyanov struct inet_frag_queue *q; 248c6fda282SPavel Emelyanov struct ip6_create_arg arg; 249abd6523dSPavel Emelyanov unsigned int hash; 2501da177e4SLinus Torvalds 251c6fda282SPavel Emelyanov arg.id = id; 252c6fda282SPavel Emelyanov arg.src = src; 253c6fda282SPavel Emelyanov arg.dst = dst; 254abd6523dSPavel Emelyanov hash = ip6qhashfn(id, src, dst); 255c6fda282SPavel Emelyanov 256abd6523dSPavel Emelyanov q = inet_frag_find(&ip6_frags, &arg, hash); 257c6fda282SPavel Emelyanov if (q == NULL) 2581da177e4SLinus Torvalds goto oom; 2591da177e4SLinus Torvalds 260c6fda282SPavel Emelyanov return container_of(q, struct frag_queue, q); 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds oom: 263a11d206dSYOSHIFUJI Hideaki IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS); 2641da177e4SLinus Torvalds return NULL; 2651da177e4SLinus Torvalds } 2661da177e4SLinus Torvalds 267f61944efSHerbert Xu static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb, 2681da177e4SLinus Torvalds struct frag_hdr *fhdr, int nhoff) 2691da177e4SLinus Torvalds { 2701da177e4SLinus Torvalds struct sk_buff *prev, *next; 271f61944efSHerbert Xu struct net_device *dev; 2721da177e4SLinus Torvalds int offset, end; 2731da177e4SLinus Torvalds 2745ab11c98SPavel Emelyanov if (fq->q.last_in & COMPLETE) 2751da177e4SLinus Torvalds goto err; 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds offset = ntohs(fhdr->frag_off) & ~0x7; 2780660e03fSArnaldo Carvalho de Melo end = offset + (ntohs(ipv6_hdr(skb)->payload_len) - 2790660e03fSArnaldo Carvalho de Melo ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1))); 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds if ((unsigned int)end > IPV6_MAXPLEN) { 282a11d206dSYOSHIFUJI Hideaki IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), 283a11d206dSYOSHIFUJI Hideaki IPSTATS_MIB_INHDRERRORS); 284d56f90a7SArnaldo Carvalho de Melo icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, 285d56f90a7SArnaldo Carvalho de Melo ((u8 *)&fhdr->frag_off - 286d56f90a7SArnaldo Carvalho de Melo skb_network_header(skb))); 287f61944efSHerbert Xu return -1; 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds 290d56f90a7SArnaldo Carvalho de Melo if (skb->ip_summed == CHECKSUM_COMPLETE) { 291d56f90a7SArnaldo Carvalho de Melo const unsigned char *nh = skb_network_header(skb); 2921da177e4SLinus Torvalds skb->csum = csum_sub(skb->csum, 293d56f90a7SArnaldo Carvalho de Melo csum_partial(nh, (u8 *)(fhdr + 1) - nh, 294d56f90a7SArnaldo Carvalho de Melo 0)); 295d56f90a7SArnaldo Carvalho de Melo } 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds /* Is this the final fragment? */ 2981da177e4SLinus Torvalds if (!(fhdr->frag_off & htons(IP6_MF))) { 2991da177e4SLinus Torvalds /* If we already have some bits beyond end 3001da177e4SLinus Torvalds * or have different end, the segment is corrupted. 3011da177e4SLinus Torvalds */ 3025ab11c98SPavel Emelyanov if (end < fq->q.len || 3035ab11c98SPavel Emelyanov ((fq->q.last_in & LAST_IN) && end != fq->q.len)) 3041da177e4SLinus Torvalds goto err; 3055ab11c98SPavel Emelyanov fq->q.last_in |= LAST_IN; 3065ab11c98SPavel Emelyanov fq->q.len = end; 3071da177e4SLinus Torvalds } else { 3081da177e4SLinus Torvalds /* Check if the fragment is rounded to 8 bytes. 3091da177e4SLinus Torvalds * Required by the RFC. 3101da177e4SLinus Torvalds */ 3111da177e4SLinus Torvalds if (end & 0x7) { 3121da177e4SLinus Torvalds /* RFC2460 says always send parameter problem in 3131da177e4SLinus Torvalds * this case. -DaveM 3141da177e4SLinus Torvalds */ 315a11d206dSYOSHIFUJI Hideaki IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), 316a11d206dSYOSHIFUJI Hideaki IPSTATS_MIB_INHDRERRORS); 3171da177e4SLinus Torvalds icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, 3181da177e4SLinus Torvalds offsetof(struct ipv6hdr, payload_len)); 319f61944efSHerbert Xu return -1; 3201da177e4SLinus Torvalds } 3215ab11c98SPavel Emelyanov if (end > fq->q.len) { 3221da177e4SLinus Torvalds /* Some bits beyond end -> corruption. */ 3235ab11c98SPavel Emelyanov if (fq->q.last_in & LAST_IN) 3241da177e4SLinus Torvalds goto err; 3255ab11c98SPavel Emelyanov fq->q.len = end; 3261da177e4SLinus Torvalds } 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds if (end == offset) 3301da177e4SLinus Torvalds goto err; 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds /* Point into the IP datagram 'data' part. */ 3331da177e4SLinus Torvalds if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) 3341da177e4SLinus Torvalds goto err; 33542ca89c1SStephen Hemminger 33642ca89c1SStephen Hemminger if (pskb_trim_rcsum(skb, end - offset)) 3371da177e4SLinus Torvalds goto err; 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds /* Find out which fragments are in front and at the back of us 3401da177e4SLinus Torvalds * in the chain of fragments so far. We must know where to put 3411da177e4SLinus Torvalds * this fragment, right? 3421da177e4SLinus Torvalds */ 3431da177e4SLinus Torvalds prev = NULL; 3445ab11c98SPavel Emelyanov for(next = fq->q.fragments; next != NULL; next = next->next) { 3451da177e4SLinus Torvalds if (FRAG6_CB(next)->offset >= offset) 3461da177e4SLinus Torvalds break; /* bingo! */ 3471da177e4SLinus Torvalds prev = next; 3481da177e4SLinus Torvalds } 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds /* We found where to put this one. Check for overlap with 3511da177e4SLinus Torvalds * preceding fragment, and, if needed, align things so that 3521da177e4SLinus Torvalds * any overlaps are eliminated. 3531da177e4SLinus Torvalds */ 3541da177e4SLinus Torvalds if (prev) { 3551da177e4SLinus Torvalds int i = (FRAG6_CB(prev)->offset + prev->len) - offset; 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds if (i > 0) { 3581da177e4SLinus Torvalds offset += i; 3591da177e4SLinus Torvalds if (end <= offset) 3601da177e4SLinus Torvalds goto err; 3611da177e4SLinus Torvalds if (!pskb_pull(skb, i)) 3621da177e4SLinus Torvalds goto err; 3631da177e4SLinus Torvalds if (skb->ip_summed != CHECKSUM_UNNECESSARY) 3641da177e4SLinus Torvalds skb->ip_summed = CHECKSUM_NONE; 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds } 3671da177e4SLinus Torvalds 3681da177e4SLinus Torvalds /* Look for overlap with succeeding segments. 3691da177e4SLinus Torvalds * If we can merge fragments, do it. 3701da177e4SLinus Torvalds */ 3711da177e4SLinus Torvalds while (next && FRAG6_CB(next)->offset < end) { 3721da177e4SLinus Torvalds int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */ 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds if (i < next->len) { 3751da177e4SLinus Torvalds /* Eat head of the next overlapped fragment 3761da177e4SLinus Torvalds * and leave the loop. The next ones cannot overlap. 3771da177e4SLinus Torvalds */ 3781da177e4SLinus Torvalds if (!pskb_pull(next, i)) 3791da177e4SLinus Torvalds goto err; 3801da177e4SLinus Torvalds FRAG6_CB(next)->offset += i; /* next fragment */ 3815ab11c98SPavel Emelyanov fq->q.meat -= i; 3821da177e4SLinus Torvalds if (next->ip_summed != CHECKSUM_UNNECESSARY) 3831da177e4SLinus Torvalds next->ip_summed = CHECKSUM_NONE; 3841da177e4SLinus Torvalds break; 3851da177e4SLinus Torvalds } else { 3861da177e4SLinus Torvalds struct sk_buff *free_it = next; 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds /* Old fragment is completely overridden with 3891da177e4SLinus Torvalds * new one drop it. 3901da177e4SLinus Torvalds */ 3911da177e4SLinus Torvalds next = next->next; 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvalds if (prev) 3941da177e4SLinus Torvalds prev->next = next; 3951da177e4SLinus Torvalds else 3965ab11c98SPavel Emelyanov fq->q.fragments = next; 3971da177e4SLinus Torvalds 3985ab11c98SPavel Emelyanov fq->q.meat -= free_it->len; 3991da177e4SLinus Torvalds frag_kfree_skb(free_it, NULL); 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds FRAG6_CB(skb)->offset = offset; 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds /* Insert this fragment in the chain of fragments. */ 4061da177e4SLinus Torvalds skb->next = next; 4071da177e4SLinus Torvalds if (prev) 4081da177e4SLinus Torvalds prev->next = skb; 4091da177e4SLinus Torvalds else 4105ab11c98SPavel Emelyanov fq->q.fragments = skb; 4111da177e4SLinus Torvalds 412f61944efSHerbert Xu dev = skb->dev; 413f61944efSHerbert Xu if (dev) { 414f61944efSHerbert Xu fq->iif = dev->ifindex; 4151da177e4SLinus Torvalds skb->dev = NULL; 416f61944efSHerbert Xu } 4175ab11c98SPavel Emelyanov fq->q.stamp = skb->tstamp; 4185ab11c98SPavel Emelyanov fq->q.meat += skb->len; 4197eb95156SPavel Emelyanov atomic_add(skb->truesize, &ip6_frags.mem); 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds /* The first fragment. 4221da177e4SLinus Torvalds * nhoffset is obtained from the first fragment, of course. 4231da177e4SLinus Torvalds */ 4241da177e4SLinus Torvalds if (offset == 0) { 4251da177e4SLinus Torvalds fq->nhoffset = nhoff; 4265ab11c98SPavel Emelyanov fq->q.last_in |= FIRST_IN; 4271da177e4SLinus Torvalds } 428f61944efSHerbert Xu 4295ab11c98SPavel Emelyanov if (fq->q.last_in == (FIRST_IN | LAST_IN) && fq->q.meat == fq->q.len) 430f61944efSHerbert Xu return ip6_frag_reasm(fq, prev, dev); 431f61944efSHerbert Xu 4327eb95156SPavel Emelyanov write_lock(&ip6_frags.lock); 4337eb95156SPavel Emelyanov list_move_tail(&fq->q.lru_list, &ip6_frags.lru_list); 4347eb95156SPavel Emelyanov write_unlock(&ip6_frags.lock); 435f61944efSHerbert Xu return -1; 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds err: 438a11d206dSYOSHIFUJI Hideaki IP6_INC_STATS(ip6_dst_idev(skb->dst), 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 { 4555ab11c98SPavel Emelyanov struct sk_buff *fp, *head = fq->q.fragments; 4561da177e4SLinus Torvalds int payload_len; 4571da177e4SLinus Torvalds unsigned int nhoff; 4581da177e4SLinus Torvalds 4591da177e4SLinus Torvalds fq_kill(fq); 4601da177e4SLinus Torvalds 461f61944efSHerbert Xu /* Make the one we just received the head. */ 462f61944efSHerbert Xu if (prev) { 463f61944efSHerbert Xu head = prev->next; 464f61944efSHerbert Xu fp = skb_clone(head, GFP_ATOMIC); 465f61944efSHerbert Xu 466f61944efSHerbert Xu if (!fp) 467f61944efSHerbert Xu goto out_oom; 468f61944efSHerbert Xu 469f61944efSHerbert Xu fp->next = head->next; 470f61944efSHerbert Xu prev->next = fp; 471f61944efSHerbert Xu 4725ab11c98SPavel Emelyanov skb_morph(head, fq->q.fragments); 4735ab11c98SPavel Emelyanov head->next = fq->q.fragments->next; 474f61944efSHerbert Xu 4755ab11c98SPavel Emelyanov kfree_skb(fq->q.fragments); 4765ab11c98SPavel Emelyanov fq->q.fragments = head; 477f61944efSHerbert Xu } 478f61944efSHerbert Xu 4791da177e4SLinus Torvalds BUG_TRAP(head != NULL); 4801da177e4SLinus Torvalds BUG_TRAP(FRAG6_CB(head)->offset == 0); 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds /* Unfragmented part is taken from the first segment. */ 483d56f90a7SArnaldo Carvalho de Melo payload_len = ((head->data - skb_network_header(head)) - 4845ab11c98SPavel Emelyanov sizeof(struct ipv6hdr) + fq->q.len - 485d56f90a7SArnaldo Carvalho de Melo sizeof(struct frag_hdr)); 4861da177e4SLinus Torvalds if (payload_len > IPV6_MAXPLEN) 4871da177e4SLinus Torvalds goto out_oversize; 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds /* Head of list must not be cloned. */ 4901da177e4SLinus Torvalds if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC)) 4911da177e4SLinus Torvalds goto out_oom; 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds /* If the first fragment is fragmented itself, we split 4941da177e4SLinus Torvalds * it to two chunks: the first with data and paged part 4951da177e4SLinus Torvalds * and the second, holding only fragments. */ 4961da177e4SLinus Torvalds if (skb_shinfo(head)->frag_list) { 4971da177e4SLinus Torvalds struct sk_buff *clone; 4981da177e4SLinus Torvalds int i, plen = 0; 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL) 5011da177e4SLinus Torvalds goto out_oom; 5021da177e4SLinus Torvalds clone->next = head->next; 5031da177e4SLinus Torvalds head->next = clone; 5041da177e4SLinus Torvalds skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list; 5051da177e4SLinus Torvalds skb_shinfo(head)->frag_list = NULL; 5061da177e4SLinus Torvalds for (i=0; i<skb_shinfo(head)->nr_frags; i++) 5071da177e4SLinus Torvalds plen += skb_shinfo(head)->frags[i].size; 5081da177e4SLinus Torvalds clone->len = clone->data_len = head->data_len - plen; 5091da177e4SLinus Torvalds head->data_len -= clone->len; 5101da177e4SLinus Torvalds head->len -= clone->len; 5111da177e4SLinus Torvalds clone->csum = 0; 5121da177e4SLinus Torvalds clone->ip_summed = head->ip_summed; 5137eb95156SPavel Emelyanov atomic_add(clone->truesize, &ip6_frags.mem); 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds /* We have to remove fragment header from datagram and to relocate 5171da177e4SLinus Torvalds * header in order to calculate ICV correctly. */ 5181da177e4SLinus Torvalds nhoff = fq->nhoffset; 519b0e380b1SArnaldo Carvalho de Melo skb_network_header(head)[nhoff] = skb_transport_header(head)[0]; 5201da177e4SLinus Torvalds memmove(head->head + sizeof(struct frag_hdr), head->head, 5211da177e4SLinus Torvalds (head->data - head->head) - sizeof(struct frag_hdr)); 522b0e380b1SArnaldo Carvalho de Melo head->mac_header += sizeof(struct frag_hdr); 523b0e380b1SArnaldo Carvalho de Melo head->network_header += sizeof(struct frag_hdr); 5241da177e4SLinus Torvalds 5251da177e4SLinus Torvalds skb_shinfo(head)->frag_list = head->next; 526badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(head); 527d56f90a7SArnaldo Carvalho de Melo skb_push(head, head->data - skb_network_header(head)); 5287eb95156SPavel Emelyanov atomic_sub(head->truesize, &ip6_frags.mem); 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds for (fp=head->next; fp; fp = fp->next) { 5311da177e4SLinus Torvalds head->data_len += fp->len; 5321da177e4SLinus Torvalds head->len += fp->len; 5331da177e4SLinus Torvalds if (head->ip_summed != fp->ip_summed) 5341da177e4SLinus Torvalds head->ip_summed = CHECKSUM_NONE; 53584fa7933SPatrick McHardy else if (head->ip_summed == CHECKSUM_COMPLETE) 5361da177e4SLinus Torvalds head->csum = csum_add(head->csum, fp->csum); 5371da177e4SLinus Torvalds head->truesize += fp->truesize; 5387eb95156SPavel Emelyanov atomic_sub(fp->truesize, &ip6_frags.mem); 5391da177e4SLinus Torvalds } 5401da177e4SLinus Torvalds 5411da177e4SLinus Torvalds head->next = NULL; 5421da177e4SLinus Torvalds head->dev = dev; 5435ab11c98SPavel Emelyanov head->tstamp = fq->q.stamp; 5440660e03fSArnaldo Carvalho de Melo ipv6_hdr(head)->payload_len = htons(payload_len); 545951dbc8aSPatrick McHardy IP6CB(head)->nhoff = nhoff; 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds /* Yes, and fold redundant checksum back. 8) */ 54884fa7933SPatrick McHardy if (head->ip_summed == CHECKSUM_COMPLETE) 549d56f90a7SArnaldo Carvalho de Melo head->csum = csum_partial(skb_network_header(head), 550cfe1fc77SArnaldo Carvalho de Melo skb_network_header_len(head), 551d56f90a7SArnaldo Carvalho de Melo head->csum); 5521da177e4SLinus Torvalds 553a11d206dSYOSHIFUJI Hideaki rcu_read_lock(); 554a11d206dSYOSHIFUJI Hideaki IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMOKS); 555a11d206dSYOSHIFUJI Hideaki rcu_read_unlock(); 5565ab11c98SPavel Emelyanov fq->q.fragments = NULL; 5571da177e4SLinus Torvalds return 1; 5581da177e4SLinus Torvalds 5591da177e4SLinus Torvalds out_oversize: 5601da177e4SLinus Torvalds if (net_ratelimit()) 5611da177e4SLinus Torvalds printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len); 5621da177e4SLinus Torvalds goto out_fail; 5631da177e4SLinus Torvalds out_oom: 5641da177e4SLinus Torvalds if (net_ratelimit()) 5651da177e4SLinus Torvalds printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n"); 5661da177e4SLinus Torvalds out_fail: 567a11d206dSYOSHIFUJI Hideaki rcu_read_lock(); 568a11d206dSYOSHIFUJI Hideaki IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS); 569a11d206dSYOSHIFUJI Hideaki rcu_read_unlock(); 5701da177e4SLinus Torvalds return -1; 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds 573e5bbef20SHerbert Xu static int ipv6_frag_rcv(struct sk_buff *skb) 5741da177e4SLinus Torvalds { 5751da177e4SLinus Torvalds struct frag_hdr *fhdr; 5761da177e4SLinus Torvalds struct frag_queue *fq; 5770660e03fSArnaldo Carvalho de Melo struct ipv6hdr *hdr = ipv6_hdr(skb); 5781da177e4SLinus Torvalds 579a11d206dSYOSHIFUJI Hideaki IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMREQDS); 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds /* Jumbo payload inhibits frag. header */ 5821da177e4SLinus Torvalds if (hdr->payload_len==0) { 583a11d206dSYOSHIFUJI Hideaki IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS); 584cfe1fc77SArnaldo Carvalho de Melo icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, 585cfe1fc77SArnaldo Carvalho de Melo skb_network_header_len(skb)); 5861da177e4SLinus Torvalds return -1; 5871da177e4SLinus Torvalds } 588ea2ae17dSArnaldo Carvalho de Melo if (!pskb_may_pull(skb, (skb_transport_offset(skb) + 589ea2ae17dSArnaldo Carvalho de Melo sizeof(struct frag_hdr)))) { 590a11d206dSYOSHIFUJI Hideaki IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS); 591cfe1fc77SArnaldo Carvalho de Melo icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, 592cfe1fc77SArnaldo Carvalho de Melo skb_network_header_len(skb)); 5931da177e4SLinus Torvalds return -1; 5941da177e4SLinus Torvalds } 5951da177e4SLinus Torvalds 5960660e03fSArnaldo Carvalho de Melo hdr = ipv6_hdr(skb); 5979c70220bSArnaldo Carvalho de Melo fhdr = (struct frag_hdr *)skb_transport_header(skb); 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds if (!(fhdr->frag_off & htons(0xFFF9))) { 6001da177e4SLinus Torvalds /* It is not a fragmented frame */ 601b0e380b1SArnaldo Carvalho de Melo skb->transport_header += sizeof(struct frag_hdr); 602a11d206dSYOSHIFUJI Hideaki IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMOKS); 6031da177e4SLinus Torvalds 604d56f90a7SArnaldo Carvalho de Melo IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb); 6051da177e4SLinus Torvalds return 1; 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds 60804128f23SPavel Emelyanov if (atomic_read(&ip6_frags.mem) > ip6_frags_ctl.high_thresh) 609a11d206dSYOSHIFUJI Hideaki ip6_evictor(ip6_dst_idev(skb->dst)); 6101da177e4SLinus Torvalds 611a11d206dSYOSHIFUJI Hideaki if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr, 612a11d206dSYOSHIFUJI Hideaki ip6_dst_idev(skb->dst))) != NULL) { 613f61944efSHerbert Xu int ret; 6141da177e4SLinus Torvalds 6155ab11c98SPavel Emelyanov spin_lock(&fq->q.lock); 6161da177e4SLinus Torvalds 617f61944efSHerbert Xu ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff); 6181da177e4SLinus Torvalds 6195ab11c98SPavel Emelyanov spin_unlock(&fq->q.lock); 6204b6cb5d8SPavel Emelyanov fq_put(fq); 6211da177e4SLinus Torvalds return ret; 6221da177e4SLinus Torvalds } 6231da177e4SLinus Torvalds 624a11d206dSYOSHIFUJI Hideaki IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS); 6251da177e4SLinus Torvalds kfree_skb(skb); 6261da177e4SLinus Torvalds return -1; 6271da177e4SLinus Torvalds } 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds static struct inet6_protocol frag_protocol = 6301da177e4SLinus Torvalds { 6311da177e4SLinus Torvalds .handler = ipv6_frag_rcv, 6321da177e4SLinus Torvalds .flags = INET6_PROTO_NOPOLICY, 6331da177e4SLinus Torvalds }; 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds void __init ipv6_frag_init(void) 6361da177e4SLinus Torvalds { 6371da177e4SLinus Torvalds if (inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT) < 0) 6381da177e4SLinus Torvalds printk(KERN_ERR "ipv6_frag_init: Could not register protocol\n"); 6391da177e4SLinus Torvalds 64004128f23SPavel Emelyanov ip6_frags.ctl = &ip6_frags_ctl; 641321a3a99SPavel Emelyanov ip6_frags.hashfn = ip6_hashfn; 642c6fda282SPavel Emelyanov ip6_frags.constructor = ip6_frag_init; 643*c9547709SPavel Emelyanov ip6_frags.destructor = NULL; 6441e4b8287SPavel Emelyanov ip6_frags.skb_free = NULL; 6451e4b8287SPavel Emelyanov ip6_frags.qsize = sizeof(struct frag_queue); 646abd6523dSPavel Emelyanov ip6_frags.match = ip6_frag_match; 647e521db9dSPavel Emelyanov ip6_frags.frag_expire = ip6_frag_expire; 6487eb95156SPavel Emelyanov inet_frags_init(&ip6_frags); 6491da177e4SLinus Torvalds } 650