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