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