Skip to contentSkip to conversation

src: add string API support

RFC PATCH New Pablo Neira Ayuso 1 participant 3 messages Jul 29, 2022 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

RFC PATCH 1/3 src: add string API support

This patch adds strset object that represent a "string set" object. There is also str object that represent a list of patterns to be added/deleted to the "string set".

This patch provides 6 example files to create "string set" and populate them with patterns:

examples/nft-str-add.c
examples/nft-str-del.c
examples/nft-str-get.c
examples/nft-strset-add.c
examples/nft-strset-del.c
examples/nft-strset-get.c

---
 examples/Makefile.am                |  26 +-
 examples/nft-str-add.c              | 134 +++++++++
 examples/nft-str-del.c              | 133 +++++++++
 examples/nft-str-get.c              | 122 ++++++++
 examples/nft-strset-add.c           | 131 ++++++++
 examples/nft-strset-del.c           | 111 +++++++
 examples/nft-strset-get.c           | 122 ++++++++
 include/libnftnl/str.h              |  56 ++++
 include/linux/netfilter/nf_tables.h |  45 ++-
 include/str.h                       |  26 ++
 src/Makefile.am                     |   1 +
 src/libnftnl.map                    |  21 ++
 src/str.c                           | 443 ++++++++++++++++++++++++++++
 13 files changed, 1368 insertions(+), 3 deletions(-)
 create mode 100644 examples/nft-str-add.c
 create mode 100644 examples/nft-str-del.c
 create mode 100644 examples/nft-str-get.c
 create mode 100644 examples/nft-strset-add.c
 create mode 100644 examples/nft-strset-del.c
 create mode 100644 examples/nft-strset-get.c
 create mode 100644 include/libnftnl/str.h
 create mode 100644 include/str.h
 create mode 100644 src/str.c
examples/Makefile.am +25 -1
index db9164ddf946..10998ffe3399 100644
@@ -37,7 +37,13 @@ check_PROGRAMS = nft-table-add		\
 		 nft-ct-helper-del	\
 		 nft-rule-ct-helper-add \
 		 nft-rule-ct-expectation-add \
-		 nft-rule-ct-timeout-add
+		 nft-rule-ct-timeout-add \
+		 nft-strset-add		\
+		 nft-strset-del		\
+		 nft-strset-get		\
+		 nft-str-add		\
+		 nft-str-del		\
+		 nft-str-get
 
 nft_table_add_SOURCES = nft-table-add.c
 nft_table_add_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
@@ -152,3 +158,21 @@ nft_rule_ct_expectation_add_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
 
 nft_rule_ct_timeout_add_SOURCES = nft-rule-ct-timeout-add.c
 nft_rule_ct_timeout_add_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
+
+nft_strset_add_SOURCES = nft-strset-add.c
+nft_strset_add_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
+
+nft_strset_del_SOURCES = nft-strset-del.c
+nft_strset_del_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
+
+nft_strset_get_SOURCES = nft-strset-get.c
+nft_strset_get_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
+
+nft_str_add_SOURCES = nft-str-add.c
+nft_str_add_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
+
+nft_str_del_SOURCES = nft-str-del.c
+nft_str_del_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
+
+nft_str_get_SOURCES = nft-str-get.c
+nft_str_get_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
examples/nft-str-add.c new file +134 -0

134 lines not on this page · raw message

examples/nft-str-del.c new file +133 -0

133 lines not on this page · raw message

examples/nft-str-get.c new file +122 -0

122 lines not on this page · raw message

examples/nft-strset-add.c new file +131 -0

131 lines not on this page · raw message

examples/nft-strset-del.c new file +111 -0

111 lines not on this page · raw message

examples/nft-strset-get.c new file +122 -0

122 lines not on this page · raw message

include/libnftnl/str.h new file +56 -0

56 lines not on this page · raw message

include/linux/netfilter/nf_tables.h +43 -2

75 lines not on this page · raw message

include/str.h new file +26 -0
new file mode 100644
index 000000000000..6800c443e365
@@ -0,0 +1,26 @@
+#ifndef _LIBNFTNL_STRING_INTERNAL_H_
+#define _LIBNFTNL_STRING_INTERNAL_H_
+
+#include <linux/netfilter/nf_tables.h>
+
+struct nftnl_string {
+	struct list_head	head;
+
+	uint32_t		family;
+	const char		*table;
+	const char		*name;
+	uint64_t		handle;
+
+	uint32_t		flags;
+
+	struct list_head	string_list;
+};
+
+struct nftnl_string_list;
+
+struct nftnl_str {
+	struct list_head	head;
+	const char		*value;
+};
+
+#endif
src/Makefile.am +1 -0
index c3b0ab974bd2..24825d4e3e38 100644
@@ -17,6 +17,7 @@ libnftnl_la_SOURCES = utils.c		\
 		      rule.c		\
 		      set.c		\
 		      set_elem.c	\
+		      str.c		\
 		      ruleset.c		\
 		      udata.c		\
 		      expr.c		\
src/libnftnl.map +21 -0
index ad8f2af060ae..c39ba541e009 100644
@@ -387,3 +387,24 @@ LIBNFTNL_16 {
 LIBNFTNL_17 {
   nftnl_set_elem_nlmsg_build;
 } LIBNFTNL_16;
+
+LIBNFTNL_18 {
+  nftnl_string_alloc;
+  nftnl_string_free;
+  nftnl_string_is_set;
+  nftnl_string_unset;
+  nftnl_string_set_data;
+  nftnl_string_set_u32;
+  nftnl_string_set_u64;
+  nftnl_string_set_str;
+  nftnl_string_get_data;
+  nftnl_string_get;
+  nftnl_string_get_str;
+  nftnl_string_get_u32;
+  nftnl_string_get_u64;
+  nftnl_string_nlmsg_build_payload;
+  nftnl_string_add;
+  nftnl_string_nlmsg_parse;
+  nftnl_string_snprintf;
+  nftnl_string_foreach;
+} LIBNFTNL_17;
src/str.c new file +443 -0

443 lines not on this page · raw message

Signature · 1 line
2.30.2
author maintainer
#1 lore raw

RFC PATCH 2/3 expr: add string expression

Add string expression to use to match on existing "string sets" from rules.

---
 include/libnftnl/expr.h             |   9 ++
 include/linux/netfilter/nf_tables.h |  26 ++++
 src/Makefile.am                     |   1 +
 src/expr/str.c                      | 223 ++++++++++++++++++++++++++++
 src/expr_ops.c                      |   2 +
 5 files changed, 261 insertions(+)
 create mode 100644 src/expr/str.c
include/libnftnl/expr.h +9 -0
index 00c63ab9d19b..997e19a13662 100644
@@ -78,6 +78,15 @@ enum {
 	NFTNL_EXPR_RT_DREG,
 };
 
+enum {
+	NFTNL_EXPR_STR_NAME	= NFTNL_EXPR_BASE,
+	NFTNL_EXPR_STR_BASE,
+	NFTNL_EXPR_STR_FROM,
+	NFTNL_EXPR_STR_TO,
+	NFTNL_EXPR_STR_DREG,
+	NFTNL_EXPR_STR_FLAGS,
+};
+
 enum {
 	NFTNL_EXPR_SOCKET_KEY	= NFTNL_EXPR_BASE,
 	NFTNL_EXPR_SOCKET_DREG,
include/linux/netfilter/nf_tables.h +26 -0
index d7a668199611..8ce2400952c0 100644
@@ -1711,6 +1711,32 @@ enum nft_string_hook_attributes {
 };
 #define NFTA_STRING_MAX	(__NFTA_STRING_MAX - 1)
 
+enum nft_string_flags {
+	NFT_STR_F_PRESENT = (1 << 0),
+};
+
+/**
+ * enum nft_str_hook_attributes - nf_tables string expression netlink attributes
+ *
+ * @NFTA_STR_NAME: string set name (NLA_STRING)
+ * @NFTA_STR_BASE: payload base (NLA_U32: nft_payload_bases)
+ * @NFTA_STR_FROM: offset to start matching from (NLA_U32)
+ * @NFTA_STR_TO: offset to end matching to (NLA_U32)
+ * @NFTA_STR_DREG: destination register (NLA_U32: nft_registers)
+ * @NFTA_STR_FLAGS: flags (NLA_U32)
+ */
+enum nft_str_hook_attributes {
+	NFTA_STR_UNSPEC,
+	NFTA_STR_NAME,
+	NFTA_STR_BASE,
+	NFTA_STR_FROM,
+	NFTA_STR_TO,
+	NFTA_STR_DREG,
+	NFTA_STR_FLAGS,
+	__NFTA_STR_MAX
+};
+#define NFTA_STR_MAX	(__NFTA_STR_MAX - 1)
+
 /**
  * enum nft_osf_attributes - nftables osf expression netlink attributes
  *
src/Makefile.am +1 -0
index 24825d4e3e38..15ce569636c8 100644
@@ -52,6 +52,7 @@ libnftnl_la_SOURCES = utils.c		\
 		      expr/quota.c	\
 		      expr/reject.c	\
 		      expr/rt.c		\
+		      expr/str.c	\
 		      expr/target.c	\
 		      expr/tunnel.c	\
 		      expr/masq.c	\
src/expr/str.c new file +223 -0

223 lines not on this page · raw message

src/expr_ops.c +2 -0
index 7248e4f98b0a..85ea82008e1a 100644
@@ -30,6 +30,7 @@ extern struct expr_ops expr_ops_range;
 extern struct expr_ops expr_ops_redir;
 extern struct expr_ops expr_ops_reject;
 extern struct expr_ops expr_ops_rt;
+extern struct expr_ops expr_ops_str;
 extern struct expr_ops expr_ops_queue;
 extern struct expr_ops expr_ops_quota;
 extern struct expr_ops expr_ops_target;
@@ -74,6 +75,7 @@ static struct expr_ops *expr_ops[] = {
 	&expr_ops_redir,
 	&expr_ops_reject,
 	&expr_ops_rt,
+	&expr_ops_str,
 	&expr_ops_queue,
 	&expr_ops_quota,
 	&expr_ops_target,
Signature · 1 line
2.30.2
author maintainer
#2 lore raw

RFC PATCH 3/3 examples: update nft-rule-add to match on string

In reply to Pablo Neira Ayuso

An example to match on existing sets from rule.

---
Note: Not intended to be merged upstream, I posted as sample.

 examples/nft-rule-add.c | 32 +++++++++++++-------------------
 1 file changed, 13 insertions(+), 19 deletions(-)
examples/nft-rule-add.c +13 -19
index 77ee4805f4a4..5dc77354ed0b 100644
@@ -29,21 +29,22 @@
 #include <libnftnl/rule.h>
 #include <libnftnl/expr.h>
 
-static void add_payload(struct nftnl_rule *r, uint32_t base, uint32_t dreg,
-			uint32_t offset, uint32_t len)
+static void add_string(struct nftnl_rule *r)
 {
 	struct nftnl_expr *e;
 
-	e = nftnl_expr_alloc("payload");
+	e = nftnl_expr_alloc("string");
 	if (e == NULL) {
-		perror("expr payload oom");
+		perror("expr string oom");
 		exit(EXIT_FAILURE);
 	}
 
-	nftnl_expr_set_u32(e, NFTNL_EXPR_PAYLOAD_BASE, base);
-	nftnl_expr_set_u32(e, NFTNL_EXPR_PAYLOAD_DREG, dreg);
-	nftnl_expr_set_u32(e, NFTNL_EXPR_PAYLOAD_OFFSET, offset);
-	nftnl_expr_set_u32(e, NFTNL_EXPR_PAYLOAD_LEN, len);
+	nftnl_expr_set_str(e, NFTNL_EXPR_STR_NAME, "y");
+	nftnl_expr_set_u32(e, NFTNL_EXPR_STR_BASE, NFT_PAYLOAD_INNER_HEADER);
+	nftnl_expr_set_u32(e, NFTNL_EXPR_STR_FROM, 0);
+	nftnl_expr_set_u32(e, NFTNL_EXPR_STR_TO, ~0U);
+	nftnl_expr_set_u32(e, NFTNL_EXPR_STR_FLAGS, NFT_STR_F_PRESENT);
+	nftnl_expr_set_u32(e, NFTNL_EXPR_STR_DREG, NFT_REG_1);
 
 	nftnl_rule_add_expr(r, e);
 }
@@ -83,9 +84,8 @@ static struct nftnl_rule *setup_rule(uint8_t family, const char *table,
 				   const char *chain, const char *handle)
 {
 	struct nftnl_rule *r = NULL;
-	uint8_t proto;
-	uint16_t dport;
 	uint64_t handle_num;
+	uint32_t v;
 
 	r = nftnl_rule_alloc();
 	if (r == NULL) {
@@ -102,15 +102,9 @@ static struct nftnl_rule *setup_rule(uint8_t family, const char *table,
 		nftnl_rule_set_u64(r, NFTNL_RULE_POSITION, handle_num);
 	}
 
-	proto = IPPROTO_TCP;
-	add_payload(r, NFT_PAYLOAD_NETWORK_HEADER, NFT_REG_1,
-		    offsetof(struct iphdr, protocol), sizeof(uint8_t));
-	add_cmp(r, NFT_REG_1, NFT_CMP_EQ, &proto, sizeof(uint8_t));
-
-	dport = htons(22);
-	add_payload(r, NFT_PAYLOAD_TRANSPORT_HEADER, NFT_REG_1,
-		    offsetof(struct tcphdr, dest), sizeof(uint16_t));
-	add_cmp(r, NFT_REG_1, NFT_CMP_EQ, &dport, sizeof(uint16_t));
+	v = 1;
+	add_string(r);
+	add_cmp(r, NFT_REG_1, NFT_CMP_EQ, &v, sizeof(uint32_t));
 
 	add_counter(r);
 
Signature · 1 line
2.30.2