1 /* $OpenBSD: dispatch.c,v 1.31 2004/09/21 04:07:03 david 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/cdefs.h>
43 __FBSDID("$FreeBSD$");
47 #include <sys/ioctl.h>
49 #include <net/if_media.h>
53 struct protocol *protocols;
54 struct timeout *timeouts;
55 static struct timeout *free_timeouts;
56 static int interfaces_invalidated;
57 void (*bootp_packet_handler)(struct interface_info *,
58 struct dhcp_packet *, int, unsigned int,
59 struct iaddr, struct hardware *);
61 static int interface_status(struct interface_info *ifinfo);
64 * Use getifaddrs() to get a list of all the attached interfaces. For
65 * each interface that's of type INET and not the loopback interface,
66 * register that interface with the network I/O software, figure out
67 * what subnet it's on, and add it to the list of interfaces.
70 discover_interfaces(struct interface_info *iface)
72 struct ifaddrs *ifap, *ifa;
73 struct sockaddr_in foo;
76 if (getifaddrs(&ifap) != 0)
77 error("getifaddrs failed");
79 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
80 if ((ifa->ifa_flags & IFF_LOOPBACK) ||
81 (ifa->ifa_flags & IFF_POINTOPOINT) ||
82 (!(ifa->ifa_flags & IFF_UP)))
85 if (strcmp(iface->name, ifa->ifa_name))
89 * If we have the capability, extract link information
90 * and record it in a linked list.
92 if (ifa->ifa_addr->sa_family == AF_LINK) {
93 struct sockaddr_dl *foo =
94 (struct sockaddr_dl *)ifa->ifa_addr;
96 iface->index = foo->sdl_index;
97 iface->hw_address.hlen = foo->sdl_alen;
98 iface->hw_address.htype = HTYPE_ETHER; /* XXX */
99 memcpy(iface->hw_address.haddr,
100 LLADDR(foo), foo->sdl_alen);
101 } else if (ifa->ifa_addr->sa_family == AF_INET) {
104 memcpy(&foo, ifa->ifa_addr, sizeof(foo));
105 if (foo.sin_addr.s_addr == htonl(INADDR_LOOPBACK))
108 if ((tif = calloc(1, sizeof(struct ifreq)))
110 error("no space to remember ifp");
111 strlcpy(tif->ifr_name, ifa->ifa_name, IFNAMSIZ);
112 memcpy(&tif->ifr_addr, ifa->ifa_addr,
113 ifa->ifa_addr->sa_len);
115 iface->primary_address = foo.sin_addr;
118 memcpy(addr.iabuf, &foo.sin_addr.s_addr, addr.len);
123 error("%s: not found", iface->name);
125 /* Register the interface... */
126 if_register_receive(iface);
127 if_register_send(iface);
128 add_protocol(iface->name, iface->rfdesc, got_one, iface);
133 reinitialize_interfaces(void)
135 interfaces_invalidated = 1;
139 * Wait for packets to come in using poll(). When a packet comes in,
140 * call receive_packet to receive the packet and possibly strip hardware
141 * addressing information from it, and then call through the
142 * bootp_packet_handler hook to try to do something with it.
147 int count, live_interfaces, i, to_msec, nfds = 0;
152 for (l = protocols; l; l = l->next)
155 fds = malloc(nfds * sizeof(struct pollfd));
157 error("Can't allocate poll structures.");
161 * Call any expired timeouts, and then if there's still
162 * a timeout registered, time out the select call then.
168 if (timeouts->when <= cur_time) {
170 timeouts = timeouts->next;
171 (*(t->func))(t->what);
172 t->next = free_timeouts;
178 * Figure timeout in milliseconds, and check for
179 * potential overflow, so we can cram into an
180 * int for poll, while not polling with a
181 * negative timeout and blocking indefinitely.
183 howlong = timeouts->when - cur_time;
184 if (howlong > INT_MAX / 1000)
185 howlong = INT_MAX / 1000;
186 to_msec = howlong * 1000;
190 /* Set up the descriptors to be polled. */
192 for (i = 0, l = protocols; l; l = l->next) {
193 struct interface_info *ip = l->local;
195 if (ip == NULL || ip->dead)
198 fds[i].events = POLLIN;
201 if (l->handler == got_one)
204 if (live_interfaces == 0)
205 error("No live interfaces to poll on - exiting.");
207 /* Wait for a packet or a timeout... XXX */
208 count = poll(fds, nfds, to_msec);
210 /* Not likely to be transitory... */
212 if (errno == EAGAIN || errno == EINTR) {
219 /* Get the current time... */
223 for (l = protocols; l; l = l->next) {
224 struct interface_info *ip;
226 if ((fds[i].revents & (POLLIN | POLLHUP))) {
228 if (ip && (l->handler != got_one ||
231 if (interfaces_invalidated)
236 interfaces_invalidated = 0;
242 got_one(struct protocol *l)
244 struct sockaddr_in from;
245 struct hardware hfrom;
250 * Packet input buffer. Must be as large as largest
253 unsigned char packbuf[4095];
254 struct dhcp_packet packet;
256 struct interface_info *ip = l->local;
258 if ((result = receive_packet(ip, u.packbuf, sizeof(u), &from,
260 warning("receive_packet failed on %s: %s", ip->name,
263 if ((!interface_status(ip)) ||
264 (ip->noifmedia && ip->errors > 20)) {
265 /* our interface has gone away. */
266 warning("Interface %s no longer appears valid.",
269 interfaces_invalidated = 1;
279 if (bootp_packet_handler) {
281 memcpy(ifrom.iabuf, &from.sin_addr, ifrom.len);
283 (*bootp_packet_handler)(ip, &u.packet, result,
284 from.sin_port, ifrom, &hfrom);
289 interface_status(struct interface_info *ifinfo)
291 char *ifname = ifinfo->name;
292 int ifsock = ifinfo->rfdesc;
294 struct ifmediareq ifmr;
296 /* get interface flags */
297 memset(&ifr, 0, sizeof(ifr));
298 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
299 if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) < 0) {
300 syslog(LOG_ERR, "ioctl(SIOCGIFFLAGS) on %s: %m", ifname);
305 * if one of UP and RUNNING flags is dropped,
306 * the interface is not active.
308 if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
311 /* Next, check carrier on the interface, if possible */
312 if (ifinfo->noifmedia)
314 memset(&ifmr, 0, sizeof(ifmr));
315 strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
316 if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
317 if (errno != EINVAL) {
318 syslog(LOG_DEBUG, "ioctl(SIOCGIFMEDIA) on %s: %m",
321 ifinfo->noifmedia = 1;
325 * EINVAL (or ENOTTY) simply means that the interface
326 * does not support the SIOCGIFMEDIA ioctl. We regard it alive.
328 ifinfo->noifmedia = 1;
331 if (ifmr.ifm_status & IFM_AVALID) {
332 switch (ifmr.ifm_active & IFM_NMASK) {
335 if (ifmr.ifm_status & IFM_ACTIVE)
351 add_timeout(time_t when, void (*where)(void *), void *what)
353 struct timeout *t, *q;
355 /* See if this timeout supersedes an existing timeout. */
357 for (q = timeouts; q; q = q->next) {
358 if (q->func == where && q->what == what) {
368 /* If we didn't supersede a timeout, allocate a timeout
373 free_timeouts = q->next;
377 q = malloc(sizeof(struct timeout));
379 error("Can't allocate timeout structure!");
387 /* Now sort this timeout into the timeout list. */
389 /* Beginning of list? */
390 if (!timeouts || timeouts->when > q->when) {
396 /* Middle of list? */
397 for (t = timeouts; t->next; t = t->next) {
398 if (t->next->when > q->when) {
411 cancel_timeout(void (*where)(void *), void *what)
413 struct timeout *t, *q;
415 /* Look for this timeout on the list, and unlink it if we find it. */
417 for (q = timeouts; q; q = q->next) {
418 if (q->func == where && q->what == what) {
428 /* If we found the timeout, put it on the free list. */
430 q->next = free_timeouts;
435 /* Add a protocol to the list of protocols... */
437 add_protocol(char *name, int fd, void (*handler)(struct protocol *),
442 p = malloc(sizeof(*p));
444 error("can't allocate protocol struct for %s", name);
447 p->handler = handler;
454 remove_protocol(struct protocol *proto)
456 struct protocol *p, *next, *prev;
459 for (p = protocols; p; p = next) {
463 prev->next = p->next;
472 interface_link_status(char *ifname)
474 struct ifmediareq ifmr;
477 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
478 error("Can't create socket");
480 memset(&ifmr, 0, sizeof(ifmr));
481 strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
482 if (ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
483 /* EINVAL -> link state unknown. treat as active */
485 syslog(LOG_DEBUG, "ioctl(SIOCGIFMEDIA) on %s: %m",
492 if (ifmr.ifm_status & IFM_AVALID) {
493 switch (ifmr.ifm_active & IFM_NMASK) {
496 if (ifmr.ifm_status & IFM_ACTIVE)