ifwatchd.c revision 1.30 1 1.30 roy /* $NetBSD: ifwatchd.c,v 1.30 2016/09/21 16:47:35 roy Exp $ */
2 1.2 martin
3 1.6 martin /*-
4 1.14 martin * Copyright (c) 2002, 2003 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.17 grant * 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.1 martin *
19 1.6 martin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.6 martin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.6 martin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.6 martin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.6 martin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.6 martin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.6 martin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.6 martin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.6 martin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.6 martin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.6 martin * POSSIBILITY OF SUCH DAMAGE.
30 1.1 martin */
31 1.1 martin
32 1.5 martin /*
33 1.5 martin * Define this for special treatment of sys/net/if_spppsubr.c based interfaces.
34 1.5 martin */
35 1.5 martin #define SPPP_IF_SUPPORT
36 1.21 dyoung
37 1.1 martin #include <sys/types.h>
38 1.1 martin #include <sys/param.h>
39 1.3 martin #include <sys/ioctl.h>
40 1.1 martin #include <sys/socket.h>
41 1.2 martin #include <sys/queue.h>
42 1.9 tron #include <sys/wait.h>
43 1.1 martin #include <net/if.h>
44 1.1 martin #include <net/if_dl.h>
45 1.20 martin #include <net/if_media.h>
46 1.5 martin #ifdef SPPP_IF_SUPPORT
47 1.5 martin #include <net/if_sppp.h>
48 1.5 martin #endif
49 1.1 martin #include <net/route.h>
50 1.2 martin #include <netinet/in.h>
51 1.2 martin #include <arpa/inet.h>
52 1.1 martin
53 1.7 tron #include <paths.h>
54 1.1 martin #include <stdio.h>
55 1.1 martin #include <stdlib.h>
56 1.1 martin #include <string.h>
57 1.1 martin #include <unistd.h>
58 1.1 martin #include <netdb.h>
59 1.3 martin #include <err.h>
60 1.3 martin #include <ifaddrs.h>
61 1.14 martin #include <syslog.h>
62 1.1 martin
63 1.19 martin enum event { ARRIVAL, DEPARTURE, UP, DOWN, CARRIER, NO_CARRIER };
64 1.19 martin
65 1.1 martin /* local functions */
66 1.26 joerg __dead static void usage(void);
67 1.30 roy static void dispatch(const void *, size_t);
68 1.30 roy static void check_addrs(const char *cp, int addrs, enum event ev);
69 1.30 roy static void invoke_script(const struct sockaddr *sa, const struct sockaddr *dst,
70 1.30 roy enum event ev, int ifindex, const char *ifname_hint);
71 1.1 martin static void list_interfaces(const char *ifnames);
72 1.30 roy static void check_announce(const struct if_announcemsghdr *ifan);
73 1.19 martin static void check_carrier(int if_index, int carrier);
74 1.1 martin static void rescan_interfaces(void);
75 1.1 martin static void free_interfaces(void);
76 1.1 martin static int find_interface(int index);
77 1.3 martin static void run_initial_ups(void);
78 1.1 martin
79 1.5 martin #ifdef SPPP_IF_SUPPORT
80 1.18 martin static int check_is_connected(const char * ifname, int def_retvalue);
81 1.18 martin #define if_is_connected(X) (check_is_connected((X), 1))
82 1.18 martin #define if_is_not_connected(X) (!check_is_connected((X), 0))
83 1.5 martin #else
84 1.5 martin #define if_is_connected(X) 1
85 1.18 martin #define if_is_not_connected(X) 1
86 1.5 martin #endif
87 1.5 martin
88 1.1 martin /* global variables */
89 1.14 martin static int verbose = 0, quiet = 0;
90 1.3 martin static int inhibit_initial = 0;
91 1.10 martin static const char *arrival_script = NULL;
92 1.10 martin static const char *departure_script = NULL;
93 1.1 martin static const char *up_script = NULL;
94 1.1 martin static const char *down_script = NULL;
95 1.19 martin static const char *carrier_script = NULL;
96 1.19 martin static const char *no_carrier_script = NULL;
97 1.19 martin static const char DummyTTY[] = _PATH_DEVNULL;
98 1.19 martin static const char DummySpeed[] = "9600";
99 1.10 martin static const char **scripts[] = {
100 1.10 martin &arrival_script,
101 1.10 martin &departure_script,
102 1.10 martin &up_script,
103 1.19 martin &down_script,
104 1.19 martin &carrier_script,
105 1.19 martin &no_carrier_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.19 martin int last_carrier_status;
112 1.1 martin char * ifname;
113 1.1 martin };
114 1.26 joerg static SLIST_HEAD(,interface_data) ifs = SLIST_HEAD_INITIALIZER(ifs);
115 1.1 martin
116 1.1 martin int
117 1.1 martin main(int argc, char **argv)
118 1.1 martin {
119 1.1 martin int c, s, n;
120 1.1 martin int errs = 0;
121 1.28 roy struct msghdr msg;
122 1.28 roy struct iovec iov[1];
123 1.28 roy char buf[2048];
124 1.1 martin
125 1.14 martin openlog(argv[0], LOG_PID|LOG_CONS, LOG_DAEMON);
126 1.21 dyoung while ((c = getopt(argc, argv, "qvhic:n:u:d:A:D:")) != -1) {
127 1.1 martin switch (c) {
128 1.1 martin case 'h':
129 1.1 martin usage();
130 1.1 martin return 0;
131 1.19 martin
132 1.3 martin case 'i':
133 1.3 martin inhibit_initial = 1;
134 1.3 martin break;
135 1.19 martin
136 1.1 martin case 'v':
137 1.1 martin verbose++;
138 1.1 martin break;
139 1.19 martin
140 1.14 martin case 'q':
141 1.14 martin quiet = 1;
142 1.14 martin break;
143 1.1 martin
144 1.19 martin case 'c':
145 1.19 martin carrier_script = optarg;
146 1.19 martin break;
147 1.19 martin
148 1.19 martin case 'n':
149 1.19 martin no_carrier_script = optarg;
150 1.19 martin break;
151 1.19 martin
152 1.1 martin case 'u':
153 1.1 martin up_script = optarg;
154 1.1 martin break;
155 1.1 martin
156 1.1 martin case 'd':
157 1.1 martin down_script = optarg;
158 1.1 martin break;
159 1.1 martin
160 1.10 martin case 'A':
161 1.10 martin arrival_script = optarg;
162 1.10 martin break;
163 1.10 martin
164 1.10 martin case 'D':
165 1.10 martin departure_script = optarg;
166 1.10 martin break;
167 1.10 martin
168 1.1 martin default:
169 1.1 martin errs++;
170 1.1 martin break;
171 1.1 martin }
172 1.21 dyoung }
173 1.1 martin
174 1.1 martin if (errs)
175 1.1 martin usage();
176 1.1 martin
177 1.1 martin argv += optind;
178 1.1 martin argc -= optind;
179 1.1 martin
180 1.21 dyoung if (argc <= 0)
181 1.1 martin usage();
182 1.1 martin
183 1.1 martin if (verbose) {
184 1.1 martin printf("up_script: %s\ndown_script: %s\n",
185 1.1 martin up_script, down_script);
186 1.10 martin printf("arrival_script: %s\ndeparture_script: %s\n",
187 1.10 martin arrival_script, departure_script);
188 1.19 martin printf("carrier_script: %s\nno_carrier_script: %s\n",
189 1.19 martin carrier_script, no_carrier_script);
190 1.1 martin printf("verbosity = %d\n", verbose);
191 1.1 martin }
192 1.1 martin
193 1.1 martin while (argc > 0) {
194 1.13 itojun list_interfaces(argv[0]);
195 1.13 itojun argv++;
196 1.13 itojun argc--;
197 1.1 martin }
198 1.1 martin
199 1.1 martin if (!verbose)
200 1.21 dyoung daemon(0, 0);
201 1.1 martin
202 1.1 martin s = socket(PF_ROUTE, SOCK_RAW, 0);
203 1.1 martin if (s < 0) {
204 1.14 martin syslog(LOG_ERR, "error opening routing socket: %m");
205 1.1 martin perror("open routing socket");
206 1.9 tron exit(EXIT_FAILURE);
207 1.1 martin }
208 1.1 martin
209 1.11 martin if (!inhibit_initial)
210 1.11 martin run_initial_ups();
211 1.11 martin
212 1.28 roy iov[0].iov_base = buf;
213 1.28 roy iov[0].iov_len = sizeof(buf);
214 1.28 roy memset(&msg, 0, sizeof(msg));
215 1.28 roy msg.msg_iov = iov;
216 1.28 roy msg.msg_iovlen = 1;
217 1.28 roy
218 1.1 martin for (;;) {
219 1.28 roy n = recvmsg(s, &msg, 0);
220 1.28 roy if (n == -1) {
221 1.28 roy syslog(LOG_ERR, "recvmsg: %m");
222 1.28 roy exit(EXIT_FAILURE);
223 1.28 roy }
224 1.28 roy if (n != 0)
225 1.28 roy dispatch(iov[0].iov_base, n);
226 1.1 martin }
227 1.1 martin
228 1.1 martin close(s);
229 1.1 martin free_interfaces();
230 1.14 martin closelog();
231 1.1 martin
232 1.9 tron return EXIT_SUCCESS;
233 1.1 martin }
234 1.1 martin
235 1.1 martin static void
236 1.21 dyoung usage(void)
237 1.1 martin {
238 1.21 dyoung fprintf(stderr,
239 1.1 martin "usage:\n"
240 1.15 wiz "\tifwatchd [-hiqv] [-A arrival-script] [-D departure-script]\n"
241 1.19 martin "\t\t [-d down-script] [-u up-script]\n"
242 1.19 martin "\t\t [-c carrier-script] [-n no-carrier-script] ifname(s)\n"
243 1.1 martin "\twhere:\n"
244 1.12 wiz "\t -A <cmd> specify command to run on interface arrival event\n"
245 1.19 martin "\t -c <cmd> specify command to run on interface carrier-detect event\n"
246 1.12 wiz "\t -D <cmd> specify command to run on interface departure event\n"
247 1.12 wiz "\t -d <cmd> specify command to run on interface down event\n"
248 1.19 martin "\t -n <cmd> specify command to run on interface no-carrier-detect event\n"
249 1.1 martin "\t -h show this help message\n"
250 1.3 martin "\t -i no (!) initial run of the up script if the interface\n"
251 1.3 martin "\t is already up on ifwatchd startup\n"
252 1.16 abs "\t -q quiet mode, don't syslog informational messages\n"
253 1.1 martin "\t -u <cmd> specify command to run on interface up event\n"
254 1.16 abs "\t -v verbose/debug output, don't run in background\n");
255 1.9 tron exit(EXIT_FAILURE);
256 1.1 martin }
257 1.1 martin
258 1.1 martin static void
259 1.30 roy dispatch(const void *msg, size_t len)
260 1.1 martin {
261 1.30 roy const struct rt_msghdr *hd = msg;
262 1.30 roy const struct if_msghdr *ifmp;
263 1.30 roy const struct ifa_msghdr *ifam;
264 1.10 martin enum event ev;
265 1.1 martin
266 1.29 roy if (hd->rtm_version != RTM_VERSION)
267 1.29 roy return;
268 1.29 roy
269 1.1 martin switch (hd->rtm_type) {
270 1.1 martin case RTM_NEWADDR:
271 1.10 martin ev = UP;
272 1.1 martin goto work;
273 1.1 martin case RTM_DELADDR:
274 1.10 martin ev = DOWN;
275 1.1 martin goto work;
276 1.1 martin case RTM_IFANNOUNCE:
277 1.1 martin rescan_interfaces();
278 1.30 roy check_announce(msg);
279 1.10 martin return;
280 1.19 martin case RTM_IFINFO:
281 1.30 roy ifmp = (const struct if_msghdr*)msg;
282 1.19 martin check_carrier(ifmp->ifm_index, ifmp->ifm_data.ifi_link_state);
283 1.19 martin return;
284 1.23 joerg case RTM_ADD:
285 1.23 joerg case RTM_DELETE:
286 1.23 joerg case RTM_CHANGE:
287 1.23 joerg case RTM_LOSING:
288 1.23 joerg case RTM_REDIRECT:
289 1.23 joerg case RTM_MISS:
290 1.23 joerg case RTM_IEEE80211:
291 1.29 roy case RTM_ONEWADDR:
292 1.29 roy case RTM_ODELADDR:
293 1.29 roy case RTM_OCHGADDR:
294 1.23 joerg return;
295 1.1 martin }
296 1.1 martin if (verbose)
297 1.23 joerg printf("unknown message ignored (%d)\n", hd->rtm_type);
298 1.1 martin return;
299 1.1 martin
300 1.1 martin work:
301 1.30 roy ifam = (const struct ifa_msghdr *)msg;
302 1.30 roy check_addrs((const char *)(ifam + 1), ifam->ifam_addrs, ev);
303 1.1 martin }
304 1.1 martin
305 1.1 martin static void
306 1.30 roy check_addrs(const char *cp, int addrs, enum event ev)
307 1.1 martin {
308 1.30 roy const struct sockaddr *sa, *ifa = NULL, *brd = NULL;
309 1.18 martin char ifname_buf[IFNAMSIZ];
310 1.18 martin const char *ifname;
311 1.27 riastrad int ifndx = 0;
312 1.27 riastrad unsigned i;
313 1.1 martin
314 1.1 martin if (addrs == 0)
315 1.13 itojun return;
316 1.1 martin for (i = 1; i; i <<= 1) {
317 1.21 dyoung if ((i & addrs) == 0)
318 1.21 dyoung continue;
319 1.30 roy sa = (const struct sockaddr *)cp;
320 1.21 dyoung if (i == RTA_IFP) {
321 1.30 roy const struct sockaddr_dl *li;
322 1.30 roy
323 1.30 roy li = (const struct sockaddr_dl *)sa;
324 1.21 dyoung ifndx = li->sdl_index;
325 1.21 dyoung if (!find_interface(ifndx)) {
326 1.21 dyoung if (verbose)
327 1.21 dyoung printf("ignoring change on interface #%d\n", ifndx);
328 1.21 dyoung return;
329 1.13 itojun }
330 1.21 dyoung } else if (i == RTA_IFA)
331 1.21 dyoung ifa = sa;
332 1.21 dyoung else if (i == RTA_BRD)
333 1.21 dyoung brd = sa;
334 1.25 martin RT_ADVANCE(cp, sa);
335 1.1 martin }
336 1.18 martin if (ifa != NULL) {
337 1.18 martin ifname = if_indextoname(ifndx, ifname_buf);
338 1.18 martin if (ifname == NULL || ev < UP)
339 1.18 martin invoke_script(ifa, brd, ev, ifndx, ifname);
340 1.18 martin else if (ev == UP) {
341 1.18 martin if (if_is_connected(ifname))
342 1.18 martin invoke_script(ifa, brd, ev, ifndx, ifname);
343 1.18 martin } else if (ev == DOWN) {
344 1.18 martin if (if_is_not_connected(ifname))
345 1.18 martin invoke_script(ifa, brd, ev, ifndx, ifname);
346 1.18 martin }
347 1.18 martin }
348 1.1 martin }
349 1.1 martin
350 1.1 martin static void
351 1.30 roy invoke_script(const struct sockaddr *sa, const struct sockaddr *dest,
352 1.30 roy enum event ev, int ifindex, const char *ifname_hint)
353 1.1 martin {
354 1.10 martin char addr[NI_MAXHOST], daddr[NI_MAXHOST], ifname_buf[IFNAMSIZ];
355 1.24 lukem const char * volatile ifname;
356 1.1 martin const char *script;
357 1.9 tron int status;
358 1.8 tron
359 1.10 martin if (sa != NULL && sa->sa_len == 0) {
360 1.13 itojun fprintf(stderr, "illegal socket address (sa_len == 0)\n");
361 1.13 itojun return;
362 1.10 martin }
363 1.10 martin if (sa != NULL && sa->sa_family == AF_INET6) {
364 1.8 tron struct sockaddr_in6 sin6;
365 1.8 tron
366 1.30 roy memcpy(&sin6, (const struct sockaddr_in6 *)sa, sizeof (sin6));
367 1.8 tron if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr))
368 1.8 tron return;
369 1.8 tron }
370 1.1 martin
371 1.10 martin addr[0] = daddr[0] = 0;
372 1.1 martin ifname = if_indextoname(ifindex, ifname_buf);
373 1.10 martin ifname = ifname ? ifname : ifname_hint;
374 1.10 martin if (ifname == NULL)
375 1.13 itojun return;
376 1.1 martin
377 1.10 martin if (sa != NULL) {
378 1.13 itojun if (getnameinfo(sa, sa->sa_len, addr, sizeof addr, NULL, 0,
379 1.13 itojun NI_NUMERICHOST)) {
380 1.13 itojun if (verbose)
381 1.13 itojun printf("getnameinfo failed\n");
382 1.13 itojun return; /* this address can not be handled */
383 1.13 itojun }
384 1.10 martin }
385 1.4 martin if (dest != NULL) {
386 1.13 itojun if (getnameinfo(dest, dest->sa_len, daddr, sizeof daddr,
387 1.13 itojun NULL, 0, NI_NUMERICHOST)) {
388 1.13 itojun if (verbose)
389 1.13 itojun printf("getnameinfo failed\n");
390 1.13 itojun return; /* this address can not be handled */
391 1.13 itojun }
392 1.4 martin }
393 1.1 martin
394 1.10 martin script = *scripts[ev];
395 1.1 martin if (script == NULL) return;
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.13 itojun fprintf(stderr, "cannot fork\n");
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 perror(script);
414 1.14 martin }
415 1.13 itojun _exit(EXIT_FAILURE);
416 1.9 tron default:
417 1.13 itojun (void) wait(&status);
418 1.1 martin }
419 1.1 martin }
420 1.1 martin
421 1.19 martin static void
422 1.19 martin list_interfaces(const char *ifnames)
423 1.1 martin {
424 1.1 martin char * names = strdup(ifnames);
425 1.1 martin char * name, *lasts;
426 1.1 martin static const char sep[] = " \t";
427 1.1 martin struct interface_data * p;
428 1.1 martin
429 1.13 itojun for (name = strtok_r(names, sep, &lasts);
430 1.13 itojun name != NULL;
431 1.13 itojun name = strtok_r(NULL, sep, &lasts)) {
432 1.13 itojun p = malloc(sizeof(*p));
433 1.13 itojun SLIST_INSERT_HEAD(&ifs, p, next);
434 1.19 martin p->last_carrier_status = -1;
435 1.13 itojun p->ifname = strdup(name);
436 1.13 itojun p->index = if_nametoindex(p->ifname);
437 1.14 martin if (!quiet)
438 1.14 martin syslog(LOG_INFO, "watching interface %s", p->ifname);
439 1.13 itojun if (verbose)
440 1.13 itojun printf("interface \"%s\" has index %d\n",
441 1.13 itojun p->ifname, p->index);
442 1.1 martin }
443 1.1 martin free(names);
444 1.1 martin }
445 1.1 martin
446 1.10 martin static void
447 1.19 martin check_carrier(int if_index, int carrier_status)
448 1.19 martin {
449 1.19 martin struct interface_data * p;
450 1.19 martin enum event ev;
451 1.19 martin
452 1.19 martin SLIST_FOREACH(p, &ifs, next)
453 1.19 martin if (p->index == if_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.19 martin
466 1.19 martin if ((carrier_status != p->last_carrier_status) ||
467 1.19 martin ((p->last_carrier_status == -1) && !inhibit_initial)) {
468 1.19 martin switch (carrier_status) {
469 1.19 martin case LINK_STATE_UP:
470 1.19 martin ev = CARRIER;
471 1.19 martin break;
472 1.19 martin case LINK_STATE_DOWN:
473 1.19 martin ev = NO_CARRIER;
474 1.19 martin break;
475 1.19 martin default:
476 1.19 martin if (verbose)
477 1.19 martin printf("unknown link status ignored\n");
478 1.19 martin return;
479 1.19 martin }
480 1.19 martin invoke_script(NULL, NULL, ev, if_index, p->ifname);
481 1.19 martin p->last_carrier_status = carrier_status;
482 1.19 martin }
483 1.19 martin }
484 1.19 martin
485 1.19 martin static void
486 1.30 roy check_announce(const struct if_announcemsghdr *ifan)
487 1.10 martin {
488 1.10 martin struct interface_data * p;
489 1.10 martin const char *ifname = ifan->ifan_name;
490 1.10 martin
491 1.10 martin SLIST_FOREACH(p, &ifs, next) {
492 1.21 dyoung if (strcmp(p->ifname, ifname) != 0)
493 1.21 dyoung continue;
494 1.21 dyoung
495 1.21 dyoung switch (ifan->ifan_what) {
496 1.21 dyoung case IFAN_ARRIVAL:
497 1.21 dyoung invoke_script(NULL, NULL, ARRIVAL, p->index,
498 1.21 dyoung NULL);
499 1.21 dyoung break;
500 1.21 dyoung case IFAN_DEPARTURE:
501 1.21 dyoung invoke_script(NULL, NULL, DEPARTURE, p->index,
502 1.21 dyoung p->ifname);
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 rescan_interfaces(void)
516 1.1 martin {
517 1.1 martin struct interface_data * p;
518 1.21 dyoung
519 1.1 martin SLIST_FOREACH(p, &ifs, next) {
520 1.13 itojun p->index = if_nametoindex(p->ifname);
521 1.13 itojun if (verbose)
522 1.13 itojun printf("interface \"%s\" has index %d\n", p->ifname,
523 1.13 itojun p->index);
524 1.1 martin }
525 1.1 martin }
526 1.1 martin
527 1.21 dyoung static void
528 1.21 dyoung free_interfaces(void)
529 1.1 martin {
530 1.1 martin struct interface_data * p;
531 1.1 martin
532 1.1 martin while (!SLIST_EMPTY(&ifs)) {
533 1.13 itojun p = SLIST_FIRST(&ifs);
534 1.13 itojun SLIST_REMOVE_HEAD(&ifs, next);
535 1.13 itojun free(p->ifname);
536 1.13 itojun free(p);
537 1.1 martin }
538 1.1 martin }
539 1.1 martin
540 1.21 dyoung static int
541 1.24 lukem find_interface(int idx)
542 1.1 martin {
543 1.1 martin struct interface_data * p;
544 1.21 dyoung
545 1.1 martin SLIST_FOREACH(p, &ifs, next)
546 1.24 lukem if (p->index == idx)
547 1.13 itojun return 1;
548 1.1 martin return 0;
549 1.3 martin }
550 1.3 martin
551 1.21 dyoung static void
552 1.21 dyoung run_initial_ups(void)
553 1.3 martin {
554 1.3 martin struct interface_data * ifd;
555 1.3 martin struct ifaddrs *res = NULL, *p;
556 1.20 martin int s;
557 1.20 martin
558 1.20 martin s = socket(AF_INET, SOCK_DGRAM, 0);
559 1.20 martin if (s < 0)
560 1.20 martin return;
561 1.3 martin
562 1.21 dyoung if (getifaddrs(&res) != 0)
563 1.21 dyoung goto out;
564 1.21 dyoung
565 1.21 dyoung for (p = res; p; p = p->ifa_next) {
566 1.21 dyoung SLIST_FOREACH(ifd, &ifs, next) {
567 1.21 dyoung if (strcmp(ifd->ifname, p->ifa_name) == 0)
568 1.21 dyoung break;
569 1.21 dyoung }
570 1.21 dyoung if (ifd == NULL)
571 1.21 dyoung continue;
572 1.13 itojun
573 1.21 dyoung if (p->ifa_addr && p->ifa_addr->sa_family == AF_LINK)
574 1.21 dyoung invoke_script(NULL, NULL, ARRIVAL, ifd->index,
575 1.21 dyoung NULL);
576 1.21 dyoung
577 1.21 dyoung if ((p->ifa_flags & IFF_UP) == 0)
578 1.21 dyoung continue;
579 1.21 dyoung if (p->ifa_addr == NULL)
580 1.21 dyoung continue;
581 1.21 dyoung if (p->ifa_addr->sa_family == AF_LINK) {
582 1.21 dyoung struct ifmediareq ifmr;
583 1.21 dyoung
584 1.21 dyoung memset(&ifmr, 0, sizeof(ifmr));
585 1.21 dyoung strncpy(ifmr.ifm_name, ifd->ifname,
586 1.21 dyoung sizeof(ifmr.ifm_name));
587 1.21 dyoung if (ioctl(s, SIOCGIFMEDIA, &ifmr) != -1
588 1.21 dyoung && (ifmr.ifm_status & IFM_AVALID)
589 1.21 dyoung && (ifmr.ifm_status & IFM_ACTIVE)) {
590 1.21 dyoung invoke_script(NULL, NULL, CARRIER,
591 1.13 itojun ifd->index, ifd->ifname);
592 1.21 dyoung ifd->last_carrier_status =
593 1.21 dyoung LINK_STATE_UP;
594 1.21 dyoung }
595 1.21 dyoung continue;
596 1.21 dyoung }
597 1.21 dyoung if (if_is_connected(ifd->ifname))
598 1.21 dyoung invoke_script(p->ifa_addr, p->ifa_dstaddr, UP,
599 1.21 dyoung ifd->index, ifd->ifname);
600 1.3 martin }
601 1.21 dyoung freeifaddrs(res);
602 1.21 dyoung out:
603 1.20 martin close(s);
604 1.1 martin }
605 1.5 martin
606 1.5 martin #ifdef SPPP_IF_SUPPORT
607 1.5 martin /*
608 1.5 martin * Special case support for in-kernel PPP interfaces.
609 1.5 martin * If these are IFF_UP, but have not yet connected or completed authentication
610 1.5 martin * we don't want to call the up script in the initial interface scan (there
611 1.5 martin * will be an UP event generated later, when IPCP completes, anyway).
612 1.5 martin *
613 1.5 martin * If this is no if_spppsubr.c based interface, this ioctl just fails and we
614 1.5 martin * treat is as connected.
615 1.5 martin */
616 1.5 martin static int
617 1.21 dyoung check_is_connected(const char *ifname, int def_retval)
618 1.5 martin {
619 1.24 lukem int s, error;
620 1.18 martin struct spppstatus oldstatus;
621 1.18 martin struct spppstatusncp status;
622 1.5 martin
623 1.5 martin memset(&status, 0, sizeof status);
624 1.5 martin strncpy(status.ifname, ifname, sizeof status.ifname);
625 1.18 martin memset(&oldstatus, 0, sizeof oldstatus);
626 1.18 martin strncpy(oldstatus.ifname, ifname, sizeof oldstatus.ifname);
627 1.18 martin
628 1.5 martin s = socket(AF_INET, SOCK_DGRAM, 0);
629 1.5 martin if (s < 0)
630 1.13 itojun return 1; /* no idea how to handle this... */
631 1.24 lukem error = ioctl(s, SPPPGETSTATUSNCP, &status);
632 1.24 lukem if (error != 0) {
633 1.24 lukem error = ioctl(s, SPPPGETSTATUS, &oldstatus);
634 1.24 lukem if (error != 0) {
635 1.18 martin /* not if_spppsubr.c based - return default */
636 1.18 martin close(s);
637 1.18 martin return def_retval;
638 1.18 martin } else {
639 1.18 martin /* can't query NCPs, so use default */
640 1.18 martin status.phase = oldstatus.phase;
641 1.18 martin status.ncpup = def_retval;
642 1.18 martin }
643 1.18 martin }
644 1.5 martin close(s);
645 1.18 martin
646 1.18 martin return status.phase == SPPP_PHASE_NETWORK && status.ncpup > 0;
647 1.5 martin }
648 1.5 martin #endif
649