Skip to contentSkip to conversation

Generalize nft_rule_list() (+ 2 fixes)

PATCH Applied Tomasz Bursztyka 2 participants 8 messages Aug 7, 2013 netfilter

lore
Share & tools
lore
#0 lore raw

cover Generalize nft_rule_list() (+ 2 fixes)

Hi,

Proper refactorization of nft_rule_list() so it uses nft_family_ops .print_firewall, thus enabling its usage by other family such as ARP etc...

2 small fixes: one about optimization (patch 1), and one about the output (patch 3)

Tomasz Bursztyka (3):
nft: Remove useless test on rulenum in nft_rule_list()
nft: Generalize nft_rule_list() against current family
nft: Print unknown target data only when relevant

 iptables/nft-ipv4.c   |  39 ++++++--
 iptables/nft-ipv6.c   |  39 ++++++--
 iptables/nft-shared.c | 239 +++++++++++++++++++++++++++++++++++++++++++++++
 iptables/nft-shared.h |  13 ++-
 iptables/nft.c        | 254 ++------------------------------------------------
 5 files changed, 317 insertions(+), 267 deletions(-)
Signature · 1 line
1.8.3.2
#1 lore raw

PATCH 1/3 nft: Remove useless test on rulenum in nft_rule_list()

Rulenum is already handled before the loop, making this test useless.

---
 iptables/nft.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
iptables/nft.c +2 -3
index a87d303..15c50a7 100644
@@ -2537,10 +2537,9 @@ int nft_rule_list(struct nft_handle *h, const char *chain, const char *table,
 		if (found)
 			printf("\n");
 
-		if (!rulenum) {
-			print_header(format, chain_name, policy_name[policy],
+		print_header(format, chain_name, policy_name[policy],
 				     &ctrs, basechain, refs);
-		}
+
 		__nft_rule_list(h, chain_name, table,
 				rulenum, format, print_firewall);
 
Signature · 1 line
1.8.3.2
maintainer Applied
#2 lore raw

Re: [iptables-nftables PATCH 1/3] nft: Remove useless test on rulenum in nft_rule_list()

Quoted 1 line · Tomasz Bursztyka

Rulenum is already handled before the loop, making this test useless.

This one was my fault.

Applied, thanks.

#3 lore raw

PATCH 2/3 nft: Generalize nft_rule_list() against current family

In reply to Tomasz Bursztyka

Now, firewall rule printing is done through nft_family_ops .print_firewall function. This moves generic part for ipv4 and ipv6 into nft-shared.c, and enables reusing nft_rule_list() for other family such as ARP which will be useful for arptables compatibility tool.

---
 iptables/nft-ipv4.c   |  39 ++++++--
 iptables/nft-ipv6.c   |  39 ++++++--
 iptables/nft-shared.c | 239 ++++++++++++++++++++++++++++++++++++++++++++++++
 iptables/nft-shared.h |  13 ++-
 iptables/nft.c        | 249 +-------------------------------------------------
 5 files changed, 315 insertions(+), 264 deletions(-)
iptables/nft-ipv4.c +31 -8
index 51ee422..81be9f4 100644
@@ -284,18 +284,41 @@ static void print_ipv4_addr(const struct iptables_command_state *cs,
 }
 
 
-static uint8_t nft_ipv4_print_firewall(const struct iptables_command_state *cs,
-				       const char *targname, unsigned int num,
-				       unsigned int format)
+static void nft_ipv4_print_firewall(struct nft_rule *r, unsigned int num,
+				    unsigned int format)
 {
-	print_firewall_details(cs, targname, cs->fw.ip.flags,
-			       cs->fw.ip.invflags, cs->fw.ip.proto,
-			       cs->fw.ip.iniface, cs->fw.ip.outiface,
+	struct iptables_command_state cs = {};
+	const char *targname = NULL;
+	const void *targinfo = NULL;
+	size_t target_len = 0;
+
+	nft_rule_to_iptables_command_state(r, &cs);
+
+	targname = nft_parse_target(r, &targinfo, &target_len);
+
+	print_firewall_details(&cs, targname, cs.fw.ip.flags,
+			       cs.fw.ip.invflags, cs.fw.ip.proto,
+			       cs.fw.ip.iniface, cs.fw.ip.outiface,
 			       num, format);
 
-	print_ipv4_addr(cs, format);
+	print_ipv4_addr(&cs, format);
 
-	return cs->fw.ip.flags;
+	if (format & FMT_NOTABLE)
+		fputs("  ", stdout);
+
+#ifdef IPT_F_GOTO
+	if (cs.fw.ip.flags & IPT_F_GOTO)
+		printf("[goto] ");
+#endif
+
+	if (print_matches(r, format) != 0)
+		return;
+
+	if (print_target(targname, targinfo, target_len, format) != 0)
+		return;
+
+	if (!(format & FMT_NONEWLINE))
+		fputc('\n', stdout);
 }
 
 static void nft_ipv4_post_parse(int command,
iptables/nft-ipv6.c +31 -8
index 61c660a..0214dcf 100644
@@ -194,18 +194,41 @@ static void print_ipv6_addr(const struct iptables_command_state *cs,
 	}
 }
 
-static uint8_t nft_ipv6_print_firewall(const struct iptables_command_state *cs,
-				       const char *targname, unsigned int num,
-				       unsigned int format)
+static void nft_ipv6_print_firewall(struct nft_rule *r, unsigned int num,
+				    unsigned int format)
 {
-	print_firewall_details(cs, targname, cs->fw6.ipv6.flags,
-			       cs->fw6.ipv6.invflags, cs->fw6.ipv6.proto,
-			       cs->fw6.ipv6.iniface, cs->fw6.ipv6.outiface,
+	struct iptables_command_state cs = {};
+	const char *targname = NULL;
+	const void *targinfo = NULL;
+	size_t target_len = 0;
+
+	nft_rule_to_iptables_command_state(r, &cs);
+
+	targname = nft_parse_target(r, &targinfo, &target_len);
+
+	print_firewall_details(&cs, targname, cs.fw6.ipv6.flags,
+			       cs.fw6.ipv6.invflags, cs.fw6.ipv6.proto,
+			       cs.fw6.ipv6.iniface, cs.fw6.ipv6.outiface,
 			       num, format);
 
-	print_ipv6_addr(cs, format);
+	print_ipv6_addr(&cs, format);
 
-	return cs->fw6.ipv6.flags;
+	if (format & FMT_NOTABLE)
+		fputs("  ", stdout);
+
+#ifdef IPT_F_GOTO
+	if (cs.fw6.ipv6.flags & IPT_F_GOTO)
+		printf("[goto] ");
+#endif
+
+	if (print_matches(r, format) != 0)
+		return;
+
+	if (print_target(targname, targinfo, target_len, format) != 0)
+		return;
+
+	if (!(format & FMT_NONEWLINE))
+		fputc('\n', stdout);
 }
 
 /* These are invalid numbers as upper layer protocol */
iptables/nft-shared.c +239 -0

259 lines not on this page · raw message

iptables/nft-shared.h +9 -4
index c59ab21..488ed63 100644
@@ -48,9 +48,8 @@ struct nft_family_ops {
 			      struct iptables_command_state *cs,
 			      uint32_t offset);
 	void (*parse_immediate)(struct iptables_command_state *cs);
-	uint8_t (*print_firewall)(const struct iptables_command_state *cs,
-				  const char *targname, unsigned int num,
-				  unsigned int format);
+	void (*print_firewall)(struct nft_rule *r, unsigned int num,
+			       unsigned int format);
 	void (*post_parse)(int command, struct iptables_command_state *cs,
 			   struct xtables_args *args);
 };
@@ -80,10 +79,16 @@ bool is_same_interfaces(const char *a_iniface, const char *a_outiface,
 void parse_meta(struct nft_rule_expr *e, uint8_t key, char *iniface,
 		unsigned char *iniface_mask, char *outiface,
 		unsigned char *outiface_mask, uint8_t *invflags);
-
+const char *nft_parse_target(struct nft_rule *r, const void **targinfo,
+			     size_t *target_len);
 void print_proto(uint16_t proto, int invert);
 void get_cmp_data(struct nft_rule_expr_iter *iter,
 		  void *data, size_t dlen, bool *inv);
+void nft_rule_to_iptables_command_state(struct nft_rule *r,
+					struct iptables_command_state *cs);
+int print_matches(struct nft_rule *r, int format);
+int print_target(const char *targname, const void *targinfo,
+		 size_t target_len, int format);
 void print_num(uint64_t number, unsigned int format);
 void print_firewall_details(const struct iptables_command_state *cs,
 			    const char *targname, uint8_t flags,
iptables/nft.c +5 -244

281 lines not on this page · raw message

Signature · 1 line
1.8.3.2
#4 lore raw

Re: [iptables-nftables PATCH 2/3] nft: Generalize nft_rule_list() against current family

Quoted 4 lines · Tomasz Bursztyka

Now, firewall rule printing is done through nft_family_ops .print_firewall function. This moves generic part for ipv4 and ipv6 into nft-shared.c, and enables reusing nft_rule_list() for other family such as ARP which will be useful for arptables compatibility tool.

Applied with one nitpick, thanks Tomasz.

Quoted 5 lines
iptables/nft-shared.c +0 -0
index c0ee4c8..ad3e675 100644

iptables/nft-shared.c void parse_meta(struct nft_rule_expr *e, uint8_t key, char *iniface, · line 289

Quoted 9 lines
iptables/nft-shared.c +6 -0
@@ -277,6 +281,59 @@ void parse_meta(struct nft_rule_expr *e, uint8_t key, char *iniface,
 	}
 }
 
+const char *nft_parse_target(struct nft_rule *r, const void **targinfo,
+			     size_t *target_len)
+{
+	struct nft_rule_expr_iter *iter;
+	struct nft_rule_expr *expr;
+	const char *targname;

Need targname = NULL, otherwise:

nft-shared.c: In function ‘nft_parse_target’:
nft-shared.c:289:14: warning: ‘targname’ may be used uninitialized in this function [-Wmaybe-uninitialized
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw

Re: [iptables-nftables PATCH 2/3] nft: Generalize nft_rule_list() against current family

Hi Pablo,

Quoted 11 lines
Quoted 6 lines

+const char *nft_parse_target(struct nft_rule *r, const void **targinfo,

iptables/nft-shared.c void parse_meta(struct nft_rule_expr *e, uint8_t key, char *iniface, · line 289

Quoted 5 lines
iptables/nft-shared.c +5 -0
+			     size_t *target_len)
+{
+	struct nft_rule_expr_iter *iter;
+	struct nft_rule_expr *expr;
+	const char *targname;

Need targname = NULL, otherwise:

nft-shared.c: In function ‘nft_parse_target’:
nft-shared.c:289:14: warning: ‘targname’ may be used uninitialized in this function [-Wmaybe-uninitialized

Missed this warning, thanks for finding it.

Tomasz
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#6 lore raw

PATCH 3/3 nft: Print unknown target data only when relevant

In reply to Tomasz Bursztyka

Bug is:
xtables -N test
xtables -A FORWARD -j test
xtables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination

Chain FORWARD (policy ACCEPT)

target     prot opt source               destination
test       all  --  anywhere             anywhere            [0 bytes of unknown target data]

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

Chain test (1 references)
target prot opt source destination

"[0 bytes of unknown target data]" should not be printed in this case.

---
 iptables/nft-shared.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
iptables/nft-shared.c +1 -1
index ad3e675..f5f905f 100644
@@ -549,7 +549,7 @@ int print_target(const char *targname, const void *targinfo,
 		if (target->print)
 			/* FIXME missing first parameter */
 			target->print(NULL, t, format & FMT_NUMERIC);
-	} else
+	} else if (target_len > 0)
 		printf("[%ld bytes of unknown target data] ", target_len);
 
 	free(t);
Signature · 1 line
1.8.3.2
maintainer Applied
#7 lore raw

Re: [iptables-nftables PATCH 3/3] nft: Print unknown target data only when relevant

Quoted 11 lines · Tomasz Bursztyka

Bug is:
xtables -N test
xtables -A FORWARD -j test
xtables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination

Chain FORWARD (policy ACCEPT)

target     prot opt source               destination
test       all  --  anywhere             anywhere            [0 bytes of unknown target data]
Quoted 7 lines

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

Chain test (1 references)
target prot opt source destination

"[0 bytes of unknown target data]" should not be printed in this case.

Applied, thanks.