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