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 * 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
35 * @(#) Header: bootp.c,v 1.4 93/09/11 03:13:51 leres Exp (LBL)
38 #include <sys/cdefs.h>
40 #include <sys/types.h>
41 #include <sys/limits.h>
42 #include <sys/endian.h>
43 #include <netinet/in.h>
44 #include <netinet/in_systm.h>
51 #define DHCP_ENV_NOVENDOR 1 /* do not parse vendor options */
52 #define DHCP_ENV_PXE 10 /* assume pxe vendor options */
53 #define DHCP_ENV_FREEBSD 11 /* assume freebsd vendor options */
54 /* set DHCP_ENV to one of the values above to export dhcp options to kenv */
55 #define DHCP_ENV DHCP_ENV_NO_VENDOR
63 struct in_addr servip
;
67 static char vm_rfc1048
[4] = VM_RFC1048
;
69 static char vm_cmu
[4] = VM_CMU
;
73 static ssize_t
bootpsend(struct iodesc
*, void *, size_t);
74 static ssize_t
bootprecv(struct iodesc
*, void *, size_t, time_t);
75 static int vend_rfc1048(u_char
*, u_int
);
77 static void vend_cmu(u_char
*);
80 #ifdef DHCP_ENV /* export the dhcp response to kenv */
82 static void setenv_(u_char
*cp
, u_char
*ep
, struct dhcp_opt
*opts
);
84 #define setenv_(a, b, c)
88 static char expected_dhcpmsgtype
= -1, dhcp_ok
;
89 struct in_addr dhcp_serverip
;
91 struct bootp
*bootp_response
;
93 /* Fetch required bootp infomation */
95 bootp(int sock
, int flag
)
100 u_char header
[HEADER_SIZE
];
104 u_char header
[HEADER_SIZE
];
110 printf("bootp: socket=%d\n", sock
);
115 if (!(d
= socktodesc(sock
))) {
116 printf("bootp: bad socket. %d\n", sock
);
121 printf("bootp: d=%lx\n", (long)d
);
125 bzero(bp
, sizeof(*bp
));
127 bp
->bp_op
= BOOTREQUEST
;
128 bp
->bp_htype
= 1; /* 10Mb Ethernet (48 bits) */
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
));
135 bp
->bp_vend
[4] = TAG_DHCP_MSGTYPE
;
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
;
147 bcopy("PXEClient", &bp
->bp_vend
[9], 9);
148 bp
->bp_vend
[18] = TAG_PARAM_REQ
;
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
;
159 bp
->bp_vend
[7] = TAG_END
;
161 bp
->bp_vend
[4] = TAG_END
;
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
);
170 expected_dhcpmsgtype
= DHCPOFFER
;
175 bootpsend
, bp
, sizeof(*bp
),
176 bootprecv
, &rbuf
.rbootp
, sizeof(rbuf
.rbootp
))
178 printf("bootp: no reply\n");
185 bp
->bp_vend
[6] = DHCPREQUEST
;
186 bp
->bp_vend
[7] = TAG_REQ_ADDR
;
188 bcopy(&rbuf
.rbootp
.bp_yiaddr
, &bp
->bp_vend
[9], 4);
189 bp
->bp_vend
[13] = TAG_SERVERID
;
191 bcopy(&dhcp_serverip
.s_addr
, &bp
->bp_vend
[15], 4);
192 bp
->bp_vend
[19] = TAG_LEASETIME
;
194 leasetime
= htonl(300);
195 bcopy(&leasetime
, &bp
->bp_vend
[21], 4);
196 if (flag
& BOOTP_PXE
) {
197 bp
->bp_vend
[25] = TAG_CLASSID
;
199 bcopy("PXEClient", &bp
->bp_vend
[27], 9);
200 bp
->bp_vend
[36] = TAG_END
;
202 bp
->bp_vend
[25] = TAG_END
;
204 expected_dhcpmsgtype
= DHCPACK
;
207 bootpsend
, bp
, sizeof(*bp
),
208 bootprecv
, &rbuf
.rbootp
, sizeof(rbuf
.rbootp
))
210 printf("DHCPREQUEST failed\n");
216 myip
= d
->myip
= rbuf
.rbootp
.bp_yiaddr
;
217 servip
= rbuf
.rbootp
.bp_siaddr
;
218 if(rootip
.s_addr
== INADDR_ANY
) rootip
= servip
;
219 bcopy(rbuf
.rbootp
.bp_file
, bootfile
, sizeof(bootfile
));
220 bootfile
[sizeof(bootfile
) - 1] = '\0';
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
);
228 netmask
= htonl(IN_CLASSC_NET
);
231 printf("'native netmask' is %s\n", intoa(netmask
));
237 printf("mask: %s\n", intoa(netmask
));
240 /* We need a gateway if root is on a different net */
241 if (!SAMENET(myip
, rootip
, netmask
)) {
244 printf("need gateway for root ip\n");
248 /* Toss gateway if on a different net */
249 if (!SAMENET(myip
, gateip
, netmask
)) {
252 printf("gateway ip (%s) bad\n", inet_ntoa(gateip
));
257 /* Bump xid so next request will be unique. */
261 /* Transmit a bootp request */
263 bootpsend(struct iodesc
*d
, void *pkt
, size_t len
)
269 printf("bootpsend: d=%lx called.\n", (long)d
);
273 bp
->bp_secs
= htons((u_short
)(getsecs() - bot
));
277 printf("bootpsend: calling sendudp\n");
280 return (sendudp(d
, pkt
, len
));
284 bootprecv(struct iodesc
*d
, void *pkt
, size_t len
, time_t tleft
)
291 printf("bootp_recvoffer: called\n");
294 n
= readudp(d
, pkt
, len
, tleft
);
295 if (n
== -1 || n
< sizeof(struct bootp
) - BOOTP_VENDSIZE
)
298 bp
= (struct bootp
*)pkt
;
302 printf("bootprecv: checked. bp = 0x%lx, n = %d\n",
305 if (bp
->bp_xid
!= htonl(d
->xid
)) {
308 printf("bootprecv: expected xid 0x%lx, got 0x%x\n",
309 d
->xid
, ntohl(bp
->bp_xid
));
317 printf("bootprecv: got one!\n");
320 /* Suck out vendor info */
321 if (bcmp(vm_rfc1048
, bp
->bp_vend
, sizeof(vm_rfc1048
)) == 0) {
322 if(vend_rfc1048(bp
->bp_vend
, sizeof(bp
->bp_vend
)) != 0)
325 /* Save copy of bootp reply or DHCP ACK message */
326 if (bp
->bp_op
== BOOTREPLY
&&
327 ((dhcp_ok
== 1 && expected_dhcpmsgtype
== DHCPACK
) ||
329 free(bootp_response
);
330 bootp_response
= malloc(sizeof (*bootp_response
));
331 if (bootp_response
!= NULL
) {
332 bcopy(bp
, bootp_response
,
333 sizeof (*bootp_response
));
337 #ifdef BOOTP_VEND_CMU
338 else if (bcmp(vm_cmu
, bp
->bp_vend
, sizeof(vm_cmu
)) == 0)
339 vend_cmu(bp
->bp_vend
);
342 printf("bootprecv: unknown vendor 0x%lx\n", (long)bp
->bp_vend
);
351 dhcp_try_rfc1048(uint8_t *cp
, size_t len
)
354 expected_dhcpmsgtype
= DHCPACK
;
355 if (bcmp(vm_rfc1048
, cp
, sizeof (vm_rfc1048
)) == 0) {
356 return (vend_rfc1048(cp
, len
));
362 vend_rfc1048(u_char
*cp
, u_int len
)
371 printf("vend_rfc1048 bootp info. len=%d\n", len
);
375 /* Step over magic cookie */
378 setenv_(cp
, ep
, NULL
);
386 if (tag
== TAG_SUBNET_MASK
) {
387 bcopy(cp
, &netmask
, sizeof(netmask
));
389 if (tag
== TAG_GATEWAY
) {
390 bcopy(cp
, &gateip
.s_addr
, sizeof(gateip
.s_addr
));
392 if (tag
== TAG_SWAPSERVER
) {
393 /* let it override bp_siaddr */
394 bcopy(cp
, &rootip
.s_addr
, sizeof(rootip
.s_addr
));
396 if (tag
== TAG_ROOTPATH
) {
397 if ((val
= getenv("dhcp.root-path")) == NULL
)
398 val
= (const char *)cp
;
399 strlcpy(rootpath
, val
, sizeof(rootpath
));
401 if (tag
== TAG_HOSTNAME
) {
402 if ((val
= getenv("dhcp.host-name")) == NULL
)
403 val
= (const char *)cp
;
404 strlcpy(hostname
, val
, sizeof(hostname
));
406 if (tag
== TAG_INTF_MTU
) {
408 if ((val
= getenv("dhcp.interface-mtu")) != NULL
) {
414 * Do not allow MTU to exceed max IPv4 packet
415 * size, max value of 16-bit word.
417 tmp
= strtoul(val
, &end
, 0);
419 *val
== '\0' || *end
!= '\0' ||
421 printf("%s: bad value: \"%s\", "
423 "dhcp.interface-mtu", val
);
425 intf_mtu
= (u_int
)tmp
;
429 intf_mtu
= be16dec(cp
);
432 if (tag
== TAG_DHCP_MSGTYPE
) {
433 if(*cp
!= expected_dhcpmsgtype
)
437 if (tag
== TAG_SERVERID
) {
438 bcopy(cp
, &dhcp_serverip
.s_addr
,
439 sizeof(dhcp_serverip
.s_addr
));
447 #ifdef BOOTP_VEND_CMU
455 printf("vend_cmu bootp info.\n");
457 vp
= (struct cmu_vend
*)cp
;
459 if (vp
->v_smask
.s_addr
!= 0) {
460 netmask
= vp
->v_smask
.s_addr
;
462 if (vp
->v_dgate
.s_addr
!= 0) {
463 gateip
= vp
->v_dgate
;
470 * Parse DHCP options and store them into kenv variables.
471 * Original code from Danny Braniss, modifications by Luigi Rizzo.
473 * The parser is driven by tables which specify the type and name of
474 * each dhcp option and how it appears in kenv.
475 * The first entry in the list contains the prefix used to set the kenv
476 * name (including the . if needed), the last entry must have a 0 tag.
477 * Entries do not need to be sorted though it helps for readability.
479 * Certain vendor-specific tables can be enabled according to DHCP_ENV.
480 * Set it to 0 if you don't want any.
482 enum opt_fmt
{ __NONE
= 0,
483 __8
= 1, __16
= 2, __32
= 4, /* Unsigned fields, value=size */
484 __IP
, /* IPv4 address */
485 __TXT
, /* C string */
486 __BYTES
, /* byte sequence, printed %02x */
487 __INDIR
, /* name=value */
488 __ILIST
, /* name=value;name=value ... */
489 __VE
, /* vendor specific, recurse */
498 static struct dhcp_opt vndr_opt
[] = { /* Vendor Specific Options */
499 #if DHCP_ENV == DHCP_ENV_FREEBSD /* FreeBSD table in the original code */
500 {0, 0, "FreeBSD"}, /* prefix */
501 {1, __TXT
, "kernel"},
502 {2, __TXT
, "kernelname"},
503 {3, __TXT
, "kernel_options"},
505 {5, __TXT
, "conf-path"},
506 {6, __TXT
, "rc.conf0"},
507 {7, __TXT
, "rc.conf1"},
508 {8, __TXT
, "rc.conf2"},
509 {9, __TXT
, "rc.conf3"},
510 {10, __TXT
, "rc.conf4"},
511 {11, __TXT
, "rc.conf5"},
512 {12, __TXT
, "rc.conf6"},
513 {13, __TXT
, "rc.conf7"},
514 {14, __TXT
, "rc.conf8"},
515 {15, __TXT
, "rc.conf9"},
517 {20, __TXT
, "boot.nfsroot.options"},
530 #elif DHCP_ENV == DHCP_ENV_PXE /* some pxe options, RFC4578 */
531 {0, 0, "pxe"}, /* prefix */
532 {93, __16
, "system-architecture"},
533 {94, __BYTES
, "network-interface"},
534 {97, __BYTES
, "machine-identifier"},
535 #else /* default (empty) table */
536 {0, 0, "dhcp.vendor."}, /* prefix */
538 {0, __TXT
, "%soption-%d"}
541 static struct dhcp_opt dhcp_opt
[] = {
542 /* DHCP Option names, formats and codes, from RFC2132. */
543 {0, 0, "dhcp."}, // prefix
544 {1, __IP
, "subnet-mask"},
545 {2, __32
, "time-offset"}, /* this is signed */
546 {3, __IP
, "routers"},
547 {4, __IP
, "time-servers"},
548 {5, __IP
, "ien116-name-servers"},
549 {6, __IP
, "domain-name-servers"},
550 {7, __IP
, "log-servers"},
551 {8, __IP
, "cookie-servers"},
552 {9, __IP
, "lpr-servers"},
553 {10, __IP
, "impress-servers"},
554 {11, __IP
, "resource-location-servers"},
555 {12, __TXT
, "host-name"},
556 {13, __16
, "boot-size"},
557 {14, __TXT
, "merit-dump"},
558 {15, __TXT
, "domain-name"},
559 {16, __IP
, "swap-server"},
560 {17, __TXT
, "root-path"},
561 {18, __TXT
, "extensions-path"},
562 {19, __8
, "ip-forwarding"},
563 {20, __8
, "non-local-source-routing"},
564 {21, __IP
, "policy-filter"},
565 {22, __16
, "max-dgram-reassembly"},
566 {23, __8
, "default-ip-ttl"},
567 {24, __32
, "path-mtu-aging-timeout"},
568 {25, __16
, "path-mtu-plateau-table"},
569 {26, __16
, "interface-mtu"},
570 {27, __8
, "all-subnets-local"},
571 {28, __IP
, "broadcast-address"},
572 {29, __8
, "perform-mask-discovery"},
573 {30, __8
, "mask-supplier"},
574 {31, __8
, "perform-router-discovery"},
575 {32, __IP
, "router-solicitation-address"},
576 {33, __IP
, "static-routes"},
577 {34, __8
, "trailer-encapsulation"},
578 {35, __32
, "arp-cache-timeout"},
579 {36, __8
, "ieee802-3-encapsulation"},
580 {37, __8
, "default-tcp-ttl"},
581 {38, __32
, "tcp-keepalive-interval"},
582 {39, __8
, "tcp-keepalive-garbage"},
583 {40, __TXT
, "nis-domain"},
584 {41, __IP
, "nis-servers"},
585 {42, __IP
, "ntp-servers"},
586 {43, __VE
, "vendor-encapsulated-options"},
587 {44, __IP
, "netbios-name-servers"},
588 {45, __IP
, "netbios-dd-server"},
589 {46, __8
, "netbios-node-type"},
590 {47, __TXT
, "netbios-scope"},
591 {48, __IP
, "x-font-servers"},
592 {49, __IP
, "x-display-managers"},
593 {50, __IP
, "dhcp-requested-address"},
594 {51, __32
, "dhcp-lease-time"},
595 {52, __8
, "dhcp-option-overload"},
596 {53, __8
, "dhcp-message-type"},
597 {54, __IP
, "dhcp-server-identifier"},
598 {55, __8
, "dhcp-parameter-request-list"},
599 {56, __TXT
, "dhcp-message"},
600 {57, __16
, "dhcp-max-message-size"},
601 {58, __32
, "dhcp-renewal-time"},
602 {59, __32
, "dhcp-rebinding-time"},
603 {60, __TXT
, "vendor-class-identifier"},
604 {61, __TXT
, "dhcp-client-identifier"},
605 {64, __TXT
, "nisplus-domain"},
606 {65, __IP
, "nisplus-servers"},
607 {66, __TXT
, "tftp-server-name"},
608 {67, __TXT
, "bootfile-name"},
609 {68, __IP
, "mobile-ip-home-agent"},
610 {69, __IP
, "smtp-server"},
611 {70, __IP
, "pop-server"},
612 {71, __IP
, "nntp-server"},
613 {72, __IP
, "www-server"},
614 {73, __IP
, "finger-server"},
615 {74, __IP
, "irc-server"},
616 {75, __IP
, "streettalk-server"},
617 {76, __IP
, "streettalk-directory-assistance-server"},
618 {77, __TXT
, "user-class"},
619 {85, __IP
, "nds-servers"},
620 {86, __TXT
, "nds-tree-name"},
621 {87, __TXT
, "nds-context"},
622 {210, __TXT
, "authenticate"},
624 /* use the following entries for arbitrary variables */
634 {0, __TXT
, "%soption-%d"}
638 * parse a dhcp response, set environment variables translating options
639 * names and values according to the tables above. Also set dhcp.tags
640 * to the list of selected tags.
643 setenv_(u_char
*cp
, u_char
*ep
, struct dhcp_opt
*opts
)
647 char tags
[512], *tp
; /* the list of tags */
649 #define FLD_SEP ',' /* separator in list of elements */
656 unsigned int size
; /* option size */
657 char *vp
, *endv
, buf
[256]; /* the value buffer */
660 tag
= *ncp
++; /* extract tag and size */
662 cp
= ncp
; /* current payload */
663 ncp
+= size
; /* point to the next option */
670 for (op
= opts
+1; op
->tag
&& op
->tag
!= tag
; op
++)
672 /* if not found we end up on the default entry */
675 * Copy data into the buffer. libstand does not have snprintf so we
676 * need to be careful with sprintf(). With strings, the source is
677 * always <256 char so shorter than the buffer so we are safe; with
678 * other arguments, the longest string is inet_ntoa which is 16 bytes
679 * so we make sure to have always enough room in the string before
684 endv
= buf
+ sizeof(buf
) - 1 - 16; /* last valid write position */
688 break; /* should not happen */
690 case __VE
: /* recurse, vendor specific */
691 setenv_(cp
, cp
+size
, vndr_opt
);
694 case __IP
: /* ip address */
695 for (; size
> 0 && vp
< endv
; size
-= 4, cp
+= 4) {
696 struct in_addr in_ip
; /* ip addresses */
699 bcopy(cp
, &in_ip
.s_addr
, sizeof(in_ip
.s_addr
));
700 sprintf(vp
, "%s", inet_ntoa(in_ip
));
705 case __BYTES
: /* opaque byte string */
706 for (; size
> 0 && vp
< endv
; size
-= 1, cp
+= 1) {
707 sprintf(vp
, "%02x", *cp
);
713 bcopy(cp
, buf
, size
); /* cannot overflow */
719 case __8
: /* op->fmt is also the length of each field */
720 for (; size
> 0 && vp
< endv
; size
-= op
->fmt
, cp
+= op
->fmt
) {
723 v
= (cp
[0]<<24) + (cp
[1]<<16) + (cp
[2]<<8) + cp
[3];
724 else if (op
->fmt
== __16
)
725 v
= (cp
[0]<<8) + cp
[1];
730 sprintf(vp
, "%u", v
);
735 case __INDIR
: /* name=value */
736 case __ILIST
: /* name=value;name=value... */
737 bcopy(cp
, buf
, size
); /* cannot overflow */
739 for (endv
= buf
; endv
; endv
= vp
) {
740 u_char
*s
= NULL
; /* semicolon ? */
742 /* skip leading whitespace */
743 while (*endv
&& strchr(" \t\n\r", *endv
))
745 vp
= strchr(endv
, '='); /* find name=value separator */
749 if (op
->fmt
== __ILIST
&& (s
= strchr(vp
, ';')))
752 vp
= s
; /* prepare for next round */
754 buf
[0] = '\0'; /* option already done */
757 if (tp
- tags
< sizeof(tags
) - 5) { /* add tag to the list */
760 sprintf(tp
, "%d", tag
);
764 char env
[128]; /* the string name */
767 sprintf(env
, op
->desc
, opts
[0].desc
, tag
);
769 sprintf(env
, "%s%s", opts
[0].desc
, op
->desc
);
771 * Do not replace existing values in the environment, so that
772 * locally-obtained values can override server-provided values.
778 char env
[128]; /* the string name */
779 sprintf(env
, "%stags", opts
[0].desc
);
780 setenv(env
, tags
, 1);
783 #endif /* additional dhcp */