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