dhclient - Nuke interface_link_status().
[dragonfly.git] / sbin / dhclient / dispatch.c
CommitLineData
80d26e48 1/* $OpenBSD: src/sbin/dhclient/dispatch.c,v 1.51 2012/06/24 16:01:18 krw Exp $ */
846204b6
HT
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 *timeouts;
52static struct timeout *free_timeouts;
53static int interfaces_invalidated;
54
55/*
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.
60 */
61void
62discover_interface(void)
63{
64 struct ifaddrs *ifap, *ifa;
65 struct ifreq *tif;
66 int len = IFNAMSIZ + sizeof(struct sockaddr_storage);
67
68 if (getifaddrs(&ifap) != 0)
69 error("getifaddrs failed");
70
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)))
75 continue;
76
77 if (strcmp(ifi->name, ifa->ifa_name))
78 continue;
79
80 /*
81 * If we have the capability, extract link information
82 * and record it in a linked list.
83 */
84 if (ifa->ifa_addr->sa_family == AF_LINK) {
85 struct sockaddr_dl *foo =
86 (struct sockaddr_dl *)ifa->ifa_addr;
87
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);
93 }
94 if (!ifi->ifp) {
95 if ((tif = malloc(len)) == NULL)
96 error("no space to remember ifp");
97 strlcpy(tif->ifr_name, ifa->ifa_name, IFNAMSIZ);
98 ifi->ifp = tif;
99 }
100 }
101
102 if (!ifi->ifp)
103 error("%s: not found", ifi->name);
104
105 /* Register the interface... */
106 if_register_receive();
107 if_register_send();
108 freeifaddrs(ifap);
109}
110
111void
112reinitialize_interface(void)
113{
114 interfaces_invalidated = 1;
115}
116
117/*
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.
121 */
122void
123dispatch(void)
124{
125 int count, to_msec;
126 struct pollfd fds[2];
127 time_t howlong;
128
129 do {
130 /*
131 * Call any expired timeouts, and then if there's still
132 * a timeout registered, time out the select call then.
133 */
134another:
d19bab2e
AHJ
135 if (!ifi)
136 error("No interfaces available");
137
846204b6
HT
138 if (!ifi->linkstat)
139 interfaces_invalidated = 0;
140
141 if (timeouts) {
142 struct timeout *t;
143
144 if (timeouts->when <= cur_time) {
145 t = timeouts;
146 timeouts = timeouts->next;
147 (*(t->func))();
148 t->next = free_timeouts;
149 free_timeouts = t;
150 goto another;
151 }
152
153 /*
154 * Figure timeout in milliseconds, and check for
155 * potential overflow, so we can cram into an
156 * int for poll, while not polling with a
157 * negative timeout and blocking indefinitely.
158 */
159 howlong = timeouts->when - cur_time;
160 if (howlong > INT_MAX / 1000)
161 howlong = INT_MAX / 1000;
162 to_msec = howlong * 1000;
163 } else
164 to_msec = -1;
165
166 /* Set up the descriptors to be polled. */
167 if (!ifi || ifi->rfdesc == -1)
168 error("No live interface to poll on");
169
170 fds[0].fd = ifi->rfdesc;
171 fds[1].fd = routefd; /* Could be -1, which will be ignored. */
172 fds[0].events = fds[1].events = POLLIN;
173
174 /* Wait for a packet or a timeout... XXX */
175 count = poll(fds, 2, to_msec);
176
177 /* Get the current time... */
178 time(&cur_time);
179
180 /* Not likely to be transitory... */
181 if (count == -1) {
182 if (errno == EAGAIN || errno == EINTR) {
183 continue;
184 } else
185 error("poll: %m");
186 }
187
188 if ((fds[0].revents & (POLLIN | POLLHUP))) {
bdf60627 189 if (ifi && ifi->linkstat && ifi->rfdesc != -1)
846204b6
HT
190 got_one();
191 }
192 if ((fds[1].revents & (POLLIN | POLLHUP))) {
193 if (ifi && !interfaces_invalidated)
194 routehandler();
195 }
196
197 interfaces_invalidated = 0;
198 } while (1);
199}
200
201void
202got_one(void)
203{
204 struct sockaddr_in from;
205 struct hardware hfrom;
206 struct iaddr ifrom;
207 ssize_t result;
208
209 if ((result = receive_packet(&from, &hfrom)) == -1) {
210 warning("receive_packet failed on %s: %s", ifi->name,
211 strerror(errno));
212 ifi->errors++;
213 if ((!interface_status(ifi->name)) ||
214 (ifi->noifmedia && ifi->errors > 20)) {
215 /* our interface has gone away. */
9930e9a8 216 error("Interface %s no longer appears valid.",
846204b6 217 ifi->name);
846204b6
HT
218 }
219 return;
220 }
221 if (result == 0)
222 return;
223
224 ifrom.len = 4;
225 memcpy(ifrom.iabuf, &from.sin_addr, ifrom.len);
226
227 do_packet(result, from.sin_port, ifrom, &hfrom);
228}
229
230int
231interface_link_forceup(char *ifname)
232{
233 struct ifreq ifr;
234 int sock;
235
236 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
237 error("Can't create socket");
238
239 memset(&ifr, 0, sizeof(ifr));
240 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
241 if (ioctl(sock, SIOCGIFFLAGS, (caddr_t)&ifr) == -1) {
242 close(sock);
243 return (-1);
244 }
245
246 if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
247 ifr.ifr_flags |= IFF_UP;
248 if (ioctl(sock, SIOCSIFFLAGS, (caddr_t)&ifr) == -1) {
249 close(sock);
250 return (-1);
251 }
252 close(sock);
253 return (0);
254 }
255 close(sock);
256 return (1);
257}
258
846204b6
HT
259int
260interface_status(char *ifname)
261{
262 struct ifreq ifr;
263 struct ifmediareq ifmr;
264 int sock;
265
266 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
267 error("Can't create socket");
268
269 /* get interface flags */
270 memset(&ifr, 0, sizeof(ifr));
271 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
f6c633e7 272 if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1) {
9930e9a8 273 error("ioctl(SIOCGIFFLAGS) on %s: %m", ifname);
846204b6
HT
274 }
275
276 /*
277 * if one of UP and RUNNING flags is dropped,
278 * the interface is not active.
279 */
280 if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
281 goto inactive;
282
283 /* Next, check carrier on the interface, if possible */
284 if (ifi->noifmedia)
285 goto active;
286 memset(&ifmr, 0, sizeof(ifmr));
287 strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
f6c633e7 288 if (ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
846204b6
HT
289 /*
290 * EINVAL or ENOTTY simply means that the interface
291 * does not support the SIOCGIFMEDIA ioctl. We regard it alive.
292 */
4cada237 293#ifdef DEBUG
846204b6
HT
294 if (errno != EINVAL && errno != ENOTTY)
295 debug("ioctl(SIOCGIFMEDIA) on %s: %m", ifname);
4cada237 296#endif
846204b6
HT
297 ifi->noifmedia = 1;
298 goto active;
299 }
300 if (ifmr.ifm_status & IFM_AVALID) {
301 if (ifmr.ifm_status & IFM_ACTIVE)
302 goto active;
303 else
304 goto inactive;
305 }
306inactive:
307 close(sock);
308 return (0);
309active:
310 close(sock);
311 return (1);
312}
313
314void
315add_timeout(time_t when, void (*where)(void))
316{
317 struct timeout *t, *q;
318
319 /* See if this timeout supersedes an existing timeout. */
320 t = NULL;
321 for (q = timeouts; q; q = q->next) {
322 if (q->func == where) {
323 if (t)
324 t->next = q->next;
325 else
326 timeouts = q->next;
327 break;
328 }
329 t = q;
330 }
331
332 /* If we didn't supersede a timeout, allocate a timeout
333 structure now. */
334 if (!q) {
335 if (free_timeouts) {
336 q = free_timeouts;
337 free_timeouts = q->next;
338 q->func = where;
339 } else {
340 q = malloc(sizeof(struct timeout));
341 if (!q)
342 error("Can't allocate timeout structure!");
343 q->func = where;
344 }
345 }
346
347 q->when = when;
348
349 /* Now sort this timeout into the timeout list. */
350
351 /* Beginning of list? */
352 if (!timeouts || timeouts->when > q->when) {
353 q->next = timeouts;
354 timeouts = q;
355 return;
356 }
357
358 /* Middle of list? */
359 for (t = timeouts; t->next; t = t->next) {
360 if (t->next->when > q->when) {
361 q->next = t->next;
362 t->next = q;
363 return;
364 }
365 }
366
367 /* End of list. */
368 t->next = q;
369 q->next = NULL;
370}
371
372void
373cancel_timeout(void (*where)(void))
374{
375 struct timeout *t, *q;
376
377 /* Look for this timeout on the list, and unlink it if we find it. */
378 t = NULL;
379 for (q = timeouts; q; q = q->next) {
380 if (q->func == where) {
381 if (t)
382 t->next = q->next;
383 else
384 timeouts = q->next;
385 break;
386 }
387 t = q;
388 }
389
390 /* If we found the timeout, put it on the free list. */
391 if (q) {
392 q->next = free_timeouts;
393 free_timeouts = q;
394 }
395}