discover.c 41.9 KB
Newer Older
David Hankins's avatar
David Hankins committed
1
/* discover.c
Ted Lemon's avatar
Ted Lemon committed
2

David Hankins's avatar
David Hankins committed
3
   Find and identify the network interfaces. */
Ted Lemon's avatar
Ted Lemon committed
4 5

/*
6
 * Copyright (c) 2004-2020 by Internet Systems Consortium, Inc. ("ISC")
7
 * Copyright (c) 1995-2003 by Internet Software Consortium
Ted Lemon's avatar
Ted Lemon committed
8
 *
9 10 11
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
Ted Lemon's avatar
Ted Lemon committed
12
 *
13 14 15 16 17 18 19
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Ted Lemon's avatar
Ted Lemon committed
20
 *
21 22 23 24
 *   Internet Systems Consortium, Inc.
 *   950 Charter Street
 *   Redwood City, CA 94063
 *   <info@isc.org>
25
 *   https://www.isc.org/
Ted Lemon's avatar
Ted Lemon committed
26
 *
Ted Lemon's avatar
Ted Lemon committed
27 28 29
 */

#include "dhcpd.h"
30

31 32 33
/* length of line we can read from the IF file, 256 is too small in some cases */
#define IF_LINE_LENGTH 1024

34
#define BSD_COMP		/* needed on Solaris for SIOCGLIFNUM */
Ted Lemon's avatar
Ted Lemon committed
35
#include <sys/ioctl.h>
36
#include <errno.h>
Ted Lemon's avatar
Ted Lemon committed
37

38 39 40 41
#ifdef HAVE_NET_IF6_H
# include <net/if6.h>
#endif

42 43 44 45
struct interface_info *interfaces = 0;
struct interface_info *dummy_interfaces = 0;
struct interface_info *fallback_interface = 0;

46
int interfaces_invalidated;
Ted Lemon's avatar
Ted Lemon committed
47
int quiet_interface_discovery;
48 49
u_int16_t local_port;
u_int16_t remote_port;
Francis Dupont's avatar
Francis Dupont committed
50
u_int16_t relay_port = 0;
51
int dhcpv4_over_dhcpv6 = 0;
52 53
int (*dhcp_interface_setup_hook) (struct interface_info *, struct iaddr *);
int (*dhcp_interface_discovery_hook) (struct interface_info *);
Ted Lemon's avatar
Ted Lemon committed
54
isc_result_t (*dhcp_interface_startup_hook) (struct interface_info *);
55
int (*dhcp_interface_shutdown_hook) (struct interface_info *);
56 57

struct in_addr limited_broadcast;
David Hankins's avatar
David Hankins committed
58

59
int local_family = AF_INET;
60
struct in_addr local_address;
61

Francis Dupont's avatar
Francis Dupont committed
62 63 64 65 66 67 68 69
#ifdef DHCPv6
/*
 * Another clear abuse of the fact that undefined IP addresses are all zeroes.
 */
struct in6_addr local_address6;
int bind_local_address6 = 0;
#endif /* DHCPv6 */

70 71 72 73
void (*bootp_packet_handler) (struct interface_info *,
			      struct dhcp_packet *, unsigned,
			      unsigned int,
			      struct iaddr, struct hardware *);
74 75

#ifdef DHCPv6
David Hankins's avatar
David Hankins committed
76 77 78 79
void (*dhcpv6_packet_handler)(struct interface_info *,
			      const char *, int,
			      int, const struct iaddr *,
			      isc_boolean_t);
80
#endif /* DHCPv6 */
David Hankins's avatar
David Hankins committed
81

Ted Lemon's avatar
Ted Lemon committed
82

Ted Lemon's avatar
Ted Lemon committed
83
omapi_object_type_t *dhcp_type_interface;
84 85 86 87 88 89 90 91
#if defined (TRACING)
trace_type_t *interface_trace;
trace_type_t *inpacket_trace;
trace_type_t *outpacket_trace;
#endif
struct interface_info **interface_vector;
int interface_count;
int interface_max;
Ted Lemon's avatar
Ted Lemon committed
92

93 94
OMAPI_OBJECT_ALLOC (interface, struct interface_info, dhcp_type_interface)

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
isc_result_t interface_setup ()
{
	isc_result_t status;
	status = omapi_object_type_register (&dhcp_type_interface,
					     "interface",
					     dhcp_interface_set_value,
					     dhcp_interface_get_value,
					     dhcp_interface_destroy,
					     dhcp_interface_signal_handler,
					     dhcp_interface_stuff_values,
					     dhcp_interface_lookup, 
					     dhcp_interface_create,
					     dhcp_interface_remove,
					     0, 0, 0,
					     sizeof (struct interface_info),
110
					     interface_initialize, RC_MISC);
111 112 113
	if (status != ISC_R_SUCCESS)
		log_fatal ("Can't register interface object type: %s",
			   isc_result_totext (status));
114

115 116 117
	return status;
}

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
#if defined (TRACING)
void interface_trace_setup ()
{
	interface_trace = trace_type_register ("interface", (void *)0,
					       trace_interface_input,
					       trace_interface_stop, MDL);
	inpacket_trace = trace_type_register ("inpacket", (void *)0,
					       trace_inpacket_input,
					       trace_inpacket_stop, MDL);
	outpacket_trace = trace_type_register ("outpacket", (void *)0,
					       trace_outpacket_input,
					       trace_outpacket_stop, MDL);
}
#endif

Ted Lemon's avatar
Ted Lemon committed
133 134 135 136 137 138 139 140
isc_result_t interface_initialize (omapi_object_t *ipo,
				   const char *file, int line)
{
	struct interface_info *ip = (struct interface_info *)ipo;
	ip -> rfdesc = ip -> wfdesc = -1;
	return ISC_R_SUCCESS;
}

Ted Lemon's avatar
Ted Lemon committed
141

David Hankins's avatar
David Hankins committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
/* 
 * Scanning for Interfaces
 * -----------------------
 *
 * To find interfaces, we create an iterator that abstracts out most 
 * of the platform specifics. Use is fairly straightforward:
 *
 * - begin_iface_scan() starts the process.
 * - Use next_iface() until it returns 0.
 * - end_iface_scan() performs any necessary cleanup.
 *
 * We check for errors on each call to next_iface(), which returns a
 * description of the error as a string if any occurs.
 *
 * We currently have code for Solaris and Linux. Other systems need
 * to have code written.
 *
 * NOTE: the long-term goal is to use the interface code from BIND 9.
 */

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFNUM) && defined(SIOCGLIFFLAGS)

/* HP/UX doesn't define struct lifconf, instead they define struct
 * if_laddrconf.  Similarly, 'struct lifreq' and 'struct lifaddrreq'.
 */
#ifdef ISC_PLATFORM_HAVEIF_LADDRCONF
# define lifc_len iflc_len
# define lifc_buf iflc_buf
# define lifc_req iflc_req
# define LIFCONF if_laddrconf
#else
# define ISC_HAVE_LIFC_FAMILY 1
# define ISC_HAVE_LIFC_FLAGS 1
# define LIFCONF lifconf
#endif

#ifdef ISC_PLATFORM_HAVEIF_LADDRREQ
# define lifr_addr iflr_addr
# define lifr_name iflr_name
# define lifr_dstaddr iflr_dstaddr
# define lifr_flags iflr_flags
# define sockaddr_storage sockaddr_ext
# define ss_family sa_family
# define LIFREQ if_laddrreq
#else
# define LIFREQ lifreq
#endif

#ifndef IF_NAMESIZE
# if defined(LIFNAMSIZ)
#  define IF_NAMESIZE	LIFNAMSIZ
193
# elif defined(IFNAMSIZ)
194 195 196 197 198
#  define IF_NAMESIZE	IFNAMSIZ
# else
#  define IF_NAMESIZE	16
# endif
#endif
Mark Andrews's avatar
Mark Andrews committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
#elif !defined(__linux) && !defined(HAVE_IFADDRS_H)
# define SIOCGLIFCONF SIOCGIFCONF
# define SIOCGLIFFLAGS SIOCGIFFLAGS
# define LIFREQ ifreq
# define LIFCONF ifconf
# define lifr_name ifr_name
# define lifr_addr ifr_addr
# define lifr_flags ifr_flags
# define lifc_len ifc_len
# define lifc_buf ifc_buf
# define lifc_req ifc_req
#ifdef _AIX
# define ss_family __ss_family
#endif
#endif
214

Mark Andrews's avatar
Mark Andrews committed
215
#if defined(SIOCGLIFCONF) && defined(SIOCGLIFFLAGS)
David Hankins's avatar
David Hankins committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
/* 
 * Solaris support
 * ---------------
 *
 * The SIOCGLIFCONF ioctl() are the extension that you need to use
 * on Solaris to get information about IPv6 addresses.
 *
 * Solaris' extended interface is documented in the if_tcp man page.
 */

/* 
 * Structure holding state about the scan.
 */
struct iface_conf_list {
	int sock;		/* file descriptor used to get information */
	int num;		/* total number of interfaces */
232
	struct LIFCONF conf;	/* structure used to get information */
David Hankins's avatar
David Hankins committed
233 234 235 236 237 238 239
	int next;		/* next interface to retrieve when iterating */
};

/* 
 * Structure used to return information about a specific interface.
 */
struct iface_info {
240
	char name[IF_NAMESIZE+1];	/* name of the interface, e.g. "bge0" */
David Hankins's avatar
David Hankins committed
241 242 243 244 245 246 247 248 249 250 251
	struct sockaddr_storage addr;	/* address information */
	isc_uint64_t flags;		/* interface flags, e.g. IFF_LOOPBACK */
};

/* 
 * Start a scan of interfaces.
 *
 * The iface_conf_list structure maintains state for this process.
 */
int 
begin_iface_scan(struct iface_conf_list *ifaces) {
252
#ifdef ISC_PLATFORM_HAVELIFNUM
David Hankins's avatar
David Hankins committed
253
	struct lifnum lifnum;
254 255 256
#else
	int lifnum;
#endif
David Hankins's avatar
David Hankins committed
257

258
	ifaces->sock = socket(local_family, SOCK_DGRAM, IPPROTO_UDP);
David Hankins's avatar
David Hankins committed
259 260 261 262 263 264
	if (ifaces->sock < 0) {
		log_error("Error creating socket to list interfaces; %m");
		return 0;
	}

	memset(&lifnum, 0, sizeof(lifnum));
265
#ifdef ISC_PLATFORM_HAVELIFNUM
David Hankins's avatar
David Hankins committed
266
	lifnum.lifn_family = AF_UNSPEC;
267
#endif
Mark Andrews's avatar
Mark Andrews committed
268
#ifdef SIOCGLIFNUM
David Hankins's avatar
David Hankins committed
269 270 271 272 273 274 275
	if (ioctl(ifaces->sock, SIOCGLIFNUM, &lifnum) < 0) {
		log_error("Error finding total number of interfaces; %m");
		close(ifaces->sock);
		ifaces->sock = -1;
		return 0;
	}

276
#ifdef ISC_PLATFORM_HAVELIFNUM
David Hankins's avatar
David Hankins committed
277
	ifaces->num = lifnum.lifn_count;
278 279 280
#else
	ifaces->num = lifnum;
#endif
Mark Andrews's avatar
Mark Andrews committed
281 282 283
#else
	ifaces->num = 64;
#endif /* SIOCGLIFNUM */
284

David Hankins's avatar
David Hankins committed
285
	memset(&ifaces->conf, 0, sizeof(ifaces->conf));
286
#ifdef ISC_HAVE_LIFC_FAMILY
David Hankins's avatar
David Hankins committed
287
	ifaces->conf.lifc_family = AF_UNSPEC;
288 289
#endif
	ifaces->conf.lifc_len = ifaces->num * sizeof(struct LIFREQ);
David Hankins's avatar
David Hankins committed
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
	ifaces->conf.lifc_buf = dmalloc(ifaces->conf.lifc_len, MDL);
	if (ifaces->conf.lifc_buf == NULL) {
		log_fatal("Out of memory getting interface list.");
	}

	if (ioctl(ifaces->sock, SIOCGLIFCONF, &ifaces->conf) < 0) {
		log_error("Error getting interfaces configuration list; %m");
		dfree(ifaces->conf.lifc_buf, MDL);
		close(ifaces->sock);
		ifaces->sock = -1;
		return 0;
	}

	ifaces->next = 0;

	return 1;
}

/*
 * Retrieve the next interface.
 *
 * Returns information in the info structure. 
312
 * Sets err to 1 if there is an error, otherwise 0.
David Hankins's avatar
David Hankins committed
313 314 315
 */
int
next_iface(struct iface_info *info, int *err, struct iface_conf_list *ifaces) {
316 317
	struct LIFREQ *p;
	struct LIFREQ tmp;
318
	isc_boolean_t foundif;
319 320
#if defined(sun) || defined(__linux)
	/* Pointer used to remove interface aliases. */
Ted Lemon's avatar
Ted Lemon committed
321
	char *s;
322
#endif
Ted Lemon's avatar
Ted Lemon committed
323

David Hankins's avatar
David Hankins committed
324
	do {
325 326
		foundif = ISC_FALSE;

David Hankins's avatar
David Hankins committed
327 328 329 330
		if (ifaces->next >= ifaces->num) {
			*err = 0;
			return 0;
		}
Ted Lemon's avatar
Ted Lemon committed
331

David Hankins's avatar
David Hankins committed
332 333
		p = ifaces->conf.lifc_req;
		p += ifaces->next;
334

David Hankins's avatar
David Hankins committed
335 336 337 338 339
		if (strlen(p->lifr_name) >= sizeof(info->name)) {
			*err = 1;
			log_error("Interface name '%s' too long", p->lifr_name);
			return 0;
		}
340 341 342 343 344 345 346

		/* Reject if interface address family does not match */
		if (p->lifr_addr.ss_family != local_family) {
			ifaces->next++;
			continue;
		}

347 348
		memset(info, 0, sizeof(struct iface_info));
		strncpy(info->name, p->lifr_name, sizeof(info->name) - 1);
Mark Andrews's avatar
Mark Andrews committed
349
		memcpy(&info->addr, &p->lifr_addr, sizeof(p->lifr_addr));
350

351
#if defined(sun) || defined(__linux)
David Hankins's avatar
David Hankins committed
352 353 354 355 356
		/* interface aliases look like "eth0:1" or "wlan1:3" */
		s = strchr(info->name, ':');
		if (s != NULL) {
			*s = '\0';
		}
357
#endif /* defined(sun) || defined(__linux) */
David Hankins's avatar
David Hankins committed
358

359 360 361
		foundif = ISC_TRUE;
	} while ((foundif == ISC_FALSE) ||
		 (strncmp(info->name, "dummy", 5) == 0));
David Hankins's avatar
David Hankins committed
362 363
	
	memset(&tmp, 0, sizeof(tmp));
364
	strncpy(tmp.lifr_name, info->name, sizeof(tmp.lifr_name) - 1);
David Hankins's avatar
David Hankins committed
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
	if (ioctl(ifaces->sock, SIOCGLIFFLAGS, &tmp) < 0) {
		log_error("Error getting interface flags for '%s'; %m", 
			  p->lifr_name);
		*err = 1;
		return 0;
	}
	info->flags = tmp.lifr_flags;

	ifaces->next++;
	*err = 0;
	return 1;
}

/*
 * End scan of interfaces.
 */
void
end_iface_scan(struct iface_conf_list *ifaces) {
	dfree(ifaces->conf.lifc_buf, MDL);
	close(ifaces->sock);
	ifaces->sock = -1;
}

Mark Andrews's avatar
Mark Andrews committed
388
#else
389 390

/* 
391
 * BSD/Linux support
392 393
 * -----------
 *
394
 * FreeBSD, NetBSD, OpenBSD, OS X/macOS and Linux all have the getifaddrs() 
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
 * function.
 *
 * The getifaddrs() man page describes the use.
 */

#include <ifaddrs.h>

/* 
 * Structure holding state about the scan.
 */
struct iface_conf_list {
	struct ifaddrs *head;	/* beginning of the list */
	struct ifaddrs *next;	/* current position in the list */
};

/* 
 * Structure used to return information about a specific interface.
 */
struct iface_info {
	char name[IFNAMSIZ];		/* name of the interface, e.g. "bge0" */
	struct sockaddr_storage addr;	/* address information */
	isc_uint64_t flags;		/* interface flags, e.g. IFF_LOOPBACK */
};

/* 
 * Start a scan of interfaces.
 *
 * The iface_conf_list structure maintains state for this process.
 */
int 
begin_iface_scan(struct iface_conf_list *ifaces) {
	if (getifaddrs(&ifaces->head) != 0) {
		log_error("Error getting interfaces; %m");
		return 0;
	}
	ifaces->next = ifaces->head;
	return 1;
}

/*
 * Retrieve the next interface.
 *
 * Returns information in the info structure. 
 * Sets err to 1 if there is an error, otherwise 0.
 */
int
next_iface(struct iface_info *info, int *err, struct iface_conf_list *ifaces) {
442 443
	size_t sa_len = 0;

444 445 446 447 448 449 450 451 452 453
	if (ifaces->next == NULL) {
		*err = 0;
		return 0;
	}
	if (strlen(ifaces->next->ifa_name) >= sizeof(info->name)) {
		log_error("Interface name '%s' too long", 
			  ifaces->next->ifa_name);
		*err = 1;
		return 0;
	}
454 455
	memset(info, 0, sizeof(struct iface_info));
	strncpy(info->name, ifaces->next->ifa_name, sizeof(info->name) - 1);
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
	memset(&info->addr, 0 , sizeof(info->addr));
	/*
	 * getifaddrs() can on Linux with some interfaces like PPP or TEQL
	 * result in a record with no address (ifa_addr).
	 */
	if (ifaces->next->ifa_addr != NULL) {
/* Linux lacks the sa_len member in struct sockaddr. */
#if defined(__linux)
		if (ifaces->next->ifa_addr->sa_family == AF_INET)
			sa_len = sizeof(struct sockaddr_in);
		else if (ifaces->next->ifa_addr->sa_family == AF_INET6)
			sa_len = sizeof(struct sockaddr_in6);
#else
		sa_len = ifaces->next->ifa_addr->sa_len;
#endif
		memcpy(&info->addr, ifaces->next->ifa_addr, sa_len);
	}
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
	info->flags = ifaces->next->ifa_flags;
	ifaces->next = ifaces->next->ifa_next;
	*err = 0;
	return 1;
}

/*
 * End scan of interfaces.
 */
void
end_iface_scan(struct iface_conf_list *ifaces) {
	freeifaddrs(ifaces->head);
	ifaces->head = NULL;
	ifaces->next = NULL;
}
David Hankins's avatar
David Hankins committed
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
#endif 

/* XXX: perhaps create drealloc() rather than do it manually */
void
add_ipv4_addr_to_interface(struct interface_info *iface, 
			   const struct in_addr *addr) {
	/*
	 * We don't expect a lot of addresses per IPv4 interface, so
	 * we use 4, as our "chunk size" for collecting addresses.
	 */
	if (iface->addresses == NULL) {
		iface->addresses = dmalloc(4 * sizeof(struct in_addr), MDL);
		if (iface->addresses == NULL) {
			log_fatal("Out of memory saving IPv4 address "
			          "on interface.");
		}
		iface->address_count = 0;
		iface->address_max = 4;
	} else if (iface->address_count >= iface->address_max) {
		struct in_addr *tmp;
		int new_max;

		new_max = iface->address_max + 4;
		tmp = dmalloc(new_max * sizeof(struct in_addr), MDL);
		if (tmp == NULL) {
			log_fatal("Out of memory saving IPv4 address "
			          "on interface.");
		}
		memcpy(tmp, 
		       iface->addresses, 
		       iface->address_max * sizeof(struct in_addr));
		dfree(iface->addresses, MDL);
		iface->addresses = tmp;
		iface->address_max = new_max;
	}
	iface->addresses[iface->address_count++] = *addr;
}

526
#ifdef DHCPv6
David Hankins's avatar
David Hankins committed
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
/* XXX: perhaps create drealloc() rather than do it manually */
void
add_ipv6_addr_to_interface(struct interface_info *iface, 
			   const struct in6_addr *addr) {
	/*
	 * Each IPv6 interface will have at least two IPv6 addresses,
	 * and likely quite a few more. So we use 8, as our "chunk size" for
	 * collecting addresses.
	 */
	if (iface->v6addresses == NULL) {
		iface->v6addresses = dmalloc(8 * sizeof(struct in6_addr), MDL);
		if (iface->v6addresses == NULL) {
			log_fatal("Out of memory saving IPv6 address "
				  "on interface.");
		}
		iface->v6address_count = 0;
		iface->v6address_max = 8;
	} else if (iface->v6address_count >= iface->v6address_max) {
		struct in6_addr *tmp;
		int new_max;

		new_max = iface->v6address_max + 8;
		tmp = dmalloc(new_max * sizeof(struct in6_addr), MDL);
		if (tmp == NULL) {
			log_fatal("Out of memory saving IPv6 address "
				  "on interface.");
		}
		memcpy(tmp, 
		       iface->v6addresses, 
		       iface->v6address_max * sizeof(struct in6_addr));
		dfree(iface->v6addresses, MDL);
		iface->v6addresses = tmp;
		iface->v6address_max = new_max;
	}
	iface->v6addresses[iface->v6address_count++] = *addr;
}
563
#endif /* DHCPv6 */
David Hankins's avatar
David Hankins committed
564 565 566 567 568 569 570 571 572 573 574 575

/* Use the SIOCGIFCONF ioctl to get a list of all the attached interfaces.
   For each interface that's of type INET and not the loopback interface,
   register that interface with the network I/O software, figure out what
   subnet it's on, and add it to the list of interfaces. */

void 
discover_interfaces(int state) {
	struct iface_conf_list ifaces;
	struct iface_info info;
	int err;

576
	struct interface_info *tmp;
David Hankins's avatar
David Hankins committed
577 578
	struct interface_info *last, *next;

579 580 581 582
#ifdef DHCPv6
        char abuf[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
#endif /* DHCPv6 */

David Hankins's avatar
David Hankins committed
583 584 585 586 587

	struct subnet *subnet;
	int ir;
	isc_result_t status;
	int wifcount = 0;
Francis Dupont's avatar
Francis Dupont committed
588 589 590 591
#ifdef RELAY_PORT
	int updone = 0;
	int downdone = 0;
#endif
David Hankins's avatar
David Hankins committed
592 593 594 595 596

	static int setup_fallback = 0;

	if (!begin_iface_scan(&ifaces)) {
		log_fatal("Can't get list of interfaces.");
597
	}
598

Ted Lemon's avatar
Ted Lemon committed
599 600 601 602 603 604 605 606
	/* If we already have a list of interfaces, and we're running as
	   a DHCP server, the interfaces were requested. */
	if (interfaces && (state == DISCOVER_SERVER ||
			   state == DISCOVER_RELAY ||
			   state == DISCOVER_REQUESTED))
		ir = 0;
	else if (state == DISCOVER_UNCONFIGURED)
		ir = INTERFACE_REQUESTED | INTERFACE_AUTOMATIC;
607
	else {
Ted Lemon's avatar
Ted Lemon committed
608
		ir = INTERFACE_REQUESTED;
609 610 611 612 613 614
		if (state == DISCOVER_RELAY && local_family == AF_INET) {
			/* We're a v4 relay without specifically requested
			 * interfaces, so mark them all as bidirectional. */
			ir |= INTERFACE_STREAMS;
		}
	}
Ted Lemon's avatar
Ted Lemon committed
615

Ted Lemon's avatar
Ted Lemon committed
616
	/* Cycle through the list of interfaces looking for IP addresses. */
David Hankins's avatar
David Hankins committed
617
	while (next_iface(&info, &err, &ifaces)) {
Ted Lemon's avatar
Ted Lemon committed
618

619
		/* See if we've seen an interface that matches this one. */
David Hankins's avatar
David Hankins committed
620 621
		for (tmp = interfaces; tmp; tmp = tmp->next) {
			if (!strcmp(tmp->name, info.name))
622
				break;
David Hankins's avatar
David Hankins committed
623
		}
624

625 626 627 628
		/* Skip non broadcast interfaces (plus loopback and
		   point-to-point in case an OS incorrectly marks them
		   as broadcast). Also skip down interfaces unless we're
		   trying to get a list of configurable interfaces. */
629 630 631 632 633 634
		if ((((local_family == AF_INET &&
		    !(info.flags & IFF_BROADCAST)) ||
#ifdef DHCPv6
		    (local_family == AF_INET6 &&
		    !(info.flags & IFF_MULTICAST)) ||
#endif
David Hankins's avatar
David Hankins committed
635 636 637
		      info.flags & IFF_LOOPBACK ||
		      info.flags & IFF_POINTOPOINT) && !tmp) ||
		    (!(info.flags & IFF_UP) &&
Ted Lemon's avatar
Ted Lemon committed
638 639 640 641 642
		     state != DISCOVER_UNCONFIGURED))
			continue;
		
		/* If there isn't already an interface by this name,
		   allocate one. */
David Hankins's avatar
David Hankins committed
643 644 645 646 647 648
		if (tmp == NULL) {
			status = interface_allocate(&tmp, MDL);
			if (status != ISC_R_SUCCESS) {
				log_fatal("Error allocating interface %s: %s",
					  info.name, isc_result_totext(status));
			}
649 650 651

			memcpy(tmp->name, info.name, sizeof(tmp->name));

David Hankins's avatar
David Hankins committed
652 653
			interface_snorf(tmp, ir);
			interface_dereference(&tmp, MDL);
654
			tmp = interfaces; /* XXX */
655 656
		}

David Hankins's avatar
David Hankins committed
657 658 659
		if (dhcp_interface_discovery_hook) {
			(*dhcp_interface_discovery_hook)(tmp);
		}
660

David Hankins's avatar
David Hankins committed
661 662 663
		if ((info.addr.ss_family == AF_INET) && 
		    (local_family == AF_INET)) {
			struct sockaddr_in *a = (struct sockaddr_in*)&info.addr;
Ted Lemon's avatar
Ted Lemon committed
664 665
			struct iaddr addr;

David Hankins's avatar
David Hankins committed
666 667 668
			/* We don't want the loopback interface. */
			if (a->sin_addr.s_addr == htonl(INADDR_LOOPBACK) &&
			    ((tmp->flags & INTERFACE_AUTOMATIC) &&
669 670
			     ((state == DISCOVER_SERVER) ||
			      (state == DISCOVER_SERVER46))))
David Hankins's avatar
David Hankins committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
				continue;

			/* If the only address we have is 0.0.0.0, we
			   shouldn't consider the interface configured. */
			if (a->sin_addr.s_addr != htonl(INADDR_ANY))
				tmp->configured = 1;

			add_ipv4_addr_to_interface(tmp, &a->sin_addr);

			/* invoke the setup hook */
			addr.len = 4;
			memcpy(addr.iabuf, &a->sin_addr.s_addr, addr.len);
			if (dhcp_interface_setup_hook) {
				(*dhcp_interface_setup_hook)(tmp, &addr);
			}
		}
687
#ifdef DHCPv6
David Hankins's avatar
David Hankins committed
688 689 690 691 692
		else if ((info.addr.ss_family == AF_INET6) && 
			 (local_family == AF_INET6)) {
			struct sockaddr_in6 *a = 
					(struct sockaddr_in6*)&info.addr;
			struct iaddr addr;
Ted Lemon's avatar
Ted Lemon committed
693 694

			/* We don't want the loopback interface. */
David Hankins's avatar
David Hankins committed
695 696
			if (IN6_IS_ADDR_LOOPBACK(&a->sin6_addr) && 
			    ((tmp->flags & INTERFACE_AUTOMATIC) &&
697 698
			     ((state == DISCOVER_SERVER) ||
			      (state == DISCOVER_SERVER46))))
699
			    continue;
Ted Lemon's avatar
Ted Lemon committed
700

David Hankins's avatar
David Hankins committed
701 702 703 704
			/* If the only address we have is 0.0.0.0, we
			   shouldn't consider the interface configured. */
			if (IN6_IS_ADDR_UNSPECIFIED(&a->sin6_addr))
				tmp->configured = 1;
Ted Lemon's avatar
Ted Lemon committed
705

David Hankins's avatar
David Hankins committed
706 707 708 709 710 711 712 713
			add_ipv6_addr_to_interface(tmp, &a->sin6_addr);

			/* invoke the setup hook */
			addr.len = 16;
			memcpy(addr.iabuf, &a->sin6_addr, addr.len);
			if (dhcp_interface_setup_hook) {
				(*dhcp_interface_setup_hook)(tmp, &addr);
			}
Ted Lemon's avatar
Ted Lemon committed
714
		}
715
#endif /* DHCPv6 */
Ted Lemon's avatar
Ted Lemon committed
716 717
	}

David Hankins's avatar
David Hankins committed
718 719 720 721 722
	if (err) {
		log_fatal("Error getting interface information.");
	}

	end_iface_scan(&ifaces);
723

Ted Lemon's avatar
Ted Lemon committed
724

725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
	/* Mock-up an 'ifp' structure which is no longer used in the
	 * new interface-sensing code, but is used in higher layers
	 * (for example to sense fallback interfaces).
	 */
	for (tmp = interfaces ; tmp != NULL ; tmp = tmp->next) {
		if (tmp->ifp == NULL) {
			struct ifreq *tif;

			tif = (struct ifreq *)dmalloc(sizeof(struct ifreq),
						      MDL);
			if (tif == NULL)
				log_fatal("no space for ifp mockup.");
			strcpy(tif->ifr_name, tmp->name);
			tmp->ifp = tif;
		}
	}


Ted Lemon's avatar
Ted Lemon committed
743 744
	/* If we're just trying to get a list of interfaces that we might
	   be able to configure, we can quit now. */
745
	if (state == DISCOVER_UNCONFIGURED) {
Ted Lemon's avatar
Ted Lemon committed
746
		return;
747
	}
Ted Lemon's avatar
Ted Lemon committed
748 749

	/* Weed out the interfaces that did not have IP addresses. */
David Hankins's avatar
David Hankins committed
750
	tmp = last = next = NULL;
751 752 753 754 755 756 757
	if (interfaces)
		interface_reference (&tmp, interfaces, MDL);
	while (tmp) {
		if (next)
			interface_dereference (&next, MDL);
		if (tmp -> next)
			interface_reference (&next, tmp -> next, MDL);
758
		/* skip interfaces that are running already */
759 760 761 762
		if (tmp -> flags & INTERFACE_RUNNING) {
			interface_dereference(&tmp, MDL);
			if(next)
				interface_reference(&tmp, next, MDL);
763
			continue;
764
		}
Ted Lemon's avatar
Ted Lemon committed
765 766 767 768
		if ((tmp -> flags & INTERFACE_AUTOMATIC) &&
		    state == DISCOVER_REQUESTED)
			tmp -> flags &= ~(INTERFACE_AUTOMATIC |
					  INTERFACE_REQUESTED);
769 770

#ifdef DHCPv6
David Hankins's avatar
David Hankins committed
771
		if (!(tmp->flags & INTERFACE_REQUESTED)) {
772 773 774
#else
		if (!tmp -> ifp || !(tmp -> flags & INTERFACE_REQUESTED)) {
#endif /* DHCPv6 */
Ted Lemon's avatar
Ted Lemon committed
775
			if ((tmp -> flags & INTERFACE_REQUESTED) != ir)
776
				log_fatal ("%s: not found", tmp -> name);
777 778 779 780
			if (!last) {
				if (interfaces)
					interface_dereference (&interfaces,
							       MDL);
781
				if (next)
782 783 784
				interface_reference (&interfaces, next, MDL);
			} else {
				interface_dereference (&last -> next, MDL);
785 786 787
				if (next)
					interface_reference (&last -> next,
							     next, MDL);
788 789 790
			}
			if (tmp -> next)
				interface_dereference (&tmp -> next, MDL);
Ted Lemon's avatar
Ted Lemon committed
791 792 793

			/* Remember the interface in case we need to know
			   about it later. */
794 795 796 797 798 799 800 801 802
			if (dummy_interfaces) {
				interface_reference (&tmp -> next,
						     dummy_interfaces, MDL);
				interface_dereference (&dummy_interfaces, MDL);
			}
			interface_reference (&dummy_interfaces, tmp, MDL);
			interface_dereference (&tmp, MDL);
			if (next)
				interface_reference (&tmp, next, MDL);
Ted Lemon's avatar
Ted Lemon committed
803 804 805 806 807
			continue;
		}
		last = tmp;

		/* We must have a subnet declaration for each interface. */
David Hankins's avatar
David Hankins committed
808 809 810 811 812
		if (!tmp->shared_network && (state == DISCOVER_SERVER)) {
			log_error("%s", "");
			if (local_family == AF_INET) {
				log_error("No subnet declaration for %s (%s).",
					  tmp->name, 
813 814 815
					  (tmp->addresses == NULL) ?
					   "no IPv4 addresses" :
					   inet_ntoa(tmp->addresses[0]));
816
#ifdef DHCPv6
David Hankins's avatar
David Hankins committed
817 818 819 820 821 822 823
			} else {
				if (tmp->v6addresses != NULL) {
					inet_ntop(AF_INET6, 
						  &tmp->v6addresses[0],
						  abuf,
						  sizeof(abuf));
				} else {
824
					strcpy(abuf, "no IPv6 addresses");
David Hankins's avatar
David Hankins committed
825
				}
826
				log_error("No subnet6 declaration for %s (%s).",
David Hankins's avatar
David Hankins committed
827 828
					  tmp->name,
					  abuf);
829
#endif /* DHCPv6 */
David Hankins's avatar
David Hankins committed
830 831
			}
			if (supports_multiple_interfaces(tmp)) {
832 833 834
				log_error ("** Ignoring requests on %s.  %s",
					   tmp -> name, "If this is not what");
				log_error ("   you want, please write %s",
835 836 837 838
#ifdef DHCPv6
				           (local_family != AF_INET) ?
					   "a subnet6 declaration" :
#endif
839 840 841 842
					   "a subnet declaration");
				log_error ("   in your dhcpd.conf file %s",
					   "for the network segment");
				log_error ("   to %s %s %s",
843
					   "which interface",
844 845
					   tmp -> name, "is attached. **");
				log_error ("%s", "");
846 847
				goto next;
			} else {
848 849 850 851 852 853
				log_error ("You must write a %s",
#ifdef DHCPv6
				           (local_family != AF_INET) ?
					   "subnet6 declaration for this" :
#endif
					   "subnet declaration for this");
854 855 856 857 858 859 860
				log_error ("subnet.   You cannot prevent %s",
					   "the DHCP server");
				log_error ("from listening on this subnet %s",
					   "because your");
				log_fatal ("operating system does not %s.",
					   "support this capability");
			}
861
		}
Ted Lemon's avatar
Ted Lemon committed
862 863 864 865 866 867 868

		/* Find subnets that don't have valid interface
		   addresses... */
		for (subnet = (tmp -> shared_network
			       ? tmp -> shared_network -> subnets
			       : (struct subnet *)0);
		     subnet; subnet = subnet -> next_sibling) {
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
			/* Set the interface address for this subnet
			   to the first address we found. */
		     	if (subnet->interface_address.len == 0) {
				if (tmp->address_count > 0) {
					subnet->interface_address.len = 4;
					memcpy(subnet->interface_address.iabuf,
					       &tmp->addresses[0].s_addr, 4);
				} else if (tmp->v6address_count > 0) {
					subnet->interface_address.len = 16;
					memcpy(subnet->interface_address.iabuf,
					       &tmp->v6addresses[0].s6_addr, 
					       16);
				} else {
					/* XXX: should be one */
					log_error("%s missing an interface "
						  "address", tmp->name);
					continue;
				}
Ted Lemon's avatar
Ted Lemon committed
887 888 889
			}
		}

890 891 892 893
		/* Flag the index as not having been set, so that the
		   interface registerer can set it or not as it chooses. */
		tmp -> index = -1;

Ted Lemon's avatar
Ted Lemon committed
894
		/* Register the interface... */
895 896 897 898 899 900 901 902 903 904
		switch (local_family) {
		case AF_INET:
			if (!dhcpv4_over_dhcpv6) {
				if_register_receive(tmp);
				if_register_send(tmp);
			} else {
				/* get_hw_addr() was called by register. */
				get_hw_addr(tmp->name, &tmp->hw_address);
			}
			break;
905
#ifdef DHCPv6
906
		case AF_INET6:
Evan Hunt's avatar
Evan Hunt committed
907 908
			if ((state == DISCOVER_SERVER) ||
			    (state == DISCOVER_RELAY)) {
David Hankins's avatar
David Hankins committed
909
				if_register6(tmp, 1);
910 911 912 913 914 915
			} else if (state == DISCOVER_SERVER46) {
				/* get_hw_addr() was called by if_register*6
				   so now we have to call it explicitly
				   to not leave the hardware address unknown
				   (some code expects it cannot be. */
				get_hw_addr(tmp->name, &tmp->hw_address);
David Hankins's avatar
David Hankins committed
916
			} else {
Shawn Routhier's avatar
Shawn Routhier committed
917
				if_register_linklocal6(tmp);
David Hankins's avatar
David Hankins committed
918
			}
919
			break;
920
#endif /* DHCPv6 */
David Hankins's avatar
David Hankins committed
921
		}
922 923

		interface_stash (tmp);
924
		wifcount++;
925
#if defined (F_SETFD)
926 927 928 929 930 931 932 933 934
		/* if_register*() are no longer always called so
		   descriptors  must be checked. */
		if ((tmp -> rfdesc >= 0) &&
		    (fcntl (tmp -> rfdesc, F_SETFD, 1) < 0))
			log_error ("Can't set close-on-exec on %s: %m",
				   tmp -> name);
		if ((tmp -> wfdesc != tmp -> rfdesc) &&
		    (tmp -> wfdesc >= 0) &&
		    (fcntl (tmp -> wfdesc, F_SETFD, 1) < 0))
935 936 937
			log_error ("Can't set close-on-exec on %s: %m",
				   tmp -> name);
#endif
938
	      next:
939 940 941
		interface_dereference (&tmp, MDL);
		if (next)
			interface_reference (&tmp, next, MDL);
Ted Lemon's avatar
Ted Lemon committed
942 943
	}

944 945 946 947 948 949
	/*
	 * Now register all the remaining interfaces as protocols.
	 * We register with omapi to allow for control of the interface,
	 * we've already registered the fd or socket with the socket
	 * manager as part of if_register_receive().
	 */
Ted Lemon's avatar
Ted Lemon committed
950
	for (tmp = interfaces; tmp; tmp = tmp -> next) {
951 952 953
		/* not if it's been registered before */
		if (tmp -> flags & INTERFACE_RUNNING)
			continue;
Ted Lemon's avatar
Ted Lemon committed
954 955
		if (tmp -> rfdesc == -1)
			continue;
956
		switch (local_family) {
957
#ifdef DHCPv6 
958
		case AF_INET6:
Francis Dupont's avatar
Francis Dupont committed
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
#ifdef RELAY_PORT
#define UPSTREAM(ifp) \
	((ifp->flags & INTERFACE_STREAMS) == INTERFACE_UPSTREAM)
#define DOWNSTREAM(ifp) \
        ((ifp->flags & INTERFACE_STREAMS) == INTERFACE_DOWNSTREAM)

			if (relay_port) {
				/*
				 * The normal IPv6 relay only needs one
				 * socket as long as we find an interface.
				 * When user relay port is defined, and we
				 * have two different UDP ports. One to
				 * receive from DHCP client with port 547,
				 * and the other is user defined for sending
				 * to the server or upstream relay agent.
				 * Thus we need to register sockets for one
				 * upstream and one downstream interfaces.
				 */
				if (updone && UPSTREAM(tmp))
					    continue;
				if (downdone && DOWNSTREAM(tmp))
					    continue;
			}
#endif
David Hankins's avatar
David Hankins committed
983 984
			status = omapi_register_io_object((omapi_object_t *)tmp,
							  if_readsocket, 
985
							  0, got_one_v6, 0, 0);
Francis Dupont's avatar
Francis Dupont committed
986 987 988 989 990 991
#ifdef RELAY_PORT
			if (UPSTREAM(tmp))
				updone++;
			else
				downdone++;
#endif
992
			break;
993
#endif /* DHCPv6 */
994 995
		case AF_INET:
		default:
David Hankins's avatar
David Hankins committed
996 997
			status = omapi_register_io_object((omapi_object_t *)tmp,
							  if_readsocket, 
998
							  0, got_one, 0, 0);
999
			break;
David Hankins's avatar
David Hankins committed
1000
		}
1001

Ted Lemon's avatar
Ted Lemon committed
1002 1003 1004
		if (status != ISC_R_SUCCESS)
			log_fatal ("Can't register I/O handle for %s: %s",
				   tmp -> name, isc_result_totext (status));
1005 1006

#if defined(DHCPv6)
Shawn Routhier's avatar
Shawn Routhier committed
1007 1008 1009 1010 1011
		/* Only register the first interface for V6, since
		 * servers and relays all use the same socket.
		 * XXX: This has some messy side effects if we start
		 * dynamically adding and removing interfaces, but
		 * we're well beyond that point in terms of mess.
1012
		 */
Francis Dupont's avatar
Francis Dupont committed
1013 1014 1015 1016 1017 1018
		if (((state == DISCOVER_SERVER) || (state == DISCOVER_RELAY))
		    && (local_family == AF_INET6)
#if defined(RELAY_PORT)
		    && ((relay_port == 0) || (updone && downdone))
#endif
		    )
1019 1020
			break;
#endif
1021
	} /* for (tmp = interfaces; ... */
Ted Lemon's avatar
Ted Lemon committed
1022

1023 1024 1025 1026 1027
	if (state == DISCOVER_SERVER && wifcount == 0) {
		log_info ("%s", "");
		log_fatal ("Not configured to listen on any interfaces!");
	}

1028 1029
	if ((local_family == AF_INET) &&
	    !setup_fallback && !dhcpv4_over_dhcpv6) {
1030
		setup_fallback = 1;
David Hankins's avatar
David Hankins committed
1031
		maybe_setup_fallback();
1032 1033
	}

1034
#if defined (F_SETFD)
1035 1036 1037 1038 1039 1040 1041 1042
	if (fallback_interface) {
	    if (fcntl (fallback_interface -> rfdesc, F_SETFD, 1) < 0)
		log_error ("Can't set close-on-exec on fallback: %m");
	    if (fallback_interface -> rfdesc != fallback_interface -> wfdesc) {
		if (fcntl (fallback_interface -> wfdesc, F_SETFD, 1) < 0)
		    log_error ("Can't set close-on-exec on fallback: %m");
	    }
	}
1043
#endif /* F_SETFD */
Ted Lemon's avatar
Ted Lemon committed
1044 1045
}

Ted Lemon's avatar
Ted Lemon committed
1046 1047 1048 1049 1050 1051
int if_readsocket (h)
	omapi_object_t *h;
{
	struct interface_info *ip;

	if (h -> type != dhcp_type_interface)
1052
		return -1;
Ted Lemon's avatar
Ted Lemon committed
1053 1054 1055 1056
	ip = (struct interface_info *)h;
	return ip -> rfdesc;
}

1057
int setup_fallback (struct interface_info **fp, const char *file, int line)
Ted Lemon's avatar
Ted Lemon committed
1058
{
1059 1060 1061 1062 1063 1064
	isc_result_t status;

	status = interface_allocate (&fallback_interface, file, line);
	if (status != ISC_R_SUCCESS)
		log_fatal ("Error allocating fallback interface: %s",
			   isc_result_totext (status));
Ted Lemon's avatar
Ted Lemon committed
1065
	strcpy (fallback_interface -> name, "fallback");
1066 1067 1068 1069
	if (dhcp_interface_setup_hook)
		(*dhcp_interface_setup_hook) (fallback_interface,
					      (struct iaddr *)0);
	status = interface_reference (fp, fallback_interface, file, line);
1070

1071
	fallback_interface -> index = -1;
1072
	interface_stash (fallback_interface);
1073
	return status == ISC_R_SUCCESS;
Ted Lemon's avatar
Ted Lemon committed
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
}

void reinitialize_interfaces ()
{
	struct interface_info *ip;

	for (ip = interfaces; ip; ip = ip -> next) {
		if_reinitialize_receive (ip);
		if_reinitialize_send (ip);
	}

Ted Lemon's avatar
Ted Lemon committed
1085 1086
	if (fallback_interface)
		if_reinitialize_send (fallback_interface);
Ted Lemon's avatar
Ted Lemon committed
1087 1088 1089

	interfaces_invalidated = 1;
}
Ted Lemon's avatar
Ted Lemon committed
1090

Ted Lemon's avatar
Ted Lemon committed
1091 1092
isc_result_t got_one (h)
	omapi_object_t *h;
Ted Lemon's avatar
Ted Lemon committed
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
{
	struct sockaddr_in from;
	struct hardware hfrom;
	struct iaddr ifrom;
	int result;
	union {
		unsigned char packbuf [4095]; /* Packet input buffer.
					 	 Must be as large as largest
						 possible MTU. */
		struct dhcp_packet packet;
	} u;
Ted Lemon's avatar
Ted Lemon committed
1104 1105 1106
	struct interface_info *ip;

	if (h -> type != dhcp_type_interface)
1107
		return DHCP_R_INVALIDARG;
Ted Lemon's avatar
Ted Lemon committed
1108
	ip = (struct interface_info *)h;
Ted Lemon's avatar
Ted Lemon committed
1109

1110
      again:
Ted Lemon's avatar
Ted Lemon committed
1111 1112
	if ((result =
	     receive_packet (ip, u.packbuf, sizeof u, &from, &hfrom)) < 0) {
1113
		log_error ("receive_packet failed on %s: %m", ip -> name);
Ted Lemon's avatar
Ted Lemon committed
1114
		return ISC_R_UNEXPECTED;
Ted Lemon's avatar
Ted Lemon committed
1115 1116
	}
	if (result == 0)
Ted Lemon's avatar
Ted Lemon committed
1117
		return ISC_R_UNEXPECTED;
Ted Lemon's avatar
Ted Lemon committed
1118

1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
	/*
	 * If we didn't at least get the fixed portion of the BOOTP
	 * packet, drop the packet.
	 * Previously we allowed packets with no sname or filename
	 * as we were aware of at least one client that did.  But
	 * a bug caused short packets to not work and nobody has
	 * complained, it seems rational to tighten up that
	 * restriction.
	 */
	if (result < DHCP_FIXED_NON_UDP)
1129 1130
		return ISC_R_UNEXPECTED;

1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
#if defined(IP_PKTINFO) && defined(IP_RECVPKTINFO) && defined(USE_V4_PKTINFO)
	{
		/* We retrieve the ifindex from the unused hfrom variable */
		unsigned int ifindex;

		memcpy(&ifindex, hfrom.hbuf, sizeof (ifindex));

		/*
		 * Seek forward from the first interface to find the matching
		 * source interface by interface index.
		 */
		ip = interfaces;
		while ((ip != NULL) && (if_nametoindex(ip->name) != ifindex))
			ip = ip->next;
		if (ip == NULL)
			return ISC_R_NOTFOUND;
	}
#endif

Ted Lemon's avatar
Ted Lemon committed
1150 1151 1152 1153
	if (bootp_packet_handler) {
		ifrom.len = 4;
		memcpy (ifrom.iabuf, &from.sin_addr, ifrom.len);

1154
		(*bootp_packet_handler) (ip, &u.packet, (unsigned)result,
Ted Lemon's avatar
Ted Lemon committed
1155 1156
					 from.sin_port, ifrom, &hfrom);
	}
1157 1158 1159 1160 1161

	/* If there is buffered data, read again.    This is for, e.g.,
	   bpf, which may return two packets at once. */
	if (ip -> rbuf_offset != ip -> rbuf_len)
		goto again;
Ted Lemon's avatar
Ted Lemon committed
1162 1163 1164
	return ISC_R_SUCCESS;
}

1165
#ifdef DHCPv6
David Hankins's avatar
David Hankins committed
1166 1167 1168 1169 1170 1171 1172 1173 1174
isc_result_t
got_one_v6(omapi_object_t *h) {
	struct sockaddr_in6 from;
	struct in6_addr to;
	struct iaddr ifrom;
	int result;
	char buf[65536];	/* maximum size for a UDP packet is 65536 */
	struct interface_info *ip;
	int is_unicast;
1175
	unsigned int if_idx = 0;
David Hankins's avatar
David Hankins committed
1176 1177

	if (h->type != dhcp_type_interface) {
1178
		return DHCP_R_INVALIDARG;
David Hankins's avatar
David Hankins committed
1179 1180 1181
	}
	ip = (struct interface_info *)h;

1182
	result = receive_packet6(ip, (unsigned char *)buf, sizeof(buf),
1183
				 &from, &to, &if_idx);
David Hankins's avatar
David Hankins committed
1184 1185 1186 1187 1188
	if (result < 0) {
		log_error("receive_packet6() failed on %s: %m", ip->name);
		return ISC_R_UNEXPECTED;
	}

1189 1190 1191 1192
	/* 0 is 'any' interface. */
	if (if_idx == 0)
		return ISC_R_NOTFOUND;

David Hankins's avatar
David Hankins committed
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
	if (dhcpv6_packet_handler != NULL) {
		/*
		 * If a packet is not multicast, we assume it is unicast.
		 */
		if (IN6_IS_ADDR_MULTICAST(&to)) { 
			is_unicast = ISC_FALSE;
		} else {
			is_unicast = ISC_TRUE;
		}

		ifrom.len = 16;
		memcpy(ifrom.iabuf, &from.sin6_addr, ifrom.len);

1206
		/* Seek forward to find the matching source interface. */
1207
		ip = interfaces;
1208 1209 1210 1211 1212 1213
		while ((ip != NULL) && (if_nametoindex(ip->name) != if_idx))
			ip = ip->next;

		if (ip == NULL)
			return ISC_R_NOTFOUND;

David Hankins's avatar
David Hankins committed
1214 1215 1216 1217 1218 1219 1220
		(*dhcpv6_packet_handler)(ip, buf, 
					 result, from.sin6_port, 
					 &ifrom, is_unicast);
	}

	return ISC_R_SUCCESS;
}
1221
#endif /* DHCPv6 */
David Hankins's avatar
David Hankins committed
1222

1223 1224 1225 1226
isc_result_t dhcp_interface_set_value  (omapi_object_t *h,
					omapi_object_t *id,
					omapi_data_string_t *name,
					omapi_typed_data_t *value)
Ted Lemon's avatar
Ted Lemon committed
1227
{
1228 1229 1230
	struct interface_info *interface;
	isc_result_t status;

Ted Lemon's avatar
Ted Lemon committed
1231
	if (h -> type != dhcp_type_interface)
1232
		return DHCP_R_INVALIDARG;
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
	interface = (struct interface_info *)h;

	if (!omapi_ds_strcmp (name, "name")) {
		if ((value -> type == omapi_datatype_data ||
		     value -> type == omapi_datatype_string) &&
		    value -> u.buffer.len < sizeof interface -> name) {
			memcpy (interface -> name,
				value -> u.buffer.value,
				value -> u.buffer.len);
			interface -> name [value -> u.buffer.len] = 0;
		} else
1244
			return DHCP_R_INVALIDARG;
1245 1246 1247 1248 1249 1250 1251
		return ISC_R_SUCCESS;
	}

	/* Try to find some inner object that can take the value. */
	if (h -> inner && h -> inner -> type -> set_value) {
		status = ((*(h -> inner -> type -> set_value))
			  (h -> inner, id, name, value));
1252
		if (status == ISC_R_SUCCESS || status == DHCP_R_UNCHANGED)
1253 1254 1255
			return status;
	}
			  
Ted Lemon's avatar
Ted Lemon committed
1256 1257 1258
	return ISC_R_NOTFOUND;
}

1259 1260 1261 1262 1263

isc_result_t dhcp_interface_get_value (omapi_object_t *h,
				       omapi_object_t *id,
				       omapi_data_string_t *name,
				       omapi_value_t **value)
Ted Lemon's avatar
Ted Lemon committed
1264
{
1265 1266 1267 1268 1269 1270 1271 1272
	return ISC_R_NOTIMPLEMENTED;
}

isc_result_t dhcp_interface_destroy (omapi_object_t *h,
					 const char *file, int line)
{
	struct interface_info *interface;

Ted Lemon's avatar
Ted Lemon committed
1273
	if (h -> type != dhcp_type_interface)
1274
		return DHCP_R_INVALIDARG;
1275 1276
	interface = (struct interface_info *)h;

1277
	if (interface -> ifp) {
1278
		dfree (interface -> ifp, file, line);
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
		interface -> ifp = 0;
	}
	if (interface -> next)
		interface_dereference (&interface -> next, file, line);
	if (interface -> rbuf) {
		dfree (interface -> rbuf, file, line);
		interface -> rbuf = (unsigned char *)0;
	}
	if (interface -> client)
		interface -> client = (struct client_state *)0;

	if (interface -> shared_network)
		omapi_object_dereference ((omapi_object_t **)
					  &interface -> shared_network, MDL);

1294
	return ISC_R_SUCCESS;
Ted Lemon's avatar
Ted Lemon committed
1295 1296
}

1297 1298
isc_result_t dhcp_interface_signal_handler (omapi_object_t *h,
					    const char *name, va_list ap)
Ted Lemon's avatar
Ted Lemon committed
1299
{
1300 1301 1302 1303
	struct interface_info *ip, *interface;
	isc_result_t status;

	if (h -> type != dhcp_type_interface)
1304
		return DHCP_R_INVALIDARG;
1305 1306
	interface = (struct interface_info *)h;

Ted Lemon's avatar
Ted Lemon committed
1307 1308 1309 1310 1311 1312 1313 1314
	/* If it's an update signal, see if the interface is dead right
	   now, or isn't known at all, and if that's the case, revive it. */
	if (!strcmp (name, "update")) {
		for (ip = dummy_interfaces; ip; ip = ip -> next)
			if (ip == interface)
				break;
		if (ip && dhcp_interface_startup_hook)
			return (*dhcp_interface_startup_hook) (ip);
1315

Ted Lemon's avatar
Ted Lemon committed
1316 1317 1318 1319 1320
		for (ip = interfaces; ip; ip = ip -> next)
			if (ip == interface)
				break;
		if (!ip && dhcp_interface_startup_hook)
			return (*dhcp_interface_startup_hook) (ip);
1321 1322 1323
	}

	/* Try to find some inner object that can take the value. */
1324
	if (h -> inner && h -> inner -> type -> signal_handler) {
1325 1326 1327 1328 1329
		status = ((*(h -> inner -> type -> signal_handler))
			  (h -> inner, name, ap));
		if (status == ISC_R_SUCCESS)
			return status;
	}
Ted Lemon's avatar
Ted Lemon committed
1330 1331 1332
	return ISC_R_NOTFOUND;
}

1333 1334 1335
isc_result_t dhcp_interface_stuff_values (omapi_object_t *c,
					  omapi_object_t *id,
					  omapi_object_t *h)
Ted Lemon's avatar
Ted Lemon committed
1336
{
1337 1338
	struct interface_info *interface;
	isc_result_t status;
Ted Lemon's avatar
Ted Lemon committed
1339 1340

	if (h -> type != dhcp_type_interface)
1341
		return DHCP_R_INVALIDARG;
1342 1343 1344 1345 1346 1347 1348
	interface = (struct interface_info *)h;

	/* Write out all the values. */

	status = omapi_connection_put_name (c, "state");
	if (status != ISC_R_SUCCESS)
		return status;
1349
	if ((interface->flags & INTERFACE_REQUESTED) != 0)
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
	    status = omapi_connection_put_string (c, "up");
	else
	    status = omapi_connection_put_string (c, "down");
	if (status != ISC_R_SUCCESS)
		return status;

	/* Write out the inner object, if any. */
	if (h -> inner && h -> inner -> type -> stuff_values) {
		status = ((*(h -> inner -> type -> stuff_values))
			  (c, id, h -> inner));
		if (status == ISC_R_SUCCESS)
			return status;
	}

Ted Lemon's avatar
Ted Lemon committed
1364 1365 1366
	return ISC_R_SUCCESS;
}

1367 1368 1369
isc_result_t dhcp_interface_lookup (omapi_object_t **ip,
				    omapi_object_t *id,
				    omapi_object_t *ref)
Ted Lemon's avatar
Ted Lemon committed
1370
{
1371 1372 1373 1374
	omapi_value_t *tv = (omapi_value_t *)0;
	isc_result_t status;
	struct interface_info *interface;

1375
	if (!ref)
1376
		return DHCP_R_NOKEYS;
1377

1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
	/* First see if we were sent a handle. */
	status = omapi_get_value_str (ref, id, "handle", &tv);
	if (status == ISC_R_SUCCESS) {
		status = omapi_handle_td_lookup (ip, tv -> value);

		omapi_value_dereference (&tv, MDL);
		if (status != ISC_R_SUCCESS)
			return status;

		/* Don't return the object if the type is wrong. */
		if ((*ip) -> type != dhcp_type_interface) {
			omapi_object_dereference (ip, MDL);
1390
			return DHCP_R_INVALIDARG;
1391 1392 1393 1394 1395 1396
		}
	}

	/* Now look for an interface name. */
	status = omapi_get_value_str (ref, id, "name", &tv);
	if (status == ISC_R_SUCCESS) {
Ted Lemon's avatar
Ted Lemon committed
1397 1398
		char *s;
		unsigned len;
1399 1400
		for (interface = interfaces; interface;
		     interface = interface -> next) {
Ted Lemon's avatar
Ted Lemon committed
1401 1402 1403 1404 1405 1406 1407