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