[OpenWrt-Devel] [PATCH] utils.c: Add ip address validation

Naresh Kumar Mehta naresh at codeaurora.org
Tue Feb 23 00:53:15 EST 2016


From: Naresh Kumar Mehta <naresh at codeaurora.org>

Do not allow configuring invalid IPv4/IPv6 addresses.
Curently if I configure LAN IP Address as 224.1.1.1, netifd will
configure it.
e.g.
uci set network.lan.ipaddr='224.1.1.1'
uci commit
/etc/init.d/network restart

Now ifconfig br-lan returns
br-lan    Link encap:Ethernet  HWaddr 00:03:7F:13:BA:17
          inet addr:224.1.1.1  Bcast:224.1.1.255  Mask:255.255.255.0
which is wrong.

If I use ifconfig eth1 224.1.1.1, I will get
ifconfig: SIOCSIFADDR: Invalid argument

it means ifconfig is working fine, whereas netifd not.
Proposed patch will test IPv4 address to make sure it is class A/B/C only.
Similarly IPv6 multicast addresses will not be allowed.

Signed-off-by: Naresh Kumar Mehta <naresh at codeaurora.org>
---
 utils.c |   19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/utils.c b/utils.c
index ba26952..de3ae4b 100644
--- a/utils.c
+++ b/utils.c
@@ -119,6 +119,7 @@ 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))
@@ -132,7 +133,23 @@ parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask)
 			return 0;
 	}
 
-	return inet_pton(af, astr, addr);
+	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;
 }
 
 char *
-- 
1.7.9.5
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list