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''.
44 //#include <sys/ioctl.h>
46 //#include <net/if_media.h>
47 //#include <ifaddrs.h>
50 struct protocol
*protocols
= NULL
;
51 struct timeout
*timeouts
= NULL
;
52 static struct timeout
*free_timeouts
= NULL
;
53 static int interfaces_invalidated
= FALSE
;
54 void (*bootp_packet_handler
)(struct interface_info
*,
55 struct dhcp_packet
*, int, unsigned int,
56 struct iaddr
, struct hardware
*);
58 static int interface_status(struct interface_info
*ifinfo
);
61 * Use getifaddrs() to get a list of all the attached interfaces. For
62 * each interface that's of type INET and not the loopback interface,
63 * register that interface with the network I/O software, figure out
64 * what subnet it's on, and add it to the list of interfaces.
67 discover_interfaces(struct interface_info
*iface
)
69 PDHCP_ADAPTER Adapter
= AdapterFindInfo( iface
);
71 if_register_receive(iface
);
72 if_register_send(iface
);
74 if( Adapter
->DhclientState
.state
!= S_STATIC
) {
75 add_protocol(iface
->name
, iface
->rfdesc
, got_one
, iface
);
76 iface
->client
->state
= S_INIT
;
82 reinitialize_interfaces(void)
84 interfaces_invalidated
= 1;
88 * Wait for packets to come in using poll(). When a packet comes in,
89 * call receive_packet to receive the packet and possibly strip hardware
90 * addressing information from it, and then call through the
91 * bootp_packet_handler hook to try to do something with it.
96 int count
, i
, to_msec
, nfds
= 0;
100 struct timeval timeval
;
104 for (l
= protocols
; l
; l
= l
->next
)
111 * Call any expired timeouts, and then if there's still
112 * a timeout registered, time out the select call then.
118 if (timeouts
->when
<= cur_time
) {
120 timeouts
= timeouts
->next
;
121 (*(t
->func
))(t
->what
);
122 t
->next
= free_timeouts
;
128 * Figure timeout in milliseconds, and check for
129 * potential overflow, so we can cram into an
130 * int for poll, while not polling with a
131 * negative timeout and blocking indefinitely.
133 howlong
= timeouts
->when
- cur_time
;
134 if (howlong
> INT_MAX
/ 1000)
135 howlong
= INT_MAX
/ 1000;
136 to_msec
= howlong
* 1000;
140 /* Set up the descriptors to be polled. */
141 for (i
= 0, l
= protocols
; l
; l
= l
->next
) {
142 struct interface_info
*ip
= l
->local
;
144 if (ip
&& (l
->handler
!= got_one
|| !ip
->dead
)) {
151 /* No interfaces for now, set the select timeout reasonably so
152 * we can recover from that condition later. */
156 /* Wait for a packet or a timeout... XXX */
157 timeval
.tv_sec
= to_msec
/ 1000;
158 timeval
.tv_usec
= (to_msec
% 1000) * 1000;
163 count
= select(nfds
, &fds
, NULL
, NULL
, &timeval
);
165 DH_DbgPrint(MID_TRACE
,("Select: %d\n", count
));
167 /* Review poll output */
168 for (i
= 0, l
= protocols
; l
; l
= l
->next
) {
169 struct interface_info
*ip
= l
->local
;
171 if (ip
&& (l
->handler
!= got_one
|| !ip
->dead
)) {
175 l
->fd
, FD_ISSET(l
->fd
, &fds
) ? "true" : "false"));
183 /* Not likely to be transitory... */
184 if (count
== SOCKET_ERROR
) {
185 if (errno
== EAGAIN
|| errno
== EINTR
) {
194 /* Get the current time... */
198 for (l
= protocols
; l
; l
= l
->next
) {
199 struct interface_info
*ip
;
201 if (FD_ISSET(l
->fd
, &fds
)) {
202 if (ip
&& (l
->handler
!= got_one
||
204 DH_DbgPrint(MID_TRACE
,("Handling %x\n", l
));
206 if (interfaces_invalidated
)
211 interfaces_invalidated
= 0;
215 ApiUnlock(); /* Not reached currently */
219 got_one(struct protocol
*l
)
221 struct sockaddr_in from
;
222 struct hardware hfrom
;
227 * Packet input buffer. Must be as large as largest
230 unsigned char packbuf
[4095];
231 struct dhcp_packet packet
;
233 struct interface_info
*ip
= l
->local
;
235 if ((result
= receive_packet(ip
, u
.packbuf
, sizeof(u
), &from
,
237 warning("receive_packet failed on %s: %s", ip
->name
,
240 if ((!interface_status(ip
)) ||
241 (ip
->noifmedia
&& ip
->errors
> 20)) {
242 /* our interface has gone away. */
243 warning("Interface %s no longer appears valid.",
246 interfaces_invalidated
= 1;
256 if (bootp_packet_handler
) {
258 memcpy(ifrom
.iabuf
, &from
.sin_addr
, ifrom
.len
);
260 (*bootp_packet_handler
)(ip
, &u
.packet
, result
,
261 from
.sin_port
, ifrom
, &hfrom
);
267 interface_status(struct interface_info
*ifinfo
)
269 char *ifname
= ifinfo
->name
;
270 int ifsock
= ifinfo
->rfdesc
;
272 struct ifmediareq ifmr
;
274 /* get interface flags */
275 memset(&ifr
, 0, sizeof(ifr
));
276 strlcpy(ifr
.ifr_name
, ifname
, sizeof(ifr
.ifr_name
));
277 if (ioctl(ifsock
, SIOCGIFFLAGS
, &ifr
) < 0) {
278 syslog(LOG_ERR
, "ioctl(SIOCGIFFLAGS) on %s: %m", ifname
);
283 * if one of UP and RUNNING flags is dropped,
284 * the interface is not active.
286 if ((ifr
.ifr_flags
& (IFF_UP
|IFF_RUNNING
)) != (IFF_UP
|IFF_RUNNING
))
289 /* Next, check carrier on the interface, if possible */
290 if (ifinfo
->noifmedia
)
292 memset(&ifmr
, 0, sizeof(ifmr
));
293 strlcpy(ifmr
.ifm_name
, ifname
, sizeof(ifmr
.ifm_name
));
294 if (ioctl(ifsock
, SIOCGIFMEDIA
, (caddr_t
)&ifmr
) < 0) {
295 if (errno
!= EINVAL
) {
296 syslog(LOG_DEBUG
, "ioctl(SIOCGIFMEDIA) on %s: %m",
299 ifinfo
->noifmedia
= 1;
303 * EINVAL (or ENOTTY) simply means that the interface
304 * does not support the SIOCGIFMEDIA ioctl. We regard it alive.
306 ifinfo
->noifmedia
= 1;
309 if (ifmr
.ifm_status
& IFM_AVALID
) {
310 switch (ifmr
.ifm_active
& IFM_NMASK
) {
312 if (ifmr
.ifm_status
& IFM_ACTIVE
)
328 interface_status(struct interface_info
*ifinfo
)
335 add_timeout(time_t when
, void (*where
)(void *), void *what
)
337 struct timeout
*t
, *q
;
339 DH_DbgPrint(MID_TRACE
,("Adding timeout %x %p %x\n", when
, where
, what
));
340 /* See if this timeout supersedes an existing timeout. */
342 for (q
= timeouts
; q
; q
= q
->next
) {
343 if (q
->func
== where
&& q
->what
== what
) {
353 /* If we didn't supersede a timeout, allocate a timeout
358 free_timeouts
= q
->next
;
362 q
= malloc(sizeof(struct timeout
));
364 error("Can't allocate timeout structure!");
372 /* Now sort this timeout into the timeout list. */
374 /* Beginning of list? */
375 if (!timeouts
|| timeouts
->when
> q
->when
) {
381 /* Middle of list? */
382 for (t
= timeouts
; t
->next
; t
= t
->next
) {
383 if (t
->next
->when
> q
->when
) {
396 cancel_timeout(void (*where
)(void *), void *what
)
398 struct timeout
*t
, *q
;
400 /* Look for this timeout on the list, and unlink it if we find it. */
402 for (q
= timeouts
; q
; q
= q
->next
) {
403 if (q
->func
== where
&& q
->what
== what
) {
413 /* If we found the timeout, put it on the free list. */
415 q
->next
= free_timeouts
;
420 /* Add a protocol to the list of protocols... */
422 add_protocol(char *name
, int fd
, void (*handler
)(struct protocol
*),
427 p
= malloc(sizeof(*p
));
429 error("can't allocate protocol struct for %s", name
);
432 p
->handler
= handler
;
439 remove_protocol(struct protocol
*proto
)
441 struct protocol
*p
, *next
, *prev
;
444 for (p
= protocols
; p
; p
= next
) {
448 prev
->next
= p
->next
;
457 find_protocol_by_adapter(struct interface_info
*info
)
461 for( p
= protocols
; p
; p
= p
->next
) {
462 if( p
->local
== (void *)info
) return p
;
469 interface_link_status(char *ifname
)
472 struct ifmediareq ifmr
;
475 if ((sock
= socket(AF_INET
, SOCK_DGRAM
, 0)) == -1)
476 error("Can't create socket");
478 memset(&ifmr
, 0, sizeof(ifmr
));
479 strlcpy(ifmr
.ifm_name
, ifname
, sizeof(ifmr
.ifm_name
));
480 if (ioctl(sock
, SIOCGIFMEDIA
, (caddr_t
)&ifmr
) == -1) {
481 /* EINVAL -> link state unknown. treat as active */
483 syslog(LOG_DEBUG
, "ioctl(SIOCGIFMEDIA) on %s: %m",
490 if (ifmr
.ifm_status
& IFM_AVALID
) {
491 if ((ifmr
.ifm_active
& IFM_NMASK
) == IFM_ETHER
) {
492 if (ifmr
.ifm_status
& IFM_ACTIVE
)