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