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