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); 72 static int vend_rfc1048(u_char *, u_int); 73 #ifdef BOOTP_VEND_CMU 74 static void vend_cmu(u_char *); 75 #endif 76 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 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 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 122 bootp(int sock) 123 { 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; 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, 179 bootpsend, bp, sizeof(*bp), 180 bootprecv, &pkt, (void **)&rbootp) == -1) { 181 printf("bootp: no reply\n"); 182 return; 183 } 184 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]); 200 201 expected_dhcpmsgtype = DHCPACK; 202 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; 209 } 210 } 211 #endif 212 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'; 219 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 231 } 232 233 #ifdef BOOTP_DEBUG 234 if (debug) 235 printf("mask: %s\n", intoa(netmask)); 236 #endif 237 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 244 } 245 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; 253 } 254 255 /* Bump xid so next request will be unique. */ 256 ++d->xid; 257 free(pkt); 258 } 259 260 /* Transmit a bootp request */ 261 static ssize_t 262 bootpsend(struct iodesc *d, void *pkt, size_t len) 263 { 264 struct bootp *bp; 265 266 #ifdef BOOTP_DEBUG 267 if (debug) 268 printf("bootpsend: d=%lx called.\n", (long)d); 269 #endif 270 271 bp = pkt; 272 bp->bp_secs = htons((u_short)(getsecs() - bot)); 273 274 #ifdef BOOTP_DEBUG 275 if (debug) 276 printf("bootpsend: calling sendudp\n"); 277 #endif 278 279 return (sendudp(d, pkt, len)); 280 } 281 282 static ssize_t 283 bootprecv(struct iodesc *d, void **pkt, void **payload, time_t tleft) 284 { 285 ssize_t n; 286 struct bootp *bp; 287 void *ptr; 288 289 #ifdef BOOTP_DEBUG 290 if (debug) 291 printf("bootp_recvoffer: called\n"); 292 #endif 293 294 ptr = NULL; 295 n = readudp(d, &ptr, (void **)&bp, tleft); 296 if (n == -1 || n < sizeof(struct bootp) - BOOTP_VENDSIZE) 297 goto bad; 298 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)); 308 } 309 #endif 310 goto bad; 311 } 312 313 #ifdef BOOTP_DEBUG 314 if (debug) 315 printf("bootprecv: got one!\n"); 316 #endif 317 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; 323 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); 333 } 334 } 335 } 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); 342 343 *pkt = ptr; 344 *payload = bp; 345 return(n); 346 bad: 347 free(ptr); 348 errno = 0; 349 return (-1); 350 } 351 352 static int 353 vend_rfc1048(u_char *cp, u_int len) 354 { 355 u_char *ep; 356 int size; 357 u_char tag; 358 const char *val; 359 360 #ifdef BOOTP_DEBUG 361 if (debug) 362 printf("vend_rfc1048 bootp info. len=%d\n", len); 363 #endif 364 ep = cp + len; 365 366 /* Step over magic cookie */ 367 cp += sizeof(int); 368 369 setenv_(cp, ep, NULL); 370 371 while (cp < ep) { 372 tag = *cp++; 373 size = *cp++; 374 if (tag == TAG_END) 375 break; 376 377 if (tag == TAG_SUBNET_MASK) { 378 bcopy(cp, &netmask, sizeof(netmask)); 379 } 380 if (tag == TAG_GATEWAY) { 381 bcopy(cp, &gateip.s_addr, sizeof(gateip.s_addr)); 382 } 383 if (tag == TAG_SWAPSERVER) { 384 /* let it override bp_siaddr */ 385 bcopy(cp, &rootip.s_addr, sizeof(rootip.s_addr)); 386 } 387 if (tag == TAG_ROOTPATH) { 388 if ((val = getenv("dhcp.root-path")) == NULL) 389 val = (const char *)cp; 390 strlcpy(rootpath, val, sizeof(rootpath)); 391 } 392 if (tag == TAG_HOSTNAME) { 393 if ((val = getenv("dhcp.host-name")) == NULL) 394 val = (const char *)cp; 395 strlcpy(hostname, val, sizeof(hostname)); 396 } 397 if (tag == TAG_INTF_MTU) { 398 intf_mtu = 0; 399 if ((val = getenv("dhcp.interface-mtu")) != NULL) { 400 unsigned long tmp; 401 char *end; 402 403 errno = 0; 404 /* 405 * Do not allow MTU to exceed max IPv4 packet 406 * size, max value of 16-bit word. 407 */ 408 tmp = strtoul(val, &end, 0); 409 if (errno != 0 || 410 *val == '\0' || *end != '\0' || 411 tmp > USHRT_MAX) { 412 printf("%s: bad value: \"%s\", " 413 "ignoring\n", 414 "dhcp.interface-mtu", val); 415 } else { 416 intf_mtu = (u_int)tmp; 417 } 418 } 419 if (intf_mtu == 0) 420 intf_mtu = be16dec(cp); 421 } 422 #ifdef SUPPORT_DHCP 423 if (tag == TAG_DHCP_MSGTYPE) { 424 if(*cp != expected_dhcpmsgtype) 425 return(-1); 426 dhcp_ok = 1; 427 } 428 if (tag == TAG_SERVERID) { 429 bcopy(cp, &dhcp_serverip.s_addr, 430 sizeof(dhcp_serverip.s_addr)); 431 } 432 #endif 433 cp += size; 434 } 435 return(0); 436 } 437 438 #ifdef BOOTP_VEND_CMU 439 static void 440 vend_cmu(u_char *cp) 441 { 442 struct cmu_vend *vp; 443 444 #ifdef BOOTP_DEBUG 445 if (debug) 446 printf("vend_cmu bootp info.\n"); 447 #endif 448 vp = (struct cmu_vend *)cp; 449 450 if (vp->v_smask.s_addr != 0) { 451 netmask = vp->v_smask.s_addr; 452 } 453 if (vp->v_dgate.s_addr != 0) { 454 gateip = vp->v_dgate; 455 } 456 } 457 #endif 458 459 #ifdef DHCP_ENV 460 /* 461 * Parse DHCP options and store them into kenv variables. 462 * Original code from Danny Braniss, modifications by Luigi Rizzo. 463 * 464 * The parser is driven by tables which specify the type and name of 465 * each dhcp option and how it appears in kenv. 466 * The first entry in the list contains the prefix used to set the kenv 467 * name (including the . if needed), the last entry must have a 0 tag. 468 * Entries do not need to be sorted though it helps for readability. 469 * 470 * Certain vendor-specific tables can be enabled according to DHCP_ENV. 471 * Set it to 0 if you don't want any. 472 */ 473 enum opt_fmt { __NONE = 0, 474 __8 = 1, __16 = 2, __32 = 4, /* Unsigned fields, value=size */ 475 __IP, /* IPv4 address */ 476 __TXT, /* C string */ 477 __BYTES, /* byte sequence, printed %02x */ 478 __INDIR, /* name=value */ 479 __ILIST, /* name=value;name=value ... */ 480 __VE, /* vendor specific, recurse */ 481 }; 482 483 struct dhcp_opt { 484 uint8_t tag; 485 uint8_t fmt; 486 const char *desc; 487 }; 488 489 static struct dhcp_opt vndr_opt[] = { /* Vendor Specific Options */ 490 #if DHCP_ENV == DHCP_ENV_FREEBSD /* FreeBSD table in the original code */ 491 {0, 0, "FreeBSD"}, /* prefix */ 492 {1, __TXT, "kernel"}, 493 {2, __TXT, "kernelname"}, 494 {3, __TXT, "kernel_options"}, 495 {4, __IP, "usr-ip"}, 496 {5, __TXT, "conf-path"}, 497 {6, __TXT, "rc.conf0"}, 498 {7, __TXT, "rc.conf1"}, 499 {8, __TXT, "rc.conf2"}, 500 {9, __TXT, "rc.conf3"}, 501 {10, __TXT, "rc.conf4"}, 502 {11, __TXT, "rc.conf5"}, 503 {12, __TXT, "rc.conf6"}, 504 {13, __TXT, "rc.conf7"}, 505 {14, __TXT, "rc.conf8"}, 506 {15, __TXT, "rc.conf9"}, 507 508 {20, __TXT, "boot.nfsroot.options"}, 509 510 {245, __INDIR, ""}, 511 {246, __INDIR, ""}, 512 {247, __INDIR, ""}, 513 {248, __INDIR, ""}, 514 {249, __INDIR, ""}, 515 {250, __INDIR, ""}, 516 {251, __INDIR, ""}, 517 {252, __INDIR, ""}, 518 {253, __INDIR, ""}, 519 {254, __INDIR, ""}, 520 521 #elif DHCP_ENV == DHCP_ENV_PXE /* some pxe options, RFC4578 */ 522 {0, 0, "pxe"}, /* prefix */ 523 {93, __16, "system-architecture"}, 524 {94, __BYTES, "network-interface"}, 525 {97, __BYTES, "machine-identifier"}, 526 #else /* default (empty) table */ 527 {0, 0, "dhcp.vendor."}, /* prefix */ 528 #endif 529 {0, __TXT, "%soption-%d"} 530 }; 531 532 static struct dhcp_opt dhcp_opt[] = { 533 /* DHCP Option names, formats and codes, from RFC2132. */ 534 {0, 0, "dhcp."}, // prefix 535 {1, __IP, "subnet-mask"}, 536 {2, __32, "time-offset"}, /* this is signed */ 537 {3, __IP, "routers"}, 538 {4, __IP, "time-servers"}, 539 {5, __IP, "ien116-name-servers"}, 540 {6, __IP, "domain-name-servers"}, 541 {7, __IP, "log-servers"}, 542 {8, __IP, "cookie-servers"}, 543 {9, __IP, "lpr-servers"}, 544 {10, __IP, "impress-servers"}, 545 {11, __IP, "resource-location-servers"}, 546 {12, __TXT, "host-name"}, 547 {13, __16, "boot-size"}, 548 {14, __TXT, "merit-dump"}, 549 {15, __TXT, "domain-name"}, 550 {16, __IP, "swap-server"}, 551 {17, __TXT, "root-path"}, 552 {18, __TXT, "extensions-path"}, 553 {19, __8, "ip-forwarding"}, 554 {20, __8, "non-local-source-routing"}, 555 {21, __IP, "policy-filter"}, 556 {22, __16, "max-dgram-reassembly"}, 557 {23, __8, "default-ip-ttl"}, 558 {24, __32, "path-mtu-aging-timeout"}, 559 {25, __16, "path-mtu-plateau-table"}, 560 {26, __16, "interface-mtu"}, 561 {27, __8, "all-subnets-local"}, 562 {28, __IP, "broadcast-address"}, 563 {29, __8, "perform-mask-discovery"}, 564 {30, __8, "mask-supplier"}, 565 {31, __8, "perform-router-discovery"}, 566 {32, __IP, "router-solicitation-address"}, 567 {33, __IP, "static-routes"}, 568 {34, __8, "trailer-encapsulation"}, 569 {35, __32, "arp-cache-timeout"}, 570 {36, __8, "ieee802-3-encapsulation"}, 571 {37, __8, "default-tcp-ttl"}, 572 {38, __32, "tcp-keepalive-interval"}, 573 {39, __8, "tcp-keepalive-garbage"}, 574 {40, __TXT, "nis-domain"}, 575 {41, __IP, "nis-servers"}, 576 {42, __IP, "ntp-servers"}, 577 {43, __VE, "vendor-encapsulated-options"}, 578 {44, __IP, "netbios-name-servers"}, 579 {45, __IP, "netbios-dd-server"}, 580 {46, __8, "netbios-node-type"}, 581 {47, __TXT, "netbios-scope"}, 582 {48, __IP, "x-font-servers"}, 583 {49, __IP, "x-display-managers"}, 584 {50, __IP, "dhcp-requested-address"}, 585 {51, __32, "dhcp-lease-time"}, 586 {52, __8, "dhcp-option-overload"}, 587 {53, __8, "dhcp-message-type"}, 588 {54, __IP, "dhcp-server-identifier"}, 589 {55, __8, "dhcp-parameter-request-list"}, 590 {56, __TXT, "dhcp-message"}, 591 {57, __16, "dhcp-max-message-size"}, 592 {58, __32, "dhcp-renewal-time"}, 593 {59, __32, "dhcp-rebinding-time"}, 594 {60, __TXT, "vendor-class-identifier"}, 595 {61, __TXT, "dhcp-client-identifier"}, 596 {64, __TXT, "nisplus-domain"}, 597 {65, __IP, "nisplus-servers"}, 598 {66, __TXT, "tftp-server-name"}, 599 {67, __TXT, "bootfile-name"}, 600 {68, __IP, "mobile-ip-home-agent"}, 601 {69, __IP, "smtp-server"}, 602 {70, __IP, "pop-server"}, 603 {71, __IP, "nntp-server"}, 604 {72, __IP, "www-server"}, 605 {73, __IP, "finger-server"}, 606 {74, __IP, "irc-server"}, 607 {75, __IP, "streettalk-server"}, 608 {76, __IP, "streettalk-directory-assistance-server"}, 609 {77, __TXT, "user-class"}, 610 {85, __IP, "nds-servers"}, 611 {86, __TXT, "nds-tree-name"}, 612 {87, __TXT, "nds-context"}, 613 {210, __TXT, "authenticate"}, 614 615 /* use the following entries for arbitrary variables */ 616 {246, __ILIST, ""}, 617 {247, __ILIST, ""}, 618 {248, __ILIST, ""}, 619 {249, __ILIST, ""}, 620 {250, __INDIR, ""}, 621 {251, __INDIR, ""}, 622 {252, __INDIR, ""}, 623 {253, __INDIR, ""}, 624 {254, __INDIR, ""}, 625 {0, __TXT, "%soption-%d"} 626 }; 627 628 /* 629 * parse a dhcp response, set environment variables translating options 630 * names and values according to the tables above. Also set dhcp.tags 631 * to the list of selected tags. 632 */ 633 static void 634 setenv_(u_char *cp, u_char *ep, struct dhcp_opt *opts) 635 { 636 u_char *ncp; 637 u_char tag; 638 char tags[512], *tp; /* the list of tags */ 639 640 #define FLD_SEP ',' /* separator in list of elements */ 641 ncp = cp; 642 tp = tags; 643 if (opts == NULL) 644 opts = dhcp_opt; 645 646 while (ncp < ep) { 647 unsigned int size; /* option size */ 648 char *vp, *endv, buf[256]; /* the value buffer */ 649 struct dhcp_opt *op; 650 651 tag = *ncp++; /* extract tag and size */ 652 size = *ncp++; 653 cp = ncp; /* current payload */ 654 ncp += size; /* point to the next option */ 655 656 if (tag == TAG_END) 657 break; 658 if (tag == 0) 659 continue; 660 661 for (op = opts+1; op->tag && op->tag != tag; op++) 662 ; 663 /* if not found we end up on the default entry */ 664 665 /* 666 * Copy data into the buffer. libstand does not have snprintf so we 667 * need to be careful with sprintf(). With strings, the source is 668 * always <256 char so shorter than the buffer so we are safe; with 669 * other arguments, the longest string is inet_ntoa which is 16 bytes 670 * so we make sure to have always enough room in the string before 671 * trying an sprint. 672 */ 673 vp = buf; 674 *vp = '\0'; 675 endv = buf + sizeof(buf) - 1 - 16; /* last valid write position */ 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; size -= op->fmt, cp += op->fmt) { 712 uint32_t v; 713 if (op->fmt == __32) 714 v = (cp[0]<<24) + (cp[1]<<16) + (cp[2]<<8) + cp[3]; 715 else if (op->fmt == __16) 716 v = (cp[0]<<8) + cp[1]; 717 else 718 v = cp[0]; 719 if (vp != buf) 720 *vp++ = FLD_SEP; 721 sprintf(vp, "%u", v); 722 vp += strlen(vp); 723 } 724 break; 725 726 case __INDIR: /* name=value */ 727 case __ILIST: /* name=value;name=value... */ 728 bcopy(cp, buf, size); /* cannot overflow */ 729 buf[size] = '\0'; 730 for (endv = buf; endv; endv = vp) { 731 u_char *s = NULL; /* semicolon ? */ 732 733 /* skip leading whitespace */ 734 while (*endv && strchr(" \t\n\r", *endv)) 735 endv++; 736 vp = strchr(endv, '='); /* find name=value separator */ 737 if (!vp) 738 break; 739 *vp++ = 0; 740 if (op->fmt == __ILIST && (s = strchr(vp, ';'))) 741 *s++ = '\0'; 742 setenv(endv, vp, 1); 743 vp = s; /* prepare for next round */ 744 } 745 buf[0] = '\0'; /* option already done */ 746 } 747 748 if (tp - tags < sizeof(tags) - 5) { /* add tag to the list */ 749 if (tp != tags) 750 *tp++ = FLD_SEP; 751 sprintf(tp, "%d", tag); 752 tp += strlen(tp); 753 } 754 if (buf[0]) { 755 char env[128]; /* the string name */ 756 757 if (op->tag == 0) 758 sprintf(env, op->desc, opts[0].desc, tag); 759 else 760 sprintf(env, "%s%s", opts[0].desc, op->desc); 761 /* 762 * Do not replace existing values in the environment, so that 763 * locally-obtained values can override server-provided values. 764 */ 765 setenv(env, buf, 0); 766 } 767 } 768 if (tp != tags) { 769 char env[128]; /* the string name */ 770 sprintf(env, "%stags", opts[0].desc); 771 setenv(env, tags, 1); 772 } 773 } 774 #endif /* additional dhcp */ 775