1 /* $OpenBSD: src/sbin/dhclient/dispatch.c,v 1.42 2008/05/26 03:11:49 deraadt Exp $ */
4 * Copyright 2004 Henning Brauer <henning@openbsd.org>
5 * Copyright (c) 1995, 1996, 1997, 1998, 1999
6 * The Internet Software Consortium. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
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''.
42 #include <sys/ioctl.h>
44 #include <net/if_media.h>
51 struct timeout *timeouts;
52 static struct timeout *free_timeouts;
53 static int interfaces_invalidated;
56 * Use getifaddrs() to get a list of all the attached interfaces. For
57 * each interface that's of type INET and not the loopback interface,
58 * register that interface with the network I/O software, figure out
59 * what subnet it's on, and add it to the list of interfaces.
62 discover_interface(void)
64 struct ifaddrs *ifap, *ifa;
66 int len = IFNAMSIZ + sizeof(struct sockaddr_storage);
68 if (getifaddrs(&ifap) != 0)
69 error("getifaddrs failed");
71 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
72 if ((ifa->ifa_flags & IFF_LOOPBACK) ||
73 (ifa->ifa_flags & IFF_POINTOPOINT) ||
74 (!(ifa->ifa_flags & IFF_UP)))
77 if (strcmp(ifi->name, ifa->ifa_name))
81 * If we have the capability, extract link information
82 * and record it in a linked list.
84 if (ifa->ifa_addr->sa_family == AF_LINK) {
85 struct sockaddr_dl *foo =
86 (struct sockaddr_dl *)ifa->ifa_addr;
88 ifi->index = foo->sdl_index;
89 ifi->hw_address.hlen = foo->sdl_alen;
90 ifi->hw_address.htype = HTYPE_ETHER; /* XXX */
91 memcpy(ifi->hw_address.haddr,
92 LLADDR(foo), foo->sdl_alen);
95 if ((tif = malloc(len)) == NULL)
96 error("no space to remember ifp");
97 strlcpy(tif->ifr_name, ifa->ifa_name, IFNAMSIZ);
103 error("%s: not found", ifi->name);
105 /* Register the interface... */
106 if_register_receive();
112 reinitialize_interface(void)
114 interfaces_invalidated = 1;
118 * Wait for packets to come in using poll(). When a packet comes in, call
119 * receive_packet to receive the packet and possibly strip hardware addressing
120 * information from it, and then call do_packet to try to do something with it.
126 struct pollfd fds[2];
131 * Call any expired timeouts, and then if there's still
132 * a timeout registered, time out the select call then.
136 interfaces_invalidated = 0;
141 if (timeouts->when <= cur_time) {
143 timeouts = timeouts->next;
145 t->next = free_timeouts;
151 * Figure timeout in milliseconds, and check for
152 * potential overflow, so we can cram into an
153 * int for poll, while not polling with a
154 * negative timeout and blocking indefinitely.
156 howlong = timeouts->when - cur_time;
157 if (howlong > INT_MAX / 1000)
158 howlong = INT_MAX / 1000;
159 to_msec = howlong * 1000;
163 /* Set up the descriptors to be polled. */
164 if (!ifi || ifi->rfdesc == -1)
165 error("No live interface to poll on");
167 fds[0].fd = ifi->rfdesc;
168 fds[1].fd = routefd; /* Could be -1, which will be ignored. */
169 fds[0].events = fds[1].events = POLLIN;
171 /* Wait for a packet or a timeout... XXX */
172 count = poll(fds, 2, to_msec);
174 /* Get the current time... */
177 /* Not likely to be transitory... */
179 if (errno == EAGAIN || errno == EINTR) {
185 if ((fds[0].revents & (POLLIN | POLLHUP))) {
187 ifi && ifi->rfdesc != -1)
190 if ((fds[1].revents & (POLLIN | POLLHUP))) {
191 if (ifi && !interfaces_invalidated)
195 interfaces_invalidated = 0;
202 struct sockaddr_in from;
203 struct hardware hfrom;
207 if ((result = receive_packet(&from, &hfrom)) == -1) {
208 warning("receive_packet failed on %s: %s", ifi->name,
211 if ((!interface_status(ifi->name)) ||
212 (ifi->noifmedia && ifi->errors > 20)) {
213 /* our interface has gone away. */
214 warning("Interface %s no longer appears valid.",
216 interfaces_invalidated = 1;
226 memcpy(ifrom.iabuf, &from.sin_addr, ifrom.len);
228 do_packet(result, from.sin_port, ifrom, &hfrom);
232 interface_link_forceup(char *ifname)
237 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
238 error("Can't create socket");
240 memset(&ifr, 0, sizeof(ifr));
241 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
242 if (ioctl(sock, SIOCGIFFLAGS, (caddr_t)&ifr) == -1) {
247 if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
248 ifr.ifr_flags |= IFF_UP;
249 if (ioctl(sock, SIOCSIFFLAGS, (caddr_t)&ifr) == -1) {
261 interface_link_forcedown(char *ifname)
266 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
267 error("Can't create socket");
269 memset(&ifr, 0, sizeof(ifr));
270 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
271 if (ioctl(sock, SIOCGIFFLAGS, (caddr_t)&ifr) == -1) {
276 if ((ifr.ifr_flags & IFF_UP) == IFF_UP) {
277 ifr.ifr_flags &= ~IFF_UP;
278 if (ioctl(sock, SIOCSIFFLAGS, (caddr_t)&ifr) == -1) {
288 interface_status(char *ifname)
291 struct ifmediareq ifmr;
294 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
295 error("Can't create socket");
297 /* get interface flags */
298 memset(&ifr, 0, sizeof(ifr));
299 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
300 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
301 warning("ioctl(SIOCGIFFLAGS) on %s: %m", ifname);
306 * if one of UP and RUNNING flags is dropped,
307 * the interface is not active.
309 if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
312 /* Next, check carrier on the interface, if possible */
315 memset(&ifmr, 0, sizeof(ifmr));
316 strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
317 if (ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
319 * EINVAL or ENOTTY simply means that the interface
320 * does not support the SIOCGIFMEDIA ioctl. We regard it alive.
322 if (errno != EINVAL && errno != ENOTTY)
323 debug("ioctl(SIOCGIFMEDIA) on %s: %m", ifname);
328 if (ifmr.ifm_status & IFM_AVALID) {
329 if (ifmr.ifm_status & IFM_ACTIVE)
343 add_timeout(time_t when, void (*where)(void))
345 struct timeout *t, *q;
347 /* See if this timeout supersedes an existing timeout. */
349 for (q = timeouts; q; q = q->next) {
350 if (q->func == where) {
360 /* If we didn't supersede a timeout, allocate a timeout
365 free_timeouts = q->next;
368 q = malloc(sizeof(struct timeout));
370 error("Can't allocate timeout structure!");
377 /* Now sort this timeout into the timeout list. */
379 /* Beginning of list? */
380 if (!timeouts || timeouts->when > q->when) {
386 /* Middle of list? */
387 for (t = timeouts; t->next; t = t->next) {
388 if (t->next->when > q->when) {
401 cancel_timeout(void (*where)(void))
403 struct timeout *t, *q;
405 /* Look for this timeout on the list, and unlink it if we find it. */
407 for (q = timeouts; q; q = q->next) {
408 if (q->func == where) {
418 /* If we found the timeout, put it on the free list. */
420 q->next = free_timeouts;
426 interface_link_status(char *ifname)
428 struct ifmediareq ifmr;
431 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
432 error("Can't create socket");
434 memset(&ifmr, 0, sizeof(ifmr));
435 strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
436 if (ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
437 /* EINVAL/ENOTTY -> link state unknown. treat as active */
438 if (errno != EINVAL && errno != ENOTTY)
439 debug("ioctl(SIOCGIFMEDIA) on %s: %m", ifname);
445 if (ifmr.ifm_status & IFM_AVALID) {
446 if (ifmr.ifm_status & IFM_ACTIVE)