1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 3 * 4 * Copyright (c) 1995 Gordon Ross, Adam Glass 5 * Copyright (c) 1992 Regents of the University of California. 6 * All rights reserved. 7 * 8 * This software was developed by the Computer Systems Engineering group 9 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 10 * contributed to Berkeley. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Lawrence Berkeley Laboratory and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * based on: 41 * nfs/krpc_subr.c 42 * $NetBSD: krpc_subr.c,v 1.10 1995/08/08 20:43:43 gwr Exp $ 43 */ 44 45 #define IN_HISTORICAL_NETS /* include class masks */ 46 47 #include <sys/cdefs.h> 48 #include "opt_bootp.h" 49 #include "opt_nfs.h" 50 #include "opt_rootdevname.h" 51 52 #include <sys/param.h> 53 #include <sys/systm.h> 54 #include <sys/endian.h> 55 #include <sys/jail.h> 56 #include <sys/kernel.h> 57 #include <sys/sockio.h> 58 #include <sys/malloc.h> 59 #include <sys/mount.h> 60 #include <sys/mbuf.h> 61 #include <sys/proc.h> 62 #include <sys/reboot.h> 63 #include <sys/socket.h> 64 #include <sys/socketvar.h> 65 #include <sys/sysctl.h> 66 #include <sys/uio.h> 67 68 #include <net/if.h> 69 #include <net/if_var.h> 70 #include <net/route.h> 71 #include <net/route/route_ctl.h> 72 73 #include <netinet/in.h> 74 #include <netinet/in_var.h> 75 #include <net/if_types.h> 76 #include <net/if_dl.h> 77 #include <net/vnet.h> 78 79 #include <nfs/nfsproto.h> 80 #include <nfsclient/nfs.h> 81 #include <nfs/nfsdiskless.h> 82 #include <nfs/krpc.h> 83 #include <nfs/xdr_subs.h> 84 85 #define BOOTP_MIN_LEN 300 /* Minimum size of bootp udp packet */ 86 87 #ifndef BOOTP_SETTLE_DELAY 88 #define BOOTP_SETTLE_DELAY 3 89 #endif 90 91 /* 92 * Wait 10 seconds for interface appearance 93 * USB ethernet adapters might require some time to pop up 94 */ 95 #ifndef BOOTP_IFACE_WAIT_TIMEOUT 96 #define BOOTP_IFACE_WAIT_TIMEOUT 10 97 #endif 98 99 /* 100 * What is the longest we will wait before re-sending a request? 101 * Note this is also the frequency of "RPC timeout" messages. 102 * The re-send loop count sup linearly to this maximum, so the 103 * first complaint will happen after (1+2+3+4+5)=15 seconds. 104 */ 105 #define MAX_RESEND_DELAY 5 /* seconds */ 106 107 /* Definitions from RFC951 */ 108 struct bootp_packet { 109 u_int8_t op; 110 u_int8_t htype; 111 u_int8_t hlen; 112 u_int8_t hops; 113 u_int32_t xid; 114 u_int16_t secs; 115 u_int16_t flags; 116 struct in_addr ciaddr; 117 struct in_addr yiaddr; 118 struct in_addr siaddr; 119 struct in_addr giaddr; 120 unsigned char chaddr[16]; 121 char sname[64]; 122 char file[128]; 123 unsigned char vend[1222]; 124 }; 125 126 struct bootpc_ifcontext { 127 STAILQ_ENTRY(bootpc_ifcontext) next; 128 struct bootp_packet call; 129 struct bootp_packet reply; 130 int replylen; 131 int overload; 132 union { 133 struct ifreq _ifreq; 134 struct in_aliasreq _in_alias_req; 135 } _req; 136 #define ireq _req._ifreq 137 #define iareq _req._in_alias_req 138 if_t ifp; 139 struct sockaddr_dl *sdl; 140 struct sockaddr_in myaddr; 141 struct sockaddr_in netmask; 142 struct sockaddr_in gw; 143 int gotgw; 144 int gotnetmask; 145 int gotrootpath; 146 int outstanding; 147 int sentmsg; 148 u_int32_t xid; 149 enum { 150 IF_BOOTP_UNRESOLVED, 151 IF_BOOTP_RESOLVED, 152 IF_BOOTP_FAILED, 153 IF_DHCP_UNRESOLVED, 154 IF_DHCP_OFFERED, 155 IF_DHCP_RESOLVED, 156 IF_DHCP_FAILED, 157 } state; 158 int dhcpquerytype; /* dhcp type sent */ 159 struct in_addr dhcpserver; 160 int gotdhcpserver; 161 uint16_t mtu; 162 }; 163 164 #define TAG_MAXLEN 1024 165 struct bootpc_tagcontext { 166 char buf[TAG_MAXLEN + 1]; 167 int overload; 168 int badopt; 169 int badtag; 170 int foundopt; 171 int taglen; 172 }; 173 174 struct bootpc_globalcontext { 175 STAILQ_HEAD(, bootpc_ifcontext) interfaces; 176 u_int32_t xid; 177 int any_root_overrides; 178 int gotrootpath; 179 int gotgw; 180 int ifnum; 181 int secs; 182 int starttime; 183 struct bootp_packet reply; 184 int replylen; 185 struct bootpc_ifcontext *setrootfs; 186 struct bootpc_ifcontext *sethostname; 187 struct bootpc_tagcontext tmptag; 188 struct bootpc_tagcontext tag; 189 }; 190 191 #define IPPORT_BOOTPC 68 192 #define IPPORT_BOOTPS 67 193 194 #define BOOTP_REQUEST 1 195 #define BOOTP_REPLY 2 196 197 /* Common tags */ 198 #define TAG_PAD 0 /* Pad option, implicit length 1 */ 199 #define TAG_SUBNETMASK 1 /* RFC 950 subnet mask */ 200 #define TAG_ROUTERS 3 /* Routers (in order of preference) */ 201 #define TAG_HOSTNAME 12 /* Client host name */ 202 #define TAG_ROOT 17 /* Root path */ 203 #define TAG_INTF_MTU 26 /* Interface MTU Size (RFC2132) */ 204 205 /* DHCP specific tags */ 206 #define TAG_OVERLOAD 52 /* Option Overload */ 207 #define TAG_MAXMSGSIZE 57 /* Maximum DHCP Message Size */ 208 209 #define TAG_END 255 /* End Option (i.e. no more options) */ 210 211 /* Overload values */ 212 #define OVERLOAD_FILE 1 213 #define OVERLOAD_SNAME 2 214 215 /* Site specific tags: */ 216 #define TAG_ROOTOPTS 130 217 #define TAG_COOKIE 134 /* ascii info for userland, via sysctl */ 218 219 #define TAG_DHCP_MSGTYPE 53 220 #define TAG_DHCP_REQ_ADDR 50 221 #define TAG_DHCP_SERVERID 54 222 #define TAG_DHCP_LEASETIME 51 223 224 #define TAG_VENDOR_INDENTIFIER 60 225 226 #define DHCP_NOMSG 0 227 #define DHCP_DISCOVER 1 228 #define DHCP_OFFER 2 229 #define DHCP_REQUEST 3 230 #define DHCP_ACK 5 231 232 /* NFS read/write block size */ 233 #ifndef BOOTP_BLOCKSIZE 234 #define BOOTP_BLOCKSIZE 8192 235 #endif 236 237 static char bootp_cookie[128]; 238 static struct socket *bootp_so; 239 SYSCTL_STRING(_kern, OID_AUTO, bootp_cookie, CTLFLAG_RD, 240 bootp_cookie, 0, "Cookie (T134) supplied by bootp server"); 241 242 /* mountd RPC */ 243 static int md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp, 244 int *fhsizep, struct nfs_args *args, struct thread *td); 245 static int setfs(struct sockaddr_in *addr, char *path, char *p, 246 const struct in_addr *siaddr); 247 static int getdec(char **ptr); 248 static int getip(char **ptr, struct in_addr *ip); 249 static void mountopts(struct nfs_args *args, char *p); 250 static int xdr_opaque_decode(struct mbuf **ptr, u_char *buf, int len); 251 static int xdr_int_decode(struct mbuf **ptr, int *iptr); 252 static void print_in_addr(struct in_addr addr); 253 static void print_sin_addr(struct sockaddr_in *addr); 254 static void clear_sinaddr(struct sockaddr_in *sin); 255 static void allocifctx(struct bootpc_globalcontext *gctx); 256 static void bootpc_compose_query(struct bootpc_ifcontext *ifctx, 257 struct thread *td); 258 static unsigned char *bootpc_tag(struct bootpc_tagcontext *tctx, 259 struct bootp_packet *bp, int len, int tag); 260 static void bootpc_tag_helper(struct bootpc_tagcontext *tctx, 261 unsigned char *start, int len, int tag); 262 263 #ifdef BOOTP_DEBUG 264 void bootpboot_p_iflist(void); 265 #endif 266 267 static int bootpc_call(struct bootpc_globalcontext *gctx, 268 struct thread *td); 269 270 static void bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx, 271 struct thread *td); 272 273 static void bootpc_adjust_interface(struct bootpc_ifcontext *ifctx, 274 struct bootpc_globalcontext *gctx, struct thread *td); 275 276 static void bootpc_decode_reply(struct nfsv3_diskless *nd, 277 struct bootpc_ifcontext *ifctx, 278 struct bootpc_globalcontext *gctx); 279 280 static int bootpc_received(struct bootpc_globalcontext *gctx, 281 struct bootpc_ifcontext *ifctx); 282 283 static __inline int bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx); 284 static __inline int bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx); 285 static __inline int bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx); 286 287 /* 288 * In order to have multiple active interfaces with address 0.0.0.0 289 * and be able to send data to a selected interface, we first set 290 * mask to /8 on all interfaces, and temporarily set it to /0 when 291 * doing sosend(). 292 */ 293 294 #ifdef BOOTP_DEBUG 295 static u_int 296 bootpboot_p_ifa(void *ifp, struct ifaddr *ifa, u_int count __unused) 297 { 298 299 printf("%s flags %x, addr ", 300 if_name(ifp), if_getflags(ifp)); 301 print_sin_addr((struct sockaddr_in *) ifa->ifa_addr); 302 printf(", broadcast "); 303 print_sin_addr((struct sockaddr_in *) ifa->ifa_dstaddr); 304 printf(", netmask "); 305 print_sin_addr((struct sockaddr_in *) ifa->ifa_netmask); 306 printf("\n"); 307 308 return (0); 309 } 310 311 void 312 bootpboot_p_iflist(void) 313 { 314 struct epoch_tracker et; 315 struct if_iter iter; 316 if_t ifp; 317 318 printf("Interface list:\n"); 319 NET_EPOCH_ENTER(et); 320 for (ifp = if_iter_start(&iter); ifp != NULL; ifp = if_iter_next(&iter)) 321 if_foreach_addr_type(ifp, AF_INET, bootpboot_p_ifa, ifp); 322 if_iter_finish(&iter); 323 NET_EPOCH_EXIT(et); 324 } 325 #endif /* defined(BOOTP_DEBUG) */ 326 327 static void 328 clear_sinaddr(struct sockaddr_in *sin) 329 { 330 331 bzero(sin, sizeof(*sin)); 332 sin->sin_len = sizeof(*sin); 333 sin->sin_family = AF_INET; 334 sin->sin_addr.s_addr = INADDR_ANY; /* XXX: htonl(INAADDR_ANY) ? */ 335 sin->sin_port = 0; 336 } 337 338 static void 339 allocifctx(struct bootpc_globalcontext *gctx) 340 { 341 struct bootpc_ifcontext *ifctx; 342 343 ifctx = malloc(sizeof(*ifctx), M_TEMP, M_WAITOK | M_ZERO); 344 ifctx->xid = gctx->xid; 345 #ifdef BOOTP_NO_DHCP 346 ifctx->state = IF_BOOTP_UNRESOLVED; 347 #else 348 ifctx->state = IF_DHCP_UNRESOLVED; 349 #endif 350 gctx->xid += 0x100; 351 STAILQ_INSERT_TAIL(&gctx->interfaces, ifctx, next); 352 } 353 354 static __inline int 355 bootpc_ifctx_isresolved(struct bootpc_ifcontext *ifctx) 356 { 357 358 if (ifctx->state == IF_BOOTP_RESOLVED || 359 ifctx->state == IF_DHCP_RESOLVED) 360 return 1; 361 return 0; 362 } 363 364 static __inline int 365 bootpc_ifctx_isunresolved(struct bootpc_ifcontext *ifctx) 366 { 367 368 if (ifctx->state == IF_BOOTP_UNRESOLVED || 369 ifctx->state == IF_DHCP_UNRESOLVED) 370 return 1; 371 return 0; 372 } 373 374 static __inline int 375 bootpc_ifctx_isfailed(struct bootpc_ifcontext *ifctx) 376 { 377 378 if (ifctx->state == IF_BOOTP_FAILED || 379 ifctx->state == IF_DHCP_FAILED) 380 return 1; 381 return 0; 382 } 383 384 static int 385 bootpc_received(struct bootpc_globalcontext *gctx, 386 struct bootpc_ifcontext *ifctx) 387 { 388 unsigned char dhcpreplytype; 389 char *p; 390 391 /* 392 * Need timeout for fallback to less 393 * desirable alternative. 394 */ 395 396 /* This call used for the side effect (badopt flag) */ 397 (void) bootpc_tag(&gctx->tmptag, &gctx->reply, 398 gctx->replylen, 399 TAG_END); 400 401 /* If packet is invalid, ignore it */ 402 if (gctx->tmptag.badopt != 0) 403 return 0; 404 405 p = bootpc_tag(&gctx->tmptag, &gctx->reply, 406 gctx->replylen, TAG_DHCP_MSGTYPE); 407 if (p != NULL) 408 dhcpreplytype = *p; 409 else 410 dhcpreplytype = DHCP_NOMSG; 411 412 switch (ifctx->dhcpquerytype) { 413 case DHCP_DISCOVER: 414 if (dhcpreplytype != DHCP_OFFER /* Normal DHCP offer */ 415 #ifndef BOOTP_FORCE_DHCP 416 && dhcpreplytype != DHCP_NOMSG /* Fallback to BOOTP */ 417 #endif 418 ) 419 return 0; 420 break; 421 case DHCP_REQUEST: 422 if (dhcpreplytype != DHCP_ACK) 423 return 0; 424 case DHCP_NOMSG: 425 break; 426 } 427 428 /* Ignore packet unless it gives us a root tag we didn't have */ 429 430 if ((ifctx->state == IF_BOOTP_RESOLVED || 431 (ifctx->dhcpquerytype == DHCP_DISCOVER && 432 (ifctx->state == IF_DHCP_OFFERED || 433 ifctx->state == IF_DHCP_RESOLVED))) && 434 (bootpc_tag(&gctx->tmptag, &ifctx->reply, 435 ifctx->replylen, 436 TAG_ROOT) != NULL || 437 bootpc_tag(&gctx->tmptag, &gctx->reply, 438 gctx->replylen, 439 TAG_ROOT) == NULL)) 440 return 0; 441 442 bcopy(&gctx->reply, &ifctx->reply, gctx->replylen); 443 ifctx->replylen = gctx->replylen; 444 445 /* XXX: Only reset if 'perfect' response */ 446 if (ifctx->state == IF_BOOTP_UNRESOLVED) 447 ifctx->state = IF_BOOTP_RESOLVED; 448 else if (ifctx->state == IF_DHCP_UNRESOLVED && 449 ifctx->dhcpquerytype == DHCP_DISCOVER) { 450 if (dhcpreplytype == DHCP_OFFER) 451 ifctx->state = IF_DHCP_OFFERED; 452 else 453 ifctx->state = IF_BOOTP_RESOLVED; /* Fallback */ 454 } else if (ifctx->state == IF_DHCP_OFFERED && 455 ifctx->dhcpquerytype == DHCP_REQUEST) 456 ifctx->state = IF_DHCP_RESOLVED; 457 458 if (ifctx->dhcpquerytype == DHCP_DISCOVER && 459 ifctx->state != IF_BOOTP_RESOLVED) { 460 p = bootpc_tag(&gctx->tmptag, &ifctx->reply, 461 ifctx->replylen, TAG_DHCP_SERVERID); 462 if (p != NULL && gctx->tmptag.taglen == 4) { 463 memcpy(&ifctx->dhcpserver, p, 4); 464 ifctx->gotdhcpserver = 1; 465 } else 466 ifctx->gotdhcpserver = 0; 467 return 1; 468 } 469 470 ifctx->gotrootpath = (bootpc_tag(&gctx->tmptag, &ifctx->reply, 471 ifctx->replylen, 472 TAG_ROOT) != NULL); 473 ifctx->gotgw = (bootpc_tag(&gctx->tmptag, &ifctx->reply, 474 ifctx->replylen, 475 TAG_ROUTERS) != NULL); 476 ifctx->gotnetmask = (bootpc_tag(&gctx->tmptag, &ifctx->reply, 477 ifctx->replylen, 478 TAG_SUBNETMASK) != NULL); 479 return 1; 480 } 481 482 static int 483 bootpc_call(struct bootpc_globalcontext *gctx, struct thread *td) 484 { 485 struct sockaddr_in *sin, dst; 486 struct uio auio; 487 struct sockopt sopt; 488 struct iovec aio; 489 int error, on, rcvflg, timo, len; 490 time_t atimo; 491 time_t rtimo; 492 struct timeval tv; 493 struct bootpc_ifcontext *ifctx; 494 int outstanding; 495 int gotrootpath; 496 int retry; 497 const char *s; 498 499 tv.tv_sec = 1; 500 tv.tv_usec = 0; 501 bzero(&sopt, sizeof(sopt)); 502 sopt.sopt_dir = SOPT_SET; 503 sopt.sopt_level = SOL_SOCKET; 504 sopt.sopt_name = SO_RCVTIMEO; 505 sopt.sopt_val = &tv; 506 sopt.sopt_valsize = sizeof tv; 507 508 error = sosetopt(bootp_so, &sopt); 509 if (error != 0) 510 goto out; 511 512 /* 513 * Enable broadcast. 514 */ 515 on = 1; 516 sopt.sopt_name = SO_BROADCAST; 517 sopt.sopt_val = &on; 518 sopt.sopt_valsize = sizeof on; 519 520 error = sosetopt(bootp_so, &sopt); 521 if (error != 0) 522 goto out; 523 524 /* 525 * Disable routing. 526 */ 527 528 on = 1; 529 sopt.sopt_name = SO_DONTROUTE; 530 sopt.sopt_val = &on; 531 sopt.sopt_valsize = sizeof on; 532 533 error = sosetopt(bootp_so, &sopt); 534 if (error != 0) 535 goto out; 536 537 /* 538 * Bind the local endpoint to a bootp client port. 539 */ 540 sin = &dst; 541 clear_sinaddr(sin); 542 sin->sin_port = htons(IPPORT_BOOTPC); 543 error = sobind(bootp_so, (struct sockaddr *)sin, td); 544 if (error != 0) { 545 printf("bind failed\n"); 546 goto out; 547 } 548 549 /* 550 * Setup socket address for the server. 551 */ 552 sin = &dst; 553 clear_sinaddr(sin); 554 sin->sin_addr.s_addr = INADDR_BROADCAST; 555 sin->sin_port = htons(IPPORT_BOOTPS); 556 557 /* 558 * Send it, repeatedly, until a reply is received, 559 * but delay each re-send by an increasing amount. 560 * If the delay hits the maximum, start complaining. 561 */ 562 timo = 0; 563 rtimo = 0; 564 for (;;) { 565 outstanding = 0; 566 gotrootpath = 0; 567 568 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) { 569 if (bootpc_ifctx_isresolved(ifctx) != 0 && 570 bootpc_tag(&gctx->tmptag, &ifctx->reply, 571 ifctx->replylen, 572 TAG_ROOT) != NULL) 573 gotrootpath = 1; 574 } 575 576 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) { 577 struct in_aliasreq *ifra = &ifctx->iareq; 578 sin = (struct sockaddr_in *)&ifra->ifra_mask; 579 580 ifctx->outstanding = 0; 581 if (bootpc_ifctx_isresolved(ifctx) != 0 && 582 gotrootpath != 0) { 583 continue; 584 } 585 if (bootpc_ifctx_isfailed(ifctx) != 0) 586 continue; 587 588 outstanding++; 589 ifctx->outstanding = 1; 590 591 /* Proceed to next step in DHCP negotiation */ 592 if ((ifctx->state == IF_DHCP_OFFERED && 593 ifctx->dhcpquerytype != DHCP_REQUEST) || 594 (ifctx->state == IF_DHCP_UNRESOLVED && 595 ifctx->dhcpquerytype != DHCP_DISCOVER) || 596 (ifctx->state == IF_BOOTP_UNRESOLVED && 597 ifctx->dhcpquerytype != DHCP_NOMSG)) { 598 ifctx->sentmsg = 0; 599 bootpc_compose_query(ifctx, td); 600 } 601 602 /* Send BOOTP request (or re-send). */ 603 604 if (ifctx->sentmsg == 0) { 605 switch(ifctx->dhcpquerytype) { 606 case DHCP_DISCOVER: 607 s = "DHCP Discover"; 608 break; 609 case DHCP_REQUEST: 610 s = "DHCP Request"; 611 break; 612 case DHCP_NOMSG: 613 default: 614 s = "BOOTP Query"; 615 break; 616 } 617 printf("Sending %s packet from " 618 "interface %s (%*D)\n", 619 s, 620 ifctx->ireq.ifr_name, 621 ifctx->sdl->sdl_alen, 622 (unsigned char *) LLADDR(ifctx->sdl), 623 ":"); 624 ifctx->sentmsg = 1; 625 } 626 627 aio.iov_base = (caddr_t) &ifctx->call; 628 aio.iov_len = sizeof(ifctx->call); 629 630 auio.uio_iov = &aio; 631 auio.uio_iovcnt = 1; 632 auio.uio_segflg = UIO_SYSSPACE; 633 auio.uio_rw = UIO_WRITE; 634 auio.uio_offset = 0; 635 auio.uio_resid = sizeof(ifctx->call); 636 auio.uio_td = td; 637 638 /* Set netmask to 0.0.0.0 */ 639 clear_sinaddr(sin); 640 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, 641 td); 642 if (error != 0) 643 panic("%s: SIOCAIFADDR, error=%d", __func__, 644 error); 645 646 error = sosend(bootp_so, (struct sockaddr *) &dst, 647 &auio, NULL, NULL, 0, td); 648 if (error != 0) 649 printf("%s: sosend: %d state %08x\n", __func__, 650 error, (int )bootp_so->so_state); 651 652 /* Set netmask to 255.0.0.0 */ 653 sin->sin_addr.s_addr = htonl(0xff000000); 654 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, 655 td); 656 if (error != 0) 657 panic("%s: SIOCAIFADDR, error=%d", __func__, 658 error); 659 } 660 661 if (outstanding == 0 && 662 (rtimo == 0 || time_second >= rtimo)) { 663 error = 0; 664 goto out; 665 } 666 667 /* Determine new timeout. */ 668 if (timo < MAX_RESEND_DELAY) 669 timo++; 670 else { 671 printf("DHCP/BOOTP timeout for server "); 672 print_sin_addr(&dst); 673 printf("\n"); 674 } 675 676 /* 677 * Wait for up to timo seconds for a reply. 678 * The socket receive timeout was set to 1 second. 679 */ 680 atimo = timo + time_second; 681 while (time_second < atimo) { 682 aio.iov_base = (caddr_t) &gctx->reply; 683 aio.iov_len = sizeof(gctx->reply); 684 685 auio.uio_iov = &aio; 686 auio.uio_iovcnt = 1; 687 auio.uio_segflg = UIO_SYSSPACE; 688 auio.uio_rw = UIO_READ; 689 auio.uio_offset = 0; 690 auio.uio_resid = sizeof(gctx->reply); 691 auio.uio_td = td; 692 693 rcvflg = 0; 694 error = soreceive(bootp_so, NULL, &auio, 695 NULL, NULL, &rcvflg); 696 gctx->secs = time_second - gctx->starttime; 697 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) { 698 if (bootpc_ifctx_isresolved(ifctx) != 0 || 699 bootpc_ifctx_isfailed(ifctx) != 0) 700 continue; 701 702 ifctx->call.secs = htons(gctx->secs); 703 } 704 if (error == EWOULDBLOCK) 705 continue; 706 if (error != 0) 707 goto out; 708 len = sizeof(gctx->reply) - auio.uio_resid; 709 710 /* Do we have the required number of bytes ? */ 711 if (len < BOOTP_MIN_LEN) 712 continue; 713 gctx->replylen = len; 714 715 /* Is it a reply? */ 716 if (gctx->reply.op != BOOTP_REPLY) 717 continue; 718 719 /* Is this an answer to our query */ 720 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) { 721 if (gctx->reply.xid != ifctx->call.xid) 722 continue; 723 724 /* Same HW address size ? */ 725 if (gctx->reply.hlen != ifctx->call.hlen) 726 continue; 727 728 /* Correct HW address ? */ 729 if (bcmp(gctx->reply.chaddr, 730 ifctx->call.chaddr, 731 ifctx->call.hlen) != 0) 732 continue; 733 734 break; 735 } 736 737 if (ifctx != NULL) { 738 s = bootpc_tag(&gctx->tmptag, 739 &gctx->reply, 740 gctx->replylen, 741 TAG_DHCP_MSGTYPE); 742 if (s != NULL) { 743 switch (*s) { 744 case DHCP_OFFER: 745 s = "DHCP Offer"; 746 break; 747 case DHCP_ACK: 748 s = "DHCP Ack"; 749 break; 750 default: 751 s = "DHCP (unexpected)"; 752 break; 753 } 754 } else 755 s = "BOOTP Reply"; 756 757 printf("Received %s packet" 758 " on %s from ", 759 s, 760 ifctx->ireq.ifr_name); 761 print_in_addr(gctx->reply.siaddr); 762 if (gctx->reply.giaddr.s_addr != 763 htonl(INADDR_ANY)) { 764 printf(" via "); 765 print_in_addr(gctx->reply.giaddr); 766 } 767 if (bootpc_received(gctx, ifctx) != 0) { 768 printf(" (accepted)"); 769 if (ifctx->outstanding) { 770 ifctx->outstanding = 0; 771 outstanding--; 772 } 773 /* Network settle delay */ 774 if (outstanding == 0) 775 atimo = time_second + 776 BOOTP_SETTLE_DELAY; 777 } else 778 printf(" (ignored)"); 779 if (ifctx->gotrootpath || 780 gctx->any_root_overrides) { 781 gotrootpath = 1; 782 rtimo = time_second + 783 BOOTP_SETTLE_DELAY; 784 if (ifctx->gotrootpath) 785 printf(" (got root path)"); 786 } 787 printf("\n"); 788 } 789 } /* while secs */ 790 #ifdef BOOTP_TIMEOUT 791 if (gctx->secs > BOOTP_TIMEOUT && BOOTP_TIMEOUT > 0) 792 break; 793 #endif 794 /* Force a retry if halfway in DHCP negotiation */ 795 retry = 0; 796 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) 797 if (ifctx->state == IF_DHCP_OFFERED) { 798 if (ifctx->dhcpquerytype == DHCP_DISCOVER) 799 retry = 1; 800 else 801 ifctx->state = IF_DHCP_UNRESOLVED; 802 } 803 804 if (retry != 0) 805 continue; 806 807 if (gotrootpath != 0) { 808 gctx->gotrootpath = gotrootpath; 809 if (rtimo != 0 && time_second >= rtimo) 810 break; 811 } 812 } /* forever send/receive */ 813 814 /* 815 * XXX: These are errors of varying seriousness being silently 816 * ignored 817 */ 818 819 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) 820 if (bootpc_ifctx_isresolved(ifctx) == 0) { 821 printf("%s timeout for interface %s\n", 822 ifctx->dhcpquerytype != DHCP_NOMSG ? 823 "DHCP" : "BOOTP", 824 ifctx->ireq.ifr_name); 825 } 826 827 if (gctx->gotrootpath != 0) { 828 #if 0 829 printf("Got a root path, ignoring remaining timeout\n"); 830 #endif 831 error = 0; 832 goto out; 833 } 834 #ifndef BOOTP_NFSROOT 835 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) 836 if (bootpc_ifctx_isresolved(ifctx) != 0) { 837 error = 0; 838 goto out; 839 } 840 #endif 841 error = ETIMEDOUT; 842 843 out: 844 return (error); 845 } 846 847 static void 848 bootpc_fakeup_interface(struct bootpc_ifcontext *ifctx, struct thread *td) 849 { 850 struct ifreq *ifr; 851 struct in_aliasreq *ifra; 852 struct sockaddr_in *sin; 853 int error; 854 855 ifr = &ifctx->ireq; 856 ifra = &ifctx->iareq; 857 858 /* 859 * Bring up the interface. 860 * 861 * Get the old interface flags and or IFF_UP into them; if 862 * IFF_UP set blindly, interface selection can be clobbered. 863 */ 864 error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td); 865 if (error != 0) 866 panic("%s: SIOCGIFFLAGS, error=%d", __func__, error); 867 ifr->ifr_flags |= IFF_UP; 868 error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td); 869 if (error != 0) 870 panic("%s: SIOCSIFFLAGS, error=%d", __func__, error); 871 872 /* 873 * Do enough of ifconfig(8) so that the chosen interface 874 * can talk to the servers. Set address to 0.0.0.0/8 and 875 * broadcast address to local broadcast. 876 */ 877 sin = (struct sockaddr_in *)&ifra->ifra_addr; 878 clear_sinaddr(sin); 879 sin = (struct sockaddr_in *)&ifra->ifra_mask; 880 clear_sinaddr(sin); 881 sin->sin_addr.s_addr = htonl(0xff000000); 882 sin = (struct sockaddr_in *)&ifra->ifra_broadaddr; 883 clear_sinaddr(sin); 884 sin->sin_addr.s_addr = htonl(INADDR_BROADCAST); 885 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td); 886 if (error != 0) 887 panic("%s: SIOCAIFADDR, error=%d", __func__, error); 888 } 889 890 static void 891 bootpc_shutdown_interface(struct bootpc_ifcontext *ifctx, struct thread *td) 892 { 893 struct ifreq *ifr; 894 struct sockaddr_in *sin; 895 int error; 896 897 ifr = &ifctx->ireq; 898 899 printf("Shutdown interface %s\n", ifctx->ireq.ifr_name); 900 error = ifioctl(bootp_so, SIOCGIFFLAGS, (caddr_t)ifr, td); 901 if (error != 0) 902 panic("%s: SIOCGIFFLAGS, error=%d", __func__, error); 903 ifr->ifr_flags &= ~IFF_UP; 904 error = ifioctl(bootp_so, SIOCSIFFLAGS, (caddr_t)ifr, td); 905 if (error != 0) 906 panic("%s: SIOCSIFFLAGS, error=%d", __func__, error); 907 908 sin = (struct sockaddr_in *) &ifr->ifr_addr; 909 clear_sinaddr(sin); 910 error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td); 911 if (error != 0) 912 panic("%s: SIOCDIFADDR, error=%d", __func__, error); 913 } 914 915 static void 916 bootpc_adjust_interface(struct bootpc_ifcontext *ifctx, 917 struct bootpc_globalcontext *gctx, struct thread *td) 918 { 919 int error; 920 struct sockaddr_in *sin; 921 struct ifreq *ifr; 922 struct in_aliasreq *ifra; 923 struct sockaddr_in *myaddr; 924 struct sockaddr_in *netmask; 925 926 ifr = &ifctx->ireq; 927 ifra = &ifctx->iareq; 928 myaddr = &ifctx->myaddr; 929 netmask = &ifctx->netmask; 930 931 if (bootpc_ifctx_isresolved(ifctx) == 0) { 932 /* Shutdown interfaces where BOOTP failed */ 933 bootpc_shutdown_interface(ifctx, td); 934 return; 935 } 936 937 printf("Adjusted interface %s", ifctx->ireq.ifr_name); 938 939 /* Do BOOTP interface options */ 940 if (ifctx->mtu != 0) { 941 printf(" (MTU=%d%s)", ifctx->mtu, 942 (ifctx->mtu > 1514) ? "/JUMBO" : ""); 943 ifr->ifr_mtu = ifctx->mtu; 944 error = ifioctl(bootp_so, SIOCSIFMTU, (caddr_t) ifr, td); 945 if (error != 0) 946 panic("%s: SIOCSIFMTU, error=%d", __func__, error); 947 } 948 printf("\n"); 949 950 /* 951 * Do enough of ifconfig(8) so that the chosen interface 952 * can talk to the servers. (just set the address) 953 */ 954 sin = (struct sockaddr_in *) &ifr->ifr_addr; 955 clear_sinaddr(sin); 956 error = ifioctl(bootp_so, SIOCDIFADDR, (caddr_t) ifr, td); 957 if (error != 0) 958 panic("%s: SIOCDIFADDR, error=%d", __func__, error); 959 960 bcopy(myaddr, &ifra->ifra_addr, sizeof(*myaddr)); 961 bcopy(netmask, &ifra->ifra_mask, sizeof(*netmask)); 962 clear_sinaddr(&ifra->ifra_broadaddr); 963 ifra->ifra_broadaddr.sin_addr.s_addr = myaddr->sin_addr.s_addr | 964 ~netmask->sin_addr.s_addr; 965 966 error = ifioctl(bootp_so, SIOCAIFADDR, (caddr_t)ifra, td); 967 if (error != 0) 968 panic("%s: SIOCAIFADDR, error=%d", __func__, error); 969 } 970 971 static void 972 bootpc_add_default_route(struct bootpc_ifcontext *ifctx) 973 { 974 int error; 975 struct sockaddr_in defdst; 976 struct sockaddr_in defmask; 977 struct rt_addrinfo info; 978 struct rib_cmd_info rc; 979 980 if (ifctx->gw.sin_addr.s_addr == htonl(INADDR_ANY)) 981 return; 982 983 clear_sinaddr(&defdst); 984 clear_sinaddr(&defmask); 985 986 bzero((caddr_t)&info, sizeof(info)); 987 info.rti_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC; 988 info.rti_info[RTAX_DST] = (struct sockaddr *)&defdst; 989 info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&defmask; 990 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ifctx->gw; 991 992 error = rib_action(RT_DEFAULT_FIB, RTM_ADD, &info, &rc); 993 994 if (error != 0) { 995 printf("%s: RTM_ADD, error=%d\n", __func__, error); 996 } 997 } 998 999 static void 1000 bootpc_remove_default_route(struct bootpc_ifcontext *ifctx) 1001 { 1002 int error; 1003 struct sockaddr_in defdst; 1004 struct sockaddr_in defmask; 1005 struct rt_addrinfo info; 1006 struct rib_cmd_info rc; 1007 1008 if (ifctx->gw.sin_addr.s_addr == htonl(INADDR_ANY)) 1009 return; 1010 1011 clear_sinaddr(&defdst); 1012 clear_sinaddr(&defmask); 1013 1014 bzero((caddr_t)&info, sizeof(info)); 1015 info.rti_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC; 1016 info.rti_info[RTAX_DST] = (struct sockaddr *)&defdst; 1017 info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&defmask; 1018 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ifctx->gw; 1019 1020 error = rib_action(RT_DEFAULT_FIB, RTM_DELETE, &info, &rc); 1021 if (error != 0) { 1022 printf("%s: RTM_DELETE, error=%d\n", __func__, error); 1023 } 1024 } 1025 1026 static int 1027 setfs(struct sockaddr_in *addr, char *path, char *p, 1028 const struct in_addr *siaddr) 1029 { 1030 1031 if (getip(&p, &addr->sin_addr) == 0) { 1032 if (siaddr != NULL && *p == '/') 1033 bcopy(siaddr, &addr->sin_addr, sizeof(struct in_addr)); 1034 else 1035 return 0; 1036 } else { 1037 if (*p != ':') 1038 return 0; 1039 p++; 1040 } 1041 1042 addr->sin_len = sizeof(struct sockaddr_in); 1043 addr->sin_family = AF_INET; 1044 1045 strlcpy(path, p, MNAMELEN); 1046 return 1; 1047 } 1048 1049 static int 1050 getip(char **ptr, struct in_addr *addr) 1051 { 1052 char *p; 1053 unsigned int ip; 1054 int val; 1055 1056 p = *ptr; 1057 ip = 0; 1058 if (((val = getdec(&p)) < 0) || (val > 255)) 1059 return 0; 1060 ip = val << 24; 1061 if (*p != '.') 1062 return 0; 1063 p++; 1064 if (((val = getdec(&p)) < 0) || (val > 255)) 1065 return 0; 1066 ip |= (val << 16); 1067 if (*p != '.') 1068 return 0; 1069 p++; 1070 if (((val = getdec(&p)) < 0) || (val > 255)) 1071 return 0; 1072 ip |= (val << 8); 1073 if (*p != '.') 1074 return 0; 1075 p++; 1076 if (((val = getdec(&p)) < 0) || (val > 255)) 1077 return 0; 1078 ip |= val; 1079 1080 addr->s_addr = htonl(ip); 1081 *ptr = p; 1082 return 1; 1083 } 1084 1085 static int 1086 getdec(char **ptr) 1087 { 1088 char *p; 1089 int ret; 1090 1091 p = *ptr; 1092 ret = 0; 1093 if ((*p < '0') || (*p > '9')) 1094 return -1; 1095 while ((*p >= '0') && (*p <= '9')) { 1096 ret = ret * 10 + (*p - '0'); 1097 p++; 1098 } 1099 *ptr = p; 1100 return ret; 1101 } 1102 1103 static void 1104 mountopts(struct nfs_args *args, char *p) 1105 { 1106 args->version = NFS_ARGSVERSION; 1107 args->rsize = BOOTP_BLOCKSIZE; 1108 args->wsize = BOOTP_BLOCKSIZE; 1109 args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT; 1110 args->sotype = SOCK_DGRAM; 1111 if (p != NULL) 1112 nfs_parse_options(p, args); 1113 } 1114 1115 static int 1116 xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len) 1117 { 1118 struct mbuf *m; 1119 int alignedlen; 1120 1121 m = *mptr; 1122 alignedlen = ( len + 3 ) & ~3; 1123 1124 if (m->m_len < alignedlen) { 1125 m = m_pullup(m, alignedlen); 1126 if (m == NULL) { 1127 *mptr = NULL; 1128 return EBADRPC; 1129 } 1130 } 1131 bcopy(mtod(m, u_char *), buf, len); 1132 m_adj(m, alignedlen); 1133 *mptr = m; 1134 return 0; 1135 } 1136 1137 static int 1138 xdr_int_decode(struct mbuf **mptr, int *iptr) 1139 { 1140 u_int32_t i; 1141 1142 if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0) 1143 return EBADRPC; 1144 *iptr = fxdr_unsigned(u_int32_t, i); 1145 return 0; 1146 } 1147 1148 static void 1149 print_sin_addr(struct sockaddr_in *sin) 1150 { 1151 1152 print_in_addr(sin->sin_addr); 1153 } 1154 1155 static void 1156 print_in_addr(struct in_addr addr) 1157 { 1158 unsigned int ip; 1159 1160 ip = ntohl(addr.s_addr); 1161 printf("%d.%d.%d.%d", 1162 ip >> 24, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255); 1163 } 1164 1165 static void 1166 bootpc_compose_query(struct bootpc_ifcontext *ifctx, struct thread *td) 1167 { 1168 unsigned char *vendp; 1169 unsigned char vendor_client[64]; 1170 uint32_t leasetime; 1171 uint8_t vendor_client_len; 1172 1173 ifctx->gotrootpath = 0; 1174 1175 bzero((caddr_t) &ifctx->call, sizeof(ifctx->call)); 1176 1177 /* bootpc part */ 1178 ifctx->call.op = BOOTP_REQUEST; /* BOOTREQUEST */ 1179 ifctx->call.htype = 1; /* 10mb ethernet */ 1180 ifctx->call.hlen = ifctx->sdl->sdl_alen;/* Hardware address length */ 1181 ifctx->call.hops = 0; 1182 if (bootpc_ifctx_isunresolved(ifctx) != 0) 1183 ifctx->xid++; 1184 ifctx->call.xid = txdr_unsigned(ifctx->xid); 1185 bcopy(LLADDR(ifctx->sdl), &ifctx->call.chaddr, ifctx->sdl->sdl_alen); 1186 1187 vendp = ifctx->call.vend; 1188 *vendp++ = 99; /* RFC1048 cookie */ 1189 *vendp++ = 130; 1190 *vendp++ = 83; 1191 *vendp++ = 99; 1192 *vendp++ = TAG_MAXMSGSIZE; 1193 *vendp++ = 2; 1194 *vendp++ = (sizeof(struct bootp_packet) >> 8) & 255; 1195 *vendp++ = sizeof(struct bootp_packet) & 255; 1196 1197 snprintf(vendor_client, sizeof(vendor_client), "%s:%s:%s", 1198 ostype, MACHINE, osrelease); 1199 vendor_client_len = strlen(vendor_client); 1200 *vendp++ = TAG_VENDOR_INDENTIFIER; 1201 *vendp++ = vendor_client_len; 1202 memcpy(vendp, vendor_client, vendor_client_len); 1203 vendp += vendor_client_len; 1204 ifctx->dhcpquerytype = DHCP_NOMSG; 1205 switch (ifctx->state) { 1206 case IF_DHCP_UNRESOLVED: 1207 *vendp++ = TAG_DHCP_MSGTYPE; 1208 *vendp++ = 1; 1209 *vendp++ = DHCP_DISCOVER; 1210 ifctx->dhcpquerytype = DHCP_DISCOVER; 1211 ifctx->gotdhcpserver = 0; 1212 break; 1213 case IF_DHCP_OFFERED: 1214 *vendp++ = TAG_DHCP_MSGTYPE; 1215 *vendp++ = 1; 1216 *vendp++ = DHCP_REQUEST; 1217 ifctx->dhcpquerytype = DHCP_REQUEST; 1218 *vendp++ = TAG_DHCP_REQ_ADDR; 1219 *vendp++ = 4; 1220 memcpy(vendp, &ifctx->reply.yiaddr, 4); 1221 vendp += 4; 1222 if (ifctx->gotdhcpserver != 0) { 1223 *vendp++ = TAG_DHCP_SERVERID; 1224 *vendp++ = 4; 1225 memcpy(vendp, &ifctx->dhcpserver, 4); 1226 vendp += 4; 1227 } 1228 *vendp++ = TAG_DHCP_LEASETIME; 1229 *vendp++ = 4; 1230 leasetime = htonl(300); 1231 memcpy(vendp, &leasetime, 4); 1232 vendp += 4; 1233 break; 1234 default: 1235 break; 1236 } 1237 *vendp = TAG_END; 1238 1239 ifctx->call.secs = 0; 1240 ifctx->call.flags = htons(0x8000); /* We need a broadcast answer */ 1241 } 1242 1243 static int 1244 bootpc_hascookie(struct bootp_packet *bp) 1245 { 1246 1247 return (bp->vend[0] == 99 && bp->vend[1] == 130 && 1248 bp->vend[2] == 83 && bp->vend[3] == 99); 1249 } 1250 1251 static void 1252 bootpc_tag_helper(struct bootpc_tagcontext *tctx, 1253 unsigned char *start, int len, int tag) 1254 { 1255 unsigned char *j; 1256 unsigned char *ej; 1257 unsigned char code; 1258 1259 if (tctx->badtag != 0 || tctx->badopt != 0) 1260 return; 1261 1262 j = start; 1263 ej = j + len; 1264 1265 while (j < ej) { 1266 code = *j++; 1267 if (code == TAG_PAD) 1268 continue; 1269 if (code == TAG_END) 1270 return; 1271 if (j >= ej || j + *j + 1 > ej) { 1272 tctx->badopt = 1; 1273 return; 1274 } 1275 len = *j++; 1276 if (code == tag) { 1277 if (tctx->taglen + len > TAG_MAXLEN) { 1278 tctx->badtag = 1; 1279 return; 1280 } 1281 tctx->foundopt = 1; 1282 if (len > 0) 1283 memcpy(tctx->buf + tctx->taglen, 1284 j, len); 1285 tctx->taglen += len; 1286 } 1287 if (code == TAG_OVERLOAD) 1288 tctx->overload = *j; 1289 1290 j += len; 1291 } 1292 } 1293 1294 static unsigned char * 1295 bootpc_tag(struct bootpc_tagcontext *tctx, 1296 struct bootp_packet *bp, int len, int tag) 1297 { 1298 tctx->overload = 0; 1299 tctx->badopt = 0; 1300 tctx->badtag = 0; 1301 tctx->foundopt = 0; 1302 tctx->taglen = 0; 1303 1304 if (bootpc_hascookie(bp) == 0) 1305 return NULL; 1306 1307 bootpc_tag_helper(tctx, &bp->vend[4], 1308 (unsigned char *) bp + len - &bp->vend[4], tag); 1309 1310 if ((tctx->overload & OVERLOAD_FILE) != 0) 1311 bootpc_tag_helper(tctx, 1312 (unsigned char *) bp->file, 1313 sizeof(bp->file), 1314 tag); 1315 if ((tctx->overload & OVERLOAD_SNAME) != 0) 1316 bootpc_tag_helper(tctx, 1317 (unsigned char *) bp->sname, 1318 sizeof(bp->sname), 1319 tag); 1320 1321 if (tctx->badopt != 0 || tctx->badtag != 0 || tctx->foundopt == 0) 1322 return NULL; 1323 tctx->buf[tctx->taglen] = '\0'; 1324 return tctx->buf; 1325 } 1326 1327 static void 1328 bootpc_decode_reply(struct nfsv3_diskless *nd, struct bootpc_ifcontext *ifctx, 1329 struct bootpc_globalcontext *gctx) 1330 { 1331 char *p, *s; 1332 1333 ifctx->gotgw = 0; 1334 ifctx->gotnetmask = 0; 1335 1336 clear_sinaddr(&ifctx->myaddr); 1337 clear_sinaddr(&ifctx->netmask); 1338 clear_sinaddr(&ifctx->gw); 1339 1340 ifctx->myaddr.sin_addr = ifctx->reply.yiaddr; 1341 1342 printf("%s at ", ifctx->ireq.ifr_name); 1343 print_sin_addr(&ifctx->myaddr); 1344 printf(" server "); 1345 print_in_addr(ifctx->reply.siaddr); 1346 1347 ifctx->gw.sin_addr = ifctx->reply.giaddr; 1348 if (ifctx->reply.giaddr.s_addr != htonl(INADDR_ANY)) { 1349 printf(" via gateway "); 1350 print_in_addr(ifctx->reply.giaddr); 1351 } 1352 1353 /* This call used for the side effect (overload flag) */ 1354 (void) bootpc_tag(&gctx->tmptag, 1355 &ifctx->reply, ifctx->replylen, TAG_END); 1356 1357 if ((gctx->tmptag.overload & OVERLOAD_SNAME) == 0) 1358 if (ifctx->reply.sname[0] != '\0') 1359 printf(" server name %s", ifctx->reply.sname); 1360 if ((gctx->tmptag.overload & OVERLOAD_FILE) == 0) 1361 if (ifctx->reply.file[0] != '\0') 1362 printf(" boot file %s", ifctx->reply.file); 1363 1364 printf("\n"); 1365 1366 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen, 1367 TAG_SUBNETMASK); 1368 if (p != NULL) { 1369 if (gctx->tag.taglen != 4) 1370 panic("bootpc: subnet mask len is %d", 1371 gctx->tag.taglen); 1372 bcopy(p, &ifctx->netmask.sin_addr, 4); 1373 ifctx->gotnetmask = 1; 1374 printf("subnet mask "); 1375 print_sin_addr(&ifctx->netmask); 1376 printf(" "); 1377 } 1378 1379 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen, 1380 TAG_ROUTERS); 1381 if (p != NULL) { 1382 /* Routers */ 1383 if (gctx->tag.taglen % 4) 1384 panic("bootpc: Router Len is %d", gctx->tag.taglen); 1385 if (gctx->tag.taglen > 0) { 1386 bcopy(p, &ifctx->gw.sin_addr, 4); 1387 printf("router "); 1388 print_sin_addr(&ifctx->gw); 1389 printf(" "); 1390 ifctx->gotgw = 1; 1391 gctx->gotgw = 1; 1392 } 1393 } 1394 1395 /* 1396 * Choose a root filesystem. If a value is forced in the environment 1397 * and it contains "nfs:", use it unconditionally. Otherwise, if the 1398 * kernel is compiled with the ROOTDEVNAME option, then use it if: 1399 * - The server doesn't provide a pathname. 1400 * - The boothowto flags include RB_DFLTROOT (user said to override 1401 * the server value). 1402 */ 1403 p = NULL; 1404 if ((s = kern_getenv("vfs.root.mountfrom")) != NULL) { 1405 if ((p = strstr(s, "nfs:")) != NULL) 1406 p = strdup(p + 4, M_TEMP); 1407 freeenv(s); 1408 } 1409 if (p == NULL) { 1410 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen, 1411 TAG_ROOT); 1412 if (p != NULL) 1413 ifctx->gotrootpath = 1; 1414 } 1415 #ifdef ROOTDEVNAME 1416 if ((p == NULL || (boothowto & RB_DFLTROOT) != 0) && 1417 (p = strstr(ROOTDEVNAME, "nfs:")) != NULL) { 1418 p += 4; 1419 } 1420 #endif 1421 if (p != NULL) { 1422 if (gctx->setrootfs != NULL) { 1423 printf("rootfs %s (ignored) ", p); 1424 } else if (setfs(&nd->root_saddr, 1425 nd->root_hostnam, p, &ifctx->reply.siaddr)) { 1426 if (*p == '/') { 1427 printf("root_server "); 1428 print_sin_addr(&nd->root_saddr); 1429 printf(" "); 1430 } 1431 printf("rootfs %s ", p); 1432 gctx->gotrootpath = 1; 1433 gctx->setrootfs = ifctx; 1434 1435 p = bootpc_tag(&gctx->tag, &ifctx->reply, 1436 ifctx->replylen, 1437 TAG_ROOTOPTS); 1438 if (p != NULL) { 1439 mountopts(&nd->root_args, p); 1440 printf("rootopts %s ", p); 1441 } 1442 } else 1443 panic("Failed to set rootfs to %s", p); 1444 } 1445 1446 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen, 1447 TAG_HOSTNAME); 1448 if (p != NULL) { 1449 if (gctx->tag.taglen >= MAXHOSTNAMELEN) 1450 panic("bootpc: hostname >= %d bytes", 1451 MAXHOSTNAMELEN); 1452 if (gctx->sethostname != NULL) { 1453 printf("hostname %s (ignored) ", p); 1454 } else { 1455 strcpy(nd->my_hostnam, p); 1456 mtx_lock(&prison0.pr_mtx); 1457 strcpy(prison0.pr_hostname, p); 1458 mtx_unlock(&prison0.pr_mtx); 1459 printf("hostname %s ", p); 1460 gctx->sethostname = ifctx; 1461 } 1462 } 1463 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen, 1464 TAG_COOKIE); 1465 if (p != NULL) { /* store in a sysctl variable */ 1466 int i, l = sizeof(bootp_cookie) - 1; 1467 for (i = 0; i < l && p[i] != '\0'; i++) 1468 bootp_cookie[i] = p[i]; 1469 p[i] = '\0'; 1470 } 1471 1472 p = bootpc_tag(&gctx->tag, &ifctx->reply, ifctx->replylen, 1473 TAG_INTF_MTU); 1474 if (p != NULL) { 1475 ifctx->mtu = be16dec(p); 1476 } 1477 1478 printf("\n"); 1479 1480 if (ifctx->gotnetmask == 0) { 1481 /* 1482 * If there is no netmask, use historical default, 1483 * but we really need the right mask from the server. 1484 */ 1485 printf("%s: no netmask received!\n", ifctx->ireq.ifr_name); 1486 if (IN_CLASSA(ntohl(ifctx->myaddr.sin_addr.s_addr))) 1487 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSA_NET); 1488 else if (IN_CLASSB(ntohl(ifctx->myaddr.sin_addr.s_addr))) 1489 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSB_NET); 1490 else 1491 ifctx->netmask.sin_addr.s_addr = htonl(IN_CLASSC_NET); 1492 } 1493 } 1494 1495 static u_int 1496 bootpc_init_ifa_cb(void *arg, struct ifaddr *ifa, u_int count) 1497 { 1498 struct sockaddr_dl *sdl = (struct sockaddr_dl *)ifa->ifa_addr; 1499 1500 if (count != 0) 1501 return (0); 1502 1503 if (sdl->sdl_type != IFT_ETHER) 1504 return (0); 1505 1506 *(struct sockaddr_dl **)arg = sdl; 1507 1508 return (1); 1509 } 1510 1511 void 1512 bootpc_init(void) 1513 { 1514 struct epoch_tracker et; 1515 struct bootpc_ifcontext *ifctx = NULL; /* Interface BOOTP contexts */ 1516 struct bootpc_globalcontext *gctx; /* Global BOOTP context */ 1517 struct sockaddr_dl *sdl; 1518 struct if_iter iter; 1519 if_t ifp; 1520 int error; 1521 #ifndef BOOTP_WIRED_TO 1522 int ifcnt; 1523 #endif 1524 struct nfsv3_diskless *nd; 1525 struct thread *td; 1526 int timeout; 1527 int delay; 1528 char *s; 1529 1530 timeout = BOOTP_IFACE_WAIT_TIMEOUT * hz; 1531 delay = hz / 10; 1532 1533 nd = &nfsv3_diskless; 1534 td = curthread; 1535 1536 /* 1537 * If already filled in, don't touch it here 1538 */ 1539 if (nfs_diskless_valid != 0) 1540 return; 1541 1542 /* 1543 * If "vfs.root.mountfrom" is set and the value is something other 1544 * than "nfs:", it means the user doesn't want to mount root via nfs, 1545 * there's no reason to continue with bootpc 1546 */ 1547 if ((s = kern_getenv("vfs.root.mountfrom")) != NULL) { 1548 if ((strncmp(s, "nfs:", 4)) != 0) { 1549 printf("%s: vfs.root.mountfrom set to %s. " 1550 "BOOTP aborted.\n", __func__, s); 1551 freeenv(s); 1552 return; 1553 } 1554 freeenv(s); 1555 } 1556 1557 gctx = malloc(sizeof(*gctx), M_TEMP, M_WAITOK | M_ZERO); 1558 STAILQ_INIT(&gctx->interfaces); 1559 gctx->xid = ~0xFFFF; 1560 gctx->starttime = time_second; 1561 1562 /* 1563 * If ROOTDEVNAME is defined or vfs.root.mountfrom is set then we have 1564 * root-path overrides that can potentially let us boot even if we don't 1565 * get a root path from the server, so we can treat that as a non-error. 1566 */ 1567 #ifdef ROOTDEVNAME 1568 gctx->any_root_overrides = 1; 1569 #else 1570 gctx->any_root_overrides = testenv("vfs.root.mountfrom"); 1571 #endif 1572 1573 /* 1574 * Find a network interface. 1575 */ 1576 CURVNET_SET(TD_TO_VNET(td)); 1577 #ifdef BOOTP_WIRED_TO 1578 printf("%s: wired to interface '%s'\n", __func__, 1579 __XSTRING(BOOTP_WIRED_TO)); 1580 allocifctx(gctx); 1581 #else 1582 /* 1583 * Preallocate interface context storage, if another interface 1584 * attaches and wins the race, it won't be eligible for bootp. 1585 */ 1586 ifcnt = 0; 1587 NET_EPOCH_ENTER(et); 1588 for (if_t ifp = if_iter_start(&iter); ifp != NULL; ifp = if_iter_next(&iter)) { 1589 if ((if_getflags(ifp) & 1590 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) == 1591 IFF_BROADCAST) 1592 ifcnt++; 1593 } 1594 if_iter_finish(&iter); 1595 NET_EPOCH_EXIT(et); 1596 if (ifcnt == 0) { 1597 printf("WARNING: BOOTP found no eligible network interfaces, skipping!\n"); 1598 goto out; 1599 } 1600 1601 for (; ifcnt > 0; ifcnt--) 1602 allocifctx(gctx); 1603 #endif 1604 1605 retry: 1606 ifctx = STAILQ_FIRST(&gctx->interfaces); 1607 NET_EPOCH_ENTER(et); 1608 for (ifp = if_iter_start(&iter); ifp != NULL; ifp = if_iter_next(&iter)) { 1609 if (ifctx == NULL) 1610 break; 1611 #ifdef BOOTP_WIRED_TO 1612 if (strcmp(if_name(ifp), __XSTRING(BOOTP_WIRED_TO)) != 0) 1613 continue; 1614 #else 1615 if ((if_getflags(ifp) & 1616 (IFF_LOOPBACK | IFF_POINTOPOINT | IFF_BROADCAST)) != 1617 IFF_BROADCAST) 1618 break; 1619 switch (if_getalloctype(ifp)) { 1620 case IFT_ETHER: 1621 break; 1622 default: 1623 continue; 1624 } 1625 #endif 1626 strlcpy(ifctx->ireq.ifr_name, if_name(ifp), 1627 sizeof(ifctx->ireq.ifr_name)); 1628 ifctx->ifp = ifp; 1629 1630 /* Get HW address */ 1631 sdl = NULL; 1632 if_foreach_addr_type(ifp, AF_LINK, bootpc_init_ifa_cb, &sdl); 1633 if (sdl == NULL) 1634 panic("bootpc: Unable to find HW address for %s", 1635 ifctx->ireq.ifr_name); 1636 ifctx->sdl = sdl; 1637 1638 ifctx = STAILQ_NEXT(ifctx, next); 1639 } 1640 if_iter_finish(&iter); 1641 NET_EPOCH_EXIT(et); 1642 CURVNET_RESTORE(); 1643 1644 if (STAILQ_EMPTY(&gctx->interfaces) || 1645 STAILQ_FIRST(&gctx->interfaces)->ifp == NULL) { 1646 if (timeout > 0) { 1647 pause("bootpc", delay); 1648 timeout -= delay; 1649 goto retry; 1650 } 1651 #ifdef BOOTP_WIRED_TO 1652 panic("%s: Could not find interface specified " 1653 "by BOOTP_WIRED_TO: " 1654 __XSTRING(BOOTP_WIRED_TO), __func__); 1655 #else 1656 panic("%s: no suitable interface", __func__); 1657 #endif 1658 } 1659 1660 error = socreate(AF_INET, &bootp_so, SOCK_DGRAM, 0, td->td_ucred, td); 1661 if (error != 0) 1662 panic("%s: socreate, error=%d", __func__, error); 1663 1664 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) 1665 bootpc_fakeup_interface(ifctx, td); 1666 1667 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) 1668 bootpc_compose_query(ifctx, td); 1669 1670 error = bootpc_call(gctx, td); 1671 if (error != 0) { 1672 printf("BOOTP call failed\n"); 1673 } 1674 1675 mountopts(&nd->root_args, NULL); 1676 1677 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) 1678 if (bootpc_ifctx_isresolved(ifctx) != 0) 1679 bootpc_decode_reply(nd, ifctx, gctx); 1680 1681 #ifdef BOOTP_NFSROOT 1682 if (gctx->gotrootpath == 0 && gctx->any_root_overrides == 0) 1683 panic("bootpc: No root path offered"); 1684 #endif 1685 1686 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) 1687 bootpc_adjust_interface(ifctx, gctx, td); 1688 1689 soclose(bootp_so); 1690 1691 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) 1692 if (ifctx->gotrootpath != 0) 1693 break; 1694 if (ifctx == NULL) { 1695 STAILQ_FOREACH(ifctx, &gctx->interfaces, next) 1696 if (bootpc_ifctx_isresolved(ifctx) != 0) 1697 break; 1698 } 1699 if (ifctx == NULL) 1700 goto out; 1701 1702 if (gctx->gotrootpath != 0) { 1703 struct epoch_tracker et; 1704 1705 kern_setenv("boot.netif.name", if_name(ifctx->ifp)); 1706 1707 NET_EPOCH_ENTER(et); 1708 bootpc_add_default_route(ifctx); 1709 NET_EPOCH_EXIT(et); 1710 error = md_mount(&nd->root_saddr, nd->root_hostnam, 1711 nd->root_fh, &nd->root_fhsize, 1712 &nd->root_args, td); 1713 NET_EPOCH_ENTER(et); 1714 bootpc_remove_default_route(ifctx); 1715 NET_EPOCH_EXIT(et); 1716 if (error != 0) { 1717 if (gctx->any_root_overrides == 0) 1718 panic("nfs_boot: mount root, error=%d", error); 1719 else 1720 goto out; 1721 } 1722 rootdevnames[0] = "nfs:"; 1723 nfs_diskless_valid = 3; 1724 } 1725 1726 strcpy(nd->myif.ifra_name, ifctx->ireq.ifr_name); 1727 bcopy(&ifctx->myaddr, &nd->myif.ifra_addr, sizeof(ifctx->myaddr)); 1728 bcopy(&ifctx->myaddr, &nd->myif.ifra_broadaddr, sizeof(ifctx->myaddr)); 1729 ((struct sockaddr_in *) &nd->myif.ifra_broadaddr)->sin_addr.s_addr = 1730 ifctx->myaddr.sin_addr.s_addr | 1731 ~ ifctx->netmask.sin_addr.s_addr; 1732 bcopy(&ifctx->netmask, &nd->myif.ifra_mask, sizeof(ifctx->netmask)); 1733 bcopy(&ifctx->gw, &nd->mygateway, sizeof(ifctx->gw)); 1734 1735 out: 1736 while((ifctx = STAILQ_FIRST(&gctx->interfaces)) != NULL) { 1737 STAILQ_REMOVE_HEAD(&gctx->interfaces, next); 1738 free(ifctx, M_TEMP); 1739 } 1740 free(gctx, M_TEMP); 1741 } 1742 1743 /* 1744 * RPC: mountd/mount 1745 * Given a server pathname, get an NFS file handle. 1746 * Also, sets sin->sin_port to the NFS service port. 1747 */ 1748 static int 1749 md_mount(struct sockaddr_in *mdsin, char *path, u_char *fhp, int *fhsizep, 1750 struct nfs_args *args, struct thread *td) 1751 { 1752 struct mbuf *m; 1753 int error; 1754 int authunixok; 1755 int authcount; 1756 int authver; 1757 1758 #define RPCPROG_MNT 100005 1759 #define RPCMNT_VER1 1 1760 #define RPCMNT_VER3 3 1761 #define RPCMNT_MOUNT 1 1762 #define AUTH_SYS 1 /* unix style (uid, gids) */ 1763 #define AUTH_UNIX AUTH_SYS 1764 1765 /* XXX honor v2/v3 flags in args->flags? */ 1766 #ifdef BOOTP_NFSV3 1767 /* First try NFS v3 */ 1768 /* Get port number for MOUNTD. */ 1769 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3, 1770 &mdsin->sin_port, td); 1771 if (error == 0) { 1772 m = xdr_string_encode(path, strlen(path)); 1773 1774 /* Do RPC to mountd. */ 1775 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3, 1776 RPCMNT_MOUNT, &m, NULL, td); 1777 } 1778 if (error == 0) { 1779 args->flags |= NFSMNT_NFSV3; 1780 } else { 1781 #endif 1782 /* Fallback to NFS v2 */ 1783 1784 /* Get port number for MOUNTD. */ 1785 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1, 1786 &mdsin->sin_port, td); 1787 if (error != 0) 1788 return error; 1789 1790 m = xdr_string_encode(path, strlen(path)); 1791 1792 /* Do RPC to mountd. */ 1793 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1, 1794 RPCMNT_MOUNT, &m, NULL, td); 1795 if (error != 0) 1796 return error; /* message already freed */ 1797 1798 #ifdef BOOTP_NFSV3 1799 } 1800 #endif 1801 1802 if (xdr_int_decode(&m, &error) != 0 || error != 0) 1803 goto bad; 1804 1805 if ((args->flags & NFSMNT_NFSV3) != 0) { 1806 if (xdr_int_decode(&m, fhsizep) != 0 || 1807 *fhsizep > NFSX_V3FHMAX || 1808 *fhsizep <= 0) 1809 goto bad; 1810 } else 1811 *fhsizep = NFSX_V2FH; 1812 1813 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0) 1814 goto bad; 1815 1816 if (args->flags & NFSMNT_NFSV3) { 1817 if (xdr_int_decode(&m, &authcount) != 0) 1818 goto bad; 1819 authunixok = 0; 1820 if (authcount < 0 || authcount > 100) 1821 goto bad; 1822 while (authcount > 0) { 1823 if (xdr_int_decode(&m, &authver) != 0) 1824 goto bad; 1825 if (authver == AUTH_UNIX) 1826 authunixok = 1; 1827 authcount--; 1828 } 1829 if (authunixok == 0) 1830 goto bad; 1831 } 1832 1833 /* Set port number for NFS use. */ 1834 error = krpc_portmap(mdsin, NFS_PROG, 1835 (args->flags & 1836 NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2, 1837 &mdsin->sin_port, td); 1838 1839 goto out; 1840 1841 bad: 1842 error = EBADRPC; 1843 1844 out: 1845 m_freem(m); 1846 return error; 1847 } 1848 1849 SYSINIT(bootp_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, bootpc_init, NULL); 1850