1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IPv6 fragment reassembly for connection tracking
5 * Copyright (C)2004 USAGI/WIDE Project
8 * Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
10 * Based on: net/ipv6/reassembly.c
13 #define pr_fmt(fmt) "IPv6-nf: " fmt
15 #include <linux/errno.h>
16 #include <linux/types.h>
17 #include <linux/string.h>
18 #include <linux/socket.h>
19 #include <linux/sockios.h>
20 #include <linux/jiffies.h>
21 #include <linux/net.h>
22 #include <linux/list.h>
23 #include <linux/netdevice.h>
24 #include <linux/in6.h>
25 #include <linux/ipv6.h>
26 #include <linux/icmpv6.h>
27 #include <linux/random.h>
28 #include <linux/slab.h>
32 #include <net/ipv6_frag.h>
34 #include <net/protocol.h>
35 #include <net/transp_v6.h>
36 #include <net/rawv6.h>
37 #include <net/ndisc.h>
38 #include <net/addrconf.h>
39 #include <net/inet_ecn.h>
40 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
41 #include <linux/sysctl.h>
42 #include <linux/netfilter.h>
43 #include <linux/netfilter_ipv6.h>
44 #include <linux/kernel.h>
45 #include <linux/module.h>
46 #include <net/netfilter/ipv6/nf_defrag_ipv6.h>
48 static const char nf_frags_cache_name[] = "nf-frags";
50 static struct inet_frags nf_frags;
54 static struct ctl_table nf_ct_frag6_sysctl_table[] = {
56 .procname = "nf_conntrack_frag6_timeout",
57 .maxlen = sizeof(unsigned int),
59 .proc_handler = proc_dointvec_jiffies,
62 .procname = "nf_conntrack_frag6_low_thresh",
63 .maxlen = sizeof(unsigned long),
65 .proc_handler = proc_doulongvec_minmax,
68 .procname = "nf_conntrack_frag6_high_thresh",
69 .maxlen = sizeof(unsigned long),
71 .proc_handler = proc_doulongvec_minmax,
76 static int nf_ct_frag6_sysctl_register(struct net *net)
78 struct ctl_table *table;
79 struct ctl_table_header *hdr;
81 table = nf_ct_frag6_sysctl_table;
82 if (!net_eq(net, &init_net)) {
83 table = kmemdup(table, sizeof(nf_ct_frag6_sysctl_table),
89 table[0].data = &net->nf_frag.fqdir->timeout;
90 table[1].data = &net->nf_frag.fqdir->low_thresh;
91 table[1].extra2 = &net->nf_frag.fqdir->high_thresh;
92 table[2].data = &net->nf_frag.fqdir->high_thresh;
93 table[2].extra1 = &net->nf_frag.fqdir->low_thresh;
94 table[2].extra2 = &init_net.nf_frag.fqdir->high_thresh;
96 hdr = register_net_sysctl(net, "net/netfilter", table);
100 net->nf_frag_frags_hdr = hdr;
104 if (!net_eq(net, &init_net))
110 static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net)
112 struct ctl_table *table;
114 table = net->nf_frag_frags_hdr->ctl_table_arg;
115 unregister_net_sysctl_table(net->nf_frag_frags_hdr);
116 if (!net_eq(net, &init_net))
121 static int nf_ct_frag6_sysctl_register(struct net *net)
125 static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net)
130 static int nf_ct_frag6_reasm(struct frag_queue *fq, struct sk_buff *skb,
131 struct sk_buff *prev_tail, struct net_device *dev);
133 static inline u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
135 return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
138 static void nf_ct_frag6_expire(struct timer_list *t)
140 struct inet_frag_queue *frag = from_timer(frag, t, timer);
141 struct frag_queue *fq;
143 fq = container_of(frag, struct frag_queue, q);
145 ip6frag_expire_frag_queue(fq->q.fqdir->net, fq);
148 /* Creation primitives. */
149 static struct frag_queue *fq_find(struct net *net, __be32 id, u32 user,
150 const struct ipv6hdr *hdr, int iif)
152 struct frag_v6_compare_key key = {
159 struct inet_frag_queue *q;
161 q = inet_frag_find(net->nf_frag.fqdir, &key);
165 return container_of(q, struct frag_queue, q);
169 static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb,
170 const struct frag_hdr *fhdr, int nhoff)
172 unsigned int payload_len;
173 struct net_device *dev;
174 struct sk_buff *prev;
175 int offset, end, err;
178 if (fq->q.flags & INET_FRAG_COMPLETE) {
179 pr_debug("Already completed\n");
183 payload_len = ntohs(ipv6_hdr(skb)->payload_len);
185 offset = ntohs(fhdr->frag_off) & ~0x7;
186 end = offset + (payload_len -
187 ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
189 if ((unsigned int)end > IPV6_MAXPLEN) {
190 pr_debug("offset is too large.\n");
194 ecn = ip6_frag_ecn(ipv6_hdr(skb));
196 if (skb->ip_summed == CHECKSUM_COMPLETE) {
197 const unsigned char *nh = skb_network_header(skb);
198 skb->csum = csum_sub(skb->csum,
199 csum_partial(nh, (u8 *)(fhdr + 1) - nh,
203 /* Is this the final fragment? */
204 if (!(fhdr->frag_off & htons(IP6_MF))) {
205 /* If we already have some bits beyond end
206 * or have different end, the segment is corrupted.
208 if (end < fq->q.len ||
209 ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len)) {
210 pr_debug("already received last fragment\n");
213 fq->q.flags |= INET_FRAG_LAST_IN;
216 /* Check if the fragment is rounded to 8 bytes.
217 * Required by the RFC.
220 /* RFC2460 says always send parameter problem in
223 pr_debug("end of fragment not rounded to 8 bytes.\n");
224 inet_frag_kill(&fq->q);
227 if (end > fq->q.len) {
228 /* Some bits beyond end -> corruption. */
229 if (fq->q.flags & INET_FRAG_LAST_IN) {
230 pr_debug("last packet already reached.\n");
240 /* Point into the IP datagram 'data' part. */
241 if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
242 pr_debug("queue: message is too short.\n");
245 if (pskb_trim_rcsum(skb, end - offset)) {
246 pr_debug("Can't trim\n");
250 /* Note : skb->rbnode and skb->dev share the same location. */
252 /* Makes sure compiler wont do silly aliasing games */
255 prev = fq->q.fragments_tail;
256 err = inet_frag_queue_insert(&fq->q, skb, offset, end);
258 if (err == IPFRAG_DUP) {
259 /* No error for duplicates, pretend they got queued. */
267 fq->iif = dev->ifindex;
269 fq->q.stamp = skb->tstamp;
270 fq->q.meat += skb->len;
272 if (payload_len > fq->q.max_size)
273 fq->q.max_size = payload_len;
274 add_frag_mem_limit(fq->q.fqdir, skb->truesize);
276 /* The first fragment.
277 * nhoffset is obtained from the first fragment, of course.
280 fq->nhoffset = nhoff;
281 fq->q.flags |= INET_FRAG_FIRST_IN;
284 if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
285 fq->q.meat == fq->q.len) {
286 unsigned long orefdst = skb->_skb_refdst;
288 skb->_skb_refdst = 0UL;
289 err = nf_ct_frag6_reasm(fq, skb, prev, dev);
290 skb->_skb_refdst = orefdst;
292 /* After queue has assumed skb ownership, only 0 or
293 * -EINPROGRESS must be returned.
295 return err ? -EINPROGRESS : 0;
302 inet_frag_kill(&fq->q);
309 * Check if this packet is complete.
311 * It is called with locked fq, and caller must check that
312 * queue is eligible for reassembly i.e. it is not COMPLETE,
313 * the last and the first frames arrived and all the bits are here.
315 static int nf_ct_frag6_reasm(struct frag_queue *fq, struct sk_buff *skb,
316 struct sk_buff *prev_tail, struct net_device *dev)
322 inet_frag_kill(&fq->q);
324 ecn = ip_frag_ecn_table[fq->ecn];
325 if (unlikely(ecn == 0xff))
328 reasm_data = inet_frag_reasm_prepare(&fq->q, skb, prev_tail);
332 payload_len = ((skb->data - skb_network_header(skb)) -
333 sizeof(struct ipv6hdr) + fq->q.len -
334 sizeof(struct frag_hdr));
335 if (payload_len > IPV6_MAXPLEN) {
336 net_dbg_ratelimited("nf_ct_frag6_reasm: payload len = %d\n",
341 /* We have to remove fragment header from datagram and to relocate
342 * header in order to calculate ICV correctly. */
343 skb_network_header(skb)[fq->nhoffset] = skb_transport_header(skb)[0];
344 memmove(skb->head + sizeof(struct frag_hdr), skb->head,
345 (skb->data - skb->head) - sizeof(struct frag_hdr));
346 skb->mac_header += sizeof(struct frag_hdr);
347 skb->network_header += sizeof(struct frag_hdr);
349 skb_reset_transport_header(skb);
351 inet_frag_reasm_finish(&fq->q, skb, reasm_data, false);
355 ipv6_hdr(skb)->payload_len = htons(payload_len);
356 ipv6_change_dsfield(ipv6_hdr(skb), 0xff, ecn);
357 IP6CB(skb)->frag_max_size = sizeof(struct ipv6hdr) + fq->q.max_size;
358 IP6CB(skb)->flags |= IP6SKB_FRAGMENTED;
360 /* Yes, and fold redundant checksum back. 8) */
361 if (skb->ip_summed == CHECKSUM_COMPLETE)
362 skb->csum = csum_partial(skb_network_header(skb),
363 skb_network_header_len(skb),
366 fq->q.rb_fragments = RB_ROOT;
367 fq->q.fragments_tail = NULL;
368 fq->q.last_run_head = NULL;
373 inet_frag_kill(&fq->q);
378 * find the header just before Fragment Header.
380 * if success return 0 and set ...
381 * (*prevhdrp): the value of "Next Header Field" in the header
382 * just before Fragment Header.
383 * (*prevhoff): the offset of "Next Header Field" in the header
384 * just before Fragment Header.
385 * (*fhoff) : the offset of Fragment Header.
387 * Based on ipv6_skip_hdr() in net/ipv6/exthdr.c
391 find_prev_fhdr(struct sk_buff *skb, u8 *prevhdrp, int *prevhoff, int *fhoff)
393 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
394 const int netoff = skb_network_offset(skb);
395 u8 prev_nhoff = netoff + offsetof(struct ipv6hdr, nexthdr);
396 int start = netoff + sizeof(struct ipv6hdr);
397 int len = skb->len - start;
398 u8 prevhdr = NEXTHDR_IPV6;
400 while (nexthdr != NEXTHDR_FRAGMENT) {
401 struct ipv6_opt_hdr hdr;
404 if (!ipv6_ext_hdr(nexthdr)) {
407 if (nexthdr == NEXTHDR_NONE) {
408 pr_debug("next header is none\n");
411 if (len < (int)sizeof(struct ipv6_opt_hdr)) {
412 pr_debug("too short\n");
415 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
417 if (nexthdr == NEXTHDR_AUTH)
418 hdrlen = ipv6_authlen(&hdr);
420 hdrlen = ipv6_optlen(&hdr);
425 nexthdr = hdr.nexthdr;
434 *prevhoff = prev_nhoff;
440 int nf_ct_frag6_gather(struct net *net, struct sk_buff *skb, u32 user)
442 u16 savethdr = skb->transport_header;
443 u8 nexthdr = NEXTHDR_FRAGMENT;
444 int fhoff, nhoff, ret;
445 struct frag_hdr *fhdr;
446 struct frag_queue *fq;
450 /* Jumbo payload inhibits frag. header */
451 if (ipv6_hdr(skb)->payload_len == 0) {
452 pr_debug("payload len = 0\n");
456 if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
459 /* Discard the first fragment if it does not include all headers
460 * RFC 8200, Section 4.5
462 if (ipv6frag_thdr_truncated(skb, fhoff, &nexthdr)) {
463 pr_debug("Drop incomplete fragment\n");
467 if (!pskb_may_pull(skb, fhoff + sizeof(*fhdr)))
470 skb_set_transport_header(skb, fhoff);
472 fhdr = (struct frag_hdr *)skb_transport_header(skb);
475 fq = fq_find(net, fhdr->identification, user, hdr,
476 skb->dev ? skb->dev->ifindex : 0);
478 pr_debug("Can't find and can't create new queue\n");
482 spin_lock_bh(&fq->q.lock);
484 ret = nf_ct_frag6_queue(fq, skb, fhdr, nhoff);
485 if (ret == -EPROTO) {
486 skb->transport_header = savethdr;
490 spin_unlock_bh(&fq->q.lock);
491 inet_frag_put(&fq->q);
494 EXPORT_SYMBOL_GPL(nf_ct_frag6_gather);
496 static int nf_ct_net_init(struct net *net)
500 res = fqdir_init(&net->nf_frag.fqdir, &nf_frags, net);
504 net->nf_frag.fqdir->high_thresh = IPV6_FRAG_HIGH_THRESH;
505 net->nf_frag.fqdir->low_thresh = IPV6_FRAG_LOW_THRESH;
506 net->nf_frag.fqdir->timeout = IPV6_FRAG_TIMEOUT;
508 res = nf_ct_frag6_sysctl_register(net);
510 fqdir_exit(net->nf_frag.fqdir);
514 static void nf_ct_net_pre_exit(struct net *net)
516 fqdir_pre_exit(net->nf_frag.fqdir);
519 static void nf_ct_net_exit(struct net *net)
521 nf_ct_frags6_sysctl_unregister(net);
522 fqdir_exit(net->nf_frag.fqdir);
525 static struct pernet_operations nf_ct_net_ops = {
526 .init = nf_ct_net_init,
527 .pre_exit = nf_ct_net_pre_exit,
528 .exit = nf_ct_net_exit,
531 static const struct rhashtable_params nfct_rhash_params = {
532 .head_offset = offsetof(struct inet_frag_queue, node),
533 .hashfn = ip6frag_key_hashfn,
534 .obj_hashfn = ip6frag_obj_hashfn,
535 .obj_cmpfn = ip6frag_obj_cmpfn,
536 .automatic_shrinking = true,
539 int nf_ct_frag6_init(void)
543 nf_frags.constructor = ip6frag_init;
544 nf_frags.destructor = NULL;
545 nf_frags.qsize = sizeof(struct frag_queue);
546 nf_frags.frag_expire = nf_ct_frag6_expire;
547 nf_frags.frags_cache_name = nf_frags_cache_name;
548 nf_frags.rhash_params = nfct_rhash_params;
549 ret = inet_frags_init(&nf_frags);
552 ret = register_pernet_subsys(&nf_ct_net_ops);
554 inet_frags_fini(&nf_frags);
560 void nf_ct_frag6_cleanup(void)
562 unregister_pernet_subsys(&nf_ct_net_ops);
563 inet_frags_fini(&nf_frags);