Skip to content

Commit e3eaa99

Browse files
author
Jan Engelhardt
committed
netfilter: xtables: generate initial table on-demand
The static initial tables are pretty large, and after the net namespace has been instantiated, they just hang around for nothing. This commit removes them and creates tables on-demand at runtime when needed. Size shrinks by 7735 bytes (x86_64). Signed-off-by: Jan Engelhardt <jengelh@medozas.de>
1 parent 2b95efe commit e3eaa99

18 files changed

Lines changed: 141 additions & 334 deletions

File tree

‎include/linux/netfilter_arp/arp_tables.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ struct arpt_error {
258258
.target.errorname = "ERROR", \
259259
}
260260

261+
extern void *arpt_alloc_initial_table(const struct xt_table *);
261262
extern struct xt_table *arpt_register_table(struct net *net,
262263
const struct xt_table *table,
263264
const struct arpt_replace *repl);

‎include/linux/netfilter_ipv4/ip_tables.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ struct ipt_error {
282282
.target.errorname = "ERROR", \
283283
}
284284

285+
extern void *ipt_alloc_initial_table(const struct xt_table *);
285286
extern unsigned int ipt_do_table(struct sk_buff *skb,
286287
unsigned int hook,
287288
const struct net_device *in,

‎include/linux/netfilter_ipv6/ip6_tables.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ ip6t_get_target(struct ip6t_entry *e)
297297
#include <linux/init.h>
298298
extern void ip6t_init(void) __init;
299299

300+
extern void *ip6t_alloc_initial_table(const struct xt_table *);
300301
extern struct xt_table *ip6t_register_table(struct net *net,
301302
const struct xt_table *table,
302303
const struct ip6t_replace *repl);

‎net/ipv4/netfilter/arp_tables.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include <linux/netfilter/x_tables.h>
2929
#include <linux/netfilter_arp/arp_tables.h>
30+
#include "../../netfilter/xt_repldata.h"
3031

3132
MODULE_LICENSE("GPL");
3233
MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
@@ -58,6 +59,12 @@ do { \
5859
#define ARP_NF_ASSERT(x)
5960
#endif
6061

62+
void *arpt_alloc_initial_table(const struct xt_table *info)
63+
{
64+
return xt_alloc_initial_table(arpt, ARPT);
65+
}
66+
EXPORT_SYMBOL_GPL(arpt_alloc_initial_table);
67+
6168
static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
6269
const char *hdr_addr, int len)
6370
{

‎net/ipv4/netfilter/arptable_filter.c‎

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
#include <linux/module.h>
9+
#include <linux/netfilter/x_tables.h>
910
#include <linux/netfilter_arp/arp_tables.h>
1011

1112
MODULE_LICENSE("GPL");
@@ -15,36 +16,6 @@ MODULE_DESCRIPTION("arptables filter table");
1516
#define FILTER_VALID_HOOKS ((1 << NF_ARP_IN) | (1 << NF_ARP_OUT) | \
1617
(1 << NF_ARP_FORWARD))
1718

18-
static const struct
19-
{
20-
struct arpt_replace repl;
21-
struct arpt_standard entries[3];
22-
struct arpt_error term;
23-
} initial_table __net_initdata = {
24-
.repl = {
25-
.name = "filter",
26-
.valid_hooks = FILTER_VALID_HOOKS,
27-
.num_entries = 4,
28-
.size = sizeof(struct arpt_standard) * 3 + sizeof(struct arpt_error),
29-
.hook_entry = {
30-
[NF_ARP_IN] = 0,
31-
[NF_ARP_OUT] = sizeof(struct arpt_standard),
32-
[NF_ARP_FORWARD] = 2 * sizeof(struct arpt_standard),
33-
},
34-
.underflow = {
35-
[NF_ARP_IN] = 0,
36-
[NF_ARP_OUT] = sizeof(struct arpt_standard),
37-
[NF_ARP_FORWARD] = 2 * sizeof(struct arpt_standard),
38-
},
39-
},
40-
.entries = {
41-
ARPT_STANDARD_INIT(NF_ACCEPT), /* ARP_IN */
42-
ARPT_STANDARD_INIT(NF_ACCEPT), /* ARP_OUT */
43-
ARPT_STANDARD_INIT(NF_ACCEPT), /* ARP_FORWARD */
44-
},
45-
.term = ARPT_ERROR_INIT,
46-
};
47-
4819
static const struct xt_table packet_filter = {
4920
.name = "filter",
5021
.valid_hooks = FILTER_VALID_HOOKS,
@@ -68,9 +39,14 @@ static struct nf_hook_ops *arpfilter_ops __read_mostly;
6839

6940
static int __net_init arptable_filter_net_init(struct net *net)
7041
{
71-
/* Register table */
42+
struct arpt_replace *repl;
43+
44+
repl = arpt_alloc_initial_table(&packet_filter);
45+
if (repl == NULL)
46+
return -ENOMEM;
7247
net->ipv4.arptable_filter =
73-
arpt_register_table(net, &packet_filter, &initial_table.repl);
48+
arpt_register_table(net, &packet_filter, repl);
49+
kfree(repl);
7450
if (IS_ERR(net->ipv4.arptable_filter))
7551
return PTR_ERR(net->ipv4.arptable_filter);
7652
return 0;

‎net/ipv4/netfilter/ip_tables.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <linux/netfilter/x_tables.h>
2929
#include <linux/netfilter_ipv4/ip_tables.h>
3030
#include <net/netfilter/nf_log.h>
31+
#include "../../netfilter/xt_repldata.h"
3132

3233
MODULE_LICENSE("GPL");
3334
MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
@@ -66,6 +67,12 @@ do { \
6667
#define inline
6768
#endif
6869

70+
void *ipt_alloc_initial_table(const struct xt_table *info)
71+
{
72+
return xt_alloc_initial_table(ipt, IPT);
73+
}
74+
EXPORT_SYMBOL_GPL(ipt_alloc_initial_table);
75+
6976
/*
7077
We keep a set of rules for each CPU, so we can avoid write-locking
7178
them in the softirq when updating the counters and therefore

‎net/ipv4/netfilter/iptable_filter.c‎

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,6 @@ MODULE_DESCRIPTION("iptables filter table");
2323
(1 << NF_INET_FORWARD) | \
2424
(1 << NF_INET_LOCAL_OUT))
2525

26-
static struct
27-
{
28-
struct ipt_replace repl;
29-
struct ipt_standard entries[3];
30-
struct ipt_error term;
31-
} initial_table __net_initdata = {
32-
.repl = {
33-
.name = "filter",
34-
.valid_hooks = FILTER_VALID_HOOKS,
35-
.num_entries = 4,
36-
.size = sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error),
37-
.hook_entry = {
38-
[NF_INET_LOCAL_IN] = 0,
39-
[NF_INET_FORWARD] = sizeof(struct ipt_standard),
40-
[NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2,
41-
},
42-
.underflow = {
43-
[NF_INET_LOCAL_IN] = 0,
44-
[NF_INET_FORWARD] = sizeof(struct ipt_standard),
45-
[NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2,
46-
},
47-
},
48-
.entries = {
49-
IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_IN */
50-
IPT_STANDARD_INIT(NF_ACCEPT), /* FORWARD */
51-
IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_OUT */
52-
},
53-
.term = IPT_ERROR_INIT, /* ERROR */
54-
};
55-
5626
static const struct xt_table packet_filter = {
5727
.name = "filter",
5828
.valid_hooks = FILTER_VALID_HOOKS,
@@ -86,9 +56,18 @@ module_param(forward, bool, 0000);
8656

8757
static int __net_init iptable_filter_net_init(struct net *net)
8858
{
89-
/* Register table */
59+
struct ipt_replace *repl;
60+
61+
repl = ipt_alloc_initial_table(&packet_filter);
62+
if (repl == NULL)
63+
return -ENOMEM;
64+
/* Entry 1 is the FORWARD hook */
65+
((struct ipt_standard *)repl->entries)[1].target.verdict =
66+
-forward - 1;
67+
9068
net->ipv4.iptable_filter =
91-
ipt_register_table(net, &packet_filter, &initial_table.repl);
69+
ipt_register_table(net, &packet_filter, repl);
70+
kfree(repl);
9271
if (IS_ERR(net->ipv4.iptable_filter))
9372
return PTR_ERR(net->ipv4.iptable_filter);
9473
return 0;
@@ -113,9 +92,6 @@ static int __init iptable_filter_init(void)
11392
return -EINVAL;
11493
}
11594

116-
/* Entry 1 is the FORWARD hook */
117-
initial_table.entries[1].target.verdict = -forward - 1;
118-
11995
ret = register_pernet_subsys(&iptable_filter_net_ops);
12096
if (ret < 0)
12197
return ret;

‎net/ipv4/netfilter/iptable_mangle.c‎

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,6 @@ MODULE_DESCRIPTION("iptables mangle table");
2727
(1 << NF_INET_LOCAL_OUT) | \
2828
(1 << NF_INET_POST_ROUTING))
2929

30-
/* Ouch - five different hooks? Maybe this should be a config option..... -- BC */
31-
static const struct
32-
{
33-
struct ipt_replace repl;
34-
struct ipt_standard entries[5];
35-
struct ipt_error term;
36-
} initial_table __net_initdata = {
37-
.repl = {
38-
.name = "mangle",
39-
.valid_hooks = MANGLE_VALID_HOOKS,
40-
.num_entries = 6,
41-
.size = sizeof(struct ipt_standard) * 5 + sizeof(struct ipt_error),
42-
.hook_entry = {
43-
[NF_INET_PRE_ROUTING] = 0,
44-
[NF_INET_LOCAL_IN] = sizeof(struct ipt_standard),
45-
[NF_INET_FORWARD] = sizeof(struct ipt_standard) * 2,
46-
[NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 3,
47-
[NF_INET_POST_ROUTING] = sizeof(struct ipt_standard) * 4,
48-
},
49-
.underflow = {
50-
[NF_INET_PRE_ROUTING] = 0,
51-
[NF_INET_LOCAL_IN] = sizeof(struct ipt_standard),
52-
[NF_INET_FORWARD] = sizeof(struct ipt_standard) * 2,
53-
[NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 3,
54-
[NF_INET_POST_ROUTING] = sizeof(struct ipt_standard) * 4,
55-
},
56-
},
57-
.entries = {
58-
IPT_STANDARD_INIT(NF_ACCEPT), /* PRE_ROUTING */
59-
IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_IN */
60-
IPT_STANDARD_INIT(NF_ACCEPT), /* FORWARD */
61-
IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_OUT */
62-
IPT_STANDARD_INIT(NF_ACCEPT), /* POST_ROUTING */
63-
},
64-
.term = IPT_ERROR_INIT, /* ERROR */
65-
};
66-
6730
static const struct xt_table packet_mangler = {
6831
.name = "mangle",
6932
.valid_hooks = MANGLE_VALID_HOOKS,
@@ -134,9 +97,14 @@ static struct nf_hook_ops *mangle_ops __read_mostly;
13497

13598
static int __net_init iptable_mangle_net_init(struct net *net)
13699
{
137-
/* Register table */
100+
struct ipt_replace *repl;
101+
102+
repl = ipt_alloc_initial_table(&packet_mangler);
103+
if (repl == NULL)
104+
return -ENOMEM;
138105
net->ipv4.iptable_mangle =
139-
ipt_register_table(net, &packet_mangler, &initial_table.repl);
106+
ipt_register_table(net, &packet_mangler, repl);
107+
kfree(repl);
140108
if (IS_ERR(net->ipv4.iptable_mangle))
141109
return PTR_ERR(net->ipv4.iptable_mangle);
142110
return 0;

‎net/ipv4/netfilter/iptable_raw.c‎

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,6 @@
99

1010
#define RAW_VALID_HOOKS ((1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_OUT))
1111

12-
static const struct
13-
{
14-
struct ipt_replace repl;
15-
struct ipt_standard entries[2];
16-
struct ipt_error term;
17-
} initial_table __net_initdata = {
18-
.repl = {
19-
.name = "raw",
20-
.valid_hooks = RAW_VALID_HOOKS,
21-
.num_entries = 3,
22-
.size = sizeof(struct ipt_standard) * 2 + sizeof(struct ipt_error),
23-
.hook_entry = {
24-
[NF_INET_PRE_ROUTING] = 0,
25-
[NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard)
26-
},
27-
.underflow = {
28-
[NF_INET_PRE_ROUTING] = 0,
29-
[NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard)
30-
},
31-
},
32-
.entries = {
33-
IPT_STANDARD_INIT(NF_ACCEPT), /* PRE_ROUTING */
34-
IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_OUT */
35-
},
36-
.term = IPT_ERROR_INIT, /* ERROR */
37-
};
38-
3912
static const struct xt_table packet_raw = {
4013
.name = "raw",
4114
.valid_hooks = RAW_VALID_HOOKS,
@@ -66,9 +39,14 @@ static struct nf_hook_ops *rawtable_ops __read_mostly;
6639

6740
static int __net_init iptable_raw_net_init(struct net *net)
6841
{
69-
/* Register table */
42+
struct ipt_replace *repl;
43+
44+
repl = ipt_alloc_initial_table(&packet_raw);
45+
if (repl == NULL)
46+
return -ENOMEM;
7047
net->ipv4.iptable_raw =
71-
ipt_register_table(net, &packet_raw, &initial_table.repl);
48+
ipt_register_table(net, &packet_raw, repl);
49+
kfree(repl);
7250
if (IS_ERR(net->ipv4.iptable_raw))
7351
return PTR_ERR(net->ipv4.iptable_raw);
7452
return 0;

‎net/ipv4/netfilter/iptable_security.c‎

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,6 @@ MODULE_DESCRIPTION("iptables security table, for MAC rules");
2727
(1 << NF_INET_FORWARD) | \
2828
(1 << NF_INET_LOCAL_OUT)
2929

30-
static const struct
31-
{
32-
struct ipt_replace repl;
33-
struct ipt_standard entries[3];
34-
struct ipt_error term;
35-
} initial_table __net_initdata = {
36-
.repl = {
37-
.name = "security",
38-
.valid_hooks = SECURITY_VALID_HOOKS,
39-
.num_entries = 4,
40-
.size = sizeof(struct ipt_standard) * 3 + sizeof(struct ipt_error),
41-
.hook_entry = {
42-
[NF_INET_LOCAL_IN] = 0,
43-
[NF_INET_FORWARD] = sizeof(struct ipt_standard),
44-
[NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2,
45-
},
46-
.underflow = {
47-
[NF_INET_LOCAL_IN] = 0,
48-
[NF_INET_FORWARD] = sizeof(struct ipt_standard),
49-
[NF_INET_LOCAL_OUT] = sizeof(struct ipt_standard) * 2,
50-
},
51-
},
52-
.entries = {
53-
IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_IN */
54-
IPT_STANDARD_INIT(NF_ACCEPT), /* FORWARD */
55-
IPT_STANDARD_INIT(NF_ACCEPT), /* LOCAL_OUT */
56-
},
57-
.term = IPT_ERROR_INIT, /* ERROR */
58-
};
59-
6030
static const struct xt_table security_table = {
6131
.name = "security",
6232
.valid_hooks = SECURITY_VALID_HOOKS,
@@ -87,9 +57,14 @@ static struct nf_hook_ops *sectbl_ops __read_mostly;
8757

8858
static int __net_init iptable_security_net_init(struct net *net)
8959
{
90-
net->ipv4.iptable_security =
91-
ipt_register_table(net, &security_table, &initial_table.repl);
60+
struct ipt_replace *repl;
9261

62+
repl = ipt_alloc_initial_table(&security_table);
63+
if (repl == NULL)
64+
return -ENOMEM;
65+
net->ipv4.iptable_security =
66+
ipt_register_table(net, &security_table, repl);
67+
kfree(repl);
9368
if (IS_ERR(net->ipv4.iptable_security))
9469
return PTR_ERR(net->ipv4.iptable_security);
9570

0 commit comments

Comments
 (0)