Skip to contentSkip to conversation

include: fetch stateful object updates for nf_tables.h cache copy

PATCH New Pablo Neira Ayuso 1 participant 7 messages Dec 9, 2016 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/7 include: fetch stateful object updates for nf_tables.h cache copy

This patch includes updates for the named stateful expressions.

---
 include/linux/netfilter/nf_tables.h | 64 +++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)
include/linux/netfilter/nf_tables.h +64 -0
index f030e59aa2ec..881d49e94569 100644
@@ -4,6 +4,7 @@
 #define NFT_TABLE_MAXNAMELEN	32
 #define NFT_CHAIN_MAXNAMELEN	32
 #define NFT_SET_MAXNAMELEN	32
+#define NFT_OBJ_MAXNAMELEN	32
 #define NFT_USERDATA_MAXLEN	256
 
 /**
@@ -85,6 +86,10 @@ enum nft_verdicts {
  * @NFT_MSG_NEWGEN: announce a new generation, only for events (enum nft_gen_attributes)
  * @NFT_MSG_GETGEN: get the rule-set generation (enum nft_gen_attributes)
  * @NFT_MSG_TRACE: trace event (enum nft_trace_attributes)
+ * @NFT_MSG_NEWOBJ: create a stateful object (enum nft_obj_attributes)
+ * @NFT_MSG_GETOBJ: get a stateful object (enum nft_obj_attributes)
+ * @NFT_MSG_DELOBJ: delete a stateful object (enum nft_obj_attributes)
+ * @NFT_MSG_GETOBJ_RESET: get and reset a stateful object (enum nft_obj_attributes)
  */
 enum nf_tables_msg_types {
 	NFT_MSG_NEWTABLE,
@@ -105,6 +110,10 @@ enum nf_tables_msg_types {
 	NFT_MSG_NEWGEN,
 	NFT_MSG_GETGEN,
 	NFT_MSG_TRACE,
+	NFT_MSG_NEWOBJ,
+	NFT_MSG_GETOBJ,
+	NFT_MSG_DELOBJ,
+	NFT_MSG_GETOBJ_RESET,
 	NFT_MSG_MAX,
 };
 
@@ -246,6 +255,7 @@ enum nft_rule_compat_attributes {
  * @NFT_SET_MAP: set is used as a dictionary
  * @NFT_SET_TIMEOUT: set uses timeouts
  * @NFT_SET_EVAL: set contains expressions for evaluation
+ * @NFT_SET_OBJECT: set contains stateful objects
  */
 enum nft_set_flags {
 	NFT_SET_ANONYMOUS		= 0x1,
@@ -254,6 +264,7 @@ enum nft_set_flags {
 	NFT_SET_MAP			= 0x8,
 	NFT_SET_TIMEOUT			= 0x10,
 	NFT_SET_EVAL			= 0x20,
+	NFT_SET_OBJECT			= 0x40,
 };
 
 /**
@@ -295,6 +306,7 @@ enum nft_set_desc_attributes {
  * @NFTA_SET_TIMEOUT: default timeout value (NLA_U64)
  * @NFTA_SET_GC_INTERVAL: garbage collection interval (NLA_U32)
  * @NFTA_SET_USERDATA: user data (NLA_BINARY)
+ * @NFTA_SET_OBJ_TYPE: stateful object type (NLA_U32: NFT_OBJECT_*)
  */
 enum nft_set_attributes {
 	NFTA_SET_UNSPEC,
@@ -312,6 +324,7 @@ enum nft_set_attributes {
 	NFTA_SET_GC_INTERVAL,
 	NFTA_SET_USERDATA,
 	NFTA_SET_PAD,
+	NFTA_SET_OBJ_TYPE,
 	__NFTA_SET_MAX
 };
 #define NFTA_SET_MAX		(__NFTA_SET_MAX - 1)
@@ -335,6 +348,7 @@ enum nft_set_elem_flags {
  * @NFTA_SET_ELEM_EXPIRATION: expiration time (NLA_U64)
  * @NFTA_SET_ELEM_USERDATA: user data (NLA_BINARY)
  * @NFTA_SET_ELEM_EXPR: expression (NLA_NESTED: nft_expr_attributes)
+ * @NFTA_SET_ELEM_OBJREF: stateful object reference (NLA_STRING)
  */
 enum nft_set_elem_attributes {
 	NFTA_SET_ELEM_UNSPEC,
@@ -346,6 +360,7 @@ enum nft_set_elem_attributes {
 	NFTA_SET_ELEM_USERDATA,
 	NFTA_SET_ELEM_EXPR,
 	NFTA_SET_ELEM_PAD,
+	NFTA_SET_ELEM_OBJREF,
 	__NFTA_SET_ELEM_MAX
 };
 #define NFTA_SET_ELEM_MAX	(__NFTA_SET_ELEM_MAX - 1)
@@ -974,6 +989,7 @@ enum nft_queue_attributes {
 
 enum nft_quota_flags {
 	NFT_QUOTA_F_INV		= (1 << 0),
+	NFT_QUOTA_F_DEPLETED	= (1 << 1),
 };
 
 /**
@@ -981,12 +997,14 @@ enum nft_quota_flags {
  *
  * @NFTA_QUOTA_BYTES: quota in bytes (NLA_U16)
  * @NFTA_QUOTA_FLAGS: flags (NLA_U32)
+ * @NFTA_QUOTA_CONSUMED: quota already consumed in bytes (NLA_U64)
  */
 enum nft_quota_attributes {
 	NFTA_QUOTA_UNSPEC,
 	NFTA_QUOTA_BYTES,
 	NFTA_QUOTA_FLAGS,
 	NFTA_QUOTA_PAD,
+	NFTA_QUOTA_CONSUMED,
 	__NFTA_QUOTA_MAX
 };
 #define NFTA_QUOTA_MAX		(__NFTA_QUOTA_MAX - 1)
@@ -1131,6 +1149,26 @@ enum nft_fwd_attributes {
 #define NFTA_FWD_MAX	(__NFTA_FWD_MAX - 1)
 
 /**
+ * enum nft_objref_attributes - nf_tables stateful object expression netlink attributes
+ *
+ * @NFTA_OBJREF_IMM_TYPE: object type for immediate reference (NLA_U32: nft_register)
+ * @NFTA_OBJREF_IMM_NAME: object name for immediate reference (NLA_STRING)
+ * @NFTA_OBJREF_SET_SREG: source register of the data to look for (NLA_U32: nft_registers)
+ * @NFTA_OBJREF_SET_NAME: name of the set where to look for (NLA_STRING)
+ * @NFTA_OBJREF_SET_ID: id of the set where to look for in this transaction (NLA_U32)
+ */
+enum nft_objref_attributes {
+	NFTA_OBJREF_UNSPEC,
+	NFTA_OBJREF_IMM_TYPE,
+	NFTA_OBJREF_IMM_NAME,
+	NFTA_OBJREF_SET_SREG,
+	NFTA_OBJREF_SET_NAME,
+	NFTA_OBJREF_SET_ID,
+	__NFTA_OBJREF_MAX
+};
+#define NFTA_OBJREF_MAX	(__NFTA_OBJREF_MAX - 1)
+
+/**
  * enum nft_gen_attributes - nf_tables ruleset generation attributes
  *
  * @NFTA_GEN_ID: Ruleset generation ID (NLA_U32)
@@ -1178,6 +1216,32 @@ enum nft_fib_flags {
 	NFTA_FIB_F_OIF		= 1 << 4,	/* restrict to oif */
 };
 
+#define NFT_OBJECT_UNSPEC	0
+#define NFT_OBJECT_COUNTER	1
+#define NFT_OBJECT_QUOTA	2
+#define __NFT_OBJECT_MAX	3
+#define NFT_OBJECT_MAX		(__NFT_OBJECT_MAX - 1)
+
+/**
+ * enum nft_object_attributes - nf_tables stateful object netlink attributes
+ *
+ * @NFTA_OBJ_TABLE: name of the table containing the expression (NLA_STRING)
+ * @NFTA_OBJ_NAME: name of this expression type (NLA_STRING)
+ * @NFTA_OBJ_TYPE: stateful object type (NLA_U32)
+ * @NFTA_OBJ_DATA: stateful object data (NLA_NESTED)
+ * @NFTA_OBJ_USE: number of references to this expression (NLA_U32)
+ */
+enum nft_object_attributes {
+	NFTA_OBJ_UNSPEC,
+	NFTA_OBJ_TABLE,
+	NFTA_OBJ_NAME,
+	NFTA_OBJ_TYPE,
+	NFTA_OBJ_DATA,
+	NFTA_OBJ_USE,
+	__NFTA_OBJ_MAX
+};
+#define NFTA_OBJ_MAX		(__NFTA_OBJ_MAX - 1)
+
 /**
  * enum nft_trace_attributes - nf_tables trace netlink attributes
  *
Signature · 1 line
2.1.4
author maintainer
#1 lore raw

PATCH 2/7 src: support for stateful objects

This patch allows you to add, to delete and to get stateful objects, this support two object types: counter and quota.

---
 examples/Makefile.am         |  12 +
 examples/nft-obj-add.c       | 123 ++++++++++
 examples/nft-obj-del.c       | 126 ++++++++++
 examples/nft-obj-get.c       | 151 ++++++++++++
 include/Makefile.am          |   1 +
 include/buffer.h             |   1 +
 include/libnftnl/Makefile.am |   1 +
 include/libnftnl/object.h    |  89 +++++++
 include/obj.h                |  55 +++++
 src/Makefile.am              |   3 +
 src/libnftnl.map             |  31 +++
 src/obj/counter.c            | 184 ++++++++++++++
 src/obj/quota.c              | 205 ++++++++++++++++
 src/object.c                 | 573 +++++++++++++++++++++++++++++++++++++++++++
 tests/Makefile.am            |   4 +
 tests/nft-object-test.c      |  78 ++++++
 tests/test-script.sh         |   1 +
 17 files changed, 1638 insertions(+)
 create mode 100644 examples/nft-obj-add.c
 create mode 100644 examples/nft-obj-del.c
 create mode 100644 examples/nft-obj-get.c
 create mode 100644 include/libnftnl/object.h
 create mode 100644 include/obj.h
 create mode 100644 src/obj/counter.c
 create mode 100644 src/obj/quota.c
 create mode 100644 src/object.c
 create mode 100644 tests/nft-object-test.c
examples/Makefile.am +12 -0
index 9dca3a15c2c4..48bc7a16c9c6 100644
@@ -22,6 +22,9 @@ check_PROGRAMS = nft-table-add		\
 		 nft-set-elem-add	\
 		 nft-set-elem-get	\
 		 nft-set-elem-del	\
+		 nft-obj-add		\
+		 nft-obj-get		\
+		 nft-obj-del		\
 		 nft-ruleset-get	\
 		 nft-ruleset-parse-file	\
 		 nft-compat-get
@@ -92,6 +95,15 @@ nft_set_elem_del_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
 nft_set_elem_get_SOURCES = nft-set-elem-get.c
 nft_set_elem_get_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
 
+nft_obj_add_SOURCES = nft-obj-add.c
+nft_obj_add_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
+
+nft_obj_del_SOURCES = nft-obj-del.c
+nft_obj_del_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
+
+nft_obj_get_SOURCES = nft-obj-get.c
+nft_obj_get_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
+
 nft_ruleset_get_SOURCES = nft-ruleset-get.c
 nft_ruleset_get_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
 
examples/nft-obj-add.c new file +123 -0

123 lines not on this page · raw message

examples/nft-obj-del.c new file +126 -0

126 lines not on this page · raw message

examples/nft-obj-get.c new file +151 -0

151 lines not on this page · raw message

include/Makefile.am +1 -0
index a049e2e1de5d..fd4cb408a44a 100644
@@ -5,6 +5,7 @@ noinst_HEADERS = internal.h	\
 		 buffer.h	\
 		 data_reg.h	\
 		 expr_ops.h	\
+		 obj.h		\
 		 linux_list.h	\
 		 set.h		\
 		 common.h	\
include/buffer.h +1 -0
index ab1d468af989..c5716570f9d0 100644
@@ -41,6 +41,7 @@ int nftnl_buf_reg(struct nftnl_buf *b, int type, union nftnl_data_reg *reg,
 #define BURST			"burst"
 #define CHAIN			"chain"
 #define CODE			"code"
+#define CONSUMED		"consumed"
 #define DATA			"data"
 #define DEVICE			"device"
 #define DIR			"dir"
include/libnftnl/Makefile.am +1 -0
index 457ec95bcbc5..6dc7b2b38590 100644
@@ -2,6 +2,7 @@ pkginclude_HEADERS = batch.h		\
 		     table.h		\
 		     trace.h		\
 		     chain.h		\
+		     object.h		\
 		     rule.h		\
 		     expr.h		\
 		     set.h		\
include/libnftnl/object.h new file +89 -0

89 lines not on this page · raw message

include/obj.h new file +55 -0

55 lines not on this page · raw message

src/Makefile.am +3 -0
index e1e4144526c3..909c6a668b77 100644
@@ -13,6 +13,7 @@ libnftnl_la_SOURCES = utils.c		\
 		      table.c		\
 		      trace.c		\
 		      chain.c		\
+		      object.c		\
 		      rule.c		\
 		      set.c		\
 		      set_elem.c	\
@@ -50,4 +51,6 @@ libnftnl_la_SOURCES = utils.c		\
 		      expr/masq.c	\
 		      expr/redir.c	\
 		      expr/hash.c	\
+		      obj/counter.c	\
+		      obj/quota.c	\
 		      libnftnl.map
src/libnftnl.map +31 -0
index abed8b9e1295..64b9b0ba4f1f 100644
@@ -533,3 +533,34 @@ LIBNFTNL_4.2 {
 	nftnl_rule_cmp;
 	nftnl_expr_cmp;
 } LIBNFTNL_4.1;
+
+LIBNFTNL_4.3 {
+	nftnl_obj_alloc;
+	nftnl_obj_free;
+	nftnl_obj_is_set;
+	nftnl_obj_unset;
+	nftnl_obj_set;
+	nftnl_obj_get;
+	nftnl_obj_set_u32;
+	nftnl_obj_set_u64;
+	nftnl_obj_set_str;
+	nftnl_obj_get_u32;
+	nftnl_obj_get_str;
+	nftnl_obj_get_u64;
+	nftnl_obj_parse;
+	nftnl_obj_parse_file;
+	nftnl_obj_snprintf;
+	nftnl_obj_fprintf;
+	nftnl_obj_nlmsg_build_payload;
+	nftnl_obj_nlmsg_parse;
+	nftnl_obj_list_alloc;
+	nftnl_obj_list_free;
+	nftnl_obj_list_is_empty;
+	nftnl_obj_list_foreach;
+	nftnl_obj_list_add;
+	nftnl_obj_list_add_tail;
+	nftnl_obj_list_del;
+	nftnl_obj_list_iter_create;
+	nftnl_obj_list_iter_next;
+	nftnl_obj_list_iter_destroy;
+} LIBNFTNL_4.2;
src/obj/counter.c new file +184 -0

184 lines not on this page · raw message

src/obj/quota.c new file +205 -0

205 lines not on this page · raw message

src/object.c new file +573 -0

573 lines not on this page · raw message

tests/Makefile.am +4 -0
index d51895974406..812ebff17126 100644
@@ -6,6 +6,7 @@ EXTRA_DIST =		test-script.sh			\
 check_PROGRAMS = 	nft-parsing-test		\
 			nft-table-test			\
 			nft-chain-test			\
+			nft-object-test			\
 			nft-rule-test			\
 			nft-set-test			\
 			nft-expr_bitwise-test		\
@@ -43,6 +44,9 @@ nft_table_test_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
 nft_chain_test_SOURCES = nft-chain-test.c
 nft_chain_test_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
 
+nft_object_test_SOURCES = nft-object-test.c
+nft_object_test_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
+
 nft_rule_test_SOURCES = nft-rule-test.c
 nft_rule_test_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
 
tests/nft-object-test.c new file +78 -0

78 lines not on this page · raw message

tests/test-script.sh +1 -0
index 3244f802e1cb..743e514da698 100755
@@ -27,4 +27,5 @@
 ./nft-rule-test
 ./nft-set-test
 ./nft-table-test
+./nft-object-test
 ./nft-parsing-test -d jsonfiles
Signature · 1 line
2.1.4
author maintainer
#2 lore raw

PATCH 3/7 expr: add stateful object reference expression

In reply to Pablo Neira Ayuso

This patch adds a new "objref" expression that you can use to refer to stateful objects from rules.

---
 include/libnftnl/expr.h      |   5 +
 src/Makefile.am              |   1 +
 src/expr/objref.c            | 215 +++++++++++++++++++++++++++++++++++++++++++
 src/expr_ops.c               |   2 +
 src/object.c                 |   5 +
 tests/Makefile.am            |   4 +
 tests/nft-expr_objref-test.c |  96 +++++++++++++++++++
 tests/test-script.sh         |   1 +
 8 files changed, 329 insertions(+)
 create mode 100644 src/expr/objref.c
 create mode 100644 tests/nft-expr_objref-test.c
include/libnftnl/expr.h +5 -0
index 979241aac535..f6ea69dfacae 100644
@@ -234,6 +234,11 @@ enum {
 	NFTNL_EXPR_FIB_FLAGS,
 };
 
+enum {
+	NFTNL_EXPR_OBJREF_IMM_TYPE	= NFTNL_EXPR_BASE,
+	NFTNL_EXPR_OBJREF_IMM_NAME,
+};
+
 /*
  * Compat
  */
src/Makefile.am +1 -0
index 909c6a668b77..485a8c4acbef 100644
@@ -42,6 +42,7 @@ libnftnl_la_SOURCES = utils.c		\
 		      expr/meta.c	\
 		      expr/numgen.c	\
 		      expr/nat.c	\
+		      expr/objref.c	\
 		      expr/payload.c	\
 		      expr/queue.c	\
 		      expr/quota.c	\
src/expr/objref.c new file +215 -0

215 lines not on this page · raw message

src/expr_ops.c +2 -0
index 4d7d1fab4577..7a0e1e3d0a26 100644
@@ -21,6 +21,7 @@ extern struct expr_ops expr_ops_match;
 extern struct expr_ops expr_ops_meta;
 extern struct expr_ops expr_ops_ng;
 extern struct expr_ops expr_ops_nat;
+extern struct expr_ops expr_ops_objref;
 extern struct expr_ops expr_ops_payload;
 extern struct expr_ops expr_ops_range;
 extern struct expr_ops expr_ops_redir;
@@ -67,6 +68,7 @@ static struct expr_ops *expr_ops[] = {
 	&expr_ops_dynset,
 	&expr_ops_hash,
 	&expr_ops_fib,
+	&expr_ops_objref,
 	NULL,
 };
 
src/object.c +5 -0
index 0190e9409797..0d3dc2b1e678 100644
@@ -276,6 +276,7 @@ static int nftnl_jansson_parse_obj(struct nftnl_obj *t, json_t *tree,
 			    struct nftnl_parse_err *err)
 {
 	const char *str;
+	uint32_t type;
 	json_t *root;
 
 	root = nftnl_jansson_get_node(tree, "obj", err);
@@ -290,6 +291,10 @@ static int nftnl_jansson_parse_obj(struct nftnl_obj *t, json_t *tree,
 	if (str != NULL)
 		nftnl_obj_set_str(t, NFTNL_OBJ_NAME, str);
 
+	if (nftnl_jansson_parse_val(root, "type", NFTNL_TYPE_U32, &type,
+				    err) < 0)
+		nftnl_obj_set_u32(t, NFTNL_OBJ_TYPE, type);
+
 	return 0;
 }
 #endif
tests/Makefile.am +4 -0
index 812ebff17126..1467d99aa62c 100644
@@ -26,6 +26,7 @@ check_PROGRAMS = 	nft-parsing-test		\
 			nft-expr_meta-test		\
 			nft-expr_numgen-test		\
 			nft-expr_nat-test		\
+			nft-expr_objref-test		\
 			nft-expr_payload-test		\
 			nft-expr_queue-test		\
 			nft-expr_range-test             \
@@ -104,6 +105,9 @@ nft_expr_numgen_test_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
 nft_expr_nat_test_SOURCES = nft-expr_nat-test.c
 nft_expr_nat_test_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
 
+nft_expr_objref_test_SOURCES = nft-expr_objref-test.c
+nft_expr_objref_test_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
+
 nft_expr_payload_test_SOURCES = nft-expr_payload-test.c
 nft_expr_payload_test_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
 
tests/nft-expr_objref-test.c new file +96 -0

96 lines not on this page · raw message

tests/test-script.sh +1 -0
index 743e514da698..2bd7ef0e6716 100755
@@ -19,6 +19,7 @@
 ./nft-expr_range-test
 ./nft-expr_redir-test
 ./nft-expr_nat-test
+./nft-expr_objref-test
 ./nft-expr_payload-test
 ./nft-expr_quota-test
 ./nft-expr_reject-test
Signature · 1 line
2.1.4
author maintainer
#3 lore raw

PATCH 4/7 set: add NFTNL_SET_OBJ_TYPE attribute

In reply to Pablo Neira Ayuso

This new attribute specifies the stateful object type this set stores. Similar to data type, but specific to store objects. You must set the NFT_SET_OBJECT flag to use this.

---
 include/libnftnl/set.h |  1 +
 include/set.h          |  1 +
 src/set.c              | 27 +++++++++++++++++++++++++++
 3 files changed, 29 insertions(+)
include/libnftnl/set.h +1 -0
index adeb16c66e2b..0c978d916e4e 100644
@@ -23,6 +23,7 @@ enum nftnl_set_attr {
 	NFTNL_SET_TIMEOUT,
 	NFTNL_SET_GC_INTERVAL,
 	NFTNL_SET_USERDATA,
+	NFTNL_SET_OBJ_TYPE,
 	__NFTNL_SET_MAX
 };
 #define NFTNL_SET_MAX (__NFTNL_SET_MAX - 1)
include/set.h +1 -0
index 85bd389a3bd8..c6deb73f54f4 100644
@@ -14,6 +14,7 @@ struct nftnl_set {
 	uint32_t		key_len;
 	uint32_t		data_type;
 	uint32_t		data_len;
+	uint32_t		obj_type;
 	struct {
 		void		*data;
 		uint32_t	len;
src/set.c +27 -0
index a42b713c6f87..14d28b502c1d 100644
@@ -80,6 +80,7 @@ void nftnl_set_unset(struct nftnl_set *s, uint16_t attr)
 	case NFTNL_SET_KEY_LEN:
 	case NFTNL_SET_DATA_TYPE:
 	case NFTNL_SET_DATA_LEN:
+	case NFTNL_SET_OBJ_TYPE:
 	case NFTNL_SET_FAMILY:
 	case NFTNL_SET_ID:
 	case NFTNL_SET_POLICY:
@@ -104,6 +105,7 @@ static uint32_t nftnl_set_validate[NFTNL_SET_MAX + 1] = {
 	[NFTNL_SET_KEY_LEN]		= sizeof(uint32_t),
 	[NFTNL_SET_DATA_TYPE]	= sizeof(uint32_t),
 	[NFTNL_SET_DATA_LEN]		= sizeof(uint32_t),
+	[NFTNL_SET_OBJ_TYPE]		= sizeof(uint32_t),
 	[NFTNL_SET_FAMILY]		= sizeof(uint32_t),
 	[NFTNL_SET_POLICY]		= sizeof(uint32_t),
 	[NFTNL_SET_DESC_SIZE]	= sizeof(uint32_t),
@@ -149,6 +151,9 @@ int nftnl_set_set_data(struct nftnl_set *s, uint16_t attr, const void *data,
 	case NFTNL_SET_DATA_LEN:
 		s->data_len = *((uint32_t *)data);
 		break;
+	case NFTNL_SET_OBJ_TYPE:
+		s->obj_type = *((uint32_t *)data);
+		break;
 	case NFTNL_SET_FAMILY:
 		s->family = *((uint32_t *)data);
 		break;
@@ -235,6 +240,9 @@ const void *nftnl_set_get_data(const struct nftnl_set *s, uint16_t attr,
 	case NFTNL_SET_DATA_LEN:
 		*data_len = sizeof(uint32_t);
 		return &s->data_len;
+	case NFTNL_SET_OBJ_TYPE:
+		*data_len = sizeof(uint32_t);
+		return &s->obj_type;
 	case NFTNL_SET_FAMILY:
 		*data_len = sizeof(uint32_t);
 		return &s->family;
@@ -360,6 +368,8 @@ void nftnl_set_nlmsg_build_payload(struct nlmsghdr *nlh, struct nftnl_set *s)
 		mnl_attr_put_u32(nlh, NFTA_SET_DATA_TYPE, htonl(s->data_type));
 	if (s->flags & (1 << NFTNL_SET_DATA_LEN))
 		mnl_attr_put_u32(nlh, NFTA_SET_DATA_LEN, htonl(s->data_len));
+	if (s->flags & (1 << NFTNL_SET_OBJ_TYPE))
+		mnl_attr_put_u32(nlh, NFTA_SET_OBJ_TYPE, htonl(s->obj_type));
 	if (s->flags & (1 << NFTNL_SET_ID))
 		mnl_attr_put_u32(nlh, NFTA_SET_ID, htonl(s->id));
 	if (s->flags & (1 << NFTNL_SET_POLICY))
@@ -498,6 +508,10 @@ int nftnl_set_nlmsg_parse(const struct nlmsghdr *nlh, struct nftnl_set *s)
 		s->data_len = ntohl(mnl_attr_get_u32(tb[NFTA_SET_DATA_LEN]));
 		s->flags |= (1 << NFTNL_SET_DATA_LEN);
 	}
+	if (tb[NFTA_SET_OBJ_TYPE]) {
+		s->obj_type = ntohl(mnl_attr_get_u32(tb[NFTA_SET_OBJ_TYPE]));
+		s->flags |= (1 << NFTNL_SET_OBJ_TYPE);
+	}
 	if (tb[NFTA_SET_ID]) {
 		s->id = ntohl(mnl_attr_get_u32(tb[NFTA_SET_ID]));
 		s->flags |= (1 << NFTNL_SET_ID);
@@ -586,6 +600,14 @@ static int nftnl_jansson_parse_set_info(struct nftnl_set *s, json_t *tree,
 		nftnl_set_set_u32(s, NFTNL_SET_DATA_LEN, data_len);
 	}
 
+	if (nftnl_jansson_node_exist(root, "obj_type")) {
+		if (nftnl_jansson_parse_val(root, "obj_type", NFTNL_TYPE_U32,
+					  &data_type, err) < 0)
+			return -1;
+
+		nftnl_set_set_u32(s, NFTNL_SET_OBJ_TYPE, data_type);
+	}
+
 	if (nftnl_jansson_node_exist(root, "policy")) {
 		if (nftnl_jansson_parse_val(root, "policy", NFTNL_TYPE_U32,
 					  &policy, err) < 0)
@@ -759,6 +781,11 @@ static int nftnl_set_snprintf_json(char *buf, size_t size,
 		ret = snprintf(buf + offset, len, ",\"data_len\":%u", s->data_len);
 		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
 	}
+	if (s->flags & (1 << NFTNL_SET_OBJ_TYPE)) {
+		ret = snprintf(buf + offset, len,
+				  ",\"obj_type\":%u", s->obj_type);
+		SNPRINTF_BUFFER_SIZE(ret, size, len, offset);
+	}
 
 	if (s->flags & (1 << NFTNL_SET_POLICY)) {
 		ret = snprintf(buf + offset, len, ",\"policy\":%u",
Signature · 1 line
2.1.4
author maintainer
#4 lore raw

PATCH 5/7 set_elem: add NFTNL_SET_ELEM_OBJREF attribute

In reply to Pablo Neira Ayuso

This new attribute allows us to attach stateful objects to elements for map lookups. This new attribute identifies the object through its name.

---
 include/libnftnl/set.h |  1 +
 include/set_elem.h     |  1 +
 src/set_elem.c         | 27 +++++++++++++++++++++++++++
 3 files changed, 29 insertions(+)
include/libnftnl/set.h +1 -0
index 0c978d916e4e..4c59ab27946f 100644
@@ -96,6 +96,7 @@ enum {
 	NFTNL_SET_ELEM_EXPIRATION,
 	NFTNL_SET_ELEM_USERDATA,
 	NFTNL_SET_ELEM_EXPR,
+	NFTNL_SET_ELEM_OBJREF,
 };
 
 struct nftnl_set_elem;
include/set_elem.h +1 -0
index 60cecc939016..d6244e60873a 100644
@@ -12,6 +12,7 @@ struct nftnl_set_elem {
 	uint32_t		flags;
 	uint64_t		timeout;
 	uint64_t		expiration;
+	const char		*objref;
 	struct {
 		void		*data;
 		uint32_t	len;
src/set_elem.c +27 -0
index 083c597e2f8e..fa8747641ee0 100644
@@ -50,6 +50,9 @@ void nftnl_set_elem_free(struct nftnl_set_elem *s)
 	if (s->flags & (1 << NFTNL_SET_ELEM_USERDATA))
 		xfree(s->user.data);
 
+	if (s->flags & (1 << NFTNL_SET_ELEM_OBJREF))
+		xfree(s->objref);
+
 	xfree(s);
 }
 EXPORT_SYMBOL_ALIAS(nftnl_set_elem_free, nft_set_elem_free);
@@ -82,6 +85,9 @@ void nftnl_set_elem_unset(struct nftnl_set_elem *s, uint16_t attr)
 	case NFTNL_SET_ELEM_EXPR:
 		nftnl_expr_free(s->expr);
 		break;
+	case NFTNL_SET_ELEM_OBJREF:
+		xfree(s->objref);
+		break;
 	default:
 		return;
 	}
@@ -129,6 +135,14 @@ int nftnl_set_elem_set(struct nftnl_set_elem *s, uint16_t attr,
 		memcpy(s->user.data, data, data_len);
 		s->user.len = data_len;
 		break;
+	case NFTNL_SET_ELEM_OBJREF:
+		if (s->flags & (1 << NFTNL_SET_ELEM_OBJREF))
+			xfree(s->objref);
+
+		s->objref = strdup(data);
+		if (!s->objref)
+			return -1;
+		break;
 	}
 	s->flags |= (1 << attr);
 	return -1;
@@ -185,6 +199,9 @@ const void *nftnl_set_elem_get(struct nftnl_set_elem *s, uint16_t attr, uint32_t
 		return s->user.data;
 	case NFTNL_SET_ELEM_EXPR:
 		return s->expr;
+	case NFTNL_SET_ELEM_OBJREF:
+		*data_len = strlen(s->objref) + 1;
+		return s->objref;
 	}
 	return NULL;
 }
@@ -271,6 +288,8 @@ void nftnl_set_elem_nlmsg_build_payload(struct nlmsghdr *nlh,
 	}
 	if (e->flags & (1 << NFTNL_SET_ELEM_USERDATA))
 		mnl_attr_put(nlh, NFTA_SET_ELEM_USERDATA, e->user.len, e->user.data);
+	if (e->flags & (1 << NFTNL_SET_ELEM_OBJREF))
+		mnl_attr_put_strz(nlh, NFTA_SET_ELEM_OBJREF, e->objref);
 }
 
 static void nftnl_set_elem_nlmsg_build_def(struct nlmsghdr *nlh,
@@ -423,6 +442,14 @@ static int nftnl_set_elems_parse2(struct nftnl_set *s, const struct nlattr *nest
 		memcpy(e->user.data, udata, e->user.len);
 		e->flags |= (1 << NFTNL_RULE_USERDATA);
 	}
+	if (tb[NFTA_SET_ELEM_OBJREF]) {
+		e->objref = strdup(mnl_attr_get_str(tb[NFTA_SET_ELEM_OBJREF]));
+		if (e->objref == NULL) {
+			ret = -1;
+			goto out_set_elem;
+		}
+		e->flags |= (1 << NFTNL_SET_ELEM_OBJREF);
+	}
 
 	/* Add this new element to this set */
 	list_add_tail(&e->head, &s->element_list);
Signature · 1 line
2.1.4
author maintainer
#5 lore raw

PATCH 6/7 expr: objref: add support for stateful object maps

In reply to Pablo Neira Ayuso

If the NFT_SET_OBJECT flag is set, then this set stores a mapping between any random user-defined arbitrary key and one stateful object. Very useful for performance lookups.

---
 include/libnftnl/expr.h |  3 +++
 src/expr/objref.c       | 72 +++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 73 insertions(+), 2 deletions(-)
include/libnftnl/expr.h +3 -0
index f6ea69dfacae..ee0784928550 100644
@@ -237,6 +237,9 @@ enum {
 enum {
 	NFTNL_EXPR_OBJREF_IMM_TYPE	= NFTNL_EXPR_BASE,
 	NFTNL_EXPR_OBJREF_IMM_NAME,
+	NFTNL_EXPR_OBJREF_SET_SREG,
+	NFTNL_EXPR_OBJREF_SET_NAME,
+	NFTNL_EXPR_OBJREF_SET_ID,
 };
 
 /*
src/expr/objref.c +70 -2

129 lines not on this page · raw message

Signature · 1 line
2.1.4
author maintainer
#6 lore raw

PATCH 7/7 quota: support for consumed bytes

In reply to Pablo Neira Ayuso

This patch extends the quota support to account for consumed bytes.

---
 include/libnftnl/expr.h |  1 +
 src/expr/quota.c        | 26 +++++++++++++++++++++++---
 2 files changed, 24 insertions(+), 3 deletions(-)
include/libnftnl/expr.h +1 -0
index ee0784928550..74e986de6b4b 100644
@@ -196,6 +196,7 @@ enum {
 enum {
 	NFTNL_EXPR_QUOTA_BYTES	= NFTNL_EXPR_BASE,
 	NFTNL_EXPR_QUOTA_FLAGS,
+	NFTNL_EXPR_QUOTA_CONSUMED,
 };
 
 enum {
src/expr/quota.c +23 -3
index 7740b24c45f3..667e6e17c28c 100644
@@ -22,6 +22,7 @@
 
 struct nftnl_expr_quota {
 	uint64_t	bytes;
+	uint64_t	consumed;
 	uint32_t	flags;
 };
 
@@ -34,6 +35,9 @@ static int nftnl_expr_quota_set(struct nftnl_expr *e, uint16_t type,
 	case NFTNL_EXPR_QUOTA_BYTES:
 		quota->bytes = *((uint64_t *)data);
 		break;
+	case NFTNL_EXPR_QUOTA_CONSUMED:
+		quota->consumed = *((uint64_t *)data);
+		break;
 	case NFTNL_EXPR_QUOTA_FLAGS:
 		quota->flags = *((uint32_t *)data);
 		break;
@@ -52,6 +56,9 @@ static const void *nftnl_expr_quota_get(const struct nftnl_expr *e,
 	case NFTNL_EXPR_QUOTA_BYTES:
 		*data_len = sizeof(quota->bytes);
 		return &quota->bytes;
+	case NFTNL_EXPR_QUOTA_CONSUMED:
+		*data_len = sizeof(quota->consumed);
+		return &quota->consumed;
 	case NFTNL_EXPR_QUOTA_FLAGS:
 		*data_len = sizeof(quota->flags);
 		return &quota->flags;
@@ -69,6 +76,7 @@ static int nftnl_expr_quota_cb(const struct nlattr *attr, void *data)
 
 	switch(type) {
 	case NFTA_QUOTA_BYTES:
+	case NFTA_QUOTA_CONSUMED:
 		if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0)
 			abi_breakage();
 		break;
@@ -89,6 +97,8 @@ nftnl_expr_quota_build(struct nlmsghdr *nlh, const struct nftnl_expr *e)
 
 	if (e->flags & (1 << NFTNL_EXPR_QUOTA_BYTES))
 		mnl_attr_put_u64(nlh, NFTA_QUOTA_BYTES, htobe64(quota->bytes));
+	if (e->flags & (1 << NFTNL_EXPR_QUOTA_CONSUMED))
+		mnl_attr_put_u64(nlh, NFTA_QUOTA_CONSUMED, htobe64(quota->consumed));
 	if (e->flags & (1 << NFTNL_EXPR_QUOTA_FLAGS))
 		mnl_attr_put_u32(nlh, NFTA_QUOTA_FLAGS, htonl(quota->flags));
 }
@@ -106,6 +116,10 @@ nftnl_expr_quota_parse(struct nftnl_expr *e, struct nlattr *attr)
 		quota->bytes = be64toh(mnl_attr_get_u64(tb[NFTA_QUOTA_BYTES]));
 		e->flags |= (1 << NFTNL_EXPR_QUOTA_BYTES);
 	}
+	if (tb[NFTA_QUOTA_CONSUMED]) {
+		quota->consumed = be64toh(mnl_attr_get_u64(tb[NFTA_QUOTA_CONSUMED]));
+		e->flags |= (1 << NFTNL_EXPR_QUOTA_CONSUMED);
+	}
 	if (tb[NFTA_QUOTA_FLAGS]) {
 		quota->flags = ntohl(mnl_attr_get_u32(tb[NFTA_QUOTA_FLAGS]));
 		e->flags |= (1 << NFTNL_EXPR_QUOTA_FLAGS);
@@ -119,12 +133,15 @@ nftnl_expr_quota_json_parse(struct nftnl_expr *e, json_t *root,
 				 struct nftnl_parse_err *err)
 {
 #ifdef JSON_PARSING
-	uint64_t bytes;
+	uint64_t bytes, consumed;
 	uint32_t flags;
 
 	if (nftnl_jansson_parse_val(root, "bytes", NFTNL_TYPE_U64, &bytes,
 				  err) == 0)
 		nftnl_expr_set_u64(e, NFTNL_EXPR_QUOTA_BYTES, bytes);
+	if (nftnl_jansson_parse_val(root, "consumed", NFTNL_TYPE_U64, &consumed,
+				  err) == 0)
+		nftnl_expr_set_u64(e, NFTNL_EXPR_QUOTA_CONSUMED, consumed);
 	if (nftnl_jansson_parse_val(root, "flags", NFTNL_TYPE_U32, &flags,
 				  err) == 0)
 		nftnl_expr_set_u32(e, NFTNL_EXPR_QUOTA_FLAGS, flags);
@@ -144,6 +161,8 @@ static int nftnl_expr_quota_export(char *buf, size_t size,
 
 	if (e->flags & (1 << NFTNL_EXPR_QUOTA_BYTES))
 		nftnl_buf_u64(&b, type, quota->bytes, BYTES);
+	if (e->flags & (1 << NFTNL_EXPR_QUOTA_CONSUMED))
+		nftnl_buf_u64(&b, type, quota->consumed, CONSUMED);
 	if (e->flags & (1 << NFTNL_EXPR_QUOTA_FLAGS))
 		nftnl_buf_u32(&b, type, quota->flags, FLAGS);
 
@@ -155,8 +174,9 @@ static int nftnl_expr_quota_snprintf_default(char *buf, size_t len,
 {
 	struct nftnl_expr_quota *quota = nftnl_expr_data(e);
 
-	return snprintf(buf, len, "bytes %"PRIu64" flags %u ",
-			quota->bytes, quota->flags);
+	return snprintf(buf, len,
+			"bytes %"PRIu64" consumed %"PRIu64" flags %u ",
+			quota->bytes, quota->consumed, quota->flags);
 }
 
 static int nftnl_expr_quota_snprintf(char *buf, size_t len, uint32_t type,
Signature · 1 line
2.1.4