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 52612Scarlsonj * Common Development and Distribution License (the "License"). 62612Scarlsonj * 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 * 253431Scarlsonj * ADOPTING state of the client state machine. This is used only during 263431Scarlsonj * diskless boot with IPv4. 270Sstevel@tonic-gate */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 300Sstevel@tonic-gate 310Sstevel@tonic-gate #include <sys/types.h> 320Sstevel@tonic-gate #include <string.h> 330Sstevel@tonic-gate #include <unistd.h> 340Sstevel@tonic-gate #include <stdlib.h> 352612Scarlsonj #include <signal.h> 360Sstevel@tonic-gate #include <sys/socket.h> 373431Scarlsonj #include <net/if_arp.h> 380Sstevel@tonic-gate #include <netinet/in.h> 390Sstevel@tonic-gate #include <sys/systeminfo.h> 400Sstevel@tonic-gate #include <netinet/inetutil.h> 410Sstevel@tonic-gate #include <netinet/dhcp.h> 420Sstevel@tonic-gate #include <dhcpmsg.h> 433431Scarlsonj #include <libdevinfo.h> 440Sstevel@tonic-gate 453431Scarlsonj #include "agent.h" 460Sstevel@tonic-gate #include "async.h" 470Sstevel@tonic-gate #include "util.h" 480Sstevel@tonic-gate #include "packet.h" 490Sstevel@tonic-gate #include "interface.h" 500Sstevel@tonic-gate #include "states.h" 510Sstevel@tonic-gate 520Sstevel@tonic-gate 530Sstevel@tonic-gate typedef struct { 540Sstevel@tonic-gate char dk_if_name[IFNAMSIZ]; 550Sstevel@tonic-gate char dk_ack[1]; 560Sstevel@tonic-gate } dhcp_kcache_t; 570Sstevel@tonic-gate 580Sstevel@tonic-gate static int get_dhcp_kcache(dhcp_kcache_t **, size_t *); 590Sstevel@tonic-gate 603431Scarlsonj static boolean_t get_prom_prop(const char *, const char *, uchar_t **, 613431Scarlsonj uint_t *); 623431Scarlsonj 630Sstevel@tonic-gate /* 640Sstevel@tonic-gate * dhcp_adopt(): adopts the interface managed by the kernel for diskless boot 650Sstevel@tonic-gate * 660Sstevel@tonic-gate * input: void 673431Scarlsonj * output: boolean_t: B_TRUE success, B_FALSE on failure 680Sstevel@tonic-gate */ 690Sstevel@tonic-gate 703431Scarlsonj boolean_t 710Sstevel@tonic-gate dhcp_adopt(void) 720Sstevel@tonic-gate { 730Sstevel@tonic-gate int retval; 740Sstevel@tonic-gate dhcp_kcache_t *kcache = NULL; 750Sstevel@tonic-gate size_t kcache_size; 760Sstevel@tonic-gate PKT_LIST *plp = NULL; 773431Scarlsonj dhcp_lif_t *lif; 783431Scarlsonj dhcp_smach_t *dsmp = NULL; 793431Scarlsonj uint_t client_id_len; 800Sstevel@tonic-gate 810Sstevel@tonic-gate retval = get_dhcp_kcache(&kcache, &kcache_size); 820Sstevel@tonic-gate if (retval == 0 || kcache_size < sizeof (dhcp_kcache_t)) { 830Sstevel@tonic-gate dhcpmsg(MSG_CRIT, "dhcp_adopt: cannot fetch kernel cache"); 840Sstevel@tonic-gate goto failure; 850Sstevel@tonic-gate } 860Sstevel@tonic-gate 870Sstevel@tonic-gate dhcpmsg(MSG_DEBUG, "dhcp_adopt: fetched %s kcache", kcache->dk_if_name); 880Sstevel@tonic-gate 890Sstevel@tonic-gate /* 900Sstevel@tonic-gate * convert the kernel's ACK into binary 910Sstevel@tonic-gate */ 920Sstevel@tonic-gate 933431Scarlsonj plp = alloc_pkt_entry(strlen(kcache->dk_ack) / 2, B_FALSE); 940Sstevel@tonic-gate if (plp == NULL) 950Sstevel@tonic-gate goto failure; 960Sstevel@tonic-gate 970Sstevel@tonic-gate dhcpmsg(MSG_DEBUG, "dhcp_adopt: allocated ACK of %d bytes", plp->len); 980Sstevel@tonic-gate 993431Scarlsonj if (hexascii_to_octet(kcache->dk_ack, plp->len * 2, plp->pkt, 1003431Scarlsonj &plp->len) != 0) { 1010Sstevel@tonic-gate dhcpmsg(MSG_CRIT, "dhcp_adopt: cannot convert kernel ACK"); 1020Sstevel@tonic-gate goto failure; 1030Sstevel@tonic-gate } 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate if (dhcp_options_scan(plp, B_TRUE) != 0) { 1060Sstevel@tonic-gate dhcpmsg(MSG_CRIT, "dhcp_adopt: cannot parse kernel ACK"); 1070Sstevel@tonic-gate goto failure; 1080Sstevel@tonic-gate } 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate /* 1110Sstevel@tonic-gate * make an interface to represent the "cached interface" in 1120Sstevel@tonic-gate * the kernel, hook up the ACK packet we made, and send out 1130Sstevel@tonic-gate * the extend request (to attempt to renew the lease). 1140Sstevel@tonic-gate * 1150Sstevel@tonic-gate * we do a send_extend() instead of doing a dhcp_init_reboot() 1160Sstevel@tonic-gate * because although dhcp_init_reboot() is more correct from a 1170Sstevel@tonic-gate * protocol perspective, it introduces a window where a 1180Sstevel@tonic-gate * diskless client has no IP address but may need to page in 1190Sstevel@tonic-gate * more of this program. we could mlockall(), but that's 1200Sstevel@tonic-gate * going to be a mess, especially with handling malloc() and 1210Sstevel@tonic-gate * stack growth, so it's easier to just renew(). the only 1220Sstevel@tonic-gate * catch here is that if we are not granted a renewal, we're 1230Sstevel@tonic-gate * totally hosed and can only bail out. 1240Sstevel@tonic-gate */ 1250Sstevel@tonic-gate 1263431Scarlsonj if ((lif = attach_lif(kcache->dk_if_name, B_FALSE, &retval)) == NULL) { 1273431Scarlsonj dhcpmsg(MSG_ERROR, "dhcp_adopt: unable to attach %s: %d", 1283431Scarlsonj kcache->dk_if_name, retval); 1293431Scarlsonj goto failure; 1303431Scarlsonj } 1313431Scarlsonj 1323431Scarlsonj if ((dsmp = insert_smach(lif, &retval)) == NULL) { 1333431Scarlsonj dhcpmsg(MSG_ERROR, "dhcp_adopt: unable to create state " 1343431Scarlsonj "machine for %s: %d", kcache->dk_if_name, retval); 1353431Scarlsonj goto failure; 1363431Scarlsonj } 1373431Scarlsonj 1383431Scarlsonj /* 1393431Scarlsonj * If the agent is adopting a lease, then OBP is initially 1403431Scarlsonj * searched for a client-id. 1413431Scarlsonj */ 1423431Scarlsonj 1433431Scarlsonj dhcpmsg(MSG_DEBUG, "dhcp_adopt: getting /chosen:clientid property"); 1443431Scarlsonj 1453431Scarlsonj client_id_len = 0; 1463431Scarlsonj if (!get_prom_prop("chosen", "client-id", &dsmp->dsm_cid, 1473431Scarlsonj &client_id_len)) { 1483431Scarlsonj /* 1493431Scarlsonj * a failure occurred trying to acquire the client-id 1503431Scarlsonj */ 1513431Scarlsonj 1523431Scarlsonj dhcpmsg(MSG_DEBUG, 1533431Scarlsonj "dhcp_adopt: cannot allocate client id for %s", 1543431Scarlsonj dsmp->dsm_name); 1553431Scarlsonj goto failure; 1563431Scarlsonj } else if (dsmp->dsm_hwtype == ARPHRD_IB && dsmp->dsm_cid == NULL) { 1573431Scarlsonj /* 1583431Scarlsonj * when the interface is infiniband and the agent 1593431Scarlsonj * is adopting the lease there must be an OBP 1603431Scarlsonj * client-id. 1613431Scarlsonj */ 1623431Scarlsonj 1633431Scarlsonj dhcpmsg(MSG_DEBUG, "dhcp_adopt: no /chosen:clientid id for %s", 1643431Scarlsonj dsmp->dsm_name); 1653431Scarlsonj goto failure; 1663431Scarlsonj } 1673431Scarlsonj 1683431Scarlsonj dsmp->dsm_cidlen = client_id_len; 1693431Scarlsonj 1703431Scarlsonj if (set_lif_dhcp(lif, B_TRUE) != DHCP_IPC_SUCCESS) 1710Sstevel@tonic-gate goto failure; 1720Sstevel@tonic-gate 1733431Scarlsonj if (!set_smach_state(dsmp, ADOPTING)) 1743431Scarlsonj goto failure; 1753431Scarlsonj dsmp->dsm_dflags = DHCP_IF_PRIMARY; 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate /* 1782612Scarlsonj * move to BOUND and use the information in our ACK packet. 1792612Scarlsonj * adoption will continue after DAD via dhcp_adopt_complete. 1800Sstevel@tonic-gate */ 1810Sstevel@tonic-gate 1823431Scarlsonj if (!dhcp_bound(dsmp, plp)) { 1830Sstevel@tonic-gate dhcpmsg(MSG_CRIT, "dhcp_adopt: cannot use cached packet"); 1840Sstevel@tonic-gate goto failure; 1850Sstevel@tonic-gate } 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate free(kcache); 1883431Scarlsonj return (B_TRUE); 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate failure: 1913431Scarlsonj /* Note: no need to free lif; dsmp holds reference */ 1923431Scarlsonj if (dsmp != NULL) 193*3522Scarlsonj remove_smach(dsmp); 1940Sstevel@tonic-gate free(kcache); 1953431Scarlsonj free_pkt_entry(plp); 1963431Scarlsonj return (B_FALSE); 1970Sstevel@tonic-gate } 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate /* 2002612Scarlsonj * dhcp_adopt_complete(): completes interface adoption process after kernel 2012612Scarlsonj * duplicate address detection (DAD) is done. 2022612Scarlsonj * 2033431Scarlsonj * input: dhcp_smach_t *: the state machine on which a lease is being adopted 2042612Scarlsonj * output: none 2052612Scarlsonj */ 2062612Scarlsonj 2072612Scarlsonj void 2083431Scarlsonj dhcp_adopt_complete(dhcp_smach_t *dsmp) 2092612Scarlsonj { 2102612Scarlsonj dhcpmsg(MSG_DEBUG, "dhcp_adopt_complete: completing adoption"); 2112612Scarlsonj 2123431Scarlsonj if (async_start(dsmp, DHCP_EXTEND, B_FALSE) == 0) { 2132612Scarlsonj dhcpmsg(MSG_CRIT, "dhcp_adopt_complete: async_start failed"); 2142612Scarlsonj return; 2152612Scarlsonj } 2162612Scarlsonj 2173431Scarlsonj if (dhcp_extending(dsmp) == 0) { 2182612Scarlsonj dhcpmsg(MSG_CRIT, 2192612Scarlsonj "dhcp_adopt_complete: cannot send renew request"); 2202612Scarlsonj return; 2212612Scarlsonj } 2222612Scarlsonj 2232612Scarlsonj if (grandparent != (pid_t)0) { 2243431Scarlsonj dhcpmsg(MSG_DEBUG, "adoption complete, signalling parent (%ld)" 2252612Scarlsonj " to exit.", grandparent); 2262612Scarlsonj (void) kill(grandparent, SIGALRM); 2272612Scarlsonj } 2282612Scarlsonj } 2292612Scarlsonj 2302612Scarlsonj /* 2310Sstevel@tonic-gate * get_dhcp_kcache(): fetches the DHCP ACK and interface name from the kernel 2320Sstevel@tonic-gate * 2330Sstevel@tonic-gate * input: dhcp_kcache_t **: a dynamically-allocated cache packet 2340Sstevel@tonic-gate * size_t *: the length of that packet (on return) 2350Sstevel@tonic-gate * output: int: nonzero on success, zero on failure 2360Sstevel@tonic-gate */ 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate static int 2390Sstevel@tonic-gate get_dhcp_kcache(dhcp_kcache_t **kernel_cachep, size_t *kcache_size) 2400Sstevel@tonic-gate { 2410Sstevel@tonic-gate char dummy; 2420Sstevel@tonic-gate long size; 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate size = sysinfo(SI_DHCP_CACHE, &dummy, sizeof (dummy)); 2450Sstevel@tonic-gate if (size == -1) 2460Sstevel@tonic-gate return (0); 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate *kcache_size = size; 2490Sstevel@tonic-gate *kernel_cachep = malloc(*kcache_size); 2500Sstevel@tonic-gate if (*kernel_cachep == NULL) 2510Sstevel@tonic-gate return (0); 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate (void) sysinfo(SI_DHCP_CACHE, (caddr_t)*kernel_cachep, size); 2540Sstevel@tonic-gate return (1); 2550Sstevel@tonic-gate } 2563431Scarlsonj 2573431Scarlsonj /* 2583431Scarlsonj * get_prom_prop(): get the value of the named property on the named node in 2593431Scarlsonj * devinfo root. 2603431Scarlsonj * 2613431Scarlsonj * input: const char *: The name of the node containing the property. 2623431Scarlsonj * const char *: The name of the property. 2633431Scarlsonj * uchar_t **: The property value, modified iff B_TRUE is returned. 2643431Scarlsonj * If no value is found the value is set to NULL. 2653431Scarlsonj * uint_t *: The length of the property value 2663431Scarlsonj * output: boolean_t: Returns B_TRUE if successful (no problems), 2673431Scarlsonj * otherwise B_FALSE. 2683431Scarlsonj * note: The memory allocated by this function must be freed by 2693431Scarlsonj * the caller. This code is derived from 2703431Scarlsonj * usr/src/lib/libwanboot/common/bootinfo_aux.c. 2713431Scarlsonj */ 2723431Scarlsonj 2733431Scarlsonj static boolean_t 2743431Scarlsonj get_prom_prop(const char *nodename, const char *propname, uchar_t **propvaluep, 2753431Scarlsonj uint_t *lenp) 2763431Scarlsonj { 2773431Scarlsonj di_node_t root_node; 2783431Scarlsonj di_node_t node; 2793431Scarlsonj di_prom_handle_t phdl = DI_PROM_HANDLE_NIL; 2803431Scarlsonj di_prom_prop_t pp; 2813431Scarlsonj uchar_t *value = NULL; 2823431Scarlsonj unsigned int len = 0; 2833431Scarlsonj boolean_t success = B_TRUE; 2843431Scarlsonj 2853431Scarlsonj /* 2863431Scarlsonj * locate root node 2873431Scarlsonj */ 2883431Scarlsonj 2893431Scarlsonj if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL || 2903431Scarlsonj (phdl = di_prom_init()) == DI_PROM_HANDLE_NIL) { 2913431Scarlsonj dhcpmsg(MSG_DEBUG, "get_prom_prop: property root node " 2923431Scarlsonj "not found"); 2933431Scarlsonj goto get_prom_prop_cleanup; 2943431Scarlsonj } 2953431Scarlsonj 2963431Scarlsonj /* 2973431Scarlsonj * locate nodename within '/' 2983431Scarlsonj */ 2993431Scarlsonj 3003431Scarlsonj for (node = di_child_node(root_node); 3013431Scarlsonj node != DI_NODE_NIL; 3023431Scarlsonj node = di_sibling_node(node)) { 3033431Scarlsonj if (strcmp(di_node_name(node), nodename) == 0) { 3043431Scarlsonj break; 3053431Scarlsonj } 3063431Scarlsonj } 3073431Scarlsonj 3083431Scarlsonj if (node == DI_NODE_NIL) { 3093431Scarlsonj dhcpmsg(MSG_DEBUG, "get_prom_prop: node not found"); 3103431Scarlsonj goto get_prom_prop_cleanup; 3113431Scarlsonj } 3123431Scarlsonj 3133431Scarlsonj /* 3143431Scarlsonj * scan all properties of /nodename for the 'propname' property 3153431Scarlsonj */ 3163431Scarlsonj 3173431Scarlsonj for (pp = di_prom_prop_next(phdl, node, DI_PROM_PROP_NIL); 3183431Scarlsonj pp != DI_PROM_PROP_NIL; 3193431Scarlsonj pp = di_prom_prop_next(phdl, node, pp)) { 3203431Scarlsonj 3213431Scarlsonj dhcpmsg(MSG_DEBUG, "get_prom_prop: property = %s", 3223431Scarlsonj di_prom_prop_name(pp)); 3233431Scarlsonj 3243431Scarlsonj if (strcmp(propname, di_prom_prop_name(pp)) == 0) { 3253431Scarlsonj break; 3263431Scarlsonj } 3273431Scarlsonj } 3283431Scarlsonj 3293431Scarlsonj if (pp == DI_PROM_PROP_NIL) { 3303431Scarlsonj dhcpmsg(MSG_DEBUG, "get_prom_prop: property not found"); 3313431Scarlsonj goto get_prom_prop_cleanup; 3323431Scarlsonj } 3333431Scarlsonj 3343431Scarlsonj /* 3353431Scarlsonj * get the property; allocate some memory copy it out 3363431Scarlsonj */ 3373431Scarlsonj 3383431Scarlsonj len = di_prom_prop_data(pp, (uchar_t **)&value); 3393431Scarlsonj 3403431Scarlsonj if (value == NULL) { 3413431Scarlsonj /* 3423431Scarlsonj * property data read problems 3433431Scarlsonj */ 3443431Scarlsonj 3453431Scarlsonj success = B_FALSE; 3463431Scarlsonj dhcpmsg(MSG_ERR, "get_prom_prop: cannot read property data"); 3473431Scarlsonj goto get_prom_prop_cleanup; 3483431Scarlsonj } 3493431Scarlsonj 3503431Scarlsonj if (propvaluep != NULL) { 3513431Scarlsonj /* 3523431Scarlsonj * allocate somewhere to copy the property value to 3533431Scarlsonj */ 3543431Scarlsonj 3553431Scarlsonj *propvaluep = calloc(len, sizeof (uchar_t)); 3563431Scarlsonj 3573431Scarlsonj if (*propvaluep == NULL) { 3583431Scarlsonj /* 3593431Scarlsonj * allocation problems 3603431Scarlsonj */ 3613431Scarlsonj 3623431Scarlsonj success = B_FALSE; 3633431Scarlsonj dhcpmsg(MSG_ERR, "get_prom_prop: cannot allocate " 3643431Scarlsonj "memory for property value"); 3653431Scarlsonj goto get_prom_prop_cleanup; 3663431Scarlsonj } 3673431Scarlsonj 3683431Scarlsonj /* 3693431Scarlsonj * copy data out 3703431Scarlsonj */ 3713431Scarlsonj 3723431Scarlsonj (void) memcpy(*propvaluep, value, len); 3733431Scarlsonj 3743431Scarlsonj /* 3753431Scarlsonj * copy out the length if a suitable pointer has 3763431Scarlsonj * been supplied 3773431Scarlsonj */ 3783431Scarlsonj 3793431Scarlsonj if (lenp != NULL) { 3803431Scarlsonj *lenp = len; 3813431Scarlsonj } 3823431Scarlsonj 3833431Scarlsonj dhcpmsg(MSG_DEBUG, "get_prom_prop: property value " 3843431Scarlsonj "length = %d", len); 3853431Scarlsonj } 3863431Scarlsonj 3873431Scarlsonj get_prom_prop_cleanup: 3883431Scarlsonj 3893431Scarlsonj if (phdl != DI_PROM_HANDLE_NIL) { 3903431Scarlsonj di_prom_fini(phdl); 3913431Scarlsonj } 3923431Scarlsonj 3933431Scarlsonj if (root_node != DI_NODE_NIL) { 3943431Scarlsonj di_fini(root_node); 3953431Scarlsonj } 3963431Scarlsonj 3973431Scarlsonj return (success); 3983431Scarlsonj } 399