CVS commit: src/usr.sbin/ifwatchd

2020-10-04 Thread Roy Marples
Module Name:src
Committed By:   roy
Date:   Sun Oct  4 20:36:32 UTC 2020

Modified Files:
src/usr.sbin/ifwatchd: ifwatchd.c

Log Message:
ifwatchd: Enable SO_RERROR to re-sync interface state.

Interface arrival, departure and link state changes will sync
and if different will be actioned.

Currently we do not track addresses, so any changes there are still lost.


To generate a diff of this commit:
cvs rdiff -u -r1.45 -r1.46 src/usr.sbin/ifwatchd/ifwatchd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ifwatchd/ifwatchd.c
diff -u src/usr.sbin/ifwatchd/ifwatchd.c:1.45 src/usr.sbin/ifwatchd/ifwatchd.c:1.46
--- src/usr.sbin/ifwatchd/ifwatchd.c:1.45	Sun Sep 27 19:55:21 2020
+++ src/usr.sbin/ifwatchd/ifwatchd.c	Sun Oct  4 20:36:32 2020
@@ -1,6 +1,6 @@
-/*	$NetBSD: ifwatchd.c,v 1.45 2020/09/27 19:55:21 roy Exp $	*/
+/*	$NetBSD: ifwatchd.c,v 1.46 2020/10/04 20:36:32 roy Exp $	*/
 #include 
-__RCSID("$NetBSD: ifwatchd.c,v 1.45 2020/09/27 19:55:21 roy Exp $");
+__RCSID("$NetBSD: ifwatchd.c,v 1.46 2020/10/04 20:36:32 roy Exp $");
 
 /*-
  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
@@ -44,15 +44,16 @@ __RCSID("$NetBSD: ifwatchd.c,v 1.45 2020
 #include 
 #include 
 
+#include 
+#include 
+#include 
+#include 
 #include 
 #include 
 #include 
 #include 
-#include 
-#include 
-#include 
-#include 
 #include 
+#include 
 
 enum event { ARRIVAL, DEPARTURE, UP, DOWN, CARRIER, NO_CARRIER };
 enum addrflag { NOTREADY, DETACHED, READY };
@@ -69,7 +70,7 @@ static void check_announce(const struct 
 static void check_carrier(const struct if_msghdr *ifm);
 static void free_interfaces(void);
 static struct interface_data * find_interface(int index);
-static void run_initial_ups(void);
+static void run_initial_ups(bool);
 
 /* global variables */
 static int verbose = 0, quiet = 0;
@@ -197,9 +198,12 @@ main(int argc, char **argv)
 	if (setsockopt(s, PF_ROUTE, RO_MSGFILTER,
 	, sizeof(msgfilter)) < 0)
 		syslog(LOG_ERR, "RO_MSGFILTER: %m");
+	n = 1;
+	if (setsockopt(s, SOL_SOCKET, SO_RERROR, , sizeof(n)) < 0)
+		syslog(LOG_ERR, "SO_RERROR: %m");
 
 	if (!inhibit_initial)
-		run_initial_ups();
+		run_initial_ups(true);
 
 	iov[0].iov_base = buf;
 	iov[0].iov_len = sizeof(buf);
@@ -210,6 +214,15 @@ main(int argc, char **argv)
 	for (;;) {
 		n = recvmsg(s, , 0);
 		if (n == -1) {
+			if (errno == ENOBUFS) {
+syslog(LOG_ERR,
+"routing socket overflow detected");
+/* XXX We don't track addresses, so they
+ * won't be reported. */
+if (!inhibit_initial)
+	run_initial_ups(false);
+continue;
+			}
 			syslog(LOG_ERR, "recvmsg: %m");
 			exit(EXIT_FAILURE);
 		}
@@ -482,6 +495,28 @@ check_carrier(const struct if_msghdr *if
 }
 
 static void
+do_announce(struct interface_data *ifd,
+unsigned short what, unsigned short index)
+{
+
+	switch (what) {
+	case IFAN_ARRIVAL:
+		ifd->index = index;
+		invoke_script(ifd->ifname, ARRIVAL, NULL, NULL);
+		break;
+	case IFAN_DEPARTURE:
+		ifd->index = -1;
+		ifd->last_carrier_status = -1;
+		invoke_script(ifd->ifname, DEPARTURE, NULL, NULL);
+		break;
+	default:
+		if (verbose)
+			(void) printf("unknown announce: what=%d\n", what);
+		break;
+	}
+}
+
+static void
 check_announce(const struct if_announcemsghdr *ifan)
 {
 	struct interface_data * p;
@@ -491,22 +526,7 @@ check_announce(const struct if_announcem
 		if (strcmp(p->ifname, ifname) != 0)
 			continue;
 
-		switch (ifan->ifan_what) {
-		case IFAN_ARRIVAL:
-			p->index = ifan->ifan_index;
-			invoke_script(p->ifname, ARRIVAL, NULL, NULL);
-			break;
-		case IFAN_DEPARTURE:
-			p->index = -1;
-			p->last_carrier_status = -1;
-			invoke_script(p->ifname, DEPARTURE, NULL, NULL);
-			break;
-		default:
-			if (verbose)
-(void) printf("unknown announce: "
-"what=%d\n", ifan->ifan_what);
-			break;
-		}
+		do_announce(p, ifan->ifan_what, ifan->ifan_index);
 		return;
 	}
 }
@@ -536,7 +556,7 @@ find_interface(int idx)
 }
 
 static void
-run_initial_ups(void)
+run_initial_ups(bool do_addrs)
 {
 	struct interface_data * ifd;
 	struct ifaddrs *res = NULL, *p;
@@ -551,6 +571,19 @@ run_initial_ups(void)
 	if (getifaddrs() != 0)
 		goto out;
 
+	/* Check if any interfaces vanished */
+	SLIST_FOREACH(ifd, , next) {
+		for (p = res; p; p = p->ifa_next) {
+			if (strcmp(ifd->ifname, p->ifa_name) != 0)
+continue;
+			ifa = p->ifa_addr;
+			if (ifa != NULL && ifa->sa_family == AF_LINK)
+break;
+		}
+		if (p == NULL)
+			do_announce(ifd, IFAN_DEPARTURE, ifd->index);
+	}
+
 	for (p = res; p; p = p->ifa_next) {
 		SLIST_FOREACH(ifd, , next) {
 			if (strcmp(ifd->ifname, p->ifa_name) == 0)
@@ -560,7 +593,8 @@ run_initial_ups(void)
 			continue;
 
 		ifa = p->ifa_addr;
-		if (ifa != NULL && ifa->sa_family == AF_LINK)
+		if (ifa != NULL && ifa->sa_family == AF_LINK &&
+		ifd->index == -1)
 			invoke_script(ifd->ifname, ARRIVAL, NULL, NULL);
 
 		if ((p->ifa_flags & 

CVS commit: src/usr.sbin/ifwatchd

2020-09-27 Thread Roy Marples
Module Name:src
Committed By:   roy
Date:   Sun Sep 27 19:55:22 UTC 2020

Modified Files:
src/usr.sbin/ifwatchd: ifwatchd.c

Log Message:
ifwatchd: remove SIOCGIFDATA and SIOCGIFMEDIA ioctls

getifaddrs(3) and route(4) provide all the data we need.


To generate a diff of this commit:
cvs rdiff -u -r1.44 -r1.45 src/usr.sbin/ifwatchd/ifwatchd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ifwatchd/ifwatchd.c
diff -u src/usr.sbin/ifwatchd/ifwatchd.c:1.44 src/usr.sbin/ifwatchd/ifwatchd.c:1.45
--- src/usr.sbin/ifwatchd/ifwatchd.c:1.44	Wed Sep 23 02:32:04 2020
+++ src/usr.sbin/ifwatchd/ifwatchd.c	Sun Sep 27 19:55:21 2020
@@ -1,6 +1,6 @@
-/*	$NetBSD: ifwatchd.c,v 1.44 2020/09/23 02:32:04 roy Exp $	*/
+/*	$NetBSD: ifwatchd.c,v 1.45 2020/09/27 19:55:21 roy Exp $	*/
 #include 
-__RCSID("$NetBSD: ifwatchd.c,v 1.44 2020/09/23 02:32:04 roy Exp $");
+__RCSID("$NetBSD: ifwatchd.c,v 1.45 2020/09/27 19:55:21 roy Exp $");
 
 /*-
  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
@@ -39,7 +39,6 @@ __RCSID("$NetBSD: ifwatchd.c,v 1.44 2020
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 #include 
@@ -536,46 +535,13 @@ find_interface(int idx)
 	return NULL;
 }
 
-static bool
-has_carrier(int s, const char *ifname)
-{
-	struct ifmediareq ifmr = { .ifm_status = 0 };
-
-	strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
-	if (ioctl(s, SIOCGIFMEDIA, ) == -1) {
-		struct ifdatareq ifdr = { .ifdr_data.ifi_link_state = 0 };
-
-		strlcpy(ifdr.ifdr_name, ifname, sizeof(ifdr.ifdr_name));
-		if (ioctl(s, SIOCGIFDATA, ) == -1) {
-			/* Should not be possible. */
-			return false;
-		}
-		if (ifdr.ifdr_data.ifi_link_state == LINK_STATE_UP)
-			return true;
-		else
-			return false;
-	}
-
-	if (!(ifmr.ifm_status & IFM_AVALID)) {
-		/*
-		 * Interface doesn't report media-valid status.
-		 * assume ok.
-		 */
-		return true;
-	}
-
-	if (ifmr.ifm_status & IFM_ACTIVE)
-		return true;
-	else
-		return false;
-}
-
 static void
 run_initial_ups(void)
 {
 	struct interface_data * ifd;
 	struct ifaddrs *res = NULL, *p;
 	struct sockaddr *ifa;
+	const struct if_data *ifi;
 	int s, aflag;
 
 	s = socket(AF_INET, SOCK_DGRAM, 0);
@@ -602,11 +568,10 @@ run_initial_ups(void)
 		if (ifa == NULL)
 			continue;
 		if (ifa->sa_family == AF_LINK) {
-			if (has_carrier(s, ifd->ifname) == 0) {
+			ifi = (const struct if_data *)p->ifa_data;
+			if (ifi->ifi_link_state == LINK_STATE_UP)
 invoke_script(ifd->ifname, CARRIER, NULL, NULL);
-ifd->last_carrier_status =
-LINK_STATE_UP;
-			}
+			ifd->last_carrier_status = ifi->ifi_link_state;
 			continue;
 		}
 		aflag = check_addrflags(ifa->sa_family, p->ifa_addrflags);



CVS commit: src/usr.sbin/ifwatchd

2020-09-22 Thread Roy Marples
Module Name:src
Committed By:   roy
Date:   Wed Sep 23 02:32:04 UTC 2020

Modified Files:
src/usr.sbin/ifwatchd: ifwatchd.c

Log Message:
ifwatchd: Check link state if no media is supported on initial ups.

We can check this via SIOCGIFDATA and ifi_link_state.


To generate a diff of this commit:
cvs rdiff -u -r1.43 -r1.44 src/usr.sbin/ifwatchd/ifwatchd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ifwatchd/ifwatchd.c
diff -u src/usr.sbin/ifwatchd/ifwatchd.c:1.43 src/usr.sbin/ifwatchd/ifwatchd.c:1.44
--- src/usr.sbin/ifwatchd/ifwatchd.c:1.43	Wed Mar  7 10:06:41 2018
+++ src/usr.sbin/ifwatchd/ifwatchd.c	Wed Sep 23 02:32:04 2020
@@ -1,6 +1,6 @@
-/*	$NetBSD: ifwatchd.c,v 1.43 2018/03/07 10:06:41 roy Exp $	*/
+/*	$NetBSD: ifwatchd.c,v 1.44 2020/09/23 02:32:04 roy Exp $	*/
 #include 
-__RCSID("$NetBSD: ifwatchd.c,v 1.43 2018/03/07 10:06:41 roy Exp $");
+__RCSID("$NetBSD: ifwatchd.c,v 1.44 2020/09/23 02:32:04 roy Exp $");
 
 /*-
  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
@@ -536,6 +536,40 @@ find_interface(int idx)
 	return NULL;
 }
 
+static bool
+has_carrier(int s, const char *ifname)
+{
+	struct ifmediareq ifmr = { .ifm_status = 0 };
+
+	strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
+	if (ioctl(s, SIOCGIFMEDIA, ) == -1) {
+		struct ifdatareq ifdr = { .ifdr_data.ifi_link_state = 0 };
+
+		strlcpy(ifdr.ifdr_name, ifname, sizeof(ifdr.ifdr_name));
+		if (ioctl(s, SIOCGIFDATA, ) == -1) {
+			/* Should not be possible. */
+			return false;
+		}
+		if (ifdr.ifdr_data.ifi_link_state == LINK_STATE_UP)
+			return true;
+		else
+			return false;
+	}
+
+	if (!(ifmr.ifm_status & IFM_AVALID)) {
+		/*
+		 * Interface doesn't report media-valid status.
+		 * assume ok.
+		 */
+		return true;
+	}
+
+	if (ifmr.ifm_status & IFM_ACTIVE)
+		return true;
+	else
+		return false;
+}
+
 static void
 run_initial_ups(void)
 {
@@ -568,18 +602,11 @@ run_initial_ups(void)
 		if (ifa == NULL)
 			continue;
 		if (ifa->sa_family == AF_LINK) {
-			struct ifmediareq ifmr;
-
-			memset(, 0, sizeof(ifmr));
-			strncpy(ifmr.ifm_name, ifd->ifname,
-			sizeof(ifmr.ifm_name));
-			if (ioctl(s, SIOCGIFMEDIA, ) != -1
-			&& (ifmr.ifm_status & IFM_AVALID)
-			&& (ifmr.ifm_status & IFM_ACTIVE)) {
+			if (has_carrier(s, ifd->ifname) == 0) {
 invoke_script(ifd->ifname, CARRIER, NULL, NULL);
 ifd->last_carrier_status =
 LINK_STATE_UP;
-			}
+			}
 			continue;
 		}
 		aflag = check_addrflags(ifa->sa_family, p->ifa_addrflags);



CVS commit: src/usr.sbin/ifwatchd

2019-02-17 Thread David H. Gutteridge
Module Name:src
Committed By:   gutteridge
Date:   Sun Feb 17 20:50:25 UTC 2019

Modified Files:
src/usr.sbin/ifwatchd: ifwatchd.8

Log Message:
ifwatchd(8): remove lingering references to dhclient(8). In this case,
its replacement handles carrier detection itself. Addresses part of
PR misc/53669.


To generate a diff of this commit:
cvs rdiff -u -r1.28 -r1.29 src/usr.sbin/ifwatchd/ifwatchd.8

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ifwatchd/ifwatchd.8
diff -u src/usr.sbin/ifwatchd/ifwatchd.8:1.28 src/usr.sbin/ifwatchd/ifwatchd.8:1.29
--- src/usr.sbin/ifwatchd/ifwatchd.8:1.28	Sun Sep 23 07:24:20 2018
+++ src/usr.sbin/ifwatchd/ifwatchd.8	Sun Feb 17 20:50:25 2019
@@ -1,4 +1,4 @@
-.\" $NetBSD: ifwatchd.8,v 1.28 2018/09/23 07:24:20 maxv Exp $
+.\" $NetBSD: ifwatchd.8,v 1.29 2019/02/17 20:50:25 gutteridge Exp $
 .\"
 .\" Copyright (c) 2001-2003 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -27,7 +27,7 @@
 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd September 22, 2018
+.Dd February 17, 2019
 .Dt IFWATCHD 8
 .Os
 .Sh NAME
@@ -163,33 +163,6 @@ Use
 in your
 .Pa /etc/ifconfig.pppoe0
 file in the on-demand case.
-.Pp
-The next example is for dhclient users.
-.Bd -literal -offset indent
-# ifwatchd -i -c /etc/dhcp/carrier-detect tlp0
-.Ed
-.Pp
-With the above command, the carrier-detect script will be invoked when
-a carrier is detected on the interface
-.Ar tlp0 .
-Note that the
-.Fl i
-flag prevents any action based on the initial state.
-A script like the following should work for most users, although it
-will not work for machines with multiple interfaces running
-.Cm dhclient .
-.Bd -literal -offset indent
-#! /bin/sh
-# Arguments:  ifname tty speed address destination
-# If there is a dhclient already running, kill it.
-# (This step could be put in a distinct no-carrier script,
-# if desired.)
-if [ -f /var/run/dhclient.pid ]; then
-   /bin/kill `/bin/cat /var/run/dhclient.pid`
-fi
-# Start dhclient again on this interface
-/sbin/dhclient $1
-.Ed
 .Sh PARAMETERS PASSED TO SCRIPTS
 The invoked scripts get passed these parameters:
 .Bl -tag -width destination



CVS commit: src/usr.sbin/ifwatchd

2018-03-07 Thread Roy Marples
Module Name:src
Committed By:   roy
Date:   Wed Mar  7 10:06:41 UTC 2018

Modified Files:
src/usr.sbin/ifwatchd: ifwatchd.c

Log Message:
Remove case labels we will never trigger due to filtering.


To generate a diff of this commit:
cvs rdiff -u -r1.42 -r1.43 src/usr.sbin/ifwatchd/ifwatchd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ifwatchd/ifwatchd.c
diff -u src/usr.sbin/ifwatchd/ifwatchd.c:1.42 src/usr.sbin/ifwatchd/ifwatchd.c:1.43
--- src/usr.sbin/ifwatchd/ifwatchd.c:1.42	Tue Apr 11 14:21:35 2017
+++ src/usr.sbin/ifwatchd/ifwatchd.c	Wed Mar  7 10:06:41 2018
@@ -1,6 +1,6 @@
-/*	$NetBSD: ifwatchd.c,v 1.42 2017/04/11 14:21:35 roy Exp $	*/
+/*	$NetBSD: ifwatchd.c,v 1.43 2018/03/07 10:06:41 roy Exp $	*/
 #include 
-__RCSID("$NetBSD: ifwatchd.c,v 1.42 2017/04/11 14:21:35 roy Exp $");
+__RCSID("$NetBSD: ifwatchd.c,v 1.43 2018/03/07 10:06:41 roy Exp $");
 
 /*-
  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
@@ -267,18 +267,8 @@ dispatch(const void *msg, size_t len)
 	case RTM_IFINFO:
 		check_carrier(msg);
 		break;
-	case RTM_ADD:
-	case RTM_DELETE:
-	case RTM_CHANGE:
-	case RTM_LOSING:
-	case RTM_REDIRECT:
-	case RTM_MISS:
-	case RTM_IEEE80211:
-	case RTM_ONEWADDR:
-	case RTM_ODELADDR:
-	case RTM_OCHGADDR:
-		break;
 	default:
+		/* Should be impossible as we filter messages. */
 		if (verbose)
 			printf("unknown message ignored (%d)\n", hd->rtm_type);
 		break;



CVS commit: src/usr.sbin/ifwatchd

2017-04-11 Thread Roy Marples
Module Name:src
Committed By:   roy
Date:   Tue Apr 11 14:21:35 UTC 2017

Modified Files:
src/usr.sbin/ifwatchd: ifwatchd.c

Log Message:
Use RO_MSGFILTER.


To generate a diff of this commit:
cvs rdiff -u -r1.41 -r1.42 src/usr.sbin/ifwatchd/ifwatchd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ifwatchd/ifwatchd.c
diff -u src/usr.sbin/ifwatchd/ifwatchd.c:1.41 src/usr.sbin/ifwatchd/ifwatchd.c:1.42
--- src/usr.sbin/ifwatchd/ifwatchd.c:1.41	Fri Oct  7 15:49:58 2016
+++ src/usr.sbin/ifwatchd/ifwatchd.c	Tue Apr 11 14:21:35 2017
@@ -1,6 +1,6 @@
-/*	$NetBSD: ifwatchd.c,v 1.41 2016/10/07 15:49:58 christos Exp $	*/
+/*	$NetBSD: ifwatchd.c,v 1.42 2017/04/11 14:21:35 roy Exp $	*/
 #include 
-__RCSID("$NetBSD: ifwatchd.c,v 1.41 2016/10/07 15:49:58 christos Exp $");
+__RCSID("$NetBSD: ifwatchd.c,v 1.42 2017/04/11 14:21:35 roy Exp $");
 
 /*-
  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
@@ -108,6 +108,10 @@ main(int argc, char **argv)
 	struct msghdr msg;
 	struct iovec iov[1];
 	char buf[2048];
+	unsigned char msgfilter[] = {
+		RTM_IFINFO, RTM_IFANNOUNCE,
+		RTM_NEWADDR, RTM_DELADDR,
+	};
 
 	openlog(argv[0], LOG_PID|LOG_CONS, LOG_DAEMON);
 	while ((c = getopt(argc, argv, "qvhic:n:u:d:A:D:")) != -1) {
@@ -191,6 +195,9 @@ main(int argc, char **argv)
 		syslog(LOG_ERR, "error opening routing socket: %m");
 		exit(EXIT_FAILURE);
 	}
+	if (setsockopt(s, PF_ROUTE, RO_MSGFILTER,
+	, sizeof(msgfilter)) < 0)
+		syslog(LOG_ERR, "RO_MSGFILTER: %m");
 
 	if (!inhibit_initial)
 		run_initial_ups();



CVS commit: src/usr.sbin/ifwatchd

2016-10-07 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Fri Oct  7 15:49:59 UTC 2016

Modified Files:
src/usr.sbin/ifwatchd: ifwatchd.c

Log Message:
CID 1373516: Missing breaks
Also delete perror, fix fprintf to be syslog.


To generate a diff of this commit:
cvs rdiff -u -r1.40 -r1.41 src/usr.sbin/ifwatchd/ifwatchd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ifwatchd/ifwatchd.c
diff -u src/usr.sbin/ifwatchd/ifwatchd.c:1.40 src/usr.sbin/ifwatchd/ifwatchd.c:1.41
--- src/usr.sbin/ifwatchd/ifwatchd.c:1.40	Thu Oct  6 07:13:57 2016
+++ src/usr.sbin/ifwatchd/ifwatchd.c	Fri Oct  7 11:49:58 2016
@@ -1,6 +1,6 @@
-/*	$NetBSD: ifwatchd.c,v 1.40 2016/10/06 11:13:57 roy Exp $	*/
+/*	$NetBSD: ifwatchd.c,v 1.41 2016/10/07 15:49:58 christos Exp $	*/
 #include 
-__RCSID("$NetBSD: ifwatchd.c,v 1.40 2016/10/06 11:13:57 roy Exp $");
+__RCSID("$NetBSD: ifwatchd.c,v 1.41 2016/10/07 15:49:58 christos Exp $");
 
 /*-
  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
@@ -189,7 +189,6 @@ main(int argc, char **argv)
 	s = socket(PF_ROUTE, SOCK_RAW, 0);
 	if (s < 0) {
 		syslog(LOG_ERR, "error opening routing socket: %m");
-		perror("open routing socket");
 		exit(EXIT_FAILURE);
 	}
 
@@ -358,29 +357,28 @@ invoke_script(const char *ifname, enum e
 
 	addr[0] = daddr[0] = 0;
 	if (sa != NULL) {
+		const struct sockaddr_in *sin;
+		const struct sockaddr_in6 *sin6;
+
 		if (sa->sa_len == 0) {
-			fprintf(stderr,
-			"illegal socket address (sa_len == 0)\n");
+			syslog(LOG_ERR,
+			"illegal socket address (sa_len == 0)");
 			return;
 		}
 		switch (sa->sa_family) {
 		case AF_INET:
-		{
-			const struct sockaddr_in *sin;
-
 			sin = (const struct sockaddr_in *)sa;
 			if (sin->sin_addr.s_addr == INADDR_ANY ||
 			sin->sin_addr.s_addr == INADDR_BROADCAST)
 return;
-		}
+			break;
 		case AF_INET6:
-		{
-			const struct sockaddr_in6 *sin6;
-
 			sin6 = (const struct sockaddr_in6 *)sa;
 			if (IN6_IS_ADDR_LINKLOCAL(>sin6_addr))
 return;
-		}
+			break;
+		default:
+			break;
 		}
 
 		if (getnameinfo(sa, sa->sa_len, addr, sizeof addr, NULL, 0,
@@ -409,14 +407,13 @@ invoke_script(const char *ifname, enum e
 
 	switch (vfork()) {
 	case -1:
-		fprintf(stderr, "cannot fork\n");
+		syslog(LOG_ERR, "cannot fork: %m");
 		break;
 	case 0:
 		if (execl(script, script, ifname, DummyTTY, DummySpeed,
 		addr, daddr, NULL) == -1) {
 			syslog(LOG_ERR, "could not execute \"%s\": %m",
 			script);
-			perror(script);
 		}
 		_exit(EXIT_FAILURE);
 	default:



CVS commit: src/usr.sbin/ifwatchd

2016-10-06 Thread Roy Marples
Module Name:src
Committed By:   roy
Date:   Thu Oct  6 11:13:57 UTC 2016

Modified Files:
src/usr.sbin/ifwatchd: ifwatchd.c

Log Message:
Inhibit initial up should only apply at program start, not for hot plugged 
interfaces while it's running.


To generate a diff of this commit:
cvs rdiff -u -r1.39 -r1.40 src/usr.sbin/ifwatchd/ifwatchd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ifwatchd/ifwatchd.c
diff -u src/usr.sbin/ifwatchd/ifwatchd.c:1.39 src/usr.sbin/ifwatchd/ifwatchd.c:1.40
--- src/usr.sbin/ifwatchd/ifwatchd.c:1.39	Thu Oct  6 11:08:55 2016
+++ src/usr.sbin/ifwatchd/ifwatchd.c	Thu Oct  6 11:13:57 2016
@@ -1,6 +1,6 @@
-/*	$NetBSD: ifwatchd.c,v 1.39 2016/10/06 11:08:55 roy Exp $	*/
+/*	$NetBSD: ifwatchd.c,v 1.40 2016/10/06 11:13:57 roy Exp $	*/
 #include 
-__RCSID("$NetBSD: ifwatchd.c,v 1.39 2016/10/06 11:08:55 roy Exp $");
+__RCSID("$NetBSD: ifwatchd.c,v 1.40 2016/10/06 11:13:57 roy Exp $");
 
 /*-
  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
@@ -470,8 +470,7 @@ check_carrier(const struct if_msghdr *if
 	 * inhibit_initial is not set
 	 */
 	carrier_status = ifm->ifm_data.ifi_link_state;
-	if ((carrier_status != p->last_carrier_status) ||
-	((p->last_carrier_status == -1) && !inhibit_initial)) {
+	if (carrier_status != p->last_carrier_status) {
 		switch (carrier_status) {
 		case LINK_STATE_UP:
 			ev = CARRIER;
@@ -506,7 +505,7 @@ check_announce(const struct if_announcem
 			break;
 		case IFAN_DEPARTURE:
 			p->index = -1;
-			p->last_carrier_status = LINK_STATE_UNKNOWN;
+			p->last_carrier_status = -1;
 			invoke_script(p->ifname, DEPARTURE, NULL, NULL);
 			break;
 		default:



CVS commit: src/usr.sbin/ifwatchd

2016-10-06 Thread Roy Marples
Module Name:src
Committed By:   roy
Date:   Thu Oct  6 11:08:56 UTC 2016

Modified Files:
src/usr.sbin/ifwatchd: ifwatchd.c

Log Message:
Remove rescan_interfaces as the interface announcement message
contains the interface index.


To generate a diff of this commit:
cvs rdiff -u -r1.38 -r1.39 src/usr.sbin/ifwatchd/ifwatchd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ifwatchd/ifwatchd.c
diff -u src/usr.sbin/ifwatchd/ifwatchd.c:1.38 src/usr.sbin/ifwatchd/ifwatchd.c:1.39
--- src/usr.sbin/ifwatchd/ifwatchd.c:1.38	Thu Oct  6 10:33:05 2016
+++ src/usr.sbin/ifwatchd/ifwatchd.c	Thu Oct  6 11:08:55 2016
@@ -1,6 +1,6 @@
-/*	$NetBSD: ifwatchd.c,v 1.38 2016/10/06 10:33:05 roy Exp $	*/
+/*	$NetBSD: ifwatchd.c,v 1.39 2016/10/06 11:08:55 roy Exp $	*/
 #include 
-__RCSID("$NetBSD: ifwatchd.c,v 1.38 2016/10/06 10:33:05 roy Exp $");
+__RCSID("$NetBSD: ifwatchd.c,v 1.39 2016/10/06 11:08:55 roy Exp $");
 
 /*-
  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
@@ -68,7 +68,6 @@ static void invoke_script(const char *if
 static void list_interfaces(const char *ifnames);
 static void check_announce(const struct if_announcemsghdr *ifan);
 static void check_carrier(const struct if_msghdr *ifm);
-static void rescan_interfaces(void);
 static void free_interfaces(void);
 static struct interface_data * find_interface(int index);
 static void run_initial_ups(void);
@@ -257,7 +256,6 @@ dispatch(const void *msg, size_t len)
 		check_addrs(msg);
 		break;
 	case RTM_IFANNOUNCE:
-		rescan_interfaces();
 		check_announce(msg);
 		break;
 	case RTM_IFINFO:
@@ -503,9 +501,12 @@ check_announce(const struct if_announcem
 
 		switch (ifan->ifan_what) {
 		case IFAN_ARRIVAL:
+			p->index = ifan->ifan_index;
 			invoke_script(p->ifname, ARRIVAL, NULL, NULL);
 			break;
 		case IFAN_DEPARTURE:
+			p->index = -1;
+			p->last_carrier_status = LINK_STATE_UNKNOWN;
 			invoke_script(p->ifname, DEPARTURE, NULL, NULL);
 			break;
 		default:
@@ -519,19 +520,6 @@ check_announce(const struct if_announcem
 }
 
 static void
-rescan_interfaces(void)
-{
-	struct interface_data * p;
-
-	SLIST_FOREACH(p, , next) {
-		p->index = if_nametoindex(p->ifname);
-		if (verbose)
-			printf("interface \"%s\" has index %d\n", p->ifname,
-			p->index);
-	}
-}
-
-static void
 free_interfaces(void)
 {
 	struct interface_data * p;



CVS commit: src/usr.sbin/ifwatchd

2016-10-06 Thread Roy Marples
Module Name:src
Committed By:   roy
Date:   Thu Oct  6 10:33:05 UTC 2016

Modified Files:
src/usr.sbin/ifwatchd: ifwatchd.c

Log Message:
Simplify logic - invoke script when address is added and ready or
when removed.


To generate a diff of this commit:
cvs rdiff -u -r1.37 -r1.38 src/usr.sbin/ifwatchd/ifwatchd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ifwatchd/ifwatchd.c
diff -u src/usr.sbin/ifwatchd/ifwatchd.c:1.37 src/usr.sbin/ifwatchd/ifwatchd.c:1.38
--- src/usr.sbin/ifwatchd/ifwatchd.c:1.37	Thu Oct  6 10:30:31 2016
+++ src/usr.sbin/ifwatchd/ifwatchd.c	Thu Oct  6 10:33:05 2016
@@ -1,6 +1,6 @@
-/*	$NetBSD: ifwatchd.c,v 1.37 2016/10/06 10:30:31 roy Exp $	*/
+/*	$NetBSD: ifwatchd.c,v 1.38 2016/10/06 10:33:05 roy Exp $	*/
 #include 
- __RCSID("$NetBSD: ifwatchd.c,v 1.37 2016/10/06 10:30:31 roy Exp $");
+__RCSID("$NetBSD: ifwatchd.c,v 1.38 2016/10/06 10:33:05 roy Exp $");
 
 /*-
  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
@@ -338,14 +338,7 @@ check_addrs(const struct ifa_msghdr *ifa
 	if (ifa != NULL && ifd != NULL) {
 		ev = ifam->ifam_type == RTM_DELADDR ? DOWN : UP;
 		aflag = check_addrflags(ifa->sa_family, ifam->ifam_addrflags);
-		if (ev == UP) {
-			if (aflag == NOTREADY)
-return;
-			if (aflag == DETACHED)
-return;		/* XXX set ev to DOWN? */
-		}
-		if ((ev == UP && aflag == READY) ||
-		(ev == DOWN && aflag == DETACHED /* XXX why DETACHED? */))
+		if ((ev == UP && aflag == READY) || ev == DOWN)
 			invoke_script(ifd->ifname, ev, ifa, brd);
 	}
 }



CVS commit: src/usr.sbin/ifwatchd

2016-10-06 Thread Roy Marples
Module Name:src
Committed By:   roy
Date:   Thu Oct  6 10:30:31 UTC 2016

Modified Files:
src/usr.sbin/ifwatchd: ifwatchd.c

Log Message:
We already know the interface name, so don't bother calling if_indextoname
to work it out again.


To generate a diff of this commit:
cvs rdiff -u -r1.36 -r1.37 src/usr.sbin/ifwatchd/ifwatchd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ifwatchd/ifwatchd.c
diff -u src/usr.sbin/ifwatchd/ifwatchd.c:1.36 src/usr.sbin/ifwatchd/ifwatchd.c:1.37
--- src/usr.sbin/ifwatchd/ifwatchd.c:1.36	Thu Sep 29 15:25:28 2016
+++ src/usr.sbin/ifwatchd/ifwatchd.c	Thu Oct  6 10:30:31 2016
@@ -1,6 +1,6 @@
-/*	$NetBSD: ifwatchd.c,v 1.36 2016/09/29 15:25:28 roy Exp $	*/
+/*	$NetBSD: ifwatchd.c,v 1.37 2016/10/06 10:30:31 roy Exp $	*/
 #include 
- __RCSID("$NetBSD: ifwatchd.c,v 1.36 2016/09/29 15:25:28 roy Exp $");
+ __RCSID("$NetBSD: ifwatchd.c,v 1.37 2016/10/06 10:30:31 roy Exp $");
 
 /*-
  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
@@ -63,8 +63,8 @@ __dead static void usage(void);
 static void dispatch(const void *, size_t);
 static enum addrflag check_addrflags(int af, int addrflags);
 static void check_addrs(const struct ifa_msghdr *ifam);
-static void invoke_script(const struct sockaddr *sa, const struct sockaddr *dst,
-enum event ev, int ifindex, const char *ifname_hint);
+static void invoke_script(const char *ifname, enum event ev,
+const struct sockaddr *sa, const struct sockaddr *dst);
 static void list_interfaces(const char *ifnames);
 static void check_announce(const struct if_announcemsghdr *ifan);
 static void check_carrier(const struct if_msghdr *ifm);
@@ -346,19 +346,26 @@ check_addrs(const struct ifa_msghdr *ifa
 		}
 		if ((ev == UP && aflag == READY) ||
 		(ev == DOWN && aflag == DETACHED /* XXX why DETACHED? */))
-			invoke_script(ifa, brd, ev, ifd->index, ifd->ifname);
+			invoke_script(ifd->ifname, ev, ifa, brd);
 	}
 }
 
 static void
-invoke_script(const struct sockaddr *sa, const struct sockaddr *dest,
-enum event ev, int ifindex, const char *ifname_hint)
+invoke_script(const char *ifname, enum event ev,
+const struct sockaddr *sa, const struct sockaddr *dest)
 {
-	char addr[NI_MAXHOST], daddr[NI_MAXHOST], ifname_buf[IFNAMSIZ];
-	const char * volatile ifname;
+	char addr[NI_MAXHOST], daddr[NI_MAXHOST];
 	const char *script;
 	int status;
 
+	if (ifname == NULL)
+		return;
+
+	script = *scripts[ev];
+	if (script == NULL)
+		return;
+
+	addr[0] = daddr[0] = 0;
 	if (sa != NULL) {
 		if (sa->sa_len == 0) {
 			fprintf(stderr,
@@ -384,15 +391,7 @@ invoke_script(const struct sockaddr *sa,
 return;
 		}
 		}
-	}
 
-	addr[0] = daddr[0] = 0;
-	ifname = if_indextoname(ifindex, ifname_buf);
-	ifname = ifname ? ifname : ifname_hint;
-	if (ifname == NULL)
-		return;
-
-	if (sa != NULL) {
 		if (getnameinfo(sa, sa->sa_len, addr, sizeof addr, NULL, 0,
 		NI_NUMERICHOST)) {
 			if (verbose)
@@ -400,6 +399,7 @@ invoke_script(const struct sockaddr *sa,
 			return;	/* this address can not be handled */
 		}
 	}
+
 	if (dest != NULL) {
 		if (getnameinfo(dest, dest->sa_len, daddr, sizeof daddr,
 		NULL, 0, NI_NUMERICHOST)) {
@@ -409,9 +409,6 @@ invoke_script(const struct sockaddr *sa,
 		}
 	}
 
-	script = *scripts[ev];
-	if (script == NULL) return;
-
 	if (verbose)
 		(void) printf("calling: %s %s %s %s %s %s\n",
 		script, ifname, DummyTTY, DummySpeed, addr, daddr);
@@ -496,7 +493,7 @@ check_carrier(const struct if_msghdr *if
 printf("unknown link status ignored\n");
 			return;
 		}
-		invoke_script(NULL, NULL, ev, ifm->ifm_index, p->ifname);
+		invoke_script(p->ifname, ev, NULL, NULL);
 		p->last_carrier_status = carrier_status;
 	}
 }
@@ -513,12 +510,10 @@ check_announce(const struct if_announcem
 
 		switch (ifan->ifan_what) {
 		case IFAN_ARRIVAL:
-			invoke_script(NULL, NULL, ARRIVAL, p->index,
-			NULL);
+			invoke_script(p->ifname, ARRIVAL, NULL, NULL);
 			break;
 		case IFAN_DEPARTURE:
-			invoke_script(NULL, NULL, DEPARTURE, p->index,
-			p->ifname);
+			invoke_script(p->ifname, DEPARTURE, NULL, NULL);
 			break;
 		default:
 			if (verbose)
@@ -592,8 +587,7 @@ run_initial_ups(void)
 
 		ifa = p->ifa_addr;
 		if (ifa != NULL && ifa->sa_family == AF_LINK)
-			invoke_script(NULL, NULL, ARRIVAL, ifd->index,
-			NULL);
+			invoke_script(ifd->ifname, ARRIVAL, NULL, NULL);
 
 		if ((p->ifa_flags & IFF_UP) == 0)
 			continue;
@@ -608,8 +602,7 @@ run_initial_ups(void)
 			if (ioctl(s, SIOCGIFMEDIA, ) != -1
 			&& (ifmr.ifm_status & IFM_AVALID)
 			&& (ifmr.ifm_status & IFM_ACTIVE)) {
-invoke_script(NULL, NULL, CARRIER,
-ifd->index, ifd->ifname);
+invoke_script(ifd->ifname, CARRIER, NULL, NULL);
 ifd->last_carrier_status =
 LINK_STATE_UP;
 			}
@@ -618,7 +611,7 @@ run_initial_ups(void)
 		aflag = check_addrflags(ifa->sa_family, p->ifa_addrflags);
 		if (aflag != 

CVS commit: src/usr.sbin/ifwatchd

2016-09-29 Thread Roy Marples
Module Name:src
Committed By:   roy
Date:   Thu Sep 29 15:25:28 UTC 2016

Modified Files:
src/usr.sbin/ifwatchd: ifwatchd.c

Log Message:
Sprinkle some RCSID loving.


To generate a diff of this commit:
cvs rdiff -u -r1.35 -r1.36 src/usr.sbin/ifwatchd/ifwatchd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ifwatchd/ifwatchd.c
diff -u src/usr.sbin/ifwatchd/ifwatchd.c:1.35 src/usr.sbin/ifwatchd/ifwatchd.c:1.36
--- src/usr.sbin/ifwatchd/ifwatchd.c:1.35	Thu Sep 29 15:21:09 2016
+++ src/usr.sbin/ifwatchd/ifwatchd.c	Thu Sep 29 15:25:28 2016
@@ -1,4 +1,6 @@
-/*	$NetBSD: ifwatchd.c,v 1.35 2016/09/29 15:21:09 roy Exp $	*/
+/*	$NetBSD: ifwatchd.c,v 1.36 2016/09/29 15:25:28 roy Exp $	*/
+#include 
+ __RCSID("$NetBSD: ifwatchd.c,v 1.36 2016/09/29 15:25:28 roy Exp $");
 
 /*-
  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.



CVS commit: src/usr.sbin/ifwatchd

2016-09-29 Thread Roy Marples
Module Name:src
Committed By:   roy
Date:   Thu Sep 29 15:21:09 UTC 2016

Modified Files:
src/usr.sbin/ifwatchd: ifwatchd.c

Log Message:
Now that sppp announces address additions and removals with state
flags, we no longer need the custom sppp code to detect if we are
connected or not.

Add commentary on if we really need to handle RTM_DELADDR only when
detached.


To generate a diff of this commit:
cvs rdiff -u -r1.34 -r1.35 src/usr.sbin/ifwatchd/ifwatchd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ifwatchd/ifwatchd.c
diff -u src/usr.sbin/ifwatchd/ifwatchd.c:1.34 src/usr.sbin/ifwatchd/ifwatchd.c:1.35
--- src/usr.sbin/ifwatchd/ifwatchd.c:1.34	Thu Sep 29 13:36:30 2016
+++ src/usr.sbin/ifwatchd/ifwatchd.c	Thu Sep 29 15:21:09 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: ifwatchd.c,v 1.34 2016/09/29 13:36:30 roy Exp $	*/
+/*	$NetBSD: ifwatchd.c,v 1.35 2016/09/29 15:21:09 roy Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
@@ -29,11 +29,6 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-/*
- * Define this for special treatment of sys/net/if_spppsubr.c based interfaces.
- */
-#define SPPP_IF_SUPPORT
-
 #include 
 #include 
 #include 
@@ -43,9 +38,6 @@
 #include 
 #include 
 #include 
-#ifdef SPPP_IF_SUPPORT
-#include 
-#endif
 #include 
 #include 
 #include 
@@ -79,15 +71,6 @@ static void free_interfaces(void);
 static struct interface_data * find_interface(int index);
 static void run_initial_ups(void);
 
-#ifdef SPPP_IF_SUPPORT
-static int check_is_connected(const char * ifname, int def_retvalue);
-#define if_is_connected(X)	(check_is_connected((X), 1))
-#define if_is_not_connected(X)	(!check_is_connected((X), 0))
-#else
-#define	if_is_connected(X)	1
-#define	if_is_not_connected(X)	1
-#endif
-
 /* global variables */
 static int verbose = 0, quiet = 0;
 static int inhibit_initial = 0;
@@ -359,8 +342,8 @@ check_addrs(const struct ifa_msghdr *ifa
 			if (aflag == DETACHED)
 return;		/* XXX set ev to DOWN? */
 		}
-		if ((ev == UP && if_is_connected(ifd->ifname)) ||
-		(ev == DOWN && if_is_not_connected(ifd->ifname)))
+		if ((ev == UP && aflag == READY) ||
+		(ev == DOWN && aflag == DETACHED /* XXX why DETACHED? */))
 			invoke_script(ifa, brd, ev, ifd->index, ifd->ifname);
 	}
 }
@@ -633,55 +616,9 @@ run_initial_ups(void)
 		aflag = check_addrflags(ifa->sa_family, p->ifa_addrflags);
 		if (aflag != READY)
 			continue;
-		if (if_is_connected(ifd->ifname))
-			invoke_script(ifa, p->ifa_dstaddr, UP,
-			ifd->index, ifd->ifname);
+		invoke_script(ifa, p->ifa_dstaddr, UP, ifd->index, ifd->ifname);
 	}
 	freeifaddrs(res);
 out:
 	close(s);
 }
-
-#ifdef SPPP_IF_SUPPORT
-/*
- * Special case support for in-kernel PPP interfaces.
- * If these are IFF_UP, but have not yet connected or completed authentication
- * we don't want to call the up script in the initial interface scan (there
- * will be an UP event generated later, when IPCP completes, anyway).
- *
- * If this is no if_spppsubr.c based interface, this ioctl just fails and we
- * treat is as connected.
- */
-static int
-check_is_connected(const char *ifname, int def_retval)
-{
-	int s, error;
-	struct spppstatus oldstatus;
-	struct spppstatusncp status;
-
-	memset(, 0, sizeof status);
-	strncpy(status.ifname, ifname, sizeof status.ifname);
-	memset(, 0, sizeof oldstatus);
-	strncpy(oldstatus.ifname, ifname, sizeof oldstatus.ifname);
-
-	s = socket(AF_INET, SOCK_DGRAM, 0);
-	if (s < 0)
-		return 1;	/* no idea how to handle this... */
-	error = ioctl(s, SPPPGETSTATUSNCP, );
-	if (error != 0) {
-		error = ioctl(s, SPPPGETSTATUS, );
-		if (error != 0) {
-			/* not if_spppsubr.c based - return default */
-			close(s);
-			return def_retval;
-		} else {
-			/* can't query NCPs, so use default */
-			status.phase = oldstatus.phase;
-			status.ncpup = def_retval;
-		}
-	}
-	close(s);
-
-	return status.phase == SPPP_PHASE_NETWORK && status.ncpup > 0;
-}
-#endif



CVS commit: src/usr.sbin/ifwatchd

2016-09-21 Thread Roy Marples
Module Name:src
Committed By:   roy
Date:   Wed Sep 21 21:07:29 UTC 2016

Modified Files:
src/usr.sbin/ifwatchd: ifwatchd.c

Log Message:
Check address is not tentative, duplicated or detached before running
scripts.

XXX Do we want new script actions for detached or duplicated addresses?


To generate a diff of this commit:
cvs rdiff -u -r1.32 -r1.33 src/usr.sbin/ifwatchd/ifwatchd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ifwatchd/ifwatchd.c
diff -u src/usr.sbin/ifwatchd/ifwatchd.c:1.32 src/usr.sbin/ifwatchd/ifwatchd.c:1.33
--- src/usr.sbin/ifwatchd/ifwatchd.c:1.32	Wed Sep 21 20:31:31 2016
+++ src/usr.sbin/ifwatchd/ifwatchd.c	Wed Sep 21 21:07:29 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: ifwatchd.c,v 1.32 2016/09/21 20:31:31 roy Exp $	*/
+/*	$NetBSD: ifwatchd.c,v 1.33 2016/09/21 21:07:29 roy Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
@@ -48,6 +48,7 @@
 #endif
 #include 
 #include 
+#include 
 #include 
 
 #include 
@@ -61,10 +62,12 @@
 #include 
 
 enum event { ARRIVAL, DEPARTURE, UP, DOWN, CARRIER, NO_CARRIER };
+enum addrflag { NOTREADY, DETACHED, READY };
 
 /* local functions */
 __dead static void usage(void);
 static void dispatch(const void *, size_t);
+static enum addrflag check_addrflags(int af, int addrflags);
 static void check_addrs(const struct ifa_msghdr *ifam);
 static void invoke_script(const struct sockaddr *sa, const struct sockaddr *dst,
 enum event ev, int ifindex, const char *ifname_hint);
@@ -293,6 +296,27 @@ dispatch(const void *msg, size_t len)
 	}
 }
 
+static enum addrflag
+check_addrflags(int af, int addrflags)
+{
+
+	switch (af) {
+	case AF_INET:
+		if (addrflags & IN_IFF_NOTREADY)
+			return NOTREADY;
+		if (addrflags & IN_IFF_DETACHED)
+			return DETACHED;
+		break;
+	case AF_INET6:
+		if (addrflags & IN6_IFF_NOTREADY)
+			return NOTREADY;
+		if (addrflags & IN6_IFF_DETACHED)
+			return DETACHED;
+		break;
+	}
+	return READY;
+}
+
 static void
 check_addrs(const struct ifa_msghdr *ifam)
 {
@@ -300,6 +324,7 @@ check_addrs(const struct ifa_msghdr *ifa
 	const struct sockaddr *sa, *ifa = NULL, *brd = NULL;
 	unsigned i;
 	struct interface_data *ifd = NULL;
+	int aflag;
 	enum event ev;
 
 	if (ifam->ifam_addrs == 0)
@@ -327,6 +352,13 @@ check_addrs(const struct ifa_msghdr *ifa
 	}
 	if (ifa != NULL && ifd != NULL) {
 		ev = ifam->ifam_type == RTM_DELADDR ? DOWN : UP;
+		aflag = check_addrflags(ifa->sa_family, ifam->ifam_addrflags);
+		if (ev == UP) {
+			if (aflag == NOTREADY)
+return;
+			if (aflag == DETACHED)
+return;		/* XXX set ev to DOWN? */
+		}
 		if ((ev == UP && if_is_connected(ifd->ifname)) ||
 		(ev == DOWN && if_is_not_connected(ifd->ifname)))
 			invoke_script(ifa, brd, ev, ifd->index, ifd->ifname);
@@ -540,7 +572,8 @@ run_initial_ups(void)
 {
 	struct interface_data * ifd;
 	struct ifaddrs *res = NULL, *p;
-	int s;
+	struct sockaddr *ifa;
+	int s, aflag;
 
 	s = socket(AF_INET, SOCK_DGRAM, 0);
 	if (s < 0)
@@ -557,15 +590,16 @@ run_initial_ups(void)
 		if (ifd == NULL)
 			continue;
 
-		if (p->ifa_addr && p->ifa_addr->sa_family == AF_LINK)
+		ifa = p->ifa_addr;
+		if (ifa != NULL && ifa->sa_family == AF_LINK)
 			invoke_script(NULL, NULL, ARRIVAL, ifd->index,
 			NULL);
 
 		if ((p->ifa_flags & IFF_UP) == 0)
 			continue;
-		if (p->ifa_addr == NULL)
+		if (ifa == NULL)
 			continue;
-		if (p->ifa_addr->sa_family == AF_LINK) {
+		if (ifa->sa_family == AF_LINK) {
 			struct ifmediareq ifmr;
 
 			memset(, 0, sizeof(ifmr));
@@ -581,8 +615,11 @@ run_initial_ups(void)
 			}
 			continue;
 		}
+		aflag = check_addrflags(ifa->sa_family, p->ifa_addrflags);
+		if (aflag != READY)
+			continue;
 		if (if_is_connected(ifd->ifname))
-			invoke_script(p->ifa_addr, p->ifa_dstaddr, UP,
+			invoke_script(ifa, p->ifa_dstaddr, UP,
 			ifd->index, ifd->ifname);
 	}
 	freeifaddrs(res);



CVS commit: src/usr.sbin/ifwatchd

2016-09-21 Thread Roy Marples
Module Name:src
Committed By:   roy
Date:   Wed Sep 21 20:31:32 UTC 2016

Modified Files:
src/usr.sbin/ifwatchd: ifwatchd.c

Log Message:
We should always know the interface the address message was for.


To generate a diff of this commit:
cvs rdiff -u -r1.31 -r1.32 src/usr.sbin/ifwatchd/ifwatchd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ifwatchd/ifwatchd.c
diff -u src/usr.sbin/ifwatchd/ifwatchd.c:1.31 src/usr.sbin/ifwatchd/ifwatchd.c:1.32
--- src/usr.sbin/ifwatchd/ifwatchd.c:1.31	Wed Sep 21 18:18:10 2016
+++ src/usr.sbin/ifwatchd/ifwatchd.c	Wed Sep 21 20:31:31 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: ifwatchd.c,v 1.31 2016/09/21 18:18:10 roy Exp $	*/
+/*	$NetBSD: ifwatchd.c,v 1.32 2016/09/21 20:31:31 roy Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
@@ -73,7 +73,7 @@ static void check_announce(const struct 
 static void check_carrier(const struct if_msghdr *ifm);
 static void rescan_interfaces(void);
 static void free_interfaces(void);
-static int find_interface(int index);
+static struct interface_data * find_interface(int index);
 static void run_initial_ups(void);
 
 #ifdef SPPP_IF_SUPPORT
@@ -298,10 +298,8 @@ check_addrs(const struct ifa_msghdr *ifa
 {
 	const char *cp = (const char *)(ifam + 1);
 	const struct sockaddr *sa, *ifa = NULL, *brd = NULL;
-	char ifname_buf[IFNAMSIZ];
-	const char *ifname;
-	int ifndx = 0;
 	unsigned i;
+	struct interface_data *ifd = NULL;
 	enum event ev;
 
 	if (ifam->ifam_addrs == 0)
@@ -314,10 +312,11 @@ check_addrs(const struct ifa_msghdr *ifa
 			const struct sockaddr_dl *li;
 
 			li = (const struct sockaddr_dl *)sa;
-			ifndx = li->sdl_index;
-			if (!find_interface(ifndx)) {
+			if ((ifd = find_interface(li->sdl_index)) == NULL) {
 if (verbose)
-	printf("ignoring change on interface #%d\n", ifndx);
+	printf("ignoring change"
+	" on interface #%d\n",
+	li->sdl_index);
 return;
 			}
 		} else if (i == RTA_IFA)
@@ -326,18 +325,11 @@ check_addrs(const struct ifa_msghdr *ifa
 			brd = sa;
 		RT_ADVANCE(cp, sa);
 	}
-	if (ifa != NULL) {
+	if (ifa != NULL && ifd != NULL) {
 		ev = ifam->ifam_type == RTM_DELADDR ? DOWN : UP;
-		ifname = if_indextoname(ifndx, ifname_buf);
-		if (ifname == NULL)
-			invoke_script(ifa, brd, ev, ifndx, ifname);
-		else if (ev == UP) {
-			if (if_is_connected(ifname))
-invoke_script(ifa, brd, ev, ifndx, ifname);
-		} else if (ev == DOWN) {
-			if (if_is_not_connected(ifname))
-invoke_script(ifa, brd, ev, ifndx, ifname);
-		}
+		if ((ev == UP && if_is_connected(ifd->ifname)) ||
+		(ev == DOWN && if_is_not_connected(ifd->ifname)))
+			invoke_script(ifa, brd, ev, ifd->index, ifd->ifname);
 	}
 }
 
@@ -532,15 +524,15 @@ free_interfaces(void)
 	}
 }
 
-static int
+static struct interface_data *
 find_interface(int idx)
 {
 	struct interface_data * p;
 
 	SLIST_FOREACH(p, , next)
 		if (p->index == idx)
-			return 1;
-	return 0;
+			return p;
+	return NULL;
 }
 
 static void



CVS commit: src/usr.sbin/ifwatchd

2016-09-21 Thread Roy Marples
Module Name:src
Committed By:   roy
Date:   Wed Sep 21 18:18:10 UTC 2016

Modified Files:
src/usr.sbin/ifwatchd: ifwatchd.c

Log Message:
Add some consistency for dispatching  always send the whole message.


To generate a diff of this commit:
cvs rdiff -u -r1.30 -r1.31 src/usr.sbin/ifwatchd/ifwatchd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ifwatchd/ifwatchd.c
diff -u src/usr.sbin/ifwatchd/ifwatchd.c:1.30 src/usr.sbin/ifwatchd/ifwatchd.c:1.31
--- src/usr.sbin/ifwatchd/ifwatchd.c:1.30	Wed Sep 21 16:47:35 2016
+++ src/usr.sbin/ifwatchd/ifwatchd.c	Wed Sep 21 18:18:10 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: ifwatchd.c,v 1.30 2016/09/21 16:47:35 roy Exp $	*/
+/*	$NetBSD: ifwatchd.c,v 1.31 2016/09/21 18:18:10 roy Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
@@ -65,12 +65,12 @@ enum event { ARRIVAL, DEPARTURE, UP, DOW
 /* local functions */
 __dead static void usage(void);
 static void dispatch(const void *, size_t);
-static void check_addrs(const char *cp, int addrs, enum event ev);
+static void check_addrs(const struct ifa_msghdr *ifam);
 static void invoke_script(const struct sockaddr *sa, const struct sockaddr *dst,
 enum event ev, int ifindex, const char *ifname_hint);
 static void list_interfaces(const char *ifnames);
 static void check_announce(const struct if_announcemsghdr *ifan);
-static void check_carrier(int if_index, int carrier);
+static void check_carrier(const struct if_msghdr *ifm);
 static void rescan_interfaces(void);
 static void free_interfaces(void);
 static int find_interface(int index);
@@ -259,28 +259,22 @@ static void
 dispatch(const void *msg, size_t len)
 {
 	const struct rt_msghdr *hd = msg;
-	const struct if_msghdr *ifmp;
-	const struct ifa_msghdr *ifam;
-	enum event ev;
 
 	if (hd->rtm_version != RTM_VERSION)
 		return;
 
 	switch (hd->rtm_type) {
 	case RTM_NEWADDR:
-		ev = UP;
-		goto work;
 	case RTM_DELADDR:
-		ev = DOWN;
-		goto work;
+		check_addrs(msg);
+		break;
 	case RTM_IFANNOUNCE:
 		rescan_interfaces();
 		check_announce(msg);
-		return;
+		break;
 	case RTM_IFINFO:
-		ifmp = (const struct if_msghdr*)msg;
-		check_carrier(ifmp->ifm_index, ifmp->ifm_data.ifi_link_state);
-		return;
+		check_carrier(msg);
+		break;
 	case RTM_ADD:
 	case RTM_DELETE:
 	case RTM_CHANGE:
@@ -291,30 +285,29 @@ dispatch(const void *msg, size_t len)
 	case RTM_ONEWADDR:
 	case RTM_ODELADDR:
 	case RTM_OCHGADDR:
-		return;
+		break;
+	default:
+		if (verbose)
+			printf("unknown message ignored (%d)\n", hd->rtm_type);
+		break;
 	}
-	if (verbose)
-		printf("unknown message ignored (%d)\n", hd->rtm_type);
-	return;
-
-work:
-	ifam = (const struct ifa_msghdr *)msg;
-	check_addrs((const char *)(ifam + 1), ifam->ifam_addrs, ev);
 }
 
 static void
-check_addrs(const char *cp, int addrs, enum event ev)
+check_addrs(const struct ifa_msghdr *ifam)
 {
+	const char *cp = (const char *)(ifam + 1);
 	const struct sockaddr *sa, *ifa = NULL, *brd = NULL;
 	char ifname_buf[IFNAMSIZ];
 	const char *ifname;
 	int ifndx = 0;
 	unsigned i;
+	enum event ev;
 
-	if (addrs == 0)
+	if (ifam->ifam_addrs == 0)
 		return;
 	for (i = 1; i; i <<= 1) {
-		if ((i & addrs) == 0)
+		if ((i & ifam->ifam_addrs) == 0)
 			continue;
 		sa = (const struct sockaddr *)cp;
 		if (i == RTA_IFP) {
@@ -334,8 +327,9 @@ check_addrs(const char *cp, int addrs, e
 		RT_ADVANCE(cp, sa);
 	}
 	if (ifa != NULL) {
+		ev = ifam->ifam_type == RTM_DELADDR ? DOWN : UP;
 		ifname = if_indextoname(ifndx, ifname_buf);
-		if (ifname == NULL || ev < UP)
+		if (ifname == NULL)
 			invoke_script(ifa, brd, ev, ifndx, ifname);
 		else if (ev == UP) {
 			if (if_is_connected(ifname))
@@ -444,13 +438,14 @@ list_interfaces(const char *ifnames)
 }
 
 static void
-check_carrier(int if_index, int carrier_status)
+check_carrier(const struct if_msghdr *ifm)
 {
 	struct interface_data * p;
+	int carrier_status;
 	enum event ev;
 
 	SLIST_FOREACH(p, , next)
-		if (p->index == if_index)
+		if (p->index == ifm->ifm_index)
 			break;
 
 	if (p == NULL)
@@ -462,7 +457,7 @@ check_carrier(int if_index, int carrier_
 	 * - this is the first time we've been called, and
 	 * inhibit_initial is not set
 	 */
-
+	carrier_status = ifm->ifm_data.ifi_link_state;
 	if ((carrier_status != p->last_carrier_status) ||
 	((p->last_carrier_status == -1) && !inhibit_initial)) {
 		switch (carrier_status) {
@@ -477,7 +472,7 @@ check_carrier(int if_index, int carrier_
 printf("unknown link status ignored\n");
 			return;
 		}
-		invoke_script(NULL, NULL, ev, if_index, p->ifname);
+		invoke_script(NULL, NULL, ev, ifm->ifm_index, p->ifname);
 		p->last_carrier_status = carrier_status;
 	}
 }



CVS commit: src/usr.sbin/ifwatchd

2016-09-21 Thread Roy Marples
Module Name:src
Committed By:   roy
Date:   Wed Sep 21 16:47:35 UTC 2016

Modified Files:
src/usr.sbin/ifwatchd: ifwatchd.c

Log Message:
Sprinkle some const.


To generate a diff of this commit:
cvs rdiff -u -r1.29 -r1.30 src/usr.sbin/ifwatchd/ifwatchd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ifwatchd/ifwatchd.c
diff -u src/usr.sbin/ifwatchd/ifwatchd.c:1.29 src/usr.sbin/ifwatchd/ifwatchd.c:1.30
--- src/usr.sbin/ifwatchd/ifwatchd.c:1.29	Wed Sep 21 14:50:48 2016
+++ src/usr.sbin/ifwatchd/ifwatchd.c	Wed Sep 21 16:47:35 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: ifwatchd.c,v 1.29 2016/09/21 14:50:48 roy Exp $	*/
+/*	$NetBSD: ifwatchd.c,v 1.30 2016/09/21 16:47:35 roy Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
@@ -64,11 +64,12 @@ enum event { ARRIVAL, DEPARTURE, UP, DOW
 
 /* local functions */
 __dead static void usage(void);
-static void dispatch(void*, size_t);
-static void check_addrs(char *cp, int addrs, enum event ev);
-static void invoke_script(struct sockaddr *sa, struct sockaddr *dst, enum event ev, int ifindex, const char *ifname_hint);
+static void dispatch(const void *, size_t);
+static void check_addrs(const char *cp, int addrs, enum event ev);
+static void invoke_script(const struct sockaddr *sa, const struct sockaddr *dst,
+enum event ev, int ifindex, const char *ifname_hint);
 static void list_interfaces(const char *ifnames);
-static void check_announce(struct if_announcemsghdr *ifan);
+static void check_announce(const struct if_announcemsghdr *ifan);
 static void check_carrier(int if_index, int carrier);
 static void rescan_interfaces(void);
 static void free_interfaces(void);
@@ -255,11 +256,11 @@ usage(void)
 }
 
 static void
-dispatch(void *msg, size_t len)
+dispatch(const void *msg, size_t len)
 {
-	struct rt_msghdr *hd = msg;
-	struct if_msghdr *ifmp;
-	struct ifa_msghdr *ifam;
+	const struct rt_msghdr *hd = msg;
+	const struct if_msghdr *ifmp;
+	const struct ifa_msghdr *ifam;
 	enum event ev;
 
 	if (hd->rtm_version != RTM_VERSION)
@@ -274,10 +275,10 @@ dispatch(void *msg, size_t len)
 		goto work;
 	case RTM_IFANNOUNCE:
 		rescan_interfaces();
-		check_announce((struct if_announcemsghdr *)msg);
+		check_announce(msg);
 		return;
 	case RTM_IFINFO:
-		ifmp = (struct if_msghdr*)msg;
+		ifmp = (const struct if_msghdr*)msg;
 		check_carrier(ifmp->ifm_index, ifmp->ifm_data.ifi_link_state);
 		return;
 	case RTM_ADD:
@@ -297,14 +298,14 @@ dispatch(void *msg, size_t len)
 	return;
 
 work:
-	ifam = (struct ifa_msghdr *)msg;
-	check_addrs((char *)(ifam + 1), ifam->ifam_addrs, ev);
+	ifam = (const struct ifa_msghdr *)msg;
+	check_addrs((const char *)(ifam + 1), ifam->ifam_addrs, ev);
 }
 
 static void
-check_addrs(char *cp, int addrs, enum event ev)
+check_addrs(const char *cp, int addrs, enum event ev)
 {
-	struct sockaddr *sa, *ifa = NULL, *brd = NULL;
+	const struct sockaddr *sa, *ifa = NULL, *brd = NULL;
 	char ifname_buf[IFNAMSIZ];
 	const char *ifname;
 	int ifndx = 0;
@@ -315,9 +316,11 @@ check_addrs(char *cp, int addrs, enum ev
 	for (i = 1; i; i <<= 1) {
 		if ((i & addrs) == 0)
 			continue;
-		sa = (struct sockaddr *)cp;
+		sa = (const struct sockaddr *)cp;
 		if (i == RTA_IFP) {
-			struct sockaddr_dl * li = (struct sockaddr_dl*)sa;
+			const struct sockaddr_dl *li;
+
+			li = (const struct sockaddr_dl *)sa;
 			ifndx = li->sdl_index;
 			if (!find_interface(ifndx)) {
 if (verbose)
@@ -345,8 +348,8 @@ check_addrs(char *cp, int addrs, enum ev
 }
 
 static void
-invoke_script(struct sockaddr *sa, struct sockaddr *dest, enum event ev,
-int ifindex, const char *ifname_hint)
+invoke_script(const struct sockaddr *sa, const struct sockaddr *dest,
+enum event ev, int ifindex, const char *ifname_hint)
 {
 	char addr[NI_MAXHOST], daddr[NI_MAXHOST], ifname_buf[IFNAMSIZ];
 	const char * volatile ifname;
@@ -360,7 +363,7 @@ invoke_script(struct sockaddr *sa, struc
 	if (sa != NULL && sa->sa_family == AF_INET6) {
 		struct sockaddr_in6 sin6;
 
-		(void) memcpy(, (struct sockaddr_in6 *)sa, sizeof (sin6));
+		memcpy(, (const struct sockaddr_in6 *)sa, sizeof (sin6));
 		if (IN6_IS_ADDR_LINKLOCAL(_addr))
 			return;
 	}
@@ -480,7 +483,7 @@ check_carrier(int if_index, int carrier_
 }
 
 static void
-check_announce(struct if_announcemsghdr *ifan)
+check_announce(const struct if_announcemsghdr *ifan)
 {
 	struct interface_data * p;
 	const char *ifname = ifan->ifan_name;



CVS commit: src/usr.sbin/ifwatchd

2016-09-21 Thread Roy Marples
Module Name:src
Committed By:   roy
Date:   Wed Sep 21 14:50:48 UTC 2016

Modified Files:
src/usr.sbin/ifwatchd: ifwatchd.c

Log Message:
Skip message if not our RTM_VERSION and silently ignore old message types.


To generate a diff of this commit:
cvs rdiff -u -r1.28 -r1.29 src/usr.sbin/ifwatchd/ifwatchd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ifwatchd/ifwatchd.c
diff -u src/usr.sbin/ifwatchd/ifwatchd.c:1.28 src/usr.sbin/ifwatchd/ifwatchd.c:1.29
--- src/usr.sbin/ifwatchd/ifwatchd.c:1.28	Wed Sep 21 14:46:55 2016
+++ src/usr.sbin/ifwatchd/ifwatchd.c	Wed Sep 21 14:50:48 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: ifwatchd.c,v 1.28 2016/09/21 14:46:55 roy Exp $	*/
+/*	$NetBSD: ifwatchd.c,v 1.29 2016/09/21 14:50:48 roy Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
@@ -262,6 +262,9 @@ dispatch(void *msg, size_t len)
 	struct ifa_msghdr *ifam;
 	enum event ev;
 
+	if (hd->rtm_version != RTM_VERSION)
+		return;
+
 	switch (hd->rtm_type) {
 	case RTM_NEWADDR:
 		ev = UP;
@@ -284,6 +287,9 @@ dispatch(void *msg, size_t len)
 	case RTM_REDIRECT:
 	case RTM_MISS:
 	case RTM_IEEE80211:
+	case RTM_ONEWADDR:
+	case RTM_ODELADDR:
+	case RTM_OCHGADDR:
 		return;
 	}
 	if (verbose)



CVS commit: src/usr.sbin/ifwatchd

2016-09-21 Thread Roy Marples
Module Name:src
Committed By:   roy
Date:   Wed Sep 21 14:46:55 UTC 2016

Modified Files:
src/usr.sbin/ifwatchd: ifwatchd.c

Log Message:
Use recvmsg to ensure we get every message rather than potentially overflowing 
our buffer.


To generate a diff of this commit:
cvs rdiff -u -r1.27 -r1.28 src/usr.sbin/ifwatchd/ifwatchd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ifwatchd/ifwatchd.c
diff -u src/usr.sbin/ifwatchd/ifwatchd.c:1.27 src/usr.sbin/ifwatchd/ifwatchd.c:1.28
--- src/usr.sbin/ifwatchd/ifwatchd.c:1.27	Wed Jan 27 18:55:51 2016
+++ src/usr.sbin/ifwatchd/ifwatchd.c	Wed Sep 21 14:46:55 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: ifwatchd.c,v 1.27 2016/01/27 18:55:51 riastradh Exp $	*/
+/*	$NetBSD: ifwatchd.c,v 1.28 2016/09/21 14:46:55 roy Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
@@ -117,7 +117,9 @@ main(int argc, char **argv)
 {
 	int c, s, n;
 	int errs = 0;
-	char msg[2048], *msgp;
+	struct msghdr msg;
+	struct iovec iov[1];
+	char buf[2048];
 
 	openlog(argv[0], LOG_PID|LOG_CONS, LOG_DAEMON);
 	while ((c = getopt(argc, argv, "qvhic:n:u:d:A:D:")) != -1) {
@@ -206,13 +208,20 @@ main(int argc, char **argv)
 	if (!inhibit_initial)
 		run_initial_ups();
 
+	iov[0].iov_base = buf;
+	iov[0].iov_len = sizeof(buf);
+	memset(, 0, sizeof(msg));
+	msg.msg_iov = iov;
+	msg.msg_iovlen = 1;
+
 	for (;;) {
-		n = read(s, msg, sizeof msg);
-		msgp = msg;
-		for (msgp = msg; n > 0;
-		 n -= ((struct rt_msghdr*)msgp)->rtm_msglen,
-		 msgp += ((struct rt_msghdr*)msgp)->rtm_msglen)
-			dispatch(msgp, n);
+		n = recvmsg(s, , 0);
+		if (n == -1) {
+			syslog(LOG_ERR, "recvmsg: %m");
+			exit(EXIT_FAILURE);
+		}
+		if (n != 0)
+			dispatch(iov[0].iov_base, n);
 	}
 
 	close(s);



CVS commit: src/usr.sbin/ifwatchd

2016-01-27 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Wed Jan 27 18:55:51 UTC 2016

Modified Files:
src/usr.sbin/ifwatchd: ifwatchd.c

Log Message:
Use unsigned, not signed, int for shifting 1 left until zero.

Signed shift into sign bit and beyond is undefined behaviour.

>From Michael McConville.


To generate a diff of this commit:
cvs rdiff -u -r1.26 -r1.27 src/usr.sbin/ifwatchd/ifwatchd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ifwatchd/ifwatchd.c
diff -u src/usr.sbin/ifwatchd/ifwatchd.c:1.26 src/usr.sbin/ifwatchd/ifwatchd.c:1.27
--- src/usr.sbin/ifwatchd/ifwatchd.c:1.26	Tue Aug 30 18:57:38 2011
+++ src/usr.sbin/ifwatchd/ifwatchd.c	Wed Jan 27 18:55:51 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: ifwatchd.c,v 1.26 2011/08/30 18:57:38 joerg Exp $	*/
+/*	$NetBSD: ifwatchd.c,v 1.27 2016/01/27 18:55:51 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
@@ -292,7 +292,8 @@ check_addrs(char *cp, int addrs, enum ev
 	struct sockaddr *sa, *ifa = NULL, *brd = NULL;
 	char ifname_buf[IFNAMSIZ];
 	const char *ifname;
-	int ifndx = 0, i;
+	int ifndx = 0;
+	unsigned i;
 
 	if (addrs == 0)
 		return;



CVS commit: src/usr.sbin/ifwatchd

2011-08-30 Thread Joerg Sonnenberger
Module Name:src
Committed By:   joerg
Date:   Tue Aug 30 18:57:38 UTC 2011

Modified Files:
src/usr.sbin/ifwatchd: ifwatchd.c

Log Message:
static + __dead


To generate a diff of this commit:
cvs rdiff -u -r1.25 -r1.26 src/usr.sbin/ifwatchd/ifwatchd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ifwatchd/ifwatchd.c
diff -u src/usr.sbin/ifwatchd/ifwatchd.c:1.25 src/usr.sbin/ifwatchd/ifwatchd.c:1.26
--- src/usr.sbin/ifwatchd/ifwatchd.c:1.25	Fri Feb  4 14:31:23 2011
+++ src/usr.sbin/ifwatchd/ifwatchd.c	Tue Aug 30 18:57:38 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: ifwatchd.c,v 1.25 2011/02/04 14:31:23 martin Exp $	*/
+/*	$NetBSD: ifwatchd.c,v 1.26 2011/08/30 18:57:38 joerg Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
@@ -63,7 +63,7 @@
 enum event { ARRIVAL, DEPARTURE, UP, DOWN, CARRIER, NO_CARRIER };
 
 /* local functions */
-static void usage(void);
+__dead static void usage(void);
 static void dispatch(void*, size_t);
 static void check_addrs(char *cp, int addrs, enum event ev);
 static void invoke_script(struct sockaddr *sa, struct sockaddr *dst, enum event ev, int ifindex, const char *ifname_hint);
@@ -110,7 +110,7 @@
 	int last_carrier_status;
 	char * ifname;
 };
-SLIST_HEAD(,interface_data) ifs = SLIST_HEAD_INITIALIZER(ifs);
+static SLIST_HEAD(,interface_data) ifs = SLIST_HEAD_INITIALIZER(ifs);
 
 int
 main(int argc, char **argv)



CVS commit: src/usr.sbin/ifwatchd

2010-04-16 Thread Jared D. McNeill
Module Name:src
Committed By:   jmcneill
Date:   Fri Apr 16 13:47:50 UTC 2010

Modified Files:
src/usr.sbin/ifwatchd: ifwatchd.8

Log Message:
add examples for setting ipv6 default routes in ip-up/ip-down scripts


To generate a diff of this commit:
cvs rdiff -u -r1.25 -r1.26 src/usr.sbin/ifwatchd/ifwatchd.8

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ifwatchd/ifwatchd.8
diff -u src/usr.sbin/ifwatchd/ifwatchd.8:1.25 src/usr.sbin/ifwatchd/ifwatchd.8:1.26
--- src/usr.sbin/ifwatchd/ifwatchd.8:1.25	Mon Mar  9 19:24:33 2009
+++ src/usr.sbin/ifwatchd/ifwatchd.8	Fri Apr 16 13:47:50 2010
@@ -1,4 +1,4 @@
-.\ $NetBSD: ifwatchd.8,v 1.25 2009/03/09 19:24:33 joerg Exp $
+.\ $NetBSD: ifwatchd.8,v 1.26 2010/04/16 13:47:50 jmcneill Exp $
 .\
 .\ Copyright (c) 2001-2003 The NetBSD Foundation, Inc.
 .\ All rights reserved.
@@ -27,7 +27,7 @@
 .\ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 .\ POSSIBILITY OF SUCH DAMAGE.
 .\
-.Dd November 4, 2004
+.Dd April 16, 2010
 .Dt IFWATCHD 8
 .Os
 .Sh NAME
@@ -142,6 +142,7 @@
 .Bd -literal -offset indent
 #! /bin/sh
 /sbin/route add default $5
+/sbin/route add -inet6 default fe80::2 -iface ifp $1
 .Ed
 .Pp
 As described below the fifth command line parameter will contain
@@ -150,6 +151,7 @@
 .Bd -literal -offset indent
 #! /bin/sh
 /sbin/route delete default $5
+/sbin/route delete -inet6 default fe80::2
 .Ed
 .Pp
 Note that this is not a good idea if you have pppoe0 configured to



CVS commit: src/usr.sbin/ifwatchd

2010-04-16 Thread Thomas Klausner
Module Name:src
Committed By:   wiz
Date:   Fri Apr 16 13:56:45 UTC 2010

Modified Files:
src/usr.sbin/ifwatchd: ifwatchd.8

Log Message:
Remove trailing whitespace, replace Ar -i with Fl i.


To generate a diff of this commit:
cvs rdiff -u -r1.26 -r1.27 src/usr.sbin/ifwatchd/ifwatchd.8

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/ifwatchd/ifwatchd.8
diff -u src/usr.sbin/ifwatchd/ifwatchd.8:1.26 src/usr.sbin/ifwatchd/ifwatchd.8:1.27
--- src/usr.sbin/ifwatchd/ifwatchd.8:1.26	Fri Apr 16 13:47:50 2010
+++ src/usr.sbin/ifwatchd/ifwatchd.8	Fri Apr 16 13:56:45 2010
@@ -1,4 +1,4 @@
-.\ $NetBSD: ifwatchd.8,v 1.26 2010/04/16 13:47:50 jmcneill Exp $
+.\ $NetBSD: ifwatchd.8,v 1.27 2010/04/16 13:56:45 wiz Exp $
 .\
 .\ Copyright (c) 2001-2003 The NetBSD Foundation, Inc.
 .\ All rights reserved.
@@ -172,13 +172,13 @@
 .Ed
 .Pp
 With the above command, the carrier-detect script will be invoked when
-a carrier is detected on the interface 
+a carrier is detected on the interface
 .Ar tlp0 .
 Note that the
-.Ar -i 
+.Fl i
 flag prevents any action based on the initial state.
 A script like the following should work for most users, although it
-will not work for machines with multiple interfaces running 
+will not work for machines with multiple interfaces running
 .Cm dhclient .
 .Bd -literal -offset indent
 #! /bin/sh