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