linux/net/ipv6/netfilter/nf_conntrack_reasm.c
<<
>>
Prefs
   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/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>
  29
  30#include <net/sock.h>
  31#include <net/snmp.h>
  32#include <net/ipv6_frag.h>
  33
  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>
  47
  48static const char nf_frags_cache_name[] = "nf-frags";
  49
  50static struct inet_frags nf_frags;
  51
  52#ifdef CONFIG_SYSCTL
  53
  54static struct ctl_table nf_ct_frag6_sysctl_table[] = {
  55        {
  56                .procname       = "nf_conntrack_frag6_timeout",
  57                .data           = &init_net.nf_frag.frags.timeout,
  58                .maxlen         = sizeof(unsigned int),
  59                .mode           = 0644,
  60                .proc_handler   = proc_dointvec_jiffies,
  61        },
  62        {
  63                .procname       = "nf_conntrack_frag6_low_thresh",
  64                .data           = &init_net.nf_frag.frags.low_thresh,
  65                .maxlen         = sizeof(unsigned long),
  66                .mode           = 0644,
  67                .proc_handler   = proc_doulongvec_minmax,
  68                .extra2         = &init_net.nf_frag.frags.high_thresh
  69        },
  70        {
  71                .procname       = "nf_conntrack_frag6_high_thresh",
  72                .data           = &init_net.nf_frag.frags.high_thresh,
  73                .maxlen         = sizeof(unsigned long),
  74                .mode           = 0644,
  75                .proc_handler   = proc_doulongvec_minmax,
  76                .extra1         = &init_net.nf_frag.frags.low_thresh
  77        },
  78        { }
  79};
  80
  81static int nf_ct_frag6_sysctl_register(struct net *net)
  82{
  83        struct ctl_table *table;
  84        struct ctl_table_header *hdr;
  85
  86        table = nf_ct_frag6_sysctl_table;
  87        if (!net_eq(net, &init_net)) {
  88                table = kmemdup(table, sizeof(nf_ct_frag6_sysctl_table),
  89                                GFP_KERNEL);
  90                if (table == NULL)
  91                        goto err_alloc;
  92
  93                table[0].data = &net->nf_frag.frags.timeout;
  94                table[1].data = &net->nf_frag.frags.low_thresh;
  95                table[1].extra2 = &net->nf_frag.frags.high_thresh;
  96                table[2].data = &net->nf_frag.frags.high_thresh;
  97                table[2].extra1 = &net->nf_frag.frags.low_thresh;
  98                table[2].extra2 = &init_net.nf_frag.frags.high_thresh;
  99        }
 100
 101        hdr = register_net_sysctl(net, "net/netfilter", table);
 102        if (hdr == NULL)
 103                goto err_reg;
 104
 105        net->nf_frag_frags_hdr = hdr;
 106        return 0;
 107
 108err_reg:
 109        if (!net_eq(net, &init_net))
 110                kfree(table);
 111err_alloc:
 112        return -ENOMEM;
 113}
 114
 115static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net)
 116{
 117        struct ctl_table *table;
 118
 119        table = net->nf_frag_frags_hdr->ctl_table_arg;
 120        unregister_net_sysctl_table(net->nf_frag_frags_hdr);
 121        if (!net_eq(net, &init_net))
 122                kfree(table);
 123}
 124
 125#else
 126static int nf_ct_frag6_sysctl_register(struct net *net)
 127{
 128        return 0;
 129}
 130static void __net_exit nf_ct_frags6_sysctl_unregister(struct net *net)
 131{
 132}
 133#endif
 134
 135static int nf_ct_frag6_reasm(struct frag_queue *fq, struct sk_buff *skb,
 136                             struct sk_buff *prev_tail, struct net_device *dev);
 137
 138static inline u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
 139{
 140        return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
 141}
 142
 143static void nf_ct_frag6_expire(struct timer_list *t)
 144{
 145        struct inet_frag_queue *frag = from_timer(frag, t, timer);
 146        struct frag_queue *fq;
 147        struct net *net;
 148
 149        fq = container_of(frag, struct frag_queue, q);
 150        net = container_of(fq->q.net, struct net, nf_frag.frags);
 151
 152        ip6frag_expire_frag_queue(net, fq);
 153}
 154
 155/* Creation primitives. */
 156static struct frag_queue *fq_find(struct net *net, __be32 id, u32 user,
 157                                  const struct ipv6hdr *hdr, int iif)
 158{
 159        struct frag_v6_compare_key key = {
 160                .id = id,
 161                .saddr = hdr->saddr,
 162                .daddr = hdr->daddr,
 163                .user = user,
 164                .iif = iif,
 165        };
 166        struct inet_frag_queue *q;
 167
 168        q = inet_frag_find(&net->nf_frag.frags, &key);
 169        if (!q)
 170                return NULL;
 171
 172        return container_of(q, struct frag_queue, q);
 173}
 174
 175
 176static int nf_ct_frag6_queue(struct frag_queue *fq, struct sk_buff *skb,
 177                             const struct frag_hdr *fhdr, int nhoff)
 178{
 179        unsigned int payload_len;
 180        struct net_device *dev;
 181        struct sk_buff *prev;
 182        int offset, end, err;
 183        u8 ecn;
 184
 185        if (fq->q.flags & INET_FRAG_COMPLETE) {
 186                pr_debug("Already completed\n");
 187                goto err;
 188        }
 189
 190        payload_len = ntohs(ipv6_hdr(skb)->payload_len);
 191
 192        offset = ntohs(fhdr->frag_off) & ~0x7;
 193        end = offset + (payload_len -
 194                        ((u8 *)(fhdr + 1) - (u8 *)(ipv6_hdr(skb) + 1)));
 195
 196        if ((unsigned int)end > IPV6_MAXPLEN) {
 197                pr_debug("offset is too large.\n");
 198                return -EINVAL;
 199        }
 200
 201        ecn = ip6_frag_ecn(ipv6_hdr(skb));
 202
 203        if (skb->ip_summed == CHECKSUM_COMPLETE) {
 204                const unsigned char *nh = skb_network_header(skb);
 205                skb->csum = csum_sub(skb->csum,
 206                                     csum_partial(nh, (u8 *)(fhdr + 1) - nh,
 207                                                  0));
 208        }
 209
 210        /* Is this the final fragment? */
 211        if (!(fhdr->frag_off & htons(IP6_MF))) {
 212                /* If we already have some bits beyond end
 213                 * or have different end, the segment is corrupted.
 214                 */
 215                if (end < fq->q.len ||
 216                    ((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len)) {
 217                        pr_debug("already received last fragment\n");
 218                        goto err;
 219                }
 220                fq->q.flags |= INET_FRAG_LAST_IN;
 221                fq->q.len = end;
 222        } else {
 223                /* Check if the fragment is rounded to 8 bytes.
 224                 * Required by the RFC.
 225                 */
 226                if (end & 0x7) {
 227                        /* RFC2460 says always send parameter problem in
 228                         * this case. -DaveM
 229                         */
 230                        pr_debug("end of fragment not rounded to 8 bytes.\n");
 231                        inet_frag_kill(&fq->q);
 232                        return -EPROTO;
 233                }
 234                if (end > fq->q.len) {
 235                        /* Some bits beyond end -> corruption. */
 236                        if (fq->q.flags & INET_FRAG_LAST_IN) {
 237                                pr_debug("last packet already reached.\n");
 238                                goto err;
 239                        }
 240                        fq->q.len = end;
 241                }
 242        }
 243
 244        if (end == offset)
 245                goto err;
 246
 247        /* Point into the IP datagram 'data' part. */
 248        if (!pskb_pull(skb, (u8 *) (fhdr + 1) - skb->data)) {
 249                pr_debug("queue: message is too short.\n");
 250                goto err;
 251        }
 252        if (pskb_trim_rcsum(skb, end - offset)) {
 253                pr_debug("Can't trim\n");
 254                goto err;
 255        }
 256
 257        /* Note : skb->rbnode and skb->dev share the same location. */
 258        dev = skb->dev;
 259        /* Makes sure compiler wont do silly aliasing games */
 260        barrier();
 261
 262        prev = fq->q.fragments_tail;
 263        err = inet_frag_queue_insert(&fq->q, skb, offset, end);
 264        if (err) {
 265                if (err == IPFRAG_DUP) {
 266                        /* No error for duplicates, pretend they got queued. */
 267                        kfree_skb(skb);
 268                        return -EINPROGRESS;
 269                }
 270                goto insert_error;
 271        }
 272
 273        if (dev)
 274                fq->iif = dev->ifindex;
 275
 276        fq->q.stamp = skb->tstamp;
 277        fq->q.meat += skb->len;
 278        fq->ecn |= ecn;
 279        if (payload_len > fq->q.max_size)
 280                fq->q.max_size = payload_len;
 281        add_frag_mem_limit(fq->q.net, skb->truesize);
 282
 283        /* The first fragment.
 284         * nhoffset is obtained from the first fragment, of course.
 285         */
 286        if (offset == 0) {
 287                fq->nhoffset = nhoff;
 288                fq->q.flags |= INET_FRAG_FIRST_IN;
 289        }
 290
 291        if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
 292            fq->q.meat == fq->q.len) {
 293                unsigned long orefdst = skb->_skb_refdst;
 294
 295                skb->_skb_refdst = 0UL;
 296                err = nf_ct_frag6_reasm(fq, skb, prev, dev);
 297                skb->_skb_refdst = orefdst;
 298
 299                /* After queue has assumed skb ownership, only 0 or
 300                 * -EINPROGRESS must be returned.
 301                 */
 302                return err ? -EINPROGRESS : 0;
 303        }
 304
 305        skb_dst_drop(skb);
 306        return -EINPROGRESS;
 307
 308insert_error:
 309        inet_frag_kill(&fq->q);
 310err:
 311        skb_dst_drop(skb);
 312        return -EINVAL;
 313}
 314
 315/*
 316 *      Check if this packet is complete.
 317 *
 318 *      It is called with locked fq, and caller must check that
 319 *      queue is eligible for reassembly i.e. it is not COMPLETE,
 320 *      the last and the first frames arrived and all the bits are here.
 321 */
 322static int nf_ct_frag6_reasm(struct frag_queue *fq, struct sk_buff *skb,
 323                             struct sk_buff *prev_tail, struct net_device *dev)
 324{
 325        void *reasm_data;
 326        int payload_len;
 327        u8 ecn;
 328
 329        inet_frag_kill(&fq->q);
 330
 331        ecn = ip_frag_ecn_table[fq->ecn];
 332        if (unlikely(ecn == 0xff))
 333                goto err;
 334
 335        reasm_data = inet_frag_reasm_prepare(&fq->q, skb, prev_tail);
 336        if (!reasm_data)
 337                goto err;
 338
 339        payload_len = ((skb->data - skb_network_header(skb)) -
 340                       sizeof(struct ipv6hdr) + fq->q.len -
 341                       sizeof(struct frag_hdr));
 342        if (payload_len > IPV6_MAXPLEN) {
 343                net_dbg_ratelimited("nf_ct_frag6_reasm: payload len = %d\n",
 344                                    payload_len);
 345                goto err;
 346        }
 347
 348        /* We have to remove fragment header from datagram and to relocate
 349         * header in order to calculate ICV correctly. */
 350        skb_network_header(skb)[fq->nhoffset] = skb_transport_header(skb)[0];
 351        memmove(skb->head + sizeof(struct frag_hdr), skb->head,
 352                (skb->data - skb->head) - sizeof(struct frag_hdr));
 353        skb->mac_header += sizeof(struct frag_hdr);
 354        skb->network_header += sizeof(struct frag_hdr);
 355
 356        skb_reset_transport_header(skb);
 357
 358        inet_frag_reasm_finish(&fq->q, skb, reasm_data);
 359
 360        skb->ignore_df = 1;
 361        skb->dev = dev;
 362        ipv6_hdr(skb)->payload_len = htons(payload_len);
 363        ipv6_change_dsfield(ipv6_hdr(skb), 0xff, ecn);
 364        IP6CB(skb)->frag_max_size = sizeof(struct ipv6hdr) + fq->q.max_size;
 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
 378err:
 379        inet_frag_kill(&fq->q);
 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 */
 396static int
 397find_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                        BUG();
 423                if (nexthdr == NEXTHDR_AUTH)
 424                        hdrlen = (hdr.hdrlen+2)<<2;
 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
 446int nf_ct_frag6_gather(struct net *net, struct sk_buff *skb, u32 user)
 447{
 448        u16 savethdr = skb->transport_header;
 449        int fhoff, nhoff, ret;
 450        struct frag_hdr *fhdr;
 451        struct frag_queue *fq;
 452        struct ipv6hdr *hdr;
 453        u8 prevhdr;
 454
 455        /* Jumbo payload inhibits frag. header */
 456        if (ipv6_hdr(skb)->payload_len == 0) {
 457                pr_debug("payload len = 0\n");
 458                return 0;
 459        }
 460
 461        if (find_prev_fhdr(skb, &prevhdr, &nhoff, &fhoff) < 0)
 462                return 0;
 463
 464        if (!pskb_may_pull(skb, fhoff + sizeof(*fhdr)))
 465                return -ENOMEM;
 466
 467        skb_set_transport_header(skb, fhoff);
 468        hdr = ipv6_hdr(skb);
 469        fhdr = (struct frag_hdr *)skb_transport_header(skb);
 470
 471        skb_orphan(skb);
 472        fq = fq_find(net, fhdr->identification, user, hdr,
 473                     skb->dev ? skb->dev->ifindex : 0);
 474        if (fq == NULL) {
 475                pr_debug("Can't find and can't create new queue\n");
 476                return -ENOMEM;
 477        }
 478
 479        spin_lock_bh(&fq->q.lock);
 480
 481        ret = nf_ct_frag6_queue(fq, skb, fhdr, nhoff);
 482        if (ret == -EPROTO) {
 483                skb->transport_header = savethdr;
 484                ret = 0;
 485        }
 486
 487        spin_unlock_bh(&fq->q.lock);
 488        inet_frag_put(&fq->q);
 489        return ret;
 490}
 491EXPORT_SYMBOL_GPL(nf_ct_frag6_gather);
 492
 493static int nf_ct_net_init(struct net *net)
 494{
 495        int res;
 496
 497        net->nf_frag.frags.high_thresh = IPV6_FRAG_HIGH_THRESH;
 498        net->nf_frag.frags.low_thresh = IPV6_FRAG_LOW_THRESH;
 499        net->nf_frag.frags.timeout = IPV6_FRAG_TIMEOUT;
 500        net->nf_frag.frags.f = &nf_frags;
 501
 502        res = inet_frags_init_net(&net->nf_frag.frags);
 503        if (res < 0)
 504                return res;
 505        res = nf_ct_frag6_sysctl_register(net);
 506        if (res < 0)
 507                inet_frags_exit_net(&net->nf_frag.frags);
 508        return res;
 509}
 510
 511static void nf_ct_net_exit(struct net *net)
 512{
 513        nf_ct_frags6_sysctl_unregister(net);
 514        inet_frags_exit_net(&net->nf_frag.frags);
 515}
 516
 517static struct pernet_operations nf_ct_net_ops = {
 518        .init = nf_ct_net_init,
 519        .exit = nf_ct_net_exit,
 520};
 521
 522static const struct rhashtable_params nfct_rhash_params = {
 523        .head_offset            = offsetof(struct inet_frag_queue, node),
 524        .hashfn                 = ip6frag_key_hashfn,
 525        .obj_hashfn             = ip6frag_obj_hashfn,
 526        .obj_cmpfn              = ip6frag_obj_cmpfn,
 527        .automatic_shrinking    = true,
 528};
 529
 530int nf_ct_frag6_init(void)
 531{
 532        int ret = 0;
 533
 534        nf_frags.constructor = ip6frag_init;
 535        nf_frags.destructor = NULL;
 536        nf_frags.qsize = sizeof(struct frag_queue);
 537        nf_frags.frag_expire = nf_ct_frag6_expire;
 538        nf_frags.frags_cache_name = nf_frags_cache_name;
 539        nf_frags.rhash_params = nfct_rhash_params;
 540        ret = inet_frags_init(&nf_frags);
 541        if (ret)
 542                goto out;
 543        ret = register_pernet_subsys(&nf_ct_net_ops);
 544        if (ret)
 545                inet_frags_fini(&nf_frags);
 546
 547out:
 548        return ret;
 549}
 550
 551void nf_ct_frag6_cleanup(void)
 552{
 553        unregister_pernet_subsys(&nf_ct_net_ops);
 554        inet_frags_fini(&nf_frags);
 555}
 556