dhclient - Assume link is up when IFM_AVALID isn't set.
[dragonfly.git] / sbin / dhclient / dispatch.c
... / ...
CommitLineData
1/* $OpenBSD: src/sbin/dhclient/dispatch.c,v 1.53 2012/07/26 18:42:58 krw 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 <sys/ioctl.h>
43
44#include <net/if_media.h>
45
46#include <ifaddrs.h>
47#include <poll.h>
48
49#include "dhcpd.h"
50
51struct timeout timeout;
52static int interfaces_invalidated;
53
54/*
55 * Use getifaddrs() to get a list of all the attached interfaces. For
56 * each interface that's of type INET and not the loopback interface,
57 * register that interface with the network I/O software, figure out
58 * what subnet it's on, and add it to the list of interfaces.
59 */
60void
61discover_interface(void)
62{
63 struct ifaddrs *ifap, *ifa;
64 struct ifreq *tif;
65 int len = IFNAMSIZ + sizeof(struct sockaddr_storage);
66
67 if (getifaddrs(&ifap) != 0)
68 error("getifaddrs failed");
69
70 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
71 if ((ifa->ifa_flags & IFF_LOOPBACK) ||
72 (ifa->ifa_flags & IFF_POINTOPOINT) ||
73 (!(ifa->ifa_flags & IFF_UP)))
74 continue;
75
76 if (strcmp(ifi->name, ifa->ifa_name))
77 continue;
78
79 /*
80 * If we have the capability, extract link information
81 * and record it in a linked list.
82 */
83 if (ifa->ifa_addr->sa_family == AF_LINK) {
84 struct sockaddr_dl *foo =
85 (struct sockaddr_dl *)ifa->ifa_addr;
86
87 ifi->index = foo->sdl_index;
88 ifi->hw_address.hlen = foo->sdl_alen;
89 ifi->hw_address.htype = HTYPE_ETHER; /* XXX */
90 memcpy(ifi->hw_address.haddr,
91 LLADDR(foo), foo->sdl_alen);
92 }
93 if (!ifi->ifp) {
94 if ((tif = malloc(len)) == NULL)
95 error("no space to remember ifp");
96 strlcpy(tif->ifr_name, ifa->ifa_name, IFNAMSIZ);
97 ifi->ifp = tif;
98 }
99 }
100
101 if (!ifi->ifp)
102 error("%s: not found", ifi->name);
103
104 /* Register the interface... */
105 if_register_receive();
106 if_register_send();
107 freeifaddrs(ifap);
108}
109
110void
111reinitialize_interface(void)
112{
113 interfaces_invalidated = 1;
114}
115
116/*
117 * Wait for packets to come in using poll(). When a packet comes in, call
118 * receive_packet to receive the packet and possibly strip hardware addressing
119 * information from it, and then call do_packet to try to do something with it.
120 */
121void
122dispatch(void)
123{
124 int count, to_msec;
125 struct pollfd fds[2];
126 time_t howlong;
127 void (*func)(void);
128
129 do {
130 /*
131 * Call expired timeout, and then if there's still
132 * a timeout registered, time out the select call then.
133 */
134another:
135 if (!ifi)
136 error("No interfaces available");
137
138 if (!ifi->linkstat)
139 interfaces_invalidated = 0;
140
141 if (timeout.func) {
142 time(&cur_time);
143 if (timeout.when <= cur_time) {
144 func = timeout.func;
145 cancel_timeout();
146 (*(func))();
147 goto another;
148 }
149 /*
150 * Figure timeout in milliseconds, and check for
151 * potential overflow, so we can cram into an
152 * int for poll, while not polling with a
153 * negative timeout and blocking indefinitely.
154 */
155 howlong = timeout.when - cur_time;
156 if (howlong > INT_MAX / 1000)
157 howlong = INT_MAX / 1000;
158 to_msec = howlong * 1000;
159 } else
160 to_msec = -1;
161
162 /* Set up the descriptors to be polled. */
163 if (!ifi || ifi->rfdesc == -1)
164 error("No live interface to poll on");
165
166 fds[0].fd = ifi->rfdesc;
167 fds[1].fd = routefd; /* Could be -1, which will be ignored. */
168 fds[0].events = fds[1].events = POLLIN;
169
170 /* Wait for a packet or a timeout... XXX */
171 count = poll(fds, 2, to_msec);
172
173 /* Not likely to be transitory... */
174 if (count == -1) {
175 if (errno == EAGAIN || errno == EINTR) {
176 continue;
177 } else
178 error("poll: %m");
179 }
180
181 if ((fds[0].revents & (POLLIN | POLLHUP))) {
182 if (ifi && ifi->linkstat && ifi->rfdesc != -1)
183 got_one();
184 }
185 if ((fds[1].revents & (POLLIN | POLLHUP))) {
186 if (ifi && !interfaces_invalidated)
187 routehandler();
188 }
189
190 interfaces_invalidated = 0;
191 } while (1);
192}
193
194void
195got_one(void)
196{
197 struct sockaddr_in from;
198 struct hardware hfrom;
199 struct iaddr ifrom;
200 ssize_t result;
201
202 if ((result = receive_packet(&from, &hfrom)) == -1) {
203 warning("receive_packet failed on %s: %s", ifi->name,
204 strerror(errno));
205 ifi->errors++;
206 if ((!interface_status(ifi->name)) ||
207 (ifi->noifmedia && ifi->errors > 20)) {
208 /* our interface has gone away. */
209 error("Interface %s no longer appears valid.",
210 ifi->name);
211 }
212 return;
213 }
214 if (result == 0)
215 return;
216
217 ifrom.len = 4;
218 memcpy(ifrom.iabuf, &from.sin_addr, ifrom.len);
219
220 do_packet(result, from.sin_port, ifrom, &hfrom);
221}
222
223int
224interface_link_forceup(char *ifname)
225{
226 struct ifreq ifr;
227 int sock;
228
229 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
230 error("Can't create socket");
231
232 memset(&ifr, 0, sizeof(ifr));
233 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
234 if (ioctl(sock, SIOCGIFFLAGS, (caddr_t)&ifr) == -1) {
235 close(sock);
236 return (-1);
237 }
238
239 if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
240 ifr.ifr_flags |= IFF_UP;
241 if (ioctl(sock, SIOCSIFFLAGS, (caddr_t)&ifr) == -1) {
242 close(sock);
243 return (-1);
244 }
245 close(sock);
246 return (0);
247 }
248 close(sock);
249 return (1);
250}
251
252int
253interface_status(char *ifname)
254{
255 struct ifreq ifr;
256 struct ifmediareq ifmr;
257 int sock;
258
259 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
260 error("Can't create socket");
261
262 /* get interface flags */
263 memset(&ifr, 0, sizeof(ifr));
264 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
265 if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1) {
266 error("ioctl(SIOCGIFFLAGS) on %s: %m", ifname);
267 }
268
269 /*
270 * if one of UP and RUNNING flags is dropped,
271 * the interface is not active.
272 */
273 if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
274 goto inactive;
275
276 /* Next, check carrier on the interface, if possible */
277 if (ifi->noifmedia)
278 goto active;
279 memset(&ifmr, 0, sizeof(ifmr));
280 strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
281 if (ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
282 /*
283 * EINVAL or ENOTTY simply means that the interface
284 * does not support the SIOCGIFMEDIA ioctl. We regard it alive.
285 */
286#ifdef DEBUG
287 if (errno != EINVAL && errno != ENOTTY)
288 debug("ioctl(SIOCGIFMEDIA) on %s: %m", ifname);
289#endif
290 ifi->noifmedia = 1;
291 goto active;
292 }
293 if (ifmr.ifm_status & IFM_AVALID) {
294 if (ifmr.ifm_status & IFM_ACTIVE)
295 goto active;
296 else
297 goto inactive;
298 }
299
300 /* Assume 'active' if IFM_AVALID is not set. */
301
302active:
303 close(sock);
304 return (1);
305inactive:
306 close(sock);
307 return (0);
308}
309
310void
311set_timeout(time_t when, void (*where)(void))
312{
313 timeout.when = when;
314 timeout.func = where;
315}
316
317void
318cancel_timeout(void)
319{
320 timeout.when = 0;
321 timeout.func = NULL;
322}