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