ifwatchd.c revision 1.4 1 /* $NetBSD: ifwatchd.c,v 1.4 2002/01/10 19:38:51 martin Exp $ */
2
3 /*
4 * Copyright (c) 2001 Martin Husemann <martin (at) duskware.de>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software withough specific prior written permission
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
31 #include <sys/queue.h>
32 #include <net/if.h>
33 #include <net/if_dl.h>
34 #include <net/route.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <netdb.h>
43 #include <err.h>
44 #include <ifaddrs.h>
45
46 /* local functions */
47 static void usage(void);
48 static void dispatch(void*, size_t);
49 static void check_addrs(char *cp, int addrs, int is_up);
50 static void invoke_script(struct sockaddr *sa, struct sockaddr *dst, int is_up, int ifindex);
51 static void list_interfaces(const char *ifnames);
52 static void rescan_interfaces(void);
53 static void free_interfaces(void);
54 static int find_interface(int index);
55 static void run_initial_ups(void);
56
57 /* stolen from /sbin/route */
58 #define ROUNDUP(a) \
59 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
60 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
61
62 /* global variables */
63 static int verbose = 0;
64 static int inhibit_initial = 0;
65 static const char *up_script = NULL;
66 static const char *down_script = NULL;
67
68 struct interface_data {
69 SLIST_ENTRY(interface_data) next;
70 int index;
71 char * ifname;
72 };
73 SLIST_HEAD(,interface_data) ifs = SLIST_HEAD_INITIALIZER(ifs);
74
75 int
76 main(int argc, char **argv)
77 {
78 int c, s, n;
79 int errs = 0;
80 char msg[2048], *msgp;
81
82 while ((c = getopt(argc, argv, "vhiu:d:")) != -1)
83 switch (c) {
84 case 'h':
85 usage();
86 return 0;
87 case 'i':
88 inhibit_initial = 1;
89 break;
90 case 'v':
91 verbose++;
92 break;
93
94 case 'u':
95 up_script = optarg;
96 break;
97
98 case 'd':
99 down_script = optarg;
100 break;
101
102 default:
103 errs++;
104 break;
105 }
106
107 if (errs)
108 usage();
109
110 argv += optind;
111 argc -= optind;
112
113 if (argc <= 0)
114 usage();
115
116 if (verbose) {
117 printf("up_script: %s\ndown_script: %s\n",
118 up_script, down_script);
119 printf("verbosity = %d\n", verbose);
120 }
121
122 while (argc > 0) {
123 list_interfaces(argv[0]);
124 argv++; argc--;
125 }
126
127 if (!verbose)
128 daemon(0,0);
129
130 if (!inhibit_initial)
131 run_initial_ups();
132
133 s = socket(PF_ROUTE, SOCK_RAW, 0);
134 if (s < 0) {
135 perror("open routing socket");
136 exit(1);
137 }
138
139 for (;;) {
140 n = read(s, msg, sizeof msg);
141 msgp = msg;
142 for (msgp = msg; n > 0; n -= ((struct rt_msghdr*)msgp)->rtm_msglen, msgp += ((struct rt_msghdr*)msgp)->rtm_msglen) {
143 dispatch(msgp, n);
144
145 }
146 }
147
148 close(s);
149 free_interfaces();
150
151 exit(0);
152 }
153
154 static void
155 usage()
156 {
157 fprintf(stderr,
158 "usage:\n"
159 "\tifwatchd [-h] [-v] [-u up-script] [-d down-script] ifname(s)\n"
160 "\twhere:\n"
161 "\t -h show this help message\n"
162 "\t -v verbose/debug output, don't run in background\n"
163 "\t -i no (!) initial run of the up script if the interface\n"
164 "\t is already up on ifwatchd startup\n"
165 "\t -u <cmd> specify command to run on interface up event\n"
166 "\t -d <cmd> specify command to run on interface down event\n");
167 exit(1);
168 }
169
170 static void
171 dispatch(void *msg, size_t len)
172 {
173 struct rt_msghdr *hd = msg;
174 struct ifa_msghdr *ifam;
175 int is_up;
176
177 is_up = 0;
178 switch (hd->rtm_type) {
179 case RTM_NEWADDR:
180 is_up = 1;
181 goto work;
182 case RTM_DELADDR:
183 is_up = 0;
184 goto work;
185 case RTM_IFANNOUNCE:
186 rescan_interfaces();
187 break;
188 }
189 if (verbose)
190 printf("unknown message ignored\n");
191 return;
192
193 work:
194 ifam = (struct ifa_msghdr *)msg;
195 check_addrs((char *)(ifam + 1), ifam->ifam_addrs, is_up);
196 }
197
198 static void
199 check_addrs(cp, addrs, is_up)
200 char *cp;
201 int addrs, is_up;
202 {
203 struct sockaddr *sa, *ifa = NULL, *brd = NULL;
204 int ifndx = 0, i;
205
206 if (addrs == 0)
207 return;
208 for (i = 1; i; i <<= 1) {
209 if (i & addrs) {
210 sa = (struct sockaddr *)cp;
211 if (i == RTA_IFP) {
212 struct sockaddr_dl * li = (struct sockaddr_dl*)sa;
213 ifndx = li->sdl_index;
214 if (!find_interface(ifndx)) {
215 if (verbose)
216 printf("ignoring change on interface #%d\n", ifndx);
217 return;
218 }
219 } else if (i == RTA_IFA) {
220 ifa = sa;
221 } else if (i == RTA_BRD) {
222 brd = sa;
223 }
224 ADVANCE(cp, sa);
225 }
226 }
227 if (ifa != NULL)
228 invoke_script(ifa, brd, is_up, ifndx);
229 }
230
231 static void
232 invoke_script(sa, dest, is_up, ifindex)
233 struct sockaddr *sa, *dest;
234 int is_up, ifindex;
235 {
236 char addr[NI_MAXHOST], daddr[NI_MAXHOST], ifname_buf[IFNAMSIZ],
237 *ifname, *cmd;
238 const char *script;
239
240 daddr[0] = 0;
241 ifname = if_indextoname(ifindex, ifname_buf);
242 if (sa->sa_len == 0) {
243 fprintf(stderr, "illegal socket address (sa_len == 0)\n");
244 return;
245 }
246
247 if (getnameinfo(sa, sa->sa_len, addr, sizeof addr, NULL, 0, NI_NUMERICHOST)) {
248 if (verbose)
249 printf("getnameinfo failed\n");
250 return; /* this address can not be handled */
251 }
252 if (dest != NULL) {
253 if (getnameinfo(dest, dest->sa_len, daddr, sizeof daddr, NULL, 0, NI_NUMERICHOST)) {
254 if (verbose)
255 printf("getnameinfo failed\n");
256 return; /* this address can not be handled */
257 }
258 }
259
260 script = is_up? up_script : down_script;
261 if (script == NULL) return;
262
263 asprintf(&cmd, "%s \"%s\" %s \"%s\" \"%s\"", script, ifname,
264 is_up?"up":"down", addr, daddr);
265 if (cmd == NULL) {
266 fprintf(stderr, "out of memory\n");
267 return;
268 }
269 if (verbose)
270 printf("calling: %s\n", cmd);
271 system(cmd);
272 free(cmd);
273 }
274
275 static void list_interfaces(const char *ifnames)
276 {
277 char * names = strdup(ifnames);
278 char * name, *lasts;
279 static const char sep[] = " \t";
280 struct interface_data * p;
281
282 for (name = strtok_r(names, sep, &lasts); name != NULL; name = strtok_r(NULL, sep, &lasts)) {
283 p = malloc(sizeof(*p));
284 SLIST_INSERT_HEAD(&ifs, p, next);
285 p->ifname = strdup(name);
286 p->index = if_nametoindex(p->ifname);
287 if (verbose)
288 printf("interface \"%s\" has index %d\n", p->ifname, p->index);
289 }
290 free(names);
291 }
292
293 static void rescan_interfaces()
294 {
295 struct interface_data * p;
296
297 SLIST_FOREACH(p, &ifs, next) {
298 p->index = if_nametoindex(p->ifname);
299 if (verbose)
300 printf("interface \"%s\" has index %d\n", p->ifname, p->index);
301 }
302 }
303
304 static void free_interfaces()
305 {
306 struct interface_data * p;
307
308 while (!SLIST_EMPTY(&ifs)) {
309 p = SLIST_FIRST(&ifs);
310 SLIST_REMOVE_HEAD(&ifs, next);
311 free(p->ifname);
312 free(p);
313 }
314 }
315
316 static int find_interface(index)
317 int index;
318 {
319 struct interface_data * p;
320
321 SLIST_FOREACH(p, &ifs, next)
322 if (p->index == index)
323 return 1;
324 return 0;
325 }
326
327 static void run_initial_ups()
328 {
329 struct interface_data * ifd;
330 struct ifaddrs *res = NULL, *p;
331
332 if (getifaddrs(&res) == 0) {
333 for (p = res; p; p = p->ifa_next) {
334 if ((p->ifa_flags & IFF_UP) == 0)
335 continue;
336 if (p->ifa_addr == NULL)
337 continue;
338 if (p->ifa_addr->sa_family == AF_LINK)
339 continue;
340 SLIST_FOREACH(ifd, &ifs, next) {
341 if (strcmp(ifd->ifname, p->ifa_name) == 0) {
342 invoke_script(p->ifa_addr, p->ifa_dstaddr, 1, ifd->index);
343 break;
344 }
345 }
346 }
347 freeifaddrs(res);
348 }
349 }
350