1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * IPv6 fragment reassembly for connection tracking
4 *
5 * Copyright (C)2004 USAGI/WIDE Project
6 *
7 * Author:
8 * Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
9 *
10 * Based on: net/ipv6/reassembly.c
11 */
12
13 #define pr_fmt(fmt) "IPv6-nf: " fmt
14
15 #include <linux/errno.h>
16 #include <linux/types.h>
17 #include <linux/string.h>
18 #include <linux/net.h>
19 #include <linux/netdevice.h>
20 #include <linux/ipv6.h>
21 #include <linux/slab.h>
22
23 #include <net/ipv6_frag.h>
24
25 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
26 #include <linux/sysctl.h>
27 #include <linux/netfilter.h>
28 #include <linux/netfilter_ipv6.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
32 #include <net/netns/generic.h>
33
34 static const char nf_frags_cache_name[] = "nf-frags";
35
36 static unsigned int nf_frag_pernet_id __read_mostly;
37 static struct inet_frags nf_frags;
38
nf_frag_pernet(struct net * net)39 static struct nft_ct_frag6_pernet *nf_frag_pernet(struct net *net)
40 {
41 return net_generic(net, nf_frag_pernet_id);
42 }
43
44 #ifdef CONFIG_SYSCTL
45
46 static struct ctl_table nf_ct_frag6_sysctl_table[] = {
47 {
48 .procname = "nf_conntrack_frag6_timeout",
49 .maxlen = sizeof(unsigned int),
50 .mode = 0644,
51 .proc_handler = proc_dointvec_jiffies,
52 },
53 {
54 .procname = "nf_conntrack_frag6_low_thresh",
55 .maxlen = sizeof(unsigned long),
56 .mode = 0644,
57 .proc_handler = proc_doulongvec_minmax,
58 },
59 {
60 .procname = "nf_conntrack_frag6_high_thresh",
61 .maxlen = sizeof(unsigned long),
62 .mode = 0644,
63 .proc_handler = proc_doulongvec_minmax,
64 },
65 };
66
nf_ct_frag6_sysctl_register(struct net * net)67 static int nf_ct_frag6_sysctl_register(struct net *net)
68 {
69 struct nft_ct_frag6_pernet *nf_frag;
70 struct ctl_table *table;
71 struct ctl_table_header *hdr;
72
73 table = nf_ct_frag6_sysctl_table;
74 if (!net_eq(net, &init_net)) {
75 table = kmemdup(table, sizeof(nf_ct_frag6_sysctl_table),
76 GFP_KERNEL);
77 if (table == NULL)
78 goto err_alloc;
79 }
80
81 nf_frag = nf_frag_pernet(net);
82
83 table[0].data = &nf_frag->fqdir->timeout;
84 table[1].data = &nf_frag->fqdir->low_thresh;
85 table[1].extra2 = &nf_frag->fqdir->high_thresh;
86 table[2].data = &nf_frag->fqdir->high_thresh;
87 table[2].extra1 = &nf_frag->fqdir->low_thresh;
88
89 hdr = register_net_sysctl_sz(net, "net/netfilter", table,
90 ARRAY_SIZE(nf_ct_frag6_sysctl_table));
91 if (hdr == NULL)
92 goto err_reg;
93
94 nf_frag->nf_frag_frags_hdr = hdr;
95 return 0;
96
97 err_reg:
98 if (!net_eq(net, &init_net))
99 kfree(table);
100 err_alloc:
101 return -ENOMEM;
102 }
103
nf_ct_frags6_sysctl_unregister(struct net * net)104 static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net)
105 {
106 struct nft_ct_frag6_pernet *nf_frag = nf_frag_pernet(net);
107 const struct ctl_table *table;
108
109 table = nf_frag->nf_frag_frags_hdr->ctl_table_arg;
110 unregister_net_sysctl_table(nf_frag->nf_frag_frags_hdr);
111 if (!net_eq(net, &init_net))
112 kfree(table);
113 }
114
115 #else
nf_ct_frag6_sysctl_register(struct net * net)116 static int nf_ct_frag6_sysctl_register(struct net *net)
117 {
118 return 0;
119 }
nf_ct_frags6_sysctl_unregister(struct net * net)120 static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net)
121 {
122 }
123 #endif
124
125 static int nf_ct_frag6_reasm(struct frag_queue *fq, struct sk_buff *skb,
126 struct sk_buff *prev_tail, struct net_device *dev,
127 int *refs);
128
ip6_frag_ecn(const struct ipv6hdr * ipv6h)129 static inline u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
130 {
131 return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
132 }
133
nf_ct_frag6_expire(struct timer_list * t)134 static void nf_ct_frag6_expire(struct timer_list *t)
135 {
136 struct inet_frag_queue *frag = timer_container_of(frag, t, timer);
137 struct frag_queue *fq;
138
139 fq = container_of(frag, struct frag_queue, q);
140
141 ip6frag_expire_frag_queue(fq->q.fqdir->net, fq);
142 }
143
144 /* Creation primitives. */
fq_find(struct net * net,__be32 id,u32 user,const struct ipv6hdr * hdr,int iif)145 static struct frag_queue *fq_find(struct net *net, __be32 id, u32 user,
146 const struct ipv6hdr *hdr, int iif)
147 {
148 struct nft_ct_frag6_pernet *nf_frag = nf_frag_pernet(net);
149 struct frag_v6_compare_key key = {
150 .id = id,
151 .saddr = hdr->saddr,
152 .daddr = hdr->daddr,
153 .user = user,
154 .iif = iif,
155 };
156 struct inet_frag_queue *q;
157
158 if (!(ipv6_addr_type(&hdr->daddr) & (IPV6_ADDR_MULTICAST |
159 IPV6_ADDR_LINKLOCAL)))
160 key.iif = 0;
161
162 q = inet_frag_find(nf_frag->fqdir, &key);
163 if (!q)
164 return NULL;
165
166 return container_of(q, struct frag_queue, q);
167 }
168
169
nf_ct_frag6_queue(struct frag_queue * fq,struct sk_buff * skb,const struct frag_hdr * fhdr,int nhoff,int * refs)170 static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb,
171 const struct frag_hdr *fhdr, int nhoff,
172 int *refs)
173 {
174 unsigned int payload_len;
175 struct net_device *dev;
176 struct sk_buff *prev;
177 int offset, end, err;
178 u8 ecn;
179
180 if (fq->q.flags & INET_FRAG_COMPLETE) {
181 pr_debug("Already completed\n");
182 goto err;
183 }
184
185 payload_len = ntohs(ipv6_hdr(skb)->payload_len);
186
187 offset = ntohs(fhdr->frag_off) & ~0x7;
188 end = offset + (payload_len -
189 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
190
191 if ((unsigned int)end > IPV6_MAXPLEN) {
192 pr_debug("offset is too large.\n");
193 return -EINVAL;
194 }
195
196 ecn = ip6_frag_ecn(ipv6_hdr(skb));
197
198 if (skb->ip_summed == CHECKSUM_COMPLETE) {
199 const unsigned char *nh = skb_network_header(skb);
200 skb->csum = csum_sub(skb->csum,
201 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
202 0));
203 }
204
205 /* Is this the final fragment? */
206 if (!(fhdr->frag_off & htons(IP6_MF))) {
207 /* If we already have some bits beyond end
208 * or have different end, the segment is corrupted.
209 */
210 if (end < fq->q.len ||
211 ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len)) {
212 pr_debug("already received last fragment\n");
213 goto err;
214 }
215 fq->q.flags |= INET_FRAG_LAST_IN;
216 fq->q.len = end;
217 } else {
218 /* Check if the fragment is rounded to 8 bytes.
219 * Required by the RFC.
220 */
221 if (end & 0x7) {
222 /* RFC2460 says always send parameter problem in
223 * this case. -DaveM
224 */
225 pr_debug("end of fragment not rounded to 8 bytes.\n");
226 inet_frag_kill(&fq->q, refs);
227 return -EPROTO;
228 }
229 if (end > fq->q.len) {
230 /* Some bits beyond end -> corruption. */
231 if (fq->q.flags & INET_FRAG_LAST_IN) {
232 pr_debug("last packet already reached.\n");
233 goto err;
234 }
235 fq->q.len = end;
236 }
237 }
238
239 if (end == offset)
240 goto err;
241
242 /* Point into the IP datagram 'data' part. */
243 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
244 pr_debug("queue: message is too short.\n");
245 goto err;
246 }
247 if (pskb_trim_rcsum(skb, end - offset)) {
248 pr_debug("Can't trim\n");
249 goto err;
250 }
251
252 /* Note : skb->rbnode and skb->dev share the same location. */
253 dev = skb->dev;
254 /* Makes sure compiler wont do silly aliasing games */
255 barrier();
256
257 prev = fq->q.fragments_tail;
258 err = inet_frag_queue_insert(&fq->q, skb, offset, end);
259 if (err) {
260 if (err == IPFRAG_DUP) {
261 /* No error for duplicates, pretend they got queued. */
262 kfree_skb_reason(skb, SKB_DROP_REASON_DUP_FRAG);
263 return -EINPROGRESS;
264 }
265 goto insert_error;
266 }
267
268 if (dev)
269 fq->iif = dev->ifindex;
270
271 fq->q.stamp = skb->tstamp;
272 fq->q.tstamp_type = skb->tstamp_type;
273 fq->q.meat += skb->len;
274 fq->ecn |= ecn;
275 if (payload_len > fq->q.max_size)
276 fq->q.max_size = payload_len;
277 add_frag_mem_limit(fq->q.fqdir, skb->truesize);
278
279 /* The first fragment.
280 * nhoffset is obtained from the first fragment, of course.
281 */
282 if (offset == 0) {
283 fq->nhoffset = nhoff;
284 fq->q.flags |= INET_FRAG_FIRST_IN;
285 }
286
287 if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
288 fq->q.meat == fq->q.len) {
289 unsigned long orefdst = skb->_skb_refdst;
290
291 skb->_skb_refdst = 0UL;
292 err = nf_ct_frag6_reasm(fq, skb, prev, dev, refs);
293 skb->_skb_refdst = orefdst;
294
295 /* After queue has assumed skb ownership, only 0 or
296 * -EINPROGRESS must be returned.
297 */
298 return err ? -EINPROGRESS : 0;
299 }
300
301 skb_dst_drop(skb);
302 skb_orphan(skb);
303 return -EINPROGRESS;
304
305 insert_error:
306 inet_frag_kill(&fq->q, refs);
307 err:
308 skb_dst_drop(skb);
309 return -EINVAL;
310 }
311
312 /*
313 * Check if this packet is complete.
314 *
315 * It is called with locked fq, and caller must check that
316 * queue is eligible for reassembly i.e. it is not COMPLETE,
317 * the last and the first frames arrived and all the bits are here.
318 */
nf_ct_frag6_reasm(struct frag_queue * fq,struct sk_buff * skb,struct sk_buff * prev_tail,struct net_device * dev,int * refs)319 static int nf_ct_frag6_reasm(struct frag_queue *fq, struct sk_buff *skb,
320 struct sk_buff *prev_tail, struct net_device *dev,
321 int *refs)
322 {
323 void *reasm_data;
324 int payload_len;
325 u8 ecn;
326
327 inet_frag_kill(&fq->q, refs);
328
329 ecn = ip_frag_ecn_table[fq->ecn];
330 if (unlikely(ecn == 0xff))
331 goto err;
332
333 reasm_data = inet_frag_reasm_prepare(&fq->q, skb, prev_tail);
334 if (!reasm_data)
335 goto err;
336
337 payload_len = -skb_network_offset(skb) -
338 sizeof(struct ipv6hdr) + fq->q.len -
339 sizeof(struct frag_hdr);
340 if (payload_len > IPV6_MAXPLEN) {
341 net_dbg_ratelimited("nf_ct_frag6_reasm: payload len = %d\n",
342 payload_len);
343 goto err;
344 }
345
346 /* We have to remove fragment header from datagram and to relocate
347 * header in order to calculate ICV correctly. */
348 skb_network_header(skb)[fq->nhoffset] = skb_transport_header(skb)[0];
349 memmove(skb->head + sizeof(struct frag_hdr), skb->head,
350 (skb->data - skb->head) - sizeof(struct frag_hdr));
351 if (skb_mac_header_was_set(skb))
352 skb->mac_header += sizeof(struct frag_hdr);
353 skb->network_header += sizeof(struct frag_hdr);
354
355 skb_reset_transport_header(skb);
356
357 inet_frag_reasm_finish(&fq->q, skb, reasm_data, false);
358
359 skb->ignore_df = 1;
360 skb->dev = dev;
361 ipv6_hdr(skb)->payload_len = htons(payload_len);
362 ipv6_change_dsfield(ipv6_hdr(skb), 0xff, ecn);
363 IP6CB(skb)->frag_max_size = sizeof(struct ipv6hdr) + fq->q.max_size;
364 IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
365
366 /* Yes, and fold redundant checksum back. 8) */
367 if (skb->ip_summed == CHECKSUM_COMPLETE)
368 skb->csum = csum_partial(skb_network_header(skb),
369 skb_network_header_len(skb),
370 skb->csum);
371
372 fq->q.rb_fragments = RB_ROOT;
373 fq->q.fragments_tail = NULL;
374 fq->q.last_run_head = NULL;
375
376 return 0;
377
378 err:
379 inet_frag_kill(&fq->q, refs);
380 return -EINVAL;
381 }
382
383 /*
384 * find the header just before Fragment Header.
385 *
386 * if success return 0 and set ...
387 * (*prevhdrp): the value of "Next Header Field" in the header
388 * just before Fragment Header.
389 * (*prevhoff): the offset of "Next Header Field" in the header
390 * just before Fragment Header.
391 * (*fhoff) : the offset of Fragment Header.
392 *
393 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
394 *
395 */
396 static int
find_prev_fhdr(struct sk_buff * skb,u8 * prevhdrp,int * prevhoff,int * fhoff)397 find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)
398 {
399 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
400 const int netoff = skb_network_offset(skb);
401 u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr);
402 int start = netoff + sizeof(struct ipv6hdr);
403 int len = skb->len - start;
404 u8 prevhdr = NEXTHDR_IPV6;
405
406 while (nexthdr != NEXTHDR_FRAGMENT) {
407 struct ipv6_opt_hdr hdr;
408 int hdrlen;
409
410 if (!ipv6_ext_hdr(nexthdr)) {
411 return -1;
412 }
413 if (nexthdr == NEXTHDR_NONE) {
414 pr_debug("next header is none\n");
415 return -1;
416 }
417 if (len < (int)sizeof(struct ipv6_opt_hdr)) {
418 pr_debug("too short\n");
419 return -1;
420 }
421 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
422 return -1;
423 if (nexthdr == NEXTHDR_AUTH)
424 hdrlen = ipv6_authlen(&hdr);
425 else
426 hdrlen = ipv6_optlen(&hdr);
427
428 prevhdr = nexthdr;
429 prev_nhoff = start;
430
431 nexthdr = hdr.nexthdr;
432 len -= hdrlen;
433 start += hdrlen;
434 }
435
436 if (len < 0)
437 return -1;
438
439 *prevhdrp = prevhdr;
440 *prevhoff = prev_nhoff;
441 *fhoff = start;
442
443 return 0;
444 }
445
nf_ct_frag6_gather(struct net * net,struct sk_buff * skb,u32 user)446 int nf_ct_frag6_gather(struct net *net, struct sk_buff *skb, u32 user)
447 {
448 u16 savethdr = skb->transport_header;
449 u8 nexthdr = NEXTHDR_FRAGMENT;
450 int fhoff, nhoff, ret;
451 struct frag_hdr *fhdr;
452 struct frag_queue *fq;
453 struct ipv6hdr *hdr;
454 int refs = 0;
455 u8 prevhdr;
456
457 /* Jumbo payload inhibits frag. header */
458 if (ipv6_hdr(skb)->payload_len == 0) {
459 pr_debug("payload len = 0\n");
460 return 0;
461 }
462
463 if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
464 return 0;
465
466 /* Discard the first fragment if it does not include all headers
467 * RFC 8200, Section 4.5
468 */
469 if (ipv6frag_thdr_truncated(skb, fhoff, &nexthdr)) {
470 pr_debug("Drop incomplete fragment\n");
471 return 0;
472 }
473
474 if (!pskb_may_pull(skb, fhoff + sizeof(*fhdr)))
475 return -ENOMEM;
476
477 skb_set_transport_header(skb, fhoff);
478 hdr = ipv6_hdr(skb);
479 fhdr = (struct frag_hdr *)skb_transport_header(skb);
480
481 rcu_read_lock();
482 fq = fq_find(net, fhdr->identification, user, hdr,
483 skb->dev ? skb->dev->ifindex : 0);
484 if (fq == NULL) {
485 rcu_read_unlock();
486 pr_debug("Can't find and can't create new queue\n");
487 return -ENOMEM;
488 }
489
490 spin_lock_bh(&fq->q.lock);
491
492 ret = nf_ct_frag6_queue(fq, skb, fhdr, nhoff, &refs);
493 if (ret == -EPROTO) {
494 skb->transport_header = savethdr;
495 ret = 0;
496 }
497
498 spin_unlock_bh(&fq->q.lock);
499 rcu_read_unlock();
500 inet_frag_putn(&fq->q, refs);
501 return ret;
502 }
503 EXPORT_SYMBOL_GPL(nf_ct_frag6_gather);
504
nf_ct_net_init(struct net * net)505 static int nf_ct_net_init(struct net *net)
506 {
507 struct nft_ct_frag6_pernet *nf_frag = nf_frag_pernet(net);
508 int res;
509
510 res = fqdir_init(&nf_frag->fqdir, &nf_frags, net);
511 if (res < 0)
512 return res;
513
514 nf_frag->fqdir->high_thresh = IPV6_FRAG_HIGH_THRESH;
515 nf_frag->fqdir->low_thresh = IPV6_FRAG_LOW_THRESH;
516 nf_frag->fqdir->timeout = IPV6_FRAG_TIMEOUT;
517
518 res = nf_ct_frag6_sysctl_register(net);
519 if (res < 0)
520 fqdir_exit(nf_frag->fqdir);
521 return res;
522 }
523
nf_ct_net_pre_exit(struct net * net)524 static void nf_ct_net_pre_exit(struct net *net)
525 {
526 struct nft_ct_frag6_pernet *nf_frag = nf_frag_pernet(net);
527
528 fqdir_pre_exit(nf_frag->fqdir);
529 }
530
nf_ct_net_exit(struct net * net)531 static void nf_ct_net_exit(struct net *net)
532 {
533 struct nft_ct_frag6_pernet *nf_frag = nf_frag_pernet(net);
534
535 nf_ct_frags6_sysctl_unregister(net);
536 fqdir_exit(nf_frag->fqdir);
537 }
538
539 static struct pernet_operations nf_ct_net_ops = {
540 .init = nf_ct_net_init,
541 .pre_exit = nf_ct_net_pre_exit,
542 .exit = nf_ct_net_exit,
543 .id = &nf_frag_pernet_id,
544 .size = sizeof(struct nft_ct_frag6_pernet),
545 };
546
547 static const struct rhashtable_params nfct_rhash_params = {
548 .head_offset = offsetof(struct inet_frag_queue, node),
549 .hashfn = ip6frag_key_hashfn,
550 .obj_hashfn = ip6frag_obj_hashfn,
551 .obj_cmpfn = ip6frag_obj_cmpfn,
552 .automatic_shrinking = true,
553 };
554
nf_ct_frag6_init(void)555 int nf_ct_frag6_init(void)
556 {
557 int ret = 0;
558
559 nf_frags.constructor = ip6frag_init;
560 nf_frags.destructor = NULL;
561 nf_frags.qsize = sizeof(struct frag_queue);
562 nf_frags.frag_expire = nf_ct_frag6_expire;
563 nf_frags.frags_cache_name = nf_frags_cache_name;
564 nf_frags.rhash_params = nfct_rhash_params;
565 ret = inet_frags_init(&nf_frags);
566 if (ret)
567 goto out;
568 ret = register_pernet_subsys(&nf_ct_net_ops);
569 if (ret)
570 inet_frags_fini(&nf_frags);
571
572 out:
573 return ret;
574 }
575
nf_ct_frag6_cleanup(void)576 void nf_ct_frag6_cleanup(void)
577 {
578 unregister_pernet_subsys(&nf_ct_net_ops);
579 inet_frags_fini(&nf_frags);
580 }
581