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 /* 223431Scarlsonj * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include <sys/types.h> 290Sstevel@tonic-gate #include <stdlib.h> 300Sstevel@tonic-gate #include <errno.h> 310Sstevel@tonic-gate #include <locale.h> 320Sstevel@tonic-gate #include <string.h> 330Sstevel@tonic-gate #include <unistd.h> 340Sstevel@tonic-gate #include <signal.h> 350Sstevel@tonic-gate #include <stdio.h> 361914Scasper #include <stdio_ext.h> 370Sstevel@tonic-gate #include <dhcp_hostconf.h> 380Sstevel@tonic-gate #include <dhcpagent_ipc.h> 393431Scarlsonj #include <dhcpagent_util.h> 400Sstevel@tonic-gate #include <dhcpmsg.h> 410Sstevel@tonic-gate #include <netinet/dhcp.h> 422546Scarlsonj #include <net/route.h> 432546Scarlsonj #include <sys/sockio.h> 440Sstevel@tonic-gate 450Sstevel@tonic-gate #include "async.h" 460Sstevel@tonic-gate #include "agent.h" 470Sstevel@tonic-gate #include "script_handler.h" 480Sstevel@tonic-gate #include "util.h" 490Sstevel@tonic-gate #include "class_id.h" 500Sstevel@tonic-gate #include "states.h" 510Sstevel@tonic-gate #include "packet.h" 523431Scarlsonj #include "interface.h" 533431Scarlsonj #include "defaults.h" 540Sstevel@tonic-gate 550Sstevel@tonic-gate #ifndef TEXT_DOMAIN 560Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 570Sstevel@tonic-gate #endif 580Sstevel@tonic-gate 590Sstevel@tonic-gate iu_timer_id_t inactivity_id; 600Sstevel@tonic-gate int class_id_len = 0; 610Sstevel@tonic-gate char *class_id; 620Sstevel@tonic-gate iu_eh_t *eh; 630Sstevel@tonic-gate iu_tq_t *tq; 640Sstevel@tonic-gate pid_t grandparent; 652546Scarlsonj int rtsock_fd; 660Sstevel@tonic-gate 670Sstevel@tonic-gate static boolean_t shutdown_started = B_FALSE; 680Sstevel@tonic-gate static boolean_t do_adopt = B_FALSE; 690Sstevel@tonic-gate static unsigned int debug_level = 0; 702546Scarlsonj static iu_eh_callback_t accept_event, ipc_event, rtsock_event; 710Sstevel@tonic-gate 720Sstevel@tonic-gate /* 730Sstevel@tonic-gate * The ipc_cmd_allowed[] table indicates which IPC commands are allowed in 740Sstevel@tonic-gate * which states; a non-zero value indicates the command is permitted. 750Sstevel@tonic-gate * 763431Scarlsonj * START is permitted if the state machine is fresh, or if we are in the 773431Scarlsonj * process of trying to obtain a lease (as a convenience to save the 783431Scarlsonj * administrator from having to do an explicit DROP). EXTEND, RELEASE, and 793431Scarlsonj * GET_TAG require a lease to be obtained in order to make sense. INFORM is 803431Scarlsonj * permitted if the interface is fresh or has an INFORM in progress or 813431Scarlsonj * previously done on it -- otherwise a DROP or RELEASE is first required. 823431Scarlsonj * PING and STATUS always make sense and thus are always permitted, as is DROP 833431Scarlsonj * in order to permit the administrator to always bail out. 840Sstevel@tonic-gate */ 850Sstevel@tonic-gate static int ipc_cmd_allowed[DHCP_NSTATES][DHCP_NIPC] = { 860Sstevel@tonic-gate /* D E P R S S I G */ 870Sstevel@tonic-gate /* R X I E T T N E */ 880Sstevel@tonic-gate /* O T N L A A F T */ 890Sstevel@tonic-gate /* P E G E R T O _ */ 900Sstevel@tonic-gate /* . N . A T U R T */ 910Sstevel@tonic-gate /* . D . S . S M A */ 920Sstevel@tonic-gate /* . . . E . . . G */ 930Sstevel@tonic-gate /* INIT */ { 1, 0, 1, 0, 1, 1, 1, 0 }, 940Sstevel@tonic-gate /* SELECTING */ { 1, 0, 1, 0, 1, 1, 0, 0 }, 950Sstevel@tonic-gate /* REQUESTING */ { 1, 0, 1, 0, 1, 1, 0, 0 }, 962546Scarlsonj /* PRE_BOUND */ { 1, 1, 1, 1, 0, 1, 0, 1 }, 970Sstevel@tonic-gate /* BOUND */ { 1, 1, 1, 1, 0, 1, 0, 1 }, 980Sstevel@tonic-gate /* RENEWING */ { 1, 1, 1, 1, 0, 1, 0, 1 }, 990Sstevel@tonic-gate /* REBINDING */ { 1, 1, 1, 1, 0, 1, 0, 1 }, 1003431Scarlsonj /* INFORMATION */ { 1, 0, 1, 0, 1, 1, 1, 1 }, 1013431Scarlsonj /* INIT_REBOOT */ { 1, 0, 1, 1, 1, 1, 0, 0 }, 1023431Scarlsonj /* ADOPTING */ { 1, 0, 1, 1, 0, 1, 0, 0 }, 1033431Scarlsonj /* INFORM_SENT */ { 1, 0, 1, 0, 1, 1, 1, 0 }, 1043431Scarlsonj /* DECLINING */ { 1, 1, 1, 1, 0, 1, 0, 1 }, 1053431Scarlsonj /* RELEASING */ { 1, 0, 1, 0, 0, 1, 0, 1 }, 1063431Scarlsonj }; 1073431Scarlsonj 1083431Scarlsonj #define CMD_ISPRIV 0x1 /* Command requires privileges */ 1093431Scarlsonj #define CMD_CREATE 0x2 /* Command creates an interface */ 1103431Scarlsonj #define CMD_BOOTP 0x4 /* Command is valid with BOOTP */ 1113431Scarlsonj #define CMD_IMMED 0x8 /* Reply is immediate (no BUSY state) */ 1123431Scarlsonj 1133431Scarlsonj static uint_t ipc_cmd_flags[DHCP_NIPC] = { 1143431Scarlsonj /* DHCP_DROP */ CMD_ISPRIV|CMD_BOOTP, 1153431Scarlsonj /* DHCP_EXTEND */ CMD_ISPRIV, 1163431Scarlsonj /* DHCP_PING */ CMD_BOOTP|CMD_IMMED, 1173431Scarlsonj /* DHCP_RELEASE */ CMD_ISPRIV, 1183431Scarlsonj /* DHCP_START */ CMD_CREATE|CMD_ISPRIV|CMD_BOOTP, 1193431Scarlsonj /* DHCP_STATUS */ CMD_BOOTP|CMD_IMMED, 1203431Scarlsonj /* DHCP_INFORM */ CMD_CREATE|CMD_ISPRIV, 1213431Scarlsonj /* DHCP_GET_TAG */ CMD_BOOTP|CMD_IMMED 1220Sstevel@tonic-gate }; 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate int 1250Sstevel@tonic-gate main(int argc, char **argv) 1260Sstevel@tonic-gate { 1270Sstevel@tonic-gate boolean_t is_daemon = B_TRUE; 1283431Scarlsonj boolean_t is_verbose; 1290Sstevel@tonic-gate int ipc_fd; 1300Sstevel@tonic-gate int c; 1310Sstevel@tonic-gate struct rlimit rl; 1320Sstevel@tonic-gate 1333431Scarlsonj debug_level = df_get_int("", B_FALSE, DF_DEBUG_LEVEL); 1343431Scarlsonj is_verbose = df_get_bool("", B_FALSE, DF_VERBOSE); 1353431Scarlsonj 1360Sstevel@tonic-gate /* 1370Sstevel@tonic-gate * -l is ignored for compatibility with old agent. 1380Sstevel@tonic-gate */ 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate while ((c = getopt(argc, argv, "vd:l:fa")) != EOF) { 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate switch (c) { 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate case 'a': 1450Sstevel@tonic-gate do_adopt = B_TRUE; 1460Sstevel@tonic-gate grandparent = getpid(); 1470Sstevel@tonic-gate break; 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate case 'd': 1500Sstevel@tonic-gate debug_level = strtoul(optarg, NULL, 0); 1510Sstevel@tonic-gate break; 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate case 'f': 1540Sstevel@tonic-gate is_daemon = B_FALSE; 1550Sstevel@tonic-gate break; 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate case 'v': 1580Sstevel@tonic-gate is_verbose = B_TRUE; 1590Sstevel@tonic-gate break; 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate case '?': 1620Sstevel@tonic-gate (void) fprintf(stderr, "usage: %s [-a] [-d n] [-f] [-v]" 1630Sstevel@tonic-gate "\n", argv[0]); 1640Sstevel@tonic-gate return (EXIT_FAILURE); 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate default: 1670Sstevel@tonic-gate break; 1680Sstevel@tonic-gate } 1690Sstevel@tonic-gate } 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1720Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate if (geteuid() != 0) { 1750Sstevel@tonic-gate dhcpmsg_init(argv[0], B_FALSE, is_verbose, debug_level); 1760Sstevel@tonic-gate dhcpmsg(MSG_ERROR, "must be super-user"); 1770Sstevel@tonic-gate dhcpmsg_fini(); 1780Sstevel@tonic-gate return (EXIT_FAILURE); 1790Sstevel@tonic-gate } 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate if (is_daemon && daemonize() == 0) { 1820Sstevel@tonic-gate dhcpmsg_init(argv[0], B_FALSE, is_verbose, debug_level); 1830Sstevel@tonic-gate dhcpmsg(MSG_ERR, "cannot become daemon, exiting"); 1840Sstevel@tonic-gate dhcpmsg_fini(); 1850Sstevel@tonic-gate return (EXIT_FAILURE); 1860Sstevel@tonic-gate } 1870Sstevel@tonic-gate 1883431Scarlsonj /* 1893431Scarlsonj * Seed the random number generator, since we're going to need it 1903431Scarlsonj * to set transaction id's and for exponential backoff. 1913431Scarlsonj */ 1923431Scarlsonj srand48(gethrtime() ^ gethostid() ^ getpid()); 1933431Scarlsonj 1940Sstevel@tonic-gate dhcpmsg_init(argv[0], is_daemon, is_verbose, debug_level); 1950Sstevel@tonic-gate (void) atexit(dhcpmsg_fini); 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate tq = iu_tq_create(); 1980Sstevel@tonic-gate eh = iu_eh_create(); 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate if (eh == NULL || tq == NULL) { 2010Sstevel@tonic-gate errno = ENOMEM; 2020Sstevel@tonic-gate dhcpmsg(MSG_ERR, "cannot create timer queue or event handler"); 2030Sstevel@tonic-gate return (EXIT_FAILURE); 2040Sstevel@tonic-gate } 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate /* 2070Sstevel@tonic-gate * ignore most signals that could be reasonably generated. 2080Sstevel@tonic-gate */ 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate (void) signal(SIGTERM, graceful_shutdown); 2110Sstevel@tonic-gate (void) signal(SIGQUIT, graceful_shutdown); 2120Sstevel@tonic-gate (void) signal(SIGPIPE, SIG_IGN); 2130Sstevel@tonic-gate (void) signal(SIGUSR1, SIG_IGN); 2140Sstevel@tonic-gate (void) signal(SIGUSR2, SIG_IGN); 2150Sstevel@tonic-gate (void) signal(SIGINT, SIG_IGN); 2160Sstevel@tonic-gate (void) signal(SIGHUP, SIG_IGN); 2170Sstevel@tonic-gate (void) signal(SIGCHLD, SIG_IGN); 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate /* 2200Sstevel@tonic-gate * upon SIGTHAW we need to refresh any non-infinite leases. 2210Sstevel@tonic-gate */ 2220Sstevel@tonic-gate 2233431Scarlsonj (void) iu_eh_register_signal(eh, SIGTHAW, refresh_smachs, NULL); 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate class_id = get_class_id(); 2260Sstevel@tonic-gate if (class_id != NULL) 2270Sstevel@tonic-gate class_id_len = strlen(class_id); 2280Sstevel@tonic-gate else 2290Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "get_class_id failed, continuing " 2300Sstevel@tonic-gate "with no vendor class id"); 2310Sstevel@tonic-gate 2320Sstevel@tonic-gate /* 2330Sstevel@tonic-gate * the inactivity timer is enabled any time there are no 2340Sstevel@tonic-gate * interfaces under DHCP control. if DHCP_INACTIVITY_WAIT 2350Sstevel@tonic-gate * seconds transpire without an interface under DHCP control, 2360Sstevel@tonic-gate * the agent shuts down. 2370Sstevel@tonic-gate */ 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate inactivity_id = iu_schedule_timer(tq, DHCP_INACTIVITY_WAIT, 2400Sstevel@tonic-gate inactivity_shutdown, NULL); 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate /* 2430Sstevel@tonic-gate * max out the number available descriptors, just in case.. 2440Sstevel@tonic-gate */ 2450Sstevel@tonic-gate 2460Sstevel@tonic-gate rl.rlim_cur = RLIM_INFINITY; 2470Sstevel@tonic-gate rl.rlim_max = RLIM_INFINITY; 2480Sstevel@tonic-gate if (setrlimit(RLIMIT_NOFILE, &rl) == -1) 2490Sstevel@tonic-gate dhcpmsg(MSG_ERR, "setrlimit failed"); 2500Sstevel@tonic-gate 2511914Scasper (void) enable_extended_FILE_stdio(-1, -1); 2521914Scasper 2530Sstevel@tonic-gate /* 2543431Scarlsonj * Create and bind default IP sockets used to control interfaces and to 2553431Scarlsonj * catch stray packets. 2563431Scarlsonj */ 2573431Scarlsonj 2583431Scarlsonj if (!dhcp_ip_default()) 2593431Scarlsonj return (EXIT_FAILURE); 2603431Scarlsonj 2613431Scarlsonj /* 2620Sstevel@tonic-gate * create the ipc channel that the agent will listen for 2630Sstevel@tonic-gate * requests on, and register it with the event handler so that 2640Sstevel@tonic-gate * `accept_event' will be called back. 2650Sstevel@tonic-gate */ 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate switch (dhcp_ipc_init(&ipc_fd)) { 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate case 0: 2700Sstevel@tonic-gate break; 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate case DHCP_IPC_E_BIND: 2730Sstevel@tonic-gate dhcpmsg(MSG_ERROR, "dhcp_ipc_init: cannot bind to port " 2740Sstevel@tonic-gate "%i (agent already running?)", IPPORT_DHCPAGENT); 2750Sstevel@tonic-gate return (EXIT_FAILURE); 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate default: 2780Sstevel@tonic-gate dhcpmsg(MSG_ERROR, "dhcp_ipc_init failed"); 2790Sstevel@tonic-gate return (EXIT_FAILURE); 2800Sstevel@tonic-gate } 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate if (iu_register_event(eh, ipc_fd, POLLIN, accept_event, 0) == -1) { 2830Sstevel@tonic-gate dhcpmsg(MSG_ERR, "cannot register ipc fd for messages"); 2840Sstevel@tonic-gate return (EXIT_FAILURE); 2850Sstevel@tonic-gate } 2860Sstevel@tonic-gate 2870Sstevel@tonic-gate /* 2882546Scarlsonj * Create the global routing socket. This is used for monitoring 2892546Scarlsonj * interface transitions, so that we learn about the kernel's Duplicate 2902546Scarlsonj * Address Detection status, and for inserting and removing default 2913431Scarlsonj * routes as learned from DHCP servers. Both v4 and v6 are handed 2923431Scarlsonj * with this one socket. 2932546Scarlsonj */ 2943431Scarlsonj rtsock_fd = socket(PF_ROUTE, SOCK_RAW, 0); 2952546Scarlsonj if (rtsock_fd == -1) { 2962546Scarlsonj dhcpmsg(MSG_ERR, "cannot open routing socket"); 2972546Scarlsonj return (EXIT_FAILURE); 2982546Scarlsonj } 2992546Scarlsonj if (iu_register_event(eh, rtsock_fd, POLLIN, rtsock_event, 0) == -1) { 3002546Scarlsonj dhcpmsg(MSG_ERR, "cannot register routing socket for messages"); 3012546Scarlsonj return (EXIT_FAILURE); 3022546Scarlsonj } 3032546Scarlsonj 3042546Scarlsonj /* 3050Sstevel@tonic-gate * if the -a (adopt) option was specified, try to adopt the 3062612Scarlsonj * kernel-managed interface before we start. 3070Sstevel@tonic-gate */ 3080Sstevel@tonic-gate 3093431Scarlsonj if (do_adopt && !dhcp_adopt()) 3102612Scarlsonj return (EXIT_FAILURE); 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate /* 3133431Scarlsonj * For DHCPv6, we own all of the interfaces marked DHCPRUNNING. As 3143431Scarlsonj * we're starting operation here, if there are any of those interfaces 3153431Scarlsonj * lingering around, they're strays, and need to be removed. 3163431Scarlsonj * 3173431Scarlsonj * It might be nice to save these addresses off somewhere -- for both 3183431Scarlsonj * v4 and v6 -- and use them as hints for later negotiation. 3193431Scarlsonj */ 3203431Scarlsonj remove_v6_strays(); 3213431Scarlsonj 3223431Scarlsonj /* 3230Sstevel@tonic-gate * enter the main event loop; this is where all the real work 3240Sstevel@tonic-gate * takes place (through registering events and scheduling timers). 3250Sstevel@tonic-gate * this function only returns when the agent is shutting down. 3260Sstevel@tonic-gate */ 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate switch (iu_handle_events(eh, tq)) { 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate case -1: 3310Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "iu_handle_events exited abnormally"); 3320Sstevel@tonic-gate break; 3330Sstevel@tonic-gate 3340Sstevel@tonic-gate case DHCP_REASON_INACTIVITY: 3350Sstevel@tonic-gate dhcpmsg(MSG_INFO, "no interfaces to manage, shutting down..."); 3360Sstevel@tonic-gate break; 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate case DHCP_REASON_TERMINATE: 3390Sstevel@tonic-gate dhcpmsg(MSG_INFO, "received SIGTERM, shutting down..."); 3400Sstevel@tonic-gate break; 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate case DHCP_REASON_SIGNAL: 3430Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "received unexpected signal, shutting " 3440Sstevel@tonic-gate "down..."); 3450Sstevel@tonic-gate break; 3460Sstevel@tonic-gate } 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate (void) iu_eh_unregister_signal(eh, SIGTHAW, NULL); 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate iu_eh_destroy(eh); 3510Sstevel@tonic-gate iu_tq_destroy(tq); 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate return (EXIT_SUCCESS); 3540Sstevel@tonic-gate } 3550Sstevel@tonic-gate 3560Sstevel@tonic-gate /* 3570Sstevel@tonic-gate * drain_script(): event loop callback during shutdown 3580Sstevel@tonic-gate * 3590Sstevel@tonic-gate * input: eh_t *: unused 3600Sstevel@tonic-gate * void *: unused 3610Sstevel@tonic-gate * output: boolean_t: B_TRUE if event loop should exit; B_FALSE otherwise 3620Sstevel@tonic-gate */ 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate /* ARGSUSED */ 3650Sstevel@tonic-gate boolean_t 3660Sstevel@tonic-gate drain_script(iu_eh_t *ehp, void *arg) 3670Sstevel@tonic-gate { 3680Sstevel@tonic-gate if (shutdown_started == B_FALSE) { 3690Sstevel@tonic-gate shutdown_started = B_TRUE; 3700Sstevel@tonic-gate if (do_adopt == B_FALSE) /* see 4291141 */ 3713431Scarlsonj nuke_smach_list(); 3720Sstevel@tonic-gate } 3730Sstevel@tonic-gate return (script_count == 0); 3740Sstevel@tonic-gate } 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate /* 3770Sstevel@tonic-gate * accept_event(): accepts a new connection on the ipc socket and registers 3780Sstevel@tonic-gate * to receive its messages with the event handler 3790Sstevel@tonic-gate * 3800Sstevel@tonic-gate * input: iu_eh_t *: unused 3810Sstevel@tonic-gate * int: the file descriptor in the iu_eh_t * the connection came in on 3820Sstevel@tonic-gate * (other arguments unused) 3830Sstevel@tonic-gate * output: void 3840Sstevel@tonic-gate */ 3850Sstevel@tonic-gate 3860Sstevel@tonic-gate /* ARGSUSED */ 3870Sstevel@tonic-gate static void 3880Sstevel@tonic-gate accept_event(iu_eh_t *ehp, int fd, short events, iu_event_id_t id, void *arg) 3890Sstevel@tonic-gate { 3900Sstevel@tonic-gate int client_fd; 3910Sstevel@tonic-gate int is_priv; 3920Sstevel@tonic-gate 3930Sstevel@tonic-gate if (dhcp_ipc_accept(fd, &client_fd, &is_priv) != 0) { 3940Sstevel@tonic-gate dhcpmsg(MSG_ERR, "accept_event: accept on ipc socket"); 3950Sstevel@tonic-gate return; 3960Sstevel@tonic-gate } 3970Sstevel@tonic-gate 3980Sstevel@tonic-gate if (iu_register_event(eh, client_fd, POLLIN, ipc_event, 3990Sstevel@tonic-gate (void *)is_priv) == -1) { 4000Sstevel@tonic-gate dhcpmsg(MSG_ERROR, "accept_event: cannot register ipc socket " 4010Sstevel@tonic-gate "for callback"); 4020Sstevel@tonic-gate } 4030Sstevel@tonic-gate } 4040Sstevel@tonic-gate 4050Sstevel@tonic-gate /* 4060Sstevel@tonic-gate * ipc_event(): processes incoming ipc requests 4070Sstevel@tonic-gate * 4080Sstevel@tonic-gate * input: iu_eh_t *: unused 4090Sstevel@tonic-gate * int: the file descriptor in the iu_eh_t * the request came in on 4100Sstevel@tonic-gate * short: unused 4113431Scarlsonj * iu_event_id_t: event ID 4120Sstevel@tonic-gate * void *: indicates whether the request is from a privileged client 4130Sstevel@tonic-gate * output: void 4140Sstevel@tonic-gate */ 4150Sstevel@tonic-gate 4160Sstevel@tonic-gate /* ARGSUSED */ 4170Sstevel@tonic-gate static void 4180Sstevel@tonic-gate ipc_event(iu_eh_t *ehp, int fd, short events, iu_event_id_t id, void *arg) 4190Sstevel@tonic-gate { 4203431Scarlsonj ipc_action_t ia, *iap; 4213431Scarlsonj dhcp_smach_t *dsmp; 4220Sstevel@tonic-gate int error, is_priv = (int)arg; 4233431Scarlsonj const char *ifname; 4243431Scarlsonj boolean_t isv6; 4250Sstevel@tonic-gate 4263431Scarlsonj ipc_action_init(&ia); 4273431Scarlsonj error = dhcp_ipc_recv_request(fd, &ia.ia_request, 4283431Scarlsonj DHCP_IPC_REQUEST_WAIT); 4293431Scarlsonj if (error != DHCP_IPC_SUCCESS) { 4303431Scarlsonj if (error != DHCP_IPC_E_EOF) { 4313431Scarlsonj dhcpmsg(MSG_ERROR, 4323431Scarlsonj "ipc_event: dhcp_ipc_recv_request failed: %s", 4333431Scarlsonj dhcp_ipc_strerror(error)); 4343431Scarlsonj } else { 4353431Scarlsonj dhcpmsg(MSG_DEBUG, "ipc_event: connection closed"); 4363431Scarlsonj } 4373431Scarlsonj if ((dsmp = lookup_smach_by_event(id)) != NULL) { 4383431Scarlsonj ipc_action_finish(dsmp, error); 4393431Scarlsonj } else { 4403431Scarlsonj (void) iu_unregister_event(eh, id, NULL); 4413431Scarlsonj (void) dhcp_ipc_close(fd); 4423431Scarlsonj } 4430Sstevel@tonic-gate return; 4440Sstevel@tonic-gate } 4450Sstevel@tonic-gate 4463431Scarlsonj /* Fill in temporary ipc_action structure for utility functions */ 4473431Scarlsonj ia.ia_cmd = DHCP_IPC_CMD(ia.ia_request->message_type); 4483431Scarlsonj ia.ia_fd = fd; 4493431Scarlsonj ia.ia_eid = id; 4503431Scarlsonj 4513431Scarlsonj if (ia.ia_cmd >= DHCP_NIPC) { 4523431Scarlsonj dhcpmsg(MSG_ERROR, 4533431Scarlsonj "ipc_event: invalid command (%s) attempted on %s", 4543431Scarlsonj dhcp_ipc_type_to_string(ia.ia_cmd), ia.ia_request->ifname); 4553431Scarlsonj send_error_reply(&ia, DHCP_IPC_E_CMD_UNKNOWN); 4560Sstevel@tonic-gate return; 4570Sstevel@tonic-gate } 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate /* return EPERM for any of the privileged actions */ 4600Sstevel@tonic-gate 4613431Scarlsonj if (!is_priv && (ipc_cmd_flags[ia.ia_cmd] & CMD_ISPRIV)) { 4623431Scarlsonj dhcpmsg(MSG_WARNING, 4633431Scarlsonj "ipc_event: privileged ipc command (%s) attempted on %s", 4643431Scarlsonj dhcp_ipc_type_to_string(ia.ia_cmd), ia.ia_request->ifname); 4653431Scarlsonj send_error_reply(&ia, DHCP_IPC_E_PERM); 4663431Scarlsonj return; 4673431Scarlsonj } 4683431Scarlsonj 4693431Scarlsonj /* 4703431Scarlsonj * Try to locate the state machine associated with this command. If 4713431Scarlsonj * the command is DHCP_START or DHCP_INFORM and there isn't a state 4723431Scarlsonj * machine already, make one (there may already be one from a previous 4733431Scarlsonj * failed attempt to START or INFORM). Otherwise, verify the reference 4743431Scarlsonj * is still valid. 4753431Scarlsonj * 4763431Scarlsonj * The interface name may be blank. In that case, we look up the 4773431Scarlsonj * primary interface, and the requested type (v4 or v6) doesn't matter. 4783431Scarlsonj */ 4793431Scarlsonj 4803431Scarlsonj isv6 = (ia.ia_request->message_type & DHCP_V6) != 0; 4813431Scarlsonj ifname = ia.ia_request->ifname; 4823431Scarlsonj if (*ifname == '\0') 4833431Scarlsonj dsmp = primary_smach(isv6); 4843431Scarlsonj else 4853431Scarlsonj dsmp = lookup_smach(ifname, isv6); 4863431Scarlsonj 4873431Scarlsonj if (dsmp != NULL) { 4883431Scarlsonj /* Note that verify_smach drops a reference */ 4893431Scarlsonj hold_smach(dsmp); 4903431Scarlsonj if (!verify_smach(dsmp)) 4913431Scarlsonj dsmp = NULL; 4923431Scarlsonj } 4930Sstevel@tonic-gate 4943431Scarlsonj if (dsmp == NULL) { 4953431Scarlsonj /* 4963431Scarlsonj * If the user asked for the primary DHCP interface, but there 4973431Scarlsonj * is none, then report failure. 4983431Scarlsonj */ 4993431Scarlsonj if (ifname[0] == '\0') { 5003431Scarlsonj error = DHCP_IPC_E_NOPRIMARY; 5013431Scarlsonj 5023431Scarlsonj /* 5033431Scarlsonj * If there's no interface, and we're starting up, then create 5043431Scarlsonj * it now, along with a state machine for it. Note that if 5053431Scarlsonj * insert_smach fails, it discards the LIF reference. 5063431Scarlsonj */ 5073431Scarlsonj } else if (ipc_cmd_flags[ia.ia_cmd] & CMD_CREATE) { 5083431Scarlsonj dhcp_lif_t *lif; 5090Sstevel@tonic-gate 5103431Scarlsonj lif = attach_lif(ifname, isv6, &error); 5113431Scarlsonj if (lif != NULL && 5123431Scarlsonj (dsmp = insert_smach(lif, &error)) != NULL) { 5133431Scarlsonj /* 5143431Scarlsonj * Get client ID and set "DHCPRUNNING" flag on 5153431Scarlsonj * logical interface. (V4 only, because V6 5163431Scarlsonj * plumbs its own interfaces.) 5173431Scarlsonj */ 5183431Scarlsonj error = get_smach_cid(dsmp); 5193431Scarlsonj if (error == DHCP_IPC_SUCCESS) 5203431Scarlsonj error = set_lif_dhcp(lif, B_FALSE); 5213431Scarlsonj if (error != DHCP_IPC_SUCCESS) { 5223522Scarlsonj remove_smach(dsmp); 5233431Scarlsonj dsmp = NULL; 5243431Scarlsonj } 5253431Scarlsonj } 5260Sstevel@tonic-gate 5273431Scarlsonj /* 5283431Scarlsonj * Otherwise, this is an operation on an unknown interface. 5293431Scarlsonj */ 5303431Scarlsonj } else { 5313431Scarlsonj error = DHCP_IPC_E_UNKIF; 5323431Scarlsonj } 5333431Scarlsonj if (dsmp == NULL) { 5343431Scarlsonj send_error_reply(&ia, error); 5350Sstevel@tonic-gate return; 5360Sstevel@tonic-gate } 5370Sstevel@tonic-gate } 5380Sstevel@tonic-gate 5393431Scarlsonj if ((dsmp->dsm_dflags & DHCP_IF_BOOTP) && 5403431Scarlsonj !(ipc_cmd_flags[ia.ia_cmd] & CMD_BOOTP)) { 5413431Scarlsonj dhcpmsg(MSG_ERROR, "command %s not valid for BOOTP on %s", 5423431Scarlsonj dhcp_ipc_type_to_string(ia.ia_cmd), dsmp->dsm_name); 5433431Scarlsonj send_error_reply(&ia, DHCP_IPC_E_BOOTP); 5440Sstevel@tonic-gate return; 5450Sstevel@tonic-gate } 5460Sstevel@tonic-gate 5470Sstevel@tonic-gate /* 5483431Scarlsonj * verify that the state machine is in a state which will allow the 5490Sstevel@tonic-gate * command. we do this up front so that we can return an error 5500Sstevel@tonic-gate * *before* needlessly cancelling an in-progress transaction. 5510Sstevel@tonic-gate */ 5520Sstevel@tonic-gate 5533431Scarlsonj if (!check_cmd_allowed(dsmp->dsm_state, ia.ia_cmd)) { 5543431Scarlsonj dhcpmsg(MSG_DEBUG, 5553431Scarlsonj "in state %s; not allowing %s command on %s", 5563431Scarlsonj dhcp_state_to_string(dsmp->dsm_state), 5573431Scarlsonj dhcp_ipc_type_to_string(ia.ia_cmd), dsmp->dsm_name); 5583431Scarlsonj send_error_reply(&ia, 5593431Scarlsonj ia.ia_cmd == DHCP_START && dsmp->dsm_state != INIT ? 5603431Scarlsonj DHCP_IPC_E_RUNNING : DHCP_IPC_E_OUTSTATE); 5610Sstevel@tonic-gate return; 5620Sstevel@tonic-gate } 5630Sstevel@tonic-gate 5643431Scarlsonj dhcpmsg(MSG_DEBUG, "in state %s; allowing %s command on %s", 5653431Scarlsonj dhcp_state_to_string(dsmp->dsm_state), 5663431Scarlsonj dhcp_ipc_type_to_string(ia.ia_cmd), dsmp->dsm_name); 5673431Scarlsonj 5683431Scarlsonj if ((ia.ia_request->message_type & DHCP_PRIMARY) && is_priv) 5693431Scarlsonj make_primary(dsmp); 5700Sstevel@tonic-gate 5710Sstevel@tonic-gate /* 5723431Scarlsonj * The current design dictates that there can be only one outstanding 5733431Scarlsonj * transaction per state machine -- this simplifies the code 5743431Scarlsonj * considerably and also fits well with RFCs 2131 and 3315. It is 5753431Scarlsonj * worth classifying the different DHCP commands into synchronous 5763431Scarlsonj * (those which we will handle now and reply to immediately) and 5773431Scarlsonj * asynchronous (those which require transactions and will be completed 5783431Scarlsonj * at an indeterminate time in the future): 5790Sstevel@tonic-gate * 5803431Scarlsonj * DROP: removes the agent's management of a state machine. 5810Sstevel@tonic-gate * asynchronous as the script program may be invoked. 5820Sstevel@tonic-gate * 5833431Scarlsonj * PING: checks to see if the agent has a named state machine. 5840Sstevel@tonic-gate * synchronous, since no packets need to be sent 5850Sstevel@tonic-gate * to the DHCP server. 5860Sstevel@tonic-gate * 5873431Scarlsonj * STATUS: returns information about a state machine. 5880Sstevel@tonic-gate * synchronous, since no packets need to be sent 5890Sstevel@tonic-gate * to the DHCP server. 5900Sstevel@tonic-gate * 5913431Scarlsonj * RELEASE: releases the agent's management of a state machine 5923431Scarlsonj * and brings the associated interfaces down. asynchronous 5933431Scarlsonj * as the script program may be invoked. 5940Sstevel@tonic-gate * 5950Sstevel@tonic-gate * EXTEND: renews a lease. asynchronous, since the agent 5960Sstevel@tonic-gate * needs to wait for an ACK, etc. 5970Sstevel@tonic-gate * 5983431Scarlsonj * START: starts DHCP on a named state machine. asynchronous since 5990Sstevel@tonic-gate * the agent needs to wait for OFFERs, ACKs, etc. 6000Sstevel@tonic-gate * 6013431Scarlsonj * INFORM: obtains configuration parameters for the system using 6023431Scarlsonj * externally configured interface. asynchronous, since the 6030Sstevel@tonic-gate * agent needs to wait for an ACK. 6040Sstevel@tonic-gate * 6053431Scarlsonj * Notice that EXTEND, INFORM, START, DROP and RELEASE are 6063431Scarlsonj * asynchronous. Notice also that asynchronous commands may occur from 6073431Scarlsonj * within the agent -- for instance, the agent will need to do implicit 6083431Scarlsonj * EXTENDs to extend the lease. In order to make the code simpler, the 6093431Scarlsonj * following rules apply for asynchronous commands: 6100Sstevel@tonic-gate * 6113431Scarlsonj * There can only be one asynchronous command at a time per state 6123431Scarlsonj * machine. The current asynchronous command is managed by the async_* 6133431Scarlsonj * api: async_start(), async_finish(), and async_cancel(). 6143431Scarlsonj * async_start() starts management of a new asynchronous command on an 6153431Scarlsonj * state machine, which should only be done after async_cancel() to 6163431Scarlsonj * terminate a previous command. When the command is completed, 6173431Scarlsonj * async_finish() should be called. 6180Sstevel@tonic-gate * 6193431Scarlsonj * Asynchronous commands started by a user command have an associated 6203431Scarlsonj * ipc_action which provides the agent with information for how to get 6213431Scarlsonj * in touch with the user command when the action completes. These 6223431Scarlsonj * ipc_action records also have an associated timeout which may be 6233431Scarlsonj * infinite. ipc_action_start() should be called when starting an 6243431Scarlsonj * asynchronous command requested by a user, which sets up the timer 6253431Scarlsonj * and keeps track of the ipc information (file descriptor, request 6263431Scarlsonj * type). When the asynchronous command completes, ipc_action_finish() 6273431Scarlsonj * should be called to return a command status code to the user and 6283431Scarlsonj * close the ipc connection). If the command does not complete before 6293431Scarlsonj * the timer fires, ipc_action_timeout() is called which closes the ipc 6303431Scarlsonj * connection and returns DHCP_IPC_E_TIMEOUT to the user. Note that 6313431Scarlsonj * independent of ipc_action_timeout(), ipc_action_finish() should be 6323431Scarlsonj * called. 6330Sstevel@tonic-gate * 6343431Scarlsonj * on a case-by-case basis, here is what happens (per state machine): 6350Sstevel@tonic-gate * 6363431Scarlsonj * o When an asynchronous command is requested, then 6373431Scarlsonj * async_cancel() is called to terminate any non-user 6383431Scarlsonj * action in progress. If there's a user action running, 6390Sstevel@tonic-gate * the user command is sent DHCP_IPC_E_PEND. 6400Sstevel@tonic-gate * 6410Sstevel@tonic-gate * o otherwise, the the transaction is started with 6420Sstevel@tonic-gate * async_start(). if the transaction is on behalf 6430Sstevel@tonic-gate * of a user, ipc_action_start() is called to keep 6440Sstevel@tonic-gate * track of the ipc information and set up the 6450Sstevel@tonic-gate * ipc_action timer. 6460Sstevel@tonic-gate * 6470Sstevel@tonic-gate * o if the command completes normally and before a 6480Sstevel@tonic-gate * timeout fires, then async_finish() is called. 6490Sstevel@tonic-gate * if there was an associated ipc_action, 6500Sstevel@tonic-gate * ipc_action_finish() is called to complete it. 6510Sstevel@tonic-gate * 6520Sstevel@tonic-gate * o if the command fails before a timeout fires, then 6533431Scarlsonj * async_finish() is called, and the state machine is 6540Sstevel@tonic-gate * is returned to a known state based on the command. 6550Sstevel@tonic-gate * if there was an associated ipc_action, 6560Sstevel@tonic-gate * ipc_action_finish() is called to complete it. 6570Sstevel@tonic-gate * 6580Sstevel@tonic-gate * o if the ipc_action timer fires before command 6590Sstevel@tonic-gate * completion, then DHCP_IPC_E_TIMEOUT is returned to 6600Sstevel@tonic-gate * the user. however, the transaction continues to 6610Sstevel@tonic-gate * be carried out asynchronously. 6620Sstevel@tonic-gate */ 6630Sstevel@tonic-gate 6643431Scarlsonj if (ipc_cmd_flags[ia.ia_cmd] & CMD_IMMED) { 6653431Scarlsonj /* 6663431Scarlsonj * Only immediate commands (ping, status, get_tag) need to 6673431Scarlsonj * worry about freeing ia through one of the reply functions 6683431Scarlsonj * before returning. 6693431Scarlsonj */ 6703431Scarlsonj iap = &ia; 6713431Scarlsonj } else { 6720Sstevel@tonic-gate /* 6730Sstevel@tonic-gate * if shutdown request has been received, send back an error. 6740Sstevel@tonic-gate */ 6750Sstevel@tonic-gate if (shutdown_started) { 6763431Scarlsonj send_error_reply(&ia, DHCP_IPC_E_OUTSTATE); 6770Sstevel@tonic-gate return; 6780Sstevel@tonic-gate } 6790Sstevel@tonic-gate 6803431Scarlsonj if (dsmp->dsm_dflags & DHCP_IF_BUSY) { 6813431Scarlsonj send_error_reply(&ia, DHCP_IPC_E_PEND); 6820Sstevel@tonic-gate return; 6830Sstevel@tonic-gate } 6840Sstevel@tonic-gate 6853431Scarlsonj if (!ipc_action_start(dsmp, &ia)) { 6863431Scarlsonj dhcpmsg(MSG_WARNING, "ipc_event: ipc_action_start " 6873431Scarlsonj "failed for %s", dsmp->dsm_name); 6883431Scarlsonj send_error_reply(&ia, DHCP_IPC_E_MEMORY); 6890Sstevel@tonic-gate return; 6900Sstevel@tonic-gate } 6910Sstevel@tonic-gate 6923431Scarlsonj /* Action structure consumed by above function */ 6933431Scarlsonj iap = &dsmp->dsm_ia; 6940Sstevel@tonic-gate } 6950Sstevel@tonic-gate 6963431Scarlsonj switch (iap->ia_cmd) { 6970Sstevel@tonic-gate 6980Sstevel@tonic-gate case DHCP_DROP: 6993431Scarlsonj (void) script_start(dsmp, isv6 ? EVENT_DROP6 : EVENT_DROP, 7003431Scarlsonj dhcp_drop, NULL, NULL); 7013431Scarlsonj break; /* not an immediate function */ 7020Sstevel@tonic-gate 7030Sstevel@tonic-gate case DHCP_EXTEND: 7043431Scarlsonj (void) dhcp_extending(dsmp); 7050Sstevel@tonic-gate break; 7060Sstevel@tonic-gate 7070Sstevel@tonic-gate case DHCP_GET_TAG: { 7080Sstevel@tonic-gate dhcp_optnum_t optnum; 7093431Scarlsonj void *opt = NULL; 7103431Scarlsonj uint_t optlen; 7110Sstevel@tonic-gate boolean_t did_alloc = B_FALSE; 7123431Scarlsonj PKT_LIST *ack = dsmp->dsm_ack; 7130Sstevel@tonic-gate 7140Sstevel@tonic-gate /* 7150Sstevel@tonic-gate * verify the request makes sense. 7160Sstevel@tonic-gate */ 7170Sstevel@tonic-gate 7183431Scarlsonj if (iap->ia_request->data_type != DHCP_TYPE_OPTNUM || 7193431Scarlsonj iap->ia_request->data_length != sizeof (dhcp_optnum_t)) { 7203431Scarlsonj send_error_reply(iap, DHCP_IPC_E_PROTO); 7213431Scarlsonj break; 7220Sstevel@tonic-gate } 7230Sstevel@tonic-gate 7243431Scarlsonj (void) memcpy(&optnum, iap->ia_request->buffer, 7253431Scarlsonj sizeof (dhcp_optnum_t)); 7263431Scarlsonj 7270Sstevel@tonic-gate load_option: 7280Sstevel@tonic-gate switch (optnum.category) { 7290Sstevel@tonic-gate 7300Sstevel@tonic-gate case DSYM_SITE: /* FALLTHRU */ 7310Sstevel@tonic-gate case DSYM_STANDARD: 7323431Scarlsonj if (isv6) { 7333431Scarlsonj opt = dhcpv6_pkt_option(ack, NULL, optnum.code, 7343431Scarlsonj NULL); 7353431Scarlsonj } else { 7363431Scarlsonj if (optnum.code <= DHCP_LAST_OPT) 7373431Scarlsonj opt = ack->opts[optnum.code]; 7383431Scarlsonj } 7390Sstevel@tonic-gate break; 7400Sstevel@tonic-gate 7410Sstevel@tonic-gate case DSYM_VENDOR: 7423431Scarlsonj if (isv6) { 7433431Scarlsonj dhcpv6_option_t *d6o; 7443431Scarlsonj uint32_t ent; 7450Sstevel@tonic-gate 7463431Scarlsonj /* 7473431Scarlsonj * Look through vendor options to find our 7483431Scarlsonj * enterprise number. 7493431Scarlsonj */ 7503431Scarlsonj d6o = NULL; 7513431Scarlsonj for (;;) { 7523431Scarlsonj d6o = dhcpv6_pkt_option(ack, d6o, 7533431Scarlsonj DHCPV6_OPT_VENDOR_OPT, &optlen); 7543431Scarlsonj if (d6o == NULL) 7553431Scarlsonj break; 7563431Scarlsonj optlen -= sizeof (*d6o); 7573431Scarlsonj if (optlen < sizeof (ent)) 7583431Scarlsonj continue; 7593431Scarlsonj (void) memcpy(&ent, d6o + 1, 7603431Scarlsonj sizeof (ent)); 7613431Scarlsonj if (ntohl(ent) != DHCPV6_SUN_ENT) 7623431Scarlsonj continue; 7633431Scarlsonj break; 7643431Scarlsonj } 7653431Scarlsonj if (d6o != NULL) { 7663431Scarlsonj /* 7673431Scarlsonj * Now find the requested vendor option 7683431Scarlsonj * within the vendor options block. 7693431Scarlsonj */ 7703431Scarlsonj opt = dhcpv6_find_option( 7713431Scarlsonj (char *)(d6o + 1) + sizeof (ent), 7723431Scarlsonj optlen - sizeof (ent), NULL, 7733431Scarlsonj optnum.code, NULL); 7743431Scarlsonj } 7753431Scarlsonj } else { 7763431Scarlsonj /* 7773431Scarlsonj * the test against VS_OPTION_START is broken 7783431Scarlsonj * up into two tests to avoid compiler warnings 7793431Scarlsonj * under intel. 7803431Scarlsonj */ 7813431Scarlsonj if ((optnum.code > VS_OPTION_START || 7823431Scarlsonj optnum.code == VS_OPTION_START) && 7833431Scarlsonj optnum.code <= VS_OPTION_END) 7843431Scarlsonj opt = ack->vs[optnum.code]; 7853431Scarlsonj } 7860Sstevel@tonic-gate break; 7870Sstevel@tonic-gate 7880Sstevel@tonic-gate case DSYM_FIELD: 7893431Scarlsonj if (isv6) { 7903431Scarlsonj dhcpv6_message_t *d6m = 7913431Scarlsonj (dhcpv6_message_t *)ack->pkt; 7923431Scarlsonj dhcpv6_option_t *d6o; 7933431Scarlsonj 7943431Scarlsonj /* Validate the packet field the user wants */ 7953431Scarlsonj optlen = optnum.code + optnum.size; 7963431Scarlsonj if (d6m->d6m_msg_type == 7973431Scarlsonj DHCPV6_MSG_RELAY_FORW || 7983431Scarlsonj d6m->d6m_msg_type == 7993431Scarlsonj DHCPV6_MSG_RELAY_REPL) { 8003431Scarlsonj if (optlen > sizeof (dhcpv6_relay_t)) 8013431Scarlsonj break; 8023431Scarlsonj } else { 8033431Scarlsonj if (optlen > sizeof (*d6m)) 8043431Scarlsonj break; 8053431Scarlsonj } 8060Sstevel@tonic-gate 8073431Scarlsonj opt = malloc(sizeof (*d6o) + optnum.size); 8083431Scarlsonj if (opt != NULL) { 8093431Scarlsonj d6o = opt; 8103431Scarlsonj d6o->d6o_code = htons(optnum.code); 8113431Scarlsonj d6o->d6o_len = htons(optnum.size); 8123431Scarlsonj (void) memcpy(d6o + 1, (caddr_t)d6m + 8133431Scarlsonj optnum.code, optnum.size); 8143431Scarlsonj } 8153431Scarlsonj } else { 8163431Scarlsonj if (optnum.code + optnum.size > sizeof (PKT)) 8173431Scarlsonj break; 8183431Scarlsonj 8193431Scarlsonj /* 8203431Scarlsonj * + 2 to account for option code and length 8213431Scarlsonj * byte 8223431Scarlsonj */ 8233431Scarlsonj opt = malloc(optnum.size + 2); 8243431Scarlsonj if (opt != NULL) { 8253431Scarlsonj DHCP_OPT *v4opt = opt; 8263431Scarlsonj 8273431Scarlsonj v4opt->len = optnum.size; 8283431Scarlsonj v4opt->code = optnum.code; 8293431Scarlsonj (void) memcpy(v4opt->value, 8303431Scarlsonj (caddr_t)ack->pkt + optnum.code, 8313431Scarlsonj optnum.size); 8323431Scarlsonj } 8333431Scarlsonj } 8343431Scarlsonj 8350Sstevel@tonic-gate if (opt == NULL) { 8363431Scarlsonj send_error_reply(iap, DHCP_IPC_E_MEMORY); 8370Sstevel@tonic-gate return; 8380Sstevel@tonic-gate } 8390Sstevel@tonic-gate did_alloc = B_TRUE; 8400Sstevel@tonic-gate break; 8410Sstevel@tonic-gate 8420Sstevel@tonic-gate default: 8433431Scarlsonj send_error_reply(iap, DHCP_IPC_E_PROTO); 8440Sstevel@tonic-gate return; 8450Sstevel@tonic-gate } 8460Sstevel@tonic-gate 8470Sstevel@tonic-gate /* 8480Sstevel@tonic-gate * return the option payload, if there was one. the "+ 2" 8490Sstevel@tonic-gate * accounts for the option code number and length byte. 8500Sstevel@tonic-gate */ 8510Sstevel@tonic-gate 8520Sstevel@tonic-gate if (opt != NULL) { 8533431Scarlsonj if (isv6) { 8543431Scarlsonj dhcpv6_option_t d6ov; 8553431Scarlsonj 8563431Scarlsonj (void) memcpy(&d6ov, opt, sizeof (d6ov)); 8573431Scarlsonj optlen = ntohs(d6ov.d6o_len) + sizeof (d6ov); 8583431Scarlsonj } else { 8593431Scarlsonj optlen = ((DHCP_OPT *)opt)->len + 2; 8603431Scarlsonj } 8613431Scarlsonj send_data_reply(iap, 0, DHCP_TYPE_OPTION, opt, optlen); 8620Sstevel@tonic-gate 8630Sstevel@tonic-gate if (did_alloc) 8640Sstevel@tonic-gate free(opt); 8653431Scarlsonj break; 8663431Scarlsonj } else if (ack != dsmp->dsm_orig_ack) { 8670Sstevel@tonic-gate /* 8680Sstevel@tonic-gate * There wasn't any definition for the option in the 8690Sstevel@tonic-gate * current ack, so now retry with the original ack if 8700Sstevel@tonic-gate * the original ack is not the current ack. 8710Sstevel@tonic-gate */ 8723431Scarlsonj ack = dsmp->dsm_orig_ack; 8730Sstevel@tonic-gate goto load_option; 8740Sstevel@tonic-gate } 8750Sstevel@tonic-gate 8760Sstevel@tonic-gate /* 8770Sstevel@tonic-gate * note that an "okay" response is returned either in 8780Sstevel@tonic-gate * the case of an unknown option or a known option 8790Sstevel@tonic-gate * with no payload. this is okay (for now) since 8800Sstevel@tonic-gate * dhcpinfo checks whether an option is valid before 8810Sstevel@tonic-gate * ever performing ipc with the agent. 8820Sstevel@tonic-gate */ 8830Sstevel@tonic-gate 8843431Scarlsonj send_ok_reply(iap); 8853431Scarlsonj break; 8860Sstevel@tonic-gate } 8870Sstevel@tonic-gate 8880Sstevel@tonic-gate case DHCP_INFORM: 8893431Scarlsonj dhcp_inform(dsmp); 8900Sstevel@tonic-gate /* next destination: dhcp_acknak() */ 8913431Scarlsonj break; /* not an immediate function */ 8920Sstevel@tonic-gate 8930Sstevel@tonic-gate case DHCP_PING: 8943431Scarlsonj if (dsmp->dsm_dflags & DHCP_IF_FAILED) 8953431Scarlsonj send_error_reply(iap, DHCP_IPC_E_FAILEDIF); 8960Sstevel@tonic-gate else 8973431Scarlsonj send_ok_reply(iap); 8983431Scarlsonj break; 8990Sstevel@tonic-gate 9000Sstevel@tonic-gate case DHCP_RELEASE: 9013431Scarlsonj (void) script_start(dsmp, isv6 ? EVENT_RELEASE6 : 9023431Scarlsonj EVENT_RELEASE, dhcp_release, "Finished with lease.", NULL); 9033431Scarlsonj break; /* not an immediate function */ 9040Sstevel@tonic-gate 9053431Scarlsonj case DHCP_START: { 9063431Scarlsonj PKT_LIST *ack, *oack; 9073431Scarlsonj PKT_LIST *plp[2]; 9083431Scarlsonj 9093431Scarlsonj deprecate_leases(dsmp); 9100Sstevel@tonic-gate 9110Sstevel@tonic-gate /* 9120Sstevel@tonic-gate * if we have a valid hostconf lying around, then jump 9130Sstevel@tonic-gate * into INIT_REBOOT. if it fails, we'll end up going 9140Sstevel@tonic-gate * through the whole selecting() procedure again. 9150Sstevel@tonic-gate */ 9160Sstevel@tonic-gate 9173431Scarlsonj error = read_hostconf(dsmp->dsm_name, plp, 2, dsmp->dsm_isv6); 9183431Scarlsonj ack = error > 0 ? plp[0] : NULL; 9193431Scarlsonj oack = error > 1 ? plp[1] : NULL; 9203431Scarlsonj 9213431Scarlsonj /* 9223431Scarlsonj * If the allocation of the old ack fails, that's fine; 9233431Scarlsonj * continue without it. 9243431Scarlsonj */ 9253431Scarlsonj if (oack == NULL) 9263431Scarlsonj oack = ack; 9273431Scarlsonj 9283431Scarlsonj /* 9293431Scarlsonj * As long as we've allocated something, start using it. 9303431Scarlsonj */ 9313431Scarlsonj if (ack != NULL) { 9323431Scarlsonj dsmp->dsm_orig_ack = oack; 9333431Scarlsonj dsmp->dsm_ack = ack; 9343431Scarlsonj dhcp_init_reboot(dsmp); 9350Sstevel@tonic-gate /* next destination: dhcp_acknak() */ 9363431Scarlsonj break; 9370Sstevel@tonic-gate } 9380Sstevel@tonic-gate 9390Sstevel@tonic-gate /* 9400Sstevel@tonic-gate * if not debugging, wait for a few seconds before 9410Sstevel@tonic-gate * going into SELECTING. 9420Sstevel@tonic-gate */ 9430Sstevel@tonic-gate 944*4106Scarlsonj if (debug_level == 0 && set_start_timer(dsmp)) { 9453431Scarlsonj /* next destination: dhcp_start() */ 9463431Scarlsonj break; 947*4106Scarlsonj } else { 948*4106Scarlsonj dhcp_selecting(dsmp); 949*4106Scarlsonj /* next destination: dhcp_requesting() */ 950*4106Scarlsonj break; 9510Sstevel@tonic-gate } 9523431Scarlsonj } 9530Sstevel@tonic-gate 9540Sstevel@tonic-gate case DHCP_STATUS: { 9550Sstevel@tonic-gate dhcp_status_t status; 9563431Scarlsonj dhcp_lease_t *dlp; 9570Sstevel@tonic-gate 9583431Scarlsonj status.if_began = monosec_to_time(dsmp->dsm_curstart_monosec); 9590Sstevel@tonic-gate 9603431Scarlsonj /* 9613431Scarlsonj * We return information on just the first lease as being 9623431Scarlsonj * representative of the lot. A better status mechanism is 9633431Scarlsonj * needed. 9643431Scarlsonj */ 9653431Scarlsonj dlp = dsmp->dsm_leases; 9663431Scarlsonj 9673431Scarlsonj if (dlp == NULL || 9683431Scarlsonj dlp->dl_lifs->lif_expire.dt_start == DHCP_PERM) { 9690Sstevel@tonic-gate status.if_t1 = DHCP_PERM; 9700Sstevel@tonic-gate status.if_t2 = DHCP_PERM; 9710Sstevel@tonic-gate status.if_lease = DHCP_PERM; 9720Sstevel@tonic-gate } else { 9733431Scarlsonj status.if_t1 = status.if_began + 9743431Scarlsonj dlp->dl_t1.dt_start; 9753431Scarlsonj status.if_t2 = status.if_began + 9763431Scarlsonj dlp->dl_t2.dt_start; 9773431Scarlsonj status.if_lease = status.if_began + 9783431Scarlsonj dlp->dl_lifs->lif_expire.dt_start; 9790Sstevel@tonic-gate } 9800Sstevel@tonic-gate 9810Sstevel@tonic-gate status.version = DHCP_STATUS_VER; 9823431Scarlsonj status.if_state = dsmp->dsm_state; 9833431Scarlsonj status.if_dflags = dsmp->dsm_dflags; 9843431Scarlsonj status.if_sent = dsmp->dsm_sent; 9853431Scarlsonj status.if_recv = dsmp->dsm_received; 9863431Scarlsonj status.if_bad_offers = dsmp->dsm_bad_offers; 9870Sstevel@tonic-gate 9883431Scarlsonj (void) strlcpy(status.if_name, dsmp->dsm_name, IFNAMSIZ); 9893431Scarlsonj 9903431Scarlsonj send_data_reply(iap, 0, DHCP_TYPE_STATUS, &status, 9910Sstevel@tonic-gate sizeof (dhcp_status_t)); 9923431Scarlsonj break; 9930Sstevel@tonic-gate } 9940Sstevel@tonic-gate } 9950Sstevel@tonic-gate } 9962546Scarlsonj 9972546Scarlsonj /* 9982546Scarlsonj * check_rtm_addr(): determine if routing socket message matches interface 9992546Scarlsonj * address 10002546Scarlsonj * 10013431Scarlsonj * input: const struct if_msghdr *: pointer to routing socket message 10023431Scarlsonj * int: routing socket message length 10033431Scarlsonj * boolean_t: set to B_TRUE if IPv6 10043431Scarlsonj * const in6_addr_t *: pointer to IP address 10053431Scarlsonj * output: boolean_t: B_TRUE if address is a match 10062546Scarlsonj */ 10073431Scarlsonj 10082546Scarlsonj static boolean_t 10093431Scarlsonj check_rtm_addr(const struct ifa_msghdr *ifam, int msglen, boolean_t isv6, 10103431Scarlsonj const in6_addr_t *addr) 10112546Scarlsonj { 10123431Scarlsonj const char *cp, *lim; 10132546Scarlsonj uint_t flag; 10143431Scarlsonj const struct sockaddr *sa; 10152546Scarlsonj 10162546Scarlsonj if (!(ifam->ifam_addrs & RTA_IFA)) 10172546Scarlsonj return (B_FALSE); 10182546Scarlsonj 10193431Scarlsonj cp = (const char *)(ifam + 1); 10203431Scarlsonj lim = (const char *)ifam + msglen; 10212546Scarlsonj for (flag = 1; flag < RTA_IFA; flag <<= 1) { 10222546Scarlsonj if (ifam->ifam_addrs & flag) { 10232546Scarlsonj /* LINTED: alignment */ 10243431Scarlsonj sa = (const struct sockaddr *)cp; 10253431Scarlsonj if ((const char *)(sa + 1) > lim) 10262546Scarlsonj return (B_FALSE); 10272546Scarlsonj switch (sa->sa_family) { 10282546Scarlsonj case AF_INET: 10292546Scarlsonj cp += sizeof (struct sockaddr_in); 10302546Scarlsonj break; 10312546Scarlsonj case AF_LINK: 10322546Scarlsonj cp += sizeof (struct sockaddr_dl); 10332546Scarlsonj break; 10342546Scarlsonj case AF_INET6: 10352546Scarlsonj cp += sizeof (struct sockaddr_in6); 10362546Scarlsonj break; 10372546Scarlsonj default: 10382546Scarlsonj cp += sizeof (struct sockaddr); 10392546Scarlsonj break; 10402546Scarlsonj } 10412546Scarlsonj } 10422546Scarlsonj } 10433431Scarlsonj if (isv6) { 10443431Scarlsonj const struct sockaddr_in6 *sin6; 10453431Scarlsonj 10463431Scarlsonj /* LINTED: alignment */ 10473431Scarlsonj sin6 = (const struct sockaddr_in6 *)cp; 10483431Scarlsonj if ((const char *)(sin6 + 1) > lim) 10493431Scarlsonj return (B_FALSE); 10503431Scarlsonj if (sin6->sin6_family != AF_INET6) 10513431Scarlsonj return (B_FALSE); 10523431Scarlsonj return (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, addr)); 10533431Scarlsonj } else { 10543431Scarlsonj const struct sockaddr_in *sinp; 10553431Scarlsonj ipaddr_t v4addr; 10563431Scarlsonj 10573431Scarlsonj /* LINTED: alignment */ 10583431Scarlsonj sinp = (const struct sockaddr_in *)cp; 10593431Scarlsonj if ((const char *)(sinp + 1) > lim) 10603431Scarlsonj return (B_FALSE); 10613431Scarlsonj if (sinp->sin_family != AF_INET) 10623431Scarlsonj return (B_FALSE); 10633431Scarlsonj IN6_V4MAPPED_TO_IPADDR(addr, v4addr); 10643431Scarlsonj return (sinp->sin_addr.s_addr == v4addr); 10653431Scarlsonj } 10663431Scarlsonj } 10673431Scarlsonj 10683431Scarlsonj /* 10693431Scarlsonj * is_rtm_v6(): determine if routing socket message is IPv6 10703431Scarlsonj * 10713431Scarlsonj * input: struct ifa_msghdr *: pointer to routing socket message 10723431Scarlsonj * int: message length 10733431Scarlsonj * output: boolean_t 10743431Scarlsonj */ 10753431Scarlsonj 10763431Scarlsonj static boolean_t 10773431Scarlsonj is_rtm_v6(const struct ifa_msghdr *ifam, int msglen) 10783431Scarlsonj { 10793431Scarlsonj const char *cp, *lim; 10803431Scarlsonj uint_t flag; 10813431Scarlsonj const struct sockaddr *sa; 10823431Scarlsonj 10833431Scarlsonj cp = (const char *)(ifam + 1); 10843431Scarlsonj lim = (const char *)ifam + msglen; 10853431Scarlsonj for (flag = ifam->ifam_addrs; flag != 0; flag &= flag - 1) { 10863431Scarlsonj /* LINTED: alignment */ 10873431Scarlsonj sa = (const struct sockaddr *)cp; 10883431Scarlsonj if ((const char *)(sa + 1) > lim) 10893431Scarlsonj return (B_FALSE); 10903431Scarlsonj switch (sa->sa_family) { 10913431Scarlsonj case AF_INET: 10923431Scarlsonj return (B_FALSE); 10933431Scarlsonj case AF_LINK: 10943431Scarlsonj cp += sizeof (struct sockaddr_dl); 10953431Scarlsonj break; 10963431Scarlsonj case AF_INET6: 10973431Scarlsonj return (B_TRUE); 10983431Scarlsonj default: 10993431Scarlsonj cp += sizeof (struct sockaddr); 11003431Scarlsonj break; 11013431Scarlsonj } 11023431Scarlsonj } 11033431Scarlsonj return (B_FALSE); 11043431Scarlsonj } 11053431Scarlsonj 11063431Scarlsonj /* 11073431Scarlsonj * check_lif(): check the state of a given logical interface and its DHCP 11083431Scarlsonj * lease. We've been told by the routing socket that the 11093431Scarlsonj * corresponding ifIndex has changed. This may mean that DAD has 11103431Scarlsonj * completed or failed. 11113431Scarlsonj * 11123431Scarlsonj * input: dhcp_lif_t *: pointer to the LIF 11133431Scarlsonj * const struct ifa_msghdr *: routing socket message 11143431Scarlsonj * int: size of routing socket message 11153431Scarlsonj * output: boolean_t: B_TRUE if DAD has completed on this interface 11163431Scarlsonj */ 11173431Scarlsonj 11183431Scarlsonj static boolean_t 11193431Scarlsonj check_lif(dhcp_lif_t *lif, const struct ifa_msghdr *ifam, int msglen) 11203431Scarlsonj { 11213431Scarlsonj boolean_t isv6, dad_wait, unplumb; 11223431Scarlsonj int fd; 11233431Scarlsonj struct lifreq lifr; 11243431Scarlsonj 11253431Scarlsonj isv6 = lif->lif_pif->pif_isv6; 11263431Scarlsonj fd = isv6 ? v6_sock_fd : v4_sock_fd; 11273431Scarlsonj 11283431Scarlsonj /* 11293431Scarlsonj * Get the real (64 bit) logical interface flags. Note that the 11303431Scarlsonj * routing socket message has flags, but these are just the lower 32 11313431Scarlsonj * bits. 11323431Scarlsonj */ 11333431Scarlsonj unplumb = B_FALSE; 11343431Scarlsonj (void) memset(&lifr, 0, sizeof (lifr)); 11353431Scarlsonj (void) strlcpy(lifr.lifr_name, lif->lif_name, sizeof (lifr.lifr_name)); 11363431Scarlsonj if (ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) { 11373431Scarlsonj /* 11383431Scarlsonj * Failing to retrieve flags means that the interface is gone. 11393431Scarlsonj * It hasn't failed to verify with DAD, but we still have to 11403431Scarlsonj * give up on it. 11413431Scarlsonj */ 11423431Scarlsonj lifr.lifr_flags = 0; 11433431Scarlsonj if (errno == ENXIO) { 11443431Scarlsonj lif->lif_plumbed = B_FALSE; 11453431Scarlsonj dhcpmsg(MSG_INFO, "%s has been removed; abandoning", 11463431Scarlsonj lif->lif_name); 1147*4106Scarlsonj if (!isv6) 1148*4106Scarlsonj discard_default_routes(lif->lif_smachs); 11493431Scarlsonj } else { 11503431Scarlsonj dhcpmsg(MSG_ERR, 11513431Scarlsonj "unable to retrieve interface flags on %s", 11523431Scarlsonj lif->lif_name); 11533431Scarlsonj } 11543431Scarlsonj unplumb = B_TRUE; 11553431Scarlsonj } else if (!check_rtm_addr(ifam, msglen, isv6, &lif->lif_v6addr)) { 11563431Scarlsonj /* 11573431Scarlsonj * If the message is not about this logical interface, 11583431Scarlsonj * then just ignore it. 11593431Scarlsonj */ 11602546Scarlsonj return (B_FALSE); 11613431Scarlsonj } else if (lifr.lifr_flags & IFF_DUPLICATE) { 11623431Scarlsonj dhcpmsg(MSG_ERROR, "interface %s has duplicate address", 11633431Scarlsonj lif->lif_name); 11643431Scarlsonj lif_mark_decline(lif, "duplicate address"); 11653431Scarlsonj close_ip_lif(lif); 11663431Scarlsonj } 11673431Scarlsonj 11683431Scarlsonj dad_wait = lif->lif_dad_wait; 11693431Scarlsonj if (dad_wait) { 11703431Scarlsonj dhcpmsg(MSG_VERBOSE, "check_lif: %s has finished DAD", 11713431Scarlsonj lif->lif_name); 11723431Scarlsonj lif->lif_dad_wait = B_FALSE; 11733431Scarlsonj } 11743431Scarlsonj 11753431Scarlsonj if (unplumb) 11763431Scarlsonj unplumb_lif(lif); 11773431Scarlsonj 11783431Scarlsonj return (dad_wait); 11793431Scarlsonj } 11803431Scarlsonj 11813431Scarlsonj /* 11823431Scarlsonj * check_main_lif(): check the state of a main logical interface for a state 11833431Scarlsonj * machine. This is used only for DHCPv6. 11843431Scarlsonj * 11853431Scarlsonj * input: dhcp_smach_t *: pointer to the state machine 11863431Scarlsonj * const struct ifa_msghdr *: routing socket message 11873431Scarlsonj * int: size of routing socket message 11883431Scarlsonj * output: boolean_t: B_TRUE if LIF is ok. 11893431Scarlsonj */ 11903431Scarlsonj 11913431Scarlsonj static boolean_t 11923431Scarlsonj check_main_lif(dhcp_smach_t *dsmp, const struct ifa_msghdr *ifam, int msglen) 11933431Scarlsonj { 11943431Scarlsonj dhcp_lif_t *lif = dsmp->dsm_lif; 11953431Scarlsonj struct lifreq lifr; 11963431Scarlsonj 11973431Scarlsonj /* 11983431Scarlsonj * Get the real (64 bit) logical interface flags. Note that the 11993431Scarlsonj * routing socket message has flags, but these are just the lower 32 12003431Scarlsonj * bits. 12013431Scarlsonj */ 12023431Scarlsonj (void) memset(&lifr, 0, sizeof (lifr)); 12033431Scarlsonj (void) strlcpy(lifr.lifr_name, lif->lif_name, sizeof (lifr.lifr_name)); 12043431Scarlsonj if (ioctl(v6_sock_fd, SIOCGLIFFLAGS, &lifr) == -1) { 12053431Scarlsonj /* 12063431Scarlsonj * Failing to retrieve flags means that the interface is gone. 12073431Scarlsonj * Our state machine is now trash. 12083431Scarlsonj */ 12093431Scarlsonj if (errno == ENXIO) { 12103431Scarlsonj dhcpmsg(MSG_INFO, "%s has been removed; abandoning", 12113431Scarlsonj lif->lif_name); 12123431Scarlsonj } else { 12133431Scarlsonj dhcpmsg(MSG_ERR, 12143431Scarlsonj "unable to retrieve interface flags on %s", 12153431Scarlsonj lif->lif_name); 12163431Scarlsonj } 12173431Scarlsonj return (B_FALSE); 12183431Scarlsonj } else if (!check_rtm_addr(ifam, msglen, B_TRUE, &lif->lif_v6addr)) { 12193431Scarlsonj /* 12203431Scarlsonj * If the message is not about this logical interface, 12213431Scarlsonj * then just ignore it. 12223431Scarlsonj */ 12233431Scarlsonj return (B_TRUE); 12243431Scarlsonj } else if (lifr.lifr_flags & IFF_DUPLICATE) { 12253431Scarlsonj dhcpmsg(MSG_ERROR, "interface %s has duplicate address", 12263431Scarlsonj lif->lif_name); 12273431Scarlsonj return (B_FALSE); 12283431Scarlsonj } else { 12293431Scarlsonj return (B_TRUE); 12303431Scarlsonj } 12313431Scarlsonj } 12323431Scarlsonj 12333431Scarlsonj /* 12343431Scarlsonj * process_link_up_down(): check the state of a physical interface for up/down 12353431Scarlsonj * transitions; must go through INIT_REBOOT state if 12363431Scarlsonj * the link flaps. 12373431Scarlsonj * 12383431Scarlsonj * input: dhcp_pif_t *: pointer to the physical interface to check 12393431Scarlsonj * const struct if_msghdr *: routing socket message 12403431Scarlsonj * output: none 12413431Scarlsonj */ 12423431Scarlsonj 12433431Scarlsonj static void 12443431Scarlsonj process_link_up_down(dhcp_pif_t *pif, const struct if_msghdr *ifm) 12453431Scarlsonj { 12463431Scarlsonj struct lifreq lifr; 12473431Scarlsonj boolean_t isv6; 12483431Scarlsonj int fd; 12493431Scarlsonj 12503431Scarlsonj /* 12513431Scarlsonj * If the message implies no change of flags, then we're done; no need 12523431Scarlsonj * to check further. Note that if we have multiple state machines on a 12533431Scarlsonj * single physical interface, this test keeps us from issuing an ioctl 12543431Scarlsonj * for each one. 12553431Scarlsonj */ 12563431Scarlsonj if ((ifm->ifm_flags & IFF_RUNNING) && pif->pif_running || 12573431Scarlsonj !(ifm->ifm_flags & IFF_RUNNING) && !pif->pif_running) 12583431Scarlsonj return; 12593431Scarlsonj 12603431Scarlsonj /* 12613431Scarlsonj * We don't know what the real interface flags are, because the 12623431Scarlsonj * if_index number is only 16 bits; we must go ask. 12633431Scarlsonj */ 12643431Scarlsonj isv6 = pif->pif_isv6; 12653431Scarlsonj fd = isv6 ? v6_sock_fd : v4_sock_fd; 12663431Scarlsonj (void) memset(&lifr, 0, sizeof (lifr)); 12673431Scarlsonj (void) strlcpy(lifr.lifr_name, pif->pif_name, sizeof (lifr.lifr_name)); 12683431Scarlsonj 12693431Scarlsonj if (ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1 || 12703431Scarlsonj !(lifr.lifr_flags & IFF_RUNNING)) { 12713431Scarlsonj /* 12723431Scarlsonj * If we've lost the interface or it has gone down, then 12733431Scarlsonj * nothing special to do; just turn off the running flag. 12743431Scarlsonj */ 12753431Scarlsonj pif_status(pif, B_FALSE); 12763431Scarlsonj } else { 12773431Scarlsonj /* 12783431Scarlsonj * Interface has come back up: go through verification process. 12793431Scarlsonj */ 12803431Scarlsonj pif_status(pif, B_TRUE); 12813431Scarlsonj } 12822546Scarlsonj } 12832546Scarlsonj 12842546Scarlsonj /* 12852546Scarlsonj * rtsock_event(): fetches routing socket messages and updates internal 12862546Scarlsonj * interface state based on those messages. 12872546Scarlsonj * 12882546Scarlsonj * input: iu_eh_t *: unused 12892546Scarlsonj * int: the routing socket file descriptor 12902546Scarlsonj * (other arguments unused) 12912546Scarlsonj * output: void 12922546Scarlsonj */ 12932546Scarlsonj 12942546Scarlsonj /* ARGSUSED */ 12952546Scarlsonj static void 12962546Scarlsonj rtsock_event(iu_eh_t *ehp, int fd, short events, iu_event_id_t id, void *arg) 12972546Scarlsonj { 12983431Scarlsonj dhcp_smach_t *dsmp, *dsmnext; 12992546Scarlsonj union { 13002546Scarlsonj struct ifa_msghdr ifam; 13013431Scarlsonj struct if_msghdr ifm; 13022546Scarlsonj char buf[1024]; 13032546Scarlsonj } msg; 13042546Scarlsonj uint16_t ifindex; 13052546Scarlsonj int msglen; 13063431Scarlsonj boolean_t isv6; 13072546Scarlsonj 13082546Scarlsonj if ((msglen = read(fd, &msg, sizeof (msg))) <= 0) 13092546Scarlsonj return; 13102546Scarlsonj 13113431Scarlsonj /* Note that the routing socket interface index is just 16 bits */ 13123431Scarlsonj if (msg.ifm.ifm_type == RTM_IFINFO) { 13133431Scarlsonj ifindex = msg.ifm.ifm_index; 13143431Scarlsonj isv6 = (msg.ifm.ifm_flags & IFF_IPV6) ? B_TRUE : B_FALSE; 13153431Scarlsonj } else if (msg.ifam.ifam_type == RTM_DELADDR || 13163431Scarlsonj msg.ifam.ifam_type == RTM_NEWADDR) { 13173431Scarlsonj ifindex = msg.ifam.ifam_index; 13183431Scarlsonj isv6 = is_rtm_v6(&msg.ifam, msglen); 13193431Scarlsonj } else { 13202546Scarlsonj return; 13213431Scarlsonj } 13222546Scarlsonj 13233431Scarlsonj for (dsmp = lookup_smach_by_uindex(ifindex, NULL, isv6); 13243431Scarlsonj dsmp != NULL; dsmp = dsmnext) { 13253431Scarlsonj DHCPSTATE oldstate; 13263431Scarlsonj boolean_t lif_finished; 13273431Scarlsonj boolean_t lease_removed; 13283431Scarlsonj dhcp_lease_t *dlp, *dlnext; 13292546Scarlsonj 13302546Scarlsonj /* 13313431Scarlsonj * Note that script_start can call dhcp_drop directly, and 13323431Scarlsonj * that will do release_smach. 13332546Scarlsonj */ 13343431Scarlsonj dsmnext = lookup_smach_by_uindex(ifindex, dsmp, isv6); 13353431Scarlsonj oldstate = dsmp->dsm_state; 13363431Scarlsonj 13373431Scarlsonj /* 13383431Scarlsonj * Look for link up/down notifications. These occur on a 13393431Scarlsonj * physical interface basis. 13403431Scarlsonj */ 13413431Scarlsonj if (msg.ifm.ifm_type == RTM_IFINFO) { 13423431Scarlsonj process_link_up_down(dsmp->dsm_lif->lif_pif, &msg.ifm); 13432546Scarlsonj continue; 13443431Scarlsonj } 13452546Scarlsonj 13462546Scarlsonj /* 13472546Scarlsonj * Since we cannot trust the flags reported by the routing 13482546Scarlsonj * socket (they're just 32 bits -- and thus never include 13492546Scarlsonj * IFF_DUPLICATE), and we can't trust the ifindex (it's only 16 13502546Scarlsonj * bits and also doesn't reflect the alias in use), we get 13512546Scarlsonj * flags on all matching interfaces, and go by that. 13522546Scarlsonj */ 13533431Scarlsonj lif_finished = B_FALSE; 13543431Scarlsonj lease_removed = B_FALSE; 13553431Scarlsonj for (dlp = dsmp->dsm_leases; dlp != NULL; dlp = dlnext) { 13563431Scarlsonj dhcp_lif_t *lif, *lifnext; 13573431Scarlsonj uint_t nlifs = dlp->dl_nlifs; 13583431Scarlsonj 13593431Scarlsonj dlnext = dlp->dl_next; 13603431Scarlsonj for (lif = dlp->dl_lifs; lif != NULL && nlifs > 0; 13613431Scarlsonj lif = lifnext, nlifs--) { 13623431Scarlsonj lifnext = lif->lif_next; 13633431Scarlsonj if (check_lif(lif, &msg.ifam, msglen)) { 13643431Scarlsonj dsmp->dsm_lif_wait--; 13653431Scarlsonj lif_finished = B_TRUE; 13663431Scarlsonj } 13673431Scarlsonj } 13683431Scarlsonj if (dlp->dl_nlifs == 0) { 13693431Scarlsonj remove_lease(dlp); 13703431Scarlsonj lease_removed = B_TRUE; 13713431Scarlsonj } 13723431Scarlsonj } 13733431Scarlsonj 13743431Scarlsonj if ((isv6 && !check_main_lif(dsmp, &msg.ifam, msglen)) || 13753431Scarlsonj (!isv6 && !verify_lif(dsmp->dsm_lif))) { 13763431Scarlsonj if (dsmp->dsm_script_pid != -1) 13773431Scarlsonj script_stop(dsmp); 13783431Scarlsonj (void) script_start(dsmp, EVENT_DROP6, dhcp_drop, NULL, 13793431Scarlsonj NULL); 13802546Scarlsonj continue; 13813431Scarlsonj } 13823431Scarlsonj 13833431Scarlsonj /* 13843431Scarlsonj * Ignore this state machine if nothing interesting has 13853431Scarlsonj * happened. 13863431Scarlsonj */ 13873431Scarlsonj if (!lif_finished && dsmp->dsm_lif_down == 0 && 13883431Scarlsonj (dsmp->dsm_leases != NULL || !lease_removed)) 13893431Scarlsonj continue; 13903431Scarlsonj 13913431Scarlsonj /* 13923431Scarlsonj * If we're still waiting for DAD to complete on some of the 13933431Scarlsonj * configured LIFs, then don't send a response. 13943431Scarlsonj */ 13953431Scarlsonj if (dsmp->dsm_lif_wait != 0) { 13963431Scarlsonj dhcpmsg(MSG_VERBOSE, "rtsock_event: %s still has %d " 13973431Scarlsonj "LIFs waiting on DAD", dsmp->dsm_name, 13983431Scarlsonj dsmp->dsm_lif_wait); 13992546Scarlsonj continue; 14002546Scarlsonj } 14012546Scarlsonj 14023431Scarlsonj /* 14033431Scarlsonj * If we have some failed LIFs, then handle them now. We'll 14043431Scarlsonj * remove them from the list. Any leases that become empty are 14053431Scarlsonj * also removed as part of the decline-generation process. 14063431Scarlsonj */ 14073431Scarlsonj if (dsmp->dsm_lif_down != 0) { 14083431Scarlsonj /* 14093431Scarlsonj * We need to switch back to PRE_BOUND state so that 14103431Scarlsonj * send_pkt_internal() uses DLPI instead of sockets. 14113431Scarlsonj * Our logical interface has already been torn down by 14123431Scarlsonj * the kernel, and thus we can't send DHCPDECLINE by 14133431Scarlsonj * way of regular IP. (Unless we're adopting -- allow 14143431Scarlsonj * the grandparent to be handled as expected.) 14153431Scarlsonj */ 14163431Scarlsonj if (oldstate != ADOPTING) { 14173431Scarlsonj (void) set_smach_state(dsmp, PRE_BOUND); 14183431Scarlsonj } 14193431Scarlsonj send_declines(dsmp); 14202546Scarlsonj } 14212546Scarlsonj 14223431Scarlsonj if (dsmp->dsm_leases == NULL) { 14233431Scarlsonj dsmp->dsm_bad_offers++; 14243431Scarlsonj /* 14253431Scarlsonj * For DHCPv6, we'll process the restart once we're 14263431Scarlsonj * done sending Decline messages, because these are 14273431Scarlsonj * supposed to be acknowledged. With DHCPv4, there's 14283431Scarlsonj * no acknowledgment for a DECLINE, so after sending 14293431Scarlsonj * it, we just restart right away. 14303431Scarlsonj */ 14313431Scarlsonj if (!dsmp->dsm_isv6) { 14323431Scarlsonj dhcpmsg(MSG_VERBOSE, "rtsock_event: %s has no " 14333431Scarlsonj "LIFs left; restarting", dsmp->dsm_name); 14343431Scarlsonj dhcp_restart(dsmp); 14353431Scarlsonj } 14363431Scarlsonj } else { 14373431Scarlsonj /* 14383431Scarlsonj * If we're now up on at least some of the leases and 14393431Scarlsonj * we were waiting for that, then kick off the rest of 14403431Scarlsonj * configuration. Lease validation and DAD are done. 14413431Scarlsonj */ 14423431Scarlsonj dhcpmsg(MSG_VERBOSE, "rtsock_event: all LIFs verified " 14433431Scarlsonj "on %s in %s state", dsmp->dsm_name, 14443431Scarlsonj dhcp_state_to_string(oldstate)); 14453431Scarlsonj if (oldstate == PRE_BOUND || 14463431Scarlsonj oldstate == ADOPTING) 14473431Scarlsonj dhcp_bound_complete(dsmp); 14483431Scarlsonj if (oldstate == ADOPTING) 14493431Scarlsonj dhcp_adopt_complete(dsmp); 14503431Scarlsonj } 14512546Scarlsonj } 14522546Scarlsonj } 14533431Scarlsonj 14543431Scarlsonj /* 14553431Scarlsonj * check_cmd_allowed(): check whether the requested command is allowed in the 14563431Scarlsonj * state specified. 14573431Scarlsonj * 14583431Scarlsonj * input: DHCPSTATE: current state 14593431Scarlsonj * dhcp_ipc_type_t: requested command 14603431Scarlsonj * output: boolean_t: B_TRUE if command is allowed in this state 14613431Scarlsonj */ 14623431Scarlsonj 14633431Scarlsonj boolean_t 14643431Scarlsonj check_cmd_allowed(DHCPSTATE state, dhcp_ipc_type_t cmd) 14653431Scarlsonj { 14663431Scarlsonj return (ipc_cmd_allowed[state][cmd] != 0); 14673431Scarlsonj } 1468