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