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