shithub: riscv

Download patch

ref: 0c7da78f4519f1352668b4b024f29fb1e887b3d2
parent: a7dab2728bde8eea68ddfdfadfad08a00b1f7c68
author: cinap_lenrek <[email protected]>
date: Sat Apr 11 18:36:19 EDT 2020

ip/ipconfig: ignore default routes targeting ourselfs

when running ndb configuration, we might inherit the ipgw=
attribute from the ipnet pointing to our own ip address
(we are the default gateway). ignore such entries.

do not add default routes with gateway equal to our own
local (ip4) or link-local ip address (ipv6).

--- a/sys/src/cmd/ip/ipconfig/ipv6.c
+++ b/sys/src/cmd/ip/ipconfig/ipv6.c
@@ -384,7 +384,9 @@
 	}
 
 	if(!tentative){
-		if(validip(conf.gaddr) && !isv4(conf.gaddr))
+		if(validip(conf.gaddr) && !isv4(conf.gaddr)
+		&& ipcmp(conf.gaddr, conf.laddr) != 0
+		&& ipcmp(conf.gaddr, conf.lladdr) != 0)
 			adddefroute(conf.gaddr, conf.laddr, conf.laddr, conf.mask);
 		return 0;
 	}
@@ -758,7 +760,9 @@
 		DEBUG("got RA from %I on %s; pfx %I %M",
 			ra->src, conf.dev, conf.v6pref, conf.mask);
 
-		if(validip(conf.gaddr))
+		if(validip(conf.gaddr)
+		&& ipcmp(conf.gaddr, conf.laddr) != 0
+		&& ipcmp(conf.gaddr, conf.lladdr) != 0)
 			adddefroute(conf.gaddr, conf.lladdr, conf.laddr, conf.mask);
 
 		if(noconfig)
--- a/sys/src/cmd/ip/ipconfig/main.c
+++ b/sys/src/cmd/ip/ipconfig/main.c
@@ -598,7 +598,8 @@
 		return -1;
 	}
 
-	if(validip(conf.gaddr) && isv4(conf.gaddr))
+	if(validip(conf.gaddr) && isv4(conf.gaddr)
+	&& ipcmp(conf.gaddr, conf.laddr) != 0)
 		adddefroute(conf.gaddr, conf.laddr, conf.laddr, conf.mask);
 
 	return 0;
@@ -987,6 +988,23 @@
 	return t;
 }
 
+/* my ips from ndb, read by ndbconfig() below */
+static uchar dbips[128*IPaddrlen];
+
+static int
+ipindb(uchar *ip)
+{
+	uchar *a;
+
+	for(a = dbips; a < &dbips[sizeof(dbips)]; a += IPaddrlen){
+		if(!validip(a))
+			break;
+		if(ipcmp(ip, a) == 0)
+			return 1;
+	}
+	return 0;
+}
+
 /* read configuration (except laddr) for myip from ndb */
 void
 ndb2conf(Ndb *db, uchar *myip)
@@ -1040,8 +1058,11 @@
 			continue;
 		}
 		if(strcmp(nt->attr, "ipgw") == 0) {
-			nt = uniquent(nt);
+			/* ignore in case we are the gateway */
+			if(ipindb(ip))
+				continue;
 			ipmove(conf.gaddr, ip);
+			nt = uniquent(nt);
 		} else if(strcmp(nt->attr, "dns") == 0) {
 			addaddrs(conf.dns, sizeof(conf.dns), ip, IPaddrlen);
 		} else if(strcmp(nt->attr, "ntp") == 0) {
@@ -1070,7 +1091,6 @@
 static void
 ndbconfig(void)
 {
-	uchar ips[128*IPaddrlen];
 	char etheraddr[32], *attr;
 	Ndbtuple *t, *nt;
 	Ndb *db;
@@ -1086,7 +1106,7 @@
 		return;
 	}
 
-	memset(ips, 0, sizeof(ips));
+	memset(dbips, 0, sizeof(dbips));
 
 	if(conf.hwatype != 1)
 		sysfatal("can't read hardware address");
@@ -1100,11 +1120,11 @@
 				nt->attr, nt->val);
 			continue;
 		}
-		addaddrs(ips, sizeof(ips), conf.laddr, IPaddrlen);
+		addaddrs(dbips, sizeof(dbips), conf.laddr, IPaddrlen);
 	}
 	ndbfree(t);
 
-	n = countaddrs(ips, sizeof(ips));
+	n = countaddrs(dbips, sizeof(dbips));
 	if(n == 0)
 		sysfatal("no ip addresses found in ndb");
 
@@ -1111,7 +1131,7 @@
 	/* add link local address first, if not already done */
 	if(!findllip(conf.lladdr, ifc)){
 		for(i = 0; i < n; i++){
-			ipmove(conf.laddr, ips+i*IPaddrlen);
+			ipmove(conf.laddr, dbips+i*IPaddrlen);
 			if(ISIPV6LINKLOCAL(conf.laddr)){
 				ipv6auto = 0;
 				ipmove(conf.lladdr, conf.laddr);
@@ -1128,7 +1148,7 @@
 
 	/* add v4 addresses and v6 if link local address is available */
 	for(i = 0; i < n; i++){
-		ipmove(conf.laddr, ips+i*IPaddrlen);
+		ipmove(conf.laddr, dbips+i*IPaddrlen);
 		if(isv4(conf.laddr) || ipcmp(conf.laddr, conf.lladdr) != 0){
 			ndb2conf(db, conf.laddr);
 			doadd();