Skip to contentSkip to conversation

iprule: avoid printing extra space after gateway for nat action

PATCH In review Thomas Haller 2 participants 5 messages Apr 23, 2019 – Apr 24, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/4 iprule: avoid printing extra space after gateway for nat action

For all other actions we avoid the trailing space, so do it here as well.

---
 ip/iprule.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ip/iprule.c +1 -1
index 83aef38e..2479c3ab 100644
@@ -443,7 +443,7 @@ int print_rule(struct nlmsghdr *n, void *arg)
 			gateway = format_host_rta(frh->family, tb[RTA_GATEWAY]);
 
 			print_string(PRINT_ANY, "nat_gateway",
-				     "map-to %s ", gateway);
+				     "map-to %s", gateway);
 		} else {
 			print_null(PRINT_ANY, "masquerade", "masquerade", NULL);
 		}
Signature · 1 line
2.20.1
#1 lore raw

PATCH 2/4 iprule: avoid trailing space in print_rule() after printing protocol

It seems print_rule() tries to avoid a trailing space at the end of the line. At least, when printing details about the actions, they no longer append the space. Probably expecting to be the last attribute that will be printed.

Don't let the protocol add the trailing space. The space at the end of the line should be printed consistently (or not).

---
 ip/iprule.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ip/iprule.c +1 -1
index 2479c3ab..ce6b7cd3 100644
@@ -468,7 +468,7 @@ int print_rule(struct nlmsghdr *n, void *arg)
 		__u8 protocol = rta_getattr_u8(tb[FRA_PROTOCOL]);
 
 		if ((protocol && protocol != RTPROT_KERNEL) || show_details > 0) {
-			print_string(PRINT_ANY, "protocol", " proto %s ",
+			print_string(PRINT_ANY, "protocol", " proto %s",
 				     rtnl_rtprot_n2a(protocol, b1, sizeof(b1)));
 		}
 	}
Signature · 1 line
2.20.1
#2 lore raw

PATCH 3/4 iprule: refactor print_rule() to use leading space before printing attribute

In reply to Thomas Haller

When printing the actions, we avoid adding the trailing space after the attribute. Possibly because we expect the action to be the last output on the line and not end with a space.

But for FR_ACT_TO_TBL nothing is printed. That means, we add double spaces if a protocol is printed as well:

    # ip rule add priority 10 protocol 10 type 1

will be printed as

10: from all lookup 1 proto mrt

The only visible effect of the patch is to avoid the double-space and avoid a trailing space if the action is FR_ACT_TO_TBL.

---
 ip/iprule.c | 84 ++++++++++++++++++++++++++---------------------------
 1 file changed, 41 insertions(+), 43 deletions(-)
ip/iprule.c +41 -43
index ce6b7cd3..b00ca7d1 100644
@@ -273,34 +273,30 @@ int print_rule(struct nlmsghdr *n, void *arg)
 		print_color_string(PRINT_ANY, ifa_family_color(frh->family),
 				   "src", "%s", src);
 		if (frh->src_len != host_len)
-			print_uint(PRINT_ANY, "srclen", "/%u ", frh->src_len);
-		else
-			print_string(PRINT_FP, NULL, " ", NULL);
+			print_uint(PRINT_ANY, "srclen", "/%u", frh->src_len);
 	} else if (frh->src_len) {
 		print_string(PRINT_ANY, "src", "from %s", "0");
-		print_uint(PRINT_ANY, "srclen", "/%u ", frh->src_len);
+		print_uint(PRINT_ANY, "srclen", "/%u", frh->src_len);
 	} else {
-		print_string(PRINT_ANY, "src", "from %s ", "all");
+		print_string(PRINT_ANY, "src", "from %s", "all");
 	}
 
 	if (tb[FRA_DST]) {
 		const char *dst = rt_addr_n2a_rta(frh->family, tb[FRA_DST]);
 
-		print_string(PRINT_FP, NULL, "to ", NULL);
+		print_string(PRINT_FP, NULL, " to ", NULL);
 		print_color_string(PRINT_ANY, ifa_family_color(frh->family),
 				   "dst", "%s", dst);
 		if (frh->dst_len != host_len)
-			print_uint(PRINT_ANY, "dstlen", "/%u ", frh->dst_len);
-		else
-			print_string(PRINT_FP, NULL, " ", NULL);
+			print_uint(PRINT_ANY, "dstlen", "/%u", frh->dst_len);
 	} else if (frh->dst_len) {
-		print_string(PRINT_ANY, "dst", "to %s", "0");
-		print_uint(PRINT_ANY, "dstlen", "/%u ", frh->dst_len);
+		print_string(PRINT_ANY, "dst", " to %s", "0");
+		print_uint(PRINT_ANY, "dstlen", "/%u", frh->dst_len);
 	}
 
 	if (frh->tos) {
 		print_string(PRINT_ANY, "tos",
-			     "tos %s ",
+			     " tos %s",
 			     rtnl_dsfield_n2a(frh->tos, b1, sizeof(b1)));
 	}
 
@@ -312,34 +308,34 @@ int print_rule(struct nlmsghdr *n, void *arg)
 
 		if (tb[FRA_FWMASK] &&
 		    (mask = rta_getattr_u32(tb[FRA_FWMASK])) != 0xFFFFFFFF) {
-			print_0xhex(PRINT_ANY, "fwmark", "fwmark %#llx", mark);
-			print_0xhex(PRINT_ANY, "fwmask", "/%#llx ", mask);
+			print_0xhex(PRINT_ANY, "fwmark", " fwmark %#llx", mark);
+			print_0xhex(PRINT_ANY, "fwmask", "/%#llx", mask);
 		} else {
-			print_0xhex(PRINT_ANY, "fwmark", "fwmark %#llx ", mark);
+			print_0xhex(PRINT_ANY, "fwmark", " fwmark %#llx", mark);
 		}
 	}
 
 	if (tb[FRA_IFNAME]) {
 		if (!is_json_context())
-			fprintf(fp, "iif ");
+			fprintf(fp, " iif ");
 		print_color_string(PRINT_ANY, COLOR_IFNAME,
-				   "iif", "%s ",
+				   "iif", "%s",
 				   rta_getattr_str(tb[FRA_IFNAME]));
 
 		if (frh->flags & FIB_RULE_IIF_DETACHED)
-			print_null(PRINT_ANY, "iif_detached", "[detached] ",
+			print_null(PRINT_ANY, "iif_detached", " [detached]",
 				   NULL);
 	}
 
 	if (tb[FRA_OIFNAME]) {
 		if (!is_json_context())
-			fprintf(fp, "oif ");
+			fprintf(fp, " oif ");
 
-		print_color_string(PRINT_ANY, COLOR_IFNAME, "oif", "%s ",
+		print_color_string(PRINT_ANY, COLOR_IFNAME, "oif", "%s",
 				   rta_getattr_str(tb[FRA_OIFNAME]));
 
 		if (frh->flags & FIB_RULE_OIF_DETACHED)
-			print_null(PRINT_ANY, "oif_detached", "[detached] ",
+			print_null(PRINT_ANY, "oif_detached", " [detached]",
 				   NULL);
 	}
 
@@ -348,19 +344,19 @@ int print_rule(struct nlmsghdr *n, void *arg)
 
 		if (mdev)
 			print_null(PRINT_ANY, "l3mdev",
-				   "lookup [l3mdev-table] ", NULL);
+				   " lookup [l3mdev-table]", NULL);
 	}
 
 	if (tb[FRA_UID_RANGE]) {
 		struct fib_rule_uid_range *r = RTA_DATA(tb[FRA_UID_RANGE]);
 
-		print_uint(PRINT_ANY, "uid_start", "uidrange %u", r->start);
-		print_uint(PRINT_ANY, "uid_end", "-%u ", r->end);
+		print_uint(PRINT_ANY, "uid_start", " uidrange %u", r->start);
+		print_uint(PRINT_ANY, "uid_end", "-%u", r->end);
 	}
 
 	if (tb[FRA_IP_PROTO]) {
 		SPRINT_BUF(pbuf);
-		print_string(PRINT_ANY, "ipproto", "ipproto %s ",
+		print_string(PRINT_ANY, "ipproto", " ipproto %s",
 			     inet_proto_n2a(rta_getattr_u8(tb[FRA_IP_PROTO]),
 					    pbuf, sizeof(pbuf)));
 	}
@@ -369,11 +365,11 @@ int print_rule(struct nlmsghdr *n, void *arg)
 		struct fib_rule_port_range *r = RTA_DATA(tb[FRA_SPORT_RANGE]);
 
 		if (r->start == r->end) {
-			print_uint(PRINT_ANY, "sport", "sport %u ", r->start);
+			print_uint(PRINT_ANY, "sport", " sport %u", r->start);
 		} else {
-			print_uint(PRINT_ANY, "sport_start", "sport %u",
+			print_uint(PRINT_ANY, "sport_start", " sport %u",
 				   r->start);
-			print_uint(PRINT_ANY, "sport_end", "-%u ", r->end);
+			print_uint(PRINT_ANY, "sport_end", "-%u", r->end);
 		}
 	}
 
@@ -381,24 +377,24 @@ int print_rule(struct nlmsghdr *n, void *arg)
 		struct fib_rule_port_range *r = RTA_DATA(tb[FRA_DPORT_RANGE]);
 
 		if (r->start == r->end) {
-			print_uint(PRINT_ANY, "dport", "dport %u ", r->start);
+			print_uint(PRINT_ANY, "dport", " dport %u", r->start);
 		} else {
-			print_uint(PRINT_ANY, "dport_start", "dport %u",
+			print_uint(PRINT_ANY, "dport_start", " dport %u",
 				   r->start);
-			print_uint(PRINT_ANY, "dport_end", "-%u ", r->end);
+			print_uint(PRINT_ANY, "dport_end", "-%u", r->end);
 		}
 	}
 
 	if (tb[FRA_TUN_ID]) {
 		__u64 tun_id = ntohll(rta_getattr_u64(tb[FRA_TUN_ID]));
 
-		print_u64(PRINT_ANY, "tun_id", "tun_id %llu ", tun_id);
+		print_u64(PRINT_ANY, "tun_id", " tun_id %llu", tun_id);
 	}
 
 	table = frh_get_table(frh, tb);
 	if (table) {
 		print_string(PRINT_ANY, "table",
-			     "lookup %s ",
+			     " lookup %s",
 			     rtnl_rttable_n2a(table, b1, sizeof(b1)));
 
 		if (tb[FRA_SUPPRESS_PREFIXLEN]) {
@@ -406,7 +402,7 @@ int print_rule(struct nlmsghdr *n, void *arg)
 
 			if (pl != -1)
 				print_int(PRINT_ANY, "suppress_prefixlen",
-					  "suppress_prefixlength %d ", pl);
+					  " suppress_prefixlength %d", pl);
 		}
 
 		if (tb[FRA_SUPPRESS_IFGROUP]) {
@@ -417,7 +413,7 @@ int print_rule(struct nlmsghdr *n, void *arg)
 					= rtnl_group_n2a(group, b1, sizeof(b1));
 
 				print_string(PRINT_ANY, "suppress_ifgroup",
-					     "suppress_ifgroup %s ", grname);
+					     " suppress_ifgroup %s", grname);
 			}
 		}
 	}
@@ -429,10 +425,12 @@ int print_rule(struct nlmsghdr *n, void *arg)
 		to &= 0xFFFF;
 		if (from)
 			print_string(PRINT_ANY,
-				     "flow_from", "realms %s/",
+				     "flow_from", " realms %s/",
 				     rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
+		else
+			print_string(PRINT_FP, NULL, " ", NULL);
 
-		print_string(PRINT_ANY, "flow_to", "%s ",
+		print_string(PRINT_ANY, "flow_to", "%s",
 			     rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
 	}
 
@@ -443,24 +441,24 @@ int print_rule(struct nlmsghdr *n, void *arg)
 			gateway = format_host_rta(frh->family, tb[RTA_GATEWAY]);
 
 			print_string(PRINT_ANY, "nat_gateway",
-				     "map-to %s", gateway);
+				     " map-to %s", gateway);
 		} else {
-			print_null(PRINT_ANY, "masquerade", "masquerade", NULL);
+			print_null(PRINT_ANY, "masquerade", " masquerade", NULL);
 		}
 	} else if (frh->action == FR_ACT_GOTO) {
 		if (tb[FRA_GOTO])
-			print_uint(PRINT_ANY, "goto", "goto %u",
+			print_uint(PRINT_ANY, "goto", " goto %u",
 				   rta_getattr_u32(tb[FRA_GOTO]));
 		else
-			print_string(PRINT_ANY, "goto", "goto %s", "none");
+			print_string(PRINT_ANY, "goto", " goto %s", "none");
 
 		if (frh->flags & FIB_RULE_UNRESOLVED)
 			print_null(PRINT_ANY, "unresolved",
 				   " [unresolved]", NULL);
 	} else if (frh->action == FR_ACT_NOP) {
-		print_null(PRINT_ANY, "nop", "nop", NULL);
+		print_null(PRINT_ANY, "nop", " nop", NULL);
 	} else if (frh->action != FR_ACT_TO_TBL) {
-		print_string(PRINT_ANY, "action", "%s",
+		print_string(PRINT_ANY, "action", " %s",
 			     rtnl_rtntype_n2a(frh->action, b1, sizeof(b1)));
 	}
 
Signature · 1 line
2.20.1
#3 lore raw

PATCH 4/4 iprule: always print realms keyword for rule

In reply to Thomas Haller
    # rule add priority 10 realms 1/0xF
    # rule add priority 10 realms 0/0xF
    # ip rule
    10:     from all lookup main 15
    10:     from all lookup main realms 1/15

The previous behavior was there since the beginning.

---
 ip/iprule.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
ip/iprule.c +1 -1
index b00ca7d1..9f5d998b 100644
@@ -428,7 +428,7 @@ int print_rule(struct nlmsghdr *n, void *arg)
 				     "flow_from", " realms %s/",
 				     rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
 		else
-			print_string(PRINT_FP, NULL, " ", NULL);
+			print_string(PRINT_FP, NULL, " realms ", NULL);
 
 		print_string(PRINT_ANY, "flow_to", "%s",
 			     rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
Signature · 1 line
2.20.1