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