Skip to contentSkip to conversation

complete typeof support

PATCH New Pablo Neira Ayuso 1 participant 12 messages Dec 17, 2019 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

cover complete typeof support

Hi Florian,

This completes your patchset adding typeof support for the remaining primary expression.

Feel free to apply and push this out on top of your patchset.

P.S: testcases/maps/typeof_maps_0 fails:

/dev/stdin:14:28-30: Error: datatype mismatch, map expects string, mapping expression has type IPv4 address
ct mark set ip daddr map @m1
~~~~~~~~ ^^^

There is a typo in the map definition:

/dev/stdin:14:28-30: Error: datatype mismatch, map expects string, mapping expression has type IPv4 address
ct mark set ip daddr map @m1
~~~~~~~~ ^^^

This should be

ct mark set osf name map @m1

dump file also needs an adjustment, you can fix this before pushing this out.

Thanks!

P.S: I might follow up at some point to expose this userdata attributes through libnftnl, later. Meanwhile, I have just pushed out the libnftnl nesting dependencies.

Pablo Neira Ayuso (11):
meta: add parse and build userdata interface
exthdr: add exthdr_desc_id enum and use it
exthdr: add parse and build userdata interface
socket: add parse and build userdata interface
osf: add parse and build userdata interface
ct: add parse and build userdata interface
numgen: add parse and build userdata interface
hash: add parse and build userdata interface
rt: add parse and build userdata interface
fib: add parse and build userdata interface
xfrm: add parse and build userdata interface

 include/exthdr.h |  15 ++++++++
 src/ct.c         |  56 ++++++++++++++++++++++++++++++
 src/expression.c |  13 +++++--
 src/exthdr.c     | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/fib.c        |  60 ++++++++++++++++++++++++++++++--
 src/hash.c       |  72 +++++++++++++++++++++++++++++++++++++++
 src/meta.c       |  51 ++++++++++++++++++++++++++++
 src/numgen.c     |  62 +++++++++++++++++++++++++++++++++
 src/osf.c        |  13 +++++++
 src/rt.c         |  51 ++++++++++++++++++++++++++++
 src/socket.c     |  51 ++++++++++++++++++++++++++++
 src/xfrm.c       |  61 +++++++++++++++++++++++++++++++++
 12 files changed, 603 insertions(+), 4 deletions(-)
Signature · 1 line
2.11.0
author maintainer
#1 lore raw

PATCH 1/11 meta: add parse and build userdata interface

Add support for meta userdata area.

---
 src/expression.c |  4 ++--
 src/meta.c       | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+), 2 deletions(-)
src/expression.c +2 -2
index a7bbde7eec1a..a79c6f55a548 100644
@@ -1226,8 +1226,8 @@ const struct expr_ops *expr_ops(const struct expr *e)
 const struct expr_ops *expr_ops_by_type(enum expr_types etype)
 {
 	switch (etype) {
-	case EXPR_PAYLOAD:
-		return &payload_expr_ops;
+	case EXPR_PAYLOAD: return &payload_expr_ops;
+	case EXPR_META: return &meta_expr_ops;
 	default:
 		break;
 	}
src/meta.c +51 -0

63 lines not on this page · raw message

Signature · 1 line
2.11.0
author maintainer
#2 lore raw

PATCH 2/11 exthdr: add exthdr_desc_id enum and use it

In reply to Pablo Neira Ayuso

This allows to identify the exthdr protocol from the userdata area.

---
 include/exthdr.h | 15 +++++++++++++++
 src/exthdr.c     | 28 ++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+)
include/exthdr.h +15 -0
index 3959a65c7713..c9a3c211b8c4 100644
@@ -5,6 +5,20 @@
 #include <tcpopt.h>
 #include <ipopt.h>
 
+enum exthdr_desc_id {
+	EXTHDR_DESC_UNKNOWN	= 0,
+	EXTHDR_DESC_HBH,
+	EXTHDR_DESC_RT,
+	EXTHDR_DESC_RT0,
+	EXTHDR_DESC_RT2,
+	EXTHDR_DESC_SRH,
+	EXTHDR_DESC_FRAG,
+	EXTHDR_DESC_DST,
+	EXTHDR_DESC_MH,
+	__EXTHDR_DESC_MAX
+};
+#define EXTHDR_DESC_MAX	(__EXTHDR_DESC_MAX - 1)
+
 /**
  * struct exthdr_desc - extension header description
  *
@@ -14,6 +28,7 @@
  */
 struct exthdr_desc {
 	const char			*name;
+	enum exthdr_desc_id		id;
 	uint8_t				type;
 	int				proto_key;
 	struct proto_hdr_template	templates[10];
src/exthdr.c +28 -0
index e1ec6f3dd52b..925b52329003 100644
@@ -23,6 +23,26 @@
 #include <expression.h>
 #include <statement.h>
 
+static const struct exthdr_desc *exthdr_definitions[PROTO_DESC_MAX + 1] = {
+	[EXTHDR_DESC_HBH]	= &exthdr_hbh,
+	[EXTHDR_DESC_RT]	= &exthdr_rt,
+	[EXTHDR_DESC_RT0]	= &exthdr_rt0,
+	[EXTHDR_DESC_RT2]	= &exthdr_rt2,
+	[EXTHDR_DESC_SRH]	= &exthdr_rt4,
+	[EXTHDR_DESC_FRAG]	= &exthdr_frag,
+	[EXTHDR_DESC_DST]	= &exthdr_dst,
+	[EXTHDR_DESC_MH]	= &exthdr_mh,
+};
+
+static const struct exthdr_desc *exthdr_find_desc(enum exthdr_desc_id desc_id)
+{
+	if (desc_id >= EXTHDR_DESC_UNKNOWN &&
+	    desc_id <= EXTHDR_DESC_MAX)
+		return exthdr_definitions[desc_id];
+
+	return NULL;
+}
+
 static void exthdr_expr_print(const struct expr *expr, struct output_ctx *octx)
 {
 	if (expr->exthdr.op == NFT_EXTHDR_OP_TCPOPT) {
@@ -281,6 +301,7 @@ bool exthdr_find_template(struct expr *expr, const struct expr *mask, unsigned i
 
 const struct exthdr_desc exthdr_hbh = {
 	.name		= "hbh",
+	.id		= EXTHDR_DESC_HBH,
 	.type		= IPPROTO_HOPOPTS,
 	.templates	= {
 		[HBHHDR_NEXTHDR]	= HBH_FIELD("nexthdr", ip6h_nxt, &inet_protocol_type),
@@ -294,6 +315,7 @@ const struct exthdr_desc exthdr_hbh = {
 
 const struct exthdr_desc exthdr_rt2 = {
 	.name           = "rt2",
+	.id		= EXTHDR_DESC_RT2,
 	.type           = IPPROTO_ROUTING,
 	.proto_key	= 2,
 	.templates	= {
@@ -307,6 +329,7 @@ const struct exthdr_desc exthdr_rt2 = {
 
 const struct exthdr_desc exthdr_rt0 = {
 	.name           = "rt0",
+	.id		= EXTHDR_DESC_RT0,
 	.type           = IPPROTO_ROUTING,
 	.proto_key      = 0,
 	.templates	= {
@@ -322,6 +345,7 @@ const struct exthdr_desc exthdr_rt0 = {
 
 const struct exthdr_desc exthdr_rt4 = {
 	.name		= "srh",
+	.id		= EXTHDR_DESC_SRH,
 	.type		= IPPROTO_ROUTING,
 	.proto_key	= 4,
 	.templates      = {
@@ -340,6 +364,7 @@ const struct exthdr_desc exthdr_rt4 = {
 
 const struct exthdr_desc exthdr_rt = {
 	.name		= "rt",
+	.id		= EXTHDR_DESC_RT,
 	.type		= IPPROTO_ROUTING,
 	.proto_key      = -1,
 #if 0
@@ -366,6 +391,7 @@ const struct exthdr_desc exthdr_rt = {
 
 const struct exthdr_desc exthdr_frag = {
 	.name		= "frag",
+	.id		= EXTHDR_DESC_FRAG,
 	.type		= IPPROTO_FRAGMENT,
 	.templates	= {
 		[FRAGHDR_NEXTHDR]	= FRAG_FIELD("nexthdr", ip6f_nxt, &inet_protocol_type),
@@ -392,6 +418,7 @@ const struct exthdr_desc exthdr_frag = {
 
 const struct exthdr_desc exthdr_dst = {
 	.name		= "dst",
+	.id		= EXTHDR_DESC_DST,
 	.type		= IPPROTO_DSTOPTS,
 	.templates	= {
 		[DSTHDR_NEXTHDR]	= DST_FIELD("nexthdr", ip6d_nxt, &inet_protocol_type),
@@ -438,6 +465,7 @@ const struct datatype mh_type_type = {
 
 const struct exthdr_desc exthdr_mh = {
 	.name		= "mh",
+	.id		= EXTHDR_DESC_MH,
 	.type		= IPPROTO_MH,
 	.templates	= {
 		[MHHDR_NEXTHDR]		= MH_FIELD("nexthdr", ip6mh_proto, &inet_protocol_type),
Signature · 1 line
2.11.0
author maintainer
#3 lore raw

PATCH 3/11 exthdr: add parse and build userdata interface

In reply to Pablo Neira Ayuso

Add support for meta userdata area.

---
 src/expression.c |  1 +
 src/exthdr.c     | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+)
src/expression.c +1 -0
index a79c6f55a548..847c88ee82c5 100644
@@ -1227,6 +1227,7 @@ const struct expr_ops *expr_ops_by_type(enum expr_types etype)
 {
 	switch (etype) {
 	case EXPR_PAYLOAD: return &payload_expr_ops;
+	case EXPR_EXTHDR: return &exthdr_expr_ops;
 	case EXPR_META: return &meta_expr_ops;
 	default:
 		break;
src/exthdr.c +74 -0

86 lines not on this page · raw message

Signature · 1 line
2.11.0
author maintainer
#4 lore raw

PATCH 4/11 socket: add parse and build userdata interface

In reply to Pablo Neira Ayuso

Add support for meta userdata area.

---
 src/expression.c |  1 +
 src/socket.c     | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)
src/expression.c +1 -0
index 847c88ee82c5..191bc2be104b 100644
@@ -1229,6 +1229,7 @@ const struct expr_ops *expr_ops_by_type(enum expr_types etype)
 	case EXPR_PAYLOAD: return &payload_expr_ops;
 	case EXPR_EXTHDR: return &exthdr_expr_ops;
 	case EXPR_META: return &meta_expr_ops;
+	case EXPR_SOCKET: return &socket_expr_ops;
 	default:
 		break;
 	}
src/socket.c +51 -0

63 lines not on this page · raw message

Signature · 1 line
2.11.0
author maintainer
#5 lore raw

PATCH 5/11 osf: add parse and build userdata interface

In reply to Pablo Neira Ayuso

Add support for meta userdata area.

---
 src/expression.c |  1 +
 src/osf.c        | 13 +++++++++++++
 2 files changed, 14 insertions(+)
src/expression.c +1 -0
index 191bc2be104b..7d198222c90b 100644
@@ -1230,6 +1230,7 @@ const struct expr_ops *expr_ops_by_type(enum expr_types etype)
 	case EXPR_EXTHDR: return &exthdr_expr_ops;
 	case EXPR_META: return &meta_expr_ops;
 	case EXPR_SOCKET: return &socket_expr_ops;
+	case EXPR_OSF: return &osf_expr_ops;
 	default:
 		break;
 	}
src/osf.c +13 -0
index f0c22393cd85..cb58315d714d 100644
@@ -37,6 +37,17 @@ static bool osf_expr_cmp(const struct expr *e1, const struct expr *e2)
 	       (e1->osf.flags == e2->osf.flags);
 }
 
+static int osf_expr_build_udata(struct nftnl_udata_buf *udbuf,
+				 const struct expr *expr)
+{
+	return 0;
+}
+
+static struct expr *osf_expr_parse_udata(const struct nftnl_udata *attr)
+{
+	return osf_expr_alloc(&internal_location, 0, 0);
+}
+
 const struct expr_ops osf_expr_ops = {
 	.type		= EXPR_OSF,
 	.name		= "osf",
@@ -44,6 +55,8 @@ const struct expr_ops osf_expr_ops = {
 	.clone		= osf_expr_clone,
 	.cmp		= osf_expr_cmp,
 	.json		= osf_expr_json,
+	.parse_udata	= osf_expr_parse_udata,
+	.build_udata	= osf_expr_build_udata,
 };
 
 struct expr *osf_expr_alloc(const struct location *loc, const uint8_t ttl,
Signature · 1 line
2.11.0
author maintainer
#6 lore raw

PATCH 6/11 ct: add parse and build userdata interface

In reply to Pablo Neira Ayuso

Add support for meta userdata area.

---
 src/ct.c         | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/expression.c |  1 +
 2 files changed, 57 insertions(+)
src/ct.c +56 -0
index 9e6a8351ffb2..db1dabd319e9 100644
@@ -367,6 +367,60 @@ static void ct_expr_pctx_update(struct proto_ctx *ctx, const struct expr *expr)
 	proto_ctx_update(ctx, left->ct.base + 1, &expr->location, desc);
 }
 
+#define NFTNL_UDATA_CT_KEY 0
+#define NFTNL_UDATA_CT_DIR 1
+#define NFTNL_UDATA_CT_MAX 2
+
+static int ct_expr_build_udata(struct nftnl_udata_buf *udbuf,
+			       const struct expr *expr)
+{
+	nftnl_udata_put_u32(udbuf, NFTNL_UDATA_CT_KEY, expr->ct.key);
+	nftnl_udata_put_u32(udbuf, NFTNL_UDATA_CT_DIR, expr->ct.direction);
+
+	return 0;
+}
+
+static int ct_parse_udata(const struct nftnl_udata *attr, void *data)
+{
+	const struct nftnl_udata **ud = data;
+	uint8_t type = nftnl_udata_type(attr);
+	uint8_t len = nftnl_udata_len(attr);
+
+	switch (type) {
+	case NFTNL_UDATA_CT_KEY:
+	case NFTNL_UDATA_CT_DIR:
+		if (len != sizeof(uint32_t))
+			return -1;
+		break;
+	default:
+		return 0;
+	}
+
+	ud[type] = attr;
+	return 0;
+}
+
+static struct expr *ct_expr_parse_udata(const struct nftnl_udata *attr)
+{
+	const struct nftnl_udata *ud[NFTNL_UDATA_CT_MAX + 1] = {};
+	uint32_t key, dir;
+	int err;
+
+	err = nftnl_udata_parse(nftnl_udata_get(attr), nftnl_udata_len(attr),
+				ct_parse_udata, ud);
+	if (err < 0)
+		return NULL;
+
+	if (!ud[NFTNL_UDATA_CT_KEY] ||
+	    !ud[NFTNL_UDATA_CT_DIR])
+		return NULL;
+
+	key = nftnl_udata_get_u32(ud[NFTNL_UDATA_CT_KEY]);
+	dir = nftnl_udata_get_u32(ud[NFTNL_UDATA_CT_DIR]);
+
+	return ct_expr_alloc(&internal_location, key, dir);
+}
+
 const struct expr_ops ct_expr_ops = {
 	.type		= EXPR_CT,
 	.name		= "ct",
@@ -375,6 +429,8 @@ const struct expr_ops ct_expr_ops = {
 	.cmp		= ct_expr_cmp,
 	.clone		= ct_expr_clone,
 	.pctx_update	= ct_expr_pctx_update,
+	.parse_udata	= ct_expr_parse_udata,
+	.build_udata	= ct_expr_build_udata,
 };
 
 struct expr *ct_expr_alloc(const struct location *loc, enum nft_ct_keys key,
src/expression.c +1 -0
index 7d198222c90b..14bf329e25c3 100644
@@ -1231,6 +1231,7 @@ const struct expr_ops *expr_ops_by_type(enum expr_types etype)
 	case EXPR_META: return &meta_expr_ops;
 	case EXPR_SOCKET: return &socket_expr_ops;
 	case EXPR_OSF: return &osf_expr_ops;
+	case EXPR_CT: return &ct_expr_ops;
 	default:
 		break;
 	}
Signature · 1 line
2.11.0
author maintainer
#7 lore raw

PATCH 7/11 numgen: add parse and build userdata interface

In reply to Pablo Neira Ayuso

Add support for meta userdata area.

---
 src/expression.c |  1 +
 src/numgen.c     | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+)
src/expression.c +1 -0
index 14bf329e25c3..1791454ea466 100644
@@ -1232,6 +1232,7 @@ const struct expr_ops *expr_ops_by_type(enum expr_types etype)
 	case EXPR_SOCKET: return &socket_expr_ops;
 	case EXPR_OSF: return &osf_expr_ops;
 	case EXPR_CT: return &ct_expr_ops;
+	case EXPR_NUMGEN: return &numgen_expr_ops;
 	default:
 		break;
 	}
src/numgen.c +62 -0

74 lines not on this page · raw message

Signature · 1 line
2.11.0
author maintainer
#8 lore raw

PATCH 8/11 hash: add parse and build userdata interface

In reply to Pablo Neira Ayuso

Add support for meta userdata area.

---
 src/expression.c |  1 +
 src/hash.c       | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+)
src/expression.c +1 -0
index 1791454ea466..7cca342b2f1a 100644
@@ -1233,6 +1233,7 @@ const struct expr_ops *expr_ops_by_type(enum expr_types etype)
 	case EXPR_OSF: return &osf_expr_ops;
 	case EXPR_CT: return &ct_expr_ops;
 	case EXPR_NUMGEN: return &numgen_expr_ops;
+	case EXPR_HASH: return &hash_expr_ops;
 	default:
 		break;
 	}
src/hash.c +72 -0

84 lines not on this page · raw message

Signature · 1 line
2.11.0
author maintainer
#9 lore raw

PATCH 9/11 rt: add parse and build userdata interface

In reply to Pablo Neira Ayuso

Add support for meta userdata area.

---
 src/expression.c |  1 +
 src/rt.c         | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)
src/expression.c +1 -0
index 7cca342b2f1a..386309b5ca27 100644
@@ -1234,6 +1234,7 @@ const struct expr_ops *expr_ops_by_type(enum expr_types etype)
 	case EXPR_CT: return &ct_expr_ops;
 	case EXPR_NUMGEN: return &numgen_expr_ops;
 	case EXPR_HASH: return &hash_expr_ops;
+	case EXPR_RT: return &rt_expr_ops;
 	default:
 		break;
 	}
src/rt.c +51 -0

63 lines not on this page · raw message

Signature · 1 line
2.11.0
author maintainer
#10 lore raw

PATCH 10/11 fib: add parse and build userdata interface

In reply to Pablo Neira Ayuso

Add support for meta userdata area.

---
 src/expression.c |  1 +
 src/fib.c        | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 59 insertions(+), 2 deletions(-)
src/expression.c +1 -0
index 386309b5ca27..43d2c9f94a84 100644
@@ -1235,6 +1235,7 @@ const struct expr_ops *expr_ops_by_type(enum expr_types etype)
 	case EXPR_NUMGEN: return &numgen_expr_ops;
 	case EXPR_HASH: return &hash_expr_ops;
 	case EXPR_RT: return &rt_expr_ops;
+	case EXPR_FIB: return &fib_expr_ops;
 	default:
 		break;
 	}
src/fib.c +58 -2

84 lines not on this page · raw message

Signature · 1 line
2.11.0
author maintainer
#11 lore raw

PATCH 11/11 xfrm: add parse and build userdata interface

In reply to Pablo Neira Ayuso

Add support for meta userdata area.

---
 src/expression.c |  1 +
 src/xfrm.c       | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+)
src/expression.c +1 -0
index 43d2c9f94a84..cb11cda43792 100644
@@ -1236,6 +1236,7 @@ const struct expr_ops *expr_ops_by_type(enum expr_types etype)
 	case EXPR_HASH: return &hash_expr_ops;
 	case EXPR_RT: return &rt_expr_ops;
 	case EXPR_FIB: return &fib_expr_ops;
+	case EXPR_XFRM: return &xfrm_expr_ops;
 	default:
 		break;
 	}
src/xfrm.c +61 -0

73 lines not on this page · raw message

Signature · 1 line
2.11.0