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