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