Skip to contentSkip to conversation

ss: always prefer family as part of host condition to default family

PATCH Superseded Thayne McCombs 2 participants 4 messages Jan 31, 2021 – Feb 5, 2021

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

PATCH 1/1 ss: always prefer family as part of host condition to default family

ss accepts an address family both with the -f option and as part of a host condition. However, if the family in the host condition is different than the the last -f option, then which family is actually used depends on the order that different families are checked.

This changes parse_hostcond to check all family prefixes before parsing the rest of the address, so that the host condition's family always has a higher priority than the "preferred" family.

---
 misc/ss.c | 50 ++++++++++++++++++++++++--------------------------
 1 file changed, 24 insertions(+), 26 deletions(-)
misc/ss.c +24 -26
index 0593627b..2a5e056a 100644
@@ -2119,24 +2119,39 @@ void *parse_hostcond(char *addr, bool is_port)
 	int fam = preferred_family;
 	struct filter *f = &current_filter;
 
-	if (fam == AF_UNIX || strncmp(addr, "unix:", 5) == 0) {
+    if (strncmp(addr, "unix:", 5) == 0) {
+        fam = AF_UNIX;
+        addr += 5;
+    } else if (strncmp(addr, "link:", 5) == 0) {
+        fam = AF_PACKET;
+        addr += 5;
+    } else if (strncmp(addr, "netlink:", 8) == 0) {
+        fam = AF_NETLINK;
+        addr += 8;
+    } else if (strncmp(addr, "vsock:", 6) == 0) {
+        fam = AF_VSOCK;
+        addr += 6;
+    } else if (strncmp(addr, "inet:", 5) == 0) {
+        fam = AF_INET;
+        addr += 5;
+    } else if (strncmp(addr, "inet6:", 6) == 0) {
+        fam = AF_INET6;
+        addr += 6;
+    }
+
+	if (fam == AF_UNIX) {
 		char *p;
 
 		a.addr.family = AF_UNIX;
-		if (strncmp(addr, "unix:", 5) == 0)
-			addr += 5;
 		p = strdup(addr);
 		a.addr.bitlen = 8*strlen(p);
 		memcpy(a.addr.data, &p, sizeof(p));
-		fam = AF_UNIX;
 		goto out;
 	}
 
-	if (fam == AF_PACKET || strncmp(addr, "link:", 5) == 0) {
+	if (fam == AF_PACKET) {
 		a.addr.family = AF_PACKET;
 		a.addr.bitlen = 0;
-		if (strncmp(addr, "link:", 5) == 0)
-			addr += 5;
 		port = strchr(addr, ':');
 		if (port) {
 			*port = 0;
@@ -2155,15 +2170,12 @@ void *parse_hostcond(char *addr, bool is_port)
 				return NULL;
 			a.addr.data[0] = ntohs(tmp);
 		}
-		fam = AF_PACKET;
 		goto out;
 	}
 
-	if (fam == AF_NETLINK || strncmp(addr, "netlink:", 8) == 0) {
+	if (fam == AF_NETLINK) {
 		a.addr.family = AF_NETLINK;
 		a.addr.bitlen = 0;
-		if (strncmp(addr, "netlink:", 8) == 0)
-			addr += 8;
 		port = strchr(addr, ':');
 		if (port) {
 			*port = 0;
@@ -2181,16 +2193,13 @@ void *parse_hostcond(char *addr, bool is_port)
 			if (nl_proto_a2n(&a.addr.data[0], addr) == -1)
 				return NULL;
 		}
-		fam = AF_NETLINK;
 		goto out;
 	}
 
-	if (fam == AF_VSOCK || strncmp(addr, "vsock:", 6) == 0) {
+	if (fam == AF_VSOCK) {
 		__u32 cid = ~(__u32)0;
 
 		a.addr.family = AF_VSOCK;
-		if (strncmp(addr, "vsock:", 6) == 0)
-			addr += 6;
 
 		if (is_port)
 			port = addr;
@@ -2212,20 +2221,9 @@ void *parse_hostcond(char *addr, bool is_port)
 				return NULL;
 		}
 		vsock_set_inet_prefix(&a.addr, cid);
-		fam = AF_VSOCK;
 		goto out;
 	}
 
-	if (fam == AF_INET || !strncmp(addr, "inet:", 5)) {
-		fam = AF_INET;
-		if (!strncmp(addr, "inet:", 5))
-			addr += 5;
-	} else if (fam == AF_INET6 || !strncmp(addr, "inet6:", 6)) {
-		fam = AF_INET6;
-		if (!strncmp(addr, "inet6:", 6))
-			addr += 6;
-	}
-
 	/* URL-like literal [] */
 	if (addr[0] == '[') {
 		addr++;
Signature · 1 line
2.30.0
#1 lore raw
Quoted 22 lines · Thayne McCombs
misc/ss.c +13 -1
index 0593627b..2a5e056a 100644
@@ -2119,24 +2119,39 @@ void *parse_hostcond(char *addr, bool is_port)
 	int fam = preferred_family;
 	struct filter *f = &current_filter;
 
-	if (fam == AF_UNIX || strncmp(addr, "unix:", 5) == 0) {
+    if (strncmp(addr, "unix:", 5) == 0) {
+        fam = AF_UNIX;
+        addr += 5;
+    } else if (strncmp(addr, "link:", 5) == 0) {
+        fam = AF_PACKET;
+        addr += 5;
+    } else if (strncmp(addr, "netlink:", 8) == 0) {
+        fam = AF_NETLINK;
+        addr += 8;
+    } else if (strncmp(addr, "vsock:", 6) == 0) {
+        fam = AF_VSOCK;
+        addr += 6;
+    } else if (strncmp(addr, "inet:", 5) == 0) {

misc/ss.c void *parse_hostcond(char *addr, bool is_port) · line 2144

Quoted 10 lines
misc/ss.c +8 -0
+        fam = AF_INET;
+        addr += 5;
+    } else if (strncmp(addr, "inet6:", 6) == 0) {
+        fam = AF_INET6;
+        addr += 6;
+    }
+
+	if (fam == AF_UNIX) {
 		char *p;
 

Looks fine to me, but you need to fix the coding style -- tabs, not spaces.

#2 lore raw

PATCH v2 1/1 ss: always prefer family as part of host condition to default family

I've fixed the indentation to use tabs instead of spaces.

-- >8 --
ss accepts an address family both with the -f option and as part of a host condition. However, if the family in the host condition is different than the the last -f option, then which family is actually used depends on the order that different families are checked.

This changes parse_hostcond to check all family prefixes before parsing the rest of the address, so that the host condition's family always has a higher priority than the "preferred" family.

---
 misc/ss.c | 50 ++++++++++++++++++++++++--------------------------
 1 file changed, 24 insertions(+), 26 deletions(-)
misc/ss.c +24 -26
index 0593627b..aefa1c2f 100644
@@ -2119,24 +2119,39 @@ void *parse_hostcond(char *addr, bool is_port)
 	int fam = preferred_family;
 	struct filter *f = &current_filter;
 
-	if (fam == AF_UNIX || strncmp(addr, "unix:", 5) == 0) {
+	if (strncmp(addr, "unix:", 5) == 0) {
+		fam = AF_UNIX;
+		addr += 5;
+	} else if (strncmp(addr, "link:", 5) == 0) {
+		fam = AF_PACKET;
+		addr += 5;
+	} else if (strncmp(addr, "netlink:", 8) == 0) {
+		fam = AF_NETLINK;
+		addr += 8;
+	} else if (strncmp(addr, "vsock:", 6) == 0) {
+		fam = AF_VSOCK;
+		addr += 6;
+	} else if (strncmp(addr, "inet:", 5) == 0) {
+		fam = AF_INET;
+		addr += 5;
+	} else if (strncmp(addr, "inet6:", 6) == 0) {
+		fam = AF_INET6;
+		addr += 6;
+	}
+
+	if (fam == AF_UNIX) {
 		char *p;
 
 		a.addr.family = AF_UNIX;
-		if (strncmp(addr, "unix:", 5) == 0)
-			addr += 5;
 		p = strdup(addr);
 		a.addr.bitlen = 8*strlen(p);
 		memcpy(a.addr.data, &p, sizeof(p));
-		fam = AF_UNIX;
 		goto out;
 	}
 
-	if (fam == AF_PACKET || strncmp(addr, "link:", 5) == 0) {
+	if (fam == AF_PACKET) {
 		a.addr.family = AF_PACKET;
 		a.addr.bitlen = 0;
-		if (strncmp(addr, "link:", 5) == 0)
-			addr += 5;
 		port = strchr(addr, ':');
 		if (port) {
 			*port = 0;
@@ -2155,15 +2170,12 @@ void *parse_hostcond(char *addr, bool is_port)
 				return NULL;
 			a.addr.data[0] = ntohs(tmp);
 		}
-		fam = AF_PACKET;
 		goto out;
 	}
 
-	if (fam == AF_NETLINK || strncmp(addr, "netlink:", 8) == 0) {
+	if (fam == AF_NETLINK) {
 		a.addr.family = AF_NETLINK;
 		a.addr.bitlen = 0;
-		if (strncmp(addr, "netlink:", 8) == 0)
-			addr += 8;
 		port = strchr(addr, ':');
 		if (port) {
 			*port = 0;
@@ -2181,16 +2193,13 @@ void *parse_hostcond(char *addr, bool is_port)
 			if (nl_proto_a2n(&a.addr.data[0], addr) == -1)
 				return NULL;
 		}
-		fam = AF_NETLINK;
 		goto out;
 	}
 
-	if (fam == AF_VSOCK || strncmp(addr, "vsock:", 6) == 0) {
+	if (fam == AF_VSOCK) {
 		__u32 cid = ~(__u32)0;
 
 		a.addr.family = AF_VSOCK;
-		if (strncmp(addr, "vsock:", 6) == 0)
-			addr += 6;
 
 		if (is_port)
 			port = addr;
@@ -2212,20 +2221,9 @@ void *parse_hostcond(char *addr, bool is_port)
 				return NULL;
 		}
 		vsock_set_inet_prefix(&a.addr, cid);
-		fam = AF_VSOCK;
 		goto out;
 	}
 
-	if (fam == AF_INET || !strncmp(addr, "inet:", 5)) {
-		fam = AF_INET;
-		if (!strncmp(addr, "inet:", 5))
-			addr += 5;
-	} else if (fam == AF_INET6 || !strncmp(addr, "inet6:", 6)) {
-		fam = AF_INET6;
-		if (!strncmp(addr, "inet6:", 6))
-			addr += 6;
-	}
-
 	/* URL-like literal [] */
 	if (addr[0] == '[') {
 		addr++;
Signature · 1 line
2.30.0
Applied
#3 lore raw
Quoted 8 lines · Thayne McCombs

I've fixed the indentation to use tabs instead of spaces.

-- >8 --
ss accepts an address family both with the -f option and as part of a host condition. However, if the family in the host condition is different than the the last -f option, then which family is actually used depends on the order that different families are checked.

Quoted 10 lines

This changes parse_hostcond to check all family prefixes before parsing the rest of the address, so that the host condition's family always has a higher priority than the "preferred" family.

---
 misc/ss.c | 50 ++++++++++++++++++++++++--------------------------
 1 file changed, 24 insertions(+), 26 deletions(-)

applied to iproute2-next. Thanks