121daa9937be5665f5ea0e98824c648d5dd0fea0
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
)
70 PDHCP_ADAPTER Adapter
= AdapterFindInfo( iface
);
72 if_register_receive(iface
);
73 if_register_send(iface
);
75 if( Adapter
->DhclientState
.state
!= S_STATIC
) {
76 add_protocol(iface
->name
, iface
->rfdesc
, got_one
, iface
);
77 iface
->client
->state
= S_INIT
;
83 reinitialize_interfaces(void)
85 interfaces_invalidated
= 1;
89 * Wait for packets to come in using poll(). When a packet comes in,
90 * call receive_packet to receive the packet and possibly strip hardware
91 * addressing information from it, and then call through the
92 * bootp_packet_handler hook to try to do something with it.
97 int count
, i
, to_msec
, nfds
= 0;
101 struct timeval timeval
;
105 for (l
= protocols
; l
; l
= l
->next
)
112 * Call any expired timeouts, and then if there's still
113 * a timeout registered, time out the select call then.
119 if (timeouts
->when
<= cur_time
) {
121 timeouts
= timeouts
->next
;
122 (*(t
->func
))(t
->what
);
123 t
->next
= free_timeouts
;
129 * Figure timeout in milliseconds, and check for
130 * potential overflow, so we can cram into an
131 * int for poll, while not polling with a
132 * negative timeout and blocking indefinitely.
134 howlong
= timeouts
->when
- cur_time
;
135 if (howlong
> INT_MAX
/ 1000)
136 howlong
= INT_MAX
/ 1000;
137 to_msec
= howlong
* 1000;
141 /* Set up the descriptors to be polled. */
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
)) {
152 /* No interfaces for now, set the select timeout reasonably so
153 * we can recover from that condition later. */
157 /* Wait for a packet or a timeout... XXX */
158 timeval
.tv_sec
= to_msec
/ 1000;
159 timeval
.tv_usec
= (to_msec
% 1000) * 1000;
164 count
= select(nfds
, &fds
, NULL
, NULL
, &timeval
);
166 DH_DbgPrint(MID_TRACE
,("Select: %d\n", count
));
168 /* Review poll output */
169 for (i
= 0, l
= protocols
; l
; l
= l
->next
) {
170 struct interface_info
*ip
= l
->local
;
172 if (ip
&& (l
->handler
!= got_one
|| !ip
->dead
)) {
176 l
->fd
, FD_ISSET(l
->fd
, &fds
) ? "true" : "false"));
184 /* Not likely to be transitory... */
185 if (count
== SOCKET_ERROR
) {
186 if (errno
== EAGAIN
|| errno
== EINTR
) {
193 /* Get the current time... */
197 for (l
= protocols
; l
; l
= l
->next
) {
198 struct interface_info
*ip
;
200 if (FD_ISSET(l
->fd
, &fds
)) {
201 if (ip
&& (l
->handler
!= got_one
||
203 DH_DbgPrint(MID_TRACE
,("Handling %x\n", l
));
205 if (interfaces_invalidated
)
210 interfaces_invalidated
= 0;
214 ApiUnlock(); /* Not reached currently */
218 got_one(struct protocol
*l
)
220 struct sockaddr_in from
;
221 struct hardware hfrom
;
226 * Packet input buffer. Must be as large as largest
229 unsigned char packbuf
[4095];
230 struct dhcp_packet packet
;
232 struct interface_info
*ip
= l
->local
;
234 if ((result
= receive_packet(ip
, u
.packbuf
, sizeof(u
), &from
,
236 warning("receive_packet failed on %s: %s", ip
->name
,
239 if ((!interface_status(ip
)) ||
240 (ip
->noifmedia
&& ip
->errors
> 20)) {
241 /* our interface has gone away. */
242 warning("Interface %s no longer appears valid.",
245 interfaces_invalidated
= 1;
255 if (bootp_packet_handler
) {
257 memcpy(ifrom
.iabuf
, &from
.sin_addr
, ifrom
.len
);
259 (*bootp_packet_handler
)(ip
, &u
.packet
, result
,
260 from
.sin_port
, ifrom
, &hfrom
);
266 interface_status(struct interface_info
*ifinfo
)
268 char *ifname
= ifinfo
->name
;
269 int ifsock
= ifinfo
->rfdesc
;
271 struct ifmediareq ifmr
;
273 /* get interface flags */
274 memset(&ifr
, 0, sizeof(ifr
));
275 strlcpy(ifr
.ifr_name
, ifname
, sizeof(ifr
.ifr_name
));
276 if (ioctl(ifsock
, SIOCGIFFLAGS
, &ifr
) < 0) {
277 syslog(LOG_ERR
, "ioctl(SIOCGIFFLAGS) on %s: %m", ifname
);
282 * if one of UP and RUNNING flags is dropped,
283 * the interface is not active.
285 if ((ifr
.ifr_flags
& (IFF_UP
|IFF_RUNNING
)) != (IFF_UP
|IFF_RUNNING
))
288 /* Next, check carrier on the interface, if possible */
289 if (ifinfo
->noifmedia
)
291 memset(&ifmr
, 0, sizeof(ifmr
));
292 strlcpy(ifmr
.ifm_name
, ifname
, sizeof(ifmr
.ifm_name
));
293 if (ioctl(ifsock
, SIOCGIFMEDIA
, (caddr_t
)&ifmr
) < 0) {
294 if (errno
!= EINVAL
) {
295 syslog(LOG_DEBUG
, "ioctl(SIOCGIFMEDIA) on %s: %m",
298 ifinfo
->noifmedia
= 1;
302 * EINVAL (or ENOTTY) simply means that the interface
303 * does not support the SIOCGIFMEDIA ioctl. We regard it alive.
305 ifinfo
->noifmedia
= 1;
308 if (ifmr
.ifm_status
& IFM_AVALID
) {
309 switch (ifmr
.ifm_active
& IFM_NMASK
) {
311 if (ifmr
.ifm_status
& IFM_ACTIVE
)
327 interface_status(struct interface_info
*ifinfo
)
334 add_timeout(time_t when
, void (*where
)(void *), void *what
)
336 struct timeout
*t
, *q
;
338 DH_DbgPrint(MID_TRACE
,("Adding timeout %x %p %x\n", when
, where
, what
));
339 /* See if this timeout supersedes an existing timeout. */
341 for (q
= timeouts
; q
; q
= q
->next
) {
342 if (q
->func
== where
&& q
->what
== what
) {
352 /* If we didn't supersede a timeout, allocate a timeout
357 free_timeouts
= q
->next
;
361 q
= malloc(sizeof(struct timeout
));
363 error("Can't allocate timeout structure!");
371 /* Now sort this timeout into the timeout list. */
373 /* Beginning of list? */
374 if (!timeouts
|| timeouts
->when
> q
->when
) {
380 /* Middle of list? */
381 for (t
= timeouts
; t
->next
; t
= t
->next
) {
382 if (t
->next
->when
> q
->when
) {
395 cancel_timeout(void (*where
)(void *), void *what
)
397 struct timeout
*t
, *q
;
399 /* Look for this timeout on the list, and unlink it if we find it. */
401 for (q
= timeouts
; q
; q
= q
->next
) {
402 if (q
->func
== where
&& q
->what
== what
) {
412 /* If we found the timeout, put it on the free list. */
414 q
->next
= free_timeouts
;
419 /* Add a protocol to the list of protocols... */
421 add_protocol(char *name
, int fd
, void (*handler
)(struct protocol
*),
426 p
= malloc(sizeof(*p
));
428 error("can't allocate protocol struct for %s", name
);
431 p
->handler
= handler
;
438 remove_protocol(struct protocol
*proto
)
440 struct protocol
*p
, *next
, *prev
;
443 for (p
= protocols
; p
; p
= next
) {
447 prev
->next
= p
->next
;
456 find_protocol_by_adapter(struct interface_info
*info
)
460 for( p
= protocols
; p
; p
= p
->next
) {
461 if( p
->local
== (void *)info
) return p
;
468 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 syslog(LOG_DEBUG
, "ioctl(SIOCGIFMEDIA) on %s: %m",
489 if (ifmr
.ifm_status
& IFM_AVALID
) {
490 if ((ifmr
.ifm_active
& IFM_NMASK
) == IFM_ETHER
) {
491 if (ifmr
.ifm_status
& IFM_ACTIVE
)