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 void (*bootp_packet_handler
)(struct interface_info
*,
54 struct dhcp_packet
*, int, unsigned int,
55 struct iaddr
, struct hardware
*);
58 * Wait for packets to come in using poll(). When a packet comes in,
59 * call receive_packet to receive the packet and possibly strip hardware
60 * addressing information from it, and then call through the
61 * bootp_packet_handler hook to try to do something with it.
66 int count
, i
, to_msec
, nfds
;
69 time_t howlong
, cur_time
;
70 struct timeval timeval
;
76 * Call any expired timeouts, and then if there's still
77 * a timeout registered, time out the select call then.
82 for (l
= protocols
, nfds
= 0; l
; l
= l
->next
)
92 if (timeouts
->when
<= cur_time
) {
94 timeouts
= timeouts
->next
;
95 (*(t
->func
))(t
->what
);
96 t
->next
= free_timeouts
;
102 * Figure timeout in milliseconds, and check for
103 * potential overflow, so we can cram into an
104 * int for poll, while not polling with a
105 * negative timeout and blocking indefinitely.
107 howlong
= timeouts
->when
- cur_time
;
108 if (howlong
> INT_MAX
/ 1000)
109 howlong
= INT_MAX
/ 1000;
110 to_msec
= howlong
* 1000;
114 /* Set up the descriptors to be polled. */
115 for (i
= 0, l
= protocols
; l
; l
= l
->next
) {
116 struct interface_info
*ip
= l
->local
;
118 if (ip
&& (l
->handler
!= got_one
|| !ip
->dead
)) {
125 /* No interfaces for now, set the select timeout reasonably so
126 * we can recover from that condition later. */
130 /* Wait for a packet or a timeout... XXX */
131 timeval
.tv_sec
= to_msec
/ 1000;
132 timeval
.tv_usec
= (to_msec
% 1000) * 1000;
137 count
= select(nfds
, &fds
, NULL
, NULL
, &timeval
);
139 DH_DbgPrint(MID_TRACE
,("Select: %d\n", count
));
141 /* Review poll output */
142 for (i
= 0, l
= protocols
; l
; l
= l
->next
) {
143 struct interface_info
*ip
= l
->local
;
145 if (ip
&& (l
->handler
!= got_one
|| !ip
->dead
)) {
149 l
->fd
, FD_ISSET(l
->fd
, &fds
) ? "true" : "false"));
157 /* Not likely to be transitory... */
158 if (count
== SOCKET_ERROR
) {
159 if (errno
== EAGAIN
|| errno
== EINTR
) {
162 error("poll: %s", strerror(errno
));
168 for (l
= protocols
; l
; l
= l
->next
) {
169 struct interface_info
*ip
;
171 if (FD_ISSET(l
->fd
, &fds
)) {
172 if (ip
&& (l
->handler
!= got_one
||
174 DH_DbgPrint(MID_TRACE
,("Handling %x\n", l
));
182 ApiUnlock(); /* Not reached currently */
186 got_one(struct protocol
*l
)
188 struct sockaddr_in from
;
189 struct hardware hfrom
;
194 * Packet input buffer. Must be as large as largest
197 unsigned char packbuf
[4095];
198 struct dhcp_packet packet
;
200 struct interface_info
*ip
= l
->local
;
201 PDHCP_ADAPTER adapter
;
203 if ((result
= receive_packet(ip
, u
.packbuf
, sizeof(u
), &from
,
205 warning("receive_packet failed on %s: %s", ip
->name
,
208 if (ip
->errors
> 20) {
209 /* our interface has gone away. */
210 warning("Interface %s no longer appears valid.",
215 adapter
= AdapterFindInfo(ip
);
217 RemoveEntryList(&adapter
->ListEntry
);
226 if (bootp_packet_handler
) {
228 memcpy(ifrom
.iabuf
, &from
.sin_addr
, ifrom
.len
);
231 adapter
= AdapterFindByHardwareAddress(u
.packet
.chaddr
,
235 warning("Discarding packet with a non-matching target physical address\n");
239 (*bootp_packet_handler
)(&adapter
->DhclientInfo
, &u
.packet
, result
,
240 from
.sin_port
, ifrom
, &hfrom
);
245 add_timeout(time_t when
, void (*where
)(void *), void *what
)
247 struct timeout
*t
, *q
;
249 DH_DbgPrint(MID_TRACE
,("Adding timeout %x %p %x\n", when
, where
, what
));
250 /* See if this timeout supersedes an existing timeout. */
252 for (q
= timeouts
; q
; q
= q
->next
) {
253 if (q
->func
== where
&& q
->what
== what
) {
263 /* If we didn't supersede a timeout, allocate a timeout
268 free_timeouts
= q
->next
;
272 q
= malloc(sizeof(struct timeout
));
274 error("Can't allocate timeout structure!");
282 /* Now sort this timeout into the timeout list. */
284 /* Beginning of list? */
285 if (!timeouts
|| timeouts
->when
> q
->when
) {
291 /* Middle of list? */
292 for (t
= timeouts
; t
->next
; t
= t
->next
) {
293 if (t
->next
->when
> q
->when
) {
306 cancel_timeout(void (*where
)(void *), void *what
)
308 struct timeout
*t
, *q
;
310 /* Look for this timeout on the list, and unlink it if we find it. */
312 for (q
= timeouts
; q
; q
= q
->next
) {
313 if (q
->func
== where
&& q
->what
== what
) {
323 /* If we found the timeout, put it on the free list. */
325 q
->next
= free_timeouts
;
330 /* Add a protocol to the list of protocols... */
332 add_protocol(char *name
, int fd
, void (*handler
)(struct protocol
*),
337 p
= malloc(sizeof(*p
));
339 error("can't allocate protocol struct for %s", name
);
342 p
->handler
= handler
;
349 remove_protocol(struct protocol
*proto
)
351 struct protocol
*p
, *next
, *prev
;
354 for (p
= protocols
; p
; p
= next
) {
358 prev
->next
= p
->next
;
367 find_protocol_by_adapter(struct interface_info
*info
)
371 for( p
= protocols
; p
; p
= p
->next
) {
372 if( p
->local
== (void *)info
) return p
;
379 interface_link_status(char *ifname
)
382 struct ifmediareq ifmr
;
385 if ((sock
= socket(AF_INET
, SOCK_DGRAM
, 0)) == -1)
386 error("Can't create socket");
388 memset(&ifmr
, 0, sizeof(ifmr
));
389 strlcpy(ifmr
.ifm_name
, ifname
, sizeof(ifmr
.ifm_name
));
390 if (ioctl(sock
, SIOCGIFMEDIA
, (caddr_t
)&ifmr
) == -1) {
391 /* EINVAL -> link state unknown. treat as active */
393 syslog(LOG_DEBUG
, "ioctl(SIOCGIFMEDIA) on %s: %m",
400 if (ifmr
.ifm_status
& IFM_AVALID
) {
401 if ((ifmr
.ifm_active
& IFM_NMASK
) == IFM_ETHER
) {
402 if (ifmr
.ifm_status
& IFM_ACTIVE
)