14a5d661aSToomas Soome /* $NetBSD: bootp.c,v 1.14 1998/02/16 11:10:54 drochner Exp $ */ 24a5d661aSToomas Soome 34a5d661aSToomas Soome /* 44a5d661aSToomas Soome * Copyright (c) 1992 Regents of the University of California. 54a5d661aSToomas Soome * All rights reserved. 64a5d661aSToomas Soome * 74a5d661aSToomas Soome * This software was developed by the Computer Systems Engineering group 84a5d661aSToomas Soome * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 94a5d661aSToomas Soome * contributed to Berkeley. 104a5d661aSToomas Soome * 114a5d661aSToomas Soome * Redistribution and use in source and binary forms, with or without 124a5d661aSToomas Soome * modification, are permitted provided that the following conditions 134a5d661aSToomas Soome * are met: 144a5d661aSToomas Soome * 1. Redistributions of source code must retain the above copyright 154a5d661aSToomas Soome * notice, this list of conditions and the following disclaimer. 164a5d661aSToomas Soome * 2. Redistributions in binary form must reproduce the above copyright 174a5d661aSToomas Soome * notice, this list of conditions and the following disclaimer in the 184a5d661aSToomas Soome * documentation and/or other materials provided with the distribution. 194a5d661aSToomas Soome * 4. Neither the name of the University nor the names of its contributors 204a5d661aSToomas Soome * may be used to endorse or promote products derived from this software 214a5d661aSToomas Soome * without specific prior written permission. 224a5d661aSToomas Soome * 234a5d661aSToomas Soome * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 244a5d661aSToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 254a5d661aSToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 264a5d661aSToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 274a5d661aSToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 284a5d661aSToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 294a5d661aSToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 304a5d661aSToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 314a5d661aSToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 324a5d661aSToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 334a5d661aSToomas Soome * SUCH DAMAGE. 344a5d661aSToomas Soome * 354a5d661aSToomas Soome * @(#) Header: bootp.c,v 1.4 93/09/11 03:13:51 leres Exp (LBL) 364a5d661aSToomas Soome */ 374a5d661aSToomas Soome 384a5d661aSToomas Soome #include <sys/cdefs.h> 394a5d661aSToomas Soome 404a5d661aSToomas Soome #include <sys/types.h> 41e1bd2803SToomas Soome #include <sys/limits.h> 42e1bd2803SToomas Soome #include <sys/endian.h> 434a5d661aSToomas Soome #include <netinet/in.h> 444a5d661aSToomas Soome #include <netinet/in_systm.h> 454a5d661aSToomas Soome 464a5d661aSToomas Soome #include <string.h> 474a5d661aSToomas Soome 484a5d661aSToomas Soome #define BOOTP_DEBUGxx 494a5d661aSToomas Soome #define SUPPORT_DHCP 504a5d661aSToomas Soome 514a5d661aSToomas Soome #define DHCP_ENV_NOVENDOR 1 /* do not parse vendor options */ 524a5d661aSToomas Soome #define DHCP_ENV_PXE 10 /* assume pxe vendor options */ 534a5d661aSToomas Soome #define DHCP_ENV_FREEBSD 11 /* assume freebsd vendor options */ 544a5d661aSToomas Soome /* set DHCP_ENV to one of the values above to export dhcp options to kenv */ 554a5d661aSToomas Soome #define DHCP_ENV DHCP_ENV_NO_VENDOR 564a5d661aSToomas Soome 574a5d661aSToomas Soome #include "stand.h" 584a5d661aSToomas Soome #include "net.h" 594a5d661aSToomas Soome #include "netif.h" 604a5d661aSToomas Soome #include "bootp.h" 614a5d661aSToomas Soome 624a5d661aSToomas Soome 634a5d661aSToomas Soome struct in_addr servip; 644a5d661aSToomas Soome 654a5d661aSToomas Soome static time_t bot; 664a5d661aSToomas Soome 674a5d661aSToomas Soome static char vm_rfc1048[4] = VM_RFC1048; 684a5d661aSToomas Soome #ifdef BOOTP_VEND_CMU 694a5d661aSToomas Soome static char vm_cmu[4] = VM_CMU; 704a5d661aSToomas Soome #endif 714a5d661aSToomas Soome 724a5d661aSToomas Soome /* Local forwards */ 734a5d661aSToomas Soome static ssize_t bootpsend(struct iodesc *, void *, size_t); 744a5d661aSToomas Soome static ssize_t bootprecv(struct iodesc *, void *, size_t, time_t); 754a5d661aSToomas Soome static int vend_rfc1048(u_char *, u_int); 764a5d661aSToomas Soome #ifdef BOOTP_VEND_CMU 774a5d661aSToomas Soome static void vend_cmu(u_char *); 784a5d661aSToomas Soome #endif 794a5d661aSToomas Soome 804a5d661aSToomas Soome #ifdef DHCP_ENV /* export the dhcp response to kenv */ 814a5d661aSToomas Soome struct dhcp_opt; 824a5d661aSToomas Soome static void setenv_(u_char *cp, u_char *ep, struct dhcp_opt *opts); 834a5d661aSToomas Soome #else 844a5d661aSToomas Soome #define setenv_(a, b, c) 854a5d661aSToomas Soome #endif 864a5d661aSToomas Soome 874a5d661aSToomas Soome #ifdef SUPPORT_DHCP 884a5d661aSToomas Soome static char expected_dhcpmsgtype = -1, dhcp_ok; 894a5d661aSToomas Soome struct in_addr dhcp_serverip; 904a5d661aSToomas Soome #endif 914a5d661aSToomas Soome 924a5d661aSToomas Soome /* Fetch required bootp infomation */ 934a5d661aSToomas Soome void 94e1bd2803SToomas Soome bootp(int sock, int flag) 954a5d661aSToomas Soome { 964a5d661aSToomas Soome struct iodesc *d; 974a5d661aSToomas Soome struct bootp *bp; 984a5d661aSToomas Soome struct { 994a5d661aSToomas Soome u_char header[HEADER_SIZE]; 1004a5d661aSToomas Soome struct bootp wbootp; 1014a5d661aSToomas Soome } wbuf; 1024a5d661aSToomas Soome struct { 1034a5d661aSToomas Soome u_char header[HEADER_SIZE]; 1044a5d661aSToomas Soome struct bootp rbootp; 1054a5d661aSToomas Soome } rbuf; 1064a5d661aSToomas Soome 1074a5d661aSToomas Soome #ifdef BOOTP_DEBUG 1084a5d661aSToomas Soome if (debug) 1094a5d661aSToomas Soome printf("bootp: socket=%d\n", sock); 1104a5d661aSToomas Soome #endif 1114a5d661aSToomas Soome if (!bot) 1124a5d661aSToomas Soome bot = getsecs(); 1134a5d661aSToomas Soome 1144a5d661aSToomas Soome if (!(d = socktodesc(sock))) { 1154a5d661aSToomas Soome printf("bootp: bad socket. %d\n", sock); 1164a5d661aSToomas Soome return; 1174a5d661aSToomas Soome } 1184a5d661aSToomas Soome #ifdef BOOTP_DEBUG 1194a5d661aSToomas Soome if (debug) 1204a5d661aSToomas Soome printf("bootp: d=%lx\n", (long)d); 1214a5d661aSToomas Soome #endif 1224a5d661aSToomas Soome 1234a5d661aSToomas Soome bp = &wbuf.wbootp; 1244a5d661aSToomas Soome bzero(bp, sizeof(*bp)); 1254a5d661aSToomas Soome 1264a5d661aSToomas Soome bp->bp_op = BOOTREQUEST; 1274a5d661aSToomas Soome bp->bp_htype = 1; /* 10Mb Ethernet (48 bits) */ 1284a5d661aSToomas Soome bp->bp_hlen = 6; 1294a5d661aSToomas Soome bp->bp_xid = htonl(d->xid); 1304a5d661aSToomas Soome MACPY(d->myea, bp->bp_chaddr); 1314a5d661aSToomas Soome strncpy(bp->bp_file, bootfile, sizeof(bp->bp_file)); 1324a5d661aSToomas Soome bcopy(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)); 1334a5d661aSToomas Soome #ifdef SUPPORT_DHCP 1344a5d661aSToomas Soome bp->bp_vend[4] = TAG_DHCP_MSGTYPE; 1354a5d661aSToomas Soome bp->bp_vend[5] = 1; 1364a5d661aSToomas Soome bp->bp_vend[6] = DHCPDISCOVER; 1374a5d661aSToomas Soome 1384a5d661aSToomas Soome /* 1394a5d661aSToomas Soome * If we are booting from PXE, we want to send the string 1404a5d661aSToomas Soome * 'PXEClient' to the DHCP server so you have the option of 1414a5d661aSToomas Soome * only responding to PXE aware dhcp requests. 1424a5d661aSToomas Soome */ 1434a5d661aSToomas Soome if (flag & BOOTP_PXE) { 1444a5d661aSToomas Soome bp->bp_vend[7] = TAG_CLASSID; 1454a5d661aSToomas Soome bp->bp_vend[8] = 9; 1464a5d661aSToomas Soome bcopy("PXEClient", &bp->bp_vend[9], 9); 147220923b8SToomas Soome bp->bp_vend[18] = TAG_PARAM_REQ; 148*26c793cfSToomas Soome bp->bp_vend[19] = 7; 149220923b8SToomas Soome bp->bp_vend[20] = TAG_ROOTPATH; 150*26c793cfSToomas Soome bp->bp_vend[21] = TAG_HOSTNAME; 151*26c793cfSToomas Soome bp->bp_vend[22] = TAG_SWAPSERVER; 152*26c793cfSToomas Soome bp->bp_vend[23] = TAG_GATEWAY; 153*26c793cfSToomas Soome bp->bp_vend[24] = TAG_SUBNET_MASK; 154*26c793cfSToomas Soome bp->bp_vend[25] = TAG_INTF_MTU; 155*26c793cfSToomas Soome bp->bp_vend[26] = TAG_SERVERID; 156*26c793cfSToomas Soome bp->bp_vend[27] = TAG_END; 1574a5d661aSToomas Soome } else 1584a5d661aSToomas Soome bp->bp_vend[7] = TAG_END; 1594a5d661aSToomas Soome #else 1604a5d661aSToomas Soome bp->bp_vend[4] = TAG_END; 1614a5d661aSToomas Soome #endif 1624a5d661aSToomas Soome 1634a5d661aSToomas Soome d->myip.s_addr = INADDR_ANY; 1644a5d661aSToomas Soome d->myport = htons(IPPORT_BOOTPC); 1654a5d661aSToomas Soome d->destip.s_addr = INADDR_BROADCAST; 1664a5d661aSToomas Soome d->destport = htons(IPPORT_BOOTPS); 1674a5d661aSToomas Soome 1684a5d661aSToomas Soome #ifdef SUPPORT_DHCP 1694a5d661aSToomas Soome expected_dhcpmsgtype = DHCPOFFER; 1704a5d661aSToomas Soome dhcp_ok = 0; 1714a5d661aSToomas Soome #endif 1724a5d661aSToomas Soome 1734a5d661aSToomas Soome if(sendrecv(d, 1744a5d661aSToomas Soome bootpsend, bp, sizeof(*bp), 1754a5d661aSToomas Soome bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp)) 1764a5d661aSToomas Soome == -1) { 1774a5d661aSToomas Soome printf("bootp: no reply\n"); 1784a5d661aSToomas Soome return; 1794a5d661aSToomas Soome } 1804a5d661aSToomas Soome 1814a5d661aSToomas Soome #ifdef SUPPORT_DHCP 1824a5d661aSToomas Soome if(dhcp_ok) { 1834a5d661aSToomas Soome u_int32_t leasetime; 1844a5d661aSToomas Soome bp->bp_vend[6] = DHCPREQUEST; 1854a5d661aSToomas Soome bp->bp_vend[7] = TAG_REQ_ADDR; 1864a5d661aSToomas Soome bp->bp_vend[8] = 4; 1874a5d661aSToomas Soome bcopy(&rbuf.rbootp.bp_yiaddr, &bp->bp_vend[9], 4); 1884a5d661aSToomas Soome bp->bp_vend[13] = TAG_SERVERID; 1894a5d661aSToomas Soome bp->bp_vend[14] = 4; 1904a5d661aSToomas Soome bcopy(&dhcp_serverip.s_addr, &bp->bp_vend[15], 4); 1914a5d661aSToomas Soome bp->bp_vend[19] = TAG_LEASETIME; 1924a5d661aSToomas Soome bp->bp_vend[20] = 4; 1934a5d661aSToomas Soome leasetime = htonl(300); 1944a5d661aSToomas Soome bcopy(&leasetime, &bp->bp_vend[21], 4); 1954a5d661aSToomas Soome if (flag & BOOTP_PXE) { 1964a5d661aSToomas Soome bp->bp_vend[25] = TAG_CLASSID; 1974a5d661aSToomas Soome bp->bp_vend[26] = 9; 1984a5d661aSToomas Soome bcopy("PXEClient", &bp->bp_vend[27], 9); 1994a5d661aSToomas Soome bp->bp_vend[36] = TAG_END; 2004a5d661aSToomas Soome } else 2014a5d661aSToomas Soome bp->bp_vend[25] = TAG_END; 2024a5d661aSToomas Soome 2034a5d661aSToomas Soome expected_dhcpmsgtype = DHCPACK; 2044a5d661aSToomas Soome 2054a5d661aSToomas Soome if(sendrecv(d, 2064a5d661aSToomas Soome bootpsend, bp, sizeof(*bp), 2074a5d661aSToomas Soome bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp)) 2084a5d661aSToomas Soome == -1) { 2094a5d661aSToomas Soome printf("DHCPREQUEST failed\n"); 2104a5d661aSToomas Soome return; 2114a5d661aSToomas Soome } 2124a5d661aSToomas Soome } 2134a5d661aSToomas Soome #endif 2144a5d661aSToomas Soome 2154a5d661aSToomas Soome myip = d->myip = rbuf.rbootp.bp_yiaddr; 2164a5d661aSToomas Soome servip = rbuf.rbootp.bp_siaddr; 2174a5d661aSToomas Soome if(rootip.s_addr == INADDR_ANY) rootip = servip; 2184a5d661aSToomas Soome bcopy(rbuf.rbootp.bp_file, bootfile, sizeof(bootfile)); 2194a5d661aSToomas Soome bootfile[sizeof(bootfile) - 1] = '\0'; 2204a5d661aSToomas Soome 2219f31b3deSToomas Soome if (!netmask) { 2224a5d661aSToomas Soome if (IN_CLASSA(ntohl(myip.s_addr))) 2239f31b3deSToomas Soome netmask = htonl(IN_CLASSA_NET); 2244a5d661aSToomas Soome else if (IN_CLASSB(ntohl(myip.s_addr))) 2259f31b3deSToomas Soome netmask = htonl(IN_CLASSB_NET); 2264a5d661aSToomas Soome else 2279f31b3deSToomas Soome netmask = htonl(IN_CLASSC_NET); 2284a5d661aSToomas Soome #ifdef BOOTP_DEBUG 2294a5d661aSToomas Soome if (debug) 2309f31b3deSToomas Soome printf("'native netmask' is %s\n", intoa(netmask)); 2314a5d661aSToomas Soome #endif 2324a5d661aSToomas Soome } 2334a5d661aSToomas Soome 2344a5d661aSToomas Soome #ifdef BOOTP_DEBUG 2354a5d661aSToomas Soome if (debug) 2364a5d661aSToomas Soome printf("mask: %s\n", intoa(netmask)); 2374a5d661aSToomas Soome #endif 2384a5d661aSToomas Soome 2394a5d661aSToomas Soome /* We need a gateway if root is on a different net */ 2404a5d661aSToomas Soome if (!SAMENET(myip, rootip, netmask)) { 2414a5d661aSToomas Soome #ifdef BOOTP_DEBUG 2424a5d661aSToomas Soome if (debug) 2434a5d661aSToomas Soome printf("need gateway for root ip\n"); 2444a5d661aSToomas Soome #endif 2454a5d661aSToomas Soome } 2464a5d661aSToomas Soome 2474a5d661aSToomas Soome /* Toss gateway if on a different net */ 2484a5d661aSToomas Soome if (!SAMENET(myip, gateip, netmask)) { 2494a5d661aSToomas Soome #ifdef BOOTP_DEBUG 2504a5d661aSToomas Soome if (debug) 2514a5d661aSToomas Soome printf("gateway ip (%s) bad\n", inet_ntoa(gateip)); 2524a5d661aSToomas Soome #endif 2534a5d661aSToomas Soome gateip.s_addr = 0; 2544a5d661aSToomas Soome } 2554a5d661aSToomas Soome 2564a5d661aSToomas Soome /* Bump xid so next request will be unique. */ 2574a5d661aSToomas Soome ++d->xid; 2584a5d661aSToomas Soome } 2594a5d661aSToomas Soome 2604a5d661aSToomas Soome /* Transmit a bootp request */ 2614a5d661aSToomas Soome static ssize_t 262e1bd2803SToomas Soome bootpsend(struct iodesc *d, void *pkt, size_t len) 2634a5d661aSToomas Soome { 2644a5d661aSToomas Soome struct bootp *bp; 2654a5d661aSToomas Soome 2664a5d661aSToomas Soome #ifdef BOOTP_DEBUG 2674a5d661aSToomas Soome if (debug) 2684a5d661aSToomas Soome printf("bootpsend: d=%lx called.\n", (long)d); 2694a5d661aSToomas Soome #endif 2704a5d661aSToomas Soome 2714a5d661aSToomas Soome bp = pkt; 2724a5d661aSToomas Soome bp->bp_secs = htons((u_short)(getsecs() - bot)); 2734a5d661aSToomas Soome 2744a5d661aSToomas Soome #ifdef BOOTP_DEBUG 2754a5d661aSToomas Soome if (debug) 2764a5d661aSToomas Soome printf("bootpsend: calling sendudp\n"); 2774a5d661aSToomas Soome #endif 2784a5d661aSToomas Soome 2794a5d661aSToomas Soome return (sendudp(d, pkt, len)); 2804a5d661aSToomas Soome } 2814a5d661aSToomas Soome 2824a5d661aSToomas Soome static ssize_t 283e1bd2803SToomas Soome bootprecv(struct iodesc *d, void *pkt, size_t len, time_t tleft) 2844a5d661aSToomas Soome { 2854a5d661aSToomas Soome ssize_t n; 2864a5d661aSToomas Soome struct bootp *bp; 2874a5d661aSToomas Soome 2884a5d661aSToomas Soome #ifdef BOOTP_DEBUGx 2894a5d661aSToomas Soome if (debug) 2904a5d661aSToomas Soome printf("bootp_recvoffer: called\n"); 2914a5d661aSToomas Soome #endif 2924a5d661aSToomas Soome 2934a5d661aSToomas Soome n = readudp(d, pkt, len, tleft); 2944a5d661aSToomas Soome if (n == -1 || n < sizeof(struct bootp) - BOOTP_VENDSIZE) 2954a5d661aSToomas Soome goto bad; 2964a5d661aSToomas Soome 2974a5d661aSToomas Soome bp = (struct bootp *)pkt; 2984a5d661aSToomas Soome 2994a5d661aSToomas Soome #ifdef BOOTP_DEBUG 3004a5d661aSToomas Soome if (debug) 3014a5d661aSToomas Soome printf("bootprecv: checked. bp = 0x%lx, n = %d\n", 3024a5d661aSToomas Soome (long)bp, (int)n); 3034a5d661aSToomas Soome #endif 3044a5d661aSToomas Soome if (bp->bp_xid != htonl(d->xid)) { 3054a5d661aSToomas Soome #ifdef BOOTP_DEBUG 3064a5d661aSToomas Soome if (debug) { 3074a5d661aSToomas Soome printf("bootprecv: expected xid 0x%lx, got 0x%x\n", 3084a5d661aSToomas Soome d->xid, ntohl(bp->bp_xid)); 3094a5d661aSToomas Soome } 3104a5d661aSToomas Soome #endif 3114a5d661aSToomas Soome goto bad; 3124a5d661aSToomas Soome } 3134a5d661aSToomas Soome 3144a5d661aSToomas Soome #ifdef BOOTP_DEBUG 3154a5d661aSToomas Soome if (debug) 3164a5d661aSToomas Soome printf("bootprecv: got one!\n"); 3174a5d661aSToomas Soome #endif 3184a5d661aSToomas Soome 3194a5d661aSToomas Soome /* Suck out vendor info */ 3204a5d661aSToomas Soome if (bcmp(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)) == 0) { 3214a5d661aSToomas Soome if(vend_rfc1048(bp->bp_vend, sizeof(bp->bp_vend)) != 0) 3224a5d661aSToomas Soome goto bad; 3234a5d661aSToomas Soome } 3244a5d661aSToomas Soome #ifdef BOOTP_VEND_CMU 3254a5d661aSToomas Soome else if (bcmp(vm_cmu, bp->bp_vend, sizeof(vm_cmu)) == 0) 3264a5d661aSToomas Soome vend_cmu(bp->bp_vend); 3274a5d661aSToomas Soome #endif 3284a5d661aSToomas Soome else 3294a5d661aSToomas Soome printf("bootprecv: unknown vendor 0x%lx\n", (long)bp->bp_vend); 3304a5d661aSToomas Soome 3314a5d661aSToomas Soome return(n); 3324a5d661aSToomas Soome bad: 3334a5d661aSToomas Soome errno = 0; 3344a5d661aSToomas Soome return (-1); 3354a5d661aSToomas Soome } 3364a5d661aSToomas Soome 3377b0ff4b9SToomas Soome int 3387b0ff4b9SToomas Soome dhcp_try_rfc1048(uint8_t *cp, size_t len) 3397b0ff4b9SToomas Soome { 3407b0ff4b9SToomas Soome 3417b0ff4b9SToomas Soome expected_dhcpmsgtype = DHCPACK; 3427b0ff4b9SToomas Soome if (bcmp(vm_rfc1048, cp, sizeof (vm_rfc1048)) == 0) { 3437b0ff4b9SToomas Soome return (vend_rfc1048(cp, len)); 3447b0ff4b9SToomas Soome } 3457b0ff4b9SToomas Soome return (-1); 3467b0ff4b9SToomas Soome } 3477b0ff4b9SToomas Soome 3484a5d661aSToomas Soome static int 349e1bd2803SToomas Soome vend_rfc1048(u_char *cp, u_int len) 3504a5d661aSToomas Soome { 3514a5d661aSToomas Soome u_char *ep; 3524a5d661aSToomas Soome int size; 3534a5d661aSToomas Soome u_char tag; 3544a5d661aSToomas Soome const char *val; 3554a5d661aSToomas Soome 3564a5d661aSToomas Soome #ifdef BOOTP_DEBUG 3574a5d661aSToomas Soome if (debug) 3584a5d661aSToomas Soome printf("vend_rfc1048 bootp info. len=%d\n", len); 3594a5d661aSToomas Soome #endif 3604a5d661aSToomas Soome ep = cp + len; 3614a5d661aSToomas Soome 3624a5d661aSToomas Soome /* Step over magic cookie */ 3634a5d661aSToomas Soome cp += sizeof(int); 3644a5d661aSToomas Soome 3654a5d661aSToomas Soome setenv_(cp, ep, NULL); 3664a5d661aSToomas Soome 3674a5d661aSToomas Soome while (cp < ep) { 3684a5d661aSToomas Soome tag = *cp++; 3694a5d661aSToomas Soome size = *cp++; 3704a5d661aSToomas Soome if (tag == TAG_END) 3714a5d661aSToomas Soome break; 3724a5d661aSToomas Soome 3734a5d661aSToomas Soome if (tag == TAG_SUBNET_MASK) { 3749f31b3deSToomas Soome bcopy(cp, &netmask, sizeof(netmask)); 3754a5d661aSToomas Soome } 3764a5d661aSToomas Soome if (tag == TAG_GATEWAY) { 3774a5d661aSToomas Soome bcopy(cp, &gateip.s_addr, sizeof(gateip.s_addr)); 3784a5d661aSToomas Soome } 3794a5d661aSToomas Soome if (tag == TAG_SWAPSERVER) { 3804a5d661aSToomas Soome /* let it override bp_siaddr */ 3814a5d661aSToomas Soome bcopy(cp, &rootip.s_addr, sizeof(rootip.s_addr)); 3824a5d661aSToomas Soome } 3834a5d661aSToomas Soome if (tag == TAG_ROOTPATH) { 3844a5d661aSToomas Soome if ((val = getenv("dhcp.root-path")) == NULL) 3854a5d661aSToomas Soome val = (const char *)cp; 3864a5d661aSToomas Soome strlcpy(rootpath, val, sizeof(rootpath)); 3874a5d661aSToomas Soome } 3884a5d661aSToomas Soome if (tag == TAG_HOSTNAME) { 3894a5d661aSToomas Soome if ((val = getenv("dhcp.host-name")) == NULL) 3904a5d661aSToomas Soome val = (const char *)cp; 3914a5d661aSToomas Soome strlcpy(hostname, val, sizeof(hostname)); 3924a5d661aSToomas Soome } 393e1bd2803SToomas Soome if (tag == TAG_INTF_MTU) { 394e1bd2803SToomas Soome intf_mtu = 0; 395e1bd2803SToomas Soome if ((val = getenv("dhcp.interface-mtu")) != NULL) { 396e1bd2803SToomas Soome unsigned long tmp; 397e1bd2803SToomas Soome char *end; 398e1bd2803SToomas Soome 399e1bd2803SToomas Soome errno = 0; 400e1bd2803SToomas Soome /* 401e1bd2803SToomas Soome * Do not allow MTU to exceed max IPv4 packet 402e1bd2803SToomas Soome * size, max value of 16-bit word. 403e1bd2803SToomas Soome */ 404e1bd2803SToomas Soome tmp = strtoul(val, &end, 0); 405e1bd2803SToomas Soome if (errno != 0 || 406e1bd2803SToomas Soome *val == '\0' || *end != '\0' || 407e1bd2803SToomas Soome tmp > USHRT_MAX) { 408e1bd2803SToomas Soome printf("%s: bad value: \"%s\", " 409e1bd2803SToomas Soome "ignoring\n", 410e1bd2803SToomas Soome "dhcp.interface-mtu", val); 411e1bd2803SToomas Soome } else { 412e1bd2803SToomas Soome intf_mtu = (u_int)tmp; 413e1bd2803SToomas Soome } 414e1bd2803SToomas Soome } 415e1bd2803SToomas Soome if (intf_mtu == 0) 416e1bd2803SToomas Soome intf_mtu = be16dec(cp); 417e1bd2803SToomas Soome } 4184a5d661aSToomas Soome #ifdef SUPPORT_DHCP 4194a5d661aSToomas Soome if (tag == TAG_DHCP_MSGTYPE) { 4204a5d661aSToomas Soome if(*cp != expected_dhcpmsgtype) 4214a5d661aSToomas Soome return(-1); 4224a5d661aSToomas Soome dhcp_ok = 1; 4234a5d661aSToomas Soome } 4244a5d661aSToomas Soome if (tag == TAG_SERVERID) { 4254a5d661aSToomas Soome bcopy(cp, &dhcp_serverip.s_addr, 4264a5d661aSToomas Soome sizeof(dhcp_serverip.s_addr)); 4274a5d661aSToomas Soome } 4284a5d661aSToomas Soome #endif 4294a5d661aSToomas Soome cp += size; 4304a5d661aSToomas Soome } 4314a5d661aSToomas Soome return(0); 4324a5d661aSToomas Soome } 4334a5d661aSToomas Soome 4344a5d661aSToomas Soome #ifdef BOOTP_VEND_CMU 4354a5d661aSToomas Soome static void 436e1bd2803SToomas Soome vend_cmu(u_char *cp) 4374a5d661aSToomas Soome { 4384a5d661aSToomas Soome struct cmu_vend *vp; 4394a5d661aSToomas Soome 4404a5d661aSToomas Soome #ifdef BOOTP_DEBUG 4414a5d661aSToomas Soome if (debug) 4424a5d661aSToomas Soome printf("vend_cmu bootp info.\n"); 4434a5d661aSToomas Soome #endif 4444a5d661aSToomas Soome vp = (struct cmu_vend *)cp; 4454a5d661aSToomas Soome 4464a5d661aSToomas Soome if (vp->v_smask.s_addr != 0) { 4479f31b3deSToomas Soome netmask = vp->v_smask.s_addr; 4484a5d661aSToomas Soome } 4494a5d661aSToomas Soome if (vp->v_dgate.s_addr != 0) { 4504a5d661aSToomas Soome gateip = vp->v_dgate; 4514a5d661aSToomas Soome } 4524a5d661aSToomas Soome } 4534a5d661aSToomas Soome #endif 4544a5d661aSToomas Soome 4554a5d661aSToomas Soome #ifdef DHCP_ENV 4564a5d661aSToomas Soome /* 4574a5d661aSToomas Soome * Parse DHCP options and store them into kenv variables. 4584a5d661aSToomas Soome * Original code from Danny Braniss, modifications by Luigi Rizzo. 4594a5d661aSToomas Soome * 4604a5d661aSToomas Soome * The parser is driven by tables which specify the type and name of 4614a5d661aSToomas Soome * each dhcp option and how it appears in kenv. 4624a5d661aSToomas Soome * The first entry in the list contains the prefix used to set the kenv 4634a5d661aSToomas Soome * name (including the . if needed), the last entry must have a 0 tag. 4644a5d661aSToomas Soome * Entries do not need to be sorted though it helps for readability. 4654a5d661aSToomas Soome * 4664a5d661aSToomas Soome * Certain vendor-specific tables can be enabled according to DHCP_ENV. 4674a5d661aSToomas Soome * Set it to 0 if you don't want any. 4684a5d661aSToomas Soome */ 4694a5d661aSToomas Soome enum opt_fmt { __NONE = 0, 4704a5d661aSToomas Soome __8 = 1, __16 = 2, __32 = 4, /* Unsigned fields, value=size */ 4714a5d661aSToomas Soome __IP, /* IPv4 address */ 4724a5d661aSToomas Soome __TXT, /* C string */ 4734a5d661aSToomas Soome __BYTES, /* byte sequence, printed %02x */ 4744a5d661aSToomas Soome __INDIR, /* name=value */ 4754a5d661aSToomas Soome __ILIST, /* name=value;name=value ... */ 4764a5d661aSToomas Soome __VE, /* vendor specific, recurse */ 4774a5d661aSToomas Soome }; 4784a5d661aSToomas Soome 4794a5d661aSToomas Soome struct dhcp_opt { 4804a5d661aSToomas Soome uint8_t tag; 4814a5d661aSToomas Soome uint8_t fmt; 4824a5d661aSToomas Soome const char *desc; 4834a5d661aSToomas Soome }; 4844a5d661aSToomas Soome 4854a5d661aSToomas Soome static struct dhcp_opt vndr_opt[] = { /* Vendor Specific Options */ 4864a5d661aSToomas Soome #if DHCP_ENV == DHCP_ENV_FREEBSD /* FreeBSD table in the original code */ 4874a5d661aSToomas Soome {0, 0, "FreeBSD"}, /* prefix */ 4884a5d661aSToomas Soome {1, __TXT, "kernel"}, 4894a5d661aSToomas Soome {2, __TXT, "kernelname"}, 4904a5d661aSToomas Soome {3, __TXT, "kernel_options"}, 4914a5d661aSToomas Soome {4, __IP, "usr-ip"}, 4924a5d661aSToomas Soome {5, __TXT, "conf-path"}, 4934a5d661aSToomas Soome {6, __TXT, "rc.conf0"}, 4944a5d661aSToomas Soome {7, __TXT, "rc.conf1"}, 4954a5d661aSToomas Soome {8, __TXT, "rc.conf2"}, 4964a5d661aSToomas Soome {9, __TXT, "rc.conf3"}, 4974a5d661aSToomas Soome {10, __TXT, "rc.conf4"}, 4984a5d661aSToomas Soome {11, __TXT, "rc.conf5"}, 4994a5d661aSToomas Soome {12, __TXT, "rc.conf6"}, 5004a5d661aSToomas Soome {13, __TXT, "rc.conf7"}, 5014a5d661aSToomas Soome {14, __TXT, "rc.conf8"}, 5024a5d661aSToomas Soome {15, __TXT, "rc.conf9"}, 5034a5d661aSToomas Soome 5044a5d661aSToomas Soome {20, __TXT, "boot.nfsroot.options"}, 5054a5d661aSToomas Soome 5064a5d661aSToomas Soome {245, __INDIR, ""}, 5074a5d661aSToomas Soome {246, __INDIR, ""}, 5084a5d661aSToomas Soome {247, __INDIR, ""}, 5094a5d661aSToomas Soome {248, __INDIR, ""}, 5104a5d661aSToomas Soome {249, __INDIR, ""}, 5114a5d661aSToomas Soome {250, __INDIR, ""}, 5124a5d661aSToomas Soome {251, __INDIR, ""}, 5134a5d661aSToomas Soome {252, __INDIR, ""}, 5144a5d661aSToomas Soome {253, __INDIR, ""}, 5154a5d661aSToomas Soome {254, __INDIR, ""}, 5164a5d661aSToomas Soome 5174a5d661aSToomas Soome #elif DHCP_ENV == DHCP_ENV_PXE /* some pxe options, RFC4578 */ 5184a5d661aSToomas Soome {0, 0, "pxe"}, /* prefix */ 5194a5d661aSToomas Soome {93, __16, "system-architecture"}, 5204a5d661aSToomas Soome {94, __BYTES, "network-interface"}, 5214a5d661aSToomas Soome {97, __BYTES, "machine-identifier"}, 5224a5d661aSToomas Soome #else /* default (empty) table */ 5234a5d661aSToomas Soome {0, 0, "dhcp.vendor."}, /* prefix */ 5244a5d661aSToomas Soome #endif 5254a5d661aSToomas Soome {0, __TXT, "%soption-%d"} 5264a5d661aSToomas Soome }; 5274a5d661aSToomas Soome 5284a5d661aSToomas Soome static struct dhcp_opt dhcp_opt[] = { 5294a5d661aSToomas Soome /* DHCP Option names, formats and codes, from RFC2132. */ 5304a5d661aSToomas Soome {0, 0, "dhcp."}, // prefix 5314a5d661aSToomas Soome {1, __IP, "subnet-mask"}, 5324a5d661aSToomas Soome {2, __32, "time-offset"}, /* this is signed */ 5334a5d661aSToomas Soome {3, __IP, "routers"}, 5344a5d661aSToomas Soome {4, __IP, "time-servers"}, 5354a5d661aSToomas Soome {5, __IP, "ien116-name-servers"}, 5364a5d661aSToomas Soome {6, __IP, "domain-name-servers"}, 5374a5d661aSToomas Soome {7, __IP, "log-servers"}, 5384a5d661aSToomas Soome {8, __IP, "cookie-servers"}, 5394a5d661aSToomas Soome {9, __IP, "lpr-servers"}, 5404a5d661aSToomas Soome {10, __IP, "impress-servers"}, 5414a5d661aSToomas Soome {11, __IP, "resource-location-servers"}, 5424a5d661aSToomas Soome {12, __TXT, "host-name"}, 5434a5d661aSToomas Soome {13, __16, "boot-size"}, 5444a5d661aSToomas Soome {14, __TXT, "merit-dump"}, 5454a5d661aSToomas Soome {15, __TXT, "domain-name"}, 5464a5d661aSToomas Soome {16, __IP, "swap-server"}, 5474a5d661aSToomas Soome {17, __TXT, "root-path"}, 5484a5d661aSToomas Soome {18, __TXT, "extensions-path"}, 5494a5d661aSToomas Soome {19, __8, "ip-forwarding"}, 5504a5d661aSToomas Soome {20, __8, "non-local-source-routing"}, 5514a5d661aSToomas Soome {21, __IP, "policy-filter"}, 5524a5d661aSToomas Soome {22, __16, "max-dgram-reassembly"}, 5534a5d661aSToomas Soome {23, __8, "default-ip-ttl"}, 5544a5d661aSToomas Soome {24, __32, "path-mtu-aging-timeout"}, 5554a5d661aSToomas Soome {25, __16, "path-mtu-plateau-table"}, 5564a5d661aSToomas Soome {26, __16, "interface-mtu"}, 5574a5d661aSToomas Soome {27, __8, "all-subnets-local"}, 5584a5d661aSToomas Soome {28, __IP, "broadcast-address"}, 5594a5d661aSToomas Soome {29, __8, "perform-mask-discovery"}, 5604a5d661aSToomas Soome {30, __8, "mask-supplier"}, 5614a5d661aSToomas Soome {31, __8, "perform-router-discovery"}, 5624a5d661aSToomas Soome {32, __IP, "router-solicitation-address"}, 5634a5d661aSToomas Soome {33, __IP, "static-routes"}, 5644a5d661aSToomas Soome {34, __8, "trailer-encapsulation"}, 5654a5d661aSToomas Soome {35, __32, "arp-cache-timeout"}, 5664a5d661aSToomas Soome {36, __8, "ieee802-3-encapsulation"}, 5674a5d661aSToomas Soome {37, __8, "default-tcp-ttl"}, 5684a5d661aSToomas Soome {38, __32, "tcp-keepalive-interval"}, 5694a5d661aSToomas Soome {39, __8, "tcp-keepalive-garbage"}, 5704a5d661aSToomas Soome {40, __TXT, "nis-domain"}, 5714a5d661aSToomas Soome {41, __IP, "nis-servers"}, 5724a5d661aSToomas Soome {42, __IP, "ntp-servers"}, 5734a5d661aSToomas Soome {43, __VE, "vendor-encapsulated-options"}, 5744a5d661aSToomas Soome {44, __IP, "netbios-name-servers"}, 5754a5d661aSToomas Soome {45, __IP, "netbios-dd-server"}, 5764a5d661aSToomas Soome {46, __8, "netbios-node-type"}, 5774a5d661aSToomas Soome {47, __TXT, "netbios-scope"}, 5784a5d661aSToomas Soome {48, __IP, "x-font-servers"}, 5794a5d661aSToomas Soome {49, __IP, "x-display-managers"}, 5804a5d661aSToomas Soome {50, __IP, "dhcp-requested-address"}, 5814a5d661aSToomas Soome {51, __32, "dhcp-lease-time"}, 5824a5d661aSToomas Soome {52, __8, "dhcp-option-overload"}, 5834a5d661aSToomas Soome {53, __8, "dhcp-message-type"}, 5844a5d661aSToomas Soome {54, __IP, "dhcp-server-identifier"}, 5854a5d661aSToomas Soome {55, __8, "dhcp-parameter-request-list"}, 5864a5d661aSToomas Soome {56, __TXT, "dhcp-message"}, 5874a5d661aSToomas Soome {57, __16, "dhcp-max-message-size"}, 5884a5d661aSToomas Soome {58, __32, "dhcp-renewal-time"}, 5894a5d661aSToomas Soome {59, __32, "dhcp-rebinding-time"}, 5904a5d661aSToomas Soome {60, __TXT, "vendor-class-identifier"}, 5914a5d661aSToomas Soome {61, __TXT, "dhcp-client-identifier"}, 5924a5d661aSToomas Soome {64, __TXT, "nisplus-domain"}, 5934a5d661aSToomas Soome {65, __IP, "nisplus-servers"}, 5944a5d661aSToomas Soome {66, __TXT, "tftp-server-name"}, 5954a5d661aSToomas Soome {67, __TXT, "bootfile-name"}, 5964a5d661aSToomas Soome {68, __IP, "mobile-ip-home-agent"}, 5974a5d661aSToomas Soome {69, __IP, "smtp-server"}, 5984a5d661aSToomas Soome {70, __IP, "pop-server"}, 5994a5d661aSToomas Soome {71, __IP, "nntp-server"}, 6004a5d661aSToomas Soome {72, __IP, "www-server"}, 6014a5d661aSToomas Soome {73, __IP, "finger-server"}, 6024a5d661aSToomas Soome {74, __IP, "irc-server"}, 6034a5d661aSToomas Soome {75, __IP, "streettalk-server"}, 6044a5d661aSToomas Soome {76, __IP, "streettalk-directory-assistance-server"}, 6054a5d661aSToomas Soome {77, __TXT, "user-class"}, 6064a5d661aSToomas Soome {85, __IP, "nds-servers"}, 6074a5d661aSToomas Soome {86, __TXT, "nds-tree-name"}, 6084a5d661aSToomas Soome {87, __TXT, "nds-context"}, 6094a5d661aSToomas Soome {210, __TXT, "authenticate"}, 6104a5d661aSToomas Soome 6114a5d661aSToomas Soome /* use the following entries for arbitrary variables */ 6124a5d661aSToomas Soome {246, __ILIST, ""}, 6134a5d661aSToomas Soome {247, __ILIST, ""}, 6144a5d661aSToomas Soome {248, __ILIST, ""}, 6154a5d661aSToomas Soome {249, __ILIST, ""}, 6164a5d661aSToomas Soome {250, __INDIR, ""}, 6174a5d661aSToomas Soome {251, __INDIR, ""}, 6184a5d661aSToomas Soome {252, __INDIR, ""}, 6194a5d661aSToomas Soome {253, __INDIR, ""}, 6204a5d661aSToomas Soome {254, __INDIR, ""}, 6214a5d661aSToomas Soome {0, __TXT, "%soption-%d"} 6224a5d661aSToomas Soome }; 6234a5d661aSToomas Soome 6244a5d661aSToomas Soome /* 6254a5d661aSToomas Soome * parse a dhcp response, set environment variables translating options 6264a5d661aSToomas Soome * names and values according to the tables above. Also set dhcp.tags 6274a5d661aSToomas Soome * to the list of selected tags. 6284a5d661aSToomas Soome */ 6294a5d661aSToomas Soome static void 6304a5d661aSToomas Soome setenv_(u_char *cp, u_char *ep, struct dhcp_opt *opts) 6314a5d661aSToomas Soome { 6324a5d661aSToomas Soome u_char *ncp; 6334a5d661aSToomas Soome u_char tag; 6344a5d661aSToomas Soome char tags[512], *tp; /* the list of tags */ 6354a5d661aSToomas Soome 6364a5d661aSToomas Soome #define FLD_SEP ',' /* separator in list of elements */ 6374a5d661aSToomas Soome ncp = cp; 6384a5d661aSToomas Soome tp = tags; 6394a5d661aSToomas Soome if (opts == NULL) 6404a5d661aSToomas Soome opts = dhcp_opt; 6414a5d661aSToomas Soome 6424a5d661aSToomas Soome while (ncp < ep) { 6434a5d661aSToomas Soome unsigned int size; /* option size */ 6444a5d661aSToomas Soome char *vp, *endv, buf[256]; /* the value buffer */ 6454a5d661aSToomas Soome struct dhcp_opt *op; 6464a5d661aSToomas Soome 6474a5d661aSToomas Soome tag = *ncp++; /* extract tag and size */ 6484a5d661aSToomas Soome size = *ncp++; 6494a5d661aSToomas Soome cp = ncp; /* current payload */ 6504a5d661aSToomas Soome ncp += size; /* point to the next option */ 6514a5d661aSToomas Soome 6524a5d661aSToomas Soome if (tag == TAG_END) 6534a5d661aSToomas Soome break; 6544a5d661aSToomas Soome if (tag == 0) 6554a5d661aSToomas Soome continue; 6564a5d661aSToomas Soome 6574a5d661aSToomas Soome for (op = opts+1; op->tag && op->tag != tag; op++) 6584a5d661aSToomas Soome ; 6594a5d661aSToomas Soome /* if not found we end up on the default entry */ 6604a5d661aSToomas Soome 6614a5d661aSToomas Soome /* 6624a5d661aSToomas Soome * Copy data into the buffer. libstand does not have snprintf so we 6634a5d661aSToomas Soome * need to be careful with sprintf(). With strings, the source is 6644a5d661aSToomas Soome * always <256 char so shorter than the buffer so we are safe; with 6654a5d661aSToomas Soome * other arguments, the longest string is inet_ntoa which is 16 bytes 6664a5d661aSToomas Soome * so we make sure to have always enough room in the string before 6674a5d661aSToomas Soome * trying an sprint. 6684a5d661aSToomas Soome */ 6694a5d661aSToomas Soome vp = buf; 6704a5d661aSToomas Soome *vp = '\0'; 6714a5d661aSToomas Soome endv = buf + sizeof(buf) - 1 - 16; /* last valid write position */ 6724a5d661aSToomas Soome 6734a5d661aSToomas Soome switch(op->fmt) { 6744a5d661aSToomas Soome case __NONE: 6754a5d661aSToomas Soome break; /* should not happen */ 6764a5d661aSToomas Soome 6774a5d661aSToomas Soome case __VE: /* recurse, vendor specific */ 6784a5d661aSToomas Soome setenv_(cp, cp+size, vndr_opt); 6794a5d661aSToomas Soome break; 6804a5d661aSToomas Soome 6814a5d661aSToomas Soome case __IP: /* ip address */ 6824a5d661aSToomas Soome for (; size > 0 && vp < endv; size -= 4, cp += 4) { 6834a5d661aSToomas Soome struct in_addr in_ip; /* ip addresses */ 6844a5d661aSToomas Soome if (vp != buf) 6854a5d661aSToomas Soome *vp++ = FLD_SEP; 6864a5d661aSToomas Soome bcopy(cp, &in_ip.s_addr, sizeof(in_ip.s_addr)); 6874a5d661aSToomas Soome sprintf(vp, "%s", inet_ntoa(in_ip)); 6884a5d661aSToomas Soome vp += strlen(vp); 6894a5d661aSToomas Soome } 6904a5d661aSToomas Soome break; 6914a5d661aSToomas Soome 6924a5d661aSToomas Soome case __BYTES: /* opaque byte string */ 6934a5d661aSToomas Soome for (; size > 0 && vp < endv; size -= 1, cp += 1) { 6944a5d661aSToomas Soome sprintf(vp, "%02x", *cp); 6954a5d661aSToomas Soome vp += strlen(vp); 6964a5d661aSToomas Soome } 6974a5d661aSToomas Soome break; 6984a5d661aSToomas Soome 6994a5d661aSToomas Soome case __TXT: 7004a5d661aSToomas Soome bcopy(cp, buf, size); /* cannot overflow */ 7014a5d661aSToomas Soome buf[size] = 0; 7024a5d661aSToomas Soome break; 7034a5d661aSToomas Soome 7044a5d661aSToomas Soome case __32: 7054a5d661aSToomas Soome case __16: 7064a5d661aSToomas Soome case __8: /* op->fmt is also the length of each field */ 7074a5d661aSToomas Soome for (; size > 0 && vp < endv; size -= op->fmt, cp += op->fmt) { 7084a5d661aSToomas Soome uint32_t v; 7094a5d661aSToomas Soome if (op->fmt == __32) 7104a5d661aSToomas Soome v = (cp[0]<<24) + (cp[1]<<16) + (cp[2]<<8) + cp[3]; 7114a5d661aSToomas Soome else if (op->fmt == __16) 7124a5d661aSToomas Soome v = (cp[0]<<8) + cp[1]; 7134a5d661aSToomas Soome else 7144a5d661aSToomas Soome v = cp[0]; 7154a5d661aSToomas Soome if (vp != buf) 7164a5d661aSToomas Soome *vp++ = FLD_SEP; 7174a5d661aSToomas Soome sprintf(vp, "%u", v); 7184a5d661aSToomas Soome vp += strlen(vp); 7194a5d661aSToomas Soome } 7204a5d661aSToomas Soome break; 7214a5d661aSToomas Soome 7224a5d661aSToomas Soome case __INDIR: /* name=value */ 7234a5d661aSToomas Soome case __ILIST: /* name=value;name=value... */ 7244a5d661aSToomas Soome bcopy(cp, buf, size); /* cannot overflow */ 7254a5d661aSToomas Soome buf[size] = '\0'; 7264a5d661aSToomas Soome for (endv = buf; endv; endv = vp) { 7274a5d661aSToomas Soome u_char *s = NULL; /* semicolon ? */ 7284a5d661aSToomas Soome 7294a5d661aSToomas Soome /* skip leading whitespace */ 7304a5d661aSToomas Soome while (*endv && strchr(" \t\n\r", *endv)) 7314a5d661aSToomas Soome endv++; 7324a5d661aSToomas Soome vp = strchr(endv, '='); /* find name=value separator */ 7334a5d661aSToomas Soome if (!vp) 7344a5d661aSToomas Soome break; 7354a5d661aSToomas Soome *vp++ = 0; 7364a5d661aSToomas Soome if (op->fmt == __ILIST && (s = strchr(vp, ';'))) 7374a5d661aSToomas Soome *s++ = '\0'; 7384a5d661aSToomas Soome setenv(endv, vp, 1); 7394a5d661aSToomas Soome vp = s; /* prepare for next round */ 7404a5d661aSToomas Soome } 7414a5d661aSToomas Soome buf[0] = '\0'; /* option already done */ 7424a5d661aSToomas Soome } 7434a5d661aSToomas Soome 7444a5d661aSToomas Soome if (tp - tags < sizeof(tags) - 5) { /* add tag to the list */ 7454a5d661aSToomas Soome if (tp != tags) 7464a5d661aSToomas Soome *tp++ = FLD_SEP; 7474a5d661aSToomas Soome sprintf(tp, "%d", tag); 7484a5d661aSToomas Soome tp += strlen(tp); 7494a5d661aSToomas Soome } 7504a5d661aSToomas Soome if (buf[0]) { 7514a5d661aSToomas Soome char env[128]; /* the string name */ 7524a5d661aSToomas Soome 7534a5d661aSToomas Soome if (op->tag == 0) 7544a5d661aSToomas Soome sprintf(env, op->desc, opts[0].desc, tag); 7554a5d661aSToomas Soome else 7564a5d661aSToomas Soome sprintf(env, "%s%s", opts[0].desc, op->desc); 7574a5d661aSToomas Soome /* 7584a5d661aSToomas Soome * Do not replace existing values in the environment, so that 7594a5d661aSToomas Soome * locally-obtained values can override server-provided values. 7604a5d661aSToomas Soome */ 7614a5d661aSToomas Soome setenv(env, buf, 0); 7624a5d661aSToomas Soome } 7634a5d661aSToomas Soome } 7644a5d661aSToomas Soome if (tp != tags) { 7654a5d661aSToomas Soome char env[128]; /* the string name */ 7664a5d661aSToomas Soome sprintf(env, "%stags", opts[0].desc); 7674a5d661aSToomas Soome setenv(env, tags, 1); 7684a5d661aSToomas Soome } 7694a5d661aSToomas Soome } 7704a5d661aSToomas Soome #endif /* additional dhcp */ 771