linux/net/netfilter/nf_conntrack_proto.c
<<
>>
Prefs
   1/* L3/L4 protocol support for nf_conntrack. */
   2
   3/* (C) 1999-2001 Paul `Rusty' Russell
   4 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
   5 * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org>
   6 * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License version 2 as
  10 * published by the Free Software Foundation.
  11 */
  12
  13#include <linux/types.h>
  14#include <linux/netfilter.h>
  15#include <linux/module.h>
  16#include <linux/slab.h>
  17#include <linux/mutex.h>
  18#include <linux/vmalloc.h>
  19#include <linux/stddef.h>
  20#include <linux/err.h>
  21#include <linux/percpu.h>
  22#include <linux/notifier.h>
  23#include <linux/kernel.h>
  24#include <linux/netdevice.h>
  25
  26#include <net/netfilter/nf_conntrack.h>
  27#include <net/netfilter/nf_conntrack_l3proto.h>
  28#include <net/netfilter/nf_conntrack_l4proto.h>
  29#include <net/netfilter/nf_conntrack_core.h>
  30#include <net/netfilter/nf_log.h>
  31
  32static struct nf_conntrack_l4proto __rcu **nf_ct_protos[NFPROTO_NUMPROTO] __read_mostly;
  33struct nf_conntrack_l3proto __rcu *nf_ct_l3protos[NFPROTO_NUMPROTO] __read_mostly;
  34EXPORT_SYMBOL_GPL(nf_ct_l3protos);
  35
  36static DEFINE_MUTEX(nf_ct_proto_mutex);
  37
  38#ifdef CONFIG_SYSCTL
  39static int
  40nf_ct_register_sysctl(struct net *net,
  41                      struct ctl_table_header **header,
  42                      const char *path,
  43                      struct ctl_table *table)
  44{
  45        if (*header == NULL) {
  46                *header = register_net_sysctl(net, path, table);
  47                if (*header == NULL)
  48                        return -ENOMEM;
  49        }
  50
  51        return 0;
  52}
  53
  54static void
  55nf_ct_unregister_sysctl(struct ctl_table_header **header,
  56                        struct ctl_table **table,
  57                        unsigned int users)
  58{
  59        if (users > 0)
  60                return;
  61
  62        unregister_net_sysctl_table(*header);
  63        kfree(*table);
  64        *header = NULL;
  65        *table = NULL;
  66}
  67
  68__printf(5, 6)
  69void nf_l4proto_log_invalid(const struct sk_buff *skb,
  70                            struct net *net,
  71                            u16 pf, u8 protonum,
  72                            const char *fmt, ...)
  73{
  74        struct va_format vaf;
  75        va_list args;
  76
  77        if (net->ct.sysctl_log_invalid != protonum &&
  78            net->ct.sysctl_log_invalid != IPPROTO_RAW)
  79                return;
  80
  81        va_start(args, fmt);
  82        vaf.fmt = fmt;
  83        vaf.va = &args;
  84
  85        nf_log_packet(net, pf, 0, skb, NULL, NULL, NULL,
  86                      "nf_ct_proto_%d: %pV ", protonum, &vaf);
  87        va_end(args);
  88}
  89EXPORT_SYMBOL_GPL(nf_l4proto_log_invalid);
  90
  91__printf(3, 4)
  92void nf_ct_l4proto_log_invalid(const struct sk_buff *skb,
  93                               const struct nf_conn *ct,
  94                               const char *fmt, ...)
  95{
  96        struct va_format vaf;
  97        struct net *net;
  98        va_list args;
  99
 100        net = nf_ct_net(ct);
 101        if (likely(net->ct.sysctl_log_invalid == 0))
 102                return;
 103
 104        va_start(args, fmt);
 105        vaf.fmt = fmt;
 106        vaf.va = &args;
 107
 108        nf_l4proto_log_invalid(skb, net, nf_ct_l3num(ct),
 109                               nf_ct_protonum(ct), "%pV", &vaf);
 110        va_end(args);
 111}
 112EXPORT_SYMBOL_GPL(nf_ct_l4proto_log_invalid);
 113#endif
 114
 115const struct nf_conntrack_l4proto *
 116__nf_ct_l4proto_find(u_int16_t l3proto, u_int8_t l4proto)
 117{
 118        if (unlikely(l3proto >= NFPROTO_NUMPROTO || nf_ct_protos[l3proto] == NULL))
 119                return &nf_conntrack_l4proto_generic;
 120
 121        return rcu_dereference(nf_ct_protos[l3proto][l4proto]);
 122}
 123EXPORT_SYMBOL_GPL(__nf_ct_l4proto_find);
 124
 125/* this is guaranteed to always return a valid protocol helper, since
 126 * it falls back to generic_protocol */
 127const struct nf_conntrack_l3proto *
 128nf_ct_l3proto_find_get(u_int16_t l3proto)
 129{
 130        struct nf_conntrack_l3proto *p;
 131
 132        rcu_read_lock();
 133        p = __nf_ct_l3proto_find(l3proto);
 134        if (!try_module_get(p->me))
 135                p = &nf_conntrack_l3proto_generic;
 136        rcu_read_unlock();
 137
 138        return p;
 139}
 140EXPORT_SYMBOL_GPL(nf_ct_l3proto_find_get);
 141
 142int
 143nf_ct_l3proto_try_module_get(unsigned short l3proto)
 144{
 145        const struct nf_conntrack_l3proto *p;
 146        int ret;
 147
 148retry:  p = nf_ct_l3proto_find_get(l3proto);
 149        if (p == &nf_conntrack_l3proto_generic) {
 150                ret = request_module("nf_conntrack-%d", l3proto);
 151                if (!ret)
 152                        goto retry;
 153
 154                return -EPROTOTYPE;
 155        }
 156
 157        return 0;
 158}
 159EXPORT_SYMBOL_GPL(nf_ct_l3proto_try_module_get);
 160
 161void nf_ct_l3proto_module_put(unsigned short l3proto)
 162{
 163        struct nf_conntrack_l3proto *p;
 164
 165        /* rcu_read_lock not necessary since the caller holds a reference, but
 166         * taken anyways to avoid lockdep warnings in __nf_ct_l3proto_find()
 167         */
 168        rcu_read_lock();
 169        p = __nf_ct_l3proto_find(l3proto);
 170        module_put(p->me);
 171        rcu_read_unlock();
 172}
 173EXPORT_SYMBOL_GPL(nf_ct_l3proto_module_put);
 174
 175static int nf_ct_netns_do_get(struct net *net, u8 nfproto)
 176{
 177        const struct nf_conntrack_l3proto *l3proto;
 178        int ret;
 179
 180        might_sleep();
 181
 182        ret = nf_ct_l3proto_try_module_get(nfproto);
 183        if (ret < 0)
 184                return ret;
 185
 186        /* we already have a reference, can't fail */
 187        rcu_read_lock();
 188        l3proto = __nf_ct_l3proto_find(nfproto);
 189        rcu_read_unlock();
 190
 191        if (!l3proto->net_ns_get)
 192                return 0;
 193
 194        ret = l3proto->net_ns_get(net);
 195        if (ret < 0)
 196                nf_ct_l3proto_module_put(nfproto);
 197
 198        return ret;
 199}
 200
 201int nf_ct_netns_get(struct net *net, u8 nfproto)
 202{
 203        int err;
 204
 205        if (nfproto == NFPROTO_INET) {
 206                err = nf_ct_netns_do_get(net, NFPROTO_IPV4);
 207                if (err < 0)
 208                        goto err1;
 209                err = nf_ct_netns_do_get(net, NFPROTO_IPV6);
 210                if (err < 0)
 211                        goto err2;
 212        } else {
 213                err = nf_ct_netns_do_get(net, nfproto);
 214                if (err < 0)
 215                        goto err1;
 216        }
 217        return 0;
 218
 219err2:
 220        nf_ct_netns_put(net, NFPROTO_IPV4);
 221err1:
 222        return err;
 223}
 224EXPORT_SYMBOL_GPL(nf_ct_netns_get);
 225
 226static void nf_ct_netns_do_put(struct net *net, u8 nfproto)
 227{
 228        const struct nf_conntrack_l3proto *l3proto;
 229
 230        might_sleep();
 231
 232        /* same as nf_conntrack_netns_get(), reference assumed */
 233        rcu_read_lock();
 234        l3proto = __nf_ct_l3proto_find(nfproto);
 235        rcu_read_unlock();
 236
 237        if (WARN_ON(!l3proto))
 238                return;
 239
 240        if (l3proto->net_ns_put)
 241                l3proto->net_ns_put(net);
 242
 243        nf_ct_l3proto_module_put(nfproto);
 244}
 245
 246void nf_ct_netns_put(struct net *net, uint8_t nfproto)
 247{
 248        if (nfproto == NFPROTO_INET) {
 249                nf_ct_netns_do_put(net, NFPROTO_IPV4);
 250                nf_ct_netns_do_put(net, NFPROTO_IPV6);
 251        } else
 252                nf_ct_netns_do_put(net, nfproto);
 253}
 254EXPORT_SYMBOL_GPL(nf_ct_netns_put);
 255
 256const struct nf_conntrack_l4proto *
 257nf_ct_l4proto_find_get(u_int16_t l3num, u_int8_t l4num)
 258{
 259        const struct nf_conntrack_l4proto *p;
 260
 261        rcu_read_lock();
 262        p = __nf_ct_l4proto_find(l3num, l4num);
 263        if (!try_module_get(p->me))
 264                p = &nf_conntrack_l4proto_generic;
 265        rcu_read_unlock();
 266
 267        return p;
 268}
 269EXPORT_SYMBOL_GPL(nf_ct_l4proto_find_get);
 270
 271void nf_ct_l4proto_put(const struct nf_conntrack_l4proto *p)
 272{
 273        module_put(p->me);
 274}
 275EXPORT_SYMBOL_GPL(nf_ct_l4proto_put);
 276
 277static int kill_l3proto(struct nf_conn *i, void *data)
 278{
 279        return nf_ct_l3num(i) == ((const struct nf_conntrack_l3proto *)data)->l3proto;
 280}
 281
 282static int kill_l4proto(struct nf_conn *i, void *data)
 283{
 284        const struct nf_conntrack_l4proto *l4proto;
 285        l4proto = data;
 286        return nf_ct_protonum(i) == l4proto->l4proto &&
 287               nf_ct_l3num(i) == l4proto->l3proto;
 288}
 289
 290int nf_ct_l3proto_register(const struct nf_conntrack_l3proto *proto)
 291{
 292        int ret = 0;
 293        struct nf_conntrack_l3proto *old;
 294
 295        if (proto->l3proto >= NFPROTO_NUMPROTO)
 296                return -EBUSY;
 297#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
 298        if (proto->tuple_to_nlattr && proto->nla_size == 0)
 299                return -EINVAL;
 300#endif
 301        mutex_lock(&nf_ct_proto_mutex);
 302        old = rcu_dereference_protected(nf_ct_l3protos[proto->l3proto],
 303                                        lockdep_is_held(&nf_ct_proto_mutex));
 304        if (old != &nf_conntrack_l3proto_generic) {
 305                ret = -EBUSY;
 306                goto out_unlock;
 307        }
 308
 309        rcu_assign_pointer(nf_ct_l3protos[proto->l3proto], proto);
 310
 311out_unlock:
 312        mutex_unlock(&nf_ct_proto_mutex);
 313        return ret;
 314
 315}
 316EXPORT_SYMBOL_GPL(nf_ct_l3proto_register);
 317
 318void nf_ct_l3proto_unregister(const struct nf_conntrack_l3proto *proto)
 319{
 320        BUG_ON(proto->l3proto >= NFPROTO_NUMPROTO);
 321
 322        mutex_lock(&nf_ct_proto_mutex);
 323        BUG_ON(rcu_dereference_protected(nf_ct_l3protos[proto->l3proto],
 324                                         lockdep_is_held(&nf_ct_proto_mutex)
 325                                         ) != proto);
 326        rcu_assign_pointer(nf_ct_l3protos[proto->l3proto],
 327                           &nf_conntrack_l3proto_generic);
 328        mutex_unlock(&nf_ct_proto_mutex);
 329
 330        synchronize_rcu();
 331        /* Remove all contrack entries for this protocol */
 332        nf_ct_iterate_destroy(kill_l3proto, (void*)proto);
 333}
 334EXPORT_SYMBOL_GPL(nf_ct_l3proto_unregister);
 335
 336static struct nf_proto_net *nf_ct_l4proto_net(struct net *net,
 337                                const struct nf_conntrack_l4proto *l4proto)
 338{
 339        if (l4proto->get_net_proto) {
 340                /* statically built-in protocols use static per-net */
 341                return l4proto->get_net_proto(net);
 342        } else if (l4proto->net_id) {
 343                /* ... and loadable protocols use dynamic per-net */
 344                return net_generic(net, *l4proto->net_id);
 345        }
 346        return NULL;
 347}
 348
 349static
 350int nf_ct_l4proto_register_sysctl(struct net *net,
 351                                  struct nf_proto_net *pn,
 352                                  const struct nf_conntrack_l4proto *l4proto)
 353{
 354        int err = 0;
 355
 356#ifdef CONFIG_SYSCTL
 357        if (pn->ctl_table != NULL) {
 358                err = nf_ct_register_sysctl(net,
 359                                            &pn->ctl_table_header,
 360                                            "net/netfilter",
 361                                            pn->ctl_table);
 362                if (err < 0) {
 363                        if (!pn->users) {
 364                                kfree(pn->ctl_table);
 365                                pn->ctl_table = NULL;
 366                        }
 367                }
 368        }
 369#endif /* CONFIG_SYSCTL */
 370        return err;
 371}
 372
 373static
 374void nf_ct_l4proto_unregister_sysctl(struct net *net,
 375                                struct nf_proto_net *pn,
 376                                const struct nf_conntrack_l4proto *l4proto)
 377{
 378#ifdef CONFIG_SYSCTL
 379        if (pn->ctl_table_header != NULL)
 380                nf_ct_unregister_sysctl(&pn->ctl_table_header,
 381                                        &pn->ctl_table,
 382                                        pn->users);
 383#endif /* CONFIG_SYSCTL */
 384}
 385
 386/* FIXME: Allow NULL functions and sub in pointers to generic for
 387   them. --RR */
 388int nf_ct_l4proto_register_one(const struct nf_conntrack_l4proto *l4proto)
 389{
 390        int ret = 0;
 391
 392        if (l4proto->l3proto >= ARRAY_SIZE(nf_ct_protos))
 393                return -EBUSY;
 394
 395        if ((l4proto->to_nlattr && l4proto->nlattr_size == 0) ||
 396            (l4proto->tuple_to_nlattr && !l4proto->nlattr_tuple_size))
 397                return -EINVAL;
 398
 399        mutex_lock(&nf_ct_proto_mutex);
 400        if (!nf_ct_protos[l4proto->l3proto]) {
 401                /* l3proto may be loaded latter. */
 402                struct nf_conntrack_l4proto __rcu **proto_array;
 403                int i;
 404
 405                proto_array =
 406                        kmalloc_array(MAX_NF_CT_PROTO,
 407                                      sizeof(struct nf_conntrack_l4proto *),
 408                                      GFP_KERNEL);
 409                if (proto_array == NULL) {
 410                        ret = -ENOMEM;
 411                        goto out_unlock;
 412                }
 413
 414                for (i = 0; i < MAX_NF_CT_PROTO; i++)
 415                        RCU_INIT_POINTER(proto_array[i],
 416                                         &nf_conntrack_l4proto_generic);
 417
 418                /* Before making proto_array visible to lockless readers,
 419                 * we must make sure its content is committed to memory.
 420                 */
 421                smp_wmb();
 422
 423                nf_ct_protos[l4proto->l3proto] = proto_array;
 424        } else if (rcu_dereference_protected(
 425                        nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
 426                        lockdep_is_held(&nf_ct_proto_mutex)
 427                        ) != &nf_conntrack_l4proto_generic) {
 428                ret = -EBUSY;
 429                goto out_unlock;
 430        }
 431
 432        rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
 433                           l4proto);
 434out_unlock:
 435        mutex_unlock(&nf_ct_proto_mutex);
 436        return ret;
 437}
 438EXPORT_SYMBOL_GPL(nf_ct_l4proto_register_one);
 439
 440int nf_ct_l4proto_pernet_register_one(struct net *net,
 441                                const struct nf_conntrack_l4proto *l4proto)
 442{
 443        int ret = 0;
 444        struct nf_proto_net *pn = NULL;
 445
 446        if (l4proto->init_net) {
 447                ret = l4proto->init_net(net, l4proto->l3proto);
 448                if (ret < 0)
 449                        goto out;
 450        }
 451
 452        pn = nf_ct_l4proto_net(net, l4proto);
 453        if (pn == NULL)
 454                goto out;
 455
 456        ret = nf_ct_l4proto_register_sysctl(net, pn, l4proto);
 457        if (ret < 0)
 458                goto out;
 459
 460        pn->users++;
 461out:
 462        return ret;
 463}
 464EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register_one);
 465
 466static void __nf_ct_l4proto_unregister_one(const struct nf_conntrack_l4proto *l4proto)
 467
 468{
 469        BUG_ON(l4proto->l3proto >= ARRAY_SIZE(nf_ct_protos));
 470
 471        BUG_ON(rcu_dereference_protected(
 472                        nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
 473                        lockdep_is_held(&nf_ct_proto_mutex)
 474                        ) != l4proto);
 475        rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto],
 476                           &nf_conntrack_l4proto_generic);
 477}
 478
 479void nf_ct_l4proto_unregister_one(const struct nf_conntrack_l4proto *l4proto)
 480{
 481        mutex_lock(&nf_ct_proto_mutex);
 482        __nf_ct_l4proto_unregister_one(l4proto);
 483        mutex_unlock(&nf_ct_proto_mutex);
 484
 485        synchronize_net();
 486        /* Remove all contrack entries for this protocol */
 487        nf_ct_iterate_destroy(kill_l4proto, (void *)l4proto);
 488}
 489EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister_one);
 490
 491void nf_ct_l4proto_pernet_unregister_one(struct net *net,
 492                                const struct nf_conntrack_l4proto *l4proto)
 493{
 494        struct nf_proto_net *pn = nf_ct_l4proto_net(net, l4proto);
 495
 496        if (pn == NULL)
 497                return;
 498
 499        pn->users--;
 500        nf_ct_l4proto_unregister_sysctl(net, pn, l4proto);
 501}
 502EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister_one);
 503
 504int nf_ct_l4proto_register(const struct nf_conntrack_l4proto * const l4proto[],
 505                           unsigned int num_proto)
 506{
 507        int ret = -EINVAL, ver;
 508        unsigned int i;
 509
 510        for (i = 0; i < num_proto; i++) {
 511                ret = nf_ct_l4proto_register_one(l4proto[i]);
 512                if (ret < 0)
 513                        break;
 514        }
 515        if (i != num_proto) {
 516                ver = l4proto[i]->l3proto == PF_INET6 ? 6 : 4;
 517                pr_err("nf_conntrack_ipv%d: can't register l4 %d proto.\n",
 518                       ver, l4proto[i]->l4proto);
 519                nf_ct_l4proto_unregister(l4proto, i);
 520        }
 521        return ret;
 522}
 523EXPORT_SYMBOL_GPL(nf_ct_l4proto_register);
 524
 525int nf_ct_l4proto_pernet_register(struct net *net,
 526                                  const struct nf_conntrack_l4proto *const l4proto[],
 527                                  unsigned int num_proto)
 528{
 529        int ret = -EINVAL;
 530        unsigned int i;
 531
 532        for (i = 0; i < num_proto; i++) {
 533                ret = nf_ct_l4proto_pernet_register_one(net, l4proto[i]);
 534                if (ret < 0)
 535                        break;
 536        }
 537        if (i != num_proto) {
 538                pr_err("nf_conntrack_proto_%d %d: pernet registration failed\n",
 539                       l4proto[i]->l4proto,
 540                       l4proto[i]->l3proto == PF_INET6 ? 6 : 4);
 541                nf_ct_l4proto_pernet_unregister(net, l4proto, i);
 542        }
 543        return ret;
 544}
 545EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register);
 546
 547void nf_ct_l4proto_unregister(const struct nf_conntrack_l4proto * const l4proto[],
 548                              unsigned int num_proto)
 549{
 550        int i;
 551
 552        mutex_lock(&nf_ct_proto_mutex);
 553        for (i = 0; i < num_proto; i++)
 554                __nf_ct_l4proto_unregister_one(l4proto[i]);
 555        mutex_unlock(&nf_ct_proto_mutex);
 556
 557        synchronize_net();
 558
 559        for (i = 0; i < num_proto; i++)
 560                nf_ct_iterate_destroy(kill_l4proto, (void *)l4proto[i]);
 561}
 562EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister);
 563
 564void nf_ct_l4proto_pernet_unregister(struct net *net,
 565                                const struct nf_conntrack_l4proto *const l4proto[],
 566                                unsigned int num_proto)
 567{
 568        while (num_proto-- != 0)
 569                nf_ct_l4proto_pernet_unregister_one(net, l4proto[num_proto]);
 570}
 571EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister);
 572
 573int nf_conntrack_proto_pernet_init(struct net *net)
 574{
 575        int err;
 576        struct nf_proto_net *pn = nf_ct_l4proto_net(net,
 577                                        &nf_conntrack_l4proto_generic);
 578
 579        err = nf_conntrack_l4proto_generic.init_net(net,
 580                                        nf_conntrack_l4proto_generic.l3proto);
 581        if (err < 0)
 582                return err;
 583        err = nf_ct_l4proto_register_sysctl(net,
 584                                            pn,
 585                                            &nf_conntrack_l4proto_generic);
 586        if (err < 0)
 587                return err;
 588
 589        pn->users++;
 590        return 0;
 591}
 592
 593void nf_conntrack_proto_pernet_fini(struct net *net)
 594{
 595        struct nf_proto_net *pn = nf_ct_l4proto_net(net,
 596                                        &nf_conntrack_l4proto_generic);
 597
 598        pn->users--;
 599        nf_ct_l4proto_unregister_sysctl(net,
 600                                        pn,
 601                                        &nf_conntrack_l4proto_generic);
 602}
 603
 604int nf_conntrack_proto_init(void)
 605{
 606        unsigned int i;
 607        for (i = 0; i < NFPROTO_NUMPROTO; i++)
 608                rcu_assign_pointer(nf_ct_l3protos[i],
 609                                   &nf_conntrack_l3proto_generic);
 610        return 0;
 611}
 612
 613void nf_conntrack_proto_fini(void)
 614{
 615        unsigned int i;
 616        /* free l3proto protocol tables */
 617        for (i = 0; i < ARRAY_SIZE(nf_ct_protos); i++)
 618                kfree(nf_ct_protos[i]);
 619}
 620