1*ca987d46SWarner Losh /* $NetBSD: bootp.c,v 1.14 1998/02/16 11:10:54 drochner Exp $ */ 2*ca987d46SWarner Losh 3*ca987d46SWarner Losh /* 4*ca987d46SWarner Losh * Copyright (c) 1992 Regents of the University of California. 5*ca987d46SWarner Losh * All rights reserved. 6*ca987d46SWarner Losh * 7*ca987d46SWarner Losh * This software was developed by the Computer Systems Engineering group 8*ca987d46SWarner Losh * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 9*ca987d46SWarner Losh * contributed to Berkeley. 10*ca987d46SWarner Losh * 11*ca987d46SWarner Losh * Redistribution and use in source and binary forms, with or without 12*ca987d46SWarner Losh * modification, are permitted provided that the following conditions 13*ca987d46SWarner Losh * are met: 14*ca987d46SWarner Losh * 1. Redistributions of source code must retain the above copyright 15*ca987d46SWarner Losh * notice, this list of conditions and the following disclaimer. 16*ca987d46SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 17*ca987d46SWarner Losh * notice, this list of conditions and the following disclaimer in the 18*ca987d46SWarner Losh * documentation and/or other materials provided with the distribution. 19*ca987d46SWarner Losh * 3. Neither the name of the University nor the names of its contributors 20*ca987d46SWarner Losh * may be used to endorse or promote products derived from this software 21*ca987d46SWarner Losh * without specific prior written permission. 22*ca987d46SWarner Losh * 23*ca987d46SWarner Losh * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24*ca987d46SWarner Losh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25*ca987d46SWarner Losh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26*ca987d46SWarner Losh * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27*ca987d46SWarner Losh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28*ca987d46SWarner Losh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29*ca987d46SWarner Losh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30*ca987d46SWarner Losh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31*ca987d46SWarner Losh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32*ca987d46SWarner Losh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33*ca987d46SWarner Losh * SUCH DAMAGE. 34*ca987d46SWarner Losh * 35*ca987d46SWarner Losh * @(#) Header: bootp.c,v 1.4 93/09/11 03:13:51 leres Exp (LBL) 36*ca987d46SWarner Losh */ 37*ca987d46SWarner Losh 38*ca987d46SWarner Losh #include <sys/cdefs.h> 39*ca987d46SWarner Losh __FBSDID("$FreeBSD$"); 40*ca987d46SWarner Losh 41*ca987d46SWarner Losh #include <stddef.h> 42*ca987d46SWarner Losh #include <sys/types.h> 43*ca987d46SWarner Losh #include <sys/limits.h> 44*ca987d46SWarner Losh #include <sys/endian.h> 45*ca987d46SWarner Losh #include <netinet/in.h> 46*ca987d46SWarner Losh #include <netinet/in_systm.h> 47*ca987d46SWarner Losh 48*ca987d46SWarner Losh #include <string.h> 49*ca987d46SWarner Losh 50*ca987d46SWarner Losh #define BOOTP_DEBUGxx 51*ca987d46SWarner Losh #define SUPPORT_DHCP 52*ca987d46SWarner Losh 53*ca987d46SWarner Losh #define DHCP_ENV_NOVENDOR 1 /* do not parse vendor options */ 54*ca987d46SWarner Losh #define DHCP_ENV_PXE 10 /* assume pxe vendor options */ 55*ca987d46SWarner Losh #define DHCP_ENV_FREEBSD 11 /* assume freebsd vendor options */ 56*ca987d46SWarner Losh /* set DHCP_ENV to one of the values above to export dhcp options to kenv */ 57*ca987d46SWarner Losh #define DHCP_ENV DHCP_ENV_NO_VENDOR 58*ca987d46SWarner Losh 59*ca987d46SWarner Losh #include "stand.h" 60*ca987d46SWarner Losh #include "net.h" 61*ca987d46SWarner Losh #include "netif.h" 62*ca987d46SWarner Losh #include "bootp.h" 63*ca987d46SWarner Losh 64*ca987d46SWarner Losh 65*ca987d46SWarner Losh struct in_addr servip; 66*ca987d46SWarner Losh 67*ca987d46SWarner Losh static time_t bot; 68*ca987d46SWarner Losh 69*ca987d46SWarner Losh static char vm_rfc1048[4] = VM_RFC1048; 70*ca987d46SWarner Losh #ifdef BOOTP_VEND_CMU 71*ca987d46SWarner Losh static char vm_cmu[4] = VM_CMU; 72*ca987d46SWarner Losh #endif 73*ca987d46SWarner Losh 74*ca987d46SWarner Losh /* Local forwards */ 75*ca987d46SWarner Losh static ssize_t bootpsend(struct iodesc *, void *, size_t); 76*ca987d46SWarner Losh static ssize_t bootprecv(struct iodesc *, void **, void **, time_t); 77*ca987d46SWarner Losh static int vend_rfc1048(u_char *, u_int); 78*ca987d46SWarner Losh #ifdef BOOTP_VEND_CMU 79*ca987d46SWarner Losh static void vend_cmu(u_char *); 80*ca987d46SWarner Losh #endif 81*ca987d46SWarner Losh 82*ca987d46SWarner Losh #ifdef DHCP_ENV /* export the dhcp response to kenv */ 83*ca987d46SWarner Losh struct dhcp_opt; 84*ca987d46SWarner Losh static void setenv_(u_char *cp, u_char *ep, struct dhcp_opt *opts); 85*ca987d46SWarner Losh #else 86*ca987d46SWarner Losh #define setenv_(a, b, c) 87*ca987d46SWarner Losh #endif 88*ca987d46SWarner Losh 89*ca987d46SWarner Losh #ifdef SUPPORT_DHCP 90*ca987d46SWarner Losh static char expected_dhcpmsgtype = -1, dhcp_ok; 91*ca987d46SWarner Losh struct in_addr dhcp_serverip; 92*ca987d46SWarner Losh #endif 93*ca987d46SWarner Losh struct bootp *bootp_response; 94*ca987d46SWarner Losh size_t bootp_response_size; 95*ca987d46SWarner Losh 96*ca987d46SWarner Losh static void 97*ca987d46SWarner Losh bootp_fill_request(unsigned char *bp_vend) 98*ca987d46SWarner Losh { 99*ca987d46SWarner Losh /* 100*ca987d46SWarner Losh * We are booting from PXE, we want to send the string 101*ca987d46SWarner Losh * 'PXEClient' to the DHCP server so you have the option of 102*ca987d46SWarner Losh * only responding to PXE aware dhcp requests. 103*ca987d46SWarner Losh */ 104*ca987d46SWarner Losh bp_vend[0] = TAG_CLASSID; 105*ca987d46SWarner Losh bp_vend[1] = 9; 106*ca987d46SWarner Losh bcopy("PXEClient", &bp_vend[2], 9); 107*ca987d46SWarner Losh bp_vend[11] = TAG_USER_CLASS; 108*ca987d46SWarner Losh /* len of each user class + number of user class */ 109*ca987d46SWarner Losh bp_vend[12] = 8; 110*ca987d46SWarner Losh /* len of the first user class */ 111*ca987d46SWarner Losh bp_vend[13] = 7; 112*ca987d46SWarner Losh bcopy("FreeBSD", &bp_vend[14], 7); 113*ca987d46SWarner Losh bp_vend[21] = TAG_PARAM_REQ; 114*ca987d46SWarner Losh bp_vend[22] = 7; 115*ca987d46SWarner Losh bp_vend[23] = TAG_ROOTPATH; 116*ca987d46SWarner Losh bp_vend[24] = TAG_HOSTNAME; 117*ca987d46SWarner Losh bp_vend[25] = TAG_SWAPSERVER; 118*ca987d46SWarner Losh bp_vend[26] = TAG_GATEWAY; 119*ca987d46SWarner Losh bp_vend[27] = TAG_SUBNET_MASK; 120*ca987d46SWarner Losh bp_vend[28] = TAG_INTF_MTU; 121*ca987d46SWarner Losh bp_vend[29] = TAG_SERVERID; 122*ca987d46SWarner Losh bp_vend[30] = TAG_END; 123*ca987d46SWarner Losh } 124*ca987d46SWarner Losh 125*ca987d46SWarner Losh /* Fetch required bootp infomation */ 126*ca987d46SWarner Losh void 127*ca987d46SWarner Losh bootp(int sock) 128*ca987d46SWarner Losh { 129*ca987d46SWarner Losh void *pkt; 130*ca987d46SWarner Losh struct iodesc *d; 131*ca987d46SWarner Losh struct bootp *bp; 132*ca987d46SWarner Losh struct { 133*ca987d46SWarner Losh u_char header[HEADER_SIZE]; 134*ca987d46SWarner Losh struct bootp wbootp; 135*ca987d46SWarner Losh } wbuf; 136*ca987d46SWarner Losh struct bootp *rbootp; 137*ca987d46SWarner Losh 138*ca987d46SWarner Losh #ifdef BOOTP_DEBUG 139*ca987d46SWarner Losh if (debug) 140*ca987d46SWarner Losh printf("bootp: socket=%d\n", sock); 141*ca987d46SWarner Losh #endif 142*ca987d46SWarner Losh if (!bot) 143*ca987d46SWarner Losh bot = getsecs(); 144*ca987d46SWarner Losh 145*ca987d46SWarner Losh if (!(d = socktodesc(sock))) { 146*ca987d46SWarner Losh printf("bootp: bad socket. %d\n", sock); 147*ca987d46SWarner Losh return; 148*ca987d46SWarner Losh } 149*ca987d46SWarner Losh #ifdef BOOTP_DEBUG 150*ca987d46SWarner Losh if (debug) 151*ca987d46SWarner Losh printf("bootp: d=%lx\n", (long)d); 152*ca987d46SWarner Losh #endif 153*ca987d46SWarner Losh 154*ca987d46SWarner Losh bp = &wbuf.wbootp; 155*ca987d46SWarner Losh bzero(bp, sizeof(*bp)); 156*ca987d46SWarner Losh 157*ca987d46SWarner Losh bp->bp_op = BOOTREQUEST; 158*ca987d46SWarner Losh bp->bp_htype = 1; /* 10Mb Ethernet (48 bits) */ 159*ca987d46SWarner Losh bp->bp_hlen = 6; 160*ca987d46SWarner Losh bp->bp_xid = htonl(d->xid); 161*ca987d46SWarner Losh MACPY(d->myea, bp->bp_chaddr); 162*ca987d46SWarner Losh strncpy(bp->bp_file, bootfile, sizeof(bp->bp_file)); 163*ca987d46SWarner Losh bcopy(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)); 164*ca987d46SWarner Losh #ifdef SUPPORT_DHCP 165*ca987d46SWarner Losh bp->bp_vend[4] = TAG_DHCP_MSGTYPE; 166*ca987d46SWarner Losh bp->bp_vend[5] = 1; 167*ca987d46SWarner Losh bp->bp_vend[6] = DHCPDISCOVER; 168*ca987d46SWarner Losh bootp_fill_request(&bp->bp_vend[7]); 169*ca987d46SWarner Losh 170*ca987d46SWarner Losh #else 171*ca987d46SWarner Losh bp->bp_vend[4] = TAG_END; 172*ca987d46SWarner Losh #endif 173*ca987d46SWarner Losh 174*ca987d46SWarner Losh d->myip.s_addr = INADDR_ANY; 175*ca987d46SWarner Losh d->myport = htons(IPPORT_BOOTPC); 176*ca987d46SWarner Losh d->destip.s_addr = INADDR_BROADCAST; 177*ca987d46SWarner Losh d->destport = htons(IPPORT_BOOTPS); 178*ca987d46SWarner Losh 179*ca987d46SWarner Losh #ifdef SUPPORT_DHCP 180*ca987d46SWarner Losh expected_dhcpmsgtype = DHCPOFFER; 181*ca987d46SWarner Losh dhcp_ok = 0; 182*ca987d46SWarner Losh #endif 183*ca987d46SWarner Losh 184*ca987d46SWarner Losh if(sendrecv(d, 185*ca987d46SWarner Losh bootpsend, bp, sizeof(*bp), 186*ca987d46SWarner Losh bootprecv, &pkt, (void **)&rbootp) == -1) { 187*ca987d46SWarner Losh printf("bootp: no reply\n"); 188*ca987d46SWarner Losh return; 189*ca987d46SWarner Losh } 190*ca987d46SWarner Losh 191*ca987d46SWarner Losh #ifdef SUPPORT_DHCP 192*ca987d46SWarner Losh if(dhcp_ok) { 193*ca987d46SWarner Losh u_int32_t leasetime; 194*ca987d46SWarner Losh bp->bp_vend[6] = DHCPREQUEST; 195*ca987d46SWarner Losh bp->bp_vend[7] = TAG_REQ_ADDR; 196*ca987d46SWarner Losh bp->bp_vend[8] = 4; 197*ca987d46SWarner Losh bcopy(&rbootp->bp_yiaddr, &bp->bp_vend[9], 4); 198*ca987d46SWarner Losh bp->bp_vend[13] = TAG_SERVERID; 199*ca987d46SWarner Losh bp->bp_vend[14] = 4; 200*ca987d46SWarner Losh bcopy(&dhcp_serverip.s_addr, &bp->bp_vend[15], 4); 201*ca987d46SWarner Losh bp->bp_vend[19] = TAG_LEASETIME; 202*ca987d46SWarner Losh bp->bp_vend[20] = 4; 203*ca987d46SWarner Losh leasetime = htonl(300); 204*ca987d46SWarner Losh bcopy(&leasetime, &bp->bp_vend[21], 4); 205*ca987d46SWarner Losh bootp_fill_request(&bp->bp_vend[25]); 206*ca987d46SWarner Losh 207*ca987d46SWarner Losh expected_dhcpmsgtype = DHCPACK; 208*ca987d46SWarner Losh 209*ca987d46SWarner Losh free(pkt); 210*ca987d46SWarner Losh if(sendrecv(d, 211*ca987d46SWarner Losh bootpsend, bp, sizeof(*bp), 212*ca987d46SWarner Losh bootprecv, &pkt, (void **)&rbootp) == -1) { 213*ca987d46SWarner Losh printf("DHCPREQUEST failed\n"); 214*ca987d46SWarner Losh return; 215*ca987d46SWarner Losh } 216*ca987d46SWarner Losh } 217*ca987d46SWarner Losh #endif 218*ca987d46SWarner Losh 219*ca987d46SWarner Losh myip = d->myip = rbootp->bp_yiaddr; 220*ca987d46SWarner Losh servip = rbootp->bp_siaddr; 221*ca987d46SWarner Losh if (rootip.s_addr == INADDR_ANY) 222*ca987d46SWarner Losh rootip = servip; 223*ca987d46SWarner Losh bcopy(rbootp->bp_file, bootfile, sizeof(bootfile)); 224*ca987d46SWarner Losh bootfile[sizeof(bootfile) - 1] = '\0'; 225*ca987d46SWarner Losh 226*ca987d46SWarner Losh if (!netmask) { 227*ca987d46SWarner Losh if (IN_CLASSA(ntohl(myip.s_addr))) 228*ca987d46SWarner Losh netmask = htonl(IN_CLASSA_NET); 229*ca987d46SWarner Losh else if (IN_CLASSB(ntohl(myip.s_addr))) 230*ca987d46SWarner Losh netmask = htonl(IN_CLASSB_NET); 231*ca987d46SWarner Losh else 232*ca987d46SWarner Losh netmask = htonl(IN_CLASSC_NET); 233*ca987d46SWarner Losh #ifdef BOOTP_DEBUG 234*ca987d46SWarner Losh if (debug) 235*ca987d46SWarner Losh printf("'native netmask' is %s\n", intoa(netmask)); 236*ca987d46SWarner Losh #endif 237*ca987d46SWarner Losh } 238*ca987d46SWarner Losh 239*ca987d46SWarner Losh #ifdef BOOTP_DEBUG 240*ca987d46SWarner Losh if (debug) 241*ca987d46SWarner Losh printf("mask: %s\n", intoa(netmask)); 242*ca987d46SWarner Losh #endif 243*ca987d46SWarner Losh 244*ca987d46SWarner Losh /* We need a gateway if root is on a different net */ 245*ca987d46SWarner Losh if (!SAMENET(myip, rootip, netmask)) { 246*ca987d46SWarner Losh #ifdef BOOTP_DEBUG 247*ca987d46SWarner Losh if (debug) 248*ca987d46SWarner Losh printf("need gateway for root ip\n"); 249*ca987d46SWarner Losh #endif 250*ca987d46SWarner Losh } 251*ca987d46SWarner Losh 252*ca987d46SWarner Losh /* Toss gateway if on a different net */ 253*ca987d46SWarner Losh if (!SAMENET(myip, gateip, netmask)) { 254*ca987d46SWarner Losh #ifdef BOOTP_DEBUG 255*ca987d46SWarner Losh if (debug) 256*ca987d46SWarner Losh printf("gateway ip (%s) bad\n", inet_ntoa(gateip)); 257*ca987d46SWarner Losh #endif 258*ca987d46SWarner Losh gateip.s_addr = 0; 259*ca987d46SWarner Losh } 260*ca987d46SWarner Losh 261*ca987d46SWarner Losh /* Bump xid so next request will be unique. */ 262*ca987d46SWarner Losh ++d->xid; 263*ca987d46SWarner Losh free(pkt); 264*ca987d46SWarner Losh } 265*ca987d46SWarner Losh 266*ca987d46SWarner Losh /* Transmit a bootp request */ 267*ca987d46SWarner Losh static ssize_t 268*ca987d46SWarner Losh bootpsend(struct iodesc *d, void *pkt, size_t len) 269*ca987d46SWarner Losh { 270*ca987d46SWarner Losh struct bootp *bp; 271*ca987d46SWarner Losh 272*ca987d46SWarner Losh #ifdef BOOTP_DEBUG 273*ca987d46SWarner Losh if (debug) 274*ca987d46SWarner Losh printf("bootpsend: d=%lx called.\n", (long)d); 275*ca987d46SWarner Losh #endif 276*ca987d46SWarner Losh 277*ca987d46SWarner Losh bp = pkt; 278*ca987d46SWarner Losh bp->bp_secs = htons((u_short)(getsecs() - bot)); 279*ca987d46SWarner Losh 280*ca987d46SWarner Losh #ifdef BOOTP_DEBUG 281*ca987d46SWarner Losh if (debug) 282*ca987d46SWarner Losh printf("bootpsend: calling sendudp\n"); 283*ca987d46SWarner Losh #endif 284*ca987d46SWarner Losh 285*ca987d46SWarner Losh return (sendudp(d, pkt, len)); 286*ca987d46SWarner Losh } 287*ca987d46SWarner Losh 288*ca987d46SWarner Losh static ssize_t 289*ca987d46SWarner Losh bootprecv(struct iodesc *d, void **pkt, void **payload, time_t tleft) 290*ca987d46SWarner Losh { 291*ca987d46SWarner Losh ssize_t n; 292*ca987d46SWarner Losh struct bootp *bp; 293*ca987d46SWarner Losh void *ptr; 294*ca987d46SWarner Losh 295*ca987d46SWarner Losh #ifdef BOOTP_DEBUG 296*ca987d46SWarner Losh if (debug) 297*ca987d46SWarner Losh printf("bootp_recvoffer: called\n"); 298*ca987d46SWarner Losh #endif 299*ca987d46SWarner Losh 300*ca987d46SWarner Losh ptr = NULL; 301*ca987d46SWarner Losh n = readudp(d, &ptr, (void **)&bp, tleft); 302*ca987d46SWarner Losh if (n == -1 || n < sizeof(struct bootp) - BOOTP_VENDSIZE) 303*ca987d46SWarner Losh goto bad; 304*ca987d46SWarner Losh 305*ca987d46SWarner Losh #ifdef BOOTP_DEBUG 306*ca987d46SWarner Losh if (debug) 307*ca987d46SWarner Losh printf("bootprecv: checked. bp = %p, n = %zd\n", bp, n); 308*ca987d46SWarner Losh #endif 309*ca987d46SWarner Losh if (bp->bp_xid != htonl(d->xid)) { 310*ca987d46SWarner Losh #ifdef BOOTP_DEBUG 311*ca987d46SWarner Losh if (debug) { 312*ca987d46SWarner Losh printf("bootprecv: expected xid 0x%lx, got 0x%x\n", 313*ca987d46SWarner Losh d->xid, ntohl(bp->bp_xid)); 314*ca987d46SWarner Losh } 315*ca987d46SWarner Losh #endif 316*ca987d46SWarner Losh goto bad; 317*ca987d46SWarner Losh } 318*ca987d46SWarner Losh 319*ca987d46SWarner Losh #ifdef BOOTP_DEBUG 320*ca987d46SWarner Losh if (debug) 321*ca987d46SWarner Losh printf("bootprecv: got one!\n"); 322*ca987d46SWarner Losh #endif 323*ca987d46SWarner Losh 324*ca987d46SWarner Losh /* Suck out vendor info */ 325*ca987d46SWarner Losh if (bcmp(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)) == 0) { 326*ca987d46SWarner Losh int vsize = n - offsetof(struct bootp, bp_vend); 327*ca987d46SWarner Losh if (vend_rfc1048(bp->bp_vend, vsize) != 0) 328*ca987d46SWarner Losh goto bad; 329*ca987d46SWarner Losh 330*ca987d46SWarner Losh /* Save copy of bootp reply or DHCP ACK message */ 331*ca987d46SWarner Losh if (bp->bp_op == BOOTREPLY && 332*ca987d46SWarner Losh ((dhcp_ok == 1 && expected_dhcpmsgtype == DHCPACK) || 333*ca987d46SWarner Losh dhcp_ok == 0)) { 334*ca987d46SWarner Losh free(bootp_response); 335*ca987d46SWarner Losh bootp_response = malloc(n); 336*ca987d46SWarner Losh if (bootp_response != NULL) { 337*ca987d46SWarner Losh bootp_response_size = n; 338*ca987d46SWarner Losh bcopy(bp, bootp_response, bootp_response_size); 339*ca987d46SWarner Losh } 340*ca987d46SWarner Losh } 341*ca987d46SWarner Losh } 342*ca987d46SWarner Losh #ifdef BOOTP_VEND_CMU 343*ca987d46SWarner Losh else if (bcmp(vm_cmu, bp->bp_vend, sizeof(vm_cmu)) == 0) 344*ca987d46SWarner Losh vend_cmu(bp->bp_vend); 345*ca987d46SWarner Losh #endif 346*ca987d46SWarner Losh else 347*ca987d46SWarner Losh printf("bootprecv: unknown vendor 0x%lx\n", (long)bp->bp_vend); 348*ca987d46SWarner Losh 349*ca987d46SWarner Losh *pkt = ptr; 350*ca987d46SWarner Losh *payload = bp; 351*ca987d46SWarner Losh return (n); 352*ca987d46SWarner Losh bad: 353*ca987d46SWarner Losh free(ptr); 354*ca987d46SWarner Losh errno = 0; 355*ca987d46SWarner Losh return (-1); 356*ca987d46SWarner Losh } 357*ca987d46SWarner Losh 358*ca987d46SWarner Losh int 359*ca987d46SWarner Losh dhcp_try_rfc1048(u_char *cp, u_int len) 360*ca987d46SWarner Losh { 361*ca987d46SWarner Losh 362*ca987d46SWarner Losh expected_dhcpmsgtype = DHCPACK; 363*ca987d46SWarner Losh if (bcmp(vm_rfc1048, cp, sizeof(vm_rfc1048)) == 0) { 364*ca987d46SWarner Losh return (vend_rfc1048(cp, len)); 365*ca987d46SWarner Losh } 366*ca987d46SWarner Losh return (-1); 367*ca987d46SWarner Losh } 368*ca987d46SWarner Losh 369*ca987d46SWarner Losh static int 370*ca987d46SWarner Losh vend_rfc1048(u_char *cp, u_int len) 371*ca987d46SWarner Losh { 372*ca987d46SWarner Losh u_char *ep; 373*ca987d46SWarner Losh int size; 374*ca987d46SWarner Losh u_char tag; 375*ca987d46SWarner Losh const char *val; 376*ca987d46SWarner Losh 377*ca987d46SWarner Losh #ifdef BOOTP_DEBUG 378*ca987d46SWarner Losh if (debug) 379*ca987d46SWarner Losh printf("vend_rfc1048 bootp info. len=%d\n", len); 380*ca987d46SWarner Losh #endif 381*ca987d46SWarner Losh ep = cp + len; 382*ca987d46SWarner Losh 383*ca987d46SWarner Losh /* Step over magic cookie */ 384*ca987d46SWarner Losh cp += sizeof(int); 385*ca987d46SWarner Losh 386*ca987d46SWarner Losh setenv_(cp, ep, NULL); 387*ca987d46SWarner Losh 388*ca987d46SWarner Losh while (cp < ep) { 389*ca987d46SWarner Losh tag = *cp++; 390*ca987d46SWarner Losh size = *cp++; 391*ca987d46SWarner Losh if (tag == TAG_END) 392*ca987d46SWarner Losh break; 393*ca987d46SWarner Losh 394*ca987d46SWarner Losh if (tag == TAG_SUBNET_MASK) { 395*ca987d46SWarner Losh bcopy(cp, &netmask, sizeof(netmask)); 396*ca987d46SWarner Losh } 397*ca987d46SWarner Losh if (tag == TAG_GATEWAY) { 398*ca987d46SWarner Losh bcopy(cp, &gateip.s_addr, sizeof(gateip.s_addr)); 399*ca987d46SWarner Losh } 400*ca987d46SWarner Losh if (tag == TAG_SWAPSERVER) { 401*ca987d46SWarner Losh /* let it override bp_siaddr */ 402*ca987d46SWarner Losh bcopy(cp, &rootip.s_addr, sizeof(rootip.s_addr)); 403*ca987d46SWarner Losh } 404*ca987d46SWarner Losh if (tag == TAG_ROOTPATH) { 405*ca987d46SWarner Losh if ((val = getenv("dhcp.root-path")) == NULL) 406*ca987d46SWarner Losh val = (const char *)cp; 407*ca987d46SWarner Losh strlcpy(rootpath, val, sizeof(rootpath)); 408*ca987d46SWarner Losh } 409*ca987d46SWarner Losh if (tag == TAG_HOSTNAME) { 410*ca987d46SWarner Losh if ((val = getenv("dhcp.host-name")) == NULL) 411*ca987d46SWarner Losh val = (const char *)cp; 412*ca987d46SWarner Losh strlcpy(hostname, val, sizeof(hostname)); 413*ca987d46SWarner Losh } 414*ca987d46SWarner Losh if (tag == TAG_INTF_MTU) { 415*ca987d46SWarner Losh intf_mtu = 0; 416*ca987d46SWarner Losh if ((val = getenv("dhcp.interface-mtu")) != NULL) { 417*ca987d46SWarner Losh unsigned long tmp; 418*ca987d46SWarner Losh char *end; 419*ca987d46SWarner Losh 420*ca987d46SWarner Losh errno = 0; 421*ca987d46SWarner Losh /* 422*ca987d46SWarner Losh * Do not allow MTU to exceed max IPv4 packet 423*ca987d46SWarner Losh * size, max value of 16-bit word. 424*ca987d46SWarner Losh */ 425*ca987d46SWarner Losh tmp = strtoul(val, &end, 0); 426*ca987d46SWarner Losh if (errno != 0 || 427*ca987d46SWarner Losh *val == '\0' || *end != '\0' || 428*ca987d46SWarner Losh tmp > USHRT_MAX) { 429*ca987d46SWarner Losh printf("%s: bad value: \"%s\", " 430*ca987d46SWarner Losh "ignoring\n", 431*ca987d46SWarner Losh "dhcp.interface-mtu", val); 432*ca987d46SWarner Losh } else { 433*ca987d46SWarner Losh intf_mtu = (u_int)tmp; 434*ca987d46SWarner Losh } 435*ca987d46SWarner Losh } 436*ca987d46SWarner Losh if (intf_mtu <= 0) 437*ca987d46SWarner Losh intf_mtu = be16dec(cp); 438*ca987d46SWarner Losh } 439*ca987d46SWarner Losh #ifdef SUPPORT_DHCP 440*ca987d46SWarner Losh if (tag == TAG_DHCP_MSGTYPE) { 441*ca987d46SWarner Losh if(*cp != expected_dhcpmsgtype) 442*ca987d46SWarner Losh return(-1); 443*ca987d46SWarner Losh dhcp_ok = 1; 444*ca987d46SWarner Losh } 445*ca987d46SWarner Losh if (tag == TAG_SERVERID) { 446*ca987d46SWarner Losh bcopy(cp, &dhcp_serverip.s_addr, 447*ca987d46SWarner Losh sizeof(dhcp_serverip.s_addr)); 448*ca987d46SWarner Losh } 449*ca987d46SWarner Losh #endif 450*ca987d46SWarner Losh cp += size; 451*ca987d46SWarner Losh } 452*ca987d46SWarner Losh return(0); 453*ca987d46SWarner Losh } 454*ca987d46SWarner Losh 455*ca987d46SWarner Losh #ifdef BOOTP_VEND_CMU 456*ca987d46SWarner Losh static void 457*ca987d46SWarner Losh vend_cmu(u_char *cp) 458*ca987d46SWarner Losh { 459*ca987d46SWarner Losh struct cmu_vend *vp; 460*ca987d46SWarner Losh 461*ca987d46SWarner Losh #ifdef BOOTP_DEBUG 462*ca987d46SWarner Losh if (debug) 463*ca987d46SWarner Losh printf("vend_cmu bootp info.\n"); 464*ca987d46SWarner Losh #endif 465*ca987d46SWarner Losh vp = (struct cmu_vend *)cp; 466*ca987d46SWarner Losh 467*ca987d46SWarner Losh if (vp->v_smask.s_addr != 0) { 468*ca987d46SWarner Losh netmask = vp->v_smask.s_addr; 469*ca987d46SWarner Losh } 470*ca987d46SWarner Losh if (vp->v_dgate.s_addr != 0) { 471*ca987d46SWarner Losh gateip = vp->v_dgate; 472*ca987d46SWarner Losh } 473*ca987d46SWarner Losh } 474*ca987d46SWarner Losh #endif 475*ca987d46SWarner Losh 476*ca987d46SWarner Losh #ifdef DHCP_ENV 477*ca987d46SWarner Losh /* 478*ca987d46SWarner Losh * Parse DHCP options and store them into kenv variables. 479*ca987d46SWarner Losh * Original code from Danny Braniss, modifications by Luigi Rizzo. 480*ca987d46SWarner Losh * 481*ca987d46SWarner Losh * The parser is driven by tables which specify the type and name of 482*ca987d46SWarner Losh * each dhcp option and how it appears in kenv. 483*ca987d46SWarner Losh * The first entry in the list contains the prefix used to set the kenv 484*ca987d46SWarner Losh * name (including the . if needed), the last entry must have a 0 tag. 485*ca987d46SWarner Losh * Entries do not need to be sorted though it helps for readability. 486*ca987d46SWarner Losh * 487*ca987d46SWarner Losh * Certain vendor-specific tables can be enabled according to DHCP_ENV. 488*ca987d46SWarner Losh * Set it to 0 if you don't want any. 489*ca987d46SWarner Losh */ 490*ca987d46SWarner Losh enum opt_fmt { __NONE = 0, 491*ca987d46SWarner Losh __8 = 1, __16 = 2, __32 = 4, /* Unsigned fields, value=size */ 492*ca987d46SWarner Losh __IP, /* IPv4 address */ 493*ca987d46SWarner Losh __TXT, /* C string */ 494*ca987d46SWarner Losh __BYTES, /* byte sequence, printed %02x */ 495*ca987d46SWarner Losh __INDIR, /* name=value */ 496*ca987d46SWarner Losh __ILIST, /* name=value;name=value ... */ 497*ca987d46SWarner Losh __VE, /* vendor specific, recurse */ 498*ca987d46SWarner Losh }; 499*ca987d46SWarner Losh 500*ca987d46SWarner Losh struct dhcp_opt { 501*ca987d46SWarner Losh uint8_t tag; 502*ca987d46SWarner Losh uint8_t fmt; 503*ca987d46SWarner Losh const char *desc; 504*ca987d46SWarner Losh }; 505*ca987d46SWarner Losh 506*ca987d46SWarner Losh static struct dhcp_opt vndr_opt[] = { /* Vendor Specific Options */ 507*ca987d46SWarner Losh #if DHCP_ENV == DHCP_ENV_FREEBSD /* FreeBSD table in the original code */ 508*ca987d46SWarner Losh {0, 0, "FreeBSD"}, /* prefix */ 509*ca987d46SWarner Losh {1, __TXT, "kernel"}, 510*ca987d46SWarner Losh {2, __TXT, "kernelname"}, 511*ca987d46SWarner Losh {3, __TXT, "kernel_options"}, 512*ca987d46SWarner Losh {4, __IP, "usr-ip"}, 513*ca987d46SWarner Losh {5, __TXT, "conf-path"}, 514*ca987d46SWarner Losh {6, __TXT, "rc.conf0"}, 515*ca987d46SWarner Losh {7, __TXT, "rc.conf1"}, 516*ca987d46SWarner Losh {8, __TXT, "rc.conf2"}, 517*ca987d46SWarner Losh {9, __TXT, "rc.conf3"}, 518*ca987d46SWarner Losh {10, __TXT, "rc.conf4"}, 519*ca987d46SWarner Losh {11, __TXT, "rc.conf5"}, 520*ca987d46SWarner Losh {12, __TXT, "rc.conf6"}, 521*ca987d46SWarner Losh {13, __TXT, "rc.conf7"}, 522*ca987d46SWarner Losh {14, __TXT, "rc.conf8"}, 523*ca987d46SWarner Losh {15, __TXT, "rc.conf9"}, 524*ca987d46SWarner Losh 525*ca987d46SWarner Losh {20, __TXT, "boot.nfsroot.options"}, 526*ca987d46SWarner Losh 527*ca987d46SWarner Losh {245, __INDIR, ""}, 528*ca987d46SWarner Losh {246, __INDIR, ""}, 529*ca987d46SWarner Losh {247, __INDIR, ""}, 530*ca987d46SWarner Losh {248, __INDIR, ""}, 531*ca987d46SWarner Losh {249, __INDIR, ""}, 532*ca987d46SWarner Losh {250, __INDIR, ""}, 533*ca987d46SWarner Losh {251, __INDIR, ""}, 534*ca987d46SWarner Losh {252, __INDIR, ""}, 535*ca987d46SWarner Losh {253, __INDIR, ""}, 536*ca987d46SWarner Losh {254, __INDIR, ""}, 537*ca987d46SWarner Losh 538*ca987d46SWarner Losh #elif DHCP_ENV == DHCP_ENV_PXE /* some pxe options, RFC4578 */ 539*ca987d46SWarner Losh {0, 0, "pxe"}, /* prefix */ 540*ca987d46SWarner Losh {93, __16, "system-architecture"}, 541*ca987d46SWarner Losh {94, __BYTES, "network-interface"}, 542*ca987d46SWarner Losh {97, __BYTES, "machine-identifier"}, 543*ca987d46SWarner Losh #else /* default (empty) table */ 544*ca987d46SWarner Losh {0, 0, "dhcp.vendor."}, /* prefix */ 545*ca987d46SWarner Losh #endif 546*ca987d46SWarner Losh {0, __TXT, "%soption-%d"} 547*ca987d46SWarner Losh }; 548*ca987d46SWarner Losh 549*ca987d46SWarner Losh static struct dhcp_opt dhcp_opt[] = { 550*ca987d46SWarner Losh /* DHCP Option names, formats and codes, from RFC2132. */ 551*ca987d46SWarner Losh {0, 0, "dhcp."}, // prefix 552*ca987d46SWarner Losh {1, __IP, "subnet-mask"}, 553*ca987d46SWarner Losh {2, __32, "time-offset"}, /* this is signed */ 554*ca987d46SWarner Losh {3, __IP, "routers"}, 555*ca987d46SWarner Losh {4, __IP, "time-servers"}, 556*ca987d46SWarner Losh {5, __IP, "ien116-name-servers"}, 557*ca987d46SWarner Losh {6, __IP, "domain-name-servers"}, 558*ca987d46SWarner Losh {7, __IP, "log-servers"}, 559*ca987d46SWarner Losh {8, __IP, "cookie-servers"}, 560*ca987d46SWarner Losh {9, __IP, "lpr-servers"}, 561*ca987d46SWarner Losh {10, __IP, "impress-servers"}, 562*ca987d46SWarner Losh {11, __IP, "resource-location-servers"}, 563*ca987d46SWarner Losh {12, __TXT, "host-name"}, 564*ca987d46SWarner Losh {13, __16, "boot-size"}, 565*ca987d46SWarner Losh {14, __TXT, "merit-dump"}, 566*ca987d46SWarner Losh {15, __TXT, "domain-name"}, 567*ca987d46SWarner Losh {16, __IP, "swap-server"}, 568*ca987d46SWarner Losh {17, __TXT, "root-path"}, 569*ca987d46SWarner Losh {18, __TXT, "extensions-path"}, 570*ca987d46SWarner Losh {19, __8, "ip-forwarding"}, 571*ca987d46SWarner Losh {20, __8, "non-local-source-routing"}, 572*ca987d46SWarner Losh {21, __IP, "policy-filter"}, 573*ca987d46SWarner Losh {22, __16, "max-dgram-reassembly"}, 574*ca987d46SWarner Losh {23, __8, "default-ip-ttl"}, 575*ca987d46SWarner Losh {24, __32, "path-mtu-aging-timeout"}, 576*ca987d46SWarner Losh {25, __16, "path-mtu-plateau-table"}, 577*ca987d46SWarner Losh {26, __16, "interface-mtu"}, 578*ca987d46SWarner Losh {27, __8, "all-subnets-local"}, 579*ca987d46SWarner Losh {28, __IP, "broadcast-address"}, 580*ca987d46SWarner Losh {29, __8, "perform-mask-discovery"}, 581*ca987d46SWarner Losh {30, __8, "mask-supplier"}, 582*ca987d46SWarner Losh {31, __8, "perform-router-discovery"}, 583*ca987d46SWarner Losh {32, __IP, "router-solicitation-address"}, 584*ca987d46SWarner Losh {33, __IP, "static-routes"}, 585*ca987d46SWarner Losh {34, __8, "trailer-encapsulation"}, 586*ca987d46SWarner Losh {35, __32, "arp-cache-timeout"}, 587*ca987d46SWarner Losh {36, __8, "ieee802-3-encapsulation"}, 588*ca987d46SWarner Losh {37, __8, "default-tcp-ttl"}, 589*ca987d46SWarner Losh {38, __32, "tcp-keepalive-interval"}, 590*ca987d46SWarner Losh {39, __8, "tcp-keepalive-garbage"}, 591*ca987d46SWarner Losh {40, __TXT, "nis-domain"}, 592*ca987d46SWarner Losh {41, __IP, "nis-servers"}, 593*ca987d46SWarner Losh {42, __IP, "ntp-servers"}, 594*ca987d46SWarner Losh {43, __VE, "vendor-encapsulated-options"}, 595*ca987d46SWarner Losh {44, __IP, "netbios-name-servers"}, 596*ca987d46SWarner Losh {45, __IP, "netbios-dd-server"}, 597*ca987d46SWarner Losh {46, __8, "netbios-node-type"}, 598*ca987d46SWarner Losh {47, __TXT, "netbios-scope"}, 599*ca987d46SWarner Losh {48, __IP, "x-font-servers"}, 600*ca987d46SWarner Losh {49, __IP, "x-display-managers"}, 601*ca987d46SWarner Losh {50, __IP, "dhcp-requested-address"}, 602*ca987d46SWarner Losh {51, __32, "dhcp-lease-time"}, 603*ca987d46SWarner Losh {52, __8, "dhcp-option-overload"}, 604*ca987d46SWarner Losh {53, __8, "dhcp-message-type"}, 605*ca987d46SWarner Losh {54, __IP, "dhcp-server-identifier"}, 606*ca987d46SWarner Losh {55, __8, "dhcp-parameter-request-list"}, 607*ca987d46SWarner Losh {56, __TXT, "dhcp-message"}, 608*ca987d46SWarner Losh {57, __16, "dhcp-max-message-size"}, 609*ca987d46SWarner Losh {58, __32, "dhcp-renewal-time"}, 610*ca987d46SWarner Losh {59, __32, "dhcp-rebinding-time"}, 611*ca987d46SWarner Losh {60, __TXT, "vendor-class-identifier"}, 612*ca987d46SWarner Losh {61, __TXT, "dhcp-client-identifier"}, 613*ca987d46SWarner Losh {64, __TXT, "nisplus-domain"}, 614*ca987d46SWarner Losh {65, __IP, "nisplus-servers"}, 615*ca987d46SWarner Losh {66, __TXT, "tftp-server-name"}, 616*ca987d46SWarner Losh {67, __TXT, "bootfile-name"}, 617*ca987d46SWarner Losh {68, __IP, "mobile-ip-home-agent"}, 618*ca987d46SWarner Losh {69, __IP, "smtp-server"}, 619*ca987d46SWarner Losh {70, __IP, "pop-server"}, 620*ca987d46SWarner Losh {71, __IP, "nntp-server"}, 621*ca987d46SWarner Losh {72, __IP, "www-server"}, 622*ca987d46SWarner Losh {73, __IP, "finger-server"}, 623*ca987d46SWarner Losh {74, __IP, "irc-server"}, 624*ca987d46SWarner Losh {75, __IP, "streettalk-server"}, 625*ca987d46SWarner Losh {76, __IP, "streettalk-directory-assistance-server"}, 626*ca987d46SWarner Losh {77, __TXT, "user-class"}, 627*ca987d46SWarner Losh {85, __IP, "nds-servers"}, 628*ca987d46SWarner Losh {86, __TXT, "nds-tree-name"}, 629*ca987d46SWarner Losh {87, __TXT, "nds-context"}, 630*ca987d46SWarner Losh {210, __TXT, "authenticate"}, 631*ca987d46SWarner Losh 632*ca987d46SWarner Losh /* use the following entries for arbitrary variables */ 633*ca987d46SWarner Losh {246, __ILIST, ""}, 634*ca987d46SWarner Losh {247, __ILIST, ""}, 635*ca987d46SWarner Losh {248, __ILIST, ""}, 636*ca987d46SWarner Losh {249, __ILIST, ""}, 637*ca987d46SWarner Losh {250, __INDIR, ""}, 638*ca987d46SWarner Losh {251, __INDIR, ""}, 639*ca987d46SWarner Losh {252, __INDIR, ""}, 640*ca987d46SWarner Losh {253, __INDIR, ""}, 641*ca987d46SWarner Losh {254, __INDIR, ""}, 642*ca987d46SWarner Losh {0, __TXT, "%soption-%d"} 643*ca987d46SWarner Losh }; 644*ca987d46SWarner Losh 645*ca987d46SWarner Losh /* 646*ca987d46SWarner Losh * parse a dhcp response, set environment variables translating options 647*ca987d46SWarner Losh * names and values according to the tables above. Also set dhcp.tags 648*ca987d46SWarner Losh * to the list of selected tags. 649*ca987d46SWarner Losh */ 650*ca987d46SWarner Losh static void 651*ca987d46SWarner Losh setenv_(u_char *cp, u_char *ep, struct dhcp_opt *opts) 652*ca987d46SWarner Losh { 653*ca987d46SWarner Losh u_char *ncp; 654*ca987d46SWarner Losh u_char tag; 655*ca987d46SWarner Losh char tags[512], *tp; /* the list of tags */ 656*ca987d46SWarner Losh 657*ca987d46SWarner Losh #define FLD_SEP ',' /* separator in list of elements */ 658*ca987d46SWarner Losh ncp = cp; 659*ca987d46SWarner Losh tp = tags; 660*ca987d46SWarner Losh if (opts == NULL) 661*ca987d46SWarner Losh opts = dhcp_opt; 662*ca987d46SWarner Losh 663*ca987d46SWarner Losh while (ncp < ep) { 664*ca987d46SWarner Losh unsigned int size; /* option size */ 665*ca987d46SWarner Losh char *vp, *endv, buf[256]; /* the value buffer */ 666*ca987d46SWarner Losh struct dhcp_opt *op; 667*ca987d46SWarner Losh 668*ca987d46SWarner Losh tag = *ncp++; /* extract tag and size */ 669*ca987d46SWarner Losh size = *ncp++; 670*ca987d46SWarner Losh cp = ncp; /* current payload */ 671*ca987d46SWarner Losh ncp += size; /* point to the next option */ 672*ca987d46SWarner Losh 673*ca987d46SWarner Losh if (tag == TAG_END) 674*ca987d46SWarner Losh break; 675*ca987d46SWarner Losh if (tag == 0) 676*ca987d46SWarner Losh continue; 677*ca987d46SWarner Losh 678*ca987d46SWarner Losh for (op = opts+1; op->tag && op->tag != tag; op++) 679*ca987d46SWarner Losh ; 680*ca987d46SWarner Losh /* if not found we end up on the default entry */ 681*ca987d46SWarner Losh 682*ca987d46SWarner Losh /* 683*ca987d46SWarner Losh * Copy data into the buffer. libstand does not have snprintf so we 684*ca987d46SWarner Losh * need to be careful with sprintf(). With strings, the source is 685*ca987d46SWarner Losh * always <256 char so shorter than the buffer so we are safe; with 686*ca987d46SWarner Losh * other arguments, the longest string is inet_ntoa which is 16 bytes 687*ca987d46SWarner Losh * so we make sure to have always enough room in the string before 688*ca987d46SWarner Losh * trying an sprint. 689*ca987d46SWarner Losh */ 690*ca987d46SWarner Losh vp = buf; 691*ca987d46SWarner Losh *vp = '\0'; 692*ca987d46SWarner Losh endv = buf + sizeof(buf) - 1 - 16; /* last valid write position */ 693*ca987d46SWarner Losh 694*ca987d46SWarner Losh switch(op->fmt) { 695*ca987d46SWarner Losh case __NONE: 696*ca987d46SWarner Losh break; /* should not happen */ 697*ca987d46SWarner Losh 698*ca987d46SWarner Losh case __VE: /* recurse, vendor specific */ 699*ca987d46SWarner Losh setenv_(cp, cp+size, vndr_opt); 700*ca987d46SWarner Losh break; 701*ca987d46SWarner Losh 702*ca987d46SWarner Losh case __IP: /* ip address */ 703*ca987d46SWarner Losh for (; size > 0 && vp < endv; size -= 4, cp += 4) { 704*ca987d46SWarner Losh struct in_addr in_ip; /* ip addresses */ 705*ca987d46SWarner Losh if (vp != buf) 706*ca987d46SWarner Losh *vp++ = FLD_SEP; 707*ca987d46SWarner Losh bcopy(cp, &in_ip.s_addr, sizeof(in_ip.s_addr)); 708*ca987d46SWarner Losh sprintf(vp, "%s", inet_ntoa(in_ip)); 709*ca987d46SWarner Losh vp += strlen(vp); 710*ca987d46SWarner Losh } 711*ca987d46SWarner Losh break; 712*ca987d46SWarner Losh 713*ca987d46SWarner Losh case __BYTES: /* opaque byte string */ 714*ca987d46SWarner Losh for (; size > 0 && vp < endv; size -= 1, cp += 1) { 715*ca987d46SWarner Losh sprintf(vp, "%02x", *cp); 716*ca987d46SWarner Losh vp += strlen(vp); 717*ca987d46SWarner Losh } 718*ca987d46SWarner Losh break; 719*ca987d46SWarner Losh 720*ca987d46SWarner Losh case __TXT: 721*ca987d46SWarner Losh bcopy(cp, buf, size); /* cannot overflow */ 722*ca987d46SWarner Losh buf[size] = 0; 723*ca987d46SWarner Losh break; 724*ca987d46SWarner Losh 725*ca987d46SWarner Losh case __32: 726*ca987d46SWarner Losh case __16: 727*ca987d46SWarner Losh case __8: /* op->fmt is also the length of each field */ 728*ca987d46SWarner Losh for (; size > 0 && vp < endv; size -= op->fmt, cp += op->fmt) { 729*ca987d46SWarner Losh uint32_t v; 730*ca987d46SWarner Losh if (op->fmt == __32) 731*ca987d46SWarner Losh v = (cp[0]<<24) + (cp[1]<<16) + (cp[2]<<8) + cp[3]; 732*ca987d46SWarner Losh else if (op->fmt == __16) 733*ca987d46SWarner Losh v = (cp[0]<<8) + cp[1]; 734*ca987d46SWarner Losh else 735*ca987d46SWarner Losh v = cp[0]; 736*ca987d46SWarner Losh if (vp != buf) 737*ca987d46SWarner Losh *vp++ = FLD_SEP; 738*ca987d46SWarner Losh sprintf(vp, "%u", v); 739*ca987d46SWarner Losh vp += strlen(vp); 740*ca987d46SWarner Losh } 741*ca987d46SWarner Losh break; 742*ca987d46SWarner Losh 743*ca987d46SWarner Losh case __INDIR: /* name=value */ 744*ca987d46SWarner Losh case __ILIST: /* name=value;name=value... */ 745*ca987d46SWarner Losh bcopy(cp, buf, size); /* cannot overflow */ 746*ca987d46SWarner Losh buf[size] = '\0'; 747*ca987d46SWarner Losh for (endv = buf; endv; endv = vp) { 748*ca987d46SWarner Losh u_char *s = NULL; /* semicolon ? */ 749*ca987d46SWarner Losh 750*ca987d46SWarner Losh /* skip leading whitespace */ 751*ca987d46SWarner Losh while (*endv && strchr(" \t\n\r", *endv)) 752*ca987d46SWarner Losh endv++; 753*ca987d46SWarner Losh vp = strchr(endv, '='); /* find name=value separator */ 754*ca987d46SWarner Losh if (!vp) 755*ca987d46SWarner Losh break; 756*ca987d46SWarner Losh *vp++ = 0; 757*ca987d46SWarner Losh if (op->fmt == __ILIST && (s = strchr(vp, ';'))) 758*ca987d46SWarner Losh *s++ = '\0'; 759*ca987d46SWarner Losh setenv(endv, vp, 1); 760*ca987d46SWarner Losh vp = s; /* prepare for next round */ 761*ca987d46SWarner Losh } 762*ca987d46SWarner Losh buf[0] = '\0'; /* option already done */ 763*ca987d46SWarner Losh } 764*ca987d46SWarner Losh 765*ca987d46SWarner Losh if (tp - tags < sizeof(tags) - 5) { /* add tag to the list */ 766*ca987d46SWarner Losh if (tp != tags) 767*ca987d46SWarner Losh *tp++ = FLD_SEP; 768*ca987d46SWarner Losh sprintf(tp, "%d", tag); 769*ca987d46SWarner Losh tp += strlen(tp); 770*ca987d46SWarner Losh } 771*ca987d46SWarner Losh if (buf[0]) { 772*ca987d46SWarner Losh char env[128]; /* the string name */ 773*ca987d46SWarner Losh 774*ca987d46SWarner Losh if (op->tag == 0) 775*ca987d46SWarner Losh sprintf(env, op->desc, opts[0].desc, tag); 776*ca987d46SWarner Losh else 777*ca987d46SWarner Losh sprintf(env, "%s%s", opts[0].desc, op->desc); 778*ca987d46SWarner Losh /* 779*ca987d46SWarner Losh * Do not replace existing values in the environment, so that 780*ca987d46SWarner Losh * locally-obtained values can override server-provided values. 781*ca987d46SWarner Losh */ 782*ca987d46SWarner Losh setenv(env, buf, 0); 783*ca987d46SWarner Losh } 784*ca987d46SWarner Losh } 785*ca987d46SWarner Losh if (tp != tags) { 786*ca987d46SWarner Losh char env[128]; /* the string name */ 787*ca987d46SWarner Losh sprintf(env, "%stags", opts[0].desc); 788*ca987d46SWarner Losh setenv(env, tags, 1); 789*ca987d46SWarner Losh } 790*ca987d46SWarner Losh } 791*ca987d46SWarner Losh #endif /* additional dhcp */ 792