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