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