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 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 * 25 * BOUND state of the DHCP client state machine. 26 */ 27 28 #pragma ident "%Z%%M% %I% %E% SMI" 29 30 #include <sys/socket.h> 31 #include <sys/types.h> 32 #include <string.h> 33 #include <netinet/in.h> 34 #include <sys/sockio.h> 35 #include <unistd.h> 36 #include <time.h> 37 #include <arpa/inet.h> 38 #include <stdlib.h> 39 #include <search.h> 40 #include <sys/sysmacros.h> 41 #include <dhcp_hostconf.h> 42 #include <dhcpagent_util.h> 43 #include <dhcpmsg.h> 44 45 #include "states.h" 46 #include "packet.h" 47 #include "util.h" 48 #include "agent.h" 49 #include "interface.h" 50 #include "script_handler.h" 51 52 /* 53 * Possible outcomes for IPv6 binding attempt. 54 */ 55 enum v6_bind_result { 56 v6Restart, /* report failure and restart state machine */ 57 v6Resent, /* new Request message has been sent */ 58 v6Done /* successful binding */ 59 }; 60 61 static enum v6_bind_result configure_v6_leases(dhcp_smach_t *); 62 static boolean_t configure_v4_lease(dhcp_smach_t *); 63 static boolean_t configure_v4_timers(dhcp_smach_t *); 64 65 /* 66 * bound_event_cb(): callback for script_start on the event EVENT_BOUND 67 * 68 * input: dhcp_smach_t *: the state machine configured 69 * void *: unused 70 * output: int: always 1 71 */ 72 73 /* ARGSUSED1 */ 74 static int 75 bound_event_cb(dhcp_smach_t *dsmp, void *arg) 76 { 77 if (dsmp->dsm_ia.ia_fd != -1) 78 ipc_action_finish(dsmp, DHCP_IPC_SUCCESS); 79 else 80 async_finish(dsmp); 81 return (1); 82 } 83 84 /* 85 * dhcp_bound(): configures an state machine and interfaces using information 86 * contained in the ACK/Reply packet and sets up lease timers. 87 * Before starting, the requested address is verified by 88 * Duplicate Address Detection to make sure it's not in use. 89 * 90 * input: dhcp_smach_t *: the state machine to move to bound 91 * PKT_LIST *: the ACK/Reply packet, or NULL to use dsmp->dsm_ack 92 * output: boolean_t: B_TRUE on success, B_FALSE on failure 93 */ 94 95 boolean_t 96 dhcp_bound(dhcp_smach_t *dsmp, PKT_LIST *ack) 97 { 98 DHCPSTATE oldstate; 99 lease_t new_lease; 100 dhcp_lif_t *lif; 101 dhcp_lease_t *dlp; 102 enum v6_bind_result v6b; 103 104 if (ack != NULL) { 105 /* If ack we're replacing is not the original, then free it */ 106 if (dsmp->dsm_ack != dsmp->dsm_orig_ack) 107 free_pkt_entry(dsmp->dsm_ack); 108 dsmp->dsm_ack = ack; 109 /* Save the first ack as the original */ 110 if (dsmp->dsm_orig_ack == NULL) 111 dsmp->dsm_orig_ack = ack; 112 } 113 114 oldstate = dsmp->dsm_state; 115 switch (oldstate) { 116 117 case ADOPTING: 118 /* Note that adoption occurs only for IPv4 DHCP. */ 119 120 /* Ignore BOOTP */ 121 if (ack->opts[CD_DHCP_TYPE] == NULL) 122 return (B_FALSE); 123 124 /* 125 * if we're adopting a lease, the lease timers 126 * only provide an upper bound since we don't know 127 * from what time they are relative to. assume we 128 * have a lease time of at most DHCP_ADOPT_LEASE_MAX. 129 */ 130 (void) memcpy(&new_lease, ack->opts[CD_LEASE_TIME]->value, 131 sizeof (lease_t)); 132 133 new_lease = htonl(MIN(ntohl(new_lease), DHCP_ADOPT_LEASE_MAX)); 134 135 (void) memcpy(ack->opts[CD_LEASE_TIME]->value, &new_lease, 136 sizeof (lease_t)); 137 138 /* 139 * we have no idea when the REQUEST that generated 140 * this ACK was sent, but for diagnostic purposes 141 * we'll assume its close to the current time. 142 */ 143 dsmp->dsm_newstart_monosec = monosec(); 144 145 if (dsmp->dsm_isv6) { 146 if ((v6b = configure_v6_leases(dsmp)) != v6Done) 147 return (v6b == v6Resent); 148 } else { 149 if (!configure_v4_lease(dsmp)) 150 return (B_FALSE); 151 152 if (!configure_v4_timers(dsmp)) 153 return (B_FALSE); 154 } 155 156 dsmp->dsm_curstart_monosec = dsmp->dsm_newstart_monosec; 157 break; 158 159 case SELECTING: 160 case REQUESTING: 161 case INIT_REBOOT: 162 163 if (dsmp->dsm_isv6) { 164 if ((v6b = configure_v6_leases(dsmp)) != v6Done) 165 return (v6b == v6Resent); 166 } else { 167 if (!configure_v4_lease(dsmp)) 168 return (B_FALSE); 169 170 if (!configure_v4_timers(dsmp)) 171 return (B_FALSE); 172 173 if (!clear_lif_deprecated(dsmp->dsm_lif)) 174 return (B_FALSE); 175 } 176 177 /* Stop sending requests now */ 178 stop_pkt_retransmission(dsmp); 179 180 /* 181 * If we didn't end up with any usable leases, then we have a 182 * problem. 183 */ 184 if (dsmp->dsm_leases == NULL) { 185 dhcpmsg(MSG_WARNING, 186 "dhcp_bound: no address lease established"); 187 return (B_FALSE); 188 } 189 190 /* 191 * If this is a Rapid-Commit (selecting state) or if we're 192 * dealing with a reboot (init-reboot), then we will have a new 193 * server ID to save. 194 */ 195 if (ack != NULL && 196 (oldstate == SELECTING || oldstate == INIT_REBOOT) && 197 dsmp->dsm_isv6 && !save_server_id(dsmp, ack)) { 198 dhcpmsg(MSG_ERROR, 199 "dhcp_bound: unable to save server ID on %s", 200 dsmp->dsm_name); 201 return (B_FALSE); 202 } 203 204 /* 205 * We will continue configuring the interfaces via 206 * dhcp_bound_complete, once kernel DAD completes. If no new 207 * leases were created (which can happen on an init-reboot used 208 * for link-up confirmation), then go straight to bound state. 209 */ 210 if (!set_smach_state(dsmp, PRE_BOUND)) 211 return (B_FALSE); 212 if (dsmp->dsm_lif_wait == 0) 213 dhcp_bound_complete(dsmp); 214 break; 215 216 case PRE_BOUND: 217 case BOUND: 218 /* This is just a duplicate ack; silently ignore it */ 219 return (B_TRUE); 220 221 case RENEWING: 222 case REBINDING: 223 224 if (dsmp->dsm_isv6) { 225 if ((v6b = configure_v6_leases(dsmp)) != v6Done) 226 return (v6b == v6Resent); 227 } else { 228 if (!configure_v4_timers(dsmp)) 229 return (B_FALSE); 230 if (!clear_lif_deprecated(dsmp->dsm_lif)) 231 return (B_FALSE); 232 } 233 234 /* 235 * If some or all of the leases were torn down by the server, 236 * then handle that as an expiry. When the script is done 237 * running for the LOSS6 event, we'll end up back here. 238 */ 239 if ((lif = find_expired_lif(dsmp)) != NULL) { 240 hold_lif(lif); 241 dhcp_expire(NULL, lif); 242 while ((lif = find_expired_lif(dsmp)) != NULL) { 243 dlp = lif->lif_lease; 244 unplumb_lif(lif); 245 if (dlp->dl_nlifs == 0) 246 remove_lease(dlp); 247 } 248 if (dsmp->dsm_leases == NULL) 249 return (B_FALSE); 250 } 251 252 if (oldstate == REBINDING && dsmp->dsm_isv6 && 253 !save_server_id(dsmp, ack)) { 254 return (B_FALSE); 255 } 256 257 /* 258 * Handle Renew/Rebind that fails to address one of our leases. 259 * (Should just never happen, but RFC 3315 section 18.1.8 260 * requires it, and TAHI tests for it.) 261 */ 262 for (dlp = dsmp->dsm_leases; dlp != NULL; dlp = dlp->dl_next) { 263 if (dlp->dl_stale && dlp->dl_nlifs > 0) 264 break; 265 } 266 if (dlp != NULL) { 267 dhcpmsg(MSG_DEBUG, "dhcp_bound: lease not updated; " 268 "allow retransmit"); 269 return (B_TRUE); 270 } 271 272 if (!set_smach_state(dsmp, BOUND)) 273 return (B_FALSE); 274 275 (void) script_start(dsmp, dsmp->dsm_isv6 ? EVENT_EXTEND6 : 276 EVENT_EXTEND, bound_event_cb, NULL, NULL); 277 278 dsmp->dsm_curstart_monosec = dsmp->dsm_newstart_monosec; 279 280 /* Stop sending requests now */ 281 stop_pkt_retransmission(dsmp); 282 break; 283 284 case INFORM_SENT: 285 286 if (dsmp->dsm_isv6 && !save_server_id(dsmp, ack)) { 287 return (B_FALSE); 288 } 289 290 (void) bound_event_cb(dsmp, NULL); 291 if (!set_smach_state(dsmp, INFORMATION)) 292 return (B_FALSE); 293 294 /* Stop sending requests now */ 295 stop_pkt_retransmission(dsmp); 296 break; 297 298 default: 299 /* something is really bizarre... */ 300 dhcpmsg(MSG_DEBUG, 301 "dhcp_bound: called in unexpected state: %s", 302 dhcp_state_to_string(dsmp->dsm_state)); 303 return (B_FALSE); 304 } 305 306 /* 307 * remove any stale hostconf file that might be lying around for 308 * this state machine. (in general, it's harmless, since we'll write a 309 * fresh one when we exit anyway, but just to reduce confusion..) 310 */ 311 312 (void) remove_hostconf(dsmp->dsm_name, dsmp->dsm_isv6); 313 return (B_TRUE); 314 } 315 316 /* 317 * dhcp_bound_complete(): complete interface configuration after DAD 318 * 319 * input: dhcp_smach_t *: the state machine now ready 320 * output: none 321 */ 322 323 void 324 dhcp_bound_complete(dhcp_smach_t *dsmp) 325 { 326 PKT_LIST *ack; 327 DHCP_OPT *router_list; 328 int i; 329 DHCPSTATE oldstate; 330 dhcp_lif_t *lif; 331 332 /* 333 * Do bound state entry processing only if running IPv4. There's no 334 * need for this with DHCPv6 because link-locals are used for I/O and 335 * because DHCPv6 isn't entangled with routing. 336 */ 337 if (dsmp->dsm_isv6) { 338 (void) set_smach_state(dsmp, BOUND); 339 dhcpmsg(MSG_DEBUG, "configure_bound: bound %s", 340 dsmp->dsm_name); 341 (void) script_start(dsmp, EVENT_BOUND6, bound_event_cb, NULL, 342 NULL); 343 dsmp->dsm_curstart_monosec = dsmp->dsm_newstart_monosec; 344 return; 345 } 346 347 /* 348 * Add each provided router; we'll clean them up when the 349 * state machine goes away or when our lease expires. 350 * 351 * Note that we do not handle default routers on IPv4 logicals; 352 * see README for details. 353 */ 354 355 ack = dsmp->dsm_ack; 356 router_list = ack->opts[CD_ROUTER]; 357 lif = dsmp->dsm_lif; 358 if (router_list != NULL && 359 (router_list->len % sizeof (ipaddr_t)) == 0 && 360 strchr(lif->lif_name, ':') == NULL) { 361 362 dsmp->dsm_nrouters = router_list->len / sizeof (ipaddr_t); 363 dsmp->dsm_routers = malloc(router_list->len); 364 if (dsmp->dsm_routers == NULL) { 365 dhcpmsg(MSG_ERR, "configure_bound: cannot allocate " 366 "default router list, ignoring default routers"); 367 dsmp->dsm_nrouters = 0; 368 } 369 370 for (i = 0; i < dsmp->dsm_nrouters; i++) { 371 372 (void) memcpy(&dsmp->dsm_routers[i].s_addr, 373 router_list->value + (i * sizeof (ipaddr_t)), 374 sizeof (ipaddr_t)); 375 376 if (!add_default_route(lif->lif_pif->pif_index, 377 &dsmp->dsm_routers[i])) { 378 dhcpmsg(MSG_ERR, "configure_bound: cannot add " 379 "default router %s on %s", inet_ntoa( 380 dsmp->dsm_routers[i]), dsmp->dsm_name); 381 dsmp->dsm_routers[i].s_addr = htonl(INADDR_ANY); 382 continue; 383 } 384 385 dhcpmsg(MSG_INFO, "added default router %s on %s", 386 inet_ntoa(dsmp->dsm_routers[i]), dsmp->dsm_name); 387 } 388 } 389 390 oldstate = dsmp->dsm_state; 391 if (!set_smach_state(dsmp, BOUND)) { 392 dhcpmsg(MSG_ERR, 393 "configure_bound: cannot set bound state on %s", 394 dsmp->dsm_name); 395 return; 396 } 397 398 dhcpmsg(MSG_DEBUG, "configure_bound: bound %s", dsmp->dsm_name); 399 400 /* 401 * We're now committed to this binding, so if it came from BOOTP, set 402 * the flag. 403 */ 404 405 if (ack->opts[CD_DHCP_TYPE] == NULL) 406 dsmp->dsm_dflags |= DHCP_IF_BOOTP; 407 408 /* 409 * If the previous state was ADOPTING, event loop has not been started 410 * at this time; so don't run the EVENT_BOUND script. 411 */ 412 if (oldstate != ADOPTING) { 413 (void) script_start(dsmp, EVENT_BOUND, bound_event_cb, NULL, 414 NULL); 415 } 416 417 dsmp->dsm_curstart_monosec = dsmp->dsm_newstart_monosec; 418 } 419 420 /* 421 * fuzzify(): adds some "fuzz" to a t1/t2 time, in accordance with RFC2131. 422 * We use up to plus or minus 2% jitter in the time. This is a 423 * small value, but the timers involved are typically long. A 424 * common T1 value is one day, and the fuzz is up to 28.8 minutes; 425 * plenty of time to make sure that individual clients don't renew 426 * all at the same time. 427 * 428 * input: uint32_t: the number of seconds until lease expiration 429 * double: the approximate percentage of that time to return 430 * output: double: a number approximating (sec * pct) 431 */ 432 433 static double 434 fuzzify(uint32_t sec, double pct) 435 { 436 return (sec * (pct + (drand48() - 0.5) / 25.0)); 437 } 438 439 /* 440 * get_pkt_times(): pulls the lease times out of a v4 DHCP packet and stores 441 * them as host byte-order relative times in the passed in 442 * parameters. 443 * 444 * input: PKT_LIST *: the packet to pull the packet times from 445 * lease_t *: where to store the relative lease time in hbo 446 * lease_t *: where to store the relative t1 time in hbo 447 * lease_t *: where to store the relative t2 time in hbo 448 * output: void 449 */ 450 451 static void 452 get_pkt_times(PKT_LIST *ack, lease_t *lease, lease_t *t1, lease_t *t2) 453 { 454 *lease = DHCP_PERM; 455 *t1 = DHCP_PERM; 456 *t2 = DHCP_PERM; 457 458 if (ack->opts[CD_DHCP_TYPE] == NULL) { 459 dhcpmsg(MSG_VERBOSE, 460 "get_pkt_times: BOOTP response; infinite lease"); 461 return; 462 } 463 if (ack->opts[CD_LEASE_TIME] == NULL) { 464 dhcpmsg(MSG_VERBOSE, 465 "get_pkt_times: no lease option provided"); 466 return; 467 } 468 if (ack->opts[CD_LEASE_TIME]->len != sizeof (lease_t)) { 469 dhcpmsg(MSG_VERBOSE, "get_pkt_times: invalid lease option"); 470 } 471 472 (void) memcpy(lease, ack->opts[CD_LEASE_TIME]->value, sizeof (lease_t)); 473 *lease = ntohl(*lease); 474 475 if (*lease == DHCP_PERM) { 476 dhcpmsg(MSG_VERBOSE, "get_pkt_times: infinite lease granted"); 477 return; 478 } 479 480 if (ack->opts[CD_T1_TIME] != NULL && 481 ack->opts[CD_T1_TIME]->len == sizeof (lease_t)) { 482 (void) memcpy(t1, ack->opts[CD_T1_TIME]->value, sizeof (*t1)); 483 *t1 = ntohl(*t1); 484 } 485 486 if (ack->opts[CD_T2_TIME] != NULL && 487 ack->opts[CD_T2_TIME]->len == sizeof (lease_t)) { 488 (void) memcpy(t2, ack->opts[CD_T2_TIME]->value, sizeof (*t2)); 489 *t2 = ntohl(*t2); 490 } 491 492 if ((*t1 == DHCP_PERM) || (*t1 >= *lease)) 493 *t1 = (lease_t)fuzzify(*lease, DHCP_T1_FACT); 494 495 if ((*t2 == DHCP_PERM) || (*t2 > *lease) || (*t2 <= *t1)) 496 *t2 = (lease_t)fuzzify(*lease, DHCP_T2_FACT); 497 498 dhcpmsg(MSG_VERBOSE, "get_pkt_times: lease %u t1 %u t2 %u", 499 *lease, *t1, *t2); 500 } 501 502 /* 503 * configure_v4_timers(): configures the lease timers on a v4 state machine 504 * 505 * input: dhcp_smach_t *: the state machine to configure 506 * output: boolean_t: B_TRUE on success, B_FALSE on failure 507 */ 508 509 static boolean_t 510 configure_v4_timers(dhcp_smach_t *dsmp) 511 { 512 PKT_LIST *ack = dsmp->dsm_ack; 513 lease_t lease, t1, t2; 514 dhcp_lease_t *dlp; 515 dhcp_lif_t *lif; 516 517 /* v4 has just one lease per state machine, and one LIF */ 518 dlp = dsmp->dsm_leases; 519 lif = dlp->dl_lifs; 520 521 /* 522 * If it's DHCP, but there's no valid lease time, then complain, 523 * decline the lease and return error. 524 */ 525 if (ack->opts[CD_DHCP_TYPE] != NULL && 526 (ack->opts[CD_LEASE_TIME] == NULL || 527 ack->opts[CD_LEASE_TIME]->len != sizeof (lease_t))) { 528 lif_mark_decline(lif, "Missing or corrupted lease time"); 529 send_declines(dsmp); 530 dhcpmsg(MSG_WARNING, "configure_v4_timers: %s lease time in " 531 "ACK on %s", ack->opts[CD_LEASE_TIME] == NULL ? "missing" : 532 "corrupt", dsmp->dsm_name); 533 return (B_FALSE); 534 } 535 536 /* Stop the T1 and T2 timers */ 537 cancel_lease_timers(dlp); 538 539 /* Stop the LEASE timer */ 540 cancel_lif_timers(lif); 541 542 /* 543 * type has already been verified as ACK. if type is not set, 544 * then we got a BOOTP packet. we now fetch the t1, t2, and 545 * lease options out of the packet into variables. they are 546 * returned as relative host-byte-ordered times. 547 */ 548 549 get_pkt_times(ack, &lease, &t1, &t2); 550 551 /* 552 * if the current lease is mysteriously close to the new 553 * lease, warn the user. unless there's less than a minute 554 * left, round to the closest minute. 555 */ 556 557 if (lif->lif_expire.dt_start != 0 && 558 abs((dsmp->dsm_newstart_monosec + lease) - 559 (dsmp->dsm_curstart_monosec + lif->lif_expire.dt_start)) < 560 DHCP_LEASE_EPS) { 561 const char *noext = "configure_v4_timers: lease renewed but " 562 "time not extended"; 563 int msg_level; 564 uint_t minleft; 565 566 if (lif->lif_expire.dt_start < DHCP_LEASE_ERROR_THRESH) 567 msg_level = MSG_ERROR; 568 else 569 msg_level = MSG_VERBOSE; 570 571 minleft = (lif->lif_expire.dt_start + 30) / 60; 572 573 if (lif->lif_expire.dt_start < 60) { 574 dhcpmsg(msg_level, "%s; expires in %d seconds", 575 noext, lif->lif_expire.dt_start); 576 } else if (minleft == 1) { 577 dhcpmsg(msg_level, "%s; expires in 1 minute", noext); 578 } else if (minleft > 120) { 579 dhcpmsg(msg_level, "%s; expires in %d hours", 580 noext, (minleft + 30) / 60); 581 } else { 582 dhcpmsg(msg_level, "%s; expires in %d minutes", 583 noext, minleft); 584 } 585 } 586 587 init_timer(&dlp->dl_t1, t1); 588 init_timer(&dlp->dl_t2, t2); 589 init_timer(&lif->lif_expire, lease); 590 591 if (lease == DHCP_PERM) { 592 dhcpmsg(MSG_INFO, 593 "configure_v4_timers: %s acquired permanent lease", 594 dsmp->dsm_name); 595 return (B_TRUE); 596 } 597 598 dhcpmsg(MSG_INFO, "configure_v4_timers: %s acquired lease, expires %s", 599 dsmp->dsm_name, 600 monosec_to_string(dsmp->dsm_newstart_monosec + lease)); 601 602 dhcpmsg(MSG_INFO, "configure_v4_timers: %s begins renewal at %s", 603 dsmp->dsm_name, monosec_to_string(dsmp->dsm_newstart_monosec + 604 dlp->dl_t1.dt_start)); 605 606 dhcpmsg(MSG_INFO, "configure_v4_timers: %s begins rebinding at %s", 607 dsmp->dsm_name, monosec_to_string(dsmp->dsm_newstart_monosec + 608 dlp->dl_t2.dt_start)); 609 610 /* 611 * according to RFC2131, there is no minimum lease time, but don't 612 * set up renew/rebind timers if lease is shorter than DHCP_REBIND_MIN. 613 */ 614 615 if (!schedule_lif_timer(lif, &lif->lif_expire, dhcp_expire)) 616 goto failure; 617 618 if (lease < DHCP_REBIND_MIN) { 619 dhcpmsg(MSG_WARNING, "configure_v4_timers: lease on %s is for " 620 "less than %d seconds!", dsmp->dsm_name, DHCP_REBIND_MIN); 621 return (B_TRUE); 622 } 623 624 if (!schedule_lease_timer(dlp, &dlp->dl_t1, dhcp_renew)) 625 goto failure; 626 627 if (!schedule_lease_timer(dlp, &dlp->dl_t2, dhcp_rebind)) 628 goto failure; 629 630 return (B_TRUE); 631 632 failure: 633 cancel_lease_timers(dlp); 634 cancel_lif_timers(lif); 635 dhcpmsg(MSG_WARNING, 636 "configure_v4_timers: cannot schedule lease timers"); 637 return (B_FALSE); 638 } 639 640 /* 641 * configure_v6_leases(): configures the IPv6 leases on a state machine from 642 * the current DHCPv6 ACK. We need to scan the ACK, 643 * create a lease for each IA_NA, and a new LIF for each 644 * IAADDR. 645 * 646 * input: dhcp_smach_t *: the machine to configure (with a valid dsm_ack) 647 * output: enum v6_bind_result: restart, resend, or done 648 */ 649 650 static enum v6_bind_result 651 configure_v6_leases(dhcp_smach_t *dsmp) 652 { 653 const dhcpv6_option_t *d6o, *d6so, *d6sso; 654 const char *optbase, *estr, *msg; 655 uint_t olen, solen, ssolen, msglen; 656 dhcpv6_ia_na_t d6in; 657 dhcpv6_iaaddr_t d6ia; 658 dhcp_lease_t *dlp; 659 uint32_t shortest; 660 dhcp_lif_t *lif; 661 uint_t nlifs; 662 boolean_t got_iana = B_FALSE; 663 uint_t scode; 664 665 for (dlp = dsmp->dsm_leases; dlp != NULL; dlp = dlp->dl_next) 666 dlp->dl_stale = B_TRUE; 667 668 d6o = NULL; 669 while ((d6o = dhcpv6_pkt_option(dsmp->dsm_ack, d6o, DHCPV6_OPT_IA_NA, 670 &olen)) != NULL) { 671 if (olen < sizeof (d6in)) { 672 dhcpmsg(MSG_WARNING, 673 "configure_v6_leases: garbled IA_NA"); 674 continue; 675 } 676 677 /* 678 * Check the IAID. It should be for our controlling LIF. If a 679 * single state machine needs to use multiple IAIDs, then this 680 * will need to change. 681 */ 682 (void) memcpy(&d6in, d6o, sizeof (d6in)); 683 d6in.d6in_iaid = ntohl(d6in.d6in_iaid); 684 if (d6in.d6in_iaid != dsmp->dsm_lif->lif_iaid) { 685 dhcpmsg(MSG_WARNING, "configure_v6_leases: ignored " 686 "IA_NA for IAID %x (not %x)", d6in.d6in_iaid, 687 dsmp->dsm_lif->lif_iaid); 688 continue; 689 } 690 691 /* 692 * See notes below; there's only one IA_NA and a single IAID 693 * for now. 694 */ 695 if ((dlp = dsmp->dsm_leases) != NULL) 696 dlp->dl_stale = B_FALSE; 697 698 /* 699 * Note that some bug-ridden servers will try to give us 700 * multiple IA_NA options for a single IAID. We ignore 701 * duplicates. 702 */ 703 if (got_iana) { 704 dhcpmsg(MSG_WARNING, "configure_v6_leases: unexpected " 705 "extra IA_NA ignored"); 706 continue; 707 } 708 709 d6in.d6in_t1 = ntohl(d6in.d6in_t1); 710 d6in.d6in_t2 = ntohl(d6in.d6in_t2); 711 712 /* RFC 3315 required check for invalid T1/T2 combinations */ 713 if (d6in.d6in_t1 > d6in.d6in_t2 && d6in.d6in_t2 != 0) { 714 dhcpmsg(MSG_WARNING, "configure_v6_leases: ignored " 715 "IA_NA with invalid T1 %u > T2 %u", d6in.d6in_t1, 716 d6in.d6in_t2); 717 continue; 718 } 719 720 /* 721 * There may be a status code here. Process if present. 722 */ 723 optbase = (const char *)d6o + sizeof (d6in); 724 olen -= sizeof (d6in); 725 d6so = dhcpv6_find_option(optbase, olen, NULL, 726 DHCPV6_OPT_STATUS_CODE, &solen); 727 scode = dhcpv6_status_code(d6so, solen, &estr, &msg, &msglen); 728 if (scode != DHCPV6_STAT_SUCCESS) { 729 dhcpmsg(MSG_WARNING, 730 "configure_v6_leases: IA_NA: %s: %.*s", 731 estr, msglen, msg); 732 } 733 print_server_msg(dsmp, msg, msglen); 734 735 /* 736 * Other errors are possible here. According to RFC 3315 737 * section 18.1.8, we ignore the entire IA if it gives the "no 738 * addresses" status code. We may try another server if we 739 * like -- we instead opt to allow the addresses to expire and 740 * then try a new server. 741 * 742 * If the status code is "no binding," then we must go back and 743 * redo the Request. Surprisingly, it doesn't matter if it's 744 * any other code. 745 */ 746 if (scode == DHCPV6_STAT_NOADDRS) { 747 dhcpmsg(MSG_DEBUG, "configure_v6_leases: ignoring " 748 "no-addrs status in IA_NA"); 749 continue; 750 } 751 752 if (scode == DHCPV6_STAT_NOBINDING) { 753 send_v6_request(dsmp); 754 return (v6Resent); 755 } 756 757 /* 758 * Find or create the lease structure. This part is simple, 759 * because we support only IA_NA and a single IAID. This means 760 * there's only one lease structure. The design supports 761 * multiple lease structures so that IA_TA and IA_PD can be 762 * added later. 763 */ 764 if ((dlp = dsmp->dsm_leases) == NULL && 765 (dlp = insert_lease(dsmp)) == NULL) { 766 dhcpmsg(MSG_ERROR, "configure_v6_leases: unable to " 767 "allocate memory for lease"); 768 return (v6Restart); 769 } 770 771 /* 772 * Iterate over the IAADDR options contained within this IA_NA. 773 */ 774 shortest = DHCPV6_INFTIME; 775 d6so = NULL; 776 while ((d6so = dhcpv6_find_option(optbase, olen, d6so, 777 DHCPV6_OPT_IAADDR, &solen)) != NULL) { 778 if (solen < sizeof (d6ia)) { 779 dhcpmsg(MSG_WARNING, 780 "configure_v6_leases: garbled IAADDR"); 781 continue; 782 } 783 (void) memcpy(&d6ia, d6so, sizeof (d6ia)); 784 785 d6ia.d6ia_preflife = ntohl(d6ia.d6ia_preflife); 786 d6ia.d6ia_vallife = ntohl(d6ia.d6ia_vallife); 787 788 /* RFC 3315 required validity check */ 789 if (d6ia.d6ia_preflife > d6ia.d6ia_vallife) { 790 dhcpmsg(MSG_WARNING, 791 "configure_v6_leases: ignored IAADDR with " 792 "preferred lifetime %u > valid %u", 793 d6ia.d6ia_preflife, d6ia.d6ia_vallife); 794 continue; 795 } 796 797 /* 798 * RFC 3315 allows a status code to be buried inside 799 * the IAADDR option. Look for it, and process if 800 * present. Process in a manner similar to that for 801 * the IA itself; TAHI checks for this. Real servers 802 * likely won't do this. 803 */ 804 d6sso = dhcpv6_find_option((const char *)d6so + 805 sizeof (d6ia), solen - sizeof (d6ia), NULL, 806 DHCPV6_OPT_STATUS_CODE, &ssolen); 807 scode = dhcpv6_status_code(d6sso, ssolen, &estr, &msg, 808 &msglen); 809 print_server_msg(dsmp, msg, msglen); 810 if (scode == DHCPV6_STAT_NOADDRS) { 811 dhcpmsg(MSG_DEBUG, "configure_v6_leases: " 812 "ignoring no-addrs status in IAADDR"); 813 continue; 814 } 815 if (scode == DHCPV6_STAT_NOBINDING) { 816 send_v6_request(dsmp); 817 return (v6Resent); 818 } 819 if (scode != DHCPV6_STAT_SUCCESS) { 820 dhcpmsg(MSG_WARNING, 821 "configure_v6_leases: IAADDR: %s", estr); 822 } 823 824 /* 825 * Locate the existing LIF within the lease associated 826 * with this address, if any. 827 */ 828 lif = dlp->dl_lifs; 829 for (nlifs = dlp->dl_nlifs; nlifs > 0; 830 nlifs--, lif = lif->lif_next) { 831 if (IN6_ARE_ADDR_EQUAL(&d6ia.d6ia_addr, 832 &lif->lif_v6addr)) 833 break; 834 } 835 836 /* 837 * If the server has set the lifetime to zero, then 838 * delete the LIF. Otherwise, set the new LIF expiry 839 * time, adding the LIF if necessary. 840 */ 841 if (d6ia.d6ia_vallife == 0) { 842 /* If it was found, then it's expired */ 843 if (nlifs != 0) { 844 dhcpmsg(MSG_DEBUG, 845 "configure_v6_leases: lif %s has " 846 "expired", lif->lif_name); 847 lif->lif_expired = B_TRUE; 848 } 849 continue; 850 } 851 852 /* If it wasn't found, then create it now. */ 853 if (nlifs == 0) { 854 lif = plumb_lif(dsmp->dsm_lif->lif_pif, 855 &d6ia.d6ia_addr); 856 if (lif == NULL) 857 continue; 858 if (++dlp->dl_nlifs == 1) { 859 dlp->dl_lifs = lif; 860 } else { 861 remque(lif); 862 insque(lif, dlp->dl_lifs); 863 } 864 lif->lif_lease = dlp; 865 lif->lif_dad_wait = _B_TRUE; 866 dsmp->dsm_lif_wait++; 867 } else { 868 /* If it was found, cancel timer */ 869 cancel_lif_timers(lif); 870 if (d6ia.d6ia_preflife != 0 && 871 !clear_lif_deprecated(lif)) { 872 unplumb_lif(lif); 873 continue; 874 } 875 } 876 877 /* Set the new expiry timers */ 878 init_timer(&lif->lif_preferred, d6ia.d6ia_preflife); 879 init_timer(&lif->lif_expire, d6ia.d6ia_vallife); 880 881 /* 882 * If the preferred lifetime is over now, then the LIF 883 * is deprecated. If it's the same as the expiry time, 884 * then we don't need a separate timer for it. 885 */ 886 if (d6ia.d6ia_preflife == 0) { 887 set_lif_deprecated(lif); 888 } else if (d6ia.d6ia_preflife != DHCPV6_INFTIME && 889 d6ia.d6ia_preflife != d6ia.d6ia_vallife && 890 !schedule_lif_timer(lif, &lif->lif_preferred, 891 dhcp_deprecate)) { 892 unplumb_lif(lif); 893 continue; 894 } 895 896 if (d6ia.d6ia_vallife != DHCPV6_INFTIME && 897 !schedule_lif_timer(lif, &lif->lif_expire, 898 dhcp_expire)) { 899 unplumb_lif(lif); 900 continue; 901 } 902 903 if (d6ia.d6ia_preflife < shortest) 904 shortest = d6ia.d6ia_preflife; 905 } 906 907 if (dlp->dl_nlifs == 0) { 908 dhcpmsg(MSG_WARNING, 909 "configure_v6_leases: no IAADDRs found in IA_NA"); 910 remove_lease(dlp); 911 continue; 912 } 913 914 if (d6in.d6in_t1 == 0 && d6in.d6in_t2 == 0) { 915 /* Default values from RFC 3315: 0.5 and 0.8 */ 916 if ((d6in.d6in_t1 = shortest / 2) == 0) 917 d6in.d6in_t1 = 1; 918 d6in.d6in_t2 = shortest - shortest / 5; 919 } 920 921 cancel_lease_timers(dlp); 922 init_timer(&dlp->dl_t1, d6in.d6in_t1); 923 init_timer(&dlp->dl_t2, d6in.d6in_t2); 924 925 if ((d6in.d6in_t1 != DHCPV6_INFTIME && 926 !schedule_lease_timer(dlp, &dlp->dl_t1, dhcp_renew)) || 927 (d6in.d6in_t2 != DHCPV6_INFTIME && 928 !schedule_lease_timer(dlp, &dlp->dl_t2, dhcp_rebind))) { 929 dhcpmsg(MSG_WARNING, "configure_v6_leases: unable to " 930 "set renew/rebind timers"); 931 } else { 932 got_iana = B_TRUE; 933 } 934 } 935 936 if (!got_iana) { 937 dhcpmsg(MSG_WARNING, 938 "configure_v6_leases: no usable IA_NA option found"); 939 } 940 941 return (v6Done); 942 } 943 944 /* 945 * configure_v4_lease(): configures the IPv4 lease on a state machine from 946 * the current DHCP ACK. There's only one lease and LIF 947 * per state machine in IPv4. 948 * 949 * input: dhcp_smach_t *: the machine to configure (with a valid dsm_ack) 950 * output: boolean_t: B_TRUE on success, B_FALSE on failure 951 */ 952 953 static boolean_t 954 configure_v4_lease(dhcp_smach_t *dsmp) 955 { 956 struct lifreq lifr; 957 struct sockaddr_in *sin; 958 PKT_LIST *ack = dsmp->dsm_ack; 959 dhcp_lease_t *dlp; 960 dhcp_lif_t *lif; 961 uint32_t addrhbo; 962 struct in_addr inaddr; 963 964 /* 965 * if we're using DHCP, then we'll have a valid CD_SERVER_ID 966 * (we checked in dhcp_acknak()); set it now so that 967 * dsmp->dsm_server is valid in case we need to send_decline(). 968 * note that we use comparisons against opts[CD_DHCP_TYPE] 969 * since we haven't set DHCP_IF_BOOTP yet (we don't do that 970 * until we're sure we want the offered address.) 971 */ 972 973 if (ack->opts[CD_DHCP_TYPE] != NULL) { 974 (void) memcpy(&inaddr, ack->opts[CD_SERVER_ID]->value, 975 sizeof (inaddr)); 976 IN6_INADDR_TO_V4MAPPED(&inaddr, &dsmp->dsm_server); 977 } 978 979 /* 980 * There needs to be exactly one lease for IPv4, and that lease 981 * controls the main LIF for the state machine. If it doesn't exist 982 * yet, then create it now. 983 */ 984 if ((dlp = dsmp->dsm_leases) == NULL && 985 (dlp = insert_lease(dsmp)) == NULL) { 986 dhcpmsg(MSG_ERROR, "configure_v4_lease: unable to allocate " 987 "memory for lease"); 988 return (B_FALSE); 989 } 990 if (dlp->dl_nlifs == 0) { 991 dlp->dl_lifs = dsmp->dsm_lif; 992 dlp->dl_nlifs = 1; 993 994 /* The lease holds a reference on the LIF */ 995 hold_lif(dlp->dl_lifs); 996 dlp->dl_lifs->lif_lease = dlp; 997 } 998 999 lif = dlp->dl_lifs; 1000 1001 IN6_INADDR_TO_V4MAPPED(&ack->pkt->yiaddr, &lif->lif_v6addr); 1002 addrhbo = ntohl(ack->pkt->yiaddr.s_addr); 1003 if ((addrhbo & IN_CLASSA_NET) == 0 || 1004 (addrhbo >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 1005 IN_CLASSD(addrhbo)) { 1006 dhcpmsg(MSG_ERROR, 1007 "configure_v4_lease: got invalid IP address %s for %s", 1008 inet_ntoa(ack->pkt->yiaddr), lif->lif_name); 1009 return (B_FALSE); 1010 } 1011 1012 (void) memset(&lifr, 0, sizeof (struct lifreq)); 1013 (void) strlcpy(lifr.lifr_name, lif->lif_name, LIFNAMSIZ); 1014 1015 /* 1016 * bring the interface online. note that there is no optimal 1017 * order here: it is considered bad taste (and in > solaris 7, 1018 * likely illegal) to bring an interface up before it has an 1019 * ip address. however, due to an apparent bug in sun fddi 1020 * 5.0, fddi will not obtain a network routing entry unless 1021 * the interface is brought up before it has an ip address. 1022 * we take the lesser of the two evils; if fddi customers have 1023 * problems, they can get a newer fddi distribution which 1024 * fixes the problem. 1025 */ 1026 1027 sin = (struct sockaddr_in *)&lifr.lifr_addr; 1028 sin->sin_family = AF_INET; 1029 1030 (void) memset(&lif->lif_v6mask, 0xff, sizeof (lif->lif_v6mask)); 1031 if (ack->opts[CD_SUBNETMASK] != NULL && 1032 ack->opts[CD_SUBNETMASK]->len == sizeof (inaddr)) { 1033 1034 (void) memcpy(&inaddr, ack->opts[CD_SUBNETMASK]->value, 1035 sizeof (inaddr)); 1036 1037 } else { 1038 1039 if (ack->opts[CD_SUBNETMASK] != NULL && 1040 ack->opts[CD_SUBNETMASK]->len != sizeof (inaddr)) { 1041 dhcpmsg(MSG_WARNING, "configure_v4_lease: specified " 1042 "subnet mask length is %d instead of %d, ignoring", 1043 ack->opts[CD_SUBNETMASK]->len, sizeof (ipaddr_t)); 1044 } else { 1045 dhcpmsg(MSG_WARNING, "configure_v4_lease: no IP " 1046 "netmask specified for %s, making best guess", 1047 lif->lif_name); 1048 } 1049 1050 /* 1051 * no legitimate IP subnet mask specified.. use best 1052 * guess. recall that lif_addr is in network order, so 1053 * imagine it's 0x11223344: then when it is read into 1054 * a register on x86, it becomes 0x44332211, so we 1055 * must ntohl() it to convert it to 0x11223344 in 1056 * order to use the macros in <netinet/in.h>. 1057 */ 1058 1059 if (IN_CLASSA(addrhbo)) 1060 inaddr.s_addr = htonl(IN_CLASSA_NET); 1061 else if (IN_CLASSB(addrhbo)) 1062 inaddr.s_addr = htonl(IN_CLASSB_NET); 1063 else /* must be class c */ 1064 inaddr.s_addr = htonl(IN_CLASSC_NET); 1065 } 1066 lif->lif_v6mask._S6_un._S6_u32[3] = inaddr.s_addr; 1067 1068 sin->sin_addr = inaddr; 1069 dhcpmsg(MSG_INFO, "configure_v4_lease: setting IP netmask to %s on %s", 1070 inet_ntoa(sin->sin_addr), lif->lif_name); 1071 1072 if (ioctl(v4_sock_fd, SIOCSLIFNETMASK, &lifr) == -1) { 1073 dhcpmsg(MSG_ERR, "configure_v4_lease: cannot set IP netmask " 1074 "on %s", lif->lif_name); 1075 return (B_FALSE); 1076 } 1077 1078 IN6_V4MAPPED_TO_INADDR(&lif->lif_v6addr, &sin->sin_addr); 1079 dhcpmsg(MSG_INFO, "configure_v4_lease: setting IP address to %s on %s", 1080 inet_ntoa(sin->sin_addr), lif->lif_name); 1081 1082 if (ioctl(v4_sock_fd, SIOCSLIFADDR, &lifr) == -1) { 1083 dhcpmsg(MSG_ERR, "configure_v4_lease: cannot set IP address " 1084 "on %s", lif->lif_name); 1085 return (B_FALSE); 1086 } 1087 1088 if (ioctl(v4_sock_fd, SIOCGLIFFLAGS, &lifr) == -1) { 1089 dhcpmsg(MSG_ERR, "configure_v4_lease: cannot get interface " 1090 "flags for %s", lif->lif_name); 1091 return (B_FALSE); 1092 } 1093 1094 lifr.lifr_flags |= IFF_UP; 1095 if (ioctl(v4_sock_fd, SIOCSLIFFLAGS, &lifr) == -1) { 1096 dhcpmsg(MSG_ERR, "configure_v4_lease: cannot set interface " 1097 "flags for %s", lif->lif_name); 1098 return (B_FALSE); 1099 } 1100 lif->lif_flags = lifr.lifr_flags; 1101 1102 lif->lif_dad_wait = B_TRUE; 1103 dsmp->dsm_lif_wait++; 1104 1105 if (ack->opts[CD_BROADCASTADDR] != NULL && 1106 ack->opts[CD_BROADCASTADDR]->len == sizeof (inaddr)) { 1107 1108 (void) memcpy(&inaddr, ack->opts[CD_BROADCASTADDR]->value, 1109 sizeof (inaddr)); 1110 1111 } else { 1112 1113 if (ack->opts[CD_BROADCASTADDR] != NULL && 1114 ack->opts[CD_BROADCASTADDR]->len != sizeof (inaddr)) { 1115 dhcpmsg(MSG_WARNING, "configure_v4_lease: specified " 1116 "broadcast address length is %d instead of %d, " 1117 "ignoring", ack->opts[CD_BROADCASTADDR]->len, 1118 sizeof (inaddr)); 1119 } else { 1120 dhcpmsg(MSG_WARNING, "configure_v4_lease: no IP " 1121 "broadcast specified for %s, making best guess", 1122 lif->lif_name); 1123 } 1124 1125 /* 1126 * no legitimate IP broadcast specified. compute it 1127 * from the IP address and netmask. 1128 */ 1129 1130 IN6_V4MAPPED_TO_INADDR(&lif->lif_v6addr, &inaddr); 1131 inaddr.s_addr |= ~lif->lif_v6mask._S6_un._S6_u32[3]; 1132 } 1133 1134 /* 1135 * the kernel will set the broadcast address for us as part of 1136 * bringing the interface up. since experience has shown that dhcp 1137 * servers sometimes provide a bogus broadcast address, we let the 1138 * kernel set it so that it's guaranteed to be correct. 1139 * 1140 * also, note any inconsistencies and save the broadcast address the 1141 * kernel set so that we can watch for changes to it. 1142 */ 1143 1144 if (ioctl(v4_sock_fd, SIOCGLIFBRDADDR, &lifr) == -1) { 1145 dhcpmsg(MSG_ERR, "configure_v4_lease: cannot get broadcast " 1146 "address for %s", lif->lif_name); 1147 return (B_FALSE); 1148 } 1149 1150 if (inaddr.s_addr != sin->sin_addr.s_addr) { 1151 dhcpmsg(MSG_WARNING, "configure_v4_lease: incorrect broadcast " 1152 "address %s specified for %s; ignoring", inet_ntoa(inaddr), 1153 lif->lif_name); 1154 } 1155 1156 lif->lif_broadcast = sin->sin_addr.s_addr; 1157 dhcpmsg(MSG_INFO, 1158 "configure_v4_lease: using broadcast address %s on %s", 1159 inet_ntoa(inaddr), lif->lif_name); 1160 return (B_TRUE); 1161 } 1162 1163 /* 1164 * save_server_id(): save off the new DHCPv6 Server ID 1165 * 1166 * input: dhcp_smach_t *: the state machine to use 1167 * PKT_LIST *: the packet with the Reply message 1168 * output: boolean_t: B_TRUE on success, B_FALSE on failure 1169 */ 1170 1171 boolean_t 1172 save_server_id(dhcp_smach_t *dsmp, PKT_LIST *msg) 1173 { 1174 const dhcpv6_option_t *d6o; 1175 uint_t olen; 1176 1177 d6o = dhcpv6_pkt_option(msg, NULL, DHCPV6_OPT_SERVERID, &olen); 1178 if (d6o == NULL) 1179 return (B_FALSE); 1180 olen -= sizeof (*d6o); 1181 free(dsmp->dsm_serverid); 1182 if ((dsmp->dsm_serverid = malloc(olen)) == NULL) { 1183 return (B_FALSE); 1184 } else { 1185 dsmp->dsm_serveridlen = olen; 1186 (void) memcpy(dsmp->dsm_serverid, d6o + 1, olen); 1187 return (B_TRUE); 1188 } 1189 } 1190