]>
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 | { | |
150 | u32 c; | |
151 | ||
152 | net_get_random_once(&nf_frags.rnd, sizeof(nf_frags.rnd)); | |
153 | c = jhash_3words(ipv6_addr_hash(saddr), ipv6_addr_hash(daddr), | |
154 | (__force u32)id, nf_frags.rnd); | |
155 | return c & (INETFRAGS_HASHSZ - 1); | |
156 | } | |
157 | ||
158 | ||
321a3a99 | 159 | static unsigned int nf_hashfn(struct inet_frag_queue *q) |
9fb9cbb1 | 160 | { |
b836c99f | 161 | const struct frag_queue *nq; |
9fb9cbb1 | 162 | |
b836c99f | 163 | nq = container_of(q, struct frag_queue, q); |
b1190570 | 164 | return nf_hash_frag(nq->id, &nq->saddr, &nq->daddr); |
9fb9cbb1 YK |
165 | } |
166 | ||
1e4b8287 PE |
167 | static void nf_skb_free(struct sk_buff *skb) |
168 | { | |
169 | if (NFCT_FRAG6_CB(skb)->orig) | |
170 | kfree_skb(NFCT_FRAG6_CB(skb)->orig); | |
171 | } | |
172 | ||
9fb9cbb1 YK |
173 | static void nf_ct_frag6_expire(unsigned long data) |
174 | { | |
b836c99f AW |
175 | struct frag_queue *fq; |
176 | struct net *net; | |
9fb9cbb1 | 177 | |
b836c99f AW |
178 | fq = container_of((struct inet_frag_queue *)data, struct frag_queue, q); |
179 | net = container_of(fq->q.net, struct net, nf_frag.frags); | |
9fb9cbb1 | 180 | |
b836c99f | 181 | ip6_expire_frag_queue(net, fq, &nf_frags); |
9fb9cbb1 YK |
182 | } |
183 | ||
184 | /* Creation primitives. */ | |
b836c99f AW |
185 | static inline struct frag_queue *fq_find(struct net *net, __be32 id, |
186 | u32 user, struct in6_addr *src, | |
b8dd6a22 | 187 | struct in6_addr *dst, u8 ecn) |
9fb9cbb1 | 188 | { |
2588fe1d | 189 | struct inet_frag_queue *q; |
c6fda282 | 190 | struct ip6_create_arg arg; |
abd6523d | 191 | unsigned int hash; |
9fb9cbb1 | 192 | |
c6fda282 | 193 | arg.id = id; |
0b5ccb2e | 194 | arg.user = user; |
c6fda282 PE |
195 | arg.src = src; |
196 | arg.dst = dst; | |
b8dd6a22 | 197 | arg.ecn = ecn; |
9a375803 PE |
198 | |
199 | read_lock_bh(&nf_frags.lock); | |
b1190570 | 200 | hash = nf_hash_frag(id, src, dst); |
9fb9cbb1 | 201 | |
c038a767 | 202 | q = inet_frag_find(&net->nf_frag.frags, &nf_frags, &arg, hash); |
b9c69896 | 203 | local_bh_enable(); |
5a3da1fe HFS |
204 | if (IS_ERR_OR_NULL(q)) { |
205 | inet_frag_maybe_warn_overflow(q, pr_fmt()); | |
206 | return NULL; | |
207 | } | |
b836c99f | 208 | return container_of(q, struct frag_queue, q); |
9fb9cbb1 YK |
209 | } |
210 | ||
9fb9cbb1 | 211 | |
b836c99f | 212 | static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb, |
58c0fb0d | 213 | const struct frag_hdr *fhdr, int nhoff) |
9fb9cbb1 YK |
214 | { |
215 | struct sk_buff *prev, *next; | |
4cdd3408 | 216 | unsigned int payload_len; |
9fb9cbb1 | 217 | int offset, end; |
b8dd6a22 | 218 | u8 ecn; |
9fb9cbb1 | 219 | |
bc578a54 | 220 | if (fq->q.last_in & INET_FRAG_COMPLETE) { |
698f9315 | 221 | pr_debug("Already completed\n"); |
9fb9cbb1 YK |
222 | goto err; |
223 | } | |
224 | ||
4cdd3408 PM |
225 | payload_len = ntohs(ipv6_hdr(skb)->payload_len); |
226 | ||
9fb9cbb1 | 227 | offset = ntohs(fhdr->frag_off) & ~0x7; |
4cdd3408 | 228 | end = offset + (payload_len - |
0660e03f | 229 | ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1))); |
9fb9cbb1 YK |
230 | |
231 | if ((unsigned int)end > IPV6_MAXPLEN) { | |
0d53778e | 232 | pr_debug("offset is too large.\n"); |
1ab1457c | 233 | return -1; |
9fb9cbb1 YK |
234 | } |
235 | ||
b8dd6a22 HFS |
236 | ecn = ip6_frag_ecn(ipv6_hdr(skb)); |
237 | ||
d56f90a7 ACM |
238 | if (skb->ip_summed == CHECKSUM_COMPLETE) { |
239 | const unsigned char *nh = skb_network_header(skb); | |
1ab1457c | 240 | skb->csum = csum_sub(skb->csum, |
d56f90a7 | 241 | csum_partial(nh, (u8 *)(fhdr + 1) - nh, |
9fb9cbb1 | 242 | 0)); |
d56f90a7 | 243 | } |
9fb9cbb1 YK |
244 | |
245 | /* Is this the final fragment? */ | |
246 | if (!(fhdr->frag_off & htons(IP6_MF))) { | |
247 | /* If we already have some bits beyond end | |
248 | * or have different end, the segment is corrupted. | |
249 | */ | |
5ab11c98 | 250 | if (end < fq->q.len || |
bc578a54 | 251 | ((fq->q.last_in & INET_FRAG_LAST_IN) && end != fq->q.len)) { |
0d53778e | 252 | pr_debug("already received last fragment\n"); |
9fb9cbb1 YK |
253 | goto err; |
254 | } | |
bc578a54 | 255 | fq->q.last_in |= INET_FRAG_LAST_IN; |
5ab11c98 | 256 | fq->q.len = end; |
9fb9cbb1 YK |
257 | } else { |
258 | /* Check if the fragment is rounded to 8 bytes. | |
259 | * Required by the RFC. | |
260 | */ | |
261 | if (end & 0x7) { | |
262 | /* RFC2460 says always send parameter problem in | |
263 | * this case. -DaveM | |
264 | */ | |
0d53778e | 265 | pr_debug("end of fragment not rounded to 8 bytes.\n"); |
9fb9cbb1 YK |
266 | return -1; |
267 | } | |
5ab11c98 | 268 | if (end > fq->q.len) { |
9fb9cbb1 | 269 | /* Some bits beyond end -> corruption. */ |
bc578a54 | 270 | if (fq->q.last_in & INET_FRAG_LAST_IN) { |
0d53778e | 271 | pr_debug("last packet already reached.\n"); |
9fb9cbb1 YK |
272 | goto err; |
273 | } | |
5ab11c98 | 274 | fq->q.len = end; |
9fb9cbb1 YK |
275 | } |
276 | } | |
277 | ||
278 | if (end == offset) | |
279 | goto err; | |
280 | ||
281 | /* Point into the IP datagram 'data' part. */ | |
282 | if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) { | |
0d53778e | 283 | pr_debug("queue: message is too short.\n"); |
9fb9cbb1 YK |
284 | goto err; |
285 | } | |
b38dfee3 | 286 | if (pskb_trim_rcsum(skb, end - offset)) { |
0d53778e | 287 | pr_debug("Can't trim\n"); |
b38dfee3 | 288 | goto err; |
9fb9cbb1 YK |
289 | } |
290 | ||
291 | /* Find out which fragments are in front and at the back of us | |
292 | * in the chain of fragments so far. We must know where to put | |
293 | * this fragment, right? | |
294 | */ | |
ea8fbe8f CG |
295 | prev = fq->q.fragments_tail; |
296 | if (!prev || NFCT_FRAG6_CB(prev)->offset < offset) { | |
297 | next = NULL; | |
298 | goto found; | |
299 | } | |
9fb9cbb1 | 300 | prev = NULL; |
5ab11c98 | 301 | for (next = fq->q.fragments; next != NULL; next = next->next) { |
9fb9cbb1 YK |
302 | if (NFCT_FRAG6_CB(next)->offset >= offset) |
303 | break; /* bingo! */ | |
304 | prev = next; | |
305 | } | |
306 | ||
ea8fbe8f | 307 | found: |
1ee89bd0 ND |
308 | /* RFC5722, Section 4: |
309 | * When reassembling an IPv6 datagram, if | |
310 | * one or more its constituent fragments is determined to be an | |
311 | * overlapping fragment, the entire datagram (and any constituent | |
312 | * fragments, including those not yet received) MUST be silently | |
313 | * discarded. | |
9fb9cbb1 | 314 | */ |
9fb9cbb1 | 315 | |
1ee89bd0 ND |
316 | /* Check for overlap with preceding fragment. */ |
317 | if (prev && | |
22e091e5 | 318 | (NFCT_FRAG6_CB(prev)->offset + prev->len) > offset) |
1ee89bd0 | 319 | goto discard_fq; |
9fb9cbb1 | 320 | |
1ee89bd0 ND |
321 | /* Look for overlap with succeeding segment. */ |
322 | if (next && NFCT_FRAG6_CB(next)->offset < end) | |
323 | goto discard_fq; | |
9fb9cbb1 YK |
324 | |
325 | NFCT_FRAG6_CB(skb)->offset = offset; | |
326 | ||
327 | /* Insert this fragment in the chain of fragments. */ | |
328 | skb->next = next; | |
ea8fbe8f CG |
329 | if (!next) |
330 | fq->q.fragments_tail = skb; | |
9fb9cbb1 YK |
331 | if (prev) |
332 | prev->next = skb; | |
333 | else | |
5ab11c98 | 334 | fq->q.fragments = skb; |
9fb9cbb1 | 335 | |
97cf00e9 HX |
336 | if (skb->dev) { |
337 | fq->iif = skb->dev->ifindex; | |
338 | skb->dev = NULL; | |
339 | } | |
5ab11c98 PE |
340 | fq->q.stamp = skb->tstamp; |
341 | fq->q.meat += skb->len; | |
b8dd6a22 | 342 | fq->ecn |= ecn; |
4cdd3408 PM |
343 | if (payload_len > fq->q.max_size) |
344 | fq->q.max_size = payload_len; | |
d433673e | 345 | add_frag_mem_limit(&fq->q, skb->truesize); |
9fb9cbb1 YK |
346 | |
347 | /* The first fragment. | |
348 | * nhoffset is obtained from the first fragment, of course. | |
349 | */ | |
350 | if (offset == 0) { | |
351 | fq->nhoffset = nhoff; | |
bc578a54 | 352 | fq->q.last_in |= INET_FRAG_FIRST_IN; |
9fb9cbb1 | 353 | } |
3ef0eb0d JDB |
354 | |
355 | inet_frag_lru_move(&fq->q); | |
9fb9cbb1 YK |
356 | return 0; |
357 | ||
1ee89bd0 | 358 | discard_fq: |
b836c99f | 359 | inet_frag_kill(&fq->q, &nf_frags); |
9fb9cbb1 YK |
360 | err: |
361 | return -1; | |
362 | } | |
363 | ||
364 | /* | |
365 | * Check if this packet is complete. | |
366 | * Returns NULL on failure by any reason, and pointer | |
367 | * to current nexthdr field in reassembled frame. | |
368 | * | |
369 | * It is called with locked fq, and caller must check that | |
370 | * queue is eligible for reassembly i.e. it is not COMPLETE, | |
371 | * the last and the first frames arrived and all the bits are here. | |
372 | */ | |
373 | static struct sk_buff * | |
b836c99f | 374 | nf_ct_frag6_reasm(struct frag_queue *fq, struct net_device *dev) |
9fb9cbb1 | 375 | { |
5ab11c98 | 376 | struct sk_buff *fp, *op, *head = fq->q.fragments; |
9fb9cbb1 | 377 | int payload_len; |
b8dd6a22 | 378 | u8 ecn; |
9fb9cbb1 | 379 | |
b836c99f | 380 | inet_frag_kill(&fq->q, &nf_frags); |
9fb9cbb1 | 381 | |
547b792c IJ |
382 | WARN_ON(head == NULL); |
383 | WARN_ON(NFCT_FRAG6_CB(head)->offset != 0); | |
9fb9cbb1 | 384 | |
b8dd6a22 HFS |
385 | ecn = ip_frag_ecn_table[fq->ecn]; |
386 | if (unlikely(ecn == 0xff)) | |
387 | goto out_fail; | |
388 | ||
9fb9cbb1 | 389 | /* Unfragmented part is taken from the first segment. */ |
d56f90a7 | 390 | payload_len = ((head->data - skb_network_header(head)) - |
5ab11c98 | 391 | sizeof(struct ipv6hdr) + fq->q.len - |
d56f90a7 | 392 | sizeof(struct frag_hdr)); |
9fb9cbb1 | 393 | if (payload_len > IPV6_MAXPLEN) { |
0d53778e | 394 | pr_debug("payload len is too large.\n"); |
9fb9cbb1 YK |
395 | goto out_oversize; |
396 | } | |
397 | ||
398 | /* Head of list must not be cloned. */ | |
14bbd6a5 | 399 | if (skb_unclone(head, GFP_ATOMIC)) { |
0d53778e | 400 | pr_debug("skb is cloned but can't expand head"); |
9fb9cbb1 YK |
401 | goto out_oom; |
402 | } | |
403 | ||
404 | /* If the first fragment is fragmented itself, we split | |
405 | * it to two chunks: the first with data and paged part | |
406 | * and the second, holding only fragments. */ | |
21dc3301 | 407 | if (skb_has_frag_list(head)) { |
9fb9cbb1 YK |
408 | struct sk_buff *clone; |
409 | int i, plen = 0; | |
410 | ||
0a9ee813 JP |
411 | clone = alloc_skb(0, GFP_ATOMIC); |
412 | if (clone == NULL) | |
9fb9cbb1 | 413 | goto out_oom; |
0a9ee813 | 414 | |
9fb9cbb1 YK |
415 | clone->next = head->next; |
416 | head->next = clone; | |
417 | skb_shinfo(clone)->frag_list = skb_shinfo(head)->frag_list; | |
343a9972 | 418 | skb_frag_list_init(head); |
9e903e08 ED |
419 | for (i = 0; i < skb_shinfo(head)->nr_frags; i++) |
420 | plen += skb_frag_size(&skb_shinfo(head)->frags[i]); | |
9fb9cbb1 YK |
421 | clone->len = clone->data_len = head->data_len - plen; |
422 | head->data_len -= clone->len; | |
423 | head->len -= clone->len; | |
424 | clone->csum = 0; | |
425 | clone->ip_summed = head->ip_summed; | |
426 | ||
427 | NFCT_FRAG6_CB(clone)->orig = NULL; | |
d433673e | 428 | add_frag_mem_limit(&fq->q, clone->truesize); |
9fb9cbb1 YK |
429 | } |
430 | ||
431 | /* We have to remove fragment header from datagram and to relocate | |
432 | * header in order to calculate ICV correctly. */ | |
bff9b61c | 433 | skb_network_header(head)[fq->nhoffset] = skb_transport_header(head)[0]; |
1ab1457c | 434 | memmove(head->head + sizeof(struct frag_hdr), head->head, |
9fb9cbb1 | 435 | (head->data - head->head) - sizeof(struct frag_hdr)); |
b0e380b1 ACM |
436 | head->mac_header += sizeof(struct frag_hdr); |
437 | head->network_header += sizeof(struct frag_hdr); | |
9fb9cbb1 YK |
438 | |
439 | skb_shinfo(head)->frag_list = head->next; | |
badff6d0 | 440 | skb_reset_transport_header(head); |
d56f90a7 | 441 | skb_push(head, head->data - skb_network_header(head)); |
9fb9cbb1 YK |
442 | |
443 | for (fp=head->next; fp; fp = fp->next) { | |
444 | head->data_len += fp->len; | |
445 | head->len += fp->len; | |
446 | if (head->ip_summed != fp->ip_summed) | |
447 | head->ip_summed = CHECKSUM_NONE; | |
84fa7933 | 448 | else if (head->ip_summed == CHECKSUM_COMPLETE) |
9fb9cbb1 YK |
449 | head->csum = csum_add(head->csum, fp->csum); |
450 | head->truesize += fp->truesize; | |
9fb9cbb1 | 451 | } |
d433673e | 452 | sub_frag_mem_limit(&fq->q, head->truesize); |
9fb9cbb1 | 453 | |
4cdd3408 | 454 | head->local_df = 1; |
9fb9cbb1 YK |
455 | head->next = NULL; |
456 | head->dev = dev; | |
5ab11c98 | 457 | head->tstamp = fq->q.stamp; |
0660e03f | 458 | ipv6_hdr(head)->payload_len = htons(payload_len); |
b8dd6a22 | 459 | ipv6_change_dsfield(ipv6_hdr(head), 0xff, ecn); |
4cdd3408 | 460 | IP6CB(head)->frag_max_size = sizeof(struct ipv6hdr) + fq->q.max_size; |
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; |
ea8fbe8f | 469 | fq->q.fragments_tail = NULL; |
9fb9cbb1 YK |
470 | |
471 | /* all original skbs are linked into the NFCT_FRAG6_CB(head).orig */ | |
472 | fp = skb_shinfo(head)->frag_list; | |
9e2dcf72 | 473 | if (fp && NFCT_FRAG6_CB(fp)->orig == NULL) |
9fb9cbb1 YK |
474 | /* at above code, head skb is divided into two skbs. */ |
475 | fp = fp->next; | |
476 | ||
477 | op = NFCT_FRAG6_CB(head)->orig; | |
478 | for (; fp; fp = fp->next) { | |
479 | struct sk_buff *orig = NFCT_FRAG6_CB(fp)->orig; | |
480 | ||
481 | op->next = orig; | |
482 | op = orig; | |
483 | NFCT_FRAG6_CB(fp)->orig = NULL; | |
484 | } | |
485 | ||
486 | return head; | |
487 | ||
488 | out_oversize: | |
e87cc472 JP |
489 | net_dbg_ratelimited("nf_ct_frag6_reasm: payload len = %d\n", |
490 | payload_len); | |
9fb9cbb1 YK |
491 | goto out_fail; |
492 | out_oom: | |
e87cc472 | 493 | net_dbg_ratelimited("nf_ct_frag6_reasm: no memory for reassembly\n"); |
9fb9cbb1 YK |
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 | 564 | struct net_device *dev = skb->dev; |
c038a767 AW |
565 | struct net *net = skb_dst(skb) ? dev_net(skb_dst(skb)->dev) |
566 | : dev_net(skb->dev); | |
9fb9cbb1 | 567 | struct frag_hdr *fhdr; |
b836c99f | 568 | struct frag_queue *fq; |
9fb9cbb1 YK |
569 | struct ipv6hdr *hdr; |
570 | int fhoff, nhoff; | |
571 | u8 prevhdr; | |
572 | struct sk_buff *ret_skb = NULL; | |
573 | ||
574 | /* Jumbo payload inhibits frag. header */ | |
0660e03f | 575 | if (ipv6_hdr(skb)->payload_len == 0) { |
0d53778e | 576 | pr_debug("payload len = 0\n"); |
9fb9cbb1 YK |
577 | return skb; |
578 | } | |
579 | ||
580 | if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0) | |
581 | return skb; | |
582 | ||
583 | clone = skb_clone(skb, GFP_ATOMIC); | |
584 | if (clone == NULL) { | |
0d53778e | 585 | pr_debug("Can't clone skb\n"); |
9fb9cbb1 YK |
586 | return skb; |
587 | } | |
588 | ||
589 | NFCT_FRAG6_CB(clone)->orig = skb; | |
590 | ||
591 | if (!pskb_may_pull(clone, fhoff + sizeof(*fhdr))) { | |
0d53778e | 592 | pr_debug("message is too short.\n"); |
9fb9cbb1 YK |
593 | goto ret_orig; |
594 | } | |
595 | ||
967b05f6 | 596 | skb_set_transport_header(clone, fhoff); |
0660e03f | 597 | hdr = ipv6_hdr(clone); |
bff9b61c | 598 | fhdr = (struct frag_hdr *)skb_transport_header(clone); |
9fb9cbb1 | 599 | |
6b102865 AW |
600 | local_bh_disable(); |
601 | inet_frag_evictor(&net->nf_frag.frags, &nf_frags, false); | |
602 | local_bh_enable(); | |
9fb9cbb1 | 603 | |
b8dd6a22 HFS |
604 | fq = fq_find(net, fhdr->identification, user, &hdr->saddr, &hdr->daddr, |
605 | 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 | ||
bc578a54 JP |
620 | if (fq->q.last_in == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) && |
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 | } | |
635 | ||
636 | void nf_ct_frag6_output(unsigned int hooknum, struct sk_buff *skb, | |
637 | struct net_device *in, struct net_device *out, | |
638 | int (*okfn)(struct sk_buff *)) | |
639 | { | |
640 | struct sk_buff *s, *s2; | |
4cdd3408 | 641 | unsigned int ret = 0; |
9fb9cbb1 YK |
642 | |
643 | for (s = NFCT_FRAG6_CB(skb)->orig; s;) { | |
644 | nf_conntrack_put_reasm(s->nfct_reasm); | |
645 | nf_conntrack_get_reasm(skb); | |
646 | s->nfct_reasm = skb; | |
647 | ||
648 | s2 = s->next; | |
f9f02cca PM |
649 | s->next = NULL; |
650 | ||
4cdd3408 PM |
651 | if (ret != -ECANCELED) |
652 | ret = NF_HOOK_THRESH(NFPROTO_IPV6, hooknum, s, | |
653 | in, out, okfn, | |
654 | NF_IP6_PRI_CONNTRACK_DEFRAG + 1); | |
655 | else | |
656 | kfree_skb(s); | |
657 | ||
9fb9cbb1 YK |
658 | s = s2; |
659 | } | |
660 | nf_conntrack_put_reasm(skb); | |
661 | } | |
662 | ||
c038a767 AW |
663 | static int nf_ct_net_init(struct net *net) |
664 | { | |
665 | net->nf_frag.frags.high_thresh = IPV6_FRAG_HIGH_THRESH; | |
666 | net->nf_frag.frags.low_thresh = IPV6_FRAG_LOW_THRESH; | |
667 | net->nf_frag.frags.timeout = IPV6_FRAG_TIMEOUT; | |
668 | inet_frags_init_net(&net->nf_frag.frags); | |
669 | ||
670 | return nf_ct_frag6_sysctl_register(net); | |
671 | } | |
672 | ||
673 | static void nf_ct_net_exit(struct net *net) | |
674 | { | |
675 | nf_ct_frags6_sysctl_unregister(net); | |
676 | inet_frags_exit_net(&net->nf_frag.frags, &nf_frags); | |
677 | } | |
678 | ||
679 | static struct pernet_operations nf_ct_net_ops = { | |
680 | .init = nf_ct_net_init, | |
681 | .exit = nf_ct_net_exit, | |
682 | }; | |
683 | ||
9fb9cbb1 YK |
684 | int nf_ct_frag6_init(void) |
685 | { | |
c038a767 AW |
686 | int ret = 0; |
687 | ||
321a3a99 | 688 | nf_frags.hashfn = nf_hashfn; |
c6fda282 | 689 | nf_frags.constructor = ip6_frag_init; |
c9547709 | 690 | nf_frags.destructor = NULL; |
1e4b8287 | 691 | nf_frags.skb_free = nf_skb_free; |
b836c99f | 692 | nf_frags.qsize = sizeof(struct frag_queue); |
abd6523d | 693 | nf_frags.match = ip6_frag_match; |
e521db9d | 694 | nf_frags.frag_expire = nf_ct_frag6_expire; |
3b4bc4a2 | 695 | nf_frags.secret_interval = 10 * 60 * HZ; |
7eb95156 | 696 | inet_frags_init(&nf_frags); |
9fb9cbb1 | 697 | |
c038a767 AW |
698 | ret = register_pernet_subsys(&nf_ct_net_ops); |
699 | if (ret) | |
e97c3e27 | 700 | inet_frags_fini(&nf_frags); |
e97c3e27 | 701 | |
c038a767 | 702 | return ret; |
9fb9cbb1 YK |
703 | } |
704 | ||
705 | void nf_ct_frag6_cleanup(void) | |
706 | { | |
c038a767 | 707 | unregister_pernet_subsys(&nf_ct_net_ops); |
7eb95156 | 708 | inet_frags_fini(&nf_frags); |
9fb9cbb1 | 709 | } |