]> git.proxmox.com Git - mirror_ubuntu-kernels.git/commitdiff
netfilter: ip_tables: pass table pointer via nf_hook_ops
authorFlorian Westphal <fw@strlen.de>
Wed, 21 Apr 2021 07:51:07 +0000 (09:51 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Mon, 26 Apr 2021 01:20:46 +0000 (03:20 +0200)
iptable_x modules rely on 'struct net' to contain a pointer to the
table that should be evaluated.

In order to remove these pointers from struct net, pass them via
the 'priv' pointer in a similar fashion as nf_tables passes the
rule data.

To do that, duplicate the nf_hook_info array passed in from the
iptable_x modules, update the ops->priv pointers of the copy to
refer to the table and then change the hookfn implementations to
just pass the 'priv' argument to the traverser.

After this patch, the xt_table pointers can already be removed
from struct net.

However, changes to struct net result in re-compile of the entire
network stack, so do the removal after arptables and ip6tables
have been converted as well.

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
include/linux/netfilter/x_tables.h
include/linux/netfilter_ipv4/ip_tables.h
net/ipv4/netfilter/ip_tables.c
net/ipv4/netfilter/iptable_filter.c
net/ipv4/netfilter/iptable_mangle.c
net/ipv4/netfilter/iptable_nat.c
net/ipv4/netfilter/iptable_raw.c
net/ipv4/netfilter/iptable_security.c
net/netfilter/x_tables.c

index b2eec7de5280d4fd27eac38e7ef22af8a86bdd56..a52cc22f806a75369f0d88d7cfe116c3f13049c5 100644 (file)
@@ -229,6 +229,9 @@ struct xt_table {
        /* Man behind the curtain... */
        struct xt_table_info *private;
 
+       /* hook ops that register the table with the netfilter core */
+       struct nf_hook_ops *ops;
+
        /* Set this to THIS_MODULE if you are a module, otherwise NULL */
        struct module *me;
 
index 73bcf7f261d2c7ad51605350a70764b58122e80e..0fdab3246ef52dea547551869146c8620935182e 100644 (file)
 
 int ipt_register_table(struct net *net, const struct xt_table *table,
                       const struct ipt_replace *repl,
-                      const struct nf_hook_ops *ops, struct xt_table **res);
-
-void ipt_unregister_table_pre_exit(struct net *net, const char *name,
-                                  const struct nf_hook_ops *ops);
+                      const struct nf_hook_ops *ops);
 
+void ipt_unregister_table_pre_exit(struct net *net, const char *name);
 void ipt_unregister_table_exit(struct net *net, const char *name);
 
 /* Standard entry. */
index 0b859ec2d3f893663f0f207b29200112c044889a..d6caaed5dd45f14d6afea8336b00d384a09b9b55 100644 (file)
@@ -1716,9 +1716,11 @@ static void __ipt_unregister_table(struct net *net, struct xt_table *table)
 
 int ipt_register_table(struct net *net, const struct xt_table *table,
                       const struct ipt_replace *repl,
-                      const struct nf_hook_ops *ops, struct xt_table **res)
+                      const struct nf_hook_ops *template_ops)
 {
-       int ret;
+       struct nf_hook_ops *ops;
+       unsigned int num_ops;
+       int ret, i;
        struct xt_table_info *newinfo;
        struct xt_table_info bootstrap = {0};
        void *loc_cpu_entry;
@@ -1732,40 +1734,57 @@ int ipt_register_table(struct net *net, const struct xt_table *table,
        memcpy(loc_cpu_entry, repl->entries, repl->size);
 
        ret = translate_table(net, newinfo, loc_cpu_entry, repl);
-       if (ret != 0)
-               goto out_free;
+       if (ret != 0) {
+               xt_free_table_info(newinfo);
+               return ret;
+       }
 
        new_table = xt_register_table(net, table, &bootstrap, newinfo);
        if (IS_ERR(new_table)) {
-               ret = PTR_ERR(new_table);
-               goto out_free;
+               xt_free_table_info(newinfo);
+               return PTR_ERR(new_table);
        }
 
-       /* set res now, will see skbs right after nf_register_net_hooks */
-       WRITE_ONCE(*res, new_table);
-       if (!ops)
+       /* No template? No need to do anything. This is used by 'nat' table, it registers
+        * with the nat core instead of the netfilter core.
+        */
+       if (!template_ops)
                return 0;
 
-       ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
-       if (ret != 0) {
-               __ipt_unregister_table(net, new_table);
-               *res = NULL;
+       num_ops = hweight32(table->valid_hooks);
+       if (num_ops == 0) {
+               ret = -EINVAL;
+               goto out_free;
        }
 
+       ops = kmemdup(template_ops, sizeof(*ops) * num_ops, GFP_KERNEL);
+       if (!ops) {
+               ret = -ENOMEM;
+               goto out_free;
+       }
+
+       for (i = 0; i < num_ops; i++)
+               ops[i].priv = new_table;
+
+       new_table->ops = ops;
+
+       ret = nf_register_net_hooks(net, ops, num_ops);
+       if (ret != 0)
+               goto out_free;
+
        return ret;
 
 out_free:
-       xt_free_table_info(newinfo);
+       __ipt_unregister_table(net, new_table);
        return ret;
 }
 
-void ipt_unregister_table_pre_exit(struct net *net, const char *name,
-                                  const struct nf_hook_ops *ops)
+void ipt_unregister_table_pre_exit(struct net *net, const char *name)
 {
        struct xt_table *table = xt_find_table(net, NFPROTO_IPV4, name);
 
        if (table)
-               nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
+               nf_unregister_net_hooks(net, table->ops, hweight32(table->valid_hooks));
 }
 
 void ipt_unregister_table_exit(struct net *net, const char *name)
index 84573fa78d1e9b00884ef540cd11c0b9e665de26..8272df7c6ad5c89e6ca84ae734cfc9eb23397642 100644 (file)
@@ -34,7 +34,7 @@ static unsigned int
 iptable_filter_hook(void *priv, struct sk_buff *skb,
                    const struct nf_hook_state *state)
 {
-       return ipt_do_table(skb, state, state->net->ipv4.iptable_filter);
+       return ipt_do_table(skb, state, priv);
 }
 
 static struct nf_hook_ops *filter_ops __read_mostly;
@@ -55,8 +55,7 @@ static int __net_init iptable_filter_table_init(struct net *net)
        ((struct ipt_standard *)repl->entries)[1].target.verdict =
                forward ? -NF_ACCEPT - 1 : -NF_DROP - 1;
 
-       err = ipt_register_table(net, &packet_filter, repl, filter_ops,
-                                &net->ipv4.iptable_filter);
+       err = ipt_register_table(net, &packet_filter, repl, filter_ops);
        kfree(repl);
        return err;
 }
@@ -71,13 +70,12 @@ static int __net_init iptable_filter_net_init(struct net *net)
 
 static void __net_exit iptable_filter_net_pre_exit(struct net *net)
 {
-       ipt_unregister_table_pre_exit(net, "filter", filter_ops);
+       ipt_unregister_table_pre_exit(net, "filter");
 }
 
 static void __net_exit iptable_filter_net_exit(struct net *net)
 {
        ipt_unregister_table_exit(net, "filter");
-       net->ipv4.iptable_filter = NULL;
 }
 
 static struct pernet_operations iptable_filter_net_ops = {
index 98e9e9053d85d28c2a3ce3a3aeaa55f7fa2709c2..2abc3836f391e3610ff1cf56f4076902d7615092 100644 (file)
@@ -37,7 +37,7 @@ static const struct xt_table packet_mangler = {
 };
 
 static unsigned int
-ipt_mangle_out(struct sk_buff *skb, const struct nf_hook_state *state)
+ipt_mangle_out(struct sk_buff *skb, const struct nf_hook_state *state, void *priv)
 {
        unsigned int ret;
        const struct iphdr *iph;
@@ -53,7 +53,7 @@ ipt_mangle_out(struct sk_buff *skb, const struct nf_hook_state *state)
        daddr = iph->daddr;
        tos = iph->tos;
 
-       ret = ipt_do_table(skb, state, state->net->ipv4.iptable_mangle);
+       ret = ipt_do_table(skb, state, priv);
        /* Reroute for ANY change. */
        if (ret != NF_DROP && ret != NF_STOLEN) {
                iph = ip_hdr(skb);
@@ -78,8 +78,8 @@ iptable_mangle_hook(void *priv,
                     const struct nf_hook_state *state)
 {
        if (state->hook == NF_INET_LOCAL_OUT)
-               return ipt_mangle_out(skb, state);
-       return ipt_do_table(skb, state, state->net->ipv4.iptable_mangle);
+               return ipt_mangle_out(skb, state, priv);
+       return ipt_do_table(skb, state, priv);
 }
 
 static struct nf_hook_ops *mangle_ops __read_mostly;
@@ -91,21 +91,19 @@ static int __net_init iptable_mangle_table_init(struct net *net)
        repl = ipt_alloc_initial_table(&packet_mangler);
        if (repl == NULL)
                return -ENOMEM;
-       ret = ipt_register_table(net, &packet_mangler, repl, mangle_ops,
-                                &net->ipv4.iptable_mangle);
+       ret = ipt_register_table(net, &packet_mangler, repl, mangle_ops);
        kfree(repl);
        return ret;
 }
 
 static void __net_exit iptable_mangle_net_pre_exit(struct net *net)
 {
-       ipt_unregister_table_pre_exit(net, "mangle", mangle_ops);
+       ipt_unregister_table_pre_exit(net, "mangle");
 }
 
 static void __net_exit iptable_mangle_net_exit(struct net *net)
 {
        ipt_unregister_table_exit(net, "mangle");
-       net->ipv4.iptable_mangle = NULL;
 }
 
 static struct pernet_operations iptable_mangle_net_ops = {
index dfa9dc63a7b51cfb5b0486e7384e4079499947b1..a9913842ef18e573ce42278c2e3f2fc808842ab1 100644 (file)
@@ -66,12 +66,19 @@ static const struct nf_hook_ops nf_nat_ipv4_ops[] = {
        },
 };
 
-static int ipt_nat_register_lookups(struct net *net, struct xt_table *table)
+static int ipt_nat_register_lookups(struct net *net)
 {
-       struct nf_hook_ops *ops = kmemdup(nf_nat_ipv4_ops, sizeof(nf_nat_ipv4_ops), GFP_KERNEL);
-       struct iptable_nat_pernet *xt_nat_net = net_generic(net, iptable_nat_net_id);
+       struct iptable_nat_pernet *xt_nat_net;
+       struct nf_hook_ops *ops;
+       struct xt_table *table;
        int i, ret;
 
+       xt_nat_net = net_generic(net, iptable_nat_net_id);
+       table = xt_find_table(net, NFPROTO_IPV4, "nat");
+       if (WARN_ON_ONCE(!table))
+               return -ENOENT;
+
+       ops = kmemdup(nf_nat_ipv4_ops, sizeof(nf_nat_ipv4_ops), GFP_KERNEL);
        if (!ops)
                return -ENOMEM;
 
@@ -109,25 +116,21 @@ static void ipt_nat_unregister_lookups(struct net *net)
 static int __net_init iptable_nat_table_init(struct net *net)
 {
        struct ipt_replace *repl;
-       struct xt_table *table;
        int ret;
 
        repl = ipt_alloc_initial_table(&nf_nat_ipv4_table);
        if (repl == NULL)
                return -ENOMEM;
-       ret = ipt_register_table(net, &nf_nat_ipv4_table, repl,
-                                NULL, &table);
+
+       ret = ipt_register_table(net, &nf_nat_ipv4_table, repl, NULL);
        if (ret < 0) {
                kfree(repl);
                return ret;
        }
 
-       ret = ipt_nat_register_lookups(net, table);
-       if (ret < 0) {
+       ret = ipt_nat_register_lookups(net);
+       if (ret < 0)
                ipt_unregister_table_exit(net, "nat");
-       } else {
-               net->ipv4.nat_table = table;
-       }
 
        kfree(repl);
        return ret;
@@ -141,7 +144,6 @@ static void __net_exit iptable_nat_net_pre_exit(struct net *net)
 static void __net_exit iptable_nat_net_exit(struct net *net)
 {
        ipt_unregister_table_exit(net, "nat");
-       net->ipv4.nat_table = NULL;
 }
 
 static struct pernet_operations iptable_nat_net_ops = {
index 18776f5a4055c8fdaf4347429c05717d0ef6da96..ceef397c1f5f3eb9f702b3b9cf95b7b1d39ee785 100644 (file)
@@ -41,7 +41,7 @@ static unsigned int
 iptable_raw_hook(void *priv, struct sk_buff *skb,
                 const struct nf_hook_state *state)
 {
-       return ipt_do_table(skb, state, state->net->ipv4.iptable_raw);
+       return ipt_do_table(skb, state, priv);
 }
 
 static struct nf_hook_ops *rawtable_ops __read_mostly;
@@ -58,21 +58,19 @@ static int __net_init iptable_raw_table_init(struct net *net)
        repl = ipt_alloc_initial_table(table);
        if (repl == NULL)
                return -ENOMEM;
-       ret = ipt_register_table(net, table, repl, rawtable_ops,
-                                &net->ipv4.iptable_raw);
+       ret = ipt_register_table(net, table, repl, rawtable_ops);
        kfree(repl);
        return ret;
 }
 
 static void __net_exit iptable_raw_net_pre_exit(struct net *net)
 {
-       ipt_unregister_table_pre_exit(net, "raw", rawtable_ops);
+       ipt_unregister_table_pre_exit(net, "raw");
 }
 
 static void __net_exit iptable_raw_net_exit(struct net *net)
 {
        ipt_unregister_table_exit(net, "raw");
-       net->ipv4.iptable_raw = NULL;
 }
 
 static struct pernet_operations iptable_raw_net_ops = {
index 3df92fb394c5a54f6faae7bf93b4d380a2836ac5..77973f5fd8f6e9332d1a55afef5d41a963e855a6 100644 (file)
@@ -40,7 +40,7 @@ static unsigned int
 iptable_security_hook(void *priv, struct sk_buff *skb,
                      const struct nf_hook_state *state)
 {
-       return ipt_do_table(skb, state, state->net->ipv4.iptable_security);
+       return ipt_do_table(skb, state, priv);
 }
 
 static struct nf_hook_ops *sectbl_ops __read_mostly;
@@ -53,21 +53,19 @@ static int __net_init iptable_security_table_init(struct net *net)
        repl = ipt_alloc_initial_table(&security_table);
        if (repl == NULL)
                return -ENOMEM;
-       ret = ipt_register_table(net, &security_table, repl, sectbl_ops,
-                                &net->ipv4.iptable_security);
+       ret = ipt_register_table(net, &security_table, repl, sectbl_ops);
        kfree(repl);
        return ret;
 }
 
 static void __net_exit iptable_security_net_pre_exit(struct net *net)
 {
-       ipt_unregister_table_pre_exit(net, "security", sectbl_ops);
+       ipt_unregister_table_pre_exit(net, "security");
 }
 
 static void __net_exit iptable_security_net_exit(struct net *net)
 {
        ipt_unregister_table_exit(net, "security");
-       net->ipv4.iptable_security = NULL;
 }
 
 static struct pernet_operations iptable_security_net_ops = {
index 1caba9507228462f0177f65774fc6399c82395a9..ef37deff8405c723f74b7e62ec977ea302136180 100644 (file)
@@ -1498,6 +1498,7 @@ void *xt_unregister_table(struct xt_table *table)
        mutex_unlock(&xt[table->af].mutex);
        audit_log_nfcfg(table->name, table->af, private->number,
                        AUDIT_XT_OP_UNREGISTER, GFP_KERNEL);
+       kfree(table->ops);
        kfree(table);
 
        return private;