17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5004388ebScasper  * Common Development and Distribution License (the "License").
6004388ebScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*d04ccbb3Scarlsonj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <errno.h>
317c478bd9Sstevel@tonic-gate #include <locale.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <signal.h>
357c478bd9Sstevel@tonic-gate #include <stdio.h>
36004388ebScasper #include <stdio_ext.h>
377c478bd9Sstevel@tonic-gate #include <dhcp_hostconf.h>
387c478bd9Sstevel@tonic-gate #include <dhcpagent_ipc.h>
39*d04ccbb3Scarlsonj #include <dhcpagent_util.h>
407c478bd9Sstevel@tonic-gate #include <dhcpmsg.h>
417c478bd9Sstevel@tonic-gate #include <netinet/dhcp.h>
4269bb4bb4Scarlsonj #include <net/route.h>
4369bb4bb4Scarlsonj #include <sys/sockio.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include "async.h"
467c478bd9Sstevel@tonic-gate #include "agent.h"
477c478bd9Sstevel@tonic-gate #include "script_handler.h"
487c478bd9Sstevel@tonic-gate #include "util.h"
497c478bd9Sstevel@tonic-gate #include "class_id.h"
507c478bd9Sstevel@tonic-gate #include "states.h"
517c478bd9Sstevel@tonic-gate #include "packet.h"
52*d04ccbb3Scarlsonj #include "interface.h"
53*d04ccbb3Scarlsonj #include "defaults.h"
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #ifndef	TEXT_DOMAIN
567c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
577c478bd9Sstevel@tonic-gate #endif
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate iu_timer_id_t		inactivity_id;
607c478bd9Sstevel@tonic-gate int			class_id_len = 0;
617c478bd9Sstevel@tonic-gate char			*class_id;
627c478bd9Sstevel@tonic-gate iu_eh_t			*eh;
637c478bd9Sstevel@tonic-gate iu_tq_t			*tq;
647c478bd9Sstevel@tonic-gate pid_t			grandparent;
6569bb4bb4Scarlsonj int			rtsock_fd;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static boolean_t	shutdown_started = B_FALSE;
687c478bd9Sstevel@tonic-gate static boolean_t	do_adopt = B_FALSE;
697c478bd9Sstevel@tonic-gate static unsigned int	debug_level = 0;
7069bb4bb4Scarlsonj static iu_eh_callback_t	accept_event, ipc_event, rtsock_event;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  * The ipc_cmd_allowed[] table indicates which IPC commands are allowed in
747c478bd9Sstevel@tonic-gate  * which states; a non-zero value indicates the command is permitted.
757c478bd9Sstevel@tonic-gate  *
76*d04ccbb3Scarlsonj  * START is permitted if the state machine is fresh, or if we are in the
77*d04ccbb3Scarlsonj  * process of trying to obtain a lease (as a convenience to save the
78*d04ccbb3Scarlsonj  * administrator from having to do an explicit DROP).  EXTEND, RELEASE, and
79*d04ccbb3Scarlsonj  * GET_TAG require a lease to be obtained in order to make sense.  INFORM is
80*d04ccbb3Scarlsonj  * permitted if the interface is fresh or has an INFORM in progress or
81*d04ccbb3Scarlsonj  * previously done on it -- otherwise a DROP or RELEASE is first required.
82*d04ccbb3Scarlsonj  * PING and STATUS always make sense and thus are always permitted, as is DROP
83*d04ccbb3Scarlsonj  * in order to permit the administrator to always bail out.
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate static int ipc_cmd_allowed[DHCP_NSTATES][DHCP_NIPC] = {
867c478bd9Sstevel@tonic-gate 	/*			  D  E	P  R  S	 S  I  G */
877c478bd9Sstevel@tonic-gate 	/*			  R  X	I  E  T	 T  N  E */
887c478bd9Sstevel@tonic-gate 	/*			  O  T	N  L  A	 A  F  T */
897c478bd9Sstevel@tonic-gate 	/*			  P  E	G  E  R	 T  O  _ */
907c478bd9Sstevel@tonic-gate 	/*			  .  N  .  A  T  U  R  T */
917c478bd9Sstevel@tonic-gate 	/*			  .  D	.  S  .  S  M  A */
927c478bd9Sstevel@tonic-gate 	/*			  .  .  .  E  .  .  .  G */
937c478bd9Sstevel@tonic-gate 	/* INIT		*/	{ 1, 0, 1, 0, 1, 1, 1, 0 },
947c478bd9Sstevel@tonic-gate 	/* SELECTING	*/	{ 1, 0, 1, 0, 1, 1, 0, 0 },
957c478bd9Sstevel@tonic-gate 	/* REQUESTING	*/	{ 1, 0, 1, 0, 1, 1, 0, 0 },
9669bb4bb4Scarlsonj 	/* PRE_BOUND	*/	{ 1, 1, 1, 1, 0, 1, 0, 1 },
977c478bd9Sstevel@tonic-gate 	/* BOUND	*/	{ 1, 1, 1, 1, 0, 1, 0, 1 },
987c478bd9Sstevel@tonic-gate 	/* RENEWING	*/	{ 1, 1, 1, 1, 0, 1, 0, 1 },
997c478bd9Sstevel@tonic-gate 	/* REBINDING	*/	{ 1, 1, 1, 1, 0, 1, 0, 1 },
100*d04ccbb3Scarlsonj 	/* INFORMATION  */	{ 1, 0, 1, 0, 1, 1, 1, 1 },
101*d04ccbb3Scarlsonj 	/* INIT_REBOOT  */	{ 1, 0, 1, 1, 1, 1, 0, 0 },
102*d04ccbb3Scarlsonj 	/* ADOPTING	*/	{ 1, 0, 1, 1, 0, 1, 0, 0 },
103*d04ccbb3Scarlsonj 	/* INFORM_SENT  */	{ 1, 0, 1, 0, 1, 1, 1, 0 },
104*d04ccbb3Scarlsonj 	/* DECLINING	*/	{ 1, 1, 1, 1, 0, 1, 0, 1 },
105*d04ccbb3Scarlsonj 	/* RELEASING	*/	{ 1, 0, 1, 0, 0, 1, 0, 1 },
106*d04ccbb3Scarlsonj };
107*d04ccbb3Scarlsonj 
108*d04ccbb3Scarlsonj #define	CMD_ISPRIV	0x1	/* Command requires privileges */
109*d04ccbb3Scarlsonj #define	CMD_CREATE	0x2	/* Command creates an interface */
110*d04ccbb3Scarlsonj #define	CMD_BOOTP	0x4	/* Command is valid with BOOTP */
111*d04ccbb3Scarlsonj #define	CMD_IMMED	0x8	/* Reply is immediate (no BUSY state) */
112*d04ccbb3Scarlsonj 
113*d04ccbb3Scarlsonj static uint_t ipc_cmd_flags[DHCP_NIPC] = {
114*d04ccbb3Scarlsonj 	/* DHCP_DROP */		CMD_ISPRIV|CMD_BOOTP,
115*d04ccbb3Scarlsonj 	/* DHCP_EXTEND */	CMD_ISPRIV,
116*d04ccbb3Scarlsonj 	/* DHCP_PING */		CMD_BOOTP|CMD_IMMED,
117*d04ccbb3Scarlsonj 	/* DHCP_RELEASE */	CMD_ISPRIV,
118*d04ccbb3Scarlsonj 	/* DHCP_START */	CMD_CREATE|CMD_ISPRIV|CMD_BOOTP,
119*d04ccbb3Scarlsonj 	/* DHCP_STATUS */	CMD_BOOTP|CMD_IMMED,
120*d04ccbb3Scarlsonj 	/* DHCP_INFORM */	CMD_CREATE|CMD_ISPRIV,
121*d04ccbb3Scarlsonj 	/* DHCP_GET_TAG */	CMD_BOOTP|CMD_IMMED
1227c478bd9Sstevel@tonic-gate };
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate int
1257c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	boolean_t	is_daemon  = B_TRUE;
128*d04ccbb3Scarlsonj 	boolean_t	is_verbose;
1297c478bd9Sstevel@tonic-gate 	int		ipc_fd;
1307c478bd9Sstevel@tonic-gate 	int		c;
1317c478bd9Sstevel@tonic-gate 	struct rlimit	rl;
1327c478bd9Sstevel@tonic-gate 
133*d04ccbb3Scarlsonj 	debug_level = df_get_int("", B_FALSE, DF_DEBUG_LEVEL);
134*d04ccbb3Scarlsonj 	is_verbose = df_get_bool("", B_FALSE, DF_VERBOSE);
135*d04ccbb3Scarlsonj 
1367c478bd9Sstevel@tonic-gate 	/*
1377c478bd9Sstevel@tonic-gate 	 * -l is ignored for compatibility with old agent.
1387c478bd9Sstevel@tonic-gate 	 */
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "vd:l:fa")) != EOF) {
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 		switch (c) {
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 		case 'a':
1457c478bd9Sstevel@tonic-gate 			do_adopt = B_TRUE;
1467c478bd9Sstevel@tonic-gate 			grandparent = getpid();
1477c478bd9Sstevel@tonic-gate 			break;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 		case 'd':
1507c478bd9Sstevel@tonic-gate 			debug_level = strtoul(optarg, NULL, 0);
1517c478bd9Sstevel@tonic-gate 			break;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 		case 'f':
1547c478bd9Sstevel@tonic-gate 			is_daemon = B_FALSE;
1557c478bd9Sstevel@tonic-gate 			break;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 		case 'v':
1587c478bd9Sstevel@tonic-gate 			is_verbose = B_TRUE;
1597c478bd9Sstevel@tonic-gate 			break;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 		case '?':
1627c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "usage: %s [-a] [-d n] [-f] [-v]"
1637c478bd9Sstevel@tonic-gate 			    "\n", argv[0]);
1647c478bd9Sstevel@tonic-gate 			return (EXIT_FAILURE);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 		default:
1677c478bd9Sstevel@tonic-gate 			break;
1687c478bd9Sstevel@tonic-gate 		}
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1727c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	if (geteuid() != 0) {
1757c478bd9Sstevel@tonic-gate 		dhcpmsg_init(argv[0], B_FALSE, is_verbose, debug_level);
1767c478bd9Sstevel@tonic-gate 		dhcpmsg(MSG_ERROR, "must be super-user");
1777c478bd9Sstevel@tonic-gate 		dhcpmsg_fini();
1787c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	if (is_daemon && daemonize() == 0) {
1827c478bd9Sstevel@tonic-gate 		dhcpmsg_init(argv[0], B_FALSE, is_verbose, debug_level);
1837c478bd9Sstevel@tonic-gate 		dhcpmsg(MSG_ERR, "cannot become daemon, exiting");
1847c478bd9Sstevel@tonic-gate 		dhcpmsg_fini();
1857c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 
188*d04ccbb3Scarlsonj 	/*
189*d04ccbb3Scarlsonj 	 * Seed the random number generator, since we're going to need it
190*d04ccbb3Scarlsonj 	 * to set transaction id's and for exponential backoff.
191*d04ccbb3Scarlsonj 	 */
192*d04ccbb3Scarlsonj 	srand48(gethrtime() ^ gethostid() ^ getpid());
193*d04ccbb3Scarlsonj 
1947c478bd9Sstevel@tonic-gate 	dhcpmsg_init(argv[0], is_daemon, is_verbose, debug_level);
1957c478bd9Sstevel@tonic-gate 	(void) atexit(dhcpmsg_fini);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	tq = iu_tq_create();
1987c478bd9Sstevel@tonic-gate 	eh = iu_eh_create();
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	if (eh == NULL || tq == NULL) {
2017c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
2027c478bd9Sstevel@tonic-gate 		dhcpmsg(MSG_ERR, "cannot create timer queue or event handler");
2037c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	/*
2077c478bd9Sstevel@tonic-gate 	 * ignore most signals that could be reasonably generated.
2087c478bd9Sstevel@tonic-gate 	 */
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	(void) signal(SIGTERM, graceful_shutdown);
2117c478bd9Sstevel@tonic-gate 	(void) signal(SIGQUIT, graceful_shutdown);
2127c478bd9Sstevel@tonic-gate 	(void) signal(SIGPIPE, SIG_IGN);
2137c478bd9Sstevel@tonic-gate 	(void) signal(SIGUSR1, SIG_IGN);
2147c478bd9Sstevel@tonic-gate 	(void) signal(SIGUSR2, SIG_IGN);
2157c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT,  SIG_IGN);
2167c478bd9Sstevel@tonic-gate 	(void) signal(SIGHUP,  SIG_IGN);
2177c478bd9Sstevel@tonic-gate 	(void) signal(SIGCHLD, SIG_IGN);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	/*
2207c478bd9Sstevel@tonic-gate 	 * upon SIGTHAW we need to refresh any non-infinite leases.
2217c478bd9Sstevel@tonic-gate 	 */
2227c478bd9Sstevel@tonic-gate 
223*d04ccbb3Scarlsonj 	(void) iu_eh_register_signal(eh, SIGTHAW, refresh_smachs, NULL);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	class_id = get_class_id();
2267c478bd9Sstevel@tonic-gate 	if (class_id != NULL)
2277c478bd9Sstevel@tonic-gate 		class_id_len = strlen(class_id);
2287c478bd9Sstevel@tonic-gate 	else
2297c478bd9Sstevel@tonic-gate 		dhcpmsg(MSG_WARNING, "get_class_id failed, continuing "
2307c478bd9Sstevel@tonic-gate 		    "with no vendor class id");
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	/*
2337c478bd9Sstevel@tonic-gate 	 * the inactivity timer is enabled any time there are no
2347c478bd9Sstevel@tonic-gate 	 * interfaces under DHCP control.  if DHCP_INACTIVITY_WAIT
2357c478bd9Sstevel@tonic-gate 	 * seconds transpire without an interface under DHCP control,
2367c478bd9Sstevel@tonic-gate 	 * the agent shuts down.
2377c478bd9Sstevel@tonic-gate 	 */
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	inactivity_id = iu_schedule_timer(tq, DHCP_INACTIVITY_WAIT,
2407c478bd9Sstevel@tonic-gate 	    inactivity_shutdown, NULL);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	/*
2437c478bd9Sstevel@tonic-gate 	 * max out the number available descriptors, just in case..
2447c478bd9Sstevel@tonic-gate 	 */
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	rl.rlim_cur = RLIM_INFINITY;
2477c478bd9Sstevel@tonic-gate 	rl.rlim_max = RLIM_INFINITY;
2487c478bd9Sstevel@tonic-gate 	if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
2497c478bd9Sstevel@tonic-gate 		dhcpmsg(MSG_ERR, "setrlimit failed");
2507c478bd9Sstevel@tonic-gate 
251004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
252004388ebScasper 
253*d04ccbb3Scarlsonj 	/*
254*d04ccbb3Scarlsonj 	 * Create and bind default IP sockets used to control interfaces and to
255*d04ccbb3Scarlsonj 	 * catch stray packets.
256*d04ccbb3Scarlsonj 	 */
257*d04ccbb3Scarlsonj 
258*d04ccbb3Scarlsonj 	if (!dhcp_ip_default())
259*d04ccbb3Scarlsonj 		return (EXIT_FAILURE);
260*d04ccbb3Scarlsonj 
2617c478bd9Sstevel@tonic-gate 	/*
2627c478bd9Sstevel@tonic-gate 	 * create the ipc channel that the agent will listen for
2637c478bd9Sstevel@tonic-gate 	 * requests on, and register it with the event handler so that
2647c478bd9Sstevel@tonic-gate 	 * `accept_event' will be called back.
2657c478bd9Sstevel@tonic-gate 	 */
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	switch (dhcp_ipc_init(&ipc_fd)) {
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	case 0:
2707c478bd9Sstevel@tonic-gate 		break;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	case DHCP_IPC_E_BIND:
2737c478bd9Sstevel@tonic-gate 		dhcpmsg(MSG_ERROR, "dhcp_ipc_init: cannot bind to port "
2747c478bd9Sstevel@tonic-gate 		    "%i (agent already running?)", IPPORT_DHCPAGENT);
2757c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	default:
2787c478bd9Sstevel@tonic-gate 		dhcpmsg(MSG_ERROR, "dhcp_ipc_init failed");
2797c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	if (iu_register_event(eh, ipc_fd, POLLIN, accept_event, 0) == -1) {
2837c478bd9Sstevel@tonic-gate 		dhcpmsg(MSG_ERR, "cannot register ipc fd for messages");
2847c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 
28769bb4bb4Scarlsonj 	/*
28869bb4bb4Scarlsonj 	 * Create the global routing socket.  This is used for monitoring
28969bb4bb4Scarlsonj 	 * interface transitions, so that we learn about the kernel's Duplicate
29069bb4bb4Scarlsonj 	 * Address Detection status, and for inserting and removing default
291*d04ccbb3Scarlsonj 	 * routes as learned from DHCP servers.  Both v4 and v6 are handed
292*d04ccbb3Scarlsonj 	 * with this one socket.
29369bb4bb4Scarlsonj 	 */
294*d04ccbb3Scarlsonj 	rtsock_fd = socket(PF_ROUTE, SOCK_RAW, 0);
29569bb4bb4Scarlsonj 	if (rtsock_fd == -1) {
29669bb4bb4Scarlsonj 		dhcpmsg(MSG_ERR, "cannot open routing socket");
29769bb4bb4Scarlsonj 		return (EXIT_FAILURE);
29869bb4bb4Scarlsonj 	}
29969bb4bb4Scarlsonj 	if (iu_register_event(eh, rtsock_fd, POLLIN, rtsock_event, 0) == -1) {
30069bb4bb4Scarlsonj 		dhcpmsg(MSG_ERR, "cannot register routing socket for messages");
30169bb4bb4Scarlsonj 		return (EXIT_FAILURE);
30269bb4bb4Scarlsonj 	}
30369bb4bb4Scarlsonj 
3047c478bd9Sstevel@tonic-gate 	/*
3057c478bd9Sstevel@tonic-gate 	 * if the -a (adopt) option was specified, try to adopt the
306dc041e83Scarlsonj 	 * kernel-managed interface before we start.
3077c478bd9Sstevel@tonic-gate 	 */
3087c478bd9Sstevel@tonic-gate 
309*d04ccbb3Scarlsonj 	if (do_adopt && !dhcp_adopt())
310dc041e83Scarlsonj 		return (EXIT_FAILURE);
3117c478bd9Sstevel@tonic-gate 
312*d04ccbb3Scarlsonj 	/*
313*d04ccbb3Scarlsonj 	 * For DHCPv6, we own all of the interfaces marked DHCPRUNNING.  As
314*d04ccbb3Scarlsonj 	 * we're starting operation here, if there are any of those interfaces
315*d04ccbb3Scarlsonj 	 * lingering around, they're strays, and need to be removed.
316*d04ccbb3Scarlsonj 	 *
317*d04ccbb3Scarlsonj 	 * It might be nice to save these addresses off somewhere -- for both
318*d04ccbb3Scarlsonj 	 * v4 and v6 -- and use them as hints for later negotiation.
319*d04ccbb3Scarlsonj 	 */
320*d04ccbb3Scarlsonj 	remove_v6_strays();
321*d04ccbb3Scarlsonj 
3227c478bd9Sstevel@tonic-gate 	/*
3237c478bd9Sstevel@tonic-gate 	 * enter the main event loop; this is where all the real work
3247c478bd9Sstevel@tonic-gate 	 * takes place (through registering events and scheduling timers).
3257c478bd9Sstevel@tonic-gate 	 * this function only returns when the agent is shutting down.
3267c478bd9Sstevel@tonic-gate 	 */
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	switch (iu_handle_events(eh, tq)) {
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	case -1:
3317c478bd9Sstevel@tonic-gate 		dhcpmsg(MSG_WARNING, "iu_handle_events exited abnormally");
3327c478bd9Sstevel@tonic-gate 		break;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	case DHCP_REASON_INACTIVITY:
3357c478bd9Sstevel@tonic-gate 		dhcpmsg(MSG_INFO, "no interfaces to manage, shutting down...");
3367c478bd9Sstevel@tonic-gate 		break;
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	case DHCP_REASON_TERMINATE:
3397c478bd9Sstevel@tonic-gate 		dhcpmsg(MSG_INFO, "received SIGTERM, shutting down...");
3407c478bd9Sstevel@tonic-gate 		break;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	case DHCP_REASON_SIGNAL:
3437c478bd9Sstevel@tonic-gate 		dhcpmsg(MSG_WARNING, "received unexpected signal, shutting "
3447c478bd9Sstevel@tonic-gate 		    "down...");
3457c478bd9Sstevel@tonic-gate 		break;
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	(void) iu_eh_unregister_signal(eh, SIGTHAW, NULL);
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	iu_eh_destroy(eh);
3517c478bd9Sstevel@tonic-gate 	iu_tq_destroy(tq);
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	return (EXIT_SUCCESS);
3547c478bd9Sstevel@tonic-gate }
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate /*
3577c478bd9Sstevel@tonic-gate  * drain_script(): event loop callback during shutdown
3587c478bd9Sstevel@tonic-gate  *
3597c478bd9Sstevel@tonic-gate  *   input: eh_t *: unused
3607c478bd9Sstevel@tonic-gate  *	    void *: unused
3617c478bd9Sstevel@tonic-gate  *  output: boolean_t: B_TRUE if event loop should exit; B_FALSE otherwise
3627c478bd9Sstevel@tonic-gate  */
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate /* ARGSUSED */
3657c478bd9Sstevel@tonic-gate boolean_t
3667c478bd9Sstevel@tonic-gate drain_script(iu_eh_t *ehp, void *arg)
3677c478bd9Sstevel@tonic-gate {
3687c478bd9Sstevel@tonic-gate 	if (shutdown_started == B_FALSE) {
3697c478bd9Sstevel@tonic-gate 		shutdown_started = B_TRUE;
3707c478bd9Sstevel@tonic-gate 		if (do_adopt == B_FALSE)	/* see 4291141 */
371*d04ccbb3Scarlsonj 			nuke_smach_list();
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 	return (script_count == 0);
3747c478bd9Sstevel@tonic-gate }
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate /*
3777c478bd9Sstevel@tonic-gate  * accept_event(): accepts a new connection on the ipc socket and registers
3787c478bd9Sstevel@tonic-gate  *		   to receive its messages with the event handler
3797c478bd9Sstevel@tonic-gate  *
3807c478bd9Sstevel@tonic-gate  *   input: iu_eh_t *: unused
3817c478bd9Sstevel@tonic-gate  *	    int: the file descriptor in the iu_eh_t * the connection came in on
3827c478bd9Sstevel@tonic-gate  *	    (other arguments unused)
3837c478bd9Sstevel@tonic-gate  *  output: void
3847c478bd9Sstevel@tonic-gate  */
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate /* ARGSUSED */
3877c478bd9Sstevel@tonic-gate static void
3887c478bd9Sstevel@tonic-gate accept_event(iu_eh_t *ehp, int fd, short events, iu_event_id_t id, void *arg)
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	int	client_fd;
3917c478bd9Sstevel@tonic-gate 	int	is_priv;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	if (dhcp_ipc_accept(fd, &client_fd, &is_priv) != 0) {
3947c478bd9Sstevel@tonic-gate 		dhcpmsg(MSG_ERR, "accept_event: accept on ipc socket");
3957c478bd9Sstevel@tonic-gate 		return;
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	if (iu_register_event(eh, client_fd, POLLIN, ipc_event,
3997c478bd9Sstevel@tonic-gate 	    (void *)is_priv) == -1) {
4007c478bd9Sstevel@tonic-gate 		dhcpmsg(MSG_ERROR, "accept_event: cannot register ipc socket "
4017c478bd9Sstevel@tonic-gate 		    "for callback");
4027c478bd9Sstevel@tonic-gate 	}
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate /*
4067c478bd9Sstevel@tonic-gate  * ipc_event(): processes incoming ipc requests
4077c478bd9Sstevel@tonic-gate  *
4087c478bd9Sstevel@tonic-gate  *   input: iu_eh_t *: unused
4097c478bd9Sstevel@tonic-gate  *	    int: the file descriptor in the iu_eh_t * the request came in on
4107c478bd9Sstevel@tonic-gate  *	    short: unused
411*d04ccbb3Scarlsonj  *	    iu_event_id_t: event ID
4127c478bd9Sstevel@tonic-gate  *	    void *: indicates whether the request is from a privileged client
4137c478bd9Sstevel@tonic-gate  *  output: void
4147c478bd9Sstevel@tonic-gate  */
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate /* ARGSUSED */
4177c478bd9Sstevel@tonic-gate static void
4187c478bd9Sstevel@tonic-gate ipc_event(iu_eh_t *ehp, int fd, short events, iu_event_id_t id, void *arg)
4197c478bd9Sstevel@tonic-gate {
420*d04ccbb3Scarlsonj 	ipc_action_t		ia, *iap;
421*d04ccbb3Scarlsonj 	dhcp_smach_t		*dsmp;
4227c478bd9Sstevel@tonic-gate 	int			error, is_priv = (int)arg;
423*d04ccbb3Scarlsonj 	const char		*ifname;
424*d04ccbb3Scarlsonj 	boolean_t		isv6;
425*d04ccbb3Scarlsonj 
426*d04ccbb3Scarlsonj 	ipc_action_init(&ia);
427*d04ccbb3Scarlsonj 	error = dhcp_ipc_recv_request(fd, &ia.ia_request,
428*d04ccbb3Scarlsonj 	    DHCP_IPC_REQUEST_WAIT);
429*d04ccbb3Scarlsonj 	if (error != DHCP_IPC_SUCCESS) {
430*d04ccbb3Scarlsonj 		if (error != DHCP_IPC_E_EOF) {
431*d04ccbb3Scarlsonj 			dhcpmsg(MSG_ERROR,
432*d04ccbb3Scarlsonj 			    "ipc_event: dhcp_ipc_recv_request failed: %s",
433*d04ccbb3Scarlsonj 			    dhcp_ipc_strerror(error));
434*d04ccbb3Scarlsonj 		} else {
435*d04ccbb3Scarlsonj 			dhcpmsg(MSG_DEBUG, "ipc_event: connection closed");
436*d04ccbb3Scarlsonj 		}
437*d04ccbb3Scarlsonj 		if ((dsmp = lookup_smach_by_event(id)) != NULL) {
438*d04ccbb3Scarlsonj 			ipc_action_finish(dsmp, error);
439*d04ccbb3Scarlsonj 		} else {
440*d04ccbb3Scarlsonj 			(void) iu_unregister_event(eh, id, NULL);
441*d04ccbb3Scarlsonj 			(void) dhcp_ipc_close(fd);
442*d04ccbb3Scarlsonj 		}
4437c478bd9Sstevel@tonic-gate 		return;
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 
446*d04ccbb3Scarlsonj 	/* Fill in temporary ipc_action structure for utility functions */
447*d04ccbb3Scarlsonj 	ia.ia_cmd = DHCP_IPC_CMD(ia.ia_request->message_type);
448*d04ccbb3Scarlsonj 	ia.ia_fd = fd;
449*d04ccbb3Scarlsonj 	ia.ia_eid = id;
450*d04ccbb3Scarlsonj 
451*d04ccbb3Scarlsonj 	if (ia.ia_cmd >= DHCP_NIPC) {
452*d04ccbb3Scarlsonj 		dhcpmsg(MSG_ERROR,
453*d04ccbb3Scarlsonj 		    "ipc_event: invalid command (%s) attempted on %s",
454*d04ccbb3Scarlsonj 		    dhcp_ipc_type_to_string(ia.ia_cmd), ia.ia_request->ifname);
455*d04ccbb3Scarlsonj 		send_error_reply(&ia, DHCP_IPC_E_CMD_UNKNOWN);
4567c478bd9Sstevel@tonic-gate 		return;
4577c478bd9Sstevel@tonic-gate 	}
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	/* return EPERM for any of the privileged actions */
4607c478bd9Sstevel@tonic-gate 
461*d04ccbb3Scarlsonj 	if (!is_priv && (ipc_cmd_flags[ia.ia_cmd] & CMD_ISPRIV)) {
462*d04ccbb3Scarlsonj 		dhcpmsg(MSG_WARNING,
463*d04ccbb3Scarlsonj 		    "ipc_event: privileged ipc command (%s) attempted on %s",
464*d04ccbb3Scarlsonj 		    dhcp_ipc_type_to_string(ia.ia_cmd), ia.ia_request->ifname);
465*d04ccbb3Scarlsonj 		send_error_reply(&ia, DHCP_IPC_E_PERM);
466*d04ccbb3Scarlsonj 		return;
4677c478bd9Sstevel@tonic-gate 	}
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	/*
470*d04ccbb3Scarlsonj 	 * Try to locate the state machine associated with this command.  If
471*d04ccbb3Scarlsonj 	 * the command is DHCP_START or DHCP_INFORM and there isn't a state
472*d04ccbb3Scarlsonj 	 * machine already, make one (there may already be one from a previous
473*d04ccbb3Scarlsonj 	 * failed attempt to START or INFORM).  Otherwise, verify the reference
474*d04ccbb3Scarlsonj 	 * is still valid.
475*d04ccbb3Scarlsonj 	 *
476*d04ccbb3Scarlsonj 	 * The interface name may be blank.  In that case, we look up the
477*d04ccbb3Scarlsonj 	 * primary interface, and the requested type (v4 or v6) doesn't matter.
4787c478bd9Sstevel@tonic-gate 	 */
4797c478bd9Sstevel@tonic-gate 
480*d04ccbb3Scarlsonj 	isv6 = (ia.ia_request->message_type & DHCP_V6) != 0;
481*d04ccbb3Scarlsonj 	ifname = ia.ia_request->ifname;
482*d04ccbb3Scarlsonj 	if (*ifname == '\0')
483*d04ccbb3Scarlsonj 		dsmp = primary_smach(isv6);
484*d04ccbb3Scarlsonj 	else
485*d04ccbb3Scarlsonj 		dsmp = lookup_smach(ifname, isv6);
4867c478bd9Sstevel@tonic-gate 
487*d04ccbb3Scarlsonj 	if (dsmp != NULL) {
488*d04ccbb3Scarlsonj 		/* Note that verify_smach drops a reference */
489*d04ccbb3Scarlsonj 		hold_smach(dsmp);
490*d04ccbb3Scarlsonj 		if (!verify_smach(dsmp))
491*d04ccbb3Scarlsonj 			dsmp = NULL;
492*d04ccbb3Scarlsonj 	}
4937c478bd9Sstevel@tonic-gate 
494*d04ccbb3Scarlsonj 	if (dsmp == NULL) {
4957c478bd9Sstevel@tonic-gate 		/*
496*d04ccbb3Scarlsonj 		 * If the user asked for the primary DHCP interface, but there
497*d04ccbb3Scarlsonj 		 * is none, then report failure.
4987c478bd9Sstevel@tonic-gate 		 */
499*d04ccbb3Scarlsonj 		if (ifname[0] == '\0') {
500*d04ccbb3Scarlsonj 			error = DHCP_IPC_E_NOPRIMARY;
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 		/*
503*d04ccbb3Scarlsonj 		 * If there's no interface, and we're starting up, then create
504*d04ccbb3Scarlsonj 		 * it now, along with a state machine for it.  Note that if
505*d04ccbb3Scarlsonj 		 * insert_smach fails, it discards the LIF reference.
5067c478bd9Sstevel@tonic-gate 		 */
507*d04ccbb3Scarlsonj 		} else if (ipc_cmd_flags[ia.ia_cmd] & CMD_CREATE) {
508*d04ccbb3Scarlsonj 			dhcp_lif_t *lif;
5097c478bd9Sstevel@tonic-gate 
510*d04ccbb3Scarlsonj 			lif = attach_lif(ifname, isv6, &error);
511*d04ccbb3Scarlsonj 			if (lif != NULL &&
512*d04ccbb3Scarlsonj 			    (dsmp = insert_smach(lif, &error)) != NULL) {
513*d04ccbb3Scarlsonj 				/*
514*d04ccbb3Scarlsonj 				 * Get client ID and set "DHCPRUNNING" flag on
515*d04ccbb3Scarlsonj 				 * logical interface.  (V4 only, because V6
516*d04ccbb3Scarlsonj 				 * plumbs its own interfaces.)
517*d04ccbb3Scarlsonj 				 */
518*d04ccbb3Scarlsonj 				error = get_smach_cid(dsmp);
519*d04ccbb3Scarlsonj 				if (error == DHCP_IPC_SUCCESS)
520*d04ccbb3Scarlsonj 					error = set_lif_dhcp(lif, B_FALSE);
521*d04ccbb3Scarlsonj 				if (error != DHCP_IPC_SUCCESS) {
522*d04ccbb3Scarlsonj 					release_smach(dsmp);
523*d04ccbb3Scarlsonj 					dsmp = NULL;
524*d04ccbb3Scarlsonj 				}
5257c478bd9Sstevel@tonic-gate 			}
5267c478bd9Sstevel@tonic-gate 
527*d04ccbb3Scarlsonj 		/*
528*d04ccbb3Scarlsonj 		 * Otherwise, this is an operation on an unknown interface.
529*d04ccbb3Scarlsonj 		 */
530*d04ccbb3Scarlsonj 		} else {
531*d04ccbb3Scarlsonj 			error = DHCP_IPC_E_UNKIF;
532*d04ccbb3Scarlsonj 		}
533*d04ccbb3Scarlsonj 		if (dsmp == NULL) {
534*d04ccbb3Scarlsonj 			send_error_reply(&ia, error);
5357c478bd9Sstevel@tonic-gate 			return;
5367c478bd9Sstevel@tonic-gate 		}
5377c478bd9Sstevel@tonic-gate 	}
5387c478bd9Sstevel@tonic-gate 
539*d04ccbb3Scarlsonj 	if ((dsmp->dsm_dflags & DHCP_IF_BOOTP) &&
540*d04ccbb3Scarlsonj 	    !(ipc_cmd_flags[ia.ia_cmd] & CMD_BOOTP)) {
541*d04ccbb3Scarlsonj 		dhcpmsg(MSG_ERROR, "command %s not valid for BOOTP on %s",
542*d04ccbb3Scarlsonj 		    dhcp_ipc_type_to_string(ia.ia_cmd), dsmp->dsm_name);
543*d04ccbb3Scarlsonj 		send_error_reply(&ia, DHCP_IPC_E_BOOTP);
5447c478bd9Sstevel@tonic-gate 		return;
5457c478bd9Sstevel@tonic-gate 	}
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	/*
548*d04ccbb3Scarlsonj 	 * verify that the state machine is in a state which will allow the
5497c478bd9Sstevel@tonic-gate 	 * command.  we do this up front so that we can return an error
5507c478bd9Sstevel@tonic-gate 	 * *before* needlessly cancelling an in-progress transaction.
5517c478bd9Sstevel@tonic-gate 	 */
5527c478bd9Sstevel@tonic-gate 
553*d04ccbb3Scarlsonj 	if (!check_cmd_allowed(dsmp->dsm_state, ia.ia_cmd)) {
554*d04ccbb3Scarlsonj 		dhcpmsg(MSG_DEBUG,
555*d04ccbb3Scarlsonj 		    "in state %s; not allowing %s command on %s",
556*d04ccbb3Scarlsonj 		    dhcp_state_to_string(dsmp->dsm_state),
557*d04ccbb3Scarlsonj 		    dhcp_ipc_type_to_string(ia.ia_cmd), dsmp->dsm_name);
558*d04ccbb3Scarlsonj 		send_error_reply(&ia,
559*d04ccbb3Scarlsonj 		    ia.ia_cmd == DHCP_START && dsmp->dsm_state != INIT ?
560*d04ccbb3Scarlsonj 		    DHCP_IPC_E_RUNNING : DHCP_IPC_E_OUTSTATE);
5617c478bd9Sstevel@tonic-gate 		return;
5627c478bd9Sstevel@tonic-gate 	}
5637c478bd9Sstevel@tonic-gate 
564*d04ccbb3Scarlsonj 	dhcpmsg(MSG_DEBUG, "in state %s; allowing %s command on %s",
565*d04ccbb3Scarlsonj 	    dhcp_state_to_string(dsmp->dsm_state),
566*d04ccbb3Scarlsonj 	    dhcp_ipc_type_to_string(ia.ia_cmd), dsmp->dsm_name);
567*d04ccbb3Scarlsonj 
568*d04ccbb3Scarlsonj 	if ((ia.ia_request->message_type & DHCP_PRIMARY) && is_priv)
569*d04ccbb3Scarlsonj 		make_primary(dsmp);
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	/*
572*d04ccbb3Scarlsonj 	 * The current design dictates that there can be only one outstanding
573*d04ccbb3Scarlsonj 	 * transaction per state machine -- this simplifies the code
574*d04ccbb3Scarlsonj 	 * considerably and also fits well with RFCs 2131 and 3315.  It is
575*d04ccbb3Scarlsonj 	 * worth classifying the different DHCP commands into synchronous
576*d04ccbb3Scarlsonj 	 * (those which we will handle now and reply to immediately) and
577*d04ccbb3Scarlsonj 	 * asynchronous (those which require transactions and will be completed
578*d04ccbb3Scarlsonj 	 * at an indeterminate time in the future):
5797c478bd9Sstevel@tonic-gate 	 *
580*d04ccbb3Scarlsonj 	 *    DROP: removes the agent's management of a state machine.
5817c478bd9Sstevel@tonic-gate 	 *	    asynchronous as the script program may be invoked.
5827c478bd9Sstevel@tonic-gate 	 *
583*d04ccbb3Scarlsonj 	 *    PING: checks to see if the agent has a named state machine.
5847c478bd9Sstevel@tonic-gate 	 *	    synchronous, since no packets need to be sent
5857c478bd9Sstevel@tonic-gate 	 *	    to the DHCP server.
5867c478bd9Sstevel@tonic-gate 	 *
587*d04ccbb3Scarlsonj 	 *  STATUS: returns information about a state machine.
5887c478bd9Sstevel@tonic-gate 	 *	    synchronous, since no packets need to be sent
5897c478bd9Sstevel@tonic-gate 	 *	    to the DHCP server.
5907c478bd9Sstevel@tonic-gate 	 *
591*d04ccbb3Scarlsonj 	 * RELEASE: releases the agent's management of a state machine
592*d04ccbb3Scarlsonj 	 *	    and brings the associated interfaces down.  asynchronous
593*d04ccbb3Scarlsonj 	 *	    as the script program may be invoked.
5947c478bd9Sstevel@tonic-gate 	 *
5957c478bd9Sstevel@tonic-gate 	 *  EXTEND: renews a lease.  asynchronous, since the agent
5967c478bd9Sstevel@tonic-gate 	 *	    needs to wait for an ACK, etc.
5977c478bd9Sstevel@tonic-gate 	 *
598*d04ccbb3Scarlsonj 	 *   START: starts DHCP on a named state machine.  asynchronous since
5997c478bd9Sstevel@tonic-gate 	 *	    the agent needs to wait for OFFERs, ACKs, etc.
6007c478bd9Sstevel@tonic-gate 	 *
601*d04ccbb3Scarlsonj 	 *  INFORM: obtains configuration parameters for the system using
602*d04ccbb3Scarlsonj 	 *	    externally configured interface.  asynchronous, since the
6037c478bd9Sstevel@tonic-gate 	 *	    agent needs to wait for an ACK.
6047c478bd9Sstevel@tonic-gate 	 *
605*d04ccbb3Scarlsonj 	 * Notice that EXTEND, INFORM, START, DROP and RELEASE are
606*d04ccbb3Scarlsonj 	 * asynchronous.  Notice also that asynchronous commands may occur from
607*d04ccbb3Scarlsonj 	 * within the agent -- for instance, the agent will need to do implicit
608*d04ccbb3Scarlsonj 	 * EXTENDs to extend the lease. In order to make the code simpler, the
609*d04ccbb3Scarlsonj 	 * following rules apply for asynchronous commands:
6107c478bd9Sstevel@tonic-gate 	 *
611*d04ccbb3Scarlsonj 	 * There can only be one asynchronous command at a time per state
612*d04ccbb3Scarlsonj 	 * machine.  The current asynchronous command is managed by the async_*
613*d04ccbb3Scarlsonj 	 * api: async_start(), async_finish(), and async_cancel().
614*d04ccbb3Scarlsonj 	 * async_start() starts management of a new asynchronous command on an
615*d04ccbb3Scarlsonj 	 * state machine, which should only be done after async_cancel() to
616*d04ccbb3Scarlsonj 	 * terminate a previous command.  When the command is completed,
617*d04ccbb3Scarlsonj 	 * async_finish() should be called.
6187c478bd9Sstevel@tonic-gate 	 *
619*d04ccbb3Scarlsonj 	 * Asynchronous commands started by a user command have an associated
620*d04ccbb3Scarlsonj 	 * ipc_action which provides the agent with information for how to get
621*d04ccbb3Scarlsonj 	 * in touch with the user command when the action completes.  These
622*d04ccbb3Scarlsonj 	 * ipc_action records also have an associated timeout which may be
623*d04ccbb3Scarlsonj 	 * infinite.  ipc_action_start() should be called when starting an
624*d04ccbb3Scarlsonj 	 * asynchronous command requested by a user, which sets up the timer
625*d04ccbb3Scarlsonj 	 * and keeps track of the ipc information (file descriptor, request
626*d04ccbb3Scarlsonj 	 * type).  When the asynchronous command completes, ipc_action_finish()
627*d04ccbb3Scarlsonj 	 * should be called to return a command status code to the user and
628*d04ccbb3Scarlsonj 	 * close the ipc connection).  If the command does not complete before
629*d04ccbb3Scarlsonj 	 * the timer fires, ipc_action_timeout() is called which closes the ipc
630*d04ccbb3Scarlsonj 	 * connection and returns DHCP_IPC_E_TIMEOUT to the user.  Note that
631*d04ccbb3Scarlsonj 	 * independent of ipc_action_timeout(), ipc_action_finish() should be
632*d04ccbb3Scarlsonj 	 * called.
6337c478bd9Sstevel@tonic-gate 	 *
634*d04ccbb3Scarlsonj 	 * on a case-by-case basis, here is what happens (per state machine):
6357c478bd9Sstevel@tonic-gate 	 *
636*d04ccbb3Scarlsonj 	 *    o When an asynchronous command is requested, then
637*d04ccbb3Scarlsonj 	 *	async_cancel() is called to terminate any non-user
638*d04ccbb3Scarlsonj 	 *	action in progress.  If there's a user action running,
6397c478bd9Sstevel@tonic-gate 	 *	the user command is sent DHCP_IPC_E_PEND.
6407c478bd9Sstevel@tonic-gate 	 *
6417c478bd9Sstevel@tonic-gate 	 *    o otherwise, the the transaction is started with
6427c478bd9Sstevel@tonic-gate 	 *	async_start().  if the transaction is on behalf
6437c478bd9Sstevel@tonic-gate 	 *	of a user, ipc_action_start() is called to keep
6447c478bd9Sstevel@tonic-gate 	 *	track of the ipc information and set up the
6457c478bd9Sstevel@tonic-gate 	 *	ipc_action timer.
6467c478bd9Sstevel@tonic-gate 	 *
6477c478bd9Sstevel@tonic-gate 	 *    o if the command completes normally and before a
6487c478bd9Sstevel@tonic-gate 	 *	timeout fires, then async_finish() is called.
6497c478bd9Sstevel@tonic-gate 	 *	if there was an associated ipc_action,
6507c478bd9Sstevel@tonic-gate 	 *	ipc_action_finish() is called to complete it.
6517c478bd9Sstevel@tonic-gate 	 *
6527c478bd9Sstevel@tonic-gate 	 *    o if the command fails before a timeout fires, then
653*d04ccbb3Scarlsonj 	 *	async_finish() is called, and the state machine is
6547c478bd9Sstevel@tonic-gate 	 *	is returned to a known state based on the command.
6557c478bd9Sstevel@tonic-gate 	 *	if there was an associated ipc_action,
6567c478bd9Sstevel@tonic-gate 	 *	ipc_action_finish() is called to complete it.
6577c478bd9Sstevel@tonic-gate 	 *
6587c478bd9Sstevel@tonic-gate 	 *    o if the ipc_action timer fires before command
6597c478bd9Sstevel@tonic-gate 	 *	completion, then DHCP_IPC_E_TIMEOUT is returned to
6607c478bd9Sstevel@tonic-gate 	 *	the user.  however, the transaction continues to
6617c478bd9Sstevel@tonic-gate 	 *	be carried out asynchronously.
6627c478bd9Sstevel@tonic-gate 	 */
6637c478bd9Sstevel@tonic-gate 
664*d04ccbb3Scarlsonj 	if (ipc_cmd_flags[ia.ia_cmd] & CMD_IMMED) {
665*d04ccbb3Scarlsonj 		/*
666*d04ccbb3Scarlsonj 		 * Only immediate commands (ping, status, get_tag) need to
667*d04ccbb3Scarlsonj 		 * worry about freeing ia through one of the reply functions
668*d04ccbb3Scarlsonj 		 * before returning.
669*d04ccbb3Scarlsonj 		 */
670*d04ccbb3Scarlsonj 		iap = &ia;
671*d04ccbb3Scarlsonj 	} else {
6727c478bd9Sstevel@tonic-gate 		/*
6737c478bd9Sstevel@tonic-gate 		 * if shutdown request has been received, send back an error.
6747c478bd9Sstevel@tonic-gate 		 */
6757c478bd9Sstevel@tonic-gate 		if (shutdown_started) {
676*d04ccbb3Scarlsonj 			send_error_reply(&ia, DHCP_IPC_E_OUTSTATE);
6777c478bd9Sstevel@tonic-gate 			return;
6787c478bd9Sstevel@tonic-gate 		}
6797c478bd9Sstevel@tonic-gate 
680*d04ccbb3Scarlsonj 		if (dsmp->dsm_dflags & DHCP_IF_BUSY) {
681*d04ccbb3Scarlsonj 			send_error_reply(&ia, DHCP_IPC_E_PEND);
6827c478bd9Sstevel@tonic-gate 			return;
6837c478bd9Sstevel@tonic-gate 		}
6847c478bd9Sstevel@tonic-gate 
685*d04ccbb3Scarlsonj 		if (!ipc_action_start(dsmp, &ia)) {
6867c478bd9Sstevel@tonic-gate 			dhcpmsg(MSG_WARNING, "ipc_event: ipc_action_start "
687*d04ccbb3Scarlsonj 			    "failed for %s", dsmp->dsm_name);
688*d04ccbb3Scarlsonj 			send_error_reply(&ia, DHCP_IPC_E_MEMORY);
6897c478bd9Sstevel@tonic-gate 			return;
6907c478bd9Sstevel@tonic-gate 		}
6917c478bd9Sstevel@tonic-gate 
692*d04ccbb3Scarlsonj 		/* Action structure consumed by above function */
693*d04ccbb3Scarlsonj 		iap = &dsmp->dsm_ia;
6947c478bd9Sstevel@tonic-gate 	}
6957c478bd9Sstevel@tonic-gate 
696*d04ccbb3Scarlsonj 	switch (iap->ia_cmd) {
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	case DHCP_DROP:
699*d04ccbb3Scarlsonj 		(void) script_start(dsmp, isv6 ? EVENT_DROP6 : EVENT_DROP,
700*d04ccbb3Scarlsonj 		    dhcp_drop, NULL, NULL);
701*d04ccbb3Scarlsonj 		break;		/* not an immediate function */
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	case DHCP_EXTEND:
704*d04ccbb3Scarlsonj 		(void) dhcp_extending(dsmp);
7057c478bd9Sstevel@tonic-gate 		break;
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	case DHCP_GET_TAG: {
7087c478bd9Sstevel@tonic-gate 		dhcp_optnum_t	optnum;
709*d04ccbb3Scarlsonj 		void		*opt = NULL;
710*d04ccbb3Scarlsonj 		uint_t		optlen;
7117c478bd9Sstevel@tonic-gate 		boolean_t	did_alloc = B_FALSE;
712*d04ccbb3Scarlsonj 		PKT_LIST	*ack = dsmp->dsm_ack;
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 		/*
7157c478bd9Sstevel@tonic-gate 		 * verify the request makes sense.
7167c478bd9Sstevel@tonic-gate 		 */
7177c478bd9Sstevel@tonic-gate 
718*d04ccbb3Scarlsonj 		if (iap->ia_request->data_type   != DHCP_TYPE_OPTNUM ||
719*d04ccbb3Scarlsonj 		    iap->ia_request->data_length != sizeof (dhcp_optnum_t)) {
720*d04ccbb3Scarlsonj 			send_error_reply(iap, DHCP_IPC_E_PROTO);
721*d04ccbb3Scarlsonj 			break;
7227c478bd9Sstevel@tonic-gate 		}
7237c478bd9Sstevel@tonic-gate 
724*d04ccbb3Scarlsonj 		(void) memcpy(&optnum, iap->ia_request->buffer,
725*d04ccbb3Scarlsonj 		    sizeof (dhcp_optnum_t));
726*d04ccbb3Scarlsonj 
7277c478bd9Sstevel@tonic-gate load_option:
7287c478bd9Sstevel@tonic-gate 		switch (optnum.category) {
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 		case DSYM_SITE:			/* FALLTHRU */
7317c478bd9Sstevel@tonic-gate 		case DSYM_STANDARD:
732*d04ccbb3Scarlsonj 			if (isv6) {
733*d04ccbb3Scarlsonj 				opt = dhcpv6_pkt_option(ack, NULL, optnum.code,
734*d04ccbb3Scarlsonj 				    NULL);
735*d04ccbb3Scarlsonj 			} else {
736*d04ccbb3Scarlsonj 				if (optnum.code <= DHCP_LAST_OPT)
737*d04ccbb3Scarlsonj 					opt = ack->opts[optnum.code];
738*d04ccbb3Scarlsonj 			}
7397c478bd9Sstevel@tonic-gate 			break;
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 		case DSYM_VENDOR:
742*d04ccbb3Scarlsonj 			if (isv6) {
743*d04ccbb3Scarlsonj 				dhcpv6_option_t *d6o;
744*d04ccbb3Scarlsonj 				uint32_t ent;
7457c478bd9Sstevel@tonic-gate 
746*d04ccbb3Scarlsonj 				/*
747*d04ccbb3Scarlsonj 				 * Look through vendor options to find our
748*d04ccbb3Scarlsonj 				 * enterprise number.
749*d04ccbb3Scarlsonj 				 */
750*d04ccbb3Scarlsonj 				d6o = NULL;
751*d04ccbb3Scarlsonj 				for (;;) {
752*d04ccbb3Scarlsonj 					d6o = dhcpv6_pkt_option(ack, d6o,
753*d04ccbb3Scarlsonj 					    DHCPV6_OPT_VENDOR_OPT, &optlen);
754*d04ccbb3Scarlsonj 					if (d6o == NULL)
755*d04ccbb3Scarlsonj 						break;
756*d04ccbb3Scarlsonj 					optlen -= sizeof (*d6o);
757*d04ccbb3Scarlsonj 					if (optlen < sizeof (ent))
758*d04ccbb3Scarlsonj 						continue;
759*d04ccbb3Scarlsonj 					(void) memcpy(&ent, d6o + 1,
760*d04ccbb3Scarlsonj 					    sizeof (ent));
761*d04ccbb3Scarlsonj 					if (ntohl(ent) != DHCPV6_SUN_ENT)
762*d04ccbb3Scarlsonj 						continue;
763*d04ccbb3Scarlsonj 					break;
764*d04ccbb3Scarlsonj 				}
765*d04ccbb3Scarlsonj 				if (d6o != NULL) {
766*d04ccbb3Scarlsonj 					/*
767*d04ccbb3Scarlsonj 					 * Now find the requested vendor option
768*d04ccbb3Scarlsonj 					 * within the vendor options block.
769*d04ccbb3Scarlsonj 					 */
770*d04ccbb3Scarlsonj 					opt = dhcpv6_find_option(
771*d04ccbb3Scarlsonj 					    (char *)(d6o + 1) + sizeof (ent),
772*d04ccbb3Scarlsonj 					    optlen - sizeof (ent), NULL,
773*d04ccbb3Scarlsonj 					    optnum.code, NULL);
774*d04ccbb3Scarlsonj 				}
775*d04ccbb3Scarlsonj 			} else {
776*d04ccbb3Scarlsonj 				/*
777*d04ccbb3Scarlsonj 				 * the test against VS_OPTION_START is broken
778*d04ccbb3Scarlsonj 				 * up into two tests to avoid compiler warnings
779*d04ccbb3Scarlsonj 				 * under intel.
780*d04ccbb3Scarlsonj 				 */
781*d04ccbb3Scarlsonj 				if ((optnum.code > VS_OPTION_START ||
782*d04ccbb3Scarlsonj 				    optnum.code == VS_OPTION_START) &&
783*d04ccbb3Scarlsonj 				    optnum.code <= VS_OPTION_END)
784*d04ccbb3Scarlsonj 					opt = ack->vs[optnum.code];
785*d04ccbb3Scarlsonj 			}
7867c478bd9Sstevel@tonic-gate 			break;
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 		case DSYM_FIELD:
789*d04ccbb3Scarlsonj 			if (isv6) {
790*d04ccbb3Scarlsonj 				dhcpv6_message_t *d6m =
791*d04ccbb3Scarlsonj 				    (dhcpv6_message_t *)ack->pkt;
792*d04ccbb3Scarlsonj 				dhcpv6_option_t *d6o;
793*d04ccbb3Scarlsonj 
794*d04ccbb3Scarlsonj 				/* Validate the packet field the user wants */
795*d04ccbb3Scarlsonj 				optlen = optnum.code + optnum.size;
796*d04ccbb3Scarlsonj 				if (d6m->d6m_msg_type ==
797*d04ccbb3Scarlsonj 				    DHCPV6_MSG_RELAY_FORW ||
798*d04ccbb3Scarlsonj 				    d6m->d6m_msg_type ==
799*d04ccbb3Scarlsonj 				    DHCPV6_MSG_RELAY_REPL) {
800*d04ccbb3Scarlsonj 					if (optlen > sizeof (dhcpv6_relay_t))
801*d04ccbb3Scarlsonj 						break;
802*d04ccbb3Scarlsonj 				} else {
803*d04ccbb3Scarlsonj 					if (optlen > sizeof (*d6m))
804*d04ccbb3Scarlsonj 						break;
805*d04ccbb3Scarlsonj 				}
806*d04ccbb3Scarlsonj 
807*d04ccbb3Scarlsonj 				opt = malloc(sizeof (*d6o) + optnum.size);
808*d04ccbb3Scarlsonj 				if (opt != NULL) {
809*d04ccbb3Scarlsonj 					d6o = opt;
810*d04ccbb3Scarlsonj 					d6o->d6o_code = htons(optnum.code);
811*d04ccbb3Scarlsonj 					d6o->d6o_len = htons(optnum.size);
812*d04ccbb3Scarlsonj 					(void) memcpy(d6o + 1, (caddr_t)d6m +
813*d04ccbb3Scarlsonj 					    optnum.code, optnum.size);
814*d04ccbb3Scarlsonj 				}
815*d04ccbb3Scarlsonj 			} else {
816*d04ccbb3Scarlsonj 				if (optnum.code + optnum.size > sizeof (PKT))
817*d04ccbb3Scarlsonj 					break;
818*d04ccbb3Scarlsonj 
819*d04ccbb3Scarlsonj 				/*
820*d04ccbb3Scarlsonj 				 * + 2 to account for option code and length
821*d04ccbb3Scarlsonj 				 * byte
822*d04ccbb3Scarlsonj 				 */
823*d04ccbb3Scarlsonj 				opt = malloc(optnum.size + 2);
824*d04ccbb3Scarlsonj 				if (opt != NULL) {
825*d04ccbb3Scarlsonj 					DHCP_OPT *v4opt = opt;
826*d04ccbb3Scarlsonj 
827*d04ccbb3Scarlsonj 					v4opt->len  = optnum.size;
828*d04ccbb3Scarlsonj 					v4opt->code = optnum.code;
829*d04ccbb3Scarlsonj 					(void) memcpy(v4opt->value,
830*d04ccbb3Scarlsonj 					    (caddr_t)ack->pkt + optnum.code,
831*d04ccbb3Scarlsonj 					    optnum.size);
832*d04ccbb3Scarlsonj 				}
833*d04ccbb3Scarlsonj 			}
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 			if (opt == NULL) {
836*d04ccbb3Scarlsonj 				send_error_reply(iap, DHCP_IPC_E_MEMORY);
8377c478bd9Sstevel@tonic-gate 				return;
8387c478bd9Sstevel@tonic-gate 			}
8397c478bd9Sstevel@tonic-gate 			did_alloc = B_TRUE;
8407c478bd9Sstevel@tonic-gate 			break;
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 		default:
843*d04ccbb3Scarlsonj 			send_error_reply(iap, DHCP_IPC_E_PROTO);
8447c478bd9Sstevel@tonic-gate 			return;
8457c478bd9Sstevel@tonic-gate 		}
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 		/*
8487c478bd9Sstevel@tonic-gate 		 * return the option payload, if there was one.  the "+ 2"
8497c478bd9Sstevel@tonic-gate 		 * accounts for the option code number and length byte.
8507c478bd9Sstevel@tonic-gate 		 */
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 		if (opt != NULL) {
853*d04ccbb3Scarlsonj 			if (isv6) {
854*d04ccbb3Scarlsonj 				dhcpv6_option_t d6ov;
855*d04ccbb3Scarlsonj 
856*d04ccbb3Scarlsonj 				(void) memcpy(&d6ov, opt, sizeof (d6ov));
857*d04ccbb3Scarlsonj 				optlen = ntohs(d6ov.d6o_len) + sizeof (d6ov);
858*d04ccbb3Scarlsonj 			} else {
859*d04ccbb3Scarlsonj 				optlen = ((DHCP_OPT *)opt)->len + 2;
860*d04ccbb3Scarlsonj 			}
861*d04ccbb3Scarlsonj 			send_data_reply(iap, 0, DHCP_TYPE_OPTION, opt, optlen);
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 			if (did_alloc)
8647c478bd9Sstevel@tonic-gate 				free(opt);
865*d04ccbb3Scarlsonj 			break;
866*d04ccbb3Scarlsonj 		} else if (ack != dsmp->dsm_orig_ack) {
8677c478bd9Sstevel@tonic-gate 			/*
8687c478bd9Sstevel@tonic-gate 			 * There wasn't any definition for the option in the
8697c478bd9Sstevel@tonic-gate 			 * current ack, so now retry with the original ack if
8707c478bd9Sstevel@tonic-gate 			 * the original ack is not the current ack.
8717c478bd9Sstevel@tonic-gate 			 */
872*d04ccbb3Scarlsonj 			ack = dsmp->dsm_orig_ack;
8737c478bd9Sstevel@tonic-gate 			goto load_option;
8747c478bd9Sstevel@tonic-gate 		}
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 		/*
8777c478bd9Sstevel@tonic-gate 		 * note that an "okay" response is returned either in
8787c478bd9Sstevel@tonic-gate 		 * the case of an unknown option or a known option
8797c478bd9Sstevel@tonic-gate 		 * with no payload.  this is okay (for now) since
8807c478bd9Sstevel@tonic-gate 		 * dhcpinfo checks whether an option is valid before
8817c478bd9Sstevel@tonic-gate 		 * ever performing ipc with the agent.
8827c478bd9Sstevel@tonic-gate 		 */
8837c478bd9Sstevel@tonic-gate 
884*d04ccbb3Scarlsonj 		send_ok_reply(iap);
885*d04ccbb3Scarlsonj 		break;
8867c478bd9Sstevel@tonic-gate 	}
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	case DHCP_INFORM:
889*d04ccbb3Scarlsonj 		dhcp_inform(dsmp);
8907c478bd9Sstevel@tonic-gate 		/* next destination: dhcp_acknak() */
891*d04ccbb3Scarlsonj 		break;		/* not an immediate function */
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	case DHCP_PING:
894*d04ccbb3Scarlsonj 		if (dsmp->dsm_dflags & DHCP_IF_FAILED)
895*d04ccbb3Scarlsonj 			send_error_reply(iap, DHCP_IPC_E_FAILEDIF);
8967c478bd9Sstevel@tonic-gate 		else
897*d04ccbb3Scarlsonj 			send_ok_reply(iap);
898*d04ccbb3Scarlsonj 		break;
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	case DHCP_RELEASE:
901*d04ccbb3Scarlsonj 		(void) script_start(dsmp, isv6 ? EVENT_RELEASE6 :
902*d04ccbb3Scarlsonj 		    EVENT_RELEASE, dhcp_release, "Finished with lease.", NULL);
903*d04ccbb3Scarlsonj 		break;		/* not an immediate function */
904*d04ccbb3Scarlsonj 
905*d04ccbb3Scarlsonj 	case DHCP_START: {
906*d04ccbb3Scarlsonj 		PKT_LIST *ack, *oack;
907*d04ccbb3Scarlsonj 		PKT_LIST *plp[2];
9087c478bd9Sstevel@tonic-gate 
909*d04ccbb3Scarlsonj 		deprecate_leases(dsmp);
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 		/*
9127c478bd9Sstevel@tonic-gate 		 * if we have a valid hostconf lying around, then jump
9137c478bd9Sstevel@tonic-gate 		 * into INIT_REBOOT.  if it fails, we'll end up going
9147c478bd9Sstevel@tonic-gate 		 * through the whole selecting() procedure again.
9157c478bd9Sstevel@tonic-gate 		 */
9167c478bd9Sstevel@tonic-gate 
917*d04ccbb3Scarlsonj 		error = read_hostconf(dsmp->dsm_name, plp, 2, dsmp->dsm_isv6);
918*d04ccbb3Scarlsonj 		ack = error > 0 ? plp[0] : NULL;
919*d04ccbb3Scarlsonj 		oack = error > 1 ? plp[1] : NULL;
920*d04ccbb3Scarlsonj 
921*d04ccbb3Scarlsonj 		/*
922*d04ccbb3Scarlsonj 		 * If the allocation of the old ack fails, that's fine;
923*d04ccbb3Scarlsonj 		 * continue without it.
924*d04ccbb3Scarlsonj 		 */
925*d04ccbb3Scarlsonj 		if (oack == NULL)
926*d04ccbb3Scarlsonj 			oack = ack;
927*d04ccbb3Scarlsonj 
928*d04ccbb3Scarlsonj 		/*
929*d04ccbb3Scarlsonj 		 * As long as we've allocated something, start using it.
930*d04ccbb3Scarlsonj 		 */
931*d04ccbb3Scarlsonj 		if (ack != NULL) {
932*d04ccbb3Scarlsonj 			dsmp->dsm_orig_ack = oack;
933*d04ccbb3Scarlsonj 			dsmp->dsm_ack = ack;
934*d04ccbb3Scarlsonj 			dhcp_init_reboot(dsmp);
9357c478bd9Sstevel@tonic-gate 			/* next destination: dhcp_acknak() */
936*d04ccbb3Scarlsonj 			break;
9377c478bd9Sstevel@tonic-gate 		}
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 		/*
9407c478bd9Sstevel@tonic-gate 		 * if not debugging, wait for a few seconds before
9417c478bd9Sstevel@tonic-gate 		 * going into SELECTING.
9427c478bd9Sstevel@tonic-gate 		 */
9437c478bd9Sstevel@tonic-gate 
944*d04ccbb3Scarlsonj 		if (debug_level == 0 &&
945*d04ccbb3Scarlsonj 		    iu_schedule_timer_ms(tq, lrand48() % DHCP_SELECT_WAIT,
946*d04ccbb3Scarlsonj 		    dhcp_start, dsmp) != -1) {
947*d04ccbb3Scarlsonj 			hold_smach(dsmp);
948*d04ccbb3Scarlsonj 			/* next destination: dhcp_start() */
949*d04ccbb3Scarlsonj 			break;
9507c478bd9Sstevel@tonic-gate 		}
9517c478bd9Sstevel@tonic-gate 
952*d04ccbb3Scarlsonj 		dhcp_selecting(dsmp);
9537c478bd9Sstevel@tonic-gate 		/* next destination: dhcp_requesting() */
954*d04ccbb3Scarlsonj 		break;
955*d04ccbb3Scarlsonj 	}
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	case DHCP_STATUS: {
9587c478bd9Sstevel@tonic-gate 		dhcp_status_t	status;
959*d04ccbb3Scarlsonj 		dhcp_lease_t	*dlp;
960*d04ccbb3Scarlsonj 
961*d04ccbb3Scarlsonj 		status.if_began = monosec_to_time(dsmp->dsm_curstart_monosec);
9627c478bd9Sstevel@tonic-gate 
963*d04ccbb3Scarlsonj 		/*
964*d04ccbb3Scarlsonj 		 * We return information on just the first lease as being
965*d04ccbb3Scarlsonj 		 * representative of the lot.  A better status mechanism is
966*d04ccbb3Scarlsonj 		 * needed.
967*d04ccbb3Scarlsonj 		 */
968*d04ccbb3Scarlsonj 		dlp = dsmp->dsm_leases;
9697c478bd9Sstevel@tonic-gate 
970*d04ccbb3Scarlsonj 		if (dlp == NULL ||
971*d04ccbb3Scarlsonj 		    dlp->dl_lifs->lif_expire.dt_start == DHCP_PERM) {
9727c478bd9Sstevel@tonic-gate 			status.if_t1	= DHCP_PERM;
9737c478bd9Sstevel@tonic-gate 			status.if_t2	= DHCP_PERM;
9747c478bd9Sstevel@tonic-gate 			status.if_lease	= DHCP_PERM;
9757c478bd9Sstevel@tonic-gate 		} else {
976*d04ccbb3Scarlsonj 			status.if_t1	= status.if_began +
977*d04ccbb3Scarlsonj 			    dlp->dl_t1.dt_start;
978*d04ccbb3Scarlsonj 			status.if_t2	= status.if_began +
979*d04ccbb3Scarlsonj 			    dlp->dl_t2.dt_start;
980*d04ccbb3Scarlsonj 			status.if_lease	= status.if_began +
981*d04ccbb3Scarlsonj 			    dlp->dl_lifs->lif_expire.dt_start;
9827c478bd9Sstevel@tonic-gate 		}
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 		status.version		= DHCP_STATUS_VER;
985*d04ccbb3Scarlsonj 		status.if_state		= dsmp->dsm_state;
986*d04ccbb3Scarlsonj 		status.if_dflags	= dsmp->dsm_dflags;
987*d04ccbb3Scarlsonj 		status.if_sent		= dsmp->dsm_sent;
988*d04ccbb3Scarlsonj 		status.if_recv		= dsmp->dsm_received;
989*d04ccbb3Scarlsonj 		status.if_bad_offers	= dsmp->dsm_bad_offers;
9907c478bd9Sstevel@tonic-gate 
991*d04ccbb3Scarlsonj 		(void) strlcpy(status.if_name, dsmp->dsm_name, IFNAMSIZ);
9927c478bd9Sstevel@tonic-gate 
993*d04ccbb3Scarlsonj 		send_data_reply(iap, 0, DHCP_TYPE_STATUS, &status,
9947c478bd9Sstevel@tonic-gate 		    sizeof (dhcp_status_t));
995*d04ccbb3Scarlsonj 		break;
9967c478bd9Sstevel@tonic-gate 	}
9977c478bd9Sstevel@tonic-gate 	}
9987c478bd9Sstevel@tonic-gate }
99969bb4bb4Scarlsonj 
100069bb4bb4Scarlsonj /*
100169bb4bb4Scarlsonj  * check_rtm_addr(): determine if routing socket message matches interface
100269bb4bb4Scarlsonj  *		     address
100369bb4bb4Scarlsonj  *
1004*d04ccbb3Scarlsonj  *   input: const struct if_msghdr *: pointer to routing socket message
1005*d04ccbb3Scarlsonj  *	    int: routing socket message length
1006*d04ccbb3Scarlsonj  *	    boolean_t: set to B_TRUE if IPv6
1007*d04ccbb3Scarlsonj  *	    const in6_addr_t *: pointer to IP address
1008*d04ccbb3Scarlsonj  *  output: boolean_t: B_TRUE if address is a match
100969bb4bb4Scarlsonj  */
1010*d04ccbb3Scarlsonj 
101169bb4bb4Scarlsonj static boolean_t
1012*d04ccbb3Scarlsonj check_rtm_addr(const struct ifa_msghdr *ifam, int msglen, boolean_t isv6,
1013*d04ccbb3Scarlsonj     const in6_addr_t *addr)
101469bb4bb4Scarlsonj {
1015*d04ccbb3Scarlsonj 	const char *cp, *lim;
101669bb4bb4Scarlsonj 	uint_t flag;
1017*d04ccbb3Scarlsonj 	const struct sockaddr *sa;
101869bb4bb4Scarlsonj 
101969bb4bb4Scarlsonj 	if (!(ifam->ifam_addrs & RTA_IFA))
102069bb4bb4Scarlsonj 		return (B_FALSE);
102169bb4bb4Scarlsonj 
1022*d04ccbb3Scarlsonj 	cp = (const char *)(ifam + 1);
1023*d04ccbb3Scarlsonj 	lim = (const char *)ifam + msglen;
102469bb4bb4Scarlsonj 	for (flag = 1; flag < RTA_IFA; flag <<= 1) {
102569bb4bb4Scarlsonj 		if (ifam->ifam_addrs & flag) {
102669bb4bb4Scarlsonj 			/* LINTED: alignment */
1027*d04ccbb3Scarlsonj 			sa = (const struct sockaddr *)cp;
1028*d04ccbb3Scarlsonj 			if ((const char *)(sa + 1) > lim)
102969bb4bb4Scarlsonj 				return (B_FALSE);
103069bb4bb4Scarlsonj 			switch (sa->sa_family) {
103169bb4bb4Scarlsonj 			case AF_INET:
103269bb4bb4Scarlsonj 				cp += sizeof (struct sockaddr_in);
103369bb4bb4Scarlsonj 				break;
103469bb4bb4Scarlsonj 			case AF_LINK:
103569bb4bb4Scarlsonj 				cp += sizeof (struct sockaddr_dl);
103669bb4bb4Scarlsonj 				break;
103769bb4bb4Scarlsonj 			case AF_INET6:
103869bb4bb4Scarlsonj 				cp += sizeof (struct sockaddr_in6);
103969bb4bb4Scarlsonj 				break;
104069bb4bb4Scarlsonj 			default:
104169bb4bb4Scarlsonj 				cp += sizeof (struct sockaddr);
104269bb4bb4Scarlsonj 				break;
104369bb4bb4Scarlsonj 			}
104469bb4bb4Scarlsonj 		}
104569bb4bb4Scarlsonj 	}
1046*d04ccbb3Scarlsonj 	if (isv6) {
1047*d04ccbb3Scarlsonj 		const struct sockaddr_in6 *sin6;
1048*d04ccbb3Scarlsonj 
1049*d04ccbb3Scarlsonj 		/* LINTED: alignment */
1050*d04ccbb3Scarlsonj 		sin6 = (const struct sockaddr_in6 *)cp;
1051*d04ccbb3Scarlsonj 		if ((const char *)(sin6 + 1) > lim)
1052*d04ccbb3Scarlsonj 			return (B_FALSE);
1053*d04ccbb3Scarlsonj 		if (sin6->sin6_family != AF_INET6)
1054*d04ccbb3Scarlsonj 			return (B_FALSE);
1055*d04ccbb3Scarlsonj 		return (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, addr));
1056*d04ccbb3Scarlsonj 	} else {
1057*d04ccbb3Scarlsonj 		const struct sockaddr_in *sinp;
1058*d04ccbb3Scarlsonj 		ipaddr_t v4addr;
1059*d04ccbb3Scarlsonj 
1060*d04ccbb3Scarlsonj 		/* LINTED: alignment */
1061*d04ccbb3Scarlsonj 		sinp = (const struct sockaddr_in *)cp;
1062*d04ccbb3Scarlsonj 		if ((const char *)(sinp + 1) > lim)
1063*d04ccbb3Scarlsonj 			return (B_FALSE);
1064*d04ccbb3Scarlsonj 		if (sinp->sin_family != AF_INET)
1065*d04ccbb3Scarlsonj 			return (B_FALSE);
1066*d04ccbb3Scarlsonj 		IN6_V4MAPPED_TO_IPADDR(addr, v4addr);
1067*d04ccbb3Scarlsonj 		return (sinp->sin_addr.s_addr == v4addr);
1068*d04ccbb3Scarlsonj 	}
1069*d04ccbb3Scarlsonj }
1070*d04ccbb3Scarlsonj 
1071*d04ccbb3Scarlsonj /*
1072*d04ccbb3Scarlsonj  * is_rtm_v6(): determine if routing socket message is IPv6
1073*d04ccbb3Scarlsonj  *
1074*d04ccbb3Scarlsonj  *   input: struct ifa_msghdr *: pointer to routing socket message
1075*d04ccbb3Scarlsonj  *	    int: message length
1076*d04ccbb3Scarlsonj  *  output: boolean_t
1077*d04ccbb3Scarlsonj  */
1078*d04ccbb3Scarlsonj 
1079*d04ccbb3Scarlsonj static boolean_t
1080*d04ccbb3Scarlsonj is_rtm_v6(const struct ifa_msghdr *ifam, int msglen)
1081*d04ccbb3Scarlsonj {
1082*d04ccbb3Scarlsonj 	const char *cp, *lim;
1083*d04ccbb3Scarlsonj 	uint_t flag;
1084*d04ccbb3Scarlsonj 	const struct sockaddr *sa;
1085*d04ccbb3Scarlsonj 
1086*d04ccbb3Scarlsonj 	cp = (const char *)(ifam + 1);
1087*d04ccbb3Scarlsonj 	lim = (const char *)ifam + msglen;
1088*d04ccbb3Scarlsonj 	for (flag = ifam->ifam_addrs; flag != 0; flag &= flag - 1) {
1089*d04ccbb3Scarlsonj 		/* LINTED: alignment */
1090*d04ccbb3Scarlsonj 		sa = (const struct sockaddr *)cp;
1091*d04ccbb3Scarlsonj 		if ((const char *)(sa + 1) > lim)
1092*d04ccbb3Scarlsonj 			return (B_FALSE);
1093*d04ccbb3Scarlsonj 		switch (sa->sa_family) {
1094*d04ccbb3Scarlsonj 		case AF_INET:
1095*d04ccbb3Scarlsonj 			return (B_FALSE);
1096*d04ccbb3Scarlsonj 		case AF_LINK:
1097*d04ccbb3Scarlsonj 			cp += sizeof (struct sockaddr_dl);
1098*d04ccbb3Scarlsonj 			break;
1099*d04ccbb3Scarlsonj 		case AF_INET6:
1100*d04ccbb3Scarlsonj 			return (B_TRUE);
1101*d04ccbb3Scarlsonj 		default:
1102*d04ccbb3Scarlsonj 			cp += sizeof (struct sockaddr);
1103*d04ccbb3Scarlsonj 			break;
1104*d04ccbb3Scarlsonj 		}
1105*d04ccbb3Scarlsonj 	}
1106*d04ccbb3Scarlsonj 	return (B_FALSE);
1107*d04ccbb3Scarlsonj }
1108*d04ccbb3Scarlsonj 
1109*d04ccbb3Scarlsonj /*
1110*d04ccbb3Scarlsonj  * check_lif(): check the state of a given logical interface and its DHCP
1111*d04ccbb3Scarlsonj  *		lease.  We've been told by the routing socket that the
1112*d04ccbb3Scarlsonj  *		corresponding ifIndex has changed.  This may mean that DAD has
1113*d04ccbb3Scarlsonj  *		completed or failed.
1114*d04ccbb3Scarlsonj  *
1115*d04ccbb3Scarlsonj  *   input: dhcp_lif_t *: pointer to the LIF
1116*d04ccbb3Scarlsonj  *	    const struct ifa_msghdr *: routing socket message
1117*d04ccbb3Scarlsonj  *	    int: size of routing socket message
1118*d04ccbb3Scarlsonj  *  output: boolean_t: B_TRUE if DAD has completed on this interface
1119*d04ccbb3Scarlsonj  */
1120*d04ccbb3Scarlsonj 
1121*d04ccbb3Scarlsonj static boolean_t
1122*d04ccbb3Scarlsonj check_lif(dhcp_lif_t *lif, const struct ifa_msghdr *ifam, int msglen)
1123*d04ccbb3Scarlsonj {
1124*d04ccbb3Scarlsonj 	boolean_t isv6, dad_wait, unplumb;
1125*d04ccbb3Scarlsonj 	int fd;
1126*d04ccbb3Scarlsonj 	struct lifreq lifr;
1127*d04ccbb3Scarlsonj 
1128*d04ccbb3Scarlsonj 	isv6 = lif->lif_pif->pif_isv6;
1129*d04ccbb3Scarlsonj 	fd = isv6 ? v6_sock_fd : v4_sock_fd;
1130*d04ccbb3Scarlsonj 
1131*d04ccbb3Scarlsonj 	/*
1132*d04ccbb3Scarlsonj 	 * Get the real (64 bit) logical interface flags.  Note that the
1133*d04ccbb3Scarlsonj 	 * routing socket message has flags, but these are just the lower 32
1134*d04ccbb3Scarlsonj 	 * bits.
1135*d04ccbb3Scarlsonj 	 */
1136*d04ccbb3Scarlsonj 	unplumb = B_FALSE;
1137*d04ccbb3Scarlsonj 	(void) memset(&lifr, 0, sizeof (lifr));
1138*d04ccbb3Scarlsonj 	(void) strlcpy(lifr.lifr_name, lif->lif_name, sizeof (lifr.lifr_name));
1139*d04ccbb3Scarlsonj 	if (ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) {
1140*d04ccbb3Scarlsonj 		/*
1141*d04ccbb3Scarlsonj 		 * Failing to retrieve flags means that the interface is gone.
1142*d04ccbb3Scarlsonj 		 * It hasn't failed to verify with DAD, but we still have to
1143*d04ccbb3Scarlsonj 		 * give up on it.
1144*d04ccbb3Scarlsonj 		 */
1145*d04ccbb3Scarlsonj 		lifr.lifr_flags = 0;
1146*d04ccbb3Scarlsonj 		if (errno == ENXIO) {
1147*d04ccbb3Scarlsonj 			lif->lif_plumbed = B_FALSE;
1148*d04ccbb3Scarlsonj 			dhcpmsg(MSG_INFO, "%s has been removed; abandoning",
1149*d04ccbb3Scarlsonj 			    lif->lif_name);
1150*d04ccbb3Scarlsonj 		} else {
1151*d04ccbb3Scarlsonj 			dhcpmsg(MSG_ERR,
1152*d04ccbb3Scarlsonj 			    "unable to retrieve interface flags on %s",
1153*d04ccbb3Scarlsonj 			    lif->lif_name);
1154*d04ccbb3Scarlsonj 		}
1155*d04ccbb3Scarlsonj 		unplumb = B_TRUE;
1156*d04ccbb3Scarlsonj 	} else if (!check_rtm_addr(ifam, msglen, isv6, &lif->lif_v6addr)) {
1157*d04ccbb3Scarlsonj 		/*
1158*d04ccbb3Scarlsonj 		 * If the message is not about this logical interface,
1159*d04ccbb3Scarlsonj 		 * then just ignore it.
1160*d04ccbb3Scarlsonj 		 */
116169bb4bb4Scarlsonj 		return (B_FALSE);
1162*d04ccbb3Scarlsonj 	} else if (lifr.lifr_flags & IFF_DUPLICATE) {
1163*d04ccbb3Scarlsonj 		dhcpmsg(MSG_ERROR, "interface %s has duplicate address",
1164*d04ccbb3Scarlsonj 		    lif->lif_name);
1165*d04ccbb3Scarlsonj 		lif_mark_decline(lif, "duplicate address");
1166*d04ccbb3Scarlsonj 		close_ip_lif(lif);
1167*d04ccbb3Scarlsonj 	}
1168*d04ccbb3Scarlsonj 
1169*d04ccbb3Scarlsonj 	dad_wait = lif->lif_dad_wait;
1170*d04ccbb3Scarlsonj 	if (dad_wait) {
1171*d04ccbb3Scarlsonj 		dhcpmsg(MSG_VERBOSE, "check_lif: %s has finished DAD",
1172*d04ccbb3Scarlsonj 		    lif->lif_name);
1173*d04ccbb3Scarlsonj 		lif->lif_dad_wait = B_FALSE;
1174*d04ccbb3Scarlsonj 	}
1175*d04ccbb3Scarlsonj 
1176*d04ccbb3Scarlsonj 	if (unplumb)
1177*d04ccbb3Scarlsonj 		unplumb_lif(lif);
1178*d04ccbb3Scarlsonj 
1179*d04ccbb3Scarlsonj 	return (dad_wait);
1180*d04ccbb3Scarlsonj }
1181*d04ccbb3Scarlsonj 
1182*d04ccbb3Scarlsonj /*
1183*d04ccbb3Scarlsonj  * check_main_lif(): check the state of a main logical interface for a state
1184*d04ccbb3Scarlsonj  *		     machine.  This is used only for DHCPv6.
1185*d04ccbb3Scarlsonj  *
1186*d04ccbb3Scarlsonj  *   input: dhcp_smach_t *: pointer to the state machine
1187*d04ccbb3Scarlsonj  *	    const struct ifa_msghdr *: routing socket message
1188*d04ccbb3Scarlsonj  *	    int: size of routing socket message
1189*d04ccbb3Scarlsonj  *  output: boolean_t: B_TRUE if LIF is ok.
1190*d04ccbb3Scarlsonj  */
1191*d04ccbb3Scarlsonj 
1192*d04ccbb3Scarlsonj static boolean_t
1193*d04ccbb3Scarlsonj check_main_lif(dhcp_smach_t *dsmp, const struct ifa_msghdr *ifam, int msglen)
1194*d04ccbb3Scarlsonj {
1195*d04ccbb3Scarlsonj 	dhcp_lif_t *lif = dsmp->dsm_lif;
1196*d04ccbb3Scarlsonj 	struct lifreq lifr;
1197*d04ccbb3Scarlsonj 
1198*d04ccbb3Scarlsonj 	/*
1199*d04ccbb3Scarlsonj 	 * Get the real (64 bit) logical interface flags.  Note that the
1200*d04ccbb3Scarlsonj 	 * routing socket message has flags, but these are just the lower 32
1201*d04ccbb3Scarlsonj 	 * bits.
1202*d04ccbb3Scarlsonj 	 */
1203*d04ccbb3Scarlsonj 	(void) memset(&lifr, 0, sizeof (lifr));
1204*d04ccbb3Scarlsonj 	(void) strlcpy(lifr.lifr_name, lif->lif_name, sizeof (lifr.lifr_name));
1205*d04ccbb3Scarlsonj 	if (ioctl(v6_sock_fd, SIOCGLIFFLAGS, &lifr) == -1) {
1206*d04ccbb3Scarlsonj 		/*
1207*d04ccbb3Scarlsonj 		 * Failing to retrieve flags means that the interface is gone.
1208*d04ccbb3Scarlsonj 		 * Our state machine is now trash.
1209*d04ccbb3Scarlsonj 		 */
1210*d04ccbb3Scarlsonj 		if (errno == ENXIO) {
1211*d04ccbb3Scarlsonj 			dhcpmsg(MSG_INFO, "%s has been removed; abandoning",
1212*d04ccbb3Scarlsonj 			    lif->lif_name);
1213*d04ccbb3Scarlsonj 		} else {
1214*d04ccbb3Scarlsonj 			dhcpmsg(MSG_ERR,
1215*d04ccbb3Scarlsonj 			    "unable to retrieve interface flags on %s",
1216*d04ccbb3Scarlsonj 			    lif->lif_name);
1217*d04ccbb3Scarlsonj 		}
1218*d04ccbb3Scarlsonj 		return (B_FALSE);
1219*d04ccbb3Scarlsonj 	} else if (!check_rtm_addr(ifam, msglen, B_TRUE, &lif->lif_v6addr)) {
1220*d04ccbb3Scarlsonj 		/*
1221*d04ccbb3Scarlsonj 		 * If the message is not about this logical interface,
1222*d04ccbb3Scarlsonj 		 * then just ignore it.
1223*d04ccbb3Scarlsonj 		 */
1224*d04ccbb3Scarlsonj 		return (B_TRUE);
1225*d04ccbb3Scarlsonj 	} else if (lifr.lifr_flags & IFF_DUPLICATE) {
1226*d04ccbb3Scarlsonj 		dhcpmsg(MSG_ERROR, "interface %s has duplicate address",
1227*d04ccbb3Scarlsonj 		    lif->lif_name);
1228*d04ccbb3Scarlsonj 		return (B_FALSE);
1229*d04ccbb3Scarlsonj 	} else {
1230*d04ccbb3Scarlsonj 		return (B_TRUE);
1231*d04ccbb3Scarlsonj 	}
1232*d04ccbb3Scarlsonj }
1233*d04ccbb3Scarlsonj 
1234*d04ccbb3Scarlsonj /*
1235*d04ccbb3Scarlsonj  * process_link_up_down(): check the state of a physical interface for up/down
1236*d04ccbb3Scarlsonj  *			   transitions; must go through INIT_REBOOT state if
1237*d04ccbb3Scarlsonj  *			   the link flaps.
1238*d04ccbb3Scarlsonj  *
1239*d04ccbb3Scarlsonj  *   input: dhcp_pif_t *: pointer to the physical interface to check
1240*d04ccbb3Scarlsonj  *	    const struct if_msghdr *: routing socket message
1241*d04ccbb3Scarlsonj  *  output: none
1242*d04ccbb3Scarlsonj  */
1243*d04ccbb3Scarlsonj 
1244*d04ccbb3Scarlsonj static void
1245*d04ccbb3Scarlsonj process_link_up_down(dhcp_pif_t *pif, const struct if_msghdr *ifm)
1246*d04ccbb3Scarlsonj {
1247*d04ccbb3Scarlsonj 	struct lifreq lifr;
1248*d04ccbb3Scarlsonj 	boolean_t isv6;
1249*d04ccbb3Scarlsonj 	int fd;
1250*d04ccbb3Scarlsonj 
1251*d04ccbb3Scarlsonj 	/*
1252*d04ccbb3Scarlsonj 	 * If the message implies no change of flags, then we're done; no need
1253*d04ccbb3Scarlsonj 	 * to check further.  Note that if we have multiple state machines on a
1254*d04ccbb3Scarlsonj 	 * single physical interface, this test keeps us from issuing an ioctl
1255*d04ccbb3Scarlsonj 	 * for each one.
1256*d04ccbb3Scarlsonj 	 */
1257*d04ccbb3Scarlsonj 	if ((ifm->ifm_flags & IFF_RUNNING) && pif->pif_running ||
1258*d04ccbb3Scarlsonj 	    !(ifm->ifm_flags & IFF_RUNNING) && !pif->pif_running)
1259*d04ccbb3Scarlsonj 		return;
1260*d04ccbb3Scarlsonj 
1261*d04ccbb3Scarlsonj 	/*
1262*d04ccbb3Scarlsonj 	 * We don't know what the real interface flags are, because the
1263*d04ccbb3Scarlsonj 	 * if_index number is only 16 bits; we must go ask.
1264*d04ccbb3Scarlsonj 	 */
1265*d04ccbb3Scarlsonj 	isv6 = pif->pif_isv6;
1266*d04ccbb3Scarlsonj 	fd = isv6 ? v6_sock_fd : v4_sock_fd;
1267*d04ccbb3Scarlsonj 	(void) memset(&lifr, 0, sizeof (lifr));
1268*d04ccbb3Scarlsonj 	(void) strlcpy(lifr.lifr_name, pif->pif_name, sizeof (lifr.lifr_name));
1269*d04ccbb3Scarlsonj 
1270*d04ccbb3Scarlsonj 	if (ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1 ||
1271*d04ccbb3Scarlsonj 	    !(lifr.lifr_flags & IFF_RUNNING)) {
1272*d04ccbb3Scarlsonj 		/*
1273*d04ccbb3Scarlsonj 		 * If we've lost the interface or it has gone down, then
1274*d04ccbb3Scarlsonj 		 * nothing special to do; just turn off the running flag.
1275*d04ccbb3Scarlsonj 		 */
1276*d04ccbb3Scarlsonj 		pif_status(pif, B_FALSE);
1277*d04ccbb3Scarlsonj 	} else {
1278*d04ccbb3Scarlsonj 		/*
1279*d04ccbb3Scarlsonj 		 * Interface has come back up: go through verification process.
1280*d04ccbb3Scarlsonj 		 */
1281*d04ccbb3Scarlsonj 		pif_status(pif, B_TRUE);
1282*d04ccbb3Scarlsonj 	}
128369bb4bb4Scarlsonj }
128469bb4bb4Scarlsonj 
128569bb4bb4Scarlsonj /*
128669bb4bb4Scarlsonj  * rtsock_event(): fetches routing socket messages and updates internal
128769bb4bb4Scarlsonj  *		   interface state based on those messages.
128869bb4bb4Scarlsonj  *
128969bb4bb4Scarlsonj  *   input: iu_eh_t *: unused
129069bb4bb4Scarlsonj  *	    int: the routing socket file descriptor
129169bb4bb4Scarlsonj  *	    (other arguments unused)
129269bb4bb4Scarlsonj  *  output: void
129369bb4bb4Scarlsonj  */
129469bb4bb4Scarlsonj 
129569bb4bb4Scarlsonj /* ARGSUSED */
129669bb4bb4Scarlsonj static void
129769bb4bb4Scarlsonj rtsock_event(iu_eh_t *ehp, int fd, short events, iu_event_id_t id, void *arg)
129869bb4bb4Scarlsonj {
1299*d04ccbb3Scarlsonj 	dhcp_smach_t *dsmp, *dsmnext;
130069bb4bb4Scarlsonj 	union {
130169bb4bb4Scarlsonj 		struct ifa_msghdr ifam;
1302*d04ccbb3Scarlsonj 		struct if_msghdr ifm;
130369bb4bb4Scarlsonj 		char buf[1024];
130469bb4bb4Scarlsonj 	} msg;
130569bb4bb4Scarlsonj 	uint16_t ifindex;
130669bb4bb4Scarlsonj 	int msglen;
1307*d04ccbb3Scarlsonj 	boolean_t isv6;
130869bb4bb4Scarlsonj 
130969bb4bb4Scarlsonj 	if ((msglen = read(fd, &msg, sizeof (msg))) <= 0)
131069bb4bb4Scarlsonj 		return;
131169bb4bb4Scarlsonj 
1312*d04ccbb3Scarlsonj 	/* Note that the routing socket interface index is just 16 bits */
1313*d04ccbb3Scarlsonj 	if (msg.ifm.ifm_type == RTM_IFINFO) {
1314*d04ccbb3Scarlsonj 		ifindex = msg.ifm.ifm_index;
1315*d04ccbb3Scarlsonj 		isv6 = (msg.ifm.ifm_flags & IFF_IPV6) ? B_TRUE : B_FALSE;
1316*d04ccbb3Scarlsonj 	} else if (msg.ifam.ifam_type == RTM_DELADDR ||
1317*d04ccbb3Scarlsonj 	    msg.ifam.ifam_type == RTM_NEWADDR) {
1318*d04ccbb3Scarlsonj 		ifindex = msg.ifam.ifam_index;
1319*d04ccbb3Scarlsonj 		isv6 = is_rtm_v6(&msg.ifam, msglen);
1320*d04ccbb3Scarlsonj 	} else {
132169bb4bb4Scarlsonj 		return;
1322*d04ccbb3Scarlsonj 	}
132369bb4bb4Scarlsonj 
1324*d04ccbb3Scarlsonj 	for (dsmp = lookup_smach_by_uindex(ifindex, NULL, isv6);
1325*d04ccbb3Scarlsonj 	    dsmp != NULL; dsmp = dsmnext) {
1326*d04ccbb3Scarlsonj 		DHCPSTATE oldstate;
1327*d04ccbb3Scarlsonj 		boolean_t lif_finished;
1328*d04ccbb3Scarlsonj 		boolean_t lease_removed;
1329*d04ccbb3Scarlsonj 		dhcp_lease_t *dlp, *dlnext;
133069bb4bb4Scarlsonj 
1331*d04ccbb3Scarlsonj 		/*
1332*d04ccbb3Scarlsonj 		 * Note that script_start can call dhcp_drop directly, and
1333*d04ccbb3Scarlsonj 		 * that will do release_smach.
1334*d04ccbb3Scarlsonj 		 */
1335*d04ccbb3Scarlsonj 		dsmnext = lookup_smach_by_uindex(ifindex, dsmp, isv6);
1336*d04ccbb3Scarlsonj 		oldstate = dsmp->dsm_state;
133769bb4bb4Scarlsonj 
133869bb4bb4Scarlsonj 		/*
1339*d04ccbb3Scarlsonj 		 * Look for link up/down notifications.  These occur on a
1340*d04ccbb3Scarlsonj 		 * physical interface basis.
134169bb4bb4Scarlsonj 		 */
1342*d04ccbb3Scarlsonj 		if (msg.ifm.ifm_type == RTM_IFINFO) {
1343*d04ccbb3Scarlsonj 			process_link_up_down(dsmp->dsm_lif->lif_pif, &msg.ifm);
134469bb4bb4Scarlsonj 			continue;
1345*d04ccbb3Scarlsonj 		}
134669bb4bb4Scarlsonj 
134769bb4bb4Scarlsonj 		/*
134869bb4bb4Scarlsonj 		 * Since we cannot trust the flags reported by the routing
134969bb4bb4Scarlsonj 		 * socket (they're just 32 bits -- and thus never include
135069bb4bb4Scarlsonj 		 * IFF_DUPLICATE), and we can't trust the ifindex (it's only 16
135169bb4bb4Scarlsonj 		 * bits and also doesn't reflect the alias in use), we get
135269bb4bb4Scarlsonj 		 * flags on all matching interfaces, and go by that.
135369bb4bb4Scarlsonj 		 */
1354*d04ccbb3Scarlsonj 		lif_finished = B_FALSE;
1355*d04ccbb3Scarlsonj 		lease_removed = B_FALSE;
1356*d04ccbb3Scarlsonj 		for (dlp = dsmp->dsm_leases; dlp != NULL; dlp = dlnext) {
1357*d04ccbb3Scarlsonj 			dhcp_lif_t *lif, *lifnext;
1358*d04ccbb3Scarlsonj 			uint_t nlifs = dlp->dl_nlifs;
1359*d04ccbb3Scarlsonj 
1360*d04ccbb3Scarlsonj 			dlnext = dlp->dl_next;
1361*d04ccbb3Scarlsonj 			for (lif = dlp->dl_lifs; lif != NULL && nlifs > 0;
1362*d04ccbb3Scarlsonj 			    lif = lifnext, nlifs--) {
1363*d04ccbb3Scarlsonj 				lifnext = lif->lif_next;
1364*d04ccbb3Scarlsonj 				if (check_lif(lif, &msg.ifam, msglen)) {
1365*d04ccbb3Scarlsonj 					dsmp->dsm_lif_wait--;
1366*d04ccbb3Scarlsonj 					lif_finished = B_TRUE;
1367*d04ccbb3Scarlsonj 				}
1368*d04ccbb3Scarlsonj 			}
1369*d04ccbb3Scarlsonj 			if (dlp->dl_nlifs == 0) {
1370*d04ccbb3Scarlsonj 				remove_lease(dlp);
1371*d04ccbb3Scarlsonj 				lease_removed = B_TRUE;
1372dc041e83Scarlsonj 			}
137369bb4bb4Scarlsonj 		}
137469bb4bb4Scarlsonj 
1375*d04ccbb3Scarlsonj 		if ((isv6 && !check_main_lif(dsmp, &msg.ifam, msglen)) ||
1376*d04ccbb3Scarlsonj 		    (!isv6 && !verify_lif(dsmp->dsm_lif))) {
1377*d04ccbb3Scarlsonj 			if (dsmp->dsm_script_pid != -1)
1378*d04ccbb3Scarlsonj 				script_stop(dsmp);
1379*d04ccbb3Scarlsonj 			(void) script_start(dsmp, EVENT_DROP6, dhcp_drop, NULL,
1380*d04ccbb3Scarlsonj 			    NULL);
1381*d04ccbb3Scarlsonj 			continue;
138269bb4bb4Scarlsonj 		}
138369bb4bb4Scarlsonj 
138469bb4bb4Scarlsonj 		/*
1385*d04ccbb3Scarlsonj 		 * Ignore this state machine if nothing interesting has
1386*d04ccbb3Scarlsonj 		 * happened.
1387*d04ccbb3Scarlsonj 		 */
1388*d04ccbb3Scarlsonj 		if (!lif_finished && dsmp->dsm_lif_down == 0 &&
1389*d04ccbb3Scarlsonj 		    (dsmp->dsm_leases != NULL || !lease_removed))
1390*d04ccbb3Scarlsonj 			continue;
1391*d04ccbb3Scarlsonj 
1392*d04ccbb3Scarlsonj 		/*
1393*d04ccbb3Scarlsonj 		 * If we're still waiting for DAD to complete on some of the
1394*d04ccbb3Scarlsonj 		 * configured LIFs, then don't send a response.
139569bb4bb4Scarlsonj 		 */
1396*d04ccbb3Scarlsonj 		if (dsmp->dsm_lif_wait != 0) {
1397*d04ccbb3Scarlsonj 			dhcpmsg(MSG_VERBOSE, "rtsock_event: %s still has %d "
1398*d04ccbb3Scarlsonj 			    "LIFs waiting on DAD", dsmp->dsm_name,
1399*d04ccbb3Scarlsonj 			    dsmp->dsm_lif_wait);
1400*d04ccbb3Scarlsonj 			continue;
1401*d04ccbb3Scarlsonj 		}
140269bb4bb4Scarlsonj 
1403*d04ccbb3Scarlsonj 		/*
1404*d04ccbb3Scarlsonj 		 * If we have some failed LIFs, then handle them now.  We'll
1405*d04ccbb3Scarlsonj 		 * remove them from the list.  Any leases that become empty are
1406*d04ccbb3Scarlsonj 		 * also removed as part of the decline-generation process.
1407*d04ccbb3Scarlsonj 		 */
1408*d04ccbb3Scarlsonj 		if (dsmp->dsm_lif_down != 0) {
1409*d04ccbb3Scarlsonj 			/*
1410*d04ccbb3Scarlsonj 			 * We need to switch back to PRE_BOUND state so that
1411*d04ccbb3Scarlsonj 			 * send_pkt_internal() uses DLPI instead of sockets.
1412*d04ccbb3Scarlsonj 			 * Our logical interface has already been torn down by
1413*d04ccbb3Scarlsonj 			 * the kernel, and thus we can't send DHCPDECLINE by
1414*d04ccbb3Scarlsonj 			 * way of regular IP.  (Unless we're adopting -- allow
1415*d04ccbb3Scarlsonj 			 * the grandparent to be handled as expected.)
1416*d04ccbb3Scarlsonj 			 */
1417*d04ccbb3Scarlsonj 			if (oldstate != ADOPTING) {
1418*d04ccbb3Scarlsonj 				(void) set_smach_state(dsmp, PRE_BOUND);
1419*d04ccbb3Scarlsonj 			}
1420*d04ccbb3Scarlsonj 			send_declines(dsmp);
1421*d04ccbb3Scarlsonj 		}
142269bb4bb4Scarlsonj 
1423*d04ccbb3Scarlsonj 		if (dsmp->dsm_leases == NULL) {
1424*d04ccbb3Scarlsonj 			dsmp->dsm_bad_offers++;
1425*d04ccbb3Scarlsonj 			/*
1426*d04ccbb3Scarlsonj 			 * For DHCPv6, we'll process the restart once we're
1427*d04ccbb3Scarlsonj 			 * done sending Decline messages, because these are
1428*d04ccbb3Scarlsonj 			 * supposed to be acknowledged.  With DHCPv4, there's
1429*d04ccbb3Scarlsonj 			 * no acknowledgment for a DECLINE, so after sending
1430*d04ccbb3Scarlsonj 			 * it, we just restart right away.
1431*d04ccbb3Scarlsonj 			 */
1432*d04ccbb3Scarlsonj 			if (!dsmp->dsm_isv6) {
1433*d04ccbb3Scarlsonj 				dhcpmsg(MSG_VERBOSE, "rtsock_event: %s has no "
1434*d04ccbb3Scarlsonj 				    "LIFs left; restarting", dsmp->dsm_name);
1435*d04ccbb3Scarlsonj 				dhcp_restart(dsmp);
1436*d04ccbb3Scarlsonj 			}
1437*d04ccbb3Scarlsonj 		} else {
1438*d04ccbb3Scarlsonj 			/*
1439*d04ccbb3Scarlsonj 			 * If we're now up on at least some of the leases and
1440*d04ccbb3Scarlsonj 			 * we were waiting for that, then kick off the rest of
1441*d04ccbb3Scarlsonj 			 * configuration.  Lease validation and DAD are done.
1442*d04ccbb3Scarlsonj 			 */
1443*d04ccbb3Scarlsonj 			dhcpmsg(MSG_VERBOSE, "rtsock_event: all LIFs verified "
1444*d04ccbb3Scarlsonj 			    "on %s in %s state", dsmp->dsm_name,
1445*d04ccbb3Scarlsonj 			    dhcp_state_to_string(oldstate));
1446*d04ccbb3Scarlsonj 			if (oldstate == PRE_BOUND ||
1447*d04ccbb3Scarlsonj 			    oldstate == ADOPTING)
1448*d04ccbb3Scarlsonj 				dhcp_bound_complete(dsmp);
1449*d04ccbb3Scarlsonj 			if (oldstate == ADOPTING)
1450*d04ccbb3Scarlsonj 				dhcp_adopt_complete(dsmp);
1451*d04ccbb3Scarlsonj 		}
145269bb4bb4Scarlsonj 	}
145369bb4bb4Scarlsonj }
1454*d04ccbb3Scarlsonj 
1455*d04ccbb3Scarlsonj /*
1456*d04ccbb3Scarlsonj  * check_cmd_allowed(): check whether the requested command is allowed in the
1457*d04ccbb3Scarlsonj  *			state specified.
1458*d04ccbb3Scarlsonj  *
1459*d04ccbb3Scarlsonj  *   input: DHCPSTATE: current state
1460*d04ccbb3Scarlsonj  *	    dhcp_ipc_type_t: requested command
1461*d04ccbb3Scarlsonj  *  output: boolean_t: B_TRUE if command is allowed in this state
1462*d04ccbb3Scarlsonj  */
1463*d04ccbb3Scarlsonj 
1464*d04ccbb3Scarlsonj boolean_t
1465*d04ccbb3Scarlsonj check_cmd_allowed(DHCPSTATE state, dhcp_ipc_type_t cmd)
1466*d04ccbb3Scarlsonj {
1467*d04ccbb3Scarlsonj 	return (ipc_cmd_allowed[state][cmd] != 0);
1468*d04ccbb3Scarlsonj }
1469