ifwatchd.c revision 1.19 1 1.19 martin /* $NetBSD: ifwatchd.c,v 1.19 2004/01/04 22:19:51 martin 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.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.14 martin #include <syslog.h>
68 1.1 martin
69 1.19 martin enum event { ARRIVAL, DEPARTURE, UP, DOWN, CARRIER, NO_CARRIER };
70 1.19 martin
71 1.1 martin /* local functions */
72 1.1 martin static void usage(void);
73 1.1 martin static void dispatch(void*, size_t);
74 1.10 martin static void check_addrs(char *cp, int addrs, enum event ev);
75 1.10 martin static void invoke_script(struct sockaddr *sa, struct sockaddr *dst, enum event ev, int ifindex, const char *ifname_hint);
76 1.1 martin static void list_interfaces(const char *ifnames);
77 1.10 martin static void check_announce(struct if_announcemsghdr *ifan);
78 1.19 martin static void check_carrier(int if_index, int carrier);
79 1.1 martin static void rescan_interfaces(void);
80 1.1 martin static void free_interfaces(void);
81 1.1 martin static int find_interface(int index);
82 1.3 martin static void run_initial_ups(void);
83 1.1 martin
84 1.5 martin #ifdef SPPP_IF_SUPPORT
85 1.18 martin static int check_is_connected(const char * ifname, int def_retvalue);
86 1.18 martin #define if_is_connected(X) (check_is_connected((X), 1))
87 1.18 martin #define if_is_not_connected(X) (!check_is_connected((X), 0))
88 1.5 martin #else
89 1.5 martin #define if_is_connected(X) 1
90 1.18 martin #define if_is_not_connected(X) 1
91 1.5 martin #endif
92 1.5 martin
93 1.1 martin /* stolen from /sbin/route */
94 1.1 martin #define ROUNDUP(a) \
95 1.1 martin ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
96 1.1 martin #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
97 1.1 martin
98 1.1 martin /* global variables */
99 1.14 martin static int verbose = 0, quiet = 0;
100 1.3 martin static int inhibit_initial = 0;
101 1.10 martin static const char *arrival_script = NULL;
102 1.10 martin static const char *departure_script = NULL;
103 1.1 martin static const char *up_script = NULL;
104 1.1 martin static const char *down_script = NULL;
105 1.19 martin static const char *carrier_script = NULL;
106 1.19 martin static const char *no_carrier_script = NULL;
107 1.19 martin static const char DummyTTY[] = _PATH_DEVNULL;
108 1.19 martin static const char DummySpeed[] = "9600";
109 1.10 martin static const char **scripts[] = {
110 1.10 martin &arrival_script,
111 1.10 martin &departure_script,
112 1.10 martin &up_script,
113 1.19 martin &down_script,
114 1.19 martin &carrier_script,
115 1.19 martin &no_carrier_script
116 1.13 itojun };
117 1.1 martin
118 1.1 martin struct interface_data {
119 1.1 martin SLIST_ENTRY(interface_data) next;
120 1.1 martin int index;
121 1.19 martin int last_carrier_status;
122 1.1 martin char * ifname;
123 1.1 martin };
124 1.1 martin SLIST_HEAD(,interface_data) ifs = SLIST_HEAD_INITIALIZER(ifs);
125 1.1 martin
126 1.1 martin int
127 1.1 martin main(int argc, char **argv)
128 1.1 martin {
129 1.1 martin int c, s, n;
130 1.1 martin int errs = 0;
131 1.1 martin char msg[2048], *msgp;
132 1.1 martin
133 1.14 martin openlog(argv[0], LOG_PID|LOG_CONS, LOG_DAEMON);
134 1.19 martin while ((c = getopt(argc, argv, "qvhic:n:u:d:A:D:")) != -1)
135 1.1 martin switch (c) {
136 1.1 martin case 'h':
137 1.1 martin usage();
138 1.1 martin return 0;
139 1.19 martin
140 1.3 martin case 'i':
141 1.3 martin inhibit_initial = 1;
142 1.3 martin break;
143 1.19 martin
144 1.1 martin case 'v':
145 1.1 martin verbose++;
146 1.1 martin break;
147 1.19 martin
148 1.14 martin case 'q':
149 1.14 martin quiet = 1;
150 1.14 martin break;
151 1.1 martin
152 1.19 martin case 'c':
153 1.19 martin carrier_script = optarg;
154 1.19 martin break;
155 1.19 martin
156 1.19 martin case 'n':
157 1.19 martin no_carrier_script = optarg;
158 1.19 martin break;
159 1.19 martin
160 1.1 martin case 'u':
161 1.1 martin up_script = optarg;
162 1.1 martin break;
163 1.1 martin
164 1.1 martin case 'd':
165 1.1 martin down_script = optarg;
166 1.1 martin break;
167 1.1 martin
168 1.10 martin case 'A':
169 1.10 martin arrival_script = optarg;
170 1.10 martin break;
171 1.10 martin
172 1.10 martin case 'D':
173 1.10 martin departure_script = optarg;
174 1.10 martin break;
175 1.10 martin
176 1.1 martin default:
177 1.1 martin errs++;
178 1.1 martin break;
179 1.1 martin }
180 1.1 martin
181 1.1 martin if (errs)
182 1.1 martin usage();
183 1.1 martin
184 1.1 martin argv += optind;
185 1.1 martin argc -= optind;
186 1.1 martin
187 1.1 martin if (argc <= 0)
188 1.1 martin usage();
189 1.1 martin
190 1.1 martin if (verbose) {
191 1.1 martin printf("up_script: %s\ndown_script: %s\n",
192 1.1 martin up_script, down_script);
193 1.10 martin printf("arrival_script: %s\ndeparture_script: %s\n",
194 1.10 martin arrival_script, departure_script);
195 1.19 martin printf("carrier_script: %s\nno_carrier_script: %s\n",
196 1.19 martin carrier_script, no_carrier_script);
197 1.1 martin printf("verbosity = %d\n", verbose);
198 1.1 martin }
199 1.1 martin
200 1.1 martin while (argc > 0) {
201 1.13 itojun list_interfaces(argv[0]);
202 1.13 itojun argv++;
203 1.13 itojun argc--;
204 1.1 martin }
205 1.1 martin
206 1.1 martin if (!verbose)
207 1.1 martin daemon(0,0);
208 1.1 martin
209 1.1 martin s = socket(PF_ROUTE, SOCK_RAW, 0);
210 1.1 martin if (s < 0) {
211 1.14 martin syslog(LOG_ERR, "error opening routing socket: %m");
212 1.1 martin perror("open routing socket");
213 1.9 tron exit(EXIT_FAILURE);
214 1.1 martin }
215 1.1 martin
216 1.11 martin if (!inhibit_initial)
217 1.11 martin run_initial_ups();
218 1.11 martin
219 1.1 martin for (;;) {
220 1.1 martin n = read(s, msg, sizeof msg);
221 1.1 martin msgp = msg;
222 1.1 martin for (msgp = msg; n > 0; n -= ((struct rt_msghdr*)msgp)->rtm_msglen, msgp += ((struct rt_msghdr*)msgp)->rtm_msglen) {
223 1.1 martin dispatch(msgp, n);
224 1.1 martin
225 1.1 martin }
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.1 martin usage()
237 1.1 martin {
238 1.1 martin 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.1 martin dispatch(void *msg, size_t len)
260 1.1 martin {
261 1.1 martin struct rt_msghdr *hd = msg;
262 1.19 martin struct if_msghdr *ifmp;
263 1.1 martin struct ifa_msghdr *ifam;
264 1.10 martin enum event ev;
265 1.1 martin
266 1.1 martin switch (hd->rtm_type) {
267 1.1 martin case RTM_NEWADDR:
268 1.10 martin ev = UP;
269 1.1 martin goto work;
270 1.1 martin case RTM_DELADDR:
271 1.10 martin ev = DOWN;
272 1.1 martin goto work;
273 1.1 martin case RTM_IFANNOUNCE:
274 1.1 martin rescan_interfaces();
275 1.10 martin check_announce((struct if_announcemsghdr *)msg);
276 1.10 martin return;
277 1.19 martin case RTM_IFINFO:
278 1.19 martin ifmp = (struct if_msghdr*)msg;
279 1.19 martin check_carrier(ifmp->ifm_index, ifmp->ifm_data.ifi_link_state);
280 1.19 martin return;
281 1.1 martin }
282 1.1 martin if (verbose)
283 1.1 martin printf("unknown message ignored\n");
284 1.1 martin return;
285 1.1 martin
286 1.1 martin work:
287 1.1 martin ifam = (struct ifa_msghdr *)msg;
288 1.10 martin check_addrs((char *)(ifam + 1), ifam->ifam_addrs, ev);
289 1.1 martin }
290 1.1 martin
291 1.1 martin static void
292 1.10 martin check_addrs(cp, addrs, ev)
293 1.1 martin char *cp;
294 1.10 martin int addrs;
295 1.10 martin enum event ev;
296 1.1 martin {
297 1.4 martin struct sockaddr *sa, *ifa = NULL, *brd = NULL;
298 1.18 martin char ifname_buf[IFNAMSIZ];
299 1.18 martin const char *ifname;
300 1.1 martin int ifndx = 0, i;
301 1.1 martin
302 1.1 martin if (addrs == 0)
303 1.13 itojun return;
304 1.1 martin for (i = 1; i; i <<= 1) {
305 1.13 itojun if (i & addrs) {
306 1.13 itojun sa = (struct sockaddr *)cp;
307 1.13 itojun if (i == RTA_IFP) {
308 1.13 itojun struct sockaddr_dl * li = (struct sockaddr_dl*)sa;
309 1.13 itojun ifndx = li->sdl_index;
310 1.13 itojun if (!find_interface(ifndx)) {
311 1.13 itojun if (verbose)
312 1.13 itojun printf("ignoring change on interface #%d\n", ifndx);
313 1.13 itojun return;
314 1.13 itojun }
315 1.13 itojun } else if (i == RTA_IFA) {
316 1.13 itojun ifa = sa;
317 1.13 itojun } else if (i == RTA_BRD) {
318 1.13 itojun brd = sa;
319 1.13 itojun }
320 1.13 itojun ADVANCE(cp, sa);
321 1.1 martin }
322 1.1 martin }
323 1.18 martin if (ifa != NULL) {
324 1.18 martin ifname = if_indextoname(ifndx, ifname_buf);
325 1.18 martin if (ifname == NULL || ev < UP)
326 1.18 martin invoke_script(ifa, brd, ev, ifndx, ifname);
327 1.18 martin else if (ev == UP) {
328 1.18 martin if (if_is_connected(ifname))
329 1.18 martin invoke_script(ifa, brd, ev, ifndx, ifname);
330 1.18 martin } else if (ev == DOWN) {
331 1.18 martin if (if_is_not_connected(ifname))
332 1.18 martin invoke_script(ifa, brd, ev, ifndx, ifname);
333 1.18 martin }
334 1.18 martin }
335 1.1 martin }
336 1.1 martin
337 1.1 martin static void
338 1.10 martin invoke_script(sa, dest, ev, ifindex, ifname_hint)
339 1.4 martin struct sockaddr *sa, *dest;
340 1.10 martin enum event ev;
341 1.10 martin int ifindex;
342 1.10 martin const char *ifname_hint;
343 1.1 martin {
344 1.10 martin char addr[NI_MAXHOST], daddr[NI_MAXHOST], ifname_buf[IFNAMSIZ];
345 1.10 martin const char *ifname;
346 1.1 martin const char *script;
347 1.9 tron int status;
348 1.8 tron
349 1.10 martin if (sa != NULL && sa->sa_len == 0) {
350 1.13 itojun fprintf(stderr, "illegal socket address (sa_len == 0)\n");
351 1.13 itojun return;
352 1.10 martin }
353 1.10 martin if (sa != NULL && sa->sa_family == AF_INET6) {
354 1.8 tron struct sockaddr_in6 sin6;
355 1.8 tron
356 1.8 tron (void) memcpy(&sin6, (struct sockaddr_in6 *)sa, sizeof (sin6));
357 1.8 tron if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr))
358 1.8 tron return;
359 1.8 tron }
360 1.1 martin
361 1.10 martin addr[0] = daddr[0] = 0;
362 1.1 martin ifname = if_indextoname(ifindex, ifname_buf);
363 1.10 martin ifname = ifname ? ifname : ifname_hint;
364 1.10 martin if (ifname == NULL)
365 1.13 itojun return;
366 1.1 martin
367 1.10 martin if (sa != NULL) {
368 1.13 itojun if (getnameinfo(sa, sa->sa_len, addr, sizeof addr, NULL, 0,
369 1.13 itojun NI_NUMERICHOST)) {
370 1.13 itojun if (verbose)
371 1.13 itojun printf("getnameinfo failed\n");
372 1.13 itojun return; /* this address can not be handled */
373 1.13 itojun }
374 1.10 martin }
375 1.4 martin if (dest != NULL) {
376 1.13 itojun if (getnameinfo(dest, dest->sa_len, daddr, sizeof daddr,
377 1.13 itojun NULL, 0, NI_NUMERICHOST)) {
378 1.13 itojun if (verbose)
379 1.13 itojun printf("getnameinfo failed\n");
380 1.13 itojun return; /* this address can not be handled */
381 1.13 itojun }
382 1.4 martin }
383 1.1 martin
384 1.10 martin script = *scripts[ev];
385 1.1 martin if (script == NULL) return;
386 1.1 martin
387 1.9 tron if (verbose)
388 1.13 itojun (void) printf("calling: %s %s %s %s %s %s\n",
389 1.13 itojun script, ifname, DummyTTY, DummySpeed, addr, daddr);
390 1.14 martin if (!quiet)
391 1.14 martin syslog(LOG_INFO, "calling: %s %s %s %s %s %s\n",
392 1.14 martin script, ifname, DummyTTY, DummySpeed, addr, daddr);
393 1.9 tron
394 1.9 tron switch (vfork()) {
395 1.9 tron case -1:
396 1.13 itojun fprintf(stderr, "cannot fork\n");
397 1.13 itojun break;
398 1.9 tron case 0:
399 1.14 martin if (execl(script, script, ifname, DummyTTY, DummySpeed,
400 1.14 martin addr, daddr, NULL) == -1) {
401 1.14 martin syslog(LOG_ERR, "could not execute \"%s\": %m",
402 1.14 martin script);
403 1.14 martin perror(script);
404 1.14 martin }
405 1.13 itojun _exit(EXIT_FAILURE);
406 1.9 tron default:
407 1.13 itojun (void) wait(&status);
408 1.1 martin }
409 1.1 martin }
410 1.1 martin
411 1.19 martin static void
412 1.19 martin list_interfaces(const char *ifnames)
413 1.1 martin {
414 1.1 martin char * names = strdup(ifnames);
415 1.1 martin char * name, *lasts;
416 1.1 martin static const char sep[] = " \t";
417 1.1 martin struct interface_data * p;
418 1.1 martin
419 1.13 itojun for (name = strtok_r(names, sep, &lasts);
420 1.13 itojun name != NULL;
421 1.13 itojun name = strtok_r(NULL, sep, &lasts)) {
422 1.13 itojun p = malloc(sizeof(*p));
423 1.13 itojun SLIST_INSERT_HEAD(&ifs, p, next);
424 1.19 martin p->last_carrier_status = -1;
425 1.13 itojun p->ifname = strdup(name);
426 1.13 itojun p->index = if_nametoindex(p->ifname);
427 1.14 martin if (!quiet)
428 1.14 martin syslog(LOG_INFO, "watching interface %s", p->ifname);
429 1.13 itojun if (verbose)
430 1.13 itojun printf("interface \"%s\" has index %d\n",
431 1.13 itojun p->ifname, p->index);
432 1.1 martin }
433 1.1 martin free(names);
434 1.1 martin }
435 1.1 martin
436 1.10 martin static void
437 1.19 martin check_carrier(int if_index, int carrier_status)
438 1.19 martin {
439 1.19 martin struct interface_data * p;
440 1.19 martin enum event ev;
441 1.19 martin
442 1.19 martin SLIST_FOREACH(p, &ifs, next)
443 1.19 martin if (p->index == if_index)
444 1.19 martin break;
445 1.19 martin
446 1.19 martin if (p == NULL)
447 1.19 martin return;
448 1.19 martin
449 1.19 martin /*
450 1.19 martin * Treat it as an event worth handling if:
451 1.19 martin * - the carrier status changed, or
452 1.19 martin * - this is the first time we've been called, and
453 1.19 martin * inhibit_initial is not set
454 1.19 martin */
455 1.19 martin
456 1.19 martin if ((carrier_status != p->last_carrier_status) ||
457 1.19 martin ((p->last_carrier_status == -1) && !inhibit_initial)) {
458 1.19 martin switch (carrier_status) {
459 1.19 martin case LINK_STATE_UP:
460 1.19 martin ev = CARRIER;
461 1.19 martin break;
462 1.19 martin case LINK_STATE_DOWN:
463 1.19 martin ev = NO_CARRIER;
464 1.19 martin break;
465 1.19 martin default:
466 1.19 martin if (verbose)
467 1.19 martin printf("unknown link status ignored\n");
468 1.19 martin return;
469 1.19 martin }
470 1.19 martin invoke_script(NULL, NULL, ev, if_index, p->ifname);
471 1.19 martin p->last_carrier_status = carrier_status;
472 1.19 martin }
473 1.19 martin }
474 1.19 martin
475 1.19 martin static void
476 1.10 martin check_announce(struct if_announcemsghdr *ifan)
477 1.10 martin {
478 1.10 martin struct interface_data * p;
479 1.10 martin const char *ifname = ifan->ifan_name;
480 1.10 martin
481 1.10 martin SLIST_FOREACH(p, &ifs, next) {
482 1.13 itojun if (strcmp(p->ifname, ifname) == 0) {
483 1.13 itojun switch (ifan->ifan_what) {
484 1.13 itojun case IFAN_ARRIVAL:
485 1.13 itojun invoke_script(NULL, NULL, ARRIVAL, p->index,
486 1.13 itojun NULL);
487 1.13 itojun break;
488 1.13 itojun case IFAN_DEPARTURE:
489 1.13 itojun invoke_script(NULL, NULL, DEPARTURE, p->index,
490 1.13 itojun p->ifname);
491 1.13 itojun break;
492 1.13 itojun default:
493 1.13 itojun if (verbose)
494 1.13 itojun (void) printf("unknown announce: "
495 1.13 itojun "what=%d\n", ifan->ifan_what);
496 1.13 itojun break;
497 1.13 itojun }
498 1.13 itojun return;
499 1.10 martin }
500 1.10 martin }
501 1.10 martin }
502 1.10 martin
503 1.1 martin static void rescan_interfaces()
504 1.1 martin {
505 1.1 martin struct interface_data * p;
506 1.1 martin
507 1.1 martin SLIST_FOREACH(p, &ifs, next) {
508 1.13 itojun p->index = if_nametoindex(p->ifname);
509 1.13 itojun if (verbose)
510 1.13 itojun printf("interface \"%s\" has index %d\n", p->ifname,
511 1.13 itojun p->index);
512 1.1 martin }
513 1.1 martin }
514 1.1 martin
515 1.1 martin static void free_interfaces()
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.1 martin static int find_interface(index)
528 1.1 martin int index;
529 1.1 martin {
530 1.1 martin struct interface_data * p;
531 1.1 martin
532 1.1 martin SLIST_FOREACH(p, &ifs, next)
533 1.13 itojun if (p->index == index)
534 1.13 itojun return 1;
535 1.1 martin return 0;
536 1.3 martin }
537 1.3 martin
538 1.3 martin static void run_initial_ups()
539 1.3 martin {
540 1.3 martin struct interface_data * ifd;
541 1.3 martin struct ifaddrs *res = NULL, *p;
542 1.3 martin
543 1.3 martin if (getifaddrs(&res) == 0) {
544 1.13 itojun for (p = res; p; p = p->ifa_next) {
545 1.13 itojun SLIST_FOREACH(ifd, &ifs, next) {
546 1.13 itojun if (strcmp(ifd->ifname, p->ifa_name) == 0)
547 1.13 itojun break;
548 1.13 itojun }
549 1.13 itojun if (ifd == NULL)
550 1.13 itojun continue;
551 1.13 itojun
552 1.13 itojun if (p->ifa_addr && p->ifa_addr->sa_family == AF_LINK)
553 1.13 itojun invoke_script(NULL, NULL, ARRIVAL, ifd->index,
554 1.13 itojun NULL);
555 1.13 itojun
556 1.13 itojun if ((p->ifa_flags & IFF_UP) == 0)
557 1.13 itojun continue;
558 1.13 itojun if (p->ifa_addr == NULL)
559 1.13 itojun continue;
560 1.13 itojun if (p->ifa_addr->sa_family == AF_LINK)
561 1.13 itojun continue;
562 1.13 itojun if (if_is_connected(ifd->ifname))
563 1.13 itojun invoke_script(p->ifa_addr, p->ifa_dstaddr, UP,
564 1.13 itojun ifd->index, ifd->ifname);
565 1.11 martin }
566 1.13 itojun freeifaddrs(res);
567 1.3 martin }
568 1.1 martin }
569 1.5 martin
570 1.5 martin #ifdef SPPP_IF_SUPPORT
571 1.5 martin /*
572 1.5 martin * Special case support for in-kernel PPP interfaces.
573 1.5 martin * If these are IFF_UP, but have not yet connected or completed authentication
574 1.5 martin * we don't want to call the up script in the initial interface scan (there
575 1.5 martin * will be an UP event generated later, when IPCP completes, anyway).
576 1.5 martin *
577 1.5 martin * If this is no if_spppsubr.c based interface, this ioctl just fails and we
578 1.5 martin * treat is as connected.
579 1.5 martin */
580 1.5 martin static int
581 1.18 martin check_is_connected(const char * ifname, int def_retval)
582 1.5 martin {
583 1.5 martin int s, err;
584 1.18 martin struct spppstatus oldstatus;
585 1.18 martin struct spppstatusncp status;
586 1.5 martin
587 1.5 martin memset(&status, 0, sizeof status);
588 1.5 martin strncpy(status.ifname, ifname, sizeof status.ifname);
589 1.18 martin memset(&oldstatus, 0, sizeof oldstatus);
590 1.18 martin strncpy(oldstatus.ifname, ifname, sizeof oldstatus.ifname);
591 1.18 martin
592 1.5 martin s = socket(AF_INET, SOCK_DGRAM, 0);
593 1.5 martin if (s < 0)
594 1.13 itojun return 1; /* no idea how to handle this... */
595 1.18 martin err = ioctl(s, SPPPGETSTATUSNCP, &status);
596 1.18 martin if (err != 0) {
597 1.18 martin err = ioctl(s, SPPPGETSTATUS, &oldstatus);
598 1.18 martin if (err != 0) {
599 1.18 martin /* not if_spppsubr.c based - return default */
600 1.18 martin close(s);
601 1.18 martin return def_retval;
602 1.18 martin } else {
603 1.18 martin /* can't query NCPs, so use default */
604 1.18 martin status.phase = oldstatus.phase;
605 1.18 martin status.ncpup = def_retval;
606 1.18 martin }
607 1.18 martin }
608 1.5 martin close(s);
609 1.18 martin
610 1.18 martin return status.phase == SPPP_PHASE_NETWORK && status.ncpup > 0;
611 1.5 martin }
612 1.5 martin #endif
613