Skip to contentSkip to conversation

netfilter: nf_tables: replace built-in tables by chain types

PATCH In review [email protected] 2 participants 4 messages Nov 4, 2012 – Nov 5, 2012

Subsystems (2)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

PATCH 1/1 netfilter: nf_tables: replace built-in tables by chain types

From: Pablo Neira Ayuso <[email protected]>

This patch replaces built-in tables by chain types. Before this patch, the special tables nat and route were created from the kernel to avoid wrong configurations.

After this patch, there are three chain types [ filter, nat and route ] that you can use in your user-space configurations while creating chains. This provides (almost) full control to the user regarding table and chain configurations.

---
 include/linux/netfilter/nf_tables.h                |    1 +
 include/net/netfilter/nf_tables.h                  |   32 ++-
 net/ipv4/netfilter/Kconfig                         |    8 +-
 net/ipv4/netfilter/Makefile                        |    4 +-
 .../{nf_table_nat_ipv4.c => nft_chain_nat_ipv4.c}  |  120 +++-------
 ...f_table_route_ipv4.c => nft_chain_route_ipv4.c} |   44 ++--
 net/ipv6/netfilter/Kconfig                         |    4 +-
 net/ipv6/netfilter/Makefile                        |    2 +-
 ...f_table_route_ipv6.c => nft_chain_route_ipv6.c} |   46 ++--
 net/netfilter/nf_tables_api.c                      |  245 ++++++++++----------
 10 files changed, 210 insertions(+), 296 deletions(-)
 rename net/ipv4/netfilter/{nf_table_nat_ipv4.c => nft_chain_nat_ipv4.c} (76%)
 rename net/ipv4/netfilter/{nf_table_route_ipv4.c => nft_chain_route_ipv4.c} (53%)
 rename net/ipv6/netfilter/{nf_table_route_ipv6.c => nft_chain_route_ipv6.c} (54%)
include/linux/netfilter/nf_tables.h +1 -0
index 46538fa..b3a6489 100644
@@ -64,6 +64,7 @@ enum nft_chain_attributes {
 	NFTA_CHAIN_POLICY,
 	NFTA_CHAIN_USE,
 	NFTA_CHAIN_NEW_NAME,
+	NFTA_CHAIN_TYPE,
 	__NFTA_CHAIN_MAX
 };
 #define NFTA_CHAIN_MAX		(__NFTA_CHAIN_MAX - 1)
include/net/netfilter/nf_tables.h +21 -11
index ea5df3f..0f721e9 100644
@@ -211,7 +211,6 @@ static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
 
 enum nft_chain_flags {
 	NFT_BASE_CHAIN			= 0x1,
-	NFT_CHAIN_BUILTIN		= 0x2,
 };
 
 /**
@@ -236,6 +235,13 @@ struct nft_chain {
 	char				name[NFT_CHAIN_MAXNAMELEN];
 };
 
+enum nft_chain_type {
+	NFT_CHAIN_T_DEFAULT = 0,
+	NFT_CHAIN_T_ROUTE,
+	NFT_CHAIN_T_NAT,
+	NFT_CHAIN_T_MAX
+};
+
 /**
  *	struct nft_base_chain - nf_tables base chain
  *
@@ -244,6 +250,7 @@ struct nft_chain {
  */
 struct nft_base_chain {
 	struct nf_hook_ops		ops;
+	enum nft_chain_type		type;
 	struct nft_chain		chain;
 };
 
@@ -258,24 +265,18 @@ extern unsigned int nft_do_chain(const struct nf_hook_ops *ops,
 				 const struct net_device *out,
 				 int (*okfn)(struct sk_buff *));
 
-enum nft_table_flags {
-	NFT_TABLE_BUILTIN		= 0x1,
-};
-
 /**
  *	struct nft_table - nf_tables table
  *
  *	@list: used internally
  *	@chains: chains in the table
  *	@sets: sets in the table
- *	@flags: bitmask of enum nft_table_flags
  *	@name: name of the table
  */
 struct nft_table {
 	struct list_head		list;
 	struct list_head		chains;
 	struct list_head		sets;
-	u16				flags;
 	char				name[];
 };
 
@@ -301,8 +302,17 @@ struct nft_af_info {
 extern int nft_register_afinfo(struct nft_af_info *);
 extern void nft_unregister_afinfo(struct nft_af_info *);
 
-extern int nft_register_table(struct nft_table *, int family);
-extern void nft_unregister_table(struct nft_table *, int family);
+struct nf_chain_type {
+	unsigned int		hook_mask;
+	const char		*name;
+	enum nft_chain_type	type;
+	nf_hookfn		*fn[NF_MAX_HOOKS];
+	struct module		*me;
+	int			family;
+};
+
+extern int nft_register_chain_type(struct nf_chain_type *);
+extern void nft_unregister_chain_type(struct nf_chain_type *);
 
 extern int nft_register_expr(struct nft_expr_type *);
 extern void nft_unregister_expr(struct nft_expr_type *);
@@ -310,8 +320,8 @@ extern void nft_unregister_expr(struct nft_expr_type *);
 #define MODULE_ALIAS_NFT_FAMILY(family)	\
 	MODULE_ALIAS("nft-afinfo-" __stringify(family))
 
-#define MODULE_ALIAS_NFT_TABLE(family, name) \
-	MODULE_ALIAS("nft-table-" __stringify(family) "-" name)
+#define MODULE_ALIAS_NFT_CHAIN(family, name) \
+	MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
 
 #define MODULE_ALIAS_NFT_EXPR(name) \
 	MODULE_ALIAS("nft-expr-" name)
net/ipv4/netfilter/Kconfig +4 -4
index d009d24..1aefe95 100644
@@ -57,13 +57,13 @@ config NFT_REJECT_IPV4
 	depends on NF_TABLES_IPV4
 	tristate "nf_tables IPv4 reject support"
 
-config NF_TABLE_ROUTE_IPV4
+config NFT_CHAIN_ROUTE_IPV4
 	depends on NF_TABLES_IPV4
-	tristate "IPv4 nf_tables route table support"
+	tristate "IPv4 nf_tables route chain support"
 
-config NF_TABLE_NAT_IPV4
+config NFT_CHAIN_NAT_IPV4
 	depends on NF_TABLES_IPV4
-	tristate "IPv4 nf_tables nat table support"
+	tristate "IPv4 nf_tables nat chain support"
 
 config IP_NF_IPTABLES
 	tristate "IP tables support (required for filtering/masq/NAT)"
net/ipv4/netfilter/Makefile +2 -2
index ed12c64..8ce715c 100644
@@ -29,8 +29,8 @@ obj-$(CONFIG_NF_NAT_PROTO_GRE) += nf_nat_proto_gre.o
 
 obj-$(CONFIG_NF_TABLES_IPV4) += nf_tables_ipv4.o
 obj-$(CONFIG_NFT_REJECT_IPV4) += nft_reject_ipv4.o
-obj-$(CONFIG_NF_TABLE_ROUTE_IPV4) += nf_table_route_ipv4.o
-obj-$(CONFIG_NF_TABLE_NAT_IPV4) += nf_table_nat_ipv4.o
+obj-$(CONFIG_NFT_CHAIN_ROUTE_IPV4) += nft_chain_route_ipv4.o
+obj-$(CONFIG_NFT_CHAIN_NAT_IPV4) += nft_chain_nat_ipv4.o
 
 # generic IP tables 
 obj-$(CONFIG_IP_NF_IPTABLES) += ip_tables.o
net/ipv4/netfilter/nft_chain_nat_ipv4.c renamed from net/ipv4/netfilter/nf_table_nat_ipv4.c +27 -93

158 lines not on this page · raw message

net/ipv4/netfilter/nft_chain_route_ipv4.c renamed from net/ipv4/netfilter/nf_table_route_ipv4.c +16 -28

64 lines not on this page · raw message

net/ipv6/netfilter/Kconfig +2 -2
index a3cf75a..20ab251 100644
@@ -29,9 +29,9 @@ config NF_TABLES_IPV6
 	depends on NF_TABLES
 	tristate "IPv6 nf_tables support"
 
-config NF_TABLE_ROUTE_IPV6
+config NFT_CHAIN_ROUTE_IPV6
 	depends on NF_TABLES_IPV6
-	tristate "IPv6 nf_tables route table support"
+	tristate "IPv6 nf_tables route chain support"
 
 config IP6_NF_IPTABLES
 	tristate "IP6 tables support (required for filtering)"
net/ipv6/netfilter/Makefile +1 -1
index a84487b..cc19b60 100644
@@ -25,7 +25,7 @@ obj-$(CONFIG_NF_DEFRAG_IPV6) += nf_defrag_ipv6.o
 
 # nf_tables
 obj-$(CONFIG_NF_TABLES_IPV6) += nf_tables_ipv6.o
-obj-$(CONFIG_NF_TABLE_ROUTE_IPV6) += nf_table_route_ipv6.o
+obj-$(CONFIG_NFT_CHAIN_ROUTE_IPV6) += nft_chain_route_ipv6.o
 
 # matches
 obj-$(CONFIG_IP6_NF_MATCH_AH) += ip6t_ah.o
net/ipv6/netfilter/nft_chain_route_ipv6.c renamed from net/ipv6/netfilter/nf_table_route_ipv6.c +17 -29

65 lines not on this page · raw message

net/netfilter/nf_tables_api.c +119 -126

404 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#1 lore raw

Hi Pablo,

Don't know why, but git cannot apply properly the patch at once. Though, nothing seems to prevent such thing. Anyway:

Quoted 13 lines
net/netfilter/nf_tables_api.c +4 -0
@@ -1807,6 +1793,10 @@ static int __init nf_tables_module_init(void)
  	if (err<  0)
  		goto err2;
 
+	nft_register_chain_type(&filter_ipv4);
+	nft_register_chain_type(&filter_ipv6);
+	nft_register_chain_type(&filter_bridge);
+
  	pr_info("nf_tables: (c) 2007-2009 Patrick McHardy<[email protected]>\n");
  	return 0;
 

net/netfilter/nf_tables_api.c err1: · line 1816

Quoted 9 lines
net/netfilter/nf_tables_api.c +3 -0
@@ -1818,6 +1808,9 @@ err1:
 
  static void __exit nf_tables_module_exit(void)
  {
+	nft_unregister_chain_type(&filter_ipv4);
+	nft_unregister_chain_type(&filter_ipv6);
+	nft_unregister_chain_type(&filter_bridge);
  	nfnetlink_subsys_unregister(&nf_tables_subsys);
  	nf_tables_core_module_exit();
  }

filter_bridge declaration is missing.

Tomasz

author maintainer
#2 lore raw
Quoted 19 lines · Tomasz Bursztyka

Hi Pablo,

Don't know why, but git cannot apply properly the patch at once. Though, nothing seems to prevent such thing.
Anyway:

Quoted 13 lines
net/netfilter/nf_tables_api.c +4 -0
@@ -1807,6 +1793,10 @@ static int __init nf_tables_module_init(void)
 	if (err<  0)
 		goto err2;
 
+	nft_register_chain_type(&filter_ipv4);
+	nft_register_chain_type(&filter_ipv6);
+	nft_register_chain_type(&filter_bridge);
+
 	pr_info("nf_tables: (c) 2007-2009 Patrick McHardy<[email protected]>\n");
 	return 0;
 
@@ -1818,6 +1808,9 @@ err1:
 
Quoted 11 lines

net/netfilter/nf_tables_api.c err1: · line 1816

Quoted 9 lines
net/netfilter/nf_tables_api.c +3 -0
 static void __exit nf_tables_module_exit(void)
 {
+	nft_unregister_chain_type(&filter_ipv4);
+	nft_unregister_chain_type(&filter_ipv6);
+	nft_unregister_chain_type(&filter_bridge);
 	nfnetlink_subsys_unregister(&nf_tables_subsys);
 	nf_tables_core_module_exit();
 }

filter_bridge declaration is missing.

It's a leftover, remove it. It's already fixed here. Sorry for the inconvenience.

author maintainer
#3 lore raw

Latest version of the patch including module refcount bumping and resolve this issue.

I accidentally send an old version of the patch.

I'll push this asap to make things more confortable for you.

Quoted 32 lines · Pablo Neira Ayuso
Quoted 19 lines · Tomasz Bursztyka

Hi Pablo,

Don't know why, but git cannot apply properly the patch at once. Though, nothing seems to prevent such thing.
Anyway:

Quoted 13 lines
(quoted diff) +4 -0
@@ -1807,6 +1793,10 @@ static int __init nf_tables_module_init(void)
 	if (err<  0)
 		goto err2;
 
+	nft_register_chain_type(&filter_ipv4);
+	nft_register_chain_type(&filter_ipv6);
+	nft_register_chain_type(&filter_bridge);
+
 	pr_info("nf_tables: (c) 2007-2009 Patrick McHardy<[email protected]>\n");
 	return 0;
 
@@ -1818,6 +1808,9 @@ err1:
 
Quoted 11 lines

Review err1: · line 1816

Quoted 9 lines
(quoted diff) +3 -0
 static void __exit nf_tables_module_exit(void)
 {
+	nft_unregister_chain_type(&filter_ipv4);
+	nft_unregister_chain_type(&filter_ipv6);
+	nft_unregister_chain_type(&filter_bridge);
 	nfnetlink_subsys_unregister(&nf_tables_subsys);
 	nf_tables_core_module_exit();
 }

filter_bridge declaration is missing.

Quoted 3 lines

It's a leftover, remove it. It's already fixed here. Sorry for the inconvenience.
From 95ed9aa1d812ec3670d0f3070135d6e78a7ba9d4 Mon Sep 17 00:00:00 2001

From: Pablo Neira Ayuso <[email protected]>
Date: Sat, 3 Nov 2012 10:21:43 +0100
Subject: [PATCH] netfilter: nf_tables: replace built-in tables by chain types

This patch replaces built-in tables by chain types. Before this patch, the special tables nat and route were created from the kernel to avoid wrong configurations.

After this patch, there are three chain types [ filter, nat and route ] that you can use in your user-space configurations while creating chains. This provides (almost) full control to the user regarding table and chain configurations.

---
 include/linux/netfilter/nf_tables.h       |    1 +
 include/net/netfilter/nf_tables.h         |   32 ++-
 net/ipv4/netfilter/Kconfig                |    8 +-
 net/ipv4/netfilter/Makefile               |    4 +-
 net/ipv4/netfilter/nf_table_nat_ipv4.c    |  419 -----------------------------
 net/ipv4/netfilter/nf_table_route_ipv4.c  |   79 ------
 net/ipv4/netfilter/nft_chain_nat_ipv4.c   |  353 ++++++++++++++++++++++++
 net/ipv4/netfilter/nft_chain_route_ipv4.c |   67 +++++
 net/ipv6/netfilter/Kconfig                |    4 +-
 net/ipv6/netfilter/Makefile               |    2 +-
 net/ipv6/netfilter/nf_table_route_ipv6.c  |   80 ------
 net/ipv6/netfilter/nft_chain_route_ipv6.c |   68 +++++
 net/netfilter/nf_tables_api.c             |  245 +++++++++--------
 13 files changed, 639 insertions(+), 723 deletions(-)
 delete mode 100644 net/ipv4/netfilter/nf_table_nat_ipv4.c
 delete mode 100644 net/ipv4/netfilter/nf_table_route_ipv4.c
 create mode 100644 net/ipv4/netfilter/nft_chain_nat_ipv4.c
 create mode 100644 net/ipv4/netfilter/nft_chain_route_ipv4.c
 delete mode 100644 net/ipv6/netfilter/nf_table_route_ipv6.c
 create mode 100644 net/ipv6/netfilter/nft_chain_route_ipv6.c
include/linux/netfilter/nf_tables.h +1 -0
index 46538fa..b3a6489 100644
@@ -64,6 +64,7 @@ enum nft_chain_attributes {
 	NFTA_CHAIN_POLICY,
 	NFTA_CHAIN_USE,
 	NFTA_CHAIN_NEW_NAME,
+	NFTA_CHAIN_TYPE,
 	__NFTA_CHAIN_MAX
 };
 #define NFTA_CHAIN_MAX		(__NFTA_CHAIN_MAX - 1)
include/net/netfilter/nf_tables.h +21 -11
index ea5df3f..0f721e9 100644
@@ -211,7 +211,6 @@ static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
 
 enum nft_chain_flags {
 	NFT_BASE_CHAIN			= 0x1,
-	NFT_CHAIN_BUILTIN		= 0x2,
 };
 
 /**
@@ -236,6 +235,13 @@ struct nft_chain {
 	char				name[NFT_CHAIN_MAXNAMELEN];
 };
 
+enum nft_chain_type {
+	NFT_CHAIN_T_DEFAULT = 0,
+	NFT_CHAIN_T_ROUTE,
+	NFT_CHAIN_T_NAT,
+	NFT_CHAIN_T_MAX
+};
+
 /**
  *	struct nft_base_chain - nf_tables base chain
  *
@@ -244,6 +250,7 @@ struct nft_chain {
  */
 struct nft_base_chain {
 	struct nf_hook_ops		ops;
+	enum nft_chain_type		type;
 	struct nft_chain		chain;
 };
 
@@ -258,24 +265,18 @@ extern unsigned int nft_do_chain(const struct nf_hook_ops *ops,
 				 const struct net_device *out,
 				 int (*okfn)(struct sk_buff *));
 
-enum nft_table_flags {
-	NFT_TABLE_BUILTIN		= 0x1,
-};
-
 /**
  *	struct nft_table - nf_tables table
  *
  *	@list: used internally
  *	@chains: chains in the table
  *	@sets: sets in the table
- *	@flags: bitmask of enum nft_table_flags
  *	@name: name of the table
  */
 struct nft_table {
 	struct list_head		list;
 	struct list_head		chains;
 	struct list_head		sets;
-	u16				flags;
 	char				name[];
 };
 
@@ -301,8 +302,17 @@ struct nft_af_info {
 extern int nft_register_afinfo(struct nft_af_info *);
 extern void nft_unregister_afinfo(struct nft_af_info *);
 
-extern int nft_register_table(struct nft_table *, int family);
-extern void nft_unregister_table(struct nft_table *, int family);
+struct nf_chain_type {
+	unsigned int		hook_mask;
+	const char		*name;
+	enum nft_chain_type	type;
+	nf_hookfn		*fn[NF_MAX_HOOKS];
+	struct module		*me;
+	int			family;
+};
+
+extern int nft_register_chain_type(struct nf_chain_type *);
+extern void nft_unregister_chain_type(struct nf_chain_type *);
 
 extern int nft_register_expr(struct nft_expr_type *);
 extern void nft_unregister_expr(struct nft_expr_type *);
@@ -310,8 +320,8 @@ extern void nft_unregister_expr(struct nft_expr_type *);
 #define MODULE_ALIAS_NFT_FAMILY(family)	\
 	MODULE_ALIAS("nft-afinfo-" __stringify(family))
 
-#define MODULE_ALIAS_NFT_TABLE(family, name) \
-	MODULE_ALIAS("nft-table-" __stringify(family) "-" name)
+#define MODULE_ALIAS_NFT_CHAIN(family, name) \
+	MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
 
 #define MODULE_ALIAS_NFT_EXPR(name) \
 	MODULE_ALIAS("nft-expr-" name)
net/ipv4/netfilter/Kconfig +4 -4
index d009d24..1aefe95 100644
@@ -57,13 +57,13 @@ config NFT_REJECT_IPV4
 	depends on NF_TABLES_IPV4
 	tristate "nf_tables IPv4 reject support"
 
-config NF_TABLE_ROUTE_IPV4
+config NFT_CHAIN_ROUTE_IPV4
 	depends on NF_TABLES_IPV4
-	tristate "IPv4 nf_tables route table support"
+	tristate "IPv4 nf_tables route chain support"
 
-config NF_TABLE_NAT_IPV4
+config NFT_CHAIN_NAT_IPV4
 	depends on NF_TABLES_IPV4
-	tristate "IPv4 nf_tables nat table support"
+	tristate "IPv4 nf_tables nat chain support"
 
 config IP_NF_IPTABLES
 	tristate "IP tables support (required for filtering/masq/NAT)"
net/ipv4/netfilter/Makefile +2 -2
index ed12c64..8ce715c 100644
@@ -29,8 +29,8 @@ obj-$(CONFIG_NF_NAT_PROTO_GRE) += nf_nat_proto_gre.o
 
 obj-$(CONFIG_NF_TABLES_IPV4) += nf_tables_ipv4.o
 obj-$(CONFIG_NFT_REJECT_IPV4) += nft_reject_ipv4.o
-obj-$(CONFIG_NF_TABLE_ROUTE_IPV4) += nf_table_route_ipv4.o
-obj-$(CONFIG_NF_TABLE_NAT_IPV4) += nf_table_nat_ipv4.o
+obj-$(CONFIG_NFT_CHAIN_ROUTE_IPV4) += nft_chain_route_ipv4.o
+obj-$(CONFIG_NFT_CHAIN_NAT_IPV4) += nft_chain_nat_ipv4.o
 
 # generic IP tables 
 obj-$(CONFIG_IP_NF_IPTABLES) += ip_tables.o
net/ipv4/netfilter/nf_table_nat_ipv4.c deleted +0 -419

419 lines not on this page · raw message

net/ipv4/netfilter/nf_table_route_ipv4.c deleted +0 -79

79 lines not on this page · raw message

net/ipv4/netfilter/nft_chain_nat_ipv4.c new file +353 -0

353 lines not on this page · raw message

net/ipv4/netfilter/nft_chain_route_ipv4.c new file +67 -0

67 lines not on this page · raw message

net/ipv6/netfilter/Kconfig +2 -2
index a3cf75a..20ab251 100644
@@ -29,9 +29,9 @@ config NF_TABLES_IPV6
 	depends on NF_TABLES
 	tristate "IPv6 nf_tables support"
 
-config NF_TABLE_ROUTE_IPV6
+config NFT_CHAIN_ROUTE_IPV6
 	depends on NF_TABLES_IPV6
-	tristate "IPv6 nf_tables route table support"
+	tristate "IPv6 nf_tables route chain support"
 
 config IP6_NF_IPTABLES
 	tristate "IP6 tables support (required for filtering)"
net/ipv6/netfilter/Makefile +1 -1
index a84487b..cc19b60 100644
@@ -25,7 +25,7 @@ obj-$(CONFIG_NF_DEFRAG_IPV6) += nf_defrag_ipv6.o
 
 # nf_tables
 obj-$(CONFIG_NF_TABLES_IPV6) += nf_tables_ipv6.o
-obj-$(CONFIG_NF_TABLE_ROUTE_IPV6) += nf_table_route_ipv6.o
+obj-$(CONFIG_NFT_CHAIN_ROUTE_IPV6) += nft_chain_route_ipv6.o
 
 # matches
 obj-$(CONFIG_IP6_NF_MATCH_AH) += ip6t_ah.o
net/ipv6/netfilter/nf_table_route_ipv6.c deleted +0 -80

80 lines not on this page · raw message

net/ipv6/netfilter/nft_chain_route_ipv6.c new file +68 -0

68 lines not on this page · raw message

net/netfilter/nf_tables_api.c +120 -125

405 lines not on this page · raw message

Signature · 1 line
1.7.10.4