Skip to contentSkip to conversation

examples: selective rule dumping

PATCH In review jalvarez 2 participants 8 messages Jul 20, 2016 – Jul 22, 2016 netfilter

lore
Share & tools
lore

jalvarez

author
#0 lore raw

PATCH 1/1 examples: selective rule dumping

Add example (based on nft-get-rule.c) to demonstrate selective rule dumping when table and / or chain attributes are set in a rule dump request. Used to test the changes made in "[PATCH nf-next] netfilter: nf_tables: allow to filter out rules by table and chain" (http://marc.info/?t=146237901200004&r=1&w=2).

--

examples/Makefile.am +4 -0
index e002d36..73450c2 100644
@@ -13,6 +13,7 @@ check_PROGRAMS = nft-table-add        \
          nft-rule-parse-add    \
          nft-rule-del        \
          nft-rule-get        \
+         nft-rule-selective-get    \
          nft-events        \
          nft-set-add        \
          nft-set-parse-add    \
@@ -40,6 +41,9 @@ nft_table_del_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
 nft_table_get_SOURCES = nft-table-get.c
 nft_table_get_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
 
+nft_rule_selective_get_SOURCES = nft-rule-selective-get.c
+nft_rule_selective_get_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
+
 nft_chain_add_SOURCES = nft-chain-add.c
 nft_chain_add_LDADD = ../src/libnftnl.la ${LIBMNL_LIBS}
 
examples/nft-rule-selective-get.c new file +164 -0

164 lines not on this page · raw message

#1 lore raw
Quoted 13 lines · jalvarez

Add example (based on nft-get-rule.c) to demonstrate selective rule dumping when table and / or chain attributes are set in a rule dump request. Used to test the changes made in "[PATCH nf-next] netfilter: nf_tables:
allow to filter out rules by table and chain"
(http://marc.info/?t=146237901200004&r=1&w=2).

--

examples/Makefile.am +0 -0
index e002d36..73450c2 100644

examples/Makefile.am check_PROGRAMS = nft-table-add \ · line 16

Quoted 4 lines
examples/Makefile.am +1 -0
@@ -13,6 +13,7 @@ check_PROGRAMS = nft-table-add        \
          nft-rule-parse-add    \
          nft-rule-del        \
          nft-rule-get        \
+         nft-rule-selective-get    \

Would you rework this to integrate these changes into nft-rule-get? I'd suggest you make the invocation look like this below.

        Usage: nft-rule-get <family> [<table> <chain>] [<xml|json>]

So table and chain must be specified at the same time to keep it simple, it is less flexible than what we actually support but I think this is fine for an example.

Thanks.

author
#2 lore raw
Quoted 21 lines · Pablo Neira Ayuso
Quoted 13 lines · jalvarez

Add example (based on nft-get-rule.c) to demonstrate selective rule dumping when table and / or chain attributes are set in a rule dump request. Used to test the changes made in "[PATCH nf-next] netfilter: nf_tables:
allow to filter out rules by table and chain"
(http://marc.info/?t=146237901200004&r=1&w=2).

--

examples/Makefile.am +0 -0
index e002d36..73450c2 100644

examples/Makefile.am check_PROGRAMS = nft-table-add \ · line 16

Quoted 4 lines
examples/Makefile.am +1 -0
@@ -13,6 +13,7 @@ check_PROGRAMS = nft-table-add        \
           nft-rule-parse-add    \
           nft-rule-del        \
           nft-rule-get        \
+         nft-rule-selective-get    \

Would you rework this to integrate these changes into nft-rule-get? I'd suggest you make the invocation look like this below.

Quoted 7 lines
         Usage: nft-rule-get <family> [<table> <chain>] [<xml|json>]

So table and chain must be specified at the same time to keep it simple, it is less flexible than what we actually support but I think this is fine for an example.

Thanks.

Here are the changes then.
I didn't put it in nft-rule-get.c at first to avoid breaking the expected behavior of the example.

--

examples/nft-rule-get.c +46 -6
index 54bee73..4a491af 100644
@@ -46,17 +46,50 @@ err:
     return MNL_CB_OK;
 }
 
+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;
+
+    r = nftnl_rule_alloc();
+    if (r == NULL) {
+        perror("OOM");
+        exit(EXIT_FAILURE);
+    }
+
+    if (table != NULL)
+        nftnl_rule_set(r, NFTNL_RULE_TABLE, table);
+
+    if (chain != NULL)
+        nftnl_rule_set(r, NFTNL_RULE_CHAIN, chain);
+
+    nftnl_rule_set_u32(r, NFTNL_RULE_FAMILY, family);
+
+    if (handle != NULL) {
+        handle_num = atoll(handle);
+        nftnl_rule_set_u64(r, NFTNL_RULE_POSITION, handle_num);
+    }
+
+    return r;
+}
+
+
+
 int main(int argc, char *argv[])
 {
     struct mnl_socket *nl;
     char buf[MNL_SOCKET_BUFFER_SIZE];
     struct nlmsghdr *nlh;
+    const char *table = NULL, *chain = NULL;
     uint32_t portid, seq, type = NFTNL_OUTPUT_DEFAULT;
-    struct nftnl_rule *t = NULL;
+    struct nftnl_rule *t, *r = NULL;
     int ret, family;
 
-    if (argc < 2 || argc > 3) {
-        fprintf(stderr, "Usage: %s <family> [xml|json]\n",
+    if (argc < 2 || argc > 5) {
+        fprintf(stderr, "Usage: %s <family> [<table> <chain>] [xml|json]\n",
             argv[0]);
         exit(EXIT_FAILURE);
     }
@@ -76,13 +109,20 @@ int main(int argc, char *argv[])
         exit(EXIT_FAILURE);
     }
 
-    if (argc == 3) {
-        if (strcmp(argv[2], "xml") == 0)
+    /* [xml|json] specified */
+    if (argc == 3 || argc == 5) {
+        if (strcmp(argv[argc - 1], "xml") == 0)
             type = NFTNL_OUTPUT_XML;
-        else if (strcmp(argv[2], "json") == 0)
+        else if (strcmp(argv[argc - 1], "json") == 0)
             type = NFTNL_OUTPUT_JSON;
     }
 
+    /* at least [<table> <chain>] specified */
+    if (argc >= 4) {
+        table = argv[2];
+        chain = argv[3];
+    }
+
     /* XXX requires table, chain and handle attributes for selective 

get */

     t = nftnl_rule_alloc();

@@ -95,6 +135,10 @@ int main(int argc, char *argv[])
nlh = nftnl_rule_nlmsg_build_hdr(buf, NFT_MSG_GETRULE, family,
NLM_F_DUMP, seq);

+ r = setup_rule(family, table, chain, NULL);
+ nftnl_rule_nlmsg_build_payload(nlh, r);
+
+

     nl = mnl_socket_open(NETLINK_NETFILTER);
     if (nl == NULL) {
         perror("mnl_socket_open");
#3 lore raw
Quoted 3 lines · jalvarez

Here are the changes then.
I didn't put it in nft-rule-get.c at first to avoid breaking the expected behavior of the example.

Thanks.

Problem with this patch: You MUA seems to mangle it, so both patch -p1 < patch and git am tell me that your patch is corrupted. I can also see indents are converted to spaces instead of tabs.

I suggest you use git-send-email to deliver patches to the mailing list to resolve this.

#4 lore raw
---
 examples/nft-rule-get.c | 56 +++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 50 insertions(+), 6 deletions(-)
examples/nft-rule-get.c +50 -6
index 54bee73..4a491af 100644
@@ -46,17 +46,50 @@ err:
 	return MNL_CB_OK;
 }
 
+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;
+
+	r = nftnl_rule_alloc();
+	if (r == NULL) {
+		perror("OOM");
+		exit(EXIT_FAILURE);
+	}
+
+	if (table != NULL)
+		nftnl_rule_set(r, NFTNL_RULE_TABLE, table);
+
+	if (chain != NULL)
+		nftnl_rule_set(r, NFTNL_RULE_CHAIN, chain);
+
+	nftnl_rule_set_u32(r, NFTNL_RULE_FAMILY, family);
+
+	if (handle != NULL) {
+		handle_num = atoll(handle);
+		nftnl_rule_set_u64(r, NFTNL_RULE_POSITION, handle_num);
+	}
+
+	return r;
+}
+
+
+
 int main(int argc, char *argv[])
 {
 	struct mnl_socket *nl;
 	char buf[MNL_SOCKET_BUFFER_SIZE];
 	struct nlmsghdr *nlh;
+	const char *table = NULL, *chain = NULL;
 	uint32_t portid, seq, type = NFTNL_OUTPUT_DEFAULT;
-	struct nftnl_rule *t = NULL;
+	struct nftnl_rule *t, *r = NULL;
 	int ret, family;
 
-	if (argc < 2 || argc > 3) {
-		fprintf(stderr, "Usage: %s <family> [xml|json]\n",
+	if (argc < 2 || argc > 5) {
+		fprintf(stderr, "Usage: %s <family> [<table> <chain>] [xml|json]\n",
 			argv[0]);
 		exit(EXIT_FAILURE);
 	}
@@ -76,13 +109,20 @@ int main(int argc, char *argv[])
 		exit(EXIT_FAILURE);
 	}
 
-	if (argc == 3) {
-		if (strcmp(argv[2], "xml") == 0)
+	/* [xml|json] specified */
+	if (argc == 3 || argc == 5) {
+		if (strcmp(argv[argc - 1], "xml") == 0)
 			type = NFTNL_OUTPUT_XML;
-		else if (strcmp(argv[2], "json") == 0)
+		else if (strcmp(argv[argc - 1], "json") == 0)
 			type = NFTNL_OUTPUT_JSON;
 	}
 
+	/* at least [<table> <chain>] specified */
+	if (argc >= 4) {
+		table = argv[2];
+		chain = argv[3];
+	}
+
 	/* XXX requires table, chain and handle attributes for selective get */
 
 	t = nftnl_rule_alloc();
@@ -95,6 +135,10 @@ int main(int argc, char *argv[])
 	nlh = nftnl_rule_nlmsg_build_hdr(buf, NFT_MSG_GETRULE, family,
 					NLM_F_DUMP, seq);
 
+	r = setup_rule(family, table, chain, NULL);
+	nftnl_rule_nlmsg_build_payload(nlh, r);
+
+
 	nl = mnl_socket_open(NETLINK_NETFILTER);
 	if (nl == NULL) {
 		perror("mnl_socket_open");
Signature · 1 line
2.7.4
#5 lore raw

Please, send me your Signed-off-by: and a couple of lines with a description.

No need to resend this patch, I'll amend this myself. But please include it next time. Thanks.

author
#6 lore raw
Quoted 5 lines · Pablo Neira Ayuso

Please, send me your Signed-off-by: and a couple of lines with a description.

No need to resend this patch, I'll amend this myself. But please include it next time. Thanks.

I apologize, I forgot to add it this time. Here are the missing lines you requested.

Improve nft-rule-get example to demonstrate selective rule dumping when table and / or chain attributes are set in a rule dump request. Usage is now as follows : nft-rule-get <family> [<table> <chain>] [<xml|json>].

#7 lore raw
Quoted 7 lines · jalvarez
Quoted 5 lines · Pablo Neira Ayuso

Please, send me your Signed-off-by: and a couple of lines with a description.

No need to resend this patch, I'll amend this myself. But please include it next time. Thanks.

Quoted 8 lines

I apologize, I forgot to add it this time. Here are the missing lines you requested.

Improve nft-rule-get example to demonstrate selective rule dumping when table and / or chain attributes are set in a rule dump request. Usage is now as follows : nft-rule-get <family> [<table> <chain>] [<xml|json>].

OK, applied, thanks.

Just for the record, I have also cleaned up a bit the code btw.