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