From 35cab9b816dbf6eabe94d4ed3eff01ebff68a695 Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Sun, 22 Dec 2024 20:06:18 +0000 Subject: [PATCH] ip/dhcpd: don't get confused by ipv6 addresses for bootp Info When looking up tftp/bootp information from ndb, filter out ipv6 addresses. This can happen when for example the tftp= attribute points to a domain or sys-name that resolves to both ipv4 and ipv6 addresses. Make validip() do the isv4() check, and provide separate validipmask() function. --- sys/src/cmd/ip/dhcpd/dat.h | 1 + sys/src/cmd/ip/dhcpd/dhcpd.c | 14 ++++++++++++-- sys/src/cmd/ip/dhcpd/ndb.c | 12 +++++++----- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/sys/src/cmd/ip/dhcpd/dat.h b/sys/src/cmd/ip/dhcpd/dat.h index 1e8ba56a8..80052674b 100644 --- a/sys/src/cmd/ip/dhcpd/dat.h +++ b/sys/src/cmd/ip/dhcpd/dat.h @@ -50,6 +50,7 @@ struct Info /* from dhcp.c */ extern int validip(uchar*); +extern int validipmask(uchar*); extern void fatal(char*, ...); extern void warning(char*, ...); #pragma varargck argpos fatal 1 diff --git a/sys/src/cmd/ip/dhcpd/dhcpd.c b/sys/src/cmd/ip/dhcpd/dhcpd.c index f1e23022a..22a60b584 100644 --- a/sys/src/cmd/ip/dhcpd/dhcpd.c +++ b/sys/src/cmd/ip/dhcpd/dhcpd.c @@ -1134,9 +1134,9 @@ miscoptions(Req *rp, uchar *ip) addrs[i] = &x[i*IPaddrlen]; /* always supply these */ - if(validip(rp->ii.ipmask)) + if(validipmask(rp->ii.ipmask)) maskopt(rp, OBmask, rp->ii.ipmask); - else if(validip(rp->gii.ipmask)) + else if(validipmask(rp->gii.ipmask)) maskopt(rp, OBmask, rp->gii.ipmask); else if((lifc = ipremoteonifc(rp->ifc, ip)) != nil) maskopt(rp, OBmask, lifc->mask); @@ -1391,6 +1391,16 @@ readsysname(void) extern int validip(uchar *ip) +{ + if(ipcmp(ip, IPnoaddr) == 0) + return 0; + if(ipcmp(ip, v4prefix) == 0) + return 0; + return isv4(ip); +} + +extern int +validipmask(uchar *ip) { if(ipcmp(ip, IPnoaddr) == 0) return 0; diff --git a/sys/src/cmd/ip/dhcpd/ndb.c b/sys/src/cmd/ip/dhcpd/ndb.c index b3d8b9896..da0244868 100644 --- a/sys/src/cmd/ip/dhcpd/ndb.c +++ b/sys/src/cmd/ip/dhcpd/ndb.c @@ -60,14 +60,18 @@ static void setipaddr(uchar *addr, char *ip) { if(ipcmp(addr, IPnoaddr) == 0) - parseip(addr, ip); + if(parseip(addr, ip) == -1 + || !validip(addr)) + ipmove(addr, IPnoaddr); /* invalid */ } static void setipmask(uchar *mask, char *ip) { if(ipcmp(mask, IPnoaddr) == 0) - parseipmask(mask, ip, 1); + if(parseipmask(mask, ip, 1) == -1 + || !validipmask(mask)) + ipmove(mask, IPnoaddr); /* invalid */ } /* @@ -228,9 +232,7 @@ lookup(Bootp *bp, Info *iip, Info *riip) for(nt = t; nt != nil; nt = nt->entry){ if(strcmp(nt->attr, "ip") != 0) continue; - if(parseip(ciaddr, nt->val) == -1 || !isv4(ciaddr)) - continue; - if(!validip(ciaddr)) + if(parseip(ciaddr, nt->val) == -1 || !validip(ciaddr)) continue; if(!samenet(ciaddr, riip)) continue;