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