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