8140 loader: network read rework
[unleashed.git] / usr / src / boot / lib / libstand / bootp.c
blobe4bbccfcaf021b783c9babca7a3fb1bc2bf82e0e
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 <stddef.h>
41 #include <sys/types.h>
42 #include <sys/limits.h>
43 #include <sys/endian.h>
44 #include <netinet/in.h>
45 #include <netinet/in_systm.h>
47 #include <string.h>
49 #define BOOTP_DEBUGxx
50 #define SUPPORT_DHCP
52 #define DHCP_ENV_NOVENDOR 1 /* do not parse vendor options */
53 #define DHCP_ENV_PXE 10 /* assume pxe vendor options */
54 #define DHCP_ENV_FREEBSD 11 /* assume freebsd vendor options */
55 /* set DHCP_ENV to one of the values above to export dhcp options to kenv */
56 #define DHCP_ENV DHCP_ENV_NO_VENDOR
58 #include "stand.h"
59 #include "net.h"
60 #include "netif.h"
61 #include "bootp.h"
64 struct in_addr servip;
66 static time_t bot;
68 static char vm_rfc1048[4] = VM_RFC1048;
69 #ifdef BOOTP_VEND_CMU
70 static char vm_cmu[4] = VM_CMU;
71 #endif
73 /* Local forwards */
74 static ssize_t bootpsend(struct iodesc *, void *, size_t);
75 static ssize_t bootprecv(struct iodesc *, void **, void **, time_t);
76 static int vend_rfc1048(u_char *, u_int);
77 #ifdef BOOTP_VEND_CMU
78 static void vend_cmu(u_char *);
79 #endif
81 #ifdef DHCP_ENV /* export the dhcp response to kenv */
82 struct dhcp_opt;
83 static void setenv_(u_char *cp, u_char *ep, struct dhcp_opt *opts);
84 #else
85 #define setenv_(a, b, c)
86 #endif
88 #ifdef SUPPORT_DHCP
89 static char expected_dhcpmsgtype = -1, dhcp_ok;
90 struct in_addr dhcp_serverip;
91 #endif
92 struct bootp *bootp_response;
93 size_t bootp_response_size;
95 /* Fetch required bootp infomation */
96 void
97 bootp(int sock, int flag)
99 void *pkt;
100 struct iodesc *d;
101 struct bootp *bp;
102 struct {
103 u_char header[HEADER_SIZE];
104 struct bootp wbootp;
105 } wbuf;
106 struct bootp *rbootp;
108 #ifdef BOOTP_DEBUG
109 if (debug)
110 printf("bootp: socket=%d\n", sock);
111 #endif
112 if (!bot)
113 bot = getsecs();
115 if (!(d = socktodesc(sock))) {
116 printf("bootp: bad socket. %d\n", sock);
117 return;
119 #ifdef BOOTP_DEBUG
120 if (debug)
121 printf("bootp: d=%lx\n", (long)d);
122 #endif
124 bp = &wbuf.wbootp;
125 bzero(bp, sizeof(*bp));
127 bp->bp_op = BOOTREQUEST;
128 bp->bp_htype = 1; /* 10Mb Ethernet (48 bits) */
129 bp->bp_hlen = 6;
130 bp->bp_xid = htonl(d->xid);
131 MACPY(d->myea, bp->bp_chaddr);
132 strncpy(bp->bp_file, bootfile, sizeof(bp->bp_file));
133 bcopy(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048));
134 #ifdef SUPPORT_DHCP
135 bp->bp_vend[4] = TAG_DHCP_MSGTYPE;
136 bp->bp_vend[5] = 1;
137 bp->bp_vend[6] = DHCPDISCOVER;
140 * If we are booting from PXE, we want to send the string
141 * 'PXEClient' to the DHCP server so you have the option of
142 * only responding to PXE aware dhcp requests.
144 if (flag & BOOTP_PXE) {
145 bp->bp_vend[7] = TAG_CLASSID;
146 bp->bp_vend[8] = 9;
147 bcopy("PXEClient", &bp->bp_vend[9], 9);
148 bp->bp_vend[18] = TAG_PARAM_REQ;
149 bp->bp_vend[19] = 7;
150 bp->bp_vend[20] = TAG_ROOTPATH;
151 bp->bp_vend[21] = TAG_HOSTNAME;
152 bp->bp_vend[22] = TAG_SWAPSERVER;
153 bp->bp_vend[23] = TAG_GATEWAY;
154 bp->bp_vend[24] = TAG_SUBNET_MASK;
155 bp->bp_vend[25] = TAG_INTF_MTU;
156 bp->bp_vend[26] = TAG_SERVERID;
157 bp->bp_vend[27] = 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, &pkt, (void **)&rbootp) == -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(&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 free(pkt);
206 if(sendrecv(d,
207 bootpsend, bp, sizeof(*bp),
208 bootprecv, &pkt, (void **)&rbootp) == -1) {
209 printf("DHCPREQUEST failed\n");
210 return;
213 #endif
215 myip = d->myip = rbootp->bp_yiaddr;
216 servip = rbootp->bp_siaddr;
217 if (rootip.s_addr == INADDR_ANY)
218 rootip = servip;
219 bcopy(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;
259 free(pkt);
262 /* Transmit a bootp request */
263 static ssize_t
264 bootpsend(struct iodesc *d, void *pkt, size_t len)
266 struct bootp *bp;
268 #ifdef BOOTP_DEBUG
269 if (debug)
270 printf("bootpsend: d=%lx called.\n", (long)d);
271 #endif
273 bp = pkt;
274 bp->bp_secs = htons((u_short)(getsecs() - bot));
276 #ifdef BOOTP_DEBUG
277 if (debug)
278 printf("bootpsend: calling sendudp\n");
279 #endif
281 return (sendudp(d, pkt, len));
284 static ssize_t
285 bootprecv(struct iodesc *d, void **pkt, void **payload, time_t tleft)
287 ssize_t n;
288 struct bootp *bp;
289 void *ptr;
291 #ifdef BOOTP_DEBUG
292 if (debug)
293 printf("bootp_recvoffer: called\n");
294 #endif
296 ptr = NULL;
297 n = readudp(d, &ptr, (void **)&bp, tleft);
298 if (n == -1 || n < sizeof(struct bootp) - BOOTP_VENDSIZE)
299 goto bad;
301 #ifdef BOOTP_DEBUG
302 if (debug)
303 printf("bootprecv: checked. bp = %p, n = %zd\n", bp, 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 int vsize = n - offsetof(struct bootp, bp_vend);
323 if(vend_rfc1048(bp->bp_vend, vsize) != 0)
324 goto bad;
326 /* Save copy of bootp reply or DHCP ACK message */
327 if (bp->bp_op == BOOTREPLY &&
328 ((dhcp_ok == 1 && expected_dhcpmsgtype == DHCPACK) ||
329 dhcp_ok == 0)) {
330 free(bootp_response);
331 bootp_response = malloc(n);
332 if (bootp_response != NULL) {
333 bootp_response_size = n;
334 bcopy(bp, bootp_response, bootp_response_size);
338 #ifdef BOOTP_VEND_CMU
339 else if (bcmp(vm_cmu, bp->bp_vend, sizeof(vm_cmu)) == 0)
340 vend_cmu(bp->bp_vend);
341 #endif
342 else
343 printf("bootprecv: unknown vendor 0x%lx\n", (long)bp->bp_vend);
345 *pkt = ptr;
346 *payload = bp;
347 return(n);
348 bad:
349 free(ptr);
350 errno = 0;
351 return (-1);
355 dhcp_try_rfc1048(uint8_t *cp, size_t len)
358 expected_dhcpmsgtype = DHCPACK;
359 if (bcmp(vm_rfc1048, cp, sizeof (vm_rfc1048)) == 0) {
360 return (vend_rfc1048(cp, len));
362 return (-1);
365 static int
366 vend_rfc1048(u_char *cp, u_int len)
368 u_char *ep;
369 int size;
370 u_char tag;
371 const char *val;
373 #ifdef BOOTP_DEBUG
374 if (debug)
375 printf("vend_rfc1048 bootp info. len=%d\n", len);
376 #endif
377 ep = cp + len;
379 /* Step over magic cookie */
380 cp += sizeof(int);
382 setenv_(cp, ep, NULL);
384 while (cp < ep) {
385 tag = *cp++;
386 size = *cp++;
387 if (tag == TAG_END)
388 break;
390 if (tag == TAG_SUBNET_MASK) {
391 bcopy(cp, &netmask, sizeof(netmask));
393 if (tag == TAG_GATEWAY) {
394 bcopy(cp, &gateip.s_addr, sizeof(gateip.s_addr));
396 if (tag == TAG_SWAPSERVER) {
397 /* let it override bp_siaddr */
398 bcopy(cp, &rootip.s_addr, sizeof(rootip.s_addr));
400 if (tag == TAG_ROOTPATH) {
401 if ((val = getenv("dhcp.root-path")) == NULL)
402 val = (const char *)cp;
403 strlcpy(rootpath, val, sizeof(rootpath));
405 if (tag == TAG_HOSTNAME) {
406 if ((val = getenv("dhcp.host-name")) == NULL)
407 val = (const char *)cp;
408 strlcpy(hostname, val, sizeof(hostname));
410 if (tag == TAG_INTF_MTU) {
411 intf_mtu = 0;
412 if ((val = getenv("dhcp.interface-mtu")) != NULL) {
413 unsigned long tmp;
414 char *end;
416 errno = 0;
418 * Do not allow MTU to exceed max IPv4 packet
419 * size, max value of 16-bit word.
421 tmp = strtoul(val, &end, 0);
422 if (errno != 0 ||
423 *val == '\0' || *end != '\0' ||
424 tmp > USHRT_MAX) {
425 printf("%s: bad value: \"%s\", "
426 "ignoring\n",
427 "dhcp.interface-mtu", val);
428 } else {
429 intf_mtu = (u_int)tmp;
432 if (intf_mtu == 0)
433 intf_mtu = be16dec(cp);
435 #ifdef SUPPORT_DHCP
436 if (tag == TAG_DHCP_MSGTYPE) {
437 if(*cp != expected_dhcpmsgtype)
438 return(-1);
439 dhcp_ok = 1;
441 if (tag == TAG_SERVERID) {
442 bcopy(cp, &dhcp_serverip.s_addr,
443 sizeof(dhcp_serverip.s_addr));
445 #endif
446 cp += size;
448 return(0);
451 #ifdef BOOTP_VEND_CMU
452 static void
453 vend_cmu(u_char *cp)
455 struct cmu_vend *vp;
457 #ifdef BOOTP_DEBUG
458 if (debug)
459 printf("vend_cmu bootp info.\n");
460 #endif
461 vp = (struct cmu_vend *)cp;
463 if (vp->v_smask.s_addr != 0) {
464 netmask = vp->v_smask.s_addr;
466 if (vp->v_dgate.s_addr != 0) {
467 gateip = vp->v_dgate;
470 #endif
472 #ifdef DHCP_ENV
474 * Parse DHCP options and store them into kenv variables.
475 * Original code from Danny Braniss, modifications by Luigi Rizzo.
477 * The parser is driven by tables which specify the type and name of
478 * each dhcp option and how it appears in kenv.
479 * The first entry in the list contains the prefix used to set the kenv
480 * name (including the . if needed), the last entry must have a 0 tag.
481 * Entries do not need to be sorted though it helps for readability.
483 * Certain vendor-specific tables can be enabled according to DHCP_ENV.
484 * Set it to 0 if you don't want any.
486 enum opt_fmt { __NONE = 0,
487 __8 = 1, __16 = 2, __32 = 4, /* Unsigned fields, value=size */
488 __IP, /* IPv4 address */
489 __TXT, /* C string */
490 __BYTES, /* byte sequence, printed %02x */
491 __INDIR, /* name=value */
492 __ILIST, /* name=value;name=value ... */
493 __VE, /* vendor specific, recurse */
496 struct dhcp_opt {
497 uint8_t tag;
498 uint8_t fmt;
499 const char *desc;
502 static struct dhcp_opt vndr_opt[] = { /* Vendor Specific Options */
503 #if DHCP_ENV == DHCP_ENV_FREEBSD /* FreeBSD table in the original code */
504 {0, 0, "FreeBSD"}, /* prefix */
505 {1, __TXT, "kernel"},
506 {2, __TXT, "kernelname"},
507 {3, __TXT, "kernel_options"},
508 {4, __IP, "usr-ip"},
509 {5, __TXT, "conf-path"},
510 {6, __TXT, "rc.conf0"},
511 {7, __TXT, "rc.conf1"},
512 {8, __TXT, "rc.conf2"},
513 {9, __TXT, "rc.conf3"},
514 {10, __TXT, "rc.conf4"},
515 {11, __TXT, "rc.conf5"},
516 {12, __TXT, "rc.conf6"},
517 {13, __TXT, "rc.conf7"},
518 {14, __TXT, "rc.conf8"},
519 {15, __TXT, "rc.conf9"},
521 {20, __TXT, "boot.nfsroot.options"},
523 {245, __INDIR, ""},
524 {246, __INDIR, ""},
525 {247, __INDIR, ""},
526 {248, __INDIR, ""},
527 {249, __INDIR, ""},
528 {250, __INDIR, ""},
529 {251, __INDIR, ""},
530 {252, __INDIR, ""},
531 {253, __INDIR, ""},
532 {254, __INDIR, ""},
534 #elif DHCP_ENV == DHCP_ENV_PXE /* some pxe options, RFC4578 */
535 {0, 0, "pxe"}, /* prefix */
536 {93, __16, "system-architecture"},
537 {94, __BYTES, "network-interface"},
538 {97, __BYTES, "machine-identifier"},
539 #else /* default (empty) table */
540 {0, 0, "dhcp.vendor."}, /* prefix */
541 #endif
542 {0, __TXT, "%soption-%d"}
545 static struct dhcp_opt dhcp_opt[] = {
546 /* DHCP Option names, formats and codes, from RFC2132. */
547 {0, 0, "dhcp."}, // prefix
548 {1, __IP, "subnet-mask"},
549 {2, __32, "time-offset"}, /* this is signed */
550 {3, __IP, "routers"},
551 {4, __IP, "time-servers"},
552 {5, __IP, "ien116-name-servers"},
553 {6, __IP, "domain-name-servers"},
554 {7, __IP, "log-servers"},
555 {8, __IP, "cookie-servers"},
556 {9, __IP, "lpr-servers"},
557 {10, __IP, "impress-servers"},
558 {11, __IP, "resource-location-servers"},
559 {12, __TXT, "host-name"},
560 {13, __16, "boot-size"},
561 {14, __TXT, "merit-dump"},
562 {15, __TXT, "domain-name"},
563 {16, __IP, "swap-server"},
564 {17, __TXT, "root-path"},
565 {18, __TXT, "extensions-path"},
566 {19, __8, "ip-forwarding"},
567 {20, __8, "non-local-source-routing"},
568 {21, __IP, "policy-filter"},
569 {22, __16, "max-dgram-reassembly"},
570 {23, __8, "default-ip-ttl"},
571 {24, __32, "path-mtu-aging-timeout"},
572 {25, __16, "path-mtu-plateau-table"},
573 {26, __16, "interface-mtu"},
574 {27, __8, "all-subnets-local"},
575 {28, __IP, "broadcast-address"},
576 {29, __8, "perform-mask-discovery"},
577 {30, __8, "mask-supplier"},
578 {31, __8, "perform-router-discovery"},
579 {32, __IP, "router-solicitation-address"},
580 {33, __IP, "static-routes"},
581 {34, __8, "trailer-encapsulation"},
582 {35, __32, "arp-cache-timeout"},
583 {36, __8, "ieee802-3-encapsulation"},
584 {37, __8, "default-tcp-ttl"},
585 {38, __32, "tcp-keepalive-interval"},
586 {39, __8, "tcp-keepalive-garbage"},
587 {40, __TXT, "nis-domain"},
588 {41, __IP, "nis-servers"},
589 {42, __IP, "ntp-servers"},
590 {43, __VE, "vendor-encapsulated-options"},
591 {44, __IP, "netbios-name-servers"},
592 {45, __IP, "netbios-dd-server"},
593 {46, __8, "netbios-node-type"},
594 {47, __TXT, "netbios-scope"},
595 {48, __IP, "x-font-servers"},
596 {49, __IP, "x-display-managers"},
597 {50, __IP, "dhcp-requested-address"},
598 {51, __32, "dhcp-lease-time"},
599 {52, __8, "dhcp-option-overload"},
600 {53, __8, "dhcp-message-type"},
601 {54, __IP, "dhcp-server-identifier"},
602 {55, __8, "dhcp-parameter-request-list"},
603 {56, __TXT, "dhcp-message"},
604 {57, __16, "dhcp-max-message-size"},
605 {58, __32, "dhcp-renewal-time"},
606 {59, __32, "dhcp-rebinding-time"},
607 {60, __TXT, "vendor-class-identifier"},
608 {61, __TXT, "dhcp-client-identifier"},
609 {64, __TXT, "nisplus-domain"},
610 {65, __IP, "nisplus-servers"},
611 {66, __TXT, "tftp-server-name"},
612 {67, __TXT, "bootfile-name"},
613 {68, __IP, "mobile-ip-home-agent"},
614 {69, __IP, "smtp-server"},
615 {70, __IP, "pop-server"},
616 {71, __IP, "nntp-server"},
617 {72, __IP, "www-server"},
618 {73, __IP, "finger-server"},
619 {74, __IP, "irc-server"},
620 {75, __IP, "streettalk-server"},
621 {76, __IP, "streettalk-directory-assistance-server"},
622 {77, __TXT, "user-class"},
623 {85, __IP, "nds-servers"},
624 {86, __TXT, "nds-tree-name"},
625 {87, __TXT, "nds-context"},
626 {210, __TXT, "authenticate"},
628 /* use the following entries for arbitrary variables */
629 {246, __ILIST, ""},
630 {247, __ILIST, ""},
631 {248, __ILIST, ""},
632 {249, __ILIST, ""},
633 {250, __INDIR, ""},
634 {251, __INDIR, ""},
635 {252, __INDIR, ""},
636 {253, __INDIR, ""},
637 {254, __INDIR, ""},
638 {0, __TXT, "%soption-%d"}
642 * parse a dhcp response, set environment variables translating options
643 * names and values according to the tables above. Also set dhcp.tags
644 * to the list of selected tags.
646 static void
647 setenv_(u_char *cp, u_char *ep, struct dhcp_opt *opts)
649 u_char *ncp;
650 u_char tag;
651 char tags[512], *tp; /* the list of tags */
653 #define FLD_SEP ',' /* separator in list of elements */
654 ncp = cp;
655 tp = tags;
656 if (opts == NULL)
657 opts = dhcp_opt;
659 while (ncp < ep) {
660 unsigned int size; /* option size */
661 char *vp, *endv, buf[256]; /* the value buffer */
662 struct dhcp_opt *op;
664 tag = *ncp++; /* extract tag and size */
665 size = *ncp++;
666 cp = ncp; /* current payload */
667 ncp += size; /* point to the next option */
669 if (tag == TAG_END)
670 break;
671 if (tag == 0)
672 continue;
674 for (op = opts+1; op->tag && op->tag != tag; op++)
676 /* if not found we end up on the default entry */
679 * Copy data into the buffer. libstand does not have snprintf so we
680 * need to be careful with sprintf(). With strings, the source is
681 * always <256 char so shorter than the buffer so we are safe; with
682 * other arguments, the longest string is inet_ntoa which is 16 bytes
683 * so we make sure to have always enough room in the string before
684 * trying an sprint.
686 vp = buf;
687 *vp = '\0';
688 endv = buf + sizeof(buf) - 1 - 16; /* last valid write position */
690 switch(op->fmt) {
691 case __NONE:
692 break; /* should not happen */
694 case __VE: /* recurse, vendor specific */
695 setenv_(cp, cp+size, vndr_opt);
696 break;
698 case __IP: /* ip address */
699 for (; size > 0 && vp < endv; size -= 4, cp += 4) {
700 struct in_addr in_ip; /* ip addresses */
701 if (vp != buf)
702 *vp++ = FLD_SEP;
703 bcopy(cp, &in_ip.s_addr, sizeof(in_ip.s_addr));
704 sprintf(vp, "%s", inet_ntoa(in_ip));
705 vp += strlen(vp);
707 break;
709 case __BYTES: /* opaque byte string */
710 for (; size > 0 && vp < endv; size -= 1, cp += 1) {
711 sprintf(vp, "%02x", *cp);
712 vp += strlen(vp);
714 break;
716 case __TXT:
717 bcopy(cp, buf, size); /* cannot overflow */
718 buf[size] = 0;
719 break;
721 case __32:
722 case __16:
723 case __8: /* op->fmt is also the length of each field */
724 for (; size > 0 && vp < endv; size -= op->fmt, cp += op->fmt) {
725 uint32_t v;
726 if (op->fmt == __32)
727 v = (cp[0]<<24) + (cp[1]<<16) + (cp[2]<<8) + cp[3];
728 else if (op->fmt == __16)
729 v = (cp[0]<<8) + cp[1];
730 else
731 v = cp[0];
732 if (vp != buf)
733 *vp++ = FLD_SEP;
734 sprintf(vp, "%u", v);
735 vp += strlen(vp);
737 break;
739 case __INDIR: /* name=value */
740 case __ILIST: /* name=value;name=value... */
741 bcopy(cp, buf, size); /* cannot overflow */
742 buf[size] = '\0';
743 for (endv = buf; endv; endv = vp) {
744 u_char *s = NULL; /* semicolon ? */
746 /* skip leading whitespace */
747 while (*endv && strchr(" \t\n\r", *endv))
748 endv++;
749 vp = strchr(endv, '='); /* find name=value separator */
750 if (!vp)
751 break;
752 *vp++ = 0;
753 if (op->fmt == __ILIST && (s = strchr(vp, ';')))
754 *s++ = '\0';
755 setenv(endv, vp, 1);
756 vp = s; /* prepare for next round */
758 buf[0] = '\0'; /* option already done */
761 if (tp - tags < sizeof(tags) - 5) { /* add tag to the list */
762 if (tp != tags)
763 *tp++ = FLD_SEP;
764 sprintf(tp, "%d", tag);
765 tp += strlen(tp);
767 if (buf[0]) {
768 char env[128]; /* the string name */
770 if (op->tag == 0)
771 sprintf(env, op->desc, opts[0].desc, tag);
772 else
773 sprintf(env, "%s%s", opts[0].desc, op->desc);
775 * Do not replace existing values in the environment, so that
776 * locally-obtained values can override server-provided values.
778 setenv(env, buf, 0);
781 if (tp != tags) {
782 char env[128]; /* the string name */
783 sprintf(env, "%stags", opts[0].desc);
784 setenv(env, tags, 1);
787 #endif /* additional dhcp */