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