xref: /illumos-gate/usr/src/cmd/cmd-inet/sbin/dhcpagent/agent.c (revision b9538c2103fde34bfe8a8c2b3e878ecd1586d182)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2016-2017, Chris Fraire <cfraire@me.com>.
24  */
25 
26 #include <sys/types.h>
27 #include <stdlib.h>
28 #include <assert.h>
29 #include <errno.h>
30 #include <locale.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <signal.h>
34 #include <stdio.h>
35 #include <stdio_ext.h>
36 #include <dhcp_hostconf.h>
37 #include <dhcpagent_ipc.h>
38 #include <dhcpagent_util.h>
39 #include <dhcpmsg.h>
40 #include <dhcp_inittab.h>
41 #include <dhcp_symbol.h>
42 #include <netinet/dhcp.h>
43 #include <net/route.h>
44 #include <sys/sockio.h>
45 #include <sys/stat.h>
46 #include <stropts.h>
47 #include <fcntl.h>
48 #include <sys/scsi/adapters/iscsi_if.h>
49 
50 #include "async.h"
51 #include "agent.h"
52 #include "script_handler.h"
53 #include "util.h"
54 #include "class_id.h"
55 #include "states.h"
56 #include "packet.h"
57 #include "interface.h"
58 #include "defaults.h"
59 
60 #ifndef	TEXT_DOMAIN
61 #define	TEXT_DOMAIN	"SYS_TEST"
62 #endif
63 
64 iu_timer_id_t		inactivity_id;
65 int			class_id_len = 0;
66 char			*class_id;
67 iu_eh_t			*eh;
68 iu_tq_t			*tq;
69 pid_t			grandparent;
70 int			rtsock_fd;
71 
72 static boolean_t	shutdown_started = B_FALSE;
73 static boolean_t	do_adopt = B_FALSE;
74 static unsigned int	debug_level = 0;
75 static iu_eh_callback_t	accept_event, ipc_event, rtsock_event;
76 static void dhcp_smach_set_msg_reqhost(dhcp_smach_t *dsmp,
77 		ipc_action_t *iap);
78 static DHCP_OPT * dhcp_get_ack_or_state(const dhcp_smach_t *dsmp,
79 		const PKT_LIST *plp, uint_t codenum, boolean_t *did_alloc);
80 
81 /*
82  * The ipc_cmd_allowed[] table indicates which IPC commands are allowed in
83  * which states; a non-zero value indicates the command is permitted.
84  *
85  * START is permitted if the state machine is fresh, or if we are in the
86  * process of trying to obtain a lease (as a convenience to save the
87  * administrator from having to do an explicit DROP).  EXTEND, RELEASE, and
88  * GET_TAG require a lease to be obtained in order to make sense.  INFORM is
89  * permitted if the interface is fresh or has an INFORM in progress or
90  * previously done on it -- otherwise a DROP or RELEASE is first required.
91  * PING and STATUS always make sense and thus are always permitted, as is DROP
92  * in order to permit the administrator to always bail out.
93  */
94 static int ipc_cmd_allowed[DHCP_NSTATES][DHCP_NIPC] = {
95 	/*			  D  E	P  R  S	 S  I  G */
96 	/*			  R  X	I  E  T	 T  N  E */
97 	/*			  O  T	N  L  A	 A  F  T */
98 	/*			  P  E	G  E  R	 T  O  _ */
99 	/*			  .  N  .  A  T  U  R  T */
100 	/*			  .  D	.  S  .  S  M  A */
101 	/*			  .  .  .  E  .  .  .  G */
102 	/* INIT		*/	{ 1, 0, 1, 0, 1, 1, 1, 0 },
103 	/* SELECTING	*/	{ 1, 0, 1, 0, 1, 1, 0, 0 },
104 	/* REQUESTING	*/	{ 1, 0, 1, 0, 1, 1, 0, 0 },
105 	/* PRE_BOUND	*/	{ 1, 1, 1, 1, 0, 1, 0, 1 },
106 	/* BOUND	*/	{ 1, 1, 1, 1, 0, 1, 0, 1 },
107 	/* RENEWING	*/	{ 1, 1, 1, 1, 0, 1, 0, 1 },
108 	/* REBINDING	*/	{ 1, 1, 1, 1, 0, 1, 0, 1 },
109 	/* INFORMATION  */	{ 1, 0, 1, 0, 1, 1, 1, 1 },
110 	/* INIT_REBOOT  */	{ 1, 0, 1, 1, 1, 1, 0, 0 },
111 	/* ADOPTING	*/	{ 1, 0, 1, 1, 0, 1, 0, 0 },
112 	/* INFORM_SENT  */	{ 1, 0, 1, 0, 1, 1, 1, 0 },
113 	/* DECLINING	*/	{ 1, 1, 1, 1, 0, 1, 0, 1 },
114 	/* RELEASING	*/	{ 1, 0, 1, 0, 0, 1, 0, 1 },
115 };
116 
117 #define	CMD_ISPRIV	0x1	/* Command requires privileges */
118 #define	CMD_CREATE	0x2	/* Command creates an interface */
119 #define	CMD_BOOTP	0x4	/* Command is valid with BOOTP */
120 #define	CMD_IMMED	0x8	/* Reply is immediate (no BUSY state) */
121 
122 static uint_t ipc_cmd_flags[DHCP_NIPC] = {
123 	/* DHCP_DROP */		CMD_ISPRIV|CMD_BOOTP,
124 	/* DHCP_EXTEND */	CMD_ISPRIV,
125 	/* DHCP_PING */		CMD_BOOTP|CMD_IMMED,
126 	/* DHCP_RELEASE */	CMD_ISPRIV,
127 	/* DHCP_START */	CMD_CREATE|CMD_ISPRIV|CMD_BOOTP,
128 	/* DHCP_STATUS */	CMD_BOOTP|CMD_IMMED,
129 	/* DHCP_INFORM */	CMD_CREATE|CMD_ISPRIV,
130 	/* DHCP_GET_TAG */	CMD_BOOTP|CMD_IMMED
131 };
132 
133 static boolean_t is_iscsi_active(void);
134 
135 int
136 main(int argc, char **argv)
137 {
138 	boolean_t	is_daemon  = B_TRUE;
139 	boolean_t	is_verbose;
140 	int		ipc_fd;
141 	int		c;
142 	int		aware = RTAW_UNDER_IPMP;
143 	struct rlimit	rl;
144 
145 	debug_level = df_get_int("", B_FALSE, DF_DEBUG_LEVEL);
146 	is_verbose = df_get_bool("", B_FALSE, DF_VERBOSE);
147 
148 	/*
149 	 * -l is ignored for compatibility with old agent.
150 	 */
151 
152 	while ((c = getopt(argc, argv, "vd:l:fa")) != EOF) {
153 
154 		switch (c) {
155 
156 		case 'a':
157 			do_adopt = B_TRUE;
158 			grandparent = getpid();
159 			break;
160 
161 		case 'd':
162 			debug_level = strtoul(optarg, NULL, 0);
163 			break;
164 
165 		case 'f':
166 			is_daemon = B_FALSE;
167 			break;
168 
169 		case 'v':
170 			is_verbose = B_TRUE;
171 			break;
172 
173 		case '?':
174 			(void) fprintf(stderr, "usage: %s [-a] [-d n] [-f] [-v]"
175 			    "\n", argv[0]);
176 			return (EXIT_FAILURE);
177 
178 		default:
179 			break;
180 		}
181 	}
182 
183 	(void) setlocale(LC_ALL, "");
184 	(void) textdomain(TEXT_DOMAIN);
185 
186 	if (geteuid() != 0) {
187 		dhcpmsg_init(argv[0], B_FALSE, is_verbose, debug_level);
188 		dhcpmsg(MSG_ERROR, "must be super-user");
189 		dhcpmsg_fini();
190 		return (EXIT_FAILURE);
191 	}
192 
193 	if (is_daemon && daemonize() == 0) {
194 		dhcpmsg_init(argv[0], B_FALSE, is_verbose, debug_level);
195 		dhcpmsg(MSG_ERR, "cannot become daemon, exiting");
196 		dhcpmsg_fini();
197 		return (EXIT_FAILURE);
198 	}
199 
200 	/*
201 	 * Seed the random number generator, since we're going to need it
202 	 * to set transaction id's and for exponential backoff.
203 	 */
204 	srand48(gethrtime() ^ gethostid() ^ getpid());
205 
206 	dhcpmsg_init(argv[0], is_daemon, is_verbose, debug_level);
207 	(void) atexit(dhcpmsg_fini);
208 
209 	tq = iu_tq_create();
210 	eh = iu_eh_create();
211 
212 	if (eh == NULL || tq == NULL) {
213 		errno = ENOMEM;
214 		dhcpmsg(MSG_ERR, "cannot create timer queue or event handler");
215 		return (EXIT_FAILURE);
216 	}
217 
218 	/*
219 	 * ignore most signals that could be reasonably generated.
220 	 */
221 
222 	(void) signal(SIGTERM, graceful_shutdown);
223 	(void) signal(SIGQUIT, graceful_shutdown);
224 	(void) signal(SIGPIPE, SIG_IGN);
225 	(void) signal(SIGUSR1, SIG_IGN);
226 	(void) signal(SIGUSR2, SIG_IGN);
227 	(void) signal(SIGINT,  SIG_IGN);
228 	(void) signal(SIGHUP,  SIG_IGN);
229 	(void) signal(SIGCHLD, SIG_IGN);
230 
231 	/*
232 	 * upon SIGTHAW we need to refresh any non-infinite leases.
233 	 */
234 
235 	(void) iu_eh_register_signal(eh, SIGTHAW, refresh_smachs, NULL);
236 
237 	class_id = get_class_id();
238 	if (class_id != NULL)
239 		class_id_len = strlen(class_id);
240 	else
241 		dhcpmsg(MSG_WARNING, "get_class_id failed, continuing "
242 		    "with no vendor class id");
243 
244 	/*
245 	 * the inactivity timer is enabled any time there are no
246 	 * interfaces under DHCP control.  if DHCP_INACTIVITY_WAIT
247 	 * seconds transpire without an interface under DHCP control,
248 	 * the agent shuts down.
249 	 */
250 
251 	inactivity_id = iu_schedule_timer(tq, DHCP_INACTIVITY_WAIT,
252 	    inactivity_shutdown, NULL);
253 
254 	/*
255 	 * max out the number available descriptors, just in case..
256 	 */
257 
258 	rl.rlim_cur = RLIM_INFINITY;
259 	rl.rlim_max = RLIM_INFINITY;
260 	if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
261 		dhcpmsg(MSG_ERR, "setrlimit failed");
262 
263 	(void) enable_extended_FILE_stdio(-1, -1);
264 
265 	/*
266 	 * Create and bind default IP sockets used to control interfaces and to
267 	 * catch stray packets.
268 	 */
269 
270 	if (!dhcp_ip_default())
271 		return (EXIT_FAILURE);
272 
273 	/*
274 	 * create the ipc channel that the agent will listen for
275 	 * requests on, and register it with the event handler so that
276 	 * `accept_event' will be called back.
277 	 */
278 
279 	switch (dhcp_ipc_init(&ipc_fd)) {
280 
281 	case 0:
282 		break;
283 
284 	case DHCP_IPC_E_BIND:
285 		dhcpmsg(MSG_ERROR, "dhcp_ipc_init: cannot bind to port "
286 		    "%i (agent already running?)", IPPORT_DHCPAGENT);
287 		return (EXIT_FAILURE);
288 
289 	default:
290 		dhcpmsg(MSG_ERROR, "dhcp_ipc_init failed");
291 		return (EXIT_FAILURE);
292 	}
293 
294 	if (iu_register_event(eh, ipc_fd, POLLIN, accept_event, 0) == -1) {
295 		dhcpmsg(MSG_ERR, "cannot register ipc fd for messages");
296 		return (EXIT_FAILURE);
297 	}
298 
299 	/*
300 	 * Create the global routing socket.  This is used for monitoring
301 	 * interface transitions, so that we learn about the kernel's Duplicate
302 	 * Address Detection status, and for inserting and removing default
303 	 * routes as learned from DHCP servers.  Both v4 and v6 are handed
304 	 * with this one socket.
305 	 */
306 	rtsock_fd = socket(PF_ROUTE, SOCK_RAW, 0);
307 	if (rtsock_fd == -1) {
308 		dhcpmsg(MSG_ERR, "cannot open routing socket");
309 		return (EXIT_FAILURE);
310 	}
311 
312 	/*
313 	 * We're IPMP-aware and can manage IPMP test addresses, so issue
314 	 * RT_AWARE to get routing socket messages for interfaces under IPMP.
315 	 */
316 	if (setsockopt(rtsock_fd, SOL_ROUTE, RT_AWARE, &aware,
317 	    sizeof (aware)) == -1) {
318 		dhcpmsg(MSG_ERR, "cannot set RT_AWARE on routing socket");
319 		return (EXIT_FAILURE);
320 	}
321 
322 	if (iu_register_event(eh, rtsock_fd, POLLIN, rtsock_event, 0) == -1) {
323 		dhcpmsg(MSG_ERR, "cannot register routing socket for messages");
324 		return (EXIT_FAILURE);
325 	}
326 
327 	/*
328 	 * if the -a (adopt) option was specified, try to adopt the
329 	 * kernel-managed interface before we start.
330 	 */
331 
332 	if (do_adopt && !dhcp_adopt())
333 		return (EXIT_FAILURE);
334 
335 	/*
336 	 * For DHCPv6, we own all of the interfaces marked DHCPRUNNING.  As
337 	 * we're starting operation here, if there are any of those interfaces
338 	 * lingering around, they're strays, and need to be removed.
339 	 *
340 	 * It might be nice to save these addresses off somewhere -- for both
341 	 * v4 and v6 -- and use them as hints for later negotiation.
342 	 */
343 	remove_v6_strays();
344 
345 	/*
346 	 * enter the main event loop; this is where all the real work
347 	 * takes place (through registering events and scheduling timers).
348 	 * this function only returns when the agent is shutting down.
349 	 */
350 
351 	switch (iu_handle_events(eh, tq)) {
352 
353 	case -1:
354 		dhcpmsg(MSG_WARNING, "iu_handle_events exited abnormally");
355 		break;
356 
357 	case DHCP_REASON_INACTIVITY:
358 		dhcpmsg(MSG_INFO, "no interfaces to manage, shutting down...");
359 		break;
360 
361 	case DHCP_REASON_TERMINATE:
362 		dhcpmsg(MSG_INFO, "received SIGTERM, shutting down...");
363 		break;
364 
365 	case DHCP_REASON_SIGNAL:
366 		dhcpmsg(MSG_WARNING, "received unexpected signal, shutting "
367 		    "down...");
368 		break;
369 	}
370 
371 	(void) iu_eh_unregister_signal(eh, SIGTHAW, NULL);
372 
373 	iu_eh_destroy(eh);
374 	iu_tq_destroy(tq);
375 
376 	return (EXIT_SUCCESS);
377 }
378 
379 /*
380  * drain_script(): event loop callback during shutdown
381  *
382  *   input: eh_t *: unused
383  *	    void *: unused
384  *  output: boolean_t: B_TRUE if event loop should exit; B_FALSE otherwise
385  */
386 
387 /* ARGSUSED */
388 boolean_t
389 drain_script(iu_eh_t *ehp, void *arg)
390 {
391 	if (shutdown_started == B_FALSE) {
392 		shutdown_started = B_TRUE;
393 		/*
394 		 * Check if the system is diskless client and/or
395 		 * there are active iSCSI sessions
396 		 *
397 		 * Do not drop the lease, or the system will be
398 		 * unable to sync(dump) through nfs/iSCSI driver
399 		 */
400 		if (!do_adopt && !is_iscsi_active()) {
401 			nuke_smach_list();
402 		}
403 	}
404 	return (script_count == 0);
405 }
406 
407 /*
408  * accept_event(): accepts a new connection on the ipc socket and registers
409  *		   to receive its messages with the event handler
410  *
411  *   input: iu_eh_t *: unused
412  *	    int: the file descriptor in the iu_eh_t * the connection came in on
413  *	    (other arguments unused)
414  *  output: void
415  */
416 
417 /* ARGSUSED */
418 static void
419 accept_event(iu_eh_t *ehp, int fd, short events, iu_event_id_t id, void *arg)
420 {
421 	int	client_fd;
422 	int	is_priv;
423 
424 	if (dhcp_ipc_accept(fd, &client_fd, &is_priv) != 0) {
425 		dhcpmsg(MSG_ERR, "accept_event: accept on ipc socket");
426 		return;
427 	}
428 
429 	if (iu_register_event(eh, client_fd, POLLIN, ipc_event,
430 	    (void *)is_priv) == -1) {
431 		dhcpmsg(MSG_ERROR, "accept_event: cannot register ipc socket "
432 		    "for callback");
433 	}
434 }
435 
436 /*
437  * ipc_event(): processes incoming ipc requests
438  *
439  *   input: iu_eh_t *: unused
440  *	    int: the file descriptor in the iu_eh_t * the request came in on
441  *	    short: unused
442  *	    iu_event_id_t: event ID
443  *	    void *: indicates whether the request is from a privileged client
444  *  output: void
445  */
446 
447 /* ARGSUSED */
448 static void
449 ipc_event(iu_eh_t *ehp, int fd, short events, iu_event_id_t id, void *arg)
450 {
451 	ipc_action_t		ia, *iap;
452 	dhcp_smach_t		*dsmp;
453 	int			error, is_priv = (int)arg;
454 	const char		*ifname;
455 	boolean_t		isv6;
456 	boolean_t		dsm_created = B_FALSE;
457 
458 	ipc_action_init(&ia);
459 	error = dhcp_ipc_recv_request(fd, &ia.ia_request,
460 	    DHCP_IPC_REQUEST_WAIT);
461 	if (error != DHCP_IPC_SUCCESS) {
462 		if (error != DHCP_IPC_E_EOF) {
463 			dhcpmsg(MSG_ERROR,
464 			    "ipc_event: dhcp_ipc_recv_request failed: %s",
465 			    dhcp_ipc_strerror(error));
466 		} else {
467 			dhcpmsg(MSG_DEBUG, "ipc_event: connection closed");
468 		}
469 		if ((dsmp = lookup_smach_by_event(id)) != NULL) {
470 			ipc_action_finish(dsmp, error);
471 		} else {
472 			(void) iu_unregister_event(eh, id, NULL);
473 			(void) dhcp_ipc_close(fd);
474 		}
475 		return;
476 	}
477 
478 	/* Fill in temporary ipc_action structure for utility functions */
479 	ia.ia_cmd = DHCP_IPC_CMD(ia.ia_request->message_type);
480 	ia.ia_fd = fd;
481 	ia.ia_eid = id;
482 
483 	if (ia.ia_cmd >= DHCP_NIPC) {
484 		dhcpmsg(MSG_ERROR,
485 		    "ipc_event: invalid command (%s) attempted on %s",
486 		    dhcp_ipc_type_to_string(ia.ia_cmd), ia.ia_request->ifname);
487 		send_error_reply(&ia, DHCP_IPC_E_CMD_UNKNOWN);
488 		return;
489 	}
490 
491 	/* return EPERM for any of the privileged actions */
492 
493 	if (!is_priv && (ipc_cmd_flags[ia.ia_cmd] & CMD_ISPRIV)) {
494 		dhcpmsg(MSG_WARNING,
495 		    "ipc_event: privileged ipc command (%s) attempted on %s",
496 		    dhcp_ipc_type_to_string(ia.ia_cmd), ia.ia_request->ifname);
497 		send_error_reply(&ia, DHCP_IPC_E_PERM);
498 		return;
499 	}
500 
501 	/*
502 	 * Try to locate the state machine associated with this command.  If
503 	 * the command is DHCP_START or DHCP_INFORM and there isn't a state
504 	 * machine already, make one (there may already be one from a previous
505 	 * failed attempt to START or INFORM).  Otherwise, verify the reference
506 	 * is still valid.
507 	 *
508 	 * The interface name may be blank.  In that case, we look up the
509 	 * primary interface, and the requested type (v4 or v6) doesn't matter.
510 	 */
511 
512 	isv6 = (ia.ia_request->message_type & DHCP_V6) != 0;
513 	ifname = ia.ia_request->ifname;
514 	if (*ifname == '\0')
515 		dsmp = primary_smach(isv6);
516 	else
517 		dsmp = lookup_smach(ifname, isv6);
518 
519 	if (dsmp != NULL) {
520 		/* Note that verify_smach drops a reference */
521 		hold_smach(dsmp);
522 		if (!verify_smach(dsmp))
523 			dsmp = NULL;
524 	}
525 
526 	if (dsmp == NULL) {
527 		/*
528 		 * If the user asked for the primary DHCP interface by giving
529 		 * an empty string and there is no primary, then check if we're
530 		 * handling dhcpinfo.  If so, then simulate primary selection.
531 		 * Otherwise, report failure.
532 		 */
533 		if (ifname[0] == '\0') {
534 			if (ia.ia_cmd == DHCP_GET_TAG)
535 				dsmp = info_primary_smach(isv6);
536 			if (dsmp == NULL)
537 				error = DHCP_IPC_E_NOPRIMARY;
538 
539 		/*
540 		 * If there's no interface, and we're starting up, then create
541 		 * it now, along with a state machine for it.  Note that if
542 		 * insert_smach fails, it discards the LIF reference.
543 		 */
544 		} else if (ipc_cmd_flags[ia.ia_cmd] & CMD_CREATE) {
545 			dhcp_lif_t *lif;
546 
547 			lif = attach_lif(ifname, isv6, &error);
548 			if (lif != NULL &&
549 			    (dsmp = insert_smach(lif, &error)) != NULL) {
550 				/*
551 				 * Get client ID for logical interface.  (V4
552 				 * only, because V6 plumbs its own interfaces.)
553 				 */
554 				error = get_smach_cid(dsmp);
555 				if (error != DHCP_IPC_SUCCESS) {
556 					remove_smach(dsmp);
557 					dsmp = NULL;
558 				}
559 				dsm_created = (dsmp != NULL);
560 			}
561 
562 		/*
563 		 * Otherwise, this is an operation on an unknown interface.
564 		 */
565 		} else {
566 			error = DHCP_IPC_E_UNKIF;
567 		}
568 		if (dsmp == NULL) {
569 			send_error_reply(&ia, error);
570 			return;
571 		}
572 	}
573 
574 	/*
575 	 * If this is a request for DHCP to manage a lease on an address,
576 	 * ensure that IFF_DHCPRUNNING is set (we don't set this when the lif
577 	 * is created because the lif may have been created for INFORM).
578 	 */
579 	if (ia.ia_cmd == DHCP_START &&
580 	    (error = set_lif_dhcp(dsmp->dsm_lif)) != DHCP_IPC_SUCCESS) {
581 		if (dsm_created)
582 			remove_smach(dsmp);
583 		send_error_reply(&ia, error);
584 		return;
585 	}
586 
587 	if ((dsmp->dsm_dflags & DHCP_IF_BOOTP) &&
588 	    !(ipc_cmd_flags[ia.ia_cmd] & CMD_BOOTP)) {
589 		dhcpmsg(MSG_ERROR, "command %s not valid for BOOTP on %s",
590 		    dhcp_ipc_type_to_string(ia.ia_cmd), dsmp->dsm_name);
591 		send_error_reply(&ia, DHCP_IPC_E_BOOTP);
592 		return;
593 	}
594 
595 	/*
596 	 * verify that the state machine is in a state which will allow the
597 	 * command.  we do this up front so that we can return an error
598 	 * *before* needlessly cancelling an in-progress transaction.
599 	 */
600 
601 	if (!check_cmd_allowed(dsmp->dsm_state, ia.ia_cmd)) {
602 		dhcpmsg(MSG_DEBUG,
603 		    "in state %s; not allowing %s command on %s",
604 		    dhcp_state_to_string(dsmp->dsm_state),
605 		    dhcp_ipc_type_to_string(ia.ia_cmd), dsmp->dsm_name);
606 		send_error_reply(&ia,
607 		    ia.ia_cmd == DHCP_START && dsmp->dsm_state != INIT ?
608 		    DHCP_IPC_E_RUNNING : DHCP_IPC_E_OUTSTATE);
609 		return;
610 	}
611 
612 	dhcpmsg(MSG_DEBUG, "in state %s; allowing %s command on %s",
613 	    dhcp_state_to_string(dsmp->dsm_state),
614 	    dhcp_ipc_type_to_string(ia.ia_cmd), dsmp->dsm_name);
615 
616 	if ((ia.ia_request->message_type & DHCP_PRIMARY) && is_priv)
617 		make_primary(dsmp);
618 
619 	/*
620 	 * The current design dictates that there can be only one outstanding
621 	 * transaction per state machine -- this simplifies the code
622 	 * considerably and also fits well with RFCs 2131 and 3315.  It is
623 	 * worth classifying the different DHCP commands into synchronous
624 	 * (those which we will handle now and reply to immediately) and
625 	 * asynchronous (those which require transactions and will be completed
626 	 * at an indeterminate time in the future):
627 	 *
628 	 *    DROP: removes the agent's management of a state machine.
629 	 *	    asynchronous as the script program may be invoked.
630 	 *
631 	 *    PING: checks to see if the agent has a named state machine.
632 	 *	    synchronous, since no packets need to be sent
633 	 *	    to the DHCP server.
634 	 *
635 	 *  STATUS: returns information about a state machine.
636 	 *	    synchronous, since no packets need to be sent
637 	 *	    to the DHCP server.
638 	 *
639 	 * RELEASE: releases the agent's management of a state machine
640 	 *	    and brings the associated interfaces down.  asynchronous
641 	 *	    as the script program may be invoked.
642 	 *
643 	 *  EXTEND: renews a lease.  asynchronous, since the agent
644 	 *	    needs to wait for an ACK, etc.
645 	 *
646 	 *   START: starts DHCP on a named state machine.  asynchronous since
647 	 *	    the agent needs to wait for OFFERs, ACKs, etc.
648 	 *
649 	 *  INFORM: obtains configuration parameters for the system using
650 	 *	    externally configured interface.  asynchronous, since the
651 	 *	    agent needs to wait for an ACK.
652 	 *
653 	 * Notice that EXTEND, INFORM, START, DROP and RELEASE are
654 	 * asynchronous.  Notice also that asynchronous commands may occur from
655 	 * within the agent -- for instance, the agent will need to do implicit
656 	 * EXTENDs to extend the lease. In order to make the code simpler, the
657 	 * following rules apply for asynchronous commands:
658 	 *
659 	 * There can only be one asynchronous command at a time per state
660 	 * machine.  The current asynchronous command is managed by the async_*
661 	 * api: async_start(), async_finish(), and async_cancel().
662 	 * async_start() starts management of a new asynchronous command on an
663 	 * state machine, which should only be done after async_cancel() to
664 	 * terminate a previous command.  When the command is completed,
665 	 * async_finish() should be called.
666 	 *
667 	 * Asynchronous commands started by a user command have an associated
668 	 * ipc_action which provides the agent with information for how to get
669 	 * in touch with the user command when the action completes.  These
670 	 * ipc_action records also have an associated timeout which may be
671 	 * infinite.  ipc_action_start() should be called when starting an
672 	 * asynchronous command requested by a user, which sets up the timer
673 	 * and keeps track of the ipc information (file descriptor, request
674 	 * type).  When the asynchronous command completes, ipc_action_finish()
675 	 * should be called to return a command status code to the user and
676 	 * close the ipc connection).  If the command does not complete before
677 	 * the timer fires, ipc_action_timeout() is called which closes the ipc
678 	 * connection and returns DHCP_IPC_E_TIMEOUT to the user.  Note that
679 	 * independent of ipc_action_timeout(), ipc_action_finish() should be
680 	 * called.
681 	 *
682 	 * on a case-by-case basis, here is what happens (per state machine):
683 	 *
684 	 *    o When an asynchronous command is requested, then
685 	 *	async_cancel() is called to terminate any non-user
686 	 *	action in progress.  If there's a user action running,
687 	 *	the user command is sent DHCP_IPC_E_PEND.
688 	 *
689 	 *    o otherwise, the the transaction is started with
690 	 *	async_start().  if the transaction is on behalf
691 	 *	of a user, ipc_action_start() is called to keep
692 	 *	track of the ipc information and set up the
693 	 *	ipc_action timer.
694 	 *
695 	 *    o if the command completes normally and before a
696 	 *	timeout fires, then async_finish() is called.
697 	 *	if there was an associated ipc_action,
698 	 *	ipc_action_finish() is called to complete it.
699 	 *
700 	 *    o if the command fails before a timeout fires, then
701 	 *	async_finish() is called, and the state machine is
702 	 *	is returned to a known state based on the command.
703 	 *	if there was an associated ipc_action,
704 	 *	ipc_action_finish() is called to complete it.
705 	 *
706 	 *    o if the ipc_action timer fires before command
707 	 *	completion, then DHCP_IPC_E_TIMEOUT is returned to
708 	 *	the user.  however, the transaction continues to
709 	 *	be carried out asynchronously.
710 	 */
711 
712 	if (ipc_cmd_flags[ia.ia_cmd] & CMD_IMMED) {
713 		/*
714 		 * Only immediate commands (ping, status, get_tag) need to
715 		 * worry about freeing ia through one of the reply functions
716 		 * before returning.
717 		 */
718 		iap = &ia;
719 	} else {
720 		/*
721 		 * if shutdown request has been received, send back an error.
722 		 */
723 		if (shutdown_started) {
724 			send_error_reply(&ia, DHCP_IPC_E_OUTSTATE);
725 			return;
726 		}
727 
728 		if (dsmp->dsm_dflags & DHCP_IF_BUSY) {
729 			send_error_reply(&ia, DHCP_IPC_E_PEND);
730 			return;
731 		}
732 
733 		if (!ipc_action_start(dsmp, &ia)) {
734 			dhcpmsg(MSG_WARNING, "ipc_event: ipc_action_start "
735 			    "failed for %s", dsmp->dsm_name);
736 			send_error_reply(&ia, DHCP_IPC_E_MEMORY);
737 			return;
738 		}
739 
740 		/* Action structure consumed by above function */
741 		iap = &dsmp->dsm_ia;
742 	}
743 
744 	switch (iap->ia_cmd) {
745 
746 	case DHCP_DROP:
747 		if (dsmp->dsm_droprelease)
748 			break;
749 		dsmp->dsm_droprelease = B_TRUE;
750 
751 		/*
752 		 * Ensure that a timer associated with the existing state
753 		 * doesn't pop while we're waiting for the script to complete.
754 		 * (If so, chaos can result -- e.g., a timer causes us to end
755 		 * up in dhcp_selecting() would start acquiring a new lease on
756 		 * dsmp while our DHCP_DROP dismantling is ongoing.)
757 		 */
758 		cancel_smach_timers(dsmp);
759 		(void) script_start(dsmp, isv6 ? EVENT_DROP6 : EVENT_DROP,
760 		    dhcp_drop, NULL, NULL);
761 		break;		/* not an immediate function */
762 
763 	case DHCP_EXTEND:
764 		dhcp_smach_set_msg_reqhost(dsmp, iap);
765 		(void) dhcp_extending(dsmp);
766 		break;
767 
768 	case DHCP_GET_TAG: {
769 		dhcp_optnum_t	optnum;
770 		void		*opt = NULL;
771 		uint_t		optlen;
772 		boolean_t	did_alloc = B_FALSE;
773 		PKT_LIST	*ack = dsmp->dsm_ack;
774 		int		i;
775 
776 		/*
777 		 * verify the request makes sense.
778 		 */
779 
780 		if (iap->ia_request->data_type   != DHCP_TYPE_OPTNUM ||
781 		    iap->ia_request->data_length != sizeof (dhcp_optnum_t)) {
782 			send_error_reply(iap, DHCP_IPC_E_PROTO);
783 			break;
784 		}
785 
786 		(void) memcpy(&optnum, iap->ia_request->buffer,
787 		    sizeof (dhcp_optnum_t));
788 
789 load_option:
790 		switch (optnum.category) {
791 
792 		case DSYM_SITE:			/* FALLTHRU */
793 		case DSYM_STANDARD:
794 			for (i = 0; i < dsmp->dsm_pillen; i++) {
795 				if (dsmp->dsm_pil[i] == optnum.code)
796 					break;
797 			}
798 			if (i < dsmp->dsm_pillen)
799 				break;
800 			if (isv6) {
801 				opt = dhcpv6_pkt_option(ack, NULL, optnum.code,
802 				    NULL);
803 			} else {
804 				opt = dhcp_get_ack_or_state(dsmp, ack,
805 				    optnum.code, &did_alloc);
806 			}
807 			break;
808 
809 		case DSYM_VENDOR:
810 			if (isv6) {
811 				dhcpv6_option_t *d6o;
812 				uint32_t ent;
813 
814 				/*
815 				 * Look through vendor options to find our
816 				 * enterprise number.
817 				 */
818 				d6o = NULL;
819 				for (;;) {
820 					d6o = dhcpv6_pkt_option(ack, d6o,
821 					    DHCPV6_OPT_VENDOR_OPT, &optlen);
822 					if (d6o == NULL)
823 						break;
824 					optlen -= sizeof (*d6o);
825 					if (optlen < sizeof (ent))
826 						continue;
827 					(void) memcpy(&ent, d6o + 1,
828 					    sizeof (ent));
829 					if (ntohl(ent) != DHCPV6_SUN_ENT)
830 						continue;
831 					break;
832 				}
833 				if (d6o != NULL) {
834 					/*
835 					 * Now find the requested vendor option
836 					 * within the vendor options block.
837 					 */
838 					opt = dhcpv6_find_option(
839 					    (char *)(d6o + 1) + sizeof (ent),
840 					    optlen - sizeof (ent), NULL,
841 					    optnum.code, NULL);
842 				}
843 			} else {
844 				/*
845 				 * the test against VS_OPTION_START is broken
846 				 * up into two tests to avoid compiler warnings
847 				 * under intel.
848 				 */
849 				if ((optnum.code > VS_OPTION_START ||
850 				    optnum.code == VS_OPTION_START) &&
851 				    optnum.code <= VS_OPTION_END)
852 					opt = ack->vs[optnum.code];
853 			}
854 			break;
855 
856 		case DSYM_FIELD:
857 			if (isv6) {
858 				dhcpv6_message_t *d6m =
859 				    (dhcpv6_message_t *)ack->pkt;
860 				dhcpv6_option_t *d6o;
861 
862 				/* Validate the packet field the user wants */
863 				optlen = optnum.code + optnum.size;
864 				if (d6m->d6m_msg_type ==
865 				    DHCPV6_MSG_RELAY_FORW ||
866 				    d6m->d6m_msg_type ==
867 				    DHCPV6_MSG_RELAY_REPL) {
868 					if (optlen > sizeof (dhcpv6_relay_t))
869 						break;
870 				} else {
871 					if (optlen > sizeof (*d6m))
872 						break;
873 				}
874 
875 				opt = malloc(sizeof (*d6o) + optnum.size);
876 				if (opt != NULL) {
877 					d6o = opt;
878 					d6o->d6o_code = htons(optnum.code);
879 					d6o->d6o_len = htons(optnum.size);
880 					(void) memcpy(d6o + 1, (caddr_t)d6m +
881 					    optnum.code, optnum.size);
882 				}
883 			} else {
884 				if (optnum.code + optnum.size > sizeof (PKT))
885 					break;
886 
887 				opt = malloc(optnum.size + DHCP_OPT_META_LEN);
888 				if (opt != NULL) {
889 					DHCP_OPT *v4opt = opt;
890 
891 					v4opt->len  = optnum.size;
892 					v4opt->code = optnum.code;
893 					(void) memcpy(v4opt->value,
894 					    (caddr_t)ack->pkt + optnum.code,
895 					    optnum.size);
896 				}
897 			}
898 
899 			if (opt == NULL) {
900 				send_error_reply(iap, DHCP_IPC_E_MEMORY);
901 				return;
902 			}
903 			did_alloc = B_TRUE;
904 			break;
905 
906 		default:
907 			send_error_reply(iap, DHCP_IPC_E_PROTO);
908 			return;
909 		}
910 
911 		/*
912 		 * return the option payload, if there was one.
913 		 */
914 
915 		if (opt != NULL) {
916 			if (isv6) {
917 				dhcpv6_option_t d6ov;
918 
919 				(void) memcpy(&d6ov, opt, sizeof (d6ov));
920 				optlen = ntohs(d6ov.d6o_len) + sizeof (d6ov);
921 			} else {
922 				optlen = ((DHCP_OPT *)opt)->len +
923 				    DHCP_OPT_META_LEN;
924 			}
925 			send_data_reply(iap, 0, DHCP_TYPE_OPTION, opt, optlen);
926 
927 			if (did_alloc)
928 				free(opt);
929 			break;
930 		} else if (ack != dsmp->dsm_orig_ack) {
931 			/*
932 			 * There wasn't any definition for the option in the
933 			 * current ack, so now retry with the original ack if
934 			 * the original ack is not the current ack.
935 			 */
936 			ack = dsmp->dsm_orig_ack;
937 			goto load_option;
938 		}
939 
940 		/*
941 		 * note that an "okay" response is returned either in
942 		 * the case of an unknown option or a known option
943 		 * with no payload.  this is okay (for now) since
944 		 * dhcpinfo checks whether an option is valid before
945 		 * ever performing ipc with the agent.
946 		 */
947 
948 		send_ok_reply(iap);
949 		break;
950 	}
951 
952 	case DHCP_INFORM:
953 		dhcp_inform(dsmp);
954 		/* next destination: dhcp_acknak() */
955 		break;		/* not an immediate function */
956 
957 	case DHCP_PING:
958 		if (dsmp->dsm_dflags & DHCP_IF_FAILED)
959 			send_error_reply(iap, DHCP_IPC_E_FAILEDIF);
960 		else
961 			send_ok_reply(iap);
962 		break;
963 
964 	case DHCP_RELEASE:
965 		if (dsmp->dsm_droprelease)
966 			break;
967 		dsmp->dsm_droprelease = B_TRUE;
968 		cancel_smach_timers(dsmp); /* see comment in DHCP_DROP above */
969 		(void) script_start(dsmp, isv6 ? EVENT_RELEASE6 :
970 		    EVENT_RELEASE, dhcp_release, "Finished with lease.", NULL);
971 		break;		/* not an immediate function */
972 
973 	case DHCP_START: {
974 		PKT_LIST *ack, *oack;
975 		PKT_LIST *plp[2];
976 
977 		deprecate_leases(dsmp);
978 		dhcp_smach_set_msg_reqhost(dsmp, iap);
979 
980 		/*
981 		 * if we have a valid hostconf lying around, then jump
982 		 * into INIT_REBOOT.  if it fails, we'll end up going
983 		 * through the whole selecting() procedure again.
984 		 */
985 
986 		error = read_hostconf(dsmp->dsm_name, plp, 2, dsmp->dsm_isv6);
987 		ack = error > 0 ? plp[0] : NULL;
988 		oack = error > 1 ? plp[1] : NULL;
989 
990 		/*
991 		 * If the allocation of the old ack fails, that's fine;
992 		 * continue without it.
993 		 */
994 		if (oack == NULL)
995 			oack = ack;
996 
997 		/*
998 		 * As long as we've allocated something, start using it.
999 		 */
1000 		if (ack != NULL) {
1001 			dsmp->dsm_orig_ack = oack;
1002 			dsmp->dsm_ack = ack;
1003 			dhcp_init_reboot(dsmp);
1004 			/* next destination: dhcp_acknak() */
1005 			break;
1006 		}
1007 
1008 		/*
1009 		 * if not debugging, wait for a few seconds before
1010 		 * going into SELECTING.
1011 		 */
1012 
1013 		if (debug_level == 0 && set_start_timer(dsmp)) {
1014 			/* next destination: dhcp_start() */
1015 			break;
1016 		} else {
1017 			dhcp_selecting(dsmp);
1018 			/* next destination: dhcp_requesting() */
1019 			break;
1020 		}
1021 	}
1022 
1023 	case DHCP_STATUS: {
1024 		dhcp_status_t	status;
1025 		dhcp_lease_t	*dlp;
1026 
1027 		status.if_began = monosec_to_time(dsmp->dsm_curstart_monosec);
1028 
1029 		/*
1030 		 * We return information on just the first lease as being
1031 		 * representative of the lot.  A better status mechanism is
1032 		 * needed.
1033 		 */
1034 		dlp = dsmp->dsm_leases;
1035 
1036 		if (dlp == NULL ||
1037 		    dlp->dl_lifs->lif_expire.dt_start == DHCP_PERM) {
1038 			status.if_t1	= DHCP_PERM;
1039 			status.if_t2	= DHCP_PERM;
1040 			status.if_lease	= DHCP_PERM;
1041 		} else {
1042 			status.if_t1	= status.if_began +
1043 			    dlp->dl_t1.dt_start;
1044 			status.if_t2	= status.if_began +
1045 			    dlp->dl_t2.dt_start;
1046 			status.if_lease	= status.if_began +
1047 			    dlp->dl_lifs->lif_expire.dt_start;
1048 		}
1049 
1050 		status.version		= DHCP_STATUS_VER;
1051 		status.if_state		= dsmp->dsm_state;
1052 		status.if_dflags	= dsmp->dsm_dflags;
1053 		status.if_sent		= dsmp->dsm_sent;
1054 		status.if_recv		= dsmp->dsm_received;
1055 		status.if_bad_offers	= dsmp->dsm_bad_offers;
1056 
1057 		(void) strlcpy(status.if_name, dsmp->dsm_name, LIFNAMSIZ);
1058 
1059 		send_data_reply(iap, 0, DHCP_TYPE_STATUS, &status,
1060 		    sizeof (dhcp_status_t));
1061 		break;
1062 	}
1063 	}
1064 }
1065 
1066 /*
1067  * dhcp_smach_set_msg_reqhost(): set dsm_msg_reqhost based on the message
1068  * content of a DHCP IPC message
1069  *
1070  *   input: dhcp_smach_t *: the state machine instance;
1071  *	    ipc_action_t *: the decoded DHCP IPC message;
1072  *  output: void
1073  */
1074 
1075 static void
1076 dhcp_smach_set_msg_reqhost(dhcp_smach_t *dsmp, ipc_action_t *iap)
1077 {
1078 	DHCP_OPT	*d4o;
1079 	dhcp_symbol_t	*entry;
1080 	char		*value;
1081 
1082 	if (dsmp->dsm_msg_reqhost != NULL) {
1083 		dhcpmsg(MSG_DEBUG,
1084 		    "dhcp_smach_set_msg_reqhost: nullify former value, %s",
1085 		    dsmp->dsm_msg_reqhost);
1086 		free(dsmp->dsm_msg_reqhost);
1087 		dsmp->dsm_msg_reqhost = NULL;
1088 	}
1089 
1090 	/*
1091 	 * if a STANDARD/HOSTNAME was sent in the IPC request, then copy that
1092 	 * value into the state machine data if decoding succeeds. Otherwise,
1093 	 * log to indicate at what step the decoding stopped.
1094 	 */
1095 
1096 	if (dsmp->dsm_isv6) {
1097 		dhcpmsg(MSG_DEBUG, "dhcp_smach_set_msg_reqhost: ipv6 is not"
1098 		    " handled");
1099 		return;
1100 	} else if (iap->ia_request->data_type != DHCP_TYPE_OPTION) {
1101 		dhcpmsg(MSG_DEBUG, "dhcp_smach_set_msg_reqhost: request type"
1102 		    " %d is not DHCP_TYPE_OPTION", iap->ia_request->data_type);
1103 		return;
1104 	}
1105 
1106 	if (iap->ia_request->buffer == NULL ||
1107 	    iap->ia_request->data_length <= DHCP_OPT_META_LEN) {
1108 		dhcpmsg(MSG_WARNING, "dhcp_smach_set_msg_reqhost:"
1109 		    " DHCP_TYPE_OPTION ia_request buffer is NULL (0) or"
1110 		    " short (1): %d",
1111 		    iap->ia_request->buffer == NULL ? 0 : 1);
1112 		return;
1113 	}
1114 
1115 	d4o = (DHCP_OPT *)iap->ia_request->buffer;
1116 	if (d4o->code != CD_HOSTNAME) {
1117 		dhcpmsg(MSG_DEBUG,
1118 		    "dhcp_smach_set_msg_reqhost: ignoring DHCPv4"
1119 		    " option %u", d4o->code);
1120 		return;
1121 	} else if (iap->ia_request->data_length - DHCP_OPT_META_LEN
1122 	    != d4o->len) {
1123 		dhcpmsg(MSG_WARNING, "dhcp_smach_set_msg_reqhost:"
1124 		    " unexpected DHCP_OPT buffer length %u for CD_HOSTNAME"
1125 		    " option length %u", iap->ia_request->data_length,
1126 		    d4o->len);
1127 		return;
1128 	}
1129 
1130 	entry = inittab_getbycode(ITAB_CAT_STANDARD, ITAB_CONS_INFO,
1131 	    CD_HOSTNAME);
1132 	if (entry == NULL) {
1133 		dhcpmsg(MSG_WARNING,
1134 		    "dhcp_smach_set_msg_reqhost: error getting"
1135 		    " ITAB_CAT_STANDARD ITAB_CONS_INFO"
1136 		    " CD_HOSTNAME entry");
1137 		return;
1138 	}
1139 
1140 	value = inittab_decode(entry, d4o->value, d4o->len,
1141 	    /* just_payload */ B_TRUE);
1142 	if (value == NULL) {
1143 		dhcpmsg(MSG_WARNING,
1144 		    "dhcp_smach_set_msg_reqhost: error decoding"
1145 		    " CD_HOSTNAME value from DHCP_OPT");
1146 	} else {
1147 		dhcpmsg(MSG_DEBUG,
1148 		    "dhcp_smach_set_msg_reqhost: host %s", value);
1149 		free(dsmp->dsm_msg_reqhost);
1150 		dsmp->dsm_msg_reqhost = value;
1151 	}
1152 	free(entry);
1153 }
1154 
1155 /*
1156  * dhcp_get_ack_or_state(): get a v4 option from the ACK or from the state
1157  * machine state for certain codes that are not ACKed (e.g., CD_CLIENT_ID)
1158  *
1159  *   input: dhcp_smach_t *: the state machine instance;
1160  *	    PKT_LIST *: the decoded DHCP IPC message;
1161  *	    uint_t: the DHCP client option code;
1162  *	    boolean_t *: a pointer to a value that will be set to B_TRUE if
1163  *	        the return value must be freed (or else set to B_FALSE);
1164  *  output: the option if found or else NULL.
1165  */
1166 
1167 static DHCP_OPT *
1168 dhcp_get_ack_or_state(const dhcp_smach_t *dsmp, const PKT_LIST *plp,
1169     uint_t codenum, boolean_t *did_alloc)
1170 {
1171 	DHCP_OPT *opt;
1172 
1173 	*did_alloc = B_FALSE;
1174 
1175 	if (codenum > DHCP_LAST_OPT)
1176 		return (NULL);
1177 
1178 	/* check the ACK first for all codes */
1179 	opt = plp->opts[codenum];
1180 	if (opt != NULL)
1181 		return (opt);
1182 
1183 	/* check the machine state also for certain codes */
1184 	switch (codenum) {
1185 	case CD_CLIENT_ID:
1186 		/*
1187 		 * CD_CLIENT_ID is not sent in an ACK, but it's possibly
1188 		 * available from the state machine data
1189 		 */
1190 
1191 		if (dsmp->dsm_cidlen > 0) {
1192 			if ((opt = malloc(dsmp->dsm_cidlen + DHCP_OPT_META_LEN))
1193 			    != NULL) {
1194 				*did_alloc = B_TRUE;
1195 				(void) encode_dhcp_opt(opt,
1196 				    B_FALSE /* is IPv6 */, CD_CLIENT_ID,
1197 				    dsmp->dsm_cid, dsmp->dsm_cidlen);
1198 			}
1199 		}
1200 		break;
1201 	default:
1202 		break;
1203 	}
1204 	return (opt);
1205 }
1206 
1207 /*
1208  * check_rtm_addr(): determine if routing socket message matches interface
1209  *		     address
1210  *
1211  *   input: const struct if_msghdr *: pointer to routing socket message
1212  *	    int: routing socket message length
1213  *	    boolean_t: set to B_TRUE if IPv6
1214  *	    const in6_addr_t *: pointer to IP address
1215  *  output: boolean_t: B_TRUE if address is a match
1216  */
1217 
1218 static boolean_t
1219 check_rtm_addr(const struct ifa_msghdr *ifam, int msglen, boolean_t isv6,
1220     const in6_addr_t *addr)
1221 {
1222 	const char *cp, *lim;
1223 	uint_t flag;
1224 	const struct sockaddr *sa;
1225 
1226 	if (!(ifam->ifam_addrs & RTA_IFA))
1227 		return (B_FALSE);
1228 
1229 	cp = (const char *)(ifam + 1);
1230 	lim = (const char *)ifam + msglen;
1231 	for (flag = 1; flag < RTA_IFA; flag <<= 1) {
1232 		if (ifam->ifam_addrs & flag) {
1233 			/* LINTED: alignment */
1234 			sa = (const struct sockaddr *)cp;
1235 			if ((const char *)(sa + 1) > lim)
1236 				return (B_FALSE);
1237 			switch (sa->sa_family) {
1238 			case AF_INET:
1239 				cp += sizeof (struct sockaddr_in);
1240 				break;
1241 			case AF_LINK:
1242 				cp += sizeof (struct sockaddr_dl);
1243 				break;
1244 			case AF_INET6:
1245 				cp += sizeof (struct sockaddr_in6);
1246 				break;
1247 			default:
1248 				cp += sizeof (struct sockaddr);
1249 				break;
1250 			}
1251 		}
1252 	}
1253 	if (isv6) {
1254 		const struct sockaddr_in6 *sin6;
1255 
1256 		/* LINTED: alignment */
1257 		sin6 = (const struct sockaddr_in6 *)cp;
1258 		if ((const char *)(sin6 + 1) > lim)
1259 			return (B_FALSE);
1260 		if (sin6->sin6_family != AF_INET6)
1261 			return (B_FALSE);
1262 		return (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, addr));
1263 	} else {
1264 		const struct sockaddr_in *sinp;
1265 		ipaddr_t v4addr;
1266 
1267 		/* LINTED: alignment */
1268 		sinp = (const struct sockaddr_in *)cp;
1269 		if ((const char *)(sinp + 1) > lim)
1270 			return (B_FALSE);
1271 		if (sinp->sin_family != AF_INET)
1272 			return (B_FALSE);
1273 		IN6_V4MAPPED_TO_IPADDR(addr, v4addr);
1274 		return (sinp->sin_addr.s_addr == v4addr);
1275 	}
1276 }
1277 
1278 /*
1279  * is_rtm_v6(): determine if routing socket message is IPv6
1280  *
1281  *   input: struct ifa_msghdr *: pointer to routing socket message
1282  *	    int: message length
1283  *  output: boolean_t
1284  */
1285 
1286 static boolean_t
1287 is_rtm_v6(const struct ifa_msghdr *ifam, int msglen)
1288 {
1289 	const char *cp, *lim;
1290 	uint_t flag;
1291 	const struct sockaddr *sa;
1292 
1293 	cp = (const char *)(ifam + 1);
1294 	lim = (const char *)ifam + msglen;
1295 	for (flag = ifam->ifam_addrs; flag != 0; flag &= flag - 1) {
1296 		/* LINTED: alignment */
1297 		sa = (const struct sockaddr *)cp;
1298 		if ((const char *)(sa + 1) > lim)
1299 			return (B_FALSE);
1300 		switch (sa->sa_family) {
1301 		case AF_INET:
1302 			return (B_FALSE);
1303 		case AF_LINK:
1304 			cp += sizeof (struct sockaddr_dl);
1305 			break;
1306 		case AF_INET6:
1307 			return (B_TRUE);
1308 		default:
1309 			cp += sizeof (struct sockaddr);
1310 			break;
1311 		}
1312 	}
1313 	return (B_FALSE);
1314 }
1315 
1316 /*
1317  * check_lif(): check the state of a given logical interface and its DHCP
1318  *		lease.  We've been told by the routing socket that the
1319  *		corresponding ifIndex has changed.  This may mean that DAD has
1320  *		completed or failed.
1321  *
1322  *   input: dhcp_lif_t *: pointer to the LIF
1323  *	    const struct ifa_msghdr *: routing socket message
1324  *	    int: size of routing socket message
1325  *  output: boolean_t: B_TRUE if DAD has completed on this interface
1326  */
1327 
1328 static boolean_t
1329 check_lif(dhcp_lif_t *lif, const struct ifa_msghdr *ifam, int msglen)
1330 {
1331 	boolean_t isv6, dad_wait, unplumb;
1332 	int fd;
1333 	struct lifreq lifr;
1334 
1335 	isv6 = lif->lif_pif->pif_isv6;
1336 	fd = isv6 ? v6_sock_fd : v4_sock_fd;
1337 
1338 	/*
1339 	 * Get the real (64 bit) logical interface flags.  Note that the
1340 	 * routing socket message has flags, but these are just the lower 32
1341 	 * bits.
1342 	 */
1343 	unplumb = B_FALSE;
1344 	(void) memset(&lifr, 0, sizeof (lifr));
1345 	(void) strlcpy(lifr.lifr_name, lif->lif_name, sizeof (lifr.lifr_name));
1346 	if (ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) {
1347 		/*
1348 		 * Failing to retrieve flags means that the interface is gone.
1349 		 * It hasn't failed to verify with DAD, but we still have to
1350 		 * give up on it.
1351 		 */
1352 		lifr.lifr_flags = 0;
1353 		if (errno == ENXIO) {
1354 			lif->lif_plumbed = B_FALSE;
1355 			dhcpmsg(MSG_INFO, "%s has been removed; abandoning",
1356 			    lif->lif_name);
1357 			if (!isv6)
1358 				discard_default_routes(lif->lif_smachs);
1359 		} else {
1360 			dhcpmsg(MSG_ERR,
1361 			    "unable to retrieve interface flags on %s",
1362 			    lif->lif_name);
1363 		}
1364 		unplumb = B_TRUE;
1365 	} else if (!check_rtm_addr(ifam, msglen, isv6, &lif->lif_v6addr)) {
1366 		/*
1367 		 * If the message is not about this logical interface,
1368 		 * then just ignore it.
1369 		 */
1370 		return (B_FALSE);
1371 	} else if (lifr.lifr_flags & IFF_DUPLICATE) {
1372 		dhcpmsg(MSG_ERROR, "interface %s has duplicate address",
1373 		    lif->lif_name);
1374 		lif_mark_decline(lif, "duplicate address");
1375 		close_ip_lif(lif);
1376 		(void) open_ip_lif(lif, INADDR_ANY, B_TRUE);
1377 	}
1378 
1379 	dad_wait = lif->lif_dad_wait;
1380 	if (dad_wait) {
1381 		dhcpmsg(MSG_VERBOSE, "check_lif: %s has finished DAD",
1382 		    lif->lif_name);
1383 		lif->lif_dad_wait = B_FALSE;
1384 	}
1385 
1386 	if (unplumb)
1387 		unplumb_lif(lif);
1388 
1389 	return (dad_wait);
1390 }
1391 
1392 /*
1393  * check_main_lif(): check the state of a main logical interface for a state
1394  *		     machine.  This is used only for DHCPv6.
1395  *
1396  *   input: dhcp_smach_t *: pointer to the state machine
1397  *	    const struct ifa_msghdr *: routing socket message
1398  *	    int: size of routing socket message
1399  *  output: boolean_t: B_TRUE if LIF is ok.
1400  */
1401 
1402 static boolean_t
1403 check_main_lif(dhcp_smach_t *dsmp, const struct ifa_msghdr *ifam, int msglen)
1404 {
1405 	dhcp_lif_t *lif = dsmp->dsm_lif;
1406 	struct lifreq lifr;
1407 
1408 	/*
1409 	 * Get the real (64 bit) logical interface flags.  Note that the
1410 	 * routing socket message has flags, but these are just the lower 32
1411 	 * bits.
1412 	 */
1413 	(void) memset(&lifr, 0, sizeof (lifr));
1414 	(void) strlcpy(lifr.lifr_name, lif->lif_name, sizeof (lifr.lifr_name));
1415 	if (ioctl(v6_sock_fd, SIOCGLIFFLAGS, &lifr) == -1) {
1416 		/*
1417 		 * Failing to retrieve flags means that the interface is gone.
1418 		 * Our state machine is now trash.
1419 		 */
1420 		if (errno == ENXIO) {
1421 			dhcpmsg(MSG_INFO, "%s has been removed; abandoning",
1422 			    lif->lif_name);
1423 		} else {
1424 			dhcpmsg(MSG_ERR,
1425 			    "unable to retrieve interface flags on %s",
1426 			    lif->lif_name);
1427 		}
1428 		return (B_FALSE);
1429 	} else if (!check_rtm_addr(ifam, msglen, B_TRUE, &lif->lif_v6addr)) {
1430 		/*
1431 		 * If the message is not about this logical interface,
1432 		 * then just ignore it.
1433 		 */
1434 		return (B_TRUE);
1435 	} else if (lifr.lifr_flags & IFF_DUPLICATE) {
1436 		dhcpmsg(MSG_ERROR, "interface %s has duplicate address",
1437 		    lif->lif_name);
1438 		return (B_FALSE);
1439 	} else {
1440 		return (B_TRUE);
1441 	}
1442 }
1443 
1444 /*
1445  * process_link_up_down(): check the state of a physical interface for up/down
1446  *			   transitions; must go through INIT_REBOOT state if
1447  *			   the link flaps.
1448  *
1449  *   input: dhcp_pif_t *: pointer to the physical interface to check
1450  *	    const struct if_msghdr *: routing socket message
1451  *  output: none
1452  */
1453 
1454 static void
1455 process_link_up_down(dhcp_pif_t *pif, const struct if_msghdr *ifm)
1456 {
1457 	struct lifreq lifr;
1458 	boolean_t isv6;
1459 	int fd;
1460 
1461 	/*
1462 	 * If the message implies no change of flags, then we're done; no need
1463 	 * to check further.  Note that if we have multiple state machines on a
1464 	 * single physical interface, this test keeps us from issuing an ioctl
1465 	 * for each one.
1466 	 */
1467 	if ((ifm->ifm_flags & IFF_RUNNING) && pif->pif_running ||
1468 	    !(ifm->ifm_flags & IFF_RUNNING) && !pif->pif_running)
1469 		return;
1470 
1471 	/*
1472 	 * We don't know what the real interface flags are, because the
1473 	 * if_index number is only 16 bits; we must go ask.
1474 	 */
1475 	isv6 = pif->pif_isv6;
1476 	fd = isv6 ? v6_sock_fd : v4_sock_fd;
1477 	(void) memset(&lifr, 0, sizeof (lifr));
1478 	(void) strlcpy(lifr.lifr_name, pif->pif_name, sizeof (lifr.lifr_name));
1479 
1480 	if (ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1 ||
1481 	    !(lifr.lifr_flags & IFF_RUNNING)) {
1482 		/*
1483 		 * If we've lost the interface or it has gone down, then
1484 		 * nothing special to do; just turn off the running flag.
1485 		 */
1486 		pif_status(pif, B_FALSE);
1487 	} else {
1488 		/*
1489 		 * Interface has come back up: go through verification process.
1490 		 */
1491 		pif_status(pif, B_TRUE);
1492 	}
1493 }
1494 
1495 /*
1496  * rtsock_event(): fetches routing socket messages and updates internal
1497  *		   interface state based on those messages.
1498  *
1499  *   input: iu_eh_t *: unused
1500  *	    int: the routing socket file descriptor
1501  *	    (other arguments unused)
1502  *  output: void
1503  */
1504 
1505 /* ARGSUSED */
1506 static void
1507 rtsock_event(iu_eh_t *ehp, int fd, short events, iu_event_id_t id, void *arg)
1508 {
1509 	dhcp_smach_t *dsmp, *dsmnext;
1510 	union {
1511 		struct ifa_msghdr ifam;
1512 		struct if_msghdr ifm;
1513 		char buf[1024];
1514 	} msg;
1515 	uint16_t ifindex;
1516 	int msglen;
1517 	boolean_t isv6;
1518 
1519 	if ((msglen = read(fd, &msg, sizeof (msg))) <= 0)
1520 		return;
1521 
1522 	/* Note that the routing socket interface index is just 16 bits */
1523 	if (msg.ifm.ifm_type == RTM_IFINFO) {
1524 		ifindex = msg.ifm.ifm_index;
1525 		isv6 = (msg.ifm.ifm_flags & IFF_IPV6) ? B_TRUE : B_FALSE;
1526 	} else if (msg.ifam.ifam_type == RTM_DELADDR ||
1527 	    msg.ifam.ifam_type == RTM_NEWADDR) {
1528 		ifindex = msg.ifam.ifam_index;
1529 		isv6 = is_rtm_v6(&msg.ifam, msglen);
1530 	} else {
1531 		return;
1532 	}
1533 
1534 	for (dsmp = lookup_smach_by_uindex(ifindex, NULL, isv6);
1535 	    dsmp != NULL; dsmp = dsmnext) {
1536 		DHCPSTATE oldstate;
1537 		boolean_t lif_finished;
1538 		boolean_t lease_removed;
1539 		dhcp_lease_t *dlp, *dlnext;
1540 
1541 		/*
1542 		 * Note that script_start can call dhcp_drop directly, and
1543 		 * that will do release_smach.
1544 		 */
1545 		dsmnext = lookup_smach_by_uindex(ifindex, dsmp, isv6);
1546 		oldstate = dsmp->dsm_state;
1547 
1548 		/*
1549 		 * Ignore state machines that are currently processing drop or
1550 		 * release; there is nothing more we can do for them.
1551 		 */
1552 		if (dsmp->dsm_droprelease)
1553 			continue;
1554 
1555 		/*
1556 		 * Look for link up/down notifications.  These occur on a
1557 		 * physical interface basis.
1558 		 */
1559 		if (msg.ifm.ifm_type == RTM_IFINFO) {
1560 			process_link_up_down(dsmp->dsm_lif->lif_pif, &msg.ifm);
1561 			continue;
1562 		}
1563 
1564 		/*
1565 		 * Since we cannot trust the flags reported by the routing
1566 		 * socket (they're just 32 bits -- and thus never include
1567 		 * IFF_DUPLICATE), and we can't trust the ifindex (it's only 16
1568 		 * bits and also doesn't reflect the alias in use), we get
1569 		 * flags on all matching interfaces, and go by that.
1570 		 */
1571 		lif_finished = B_FALSE;
1572 		lease_removed = B_FALSE;
1573 		for (dlp = dsmp->dsm_leases; dlp != NULL; dlp = dlnext) {
1574 			dhcp_lif_t *lif, *lifnext;
1575 			uint_t nlifs = dlp->dl_nlifs;
1576 
1577 			dlnext = dlp->dl_next;
1578 			for (lif = dlp->dl_lifs; lif != NULL && nlifs > 0;
1579 			    lif = lifnext, nlifs--) {
1580 				lifnext = lif->lif_next;
1581 				if (check_lif(lif, &msg.ifam, msglen)) {
1582 					dsmp->dsm_lif_wait--;
1583 					lif_finished = B_TRUE;
1584 				}
1585 			}
1586 			if (dlp->dl_nlifs == 0) {
1587 				remove_lease(dlp);
1588 				lease_removed = B_TRUE;
1589 			}
1590 		}
1591 
1592 		if ((isv6 && !check_main_lif(dsmp, &msg.ifam, msglen)) ||
1593 		    (!isv6 && !verify_lif(dsmp->dsm_lif))) {
1594 			finished_smach(dsmp, DHCP_IPC_E_INVIF);
1595 			continue;
1596 		}
1597 
1598 		/*
1599 		 * Ignore this state machine if nothing interesting has
1600 		 * happened.
1601 		 */
1602 		if (!lif_finished && dsmp->dsm_lif_down == 0 &&
1603 		    (dsmp->dsm_leases != NULL || !lease_removed))
1604 			continue;
1605 
1606 		/*
1607 		 * If we're still waiting for DAD to complete on some of the
1608 		 * configured LIFs, then don't send a response.
1609 		 */
1610 		if (dsmp->dsm_lif_wait != 0) {
1611 			dhcpmsg(MSG_VERBOSE, "rtsock_event: %s still has %d "
1612 			    "LIFs waiting on DAD", dsmp->dsm_name,
1613 			    dsmp->dsm_lif_wait);
1614 			continue;
1615 		}
1616 
1617 		/*
1618 		 * If we have some failed LIFs, then handle them now.  We'll
1619 		 * remove them from the list.  Any leases that become empty are
1620 		 * also removed as part of the decline-generation process.
1621 		 */
1622 		if (dsmp->dsm_lif_down != 0)
1623 			send_declines(dsmp);
1624 
1625 		if (dsmp->dsm_leases == NULL) {
1626 			dsmp->dsm_bad_offers++;
1627 			/*
1628 			 * For DHCPv6, we'll process the restart once we're
1629 			 * done sending Decline messages, because these are
1630 			 * supposed to be acknowledged.  With DHCPv4, there's
1631 			 * no acknowledgment for a DECLINE, so after sending
1632 			 * it, we just restart right away.
1633 			 */
1634 			if (!dsmp->dsm_isv6) {
1635 				dhcpmsg(MSG_VERBOSE, "rtsock_event: %s has no "
1636 				    "LIFs left", dsmp->dsm_name);
1637 				dhcp_restart(dsmp);
1638 			}
1639 		} else {
1640 			/*
1641 			 * If we're now up on at least some of the leases and
1642 			 * we were waiting for that, then kick off the rest of
1643 			 * configuration.  Lease validation and DAD are done.
1644 			 */
1645 			dhcpmsg(MSG_VERBOSE, "rtsock_event: all LIFs verified "
1646 			    "on %s in %s state", dsmp->dsm_name,
1647 			    dhcp_state_to_string(oldstate));
1648 			if (oldstate == PRE_BOUND ||
1649 			    oldstate == ADOPTING)
1650 				dhcp_bound_complete(dsmp);
1651 			if (oldstate == ADOPTING)
1652 				dhcp_adopt_complete(dsmp);
1653 		}
1654 	}
1655 }
1656 
1657 /*
1658  * check_cmd_allowed(): check whether the requested command is allowed in the
1659  *			state specified.
1660  *
1661  *   input: DHCPSTATE: current state
1662  *	    dhcp_ipc_type_t: requested command
1663  *  output: boolean_t: B_TRUE if command is allowed in this state
1664  */
1665 
1666 boolean_t
1667 check_cmd_allowed(DHCPSTATE state, dhcp_ipc_type_t cmd)
1668 {
1669 	return (ipc_cmd_allowed[state][cmd] != 0);
1670 }
1671 
1672 static boolean_t
1673 is_iscsi_active(void)
1674 {
1675 	int fd;
1676 	int active = 0;
1677 
1678 	if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) != -1) {
1679 		if (ioctl(fd, ISCSI_IS_ACTIVE, &active) != 0)
1680 			active = 0;
1681 		(void) close(fd);
1682 	}
1683 
1684 	return (active != 0);
1685 }
1686