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