]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blobdiff - net/netfilter/nf_conntrack_core.c
netfilter: netns nf_conntrack: per-netns expectations
[mirror_ubuntu-zesty-kernel.git] / net / netfilter / nf_conntrack_core.c
index 9d1830da8e84b86286c793cf8f58f98f10f793ce..c188edea24921f4644af6183943023ea22d4545c 100644 (file)
 DEFINE_SPINLOCK(nf_conntrack_lock);
 EXPORT_SYMBOL_GPL(nf_conntrack_lock);
 
-/* nf_conntrack_standalone needs this */
-atomic_t nf_conntrack_count = ATOMIC_INIT(0);
-EXPORT_SYMBOL_GPL(nf_conntrack_count);
-
 unsigned int nf_conntrack_htable_size __read_mostly;
 EXPORT_SYMBOL_GPL(nf_conntrack_htable_size);
 
 int nf_conntrack_max __read_mostly;
 EXPORT_SYMBOL_GPL(nf_conntrack_max);
 
-struct hlist_head *nf_conntrack_hash __read_mostly;
-EXPORT_SYMBOL_GPL(nf_conntrack_hash);
-
 struct nf_conn nf_conntrack_untracked __read_mostly;
 EXPORT_SYMBOL_GPL(nf_conntrack_untracked);
 
 unsigned int nf_ct_log_invalid __read_mostly;
 HLIST_HEAD(unconfirmed);
-static int nf_conntrack_vmalloc __read_mostly;
 static struct kmem_cache *nf_conntrack_cachep __read_mostly;
 
 DEFINE_PER_CPU(struct ip_conntrack_stat, nf_conntrack_stat);
@@ -246,7 +238,7 @@ static void death_by_timeout(unsigned long ul_conntrack)
 }
 
 struct nf_conntrack_tuple_hash *
-__nf_conntrack_find(const struct nf_conntrack_tuple *tuple)
+__nf_conntrack_find(struct net *net, const struct nf_conntrack_tuple *tuple)
 {
        struct nf_conntrack_tuple_hash *h;
        struct hlist_node *n;
@@ -256,7 +248,7 @@ __nf_conntrack_find(const struct nf_conntrack_tuple *tuple)
         * at least once for the stats anyway.
         */
        local_bh_disable();
-       hlist_for_each_entry_rcu(h, n, &nf_conntrack_hash[hash], hnode) {
+       hlist_for_each_entry_rcu(h, n, &net->ct.hash[hash], hnode) {
                if (nf_ct_tuple_equal(tuple, &h->tuple)) {
                        NF_CT_STAT_INC(found);
                        local_bh_enable();
@@ -272,13 +264,13 @@ EXPORT_SYMBOL_GPL(__nf_conntrack_find);
 
 /* Find a connection corresponding to a tuple. */
 struct nf_conntrack_tuple_hash *
-nf_conntrack_find_get(const struct nf_conntrack_tuple *tuple)
+nf_conntrack_find_get(struct net *net, const struct nf_conntrack_tuple *tuple)
 {
        struct nf_conntrack_tuple_hash *h;
        struct nf_conn *ct;
 
        rcu_read_lock();
-       h = __nf_conntrack_find(tuple);
+       h = __nf_conntrack_find(net, tuple);
        if (h) {
                ct = nf_ct_tuplehash_to_ctrack(h);
                if (unlikely(!atomic_inc_not_zero(&ct->ct_general.use)))
@@ -294,10 +286,12 @@ static void __nf_conntrack_hash_insert(struct nf_conn *ct,
                                       unsigned int hash,
                                       unsigned int repl_hash)
 {
+       struct net *net = nf_ct_net(ct);
+
        hlist_add_head_rcu(&ct->tuplehash[IP_CT_DIR_ORIGINAL].hnode,
-                          &nf_conntrack_hash[hash]);
+                          &net->ct.hash[hash]);
        hlist_add_head_rcu(&ct->tuplehash[IP_CT_DIR_REPLY].hnode,
-                          &nf_conntrack_hash[repl_hash]);
+                          &net->ct.hash[repl_hash]);
 }
 
 void nf_conntrack_hash_insert(struct nf_conn *ct)
@@ -323,8 +317,10 @@ __nf_conntrack_confirm(struct sk_buff *skb)
        struct nf_conn_help *help;
        struct hlist_node *n;
        enum ip_conntrack_info ctinfo;
+       struct net *net;
 
        ct = nf_ct_get(skb, &ctinfo);
+       net = nf_ct_net(ct);
 
        /* ipt_REJECT uses nf_conntrack_attach to attach related
           ICMP/TCP RST packets in other direction.  Actual packet
@@ -351,11 +347,11 @@ __nf_conntrack_confirm(struct sk_buff *skb)
        /* See if there's one in the list already, including reverse:
           NAT could have grabbed it without realizing, since we're
           not in the hash.  If there is, we lost race. */
-       hlist_for_each_entry(h, n, &nf_conntrack_hash[hash], hnode)
+       hlist_for_each_entry(h, n, &net->ct.hash[hash], hnode)
                if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
                                      &h->tuple))
                        goto out;
-       hlist_for_each_entry(h, n, &nf_conntrack_hash[repl_hash], hnode)
+       hlist_for_each_entry(h, n, &net->ct.hash[repl_hash], hnode)
                if (nf_ct_tuple_equal(&ct->tuplehash[IP_CT_DIR_REPLY].tuple,
                                      &h->tuple))
                        goto out;
@@ -398,6 +394,7 @@ int
 nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
                         const struct nf_conn *ignored_conntrack)
 {
+       struct net *net = nf_ct_net(ignored_conntrack);
        struct nf_conntrack_tuple_hash *h;
        struct hlist_node *n;
        unsigned int hash = hash_conntrack(tuple);
@@ -406,7 +403,7 @@ nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
         * least once for the stats anyway.
         */
        rcu_read_lock_bh();
-       hlist_for_each_entry_rcu(h, n, &nf_conntrack_hash[hash], hnode) {
+       hlist_for_each_entry_rcu(h, n, &net->ct.hash[hash], hnode) {
                if (nf_ct_tuplehash_to_ctrack(h) != ignored_conntrack &&
                    nf_ct_tuple_equal(tuple, &h->tuple)) {
                        NF_CT_STAT_INC(found);
@@ -425,7 +422,7 @@ EXPORT_SYMBOL_GPL(nf_conntrack_tuple_taken);
 
 /* There's a small race here where we may free a just-assured
    connection.  Too bad: we're in trouble anyway. */
-static noinline int early_drop(unsigned int hash)
+static noinline int early_drop(struct net *net, unsigned int hash)
 {
        /* Use oldest entry, which is roughly LRU */
        struct nf_conntrack_tuple_hash *h;
@@ -436,7 +433,7 @@ static noinline int early_drop(unsigned int hash)
 
        rcu_read_lock();
        for (i = 0; i < nf_conntrack_htable_size; i++) {
-               hlist_for_each_entry_rcu(h, n, &nf_conntrack_hash[hash],
+               hlist_for_each_entry_rcu(h, n, &net->ct.hash[hash],
                                         hnode) {
                        tmp = nf_ct_tuplehash_to_ctrack(h);
                        if (!test_bit(IPS_ASSURED_BIT, &tmp->status))
@@ -464,7 +461,8 @@ static noinline int early_drop(unsigned int hash)
        return dropped;
 }
 
-struct nf_conn *nf_conntrack_alloc(const struct nf_conntrack_tuple *orig,
+struct nf_conn *nf_conntrack_alloc(struct net *net,
+                                  const struct nf_conntrack_tuple *orig,
                                   const struct nf_conntrack_tuple *repl,
                                   gfp_t gfp)
 {
@@ -476,13 +474,13 @@ struct nf_conn *nf_conntrack_alloc(const struct nf_conntrack_tuple *orig,
        }
 
        /* We don't want any race condition at early drop stage */
-       atomic_inc(&nf_conntrack_count);
+       atomic_inc(&net->ct.count);
 
        if (nf_conntrack_max &&
-           unlikely(atomic_read(&nf_conntrack_count) > nf_conntrack_max)) {
+           unlikely(atomic_read(&net->ct.count) > nf_conntrack_max)) {
                unsigned int hash = hash_conntrack(orig);
-               if (!early_drop(hash)) {
-                       atomic_dec(&nf_conntrack_count);
+               if (!early_drop(net, hash)) {
+                       atomic_dec(&net->ct.count);
                        if (net_ratelimit())
                                printk(KERN_WARNING
                                       "nf_conntrack: table full, dropping"
@@ -494,7 +492,7 @@ struct nf_conn *nf_conntrack_alloc(const struct nf_conntrack_tuple *orig,
        ct = kmem_cache_zalloc(nf_conntrack_cachep, gfp);
        if (ct == NULL) {
                pr_debug("nf_conntrack_alloc: Can't alloc conntrack.\n");
-               atomic_dec(&nf_conntrack_count);
+               atomic_dec(&net->ct.count);
                return ERR_PTR(-ENOMEM);
        }
 
@@ -503,6 +501,9 @@ struct nf_conn *nf_conntrack_alloc(const struct nf_conntrack_tuple *orig,
        ct->tuplehash[IP_CT_DIR_REPLY].tuple = *repl;
        /* Don't set timer yet: wait for confirmation */
        setup_timer(&ct->timeout, death_by_timeout, (unsigned long)ct);
+#ifdef CONFIG_NET_NS
+       ct->ct_net = net;
+#endif
        INIT_RCU_HEAD(&ct->rcu);
 
        return ct;
@@ -512,10 +513,11 @@ EXPORT_SYMBOL_GPL(nf_conntrack_alloc);
 static void nf_conntrack_free_rcu(struct rcu_head *head)
 {
        struct nf_conn *ct = container_of(head, struct nf_conn, rcu);
+       struct net *net = nf_ct_net(ct);
 
        nf_ct_ext_free(ct);
        kmem_cache_free(nf_conntrack_cachep, ct);
-       atomic_dec(&nf_conntrack_count);
+       atomic_dec(&net->ct.count);
 }
 
 void nf_conntrack_free(struct nf_conn *ct)
@@ -528,7 +530,8 @@ EXPORT_SYMBOL_GPL(nf_conntrack_free);
 /* Allocate a new conntrack: we return -ENOMEM if classification
    failed due to stress.  Otherwise it really is unclassifiable. */
 static struct nf_conntrack_tuple_hash *
-init_conntrack(const struct nf_conntrack_tuple *tuple,
+init_conntrack(struct net *net,
+              const struct nf_conntrack_tuple *tuple,
               struct nf_conntrack_l3proto *l3proto,
               struct nf_conntrack_l4proto *l4proto,
               struct sk_buff *skb,
@@ -544,7 +547,7 @@ init_conntrack(const struct nf_conntrack_tuple *tuple,
                return NULL;
        }
 
-       ct = nf_conntrack_alloc(tuple, &repl_tuple, GFP_ATOMIC);
+       ct = nf_conntrack_alloc(net, tuple, &repl_tuple, GFP_ATOMIC);
        if (ct == NULL || IS_ERR(ct)) {
                pr_debug("Can't allocate conntrack.\n");
                return (struct nf_conntrack_tuple_hash *)ct;
@@ -559,7 +562,7 @@ init_conntrack(const struct nf_conntrack_tuple *tuple,
        nf_ct_acct_ext_add(ct, GFP_ATOMIC);
 
        spin_lock_bh(&nf_conntrack_lock);
-       exp = nf_ct_find_expectation(tuple);
+       exp = nf_ct_find_expectation(net, tuple);
        if (exp) {
                pr_debug("conntrack: expectation arrives ct=%p exp=%p\n",
                         ct, exp);
@@ -629,9 +632,10 @@ resolve_normal_ct(struct sk_buff *skb,
        }
 
        /* look for tuple match */
-       h = nf_conntrack_find_get(&tuple);
+       h = nf_conntrack_find_get(&init_net, &tuple);
        if (!h) {
-               h = init_conntrack(&tuple, l3proto, l4proto, skb, dataoff);
+               h = init_conntrack(&init_net, &tuple, l3proto, l4proto, skb,
+                                  dataoff);
                if (!h)
                        return NULL;
                if (IS_ERR(h))
@@ -665,7 +669,7 @@ resolve_normal_ct(struct sk_buff *skb,
 }
 
 unsigned int
-nf_conntrack_in(int pf, unsigned int hooknum, struct sk_buff *skb)
+nf_conntrack_in(u_int8_t pf, unsigned int hooknum, struct sk_buff *skb)
 {
        struct nf_conn *ct;
        enum ip_conntrack_info ctinfo;
@@ -683,7 +687,7 @@ nf_conntrack_in(int pf, unsigned int hooknum, struct sk_buff *skb)
        }
 
        /* rcu_read_lock()ed by nf_hook_slow */
-       l3proto = __nf_ct_l3proto_find((u_int16_t)pf);
+       l3proto = __nf_ct_l3proto_find(pf);
        ret = l3proto->get_l4proto(skb, skb_network_offset(skb),
                                   &dataoff, &protonum);
        if (ret <= 0) {
@@ -693,7 +697,7 @@ nf_conntrack_in(int pf, unsigned int hooknum, struct sk_buff *skb)
                return -ret;
        }
 
-       l4proto = __nf_ct_l4proto_find((u_int16_t)pf, protonum);
+       l4proto = __nf_ct_l4proto_find(pf, protonum);
 
        /* It may be an special packet, error, unclean...
         * inverse of the return code tells to the netfilter
@@ -938,7 +942,7 @@ static void nf_conntrack_attach(struct sk_buff *nskb, struct sk_buff *skb)
 
 /* Bring out ya dead! */
 static struct nf_conn *
-get_next_corpse(int (*iter)(struct nf_conn *i, void *data),
+get_next_corpse(struct net *net, int (*iter)(struct nf_conn *i, void *data),
                void *data, unsigned int *bucket)
 {
        struct nf_conntrack_tuple_hash *h;
@@ -947,7 +951,7 @@ get_next_corpse(int (*iter)(struct nf_conn *i, void *data),
 
        spin_lock_bh(&nf_conntrack_lock);
        for (; *bucket < nf_conntrack_htable_size; (*bucket)++) {
-               hlist_for_each_entry(h, n, &nf_conntrack_hash[*bucket], hnode) {
+               hlist_for_each_entry(h, n, &net->ct.hash[*bucket], hnode) {
                        ct = nf_ct_tuplehash_to_ctrack(h);
                        if (iter(ct, data))
                                goto found;
@@ -966,13 +970,14 @@ found:
        return ct;
 }
 
-void
-nf_ct_iterate_cleanup(int (*iter)(struct nf_conn *i, void *data), void *data)
+void nf_ct_iterate_cleanup(struct net *net,
+                          int (*iter)(struct nf_conn *i, void *data),
+                          void *data)
 {
        struct nf_conn *ct;
        unsigned int bucket = 0;
 
-       while ((ct = get_next_corpse(iter, data, &bucket)) != NULL) {
+       while ((ct = get_next_corpse(net, iter, data, &bucket)) != NULL) {
                /* Time to push up daises... */
                if (del_timer(&ct->timeout))
                        death_by_timeout((unsigned long)ct);
@@ -998,15 +1003,15 @@ void nf_ct_free_hashtable(struct hlist_head *hash, int vmalloced, unsigned int s
 }
 EXPORT_SYMBOL_GPL(nf_ct_free_hashtable);
 
-void nf_conntrack_flush(void)
+void nf_conntrack_flush(struct net *net)
 {
-       nf_ct_iterate_cleanup(kill_all, NULL);
+       nf_ct_iterate_cleanup(net, kill_all, NULL);
 }
 EXPORT_SYMBOL_GPL(nf_conntrack_flush);
 
 /* Mishearing the voices in his head, our hero wonders how he's
    supposed to kill the mall. */
-void nf_conntrack_cleanup(void)
+void nf_conntrack_cleanup(struct net *net)
 {
        rcu_assign_pointer(ip_ct_attach, NULL);
 
@@ -1017,8 +1022,8 @@ void nf_conntrack_cleanup(void)
 
        nf_ct_event_cache_flush();
  i_see_dead_people:
-       nf_conntrack_flush();
-       if (atomic_read(&nf_conntrack_count) != 0) {
+       nf_conntrack_flush(net);
+       if (atomic_read(&net->ct.count) != 0) {
                schedule();
                goto i_see_dead_people;
        }
@@ -1029,11 +1034,11 @@ void nf_conntrack_cleanup(void)
        rcu_assign_pointer(nf_ct_destroy, NULL);
 
        kmem_cache_destroy(nf_conntrack_cachep);
-       nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_vmalloc,
+       nf_ct_free_hashtable(net->ct.hash, net->ct.hash_vmalloc,
                             nf_conntrack_htable_size);
 
        nf_conntrack_acct_fini();
-       nf_conntrack_expect_fini();
+       nf_conntrack_expect_fini(net);
        nf_conntrack_helper_fini();
        nf_conntrack_proto_fini();
 }
@@ -1094,8 +1099,8 @@ int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp)
         */
        spin_lock_bh(&nf_conntrack_lock);
        for (i = 0; i < nf_conntrack_htable_size; i++) {
-               while (!hlist_empty(&nf_conntrack_hash[i])) {
-                       h = hlist_entry(nf_conntrack_hash[i].first,
+               while (!hlist_empty(&init_net.ct.hash[i])) {
+                       h = hlist_entry(init_net.ct.hash[i].first,
                                        struct nf_conntrack_tuple_hash, hnode);
                        hlist_del_rcu(&h->hnode);
                        bucket = __hash_conntrack(&h->tuple, hashsize, rnd);
@@ -1103,12 +1108,12 @@ int nf_conntrack_set_hashsize(const char *val, struct kernel_param *kp)
                }
        }
        old_size = nf_conntrack_htable_size;
-       old_vmalloced = nf_conntrack_vmalloc;
-       old_hash = nf_conntrack_hash;
+       old_vmalloced = init_net.ct.hash_vmalloc;
+       old_hash = init_net.ct.hash;
 
        nf_conntrack_htable_size = hashsize;
-       nf_conntrack_vmalloc = vmalloced;
-       nf_conntrack_hash = hash;
+       init_net.ct.hash_vmalloc = vmalloced;
+       init_net.ct.hash = hash;
        nf_conntrack_hash_rnd = rnd;
        spin_unlock_bh(&nf_conntrack_lock);
 
@@ -1120,7 +1125,7 @@ EXPORT_SYMBOL_GPL(nf_conntrack_set_hashsize);
 module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint,
                  &nf_conntrack_htable_size, 0600);
 
-int __init nf_conntrack_init(void)
+int nf_conntrack_init(struct net *net)
 {
        int max_factor = 8;
        int ret;
@@ -1142,9 +1147,10 @@ int __init nf_conntrack_init(void)
                 * entries. */
                max_factor = 4;
        }
-       nf_conntrack_hash = nf_ct_alloc_hashtable(&nf_conntrack_htable_size,
-                                                 &nf_conntrack_vmalloc);
-       if (!nf_conntrack_hash) {
+       atomic_set(&net->ct.count, 0);
+       net->ct.hash = nf_ct_alloc_hashtable(&nf_conntrack_htable_size,
+                                                 &net->ct.hash_vmalloc);
+       if (!net->ct.hash) {
                printk(KERN_ERR "Unable to create nf_conntrack_hash\n");
                goto err_out;
        }
@@ -1167,7 +1173,7 @@ int __init nf_conntrack_init(void)
        if (ret < 0)
                goto err_free_conntrack_slab;
 
-       ret = nf_conntrack_expect_init();
+       ret = nf_conntrack_expect_init(net);
        if (ret < 0)
                goto out_fini_proto;
 
@@ -1185,6 +1191,9 @@ int __init nf_conntrack_init(void)
 
        /* Set up fake conntrack:
            - to never be deleted, not in any hashes */
+#ifdef CONFIG_NET_NS
+       nf_conntrack_untracked.ct_net = &init_net;
+#endif
        atomic_set(&nf_conntrack_untracked.ct_general.use, 1);
        /*  - and look it like as a confirmed connection */
        set_bit(IPS_CONFIRMED_BIT, &nf_conntrack_untracked.status);
@@ -1194,13 +1203,13 @@ int __init nf_conntrack_init(void)
 out_fini_helper:
        nf_conntrack_helper_fini();
 out_fini_expect:
-       nf_conntrack_expect_fini();
+       nf_conntrack_expect_fini(net);
 out_fini_proto:
        nf_conntrack_proto_fini();
 err_free_conntrack_slab:
        kmem_cache_destroy(nf_conntrack_cachep);
 err_free_hash:
-       nf_ct_free_hashtable(nf_conntrack_hash, nf_conntrack_vmalloc,
+       nf_ct_free_hashtable(net->ct.hash, net->ct.hash_vmalloc,
                             nf_conntrack_htable_size);
 err_out:
        return -ENOMEM;