]>
Commit | Line | Data |
---|---|---|
9fb9cbb1 YK |
1 | /* |
2 | * IPv6 fragment reassembly for connection tracking | |
3 | * | |
4 | * Copyright (C)2004 USAGI/WIDE Project | |
5 | * | |
6 | * Author: | |
7 | * Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp> | |
8 | * | |
9 | * Based on: net/ipv6/reassembly.c | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or | |
12 | * modify it under the terms of the GNU General Public License | |
13 | * as published by the Free Software Foundation; either version | |
14 | * 2 of the License, or (at your option) any later version. | |
15 | */ | |
16 | ||
9fb9cbb1 YK |
17 | #include <linux/errno.h> |
18 | #include <linux/types.h> | |
19 | #include <linux/string.h> | |
20 | #include <linux/socket.h> | |
21 | #include <linux/sockios.h> | |
22 | #include <linux/jiffies.h> | |
23 | #include <linux/net.h> | |
24 | #include <linux/list.h> | |
25 | #include <linux/netdevice.h> | |
26 | #include <linux/in6.h> | |
27 | #include <linux/ipv6.h> | |
28 | #include <linux/icmpv6.h> | |
29 | #include <linux/random.h> | |
30 | #include <linux/jhash.h> | |
31 | ||
32 | #include <net/sock.h> | |
33 | #include <net/snmp.h> | |
34 | ||
35 | #include <net/ipv6.h> | |
36 | #include <net/protocol.h> | |
37 | #include <net/transp_v6.h> | |
38 | #include <net/rawv6.h> | |
39 | #include <net/ndisc.h> | |
40 | #include <net/addrconf.h> | |
41 | #include <linux/sysctl.h> | |
42 | #include <linux/netfilter.h> | |
43 | #include <linux/netfilter_ipv6.h> | |
44 | #include <linux/kernel.h> | |
45 | #include <linux/module.h> | |
46 | ||
47 | #if 0 | |
48 | #define DEBUGP printk | |
49 | #else | |
50 | #define DEBUGP(format, args...) | |
51 | #endif | |
52 | ||
53 | #define NF_CT_FRAG6_HIGH_THRESH 262144 /* == 256*1024 */ | |
54 | #define NF_CT_FRAG6_LOW_THRESH 196608 /* == 192*1024 */ | |
55 | #define NF_CT_FRAG6_TIMEOUT IPV6_FRAG_TIMEOUT | |
56 | ||
94aec08e BH |
57 | unsigned int nf_ct_frag6_high_thresh __read_mostly = 256*1024; |
58 | unsigned int nf_ct_frag6_low_thresh __read_mostly = 192*1024; | |
59 | unsigned long nf_ct_frag6_timeout __read_mostly = IPV6_FRAG_TIMEOUT; | |
9fb9cbb1 YK |
60 | |
61 | struct nf_ct_frag6_skb_cb | |
62 | { | |
63 | struct inet6_skb_parm h; | |
64 | int offset; | |
65 | struct sk_buff *orig; | |
66 | }; | |
67 | ||
68 | #define NFCT_FRAG6_CB(skb) ((struct nf_ct_frag6_skb_cb*)((skb)->cb)) | |
69 | ||
70 | struct nf_ct_frag6_queue | |
71 | { | |
2e4e6a17 HW |
72 | struct hlist_node list; |
73 | struct list_head lru_list; /* lru list member */ | |
9fb9cbb1 YK |
74 | |
75 | __u32 id; /* fragment id */ | |
76 | struct in6_addr saddr; | |
77 | struct in6_addr daddr; | |
78 | ||
79 | spinlock_t lock; | |
80 | atomic_t refcnt; | |
81 | struct timer_list timer; /* expire timer */ | |
82 | struct sk_buff *fragments; | |
83 | int len; | |
84 | int meat; | |
85 | struct timeval stamp; | |
86 | unsigned int csum; | |
87 | __u8 last_in; /* has first/last segment arrived? */ | |
88 | #define COMPLETE 4 | |
89 | #define FIRST_IN 2 | |
90 | #define LAST_IN 1 | |
91 | __u16 nhoffset; | |
9fb9cbb1 YK |
92 | }; |
93 | ||
94 | /* Hash table. */ | |
95 | ||
96 | #define FRAG6Q_HASHSZ 64 | |
97 | ||
2e4e6a17 | 98 | static struct hlist_head nf_ct_frag6_hash[FRAG6Q_HASHSZ]; |
181a46a5 | 99 | static DEFINE_RWLOCK(nf_ct_frag6_lock); |
9fb9cbb1 YK |
100 | static u32 nf_ct_frag6_hash_rnd; |
101 | static LIST_HEAD(nf_ct_frag6_lru_list); | |
102 | int nf_ct_frag6_nqueues = 0; | |
103 | ||
104 | static __inline__ void __fq_unlink(struct nf_ct_frag6_queue *fq) | |
105 | { | |
2e4e6a17 | 106 | hlist_del(&fq->list); |
9fb9cbb1 YK |
107 | list_del(&fq->lru_list); |
108 | nf_ct_frag6_nqueues--; | |
109 | } | |
110 | ||
111 | static __inline__ void fq_unlink(struct nf_ct_frag6_queue *fq) | |
112 | { | |
113 | write_lock(&nf_ct_frag6_lock); | |
114 | __fq_unlink(fq); | |
115 | write_unlock(&nf_ct_frag6_lock); | |
116 | } | |
117 | ||
118 | static unsigned int ip6qhashfn(u32 id, struct in6_addr *saddr, | |
119 | struct in6_addr *daddr) | |
120 | { | |
121 | u32 a, b, c; | |
122 | ||
123 | a = saddr->s6_addr32[0]; | |
124 | b = saddr->s6_addr32[1]; | |
125 | c = saddr->s6_addr32[2]; | |
126 | ||
127 | a += JHASH_GOLDEN_RATIO; | |
128 | b += JHASH_GOLDEN_RATIO; | |
129 | c += nf_ct_frag6_hash_rnd; | |
130 | __jhash_mix(a, b, c); | |
131 | ||
132 | a += saddr->s6_addr32[3]; | |
133 | b += daddr->s6_addr32[0]; | |
134 | c += daddr->s6_addr32[1]; | |
135 | __jhash_mix(a, b, c); | |
136 | ||
137 | a += daddr->s6_addr32[2]; | |
138 | b += daddr->s6_addr32[3]; | |
139 | c += id; | |
140 | __jhash_mix(a, b, c); | |
141 | ||
142 | return c & (FRAG6Q_HASHSZ - 1); | |
143 | } | |
144 | ||
145 | static struct timer_list nf_ct_frag6_secret_timer; | |
146 | int nf_ct_frag6_secret_interval = 10 * 60 * HZ; | |
147 | ||
148 | static void nf_ct_frag6_secret_rebuild(unsigned long dummy) | |
149 | { | |
150 | unsigned long now = jiffies; | |
151 | int i; | |
152 | ||
153 | write_lock(&nf_ct_frag6_lock); | |
154 | get_random_bytes(&nf_ct_frag6_hash_rnd, sizeof(u32)); | |
155 | for (i = 0; i < FRAG6Q_HASHSZ; i++) { | |
156 | struct nf_ct_frag6_queue *q; | |
2e4e6a17 | 157 | struct hlist_node *p, *n; |
9fb9cbb1 | 158 | |
2e4e6a17 | 159 | hlist_for_each_entry_safe(q, p, n, &nf_ct_frag6_hash[i], list) { |
9fb9cbb1 YK |
160 | unsigned int hval = ip6qhashfn(q->id, |
161 | &q->saddr, | |
162 | &q->daddr); | |
9fb9cbb1 | 163 | if (hval != i) { |
2e4e6a17 | 164 | hlist_del(&q->list); |
9fb9cbb1 | 165 | /* Relink to new hash chain. */ |
2e4e6a17 HW |
166 | hlist_add_head(&q->list, |
167 | &nf_ct_frag6_hash[hval]); | |
9fb9cbb1 | 168 | } |
9fb9cbb1 YK |
169 | } |
170 | } | |
171 | write_unlock(&nf_ct_frag6_lock); | |
172 | ||
173 | mod_timer(&nf_ct_frag6_secret_timer, now + nf_ct_frag6_secret_interval); | |
174 | } | |
175 | ||
176 | atomic_t nf_ct_frag6_mem = ATOMIC_INIT(0); | |
177 | ||
178 | /* Memory Tracking Functions. */ | |
1ba430bc | 179 | static inline void frag_kfree_skb(struct sk_buff *skb, unsigned int *work) |
9fb9cbb1 | 180 | { |
1ba430bc YK |
181 | if (work) |
182 | *work -= skb->truesize; | |
9fb9cbb1 YK |
183 | atomic_sub(skb->truesize, &nf_ct_frag6_mem); |
184 | if (NFCT_FRAG6_CB(skb)->orig) | |
185 | kfree_skb(NFCT_FRAG6_CB(skb)->orig); | |
186 | ||
187 | kfree_skb(skb); | |
188 | } | |
189 | ||
1ba430bc YK |
190 | static inline void frag_free_queue(struct nf_ct_frag6_queue *fq, |
191 | unsigned int *work) | |
9fb9cbb1 | 192 | { |
1ba430bc YK |
193 | if (work) |
194 | *work -= sizeof(struct nf_ct_frag6_queue); | |
9fb9cbb1 YK |
195 | atomic_sub(sizeof(struct nf_ct_frag6_queue), &nf_ct_frag6_mem); |
196 | kfree(fq); | |
197 | } | |
198 | ||
199 | static inline struct nf_ct_frag6_queue *frag_alloc_queue(void) | |
200 | { | |
201 | struct nf_ct_frag6_queue *fq = kmalloc(sizeof(struct nf_ct_frag6_queue), GFP_ATOMIC); | |
202 | ||
203 | if (!fq) | |
204 | return NULL; | |
205 | atomic_add(sizeof(struct nf_ct_frag6_queue), &nf_ct_frag6_mem); | |
206 | return fq; | |
207 | } | |
208 | ||
209 | /* Destruction primitives. */ | |
210 | ||
211 | /* Complete destruction of fq. */ | |
1ba430bc YK |
212 | static void nf_ct_frag6_destroy(struct nf_ct_frag6_queue *fq, |
213 | unsigned int *work) | |
9fb9cbb1 YK |
214 | { |
215 | struct sk_buff *fp; | |
216 | ||
217 | BUG_TRAP(fq->last_in&COMPLETE); | |
218 | BUG_TRAP(del_timer(&fq->timer) == 0); | |
219 | ||
220 | /* Release all fragment data. */ | |
221 | fp = fq->fragments; | |
222 | while (fp) { | |
223 | struct sk_buff *xp = fp->next; | |
224 | ||
1ba430bc | 225 | frag_kfree_skb(fp, work); |
9fb9cbb1 YK |
226 | fp = xp; |
227 | } | |
228 | ||
1ba430bc | 229 | frag_free_queue(fq, work); |
9fb9cbb1 YK |
230 | } |
231 | ||
1ba430bc | 232 | static __inline__ void fq_put(struct nf_ct_frag6_queue *fq, unsigned int *work) |
9fb9cbb1 YK |
233 | { |
234 | if (atomic_dec_and_test(&fq->refcnt)) | |
1ba430bc | 235 | nf_ct_frag6_destroy(fq, work); |
9fb9cbb1 YK |
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 nf_ct_frag6_queue *fq) | |
242 | { | |
243 | if (del_timer(&fq->timer)) | |
244 | atomic_dec(&fq->refcnt); | |
245 | ||
246 | if (!(fq->last_in & COMPLETE)) { | |
247 | fq_unlink(fq); | |
248 | atomic_dec(&fq->refcnt); | |
249 | fq->last_in |= COMPLETE; | |
250 | } | |
251 | } | |
252 | ||
253 | static void nf_ct_frag6_evictor(void) | |
254 | { | |
255 | struct nf_ct_frag6_queue *fq; | |
256 | struct list_head *tmp; | |
1ba430bc | 257 | unsigned int work; |
9fb9cbb1 | 258 | |
1ba430bc YK |
259 | work = atomic_read(&nf_ct_frag6_mem); |
260 | if (work <= nf_ct_frag6_low_thresh) | |
261 | return; | |
262 | ||
263 | work -= nf_ct_frag6_low_thresh; | |
264 | while (work > 0) { | |
9fb9cbb1 YK |
265 | read_lock(&nf_ct_frag6_lock); |
266 | if (list_empty(&nf_ct_frag6_lru_list)) { | |
267 | read_unlock(&nf_ct_frag6_lock); | |
268 | return; | |
269 | } | |
270 | tmp = nf_ct_frag6_lru_list.next; | |
302fe175 | 271 | BUG_ON(tmp == NULL); |
9fb9cbb1 YK |
272 | fq = list_entry(tmp, struct nf_ct_frag6_queue, lru_list); |
273 | atomic_inc(&fq->refcnt); | |
274 | read_unlock(&nf_ct_frag6_lock); | |
275 | ||
276 | spin_lock(&fq->lock); | |
277 | if (!(fq->last_in&COMPLETE)) | |
278 | fq_kill(fq); | |
279 | spin_unlock(&fq->lock); | |
280 | ||
1ba430bc | 281 | fq_put(fq, &work); |
9fb9cbb1 YK |
282 | } |
283 | } | |
284 | ||
285 | static void nf_ct_frag6_expire(unsigned long data) | |
286 | { | |
287 | struct nf_ct_frag6_queue *fq = (struct nf_ct_frag6_queue *) data; | |
288 | ||
289 | spin_lock(&fq->lock); | |
290 | ||
291 | if (fq->last_in & COMPLETE) | |
292 | goto out; | |
293 | ||
294 | fq_kill(fq); | |
295 | ||
296 | out: | |
297 | spin_unlock(&fq->lock); | |
1ba430bc | 298 | fq_put(fq, NULL); |
9fb9cbb1 YK |
299 | } |
300 | ||
301 | /* Creation primitives. */ | |
302 | ||
9fb9cbb1 YK |
303 | static struct nf_ct_frag6_queue *nf_ct_frag6_intern(unsigned int hash, |
304 | struct nf_ct_frag6_queue *fq_in) | |
305 | { | |
306 | struct nf_ct_frag6_queue *fq; | |
2e4e6a17 HW |
307 | #ifdef CONFIG_SMP |
308 | struct hlist_node *n; | |
309 | #endif | |
9fb9cbb1 YK |
310 | |
311 | write_lock(&nf_ct_frag6_lock); | |
312 | #ifdef CONFIG_SMP | |
2e4e6a17 | 313 | hlist_for_each_entry(fq, n, &nf_ct_frag6_hash[hash], list) { |
9fb9cbb1 | 314 | if (fq->id == fq_in->id && |
6ea46c9c YK |
315 | ipv6_addr_equal(&fq_in->saddr, &fq->saddr) && |
316 | ipv6_addr_equal(&fq_in->daddr, &fq->daddr)) { | |
9fb9cbb1 YK |
317 | atomic_inc(&fq->refcnt); |
318 | write_unlock(&nf_ct_frag6_lock); | |
319 | fq_in->last_in |= COMPLETE; | |
1ba430bc | 320 | fq_put(fq_in, NULL); |
9fb9cbb1 YK |
321 | return fq; |
322 | } | |
323 | } | |
324 | #endif | |
325 | fq = fq_in; | |
326 | ||
327 | if (!mod_timer(&fq->timer, jiffies + nf_ct_frag6_timeout)) | |
328 | atomic_inc(&fq->refcnt); | |
329 | ||
330 | atomic_inc(&fq->refcnt); | |
2e4e6a17 | 331 | hlist_add_head(&fq->list, &nf_ct_frag6_hash[hash]); |
9fb9cbb1 YK |
332 | INIT_LIST_HEAD(&fq->lru_list); |
333 | list_add_tail(&fq->lru_list, &nf_ct_frag6_lru_list); | |
334 | nf_ct_frag6_nqueues++; | |
335 | write_unlock(&nf_ct_frag6_lock); | |
336 | return fq; | |
337 | } | |
338 | ||
339 | ||
340 | static struct nf_ct_frag6_queue * | |
341 | nf_ct_frag6_create(unsigned int hash, u32 id, struct in6_addr *src, struct in6_addr *dst) | |
342 | { | |
343 | struct nf_ct_frag6_queue *fq; | |
344 | ||
345 | if ((fq = frag_alloc_queue()) == NULL) { | |
346 | DEBUGP("Can't alloc new queue\n"); | |
347 | goto oom; | |
348 | } | |
349 | ||
350 | memset(fq, 0, sizeof(struct nf_ct_frag6_queue)); | |
351 | ||
352 | fq->id = id; | |
353 | ipv6_addr_copy(&fq->saddr, src); | |
354 | ipv6_addr_copy(&fq->daddr, dst); | |
355 | ||
356 | init_timer(&fq->timer); | |
357 | fq->timer.function = nf_ct_frag6_expire; | |
358 | fq->timer.data = (long) fq; | |
181a46a5 | 359 | spin_lock_init(&fq->lock); |
9fb9cbb1 YK |
360 | atomic_set(&fq->refcnt, 1); |
361 | ||
362 | return nf_ct_frag6_intern(hash, fq); | |
363 | ||
364 | oom: | |
365 | return NULL; | |
366 | } | |
367 | ||
368 | static __inline__ struct nf_ct_frag6_queue * | |
369 | fq_find(u32 id, struct in6_addr *src, struct in6_addr *dst) | |
370 | { | |
371 | struct nf_ct_frag6_queue *fq; | |
2e4e6a17 | 372 | struct hlist_node *n; |
9fb9cbb1 YK |
373 | unsigned int hash = ip6qhashfn(id, src, dst); |
374 | ||
375 | read_lock(&nf_ct_frag6_lock); | |
2e4e6a17 | 376 | hlist_for_each_entry(fq, n, &nf_ct_frag6_hash[hash], list) { |
9fb9cbb1 | 377 | if (fq->id == id && |
6ea46c9c YK |
378 | ipv6_addr_equal(src, &fq->saddr) && |
379 | ipv6_addr_equal(dst, &fq->daddr)) { | |
9fb9cbb1 YK |
380 | atomic_inc(&fq->refcnt); |
381 | read_unlock(&nf_ct_frag6_lock); | |
382 | return fq; | |
383 | } | |
384 | } | |
385 | read_unlock(&nf_ct_frag6_lock); | |
386 | ||
387 | return nf_ct_frag6_create(hash, id, src, dst); | |
388 | } | |
389 | ||
390 | ||
391 | static int nf_ct_frag6_queue(struct nf_ct_frag6_queue *fq, struct sk_buff *skb, | |
392 | struct frag_hdr *fhdr, int nhoff) | |
393 | { | |
394 | struct sk_buff *prev, *next; | |
395 | int offset, end; | |
396 | ||
397 | if (fq->last_in & COMPLETE) { | |
398 | DEBUGP("Allready completed\n"); | |
399 | goto err; | |
400 | } | |
401 | ||
402 | offset = ntohs(fhdr->frag_off) & ~0x7; | |
403 | end = offset + (ntohs(skb->nh.ipv6h->payload_len) - | |
404 | ((u8 *) (fhdr + 1) - (u8 *) (skb->nh.ipv6h + 1))); | |
405 | ||
406 | if ((unsigned int)end > IPV6_MAXPLEN) { | |
407 | DEBUGP("offset is too large.\n"); | |
408 | return -1; | |
409 | } | |
410 | ||
84fa7933 | 411 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
9fb9cbb1 YK |
412 | skb->csum = csum_sub(skb->csum, |
413 | csum_partial(skb->nh.raw, | |
414 | (u8*)(fhdr + 1) - skb->nh.raw, | |
415 | 0)); | |
416 | ||
417 | /* Is this the final fragment? */ | |
418 | if (!(fhdr->frag_off & htons(IP6_MF))) { | |
419 | /* If we already have some bits beyond end | |
420 | * or have different end, the segment is corrupted. | |
421 | */ | |
422 | if (end < fq->len || | |
423 | ((fq->last_in & LAST_IN) && end != fq->len)) { | |
424 | DEBUGP("already received last fragment\n"); | |
425 | goto err; | |
426 | } | |
427 | fq->last_in |= LAST_IN; | |
428 | fq->len = end; | |
429 | } else { | |
430 | /* Check if the fragment is rounded to 8 bytes. | |
431 | * Required by the RFC. | |
432 | */ | |
433 | if (end & 0x7) { | |
434 | /* RFC2460 says always send parameter problem in | |
435 | * this case. -DaveM | |
436 | */ | |
437 | DEBUGP("the end of this fragment is not rounded to 8 bytes.\n"); | |
438 | return -1; | |
439 | } | |
440 | if (end > fq->len) { | |
441 | /* Some bits beyond end -> corruption. */ | |
442 | if (fq->last_in & LAST_IN) { | |
443 | DEBUGP("last packet already reached.\n"); | |
444 | goto err; | |
445 | } | |
446 | fq->len = end; | |
447 | } | |
448 | } | |
449 | ||
450 | if (end == offset) | |
451 | goto err; | |
452 | ||
453 | /* Point into the IP datagram 'data' part. */ | |
454 | if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) { | |
455 | DEBUGP("queue: message is too short.\n"); | |
456 | goto err; | |
457 | } | |
b38dfee3 HX |
458 | if (pskb_trim_rcsum(skb, end - offset)) { |
459 | DEBUGP("Can't trim\n"); | |
460 | goto err; | |
9fb9cbb1 YK |
461 | } |
462 | ||
463 | /* Find out which fragments are in front and at the back of us | |
464 | * in the chain of fragments so far. We must know where to put | |
465 | * this fragment, right? | |
466 | */ | |
467 | prev = NULL; | |
468 | for (next = fq->fragments; next != NULL; next = next->next) { | |
469 | if (NFCT_FRAG6_CB(next)->offset >= offset) | |
470 | break; /* bingo! */ | |
471 | prev = next; | |
472 | } | |
473 | ||
474 | /* We found where to put this one. Check for overlap with | |
475 | * preceding fragment, and, if needed, align things so that | |
476 | * any overlaps are eliminated. | |
477 | */ | |
478 | if (prev) { | |
479 | int i = (NFCT_FRAG6_CB(prev)->offset + prev->len) - offset; | |
480 | ||
481 | if (i > 0) { | |
482 | offset += i; | |
483 | if (end <= offset) { | |
484 | DEBUGP("overlap\n"); | |
485 | goto err; | |
486 | } | |
487 | if (!pskb_pull(skb, i)) { | |
488 | DEBUGP("Can't pull\n"); | |
489 | goto err; | |
490 | } | |
491 | if (skb->ip_summed != CHECKSUM_UNNECESSARY) | |
492 | skb->ip_summed = CHECKSUM_NONE; | |
493 | } | |
494 | } | |
495 | ||
496 | /* Look for overlap with succeeding segments. | |
497 | * If we can merge fragments, do it. | |
498 | */ | |
499 | while (next && NFCT_FRAG6_CB(next)->offset < end) { | |
500 | /* overlap is 'i' bytes */ | |
501 | int i = end - NFCT_FRAG6_CB(next)->offset; | |
502 | ||
503 | if (i < next->len) { | |
504 | /* Eat head of the next overlapped fragment | |
505 | * and leave the loop. The next ones cannot overlap. | |
506 | */ | |
507 | DEBUGP("Eat head of the overlapped parts.: %d", i); | |
508 | if (!pskb_pull(next, i)) | |
509 | goto err; | |
510 | ||
511 | /* next fragment */ | |
512 | NFCT_FRAG6_CB(next)->offset += i; | |
513 | fq->meat -= i; | |
514 | if (next->ip_summed != CHECKSUM_UNNECESSARY) | |
515 | next->ip_summed = CHECKSUM_NONE; | |
516 | break; | |
517 | } else { | |
518 | struct sk_buff *free_it = next; | |
519 | ||
520 | /* Old fragmnet is completely overridden with | |
521 | * new one drop it. | |
522 | */ | |
523 | next = next->next; | |
524 | ||
525 | if (prev) | |
526 | prev->next = next; | |
527 | else | |
528 | fq->fragments = next; | |
529 | ||
530 | fq->meat -= free_it->len; | |
1ba430bc | 531 | frag_kfree_skb(free_it, NULL); |
9fb9cbb1 YK |
532 | } |
533 | } | |
534 | ||
535 | NFCT_FRAG6_CB(skb)->offset = offset; | |
536 | ||
537 | /* Insert this fragment in the chain of fragments. */ | |
538 | skb->next = next; | |
539 | if (prev) | |
540 | prev->next = skb; | |
541 | else | |
542 | fq->fragments = skb; | |
543 | ||
544 | skb->dev = NULL; | |
545 | skb_get_timestamp(skb, &fq->stamp); | |
546 | fq->meat += skb->len; | |
547 | atomic_add(skb->truesize, &nf_ct_frag6_mem); | |
548 | ||
549 | /* The first fragment. | |
550 | * nhoffset is obtained from the first fragment, of course. | |
551 | */ | |
552 | if (offset == 0) { | |
553 | fq->nhoffset = nhoff; | |
554 | fq->last_in |= FIRST_IN; | |
555 | } | |
556 | write_lock(&nf_ct_frag6_lock); | |
557 | list_move_tail(&fq->lru_list, &nf_ct_frag6_lru_list); | |
558 | write_unlock(&nf_ct_frag6_lock); | |
559 | return 0; | |
560 | ||
561 | err: | |
562 | return -1; | |
563 | } | |
564 | ||
565 | /* | |
566 | * Check if this packet is complete. | |
567 | * Returns NULL on failure by any reason, and pointer | |
568 | * to current nexthdr field in reassembled frame. | |
569 | * | |
570 | * It is called with locked fq, and caller must check that | |
571 | * queue is eligible for reassembly i.e. it is not COMPLETE, | |
572 | * the last and the first frames arrived and all the bits are here. | |
573 | */ | |
574 | static struct sk_buff * | |
575 | nf_ct_frag6_reasm(struct nf_ct_frag6_queue *fq, struct net_device *dev) | |
576 | { | |
577 | struct sk_buff *fp, *op, *head = fq->fragments; | |
578 | int payload_len; | |
579 | ||
580 | fq_kill(fq); | |
581 | ||
582 | BUG_TRAP(head != NULL); | |
583 | BUG_TRAP(NFCT_FRAG6_CB(head)->offset == 0); | |
584 | ||
585 | /* Unfragmented part is taken from the first segment. */ | |
586 | payload_len = (head->data - head->nh.raw) - sizeof(struct ipv6hdr) + fq->len - sizeof(struct frag_hdr); | |
587 | if (payload_len > IPV6_MAXPLEN) { | |
588 | DEBUGP("payload len is too large.\n"); | |
589 | goto out_oversize; | |
590 | } | |
591 | ||
592 | /* Head of list must not be cloned. */ | |
593 | if (skb_cloned(head) && pskb_expand_head(head, 0, 0, GFP_ATOMIC)) { | |
594 | DEBUGP("skb is cloned but can't expand head"); | |
595 | goto out_oom; | |
596 | } | |
597 | ||
598 | /* If the first fragment is fragmented itself, we split | |
599 | * it to two chunks: the first with data and paged part | |
600 | * and the second, holding only fragments. */ | |
601 | if (skb_shinfo(head)->frag_list) { | |
602 | struct sk_buff *clone; | |
603 | int i, plen = 0; | |
604 | ||
605 | if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL) { | |
606 | DEBUGP("Can't alloc skb\n"); | |
607 | goto out_oom; | |
608 | } | |
609 | clone->next = head->next; | |
610 | head->next = clone; | |
611 | skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list; | |
612 | skb_shinfo(head)->frag_list = NULL; | |
613 | for (i=0; i<skb_shinfo(head)->nr_frags; i++) | |
614 | plen += skb_shinfo(head)->frags[i].size; | |
615 | clone->len = clone->data_len = head->data_len - plen; | |
616 | head->data_len -= clone->len; | |
617 | head->len -= clone->len; | |
618 | clone->csum = 0; | |
619 | clone->ip_summed = head->ip_summed; | |
620 | ||
621 | NFCT_FRAG6_CB(clone)->orig = NULL; | |
622 | atomic_add(clone->truesize, &nf_ct_frag6_mem); | |
623 | } | |
624 | ||
625 | /* We have to remove fragment header from datagram and to relocate | |
626 | * header in order to calculate ICV correctly. */ | |
627 | head->nh.raw[fq->nhoffset] = head->h.raw[0]; | |
628 | memmove(head->head + sizeof(struct frag_hdr), head->head, | |
629 | (head->data - head->head) - sizeof(struct frag_hdr)); | |
630 | head->mac.raw += sizeof(struct frag_hdr); | |
631 | head->nh.raw += sizeof(struct frag_hdr); | |
632 | ||
633 | skb_shinfo(head)->frag_list = head->next; | |
634 | head->h.raw = head->data; | |
635 | skb_push(head, head->data - head->nh.raw); | |
636 | atomic_sub(head->truesize, &nf_ct_frag6_mem); | |
637 | ||
638 | for (fp=head->next; fp; fp = fp->next) { | |
639 | head->data_len += fp->len; | |
640 | head->len += fp->len; | |
641 | if (head->ip_summed != fp->ip_summed) | |
642 | head->ip_summed = CHECKSUM_NONE; | |
84fa7933 | 643 | else if (head->ip_summed == CHECKSUM_COMPLETE) |
9fb9cbb1 YK |
644 | head->csum = csum_add(head->csum, fp->csum); |
645 | head->truesize += fp->truesize; | |
646 | atomic_sub(fp->truesize, &nf_ct_frag6_mem); | |
647 | } | |
648 | ||
649 | head->next = NULL; | |
650 | head->dev = dev; | |
651 | skb_set_timestamp(head, &fq->stamp); | |
652 | head->nh.ipv6h->payload_len = htons(payload_len); | |
653 | ||
654 | /* Yes, and fold redundant checksum back. 8) */ | |
84fa7933 | 655 | if (head->ip_summed == CHECKSUM_COMPLETE) |
9fb9cbb1 YK |
656 | head->csum = csum_partial(head->nh.raw, head->h.raw-head->nh.raw, head->csum); |
657 | ||
658 | fq->fragments = NULL; | |
659 | ||
660 | /* all original skbs are linked into the NFCT_FRAG6_CB(head).orig */ | |
661 | fp = skb_shinfo(head)->frag_list; | |
662 | if (NFCT_FRAG6_CB(fp)->orig == NULL) | |
663 | /* at above code, head skb is divided into two skbs. */ | |
664 | fp = fp->next; | |
665 | ||
666 | op = NFCT_FRAG6_CB(head)->orig; | |
667 | for (; fp; fp = fp->next) { | |
668 | struct sk_buff *orig = NFCT_FRAG6_CB(fp)->orig; | |
669 | ||
670 | op->next = orig; | |
671 | op = orig; | |
672 | NFCT_FRAG6_CB(fp)->orig = NULL; | |
673 | } | |
674 | ||
675 | return head; | |
676 | ||
677 | out_oversize: | |
678 | if (net_ratelimit()) | |
679 | printk(KERN_DEBUG "nf_ct_frag6_reasm: payload len = %d\n", payload_len); | |
680 | goto out_fail; | |
681 | out_oom: | |
682 | if (net_ratelimit()) | |
683 | printk(KERN_DEBUG "nf_ct_frag6_reasm: no memory for reassembly\n"); | |
684 | out_fail: | |
685 | return NULL; | |
686 | } | |
687 | ||
688 | /* | |
689 | * find the header just before Fragment Header. | |
690 | * | |
691 | * if success return 0 and set ... | |
692 | * (*prevhdrp): the value of "Next Header Field" in the header | |
693 | * just before Fragment Header. | |
694 | * (*prevhoff): the offset of "Next Header Field" in the header | |
695 | * just before Fragment Header. | |
696 | * (*fhoff) : the offset of Fragment Header. | |
697 | * | |
698 | * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c | |
699 | * | |
700 | */ | |
701 | static int | |
702 | find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff) | |
703 | { | |
704 | u8 nexthdr = skb->nh.ipv6h->nexthdr; | |
705 | u8 prev_nhoff = (u8 *)&skb->nh.ipv6h->nexthdr - skb->data; | |
706 | int start = (u8 *)(skb->nh.ipv6h+1) - skb->data; | |
707 | int len = skb->len - start; | |
708 | u8 prevhdr = NEXTHDR_IPV6; | |
709 | ||
710 | while (nexthdr != NEXTHDR_FRAGMENT) { | |
711 | struct ipv6_opt_hdr hdr; | |
712 | int hdrlen; | |
713 | ||
714 | if (!ipv6_ext_hdr(nexthdr)) { | |
715 | return -1; | |
716 | } | |
717 | if (len < (int)sizeof(struct ipv6_opt_hdr)) { | |
718 | DEBUGP("too short\n"); | |
719 | return -1; | |
720 | } | |
721 | if (nexthdr == NEXTHDR_NONE) { | |
722 | DEBUGP("next header is none\n"); | |
723 | return -1; | |
724 | } | |
725 | if (skb_copy_bits(skb, start, &hdr, sizeof(hdr))) | |
726 | BUG(); | |
727 | if (nexthdr == NEXTHDR_AUTH) | |
728 | hdrlen = (hdr.hdrlen+2)<<2; | |
729 | else | |
730 | hdrlen = ipv6_optlen(&hdr); | |
731 | ||
732 | prevhdr = nexthdr; | |
733 | prev_nhoff = start; | |
734 | ||
735 | nexthdr = hdr.nexthdr; | |
736 | len -= hdrlen; | |
737 | start += hdrlen; | |
738 | } | |
739 | ||
740 | if (len < 0) | |
741 | return -1; | |
742 | ||
743 | *prevhdrp = prevhdr; | |
744 | *prevhoff = prev_nhoff; | |
745 | *fhoff = start; | |
746 | ||
747 | return 0; | |
748 | } | |
749 | ||
750 | struct sk_buff *nf_ct_frag6_gather(struct sk_buff *skb) | |
751 | { | |
752 | struct sk_buff *clone; | |
753 | struct net_device *dev = skb->dev; | |
754 | struct frag_hdr *fhdr; | |
755 | struct nf_ct_frag6_queue *fq; | |
756 | struct ipv6hdr *hdr; | |
757 | int fhoff, nhoff; | |
758 | u8 prevhdr; | |
759 | struct sk_buff *ret_skb = NULL; | |
760 | ||
761 | /* Jumbo payload inhibits frag. header */ | |
762 | if (skb->nh.ipv6h->payload_len == 0) { | |
763 | DEBUGP("payload len = 0\n"); | |
764 | return skb; | |
765 | } | |
766 | ||
767 | if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0) | |
768 | return skb; | |
769 | ||
770 | clone = skb_clone(skb, GFP_ATOMIC); | |
771 | if (clone == NULL) { | |
772 | DEBUGP("Can't clone skb\n"); | |
773 | return skb; | |
774 | } | |
775 | ||
776 | NFCT_FRAG6_CB(clone)->orig = skb; | |
777 | ||
778 | if (!pskb_may_pull(clone, fhoff + sizeof(*fhdr))) { | |
779 | DEBUGP("message is too short.\n"); | |
780 | goto ret_orig; | |
781 | } | |
782 | ||
783 | clone->h.raw = clone->data + fhoff; | |
784 | hdr = clone->nh.ipv6h; | |
785 | fhdr = (struct frag_hdr *)clone->h.raw; | |
786 | ||
787 | if (!(fhdr->frag_off & htons(0xFFF9))) { | |
788 | DEBUGP("Invalid fragment offset\n"); | |
789 | /* It is not a fragmented frame */ | |
790 | goto ret_orig; | |
791 | } | |
792 | ||
793 | if (atomic_read(&nf_ct_frag6_mem) > nf_ct_frag6_high_thresh) | |
794 | nf_ct_frag6_evictor(); | |
795 | ||
796 | fq = fq_find(fhdr->identification, &hdr->saddr, &hdr->daddr); | |
797 | if (fq == NULL) { | |
798 | DEBUGP("Can't find and can't create new queue\n"); | |
799 | goto ret_orig; | |
800 | } | |
801 | ||
802 | spin_lock(&fq->lock); | |
803 | ||
804 | if (nf_ct_frag6_queue(fq, clone, fhdr, nhoff) < 0) { | |
805 | spin_unlock(&fq->lock); | |
806 | DEBUGP("Can't insert skb to queue\n"); | |
1ba430bc | 807 | fq_put(fq, NULL); |
9fb9cbb1 YK |
808 | goto ret_orig; |
809 | } | |
810 | ||
811 | if (fq->last_in == (FIRST_IN|LAST_IN) && fq->meat == fq->len) { | |
812 | ret_skb = nf_ct_frag6_reasm(fq, dev); | |
813 | if (ret_skb == NULL) | |
814 | DEBUGP("Can't reassemble fragmented packets\n"); | |
815 | } | |
816 | spin_unlock(&fq->lock); | |
817 | ||
1ba430bc | 818 | fq_put(fq, NULL); |
9fb9cbb1 YK |
819 | return ret_skb; |
820 | ||
821 | ret_orig: | |
822 | kfree_skb(clone); | |
823 | return skb; | |
824 | } | |
825 | ||
826 | void nf_ct_frag6_output(unsigned int hooknum, struct sk_buff *skb, | |
827 | struct net_device *in, struct net_device *out, | |
828 | int (*okfn)(struct sk_buff *)) | |
829 | { | |
830 | struct sk_buff *s, *s2; | |
831 | ||
832 | for (s = NFCT_FRAG6_CB(skb)->orig; s;) { | |
833 | nf_conntrack_put_reasm(s->nfct_reasm); | |
834 | nf_conntrack_get_reasm(skb); | |
835 | s->nfct_reasm = skb; | |
836 | ||
837 | s2 = s->next; | |
838 | NF_HOOK_THRESH(PF_INET6, hooknum, s, in, out, okfn, | |
839 | NF_IP6_PRI_CONNTRACK_DEFRAG + 1); | |
840 | s = s2; | |
841 | } | |
842 | nf_conntrack_put_reasm(skb); | |
843 | } | |
844 | ||
845 | int nf_ct_frag6_kfree_frags(struct sk_buff *skb) | |
846 | { | |
847 | struct sk_buff *s, *s2; | |
848 | ||
849 | for (s = NFCT_FRAG6_CB(skb)->orig; s; s = s2) { | |
850 | ||
851 | s2 = s->next; | |
852 | kfree_skb(s); | |
853 | } | |
854 | ||
855 | kfree_skb(skb); | |
856 | ||
857 | return 0; | |
858 | } | |
859 | ||
860 | int nf_ct_frag6_init(void) | |
861 | { | |
862 | nf_ct_frag6_hash_rnd = (u32) ((num_physpages ^ (num_physpages>>7)) ^ | |
863 | (jiffies ^ (jiffies >> 6))); | |
864 | ||
865 | init_timer(&nf_ct_frag6_secret_timer); | |
866 | nf_ct_frag6_secret_timer.function = nf_ct_frag6_secret_rebuild; | |
867 | nf_ct_frag6_secret_timer.expires = jiffies | |
868 | + nf_ct_frag6_secret_interval; | |
869 | add_timer(&nf_ct_frag6_secret_timer); | |
870 | ||
871 | return 0; | |
872 | } | |
873 | ||
874 | void nf_ct_frag6_cleanup(void) | |
875 | { | |
876 | del_timer(&nf_ct_frag6_secret_timer); | |
302fe175 | 877 | nf_ct_frag6_low_thresh = 0; |
9fb9cbb1 YK |
878 | nf_ct_frag6_evictor(); |
879 | } |