1 /* $NetBSD: bootp.c,v 1.14 1998/02/16 11:10:54 drochner Exp $ */
4 * Copyright (c) 1992 Regents of the University of California.
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
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 * 3. 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
35 * @(#) Header: bootp.c,v 1.4 93/09/11 03:13:51 leres Exp (LBL)
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 #include <sys/types.h>
43 #include <sys/limits.h>
44 #include <sys/endian.h>
45 #include <netinet/in.h>
46 #include <netinet/in_systm.h>
53 #define DHCP_ENV_NOVENDOR 1 /* do not parse vendor options */
54 #define DHCP_ENV_PXE 10 /* assume pxe vendor options */
55 #define DHCP_ENV_FREEBSD 11 /* assume freebsd vendor options */
56 /* set DHCP_ENV to one of the values above to export dhcp options to kenv */
57 #define DHCP_ENV DHCP_ENV_NO_VENDOR
65 struct in_addr servip;
69 static char vm_rfc1048[4] = VM_RFC1048;
71 static char vm_cmu[4] = VM_CMU;
75 static ssize_t bootpsend(struct iodesc *, void *, size_t);
76 static ssize_t bootprecv(struct iodesc *, void **, void **, time_t);
77 static int vend_rfc1048(u_char *, u_int);
79 static void vend_cmu(u_char *);
82 #ifdef DHCP_ENV /* export the dhcp response to kenv */
84 static void setenv_(u_char *cp, u_char *ep, struct dhcp_opt *opts);
86 #define setenv_(a, b, c)
90 static char expected_dhcpmsgtype = -1, dhcp_ok;
91 struct in_addr dhcp_serverip;
93 struct bootp *bootp_response;
94 size_t bootp_response_size;
97 bootp_fill_request(unsigned char *bp_vend)
100 * We are booting from PXE, we want to send the string
101 * 'PXEClient' to the DHCP server so you have the option of
102 * only responding to PXE aware dhcp requests.
104 bp_vend[0] = TAG_CLASSID;
106 bcopy("PXEClient", &bp_vend[2], 9);
107 bp_vend[11] = TAG_USER_CLASS;
108 /* len of each user class + number of user class */
110 /* len of the first user class */
112 bcopy("FreeBSD", &bp_vend[14], 7);
113 bp_vend[21] = TAG_PARAM_REQ;
115 bp_vend[23] = TAG_ROOTPATH;
116 bp_vend[24] = TAG_HOSTNAME;
117 bp_vend[25] = TAG_SWAPSERVER;
118 bp_vend[26] = TAG_GATEWAY;
119 bp_vend[27] = TAG_SUBNET_MASK;
120 bp_vend[28] = TAG_INTF_MTU;
121 bp_vend[29] = TAG_SERVERID;
122 bp_vend[30] = TAG_END;
125 /* Fetch required bootp infomation */
133 u_char header[HEADER_SIZE];
136 struct bootp *rbootp;
140 printf("bootp: socket=%d\n", sock);
145 if (!(d = socktodesc(sock))) {
146 printf("bootp: bad socket. %d\n", sock);
151 printf("bootp: d=%lx\n", (long)d);
155 bzero(bp, sizeof(*bp));
157 bp->bp_op = BOOTREQUEST;
158 bp->bp_htype = 1; /* 10Mb Ethernet (48 bits) */
160 bp->bp_xid = htonl(d->xid);
161 MACPY(d->myea, bp->bp_chaddr);
162 strncpy(bp->bp_file, bootfile, sizeof(bp->bp_file));
163 bcopy(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048));
165 bp->bp_vend[4] = TAG_DHCP_MSGTYPE;
167 bp->bp_vend[6] = DHCPDISCOVER;
168 bootp_fill_request(&bp->bp_vend[7]);
171 bp->bp_vend[4] = TAG_END;
174 d->myip.s_addr = INADDR_ANY;
175 d->myport = htons(IPPORT_BOOTPC);
176 d->destip.s_addr = INADDR_BROADCAST;
177 d->destport = htons(IPPORT_BOOTPS);
180 expected_dhcpmsgtype = DHCPOFFER;
185 bootpsend, bp, sizeof(*bp),
186 bootprecv, &pkt, (void **)&rbootp) == -1) {
187 printf("bootp: no reply\n");
194 bp->bp_vend[6] = DHCPREQUEST;
195 bp->bp_vend[7] = TAG_REQ_ADDR;
197 bcopy(&rbootp->bp_yiaddr, &bp->bp_vend[9], 4);
198 bp->bp_vend[13] = TAG_SERVERID;
200 bcopy(&dhcp_serverip.s_addr, &bp->bp_vend[15], 4);
201 bp->bp_vend[19] = TAG_LEASETIME;
203 leasetime = htonl(300);
204 bcopy(&leasetime, &bp->bp_vend[21], 4);
205 bootp_fill_request(&bp->bp_vend[25]);
207 expected_dhcpmsgtype = DHCPACK;
211 bootpsend, bp, sizeof(*bp),
212 bootprecv, &pkt, (void **)&rbootp) == -1) {
213 printf("DHCPREQUEST failed\n");
219 myip = d->myip = rbootp->bp_yiaddr;
220 servip = rbootp->bp_siaddr;
221 if (rootip.s_addr == INADDR_ANY)
223 bcopy(rbootp->bp_file, bootfile, sizeof(bootfile));
224 bootfile[sizeof(bootfile) - 1] = '\0';
227 if (IN_CLASSA(ntohl(myip.s_addr)))
228 netmask = htonl(IN_CLASSA_NET);
229 else if (IN_CLASSB(ntohl(myip.s_addr)))
230 netmask = htonl(IN_CLASSB_NET);
232 netmask = htonl(IN_CLASSC_NET);
235 printf("'native netmask' is %s\n", intoa(netmask));
241 printf("mask: %s\n", intoa(netmask));
244 /* We need a gateway if root is on a different net */
245 if (!SAMENET(myip, rootip, netmask)) {
248 printf("need gateway for root ip\n");
252 /* Toss gateway if on a different net */
253 if (!SAMENET(myip, gateip, netmask)) {
256 printf("gateway ip (%s) bad\n", inet_ntoa(gateip));
261 /* Bump xid so next request will be unique. */
266 /* Transmit a bootp request */
268 bootpsend(struct iodesc *d, void *pkt, size_t len)
274 printf("bootpsend: d=%lx called.\n", (long)d);
278 bp->bp_secs = htons((u_short)(getsecs() - bot));
282 printf("bootpsend: calling sendudp\n");
285 return (sendudp(d, pkt, len));
289 bootprecv(struct iodesc *d, void **pkt, void **payload, time_t tleft)
297 printf("bootp_recvoffer: called\n");
301 n = readudp(d, &ptr, (void **)&bp, tleft);
302 if (n == -1 || n < sizeof(struct bootp) - BOOTP_VENDSIZE)
307 printf("bootprecv: checked. bp = %p, n = %zd\n", bp, n);
309 if (bp->bp_xid != htonl(d->xid)) {
312 printf("bootprecv: expected xid 0x%lx, got 0x%x\n",
313 d->xid, ntohl(bp->bp_xid));
321 printf("bootprecv: got one!\n");
324 /* Suck out vendor info */
325 if (bcmp(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)) == 0) {
326 int vsize = n - offsetof(struct bootp, bp_vend);
327 if (vend_rfc1048(bp->bp_vend, vsize) != 0)
330 /* Save copy of bootp reply or DHCP ACK message */
331 if (bp->bp_op == BOOTREPLY &&
332 ((dhcp_ok == 1 && expected_dhcpmsgtype == DHCPACK) ||
334 free(bootp_response);
335 bootp_response = malloc(n);
336 if (bootp_response != NULL) {
337 bootp_response_size = n;
338 bcopy(bp, bootp_response, bootp_response_size);
342 #ifdef BOOTP_VEND_CMU
343 else if (bcmp(vm_cmu, bp->bp_vend, sizeof(vm_cmu)) == 0)
344 vend_cmu(bp->bp_vend);
347 printf("bootprecv: unknown vendor 0x%lx\n", (long)bp->bp_vend);
359 dhcp_try_rfc1048(u_char *cp, u_int len)
362 expected_dhcpmsgtype = DHCPACK;
363 if (bcmp(vm_rfc1048, cp, sizeof(vm_rfc1048)) == 0) {
364 return (vend_rfc1048(cp, len));
370 vend_rfc1048(u_char *cp, u_int len)
379 printf("vend_rfc1048 bootp info. len=%d\n", len);
383 /* Step over magic cookie */
386 setenv_(cp, ep, NULL);
394 if (tag == TAG_SUBNET_MASK) {
395 bcopy(cp, &netmask, sizeof(netmask));
397 if (tag == TAG_GATEWAY) {
398 bcopy(cp, &gateip.s_addr, sizeof(gateip.s_addr));
400 if (tag == TAG_SWAPSERVER) {
401 /* let it override bp_siaddr */
402 bcopy(cp, &rootip.s_addr, sizeof(rootip.s_addr));
404 if (tag == TAG_ROOTPATH) {
405 if ((val = getenv("dhcp.root-path")) == NULL)
406 val = (const char *)cp;
407 strlcpy(rootpath, val, sizeof(rootpath));
409 if (tag == TAG_HOSTNAME) {
410 if ((val = getenv("dhcp.host-name")) == NULL)
411 val = (const char *)cp;
412 strlcpy(hostname, val, sizeof(hostname));
414 if (tag == TAG_INTF_MTU) {
416 if ((val = getenv("dhcp.interface-mtu")) != NULL) {
422 * Do not allow MTU to exceed max IPv4 packet
423 * size, max value of 16-bit word.
425 tmp = strtoul(val, &end, 0);
427 *val == '\0' || *end != '\0' ||
429 printf("%s: bad value: \"%s\", "
431 "dhcp.interface-mtu", val);
433 intf_mtu = (u_int)tmp;
437 intf_mtu = be16dec(cp);
440 if (tag == TAG_DHCP_MSGTYPE) {
441 if(*cp != expected_dhcpmsgtype)
445 if (tag == TAG_SERVERID) {
446 bcopy(cp, &dhcp_serverip.s_addr,
447 sizeof(dhcp_serverip.s_addr));
455 #ifdef BOOTP_VEND_CMU
463 printf("vend_cmu bootp info.\n");
465 vp = (struct cmu_vend *)cp;
467 if (vp->v_smask.s_addr != 0) {
468 netmask = vp->v_smask.s_addr;
470 if (vp->v_dgate.s_addr != 0) {
471 gateip = vp->v_dgate;
478 * Parse DHCP options and store them into kenv variables.
479 * Original code from Danny Braniss, modifications by Luigi Rizzo.
481 * The parser is driven by tables which specify the type and name of
482 * each dhcp option and how it appears in kenv.
483 * The first entry in the list contains the prefix used to set the kenv
484 * name (including the . if needed), the last entry must have a 0 tag.
485 * Entries do not need to be sorted though it helps for readability.
487 * Certain vendor-specific tables can be enabled according to DHCP_ENV.
488 * Set it to 0 if you don't want any.
490 enum opt_fmt { __NONE = 0,
491 __8 = 1, __16 = 2, __32 = 4, /* Unsigned fields, value=size */
492 __IP, /* IPv4 address */
493 __TXT, /* C string */
494 __BYTES, /* byte sequence, printed %02x */
495 __INDIR, /* name=value */
496 __ILIST, /* name=value;name=value ... */
497 __VE, /* vendor specific, recurse */
506 static struct dhcp_opt vndr_opt[] = { /* Vendor Specific Options */
507 #if DHCP_ENV == DHCP_ENV_FREEBSD /* FreeBSD table in the original code */
508 {0, 0, "FreeBSD"}, /* prefix */
509 {1, __TXT, "kernel"},
510 {2, __TXT, "kernelname"},
511 {3, __TXT, "kernel_options"},
513 {5, __TXT, "conf-path"},
514 {6, __TXT, "rc.conf0"},
515 {7, __TXT, "rc.conf1"},
516 {8, __TXT, "rc.conf2"},
517 {9, __TXT, "rc.conf3"},
518 {10, __TXT, "rc.conf4"},
519 {11, __TXT, "rc.conf5"},
520 {12, __TXT, "rc.conf6"},
521 {13, __TXT, "rc.conf7"},
522 {14, __TXT, "rc.conf8"},
523 {15, __TXT, "rc.conf9"},
525 {20, __TXT, "boot.nfsroot.options"},
538 #elif DHCP_ENV == DHCP_ENV_PXE /* some pxe options, RFC4578 */
539 {0, 0, "pxe"}, /* prefix */
540 {93, __16, "system-architecture"},
541 {94, __BYTES, "network-interface"},
542 {97, __BYTES, "machine-identifier"},
543 #else /* default (empty) table */
544 {0, 0, "dhcp.vendor."}, /* prefix */
546 {0, __TXT, "%soption-%d"}
549 static struct dhcp_opt dhcp_opt[] = {
550 /* DHCP Option names, formats and codes, from RFC2132. */
551 {0, 0, "dhcp."}, // prefix
552 {1, __IP, "subnet-mask"},
553 {2, __32, "time-offset"}, /* this is signed */
554 {3, __IP, "routers"},
555 {4, __IP, "time-servers"},
556 {5, __IP, "ien116-name-servers"},
557 {6, __IP, "domain-name-servers"},
558 {7, __IP, "log-servers"},
559 {8, __IP, "cookie-servers"},
560 {9, __IP, "lpr-servers"},
561 {10, __IP, "impress-servers"},
562 {11, __IP, "resource-location-servers"},
563 {12, __TXT, "host-name"},
564 {13, __16, "boot-size"},
565 {14, __TXT, "merit-dump"},
566 {15, __TXT, "domain-name"},
567 {16, __IP, "swap-server"},
568 {17, __TXT, "root-path"},
569 {18, __TXT, "extensions-path"},
570 {19, __8, "ip-forwarding"},
571 {20, __8, "non-local-source-routing"},
572 {21, __IP, "policy-filter"},
573 {22, __16, "max-dgram-reassembly"},
574 {23, __8, "default-ip-ttl"},
575 {24, __32, "path-mtu-aging-timeout"},
576 {25, __16, "path-mtu-plateau-table"},
577 {26, __16, "interface-mtu"},
578 {27, __8, "all-subnets-local"},
579 {28, __IP, "broadcast-address"},
580 {29, __8, "perform-mask-discovery"},
581 {30, __8, "mask-supplier"},
582 {31, __8, "perform-router-discovery"},
583 {32, __IP, "router-solicitation-address"},
584 {33, __IP, "static-routes"},
585 {34, __8, "trailer-encapsulation"},
586 {35, __32, "arp-cache-timeout"},
587 {36, __8, "ieee802-3-encapsulation"},
588 {37, __8, "default-tcp-ttl"},
589 {38, __32, "tcp-keepalive-interval"},
590 {39, __8, "tcp-keepalive-garbage"},
591 {40, __TXT, "nis-domain"},
592 {41, __IP, "nis-servers"},
593 {42, __IP, "ntp-servers"},
594 {43, __VE, "vendor-encapsulated-options"},
595 {44, __IP, "netbios-name-servers"},
596 {45, __IP, "netbios-dd-server"},
597 {46, __8, "netbios-node-type"},
598 {47, __TXT, "netbios-scope"},
599 {48, __IP, "x-font-servers"},
600 {49, __IP, "x-display-managers"},
601 {50, __IP, "dhcp-requested-address"},
602 {51, __32, "dhcp-lease-time"},
603 {52, __8, "dhcp-option-overload"},
604 {53, __8, "dhcp-message-type"},
605 {54, __IP, "dhcp-server-identifier"},
606 {55, __8, "dhcp-parameter-request-list"},
607 {56, __TXT, "dhcp-message"},
608 {57, __16, "dhcp-max-message-size"},
609 {58, __32, "dhcp-renewal-time"},
610 {59, __32, "dhcp-rebinding-time"},
611 {60, __TXT, "vendor-class-identifier"},
612 {61, __TXT, "dhcp-client-identifier"},
613 {64, __TXT, "nisplus-domain"},
614 {65, __IP, "nisplus-servers"},
615 {66, __TXT, "tftp-server-name"},
616 {67, __TXT, "bootfile-name"},
617 {68, __IP, "mobile-ip-home-agent"},
618 {69, __IP, "smtp-server"},
619 {70, __IP, "pop-server"},
620 {71, __IP, "nntp-server"},
621 {72, __IP, "www-server"},
622 {73, __IP, "finger-server"},
623 {74, __IP, "irc-server"},
624 {75, __IP, "streettalk-server"},
625 {76, __IP, "streettalk-directory-assistance-server"},
626 {77, __TXT, "user-class"},
627 {85, __IP, "nds-servers"},
628 {86, __TXT, "nds-tree-name"},
629 {87, __TXT, "nds-context"},
630 {210, __TXT, "authenticate"},
632 /* use the following entries for arbitrary variables */
642 {0, __TXT, "%soption-%d"}
646 * parse a dhcp response, set environment variables translating options
647 * names and values according to the tables above. Also set dhcp.tags
648 * to the list of selected tags.
651 setenv_(u_char *cp, u_char *ep, struct dhcp_opt *opts)
655 char tags[512], *tp; /* the list of tags */
657 #define FLD_SEP ',' /* separator in list of elements */
664 unsigned int size; /* option size */
665 char *vp, *endv, buf[256]; /* the value buffer */
668 tag = *ncp++; /* extract tag and size */
670 cp = ncp; /* current payload */
671 ncp += size; /* point to the next option */
678 for (op = opts+1; op->tag && op->tag != tag; op++)
680 /* if not found we end up on the default entry */
683 * Copy data into the buffer. libstand does not have snprintf so we
684 * need to be careful with sprintf(). With strings, the source is
685 * always <256 char so shorter than the buffer so we are safe; with
686 * other arguments, the longest string is inet_ntoa which is 16 bytes
687 * so we make sure to have always enough room in the string before
692 endv = buf + sizeof(buf) - 1 - 16; /* last valid write position */
696 break; /* should not happen */
698 case __VE: /* recurse, vendor specific */
699 setenv_(cp, cp+size, vndr_opt);
702 case __IP: /* ip address */
703 for (; size > 0 && vp < endv; size -= 4, cp += 4) {
704 struct in_addr in_ip; /* ip addresses */
707 bcopy(cp, &in_ip.s_addr, sizeof(in_ip.s_addr));
708 sprintf(vp, "%s", inet_ntoa(in_ip));
713 case __BYTES: /* opaque byte string */
714 for (; size > 0 && vp < endv; size -= 1, cp += 1) {
715 sprintf(vp, "%02x", *cp);
721 bcopy(cp, buf, size); /* cannot overflow */
727 case __8: /* op->fmt is also the length of each field */
728 for (; size > 0 && vp < endv; size -= op->fmt, cp += op->fmt) {
731 v = (cp[0]<<24) + (cp[1]<<16) + (cp[2]<<8) + cp[3];
732 else if (op->fmt == __16)
733 v = (cp[0]<<8) + cp[1];
738 sprintf(vp, "%u", v);
743 case __INDIR: /* name=value */
744 case __ILIST: /* name=value;name=value... */
745 bcopy(cp, buf, size); /* cannot overflow */
747 for (endv = buf; endv; endv = vp) {
748 u_char *s = NULL; /* semicolon ? */
750 /* skip leading whitespace */
751 while (*endv && strchr(" \t\n\r", *endv))
753 vp = strchr(endv, '='); /* find name=value separator */
757 if (op->fmt == __ILIST && (s = strchr(vp, ';')))
760 vp = s; /* prepare for next round */
762 buf[0] = '\0'; /* option already done */
765 if (tp - tags < sizeof(tags) - 5) { /* add tag to the list */
768 sprintf(tp, "%d", tag);
772 char env[128]; /* the string name */
775 sprintf(env, op->desc, opts[0].desc, tag);
777 sprintf(env, "%s%s", opts[0].desc, op->desc);
779 * Do not replace existing values in the environment, so that
780 * locally-obtained values can override server-provided values.
786 char env[128]; /* the string name */
787 sprintf(env, "%stags", opts[0].desc);
788 setenv(env, tags, 1);
791 #endif /* additional dhcp */