Skip to content

Commit 7e9c6ee

Browse files
Jan Engelhardtkaber
authored andcommitted
netfilter: Introduce NFPROTO_* constants
The netfilter subsystem only supports a handful of protocols (much less than PF_*) and even non-PF protocols like ARP and pseudo-protocols like PF_BRIDGE. By creating NFPROTO_*, we can earn a few memory savings on arrays that previously were always PF_MAX-sized and keep the pseudo-protocols to ourselves. Signed-off-by: Jan Engelhardt <jengelh@medozas.de> Signed-off-by: Patrick McHardy <kaber@trash.net>
1 parent 079aa88 commit 7e9c6ee

5 files changed

Lines changed: 37 additions & 25 deletions

File tree

‎include/linux/netfilter.h‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ enum nf_inet_hooks {
5252
NF_INET_NUMHOOKS
5353
};
5454

55+
enum {
56+
NFPROTO_UNSPEC = 0,
57+
NFPROTO_IPV4 = 2,
58+
NFPROTO_ARP = 3,
59+
NFPROTO_BRIDGE = 7,
60+
NFPROTO_IPV6 = 10,
61+
NFPROTO_DECNET = 12,
62+
NFPROTO_NUMPROTO,
63+
};
64+
5565
union nf_inet_addr {
5666
__u32 all[4];
5767
__be32 ip;
@@ -138,7 +148,7 @@ extern struct ctl_path nf_net_netfilter_sysctl_path[];
138148
extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path[];
139149
#endif /* CONFIG_SYSCTL */
140150

141-
extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
151+
extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
142152

143153
int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
144154
struct net_device *indev, struct net_device *outdev,
@@ -247,7 +257,7 @@ struct nf_afinfo {
247257
int route_key_size;
248258
};
249259

250-
extern const struct nf_afinfo *nf_afinfo[NPROTO];
260+
extern const struct nf_afinfo *nf_afinfo[NFPROTO_NUMPROTO];
251261
static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
252262
{
253263
return rcu_dereference(nf_afinfo[family]);

‎net/netfilter/core.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
static DEFINE_MUTEX(afinfo_mutex);
2828

29-
const struct nf_afinfo *nf_afinfo[NPROTO] __read_mostly;
29+
const struct nf_afinfo *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
3030
EXPORT_SYMBOL(nf_afinfo);
3131

3232
int nf_register_afinfo(const struct nf_afinfo *afinfo)
@@ -51,7 +51,7 @@ void nf_unregister_afinfo(const struct nf_afinfo *afinfo)
5151
}
5252
EXPORT_SYMBOL_GPL(nf_unregister_afinfo);
5353

54-
struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS] __read_mostly;
54+
struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS] __read_mostly;
5555
EXPORT_SYMBOL(nf_hooks);
5656
static DEFINE_MUTEX(nf_hook_mutex);
5757

@@ -264,7 +264,7 @@ EXPORT_SYMBOL(proc_net_netfilter);
264264
void __init netfilter_init(void)
265265
{
266266
int i, h;
267-
for (i = 0; i < NPROTO; i++) {
267+
for (i = 0; i < ARRAY_SIZE(nf_hooks); i++) {
268268
for (h = 0; h < NF_MAX_HOOKS; h++)
269269
INIT_LIST_HEAD(&nf_hooks[i][h]);
270270
}

‎net/netfilter/nf_log.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#define NF_LOG_PREFIXLEN 128
1717

18-
static const struct nf_logger *nf_loggers[NPROTO] __read_mostly;
18+
static const struct nf_logger *nf_loggers[NFPROTO_NUMPROTO] __read_mostly;
1919
static DEFINE_MUTEX(nf_log_mutex);
2020

2121
/* return EBUSY if somebody else is registered, EEXIST if the same logger
@@ -24,7 +24,7 @@ int nf_log_register(u_int8_t pf, const struct nf_logger *logger)
2424
{
2525
int ret;
2626

27-
if (pf >= NPROTO)
27+
if (pf >= ARRAY_SIZE(nf_loggers))
2828
return -EINVAL;
2929

3030
/* Any setup of logging members must be done before
@@ -47,7 +47,7 @@ EXPORT_SYMBOL(nf_log_register);
4747

4848
void nf_log_unregister_pf(u_int8_t pf)
4949
{
50-
if (pf >= NPROTO)
50+
if (pf >= ARRAY_SIZE(nf_loggers))
5151
return;
5252
mutex_lock(&nf_log_mutex);
5353
rcu_assign_pointer(nf_loggers[pf], NULL);
@@ -63,7 +63,7 @@ void nf_log_unregister(const struct nf_logger *logger)
6363
int i;
6464

6565
mutex_lock(&nf_log_mutex);
66-
for (i = 0; i < NPROTO; i++) {
66+
for (i = 0; i < ARRAY_SIZE(nf_loggers); i++) {
6767
if (nf_loggers[i] == logger)
6868
rcu_assign_pointer(nf_loggers[i], NULL);
6969
}
@@ -103,7 +103,7 @@ static void *seq_start(struct seq_file *seq, loff_t *pos)
103103
{
104104
rcu_read_lock();
105105

106-
if (*pos >= NPROTO)
106+
if (*pos >= ARRAY_SIZE(nf_loggers))
107107
return NULL;
108108

109109
return pos;
@@ -113,7 +113,7 @@ static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
113113
{
114114
(*pos)++;
115115

116-
if (*pos >= NPROTO)
116+
if (*pos >= ARRAY_SIZE(nf_loggers))
117117
return NULL;
118118

119119
return pos;

‎net/netfilter/nf_queue.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* long term mutex. The handler must provide an an outfn() to accept packets
1717
* for queueing and must reinject all packets it receives, no matter what.
1818
*/
19-
static const struct nf_queue_handler *queue_handler[NPROTO];
19+
static const struct nf_queue_handler *queue_handler[NFPROTO_NUMPROTO] __read_mostly;
2020

2121
static DEFINE_MUTEX(queue_handler_mutex);
2222

@@ -26,7 +26,7 @@ int nf_register_queue_handler(u_int8_t pf, const struct nf_queue_handler *qh)
2626
{
2727
int ret;
2828

29-
if (pf >= NPROTO)
29+
if (pf >= ARRAY_SIZE(queue_handler))
3030
return -EINVAL;
3131

3232
mutex_lock(&queue_handler_mutex);
@@ -47,7 +47,7 @@ EXPORT_SYMBOL(nf_register_queue_handler);
4747
/* The caller must flush their queue before this */
4848
int nf_unregister_queue_handler(u_int8_t pf, const struct nf_queue_handler *qh)
4949
{
50-
if (pf >= NPROTO)
50+
if (pf >= ARRAY_SIZE(queue_handler))
5151
return -EINVAL;
5252

5353
mutex_lock(&queue_handler_mutex);
@@ -70,7 +70,7 @@ void nf_unregister_queue_handlers(const struct nf_queue_handler *qh)
7070
u_int8_t pf;
7171

7272
mutex_lock(&queue_handler_mutex);
73-
for (pf = 0; pf < NPROTO; pf++) {
73+
for (pf = 0; pf < ARRAY_SIZE(queue_handler); pf++) {
7474
if (queue_handler[pf] == qh)
7575
rcu_assign_pointer(queue_handler[pf], NULL);
7676
}
@@ -285,7 +285,7 @@ EXPORT_SYMBOL(nf_reinject);
285285
#ifdef CONFIG_PROC_FS
286286
static void *seq_start(struct seq_file *seq, loff_t *pos)
287287
{
288-
if (*pos >= NPROTO)
288+
if (*pos >= ARRAY_SIZE(queue_handler))
289289
return NULL;
290290

291291
return pos;
@@ -295,7 +295,7 @@ static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
295295
{
296296
(*pos)++;
297297

298-
if (*pos >= NPROTO)
298+
if (*pos >= ARRAY_SIZE(queue_handler))
299299
return NULL;
300300

301301
return pos;

‎net/netfilter/x_tables.c‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ static struct xt_af *xt;
5858
#define duprintf(format, args...)
5959
#endif
6060

61-
static const char *const xt_prefix[NPROTO] = {
62-
[AF_INET] = "ip",
63-
[AF_INET6] = "ip6",
64-
[NF_ARP] = "arp",
61+
static const char *const xt_prefix[NFPROTO_NUMPROTO] = {
62+
[NFPROTO_UNSPEC] = "x",
63+
[NFPROTO_IPV4] = "ip",
64+
[NFPROTO_ARP] = "arp",
65+
[NFPROTO_BRIDGE] = "eb",
66+
[NFPROTO_IPV6] = "ip6",
6567
};
6668

6769
/* Registration hooks for targets. */
@@ -932,7 +934,7 @@ int xt_proto_init(struct net *net, u_int8_t af)
932934
struct proc_dir_entry *proc;
933935
#endif
934936

935-
if (af >= NPROTO)
937+
if (af >= ARRAY_SIZE(xt_prefix))
936938
return -EINVAL;
937939

938940

@@ -1001,7 +1003,7 @@ static int __net_init xt_net_init(struct net *net)
10011003
{
10021004
int i;
10031005

1004-
for (i = 0; i < NPROTO; i++)
1006+
for (i = 0; i < NFPROTO_NUMPROTO; i++)
10051007
INIT_LIST_HEAD(&net->xt.tables[i]);
10061008
return 0;
10071009
}
@@ -1014,11 +1016,11 @@ static int __init xt_init(void)
10141016
{
10151017
int i, rv;
10161018

1017-
xt = kmalloc(sizeof(struct xt_af) * NPROTO, GFP_KERNEL);
1019+
xt = kmalloc(sizeof(struct xt_af) * NFPROTO_NUMPROTO, GFP_KERNEL);
10181020
if (!xt)
10191021
return -ENOMEM;
10201022

1021-
for (i = 0; i < NPROTO; i++) {
1023+
for (i = 0; i < NFPROTO_NUMPROTO; i++) {
10221024
mutex_init(&xt[i].mutex);
10231025
#ifdef CONFIG_COMPAT
10241026
mutex_init(&xt[i].compat_mutex);

0 commit comments

Comments
 (0)