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 | ||
5a3da1fe HFS |
17 | #define pr_fmt(fmt) "IPv6-nf: " fmt |
18 | ||
9fb9cbb1 YK |
19 | #include <linux/errno.h> |
20 | #include <linux/types.h> | |
21 | #include <linux/string.h> | |
22 | #include <linux/socket.h> | |
23 | #include <linux/sockios.h> | |
24 | #include <linux/jiffies.h> | |
25 | #include <linux/net.h> | |
26 | #include <linux/list.h> | |
27 | #include <linux/netdevice.h> | |
28 | #include <linux/in6.h> | |
29 | #include <linux/ipv6.h> | |
30 | #include <linux/icmpv6.h> | |
31 | #include <linux/random.h> | |
5a0e3ad6 | 32 | #include <linux/slab.h> |
9fb9cbb1 YK |
33 | |
34 | #include <net/sock.h> | |
35 | #include <net/snmp.h> | |
5ab11c98 | 36 | #include <net/inet_frag.h> |
9fb9cbb1 YK |
37 | |
38 | #include <net/ipv6.h> | |
39 | #include <net/protocol.h> | |
40 | #include <net/transp_v6.h> | |
41 | #include <net/rawv6.h> | |
42 | #include <net/ndisc.h> | |
43 | #include <net/addrconf.h> | |
b8dd6a22 | 44 | #include <net/inet_ecn.h> |
99fa5f53 | 45 | #include <net/netfilter/ipv6/nf_conntrack_ipv6.h> |
9fb9cbb1 YK |
46 | #include <linux/sysctl.h> |
47 | #include <linux/netfilter.h> | |
48 | #include <linux/netfilter_ipv6.h> | |
49 | #include <linux/kernel.h> | |
50 | #include <linux/module.h> | |
bced94ed | 51 | #include <net/netfilter/ipv6/nf_defrag_ipv6.h> |
9fb9cbb1 | 52 | |
9fb9cbb1 | 53 | |
9fb9cbb1 YK |
54 | struct nf_ct_frag6_skb_cb |
55 | { | |
56 | struct inet6_skb_parm h; | |
57 | int offset; | |
58 | struct sk_buff *orig; | |
59 | }; | |
60 | ||
61 | #define NFCT_FRAG6_CB(skb) ((struct nf_ct_frag6_skb_cb*)((skb)->cb)) | |
62 | ||
7eb95156 | 63 | static struct inet_frags nf_frags; |
9fb9cbb1 | 64 | |
8d8354d2 | 65 | #ifdef CONFIG_SYSCTL |
be9e9163 | 66 | static struct ctl_table nf_ct_frag6_sysctl_table[] = { |
8d8354d2 PE |
67 | { |
68 | .procname = "nf_conntrack_frag6_timeout", | |
c038a767 | 69 | .data = &init_net.nf_frag.frags.timeout, |
8d8354d2 PE |
70 | .maxlen = sizeof(unsigned int), |
71 | .mode = 0644, | |
6d9f239a | 72 | .proc_handler = proc_dointvec_jiffies, |
8d8354d2 PE |
73 | }, |
74 | { | |
8d8354d2 | 75 | .procname = "nf_conntrack_frag6_low_thresh", |
c038a767 | 76 | .data = &init_net.nf_frag.frags.low_thresh, |
8d8354d2 PE |
77 | .maxlen = sizeof(unsigned int), |
78 | .mode = 0644, | |
6d9f239a | 79 | .proc_handler = proc_dointvec, |
8d8354d2 PE |
80 | }, |
81 | { | |
8d8354d2 | 82 | .procname = "nf_conntrack_frag6_high_thresh", |
c038a767 | 83 | .data = &init_net.nf_frag.frags.high_thresh, |
8d8354d2 PE |
84 | .maxlen = sizeof(unsigned int), |
85 | .mode = 0644, | |
6d9f239a | 86 | .proc_handler = proc_dointvec, |
8d8354d2 | 87 | }, |
f8572d8f | 88 | { } |
8d8354d2 | 89 | }; |
e97c3e27 | 90 | |
f1df1374 | 91 | static int nf_ct_frag6_sysctl_register(struct net *net) |
c038a767 AW |
92 | { |
93 | struct ctl_table *table; | |
94 | struct ctl_table_header *hdr; | |
95 | ||
96 | table = nf_ct_frag6_sysctl_table; | |
97 | if (!net_eq(net, &init_net)) { | |
98 | table = kmemdup(table, sizeof(nf_ct_frag6_sysctl_table), | |
99 | GFP_KERNEL); | |
100 | if (table == NULL) | |
101 | goto err_alloc; | |
102 | ||
894e2ac8 MK |
103 | table[0].data = &net->nf_frag.frags.timeout; |
104 | table[1].data = &net->nf_frag.frags.low_thresh; | |
105 | table[2].data = &net->nf_frag.frags.high_thresh; | |
c038a767 AW |
106 | } |
107 | ||
108 | hdr = register_net_sysctl(net, "net/netfilter", table); | |
109 | if (hdr == NULL) | |
110 | goto err_reg; | |
111 | ||
4b7cc7fc | 112 | net->nf_frag.sysctl.frags_hdr = hdr; |
c038a767 AW |
113 | return 0; |
114 | ||
115 | err_reg: | |
116 | if (!net_eq(net, &init_net)) | |
117 | kfree(table); | |
118 | err_alloc: | |
119 | return -ENOMEM; | |
120 | } | |
121 | ||
122 | static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net) | |
123 | { | |
124 | struct ctl_table *table; | |
125 | ||
126 | table = net->nf_frag.sysctl.frags_hdr->ctl_table_arg; | |
127 | unregister_net_sysctl_table(net->nf_frag.sysctl.frags_hdr); | |
128 | if (!net_eq(net, &init_net)) | |
129 | kfree(table); | |
130 | } | |
131 | ||
132 | #else | |
f1df1374 | 133 | static int nf_ct_frag6_sysctl_register(struct net *net) |
c038a767 AW |
134 | { |
135 | return 0; | |
136 | } | |
137 | static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net) | |
138 | { | |
139 | } | |
8d8354d2 PE |
140 | #endif |
141 | ||
b8dd6a22 HFS |
142 | static inline u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h) |
143 | { | |
144 | return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK); | |
145 | } | |
146 | ||
b1190570 HFS |
147 | static unsigned int nf_hash_frag(__be32 id, const struct in6_addr *saddr, |
148 | const struct in6_addr *daddr) | |
149 | { | |
b1190570 | 150 | net_get_random_once(&nf_frags.rnd, sizeof(nf_frags.rnd)); |
fb3cfe6e FW |
151 | return jhash_3words(ipv6_addr_hash(saddr), ipv6_addr_hash(daddr), |
152 | (__force u32)id, nf_frags.rnd); | |
b1190570 HFS |
153 | } |
154 | ||
155 | ||
36c77782 | 156 | static unsigned int nf_hashfn(const struct inet_frag_queue *q) |
9fb9cbb1 | 157 | { |
b836c99f | 158 | const struct frag_queue *nq; |
9fb9cbb1 | 159 | |
b836c99f | 160 | nq = container_of(q, struct frag_queue, q); |
b1190570 | 161 | return nf_hash_frag(nq->id, &nq->saddr, &nq->daddr); |
9fb9cbb1 YK |
162 | } |
163 | ||
1e4b8287 PE |
164 | static void nf_skb_free(struct sk_buff *skb) |
165 | { | |
166 | if (NFCT_FRAG6_CB(skb)->orig) | |
167 | kfree_skb(NFCT_FRAG6_CB(skb)->orig); | |
168 | } | |
169 | ||
9fb9cbb1 YK |
170 | static void nf_ct_frag6_expire(unsigned long data) |
171 | { | |
b836c99f AW |
172 | struct frag_queue *fq; |
173 | struct net *net; | |
9fb9cbb1 | 174 | |
b836c99f AW |
175 | fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q); |
176 | net = container_of(fq->q.net, struct net, nf_frag.frags); | |
9fb9cbb1 | 177 | |
b836c99f | 178 | ip6_expire_frag_queue(net, fq, &nf_frags); |
9fb9cbb1 YK |
179 | } |
180 | ||
181 | /* Creation primitives. */ | |
b836c99f AW |
182 | static inline struct frag_queue *fq_find(struct net *net, __be32 id, |
183 | u32 user, struct in6_addr *src, | |
b8dd6a22 | 184 | struct in6_addr *dst, u8 ecn) |
9fb9cbb1 | 185 | { |
2588fe1d | 186 | struct inet_frag_queue *q; |
c6fda282 | 187 | struct ip6_create_arg arg; |
abd6523d | 188 | unsigned int hash; |
9fb9cbb1 | 189 | |
c6fda282 | 190 | arg.id = id; |
0b5ccb2e | 191 | arg.user = user; |
c6fda282 PE |
192 | arg.src = src; |
193 | arg.dst = dst; | |
b8dd6a22 | 194 | arg.ecn = ecn; |
9a375803 | 195 | |
ab1c724f | 196 | local_bh_disable(); |
b1190570 | 197 | hash = nf_hash_frag(id, src, dst); |
9fb9cbb1 | 198 | |
c038a767 | 199 | q = inet_frag_find(&net->nf_frag.frags, &nf_frags, &arg, hash); |
b9c69896 | 200 | local_bh_enable(); |
5a3da1fe HFS |
201 | if (IS_ERR_OR_NULL(q)) { |
202 | inet_frag_maybe_warn_overflow(q, pr_fmt()); | |
203 | return NULL; | |
204 | } | |
b836c99f | 205 | return container_of(q, struct frag_queue, q); |
9fb9cbb1 YK |
206 | } |
207 | ||
9fb9cbb1 | 208 | |
b836c99f | 209 | static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb, |
58c0fb0d | 210 | const struct frag_hdr *fhdr, int nhoff) |
9fb9cbb1 YK |
211 | { |
212 | struct sk_buff *prev, *next; | |
4cdd3408 | 213 | unsigned int payload_len; |
9fb9cbb1 | 214 | int offset, end; |
b8dd6a22 | 215 | u8 ecn; |
9fb9cbb1 | 216 | |
bc578a54 | 217 | if (fq->q.last_in & INET_FRAG_COMPLETE) { |
698f9315 | 218 | pr_debug("Already completed\n"); |
9fb9cbb1 YK |
219 | goto err; |
220 | } | |
221 | ||
4cdd3408 PM |
222 | payload_len = ntohs(ipv6_hdr(skb)->payload_len); |
223 | ||
9fb9cbb1 | 224 | offset = ntohs(fhdr->frag_off) & ~0x7; |
4cdd3408 | 225 | end = offset + (payload_len - |
0660e03f | 226 | ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1))); |
9fb9cbb1 YK |
227 | |
228 | if ((unsigned int)end > IPV6_MAXPLEN) { | |
0d53778e | 229 | pr_debug("offset is too large.\n"); |
1ab1457c | 230 | return -1; |
9fb9cbb1 YK |
231 | } |
232 | ||
b8dd6a22 HFS |
233 | ecn = ip6_frag_ecn(ipv6_hdr(skb)); |
234 | ||
d56f90a7 ACM |
235 | if (skb->ip_summed == CHECKSUM_COMPLETE) { |
236 | const unsigned char *nh = skb_network_header(skb); | |
1ab1457c | 237 | skb->csum = csum_sub(skb->csum, |
d56f90a7 | 238 | csum_partial(nh, (u8 *)(fhdr + 1) - nh, |
9fb9cbb1 | 239 | 0)); |
d56f90a7 | 240 | } |
9fb9cbb1 YK |
241 | |
242 | /* Is this the final fragment? */ | |
243 | if (!(fhdr->frag_off & htons(IP6_MF))) { | |
244 | /* If we already have some bits beyond end | |
245 | * or have different end, the segment is corrupted. | |
246 | */ | |
5ab11c98 | 247 | if (end < fq->q.len || |
bc578a54 | 248 | ((fq->q.last_in & INET_FRAG_LAST_IN) && end != fq->q.len)) { |
0d53778e | 249 | pr_debug("already received last fragment\n"); |
9fb9cbb1 YK |
250 | goto err; |
251 | } | |
bc578a54 | 252 | fq->q.last_in |= INET_FRAG_LAST_IN; |
5ab11c98 | 253 | fq->q.len = end; |
9fb9cbb1 YK |
254 | } else { |
255 | /* Check if the fragment is rounded to 8 bytes. | |
256 | * Required by the RFC. | |
257 | */ | |
258 | if (end & 0x7) { | |
259 | /* RFC2460 says always send parameter problem in | |
260 | * this case. -DaveM | |
261 | */ | |
0d53778e | 262 | pr_debug("end of fragment not rounded to 8 bytes.\n"); |
9fb9cbb1 YK |
263 | return -1; |
264 | } | |
5ab11c98 | 265 | if (end > fq->q.len) { |
9fb9cbb1 | 266 | /* Some bits beyond end -> corruption. */ |
bc578a54 | 267 | if (fq->q.last_in & INET_FRAG_LAST_IN) { |
0d53778e | 268 | pr_debug("last packet already reached.\n"); |
9fb9cbb1 YK |
269 | goto err; |
270 | } | |
5ab11c98 | 271 | fq->q.len = end; |
9fb9cbb1 YK |
272 | } |
273 | } | |
274 | ||
275 | if (end == offset) | |
276 | goto err; | |
277 | ||
278 | /* Point into the IP datagram 'data' part. */ | |
279 | if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) { | |
0d53778e | 280 | pr_debug("queue: message is too short.\n"); |
9fb9cbb1 YK |
281 | goto err; |
282 | } | |
b38dfee3 | 283 | if (pskb_trim_rcsum(skb, end - offset)) { |
0d53778e | 284 | pr_debug("Can't trim\n"); |
b38dfee3 | 285 | goto err; |
9fb9cbb1 YK |
286 | } |
287 | ||
288 | /* Find out which fragments are in front and at the back of us | |
289 | * in the chain of fragments so far. We must know where to put | |
290 | * this fragment, right? | |
291 | */ | |
ea8fbe8f CG |
292 | prev = fq->q.fragments_tail; |
293 | if (!prev || NFCT_FRAG6_CB(prev)->offset < offset) { | |
294 | next = NULL; | |
295 | goto found; | |
296 | } | |
9fb9cbb1 | 297 | prev = NULL; |
5ab11c98 | 298 | for (next = fq->q.fragments; next != NULL; next = next->next) { |
9fb9cbb1 YK |
299 | if (NFCT_FRAG6_CB(next)->offset >= offset) |
300 | break; /* bingo! */ | |
301 | prev = next; | |
302 | } | |
303 | ||
ea8fbe8f | 304 | found: |
1ee89bd0 ND |
305 | /* RFC5722, Section 4: |
306 | * When reassembling an IPv6 datagram, if | |
307 | * one or more its constituent fragments is determined to be an | |
308 | * overlapping fragment, the entire datagram (and any constituent | |
309 | * fragments, including those not yet received) MUST be silently | |
310 | * discarded. | |
9fb9cbb1 | 311 | */ |
9fb9cbb1 | 312 | |
1ee89bd0 ND |
313 | /* Check for overlap with preceding fragment. */ |
314 | if (prev && | |
22e091e5 | 315 | (NFCT_FRAG6_CB(prev)->offset + prev->len) > offset) |
1ee89bd0 | 316 | goto discard_fq; |
9fb9cbb1 | 317 | |
1ee89bd0 ND |
318 | /* Look for overlap with succeeding segment. */ |
319 | if (next && NFCT_FRAG6_CB(next)->offset < end) | |
320 | goto discard_fq; | |
9fb9cbb1 YK |
321 | |
322 | NFCT_FRAG6_CB(skb)->offset = offset; | |
323 | ||
324 | /* Insert this fragment in the chain of fragments. */ | |
325 | skb->next = next; | |
ea8fbe8f CG |
326 | if (!next) |
327 | fq->q.fragments_tail = skb; | |
9fb9cbb1 YK |
328 | if (prev) |
329 | prev->next = skb; | |
330 | else | |
5ab11c98 | 331 | fq->q.fragments = skb; |
9fb9cbb1 | 332 | |
97cf00e9 HX |
333 | if (skb->dev) { |
334 | fq->iif = skb->dev->ifindex; | |
335 | skb->dev = NULL; | |
336 | } | |
5ab11c98 PE |
337 | fq->q.stamp = skb->tstamp; |
338 | fq->q.meat += skb->len; | |
b8dd6a22 | 339 | fq->ecn |= ecn; |
4cdd3408 PM |
340 | if (payload_len > fq->q.max_size) |
341 | fq->q.max_size = payload_len; | |
d433673e | 342 | add_frag_mem_limit(&fq->q, skb->truesize); |
9fb9cbb1 YK |
343 | |
344 | /* The first fragment. | |
345 | * nhoffset is obtained from the first fragment, of course. | |
346 | */ | |
347 | if (offset == 0) { | |
348 | fq->nhoffset = nhoff; | |
bc578a54 | 349 | fq->q.last_in |= INET_FRAG_FIRST_IN; |
9fb9cbb1 | 350 | } |
3ef0eb0d | 351 | |
9fb9cbb1 YK |
352 | return 0; |
353 | ||
1ee89bd0 | 354 | discard_fq: |
b836c99f | 355 | inet_frag_kill(&fq->q, &nf_frags); |
9fb9cbb1 YK |
356 | err: |
357 | return -1; | |
358 | } | |
359 | ||
360 | /* | |
361 | * Check if this packet is complete. | |
362 | * Returns NULL on failure by any reason, and pointer | |
363 | * to current nexthdr field in reassembled frame. | |
364 | * | |
365 | * It is called with locked fq, and caller must check that | |
366 | * queue is eligible for reassembly i.e. it is not COMPLETE, | |
367 | * the last and the first frames arrived and all the bits are here. | |
368 | */ | |
369 | static struct sk_buff * | |
b836c99f | 370 | nf_ct_frag6_reasm(struct frag_queue *fq, struct net_device *dev) |
9fb9cbb1 | 371 | { |
5ab11c98 | 372 | struct sk_buff *fp, *op, *head = fq->q.fragments; |
9fb9cbb1 | 373 | int payload_len; |
b8dd6a22 | 374 | u8 ecn; |
9fb9cbb1 | 375 | |
b836c99f | 376 | inet_frag_kill(&fq->q, &nf_frags); |
9fb9cbb1 | 377 | |
547b792c IJ |
378 | WARN_ON(head == NULL); |
379 | WARN_ON(NFCT_FRAG6_CB(head)->offset != 0); | |
9fb9cbb1 | 380 | |
b8dd6a22 HFS |
381 | ecn = ip_frag_ecn_table[fq->ecn]; |
382 | if (unlikely(ecn == 0xff)) | |
383 | goto out_fail; | |
384 | ||
9fb9cbb1 | 385 | /* Unfragmented part is taken from the first segment. */ |
d56f90a7 | 386 | payload_len = ((head->data - skb_network_header(head)) - |
5ab11c98 | 387 | sizeof(struct ipv6hdr) + fq->q.len - |
d56f90a7 | 388 | sizeof(struct frag_hdr)); |
9fb9cbb1 | 389 | if (payload_len > IPV6_MAXPLEN) { |
0d53778e | 390 | pr_debug("payload len is too large.\n"); |
9fb9cbb1 YK |
391 | goto out_oversize; |
392 | } | |
393 | ||
394 | /* Head of list must not be cloned. */ | |
14bbd6a5 | 395 | if (skb_unclone(head, GFP_ATOMIC)) { |
0d53778e | 396 | pr_debug("skb is cloned but can't expand head"); |
9fb9cbb1 YK |
397 | goto out_oom; |
398 | } | |
399 | ||
400 | /* If the first fragment is fragmented itself, we split | |
401 | * it to two chunks: the first with data and paged part | |
402 | * and the second, holding only fragments. */ | |
21dc3301 | 403 | if (skb_has_frag_list(head)) { |
9fb9cbb1 YK |
404 | struct sk_buff *clone; |
405 | int i, plen = 0; | |
406 | ||
0a9ee813 JP |
407 | clone = alloc_skb(0, GFP_ATOMIC); |
408 | if (clone == NULL) | |
9fb9cbb1 | 409 | goto out_oom; |
0a9ee813 | 410 | |
9fb9cbb1 YK |
411 | clone->next = head->next; |
412 | head->next = clone; | |
413 | skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list; | |
343a9972 | 414 | skb_frag_list_init(head); |
9e903e08 ED |
415 | for (i = 0; i < skb_shinfo(head)->nr_frags; i++) |
416 | plen += skb_frag_size(&skb_shinfo(head)->frags[i]); | |
9fb9cbb1 YK |
417 | clone->len = clone->data_len = head->data_len - plen; |
418 | head->data_len -= clone->len; | |
419 | head->len -= clone->len; | |
420 | clone->csum = 0; | |
421 | clone->ip_summed = head->ip_summed; | |
422 | ||
423 | NFCT_FRAG6_CB(clone)->orig = NULL; | |
d433673e | 424 | add_frag_mem_limit(&fq->q, clone->truesize); |
9fb9cbb1 YK |
425 | } |
426 | ||
427 | /* We have to remove fragment header from datagram and to relocate | |
428 | * header in order to calculate ICV correctly. */ | |
bff9b61c | 429 | skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0]; |
1ab1457c | 430 | memmove(head->head + sizeof(struct frag_hdr), head->head, |
9fb9cbb1 | 431 | (head->data - head->head) - sizeof(struct frag_hdr)); |
b0e380b1 ACM |
432 | head->mac_header += sizeof(struct frag_hdr); |
433 | head->network_header += sizeof(struct frag_hdr); | |
9fb9cbb1 YK |
434 | |
435 | skb_shinfo(head)->frag_list = head->next; | |
badff6d0 | 436 | skb_reset_transport_header(head); |
d56f90a7 | 437 | skb_push(head, head->data - skb_network_header(head)); |
9fb9cbb1 YK |
438 | |
439 | for (fp=head->next; fp; fp = fp->next) { | |
440 | head->data_len += fp->len; | |
441 | head->len += fp->len; | |
442 | if (head->ip_summed != fp->ip_summed) | |
443 | head->ip_summed = CHECKSUM_NONE; | |
84fa7933 | 444 | else if (head->ip_summed == CHECKSUM_COMPLETE) |
9fb9cbb1 YK |
445 | head->csum = csum_add(head->csum, fp->csum); |
446 | head->truesize += fp->truesize; | |
9fb9cbb1 | 447 | } |
d433673e | 448 | sub_frag_mem_limit(&fq->q, head->truesize); |
9fb9cbb1 | 449 | |
60ff7467 | 450 | head->ignore_df = 1; |
9fb9cbb1 YK |
451 | head->next = NULL; |
452 | head->dev = dev; | |
5ab11c98 | 453 | head->tstamp = fq->q.stamp; |
0660e03f | 454 | ipv6_hdr(head)->payload_len = htons(payload_len); |
b8dd6a22 | 455 | ipv6_change_dsfield(ipv6_hdr(head), 0xff, ecn); |
4cdd3408 | 456 | IP6CB(head)->frag_max_size = sizeof(struct ipv6hdr) + fq->q.max_size; |
9fb9cbb1 YK |
457 | |
458 | /* Yes, and fold redundant checksum back. 8) */ | |
84fa7933 | 459 | if (head->ip_summed == CHECKSUM_COMPLETE) |
d56f90a7 | 460 | head->csum = csum_partial(skb_network_header(head), |
cfe1fc77 | 461 | skb_network_header_len(head), |
d56f90a7 | 462 | head->csum); |
9fb9cbb1 | 463 | |
5ab11c98 | 464 | fq->q.fragments = NULL; |
ea8fbe8f | 465 | fq->q.fragments_tail = NULL; |
9fb9cbb1 YK |
466 | |
467 | /* all original skbs are linked into the NFCT_FRAG6_CB(head).orig */ | |
468 | fp = skb_shinfo(head)->frag_list; | |
9e2dcf72 | 469 | if (fp && NFCT_FRAG6_CB(fp)->orig == NULL) |
9fb9cbb1 YK |
470 | /* at above code, head skb is divided into two skbs. */ |
471 | fp = fp->next; | |
472 | ||
473 | op = NFCT_FRAG6_CB(head)->orig; | |
474 | for (; fp; fp = fp->next) { | |
475 | struct sk_buff *orig = NFCT_FRAG6_CB(fp)->orig; | |
476 | ||
477 | op->next = orig; | |
478 | op = orig; | |
479 | NFCT_FRAG6_CB(fp)->orig = NULL; | |
480 | } | |
481 | ||
482 | return head; | |
483 | ||
484 | out_oversize: | |
e87cc472 JP |
485 | net_dbg_ratelimited("nf_ct_frag6_reasm: payload len = %d\n", |
486 | payload_len); | |
9fb9cbb1 YK |
487 | goto out_fail; |
488 | out_oom: | |
e87cc472 | 489 | net_dbg_ratelimited("nf_ct_frag6_reasm: no memory for reassembly\n"); |
9fb9cbb1 YK |
490 | out_fail: |
491 | return NULL; | |
492 | } | |
493 | ||
494 | /* | |
495 | * find the header just before Fragment Header. | |
496 | * | |
497 | * if success return 0 and set ... | |
498 | * (*prevhdrp): the value of "Next Header Field" in the header | |
499 | * just before Fragment Header. | |
500 | * (*prevhoff): the offset of "Next Header Field" in the header | |
501 | * just before Fragment Header. | |
502 | * (*fhoff) : the offset of Fragment Header. | |
503 | * | |
504 | * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c | |
505 | * | |
506 | */ | |
507 | static int | |
508 | find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff) | |
509 | { | |
0660e03f | 510 | u8 nexthdr = ipv6_hdr(skb)->nexthdr; |
6b88dd96 ACM |
511 | const int netoff = skb_network_offset(skb); |
512 | u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr); | |
513 | int start = netoff + sizeof(struct ipv6hdr); | |
9fb9cbb1 YK |
514 | int len = skb->len - start; |
515 | u8 prevhdr = NEXTHDR_IPV6; | |
516 | ||
1ab1457c YH |
517 | while (nexthdr != NEXTHDR_FRAGMENT) { |
518 | struct ipv6_opt_hdr hdr; | |
519 | int hdrlen; | |
9fb9cbb1 YK |
520 | |
521 | if (!ipv6_ext_hdr(nexthdr)) { | |
522 | return -1; | |
523 | } | |
1ab1457c | 524 | if (nexthdr == NEXTHDR_NONE) { |
0d53778e | 525 | pr_debug("next header is none\n"); |
9fb9cbb1 YK |
526 | return -1; |
527 | } | |
d1238d53 CP |
528 | if (len < (int)sizeof(struct ipv6_opt_hdr)) { |
529 | pr_debug("too short\n"); | |
530 | return -1; | |
531 | } | |
1ab1457c YH |
532 | if (skb_copy_bits(skb, start, &hdr, sizeof(hdr))) |
533 | BUG(); | |
534 | if (nexthdr == NEXTHDR_AUTH) | |
535 | hdrlen = (hdr.hdrlen+2)<<2; | |
536 | else | |
537 | hdrlen = ipv6_optlen(&hdr); | |
9fb9cbb1 YK |
538 | |
539 | prevhdr = nexthdr; | |
540 | prev_nhoff = start; | |
541 | ||
1ab1457c YH |
542 | nexthdr = hdr.nexthdr; |
543 | len -= hdrlen; | |
544 | start += hdrlen; | |
545 | } | |
9fb9cbb1 YK |
546 | |
547 | if (len < 0) | |
548 | return -1; | |
549 | ||
550 | *prevhdrp = prevhdr; | |
551 | *prevhoff = prev_nhoff; | |
552 | *fhoff = start; | |
553 | ||
554 | return 0; | |
555 | } | |
556 | ||
0b5ccb2e | 557 | struct sk_buff *nf_ct_frag6_gather(struct sk_buff *skb, u32 user) |
9fb9cbb1 | 558 | { |
1ab1457c | 559 | struct sk_buff *clone; |
9fb9cbb1 | 560 | struct net_device *dev = skb->dev; |
c038a767 AW |
561 | struct net *net = skb_dst(skb) ? dev_net(skb_dst(skb)->dev) |
562 | : dev_net(skb->dev); | |
9fb9cbb1 | 563 | struct frag_hdr *fhdr; |
b836c99f | 564 | struct frag_queue *fq; |
9fb9cbb1 YK |
565 | struct ipv6hdr *hdr; |
566 | int fhoff, nhoff; | |
567 | u8 prevhdr; | |
568 | struct sk_buff *ret_skb = NULL; | |
569 | ||
570 | /* Jumbo payload inhibits frag. header */ | |
0660e03f | 571 | if (ipv6_hdr(skb)->payload_len == 0) { |
0d53778e | 572 | pr_debug("payload len = 0\n"); |
9fb9cbb1 YK |
573 | return skb; |
574 | } | |
575 | ||
576 | if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0) | |
577 | return skb; | |
578 | ||
579 | clone = skb_clone(skb, GFP_ATOMIC); | |
580 | if (clone == NULL) { | |
0d53778e | 581 | pr_debug("Can't clone skb\n"); |
9fb9cbb1 YK |
582 | return skb; |
583 | } | |
584 | ||
585 | NFCT_FRAG6_CB(clone)->orig = skb; | |
586 | ||
587 | if (!pskb_may_pull(clone, fhoff + sizeof(*fhdr))) { | |
0d53778e | 588 | pr_debug("message is too short.\n"); |
9fb9cbb1 YK |
589 | goto ret_orig; |
590 | } | |
591 | ||
967b05f6 | 592 | skb_set_transport_header(clone, fhoff); |
0660e03f | 593 | hdr = ipv6_hdr(clone); |
bff9b61c | 594 | fhdr = (struct frag_hdr *)skb_transport_header(clone); |
9fb9cbb1 | 595 | |
b8dd6a22 HFS |
596 | fq = fq_find(net, fhdr->identification, user, &hdr->saddr, &hdr->daddr, |
597 | ip6_frag_ecn(hdr)); | |
9fb9cbb1 | 598 | if (fq == NULL) { |
0d53778e | 599 | pr_debug("Can't find and can't create new queue\n"); |
9fb9cbb1 YK |
600 | goto ret_orig; |
601 | } | |
602 | ||
b9c69896 | 603 | spin_lock_bh(&fq->q.lock); |
9fb9cbb1 YK |
604 | |
605 | if (nf_ct_frag6_queue(fq, clone, fhdr, nhoff) < 0) { | |
b9c69896 | 606 | spin_unlock_bh(&fq->q.lock); |
0d53778e | 607 | pr_debug("Can't insert skb to queue\n"); |
b836c99f | 608 | inet_frag_put(&fq->q, &nf_frags); |
9fb9cbb1 YK |
609 | goto ret_orig; |
610 | } | |
611 | ||
bc578a54 JP |
612 | if (fq->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) && |
613 | fq->q.meat == fq->q.len) { | |
9fb9cbb1 YK |
614 | ret_skb = nf_ct_frag6_reasm(fq, dev); |
615 | if (ret_skb == NULL) | |
0d53778e | 616 | pr_debug("Can't reassemble fragmented packets\n"); |
9fb9cbb1 | 617 | } |
b9c69896 | 618 | spin_unlock_bh(&fq->q.lock); |
9fb9cbb1 | 619 | |
b836c99f | 620 | inet_frag_put(&fq->q, &nf_frags); |
9fb9cbb1 YK |
621 | return ret_skb; |
622 | ||
623 | ret_orig: | |
624 | kfree_skb(clone); | |
625 | return skb; | |
626 | } | |
627 | ||
6aafeef0 | 628 | void nf_ct_frag6_consume_orig(struct sk_buff *skb) |
9fb9cbb1 YK |
629 | { |
630 | struct sk_buff *s, *s2; | |
631 | ||
632 | for (s = NFCT_FRAG6_CB(skb)->orig; s;) { | |
9fb9cbb1 | 633 | s2 = s->next; |
f9f02cca | 634 | s->next = NULL; |
6aafeef0 | 635 | consume_skb(s); |
9fb9cbb1 YK |
636 | s = s2; |
637 | } | |
9fb9cbb1 YK |
638 | } |
639 | ||
c038a767 AW |
640 | static int nf_ct_net_init(struct net *net) |
641 | { | |
642 | net->nf_frag.frags.high_thresh = IPV6_FRAG_HIGH_THRESH; | |
643 | net->nf_frag.frags.low_thresh = IPV6_FRAG_LOW_THRESH; | |
644 | net->nf_frag.frags.timeout = IPV6_FRAG_TIMEOUT; | |
645 | inet_frags_init_net(&net->nf_frag.frags); | |
646 | ||
647 | return nf_ct_frag6_sysctl_register(net); | |
648 | } | |
649 | ||
650 | static void nf_ct_net_exit(struct net *net) | |
651 | { | |
652 | nf_ct_frags6_sysctl_unregister(net); | |
653 | inet_frags_exit_net(&net->nf_frag.frags, &nf_frags); | |
654 | } | |
655 | ||
656 | static struct pernet_operations nf_ct_net_ops = { | |
657 | .init = nf_ct_net_init, | |
658 | .exit = nf_ct_net_exit, | |
659 | }; | |
660 | ||
9fb9cbb1 YK |
661 | int nf_ct_frag6_init(void) |
662 | { | |
c038a767 AW |
663 | int ret = 0; |
664 | ||
321a3a99 | 665 | nf_frags.hashfn = nf_hashfn; |
c6fda282 | 666 | nf_frags.constructor = ip6_frag_init; |
c9547709 | 667 | nf_frags.destructor = NULL; |
1e4b8287 | 668 | nf_frags.skb_free = nf_skb_free; |
b836c99f | 669 | nf_frags.qsize = sizeof(struct frag_queue); |
abd6523d | 670 | nf_frags.match = ip6_frag_match; |
e521db9d | 671 | nf_frags.frag_expire = nf_ct_frag6_expire; |
7eb95156 | 672 | inet_frags_init(&nf_frags); |
9fb9cbb1 | 673 | |
c038a767 AW |
674 | ret = register_pernet_subsys(&nf_ct_net_ops); |
675 | if (ret) | |
e97c3e27 | 676 | inet_frags_fini(&nf_frags); |
e97c3e27 | 677 | |
c038a767 | 678 | return ret; |
9fb9cbb1 YK |
679 | } |
680 | ||
681 | void nf_ct_frag6_cleanup(void) | |
682 | { | |
c038a767 | 683 | unregister_pernet_subsys(&nf_ct_net_ops); |
7eb95156 | 684 | inet_frags_fini(&nf_frags); |
9fb9cbb1 | 685 | } |