1 /* $NetBSD: nfs_boot.c,v 1.49 1998/09/13 13:49:29 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 1995, 1997 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Adam Glass and Gordon W. Ross. 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 NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * Support for NFS diskless booting, specifically getting information 41 * about where to mount root from, what pathnames, etc. 42 */ 43 44 #include "opt_nfs_boot.h" 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/kernel.h> 49 #include <sys/conf.h> 50 #include <sys/device.h> 51 #include <sys/ioctl.h> 52 #include <sys/proc.h> 53 #include <sys/mount.h> 54 #include <sys/mbuf.h> 55 #include <sys/reboot.h> 56 #include <sys/socket.h> 57 #include <sys/socketvar.h> 58 59 #include <net/if.h> 60 #include <net/route.h> 61 #include <net/if_ether.h> 62 #include <net/if_types.h> 63 64 #include <netinet/in.h> 65 #include <netinet/if_inarp.h> 66 67 #include <nfs/rpcv2.h> 68 #include <nfs/krpc.h> 69 #include <nfs/xdr_subs.h> 70 71 #include <nfs/nfsproto.h> 72 #include <nfs/nfs.h> 73 #include <nfs/nfsmount.h> 74 #include <nfs/nfsdiskless.h> 75 76 /* 77 * There are two implementations of NFS diskless boot. 78 * One implementation uses BOOTP (RFC951, RFC1048), 79 * the other uses Sun RPC/bootparams. See the files: 80 * nfs_bootp.c: BOOTP (RFC951, RFC1048) 81 * nfs_bootsun.c: Sun RPC/bootparams 82 */ 83 #if defined(NFS_BOOT_BOOTP) || defined(NFS_BOOT_DHCP) 84 int nfs_boot_rfc951 = 1; /* BOOTP enabled (default) */ 85 #endif 86 #ifdef NFS_BOOT_BOOTPARAM 87 int nfs_boot_bootparam = 1; /* BOOTPARAM enabled (default) */ 88 #endif 89 90 /* mountd RPC */ 91 static int md_mount __P((struct sockaddr_in *mdsin, char *path, 92 struct nfs_args *argp)); 93 94 static void nfs_boot_defrt __P((struct in_addr *)); 95 static int nfs_boot_getfh __P((struct nfs_dlmount *ndm)); 96 97 98 /* 99 * Called with an empty nfs_diskless struct to be filled in. 100 * Find an interface, determine its ip address (etc.) and 101 * save all the boot parameters in the nfs_diskless struct. 102 */ 103 int 104 nfs_boot_init(nd, procp) 105 struct nfs_diskless *nd; 106 struct proc *procp; 107 { 108 struct ifnet *ifp; 109 int error; 110 111 /* 112 * Find the network interface. 113 */ 114 ifp = ifunit(root_device->dv_xname); 115 if (ifp == NULL) { 116 printf("nfs_boot: '%s' not found\n", 117 root_device->dv_xname); 118 return (ENXIO); 119 } 120 121 error = EADDRNOTAVAIL; /* ??? */ 122 #if defined(NFS_BOOT_BOOTP) || defined(NFS_BOOT_DHCP) 123 if (error && nfs_boot_rfc951) { 124 #if defined(NFS_BOOT_DHCP) 125 printf("nfs_boot: trying DHCP/BOOTP\n"); 126 #else 127 printf("nfs_boot: trying BOOTP\n"); 128 #endif 129 error = nfs_bootdhcp(ifp, nd, procp); 130 } 131 #endif 132 #ifdef NFS_BOOT_BOOTPARAM 133 if (error && nfs_boot_bootparam) { 134 printf("nfs_boot: trying RARP (and RPC/bootparam)\n"); 135 error = nfs_bootparam(ifp, nd, procp); 136 } 137 #endif 138 if (error) 139 return (error); 140 141 /* 142 * If the gateway address is set, add a default route. 143 * (The mountd RPCs may go across a gateway.) 144 */ 145 if (nd->nd_gwip.s_addr) 146 nfs_boot_defrt(&nd->nd_gwip); 147 148 /* 149 * Now fetch the NFS file handles as appropriate. 150 */ 151 error = nfs_boot_getfh(&nd->nd_root); 152 153 return (error); 154 } 155 156 int nfs_boot_setrecvtimo(so) 157 struct socket *so; 158 { 159 struct mbuf *m; 160 struct timeval *tv; 161 162 m = m_get(M_WAIT, MT_SOOPTS); 163 tv = mtod(m, struct timeval *); 164 m->m_len = sizeof(*tv); 165 tv->tv_sec = 1; 166 tv->tv_usec = 0; 167 return(sosetopt(so, SOL_SOCKET, SO_RCVTIMEO, m)); 168 } 169 170 int nfs_boot_enbroadcast(so) 171 struct socket *so; 172 { 173 struct mbuf *m; 174 int32_t *on; 175 176 m = m_get(M_WAIT, MT_SOOPTS); 177 on = mtod(m, int32_t *); 178 m->m_len = sizeof(*on); 179 *on = 1; 180 return(sosetopt(so, SOL_SOCKET, SO_BROADCAST, m)); 181 } 182 183 int nfs_boot_sobind_ipport(so, port) 184 struct socket *so; 185 u_int16_t port; 186 { 187 struct mbuf *m; 188 struct sockaddr_in *sin; 189 int error; 190 191 m = m_getclr(M_WAIT, MT_SONAME); 192 sin = mtod(m, struct sockaddr_in *); 193 sin->sin_len = m->m_len = sizeof(*sin); 194 sin->sin_family = AF_INET; 195 sin->sin_addr.s_addr = INADDR_ANY; 196 sin->sin_port = htons(port); 197 error = sobind(so, m); 198 m_freem(m); 199 return(error); 200 } 201 202 /* 203 * What is the longest we will wait before re-sending a request? 204 * Note this is also the frequency of "timeout" messages. 205 * The re-send loop counts up linearly to this maximum, so the 206 * first complaint will happen after (1+2+3+4+5)=15 seconds. 207 */ 208 #define MAX_RESEND_DELAY 5 /* seconds */ 209 #define TOTAL_TIMEOUT 30 /* seconds */ 210 211 int nfs_boot_sendrecv(so, nam, sndproc, snd, rcvproc, rcv, 212 from_p, context) 213 struct socket *so; 214 struct mbuf *nam; 215 int (*sndproc) __P((struct mbuf*, void*, int)); 216 struct mbuf *snd; 217 int (*rcvproc) __P((struct mbuf*, void*)); 218 struct mbuf **rcv, **from_p; 219 void *context; 220 { 221 int error, rcvflg, timo, secs, waited; 222 struct mbuf *m, *from; 223 struct uio uio; 224 225 /* Free at end if not null. */ 226 from = NULL; 227 228 /* 229 * Send it, repeatedly, until a reply is received, 230 * but delay each re-send by an increasing amount. 231 * If the delay hits the maximum, start complaining. 232 */ 233 waited = timo = 0; 234 send_again: 235 waited += timo; 236 if (waited >= TOTAL_TIMEOUT) 237 return(ETIMEDOUT); 238 239 /* Determine new timeout. */ 240 if (timo < MAX_RESEND_DELAY) 241 timo++; 242 else 243 printf("nfs_boot: timeout...\n"); 244 245 if (sndproc) { 246 error = (*sndproc)(snd, context, waited); 247 if (error) 248 goto out; 249 } 250 251 /* Send request (or re-send). */ 252 m = m_copypacket(snd, M_WAIT); 253 if (m == NULL) { 254 error = ENOBUFS; 255 goto out; 256 } 257 error = (*so->so_send)(so, nam, NULL, m, NULL, 0); 258 if (error) { 259 printf("nfs_boot: sosend: %d\n", error); 260 goto out; 261 } 262 m = NULL; 263 264 /* 265 * Wait for up to timo seconds for a reply. 266 * The socket receive timeout was set to 1 second. 267 */ 268 269 secs = timo; 270 for (;;) { 271 if (from) { 272 m_freem(from); 273 from = NULL; 274 } 275 if (m) { 276 m_freem(m); 277 m = NULL; 278 } 279 uio.uio_resid = 1 << 16; /* ??? */ 280 rcvflg = 0; 281 error = (*so->so_receive)(so, &from, &uio, &m, NULL, &rcvflg); 282 if (error == EWOULDBLOCK) { 283 if (--secs <= 0) 284 goto send_again; 285 continue; 286 } 287 if (error) 288 goto out; 289 #ifdef DIAGNOSTIC 290 if (!m || !(m->m_flags & M_PKTHDR) 291 || (1 << 16) - uio.uio_resid != m->m_pkthdr.len) 292 panic("nfs_boot_sendrecv: return size"); 293 #endif 294 295 if ((*rcvproc)(m, context)) 296 continue; 297 298 if (rcv) 299 *rcv = m; 300 else 301 m_freem(m); 302 if (from_p) { 303 *from_p = from; 304 from = NULL; 305 } 306 break; 307 } 308 out: 309 if (from) m_freem(from); 310 return(error); 311 } 312 313 /* 314 * Install a default route to the passed IP address. 315 */ 316 static void 317 nfs_boot_defrt(gw_ip) 318 struct in_addr *gw_ip; 319 { 320 struct sockaddr dst, gw, mask; 321 struct sockaddr_in *sin; 322 int error; 323 324 /* Destination: (default) */ 325 memset((caddr_t)&dst, 0, sizeof(dst)); 326 dst.sa_len = sizeof(dst); 327 dst.sa_family = AF_INET; 328 /* Gateway: */ 329 memset((caddr_t)&gw, 0, sizeof(gw)); 330 sin = (struct sockaddr_in *)&gw; 331 sin->sin_len = sizeof(*sin); 332 sin->sin_family = AF_INET; 333 sin->sin_addr.s_addr = gw_ip->s_addr; 334 /* Mask: (zero length) */ 335 /* XXX - Just pass a null pointer? */ 336 memset(&mask, 0, sizeof(mask)); 337 338 /* add, dest, gw, mask, flags, 0 */ 339 error = rtrequest(RTM_ADD, &dst, &gw, &mask, 340 (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL); 341 if (error) { 342 printf("nfs_boot: add route, error=%d\n", error); 343 error = 0; 344 } 345 } 346 347 /* 348 * Get an initial NFS file handle using Sun RPC/mountd. 349 * Separate function because we used to call it twice. 350 * (once for root and once for swap) 351 */ 352 static int 353 nfs_boot_getfh(ndm) 354 struct nfs_dlmount *ndm; /* output */ 355 { 356 struct nfs_args *args; 357 struct sockaddr_in *sin; 358 char *pathname; 359 int error; 360 u_int16_t port; 361 362 args = &ndm->ndm_args; 363 364 /* Initialize mount args. */ 365 memset((caddr_t) args, 0, sizeof(*args)); 366 args->addr = &ndm->ndm_saddr; 367 args->addrlen = args->addr->sa_len; 368 #ifdef NFS_BOOT_TCP 369 args->sotype = SOCK_STREAM; 370 #else 371 args->sotype = SOCK_DGRAM; 372 #endif 373 args->fh = ndm->ndm_fh; 374 args->hostname = ndm->ndm_host; 375 args->flags = NFSMNT_RESVPORT | NFSMNT_NFSV3; 376 377 #ifdef NFS_BOOT_OPTIONS 378 args->flags |= NFS_BOOT_OPTIONS; 379 #endif 380 #ifdef NFS_BOOT_RWSIZE 381 /* 382 * Reduce rsize,wsize for interfaces that consistently 383 * drop fragments of long UDP messages. (i.e. wd8003). 384 * You can always change these later via remount. 385 */ 386 args->flags |= NFSMNT_WSIZE | NFSMNT_RSIZE; 387 args->wsize = NFS_BOOT_RWSIZE; 388 args->rsize = NFS_BOOT_RWSIZE; 389 #endif 390 391 /* 392 * Find the pathname part of the "server:pathname" 393 * string left in ndm->ndm_host by nfs_boot_init. 394 */ 395 pathname = strchr(ndm->ndm_host, ':'); 396 if (pathname == 0) { 397 printf("nfs_boot: getfh - no pathname\n"); 398 return (EIO); 399 } 400 pathname++; 401 402 /* 403 * Get file handle using RPC to mountd/mount 404 */ 405 sin = (struct sockaddr_in *)&ndm->ndm_saddr; 406 error = md_mount(sin, pathname, args); 407 if (error) { 408 printf("nfs_boot: mountd `%s', error=%d\n", 409 ndm->ndm_host, error); 410 return (error); 411 } 412 413 /* Set port number for NFS use. */ 414 /* XXX: NFS port is always 2049, right? */ 415 #ifdef NFS_BOOT_TCP 416 retry: 417 #endif 418 error = krpc_portmap(sin, NFS_PROG, 419 (args->flags & NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2, 420 (args->sotype == SOCK_STREAM) ? IPPROTO_TCP : IPPROTO_UDP, 421 &port); 422 if (port == htons(0)) 423 error = EIO; 424 if (error) { 425 #ifdef NFS_BOOT_TCP 426 if (args->sotype == SOCK_STREAM) { 427 args->sotype = SOCK_DGRAM; 428 goto retry; 429 } 430 #endif 431 printf("nfs_boot: portmap NFS, error=%d\n", error); 432 return (error); 433 } 434 sin->sin_port = port; 435 return (0); 436 } 437 438 439 /* 440 * RPC: mountd/mount 441 * Given a server pathname, get an NFS file handle. 442 * Also, sets sin->sin_port to the NFS service port. 443 */ 444 static int 445 md_mount(mdsin, path, argp) 446 struct sockaddr_in *mdsin; /* mountd server address */ 447 char *path; 448 struct nfs_args *argp; 449 { 450 /* The RPC structures */ 451 struct rdata { 452 u_int32_t errno; 453 union { 454 u_int8_t v2fh[NFSX_V2FH]; 455 struct { 456 u_int32_t fhlen; 457 u_int8_t fh[1]; 458 } v3fh; 459 } fh; 460 } *rdata; 461 struct mbuf *m; 462 u_int8_t *fh; 463 int minlen, error; 464 int mntver; 465 466 mntver = (argp->flags & NFSMNT_NFSV3) ? 3 : 2; 467 do { 468 /* 469 * Get port number for MOUNTD. 470 */ 471 error = krpc_portmap(mdsin, RPCPROG_MNT, mntver, 472 IPPROTO_UDP, &mdsin->sin_port); 473 if (error) 474 continue; 475 476 /* This mbuf is consumed by krpc_call. */ 477 m = xdr_string_encode(path, strlen(path)); 478 if (m == NULL) 479 return ENOMEM; 480 481 /* Do RPC to mountd. */ 482 error = krpc_call(mdsin, RPCPROG_MNT, mntver, 483 RPCMNT_MOUNT, &m, NULL); 484 if (error != EPROGMISMATCH) 485 break; 486 /* Try lower version of mountd. */ 487 } while (--mntver >= 1); 488 if (error) { 489 printf("nfs_boot: mountd error=%d\n", error); 490 return error; 491 } 492 if (mntver != 3) 493 argp->flags &= ~NFSMNT_NFSV3; 494 495 /* The reply might have only the errno. */ 496 if (m->m_len < 4) 497 goto bad; 498 /* Have at least errno, so check that. */ 499 rdata = mtod(m, struct rdata *); 500 error = fxdr_unsigned(u_int32_t, rdata->errno); 501 if (error) 502 goto out; 503 504 /* Have errno==0, so the fh must be there. */ 505 if (mntver == 3) { 506 argp->fhsize = fxdr_unsigned(u_int32_t, rdata->fh.v3fh.fhlen); 507 if (argp->fhsize > NFSX_V3FHMAX) 508 goto bad; 509 minlen = 2 * sizeof(u_int32_t) + argp->fhsize; 510 } else { 511 argp->fhsize = NFSX_V2FH; 512 minlen = sizeof(u_int32_t) + argp->fhsize; 513 } 514 515 if (m->m_len < minlen) { 516 m = m_pullup(m, minlen); 517 if (m == NULL) 518 return(EBADRPC); 519 rdata = mtod(m, struct rdata *); 520 } 521 522 fh = (mntver == 3) ? 523 rdata->fh.v3fh.fh : rdata->fh.v2fh; 524 memcpy(argp->fh, fh, argp->fhsize); 525 526 goto out; 527 528 bad: 529 error = EBADRPC; 530 531 out: 532 m_freem(m); 533 return error; 534 } 535