xref: /openbsd-src/usr.sbin/dhcrelay/dispatch.c (revision c4532b7fec20f8fcb12c565244393847efdf29a0)
1*c4532b7fSclaudio /*	$OpenBSD: dispatch.c,v 1.23 2021/01/17 13:40:59 claudio Exp $	*/
248be18b4Shenning 
348be18b4Shenning /*
448be18b4Shenning  * Copyright 2004 Henning Brauer <henning@openbsd.org>
548be18b4Shenning  * Copyright (c) 1995, 1996, 1997, 1998, 1999
648be18b4Shenning  * The Internet Software Consortium.   All rights reserved.
748be18b4Shenning  *
848be18b4Shenning  * Redistribution and use in source and binary forms, with or without
948be18b4Shenning  * modification, are permitted provided that the following conditions
1048be18b4Shenning  * are met:
1148be18b4Shenning  *
1248be18b4Shenning  * 1. Redistributions of source code must retain the above copyright
1348be18b4Shenning  *    notice, this list of conditions and the following disclaimer.
1448be18b4Shenning  * 2. Redistributions in binary form must reproduce the above copyright
1548be18b4Shenning  *    notice, this list of conditions and the following disclaimer in the
1648be18b4Shenning  *    documentation and/or other materials provided with the distribution.
1748be18b4Shenning  * 3. Neither the name of The Internet Software Consortium nor the names
1848be18b4Shenning  *    of its contributors may be used to endorse or promote products derived
1948be18b4Shenning  *    from this software without specific prior written permission.
2048be18b4Shenning  *
2148be18b4Shenning  * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
2248be18b4Shenning  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
2348be18b4Shenning  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
2448be18b4Shenning  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2548be18b4Shenning  * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
2648be18b4Shenning  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2748be18b4Shenning  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2848be18b4Shenning  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
2948be18b4Shenning  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
3048be18b4Shenning  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
3148be18b4Shenning  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
3248be18b4Shenning  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3348be18b4Shenning  * SUCH DAMAGE.
3448be18b4Shenning  *
3548be18b4Shenning  * This software has been written for the Internet Software Consortium
3648be18b4Shenning  * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
3748be18b4Shenning  * Enterprises.  To learn more about the Internet Software Consortium,
3848be18b4Shenning  * see ``http://www.vix.com/isc''.  To learn more about Vixie
3948be18b4Shenning  * Enterprises, see ``http://www.vix.com''.
4048be18b4Shenning  */
4148be18b4Shenning 
42f70ef60cSkrw #include <sys/types.h>
4348be18b4Shenning #include <sys/ioctl.h>
44f70ef60cSkrw #include <sys/socket.h>
4548be18b4Shenning 
46f70ef60cSkrw #include <net/if.h>
47f70ef60cSkrw #include <net/if_dl.h>
4848be18b4Shenning #include <net/if_media.h>
494be048dcSreyk #include <net/if_types.h>
504be048dcSreyk 
51f70ef60cSkrw #include <netinet/in.h>
52962cae8eSrzalamena #include <netinet/if_ether.h>
53f70ef60cSkrw 
54f70ef60cSkrw #include <errno.h>
5548be18b4Shenning #include <ifaddrs.h>
56f70ef60cSkrw #include <limits.h>
5748be18b4Shenning #include <poll.h>
58f70ef60cSkrw #include <stdlib.h>
59f70ef60cSkrw #include <string.h>
60f70ef60cSkrw #include <syslog.h>
61579e3f2dSguenther #include <time.h>
62f70ef60cSkrw #include <unistd.h>
63f70ef60cSkrw 
64f70ef60cSkrw #include "dhcp.h"
65f70ef60cSkrw #include "dhcpd.h"
66986dbb4cSkrw #include "log.h"
6748be18b4Shenning 
68962cae8eSrzalamena /*
69962cae8eSrzalamena  * Macros implementation used to generate link-local addresses. This
70962cae8eSrzalamena  * code was copied from: sys/netinet6/in6_ifattach.c.
71962cae8eSrzalamena  */
72962cae8eSrzalamena #define EUI64_UBIT		0x02
73962cae8eSrzalamena #define EUI64_TO_IFID(in6) \
74962cae8eSrzalamena 	do { (in6)->s6_addr[8] ^= EUI64_UBIT; } while (0)
75962cae8eSrzalamena 
7648be18b4Shenning struct protocol *protocols;
7748be18b4Shenning struct timeout *timeouts;
7848be18b4Shenning static struct timeout *free_timeouts;
7948be18b4Shenning static int interfaces_invalidated;
80cb13f214Sderaadt 
8148be18b4Shenning void (*bootp_packet_handler)(struct interface_info *,
82fa3d4f89Srzalamena     struct dhcp_packet *, int, struct packet_ctx *);
8348be18b4Shenning 
8448be18b4Shenning static int interface_status(struct interface_info *ifinfo);
8548be18b4Shenning 
86ffc715d7Srzalamena struct interface_info *
iflist_getbyname(const char * name)87962cae8eSrzalamena iflist_getbyname(const char *name)
8848be18b4Shenning {
89962cae8eSrzalamena 	struct interface_info	*intf;
90962cae8eSrzalamena 
91962cae8eSrzalamena 	TAILQ_FOREACH(intf, &intflist, entry) {
92962cae8eSrzalamena 		if (strcmp(intf->name, name) != 0)
93962cae8eSrzalamena 			continue;
94962cae8eSrzalamena 
95962cae8eSrzalamena 		return intf;
96962cae8eSrzalamena 	}
97962cae8eSrzalamena 
98962cae8eSrzalamena 	return NULL;
99962cae8eSrzalamena }
100962cae8eSrzalamena 
101962cae8eSrzalamena void
setup_iflist(void)102962cae8eSrzalamena setup_iflist(void)
103962cae8eSrzalamena {
104962cae8eSrzalamena 	struct interface_info		*intf;
105962cae8eSrzalamena 	struct sockaddr_dl		*sdl;
10648be18b4Shenning 	struct ifaddrs			*ifap, *ifa;
107962cae8eSrzalamena 	struct if_data			*ifi;
108a29cd94bSrzalamena 	struct sockaddr_in		*sin;
109962cae8eSrzalamena 	struct sockaddr_in6		*sin6;
110ffc715d7Srzalamena 
111962cae8eSrzalamena 	TAILQ_INIT(&intflist);
112962cae8eSrzalamena 	if (getifaddrs(&ifap))
113986dbb4cSkrw 		fatalx("getifaddrs failed");
11448be18b4Shenning 
11548be18b4Shenning 	for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
11648be18b4Shenning 		if ((ifa->ifa_flags & IFF_LOOPBACK) ||
117962cae8eSrzalamena 		    (ifa->ifa_flags & IFF_POINTOPOINT))
11848be18b4Shenning 			continue;
11948be18b4Shenning 
120962cae8eSrzalamena 		/* Find interface or create it. */
121962cae8eSrzalamena 		intf = iflist_getbyname(ifa->ifa_name);
122962cae8eSrzalamena 		if (intf == NULL) {
123962cae8eSrzalamena 			intf = calloc(1, sizeof(*intf));
124962cae8eSrzalamena 			if (intf == NULL)
125962cae8eSrzalamena 				fatal("calloc");
12648be18b4Shenning 
127962cae8eSrzalamena 			strlcpy(intf->name, ifa->ifa_name,
128962cae8eSrzalamena 			    sizeof(intf->name));
129962cae8eSrzalamena 			TAILQ_INSERT_HEAD(&intflist, intf, entry);
130962cae8eSrzalamena 		}
131a29cd94bSrzalamena 
132962cae8eSrzalamena 		/* Signal disabled interface. */
133962cae8eSrzalamena 		if ((ifa->ifa_flags & IFF_UP) == 0)
134962cae8eSrzalamena 			intf->dead = 1;
135962cae8eSrzalamena 
13648be18b4Shenning 		if (ifa->ifa_addr->sa_family == AF_LINK) {
137962cae8eSrzalamena 			sdl = (struct sockaddr_dl *)ifa->ifa_addr;
138962cae8eSrzalamena 			ifi = (struct if_data *)ifa->ifa_data;
139cb13f214Sderaadt 
1408d53af64Sreyk 			/* Skip unsupported interfaces. */
141962cae8eSrzalamena 			if (ifi->ifi_type != IFT_ETHER &&
1428d53af64Sreyk 			    ifi->ifi_type != IFT_ENC &&
1438d53af64Sreyk 			    ifi->ifi_type != IFT_CARP) {
144962cae8eSrzalamena 				TAILQ_REMOVE(&intflist, intf, entry);
145962cae8eSrzalamena 				free(intf);
146962cae8eSrzalamena 				continue;
147962cae8eSrzalamena 			}
148962cae8eSrzalamena 
1498d53af64Sreyk 			if (ifi->ifi_type == IFT_ENC)
150962cae8eSrzalamena 				intf->hw_address.htype = HTYPE_IPSEC_TUNNEL;
1518d53af64Sreyk 			else
1528d53af64Sreyk 				intf->hw_address.htype = HTYPE_ETHER;
153962cae8eSrzalamena 
154962cae8eSrzalamena 			intf->index = sdl->sdl_index;
155962cae8eSrzalamena 			intf->hw_address.hlen = sdl->sdl_alen;
156962cae8eSrzalamena 			memcpy(intf->hw_address.haddr,
157962cae8eSrzalamena 			    LLADDR(sdl), sdl->sdl_alen);
15848be18b4Shenning 		} else if (ifa->ifa_addr->sa_family == AF_INET) {
159a29cd94bSrzalamena 			sin = (struct sockaddr_in *)ifa->ifa_addr;
160962cae8eSrzalamena 			if (sin->sin_addr.s_addr == htonl(INADDR_LOOPBACK) ||
161962cae8eSrzalamena 			    intf->primary_address.s_addr != INADDR_ANY)
162a29cd94bSrzalamena 				continue;
163a29cd94bSrzalamena 
164962cae8eSrzalamena 			intf->primary_address = sin->sin_addr;
165962cae8eSrzalamena 		} else if (ifa->ifa_addr->sa_family == AF_INET6) {
166962cae8eSrzalamena 			sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
167962cae8eSrzalamena 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
168962cae8eSrzalamena 				intf->linklocal = sin6->sin6_addr;
169*c4532b7fSclaudio #ifdef __KAME__
170*c4532b7fSclaudio 				/*
171*c4532b7fSclaudio 				 * Remove possible scope from address if
172*c4532b7fSclaudio 				 * link-local.
173*c4532b7fSclaudio 				 */
174962cae8eSrzalamena 				intf->linklocal.s6_addr[2] = 0;
175962cae8eSrzalamena 				intf->linklocal.s6_addr[3] = 0;
176*c4532b7fSclaudio #endif
177962cae8eSrzalamena 			} else
178962cae8eSrzalamena 				intf->gipv6 = 1;
179962cae8eSrzalamena 
180962cae8eSrzalamena 			/* At least one IPv6 address was found. */
181962cae8eSrzalamena 			intf->ipv6 = 1;
18248be18b4Shenning 		}
18348be18b4Shenning 	}
18448be18b4Shenning 
185ffc715d7Srzalamena 	freeifaddrs(ifap);
186ffc715d7Srzalamena 
187962cae8eSrzalamena 	/*
188962cae8eSrzalamena 	 * Generate link-local IPv6 address for interfaces without it.
189962cae8eSrzalamena 	 *
190962cae8eSrzalamena 	 * For IPv6 DHCP Relay it doesn't matter what is used for
191962cae8eSrzalamena 	 * link-addr field, so let's generate an address that won't
192962cae8eSrzalamena 	 * change during execution so we can always find the interface
193962cae8eSrzalamena 	 * to relay packets back. This is only used for layer 2 relaying
194962cae8eSrzalamena 	 * when the interface might not have an address.
195962cae8eSrzalamena 	 */
196962cae8eSrzalamena 	TAILQ_FOREACH(intf, &intflist, entry) {
197962cae8eSrzalamena 		if (memcmp(&intf->linklocal, &in6addr_any,
198962cae8eSrzalamena 		    sizeof(in6addr_any)) != 0)
199962cae8eSrzalamena 			continue;
200962cae8eSrzalamena 
201962cae8eSrzalamena 		intf->linklocal.s6_addr[0] = 0xfe;
202962cae8eSrzalamena 		intf->linklocal.s6_addr[1] = 0x80;
203962cae8eSrzalamena 		intf->linklocal.s6_addr[8] = intf->hw_address.haddr[0];
204962cae8eSrzalamena 		intf->linklocal.s6_addr[9] = intf->hw_address.haddr[1];
205962cae8eSrzalamena 		intf->linklocal.s6_addr[10] = intf->hw_address.haddr[2];
206962cae8eSrzalamena 		intf->linklocal.s6_addr[11] = 0xff;
207962cae8eSrzalamena 		intf->linklocal.s6_addr[12] = 0xfe;
208962cae8eSrzalamena 		intf->linklocal.s6_addr[13] = intf->hw_address.haddr[3];
209962cae8eSrzalamena 		intf->linklocal.s6_addr[14] = intf->hw_address.haddr[4];
210962cae8eSrzalamena 		intf->linklocal.s6_addr[15] = intf->hw_address.haddr[5];
211962cae8eSrzalamena 		EUI64_TO_IFID(&intf->linklocal);
212962cae8eSrzalamena 	}
213a29cd94bSrzalamena }
214a29cd94bSrzalamena 
215962cae8eSrzalamena struct interface_info *
register_interface(const char * ifname,void (* handler)(struct protocol *),int isserver)216962cae8eSrzalamena register_interface(const char *ifname, void (*handler)(struct protocol *),
217962cae8eSrzalamena     int isserver)
218962cae8eSrzalamena {
219962cae8eSrzalamena 	struct interface_info		*intf;
220962cae8eSrzalamena 
221962cae8eSrzalamena 	if ((intf = iflist_getbyname(ifname)) == NULL)
222962cae8eSrzalamena 		return NULL;
223962cae8eSrzalamena 
224962cae8eSrzalamena 	/* Don't register disabled interfaces. */
225962cae8eSrzalamena 	if (intf->dead)
226962cae8eSrzalamena 		return NULL;
227962cae8eSrzalamena 
228962cae8eSrzalamena 	/* Check if we already registered the interface. */
229962cae8eSrzalamena 	if (intf->ifr.ifr_name[0] != 0)
230962cae8eSrzalamena 		return intf;
231962cae8eSrzalamena 
232962cae8eSrzalamena 	if (strlcpy(intf->ifr.ifr_name, ifname,
233962cae8eSrzalamena 	    sizeof(intf->ifr.ifr_name)) >= sizeof(intf->ifr.ifr_name))
234986dbb4cSkrw 		fatalx("interface name '%s' too long", ifname);
23548be18b4Shenning 
236962cae8eSrzalamena 	if_register_receive(intf, isserver);
237962cae8eSrzalamena 	if_register_send(intf);
238962cae8eSrzalamena 	add_protocol(intf->name, intf->rfdesc, handler, intf);
239ffc715d7Srzalamena 
240962cae8eSrzalamena 	return intf;
24148be18b4Shenning }
24248be18b4Shenning 
24348be18b4Shenning /*
24448be18b4Shenning  * Wait for packets to come in using poll().  When a packet comes in,
24548be18b4Shenning  * call receive_packet to receive the packet and possibly strip hardware
24648be18b4Shenning  * addressing information from it, and then call through the
24748be18b4Shenning  * bootp_packet_handler hook to try to do something with it.
24848be18b4Shenning  */
24948be18b4Shenning void
dispatch(void)25048be18b4Shenning dispatch(void)
25148be18b4Shenning {
25248be18b4Shenning 	int count, i, to_msec, nfds = 0;
25348be18b4Shenning 	struct protocol *l;
25448be18b4Shenning 	struct pollfd *fds;
25548be18b4Shenning 	time_t howlong;
25648be18b4Shenning 
25748be18b4Shenning 	nfds = 0;
25848be18b4Shenning 	for (l = protocols; l; l = l->next)
25948be18b4Shenning 		nfds++;
26048be18b4Shenning 
2611ed98fdfSderaadt 	fds = calloc(nfds, sizeof(struct pollfd));
26248be18b4Shenning 	if (fds == NULL)
263986dbb4cSkrw 		fatalx("Can't allocate poll structures.");
26448be18b4Shenning 
26548be18b4Shenning 	do {
26648be18b4Shenning 		/*
26748be18b4Shenning 		 * Call any expired timeouts, and then if there's still
26848be18b4Shenning 		 * a timeout registered, time out the select call then.
26948be18b4Shenning 		 */
27048be18b4Shenning another:
27148be18b4Shenning 		if (timeouts) {
27248be18b4Shenning 			if (timeouts->when <= cur_time) {
273cb13f214Sderaadt 				struct timeout *t = timeouts;
274cb13f214Sderaadt 
27548be18b4Shenning 				timeouts = timeouts->next;
27648be18b4Shenning 				(*(t->func))(t->what);
27748be18b4Shenning 				t->next = free_timeouts;
27848be18b4Shenning 				free_timeouts = t;
27948be18b4Shenning 				goto another;
28048be18b4Shenning 			}
28148be18b4Shenning 
28248be18b4Shenning 			/*
28348be18b4Shenning 			 * Figure timeout in milliseconds, and check for
28448be18b4Shenning 			 * potential overflow, so we can cram into an
28548be18b4Shenning 			 * int for poll, while not polling with a
286fd9f780dSdavid 			 * negative timeout and blocking indefinitely.
28748be18b4Shenning 			 */
28848be18b4Shenning 			howlong = timeouts->when - cur_time;
28948be18b4Shenning 			if (howlong > INT_MAX / 1000)
29048be18b4Shenning 				howlong = INT_MAX / 1000;
29148be18b4Shenning 			to_msec = howlong * 1000;
29248be18b4Shenning 		} else
29348be18b4Shenning 			to_msec = -1;
29448be18b4Shenning 
29548be18b4Shenning 		/* Set up the descriptors to be polled. */
29648be18b4Shenning 		i = 0;
29748be18b4Shenning 
29848be18b4Shenning 		for (l = protocols; l; l = l->next) {
29948be18b4Shenning 			struct interface_info *ip = l->local;
300cb13f214Sderaadt 
30148be18b4Shenning 			if (ip && (l->handler != got_one || !ip->dead)) {
30248be18b4Shenning 				fds[i].fd = l->fd;
30348be18b4Shenning 				fds[i].events = POLLIN;
30448be18b4Shenning 				fds[i].revents = 0;
30548be18b4Shenning 				i++;
30648be18b4Shenning 			}
30748be18b4Shenning 		}
30848be18b4Shenning 
30948be18b4Shenning 		if (i == 0)
310986dbb4cSkrw 			fatalx("No live interfaces to poll on - exiting.");
31148be18b4Shenning 
31248be18b4Shenning 		/* Wait for a packet or a timeout... XXX */
31348be18b4Shenning 		count = poll(fds, nfds, to_msec);
31448be18b4Shenning 
31548be18b4Shenning 		/* Not likely to be transitory... */
31648be18b4Shenning 		if (count == -1) {
31748be18b4Shenning 			if (errno == EAGAIN || errno == EINTR) {
31848be18b4Shenning 				time(&cur_time);
31948be18b4Shenning 				continue;
32048be18b4Shenning 			}
32148be18b4Shenning 			else
322d8cc3220Skrw 				fatal("poll");
32348be18b4Shenning 		}
32448be18b4Shenning 
32548be18b4Shenning 		/* Get the current time... */
32648be18b4Shenning 		time(&cur_time);
32748be18b4Shenning 
32848be18b4Shenning 		i = 0;
32948be18b4Shenning 		for (l = protocols; l; l = l->next) {
330cb13f214Sderaadt 			struct interface_info *ip = l->local;
331cb13f214Sderaadt 
33287cb6619Scanacar 			if ((fds[i].revents & (POLLIN | POLLHUP))) {
33348be18b4Shenning 				fds[i].revents = 0;
33448be18b4Shenning 				if (ip && (l->handler != got_one ||
33548be18b4Shenning 				    !ip->dead))
33648be18b4Shenning 					(*(l->handler))(l);
33748be18b4Shenning 				if (interfaces_invalidated)
33848be18b4Shenning 					break;
33948be18b4Shenning 			}
34048be18b4Shenning 			i++;
34148be18b4Shenning 		}
34248be18b4Shenning 		interfaces_invalidated = 0;
34348be18b4Shenning 	} while (1);
34448be18b4Shenning }
34548be18b4Shenning 
34648be18b4Shenning 
34748be18b4Shenning void
got_one(struct protocol * l)34848be18b4Shenning got_one(struct protocol *l)
34948be18b4Shenning {
350fa3d4f89Srzalamena 	struct packet_ctx pc;
35172bd338bSreyk 	ssize_t result;
35248be18b4Shenning 	union {
35348be18b4Shenning 		/*
35448be18b4Shenning 		 * Packet input buffer.  Must be as large as largest
35548be18b4Shenning 		 * possible MTU.
35648be18b4Shenning 		 */
35748be18b4Shenning 		unsigned char packbuf[4095];
35848be18b4Shenning 		struct dhcp_packet packet;
35948be18b4Shenning 	} u;
36048be18b4Shenning 	struct interface_info *ip = l->local;
36148be18b4Shenning 
362fa3d4f89Srzalamena 	memset(&pc, 0, sizeof(pc));
363fa3d4f89Srzalamena 
364fa3d4f89Srzalamena 	if ((result = receive_packet(ip, u.packbuf, sizeof(u), &pc)) == -1) {
36588f6d33eSkrw 		log_warn("receive_packet failed on %s", ip->name);
36648be18b4Shenning 		ip->errors++;
36748be18b4Shenning 		if ((!interface_status(ip)) ||
36848be18b4Shenning 		    (ip->noifmedia && ip->errors > 20)) {
36948be18b4Shenning 			/* our interface has gone away. */
370986dbb4cSkrw 			log_warnx("Interface %s no longer appears valid.",
37148be18b4Shenning 			    ip->name);
37248be18b4Shenning 			ip->dead = 1;
37348be18b4Shenning 			interfaces_invalidated = 1;
37448be18b4Shenning 			close(l->fd);
37548be18b4Shenning 			remove_protocol(l);
37648be18b4Shenning 			free(ip);
37748be18b4Shenning 		}
37848be18b4Shenning 		return;
37948be18b4Shenning 	}
38048be18b4Shenning 	if (result == 0)
38148be18b4Shenning 		return;
38248be18b4Shenning 
383fa3d4f89Srzalamena 	if (bootp_packet_handler)
384fa3d4f89Srzalamena 		(*bootp_packet_handler)(ip, &u.packet, result, &pc);
38548be18b4Shenning }
38648be18b4Shenning 
38748be18b4Shenning int
interface_status(struct interface_info * ifinfo)38848be18b4Shenning interface_status(struct interface_info *ifinfo)
38948be18b4Shenning {
39048be18b4Shenning 	char *ifname = ifinfo->name;
39148be18b4Shenning 	int ifsock = ifinfo->rfdesc;
39248be18b4Shenning 	struct ifreq ifr;
39348be18b4Shenning 	struct ifmediareq ifmr;
39448be18b4Shenning 
39548be18b4Shenning 	/* get interface flags */
39648be18b4Shenning 	memset(&ifr, 0, sizeof(ifr));
39748be18b4Shenning 	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
39823ccc430Sclaudio 	if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) == -1) {
399d8cc3220Skrw 		log_warn("ioctl(SIOCGIFFLAGS) on %s", ifname);
40048be18b4Shenning 		goto inactive;
40148be18b4Shenning 	}
40248be18b4Shenning 	/*
40348be18b4Shenning 	 * if one of UP and RUNNING flags is dropped,
40448be18b4Shenning 	 * the interface is not active.
40548be18b4Shenning 	 */
40648be18b4Shenning 	if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
40748be18b4Shenning 		goto inactive;
40848be18b4Shenning 	}
40948be18b4Shenning 	/* Next, check carrier on the interface, if possible */
41048be18b4Shenning 	if (ifinfo->noifmedia)
41148be18b4Shenning 		goto active;
41248be18b4Shenning 	memset(&ifmr, 0, sizeof(ifmr));
41348be18b4Shenning 	strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
41423ccc430Sclaudio 	if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
41548be18b4Shenning 		if (errno != EINVAL) {
416d8cc3220Skrw 			log_debug("ioctl(SIOCGIFMEDIA) on %s", ifname);
41748be18b4Shenning 			ifinfo->noifmedia = 1;
41848be18b4Shenning 			goto active;
41948be18b4Shenning 		}
42048be18b4Shenning 		/*
42148be18b4Shenning 		 * EINVAL (or ENOTTY) simply means that the interface
42248be18b4Shenning 		 * does not support the SIOCGIFMEDIA ioctl. We regard it alive.
42348be18b4Shenning 		 */
42448be18b4Shenning 		ifinfo->noifmedia = 1;
42548be18b4Shenning 		goto active;
42648be18b4Shenning 	}
42748be18b4Shenning 	if (ifmr.ifm_status & IFM_AVALID) {
42848be18b4Shenning 		switch (ifmr.ifm_active & IFM_NMASK) {
42948be18b4Shenning 		case IFM_ETHER:
43048be18b4Shenning 			if (ifmr.ifm_status & IFM_ACTIVE)
43148be18b4Shenning 				goto active;
43248be18b4Shenning 			else
43348be18b4Shenning 				goto inactive;
43448be18b4Shenning 			break;
43548be18b4Shenning 		default:
43648be18b4Shenning 			goto inactive;
43748be18b4Shenning 		}
43848be18b4Shenning 	}
43948be18b4Shenning inactive:
44048be18b4Shenning 	return (0);
44148be18b4Shenning active:
44248be18b4Shenning 	return (1);
44348be18b4Shenning }
44448be18b4Shenning 
44548be18b4Shenning /* Add a protocol to the list of protocols... */
44648be18b4Shenning void
add_protocol(char * name,int fd,void (* handler)(struct protocol *),void * local)44748be18b4Shenning add_protocol(char *name, int fd, void (*handler)(struct protocol *),
44848be18b4Shenning     void *local)
44948be18b4Shenning {
45048be18b4Shenning 	struct protocol *p;
45148be18b4Shenning 
45248be18b4Shenning 	p = malloc(sizeof(*p));
45348be18b4Shenning 	if (!p)
454986dbb4cSkrw 		fatalx("can't allocate protocol struct for %s", name);
45548be18b4Shenning 
45648be18b4Shenning 	p->fd = fd;
45748be18b4Shenning 	p->handler = handler;
45848be18b4Shenning 	p->local = local;
45948be18b4Shenning 	p->next = protocols;
46048be18b4Shenning 	protocols = p;
46148be18b4Shenning }
46248be18b4Shenning 
46348be18b4Shenning void
remove_protocol(struct protocol * proto)46448be18b4Shenning remove_protocol(struct protocol *proto)
46548be18b4Shenning {
46648be18b4Shenning 	struct protocol *p, *next, *prev;
46748be18b4Shenning 
46848be18b4Shenning 	prev = NULL;
46948be18b4Shenning 	for (p = protocols; p; p = next) {
47048be18b4Shenning 		next = p->next;
47148be18b4Shenning 		if (p == proto) {
47248be18b4Shenning 			if (prev)
47348be18b4Shenning 				prev->next = p->next;
47448be18b4Shenning 			else
47548be18b4Shenning 				protocols = p->next;
47648be18b4Shenning 			free(p);
47748be18b4Shenning 		}
47848be18b4Shenning 	}
47948be18b4Shenning }
480