Home | History | Annotate | Line # | Download | only in ifwatchd
ifwatchd.c revision 1.14
      1  1.14  martin /*	$NetBSD: ifwatchd.c,v 1.14 2003/06/23 21:50:12 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.6  martin  * 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.5  martin static int if_is_connected(const char * ifname);
     84   1.5  martin #else
     85   1.5  martin #define	if_is_connected(X)	1
     86   1.5  martin #endif
     87   1.5  martin 
     88   1.1  martin /* stolen from /sbin/route */
     89   1.1  martin #define ROUNDUP(a) \
     90   1.1  martin 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
     91   1.1  martin #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
     92   1.1  martin 
     93   1.1  martin /* global variables */
     94  1.14  martin static int verbose = 0, quiet = 0;
     95   1.3  martin static int inhibit_initial = 0;
     96  1.10  martin static const char *arrival_script = NULL;
     97  1.10  martin static const char *departure_script = NULL;
     98   1.1  martin static const char *up_script = NULL;
     99   1.1  martin static const char *down_script = NULL;
    100   1.9    tron static char DummyTTY[] = _PATH_DEVNULL;
    101   1.9    tron static 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.10  martin 	&down_script
    107  1.13  itojun };
    108   1.1  martin 
    109   1.1  martin struct interface_data {
    110   1.1  martin 	SLIST_ENTRY(interface_data) next;
    111   1.1  martin 	int index;
    112   1.1  martin 	char * ifname;
    113   1.1  martin };
    114   1.1  martin SLIST_HEAD(,interface_data) ifs = SLIST_HEAD_INITIALIZER(ifs);
    115   1.1  martin 
    116   1.1  martin int
    117   1.1  martin main(int argc, char **argv)
    118   1.1  martin {
    119   1.1  martin 	int c, s, n;
    120   1.1  martin 	int errs = 0;
    121   1.1  martin 	char msg[2048], *msgp;
    122   1.1  martin 
    123  1.14  martin 	openlog(argv[0], LOG_PID|LOG_CONS, LOG_DAEMON);
    124  1.14  martin 	while ((c = getopt(argc, argv, "qvhiu:d:A:D:")) != -1)
    125   1.1  martin 		switch (c) {
    126   1.1  martin 		case 'h':
    127   1.1  martin 			usage();
    128   1.1  martin 			return 0;
    129   1.3  martin 		case 'i':
    130   1.3  martin 			inhibit_initial = 1;
    131   1.3  martin 			break;
    132   1.1  martin 		case 'v':
    133   1.1  martin 			verbose++;
    134   1.1  martin 			break;
    135  1.14  martin 		case 'q':
    136  1.14  martin 			quiet = 1;
    137  1.14  martin 			break;
    138   1.1  martin 
    139   1.1  martin 		case 'u':
    140   1.1  martin 			up_script = optarg;
    141   1.1  martin 			break;
    142   1.1  martin 
    143   1.1  martin 		case 'd':
    144   1.1  martin 			down_script = optarg;
    145   1.1  martin 			break;
    146   1.1  martin 
    147  1.10  martin 		case 'A':
    148  1.10  martin 			arrival_script = optarg;
    149  1.10  martin 			break;
    150  1.10  martin 
    151  1.10  martin 		case 'D':
    152  1.10  martin 			departure_script = optarg;
    153  1.10  martin 			break;
    154  1.10  martin 
    155   1.1  martin 		default:
    156   1.1  martin 			errs++;
    157   1.1  martin 			break;
    158   1.1  martin 		}
    159   1.1  martin 
    160   1.1  martin 	if (errs)
    161   1.1  martin 		usage();
    162   1.1  martin 
    163   1.1  martin 	argv += optind;
    164   1.1  martin 	argc -= optind;
    165   1.1  martin 
    166   1.1  martin 	if (argc <= 0)
    167   1.1  martin 		usage();
    168   1.1  martin 
    169   1.1  martin 	if (verbose) {
    170   1.1  martin 		printf("up_script: %s\ndown_script: %s\n",
    171   1.1  martin 			up_script, down_script);
    172  1.10  martin 		printf("arrival_script: %s\ndeparture_script: %s\n",
    173  1.10  martin 			arrival_script, departure_script);
    174   1.1  martin 		printf("verbosity = %d\n", verbose);
    175   1.1  martin 	}
    176   1.1  martin 
    177   1.1  martin 	while (argc > 0) {
    178  1.13  itojun 		list_interfaces(argv[0]);
    179  1.13  itojun 		argv++;
    180  1.13  itojun 		argc--;
    181   1.1  martin 	}
    182   1.1  martin 
    183   1.1  martin 	if (!verbose)
    184   1.1  martin 		daemon(0,0);
    185   1.1  martin 
    186   1.1  martin 	s = socket(PF_ROUTE, SOCK_RAW, 0);
    187   1.1  martin 	if (s < 0) {
    188  1.14  martin 		syslog(LOG_ERR, "error opening routing socket: %m");
    189   1.1  martin 		perror("open routing socket");
    190   1.9    tron 		exit(EXIT_FAILURE);
    191   1.1  martin 	}
    192   1.1  martin 
    193  1.11  martin 	if (!inhibit_initial)
    194  1.11  martin 		run_initial_ups();
    195  1.11  martin 
    196   1.1  martin 	for (;;) {
    197   1.1  martin 		n = read(s, msg, sizeof msg);
    198   1.1  martin 		msgp = msg;
    199   1.1  martin 		for (msgp = msg; n > 0; n -= ((struct rt_msghdr*)msgp)->rtm_msglen, msgp += ((struct rt_msghdr*)msgp)->rtm_msglen) {
    200   1.1  martin 			dispatch(msgp, n);
    201   1.1  martin 
    202   1.1  martin 		}
    203   1.1  martin 	}
    204   1.1  martin 
    205   1.1  martin 	close(s);
    206   1.1  martin 	free_interfaces();
    207  1.14  martin 	closelog();
    208   1.1  martin 
    209   1.9    tron 	return EXIT_SUCCESS;
    210   1.1  martin }
    211   1.1  martin 
    212   1.1  martin static void
    213   1.1  martin usage()
    214   1.1  martin {
    215   1.1  martin 	fprintf(stderr,
    216   1.1  martin 	    "usage:\n"
    217  1.14  martin 	    "\tifwatchd [-hiv] [-A arrival-script] [-D departure-script]\n"
    218  1.14  martin 	    "\t\t  [-d down-script] [-u up-script] ifname(s)\n"
    219   1.1  martin 	    "\twhere:\n"
    220  1.12     wiz 	    "\t -A <cmd> specify command to run on interface arrival event\n"
    221  1.12     wiz 	    "\t -D <cmd> specify command to run on interface departure event\n"
    222  1.12     wiz 	    "\t -d <cmd> specify command to run on interface down event\n"
    223   1.1  martin 	    "\t -h       show this help message\n"
    224   1.3  martin 	    "\t -i       no (!) initial run of the up script if the interface\n"
    225   1.3  martin 	    "\t          is already up on ifwatchd startup\n"
    226   1.1  martin 	    "\t -u <cmd> specify command to run on interface up event\n"
    227  1.14  martin 	    "\t -v       verbose/debug output, don't run in background\n"
    228  1.14  martin 	    "\t -q       quiet mode, don't syslog informational messages\n");
    229   1.9    tron 	exit(EXIT_FAILURE);
    230   1.1  martin }
    231   1.1  martin 
    232   1.1  martin static void
    233   1.1  martin dispatch(void *msg, size_t len)
    234   1.1  martin {
    235   1.1  martin 	struct rt_msghdr *hd = msg;
    236   1.1  martin 	struct ifa_msghdr *ifam;
    237  1.10  martin 	enum event ev;
    238   1.1  martin 
    239   1.1  martin 	switch (hd->rtm_type) {
    240   1.1  martin 	case RTM_NEWADDR:
    241  1.10  martin 		ev = UP;
    242   1.1  martin 		goto work;
    243   1.1  martin 	case RTM_DELADDR:
    244  1.10  martin 		ev = DOWN;
    245   1.1  martin 		goto work;
    246   1.1  martin 	case RTM_IFANNOUNCE:
    247   1.1  martin 		rescan_interfaces();
    248  1.10  martin 		check_announce((struct if_announcemsghdr *)msg);
    249  1.10  martin 		return;
    250   1.1  martin 	}
    251   1.1  martin 	if (verbose)
    252   1.1  martin 		printf("unknown message ignored\n");
    253   1.1  martin 	return;
    254   1.1  martin 
    255   1.1  martin work:
    256   1.1  martin 	ifam = (struct ifa_msghdr *)msg;
    257  1.10  martin 	check_addrs((char *)(ifam + 1), ifam->ifam_addrs, ev);
    258   1.1  martin }
    259   1.1  martin 
    260   1.1  martin static void
    261  1.10  martin check_addrs(cp, addrs, ev)
    262   1.1  martin 	char    *cp;
    263  1.10  martin 	int     addrs;
    264  1.10  martin 	enum event ev;
    265   1.1  martin {
    266   1.4  martin 	struct sockaddr *sa, *ifa = NULL, *brd = NULL;
    267   1.1  martin 	int ifndx = 0, i;
    268   1.1  martin 
    269   1.1  martin 	if (addrs == 0)
    270  1.13  itojun 		return;
    271   1.1  martin 	for (i = 1; i; i <<= 1) {
    272  1.13  itojun 		if (i & addrs) {
    273  1.13  itojun 			sa = (struct sockaddr *)cp;
    274  1.13  itojun 			if (i == RTA_IFP) {
    275  1.13  itojun 				struct sockaddr_dl * li = (struct sockaddr_dl*)sa;
    276  1.13  itojun 				ifndx = li->sdl_index;
    277  1.13  itojun 				if (!find_interface(ifndx)) {
    278  1.13  itojun 					if (verbose)
    279  1.13  itojun 						printf("ignoring change on interface #%d\n", ifndx);
    280  1.13  itojun 					return;
    281  1.13  itojun 				}
    282  1.13  itojun 			} else if (i == RTA_IFA) {
    283  1.13  itojun 				ifa = sa;
    284  1.13  itojun 			} else if (i == RTA_BRD) {
    285  1.13  itojun 				brd = sa;
    286  1.13  itojun 			}
    287  1.13  itojun 			ADVANCE(cp, sa);
    288   1.1  martin 		}
    289   1.1  martin 	}
    290   1.4  martin 	if (ifa != NULL)
    291  1.13  itojun 		invoke_script(ifa, brd, ev, ifndx, NULL);
    292   1.1  martin }
    293   1.1  martin 
    294   1.1  martin static void
    295  1.10  martin invoke_script(sa, dest, ev, ifindex, ifname_hint)
    296   1.4  martin 	struct sockaddr *sa, *dest;
    297  1.10  martin 	enum event ev;
    298  1.10  martin 	int ifindex;
    299  1.10  martin 	const char *ifname_hint;
    300   1.1  martin {
    301  1.10  martin 	char addr[NI_MAXHOST], daddr[NI_MAXHOST], ifname_buf[IFNAMSIZ];
    302  1.10  martin 	const char *ifname;
    303   1.1  martin 	const char *script;
    304   1.9    tron 	int status;
    305   1.8    tron 
    306  1.10  martin 	if (sa != NULL && sa->sa_len == 0) {
    307  1.13  itojun 		fprintf(stderr, "illegal socket address (sa_len == 0)\n");
    308  1.13  itojun 		return;
    309  1.10  martin 	}
    310  1.10  martin 	if (sa != NULL && sa->sa_family == AF_INET6) {
    311   1.8    tron 		struct sockaddr_in6 sin6;
    312   1.8    tron 
    313   1.8    tron 		(void) memcpy(&sin6, (struct sockaddr_in6 *)sa, sizeof (sin6));
    314   1.8    tron 		if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr))
    315   1.8    tron 			return;
    316   1.8    tron 	}
    317   1.1  martin 
    318  1.10  martin 	addr[0] = daddr[0] = 0;
    319   1.1  martin 	ifname = if_indextoname(ifindex, ifname_buf);
    320  1.10  martin 	ifname = ifname ? ifname : ifname_hint;
    321  1.10  martin 	if (ifname == NULL)
    322  1.13  itojun 		return;
    323   1.1  martin 
    324  1.10  martin 	if (sa != NULL) {
    325  1.13  itojun 		if (getnameinfo(sa, sa->sa_len, addr, sizeof addr, NULL, 0,
    326  1.13  itojun 		    NI_NUMERICHOST)) {
    327  1.13  itojun 			if (verbose)
    328  1.13  itojun 				printf("getnameinfo failed\n");
    329  1.13  itojun 			return;	/* this address can not be handled */
    330  1.13  itojun 		}
    331  1.10  martin 	}
    332   1.4  martin 	if (dest != NULL) {
    333  1.13  itojun 		if (getnameinfo(dest, dest->sa_len, daddr, sizeof daddr,
    334  1.13  itojun 		    NULL, 0, NI_NUMERICHOST)) {
    335  1.13  itojun 			if (verbose)
    336  1.13  itojun 				printf("getnameinfo failed\n");
    337  1.13  itojun 			return;	/* this address can not be handled */
    338  1.13  itojun 		}
    339   1.4  martin 	}
    340   1.1  martin 
    341  1.10  martin 	script = *scripts[ev];
    342   1.1  martin 	if (script == NULL) return;
    343   1.1  martin 
    344   1.9    tron 	if (verbose)
    345  1.13  itojun 		(void) printf("calling: %s %s %s %s %s %s\n",
    346  1.13  itojun 		    script, ifname, DummyTTY, DummySpeed, addr, daddr);
    347  1.14  martin 	if (!quiet)
    348  1.14  martin 		syslog(LOG_INFO, "calling: %s %s %s %s %s %s\n",
    349  1.14  martin 		    script, ifname, DummyTTY, DummySpeed, addr, daddr);
    350   1.9    tron 
    351   1.9    tron 	switch (vfork()) {
    352   1.9    tron 	case -1:
    353  1.13  itojun 		fprintf(stderr, "cannot fork\n");
    354  1.13  itojun 		break;
    355   1.9    tron 	case 0:
    356  1.14  martin 		if (execl(script, script, ifname, DummyTTY, DummySpeed,
    357  1.14  martin 		    addr, daddr, NULL) == -1) {
    358  1.14  martin 			syslog(LOG_ERR, "could not execute \"%s\": %m",
    359  1.14  martin 			    script);
    360  1.14  martin 			perror(script);
    361  1.14  martin 		}
    362  1.13  itojun 		_exit(EXIT_FAILURE);
    363   1.9    tron 	default:
    364  1.13  itojun 		(void) wait(&status);
    365   1.1  martin 	}
    366   1.1  martin }
    367   1.1  martin 
    368   1.1  martin static void list_interfaces(const char *ifnames)
    369   1.1  martin {
    370   1.1  martin 	char * names = strdup(ifnames);
    371   1.1  martin 	char * name, *lasts;
    372   1.1  martin 	static const char sep[] = " \t";
    373   1.1  martin 	struct interface_data * p;
    374   1.1  martin 
    375  1.13  itojun 	for (name = strtok_r(names, sep, &lasts);
    376  1.13  itojun 	    name != NULL;
    377  1.13  itojun 	    name = strtok_r(NULL, sep, &lasts)) {
    378  1.13  itojun 		p = malloc(sizeof(*p));
    379  1.13  itojun 		SLIST_INSERT_HEAD(&ifs, p, next);
    380  1.13  itojun 		p->ifname = strdup(name);
    381  1.13  itojun 		p->index = if_nametoindex(p->ifname);
    382  1.14  martin 		if (!quiet)
    383  1.14  martin 			syslog(LOG_INFO, "watching interface %s", p->ifname);
    384  1.13  itojun 		if (verbose)
    385  1.13  itojun 			printf("interface \"%s\" has index %d\n",
    386  1.13  itojun 			    p->ifname, p->index);
    387   1.1  martin 	}
    388   1.1  martin 	free(names);
    389   1.1  martin }
    390   1.1  martin 
    391  1.10  martin static void
    392  1.10  martin check_announce(struct if_announcemsghdr *ifan)
    393  1.10  martin {
    394  1.10  martin 	struct interface_data * p;
    395  1.10  martin 	const char *ifname = ifan->ifan_name;
    396  1.10  martin 
    397  1.10  martin 	SLIST_FOREACH(p, &ifs, next) {
    398  1.13  itojun 		if (strcmp(p->ifname, ifname) == 0) {
    399  1.13  itojun 			switch (ifan->ifan_what) {
    400  1.13  itojun 			case IFAN_ARRIVAL:
    401  1.13  itojun 				invoke_script(NULL, NULL, ARRIVAL, p->index,
    402  1.13  itojun 				    NULL);
    403  1.13  itojun 				break;
    404  1.13  itojun 			case IFAN_DEPARTURE:
    405  1.13  itojun 				invoke_script(NULL, NULL, DEPARTURE, p->index,
    406  1.13  itojun 				    p->ifname);
    407  1.13  itojun 				break;
    408  1.13  itojun 			default:
    409  1.13  itojun 				if (verbose)
    410  1.13  itojun 					(void) printf("unknown announce: "
    411  1.13  itojun 					    "what=%d\n", ifan->ifan_what);
    412  1.13  itojun 				break;
    413  1.13  itojun 			}
    414  1.13  itojun 			return;
    415  1.10  martin 		}
    416  1.10  martin 	}
    417  1.10  martin }
    418  1.10  martin 
    419   1.1  martin static void rescan_interfaces()
    420   1.1  martin {
    421   1.1  martin 	struct interface_data * p;
    422   1.1  martin 
    423   1.1  martin 	SLIST_FOREACH(p, &ifs, next) {
    424  1.13  itojun 		p->index = if_nametoindex(p->ifname);
    425  1.13  itojun 		if (verbose)
    426  1.13  itojun 			printf("interface \"%s\" has index %d\n", p->ifname,
    427  1.13  itojun 			    p->index);
    428   1.1  martin 	}
    429   1.1  martin }
    430   1.1  martin 
    431   1.1  martin static void free_interfaces()
    432   1.1  martin {
    433   1.1  martin 	struct interface_data * p;
    434   1.1  martin 
    435   1.1  martin 	while (!SLIST_EMPTY(&ifs)) {
    436  1.13  itojun 		p = SLIST_FIRST(&ifs);
    437  1.13  itojun 		SLIST_REMOVE_HEAD(&ifs, next);
    438  1.13  itojun 		free(p->ifname);
    439  1.13  itojun 		free(p);
    440   1.1  martin 	}
    441   1.1  martin }
    442   1.1  martin 
    443   1.1  martin static int find_interface(index)
    444   1.1  martin 	int index;
    445   1.1  martin {
    446   1.1  martin 	struct interface_data * p;
    447   1.1  martin 
    448   1.1  martin 	SLIST_FOREACH(p, &ifs, next)
    449  1.13  itojun 		if (p->index == index)
    450  1.13  itojun 			return 1;
    451   1.1  martin 	return 0;
    452   1.3  martin }
    453   1.3  martin 
    454   1.3  martin static void run_initial_ups()
    455   1.3  martin {
    456   1.3  martin 	struct interface_data * ifd;
    457   1.3  martin 	struct ifaddrs *res = NULL, *p;
    458   1.3  martin 
    459   1.3  martin 	if (getifaddrs(&res) == 0) {
    460  1.13  itojun 		for (p = res; p; p = p->ifa_next) {
    461  1.13  itojun 			SLIST_FOREACH(ifd, &ifs, next) {
    462  1.13  itojun 				if (strcmp(ifd->ifname, p->ifa_name) == 0)
    463  1.13  itojun 					break;
    464  1.13  itojun 			}
    465  1.13  itojun 			if (ifd == NULL)
    466  1.13  itojun 				continue;
    467  1.13  itojun 
    468  1.13  itojun 			if (p->ifa_addr && p->ifa_addr->sa_family == AF_LINK)
    469  1.13  itojun 				invoke_script(NULL, NULL, ARRIVAL, ifd->index,
    470  1.13  itojun 				    NULL);
    471  1.13  itojun 
    472  1.13  itojun 			if ((p->ifa_flags & IFF_UP) == 0)
    473  1.13  itojun 				continue;
    474  1.13  itojun 			if (p->ifa_addr == NULL)
    475  1.13  itojun 				continue;
    476  1.13  itojun 			if (p->ifa_addr->sa_family == AF_LINK)
    477  1.13  itojun 				continue;
    478  1.13  itojun 			if (if_is_connected(ifd->ifname))
    479  1.13  itojun 				invoke_script(p->ifa_addr, p->ifa_dstaddr, UP,
    480  1.13  itojun 				    ifd->index, ifd->ifname);
    481  1.11  martin 		}
    482  1.13  itojun 		freeifaddrs(res);
    483   1.3  martin 	}
    484   1.1  martin }
    485   1.5  martin 
    486   1.5  martin #ifdef SPPP_IF_SUPPORT
    487   1.5  martin /*
    488   1.5  martin  * Special case support for in-kernel PPP interfaces.
    489   1.5  martin  * If these are IFF_UP, but have not yet connected or completed authentication
    490   1.5  martin  * we don't want to call the up script in the initial interface scan (there
    491   1.5  martin  * will be an UP event generated later, when IPCP completes, anyway).
    492   1.5  martin  *
    493   1.5  martin  * If this is no if_spppsubr.c based interface, this ioctl just fails and we
    494   1.5  martin  * treat is as connected.
    495   1.5  martin  */
    496   1.5  martin static int
    497   1.5  martin if_is_connected(const char * ifname)
    498   1.5  martin {
    499   1.5  martin 	int s, err;
    500   1.5  martin 	struct spppstatus status;
    501   1.5  martin 
    502   1.5  martin 	memset(&status, 0, sizeof status);
    503   1.5  martin 	strncpy(status.ifname, ifname, sizeof status.ifname);
    504   1.5  martin 	s = socket(AF_INET, SOCK_DGRAM, 0);
    505   1.5  martin 	if (s < 0)
    506  1.13  itojun 		return 1;	/* no idea how to handle this... */
    507   1.5  martin 	err = ioctl(s, SPPPGETSTATUS, &status);
    508   1.5  martin 	if (err != 0)
    509  1.13  itojun 		/* not if_spppsubr.c based - call it connected */
    510  1.13  itojun 		status.phase = SPPP_PHASE_NETWORK;
    511   1.5  martin 	close(s);
    512   1.5  martin 	return status.phase == SPPP_PHASE_NETWORK;
    513   1.5  martin }
    514   1.5  martin #endif
    515