linux/net/netfilter/nf_conntrack_standalone.c
<<
>>
Prefs
   1/* (C) 1999-2001 Paul `Rusty' Russell
   2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
   3 * (C) 2005-2012 Patrick McHardy <kaber@trash.net>
   4 *
   5 * This program is free software; you can redistribute it and/or modify
   6 * it under the terms of the GNU General Public License version 2 as
   7 * published by the Free Software Foundation.
   8 */
   9
  10#include <linux/types.h>
  11#include <linux/netfilter.h>
  12#include <linux/slab.h>
  13#include <linux/module.h>
  14#include <linux/skbuff.h>
  15#include <linux/proc_fs.h>
  16#include <linux/seq_file.h>
  17#include <linux/percpu.h>
  18#include <linux/netdevice.h>
  19#include <linux/security.h>
  20#include <net/net_namespace.h>
  21#ifdef CONFIG_SYSCTL
  22#include <linux/sysctl.h>
  23#endif
  24
  25#include <net/netfilter/nf_conntrack.h>
  26#include <net/netfilter/nf_conntrack_core.h>
  27#include <net/netfilter/nf_conntrack_l3proto.h>
  28#include <net/netfilter/nf_conntrack_l4proto.h>
  29#include <net/netfilter/nf_conntrack_expect.h>
  30#include <net/netfilter/nf_conntrack_helper.h>
  31#include <net/netfilter/nf_conntrack_acct.h>
  32#include <net/netfilter/nf_conntrack_zones.h>
  33#include <net/netfilter/nf_conntrack_timestamp.h>
  34#include <linux/rculist_nulls.h>
  35
  36MODULE_LICENSE("GPL");
  37
  38#ifdef CONFIG_NF_CONNTRACK_PROCFS
  39int
  40print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
  41            const struct nf_conntrack_l3proto *l3proto,
  42            const struct nf_conntrack_l4proto *l4proto)
  43{
  44        return l3proto->print_tuple(s, tuple) || l4proto->print_tuple(s, tuple);
  45}
  46EXPORT_SYMBOL_GPL(print_tuple);
  47
  48struct ct_iter_state {
  49        struct seq_net_private p;
  50        unsigned int bucket;
  51        u_int64_t time_now;
  52};
  53
  54static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
  55{
  56        struct net *net = seq_file_net(seq);
  57        struct ct_iter_state *st = seq->private;
  58        struct hlist_nulls_node *n;
  59
  60        for (st->bucket = 0;
  61             st->bucket < net->ct.htable_size;
  62             st->bucket++) {
  63                n = rcu_dereference(hlist_nulls_first_rcu(&net->ct.hash[st->bucket]));
  64                if (!is_a_nulls(n))
  65                        return n;
  66        }
  67        return NULL;
  68}
  69
  70static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
  71                                      struct hlist_nulls_node *head)
  72{
  73        struct net *net = seq_file_net(seq);
  74        struct ct_iter_state *st = seq->private;
  75
  76        head = rcu_dereference(hlist_nulls_next_rcu(head));
  77        while (is_a_nulls(head)) {
  78                if (likely(get_nulls_value(head) == st->bucket)) {
  79                        if (++st->bucket >= net->ct.htable_size)
  80                                return NULL;
  81                }
  82                head = rcu_dereference(
  83                                hlist_nulls_first_rcu(
  84                                        &net->ct.hash[st->bucket]));
  85        }
  86        return head;
  87}
  88
  89static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
  90{
  91        struct hlist_nulls_node *head = ct_get_first(seq);
  92
  93        if (head)
  94                while (pos && (head = ct_get_next(seq, head)))
  95                        pos--;
  96        return pos ? NULL : head;
  97}
  98
  99static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
 100        __acquires(RCU)
 101{
 102        struct ct_iter_state *st = seq->private;
 103
 104        st->time_now = ktime_to_ns(ktime_get_real());
 105        rcu_read_lock();
 106        return ct_get_idx(seq, *pos);
 107}
 108
 109static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
 110{
 111        (*pos)++;
 112        return ct_get_next(s, v);
 113}
 114
 115static void ct_seq_stop(struct seq_file *s, void *v)
 116        __releases(RCU)
 117{
 118        rcu_read_unlock();
 119}
 120
 121#ifdef CONFIG_NF_CONNTRACK_SECMARK
 122static int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
 123{
 124        int ret;
 125        u32 len;
 126        char *secctx;
 127
 128        ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
 129        if (ret)
 130                return 0;
 131
 132        ret = seq_printf(s, "secctx=%s ", secctx);
 133
 134        security_release_secctx(secctx, len);
 135        return ret;
 136}
 137#else
 138static inline int ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
 139{
 140        return 0;
 141}
 142#endif
 143
 144#ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
 145static int ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
 146{
 147        struct ct_iter_state *st = s->private;
 148        struct nf_conn_tstamp *tstamp;
 149        s64 delta_time;
 150
 151        tstamp = nf_conn_tstamp_find(ct);
 152        if (tstamp) {
 153                delta_time = st->time_now - tstamp->start;
 154                if (delta_time > 0)
 155                        delta_time = div_s64(delta_time, NSEC_PER_SEC);
 156                else
 157                        delta_time = 0;
 158
 159                return seq_printf(s, "delta-time=%llu ",
 160                                  (unsigned long long)delta_time);
 161        }
 162        return 0;
 163}
 164#else
 165static inline int
 166ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
 167{
 168        return 0;
 169}
 170#endif
 171
 172/* return 0 on success, 1 in case of error */
 173static int ct_seq_show(struct seq_file *s, void *v)
 174{
 175        struct nf_conntrack_tuple_hash *hash = v;
 176        struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
 177        const struct nf_conntrack_l3proto *l3proto;
 178        const struct nf_conntrack_l4proto *l4proto;
 179        int ret = 0;
 180
 181        NF_CT_ASSERT(ct);
 182        if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
 183                return 0;
 184
 185        /* we only want to print DIR_ORIGINAL */
 186        if (NF_CT_DIRECTION(hash))
 187                goto release;
 188
 189        l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
 190        NF_CT_ASSERT(l3proto);
 191        l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
 192        NF_CT_ASSERT(l4proto);
 193
 194        ret = -ENOSPC;
 195        if (seq_printf(s, "%-8s %u %-8s %u %ld ",
 196                       l3proto->name, nf_ct_l3num(ct),
 197                       l4proto->name, nf_ct_protonum(ct),
 198                       timer_pending(&ct->timeout)
 199                       ? (long)(ct->timeout.expires - jiffies)/HZ : 0) != 0)
 200                goto release;
 201
 202        if (l4proto->print_conntrack && l4proto->print_conntrack(s, ct))
 203                goto release;
 204
 205        if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
 206                        l3proto, l4proto))
 207                goto release;
 208
 209        if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
 210                goto release;
 211
 212        if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
 213                if (seq_printf(s, "[UNREPLIED] "))
 214                        goto release;
 215
 216        if (print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple,
 217                        l3proto, l4proto))
 218                goto release;
 219
 220        if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
 221                goto release;
 222
 223        if (test_bit(IPS_ASSURED_BIT, &ct->status))
 224                if (seq_printf(s, "[ASSURED] "))
 225                        goto release;
 226
 227#if defined(CONFIG_NF_CONNTRACK_MARK)
 228        if (seq_printf(s, "mark=%u ", ct->mark))
 229                goto release;
 230#endif
 231
 232        if (ct_show_secctx(s, ct))
 233                goto release;
 234
 235#ifdef CONFIG_NF_CONNTRACK_ZONES
 236        if (seq_printf(s, "zone=%u ", nf_ct_zone(ct)))
 237                goto release;
 238#endif
 239
 240        if (ct_show_delta_time(s, ct))
 241                goto release;
 242
 243        if (seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use)))
 244                goto release;
 245
 246        ret = 0;
 247release:
 248        nf_ct_put(ct);
 249        return ret;
 250}
 251
 252static const struct seq_operations ct_seq_ops = {
 253        .start = ct_seq_start,
 254        .next  = ct_seq_next,
 255        .stop  = ct_seq_stop,
 256        .show  = ct_seq_show
 257};
 258
 259static int ct_open(struct inode *inode, struct file *file)
 260{
 261        return seq_open_net(inode, file, &ct_seq_ops,
 262                        sizeof(struct ct_iter_state));
 263}
 264
 265static const struct file_operations ct_file_ops = {
 266        .owner   = THIS_MODULE,
 267        .open    = ct_open,
 268        .read    = seq_read,
 269        .llseek  = seq_lseek,
 270        .release = seq_release_net,
 271};
 272
 273static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
 274{
 275        struct net *net = seq_file_net(seq);
 276        int cpu;
 277
 278        if (*pos == 0)
 279                return SEQ_START_TOKEN;
 280
 281        for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
 282                if (!cpu_possible(cpu))
 283                        continue;
 284                *pos = cpu + 1;
 285                return per_cpu_ptr(net->ct.stat, cpu);
 286        }
 287
 288        return NULL;
 289}
 290
 291static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
 292{
 293        struct net *net = seq_file_net(seq);
 294        int cpu;
 295
 296        for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
 297                if (!cpu_possible(cpu))
 298                        continue;
 299                *pos = cpu + 1;
 300                return per_cpu_ptr(net->ct.stat, cpu);
 301        }
 302
 303        return NULL;
 304}
 305
 306static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
 307{
 308}
 309
 310static int ct_cpu_seq_show(struct seq_file *seq, void *v)
 311{
 312        struct net *net = seq_file_net(seq);
 313        unsigned int nr_conntracks = atomic_read(&net->ct.count);
 314        const struct ip_conntrack_stat *st = v;
 315
 316        if (v == SEQ_START_TOKEN) {
 317                seq_printf(seq, "entries  searched found new invalid ignore delete delete_list insert insert_failed drop early_drop icmp_error  expect_new expect_create expect_delete search_restart\n");
 318                return 0;
 319        }
 320
 321        seq_printf(seq, "%08x  %08x %08x %08x %08x %08x %08x %08x "
 322                        "%08x %08x %08x %08x %08x  %08x %08x %08x %08x\n",
 323                   nr_conntracks,
 324                   st->searched,
 325                   st->found,
 326                   st->new,
 327                   st->invalid,
 328                   st->ignore,
 329                   st->delete,
 330                   st->delete_list,
 331                   st->insert,
 332                   st->insert_failed,
 333                   st->drop,
 334                   st->early_drop,
 335                   st->error,
 336
 337                   st->expect_new,
 338                   st->expect_create,
 339                   st->expect_delete,
 340                   st->search_restart
 341                );
 342        return 0;
 343}
 344
 345static const struct seq_operations ct_cpu_seq_ops = {
 346        .start  = ct_cpu_seq_start,
 347        .next   = ct_cpu_seq_next,
 348        .stop   = ct_cpu_seq_stop,
 349        .show   = ct_cpu_seq_show,
 350};
 351
 352static int ct_cpu_seq_open(struct inode *inode, struct file *file)
 353{
 354        return seq_open_net(inode, file, &ct_cpu_seq_ops,
 355                            sizeof(struct seq_net_private));
 356}
 357
 358static const struct file_operations ct_cpu_seq_fops = {
 359        .owner   = THIS_MODULE,
 360        .open    = ct_cpu_seq_open,
 361        .read    = seq_read,
 362        .llseek  = seq_lseek,
 363        .release = seq_release_net,
 364};
 365
 366static int nf_conntrack_standalone_init_proc(struct net *net)
 367{
 368        struct proc_dir_entry *pde;
 369
 370        pde = proc_create("nf_conntrack", 0440, net->proc_net, &ct_file_ops);
 371        if (!pde)
 372                goto out_nf_conntrack;
 373
 374        pde = proc_create("nf_conntrack", S_IRUGO, net->proc_net_stat,
 375                          &ct_cpu_seq_fops);
 376        if (!pde)
 377                goto out_stat_nf_conntrack;
 378        return 0;
 379
 380out_stat_nf_conntrack:
 381        remove_proc_entry("nf_conntrack", net->proc_net);
 382out_nf_conntrack:
 383        return -ENOMEM;
 384}
 385
 386static void nf_conntrack_standalone_fini_proc(struct net *net)
 387{
 388        remove_proc_entry("nf_conntrack", net->proc_net_stat);
 389        remove_proc_entry("nf_conntrack", net->proc_net);
 390}
 391#else
 392static int nf_conntrack_standalone_init_proc(struct net *net)
 393{
 394        return 0;
 395}
 396
 397static void nf_conntrack_standalone_fini_proc(struct net *net)
 398{
 399}
 400#endif /* CONFIG_NF_CONNTRACK_PROCFS */
 401
 402/* Sysctl support */
 403
 404#ifdef CONFIG_SYSCTL
 405/* Log invalid packets of a given protocol */
 406static int log_invalid_proto_min = 0;
 407static int log_invalid_proto_max = 255;
 408
 409static struct ctl_table_header *nf_ct_netfilter_header;
 410
 411static struct ctl_table nf_ct_sysctl_table[] = {
 412        {
 413                .procname       = "nf_conntrack_max",
 414                .data           = &nf_conntrack_max,
 415                .maxlen         = sizeof(int),
 416                .mode           = 0644,
 417                .proc_handler   = proc_dointvec,
 418        },
 419        {
 420                .procname       = "nf_conntrack_count",
 421                .data           = &init_net.ct.count,
 422                .maxlen         = sizeof(int),
 423                .mode           = 0444,
 424                .proc_handler   = proc_dointvec,
 425        },
 426        {
 427                .procname       = "nf_conntrack_buckets",
 428                .data           = &init_net.ct.htable_size,
 429                .maxlen         = sizeof(unsigned int),
 430                .mode           = 0444,
 431                .proc_handler   = proc_dointvec,
 432        },
 433        {
 434                .procname       = "nf_conntrack_checksum",
 435                .data           = &init_net.ct.sysctl_checksum,
 436                .maxlen         = sizeof(unsigned int),
 437                .mode           = 0644,
 438                .proc_handler   = proc_dointvec,
 439        },
 440        {
 441                .procname       = "nf_conntrack_log_invalid",
 442                .data           = &init_net.ct.sysctl_log_invalid,
 443                .maxlen         = sizeof(unsigned int),
 444                .mode           = 0644,
 445                .proc_handler   = proc_dointvec_minmax,
 446                .extra1         = &log_invalid_proto_min,
 447                .extra2         = &log_invalid_proto_max,
 448        },
 449        {
 450                .procname       = "nf_conntrack_expect_max",
 451                .data           = &nf_ct_expect_max,
 452                .maxlen         = sizeof(int),
 453                .mode           = 0644,
 454                .proc_handler   = proc_dointvec,
 455        },
 456        { }
 457};
 458
 459#define NET_NF_CONNTRACK_MAX 2089
 460
 461static struct ctl_table nf_ct_netfilter_table[] = {
 462        {
 463                .procname       = "nf_conntrack_max",
 464                .data           = &nf_conntrack_max,
 465                .maxlen         = sizeof(int),
 466                .mode           = 0644,
 467                .proc_handler   = proc_dointvec,
 468        },
 469        { }
 470};
 471
 472static int nf_conntrack_standalone_init_sysctl(struct net *net)
 473{
 474        struct ctl_table *table;
 475
 476        table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
 477                        GFP_KERNEL);
 478        if (!table)
 479                goto out_kmemdup;
 480
 481        table[1].data = &net->ct.count;
 482        table[2].data = &net->ct.htable_size;
 483        table[3].data = &net->ct.sysctl_checksum;
 484        table[4].data = &net->ct.sysctl_log_invalid;
 485
 486        /* Don't export sysctls to unprivileged users */
 487        if (net->user_ns != &init_user_ns)
 488                table[0].procname = NULL;
 489
 490        net->ct.sysctl_header = register_net_sysctl(net, "net/netfilter", table);
 491        if (!net->ct.sysctl_header)
 492                goto out_unregister_netfilter;
 493
 494        return 0;
 495
 496out_unregister_netfilter:
 497        kfree(table);
 498out_kmemdup:
 499        return -ENOMEM;
 500}
 501
 502static void nf_conntrack_standalone_fini_sysctl(struct net *net)
 503{
 504        struct ctl_table *table;
 505
 506        table = net->ct.sysctl_header->ctl_table_arg;
 507        unregister_net_sysctl_table(net->ct.sysctl_header);
 508        kfree(table);
 509}
 510#else
 511static int nf_conntrack_standalone_init_sysctl(struct net *net)
 512{
 513        return 0;
 514}
 515
 516static void nf_conntrack_standalone_fini_sysctl(struct net *net)
 517{
 518}
 519#endif /* CONFIG_SYSCTL */
 520
 521static int nf_conntrack_pernet_init(struct net *net)
 522{
 523        int ret;
 524
 525        ret = nf_conntrack_init_net(net);
 526        if (ret < 0)
 527                goto out_init;
 528
 529        ret = nf_conntrack_standalone_init_proc(net);
 530        if (ret < 0)
 531                goto out_proc;
 532
 533        net->ct.sysctl_checksum = 1;
 534        net->ct.sysctl_log_invalid = 0;
 535        ret = nf_conntrack_standalone_init_sysctl(net);
 536        if (ret < 0)
 537                goto out_sysctl;
 538
 539        return 0;
 540
 541out_sysctl:
 542        nf_conntrack_standalone_fini_proc(net);
 543out_proc:
 544        nf_conntrack_cleanup_net(net);
 545out_init:
 546        return ret;
 547}
 548
 549static void nf_conntrack_pernet_exit(struct list_head *net_exit_list)
 550{
 551        struct net *net;
 552
 553        list_for_each_entry(net, net_exit_list, exit_list) {
 554                nf_conntrack_standalone_fini_sysctl(net);
 555                nf_conntrack_standalone_fini_proc(net);
 556        }
 557        nf_conntrack_cleanup_net_list(net_exit_list);
 558}
 559
 560static struct pernet_operations nf_conntrack_net_ops = {
 561        .init           = nf_conntrack_pernet_init,
 562        .exit_batch     = nf_conntrack_pernet_exit,
 563};
 564
 565static int __init nf_conntrack_standalone_init(void)
 566{
 567        int ret = nf_conntrack_init_start();
 568        if (ret < 0)
 569                goto out_start;
 570
 571#ifdef CONFIG_SYSCTL
 572        nf_ct_netfilter_header =
 573                register_net_sysctl(&init_net, "net", nf_ct_netfilter_table);
 574        if (!nf_ct_netfilter_header) {
 575                pr_err("nf_conntrack: can't register to sysctl.\n");
 576                ret = -ENOMEM;
 577                goto out_sysctl;
 578        }
 579#endif
 580
 581        ret = register_pernet_subsys(&nf_conntrack_net_ops);
 582        if (ret < 0)
 583                goto out_pernet;
 584
 585        nf_conntrack_init_end();
 586        return 0;
 587
 588out_pernet:
 589#ifdef CONFIG_SYSCTL
 590        unregister_net_sysctl_table(nf_ct_netfilter_header);
 591out_sysctl:
 592#endif
 593        nf_conntrack_cleanup_end();
 594out_start:
 595        return ret;
 596}
 597
 598static void __exit nf_conntrack_standalone_fini(void)
 599{
 600        nf_conntrack_cleanup_start();
 601        unregister_pernet_subsys(&nf_conntrack_net_ops);
 602#ifdef CONFIG_SYSCTL
 603        unregister_net_sysctl_table(nf_ct_netfilter_header);
 604#endif
 605        nf_conntrack_cleanup_end();
 606}
 607
 608module_init(nf_conntrack_standalone_init);
 609module_exit(nf_conntrack_standalone_fini);
 610
 611/* Some modules need us, but don't depend directly on any symbol.
 612   They should call this. */
 613void need_conntrack(void)
 614{
 615}
 616EXPORT_SYMBOL_GPL(need_conntrack);
 617
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.