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