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