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