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;
82 if (getifaddrs(&ifap) != 0)
83 error("getifaddrs failed");
85 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
86 if ((ifa->ifa_flags & IFF_LOOPBACK) ||
87 (ifa->ifa_flags & IFF_POINTOPOINT) ||
88 (!(ifa->ifa_flags & IFF_UP)))
91 if (strcmp(iface->name, ifa->ifa_name))
95 * If we have the capability, extract link information
96 * and record it in a linked list.
98 if (ifa->ifa_addr->sa_family == AF_LINK) {
99 struct sockaddr_dl *foo;
102 * The implementation of getifaddrs should guarantee
105 assert_aligned(ifa->ifa_addr,
106 _Alignof(struct sockaddr_dl));
108 #pragma clang diagnostic push
109 #pragma clang diagnostic ignored "-Wcast-align"
111 foo = (struct sockaddr_dl *)ifa->ifa_addr;
113 #pragma clang diagnostic pop
116 iface->index = foo->sdl_index;
117 iface->hw_address.hlen = foo->sdl_alen;
118 iface->hw_address.htype = HTYPE_ETHER; /* XXX */
119 memcpy(iface->hw_address.haddr,
120 LLADDR(foo), foo->sdl_alen);
121 } else if (ifa->ifa_addr->sa_family == AF_INET) {
122 struct sockaddr_in foo;
125 memcpy(&foo, ifa->ifa_addr, sizeof(foo));
126 if (foo.sin_addr.s_addr == htonl(INADDR_LOOPBACK))
129 if ((tif = calloc(1, sizeof(struct ifreq)))
131 error("no space to remember ifp");
132 strlcpy(tif->ifr_name, ifa->ifa_name, IFNAMSIZ);
133 memcpy(&tif->ifr_addr, ifa->ifa_addr,
134 ifa->ifa_addr->sa_len);
136 iface->primary_address = foo.sin_addr;
139 memcpy(addr.iabuf, &foo.sin_addr.s_addr, addr.len);
144 error("%s: not found", iface->name);
146 /* Register the interface... */
147 if_register_receive(iface);
148 if_register_send(iface);
149 add_protocol(iface->name, iface->rfdesc, got_one, iface);
154 reinitialize_interfaces(void)
156 interfaces_invalidated = 1;
160 * Wait for packets to come in using poll(). When a packet comes in,
161 * call receive_packet to receive the packet and possibly strip hardware
162 * addressing information from it, and then call through the
163 * bootp_packet_handler hook to try to do something with it.
168 int count, live_interfaces, i, to_msec, nfds = 0;
173 for (l = protocols; l; l = l->next)
176 fds = malloc(nfds * sizeof(struct pollfd));
178 error("Can't allocate poll structures.");
182 * Call any expired timeouts, and then if there's still
183 * a timeout registered, time out the select call then.
189 if (timeouts->when <= cur_time) {
191 timeouts = timeouts->next;
192 (*(t->func))(t->what);
193 t->next = free_timeouts;
199 * Figure timeout in milliseconds, and check for
200 * potential overflow, so we can cram into an
201 * int for poll, while not polling with a
202 * negative timeout and blocking indefinitely.
204 howlong = timeouts->when - cur_time;
205 if (howlong > INT_MAX / 1000)
206 howlong = INT_MAX / 1000;
207 to_msec = howlong * 1000;
211 /* Set up the descriptors to be polled. */
213 for (i = 0, l = protocols; l; l = l->next) {
214 struct interface_info *ip = l->local;
216 if (ip == NULL || ip->dead)
219 fds[i].events = POLLIN;
222 if (l->handler == got_one)
225 if (live_interfaces == 0)
226 error("No live interfaces to poll on - exiting.");
228 /* Wait for a packet or a timeout... XXX */
229 count = poll(fds, nfds, to_msec);
231 /* Not likely to be transitory... */
233 if (errno == EAGAIN || errno == EINTR) {
240 /* Get the current time... */
244 for (l = protocols; l; l = l->next) {
245 struct interface_info *ip;
247 if ((fds[i].revents & (POLLIN | POLLHUP))) {
249 if (ip && (l->handler != got_one ||
252 if (interfaces_invalidated)
257 interfaces_invalidated = 0;
263 got_one(struct protocol *l)
265 struct sockaddr_in from;
266 struct hardware hfrom;
271 * Packet input buffer. Must be as large as largest
274 unsigned char packbuf[4095];
275 struct dhcp_packet packet;
277 struct interface_info *ip = l->local;
279 if ((result = receive_packet(ip, u.packbuf, sizeof(u), &from,
281 warning("receive_packet failed on %s: %s", ip->name,
284 if ((!interface_status(ip)) ||
285 (ip->noifmedia && ip->errors > 20)) {
286 /* our interface has gone away. */
287 warning("Interface %s no longer appears valid.",
290 interfaces_invalidated = 1;
300 if (bootp_packet_handler) {
302 memcpy(ifrom.iabuf, &from.sin_addr, ifrom.len);
304 (*bootp_packet_handler)(ip, &u.packet, result,
305 from.sin_port, ifrom, &hfrom);
310 interface_status(struct interface_info *ifinfo)
312 char *ifname = ifinfo->name;
313 int ifsock = ifinfo->rfdesc;
315 struct ifmediareq ifmr;
317 /* get interface flags */
318 memset(&ifr, 0, sizeof(ifr));
319 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
320 if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) < 0) {
321 cap_syslog(capsyslog, LOG_ERR, "ioctl(SIOCGIFFLAGS) on %s: %m",
327 * if one of UP and RUNNING flags is dropped,
328 * the interface is not active.
330 if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
333 /* Next, check carrier on the interface, if possible */
334 if (ifinfo->noifmedia)
336 memset(&ifmr, 0, sizeof(ifmr));
337 strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
338 if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
339 if (errno != EINVAL) {
340 cap_syslog(capsyslog, LOG_DEBUG,
341 "ioctl(SIOCGIFMEDIA) on %s: %m", ifname);
342 ifinfo->noifmedia = 1;
346 * EINVAL (or ENOTTY) simply means that the interface
347 * does not support the SIOCGIFMEDIA ioctl. We regard it alive.
349 ifinfo->noifmedia = 1;
352 if (ifmr.ifm_status & IFM_AVALID) {
353 switch (ifmr.ifm_active & IFM_NMASK) {
356 if (ifmr.ifm_status & IFM_ACTIVE)
372 add_timeout(time_t when, void (*where)(void *), void *what)
374 struct timeout *t, *q;
376 /* See if this timeout supersedes an existing timeout. */
378 for (q = timeouts; q; q = q->next) {
379 if (q->func == where && q->what == what) {
389 /* If we didn't supersede a timeout, allocate a timeout
394 free_timeouts = q->next;
398 q = malloc(sizeof(struct timeout));
400 error("Can't allocate timeout structure!");
408 /* Now sort this timeout into the timeout list. */
410 /* Beginning of list? */
411 if (!timeouts || timeouts->when > q->when) {
417 /* Middle of list? */
418 for (t = timeouts; t->next; t = t->next) {
419 if (t->next->when > q->when) {
432 cancel_timeout(void (*where)(void *), void *what)
434 struct timeout *t, *q;
436 /* Look for this timeout on the list, and unlink it if we find it. */
438 for (q = timeouts; q; q = q->next) {
439 if (q->func == where && q->what == what) {
449 /* If we found the timeout, put it on the free list. */
451 q->next = free_timeouts;
456 /* Add a protocol to the list of protocols... */
458 add_protocol(const char *name, int fd, void (*handler)(struct protocol *),
463 p = malloc(sizeof(*p));
465 error("can't allocate protocol struct for %s", name);
468 p->handler = handler;
475 remove_protocol(struct protocol *proto)
477 struct protocol *p, *next;
479 for (p = protocols; p; p = next) {
489 interface_link_status(char *ifname)
491 struct ifmediareq ifmr;
494 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
495 error("Can't create socket");
497 memset(&ifmr, 0, sizeof(ifmr));
498 strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
499 if (ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
500 /* EINVAL -> link state unknown. treat as active */
502 cap_syslog(capsyslog, LOG_DEBUG,
503 "ioctl(SIOCGIFMEDIA) on %s: %m", ifname);
509 if (ifmr.ifm_status & IFM_AVALID) {
510 switch (ifmr.ifm_active & IFM_NMASK) {
513 if (ifmr.ifm_status & IFM_ACTIVE)
523 interface_set_mtu_unpriv(int privfd, u_int16_t mtu)
529 hdr.code = IMSG_SET_INTERFACE_MTU;
530 hdr.len = sizeof(hdr) +
533 if ((buf = buf_open(hdr.len)) == NULL)
534 error("buf_open: %m");
536 errs += buf_add(buf, &hdr, sizeof(hdr));
537 errs += buf_add(buf, &mtu, sizeof(mtu));
539 error("buf_add: %m");
541 if (buf_close(privfd, buf) == -1)
542 error("buf_close: %m");
546 interface_set_mtu_priv(char *ifname, u_int16_t mtu)
552 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
553 error("Can't create socket");
555 memset(&ifr, 0, sizeof(ifr));
558 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
560 if (ioctl(sock, SIOCGIFMTU, (caddr_t)&ifr) == -1)
561 warning("SIOCGIFMTU failed (%s): %s", ifname,
564 old_mtu = ifr.ifr_mtu;
566 if (mtu != old_mtu) {
569 if (ioctl(sock, SIOCSIFMTU, &ifr) == -1)
570 warning("SIOCSIFMTU failed (%d): %s", mtu,