ifwatchd.c revision 1.46 1 /* $NetBSD: ifwatchd.c,v 1.46 2020/10/04 20:36:32 roy Exp $ */
2 #include <sys/cdefs.h>
3 __RCSID("$NetBSD: ifwatchd.c,v 1.46 2020/10/04 20:36:32 roy Exp $");
4
5 /*-
6 * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Martin Husemann <martin (at) NetBSD.org>.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 #include <sys/queue.h>
39 #include <sys/wait.h>
40 #include <net/if.h>
41 #include <net/if_dl.h>
42 #include <net/route.h>
43 #include <netinet/in.h>
44 #include <netinet/in_var.h>
45 #include <arpa/inet.h>
46
47 #include <err.h>
48 #include <errno.h>
49 #include <ifaddrs.h>
50 #include <netdb.h>
51 #include <paths.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <syslog.h>
56 #include <unistd.h>
57
58 enum event { ARRIVAL, DEPARTURE, UP, DOWN, CARRIER, NO_CARRIER };
59 enum addrflag { NOTREADY, DETACHED, READY };
60
61 /* local functions */
62 __dead static void usage(void);
63 static void dispatch(const void *, size_t);
64 static enum addrflag check_addrflags(int af, int addrflags);
65 static void check_addrs(const struct ifa_msghdr *ifam);
66 static void invoke_script(const char *ifname, enum event ev,
67 const struct sockaddr *sa, const struct sockaddr *dst);
68 static void list_interfaces(const char *ifnames);
69 static void check_announce(const struct if_announcemsghdr *ifan);
70 static void check_carrier(const struct if_msghdr *ifm);
71 static void free_interfaces(void);
72 static struct interface_data * find_interface(int index);
73 static void run_initial_ups(bool);
74
75 /* global variables */
76 static int verbose = 0, quiet = 0;
77 static int inhibit_initial = 0;
78 static const char *arrival_script = NULL;
79 static const char *departure_script = NULL;
80 static const char *up_script = NULL;
81 static const char *down_script = NULL;
82 static const char *carrier_script = NULL;
83 static const char *no_carrier_script = NULL;
84 static const char DummyTTY[] = _PATH_DEVNULL;
85 static const char DummySpeed[] = "9600";
86 static const char **scripts[] = {
87 &arrival_script,
88 &departure_script,
89 &up_script,
90 &down_script,
91 &carrier_script,
92 &no_carrier_script
93 };
94
95 struct interface_data {
96 SLIST_ENTRY(interface_data) next;
97 int index;
98 int last_carrier_status;
99 char * ifname;
100 };
101 static SLIST_HEAD(,interface_data) ifs = SLIST_HEAD_INITIALIZER(ifs);
102
103 int
104 main(int argc, char **argv)
105 {
106 int c, s, n;
107 int errs = 0;
108 struct msghdr msg;
109 struct iovec iov[1];
110 char buf[2048];
111 unsigned char msgfilter[] = {
112 RTM_IFINFO, RTM_IFANNOUNCE,
113 RTM_NEWADDR, RTM_DELADDR,
114 };
115
116 openlog(argv[0], LOG_PID|LOG_CONS, LOG_DAEMON);
117 while ((c = getopt(argc, argv, "qvhic:n:u:d:A:D:")) != -1) {
118 switch (c) {
119 case 'h':
120 usage();
121 return 0;
122
123 case 'i':
124 inhibit_initial = 1;
125 break;
126
127 case 'v':
128 verbose++;
129 break;
130
131 case 'q':
132 quiet = 1;
133 break;
134
135 case 'c':
136 carrier_script = optarg;
137 break;
138
139 case 'n':
140 no_carrier_script = optarg;
141 break;
142
143 case 'u':
144 up_script = optarg;
145 break;
146
147 case 'd':
148 down_script = optarg;
149 break;
150
151 case 'A':
152 arrival_script = optarg;
153 break;
154
155 case 'D':
156 departure_script = optarg;
157 break;
158
159 default:
160 errs++;
161 break;
162 }
163 }
164
165 if (errs)
166 usage();
167
168 argv += optind;
169 argc -= optind;
170
171 if (argc <= 0)
172 usage();
173
174 if (verbose) {
175 printf("up_script: %s\ndown_script: %s\n",
176 up_script, down_script);
177 printf("arrival_script: %s\ndeparture_script: %s\n",
178 arrival_script, departure_script);
179 printf("carrier_script: %s\nno_carrier_script: %s\n",
180 carrier_script, no_carrier_script);
181 printf("verbosity = %d\n", verbose);
182 }
183
184 while (argc > 0) {
185 list_interfaces(argv[0]);
186 argv++;
187 argc--;
188 }
189
190 if (!verbose)
191 daemon(0, 0);
192
193 s = socket(PF_ROUTE, SOCK_RAW, 0);
194 if (s < 0) {
195 syslog(LOG_ERR, "error opening routing socket: %m");
196 exit(EXIT_FAILURE);
197 }
198 if (setsockopt(s, PF_ROUTE, RO_MSGFILTER,
199 &msgfilter, sizeof(msgfilter)) < 0)
200 syslog(LOG_ERR, "RO_MSGFILTER: %m");
201 n = 1;
202 if (setsockopt(s, SOL_SOCKET, SO_RERROR, &n, sizeof(n)) < 0)
203 syslog(LOG_ERR, "SO_RERROR: %m");
204
205 if (!inhibit_initial)
206 run_initial_ups(true);
207
208 iov[0].iov_base = buf;
209 iov[0].iov_len = sizeof(buf);
210 memset(&msg, 0, sizeof(msg));
211 msg.msg_iov = iov;
212 msg.msg_iovlen = 1;
213
214 for (;;) {
215 n = recvmsg(s, &msg, 0);
216 if (n == -1) {
217 if (errno == ENOBUFS) {
218 syslog(LOG_ERR,
219 "routing socket overflow detected");
220 /* XXX We don't track addresses, so they
221 * won't be reported. */
222 if (!inhibit_initial)
223 run_initial_ups(false);
224 continue;
225 }
226 syslog(LOG_ERR, "recvmsg: %m");
227 exit(EXIT_FAILURE);
228 }
229 if (n != 0)
230 dispatch(iov[0].iov_base, n);
231 }
232
233 close(s);
234 free_interfaces();
235 closelog();
236
237 return EXIT_SUCCESS;
238 }
239
240 static void
241 usage(void)
242 {
243 fprintf(stderr,
244 "usage:\n"
245 "\tifwatchd [-hiqv] [-A arrival-script] [-D departure-script]\n"
246 "\t\t [-d down-script] [-u up-script]\n"
247 "\t\t [-c carrier-script] [-n no-carrier-script] ifname(s)\n"
248 "\twhere:\n"
249 "\t -A <cmd> specify command to run on interface arrival event\n"
250 "\t -c <cmd> specify command to run on interface carrier-detect event\n"
251 "\t -D <cmd> specify command to run on interface departure event\n"
252 "\t -d <cmd> specify command to run on interface down event\n"
253 "\t -n <cmd> specify command to run on interface no-carrier-detect event\n"
254 "\t -h show this help message\n"
255 "\t -i no (!) initial run of the up script if the interface\n"
256 "\t is already up on ifwatchd startup\n"
257 "\t -q quiet mode, don't syslog informational messages\n"
258 "\t -u <cmd> specify command to run on interface up event\n"
259 "\t -v verbose/debug output, don't run in background\n");
260 exit(EXIT_FAILURE);
261 }
262
263 static void
264 dispatch(const void *msg, size_t len)
265 {
266 const struct rt_msghdr *hd = msg;
267
268 if (hd->rtm_version != RTM_VERSION)
269 return;
270
271 switch (hd->rtm_type) {
272 case RTM_NEWADDR:
273 case RTM_DELADDR:
274 check_addrs(msg);
275 break;
276 case RTM_IFANNOUNCE:
277 check_announce(msg);
278 break;
279 case RTM_IFINFO:
280 check_carrier(msg);
281 break;
282 default:
283 /* Should be impossible as we filter messages. */
284 if (verbose)
285 printf("unknown message ignored (%d)\n", hd->rtm_type);
286 break;
287 }
288 }
289
290 static enum addrflag
291 check_addrflags(int af, int addrflags)
292 {
293
294 switch (af) {
295 case AF_INET:
296 if (addrflags & IN_IFF_NOTREADY)
297 return NOTREADY;
298 if (addrflags & IN_IFF_DETACHED)
299 return DETACHED;
300 break;
301 case AF_INET6:
302 if (addrflags & IN6_IFF_NOTREADY)
303 return NOTREADY;
304 if (addrflags & IN6_IFF_DETACHED)
305 return DETACHED;
306 break;
307 }
308 return READY;
309 }
310
311 static void
312 check_addrs(const struct ifa_msghdr *ifam)
313 {
314 const char *cp = (const char *)(ifam + 1);
315 const struct sockaddr *sa, *ifa = NULL, *brd = NULL;
316 unsigned i;
317 struct interface_data *ifd = NULL;
318 int aflag;
319 enum event ev;
320
321 if (ifam->ifam_addrs == 0)
322 return;
323 for (i = 1; i; i <<= 1) {
324 if ((i & ifam->ifam_addrs) == 0)
325 continue;
326 sa = (const struct sockaddr *)cp;
327 if (i == RTA_IFP) {
328 const struct sockaddr_dl *li;
329
330 li = (const struct sockaddr_dl *)sa;
331 if ((ifd = find_interface(li->sdl_index)) == NULL) {
332 if (verbose)
333 printf("ignoring change"
334 " on interface #%d\n",
335 li->sdl_index);
336 return;
337 }
338 } else if (i == RTA_IFA)
339 ifa = sa;
340 else if (i == RTA_BRD)
341 brd = sa;
342 RT_ADVANCE(cp, sa);
343 }
344 if (ifa != NULL && ifd != NULL) {
345 ev = ifam->ifam_type == RTM_DELADDR ? DOWN : UP;
346 aflag = check_addrflags(ifa->sa_family, ifam->ifam_addrflags);
347 if ((ev == UP && aflag == READY) || ev == DOWN)
348 invoke_script(ifd->ifname, ev, ifa, brd);
349 }
350 }
351
352 static void
353 invoke_script(const char *ifname, enum event ev,
354 const struct sockaddr *sa, const struct sockaddr *dest)
355 {
356 char addr[NI_MAXHOST], daddr[NI_MAXHOST];
357 const char *script;
358 int status;
359
360 if (ifname == NULL)
361 return;
362
363 script = *scripts[ev];
364 if (script == NULL)
365 return;
366
367 addr[0] = daddr[0] = 0;
368 if (sa != NULL) {
369 const struct sockaddr_in *sin;
370 const struct sockaddr_in6 *sin6;
371
372 if (sa->sa_len == 0) {
373 syslog(LOG_ERR,
374 "illegal socket address (sa_len == 0)");
375 return;
376 }
377 switch (sa->sa_family) {
378 case AF_INET:
379 sin = (const struct sockaddr_in *)sa;
380 if (sin->sin_addr.s_addr == INADDR_ANY ||
381 sin->sin_addr.s_addr == INADDR_BROADCAST)
382 return;
383 break;
384 case AF_INET6:
385 sin6 = (const struct sockaddr_in6 *)sa;
386 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
387 return;
388 break;
389 default:
390 break;
391 }
392
393 if (getnameinfo(sa, sa->sa_len, addr, sizeof addr, NULL, 0,
394 NI_NUMERICHOST)) {
395 if (verbose)
396 printf("getnameinfo failed\n");
397 return; /* this address can not be handled */
398 }
399 }
400
401 if (dest != NULL) {
402 if (getnameinfo(dest, dest->sa_len, daddr, sizeof daddr,
403 NULL, 0, NI_NUMERICHOST)) {
404 if (verbose)
405 printf("getnameinfo failed\n");
406 return; /* this address can not be handled */
407 }
408 }
409
410 if (verbose)
411 (void) printf("calling: %s %s %s %s %s %s\n",
412 script, ifname, DummyTTY, DummySpeed, addr, daddr);
413 if (!quiet)
414 syslog(LOG_INFO, "calling: %s %s %s %s %s %s\n",
415 script, ifname, DummyTTY, DummySpeed, addr, daddr);
416
417 switch (vfork()) {
418 case -1:
419 syslog(LOG_ERR, "cannot fork: %m");
420 break;
421 case 0:
422 if (execl(script, script, ifname, DummyTTY, DummySpeed,
423 addr, daddr, NULL) == -1) {
424 syslog(LOG_ERR, "could not execute \"%s\": %m",
425 script);
426 }
427 _exit(EXIT_FAILURE);
428 default:
429 (void) wait(&status);
430 }
431 }
432
433 static void
434 list_interfaces(const char *ifnames)
435 {
436 char * names = strdup(ifnames);
437 char * name, *lasts;
438 static const char sep[] = " \t";
439 struct interface_data * p;
440
441 for (name = strtok_r(names, sep, &lasts);
442 name != NULL;
443 name = strtok_r(NULL, sep, &lasts)) {
444 p = malloc(sizeof(*p));
445 SLIST_INSERT_HEAD(&ifs, p, next);
446 p->last_carrier_status = -1;
447 p->ifname = strdup(name);
448 p->index = if_nametoindex(p->ifname);
449 if (!quiet)
450 syslog(LOG_INFO, "watching interface %s", p->ifname);
451 if (verbose)
452 printf("interface \"%s\" has index %d\n",
453 p->ifname, p->index);
454 }
455 free(names);
456 }
457
458 static void
459 check_carrier(const struct if_msghdr *ifm)
460 {
461 struct interface_data * p;
462 int carrier_status;
463 enum event ev;
464
465 SLIST_FOREACH(p, &ifs, next)
466 if (p->index == ifm->ifm_index)
467 break;
468
469 if (p == NULL)
470 return;
471
472 /*
473 * Treat it as an event worth handling if:
474 * - the carrier status changed, or
475 * - this is the first time we've been called, and
476 * inhibit_initial is not set
477 */
478 carrier_status = ifm->ifm_data.ifi_link_state;
479 if (carrier_status != p->last_carrier_status) {
480 switch (carrier_status) {
481 case LINK_STATE_UP:
482 ev = CARRIER;
483 break;
484 case LINK_STATE_DOWN:
485 ev = NO_CARRIER;
486 break;
487 default:
488 if (verbose)
489 printf("unknown link status ignored\n");
490 return;
491 }
492 invoke_script(p->ifname, ev, NULL, NULL);
493 p->last_carrier_status = carrier_status;
494 }
495 }
496
497 static void
498 do_announce(struct interface_data *ifd,
499 unsigned short what, unsigned short index)
500 {
501
502 switch (what) {
503 case IFAN_ARRIVAL:
504 ifd->index = index;
505 invoke_script(ifd->ifname, ARRIVAL, NULL, NULL);
506 break;
507 case IFAN_DEPARTURE:
508 ifd->index = -1;
509 ifd->last_carrier_status = -1;
510 invoke_script(ifd->ifname, DEPARTURE, NULL, NULL);
511 break;
512 default:
513 if (verbose)
514 (void) printf("unknown announce: what=%d\n", what);
515 break;
516 }
517 }
518
519 static void
520 check_announce(const struct if_announcemsghdr *ifan)
521 {
522 struct interface_data * p;
523 const char *ifname = ifan->ifan_name;
524
525 SLIST_FOREACH(p, &ifs, next) {
526 if (strcmp(p->ifname, ifname) != 0)
527 continue;
528
529 do_announce(p, ifan->ifan_what, ifan->ifan_index);
530 return;
531 }
532 }
533
534 static void
535 free_interfaces(void)
536 {
537 struct interface_data * p;
538
539 while (!SLIST_EMPTY(&ifs)) {
540 p = SLIST_FIRST(&ifs);
541 SLIST_REMOVE_HEAD(&ifs, next);
542 free(p->ifname);
543 free(p);
544 }
545 }
546
547 static struct interface_data *
548 find_interface(int idx)
549 {
550 struct interface_data * p;
551
552 SLIST_FOREACH(p, &ifs, next)
553 if (p->index == idx)
554 return p;
555 return NULL;
556 }
557
558 static void
559 run_initial_ups(bool do_addrs)
560 {
561 struct interface_data * ifd;
562 struct ifaddrs *res = NULL, *p;
563 struct sockaddr *ifa;
564 const struct if_data *ifi;
565 int s, aflag;
566
567 s = socket(AF_INET, SOCK_DGRAM, 0);
568 if (s < 0)
569 return;
570
571 if (getifaddrs(&res) != 0)
572 goto out;
573
574 /* Check if any interfaces vanished */
575 SLIST_FOREACH(ifd, &ifs, next) {
576 for (p = res; p; p = p->ifa_next) {
577 if (strcmp(ifd->ifname, p->ifa_name) != 0)
578 continue;
579 ifa = p->ifa_addr;
580 if (ifa != NULL && ifa->sa_family == AF_LINK)
581 break;
582 }
583 if (p == NULL)
584 do_announce(ifd, IFAN_DEPARTURE, ifd->index);
585 }
586
587 for (p = res; p; p = p->ifa_next) {
588 SLIST_FOREACH(ifd, &ifs, next) {
589 if (strcmp(ifd->ifname, p->ifa_name) == 0)
590 break;
591 }
592 if (ifd == NULL)
593 continue;
594
595 ifa = p->ifa_addr;
596 if (ifa != NULL && ifa->sa_family == AF_LINK &&
597 ifd->index == -1)
598 invoke_script(ifd->ifname, ARRIVAL, NULL, NULL);
599
600 if ((p->ifa_flags & IFF_UP) == 0)
601 continue;
602 if (ifa == NULL)
603 continue;
604 if (ifa->sa_family == AF_LINK) {
605 ifi = (const struct if_data *)p->ifa_data;
606 if (ifd->last_carrier_status == ifi->ifi_link_state)
607 continue;
608 switch (ifi->ifi_link_state) {
609 case LINK_STATE_UP:
610 invoke_script(ifd->ifname, CARRIER, NULL, NULL);
611 break;
612 case LINK_STATE_DOWN:
613 if (ifd->last_carrier_status == -1)
614 break;
615 invoke_script(ifd->ifname, CARRIER, NULL, NULL);
616 break;
617 }
618 ifd->last_carrier_status = ifi->ifi_link_state;
619 continue;
620 }
621 if (!do_addrs)
622 continue;
623 aflag = check_addrflags(ifa->sa_family, p->ifa_addrflags);
624 if (aflag != READY)
625 continue;
626 invoke_script(ifd->ifname, UP, ifa, p->ifa_dstaddr);
627 }
628 freeifaddrs(res);
629 out:
630 close(s);
631 }
632