Browse Source

utils: Move IP address validation to parse_addr function

Commit 7a51f23e adds IP address validation in the function parse_ip_and_netmask;
however the added check is too restrictive as the function is used on several places
resulting into the problem multicast routes cannot be added anymore via UCI.
Therefore move the IP host address validation to the function parse_addr so
experimantal/multicast addresses cannot be added as a host IP address while
multicast routes can be added again.

Signed-off-by: Hans Dedecker <dedeckeh@gmail.com>
Hans Dedecker 7 years ago
parent
commit
cec1ce296e
2 changed files with 16 additions and 23 deletions
  1. 15 5
      proto.c
  2. 1 18
      utils.c

+ 15 - 5
proto.c

@@ -123,17 +123,27 @@ parse_addr(struct interface *iface, const char *str, bool v6, int mask,
 		return false;
 
 	addr->mask = mask;
-	if (!parse_ip_and_netmask(af, str, &addr->addr, &addr->mask)) {
-		interface_add_error(iface, "proto", "INVALID_ADDRESS", &str, 1);
-		free(addr);
-		return false;
-	}
+	if (!parse_ip_and_netmask(af, str, &addr->addr, &addr->mask))
+		goto error;
+
+	if (!v6) {
+		if (IN_EXPERIMENTAL(ntohl(addr->addr.in.s_addr)))
+			goto error;
+
+	} else if (IN6_IS_ADDR_MULTICAST(&addr->addr.in6))
+		goto error;
 
 	if (broadcast)
 		addr->broadcast = broadcast;
 
 	vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
 	return true;
+
+error:
+	interface_add_error(iface, "proto", "INVALID_ADDRESS", &str, 1);
+	free(addr);
+
+	return false;
 }
 
 static int

+ 1 - 18
utils.c

@@ -119,7 +119,6 @@ int
 parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask)
 {
 	char *astr = alloca(strlen(str) + 1);
-	int ret = 0;
 
 	strcpy(astr, str);
 	if (!split_netmask(astr, netmask, af == AF_INET6))
@@ -133,23 +132,7 @@ parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask)
 			return 0;
 	}
 
-	ret = inet_pton(af, astr, addr);
-	if (ret > 0) {
-		if (af == AF_INET) {
-			struct in_addr *ip4_addr = (struct in_addr *)addr;
-			uint32_t host_addr = ntohl(ip4_addr->s_addr);
-
-			if (IN_EXPERIMENTAL(host_addr)) {
-				return 0;
-			}
-		}
-		else if (af == AF_INET6) {
-			if (IN6_IS_ADDR_MULTICAST((struct in6_addr *)addr)) {
-				return 0;
-			}
-		}
-	}
-	return ret;
+	return inet_pton(af, astr, addr);
 }
 
 char *