xref: /freebsd/sbin/dhclient/dispatch.c (revision b59017c5cad90d0f09a59e68c00457b7faf93e7c)
1 /*	$OpenBSD: dispatch.c,v 1.31 2004/09/21 04:07:03 david Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright 2004 Henning Brauer <henning@openbsd.org>
7  * Copyright (c) 1995, 1996, 1997, 1998, 1999
8  * The Internet Software Consortium.   All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of The Internet Software Consortium nor the names
20  *    of its contributors may be used to endorse or promote products derived
21  *    from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
24  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
28  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
31  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  * This software has been written for the Internet Software Consortium
38  * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
39  * Enterprises.  To learn more about the Internet Software Consortium,
40  * see ``http://www.vix.com/isc''.  To learn more about Vixie
41  * Enterprises, see ``http://www.vix.com''.
42  */
43 
44 #include <sys/cdefs.h>
45 #include "dhcpd.h"
46 #include "privsep.h"
47 
48 #include <sys/ioctl.h>
49 
50 #include <assert.h>
51 #include <net/if_media.h>
52 #include <ifaddrs.h>
53 #include <poll.h>
54 
55 /* Assert that pointer p is aligned to at least align bytes */
56 #define assert_aligned(p, align) assert((((uintptr_t)p) & ((align) - 1)) == 0)
57 
58 static struct protocol *protocols;
59 static const struct timespec timespec_intmax_ms = {
60 	.tv_sec = INT_MAX / 1000,
61 	.tv_nsec = (INT_MAX % 1000) * 1000000
62 };
63 static struct timeout *timeouts;
64 static struct timeout *free_timeouts;
65 static int interfaces_invalidated;
66 void (*bootp_packet_handler)(struct interface_info *,
67     struct dhcp_packet *, int, unsigned int,
68     struct iaddr, struct hardware *);
69 
70 static int interface_status(struct interface_info *ifinfo);
71 
72 /*
73  * Use getifaddrs() to get a list of all the attached interfaces.  For
74  * each interface that's of type INET and not the loopback interface,
75  * register that interface with the network I/O software, figure out
76  * what subnet it's on, and add it to the list of interfaces.
77  */
78 void
79 discover_interfaces(struct interface_info *iface)
80 {
81 	struct ifaddrs *ifap, *ifa;
82 	struct ifreq *tif;
83 
84 	if (getifaddrs(&ifap) != 0)
85 		error("getifaddrs failed");
86 
87 	for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
88 		if ((ifa->ifa_flags & IFF_LOOPBACK) ||
89 		    (ifa->ifa_flags & IFF_POINTOPOINT) ||
90 		    (!(ifa->ifa_flags & IFF_UP)))
91 			continue;
92 
93 		if (strcmp(iface->name, ifa->ifa_name))
94 			continue;
95 
96 		/*
97 		 * If we have the capability, extract link information
98 		 * and record it in a linked list.
99 		 */
100 		if (ifa->ifa_addr->sa_family == AF_LINK) {
101 			struct sockaddr_dl *foo;
102 
103 			/*
104 			 * The implementation of getifaddrs should guarantee
105 			 * this alignment
106 			 */
107 			assert_aligned(ifa->ifa_addr,
108 				       _Alignof(struct sockaddr_dl));
109 #ifdef __clang__
110 #pragma clang diagnostic push
111 #pragma clang diagnostic ignored "-Wcast-align"
112 #endif
113 			foo = (struct sockaddr_dl *)ifa->ifa_addr;
114 #ifdef __clang__
115 #pragma clang diagnostic pop
116 #endif
117 
118 			iface->index = foo->sdl_index;
119 			iface->hw_address.hlen = foo->sdl_alen;
120 			iface->hw_address.htype = HTYPE_ETHER; /* XXX */
121 			memcpy(iface->hw_address.haddr,
122 			    LLADDR(foo), foo->sdl_alen);
123 		}
124 		if (!iface->ifp) {
125 			if ((tif = calloc(1, sizeof(struct ifreq))) == NULL)
126 				error("no space to remember ifp");
127 			strlcpy(tif->ifr_name, ifa->ifa_name, IFNAMSIZ);
128 			iface->ifp = tif;
129 		}
130 
131 	}
132 
133 	if (!iface->ifp)
134 		error("%s: not found", iface->name);
135 
136 	/* Register the interface... */
137 	if_register_receive(iface);
138 	if_register_send(iface);
139 	add_protocol(iface->name, iface->rfdesc, got_one, iface);
140 	freeifaddrs(ifap);
141 }
142 
143 void
144 reinitialize_interfaces(void)
145 {
146 	interfaces_invalidated = 1;
147 }
148 
149 /*
150  * Wait for packets to come in using poll().  When a packet comes in,
151  * call receive_packet to receive the packet and possibly strip hardware
152  * addressing information from it, and then call through the
153  * bootp_packet_handler hook to try to do something with it.
154  */
155 void
156 dispatch(void)
157 {
158 	int count, live_interfaces, i, to_msec, nfds = 0;
159 	struct protocol *l;
160 	struct pollfd *fds;
161 	struct timespec howlong;
162 	time_now.tv_sec = cur_time;
163 	time_now.tv_nsec = 0;
164 
165 	for (l = protocols; l; l = l->next)
166 		nfds++;
167 
168 	fds = malloc(nfds * sizeof(struct pollfd));
169 	if (fds == NULL)
170 		error("Can't allocate poll structures.");
171 
172 	do {
173 		/*
174 		 * Call any expired timeouts, and then if there's still
175 		 * a timeout registered, time out the select call then.
176 		 */
177 another:
178 		if (timeouts) {
179 			struct timeout *t;
180 
181 			if (timespeccmp(&timeouts->when, &time_now, <=)) {
182 				t = timeouts;
183 				timeouts = timeouts->next;
184 				(*(t->func))(t->what);
185 				t->next = free_timeouts;
186 				free_timeouts = t;
187 				goto another;
188 			}
189 
190 			/*
191 			 * Figure timeout in milliseconds, and check for
192 			 * potential overflow, so we can cram into an
193 			 * int for poll, while not polling with a
194 			 * negative timeout and blocking indefinitely.
195 			 */
196 			timespecsub(&timeouts->when, &time_now, &howlong);
197 			if (timespeccmp(&howlong, &timespec_intmax_ms, >))
198 				howlong = timespec_intmax_ms;
199 			to_msec = howlong.tv_sec * 1000 + howlong.tv_nsec / 1000000;
200 		} else
201 			to_msec = -1;
202 
203 		/* Set up the descriptors to be polled. */
204 		live_interfaces = 0;
205 		for (i = 0, l = protocols; l; l = l->next) {
206 			struct interface_info *ip = l->local;
207 
208 			if (ip == NULL || ip->dead)
209 				continue;
210 			fds[i].fd = l->fd;
211 			fds[i].events = POLLIN;
212 			fds[i].revents = 0;
213 			i++;
214 			if (l->handler == got_one)
215 				live_interfaces++;
216 		}
217 		if (live_interfaces == 0)
218 			error("No live interfaces to poll on - exiting.");
219 
220 		/* Wait for a packet or a timeout... XXX */
221 		count = poll(fds, nfds, to_msec);
222 
223 		/* Not likely to be transitory... */
224 		if (count == -1) {
225 			if (errno == EAGAIN || errno == EINTR) {
226 				clock_gettime(CLOCK_MONOTONIC, &time_now);
227 				cur_time = time_now.tv_sec;
228 				continue;
229 			} else
230 				error("poll: %m");
231 		}
232 
233 		/* Get the current time... */
234 		clock_gettime(CLOCK_MONOTONIC, &time_now);
235 		cur_time = time_now.tv_sec;
236 
237 		i = 0;
238 		for (l = protocols; l; l = l->next) {
239 			struct interface_info *ip;
240 			ip = l->local;
241 			if ((fds[i].revents & (POLLIN | POLLHUP))) {
242 				fds[i].revents = 0;
243 				if (ip && (l->handler != got_one ||
244 				    !ip->dead))
245 					(*(l->handler))(l);
246 				if (interfaces_invalidated)
247 					break;
248 			}
249 			i++;
250 		}
251 		interfaces_invalidated = 0;
252 	} while (1);
253 }
254 
255 
256 void
257 got_one(struct protocol *l)
258 {
259 	struct sockaddr_in from;
260 	struct hardware hfrom;
261 	struct iaddr ifrom;
262 	ssize_t result;
263 	union {
264 		/*
265 		 * Packet input buffer.  Must be as large as largest
266 		 * possible MTU.
267 		 */
268 		unsigned char packbuf[4095];
269 		struct dhcp_packet packet;
270 	} u;
271 	struct interface_info *ip = l->local;
272 
273 	if ((result = receive_packet(ip, u.packbuf, sizeof(u), &from,
274 	    &hfrom)) == -1) {
275 		warning("receive_packet failed on %s: %s", ip->name,
276 		    strerror(errno));
277 		ip->errors++;
278 		if ((!interface_status(ip)) ||
279 		    (ip->noifmedia && ip->errors > 20)) {
280 			/* our interface has gone away. */
281 			warning("Interface %s no longer appears valid.",
282 			    ip->name);
283 			ip->dead = 1;
284 			interfaces_invalidated = 1;
285 			close(l->fd);
286 			remove_protocol(l);
287 			free(ip);
288 		}
289 		return;
290 	}
291 	if (result == 0)
292 		return;
293 
294 	if (bootp_packet_handler) {
295 		ifrom.len = 4;
296 		memcpy(ifrom.iabuf, &from.sin_addr, ifrom.len);
297 
298 		(*bootp_packet_handler)(ip, &u.packet, result,
299 		    from.sin_port, ifrom, &hfrom);
300 	}
301 }
302 
303 int
304 interface_status(struct interface_info *ifinfo)
305 {
306 	char *ifname = ifinfo->name;
307 	int ifsock = ifinfo->rfdesc;
308 	struct ifreq ifr;
309 	struct ifmediareq ifmr;
310 
311 	/* get interface flags */
312 	memset(&ifr, 0, sizeof(ifr));
313 	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
314 	if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) < 0) {
315 		cap_syslog(capsyslog, LOG_ERR, "ioctl(SIOCGIFFLAGS) on %s: %m",
316 		    ifname);
317 		goto inactive;
318 	}
319 
320 	/*
321 	 * if one of UP and RUNNING flags is dropped,
322 	 * the interface is not active.
323 	 */
324 	if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
325 		goto inactive;
326 
327 	/* Next, check carrier on the interface, if possible */
328 	if (ifinfo->noifmedia)
329 		goto active;
330 	memset(&ifmr, 0, sizeof(ifmr));
331 	strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
332 	if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0) {
333 		if (errno != EINVAL) {
334 			cap_syslog(capsyslog, LOG_DEBUG,
335 			    "ioctl(SIOCGIFMEDIA) on %s: %m", ifname);
336 			ifinfo->noifmedia = 1;
337 			goto active;
338 		}
339 		/*
340 		 * EINVAL (or ENOTTY) simply means that the interface
341 		 * does not support the SIOCGIFMEDIA ioctl. We regard it alive.
342 		 */
343 		ifinfo->noifmedia = 1;
344 		goto active;
345 	}
346 	if (ifmr.ifm_status & IFM_AVALID) {
347 		switch (ifmr.ifm_active & IFM_NMASK) {
348 		case IFM_ETHER:
349 		case IFM_IEEE80211:
350 			if (ifmr.ifm_status & IFM_ACTIVE)
351 				goto active;
352 			else
353 				goto inactive;
354 			break;
355 		default:
356 			goto inactive;
357 		}
358 	}
359 inactive:
360 	return (0);
361 active:
362 	return (1);
363 }
364 
365 void
366 add_timeout(time_t when_s, void (*where)(void *), void *what)
367 {
368 	struct timespec when = { .tv_sec = when_s, .tv_nsec = 0 };
369 	add_timeout_timespec(when, where, what);
370 }
371 
372 void
373 add_timeout_timespec(struct timespec when, void (*where)(void *), void *what)
374 {
375 	struct timeout *t, *q;
376 
377 	/* See if this timeout supersedes an existing timeout. */
378 	t = NULL;
379 	for (q = timeouts; q; q = q->next) {
380 		if (q->func == where && q->what == what) {
381 			if (t)
382 				t->next = q->next;
383 			else
384 				timeouts = q->next;
385 			break;
386 		}
387 		t = q;
388 	}
389 
390 	/* If we didn't supersede a timeout, allocate a timeout
391 	   structure now. */
392 	if (!q) {
393 		if (free_timeouts) {
394 			q = free_timeouts;
395 			free_timeouts = q->next;
396 			q->func = where;
397 			q->what = what;
398 		} else {
399 			q = malloc(sizeof(struct timeout));
400 			if (!q)
401 				error("Can't allocate timeout structure!");
402 			q->func = where;
403 			q->what = what;
404 		}
405 	}
406 
407 	q->when = when;
408 
409 	/* Now sort this timeout into the timeout list. */
410 
411 	/* Beginning of list? */
412 	if (!timeouts || timespeccmp(&timeouts->when, &q->when, >)) {
413 		q->next = timeouts;
414 		timeouts = q;
415 		return;
416 	}
417 
418 	/* Middle of list? */
419 	for (t = timeouts; t->next; t = t->next) {
420 		if (timespeccmp(&t->next->when, &q->when, >)) {
421 			q->next = t->next;
422 			t->next = q;
423 			return;
424 		}
425 	}
426 
427 	/* End of list. */
428 	t->next = q;
429 	q->next = NULL;
430 }
431 
432 void
433 cancel_timeout(void (*where)(void *), void *what)
434 {
435 	struct timeout *t, *q;
436 
437 	/* Look for this timeout on the list, and unlink it if we find it. */
438 	t = NULL;
439 	for (q = timeouts; q; q = q->next) {
440 		if (q->func == where && q->what == what) {
441 			if (t)
442 				t->next = q->next;
443 			else
444 				timeouts = q->next;
445 			break;
446 		}
447 		t = q;
448 	}
449 
450 	/* If we found the timeout, put it on the free list. */
451 	if (q) {
452 		q->next = free_timeouts;
453 		free_timeouts = q;
454 	}
455 }
456 
457 /* Add a protocol to the list of protocols... */
458 void
459 add_protocol(const char *name, int fd, void (*handler)(struct protocol *),
460     void *local)
461 {
462 	struct protocol *p;
463 
464 	p = malloc(sizeof(*p));
465 	if (!p)
466 		error("can't allocate protocol struct for %s", name);
467 
468 	p->fd = fd;
469 	p->handler = handler;
470 	p->local = local;
471 	p->next = protocols;
472 	protocols = p;
473 }
474 
475 void
476 remove_protocol(struct protocol *proto)
477 {
478 	struct protocol *p, *prev;
479 
480 	for (p = protocols, prev = NULL; p != NULL; prev = p, p = p->next) {
481 		if (p == proto) {
482 			if (prev == NULL)
483 				protocols = p->next;
484 			else
485 				prev->next = p->next;
486 			free(p);
487 			break;
488 		}
489 	}
490 }
491 
492 int
493 interface_link_status(char *ifname)
494 {
495 	struct ifmediareq ifmr;
496 	int sock;
497 
498 	if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
499 		error("Can't create socket");
500 
501 	memset(&ifmr, 0, sizeof(ifmr));
502 	strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
503 	if (ioctl(sock, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
504 		/* EINVAL -> link state unknown. treat as active */
505 		if (errno != EINVAL)
506 			cap_syslog(capsyslog, LOG_DEBUG,
507 			    "ioctl(SIOCGIFMEDIA) on %s: %m", ifname);
508 		close(sock);
509 		return (1);
510 	}
511 	close(sock);
512 
513 	if (ifmr.ifm_status & IFM_AVALID) {
514 		switch (ifmr.ifm_active & IFM_NMASK) {
515 		case IFM_ETHER:
516 		case IFM_IEEE80211:
517 			if (ifmr.ifm_status & IFM_ACTIVE)
518 				return (1);
519 			else
520 				return (0);
521 		}
522 	}
523 	return (1);
524 }
525 
526 void
527 interface_set_mtu_unpriv(int privfd, u_int16_t mtu)
528 {
529 	struct imsg_hdr hdr;
530 	struct buf *buf;
531 	int errs = 0;
532 
533 	hdr.code = IMSG_SET_INTERFACE_MTU;
534 	hdr.len = sizeof(hdr) +
535 		sizeof(u_int16_t);
536 
537 	if ((buf = buf_open(hdr.len)) == NULL)
538 		error("buf_open: %m");
539 
540 	errs += buf_add(buf, &hdr, sizeof(hdr));
541 	errs += buf_add(buf, &mtu, sizeof(mtu));
542 	if (errs)
543 		error("buf_add: %m");
544 
545 	if (buf_close(privfd, buf) == -1)
546 		error("buf_close: %m");
547 }
548 
549 void
550 interface_set_mtu_priv(char *ifname, u_int16_t mtu)
551 {
552 	struct ifreq ifr;
553 	int sock;
554 	u_int16_t old_mtu;
555 
556 	if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
557 		error("Can't create socket");
558 
559 	memset(&ifr, 0, sizeof(ifr));
560 	old_mtu = 0;
561 
562 	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
563 
564 	if (ioctl(sock, SIOCGIFMTU, (caddr_t)&ifr) == -1)
565 		warning("SIOCGIFMTU failed (%s): %s", ifname,
566 			strerror(errno));
567 	else
568 		old_mtu = ifr.ifr_mtu;
569 
570 	if (mtu != old_mtu) {
571 		ifr.ifr_mtu = mtu;
572 
573 		if (ioctl(sock, SIOCSIFMTU, &ifr) == -1)
574 			warning("SIOCSIFMTU failed (%d): %s", mtu,
575 				strerror(errno));
576 	}
577 
578 	close(sock);
579 }
580