1 /* $OpenBSD: dispatch.c,v 1.31 2004/09/21 04:07:03 david Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright 2004 Henning Brauer <henning@openbsd.org>
7 * Copyright (c) 1995, 1996, 1997, 1998, 1999
8 * The Internet Software Consortium. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of The Internet Software Consortium nor the names
20 * of its contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
24 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
31 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * This software has been written for the Internet Software Consortium
38 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
39 * Enterprises. To learn more about the Internet Software Consortium,
40 * see ``http://www.vix.com/isc''. To learn more about Vixie
41 * Enterprises, see ``http://www.vix.com''.
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
50 #include <sys/ioctl.h>
52 #include <net/if_media.h>
56 struct protocol *protocols;
57 struct timeout *timeouts;
58 static struct timeout *free_timeouts;
59 static int interfaces_invalidated;
60 void (*bootp_packet_handler)(struct interface_info *,
61 struct dhcp_packet *, int, unsigned int,
62 struct iaddr, struct hardware *);
64 static int interface_status(struct interface_info *ifinfo);
67 * Use getifaddrs() to get a list of all the attached interfaces. For
68 * each interface that's of type INET and not the loopback interface,
69 * register that interface with the network I/O software, figure out
70 * what subnet it's on, and add it to the list of interfaces.
73 discover_interfaces(struct interface_info *iface)
75 struct ifaddrs *ifap, *ifa;
76 struct sockaddr_in foo;
79 if (getifaddrs(&ifap) != 0)
80 error("getifaddrs failed");
82 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
83 if ((ifa->ifa_flags & IFF_LOOPBACK) ||
84 (ifa->ifa_flags & IFF_POINTOPOINT) ||
85 (!(ifa->ifa_flags & IFF_UP)))
88 if (strcmp(iface->name, ifa->ifa_name))
92 * If we have the capability, extract link information
93 * and record it in a linked list.
95 if (ifa->ifa_addr->sa_family == AF_LINK) {
96 struct sockaddr_dl *foo =
97 (struct sockaddr_dl *)ifa->ifa_addr;
99 iface->index = foo->sdl_index;
100 iface->hw_address.hlen = foo->sdl_alen;
101 iface->hw_address.htype = HTYPE_ETHER; /* XXX */
102 memcpy(iface->hw_address.haddr,
103 LLADDR(foo), foo->sdl_alen);
104 } else if (ifa->ifa_addr->sa_family == AF_INET) {
107 memcpy(&foo, ifa->ifa_addr, sizeof(foo));
108 if (foo.sin_addr.s_addr == htonl(INADDR_LOOPBACK))
111 if ((tif = calloc(1, sizeof(struct ifreq)))
113 error("no space to remember ifp");
114 strlcpy(tif->ifr_name, ifa->ifa_name, IFNAMSIZ);
115 memcpy(&tif->ifr_addr, ifa->ifa_addr,
116 ifa->ifa_addr->sa_len);
118 iface->primary_address = foo.sin_addr;
121 memcpy(addr.iabuf, &foo.sin_addr.s_addr, addr.len);
126 error("%s: not found", iface->name);
128 /* Register the interface... */
129 if_register_receive(iface);
130 if_register_send(iface);
131 add_protocol(iface->name, iface->rfdesc, got_one, iface);
136 reinitialize_interfaces(void)
138 interfaces_invalidated = 1;
142 * Wait for packets to come in using poll(). When a packet comes in,
143 * call receive_packet to receive the packet and possibly strip hardware
144 * addressing information from it, and then call through the
145 * bootp_packet_handler hook to try to do something with it.
150 int count, live_interfaces, i, to_msec, nfds = 0;
155 for (l = protocols; l; l = l->next)
158 fds = malloc(nfds * sizeof(struct pollfd));
160 error("Can't allocate poll structures.");
164 * Call any expired timeouts, and then if there's still
165 * a timeout registered, time out the select call then.
171 if (timeouts->when <= cur_time) {
173 timeouts = timeouts->next;
174 (*(t->func))(t->what);
175 t->next = free_timeouts;
181 * Figure timeout in milliseconds, and check for
182 * potential overflow, so we can cram into an
183 * int for poll, while not polling with a
184 * negative timeout and blocking indefinitely.
186 howlong = timeouts->when - cur_time;
187 if (howlong > INT_MAX / 1000)
188 howlong = INT_MAX / 1000;
189 to_msec = howlong * 1000;
193 /* Set up the descriptors to be polled. */
195 for (i = 0, l = protocols; l; l = l->next) {
196 struct interface_info *ip = l->local;
198 if (ip == NULL || ip->dead)
201 fds[i].events = POLLIN;
204 if (l->handler == got_one)
207 if (live_interfaces == 0)
208 error("No live interfaces to poll on - exiting.");
210 /* Wait for a packet or a timeout... XXX */
211 count = poll(fds, nfds, to_msec);
213 /* Not likely to be transitory... */
215 if (errno == EAGAIN || errno == EINTR) {
222 /* Get the current time... */
226 for (l = protocols; l; l = l->next) {
227 struct interface_info *ip;
229 if ((fds[i].revents & (POLLIN | POLLHUP))) {
231 if (ip && (l->handler != got_one ||
234 if (interfaces_invalidated)
239 interfaces_invalidated = 0;
245 got_one(struct protocol *l)
247 struct sockaddr_in from;
248 struct hardware hfrom;
253 * Packet input buffer. Must be as large as largest
256 unsigned char packbuf[4095];
257 struct dhcp_packet packet;
259 struct interface_info *ip = l->local;
261 if ((result = receive_packet(ip, u.packbuf, sizeof(u), &from,
263 warning("receive_packet failed on %s: %s", ip->name,
266 if ((!interface_status(ip)) ||
267 (ip->noifmedia && ip->errors > 20)) {
268 /* our interface has gone away. */
269 warning("Interface %s no longer appears valid.",
272 interfaces_invalidated = 1;
282 if (bootp_packet_handler) {
284 memcpy(ifrom.iabuf, &from.sin_addr, ifrom.len);
286 (*bootp_packet_handler)(ip, &u.packet, result,
287 from.sin_port, ifrom, &hfrom);
292 interface_status(struct interface_info *ifinfo)
294 char *ifname = ifinfo->name;
295 int ifsock = ifinfo->rfdesc;
297 struct ifmediareq ifmr;
299 /* get interface flags */
300 memset(&ifr, 0, sizeof(ifr));
301 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
302 if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) < 0) {
303 cap_syslog(capsyslog, LOG_ERR, "ioctl(SIOCGIFFLAGS) on %s: %m",
309 * if one of UP and RUNNING flags is dropped,
310 * the interface is not active.
312 if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
315 /* Next, check carrier on the interface, if possible */
316 if (ifinfo->noifmedia)
318 memset(&ifmr, 0, sizeof(ifmr));
319 strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
320 if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
321 if (errno != EINVAL) {
322 cap_syslog(capsyslog, LOG_DEBUG,
323 "ioctl(SIOCGIFMEDIA) on %s: %m", ifname);
324 ifinfo->noifmedia = 1;
328 * EINVAL (or ENOTTY) simply means that the interface
329 * does not support the SIOCGIFMEDIA ioctl. We regard it alive.
331 ifinfo->noifmedia = 1;
334 if (ifmr.ifm_status & IFM_AVALID) {
335 switch (ifmr.ifm_active & IFM_NMASK) {
338 if (ifmr.ifm_status & IFM_ACTIVE)
354 add_timeout(time_t when, void (*where)(void *), void *what)
356 struct timeout *t, *q;
358 /* See if this timeout supersedes an existing timeout. */
360 for (q = timeouts; q; q = q->next) {
361 if (q->func == where && q->what == what) {
371 /* If we didn't supersede a timeout, allocate a timeout
376 free_timeouts = q->next;
380 q = malloc(sizeof(struct timeout));
382 error("Can't allocate timeout structure!");
390 /* Now sort this timeout into the timeout list. */
392 /* Beginning of list? */
393 if (!timeouts || timeouts->when > q->when) {
399 /* Middle of list? */
400 for (t = timeouts; t->next; t = t->next) {
401 if (t->next->when > q->when) {
414 cancel_timeout(void (*where)(void *), void *what)
416 struct timeout *t, *q;
418 /* Look for this timeout on the list, and unlink it if we find it. */
420 for (q = timeouts; q; q = q->next) {
421 if (q->func == where && q->what == what) {
431 /* If we found the timeout, put it on the free list. */
433 q->next = free_timeouts;
438 /* Add a protocol to the list of protocols... */
440 add_protocol(char *name, int fd, void (*handler)(struct protocol *),
445 p = malloc(sizeof(*p));
447 error("can't allocate protocol struct for %s", name);
450 p->handler = handler;
457 remove_protocol(struct protocol *proto)
459 struct protocol *p, *next;
461 for (p = protocols; p; p = next) {
471 interface_link_status(char *ifname)
473 struct ifmediareq ifmr;
476 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
477 error("Can't create socket");
479 memset(&ifmr, 0, sizeof(ifmr));
480 strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
481 if (ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
482 /* EINVAL -> link state unknown. treat as active */
484 cap_syslog(capsyslog, LOG_DEBUG,
485 "ioctl(SIOCGIFMEDIA) on %s: %m", ifname);
491 if (ifmr.ifm_status & IFM_AVALID) {
492 switch (ifmr.ifm_active & IFM_NMASK) {
495 if (ifmr.ifm_status & IFM_ACTIVE)
505 interface_set_mtu_unpriv(int privfd, u_int16_t mtu)
511 hdr.code = IMSG_SET_INTERFACE_MTU;
512 hdr.len = sizeof(hdr) +
515 if ((buf = buf_open(hdr.len)) == NULL)
516 error("buf_open: %m");
518 errs += buf_add(buf, &hdr, sizeof(hdr));
519 errs += buf_add(buf, &mtu, sizeof(mtu));
521 error("buf_add: %m");
523 if (buf_close(privfd, buf) == -1)
524 error("buf_close: %m");
528 interface_set_mtu_priv(char *ifname, u_int16_t mtu)
533 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
534 error("Can't create socket");
536 memset(&ifr, 0, sizeof(ifr));
538 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
541 if (ioctl(sock, SIOCSIFMTU, &ifr) == -1)
542 warning("SIOCSIFMTU failed (%d): %s", mtu,