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