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