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