Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * IPv6 fragment reassembly | |
1ab1457c | 3 | * Linux INET6 implementation |
1da177e4 LT |
4 | * |
5 | * Authors: | |
1ab1457c | 6 | * Pedro Roque <roque@di.fc.ul.pt> |
1da177e4 LT |
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 | ||
1ab1457c YH |
18 | /* |
19 | * Fixes: | |
1da177e4 LT |
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 | */ | |
1da177e4 LT |
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> | |
f61944ef | 45 | #include <linux/skbuff.h> |
1da177e4 LT |
46 | |
47 | #include <net/sock.h> | |
48 | #include <net/snmp.h> | |
49 | ||
50 | #include <net/ipv6.h> | |
a11d206d | 51 | #include <net/ip6_route.h> |
1da177e4 LT |
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> | |
5ab11c98 | 57 | #include <net/inet_frag.h> |
1da177e4 | 58 | |
ab32ea5d BH |
59 | int sysctl_ip6frag_high_thresh __read_mostly = 256*1024; |
60 | int sysctl_ip6frag_low_thresh __read_mostly = 192*1024; | |
1da177e4 | 61 | |
ab32ea5d | 62 | int sysctl_ip6frag_time __read_mostly = IPV6_FRAG_TIMEOUT; |
1da177e4 LT |
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 | { | |
5ab11c98 | 79 | struct inet_frag_queue q; |
1da177e4 | 80 | |
e69a4adc | 81 | __be32 id; /* fragment id */ |
1da177e4 LT |
82 | struct in6_addr saddr; |
83 | struct in6_addr daddr; | |
84 | ||
1da177e4 | 85 | int iif; |
1da177e4 | 86 | unsigned int csum; |
1da177e4 | 87 | __u16 nhoffset; |
1da177e4 LT |
88 | }; |
89 | ||
90 | /* Hash table. */ | |
91 | ||
92 | #define IP6Q_HASHSZ 64 | |
93 | ||
e7c8a41e | 94 | static struct hlist_head ip6_frag_hash[IP6Q_HASHSZ]; |
1da177e4 LT |
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 | ||
f61944ef HX |
100 | static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev, |
101 | struct net_device *dev); | |
102 | ||
1da177e4 LT |
103 | static __inline__ void __fq_unlink(struct frag_queue *fq) |
104 | { | |
5ab11c98 PE |
105 | hlist_del(&fq->q.list); |
106 | list_del(&fq->q.lru_list); | |
1da177e4 LT |
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 | ||
f6596f9d ZB |
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 | */ | |
e69a4adc | 121 | static unsigned int ip6qhashfn(__be32 id, struct in6_addr *saddr, |
1da177e4 LT |
122 | struct in6_addr *daddr) |
123 | { | |
124 | u32 a, b, c; | |
125 | ||
e69a4adc AV |
126 | a = (__force u32)saddr->s6_addr32[0]; |
127 | b = (__force u32)saddr->s6_addr32[1]; | |
128 | c = (__force u32)saddr->s6_addr32[2]; | |
1da177e4 LT |
129 | |
130 | a += JHASH_GOLDEN_RATIO; | |
131 | b += JHASH_GOLDEN_RATIO; | |
132 | c += ip6_frag_hash_rnd; | |
133 | __jhash_mix(a, b, c); | |
134 | ||
e69a4adc AV |
135 | a += (__force u32)saddr->s6_addr32[3]; |
136 | b += (__force u32)daddr->s6_addr32[0]; | |
137 | c += (__force u32)daddr->s6_addr32[1]; | |
1da177e4 LT |
138 | __jhash_mix(a, b, c); |
139 | ||
e69a4adc AV |
140 | a += (__force u32)daddr->s6_addr32[2]; |
141 | b += (__force u32)daddr->s6_addr32[3]; | |
142 | c += (__force u32)id; | |
1da177e4 LT |
143 | __jhash_mix(a, b, c); |
144 | ||
145 | return c & (IP6Q_HASHSZ - 1); | |
146 | } | |
147 | ||
148 | static struct timer_list ip6_frag_secret_timer; | |
ab32ea5d | 149 | int sysctl_ip6frag_secret_interval __read_mostly = 10 * 60 * HZ; |
1da177e4 LT |
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; | |
e7c8a41e | 160 | struct hlist_node *p, *n; |
1da177e4 | 161 | |
5ab11c98 | 162 | hlist_for_each_entry_safe(q, p, n, &ip6_frag_hash[i], q.list) { |
1da177e4 LT |
163 | unsigned int hval = ip6qhashfn(q->id, |
164 | &q->saddr, | |
165 | &q->daddr); | |
166 | ||
167 | if (hval != i) { | |
5ab11c98 | 168 | hlist_del(&q->q.list); |
1da177e4 LT |
169 | |
170 | /* Relink to new hash chain. */ | |
5ab11c98 | 171 | hlist_add_head(&q->q.list, |
e7c8a41e | 172 | &ip6_frag_hash[hval]); |
1da177e4 | 173 | |
e7c8a41e | 174 | } |
1da177e4 LT |
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 | { | |
78c784c4 | 203 | struct frag_queue *fq = kzalloc(sizeof(struct frag_queue), GFP_ATOMIC); |
1da177e4 LT |
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 | ||
5ab11c98 PE |
218 | BUG_TRAP(fq->q.last_in&COMPLETE); |
219 | BUG_TRAP(del_timer(&fq->q.timer) == 0); | |
1da177e4 LT |
220 | |
221 | /* Release all fragment data. */ | |
5ab11c98 | 222 | fp = fq->q.fragments; |
1da177e4 LT |
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 | { | |
5ab11c98 | 235 | if (atomic_dec_and_test(&fq->q.refcnt)) |
1da177e4 LT |
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 | { | |
5ab11c98 PE |
244 | if (del_timer(&fq->q.timer)) |
245 | atomic_dec(&fq->q.refcnt); | |
1da177e4 | 246 | |
5ab11c98 | 247 | if (!(fq->q.last_in & COMPLETE)) { |
1da177e4 | 248 | fq_unlink(fq); |
5ab11c98 PE |
249 | atomic_dec(&fq->q.refcnt); |
250 | fq->q.last_in |= COMPLETE; | |
1da177e4 LT |
251 | } |
252 | } | |
253 | ||
a11d206d | 254 | static void ip6_evictor(struct inet6_dev *idev) |
1da177e4 LT |
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; | |
5ab11c98 PE |
271 | fq = list_entry(tmp, struct frag_queue, q.lru_list); |
272 | atomic_inc(&fq->q.refcnt); | |
1da177e4 LT |
273 | read_unlock(&ip6_frag_lock); |
274 | ||
5ab11c98 PE |
275 | spin_lock(&fq->q.lock); |
276 | if (!(fq->q.last_in&COMPLETE)) | |
1da177e4 | 277 | fq_kill(fq); |
5ab11c98 | 278 | spin_unlock(&fq->q.lock); |
1da177e4 LT |
279 | |
280 | fq_put(fq, &work); | |
a11d206d | 281 | IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS); |
1da177e4 LT |
282 | } |
283 | } | |
284 | ||
285 | static void ip6_frag_expire(unsigned long data) | |
286 | { | |
287 | struct frag_queue *fq = (struct frag_queue *) data; | |
a11d206d | 288 | struct net_device *dev = NULL; |
1da177e4 | 289 | |
5ab11c98 | 290 | spin_lock(&fq->q.lock); |
1da177e4 | 291 | |
5ab11c98 | 292 | if (fq->q.last_in & COMPLETE) |
1da177e4 LT |
293 | goto out; |
294 | ||
295 | fq_kill(fq); | |
296 | ||
881d966b | 297 | dev = dev_get_by_index(&init_net, fq->iif); |
a11d206d YH |
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(); | |
1da177e4 | 305 | |
78c784c4 | 306 | /* Don't send error if the first segment did not arrive. */ |
5ab11c98 | 307 | if (!(fq->q.last_in&FIRST_IN) || !fq->q.fragments) |
78c784c4 IO |
308 | goto out; |
309 | ||
78c784c4 IO |
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 | */ | |
5ab11c98 PE |
315 | fq->q.fragments->dev = dev; |
316 | icmpv6_send(fq->q.fragments, ICMPV6_TIME_EXCEED, ICMPV6_EXC_FRAGTIME, 0, dev); | |
1da177e4 | 317 | out: |
a11d206d YH |
318 | if (dev) |
319 | dev_put(dev); | |
5ab11c98 | 320 | spin_unlock(&fq->q.lock); |
1da177e4 LT |
321 | fq_put(fq, NULL); |
322 | } | |
323 | ||
324 | /* Creation primitives. */ | |
325 | ||
326 | ||
f6596f9d | 327 | static struct frag_queue *ip6_frag_intern(struct frag_queue *fq_in) |
1da177e4 LT |
328 | { |
329 | struct frag_queue *fq; | |
f6596f9d | 330 | unsigned int hash; |
e7c8a41e YK |
331 | #ifdef CONFIG_SMP |
332 | struct hlist_node *n; | |
333 | #endif | |
1da177e4 LT |
334 | |
335 | write_lock(&ip6_frag_lock); | |
f6596f9d | 336 | hash = ip6qhashfn(fq_in->id, &fq_in->saddr, &fq_in->daddr); |
1da177e4 | 337 | #ifdef CONFIG_SMP |
5ab11c98 | 338 | hlist_for_each_entry(fq, n, &ip6_frag_hash[hash], q.list) { |
1ab1457c | 339 | if (fq->id == fq_in->id && |
1da177e4 LT |
340 | ipv6_addr_equal(&fq_in->saddr, &fq->saddr) && |
341 | ipv6_addr_equal(&fq_in->daddr, &fq->daddr)) { | |
5ab11c98 | 342 | atomic_inc(&fq->q.refcnt); |
1da177e4 | 343 | write_unlock(&ip6_frag_lock); |
5ab11c98 | 344 | fq_in->q.last_in |= COMPLETE; |
1da177e4 LT |
345 | fq_put(fq_in, NULL); |
346 | return fq; | |
347 | } | |
348 | } | |
349 | #endif | |
350 | fq = fq_in; | |
351 | ||
5ab11c98 PE |
352 | if (!mod_timer(&fq->q.timer, jiffies + sysctl_ip6frag_time)) |
353 | atomic_inc(&fq->q.refcnt); | |
1da177e4 | 354 | |
5ab11c98 PE |
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); | |
1da177e4 LT |
359 | ip6_frag_nqueues++; |
360 | write_unlock(&ip6_frag_lock); | |
361 | return fq; | |
362 | } | |
363 | ||
364 | ||
365 | static struct frag_queue * | |
e69a4adc | 366 | ip6_frag_create(__be32 id, struct in6_addr *src, struct in6_addr *dst, |
a11d206d | 367 | struct inet6_dev *idev) |
1da177e4 LT |
368 | { |
369 | struct frag_queue *fq; | |
370 | ||
371 | if ((fq = frag_alloc_queue()) == NULL) | |
372 | goto oom; | |
373 | ||
1da177e4 LT |
374 | fq->id = id; |
375 | ipv6_addr_copy(&fq->saddr, src); | |
376 | ipv6_addr_copy(&fq->daddr, dst); | |
377 | ||
5ab11c98 PE |
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); | |
1da177e4 | 383 | |
f6596f9d | 384 | return ip6_frag_intern(fq); |
1da177e4 LT |
385 | |
386 | oom: | |
a11d206d | 387 | IP6_INC_STATS_BH(idev, IPSTATS_MIB_REASMFAILS); |
1da177e4 LT |
388 | return NULL; |
389 | } | |
390 | ||
391 | static __inline__ struct frag_queue * | |
e69a4adc | 392 | fq_find(__be32 id, struct in6_addr *src, struct in6_addr *dst, |
a11d206d | 393 | struct inet6_dev *idev) |
1da177e4 LT |
394 | { |
395 | struct frag_queue *fq; | |
e7c8a41e | 396 | struct hlist_node *n; |
f6596f9d | 397 | unsigned int hash; |
1da177e4 LT |
398 | |
399 | read_lock(&ip6_frag_lock); | |
f6596f9d | 400 | hash = ip6qhashfn(id, src, dst); |
5ab11c98 | 401 | hlist_for_each_entry(fq, n, &ip6_frag_hash[hash], q.list) { |
1ab1457c | 402 | if (fq->id == id && |
1da177e4 LT |
403 | ipv6_addr_equal(src, &fq->saddr) && |
404 | ipv6_addr_equal(dst, &fq->daddr)) { | |
5ab11c98 | 405 | atomic_inc(&fq->q.refcnt); |
1da177e4 LT |
406 | read_unlock(&ip6_frag_lock); |
407 | return fq; | |
408 | } | |
409 | } | |
410 | read_unlock(&ip6_frag_lock); | |
411 | ||
a11d206d | 412 | return ip6_frag_create(id, src, dst, idev); |
1da177e4 LT |
413 | } |
414 | ||
415 | ||
f61944ef | 416 | static int ip6_frag_queue(struct frag_queue *fq, struct sk_buff *skb, |
1da177e4 LT |
417 | struct frag_hdr *fhdr, int nhoff) |
418 | { | |
419 | struct sk_buff *prev, *next; | |
f61944ef | 420 | struct net_device *dev; |
1da177e4 LT |
421 | int offset, end; |
422 | ||
5ab11c98 | 423 | if (fq->q.last_in & COMPLETE) |
1da177e4 LT |
424 | goto err; |
425 | ||
426 | offset = ntohs(fhdr->frag_off) & ~0x7; | |
0660e03f ACM |
427 | end = offset + (ntohs(ipv6_hdr(skb)->payload_len) - |
428 | ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1))); | |
1da177e4 LT |
429 | |
430 | if ((unsigned int)end > IPV6_MAXPLEN) { | |
a11d206d YH |
431 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), |
432 | IPSTATS_MIB_INHDRERRORS); | |
d56f90a7 ACM |
433 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
434 | ((u8 *)&fhdr->frag_off - | |
435 | skb_network_header(skb))); | |
f61944ef | 436 | return -1; |
1da177e4 LT |
437 | } |
438 | ||
d56f90a7 ACM |
439 | if (skb->ip_summed == CHECKSUM_COMPLETE) { |
440 | const unsigned char *nh = skb_network_header(skb); | |
1ab1457c | 441 | skb->csum = csum_sub(skb->csum, |
d56f90a7 ACM |
442 | csum_partial(nh, (u8 *)(fhdr + 1) - nh, |
443 | 0)); | |
444 | } | |
1da177e4 LT |
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 | */ | |
5ab11c98 PE |
451 | if (end < fq->q.len || |
452 | ((fq->q.last_in & LAST_IN) && end != fq->q.len)) | |
1da177e4 | 453 | goto err; |
5ab11c98 PE |
454 | fq->q.last_in |= LAST_IN; |
455 | fq->q.len = end; | |
1da177e4 LT |
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 | */ | |
a11d206d YH |
464 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), |
465 | IPSTATS_MIB_INHDRERRORS); | |
1ab1457c | 466 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
1da177e4 | 467 | offsetof(struct ipv6hdr, payload_len)); |
f61944ef | 468 | return -1; |
1da177e4 | 469 | } |
5ab11c98 | 470 | if (end > fq->q.len) { |
1da177e4 | 471 | /* Some bits beyond end -> corruption. */ |
5ab11c98 | 472 | if (fq->q.last_in & LAST_IN) |
1da177e4 | 473 | goto err; |
5ab11c98 | 474 | fq->q.len = end; |
1da177e4 LT |
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; | |
1ab1457c | 484 | |
42ca89c1 SH |
485 | if (pskb_trim_rcsum(skb, end - offset)) |
486 | goto err; | |
1da177e4 LT |
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; | |
5ab11c98 | 493 | for(next = fq->q.fragments; next != NULL; next = next->next) { |
1da177e4 LT |
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 */ | |
5ab11c98 | 530 | fq->q.meat -= i; |
1da177e4 LT |
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 | |
5ab11c98 | 545 | fq->q.fragments = next; |
1da177e4 | 546 | |
5ab11c98 | 547 | fq->q.meat -= free_it->len; |
1da177e4 LT |
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 | |
5ab11c98 | 559 | fq->q.fragments = skb; |
1da177e4 | 560 | |
f61944ef HX |
561 | dev = skb->dev; |
562 | if (dev) { | |
563 | fq->iif = dev->ifindex; | |
564 | skb->dev = NULL; | |
565 | } | |
5ab11c98 PE |
566 | fq->q.stamp = skb->tstamp; |
567 | fq->q.meat += skb->len; | |
1da177e4 LT |
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; | |
5ab11c98 | 575 | fq->q.last_in |= FIRST_IN; |
1da177e4 | 576 | } |
f61944ef | 577 | |
5ab11c98 | 578 | if (fq->q.last_in == (FIRST_IN | LAST_IN) && fq->q.meat == fq->q.len) |
f61944ef HX |
579 | return ip6_frag_reasm(fq, prev, dev); |
580 | ||
1da177e4 | 581 | write_lock(&ip6_frag_lock); |
5ab11c98 | 582 | list_move_tail(&fq->q.lru_list, &ip6_frag_lru_list); |
1da177e4 | 583 | write_unlock(&ip6_frag_lock); |
f61944ef | 584 | return -1; |
1da177e4 LT |
585 | |
586 | err: | |
a11d206d | 587 | IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS); |
1da177e4 | 588 | kfree_skb(skb); |
f61944ef | 589 | return -1; |
1da177e4 LT |
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 | */ | |
f61944ef | 601 | static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev, |
1da177e4 LT |
602 | struct net_device *dev) |
603 | { | |
5ab11c98 | 604 | struct sk_buff *fp, *head = fq->q.fragments; |
1da177e4 LT |
605 | int payload_len; |
606 | unsigned int nhoff; | |
607 | ||
608 | fq_kill(fq); | |
609 | ||
f61944ef HX |
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 | ||
5ab11c98 PE |
621 | skb_morph(head, fq->q.fragments); |
622 | head->next = fq->q.fragments->next; | |
f61944ef | 623 | |
5ab11c98 PE |
624 | kfree_skb(fq->q.fragments); |
625 | fq->q.fragments = head; | |
f61944ef HX |
626 | } |
627 | ||
1da177e4 LT |
628 | BUG_TRAP(head != NULL); |
629 | BUG_TRAP(FRAG6_CB(head)->offset == 0); | |
630 | ||
631 | /* Unfragmented part is taken from the first segment. */ | |
d56f90a7 | 632 | payload_len = ((head->data - skb_network_header(head)) - |
5ab11c98 | 633 | sizeof(struct ipv6hdr) + fq->q.len - |
d56f90a7 | 634 | sizeof(struct frag_hdr)); |
1da177e4 LT |
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; | |
b0e380b1 | 668 | skb_network_header(head)[nhoff] = skb_transport_header(head)[0]; |
1ab1457c | 669 | memmove(head->head + sizeof(struct frag_hdr), head->head, |
1da177e4 | 670 | (head->data - head->head) - sizeof(struct frag_hdr)); |
b0e380b1 ACM |
671 | head->mac_header += sizeof(struct frag_hdr); |
672 | head->network_header += sizeof(struct frag_hdr); | |
1da177e4 LT |
673 | |
674 | skb_shinfo(head)->frag_list = head->next; | |
badff6d0 | 675 | skb_reset_transport_header(head); |
d56f90a7 | 676 | skb_push(head, head->data - skb_network_header(head)); |
1da177e4 LT |
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; | |
84fa7933 | 684 | else if (head->ip_summed == CHECKSUM_COMPLETE) |
1da177e4 LT |
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; | |
5ab11c98 | 692 | head->tstamp = fq->q.stamp; |
0660e03f | 693 | ipv6_hdr(head)->payload_len = htons(payload_len); |
951dbc8a | 694 | IP6CB(head)->nhoff = nhoff; |
1da177e4 | 695 | |
1da177e4 | 696 | /* Yes, and fold redundant checksum back. 8) */ |
84fa7933 | 697 | if (head->ip_summed == CHECKSUM_COMPLETE) |
d56f90a7 | 698 | head->csum = csum_partial(skb_network_header(head), |
cfe1fc77 | 699 | skb_network_header_len(head), |
d56f90a7 | 700 | head->csum); |
1da177e4 | 701 | |
a11d206d YH |
702 | rcu_read_lock(); |
703 | IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMOKS); | |
704 | rcu_read_unlock(); | |
5ab11c98 | 705 | fq->q.fragments = NULL; |
1da177e4 LT |
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: | |
a11d206d YH |
716 | rcu_read_lock(); |
717 | IP6_INC_STATS_BH(__in6_dev_get(dev), IPSTATS_MIB_REASMFAILS); | |
718 | rcu_read_unlock(); | |
1da177e4 LT |
719 | return -1; |
720 | } | |
721 | ||
951dbc8a | 722 | static int ipv6_frag_rcv(struct sk_buff **skbp) |
1da177e4 | 723 | { |
1ab1457c | 724 | struct sk_buff *skb = *skbp; |
1da177e4 LT |
725 | struct frag_hdr *fhdr; |
726 | struct frag_queue *fq; | |
0660e03f | 727 | struct ipv6hdr *hdr = ipv6_hdr(skb); |
1da177e4 | 728 | |
a11d206d | 729 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMREQDS); |
1da177e4 LT |
730 | |
731 | /* Jumbo payload inhibits frag. header */ | |
732 | if (hdr->payload_len==0) { | |
a11d206d | 733 | IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS); |
cfe1fc77 ACM |
734 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
735 | skb_network_header_len(skb)); | |
1da177e4 LT |
736 | return -1; |
737 | } | |
ea2ae17d ACM |
738 | if (!pskb_may_pull(skb, (skb_transport_offset(skb) + |
739 | sizeof(struct frag_hdr)))) { | |
a11d206d | 740 | IP6_INC_STATS(ip6_dst_idev(skb->dst), IPSTATS_MIB_INHDRERRORS); |
cfe1fc77 ACM |
741 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
742 | skb_network_header_len(skb)); | |
1da177e4 LT |
743 | return -1; |
744 | } | |
745 | ||
0660e03f | 746 | hdr = ipv6_hdr(skb); |
9c70220b | 747 | fhdr = (struct frag_hdr *)skb_transport_header(skb); |
1da177e4 LT |
748 | |
749 | if (!(fhdr->frag_off & htons(0xFFF9))) { | |
750 | /* It is not a fragmented frame */ | |
b0e380b1 | 751 | skb->transport_header += sizeof(struct frag_hdr); |
a11d206d | 752 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMOKS); |
1da177e4 | 753 | |
d56f90a7 | 754 | IP6CB(skb)->nhoff = (u8 *)fhdr - skb_network_header(skb); |
1da177e4 LT |
755 | return 1; |
756 | } | |
757 | ||
758 | if (atomic_read(&ip6_frag_mem) > sysctl_ip6frag_high_thresh) | |
a11d206d | 759 | ip6_evictor(ip6_dst_idev(skb->dst)); |
1da177e4 | 760 | |
a11d206d YH |
761 | if ((fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr, |
762 | ip6_dst_idev(skb->dst))) != NULL) { | |
f61944ef | 763 | int ret; |
1da177e4 | 764 | |
5ab11c98 | 765 | spin_lock(&fq->q.lock); |
1da177e4 | 766 | |
f61944ef | 767 | ret = ip6_frag_queue(fq, skb, fhdr, IP6CB(skb)->nhoff); |
1da177e4 | 768 | |
5ab11c98 | 769 | spin_unlock(&fq->q.lock); |
1da177e4 LT |
770 | fq_put(fq, NULL); |
771 | return ret; | |
772 | } | |
773 | ||
a11d206d | 774 | IP6_INC_STATS_BH(ip6_dst_idev(skb->dst), IPSTATS_MIB_REASMFAILS); |
1da177e4 LT |
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 | } |