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