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 | |
Brian Haley | ab32ea5 | 2006-09-22 14:15:41 -0700 | [diff] [blame] | 59 | int sysctl_ip6frag_high_thresh __read_mostly = 256*1024; |
| 60 | int sysctl_ip6frag_low_thresh __read_mostly = 192*1024; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Brian Haley | ab32ea5 | 2006-09-22 14:15:41 -0700 | [diff] [blame] | 62 | int sysctl_ip6frag_time __read_mostly = IPV6_FRAG_TIMEOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | struct ip6frag_skb_cb |
| 65 | { |
| 66 | struct inet6_skb_parm h; |
| 67 | int offset; |
| 68 | }; |
| 69 | |
| 70 | #define FRAG6_CB(skb) ((struct ip6frag_skb_cb*)((skb)->cb)) |
| 71 | |
| 72 | |
| 73 | /* |
| 74 | * Equivalent of ipv4 struct ipq |
| 75 | */ |
| 76 | |
| 77 | struct frag_queue |
| 78 | { |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 79 | struct inet_frag_queue q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 81 | __be32 id; /* fragment id */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | struct in6_addr saddr; |
| 83 | struct in6_addr daddr; |
| 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | int iif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | unsigned int csum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | __u16 nhoffset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | /* Hash table. */ |
| 91 | |
| 92 | #define IP6Q_HASHSZ 64 |
| 93 | |
Yasuyuki Kozakai | e7c8a41 | 2005-11-16 12:55:37 -0800 | [diff] [blame] | 94 | static struct hlist_head ip6_frag_hash[IP6Q_HASHSZ]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | static DEFINE_RWLOCK(ip6_frag_lock); |
| 96 | static u32 ip6_frag_hash_rnd; |
| 97 | static LIST_HEAD(ip6_frag_lru_list); |
| 98 | int ip6_frag_nqueues = 0; |
| 99 | |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 100 | static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev, |
| 101 | struct net_device *dev); |
| 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | static __inline__ void __fq_unlink(struct frag_queue *fq) |
| 104 | { |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 105 | hlist_del(&fq->q.list); |
| 106 | list_del(&fq->q.lru_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | ip6_frag_nqueues--; |
| 108 | } |
| 109 | |
| 110 | static __inline__ void fq_unlink(struct frag_queue *fq) |
| 111 | { |
| 112 | write_lock(&ip6_frag_lock); |
| 113 | __fq_unlink(fq); |
| 114 | write_unlock(&ip6_frag_lock); |
| 115 | } |
| 116 | |
Zach Brown | f6596f9 | 2006-04-10 16:05:34 -0700 | [diff] [blame] | 117 | /* |
| 118 | * callers should be careful not to use the hash value outside the ipfrag_lock |
| 119 | * as doing so could race with ipfrag_hash_rnd being recalculated. |
| 120 | */ |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 121 | static unsigned int ip6qhashfn(__be32 id, struct in6_addr *saddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | struct in6_addr *daddr) |
| 123 | { |
| 124 | u32 a, b, c; |
| 125 | |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 126 | a = (__force u32)saddr->s6_addr32[0]; |
| 127 | b = (__force u32)saddr->s6_addr32[1]; |
| 128 | c = (__force u32)saddr->s6_addr32[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
| 130 | a += JHASH_GOLDEN_RATIO; |
| 131 | b += JHASH_GOLDEN_RATIO; |
| 132 | c += ip6_frag_hash_rnd; |
| 133 | __jhash_mix(a, b, c); |
| 134 | |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 135 | a += (__force u32)saddr->s6_addr32[3]; |
| 136 | b += (__force u32)daddr->s6_addr32[0]; |
| 137 | c += (__force u32)daddr->s6_addr32[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | __jhash_mix(a, b, c); |
| 139 | |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 140 | a += (__force u32)daddr->s6_addr32[2]; |
| 141 | b += (__force u32)daddr->s6_addr32[3]; |
| 142 | c += (__force u32)id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | __jhash_mix(a, b, c); |
| 144 | |
| 145 | return c & (IP6Q_HASHSZ - 1); |
| 146 | } |
| 147 | |
| 148 | static struct timer_list ip6_frag_secret_timer; |
Brian Haley | ab32ea5 | 2006-09-22 14:15:41 -0700 | [diff] [blame] | 149 | int sysctl_ip6frag_secret_interval __read_mostly = 10 * 60 * HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
| 151 | static void ip6_frag_secret_rebuild(unsigned long dummy) |
| 152 | { |
| 153 | unsigned long now = jiffies; |
| 154 | int i; |
| 155 | |
| 156 | write_lock(&ip6_frag_lock); |
| 157 | get_random_bytes(&ip6_frag_hash_rnd, sizeof(u32)); |
| 158 | for (i = 0; i < IP6Q_HASHSZ; i++) { |
| 159 | struct frag_queue *q; |
Yasuyuki Kozakai | e7c8a41 | 2005-11-16 12:55:37 -0800 | [diff] [blame] | 160 | struct hlist_node *p, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 162 | hlist_for_each_entry_safe(q, p, n, &ip6_frag_hash[i], q.list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | unsigned int hval = ip6qhashfn(q->id, |
| 164 | &q->saddr, |
| 165 | &q->daddr); |
| 166 | |
| 167 | if (hval != i) { |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 168 | hlist_del(&q->q.list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
| 170 | /* Relink to new hash chain. */ |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 171 | hlist_add_head(&q->q.list, |
Yasuyuki Kozakai | e7c8a41 | 2005-11-16 12:55:37 -0800 | [diff] [blame] | 172 | &ip6_frag_hash[hval]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
Yasuyuki Kozakai | e7c8a41 | 2005-11-16 12:55:37 -0800 | [diff] [blame] | 174 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | write_unlock(&ip6_frag_lock); |
| 178 | |
| 179 | mod_timer(&ip6_frag_secret_timer, now + sysctl_ip6frag_secret_interval); |
| 180 | } |
| 181 | |
| 182 | atomic_t ip6_frag_mem = ATOMIC_INIT(0); |
| 183 | |
| 184 | /* Memory Tracking Functions. */ |
| 185 | static inline void frag_kfree_skb(struct sk_buff *skb, int *work) |
| 186 | { |
| 187 | if (work) |
| 188 | *work -= skb->truesize; |
| 189 | atomic_sub(skb->truesize, &ip6_frag_mem); |
| 190 | kfree_skb(skb); |
| 191 | } |
| 192 | |
| 193 | static inline void frag_free_queue(struct frag_queue *fq, int *work) |
| 194 | { |
| 195 | if (work) |
| 196 | *work -= sizeof(struct frag_queue); |
| 197 | atomic_sub(sizeof(struct frag_queue), &ip6_frag_mem); |
| 198 | kfree(fq); |
| 199 | } |
| 200 | |
| 201 | static inline struct frag_queue *frag_alloc_queue(void) |
| 202 | { |
Ingo Oeser | 78c784c | 2006-03-20 23:01:17 -0800 | [diff] [blame] | 203 | struct frag_queue *fq = kzalloc(sizeof(struct frag_queue), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
| 205 | if(!fq) |
| 206 | return NULL; |
| 207 | atomic_add(sizeof(struct frag_queue), &ip6_frag_mem); |
| 208 | return fq; |
| 209 | } |
| 210 | |
| 211 | /* Destruction primitives. */ |
| 212 | |
| 213 | /* Complete destruction of fq. */ |
| 214 | static void ip6_frag_destroy(struct frag_queue *fq, int *work) |
| 215 | { |
| 216 | struct sk_buff *fp; |
| 217 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 218 | BUG_TRAP(fq->q.last_in&COMPLETE); |
| 219 | BUG_TRAP(del_timer(&fq->q.timer) == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
| 221 | /* Release all fragment data. */ |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 222 | fp = fq->q.fragments; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | while (fp) { |
| 224 | struct sk_buff *xp = fp->next; |
| 225 | |
| 226 | frag_kfree_skb(fp, work); |
| 227 | fp = xp; |
| 228 | } |
| 229 | |
| 230 | frag_free_queue(fq, work); |
| 231 | } |
| 232 | |
| 233 | static __inline__ void fq_put(struct frag_queue *fq, int *work) |
| 234 | { |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 235 | if (atomic_dec_and_test(&fq->q.refcnt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | ip6_frag_destroy(fq, work); |
| 237 | } |
| 238 | |
| 239 | /* Kill fq entry. It is not destroyed immediately, |
| 240 | * because caller (and someone more) holds reference count. |
| 241 | */ |
| 242 | static __inline__ void fq_kill(struct frag_queue *fq) |
| 243 | { |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 244 | if (del_timer(&fq->q.timer)) |
| 245 | atomic_dec(&fq->q.refcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 247 | if (!(fq->q.last_in & COMPLETE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | fq_unlink(fq); |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 249 | atomic_dec(&fq->q.refcnt); |
| 250 | fq->q.last_in |= COMPLETE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 254 | static void ip6_evictor(struct inet6_dev *idev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | { |
| 256 | struct frag_queue *fq; |
| 257 | struct list_head *tmp; |
| 258 | int work; |
| 259 | |
| 260 | work = atomic_read(&ip6_frag_mem) - sysctl_ip6frag_low_thresh; |
| 261 | if (work <= 0) |
| 262 | return; |
| 263 | |
| 264 | while(work > 0) { |
| 265 | read_lock(&ip6_frag_lock); |
| 266 | if (list_empty(&ip6_frag_lru_list)) { |
| 267 | read_unlock(&ip6_frag_lock); |
| 268 | return; |
| 269 | } |
| 270 | tmp = ip6_frag_lru_list.next; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 271 | fq = list_entry(tmp, struct frag_queue, q.lru_list); |
| 272 | atomic_inc(&fq->q.refcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | read_unlock(&ip6_frag_lock); |
| 274 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 275 | spin_lock(&fq->q.lock); |
| 276 | if (!(fq->q.last_in&COMPLETE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | fq_kill(fq); |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 278 | spin_unlock(&fq->q.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
| 280 | fq_put(fq, &work); |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 281 | IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
| 285 | static void ip6_frag_expire(unsigned long data) |
| 286 | { |
| 287 | struct frag_queue *fq = (struct frag_queue *) data; |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 288 | struct net_device *dev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 290 | spin_lock(&fq->q.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 292 | if (fq->q.last_in & COMPLETE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | goto out; |
| 294 | |
| 295 | fq_kill(fq); |
| 296 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 297 | dev = dev_get_by_index(&init_net, fq->iif); |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 298 | if (!dev) |
| 299 | goto out; |
| 300 | |
| 301 | rcu_read_lock(); |
| 302 | IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMTIMEOUT); |
| 303 | IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS); |
| 304 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
Ingo Oeser | 78c784c | 2006-03-20 23:01:17 -0800 | [diff] [blame] | 306 | /* Don't send error if the first segment did not arrive. */ |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 307 | if (!(fq->q.last_in&FIRST_IN) || !fq->q.fragments) |
Ingo Oeser | 78c784c | 2006-03-20 23:01:17 -0800 | [diff] [blame] | 308 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
Ingo Oeser | 78c784c | 2006-03-20 23:01:17 -0800 | [diff] [blame] | 310 | /* |
| 311 | But use as source device on which LAST ARRIVED |
| 312 | segment was received. And do not use fq->dev |
| 313 | pointer directly, device might already disappeared. |
| 314 | */ |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 315 | fq->q.fragments->dev = dev; |
| 316 | 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] | 317 | out: |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 318 | if (dev) |
| 319 | dev_put(dev); |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 320 | spin_unlock(&fq->q.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | fq_put(fq, NULL); |
| 322 | } |
| 323 | |
| 324 | /* Creation primitives. */ |
| 325 | |
| 326 | |
Zach Brown | f6596f9 | 2006-04-10 16:05:34 -0700 | [diff] [blame] | 327 | static struct frag_queue *ip6_frag_intern(struct frag_queue *fq_in) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | { |
| 329 | struct frag_queue *fq; |
Zach Brown | f6596f9 | 2006-04-10 16:05:34 -0700 | [diff] [blame] | 330 | unsigned int hash; |
Yasuyuki Kozakai | e7c8a41 | 2005-11-16 12:55:37 -0800 | [diff] [blame] | 331 | #ifdef CONFIG_SMP |
| 332 | struct hlist_node *n; |
| 333 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
| 335 | write_lock(&ip6_frag_lock); |
Zach Brown | f6596f9 | 2006-04-10 16:05:34 -0700 | [diff] [blame] | 336 | hash = ip6qhashfn(fq_in->id, &fq_in->saddr, &fq_in->daddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | #ifdef CONFIG_SMP |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 338 | hlist_for_each_entry(fq, n, &ip6_frag_hash[hash], q.list) { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 339 | if (fq->id == fq_in->id && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | ipv6_addr_equal(&fq_in->saddr, &fq->saddr) && |
| 341 | ipv6_addr_equal(&fq_in->daddr, &fq->daddr)) { |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 342 | atomic_inc(&fq->q.refcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | write_unlock(&ip6_frag_lock); |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 344 | fq_in->q.last_in |= COMPLETE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | fq_put(fq_in, NULL); |
| 346 | return fq; |
| 347 | } |
| 348 | } |
| 349 | #endif |
| 350 | fq = fq_in; |
| 351 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 352 | if (!mod_timer(&fq->q.timer, jiffies + sysctl_ip6frag_time)) |
| 353 | atomic_inc(&fq->q.refcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 355 | atomic_inc(&fq->q.refcnt); |
| 356 | hlist_add_head(&fq->q.list, &ip6_frag_hash[hash]); |
| 357 | INIT_LIST_HEAD(&fq->q.lru_list); |
| 358 | list_add_tail(&fq->q.lru_list, &ip6_frag_lru_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | ip6_frag_nqueues++; |
| 360 | write_unlock(&ip6_frag_lock); |
| 361 | return fq; |
| 362 | } |
| 363 | |
| 364 | |
| 365 | static struct frag_queue * |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 366 | ip6_frag_create(__be32 id, struct in6_addr *src, struct in6_addr *dst, |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 367 | struct inet6_dev *idev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | { |
| 369 | struct frag_queue *fq; |
| 370 | |
| 371 | if ((fq = frag_alloc_queue()) == NULL) |
| 372 | goto oom; |
| 373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | fq->id = id; |
| 375 | ipv6_addr_copy(&fq->saddr, src); |
| 376 | ipv6_addr_copy(&fq->daddr, dst); |
| 377 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 378 | init_timer(&fq->q.timer); |
| 379 | fq->q.timer.function = ip6_frag_expire; |
| 380 | fq->q.timer.data = (long) fq; |
| 381 | spin_lock_init(&fq->q.lock); |
| 382 | atomic_set(&fq->q.refcnt, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
Zach Brown | f6596f9 | 2006-04-10 16:05:34 -0700 | [diff] [blame] | 384 | return ip6_frag_intern(fq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
| 386 | oom: |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 387 | IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | return NULL; |
| 389 | } |
| 390 | |
| 391 | static __inline__ struct frag_queue * |
Al Viro | e69a4ad | 2006-11-14 20:56:00 -0800 | [diff] [blame] | 392 | fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst, |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 393 | struct inet6_dev *idev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | { |
| 395 | struct frag_queue *fq; |
Yasuyuki Kozakai | e7c8a41 | 2005-11-16 12:55:37 -0800 | [diff] [blame] | 396 | struct hlist_node *n; |
Zach Brown | f6596f9 | 2006-04-10 16:05:34 -0700 | [diff] [blame] | 397 | unsigned int hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
| 399 | read_lock(&ip6_frag_lock); |
Zach Brown | f6596f9 | 2006-04-10 16:05:34 -0700 | [diff] [blame] | 400 | hash = ip6qhashfn(id, src, dst); |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 401 | hlist_for_each_entry(fq, n, &ip6_frag_hash[hash], q.list) { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 402 | if (fq->id == id && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | ipv6_addr_equal(src, &fq->saddr) && |
| 404 | ipv6_addr_equal(dst, &fq->daddr)) { |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 405 | atomic_inc(&fq->q.refcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | read_unlock(&ip6_frag_lock); |
| 407 | return fq; |
| 408 | } |
| 409 | } |
| 410 | read_unlock(&ip6_frag_lock); |
| 411 | |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 412 | return ip6_frag_create(id, src, dst, idev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 416 | 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] | 417 | struct frag_hdr *fhdr, int nhoff) |
| 418 | { |
| 419 | struct sk_buff *prev, *next; |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 420 | struct net_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | int offset, end; |
| 422 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 423 | if (fq->q.last_in & COMPLETE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | goto err; |
| 425 | |
| 426 | offset = ntohs(fhdr->frag_off) & ~0x7; |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 427 | end = offset + (ntohs(ipv6_hdr(skb)->payload_len) - |
| 428 | ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
| 430 | if ((unsigned int)end > IPV6_MAXPLEN) { |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 431 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), |
| 432 | IPSTATS_MIB_INHDRERRORS); |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 433 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
| 434 | ((u8 *)&fhdr->frag_off - |
| 435 | skb_network_header(skb))); |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 436 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 439 | if (skb->ip_summed == CHECKSUM_COMPLETE) { |
| 440 | const unsigned char *nh = skb_network_header(skb); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 441 | skb->csum = csum_sub(skb->csum, |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 442 | csum_partial(nh, (u8 *)(fhdr + 1) - nh, |
| 443 | 0)); |
| 444 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
| 446 | /* Is this the final fragment? */ |
| 447 | if (!(fhdr->frag_off & htons(IP6_MF))) { |
| 448 | /* If we already have some bits beyond end |
| 449 | * or have different end, the segment is corrupted. |
| 450 | */ |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 451 | if (end < fq->q.len || |
| 452 | ((fq->q.last_in & LAST_IN) && end != fq->q.len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | goto err; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 454 | fq->q.last_in |= LAST_IN; |
| 455 | fq->q.len = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | } else { |
| 457 | /* Check if the fragment is rounded to 8 bytes. |
| 458 | * Required by the RFC. |
| 459 | */ |
| 460 | if (end & 0x7) { |
| 461 | /* RFC2460 says always send parameter problem in |
| 462 | * this case. -DaveM |
| 463 | */ |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 464 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), |
| 465 | IPSTATS_MIB_INHDRERRORS); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 466 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | offsetof(struct ipv6hdr, payload_len)); |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 468 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | } |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 470 | if (end > fq->q.len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | /* Some bits beyond end -> corruption. */ |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 472 | if (fq->q.last_in & LAST_IN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | goto err; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 474 | fq->q.len = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
| 478 | if (end == offset) |
| 479 | goto err; |
| 480 | |
| 481 | /* Point into the IP datagram 'data' part. */ |
| 482 | if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) |
| 483 | goto err; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 484 | |
Stephen Hemminger | 42ca89c | 2005-09-08 12:57:43 -0700 | [diff] [blame] | 485 | if (pskb_trim_rcsum(skb, end - offset)) |
| 486 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
| 488 | /* Find out which fragments are in front and at the back of us |
| 489 | * in the chain of fragments so far. We must know where to put |
| 490 | * this fragment, right? |
| 491 | */ |
| 492 | prev = NULL; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 493 | for(next = fq->q.fragments; next != NULL; next = next->next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | if (FRAG6_CB(next)->offset >= offset) |
| 495 | break; /* bingo! */ |
| 496 | prev = next; |
| 497 | } |
| 498 | |
| 499 | /* We found where to put this one. Check for overlap with |
| 500 | * preceding fragment, and, if needed, align things so that |
| 501 | * any overlaps are eliminated. |
| 502 | */ |
| 503 | if (prev) { |
| 504 | int i = (FRAG6_CB(prev)->offset + prev->len) - offset; |
| 505 | |
| 506 | if (i > 0) { |
| 507 | offset += i; |
| 508 | if (end <= offset) |
| 509 | goto err; |
| 510 | if (!pskb_pull(skb, i)) |
| 511 | goto err; |
| 512 | if (skb->ip_summed != CHECKSUM_UNNECESSARY) |
| 513 | skb->ip_summed = CHECKSUM_NONE; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | /* Look for overlap with succeeding segments. |
| 518 | * If we can merge fragments, do it. |
| 519 | */ |
| 520 | while (next && FRAG6_CB(next)->offset < end) { |
| 521 | int i = end - FRAG6_CB(next)->offset; /* overlap is 'i' bytes */ |
| 522 | |
| 523 | if (i < next->len) { |
| 524 | /* Eat head of the next overlapped fragment |
| 525 | * and leave the loop. The next ones cannot overlap. |
| 526 | */ |
| 527 | if (!pskb_pull(next, i)) |
| 528 | goto err; |
| 529 | FRAG6_CB(next)->offset += i; /* next fragment */ |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 530 | fq->q.meat -= i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | if (next->ip_summed != CHECKSUM_UNNECESSARY) |
| 532 | next->ip_summed = CHECKSUM_NONE; |
| 533 | break; |
| 534 | } else { |
| 535 | struct sk_buff *free_it = next; |
| 536 | |
| 537 | /* Old fragment is completely overridden with |
| 538 | * new one drop it. |
| 539 | */ |
| 540 | next = next->next; |
| 541 | |
| 542 | if (prev) |
| 543 | prev->next = next; |
| 544 | else |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 545 | fq->q.fragments = next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 547 | fq->q.meat -= free_it->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | frag_kfree_skb(free_it, NULL); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | FRAG6_CB(skb)->offset = offset; |
| 553 | |
| 554 | /* Insert this fragment in the chain of fragments. */ |
| 555 | skb->next = next; |
| 556 | if (prev) |
| 557 | prev->next = skb; |
| 558 | else |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 559 | fq->q.fragments = skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 561 | dev = skb->dev; |
| 562 | if (dev) { |
| 563 | fq->iif = dev->ifindex; |
| 564 | skb->dev = NULL; |
| 565 | } |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 566 | fq->q.stamp = skb->tstamp; |
| 567 | fq->q.meat += skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | atomic_add(skb->truesize, &ip6_frag_mem); |
| 569 | |
| 570 | /* The first fragment. |
| 571 | * nhoffset is obtained from the first fragment, of course. |
| 572 | */ |
| 573 | if (offset == 0) { |
| 574 | fq->nhoffset = nhoff; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 575 | fq->q.last_in |= FIRST_IN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | } |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 577 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 578 | 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] | 579 | return ip6_frag_reasm(fq, prev, dev); |
| 580 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | write_lock(&ip6_frag_lock); |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 582 | list_move_tail(&fq->q.lru_list, &ip6_frag_lru_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | write_unlock(&ip6_frag_lock); |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 584 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
| 586 | err: |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 587 | IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | kfree_skb(skb); |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 589 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | /* |
| 593 | * Check if this packet is complete. |
| 594 | * Returns NULL on failure by any reason, and pointer |
| 595 | * to current nexthdr field in reassembled frame. |
| 596 | * |
| 597 | * It is called with locked fq, and caller must check that |
| 598 | * queue is eligible for reassembly i.e. it is not COMPLETE, |
| 599 | * the last and the first frames arrived and all the bits are here. |
| 600 | */ |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 601 | 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] | 602 | struct net_device *dev) |
| 603 | { |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 604 | struct sk_buff *fp, *head = fq->q.fragments; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | int payload_len; |
| 606 | unsigned int nhoff; |
| 607 | |
| 608 | fq_kill(fq); |
| 609 | |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 610 | /* Make the one we just received the head. */ |
| 611 | if (prev) { |
| 612 | head = prev->next; |
| 613 | fp = skb_clone(head, GFP_ATOMIC); |
| 614 | |
| 615 | if (!fp) |
| 616 | goto out_oom; |
| 617 | |
| 618 | fp->next = head->next; |
| 619 | prev->next = fp; |
| 620 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 621 | skb_morph(head, fq->q.fragments); |
| 622 | head->next = fq->q.fragments->next; |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 623 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 624 | kfree_skb(fq->q.fragments); |
| 625 | fq->q.fragments = head; |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | BUG_TRAP(head != NULL); |
| 629 | BUG_TRAP(FRAG6_CB(head)->offset == 0); |
| 630 | |
| 631 | /* Unfragmented part is taken from the first segment. */ |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 632 | payload_len = ((head->data - skb_network_header(head)) - |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 633 | sizeof(struct ipv6hdr) + fq->q.len - |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 634 | sizeof(struct frag_hdr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | if (payload_len > IPV6_MAXPLEN) |
| 636 | goto out_oversize; |
| 637 | |
| 638 | /* Head of list must not be cloned. */ |
| 639 | if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC)) |
| 640 | goto out_oom; |
| 641 | |
| 642 | /* If the first fragment is fragmented itself, we split |
| 643 | * it to two chunks: the first with data and paged part |
| 644 | * and the second, holding only fragments. */ |
| 645 | if (skb_shinfo(head)->frag_list) { |
| 646 | struct sk_buff *clone; |
| 647 | int i, plen = 0; |
| 648 | |
| 649 | if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL) |
| 650 | goto out_oom; |
| 651 | clone->next = head->next; |
| 652 | head->next = clone; |
| 653 | skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list; |
| 654 | skb_shinfo(head)->frag_list = NULL; |
| 655 | for (i=0; i<skb_shinfo(head)->nr_frags; i++) |
| 656 | plen += skb_shinfo(head)->frags[i].size; |
| 657 | clone->len = clone->data_len = head->data_len - plen; |
| 658 | head->data_len -= clone->len; |
| 659 | head->len -= clone->len; |
| 660 | clone->csum = 0; |
| 661 | clone->ip_summed = head->ip_summed; |
| 662 | atomic_add(clone->truesize, &ip6_frag_mem); |
| 663 | } |
| 664 | |
| 665 | /* We have to remove fragment header from datagram and to relocate |
| 666 | * header in order to calculate ICV correctly. */ |
| 667 | nhoff = fq->nhoffset; |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 668 | skb_network_header(head)[nhoff] = skb_transport_header(head)[0]; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 669 | memmove(head->head + sizeof(struct frag_hdr), head->head, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | (head->data - head->head) - sizeof(struct frag_hdr)); |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 671 | head->mac_header += sizeof(struct frag_hdr); |
| 672 | head->network_header += sizeof(struct frag_hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | |
| 674 | skb_shinfo(head)->frag_list = head->next; |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 675 | skb_reset_transport_header(head); |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 676 | skb_push(head, head->data - skb_network_header(head)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | atomic_sub(head->truesize, &ip6_frag_mem); |
| 678 | |
| 679 | for (fp=head->next; fp; fp = fp->next) { |
| 680 | head->data_len += fp->len; |
| 681 | head->len += fp->len; |
| 682 | if (head->ip_summed != fp->ip_summed) |
| 683 | head->ip_summed = CHECKSUM_NONE; |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 684 | else if (head->ip_summed == CHECKSUM_COMPLETE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | head->csum = csum_add(head->csum, fp->csum); |
| 686 | head->truesize += fp->truesize; |
| 687 | atomic_sub(fp->truesize, &ip6_frag_mem); |
| 688 | } |
| 689 | |
| 690 | head->next = NULL; |
| 691 | head->dev = dev; |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 692 | head->tstamp = fq->q.stamp; |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 693 | ipv6_hdr(head)->payload_len = htons(payload_len); |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 694 | IP6CB(head)->nhoff = nhoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | /* Yes, and fold redundant checksum back. 8) */ |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 697 | if (head->ip_summed == CHECKSUM_COMPLETE) |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 698 | head->csum = csum_partial(skb_network_header(head), |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 699 | skb_network_header_len(head), |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 700 | head->csum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 702 | rcu_read_lock(); |
| 703 | IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMOKS); |
| 704 | rcu_read_unlock(); |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 705 | fq->q.fragments = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | return 1; |
| 707 | |
| 708 | out_oversize: |
| 709 | if (net_ratelimit()) |
| 710 | printk(KERN_DEBUG "ip6_frag_reasm: payload len = %d\n", payload_len); |
| 711 | goto out_fail; |
| 712 | out_oom: |
| 713 | if (net_ratelimit()) |
| 714 | printk(KERN_DEBUG "ip6_frag_reasm: no memory for reassembly\n"); |
| 715 | out_fail: |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 716 | rcu_read_lock(); |
| 717 | IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS); |
| 718 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | return -1; |
| 720 | } |
| 721 | |
Patrick McHardy | 951dbc8 | 2006-01-06 23:02:34 -0800 | [diff] [blame] | 722 | static int ipv6_frag_rcv(struct sk_buff **skbp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 724 | struct sk_buff *skb = *skbp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | struct frag_hdr *fhdr; |
| 726 | struct frag_queue *fq; |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 727 | struct ipv6hdr *hdr = ipv6_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 729 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMREQDS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | |
| 731 | /* Jumbo payload inhibits frag. header */ |
| 732 | if (hdr->payload_len==0) { |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 733 | 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] | 734 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
| 735 | skb_network_header_len(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | return -1; |
| 737 | } |
Arnaldo Carvalho de Melo | ea2ae17 | 2007-04-25 17:55:53 -0700 | [diff] [blame] | 738 | if (!pskb_may_pull(skb, (skb_transport_offset(skb) + |
| 739 | sizeof(struct frag_hdr)))) { |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 740 | 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] | 741 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
| 742 | skb_network_header_len(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | return -1; |
| 744 | } |
| 745 | |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 746 | hdr = ipv6_hdr(skb); |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 747 | fhdr = (struct frag_hdr *)skb_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | |
| 749 | if (!(fhdr->frag_off & htons(0xFFF9))) { |
| 750 | /* It is not a fragmented frame */ |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 751 | skb->transport_header += sizeof(struct frag_hdr); |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 752 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMOKS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 754 | IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | return 1; |
| 756 | } |
| 757 | |
| 758 | if (atomic_read(&ip6_frag_mem) > sysctl_ip6frag_high_thresh) |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 759 | ip6_evictor(ip6_dst_idev(skb->dst)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 761 | if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr, |
| 762 | ip6_dst_idev(skb->dst))) != NULL) { |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 763 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 765 | spin_lock(&fq->q.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | |
Herbert Xu | f61944e | 2007-10-15 01:28:47 -0700 | [diff] [blame] | 767 | ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | |
Pavel Emelyanov | 5ab11c9 | 2007-10-15 02:24:19 -0700 | [diff] [blame^] | 769 | spin_unlock(&fq->q.lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | fq_put(fq, NULL); |
| 771 | return ret; |
| 772 | } |
| 773 | |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 774 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | kfree_skb(skb); |
| 776 | return -1; |
| 777 | } |
| 778 | |
| 779 | static struct inet6_protocol frag_protocol = |
| 780 | { |
| 781 | .handler = ipv6_frag_rcv, |
| 782 | .flags = INET6_PROTO_NOPOLICY, |
| 783 | }; |
| 784 | |
| 785 | void __init ipv6_frag_init(void) |
| 786 | { |
| 787 | if (inet6_add_protocol(&frag_protocol, IPPROTO_FRAGMENT) < 0) |
| 788 | printk(KERN_ERR "ipv6_frag_init: Could not register protocol\n"); |
| 789 | |
| 790 | ip6_frag_hash_rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^ |
| 791 | (jiffies ^ (jiffies >> 6))); |
| 792 | |
| 793 | init_timer(&ip6_frag_secret_timer); |
| 794 | ip6_frag_secret_timer.function = ip6_frag_secret_rebuild; |
| 795 | ip6_frag_secret_timer.expires = jiffies + sysctl_ip6frag_secret_interval; |
| 796 | add_timer(&ip6_frag_secret_timer); |
| 797 | } |