xref: /openbsd/usr.sbin/dhcpd/dispatch.c (revision 8932bfb7)
1 /*	$OpenBSD: dispatch.c,v 1.28 2010/11/19 08:00:56 ajacoutot 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 	if ((result = receive_packet(ip, u.packbuf, sizeof u,
395 	    &from, &hfrom)) == -1) {
396 		warning("receive_packet failed on %s: %s", ip->name,
397 		    strerror(errno));
398 		ip->errors++;
399 		if ((!interface_status(ip)) ||
400 		    (ip->noifmedia && ip->errors > 20)) {
401 			/* our interface has gone away. */
402 			warning("Interface %s no longer appears valid.",
403 			    ip->name);
404 			ip->dead = 1;
405 			interfaces_invalidated = 1;
406 			close(l->fd);
407 			remove_protocol(l);
408 			free(ip);
409 		}
410 		return;
411 	}
412 	if (result == 0)
413 		return;
414 
415 	if (bootp_packet_handler) {
416 		ifrom.len = 4;
417 		memcpy(ifrom.iabuf, &from.sin_addr, ifrom.len);
418 
419 		(*bootp_packet_handler)(ip, &u.packet, result,
420 		    from.sin_port, ifrom, &hfrom);
421 	}
422 }
423 
424 int
425 interface_status(struct interface_info *ifinfo)
426 {
427 	char * ifname = ifinfo->name;
428 	int ifsock = ifinfo->rfdesc;
429 	struct ifreq ifr;
430 	struct ifmediareq ifmr;
431 
432 	/* get interface flags */
433 	memset(&ifr, 0, sizeof(ifr));
434 	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
435 	if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) == -1) {
436 		syslog(LOG_ERR, "ioctl(SIOCGIFFLAGS) on %s: %m", ifname);
437 		goto inactive;
438 	}
439 	/*
440 	 * if one of UP and RUNNING flags is dropped,
441 	 * the interface is not active.
442 	 */
443 	if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
444 		goto inactive;
445 
446 	/* Next, check carrier on the interface, if possible */
447 	if (ifinfo->noifmedia)
448 		goto active;
449 	memset(&ifmr, 0, sizeof(ifmr));
450 	strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
451 	if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
452 		if (errno != EINVAL) {
453 			syslog(LOG_DEBUG, "ioctl(SIOCGIFMEDIA) on %s: %m",
454 			    ifname);
455 			ifinfo->noifmedia = 1;
456 			goto active;
457 		}
458 		/*
459 		 * EINVAL (or ENOTTY) simply means that the interface
460 		 * does not support the SIOCGIFMEDIA ioctl. We regard it alive.
461 		 */
462 		ifinfo->noifmedia = 1;
463 		goto active;
464 	}
465 	if (ifmr.ifm_status & IFM_AVALID) {
466 		switch (ifmr.ifm_active & IFM_NMASK) {
467 		case IFM_ETHER:
468 			if (ifmr.ifm_status & IFM_ACTIVE)
469 				goto active;
470 			else
471 				goto inactive;
472 			break;
473 		default:
474 			goto inactive;
475 		}
476 	}
477  inactive:
478 	return (0);
479  active:
480 	return (1);
481 }
482 
483 int
484 locate_network(struct packet *packet)
485 {
486 	struct iaddr ia;
487 
488 	/* If this came through a gateway, find the corresponding subnet... */
489 	if (packet->raw->giaddr.s_addr) {
490 		struct subnet *subnet;
491 
492 		ia.len = 4;
493 		memcpy(ia.iabuf, &packet->raw->giaddr, 4);
494 		subnet = find_subnet(ia);
495 		if (subnet)
496 			packet->shared_network = subnet->shared_network;
497 		else
498 			packet->shared_network = NULL;
499 	} else {
500 		packet->shared_network = packet->interface->shared_network;
501 	}
502 	if (packet->shared_network)
503 		return 1;
504 	return 0;
505 }
506 
507 void
508 add_timeout(time_t when, void (*where)(void *), void *what)
509 {
510 	struct dhcpd_timeout *t, *q;
511 
512 	/* See if this timeout supersedes an existing timeout. */
513 	t = NULL;
514 	for (q = timeouts; q; q = q->next) {
515 		if (q->func == where && q->what == what) {
516 			if (t)
517 				t->next = q->next;
518 			else
519 				timeouts = q->next;
520 			break;
521 		}
522 		t = q;
523 	}
524 
525 	/* If we didn't supersede a timeout, allocate a timeout
526 	   structure now. */
527 	if (!q) {
528 		if (free_timeouts) {
529 			q = free_timeouts;
530 			free_timeouts = q->next;
531 			q->func = where;
532 			q->what = what;
533 		} else {
534 			q = (struct dhcpd_timeout *)malloc(sizeof (struct dhcpd_timeout));
535 			if (!q)
536 				error("Can't allocate timeout structure!");
537 			q->func = where;
538 			q->what = what;
539 		}
540 	}
541 
542 	q->when = when;
543 
544 	/* Now sort this timeout into the timeout list. */
545 
546 	/* Beginning of list? */
547 	if (!timeouts || timeouts->when > q->when) {
548 		q->next = timeouts;
549 		timeouts = q;
550 		return;
551 	}
552 
553 	/* Middle of list? */
554 	for (t = timeouts; t->next; t = t->next) {
555 		if (t->next->when > q->when) {
556 			q->next = t->next;
557 			t->next = q;
558 			return;
559 		}
560 	}
561 
562 	/* End of list. */
563 	t->next = q;
564 	q->next = NULL;
565 }
566 
567 void
568 cancel_timeout(void (*where)(void *), void *what)
569 {
570 	struct dhcpd_timeout *t, *q;
571 
572 	/* Look for this timeout on the list, and unlink it if we find it. */
573 	t = NULL;
574 	for (q = timeouts; q; q = q->next) {
575 		if (q->func == where && q->what == what) {
576 			if (t)
577 				t->next = q->next;
578 			else
579 				timeouts = q->next;
580 			break;
581 		}
582 		t = q;
583 	}
584 
585 	/* If we found the timeout, put it on the free list. */
586 	if (q) {
587 		q->next = free_timeouts;
588 		free_timeouts = q;
589 	}
590 }
591 
592 /* Add a protocol to the list of protocols... */
593 void
594 add_protocol(char *name, int fd, void (*handler)(struct protocol *),
595     void *local)
596 {
597 	struct protocol *p;
598 
599 	p = (struct protocol *)malloc(sizeof *p);
600 	if (!p)
601 		error("can't allocate protocol struct for %s", name);
602 	p->fd = fd;
603 	p->handler = handler;
604 	p->local = local;
605 	p->next = protocols;
606 	protocols = p;
607 }
608 
609 void
610 remove_protocol(struct protocol *proto)
611 {
612 	struct protocol *p, *next, *prev = NULL;
613 
614 	for (p = protocols; p; p = next) {
615 		next = p->next;
616 		if (p == proto) {
617 			if (prev)
618 				prev->next = p->next;
619 			else
620 				protocols = p->next;
621 			free(p);
622 		}
623 	}
624 }
625 
626 int
627 get_rdomain(char *name)
628 {
629 	int rv = 0, s;
630 	struct  ifreq ifr;
631 
632 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
633 		error("get_rdomain socket: %m");
634 
635 	bzero(&ifr, sizeof(ifr));
636 	strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
637 	if (ioctl(s, SIOCGIFRDOMAIN, (caddr_t)&ifr) != -1)
638 		rv = ifr.ifr_rdomainid;
639 
640 	close(s);
641 	return rv;
642 }
643