bootp.c (165906) | bootp.c (185643) |
---|---|
1/* $NetBSD: bootp.c,v 1.14 1998/02/16 11:10:54 drochner Exp $ */ 2 3/* 4 * Copyright (c) 1992 Regents of the University of California. 5 * All rights reserved. 6 * 7 * This software was developed by the Computer Systems Engineering group 8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and --- 22 unchanged lines hidden (view full) --- 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. 34 * 35 * @(#) Header: bootp.c,v 1.4 93/09/11 03:13:51 leres Exp (LBL) 36 */ 37 38#include <sys/cdefs.h> | 1/* $NetBSD: bootp.c,v 1.14 1998/02/16 11:10:54 drochner Exp $ */ 2 3/* 4 * Copyright (c) 1992 Regents of the University of California. 5 * All rights reserved. 6 * 7 * This software was developed by the Computer Systems Engineering group 8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and --- 22 unchanged lines hidden (view full) --- 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. 34 * 35 * @(#) Header: bootp.c,v 1.4 93/09/11 03:13:51 leres Exp (LBL) 36 */ 37 38#include <sys/cdefs.h> |
39__FBSDID("$FreeBSD: head/lib/libstand/bootp.c 165906 2007-01-09 01:02:06Z imp $"); | 39__FBSDID("$FreeBSD: head/lib/libstand/bootp.c 185643 2008-12-05 17:13:40Z luigi $"); |
40 41#include <sys/types.h> 42#include <netinet/in.h> 43#include <netinet/in_systm.h> 44 45#include <string.h> 46 47#define BOOTP_DEBUGxx 48#define SUPPORT_DHCP 49 | 40 41#include <sys/types.h> 42#include <netinet/in.h> 43#include <netinet/in_systm.h> 44 45#include <string.h> 46 47#define BOOTP_DEBUGxx 48#define SUPPORT_DHCP 49 |
50#define DHCP_ENV_NOVENDOR 1 /* do not parse vendor options */ 51#define DHCP_ENV_PXE 10 /* assume pxe vendor options */ 52#define DHCP_ENV_FREEBSD 11 /* assume freebsd vendor options 53/* set DHCP_ENV to one of the values above to export dhcp options to kenv */ 54#define DHCP_ENV DHCP_ENV_NO_VENDOR 55 |
|
50#include "stand.h" 51#include "net.h" 52#include "netif.h" 53#include "bootp.h" 54 55 56struct in_addr servip; 57 --- 9 unchanged lines hidden (view full) --- 67/* Local forwards */ 68static ssize_t bootpsend(struct iodesc *, void *, size_t); 69static ssize_t bootprecv(struct iodesc *, void *, size_t, time_t); 70static int vend_rfc1048(u_char *, u_int); 71#ifdef BOOTP_VEND_CMU 72static void vend_cmu(u_char *); 73#endif 74 | 56#include "stand.h" 57#include "net.h" 58#include "netif.h" 59#include "bootp.h" 60 61 62struct in_addr servip; 63 --- 9 unchanged lines hidden (view full) --- 73/* Local forwards */ 74static ssize_t bootpsend(struct iodesc *, void *, size_t); 75static ssize_t bootprecv(struct iodesc *, void *, size_t, time_t); 76static int vend_rfc1048(u_char *, u_int); 77#ifdef BOOTP_VEND_CMU 78static void vend_cmu(u_char *); 79#endif 80 |
81#ifdef DHCP_ENV /* export the dhcp response to kenv */ 82struct dhcp_opt; 83static void setenv_(u_char *cp, u_char *ep, struct dhcp_opt *opts); 84#else 85#define setenv_(a, b, c) 86#endif 87 |
|
75#ifdef SUPPORT_DHCP 76static char expected_dhcpmsgtype = -1, dhcp_ok; 77struct in_addr dhcp_serverip; 78#endif 79 80/* Fetch required bootp infomation */ 81void 82bootp(sock, flag) --- 263 unchanged lines hidden (view full) --- 346 if (debug) 347 printf("vend_rfc1048 bootp info. len=%d\n", len); 348#endif 349 ep = cp + len; 350 351 /* Step over magic cookie */ 352 cp += sizeof(int); 353 | 88#ifdef SUPPORT_DHCP 89static char expected_dhcpmsgtype = -1, dhcp_ok; 90struct in_addr dhcp_serverip; 91#endif 92 93/* Fetch required bootp infomation */ 94void 95bootp(sock, flag) --- 263 unchanged lines hidden (view full) --- 359 if (debug) 360 printf("vend_rfc1048 bootp info. len=%d\n", len); 361#endif 362 ep = cp + len; 363 364 /* Step over magic cookie */ 365 cp += sizeof(int); 366 |
367 setenv_(cp, ep, NULL); 368 |
|
354 while (cp < ep) { 355 tag = *cp++; 356 size = *cp++; 357 if (tag == TAG_END) 358 break; 359 360 if (tag == TAG_SUBNET_MASK) { 361 bcopy(cp, &smask, sizeof(smask)); --- 45 unchanged lines hidden (view full) --- 407 if (vp->v_smask.s_addr != 0) { 408 smask = vp->v_smask.s_addr; 409 } 410 if (vp->v_dgate.s_addr != 0) { 411 gateip = vp->v_dgate; 412 } 413} 414#endif | 369 while (cp < ep) { 370 tag = *cp++; 371 size = *cp++; 372 if (tag == TAG_END) 373 break; 374 375 if (tag == TAG_SUBNET_MASK) { 376 bcopy(cp, &smask, sizeof(smask)); --- 45 unchanged lines hidden (view full) --- 422 if (vp->v_smask.s_addr != 0) { 423 smask = vp->v_smask.s_addr; 424 } 425 if (vp->v_dgate.s_addr != 0) { 426 gateip = vp->v_dgate; 427 } 428} 429#endif |
430 431#ifdef DHCP_ENV 432/* 433 * Parse DHCP options and store them into kenv variables. 434 * Original code from Danny Braniss, modifications by Luigi Rizzo. 435 * 436 * The parser is driven by tables which specify the type and name of 437 * each dhcp option and how it appears in kenv. 438 * The first entry in the list contains the prefix used to set the kenv 439 * name (including the . if needed), the last entry must have a 0 tag. 440 * Entries do not need to be sorted though it helps for readability. 441 * 442 * Certain vendor-specific tables can be enabled according to DHCP_ENV. 443 * Set it to 0 if you don't want any. 444 */ 445enum opt_fmt { __NONE = 0, 446 __8 = 1, __16 = 2, __32 = 4, /* Unsigned fields, value=size */ 447 __IP, /* IPv4 address */ 448 __TXT, /* C string */ 449 __BYTES, /* byte sequence, printed %02x */ 450 __INDIR, /* name=value */ 451 __ILIST, /* name=value;name=value ... */ 452 __VE, /* vendor specific, recurse */ 453}; 454 455struct dhcp_opt { 456 uint8_t tag; 457 uint8_t fmt; 458 const char *desc; 459}; 460 461static struct dhcp_opt vndr_opt[] = { /* Vendor Specific Options */ 462#if DHCP_ENV == DHCP_ENV_FREEBSD /* FreeBSD table in the original code */ 463 {0, 0, "FreeBSD"}, /* prefix */ 464 {1, __TXT, "kernel"}, 465 {2, __TXT, "kernelname"}, 466 {3, __TXT, "kernel_options"}, 467 {4, __IP, "usr-ip"}, 468 {5, __TXT, "conf-path"}, 469 {6, __TXT, "rc.conf0"}, 470 {7, __TXT, "rc.conf1"}, 471 {8, __TXT, "rc.conf2"}, 472 {9, __TXT, "rc.conf3"}, 473 {10, __TXT, "rc.conf4"}, 474 {11, __TXT, "rc.conf5"}, 475 {12, __TXT, "rc.conf6"}, 476 {13, __TXT, "rc.conf7"}, 477 {14, __TXT, "rc.conf8"}, 478 {15, __TXT, "rc.conf9"}, 479 480 {20, __TXT, "boot.nfsroot.options"}, 481 482 {245, __INDIR, ""}, 483 {246, __INDIR, ""}, 484 {247, __INDIR, ""}, 485 {248, __INDIR, ""}, 486 {249, __INDIR, ""}, 487 {250, __INDIR, ""}, 488 {251, __INDIR, ""}, 489 {252, __INDIR, ""}, 490 {253, __INDIR, ""}, 491 {254, __INDIR, ""}, 492 493#elif DHCP_ENV == DHCP_ENV_PXE /* some pxe options, RFC4578 */ 494 {0, 0, "pxe"}, /* prefix */ 495 {93, __16, "system-architecture"}, 496 {94, __BYTES, "network-interface"}, 497 {97, __BYTES, "machine-identifier"}, 498#else /* default (empty) table */ 499 {0, 0, ""}, /* prefix */ 500#endif 501 {0, __TXT, "%soption-%d"} 502}; 503 504static struct dhcp_opt dhcp_opt[] = { 505 /* DHCP Option names, formats and codes, from RFC2132. */ 506 {0, 0, "dhcp."}, // prefix 507 {1, __IP, "subnet-mask"}, 508 {2, __32, "time-offset"}, /* this is signed */ 509 {3, __IP, "routers"}, 510 {4, __IP, "time-servers"}, 511 {5, __IP, "ien116-name-servers"}, 512 {6, __IP, "domain-name-servers"}, 513 {7, __IP, "log-servers"}, 514 {8, __IP, "cookie-servers"}, 515 {9, __IP, "lpr-servers"}, 516 {10, __IP, "impress-servers"}, 517 {11, __IP, "resource-location-servers"}, 518 {12, __TXT, "host-name"}, 519 {13, __16, "boot-size"}, 520 {14, __TXT, "merit-dump"}, 521 {15, __TXT, "domain-name"}, 522 {16, __IP, "swap-server"}, 523 {17, __TXT, "root-path"}, 524 {18, __TXT, "extensions-path"}, 525 {19, __8, "ip-forwarding"}, 526 {20, __8, "non-local-source-routing"}, 527 {21, __IP, "policy-filter"}, 528 {22, __16, "max-dgram-reassembly"}, 529 {23, __8, "default-ip-ttl"}, 530 {24, __32, "path-mtu-aging-timeout"}, 531 {25, __16, "path-mtu-plateau-table"}, 532 {26, __16, "interface-mtu"}, 533 {27, __8, "all-subnets-local"}, 534 {28, __IP, "broadcast-address"}, 535 {29, __8, "perform-mask-discovery"}, 536 {30, __8, "mask-supplier"}, 537 {31, __8, "perform-router-discovery"}, 538 {32, __IP, "router-solicitation-address"}, 539 {33, __IP, "static-routes"}, 540 {34, __8, "trailer-encapsulation"}, 541 {35, __32, "arp-cache-timeout"}, 542 {36, __8, "ieee802-3-encapsulation"}, 543 {37, __8, "default-tcp-ttl"}, 544 {38, __32, "tcp-keepalive-interval"}, 545 {39, __8, "tcp-keepalive-garbage"}, 546 {40, __TXT, "nis-domain"}, 547 {41, __IP, "nis-servers"}, 548 {42, __IP, "ntp-servers"}, 549 {43, __VE, "vendor-encapsulated-options"}, 550 {44, __IP, "netbios-name-servers"}, 551 {45, __IP, "netbios-dd-server"}, 552 {46, __8, "netbios-node-type"}, 553 {47, __TXT, "netbios-scope"}, 554 {48, __IP, "x-font-servers"}, 555 {49, __IP, "x-display-managers"}, 556 {50, __IP, "dhcp-requested-address"}, 557 {51, __32, "dhcp-lease-time"}, 558 {52, __8, "dhcp-option-overload"}, 559 {53, __8, "dhcp-message-type"}, 560 {54, __IP, "dhcp-server-identifier"}, 561 {55, __8, "dhcp-parameter-request-list"}, 562 {56, __TXT, "dhcp-message"}, 563 {57, __16, "dhcp-max-message-size"}, 564 {58, __32, "dhcp-renewal-time"}, 565 {59, __32, "dhcp-rebinding-time"}, 566 {60, __TXT, "vendor-class-identifier"}, 567 {61, __TXT, "dhcp-client-identifier"}, 568 {64, __TXT, "nisplus-domain"}, 569 {65, __IP, "nisplus-servers"}, 570 {66, __TXT, "tftp-server-name"}, 571 {67, __TXT, "bootfile-name"}, 572 {68, __IP, "mobile-ip-home-agent"}, 573 {69, __IP, "smtp-server"}, 574 {70, __IP, "pop-server"}, 575 {71, __IP, "nntp-server"}, 576 {72, __IP, "www-server"}, 577 {73, __IP, "finger-server"}, 578 {74, __IP, "irc-server"}, 579 {75, __IP, "streettalk-server"}, 580 {76, __IP, "streettalk-directory-assistance-server"}, 581 {77, __TXT, "user-class"}, 582 {85, __IP, "nds-servers"}, 583 {86, __TXT, "nds-tree-name"}, 584 {87, __TXT, "nds-context"}, 585 {210, __TXT, "authenticate"}, 586 587 /* use the following entries for arbitrary variables */ 588 {246, __ILIST, ""}, 589 {247, __ILIST, ""}, 590 {248, __ILIST, ""}, 591 {249, __ILIST, ""}, 592 {250, __INDIR, ""}, 593 {251, __INDIR, ""}, 594 {252, __INDIR, ""}, 595 {253, __INDIR, ""}, 596 {254, __INDIR, ""}, 597 {0, __TXT, "%soption-%d"} 598}; 599 600/* 601 * parse a dhcp response, set environment variables translating options 602 * names and values according to the tables above. Also set dhcp.tags 603 * to the list of selected tags. 604 */ 605static void 606setenv_(u_char *cp, u_char *ep, struct dhcp_opt *opts) 607{ 608 u_char *ncp; 609 u_char tag; 610 char tags[512], *tp; /* the list of tags */ 611 612#define FLD_SEP ',' /* separator in list of elements */ 613 ncp = cp; 614 tp = tags; 615 if (opts == NULL) 616 opts = dhcp_opt; 617 618 while (ncp < ep) { 619 unsigned int size; /* option size */ 620 char *vp, *endv, buf[256]; /* the value buffer */ 621 struct dhcp_opt *op; 622 623 tag = *ncp++; /* extract tag and size */ 624 size = *ncp++; 625 cp = ncp; /* current payload */ 626 ncp += size; /* point to the next option */ 627 628 if (tag == TAG_END) 629 break; 630 if (tag == 0) 631 continue; 632 633 for (op = opts+1; op->tag && op->tag != tag; op++) 634 ; 635 /* if not found we end up on the default entry */ 636 637 /* 638 * Copy data into the buffer. libstand does not have snprintf so we 639 * need to be careful with sprintf(). With strings, the source is 640 * always <256 char so shorter than the buffer so we are safe; with 641 * other arguments, the longest string is inet_ntoa which is 16 bytes 642 * so we make sure to have always enough room in the string before 643 * trying an sprint. 644 */ 645 vp = buf; 646 *vp = '\0'; 647 endv = buf + sizeof(buf) - 1 - 16; /* last valid write position */ 648 649 switch(op->fmt) { 650 case __NONE: 651 break; /* should not happen */ 652 653 case __VE: /* recurse, vendor specific */ 654 setenv_(cp, cp+size, vndr_opt); 655 break; 656 657 case __IP: /* ip address */ 658 for (; size > 0 && vp < endv; size -= 4, cp += 4) { 659 struct in_addr in_ip; /* ip addresses */ 660 if (vp != buf) 661 *vp++ = FLD_SEP; 662 bcopy(cp, &in_ip.s_addr, sizeof(in_ip.s_addr)); 663 sprintf(vp, "%s", inet_ntoa(in_ip)); 664 vp += strlen(vp); 665 } 666 break; 667 668 case __BYTES: /* opaque byte string */ 669 for (; size > 0 && vp < endv; size -= 1, cp += 1) { 670 sprintf(vp, "%02x", *cp); 671 vp += strlen(vp); 672 } 673 break; 674 675 case __TXT: 676 bcopy(cp, buf, size); /* cannot overflow */ 677 buf[size] = 0; 678 break; 679 680 case __32: 681 case __16: 682 case __8: /* op->fmt is also the length of each field */ 683 for (; size > 0 && vp < endv; size -= op->fmt, cp += op->fmt) { 684 uint32_t v; 685 if (op->fmt == __32) 686 v = (cp[0]<<24) + (cp[1]<<16) + (cp[2]<<8) + cp[3]; 687 else if (op->fmt == __16) 688 v = (cp[0]<<8) + cp[1]; 689 else 690 v = cp[0]; 691 if (vp != buf) 692 *vp++ = FLD_SEP; 693 sprintf(vp, "%u", v); 694 vp += strlen(vp); 695 } 696 break; 697 698 case __INDIR: /* name=value */ 699 case __ILIST: /* name=value;name=value... */ 700 bcopy(cp, buf, size); /* cannot overflow */ 701 buf[size] = '\0'; 702 for (endv = buf; endv; endv = vp) { 703 u_char *s = NULL; /* semicolon ? */ 704 705 /* skip leading whitespace */ 706 while (*endv && index(" \t\n\r", *endv)) 707 endv++; 708 vp = index(endv, '='); /* find name=value separator */ 709 if (!vp) 710 break; 711 *vp++ = 0; 712 if (op->fmt == __ILIST && (s = index(vp, ';'))) 713 *s++ = '\0'; 714 setenv(endv, vp, 1); 715 vp = s; /* prepare for next round */ 716 } 717 buf[0] = '\0'; /* option already done */ 718 } 719 720 if (tp - tags < sizeof(tags) - 5) { /* add tag to the list */ 721 if (tp != tags) 722 *tp++ = FLD_SEP; 723 sprintf(tp, "%d", tag); 724 tp += strlen(tp); 725 } 726 if (buf[0]) { 727 char env[128]; /* the string name */ 728 729 if (op->tag == 0) 730 sprintf(env, op->desc, opts[0].desc, tag); 731 else 732 sprintf(env, "%s%s", opts[0].desc, op->desc); 733 setenv(env, buf, 1); 734 } 735 } 736 if (tp != tags) { 737 char env[128]; /* the string name */ 738 sprintf(env, "%stags", opts[0].desc); 739 setenv(env, tags, 1); 740 } 741} 742#endif /* additional dhcp */ |
|