ifwatchd.c revision 1.9 1 1.9 tron /* $NetBSD: ifwatchd.c,v 1.9 2002/04/15 21:08:41 tron Exp $ */
2 1.2 martin
3 1.6 martin /*-
4 1.6 martin * Copyright (c) 2002 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.6 martin * 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.1 martin
68 1.1 martin /* local functions */
69 1.1 martin static void usage(void);
70 1.1 martin static void dispatch(void*, size_t);
71 1.1 martin static void check_addrs(char *cp, int addrs, int is_up);
72 1.4 martin static void invoke_script(struct sockaddr *sa, struct sockaddr *dst, int is_up, int ifindex);
73 1.1 martin static void list_interfaces(const char *ifnames);
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.5 martin static int if_is_connected(const char * ifname);
81 1.5 martin #else
82 1.5 martin #define if_is_connected(X) 1
83 1.5 martin #endif
84 1.5 martin
85 1.1 martin /* stolen from /sbin/route */
86 1.1 martin #define ROUNDUP(a) \
87 1.1 martin ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
88 1.1 martin #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
89 1.1 martin
90 1.1 martin /* global variables */
91 1.1 martin static int verbose = 0;
92 1.3 martin static int inhibit_initial = 0;
93 1.1 martin static const char *up_script = NULL;
94 1.1 martin static const char *down_script = NULL;
95 1.9 tron static char DummyTTY[] = _PATH_DEVNULL;
96 1.9 tron static char DummySpeed[] = "9600";
97 1.1 martin
98 1.1 martin struct interface_data {
99 1.1 martin SLIST_ENTRY(interface_data) next;
100 1.1 martin int index;
101 1.1 martin char * ifname;
102 1.1 martin };
103 1.1 martin SLIST_HEAD(,interface_data) ifs = SLIST_HEAD_INITIALIZER(ifs);
104 1.1 martin
105 1.1 martin int
106 1.1 martin main(int argc, char **argv)
107 1.1 martin {
108 1.1 martin int c, s, n;
109 1.1 martin int errs = 0;
110 1.1 martin char msg[2048], *msgp;
111 1.1 martin
112 1.3 martin while ((c = getopt(argc, argv, "vhiu:d:")) != -1)
113 1.1 martin switch (c) {
114 1.1 martin case 'h':
115 1.1 martin usage();
116 1.1 martin return 0;
117 1.3 martin case 'i':
118 1.3 martin inhibit_initial = 1;
119 1.3 martin break;
120 1.1 martin case 'v':
121 1.1 martin verbose++;
122 1.1 martin break;
123 1.1 martin
124 1.1 martin case 'u':
125 1.1 martin up_script = optarg;
126 1.1 martin break;
127 1.1 martin
128 1.1 martin case 'd':
129 1.1 martin down_script = optarg;
130 1.1 martin break;
131 1.1 martin
132 1.1 martin default:
133 1.1 martin errs++;
134 1.1 martin break;
135 1.1 martin }
136 1.1 martin
137 1.1 martin if (errs)
138 1.1 martin usage();
139 1.1 martin
140 1.1 martin argv += optind;
141 1.1 martin argc -= optind;
142 1.1 martin
143 1.1 martin if (argc <= 0)
144 1.1 martin usage();
145 1.1 martin
146 1.1 martin if (verbose) {
147 1.1 martin printf("up_script: %s\ndown_script: %s\n",
148 1.1 martin up_script, down_script);
149 1.1 martin printf("verbosity = %d\n", verbose);
150 1.1 martin }
151 1.1 martin
152 1.1 martin while (argc > 0) {
153 1.1 martin list_interfaces(argv[0]);
154 1.1 martin argv++; argc--;
155 1.1 martin }
156 1.1 martin
157 1.1 martin if (!verbose)
158 1.1 martin daemon(0,0);
159 1.1 martin
160 1.3 martin if (!inhibit_initial)
161 1.3 martin run_initial_ups();
162 1.3 martin
163 1.1 martin s = socket(PF_ROUTE, SOCK_RAW, 0);
164 1.1 martin if (s < 0) {
165 1.1 martin perror("open routing socket");
166 1.9 tron exit(EXIT_FAILURE);
167 1.1 martin }
168 1.1 martin
169 1.1 martin for (;;) {
170 1.1 martin n = read(s, msg, sizeof msg);
171 1.1 martin msgp = msg;
172 1.1 martin for (msgp = msg; n > 0; n -= ((struct rt_msghdr*)msgp)->rtm_msglen, msgp += ((struct rt_msghdr*)msgp)->rtm_msglen) {
173 1.1 martin dispatch(msgp, n);
174 1.1 martin
175 1.1 martin }
176 1.1 martin }
177 1.1 martin
178 1.1 martin close(s);
179 1.1 martin free_interfaces();
180 1.1 martin
181 1.9 tron return EXIT_SUCCESS;
182 1.1 martin }
183 1.1 martin
184 1.1 martin static void
185 1.1 martin usage()
186 1.1 martin {
187 1.1 martin fprintf(stderr,
188 1.1 martin "usage:\n"
189 1.1 martin "\tifwatchd [-h] [-v] [-u up-script] [-d down-script] ifname(s)\n"
190 1.1 martin "\twhere:\n"
191 1.1 martin "\t -h show this help message\n"
192 1.1 martin "\t -v verbose/debug output, don't run in background\n"
193 1.3 martin "\t -i no (!) initial run of the up script if the interface\n"
194 1.3 martin "\t is already up on ifwatchd startup\n"
195 1.1 martin "\t -u <cmd> specify command to run on interface up event\n"
196 1.1 martin "\t -d <cmd> specify command to run on interface down event\n");
197 1.9 tron exit(EXIT_FAILURE);
198 1.1 martin }
199 1.1 martin
200 1.1 martin static void
201 1.1 martin dispatch(void *msg, size_t len)
202 1.1 martin {
203 1.1 martin struct rt_msghdr *hd = msg;
204 1.1 martin struct ifa_msghdr *ifam;
205 1.1 martin int is_up;
206 1.1 martin
207 1.1 martin is_up = 0;
208 1.1 martin switch (hd->rtm_type) {
209 1.1 martin case RTM_NEWADDR:
210 1.1 martin is_up = 1;
211 1.1 martin goto work;
212 1.1 martin case RTM_DELADDR:
213 1.1 martin is_up = 0;
214 1.1 martin goto work;
215 1.1 martin case RTM_IFANNOUNCE:
216 1.1 martin rescan_interfaces();
217 1.1 martin break;
218 1.1 martin }
219 1.1 martin if (verbose)
220 1.1 martin printf("unknown message ignored\n");
221 1.1 martin return;
222 1.1 martin
223 1.1 martin work:
224 1.1 martin ifam = (struct ifa_msghdr *)msg;
225 1.1 martin check_addrs((char *)(ifam + 1), ifam->ifam_addrs, is_up);
226 1.1 martin }
227 1.1 martin
228 1.1 martin static void
229 1.1 martin check_addrs(cp, addrs, is_up)
230 1.1 martin char *cp;
231 1.1 martin int addrs, is_up;
232 1.1 martin {
233 1.4 martin struct sockaddr *sa, *ifa = NULL, *brd = NULL;
234 1.1 martin int ifndx = 0, i;
235 1.1 martin
236 1.1 martin if (addrs == 0)
237 1.1 martin return;
238 1.1 martin for (i = 1; i; i <<= 1) {
239 1.1 martin if (i & addrs) {
240 1.1 martin sa = (struct sockaddr *)cp;
241 1.1 martin if (i == RTA_IFP) {
242 1.1 martin struct sockaddr_dl * li = (struct sockaddr_dl*)sa;
243 1.1 martin ifndx = li->sdl_index;
244 1.1 martin if (!find_interface(ifndx)) {
245 1.1 martin if (verbose)
246 1.1 martin printf("ignoring change on interface #%d\n", ifndx);
247 1.1 martin return;
248 1.1 martin }
249 1.1 martin } else if (i == RTA_IFA) {
250 1.4 martin ifa = sa;
251 1.4 martin } else if (i == RTA_BRD) {
252 1.4 martin brd = sa;
253 1.1 martin }
254 1.1 martin ADVANCE(cp, sa);
255 1.1 martin }
256 1.1 martin }
257 1.4 martin if (ifa != NULL)
258 1.8 tron invoke_script(ifa, brd, is_up, ifndx);
259 1.1 martin }
260 1.1 martin
261 1.1 martin static void
262 1.4 martin invoke_script(sa, dest, is_up, ifindex)
263 1.4 martin struct sockaddr *sa, *dest;
264 1.1 martin int is_up, ifindex;
265 1.1 martin {
266 1.4 martin char addr[NI_MAXHOST], daddr[NI_MAXHOST], ifname_buf[IFNAMSIZ],
267 1.9 tron *ifname;
268 1.1 martin const char *script;
269 1.9 tron int status;
270 1.8 tron
271 1.8 tron if (sa->sa_family == AF_INET6) {
272 1.8 tron struct sockaddr_in6 sin6;
273 1.8 tron
274 1.8 tron (void) memcpy(&sin6, (struct sockaddr_in6 *)sa, sizeof (sin6));
275 1.8 tron if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr))
276 1.8 tron return;
277 1.8 tron }
278 1.1 martin
279 1.4 martin daddr[0] = 0;
280 1.1 martin ifname = if_indextoname(ifindex, ifname_buf);
281 1.1 martin if (sa->sa_len == 0) {
282 1.1 martin fprintf(stderr, "illegal socket address (sa_len == 0)\n");
283 1.1 martin return;
284 1.1 martin }
285 1.1 martin
286 1.1 martin if (getnameinfo(sa, sa->sa_len, addr, sizeof addr, NULL, 0, NI_NUMERICHOST)) {
287 1.1 martin if (verbose)
288 1.1 martin printf("getnameinfo failed\n");
289 1.1 martin return; /* this address can not be handled */
290 1.1 martin }
291 1.4 martin if (dest != NULL) {
292 1.4 martin if (getnameinfo(dest, dest->sa_len, daddr, sizeof daddr, NULL, 0, NI_NUMERICHOST)) {
293 1.4 martin if (verbose)
294 1.4 martin printf("getnameinfo failed\n");
295 1.4 martin return; /* this address can not be handled */
296 1.4 martin }
297 1.4 martin }
298 1.1 martin
299 1.1 martin script = is_up? up_script : down_script;
300 1.1 martin if (script == NULL) return;
301 1.1 martin
302 1.9 tron if (verbose)
303 1.9 tron (void) printf("calling: %s %s %s %s %s %s\n",
304 1.9 tron script, ifname, DummyTTY, DummySpeed, addr, daddr);
305 1.9 tron
306 1.9 tron switch (vfork()) {
307 1.9 tron case -1:
308 1.9 tron fprintf(stderr, "cannot fork\n");
309 1.9 tron break;
310 1.9 tron case 0:
311 1.9 tron (void) execl(script, script, ifname, DummyTTY, DummySpeed,
312 1.9 tron addr, daddr, NULL);
313 1.9 tron _exit(EXIT_FAILURE);
314 1.9 tron default:
315 1.9 tron (void) wait(&status);
316 1.1 martin }
317 1.1 martin }
318 1.1 martin
319 1.1 martin static void list_interfaces(const char *ifnames)
320 1.1 martin {
321 1.1 martin char * names = strdup(ifnames);
322 1.1 martin char * name, *lasts;
323 1.1 martin static const char sep[] = " \t";
324 1.1 martin struct interface_data * p;
325 1.1 martin
326 1.1 martin for (name = strtok_r(names, sep, &lasts); name != NULL; name = strtok_r(NULL, sep, &lasts)) {
327 1.1 martin p = malloc(sizeof(*p));
328 1.1 martin SLIST_INSERT_HEAD(&ifs, p, next);
329 1.1 martin p->ifname = strdup(name);
330 1.1 martin p->index = if_nametoindex(p->ifname);
331 1.1 martin if (verbose)
332 1.1 martin printf("interface \"%s\" has index %d\n", p->ifname, p->index);
333 1.1 martin }
334 1.1 martin free(names);
335 1.1 martin }
336 1.1 martin
337 1.1 martin static void rescan_interfaces()
338 1.1 martin {
339 1.1 martin struct interface_data * p;
340 1.1 martin
341 1.1 martin SLIST_FOREACH(p, &ifs, next) {
342 1.1 martin p->index = if_nametoindex(p->ifname);
343 1.1 martin if (verbose)
344 1.1 martin printf("interface \"%s\" has index %d\n", p->ifname, p->index);
345 1.1 martin }
346 1.1 martin }
347 1.1 martin
348 1.1 martin static void free_interfaces()
349 1.1 martin {
350 1.1 martin struct interface_data * p;
351 1.1 martin
352 1.1 martin while (!SLIST_EMPTY(&ifs)) {
353 1.1 martin p = SLIST_FIRST(&ifs);
354 1.1 martin SLIST_REMOVE_HEAD(&ifs, next);
355 1.1 martin free(p->ifname);
356 1.1 martin free(p);
357 1.1 martin }
358 1.1 martin }
359 1.1 martin
360 1.1 martin static int find_interface(index)
361 1.1 martin int index;
362 1.1 martin {
363 1.1 martin struct interface_data * p;
364 1.1 martin
365 1.1 martin SLIST_FOREACH(p, &ifs, next)
366 1.1 martin if (p->index == index)
367 1.1 martin return 1;
368 1.1 martin return 0;
369 1.3 martin }
370 1.3 martin
371 1.3 martin static void run_initial_ups()
372 1.3 martin {
373 1.3 martin struct interface_data * ifd;
374 1.3 martin struct ifaddrs *res = NULL, *p;
375 1.3 martin
376 1.3 martin if (getifaddrs(&res) == 0) {
377 1.3 martin for (p = res; p; p = p->ifa_next) {
378 1.3 martin if ((p->ifa_flags & IFF_UP) == 0)
379 1.3 martin continue;
380 1.3 martin if (p->ifa_addr == NULL)
381 1.3 martin continue;
382 1.3 martin if (p->ifa_addr->sa_family == AF_LINK)
383 1.3 martin continue;
384 1.3 martin SLIST_FOREACH(ifd, &ifs, next) {
385 1.3 martin if (strcmp(ifd->ifname, p->ifa_name) == 0) {
386 1.5 martin if (if_is_connected(ifd->ifname))
387 1.5 martin invoke_script(p->ifa_addr, p->ifa_dstaddr, 1, ifd->index);
388 1.3 martin break;
389 1.3 martin }
390 1.3 martin }
391 1.3 martin }
392 1.3 martin freeifaddrs(res);
393 1.3 martin }
394 1.1 martin }
395 1.5 martin
396 1.5 martin #ifdef SPPP_IF_SUPPORT
397 1.5 martin /*
398 1.5 martin * Special case support for in-kernel PPP interfaces.
399 1.5 martin * If these are IFF_UP, but have not yet connected or completed authentication
400 1.5 martin * we don't want to call the up script in the initial interface scan (there
401 1.5 martin * will be an UP event generated later, when IPCP completes, anyway).
402 1.5 martin *
403 1.5 martin * If this is no if_spppsubr.c based interface, this ioctl just fails and we
404 1.5 martin * treat is as connected.
405 1.5 martin */
406 1.5 martin static int
407 1.5 martin if_is_connected(const char * ifname)
408 1.5 martin {
409 1.5 martin int s, err;
410 1.5 martin struct spppstatus status;
411 1.5 martin
412 1.5 martin memset(&status, 0, sizeof status);
413 1.5 martin strncpy(status.ifname, ifname, sizeof status.ifname);
414 1.5 martin s = socket(AF_INET, SOCK_DGRAM, 0);
415 1.5 martin if (s < 0)
416 1.5 martin return 1; /* no idea how to handle this... */
417 1.5 martin err = ioctl(s, SPPPGETSTATUS, &status);
418 1.5 martin if (err != 0)
419 1.5 martin /* not if_spppsubr.c based - call it connected */
420 1.5 martin status.phase = SPPP_PHASE_NETWORK;
421 1.5 martin close(s);
422 1.5 martin return status.phase == SPPP_PHASE_NETWORK;
423 1.5 martin }
424 1.5 martin #endif
425