Skip to content

Commit ae68933

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: ip_tables: pass table pointer via nf_hook_ops
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>
1 parent a4aeafa commit ae68933

9 files changed

Lines changed: 71 additions & 56 deletions

File tree

‎include/linux/netfilter/x_tables.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ struct xt_table {
229229
/* Man behind the curtain... */
230230
struct xt_table_info *private;
231231

232+
/* hook ops that register the table with the netfilter core */
233+
struct nf_hook_ops *ops;
234+
232235
/* Set this to THIS_MODULE if you are a module, otherwise NULL */
233236
struct module *me;
234237

‎include/linux/netfilter_ipv4/ip_tables.h‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@
2424

2525
int ipt_register_table(struct net *net, const struct xt_table *table,
2626
const struct ipt_replace *repl,
27-
const struct nf_hook_ops *ops, struct xt_table **res);
28-
29-
void ipt_unregister_table_pre_exit(struct net *net, const char *name,
30-
const struct nf_hook_ops *ops);
27+
const struct nf_hook_ops *ops);
3128

29+
void ipt_unregister_table_pre_exit(struct net *net, const char *name);
3230
void ipt_unregister_table_exit(struct net *net, const char *name);
3331

3432
/* Standard entry. */

‎net/ipv4/netfilter/ip_tables.c‎

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,9 +1716,11 @@ static void __ipt_unregister_table(struct net *net, struct xt_table *table)
17161716

17171717
int ipt_register_table(struct net *net, const struct xt_table *table,
17181718
const struct ipt_replace *repl,
1719-
const struct nf_hook_ops *ops, struct xt_table **res)
1719+
const struct nf_hook_ops *template_ops)
17201720
{
1721-
int ret;
1721+
struct nf_hook_ops *ops;
1722+
unsigned int num_ops;
1723+
int ret, i;
17221724
struct xt_table_info *newinfo;
17231725
struct xt_table_info bootstrap = {0};
17241726
void *loc_cpu_entry;
@@ -1732,40 +1734,57 @@ int ipt_register_table(struct net *net, const struct xt_table *table,
17321734
memcpy(loc_cpu_entry, repl->entries, repl->size);
17331735

17341736
ret = translate_table(net, newinfo, loc_cpu_entry, repl);
1735-
if (ret != 0)
1736-
goto out_free;
1737+
if (ret != 0) {
1738+
xt_free_table_info(newinfo);
1739+
return ret;
1740+
}
17371741

17381742
new_table = xt_register_table(net, table, &bootstrap, newinfo);
17391743
if (IS_ERR(new_table)) {
1740-
ret = PTR_ERR(new_table);
1741-
goto out_free;
1744+
xt_free_table_info(newinfo);
1745+
return PTR_ERR(new_table);
17421746
}
17431747

1744-
/* set res now, will see skbs right after nf_register_net_hooks */
1745-
WRITE_ONCE(*res, new_table);
1746-
if (!ops)
1748+
/* No template? No need to do anything. This is used by 'nat' table, it registers
1749+
* with the nat core instead of the netfilter core.
1750+
*/
1751+
if (!template_ops)
17471752
return 0;
17481753

1749-
ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
1750-
if (ret != 0) {
1751-
__ipt_unregister_table(net, new_table);
1752-
*res = NULL;
1754+
num_ops = hweight32(table->valid_hooks);
1755+
if (num_ops == 0) {
1756+
ret = -EINVAL;
1757+
goto out_free;
17531758
}
17541759

1760+
ops = kmemdup(template_ops, sizeof(*ops) * num_ops, GFP_KERNEL);
1761+
if (!ops) {
1762+
ret = -ENOMEM;
1763+
goto out_free;
1764+
}
1765+
1766+
for (i = 0; i < num_ops; i++)
1767+
ops[i].priv = new_table;
1768+
1769+
new_table->ops = ops;
1770+
1771+
ret = nf_register_net_hooks(net, ops, num_ops);
1772+
if (ret != 0)
1773+
goto out_free;
1774+
17551775
return ret;
17561776

17571777
out_free:
1758-
xt_free_table_info(newinfo);
1778+
__ipt_unregister_table(net, new_table);
17591779
return ret;
17601780
}
17611781

1762-
void ipt_unregister_table_pre_exit(struct net *net, const char *name,
1763-
const struct nf_hook_ops *ops)
1782+
void ipt_unregister_table_pre_exit(struct net *net, const char *name)
17641783
{
17651784
struct xt_table *table = xt_find_table(net, NFPROTO_IPV4, name);
17661785

17671786
if (table)
1768-
nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
1787+
nf_unregister_net_hooks(net, table->ops, hweight32(table->valid_hooks));
17691788
}
17701789

17711790
void ipt_unregister_table_exit(struct net *net, const char *name)

‎net/ipv4/netfilter/iptable_filter.c‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static unsigned int
3434
iptable_filter_hook(void *priv, struct sk_buff *skb,
3535
const struct nf_hook_state *state)
3636
{
37-
return ipt_do_table(skb, state, state->net->ipv4.iptable_filter);
37+
return ipt_do_table(skb, state, priv);
3838
}
3939

4040
static struct nf_hook_ops *filter_ops __read_mostly;
@@ -55,8 +55,7 @@ static int __net_init iptable_filter_table_init(struct net *net)
5555
((struct ipt_standard *)repl->entries)[1].target.verdict =
5656
forward ? -NF_ACCEPT - 1 : -NF_DROP - 1;
5757

58-
err = ipt_register_table(net, &packet_filter, repl, filter_ops,
59-
&net->ipv4.iptable_filter);
58+
err = ipt_register_table(net, &packet_filter, repl, filter_ops);
6059
kfree(repl);
6160
return err;
6261
}
@@ -71,13 +70,12 @@ static int __net_init iptable_filter_net_init(struct net *net)
7170

7271
static void __net_exit iptable_filter_net_pre_exit(struct net *net)
7372
{
74-
ipt_unregister_table_pre_exit(net, "filter", filter_ops);
73+
ipt_unregister_table_pre_exit(net, "filter");
7574
}
7675

7776
static void __net_exit iptable_filter_net_exit(struct net *net)
7877
{
7978
ipt_unregister_table_exit(net, "filter");
80-
net->ipv4.iptable_filter = NULL;
8179
}
8280

8381
static struct pernet_operations iptable_filter_net_ops = {

‎net/ipv4/netfilter/iptable_mangle.c‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static const struct xt_table packet_mangler = {
3737
};
3838

3939
static unsigned int
40-
ipt_mangle_out(struct sk_buff *skb, const struct nf_hook_state *state)
40+
ipt_mangle_out(struct sk_buff *skb, const struct nf_hook_state *state, void *priv)
4141
{
4242
unsigned int ret;
4343
const struct iphdr *iph;
@@ -53,7 +53,7 @@ ipt_mangle_out(struct sk_buff *skb, const struct nf_hook_state *state)
5353
daddr = iph->daddr;
5454
tos = iph->tos;
5555

56-
ret = ipt_do_table(skb, state, state->net->ipv4.iptable_mangle);
56+
ret = ipt_do_table(skb, state, priv);
5757
/* Reroute for ANY change. */
5858
if (ret != NF_DROP && ret != NF_STOLEN) {
5959
iph = ip_hdr(skb);
@@ -78,8 +78,8 @@ iptable_mangle_hook(void *priv,
7878
const struct nf_hook_state *state)
7979
{
8080
if (state->hook == NF_INET_LOCAL_OUT)
81-
return ipt_mangle_out(skb, state);
82-
return ipt_do_table(skb, state, state->net->ipv4.iptable_mangle);
81+
return ipt_mangle_out(skb, state, priv);
82+
return ipt_do_table(skb, state, priv);
8383
}
8484

8585
static struct nf_hook_ops *mangle_ops __read_mostly;
@@ -91,21 +91,19 @@ static int __net_init iptable_mangle_table_init(struct net *net)
9191
repl = ipt_alloc_initial_table(&packet_mangler);
9292
if (repl == NULL)
9393
return -ENOMEM;
94-
ret = ipt_register_table(net, &packet_mangler, repl, mangle_ops,
95-
&net->ipv4.iptable_mangle);
94+
ret = ipt_register_table(net, &packet_mangler, repl, mangle_ops);
9695
kfree(repl);
9796
return ret;
9897
}
9998

10099
static void __net_exit iptable_mangle_net_pre_exit(struct net *net)
101100
{
102-
ipt_unregister_table_pre_exit(net, "mangle", mangle_ops);
101+
ipt_unregister_table_pre_exit(net, "mangle");
103102
}
104103

105104
static void __net_exit iptable_mangle_net_exit(struct net *net)
106105
{
107106
ipt_unregister_table_exit(net, "mangle");
108-
net->ipv4.iptable_mangle = NULL;
109107
}
110108

111109
static struct pernet_operations iptable_mangle_net_ops = {

‎net/ipv4/netfilter/iptable_nat.c‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,19 @@ static const struct nf_hook_ops nf_nat_ipv4_ops[] = {
6666
},
6767
};
6868

69-
static int ipt_nat_register_lookups(struct net *net, struct xt_table *table)
69+
static int ipt_nat_register_lookups(struct net *net)
7070
{
71-
struct nf_hook_ops *ops = kmemdup(nf_nat_ipv4_ops, sizeof(nf_nat_ipv4_ops), GFP_KERNEL);
72-
struct iptable_nat_pernet *xt_nat_net = net_generic(net, iptable_nat_net_id);
71+
struct iptable_nat_pernet *xt_nat_net;
72+
struct nf_hook_ops *ops;
73+
struct xt_table *table;
7374
int i, ret;
7475

76+
xt_nat_net = net_generic(net, iptable_nat_net_id);
77+
table = xt_find_table(net, NFPROTO_IPV4, "nat");
78+
if (WARN_ON_ONCE(!table))
79+
return -ENOENT;
80+
81+
ops = kmemdup(nf_nat_ipv4_ops, sizeof(nf_nat_ipv4_ops), GFP_KERNEL);
7582
if (!ops)
7683
return -ENOMEM;
7784

@@ -109,25 +116,21 @@ static void ipt_nat_unregister_lookups(struct net *net)
109116
static int __net_init iptable_nat_table_init(struct net *net)
110117
{
111118
struct ipt_replace *repl;
112-
struct xt_table *table;
113119
int ret;
114120

115121
repl = ipt_alloc_initial_table(&nf_nat_ipv4_table);
116122
if (repl == NULL)
117123
return -ENOMEM;
118-
ret = ipt_register_table(net, &nf_nat_ipv4_table, repl,
119-
NULL, &table);
124+
125+
ret = ipt_register_table(net, &nf_nat_ipv4_table, repl, NULL);
120126
if (ret < 0) {
121127
kfree(repl);
122128
return ret;
123129
}
124130

125-
ret = ipt_nat_register_lookups(net, table);
126-
if (ret < 0) {
131+
ret = ipt_nat_register_lookups(net);
132+
if (ret < 0)
127133
ipt_unregister_table_exit(net, "nat");
128-
} else {
129-
net->ipv4.nat_table = table;
130-
}
131134

132135
kfree(repl);
133136
return ret;
@@ -141,7 +144,6 @@ static void __net_exit iptable_nat_net_pre_exit(struct net *net)
141144
static void __net_exit iptable_nat_net_exit(struct net *net)
142145
{
143146
ipt_unregister_table_exit(net, "nat");
144-
net->ipv4.nat_table = NULL;
145147
}
146148

147149
static struct pernet_operations iptable_nat_net_ops = {

‎net/ipv4/netfilter/iptable_raw.c‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static unsigned int
4141
iptable_raw_hook(void *priv, struct sk_buff *skb,
4242
const struct nf_hook_state *state)
4343
{
44-
return ipt_do_table(skb, state, state->net->ipv4.iptable_raw);
44+
return ipt_do_table(skb, state, priv);
4545
}
4646

4747
static struct nf_hook_ops *rawtable_ops __read_mostly;
@@ -58,21 +58,19 @@ static int __net_init iptable_raw_table_init(struct net *net)
5858
repl = ipt_alloc_initial_table(table);
5959
if (repl == NULL)
6060
return -ENOMEM;
61-
ret = ipt_register_table(net, table, repl, rawtable_ops,
62-
&net->ipv4.iptable_raw);
61+
ret = ipt_register_table(net, table, repl, rawtable_ops);
6362
kfree(repl);
6463
return ret;
6564
}
6665

6766
static void __net_exit iptable_raw_net_pre_exit(struct net *net)
6867
{
69-
ipt_unregister_table_pre_exit(net, "raw", rawtable_ops);
68+
ipt_unregister_table_pre_exit(net, "raw");
7069
}
7170

7271
static void __net_exit iptable_raw_net_exit(struct net *net)
7372
{
7473
ipt_unregister_table_exit(net, "raw");
75-
net->ipv4.iptable_raw = NULL;
7674
}
7775

7876
static struct pernet_operations iptable_raw_net_ops = {

‎net/ipv4/netfilter/iptable_security.c‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static unsigned int
4040
iptable_security_hook(void *priv, struct sk_buff *skb,
4141
const struct nf_hook_state *state)
4242
{
43-
return ipt_do_table(skb, state, state->net->ipv4.iptable_security);
43+
return ipt_do_table(skb, state, priv);
4444
}
4545

4646
static struct nf_hook_ops *sectbl_ops __read_mostly;
@@ -53,21 +53,19 @@ static int __net_init iptable_security_table_init(struct net *net)
5353
repl = ipt_alloc_initial_table(&security_table);
5454
if (repl == NULL)
5555
return -ENOMEM;
56-
ret = ipt_register_table(net, &security_table, repl, sectbl_ops,
57-
&net->ipv4.iptable_security);
56+
ret = ipt_register_table(net, &security_table, repl, sectbl_ops);
5857
kfree(repl);
5958
return ret;
6059
}
6160

6261
static void __net_exit iptable_security_net_pre_exit(struct net *net)
6362
{
64-
ipt_unregister_table_pre_exit(net, "security", sectbl_ops);
63+
ipt_unregister_table_pre_exit(net, "security");
6564
}
6665

6766
static void __net_exit iptable_security_net_exit(struct net *net)
6867
{
6968
ipt_unregister_table_exit(net, "security");
70-
net->ipv4.iptable_security = NULL;
7169
}
7270

7371
static struct pernet_operations iptable_security_net_ops = {

‎net/netfilter/x_tables.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,6 +1498,7 @@ void *xt_unregister_table(struct xt_table *table)
14981498
mutex_unlock(&xt[table->af].mutex);
14991499
audit_log_nfcfg(table->name, table->af, private->number,
15001500
AUDIT_XT_OP_UNREGISTER, GFP_KERNEL);
1501+
kfree(table->ops);
15011502
kfree(table);
15021503

15031504
return private;

0 commit comments

Comments
 (0)