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 2005 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate * 26*0Sstevel@tonic-gate * SELECTING 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 <stdlib.h> 33*0Sstevel@tonic-gate #include <stdio.h> 34*0Sstevel@tonic-gate #include <strings.h> 35*0Sstevel@tonic-gate #include <time.h> 36*0Sstevel@tonic-gate #include <limits.h> 37*0Sstevel@tonic-gate #include <netinet/in.h> 38*0Sstevel@tonic-gate #include <sys/socket.h> 39*0Sstevel@tonic-gate #include <net/route.h> 40*0Sstevel@tonic-gate #include <net/if.h> 41*0Sstevel@tonic-gate #include <netinet/dhcp.h> 42*0Sstevel@tonic-gate #include <netinet/udp.h> 43*0Sstevel@tonic-gate #include <netinet/ip_var.h> 44*0Sstevel@tonic-gate #include <netinet/udp_var.h> 45*0Sstevel@tonic-gate #include <stropts.h> /* FLUSHR/FLUSHW */ 46*0Sstevel@tonic-gate #include <dhcpmsg.h> 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate #include "states.h" 49*0Sstevel@tonic-gate #include "agent.h" 50*0Sstevel@tonic-gate #include "util.h" 51*0Sstevel@tonic-gate #include "interface.h" 52*0Sstevel@tonic-gate #include "packet.h" 53*0Sstevel@tonic-gate #include "defaults.h" 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gate static iu_eh_callback_t dhcp_collect_offers; 56*0Sstevel@tonic-gate static stop_func_t stop_selecting; 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate /* 59*0Sstevel@tonic-gate * dhcp_start(): starts DHCP on an interface 60*0Sstevel@tonic-gate * 61*0Sstevel@tonic-gate * input: iu_tq_t *: unused 62*0Sstevel@tonic-gate * void *: the interface to start DHCP on 63*0Sstevel@tonic-gate * output: void 64*0Sstevel@tonic-gate */ 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate /* ARGSUSED */ 67*0Sstevel@tonic-gate void 68*0Sstevel@tonic-gate dhcp_start(iu_tq_t *tqp, void *arg) 69*0Sstevel@tonic-gate { 70*0Sstevel@tonic-gate struct ifslist *ifsp = (struct ifslist *)arg; 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate if (check_ifs(ifsp) == 0) { 73*0Sstevel@tonic-gate (void) release_ifs(ifsp); 74*0Sstevel@tonic-gate return; 75*0Sstevel@tonic-gate } 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate dhcpmsg(MSG_VERBOSE, "starting DHCP on %s", ifsp->if_name); 78*0Sstevel@tonic-gate dhcp_selecting(ifsp); 79*0Sstevel@tonic-gate } 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gate /* 82*0Sstevel@tonic-gate * dhcp_selecting(): sends a DISCOVER and sets up reception for an OFFER 83*0Sstevel@tonic-gate * 84*0Sstevel@tonic-gate * input: struct ifslist *: the interface to send the DISCOVER on, ... 85*0Sstevel@tonic-gate * output: void 86*0Sstevel@tonic-gate */ 87*0Sstevel@tonic-gate 88*0Sstevel@tonic-gate void 89*0Sstevel@tonic-gate dhcp_selecting(struct ifslist *ifsp) 90*0Sstevel@tonic-gate { 91*0Sstevel@tonic-gate dhcp_pkt_t *dpkt; 92*0Sstevel@tonic-gate const char *reqhost; 93*0Sstevel@tonic-gate char hostfile[PATH_MAX + 1]; 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate /* 96*0Sstevel@tonic-gate * we first set up to collect OFFER packets as they arrive. 97*0Sstevel@tonic-gate * we then send out DISCOVER probes. then we wait at a 98*0Sstevel@tonic-gate * user-tunable number of seconds before seeing if OFFERs have 99*0Sstevel@tonic-gate * come in response to our DISCOVER. if none have come in, we 100*0Sstevel@tonic-gate * continue to wait, sending out our DISCOVER probes with 101*0Sstevel@tonic-gate * exponential backoff. if an OFFER is never received, we 102*0Sstevel@tonic-gate * will wait forever (note that since we're event-driven 103*0Sstevel@tonic-gate * though, we're still able to service other interfaces.) 104*0Sstevel@tonic-gate * 105*0Sstevel@tonic-gate * note that we do an reset_ifs() here because we may be 106*0Sstevel@tonic-gate * landing in dhcp_selecting() as a result of restarting DHCP, 107*0Sstevel@tonic-gate * so the ifs may not be fresh. 108*0Sstevel@tonic-gate */ 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate reset_ifs(ifsp); 111*0Sstevel@tonic-gate ifsp->if_state = SELECTING; 112*0Sstevel@tonic-gate 113*0Sstevel@tonic-gate if ((ifsp->if_offer_id = iu_register_event(eh, ifsp->if_dlpi_fd, POLLIN, 114*0Sstevel@tonic-gate dhcp_collect_offers, ifsp)) == -1) { 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate dhcpmsg(MSG_ERROR, "dhcp_selecting: cannot register to collect " 117*0Sstevel@tonic-gate "OFFER packets, reverting to INIT on %s", 118*0Sstevel@tonic-gate ifsp->if_name); 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gate ifsp->if_state = INIT; 121*0Sstevel@tonic-gate ifsp->if_dflags |= DHCP_IF_FAILED; 122*0Sstevel@tonic-gate ipc_action_finish(ifsp, DHCP_IPC_E_MEMORY); 123*0Sstevel@tonic-gate async_finish(ifsp); 124*0Sstevel@tonic-gate return; 125*0Sstevel@tonic-gate } else 126*0Sstevel@tonic-gate hold_ifs(ifsp); 127*0Sstevel@tonic-gate 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gate if (iu_schedule_timer(tq, ifsp->if_offer_wait, dhcp_requesting, 130*0Sstevel@tonic-gate ifsp) == -1) { 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gate dhcpmsg(MSG_ERROR, "dhcp_selecting: cannot schedule to read " 133*0Sstevel@tonic-gate "OFFER packets"); 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gate if (iu_unregister_event(eh, ifsp->if_offer_id, NULL) != 0) { 136*0Sstevel@tonic-gate ifsp->if_offer_id = -1; 137*0Sstevel@tonic-gate (void) release_ifs(ifsp); 138*0Sstevel@tonic-gate } 139*0Sstevel@tonic-gate 140*0Sstevel@tonic-gate ifsp->if_state = INIT; 141*0Sstevel@tonic-gate ifsp->if_dflags |= DHCP_IF_FAILED; 142*0Sstevel@tonic-gate ipc_action_finish(ifsp, DHCP_IPC_E_MEMORY); 143*0Sstevel@tonic-gate async_finish(ifsp); 144*0Sstevel@tonic-gate return; 145*0Sstevel@tonic-gate } else 146*0Sstevel@tonic-gate hold_ifs(ifsp); 147*0Sstevel@tonic-gate 148*0Sstevel@tonic-gate /* 149*0Sstevel@tonic-gate * Assemble DHCPDISCOVER message. The max dhcp message size 150*0Sstevel@tonic-gate * option is set to the interface max, minus the size of the udp and 151*0Sstevel@tonic-gate * ip headers. 152*0Sstevel@tonic-gate */ 153*0Sstevel@tonic-gate 154*0Sstevel@tonic-gate dpkt = init_pkt(ifsp, DISCOVER); 155*0Sstevel@tonic-gate 156*0Sstevel@tonic-gate add_pkt_opt16(dpkt, CD_MAX_DHCP_SIZE, htons(ifsp->if_max - 157*0Sstevel@tonic-gate sizeof (struct udpiphdr))); 158*0Sstevel@tonic-gate add_pkt_opt32(dpkt, CD_LEASE_TIME, htonl(DHCP_PERM)); 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gate add_pkt_opt(dpkt, CD_CLASS_ID, class_id, class_id_len); 161*0Sstevel@tonic-gate add_pkt_opt(dpkt, CD_REQUEST_LIST, ifsp->if_prl, ifsp->if_prllen); 162*0Sstevel@tonic-gate 163*0Sstevel@tonic-gate if (df_get_bool(ifsp->if_name, DF_REQUEST_HOSTNAME)) { 164*0Sstevel@tonic-gate dhcpmsg(MSG_DEBUG, "dhcp_selecting: DF_REQUEST_HOSTNAME"); 165*0Sstevel@tonic-gate (void) snprintf(hostfile, sizeof (hostfile), "/etc/hostname.%s", 166*0Sstevel@tonic-gate ifsp->if_name); 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate if ((reqhost = iffile_to_hostname(hostfile)) != NULL) { 169*0Sstevel@tonic-gate dhcpmsg(MSG_DEBUG, "dhcp_selecting: host %s", reqhost); 170*0Sstevel@tonic-gate if ((ifsp->if_reqhost = strdup(reqhost)) != NULL) 171*0Sstevel@tonic-gate add_pkt_opt(dpkt, CD_HOSTNAME, ifsp->if_reqhost, 172*0Sstevel@tonic-gate strlen(ifsp->if_reqhost)); 173*0Sstevel@tonic-gate else 174*0Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "dhcp_selecting: cannot" 175*0Sstevel@tonic-gate " allocate memory for host name option"); 176*0Sstevel@tonic-gate } 177*0Sstevel@tonic-gate } 178*0Sstevel@tonic-gate add_pkt_opt(dpkt, CD_END, NULL, 0); 179*0Sstevel@tonic-gate 180*0Sstevel@tonic-gate (void) send_pkt(ifsp, dpkt, htonl(INADDR_BROADCAST), stop_selecting); 181*0Sstevel@tonic-gate } 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gate /* 184*0Sstevel@tonic-gate * dhcp_collect_offers(): collects incoming OFFERs to a DISCOVER 185*0Sstevel@tonic-gate * 186*0Sstevel@tonic-gate * input: iu_eh_t *: unused 187*0Sstevel@tonic-gate * int: the file descriptor the OFFER arrived on 188*0Sstevel@tonic-gate * short: unused 189*0Sstevel@tonic-gate * iu_event_id_t: the id of this event callback with the handler 190*0Sstevel@tonic-gate * void *: the interface that received the OFFER 191*0Sstevel@tonic-gate * output: void 192*0Sstevel@tonic-gate */ 193*0Sstevel@tonic-gate 194*0Sstevel@tonic-gate /* ARGSUSED */ 195*0Sstevel@tonic-gate static void 196*0Sstevel@tonic-gate dhcp_collect_offers(iu_eh_t *eh, int fd, short events, iu_event_id_t id, 197*0Sstevel@tonic-gate void *arg) 198*0Sstevel@tonic-gate { 199*0Sstevel@tonic-gate struct ifslist *ifsp = (struct ifslist *)arg; 200*0Sstevel@tonic-gate 201*0Sstevel@tonic-gate if (verify_ifs(ifsp) == 0) { 202*0Sstevel@tonic-gate (void) ioctl(fd, I_FLUSH, FLUSHR|FLUSHW); 203*0Sstevel@tonic-gate return; 204*0Sstevel@tonic-gate } 205*0Sstevel@tonic-gate 206*0Sstevel@tonic-gate /* 207*0Sstevel@tonic-gate * DHCP_PUNTYPED messages are BOOTP server responses. 208*0Sstevel@tonic-gate */ 209*0Sstevel@tonic-gate 210*0Sstevel@tonic-gate (void) recv_pkt(ifsp, fd, DHCP_POFFER|DHCP_PUNTYPED, B_TRUE); 211*0Sstevel@tonic-gate } 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate /* 214*0Sstevel@tonic-gate * stop_selecting(): decides when to stop retransmitting DISCOVERs (never) 215*0Sstevel@tonic-gate * 216*0Sstevel@tonic-gate * input: struct ifslist *: the interface DISCOVERs are being sent on 217*0Sstevel@tonic-gate * unsigned int: the number of DISCOVERs sent so far 218*0Sstevel@tonic-gate * output: boolean_t: B_TRUE if retransmissions should stop 219*0Sstevel@tonic-gate */ 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate /* ARGSUSED */ 222*0Sstevel@tonic-gate static boolean_t 223*0Sstevel@tonic-gate stop_selecting(struct ifslist *ifsp, unsigned int n_discovers) 224*0Sstevel@tonic-gate { 225*0Sstevel@tonic-gate return (B_FALSE); 226*0Sstevel@tonic-gate } 227