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