ifwatchd.c revision 1.10 1 /* $NetBSD: ifwatchd.c,v 1.10 2003/03/05 09:03:49 martin 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++; argc--;
175 }
176
177 if (!verbose)
178 daemon(0,0);
179
180 if (!inhibit_initial)
181 run_initial_ups();
182
183 s = socket(PF_ROUTE, SOCK_RAW, 0);
184 if (s < 0) {
185 perror("open routing socket");
186 exit(EXIT_FAILURE);
187 }
188
189 for (;;) {
190 n = read(s, msg, sizeof msg);
191 msgp = msg;
192 for (msgp = msg; n > 0; n -= ((struct rt_msghdr*)msgp)->rtm_msglen, msgp += ((struct rt_msghdr*)msgp)->rtm_msglen) {
193 dispatch(msgp, n);
194
195 }
196 }
197
198 close(s);
199 free_interfaces();
200
201 return EXIT_SUCCESS;
202 }
203
204 static void
205 usage()
206 {
207 fprintf(stderr,
208 "usage:\n"
209 "\tifwatchd [-h] [-v] [-u up-script] [-d down-script] [-A arrival-script] [-D departure-script] ifname(s)\n"
210 "\twhere:\n"
211 "\t -h show this help message\n"
212 "\t -v verbose/debug output, don't run in background\n"
213 "\t -i no (!) initial run of the up script if the interface\n"
214 "\t is already up on ifwatchd startup\n"
215 "\t -u <cmd> specify command to run on interface up event\n"
216 "\t -d <cmd> specify command to run on interface down event\n"
217 "\t -A <cmd> specify command to run on interface arrival event\n"
218 "\t -D <cmd> specify command to run on interface departure event\n");
219 exit(EXIT_FAILURE);
220 }
221
222 static void
223 dispatch(void *msg, size_t len)
224 {
225 struct rt_msghdr *hd = msg;
226 struct ifa_msghdr *ifam;
227 enum event ev;
228
229 switch (hd->rtm_type) {
230 case RTM_NEWADDR:
231 ev = UP;
232 goto work;
233 case RTM_DELADDR:
234 ev = DOWN;
235 goto work;
236 case RTM_IFANNOUNCE:
237 rescan_interfaces();
238 check_announce((struct if_announcemsghdr *)msg);
239 return;
240 }
241 if (verbose)
242 printf("unknown message ignored\n");
243 return;
244
245 work:
246 ifam = (struct ifa_msghdr *)msg;
247 check_addrs((char *)(ifam + 1), ifam->ifam_addrs, ev);
248 }
249
250 static void
251 check_addrs(cp, addrs, ev)
252 char *cp;
253 int addrs;
254 enum event ev;
255 {
256 struct sockaddr *sa, *ifa = NULL, *brd = NULL;
257 int ifndx = 0, i;
258
259 if (addrs == 0)
260 return;
261 for (i = 1; i; i <<= 1) {
262 if (i & addrs) {
263 sa = (struct sockaddr *)cp;
264 if (i == RTA_IFP) {
265 struct sockaddr_dl * li = (struct sockaddr_dl*)sa;
266 ifndx = li->sdl_index;
267 if (!find_interface(ifndx)) {
268 if (verbose)
269 printf("ignoring change on interface #%d\n", ifndx);
270 return;
271 }
272 } else if (i == RTA_IFA) {
273 ifa = sa;
274 } else if (i == RTA_BRD) {
275 brd = sa;
276 }
277 ADVANCE(cp, sa);
278 }
279 }
280 if (ifa != NULL)
281 invoke_script(ifa, brd, ev, ifndx, NULL);
282 }
283
284 static void
285 invoke_script(sa, dest, ev, ifindex, ifname_hint)
286 struct sockaddr *sa, *dest;
287 enum event ev;
288 int ifindex;
289 const char *ifname_hint;
290 {
291 char addr[NI_MAXHOST], daddr[NI_MAXHOST], ifname_buf[IFNAMSIZ];
292 const char *ifname;
293 const char *script;
294 int status;
295
296 if (sa != NULL && sa->sa_len == 0) {
297 fprintf(stderr, "illegal socket address (sa_len == 0)\n");
298 return;
299 }
300 if (sa != NULL && sa->sa_family == AF_INET6) {
301 struct sockaddr_in6 sin6;
302
303 (void) memcpy(&sin6, (struct sockaddr_in6 *)sa, sizeof (sin6));
304 if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr))
305 return;
306 }
307
308 addr[0] = daddr[0] = 0;
309 ifname = if_indextoname(ifindex, ifname_buf);
310 ifname = ifname ? ifname : ifname_hint;
311 if (ifname == NULL)
312 return;
313
314 if (sa != NULL) {
315 if (getnameinfo(sa, sa->sa_len, addr, sizeof addr, NULL, 0, NI_NUMERICHOST)) {
316 if (verbose)
317 printf("getnameinfo failed\n");
318 return; /* this address can not be handled */
319 }
320 }
321 if (dest != NULL) {
322 if (getnameinfo(dest, dest->sa_len, daddr, sizeof daddr, NULL, 0, NI_NUMERICHOST)) {
323 if (verbose)
324 printf("getnameinfo failed\n");
325 return; /* this address can not be handled */
326 }
327 }
328
329 script = *scripts[ev];
330 if (script == NULL) return;
331
332 if (verbose)
333 (void) printf("calling: %s %s %s %s %s %s\n",
334 script, ifname, DummyTTY, DummySpeed, addr, daddr);
335
336 switch (vfork()) {
337 case -1:
338 fprintf(stderr, "cannot fork\n");
339 break;
340 case 0:
341 (void) execl(script, script, ifname, DummyTTY, DummySpeed,
342 addr, daddr, NULL);
343 _exit(EXIT_FAILURE);
344 default:
345 (void) wait(&status);
346 }
347 }
348
349 static void list_interfaces(const char *ifnames)
350 {
351 char * names = strdup(ifnames);
352 char * name, *lasts;
353 static const char sep[] = " \t";
354 struct interface_data * p;
355
356 for (name = strtok_r(names, sep, &lasts); name != NULL; name = strtok_r(NULL, sep, &lasts)) {
357 p = malloc(sizeof(*p));
358 SLIST_INSERT_HEAD(&ifs, p, next);
359 p->ifname = strdup(name);
360 p->index = if_nametoindex(p->ifname);
361 if (verbose)
362 printf("interface \"%s\" has index %d\n", p->ifname, p->index);
363 }
364 free(names);
365 }
366
367 static void
368 check_announce(struct if_announcemsghdr *ifan)
369 {
370 struct interface_data * p;
371 const char *ifname = ifan->ifan_name;
372
373 SLIST_FOREACH(p, &ifs, next) {
374 if (strcmp(p->ifname, ifname) == 0) {
375 switch (ifan->ifan_what) {
376 case IFAN_ARRIVAL:
377 invoke_script(NULL, NULL, ARRIVAL, p->index, NULL);
378 break;
379 case IFAN_DEPARTURE:
380 invoke_script(NULL, NULL, DEPARTURE, p->index, p->ifname);
381 break;
382 default:
383 if (verbose)
384 (void) printf("unknown announce: what=%d\n", ifan->ifan_what);
385 break;
386 }
387 return;
388 }
389 }
390 }
391
392 static void rescan_interfaces()
393 {
394 struct interface_data * p;
395
396 SLIST_FOREACH(p, &ifs, next) {
397 p->index = if_nametoindex(p->ifname);
398 if (verbose)
399 printf("interface \"%s\" has index %d\n", p->ifname, p->index);
400 }
401 }
402
403 static void free_interfaces()
404 {
405 struct interface_data * p;
406
407 while (!SLIST_EMPTY(&ifs)) {
408 p = SLIST_FIRST(&ifs);
409 SLIST_REMOVE_HEAD(&ifs, next);
410 free(p->ifname);
411 free(p);
412 }
413 }
414
415 static int find_interface(index)
416 int index;
417 {
418 struct interface_data * p;
419
420 SLIST_FOREACH(p, &ifs, next)
421 if (p->index == index)
422 return 1;
423 return 0;
424 }
425
426 static void run_initial_ups()
427 {
428 struct interface_data * ifd;
429 struct ifaddrs *res = NULL, *p;
430
431 if (getifaddrs(&res) == 0) {
432 for (p = res; p; p = p->ifa_next) {
433 if ((p->ifa_flags & IFF_UP) == 0)
434 continue;
435 if (p->ifa_addr == NULL)
436 continue;
437 if (p->ifa_addr->sa_family == AF_LINK)
438 continue;
439 SLIST_FOREACH(ifd, &ifs, next) {
440 if (strcmp(ifd->ifname, p->ifa_name) == 0) {
441 if (if_is_connected(ifd->ifname))
442 invoke_script(p->ifa_addr, p->ifa_dstaddr, UP, ifd->index, ifd->ifname);
443 break;
444 }
445 }
446 }
447 freeifaddrs(res);
448 }
449 }
450
451 #ifdef SPPP_IF_SUPPORT
452 /*
453 * Special case support for in-kernel PPP interfaces.
454 * If these are IFF_UP, but have not yet connected or completed authentication
455 * we don't want to call the up script in the initial interface scan (there
456 * will be an UP event generated later, when IPCP completes, anyway).
457 *
458 * If this is no if_spppsubr.c based interface, this ioctl just fails and we
459 * treat is as connected.
460 */
461 static int
462 if_is_connected(const char * ifname)
463 {
464 int s, err;
465 struct spppstatus status;
466
467 memset(&status, 0, sizeof status);
468 strncpy(status.ifname, ifname, sizeof status.ifname);
469 s = socket(AF_INET, SOCK_DGRAM, 0);
470 if (s < 0)
471 return 1; /* no idea how to handle this... */
472 err = ioctl(s, SPPPGETSTATUS, &status);
473 if (err != 0)
474 /* not if_spppsubr.c based - call it connected */
475 status.phase = SPPP_PHASE_NETWORK;
476 close(s);
477 return status.phase == SPPP_PHASE_NETWORK;
478 }
479 #endif
480