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