xref: /titanic_52/usr/src/boot/lib/libstand/bootp.c (revision be960ecca44c283ac7875e6d2f1746056298cedf)
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.
17*be960eccSToomas 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 
914a5d661aSToomas Soome /* Fetch required bootp infomation */
924a5d661aSToomas Soome void
93e1bd2803SToomas Soome bootp(int sock, int flag)
944a5d661aSToomas Soome {
957b2a1233SToomas Soome 	void *pkt;
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;
1027b2a1233SToomas Soome 	struct bootp *rbootp;
1034a5d661aSToomas Soome 
1044a5d661aSToomas Soome #ifdef BOOTP_DEBUG
1054a5d661aSToomas Soome  	if (debug)
1064a5d661aSToomas Soome 		printf("bootp: socket=%d\n", sock);
1074a5d661aSToomas Soome #endif
1084a5d661aSToomas Soome 	if (!bot)
1094a5d661aSToomas Soome 		bot = getsecs();
1104a5d661aSToomas Soome 
1114a5d661aSToomas Soome 	if (!(d = socktodesc(sock))) {
1124a5d661aSToomas Soome 		printf("bootp: bad socket. %d\n", sock);
1134a5d661aSToomas Soome 		return;
1144a5d661aSToomas Soome 	}
1154a5d661aSToomas Soome #ifdef BOOTP_DEBUG
1164a5d661aSToomas Soome  	if (debug)
1174a5d661aSToomas Soome 		printf("bootp: d=%lx\n", (long)d);
1184a5d661aSToomas Soome #endif
1194a5d661aSToomas Soome 
1204a5d661aSToomas Soome 	bp = &wbuf.wbootp;
1214a5d661aSToomas Soome 	bzero(bp, sizeof(*bp));
1224a5d661aSToomas Soome 
1234a5d661aSToomas Soome 	bp->bp_op = BOOTREQUEST;
1244a5d661aSToomas Soome 	bp->bp_htype = 1;		/* 10Mb Ethernet (48 bits) */
1254a5d661aSToomas Soome 	bp->bp_hlen = 6;
1264a5d661aSToomas Soome 	bp->bp_xid = htonl(d->xid);
1274a5d661aSToomas Soome 	MACPY(d->myea, bp->bp_chaddr);
1284a5d661aSToomas Soome 	strncpy(bp->bp_file, bootfile, sizeof(bp->bp_file));
1294a5d661aSToomas Soome 	bcopy(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048));
1304a5d661aSToomas Soome #ifdef SUPPORT_DHCP
1314a5d661aSToomas Soome 	bp->bp_vend[4] = TAG_DHCP_MSGTYPE;
1324a5d661aSToomas Soome 	bp->bp_vend[5] = 1;
1334a5d661aSToomas Soome 	bp->bp_vend[6] = DHCPDISCOVER;
1344a5d661aSToomas Soome 
1354a5d661aSToomas Soome 	/*
1364a5d661aSToomas Soome 	 * If we are booting from PXE, we want to send the string
1374a5d661aSToomas Soome 	 * 'PXEClient' to the DHCP server so you have the option of
1384a5d661aSToomas Soome 	 * only responding to PXE aware dhcp requests.
1394a5d661aSToomas Soome 	 */
1404a5d661aSToomas Soome 	if (flag & BOOTP_PXE) {
1414a5d661aSToomas Soome 		bp->bp_vend[7] = TAG_CLASSID;
1424a5d661aSToomas Soome 		bp->bp_vend[8] = 9;
1434a5d661aSToomas Soome 		bcopy("PXEClient", &bp->bp_vend[9], 9);
144*be960eccSToomas Soome 		bp->bp_vend[18] = TAG_USER_CLASS;
145*be960eccSToomas Soome 		/* len of each user class + number of user class */
146*be960eccSToomas Soome 		bp->bp_vend[19] = 8;
147*be960eccSToomas Soome 		/* len of the first user class */
148*be960eccSToomas Soome 		bp->bp_vend[20] = 7;
149*be960eccSToomas Soome 		bcopy("illumos", &bp->bp_vend[21], 7);
150*be960eccSToomas Soome 		bp->bp_vend[28] = TAG_PARAM_REQ;
151*be960eccSToomas Soome 		bp->bp_vend[29] = 7;
152*be960eccSToomas Soome 		bp->bp_vend[30] = TAG_ROOTPATH;
153*be960eccSToomas Soome 		bp->bp_vend[31] = TAG_HOSTNAME;
154*be960eccSToomas Soome 		bp->bp_vend[32] = TAG_SWAPSERVER;
155*be960eccSToomas Soome 		bp->bp_vend[33] = TAG_GATEWAY;
156*be960eccSToomas Soome 		bp->bp_vend[34] = TAG_SUBNET_MASK;
157*be960eccSToomas Soome 		bp->bp_vend[35] = TAG_INTF_MTU;
158*be960eccSToomas Soome 		bp->bp_vend[36] = TAG_SERVERID;
159*be960eccSToomas Soome 		bp->bp_vend[37] = TAG_END;
1604a5d661aSToomas Soome 	} else
1614a5d661aSToomas Soome 		bp->bp_vend[7] = TAG_END;
1624a5d661aSToomas Soome #else
1634a5d661aSToomas Soome 	bp->bp_vend[4] = TAG_END;
1644a5d661aSToomas Soome #endif
1654a5d661aSToomas Soome 
1664a5d661aSToomas Soome 	d->myip.s_addr = INADDR_ANY;
1674a5d661aSToomas Soome 	d->myport = htons(IPPORT_BOOTPC);
1684a5d661aSToomas Soome 	d->destip.s_addr = INADDR_BROADCAST;
1694a5d661aSToomas Soome 	d->destport = htons(IPPORT_BOOTPS);
1704a5d661aSToomas Soome 
1714a5d661aSToomas Soome #ifdef SUPPORT_DHCP
1724a5d661aSToomas Soome 	expected_dhcpmsgtype = DHCPOFFER;
1734a5d661aSToomas Soome 	dhcp_ok = 0;
1744a5d661aSToomas Soome #endif
1754a5d661aSToomas Soome 
1764a5d661aSToomas Soome 	if(sendrecv(d,
1774a5d661aSToomas Soome 		    bootpsend, bp, sizeof(*bp),
1787b2a1233SToomas Soome 		    bootprecv, &pkt, (void **)&rbootp) == -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;
1897b2a1233SToomas Soome 		bcopy(&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 
2077b2a1233SToomas Soome 		free(pkt);
2084a5d661aSToomas Soome 		if(sendrecv(d,
2094a5d661aSToomas Soome 			    bootpsend, bp, sizeof(*bp),
2107b2a1233SToomas Soome 			    bootprecv, &pkt, (void **)&rbootp) == -1) {
2114a5d661aSToomas Soome 			printf("DHCPREQUEST failed\n");
2124a5d661aSToomas Soome 			return;
2134a5d661aSToomas Soome 		}
2144a5d661aSToomas Soome 	}
2154a5d661aSToomas Soome #endif
2164a5d661aSToomas Soome 
2177b2a1233SToomas Soome 	myip = d->myip = rbootp->bp_yiaddr;
2187b2a1233SToomas Soome 	servip = rbootp->bp_siaddr;
2197b2a1233SToomas Soome 	if (rootip.s_addr == INADDR_ANY)
2207b2a1233SToomas Soome 		rootip = servip;
2217b2a1233SToomas Soome 	bcopy(rbootp->bp_file, bootfile, sizeof(bootfile));
2224a5d661aSToomas Soome 	bootfile[sizeof(bootfile) - 1] = '\0';
2234a5d661aSToomas Soome 
2249f31b3deSToomas Soome 	if (!netmask) {
2254a5d661aSToomas Soome 		if (IN_CLASSA(ntohl(myip.s_addr)))
2269f31b3deSToomas Soome 			netmask = htonl(IN_CLASSA_NET);
2274a5d661aSToomas Soome 		else if (IN_CLASSB(ntohl(myip.s_addr)))
2289f31b3deSToomas Soome 			netmask = htonl(IN_CLASSB_NET);
2294a5d661aSToomas Soome 		else
2309f31b3deSToomas Soome 			netmask = htonl(IN_CLASSC_NET);
2314a5d661aSToomas Soome #ifdef BOOTP_DEBUG
2324a5d661aSToomas Soome 		if (debug)
2339f31b3deSToomas Soome 			printf("'native netmask' is %s\n", intoa(netmask));
2344a5d661aSToomas Soome #endif
2354a5d661aSToomas Soome 	}
2364a5d661aSToomas Soome 
2374a5d661aSToomas Soome #ifdef BOOTP_DEBUG
2384a5d661aSToomas Soome 	if (debug)
2394a5d661aSToomas Soome 		printf("mask: %s\n", intoa(netmask));
2404a5d661aSToomas Soome #endif
2414a5d661aSToomas Soome 
2424a5d661aSToomas Soome 	/* We need a gateway if root is on a different net */
2434a5d661aSToomas Soome 	if (!SAMENET(myip, rootip, netmask)) {
2444a5d661aSToomas Soome #ifdef BOOTP_DEBUG
2454a5d661aSToomas Soome 		if (debug)
2464a5d661aSToomas Soome 			printf("need gateway for root ip\n");
2474a5d661aSToomas Soome #endif
2484a5d661aSToomas Soome 	}
2494a5d661aSToomas Soome 
2504a5d661aSToomas Soome 	/* Toss gateway if on a different net */
2514a5d661aSToomas Soome 	if (!SAMENET(myip, gateip, netmask)) {
2524a5d661aSToomas Soome #ifdef BOOTP_DEBUG
2534a5d661aSToomas Soome 		if (debug)
2544a5d661aSToomas Soome 			printf("gateway ip (%s) bad\n", inet_ntoa(gateip));
2554a5d661aSToomas Soome #endif
2564a5d661aSToomas Soome 		gateip.s_addr = 0;
2574a5d661aSToomas Soome 	}
2584a5d661aSToomas Soome 
2594a5d661aSToomas Soome 	/* Bump xid so next request will be unique. */
2604a5d661aSToomas Soome 	++d->xid;
2617b2a1233SToomas Soome 	free(pkt);
2624a5d661aSToomas Soome }
2634a5d661aSToomas Soome 
2644a5d661aSToomas Soome /* Transmit a bootp request */
2654a5d661aSToomas Soome static ssize_t
266e1bd2803SToomas Soome bootpsend(struct iodesc *d, void *pkt, size_t len)
2674a5d661aSToomas Soome {
2684a5d661aSToomas Soome 	struct bootp *bp;
2694a5d661aSToomas Soome 
2704a5d661aSToomas Soome #ifdef BOOTP_DEBUG
2714a5d661aSToomas Soome 	if (debug)
2724a5d661aSToomas Soome 		printf("bootpsend: d=%lx called.\n", (long)d);
2734a5d661aSToomas Soome #endif
2744a5d661aSToomas Soome 
2754a5d661aSToomas Soome 	bp = pkt;
2764a5d661aSToomas Soome 	bp->bp_secs = htons((u_short)(getsecs() - bot));
2774a5d661aSToomas Soome 
2784a5d661aSToomas Soome #ifdef BOOTP_DEBUG
2794a5d661aSToomas Soome 	if (debug)
2804a5d661aSToomas Soome 		printf("bootpsend: calling sendudp\n");
2814a5d661aSToomas Soome #endif
2824a5d661aSToomas Soome 
2834a5d661aSToomas Soome 	return (sendudp(d, pkt, len));
2844a5d661aSToomas Soome }
2854a5d661aSToomas Soome 
2864a5d661aSToomas Soome static ssize_t
2877b2a1233SToomas Soome bootprecv(struct iodesc *d, void **pkt, void **payload, time_t tleft)
2884a5d661aSToomas Soome {
2894a5d661aSToomas Soome 	ssize_t n;
2904a5d661aSToomas Soome 	struct bootp *bp;
2917b2a1233SToomas Soome 	void *ptr;
2924a5d661aSToomas Soome 
2937b2a1233SToomas Soome #ifdef BOOTP_DEBUG
2944a5d661aSToomas Soome 	if (debug)
2954a5d661aSToomas Soome 		printf("bootp_recvoffer: called\n");
2964a5d661aSToomas Soome #endif
2974a5d661aSToomas Soome 
2987b2a1233SToomas Soome 	ptr = NULL;
2997b2a1233SToomas Soome 	n = readudp(d, &ptr, (void **)&bp, tleft);
3004a5d661aSToomas Soome 	if (n == -1 || n < sizeof(struct bootp) - BOOTP_VENDSIZE)
3014a5d661aSToomas Soome 		goto bad;
3024a5d661aSToomas Soome 
3034a5d661aSToomas Soome #ifdef BOOTP_DEBUG
3044a5d661aSToomas Soome 	if (debug)
3057b2a1233SToomas Soome 		printf("bootprecv: checked.  bp = %p, n = %zd\n", bp, n);
3064a5d661aSToomas Soome #endif
3074a5d661aSToomas Soome 	if (bp->bp_xid != htonl(d->xid)) {
3084a5d661aSToomas Soome #ifdef BOOTP_DEBUG
3094a5d661aSToomas Soome 		if (debug) {
3104a5d661aSToomas Soome 			printf("bootprecv: expected xid 0x%lx, got 0x%x\n",
3114a5d661aSToomas Soome 			    d->xid, ntohl(bp->bp_xid));
3124a5d661aSToomas Soome 		}
3134a5d661aSToomas Soome #endif
3144a5d661aSToomas Soome 		goto bad;
3154a5d661aSToomas Soome 	}
3164a5d661aSToomas Soome 
3174a5d661aSToomas Soome #ifdef BOOTP_DEBUG
3184a5d661aSToomas Soome 	if (debug)
3194a5d661aSToomas Soome 		printf("bootprecv: got one!\n");
3204a5d661aSToomas Soome #endif
3214a5d661aSToomas Soome 
3224a5d661aSToomas Soome 	/* Suck out vendor info */
3234a5d661aSToomas Soome 	if (bcmp(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)) == 0) {
3247b2a1233SToomas Soome 		int vsize = n - offsetof(struct bootp, bp_vend);
3257b2a1233SToomas Soome 		if(vend_rfc1048(bp->bp_vend, vsize) != 0)
3264a5d661aSToomas Soome 		    goto bad;
32759dfa57dSToomas Soome 
32859dfa57dSToomas Soome 		/* Save copy of bootp reply or DHCP ACK message */
32959dfa57dSToomas Soome 		if (bp->bp_op == BOOTREPLY &&
33059dfa57dSToomas Soome 		    ((dhcp_ok == 1 && expected_dhcpmsgtype == DHCPACK) ||
33159dfa57dSToomas Soome 		    dhcp_ok == 0)) {
33259dfa57dSToomas Soome 			free(bootp_response);
3337b2a1233SToomas Soome 			bootp_response = malloc(n);
33459dfa57dSToomas Soome 			if (bootp_response != NULL) {
3357b2a1233SToomas Soome 				bootp_response_size = n;
3367b2a1233SToomas Soome 				bcopy(bp, bootp_response, bootp_response_size);
33759dfa57dSToomas Soome 			}
33859dfa57dSToomas Soome 		}
3394a5d661aSToomas Soome 	}
3404a5d661aSToomas Soome #ifdef BOOTP_VEND_CMU
3414a5d661aSToomas Soome 	else if (bcmp(vm_cmu, bp->bp_vend, sizeof(vm_cmu)) == 0)
3424a5d661aSToomas Soome 		vend_cmu(bp->bp_vend);
3434a5d661aSToomas Soome #endif
3444a5d661aSToomas Soome 	else
3454a5d661aSToomas Soome 		printf("bootprecv: unknown vendor 0x%lx\n", (long)bp->bp_vend);
3464a5d661aSToomas Soome 
3477b2a1233SToomas Soome 	*pkt = ptr;
3487b2a1233SToomas Soome 	*payload = bp;
3494a5d661aSToomas Soome 	return(n);
3504a5d661aSToomas Soome bad:
3517b2a1233SToomas Soome 	free(ptr);
3524a5d661aSToomas Soome 	errno = 0;
3534a5d661aSToomas Soome 	return (-1);
3544a5d661aSToomas Soome }
3554a5d661aSToomas Soome 
3567b0ff4b9SToomas Soome int
3577b0ff4b9SToomas Soome dhcp_try_rfc1048(uint8_t *cp, size_t len)
3587b0ff4b9SToomas Soome {
3597b0ff4b9SToomas Soome 
3607b0ff4b9SToomas Soome 	expected_dhcpmsgtype = DHCPACK;
3617b0ff4b9SToomas Soome 	if (bcmp(vm_rfc1048, cp, sizeof (vm_rfc1048)) == 0) {
3627b0ff4b9SToomas Soome 		return (vend_rfc1048(cp, len));
3637b0ff4b9SToomas Soome 	}
3647b0ff4b9SToomas Soome 	return (-1);
3657b0ff4b9SToomas Soome }
3667b0ff4b9SToomas Soome 
3674a5d661aSToomas Soome static int
368e1bd2803SToomas Soome vend_rfc1048(u_char *cp, u_int len)
3694a5d661aSToomas Soome {
3704a5d661aSToomas Soome 	u_char *ep;
3714a5d661aSToomas Soome 	int size;
3724a5d661aSToomas Soome 	u_char tag;
3734a5d661aSToomas Soome 	const char *val;
3744a5d661aSToomas Soome 
3754a5d661aSToomas Soome #ifdef BOOTP_DEBUG
3764a5d661aSToomas Soome 	if (debug)
3774a5d661aSToomas Soome 		printf("vend_rfc1048 bootp info. len=%d\n", len);
3784a5d661aSToomas Soome #endif
3794a5d661aSToomas Soome 	ep = cp + len;
3804a5d661aSToomas Soome 
3814a5d661aSToomas Soome 	/* Step over magic cookie */
3824a5d661aSToomas Soome 	cp += sizeof(int);
3834a5d661aSToomas Soome 
3844a5d661aSToomas Soome 	setenv_(cp, ep, NULL);
3854a5d661aSToomas Soome 
3864a5d661aSToomas Soome 	while (cp < ep) {
3874a5d661aSToomas Soome 		tag = *cp++;
3884a5d661aSToomas Soome 		size = *cp++;
3894a5d661aSToomas Soome 		if (tag == TAG_END)
3904a5d661aSToomas Soome 			break;
3914a5d661aSToomas Soome 
3924a5d661aSToomas Soome 		if (tag == TAG_SUBNET_MASK) {
3939f31b3deSToomas Soome 			bcopy(cp, &netmask, sizeof(netmask));
3944a5d661aSToomas Soome 		}
3954a5d661aSToomas Soome 		if (tag == TAG_GATEWAY) {
3964a5d661aSToomas Soome 			bcopy(cp, &gateip.s_addr, sizeof(gateip.s_addr));
3974a5d661aSToomas Soome 		}
3984a5d661aSToomas Soome 		if (tag == TAG_SWAPSERVER) {
3994a5d661aSToomas Soome 			/* let it override bp_siaddr */
4004a5d661aSToomas Soome 			bcopy(cp, &rootip.s_addr, sizeof(rootip.s_addr));
4014a5d661aSToomas Soome 		}
4024a5d661aSToomas Soome 		if (tag == TAG_ROOTPATH) {
4034a5d661aSToomas Soome 			if ((val = getenv("dhcp.root-path")) == NULL)
4044a5d661aSToomas Soome 				val = (const char *)cp;
4054a5d661aSToomas Soome 			strlcpy(rootpath, val, sizeof(rootpath));
4064a5d661aSToomas Soome 		}
4074a5d661aSToomas Soome 		if (tag == TAG_HOSTNAME) {
4084a5d661aSToomas Soome 			if ((val = getenv("dhcp.host-name")) == NULL)
4094a5d661aSToomas Soome 				val = (const char *)cp;
4104a5d661aSToomas Soome 			strlcpy(hostname, val, sizeof(hostname));
4114a5d661aSToomas Soome 		}
412e1bd2803SToomas Soome 		if (tag == TAG_INTF_MTU) {
413e1bd2803SToomas Soome 			intf_mtu = 0;
414e1bd2803SToomas Soome 			if ((val = getenv("dhcp.interface-mtu")) != NULL) {
415e1bd2803SToomas Soome 				unsigned long tmp;
416e1bd2803SToomas Soome 				char *end;
417e1bd2803SToomas Soome 
418e1bd2803SToomas Soome 				errno = 0;
419e1bd2803SToomas Soome 				/*
420e1bd2803SToomas Soome 				 * Do not allow MTU to exceed max IPv4 packet
421e1bd2803SToomas Soome 				 * size, max value of 16-bit word.
422e1bd2803SToomas Soome 				 */
423e1bd2803SToomas Soome 				tmp = strtoul(val, &end, 0);
424e1bd2803SToomas Soome 				if (errno != 0 ||
425e1bd2803SToomas Soome 				    *val == '\0' || *end != '\0' ||
426e1bd2803SToomas Soome 				    tmp > USHRT_MAX) {
427e1bd2803SToomas Soome 					printf("%s: bad value: \"%s\", "
428e1bd2803SToomas Soome 					    "ignoring\n",
429e1bd2803SToomas Soome 					    "dhcp.interface-mtu", val);
430e1bd2803SToomas Soome 				} else {
431e1bd2803SToomas Soome 					intf_mtu = (u_int)tmp;
432e1bd2803SToomas Soome 				}
433e1bd2803SToomas Soome 			}
434e1bd2803SToomas Soome 			if (intf_mtu == 0)
435e1bd2803SToomas Soome 				intf_mtu = be16dec(cp);
436e1bd2803SToomas Soome 		}
4374a5d661aSToomas Soome #ifdef SUPPORT_DHCP
4384a5d661aSToomas Soome 		if (tag == TAG_DHCP_MSGTYPE) {
4394a5d661aSToomas Soome 			if(*cp != expected_dhcpmsgtype)
4404a5d661aSToomas Soome 			    return(-1);
4414a5d661aSToomas Soome 			dhcp_ok = 1;
4424a5d661aSToomas Soome 		}
4434a5d661aSToomas Soome 		if (tag == TAG_SERVERID) {
4444a5d661aSToomas Soome 			bcopy(cp, &dhcp_serverip.s_addr,
4454a5d661aSToomas Soome 			      sizeof(dhcp_serverip.s_addr));
4464a5d661aSToomas Soome 		}
4474a5d661aSToomas Soome #endif
4484a5d661aSToomas Soome 		cp += size;
4494a5d661aSToomas Soome 	}
4504a5d661aSToomas Soome 	return(0);
4514a5d661aSToomas Soome }
4524a5d661aSToomas Soome 
4534a5d661aSToomas Soome #ifdef BOOTP_VEND_CMU
4544a5d661aSToomas Soome static void
455e1bd2803SToomas Soome vend_cmu(u_char *cp)
4564a5d661aSToomas Soome {
4574a5d661aSToomas Soome 	struct cmu_vend *vp;
4584a5d661aSToomas Soome 
4594a5d661aSToomas Soome #ifdef BOOTP_DEBUG
4604a5d661aSToomas Soome 	if (debug)
4614a5d661aSToomas Soome 		printf("vend_cmu bootp info.\n");
4624a5d661aSToomas Soome #endif
4634a5d661aSToomas Soome 	vp = (struct cmu_vend *)cp;
4644a5d661aSToomas Soome 
4654a5d661aSToomas Soome 	if (vp->v_smask.s_addr != 0) {
4669f31b3deSToomas Soome 		netmask = vp->v_smask.s_addr;
4674a5d661aSToomas Soome 	}
4684a5d661aSToomas Soome 	if (vp->v_dgate.s_addr != 0) {
4694a5d661aSToomas Soome 		gateip = vp->v_dgate;
4704a5d661aSToomas Soome 	}
4714a5d661aSToomas Soome }
4724a5d661aSToomas Soome #endif
4734a5d661aSToomas Soome 
4744a5d661aSToomas Soome #ifdef DHCP_ENV
4754a5d661aSToomas Soome /*
4764a5d661aSToomas Soome  * Parse DHCP options and store them into kenv variables.
4774a5d661aSToomas Soome  * Original code from Danny Braniss, modifications by Luigi Rizzo.
4784a5d661aSToomas Soome  *
4794a5d661aSToomas Soome  * The parser is driven by tables which specify the type and name of
4804a5d661aSToomas Soome  * each dhcp option and how it appears in kenv.
4814a5d661aSToomas Soome  * The first entry in the list contains the prefix used to set the kenv
4824a5d661aSToomas Soome  * name (including the . if needed), the last entry must have a 0 tag.
4834a5d661aSToomas Soome  * Entries do not need to be sorted though it helps for readability.
4844a5d661aSToomas Soome  *
4854a5d661aSToomas Soome  * Certain vendor-specific tables can be enabled according to DHCP_ENV.
4864a5d661aSToomas Soome  * Set it to 0 if you don't want any.
4874a5d661aSToomas Soome  */
4884a5d661aSToomas Soome enum opt_fmt { __NONE = 0,
4894a5d661aSToomas Soome 	__8 = 1, __16 = 2, __32 = 4,	/* Unsigned fields, value=size	*/
4904a5d661aSToomas Soome 	__IP,				/* IPv4 address			*/
4914a5d661aSToomas Soome 	__TXT,				/* C string			*/
4924a5d661aSToomas Soome 	__BYTES,			/* byte sequence, printed %02x	*/
4934a5d661aSToomas Soome 	__INDIR,			/* name=value			*/
4944a5d661aSToomas Soome 	__ILIST,			/* name=value;name=value ... */
4954a5d661aSToomas Soome 	__VE,				/* vendor specific, recurse	*/
4964a5d661aSToomas Soome };
4974a5d661aSToomas Soome 
4984a5d661aSToomas Soome struct dhcp_opt {
4994a5d661aSToomas Soome 	uint8_t	tag;
5004a5d661aSToomas Soome 	uint8_t	fmt;
5014a5d661aSToomas Soome 	const char	*desc;
5024a5d661aSToomas Soome };
5034a5d661aSToomas Soome 
5044a5d661aSToomas Soome static struct dhcp_opt vndr_opt[] = { /* Vendor Specific Options */
5054a5d661aSToomas Soome #if DHCP_ENV == DHCP_ENV_FREEBSD /* FreeBSD table in the original code */
5064a5d661aSToomas Soome 	{0,	0,	"FreeBSD"},		/* prefix */
5074a5d661aSToomas Soome 	{1,	__TXT,	"kernel"},
5084a5d661aSToomas Soome 	{2,	__TXT,	"kernelname"},
5094a5d661aSToomas Soome 	{3,	__TXT,	"kernel_options"},
5104a5d661aSToomas Soome 	{4,	__IP,	"usr-ip"},
5114a5d661aSToomas Soome 	{5,	__TXT,	"conf-path"},
5124a5d661aSToomas Soome 	{6,	__TXT,	"rc.conf0"},
5134a5d661aSToomas Soome 	{7,	__TXT,	"rc.conf1"},
5144a5d661aSToomas Soome 	{8,	__TXT,	"rc.conf2"},
5154a5d661aSToomas Soome 	{9,	__TXT,	"rc.conf3"},
5164a5d661aSToomas Soome 	{10,	__TXT,	"rc.conf4"},
5174a5d661aSToomas Soome 	{11,	__TXT,	"rc.conf5"},
5184a5d661aSToomas Soome 	{12,	__TXT,	"rc.conf6"},
5194a5d661aSToomas Soome 	{13,	__TXT,	"rc.conf7"},
5204a5d661aSToomas Soome 	{14,	__TXT,	"rc.conf8"},
5214a5d661aSToomas Soome 	{15,	__TXT,	"rc.conf9"},
5224a5d661aSToomas Soome 
5234a5d661aSToomas Soome 	{20,	__TXT,  "boot.nfsroot.options"},
5244a5d661aSToomas Soome 
5254a5d661aSToomas Soome 	{245,	__INDIR, ""},
5264a5d661aSToomas Soome 	{246,	__INDIR, ""},
5274a5d661aSToomas Soome 	{247,	__INDIR, ""},
5284a5d661aSToomas Soome 	{248,	__INDIR, ""},
5294a5d661aSToomas Soome 	{249,	__INDIR, ""},
5304a5d661aSToomas Soome 	{250,	__INDIR, ""},
5314a5d661aSToomas Soome 	{251,	__INDIR, ""},
5324a5d661aSToomas Soome 	{252,	__INDIR, ""},
5334a5d661aSToomas Soome 	{253,	__INDIR, ""},
5344a5d661aSToomas Soome 	{254,	__INDIR, ""},
5354a5d661aSToomas Soome 
5364a5d661aSToomas Soome #elif DHCP_ENV == DHCP_ENV_PXE		/* some pxe options, RFC4578 */
5374a5d661aSToomas Soome 	{0,	0,	"pxe"},		/* prefix */
5384a5d661aSToomas Soome 	{93,	__16,	"system-architecture"},
5394a5d661aSToomas Soome 	{94,	__BYTES,	"network-interface"},
5404a5d661aSToomas Soome 	{97,	__BYTES,	"machine-identifier"},
5414a5d661aSToomas Soome #else					/* default (empty) table */
5424a5d661aSToomas Soome 	{0,	0,	"dhcp.vendor."},		/* prefix */
5434a5d661aSToomas Soome #endif
5444a5d661aSToomas Soome 	{0,	__TXT,	"%soption-%d"}
5454a5d661aSToomas Soome };
5464a5d661aSToomas Soome 
5474a5d661aSToomas Soome static struct dhcp_opt dhcp_opt[] = {
5484a5d661aSToomas Soome 	/* DHCP Option names, formats and codes, from RFC2132. */
5494a5d661aSToomas Soome 	{0,	0,	"dhcp."},	// prefix
5504a5d661aSToomas Soome 	{1,	__IP,	"subnet-mask"},
5514a5d661aSToomas Soome 	{2,	__32,	"time-offset"}, /* this is signed */
5524a5d661aSToomas Soome 	{3,	__IP,	"routers"},
5534a5d661aSToomas Soome 	{4,	__IP,	"time-servers"},
5544a5d661aSToomas Soome 	{5,	__IP,	"ien116-name-servers"},
5554a5d661aSToomas Soome 	{6,	__IP,	"domain-name-servers"},
5564a5d661aSToomas Soome 	{7,	__IP,	"log-servers"},
5574a5d661aSToomas Soome 	{8,	__IP,	"cookie-servers"},
5584a5d661aSToomas Soome 	{9,	__IP,	"lpr-servers"},
5594a5d661aSToomas Soome 	{10,	__IP,	"impress-servers"},
5604a5d661aSToomas Soome 	{11,	__IP,	"resource-location-servers"},
5614a5d661aSToomas Soome 	{12,	__TXT,	"host-name"},
5624a5d661aSToomas Soome 	{13,	__16,	"boot-size"},
5634a5d661aSToomas Soome 	{14,	__TXT,	"merit-dump"},
5644a5d661aSToomas Soome 	{15,	__TXT,	"domain-name"},
5654a5d661aSToomas Soome 	{16,	__IP,	"swap-server"},
5664a5d661aSToomas Soome 	{17,	__TXT,	"root-path"},
5674a5d661aSToomas Soome 	{18,	__TXT,	"extensions-path"},
5684a5d661aSToomas Soome 	{19,	__8,	"ip-forwarding"},
5694a5d661aSToomas Soome 	{20,	__8,	"non-local-source-routing"},
5704a5d661aSToomas Soome 	{21,	__IP,	"policy-filter"},
5714a5d661aSToomas Soome 	{22,	__16,	"max-dgram-reassembly"},
5724a5d661aSToomas Soome 	{23,	__8,	"default-ip-ttl"},
5734a5d661aSToomas Soome 	{24,	__32,	"path-mtu-aging-timeout"},
5744a5d661aSToomas Soome 	{25,	__16,	"path-mtu-plateau-table"},
5754a5d661aSToomas Soome 	{26,	__16,	"interface-mtu"},
5764a5d661aSToomas Soome 	{27,	__8,	"all-subnets-local"},
5774a5d661aSToomas Soome 	{28,	__IP,	"broadcast-address"},
5784a5d661aSToomas Soome 	{29,	__8,	"perform-mask-discovery"},
5794a5d661aSToomas Soome 	{30,	__8,	"mask-supplier"},
5804a5d661aSToomas Soome 	{31,	__8,	"perform-router-discovery"},
5814a5d661aSToomas Soome 	{32,	__IP,	"router-solicitation-address"},
5824a5d661aSToomas Soome 	{33,	__IP,	"static-routes"},
5834a5d661aSToomas Soome 	{34,	__8,	"trailer-encapsulation"},
5844a5d661aSToomas Soome 	{35,	__32,	"arp-cache-timeout"},
5854a5d661aSToomas Soome 	{36,	__8,	"ieee802-3-encapsulation"},
5864a5d661aSToomas Soome 	{37,	__8,	"default-tcp-ttl"},
5874a5d661aSToomas Soome 	{38,	__32,	"tcp-keepalive-interval"},
5884a5d661aSToomas Soome 	{39,	__8,	"tcp-keepalive-garbage"},
5894a5d661aSToomas Soome 	{40,	__TXT,	"nis-domain"},
5904a5d661aSToomas Soome 	{41,	__IP,	"nis-servers"},
5914a5d661aSToomas Soome 	{42,	__IP,	"ntp-servers"},
5924a5d661aSToomas Soome 	{43,	__VE,	"vendor-encapsulated-options"},
5934a5d661aSToomas Soome 	{44,	__IP,	"netbios-name-servers"},
5944a5d661aSToomas Soome 	{45,	__IP,	"netbios-dd-server"},
5954a5d661aSToomas Soome 	{46,	__8,	"netbios-node-type"},
5964a5d661aSToomas Soome 	{47,	__TXT,	"netbios-scope"},
5974a5d661aSToomas Soome 	{48,	__IP,	"x-font-servers"},
5984a5d661aSToomas Soome 	{49,	__IP,	"x-display-managers"},
5994a5d661aSToomas Soome 	{50,	__IP,	"dhcp-requested-address"},
6004a5d661aSToomas Soome 	{51,	__32,	"dhcp-lease-time"},
6014a5d661aSToomas Soome 	{52,	__8,	"dhcp-option-overload"},
6024a5d661aSToomas Soome 	{53,	__8,	"dhcp-message-type"},
6034a5d661aSToomas Soome 	{54,	__IP,	"dhcp-server-identifier"},
6044a5d661aSToomas Soome 	{55,	__8,	"dhcp-parameter-request-list"},
6054a5d661aSToomas Soome 	{56,	__TXT,	"dhcp-message"},
6064a5d661aSToomas Soome 	{57,	__16,	"dhcp-max-message-size"},
6074a5d661aSToomas Soome 	{58,	__32,	"dhcp-renewal-time"},
6084a5d661aSToomas Soome 	{59,	__32,	"dhcp-rebinding-time"},
6094a5d661aSToomas Soome 	{60,	__TXT,	"vendor-class-identifier"},
6104a5d661aSToomas Soome 	{61,	__TXT,	"dhcp-client-identifier"},
6114a5d661aSToomas Soome 	{64,	__TXT,	"nisplus-domain"},
6124a5d661aSToomas Soome 	{65,	__IP,	"nisplus-servers"},
6134a5d661aSToomas Soome 	{66,	__TXT,	"tftp-server-name"},
6144a5d661aSToomas Soome 	{67,	__TXT,	"bootfile-name"},
6154a5d661aSToomas Soome 	{68,	__IP,	"mobile-ip-home-agent"},
6164a5d661aSToomas Soome 	{69,	__IP,	"smtp-server"},
6174a5d661aSToomas Soome 	{70,	__IP,	"pop-server"},
6184a5d661aSToomas Soome 	{71,	__IP,	"nntp-server"},
6194a5d661aSToomas Soome 	{72,	__IP,	"www-server"},
6204a5d661aSToomas Soome 	{73,	__IP,	"finger-server"},
6214a5d661aSToomas Soome 	{74,	__IP,	"irc-server"},
6224a5d661aSToomas Soome 	{75,	__IP,	"streettalk-server"},
6234a5d661aSToomas Soome 	{76,	__IP,	"streettalk-directory-assistance-server"},
6244a5d661aSToomas Soome 	{77,	__TXT,	"user-class"},
6254a5d661aSToomas Soome 	{85,	__IP,	"nds-servers"},
6264a5d661aSToomas Soome 	{86,	__TXT,	"nds-tree-name"},
6274a5d661aSToomas Soome 	{87,	__TXT,	"nds-context"},
6284a5d661aSToomas Soome 	{210,	__TXT,	"authenticate"},
6294a5d661aSToomas Soome 
6304a5d661aSToomas Soome 	/* use the following entries for arbitrary variables */
6314a5d661aSToomas Soome 	{246,	__ILIST, ""},
6324a5d661aSToomas Soome 	{247,	__ILIST, ""},
6334a5d661aSToomas Soome 	{248,	__ILIST, ""},
6344a5d661aSToomas Soome 	{249,	__ILIST, ""},
6354a5d661aSToomas Soome 	{250,	__INDIR, ""},
6364a5d661aSToomas Soome 	{251,	__INDIR, ""},
6374a5d661aSToomas Soome 	{252,	__INDIR, ""},
6384a5d661aSToomas Soome 	{253,	__INDIR, ""},
6394a5d661aSToomas Soome 	{254,	__INDIR, ""},
6404a5d661aSToomas Soome 	{0,	__TXT,	"%soption-%d"}
6414a5d661aSToomas Soome };
6424a5d661aSToomas Soome 
6434a5d661aSToomas Soome /*
6444a5d661aSToomas Soome  * parse a dhcp response, set environment variables translating options
6454a5d661aSToomas Soome  * names and values according to the tables above. Also set dhcp.tags
6464a5d661aSToomas Soome  * to the list of selected tags.
6474a5d661aSToomas Soome  */
6484a5d661aSToomas Soome static void
6494a5d661aSToomas Soome setenv_(u_char *cp,  u_char *ep, struct dhcp_opt *opts)
6504a5d661aSToomas Soome {
6514a5d661aSToomas Soome     u_char	*ncp;
6524a5d661aSToomas Soome     u_char	tag;
6534a5d661aSToomas Soome     char	tags[512], *tp;	/* the list of tags */
6544a5d661aSToomas Soome 
6554a5d661aSToomas Soome #define FLD_SEP	','	/* separator in list of elements */
6564a5d661aSToomas Soome     ncp = cp;
6574a5d661aSToomas Soome     tp = tags;
6584a5d661aSToomas Soome     if (opts == NULL)
6594a5d661aSToomas Soome 	opts = dhcp_opt;
6604a5d661aSToomas Soome 
6614a5d661aSToomas Soome     while (ncp < ep) {
6624a5d661aSToomas Soome 	unsigned int	size;		/* option size */
6634a5d661aSToomas Soome 	char *vp, *endv, buf[256];	/* the value buffer */
6644a5d661aSToomas Soome 	struct dhcp_opt *op;
6654a5d661aSToomas Soome 
6664a5d661aSToomas Soome 	tag = *ncp++;			/* extract tag and size */
6674a5d661aSToomas Soome 	size = *ncp++;
6684a5d661aSToomas Soome 	cp = ncp;			/* current payload */
6694a5d661aSToomas Soome 	ncp += size;			/* point to the next option */
6704a5d661aSToomas Soome 
6714a5d661aSToomas Soome 	if (tag == TAG_END)
6724a5d661aSToomas Soome 	    break;
6734a5d661aSToomas Soome 	if (tag == 0)
6744a5d661aSToomas Soome 	    continue;
6754a5d661aSToomas Soome 
6764a5d661aSToomas Soome 	for (op = opts+1; op->tag && op->tag != tag; op++)
6774a5d661aSToomas Soome 		;
6784a5d661aSToomas Soome 	/* if not found we end up on the default entry */
6794a5d661aSToomas Soome 
6804a5d661aSToomas Soome 	/*
6814a5d661aSToomas Soome 	 * Copy data into the buffer. libstand does not have snprintf so we
6824a5d661aSToomas Soome 	 * need to be careful with sprintf(). With strings, the source is
6834a5d661aSToomas Soome 	 * always <256 char so shorter than the buffer so we are safe; with
6844a5d661aSToomas Soome 	 * other arguments, the longest string is inet_ntoa which is 16 bytes
6854a5d661aSToomas Soome 	 * so we make sure to have always enough room in the string before
6864a5d661aSToomas Soome 	 * trying an sprint.
6874a5d661aSToomas Soome 	 */
6884a5d661aSToomas Soome 	vp = buf;
6894a5d661aSToomas Soome 	*vp = '\0';
6904a5d661aSToomas Soome 	endv = buf + sizeof(buf) - 1 - 16;	/* last valid write position */
6914a5d661aSToomas Soome 
6924a5d661aSToomas Soome 	switch(op->fmt) {
6934a5d661aSToomas Soome 	case __NONE:
6944a5d661aSToomas Soome 	    break;	/* should not happen */
6954a5d661aSToomas Soome 
6964a5d661aSToomas Soome 	case __VE: /* recurse, vendor specific */
6974a5d661aSToomas Soome 	    setenv_(cp, cp+size, vndr_opt);
6984a5d661aSToomas Soome 	    break;
6994a5d661aSToomas Soome 
7004a5d661aSToomas Soome 	case __IP:	/* ip address */
7014a5d661aSToomas Soome 	    for (; size > 0 && vp < endv; size -= 4, cp += 4) {
7024a5d661aSToomas Soome 		struct	in_addr in_ip;		/* ip addresses */
7034a5d661aSToomas Soome 		if (vp != buf)
7044a5d661aSToomas Soome 		    *vp++ = FLD_SEP;
7054a5d661aSToomas Soome 		bcopy(cp, &in_ip.s_addr, sizeof(in_ip.s_addr));
7064a5d661aSToomas Soome 		sprintf(vp, "%s", inet_ntoa(in_ip));
7074a5d661aSToomas Soome 		vp += strlen(vp);
7084a5d661aSToomas Soome 	    }
7094a5d661aSToomas Soome 	    break;
7104a5d661aSToomas Soome 
7114a5d661aSToomas Soome 	case __BYTES:	/* opaque byte string */
7124a5d661aSToomas Soome 	    for (; size > 0 && vp < endv; size -= 1, cp += 1) {
7134a5d661aSToomas Soome 		sprintf(vp, "%02x", *cp);
7144a5d661aSToomas Soome 		vp += strlen(vp);
7154a5d661aSToomas Soome 	    }
7164a5d661aSToomas Soome 	    break;
7174a5d661aSToomas Soome 
7184a5d661aSToomas Soome 	case __TXT:
7194a5d661aSToomas Soome 	    bcopy(cp, buf, size);	/* cannot overflow */
7204a5d661aSToomas Soome 	    buf[size] = 0;
7214a5d661aSToomas Soome 	    break;
7224a5d661aSToomas Soome 
7234a5d661aSToomas Soome 	case __32:
7244a5d661aSToomas Soome 	case __16:
7254a5d661aSToomas Soome 	case __8:	/* op->fmt is also the length of each field */
7264a5d661aSToomas Soome 	    for (; size > 0 && vp < endv; size -= op->fmt, cp += op->fmt) {
7274a5d661aSToomas Soome 		uint32_t v;
7284a5d661aSToomas Soome 		if (op->fmt == __32)
7294a5d661aSToomas Soome 			v = (cp[0]<<24) + (cp[1]<<16) + (cp[2]<<8) + cp[3];
7304a5d661aSToomas Soome 		else if (op->fmt == __16)
7314a5d661aSToomas Soome 			v = (cp[0]<<8) + cp[1];
7324a5d661aSToomas Soome 		else
7334a5d661aSToomas Soome 			v = cp[0];
7344a5d661aSToomas Soome 		if (vp != buf)
7354a5d661aSToomas Soome 		    *vp++ = FLD_SEP;
7364a5d661aSToomas Soome 		sprintf(vp, "%u", v);
7374a5d661aSToomas Soome 		vp += strlen(vp);
7384a5d661aSToomas Soome 	    }
7394a5d661aSToomas Soome 	    break;
7404a5d661aSToomas Soome 
7414a5d661aSToomas Soome 	case __INDIR:	/* name=value */
7424a5d661aSToomas Soome 	case __ILIST:	/* name=value;name=value... */
7434a5d661aSToomas Soome 	    bcopy(cp, buf, size);	/* cannot overflow */
7444a5d661aSToomas Soome 	    buf[size] = '\0';
7454a5d661aSToomas Soome 	    for (endv = buf; endv; endv = vp) {
7464a5d661aSToomas Soome 		u_char *s = NULL;	/* semicolon ? */
7474a5d661aSToomas Soome 
7484a5d661aSToomas Soome 		/* skip leading whitespace */
7494a5d661aSToomas Soome 		while (*endv && strchr(" \t\n\r", *endv))
7504a5d661aSToomas Soome 		    endv++;
7514a5d661aSToomas Soome 		vp = strchr(endv, '=');	/* find name=value separator */
7524a5d661aSToomas Soome 		if (!vp)
7534a5d661aSToomas Soome 		    break;
7544a5d661aSToomas Soome 		*vp++ = 0;
7554a5d661aSToomas Soome 		if (op->fmt == __ILIST && (s = strchr(vp, ';')))
7564a5d661aSToomas Soome 		    *s++ = '\0';
7574a5d661aSToomas Soome 		setenv(endv, vp, 1);
7584a5d661aSToomas Soome 		vp = s;	/* prepare for next round */
7594a5d661aSToomas Soome 	    }
7604a5d661aSToomas Soome 	    buf[0] = '\0';	/* option already done */
7614a5d661aSToomas Soome 	}
7624a5d661aSToomas Soome 
7634a5d661aSToomas Soome 	if (tp - tags < sizeof(tags) - 5) {	/* add tag to the list */
7644a5d661aSToomas Soome 	    if (tp != tags)
7654a5d661aSToomas Soome 		*tp++ = FLD_SEP;
7664a5d661aSToomas Soome 	    sprintf(tp, "%d", tag);
7674a5d661aSToomas Soome 	    tp += strlen(tp);
7684a5d661aSToomas Soome 	}
7694a5d661aSToomas Soome 	if (buf[0]) {
7704a5d661aSToomas Soome 	    char	env[128];	/* the string name */
7714a5d661aSToomas Soome 
7724a5d661aSToomas Soome 	    if (op->tag == 0)
7734a5d661aSToomas Soome 		sprintf(env, op->desc, opts[0].desc, tag);
7744a5d661aSToomas Soome 	    else
7754a5d661aSToomas Soome 		sprintf(env, "%s%s", opts[0].desc, op->desc);
7764a5d661aSToomas Soome 	    /*
7774a5d661aSToomas Soome 	     * Do not replace existing values in the environment, so that
7784a5d661aSToomas Soome 	     * locally-obtained values can override server-provided values.
7794a5d661aSToomas Soome 	     */
7804a5d661aSToomas Soome 	    setenv(env, buf, 0);
7814a5d661aSToomas Soome 	}
7824a5d661aSToomas Soome     }
7834a5d661aSToomas Soome     if (tp != tags) {
7844a5d661aSToomas Soome 	char	env[128];	/* the string name */
7854a5d661aSToomas Soome 	sprintf(env, "%stags", opts[0].desc);
7864a5d661aSToomas Soome 	setenv(env, tags, 1);
7874a5d661aSToomas Soome     }
7884a5d661aSToomas Soome }
7894a5d661aSToomas Soome #endif /* additional dhcp */
790