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).
This commit is contained in:
cinap_lenrek 2020-04-11 22:36:19 +02:00
parent a7dab2728b
commit 0c7da78f45
2 changed files with 34 additions and 10 deletions

View file

@ -384,7 +384,9 @@ Again:
} }
if(!tentative){ 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); adddefroute(conf.gaddr, conf.laddr, conf.laddr, conf.mask);
return 0; return 0;
} }
@ -758,7 +760,9 @@ recvrahost(uchar buf[], int pktlen)
DEBUG("got RA from %I on %s; pfx %I %M", DEBUG("got RA from %I on %s; pfx %I %M",
ra->src, conf.dev, conf.v6pref, conf.mask); 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); adddefroute(conf.gaddr, conf.lladdr, conf.laddr, conf.mask);
if(noconfig) if(noconfig)

View file

@ -598,7 +598,8 @@ ip4cfg(void)
return -1; 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); adddefroute(conf.gaddr, conf.laddr, conf.laddr, conf.mask);
return 0; return 0;
@ -987,6 +988,23 @@ uniquent(Ndbtuple *t)
return t; 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 */ /* read configuration (except laddr) for myip from ndb */
void void
ndb2conf(Ndb *db, uchar *myip) ndb2conf(Ndb *db, uchar *myip)
@ -1040,8 +1058,11 @@ ndb2conf(Ndb *db, uchar *myip)
continue; continue;
} }
if(strcmp(nt->attr, "ipgw") == 0) { if(strcmp(nt->attr, "ipgw") == 0) {
nt = uniquent(nt); /* ignore in case we are the gateway */
if(ipindb(ip))
continue;
ipmove(conf.gaddr, ip); ipmove(conf.gaddr, ip);
nt = uniquent(nt);
} else if(strcmp(nt->attr, "dns") == 0) { } else if(strcmp(nt->attr, "dns") == 0) {
addaddrs(conf.dns, sizeof(conf.dns), ip, IPaddrlen); addaddrs(conf.dns, sizeof(conf.dns), ip, IPaddrlen);
} else if(strcmp(nt->attr, "ntp") == 0) { } else if(strcmp(nt->attr, "ntp") == 0) {
@ -1070,7 +1091,6 @@ opendatabase(void)
static void static void
ndbconfig(void) ndbconfig(void)
{ {
uchar ips[128*IPaddrlen];
char etheraddr[32], *attr; char etheraddr[32], *attr;
Ndbtuple *t, *nt; Ndbtuple *t, *nt;
Ndb *db; Ndb *db;
@ -1086,7 +1106,7 @@ ndbconfig(void)
return; return;
} }
memset(ips, 0, sizeof(ips)); memset(dbips, 0, sizeof(dbips));
if(conf.hwatype != 1) if(conf.hwatype != 1)
sysfatal("can't read hardware address"); sysfatal("can't read hardware address");
@ -1100,18 +1120,18 @@ ndbconfig(void)
nt->attr, nt->val); nt->attr, nt->val);
continue; continue;
} }
addaddrs(ips, sizeof(ips), conf.laddr, IPaddrlen); addaddrs(dbips, sizeof(dbips), conf.laddr, IPaddrlen);
} }
ndbfree(t); ndbfree(t);
n = countaddrs(ips, sizeof(ips)); n = countaddrs(dbips, sizeof(dbips));
if(n == 0) if(n == 0)
sysfatal("no ip addresses found in ndb"); sysfatal("no ip addresses found in ndb");
/* add link local address first, if not already done */ /* add link local address first, if not already done */
if(!findllip(conf.lladdr, ifc)){ if(!findllip(conf.lladdr, ifc)){
for(i = 0; i < n; i++){ for(i = 0; i < n; i++){
ipmove(conf.laddr, ips+i*IPaddrlen); ipmove(conf.laddr, dbips+i*IPaddrlen);
if(ISIPV6LINKLOCAL(conf.laddr)){ if(ISIPV6LINKLOCAL(conf.laddr)){
ipv6auto = 0; ipv6auto = 0;
ipmove(conf.lladdr, conf.laddr); ipmove(conf.lladdr, conf.laddr);
@ -1128,7 +1148,7 @@ ndbconfig(void)
/* add v4 addresses and v6 if link local address is available */ /* add v4 addresses and v6 if link local address is available */
for(i = 0; i < n; i++){ 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){ if(isv4(conf.laddr) || ipcmp(conf.laddr, conf.lladdr) != 0){
ndb2conf(db, conf.laddr); ndb2conf(db, conf.laddr);
doadd(); doadd();