xref: /freebsd/stand/libsa/bootp.c (revision 56e53cb8ef000c3ef72337a4095987a932cdedef)
1ca987d46SWarner Losh /*	$NetBSD: bootp.c,v 1.14 1998/02/16 11:10:54 drochner Exp $	*/
2ca987d46SWarner Losh 
3ca987d46SWarner Losh /*
4ca987d46SWarner Losh  * Copyright (c) 1992 Regents of the University of California.
5ca987d46SWarner Losh  * All rights reserved.
6ca987d46SWarner Losh  *
7ca987d46SWarner Losh  * This software was developed by the Computer Systems Engineering group
8ca987d46SWarner Losh  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9ca987d46SWarner Losh  * contributed to Berkeley.
10ca987d46SWarner Losh  *
11ca987d46SWarner Losh  * Redistribution and use in source and binary forms, with or without
12ca987d46SWarner Losh  * modification, are permitted provided that the following conditions
13ca987d46SWarner Losh  * are met:
14ca987d46SWarner Losh  * 1. Redistributions of source code must retain the above copyright
15ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer.
16ca987d46SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
17ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
18ca987d46SWarner Losh  *    documentation and/or other materials provided with the distribution.
19ca987d46SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
20ca987d46SWarner Losh  *    may be used to endorse or promote products derived from this software
21ca987d46SWarner Losh  *    without specific prior written permission.
22ca987d46SWarner Losh  *
23ca987d46SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24ca987d46SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ca987d46SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ca987d46SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27ca987d46SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ca987d46SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ca987d46SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ca987d46SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ca987d46SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ca987d46SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ca987d46SWarner Losh  * SUCH DAMAGE.
34ca987d46SWarner Losh  *
35ca987d46SWarner Losh  * @(#) Header: bootp.c,v 1.4 93/09/11 03:13:51 leres Exp  (LBL)
36ca987d46SWarner Losh  */
37ca987d46SWarner Losh 
38ca987d46SWarner Losh #include <sys/cdefs.h>
39ca987d46SWarner Losh __FBSDID("$FreeBSD$");
40ca987d46SWarner Losh 
41ca987d46SWarner Losh #include <stddef.h>
42ca987d46SWarner Losh #include <sys/types.h>
43ca987d46SWarner Losh #include <sys/limits.h>
44ca987d46SWarner Losh #include <sys/endian.h>
45ca987d46SWarner Losh #include <netinet/in.h>
46ca987d46SWarner Losh #include <netinet/in_systm.h>
47ca987d46SWarner Losh 
48ca987d46SWarner Losh #include <string.h>
49ca987d46SWarner Losh 
50ca987d46SWarner Losh #define BOOTP_DEBUGxx
51ca987d46SWarner Losh #define SUPPORT_DHCP
52ca987d46SWarner Losh 
53ca987d46SWarner Losh #define	DHCP_ENV_NOVENDOR	1	/* do not parse vendor options */
54ca987d46SWarner Losh #define	DHCP_ENV_PXE		10	/* assume pxe vendor options */
55ca987d46SWarner Losh #define	DHCP_ENV_FREEBSD	11	/* assume freebsd vendor options */
56ca987d46SWarner Losh /* set DHCP_ENV to one of the values above to export dhcp options to kenv */
57ca987d46SWarner Losh #define DHCP_ENV		DHCP_ENV_NO_VENDOR
58ca987d46SWarner Losh 
59ca987d46SWarner Losh #include "stand.h"
60ca987d46SWarner Losh #include "net.h"
61ca987d46SWarner Losh #include "netif.h"
62ca987d46SWarner Losh #include "bootp.h"
63ca987d46SWarner Losh 
64ca987d46SWarner Losh 
65ca987d46SWarner Losh struct in_addr servip;
66ca987d46SWarner Losh 
67ca987d46SWarner Losh static time_t	bot;
68ca987d46SWarner Losh 
69ca987d46SWarner Losh static	char vm_rfc1048[4] = VM_RFC1048;
70ca987d46SWarner Losh #ifdef BOOTP_VEND_CMU
71ca987d46SWarner Losh static	char vm_cmu[4] = VM_CMU;
72ca987d46SWarner Losh #endif
73ca987d46SWarner Losh 
74ca987d46SWarner Losh /* Local forwards */
75ca987d46SWarner Losh static	ssize_t bootpsend(struct iodesc *, void *, size_t);
76c5b86c3bSKyle Evans static	ssize_t bootprecv(struct iodesc *, void **, void **, time_t, void *);
77ca987d46SWarner Losh static	int vend_rfc1048(u_char *, u_int);
78ca987d46SWarner Losh #ifdef BOOTP_VEND_CMU
79ca987d46SWarner Losh static	void vend_cmu(u_char *);
80ca987d46SWarner Losh #endif
81ca987d46SWarner Losh 
82ca987d46SWarner Losh #ifdef DHCP_ENV		/* export the dhcp response to kenv */
83ca987d46SWarner Losh struct dhcp_opt;
84ca987d46SWarner Losh static void setenv_(u_char *cp,  u_char *ep, struct dhcp_opt *opts);
85ca987d46SWarner Losh #else
86ca987d46SWarner Losh #define setenv_(a, b, c)
87ca987d46SWarner Losh #endif
88ca987d46SWarner Losh 
89ca987d46SWarner Losh #ifdef SUPPORT_DHCP
90ca987d46SWarner Losh static char expected_dhcpmsgtype = -1, dhcp_ok;
91ca987d46SWarner Losh struct in_addr dhcp_serverip;
92ca987d46SWarner Losh #endif
93ca987d46SWarner Losh struct bootp *bootp_response;
94ca987d46SWarner Losh size_t bootp_response_size;
95ca987d46SWarner Losh 
96ca987d46SWarner Losh static void
97ca987d46SWarner Losh bootp_fill_request(unsigned char *bp_vend)
98ca987d46SWarner Losh {
99ca987d46SWarner Losh 	/*
100ca987d46SWarner Losh 	 * We are booting from PXE, we want to send the string
101ca987d46SWarner Losh 	 * 'PXEClient' to the DHCP server so you have the option of
102ca987d46SWarner Losh 	 * only responding to PXE aware dhcp requests.
103ca987d46SWarner Losh 	 */
104ca987d46SWarner Losh 	bp_vend[0] = TAG_CLASSID;
105ca987d46SWarner Losh 	bp_vend[1] = 9;
106ca987d46SWarner Losh 	bcopy("PXEClient", &bp_vend[2], 9);
107ca987d46SWarner Losh 	bp_vend[11] = TAG_USER_CLASS;
108ca987d46SWarner Losh 	/* len of each user class + number of user class */
109ca987d46SWarner Losh 	bp_vend[12] = 8;
110ca987d46SWarner Losh 	/* len of the first user class */
111ca987d46SWarner Losh 	bp_vend[13] = 7;
112ca987d46SWarner Losh 	bcopy("FreeBSD", &bp_vend[14], 7);
113ca987d46SWarner Losh 	bp_vend[21] = TAG_PARAM_REQ;
114ca987d46SWarner Losh 	bp_vend[22] = 7;
115ca987d46SWarner Losh 	bp_vend[23] = TAG_ROOTPATH;
116ca987d46SWarner Losh 	bp_vend[24] = TAG_HOSTNAME;
117ca987d46SWarner Losh 	bp_vend[25] = TAG_SWAPSERVER;
118ca987d46SWarner Losh 	bp_vend[26] = TAG_GATEWAY;
119ca987d46SWarner Losh 	bp_vend[27] = TAG_SUBNET_MASK;
120ca987d46SWarner Losh 	bp_vend[28] = TAG_INTF_MTU;
121ca987d46SWarner Losh 	bp_vend[29] = TAG_SERVERID;
122ca987d46SWarner Losh 	bp_vend[30] = TAG_END;
123ca987d46SWarner Losh }
124ca987d46SWarner Losh 
125ca987d46SWarner Losh /* Fetch required bootp infomation */
126ca987d46SWarner Losh void
127ca987d46SWarner Losh bootp(int sock)
128ca987d46SWarner Losh {
129ca987d46SWarner Losh 	void *pkt;
130ca987d46SWarner Losh 	struct iodesc *d;
131ca987d46SWarner Losh 	struct bootp *bp;
132ca987d46SWarner Losh 	struct {
133ca987d46SWarner Losh 		u_char header[HEADER_SIZE];
134ca987d46SWarner Losh 		struct bootp wbootp;
135ca987d46SWarner Losh 	} wbuf;
136ca987d46SWarner Losh 	struct bootp *rbootp;
137ca987d46SWarner Losh 
138ca987d46SWarner Losh #ifdef BOOTP_DEBUG
139ca987d46SWarner Losh  	if (debug)
140ca987d46SWarner Losh 		printf("bootp: socket=%d\n", sock);
141ca987d46SWarner Losh #endif
142ca987d46SWarner Losh 	if (!bot)
143ca987d46SWarner Losh 		bot = getsecs();
144ca987d46SWarner Losh 
145ca987d46SWarner Losh 	if (!(d = socktodesc(sock))) {
146ca987d46SWarner Losh 		printf("bootp: bad socket. %d\n", sock);
147ca987d46SWarner Losh 		return;
148ca987d46SWarner Losh 	}
149ca987d46SWarner Losh #ifdef BOOTP_DEBUG
150ca987d46SWarner Losh  	if (debug)
151ca987d46SWarner Losh 		printf("bootp: d=%lx\n", (long)d);
152ca987d46SWarner Losh #endif
153ca987d46SWarner Losh 
154ca987d46SWarner Losh 	bp = &wbuf.wbootp;
155ca987d46SWarner Losh 	bzero(bp, sizeof(*bp));
156ca987d46SWarner Losh 
157ca987d46SWarner Losh 	bp->bp_op = BOOTREQUEST;
158ca987d46SWarner Losh 	bp->bp_htype = 1;		/* 10Mb Ethernet (48 bits) */
159ca987d46SWarner Losh 	bp->bp_hlen = 6;
160ca987d46SWarner Losh 	bp->bp_xid = htonl(d->xid);
161ca987d46SWarner Losh 	MACPY(d->myea, bp->bp_chaddr);
162ca987d46SWarner Losh 	strncpy(bp->bp_file, bootfile, sizeof(bp->bp_file));
163ca987d46SWarner Losh 	bcopy(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048));
164ca987d46SWarner Losh #ifdef SUPPORT_DHCP
165ca987d46SWarner Losh 	bp->bp_vend[4] = TAG_DHCP_MSGTYPE;
166ca987d46SWarner Losh 	bp->bp_vend[5] = 1;
167ca987d46SWarner Losh 	bp->bp_vend[6] = DHCPDISCOVER;
168ca987d46SWarner Losh 	bootp_fill_request(&bp->bp_vend[7]);
169ca987d46SWarner Losh 
170ca987d46SWarner Losh #else
171ca987d46SWarner Losh 	bp->bp_vend[4] = TAG_END;
172ca987d46SWarner Losh #endif
173ca987d46SWarner Losh 
174ca987d46SWarner Losh 	d->myip.s_addr = INADDR_ANY;
175ca987d46SWarner Losh 	d->myport = htons(IPPORT_BOOTPC);
176ca987d46SWarner Losh 	d->destip.s_addr = INADDR_BROADCAST;
177ca987d46SWarner Losh 	d->destport = htons(IPPORT_BOOTPS);
178ca987d46SWarner Losh 
179ca987d46SWarner Losh #ifdef SUPPORT_DHCP
180ca987d46SWarner Losh 	expected_dhcpmsgtype = DHCPOFFER;
181ca987d46SWarner Losh 	dhcp_ok = 0;
182ca987d46SWarner Losh #endif
183ca987d46SWarner Losh 
184ca987d46SWarner Losh 	if(sendrecv(d,
185ca987d46SWarner Losh 		    bootpsend, bp, sizeof(*bp),
186c5b86c3bSKyle Evans 		    bootprecv, &pkt, (void **)&rbootp, NULL) == -1) {
187ca987d46SWarner Losh 	    printf("bootp: no reply\n");
188ca987d46SWarner Losh 	    return;
189ca987d46SWarner Losh 	}
190ca987d46SWarner Losh 
191ca987d46SWarner Losh #ifdef SUPPORT_DHCP
192ca987d46SWarner Losh 	if(dhcp_ok) {
193*56e53cb8SWarner Losh 		uint32_t leasetime;
194ca987d46SWarner Losh 		bp->bp_vend[6] = DHCPREQUEST;
195ca987d46SWarner Losh 		bp->bp_vend[7] = TAG_REQ_ADDR;
196ca987d46SWarner Losh 		bp->bp_vend[8] = 4;
197ca987d46SWarner Losh 		bcopy(&rbootp->bp_yiaddr, &bp->bp_vend[9], 4);
198ca987d46SWarner Losh 		bp->bp_vend[13] = TAG_SERVERID;
199ca987d46SWarner Losh 		bp->bp_vend[14] = 4;
200ca987d46SWarner Losh 		bcopy(&dhcp_serverip.s_addr, &bp->bp_vend[15], 4);
201ca987d46SWarner Losh 		bp->bp_vend[19] = TAG_LEASETIME;
202ca987d46SWarner Losh 		bp->bp_vend[20] = 4;
203ca987d46SWarner Losh 		leasetime = htonl(300);
204ca987d46SWarner Losh 		bcopy(&leasetime, &bp->bp_vend[21], 4);
205ca987d46SWarner Losh 		bootp_fill_request(&bp->bp_vend[25]);
206ca987d46SWarner Losh 
207ca987d46SWarner Losh 		expected_dhcpmsgtype = DHCPACK;
208ca987d46SWarner Losh 
209ca987d46SWarner Losh 		free(pkt);
210ca987d46SWarner Losh 		if(sendrecv(d,
211ca987d46SWarner Losh 			    bootpsend, bp, sizeof(*bp),
212c5b86c3bSKyle Evans 			    bootprecv, &pkt, (void **)&rbootp, NULL) == -1) {
213ca987d46SWarner Losh 			printf("DHCPREQUEST failed\n");
214ca987d46SWarner Losh 			return;
215ca987d46SWarner Losh 		}
216ca987d46SWarner Losh 	}
217ca987d46SWarner Losh #endif
218ca987d46SWarner Losh 
219ca987d46SWarner Losh 	myip = d->myip = rbootp->bp_yiaddr;
220ca987d46SWarner Losh 	servip = rbootp->bp_siaddr;
221ca987d46SWarner Losh 	if (rootip.s_addr == INADDR_ANY)
222ca987d46SWarner Losh 		rootip = servip;
223ca987d46SWarner Losh 	bcopy(rbootp->bp_file, bootfile, sizeof(bootfile));
224ca987d46SWarner Losh 	bootfile[sizeof(bootfile) - 1] = '\0';
225ca987d46SWarner Losh 
226ca987d46SWarner Losh 	if (!netmask) {
227ca987d46SWarner Losh 		if (IN_CLASSA(ntohl(myip.s_addr)))
228ca987d46SWarner Losh 			netmask = htonl(IN_CLASSA_NET);
229ca987d46SWarner Losh 		else if (IN_CLASSB(ntohl(myip.s_addr)))
230ca987d46SWarner Losh 			netmask = htonl(IN_CLASSB_NET);
231ca987d46SWarner Losh 		else
232ca987d46SWarner Losh 			netmask = htonl(IN_CLASSC_NET);
233ca987d46SWarner Losh #ifdef BOOTP_DEBUG
234ca987d46SWarner Losh 		if (debug)
235ca987d46SWarner Losh 			printf("'native netmask' is %s\n", intoa(netmask));
236ca987d46SWarner Losh #endif
237ca987d46SWarner Losh 	}
238ca987d46SWarner Losh 
239ca987d46SWarner Losh #ifdef BOOTP_DEBUG
240ca987d46SWarner Losh 	if (debug)
241ca987d46SWarner Losh 		printf("mask: %s\n", intoa(netmask));
242ca987d46SWarner Losh #endif
243ca987d46SWarner Losh 
244ca987d46SWarner Losh 	/* We need a gateway if root is on a different net */
245ca987d46SWarner Losh 	if (!SAMENET(myip, rootip, netmask)) {
246ca987d46SWarner Losh #ifdef BOOTP_DEBUG
247ca987d46SWarner Losh 		if (debug)
248ca987d46SWarner Losh 			printf("need gateway for root ip\n");
249ca987d46SWarner Losh #endif
250ca987d46SWarner Losh 	}
251ca987d46SWarner Losh 
252ca987d46SWarner Losh 	/* Toss gateway if on a different net */
253ca987d46SWarner Losh 	if (!SAMENET(myip, gateip, netmask)) {
254ca987d46SWarner Losh #ifdef BOOTP_DEBUG
255ca987d46SWarner Losh 		if (debug)
256ca987d46SWarner Losh 			printf("gateway ip (%s) bad\n", inet_ntoa(gateip));
257ca987d46SWarner Losh #endif
258ca987d46SWarner Losh 		gateip.s_addr = 0;
259ca987d46SWarner Losh 	}
260ca987d46SWarner Losh 
261ca987d46SWarner Losh 	/* Bump xid so next request will be unique. */
262ca987d46SWarner Losh 	++d->xid;
263ca987d46SWarner Losh 	free(pkt);
264ca987d46SWarner Losh }
265ca987d46SWarner Losh 
266ca987d46SWarner Losh /* Transmit a bootp request */
267ca987d46SWarner Losh static ssize_t
268ca987d46SWarner Losh bootpsend(struct iodesc *d, void *pkt, size_t len)
269ca987d46SWarner Losh {
270ca987d46SWarner Losh 	struct bootp *bp;
271ca987d46SWarner Losh 
272ca987d46SWarner Losh #ifdef BOOTP_DEBUG
273ca987d46SWarner Losh 	if (debug)
274ca987d46SWarner Losh 		printf("bootpsend: d=%lx called.\n", (long)d);
275ca987d46SWarner Losh #endif
276ca987d46SWarner Losh 
277ca987d46SWarner Losh 	bp = pkt;
278ca987d46SWarner Losh 	bp->bp_secs = htons((u_short)(getsecs() - bot));
279ca987d46SWarner Losh 
280ca987d46SWarner Losh #ifdef BOOTP_DEBUG
281ca987d46SWarner Losh 	if (debug)
282ca987d46SWarner Losh 		printf("bootpsend: calling sendudp\n");
283ca987d46SWarner Losh #endif
284ca987d46SWarner Losh 
285ca987d46SWarner Losh 	return (sendudp(d, pkt, len));
286ca987d46SWarner Losh }
287ca987d46SWarner Losh 
288ca987d46SWarner Losh static ssize_t
289c5b86c3bSKyle Evans bootprecv(struct iodesc *d, void **pkt, void **payload, time_t tleft,
290c5b86c3bSKyle Evans     void *extra)
291ca987d46SWarner Losh {
292ca987d46SWarner Losh 	ssize_t n;
293ca987d46SWarner Losh 	struct bootp *bp;
294ca987d46SWarner Losh 	void *ptr;
295ca987d46SWarner Losh 
296ca987d46SWarner Losh #ifdef BOOTP_DEBUG
297ca987d46SWarner Losh 	if (debug)
298ca987d46SWarner Losh 		printf("bootp_recvoffer: called\n");
299ca987d46SWarner Losh #endif
300ca987d46SWarner Losh 
301ca987d46SWarner Losh 	ptr = NULL;
302ca987d46SWarner Losh 	n = readudp(d, &ptr, (void **)&bp, tleft);
303ca987d46SWarner Losh 	if (n == -1 || n < sizeof(struct bootp) - BOOTP_VENDSIZE)
304ca987d46SWarner Losh 		goto bad;
305ca987d46SWarner Losh 
306ca987d46SWarner Losh #ifdef BOOTP_DEBUG
307ca987d46SWarner Losh 	if (debug)
308ca987d46SWarner Losh 		printf("bootprecv: checked.  bp = %p, n = %zd\n", bp, n);
309ca987d46SWarner Losh #endif
310ca987d46SWarner Losh 	if (bp->bp_xid != htonl(d->xid)) {
311ca987d46SWarner Losh #ifdef BOOTP_DEBUG
312ca987d46SWarner Losh 		if (debug) {
313ca987d46SWarner Losh 			printf("bootprecv: expected xid 0x%lx, got 0x%x\n",
314ca987d46SWarner Losh 			    d->xid, ntohl(bp->bp_xid));
315ca987d46SWarner Losh 		}
316ca987d46SWarner Losh #endif
317ca987d46SWarner Losh 		goto bad;
318ca987d46SWarner Losh 	}
319ca987d46SWarner Losh 
320ca987d46SWarner Losh #ifdef BOOTP_DEBUG
321ca987d46SWarner Losh 	if (debug)
322ca987d46SWarner Losh 		printf("bootprecv: got one!\n");
323ca987d46SWarner Losh #endif
324ca987d46SWarner Losh 
325ca987d46SWarner Losh 	/* Suck out vendor info */
326ca987d46SWarner Losh 	if (bcmp(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)) == 0) {
327ca987d46SWarner Losh 		int vsize = n - offsetof(struct bootp, bp_vend);
328ca987d46SWarner Losh 		if (vend_rfc1048(bp->bp_vend, vsize) != 0)
329ca987d46SWarner Losh 		    goto bad;
330ca987d46SWarner Losh 
331ca987d46SWarner Losh 		/* Save copy of bootp reply or DHCP ACK message */
332ca987d46SWarner Losh 		if (bp->bp_op == BOOTREPLY &&
333ca987d46SWarner Losh 		    ((dhcp_ok == 1 && expected_dhcpmsgtype == DHCPACK) ||
334ca987d46SWarner Losh 		    dhcp_ok == 0)) {
335ca987d46SWarner Losh 			free(bootp_response);
336ca987d46SWarner Losh 			bootp_response = malloc(n);
337ca987d46SWarner Losh 			if (bootp_response != NULL) {
338ca987d46SWarner Losh 				bootp_response_size = n;
339ca987d46SWarner Losh 				bcopy(bp, bootp_response, bootp_response_size);
340ca987d46SWarner Losh 			}
341ca987d46SWarner Losh 		}
342ca987d46SWarner Losh 	}
343ca987d46SWarner Losh #ifdef BOOTP_VEND_CMU
344ca987d46SWarner Losh 	else if (bcmp(vm_cmu, bp->bp_vend, sizeof(vm_cmu)) == 0)
345ca987d46SWarner Losh 		vend_cmu(bp->bp_vend);
346ca987d46SWarner Losh #endif
347ca987d46SWarner Losh 	else
348ca987d46SWarner Losh 		printf("bootprecv: unknown vendor 0x%lx\n", (long)bp->bp_vend);
349ca987d46SWarner Losh 
350ca987d46SWarner Losh 	*pkt = ptr;
351ca987d46SWarner Losh 	*payload = bp;
352ca987d46SWarner Losh 	return (n);
353ca987d46SWarner Losh bad:
354ca987d46SWarner Losh 	free(ptr);
355ca987d46SWarner Losh 	errno = 0;
356ca987d46SWarner Losh 	return (-1);
357ca987d46SWarner Losh }
358ca987d46SWarner Losh 
359ca987d46SWarner Losh static int
360ca987d46SWarner Losh vend_rfc1048(u_char *cp, u_int len)
361ca987d46SWarner Losh {
362ca987d46SWarner Losh 	u_char *ep;
363ca987d46SWarner Losh 	int size;
364ca987d46SWarner Losh 	u_char tag;
365ca987d46SWarner Losh 	const char *val;
366ca987d46SWarner Losh 
367ca987d46SWarner Losh #ifdef BOOTP_DEBUG
368ca987d46SWarner Losh 	if (debug)
369ca987d46SWarner Losh 		printf("vend_rfc1048 bootp info. len=%d\n", len);
370ca987d46SWarner Losh #endif
371ca987d46SWarner Losh 	ep = cp + len;
372ca987d46SWarner Losh 
373ca987d46SWarner Losh 	/* Step over magic cookie */
374ca987d46SWarner Losh 	cp += sizeof(int);
375ca987d46SWarner Losh 
376ca987d46SWarner Losh 	setenv_(cp, ep, NULL);
377ca987d46SWarner Losh 
378ca987d46SWarner Losh 	while (cp < ep) {
379ca987d46SWarner Losh 		tag = *cp++;
380ca987d46SWarner Losh 		size = *cp++;
381ca987d46SWarner Losh 		if (tag == TAG_END)
382ca987d46SWarner Losh 			break;
383ca987d46SWarner Losh 
384ca987d46SWarner Losh 		if (tag == TAG_SUBNET_MASK) {
385ca987d46SWarner Losh 			bcopy(cp, &netmask, sizeof(netmask));
386ca987d46SWarner Losh 		}
387ca987d46SWarner Losh 		if (tag == TAG_GATEWAY) {
388ca987d46SWarner Losh 			bcopy(cp, &gateip.s_addr, sizeof(gateip.s_addr));
389ca987d46SWarner Losh 		}
390ca987d46SWarner Losh 		if (tag == TAG_SWAPSERVER) {
391ca987d46SWarner Losh 			/* let it override bp_siaddr */
392ca987d46SWarner Losh 			bcopy(cp, &rootip.s_addr, sizeof(rootip.s_addr));
393ca987d46SWarner Losh 		}
394ca987d46SWarner Losh 		if (tag == TAG_ROOTPATH) {
395ca987d46SWarner Losh 			if ((val = getenv("dhcp.root-path")) == NULL)
396ca987d46SWarner Losh 				val = (const char *)cp;
397ca987d46SWarner Losh 			strlcpy(rootpath, val, sizeof(rootpath));
398ca987d46SWarner Losh 		}
399ca987d46SWarner Losh 		if (tag == TAG_HOSTNAME) {
400ca987d46SWarner Losh 			if ((val = getenv("dhcp.host-name")) == NULL)
401ca987d46SWarner Losh 				val = (const char *)cp;
402ca987d46SWarner Losh 			strlcpy(hostname, val, sizeof(hostname));
403ca987d46SWarner Losh 		}
404ca987d46SWarner Losh 		if (tag == TAG_INTF_MTU) {
405ca987d46SWarner Losh 			intf_mtu = 0;
406ca987d46SWarner Losh 			if ((val = getenv("dhcp.interface-mtu")) != NULL) {
407ca987d46SWarner Losh 				unsigned long tmp;
408ca987d46SWarner Losh 				char *end;
409ca987d46SWarner Losh 
410ca987d46SWarner Losh 				errno = 0;
411ca987d46SWarner Losh 				/*
412ca987d46SWarner Losh 				 * Do not allow MTU to exceed max IPv4 packet
413ca987d46SWarner Losh 				 * size, max value of 16-bit word.
414ca987d46SWarner Losh 				 */
415ca987d46SWarner Losh 				tmp = strtoul(val, &end, 0);
416ca987d46SWarner Losh 				if (errno != 0 ||
417ca987d46SWarner Losh 				    *val == '\0' || *end != '\0' ||
418ca987d46SWarner Losh 				    tmp > USHRT_MAX) {
419ca987d46SWarner Losh 					printf("%s: bad value: \"%s\", "
420ca987d46SWarner Losh 					    "ignoring\n",
421ca987d46SWarner Losh 					    "dhcp.interface-mtu", val);
422ca987d46SWarner Losh 				} else {
423ca987d46SWarner Losh 					intf_mtu = (u_int)tmp;
424ca987d46SWarner Losh 				}
425ca987d46SWarner Losh 			}
426ca987d46SWarner Losh 			if (intf_mtu <= 0)
427ca987d46SWarner Losh 				intf_mtu = be16dec(cp);
428ca987d46SWarner Losh 		}
429ca987d46SWarner Losh #ifdef SUPPORT_DHCP
430ca987d46SWarner Losh 		if (tag == TAG_DHCP_MSGTYPE) {
431ca987d46SWarner Losh 			if(*cp != expected_dhcpmsgtype)
432ca987d46SWarner Losh 			    return(-1);
433ca987d46SWarner Losh 			dhcp_ok = 1;
434ca987d46SWarner Losh 		}
435ca987d46SWarner Losh 		if (tag == TAG_SERVERID) {
436ca987d46SWarner Losh 			bcopy(cp, &dhcp_serverip.s_addr,
437ca987d46SWarner Losh 			      sizeof(dhcp_serverip.s_addr));
438ca987d46SWarner Losh 		}
439ca987d46SWarner Losh #endif
440ca987d46SWarner Losh 		cp += size;
441ca987d46SWarner Losh 	}
442ca987d46SWarner Losh 	return(0);
443ca987d46SWarner Losh }
444ca987d46SWarner Losh 
445ca987d46SWarner Losh #ifdef BOOTP_VEND_CMU
446ca987d46SWarner Losh static void
447ca987d46SWarner Losh vend_cmu(u_char *cp)
448ca987d46SWarner Losh {
449ca987d46SWarner Losh 	struct cmu_vend *vp;
450ca987d46SWarner Losh 
451ca987d46SWarner Losh #ifdef BOOTP_DEBUG
452ca987d46SWarner Losh 	if (debug)
453ca987d46SWarner Losh 		printf("vend_cmu bootp info.\n");
454ca987d46SWarner Losh #endif
455ca987d46SWarner Losh 	vp = (struct cmu_vend *)cp;
456ca987d46SWarner Losh 
457ca987d46SWarner Losh 	if (vp->v_smask.s_addr != 0) {
458ca987d46SWarner Losh 		netmask = vp->v_smask.s_addr;
459ca987d46SWarner Losh 	}
460ca987d46SWarner Losh 	if (vp->v_dgate.s_addr != 0) {
461ca987d46SWarner Losh 		gateip = vp->v_dgate;
462ca987d46SWarner Losh 	}
463ca987d46SWarner Losh }
464ca987d46SWarner Losh #endif
465ca987d46SWarner Losh 
466ca987d46SWarner Losh #ifdef DHCP_ENV
467ca987d46SWarner Losh /*
468ca987d46SWarner Losh  * Parse DHCP options and store them into kenv variables.
469ca987d46SWarner Losh  * Original code from Danny Braniss, modifications by Luigi Rizzo.
470ca987d46SWarner Losh  *
471ca987d46SWarner Losh  * The parser is driven by tables which specify the type and name of
472ca987d46SWarner Losh  * each dhcp option and how it appears in kenv.
473ca987d46SWarner Losh  * The first entry in the list contains the prefix used to set the kenv
474ca987d46SWarner Losh  * name (including the . if needed), the last entry must have a 0 tag.
475ca987d46SWarner Losh  * Entries do not need to be sorted though it helps for readability.
476ca987d46SWarner Losh  *
477ca987d46SWarner Losh  * Certain vendor-specific tables can be enabled according to DHCP_ENV.
478ca987d46SWarner Losh  * Set it to 0 if you don't want any.
479ca987d46SWarner Losh  */
480ca987d46SWarner Losh enum opt_fmt { __NONE = 0,
481ca987d46SWarner Losh 	__8 = 1, __16 = 2, __32 = 4,	/* Unsigned fields, value=size	*/
482ca987d46SWarner Losh 	__IP,				/* IPv4 address			*/
483ca987d46SWarner Losh 	__TXT,				/* C string			*/
484ca987d46SWarner Losh 	__BYTES,			/* byte sequence, printed %02x	*/
485ca987d46SWarner Losh 	__INDIR,			/* name=value			*/
486ca987d46SWarner Losh 	__ILIST,			/* name=value;name=value ... */
487ca987d46SWarner Losh 	__VE,				/* vendor specific, recurse	*/
488ca987d46SWarner Losh };
489ca987d46SWarner Losh 
490ca987d46SWarner Losh struct dhcp_opt {
491ca987d46SWarner Losh 	uint8_t	tag;
492ca987d46SWarner Losh 	uint8_t	fmt;
493ca987d46SWarner Losh 	const char	*desc;
494ca987d46SWarner Losh };
495ca987d46SWarner Losh 
496ca987d46SWarner Losh static struct dhcp_opt vndr_opt[] = { /* Vendor Specific Options */
497ca987d46SWarner Losh #if DHCP_ENV == DHCP_ENV_FREEBSD /* FreeBSD table in the original code */
498ca987d46SWarner Losh 	{0,	0,	"FreeBSD"},		/* prefix */
499ca987d46SWarner Losh 	{1,	__TXT,	"kernel"},
500ca987d46SWarner Losh 	{2,	__TXT,	"kernelname"},
501ca987d46SWarner Losh 	{3,	__TXT,	"kernel_options"},
502ca987d46SWarner Losh 	{4,	__IP,	"usr-ip"},
503ca987d46SWarner Losh 	{5,	__TXT,	"conf-path"},
504ca987d46SWarner Losh 	{6,	__TXT,	"rc.conf0"},
505ca987d46SWarner Losh 	{7,	__TXT,	"rc.conf1"},
506ca987d46SWarner Losh 	{8,	__TXT,	"rc.conf2"},
507ca987d46SWarner Losh 	{9,	__TXT,	"rc.conf3"},
508ca987d46SWarner Losh 	{10,	__TXT,	"rc.conf4"},
509ca987d46SWarner Losh 	{11,	__TXT,	"rc.conf5"},
510ca987d46SWarner Losh 	{12,	__TXT,	"rc.conf6"},
511ca987d46SWarner Losh 	{13,	__TXT,	"rc.conf7"},
512ca987d46SWarner Losh 	{14,	__TXT,	"rc.conf8"},
513ca987d46SWarner Losh 	{15,	__TXT,	"rc.conf9"},
514ca987d46SWarner Losh 
515ca987d46SWarner Losh 	{20,	__TXT,  "boot.nfsroot.options"},
516ca987d46SWarner Losh 
517ca987d46SWarner Losh 	{245,	__INDIR, ""},
518ca987d46SWarner Losh 	{246,	__INDIR, ""},
519ca987d46SWarner Losh 	{247,	__INDIR, ""},
520ca987d46SWarner Losh 	{248,	__INDIR, ""},
521ca987d46SWarner Losh 	{249,	__INDIR, ""},
522ca987d46SWarner Losh 	{250,	__INDIR, ""},
523ca987d46SWarner Losh 	{251,	__INDIR, ""},
524ca987d46SWarner Losh 	{252,	__INDIR, ""},
525ca987d46SWarner Losh 	{253,	__INDIR, ""},
526ca987d46SWarner Losh 	{254,	__INDIR, ""},
527ca987d46SWarner Losh 
528ca987d46SWarner Losh #elif DHCP_ENV == DHCP_ENV_PXE		/* some pxe options, RFC4578 */
529ca987d46SWarner Losh 	{0,	0,	"pxe"},		/* prefix */
530ca987d46SWarner Losh 	{93,	__16,	"system-architecture"},
531ca987d46SWarner Losh 	{94,	__BYTES,	"network-interface"},
532ca987d46SWarner Losh 	{97,	__BYTES,	"machine-identifier"},
533ca987d46SWarner Losh #else					/* default (empty) table */
534ca987d46SWarner Losh 	{0,	0,	"dhcp.vendor."},		/* prefix */
535ca987d46SWarner Losh #endif
536ca987d46SWarner Losh 	{0,	__TXT,	"%soption-%d"}
537ca987d46SWarner Losh };
538ca987d46SWarner Losh 
539ca987d46SWarner Losh static struct dhcp_opt dhcp_opt[] = {
540ca987d46SWarner Losh 	/* DHCP Option names, formats and codes, from RFC2132. */
541ca987d46SWarner Losh 	{0,	0,	"dhcp."},	// prefix
542ca987d46SWarner Losh 	{1,	__IP,	"subnet-mask"},
543ca987d46SWarner Losh 	{2,	__32,	"time-offset"}, /* this is signed */
544ca987d46SWarner Losh 	{3,	__IP,	"routers"},
545ca987d46SWarner Losh 	{4,	__IP,	"time-servers"},
546ca987d46SWarner Losh 	{5,	__IP,	"ien116-name-servers"},
547ca987d46SWarner Losh 	{6,	__IP,	"domain-name-servers"},
548ca987d46SWarner Losh 	{7,	__IP,	"log-servers"},
549ca987d46SWarner Losh 	{8,	__IP,	"cookie-servers"},
550ca987d46SWarner Losh 	{9,	__IP,	"lpr-servers"},
551ca987d46SWarner Losh 	{10,	__IP,	"impress-servers"},
552ca987d46SWarner Losh 	{11,	__IP,	"resource-location-servers"},
553ca987d46SWarner Losh 	{12,	__TXT,	"host-name"},
554ca987d46SWarner Losh 	{13,	__16,	"boot-size"},
555ca987d46SWarner Losh 	{14,	__TXT,	"merit-dump"},
556ca987d46SWarner Losh 	{15,	__TXT,	"domain-name"},
557ca987d46SWarner Losh 	{16,	__IP,	"swap-server"},
558ca987d46SWarner Losh 	{17,	__TXT,	"root-path"},
559ca987d46SWarner Losh 	{18,	__TXT,	"extensions-path"},
560ca987d46SWarner Losh 	{19,	__8,	"ip-forwarding"},
561ca987d46SWarner Losh 	{20,	__8,	"non-local-source-routing"},
562ca987d46SWarner Losh 	{21,	__IP,	"policy-filter"},
563ca987d46SWarner Losh 	{22,	__16,	"max-dgram-reassembly"},
564ca987d46SWarner Losh 	{23,	__8,	"default-ip-ttl"},
565ca987d46SWarner Losh 	{24,	__32,	"path-mtu-aging-timeout"},
566ca987d46SWarner Losh 	{25,	__16,	"path-mtu-plateau-table"},
567ca987d46SWarner Losh 	{26,	__16,	"interface-mtu"},
568ca987d46SWarner Losh 	{27,	__8,	"all-subnets-local"},
569ca987d46SWarner Losh 	{28,	__IP,	"broadcast-address"},
570ca987d46SWarner Losh 	{29,	__8,	"perform-mask-discovery"},
571ca987d46SWarner Losh 	{30,	__8,	"mask-supplier"},
572ca987d46SWarner Losh 	{31,	__8,	"perform-router-discovery"},
573ca987d46SWarner Losh 	{32,	__IP,	"router-solicitation-address"},
574ca987d46SWarner Losh 	{33,	__IP,	"static-routes"},
575ca987d46SWarner Losh 	{34,	__8,	"trailer-encapsulation"},
576ca987d46SWarner Losh 	{35,	__32,	"arp-cache-timeout"},
577ca987d46SWarner Losh 	{36,	__8,	"ieee802-3-encapsulation"},
578ca987d46SWarner Losh 	{37,	__8,	"default-tcp-ttl"},
579ca987d46SWarner Losh 	{38,	__32,	"tcp-keepalive-interval"},
580ca987d46SWarner Losh 	{39,	__8,	"tcp-keepalive-garbage"},
581ca987d46SWarner Losh 	{40,	__TXT,	"nis-domain"},
582ca987d46SWarner Losh 	{41,	__IP,	"nis-servers"},
583ca987d46SWarner Losh 	{42,	__IP,	"ntp-servers"},
584ca987d46SWarner Losh 	{43,	__VE,	"vendor-encapsulated-options"},
585ca987d46SWarner Losh 	{44,	__IP,	"netbios-name-servers"},
586ca987d46SWarner Losh 	{45,	__IP,	"netbios-dd-server"},
587ca987d46SWarner Losh 	{46,	__8,	"netbios-node-type"},
588ca987d46SWarner Losh 	{47,	__TXT,	"netbios-scope"},
589ca987d46SWarner Losh 	{48,	__IP,	"x-font-servers"},
590ca987d46SWarner Losh 	{49,	__IP,	"x-display-managers"},
591ca987d46SWarner Losh 	{50,	__IP,	"dhcp-requested-address"},
592ca987d46SWarner Losh 	{51,	__32,	"dhcp-lease-time"},
593ca987d46SWarner Losh 	{52,	__8,	"dhcp-option-overload"},
594ca987d46SWarner Losh 	{53,	__8,	"dhcp-message-type"},
595ca987d46SWarner Losh 	{54,	__IP,	"dhcp-server-identifier"},
596ca987d46SWarner Losh 	{55,	__8,	"dhcp-parameter-request-list"},
597ca987d46SWarner Losh 	{56,	__TXT,	"dhcp-message"},
598ca987d46SWarner Losh 	{57,	__16,	"dhcp-max-message-size"},
599ca987d46SWarner Losh 	{58,	__32,	"dhcp-renewal-time"},
600ca987d46SWarner Losh 	{59,	__32,	"dhcp-rebinding-time"},
601ca987d46SWarner Losh 	{60,	__TXT,	"vendor-class-identifier"},
602ca987d46SWarner Losh 	{61,	__TXT,	"dhcp-client-identifier"},
603ca987d46SWarner Losh 	{64,	__TXT,	"nisplus-domain"},
604ca987d46SWarner Losh 	{65,	__IP,	"nisplus-servers"},
605ca987d46SWarner Losh 	{66,	__TXT,	"tftp-server-name"},
606ca987d46SWarner Losh 	{67,	__TXT,	"bootfile-name"},
607ca987d46SWarner Losh 	{68,	__IP,	"mobile-ip-home-agent"},
608ca987d46SWarner Losh 	{69,	__IP,	"smtp-server"},
609ca987d46SWarner Losh 	{70,	__IP,	"pop-server"},
610ca987d46SWarner Losh 	{71,	__IP,	"nntp-server"},
611ca987d46SWarner Losh 	{72,	__IP,	"www-server"},
612ca987d46SWarner Losh 	{73,	__IP,	"finger-server"},
613ca987d46SWarner Losh 	{74,	__IP,	"irc-server"},
614ca987d46SWarner Losh 	{75,	__IP,	"streettalk-server"},
615ca987d46SWarner Losh 	{76,	__IP,	"streettalk-directory-assistance-server"},
616ca987d46SWarner Losh 	{77,	__TXT,	"user-class"},
617ca987d46SWarner Losh 	{85,	__IP,	"nds-servers"},
618ca987d46SWarner Losh 	{86,	__TXT,	"nds-tree-name"},
619ca987d46SWarner Losh 	{87,	__TXT,	"nds-context"},
620ca987d46SWarner Losh 	{210,	__TXT,	"authenticate"},
621ca987d46SWarner Losh 
622ca987d46SWarner Losh 	/* use the following entries for arbitrary variables */
623ca987d46SWarner Losh 	{246,	__ILIST, ""},
624ca987d46SWarner Losh 	{247,	__ILIST, ""},
625ca987d46SWarner Losh 	{248,	__ILIST, ""},
626ca987d46SWarner Losh 	{249,	__ILIST, ""},
627ca987d46SWarner Losh 	{250,	__INDIR, ""},
628ca987d46SWarner Losh 	{251,	__INDIR, ""},
629ca987d46SWarner Losh 	{252,	__INDIR, ""},
630ca987d46SWarner Losh 	{253,	__INDIR, ""},
631ca987d46SWarner Losh 	{254,	__INDIR, ""},
632ca987d46SWarner Losh 	{0,	__TXT,	"%soption-%d"}
633ca987d46SWarner Losh };
634ca987d46SWarner Losh 
635ca987d46SWarner Losh /*
636ca987d46SWarner Losh  * parse a dhcp response, set environment variables translating options
637ca987d46SWarner Losh  * names and values according to the tables above. Also set dhcp.tags
638ca987d46SWarner Losh  * to the list of selected tags.
639ca987d46SWarner Losh  */
640ca987d46SWarner Losh static void
641ca987d46SWarner Losh setenv_(u_char *cp,  u_char *ep, struct dhcp_opt *opts)
642ca987d46SWarner Losh {
643ca987d46SWarner Losh     u_char	*ncp;
644ca987d46SWarner Losh     u_char	tag;
645ca987d46SWarner Losh     char	tags[512], *tp;	/* the list of tags */
646ca987d46SWarner Losh 
647ca987d46SWarner Losh #define FLD_SEP	','	/* separator in list of elements */
648ca987d46SWarner Losh     ncp = cp;
649ca987d46SWarner Losh     tp = tags;
650ca987d46SWarner Losh     if (opts == NULL)
651ca987d46SWarner Losh 	opts = dhcp_opt;
652ca987d46SWarner Losh 
653ca987d46SWarner Losh     while (ncp < ep) {
654ca987d46SWarner Losh 	unsigned int	size;		/* option size */
655ca987d46SWarner Losh 	char *vp, *endv, buf[256];	/* the value buffer */
656ca987d46SWarner Losh 	struct dhcp_opt *op;
657ca987d46SWarner Losh 
658ca987d46SWarner Losh 	tag = *ncp++;			/* extract tag and size */
659ca987d46SWarner Losh 	size = *ncp++;
660ca987d46SWarner Losh 	cp = ncp;			/* current payload */
661ca987d46SWarner Losh 	ncp += size;			/* point to the next option */
662ca987d46SWarner Losh 
663ca987d46SWarner Losh 	if (tag == TAG_END)
664ca987d46SWarner Losh 	    break;
665ca987d46SWarner Losh 	if (tag == 0)
666ca987d46SWarner Losh 	    continue;
667ca987d46SWarner Losh 
668ca987d46SWarner Losh 	for (op = opts+1; op->tag && op->tag != tag; op++)
669ca987d46SWarner Losh 		;
670ca987d46SWarner Losh 	/* if not found we end up on the default entry */
671ca987d46SWarner Losh 
672ca987d46SWarner Losh 	/*
673ca987d46SWarner Losh 	 * Copy data into the buffer. libstand does not have snprintf so we
674ca987d46SWarner Losh 	 * need to be careful with sprintf(). With strings, the source is
675ca987d46SWarner Losh 	 * always <256 char so shorter than the buffer so we are safe; with
676ca987d46SWarner Losh 	 * other arguments, the longest string is inet_ntoa which is 16 bytes
677ca987d46SWarner Losh 	 * so we make sure to have always enough room in the string before
678ca987d46SWarner Losh 	 * trying an sprint.
679ca987d46SWarner Losh 	 */
680ca987d46SWarner Losh 	vp = buf;
681ca987d46SWarner Losh 	*vp = '\0';
682ca987d46SWarner Losh 	endv = buf + sizeof(buf) - 1 - 16;	/* last valid write position */
683ca987d46SWarner Losh 
684ca987d46SWarner Losh 	switch(op->fmt) {
685ca987d46SWarner Losh 	case __NONE:
686ca987d46SWarner Losh 	    break;	/* should not happen */
687ca987d46SWarner Losh 
688ca987d46SWarner Losh 	case __VE: /* recurse, vendor specific */
689ca987d46SWarner Losh 	    setenv_(cp, cp+size, vndr_opt);
690ca987d46SWarner Losh 	    break;
691ca987d46SWarner Losh 
692ca987d46SWarner Losh 	case __IP:	/* ip address */
693ca987d46SWarner Losh 	    for (; size > 0 && vp < endv; size -= 4, cp += 4) {
694ca987d46SWarner Losh 		struct	in_addr in_ip;		/* ip addresses */
695ca987d46SWarner Losh 		if (vp != buf)
696ca987d46SWarner Losh 		    *vp++ = FLD_SEP;
697ca987d46SWarner Losh 		bcopy(cp, &in_ip.s_addr, sizeof(in_ip.s_addr));
698ca987d46SWarner Losh 		sprintf(vp, "%s", inet_ntoa(in_ip));
699ca987d46SWarner Losh 		vp += strlen(vp);
700ca987d46SWarner Losh 	    }
701ca987d46SWarner Losh 	    break;
702ca987d46SWarner Losh 
703ca987d46SWarner Losh 	case __BYTES:	/* opaque byte string */
704ca987d46SWarner Losh 	    for (; size > 0 && vp < endv; size -= 1, cp += 1) {
705ca987d46SWarner Losh 		sprintf(vp, "%02x", *cp);
706ca987d46SWarner Losh 		vp += strlen(vp);
707ca987d46SWarner Losh 	    }
708ca987d46SWarner Losh 	    break;
709ca987d46SWarner Losh 
710ca987d46SWarner Losh 	case __TXT:
711ca987d46SWarner Losh 	    bcopy(cp, buf, size);	/* cannot overflow */
712ca987d46SWarner Losh 	    buf[size] = 0;
713ca987d46SWarner Losh 	    break;
714ca987d46SWarner Losh 
715ca987d46SWarner Losh 	case __32:
716ca987d46SWarner Losh 	case __16:
717ca987d46SWarner Losh 	case __8:	/* op->fmt is also the length of each field */
718ca987d46SWarner Losh 	    for (; size > 0 && vp < endv; size -= op->fmt, cp += op->fmt) {
719ca987d46SWarner Losh 		uint32_t v;
720ca987d46SWarner Losh 		if (op->fmt == __32)
721ca987d46SWarner Losh 			v = (cp[0]<<24) + (cp[1]<<16) + (cp[2]<<8) + cp[3];
722ca987d46SWarner Losh 		else if (op->fmt == __16)
723ca987d46SWarner Losh 			v = (cp[0]<<8) + cp[1];
724ca987d46SWarner Losh 		else
725ca987d46SWarner Losh 			v = cp[0];
726ca987d46SWarner Losh 		if (vp != buf)
727ca987d46SWarner Losh 		    *vp++ = FLD_SEP;
728ca987d46SWarner Losh 		sprintf(vp, "%u", v);
729ca987d46SWarner Losh 		vp += strlen(vp);
730ca987d46SWarner Losh 	    }
731ca987d46SWarner Losh 	    break;
732ca987d46SWarner Losh 
733ca987d46SWarner Losh 	case __INDIR:	/* name=value */
734ca987d46SWarner Losh 	case __ILIST:	/* name=value;name=value... */
735ca987d46SWarner Losh 	    bcopy(cp, buf, size);	/* cannot overflow */
736ca987d46SWarner Losh 	    buf[size] = '\0';
737ca987d46SWarner Losh 	    for (endv = buf; endv; endv = vp) {
738ca987d46SWarner Losh 		u_char *s = NULL;	/* semicolon ? */
739ca987d46SWarner Losh 
740ca987d46SWarner Losh 		/* skip leading whitespace */
741ca987d46SWarner Losh 		while (*endv && strchr(" \t\n\r", *endv))
742ca987d46SWarner Losh 		    endv++;
743ca987d46SWarner Losh 		vp = strchr(endv, '=');	/* find name=value separator */
744ca987d46SWarner Losh 		if (!vp)
745ca987d46SWarner Losh 		    break;
746ca987d46SWarner Losh 		*vp++ = 0;
747ca987d46SWarner Losh 		if (op->fmt == __ILIST && (s = strchr(vp, ';')))
748ca987d46SWarner Losh 		    *s++ = '\0';
749ca987d46SWarner Losh 		setenv(endv, vp, 1);
750ca987d46SWarner Losh 		vp = s;	/* prepare for next round */
751ca987d46SWarner Losh 	    }
752ca987d46SWarner Losh 	    buf[0] = '\0';	/* option already done */
753ca987d46SWarner Losh 	}
754ca987d46SWarner Losh 
755ca987d46SWarner Losh 	if (tp - tags < sizeof(tags) - 5) {	/* add tag to the list */
756ca987d46SWarner Losh 	    if (tp != tags)
757ca987d46SWarner Losh 		*tp++ = FLD_SEP;
758ca987d46SWarner Losh 	    sprintf(tp, "%d", tag);
759ca987d46SWarner Losh 	    tp += strlen(tp);
760ca987d46SWarner Losh 	}
761ca987d46SWarner Losh 	if (buf[0]) {
762ca987d46SWarner Losh 	    char	env[128];	/* the string name */
763ca987d46SWarner Losh 
764ca987d46SWarner Losh 	    if (op->tag == 0)
765ca987d46SWarner Losh 		sprintf(env, op->desc, opts[0].desc, tag);
766ca987d46SWarner Losh 	    else
767ca987d46SWarner Losh 		sprintf(env, "%s%s", opts[0].desc, op->desc);
768ca987d46SWarner Losh 	    /*
769ca987d46SWarner Losh 	     * Do not replace existing values in the environment, so that
770ca987d46SWarner Losh 	     * locally-obtained values can override server-provided values.
771ca987d46SWarner Losh 	     */
772ca987d46SWarner Losh 	    setenv(env, buf, 0);
773ca987d46SWarner Losh 	}
774ca987d46SWarner Losh     }
775ca987d46SWarner Losh     if (tp != tags) {
776ca987d46SWarner Losh 	char	env[128];	/* the string name */
777ca987d46SWarner Losh 	sprintf(env, "%stags", opts[0].desc);
778ca987d46SWarner Losh 	setenv(env, tags, 1);
779ca987d46SWarner Losh     }
780ca987d46SWarner Losh }
781ca987d46SWarner Losh #endif /* additional dhcp */
782