1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate * 26*0Sstevel@tonic-gate * ADOPTING state of the client state machine. 27*0Sstevel@tonic-gate */ 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate #include <sys/types.h> 32*0Sstevel@tonic-gate #include <string.h> 33*0Sstevel@tonic-gate #include <unistd.h> 34*0Sstevel@tonic-gate #include <stdlib.h> 35*0Sstevel@tonic-gate #include <sys/sockio.h> 36*0Sstevel@tonic-gate #include <sys/socket.h> 37*0Sstevel@tonic-gate #include <netinet/in.h> 38*0Sstevel@tonic-gate #include <sys/systeminfo.h> 39*0Sstevel@tonic-gate #include <netinet/inetutil.h> 40*0Sstevel@tonic-gate #include <netinet/dhcp.h> 41*0Sstevel@tonic-gate #include <dhcpmsg.h> 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gate #include "async.h" 44*0Sstevel@tonic-gate #include "util.h" 45*0Sstevel@tonic-gate #include "packet.h" 46*0Sstevel@tonic-gate #include "interface.h" 47*0Sstevel@tonic-gate #include "states.h" 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate typedef struct { 51*0Sstevel@tonic-gate char dk_if_name[IFNAMSIZ]; 52*0Sstevel@tonic-gate char dk_ack[1]; 53*0Sstevel@tonic-gate } dhcp_kcache_t; 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gate static int get_dhcp_kcache(dhcp_kcache_t **, size_t *); 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate /* 58*0Sstevel@tonic-gate * dhcp_adopt(): adopts the interface managed by the kernel for diskless boot 59*0Sstevel@tonic-gate * 60*0Sstevel@tonic-gate * input: void 61*0Sstevel@tonic-gate * output: int: nonzero on success, zero on failure 62*0Sstevel@tonic-gate */ 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate int 65*0Sstevel@tonic-gate dhcp_adopt(void) 66*0Sstevel@tonic-gate { 67*0Sstevel@tonic-gate int retval; 68*0Sstevel@tonic-gate dhcp_kcache_t *kcache = NULL; 69*0Sstevel@tonic-gate size_t kcache_size; 70*0Sstevel@tonic-gate PKT_LIST *plp = NULL; 71*0Sstevel@tonic-gate struct ifslist *ifsp; 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate retval = get_dhcp_kcache(&kcache, &kcache_size); 74*0Sstevel@tonic-gate if (retval == 0 || kcache_size < sizeof (dhcp_kcache_t)) { 75*0Sstevel@tonic-gate dhcpmsg(MSG_CRIT, "dhcp_adopt: cannot fetch kernel cache"); 76*0Sstevel@tonic-gate goto failure; 77*0Sstevel@tonic-gate } 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate dhcpmsg(MSG_DEBUG, "dhcp_adopt: fetched %s kcache", kcache->dk_if_name); 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gate /* 82*0Sstevel@tonic-gate * convert the kernel's ACK into binary 83*0Sstevel@tonic-gate */ 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate plp = calloc(1, sizeof (PKT_LIST)); 86*0Sstevel@tonic-gate if (plp == NULL) 87*0Sstevel@tonic-gate goto failure; 88*0Sstevel@tonic-gate 89*0Sstevel@tonic-gate plp->len = strlen(kcache->dk_ack) / 2; 90*0Sstevel@tonic-gate plp->pkt = malloc(plp->len); 91*0Sstevel@tonic-gate if (plp->pkt == NULL) 92*0Sstevel@tonic-gate goto failure; 93*0Sstevel@tonic-gate 94*0Sstevel@tonic-gate dhcpmsg(MSG_DEBUG, "dhcp_adopt: allocated ACK of %d bytes", plp->len); 95*0Sstevel@tonic-gate 96*0Sstevel@tonic-gate if (hexascii_to_octet(kcache->dk_ack, plp->len * 2, plp->pkt, &plp->len) 97*0Sstevel@tonic-gate != 0) { 98*0Sstevel@tonic-gate dhcpmsg(MSG_CRIT, "dhcp_adopt: cannot convert kernel ACK"); 99*0Sstevel@tonic-gate goto failure; 100*0Sstevel@tonic-gate } 101*0Sstevel@tonic-gate 102*0Sstevel@tonic-gate if (dhcp_options_scan(plp, B_TRUE) != 0) { 103*0Sstevel@tonic-gate dhcpmsg(MSG_CRIT, "dhcp_adopt: cannot parse kernel ACK"); 104*0Sstevel@tonic-gate goto failure; 105*0Sstevel@tonic-gate } 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate /* 108*0Sstevel@tonic-gate * make an interface to represent the "cached interface" in 109*0Sstevel@tonic-gate * the kernel, hook up the ACK packet we made, and send out 110*0Sstevel@tonic-gate * the extend request (to attempt to renew the lease). 111*0Sstevel@tonic-gate * 112*0Sstevel@tonic-gate * we do a send_extend() instead of doing a dhcp_init_reboot() 113*0Sstevel@tonic-gate * because although dhcp_init_reboot() is more correct from a 114*0Sstevel@tonic-gate * protocol perspective, it introduces a window where a 115*0Sstevel@tonic-gate * diskless client has no IP address but may need to page in 116*0Sstevel@tonic-gate * more of this program. we could mlockall(), but that's 117*0Sstevel@tonic-gate * going to be a mess, especially with handling malloc() and 118*0Sstevel@tonic-gate * stack growth, so it's easier to just renew(). the only 119*0Sstevel@tonic-gate * catch here is that if we are not granted a renewal, we're 120*0Sstevel@tonic-gate * totally hosed and can only bail out. 121*0Sstevel@tonic-gate */ 122*0Sstevel@tonic-gate 123*0Sstevel@tonic-gate ifsp = insert_ifs(kcache->dk_if_name, B_TRUE, &retval); 124*0Sstevel@tonic-gate if (ifsp == NULL) 125*0Sstevel@tonic-gate goto failure; 126*0Sstevel@tonic-gate 127*0Sstevel@tonic-gate ifsp->if_state = ADOPTING; 128*0Sstevel@tonic-gate ifsp->if_dflags |= DHCP_IF_PRIMARY; 129*0Sstevel@tonic-gate 130*0Sstevel@tonic-gate /* 131*0Sstevel@tonic-gate * move to BOUND and use the information in our ACK packet 132*0Sstevel@tonic-gate */ 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gate if (dhcp_bound(ifsp, plp) == 0) { 135*0Sstevel@tonic-gate dhcpmsg(MSG_CRIT, "dhcp_adopt: cannot use cached packet"); 136*0Sstevel@tonic-gate goto failure; 137*0Sstevel@tonic-gate } 138*0Sstevel@tonic-gate 139*0Sstevel@tonic-gate if (async_start(ifsp, DHCP_EXTEND, B_FALSE) == 0) { 140*0Sstevel@tonic-gate dhcpmsg(MSG_CRIT, "dhcp_adopt: async_start failed"); 141*0Sstevel@tonic-gate goto failure; 142*0Sstevel@tonic-gate } 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate if (dhcp_extending(ifsp) == 0) { 145*0Sstevel@tonic-gate dhcpmsg(MSG_CRIT, "dhcp_adopt: cannot send renew request"); 146*0Sstevel@tonic-gate goto failure; 147*0Sstevel@tonic-gate } 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate free(kcache); 150*0Sstevel@tonic-gate return (1); 151*0Sstevel@tonic-gate 152*0Sstevel@tonic-gate failure: 153*0Sstevel@tonic-gate free(kcache); 154*0Sstevel@tonic-gate if (plp != NULL) 155*0Sstevel@tonic-gate free(plp->pkt); 156*0Sstevel@tonic-gate free(plp); 157*0Sstevel@tonic-gate return (0); 158*0Sstevel@tonic-gate } 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gate /* 161*0Sstevel@tonic-gate * get_dhcp_kcache(): fetches the DHCP ACK and interface name from the kernel 162*0Sstevel@tonic-gate * 163*0Sstevel@tonic-gate * input: dhcp_kcache_t **: a dynamically-allocated cache packet 164*0Sstevel@tonic-gate * size_t *: the length of that packet (on return) 165*0Sstevel@tonic-gate * output: int: nonzero on success, zero on failure 166*0Sstevel@tonic-gate */ 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate static int 169*0Sstevel@tonic-gate get_dhcp_kcache(dhcp_kcache_t **kernel_cachep, size_t *kcache_size) 170*0Sstevel@tonic-gate { 171*0Sstevel@tonic-gate char dummy; 172*0Sstevel@tonic-gate long size; 173*0Sstevel@tonic-gate 174*0Sstevel@tonic-gate size = sysinfo(SI_DHCP_CACHE, &dummy, sizeof (dummy)); 175*0Sstevel@tonic-gate if (size == -1) 176*0Sstevel@tonic-gate return (0); 177*0Sstevel@tonic-gate 178*0Sstevel@tonic-gate *kcache_size = size; 179*0Sstevel@tonic-gate *kernel_cachep = malloc(*kcache_size); 180*0Sstevel@tonic-gate if (*kernel_cachep == NULL) 181*0Sstevel@tonic-gate return (0); 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gate (void) sysinfo(SI_DHCP_CACHE, (caddr_t)*kernel_cachep, size); 184*0Sstevel@tonic-gate return (1); 185*0Sstevel@tonic-gate } 186