Home | History | Annotate | Line # | Download | only in ifwatchd
ifwatchd.c revision 1.18
      1  1.18  martin /*	$NetBSD: ifwatchd.c,v 1.18 2003/12/27 00:05:46 martin 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.6  martin  * 3. All advertising materials mentioning features or use of this software
     19   1.6  martin  *    must display the following acknowledgement:
     20   1.6  martin  *        This product includes software developed by the NetBSD
     21   1.6  martin  *        Foundation, Inc. and its contributors.
     22   1.6  martin  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.6  martin  *    contributors may be used to endorse or promote products derived
     24   1.6  martin  *    from this software without specific prior written permission.
     25   1.1  martin  *
     26   1.6  martin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.6  martin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.6  martin  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.6  martin  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.6  martin  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.6  martin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.6  martin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.6  martin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.6  martin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.6  martin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.6  martin  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1  martin  */
     38   1.1  martin 
     39   1.5  martin /*
     40   1.5  martin  * Define this for special treatment of sys/net/if_spppsubr.c based interfaces.
     41   1.5  martin  */
     42   1.5  martin #define SPPP_IF_SUPPORT
     43   1.5  martin 
     44   1.1  martin #include <sys/types.h>
     45   1.1  martin #include <sys/param.h>
     46   1.3  martin #include <sys/ioctl.h>
     47   1.1  martin #include <sys/socket.h>
     48   1.2  martin #include <sys/queue.h>
     49   1.9    tron #include <sys/wait.h>
     50   1.1  martin #include <net/if.h>
     51   1.1  martin #include <net/if_dl.h>
     52   1.5  martin #ifdef SPPP_IF_SUPPORT
     53   1.5  martin #include <net/if_sppp.h>
     54   1.5  martin #endif
     55   1.1  martin #include <net/route.h>
     56   1.2  martin #include <netinet/in.h>
     57   1.2  martin #include <arpa/inet.h>
     58   1.1  martin 
     59   1.7    tron #include <paths.h>
     60   1.1  martin #include <stdio.h>
     61   1.1  martin #include <stdlib.h>
     62   1.1  martin #include <string.h>
     63   1.1  martin #include <unistd.h>
     64   1.1  martin #include <netdb.h>
     65   1.3  martin #include <err.h>
     66   1.3  martin #include <ifaddrs.h>
     67  1.14  martin #include <syslog.h>
     68   1.1  martin 
     69  1.10  martin enum event { ARRIVAL, DEPARTURE, UP, DOWN };
     70   1.1  martin /* local functions */
     71   1.1  martin static void usage(void);
     72   1.1  martin static void dispatch(void*, size_t);
     73  1.10  martin static void check_addrs(char *cp, int addrs, enum event ev);
     74  1.10  martin static void invoke_script(struct sockaddr *sa, struct sockaddr *dst, enum event ev, int ifindex, const char *ifname_hint);
     75   1.1  martin static void list_interfaces(const char *ifnames);
     76  1.10  martin static void check_announce(struct if_announcemsghdr *ifan);
     77   1.1  martin static void rescan_interfaces(void);
     78   1.1  martin static void free_interfaces(void);
     79   1.1  martin static int 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 /* stolen from /sbin/route */
     92   1.1  martin #define ROUNDUP(a) \
     93   1.1  martin 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
     94   1.1  martin #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
     95   1.1  martin 
     96   1.1  martin /* global variables */
     97  1.14  martin static int verbose = 0, quiet = 0;
     98   1.3  martin static int inhibit_initial = 0;
     99  1.10  martin static const char *arrival_script = NULL;
    100  1.10  martin static const char *departure_script = NULL;
    101   1.1  martin static const char *up_script = NULL;
    102   1.1  martin static const char *down_script = NULL;
    103   1.9    tron static char DummyTTY[] = _PATH_DEVNULL;
    104   1.9    tron static char DummySpeed[] = "9600";
    105  1.10  martin static const char **scripts[] = {
    106  1.10  martin 	&arrival_script,
    107  1.10  martin 	&departure_script,
    108  1.10  martin 	&up_script,
    109  1.10  martin 	&down_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.1  martin 	char * ifname;
    116   1.1  martin };
    117   1.1  martin 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.1  martin 	char msg[2048], *msgp;
    125   1.1  martin 
    126  1.14  martin 	openlog(argv[0], LOG_PID|LOG_CONS, LOG_DAEMON);
    127  1.14  martin 	while ((c = getopt(argc, argv, "qvhiu:d:A:D:")) != -1)
    128   1.1  martin 		switch (c) {
    129   1.1  martin 		case 'h':
    130   1.1  martin 			usage();
    131   1.1  martin 			return 0;
    132   1.3  martin 		case 'i':
    133   1.3  martin 			inhibit_initial = 1;
    134   1.3  martin 			break;
    135   1.1  martin 		case 'v':
    136   1.1  martin 			verbose++;
    137   1.1  martin 			break;
    138  1.14  martin 		case 'q':
    139  1.14  martin 			quiet = 1;
    140  1.14  martin 			break;
    141   1.1  martin 
    142   1.1  martin 		case 'u':
    143   1.1  martin 			up_script = optarg;
    144   1.1  martin 			break;
    145   1.1  martin 
    146   1.1  martin 		case 'd':
    147   1.1  martin 			down_script = optarg;
    148   1.1  martin 			break;
    149   1.1  martin 
    150  1.10  martin 		case 'A':
    151  1.10  martin 			arrival_script = optarg;
    152  1.10  martin 			break;
    153  1.10  martin 
    154  1.10  martin 		case 'D':
    155  1.10  martin 			departure_script = optarg;
    156  1.10  martin 			break;
    157  1.10  martin 
    158   1.1  martin 		default:
    159   1.1  martin 			errs++;
    160   1.1  martin 			break;
    161   1.1  martin 		}
    162   1.1  martin 
    163   1.1  martin 	if (errs)
    164   1.1  martin 		usage();
    165   1.1  martin 
    166   1.1  martin 	argv += optind;
    167   1.1  martin 	argc -= optind;
    168   1.1  martin 
    169   1.1  martin 	if (argc <= 0)
    170   1.1  martin 		usage();
    171   1.1  martin 
    172   1.1  martin 	if (verbose) {
    173   1.1  martin 		printf("up_script: %s\ndown_script: %s\n",
    174   1.1  martin 			up_script, down_script);
    175  1.10  martin 		printf("arrival_script: %s\ndeparture_script: %s\n",
    176  1.10  martin 			arrival_script, departure_script);
    177   1.1  martin 		printf("verbosity = %d\n", verbose);
    178   1.1  martin 	}
    179   1.1  martin 
    180   1.1  martin 	while (argc > 0) {
    181  1.13  itojun 		list_interfaces(argv[0]);
    182  1.13  itojun 		argv++;
    183  1.13  itojun 		argc--;
    184   1.1  martin 	}
    185   1.1  martin 
    186   1.1  martin 	if (!verbose)
    187   1.1  martin 		daemon(0,0);
    188   1.1  martin 
    189   1.1  martin 	s = socket(PF_ROUTE, SOCK_RAW, 0);
    190   1.1  martin 	if (s < 0) {
    191  1.14  martin 		syslog(LOG_ERR, "error opening routing socket: %m");
    192   1.1  martin 		perror("open routing socket");
    193   1.9    tron 		exit(EXIT_FAILURE);
    194   1.1  martin 	}
    195   1.1  martin 
    196  1.11  martin 	if (!inhibit_initial)
    197  1.11  martin 		run_initial_ups();
    198  1.11  martin 
    199   1.1  martin 	for (;;) {
    200   1.1  martin 		n = read(s, msg, sizeof msg);
    201   1.1  martin 		msgp = msg;
    202   1.1  martin 		for (msgp = msg; n > 0; n -= ((struct rt_msghdr*)msgp)->rtm_msglen, msgp += ((struct rt_msghdr*)msgp)->rtm_msglen) {
    203   1.1  martin 			dispatch(msgp, n);
    204   1.1  martin 
    205   1.1  martin 		}
    206   1.1  martin 	}
    207   1.1  martin 
    208   1.1  martin 	close(s);
    209   1.1  martin 	free_interfaces();
    210  1.14  martin 	closelog();
    211   1.1  martin 
    212   1.9    tron 	return EXIT_SUCCESS;
    213   1.1  martin }
    214   1.1  martin 
    215   1.1  martin static void
    216   1.1  martin usage()
    217   1.1  martin {
    218   1.1  martin 	fprintf(stderr,
    219   1.1  martin 	    "usage:\n"
    220  1.15     wiz 	    "\tifwatchd [-hiqv] [-A arrival-script] [-D departure-script]\n"
    221  1.14  martin 	    "\t\t  [-d down-script] [-u up-script] ifname(s)\n"
    222   1.1  martin 	    "\twhere:\n"
    223  1.12     wiz 	    "\t -A <cmd> specify command to run on interface arrival event\n"
    224  1.12     wiz 	    "\t -D <cmd> specify command to run on interface departure event\n"
    225  1.12     wiz 	    "\t -d <cmd> specify command to run on interface down event\n"
    226   1.1  martin 	    "\t -h       show this help message\n"
    227   1.3  martin 	    "\t -i       no (!) initial run of the up script if the interface\n"
    228   1.3  martin 	    "\t          is already up on ifwatchd startup\n"
    229  1.16     abs 	    "\t -q       quiet mode, don't syslog informational messages\n"
    230   1.1  martin 	    "\t -u <cmd> specify command to run on interface up event\n"
    231  1.16     abs 	    "\t -v       verbose/debug output, don't run in background\n");
    232   1.9    tron 	exit(EXIT_FAILURE);
    233   1.1  martin }
    234   1.1  martin 
    235   1.1  martin static void
    236   1.1  martin dispatch(void *msg, size_t len)
    237   1.1  martin {
    238   1.1  martin 	struct rt_msghdr *hd = msg;
    239   1.1  martin 	struct ifa_msghdr *ifam;
    240  1.10  martin 	enum event ev;
    241   1.1  martin 
    242   1.1  martin 	switch (hd->rtm_type) {
    243   1.1  martin 	case RTM_NEWADDR:
    244  1.10  martin 		ev = UP;
    245   1.1  martin 		goto work;
    246   1.1  martin 	case RTM_DELADDR:
    247  1.10  martin 		ev = DOWN;
    248   1.1  martin 		goto work;
    249   1.1  martin 	case RTM_IFANNOUNCE:
    250   1.1  martin 		rescan_interfaces();
    251  1.10  martin 		check_announce((struct if_announcemsghdr *)msg);
    252  1.10  martin 		return;
    253   1.1  martin 	}
    254   1.1  martin 	if (verbose)
    255   1.1  martin 		printf("unknown message ignored\n");
    256   1.1  martin 	return;
    257   1.1  martin 
    258   1.1  martin work:
    259   1.1  martin 	ifam = (struct ifa_msghdr *)msg;
    260  1.10  martin 	check_addrs((char *)(ifam + 1), ifam->ifam_addrs, ev);
    261   1.1  martin }
    262   1.1  martin 
    263   1.1  martin static void
    264  1.10  martin check_addrs(cp, addrs, ev)
    265   1.1  martin 	char    *cp;
    266  1.10  martin 	int     addrs;
    267  1.10  martin 	enum event ev;
    268   1.1  martin {
    269   1.4  martin 	struct sockaddr *sa, *ifa = NULL, *brd = NULL;
    270  1.18  martin 	char ifname_buf[IFNAMSIZ];
    271  1.18  martin 	const char *ifname;
    272   1.1  martin 	int ifndx = 0, i;
    273   1.1  martin 
    274   1.1  martin 	if (addrs == 0)
    275  1.13  itojun 		return;
    276   1.1  martin 	for (i = 1; i; i <<= 1) {
    277  1.13  itojun 		if (i & addrs) {
    278  1.13  itojun 			sa = (struct sockaddr *)cp;
    279  1.13  itojun 			if (i == RTA_IFP) {
    280  1.13  itojun 				struct sockaddr_dl * li = (struct sockaddr_dl*)sa;
    281  1.13  itojun 				ifndx = li->sdl_index;
    282  1.13  itojun 				if (!find_interface(ifndx)) {
    283  1.13  itojun 					if (verbose)
    284  1.13  itojun 						printf("ignoring change on interface #%d\n", ifndx);
    285  1.13  itojun 					return;
    286  1.13  itojun 				}
    287  1.13  itojun 			} else if (i == RTA_IFA) {
    288  1.13  itojun 				ifa = sa;
    289  1.13  itojun 			} else if (i == RTA_BRD) {
    290  1.13  itojun 				brd = sa;
    291  1.13  itojun 			}
    292  1.13  itojun 			ADVANCE(cp, sa);
    293   1.1  martin 		}
    294   1.1  martin 	}
    295  1.18  martin 	if (ifa != NULL) {
    296  1.18  martin 		ifname = if_indextoname(ifndx, ifname_buf);
    297  1.18  martin 		if (ifname == NULL || ev < UP)
    298  1.18  martin 			invoke_script(ifa, brd, ev, ifndx, ifname);
    299  1.18  martin 		else if (ev == UP) {
    300  1.18  martin 			if (if_is_connected(ifname))
    301  1.18  martin 				invoke_script(ifa, brd, ev, ifndx, ifname);
    302  1.18  martin 		} else if (ev == DOWN) {
    303  1.18  martin 			if (if_is_not_connected(ifname))
    304  1.18  martin 				invoke_script(ifa, brd, ev, ifndx, ifname);
    305  1.18  martin 		}
    306  1.18  martin 	}
    307   1.1  martin }
    308   1.1  martin 
    309   1.1  martin static void
    310  1.10  martin invoke_script(sa, dest, ev, ifindex, ifname_hint)
    311   1.4  martin 	struct sockaddr *sa, *dest;
    312  1.10  martin 	enum event ev;
    313  1.10  martin 	int ifindex;
    314  1.10  martin 	const char *ifname_hint;
    315   1.1  martin {
    316  1.10  martin 	char addr[NI_MAXHOST], daddr[NI_MAXHOST], ifname_buf[IFNAMSIZ];
    317  1.10  martin 	const char *ifname;
    318   1.1  martin 	const char *script;
    319   1.9    tron 	int status;
    320   1.8    tron 
    321  1.10  martin 	if (sa != NULL && sa->sa_len == 0) {
    322  1.13  itojun 		fprintf(stderr, "illegal socket address (sa_len == 0)\n");
    323  1.13  itojun 		return;
    324  1.10  martin 	}
    325  1.10  martin 	if (sa != NULL && sa->sa_family == AF_INET6) {
    326   1.8    tron 		struct sockaddr_in6 sin6;
    327   1.8    tron 
    328   1.8    tron 		(void) memcpy(&sin6, (struct sockaddr_in6 *)sa, sizeof (sin6));
    329   1.8    tron 		if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr))
    330   1.8    tron 			return;
    331   1.8    tron 	}
    332   1.1  martin 
    333  1.10  martin 	addr[0] = daddr[0] = 0;
    334   1.1  martin 	ifname = if_indextoname(ifindex, ifname_buf);
    335  1.10  martin 	ifname = ifname ? ifname : ifname_hint;
    336  1.10  martin 	if (ifname == NULL)
    337  1.13  itojun 		return;
    338   1.1  martin 
    339  1.10  martin 	if (sa != NULL) {
    340  1.13  itojun 		if (getnameinfo(sa, sa->sa_len, addr, sizeof addr, NULL, 0,
    341  1.13  itojun 		    NI_NUMERICHOST)) {
    342  1.13  itojun 			if (verbose)
    343  1.13  itojun 				printf("getnameinfo failed\n");
    344  1.13  itojun 			return;	/* this address can not be handled */
    345  1.13  itojun 		}
    346  1.10  martin 	}
    347   1.4  martin 	if (dest != NULL) {
    348  1.13  itojun 		if (getnameinfo(dest, dest->sa_len, daddr, sizeof daddr,
    349  1.13  itojun 		    NULL, 0, NI_NUMERICHOST)) {
    350  1.13  itojun 			if (verbose)
    351  1.13  itojun 				printf("getnameinfo failed\n");
    352  1.13  itojun 			return;	/* this address can not be handled */
    353  1.13  itojun 		}
    354   1.4  martin 	}
    355   1.1  martin 
    356  1.10  martin 	script = *scripts[ev];
    357   1.1  martin 	if (script == NULL) return;
    358   1.1  martin 
    359   1.9    tron 	if (verbose)
    360  1.13  itojun 		(void) printf("calling: %s %s %s %s %s %s\n",
    361  1.13  itojun 		    script, ifname, DummyTTY, DummySpeed, addr, daddr);
    362  1.14  martin 	if (!quiet)
    363  1.14  martin 		syslog(LOG_INFO, "calling: %s %s %s %s %s %s\n",
    364  1.14  martin 		    script, ifname, DummyTTY, DummySpeed, addr, daddr);
    365   1.9    tron 
    366   1.9    tron 	switch (vfork()) {
    367   1.9    tron 	case -1:
    368  1.13  itojun 		fprintf(stderr, "cannot fork\n");
    369  1.13  itojun 		break;
    370   1.9    tron 	case 0:
    371  1.14  martin 		if (execl(script, script, ifname, DummyTTY, DummySpeed,
    372  1.14  martin 		    addr, daddr, NULL) == -1) {
    373  1.14  martin 			syslog(LOG_ERR, "could not execute \"%s\": %m",
    374  1.14  martin 			    script);
    375  1.14  martin 			perror(script);
    376  1.14  martin 		}
    377  1.13  itojun 		_exit(EXIT_FAILURE);
    378   1.9    tron 	default:
    379  1.13  itojun 		(void) wait(&status);
    380   1.1  martin 	}
    381   1.1  martin }
    382   1.1  martin 
    383   1.1  martin static void list_interfaces(const char *ifnames)
    384   1.1  martin {
    385   1.1  martin 	char * names = strdup(ifnames);
    386   1.1  martin 	char * name, *lasts;
    387   1.1  martin 	static const char sep[] = " \t";
    388   1.1  martin 	struct interface_data * p;
    389   1.1  martin 
    390  1.13  itojun 	for (name = strtok_r(names, sep, &lasts);
    391  1.13  itojun 	    name != NULL;
    392  1.13  itojun 	    name = strtok_r(NULL, sep, &lasts)) {
    393  1.13  itojun 		p = malloc(sizeof(*p));
    394  1.13  itojun 		SLIST_INSERT_HEAD(&ifs, p, next);
    395  1.13  itojun 		p->ifname = strdup(name);
    396  1.13  itojun 		p->index = if_nametoindex(p->ifname);
    397  1.14  martin 		if (!quiet)
    398  1.14  martin 			syslog(LOG_INFO, "watching interface %s", p->ifname);
    399  1.13  itojun 		if (verbose)
    400  1.13  itojun 			printf("interface \"%s\" has index %d\n",
    401  1.13  itojun 			    p->ifname, p->index);
    402   1.1  martin 	}
    403   1.1  martin 	free(names);
    404   1.1  martin }
    405   1.1  martin 
    406  1.10  martin static void
    407  1.10  martin check_announce(struct if_announcemsghdr *ifan)
    408  1.10  martin {
    409  1.10  martin 	struct interface_data * p;
    410  1.10  martin 	const char *ifname = ifan->ifan_name;
    411  1.10  martin 
    412  1.10  martin 	SLIST_FOREACH(p, &ifs, next) {
    413  1.13  itojun 		if (strcmp(p->ifname, ifname) == 0) {
    414  1.13  itojun 			switch (ifan->ifan_what) {
    415  1.13  itojun 			case IFAN_ARRIVAL:
    416  1.13  itojun 				invoke_script(NULL, NULL, ARRIVAL, p->index,
    417  1.13  itojun 				    NULL);
    418  1.13  itojun 				break;
    419  1.13  itojun 			case IFAN_DEPARTURE:
    420  1.13  itojun 				invoke_script(NULL, NULL, DEPARTURE, p->index,
    421  1.13  itojun 				    p->ifname);
    422  1.13  itojun 				break;
    423  1.13  itojun 			default:
    424  1.13  itojun 				if (verbose)
    425  1.13  itojun 					(void) printf("unknown announce: "
    426  1.13  itojun 					    "what=%d\n", ifan->ifan_what);
    427  1.13  itojun 				break;
    428  1.13  itojun 			}
    429  1.13  itojun 			return;
    430  1.10  martin 		}
    431  1.10  martin 	}
    432  1.10  martin }
    433  1.10  martin 
    434   1.1  martin static void rescan_interfaces()
    435   1.1  martin {
    436   1.1  martin 	struct interface_data * p;
    437   1.1  martin 
    438   1.1  martin 	SLIST_FOREACH(p, &ifs, next) {
    439  1.13  itojun 		p->index = if_nametoindex(p->ifname);
    440  1.13  itojun 		if (verbose)
    441  1.13  itojun 			printf("interface \"%s\" has index %d\n", p->ifname,
    442  1.13  itojun 			    p->index);
    443   1.1  martin 	}
    444   1.1  martin }
    445   1.1  martin 
    446   1.1  martin static void free_interfaces()
    447   1.1  martin {
    448   1.1  martin 	struct interface_data * p;
    449   1.1  martin 
    450   1.1  martin 	while (!SLIST_EMPTY(&ifs)) {
    451  1.13  itojun 		p = SLIST_FIRST(&ifs);
    452  1.13  itojun 		SLIST_REMOVE_HEAD(&ifs, next);
    453  1.13  itojun 		free(p->ifname);
    454  1.13  itojun 		free(p);
    455   1.1  martin 	}
    456   1.1  martin }
    457   1.1  martin 
    458   1.1  martin static int find_interface(index)
    459   1.1  martin 	int index;
    460   1.1  martin {
    461   1.1  martin 	struct interface_data * p;
    462   1.1  martin 
    463   1.1  martin 	SLIST_FOREACH(p, &ifs, next)
    464  1.13  itojun 		if (p->index == index)
    465  1.13  itojun 			return 1;
    466   1.1  martin 	return 0;
    467   1.3  martin }
    468   1.3  martin 
    469   1.3  martin static void run_initial_ups()
    470   1.3  martin {
    471   1.3  martin 	struct interface_data * ifd;
    472   1.3  martin 	struct ifaddrs *res = NULL, *p;
    473   1.3  martin 
    474   1.3  martin 	if (getifaddrs(&res) == 0) {
    475  1.13  itojun 		for (p = res; p; p = p->ifa_next) {
    476  1.13  itojun 			SLIST_FOREACH(ifd, &ifs, next) {
    477  1.13  itojun 				if (strcmp(ifd->ifname, p->ifa_name) == 0)
    478  1.13  itojun 					break;
    479  1.13  itojun 			}
    480  1.13  itojun 			if (ifd == NULL)
    481  1.13  itojun 				continue;
    482  1.13  itojun 
    483  1.13  itojun 			if (p->ifa_addr && p->ifa_addr->sa_family == AF_LINK)
    484  1.13  itojun 				invoke_script(NULL, NULL, ARRIVAL, ifd->index,
    485  1.13  itojun 				    NULL);
    486  1.13  itojun 
    487  1.13  itojun 			if ((p->ifa_flags & IFF_UP) == 0)
    488  1.13  itojun 				continue;
    489  1.13  itojun 			if (p->ifa_addr == NULL)
    490  1.13  itojun 				continue;
    491  1.13  itojun 			if (p->ifa_addr->sa_family == AF_LINK)
    492  1.13  itojun 				continue;
    493  1.13  itojun 			if (if_is_connected(ifd->ifname))
    494  1.13  itojun 				invoke_script(p->ifa_addr, p->ifa_dstaddr, UP,
    495  1.13  itojun 				    ifd->index, ifd->ifname);
    496  1.11  martin 		}
    497  1.13  itojun 		freeifaddrs(res);
    498   1.3  martin 	}
    499   1.1  martin }
    500   1.5  martin 
    501   1.5  martin #ifdef SPPP_IF_SUPPORT
    502   1.5  martin /*
    503   1.5  martin  * Special case support for in-kernel PPP interfaces.
    504   1.5  martin  * If these are IFF_UP, but have not yet connected or completed authentication
    505   1.5  martin  * we don't want to call the up script in the initial interface scan (there
    506   1.5  martin  * will be an UP event generated later, when IPCP completes, anyway).
    507   1.5  martin  *
    508   1.5  martin  * If this is no if_spppsubr.c based interface, this ioctl just fails and we
    509   1.5  martin  * treat is as connected.
    510   1.5  martin  */
    511   1.5  martin static int
    512  1.18  martin check_is_connected(const char * ifname, int def_retval)
    513   1.5  martin {
    514   1.5  martin 	int s, err;
    515  1.18  martin 	struct spppstatus oldstatus;
    516  1.18  martin 	struct spppstatusncp status;
    517   1.5  martin 
    518   1.5  martin 	memset(&status, 0, sizeof status);
    519   1.5  martin 	strncpy(status.ifname, ifname, sizeof status.ifname);
    520  1.18  martin 	memset(&oldstatus, 0, sizeof oldstatus);
    521  1.18  martin 	strncpy(oldstatus.ifname, ifname, sizeof oldstatus.ifname);
    522  1.18  martin 
    523   1.5  martin 	s = socket(AF_INET, SOCK_DGRAM, 0);
    524   1.5  martin 	if (s < 0)
    525  1.13  itojun 		return 1;	/* no idea how to handle this... */
    526  1.18  martin 	err = ioctl(s, SPPPGETSTATUSNCP, &status);
    527  1.18  martin 	if (err != 0) {
    528  1.18  martin 		err = ioctl(s, SPPPGETSTATUS, &oldstatus);
    529  1.18  martin 		if (err != 0) {
    530  1.18  martin 			/* not if_spppsubr.c based - return default */
    531  1.18  martin 			close(s);
    532  1.18  martin 			return def_retval;
    533  1.18  martin 		} else {
    534  1.18  martin 			/* can't query NCPs, so use default */
    535  1.18  martin 			status.phase = oldstatus.phase;
    536  1.18  martin 			status.ncpup = def_retval;
    537  1.18  martin 		}
    538  1.18  martin 	}
    539   1.5  martin 	close(s);
    540  1.18  martin 
    541  1.18  martin 	return status.phase == SPPP_PHASE_NETWORK && status.ncpup > 0;
    542   1.5  martin }
    543   1.5  martin #endif
    544