Skip to contentSkip to conversation

src: add ruleset generation class

PATCH New Pablo Neira Ayuso 1 participant 1 message Sep 12, 2014 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/1 src: add ruleset generation class

The generation object currently only contains the uint32_t that indicates the generation ID. I could have just add the API to return the uint32_t ID instead, but I think this API is easier to extend without adding new APIs.

We can probably include meaningful statistics in the generation message in the future without much hassle.

This patch also extends examples/nft-events.c.

---
 examples/nft-events.c               |   28 +++++
 include/libnftnl/gen.h              |   51 +++++++++
 include/linux/netfilter/nf_tables.h |   19 +++-
 src/Makefile.am                     |    1 +
 src/gen.c                           |  199 +++++++++++++++++++++++++++++++++++
 src/libnftnl.map                    |   14 +++
 6 files changed, 310 insertions(+), 2 deletions(-)
 create mode 100644 include/libnftnl/gen.h
 create mode 100644 src/gen.c
examples/nft-events.c +28 -0
index fb18e55..ef860d3 100644
@@ -22,6 +22,7 @@
 #include <libnftnl/chain.h>
 #include <libnftnl/rule.h>
 #include <libnftnl/set.h>
+#include <libnftnl/gen.h>
 #include <libnftnl/common.h>
 
 static uint32_t event2flag(uint32_t event)
@@ -32,6 +33,7 @@ static uint32_t event2flag(uint32_t event)
 	case NFT_MSG_NEWRULE:
 	case NFT_MSG_NEWSET:
 	case NFT_MSG_NEWSETELEM:
+	case NFT_MSG_NEWGEN:
 		return NFT_OF_EVENT_NEW;
 	case NFT_MSG_DELTABLE:
 	case NFT_MSG_DELCHAIN:
@@ -165,6 +167,29 @@ err:
 	return MNL_CB_OK;
 }
 
+static int gen_cb(const struct nlmsghdr *nlh, int event, int type)
+{
+	struct nft_gen *gen;
+
+	gen = nft_gen_alloc();
+	if (gen == NULL) {
+		perror("OOM");
+		goto err;
+	}
+
+	if (nft_gen_nlmsg_parse(nlh, gen) < 0) {
+		perror("nft_gen_parse");
+		goto err_free;
+	}
+
+	nft_gen_fprintf(stdout, gen, type, event2flag(event));
+	fprintf(stdout, "\n");
+err_free:
+	nft_gen_free(gen);
+err:
+	return MNL_CB_OK;
+}
+
 static int events_cb(const struct nlmsghdr *nlh, void *data)
 {
 	int ret = MNL_CB_OK;
@@ -192,6 +217,9 @@ static int events_cb(const struct nlmsghdr *nlh, void *data)
 	case NFT_MSG_DELSETELEM:
 		ret = setelem_cb(nlh, event, type);
 		break;
+	case NFT_MSG_NEWGEN:
+		ret = gen_cb(nlh, event, type);
+		break;
 	}
 
 	return ret;
include/libnftnl/gen.h new file +51 -0

51 lines not on this page · raw message

include/linux/netfilter/nf_tables.h +17 -2
index 36c4ca2..b72ccfe 100644
@@ -51,6 +51,8 @@ enum nft_verdicts {
  * @NFT_MSG_NEWSETELEM: create a new set element (enum nft_set_elem_attributes)
  * @NFT_MSG_GETSETELEM: get a set element (enum nft_set_elem_attributes)
  * @NFT_MSG_DELSETELEM: delete a set element (enum nft_set_elem_attributes)
+ * @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)
  */
 enum nf_tables_msg_types {
 	NFT_MSG_NEWTABLE,
@@ -68,6 +70,8 @@ enum nf_tables_msg_types {
 	NFT_MSG_NEWSETELEM,
 	NFT_MSG_GETSETELEM,
 	NFT_MSG_DELSETELEM,
+	NFT_MSG_NEWGEN,
+	NFT_MSG_GETGEN,
 	NFT_MSG_MAX,
 };
 
@@ -785,7 +789,7 @@ enum nft_nat_types {
  * @NFTA_NAT_REG_ADDR_MAX: source register of address range end (NLA_U32: nft_registers)
  * @NFTA_NAT_REG_PROTO_MIN: source register of proto range start (NLA_U32: nft_registers)
  * @NFTA_NAT_REG_PROTO_MAX: source register of proto range end (NLA_U32: nft_registers)
- * @NFTA_NAT_FLAGS: additional NAT configuration (NF_NAT_RANGE_*) (NLA_U32)
+ * @NFTA_NAT_FLAGS: NAT flags (see NF_NAT_RANGE_* in linux/netfilter/nf_nat.h) (NLA_U32)
  */
 enum nft_nat_attributes {
 	NFTA_NAT_UNSPEC,
@@ -803,7 +807,7 @@ enum nft_nat_attributes {
 /**
  * enum nft_masq_attributes - nf_tables masquerade expression attributes
  *
- * @NFTA_MASQ_FLAGS: additional masquerade configuration (NF_NAT_RANGE_*) (NLA_U32)
+ * @NFTA_MASQ_FLAGS: NAT flags (see NF_NAT_RANGE_* in linux/netfilter/nf_nat.h) (NLA_U32)
  */
 enum nft_masq_attributes {
 	NFTA_MASQ_UNSPEC,
@@ -812,5 +816,16 @@ enum nft_masq_attributes {
 };
 #define NFTA_MASQ_MAX		(__NFTA_MASQ_MAX - 1)
 
+/**
+ * enum nft_gen_attributes - nf_tables ruleset generation attributes
+ *
+ * @NFTA_GEN_ID: Ruleset generation ID (NLA_U32)
+ */
+enum nft_gen_attributes {
+	NFTA_GEN_UNSPEC,
+	NFTA_GEN_ID,
+	__NFTA_GEN_MAX
+};
+#define NFTA_GEN_MAX		(__NFTA_GEN_MAX - 1)
 
 #endif /* _LINUX_NF_TABLES_H */
src/Makefile.am +1 -0
index d57ff96..65bc82a 100644
@@ -7,6 +7,7 @@ libnftnl_la_LDFLAGS = -Wl,--version-script=$(srcdir)/libnftnl.map	\
 
 libnftnl_la_SOURCES = utils.c		\
 		      common.c		\
+		      gen.c		\
 		      table.c		\
 		      chain.c		\
 		      rule.c		\
src/gen.c new file +199 -0

199 lines not on this page · raw message

src/libnftnl.map +14 -0
index d8dcf8e..be7b998 100644
@@ -212,4 +212,18 @@ LIBNFTNL_1.2 {
   nft_batch_is_supported;
   nft_batch_begin;
   nft_batch_end;
+
+  nft_gen_alloc;
+  nft_gen_free;
+  nft_gen_attr_is_set;
+  nft_gen_attr_unset;
+  nft_gen_attr_set_data;
+  nft_gen_attr_set;
+  nft_gen_attr_set_u32;
+  nft_gen_attr_get_data;
+  nft_gen_attr_get;
+  nft_gen_attr_get_u32;
+  nft_gen_nlmsg_parse;
+  nft_gen_snprintf;
+  nft_gen_fprintf;
 } LIBNFTNL_1.1;
Signature · 1 line
1.7.10.4