7987 loader: pxeboot should avoid unnecessary DHCP request
[unleashed.git] / usr / src / boot / lib / libstand / bootp.c
blobeffe074dce297001543bce7485c8adc3cc8bc46c
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] = 8;
149 bp->bp_vend[20] = TAG_ROOTPATH;
150 bp->bp_vend[21] = TAG_TFTP_SERVER;
151 bp->bp_vend[22] = TAG_HOSTNAME;
152 bp->bp_vend[23] = TAG_SWAPSERVER;
153 bp->bp_vend[24] = TAG_GATEWAY;
154 bp->bp_vend[25] = TAG_SUBNET_MASK;
155 bp->bp_vend[26] = TAG_INTF_MTU;
156 bp->bp_vend[27] = TAG_SERVERID;
157 bp->bp_vend[28] = TAG_END;
158 } else
159 bp->bp_vend[7] = TAG_END;
160 #else
161 bp->bp_vend[4] = TAG_END;
162 #endif
164 d->myip.s_addr = INADDR_ANY;
165 d->myport = htons(IPPORT_BOOTPC);
166 d->destip.s_addr = INADDR_BROADCAST;
167 d->destport = htons(IPPORT_BOOTPS);
169 #ifdef SUPPORT_DHCP
170 expected_dhcpmsgtype = DHCPOFFER;
171 dhcp_ok = 0;
172 #endif
174 if(sendrecv(d,
175 bootpsend, bp, sizeof(*bp),
176 bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp))
177 == -1) {
178 printf("bootp: no reply\n");
179 return;
182 #ifdef SUPPORT_DHCP
183 if(dhcp_ok) {
184 u_int32_t leasetime;
185 bp->bp_vend[6] = DHCPREQUEST;
186 bp->bp_vend[7] = TAG_REQ_ADDR;
187 bp->bp_vend[8] = 4;
188 bcopy(&rbuf.rbootp.bp_yiaddr, &bp->bp_vend[9], 4);
189 bp->bp_vend[13] = TAG_SERVERID;
190 bp->bp_vend[14] = 4;
191 bcopy(&dhcp_serverip.s_addr, &bp->bp_vend[15], 4);
192 bp->bp_vend[19] = TAG_LEASETIME;
193 bp->bp_vend[20] = 4;
194 leasetime = htonl(300);
195 bcopy(&leasetime, &bp->bp_vend[21], 4);
196 if (flag & BOOTP_PXE) {
197 bp->bp_vend[25] = TAG_CLASSID;
198 bp->bp_vend[26] = 9;
199 bcopy("PXEClient", &bp->bp_vend[27], 9);
200 bp->bp_vend[36] = TAG_END;
201 } else
202 bp->bp_vend[25] = TAG_END;
204 expected_dhcpmsgtype = DHCPACK;
206 if(sendrecv(d,
207 bootpsend, bp, sizeof(*bp),
208 bootprecv, &rbuf.rbootp, sizeof(rbuf.rbootp))
209 == -1) {
210 printf("DHCPREQUEST failed\n");
211 return;
214 #endif
216 myip = d->myip = rbuf.rbootp.bp_yiaddr;
217 servip = rbuf.rbootp.bp_siaddr;
218 if(rootip.s_addr == INADDR_ANY) rootip = servip;
219 bcopy(rbuf.rbootp.bp_file, bootfile, sizeof(bootfile));
220 bootfile[sizeof(bootfile) - 1] = '\0';
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
235 #ifdef BOOTP_DEBUG
236 if (debug)
237 printf("mask: %s\n", intoa(netmask));
238 #endif
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
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;
257 /* Bump xid so next request will be unique. */
258 ++d->xid;
261 /* Transmit a bootp request */
262 static ssize_t
263 bootpsend(struct iodesc *d, void *pkt, size_t len)
265 struct bootp *bp;
267 #ifdef BOOTP_DEBUG
268 if (debug)
269 printf("bootpsend: d=%lx called.\n", (long)d);
270 #endif
272 bp = pkt;
273 bp->bp_secs = htons((u_short)(getsecs() - bot));
275 #ifdef BOOTP_DEBUG
276 if (debug)
277 printf("bootpsend: calling sendudp\n");
278 #endif
280 return (sendudp(d, pkt, len));
283 static ssize_t
284 bootprecv(struct iodesc *d, void *pkt, size_t len, time_t tleft)
286 ssize_t n;
287 struct bootp *bp;
289 #ifdef BOOTP_DEBUGx
290 if (debug)
291 printf("bootp_recvoffer: called\n");
292 #endif
294 n = readudp(d, pkt, len, tleft);
295 if (n == -1 || n < sizeof(struct bootp) - BOOTP_VENDSIZE)
296 goto bad;
298 bp = (struct bootp *)pkt;
300 #ifdef BOOTP_DEBUG
301 if (debug)
302 printf("bootprecv: checked. bp = 0x%lx, n = %d\n",
303 (long)bp, (int)n);
304 #endif
305 if (bp->bp_xid != htonl(d->xid)) {
306 #ifdef BOOTP_DEBUG
307 if (debug) {
308 printf("bootprecv: expected xid 0x%lx, got 0x%x\n",
309 d->xid, ntohl(bp->bp_xid));
311 #endif
312 goto bad;
315 #ifdef BOOTP_DEBUG
316 if (debug)
317 printf("bootprecv: got one!\n");
318 #endif
320 /* Suck out vendor info */
321 if (bcmp(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)) == 0) {
322 if(vend_rfc1048(bp->bp_vend, sizeof(bp->bp_vend)) != 0)
323 goto bad;
325 #ifdef BOOTP_VEND_CMU
326 else if (bcmp(vm_cmu, bp->bp_vend, sizeof(vm_cmu)) == 0)
327 vend_cmu(bp->bp_vend);
328 #endif
329 else
330 printf("bootprecv: unknown vendor 0x%lx\n", (long)bp->bp_vend);
332 return(n);
333 bad:
334 errno = 0;
335 return (-1);
339 dhcp_try_rfc1048(uint8_t *cp, size_t len)
342 expected_dhcpmsgtype = DHCPACK;
343 if (bcmp(vm_rfc1048, cp, sizeof (vm_rfc1048)) == 0) {
344 return (vend_rfc1048(cp, len));
346 return (-1);
349 static int
350 vend_rfc1048(u_char *cp, u_int len)
352 u_char *ep;
353 int size;
354 u_char tag;
355 const char *val;
357 #ifdef BOOTP_DEBUG
358 if (debug)
359 printf("vend_rfc1048 bootp info. len=%d\n", len);
360 #endif
361 ep = cp + len;
363 /* Step over magic cookie */
364 cp += sizeof(int);
366 setenv_(cp, ep, NULL);
368 while (cp < ep) {
369 tag = *cp++;
370 size = *cp++;
371 if (tag == TAG_END)
372 break;
374 if (tag == TAG_SUBNET_MASK) {
375 bcopy(cp, &netmask, sizeof(netmask));
377 if (tag == TAG_GATEWAY) {
378 bcopy(cp, &gateip.s_addr, sizeof(gateip.s_addr));
380 if (tag == TAG_SWAPSERVER) {
381 /* let it override bp_siaddr */
382 bcopy(cp, &rootip.s_addr, sizeof(rootip.s_addr));
384 if (tag == TAG_ROOTPATH) {
385 if ((val = getenv("dhcp.root-path")) == NULL)
386 val = (const char *)cp;
387 strlcpy(rootpath, val, sizeof(rootpath));
389 if (tag == TAG_HOSTNAME) {
390 if ((val = getenv("dhcp.host-name")) == NULL)
391 val = (const char *)cp;
392 strlcpy(hostname, val, sizeof(hostname));
394 if (tag == TAG_INTF_MTU) {
395 intf_mtu = 0;
396 if ((val = getenv("dhcp.interface-mtu")) != NULL) {
397 unsigned long tmp;
398 char *end;
400 errno = 0;
402 * Do not allow MTU to exceed max IPv4 packet
403 * size, max value of 16-bit word.
405 tmp = strtoul(val, &end, 0);
406 if (errno != 0 ||
407 *val == '\0' || *end != '\0' ||
408 tmp > USHRT_MAX) {
409 printf("%s: bad value: \"%s\", "
410 "ignoring\n",
411 "dhcp.interface-mtu", val);
412 } else {
413 intf_mtu = (u_int)tmp;
416 if (intf_mtu == 0)
417 intf_mtu = be16dec(cp);
419 #ifdef SUPPORT_DHCP
420 if (tag == TAG_DHCP_MSGTYPE) {
421 if(*cp != expected_dhcpmsgtype)
422 return(-1);
423 dhcp_ok = 1;
425 if (tag == TAG_SERVERID) {
426 bcopy(cp, &dhcp_serverip.s_addr,
427 sizeof(dhcp_serverip.s_addr));
429 if (tag == TAG_TFTP_SERVER) {
430 bcopy(cp, &tftpip.s_addr,
431 sizeof(tftpip.s_addr));
433 #endif
434 cp += size;
436 return(0);
439 #ifdef BOOTP_VEND_CMU
440 static void
441 vend_cmu(u_char *cp)
443 struct cmu_vend *vp;
445 #ifdef BOOTP_DEBUG
446 if (debug)
447 printf("vend_cmu bootp info.\n");
448 #endif
449 vp = (struct cmu_vend *)cp;
451 if (vp->v_smask.s_addr != 0) {
452 netmask = vp->v_smask.s_addr;
454 if (vp->v_dgate.s_addr != 0) {
455 gateip = vp->v_dgate;
458 #endif
460 #ifdef DHCP_ENV
462 * Parse DHCP options and store them into kenv variables.
463 * Original code from Danny Braniss, modifications by Luigi Rizzo.
465 * The parser is driven by tables which specify the type and name of
466 * each dhcp option and how it appears in kenv.
467 * The first entry in the list contains the prefix used to set the kenv
468 * name (including the . if needed), the last entry must have a 0 tag.
469 * Entries do not need to be sorted though it helps for readability.
471 * Certain vendor-specific tables can be enabled according to DHCP_ENV.
472 * Set it to 0 if you don't want any.
474 enum opt_fmt { __NONE = 0,
475 __8 = 1, __16 = 2, __32 = 4, /* Unsigned fields, value=size */
476 __IP, /* IPv4 address */
477 __TXT, /* C string */
478 __BYTES, /* byte sequence, printed %02x */
479 __INDIR, /* name=value */
480 __ILIST, /* name=value;name=value ... */
481 __VE, /* vendor specific, recurse */
484 struct dhcp_opt {
485 uint8_t tag;
486 uint8_t fmt;
487 const char *desc;
490 static struct dhcp_opt vndr_opt[] = { /* Vendor Specific Options */
491 #if DHCP_ENV == DHCP_ENV_FREEBSD /* FreeBSD table in the original code */
492 {0, 0, "FreeBSD"}, /* prefix */
493 {1, __TXT, "kernel"},
494 {2, __TXT, "kernelname"},
495 {3, __TXT, "kernel_options"},
496 {4, __IP, "usr-ip"},
497 {5, __TXT, "conf-path"},
498 {6, __TXT, "rc.conf0"},
499 {7, __TXT, "rc.conf1"},
500 {8, __TXT, "rc.conf2"},
501 {9, __TXT, "rc.conf3"},
502 {10, __TXT, "rc.conf4"},
503 {11, __TXT, "rc.conf5"},
504 {12, __TXT, "rc.conf6"},
505 {13, __TXT, "rc.conf7"},
506 {14, __TXT, "rc.conf8"},
507 {15, __TXT, "rc.conf9"},
509 {20, __TXT, "boot.nfsroot.options"},
511 {245, __INDIR, ""},
512 {246, __INDIR, ""},
513 {247, __INDIR, ""},
514 {248, __INDIR, ""},
515 {249, __INDIR, ""},
516 {250, __INDIR, ""},
517 {251, __INDIR, ""},
518 {252, __INDIR, ""},
519 {253, __INDIR, ""},
520 {254, __INDIR, ""},
522 #elif DHCP_ENV == DHCP_ENV_PXE /* some pxe options, RFC4578 */
523 {0, 0, "pxe"}, /* prefix */
524 {93, __16, "system-architecture"},
525 {94, __BYTES, "network-interface"},
526 {97, __BYTES, "machine-identifier"},
527 #else /* default (empty) table */
528 {0, 0, "dhcp.vendor."}, /* prefix */
529 #endif
530 {0, __TXT, "%soption-%d"}
533 static struct dhcp_opt dhcp_opt[] = {
534 /* DHCP Option names, formats and codes, from RFC2132. */
535 {0, 0, "dhcp."}, // prefix
536 {1, __IP, "subnet-mask"},
537 {2, __32, "time-offset"}, /* this is signed */
538 {3, __IP, "routers"},
539 {4, __IP, "time-servers"},
540 {5, __IP, "ien116-name-servers"},
541 {6, __IP, "domain-name-servers"},
542 {7, __IP, "log-servers"},
543 {8, __IP, "cookie-servers"},
544 {9, __IP, "lpr-servers"},
545 {10, __IP, "impress-servers"},
546 {11, __IP, "resource-location-servers"},
547 {12, __TXT, "host-name"},
548 {13, __16, "boot-size"},
549 {14, __TXT, "merit-dump"},
550 {15, __TXT, "domain-name"},
551 {16, __IP, "swap-server"},
552 {17, __TXT, "root-path"},
553 {18, __TXT, "extensions-path"},
554 {19, __8, "ip-forwarding"},
555 {20, __8, "non-local-source-routing"},
556 {21, __IP, "policy-filter"},
557 {22, __16, "max-dgram-reassembly"},
558 {23, __8, "default-ip-ttl"},
559 {24, __32, "path-mtu-aging-timeout"},
560 {25, __16, "path-mtu-plateau-table"},
561 {26, __16, "interface-mtu"},
562 {27, __8, "all-subnets-local"},
563 {28, __IP, "broadcast-address"},
564 {29, __8, "perform-mask-discovery"},
565 {30, __8, "mask-supplier"},
566 {31, __8, "perform-router-discovery"},
567 {32, __IP, "router-solicitation-address"},
568 {33, __IP, "static-routes"},
569 {34, __8, "trailer-encapsulation"},
570 {35, __32, "arp-cache-timeout"},
571 {36, __8, "ieee802-3-encapsulation"},
572 {37, __8, "default-tcp-ttl"},
573 {38, __32, "tcp-keepalive-interval"},
574 {39, __8, "tcp-keepalive-garbage"},
575 {40, __TXT, "nis-domain"},
576 {41, __IP, "nis-servers"},
577 {42, __IP, "ntp-servers"},
578 {43, __VE, "vendor-encapsulated-options"},
579 {44, __IP, "netbios-name-servers"},
580 {45, __IP, "netbios-dd-server"},
581 {46, __8, "netbios-node-type"},
582 {47, __TXT, "netbios-scope"},
583 {48, __IP, "x-font-servers"},
584 {49, __IP, "x-display-managers"},
585 {50, __IP, "dhcp-requested-address"},
586 {51, __32, "dhcp-lease-time"},
587 {52, __8, "dhcp-option-overload"},
588 {53, __8, "dhcp-message-type"},
589 {54, __IP, "dhcp-server-identifier"},
590 {55, __8, "dhcp-parameter-request-list"},
591 {56, __TXT, "dhcp-message"},
592 {57, __16, "dhcp-max-message-size"},
593 {58, __32, "dhcp-renewal-time"},
594 {59, __32, "dhcp-rebinding-time"},
595 {60, __TXT, "vendor-class-identifier"},
596 {61, __TXT, "dhcp-client-identifier"},
597 {64, __TXT, "nisplus-domain"},
598 {65, __IP, "nisplus-servers"},
599 {66, __TXT, "tftp-server-name"},
600 {67, __TXT, "bootfile-name"},
601 {68, __IP, "mobile-ip-home-agent"},
602 {69, __IP, "smtp-server"},
603 {70, __IP, "pop-server"},
604 {71, __IP, "nntp-server"},
605 {72, __IP, "www-server"},
606 {73, __IP, "finger-server"},
607 {74, __IP, "irc-server"},
608 {75, __IP, "streettalk-server"},
609 {76, __IP, "streettalk-directory-assistance-server"},
610 {77, __TXT, "user-class"},
611 {85, __IP, "nds-servers"},
612 {86, __TXT, "nds-tree-name"},
613 {87, __TXT, "nds-context"},
614 {210, __TXT, "authenticate"},
616 /* use the following entries for arbitrary variables */
617 {246, __ILIST, ""},
618 {247, __ILIST, ""},
619 {248, __ILIST, ""},
620 {249, __ILIST, ""},
621 {250, __INDIR, ""},
622 {251, __INDIR, ""},
623 {252, __INDIR, ""},
624 {253, __INDIR, ""},
625 {254, __INDIR, ""},
626 {0, __TXT, "%soption-%d"}
630 * parse a dhcp response, set environment variables translating options
631 * names and values according to the tables above. Also set dhcp.tags
632 * to the list of selected tags.
634 static void
635 setenv_(u_char *cp, u_char *ep, struct dhcp_opt *opts)
637 u_char *ncp;
638 u_char tag;
639 char tags[512], *tp; /* the list of tags */
641 #define FLD_SEP ',' /* separator in list of elements */
642 ncp = cp;
643 tp = tags;
644 if (opts == NULL)
645 opts = dhcp_opt;
647 while (ncp < ep) {
648 unsigned int size; /* option size */
649 char *vp, *endv, buf[256]; /* the value buffer */
650 struct dhcp_opt *op;
652 tag = *ncp++; /* extract tag and size */
653 size = *ncp++;
654 cp = ncp; /* current payload */
655 ncp += size; /* point to the next option */
657 if (tag == TAG_END)
658 break;
659 if (tag == 0)
660 continue;
662 for (op = opts+1; op->tag && op->tag != tag; op++)
664 /* if not found we end up on the default entry */
667 * Copy data into the buffer. libstand does not have snprintf so we
668 * need to be careful with sprintf(). With strings, the source is
669 * always <256 char so shorter than the buffer so we are safe; with
670 * other arguments, the longest string is inet_ntoa which is 16 bytes
671 * so we make sure to have always enough room in the string before
672 * trying an sprint.
674 vp = buf;
675 *vp = '\0';
676 endv = buf + sizeof(buf) - 1 - 16; /* last valid write position */
678 switch(op->fmt) {
679 case __NONE:
680 break; /* should not happen */
682 case __VE: /* recurse, vendor specific */
683 setenv_(cp, cp+size, vndr_opt);
684 break;
686 case __IP: /* ip address */
687 for (; size > 0 && vp < endv; size -= 4, cp += 4) {
688 struct in_addr in_ip; /* ip addresses */
689 if (vp != buf)
690 *vp++ = FLD_SEP;
691 bcopy(cp, &in_ip.s_addr, sizeof(in_ip.s_addr));
692 sprintf(vp, "%s", inet_ntoa(in_ip));
693 vp += strlen(vp);
695 break;
697 case __BYTES: /* opaque byte string */
698 for (; size > 0 && vp < endv; size -= 1, cp += 1) {
699 sprintf(vp, "%02x", *cp);
700 vp += strlen(vp);
702 break;
704 case __TXT:
705 bcopy(cp, buf, size); /* cannot overflow */
706 buf[size] = 0;
707 break;
709 case __32:
710 case __16:
711 case __8: /* op->fmt is also the length of each field */
712 for (; size > 0 && vp < endv; size -= op->fmt, cp += op->fmt) {
713 uint32_t v;
714 if (op->fmt == __32)
715 v = (cp[0]<<24) + (cp[1]<<16) + (cp[2]<<8) + cp[3];
716 else if (op->fmt == __16)
717 v = (cp[0]<<8) + cp[1];
718 else
719 v = cp[0];
720 if (vp != buf)
721 *vp++ = FLD_SEP;
722 sprintf(vp, "%u", v);
723 vp += strlen(vp);
725 break;
727 case __INDIR: /* name=value */
728 case __ILIST: /* name=value;name=value... */
729 bcopy(cp, buf, size); /* cannot overflow */
730 buf[size] = '\0';
731 for (endv = buf; endv; endv = vp) {
732 u_char *s = NULL; /* semicolon ? */
734 /* skip leading whitespace */
735 while (*endv && strchr(" \t\n\r", *endv))
736 endv++;
737 vp = strchr(endv, '='); /* find name=value separator */
738 if (!vp)
739 break;
740 *vp++ = 0;
741 if (op->fmt == __ILIST && (s = strchr(vp, ';')))
742 *s++ = '\0';
743 setenv(endv, vp, 1);
744 vp = s; /* prepare for next round */
746 buf[0] = '\0'; /* option already done */
749 if (tp - tags < sizeof(tags) - 5) { /* add tag to the list */
750 if (tp != tags)
751 *tp++ = FLD_SEP;
752 sprintf(tp, "%d", tag);
753 tp += strlen(tp);
755 if (buf[0]) {
756 char env[128]; /* the string name */
758 if (op->tag == 0)
759 sprintf(env, op->desc, opts[0].desc, tag);
760 else
761 sprintf(env, "%s%s", opts[0].desc, op->desc);
763 * Do not replace existing values in the environment, so that
764 * locally-obtained values can override server-provided values.
766 setenv(env, buf, 0);
769 if (tp != tags) {
770 char env[128]; /* the string name */
771 sprintf(env, "%stags", opts[0].desc);
772 setenv(env, tags, 1);
775 #endif /* additional dhcp */