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 /*
220a3e1f6cSVasumathi Sundaram * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
23b31320a7SChris Fraire * Copyright (c) 2016-2017, Chris Fraire <cfraire@me.com>.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
283644994cSmeem #include <assert.h>
297c478bd9Sstevel@tonic-gate #include <errno.h>
307c478bd9Sstevel@tonic-gate #include <locale.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <signal.h>
347c478bd9Sstevel@tonic-gate #include <stdio.h>
35004388ebScasper #include <stdio_ext.h>
367c478bd9Sstevel@tonic-gate #include <dhcp_hostconf.h>
377c478bd9Sstevel@tonic-gate #include <dhcpagent_ipc.h>
38d04ccbb3Scarlsonj #include <dhcpagent_util.h>
397c478bd9Sstevel@tonic-gate #include <dhcpmsg.h>
40b31320a7SChris Fraire #include <dhcp_inittab.h>
41b31320a7SChris Fraire #include <dhcp_symbol.h>
427c478bd9Sstevel@tonic-gate #include <netinet/dhcp.h>
4369bb4bb4Scarlsonj #include <net/route.h>
4469bb4bb4Scarlsonj #include <sys/sockio.h>
456cefaae1SJack Meng #include <sys/stat.h>
466cefaae1SJack Meng #include <stropts.h>
476cefaae1SJack Meng #include <fcntl.h>
486cefaae1SJack Meng #include <sys/scsi/adapters/iscsi_if.h>
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate #include "async.h"
517c478bd9Sstevel@tonic-gate #include "agent.h"
527c478bd9Sstevel@tonic-gate #include "script_handler.h"
537c478bd9Sstevel@tonic-gate #include "util.h"
547c478bd9Sstevel@tonic-gate #include "class_id.h"
557c478bd9Sstevel@tonic-gate #include "states.h"
567c478bd9Sstevel@tonic-gate #include "packet.h"
57d04ccbb3Scarlsonj #include "interface.h"
58d04ccbb3Scarlsonj #include "defaults.h"
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN
617c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
627c478bd9Sstevel@tonic-gate #endif
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate iu_timer_id_t inactivity_id;
657c478bd9Sstevel@tonic-gate int class_id_len = 0;
667c478bd9Sstevel@tonic-gate char *class_id;
677c478bd9Sstevel@tonic-gate iu_eh_t *eh;
687c478bd9Sstevel@tonic-gate iu_tq_t *tq;
697c478bd9Sstevel@tonic-gate pid_t grandparent;
7069bb4bb4Scarlsonj int rtsock_fd;
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate static boolean_t shutdown_started = B_FALSE;
737c478bd9Sstevel@tonic-gate static boolean_t do_adopt = B_FALSE;
747c478bd9Sstevel@tonic-gate static unsigned int debug_level = 0;
7569bb4bb4Scarlsonj static iu_eh_callback_t accept_event, ipc_event, rtsock_event;
76b31320a7SChris Fraire static void dhcp_smach_set_msg_reqhost(dhcp_smach_t *dsmp,
77b31320a7SChris Fraire ipc_action_t *iap);
78b31320a7SChris Fraire static DHCP_OPT * dhcp_get_ack_or_state(const dhcp_smach_t *dsmp,
79b31320a7SChris Fraire const PKT_LIST *plp, uint_t codenum, boolean_t *did_alloc);
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate * The ipc_cmd_allowed[] table indicates which IPC commands are allowed in
837c478bd9Sstevel@tonic-gate * which states; a non-zero value indicates the command is permitted.
847c478bd9Sstevel@tonic-gate *
85d04ccbb3Scarlsonj * START is permitted if the state machine is fresh, or if we are in the
86d04ccbb3Scarlsonj * process of trying to obtain a lease (as a convenience to save the
87d04ccbb3Scarlsonj * administrator from having to do an explicit DROP). EXTEND, RELEASE, and
88d04ccbb3Scarlsonj * GET_TAG require a lease to be obtained in order to make sense. INFORM is
89d04ccbb3Scarlsonj * permitted if the interface is fresh or has an INFORM in progress or
90d04ccbb3Scarlsonj * previously done on it -- otherwise a DROP or RELEASE is first required.
91d04ccbb3Scarlsonj * PING and STATUS always make sense and thus are always permitted, as is DROP
92d04ccbb3Scarlsonj * in order to permit the administrator to always bail out.
937c478bd9Sstevel@tonic-gate */
947c478bd9Sstevel@tonic-gate static int ipc_cmd_allowed[DHCP_NSTATES][DHCP_NIPC] = {
957c478bd9Sstevel@tonic-gate /* D E P R S S I G */
967c478bd9Sstevel@tonic-gate /* R X I E T T N E */
977c478bd9Sstevel@tonic-gate /* O T N L A A F T */
987c478bd9Sstevel@tonic-gate /* P E G E R T O _ */
997c478bd9Sstevel@tonic-gate /* . N . A T U R T */
1007c478bd9Sstevel@tonic-gate /* . D . S . S M A */
1017c478bd9Sstevel@tonic-gate /* . . . E . . . G */
1027c478bd9Sstevel@tonic-gate /* INIT */ { 1, 0, 1, 0, 1, 1, 1, 0 },
1037c478bd9Sstevel@tonic-gate /* SELECTING */ { 1, 0, 1, 0, 1, 1, 0, 0 },
1047c478bd9Sstevel@tonic-gate /* REQUESTING */ { 1, 0, 1, 0, 1, 1, 0, 0 },
10569bb4bb4Scarlsonj /* PRE_BOUND */ { 1, 1, 1, 1, 0, 1, 0, 1 },
1067c478bd9Sstevel@tonic-gate /* BOUND */ { 1, 1, 1, 1, 0, 1, 0, 1 },
1077c478bd9Sstevel@tonic-gate /* RENEWING */ { 1, 1, 1, 1, 0, 1, 0, 1 },
1087c478bd9Sstevel@tonic-gate /* REBINDING */ { 1, 1, 1, 1, 0, 1, 0, 1 },
109d04ccbb3Scarlsonj /* INFORMATION */ { 1, 0, 1, 0, 1, 1, 1, 1 },
110d04ccbb3Scarlsonj /* INIT_REBOOT */ { 1, 0, 1, 1, 1, 1, 0, 0 },
111d04ccbb3Scarlsonj /* ADOPTING */ { 1, 0, 1, 1, 0, 1, 0, 0 },
112d04ccbb3Scarlsonj /* INFORM_SENT */ { 1, 0, 1, 0, 1, 1, 1, 0 },
113d04ccbb3Scarlsonj /* DECLINING */ { 1, 1, 1, 1, 0, 1, 0, 1 },
114d04ccbb3Scarlsonj /* RELEASING */ { 1, 0, 1, 0, 0, 1, 0, 1 },
115d04ccbb3Scarlsonj };
116d04ccbb3Scarlsonj
117d04ccbb3Scarlsonj #define CMD_ISPRIV 0x1 /* Command requires privileges */
118d04ccbb3Scarlsonj #define CMD_CREATE 0x2 /* Command creates an interface */
119d04ccbb3Scarlsonj #define CMD_BOOTP 0x4 /* Command is valid with BOOTP */
120d04ccbb3Scarlsonj #define CMD_IMMED 0x8 /* Reply is immediate (no BUSY state) */
121d04ccbb3Scarlsonj
122d04ccbb3Scarlsonj static uint_t ipc_cmd_flags[DHCP_NIPC] = {
123d04ccbb3Scarlsonj /* DHCP_DROP */ CMD_ISPRIV|CMD_BOOTP,
124d04ccbb3Scarlsonj /* DHCP_EXTEND */ CMD_ISPRIV,
125d04ccbb3Scarlsonj /* DHCP_PING */ CMD_BOOTP|CMD_IMMED,
126d04ccbb3Scarlsonj /* DHCP_RELEASE */ CMD_ISPRIV,
127d04ccbb3Scarlsonj /* DHCP_START */ CMD_CREATE|CMD_ISPRIV|CMD_BOOTP,
128d04ccbb3Scarlsonj /* DHCP_STATUS */ CMD_BOOTP|CMD_IMMED,
129d04ccbb3Scarlsonj /* DHCP_INFORM */ CMD_CREATE|CMD_ISPRIV,
130d04ccbb3Scarlsonj /* DHCP_GET_TAG */ CMD_BOOTP|CMD_IMMED
1317c478bd9Sstevel@tonic-gate };
1327c478bd9Sstevel@tonic-gate
1333644994cSmeem static boolean_t is_iscsi_active(void);
1346cefaae1SJack Meng
1357c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)1367c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate boolean_t is_daemon = B_TRUE;
139d04ccbb3Scarlsonj boolean_t is_verbose;
1407c478bd9Sstevel@tonic-gate int ipc_fd;
1417c478bd9Sstevel@tonic-gate int c;
142e11c3f44Smeem int aware = RTAW_UNDER_IPMP;
1437c478bd9Sstevel@tonic-gate struct rlimit rl;
1447c478bd9Sstevel@tonic-gate
145d04ccbb3Scarlsonj debug_level = df_get_int("", B_FALSE, DF_DEBUG_LEVEL);
146d04ccbb3Scarlsonj is_verbose = df_get_bool("", B_FALSE, DF_VERBOSE);
147d04ccbb3Scarlsonj
1487c478bd9Sstevel@tonic-gate /*
1497c478bd9Sstevel@tonic-gate * -l is ignored for compatibility with old agent.
1507c478bd9Sstevel@tonic-gate */
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "vd:l:fa")) != EOF) {
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate switch (c) {
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate case 'a':
1577c478bd9Sstevel@tonic-gate do_adopt = B_TRUE;
1587c478bd9Sstevel@tonic-gate grandparent = getpid();
1597c478bd9Sstevel@tonic-gate break;
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate case 'd':
1627c478bd9Sstevel@tonic-gate debug_level = strtoul(optarg, NULL, 0);
1637c478bd9Sstevel@tonic-gate break;
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate case 'f':
1667c478bd9Sstevel@tonic-gate is_daemon = B_FALSE;
1677c478bd9Sstevel@tonic-gate break;
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate case 'v':
1707c478bd9Sstevel@tonic-gate is_verbose = B_TRUE;
1717c478bd9Sstevel@tonic-gate break;
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate case '?':
1747c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "usage: %s [-a] [-d n] [-f] [-v]"
1757c478bd9Sstevel@tonic-gate "\n", argv[0]);
1767c478bd9Sstevel@tonic-gate return (EXIT_FAILURE);
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate default:
1797c478bd9Sstevel@tonic-gate break;
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate
1837c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1847c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
1857c478bd9Sstevel@tonic-gate
1867c478bd9Sstevel@tonic-gate if (geteuid() != 0) {
1877c478bd9Sstevel@tonic-gate dhcpmsg_init(argv[0], B_FALSE, is_verbose, debug_level);
1887c478bd9Sstevel@tonic-gate dhcpmsg(MSG_ERROR, "must be super-user");
1897c478bd9Sstevel@tonic-gate dhcpmsg_fini();
1907c478bd9Sstevel@tonic-gate return (EXIT_FAILURE);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate if (is_daemon && daemonize() == 0) {
1947c478bd9Sstevel@tonic-gate dhcpmsg_init(argv[0], B_FALSE, is_verbose, debug_level);
1957c478bd9Sstevel@tonic-gate dhcpmsg(MSG_ERR, "cannot become daemon, exiting");
1967c478bd9Sstevel@tonic-gate dhcpmsg_fini();
1977c478bd9Sstevel@tonic-gate return (EXIT_FAILURE);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate
200d04ccbb3Scarlsonj /*
201d04ccbb3Scarlsonj * Seed the random number generator, since we're going to need it
202d04ccbb3Scarlsonj * to set transaction id's and for exponential backoff.
203d04ccbb3Scarlsonj */
204d04ccbb3Scarlsonj srand48(gethrtime() ^ gethostid() ^ getpid());
205d04ccbb3Scarlsonj
2067c478bd9Sstevel@tonic-gate dhcpmsg_init(argv[0], is_daemon, is_verbose, debug_level);
2077c478bd9Sstevel@tonic-gate (void) atexit(dhcpmsg_fini);
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate tq = iu_tq_create();
2107c478bd9Sstevel@tonic-gate eh = iu_eh_create();
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate if (eh == NULL || tq == NULL) {
2137c478bd9Sstevel@tonic-gate errno = ENOMEM;
2147c478bd9Sstevel@tonic-gate dhcpmsg(MSG_ERR, "cannot create timer queue or event handler");
2157c478bd9Sstevel@tonic-gate return (EXIT_FAILURE);
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate /*
2197c478bd9Sstevel@tonic-gate * ignore most signals that could be reasonably generated.
2207c478bd9Sstevel@tonic-gate */
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate (void) signal(SIGTERM, graceful_shutdown);
2237c478bd9Sstevel@tonic-gate (void) signal(SIGQUIT, graceful_shutdown);
2247c478bd9Sstevel@tonic-gate (void) signal(SIGPIPE, SIG_IGN);
2257c478bd9Sstevel@tonic-gate (void) signal(SIGUSR1, SIG_IGN);
2267c478bd9Sstevel@tonic-gate (void) signal(SIGUSR2, SIG_IGN);
2277c478bd9Sstevel@tonic-gate (void) signal(SIGINT, SIG_IGN);
2287c478bd9Sstevel@tonic-gate (void) signal(SIGHUP, SIG_IGN);
2297c478bd9Sstevel@tonic-gate (void) signal(SIGCHLD, SIG_IGN);
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate * upon SIGTHAW we need to refresh any non-infinite leases.
2337c478bd9Sstevel@tonic-gate */
2347c478bd9Sstevel@tonic-gate
235d04ccbb3Scarlsonj (void) iu_eh_register_signal(eh, SIGTHAW, refresh_smachs, NULL);
2367c478bd9Sstevel@tonic-gate
2377c478bd9Sstevel@tonic-gate class_id = get_class_id();
2387c478bd9Sstevel@tonic-gate if (class_id != NULL)
2397c478bd9Sstevel@tonic-gate class_id_len = strlen(class_id);
2407c478bd9Sstevel@tonic-gate else
2417c478bd9Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "get_class_id failed, continuing "
2427c478bd9Sstevel@tonic-gate "with no vendor class id");
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate /*
2457c478bd9Sstevel@tonic-gate * the inactivity timer is enabled any time there are no
2467c478bd9Sstevel@tonic-gate * interfaces under DHCP control. if DHCP_INACTIVITY_WAIT
2477c478bd9Sstevel@tonic-gate * seconds transpire without an interface under DHCP control,
2487c478bd9Sstevel@tonic-gate * the agent shuts down.
2497c478bd9Sstevel@tonic-gate */
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate inactivity_id = iu_schedule_timer(tq, DHCP_INACTIVITY_WAIT,
2527c478bd9Sstevel@tonic-gate inactivity_shutdown, NULL);
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate /*
2557c478bd9Sstevel@tonic-gate * max out the number available descriptors, just in case..
2567c478bd9Sstevel@tonic-gate */
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate rl.rlim_cur = RLIM_INFINITY;
2597c478bd9Sstevel@tonic-gate rl.rlim_max = RLIM_INFINITY;
2607c478bd9Sstevel@tonic-gate if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
2617c478bd9Sstevel@tonic-gate dhcpmsg(MSG_ERR, "setrlimit failed");
2627c478bd9Sstevel@tonic-gate
263004388ebScasper (void) enable_extended_FILE_stdio(-1, -1);
264004388ebScasper
265d04ccbb3Scarlsonj /*
266d04ccbb3Scarlsonj * Create and bind default IP sockets used to control interfaces and to
267d04ccbb3Scarlsonj * catch stray packets.
268d04ccbb3Scarlsonj */
269d04ccbb3Scarlsonj
270d04ccbb3Scarlsonj if (!dhcp_ip_default())
271d04ccbb3Scarlsonj return (EXIT_FAILURE);
272d04ccbb3Scarlsonj
2737c478bd9Sstevel@tonic-gate /*
2747c478bd9Sstevel@tonic-gate * create the ipc channel that the agent will listen for
2757c478bd9Sstevel@tonic-gate * requests on, and register it with the event handler so that
2767c478bd9Sstevel@tonic-gate * `accept_event' will be called back.
2777c478bd9Sstevel@tonic-gate */
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate switch (dhcp_ipc_init(&ipc_fd)) {
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate case 0:
2827c478bd9Sstevel@tonic-gate break;
2837c478bd9Sstevel@tonic-gate
2847c478bd9Sstevel@tonic-gate case DHCP_IPC_E_BIND:
2857c478bd9Sstevel@tonic-gate dhcpmsg(MSG_ERROR, "dhcp_ipc_init: cannot bind to port "
2867c478bd9Sstevel@tonic-gate "%i (agent already running?)", IPPORT_DHCPAGENT);
2877c478bd9Sstevel@tonic-gate return (EXIT_FAILURE);
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate default:
2907c478bd9Sstevel@tonic-gate dhcpmsg(MSG_ERROR, "dhcp_ipc_init failed");
2917c478bd9Sstevel@tonic-gate return (EXIT_FAILURE);
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate
2947c478bd9Sstevel@tonic-gate if (iu_register_event(eh, ipc_fd, POLLIN, accept_event, 0) == -1) {
2957c478bd9Sstevel@tonic-gate dhcpmsg(MSG_ERR, "cannot register ipc fd for messages");
2967c478bd9Sstevel@tonic-gate return (EXIT_FAILURE);
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate
29969bb4bb4Scarlsonj /*
30069bb4bb4Scarlsonj * Create the global routing socket. This is used for monitoring
30169bb4bb4Scarlsonj * interface transitions, so that we learn about the kernel's Duplicate
30269bb4bb4Scarlsonj * Address Detection status, and for inserting and removing default
303d04ccbb3Scarlsonj * routes as learned from DHCP servers. Both v4 and v6 are handed
304d04ccbb3Scarlsonj * with this one socket.
30569bb4bb4Scarlsonj */
306d04ccbb3Scarlsonj rtsock_fd = socket(PF_ROUTE, SOCK_RAW, 0);
30769bb4bb4Scarlsonj if (rtsock_fd == -1) {
30869bb4bb4Scarlsonj dhcpmsg(MSG_ERR, "cannot open routing socket");
30969bb4bb4Scarlsonj return (EXIT_FAILURE);
31069bb4bb4Scarlsonj }
311e11c3f44Smeem
312e11c3f44Smeem /*
313e11c3f44Smeem * We're IPMP-aware and can manage IPMP test addresses, so issue
314e11c3f44Smeem * RT_AWARE to get routing socket messages for interfaces under IPMP.
315e11c3f44Smeem */
316e11c3f44Smeem if (setsockopt(rtsock_fd, SOL_ROUTE, RT_AWARE, &aware,
317e11c3f44Smeem sizeof (aware)) == -1) {
318e11c3f44Smeem dhcpmsg(MSG_ERR, "cannot set RT_AWARE on routing socket");
319e11c3f44Smeem return (EXIT_FAILURE);
320e11c3f44Smeem }
321e11c3f44Smeem
32269bb4bb4Scarlsonj if (iu_register_event(eh, rtsock_fd, POLLIN, rtsock_event, 0) == -1) {
32369bb4bb4Scarlsonj dhcpmsg(MSG_ERR, "cannot register routing socket for messages");
32469bb4bb4Scarlsonj return (EXIT_FAILURE);
32569bb4bb4Scarlsonj }
32669bb4bb4Scarlsonj
3277c478bd9Sstevel@tonic-gate /*
3287c478bd9Sstevel@tonic-gate * if the -a (adopt) option was specified, try to adopt the
329dc041e83Scarlsonj * kernel-managed interface before we start.
3307c478bd9Sstevel@tonic-gate */
3317c478bd9Sstevel@tonic-gate
332d04ccbb3Scarlsonj if (do_adopt && !dhcp_adopt())
333dc041e83Scarlsonj return (EXIT_FAILURE);
3347c478bd9Sstevel@tonic-gate
335d04ccbb3Scarlsonj /*
336d04ccbb3Scarlsonj * For DHCPv6, we own all of the interfaces marked DHCPRUNNING. As
337d04ccbb3Scarlsonj * we're starting operation here, if there are any of those interfaces
338d04ccbb3Scarlsonj * lingering around, they're strays, and need to be removed.
339d04ccbb3Scarlsonj *
340d04ccbb3Scarlsonj * It might be nice to save these addresses off somewhere -- for both
341d04ccbb3Scarlsonj * v4 and v6 -- and use them as hints for later negotiation.
342d04ccbb3Scarlsonj */
343d04ccbb3Scarlsonj remove_v6_strays();
344d04ccbb3Scarlsonj
3457c478bd9Sstevel@tonic-gate /*
3467c478bd9Sstevel@tonic-gate * enter the main event loop; this is where all the real work
3477c478bd9Sstevel@tonic-gate * takes place (through registering events and scheduling timers).
3487c478bd9Sstevel@tonic-gate * this function only returns when the agent is shutting down.
3497c478bd9Sstevel@tonic-gate */
3507c478bd9Sstevel@tonic-gate
3517c478bd9Sstevel@tonic-gate switch (iu_handle_events(eh, tq)) {
3527c478bd9Sstevel@tonic-gate
3537c478bd9Sstevel@tonic-gate case -1:
3547c478bd9Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "iu_handle_events exited abnormally");
3557c478bd9Sstevel@tonic-gate break;
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate case DHCP_REASON_INACTIVITY:
3587c478bd9Sstevel@tonic-gate dhcpmsg(MSG_INFO, "no interfaces to manage, shutting down...");
3597c478bd9Sstevel@tonic-gate break;
3607c478bd9Sstevel@tonic-gate
3617c478bd9Sstevel@tonic-gate case DHCP_REASON_TERMINATE:
3627c478bd9Sstevel@tonic-gate dhcpmsg(MSG_INFO, "received SIGTERM, shutting down...");
3637c478bd9Sstevel@tonic-gate break;
3647c478bd9Sstevel@tonic-gate
3657c478bd9Sstevel@tonic-gate case DHCP_REASON_SIGNAL:
3667c478bd9Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "received unexpected signal, shutting "
3677c478bd9Sstevel@tonic-gate "down...");
3687c478bd9Sstevel@tonic-gate break;
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate
3717c478bd9Sstevel@tonic-gate (void) iu_eh_unregister_signal(eh, SIGTHAW, NULL);
3727c478bd9Sstevel@tonic-gate
3737c478bd9Sstevel@tonic-gate iu_eh_destroy(eh);
3747c478bd9Sstevel@tonic-gate iu_tq_destroy(tq);
3757c478bd9Sstevel@tonic-gate
3767c478bd9Sstevel@tonic-gate return (EXIT_SUCCESS);
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate
3797c478bd9Sstevel@tonic-gate /*
3807c478bd9Sstevel@tonic-gate * drain_script(): event loop callback during shutdown
3817c478bd9Sstevel@tonic-gate *
3827c478bd9Sstevel@tonic-gate * input: eh_t *: unused
3837c478bd9Sstevel@tonic-gate * void *: unused
3847c478bd9Sstevel@tonic-gate * output: boolean_t: B_TRUE if event loop should exit; B_FALSE otherwise
3857c478bd9Sstevel@tonic-gate */
3867c478bd9Sstevel@tonic-gate
3877c478bd9Sstevel@tonic-gate /* ARGSUSED */
3887c478bd9Sstevel@tonic-gate boolean_t
drain_script(iu_eh_t * ehp,void * arg)3897c478bd9Sstevel@tonic-gate drain_script(iu_eh_t *ehp, void *arg)
3907c478bd9Sstevel@tonic-gate {
3917c478bd9Sstevel@tonic-gate if (shutdown_started == B_FALSE) {
3927c478bd9Sstevel@tonic-gate shutdown_started = B_TRUE;
3936cefaae1SJack Meng /*
3946cefaae1SJack Meng * Check if the system is diskless client and/or
3956cefaae1SJack Meng * there are active iSCSI sessions
3966cefaae1SJack Meng *
3976cefaae1SJack Meng * Do not drop the lease, or the system will be
3986cefaae1SJack Meng * unable to sync(dump) through nfs/iSCSI driver
3996cefaae1SJack Meng */
4006cefaae1SJack Meng if (!do_adopt && !is_iscsi_active()) {
401d04ccbb3Scarlsonj nuke_smach_list();
4026cefaae1SJack Meng }
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate return (script_count == 0);
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate
4077c478bd9Sstevel@tonic-gate /*
4087c478bd9Sstevel@tonic-gate * accept_event(): accepts a new connection on the ipc socket and registers
4097c478bd9Sstevel@tonic-gate * to receive its messages with the event handler
4107c478bd9Sstevel@tonic-gate *
4117c478bd9Sstevel@tonic-gate * input: iu_eh_t *: unused
4127c478bd9Sstevel@tonic-gate * int: the file descriptor in the iu_eh_t * the connection came in on
4137c478bd9Sstevel@tonic-gate * (other arguments unused)
4147c478bd9Sstevel@tonic-gate * output: void
4157c478bd9Sstevel@tonic-gate */
4167c478bd9Sstevel@tonic-gate
4177c478bd9Sstevel@tonic-gate /* ARGSUSED */
4187c478bd9Sstevel@tonic-gate static void
accept_event(iu_eh_t * ehp,int fd,short events,iu_event_id_t id,void * arg)4197c478bd9Sstevel@tonic-gate accept_event(iu_eh_t *ehp, int fd, short events, iu_event_id_t id, void *arg)
4207c478bd9Sstevel@tonic-gate {
4217c478bd9Sstevel@tonic-gate int client_fd;
4227c478bd9Sstevel@tonic-gate int is_priv;
4237c478bd9Sstevel@tonic-gate
4247c478bd9Sstevel@tonic-gate if (dhcp_ipc_accept(fd, &client_fd, &is_priv) != 0) {
4257c478bd9Sstevel@tonic-gate dhcpmsg(MSG_ERR, "accept_event: accept on ipc socket");
4267c478bd9Sstevel@tonic-gate return;
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate
4297c478bd9Sstevel@tonic-gate if (iu_register_event(eh, client_fd, POLLIN, ipc_event,
4307c478bd9Sstevel@tonic-gate (void *)is_priv) == -1) {
4317c478bd9Sstevel@tonic-gate dhcpmsg(MSG_ERROR, "accept_event: cannot register ipc socket "
4327c478bd9Sstevel@tonic-gate "for callback");
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate
4367c478bd9Sstevel@tonic-gate /*
4377c478bd9Sstevel@tonic-gate * ipc_event(): processes incoming ipc requests
4387c478bd9Sstevel@tonic-gate *
4397c478bd9Sstevel@tonic-gate * input: iu_eh_t *: unused
4407c478bd9Sstevel@tonic-gate * int: the file descriptor in the iu_eh_t * the request came in on
4417c478bd9Sstevel@tonic-gate * short: unused
442d04ccbb3Scarlsonj * iu_event_id_t: event ID
4437c478bd9Sstevel@tonic-gate * void *: indicates whether the request is from a privileged client
4447c478bd9Sstevel@tonic-gate * output: void
4457c478bd9Sstevel@tonic-gate */
4467c478bd9Sstevel@tonic-gate
4477c478bd9Sstevel@tonic-gate /* ARGSUSED */
4487c478bd9Sstevel@tonic-gate static void
ipc_event(iu_eh_t * ehp,int fd,short events,iu_event_id_t id,void * arg)4497c478bd9Sstevel@tonic-gate ipc_event(iu_eh_t *ehp, int fd, short events, iu_event_id_t id, void *arg)
4507c478bd9Sstevel@tonic-gate {
451d04ccbb3Scarlsonj ipc_action_t ia, *iap;
452d04ccbb3Scarlsonj dhcp_smach_t *dsmp;
4537c478bd9Sstevel@tonic-gate int error, is_priv = (int)arg;
454d04ccbb3Scarlsonj const char *ifname;
455d04ccbb3Scarlsonj boolean_t isv6;
456dc918d99Smeem boolean_t dsm_created = B_FALSE;
457d04ccbb3Scarlsonj
458d04ccbb3Scarlsonj ipc_action_init(&ia);
459d04ccbb3Scarlsonj error = dhcp_ipc_recv_request(fd, &ia.ia_request,
460d04ccbb3Scarlsonj DHCP_IPC_REQUEST_WAIT);
461d04ccbb3Scarlsonj if (error != DHCP_IPC_SUCCESS) {
462d04ccbb3Scarlsonj if (error != DHCP_IPC_E_EOF) {
463d04ccbb3Scarlsonj dhcpmsg(MSG_ERROR,
464d04ccbb3Scarlsonj "ipc_event: dhcp_ipc_recv_request failed: %s",
465d04ccbb3Scarlsonj dhcp_ipc_strerror(error));
466d04ccbb3Scarlsonj } else {
467d04ccbb3Scarlsonj dhcpmsg(MSG_DEBUG, "ipc_event: connection closed");
468d04ccbb3Scarlsonj }
469d04ccbb3Scarlsonj if ((dsmp = lookup_smach_by_event(id)) != NULL) {
470d04ccbb3Scarlsonj ipc_action_finish(dsmp, error);
471d04ccbb3Scarlsonj } else {
472d04ccbb3Scarlsonj (void) iu_unregister_event(eh, id, NULL);
473d04ccbb3Scarlsonj (void) dhcp_ipc_close(fd);
474d04ccbb3Scarlsonj }
4757c478bd9Sstevel@tonic-gate return;
4767c478bd9Sstevel@tonic-gate }
4777c478bd9Sstevel@tonic-gate
478d04ccbb3Scarlsonj /* Fill in temporary ipc_action structure for utility functions */
479d04ccbb3Scarlsonj ia.ia_cmd = DHCP_IPC_CMD(ia.ia_request->message_type);
480d04ccbb3Scarlsonj ia.ia_fd = fd;
481d04ccbb3Scarlsonj ia.ia_eid = id;
482d04ccbb3Scarlsonj
483d04ccbb3Scarlsonj if (ia.ia_cmd >= DHCP_NIPC) {
484d04ccbb3Scarlsonj dhcpmsg(MSG_ERROR,
485d04ccbb3Scarlsonj "ipc_event: invalid command (%s) attempted on %s",
486d04ccbb3Scarlsonj dhcp_ipc_type_to_string(ia.ia_cmd), ia.ia_request->ifname);
487d04ccbb3Scarlsonj send_error_reply(&ia, DHCP_IPC_E_CMD_UNKNOWN);
4887c478bd9Sstevel@tonic-gate return;
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate
4917c478bd9Sstevel@tonic-gate /* return EPERM for any of the privileged actions */
4927c478bd9Sstevel@tonic-gate
493d04ccbb3Scarlsonj if (!is_priv && (ipc_cmd_flags[ia.ia_cmd] & CMD_ISPRIV)) {
494d04ccbb3Scarlsonj dhcpmsg(MSG_WARNING,
495d04ccbb3Scarlsonj "ipc_event: privileged ipc command (%s) attempted on %s",
496d04ccbb3Scarlsonj dhcp_ipc_type_to_string(ia.ia_cmd), ia.ia_request->ifname);
497d04ccbb3Scarlsonj send_error_reply(&ia, DHCP_IPC_E_PERM);
498d04ccbb3Scarlsonj return;
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate
5017c478bd9Sstevel@tonic-gate /*
502d04ccbb3Scarlsonj * Try to locate the state machine associated with this command. If
503d04ccbb3Scarlsonj * the command is DHCP_START or DHCP_INFORM and there isn't a state
504d04ccbb3Scarlsonj * machine already, make one (there may already be one from a previous
505d04ccbb3Scarlsonj * failed attempt to START or INFORM). Otherwise, verify the reference
506d04ccbb3Scarlsonj * is still valid.
507d04ccbb3Scarlsonj *
508d04ccbb3Scarlsonj * The interface name may be blank. In that case, we look up the
509d04ccbb3Scarlsonj * primary interface, and the requested type (v4 or v6) doesn't matter.
5107c478bd9Sstevel@tonic-gate */
5117c478bd9Sstevel@tonic-gate
512d04ccbb3Scarlsonj isv6 = (ia.ia_request->message_type & DHCP_V6) != 0;
513d04ccbb3Scarlsonj ifname = ia.ia_request->ifname;
514d04ccbb3Scarlsonj if (*ifname == '\0')
515d04ccbb3Scarlsonj dsmp = primary_smach(isv6);
516d04ccbb3Scarlsonj else
517d04ccbb3Scarlsonj dsmp = lookup_smach(ifname, isv6);
5187c478bd9Sstevel@tonic-gate
519d04ccbb3Scarlsonj if (dsmp != NULL) {
520d04ccbb3Scarlsonj /* Note that verify_smach drops a reference */
521d04ccbb3Scarlsonj hold_smach(dsmp);
522d04ccbb3Scarlsonj if (!verify_smach(dsmp))
523d04ccbb3Scarlsonj dsmp = NULL;
524d04ccbb3Scarlsonj }
5257c478bd9Sstevel@tonic-gate
526d04ccbb3Scarlsonj if (dsmp == NULL) {
5277c478bd9Sstevel@tonic-gate /*
528a1196271SJames Carlson * If the user asked for the primary DHCP interface by giving
529a1196271SJames Carlson * an empty string and there is no primary, then check if we're
530a1196271SJames Carlson * handling dhcpinfo. If so, then simulate primary selection.
531a1196271SJames Carlson * Otherwise, report failure.
5327c478bd9Sstevel@tonic-gate */
533d04ccbb3Scarlsonj if (ifname[0] == '\0') {
534a1196271SJames Carlson if (ia.ia_cmd == DHCP_GET_TAG)
535a1196271SJames Carlson dsmp = info_primary_smach(isv6);
536a1196271SJames Carlson if (dsmp == NULL)
537a1196271SJames Carlson error = DHCP_IPC_E_NOPRIMARY;
5387c478bd9Sstevel@tonic-gate
5397c478bd9Sstevel@tonic-gate /*
540d04ccbb3Scarlsonj * If there's no interface, and we're starting up, then create
541d04ccbb3Scarlsonj * it now, along with a state machine for it. Note that if
542d04ccbb3Scarlsonj * insert_smach fails, it discards the LIF reference.
5437c478bd9Sstevel@tonic-gate */
544d04ccbb3Scarlsonj } else if (ipc_cmd_flags[ia.ia_cmd] & CMD_CREATE) {
545d04ccbb3Scarlsonj dhcp_lif_t *lif;
5467c478bd9Sstevel@tonic-gate
547d04ccbb3Scarlsonj lif = attach_lif(ifname, isv6, &error);
548d04ccbb3Scarlsonj if (lif != NULL &&
549d04ccbb3Scarlsonj (dsmp = insert_smach(lif, &error)) != NULL) {
550d04ccbb3Scarlsonj /*
551dc918d99Smeem * Get client ID for logical interface. (V4
552dc918d99Smeem * only, because V6 plumbs its own interfaces.)
553d04ccbb3Scarlsonj */
554d04ccbb3Scarlsonj error = get_smach_cid(dsmp);
555d04ccbb3Scarlsonj if (error != DHCP_IPC_SUCCESS) {
5563589885cScarlsonj remove_smach(dsmp);
557d04ccbb3Scarlsonj dsmp = NULL;
558d04ccbb3Scarlsonj }
559dc918d99Smeem dsm_created = (dsmp != NULL);
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate
562d04ccbb3Scarlsonj /*
563d04ccbb3Scarlsonj * Otherwise, this is an operation on an unknown interface.
564d04ccbb3Scarlsonj */
565d04ccbb3Scarlsonj } else {
566d04ccbb3Scarlsonj error = DHCP_IPC_E_UNKIF;
567d04ccbb3Scarlsonj }
568d04ccbb3Scarlsonj if (dsmp == NULL) {
569d04ccbb3Scarlsonj send_error_reply(&ia, error);
5707c478bd9Sstevel@tonic-gate return;
5717c478bd9Sstevel@tonic-gate }
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate
574dc918d99Smeem /*
575dc918d99Smeem * If this is a request for DHCP to manage a lease on an address,
576dc918d99Smeem * ensure that IFF_DHCPRUNNING is set (we don't set this when the lif
577dc918d99Smeem * is created because the lif may have been created for INFORM).
578dc918d99Smeem */
579dc918d99Smeem if (ia.ia_cmd == DHCP_START &&
580dc918d99Smeem (error = set_lif_dhcp(dsmp->dsm_lif)) != DHCP_IPC_SUCCESS) {
581dc918d99Smeem if (dsm_created)
582dc918d99Smeem remove_smach(dsmp);
583dc918d99Smeem send_error_reply(&ia, error);
584dc918d99Smeem return;
585dc918d99Smeem }
586dc918d99Smeem
587d04ccbb3Scarlsonj if ((dsmp->dsm_dflags & DHCP_IF_BOOTP) &&
588d04ccbb3Scarlsonj !(ipc_cmd_flags[ia.ia_cmd] & CMD_BOOTP)) {
589d04ccbb3Scarlsonj dhcpmsg(MSG_ERROR, "command %s not valid for BOOTP on %s",
590d04ccbb3Scarlsonj dhcp_ipc_type_to_string(ia.ia_cmd), dsmp->dsm_name);
591d04ccbb3Scarlsonj send_error_reply(&ia, DHCP_IPC_E_BOOTP);
5927c478bd9Sstevel@tonic-gate return;
5937c478bd9Sstevel@tonic-gate }
5947c478bd9Sstevel@tonic-gate
5957c478bd9Sstevel@tonic-gate /*
596d04ccbb3Scarlsonj * verify that the state machine is in a state which will allow the
5977c478bd9Sstevel@tonic-gate * command. we do this up front so that we can return an error
5987c478bd9Sstevel@tonic-gate * *before* needlessly cancelling an in-progress transaction.
5997c478bd9Sstevel@tonic-gate */
6007c478bd9Sstevel@tonic-gate
601d04ccbb3Scarlsonj if (!check_cmd_allowed(dsmp->dsm_state, ia.ia_cmd)) {
602d04ccbb3Scarlsonj dhcpmsg(MSG_DEBUG,
603d04ccbb3Scarlsonj "in state %s; not allowing %s command on %s",
604d04ccbb3Scarlsonj dhcp_state_to_string(dsmp->dsm_state),
605d04ccbb3Scarlsonj dhcp_ipc_type_to_string(ia.ia_cmd), dsmp->dsm_name);
606d04ccbb3Scarlsonj send_error_reply(&ia,
607d04ccbb3Scarlsonj ia.ia_cmd == DHCP_START && dsmp->dsm_state != INIT ?
608d04ccbb3Scarlsonj DHCP_IPC_E_RUNNING : DHCP_IPC_E_OUTSTATE);
6097c478bd9Sstevel@tonic-gate return;
6107c478bd9Sstevel@tonic-gate }
6117c478bd9Sstevel@tonic-gate
612d04ccbb3Scarlsonj dhcpmsg(MSG_DEBUG, "in state %s; allowing %s command on %s",
613d04ccbb3Scarlsonj dhcp_state_to_string(dsmp->dsm_state),
614d04ccbb3Scarlsonj dhcp_ipc_type_to_string(ia.ia_cmd), dsmp->dsm_name);
615d04ccbb3Scarlsonj
616d04ccbb3Scarlsonj if ((ia.ia_request->message_type & DHCP_PRIMARY) && is_priv)
617d04ccbb3Scarlsonj make_primary(dsmp);
6187c478bd9Sstevel@tonic-gate
6197c478bd9Sstevel@tonic-gate /*
620d04ccbb3Scarlsonj * The current design dictates that there can be only one outstanding
621d04ccbb3Scarlsonj * transaction per state machine -- this simplifies the code
622d04ccbb3Scarlsonj * considerably and also fits well with RFCs 2131 and 3315. It is
623d04ccbb3Scarlsonj * worth classifying the different DHCP commands into synchronous
624d04ccbb3Scarlsonj * (those which we will handle now and reply to immediately) and
625d04ccbb3Scarlsonj * asynchronous (those which require transactions and will be completed
626d04ccbb3Scarlsonj * at an indeterminate time in the future):
6277c478bd9Sstevel@tonic-gate *
628d04ccbb3Scarlsonj * DROP: removes the agent's management of a state machine.
6297c478bd9Sstevel@tonic-gate * asynchronous as the script program may be invoked.
6307c478bd9Sstevel@tonic-gate *
631d04ccbb3Scarlsonj * PING: checks to see if the agent has a named state machine.
6327c478bd9Sstevel@tonic-gate * synchronous, since no packets need to be sent
6337c478bd9Sstevel@tonic-gate * to the DHCP server.
6347c478bd9Sstevel@tonic-gate *
635d04ccbb3Scarlsonj * STATUS: returns information about a state machine.
6367c478bd9Sstevel@tonic-gate * synchronous, since no packets need to be sent
6377c478bd9Sstevel@tonic-gate * to the DHCP server.
6387c478bd9Sstevel@tonic-gate *
639d04ccbb3Scarlsonj * RELEASE: releases the agent's management of a state machine
640d04ccbb3Scarlsonj * and brings the associated interfaces down. asynchronous
641d04ccbb3Scarlsonj * as the script program may be invoked.
6427c478bd9Sstevel@tonic-gate *
6437c478bd9Sstevel@tonic-gate * EXTEND: renews a lease. asynchronous, since the agent
6447c478bd9Sstevel@tonic-gate * needs to wait for an ACK, etc.
6457c478bd9Sstevel@tonic-gate *
646d04ccbb3Scarlsonj * START: starts DHCP on a named state machine. asynchronous since
6477c478bd9Sstevel@tonic-gate * the agent needs to wait for OFFERs, ACKs, etc.
6487c478bd9Sstevel@tonic-gate *
649d04ccbb3Scarlsonj * INFORM: obtains configuration parameters for the system using
650d04ccbb3Scarlsonj * externally configured interface. asynchronous, since the
6517c478bd9Sstevel@tonic-gate * agent needs to wait for an ACK.
6527c478bd9Sstevel@tonic-gate *
653d04ccbb3Scarlsonj * Notice that EXTEND, INFORM, START, DROP and RELEASE are
654d04ccbb3Scarlsonj * asynchronous. Notice also that asynchronous commands may occur from
655d04ccbb3Scarlsonj * within the agent -- for instance, the agent will need to do implicit
656d04ccbb3Scarlsonj * EXTENDs to extend the lease. In order to make the code simpler, the
657d04ccbb3Scarlsonj * following rules apply for asynchronous commands:
6587c478bd9Sstevel@tonic-gate *
659d04ccbb3Scarlsonj * There can only be one asynchronous command at a time per state
660d04ccbb3Scarlsonj * machine. The current asynchronous command is managed by the async_*
661d04ccbb3Scarlsonj * api: async_start(), async_finish(), and async_cancel().
662d04ccbb3Scarlsonj * async_start() starts management of a new asynchronous command on an
663d04ccbb3Scarlsonj * state machine, which should only be done after async_cancel() to
664d04ccbb3Scarlsonj * terminate a previous command. When the command is completed,
665d04ccbb3Scarlsonj * async_finish() should be called.
6667c478bd9Sstevel@tonic-gate *
667d04ccbb3Scarlsonj * Asynchronous commands started by a user command have an associated
668d04ccbb3Scarlsonj * ipc_action which provides the agent with information for how to get
669d04ccbb3Scarlsonj * in touch with the user command when the action completes. These
670d04ccbb3Scarlsonj * ipc_action records also have an associated timeout which may be
671d04ccbb3Scarlsonj * infinite. ipc_action_start() should be called when starting an
672d04ccbb3Scarlsonj * asynchronous command requested by a user, which sets up the timer
673d04ccbb3Scarlsonj * and keeps track of the ipc information (file descriptor, request
674d04ccbb3Scarlsonj * type). When the asynchronous command completes, ipc_action_finish()
675d04ccbb3Scarlsonj * should be called to return a command status code to the user and
676d04ccbb3Scarlsonj * close the ipc connection). If the command does not complete before
677d04ccbb3Scarlsonj * the timer fires, ipc_action_timeout() is called which closes the ipc
678d04ccbb3Scarlsonj * connection and returns DHCP_IPC_E_TIMEOUT to the user. Note that
679d04ccbb3Scarlsonj * independent of ipc_action_timeout(), ipc_action_finish() should be
680d04ccbb3Scarlsonj * called.
6817c478bd9Sstevel@tonic-gate *
682d04ccbb3Scarlsonj * on a case-by-case basis, here is what happens (per state machine):
6837c478bd9Sstevel@tonic-gate *
684d04ccbb3Scarlsonj * o When an asynchronous command is requested, then
685d04ccbb3Scarlsonj * async_cancel() is called to terminate any non-user
686d04ccbb3Scarlsonj * action in progress. If there's a user action running,
6877c478bd9Sstevel@tonic-gate * the user command is sent DHCP_IPC_E_PEND.
6887c478bd9Sstevel@tonic-gate *
689d14c4fc9SJoshua M. Clulow * o otherwise, the transaction is started with
6907c478bd9Sstevel@tonic-gate * async_start(). if the transaction is on behalf
6917c478bd9Sstevel@tonic-gate * of a user, ipc_action_start() is called to keep
6927c478bd9Sstevel@tonic-gate * track of the ipc information and set up the
6937c478bd9Sstevel@tonic-gate * ipc_action timer.
6947c478bd9Sstevel@tonic-gate *
6957c478bd9Sstevel@tonic-gate * o if the command completes normally and before a
6967c478bd9Sstevel@tonic-gate * timeout fires, then async_finish() is called.
6977c478bd9Sstevel@tonic-gate * if there was an associated ipc_action,
6987c478bd9Sstevel@tonic-gate * ipc_action_finish() is called to complete it.
6997c478bd9Sstevel@tonic-gate *
7007c478bd9Sstevel@tonic-gate * o if the command fails before a timeout fires, then
701d04ccbb3Scarlsonj * async_finish() is called, and the state machine is
7027c478bd9Sstevel@tonic-gate * is returned to a known state based on the command.
7037c478bd9Sstevel@tonic-gate * if there was an associated ipc_action,
7047c478bd9Sstevel@tonic-gate * ipc_action_finish() is called to complete it.
7057c478bd9Sstevel@tonic-gate *
7067c478bd9Sstevel@tonic-gate * o if the ipc_action timer fires before command
7077c478bd9Sstevel@tonic-gate * completion, then DHCP_IPC_E_TIMEOUT is returned to
7087c478bd9Sstevel@tonic-gate * the user. however, the transaction continues to
7097c478bd9Sstevel@tonic-gate * be carried out asynchronously.
7107c478bd9Sstevel@tonic-gate */
7117c478bd9Sstevel@tonic-gate
712d04ccbb3Scarlsonj if (ipc_cmd_flags[ia.ia_cmd] & CMD_IMMED) {
713d04ccbb3Scarlsonj /*
714d04ccbb3Scarlsonj * Only immediate commands (ping, status, get_tag) need to
715d04ccbb3Scarlsonj * worry about freeing ia through one of the reply functions
716d04ccbb3Scarlsonj * before returning.
717d04ccbb3Scarlsonj */
718d04ccbb3Scarlsonj iap = &ia;
719d04ccbb3Scarlsonj } else {
7207c478bd9Sstevel@tonic-gate /*
7217c478bd9Sstevel@tonic-gate * if shutdown request has been received, send back an error.
7227c478bd9Sstevel@tonic-gate */
7237c478bd9Sstevel@tonic-gate if (shutdown_started) {
724d04ccbb3Scarlsonj send_error_reply(&ia, DHCP_IPC_E_OUTSTATE);
7257c478bd9Sstevel@tonic-gate return;
7267c478bd9Sstevel@tonic-gate }
7277c478bd9Sstevel@tonic-gate
728d04ccbb3Scarlsonj if (dsmp->dsm_dflags & DHCP_IF_BUSY) {
729d04ccbb3Scarlsonj send_error_reply(&ia, DHCP_IPC_E_PEND);
7307c478bd9Sstevel@tonic-gate return;
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate
733d04ccbb3Scarlsonj if (!ipc_action_start(dsmp, &ia)) {
7347c478bd9Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "ipc_event: ipc_action_start "
735d04ccbb3Scarlsonj "failed for %s", dsmp->dsm_name);
736d04ccbb3Scarlsonj send_error_reply(&ia, DHCP_IPC_E_MEMORY);
7377c478bd9Sstevel@tonic-gate return;
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate
740d04ccbb3Scarlsonj /* Action structure consumed by above function */
741d04ccbb3Scarlsonj iap = &dsmp->dsm_ia;
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate
744d04ccbb3Scarlsonj switch (iap->ia_cmd) {
7457c478bd9Sstevel@tonic-gate
7467c478bd9Sstevel@tonic-gate case DHCP_DROP:
7478ff86213Scarlsonj if (dsmp->dsm_droprelease)
7488ff86213Scarlsonj break;
7498ff86213Scarlsonj dsmp->dsm_droprelease = B_TRUE;
7503644994cSmeem
7513644994cSmeem /*
7523644994cSmeem * Ensure that a timer associated with the existing state
7533644994cSmeem * doesn't pop while we're waiting for the script to complete.
7543644994cSmeem * (If so, chaos can result -- e.g., a timer causes us to end
7553644994cSmeem * up in dhcp_selecting() would start acquiring a new lease on
7563644994cSmeem * dsmp while our DHCP_DROP dismantling is ongoing.)
7573644994cSmeem */
7583644994cSmeem cancel_smach_timers(dsmp);
759d04ccbb3Scarlsonj (void) script_start(dsmp, isv6 ? EVENT_DROP6 : EVENT_DROP,
760d04ccbb3Scarlsonj dhcp_drop, NULL, NULL);
761d04ccbb3Scarlsonj break; /* not an immediate function */
7627c478bd9Sstevel@tonic-gate
7637c478bd9Sstevel@tonic-gate case DHCP_EXTEND:
764b31320a7SChris Fraire dhcp_smach_set_msg_reqhost(dsmp, iap);
765d04ccbb3Scarlsonj (void) dhcp_extending(dsmp);
7667c478bd9Sstevel@tonic-gate break;
7677c478bd9Sstevel@tonic-gate
7687c478bd9Sstevel@tonic-gate case DHCP_GET_TAG: {
7697c478bd9Sstevel@tonic-gate dhcp_optnum_t optnum;
770d04ccbb3Scarlsonj void *opt = NULL;
771d04ccbb3Scarlsonj uint_t optlen;
7727c478bd9Sstevel@tonic-gate boolean_t did_alloc = B_FALSE;
773d04ccbb3Scarlsonj PKT_LIST *ack = dsmp->dsm_ack;
774a1196271SJames Carlson int i;
7757c478bd9Sstevel@tonic-gate
7767c478bd9Sstevel@tonic-gate /*
7777c478bd9Sstevel@tonic-gate * verify the request makes sense.
7787c478bd9Sstevel@tonic-gate */
7797c478bd9Sstevel@tonic-gate
780d04ccbb3Scarlsonj if (iap->ia_request->data_type != DHCP_TYPE_OPTNUM ||
781d04ccbb3Scarlsonj iap->ia_request->data_length != sizeof (dhcp_optnum_t)) {
782d04ccbb3Scarlsonj send_error_reply(iap, DHCP_IPC_E_PROTO);
783d04ccbb3Scarlsonj break;
7847c478bd9Sstevel@tonic-gate }
7857c478bd9Sstevel@tonic-gate
786d04ccbb3Scarlsonj (void) memcpy(&optnum, iap->ia_request->buffer,
787d04ccbb3Scarlsonj sizeof (dhcp_optnum_t));
788d04ccbb3Scarlsonj
7897c478bd9Sstevel@tonic-gate load_option:
7907c478bd9Sstevel@tonic-gate switch (optnum.category) {
7917c478bd9Sstevel@tonic-gate
7927c478bd9Sstevel@tonic-gate case DSYM_SITE: /* FALLTHRU */
7937c478bd9Sstevel@tonic-gate case DSYM_STANDARD:
794a1196271SJames Carlson for (i = 0; i < dsmp->dsm_pillen; i++) {
795a1196271SJames Carlson if (dsmp->dsm_pil[i] == optnum.code)
796a1196271SJames Carlson break;
797a1196271SJames Carlson }
798a1196271SJames Carlson if (i < dsmp->dsm_pillen)
799a1196271SJames Carlson break;
800d04ccbb3Scarlsonj if (isv6) {
801d04ccbb3Scarlsonj opt = dhcpv6_pkt_option(ack, NULL, optnum.code,
802d04ccbb3Scarlsonj NULL);
803d04ccbb3Scarlsonj } else {
804b31320a7SChris Fraire opt = dhcp_get_ack_or_state(dsmp, ack,
805b31320a7SChris Fraire optnum.code, &did_alloc);
806d04ccbb3Scarlsonj }
8077c478bd9Sstevel@tonic-gate break;
8087c478bd9Sstevel@tonic-gate
8097c478bd9Sstevel@tonic-gate case DSYM_VENDOR:
810d04ccbb3Scarlsonj if (isv6) {
811d04ccbb3Scarlsonj dhcpv6_option_t *d6o;
812d04ccbb3Scarlsonj uint32_t ent;
8137c478bd9Sstevel@tonic-gate
814d04ccbb3Scarlsonj /*
815d04ccbb3Scarlsonj * Look through vendor options to find our
816d04ccbb3Scarlsonj * enterprise number.
817d04ccbb3Scarlsonj */
818d04ccbb3Scarlsonj d6o = NULL;
819d04ccbb3Scarlsonj for (;;) {
820d04ccbb3Scarlsonj d6o = dhcpv6_pkt_option(ack, d6o,
821d04ccbb3Scarlsonj DHCPV6_OPT_VENDOR_OPT, &optlen);
822d04ccbb3Scarlsonj if (d6o == NULL)
823d04ccbb3Scarlsonj break;
824d04ccbb3Scarlsonj optlen -= sizeof (*d6o);
825d04ccbb3Scarlsonj if (optlen < sizeof (ent))
826d04ccbb3Scarlsonj continue;
827d04ccbb3Scarlsonj (void) memcpy(&ent, d6o + 1,
828d04ccbb3Scarlsonj sizeof (ent));
829d04ccbb3Scarlsonj if (ntohl(ent) != DHCPV6_SUN_ENT)
830d04ccbb3Scarlsonj continue;
831d04ccbb3Scarlsonj break;
832d04ccbb3Scarlsonj }
833d04ccbb3Scarlsonj if (d6o != NULL) {
834d04ccbb3Scarlsonj /*
835d04ccbb3Scarlsonj * Now find the requested vendor option
836d04ccbb3Scarlsonj * within the vendor options block.
837d04ccbb3Scarlsonj */
838d04ccbb3Scarlsonj opt = dhcpv6_find_option(
839d04ccbb3Scarlsonj (char *)(d6o + 1) + sizeof (ent),
840d04ccbb3Scarlsonj optlen - sizeof (ent), NULL,
841d04ccbb3Scarlsonj optnum.code, NULL);
842d04ccbb3Scarlsonj }
843d04ccbb3Scarlsonj } else {
844d04ccbb3Scarlsonj /*
845d04ccbb3Scarlsonj * the test against VS_OPTION_START is broken
846d04ccbb3Scarlsonj * up into two tests to avoid compiler warnings
847d04ccbb3Scarlsonj * under intel.
848d04ccbb3Scarlsonj */
849d04ccbb3Scarlsonj if ((optnum.code > VS_OPTION_START ||
850d04ccbb3Scarlsonj optnum.code == VS_OPTION_START) &&
851d04ccbb3Scarlsonj optnum.code <= VS_OPTION_END)
852d04ccbb3Scarlsonj opt = ack->vs[optnum.code];
853d04ccbb3Scarlsonj }
8547c478bd9Sstevel@tonic-gate break;
8557c478bd9Sstevel@tonic-gate
8567c478bd9Sstevel@tonic-gate case DSYM_FIELD:
857d04ccbb3Scarlsonj if (isv6) {
858d04ccbb3Scarlsonj dhcpv6_message_t *d6m =
859d04ccbb3Scarlsonj (dhcpv6_message_t *)ack->pkt;
860d04ccbb3Scarlsonj dhcpv6_option_t *d6o;
861d04ccbb3Scarlsonj
862d04ccbb3Scarlsonj /* Validate the packet field the user wants */
863d04ccbb3Scarlsonj optlen = optnum.code + optnum.size;
864d04ccbb3Scarlsonj if (d6m->d6m_msg_type ==
865d04ccbb3Scarlsonj DHCPV6_MSG_RELAY_FORW ||
866d04ccbb3Scarlsonj d6m->d6m_msg_type ==
867d04ccbb3Scarlsonj DHCPV6_MSG_RELAY_REPL) {
868d04ccbb3Scarlsonj if (optlen > sizeof (dhcpv6_relay_t))
869d04ccbb3Scarlsonj break;
870d04ccbb3Scarlsonj } else {
871d04ccbb3Scarlsonj if (optlen > sizeof (*d6m))
872d04ccbb3Scarlsonj break;
873d04ccbb3Scarlsonj }
874d04ccbb3Scarlsonj
875d04ccbb3Scarlsonj opt = malloc(sizeof (*d6o) + optnum.size);
876d04ccbb3Scarlsonj if (opt != NULL) {
877d04ccbb3Scarlsonj d6o = opt;
878d04ccbb3Scarlsonj d6o->d6o_code = htons(optnum.code);
879d04ccbb3Scarlsonj d6o->d6o_len = htons(optnum.size);
880d04ccbb3Scarlsonj (void) memcpy(d6o + 1, (caddr_t)d6m +
881d04ccbb3Scarlsonj optnum.code, optnum.size);
882d04ccbb3Scarlsonj }
883d04ccbb3Scarlsonj } else {
884d04ccbb3Scarlsonj if (optnum.code + optnum.size > sizeof (PKT))
885d04ccbb3Scarlsonj break;
886d04ccbb3Scarlsonj
887b31320a7SChris Fraire opt = malloc(optnum.size + DHCP_OPT_META_LEN);
888d04ccbb3Scarlsonj if (opt != NULL) {
889d04ccbb3Scarlsonj DHCP_OPT *v4opt = opt;
890d04ccbb3Scarlsonj
891d04ccbb3Scarlsonj v4opt->len = optnum.size;
892d04ccbb3Scarlsonj v4opt->code = optnum.code;
893d04ccbb3Scarlsonj (void) memcpy(v4opt->value,
894d04ccbb3Scarlsonj (caddr_t)ack->pkt + optnum.code,
895d04ccbb3Scarlsonj optnum.size);
896d04ccbb3Scarlsonj }
897d04ccbb3Scarlsonj }
8987c478bd9Sstevel@tonic-gate
8997c478bd9Sstevel@tonic-gate if (opt == NULL) {
900d04ccbb3Scarlsonj send_error_reply(iap, DHCP_IPC_E_MEMORY);
9017c478bd9Sstevel@tonic-gate return;
9027c478bd9Sstevel@tonic-gate }
9037c478bd9Sstevel@tonic-gate did_alloc = B_TRUE;
9047c478bd9Sstevel@tonic-gate break;
9057c478bd9Sstevel@tonic-gate
9067c478bd9Sstevel@tonic-gate default:
907d04ccbb3Scarlsonj send_error_reply(iap, DHCP_IPC_E_PROTO);
9087c478bd9Sstevel@tonic-gate return;
9097c478bd9Sstevel@tonic-gate }
9107c478bd9Sstevel@tonic-gate
9117c478bd9Sstevel@tonic-gate /*
912b31320a7SChris Fraire * return the option payload, if there was one.
9137c478bd9Sstevel@tonic-gate */
9147c478bd9Sstevel@tonic-gate
9157c478bd9Sstevel@tonic-gate if (opt != NULL) {
916d04ccbb3Scarlsonj if (isv6) {
917d04ccbb3Scarlsonj dhcpv6_option_t d6ov;
918d04ccbb3Scarlsonj
919d04ccbb3Scarlsonj (void) memcpy(&d6ov, opt, sizeof (d6ov));
920d04ccbb3Scarlsonj optlen = ntohs(d6ov.d6o_len) + sizeof (d6ov);
921d04ccbb3Scarlsonj } else {
922b31320a7SChris Fraire optlen = ((DHCP_OPT *)opt)->len +
923b31320a7SChris Fraire DHCP_OPT_META_LEN;
924d04ccbb3Scarlsonj }
925d04ccbb3Scarlsonj send_data_reply(iap, 0, DHCP_TYPE_OPTION, opt, optlen);
9267c478bd9Sstevel@tonic-gate
9277c478bd9Sstevel@tonic-gate if (did_alloc)
9287c478bd9Sstevel@tonic-gate free(opt);
929d04ccbb3Scarlsonj break;
930d04ccbb3Scarlsonj } else if (ack != dsmp->dsm_orig_ack) {
9317c478bd9Sstevel@tonic-gate /*
9327c478bd9Sstevel@tonic-gate * There wasn't any definition for the option in the
9337c478bd9Sstevel@tonic-gate * current ack, so now retry with the original ack if
9347c478bd9Sstevel@tonic-gate * the original ack is not the current ack.
9357c478bd9Sstevel@tonic-gate */
936d04ccbb3Scarlsonj ack = dsmp->dsm_orig_ack;
9377c478bd9Sstevel@tonic-gate goto load_option;
9387c478bd9Sstevel@tonic-gate }
9397c478bd9Sstevel@tonic-gate
9407c478bd9Sstevel@tonic-gate /*
9417c478bd9Sstevel@tonic-gate * note that an "okay" response is returned either in
9427c478bd9Sstevel@tonic-gate * the case of an unknown option or a known option
9437c478bd9Sstevel@tonic-gate * with no payload. this is okay (for now) since
9447c478bd9Sstevel@tonic-gate * dhcpinfo checks whether an option is valid before
9457c478bd9Sstevel@tonic-gate * ever performing ipc with the agent.
9467c478bd9Sstevel@tonic-gate */
9477c478bd9Sstevel@tonic-gate
948d04ccbb3Scarlsonj send_ok_reply(iap);
949d04ccbb3Scarlsonj break;
9507c478bd9Sstevel@tonic-gate }
9517c478bd9Sstevel@tonic-gate
9527c478bd9Sstevel@tonic-gate case DHCP_INFORM:
953d04ccbb3Scarlsonj dhcp_inform(dsmp);
9547c478bd9Sstevel@tonic-gate /* next destination: dhcp_acknak() */
955d04ccbb3Scarlsonj break; /* not an immediate function */
9567c478bd9Sstevel@tonic-gate
9577c478bd9Sstevel@tonic-gate case DHCP_PING:
958d04ccbb3Scarlsonj if (dsmp->dsm_dflags & DHCP_IF_FAILED)
959d04ccbb3Scarlsonj send_error_reply(iap, DHCP_IPC_E_FAILEDIF);
9607c478bd9Sstevel@tonic-gate else
961d04ccbb3Scarlsonj send_ok_reply(iap);
962d04ccbb3Scarlsonj break;
9637c478bd9Sstevel@tonic-gate
9647c478bd9Sstevel@tonic-gate case DHCP_RELEASE:
9658ff86213Scarlsonj if (dsmp->dsm_droprelease)
9668ff86213Scarlsonj break;
9678ff86213Scarlsonj dsmp->dsm_droprelease = B_TRUE;
9683644994cSmeem cancel_smach_timers(dsmp); /* see comment in DHCP_DROP above */
969d04ccbb3Scarlsonj (void) script_start(dsmp, isv6 ? EVENT_RELEASE6 :
970d04ccbb3Scarlsonj EVENT_RELEASE, dhcp_release, "Finished with lease.", NULL);
971d04ccbb3Scarlsonj break; /* not an immediate function */
972d04ccbb3Scarlsonj
973d04ccbb3Scarlsonj case DHCP_START: {
974d04ccbb3Scarlsonj PKT_LIST *ack, *oack;
975d04ccbb3Scarlsonj PKT_LIST *plp[2];
9767c478bd9Sstevel@tonic-gate
977d04ccbb3Scarlsonj deprecate_leases(dsmp);
978b31320a7SChris Fraire dhcp_smach_set_msg_reqhost(dsmp, iap);
9797c478bd9Sstevel@tonic-gate
9807c478bd9Sstevel@tonic-gate /*
9817c478bd9Sstevel@tonic-gate * if we have a valid hostconf lying around, then jump
9827c478bd9Sstevel@tonic-gate * into INIT_REBOOT. if it fails, we'll end up going
9837c478bd9Sstevel@tonic-gate * through the whole selecting() procedure again.
9847c478bd9Sstevel@tonic-gate */
9857c478bd9Sstevel@tonic-gate
986d04ccbb3Scarlsonj error = read_hostconf(dsmp->dsm_name, plp, 2, dsmp->dsm_isv6);
987d04ccbb3Scarlsonj ack = error > 0 ? plp[0] : NULL;
988d04ccbb3Scarlsonj oack = error > 1 ? plp[1] : NULL;
989d04ccbb3Scarlsonj
990d04ccbb3Scarlsonj /*
991d04ccbb3Scarlsonj * If the allocation of the old ack fails, that's fine;
992d04ccbb3Scarlsonj * continue without it.
993d04ccbb3Scarlsonj */
994d04ccbb3Scarlsonj if (oack == NULL)
995d04ccbb3Scarlsonj oack = ack;
996d04ccbb3Scarlsonj
997d04ccbb3Scarlsonj /*
998d04ccbb3Scarlsonj * As long as we've allocated something, start using it.
999d04ccbb3Scarlsonj */
1000d04ccbb3Scarlsonj if (ack != NULL) {
1001d04ccbb3Scarlsonj dsmp->dsm_orig_ack = oack;
1002d04ccbb3Scarlsonj dsmp->dsm_ack = ack;
1003d04ccbb3Scarlsonj dhcp_init_reboot(dsmp);
10047c478bd9Sstevel@tonic-gate /* next destination: dhcp_acknak() */
1005d04ccbb3Scarlsonj break;
10067c478bd9Sstevel@tonic-gate }
10077c478bd9Sstevel@tonic-gate
10087c478bd9Sstevel@tonic-gate /*
10097c478bd9Sstevel@tonic-gate * if not debugging, wait for a few seconds before
10107c478bd9Sstevel@tonic-gate * going into SELECTING.
10117c478bd9Sstevel@tonic-gate */
10127c478bd9Sstevel@tonic-gate
1013e8f5b7dfSToomas Soome if (debug_level != 0 || !set_start_timer(dsmp)) {
1014cfb9c9abScarlsonj dhcp_selecting(dsmp);
1015cfb9c9abScarlsonj /* next destination: dhcp_requesting() */
10167c478bd9Sstevel@tonic-gate }
1017e8f5b7dfSToomas Soome /* else next destination: dhcp_start() */
1018d04ccbb3Scarlsonj }
1019e8f5b7dfSToomas Soome break;
10207c478bd9Sstevel@tonic-gate
10217c478bd9Sstevel@tonic-gate case DHCP_STATUS: {
10227c478bd9Sstevel@tonic-gate dhcp_status_t status;
1023d04ccbb3Scarlsonj dhcp_lease_t *dlp;
1024d04ccbb3Scarlsonj
1025d04ccbb3Scarlsonj status.if_began = monosec_to_time(dsmp->dsm_curstart_monosec);
10267c478bd9Sstevel@tonic-gate
1027d04ccbb3Scarlsonj /*
1028d04ccbb3Scarlsonj * We return information on just the first lease as being
1029d04ccbb3Scarlsonj * representative of the lot. A better status mechanism is
1030d04ccbb3Scarlsonj * needed.
1031d04ccbb3Scarlsonj */
1032d04ccbb3Scarlsonj dlp = dsmp->dsm_leases;
10337c478bd9Sstevel@tonic-gate
1034d04ccbb3Scarlsonj if (dlp == NULL ||
1035d04ccbb3Scarlsonj dlp->dl_lifs->lif_expire.dt_start == DHCP_PERM) {
10367c478bd9Sstevel@tonic-gate status.if_t1 = DHCP_PERM;
10377c478bd9Sstevel@tonic-gate status.if_t2 = DHCP_PERM;
10387c478bd9Sstevel@tonic-gate status.if_lease = DHCP_PERM;
10397c478bd9Sstevel@tonic-gate } else {
1040d04ccbb3Scarlsonj status.if_t1 = status.if_began +
1041d04ccbb3Scarlsonj dlp->dl_t1.dt_start;
1042d04ccbb3Scarlsonj status.if_t2 = status.if_began +
1043d04ccbb3Scarlsonj dlp->dl_t2.dt_start;
1044d04ccbb3Scarlsonj status.if_lease = status.if_began +
1045d04ccbb3Scarlsonj dlp->dl_lifs->lif_expire.dt_start;
10467c478bd9Sstevel@tonic-gate }
10477c478bd9Sstevel@tonic-gate
10487c478bd9Sstevel@tonic-gate status.version = DHCP_STATUS_VER;
1049d04ccbb3Scarlsonj status.if_state = dsmp->dsm_state;
1050d04ccbb3Scarlsonj status.if_dflags = dsmp->dsm_dflags;
1051d04ccbb3Scarlsonj status.if_sent = dsmp->dsm_sent;
1052d04ccbb3Scarlsonj status.if_recv = dsmp->dsm_received;
1053d04ccbb3Scarlsonj status.if_bad_offers = dsmp->dsm_bad_offers;
10547c478bd9Sstevel@tonic-gate
105565c8f1c0Smeem (void) strlcpy(status.if_name, dsmp->dsm_name, LIFNAMSIZ);
10567c478bd9Sstevel@tonic-gate
1057d04ccbb3Scarlsonj send_data_reply(iap, 0, DHCP_TYPE_STATUS, &status,
10587c478bd9Sstevel@tonic-gate sizeof (dhcp_status_t));
1059d04ccbb3Scarlsonj break;
10607c478bd9Sstevel@tonic-gate }
10617c478bd9Sstevel@tonic-gate }
10627c478bd9Sstevel@tonic-gate }
106369bb4bb4Scarlsonj
1064b31320a7SChris Fraire /*
1065b31320a7SChris Fraire * dhcp_smach_set_msg_reqhost(): set dsm_msg_reqhost based on the message
1066b31320a7SChris Fraire * content of a DHCP IPC message
1067b31320a7SChris Fraire *
1068b31320a7SChris Fraire * input: dhcp_smach_t *: the state machine instance;
1069b31320a7SChris Fraire * ipc_action_t *: the decoded DHCP IPC message;
1070b31320a7SChris Fraire * output: void
1071b31320a7SChris Fraire */
1072b31320a7SChris Fraire
1073b31320a7SChris Fraire static void
dhcp_smach_set_msg_reqhost(dhcp_smach_t * dsmp,ipc_action_t * iap)1074b31320a7SChris Fraire dhcp_smach_set_msg_reqhost(dhcp_smach_t *dsmp, ipc_action_t *iap)
1075b31320a7SChris Fraire {
1076b31320a7SChris Fraire DHCP_OPT *d4o;
1077b31320a7SChris Fraire dhcp_symbol_t *entry;
1078b31320a7SChris Fraire char *value;
1079b31320a7SChris Fraire
1080b31320a7SChris Fraire if (dsmp->dsm_msg_reqhost != NULL) {
1081b31320a7SChris Fraire dhcpmsg(MSG_DEBUG,
1082b31320a7SChris Fraire "dhcp_smach_set_msg_reqhost: nullify former value, %s",
1083b31320a7SChris Fraire dsmp->dsm_msg_reqhost);
1084b31320a7SChris Fraire free(dsmp->dsm_msg_reqhost);
1085b31320a7SChris Fraire dsmp->dsm_msg_reqhost = NULL;
1086b31320a7SChris Fraire }
1087b31320a7SChris Fraire
1088b31320a7SChris Fraire /*
1089b31320a7SChris Fraire * if a STANDARD/HOSTNAME was sent in the IPC request, then copy that
1090b31320a7SChris Fraire * value into the state machine data if decoding succeeds. Otherwise,
1091b31320a7SChris Fraire * log to indicate at what step the decoding stopped.
1092b31320a7SChris Fraire */
1093b31320a7SChris Fraire
1094b31320a7SChris Fraire if (dsmp->dsm_isv6) {
1095b31320a7SChris Fraire dhcpmsg(MSG_DEBUG, "dhcp_smach_set_msg_reqhost: ipv6 is not"
1096b31320a7SChris Fraire " handled");
1097b31320a7SChris Fraire return;
1098b31320a7SChris Fraire } else if (iap->ia_request->data_type != DHCP_TYPE_OPTION) {
1099b31320a7SChris Fraire dhcpmsg(MSG_DEBUG, "dhcp_smach_set_msg_reqhost: request type"
1100b31320a7SChris Fraire " %d is not DHCP_TYPE_OPTION", iap->ia_request->data_type);
1101b31320a7SChris Fraire return;
1102b31320a7SChris Fraire }
1103b31320a7SChris Fraire
1104*61e172feSToomas Soome if (iap->ia_request->data_length <= DHCP_OPT_META_LEN) {
1105b31320a7SChris Fraire dhcpmsg(MSG_WARNING, "dhcp_smach_set_msg_reqhost:"
1106*61e172feSToomas Soome " DHCP_TYPE_OPTION ia_request buffer is short");
1107b31320a7SChris Fraire return;
1108b31320a7SChris Fraire }
1109b31320a7SChris Fraire
1110b31320a7SChris Fraire d4o = (DHCP_OPT *)iap->ia_request->buffer;
1111b31320a7SChris Fraire if (d4o->code != CD_HOSTNAME) {
1112b31320a7SChris Fraire dhcpmsg(MSG_DEBUG,
1113b31320a7SChris Fraire "dhcp_smach_set_msg_reqhost: ignoring DHCPv4"
1114b31320a7SChris Fraire " option %u", d4o->code);
1115b31320a7SChris Fraire return;
1116b31320a7SChris Fraire } else if (iap->ia_request->data_length - DHCP_OPT_META_LEN
1117b31320a7SChris Fraire != d4o->len) {
1118b31320a7SChris Fraire dhcpmsg(MSG_WARNING, "dhcp_smach_set_msg_reqhost:"
1119b31320a7SChris Fraire " unexpected DHCP_OPT buffer length %u for CD_HOSTNAME"
1120b31320a7SChris Fraire " option length %u", iap->ia_request->data_length,
1121b31320a7SChris Fraire d4o->len);
1122b31320a7SChris Fraire return;
1123b31320a7SChris Fraire }
1124b31320a7SChris Fraire
1125b31320a7SChris Fraire entry = inittab_getbycode(ITAB_CAT_STANDARD, ITAB_CONS_INFO,
1126b31320a7SChris Fraire CD_HOSTNAME);
1127b31320a7SChris Fraire if (entry == NULL) {
1128b31320a7SChris Fraire dhcpmsg(MSG_WARNING,
1129b31320a7SChris Fraire "dhcp_smach_set_msg_reqhost: error getting"
1130b31320a7SChris Fraire " ITAB_CAT_STANDARD ITAB_CONS_INFO"
1131b31320a7SChris Fraire " CD_HOSTNAME entry");
1132b31320a7SChris Fraire return;
1133b31320a7SChris Fraire }
1134b31320a7SChris Fraire
1135b31320a7SChris Fraire value = inittab_decode(entry, d4o->value, d4o->len,
1136b31320a7SChris Fraire /* just_payload */ B_TRUE);
1137b31320a7SChris Fraire if (value == NULL) {
1138b31320a7SChris Fraire dhcpmsg(MSG_WARNING,
1139b31320a7SChris Fraire "dhcp_smach_set_msg_reqhost: error decoding"
1140b31320a7SChris Fraire " CD_HOSTNAME value from DHCP_OPT");
1141b31320a7SChris Fraire } else {
1142b31320a7SChris Fraire dhcpmsg(MSG_DEBUG,
1143b31320a7SChris Fraire "dhcp_smach_set_msg_reqhost: host %s", value);
1144b31320a7SChris Fraire free(dsmp->dsm_msg_reqhost);
1145b31320a7SChris Fraire dsmp->dsm_msg_reqhost = value;
1146b31320a7SChris Fraire }
1147b31320a7SChris Fraire free(entry);
1148b31320a7SChris Fraire }
1149b31320a7SChris Fraire
1150b31320a7SChris Fraire /*
1151b31320a7SChris Fraire * dhcp_get_ack_or_state(): get a v4 option from the ACK or from the state
1152b31320a7SChris Fraire * machine state for certain codes that are not ACKed (e.g., CD_CLIENT_ID)
1153b31320a7SChris Fraire *
1154b31320a7SChris Fraire * input: dhcp_smach_t *: the state machine instance;
1155b31320a7SChris Fraire * PKT_LIST *: the decoded DHCP IPC message;
1156b31320a7SChris Fraire * uint_t: the DHCP client option code;
1157b31320a7SChris Fraire * boolean_t *: a pointer to a value that will be set to B_TRUE if
1158b31320a7SChris Fraire * the return value must be freed (or else set to B_FALSE);
1159b31320a7SChris Fraire * output: the option if found or else NULL.
1160b31320a7SChris Fraire */
1161b31320a7SChris Fraire
1162b31320a7SChris Fraire static DHCP_OPT *
dhcp_get_ack_or_state(const dhcp_smach_t * dsmp,const PKT_LIST * plp,uint_t codenum,boolean_t * did_alloc)1163b31320a7SChris Fraire dhcp_get_ack_or_state(const dhcp_smach_t *dsmp, const PKT_LIST *plp,
1164b31320a7SChris Fraire uint_t codenum, boolean_t *did_alloc)
1165b31320a7SChris Fraire {
1166b31320a7SChris Fraire DHCP_OPT *opt;
1167b31320a7SChris Fraire
1168b31320a7SChris Fraire *did_alloc = B_FALSE;
1169b31320a7SChris Fraire
1170b31320a7SChris Fraire if (codenum > DHCP_LAST_OPT)
1171b31320a7SChris Fraire return (NULL);
1172b31320a7SChris Fraire
1173b31320a7SChris Fraire /* check the ACK first for all codes */
1174b31320a7SChris Fraire opt = plp->opts[codenum];
1175b31320a7SChris Fraire if (opt != NULL)
1176b31320a7SChris Fraire return (opt);
1177b31320a7SChris Fraire
1178b31320a7SChris Fraire /* check the machine state also for certain codes */
1179b31320a7SChris Fraire switch (codenum) {
1180b31320a7SChris Fraire case CD_CLIENT_ID:
1181b31320a7SChris Fraire /*
1182b31320a7SChris Fraire * CD_CLIENT_ID is not sent in an ACK, but it's possibly
1183b31320a7SChris Fraire * available from the state machine data
1184b31320a7SChris Fraire */
1185b31320a7SChris Fraire
1186b31320a7SChris Fraire if (dsmp->dsm_cidlen > 0) {
1187b31320a7SChris Fraire if ((opt = malloc(dsmp->dsm_cidlen + DHCP_OPT_META_LEN))
1188b31320a7SChris Fraire != NULL) {
1189b31320a7SChris Fraire *did_alloc = B_TRUE;
1190b31320a7SChris Fraire (void) encode_dhcp_opt(opt,
1191b31320a7SChris Fraire B_FALSE /* is IPv6 */, CD_CLIENT_ID,
1192b31320a7SChris Fraire dsmp->dsm_cid, dsmp->dsm_cidlen);
1193b31320a7SChris Fraire }
1194b31320a7SChris Fraire }
1195b31320a7SChris Fraire break;
1196b31320a7SChris Fraire default:
1197b31320a7SChris Fraire break;
1198b31320a7SChris Fraire }
1199b31320a7SChris Fraire return (opt);
1200b31320a7SChris Fraire }
1201b31320a7SChris Fraire
120269bb4bb4Scarlsonj /*
120369bb4bb4Scarlsonj * check_rtm_addr(): determine if routing socket message matches interface
120469bb4bb4Scarlsonj * address
120569bb4bb4Scarlsonj *
1206d04ccbb3Scarlsonj * input: const struct if_msghdr *: pointer to routing socket message
1207d04ccbb3Scarlsonj * int: routing socket message length
1208d04ccbb3Scarlsonj * boolean_t: set to B_TRUE if IPv6
1209d04ccbb3Scarlsonj * const in6_addr_t *: pointer to IP address
1210d04ccbb3Scarlsonj * output: boolean_t: B_TRUE if address is a match
121169bb4bb4Scarlsonj */
1212d04ccbb3Scarlsonj
121369bb4bb4Scarlsonj static boolean_t
check_rtm_addr(const struct ifa_msghdr * ifam,int msglen,boolean_t isv6,const in6_addr_t * addr)1214d04ccbb3Scarlsonj check_rtm_addr(const struct ifa_msghdr *ifam, int msglen, boolean_t isv6,
1215d04ccbb3Scarlsonj const in6_addr_t *addr)
121669bb4bb4Scarlsonj {
1217d04ccbb3Scarlsonj const char *cp, *lim;
121869bb4bb4Scarlsonj uint_t flag;
1219d04ccbb3Scarlsonj const struct sockaddr *sa;
122069bb4bb4Scarlsonj
122169bb4bb4Scarlsonj if (!(ifam->ifam_addrs & RTA_IFA))
122269bb4bb4Scarlsonj return (B_FALSE);
122369bb4bb4Scarlsonj
1224d04ccbb3Scarlsonj cp = (const char *)(ifam + 1);
1225d04ccbb3Scarlsonj lim = (const char *)ifam + msglen;
122669bb4bb4Scarlsonj for (flag = 1; flag < RTA_IFA; flag <<= 1) {
122769bb4bb4Scarlsonj if (ifam->ifam_addrs & flag) {
122869bb4bb4Scarlsonj /* LINTED: alignment */
1229d04ccbb3Scarlsonj sa = (const struct sockaddr *)cp;
1230d04ccbb3Scarlsonj if ((const char *)(sa + 1) > lim)
123169bb4bb4Scarlsonj return (B_FALSE);
123269bb4bb4Scarlsonj switch (sa->sa_family) {
123369bb4bb4Scarlsonj case AF_INET:
123469bb4bb4Scarlsonj cp += sizeof (struct sockaddr_in);
123569bb4bb4Scarlsonj break;
123669bb4bb4Scarlsonj case AF_LINK:
123769bb4bb4Scarlsonj cp += sizeof (struct sockaddr_dl);
123869bb4bb4Scarlsonj break;
123969bb4bb4Scarlsonj case AF_INET6:
124069bb4bb4Scarlsonj cp += sizeof (struct sockaddr_in6);
124169bb4bb4Scarlsonj break;
124269bb4bb4Scarlsonj default:
124369bb4bb4Scarlsonj cp += sizeof (struct sockaddr);
124469bb4bb4Scarlsonj break;
124569bb4bb4Scarlsonj }
124669bb4bb4Scarlsonj }
124769bb4bb4Scarlsonj }
1248d04ccbb3Scarlsonj if (isv6) {
1249d04ccbb3Scarlsonj const struct sockaddr_in6 *sin6;
1250d04ccbb3Scarlsonj
1251d04ccbb3Scarlsonj /* LINTED: alignment */
1252d04ccbb3Scarlsonj sin6 = (const struct sockaddr_in6 *)cp;
1253d04ccbb3Scarlsonj if ((const char *)(sin6 + 1) > lim)
1254d04ccbb3Scarlsonj return (B_FALSE);
1255d04ccbb3Scarlsonj if (sin6->sin6_family != AF_INET6)
1256d04ccbb3Scarlsonj return (B_FALSE);
1257d04ccbb3Scarlsonj return (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, addr));
1258d04ccbb3Scarlsonj } else {
1259d04ccbb3Scarlsonj const struct sockaddr_in *sinp;
1260d04ccbb3Scarlsonj ipaddr_t v4addr;
1261d04ccbb3Scarlsonj
1262d04ccbb3Scarlsonj /* LINTED: alignment */
1263d04ccbb3Scarlsonj sinp = (const struct sockaddr_in *)cp;
1264d04ccbb3Scarlsonj if ((const char *)(sinp + 1) > lim)
1265d04ccbb3Scarlsonj return (B_FALSE);
1266d04ccbb3Scarlsonj if (sinp->sin_family != AF_INET)
1267d04ccbb3Scarlsonj return (B_FALSE);
1268d04ccbb3Scarlsonj IN6_V4MAPPED_TO_IPADDR(addr, v4addr);
1269d04ccbb3Scarlsonj return (sinp->sin_addr.s_addr == v4addr);
1270d04ccbb3Scarlsonj }
1271d04ccbb3Scarlsonj }
1272d04ccbb3Scarlsonj
1273d04ccbb3Scarlsonj /*
1274d04ccbb3Scarlsonj * is_rtm_v6(): determine if routing socket message is IPv6
1275d04ccbb3Scarlsonj *
1276d04ccbb3Scarlsonj * input: struct ifa_msghdr *: pointer to routing socket message
1277d04ccbb3Scarlsonj * int: message length
1278d04ccbb3Scarlsonj * output: boolean_t
1279d04ccbb3Scarlsonj */
1280d04ccbb3Scarlsonj
1281d04ccbb3Scarlsonj static boolean_t
is_rtm_v6(const struct ifa_msghdr * ifam,int msglen)1282d04ccbb3Scarlsonj is_rtm_v6(const struct ifa_msghdr *ifam, int msglen)
1283d04ccbb3Scarlsonj {
1284d04ccbb3Scarlsonj const char *cp, *lim;
1285d04ccbb3Scarlsonj uint_t flag;
1286d04ccbb3Scarlsonj const struct sockaddr *sa;
1287d04ccbb3Scarlsonj
1288d04ccbb3Scarlsonj cp = (const char *)(ifam + 1);
1289d04ccbb3Scarlsonj lim = (const char *)ifam + msglen;
1290d04ccbb3Scarlsonj for (flag = ifam->ifam_addrs; flag != 0; flag &= flag - 1) {
1291d04ccbb3Scarlsonj /* LINTED: alignment */
1292d04ccbb3Scarlsonj sa = (const struct sockaddr *)cp;
1293d04ccbb3Scarlsonj if ((const char *)(sa + 1) > lim)
1294d04ccbb3Scarlsonj return (B_FALSE);
1295d04ccbb3Scarlsonj switch (sa->sa_family) {
1296d04ccbb3Scarlsonj case AF_INET:
1297d04ccbb3Scarlsonj return (B_FALSE);
1298d04ccbb3Scarlsonj case AF_LINK:
1299d04ccbb3Scarlsonj cp += sizeof (struct sockaddr_dl);
1300d04ccbb3Scarlsonj break;
1301d04ccbb3Scarlsonj case AF_INET6:
1302d04ccbb3Scarlsonj return (B_TRUE);
1303d04ccbb3Scarlsonj default:
1304d04ccbb3Scarlsonj cp += sizeof (struct sockaddr);
1305d04ccbb3Scarlsonj break;
1306d04ccbb3Scarlsonj }
1307d04ccbb3Scarlsonj }
1308d04ccbb3Scarlsonj return (B_FALSE);
1309d04ccbb3Scarlsonj }
1310d04ccbb3Scarlsonj
1311d04ccbb3Scarlsonj /*
1312d04ccbb3Scarlsonj * check_lif(): check the state of a given logical interface and its DHCP
1313d04ccbb3Scarlsonj * lease. We've been told by the routing socket that the
1314d04ccbb3Scarlsonj * corresponding ifIndex has changed. This may mean that DAD has
1315d04ccbb3Scarlsonj * completed or failed.
1316d04ccbb3Scarlsonj *
1317d04ccbb3Scarlsonj * input: dhcp_lif_t *: pointer to the LIF
1318d04ccbb3Scarlsonj * const struct ifa_msghdr *: routing socket message
1319d04ccbb3Scarlsonj * int: size of routing socket message
1320d04ccbb3Scarlsonj * output: boolean_t: B_TRUE if DAD has completed on this interface
1321d04ccbb3Scarlsonj */
1322d04ccbb3Scarlsonj
1323d04ccbb3Scarlsonj static boolean_t
check_lif(dhcp_lif_t * lif,const struct ifa_msghdr * ifam,int msglen)1324d04ccbb3Scarlsonj check_lif(dhcp_lif_t *lif, const struct ifa_msghdr *ifam, int msglen)
1325d04ccbb3Scarlsonj {
1326d04ccbb3Scarlsonj boolean_t isv6, dad_wait, unplumb;
1327d04ccbb3Scarlsonj int fd;
1328d04ccbb3Scarlsonj struct lifreq lifr;
1329d04ccbb3Scarlsonj
1330d04ccbb3Scarlsonj isv6 = lif->lif_pif->pif_isv6;
1331d04ccbb3Scarlsonj fd = isv6 ? v6_sock_fd : v4_sock_fd;
1332d04ccbb3Scarlsonj
1333d04ccbb3Scarlsonj /*
1334d04ccbb3Scarlsonj * Get the real (64 bit) logical interface flags. Note that the
1335d04ccbb3Scarlsonj * routing socket message has flags, but these are just the lower 32
1336d04ccbb3Scarlsonj * bits.
1337d04ccbb3Scarlsonj */
1338d04ccbb3Scarlsonj unplumb = B_FALSE;
1339d04ccbb3Scarlsonj (void) memset(&lifr, 0, sizeof (lifr));
1340d04ccbb3Scarlsonj (void) strlcpy(lifr.lifr_name, lif->lif_name, sizeof (lifr.lifr_name));
1341d04ccbb3Scarlsonj if (ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) {
1342d04ccbb3Scarlsonj /*
1343d04ccbb3Scarlsonj * Failing to retrieve flags means that the interface is gone.
1344d04ccbb3Scarlsonj * It hasn't failed to verify with DAD, but we still have to
1345d04ccbb3Scarlsonj * give up on it.
1346d04ccbb3Scarlsonj */
1347d04ccbb3Scarlsonj lifr.lifr_flags = 0;
1348d04ccbb3Scarlsonj if (errno == ENXIO) {
1349d04ccbb3Scarlsonj lif->lif_plumbed = B_FALSE;
1350d04ccbb3Scarlsonj dhcpmsg(MSG_INFO, "%s has been removed; abandoning",
1351d04ccbb3Scarlsonj lif->lif_name);
1352cfb9c9abScarlsonj if (!isv6)
1353cfb9c9abScarlsonj discard_default_routes(lif->lif_smachs);
1354d04ccbb3Scarlsonj } else {
1355d04ccbb3Scarlsonj dhcpmsg(MSG_ERR,
1356d04ccbb3Scarlsonj "unable to retrieve interface flags on %s",
1357d04ccbb3Scarlsonj lif->lif_name);
1358d04ccbb3Scarlsonj }
1359d04ccbb3Scarlsonj unplumb = B_TRUE;
1360d04ccbb3Scarlsonj } else if (!check_rtm_addr(ifam, msglen, isv6, &lif->lif_v6addr)) {
1361d04ccbb3Scarlsonj /*
1362d04ccbb3Scarlsonj * If the message is not about this logical interface,
1363d04ccbb3Scarlsonj * then just ignore it.
1364d04ccbb3Scarlsonj */
136569bb4bb4Scarlsonj return (B_FALSE);
1366d04ccbb3Scarlsonj } else if (lifr.lifr_flags & IFF_DUPLICATE) {
1367d04ccbb3Scarlsonj dhcpmsg(MSG_ERROR, "interface %s has duplicate address",
1368d04ccbb3Scarlsonj lif->lif_name);
1369d04ccbb3Scarlsonj lif_mark_decline(lif, "duplicate address");
1370d04ccbb3Scarlsonj close_ip_lif(lif);
1371e11c3f44Smeem (void) open_ip_lif(lif, INADDR_ANY, B_TRUE);
1372d04ccbb3Scarlsonj }
1373d04ccbb3Scarlsonj
1374d04ccbb3Scarlsonj dad_wait = lif->lif_dad_wait;
1375d04ccbb3Scarlsonj if (dad_wait) {
1376d04ccbb3Scarlsonj dhcpmsg(MSG_VERBOSE, "check_lif: %s has finished DAD",
1377d04ccbb3Scarlsonj lif->lif_name);
1378d04ccbb3Scarlsonj lif->lif_dad_wait = B_FALSE;
1379d04ccbb3Scarlsonj }
1380d04ccbb3Scarlsonj
1381d04ccbb3Scarlsonj if (unplumb)
1382d04ccbb3Scarlsonj unplumb_lif(lif);
1383d04ccbb3Scarlsonj
1384d04ccbb3Scarlsonj return (dad_wait);
1385d04ccbb3Scarlsonj }
1386d04ccbb3Scarlsonj
1387d04ccbb3Scarlsonj /*
1388d04ccbb3Scarlsonj * check_main_lif(): check the state of a main logical interface for a state
1389d04ccbb3Scarlsonj * machine. This is used only for DHCPv6.
1390d04ccbb3Scarlsonj *
1391d04ccbb3Scarlsonj * input: dhcp_smach_t *: pointer to the state machine
1392d04ccbb3Scarlsonj * const struct ifa_msghdr *: routing socket message
1393d04ccbb3Scarlsonj * int: size of routing socket message
1394d04ccbb3Scarlsonj * output: boolean_t: B_TRUE if LIF is ok.
1395d04ccbb3Scarlsonj */
1396d04ccbb3Scarlsonj
1397d04ccbb3Scarlsonj static boolean_t
check_main_lif(dhcp_smach_t * dsmp,const struct ifa_msghdr * ifam,int msglen)1398d04ccbb3Scarlsonj check_main_lif(dhcp_smach_t *dsmp, const struct ifa_msghdr *ifam, int msglen)
1399d04ccbb3Scarlsonj {
1400d04ccbb3Scarlsonj dhcp_lif_t *lif = dsmp->dsm_lif;
1401d04ccbb3Scarlsonj struct lifreq lifr;
1402d04ccbb3Scarlsonj
1403d04ccbb3Scarlsonj /*
1404d04ccbb3Scarlsonj * Get the real (64 bit) logical interface flags. Note that the
1405d04ccbb3Scarlsonj * routing socket message has flags, but these are just the lower 32
1406d04ccbb3Scarlsonj * bits.
1407d04ccbb3Scarlsonj */
1408d04ccbb3Scarlsonj (void) memset(&lifr, 0, sizeof (lifr));
1409d04ccbb3Scarlsonj (void) strlcpy(lifr.lifr_name, lif->lif_name, sizeof (lifr.lifr_name));
1410d04ccbb3Scarlsonj if (ioctl(v6_sock_fd, SIOCGLIFFLAGS, &lifr) == -1) {
1411d04ccbb3Scarlsonj /*
1412d04ccbb3Scarlsonj * Failing to retrieve flags means that the interface is gone.
1413d04ccbb3Scarlsonj * Our state machine is now trash.
1414d04ccbb3Scarlsonj */
1415d04ccbb3Scarlsonj if (errno == ENXIO) {
1416d04ccbb3Scarlsonj dhcpmsg(MSG_INFO, "%s has been removed; abandoning",
1417d04ccbb3Scarlsonj lif->lif_name);
1418d04ccbb3Scarlsonj } else {
1419d04ccbb3Scarlsonj dhcpmsg(MSG_ERR,
1420d04ccbb3Scarlsonj "unable to retrieve interface flags on %s",
1421d04ccbb3Scarlsonj lif->lif_name);
1422d04ccbb3Scarlsonj }
1423d04ccbb3Scarlsonj return (B_FALSE);
1424d04ccbb3Scarlsonj } else if (!check_rtm_addr(ifam, msglen, B_TRUE, &lif->lif_v6addr)) {
1425d04ccbb3Scarlsonj /*
1426d04ccbb3Scarlsonj * If the message is not about this logical interface,
1427d04ccbb3Scarlsonj * then just ignore it.
1428d04ccbb3Scarlsonj */
1429d04ccbb3Scarlsonj return (B_TRUE);
1430d04ccbb3Scarlsonj } else if (lifr.lifr_flags & IFF_DUPLICATE) {
1431d04ccbb3Scarlsonj dhcpmsg(MSG_ERROR, "interface %s has duplicate address",
1432d04ccbb3Scarlsonj lif->lif_name);
1433d04ccbb3Scarlsonj return (B_FALSE);
1434d04ccbb3Scarlsonj } else {
1435d04ccbb3Scarlsonj return (B_TRUE);
1436d04ccbb3Scarlsonj }
1437d04ccbb3Scarlsonj }
1438d04ccbb3Scarlsonj
1439d04ccbb3Scarlsonj /*
1440d04ccbb3Scarlsonj * process_link_up_down(): check the state of a physical interface for up/down
1441d04ccbb3Scarlsonj * transitions; must go through INIT_REBOOT state if
1442d04ccbb3Scarlsonj * the link flaps.
1443d04ccbb3Scarlsonj *
1444d04ccbb3Scarlsonj * input: dhcp_pif_t *: pointer to the physical interface to check
1445d04ccbb3Scarlsonj * const struct if_msghdr *: routing socket message
1446d04ccbb3Scarlsonj * output: none
1447d04ccbb3Scarlsonj */
1448d04ccbb3Scarlsonj
1449d04ccbb3Scarlsonj static void
process_link_up_down(dhcp_pif_t * pif,const struct if_msghdr * ifm)1450d04ccbb3Scarlsonj process_link_up_down(dhcp_pif_t *pif, const struct if_msghdr *ifm)
1451d04ccbb3Scarlsonj {
1452d04ccbb3Scarlsonj struct lifreq lifr;
1453d04ccbb3Scarlsonj boolean_t isv6;
1454d04ccbb3Scarlsonj int fd;
1455d04ccbb3Scarlsonj
1456d04ccbb3Scarlsonj /*
1457d04ccbb3Scarlsonj * If the message implies no change of flags, then we're done; no need
1458d04ccbb3Scarlsonj * to check further. Note that if we have multiple state machines on a
1459d04ccbb3Scarlsonj * single physical interface, this test keeps us from issuing an ioctl
1460d04ccbb3Scarlsonj * for each one.
1461d04ccbb3Scarlsonj */
1462d04ccbb3Scarlsonj if ((ifm->ifm_flags & IFF_RUNNING) && pif->pif_running ||
1463d04ccbb3Scarlsonj !(ifm->ifm_flags & IFF_RUNNING) && !pif->pif_running)
1464d04ccbb3Scarlsonj return;
1465d04ccbb3Scarlsonj
1466d04ccbb3Scarlsonj /*
1467d04ccbb3Scarlsonj * We don't know what the real interface flags are, because the
1468d04ccbb3Scarlsonj * if_index number is only 16 bits; we must go ask.
1469d04ccbb3Scarlsonj */
1470d04ccbb3Scarlsonj isv6 = pif->pif_isv6;
1471d04ccbb3Scarlsonj fd = isv6 ? v6_sock_fd : v4_sock_fd;
1472d04ccbb3Scarlsonj (void) memset(&lifr, 0, sizeof (lifr));
1473d04ccbb3Scarlsonj (void) strlcpy(lifr.lifr_name, pif->pif_name, sizeof (lifr.lifr_name));
1474d04ccbb3Scarlsonj
1475d04ccbb3Scarlsonj if (ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1 ||
1476d04ccbb3Scarlsonj !(lifr.lifr_flags & IFF_RUNNING)) {
1477d04ccbb3Scarlsonj /*
1478d04ccbb3Scarlsonj * If we've lost the interface or it has gone down, then
1479d04ccbb3Scarlsonj * nothing special to do; just turn off the running flag.
1480d04ccbb3Scarlsonj */
1481d04ccbb3Scarlsonj pif_status(pif, B_FALSE);
1482d04ccbb3Scarlsonj } else {
1483d04ccbb3Scarlsonj /*
1484d04ccbb3Scarlsonj * Interface has come back up: go through verification process.
1485d04ccbb3Scarlsonj */
1486d04ccbb3Scarlsonj pif_status(pif, B_TRUE);
1487d04ccbb3Scarlsonj }
148869bb4bb4Scarlsonj }
148969bb4bb4Scarlsonj
149069bb4bb4Scarlsonj /*
149169bb4bb4Scarlsonj * rtsock_event(): fetches routing socket messages and updates internal
149269bb4bb4Scarlsonj * interface state based on those messages.
149369bb4bb4Scarlsonj *
149469bb4bb4Scarlsonj * input: iu_eh_t *: unused
149569bb4bb4Scarlsonj * int: the routing socket file descriptor
149669bb4bb4Scarlsonj * (other arguments unused)
149769bb4bb4Scarlsonj * output: void
149869bb4bb4Scarlsonj */
149969bb4bb4Scarlsonj
150069bb4bb4Scarlsonj /* ARGSUSED */
150169bb4bb4Scarlsonj static void
rtsock_event(iu_eh_t * ehp,int fd,short events,iu_event_id_t id,void * arg)150269bb4bb4Scarlsonj rtsock_event(iu_eh_t *ehp, int fd, short events, iu_event_id_t id, void *arg)
150369bb4bb4Scarlsonj {
1504d04ccbb3Scarlsonj dhcp_smach_t *dsmp, *dsmnext;
150569bb4bb4Scarlsonj union {
150669bb4bb4Scarlsonj struct ifa_msghdr ifam;
1507d04ccbb3Scarlsonj struct if_msghdr ifm;
150869bb4bb4Scarlsonj char buf[1024];
150969bb4bb4Scarlsonj } msg;
151069bb4bb4Scarlsonj uint16_t ifindex;
151169bb4bb4Scarlsonj int msglen;
1512d04ccbb3Scarlsonj boolean_t isv6;
151369bb4bb4Scarlsonj
151469bb4bb4Scarlsonj if ((msglen = read(fd, &msg, sizeof (msg))) <= 0)
151569bb4bb4Scarlsonj return;
151669bb4bb4Scarlsonj
1517d04ccbb3Scarlsonj /* Note that the routing socket interface index is just 16 bits */
1518d04ccbb3Scarlsonj if (msg.ifm.ifm_type == RTM_IFINFO) {
1519d04ccbb3Scarlsonj ifindex = msg.ifm.ifm_index;
1520d04ccbb3Scarlsonj isv6 = (msg.ifm.ifm_flags & IFF_IPV6) ? B_TRUE : B_FALSE;
1521d04ccbb3Scarlsonj } else if (msg.ifam.ifam_type == RTM_DELADDR ||
1522d04ccbb3Scarlsonj msg.ifam.ifam_type == RTM_NEWADDR) {
1523d04ccbb3Scarlsonj ifindex = msg.ifam.ifam_index;
1524d04ccbb3Scarlsonj isv6 = is_rtm_v6(&msg.ifam, msglen);
1525d04ccbb3Scarlsonj } else {
152669bb4bb4Scarlsonj return;
1527d04ccbb3Scarlsonj }
152869bb4bb4Scarlsonj
1529d04ccbb3Scarlsonj for (dsmp = lookup_smach_by_uindex(ifindex, NULL, isv6);
1530d04ccbb3Scarlsonj dsmp != NULL; dsmp = dsmnext) {
1531d04ccbb3Scarlsonj DHCPSTATE oldstate;
1532d04ccbb3Scarlsonj boolean_t lif_finished;
1533d04ccbb3Scarlsonj boolean_t lease_removed;
1534d04ccbb3Scarlsonj dhcp_lease_t *dlp, *dlnext;
153569bb4bb4Scarlsonj
1536d04ccbb3Scarlsonj /*
1537d04ccbb3Scarlsonj * Note that script_start can call dhcp_drop directly, and
1538d04ccbb3Scarlsonj * that will do release_smach.
1539d04ccbb3Scarlsonj */
1540d04ccbb3Scarlsonj dsmnext = lookup_smach_by_uindex(ifindex, dsmp, isv6);
1541d04ccbb3Scarlsonj oldstate = dsmp->dsm_state;
154269bb4bb4Scarlsonj
15438ff86213Scarlsonj /*
15448ff86213Scarlsonj * Ignore state machines that are currently processing drop or
15458ff86213Scarlsonj * release; there is nothing more we can do for them.
15468ff86213Scarlsonj */
15478ff86213Scarlsonj if (dsmp->dsm_droprelease)
15488ff86213Scarlsonj continue;
15498ff86213Scarlsonj
155069bb4bb4Scarlsonj /*
1551d04ccbb3Scarlsonj * Look for link up/down notifications. These occur on a
1552d04ccbb3Scarlsonj * physical interface basis.
155369bb4bb4Scarlsonj */
1554d04ccbb3Scarlsonj if (msg.ifm.ifm_type == RTM_IFINFO) {
1555d04ccbb3Scarlsonj process_link_up_down(dsmp->dsm_lif->lif_pif, &msg.ifm);
155669bb4bb4Scarlsonj continue;
1557d04ccbb3Scarlsonj }
155869bb4bb4Scarlsonj
155969bb4bb4Scarlsonj /*
156069bb4bb4Scarlsonj * Since we cannot trust the flags reported by the routing
156169bb4bb4Scarlsonj * socket (they're just 32 bits -- and thus never include
156269bb4bb4Scarlsonj * IFF_DUPLICATE), and we can't trust the ifindex (it's only 16
156369bb4bb4Scarlsonj * bits and also doesn't reflect the alias in use), we get
156469bb4bb4Scarlsonj * flags on all matching interfaces, and go by that.
156569bb4bb4Scarlsonj */
1566d04ccbb3Scarlsonj lif_finished = B_FALSE;
1567d04ccbb3Scarlsonj lease_removed = B_FALSE;
1568d04ccbb3Scarlsonj for (dlp = dsmp->dsm_leases; dlp != NULL; dlp = dlnext) {
1569d04ccbb3Scarlsonj dhcp_lif_t *lif, *lifnext;
1570d04ccbb3Scarlsonj uint_t nlifs = dlp->dl_nlifs;
1571d04ccbb3Scarlsonj
1572d04ccbb3Scarlsonj dlnext = dlp->dl_next;
1573d04ccbb3Scarlsonj for (lif = dlp->dl_lifs; lif != NULL && nlifs > 0;
1574d04ccbb3Scarlsonj lif = lifnext, nlifs--) {
1575d04ccbb3Scarlsonj lifnext = lif->lif_next;
1576d04ccbb3Scarlsonj if (check_lif(lif, &msg.ifam, msglen)) {
1577d04ccbb3Scarlsonj dsmp->dsm_lif_wait--;
1578d04ccbb3Scarlsonj lif_finished = B_TRUE;
1579d04ccbb3Scarlsonj }
1580d04ccbb3Scarlsonj }
1581d04ccbb3Scarlsonj if (dlp->dl_nlifs == 0) {
1582d04ccbb3Scarlsonj remove_lease(dlp);
1583d04ccbb3Scarlsonj lease_removed = B_TRUE;
1584dc041e83Scarlsonj }
158569bb4bb4Scarlsonj }
158669bb4bb4Scarlsonj
1587d04ccbb3Scarlsonj if ((isv6 && !check_main_lif(dsmp, &msg.ifam, msglen)) ||
1588d04ccbb3Scarlsonj (!isv6 && !verify_lif(dsmp->dsm_lif))) {
15890a3e1f6cSVasumathi Sundaram finished_smach(dsmp, DHCP_IPC_E_INVIF);
1590d04ccbb3Scarlsonj continue;
159169bb4bb4Scarlsonj }
159269bb4bb4Scarlsonj
159369bb4bb4Scarlsonj /*
1594d04ccbb3Scarlsonj * Ignore this state machine if nothing interesting has
1595d04ccbb3Scarlsonj * happened.
1596d04ccbb3Scarlsonj */
1597d04ccbb3Scarlsonj if (!lif_finished && dsmp->dsm_lif_down == 0 &&
1598d04ccbb3Scarlsonj (dsmp->dsm_leases != NULL || !lease_removed))
1599d04ccbb3Scarlsonj continue;
1600d04ccbb3Scarlsonj
1601d04ccbb3Scarlsonj /*
1602d04ccbb3Scarlsonj * If we're still waiting for DAD to complete on some of the
1603d04ccbb3Scarlsonj * configured LIFs, then don't send a response.
160469bb4bb4Scarlsonj */
1605d04ccbb3Scarlsonj if (dsmp->dsm_lif_wait != 0) {
1606d04ccbb3Scarlsonj dhcpmsg(MSG_VERBOSE, "rtsock_event: %s still has %d "
1607d04ccbb3Scarlsonj "LIFs waiting on DAD", dsmp->dsm_name,
1608d04ccbb3Scarlsonj dsmp->dsm_lif_wait);
1609d04ccbb3Scarlsonj continue;
1610d04ccbb3Scarlsonj }
161169bb4bb4Scarlsonj
1612d04ccbb3Scarlsonj /*
1613d04ccbb3Scarlsonj * If we have some failed LIFs, then handle them now. We'll
1614d04ccbb3Scarlsonj * remove them from the list. Any leases that become empty are
1615d04ccbb3Scarlsonj * also removed as part of the decline-generation process.
1616d04ccbb3Scarlsonj */
1617e704a8f2Smeem if (dsmp->dsm_lif_down != 0)
1618d04ccbb3Scarlsonj send_declines(dsmp);
161969bb4bb4Scarlsonj
1620d04ccbb3Scarlsonj if (dsmp->dsm_leases == NULL) {
1621d04ccbb3Scarlsonj dsmp->dsm_bad_offers++;
1622d04ccbb3Scarlsonj /*
1623d04ccbb3Scarlsonj * For DHCPv6, we'll process the restart once we're
1624d04ccbb3Scarlsonj * done sending Decline messages, because these are
1625d04ccbb3Scarlsonj * supposed to be acknowledged. With DHCPv4, there's
1626d04ccbb3Scarlsonj * no acknowledgment for a DECLINE, so after sending
1627d04ccbb3Scarlsonj * it, we just restart right away.
1628d04ccbb3Scarlsonj */
1629d04ccbb3Scarlsonj if (!dsmp->dsm_isv6) {
1630d04ccbb3Scarlsonj dhcpmsg(MSG_VERBOSE, "rtsock_event: %s has no "
1631906cb642Scarlsonj "LIFs left", dsmp->dsm_name);
1632d04ccbb3Scarlsonj dhcp_restart(dsmp);
1633d04ccbb3Scarlsonj }
1634d04ccbb3Scarlsonj } else {
1635d04ccbb3Scarlsonj /*
1636d04ccbb3Scarlsonj * If we're now up on at least some of the leases and
1637d04ccbb3Scarlsonj * we were waiting for that, then kick off the rest of
1638d04ccbb3Scarlsonj * configuration. Lease validation and DAD are done.
1639d04ccbb3Scarlsonj */
1640d04ccbb3Scarlsonj dhcpmsg(MSG_VERBOSE, "rtsock_event: all LIFs verified "
1641d04ccbb3Scarlsonj "on %s in %s state", dsmp->dsm_name,
1642d04ccbb3Scarlsonj dhcp_state_to_string(oldstate));
1643d04ccbb3Scarlsonj if (oldstate == PRE_BOUND ||
1644d04ccbb3Scarlsonj oldstate == ADOPTING)
1645d04ccbb3Scarlsonj dhcp_bound_complete(dsmp);
1646d04ccbb3Scarlsonj if (oldstate == ADOPTING)
1647d04ccbb3Scarlsonj dhcp_adopt_complete(dsmp);
1648d04ccbb3Scarlsonj }
164969bb4bb4Scarlsonj }
165069bb4bb4Scarlsonj }
1651d04ccbb3Scarlsonj
1652d04ccbb3Scarlsonj /*
1653d04ccbb3Scarlsonj * check_cmd_allowed(): check whether the requested command is allowed in the
1654d04ccbb3Scarlsonj * state specified.
1655d04ccbb3Scarlsonj *
1656d04ccbb3Scarlsonj * input: DHCPSTATE: current state
1657d04ccbb3Scarlsonj * dhcp_ipc_type_t: requested command
1658d04ccbb3Scarlsonj * output: boolean_t: B_TRUE if command is allowed in this state
1659d04ccbb3Scarlsonj */
1660d04ccbb3Scarlsonj
1661d04ccbb3Scarlsonj boolean_t
check_cmd_allowed(DHCPSTATE state,dhcp_ipc_type_t cmd)1662d04ccbb3Scarlsonj check_cmd_allowed(DHCPSTATE state, dhcp_ipc_type_t cmd)
1663d04ccbb3Scarlsonj {
1664d04ccbb3Scarlsonj return (ipc_cmd_allowed[state][cmd] != 0);
1665d04ccbb3Scarlsonj }
16666cefaae1SJack Meng
16676cefaae1SJack Meng static boolean_t
is_iscsi_active(void)16686cefaae1SJack Meng is_iscsi_active(void)
16696cefaae1SJack Meng {
16703644994cSmeem int fd;
16713644994cSmeem int active = 0;
16726cefaae1SJack Meng
16733644994cSmeem if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) != -1) {
16743644994cSmeem if (ioctl(fd, ISCSI_IS_ACTIVE, &active) != 0)
16753644994cSmeem active = 0;
16763644994cSmeem (void) close(fd);
16776cefaae1SJack Meng }
16786cefaae1SJack Meng
16793644994cSmeem return (active != 0);
16806cefaae1SJack Meng }
1681