xref: /openbsd-src/usr.sbin/dhcpd/dispatch.c (revision a514d4594e28eba7483096cbe8dfa64a3b82b617)
1*a514d459Skn /*	$OpenBSD: dispatch.c,v 1.45 2023/09/02 10:18:45 kn Exp $ */
2e853bc5dShenning 
3e853bc5dShenning /*
4e853bc5dShenning  * Copyright (c) 1995, 1996, 1997, 1998, 1999
5e853bc5dShenning  * The Internet Software Consortium.   All rights reserved.
6e853bc5dShenning  *
7e853bc5dShenning  * Redistribution and use in source and binary forms, with or without
8e853bc5dShenning  * modification, are permitted provided that the following conditions
9e853bc5dShenning  * are met:
10e853bc5dShenning  *
11e853bc5dShenning  * 1. Redistributions of source code must retain the above copyright
12e853bc5dShenning  *    notice, this list of conditions and the following disclaimer.
13e853bc5dShenning  * 2. Redistributions in binary form must reproduce the above copyright
14e853bc5dShenning  *    notice, this list of conditions and the following disclaimer in the
15e853bc5dShenning  *    documentation and/or other materials provided with the distribution.
16e853bc5dShenning  * 3. Neither the name of The Internet Software Consortium nor the names
17e853bc5dShenning  *    of its contributors may be used to endorse or promote products derived
18e853bc5dShenning  *    from this software without specific prior written permission.
19e853bc5dShenning  *
20e853bc5dShenning  * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
21e853bc5dShenning  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22e853bc5dShenning  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23e853bc5dShenning  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24e853bc5dShenning  * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
25e853bc5dShenning  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26e853bc5dShenning  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27e853bc5dShenning  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28e853bc5dShenning  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29e853bc5dShenning  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30e853bc5dShenning  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31e853bc5dShenning  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32e853bc5dShenning  * SUCH DAMAGE.
33e853bc5dShenning  *
34e853bc5dShenning  * This software has been written for the Internet Software Consortium
35e853bc5dShenning  * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
36e853bc5dShenning  * Enterprises.  To learn more about the Internet Software Consortium,
37e853bc5dShenning  * see ``http://www.vix.com/isc''.  To learn more about Vixie
38e853bc5dShenning  * Enterprises, see ``http://www.vix.com''.
39e853bc5dShenning  */
40e853bc5dShenning 
41837cddffSkrw #include <sys/types.h>
42837cddffSkrw #include <sys/ioctl.h>
43837cddffSkrw #include <sys/socket.h>
44837cddffSkrw 
45837cddffSkrw #include <arpa/inet.h>
46837cddffSkrw 
47837cddffSkrw #include <net/if.h>
48837cddffSkrw #include <net/if_dl.h>
49837cddffSkrw #include <net/if_media.h>
50837cddffSkrw 
51837cddffSkrw #include <netinet/in.h>
52837cddffSkrw 
53837cddffSkrw #include <errno.h>
54837cddffSkrw #include <ifaddrs.h>
55837cddffSkrw #include <limits.h>
56837cddffSkrw #include <poll.h>
57837cddffSkrw #include <stdio.h>
58837cddffSkrw #include <stdlib.h>
59837cddffSkrw #include <string.h>
60837cddffSkrw #include <syslog.h>
61579e3f2dSguenther #include <time.h>
62837cddffSkrw #include <unistd.h>
63837cddffSkrw 
64837cddffSkrw #include "dhcp.h"
65837cddffSkrw #include "tree.h"
66e853bc5dShenning #include "dhcpd.h"
67c525a185Skrw #include "log.h"
685f515bebSbeck #include "sync.h"
69e853bc5dShenning 
705f515bebSbeck extern int syncfd;
715f515bebSbeck 
72cc3e8043Sclaudio struct interface_info *interfaces;
73e853bc5dShenning struct protocol *protocols;
741e15d177Sckuethe struct dhcpd_timeout *timeouts;
751e15d177Sckuethe static struct dhcpd_timeout *free_timeouts;
76e853bc5dShenning static int interfaces_invalidated;
77e853bc5dShenning 
78e853bc5dShenning static int interface_status(struct interface_info *ifinfo);
79daa83f4cSclaudio int get_rdomain(char *);
80e853bc5dShenning 
81e853bc5dShenning /* Use getifaddrs() to get a list of all the attached interfaces.
82e853bc5dShenning    For each interface that's of type INET and not the loopback interface,
83e853bc5dShenning    register that interface with the network I/O software, figure out what
84e853bc5dShenning    subnet it's on, and add it to the list of interfaces. */
85e853bc5dShenning 
86ea507cabSderaadt void
discover_interfaces(int * rdomain)87daa83f4cSclaudio discover_interfaces(int *rdomain)
88e853bc5dShenning {
89e853bc5dShenning 	struct interface_info *tmp;
90e853bc5dShenning 	struct interface_info *last, *next;
91e853bc5dShenning 	struct subnet *subnet;
92e853bc5dShenning 	struct shared_network *share;
93e853bc5dShenning 	struct sockaddr_in foo;
94daa83f4cSclaudio 	int ir = 0, ird;
95e853bc5dShenning 	struct ifreq *tif;
96e853bc5dShenning 	struct ifaddrs *ifap, *ifa;
97e853bc5dShenning 
98e853bc5dShenning 	if (getifaddrs(&ifap) != 0)
99c525a185Skrw 		fatalx("getifaddrs failed");
100e853bc5dShenning 
10197f5f068Skrw 	/*
10297f5f068Skrw 	 * If we already have a list of interfaces, the interfaces were
10397f5f068Skrw 	 * requested.
10497f5f068Skrw 	 */
10597f5f068Skrw 	if (interfaces != NULL)
10697f5f068Skrw 		ir = 1;
107daa83f4cSclaudio 	else
108daa83f4cSclaudio 		/* must specify an interface when rdomains are used */
109daa83f4cSclaudio 		*rdomain = 0;
110e853bc5dShenning 
111e853bc5dShenning 	/* Cycle through the list of interfaces looking for IP addresses. */
112e853bc5dShenning 	for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
113ea507cabSderaadt 		/*
114ea507cabSderaadt 		 * See if this is the sort of interface we want to
115*a514d459Skn 		 * deal with.
116ea507cabSderaadt 		 */
117e853bc5dShenning 		if ((ifa->ifa_flags & IFF_LOOPBACK) ||
118e853bc5dShenning 		    (ifa->ifa_flags & IFF_POINTOPOINT) ||
119255ca50cSajacoutot 		    (!(ifa->ifa_flags & IFF_BROADCAST)))
120e853bc5dShenning 			continue;
121e853bc5dShenning 
122e853bc5dShenning 		/* See if we've seen an interface that matches this one. */
123e853bc5dShenning 		for (tmp = interfaces; tmp; tmp = tmp->next)
124e853bc5dShenning 			if (!strcmp(tmp->name, ifa->ifa_name))
125e853bc5dShenning 				break;
126e853bc5dShenning 
12797f5f068Skrw 		/* If we are looking for specific interfaces, ignore others. */
12897f5f068Skrw 		if (tmp == NULL && ir)
12997f5f068Skrw 			continue;
13097f5f068Skrw 
131daa83f4cSclaudio 		ird = get_rdomain(ifa->ifa_name);
132daa83f4cSclaudio 		if (*rdomain == -1)
133daa83f4cSclaudio 			*rdomain = ird;
134daa83f4cSclaudio 		else if (*rdomain != ird && ir)
135c525a185Skrw 			fatalx("Interface %s is not in rdomain %d",
136daa83f4cSclaudio 			    tmp->name, *rdomain);
137daa83f4cSclaudio 		else if (*rdomain != ird && !ir)
138daa83f4cSclaudio 			continue;
139daa83f4cSclaudio 
140e853bc5dShenning 		/* If there isn't already an interface by this name,
141e853bc5dShenning 		   allocate one. */
14297f5f068Skrw 		if (tmp == NULL) {
143d60fc4a4Skrw 			tmp = calloc(1, sizeof *tmp);
144e853bc5dShenning 			if (!tmp)
145c525a185Skrw 				fatalx("Insufficient memory to %s %s",
146e853bc5dShenning 				    "record interface", ifa->ifa_name);
147e853bc5dShenning 			strlcpy(tmp->name, ifa->ifa_name, sizeof(tmp->name));
148e853bc5dShenning 			tmp->next = interfaces;
149e853bc5dShenning 			tmp->noifmedia = tmp->dead = tmp->errors = 0;
150e853bc5dShenning 			interfaces = tmp;
151e853bc5dShenning 		}
152e853bc5dShenning 
153e853bc5dShenning 		/* If we have the capability, extract link information
154e853bc5dShenning 		   and record it in a linked list. */
155e853bc5dShenning 		if (ifa->ifa_addr->sa_family == AF_LINK) {
156c0773b7bSkrw 			struct sockaddr_dl *sdl =
157ea507cabSderaadt 			    ((struct sockaddr_dl *)(ifa->ifa_addr));
158c0773b7bSkrw 			tmp->index = sdl->sdl_index;
159c0773b7bSkrw 			tmp->hw_address.hlen = sdl->sdl_alen;
160e853bc5dShenning 			tmp->hw_address.htype = HTYPE_ETHER; /* XXX */
161e853bc5dShenning 			memcpy(tmp->hw_address.haddr,
162c0773b7bSkrw 			    LLADDR(sdl), sdl->sdl_alen);
163e853bc5dShenning 		} else if (ifa->ifa_addr->sa_family == AF_INET) {
164e853bc5dShenning 			struct iaddr addr;
165e853bc5dShenning 
166e853bc5dShenning 			/* Get a pointer to the address... */
1671674d7d2Skrw 			memcpy(&foo, ifa->ifa_addr, sizeof(foo));
168e853bc5dShenning 
169e853bc5dShenning 			/* We don't want the loopback interface. */
170e853bc5dShenning 			if (foo.sin_addr.s_addr == htonl (INADDR_LOOPBACK))
171e853bc5dShenning 				continue;
172e853bc5dShenning 
173e853bc5dShenning 			/* If this is the first real IP address we've
174e853bc5dShenning 			   found, keep a pointer to ifreq structure in
175e853bc5dShenning 			   which we found it. */
176e853bc5dShenning 			if (!tmp->ifp) {
177ea507cabSderaadt 				int len = (IFNAMSIZ + ifa->ifa_addr->sa_len);
17835de856eSderaadt 				tif = malloc(len);
179e853bc5dShenning 				if (!tif)
180c525a185Skrw 					fatalx("no space to remember ifp.");
18135318e8fSkrw 				strlcpy(tif->ifr_name, ifa->ifa_name,
18235318e8fSkrw 				    IFNAMSIZ);
183e853bc5dShenning 				memcpy(&tif->ifr_addr, ifa->ifa_addr,
184e853bc5dShenning 				    ifa->ifa_addr->sa_len);
185e853bc5dShenning 				tmp->ifp = tif;
186e853bc5dShenning 				tmp->primary_address = foo.sin_addr;
187e853bc5dShenning 			}
188e853bc5dShenning 
189e853bc5dShenning 			/* Grab the address... */
190e853bc5dShenning 			addr.len = 4;
191ea507cabSderaadt 			memcpy(addr.iabuf, &foo.sin_addr.s_addr, addr.len);
192e853bc5dShenning 
193e853bc5dShenning 			/* If there's a registered subnet for this address,
194e853bc5dShenning 			   connect it together... */
195e853bc5dShenning 			if ((subnet = find_subnet(addr))) {
196e853bc5dShenning 				/* If this interface has multiple aliases
197e853bc5dShenning 				   on the same subnet, ignore all but the
198e853bc5dShenning 				   first we encounter. */
199e853bc5dShenning 				if (!subnet->interface) {
200e853bc5dShenning 					subnet->interface = tmp;
201e853bc5dShenning 					subnet->interface_address = addr;
202e853bc5dShenning 				} else if (subnet->interface != tmp) {
203c525a185Skrw 					log_warnx("Multiple %s %s: %s %s",
204e853bc5dShenning 					    "interfaces match the",
205e853bc5dShenning 					    "same subnet",
206e853bc5dShenning 					    subnet->interface->name,
207e853bc5dShenning 					    tmp->name);
208e853bc5dShenning 				}
209e853bc5dShenning 				share = subnet->shared_network;
210e853bc5dShenning 				if (tmp->shared_network &&
211e853bc5dShenning 				    tmp->shared_network != share) {
212c525a185Skrw 					log_warnx("Interface %s matches %s",
213e853bc5dShenning 					    tmp->name,
214e853bc5dShenning 					    "multiple shared networks");
215e853bc5dShenning 				} else {
216e853bc5dShenning 					tmp->shared_network = share;
217e853bc5dShenning 				}
218e853bc5dShenning 
219e853bc5dShenning 				if (!share->interface) {
220e853bc5dShenning 					share->interface = tmp;
221e853bc5dShenning 				} else if (share->interface != tmp) {
222c525a185Skrw 					log_warnx("Multiple %s %s: %s %s",
223e853bc5dShenning 					    "interfaces match the",
224e853bc5dShenning 					    "same shared network",
225e853bc5dShenning 					    share->interface->name,
226e853bc5dShenning 					    tmp->name);
227e853bc5dShenning 				}
228e853bc5dShenning 			}
229e853bc5dShenning 		}
230e853bc5dShenning 	}
231e853bc5dShenning 
232abad4d9fSkrw 	/* Discard interfaces we can't listen on. */
2338e1c2028Sderaadt 	last = NULL;
234e853bc5dShenning 	for (tmp = interfaces; tmp; tmp = next) {
235e853bc5dShenning 		next = tmp->next;
236abad4d9fSkrw 
23797f5f068Skrw 		if (!tmp->ifp) {
238c525a185Skrw 			log_warnx("Can't listen on %s - it has no IP address.",
23960111adeSkrw 			    tmp->name);
24097f5f068Skrw 			/* Remove tmp from the list of interfaces. */
241e853bc5dShenning 			if (!last)
242e853bc5dShenning 				interfaces = interfaces->next;
243e853bc5dShenning 			else
244e853bc5dShenning 				last->next = tmp->next;
245e853bc5dShenning 			continue;
246e853bc5dShenning 		}
247e853bc5dShenning 
248ea507cabSderaadt 		memcpy(&foo, &tmp->ifp->ifr_addr, sizeof tmp->ifp->ifr_addr);
249e853bc5dShenning 
25097f5f068Skrw 		if (!tmp->shared_network) {
25135318e8fSkrw 			log_warnx("Can't listen on %s - dhcpd.conf has no "
25235318e8fSkrw 			    "subnet declaration for %s.", tmp->name,
253abad4d9fSkrw 			    inet_ntoa(foo.sin_addr));
254abad4d9fSkrw 			/* Remove tmp from the list of interfaces. */
255abad4d9fSkrw 			if (!last)
256abad4d9fSkrw 				interfaces = interfaces->next;
257abad4d9fSkrw 			else
258abad4d9fSkrw 				last->next = tmp->next;
259abad4d9fSkrw 			continue;
260e853bc5dShenning 		}
261e853bc5dShenning 
262abad4d9fSkrw 		last = tmp;
263abad4d9fSkrw 
264abad4d9fSkrw 		/* Find subnets that don't have valid interface addresses. */
26535318e8fSkrw 		for (subnet = (tmp->shared_network ?
26635318e8fSkrw 		    tmp->shared_network->subnets : NULL); subnet;
26735318e8fSkrw 		    subnet = subnet->next_sibling) {
268e853bc5dShenning 			if (!subnet->interface_address.len) {
269ea507cabSderaadt 				/*
270ea507cabSderaadt 				 * Set the interface address for this subnet
271ea507cabSderaadt 				 * to the first address we found.
272ea507cabSderaadt 				 */
273e853bc5dShenning 				subnet->interface_address.len = 4;
274e853bc5dShenning 				memcpy(subnet->interface_address.iabuf,
275e853bc5dShenning 				    &foo.sin_addr.s_addr, 4);
276e853bc5dShenning 			}
277e853bc5dShenning 		}
278e853bc5dShenning 
279e853bc5dShenning 		/* Register the interface... */
280e853bc5dShenning 		if_register_receive(tmp);
281e853bc5dShenning 		if_register_send(tmp);
282c525a185Skrw 		log_info("Listening on %s (%s).", tmp->name,
283c7748be4Ssthen 		    inet_ntoa(foo.sin_addr));
284e853bc5dShenning 	}
285e853bc5dShenning 
28697f5f068Skrw 	if (interfaces == NULL)
287c525a185Skrw 		fatalx("No interfaces to listen on.");
28897f5f068Skrw 
289e853bc5dShenning 	/* Now register all the remaining interfaces as protocols. */
290ea507cabSderaadt 	for (tmp = interfaces; tmp; tmp = tmp->next)
291e853bc5dShenning 		add_protocol(tmp->name, tmp->rfdesc, got_one, tmp);
292e853bc5dShenning 
293e853bc5dShenning 	freeifaddrs(ifap);
294e853bc5dShenning }
295e853bc5dShenning 
296ea507cabSderaadt /*
297ea507cabSderaadt  * Wait for packets to come in using poll().  When a packet comes in,
298ea507cabSderaadt  * call receive_packet to receive the packet and possibly strip hardware
2994671a004Spelikan  * addressing information from it, and then process it in do_packet.
300ea507cabSderaadt  */
301ea507cabSderaadt void
dispatch(void)302ea507cabSderaadt dispatch(void)
303e853bc5dShenning {
30414fb3058Smillert 	int nfds, i, to_msec;
305e853bc5dShenning 	struct protocol *l;
30614fb3058Smillert 	static struct pollfd *fds;
30714fb3058Smillert 	static int nfds_max;
308e853bc5dShenning 	time_t howlong;
309e853bc5dShenning 
31014fb3058Smillert 	for (nfds = 0, l = protocols; l; l = l->next)
31114fb3058Smillert 		nfds++;
3125f515bebSbeck 	if (syncfd != -1)
3135f515bebSbeck 		nfds++;
31414fb3058Smillert 	if (nfds > nfds_max) {
315f4e02bb4Sderaadt 		fds = reallocarray(fds, nfds, sizeof(struct pollfd));
316e853bc5dShenning 		if (fds == NULL)
317c525a185Skrw 			fatalx("Can't allocate poll structures.");
31814fb3058Smillert 		nfds_max = nfds;
31914fb3058Smillert 	}
320e853bc5dShenning 
32114fb3058Smillert 	for (;;) {
322ea507cabSderaadt 		/*
323ea507cabSderaadt 		 * Call any expired timeouts, and then if there's
32414fb3058Smillert 		 * still a timeout registered, time out the poll
325ea507cabSderaadt 		 * call then.
326ea507cabSderaadt 		 */
32714fb3058Smillert 		time(&cur_time);
328e853bc5dShenning another:
329e853bc5dShenning 		if (timeouts) {
330e853bc5dShenning 			if (timeouts->when <= cur_time) {
3311e15d177Sckuethe 				struct dhcpd_timeout *t = timeouts;
332e853bc5dShenning 				timeouts = timeouts->next;
333e853bc5dShenning 				(*(t->func))(t->what);
334e853bc5dShenning 				t->next = free_timeouts;
335e853bc5dShenning 				free_timeouts = t;
336e853bc5dShenning 				goto another;
337e853bc5dShenning 			}
338ea507cabSderaadt 
339e853bc5dShenning 			/*
340e853bc5dShenning 			 * Figure timeout in milliseconds, and check for
341e853bc5dShenning 			 * potential overflow, so we can cram into an int
342e853bc5dShenning 			 * for poll, while not polling with a negative
343fd9f780dSdavid 			 * timeout and blocking indefinitely.
344e853bc5dShenning 			 */
345e853bc5dShenning 			howlong = timeouts->when - cur_time;
346e853bc5dShenning 			if (howlong > INT_MAX / 1000)
347e853bc5dShenning 				howlong = INT_MAX / 1000;
348e853bc5dShenning 			to_msec = howlong * 1000;
349e853bc5dShenning 		} else
350e853bc5dShenning 			to_msec = -1;
351e853bc5dShenning 
352e853bc5dShenning 		/* Set up the descriptors to be polled. */
35314fb3058Smillert 		for (i = 0, l = protocols; l; l = l->next) {
354e853bc5dShenning 			struct interface_info *ip = l->local;
355ea507cabSderaadt 
356e853bc5dShenning 			if (ip && (l->handler != got_one || !ip->dead)) {
357e853bc5dShenning 				fds[i].fd = l->fd;
358e853bc5dShenning 				fds[i].events = POLLIN;
359e853bc5dShenning 				++i;
360e853bc5dShenning 			}
361e853bc5dShenning 		}
3625f515bebSbeck 
363e853bc5dShenning 		if (i == 0)
364c525a185Skrw 			fatalx("No live interfaces to poll on - exiting.");
365e853bc5dShenning 
3665f515bebSbeck 		if (syncfd != -1) {
3675f515bebSbeck 			/* add syncer */
3685f515bebSbeck 			fds[i].fd = syncfd;
3695f515bebSbeck 			fds[i].events = POLLIN;
3705f515bebSbeck 		}
3715f515bebSbeck 
37214fb3058Smillert 		/* Wait for a packet or a timeout... */
37314fb3058Smillert 		switch (poll(fds, nfds, to_msec)) {
37414fb3058Smillert 		case -1:
37514fb3058Smillert 			if (errno != EAGAIN && errno != EINTR)
3760438cf0aSkrw 				fatal("poll");
37714fb3058Smillert 			/* FALLTHROUGH */
37814fb3058Smillert 		case 0:
37914fb3058Smillert 			continue;	/* no packets */
380e853bc5dShenning 		}
3814eb66b40Sgerhard 		time(&cur_time);
382e853bc5dShenning 
38314fb3058Smillert 		for (i = 0, l = protocols; l; l = l->next) {
384ea507cabSderaadt 			struct interface_info *ip = l->local;
385ea507cabSderaadt 
38699ddc12eScanacar 			if ((fds[i].revents & (POLLIN | POLLHUP))) {
387e853bc5dShenning 				if (ip && (l->handler != got_one ||
388e853bc5dShenning 				    !ip->dead))
389e853bc5dShenning 					(*(l->handler))(l);
390e853bc5dShenning 				if (interfaces_invalidated)
391e853bc5dShenning 					break;
392e853bc5dShenning 			}
393e853bc5dShenning 			++i;
394e853bc5dShenning 		}
3955f515bebSbeck 		if ((syncfd != -1) && (fds[i].revents & (POLLIN | POLLHUP)))
3965f515bebSbeck 			sync_recv();
397e853bc5dShenning 		interfaces_invalidated = 0;
39814fb3058Smillert 	}
399e853bc5dShenning }
400e853bc5dShenning 
401e853bc5dShenning 
402ea507cabSderaadt void
got_one(struct protocol * l)403ea507cabSderaadt got_one(struct protocol *l)
404e853bc5dShenning {
405e853bc5dShenning 	struct sockaddr_in from;
406e853bc5dShenning 	struct hardware hfrom;
407e853bc5dShenning 	struct iaddr ifrom;
408f82538e7Sderaadt 	ssize_t result;
409e853bc5dShenning 	union {
410ea507cabSderaadt 		unsigned char packbuf[4095];
411e853bc5dShenning 		struct dhcp_packet packet;
412e853bc5dShenning 	} u;
413e853bc5dShenning 	struct interface_info *ip = l->local;
414e853bc5dShenning 
415359ce2c3Smestre 	memset(&u, 0, sizeof(u));
416a720dd5fSkrw 
417ea507cabSderaadt 	if ((result = receive_packet(ip, u.packbuf, sizeof u,
418ea507cabSderaadt 	    &from, &hfrom)) == -1) {
419a76b277aSkrw 		log_warn("receive_packet failed on %s", ip->name);
420e853bc5dShenning 		ip->errors++;
421ea507cabSderaadt 		if ((!interface_status(ip)) ||
422ea507cabSderaadt 		    (ip->noifmedia && ip->errors > 20)) {
423e853bc5dShenning 			/* our interface has gone away. */
424c525a185Skrw 			log_warnx("Interface %s no longer appears valid.",
425e853bc5dShenning 			    ip->name);
426e853bc5dShenning 			ip->dead = 1;
427e853bc5dShenning 			interfaces_invalidated = 1;
428e853bc5dShenning 			close(l->fd);
429e853bc5dShenning 			remove_protocol(l);
430e853bc5dShenning 			free(ip);
431e853bc5dShenning 		}
432e853bc5dShenning 		return;
433e853bc5dShenning 	}
434e853bc5dShenning 	if (result == 0)
435e853bc5dShenning 		return;
436e853bc5dShenning 
437e853bc5dShenning 	ifrom.len = 4;
438e853bc5dShenning 	memcpy(ifrom.iabuf, &from.sin_addr, ifrom.len);
439e853bc5dShenning 
4404671a004Spelikan 	do_packet(ip, &u.packet, result, from.sin_port, ifrom, &hfrom);
441e853bc5dShenning }
442e853bc5dShenning 
443e853bc5dShenning int
interface_status(struct interface_info * ifinfo)444e853bc5dShenning interface_status(struct interface_info *ifinfo)
445e853bc5dShenning {
446e853bc5dShenning 	char * ifname = ifinfo->name;
447e853bc5dShenning 	int ifsock = ifinfo->rfdesc;
448e853bc5dShenning 	struct ifreq ifr;
449e853bc5dShenning 	struct ifmediareq ifmr;
450e853bc5dShenning 
451e853bc5dShenning 	/* get interface flags */
452e853bc5dShenning 	memset(&ifr, 0, sizeof(ifr));
453e853bc5dShenning 	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
4549bb003e4Sclaudio 	if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) == -1) {
4550438cf0aSkrw 		log_warn("ioctl(SIOCGIFFLAGS) on %s", ifname);
456e853bc5dShenning 		goto inactive;
457e853bc5dShenning 	}
458e853bc5dShenning 	/*
459e853bc5dShenning 	 * if one of UP and RUNNING flags is dropped,
460e853bc5dShenning 	 * the interface is not active.
461e853bc5dShenning 	 */
462ea507cabSderaadt 	if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
463e853bc5dShenning 		goto inactive;
464ea507cabSderaadt 
465e853bc5dShenning 	/* Next, check carrier on the interface, if possible */
466e853bc5dShenning 	if (ifinfo->noifmedia)
467e853bc5dShenning 		goto active;
468e853bc5dShenning 	memset(&ifmr, 0, sizeof(ifmr));
469e853bc5dShenning 	strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
4709bb003e4Sclaudio 	if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
471e853bc5dShenning 		if (errno != EINVAL) {
4720438cf0aSkrw 			log_debug("ioctl(SIOCGIFMEDIA) on %s", ifname);
473e853bc5dShenning 			ifinfo->noifmedia = 1;
474e853bc5dShenning 			goto active;
475e853bc5dShenning 		}
476e853bc5dShenning 		/*
477e853bc5dShenning 		 * EINVAL (or ENOTTY) simply means that the interface
478e853bc5dShenning 		 * does not support the SIOCGIFMEDIA ioctl. We regard it alive.
479e853bc5dShenning 		 */
480e853bc5dShenning 		ifinfo->noifmedia = 1;
481e853bc5dShenning 		goto active;
482e853bc5dShenning 	}
483e853bc5dShenning 	if (ifmr.ifm_status & IFM_AVALID) {
484e853bc5dShenning 		switch (ifmr.ifm_active & IFM_NMASK) {
485e853bc5dShenning 		case IFM_ETHER:
486e853bc5dShenning 			if (ifmr.ifm_status & IFM_ACTIVE)
487e853bc5dShenning 				goto active;
488e853bc5dShenning 			else
489e853bc5dShenning 				goto inactive;
490e853bc5dShenning 			break;
491e853bc5dShenning 		default:
492e853bc5dShenning 			goto inactive;
493e853bc5dShenning 		}
494e853bc5dShenning 	}
495e853bc5dShenning  inactive:
496e853bc5dShenning 	return (0);
497e853bc5dShenning  active:
498e853bc5dShenning 	return (1);
499e853bc5dShenning }
500e853bc5dShenning 
501ea507cabSderaadt int
locate_network(struct packet * packet)502ea507cabSderaadt locate_network(struct packet *packet)
503e853bc5dShenning {
504e853bc5dShenning 	struct iaddr ia;
505e853bc5dShenning 
506e853bc5dShenning 	/* If this came through a gateway, find the corresponding subnet... */
507e853bc5dShenning 	if (packet->raw->giaddr.s_addr) {
508e853bc5dShenning 		struct subnet *subnet;
509ea507cabSderaadt 
510e853bc5dShenning 		ia.len = 4;
511e853bc5dShenning 		memcpy(ia.iabuf, &packet->raw->giaddr, 4);
512e853bc5dShenning 		subnet = find_subnet(ia);
513e853bc5dShenning 		if (subnet)
514e853bc5dShenning 			packet->shared_network = subnet->shared_network;
515e853bc5dShenning 		else
516ea507cabSderaadt 			packet->shared_network = NULL;
517e853bc5dShenning 	} else {
518ea507cabSderaadt 		packet->shared_network = packet->interface->shared_network;
519e853bc5dShenning 	}
520e853bc5dShenning 	if (packet->shared_network)
521e853bc5dShenning 		return 1;
522e853bc5dShenning 	return 0;
523e853bc5dShenning }
524e853bc5dShenning 
525ea507cabSderaadt void
add_timeout(time_t when,void (* where)(void *),void * what)526ea507cabSderaadt add_timeout(time_t when, void (*where)(void *), void *what)
527e853bc5dShenning {
5281e15d177Sckuethe 	struct dhcpd_timeout *t, *q;
529e853bc5dShenning 
530e853bc5dShenning 	/* See if this timeout supersedes an existing timeout. */
5318e1c2028Sderaadt 	t = NULL;
532e853bc5dShenning 	for (q = timeouts; q; q = q->next) {
533e853bc5dShenning 		if (q->func == where && q->what == what) {
534e853bc5dShenning 			if (t)
535e853bc5dShenning 				t->next = q->next;
536e853bc5dShenning 			else
537e853bc5dShenning 				timeouts = q->next;
538e853bc5dShenning 			break;
539e853bc5dShenning 		}
540e853bc5dShenning 		t = q;
541e853bc5dShenning 	}
542e853bc5dShenning 
543e853bc5dShenning 	/* If we didn't supersede a timeout, allocate a timeout
544e853bc5dShenning 	   structure now. */
545e853bc5dShenning 	if (!q) {
546e853bc5dShenning 		if (free_timeouts) {
547e853bc5dShenning 			q = free_timeouts;
548e853bc5dShenning 			free_timeouts = q->next;
549e853bc5dShenning 			q->func = where;
550e853bc5dShenning 			q->what = what;
551e853bc5dShenning 		} else {
55235de856eSderaadt 			q = malloc(sizeof (struct dhcpd_timeout));
553e853bc5dShenning 			if (!q)
554c525a185Skrw 				fatalx("Can't allocate timeout structure!");
555e853bc5dShenning 			q->func = where;
556e853bc5dShenning 			q->what = what;
557e853bc5dShenning 		}
558e853bc5dShenning 	}
559e853bc5dShenning 
560e853bc5dShenning 	q->when = when;
561e853bc5dShenning 
562e853bc5dShenning 	/* Now sort this timeout into the timeout list. */
563e853bc5dShenning 
564e853bc5dShenning 	/* Beginning of list? */
565e853bc5dShenning 	if (!timeouts || timeouts->when > q->when) {
566e853bc5dShenning 		q->next = timeouts;
567e853bc5dShenning 		timeouts = q;
568e853bc5dShenning 		return;
569e853bc5dShenning 	}
570e853bc5dShenning 
571e853bc5dShenning 	/* Middle of list? */
572e853bc5dShenning 	for (t = timeouts; t->next; t = t->next) {
573e853bc5dShenning 		if (t->next->when > q->when) {
574e853bc5dShenning 			q->next = t->next;
575e853bc5dShenning 			t->next = q;
576e853bc5dShenning 			return;
577e853bc5dShenning 		}
578e853bc5dShenning 	}
579e853bc5dShenning 
580e853bc5dShenning 	/* End of list. */
581e853bc5dShenning 	t->next = q;
582ea507cabSderaadt 	q->next = NULL;
583e853bc5dShenning }
584e853bc5dShenning 
585ea507cabSderaadt void
cancel_timeout(void (* where)(void *),void * what)586ea507cabSderaadt cancel_timeout(void (*where)(void *), void *what)
587e853bc5dShenning {
5881e15d177Sckuethe 	struct dhcpd_timeout *t, *q;
589e853bc5dShenning 
590e853bc5dShenning 	/* Look for this timeout on the list, and unlink it if we find it. */
5918e1c2028Sderaadt 	t = NULL;
592e853bc5dShenning 	for (q = timeouts; q; q = q->next) {
593e853bc5dShenning 		if (q->func == where && q->what == what) {
594e853bc5dShenning 			if (t)
595e853bc5dShenning 				t->next = q->next;
596e853bc5dShenning 			else
597e853bc5dShenning 				timeouts = q->next;
598e853bc5dShenning 			break;
599e853bc5dShenning 		}
600e853bc5dShenning 		t = q;
601e853bc5dShenning 	}
602e853bc5dShenning 
603e853bc5dShenning 	/* If we found the timeout, put it on the free list. */
604e853bc5dShenning 	if (q) {
605e853bc5dShenning 		q->next = free_timeouts;
606e853bc5dShenning 		free_timeouts = q;
607e853bc5dShenning 	}
608e853bc5dShenning }
609e853bc5dShenning 
610e853bc5dShenning /* Add a protocol to the list of protocols... */
611ea507cabSderaadt void
add_protocol(char * name,int fd,void (* handler)(struct protocol *),void * local)612ea507cabSderaadt add_protocol(char *name, int fd, void (*handler)(struct protocol *),
613ea507cabSderaadt     void *local)
614e853bc5dShenning {
615e853bc5dShenning 	struct protocol *p;
616e853bc5dShenning 
61735de856eSderaadt 	p = malloc(sizeof *p);
618e853bc5dShenning 	if (!p)
619c525a185Skrw 		fatalx("can't allocate protocol struct for %s", name);
620e853bc5dShenning 	p->fd = fd;
621e853bc5dShenning 	p->handler = handler;
622e853bc5dShenning 	p->local = local;
623e853bc5dShenning 	p->next = protocols;
624e853bc5dShenning 	protocols = p;
625e853bc5dShenning }
626e853bc5dShenning 
627ea507cabSderaadt void
remove_protocol(struct protocol * proto)628ea507cabSderaadt remove_protocol(struct protocol *proto)
629e853bc5dShenning {
630ea507cabSderaadt 	struct protocol *p, *next, *prev = NULL;
631e853bc5dShenning 
632e853bc5dShenning 	for (p = protocols; p; p = next) {
633e853bc5dShenning 		next = p->next;
634e853bc5dShenning 		if (p == proto) {
635e853bc5dShenning 			if (prev)
636e853bc5dShenning 				prev->next = p->next;
637e853bc5dShenning 			else
638e853bc5dShenning 				protocols = p->next;
639e853bc5dShenning 			free(p);
640e853bc5dShenning 		}
641e853bc5dShenning 	}
642e853bc5dShenning }
643daa83f4cSclaudio 
644daa83f4cSclaudio int
get_rdomain(char * name)645daa83f4cSclaudio get_rdomain(char *name)
646daa83f4cSclaudio {
647daa83f4cSclaudio 	int rv = 0, s;
648daa83f4cSclaudio 	struct  ifreq ifr;
649daa83f4cSclaudio 
650daa83f4cSclaudio 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
6510438cf0aSkrw 		fatal("get_rdomain socket");
652daa83f4cSclaudio 
653359ce2c3Smestre 	memset(&ifr, 0, sizeof(ifr));
654daa83f4cSclaudio 	strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
6558bb39f08Sguenther 	if (ioctl(s, SIOCGIFRDOMAIN, (caddr_t)&ifr) != -1)
656daa83f4cSclaudio 		rv = ifr.ifr_rdomainid;
657daa83f4cSclaudio 
658daa83f4cSclaudio 	close(s);
659daa83f4cSclaudio 	return rv;
660daa83f4cSclaudio }
661