Skip to contentSkip to conversation

Don't add route for invalid dst and gateway address combinations

PATCH In review Zoltán Böszörményi 2 participants 3 messages Aug 28, 2023 – Nov 8, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Don't add route for invalid dst and gateway address combinations

Don't add route for link-local or unspecified destination address coupled with unspecified gateway address.

When the machine has multiple interfaces, it may occur that some of them don't have a DHCP server behind them, and there is no fixed address assigned to them either.

Such interfaces get a link-local address assigned by Connman and routes are added to them with 0.0.0.0 gateway address on IPv4.

Adding such routes breaks networking because these routes behave exactly like the default route but they take precedence over the default and are the equivalent of /dev/null for network packets.

Observed with virtual veth* interfaces created by docker where only the bridge interface has a DHCP assigned address.

---
 src/inet.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
src/inet.c +24 -0
index 4039a73c..3465d50c 100644
@@ -611,6 +611,16 @@ int connman_inet_add_network_route(int index, const char *host,
 	addr.sin_addr.s_addr = inet_addr(host);
 	memcpy(&rt.rt_dst, &addr, sizeof(rt.rt_dst));
 
+	/*
+	 * Don't add a routes for link-local or unspecified
+	 * destination address coupled with unspecified gateway.
+	 */
+	if ((!host || is_addr_ll(AF_INET, (struct sockaddr *)&addr) || __connman_inet_is_any_addr(host, AF_INET))
+			&& (!gateway || __connman_inet_is_any_addr(gateway, AF_INET))) {
+		close(sk);
+		return -EINVAL;
+	}
+
 	memset(&addr, 0, sizeof(addr));
 	addr.sin_family = AF_INET;
 	if (gateway)
@@ -745,6 +755,7 @@ int connman_inet_add_ipv6_network_route(int index, const char *host,
 					const char *gateway,
 					unsigned char prefix_len)
 {
+	struct sockaddr_in6 addr;
 	struct in6_rtmsg rt;
 	int sk, err = 0;
 
@@ -753,6 +764,19 @@ int connman_inet_add_ipv6_network_route(int index, const char *host,
 	if (!host)
 		return -EINVAL;
 
+	if (inet_pton(AF_INET6, host, &addr.sin6_addr) != 1) {
+		err = -errno;
+		goto out;
+	}
+
+	/*
+	 * Don't add a route for link-local or unspecified
+	 * destination address coupled with unspecified gateway.
+	 */
+	if ((!host || is_addr_ll(AF_INET6, (struct sockaddr *)&addr) || __connman_inet_is_any_addr(host, AF_INET6))
+			&& (!gateway || __connman_inet_is_any_addr(gateway, AF_INET6)))
+		return -EINVAL;
+
 	memset(&rt, 0, sizeof(rt));
 
 	rt.rtmsg_dst_len = prefix_len;
Signature · 1 line
2.41.0
#1 lore raw

Ping.

2023. 08. 28. 13:18 keltezéssel, Zoltán Böszörményi írta:

Quoted 69 lines

Don't add route for link-local or unspecified destination address coupled with unspecified gateway address.

When the machine has multiple interfaces, it may occur that some of them don't have a DHCP server behind them, and there is no fixed address assigned to them either.

Such interfaces get a link-local address assigned by Connman and routes are added to them with 0.0.0.0 gateway address on IPv4.

Adding such routes breaks networking because these routes behave exactly like the default route but they take precedence over the default and are the equivalent of /dev/null for network packets.

Observed with virtual veth* interfaces created by docker where only the bridge interface has a DHCP assigned address.

---
  src/inet.c | 24 ++++++++++++++++++++++++
  1 file changed, 24 insertions(+)
src/inet.c +24 -0
index 4039a73c..3465d50c 100644
@@ -611,6 +611,16 @@ int connman_inet_add_network_route(int index, const char *host,
  	addr.sin_addr.s_addr = inet_addr(host);
  	memcpy(&rt.rt_dst, &addr, sizeof(rt.rt_dst));
 
+	/*
+	 * Don't add a routes for link-local or unspecified
+	 * destination address coupled with unspecified gateway.
+	 */
+	if ((!host || is_addr_ll(AF_INET, (struct sockaddr *)&addr) || __connman_inet_is_any_addr(host, AF_INET))
+			&& (!gateway || __connman_inet_is_any_addr(gateway, AF_INET))) {
+		close(sk);
+		return -EINVAL;
+	}
+
  	memset(&addr, 0, sizeof(addr));
  	addr.sin_family = AF_INET;
  	if (gateway)
@@ -745,6 +755,7 @@ int connman_inet_add_ipv6_network_route(int index, const char *host,
  					const char *gateway,
  					unsigned char prefix_len)
  {
+	struct sockaddr_in6 addr;
  	struct in6_rtmsg rt;
  	int sk, err = 0;
 
@@ -753,6 +764,19 @@ int connman_inet_add_ipv6_network_route(int index, const char *host,
  	if (!host)
  		return -EINVAL;
 
+	if (inet_pton(AF_INET6, host, &addr.sin6_addr) != 1) {
+		err = -errno;
+		goto out;
+	}
+
+	/*
+	 * Don't add a route for link-local or unspecified
+	 * destination address coupled with unspecified gateway.
+	 */
+	if ((!host || is_addr_ll(AF_INET6, (struct sockaddr *)&addr) || __connman_inet_is_any_addr(host, AF_INET6))
+			&& (!gateway || __connman_inet_is_any_addr(gateway, AF_INET6)))
+		return -EINVAL;
+
  	memset(&rt, 0, sizeof(rt));
 
  	rt.rtmsg_dst_len = prefix_len;
#2 lore raw
In reply to Zoltán Böszörményi

Hi Zoltan,

Quoted 14 lines

Don't add route for link-local or unspecified destination address coupled with unspecified gateway address.

When the machine has multiple interfaces, it may occur that some of them don't have a DHCP server behind them, and there is no fixed address assigned to them either.

Such interfaces get a link-local address assigned by Connman and routes are added to them with 0.0.0.0 gateway address on IPv4.

Adding such routes breaks networking because these routes behave exactly like the default route but they take precedence over the default and are the equivalent of /dev/null for network packets.

Quoted 5 lines

Observed with virtual veth* interfaces created by docker where only the bridge interface has a DHCP assigned address.
---
src/inet.c | 24 ++++++++++++++++++++++++

1 file changed, 24 insertions(+)

patch has been applied.

Regards

Marcel