Skip to contentSkip to conversation

src: get it sync with current include/linux/netfilter/nf_tables.h

PATCH In review Pablo Neira Ayuso 2 participants 7 messages Jun 21, 2013 – Jun 28, 2013 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/5 src: get it sync with current include/linux/netfilter/nf_tables.h

This requires removing the NFT_CT_L3PROTOCOL which is not there anymore and remove references to that definition in other parts of the code.

---
 include/linux/netfilter/nf_tables.h |   55 ++++++++++++++++++++++++++++++-----
 src/ct.c                            |    3 --
 src/parser.y                        |    2 --
 src/scanner.l                       |    1 -
 4 files changed, 47 insertions(+), 14 deletions(-)
include/linux/netfilter/nf_tables.h +47 -8
index 1c23073..8300089 100644
@@ -1,6 +1,8 @@
 #ifndef _LINUX_NF_TABLES_H
 #define _LINUX_NF_TABLES_H
 
+#define NFT_CHAIN_MAXNAMELEN 32
+
 enum nft_registers {
 	NFT_REG_VERDICT,
 	NFT_REG_1,
@@ -46,6 +48,8 @@ enum nf_tables_msg_types {
 	NFT_MSG_NEWSETELEM,
 	NFT_MSG_GETSETELEM,
 	NFT_MSG_DELSETELEM,
+	NFT_MSG_COMMIT,
+	NFT_MSG_ABORT,
 	NFT_MSG_MAX,
 };
 
@@ -76,6 +80,15 @@ enum nft_hook_attributes {
 #define NFTA_HOOK_MAX		(__NFTA_HOOK_MAX - 1)
 
 /**
+ * enum nft_table_flags - nf_tables table flags
+ *
+ * @NFT_TABLE_F_DORMANT: this table is not active
+ */
+enum nft_table_flags {
+	NFT_TABLE_F_DORMANT	= 0x1,
+};
+
+/**
  * enum nft_table_attributes - nf_tables table netlink attributes
  *
  * @NFTA_TABLE_NAME: name of the table (NLA_STRING)
@@ -83,6 +96,7 @@ enum nft_hook_attributes {
 enum nft_table_attributes {
 	NFTA_TABLE_UNSPEC,
 	NFTA_TABLE_NAME,
+	NFTA_TABLE_FLAGS,
 	__NFTA_TABLE_MAX
 };
 #define NFTA_TABLE_MAX		(__NFTA_TABLE_MAX - 1)
@@ -97,12 +111,22 @@ enum nft_table_attributes {
 enum nft_chain_attributes {
 	NFTA_CHAIN_UNSPEC,
 	NFTA_CHAIN_TABLE,
+	NFTA_CHAIN_HANDLE,
 	NFTA_CHAIN_NAME,
 	NFTA_CHAIN_HOOK,
+	NFTA_CHAIN_POLICY,
+	NFTA_CHAIN_USE,
+	NFTA_CHAIN_TYPE,
+	NFTA_CHAIN_COUNTERS,
 	__NFTA_CHAIN_MAX
 };
 #define NFTA_CHAIN_MAX		(__NFTA_CHAIN_MAX - 1)
 
+enum {
+	NFT_RULE_F_COMMIT	= (1 << 0),
+	NFT_RULE_F_MASK		= NFT_RULE_F_COMMIT,
+};
+
 /**
  * enum nft_rule_attributes - nf_tables rule netlink attributes
  *
@@ -117,10 +141,25 @@ enum nft_rule_attributes {
 	NFTA_RULE_CHAIN,
 	NFTA_RULE_HANDLE,
 	NFTA_RULE_EXPRESSIONS,
+	NFTA_RULE_FLAGS,
+	NFTA_RULE_COMPAT,
 	__NFTA_RULE_MAX
 };
 #define NFTA_RULE_MAX		(__NFTA_RULE_MAX - 1)
 
+enum nft_rule_compat_flags {
+	NFT_RULE_COMPAT_F_INV	= (1 << 1),
+	NFT_RULE_COMPAT_F_MASK	= NFT_RULE_COMPAT_F_INV,
+};
+
+enum nft_rule_compat_attributes {
+	NFTA_RULE_COMPAT_UNSPEC,
+	NFTA_RULE_COMPAT_PROTO,
+	NFTA_RULE_COMPAT_FLAGS,
+	__NFTA_RULE_COMPAT_MAX
+};
+#define NFTA_RULE_COMPAT_MAX	(__NFTA_RULE_COMPAT_MAX - 1)
+
 /**
  * enum nft_set_flags - nf_tables set flags
  *
@@ -310,6 +349,7 @@ enum nft_bitwise_attributes {
 };
 #define NFTA_BITWISE_MAX	(__NFTA_BITWISE_MAX - 1)
 
+
 enum nft_byteorder_ops {
 	NFT_BYTEORDER_NTOH,
 	NFT_BYTEORDER_HTON,
@@ -462,10 +502,9 @@ enum nft_meta_attributes {
  * @NFT_CT_SECMARK: conntrack secmark value
  * @NFT_CT_EXPIRATION: relative conntrack expiration time in ms
  * @NFT_CT_HELPER: connection tracking helper assigned to conntrack
- * @NFT_CT_L3PROTOCOL: conntrack layer 3 protocol
+ * @NFT_CT_PROTOCOL: conntrack layer 3 protocol
  * @NFT_CT_SRC: conntrack layer 3 protocol source (IPv4/IPv6 address)
  * @NFT_CT_DST: conntrack layer 3 protocol destination (IPv4/IPv6 address)
- * @NFT_CT_PROTOCOL: conntrack layer 4 protocol
  * @NFT_CT_PROTO_SRC: conntrack layer 4 protocol source
  * @NFT_CT_PROTO_DST: conntrack layer 4 protocol destination
  */
@@ -477,10 +516,9 @@ enum nft_ct_keys {
 	NFT_CT_SECMARK,
 	NFT_CT_EXPIRATION,
 	NFT_CT_HELPER,
-	NFT_CT_L3PROTOCOL,
+	NFT_CT_PROTOCOL,
 	NFT_CT_SRC,
 	NFT_CT_DST,
-	NFT_CT_PROTOCOL,
 	NFT_CT_PROTO_SRC,
 	NFT_CT_PROTO_DST,
 };
@@ -595,10 +633,11 @@ enum nft_nat_types {
 enum nft_nat_attributes {
 	NFTA_NAT_UNSPEC,
 	NFTA_NAT_TYPE,
-	NFTA_NAT_ADDR_MIN,
-	NFTA_NAT_ADDR_MAX,
-	NFTA_NAT_PROTO_MIN,
-	NFTA_NAT_PROTO_MAX,
+	NFTA_NAT_FAMILY,
+	NFTA_NAT_REG_ADDR_MIN,
+	NFTA_NAT_REG_ADDR_MAX,
+	NFTA_NAT_REG_PROTO_MIN,
+	NFTA_NAT_REG_PROTO_MAX,
 	__NFTA_NAT_MAX
 };
 #define NFTA_NAT_MAX		(__NFTA_NAT_MAX - 1)
src/ct.c +0 -3
index 1baefed..ebca9b7 100644
@@ -111,9 +111,6 @@ static const struct ct_template ct_templates[] = {
 					      4 * BITS_PER_BYTE),
 	[NFT_CT_HELPER]		= CT_TEMPLATE("helper",	    &string_type,
 					      BYTEORDER_INVALID, 0),
-	[NFT_CT_L3PROTOCOL]	= CT_TEMPLATE("l3proto",    &invalid_type,
-					      BYTEORDER_INVALID,
-					      BITS_PER_BYTE),
 	[NFT_CT_SRC]		= CT_TEMPLATE("saddr",	    &invalid_type,
 					      BYTEORDER_BIG_ENDIAN, 0),
 	[NFT_CT_DST]		= CT_TEMPLATE("daddr",	    &invalid_type,
src/parser.y +0 -2
index 2923b59..25a362f 100644
@@ -295,7 +295,6 @@ static void location_update(struct location *loc, struct location *rhs, int n)
 %token STATUS			"status"
 %token EXPIRATION		"expiration"
 %token HELPER			"helper"
-%token L3PROTOCOL		"l3proto"
 %token PROTO_SRC		"proto-src"
 %token PROTO_DST		"proto-dst"
 
@@ -1309,7 +1308,6 @@ ct_key			:	STATE		{ $$ = NFT_CT_STATE; }
 			|	SECMARK		{ $$ = NFT_CT_SECMARK; }
 			|	EXPIRATION	{ $$ = NFT_CT_EXPIRATION; }
 			|	HELPER		{ $$ = NFT_CT_HELPER; }
-			|	L3PROTOCOL	{ $$ = NFT_CT_L3PROTOCOL; }
 			|	SADDR		{ $$ = NFT_CT_SRC; }
 			|	DADDR		{ $$ = NFT_CT_DST; }
 			|	PROTOCOL	{ $$ = NFT_CT_PROTOCOL; }
src/scanner.l +0 -1
index fe7b86c..c2cf621 100644
@@ -382,7 +382,6 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 "status"		{ return STATUS; }
 "expiration"		{ return EXPIRATION; }
 "helper"		{ return HELPER; }
-"l3proto"		{ return L3PROTOCOL; }
 "proto-src"		{ return PROTO_SRC; }
 "proto-dst"		{ return PROTO_DST; }
 
Signature · 1 line
1.7.10.4
author maintainer
#1 lore raw

PATCH 2/5 rule: family field in struct handle is unsigned

---
 include/rule.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/rule.h +1 -1
index eda6b2c..e0debe3 100644
@@ -15,7 +15,7 @@
  * @handle:	rule handle (rules only)
  */
 struct handle {
-	int			family;
+	uint32_t		family;
 	const char		*table;
 	const char		*chain;
 	const char		*set;
Signature · 1 line
1.7.10.4
author maintainer
#2 lore raw

PATCH 3/5 meta: use if_nametoindex and if_indextoname

In reply to Pablo Neira Ayuso

Instead of having a cache of ifindex based on libnl. Those functions basically use rtnetlink as well to perform the translation.

---
 src/meta.c |   48 ++++--------------------------------------------
 1 file changed, 4 insertions(+), 44 deletions(-)
src/meta.c +4 -44
index 54d4d64..c5719b9 100644
@@ -14,6 +14,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdint.h>
+#include <net/if.h>
 #include <net/if_arp.h>
 #include <pwd.h>
 #include <grp.h>
@@ -96,43 +97,13 @@ static const struct datatype tchandle_type = {
 	.parse		= tchandle_type_parse,
 };
 
-static struct nl_cache *link_cache;
-
-static int link_cache_init(void)
-{
-	struct nl_sock *rt_sock;
-	int err;
-
-	rt_sock = nl_socket_alloc();
-	if (rt_sock == NULL)
-		memory_allocation_error();
-
-	err = nl_connect(rt_sock, NETLINK_ROUTE);
-	if (err < 0)
-		goto err;
-	err = rtnl_link_alloc_cache(rt_sock, &link_cache);
-	if (err < 0)
-		goto err;
-	nl_cache_mngt_provide(link_cache);
-	nl_socket_free(rt_sock);
-	return 0;
-
-err:
-	nl_socket_free(rt_sock);
-	return err;
-}
-
 static void ifindex_type_print(const struct expr *expr)
 {
 	char name[IFNAMSIZ];
 	int ifindex;
 
-	if (link_cache == NULL)
-		link_cache_init();
-
 	ifindex = mpz_get_uint32(expr->value);
-	if (link_cache != NULL &&
-	    rtnl_link_i2name(link_cache, ifindex, name, sizeof(name)))
+	if (if_indextoname(ifindex, name))
 		printf("%s", name);
 	else
 		printf("%d", ifindex);
@@ -141,15 +112,9 @@ static void ifindex_type_print(const struct expr *expr)
 static struct error_record *ifindex_type_parse(const struct expr *sym,
 					       struct expr **res)
 {
-	int ifindex, err;
-
-	if (link_cache == NULL &&
-	    (err = link_cache_init()) < 0)
-		return error(&sym->location,
-			     "Could not initialize link cache: %s",
-			     nl_geterror(err));
+	int ifindex;
 
-	ifindex = rtnl_link_name2i(link_cache, sym->identifier);
+	ifindex = if_nametoindex(sym->identifier);
 	if (ifindex == 0)
 		return error(&sym->location, "Interface does not exist");
 
@@ -159,11 +124,6 @@ static struct error_record *ifindex_type_parse(const struct expr *sym,
 	return NULL;
 }
 
-static void __exit ifindex_table_free(void)
-{
-	nl_cache_free(link_cache);
-}
-
 static const struct datatype ifindex_type = {
 	.type		= TYPE_IFINDEX,
 	.name		= "ifindex",
Signature · 1 line
1.7.10.4
author maintainer
#3 lore raw

PATCH 4/5 meta: replace rtnl_tc_handle2str and rtnl_tc_str2handle

In reply to Pablo Neira Ayuso

Provide replacements for rtnl_tc_handle2str and rtnl_tc_str2handle, it removes the dependency with libnl-route.

---
 configure.ac |    3 ---
 src/meta.c   |   53 +++++++++++++++++++++++++++++++++++++++++++----------
 2 files changed, 43 insertions(+), 13 deletions(-)
configure.ac +0 -3
index d8a850c..316d043 100644
@@ -55,9 +55,6 @@ AC_CHECK_LIB([nl], [nl_socket_alloc], ,
 AC_CHECK_LIB([nl-nf], [nfnl_nft_rule_alloc], ,
 	     AC_MSG_ERROR([No suitable version of libnl-nf found]))
 
-AC_CHECK_LIB([nl-route], [rtnl_link_alloc], ,
-	     AC_MSG_ERROR([No suitable version of libnl-route found]))
-
 AC_CHECK_LIB([gmp], [__gmpz_init], ,
 	     AC_MSG_ERROR([No suitable version of libgmp found]))
 
src/meta.c +43 -10

79 lines not on this page · raw message

Signature · 1 line
1.7.10.4
author maintainer
#4 lore raw

PATCH 5/5 src: use libnftables

In reply to Pablo Neira Ayuso

This patch migrates nft to use the libnftables library, that is used by the iptables over nftables compat utility as well. Most of the conversion was pretty straight forward. Some small significant changes happened in the handling of set element and immediate data abstraction that libnl provides. libnftables is a bit more granular since it splits the struct nfnl_nft_data into three attributes: verdict, chain and plain data (used in maps).

I have added a new file src/mnl.c that contains the low level netlink communication that now resides in nftables source tree instead of the library. This should help to implement the batching support using libmnl in follow up patches.

I also spent some significant amount of time running my tests to make sure that we don't increase the number of bugs that we already have (I plan to provide a list of those that I have detected and diagnosed, so anyone else can help us to fix them).

As a side effect, this change should also prepare the field for the support JSON and XML support anytime soon.

---
 configure.ac              |    8 +-
 include/mnl.h             |   43 +++
 include/netlink.h         |   67 ++--
 include/nftables.h        |    2 +-
 src/Makefile.in           |    1 +
 src/erec.c                |    2 +-
 src/mnl.c                 |  478 +++++++++++++++++++++++++
 src/netlink.c             |  846 ++++++++++++++++++++++-----------------------
 src/netlink_delinearize.c |  190 ++++++----
 src/netlink_linearize.c   |  337 ++++++++++--------
 10 files changed, 1303 insertions(+), 671 deletions(-)
 create mode 100644 include/mnl.h
 create mode 100644 src/mnl.c
configure.ac +4 -4
index 316d043..811d7e2 100644
@@ -49,11 +49,11 @@ then
 fi
 
 # Checks for libraries.
-AC_CHECK_LIB([nl], [nl_socket_alloc], ,
-	     AC_MSG_ERROR([No suitable version of libnl found]))
+AC_CHECK_LIB([mnl], [mnl_socket_open], ,
+	     AC_MSG_ERROR([No suitable version of libmnl found]))
 
-AC_CHECK_LIB([nl-nf], [nfnl_nft_rule_alloc], ,
-	     AC_MSG_ERROR([No suitable version of libnl-nf found]))
+AC_CHECK_LIB([nftables], [nft_rule_alloc], ,
+	     AC_MSG_ERROR([No suitable version of libnftables found]))
 
 AC_CHECK_LIB([gmp], [__gmpz_init], ,
 	     AC_MSG_ERROR([No suitable version of libgmp found]))
include/mnl.h new file +43 -0

43 lines not on this page · raw message

include/netlink.h +43 -24

105 lines not on this page · raw message

include/nftables.h +1 -1
index ff91d93..ec21d37 100644
@@ -50,7 +50,7 @@ struct location {
 			unsigned int		last_column;
 		};
 		struct {
-			struct nl_object	*nl_obj;
+			void			*nle;
 		};
 	};
 };
src/Makefile.in +1 -0
index 6bfcd64..658e9b3 100644
@@ -21,6 +21,7 @@ nft-obj			+= rbtree.o
 nft-obj			+= gmputil.o
 nft-obj			+= utils.o
 nft-obj			+= erec.o
+nft-obj			+= mnl.o
 
 nft-obj			+= parser.o
 nft-extra-clean-files	+= parser.c parser.h
src/erec.c +1 -1
index f2b0ce6..7451d94 100644
@@ -107,7 +107,7 @@ void erec_print(FILE *f, const struct error_record *erec)
 		fprintf(f, "%s\n", erec->msg);
 		for (l = 0; l < (int)erec->num_locations; l++) {
 			loc = &erec->locations[l];
-			netlink_dump_object(loc->nl_obj);
+			netlink_dump_expr(loc->nle);
 		}
 		fprintf(f, "\n");
 	} else {
src/mnl.c new file +478 -0

478 lines not on this page · raw message

src/netlink.c +419 -427

1,339 lines not on this page · raw message

src/netlink_delinearize.c +116 -74

450 lines not on this page · raw message

src/netlink_linearize.c +197 -140

547 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
1.7.10.4

Part of the diff is not shown here. View the raw message.

#5 lore raw
In reply to Pablo Neira Ayuso

Hi Pablo,
i've tried to apply this patch, but it failed:

Hunk #1 succeeded at 1 with fuzz 2.
Hunk #2 FAILED at 48.
Hunk #3 succeeded at 78 with fuzz 1.
Hunk #4 FAILED at 94.
Hunk #5 FAILED at 108.
Hunk #6 FAILED at 128.
Hunk #7 succeeded at 321 with fuzz 2.
Hunk #9 FAILED at 488.
Hunk #10 FAILED at 606.
6 out of 10 hunks FAILED -- saving rejects to file include/linux/netfilter/nf_tables.h.rej
patching file src/ct.c
Hunk #1 FAILED at 111.
1 out of 1 hunk FAILED -- saving rejects to file src/ct.c.rej patching file src/parser.y
Hunk #1 FAILED at 295.
Hunk #2 FAILED at 1309.
2 out of 2 hunks FAILED -- saving rejects to file src/parser.y.rej patching file src/scanner.l
Hunk #1 FAILED at 382.
1 out of 1 hunk FAILED -- saving rejects to file src/scanner.l.rej

I have to apply other patches before this?

Thanks,
Regards.

author maintainer
#6 lore raw

Hi Giuseppe,

Quoted 2 lines · Giuseppe Longo

Hi Pablo,
i've tried to apply this patch, but it failed:

Just pushed my pending patchset to master including improvements with regards to the original patchset.

Regards.