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>
50 #include <net/if_media.h>
54 struct protocol *protocols;
55 struct timeout *timeouts;
56 static struct timeout *free_timeouts;
57 static int interfaces_invalidated;
58 void (*bootp_packet_handler)(struct interface_info *,
59 struct dhcp_packet *, int, unsigned int,
60 struct iaddr, struct hardware *);
62 static int interface_status(struct interface_info *ifinfo);
65 * Use getifaddrs() to get a list of all the attached interfaces. For
66 * each interface that's of type INET and not the loopback interface,
67 * register that interface with the network I/O software, figure out
68 * what subnet it's on, and add it to the list of interfaces.
71 discover_interfaces(struct interface_info *iface)
73 struct ifaddrs *ifap, *ifa;
74 struct sockaddr_in foo;
77 if (getifaddrs(&ifap) != 0)
78 error("getifaddrs failed");
80 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
81 if ((ifa->ifa_flags & IFF_LOOPBACK) ||
82 (ifa->ifa_flags & IFF_POINTOPOINT) ||
83 (!(ifa->ifa_flags & IFF_UP)))
86 if (strcmp(iface->name, ifa->ifa_name))
90 * If we have the capability, extract link information
91 * and record it in a linked list.
93 if (ifa->ifa_addr->sa_family == AF_LINK) {
94 struct sockaddr_dl *foo =
95 (struct sockaddr_dl *)ifa->ifa_addr;
97 iface->index = foo->sdl_index;
98 iface->hw_address.hlen = foo->sdl_alen;
99 iface->hw_address.htype = HTYPE_ETHER; /* XXX */
100 memcpy(iface->hw_address.haddr,
101 LLADDR(foo), foo->sdl_alen);
102 } else if (ifa->ifa_addr->sa_family == AF_INET) {
105 memcpy(&foo, ifa->ifa_addr, sizeof(foo));
106 if (foo.sin_addr.s_addr == htonl(INADDR_LOOPBACK))
109 if ((tif = calloc(1, sizeof(struct ifreq)))
111 error("no space to remember ifp");
112 strlcpy(tif->ifr_name, ifa->ifa_name, IFNAMSIZ);
113 memcpy(&tif->ifr_addr, ifa->ifa_addr,
114 ifa->ifa_addr->sa_len);
116 iface->primary_address = foo.sin_addr;
119 memcpy(addr.iabuf, &foo.sin_addr.s_addr, addr.len);
124 error("%s: not found", iface->name);
126 /* Register the interface... */
127 if_register_receive(iface);
128 if_register_send(iface);
129 add_protocol(iface->name, iface->rfdesc, got_one, iface);
134 reinitialize_interfaces(void)
136 interfaces_invalidated = 1;
140 * Wait for packets to come in using poll(). When a packet comes in,
141 * call receive_packet to receive the packet and possibly strip hardware
142 * addressing information from it, and then call through the
143 * bootp_packet_handler hook to try to do something with it.
148 int count, live_interfaces, i, to_msec, nfds = 0;
153 for (l = protocols; l; l = l->next)
156 fds = malloc(nfds * sizeof(struct pollfd));
158 error("Can't allocate poll structures.");
162 * Call any expired timeouts, and then if there's still
163 * a timeout registered, time out the select call then.
169 if (timeouts->when <= cur_time) {
171 timeouts = timeouts->next;
172 (*(t->func))(t->what);
173 t->next = free_timeouts;
179 * Figure timeout in milliseconds, and check for
180 * potential overflow, so we can cram into an
181 * int for poll, while not polling with a
182 * negative timeout and blocking indefinitely.
184 howlong = timeouts->when - cur_time;
185 if (howlong > INT_MAX / 1000)
186 howlong = INT_MAX / 1000;
187 to_msec = howlong * 1000;
191 /* Set up the descriptors to be polled. */
193 for (i = 0, l = protocols; l; l = l->next) {
194 struct interface_info *ip = l->local;
196 if (ip == NULL || ip->dead)
199 fds[i].events = POLLIN;
202 if (l->handler == got_one)
205 if (live_interfaces == 0)
206 error("No live interfaces to poll on - exiting.");
208 /* Wait for a packet or a timeout... XXX */
209 count = poll(fds, nfds, to_msec);
211 /* Not likely to be transitory... */
213 if (errno == EAGAIN || errno == EINTR) {
220 /* Get the current time... */
224 for (l = protocols; l; l = l->next) {
225 struct interface_info *ip;
227 if ((fds[i].revents & (POLLIN | POLLHUP))) {
229 if (ip && (l->handler != got_one ||
232 if (interfaces_invalidated)
237 interfaces_invalidated = 0;
243 got_one(struct protocol *l)
245 struct sockaddr_in from;
246 struct hardware hfrom;
251 * Packet input buffer. Must be as large as largest
254 unsigned char packbuf[4095];
255 struct dhcp_packet packet;
257 struct interface_info *ip = l->local;
259 if ((result = receive_packet(ip, u.packbuf, sizeof(u), &from,
261 warning("receive_packet failed on %s: %s", ip->name,
264 if ((!interface_status(ip)) ||
265 (ip->noifmedia && ip->errors > 20)) {
266 /* our interface has gone away. */
267 warning("Interface %s no longer appears valid.",
270 interfaces_invalidated = 1;
280 if (bootp_packet_handler) {
282 memcpy(ifrom.iabuf, &from.sin_addr, ifrom.len);
284 (*bootp_packet_handler)(ip, &u.packet, result,
285 from.sin_port, ifrom, &hfrom);
290 interface_status(struct interface_info *ifinfo)
292 char *ifname = ifinfo->name;
293 int ifsock = ifinfo->rfdesc;
295 struct ifmediareq ifmr;
297 /* get interface flags */
298 memset(&ifr, 0, sizeof(ifr));
299 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
300 if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) < 0) {
301 cap_syslog(capsyslog, LOG_ERR, "ioctl(SIOCGIFFLAGS) on %s: %m",
307 * if one of UP and RUNNING flags is dropped,
308 * the interface is not active.
310 if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
313 /* Next, check carrier on the interface, if possible */
314 if (ifinfo->noifmedia)
316 memset(&ifmr, 0, sizeof(ifmr));
317 strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
318 if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
319 if (errno != EINVAL) {
320 cap_syslog(capsyslog, LOG_DEBUG,
321 "ioctl(SIOCGIFMEDIA) on %s: %m", ifname);
322 ifinfo->noifmedia = 1;
326 * EINVAL (or ENOTTY) simply means that the interface
327 * does not support the SIOCGIFMEDIA ioctl. We regard it alive.
329 ifinfo->noifmedia = 1;
332 if (ifmr.ifm_status & IFM_AVALID) {
333 switch (ifmr.ifm_active & IFM_NMASK) {
336 if (ifmr.ifm_status & IFM_ACTIVE)
352 add_timeout(time_t when, void (*where)(void *), void *what)
354 struct timeout *t, *q;
356 /* See if this timeout supersedes an existing timeout. */
358 for (q = timeouts; q; q = q->next) {
359 if (q->func == where && q->what == what) {
369 /* If we didn't supersede a timeout, allocate a timeout
374 free_timeouts = q->next;
378 q = malloc(sizeof(struct timeout));
380 error("Can't allocate timeout structure!");
388 /* Now sort this timeout into the timeout list. */
390 /* Beginning of list? */
391 if (!timeouts || timeouts->when > q->when) {
397 /* Middle of list? */
398 for (t = timeouts; t->next; t = t->next) {
399 if (t->next->when > q->when) {
412 cancel_timeout(void (*where)(void *), void *what)
414 struct timeout *t, *q;
416 /* Look for this timeout on the list, and unlink it if we find it. */
418 for (q = timeouts; q; q = q->next) {
419 if (q->func == where && q->what == what) {
429 /* If we found the timeout, put it on the free list. */
431 q->next = free_timeouts;
436 /* Add a protocol to the list of protocols... */
438 add_protocol(char *name, int fd, void (*handler)(struct protocol *),
443 p = malloc(sizeof(*p));
445 error("can't allocate protocol struct for %s", name);
448 p->handler = handler;
455 remove_protocol(struct protocol *proto)
457 struct protocol *p, *next;
459 for (p = protocols; p; p = next) {
469 interface_link_status(char *ifname)
471 struct ifmediareq ifmr;
474 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
475 error("Can't create socket");
477 memset(&ifmr, 0, sizeof(ifmr));
478 strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
479 if (ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
480 /* EINVAL -> link state unknown. treat as active */
482 cap_syslog(capsyslog, LOG_DEBUG,
483 "ioctl(SIOCGIFMEDIA) on %s: %m", ifname);
489 if (ifmr.ifm_status & IFM_AVALID) {
490 switch (ifmr.ifm_active & IFM_NMASK) {
493 if (ifmr.ifm_status & IFM_ACTIVE)
503 interface_set_mtu_unpriv(int privfd, u_int16_t mtu)
509 hdr.code = IMSG_SET_INTERFACE_MTU;
510 hdr.len = sizeof(hdr) +
513 if ((buf = buf_open(hdr.len)) == NULL)
514 error("buf_open: %m");
516 errs += buf_add(buf, &hdr, sizeof(hdr));
517 errs += buf_add(buf, &mtu, sizeof(mtu));
519 error("buf_add: %m");
521 if (buf_close(privfd, buf) == -1)
522 error("buf_close: %m");
526 interface_set_mtu_priv(char *ifname, u_int16_t mtu)
531 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
532 error("Can't create socket");
534 memset(&ifr, 0, sizeof(ifr));
536 strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
539 if (ioctl(sock, SIOCSIFMTU, &ifr) == -1)
540 warning("SIOCSIFMTU failed (%d): %s", mtu,