1 /*	$OpenBSD: dispatch.c,v 1.39 2007/02/14 23:15:01 stevesk Exp $	*/
2 
3 /*
4  * Copyright 2004 Henning Brauer <henning@openbsd.org>
5  * Copyright (c) 1995, 1996, 1997, 1998, 1999
6  * The Internet Software Consortium.   All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of The Internet Software Consortium nor the names
18  *    of its contributors may be used to endorse or promote products derived
19  *    from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
22  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * This software has been written for the Internet Software Consortium
36  * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
37  * Enterprises.  To learn more about the Internet Software Consortium,
38  * see ``http://www.vix.com/isc''.  To learn more about Vixie
39  * Enterprises, see ``http://www.vix.com''.
40  */
41 
42 #include "dhcpd.h"
43 
44 #include <sys/ioctl.h>
45 
46 #include <net/if_media.h>
47 #include <ifaddrs.h>
48 #include <poll.h>
49 
50 struct timeout *timeouts;
51 static struct timeout *free_timeouts;
52 static int interfaces_invalidated;
53 
54 static int interface_status(void);
55 
56 /*
57  * Use getifaddrs() to get a list of all the attached interfaces.  For
58  * each interface that's of type INET and not the loopback interface,
59  * register that interface with the network I/O software, figure out
60  * what subnet it's on, and add it to the list of interfaces.
61  */
62 void
discover_interface(void)63 discover_interface(void)
64 {
65 	struct ifaddrs *ifap, *ifa;
66 	struct ifreq *tif;
67 	int len = IFNAMSIZ + sizeof(struct sockaddr_storage);
68 
69 	if (getifaddrs(&ifap) != 0)
70 		error("getifaddrs failed");
71 
72 	for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
73 		if ((ifa->ifa_flags & IFF_LOOPBACK) ||
74 		    (ifa->ifa_flags & IFF_POINTOPOINT) ||
75 		    (!(ifa->ifa_flags & IFF_UP)))
76 			continue;
77 
78 		if (strcmp(ifi->name, ifa->ifa_name))
79 			continue;
80 
81 		/*
82 		 * If we have the capability, extract link information
83 		 * and record it in a linked list.
84 		 */
85 		if (ifa->ifa_addr->sa_family == AF_LINK) {
86 			struct sockaddr_dl *foo =
87 			    (struct sockaddr_dl *)ifa->ifa_addr;
88 
89 			ifi->index = foo->sdl_index;
90 			ifi->hw_address.hlen = foo->sdl_alen;
91 			ifi->hw_address.htype = HTYPE_ETHER; /* XXX */
92 			memcpy(ifi->hw_address.haddr,
93 			    LLADDR(foo), foo->sdl_alen);
94 		}
95 		if (!ifi->ifp) {
96 			if ((tif = malloc(len)) == NULL)
97 				error("no space to remember ifp");
98 			strlcpy(tif->ifr_name, ifa->ifa_name, IFNAMSIZ);
99 			ifi->ifp = tif;
100 		}
101 	}
102 
103 	if (!ifi->ifp)
104 		error("%s: not found", ifi->name);
105 
106 	/* Register the interface... */
107 	if_register_receive();
108 	if_register_send();
109 	freeifaddrs(ifap);
110 }
111 
112 void
reinitialize_interface(void)113 reinitialize_interface(void)
114 {
115 	interfaces_invalidated = 1;
116 }
117 
118 /*
119  * Wait for packets to come in using poll().  When a packet comes in, call
120  * receive_packet to receive the packet and possibly strip hardware addressing
121  * information from it, and then call do_packet to try to do something with it.
122  */
123 void
dispatch(void)124 dispatch(void)
125 {
126 	int count, to_msec;
127 	struct pollfd fds[2];
128 	time_t howlong;
129 
130 	do {
131 		/*
132 		 * Call any expired timeouts, and then if there's still
133 		 * a timeout registered, time out the select call then.
134 		 */
135 another:
136 		if (timeouts) {
137 			struct timeout *t;
138 
139 			if (timeouts->when <= cur_time) {
140 				t = timeouts;
141 				timeouts = timeouts->next;
142 				(*(t->func))();
143 				t->next = free_timeouts;
144 				free_timeouts = t;
145 				goto another;
146 			}
147 
148 			/*
149 			 * Figure timeout in milliseconds, and check for
150 			 * potential overflow, so we can cram into an
151 			 * int for poll, while not polling with a
152 			 * negative timeout and blocking indefinitely.
153 			 */
154 			howlong = timeouts->when - cur_time;
155 			if (howlong > INT_MAX / 1000)
156 				howlong = INT_MAX / 1000;
157 			to_msec = howlong * 1000;
158 		} else
159 			to_msec = -1;
160 
161 		/* Set up the descriptors to be polled. */
162 		if (!ifi || ifi->rfdesc == -1)
163 			error("No live interface to poll on");
164 
165 		fds[0].fd = ifi->rfdesc;
166 		fds[1].fd = routefd; /* Could be -1, which will be ignored. */
167 		fds[0].events = fds[1].events = POLLIN;
168 
169 		/* Wait for a packet or a timeout... XXX */
170 		count = poll(fds, 2, to_msec);
171 
172 		/* Get the current time... */
173 		time(&cur_time);
174 
175 		/* Not likely to be transitory... */
176 		if (count == -1) {
177 			if (errno == EAGAIN || errno == EINTR) {
178 				continue;
179 			} else
180 				error("poll: %m");
181 		}
182 
183 		if ((fds[0].revents & (POLLIN | POLLHUP))) {
184 			if (ifi && ifi->rfdesc != -1)
185 				got_one();
186 		}
187 		if ((fds[1].revents & (POLLIN | POLLHUP))) {
188 			if (ifi && !interfaces_invalidated)
189 				routehandler();
190 		}
191 
192 		interfaces_invalidated = 0;
193 	} while (1);
194 }
195 
196 void
got_one(void)197 got_one(void)
198 {
199 	struct sockaddr_in from;
200 	struct hardware hfrom;
201 	struct iaddr ifrom;
202 	ssize_t result;
203 
204 	if ((result = receive_packet(&from, &hfrom)) == -1) {
205 		warning("receive_packet failed on %s: %s", ifi->name,
206 		    strerror(errno));
207 		ifi->errors++;
208 		if ((!interface_status()) ||
209 		    (ifi->noifmedia && ifi->errors > 20)) {
210 			/* our interface has gone away. */
211 			warning("Interface %s no longer appears valid.",
212 			    ifi->name);
213 			interfaces_invalidated = 1;
214 			close(ifi->rfdesc);
215 			ifi->rfdesc = -1;
216 		}
217 		return;
218 	}
219 	if (result == 0)
220 		return;
221 
222 	ifrom.len = 4;
223 	memcpy(ifrom.iabuf, &from.sin_addr, ifrom.len);
224 
225 	do_packet(result, from.sin_port, ifrom, &hfrom);
226 }
227 
228 int
interface_link_forceup(char * ifname)229 interface_link_forceup(char *ifname)
230 {
231 	struct ifreq ifr;
232 	int sock;
233 
234 	if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
235 		error("Can't create socket");
236 
237 	memset(&ifr, 0, sizeof(ifr));
238 	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
239 	if (ioctl(sock, SIOCGIFFLAGS, (caddr_t)&ifr) == -1) {
240 		close(sock);
241 		return (-1);
242 	}
243 
244 	if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
245 		ifr.ifr_flags |= IFF_UP;
246 		if (ioctl(sock, SIOCSIFFLAGS, (caddr_t)&ifr) == -1) {
247 			close(sock);
248 			return (-1);
249 		}
250 		close(sock);
251 		return (0);
252 	}
253 	close(sock);
254 	return (1);
255 }
256 
257 void
interface_link_forcedown(char * ifname)258 interface_link_forcedown(char *ifname)
259 {
260 	struct ifreq ifr;
261 	int sock;
262 
263 	if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
264 		error("Can't create socket");
265 
266 	memset(&ifr, 0, sizeof(ifr));
267 	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
268 	if (ioctl(sock, SIOCGIFFLAGS, (caddr_t)&ifr) == -1) {
269 		close(sock);
270 		return;
271 	}
272 
273 	if ((ifr.ifr_flags & IFF_UP) == IFF_UP) {
274 		ifr.ifr_flags &= ~IFF_UP;
275 		if (ioctl(sock, SIOCSIFFLAGS, (caddr_t)&ifr) == -1) {
276 			close(sock);
277 			return;
278 		}
279 	}
280 
281 	close(sock);
282 }
283 
284 int
interface_status(void)285 interface_status(void)
286 {
287 	char *ifname = ifi->name;
288 	int ifsock = ifi->rfdesc;
289 	struct ifreq ifr;
290 	struct ifmediareq ifmr;
291 
292 	/* get interface flags */
293 	memset(&ifr, 0, sizeof(ifr));
294 	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
295 	if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) < 0) {
296 		warning("ioctl(SIOCGIFFLAGS) on %s: %m", ifname);
297 		goto inactive;
298 	}
299 
300 	/*
301 	 * if one of UP and RUNNING flags is dropped,
302 	 * the interface is not active.
303 	 */
304 	if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
305 		goto inactive;
306 
307 	/* Next, check carrier on the interface, if possible */
308 	if (ifi->noifmedia)
309 		goto active;
310 	memset(&ifmr, 0, sizeof(ifmr));
311 	strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
312 	if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
313 		if (errno != EINVAL) {
314 			debug("ioctl(SIOCGIFMEDIA) on %s: %m", ifname);
315 
316 			ifi->noifmedia = 1;
317 			goto active;
318 		}
319 		/*
320 		 * EINVAL (or ENOTTY) simply means that the interface
321 		 * does not support the SIOCGIFMEDIA ioctl. We regard it alive.
322 		 */
323 		ifi->noifmedia = 1;
324 		goto active;
325 	}
326 	if (ifmr.ifm_status & IFM_AVALID) {
327 		switch (ifmr.ifm_active & IFM_NMASK) {
328 		case IFM_ETHER:
329 			if (ifmr.ifm_status & IFM_ACTIVE)
330 				goto active;
331 			else
332 				goto inactive;
333 			break;
334 		default:
335 			goto inactive;
336 		}
337 	}
338 inactive:
339 	return (0);
340 active:
341 	return (1);
342 }
343 
344 void
add_timeout(time_t when,void (* where)(void))345 add_timeout(time_t when, void (*where)(void))
346 {
347 	struct timeout *t, *q;
348 
349 	/* See if this timeout supersedes an existing timeout. */
350 	t = NULL;
351 	for (q = timeouts; q; q = q->next) {
352 		if (q->func == where) {
353 			if (t)
354 				t->next = q->next;
355 			else
356 				timeouts = q->next;
357 			break;
358 		}
359 		t = q;
360 	}
361 
362 	/* If we didn't supersede a timeout, allocate a timeout
363 	   structure now. */
364 	if (!q) {
365 		if (free_timeouts) {
366 			q = free_timeouts;
367 			free_timeouts = q->next;
368 			q->func = where;
369 		} else {
370 			q = malloc(sizeof(struct timeout));
371 			if (!q)
372 				error("Can't allocate timeout structure!");
373 			q->func = where;
374 		}
375 	}
376 
377 	q->when = when;
378 
379 	/* Now sort this timeout into the timeout list. */
380 
381 	/* Beginning of list? */
382 	if (!timeouts || timeouts->when > q->when) {
383 		q->next = timeouts;
384 		timeouts = q;
385 		return;
386 	}
387 
388 	/* Middle of list? */
389 	for (t = timeouts; t->next; t = t->next) {
390 		if (t->next->when > q->when) {
391 			q->next = t->next;
392 			t->next = q;
393 			return;
394 		}
395 	}
396 
397 	/* End of list. */
398 	t->next = q;
399 	q->next = NULL;
400 }
401 
402 void
cancel_timeout(void (* where)(void))403 cancel_timeout(void (*where)(void))
404 {
405 	struct timeout *t, *q;
406 
407 	/* Look for this timeout on the list, and unlink it if we find it. */
408 	t = NULL;
409 	for (q = timeouts; q; q = q->next) {
410 		if (q->func == where) {
411 			if (t)
412 				t->next = q->next;
413 			else
414 				timeouts = q->next;
415 			break;
416 		}
417 		t = q;
418 	}
419 
420 	/* If we found the timeout, put it on the free list. */
421 	if (q) {
422 		q->next = free_timeouts;
423 		free_timeouts = q;
424 	}
425 }
426 
427 int
interface_link_status(char * ifname)428 interface_link_status(char *ifname)
429 {
430 	struct ifmediareq ifmr;
431 	int sock;
432 
433 	if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
434 		error("Can't create socket");
435 
436 	memset(&ifmr, 0, sizeof(ifmr));
437 	strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
438 	if (ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
439 		/* EINVAL -> link state unknown. treat as active */
440 		if (errno != EINVAL)
441 			debug("ioctl(SIOCGIFMEDIA) on %s: %m", ifname);
442 		close(sock);
443 		return (1);
444 	}
445 	close(sock);
446 
447 	if (ifmr.ifm_status & IFM_AVALID) {
448 		if ((ifmr.ifm_active & IFM_NMASK) == IFM_ETHER) {
449 			if (ifmr.ifm_status & IFM_ACTIVE)
450 				return (1);
451 			else
452 				return (0);
453 		}
454 	}
455 	return (1);
456 }
457