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>
53 #include <net/if_media.h>
57 /* Assert that pointer p is aligned to at least align bytes */
58 #define assert_aligned(p, align) assert((((uintptr_t)p) & ((align) - 1)) == 0)
60 static struct protocol *protocols;
61 static struct timeout *timeouts;
62 static struct timeout *free_timeouts;
63 static int interfaces_invalidated;
64 void (*bootp_packet_handler)(struct interface_info *,
65 struct dhcp_packet *, int, unsigned int,
66 struct iaddr, struct hardware *);
68 static int interface_status(struct interface_info *ifinfo);
71 * Use getifaddrs() to get a list of all the attached interfaces. For
72 * each interface that's of type INET and not the loopback interface,
73 * register that interface with the network I/O software, figure out
74 * what subnet it's on, and add it to the list of interfaces.
77 discover_interfaces(struct interface_info *iface)
79 struct ifaddrs *ifap, *ifa;
81 int len = IFNAMSIZ + sizeof(struct sockaddr_storage);
83 if (getifaddrs(&ifap) != 0)
84 error("getifaddrs failed");
86 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
87 if ((ifa->ifa_flags & IFF_LOOPBACK) ||
88 (ifa->ifa_flags & IFF_POINTOPOINT) ||
89 (!(ifa->ifa_flags & IFF_UP)))
92 if (strcmp(iface->name, ifa->ifa_name))
96 * If we have the capability, extract link information
97 * and record it in a linked list.
99 if (ifa->ifa_addr->sa_family == AF_LINK) {
100 struct sockaddr_dl *foo;
103 * The implementation of getifaddrs should guarantee
106 assert_aligned(ifa->ifa_addr,
107 _Alignof(struct sockaddr_dl));
109 #pragma clang diagnostic push
110 #pragma clang diagnostic ignored "-Wcast-align"
112 foo = (struct sockaddr_dl *)ifa->ifa_addr;
114 #pragma clang diagnostic pop
117 iface->index = foo->sdl_index;
118 iface->hw_address.hlen = foo->sdl_alen;
119 iface->hw_address.htype = HTYPE_ETHER; /* XXX */
120 memcpy(iface->hw_address.haddr,
121 LLADDR(foo), foo->sdl_alen);
124 if ((tif = calloc(1, len)) == NULL)
125 error("no space to remember ifp");
126 strlcpy(tif->ifr_name, ifa->ifa_name, IFNAMSIZ);
133 error("%s: not found", iface->name);
135 /* Register the interface... */
136 if_register_receive(iface);
137 if_register_send(iface);
138 add_protocol(iface->name, iface->rfdesc, got_one, iface);
143 reinitialize_interfaces(void)
145 interfaces_invalidated = 1;
149 * Wait for packets to come in using poll(). When a packet comes in,
150 * call receive_packet to receive the packet and possibly strip hardware
151 * addressing information from it, and then call through the
152 * bootp_packet_handler hook to try to do something with it.
157 int count, live_interfaces, i, to_msec, nfds = 0;
162 for (l = protocols; l; l = l->next)
165 fds = malloc(nfds * sizeof(struct pollfd));
167 error("Can't allocate poll structures.");
171 * Call any expired timeouts, and then if there's still
172 * a timeout registered, time out the select call then.
178 if (timeouts->when <= cur_time) {
180 timeouts = timeouts->next;
181 (*(t->func))(t->what);
182 t->next = free_timeouts;
188 * Figure timeout in milliseconds, and check for
189 * potential overflow, so we can cram into an
190 * int for poll, while not polling with a
191 * negative timeout and blocking indefinitely.
193 howlong = timeouts->when - cur_time;
194 if (howlong > INT_MAX / 1000)
195 howlong = INT_MAX / 1000;
196 to_msec = howlong * 1000;
200 /* Set up the descriptors to be polled. */
202 for (i = 0, l = protocols; l; l = l->next) {
203 struct interface_info *ip = l->local;
205 if (ip == NULL || ip->dead)
208 fds[i].events = POLLIN;
211 if (l->handler == got_one)
214 if (live_interfaces == 0)
215 error("No live interfaces to poll on - exiting.");
217 /* Wait for a packet or a timeout... XXX */
218 count = poll(fds, nfds, to_msec);
220 /* Not likely to be transitory... */
222 if (errno == EAGAIN || errno == EINTR) {
229 /* Get the current time... */
233 for (l = protocols; l; l = l->next) {
234 struct interface_info *ip;
236 if ((fds[i].revents & (POLLIN | POLLHUP))) {
238 if (ip && (l->handler != got_one ||
241 if (interfaces_invalidated)
246 interfaces_invalidated = 0;
252 got_one(struct protocol *l)
254 struct sockaddr_in from;
255 struct hardware hfrom;
260 * Packet input buffer. Must be as large as largest
263 unsigned char packbuf[4095];
264 struct dhcp_packet packet;
266 struct interface_info *ip = l->local;
268 if ((result = receive_packet(ip, u.packbuf, sizeof(u), &from,
270 warning("receive_packet failed on %s: %s", ip->name,
273 if ((!interface_status(ip)) ||
274 (ip->noifmedia && ip->errors > 20)) {
275 /* our interface has gone away. */
276 warning("Interface %s no longer appears valid.",
279 interfaces_invalidated = 1;
289 if (bootp_packet_handler) {
291 memcpy(ifrom.iabuf, &from.sin_addr, ifrom.len);
293 (*bootp_packet_handler)(ip, &u.packet, result,
294 from.sin_port, ifrom, &hfrom);
299 interface_status(struct interface_info *ifinfo)
301 char *ifname = ifinfo->name;
302 int ifsock = ifinfo->rfdesc;
304 struct ifmediareq ifmr;
306 /* get interface flags */
307 memset(&ifr, 0, sizeof(ifr));
308 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
309 if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) < 0) {
310 cap_syslog(capsyslog, LOG_ERR, "ioctl(SIOCGIFFLAGS) on %s: %m",
316 * if one of UP and RUNNING flags is dropped,
317 * the interface is not active.
319 if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
322 /* Next, check carrier on the interface, if possible */
323 if (ifinfo->noifmedia)
325 memset(&ifmr, 0, sizeof(ifmr));
326 strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
327 if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
328 if (errno != EINVAL) {
329 cap_syslog(capsyslog, LOG_DEBUG,
330 "ioctl(SIOCGIFMEDIA) on %s: %m", ifname);
331 ifinfo->noifmedia = 1;
335 * EINVAL (or ENOTTY) simply means that the interface
336 * does not support the SIOCGIFMEDIA ioctl. We regard it alive.
338 ifinfo->noifmedia = 1;
341 if (ifmr.ifm_status & IFM_AVALID) {
342 switch (ifmr.ifm_active & IFM_NMASK) {
345 if (ifmr.ifm_status & IFM_ACTIVE)
361 add_timeout(time_t when, void (*where)(void *), void *what)
363 struct timeout *t, *q;
365 /* See if this timeout supersedes an existing timeout. */
367 for (q = timeouts; q; q = q->next) {
368 if (q->func == where && q->what == what) {
378 /* If we didn't supersede a timeout, allocate a timeout
383 free_timeouts = q->next;
387 q = malloc(sizeof(struct timeout));
389 error("Can't allocate timeout structure!");
397 /* Now sort this timeout into the timeout list. */
399 /* Beginning of list? */
400 if (!timeouts || timeouts->when > q->when) {
406 /* Middle of list? */
407 for (t = timeouts; t->next; t = t->next) {
408 if (t->next->when > q->when) {
421 cancel_timeout(void (*where)(void *), void *what)
423 struct timeout *t, *q;
425 /* Look for this timeout on the list, and unlink it if we find it. */
427 for (q = timeouts; q; q = q->next) {
428 if (q->func == where && q->what == what) {
438 /* If we found the timeout, put it on the free list. */
440 q->next = free_timeouts;
445 /* Add a protocol to the list of protocols... */
447 add_protocol(const char *name, int fd, void (*handler)(struct protocol *),
452 p = malloc(sizeof(*p));
454 error("can't allocate protocol struct for %s", name);
457 p->handler = handler;
464 remove_protocol(struct protocol *proto)
466 struct protocol *p, *prev;
468 for (p = protocols, prev = NULL; p != NULL; prev = p, p = p->next) {
473 prev->next = p->next;
481 interface_link_status(char *ifname)
483 struct ifmediareq ifmr;
486 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
487 error("Can't create socket");
489 memset(&ifmr, 0, sizeof(ifmr));
490 strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
491 if (ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
492 /* EINVAL -> link state unknown. treat as active */
494 cap_syslog(capsyslog, LOG_DEBUG,
495 "ioctl(SIOCGIFMEDIA) on %s: %m", ifname);
501 if (ifmr.ifm_status & IFM_AVALID) {
502 switch (ifmr.ifm_active & IFM_NMASK) {
505 if (ifmr.ifm_status & IFM_ACTIVE)
515 interface_set_mtu_unpriv(int privfd, u_int16_t mtu)
521 hdr.code = IMSG_SET_INTERFACE_MTU;
522 hdr.len = sizeof(hdr) +
525 if ((buf = buf_open(hdr.len)) == NULL)
526 error("buf_open: %m");
528 errs += buf_add(buf, &hdr, sizeof(hdr));
529 errs += buf_add(buf, &mtu, sizeof(mtu));
531 error("buf_add: %m");
533 if (buf_close(privfd, buf) == -1)
534 error("buf_close: %m");
538 interface_set_mtu_priv(char *ifname, u_int16_t mtu)
544 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
545 error("Can't create socket");
547 memset(&ifr, 0, sizeof(ifr));
550 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
552 if (ioctl(sock, SIOCGIFMTU, (caddr_t)&ifr) == -1)
553 warning("SIOCGIFMTU failed (%s): %s", ifname,
556 old_mtu = ifr.ifr_mtu;
558 if (mtu != old_mtu) {
561 if (ioctl(sock, SIOCSIFMTU, &ifr) == -1)
562 warning("SIOCSIFMTU failed (%d): %s", mtu,