Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * IPv6 fragment reassembly |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3 | * Linux INET6 implementation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * Authors: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 6 | * Pedro Roque <roque@di.fc.ul.pt> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 8 | * $Id: reassembly.c,v 1.26 2001/03/07 22:00:57 davem Exp $ |
| 9 | * |
| 10 | * Based on: net/ipv4/ip_fragment.c |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version |
| 15 | * 2 of the License, or (at your option) any later version. |
| 16 | */ |
| 17 | |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 18 | /* |
| 19 | * Fixes: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | * Andi Kleen Make it work with multiple hosts. |
| 21 | * More RFC compliance. |
| 22 | * |
| 23 | * Horst von Brand Add missing #include <linux/string.h> |
| 24 | * Alexey Kuznetsov SMP races, threading, cleanup. |
| 25 | * Patrick McHardy LRU queue of frag heads for evictor. |
| 26 | * Mitsuru KANDA @USAGI Register inet6_protocol{}. |
| 27 | * David Stevens and |
| 28 | * YOSHIFUJI,H. @USAGI Always remove fragment header to |
| 29 | * calculate ICV correctly. |
| 30 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/errno.h> |
| 32 | #include <linux/types.h> |
| 33 | #include <linux/string.h> |
| 34 | #include <linux/socket.h> |
| 35 | #include <linux/sockios.h> |
| 36 | #include <linux/jiffies.h> |
| 37 | #include <linux/net.h> |
| 38 | #include <linux/list.h> |
| 39 | #include <linux/netdevice.h> |
| 40 | #include <linux/in6.h> |
| 41 | #include <linux/ipv6.h> |
| 42 | #include <linux/icmpv6.h> |
| 43 | #include <linux/random.h> |
| 44 | #include <linux/jhash.h> |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 45 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | #include <net/sock.h> |
| 48 | #include <net/snmp.h> |
| 49 | |
| 50 | #include <net/ipv6.h> |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 51 | #include <net/ip6_route.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <net/protocol.h> |
| 53 | #include <net/transp_v6.h> |
| 54 | #include <net/rawv6.h> |
| 55 | #include <net/ndisc.h> |
| 56 | #include <net/addrconf.h> |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 57 | #include <net/inet_frag.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | struct ip6frag_skb_cb |
| 60 | { |
| 61 | struct inet6_skb_parm h; |
| 62 | int offset; |
| 63 | }; |
| 64 | |
| 65 | #define FRAG6_CB(skb) ((struct ip6frag_skb_cb*)((skb)->cb)) |
| 66 | |
| 67 | |
| 68 | /* |
| 69 | * Equivalent of ipv4 struct ipq |
| 70 | */ |
| 71 | |
| 72 | struct frag_queue |
| 73 | { |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 74 | struct inet_frag_queue q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 76 | __be32 id; /* fragment id */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | struct in6_addr saddr; |
| 78 | struct in6_addr daddr; |
| 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | int iif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | unsigned int csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | __u16 nhoffset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 85 | static struct inet_frags ip6_frags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 87 | int ip6_frag_nqueues(void) |
| 88 | { |
| 89 | return ip6_frags.nqueues; |
| 90 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 92 | int ip6_frag_mem(void) |
| 93 | { |
| 94 | return atomic_read(&ip6_frags.mem); |
| 95 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 97 | static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev, |
| 98 | struct net_device *dev); |
| 99 | |
Zach Brown | f6596f9 | 2006-04-10 16:05:34 -0700 | [diff] [blame] | 100 | /* |
| 101 | * callers should be careful not to use the hash value outside the ipfrag_lock |
| 102 | * as doing so could race with ipfrag_hash_rnd being recalculated. |
| 103 | */ |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 104 | static unsigned int ip6qhashfn(__be32 id, struct in6_addr *saddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | struct in6_addr *daddr) |
| 106 | { |
| 107 | u32 a, b, c; |
| 108 | |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 109 | a = (__force u32)saddr->s6_addr32[0]; |
| 110 | b = (__force u32)saddr->s6_addr32[1]; |
| 111 | c = (__force u32)saddr->s6_addr32[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | a += JHASH_GOLDEN_RATIO; |
| 114 | b += JHASH_GOLDEN_RATIO; |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 115 | c += ip6_frags.rnd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | __jhash_mix(a, b, c); |
| 117 | |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 118 | a += (__force u32)saddr->s6_addr32[3]; |
| 119 | b += (__force u32)daddr->s6_addr32[0]; |
| 120 | c += (__force u32)daddr->s6_addr32[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | __jhash_mix(a, b, c); |
| 122 | |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 123 | a += (__force u32)daddr->s6_addr32[2]; |
| 124 | b += (__force u32)daddr->s6_addr32[3]; |
| 125 | c += (__force u32)id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | __jhash_mix(a, b, c); |
| 127 | |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 128 | return c & (INETFRAGS_HASHSZ - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Pavel Emelyanov | 321a3a9 | 2007-10-15 02:38:08 -0700 | [diff] [blame] | 131 | static unsigned int ip6_hashfn(struct inet_frag_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | { |
Pavel Emelyanov | 321a3a9 | 2007-10-15 02:38:08 -0700 | [diff] [blame] | 133 | struct frag_queue *fq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Pavel Emelyanov | 321a3a9 | 2007-10-15 02:38:08 -0700 | [diff] [blame] | 135 | fq = container_of(q, struct frag_queue, q); |
| 136 | return ip6qhashfn(fq->id, &fq->saddr, &fq->daddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 139 | int ip6_frag_match(struct inet_frag_queue *q, void *a) |
| 140 | { |
| 141 | struct frag_queue *fq; |
| 142 | struct ip6_create_arg *arg = a; |
| 143 | |
| 144 | fq = container_of(q, struct frag_queue, q); |
| 145 | return (fq->id == arg->id && |
| 146 | ipv6_addr_equal(&fq->saddr, arg->src) && |
| 147 | ipv6_addr_equal(&fq->daddr, arg->dst)); |
| 148 | } |
| 149 | EXPORT_SYMBOL(ip6_frag_match); |
| 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | /* Memory Tracking Functions. */ |
| 152 | static inline void frag_kfree_skb(struct sk_buff *skb, int *work) |
| 153 | { |
| 154 | if (work) |
| 155 | *work -= skb->truesize; |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 156 | atomic_sub(skb->truesize, &ip6_frags.mem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | kfree_skb(skb); |
| 158 | } |
| 159 | |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 160 | void ip6_frag_init(struct inet_frag_queue *q, void *a) |
| 161 | { |
| 162 | struct frag_queue *fq = container_of(q, struct frag_queue, q); |
| 163 | struct ip6_create_arg *arg = a; |
| 164 | |
| 165 | fq->id = arg->id; |
| 166 | ipv6_addr_copy(&fq->saddr, arg->src); |
| 167 | ipv6_addr_copy(&fq->daddr, arg->dst); |
| 168 | } |
| 169 | EXPORT_SYMBOL(ip6_frag_init); |
| 170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | /* Destruction primitives. */ |
| 172 | |
Pavel Emelyanov | 4b6cb5d | 2007-10-15 02:41:09 -0700 | [diff] [blame] | 173 | static __inline__ void fq_put(struct frag_queue *fq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
Pavel Emelyanov | 762cc40 | 2007-10-15 02:41:56 -0700 | [diff] [blame] | 175 | inet_frag_put(&fq->q, &ip6_frags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /* Kill fq entry. It is not destroyed immediately, |
| 179 | * because caller (and someone more) holds reference count. |
| 180 | */ |
| 181 | static __inline__ void fq_kill(struct frag_queue *fq) |
| 182 | { |
Pavel Emelyanov | 277e650 | 2007-10-15 02:37:18 -0700 | [diff] [blame] | 183 | inet_frag_kill(&fq->q, &ip6_frags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
| 185 | |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 186 | static void ip6_evictor(struct inet6_dev *idev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | { |
Pavel Emelyanov | 8e7999c | 2007-10-15 02:40:06 -0700 | [diff] [blame] | 188 | int evicted; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
Pavel Emelyanov | 8e7999c | 2007-10-15 02:40:06 -0700 | [diff] [blame] | 190 | evicted = inet_frag_evictor(&ip6_frags); |
| 191 | if (evicted) |
| 192 | IP6_ADD_STATS_BH(idev, IPSTATS_MIB_REASMFAILS, evicted); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static void ip6_frag_expire(unsigned long data) |
| 196 | { |
Pavel Emelyanov | e521db9 | 2007-10-17 19:45:23 -0700 | [diff] [blame] | 197 | struct frag_queue *fq; |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 198 | struct net_device *dev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
Pavel Emelyanov | e521db9 | 2007-10-17 19:45:23 -0700 | [diff] [blame] | 200 | fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q); |
| 201 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 202 | spin_lock(&fq->q.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 204 | if (fq->q.last_in & COMPLETE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | goto out; |
| 206 | |
| 207 | fq_kill(fq); |
| 208 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 209 | dev = dev_get_by_index(&init_net, fq->iif); |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 210 | if (!dev) |
| 211 | goto out; |
| 212 | |
| 213 | rcu_read_lock(); |
| 214 | IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT); |
| 215 | IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS); |
| 216 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
Ingo Oeser | 78c784c | 2006-03-20 23:01:17 -0800 | [diff] [blame] | 218 | /* Don't send error if the first segment did not arrive. */ |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 219 | if (!(fq->q.last_in&FIRST_IN) || !fq->q.fragments) |
Ingo Oeser | 78c784c | 2006-03-20 23:01:17 -0800 | [diff] [blame] | 220 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
Ingo Oeser | 78c784c | 2006-03-20 23:01:17 -0800 | [diff] [blame] | 222 | /* |
| 223 | But use as source device on which LAST ARRIVED |
| 224 | segment was received. And do not use fq->dev |
| 225 | pointer directly, device might already disappeared. |
| 226 | */ |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 227 | fq->q.fragments->dev = dev; |
| 228 | icmpv6_send(fq->q.fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | out: |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 230 | if (dev) |
| 231 | dev_put(dev); |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 232 | spin_unlock(&fq->q.lock); |
Pavel Emelyanov | 4b6cb5d | 2007-10-15 02:41:09 -0700 | [diff] [blame] | 233 | fq_put(fq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 236 | static __inline__ struct frag_queue * |
| 237 | fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst, |
| 238 | struct inet6_dev *idev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | { |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 240 | struct inet_frag_queue *q; |
| 241 | struct ip6_create_arg arg; |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 242 | unsigned int hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 244 | arg.id = id; |
| 245 | arg.src = src; |
| 246 | arg.dst = dst; |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 247 | hash = ip6qhashfn(id, src, dst); |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 248 | |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 249 | q = inet_frag_find(&ip6_frags, &arg, hash); |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 250 | if (q == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | goto oom; |
| 252 | |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 253 | return container_of(q, struct frag_queue, q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
| 255 | oom: |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 256 | IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | return NULL; |
| 258 | } |
| 259 | |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 260 | static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | struct frag_hdr *fhdr, int nhoff) |
| 262 | { |
| 263 | struct sk_buff *prev, *next; |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 264 | struct net_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | int offset, end; |
| 266 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 267 | if (fq->q.last_in & COMPLETE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | goto err; |
| 269 | |
| 270 | offset = ntohs(fhdr->frag_off) & ~0x7; |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 271 | end = offset + (ntohs(ipv6_hdr(skb)->payload_len) - |
| 272 | ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
| 274 | if ((unsigned int)end > IPV6_MAXPLEN) { |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 275 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), |
| 276 | IPSTATS_MIB_INHDRERRORS); |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 277 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
| 278 | ((u8 *)&fhdr->frag_off - |
| 279 | skb_network_header(skb))); |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 280 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 283 | if (skb->ip_summed == CHECKSUM_COMPLETE) { |
| 284 | const unsigned char *nh = skb_network_header(skb); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 285 | skb->csum = csum_sub(skb->csum, |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 286 | csum_partial(nh, (u8 *)(fhdr + 1) - nh, |
| 287 | 0)); |
| 288 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
| 290 | /* Is this the final fragment? */ |
| 291 | if (!(fhdr->frag_off & htons(IP6_MF))) { |
| 292 | /* If we already have some bits beyond end |
| 293 | * or have different end, the segment is corrupted. |
| 294 | */ |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 295 | if (end < fq->q.len || |
| 296 | ((fq->q.last_in & LAST_IN) && end != fq->q.len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | goto err; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 298 | fq->q.last_in |= LAST_IN; |
| 299 | fq->q.len = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | } else { |
| 301 | /* Check if the fragment is rounded to 8 bytes. |
| 302 | * Required by the RFC. |
| 303 | */ |
| 304 | if (end & 0x7) { |
| 305 | /* RFC2460 says always send parameter problem in |
| 306 | * this case. -DaveM |
| 307 | */ |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 308 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), |
| 309 | IPSTATS_MIB_INHDRERRORS); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 310 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | offsetof(struct ipv6hdr, payload_len)); |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 312 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | } |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 314 | if (end > fq->q.len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | /* Some bits beyond end -> corruption. */ |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 316 | if (fq->q.last_in & LAST_IN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | goto err; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 318 | fq->q.len = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
| 322 | if (end == offset) |
| 323 | goto err; |
| 324 | |
| 325 | /* Point into the IP datagram 'data' part. */ |
| 326 | if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) |
| 327 | goto err; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 328 | |
Stephen Hemminger | 42ca89c | 2005-09-08 12:57:43 -0700 | [diff] [blame] | 329 | if (pskb_trim_rcsum(skb, end - offset)) |
| 330 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
| 332 | /* Find out which fragments are in front and at the back of us |
| 333 | * in the chain of fragments so far. We must know where to put |
| 334 | * this fragment, right? |
| 335 | */ |
| 336 | prev = NULL; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 337 | for(next = fq->q.fragments; next != NULL; next = next->next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | if (FRAG6_CB(next)->offset >= offset) |
| 339 | break; /* bingo! */ |
| 340 | prev = next; |
| 341 | } |
| 342 | |
| 343 | /* We found where to put this one. Check for overlap with |
| 344 | * preceding fragment, and, if needed, align things so that |
| 345 | * any overlaps are eliminated. |
| 346 | */ |
| 347 | if (prev) { |
| 348 | int i = (FRAG6_CB(prev)->offset + prev->len) - offset; |
| 349 | |
| 350 | if (i > 0) { |
| 351 | offset += i; |
| 352 | if (end <= offset) |
| 353 | goto err; |
| 354 | if (!pskb_pull(skb, i)) |
| 355 | goto err; |
| 356 | if (skb->ip_summed != CHECKSUM_UNNECESSARY) |
| 357 | skb->ip_summed = CHECKSUM_NONE; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | /* Look for overlap with succeeding segments. |
| 362 | * If we can merge fragments, do it. |
| 363 | */ |
| 364 | while (next && FRAG6_CB(next)->offset < end) { |
| 365 | int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */ |
| 366 | |
| 367 | if (i < next->len) { |
| 368 | /* Eat head of the next overlapped fragment |
| 369 | * and leave the loop. The next ones cannot overlap. |
| 370 | */ |
| 371 | if (!pskb_pull(next, i)) |
| 372 | goto err; |
| 373 | FRAG6_CB(next)->offset += i; /* next fragment */ |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 374 | fq->q.meat -= i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | if (next->ip_summed != CHECKSUM_UNNECESSARY) |
| 376 | next->ip_summed = CHECKSUM_NONE; |
| 377 | break; |
| 378 | } else { |
| 379 | struct sk_buff *free_it = next; |
| 380 | |
| 381 | /* Old fragment is completely overridden with |
| 382 | * new one drop it. |
| 383 | */ |
| 384 | next = next->next; |
| 385 | |
| 386 | if (prev) |
| 387 | prev->next = next; |
| 388 | else |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 389 | fq->q.fragments = next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 391 | fq->q.meat -= free_it->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | frag_kfree_skb(free_it, NULL); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | FRAG6_CB(skb)->offset = offset; |
| 397 | |
| 398 | /* Insert this fragment in the chain of fragments. */ |
| 399 | skb->next = next; |
| 400 | if (prev) |
| 401 | prev->next = skb; |
| 402 | else |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 403 | fq->q.fragments = skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 405 | dev = skb->dev; |
| 406 | if (dev) { |
| 407 | fq->iif = dev->ifindex; |
| 408 | skb->dev = NULL; |
| 409 | } |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 410 | fq->q.stamp = skb->tstamp; |
| 411 | fq->q.meat += skb->len; |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 412 | atomic_add(skb->truesize, &ip6_frags.mem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
| 414 | /* The first fragment. |
| 415 | * nhoffset is obtained from the first fragment, of course. |
| 416 | */ |
| 417 | if (offset == 0) { |
| 418 | fq->nhoffset = nhoff; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 419 | fq->q.last_in |= FIRST_IN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | } |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 421 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 422 | if (fq->q.last_in == (FIRST_IN | LAST_IN) && fq->q.meat == fq->q.len) |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 423 | return ip6_frag_reasm(fq, prev, dev); |
| 424 | |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 425 | write_lock(&ip6_frags.lock); |
| 426 | list_move_tail(&fq->q.lru_list, &ip6_frags.lru_list); |
| 427 | write_unlock(&ip6_frags.lock); |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 428 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
| 430 | err: |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 431 | IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | kfree_skb(skb); |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 433 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /* |
| 437 | * Check if this packet is complete. |
| 438 | * Returns NULL on failure by any reason, and pointer |
| 439 | * to current nexthdr field in reassembled frame. |
| 440 | * |
| 441 | * It is called with locked fq, and caller must check that |
| 442 | * queue is eligible for reassembly i.e. it is not COMPLETE, |
| 443 | * the last and the first frames arrived and all the bits are here. |
| 444 | */ |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 445 | static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | struct net_device *dev) |
| 447 | { |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 448 | struct sk_buff *fp, *head = fq->q.fragments; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | int payload_len; |
| 450 | unsigned int nhoff; |
| 451 | |
| 452 | fq_kill(fq); |
| 453 | |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 454 | /* Make the one we just received the head. */ |
| 455 | if (prev) { |
| 456 | head = prev->next; |
| 457 | fp = skb_clone(head, GFP_ATOMIC); |
| 458 | |
| 459 | if (!fp) |
| 460 | goto out_oom; |
| 461 | |
| 462 | fp->next = head->next; |
| 463 | prev->next = fp; |
| 464 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 465 | skb_morph(head, fq->q.fragments); |
| 466 | head->next = fq->q.fragments->next; |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 467 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 468 | kfree_skb(fq->q.fragments); |
| 469 | fq->q.fragments = head; |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | BUG_TRAP(head != NULL); |
| 473 | BUG_TRAP(FRAG6_CB(head)->offset == 0); |
| 474 | |
| 475 | /* Unfragmented part is taken from the first segment. */ |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 476 | payload_len = ((head->data - skb_network_header(head)) - |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 477 | sizeof(struct ipv6hdr) + fq->q.len - |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 478 | sizeof(struct frag_hdr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | if (payload_len > IPV6_MAXPLEN) |
| 480 | goto out_oversize; |
| 481 | |
| 482 | /* Head of list must not be cloned. */ |
| 483 | if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC)) |
| 484 | goto out_oom; |
| 485 | |
| 486 | /* If the first fragment is fragmented itself, we split |
| 487 | * it to two chunks: the first with data and paged part |
| 488 | * and the second, holding only fragments. */ |
| 489 | if (skb_shinfo(head)->frag_list) { |
| 490 | struct sk_buff *clone; |
| 491 | int i, plen = 0; |
| 492 | |
| 493 | if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL) |
| 494 | goto out_oom; |
| 495 | clone->next = head->next; |
| 496 | head->next = clone; |
| 497 | skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list; |
| 498 | skb_shinfo(head)->frag_list = NULL; |
| 499 | for (i=0; i<skb_shinfo(head)->nr_frags; i++) |
| 500 | plen += skb_shinfo(head)->frags[i].size; |
| 501 | clone->len = clone->data_len = head->data_len - plen; |
| 502 | head->data_len -= clone->len; |
| 503 | head->len -= clone->len; |
| 504 | clone->csum = 0; |
| 505 | clone->ip_summed = head->ip_summed; |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 506 | atomic_add(clone->truesize, &ip6_frags.mem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | /* We have to remove fragment header from datagram and to relocate |
| 510 | * header in order to calculate ICV correctly. */ |
| 511 | nhoff = fq->nhoffset; |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 512 | skb_network_header(head)[nhoff] = skb_transport_header(head)[0]; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 513 | memmove(head->head + sizeof(struct frag_hdr), head->head, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | (head->data - head->head) - sizeof(struct frag_hdr)); |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 515 | head->mac_header += sizeof(struct frag_hdr); |
| 516 | head->network_header += sizeof(struct frag_hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | |
| 518 | skb_shinfo(head)->frag_list = head->next; |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 519 | skb_reset_transport_header(head); |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 520 | skb_push(head, head->data - skb_network_header(head)); |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 521 | atomic_sub(head->truesize, &ip6_frags.mem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
| 523 | for (fp=head->next; fp; fp = fp->next) { |
| 524 | head->data_len += fp->len; |
| 525 | head->len += fp->len; |
| 526 | if (head->ip_summed != fp->ip_summed) |
| 527 | head->ip_summed = CHECKSUM_NONE; |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 528 | else if (head->ip_summed == CHECKSUM_COMPLETE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | head->csum = csum_add(head->csum, fp->csum); |
| 530 | head->truesize += fp->truesize; |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 531 | atomic_sub(fp->truesize, &ip6_frags.mem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | head->next = NULL; |
| 535 | head->dev = dev; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 536 | head->tstamp = fq->q.stamp; |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 537 | ipv6_hdr(head)->payload_len = htons(payload_len); |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 538 | IP6CB(head)->nhoff = nhoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | /* Yes, and fold redundant checksum back. 8) */ |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 541 | if (head->ip_summed == CHECKSUM_COMPLETE) |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 542 | head->csum = csum_partial(skb_network_header(head), |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 543 | skb_network_header_len(head), |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 544 | head->csum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 546 | rcu_read_lock(); |
| 547 | IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMOKS); |
| 548 | rcu_read_unlock(); |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 549 | fq->q.fragments = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | return 1; |
| 551 | |
| 552 | out_oversize: |
| 553 | if (net_ratelimit()) |
| 554 | printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len); |
| 555 | goto out_fail; |
| 556 | out_oom: |
| 557 | if (net_ratelimit()) |
| 558 | printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n"); |
| 559 | out_fail: |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 560 | rcu_read_lock(); |
| 561 | IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS); |
| 562 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | return -1; |
| 564 | } |
| 565 | |
Herbert Xu | e5bbef2 | 2007-10-15 12:50:28 -0700 | [diff] [blame] | 566 | static int ipv6_frag_rcv(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | struct frag_hdr *fhdr; |
| 569 | struct frag_queue *fq; |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 570 | struct ipv6hdr *hdr = ipv6_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 572 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMREQDS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | |
| 574 | /* Jumbo payload inhibits frag. header */ |
| 575 | if (hdr->payload_len==0) { |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 576 | IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS); |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 577 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
| 578 | skb_network_header_len(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | return -1; |
| 580 | } |
Arnaldo Carvalho de Melo | ea2ae17 | 2007-04-25 17:55:53 -0700 | [diff] [blame] | 581 | if (!pskb_may_pull(skb, (skb_transport_offset(skb) + |
| 582 | sizeof(struct frag_hdr)))) { |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 583 | IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS); |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 584 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
| 585 | skb_network_header_len(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | return -1; |
| 587 | } |
| 588 | |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 589 | hdr = ipv6_hdr(skb); |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 590 | fhdr = (struct frag_hdr *)skb_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | |
| 592 | if (!(fhdr->frag_off & htons(0xFFF9))) { |
| 593 | /* It is not a fragmented frame */ |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 594 | skb->transport_header += sizeof(struct frag_hdr); |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 595 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMOKS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 597 | IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | return 1; |
| 599 | } |
| 600 | |
Daniel Lezcano | e71e034 | 2008-01-10 02:56:03 -0800 | [diff] [blame] | 601 | if (atomic_read(&ip6_frags.mem) > init_net.ipv6.sysctl.frags.high_thresh) |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 602 | ip6_evictor(ip6_dst_idev(skb->dst)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 604 | if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr, |
| 605 | ip6_dst_idev(skb->dst))) != NULL) { |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 606 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 608 | spin_lock(&fq->q.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 610 | ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame] | 612 | spin_unlock(&fq->q.lock); |
Pavel Emelyanov | 4b6cb5d | 2007-10-15 02:41:09 -0700 | [diff] [blame] | 613 | fq_put(fq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | return ret; |
| 615 | } |
| 616 | |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 617 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | kfree_skb(skb); |
| 619 | return -1; |
| 620 | } |
| 621 | |
| 622 | static struct inet6_protocol frag_protocol = |
| 623 | { |
| 624 | .handler = ipv6_frag_rcv, |
| 625 | .flags = INET6_PROTO_NOPOLICY, |
| 626 | }; |
| 627 | |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame^] | 628 | #ifdef CONFIG_SYSCTL |
| 629 | static struct ctl_table ip6_frags_ctl_table[] = { |
| 630 | { |
| 631 | .ctl_name = NET_IPV6_IP6FRAG_HIGH_THRESH, |
| 632 | .procname = "ip6frag_high_thresh", |
| 633 | .data = &init_net.ipv6.sysctl.frags.high_thresh, |
| 634 | .maxlen = sizeof(int), |
| 635 | .mode = 0644, |
| 636 | .proc_handler = &proc_dointvec |
| 637 | }, |
| 638 | { |
| 639 | .ctl_name = NET_IPV6_IP6FRAG_LOW_THRESH, |
| 640 | .procname = "ip6frag_low_thresh", |
| 641 | .data = &init_net.ipv6.sysctl.frags.low_thresh, |
| 642 | .maxlen = sizeof(int), |
| 643 | .mode = 0644, |
| 644 | .proc_handler = &proc_dointvec |
| 645 | }, |
| 646 | { |
| 647 | .ctl_name = NET_IPV6_IP6FRAG_TIME, |
| 648 | .procname = "ip6frag_time", |
| 649 | .data = &init_net.ipv6.sysctl.frags.timeout, |
| 650 | .maxlen = sizeof(int), |
| 651 | .mode = 0644, |
| 652 | .proc_handler = &proc_dointvec_jiffies, |
| 653 | .strategy = &sysctl_jiffies, |
| 654 | }, |
| 655 | { |
| 656 | .ctl_name = NET_IPV6_IP6FRAG_SECRET_INTERVAL, |
| 657 | .procname = "ip6frag_secret_interval", |
| 658 | .data = &init_net.ipv6.sysctl.frags.secret_interval, |
| 659 | .maxlen = sizeof(int), |
| 660 | .mode = 0644, |
| 661 | .proc_handler = &proc_dointvec_jiffies, |
| 662 | .strategy = &sysctl_jiffies |
| 663 | }, |
| 664 | { } |
| 665 | }; |
Daniel Lezcano | 7d460db | 2008-01-18 23:52:35 -0800 | [diff] [blame] | 666 | |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame^] | 667 | static int ip6_frags_sysctl_register(struct net *net) |
| 668 | { |
| 669 | struct ctl_table_header *hdr; |
| 670 | |
| 671 | hdr = register_net_sysctl_table(net, net_ipv6_ctl_path, |
| 672 | ip6_frags_ctl_table); |
| 673 | return hdr == NULL ? -ENOMEM : 0; |
| 674 | } |
| 675 | #else |
| 676 | static inline int ip6_frags_sysctl_register(struct net *net) |
| 677 | { |
| 678 | return 0; |
| 679 | } |
| 680 | #endif |
| 681 | |
| 682 | static int ipv6_frags_init_net(struct net *net) |
| 683 | { |
Daniel Lezcano | e71e034 | 2008-01-10 02:56:03 -0800 | [diff] [blame] | 684 | ip6_frags.ctl = &net->ipv6.sysctl.frags; |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame^] | 685 | |
| 686 | net->ipv6.sysctl.frags.high_thresh = 256 * 1024; |
| 687 | net->ipv6.sysctl.frags.low_thresh = 192 * 1024; |
| 688 | net->ipv6.sysctl.frags.timeout = IPV6_FRAG_TIMEOUT; |
| 689 | net->ipv6.sysctl.frags.secret_interval = 10 * 60 * HZ; |
| 690 | |
| 691 | return ip6_frags_sysctl_register(net); |
Daniel Lezcano | e71e034 | 2008-01-10 02:56:03 -0800 | [diff] [blame] | 692 | } |
| 693 | |
Daniel Lezcano | 853cbba | 2007-12-11 02:24:29 -0800 | [diff] [blame] | 694 | int __init ipv6_frag_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | { |
Daniel Lezcano | 853cbba | 2007-12-11 02:24:29 -0800 | [diff] [blame] | 696 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
Daniel Lezcano | 853cbba | 2007-12-11 02:24:29 -0800 | [diff] [blame] | 698 | ret = inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT); |
| 699 | if (ret) |
| 700 | goto out; |
Daniel Lezcano | e71e034 | 2008-01-10 02:56:03 -0800 | [diff] [blame] | 701 | |
Pavel Emelyanov | 8d8354d | 2008-01-22 05:58:31 -0800 | [diff] [blame^] | 702 | ipv6_frags_init_net(&init_net); |
| 703 | |
Pavel Emelyanov | 321a3a9 | 2007-10-15 02:38:08 -0700 | [diff] [blame] | 704 | ip6_frags.hashfn = ip6_hashfn; |
Pavel Emelyanov | c6fda28 | 2007-10-17 19:46:47 -0700 | [diff] [blame] | 705 | ip6_frags.constructor = ip6_frag_init; |
Pavel Emelyanov | c954770 | 2007-10-17 19:48:26 -0700 | [diff] [blame] | 706 | ip6_frags.destructor = NULL; |
Pavel Emelyanov | 1e4b828 | 2007-10-15 02:39:14 -0700 | [diff] [blame] | 707 | ip6_frags.skb_free = NULL; |
| 708 | ip6_frags.qsize = sizeof(struct frag_queue); |
Pavel Emelyanov | abd6523 | 2007-10-17 19:47:21 -0700 | [diff] [blame] | 709 | ip6_frags.match = ip6_frag_match; |
Pavel Emelyanov | e521db9 | 2007-10-17 19:45:23 -0700 | [diff] [blame] | 710 | ip6_frags.frag_expire = ip6_frag_expire; |
Pavel Emelyanov | 7eb9515 | 2007-10-15 02:31:52 -0700 | [diff] [blame] | 711 | inet_frags_init(&ip6_frags); |
Daniel Lezcano | 853cbba | 2007-12-11 02:24:29 -0800 | [diff] [blame] | 712 | out: |
| 713 | return ret; |
| 714 | } |
| 715 | |
| 716 | void ipv6_frag_exit(void) |
| 717 | { |
| 718 | inet_frags_fini(&ip6_frags); |
| 719 | inet6_del_protocol(&frag_protocol, IPPROTO_FRAGMENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | } |