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