Home | History | Annotate | Line # | Download | only in ifwatchd
ifwatchd.c revision 1.7
      1 /*	$NetBSD: ifwatchd.c,v 1.7 2002/04/15 17:32:18 tron Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Martin Husemann <martin (at) NetBSD.ORG>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Define this for special treatment of sys/net/if_spppsubr.c based interfaces.
     41  */
     42 #define SPPP_IF_SUPPORT
     43 
     44 #include <sys/types.h>
     45 #include <sys/param.h>
     46 #include <sys/ioctl.h>
     47 #include <sys/socket.h>
     48 #include <sys/queue.h>
     49 #include <net/if.h>
     50 #include <net/if_dl.h>
     51 #ifdef SPPP_IF_SUPPORT
     52 #include <net/if_sppp.h>
     53 #endif
     54 #include <net/route.h>
     55 #include <netinet/in.h>
     56 #include <arpa/inet.h>
     57 
     58 #include <paths.h>
     59 #include <stdio.h>
     60 #include <stdlib.h>
     61 #include <string.h>
     62 #include <unistd.h>
     63 #include <netdb.h>
     64 #include <err.h>
     65 #include <ifaddrs.h>
     66 
     67 /* local functions */
     68 static void usage(void);
     69 static void dispatch(void*, size_t);
     70 static void check_addrs(char *cp, int addrs, int is_up);
     71 static void invoke_script(struct sockaddr *sa, struct sockaddr *dst, int is_up, int ifindex);
     72 static void list_interfaces(const char *ifnames);
     73 static void rescan_interfaces(void);
     74 static void free_interfaces(void);
     75 static int find_interface(int index);
     76 static void run_initial_ups(void);
     77 
     78 #ifdef SPPP_IF_SUPPORT
     79 static int if_is_connected(const char * ifname);
     80 #else
     81 #define	if_is_connected(X)	1
     82 #endif
     83 
     84 /* stolen from /sbin/route */
     85 #define ROUNDUP(a) \
     86 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
     87 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
     88 
     89 /* global variables */
     90 static int verbose = 0;
     91 static int inhibit_initial = 0;
     92 static const char *up_script = NULL;
     93 static const char *down_script = NULL;
     94 
     95 struct interface_data {
     96 	SLIST_ENTRY(interface_data) next;
     97 	int index;
     98 	char * ifname;
     99 };
    100 SLIST_HEAD(,interface_data) ifs = SLIST_HEAD_INITIALIZER(ifs);
    101 
    102 int
    103 main(int argc, char **argv)
    104 {
    105 	int c, s, n;
    106 	int errs = 0;
    107 	char msg[2048], *msgp;
    108 
    109 	while ((c = getopt(argc, argv, "vhiu:d:")) != -1)
    110 		switch (c) {
    111 		case 'h':
    112 			usage();
    113 			return 0;
    114 		case 'i':
    115 			inhibit_initial = 1;
    116 			break;
    117 		case 'v':
    118 			verbose++;
    119 			break;
    120 
    121 		case 'u':
    122 			up_script = optarg;
    123 			break;
    124 
    125 		case 'd':
    126 			down_script = optarg;
    127 			break;
    128 
    129 		default:
    130 			errs++;
    131 			break;
    132 		}
    133 
    134 	if (errs)
    135 		usage();
    136 
    137 	argv += optind;
    138 	argc -= optind;
    139 
    140 	if (argc <= 0)
    141 		usage();
    142 
    143 	if (verbose) {
    144 		printf("up_script: %s\ndown_script: %s\n",
    145 			up_script, down_script);
    146 		printf("verbosity = %d\n", verbose);
    147 	}
    148 
    149 	while (argc > 0) {
    150 	    list_interfaces(argv[0]);
    151 	    argv++; argc--;
    152 	}
    153 
    154 	if (!verbose)
    155 		daemon(0,0);
    156 
    157 	if (!inhibit_initial)
    158 		run_initial_ups();
    159 
    160 	s = socket(PF_ROUTE, SOCK_RAW, 0);
    161 	if (s < 0) {
    162 		perror("open routing socket");
    163 		exit(1);
    164 	}
    165 
    166 	for (;;) {
    167 		n = read(s, msg, sizeof msg);
    168 		msgp = msg;
    169 		for (msgp = msg; n > 0; n -= ((struct rt_msghdr*)msgp)->rtm_msglen, msgp += ((struct rt_msghdr*)msgp)->rtm_msglen) {
    170 			dispatch(msgp, n);
    171 
    172 		}
    173 	}
    174 
    175 	close(s);
    176 	free_interfaces();
    177 
    178 	exit(0);
    179 }
    180 
    181 static void
    182 usage()
    183 {
    184 	fprintf(stderr,
    185 	    "usage:\n"
    186 	    "\tifwatchd [-h] [-v] [-u up-script] [-d down-script] ifname(s)\n"
    187 	    "\twhere:\n"
    188 	    "\t -h       show this help message\n"
    189 	    "\t -v       verbose/debug output, don't run in background\n"
    190 	    "\t -i       no (!) initial run of the up script if the interface\n"
    191 	    "\t          is already up on ifwatchd startup\n"
    192 	    "\t -u <cmd> specify command to run on interface up event\n"
    193 	    "\t -d <cmd> specify command to run on interface down event\n");
    194 	exit(1);
    195 }
    196 
    197 static void
    198 dispatch(void *msg, size_t len)
    199 {
    200 	struct rt_msghdr *hd = msg;
    201 	struct ifa_msghdr *ifam;
    202 	int is_up;
    203 
    204 	is_up = 0;
    205 	switch (hd->rtm_type) {
    206 	case RTM_NEWADDR:
    207 		is_up = 1;
    208 		goto work;
    209 	case RTM_DELADDR:
    210 		is_up = 0;
    211 		goto work;
    212 	case RTM_IFANNOUNCE:
    213 		rescan_interfaces();
    214 		break;
    215 	}
    216 	if (verbose)
    217 		printf("unknown message ignored\n");
    218 	return;
    219 
    220 work:
    221 	ifam = (struct ifa_msghdr *)msg;
    222 	check_addrs((char *)(ifam + 1), ifam->ifam_addrs, is_up);
    223 }
    224 
    225 static void
    226 check_addrs(cp, addrs, is_up)
    227 	char    *cp;
    228 	int     addrs, is_up;
    229 {
    230 	struct sockaddr *sa, *ifa = NULL, *brd = NULL;
    231 	int ifndx = 0, i;
    232 
    233 	if (addrs == 0)
    234 	    return;
    235 	for (i = 1; i; i <<= 1) {
    236 	    if (i & addrs) {
    237 		sa = (struct sockaddr *)cp;
    238 		if (i == RTA_IFP) {
    239 		    struct sockaddr_dl * li = (struct sockaddr_dl*)sa;
    240 		    ifndx = li->sdl_index;
    241 		    if (!find_interface(ifndx)) {
    242 			if (verbose)
    243 			    printf("ignoring change on interface #%d\n", ifndx);
    244 			return;
    245 		    }
    246 		} else if (i == RTA_IFA) {
    247 		    ifa = sa;
    248 		} else if (i == RTA_BRD) {
    249 		    brd = sa;
    250 		}
    251 		ADVANCE(cp, sa);
    252 	    }
    253 	}
    254 	if (ifa != NULL)
    255 	   invoke_script(ifa, brd, is_up, ifndx);
    256 }
    257 
    258 static void
    259 invoke_script(sa, dest, is_up, ifindex)
    260 	struct sockaddr *sa, *dest;
    261 	int is_up, ifindex;
    262 {
    263 	char addr[NI_MAXHOST], daddr[NI_MAXHOST], ifname_buf[IFNAMSIZ],
    264 	     *ifname, *cmd;
    265 	const char *script;
    266 
    267 	daddr[0] = 0;
    268 	ifname = if_indextoname(ifindex, ifname_buf);
    269 	if (sa->sa_len == 0) {
    270 	    fprintf(stderr, "illegal socket address (sa_len == 0)\n");
    271 	    return;
    272 	}
    273 
    274 	if (getnameinfo(sa, sa->sa_len, addr, sizeof addr, NULL, 0, NI_NUMERICHOST)) {
    275 	    if (verbose)
    276 		printf("getnameinfo failed\n");
    277 	    return;	/* this address can not be handled */
    278 	}
    279 	if (dest != NULL) {
    280 	    if (getnameinfo(dest, dest->sa_len, daddr, sizeof daddr, NULL, 0, NI_NUMERICHOST)) {
    281 		if (verbose)
    282 		    printf("getnameinfo failed\n");
    283 		return;	/* this address can not be handled */
    284 	    }
    285 	}
    286 
    287 	script = is_up? up_script : down_script;
    288 	if (script == NULL) return;
    289 
    290 	asprintf(&cmd, "%s \"%s\" " _PATH_DEVNULL " 9600 \"%s\" \"%s\"",
    291 			script, ifname, addr, daddr);
    292 	if (cmd == NULL) {
    293 	    fprintf(stderr, "out of memory\n");
    294 	    return;
    295 	}
    296 	if (verbose)
    297 	    printf("calling: %s\n", cmd);
    298 	system(cmd);
    299 	free(cmd);
    300 }
    301 
    302 static void list_interfaces(const char *ifnames)
    303 {
    304 	char * names = strdup(ifnames);
    305 	char * name, *lasts;
    306 	static const char sep[] = " \t";
    307 	struct interface_data * p;
    308 
    309 	for (name = strtok_r(names, sep, &lasts); name != NULL; name = strtok_r(NULL, sep, &lasts)) {
    310 	    p = malloc(sizeof(*p));
    311 	    SLIST_INSERT_HEAD(&ifs, p, next);
    312 	    p->ifname = strdup(name);
    313 	    p->index = if_nametoindex(p->ifname);
    314 	    if (verbose)
    315 		printf("interface \"%s\" has index %d\n", p->ifname, p->index);
    316 	}
    317 	free(names);
    318 }
    319 
    320 static void rescan_interfaces()
    321 {
    322 	struct interface_data * p;
    323 
    324 	SLIST_FOREACH(p, &ifs, next) {
    325 	    p->index = if_nametoindex(p->ifname);
    326 	    if (verbose)
    327 		printf("interface \"%s\" has index %d\n", p->ifname, p->index);
    328 	}
    329 }
    330 
    331 static void free_interfaces()
    332 {
    333 	struct interface_data * p;
    334 
    335 	while (!SLIST_EMPTY(&ifs)) {
    336 	    p = SLIST_FIRST(&ifs);
    337 	    SLIST_REMOVE_HEAD(&ifs, next);
    338 	    free(p->ifname);
    339 	    free(p);
    340 	}
    341 }
    342 
    343 static int find_interface(index)
    344 	int index;
    345 {
    346 	struct interface_data * p;
    347 
    348 	SLIST_FOREACH(p, &ifs, next)
    349 	    if (p->index == index)
    350 		return 1;
    351 	return 0;
    352 }
    353 
    354 static void run_initial_ups()
    355 {
    356 	struct interface_data * ifd;
    357 	struct ifaddrs *res = NULL, *p;
    358 
    359 	if (getifaddrs(&res) == 0) {
    360 	    for (p = res; p; p = p->ifa_next) {
    361 		if ((p->ifa_flags & IFF_UP) == 0)
    362 		    continue;
    363 		if (p->ifa_addr == NULL)
    364 		    continue;
    365 		if (p->ifa_addr->sa_family == AF_LINK)
    366 		    continue;
    367 		SLIST_FOREACH(ifd, &ifs, next) {
    368 		    if (strcmp(ifd->ifname, p->ifa_name) == 0) {
    369 		    	if (if_is_connected(ifd->ifname))
    370 			    invoke_script(p->ifa_addr, p->ifa_dstaddr, 1, ifd->index);
    371 			break;
    372 		    }
    373 		}
    374 	    }
    375 	    freeifaddrs(res);
    376 	}
    377 }
    378 
    379 #ifdef SPPP_IF_SUPPORT
    380 /*
    381  * Special case support for in-kernel PPP interfaces.
    382  * If these are IFF_UP, but have not yet connected or completed authentication
    383  * we don't want to call the up script in the initial interface scan (there
    384  * will be an UP event generated later, when IPCP completes, anyway).
    385  *
    386  * If this is no if_spppsubr.c based interface, this ioctl just fails and we
    387  * treat is as connected.
    388  */
    389 static int
    390 if_is_connected(const char * ifname)
    391 {
    392 	int s, err;
    393 	struct spppstatus status;
    394 
    395 	memset(&status, 0, sizeof status);
    396 	strncpy(status.ifname, ifname, sizeof status.ifname);
    397 	s = socket(AF_INET, SOCK_DGRAM, 0);
    398 	if (s < 0)
    399 	    return 1;	/* no idea how to handle this... */
    400 	err = ioctl(s, SPPPGETSTATUS, &status);
    401 	if (err != 0)
    402 	    /* not if_spppsubr.c based - call it connected */
    403 	    status.phase = SPPP_PHASE_NETWORK;
    404 	close(s);
    405 	return status.phase == SPPP_PHASE_NETWORK;
    406 }
    407 #endif
    408