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