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