Home | History | Annotate | Line # | Download | only in ifwatchd
ifwatchd.c revision 1.33
      1  1.33       roy /*	$NetBSD: ifwatchd.c,v 1.33 2016/09/21 21:07:29 roy Exp $	*/
      2   1.2    martin 
      3   1.6    martin /*-
      4  1.14    martin  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
      5   1.1    martin  * All rights reserved.
      6   1.1    martin  *
      7   1.6    martin  * This code is derived from software contributed to The NetBSD Foundation
      8  1.17     grant  * by Martin Husemann <martin (at) NetBSD.org>.
      9   1.6    martin  *
     10   1.1    martin  * Redistribution and use in source and binary forms, with or without
     11   1.1    martin  * modification, are permitted provided that the following conditions
     12   1.1    martin  * are met:
     13   1.1    martin  * 1. Redistributions of source code must retain the above copyright
     14   1.1    martin  *    notice, this list of conditions and the following disclaimer.
     15   1.6    martin  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.6    martin  *    notice, this list of conditions and the following disclaimer in the
     17   1.6    martin  *    documentation and/or other materials provided with the distribution.
     18   1.1    martin  *
     19   1.6    martin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.6    martin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.6    martin  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.6    martin  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.6    martin  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.6    martin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.6    martin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.6    martin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.6    martin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.6    martin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.6    martin  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1    martin  */
     31   1.1    martin 
     32   1.5    martin /*
     33   1.5    martin  * Define this for special treatment of sys/net/if_spppsubr.c based interfaces.
     34   1.5    martin  */
     35   1.5    martin #define SPPP_IF_SUPPORT
     36  1.21    dyoung 
     37   1.1    martin #include <sys/types.h>
     38   1.1    martin #include <sys/param.h>
     39   1.3    martin #include <sys/ioctl.h>
     40   1.1    martin #include <sys/socket.h>
     41   1.2    martin #include <sys/queue.h>
     42   1.9      tron #include <sys/wait.h>
     43   1.1    martin #include <net/if.h>
     44   1.1    martin #include <net/if_dl.h>
     45  1.20    martin #include <net/if_media.h>
     46   1.5    martin #ifdef SPPP_IF_SUPPORT
     47   1.5    martin #include <net/if_sppp.h>
     48   1.5    martin #endif
     49   1.1    martin #include <net/route.h>
     50   1.2    martin #include <netinet/in.h>
     51  1.33       roy #include <netinet/in_var.h>
     52   1.2    martin #include <arpa/inet.h>
     53   1.1    martin 
     54   1.7      tron #include <paths.h>
     55   1.1    martin #include <stdio.h>
     56   1.1    martin #include <stdlib.h>
     57   1.1    martin #include <string.h>
     58   1.1    martin #include <unistd.h>
     59   1.1    martin #include <netdb.h>
     60   1.3    martin #include <err.h>
     61   1.3    martin #include <ifaddrs.h>
     62  1.14    martin #include <syslog.h>
     63   1.1    martin 
     64  1.19    martin enum event { ARRIVAL, DEPARTURE, UP, DOWN, CARRIER, NO_CARRIER };
     65  1.33       roy enum addrflag { NOTREADY, DETACHED, READY };
     66  1.19    martin 
     67   1.1    martin /* local functions */
     68  1.26     joerg __dead static void usage(void);
     69  1.30       roy static void dispatch(const void *, size_t);
     70  1.33       roy static enum addrflag check_addrflags(int af, int addrflags);
     71  1.31       roy static void check_addrs(const struct ifa_msghdr *ifam);
     72  1.30       roy static void invoke_script(const struct sockaddr *sa, const struct sockaddr *dst,
     73  1.30       roy     enum event ev, int ifindex, const char *ifname_hint);
     74   1.1    martin static void list_interfaces(const char *ifnames);
     75  1.30       roy static void check_announce(const struct if_announcemsghdr *ifan);
     76  1.31       roy static void check_carrier(const struct if_msghdr *ifm);
     77   1.1    martin static void rescan_interfaces(void);
     78   1.1    martin static void free_interfaces(void);
     79  1.32       roy static struct interface_data * find_interface(int index);
     80   1.3    martin static void run_initial_ups(void);
     81   1.1    martin 
     82   1.5    martin #ifdef SPPP_IF_SUPPORT
     83  1.18    martin static int check_is_connected(const char * ifname, int def_retvalue);
     84  1.18    martin #define if_is_connected(X)	(check_is_connected((X), 1))
     85  1.18    martin #define if_is_not_connected(X)	(!check_is_connected((X), 0))
     86   1.5    martin #else
     87   1.5    martin #define	if_is_connected(X)	1
     88  1.18    martin #define	if_is_not_connected(X)	1
     89   1.5    martin #endif
     90   1.5    martin 
     91   1.1    martin /* global variables */
     92  1.14    martin static int verbose = 0, quiet = 0;
     93   1.3    martin static int inhibit_initial = 0;
     94  1.10    martin static const char *arrival_script = NULL;
     95  1.10    martin static const char *departure_script = NULL;
     96   1.1    martin static const char *up_script = NULL;
     97   1.1    martin static const char *down_script = NULL;
     98  1.19    martin static const char *carrier_script = NULL;
     99  1.19    martin static const char *no_carrier_script = NULL;
    100  1.19    martin static const char DummyTTY[] = _PATH_DEVNULL;
    101  1.19    martin static const char DummySpeed[] = "9600";
    102  1.10    martin static const char **scripts[] = {
    103  1.10    martin 	&arrival_script,
    104  1.10    martin 	&departure_script,
    105  1.10    martin 	&up_script,
    106  1.19    martin 	&down_script,
    107  1.19    martin 	&carrier_script,
    108  1.19    martin 	&no_carrier_script
    109  1.13    itojun };
    110   1.1    martin 
    111   1.1    martin struct interface_data {
    112   1.1    martin 	SLIST_ENTRY(interface_data) next;
    113   1.1    martin 	int index;
    114  1.19    martin 	int last_carrier_status;
    115   1.1    martin 	char * ifname;
    116   1.1    martin };
    117  1.26     joerg static SLIST_HEAD(,interface_data) ifs = SLIST_HEAD_INITIALIZER(ifs);
    118   1.1    martin 
    119   1.1    martin int
    120   1.1    martin main(int argc, char **argv)
    121   1.1    martin {
    122   1.1    martin 	int c, s, n;
    123   1.1    martin 	int errs = 0;
    124  1.28       roy 	struct msghdr msg;
    125  1.28       roy 	struct iovec iov[1];
    126  1.28       roy 	char buf[2048];
    127   1.1    martin 
    128  1.14    martin 	openlog(argv[0], LOG_PID|LOG_CONS, LOG_DAEMON);
    129  1.21    dyoung 	while ((c = getopt(argc, argv, "qvhic:n:u:d:A:D:")) != -1) {
    130   1.1    martin 		switch (c) {
    131   1.1    martin 		case 'h':
    132   1.1    martin 			usage();
    133   1.1    martin 			return 0;
    134  1.19    martin 
    135   1.3    martin 		case 'i':
    136   1.3    martin 			inhibit_initial = 1;
    137   1.3    martin 			break;
    138  1.19    martin 
    139   1.1    martin 		case 'v':
    140   1.1    martin 			verbose++;
    141   1.1    martin 			break;
    142  1.19    martin 
    143  1.14    martin 		case 'q':
    144  1.14    martin 			quiet = 1;
    145  1.14    martin 			break;
    146   1.1    martin 
    147  1.19    martin 		case 'c':
    148  1.19    martin 			carrier_script = optarg;
    149  1.19    martin 			break;
    150  1.19    martin 
    151  1.19    martin 		case 'n':
    152  1.19    martin 			no_carrier_script = optarg;
    153  1.19    martin 			break;
    154  1.19    martin 
    155   1.1    martin 		case 'u':
    156   1.1    martin 			up_script = optarg;
    157   1.1    martin 			break;
    158   1.1    martin 
    159   1.1    martin 		case 'd':
    160   1.1    martin 			down_script = optarg;
    161   1.1    martin 			break;
    162   1.1    martin 
    163  1.10    martin 		case 'A':
    164  1.10    martin 			arrival_script = optarg;
    165  1.10    martin 			break;
    166  1.10    martin 
    167  1.10    martin 		case 'D':
    168  1.10    martin 			departure_script = optarg;
    169  1.10    martin 			break;
    170  1.10    martin 
    171   1.1    martin 		default:
    172   1.1    martin 			errs++;
    173   1.1    martin 			break;
    174   1.1    martin 		}
    175  1.21    dyoung 	}
    176   1.1    martin 
    177   1.1    martin 	if (errs)
    178   1.1    martin 		usage();
    179   1.1    martin 
    180   1.1    martin 	argv += optind;
    181   1.1    martin 	argc -= optind;
    182   1.1    martin 
    183  1.21    dyoung 	if (argc <= 0)
    184   1.1    martin 		usage();
    185   1.1    martin 
    186   1.1    martin 	if (verbose) {
    187   1.1    martin 		printf("up_script: %s\ndown_script: %s\n",
    188   1.1    martin 			up_script, down_script);
    189  1.10    martin 		printf("arrival_script: %s\ndeparture_script: %s\n",
    190  1.10    martin 			arrival_script, departure_script);
    191  1.19    martin 		printf("carrier_script: %s\nno_carrier_script: %s\n",
    192  1.19    martin 			carrier_script, no_carrier_script);
    193   1.1    martin 		printf("verbosity = %d\n", verbose);
    194   1.1    martin 	}
    195   1.1    martin 
    196   1.1    martin 	while (argc > 0) {
    197  1.13    itojun 		list_interfaces(argv[0]);
    198  1.13    itojun 		argv++;
    199  1.13    itojun 		argc--;
    200   1.1    martin 	}
    201   1.1    martin 
    202   1.1    martin 	if (!verbose)
    203  1.21    dyoung 		daemon(0, 0);
    204   1.1    martin 
    205   1.1    martin 	s = socket(PF_ROUTE, SOCK_RAW, 0);
    206   1.1    martin 	if (s < 0) {
    207  1.14    martin 		syslog(LOG_ERR, "error opening routing socket: %m");
    208   1.1    martin 		perror("open routing socket");
    209   1.9      tron 		exit(EXIT_FAILURE);
    210   1.1    martin 	}
    211   1.1    martin 
    212  1.11    martin 	if (!inhibit_initial)
    213  1.11    martin 		run_initial_ups();
    214  1.11    martin 
    215  1.28       roy 	iov[0].iov_base = buf;
    216  1.28       roy 	iov[0].iov_len = sizeof(buf);
    217  1.28       roy 	memset(&msg, 0, sizeof(msg));
    218  1.28       roy 	msg.msg_iov = iov;
    219  1.28       roy 	msg.msg_iovlen = 1;
    220  1.28       roy 
    221   1.1    martin 	for (;;) {
    222  1.28       roy 		n = recvmsg(s, &msg, 0);
    223  1.28       roy 		if (n == -1) {
    224  1.28       roy 			syslog(LOG_ERR, "recvmsg: %m");
    225  1.28       roy 			exit(EXIT_FAILURE);
    226  1.28       roy 		}
    227  1.28       roy 		if (n != 0)
    228  1.28       roy 			dispatch(iov[0].iov_base, n);
    229   1.1    martin 	}
    230   1.1    martin 
    231   1.1    martin 	close(s);
    232   1.1    martin 	free_interfaces();
    233  1.14    martin 	closelog();
    234   1.1    martin 
    235   1.9      tron 	return EXIT_SUCCESS;
    236   1.1    martin }
    237   1.1    martin 
    238   1.1    martin static void
    239  1.21    dyoung usage(void)
    240   1.1    martin {
    241  1.21    dyoung 	fprintf(stderr,
    242   1.1    martin 	    "usage:\n"
    243  1.15       wiz 	    "\tifwatchd [-hiqv] [-A arrival-script] [-D departure-script]\n"
    244  1.19    martin 	    "\t\t  [-d down-script] [-u up-script]\n"
    245  1.19    martin 	    "\t\t  [-c carrier-script] [-n no-carrier-script] ifname(s)\n"
    246   1.1    martin 	    "\twhere:\n"
    247  1.12       wiz 	    "\t -A <cmd> specify command to run on interface arrival event\n"
    248  1.19    martin 	    "\t -c <cmd> specify command to run on interface carrier-detect event\n"
    249  1.12       wiz 	    "\t -D <cmd> specify command to run on interface departure event\n"
    250  1.12       wiz 	    "\t -d <cmd> specify command to run on interface down event\n"
    251  1.19    martin 	    "\t -n <cmd> specify command to run on interface no-carrier-detect event\n"
    252   1.1    martin 	    "\t -h       show this help message\n"
    253   1.3    martin 	    "\t -i       no (!) initial run of the up script if the interface\n"
    254   1.3    martin 	    "\t          is already up on ifwatchd startup\n"
    255  1.16       abs 	    "\t -q       quiet mode, don't syslog informational messages\n"
    256   1.1    martin 	    "\t -u <cmd> specify command to run on interface up event\n"
    257  1.16       abs 	    "\t -v       verbose/debug output, don't run in background\n");
    258   1.9      tron 	exit(EXIT_FAILURE);
    259   1.1    martin }
    260   1.1    martin 
    261   1.1    martin static void
    262  1.30       roy dispatch(const void *msg, size_t len)
    263   1.1    martin {
    264  1.30       roy 	const struct rt_msghdr *hd = msg;
    265   1.1    martin 
    266  1.29       roy 	if (hd->rtm_version != RTM_VERSION)
    267  1.29       roy 		return;
    268  1.29       roy 
    269   1.1    martin 	switch (hd->rtm_type) {
    270   1.1    martin 	case RTM_NEWADDR:
    271   1.1    martin 	case RTM_DELADDR:
    272  1.31       roy 		check_addrs(msg);
    273  1.31       roy 		break;
    274   1.1    martin 	case RTM_IFANNOUNCE:
    275   1.1    martin 		rescan_interfaces();
    276  1.30       roy 		check_announce(msg);
    277  1.31       roy 		break;
    278  1.19    martin 	case RTM_IFINFO:
    279  1.31       roy 		check_carrier(msg);
    280  1.31       roy 		break;
    281  1.23     joerg 	case RTM_ADD:
    282  1.23     joerg 	case RTM_DELETE:
    283  1.23     joerg 	case RTM_CHANGE:
    284  1.23     joerg 	case RTM_LOSING:
    285  1.23     joerg 	case RTM_REDIRECT:
    286  1.23     joerg 	case RTM_MISS:
    287  1.23     joerg 	case RTM_IEEE80211:
    288  1.29       roy 	case RTM_ONEWADDR:
    289  1.29       roy 	case RTM_ODELADDR:
    290  1.29       roy 	case RTM_OCHGADDR:
    291  1.31       roy 		break;
    292  1.31       roy 	default:
    293  1.31       roy 		if (verbose)
    294  1.31       roy 			printf("unknown message ignored (%d)\n", hd->rtm_type);
    295  1.31       roy 		break;
    296   1.1    martin 	}
    297   1.1    martin }
    298   1.1    martin 
    299  1.33       roy static enum addrflag
    300  1.33       roy check_addrflags(int af, int addrflags)
    301  1.33       roy {
    302  1.33       roy 
    303  1.33       roy 	switch (af) {
    304  1.33       roy 	case AF_INET:
    305  1.33       roy 		if (addrflags & IN_IFF_NOTREADY)
    306  1.33       roy 			return NOTREADY;
    307  1.33       roy 		if (addrflags & IN_IFF_DETACHED)
    308  1.33       roy 			return DETACHED;
    309  1.33       roy 		break;
    310  1.33       roy 	case AF_INET6:
    311  1.33       roy 		if (addrflags & IN6_IFF_NOTREADY)
    312  1.33       roy 			return NOTREADY;
    313  1.33       roy 		if (addrflags & IN6_IFF_DETACHED)
    314  1.33       roy 			return DETACHED;
    315  1.33       roy 		break;
    316  1.33       roy 	}
    317  1.33       roy 	return READY;
    318  1.33       roy }
    319  1.33       roy 
    320   1.1    martin static void
    321  1.31       roy check_addrs(const struct ifa_msghdr *ifam)
    322   1.1    martin {
    323  1.31       roy 	const char *cp = (const char *)(ifam + 1);
    324  1.30       roy 	const struct sockaddr *sa, *ifa = NULL, *brd = NULL;
    325  1.27  riastrad 	unsigned i;
    326  1.32       roy 	struct interface_data *ifd = NULL;
    327  1.33       roy 	int aflag;
    328  1.31       roy 	enum event ev;
    329   1.1    martin 
    330  1.31       roy 	if (ifam->ifam_addrs == 0)
    331  1.13    itojun 		return;
    332   1.1    martin 	for (i = 1; i; i <<= 1) {
    333  1.31       roy 		if ((i & ifam->ifam_addrs) == 0)
    334  1.21    dyoung 			continue;
    335  1.30       roy 		sa = (const struct sockaddr *)cp;
    336  1.21    dyoung 		if (i == RTA_IFP) {
    337  1.30       roy 			const struct sockaddr_dl *li;
    338  1.30       roy 
    339  1.30       roy 			li = (const struct sockaddr_dl *)sa;
    340  1.32       roy 			if ((ifd = find_interface(li->sdl_index)) == NULL) {
    341  1.21    dyoung 				if (verbose)
    342  1.32       roy 					printf("ignoring change"
    343  1.32       roy 					    " on interface #%d\n",
    344  1.32       roy 					    li->sdl_index);
    345  1.21    dyoung 				return;
    346  1.13    itojun 			}
    347  1.21    dyoung 		} else if (i == RTA_IFA)
    348  1.21    dyoung 			ifa = sa;
    349  1.21    dyoung 		else if (i == RTA_BRD)
    350  1.21    dyoung 			brd = sa;
    351  1.25    martin 		RT_ADVANCE(cp, sa);
    352   1.1    martin 	}
    353  1.32       roy 	if (ifa != NULL && ifd != NULL) {
    354  1.31       roy 		ev = ifam->ifam_type == RTM_DELADDR ? DOWN : UP;
    355  1.33       roy 		aflag = check_addrflags(ifa->sa_family, ifam->ifam_addrflags);
    356  1.33       roy 		if (ev == UP) {
    357  1.33       roy 			if (aflag == NOTREADY)
    358  1.33       roy 				return;
    359  1.33       roy 			if (aflag == DETACHED)
    360  1.33       roy 				return;		/* XXX set ev to DOWN? */
    361  1.33       roy 		}
    362  1.32       roy 		if ((ev == UP && if_is_connected(ifd->ifname)) ||
    363  1.32       roy 		    (ev == DOWN && if_is_not_connected(ifd->ifname)))
    364  1.32       roy 			invoke_script(ifa, brd, ev, ifd->index, ifd->ifname);
    365  1.18    martin 	}
    366   1.1    martin }
    367   1.1    martin 
    368   1.1    martin static void
    369  1.30       roy invoke_script(const struct sockaddr *sa, const struct sockaddr *dest,
    370  1.30       roy     enum event ev, int ifindex, const char *ifname_hint)
    371   1.1    martin {
    372  1.10    martin 	char addr[NI_MAXHOST], daddr[NI_MAXHOST], ifname_buf[IFNAMSIZ];
    373  1.24     lukem 	const char * volatile ifname;
    374   1.1    martin 	const char *script;
    375   1.9      tron 	int status;
    376   1.8      tron 
    377  1.10    martin 	if (sa != NULL && sa->sa_len == 0) {
    378  1.13    itojun 		fprintf(stderr, "illegal socket address (sa_len == 0)\n");
    379  1.13    itojun 		return;
    380  1.10    martin 	}
    381  1.10    martin 	if (sa != NULL && sa->sa_family == AF_INET6) {
    382   1.8      tron 		struct sockaddr_in6 sin6;
    383   1.8      tron 
    384  1.30       roy 		memcpy(&sin6, (const struct sockaddr_in6 *)sa, sizeof (sin6));
    385   1.8      tron 		if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr))
    386   1.8      tron 			return;
    387   1.8      tron 	}
    388   1.1    martin 
    389  1.10    martin 	addr[0] = daddr[0] = 0;
    390   1.1    martin 	ifname = if_indextoname(ifindex, ifname_buf);
    391  1.10    martin 	ifname = ifname ? ifname : ifname_hint;
    392  1.10    martin 	if (ifname == NULL)
    393  1.13    itojun 		return;
    394   1.1    martin 
    395  1.10    martin 	if (sa != NULL) {
    396  1.13    itojun 		if (getnameinfo(sa, sa->sa_len, addr, sizeof addr, NULL, 0,
    397  1.13    itojun 		    NI_NUMERICHOST)) {
    398  1.13    itojun 			if (verbose)
    399  1.13    itojun 				printf("getnameinfo failed\n");
    400  1.13    itojun 			return;	/* this address can not be handled */
    401  1.13    itojun 		}
    402  1.10    martin 	}
    403   1.4    martin 	if (dest != NULL) {
    404  1.13    itojun 		if (getnameinfo(dest, dest->sa_len, daddr, sizeof daddr,
    405  1.13    itojun 		    NULL, 0, NI_NUMERICHOST)) {
    406  1.13    itojun 			if (verbose)
    407  1.13    itojun 				printf("getnameinfo failed\n");
    408  1.13    itojun 			return;	/* this address can not be handled */
    409  1.13    itojun 		}
    410   1.4    martin 	}
    411   1.1    martin 
    412  1.10    martin 	script = *scripts[ev];
    413   1.1    martin 	if (script == NULL) return;
    414   1.1    martin 
    415   1.9      tron 	if (verbose)
    416  1.13    itojun 		(void) printf("calling: %s %s %s %s %s %s\n",
    417  1.13    itojun 		    script, ifname, DummyTTY, DummySpeed, addr, daddr);
    418  1.14    martin 	if (!quiet)
    419  1.14    martin 		syslog(LOG_INFO, "calling: %s %s %s %s %s %s\n",
    420  1.14    martin 		    script, ifname, DummyTTY, DummySpeed, addr, daddr);
    421   1.9      tron 
    422   1.9      tron 	switch (vfork()) {
    423   1.9      tron 	case -1:
    424  1.13    itojun 		fprintf(stderr, "cannot fork\n");
    425  1.13    itojun 		break;
    426   1.9      tron 	case 0:
    427  1.14    martin 		if (execl(script, script, ifname, DummyTTY, DummySpeed,
    428  1.14    martin 		    addr, daddr, NULL) == -1) {
    429  1.14    martin 			syslog(LOG_ERR, "could not execute \"%s\": %m",
    430  1.14    martin 			    script);
    431  1.14    martin 			perror(script);
    432  1.14    martin 		}
    433  1.13    itojun 		_exit(EXIT_FAILURE);
    434   1.9      tron 	default:
    435  1.13    itojun 		(void) wait(&status);
    436   1.1    martin 	}
    437   1.1    martin }
    438   1.1    martin 
    439  1.19    martin static void
    440  1.19    martin list_interfaces(const char *ifnames)
    441   1.1    martin {
    442   1.1    martin 	char * names = strdup(ifnames);
    443   1.1    martin 	char * name, *lasts;
    444   1.1    martin 	static const char sep[] = " \t";
    445   1.1    martin 	struct interface_data * p;
    446   1.1    martin 
    447  1.13    itojun 	for (name = strtok_r(names, sep, &lasts);
    448  1.13    itojun 	    name != NULL;
    449  1.13    itojun 	    name = strtok_r(NULL, sep, &lasts)) {
    450  1.13    itojun 		p = malloc(sizeof(*p));
    451  1.13    itojun 		SLIST_INSERT_HEAD(&ifs, p, next);
    452  1.19    martin 		p->last_carrier_status = -1;
    453  1.13    itojun 		p->ifname = strdup(name);
    454  1.13    itojun 		p->index = if_nametoindex(p->ifname);
    455  1.14    martin 		if (!quiet)
    456  1.14    martin 			syslog(LOG_INFO, "watching interface %s", p->ifname);
    457  1.13    itojun 		if (verbose)
    458  1.13    itojun 			printf("interface \"%s\" has index %d\n",
    459  1.13    itojun 			    p->ifname, p->index);
    460   1.1    martin 	}
    461   1.1    martin 	free(names);
    462   1.1    martin }
    463   1.1    martin 
    464  1.10    martin static void
    465  1.31       roy check_carrier(const struct if_msghdr *ifm)
    466  1.19    martin {
    467  1.19    martin 	struct interface_data * p;
    468  1.31       roy 	int carrier_status;
    469  1.19    martin 	enum event ev;
    470  1.19    martin 
    471  1.19    martin 	SLIST_FOREACH(p, &ifs, next)
    472  1.31       roy 		if (p->index == ifm->ifm_index)
    473  1.19    martin 			break;
    474  1.19    martin 
    475  1.19    martin 	if (p == NULL)
    476  1.19    martin 		return;
    477  1.19    martin 
    478  1.21    dyoung 	/*
    479  1.19    martin 	 * Treat it as an event worth handling if:
    480  1.19    martin 	 * - the carrier status changed, or
    481  1.19    martin 	 * - this is the first time we've been called, and
    482  1.19    martin 	 * inhibit_initial is not set
    483  1.19    martin 	 */
    484  1.31       roy 	carrier_status = ifm->ifm_data.ifi_link_state;
    485  1.19    martin 	if ((carrier_status != p->last_carrier_status) ||
    486  1.19    martin 	    ((p->last_carrier_status == -1) && !inhibit_initial)) {
    487  1.19    martin 		switch (carrier_status) {
    488  1.19    martin 		case LINK_STATE_UP:
    489  1.19    martin 			ev = CARRIER;
    490  1.19    martin 			break;
    491  1.19    martin 		case LINK_STATE_DOWN:
    492  1.19    martin 			ev = NO_CARRIER;
    493  1.19    martin 			break;
    494  1.19    martin 		default:
    495  1.19    martin 			if (verbose)
    496  1.19    martin 				printf("unknown link status ignored\n");
    497  1.19    martin 			return;
    498  1.19    martin 		}
    499  1.31       roy 		invoke_script(NULL, NULL, ev, ifm->ifm_index, p->ifname);
    500  1.19    martin 		p->last_carrier_status = carrier_status;
    501  1.19    martin 	}
    502  1.19    martin }
    503  1.19    martin 
    504  1.19    martin static void
    505  1.30       roy check_announce(const struct if_announcemsghdr *ifan)
    506  1.10    martin {
    507  1.10    martin 	struct interface_data * p;
    508  1.10    martin 	const char *ifname = ifan->ifan_name;
    509  1.10    martin 
    510  1.10    martin 	SLIST_FOREACH(p, &ifs, next) {
    511  1.21    dyoung 		if (strcmp(p->ifname, ifname) != 0)
    512  1.21    dyoung 			continue;
    513  1.21    dyoung 
    514  1.21    dyoung 		switch (ifan->ifan_what) {
    515  1.21    dyoung 		case IFAN_ARRIVAL:
    516  1.21    dyoung 			invoke_script(NULL, NULL, ARRIVAL, p->index,
    517  1.21    dyoung 			    NULL);
    518  1.21    dyoung 			break;
    519  1.21    dyoung 		case IFAN_DEPARTURE:
    520  1.21    dyoung 			invoke_script(NULL, NULL, DEPARTURE, p->index,
    521  1.21    dyoung 			    p->ifname);
    522  1.21    dyoung 			break;
    523  1.21    dyoung 		default:
    524  1.21    dyoung 			if (verbose)
    525  1.21    dyoung 				(void) printf("unknown announce: "
    526  1.21    dyoung 				    "what=%d\n", ifan->ifan_what);
    527  1.21    dyoung 			break;
    528  1.10    martin 		}
    529  1.21    dyoung 		return;
    530  1.10    martin 	}
    531  1.10    martin }
    532  1.10    martin 
    533  1.21    dyoung static void
    534  1.21    dyoung rescan_interfaces(void)
    535   1.1    martin {
    536   1.1    martin 	struct interface_data * p;
    537  1.21    dyoung 
    538   1.1    martin 	SLIST_FOREACH(p, &ifs, next) {
    539  1.13    itojun 		p->index = if_nametoindex(p->ifname);
    540  1.13    itojun 		if (verbose)
    541  1.13    itojun 			printf("interface \"%s\" has index %d\n", p->ifname,
    542  1.13    itojun 			    p->index);
    543   1.1    martin 	}
    544   1.1    martin }
    545   1.1    martin 
    546  1.21    dyoung static void
    547  1.21    dyoung free_interfaces(void)
    548   1.1    martin {
    549   1.1    martin 	struct interface_data * p;
    550   1.1    martin 
    551   1.1    martin 	while (!SLIST_EMPTY(&ifs)) {
    552  1.13    itojun 		p = SLIST_FIRST(&ifs);
    553  1.13    itojun 		SLIST_REMOVE_HEAD(&ifs, next);
    554  1.13    itojun 		free(p->ifname);
    555  1.13    itojun 		free(p);
    556   1.1    martin 	}
    557   1.1    martin }
    558   1.1    martin 
    559  1.32       roy static struct interface_data *
    560  1.24     lukem find_interface(int idx)
    561   1.1    martin {
    562   1.1    martin 	struct interface_data * p;
    563  1.21    dyoung 
    564   1.1    martin 	SLIST_FOREACH(p, &ifs, next)
    565  1.24     lukem 		if (p->index == idx)
    566  1.32       roy 			return p;
    567  1.32       roy 	return NULL;
    568   1.3    martin }
    569   1.3    martin 
    570  1.21    dyoung static void
    571  1.21    dyoung run_initial_ups(void)
    572   1.3    martin {
    573   1.3    martin 	struct interface_data * ifd;
    574   1.3    martin 	struct ifaddrs *res = NULL, *p;
    575  1.33       roy 	struct sockaddr *ifa;
    576  1.33       roy 	int s, aflag;
    577  1.20    martin 
    578  1.20    martin 	s = socket(AF_INET, SOCK_DGRAM, 0);
    579  1.20    martin 	if (s < 0)
    580  1.20    martin 		return;
    581   1.3    martin 
    582  1.21    dyoung 	if (getifaddrs(&res) != 0)
    583  1.21    dyoung 		goto out;
    584  1.21    dyoung 
    585  1.21    dyoung 	for (p = res; p; p = p->ifa_next) {
    586  1.21    dyoung 		SLIST_FOREACH(ifd, &ifs, next) {
    587  1.21    dyoung 			if (strcmp(ifd->ifname, p->ifa_name) == 0)
    588  1.21    dyoung 				break;
    589  1.21    dyoung 		}
    590  1.21    dyoung 		if (ifd == NULL)
    591  1.21    dyoung 			continue;
    592  1.13    itojun 
    593  1.33       roy 		ifa = p->ifa_addr;
    594  1.33       roy 		if (ifa != NULL && ifa->sa_family == AF_LINK)
    595  1.21    dyoung 			invoke_script(NULL, NULL, ARRIVAL, ifd->index,
    596  1.21    dyoung 			    NULL);
    597  1.21    dyoung 
    598  1.21    dyoung 		if ((p->ifa_flags & IFF_UP) == 0)
    599  1.21    dyoung 			continue;
    600  1.33       roy 		if (ifa == NULL)
    601  1.21    dyoung 			continue;
    602  1.33       roy 		if (ifa->sa_family == AF_LINK) {
    603  1.21    dyoung 			struct ifmediareq ifmr;
    604  1.21    dyoung 
    605  1.21    dyoung 			memset(&ifmr, 0, sizeof(ifmr));
    606  1.21    dyoung 			strncpy(ifmr.ifm_name, ifd->ifname,
    607  1.21    dyoung 			    sizeof(ifmr.ifm_name));
    608  1.21    dyoung 			if (ioctl(s, SIOCGIFMEDIA, &ifmr) != -1
    609  1.21    dyoung 			    && (ifmr.ifm_status & IFM_AVALID)
    610  1.21    dyoung 			    && (ifmr.ifm_status & IFM_ACTIVE)) {
    611  1.21    dyoung 				invoke_script(NULL, NULL, CARRIER,
    612  1.13    itojun 				    ifd->index, ifd->ifname);
    613  1.21    dyoung 				ifd->last_carrier_status =
    614  1.21    dyoung 				    LINK_STATE_UP;
    615  1.21    dyoung 			    }
    616  1.21    dyoung 			continue;
    617  1.21    dyoung 		}
    618  1.33       roy 		aflag = check_addrflags(ifa->sa_family, p->ifa_addrflags);
    619  1.33       roy 		if (aflag != READY)
    620  1.33       roy 			continue;
    621  1.21    dyoung 		if (if_is_connected(ifd->ifname))
    622  1.33       roy 			invoke_script(ifa, p->ifa_dstaddr, UP,
    623  1.21    dyoung 			    ifd->index, ifd->ifname);
    624   1.3    martin 	}
    625  1.21    dyoung 	freeifaddrs(res);
    626  1.21    dyoung out:
    627  1.20    martin 	close(s);
    628   1.1    martin }
    629   1.5    martin 
    630   1.5    martin #ifdef SPPP_IF_SUPPORT
    631   1.5    martin /*
    632   1.5    martin  * Special case support for in-kernel PPP interfaces.
    633   1.5    martin  * If these are IFF_UP, but have not yet connected or completed authentication
    634   1.5    martin  * we don't want to call the up script in the initial interface scan (there
    635   1.5    martin  * will be an UP event generated later, when IPCP completes, anyway).
    636   1.5    martin  *
    637   1.5    martin  * If this is no if_spppsubr.c based interface, this ioctl just fails and we
    638   1.5    martin  * treat is as connected.
    639   1.5    martin  */
    640   1.5    martin static int
    641  1.21    dyoung check_is_connected(const char *ifname, int def_retval)
    642   1.5    martin {
    643  1.24     lukem 	int s, error;
    644  1.18    martin 	struct spppstatus oldstatus;
    645  1.18    martin 	struct spppstatusncp status;
    646   1.5    martin 
    647   1.5    martin 	memset(&status, 0, sizeof status);
    648   1.5    martin 	strncpy(status.ifname, ifname, sizeof status.ifname);
    649  1.18    martin 	memset(&oldstatus, 0, sizeof oldstatus);
    650  1.18    martin 	strncpy(oldstatus.ifname, ifname, sizeof oldstatus.ifname);
    651  1.18    martin 
    652   1.5    martin 	s = socket(AF_INET, SOCK_DGRAM, 0);
    653   1.5    martin 	if (s < 0)
    654  1.13    itojun 		return 1;	/* no idea how to handle this... */
    655  1.24     lukem 	error = ioctl(s, SPPPGETSTATUSNCP, &status);
    656  1.24     lukem 	if (error != 0) {
    657  1.24     lukem 		error = ioctl(s, SPPPGETSTATUS, &oldstatus);
    658  1.24     lukem 		if (error != 0) {
    659  1.18    martin 			/* not if_spppsubr.c based - return default */
    660  1.18    martin 			close(s);
    661  1.18    martin 			return def_retval;
    662  1.18    martin 		} else {
    663  1.18    martin 			/* can't query NCPs, so use default */
    664  1.18    martin 			status.phase = oldstatus.phase;
    665  1.18    martin 			status.ncpup = def_retval;
    666  1.18    martin 		}
    667  1.18    martin 	}
    668   1.5    martin 	close(s);
    669  1.18    martin 
    670  1.18    martin 	return status.phase == SPPP_PHASE_NETWORK && status.ncpup > 0;
    671   1.5    martin }
    672   1.5    martin #endif
    673