10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51914Scasper * Common Development and Distribution License (the "License").
61914Scasper * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*12989SVasumathi.Sundaram@oracle.COM * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate #include <sys/types.h>
260Sstevel@tonic-gate #include <stdlib.h>
279508SPeter.Memishian@Sun.COM #include <assert.h>
280Sstevel@tonic-gate #include <errno.h>
290Sstevel@tonic-gate #include <locale.h>
300Sstevel@tonic-gate #include <string.h>
310Sstevel@tonic-gate #include <unistd.h>
320Sstevel@tonic-gate #include <signal.h>
330Sstevel@tonic-gate #include <stdio.h>
341914Scasper #include <stdio_ext.h>
350Sstevel@tonic-gate #include <dhcp_hostconf.h>
360Sstevel@tonic-gate #include <dhcpagent_ipc.h>
373431Scarlsonj #include <dhcpagent_util.h>
380Sstevel@tonic-gate #include <dhcpmsg.h>
390Sstevel@tonic-gate #include <netinet/dhcp.h>
402546Scarlsonj #include <net/route.h>
412546Scarlsonj #include <sys/sockio.h>
428194SJack.Meng@Sun.COM #include <sys/stat.h>
438194SJack.Meng@Sun.COM #include <stropts.h>
448194SJack.Meng@Sun.COM #include <fcntl.h>
458194SJack.Meng@Sun.COM #include <sys/scsi/adapters/iscsi_if.h>
460Sstevel@tonic-gate
470Sstevel@tonic-gate #include "async.h"
480Sstevel@tonic-gate #include "agent.h"
490Sstevel@tonic-gate #include "script_handler.h"
500Sstevel@tonic-gate #include "util.h"
510Sstevel@tonic-gate #include "class_id.h"
520Sstevel@tonic-gate #include "states.h"
530Sstevel@tonic-gate #include "packet.h"
543431Scarlsonj #include "interface.h"
553431Scarlsonj #include "defaults.h"
560Sstevel@tonic-gate
570Sstevel@tonic-gate #ifndef TEXT_DOMAIN
580Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
590Sstevel@tonic-gate #endif
600Sstevel@tonic-gate
610Sstevel@tonic-gate iu_timer_id_t inactivity_id;
620Sstevel@tonic-gate int class_id_len = 0;
630Sstevel@tonic-gate char *class_id;
640Sstevel@tonic-gate iu_eh_t *eh;
650Sstevel@tonic-gate iu_tq_t *tq;
660Sstevel@tonic-gate pid_t grandparent;
672546Scarlsonj int rtsock_fd;
680Sstevel@tonic-gate
690Sstevel@tonic-gate static boolean_t shutdown_started = B_FALSE;
700Sstevel@tonic-gate static boolean_t do_adopt = B_FALSE;
710Sstevel@tonic-gate static unsigned int debug_level = 0;
722546Scarlsonj static iu_eh_callback_t accept_event, ipc_event, rtsock_event;
730Sstevel@tonic-gate
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate * The ipc_cmd_allowed[] table indicates which IPC commands are allowed in
760Sstevel@tonic-gate * which states; a non-zero value indicates the command is permitted.
770Sstevel@tonic-gate *
783431Scarlsonj * START is permitted if the state machine is fresh, or if we are in the
793431Scarlsonj * process of trying to obtain a lease (as a convenience to save the
803431Scarlsonj * administrator from having to do an explicit DROP). EXTEND, RELEASE, and
813431Scarlsonj * GET_TAG require a lease to be obtained in order to make sense. INFORM is
823431Scarlsonj * permitted if the interface is fresh or has an INFORM in progress or
833431Scarlsonj * previously done on it -- otherwise a DROP or RELEASE is first required.
843431Scarlsonj * PING and STATUS always make sense and thus are always permitted, as is DROP
853431Scarlsonj * in order to permit the administrator to always bail out.
860Sstevel@tonic-gate */
870Sstevel@tonic-gate static int ipc_cmd_allowed[DHCP_NSTATES][DHCP_NIPC] = {
880Sstevel@tonic-gate /* D E P R S S I G */
890Sstevel@tonic-gate /* R X I E T T N E */
900Sstevel@tonic-gate /* O T N L A A F T */
910Sstevel@tonic-gate /* P E G E R T O _ */
920Sstevel@tonic-gate /* . N . A T U R T */
930Sstevel@tonic-gate /* . D . S . S M A */
940Sstevel@tonic-gate /* . . . E . . . G */
950Sstevel@tonic-gate /* INIT */ { 1, 0, 1, 0, 1, 1, 1, 0 },
960Sstevel@tonic-gate /* SELECTING */ { 1, 0, 1, 0, 1, 1, 0, 0 },
970Sstevel@tonic-gate /* REQUESTING */ { 1, 0, 1, 0, 1, 1, 0, 0 },
982546Scarlsonj /* PRE_BOUND */ { 1, 1, 1, 1, 0, 1, 0, 1 },
990Sstevel@tonic-gate /* BOUND */ { 1, 1, 1, 1, 0, 1, 0, 1 },
1000Sstevel@tonic-gate /* RENEWING */ { 1, 1, 1, 1, 0, 1, 0, 1 },
1010Sstevel@tonic-gate /* REBINDING */ { 1, 1, 1, 1, 0, 1, 0, 1 },
1023431Scarlsonj /* INFORMATION */ { 1, 0, 1, 0, 1, 1, 1, 1 },
1033431Scarlsonj /* INIT_REBOOT */ { 1, 0, 1, 1, 1, 1, 0, 0 },
1043431Scarlsonj /* ADOPTING */ { 1, 0, 1, 1, 0, 1, 0, 0 },
1053431Scarlsonj /* INFORM_SENT */ { 1, 0, 1, 0, 1, 1, 1, 0 },
1063431Scarlsonj /* DECLINING */ { 1, 1, 1, 1, 0, 1, 0, 1 },
1073431Scarlsonj /* RELEASING */ { 1, 0, 1, 0, 0, 1, 0, 1 },
1083431Scarlsonj };
1093431Scarlsonj
1103431Scarlsonj #define CMD_ISPRIV 0x1 /* Command requires privileges */
1113431Scarlsonj #define CMD_CREATE 0x2 /* Command creates an interface */
1123431Scarlsonj #define CMD_BOOTP 0x4 /* Command is valid with BOOTP */
1133431Scarlsonj #define CMD_IMMED 0x8 /* Reply is immediate (no BUSY state) */
1143431Scarlsonj
1153431Scarlsonj static uint_t ipc_cmd_flags[DHCP_NIPC] = {
1163431Scarlsonj /* DHCP_DROP */ CMD_ISPRIV|CMD_BOOTP,
1173431Scarlsonj /* DHCP_EXTEND */ CMD_ISPRIV,
1183431Scarlsonj /* DHCP_PING */ CMD_BOOTP|CMD_IMMED,
1193431Scarlsonj /* DHCP_RELEASE */ CMD_ISPRIV,
1203431Scarlsonj /* DHCP_START */ CMD_CREATE|CMD_ISPRIV|CMD_BOOTP,
1213431Scarlsonj /* DHCP_STATUS */ CMD_BOOTP|CMD_IMMED,
1223431Scarlsonj /* DHCP_INFORM */ CMD_CREATE|CMD_ISPRIV,
1233431Scarlsonj /* DHCP_GET_TAG */ CMD_BOOTP|CMD_IMMED
1240Sstevel@tonic-gate };
1250Sstevel@tonic-gate
1269508SPeter.Memishian@Sun.COM static boolean_t is_iscsi_active(void);
1278194SJack.Meng@Sun.COM
1280Sstevel@tonic-gate int
main(int argc,char ** argv)1290Sstevel@tonic-gate main(int argc, char **argv)
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate boolean_t is_daemon = B_TRUE;
1323431Scarlsonj boolean_t is_verbose;
1330Sstevel@tonic-gate int ipc_fd;
1340Sstevel@tonic-gate int c;
1358485SPeter.Memishian@Sun.COM int aware = RTAW_UNDER_IPMP;
1360Sstevel@tonic-gate struct rlimit rl;
1370Sstevel@tonic-gate
1383431Scarlsonj debug_level = df_get_int("", B_FALSE, DF_DEBUG_LEVEL);
1393431Scarlsonj is_verbose = df_get_bool("", B_FALSE, DF_VERBOSE);
1403431Scarlsonj
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate * -l is ignored for compatibility with old agent.
1430Sstevel@tonic-gate */
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate while ((c = getopt(argc, argv, "vd:l:fa")) != EOF) {
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate switch (c) {
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate case 'a':
1500Sstevel@tonic-gate do_adopt = B_TRUE;
1510Sstevel@tonic-gate grandparent = getpid();
1520Sstevel@tonic-gate break;
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate case 'd':
1550Sstevel@tonic-gate debug_level = strtoul(optarg, NULL, 0);
1560Sstevel@tonic-gate break;
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate case 'f':
1590Sstevel@tonic-gate is_daemon = B_FALSE;
1600Sstevel@tonic-gate break;
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate case 'v':
1630Sstevel@tonic-gate is_verbose = B_TRUE;
1640Sstevel@tonic-gate break;
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate case '?':
1670Sstevel@tonic-gate (void) fprintf(stderr, "usage: %s [-a] [-d n] [-f] [-v]"
1680Sstevel@tonic-gate "\n", argv[0]);
1690Sstevel@tonic-gate return (EXIT_FAILURE);
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate default:
1720Sstevel@tonic-gate break;
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1770Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate if (geteuid() != 0) {
1800Sstevel@tonic-gate dhcpmsg_init(argv[0], B_FALSE, is_verbose, debug_level);
1810Sstevel@tonic-gate dhcpmsg(MSG_ERROR, "must be super-user");
1820Sstevel@tonic-gate dhcpmsg_fini();
1830Sstevel@tonic-gate return (EXIT_FAILURE);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate if (is_daemon && daemonize() == 0) {
1870Sstevel@tonic-gate dhcpmsg_init(argv[0], B_FALSE, is_verbose, debug_level);
1880Sstevel@tonic-gate dhcpmsg(MSG_ERR, "cannot become daemon, exiting");
1890Sstevel@tonic-gate dhcpmsg_fini();
1900Sstevel@tonic-gate return (EXIT_FAILURE);
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate
1933431Scarlsonj /*
1943431Scarlsonj * Seed the random number generator, since we're going to need it
1953431Scarlsonj * to set transaction id's and for exponential backoff.
1963431Scarlsonj */
1973431Scarlsonj srand48(gethrtime() ^ gethostid() ^ getpid());
1983431Scarlsonj
1990Sstevel@tonic-gate dhcpmsg_init(argv[0], is_daemon, is_verbose, debug_level);
2000Sstevel@tonic-gate (void) atexit(dhcpmsg_fini);
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate tq = iu_tq_create();
2030Sstevel@tonic-gate eh = iu_eh_create();
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate if (eh == NULL || tq == NULL) {
2060Sstevel@tonic-gate errno = ENOMEM;
2070Sstevel@tonic-gate dhcpmsg(MSG_ERR, "cannot create timer queue or event handler");
2080Sstevel@tonic-gate return (EXIT_FAILURE);
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate /*
2120Sstevel@tonic-gate * ignore most signals that could be reasonably generated.
2130Sstevel@tonic-gate */
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate (void) signal(SIGTERM, graceful_shutdown);
2160Sstevel@tonic-gate (void) signal(SIGQUIT, graceful_shutdown);
2170Sstevel@tonic-gate (void) signal(SIGPIPE, SIG_IGN);
2180Sstevel@tonic-gate (void) signal(SIGUSR1, SIG_IGN);
2190Sstevel@tonic-gate (void) signal(SIGUSR2, SIG_IGN);
2200Sstevel@tonic-gate (void) signal(SIGINT, SIG_IGN);
2210Sstevel@tonic-gate (void) signal(SIGHUP, SIG_IGN);
2220Sstevel@tonic-gate (void) signal(SIGCHLD, SIG_IGN);
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate /*
2250Sstevel@tonic-gate * upon SIGTHAW we need to refresh any non-infinite leases.
2260Sstevel@tonic-gate */
2270Sstevel@tonic-gate
2283431Scarlsonj (void) iu_eh_register_signal(eh, SIGTHAW, refresh_smachs, NULL);
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate class_id = get_class_id();
2310Sstevel@tonic-gate if (class_id != NULL)
2320Sstevel@tonic-gate class_id_len = strlen(class_id);
2330Sstevel@tonic-gate else
2340Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "get_class_id failed, continuing "
2350Sstevel@tonic-gate "with no vendor class id");
2360Sstevel@tonic-gate
2370Sstevel@tonic-gate /*
2380Sstevel@tonic-gate * the inactivity timer is enabled any time there are no
2390Sstevel@tonic-gate * interfaces under DHCP control. if DHCP_INACTIVITY_WAIT
2400Sstevel@tonic-gate * seconds transpire without an interface under DHCP control,
2410Sstevel@tonic-gate * the agent shuts down.
2420Sstevel@tonic-gate */
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate inactivity_id = iu_schedule_timer(tq, DHCP_INACTIVITY_WAIT,
2450Sstevel@tonic-gate inactivity_shutdown, NULL);
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate /*
2480Sstevel@tonic-gate * max out the number available descriptors, just in case..
2490Sstevel@tonic-gate */
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate rl.rlim_cur = RLIM_INFINITY;
2520Sstevel@tonic-gate rl.rlim_max = RLIM_INFINITY;
2530Sstevel@tonic-gate if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
2540Sstevel@tonic-gate dhcpmsg(MSG_ERR, "setrlimit failed");
2550Sstevel@tonic-gate
2561914Scasper (void) enable_extended_FILE_stdio(-1, -1);
2571914Scasper
2580Sstevel@tonic-gate /*
2593431Scarlsonj * Create and bind default IP sockets used to control interfaces and to
2603431Scarlsonj * catch stray packets.
2613431Scarlsonj */
2623431Scarlsonj
2633431Scarlsonj if (!dhcp_ip_default())
2643431Scarlsonj return (EXIT_FAILURE);
2653431Scarlsonj
2663431Scarlsonj /*
2670Sstevel@tonic-gate * create the ipc channel that the agent will listen for
2680Sstevel@tonic-gate * requests on, and register it with the event handler so that
2690Sstevel@tonic-gate * `accept_event' will be called back.
2700Sstevel@tonic-gate */
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate switch (dhcp_ipc_init(&ipc_fd)) {
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate case 0:
2750Sstevel@tonic-gate break;
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate case DHCP_IPC_E_BIND:
2780Sstevel@tonic-gate dhcpmsg(MSG_ERROR, "dhcp_ipc_init: cannot bind to port "
2790Sstevel@tonic-gate "%i (agent already running?)", IPPORT_DHCPAGENT);
2800Sstevel@tonic-gate return (EXIT_FAILURE);
2810Sstevel@tonic-gate
2820Sstevel@tonic-gate default:
2830Sstevel@tonic-gate dhcpmsg(MSG_ERROR, "dhcp_ipc_init failed");
2840Sstevel@tonic-gate return (EXIT_FAILURE);
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate if (iu_register_event(eh, ipc_fd, POLLIN, accept_event, 0) == -1) {
2880Sstevel@tonic-gate dhcpmsg(MSG_ERR, "cannot register ipc fd for messages");
2890Sstevel@tonic-gate return (EXIT_FAILURE);
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate /*
2932546Scarlsonj * Create the global routing socket. This is used for monitoring
2942546Scarlsonj * interface transitions, so that we learn about the kernel's Duplicate
2952546Scarlsonj * Address Detection status, and for inserting and removing default
2963431Scarlsonj * routes as learned from DHCP servers. Both v4 and v6 are handed
2973431Scarlsonj * with this one socket.
2982546Scarlsonj */
2993431Scarlsonj rtsock_fd = socket(PF_ROUTE, SOCK_RAW, 0);
3002546Scarlsonj if (rtsock_fd == -1) {
3012546Scarlsonj dhcpmsg(MSG_ERR, "cannot open routing socket");
3022546Scarlsonj return (EXIT_FAILURE);
3032546Scarlsonj }
3048485SPeter.Memishian@Sun.COM
3058485SPeter.Memishian@Sun.COM /*
3068485SPeter.Memishian@Sun.COM * We're IPMP-aware and can manage IPMP test addresses, so issue
3078485SPeter.Memishian@Sun.COM * RT_AWARE to get routing socket messages for interfaces under IPMP.
3088485SPeter.Memishian@Sun.COM */
3098485SPeter.Memishian@Sun.COM if (setsockopt(rtsock_fd, SOL_ROUTE, RT_AWARE, &aware,
3108485SPeter.Memishian@Sun.COM sizeof (aware)) == -1) {
3118485SPeter.Memishian@Sun.COM dhcpmsg(MSG_ERR, "cannot set RT_AWARE on routing socket");
3128485SPeter.Memishian@Sun.COM return (EXIT_FAILURE);
3138485SPeter.Memishian@Sun.COM }
3148485SPeter.Memishian@Sun.COM
3152546Scarlsonj if (iu_register_event(eh, rtsock_fd, POLLIN, rtsock_event, 0) == -1) {
3162546Scarlsonj dhcpmsg(MSG_ERR, "cannot register routing socket for messages");
3172546Scarlsonj return (EXIT_FAILURE);
3182546Scarlsonj }
3192546Scarlsonj
3202546Scarlsonj /*
3210Sstevel@tonic-gate * if the -a (adopt) option was specified, try to adopt the
3222612Scarlsonj * kernel-managed interface before we start.
3230Sstevel@tonic-gate */
3240Sstevel@tonic-gate
3253431Scarlsonj if (do_adopt && !dhcp_adopt())
3262612Scarlsonj return (EXIT_FAILURE);
3270Sstevel@tonic-gate
3280Sstevel@tonic-gate /*
3293431Scarlsonj * For DHCPv6, we own all of the interfaces marked DHCPRUNNING. As
3303431Scarlsonj * we're starting operation here, if there are any of those interfaces
3313431Scarlsonj * lingering around, they're strays, and need to be removed.
3323431Scarlsonj *
3333431Scarlsonj * It might be nice to save these addresses off somewhere -- for both
3343431Scarlsonj * v4 and v6 -- and use them as hints for later negotiation.
3353431Scarlsonj */
3363431Scarlsonj remove_v6_strays();
3373431Scarlsonj
3383431Scarlsonj /*
3390Sstevel@tonic-gate * enter the main event loop; this is where all the real work
3400Sstevel@tonic-gate * takes place (through registering events and scheduling timers).
3410Sstevel@tonic-gate * this function only returns when the agent is shutting down.
3420Sstevel@tonic-gate */
3430Sstevel@tonic-gate
3440Sstevel@tonic-gate switch (iu_handle_events(eh, tq)) {
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate case -1:
3470Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "iu_handle_events exited abnormally");
3480Sstevel@tonic-gate break;
3490Sstevel@tonic-gate
3500Sstevel@tonic-gate case DHCP_REASON_INACTIVITY:
3510Sstevel@tonic-gate dhcpmsg(MSG_INFO, "no interfaces to manage, shutting down...");
3520Sstevel@tonic-gate break;
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate case DHCP_REASON_TERMINATE:
3550Sstevel@tonic-gate dhcpmsg(MSG_INFO, "received SIGTERM, shutting down...");
3560Sstevel@tonic-gate break;
3570Sstevel@tonic-gate
3580Sstevel@tonic-gate case DHCP_REASON_SIGNAL:
3590Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "received unexpected signal, shutting "
3600Sstevel@tonic-gate "down...");
3610Sstevel@tonic-gate break;
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate (void) iu_eh_unregister_signal(eh, SIGTHAW, NULL);
3650Sstevel@tonic-gate
3660Sstevel@tonic-gate iu_eh_destroy(eh);
3670Sstevel@tonic-gate iu_tq_destroy(tq);
3680Sstevel@tonic-gate
3690Sstevel@tonic-gate return (EXIT_SUCCESS);
3700Sstevel@tonic-gate }
3710Sstevel@tonic-gate
3720Sstevel@tonic-gate /*
3730Sstevel@tonic-gate * drain_script(): event loop callback during shutdown
3740Sstevel@tonic-gate *
3750Sstevel@tonic-gate * input: eh_t *: unused
3760Sstevel@tonic-gate * void *: unused
3770Sstevel@tonic-gate * output: boolean_t: B_TRUE if event loop should exit; B_FALSE otherwise
3780Sstevel@tonic-gate */
3790Sstevel@tonic-gate
3800Sstevel@tonic-gate /* ARGSUSED */
3810Sstevel@tonic-gate boolean_t
drain_script(iu_eh_t * ehp,void * arg)3820Sstevel@tonic-gate drain_script(iu_eh_t *ehp, void *arg)
3830Sstevel@tonic-gate {
3840Sstevel@tonic-gate if (shutdown_started == B_FALSE) {
3850Sstevel@tonic-gate shutdown_started = B_TRUE;
3868194SJack.Meng@Sun.COM /*
3878194SJack.Meng@Sun.COM * Check if the system is diskless client and/or
3888194SJack.Meng@Sun.COM * there are active iSCSI sessions
3898194SJack.Meng@Sun.COM *
3908194SJack.Meng@Sun.COM * Do not drop the lease, or the system will be
3918194SJack.Meng@Sun.COM * unable to sync(dump) through nfs/iSCSI driver
3928194SJack.Meng@Sun.COM */
3938194SJack.Meng@Sun.COM if (!do_adopt && !is_iscsi_active()) {
3943431Scarlsonj nuke_smach_list();
3958194SJack.Meng@Sun.COM }
3960Sstevel@tonic-gate }
3970Sstevel@tonic-gate return (script_count == 0);
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate
4000Sstevel@tonic-gate /*
4010Sstevel@tonic-gate * accept_event(): accepts a new connection on the ipc socket and registers
4020Sstevel@tonic-gate * to receive its messages with the event handler
4030Sstevel@tonic-gate *
4040Sstevel@tonic-gate * input: iu_eh_t *: unused
4050Sstevel@tonic-gate * int: the file descriptor in the iu_eh_t * the connection came in on
4060Sstevel@tonic-gate * (other arguments unused)
4070Sstevel@tonic-gate * output: void
4080Sstevel@tonic-gate */
4090Sstevel@tonic-gate
4100Sstevel@tonic-gate /* ARGSUSED */
4110Sstevel@tonic-gate static void
accept_event(iu_eh_t * ehp,int fd,short events,iu_event_id_t id,void * arg)4120Sstevel@tonic-gate accept_event(iu_eh_t *ehp, int fd, short events, iu_event_id_t id, void *arg)
4130Sstevel@tonic-gate {
4140Sstevel@tonic-gate int client_fd;
4150Sstevel@tonic-gate int is_priv;
4160Sstevel@tonic-gate
4170Sstevel@tonic-gate if (dhcp_ipc_accept(fd, &client_fd, &is_priv) != 0) {
4180Sstevel@tonic-gate dhcpmsg(MSG_ERR, "accept_event: accept on ipc socket");
4190Sstevel@tonic-gate return;
4200Sstevel@tonic-gate }
4210Sstevel@tonic-gate
4220Sstevel@tonic-gate if (iu_register_event(eh, client_fd, POLLIN, ipc_event,
4230Sstevel@tonic-gate (void *)is_priv) == -1) {
4240Sstevel@tonic-gate dhcpmsg(MSG_ERROR, "accept_event: cannot register ipc socket "
4250Sstevel@tonic-gate "for callback");
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate
4290Sstevel@tonic-gate /*
4300Sstevel@tonic-gate * ipc_event(): processes incoming ipc requests
4310Sstevel@tonic-gate *
4320Sstevel@tonic-gate * input: iu_eh_t *: unused
4330Sstevel@tonic-gate * int: the file descriptor in the iu_eh_t * the request came in on
4340Sstevel@tonic-gate * short: unused
4353431Scarlsonj * iu_event_id_t: event ID
4360Sstevel@tonic-gate * void *: indicates whether the request is from a privileged client
4370Sstevel@tonic-gate * output: void
4380Sstevel@tonic-gate */
4390Sstevel@tonic-gate
4400Sstevel@tonic-gate /* ARGSUSED */
4410Sstevel@tonic-gate static void
ipc_event(iu_eh_t * ehp,int fd,short events,iu_event_id_t id,void * arg)4420Sstevel@tonic-gate ipc_event(iu_eh_t *ehp, int fd, short events, iu_event_id_t id, void *arg)
4430Sstevel@tonic-gate {
4443431Scarlsonj ipc_action_t ia, *iap;
4453431Scarlsonj dhcp_smach_t *dsmp;
4460Sstevel@tonic-gate int error, is_priv = (int)arg;
4473431Scarlsonj const char *ifname;
4483431Scarlsonj boolean_t isv6;
44910785SPeter.Memishian@Sun.COM boolean_t dsm_created = B_FALSE;
4500Sstevel@tonic-gate
4513431Scarlsonj ipc_action_init(&ia);
4523431Scarlsonj error = dhcp_ipc_recv_request(fd, &ia.ia_request,
4533431Scarlsonj DHCP_IPC_REQUEST_WAIT);
4543431Scarlsonj if (error != DHCP_IPC_SUCCESS) {
4553431Scarlsonj if (error != DHCP_IPC_E_EOF) {
4563431Scarlsonj dhcpmsg(MSG_ERROR,
4573431Scarlsonj "ipc_event: dhcp_ipc_recv_request failed: %s",
4583431Scarlsonj dhcp_ipc_strerror(error));
4593431Scarlsonj } else {
4603431Scarlsonj dhcpmsg(MSG_DEBUG, "ipc_event: connection closed");
4613431Scarlsonj }
4623431Scarlsonj if ((dsmp = lookup_smach_by_event(id)) != NULL) {
4633431Scarlsonj ipc_action_finish(dsmp, error);
4643431Scarlsonj } else {
4653431Scarlsonj (void) iu_unregister_event(eh, id, NULL);
4663431Scarlsonj (void) dhcp_ipc_close(fd);
4673431Scarlsonj }
4680Sstevel@tonic-gate return;
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate
4713431Scarlsonj /* Fill in temporary ipc_action structure for utility functions */
4723431Scarlsonj ia.ia_cmd = DHCP_IPC_CMD(ia.ia_request->message_type);
4733431Scarlsonj ia.ia_fd = fd;
4743431Scarlsonj ia.ia_eid = id;
4753431Scarlsonj
4763431Scarlsonj if (ia.ia_cmd >= DHCP_NIPC) {
4773431Scarlsonj dhcpmsg(MSG_ERROR,
4783431Scarlsonj "ipc_event: invalid command (%s) attempted on %s",
4793431Scarlsonj dhcp_ipc_type_to_string(ia.ia_cmd), ia.ia_request->ifname);
4803431Scarlsonj send_error_reply(&ia, DHCP_IPC_E_CMD_UNKNOWN);
4810Sstevel@tonic-gate return;
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate
4840Sstevel@tonic-gate /* return EPERM for any of the privileged actions */
4850Sstevel@tonic-gate
4863431Scarlsonj if (!is_priv && (ipc_cmd_flags[ia.ia_cmd] & CMD_ISPRIV)) {
4873431Scarlsonj dhcpmsg(MSG_WARNING,
4883431Scarlsonj "ipc_event: privileged ipc command (%s) attempted on %s",
4893431Scarlsonj dhcp_ipc_type_to_string(ia.ia_cmd), ia.ia_request->ifname);
4903431Scarlsonj send_error_reply(&ia, DHCP_IPC_E_PERM);
4913431Scarlsonj return;
4923431Scarlsonj }
4933431Scarlsonj
4943431Scarlsonj /*
4953431Scarlsonj * Try to locate the state machine associated with this command. If
4963431Scarlsonj * the command is DHCP_START or DHCP_INFORM and there isn't a state
4973431Scarlsonj * machine already, make one (there may already be one from a previous
4983431Scarlsonj * failed attempt to START or INFORM). Otherwise, verify the reference
4993431Scarlsonj * is still valid.
5003431Scarlsonj *
5013431Scarlsonj * The interface name may be blank. In that case, we look up the
5023431Scarlsonj * primary interface, and the requested type (v4 or v6) doesn't matter.
5033431Scarlsonj */
5043431Scarlsonj
5053431Scarlsonj isv6 = (ia.ia_request->message_type & DHCP_V6) != 0;
5063431Scarlsonj ifname = ia.ia_request->ifname;
5073431Scarlsonj if (*ifname == '\0')
5083431Scarlsonj dsmp = primary_smach(isv6);
5093431Scarlsonj else
5103431Scarlsonj dsmp = lookup_smach(ifname, isv6);
5113431Scarlsonj
5123431Scarlsonj if (dsmp != NULL) {
5133431Scarlsonj /* Note that verify_smach drops a reference */
5143431Scarlsonj hold_smach(dsmp);
5153431Scarlsonj if (!verify_smach(dsmp))
5163431Scarlsonj dsmp = NULL;
5173431Scarlsonj }
5180Sstevel@tonic-gate
5193431Scarlsonj if (dsmp == NULL) {
5203431Scarlsonj /*
5219633Sjames.d.carlson@sun.com * If the user asked for the primary DHCP interface by giving
5229633Sjames.d.carlson@sun.com * an empty string and there is no primary, then check if we're
5239633Sjames.d.carlson@sun.com * handling dhcpinfo. If so, then simulate primary selection.
5249633Sjames.d.carlson@sun.com * Otherwise, report failure.
5253431Scarlsonj */
5263431Scarlsonj if (ifname[0] == '\0') {
5279633Sjames.d.carlson@sun.com if (ia.ia_cmd == DHCP_GET_TAG)
5289633Sjames.d.carlson@sun.com dsmp = info_primary_smach(isv6);
5299633Sjames.d.carlson@sun.com if (dsmp == NULL)
5309633Sjames.d.carlson@sun.com error = DHCP_IPC_E_NOPRIMARY;
5313431Scarlsonj
5323431Scarlsonj /*
5333431Scarlsonj * If there's no interface, and we're starting up, then create
5343431Scarlsonj * it now, along with a state machine for it. Note that if
5353431Scarlsonj * insert_smach fails, it discards the LIF reference.
5363431Scarlsonj */
5373431Scarlsonj } else if (ipc_cmd_flags[ia.ia_cmd] & CMD_CREATE) {
5383431Scarlsonj dhcp_lif_t *lif;
5390Sstevel@tonic-gate
5403431Scarlsonj lif = attach_lif(ifname, isv6, &error);
5413431Scarlsonj if (lif != NULL &&
5423431Scarlsonj (dsmp = insert_smach(lif, &error)) != NULL) {
5433431Scarlsonj /*
54410785SPeter.Memishian@Sun.COM * Get client ID for logical interface. (V4
54510785SPeter.Memishian@Sun.COM * only, because V6 plumbs its own interfaces.)
5463431Scarlsonj */
5473431Scarlsonj error = get_smach_cid(dsmp);
5483431Scarlsonj if (error != DHCP_IPC_SUCCESS) {
5493522Scarlsonj remove_smach(dsmp);
5503431Scarlsonj dsmp = NULL;
5513431Scarlsonj }
55210785SPeter.Memishian@Sun.COM dsm_created = (dsmp != NULL);
5533431Scarlsonj }
5540Sstevel@tonic-gate
5553431Scarlsonj /*
5563431Scarlsonj * Otherwise, this is an operation on an unknown interface.
5573431Scarlsonj */
5583431Scarlsonj } else {
5593431Scarlsonj error = DHCP_IPC_E_UNKIF;
5603431Scarlsonj }
5613431Scarlsonj if (dsmp == NULL) {
5623431Scarlsonj send_error_reply(&ia, error);
5630Sstevel@tonic-gate return;
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate
56710785SPeter.Memishian@Sun.COM /*
56810785SPeter.Memishian@Sun.COM * If this is a request for DHCP to manage a lease on an address,
56910785SPeter.Memishian@Sun.COM * ensure that IFF_DHCPRUNNING is set (we don't set this when the lif
57010785SPeter.Memishian@Sun.COM * is created because the lif may have been created for INFORM).
57110785SPeter.Memishian@Sun.COM */
57210785SPeter.Memishian@Sun.COM if (ia.ia_cmd == DHCP_START &&
57310785SPeter.Memishian@Sun.COM (error = set_lif_dhcp(dsmp->dsm_lif)) != DHCP_IPC_SUCCESS) {
57410785SPeter.Memishian@Sun.COM if (dsm_created)
57510785SPeter.Memishian@Sun.COM remove_smach(dsmp);
57610785SPeter.Memishian@Sun.COM send_error_reply(&ia, error);
57710785SPeter.Memishian@Sun.COM return;
57810785SPeter.Memishian@Sun.COM }
57910785SPeter.Memishian@Sun.COM
5803431Scarlsonj if ((dsmp->dsm_dflags & DHCP_IF_BOOTP) &&
5813431Scarlsonj !(ipc_cmd_flags[ia.ia_cmd] & CMD_BOOTP)) {
5823431Scarlsonj dhcpmsg(MSG_ERROR, "command %s not valid for BOOTP on %s",
5833431Scarlsonj dhcp_ipc_type_to_string(ia.ia_cmd), dsmp->dsm_name);
5843431Scarlsonj send_error_reply(&ia, DHCP_IPC_E_BOOTP);
5850Sstevel@tonic-gate return;
5860Sstevel@tonic-gate }
5870Sstevel@tonic-gate
5880Sstevel@tonic-gate /*
5893431Scarlsonj * verify that the state machine is in a state which will allow the
5900Sstevel@tonic-gate * command. we do this up front so that we can return an error
5910Sstevel@tonic-gate * *before* needlessly cancelling an in-progress transaction.
5920Sstevel@tonic-gate */
5930Sstevel@tonic-gate
5943431Scarlsonj if (!check_cmd_allowed(dsmp->dsm_state, ia.ia_cmd)) {
5953431Scarlsonj dhcpmsg(MSG_DEBUG,
5963431Scarlsonj "in state %s; not allowing %s command on %s",
5973431Scarlsonj dhcp_state_to_string(dsmp->dsm_state),
5983431Scarlsonj dhcp_ipc_type_to_string(ia.ia_cmd), dsmp->dsm_name);
5993431Scarlsonj send_error_reply(&ia,
6003431Scarlsonj ia.ia_cmd == DHCP_START && dsmp->dsm_state != INIT ?
6013431Scarlsonj DHCP_IPC_E_RUNNING : DHCP_IPC_E_OUTSTATE);
6020Sstevel@tonic-gate return;
6030Sstevel@tonic-gate }
6040Sstevel@tonic-gate
6053431Scarlsonj dhcpmsg(MSG_DEBUG, "in state %s; allowing %s command on %s",
6063431Scarlsonj dhcp_state_to_string(dsmp->dsm_state),
6073431Scarlsonj dhcp_ipc_type_to_string(ia.ia_cmd), dsmp->dsm_name);
6083431Scarlsonj
6093431Scarlsonj if ((ia.ia_request->message_type & DHCP_PRIMARY) && is_priv)
6103431Scarlsonj make_primary(dsmp);
6110Sstevel@tonic-gate
6120Sstevel@tonic-gate /*
6133431Scarlsonj * The current design dictates that there can be only one outstanding
6143431Scarlsonj * transaction per state machine -- this simplifies the code
6153431Scarlsonj * considerably and also fits well with RFCs 2131 and 3315. It is
6163431Scarlsonj * worth classifying the different DHCP commands into synchronous
6173431Scarlsonj * (those which we will handle now and reply to immediately) and
6183431Scarlsonj * asynchronous (those which require transactions and will be completed
6193431Scarlsonj * at an indeterminate time in the future):
6200Sstevel@tonic-gate *
6213431Scarlsonj * DROP: removes the agent's management of a state machine.
6220Sstevel@tonic-gate * asynchronous as the script program may be invoked.
6230Sstevel@tonic-gate *
6243431Scarlsonj * PING: checks to see if the agent has a named state machine.
6250Sstevel@tonic-gate * synchronous, since no packets need to be sent
6260Sstevel@tonic-gate * to the DHCP server.
6270Sstevel@tonic-gate *
6283431Scarlsonj * STATUS: returns information about a state machine.
6290Sstevel@tonic-gate * synchronous, since no packets need to be sent
6300Sstevel@tonic-gate * to the DHCP server.
6310Sstevel@tonic-gate *
6323431Scarlsonj * RELEASE: releases the agent's management of a state machine
6333431Scarlsonj * and brings the associated interfaces down. asynchronous
6343431Scarlsonj * as the script program may be invoked.
6350Sstevel@tonic-gate *
6360Sstevel@tonic-gate * EXTEND: renews a lease. asynchronous, since the agent
6370Sstevel@tonic-gate * needs to wait for an ACK, etc.
6380Sstevel@tonic-gate *
6393431Scarlsonj * START: starts DHCP on a named state machine. asynchronous since
6400Sstevel@tonic-gate * the agent needs to wait for OFFERs, ACKs, etc.
6410Sstevel@tonic-gate *
6423431Scarlsonj * INFORM: obtains configuration parameters for the system using
6433431Scarlsonj * externally configured interface. asynchronous, since the
6440Sstevel@tonic-gate * agent needs to wait for an ACK.
6450Sstevel@tonic-gate *
6463431Scarlsonj * Notice that EXTEND, INFORM, START, DROP and RELEASE are
6473431Scarlsonj * asynchronous. Notice also that asynchronous commands may occur from
6483431Scarlsonj * within the agent -- for instance, the agent will need to do implicit
6493431Scarlsonj * EXTENDs to extend the lease. In order to make the code simpler, the
6503431Scarlsonj * following rules apply for asynchronous commands:
6510Sstevel@tonic-gate *
6523431Scarlsonj * There can only be one asynchronous command at a time per state
6533431Scarlsonj * machine. The current asynchronous command is managed by the async_*
6543431Scarlsonj * api: async_start(), async_finish(), and async_cancel().
6553431Scarlsonj * async_start() starts management of a new asynchronous command on an
6563431Scarlsonj * state machine, which should only be done after async_cancel() to
6573431Scarlsonj * terminate a previous command. When the command is completed,
6583431Scarlsonj * async_finish() should be called.
6590Sstevel@tonic-gate *
6603431Scarlsonj * Asynchronous commands started by a user command have an associated
6613431Scarlsonj * ipc_action which provides the agent with information for how to get
6623431Scarlsonj * in touch with the user command when the action completes. These
6633431Scarlsonj * ipc_action records also have an associated timeout which may be
6643431Scarlsonj * infinite. ipc_action_start() should be called when starting an
6653431Scarlsonj * asynchronous command requested by a user, which sets up the timer
6663431Scarlsonj * and keeps track of the ipc information (file descriptor, request
6673431Scarlsonj * type). When the asynchronous command completes, ipc_action_finish()
6683431Scarlsonj * should be called to return a command status code to the user and
6693431Scarlsonj * close the ipc connection). If the command does not complete before
6703431Scarlsonj * the timer fires, ipc_action_timeout() is called which closes the ipc
6713431Scarlsonj * connection and returns DHCP_IPC_E_TIMEOUT to the user. Note that
6723431Scarlsonj * independent of ipc_action_timeout(), ipc_action_finish() should be
6733431Scarlsonj * called.
6740Sstevel@tonic-gate *
6753431Scarlsonj * on a case-by-case basis, here is what happens (per state machine):
6760Sstevel@tonic-gate *
6773431Scarlsonj * o When an asynchronous command is requested, then
6783431Scarlsonj * async_cancel() is called to terminate any non-user
6793431Scarlsonj * action in progress. If there's a user action running,
6800Sstevel@tonic-gate * the user command is sent DHCP_IPC_E_PEND.
6810Sstevel@tonic-gate *
6820Sstevel@tonic-gate * o otherwise, the the transaction is started with
6830Sstevel@tonic-gate * async_start(). if the transaction is on behalf
6840Sstevel@tonic-gate * of a user, ipc_action_start() is called to keep
6850Sstevel@tonic-gate * track of the ipc information and set up the
6860Sstevel@tonic-gate * ipc_action timer.
6870Sstevel@tonic-gate *
6880Sstevel@tonic-gate * o if the command completes normally and before a
6890Sstevel@tonic-gate * timeout fires, then async_finish() is called.
6900Sstevel@tonic-gate * if there was an associated ipc_action,
6910Sstevel@tonic-gate * ipc_action_finish() is called to complete it.
6920Sstevel@tonic-gate *
6930Sstevel@tonic-gate * o if the command fails before a timeout fires, then
6943431Scarlsonj * async_finish() is called, and the state machine is
6950Sstevel@tonic-gate * is returned to a known state based on the command.
6960Sstevel@tonic-gate * if there was an associated ipc_action,
6970Sstevel@tonic-gate * ipc_action_finish() is called to complete it.
6980Sstevel@tonic-gate *
6990Sstevel@tonic-gate * o if the ipc_action timer fires before command
7000Sstevel@tonic-gate * completion, then DHCP_IPC_E_TIMEOUT is returned to
7010Sstevel@tonic-gate * the user. however, the transaction continues to
7020Sstevel@tonic-gate * be carried out asynchronously.
7030Sstevel@tonic-gate */
7040Sstevel@tonic-gate
7053431Scarlsonj if (ipc_cmd_flags[ia.ia_cmd] & CMD_IMMED) {
7063431Scarlsonj /*
7073431Scarlsonj * Only immediate commands (ping, status, get_tag) need to
7083431Scarlsonj * worry about freeing ia through one of the reply functions
7093431Scarlsonj * before returning.
7103431Scarlsonj */
7113431Scarlsonj iap = &ia;
7123431Scarlsonj } else {
7130Sstevel@tonic-gate /*
7140Sstevel@tonic-gate * if shutdown request has been received, send back an error.
7150Sstevel@tonic-gate */
7160Sstevel@tonic-gate if (shutdown_started) {
7173431Scarlsonj send_error_reply(&ia, DHCP_IPC_E_OUTSTATE);
7180Sstevel@tonic-gate return;
7190Sstevel@tonic-gate }
7200Sstevel@tonic-gate
7213431Scarlsonj if (dsmp->dsm_dflags & DHCP_IF_BUSY) {
7223431Scarlsonj send_error_reply(&ia, DHCP_IPC_E_PEND);
7230Sstevel@tonic-gate return;
7240Sstevel@tonic-gate }
7250Sstevel@tonic-gate
7263431Scarlsonj if (!ipc_action_start(dsmp, &ia)) {
7273431Scarlsonj dhcpmsg(MSG_WARNING, "ipc_event: ipc_action_start "
7283431Scarlsonj "failed for %s", dsmp->dsm_name);
7293431Scarlsonj send_error_reply(&ia, DHCP_IPC_E_MEMORY);
7300Sstevel@tonic-gate return;
7310Sstevel@tonic-gate }
7320Sstevel@tonic-gate
7333431Scarlsonj /* Action structure consumed by above function */
7343431Scarlsonj iap = &dsmp->dsm_ia;
7350Sstevel@tonic-gate }
7360Sstevel@tonic-gate
7373431Scarlsonj switch (iap->ia_cmd) {
7380Sstevel@tonic-gate
7390Sstevel@tonic-gate case DHCP_DROP:
7406508Scarlsonj if (dsmp->dsm_droprelease)
7416508Scarlsonj break;
7426508Scarlsonj dsmp->dsm_droprelease = B_TRUE;
7439508SPeter.Memishian@Sun.COM
7449508SPeter.Memishian@Sun.COM /*
7459508SPeter.Memishian@Sun.COM * Ensure that a timer associated with the existing state
7469508SPeter.Memishian@Sun.COM * doesn't pop while we're waiting for the script to complete.
7479508SPeter.Memishian@Sun.COM * (If so, chaos can result -- e.g., a timer causes us to end
7489508SPeter.Memishian@Sun.COM * up in dhcp_selecting() would start acquiring a new lease on
7499508SPeter.Memishian@Sun.COM * dsmp while our DHCP_DROP dismantling is ongoing.)
7509508SPeter.Memishian@Sun.COM */
7519508SPeter.Memishian@Sun.COM cancel_smach_timers(dsmp);
7523431Scarlsonj (void) script_start(dsmp, isv6 ? EVENT_DROP6 : EVENT_DROP,
7533431Scarlsonj dhcp_drop, NULL, NULL);
7543431Scarlsonj break; /* not an immediate function */
7550Sstevel@tonic-gate
7560Sstevel@tonic-gate case DHCP_EXTEND:
7573431Scarlsonj (void) dhcp_extending(dsmp);
7580Sstevel@tonic-gate break;
7590Sstevel@tonic-gate
7600Sstevel@tonic-gate case DHCP_GET_TAG: {
7610Sstevel@tonic-gate dhcp_optnum_t optnum;
7623431Scarlsonj void *opt = NULL;
7633431Scarlsonj uint_t optlen;
7640Sstevel@tonic-gate boolean_t did_alloc = B_FALSE;
7653431Scarlsonj PKT_LIST *ack = dsmp->dsm_ack;
7669633Sjames.d.carlson@sun.com int i;
7670Sstevel@tonic-gate
7680Sstevel@tonic-gate /*
7690Sstevel@tonic-gate * verify the request makes sense.
7700Sstevel@tonic-gate */
7710Sstevel@tonic-gate
7723431Scarlsonj if (iap->ia_request->data_type != DHCP_TYPE_OPTNUM ||
7733431Scarlsonj iap->ia_request->data_length != sizeof (dhcp_optnum_t)) {
7743431Scarlsonj send_error_reply(iap, DHCP_IPC_E_PROTO);
7753431Scarlsonj break;
7760Sstevel@tonic-gate }
7770Sstevel@tonic-gate
7783431Scarlsonj (void) memcpy(&optnum, iap->ia_request->buffer,
7793431Scarlsonj sizeof (dhcp_optnum_t));
7803431Scarlsonj
7810Sstevel@tonic-gate load_option:
7820Sstevel@tonic-gate switch (optnum.category) {
7830Sstevel@tonic-gate
7840Sstevel@tonic-gate case DSYM_SITE: /* FALLTHRU */
7850Sstevel@tonic-gate case DSYM_STANDARD:
7869633Sjames.d.carlson@sun.com for (i = 0; i < dsmp->dsm_pillen; i++) {
7879633Sjames.d.carlson@sun.com if (dsmp->dsm_pil[i] == optnum.code)
7889633Sjames.d.carlson@sun.com break;
7899633Sjames.d.carlson@sun.com }
7909633Sjames.d.carlson@sun.com if (i < dsmp->dsm_pillen)
7919633Sjames.d.carlson@sun.com break;
7923431Scarlsonj if (isv6) {
7933431Scarlsonj opt = dhcpv6_pkt_option(ack, NULL, optnum.code,
7943431Scarlsonj NULL);
7953431Scarlsonj } else {
7963431Scarlsonj if (optnum.code <= DHCP_LAST_OPT)
7973431Scarlsonj opt = ack->opts[optnum.code];
7983431Scarlsonj }
7990Sstevel@tonic-gate break;
8000Sstevel@tonic-gate
8010Sstevel@tonic-gate case DSYM_VENDOR:
8023431Scarlsonj if (isv6) {
8033431Scarlsonj dhcpv6_option_t *d6o;
8043431Scarlsonj uint32_t ent;
8050Sstevel@tonic-gate
8063431Scarlsonj /*
8073431Scarlsonj * Look through vendor options to find our
8083431Scarlsonj * enterprise number.
8093431Scarlsonj */
8103431Scarlsonj d6o = NULL;
8113431Scarlsonj for (;;) {
8123431Scarlsonj d6o = dhcpv6_pkt_option(ack, d6o,
8133431Scarlsonj DHCPV6_OPT_VENDOR_OPT, &optlen);
8143431Scarlsonj if (d6o == NULL)
8153431Scarlsonj break;
8163431Scarlsonj optlen -= sizeof (*d6o);
8173431Scarlsonj if (optlen < sizeof (ent))
8183431Scarlsonj continue;
8193431Scarlsonj (void) memcpy(&ent, d6o + 1,
8203431Scarlsonj sizeof (ent));
8213431Scarlsonj if (ntohl(ent) != DHCPV6_SUN_ENT)
8223431Scarlsonj continue;
8233431Scarlsonj break;
8243431Scarlsonj }
8253431Scarlsonj if (d6o != NULL) {
8263431Scarlsonj /*
8273431Scarlsonj * Now find the requested vendor option
8283431Scarlsonj * within the vendor options block.
8293431Scarlsonj */
8303431Scarlsonj opt = dhcpv6_find_option(
8313431Scarlsonj (char *)(d6o + 1) + sizeof (ent),
8323431Scarlsonj optlen - sizeof (ent), NULL,
8333431Scarlsonj optnum.code, NULL);
8343431Scarlsonj }
8353431Scarlsonj } else {
8363431Scarlsonj /*
8373431Scarlsonj * the test against VS_OPTION_START is broken
8383431Scarlsonj * up into two tests to avoid compiler warnings
8393431Scarlsonj * under intel.
8403431Scarlsonj */
8413431Scarlsonj if ((optnum.code > VS_OPTION_START ||
8423431Scarlsonj optnum.code == VS_OPTION_START) &&
8433431Scarlsonj optnum.code <= VS_OPTION_END)
8443431Scarlsonj opt = ack->vs[optnum.code];
8453431Scarlsonj }
8460Sstevel@tonic-gate break;
8470Sstevel@tonic-gate
8480Sstevel@tonic-gate case DSYM_FIELD:
8493431Scarlsonj if (isv6) {
8503431Scarlsonj dhcpv6_message_t *d6m =
8513431Scarlsonj (dhcpv6_message_t *)ack->pkt;
8523431Scarlsonj dhcpv6_option_t *d6o;
8533431Scarlsonj
8543431Scarlsonj /* Validate the packet field the user wants */
8553431Scarlsonj optlen = optnum.code + optnum.size;
8563431Scarlsonj if (d6m->d6m_msg_type ==
8573431Scarlsonj DHCPV6_MSG_RELAY_FORW ||
8583431Scarlsonj d6m->d6m_msg_type ==
8593431Scarlsonj DHCPV6_MSG_RELAY_REPL) {
8603431Scarlsonj if (optlen > sizeof (dhcpv6_relay_t))
8613431Scarlsonj break;
8623431Scarlsonj } else {
8633431Scarlsonj if (optlen > sizeof (*d6m))
8643431Scarlsonj break;
8653431Scarlsonj }
8660Sstevel@tonic-gate
8673431Scarlsonj opt = malloc(sizeof (*d6o) + optnum.size);
8683431Scarlsonj if (opt != NULL) {
8693431Scarlsonj d6o = opt;
8703431Scarlsonj d6o->d6o_code = htons(optnum.code);
8713431Scarlsonj d6o->d6o_len = htons(optnum.size);
8723431Scarlsonj (void) memcpy(d6o + 1, (caddr_t)d6m +
8733431Scarlsonj optnum.code, optnum.size);
8743431Scarlsonj }
8753431Scarlsonj } else {
8763431Scarlsonj if (optnum.code + optnum.size > sizeof (PKT))
8773431Scarlsonj break;
8783431Scarlsonj
8793431Scarlsonj /*
8803431Scarlsonj * + 2 to account for option code and length
8813431Scarlsonj * byte
8823431Scarlsonj */
8833431Scarlsonj opt = malloc(optnum.size + 2);
8843431Scarlsonj if (opt != NULL) {
8853431Scarlsonj DHCP_OPT *v4opt = opt;
8863431Scarlsonj
8873431Scarlsonj v4opt->len = optnum.size;
8883431Scarlsonj v4opt->code = optnum.code;
8893431Scarlsonj (void) memcpy(v4opt->value,
8903431Scarlsonj (caddr_t)ack->pkt + optnum.code,
8913431Scarlsonj optnum.size);
8923431Scarlsonj }
8933431Scarlsonj }
8943431Scarlsonj
8950Sstevel@tonic-gate if (opt == NULL) {
8963431Scarlsonj send_error_reply(iap, DHCP_IPC_E_MEMORY);
8970Sstevel@tonic-gate return;
8980Sstevel@tonic-gate }
8990Sstevel@tonic-gate did_alloc = B_TRUE;
9000Sstevel@tonic-gate break;
9010Sstevel@tonic-gate
9020Sstevel@tonic-gate default:
9033431Scarlsonj send_error_reply(iap, DHCP_IPC_E_PROTO);
9040Sstevel@tonic-gate return;
9050Sstevel@tonic-gate }
9060Sstevel@tonic-gate
9070Sstevel@tonic-gate /*
9080Sstevel@tonic-gate * return the option payload, if there was one. the "+ 2"
9090Sstevel@tonic-gate * accounts for the option code number and length byte.
9100Sstevel@tonic-gate */
9110Sstevel@tonic-gate
9120Sstevel@tonic-gate if (opt != NULL) {
9133431Scarlsonj if (isv6) {
9143431Scarlsonj dhcpv6_option_t d6ov;
9153431Scarlsonj
9163431Scarlsonj (void) memcpy(&d6ov, opt, sizeof (d6ov));
9173431Scarlsonj optlen = ntohs(d6ov.d6o_len) + sizeof (d6ov);
9183431Scarlsonj } else {
9193431Scarlsonj optlen = ((DHCP_OPT *)opt)->len + 2;
9203431Scarlsonj }
9213431Scarlsonj send_data_reply(iap, 0, DHCP_TYPE_OPTION, opt, optlen);
9220Sstevel@tonic-gate
9230Sstevel@tonic-gate if (did_alloc)
9240Sstevel@tonic-gate free(opt);
9253431Scarlsonj break;
9263431Scarlsonj } else if (ack != dsmp->dsm_orig_ack) {
9270Sstevel@tonic-gate /*
9280Sstevel@tonic-gate * There wasn't any definition for the option in the
9290Sstevel@tonic-gate * current ack, so now retry with the original ack if
9300Sstevel@tonic-gate * the original ack is not the current ack.
9310Sstevel@tonic-gate */
9323431Scarlsonj ack = dsmp->dsm_orig_ack;
9330Sstevel@tonic-gate goto load_option;
9340Sstevel@tonic-gate }
9350Sstevel@tonic-gate
9360Sstevel@tonic-gate /*
9370Sstevel@tonic-gate * note that an "okay" response is returned either in
9380Sstevel@tonic-gate * the case of an unknown option or a known option
9390Sstevel@tonic-gate * with no payload. this is okay (for now) since
9400Sstevel@tonic-gate * dhcpinfo checks whether an option is valid before
9410Sstevel@tonic-gate * ever performing ipc with the agent.
9420Sstevel@tonic-gate */
9430Sstevel@tonic-gate
9443431Scarlsonj send_ok_reply(iap);
9453431Scarlsonj break;
9460Sstevel@tonic-gate }
9470Sstevel@tonic-gate
9480Sstevel@tonic-gate case DHCP_INFORM:
9493431Scarlsonj dhcp_inform(dsmp);
9500Sstevel@tonic-gate /* next destination: dhcp_acknak() */
9513431Scarlsonj break; /* not an immediate function */
9520Sstevel@tonic-gate
9530Sstevel@tonic-gate case DHCP_PING:
9543431Scarlsonj if (dsmp->dsm_dflags & DHCP_IF_FAILED)
9553431Scarlsonj send_error_reply(iap, DHCP_IPC_E_FAILEDIF);
9560Sstevel@tonic-gate else
9573431Scarlsonj send_ok_reply(iap);
9583431Scarlsonj break;
9590Sstevel@tonic-gate
9600Sstevel@tonic-gate case DHCP_RELEASE:
9616508Scarlsonj if (dsmp->dsm_droprelease)
9626508Scarlsonj break;
9636508Scarlsonj dsmp->dsm_droprelease = B_TRUE;
9649508SPeter.Memishian@Sun.COM cancel_smach_timers(dsmp); /* see comment in DHCP_DROP above */
9653431Scarlsonj (void) script_start(dsmp, isv6 ? EVENT_RELEASE6 :
9663431Scarlsonj EVENT_RELEASE, dhcp_release, "Finished with lease.", NULL);
9673431Scarlsonj break; /* not an immediate function */
9680Sstevel@tonic-gate
9693431Scarlsonj case DHCP_START: {
9703431Scarlsonj PKT_LIST *ack, *oack;
9713431Scarlsonj PKT_LIST *plp[2];
9723431Scarlsonj
9733431Scarlsonj deprecate_leases(dsmp);
9740Sstevel@tonic-gate
9750Sstevel@tonic-gate /*
9760Sstevel@tonic-gate * if we have a valid hostconf lying around, then jump
9770Sstevel@tonic-gate * into INIT_REBOOT. if it fails, we'll end up going
9780Sstevel@tonic-gate * through the whole selecting() procedure again.
9790Sstevel@tonic-gate */
9800Sstevel@tonic-gate
9813431Scarlsonj error = read_hostconf(dsmp->dsm_name, plp, 2, dsmp->dsm_isv6);
9823431Scarlsonj ack = error > 0 ? plp[0] : NULL;
9833431Scarlsonj oack = error > 1 ? plp[1] : NULL;
9843431Scarlsonj
9853431Scarlsonj /*
9863431Scarlsonj * If the allocation of the old ack fails, that's fine;
9873431Scarlsonj * continue without it.
9883431Scarlsonj */
9893431Scarlsonj if (oack == NULL)
9903431Scarlsonj oack = ack;
9913431Scarlsonj
9923431Scarlsonj /*
9933431Scarlsonj * As long as we've allocated something, start using it.
9943431Scarlsonj */
9953431Scarlsonj if (ack != NULL) {
9963431Scarlsonj dsmp->dsm_orig_ack = oack;
9973431Scarlsonj dsmp->dsm_ack = ack;
9983431Scarlsonj dhcp_init_reboot(dsmp);
9990Sstevel@tonic-gate /* next destination: dhcp_acknak() */
10003431Scarlsonj break;
10010Sstevel@tonic-gate }
10020Sstevel@tonic-gate
10030Sstevel@tonic-gate /*
10040Sstevel@tonic-gate * if not debugging, wait for a few seconds before
10050Sstevel@tonic-gate * going into SELECTING.
10060Sstevel@tonic-gate */
10070Sstevel@tonic-gate
10084106Scarlsonj if (debug_level == 0 && set_start_timer(dsmp)) {
10093431Scarlsonj /* next destination: dhcp_start() */
10103431Scarlsonj break;
10114106Scarlsonj } else {
10124106Scarlsonj dhcp_selecting(dsmp);
10134106Scarlsonj /* next destination: dhcp_requesting() */
10144106Scarlsonj break;
10150Sstevel@tonic-gate }
10163431Scarlsonj }
10170Sstevel@tonic-gate
10180Sstevel@tonic-gate case DHCP_STATUS: {
10190Sstevel@tonic-gate dhcp_status_t status;
10203431Scarlsonj dhcp_lease_t *dlp;
10210Sstevel@tonic-gate
10223431Scarlsonj status.if_began = monosec_to_time(dsmp->dsm_curstart_monosec);
10230Sstevel@tonic-gate
10243431Scarlsonj /*
10253431Scarlsonj * We return information on just the first lease as being
10263431Scarlsonj * representative of the lot. A better status mechanism is
10273431Scarlsonj * needed.
10283431Scarlsonj */
10293431Scarlsonj dlp = dsmp->dsm_leases;
10303431Scarlsonj
10313431Scarlsonj if (dlp == NULL ||
10323431Scarlsonj dlp->dl_lifs->lif_expire.dt_start == DHCP_PERM) {
10330Sstevel@tonic-gate status.if_t1 = DHCP_PERM;
10340Sstevel@tonic-gate status.if_t2 = DHCP_PERM;
10350Sstevel@tonic-gate status.if_lease = DHCP_PERM;
10360Sstevel@tonic-gate } else {
10373431Scarlsonj status.if_t1 = status.if_began +
10383431Scarlsonj dlp->dl_t1.dt_start;
10393431Scarlsonj status.if_t2 = status.if_began +
10403431Scarlsonj dlp->dl_t2.dt_start;
10413431Scarlsonj status.if_lease = status.if_began +
10423431Scarlsonj dlp->dl_lifs->lif_expire.dt_start;
10430Sstevel@tonic-gate }
10440Sstevel@tonic-gate
10450Sstevel@tonic-gate status.version = DHCP_STATUS_VER;
10463431Scarlsonj status.if_state = dsmp->dsm_state;
10473431Scarlsonj status.if_dflags = dsmp->dsm_dflags;
10483431Scarlsonj status.if_sent = dsmp->dsm_sent;
10493431Scarlsonj status.if_recv = dsmp->dsm_received;
10503431Scarlsonj status.if_bad_offers = dsmp->dsm_bad_offers;
10510Sstevel@tonic-gate
10528562SPeter.Memishian@Sun.COM (void) strlcpy(status.if_name, dsmp->dsm_name, LIFNAMSIZ);
10533431Scarlsonj
10543431Scarlsonj send_data_reply(iap, 0, DHCP_TYPE_STATUS, &status,
10550Sstevel@tonic-gate sizeof (dhcp_status_t));
10563431Scarlsonj break;
10570Sstevel@tonic-gate }
10580Sstevel@tonic-gate }
10590Sstevel@tonic-gate }
10602546Scarlsonj
10612546Scarlsonj /*
10622546Scarlsonj * check_rtm_addr(): determine if routing socket message matches interface
10632546Scarlsonj * address
10642546Scarlsonj *
10653431Scarlsonj * input: const struct if_msghdr *: pointer to routing socket message
10663431Scarlsonj * int: routing socket message length
10673431Scarlsonj * boolean_t: set to B_TRUE if IPv6
10683431Scarlsonj * const in6_addr_t *: pointer to IP address
10693431Scarlsonj * output: boolean_t: B_TRUE if address is a match
10702546Scarlsonj */
10713431Scarlsonj
10722546Scarlsonj static boolean_t
check_rtm_addr(const struct ifa_msghdr * ifam,int msglen,boolean_t isv6,const in6_addr_t * addr)10733431Scarlsonj check_rtm_addr(const struct ifa_msghdr *ifam, int msglen, boolean_t isv6,
10743431Scarlsonj const in6_addr_t *addr)
10752546Scarlsonj {
10763431Scarlsonj const char *cp, *lim;
10772546Scarlsonj uint_t flag;
10783431Scarlsonj const struct sockaddr *sa;
10792546Scarlsonj
10802546Scarlsonj if (!(ifam->ifam_addrs & RTA_IFA))
10812546Scarlsonj return (B_FALSE);
10822546Scarlsonj
10833431Scarlsonj cp = (const char *)(ifam + 1);
10843431Scarlsonj lim = (const char *)ifam + msglen;
10852546Scarlsonj for (flag = 1; flag < RTA_IFA; flag <<= 1) {
10862546Scarlsonj if (ifam->ifam_addrs & flag) {
10872546Scarlsonj /* LINTED: alignment */
10883431Scarlsonj sa = (const struct sockaddr *)cp;
10893431Scarlsonj if ((const char *)(sa + 1) > lim)
10902546Scarlsonj return (B_FALSE);
10912546Scarlsonj switch (sa->sa_family) {
10922546Scarlsonj case AF_INET:
10932546Scarlsonj cp += sizeof (struct sockaddr_in);
10942546Scarlsonj break;
10952546Scarlsonj case AF_LINK:
10962546Scarlsonj cp += sizeof (struct sockaddr_dl);
10972546Scarlsonj break;
10982546Scarlsonj case AF_INET6:
10992546Scarlsonj cp += sizeof (struct sockaddr_in6);
11002546Scarlsonj break;
11012546Scarlsonj default:
11022546Scarlsonj cp += sizeof (struct sockaddr);
11032546Scarlsonj break;
11042546Scarlsonj }
11052546Scarlsonj }
11062546Scarlsonj }
11073431Scarlsonj if (isv6) {
11083431Scarlsonj const struct sockaddr_in6 *sin6;
11093431Scarlsonj
11103431Scarlsonj /* LINTED: alignment */
11113431Scarlsonj sin6 = (const struct sockaddr_in6 *)cp;
11123431Scarlsonj if ((const char *)(sin6 + 1) > lim)
11133431Scarlsonj return (B_FALSE);
11143431Scarlsonj if (sin6->sin6_family != AF_INET6)
11153431Scarlsonj return (B_FALSE);
11163431Scarlsonj return (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, addr));
11173431Scarlsonj } else {
11183431Scarlsonj const struct sockaddr_in *sinp;
11193431Scarlsonj ipaddr_t v4addr;
11203431Scarlsonj
11213431Scarlsonj /* LINTED: alignment */
11223431Scarlsonj sinp = (const struct sockaddr_in *)cp;
11233431Scarlsonj if ((const char *)(sinp + 1) > lim)
11243431Scarlsonj return (B_FALSE);
11253431Scarlsonj if (sinp->sin_family != AF_INET)
11263431Scarlsonj return (B_FALSE);
11273431Scarlsonj IN6_V4MAPPED_TO_IPADDR(addr, v4addr);
11283431Scarlsonj return (sinp->sin_addr.s_addr == v4addr);
11293431Scarlsonj }
11303431Scarlsonj }
11313431Scarlsonj
11323431Scarlsonj /*
11333431Scarlsonj * is_rtm_v6(): determine if routing socket message is IPv6
11343431Scarlsonj *
11353431Scarlsonj * input: struct ifa_msghdr *: pointer to routing socket message
11363431Scarlsonj * int: message length
11373431Scarlsonj * output: boolean_t
11383431Scarlsonj */
11393431Scarlsonj
11403431Scarlsonj static boolean_t
is_rtm_v6(const struct ifa_msghdr * ifam,int msglen)11413431Scarlsonj is_rtm_v6(const struct ifa_msghdr *ifam, int msglen)
11423431Scarlsonj {
11433431Scarlsonj const char *cp, *lim;
11443431Scarlsonj uint_t flag;
11453431Scarlsonj const struct sockaddr *sa;
11463431Scarlsonj
11473431Scarlsonj cp = (const char *)(ifam + 1);
11483431Scarlsonj lim = (const char *)ifam + msglen;
11493431Scarlsonj for (flag = ifam->ifam_addrs; flag != 0; flag &= flag - 1) {
11503431Scarlsonj /* LINTED: alignment */
11513431Scarlsonj sa = (const struct sockaddr *)cp;
11523431Scarlsonj if ((const char *)(sa + 1) > lim)
11533431Scarlsonj return (B_FALSE);
11543431Scarlsonj switch (sa->sa_family) {
11553431Scarlsonj case AF_INET:
11563431Scarlsonj return (B_FALSE);
11573431Scarlsonj case AF_LINK:
11583431Scarlsonj cp += sizeof (struct sockaddr_dl);
11593431Scarlsonj break;
11603431Scarlsonj case AF_INET6:
11613431Scarlsonj return (B_TRUE);
11623431Scarlsonj default:
11633431Scarlsonj cp += sizeof (struct sockaddr);
11643431Scarlsonj break;
11653431Scarlsonj }
11663431Scarlsonj }
11673431Scarlsonj return (B_FALSE);
11683431Scarlsonj }
11693431Scarlsonj
11703431Scarlsonj /*
11713431Scarlsonj * check_lif(): check the state of a given logical interface and its DHCP
11723431Scarlsonj * lease. We've been told by the routing socket that the
11733431Scarlsonj * corresponding ifIndex has changed. This may mean that DAD has
11743431Scarlsonj * completed or failed.
11753431Scarlsonj *
11763431Scarlsonj * input: dhcp_lif_t *: pointer to the LIF
11773431Scarlsonj * const struct ifa_msghdr *: routing socket message
11783431Scarlsonj * int: size of routing socket message
11793431Scarlsonj * output: boolean_t: B_TRUE if DAD has completed on this interface
11803431Scarlsonj */
11813431Scarlsonj
11823431Scarlsonj static boolean_t
check_lif(dhcp_lif_t * lif,const struct ifa_msghdr * ifam,int msglen)11833431Scarlsonj check_lif(dhcp_lif_t *lif, const struct ifa_msghdr *ifam, int msglen)
11843431Scarlsonj {
11853431Scarlsonj boolean_t isv6, dad_wait, unplumb;
11863431Scarlsonj int fd;
11873431Scarlsonj struct lifreq lifr;
11883431Scarlsonj
11893431Scarlsonj isv6 = lif->lif_pif->pif_isv6;
11903431Scarlsonj fd = isv6 ? v6_sock_fd : v4_sock_fd;
11913431Scarlsonj
11923431Scarlsonj /*
11933431Scarlsonj * Get the real (64 bit) logical interface flags. Note that the
11943431Scarlsonj * routing socket message has flags, but these are just the lower 32
11953431Scarlsonj * bits.
11963431Scarlsonj */
11973431Scarlsonj unplumb = B_FALSE;
11983431Scarlsonj (void) memset(&lifr, 0, sizeof (lifr));
11993431Scarlsonj (void) strlcpy(lifr.lifr_name, lif->lif_name, sizeof (lifr.lifr_name));
12003431Scarlsonj if (ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) {
12013431Scarlsonj /*
12023431Scarlsonj * Failing to retrieve flags means that the interface is gone.
12033431Scarlsonj * It hasn't failed to verify with DAD, but we still have to
12043431Scarlsonj * give up on it.
12053431Scarlsonj */
12063431Scarlsonj lifr.lifr_flags = 0;
12073431Scarlsonj if (errno == ENXIO) {
12083431Scarlsonj lif->lif_plumbed = B_FALSE;
12093431Scarlsonj dhcpmsg(MSG_INFO, "%s has been removed; abandoning",
12103431Scarlsonj lif->lif_name);
12114106Scarlsonj if (!isv6)
12124106Scarlsonj discard_default_routes(lif->lif_smachs);
12133431Scarlsonj } else {
12143431Scarlsonj dhcpmsg(MSG_ERR,
12153431Scarlsonj "unable to retrieve interface flags on %s",
12163431Scarlsonj lif->lif_name);
12173431Scarlsonj }
12183431Scarlsonj unplumb = B_TRUE;
12193431Scarlsonj } else if (!check_rtm_addr(ifam, msglen, isv6, &lif->lif_v6addr)) {
12203431Scarlsonj /*
12213431Scarlsonj * If the message is not about this logical interface,
12223431Scarlsonj * then just ignore it.
12233431Scarlsonj */
12242546Scarlsonj return (B_FALSE);
12253431Scarlsonj } else if (lifr.lifr_flags & IFF_DUPLICATE) {
12263431Scarlsonj dhcpmsg(MSG_ERROR, "interface %s has duplicate address",
12273431Scarlsonj lif->lif_name);
12283431Scarlsonj lif_mark_decline(lif, "duplicate address");
12293431Scarlsonj close_ip_lif(lif);
12308485SPeter.Memishian@Sun.COM (void) open_ip_lif(lif, INADDR_ANY, B_TRUE);
12313431Scarlsonj }
12323431Scarlsonj
12333431Scarlsonj dad_wait = lif->lif_dad_wait;
12343431Scarlsonj if (dad_wait) {
12353431Scarlsonj dhcpmsg(MSG_VERBOSE, "check_lif: %s has finished DAD",
12363431Scarlsonj lif->lif_name);
12373431Scarlsonj lif->lif_dad_wait = B_FALSE;
12383431Scarlsonj }
12393431Scarlsonj
12403431Scarlsonj if (unplumb)
12413431Scarlsonj unplumb_lif(lif);
12423431Scarlsonj
12433431Scarlsonj return (dad_wait);
12443431Scarlsonj }
12453431Scarlsonj
12463431Scarlsonj /*
12473431Scarlsonj * check_main_lif(): check the state of a main logical interface for a state
12483431Scarlsonj * machine. This is used only for DHCPv6.
12493431Scarlsonj *
12503431Scarlsonj * input: dhcp_smach_t *: pointer to the state machine
12513431Scarlsonj * const struct ifa_msghdr *: routing socket message
12523431Scarlsonj * int: size of routing socket message
12533431Scarlsonj * output: boolean_t: B_TRUE if LIF is ok.
12543431Scarlsonj */
12553431Scarlsonj
12563431Scarlsonj static boolean_t
check_main_lif(dhcp_smach_t * dsmp,const struct ifa_msghdr * ifam,int msglen)12573431Scarlsonj check_main_lif(dhcp_smach_t *dsmp, const struct ifa_msghdr *ifam, int msglen)
12583431Scarlsonj {
12593431Scarlsonj dhcp_lif_t *lif = dsmp->dsm_lif;
12603431Scarlsonj struct lifreq lifr;
12613431Scarlsonj
12623431Scarlsonj /*
12633431Scarlsonj * Get the real (64 bit) logical interface flags. Note that the
12643431Scarlsonj * routing socket message has flags, but these are just the lower 32
12653431Scarlsonj * bits.
12663431Scarlsonj */
12673431Scarlsonj (void) memset(&lifr, 0, sizeof (lifr));
12683431Scarlsonj (void) strlcpy(lifr.lifr_name, lif->lif_name, sizeof (lifr.lifr_name));
12693431Scarlsonj if (ioctl(v6_sock_fd, SIOCGLIFFLAGS, &lifr) == -1) {
12703431Scarlsonj /*
12713431Scarlsonj * Failing to retrieve flags means that the interface is gone.
12723431Scarlsonj * Our state machine is now trash.
12733431Scarlsonj */
12743431Scarlsonj if (errno == ENXIO) {
12753431Scarlsonj dhcpmsg(MSG_INFO, "%s has been removed; abandoning",
12763431Scarlsonj lif->lif_name);
12773431Scarlsonj } else {
12783431Scarlsonj dhcpmsg(MSG_ERR,
12793431Scarlsonj "unable to retrieve interface flags on %s",
12803431Scarlsonj lif->lif_name);
12813431Scarlsonj }
12823431Scarlsonj return (B_FALSE);
12833431Scarlsonj } else if (!check_rtm_addr(ifam, msglen, B_TRUE, &lif->lif_v6addr)) {
12843431Scarlsonj /*
12853431Scarlsonj * If the message is not about this logical interface,
12863431Scarlsonj * then just ignore it.
12873431Scarlsonj */
12883431Scarlsonj return (B_TRUE);
12893431Scarlsonj } else if (lifr.lifr_flags & IFF_DUPLICATE) {
12903431Scarlsonj dhcpmsg(MSG_ERROR, "interface %s has duplicate address",
12913431Scarlsonj lif->lif_name);
12923431Scarlsonj return (B_FALSE);
12933431Scarlsonj } else {
12943431Scarlsonj return (B_TRUE);
12953431Scarlsonj }
12963431Scarlsonj }
12973431Scarlsonj
12983431Scarlsonj /*
12993431Scarlsonj * process_link_up_down(): check the state of a physical interface for up/down
13003431Scarlsonj * transitions; must go through INIT_REBOOT state if
13013431Scarlsonj * the link flaps.
13023431Scarlsonj *
13033431Scarlsonj * input: dhcp_pif_t *: pointer to the physical interface to check
13043431Scarlsonj * const struct if_msghdr *: routing socket message
13053431Scarlsonj * output: none
13063431Scarlsonj */
13073431Scarlsonj
13083431Scarlsonj static void
process_link_up_down(dhcp_pif_t * pif,const struct if_msghdr * ifm)13093431Scarlsonj process_link_up_down(dhcp_pif_t *pif, const struct if_msghdr *ifm)
13103431Scarlsonj {
13113431Scarlsonj struct lifreq lifr;
13123431Scarlsonj boolean_t isv6;
13133431Scarlsonj int fd;
13143431Scarlsonj
13153431Scarlsonj /*
13163431Scarlsonj * If the message implies no change of flags, then we're done; no need
13173431Scarlsonj * to check further. Note that if we have multiple state machines on a
13183431Scarlsonj * single physical interface, this test keeps us from issuing an ioctl
13193431Scarlsonj * for each one.
13203431Scarlsonj */
13213431Scarlsonj if ((ifm->ifm_flags & IFF_RUNNING) && pif->pif_running ||
13223431Scarlsonj !(ifm->ifm_flags & IFF_RUNNING) && !pif->pif_running)
13233431Scarlsonj return;
13243431Scarlsonj
13253431Scarlsonj /*
13263431Scarlsonj * We don't know what the real interface flags are, because the
13273431Scarlsonj * if_index number is only 16 bits; we must go ask.
13283431Scarlsonj */
13293431Scarlsonj isv6 = pif->pif_isv6;
13303431Scarlsonj fd = isv6 ? v6_sock_fd : v4_sock_fd;
13313431Scarlsonj (void) memset(&lifr, 0, sizeof (lifr));
13323431Scarlsonj (void) strlcpy(lifr.lifr_name, pif->pif_name, sizeof (lifr.lifr_name));
13333431Scarlsonj
13343431Scarlsonj if (ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1 ||
13353431Scarlsonj !(lifr.lifr_flags & IFF_RUNNING)) {
13363431Scarlsonj /*
13373431Scarlsonj * If we've lost the interface or it has gone down, then
13383431Scarlsonj * nothing special to do; just turn off the running flag.
13393431Scarlsonj */
13403431Scarlsonj pif_status(pif, B_FALSE);
13413431Scarlsonj } else {
13423431Scarlsonj /*
13433431Scarlsonj * Interface has come back up: go through verification process.
13443431Scarlsonj */
13453431Scarlsonj pif_status(pif, B_TRUE);
13463431Scarlsonj }
13472546Scarlsonj }
13482546Scarlsonj
13492546Scarlsonj /*
13502546Scarlsonj * rtsock_event(): fetches routing socket messages and updates internal
13512546Scarlsonj * interface state based on those messages.
13522546Scarlsonj *
13532546Scarlsonj * input: iu_eh_t *: unused
13542546Scarlsonj * int: the routing socket file descriptor
13552546Scarlsonj * (other arguments unused)
13562546Scarlsonj * output: void
13572546Scarlsonj */
13582546Scarlsonj
13592546Scarlsonj /* ARGSUSED */
13602546Scarlsonj static void
rtsock_event(iu_eh_t * ehp,int fd,short events,iu_event_id_t id,void * arg)13612546Scarlsonj rtsock_event(iu_eh_t *ehp, int fd, short events, iu_event_id_t id, void *arg)
13622546Scarlsonj {
13633431Scarlsonj dhcp_smach_t *dsmp, *dsmnext;
13642546Scarlsonj union {
13652546Scarlsonj struct ifa_msghdr ifam;
13663431Scarlsonj struct if_msghdr ifm;
13672546Scarlsonj char buf[1024];
13682546Scarlsonj } msg;
13692546Scarlsonj uint16_t ifindex;
13702546Scarlsonj int msglen;
13713431Scarlsonj boolean_t isv6;
13722546Scarlsonj
13732546Scarlsonj if ((msglen = read(fd, &msg, sizeof (msg))) <= 0)
13742546Scarlsonj return;
13752546Scarlsonj
13763431Scarlsonj /* Note that the routing socket interface index is just 16 bits */
13773431Scarlsonj if (msg.ifm.ifm_type == RTM_IFINFO) {
13783431Scarlsonj ifindex = msg.ifm.ifm_index;
13793431Scarlsonj isv6 = (msg.ifm.ifm_flags & IFF_IPV6) ? B_TRUE : B_FALSE;
13803431Scarlsonj } else if (msg.ifam.ifam_type == RTM_DELADDR ||
13813431Scarlsonj msg.ifam.ifam_type == RTM_NEWADDR) {
13823431Scarlsonj ifindex = msg.ifam.ifam_index;
13833431Scarlsonj isv6 = is_rtm_v6(&msg.ifam, msglen);
13843431Scarlsonj } else {
13852546Scarlsonj return;
13863431Scarlsonj }
13872546Scarlsonj
13883431Scarlsonj for (dsmp = lookup_smach_by_uindex(ifindex, NULL, isv6);
13893431Scarlsonj dsmp != NULL; dsmp = dsmnext) {
13903431Scarlsonj DHCPSTATE oldstate;
13913431Scarlsonj boolean_t lif_finished;
13923431Scarlsonj boolean_t lease_removed;
13933431Scarlsonj dhcp_lease_t *dlp, *dlnext;
13942546Scarlsonj
13952546Scarlsonj /*
13963431Scarlsonj * Note that script_start can call dhcp_drop directly, and
13973431Scarlsonj * that will do release_smach.
13982546Scarlsonj */
13993431Scarlsonj dsmnext = lookup_smach_by_uindex(ifindex, dsmp, isv6);
14003431Scarlsonj oldstate = dsmp->dsm_state;
14013431Scarlsonj
14023431Scarlsonj /*
14036508Scarlsonj * Ignore state machines that are currently processing drop or
14046508Scarlsonj * release; there is nothing more we can do for them.
14056508Scarlsonj */
14066508Scarlsonj if (dsmp->dsm_droprelease)
14076508Scarlsonj continue;
14086508Scarlsonj
14096508Scarlsonj /*
14103431Scarlsonj * Look for link up/down notifications. These occur on a
14113431Scarlsonj * physical interface basis.
14123431Scarlsonj */
14133431Scarlsonj if (msg.ifm.ifm_type == RTM_IFINFO) {
14143431Scarlsonj process_link_up_down(dsmp->dsm_lif->lif_pif, &msg.ifm);
14152546Scarlsonj continue;
14163431Scarlsonj }
14172546Scarlsonj
14182546Scarlsonj /*
14192546Scarlsonj * Since we cannot trust the flags reported by the routing
14202546Scarlsonj * socket (they're just 32 bits -- and thus never include
14212546Scarlsonj * IFF_DUPLICATE), and we can't trust the ifindex (it's only 16
14222546Scarlsonj * bits and also doesn't reflect the alias in use), we get
14232546Scarlsonj * flags on all matching interfaces, and go by that.
14242546Scarlsonj */
14253431Scarlsonj lif_finished = B_FALSE;
14263431Scarlsonj lease_removed = B_FALSE;
14273431Scarlsonj for (dlp = dsmp->dsm_leases; dlp != NULL; dlp = dlnext) {
14283431Scarlsonj dhcp_lif_t *lif, *lifnext;
14293431Scarlsonj uint_t nlifs = dlp->dl_nlifs;
14303431Scarlsonj
14313431Scarlsonj dlnext = dlp->dl_next;
14323431Scarlsonj for (lif = dlp->dl_lifs; lif != NULL && nlifs > 0;
14333431Scarlsonj lif = lifnext, nlifs--) {
14343431Scarlsonj lifnext = lif->lif_next;
14353431Scarlsonj if (check_lif(lif, &msg.ifam, msglen)) {
14363431Scarlsonj dsmp->dsm_lif_wait--;
14373431Scarlsonj lif_finished = B_TRUE;
14383431Scarlsonj }
14393431Scarlsonj }
14403431Scarlsonj if (dlp->dl_nlifs == 0) {
14413431Scarlsonj remove_lease(dlp);
14423431Scarlsonj lease_removed = B_TRUE;
14433431Scarlsonj }
14443431Scarlsonj }
14453431Scarlsonj
14463431Scarlsonj if ((isv6 && !check_main_lif(dsmp, &msg.ifam, msglen)) ||
14473431Scarlsonj (!isv6 && !verify_lif(dsmp->dsm_lif))) {
1448*12989SVasumathi.Sundaram@oracle.COM finished_smach(dsmp, DHCP_IPC_E_INVIF);
14492546Scarlsonj continue;
14503431Scarlsonj }
14513431Scarlsonj
14523431Scarlsonj /*
14533431Scarlsonj * Ignore this state machine if nothing interesting has
14543431Scarlsonj * happened.
14553431Scarlsonj */
14563431Scarlsonj if (!lif_finished && dsmp->dsm_lif_down == 0 &&
14573431Scarlsonj (dsmp->dsm_leases != NULL || !lease_removed))
14583431Scarlsonj continue;
14593431Scarlsonj
14603431Scarlsonj /*
14613431Scarlsonj * If we're still waiting for DAD to complete on some of the
14623431Scarlsonj * configured LIFs, then don't send a response.
14633431Scarlsonj */
14643431Scarlsonj if (dsmp->dsm_lif_wait != 0) {
14653431Scarlsonj dhcpmsg(MSG_VERBOSE, "rtsock_event: %s still has %d "
14663431Scarlsonj "LIFs waiting on DAD", dsmp->dsm_name,
14673431Scarlsonj dsmp->dsm_lif_wait);
14682546Scarlsonj continue;
14692546Scarlsonj }
14702546Scarlsonj
14713431Scarlsonj /*
14723431Scarlsonj * If we have some failed LIFs, then handle them now. We'll
14733431Scarlsonj * remove them from the list. Any leases that become empty are
14743431Scarlsonj * also removed as part of the decline-generation process.
14753431Scarlsonj */
14765381Smeem if (dsmp->dsm_lif_down != 0)
14773431Scarlsonj send_declines(dsmp);
14782546Scarlsonj
14793431Scarlsonj if (dsmp->dsm_leases == NULL) {
14803431Scarlsonj dsmp->dsm_bad_offers++;
14813431Scarlsonj /*
14823431Scarlsonj * For DHCPv6, we'll process the restart once we're
14833431Scarlsonj * done sending Decline messages, because these are
14843431Scarlsonj * supposed to be acknowledged. With DHCPv4, there's
14853431Scarlsonj * no acknowledgment for a DECLINE, so after sending
14863431Scarlsonj * it, we just restart right away.
14873431Scarlsonj */
14883431Scarlsonj if (!dsmp->dsm_isv6) {
14893431Scarlsonj dhcpmsg(MSG_VERBOSE, "rtsock_event: %s has no "
14904516Scarlsonj "LIFs left", dsmp->dsm_name);
14913431Scarlsonj dhcp_restart(dsmp);
14923431Scarlsonj }
14933431Scarlsonj } else {
14943431Scarlsonj /*
14953431Scarlsonj * If we're now up on at least some of the leases and
14963431Scarlsonj * we were waiting for that, then kick off the rest of
14973431Scarlsonj * configuration. Lease validation and DAD are done.
14983431Scarlsonj */
14993431Scarlsonj dhcpmsg(MSG_VERBOSE, "rtsock_event: all LIFs verified "
15003431Scarlsonj "on %s in %s state", dsmp->dsm_name,
15013431Scarlsonj dhcp_state_to_string(oldstate));
15023431Scarlsonj if (oldstate == PRE_BOUND ||
15033431Scarlsonj oldstate == ADOPTING)
15043431Scarlsonj dhcp_bound_complete(dsmp);
15053431Scarlsonj if (oldstate == ADOPTING)
15063431Scarlsonj dhcp_adopt_complete(dsmp);
15073431Scarlsonj }
15082546Scarlsonj }
15092546Scarlsonj }
15103431Scarlsonj
15113431Scarlsonj /*
15123431Scarlsonj * check_cmd_allowed(): check whether the requested command is allowed in the
15133431Scarlsonj * state specified.
15143431Scarlsonj *
15153431Scarlsonj * input: DHCPSTATE: current state
15163431Scarlsonj * dhcp_ipc_type_t: requested command
15173431Scarlsonj * output: boolean_t: B_TRUE if command is allowed in this state
15183431Scarlsonj */
15193431Scarlsonj
15203431Scarlsonj boolean_t
check_cmd_allowed(DHCPSTATE state,dhcp_ipc_type_t cmd)15213431Scarlsonj check_cmd_allowed(DHCPSTATE state, dhcp_ipc_type_t cmd)
15223431Scarlsonj {
15233431Scarlsonj return (ipc_cmd_allowed[state][cmd] != 0);
15243431Scarlsonj }
15258194SJack.Meng@Sun.COM
15268194SJack.Meng@Sun.COM static boolean_t
is_iscsi_active(void)15278194SJack.Meng@Sun.COM is_iscsi_active(void)
15288194SJack.Meng@Sun.COM {
15299508SPeter.Memishian@Sun.COM int fd;
15309508SPeter.Memishian@Sun.COM int active = 0;
15318194SJack.Meng@Sun.COM
15329508SPeter.Memishian@Sun.COM if ((fd = open(ISCSI_DRIVER_DEVCTL, O_RDONLY)) != -1) {
15339508SPeter.Memishian@Sun.COM if (ioctl(fd, ISCSI_IS_ACTIVE, &active) != 0)
15349508SPeter.Memishian@Sun.COM active = 0;
15359508SPeter.Memishian@Sun.COM (void) close(fd);
15368194SJack.Meng@Sun.COM }
15378194SJack.Meng@Sun.COM
15389508SPeter.Memishian@Sun.COM return (active != 0);
15398194SJack.Meng@Sun.COM }
1540