shithub: riscv

Download patch

ref: b85245f5d27480cf1a0e6ec5c4dd7893c4130242
parent: 84e67ffa88e4d07b2e5848d666296a3e41750da6
author: cinap_lenrek <[email protected]>
date: Sun Jan 14 14:33:30 EST 2018

ip/ipconfig: add v6 deault route from router advertisements

--- a/sys/src/cmd/ip/ipconfig/ipconfig.h
+++ b/sys/src/cmd/ip/ipconfig/ipconfig.h
@@ -57,12 +57,10 @@
 	int 	maxraint; /* rfc2461, p.39: 4sec ≤ maxraint ≤ 1800sec, def 600 */
 	int	minraint;	/* 3sec ≤ minraint ≤ 0.75*maxraint */
 	int	linkmtu;
+	int	routerlt;	/* router life time */
 	int	reachtime;	/* 3,600,000 msec, default 0 */
 	int	rxmitra;	/* default 0 */
 	int	ttl;		/* default 0 (unspecified) */
-	/* default gateway params */
-	uchar	v6gaddr[IPaddrlen];
-	int	routerlt;	/* router life time */
 
 	/* prefix related */
 	uchar	v6pref[IPaddrlen];
@@ -108,10 +106,10 @@
 void	doadd(int);
 void	doremove(void);
 void	dounbind(void);
-int	ipconfig4(void);
-int	ipconfig6(int);
+int	isether(void);
 long	jitter(void);
 void	lookforip(char*);
+void	mklladdr(void);
 void	mkclientid(void);
 int	nipifcs(char*);
 int	openlisten(void);
@@ -143,7 +141,6 @@
  */
 
 void	doipv6(int);
-int	ipconfig6(int);
 void	recvra6(void);
 void	sendra6(void);
 void	v6paraminit(Conf *);
--- a/sys/src/cmd/ip/ipconfig/ipv6.c
+++ b/sys/src/cmd/ip/ipconfig/ipv6.c
@@ -318,31 +318,6 @@
 		noted(NDFLT);
 }
 
-/*
- * based on libthread's threadsetname, but drags in less library code.
- * actually just sets the arguments displayed.
- */
-void
-procsetname(char *fmt, ...)
-{
-	int fd;
-	char *cmdname;
-	char buf[128];
-	va_list arg;
-
-	va_start(arg, fmt);
-	cmdname = vsmprint(fmt, arg);
-	va_end(arg);
-	if (cmdname == nil)
-		return;
-	snprint(buf, sizeof buf, "#p/%d/args", getpid());
-	if((fd = open(buf, OWRITE)) >= 0){
-		write(fd, cmdname, strlen(cmdname)+1);
-		close(fd);
-	}
-	free(cmdname);
-}
-
 int
 dialicmp(uchar *dst, int dport, int *ctlfd)
 {
@@ -383,18 +358,14 @@
 
 /* add ipv6 addr to an interface */
 int
-ip6cfg(int autoconf)
+ip6cfg(void)
 {
 	int tentative, dupfound = 0, n;
 	char *p, buf[256];
-	uchar ethaddr[6];
 	Biobuf *bp;
 
-	if (autoconf) {			/* create link-local addr */
-		if (myetheraddr(ethaddr, conf.dev) < 0)
-			sysfatal("myetheraddr w/ %s failed: %r", conf.dev);
-		ea2lla(conf.laddr, ethaddr);
-	}
+	if(!validip(conf.laddr) || isv4(conf.laddr))
+		return -1;
 
 	tentative = dupl_disc;
 
@@ -562,7 +533,6 @@
 	static int first = 1;
 
 	ra = (Routeradv*)buf;
-//	memmove(conf.v6gaddr, ra->src, IPaddrlen);
 	conf.ttl = ra->cttl;
 	conf.mflag = (MFMASK & ra->mor);
 	conf.oflag = (OCMASK & ra->mor);
@@ -570,7 +540,6 @@
 	conf.reachtime = nhgetl(ra->rchbltime);
 	conf.rxmitra =   nhgetl(ra->rxmtimer);
 
-//	issueadd6(&conf);		/* for conf.v6gaddr? */
 	if (fprint(conf.cfd, "ra6 recvra 1") < 0)
 		ralog("write(ra6 recvra 1) failed: %r");
 	issuebasera6(&conf);
@@ -592,7 +561,6 @@
 					"router adv %I", ra->src);
 				return;
 			}
-
 			snprint(abuf, sizeof abuf, "%s/arp", conf.mpoint);
 			arpfd = open(abuf, OWRITE);
 			if (arpfd < 0) {
@@ -650,6 +618,8 @@
 			break;
 		}
 	}
+	if(conf.routerlt != 0 && ISIPV6LINKLOCAL(ra->src))
+		adddefroute(conf.mpoint, ra->src);
 }
 
 /*
--- a/sys/src/cmd/ip/ipconfig/main.c
+++ b/sys/src/cmd/ip/ipconfig/main.c
@@ -9,6 +9,8 @@
 #include "../dhcp.h"
 #include "ipconfig.h"
 
+#include <libsec.h> /* genrandom() */
+
 #define DEBUG if(debug)warning
 
 /* possible verbs */
@@ -190,7 +192,7 @@
 void	dounbind(void);
 void	getoptions(uchar*);
 int	ip4cfg(void);
-int	ip6cfg(int a);
+int	ip6cfg(void);
 void	lookforip(char*);
 void	mkclientid(void);
 void	ndbconfig(void);
@@ -593,10 +595,6 @@
 void
 doadd(int retry)
 {
-	int ppp;
-
-	ppp = strcmp(conf.type, "ppp") == 0;
-
 	/* get number of preexisting interfaces */
 	nip = nipifcs(conf.mpoint);
 	if(beprimary == -1 && nip == 0)
@@ -609,20 +607,13 @@
 		binddevice();
 	}
 
-	if (ipv6auto && !ppp) {
-		if (ip6cfg(ipv6auto) < 0)
-			sysfatal("can't automatically start IPv6 on %s",
-				conf.dev);
-	} else if (validip(conf.laddr) && !isv4(conf.laddr)) {
-		if (ip6cfg(0) < 0)
-			sysfatal("can't start IPv6 on %s, address %I",
-				conf.dev, conf.laddr);
-	}
-
-	if(!validip(conf.laddr) && !ppp)
+	if(!validip(conf.laddr))
 		if(dondbconfig)
 			ndbconfig();
-		else
+		else if(ipv6auto){
+			mklladdr();
+			dodhcp = 0;
+		} else
 			dodhcp = 1;
 
 	/* run dhcp if we need something */
@@ -642,10 +633,15 @@
 	if(noconfig)
 		return;
 
-	if(ip4cfg() < 0)
-		sysfatal("can't start ip");
-	else if(dodhcp && conf.lease != Lforever)
-		dhcpwatch(0);
+	if(!isv4(conf.laddr)){
+		if(ip6cfg() < 0)
+			sysfatal("can't start IPv6 on %s, address %I", conf.dev, conf.laddr);
+	} else {
+		if(ip4cfg() < 0)
+			sysfatal("can't start IPv4 on %s, address %I", conf.dev, conf.laddr);
+		else if(dodhcp && conf.lease != Lforever)
+			dhcpwatch(0);
+	}
 
 	/* leave everything we've learned somewhere other procs can find it */
 	if(beprimary){
@@ -736,23 +732,37 @@
 	close(cfd);
 }
 
+int
+isether(void)
+{
+	return strcmp(conf.type, "ether") == 0 || strcmp(conf.type, "gbe") == 0;
+}
+
+/* create link local address */
+void
+mklladdr(void)
+{
+	if(!isether() || myetheraddr(conf.hwa, conf.dev) != 0)
+		genrandom(conf.hwa, sizeof(conf.hwa));
+	ea2lla(conf.laddr, conf.hwa);
+}
+
 /* create a client id */
 void
 mkclientid(void)
 {
-	if(strcmp(conf.type, "ether") == 0 || strcmp(conf.type, "gbe") == 0)
-		if(myetheraddr(conf.hwa, conf.dev) == 0){
-			conf.hwalen = 6;
-			conf.hwatype = 1;
-			conf.cid[0] = conf.hwatype;
-			memmove(&conf.cid[1], conf.hwa, conf.hwalen);
-			conf.cidlen = conf.hwalen+1;
-		} else {
-			conf.hwatype = -1;
-			snprint((char*)conf.cid, sizeof conf.cid,
-				"plan9_%ld.%d", lrand(), getpid());
-			conf.cidlen = strlen((char*)conf.cid);
-		}
+	if(isether() && myetheraddr(conf.hwa, conf.dev) == 0){
+		conf.hwalen = 6;
+		conf.hwatype = 1;
+		conf.cid[0] = conf.hwatype;
+		memmove(&conf.cid[1], conf.hwa, conf.hwalen);
+		conf.cidlen = conf.hwalen+1;
+	} else {
+		conf.hwatype = -1;
+		snprint((char*)conf.cid, sizeof conf.cid,
+			"plan9_%ld.%d", lrand(), getpid());
+		conf.cidlen = strlen((char*)conf.cid);
+	}
 }
 
 /* bind ip into the namespace */
@@ -775,8 +785,7 @@
 	int fd;
 	Ctl *cp;
 
-	if (firstctl == nil ||
-	    strcmp(conf.type, "ether") != 0 && strcmp(conf.type, "gbe") != 0)
+	if (firstctl == nil || !isether())
 		return;
 
 	snprint(ctlfile, sizeof ctlfile, "%s/clone", conf.dev);
@@ -827,7 +836,7 @@
 	char buf[256];
 	int n;
 
-	if(!validip(conf.laddr))
+	if(!validip(conf.laddr) || !isv4(conf.laddr))
 		return -1;
 
 	n = sprint(buf, "add");
@@ -951,7 +960,7 @@
 	}
 
 	dolog = 1;			/* log, don't print */
-	procsetname("dhcpwatch");
+	procsetname("dhcpwatch on %s", conf.dev);
 	/* keep trying to renew the lease */
 	for(;;){
 		secs = conf.lease/2;
@@ -1770,6 +1779,31 @@
 	return -1;
 }
 
+/*
+ * based on libthread's threadsetname, but drags in less library code.
+ * actually just sets the arguments displayed.
+ */
+void
+procsetname(char *fmt, ...)
+{
+	int fd;
+	char *cmdname;
+	char buf[128];
+	va_list arg;
+
+	va_start(arg, fmt);
+	cmdname = vsmprint(fmt, arg);
+	va_end(arg);
+	if (cmdname == nil)
+		return;
+	snprint(buf, sizeof buf, "#p/%d/args", getpid());
+	if((fd = open(buf, OWRITE)) >= 0){
+		write(fd, cmdname, strlen(cmdname)+1);
+		close(fd);
+	}
+	free(cmdname);
+}
+
 /* get everything out of ndb */
 void
 ndbconfig(void)
@@ -1783,8 +1817,7 @@
 	db = ndbopen(dbfile);
 	if(db == nil)
 		sysfatal("can't open ndb: %r");
-	if (strcmp(conf.type, "ether") != 0 && strcmp(conf.type, "gbe") != 0 ||
-	    myetheraddr(conf.hwa, conf.dev) != 0)
+	if (!isether() || myetheraddr(conf.hwa, conf.dev) != 0)
 		sysfatal("can't read hardware address");
 	sprint(etheraddr, "%E", conf.hwa);
 	nattr = 0;