Skip to content

Commit 2b95efe

Browse files
author
Jan Engelhardt
committed
netfilter: xtables: use xt_table for hook instantiation
The respective xt_table structures already have most of the metadata needed for hook setup. Add a 'priority' field to struct xt_table so that xt_hook_link() can be called with a reduced number of arguments. So should we be having more tables in the future, it comes at no static cost (only runtime, as before) - space saved: 6807373->6806555. Signed-off-by: Jan Engelhardt <jengelh@medozas.de>
1 parent 2b21e05 commit 2b95efe

11 files changed

Lines changed: 121 additions & 250 deletions

File tree

‎include/linux/netfilter/x_tables.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ struct xt_table {
361361
struct module *me;
362362

363363
u_int8_t af; /* address/protocol family */
364+
int priority; /* hook order */
364365

365366
/* A unique name... */
366367
const char name[XT_TABLE_MAXNAMELEN];
@@ -522,6 +523,9 @@ static inline unsigned long ifname_compare_aligned(const char *_a,
522523
return ret;
523524
}
524525

526+
extern struct nf_hook_ops *xt_hook_link(const struct xt_table *, nf_hookfn *);
527+
extern void xt_hook_unlink(const struct xt_table *, struct nf_hook_ops *);
528+
525529
#ifdef CONFIG_COMPAT
526530
#include <net/compat.h>
527531

‎net/ipv4/netfilter/arptable_filter.c‎

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ static const struct xt_table packet_filter = {
5050
.valid_hooks = FILTER_VALID_HOOKS,
5151
.me = THIS_MODULE,
5252
.af = NFPROTO_ARP,
53+
.priority = NF_IP_PRI_FILTER,
5354
};
5455

5556
/* The work comes in here from netfilter.c */
@@ -63,29 +64,7 @@ arptable_filter_hook(unsigned int hook, struct sk_buff *skb,
6364
return arpt_do_table(skb, hook, in, out, net->ipv4.arptable_filter);
6465
}
6566

66-
static struct nf_hook_ops arpt_ops[] __read_mostly = {
67-
{
68-
.hook = arptable_filter_hook,
69-
.owner = THIS_MODULE,
70-
.pf = NFPROTO_ARP,
71-
.hooknum = NF_ARP_IN,
72-
.priority = NF_IP_PRI_FILTER,
73-
},
74-
{
75-
.hook = arptable_filter_hook,
76-
.owner = THIS_MODULE,
77-
.pf = NFPROTO_ARP,
78-
.hooknum = NF_ARP_OUT,
79-
.priority = NF_IP_PRI_FILTER,
80-
},
81-
{
82-
.hook = arptable_filter_hook,
83-
.owner = THIS_MODULE,
84-
.pf = NFPROTO_ARP,
85-
.hooknum = NF_ARP_FORWARD,
86-
.priority = NF_IP_PRI_FILTER,
87-
},
88-
};
67+
static struct nf_hook_ops *arpfilter_ops __read_mostly;
8968

9069
static int __net_init arptable_filter_net_init(struct net *net)
9170
{
@@ -115,9 +94,11 @@ static int __init arptable_filter_init(void)
11594
if (ret < 0)
11695
return ret;
11796

118-
ret = nf_register_hooks(arpt_ops, ARRAY_SIZE(arpt_ops));
119-
if (ret < 0)
97+
arpfilter_ops = xt_hook_link(&packet_filter, arptable_filter_hook);
98+
if (IS_ERR(arpfilter_ops)) {
99+
ret = PTR_ERR(arpfilter_ops);
120100
goto cleanup_table;
101+
}
121102
return ret;
122103

123104
cleanup_table:
@@ -127,7 +108,7 @@ static int __init arptable_filter_init(void)
127108

128109
static void __exit arptable_filter_fini(void)
129110
{
130-
nf_unregister_hooks(arpt_ops, ARRAY_SIZE(arpt_ops));
111+
xt_hook_unlink(&packet_filter, arpfilter_ops);
131112
unregister_pernet_subsys(&arptable_filter_net_ops);
132113
}
133114

‎net/ipv4/netfilter/iptable_filter.c‎

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ static const struct xt_table packet_filter = {
5858
.valid_hooks = FILTER_VALID_HOOKS,
5959
.me = THIS_MODULE,
6060
.af = NFPROTO_IPV4,
61+
.priority = NF_IP_PRI_FILTER,
6162
};
6263

6364
static unsigned int
@@ -77,29 +78,7 @@ iptable_filter_hook(unsigned int hook, struct sk_buff *skb,
7778
return ipt_do_table(skb, hook, in, out, net->ipv4.iptable_filter);
7879
}
7980

80-
static struct nf_hook_ops ipt_ops[] __read_mostly = {
81-
{
82-
.hook = iptable_filter_hook,
83-
.owner = THIS_MODULE,
84-
.pf = NFPROTO_IPV4,
85-
.hooknum = NF_INET_LOCAL_IN,
86-
.priority = NF_IP_PRI_FILTER,
87-
},
88-
{
89-
.hook = iptable_filter_hook,
90-
.owner = THIS_MODULE,
91-
.pf = NFPROTO_IPV4,
92-
.hooknum = NF_INET_FORWARD,
93-
.priority = NF_IP_PRI_FILTER,
94-
},
95-
{
96-
.hook = iptable_filter_hook,
97-
.owner = THIS_MODULE,
98-
.pf = NFPROTO_IPV4,
99-
.hooknum = NF_INET_LOCAL_OUT,
100-
.priority = NF_IP_PRI_FILTER,
101-
},
102-
};
81+
static struct nf_hook_ops *filter_ops __read_mostly;
10382

10483
/* Default to forward because I got too much mail already. */
10584
static int forward = NF_ACCEPT;
@@ -142,9 +121,11 @@ static int __init iptable_filter_init(void)
142121
return ret;
143122

144123
/* Register hooks */
145-
ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
146-
if (ret < 0)
124+
filter_ops = xt_hook_link(&packet_filter, iptable_filter_hook);
125+
if (IS_ERR(filter_ops)) {
126+
ret = PTR_ERR(filter_ops);
147127
goto cleanup_table;
128+
}
148129

149130
return ret;
150131

@@ -155,7 +136,7 @@ static int __init iptable_filter_init(void)
155136

156137
static void __exit iptable_filter_fini(void)
157138
{
158-
nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
139+
xt_hook_unlink(&packet_filter, filter_ops);
159140
unregister_pernet_subsys(&iptable_filter_net_ops);
160141
}
161142

‎net/ipv4/netfilter/iptable_mangle.c‎

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ static const struct xt_table packet_mangler = {
6969
.valid_hooks = MANGLE_VALID_HOOKS,
7070
.me = THIS_MODULE,
7171
.af = NFPROTO_IPV4,
72+
.priority = NF_IP_PRI_MANGLE,
7273
};
7374

7475
static unsigned int
@@ -129,43 +130,7 @@ iptable_mangle_hook(unsigned int hook,
129130
dev_net(in)->ipv4.iptable_mangle);
130131
}
131132

132-
static struct nf_hook_ops ipt_ops[] __read_mostly = {
133-
{
134-
.hook = iptable_mangle_hook,
135-
.owner = THIS_MODULE,
136-
.pf = NFPROTO_IPV4,
137-
.hooknum = NF_INET_PRE_ROUTING,
138-
.priority = NF_IP_PRI_MANGLE,
139-
},
140-
{
141-
.hook = iptable_mangle_hook,
142-
.owner = THIS_MODULE,
143-
.pf = NFPROTO_IPV4,
144-
.hooknum = NF_INET_LOCAL_IN,
145-
.priority = NF_IP_PRI_MANGLE,
146-
},
147-
{
148-
.hook = iptable_mangle_hook,
149-
.owner = THIS_MODULE,
150-
.pf = NFPROTO_IPV4,
151-
.hooknum = NF_INET_FORWARD,
152-
.priority = NF_IP_PRI_MANGLE,
153-
},
154-
{
155-
.hook = iptable_mangle_hook,
156-
.owner = THIS_MODULE,
157-
.pf = NFPROTO_IPV4,
158-
.hooknum = NF_INET_LOCAL_OUT,
159-
.priority = NF_IP_PRI_MANGLE,
160-
},
161-
{
162-
.hook = iptable_mangle_hook,
163-
.owner = THIS_MODULE,
164-
.pf = NFPROTO_IPV4,
165-
.hooknum = NF_INET_POST_ROUTING,
166-
.priority = NF_IP_PRI_MANGLE,
167-
},
168-
};
133+
static struct nf_hook_ops *mangle_ops __read_mostly;
169134

170135
static int __net_init iptable_mangle_net_init(struct net *net)
171136
{
@@ -196,9 +161,11 @@ static int __init iptable_mangle_init(void)
196161
return ret;
197162

198163
/* Register hooks */
199-
ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
200-
if (ret < 0)
164+
mangle_ops = xt_hook_link(&packet_mangler, iptable_mangle_hook);
165+
if (IS_ERR(mangle_ops)) {
166+
ret = PTR_ERR(mangle_ops);
201167
goto cleanup_table;
168+
}
202169

203170
return ret;
204171

@@ -209,7 +176,7 @@ static int __init iptable_mangle_init(void)
209176

210177
static void __exit iptable_mangle_fini(void)
211178
{
212-
nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
179+
xt_hook_unlink(&packet_mangler, mangle_ops);
213180
unregister_pernet_subsys(&iptable_mangle_net_ops);
214181
}
215182

‎net/ipv4/netfilter/iptable_raw.c‎

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ static const struct xt_table packet_raw = {
4141
.valid_hooks = RAW_VALID_HOOKS,
4242
.me = THIS_MODULE,
4343
.af = NFPROTO_IPV4,
44+
.priority = NF_IP_PRI_RAW,
4445
};
4546

4647
/* The work comes in here from netfilter.c. */
@@ -61,23 +62,7 @@ iptable_raw_hook(unsigned int hook, struct sk_buff *skb,
6162
return ipt_do_table(skb, hook, in, out, net->ipv4.iptable_raw);
6263
}
6364

64-
/* 'raw' is the very first table. */
65-
static struct nf_hook_ops ipt_ops[] __read_mostly = {
66-
{
67-
.hook = iptable_raw_hook,
68-
.pf = NFPROTO_IPV4,
69-
.hooknum = NF_INET_PRE_ROUTING,
70-
.priority = NF_IP_PRI_RAW,
71-
.owner = THIS_MODULE,
72-
},
73-
{
74-
.hook = iptable_raw_hook,
75-
.pf = NFPROTO_IPV4,
76-
.hooknum = NF_INET_LOCAL_OUT,
77-
.priority = NF_IP_PRI_RAW,
78-
.owner = THIS_MODULE,
79-
},
80-
};
65+
static struct nf_hook_ops *rawtable_ops __read_mostly;
8166

8267
static int __net_init iptable_raw_net_init(struct net *net)
8368
{
@@ -108,9 +93,11 @@ static int __init iptable_raw_init(void)
10893
return ret;
10994

11095
/* Register hooks */
111-
ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
112-
if (ret < 0)
96+
rawtable_ops = xt_hook_link(&packet_raw, iptable_raw_hook);
97+
if (IS_ERR(rawtable_ops)) {
98+
ret = PTR_ERR(rawtable_ops);
11399
goto cleanup_table;
100+
}
114101

115102
return ret;
116103

@@ -121,7 +108,7 @@ static int __init iptable_raw_init(void)
121108

122109
static void __exit iptable_raw_fini(void)
123110
{
124-
nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
111+
xt_hook_unlink(&packet_raw, rawtable_ops);
125112
unregister_pernet_subsys(&iptable_raw_net_ops);
126113
}
127114

‎net/ipv4/netfilter/iptable_security.c‎

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ static const struct xt_table security_table = {
6262
.valid_hooks = SECURITY_VALID_HOOKS,
6363
.me = THIS_MODULE,
6464
.af = NFPROTO_IPV4,
65+
.priority = NF_IP_PRI_SECURITY,
6566
};
6667

6768
static unsigned int
@@ -82,29 +83,7 @@ iptable_security_hook(unsigned int hook, struct sk_buff *skb,
8283
return ipt_do_table(skb, hook, in, out, net->ipv4.iptable_security);
8384
}
8485

85-
static struct nf_hook_ops ipt_ops[] __read_mostly = {
86-
{
87-
.hook = iptable_security_hook,
88-
.owner = THIS_MODULE,
89-
.pf = NFPROTO_IPV4,
90-
.hooknum = NF_INET_LOCAL_IN,
91-
.priority = NF_IP_PRI_SECURITY,
92-
},
93-
{
94-
.hook = iptable_security_hook,
95-
.owner = THIS_MODULE,
96-
.pf = NFPROTO_IPV4,
97-
.hooknum = NF_INET_FORWARD,
98-
.priority = NF_IP_PRI_SECURITY,
99-
},
100-
{
101-
.hook = iptable_security_hook,
102-
.owner = THIS_MODULE,
103-
.pf = NFPROTO_IPV4,
104-
.hooknum = NF_INET_LOCAL_OUT,
105-
.priority = NF_IP_PRI_SECURITY,
106-
},
107-
};
86+
static struct nf_hook_ops *sectbl_ops __read_mostly;
10887

10988
static int __net_init iptable_security_net_init(struct net *net)
11089
{
@@ -135,9 +114,11 @@ static int __init iptable_security_init(void)
135114
if (ret < 0)
136115
return ret;
137116

138-
ret = nf_register_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
139-
if (ret < 0)
117+
sectbl_ops = xt_hook_link(&security_table, iptable_security_hook);
118+
if (IS_ERR(sectbl_ops)) {
119+
ret = PTR_ERR(sectbl_ops);
140120
goto cleanup_table;
121+
}
141122

142123
return ret;
143124

@@ -148,7 +129,7 @@ static int __init iptable_security_init(void)
148129

149130
static void __exit iptable_security_fini(void)
150131
{
151-
nf_unregister_hooks(ipt_ops, ARRAY_SIZE(ipt_ops));
132+
xt_hook_unlink(&security_table, sectbl_ops);
152133
unregister_pernet_subsys(&iptable_security_net_ops);
153134
}
154135

‎net/ipv6/netfilter/ip6table_filter.c‎

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ static const struct xt_table packet_filter = {
5656
.valid_hooks = FILTER_VALID_HOOKS,
5757
.me = THIS_MODULE,
5858
.af = NFPROTO_IPV6,
59+
.priority = NF_IP6_PRI_FILTER,
5960
};
6061

6162
/* The work comes in here from netfilter.c. */
@@ -69,29 +70,7 @@ ip6table_filter_hook(unsigned int hook, struct sk_buff *skb,
6970
return ip6t_do_table(skb, hook, in, out, net->ipv6.ip6table_filter);
7071
}
7172

72-
static struct nf_hook_ops ip6t_ops[] __read_mostly = {
73-
{
74-
.hook = ip6table_filter_hook,
75-
.owner = THIS_MODULE,
76-
.pf = NFPROTO_IPV6,
77-
.hooknum = NF_INET_LOCAL_IN,
78-
.priority = NF_IP6_PRI_FILTER,
79-
},
80-
{
81-
.hook = ip6table_filter_hook,
82-
.owner = THIS_MODULE,
83-
.pf = NFPROTO_IPV6,
84-
.hooknum = NF_INET_FORWARD,
85-
.priority = NF_IP6_PRI_FILTER,
86-
},
87-
{
88-
.hook = ip6table_filter_hook,
89-
.owner = THIS_MODULE,
90-
.pf = NFPROTO_IPV6,
91-
.hooknum = NF_INET_LOCAL_OUT,
92-
.priority = NF_IP6_PRI_FILTER,
93-
},
94-
};
73+
static struct nf_hook_ops *filter_ops __read_mostly;
9574

9675
/* Default to forward because I got too much mail already. */
9776
static int forward = NF_ACCEPT;
@@ -134,9 +113,11 @@ static int __init ip6table_filter_init(void)
134113
return ret;
135114

136115
/* Register hooks */
137-
ret = nf_register_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops));
138-
if (ret < 0)
116+
filter_ops = xt_hook_link(&packet_filter, ip6table_filter_hook);
117+
if (IS_ERR(filter_ops)) {
118+
ret = PTR_ERR(filter_ops);
139119
goto cleanup_table;
120+
}
140121

141122
return ret;
142123

@@ -147,7 +128,7 @@ static int __init ip6table_filter_init(void)
147128

148129
static void __exit ip6table_filter_fini(void)
149130
{
150-
nf_unregister_hooks(ip6t_ops, ARRAY_SIZE(ip6t_ops));
131+
xt_hook_unlink(&packet_filter, filter_ops);
151132
unregister_pernet_subsys(&ip6table_filter_net_ops);
152133
}
153134

0 commit comments

Comments
 (0)