Home | History | Annotate | Line # | Download | only in nfs
nfs_boot.c revision 1.24
      1 /*	$NetBSD: nfs_boot.c,v 1.24 1996/02/16 15:18:19 gwr Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1995 Adam Glass, Gordon Ross
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the authors may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/param.h>
     31 #include <sys/systm.h>
     32 #include <sys/kernel.h>
     33 #include <sys/conf.h>
     34 #include <sys/ioctl.h>
     35 #include <sys/proc.h>
     36 #include <sys/mount.h>
     37 #include <sys/mbuf.h>
     38 #include <sys/reboot.h>
     39 #include <sys/socket.h>
     40 #include <sys/socketvar.h>
     41 
     42 #include <net/if.h>
     43 #include <net/route.h>
     44 
     45 #include <netinet/in.h>
     46 #include <netinet/if_ether.h>
     47 
     48 #include <nfs/rpcv2.h>
     49 #include <nfs/nfsv2.h>
     50 #include <nfs/nfs.h>
     51 #include <nfs/nfsdiskless.h>
     52 #include <nfs/krpc.h>
     53 #include <nfs/xdr_subs.h>
     54 #include <nfs/nfs_var.h>
     55 
     56 #include "ether.h"
     57 #if NETHER == 0
     58 
     59 int nfs_boot_init(nd, procp)
     60 	struct nfs_diskless *nd;
     61 	struct proc *procp;
     62 {
     63 	panic("nfs_boot_init: no ether");
     64 }
     65 
     66 void
     67 nfs_boot_getfh(bpsin, key, ndmntp)
     68 	struct sockaddr_in *bpsin;
     69 	char *key;
     70 	struct nfs_dlmount *ndmntp;
     71 {
     72 	/* can not get here */
     73 }
     74 
     75 #else /* NETHER */
     76 
     77 /*
     78  * Support for NFS diskless booting, specifically getting information
     79  * about where to boot from, what pathnames, etc.
     80  *
     81  * This implememtation uses RARP and the bootparam RPC.
     82  * We are forced to implement RPC anyway (to get file handles)
     83  * so we might as well take advantage of it for bootparam too.
     84  *
     85  * The diskless boot sequence goes as follows:
     86  * (1) Use RARP to get our interface address
     87  * (2) Use RPC/bootparam/whoami to get our hostname,
     88  *     our IP address, and the server's IP address.
     89  * (3) Use RPC/bootparam/getfile to get the root path
     90  * (4) Use RPC/mountd to get the root file handle
     91  * (5) Use RPC/bootparam/getfile to get the swap path
     92  * (6) Use RPC/mountd to get the swap file handle
     93  *
     94  * (This happens to be the way Sun does it too.)
     95  */
     96 
     97 /* bootparam RPC */
     98 static int bp_whoami __P((struct sockaddr_in *bpsin,
     99 	struct in_addr *my_ip, struct in_addr *gw_ip));
    100 static int bp_getfile __P((struct sockaddr_in *bpsin, char *key,
    101 	struct sockaddr_in *mdsin, char *servname, char *path));
    102 
    103 /* mountd RPC */
    104 static int md_mount __P((struct sockaddr_in *mdsin, char *path,
    105 	u_char *fh));
    106 
    107 /* other helpers */
    108 static void get_path_and_handle __P((struct sockaddr_in *bpsin,
    109 	char *key, struct nfs_dlmount *ndmntp));
    110 
    111 char	*nfsbootdevname;
    112 
    113 /*
    114  * Called with an empty nfs_diskless struct to be filled in.
    115  */
    116 int
    117 nfs_boot_init(nd, procp)
    118 	struct nfs_diskless *nd;
    119 	struct proc *procp;
    120 {
    121 	struct ifreq ireq;
    122 	struct in_addr my_ip, gw_ip;
    123 	struct sockaddr_in bp_sin;
    124 	struct sockaddr_in *sin;
    125 	struct ifnet *ifp;
    126 	struct socket *so;
    127 	int error;
    128 
    129 	/*
    130 	 * Find an interface, rarp for its ip address, stuff it, the
    131 	 * implied broadcast addr, and netmask into a nfs_diskless struct.
    132 	 *
    133 	 * This was moved here from nfs_vfsops.c because this procedure
    134 	 * would be quite different if someone decides to write (i.e.) a
    135 	 * BOOTP version of this file (might not use RARP, etc.)
    136 	 */
    137 
    138 	/*
    139 	 * Find a network interface.
    140 	 */
    141 	if (nfsbootdevname)
    142 		ifp = ifunit(nfsbootdevname);
    143 	else
    144 		for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next)
    145 			if ((ifp->if_flags &
    146 			     (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
    147 				break;
    148 	if (ifp == NULL)
    149 		panic("nfs_boot: no suitable interface");
    150 	sprintf(ireq.ifr_name, "%s%d", ifp->if_name, ifp->if_unit);
    151 	printf("nfs_boot: using network interface '%s'\n",
    152 	    ireq.ifr_name);
    153 
    154 	/*
    155 	 * Bring up the interface.
    156 	 *
    157 	 * Get the old interface flags and or IFF_UP into them; if
    158 	 * IFF_UP set blindly, interface selection can be clobbered.
    159 	 */
    160 	if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0)) != 0)
    161 		panic("nfs_boot: socreate, error=%d", error);
    162 	error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)&ireq, procp);
    163 	if (error)
    164 		panic("nfs_boot: GIFFLAGS, error=%d", error);
    165 	ireq.ifr_flags |= IFF_UP;
    166 	error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)&ireq, procp);
    167 	if (error)
    168 		panic("nfs_boot: SIFFLAGS, error=%d", error);
    169 
    170 	/*
    171 	 * Do RARP for the interface address.
    172 	 */
    173 	if ((error = revarpwhoami(&my_ip, ifp)) != 0)
    174 		panic("revarp failed, error=%d", error);
    175 	printf("nfs_boot: client_addr=0x%x\n", ntohl(my_ip.s_addr));
    176 
    177 	/*
    178 	 * Do enough of ifconfig(8) so that the chosen interface
    179 	 * can talk to the servers.  (just set the address)
    180 	 */
    181 	sin = (struct sockaddr_in *)&ireq.ifr_addr;
    182 	bzero((caddr_t)sin, sizeof(*sin));
    183 	sin->sin_len = sizeof(*sin);
    184 	sin->sin_family = AF_INET;
    185 	sin->sin_addr.s_addr = my_ip.s_addr;
    186 	error = ifioctl(so, SIOCSIFADDR, (caddr_t)&ireq, procp);
    187 	if (error)
    188 		panic("nfs_boot: set if addr, error=%d", error);
    189 
    190 	soclose(so);
    191 
    192 	/*
    193 	 * Get client name and gateway address.
    194 	 * RPC: bootparam/whoami
    195 	 * Use the old broadcast address for the WHOAMI
    196 	 * call because we do not yet know our netmask.
    197 	 * The server address returned by the WHOAMI call
    198 	 * is used for all subsequent booptaram RPCs.
    199 	 */
    200 	bzero((caddr_t)&bp_sin, sizeof(bp_sin));
    201 	bp_sin.sin_len = sizeof(bp_sin);
    202 	bp_sin.sin_family = AF_INET;
    203 	bp_sin.sin_addr.s_addr = INADDR_BROADCAST;
    204 	hostnamelen = MAXHOSTNAMELEN;
    205 
    206 	/* this returns gateway IP address */
    207 	error = bp_whoami(&bp_sin, &my_ip, &gw_ip);
    208 	if (error)
    209 		panic("nfs_boot: bootparam whoami, error=%d", error);
    210 	printf("nfs_boot: server_addr=0x%x\n",
    211 		   ntohl(bp_sin.sin_addr.s_addr));
    212 	printf("nfs_boot: hostname=%s\n", hostname);
    213 
    214 #ifdef	NFS_BOOT_GATEWAY
    215 	/*
    216 	 * XXX - This code is conditionally compiled only because
    217 	 * many bootparam servers (in particular, SunOS 4.1.3)
    218 	 * always set the gateway address to their own address.
    219 	 * The bootparam server is not necessarily the gateway.
    220 	 * We could just believe the server, and at worst you would
    221 	 * need to delete the incorrect default route before adding
    222 	 * the correct one, but for simplicity, ignore the gateway.
    223 	 * If your server is OK, you can turn on this option.
    224 	 *
    225 	 * If the gateway address is set, add a default route.
    226 	 * (The mountd RPCs may go across a gateway.)
    227 	 */
    228 	if (gw_ip.s_addr) {
    229 		struct sockaddr dst, gw, mask;
    230 		/* Destination: (default) */
    231 		bzero((caddr_t)&dst, sizeof(dst));
    232 		dst.sa_len = sizeof(dst);
    233 		dst.sa_family = AF_INET;
    234 		/* Gateway: */
    235 		bzero((caddr_t)&gw, sizeof(gw));
    236 		sin = (struct sockaddr_in *)&gw;
    237 		sin->sin_len = sizeof(gw);
    238 		sin->sin_family = AF_INET;
    239 		sin->sin_addr.s_addr = gw_ip.s_addr;
    240 		/* Mask: (zero length) */
    241 		bzero(&mask, sizeof(mask));
    242 
    243 		printf("nfs_boot: gateway=0x%x\n", ntohl(gw_ip.s_addr));
    244 		/* add, dest, gw, mask, flags, 0 */
    245 		error = rtrequest(RTM_ADD, &dst, (struct sockaddr *)&gw,
    246 		    &mask, (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
    247 		if (error)
    248 			printf("nfs_boot: add route, error=%d\n", error);
    249 	}
    250 #endif
    251 
    252 	bcopy(&bp_sin, &nd->nd_boot, sizeof(bp_sin));
    253 
    254 	return (0);
    255 }
    256 
    257 void
    258 nfs_boot_getfh(bpsin, key, ndmntp)
    259 	struct sockaddr_in *bpsin;	/* bootparam server */
    260 	char *key;			/* root or swap */
    261 	struct nfs_dlmount *ndmntp;	/* output */
    262 {
    263 	char pathname[MAXPATHLEN];
    264 	char *sp, *dp, *endp;
    265 	struct sockaddr_in *sin;
    266 	int error;
    267 
    268 	sin = &ndmntp->ndm_saddr;
    269 
    270 	/*
    271 	 * Get server:pathname for "key" (root or swap)
    272 	 * using RPC to bootparam/getfile
    273 	 */
    274 	error = bp_getfile(bpsin, key, sin,
    275 	    ndmntp->ndm_host, pathname);
    276 	if (error)
    277 		panic("nfs_boot: bootparam get %s: %d", key, error);
    278 
    279 	/*
    280 	 * Get file handle for "key" (root or swap)
    281 	 * using RPC to mountd/mount
    282 	 */
    283 	error = md_mount(sin, pathname, ndmntp->ndm_fh);
    284 	if (error)
    285 		panic("nfs_boot: mountd %s, error=%d", key, error);
    286 
    287 	/* Set port number for NFS use. */
    288 	/* XXX: NFS port is always 2049, right? */
    289 	error = krpc_portmap(sin, NFS_PROG, NFS_VER2, &sin->sin_port);
    290 	if (error)
    291 		panic("nfs_boot: portmap NFS/v2, error=%d", error);
    292 
    293 	/* Construct remote path (for getmntinfo(3)) */
    294 	dp = ndmntp->ndm_host;
    295 	endp = dp + MNAMELEN - 1;
    296 	dp += strlen(dp);
    297 	*dp++ = ':';
    298 	for (sp = pathname; *sp && dp < endp;)
    299 		*dp++ = *sp++;
    300 	*dp = '\0';
    301 
    302 }
    303 
    304 
    305 /*
    306  * RPC: bootparam/whoami
    307  * Given client IP address, get:
    308  *	client name	(hostname)
    309  *	domain name (domainname)
    310  *	gateway address
    311  *
    312  * The hostname and domainname are set here for convenience.
    313  *
    314  * Note - bpsin is initialized to the broadcast address,
    315  * and will be replaced with the bootparam server address
    316  * after this call is complete.  Have to use PMAP_PROC_CALL
    317  * to make sure we get responses only from a servers that
    318  * know about us (don't want to broadcast a getport call).
    319  */
    320 static int
    321 bp_whoami(bpsin, my_ip, gw_ip)
    322 	struct sockaddr_in *bpsin;
    323 	struct in_addr *my_ip;
    324 	struct in_addr *gw_ip;
    325 {
    326 	/* RPC structures for PMAPPROC_CALLIT */
    327 	struct whoami_call {
    328 		u_int32_t call_prog;
    329 		u_int32_t call_vers;
    330 		u_int32_t call_proc;
    331 		u_int32_t call_arglen;
    332 	} *call;
    333 	struct callit_reply {
    334 		u_int32_t port;
    335 		u_int32_t encap_len;
    336 		/* encapsulated data here */
    337 	} *reply;
    338 
    339 	struct mbuf *m, *from;
    340 	struct sockaddr_in *sin;
    341 	int error, msg_len;
    342 	int16_t port;
    343 
    344 	/*
    345 	 * Build request message for PMAPPROC_CALLIT.
    346 	 */
    347 	m = m_get(M_WAIT, MT_DATA);
    348 	call = mtod(m, struct whoami_call *);
    349 	m->m_len = sizeof(*call);
    350 	call->call_prog = txdr_unsigned(BOOTPARAM_PROG);
    351 	call->call_vers = txdr_unsigned(BOOTPARAM_VERS);
    352 	call->call_proc = txdr_unsigned(BOOTPARAM_WHOAMI);
    353 
    354 	/*
    355 	 * append encapsulated data (client IP address)
    356 	 */
    357 	m->m_next = xdr_inaddr_encode(my_ip);
    358 	call->call_arglen = txdr_unsigned(m->m_next->m_len);
    359 
    360 	/* RPC: portmap/callit */
    361 	bpsin->sin_port = htons(PMAPPORT);
    362 	from = NULL;
    363 	error = krpc_call(bpsin, PMAPPROG, PMAPVERS,
    364 			PMAPPROC_CALLIT, &m, &from);
    365 	if (error)
    366 		return error;
    367 
    368 	/*
    369 	 * Parse result message.
    370 	 */
    371 	if (m->m_len < sizeof(*reply)) {
    372 		m = m_pullup(m, sizeof(*reply));
    373 		if (m == NULL)
    374 			goto bad;
    375 	}
    376 	reply = mtod(m, struct callit_reply *);
    377 	port = fxdr_unsigned(u_int32_t, reply->port);
    378 	msg_len = fxdr_unsigned(u_int32_t, reply->encap_len);
    379 	m_adj(m, sizeof(*reply));
    380 
    381 	/*
    382 	 * Save bootparam server address
    383 	 */
    384 	sin = mtod(from, struct sockaddr_in *);
    385 	bpsin->sin_port = htons(port);
    386 	bpsin->sin_addr.s_addr = sin->sin_addr.s_addr;
    387 
    388 	/* client name */
    389 	hostnamelen = MAXHOSTNAMELEN-1;
    390 	m = xdr_string_decode(m, hostname, &hostnamelen);
    391 	if (m == NULL)
    392 		goto bad;
    393 
    394 	/* domain name */
    395 	domainnamelen = MAXHOSTNAMELEN-1;
    396 	m = xdr_string_decode(m, domainname, &domainnamelen);
    397 	if (m == NULL)
    398 		goto bad;
    399 
    400 	/* gateway address */
    401 	m = xdr_inaddr_decode(m, gw_ip);
    402 	if (m == NULL)
    403 		goto bad;
    404 
    405 	/* success */
    406 	goto out;
    407 
    408 bad:
    409 	printf("nfs_boot: bootparam_whoami: bad reply\n");
    410 	error = EBADRPC;
    411 
    412 out:
    413 	if (from)
    414 		m_freem(from);
    415 	if (m)
    416 		m_freem(m);
    417 	return(error);
    418 }
    419 
    420 
    421 /*
    422  * RPC: bootparam/getfile
    423  * Given client name and file "key", get:
    424  *	server name
    425  *	server IP address
    426  *	server pathname
    427  */
    428 static int
    429 bp_getfile(bpsin, key, md_sin, serv_name, pathname)
    430 	struct sockaddr_in *bpsin;
    431 	char *key;
    432 	struct sockaddr_in *md_sin;
    433 	char *serv_name;
    434 	char *pathname;
    435 {
    436 	struct mbuf *m;
    437 	struct sockaddr_in *sin;
    438 	struct in_addr inaddr;
    439 	int error, sn_len, path_len;
    440 
    441 	/*
    442 	 * Build request message.
    443 	 */
    444 
    445 	/* client name (hostname) */
    446 	m  = xdr_string_encode(hostname, hostnamelen);
    447 	if (m == NULL)
    448 		return (ENOMEM);
    449 
    450 	/* key name (root or swap) */
    451 	m->m_next = xdr_string_encode(key, strlen(key));
    452 	if (m->m_next == NULL)
    453 		return (ENOMEM);
    454 
    455 	/* RPC: bootparam/getfile */
    456 	error = krpc_call(bpsin, BOOTPARAM_PROG, BOOTPARAM_VERS,
    457 			BOOTPARAM_GETFILE, &m, NULL);
    458 	if (error)
    459 		return error;
    460 
    461 	/*
    462 	 * Parse result message.
    463 	 */
    464 
    465 	/* server name */
    466 	sn_len = MNAMELEN-1;
    467 	m = xdr_string_decode(m, serv_name, &sn_len);
    468 	if (m == NULL)
    469 		goto bad;
    470 
    471 	/* server IP address (mountd/NFS) */
    472 	m = xdr_inaddr_decode(m, &inaddr);
    473 	if (m == NULL)
    474 		goto bad;
    475 
    476 	/* server pathname */
    477 	path_len = MAXPATHLEN-1;
    478 	m = xdr_string_decode(m, pathname, &path_len);
    479 	if (m == NULL)
    480 		goto bad;
    481 
    482 	/* setup server socket address */
    483 	sin = md_sin;
    484 	bzero((caddr_t)sin, sizeof(*sin));
    485 	sin->sin_len = sizeof(*sin);
    486 	sin->sin_family = AF_INET;
    487 	sin->sin_addr = inaddr;
    488 
    489 	/* success */
    490 	goto out;
    491 
    492 bad:
    493 	printf("nfs_boot: bootparam_getfile: bad reply\n");
    494 	error = EBADRPC;
    495 
    496 out:
    497 	m_freem(m);
    498 	return(0);
    499 }
    500 
    501 
    502 /*
    503  * RPC: mountd/mount
    504  * Given a server pathname, get an NFS file handle.
    505  * Also, sets sin->sin_port to the NFS service port.
    506  */
    507 static int
    508 md_mount(mdsin, path, fhp)
    509 	struct sockaddr_in *mdsin;		/* mountd server address */
    510 	char *path;
    511 	u_char *fhp;
    512 {
    513 	/* The RPC structures */
    514 	struct rdata {
    515 		u_int32_t errno;
    516 		u_int8_t  fh[NFS_FHSIZE];
    517 	} *rdata;
    518 	struct mbuf *m;
    519 	int error;
    520 
    521 	/* Get port number for MOUNTD. */
    522 	error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
    523 						 &mdsin->sin_port);
    524 	if (error) return error;
    525 
    526 	m = xdr_string_encode(path, strlen(path));
    527 	if (m == NULL)
    528 		return (ENOMEM);
    529 
    530 	/* Do RPC to mountd. */
    531 	error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
    532 			RPCMNT_MOUNT, &m, NULL);
    533 	if (error)
    534 		return error;	/* message already freed */
    535 
    536 	/* The reply might have only the errno. */
    537 	if (m->m_len < 4)
    538 		goto bad;
    539 	/* Have at least errno, so check that. */
    540 	rdata = mtod(m, struct rdata *);
    541 	error = fxdr_unsigned(u_int32_t, rdata->errno);
    542 	if (error)
    543 		goto out;
    544 
    545 	/* Have errno==0, so the fh must be there. */
    546 	if (m->m_len < sizeof(*rdata)) {
    547 		m = m_pullup(m, sizeof(*rdata));
    548 		if (m == NULL)
    549 			goto bad;
    550 		rdata = mtod(m, struct rdata *);
    551 	}
    552 	bcopy(rdata->fh, fhp, NFS_FHSIZE);
    553 	goto out;
    554 
    555 bad:
    556 	error = EBADRPC;
    557 
    558 out:
    559 	m_freem(m);
    560 	return error;
    561 }
    562 
    563 #endif /* NETHER */
    564