dhclient - Silence dhclient by immediately exiting if the interface doesn't exist.
[dragonfly.git] / sbin / dhclient / dispatch.c
CommitLineData
9930e9a8 1/* $OpenBSD: src/sbin/dhclient/dispatch.c,v 1.45 2009/11/26 23:14:29 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:
135 if (!ifi->linkstat)
136 interfaces_invalidated = 0;
137
138 if (timeouts) {
139 struct timeout *t;
140
141 if (timeouts->when <= cur_time) {
142 t = timeouts;
143 timeouts = timeouts->next;
144 (*(t->func))();
145 t->next = free_timeouts;
146 free_timeouts = t;
147 goto another;
148 }
149
150 /*
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.
155 */
156 howlong = timeouts->when - cur_time;
157 if (howlong > INT_MAX / 1000)
158 howlong = INT_MAX / 1000;
159 to_msec = howlong * 1000;
160 } else
161 to_msec = -1;
162
163 /* Set up the descriptors to be polled. */
164 if (!ifi || ifi->rfdesc == -1)
165 error("No live interface to poll on");
166
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;
170
171 /* Wait for a packet or a timeout... XXX */
172 count = poll(fds, 2, to_msec);
173
174 /* Get the current time... */
175 time(&cur_time);
176
177 /* Not likely to be transitory... */
178 if (count == -1) {
179 if (errno == EAGAIN || errno == EINTR) {
180 continue;
181 } else
182 error("poll: %m");
183 }
184
185 if ((fds[0].revents & (POLLIN | POLLHUP))) {
bdf60627 186 if (ifi && ifi->linkstat && ifi->rfdesc != -1)
846204b6
HT
187 got_one();
188 }
189 if ((fds[1].revents & (POLLIN | POLLHUP))) {
190 if (ifi && !interfaces_invalidated)
191 routehandler();
192 }
193
194 interfaces_invalidated = 0;
195 } while (1);
196}
197
198void
199got_one(void)
200{
201 struct sockaddr_in from;
202 struct hardware hfrom;
203 struct iaddr ifrom;
204 ssize_t result;
205
206 if ((result = receive_packet(&from, &hfrom)) == -1) {
207 warning("receive_packet failed on %s: %s", ifi->name,
208 strerror(errno));
209 ifi->errors++;
210 if ((!interface_status(ifi->name)) ||
211 (ifi->noifmedia && ifi->errors > 20)) {
212 /* our interface has gone away. */
9930e9a8 213 error("Interface %s no longer appears valid.",
846204b6 214 ifi->name);
846204b6
HT
215 }
216 return;
217 }
218 if (result == 0)
219 return;
220
221 ifrom.len = 4;
222 memcpy(ifrom.iabuf, &from.sin_addr, ifrom.len);
223
224 do_packet(result, from.sin_port, ifrom, &hfrom);
225}
226
227int
228interface_link_forceup(char *ifname)
229{
230 struct ifreq ifr;
231 int sock;
232
233 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
234 error("Can't create socket");
235
236 memset(&ifr, 0, sizeof(ifr));
237 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
238 if (ioctl(sock, SIOCGIFFLAGS, (caddr_t)&ifr) == -1) {
239 close(sock);
240 return (-1);
241 }
242
243 if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
244 ifr.ifr_flags |= IFF_UP;
245 if (ioctl(sock, SIOCSIFFLAGS, (caddr_t)&ifr) == -1) {
246 close(sock);
247 return (-1);
248 }
249 close(sock);
250 return (0);
251 }
252 close(sock);
253 return (1);
254}
255
256void
257interface_link_forcedown(char *ifname)
258{
259 struct ifreq ifr;
260 int sock;
261
262 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
263 error("Can't create socket");
264
265 memset(&ifr, 0, sizeof(ifr));
266 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
267 if (ioctl(sock, SIOCGIFFLAGS, (caddr_t)&ifr) == -1) {
268 close(sock);
269 return;
270 }
271
272 if ((ifr.ifr_flags & IFF_UP) == IFF_UP) {
273 ifr.ifr_flags &= ~IFF_UP;
274 if (ioctl(sock, SIOCSIFFLAGS, (caddr_t)&ifr) == -1) {
275 close(sock);
276 return;
277 }
278 }
279
280 close(sock);
281}
282
283int
284interface_status(char *ifname)
285{
286 struct ifreq ifr;
287 struct ifmediareq ifmr;
288 int sock;
289
290 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
291 error("Can't create socket");
292
293 /* get interface flags */
294 memset(&ifr, 0, sizeof(ifr));
295 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
296 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
9930e9a8 297 error("ioctl(SIOCGIFFLAGS) on %s: %m", ifname);
846204b6
HT
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(sock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
313 /*
314 * EINVAL or ENOTTY simply means that the interface
315 * does not support the SIOCGIFMEDIA ioctl. We regard it alive.
316 */
4cada237 317#ifdef DEBUG
846204b6
HT
318 if (errno != EINVAL && errno != ENOTTY)
319 debug("ioctl(SIOCGIFMEDIA) on %s: %m", ifname);
4cada237 320#endif
846204b6
HT
321 ifi->noifmedia = 1;
322 goto active;
323 }
324 if (ifmr.ifm_status & IFM_AVALID) {
325 if (ifmr.ifm_status & IFM_ACTIVE)
326 goto active;
327 else
328 goto inactive;
329 }
330inactive:
331 close(sock);
332 return (0);
333active:
334 close(sock);
335 return (1);
336}
337
338void
339add_timeout(time_t when, void (*where)(void))
340{
341 struct timeout *t, *q;
342
343 /* See if this timeout supersedes an existing timeout. */
344 t = NULL;
345 for (q = timeouts; q; q = q->next) {
346 if (q->func == where) {
347 if (t)
348 t->next = q->next;
349 else
350 timeouts = q->next;
351 break;
352 }
353 t = q;
354 }
355
356 /* If we didn't supersede a timeout, allocate a timeout
357 structure now. */
358 if (!q) {
359 if (free_timeouts) {
360 q = free_timeouts;
361 free_timeouts = q->next;
362 q->func = where;
363 } else {
364 q = malloc(sizeof(struct timeout));
365 if (!q)
366 error("Can't allocate timeout structure!");
367 q->func = where;
368 }
369 }
370
371 q->when = when;
372
373 /* Now sort this timeout into the timeout list. */
374
375 /* Beginning of list? */
376 if (!timeouts || timeouts->when > q->when) {
377 q->next = timeouts;
378 timeouts = q;
379 return;
380 }
381
382 /* Middle of list? */
383 for (t = timeouts; t->next; t = t->next) {
384 if (t->next->when > q->when) {
385 q->next = t->next;
386 t->next = q;
387 return;
388 }
389 }
390
391 /* End of list. */
392 t->next = q;
393 q->next = NULL;
394}
395
396void
397cancel_timeout(void (*where)(void))
398{
399 struct timeout *t, *q;
400
401 /* Look for this timeout on the list, and unlink it if we find it. */
402 t = NULL;
403 for (q = timeouts; q; q = q->next) {
404 if (q->func == where) {
405 if (t)
406 t->next = q->next;
407 else
408 timeouts = q->next;
409 break;
410 }
411 t = q;
412 }
413
414 /* If we found the timeout, put it on the free list. */
415 if (q) {
416 q->next = free_timeouts;
417 free_timeouts = q;
418 }
419}
420
421int
422interface_link_status(char *ifname)
423{
424 struct ifmediareq ifmr;
425 int sock;
426
427 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
428 error("Can't create socket");
429
430 memset(&ifmr, 0, sizeof(ifmr));
431 strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
432 if (ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
433 /* EINVAL/ENOTTY -> link state unknown. treat as active */
4cada237 434#ifdef DEBUG
846204b6
HT
435 if (errno != EINVAL && errno != ENOTTY)
436 debug("ioctl(SIOCGIFMEDIA) on %s: %m", ifname);
4cada237 437#endif
846204b6
HT
438 close(sock);
439 return (1);
440 }
441 close(sock);
442
443 if (ifmr.ifm_status & IFM_AVALID) {
444 if (ifmr.ifm_status & IFM_ACTIVE)
445 return (1);
446 else
447 return (0);
448 }
449 return (1);
450}