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