Skip to content

Commit 9370761

Browse files
committed
netfilter: nf_tables: convert built-in tables/chains to chain types
This patch converts built-in tables/chains to chain types that allows you to deploy customized table and chain configurations from userspace. After this patch, you have to specify the chain type when creating a new chain: add chain ip filter output { type filter hook input priority 0; } ^^^^ ------ The existing chain types after this patch are: filter, route and nat. Note that tables are just containers of chains with no specific semantics, which is a significant change with regards to iptables. Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent c29b72e commit 9370761

File tree

12 files changed

+221
-274
lines changed

12 files changed

+221
-274
lines changed

include/net/netfilter/nf_tables.h

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,6 @@ static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
336336

337337
enum nft_chain_flags {
338338
NFT_BASE_CHAIN = 0x1,
339-
NFT_CHAIN_BUILTIN = 0x2,
340339
};
341340

342341
/**
@@ -362,14 +361,23 @@ struct nft_chain {
362361
char name[NFT_CHAIN_MAXNAMELEN];
363362
};
364363

364+
enum nft_chain_type {
365+
NFT_CHAIN_T_DEFAULT = 0,
366+
NFT_CHAIN_T_ROUTE,
367+
NFT_CHAIN_T_NAT,
368+
NFT_CHAIN_T_MAX
369+
};
370+
365371
/**
366372
* struct nft_base_chain - nf_tables base chain
367373
*
368374
* @ops: netfilter hook ops
375+
* @type: chain type
369376
* @chain: the chain
370377
*/
371378
struct nft_base_chain {
372379
struct nf_hook_ops ops;
380+
enum nft_chain_type type;
373381
struct nft_chain chain;
374382
};
375383

@@ -384,10 +392,6 @@ extern unsigned int nft_do_chain(const struct nf_hook_ops *ops,
384392
const struct net_device *out,
385393
int (*okfn)(struct sk_buff *));
386394

387-
enum nft_table_flags {
388-
NFT_TABLE_BUILTIN = 0x1,
389-
};
390-
391395
/**
392396
* struct nft_table - nf_tables table
393397
*
@@ -431,17 +435,26 @@ struct nft_af_info {
431435
extern int nft_register_afinfo(struct nft_af_info *);
432436
extern void nft_unregister_afinfo(struct nft_af_info *);
433437

434-
extern int nft_register_table(struct nft_table *, int family);
435-
extern void nft_unregister_table(struct nft_table *, int family);
438+
struct nf_chain_type {
439+
unsigned int hook_mask;
440+
const char *name;
441+
enum nft_chain_type type;
442+
nf_hookfn *fn[NF_MAX_HOOKS];
443+
struct module *me;
444+
int family;
445+
};
446+
447+
extern int nft_register_chain_type(struct nf_chain_type *);
448+
extern void nft_unregister_chain_type(struct nf_chain_type *);
436449

437450
extern int nft_register_expr(struct nft_expr_type *);
438451
extern void nft_unregister_expr(struct nft_expr_type *);
439452

440453
#define MODULE_ALIAS_NFT_FAMILY(family) \
441454
MODULE_ALIAS("nft-afinfo-" __stringify(family))
442455

443-
#define MODULE_ALIAS_NFT_TABLE(family, name) \
444-
MODULE_ALIAS("nft-table-" __stringify(family) "-" name)
456+
#define MODULE_ALIAS_NFT_CHAIN(family, name) \
457+
MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
445458

446459
#define MODULE_ALIAS_NFT_EXPR(name) \
447460
MODULE_ALIAS("nft-expr-" name)

include/uapi/linux/netfilter/nf_tables.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,15 @@ enum nft_table_attributes {
115115
* @NFTA_CHAIN_HANDLE: numeric handle of the chain (NLA_U64)
116116
* @NFTA_CHAIN_NAME: name of the chain (NLA_STRING)
117117
* @NFTA_CHAIN_HOOK: hook specification for basechains (NLA_NESTED: nft_hook_attributes)
118+
* @NFTA_CHAIN_TYPE: type name of the string (NLA_NUL_STRING)
118119
*/
119120
enum nft_chain_attributes {
120121
NFTA_CHAIN_UNSPEC,
121122
NFTA_CHAIN_TABLE,
122123
NFTA_CHAIN_HANDLE,
123124
NFTA_CHAIN_NAME,
124125
NFTA_CHAIN_HOOK,
126+
NFTA_CHAIN_TYPE,
125127
__NFTA_CHAIN_MAX
126128
};
127129
#define NFTA_CHAIN_MAX (__NFTA_CHAIN_MAX - 1)

net/ipv4/netfilter/Kconfig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ config NFT_REJECT_IPV4
4444
depends on NF_TABLES_IPV4
4545
tristate "nf_tables IPv4 reject support"
4646

47-
config NF_TABLE_ROUTE_IPV4
47+
config NFT_CHAIN_ROUTE_IPV4
4848
depends on NF_TABLES_IPV4
49-
tristate "IPv4 nf_tables route table support"
49+
tristate "IPv4 nf_tables route chain support"
5050

51-
config NF_TABLE_NAT_IPV4
51+
config NFT_CHAIN_NAT_IPV4
5252
depends on NF_TABLES_IPV4
53-
tristate "IPv4 nf_tables nat table support"
53+
tristate "IPv4 nf_tables nat chain support"
5454

5555
config IP_NF_IPTABLES
5656
tristate "IP tables support (required for filtering/masq/NAT)"

net/ipv4/netfilter/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ obj-$(CONFIG_NF_NAT_PROTO_GRE) += nf_nat_proto_gre.o
2929

3030
obj-$(CONFIG_NF_TABLES_IPV4) += nf_tables_ipv4.o
3131
obj-$(CONFIG_NFT_REJECT_IPV4) += nft_reject_ipv4.o
32-
obj-$(CONFIG_NF_TABLE_ROUTE_IPV4) += nf_table_route_ipv4.o
33-
obj-$(CONFIG_NF_TABLE_NAT_IPV4) += nf_table_nat_ipv4.o
32+
obj-$(CONFIG_NFT_CHAIN_ROUTE_IPV4) += nft_chain_route_ipv4.o
33+
obj-$(CONFIG_NFT_CHAIN_NAT_IPV4) += nft_chain_nat_ipv4.o
3434

3535
# generic IP tables
3636
obj-$(CONFIG_IP_NF_IPTABLES) += ip_tables.o

net/ipv4/netfilter/nf_tables_ipv4.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
3+
* Copyright (c) 2012-2013 Pablo Neira Ayuso <pablo@netfilter.org>
34
*
45
* This program is free software; you can redistribute it and/or modify
56
* it under the terms of the GNU General Public License version 2 as
@@ -41,14 +42,34 @@ static struct nft_af_info nft_af_ipv4 __read_mostly = {
4142
},
4243
};
4344

45+
static struct nf_chain_type filter_ipv4 = {
46+
.family = NFPROTO_IPV4,
47+
.name = "filter",
48+
.type = NFT_CHAIN_T_DEFAULT,
49+
.hook_mask = (1 << NF_INET_LOCAL_IN) |
50+
(1 << NF_INET_LOCAL_OUT) |
51+
(1 << NF_INET_FORWARD) |
52+
(1 << NF_INET_PRE_ROUTING) |
53+
(1 << NF_INET_POST_ROUTING),
54+
.fn = {
55+
[NF_INET_LOCAL_IN] = nft_do_chain,
56+
[NF_INET_LOCAL_OUT] = nft_do_chain,
57+
[NF_INET_FORWARD] = nft_do_chain,
58+
[NF_INET_PRE_ROUTING] = nft_do_chain,
59+
[NF_INET_POST_ROUTING] = nft_do_chain,
60+
},
61+
};
62+
4463
static int __init nf_tables_ipv4_init(void)
4564
{
65+
nft_register_chain_type(&filter_ipv4);
4666
return nft_register_afinfo(&nft_af_ipv4);
4767
}
4868

4969
static void __exit nf_tables_ipv4_exit(void)
5070
{
5171
nft_unregister_afinfo(&nft_af_ipv4);
72+
nft_unregister_chain_type(&filter_ipv4);
5273
}
5374

5475
module_init(nf_tables_ipv4_init);

net/ipv4/netfilter/nf_table_nat_ipv4.c renamed to net/ipv4/netfilter/nft_chain_nat_ipv4.c

Lines changed: 27 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
3+
* Copyright (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>
34
*
45
* This program is free software; you can redistribute it and/or modify
56
* it under the terms of the GNU General Public License version 2 as
@@ -167,7 +168,7 @@ static struct nft_expr_type nft_nat_type __read_mostly = {
167168
};
168169

169170
/*
170-
* NAT table
171+
* NAT chains
171172
*/
172173

173174
static unsigned int nf_nat_fn(const struct nf_hook_ops *ops,
@@ -301,115 +302,52 @@ static unsigned int nf_nat_output(const struct nf_hook_ops *ops,
301302
return ret;
302303
}
303304

304-
static struct nft_base_chain nf_chain_nat_prerouting __read_mostly = {
305-
.chain = {
306-
.name = "PREROUTING",
307-
.rules = LIST_HEAD_INIT(nf_chain_nat_prerouting.chain.rules),
308-
.flags = NFT_BASE_CHAIN | NFT_CHAIN_BUILTIN,
309-
},
310-
.ops = {
311-
.hook = nf_nat_prerouting,
312-
.owner = THIS_MODULE,
313-
.pf = NFPROTO_IPV4,
314-
.hooknum = NF_INET_PRE_ROUTING,
315-
.priority = NF_IP_PRI_NAT_DST,
316-
.priv = &nf_chain_nat_prerouting.chain,
317-
},
318-
};
319-
320-
static struct nft_base_chain nf_chain_nat_postrouting __read_mostly = {
321-
.chain = {
322-
.name = "POSTROUTING",
323-
.rules = LIST_HEAD_INIT(nf_chain_nat_postrouting.chain.rules),
324-
.flags = NFT_BASE_CHAIN | NFT_CHAIN_BUILTIN,
325-
},
326-
.ops = {
327-
.hook = nf_nat_postrouting,
328-
.owner = THIS_MODULE,
329-
.pf = NFPROTO_IPV4,
330-
.hooknum = NF_INET_POST_ROUTING,
331-
.priority = NF_IP_PRI_NAT_SRC,
332-
.priv = &nf_chain_nat_postrouting.chain,
333-
},
334-
};
335-
336-
static struct nft_base_chain nf_chain_nat_output __read_mostly = {
337-
.chain = {
338-
.name = "OUTPUT",
339-
.rules = LIST_HEAD_INIT(nf_chain_nat_output.chain.rules),
340-
.flags = NFT_BASE_CHAIN | NFT_CHAIN_BUILTIN,
341-
},
342-
.ops = {
343-
.hook = nf_nat_output,
344-
.owner = THIS_MODULE,
345-
.pf = NFPROTO_IPV4,
346-
.hooknum = NF_INET_LOCAL_OUT,
347-
.priority = NF_IP_PRI_NAT_DST,
348-
.priv = &nf_chain_nat_output.chain,
349-
},
350-
};
351-
352-
static struct nft_base_chain nf_chain_nat_input __read_mostly = {
353-
.chain = {
354-
.name = "INPUT",
355-
.rules = LIST_HEAD_INIT(nf_chain_nat_input.chain.rules),
356-
.flags = NFT_BASE_CHAIN | NFT_CHAIN_BUILTIN,
357-
},
358-
.ops = {
359-
.hook = nf_nat_fn,
360-
.owner = THIS_MODULE,
361-
.pf = NFPROTO_IPV4,
362-
.hooknum = NF_INET_LOCAL_IN,
363-
.priority = NF_IP_PRI_NAT_SRC,
364-
.priv = &nf_chain_nat_input.chain,
305+
struct nf_chain_type nft_chain_nat_ipv4 = {
306+
.family = NFPROTO_IPV4,
307+
.name = "nat",
308+
.type = NFT_CHAIN_T_NAT,
309+
.hook_mask = (1 << NF_INET_PRE_ROUTING) |
310+
(1 << NF_INET_POST_ROUTING) |
311+
(1 << NF_INET_LOCAL_OUT) |
312+
(1 << NF_INET_LOCAL_IN),
313+
.fn = {
314+
[NF_INET_PRE_ROUTING] = nf_nat_prerouting,
315+
[NF_INET_POST_ROUTING] = nf_nat_postrouting,
316+
[NF_INET_LOCAL_OUT] = nf_nat_output,
317+
[NF_INET_LOCAL_IN] = nf_nat_fn,
365318
},
319+
.me = THIS_MODULE,
366320
};
367321

368-
369-
static struct nft_table nf_table_nat_ipv4 __read_mostly = {
370-
.name = "nat",
371-
.chains = LIST_HEAD_INIT(nf_table_nat_ipv4.chains),
372-
};
373-
374-
static int __init nf_table_nat_init(void)
322+
static int __init nft_chain_nat_init(void)
375323
{
376324
int err;
377325

378-
list_add_tail(&nf_chain_nat_prerouting.chain.list,
379-
&nf_table_nat_ipv4.chains);
380-
list_add_tail(&nf_chain_nat_postrouting.chain.list,
381-
&nf_table_nat_ipv4.chains);
382-
list_add_tail(&nf_chain_nat_output.chain.list,
383-
&nf_table_nat_ipv4.chains);
384-
list_add_tail(&nf_chain_nat_input.chain.list,
385-
&nf_table_nat_ipv4.chains);
386-
387-
err = nft_register_table(&nf_table_nat_ipv4, NFPROTO_IPV4);
326+
err = nft_register_chain_type(&nft_chain_nat_ipv4);
388327
if (err < 0)
389-
goto err1;
328+
return err;
390329

391330
err = nft_register_expr(&nft_nat_type);
392331
if (err < 0)
393-
goto err2;
332+
goto err;
394333

395334
return 0;
396335

397-
err2:
398-
nft_unregister_table(&nf_table_nat_ipv4, NFPROTO_IPV4);
399-
err1:
336+
err:
337+
nft_unregister_chain_type(&nft_chain_nat_ipv4);
400338
return err;
401339
}
402340

403-
static void __exit nf_table_nat_exit(void)
341+
static void __exit nft_chain_nat_exit(void)
404342
{
405343
nft_unregister_expr(&nft_nat_type);
406-
nft_unregister_table(&nf_table_nat_ipv4, AF_INET);
344+
nft_unregister_chain_type(&nft_chain_nat_ipv4);
407345
}
408346

409-
module_init(nf_table_nat_init);
410-
module_exit(nf_table_nat_exit);
347+
module_init(nft_chain_nat_init);
348+
module_exit(nft_chain_nat_exit);
411349

412350
MODULE_LICENSE("GPL");
413351
MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
414-
MODULE_ALIAS_NFT_TABLE(AF_INET, "nat");
352+
MODULE_ALIAS_NFT_CHAIN(AF_INET, "nat");
415353
MODULE_ALIAS_NFT_EXPR("nat");

net/ipv4/netfilter/nf_table_route_ipv4.c renamed to net/ipv4/netfilter/nft_chain_route_ipv4.c

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
3+
* Copyright (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>
34
*
45
* This program is free software; you can redistribute it and/or modify
56
* it under the terms of the GNU General Public License version 2 as
@@ -56,42 +57,30 @@ static unsigned int nf_route_table_hook(const struct nf_hook_ops *ops,
5657
return ret;
5758
}
5859

59-
static struct nft_base_chain nf_chain_route_output __read_mostly = {
60-
.chain = {
61-
.name = "OUTPUT",
62-
.rules = LIST_HEAD_INIT(nf_chain_route_output.chain.rules),
63-
.flags = NFT_BASE_CHAIN | NFT_CHAIN_BUILTIN,
60+
static struct nf_chain_type nft_chain_route_ipv4 = {
61+
.family = NFPROTO_IPV4,
62+
.name = "route",
63+
.type = NFT_CHAIN_T_ROUTE,
64+
.hook_mask = (1 << NF_INET_LOCAL_OUT),
65+
.fn = {
66+
[NF_INET_LOCAL_OUT] = nf_route_table_hook,
6467
},
65-
.ops = {
66-
.hook = nf_route_table_hook,
67-
.owner = THIS_MODULE,
68-
.pf = NFPROTO_IPV4,
69-
.hooknum = NF_INET_LOCAL_OUT,
70-
.priority = NF_IP_PRI_MANGLE,
71-
.priv = &nf_chain_route_output.chain,
72-
},
73-
};
74-
75-
static struct nft_table nf_table_route_ipv4 __read_mostly = {
76-
.name = "route",
77-
.chains = LIST_HEAD_INIT(nf_table_route_ipv4.chains),
68+
.me = THIS_MODULE,
7869
};
7970

80-
static int __init nf_table_route_init(void)
71+
static int __init nft_chain_route_init(void)
8172
{
82-
list_add_tail(&nf_chain_route_output.chain.list,
83-
&nf_table_route_ipv4.chains);
84-
return nft_register_table(&nf_table_route_ipv4, NFPROTO_IPV4);
73+
return nft_register_chain_type(&nft_chain_route_ipv4);
8574
}
8675

87-
static void __exit nf_table_route_exit(void)
76+
static void __exit nft_chain_route_exit(void)
8877
{
89-
nft_unregister_table(&nf_table_route_ipv4, NFPROTO_IPV4);
78+
nft_unregister_chain_type(&nft_chain_route_ipv4);
9079
}
9180

92-
module_init(nf_table_route_init);
93-
module_exit(nf_table_route_exit);
81+
module_init(nft_chain_route_init);
82+
module_exit(nft_chain_route_exit);
9483

9584
MODULE_LICENSE("GPL");
9685
MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
97-
MODULE_ALIAS_NFT_TABLE(AF_INET, "route");
86+
MODULE_ALIAS_NFT_CHAIN(AF_INET, "route");

net/ipv6/netfilter/Kconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ config NF_TABLES_IPV6
2929
depends on NF_TABLES
3030
tristate "IPv6 nf_tables support"
3131

32-
config NF_TABLE_ROUTE_IPV6
32+
config NFT_CHAIN_ROUTE_IPV6
3333
depends on NF_TABLES_IPV6
34-
tristate "IPv6 nf_tables route table support"
34+
tristate "IPv6 nf_tables route chain support"
3535

3636
config IP6_NF_IPTABLES
3737
tristate "IP6 tables support (required for filtering)"

0 commit comments

Comments
 (0)