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