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$");
48 #include <sys/ioctl.h>
51 #include <net/if_media.h>
55 /* Assert that pointer p is aligned to at least align bytes */
56 #define assert_aligned(p, align) assert((((uintptr_t)p) & ((align) - 1)) == 0)
58 struct protocol *protocols;
59 struct timeout *timeouts;
60 static struct timeout *free_timeouts;
61 static int interfaces_invalidated;
62 void (*bootp_packet_handler)(struct interface_info *,
63 struct dhcp_packet *, int, unsigned int,
64 struct iaddr, struct hardware *);
66 static int interface_status(struct interface_info *ifinfo);
69 * Use getifaddrs() to get a list of all the attached interfaces. For
70 * each interface that's of type INET and not the loopback interface,
71 * register that interface with the network I/O software, figure out
72 * what subnet it's on, and add it to the list of interfaces.
75 discover_interfaces(struct interface_info *iface)
77 struct ifaddrs *ifap, *ifa;
80 if (getifaddrs(&ifap) != 0)
81 error("getifaddrs failed");
83 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
84 if ((ifa->ifa_flags & IFF_LOOPBACK) ||
85 (ifa->ifa_flags & IFF_POINTOPOINT) ||
86 (!(ifa->ifa_flags & IFF_UP)))
89 if (strcmp(iface->name, ifa->ifa_name))
93 * If we have the capability, extract link information
94 * and record it in a linked list.
96 if (ifa->ifa_addr->sa_family == AF_LINK) {
97 struct sockaddr_dl *foo;
100 * The implementation of getifaddrs should guarantee
103 assert_aligned(ifa->ifa_addr,
104 _Alignof(struct sockaddr_dl));
106 #pragma clang diagnostic push
107 #pragma clang diagnostic ignored "-Wcast-align"
109 foo = (struct sockaddr_dl *)ifa->ifa_addr;
111 #pragma clang diagnostic pop
114 iface->index = foo->sdl_index;
115 iface->hw_address.hlen = foo->sdl_alen;
116 iface->hw_address.htype = HTYPE_ETHER; /* XXX */
117 memcpy(iface->hw_address.haddr,
118 LLADDR(foo), foo->sdl_alen);
119 } else if (ifa->ifa_addr->sa_family == AF_INET) {
120 struct sockaddr_in foo;
123 memcpy(&foo, ifa->ifa_addr, sizeof(foo));
124 if (foo.sin_addr.s_addr == htonl(INADDR_LOOPBACK))
127 if ((tif = calloc(1, sizeof(struct ifreq)))
129 error("no space to remember ifp");
130 strlcpy(tif->ifr_name, ifa->ifa_name, IFNAMSIZ);
131 memcpy(&tif->ifr_addr, ifa->ifa_addr,
132 ifa->ifa_addr->sa_len);
134 iface->primary_address = foo.sin_addr;
137 memcpy(addr.iabuf, &foo.sin_addr.s_addr, addr.len);
142 error("%s: not found", iface->name);
144 /* Register the interface... */
145 if_register_receive(iface);
146 if_register_send(iface);
147 add_protocol(iface->name, iface->rfdesc, got_one, iface);
152 reinitialize_interfaces(void)
154 interfaces_invalidated = 1;
158 * Wait for packets to come in using poll(). When a packet comes in,
159 * call receive_packet to receive the packet and possibly strip hardware
160 * addressing information from it, and then call through the
161 * bootp_packet_handler hook to try to do something with it.
166 int count, live_interfaces, i, to_msec, nfds = 0;
171 for (l = protocols; l; l = l->next)
174 fds = malloc(nfds * sizeof(struct pollfd));
176 error("Can't allocate poll structures.");
180 * Call any expired timeouts, and then if there's still
181 * a timeout registered, time out the select call then.
187 if (timeouts->when <= cur_time) {
189 timeouts = timeouts->next;
190 (*(t->func))(t->what);
191 t->next = free_timeouts;
197 * Figure timeout in milliseconds, and check for
198 * potential overflow, so we can cram into an
199 * int for poll, while not polling with a
200 * negative timeout and blocking indefinitely.
202 howlong = timeouts->when - cur_time;
203 if (howlong > INT_MAX / 1000)
204 howlong = INT_MAX / 1000;
205 to_msec = howlong * 1000;
209 /* Set up the descriptors to be polled. */
211 for (i = 0, l = protocols; l; l = l->next) {
212 struct interface_info *ip = l->local;
214 if (ip == NULL || ip->dead)
217 fds[i].events = POLLIN;
220 if (l->handler == got_one)
223 if (live_interfaces == 0)
224 error("No live interfaces to poll on - exiting.");
226 /* Wait for a packet or a timeout... XXX */
227 count = poll(fds, nfds, to_msec);
229 /* Not likely to be transitory... */
231 if (errno == EAGAIN || errno == EINTR) {
238 /* Get the current time... */
242 for (l = protocols; l; l = l->next) {
243 struct interface_info *ip;
245 if ((fds[i].revents & (POLLIN | POLLHUP))) {
247 if (ip && (l->handler != got_one ||
250 if (interfaces_invalidated)
255 interfaces_invalidated = 0;
261 got_one(struct protocol *l)
263 struct sockaddr_in from;
264 struct hardware hfrom;
269 * Packet input buffer. Must be as large as largest
272 unsigned char packbuf[4095];
273 struct dhcp_packet packet;
275 struct interface_info *ip = l->local;
277 if ((result = receive_packet(ip, u.packbuf, sizeof(u), &from,
279 warning("receive_packet failed on %s: %s", ip->name,
282 if ((!interface_status(ip)) ||
283 (ip->noifmedia && ip->errors > 20)) {
284 /* our interface has gone away. */
285 warning("Interface %s no longer appears valid.",
288 interfaces_invalidated = 1;
298 if (bootp_packet_handler) {
300 memcpy(ifrom.iabuf, &from.sin_addr, ifrom.len);
302 (*bootp_packet_handler)(ip, &u.packet, result,
303 from.sin_port, ifrom, &hfrom);
308 interface_status(struct interface_info *ifinfo)
310 char *ifname = ifinfo->name;
311 int ifsock = ifinfo->rfdesc;
313 struct ifmediareq ifmr;
315 /* get interface flags */
316 memset(&ifr, 0, sizeof(ifr));
317 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
318 if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) < 0) {
319 syslog(LOG_ERR, "ioctl(SIOCGIFFLAGS) on %s: %m", ifname);
324 * if one of UP and RUNNING flags is dropped,
325 * the interface is not active.
327 if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
330 /* Next, check carrier on the interface, if possible */
331 if (ifinfo->noifmedia)
333 memset(&ifmr, 0, sizeof(ifmr));
334 strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
335 if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
336 if (errno != EINVAL) {
337 syslog(LOG_DEBUG, "ioctl(SIOCGIFMEDIA) on %s: %m",
340 ifinfo->noifmedia = 1;
344 * EINVAL (or ENOTTY) simply means that the interface
345 * does not support the SIOCGIFMEDIA ioctl. We regard it alive.
347 ifinfo->noifmedia = 1;
350 if (ifmr.ifm_status & IFM_AVALID) {
351 switch (ifmr.ifm_active & IFM_NMASK) {
354 if (ifmr.ifm_status & IFM_ACTIVE)
370 add_timeout(time_t when, void (*where)(void *), void *what)
372 struct timeout *t, *q;
374 /* See if this timeout supersedes an existing timeout. */
376 for (q = timeouts; q; q = q->next) {
377 if (q->func == where && q->what == what) {
387 /* If we didn't supersede a timeout, allocate a timeout
392 free_timeouts = q->next;
396 q = malloc(sizeof(struct timeout));
398 error("Can't allocate timeout structure!");
406 /* Now sort this timeout into the timeout list. */
408 /* Beginning of list? */
409 if (!timeouts || timeouts->when > q->when) {
415 /* Middle of list? */
416 for (t = timeouts; t->next; t = t->next) {
417 if (t->next->when > q->when) {
430 cancel_timeout(void (*where)(void *), void *what)
432 struct timeout *t, *q;
434 /* Look for this timeout on the list, and unlink it if we find it. */
436 for (q = timeouts; q; q = q->next) {
437 if (q->func == where && q->what == what) {
447 /* If we found the timeout, put it on the free list. */
449 q->next = free_timeouts;
454 /* Add a protocol to the list of protocols... */
456 add_protocol(const char *name, int fd, void (*handler)(struct protocol *),
461 p = malloc(sizeof(*p));
463 error("can't allocate protocol struct for %s", name);
466 p->handler = handler;
473 remove_protocol(struct protocol *proto)
475 struct protocol *p, *next;
477 for (p = protocols; p; p = next) {
487 interface_link_status(char *ifname)
489 struct ifmediareq ifmr;
492 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
493 error("Can't create socket");
495 memset(&ifmr, 0, sizeof(ifmr));
496 strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
497 if (ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
498 /* EINVAL -> link state unknown. treat as active */
500 syslog(LOG_DEBUG, "ioctl(SIOCGIFMEDIA) on %s: %m",
507 if (ifmr.ifm_status & IFM_AVALID) {
508 switch (ifmr.ifm_active & IFM_NMASK) {
511 if (ifmr.ifm_status & IFM_ACTIVE)
521 interface_set_mtu_unpriv(int privfd, u_int16_t mtu)
527 hdr.code = IMSG_SET_INTERFACE_MTU;
528 hdr.len = sizeof(hdr) +
531 if ((buf = buf_open(hdr.len)) == NULL)
532 error("buf_open: %m");
534 errs += buf_add(buf, &hdr, sizeof(hdr));
535 errs += buf_add(buf, &mtu, sizeof(mtu));
537 error("buf_add: %m");
539 if (buf_close(privfd, buf) == -1)
540 error("buf_close: %m");
544 interface_set_mtu_priv(char *ifname, u_int16_t mtu)
550 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
551 error("Can't create socket");
553 memset(&ifr, 0, sizeof(ifr));
556 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
558 if (ioctl(sock, SIOCGIFMTU, (caddr_t)&ifr) == -1)
559 warning("SIOCGIFMTU failed (%s): %s", ifname,
562 old_mtu = ifr.ifr_mtu;
564 if (mtu != old_mtu) {
567 if (ioctl(sock, SIOCSIFMTU, &ifr) == -1)
568 warning("SIOCSIFMTU failed (%d): %s", mtu,