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