linux/net/netfilter/nf_conntrack_standalone.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2#include <linux/types.h>
   3#include <linux/netfilter.h>
   4#include <linux/slab.h>
   5#include <linux/module.h>
   6#include <linux/skbuff.h>
   7#include <linux/proc_fs.h>
   8#include <linux/seq_file.h>
   9#include <linux/percpu.h>
  10#include <linux/netdevice.h>
  11#include <linux/security.h>
  12#include <net/net_namespace.h>
  13#ifdef CONFIG_SYSCTL
  14#include <linux/sysctl.h>
  15#endif
  16
  17#include <net/netfilter/nf_conntrack.h>
  18#include <net/netfilter/nf_conntrack_core.h>
  19#include <net/netfilter/nf_conntrack_l4proto.h>
  20#include <net/netfilter/nf_conntrack_expect.h>
  21#include <net/netfilter/nf_conntrack_helper.h>
  22#include <net/netfilter/nf_conntrack_acct.h>
  23#include <net/netfilter/nf_conntrack_zones.h>
  24#include <net/netfilter/nf_conntrack_timestamp.h>
  25#include <linux/rculist_nulls.h>
  26
  27static bool enable_hooks __read_mostly;
  28MODULE_PARM_DESC(enable_hooks, "Always enable conntrack hooks");
  29module_param(enable_hooks, bool, 0000);
  30
  31unsigned int nf_conntrack_net_id __read_mostly;
  32
  33#ifdef CONFIG_NF_CONNTRACK_PROCFS
  34void
  35print_tuple(struct seq_file *s, const struct nf_conntrack_tuple *tuple,
  36            const struct nf_conntrack_l4proto *l4proto)
  37{
  38        switch (tuple->src.l3num) {
  39        case NFPROTO_IPV4:
  40                seq_printf(s, "src=%pI4 dst=%pI4 ",
  41                           &tuple->src.u3.ip, &tuple->dst.u3.ip);
  42                break;
  43        case NFPROTO_IPV6:
  44                seq_printf(s, "src=%pI6 dst=%pI6 ",
  45                           tuple->src.u3.ip6, tuple->dst.u3.ip6);
  46                break;
  47        default:
  48                break;
  49        }
  50
  51        switch (l4proto->l4proto) {
  52        case IPPROTO_ICMP:
  53                seq_printf(s, "type=%u code=%u id=%u ",
  54                           tuple->dst.u.icmp.type,
  55                           tuple->dst.u.icmp.code,
  56                           ntohs(tuple->src.u.icmp.id));
  57                break;
  58        case IPPROTO_TCP:
  59                seq_printf(s, "sport=%hu dport=%hu ",
  60                           ntohs(tuple->src.u.tcp.port),
  61                           ntohs(tuple->dst.u.tcp.port));
  62                break;
  63        case IPPROTO_UDPLITE:
  64        case IPPROTO_UDP:
  65                seq_printf(s, "sport=%hu dport=%hu ",
  66                           ntohs(tuple->src.u.udp.port),
  67                           ntohs(tuple->dst.u.udp.port));
  68
  69                break;
  70        case IPPROTO_DCCP:
  71                seq_printf(s, "sport=%hu dport=%hu ",
  72                           ntohs(tuple->src.u.dccp.port),
  73                           ntohs(tuple->dst.u.dccp.port));
  74                break;
  75        case IPPROTO_SCTP:
  76                seq_printf(s, "sport=%hu dport=%hu ",
  77                           ntohs(tuple->src.u.sctp.port),
  78                           ntohs(tuple->dst.u.sctp.port));
  79                break;
  80        case IPPROTO_ICMPV6:
  81                seq_printf(s, "type=%u code=%u id=%u ",
  82                           tuple->dst.u.icmp.type,
  83                           tuple->dst.u.icmp.code,
  84                           ntohs(tuple->src.u.icmp.id));
  85                break;
  86        case IPPROTO_GRE:
  87                seq_printf(s, "srckey=0x%x dstkey=0x%x ",
  88                           ntohs(tuple->src.u.gre.key),
  89                           ntohs(tuple->dst.u.gre.key));
  90                break;
  91        default:
  92                break;
  93        }
  94}
  95EXPORT_SYMBOL_GPL(print_tuple);
  96
  97struct ct_iter_state {
  98        struct seq_net_private p;
  99        struct hlist_nulls_head *hash;
 100        unsigned int htable_size;
 101        unsigned int bucket;
 102        u_int64_t time_now;
 103};
 104
 105static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
 106{
 107        struct ct_iter_state *st = seq->private;
 108        struct hlist_nulls_node *n;
 109
 110        for (st->bucket = 0;
 111             st->bucket < st->htable_size;
 112             st->bucket++) {
 113                n = rcu_dereference(
 114                        hlist_nulls_first_rcu(&st->hash[st->bucket]));
 115                if (!is_a_nulls(n))
 116                        return n;
 117        }
 118        return NULL;
 119}
 120
 121static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
 122                                      struct hlist_nulls_node *head)
 123{
 124        struct ct_iter_state *st = seq->private;
 125
 126        head = rcu_dereference(hlist_nulls_next_rcu(head));
 127        while (is_a_nulls(head)) {
 128                if (likely(get_nulls_value(head) == st->bucket)) {
 129                        if (++st->bucket >= st->htable_size)
 130                                return NULL;
 131                }
 132                head = rcu_dereference(
 133                        hlist_nulls_first_rcu(&st->hash[st->bucket]));
 134        }
 135        return head;
 136}
 137
 138static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
 139{
 140        struct hlist_nulls_node *head = ct_get_first(seq);
 141
 142        if (head)
 143                while (pos && (head = ct_get_next(seq, head)))
 144                        pos--;
 145        return pos ? NULL : head;
 146}
 147
 148static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
 149        __acquires(RCU)
 150{
 151        struct ct_iter_state *st = seq->private;
 152
 153        st->time_now = ktime_get_real_ns();
 154        rcu_read_lock();
 155
 156        nf_conntrack_get_ht(&st->hash, &st->htable_size);
 157        return ct_get_idx(seq, *pos);
 158}
 159
 160static void *ct_seq_next(struct seq_file *s, void *v, loff_t *pos)
 161{
 162        (*pos)++;
 163        return ct_get_next(s, v);
 164}
 165
 166static void ct_seq_stop(struct seq_file *s, void *v)
 167        __releases(RCU)
 168{
 169        rcu_read_unlock();
 170}
 171
 172#ifdef CONFIG_NF_CONNTRACK_SECMARK
 173static void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
 174{
 175        int ret;
 176        u32 len;
 177        char *secctx;
 178
 179        ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
 180        if (ret)
 181                return;
 182
 183        seq_printf(s, "secctx=%s ", secctx);
 184
 185        security_release_secctx(secctx, len);
 186}
 187#else
 188static inline void ct_show_secctx(struct seq_file *s, const struct nf_conn *ct)
 189{
 190}
 191#endif
 192
 193#ifdef CONFIG_NF_CONNTRACK_ZONES
 194static void ct_show_zone(struct seq_file *s, const struct nf_conn *ct,
 195                         int dir)
 196{
 197        const struct nf_conntrack_zone *zone = nf_ct_zone(ct);
 198
 199        if (zone->dir != dir)
 200                return;
 201        switch (zone->dir) {
 202        case NF_CT_DEFAULT_ZONE_DIR:
 203                seq_printf(s, "zone=%u ", zone->id);
 204                break;
 205        case NF_CT_ZONE_DIR_ORIG:
 206                seq_printf(s, "zone-orig=%u ", zone->id);
 207                break;
 208        case NF_CT_ZONE_DIR_REPL:
 209                seq_printf(s, "zone-reply=%u ", zone->id);
 210                break;
 211        default:
 212                break;
 213        }
 214}
 215#else
 216static inline void ct_show_zone(struct seq_file *s, const struct nf_conn *ct,
 217                                int dir)
 218{
 219}
 220#endif
 221
 222#ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
 223static void ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
 224{
 225        struct ct_iter_state *st = s->private;
 226        struct nf_conn_tstamp *tstamp;
 227        s64 delta_time;
 228
 229        tstamp = nf_conn_tstamp_find(ct);
 230        if (tstamp) {
 231                delta_time = st->time_now - tstamp->start;
 232                if (delta_time > 0)
 233                        delta_time = div_s64(delta_time, NSEC_PER_SEC);
 234                else
 235                        delta_time = 0;
 236
 237                seq_printf(s, "delta-time=%llu ",
 238                           (unsigned long long)delta_time);
 239        }
 240        return;
 241}
 242#else
 243static inline void
 244ct_show_delta_time(struct seq_file *s, const struct nf_conn *ct)
 245{
 246}
 247#endif
 248
 249static const char* l3proto_name(u16 proto)
 250{
 251        switch (proto) {
 252        case AF_INET: return "ipv4";
 253        case AF_INET6: return "ipv6";
 254        }
 255
 256        return "unknown";
 257}
 258
 259static const char* l4proto_name(u16 proto)
 260{
 261        switch (proto) {
 262        case IPPROTO_ICMP: return "icmp";
 263        case IPPROTO_TCP: return "tcp";
 264        case IPPROTO_UDP: return "udp";
 265        case IPPROTO_DCCP: return "dccp";
 266        case IPPROTO_GRE: return "gre";
 267        case IPPROTO_SCTP: return "sctp";
 268        case IPPROTO_UDPLITE: return "udplite";
 269        case IPPROTO_ICMPV6: return "icmpv6";
 270        }
 271
 272        return "unknown";
 273}
 274
 275static unsigned int
 276seq_print_acct(struct seq_file *s, const struct nf_conn *ct, int dir)
 277{
 278        struct nf_conn_acct *acct;
 279        struct nf_conn_counter *counter;
 280
 281        acct = nf_conn_acct_find(ct);
 282        if (!acct)
 283                return 0;
 284
 285        counter = acct->counter;
 286        seq_printf(s, "packets=%llu bytes=%llu ",
 287                   (unsigned long long)atomic64_read(&counter[dir].packets),
 288                   (unsigned long long)atomic64_read(&counter[dir].bytes));
 289
 290        return 0;
 291}
 292
 293/* return 0 on success, 1 in case of error */
 294static int ct_seq_show(struct seq_file *s, void *v)
 295{
 296        struct nf_conntrack_tuple_hash *hash = v;
 297        struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(hash);
 298        const struct nf_conntrack_l4proto *l4proto;
 299        struct net *net = seq_file_net(s);
 300        int ret = 0;
 301
 302        WARN_ON(!ct);
 303        if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
 304                return 0;
 305
 306        if (nf_ct_should_gc(ct)) {
 307                nf_ct_kill(ct);
 308                goto release;
 309        }
 310
 311        /* we only want to print DIR_ORIGINAL */
 312        if (NF_CT_DIRECTION(hash))
 313                goto release;
 314
 315        if (!net_eq(nf_ct_net(ct), net))
 316                goto release;
 317
 318        l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct));
 319
 320        ret = -ENOSPC;
 321        seq_printf(s, "%-8s %u %-8s %u ",
 322                   l3proto_name(nf_ct_l3num(ct)), nf_ct_l3num(ct),
 323                   l4proto_name(l4proto->l4proto), nf_ct_protonum(ct));
 324
 325        if (!test_bit(IPS_OFFLOAD_BIT, &ct->status))
 326                seq_printf(s, "%ld ", nf_ct_expires(ct)  / HZ);
 327
 328        if (l4proto->print_conntrack)
 329                l4proto->print_conntrack(s, ct);
 330
 331        print_tuple(s, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
 332                    l4proto);
 333
 334        ct_show_zone(s, ct, NF_CT_ZONE_DIR_ORIG);
 335
 336        if (seq_has_overflowed(s))
 337                goto release;
 338
 339        if (seq_print_acct(s, ct, IP_CT_DIR_ORIGINAL))
 340                goto release;
 341
 342        if (!(test_bit(IPS_SEEN_REPLY_BIT, &ct->status)))
 343                seq_puts(s, "[UNREPLIED] ");
 344
 345        print_tuple(s, &ct->tuplehash[IP_CT_DIR_REPLY].tuple, l4proto);
 346
 347        ct_show_zone(s, ct, NF_CT_ZONE_DIR_REPL);
 348
 349        if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
 350                goto release;
 351
 352        if (test_bit(IPS_HW_OFFLOAD_BIT, &ct->status))
 353                seq_puts(s, "[HW_OFFLOAD] ");
 354        else if (test_bit(IPS_OFFLOAD_BIT, &ct->status))
 355                seq_puts(s, "[OFFLOAD] ");
 356        else if (test_bit(IPS_ASSURED_BIT, &ct->status))
 357                seq_puts(s, "[ASSURED] ");
 358
 359        if (seq_has_overflowed(s))
 360                goto release;
 361
 362#if defined(CONFIG_NF_CONNTRACK_MARK)
 363        seq_printf(s, "mark=%u ", ct->mark);
 364#endif
 365
 366        ct_show_secctx(s, ct);
 367        ct_show_zone(s, ct, NF_CT_DEFAULT_ZONE_DIR);
 368        ct_show_delta_time(s, ct);
 369
 370        seq_printf(s, "use=%u\n", atomic_read(&ct->ct_general.use));
 371
 372        if (seq_has_overflowed(s))
 373                goto release;
 374
 375        ret = 0;
 376release:
 377        nf_ct_put(ct);
 378        return ret;
 379}
 380
 381static const struct seq_operations ct_seq_ops = {
 382        .start = ct_seq_start,
 383        .next  = ct_seq_next,
 384        .stop  = ct_seq_stop,
 385        .show  = ct_seq_show
 386};
 387
 388static void *ct_cpu_seq_start(struct seq_file *seq, loff_t *pos)
 389{
 390        struct net *net = seq_file_net(seq);
 391        int cpu;
 392
 393        if (*pos == 0)
 394                return SEQ_START_TOKEN;
 395
 396        for (cpu = *pos-1; cpu < nr_cpu_ids; ++cpu) {
 397                if (!cpu_possible(cpu))
 398                        continue;
 399                *pos = cpu + 1;
 400                return per_cpu_ptr(net->ct.stat, cpu);
 401        }
 402
 403        return NULL;
 404}
 405
 406static void *ct_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
 407{
 408        struct net *net = seq_file_net(seq);
 409        int cpu;
 410
 411        for (cpu = *pos; cpu < nr_cpu_ids; ++cpu) {
 412                if (!cpu_possible(cpu))
 413                        continue;
 414                *pos = cpu + 1;
 415                return per_cpu_ptr(net->ct.stat, cpu);
 416        }
 417        (*pos)++;
 418        return NULL;
 419}
 420
 421static void ct_cpu_seq_stop(struct seq_file *seq, void *v)
 422{
 423}
 424
 425static int ct_cpu_seq_show(struct seq_file *seq, void *v)
 426{
 427        struct net *net = seq_file_net(seq);
 428        const struct ip_conntrack_stat *st = v;
 429        unsigned int nr_conntracks;
 430
 431        if (v == SEQ_START_TOKEN) {
 432                seq_puts(seq, "entries  clashres found new invalid ignore delete delete_list insert insert_failed drop early_drop icmp_error  expect_new expect_create expect_delete search_restart\n");
 433                return 0;
 434        }
 435
 436        nr_conntracks = nf_conntrack_count(net);
 437
 438        seq_printf(seq, "%08x  %08x %08x %08x %08x %08x %08x %08x "
 439                        "%08x %08x %08x %08x %08x  %08x %08x %08x %08x\n",
 440                   nr_conntracks,
 441                   st->clash_resolve,
 442                   st->found,
 443                   0,
 444                   st->invalid,
 445                   0,
 446                   0,
 447                   0,
 448                   st->insert,
 449                   st->insert_failed,
 450                   st->drop,
 451                   st->early_drop,
 452                   st->error,
 453
 454                   st->expect_new,
 455                   st->expect_create,
 456                   st->expect_delete,
 457                   st->search_restart
 458                );
 459        return 0;
 460}
 461
 462static const struct seq_operations ct_cpu_seq_ops = {
 463        .start  = ct_cpu_seq_start,
 464        .next   = ct_cpu_seq_next,
 465        .stop   = ct_cpu_seq_stop,
 466        .show   = ct_cpu_seq_show,
 467};
 468
 469static int nf_conntrack_standalone_init_proc(struct net *net)
 470{
 471        struct proc_dir_entry *pde;
 472        kuid_t root_uid;
 473        kgid_t root_gid;
 474
 475        pde = proc_create_net("nf_conntrack", 0440, net->proc_net, &ct_seq_ops,
 476                        sizeof(struct ct_iter_state));
 477        if (!pde)
 478                goto out_nf_conntrack;
 479
 480        root_uid = make_kuid(net->user_ns, 0);
 481        root_gid = make_kgid(net->user_ns, 0);
 482        if (uid_valid(root_uid) && gid_valid(root_gid))
 483                proc_set_user(pde, root_uid, root_gid);
 484
 485        pde = proc_create_net("nf_conntrack", 0444, net->proc_net_stat,
 486                        &ct_cpu_seq_ops, sizeof(struct seq_net_private));
 487        if (!pde)
 488                goto out_stat_nf_conntrack;
 489        return 0;
 490
 491out_stat_nf_conntrack:
 492        remove_proc_entry("nf_conntrack", net->proc_net);
 493out_nf_conntrack:
 494        return -ENOMEM;
 495}
 496
 497static void nf_conntrack_standalone_fini_proc(struct net *net)
 498{
 499        remove_proc_entry("nf_conntrack", net->proc_net_stat);
 500        remove_proc_entry("nf_conntrack", net->proc_net);
 501}
 502#else
 503static int nf_conntrack_standalone_init_proc(struct net *net)
 504{
 505        return 0;
 506}
 507
 508static void nf_conntrack_standalone_fini_proc(struct net *net)
 509{
 510}
 511#endif /* CONFIG_NF_CONNTRACK_PROCFS */
 512
 513u32 nf_conntrack_count(const struct net *net)
 514{
 515        const struct nf_conntrack_net *cnet = nf_ct_pernet(net);
 516
 517        return atomic_read(&cnet->count);
 518}
 519EXPORT_SYMBOL_GPL(nf_conntrack_count);
 520
 521/* Sysctl support */
 522
 523#ifdef CONFIG_SYSCTL
 524/* size the user *wants to set */
 525static unsigned int nf_conntrack_htable_size_user __read_mostly;
 526
 527static int
 528nf_conntrack_hash_sysctl(struct ctl_table *table, int write,
 529                         void *buffer, size_t *lenp, loff_t *ppos)
 530{
 531        int ret;
 532
 533        /* module_param hashsize could have changed value */
 534        nf_conntrack_htable_size_user = nf_conntrack_htable_size;
 535
 536        ret = proc_dointvec(table, write, buffer, lenp, ppos);
 537        if (ret < 0 || !write)
 538                return ret;
 539
 540        /* update ret, we might not be able to satisfy request */
 541        ret = nf_conntrack_hash_resize(nf_conntrack_htable_size_user);
 542
 543        /* update it to the actual value used by conntrack */
 544        nf_conntrack_htable_size_user = nf_conntrack_htable_size;
 545        return ret;
 546}
 547
 548static struct ctl_table_header *nf_ct_netfilter_header;
 549
 550enum nf_ct_sysctl_index {
 551        NF_SYSCTL_CT_MAX,
 552        NF_SYSCTL_CT_COUNT,
 553        NF_SYSCTL_CT_BUCKETS,
 554        NF_SYSCTL_CT_CHECKSUM,
 555        NF_SYSCTL_CT_LOG_INVALID,
 556        NF_SYSCTL_CT_EXPECT_MAX,
 557        NF_SYSCTL_CT_ACCT,
 558        NF_SYSCTL_CT_HELPER,
 559#ifdef CONFIG_NF_CONNTRACK_EVENTS
 560        NF_SYSCTL_CT_EVENTS,
 561#endif
 562#ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
 563        NF_SYSCTL_CT_TIMESTAMP,
 564#endif
 565        NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC,
 566        NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_SENT,
 567        NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_RECV,
 568        NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ESTABLISHED,
 569        NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_FIN_WAIT,
 570        NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE_WAIT,
 571        NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_LAST_ACK,
 572        NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_TIME_WAIT,
 573        NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE,
 574        NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_RETRANS,
 575        NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_UNACK,
 576#if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
 577        NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD,
 578#endif
 579        NF_SYSCTL_CT_PROTO_TCP_LOOSE,
 580        NF_SYSCTL_CT_PROTO_TCP_LIBERAL,
 581        NF_SYSCTL_CT_PROTO_TCP_IGNORE_INVALID_RST,
 582        NF_SYSCTL_CT_PROTO_TCP_MAX_RETRANS,
 583        NF_SYSCTL_CT_PROTO_TIMEOUT_UDP,
 584        NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM,
 585#if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
 586        NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD,
 587#endif
 588        NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP,
 589        NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6,
 590#ifdef CONFIG_NF_CT_PROTO_SCTP
 591        NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_CLOSED,
 592        NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_WAIT,
 593        NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_ECHOED,
 594        NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ESTABLISHED,
 595        NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_SENT,
 596        NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_RECD,
 597        NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT,
 598        NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_SENT,
 599        NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_ACKED,
 600#endif
 601#ifdef CONFIG_NF_CT_PROTO_DCCP
 602        NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_REQUEST,
 603        NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_RESPOND,
 604        NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_PARTOPEN,
 605        NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_OPEN,
 606        NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSEREQ,
 607        NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSING,
 608        NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_TIMEWAIT,
 609        NF_SYSCTL_CT_PROTO_DCCP_LOOSE,
 610#endif
 611#ifdef CONFIG_NF_CT_PROTO_GRE
 612        NF_SYSCTL_CT_PROTO_TIMEOUT_GRE,
 613        NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM,
 614#endif
 615
 616        __NF_SYSCTL_CT_LAST_SYSCTL,
 617};
 618
 619#define NF_SYSCTL_CT_LAST_SYSCTL (__NF_SYSCTL_CT_LAST_SYSCTL + 1)
 620
 621static struct ctl_table nf_ct_sysctl_table[] = {
 622        [NF_SYSCTL_CT_MAX] = {
 623                .procname       = "nf_conntrack_max",
 624                .data           = &nf_conntrack_max,
 625                .maxlen         = sizeof(int),
 626                .mode           = 0644,
 627                .proc_handler   = proc_dointvec,
 628        },
 629        [NF_SYSCTL_CT_COUNT] = {
 630                .procname       = "nf_conntrack_count",
 631                .maxlen         = sizeof(int),
 632                .mode           = 0444,
 633                .proc_handler   = proc_dointvec,
 634        },
 635        [NF_SYSCTL_CT_BUCKETS] = {
 636                .procname       = "nf_conntrack_buckets",
 637                .data           = &nf_conntrack_htable_size_user,
 638                .maxlen         = sizeof(unsigned int),
 639                .mode           = 0644,
 640                .proc_handler   = nf_conntrack_hash_sysctl,
 641        },
 642        [NF_SYSCTL_CT_CHECKSUM] = {
 643                .procname       = "nf_conntrack_checksum",
 644                .data           = &init_net.ct.sysctl_checksum,
 645                .maxlen         = sizeof(u8),
 646                .mode           = 0644,
 647                .proc_handler   = proc_dou8vec_minmax,
 648                .extra1         = SYSCTL_ZERO,
 649                .extra2         = SYSCTL_ONE,
 650        },
 651        [NF_SYSCTL_CT_LOG_INVALID] = {
 652                .procname       = "nf_conntrack_log_invalid",
 653                .data           = &init_net.ct.sysctl_log_invalid,
 654                .maxlen         = sizeof(u8),
 655                .mode           = 0644,
 656                .proc_handler   = proc_dou8vec_minmax,
 657        },
 658        [NF_SYSCTL_CT_EXPECT_MAX] = {
 659                .procname       = "nf_conntrack_expect_max",
 660                .data           = &nf_ct_expect_max,
 661                .maxlen         = sizeof(int),
 662                .mode           = 0644,
 663                .proc_handler   = proc_dointvec,
 664        },
 665        [NF_SYSCTL_CT_ACCT] = {
 666                .procname       = "nf_conntrack_acct",
 667                .data           = &init_net.ct.sysctl_acct,
 668                .maxlen         = sizeof(u8),
 669                .mode           = 0644,
 670                .proc_handler   = proc_dou8vec_minmax,
 671                .extra1         = SYSCTL_ZERO,
 672                .extra2         = SYSCTL_ONE,
 673        },
 674        [NF_SYSCTL_CT_HELPER] = {
 675                .procname       = "nf_conntrack_helper",
 676                .maxlen         = sizeof(u8),
 677                .mode           = 0644,
 678                .proc_handler   = proc_dou8vec_minmax,
 679                .extra1         = SYSCTL_ZERO,
 680                .extra2         = SYSCTL_ONE,
 681        },
 682#ifdef CONFIG_NF_CONNTRACK_EVENTS
 683        [NF_SYSCTL_CT_EVENTS] = {
 684                .procname       = "nf_conntrack_events",
 685                .data           = &init_net.ct.sysctl_events,
 686                .maxlen         = sizeof(u8),
 687                .mode           = 0644,
 688                .proc_handler   = proc_dou8vec_minmax,
 689                .extra1         = SYSCTL_ZERO,
 690                .extra2         = SYSCTL_ONE,
 691        },
 692#endif
 693#ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
 694        [NF_SYSCTL_CT_TIMESTAMP] = {
 695                .procname       = "nf_conntrack_timestamp",
 696                .data           = &init_net.ct.sysctl_tstamp,
 697                .maxlen         = sizeof(u8),
 698                .mode           = 0644,
 699                .proc_handler   = proc_dou8vec_minmax,
 700                .extra1         = SYSCTL_ZERO,
 701                .extra2         = SYSCTL_ONE,
 702        },
 703#endif
 704        [NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC] = {
 705                .procname       = "nf_conntrack_generic_timeout",
 706                .maxlen         = sizeof(unsigned int),
 707                .mode           = 0644,
 708                .proc_handler   = proc_dointvec_jiffies,
 709        },
 710        [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_SENT] = {
 711                .procname       = "nf_conntrack_tcp_timeout_syn_sent",
 712                .maxlen         = sizeof(unsigned int),
 713                .mode           = 0644,
 714                .proc_handler   = proc_dointvec_jiffies,
 715        },
 716        [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_SYN_RECV] = {
 717                .procname       = "nf_conntrack_tcp_timeout_syn_recv",
 718                .maxlen         = sizeof(unsigned int),
 719                .mode           = 0644,
 720                .proc_handler   = proc_dointvec_jiffies,
 721        },
 722        [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ESTABLISHED] = {
 723                .procname       = "nf_conntrack_tcp_timeout_established",
 724                .maxlen         = sizeof(unsigned int),
 725                .mode           = 0644,
 726                .proc_handler   = proc_dointvec_jiffies,
 727        },
 728        [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_FIN_WAIT] = {
 729                .procname       = "nf_conntrack_tcp_timeout_fin_wait",
 730                .maxlen         = sizeof(unsigned int),
 731                .mode           = 0644,
 732                .proc_handler   = proc_dointvec_jiffies,
 733        },
 734        [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE_WAIT] = {
 735                .procname       = "nf_conntrack_tcp_timeout_close_wait",
 736                .maxlen         = sizeof(unsigned int),
 737                .mode           = 0644,
 738                .proc_handler   = proc_dointvec_jiffies,
 739        },
 740        [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_LAST_ACK] = {
 741                .procname       = "nf_conntrack_tcp_timeout_last_ack",
 742                .maxlen         = sizeof(unsigned int),
 743                .mode           = 0644,
 744                .proc_handler   = proc_dointvec_jiffies,
 745        },
 746        [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_TIME_WAIT] = {
 747                .procname       = "nf_conntrack_tcp_timeout_time_wait",
 748                .maxlen         = sizeof(unsigned int),
 749                .mode           = 0644,
 750                .proc_handler   = proc_dointvec_jiffies,
 751        },
 752        [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE] = {
 753                .procname       = "nf_conntrack_tcp_timeout_close",
 754                .maxlen         = sizeof(unsigned int),
 755                .mode           = 0644,
 756                .proc_handler   = proc_dointvec_jiffies,
 757        },
 758        [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_RETRANS] = {
 759                .procname       = "nf_conntrack_tcp_timeout_max_retrans",
 760                .maxlen         = sizeof(unsigned int),
 761                .mode           = 0644,
 762                .proc_handler   = proc_dointvec_jiffies,
 763        },
 764        [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_UNACK] = {
 765                .procname       = "nf_conntrack_tcp_timeout_unacknowledged",
 766                .maxlen         = sizeof(unsigned int),
 767                .mode           = 0644,
 768                .proc_handler   = proc_dointvec_jiffies,
 769        },
 770#if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
 771        [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD] = {
 772                .procname       = "nf_flowtable_tcp_timeout",
 773                .maxlen         = sizeof(unsigned int),
 774                .mode           = 0644,
 775                .proc_handler   = proc_dointvec_jiffies,
 776        },
 777#endif
 778        [NF_SYSCTL_CT_PROTO_TCP_LOOSE] = {
 779                .procname       = "nf_conntrack_tcp_loose",
 780                .maxlen         = sizeof(u8),
 781                .mode           = 0644,
 782                .proc_handler   = proc_dou8vec_minmax,
 783                .extra1         = SYSCTL_ZERO,
 784                .extra2         = SYSCTL_ONE,
 785        },
 786        [NF_SYSCTL_CT_PROTO_TCP_LIBERAL] = {
 787                .procname       = "nf_conntrack_tcp_be_liberal",
 788                .maxlen         = sizeof(u8),
 789                .mode           = 0644,
 790                .proc_handler   = proc_dou8vec_minmax,
 791                .extra1         = SYSCTL_ZERO,
 792                .extra2         = SYSCTL_ONE,
 793        },
 794        [NF_SYSCTL_CT_PROTO_TCP_IGNORE_INVALID_RST] = {
 795                .procname       = "nf_conntrack_tcp_ignore_invalid_rst",
 796                .maxlen         = sizeof(u8),
 797                .mode           = 0644,
 798                .proc_handler   = proc_dou8vec_minmax,
 799                .extra1         = SYSCTL_ZERO,
 800                .extra2         = SYSCTL_ONE,
 801        },
 802        [NF_SYSCTL_CT_PROTO_TCP_MAX_RETRANS] = {
 803                .procname       = "nf_conntrack_tcp_max_retrans",
 804                .maxlen         = sizeof(u8),
 805                .mode           = 0644,
 806                .proc_handler   = proc_dou8vec_minmax,
 807        },
 808        [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP] = {
 809                .procname       = "nf_conntrack_udp_timeout",
 810                .maxlen         = sizeof(unsigned int),
 811                .mode           = 0644,
 812                .proc_handler   = proc_dointvec_jiffies,
 813        },
 814        [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM] = {
 815                .procname       = "nf_conntrack_udp_timeout_stream",
 816                .maxlen         = sizeof(unsigned int),
 817                .mode           = 0644,
 818                .proc_handler   = proc_dointvec_jiffies,
 819        },
 820#if IS_ENABLED(CONFIG_NFT_FLOW_OFFLOAD)
 821        [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD] = {
 822                .procname       = "nf_flowtable_udp_timeout",
 823                .maxlen         = sizeof(unsigned int),
 824                .mode           = 0644,
 825                .proc_handler   = proc_dointvec_jiffies,
 826        },
 827#endif
 828        [NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP] = {
 829                .procname       = "nf_conntrack_icmp_timeout",
 830                .maxlen         = sizeof(unsigned int),
 831                .mode           = 0644,
 832                .proc_handler   = proc_dointvec_jiffies,
 833        },
 834        [NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6] = {
 835                .procname       = "nf_conntrack_icmpv6_timeout",
 836                .maxlen         = sizeof(unsigned int),
 837                .mode           = 0644,
 838                .proc_handler   = proc_dointvec_jiffies,
 839        },
 840#ifdef CONFIG_NF_CT_PROTO_SCTP
 841        [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_CLOSED] = {
 842                .procname       = "nf_conntrack_sctp_timeout_closed",
 843                .maxlen         = sizeof(unsigned int),
 844                .mode           = 0644,
 845                .proc_handler   = proc_dointvec_jiffies,
 846        },
 847        [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_WAIT] = {
 848                .procname       = "nf_conntrack_sctp_timeout_cookie_wait",
 849                .maxlen         = sizeof(unsigned int),
 850                .mode           = 0644,
 851                .proc_handler   = proc_dointvec_jiffies,
 852        },
 853        [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_COOKIE_ECHOED] = {
 854                .procname       = "nf_conntrack_sctp_timeout_cookie_echoed",
 855                .maxlen         = sizeof(unsigned int),
 856                .mode           = 0644,
 857                .proc_handler   = proc_dointvec_jiffies,
 858        },
 859        [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ESTABLISHED] = {
 860                .procname       = "nf_conntrack_sctp_timeout_established",
 861                .maxlen         = sizeof(unsigned int),
 862                .mode           = 0644,
 863                .proc_handler   = proc_dointvec_jiffies,
 864        },
 865        [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_SENT] = {
 866                .procname       = "nf_conntrack_sctp_timeout_shutdown_sent",
 867                .maxlen         = sizeof(unsigned int),
 868                .mode           = 0644,
 869                .proc_handler   = proc_dointvec_jiffies,
 870        },
 871        [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_RECD] = {
 872                .procname       = "nf_conntrack_sctp_timeout_shutdown_recd",
 873                .maxlen         = sizeof(unsigned int),
 874                .mode           = 0644,
 875                .proc_handler   = proc_dointvec_jiffies,
 876        },
 877        [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_SHUTDOWN_ACK_SENT] = {
 878                .procname       = "nf_conntrack_sctp_timeout_shutdown_ack_sent",
 879                .maxlen         = sizeof(unsigned int),
 880                .mode           = 0644,
 881                .proc_handler   = proc_dointvec_jiffies,
 882        },
 883        [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_SENT] = {
 884                .procname       = "nf_conntrack_sctp_timeout_heartbeat_sent",
 885                .maxlen         = sizeof(unsigned int),
 886                .mode           = 0644,
 887                .proc_handler   = proc_dointvec_jiffies,
 888        },
 889        [NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_HEARTBEAT_ACKED] = {
 890                .procname       = "nf_conntrack_sctp_timeout_heartbeat_acked",
 891                .maxlen         = sizeof(unsigned int),
 892                .mode           = 0644,
 893                .proc_handler   = proc_dointvec_jiffies,
 894        },
 895#endif
 896#ifdef CONFIG_NF_CT_PROTO_DCCP
 897        [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_REQUEST] = {
 898                .procname       = "nf_conntrack_dccp_timeout_request",
 899                .maxlen         = sizeof(unsigned int),
 900                .mode           = 0644,
 901                .proc_handler   = proc_dointvec_jiffies,
 902        },
 903        [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_RESPOND] = {
 904                .procname       = "nf_conntrack_dccp_timeout_respond",
 905                .maxlen         = sizeof(unsigned int),
 906                .mode           = 0644,
 907                .proc_handler   = proc_dointvec_jiffies,
 908        },
 909        [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_PARTOPEN] = {
 910                .procname       = "nf_conntrack_dccp_timeout_partopen",
 911                .maxlen         = sizeof(unsigned int),
 912                .mode           = 0644,
 913                .proc_handler   = proc_dointvec_jiffies,
 914        },
 915        [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_OPEN] = {
 916                .procname       = "nf_conntrack_dccp_timeout_open",
 917                .maxlen         = sizeof(unsigned int),
 918                .mode           = 0644,
 919                .proc_handler   = proc_dointvec_jiffies,
 920        },
 921        [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSEREQ] = {
 922                .procname       = "nf_conntrack_dccp_timeout_closereq",
 923                .maxlen         = sizeof(unsigned int),
 924                .mode           = 0644,
 925                .proc_handler   = proc_dointvec_jiffies,
 926        },
 927        [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_CLOSING] = {
 928                .procname       = "nf_conntrack_dccp_timeout_closing",
 929                .maxlen         = sizeof(unsigned int),
 930                .mode           = 0644,
 931                .proc_handler   = proc_dointvec_jiffies,
 932        },
 933        [NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_TIMEWAIT] = {
 934                .procname       = "nf_conntrack_dccp_timeout_timewait",
 935                .maxlen         = sizeof(unsigned int),
 936                .mode           = 0644,
 937                .proc_handler   = proc_dointvec_jiffies,
 938        },
 939        [NF_SYSCTL_CT_PROTO_DCCP_LOOSE] = {
 940                .procname       = "nf_conntrack_dccp_loose",
 941                .maxlen         = sizeof(u8),
 942                .mode           = 0644,
 943                .proc_handler   = proc_dou8vec_minmax,
 944                .extra1         = SYSCTL_ZERO,
 945                .extra2         = SYSCTL_ONE,
 946        },
 947#endif
 948#ifdef CONFIG_NF_CT_PROTO_GRE
 949        [NF_SYSCTL_CT_PROTO_TIMEOUT_GRE] = {
 950                .procname       = "nf_conntrack_gre_timeout",
 951                .maxlen         = sizeof(unsigned int),
 952                .mode           = 0644,
 953                .proc_handler   = proc_dointvec_jiffies,
 954        },
 955        [NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM] = {
 956                .procname       = "nf_conntrack_gre_timeout_stream",
 957                .maxlen         = sizeof(unsigned int),
 958                .mode           = 0644,
 959                .proc_handler   = proc_dointvec_jiffies,
 960        },
 961#endif
 962        {}
 963};
 964
 965static struct ctl_table nf_ct_netfilter_table[] = {
 966        {
 967                .procname       = "nf_conntrack_max",
 968                .data           = &nf_conntrack_max,
 969                .maxlen         = sizeof(int),
 970                .mode           = 0644,
 971                .proc_handler   = proc_dointvec,
 972        },
 973        { }
 974};
 975
 976static void nf_conntrack_standalone_init_tcp_sysctl(struct net *net,
 977                                                    struct ctl_table *table)
 978{
 979        struct nf_tcp_net *tn = nf_tcp_pernet(net);
 980
 981#define XASSIGN(XNAME, tn) \
 982        table[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_ ## XNAME].data = \
 983                        &(tn)->timeouts[TCP_CONNTRACK_ ## XNAME]
 984
 985        XASSIGN(SYN_SENT, tn);
 986        XASSIGN(SYN_RECV, tn);
 987        XASSIGN(ESTABLISHED, tn);
 988        XASSIGN(FIN_WAIT, tn);
 989        XASSIGN(CLOSE_WAIT, tn);
 990        XASSIGN(LAST_ACK, tn);
 991        XASSIGN(TIME_WAIT, tn);
 992        XASSIGN(CLOSE, tn);
 993        XASSIGN(RETRANS, tn);
 994        XASSIGN(UNACK, tn);
 995#undef XASSIGN
 996#define XASSIGN(XNAME, rval) \
 997        table[NF_SYSCTL_CT_PROTO_TCP_ ## XNAME].data = (rval)
 998
 999        XASSIGN(LOOSE, &tn->tcp_loose);
1000        XASSIGN(LIBERAL, &tn->tcp_be_liberal);
1001        XASSIGN(MAX_RETRANS, &tn->tcp_max_retrans);
1002        XASSIGN(IGNORE_INVALID_RST, &tn->tcp_ignore_invalid_rst);
1003#undef XASSIGN
1004
1005#if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
1006        table[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD].data = &tn->offload_timeout;
1007#endif
1008
1009}
1010
1011static void nf_conntrack_standalone_init_sctp_sysctl(struct net *net,
1012                                                     struct ctl_table *table)
1013{
1014#ifdef CONFIG_NF_CT_PROTO_SCTP
1015        struct nf_sctp_net *sn = nf_sctp_pernet(net);
1016
1017#define XASSIGN(XNAME, sn) \
1018        table[NF_SYSCTL_CT_PROTO_TIMEOUT_SCTP_ ## XNAME].data = \
1019                        &(sn)->timeouts[SCTP_CONNTRACK_ ## XNAME]
1020
1021        XASSIGN(CLOSED, sn);
1022        XASSIGN(COOKIE_WAIT, sn);
1023        XASSIGN(COOKIE_ECHOED, sn);
1024        XASSIGN(ESTABLISHED, sn);
1025        XASSIGN(SHUTDOWN_SENT, sn);
1026        XASSIGN(SHUTDOWN_RECD, sn);
1027        XASSIGN(SHUTDOWN_ACK_SENT, sn);
1028        XASSIGN(HEARTBEAT_SENT, sn);
1029        XASSIGN(HEARTBEAT_ACKED, sn);
1030#undef XASSIGN
1031#endif
1032}
1033
1034static void nf_conntrack_standalone_init_dccp_sysctl(struct net *net,
1035                                                     struct ctl_table *table)
1036{
1037#ifdef CONFIG_NF_CT_PROTO_DCCP
1038        struct nf_dccp_net *dn = nf_dccp_pernet(net);
1039
1040#define XASSIGN(XNAME, dn) \
1041        table[NF_SYSCTL_CT_PROTO_TIMEOUT_DCCP_ ## XNAME].data = \
1042                        &(dn)->dccp_timeout[CT_DCCP_ ## XNAME]
1043
1044        XASSIGN(REQUEST, dn);
1045        XASSIGN(RESPOND, dn);
1046        XASSIGN(PARTOPEN, dn);
1047        XASSIGN(OPEN, dn);
1048        XASSIGN(CLOSEREQ, dn);
1049        XASSIGN(CLOSING, dn);
1050        XASSIGN(TIMEWAIT, dn);
1051#undef XASSIGN
1052
1053        table[NF_SYSCTL_CT_PROTO_DCCP_LOOSE].data = &dn->dccp_loose;
1054#endif
1055}
1056
1057static void nf_conntrack_standalone_init_gre_sysctl(struct net *net,
1058                                                    struct ctl_table *table)
1059{
1060#ifdef CONFIG_NF_CT_PROTO_GRE
1061        struct nf_gre_net *gn = nf_gre_pernet(net);
1062
1063        table[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE].data = &gn->timeouts[GRE_CT_UNREPLIED];
1064        table[NF_SYSCTL_CT_PROTO_TIMEOUT_GRE_STREAM].data = &gn->timeouts[GRE_CT_REPLIED];
1065#endif
1066}
1067
1068static int nf_conntrack_standalone_init_sysctl(struct net *net)
1069{
1070        struct nf_conntrack_net *cnet = nf_ct_pernet(net);
1071        struct nf_udp_net *un = nf_udp_pernet(net);
1072        struct ctl_table *table;
1073
1074        BUILD_BUG_ON(ARRAY_SIZE(nf_ct_sysctl_table) != NF_SYSCTL_CT_LAST_SYSCTL);
1075
1076        table = kmemdup(nf_ct_sysctl_table, sizeof(nf_ct_sysctl_table),
1077                        GFP_KERNEL);
1078        if (!table)
1079                return -ENOMEM;
1080
1081        table[NF_SYSCTL_CT_COUNT].data = &cnet->count;
1082        table[NF_SYSCTL_CT_CHECKSUM].data = &net->ct.sysctl_checksum;
1083        table[NF_SYSCTL_CT_LOG_INVALID].data = &net->ct.sysctl_log_invalid;
1084        table[NF_SYSCTL_CT_ACCT].data = &net->ct.sysctl_acct;
1085        table[NF_SYSCTL_CT_HELPER].data = &cnet->sysctl_auto_assign_helper;
1086#ifdef CONFIG_NF_CONNTRACK_EVENTS
1087        table[NF_SYSCTL_CT_EVENTS].data = &net->ct.sysctl_events;
1088#endif
1089#ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
1090        table[NF_SYSCTL_CT_TIMESTAMP].data = &net->ct.sysctl_tstamp;
1091#endif
1092        table[NF_SYSCTL_CT_PROTO_TIMEOUT_GENERIC].data = &nf_generic_pernet(net)->timeout;
1093        table[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP].data = &nf_icmp_pernet(net)->timeout;
1094        table[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6].data = &nf_icmpv6_pernet(net)->timeout;
1095        table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP].data = &un->timeouts[UDP_CT_UNREPLIED];
1096        table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM].data = &un->timeouts[UDP_CT_REPLIED];
1097#if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
1098        table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD].data = &un->offload_timeout;
1099#endif
1100
1101        nf_conntrack_standalone_init_tcp_sysctl(net, table);
1102        nf_conntrack_standalone_init_sctp_sysctl(net, table);
1103        nf_conntrack_standalone_init_dccp_sysctl(net, table);
1104        nf_conntrack_standalone_init_gre_sysctl(net, table);
1105
1106        /* Don't allow non-init_net ns to alter global sysctls */
1107        if (!net_eq(&init_net, net)) {
1108                table[NF_SYSCTL_CT_MAX].mode = 0444;
1109                table[NF_SYSCTL_CT_EXPECT_MAX].mode = 0444;
1110                table[NF_SYSCTL_CT_BUCKETS].mode = 0444;
1111        }
1112
1113        cnet->sysctl_header = register_net_sysctl(net, "net/netfilter", table);
1114        if (!cnet->sysctl_header)
1115                goto out_unregister_netfilter;
1116
1117        return 0;
1118
1119out_unregister_netfilter:
1120        kfree(table);
1121        return -ENOMEM;
1122}
1123
1124static void nf_conntrack_standalone_fini_sysctl(struct net *net)
1125{
1126        struct nf_conntrack_net *cnet = nf_ct_pernet(net);
1127        struct ctl_table *table;
1128
1129        table = cnet->sysctl_header->ctl_table_arg;
1130        unregister_net_sysctl_table(cnet->sysctl_header);
1131        kfree(table);
1132}
1133#else
1134static int nf_conntrack_standalone_init_sysctl(struct net *net)
1135{
1136        return 0;
1137}
1138
1139static void nf_conntrack_standalone_fini_sysctl(struct net *net)
1140{
1141}
1142#endif /* CONFIG_SYSCTL */
1143
1144static void nf_conntrack_fini_net(struct net *net)
1145{
1146        if (enable_hooks)
1147                nf_ct_netns_put(net, NFPROTO_INET);
1148
1149        nf_conntrack_standalone_fini_proc(net);
1150        nf_conntrack_standalone_fini_sysctl(net);
1151}
1152
1153static int nf_conntrack_pernet_init(struct net *net)
1154{
1155        int ret;
1156
1157        net->ct.sysctl_checksum = 1;
1158
1159        ret = nf_conntrack_standalone_init_sysctl(net);
1160        if (ret < 0)
1161                return ret;
1162
1163        ret = nf_conntrack_standalone_init_proc(net);
1164        if (ret < 0)
1165                goto out_proc;
1166
1167        ret = nf_conntrack_init_net(net);
1168        if (ret < 0)
1169                goto out_init_net;
1170
1171        if (enable_hooks) {
1172                ret = nf_ct_netns_get(net, NFPROTO_INET);
1173                if (ret < 0)
1174                        goto out_hooks;
1175        }
1176
1177        return 0;
1178
1179out_hooks:
1180        nf_conntrack_cleanup_net(net);
1181out_init_net:
1182        nf_conntrack_standalone_fini_proc(net);
1183out_proc:
1184        nf_conntrack_standalone_fini_sysctl(net);
1185        return ret;
1186}
1187
1188static void nf_conntrack_pernet_exit(struct list_head *net_exit_list)
1189{
1190        struct net *net;
1191
1192        list_for_each_entry(net, net_exit_list, exit_list)
1193                nf_conntrack_fini_net(net);
1194
1195        nf_conntrack_cleanup_net_list(net_exit_list);
1196}
1197
1198static struct pernet_operations nf_conntrack_net_ops = {
1199        .init           = nf_conntrack_pernet_init,
1200        .exit_batch     = nf_conntrack_pernet_exit,
1201        .id             = &nf_conntrack_net_id,
1202        .size = sizeof(struct nf_conntrack_net),
1203};
1204
1205static int __init nf_conntrack_standalone_init(void)
1206{
1207        int ret = nf_conntrack_init_start();
1208        if (ret < 0)
1209                goto out_start;
1210
1211        BUILD_BUG_ON(NFCT_INFOMASK <= IP_CT_NUMBER);
1212
1213#ifdef CONFIG_SYSCTL
1214        nf_ct_netfilter_header =
1215                register_net_sysctl(&init_net, "net", nf_ct_netfilter_table);
1216        if (!nf_ct_netfilter_header) {
1217                pr_err("nf_conntrack: can't register to sysctl.\n");
1218                ret = -ENOMEM;
1219                goto out_sysctl;
1220        }
1221
1222        nf_conntrack_htable_size_user = nf_conntrack_htable_size;
1223#endif
1224
1225        ret = register_pernet_subsys(&nf_conntrack_net_ops);
1226        if (ret < 0)
1227                goto out_pernet;
1228
1229        nf_conntrack_init_end();
1230        return 0;
1231
1232out_pernet:
1233#ifdef CONFIG_SYSCTL
1234        unregister_net_sysctl_table(nf_ct_netfilter_header);
1235out_sysctl:
1236#endif
1237        nf_conntrack_cleanup_end();
1238out_start:
1239        return ret;
1240}
1241
1242static void __exit nf_conntrack_standalone_fini(void)
1243{
1244        nf_conntrack_cleanup_start();
1245        unregister_pernet_subsys(&nf_conntrack_net_ops);
1246#ifdef CONFIG_SYSCTL
1247        unregister_net_sysctl_table(nf_ct_netfilter_header);
1248#endif
1249        nf_conntrack_cleanup_end();
1250}
1251
1252module_init(nf_conntrack_standalone_init);
1253module_exit(nf_conntrack_standalone_fini);
1254