Home | History | Annotate | Line # | Download | only in ifwatchd
ifwatchd.c revision 1.23
      1  1.23   joerg /*	$NetBSD: ifwatchd.c,v 1.23 2008/05/24 17:45:14 joerg 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.2  martin #include <arpa/inet.h>
     52   1.1  martin 
     53   1.7    tron #include <paths.h>
     54   1.1  martin #include <stdio.h>
     55   1.1  martin #include <stdlib.h>
     56   1.1  martin #include <string.h>
     57   1.1  martin #include <unistd.h>
     58   1.1  martin #include <netdb.h>
     59   1.3  martin #include <err.h>
     60   1.3  martin #include <ifaddrs.h>
     61  1.14  martin #include <syslog.h>
     62   1.1  martin 
     63  1.19  martin enum event { ARRIVAL, DEPARTURE, UP, DOWN, CARRIER, NO_CARRIER };
     64  1.19  martin 
     65   1.1  martin /* local functions */
     66   1.1  martin static void usage(void);
     67   1.1  martin static void dispatch(void*, size_t);
     68  1.10  martin static void check_addrs(char *cp, int addrs, enum event ev);
     69  1.10  martin static void invoke_script(struct sockaddr *sa, struct sockaddr *dst, enum event ev, int ifindex, const char *ifname_hint);
     70   1.1  martin static void list_interfaces(const char *ifnames);
     71  1.10  martin static void check_announce(struct if_announcemsghdr *ifan);
     72  1.19  martin static void check_carrier(int if_index, int carrier);
     73   1.1  martin static void rescan_interfaces(void);
     74   1.1  martin static void free_interfaces(void);
     75   1.1  martin static int find_interface(int index);
     76   1.3  martin static void run_initial_ups(void);
     77   1.1  martin 
     78   1.5  martin #ifdef SPPP_IF_SUPPORT
     79  1.18  martin static int check_is_connected(const char * ifname, int def_retvalue);
     80  1.18  martin #define if_is_connected(X)	(check_is_connected((X), 1))
     81  1.18  martin #define if_is_not_connected(X)	(!check_is_connected((X), 0))
     82   1.5  martin #else
     83   1.5  martin #define	if_is_connected(X)	1
     84  1.18  martin #define	if_is_not_connected(X)	1
     85   1.5  martin #endif
     86   1.5  martin 
     87   1.1  martin /* stolen from /sbin/route */
     88   1.1  martin #define ROUNDUP(a) \
     89   1.1  martin 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
     90   1.1  martin #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
     91   1.1  martin 
     92   1.1  martin /* global variables */
     93  1.14  martin static int verbose = 0, quiet = 0;
     94   1.3  martin static int inhibit_initial = 0;
     95  1.10  martin static const char *arrival_script = NULL;
     96  1.10  martin static const char *departure_script = NULL;
     97   1.1  martin static const char *up_script = NULL;
     98   1.1  martin static const char *down_script = NULL;
     99  1.19  martin static const char *carrier_script = NULL;
    100  1.19  martin static const char *no_carrier_script = NULL;
    101  1.19  martin static const char DummyTTY[] = _PATH_DEVNULL;
    102  1.19  martin static const char DummySpeed[] = "9600";
    103  1.10  martin static const char **scripts[] = {
    104  1.10  martin 	&arrival_script,
    105  1.10  martin 	&departure_script,
    106  1.10  martin 	&up_script,
    107  1.19  martin 	&down_script,
    108  1.19  martin 	&carrier_script,
    109  1.19  martin 	&no_carrier_script
    110  1.13  itojun };
    111   1.1  martin 
    112   1.1  martin struct interface_data {
    113   1.1  martin 	SLIST_ENTRY(interface_data) next;
    114   1.1  martin 	int index;
    115  1.19  martin 	int last_carrier_status;
    116   1.1  martin 	char * ifname;
    117   1.1  martin };
    118   1.1  martin SLIST_HEAD(,interface_data) ifs = SLIST_HEAD_INITIALIZER(ifs);
    119   1.1  martin 
    120   1.1  martin int
    121   1.1  martin main(int argc, char **argv)
    122   1.1  martin {
    123   1.1  martin 	int c, s, n;
    124   1.1  martin 	int errs = 0;
    125   1.1  martin 	char msg[2048], *msgp;
    126   1.1  martin 
    127  1.14  martin 	openlog(argv[0], LOG_PID|LOG_CONS, LOG_DAEMON);
    128  1.21  dyoung 	while ((c = getopt(argc, argv, "qvhic:n:u:d:A:D:")) != -1) {
    129   1.1  martin 		switch (c) {
    130   1.1  martin 		case 'h':
    131   1.1  martin 			usage();
    132   1.1  martin 			return 0;
    133  1.19  martin 
    134   1.3  martin 		case 'i':
    135   1.3  martin 			inhibit_initial = 1;
    136   1.3  martin 			break;
    137  1.19  martin 
    138   1.1  martin 		case 'v':
    139   1.1  martin 			verbose++;
    140   1.1  martin 			break;
    141  1.19  martin 
    142  1.14  martin 		case 'q':
    143  1.14  martin 			quiet = 1;
    144  1.14  martin 			break;
    145   1.1  martin 
    146  1.19  martin 		case 'c':
    147  1.19  martin 			carrier_script = optarg;
    148  1.19  martin 			break;
    149  1.19  martin 
    150  1.19  martin 		case 'n':
    151  1.19  martin 			no_carrier_script = optarg;
    152  1.19  martin 			break;
    153  1.19  martin 
    154   1.1  martin 		case 'u':
    155   1.1  martin 			up_script = optarg;
    156   1.1  martin 			break;
    157   1.1  martin 
    158   1.1  martin 		case 'd':
    159   1.1  martin 			down_script = optarg;
    160   1.1  martin 			break;
    161   1.1  martin 
    162  1.10  martin 		case 'A':
    163  1.10  martin 			arrival_script = optarg;
    164  1.10  martin 			break;
    165  1.10  martin 
    166  1.10  martin 		case 'D':
    167  1.10  martin 			departure_script = optarg;
    168  1.10  martin 			break;
    169  1.10  martin 
    170   1.1  martin 		default:
    171   1.1  martin 			errs++;
    172   1.1  martin 			break;
    173   1.1  martin 		}
    174  1.21  dyoung 	}
    175   1.1  martin 
    176   1.1  martin 	if (errs)
    177   1.1  martin 		usage();
    178   1.1  martin 
    179   1.1  martin 	argv += optind;
    180   1.1  martin 	argc -= optind;
    181   1.1  martin 
    182  1.21  dyoung 	if (argc <= 0)
    183   1.1  martin 		usage();
    184   1.1  martin 
    185   1.1  martin 	if (verbose) {
    186   1.1  martin 		printf("up_script: %s\ndown_script: %s\n",
    187   1.1  martin 			up_script, down_script);
    188  1.10  martin 		printf("arrival_script: %s\ndeparture_script: %s\n",
    189  1.10  martin 			arrival_script, departure_script);
    190  1.19  martin 		printf("carrier_script: %s\nno_carrier_script: %s\n",
    191  1.19  martin 			carrier_script, no_carrier_script);
    192   1.1  martin 		printf("verbosity = %d\n", verbose);
    193   1.1  martin 	}
    194   1.1  martin 
    195   1.1  martin 	while (argc > 0) {
    196  1.13  itojun 		list_interfaces(argv[0]);
    197  1.13  itojun 		argv++;
    198  1.13  itojun 		argc--;
    199   1.1  martin 	}
    200   1.1  martin 
    201   1.1  martin 	if (!verbose)
    202  1.21  dyoung 		daemon(0, 0);
    203   1.1  martin 
    204   1.1  martin 	s = socket(PF_ROUTE, SOCK_RAW, 0);
    205   1.1  martin 	if (s < 0) {
    206  1.14  martin 		syslog(LOG_ERR, "error opening routing socket: %m");
    207   1.1  martin 		perror("open routing socket");
    208   1.9    tron 		exit(EXIT_FAILURE);
    209   1.1  martin 	}
    210   1.1  martin 
    211  1.11  martin 	if (!inhibit_initial)
    212  1.11  martin 		run_initial_ups();
    213  1.11  martin 
    214   1.1  martin 	for (;;) {
    215   1.1  martin 		n = read(s, msg, sizeof msg);
    216   1.1  martin 		msgp = msg;
    217  1.21  dyoung 		for (msgp = msg; n > 0;
    218  1.21  dyoung 		     n -= ((struct rt_msghdr*)msgp)->rtm_msglen,
    219  1.21  dyoung 		     msgp += ((struct rt_msghdr*)msgp)->rtm_msglen)
    220   1.1  martin 			dispatch(msgp, n);
    221   1.1  martin 	}
    222   1.1  martin 
    223   1.1  martin 	close(s);
    224   1.1  martin 	free_interfaces();
    225  1.14  martin 	closelog();
    226   1.1  martin 
    227   1.9    tron 	return EXIT_SUCCESS;
    228   1.1  martin }
    229   1.1  martin 
    230   1.1  martin static void
    231  1.21  dyoung usage(void)
    232   1.1  martin {
    233  1.21  dyoung 	fprintf(stderr,
    234   1.1  martin 	    "usage:\n"
    235  1.15     wiz 	    "\tifwatchd [-hiqv] [-A arrival-script] [-D departure-script]\n"
    236  1.19  martin 	    "\t\t  [-d down-script] [-u up-script]\n"
    237  1.19  martin 	    "\t\t  [-c carrier-script] [-n no-carrier-script] ifname(s)\n"
    238   1.1  martin 	    "\twhere:\n"
    239  1.12     wiz 	    "\t -A <cmd> specify command to run on interface arrival event\n"
    240  1.19  martin 	    "\t -c <cmd> specify command to run on interface carrier-detect event\n"
    241  1.12     wiz 	    "\t -D <cmd> specify command to run on interface departure event\n"
    242  1.12     wiz 	    "\t -d <cmd> specify command to run on interface down event\n"
    243  1.19  martin 	    "\t -n <cmd> specify command to run on interface no-carrier-detect event\n"
    244   1.1  martin 	    "\t -h       show this help message\n"
    245   1.3  martin 	    "\t -i       no (!) initial run of the up script if the interface\n"
    246   1.3  martin 	    "\t          is already up on ifwatchd startup\n"
    247  1.16     abs 	    "\t -q       quiet mode, don't syslog informational messages\n"
    248   1.1  martin 	    "\t -u <cmd> specify command to run on interface up event\n"
    249  1.16     abs 	    "\t -v       verbose/debug output, don't run in background\n");
    250   1.9    tron 	exit(EXIT_FAILURE);
    251   1.1  martin }
    252   1.1  martin 
    253   1.1  martin static void
    254   1.1  martin dispatch(void *msg, size_t len)
    255   1.1  martin {
    256   1.1  martin 	struct rt_msghdr *hd = msg;
    257  1.19  martin 	struct if_msghdr *ifmp;
    258   1.1  martin 	struct ifa_msghdr *ifam;
    259  1.10  martin 	enum event ev;
    260   1.1  martin 
    261   1.1  martin 	switch (hd->rtm_type) {
    262   1.1  martin 	case RTM_NEWADDR:
    263  1.10  martin 		ev = UP;
    264   1.1  martin 		goto work;
    265   1.1  martin 	case RTM_DELADDR:
    266  1.10  martin 		ev = DOWN;
    267   1.1  martin 		goto work;
    268   1.1  martin 	case RTM_IFANNOUNCE:
    269   1.1  martin 		rescan_interfaces();
    270  1.10  martin 		check_announce((struct if_announcemsghdr *)msg);
    271  1.10  martin 		return;
    272  1.19  martin 	case RTM_IFINFO:
    273  1.19  martin 		ifmp = (struct if_msghdr*)msg;
    274  1.19  martin 		check_carrier(ifmp->ifm_index, ifmp->ifm_data.ifi_link_state);
    275  1.19  martin 		return;
    276  1.23   joerg 	case RTM_ADD:
    277  1.23   joerg 	case RTM_DELETE:
    278  1.23   joerg 	case RTM_CHANGE:
    279  1.23   joerg 	case RTM_LOSING:
    280  1.23   joerg 	case RTM_REDIRECT:
    281  1.23   joerg 	case RTM_MISS:
    282  1.23   joerg 	case RTM_IEEE80211:
    283  1.23   joerg 		return;
    284   1.1  martin 	}
    285   1.1  martin 	if (verbose)
    286  1.23   joerg 		printf("unknown message ignored (%d)\n", hd->rtm_type);
    287   1.1  martin 	return;
    288   1.1  martin 
    289   1.1  martin work:
    290   1.1  martin 	ifam = (struct ifa_msghdr *)msg;
    291  1.10  martin 	check_addrs((char *)(ifam + 1), ifam->ifam_addrs, ev);
    292   1.1  martin }
    293   1.1  martin 
    294   1.1  martin static void
    295  1.21  dyoung check_addrs(char *cp, int addrs, enum event ev)
    296   1.1  martin {
    297   1.4  martin 	struct sockaddr *sa, *ifa = NULL, *brd = NULL;
    298  1.18  martin 	char ifname_buf[IFNAMSIZ];
    299  1.18  martin 	const char *ifname;
    300   1.1  martin 	int ifndx = 0, i;
    301   1.1  martin 
    302   1.1  martin 	if (addrs == 0)
    303  1.13  itojun 		return;
    304   1.1  martin 	for (i = 1; i; i <<= 1) {
    305  1.21  dyoung 		if ((i & addrs) == 0)
    306  1.21  dyoung 			continue;
    307  1.21  dyoung 		sa = (struct sockaddr *)cp;
    308  1.21  dyoung 		if (i == RTA_IFP) {
    309  1.21  dyoung 			struct sockaddr_dl * li = (struct sockaddr_dl*)sa;
    310  1.21  dyoung 			ifndx = li->sdl_index;
    311  1.21  dyoung 			if (!find_interface(ifndx)) {
    312  1.21  dyoung 				if (verbose)
    313  1.21  dyoung 					printf("ignoring change on interface #%d\n", ifndx);
    314  1.21  dyoung 				return;
    315  1.13  itojun 			}
    316  1.21  dyoung 		} else if (i == RTA_IFA)
    317  1.21  dyoung 			ifa = sa;
    318  1.21  dyoung 		else if (i == RTA_BRD)
    319  1.21  dyoung 			brd = sa;
    320  1.21  dyoung 		ADVANCE(cp, sa);
    321   1.1  martin 	}
    322  1.18  martin 	if (ifa != NULL) {
    323  1.18  martin 		ifname = if_indextoname(ifndx, ifname_buf);
    324  1.18  martin 		if (ifname == NULL || ev < UP)
    325  1.18  martin 			invoke_script(ifa, brd, ev, ifndx, ifname);
    326  1.18  martin 		else if (ev == UP) {
    327  1.18  martin 			if (if_is_connected(ifname))
    328  1.18  martin 				invoke_script(ifa, brd, ev, ifndx, ifname);
    329  1.18  martin 		} else if (ev == DOWN) {
    330  1.18  martin 			if (if_is_not_connected(ifname))
    331  1.18  martin 				invoke_script(ifa, brd, ev, ifndx, ifname);
    332  1.18  martin 		}
    333  1.18  martin 	}
    334   1.1  martin }
    335   1.1  martin 
    336   1.1  martin static void
    337  1.21  dyoung invoke_script(struct sockaddr *sa, struct sockaddr *dest, enum event ev,
    338  1.21  dyoung     int ifindex, const char *ifname_hint)
    339   1.1  martin {
    340  1.10  martin 	char addr[NI_MAXHOST], daddr[NI_MAXHOST], ifname_buf[IFNAMSIZ];
    341  1.10  martin 	const char *ifname;
    342   1.1  martin 	const char *script;
    343   1.9    tron 	int status;
    344   1.8    tron 
    345  1.10  martin 	if (sa != NULL && sa->sa_len == 0) {
    346  1.13  itojun 		fprintf(stderr, "illegal socket address (sa_len == 0)\n");
    347  1.13  itojun 		return;
    348  1.10  martin 	}
    349  1.10  martin 	if (sa != NULL && sa->sa_family == AF_INET6) {
    350   1.8    tron 		struct sockaddr_in6 sin6;
    351   1.8    tron 
    352   1.8    tron 		(void) memcpy(&sin6, (struct sockaddr_in6 *)sa, sizeof (sin6));
    353   1.8    tron 		if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr))
    354   1.8    tron 			return;
    355   1.8    tron 	}
    356   1.1  martin 
    357  1.10  martin 	addr[0] = daddr[0] = 0;
    358   1.1  martin 	ifname = if_indextoname(ifindex, ifname_buf);
    359  1.10  martin 	ifname = ifname ? ifname : ifname_hint;
    360  1.10  martin 	if (ifname == NULL)
    361  1.13  itojun 		return;
    362   1.1  martin 
    363  1.10  martin 	if (sa != NULL) {
    364  1.13  itojun 		if (getnameinfo(sa, sa->sa_len, addr, sizeof addr, NULL, 0,
    365  1.13  itojun 		    NI_NUMERICHOST)) {
    366  1.13  itojun 			if (verbose)
    367  1.13  itojun 				printf("getnameinfo failed\n");
    368  1.13  itojun 			return;	/* this address can not be handled */
    369  1.13  itojun 		}
    370  1.10  martin 	}
    371   1.4  martin 	if (dest != NULL) {
    372  1.13  itojun 		if (getnameinfo(dest, dest->sa_len, daddr, sizeof daddr,
    373  1.13  itojun 		    NULL, 0, NI_NUMERICHOST)) {
    374  1.13  itojun 			if (verbose)
    375  1.13  itojun 				printf("getnameinfo failed\n");
    376  1.13  itojun 			return;	/* this address can not be handled */
    377  1.13  itojun 		}
    378   1.4  martin 	}
    379   1.1  martin 
    380  1.10  martin 	script = *scripts[ev];
    381   1.1  martin 	if (script == NULL) return;
    382   1.1  martin 
    383   1.9    tron 	if (verbose)
    384  1.13  itojun 		(void) printf("calling: %s %s %s %s %s %s\n",
    385  1.13  itojun 		    script, ifname, DummyTTY, DummySpeed, addr, daddr);
    386  1.14  martin 	if (!quiet)
    387  1.14  martin 		syslog(LOG_INFO, "calling: %s %s %s %s %s %s\n",
    388  1.14  martin 		    script, ifname, DummyTTY, DummySpeed, addr, daddr);
    389   1.9    tron 
    390   1.9    tron 	switch (vfork()) {
    391   1.9    tron 	case -1:
    392  1.13  itojun 		fprintf(stderr, "cannot fork\n");
    393  1.13  itojun 		break;
    394   1.9    tron 	case 0:
    395  1.14  martin 		if (execl(script, script, ifname, DummyTTY, DummySpeed,
    396  1.14  martin 		    addr, daddr, NULL) == -1) {
    397  1.14  martin 			syslog(LOG_ERR, "could not execute \"%s\": %m",
    398  1.14  martin 			    script);
    399  1.14  martin 			perror(script);
    400  1.14  martin 		}
    401  1.13  itojun 		_exit(EXIT_FAILURE);
    402   1.9    tron 	default:
    403  1.13  itojun 		(void) wait(&status);
    404   1.1  martin 	}
    405   1.1  martin }
    406   1.1  martin 
    407  1.19  martin static void
    408  1.19  martin list_interfaces(const char *ifnames)
    409   1.1  martin {
    410   1.1  martin 	char * names = strdup(ifnames);
    411   1.1  martin 	char * name, *lasts;
    412   1.1  martin 	static const char sep[] = " \t";
    413   1.1  martin 	struct interface_data * p;
    414   1.1  martin 
    415  1.13  itojun 	for (name = strtok_r(names, sep, &lasts);
    416  1.13  itojun 	    name != NULL;
    417  1.13  itojun 	    name = strtok_r(NULL, sep, &lasts)) {
    418  1.13  itojun 		p = malloc(sizeof(*p));
    419  1.13  itojun 		SLIST_INSERT_HEAD(&ifs, p, next);
    420  1.19  martin 		p->last_carrier_status = -1;
    421  1.13  itojun 		p->ifname = strdup(name);
    422  1.13  itojun 		p->index = if_nametoindex(p->ifname);
    423  1.14  martin 		if (!quiet)
    424  1.14  martin 			syslog(LOG_INFO, "watching interface %s", p->ifname);
    425  1.13  itojun 		if (verbose)
    426  1.13  itojun 			printf("interface \"%s\" has index %d\n",
    427  1.13  itojun 			    p->ifname, p->index);
    428   1.1  martin 	}
    429   1.1  martin 	free(names);
    430   1.1  martin }
    431   1.1  martin 
    432  1.10  martin static void
    433  1.19  martin check_carrier(int if_index, int carrier_status)
    434  1.19  martin {
    435  1.19  martin 	struct interface_data * p;
    436  1.19  martin 	enum event ev;
    437  1.19  martin 
    438  1.19  martin 	SLIST_FOREACH(p, &ifs, next)
    439  1.19  martin 		if (p->index == if_index)
    440  1.19  martin 			break;
    441  1.19  martin 
    442  1.19  martin 	if (p == NULL)
    443  1.19  martin 		return;
    444  1.19  martin 
    445  1.21  dyoung 	/*
    446  1.19  martin 	 * Treat it as an event worth handling if:
    447  1.19  martin 	 * - the carrier status changed, or
    448  1.19  martin 	 * - this is the first time we've been called, and
    449  1.19  martin 	 * inhibit_initial is not set
    450  1.19  martin 	 */
    451  1.19  martin 
    452  1.19  martin 	if ((carrier_status != p->last_carrier_status) ||
    453  1.19  martin 	    ((p->last_carrier_status == -1) && !inhibit_initial)) {
    454  1.19  martin 		switch (carrier_status) {
    455  1.19  martin 		case LINK_STATE_UP:
    456  1.19  martin 			ev = CARRIER;
    457  1.19  martin 			break;
    458  1.19  martin 		case LINK_STATE_DOWN:
    459  1.19  martin 			ev = NO_CARRIER;
    460  1.19  martin 			break;
    461  1.19  martin 		default:
    462  1.19  martin 			if (verbose)
    463  1.19  martin 				printf("unknown link status ignored\n");
    464  1.19  martin 			return;
    465  1.19  martin 		}
    466  1.19  martin 		invoke_script(NULL, NULL, ev, if_index, p->ifname);
    467  1.19  martin 		p->last_carrier_status = carrier_status;
    468  1.19  martin 	}
    469  1.19  martin }
    470  1.19  martin 
    471  1.19  martin static void
    472  1.10  martin check_announce(struct if_announcemsghdr *ifan)
    473  1.10  martin {
    474  1.10  martin 	struct interface_data * p;
    475  1.10  martin 	const char *ifname = ifan->ifan_name;
    476  1.10  martin 
    477  1.10  martin 	SLIST_FOREACH(p, &ifs, next) {
    478  1.21  dyoung 		if (strcmp(p->ifname, ifname) != 0)
    479  1.21  dyoung 			continue;
    480  1.21  dyoung 
    481  1.21  dyoung 		switch (ifan->ifan_what) {
    482  1.21  dyoung 		case IFAN_ARRIVAL:
    483  1.21  dyoung 			invoke_script(NULL, NULL, ARRIVAL, p->index,
    484  1.21  dyoung 			    NULL);
    485  1.21  dyoung 			break;
    486  1.21  dyoung 		case IFAN_DEPARTURE:
    487  1.21  dyoung 			invoke_script(NULL, NULL, DEPARTURE, p->index,
    488  1.21  dyoung 			    p->ifname);
    489  1.21  dyoung 			break;
    490  1.21  dyoung 		default:
    491  1.21  dyoung 			if (verbose)
    492  1.21  dyoung 				(void) printf("unknown announce: "
    493  1.21  dyoung 				    "what=%d\n", ifan->ifan_what);
    494  1.21  dyoung 			break;
    495  1.10  martin 		}
    496  1.21  dyoung 		return;
    497  1.10  martin 	}
    498  1.10  martin }
    499  1.10  martin 
    500  1.21  dyoung static void
    501  1.21  dyoung rescan_interfaces(void)
    502   1.1  martin {
    503   1.1  martin 	struct interface_data * p;
    504  1.21  dyoung 
    505   1.1  martin 	SLIST_FOREACH(p, &ifs, next) {
    506  1.13  itojun 		p->index = if_nametoindex(p->ifname);
    507  1.13  itojun 		if (verbose)
    508  1.13  itojun 			printf("interface \"%s\" has index %d\n", p->ifname,
    509  1.13  itojun 			    p->index);
    510   1.1  martin 	}
    511   1.1  martin }
    512   1.1  martin 
    513  1.21  dyoung static void
    514  1.21  dyoung free_interfaces(void)
    515   1.1  martin {
    516   1.1  martin 	struct interface_data * p;
    517   1.1  martin 
    518   1.1  martin 	while (!SLIST_EMPTY(&ifs)) {
    519  1.13  itojun 		p = SLIST_FIRST(&ifs);
    520  1.13  itojun 		SLIST_REMOVE_HEAD(&ifs, next);
    521  1.13  itojun 		free(p->ifname);
    522  1.13  itojun 		free(p);
    523   1.1  martin 	}
    524   1.1  martin }
    525   1.1  martin 
    526  1.21  dyoung static int
    527  1.21  dyoung find_interface(int index)
    528   1.1  martin {
    529   1.1  martin 	struct interface_data * p;
    530  1.21  dyoung 
    531   1.1  martin 	SLIST_FOREACH(p, &ifs, next)
    532  1.13  itojun 		if (p->index == index)
    533  1.13  itojun 			return 1;
    534   1.1  martin 	return 0;
    535   1.3  martin }
    536   1.3  martin 
    537  1.21  dyoung static void
    538  1.21  dyoung run_initial_ups(void)
    539   1.3  martin {
    540   1.3  martin 	struct interface_data * ifd;
    541   1.3  martin 	struct ifaddrs *res = NULL, *p;
    542  1.20  martin 	int s;
    543  1.20  martin 
    544  1.20  martin 	s = socket(AF_INET, SOCK_DGRAM, 0);
    545  1.20  martin 	if (s < 0)
    546  1.20  martin 		return;
    547   1.3  martin 
    548  1.21  dyoung 	if (getifaddrs(&res) != 0)
    549  1.21  dyoung 		goto out;
    550  1.21  dyoung 
    551  1.21  dyoung 	for (p = res; p; p = p->ifa_next) {
    552  1.21  dyoung 		SLIST_FOREACH(ifd, &ifs, next) {
    553  1.21  dyoung 			if (strcmp(ifd->ifname, p->ifa_name) == 0)
    554  1.21  dyoung 				break;
    555  1.21  dyoung 		}
    556  1.21  dyoung 		if (ifd == NULL)
    557  1.21  dyoung 			continue;
    558  1.13  itojun 
    559  1.21  dyoung 		if (p->ifa_addr && p->ifa_addr->sa_family == AF_LINK)
    560  1.21  dyoung 			invoke_script(NULL, NULL, ARRIVAL, ifd->index,
    561  1.21  dyoung 			    NULL);
    562  1.21  dyoung 
    563  1.21  dyoung 		if ((p->ifa_flags & IFF_UP) == 0)
    564  1.21  dyoung 			continue;
    565  1.21  dyoung 		if (p->ifa_addr == NULL)
    566  1.21  dyoung 			continue;
    567  1.21  dyoung 		if (p->ifa_addr->sa_family == AF_LINK) {
    568  1.21  dyoung 			struct ifmediareq ifmr;
    569  1.21  dyoung 
    570  1.21  dyoung 			memset(&ifmr, 0, sizeof(ifmr));
    571  1.21  dyoung 			strncpy(ifmr.ifm_name, ifd->ifname,
    572  1.21  dyoung 			    sizeof(ifmr.ifm_name));
    573  1.21  dyoung 			if (ioctl(s, SIOCGIFMEDIA, &ifmr) != -1
    574  1.21  dyoung 			    && (ifmr.ifm_status & IFM_AVALID)
    575  1.21  dyoung 			    && (ifmr.ifm_status & IFM_ACTIVE)) {
    576  1.21  dyoung 				invoke_script(NULL, NULL, CARRIER,
    577  1.13  itojun 				    ifd->index, ifd->ifname);
    578  1.21  dyoung 				ifd->last_carrier_status =
    579  1.21  dyoung 				    LINK_STATE_UP;
    580  1.21  dyoung 			    }
    581  1.21  dyoung 			continue;
    582  1.21  dyoung 		}
    583  1.21  dyoung 		if (if_is_connected(ifd->ifname))
    584  1.21  dyoung 			invoke_script(p->ifa_addr, p->ifa_dstaddr, UP,
    585  1.21  dyoung 			    ifd->index, ifd->ifname);
    586   1.3  martin 	}
    587  1.21  dyoung 	freeifaddrs(res);
    588  1.21  dyoung out:
    589  1.20  martin 	close(s);
    590   1.1  martin }
    591   1.5  martin 
    592   1.5  martin #ifdef SPPP_IF_SUPPORT
    593   1.5  martin /*
    594   1.5  martin  * Special case support for in-kernel PPP interfaces.
    595   1.5  martin  * If these are IFF_UP, but have not yet connected or completed authentication
    596   1.5  martin  * we don't want to call the up script in the initial interface scan (there
    597   1.5  martin  * will be an UP event generated later, when IPCP completes, anyway).
    598   1.5  martin  *
    599   1.5  martin  * If this is no if_spppsubr.c based interface, this ioctl just fails and we
    600   1.5  martin  * treat is as connected.
    601   1.5  martin  */
    602   1.5  martin static int
    603  1.21  dyoung check_is_connected(const char *ifname, int def_retval)
    604   1.5  martin {
    605   1.5  martin 	int s, err;
    606  1.18  martin 	struct spppstatus oldstatus;
    607  1.18  martin 	struct spppstatusncp status;
    608   1.5  martin 
    609   1.5  martin 	memset(&status, 0, sizeof status);
    610   1.5  martin 	strncpy(status.ifname, ifname, sizeof status.ifname);
    611  1.18  martin 	memset(&oldstatus, 0, sizeof oldstatus);
    612  1.18  martin 	strncpy(oldstatus.ifname, ifname, sizeof oldstatus.ifname);
    613  1.18  martin 
    614   1.5  martin 	s = socket(AF_INET, SOCK_DGRAM, 0);
    615   1.5  martin 	if (s < 0)
    616  1.13  itojun 		return 1;	/* no idea how to handle this... */
    617  1.18  martin 	err = ioctl(s, SPPPGETSTATUSNCP, &status);
    618  1.18  martin 	if (err != 0) {
    619  1.18  martin 		err = ioctl(s, SPPPGETSTATUS, &oldstatus);
    620  1.18  martin 		if (err != 0) {
    621  1.18  martin 			/* not if_spppsubr.c based - return default */
    622  1.18  martin 			close(s);
    623  1.18  martin 			return def_retval;
    624  1.18  martin 		} else {
    625  1.18  martin 			/* can't query NCPs, so use default */
    626  1.18  martin 			status.phase = oldstatus.phase;
    627  1.18  martin 			status.ncpup = def_retval;
    628  1.18  martin 		}
    629  1.18  martin 	}
    630   1.5  martin 	close(s);
    631  1.18  martin 
    632  1.18  martin 	return status.phase == SPPP_PHASE_NETWORK && status.ncpup > 0;
    633   1.5  martin }
    634   1.5  martin #endif
    635