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