GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/dhcrelay6/dispatch.c Lines: 0 183 0.0 %
Date: 2017-11-07 Branches: 0 130 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: dispatch.c,v 1.1 2017/03/17 14:45:16 rzalamena Exp $	*/
2
3
/*
4
 * Copyright 2004 Henning Brauer <henning@openbsd.org>
5
 * Copyright (c) 1995, 1996, 1997, 1998, 1999
6
 * The Internet Software Consortium.   All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 *
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 * 3. Neither the name of The Internet Software Consortium nor the names
18
 *    of its contributors may be used to endorse or promote products derived
19
 *    from this software without specific prior written permission.
20
 *
21
 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
22
 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25
 * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
26
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33
 * SUCH DAMAGE.
34
 *
35
 * This software has been written for the Internet Software Consortium
36
 * by Ted Lemon <mellon@fugue.com> in cooperation with Vixie
37
 * Enterprises.  To learn more about the Internet Software Consortium,
38
 * see ``http://www.vix.com/isc''.  To learn more about Vixie
39
 * Enterprises, see ``http://www.vix.com''.
40
 */
41
42
#include <sys/types.h>
43
#include <sys/ioctl.h>
44
#include <sys/socket.h>
45
46
#include <net/if.h>
47
#include <net/if_dl.h>
48
#include <net/if_media.h>
49
#include <net/if_types.h>
50
51
#include <netinet/in.h>
52
#include <netinet/if_ether.h>
53
54
#include <errno.h>
55
#include <ifaddrs.h>
56
#include <limits.h>
57
#include <poll.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 "dhcpd.h"
66
#include "log.h"
67
68
/*
69
 * Macros implementation used to generate link-local addresses. This
70
 * code was copied from: sys/netinet6/in6_ifattach.c.
71
 */
72
#define EUI64_UBIT		0x02
73
#define EUI64_TO_IFID(in6) \
74
	do { (in6)->s6_addr[8] ^= EUI64_UBIT; } while (0)
75
76
struct protocol *protocols;
77
struct timeout *timeouts;
78
static struct timeout *free_timeouts;
79
static int interfaces_invalidated;
80
81
void (*bootp_packet_handler)(struct interface_info *,
82
    void *, size_t, struct packet_ctx *);
83
84
static int interface_status(struct interface_info *ifinfo);
85
86
struct interface_info *
87
iflist_getbyindex(unsigned int index)
88
{
89
	struct interface_info	*intf;
90
91
	TAILQ_FOREACH(intf, &intflist, entry) {
92
		if (intf->index != index)
93
			continue;
94
95
		return intf;
96
	}
97
98
	return NULL;
99
}
100
101
struct interface_info *
102
iflist_getbyname(const char *name)
103
{
104
	struct interface_info	*intf;
105
106
	TAILQ_FOREACH(intf, &intflist, entry) {
107
		if (strcmp(intf->name, name) != 0)
108
			continue;
109
110
		return intf;
111
	}
112
113
	return NULL;
114
}
115
116
struct interface_info *
117
iflist_getbyaddr6(struct in6_addr *addr)
118
{
119
	struct interface_info	*intf;
120
121
	TAILQ_FOREACH(intf, &intflist, entry) {
122
		/* Look for link-layer address. */
123
		if (memcmp(&intf->linklocal, addr, sizeof(*addr)) == 0)
124
			return intf;
125
	}
126
127
	return NULL;
128
}
129
130
void
131
setup_iflist(void)
132
{
133
	struct interface_info		*intf;
134
	struct sockaddr_dl		*sdl;
135
	struct ifaddrs			*ifap, *ifa;
136
	struct if_data			*ifi;
137
	struct sockaddr_in		*sin;
138
	struct sockaddr_in6		*sin6;
139
140
	TAILQ_INIT(&intflist);
141
	if (getifaddrs(&ifap))
142
		fatalx("getifaddrs failed");
143
144
	for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
145
		if ((ifa->ifa_flags & IFF_LOOPBACK) ||
146
		    (ifa->ifa_flags & IFF_POINTOPOINT))
147
			continue;
148
149
		/* Find interface or create it. */
150
		intf = iflist_getbyname(ifa->ifa_name);
151
		if (intf == NULL) {
152
			intf = calloc(1, sizeof(*intf));
153
			if (intf == NULL)
154
				fatal("calloc");
155
156
			strlcpy(intf->name, ifa->ifa_name,
157
			    sizeof(intf->name));
158
			TAILQ_INSERT_HEAD(&intflist, intf, entry);
159
		}
160
161
		/* Signal disabled interface. */
162
		if ((ifa->ifa_flags & IFF_UP) == 0)
163
			intf->dead = 1;
164
165
		if (ifa->ifa_addr->sa_family == AF_LINK) {
166
			sdl = (struct sockaddr_dl *)ifa->ifa_addr;
167
			ifi = (struct if_data *)ifa->ifa_data;
168
169
			/* Skip non ethernet interfaces. */
170
			if (ifi->ifi_type != IFT_ETHER &&
171
			    ifi->ifi_type != IFT_ENC) {
172
				TAILQ_REMOVE(&intflist, intf, entry);
173
				free(intf);
174
				continue;
175
			}
176
177
			intf->index = sdl->sdl_index;
178
			intf->hw_address.hlen = sdl->sdl_alen;
179
			memcpy(intf->hw_address.haddr,
180
			    LLADDR(sdl), sdl->sdl_alen);
181
		} else if (ifa->ifa_addr->sa_family == AF_INET) {
182
			sin = (struct sockaddr_in *)ifa->ifa_addr;
183
			if (sin->sin_addr.s_addr == htonl(INADDR_LOOPBACK) ||
184
			    intf->primary_address.s_addr != INADDR_ANY)
185
				continue;
186
187
			intf->primary_address = sin->sin_addr;
188
		} else if (ifa->ifa_addr->sa_family == AF_INET6) {
189
			sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
190
			/* Remove the scope from address if link-local. */
191
			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
192
				intf->linklocal = sin6->sin6_addr;
193
				intf->linklocal.s6_addr[2] = 0;
194
				intf->linklocal.s6_addr[3] = 0;
195
			} else
196
				intf->gipv6 = 1;
197
198
			/* At least one IPv6 address was found. */
199
			intf->ipv6 = 1;
200
		}
201
	}
202
203
	freeifaddrs(ifap);
204
205
	/*
206
	 * Generate link-local IPv6 address for interfaces without it.
207
	 *
208
	 * For IPv6 DHCP Relay it doesn't matter what is used for
209
	 * link-addr field, so let's generate an address that won't
210
	 * change during execution so we can always find the interface
211
	 * to relay packets back. This is only used for layer 2 relaying
212
	 * when the interface might not have an address.
213
	 */
214
	TAILQ_FOREACH(intf, &intflist, entry) {
215
		if (memcmp(&intf->linklocal, &in6addr_any,
216
		    sizeof(in6addr_any)) != 0)
217
			continue;
218
219
		intf->linklocal.s6_addr[0] = 0xfe;
220
		intf->linklocal.s6_addr[1] = 0x80;
221
		intf->linklocal.s6_addr[8] = intf->hw_address.haddr[0];
222
		intf->linklocal.s6_addr[9] = intf->hw_address.haddr[1];
223
		intf->linklocal.s6_addr[10] = intf->hw_address.haddr[2];
224
		intf->linklocal.s6_addr[11] = 0xff;
225
		intf->linklocal.s6_addr[12] = 0xfe;
226
		intf->linklocal.s6_addr[13] = intf->hw_address.haddr[3];
227
		intf->linklocal.s6_addr[14] = intf->hw_address.haddr[4];
228
		intf->linklocal.s6_addr[15] = intf->hw_address.haddr[5];
229
		EUI64_TO_IFID(&intf->linklocal);
230
	}
231
}
232
233
struct interface_info *
234
register_interface(const char *ifname, void (*handler)(struct protocol *))
235
{
236
	struct interface_info		*intf;
237
238
	if ((intf = iflist_getbyname(ifname)) == NULL)
239
		return NULL;
240
241
	/* Don't register disabled interfaces. */
242
	if (intf->dead)
243
		return NULL;
244
245
	/* Check if we already registered the interface. */
246
	if (intf->ifr.ifr_name[0] != 0)
247
		return intf;
248
249
	if (strlcpy(intf->ifr.ifr_name, ifname,
250
	    sizeof(intf->ifr.ifr_name)) >= sizeof(intf->ifr.ifr_name))
251
		fatalx("interface name '%s' too long", ifname);
252
253
	if_register_receive(intf);
254
	if_register_send(intf);
255
	add_protocol(intf->name, intf->rfdesc, handler, intf);
256
257
	return intf;
258
}
259
260
/*
261
 * Wait for packets to come in using poll().  When a packet comes in,
262
 * call receive_packet to receive the packet and possibly strip hardware
263
 * addressing information from it, and then call through the
264
 * bootp_packet_handler hook to try to do something with it.
265
 */
266
void
267
dispatch(void)
268
{
269
	int count, i, to_msec, nfds = 0;
270
	struct protocol *l;
271
	struct pollfd *fds;
272
	time_t howlong;
273
274
	nfds = 0;
275
	for (l = protocols; l; l = l->next)
276
		nfds++;
277
278
	fds = calloc(nfds, sizeof(struct pollfd));
279
	if (fds == NULL)
280
		fatalx("Can't allocate poll structures.");
281
282
	do {
283
		/*
284
		 * Call any expired timeouts, and then if there's still
285
		 * a timeout registered, time out the select call then.
286
		 */
287
another:
288
		if (timeouts) {
289
			if (timeouts->when <= cur_time) {
290
				struct timeout *t = timeouts;
291
292
				timeouts = timeouts->next;
293
				(*(t->func))(t->what);
294
				t->next = free_timeouts;
295
				free_timeouts = t;
296
				goto another;
297
			}
298
299
			/*
300
			 * Figure timeout in milliseconds, and check for
301
			 * potential overflow, so we can cram into an
302
			 * int for poll, while not polling with a
303
			 * negative timeout and blocking indefinitely.
304
			 */
305
			howlong = timeouts->when - cur_time;
306
			if (howlong > INT_MAX / 1000)
307
				howlong = INT_MAX / 1000;
308
			to_msec = howlong * 1000;
309
		} else
310
			to_msec = -1;
311
312
		/* Set up the descriptors to be polled. */
313
		i = 0;
314
315
		for (l = protocols; l; l = l->next) {
316
			struct interface_info *ip = l->local;
317
318
			if (ip && (l->handler != got_one || !ip->dead)) {
319
				fds[i].fd = l->fd;
320
				fds[i].events = POLLIN;
321
				fds[i].revents = 0;
322
				i++;
323
			}
324
		}
325
326
		if (i == 0)
327
			fatalx("No live interfaces to poll on - exiting.");
328
329
		/* Wait for a packet or a timeout... XXX */
330
		count = poll(fds, nfds, to_msec);
331
332
		/* Not likely to be transitory... */
333
		if (count == -1) {
334
			if (errno == EAGAIN || errno == EINTR) {
335
				time(&cur_time);
336
				continue;
337
			}
338
			else
339
				fatal("poll");
340
		}
341
342
		/* Get the current time... */
343
		time(&cur_time);
344
345
		i = 0;
346
		for (l = protocols; l; l = l->next) {
347
			struct interface_info *ip = l->local;
348
349
			if ((fds[i].revents & (POLLIN | POLLHUP))) {
350
				fds[i].revents = 0;
351
				if (ip && (l->handler != got_one ||
352
				    !ip->dead))
353
					(*(l->handler))(l);
354
				if (interfaces_invalidated)
355
					break;
356
			}
357
			i++;
358
		}
359
		interfaces_invalidated = 0;
360
	} while (1);
361
}
362
363
364
void
365
got_one(struct protocol *l)
366
{
367
	struct packet_ctx pc;
368
	ssize_t result;
369
	uint8_t buf[4096];
370
	struct interface_info *ip = l->local;
371
372
	memset(&pc, 0, sizeof(pc));
373
374
	if ((result = receive_packet(ip, buf, sizeof(buf), &pc)) == -1) {
375
		log_warn("receive_packet failed on %s", ip->name);
376
		ip->errors++;
377
		if ((!interface_status(ip)) ||
378
		    (ip->noifmedia && ip->errors > 20)) {
379
			/* our interface has gone away. */
380
			log_warnx("Interface %s no longer appears valid.",
381
			    ip->name);
382
			ip->dead = 1;
383
			interfaces_invalidated = 1;
384
			close(l->fd);
385
			remove_protocol(l);
386
			free(ip);
387
		}
388
		return;
389
	}
390
	if (result == 0)
391
		return;
392
393
	if (bootp_packet_handler)
394
		(*bootp_packet_handler)(ip, buf, result, &pc);
395
}
396
397
int
398
interface_status(struct interface_info *ifinfo)
399
{
400
	char *ifname = ifinfo->name;
401
	int ifsock = ifinfo->rfdesc;
402
	struct ifreq ifr;
403
	struct ifmediareq ifmr;
404
405
	/* get interface flags */
406
	memset(&ifr, 0, sizeof(ifr));
407
	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
408
	if (ioctl(ifsock, SIOCGIFFLAGS, &ifr) == -1) {
409
		log_warn("ioctl(SIOCGIFFLAGS) on %s", ifname);
410
		goto inactive;
411
	}
412
	/*
413
	 * if one of UP and RUNNING flags is dropped,
414
	 * the interface is not active.
415
	 */
416
	if ((ifr.ifr_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
417
		goto inactive;
418
	}
419
	/* Next, check carrier on the interface, if possible */
420
	if (ifinfo->noifmedia)
421
		goto active;
422
	memset(&ifmr, 0, sizeof(ifmr));
423
	strlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
424
	if (ioctl(ifsock, SIOCGIFMEDIA, (caddr_t)&ifmr) == -1) {
425
		if (errno != EINVAL) {
426
			log_debug("ioctl(SIOCGIFMEDIA) on %s", ifname);
427
			ifinfo->noifmedia = 1;
428
			goto active;
429
		}
430
		/*
431
		 * EINVAL (or ENOTTY) simply means that the interface
432
		 * does not support the SIOCGIFMEDIA ioctl. We regard it alive.
433
		 */
434
		ifinfo->noifmedia = 1;
435
		goto active;
436
	}
437
	if (ifmr.ifm_status & IFM_AVALID) {
438
		switch (ifmr.ifm_active & IFM_NMASK) {
439
		case IFM_ETHER:
440
			if (ifmr.ifm_status & IFM_ACTIVE)
441
				goto active;
442
			else
443
				goto inactive;
444
			break;
445
		default:
446
			goto inactive;
447
		}
448
	}
449
inactive:
450
	return (0);
451
active:
452
	return (1);
453
}
454
455
/* Add a protocol to the list of protocols... */
456
void
457
add_protocol(char *name, int fd, void (*handler)(struct protocol *),
458
    void *local)
459
{
460
	struct protocol *p;
461
462
	p = malloc(sizeof(*p));
463
	if (!p)
464
		fatalx("can't allocate protocol struct for %s", name);
465
466
	p->fd = fd;
467
	p->handler = handler;
468
	p->local = local;
469
	p->next = protocols;
470
	protocols = p;
471
}
472
473
void
474
remove_protocol(struct protocol *proto)
475
{
476
	struct protocol *p, *next, *prev;
477
478
	prev = NULL;
479
	for (p = protocols; p; p = next) {
480
		next = p->next;
481
		if (p == proto) {
482
			if (prev)
483
				prev->next = p->next;
484
			else
485
				protocols = p->next;
486
			free(p);
487
		}
488
	}
489
}