Home | History | Annotate | Line # | Download | only in nfs
nfs_boot.c revision 1.17
      1 /*    $NetBSD: nfs_boot.c,v 1.17 1995/05/20 01:52:52 mycroft 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 
     55 #include "ether.h"
     56 #if NETHER == 0
     57 
     58 int nfs_boot_init(nd, procp)
     59 	struct nfs_diskless *nd;
     60 	struct proc *procp;
     61 {
     62 	panic("nfs_boot_init: no ether");
     63 }
     64 
     65 #else /* NETHER */
     66 
     67 /*
     68  * Support for NFS diskless booting, specifically getting information
     69  * about where to boot from, what pathnames, etc.
     70  *
     71  * This implememtation uses RARP and the bootparam RPC.
     72  * We are forced to implement RPC anyway (to get file handles)
     73  * so we might as well take advantage of it for bootparam too.
     74  *
     75  * The diskless boot sequence goes as follows:
     76  * (1) Use RARP to get our interface address
     77  * (2) Use RPC/bootparam/whoami to get our hostname,
     78  *     our IP address, and the server's IP address.
     79  * (3) Use RPC/bootparam/getfile to get the root path
     80  * (4) Use RPC/mountd to get the root file handle
     81  * (5) Use RPC/bootparam/getfile to get the swap path
     82  * (6) Use RPC/mountd to get the swap file handle
     83  *
     84  * (This happens to be the way Sun does it too.)
     85  */
     86 
     87 /* bootparam RPC */
     88 static int bp_whoami __P((struct sockaddr_in *bpsin,
     89 	struct in_addr *my_ip, struct in_addr *gw_ip));
     90 static int bp_getfile __P((struct sockaddr_in *bpsin, char *key,
     91 	struct sockaddr_in *mdsin, char *servname, char *path));
     92 
     93 /* mountd RPC */
     94 static int md_mount __P((struct sockaddr_in *mdsin, char *path,
     95 	u_char *fh));
     96 
     97 /* other helpers */
     98 static void get_path_and_handle __P((struct sockaddr_in *bpsin,
     99 	char *key, struct nfs_dlmount *ndmntp));
    100 
    101 char	*nfsbootdevname;
    102 
    103 /*
    104  * Called with an empty nfs_diskless struct to be filled in.
    105  */
    106 int
    107 nfs_boot_init(nd, procp)
    108 	struct nfs_diskless *nd;
    109 	struct proc *procp;
    110 {
    111 	struct ifreq ireq;
    112 	struct in_addr my_ip, gw_ip;
    113 	struct sockaddr_in bp_sin;
    114 	struct sockaddr_in *sin;
    115 	struct ifnet *ifp;
    116 	struct socket *so;
    117 	int error;
    118 
    119 	/*
    120 	 * Find an interface, rarp for its ip address, stuff it, the
    121 	 * implied broadcast addr, and netmask into a nfs_diskless struct.
    122 	 *
    123 	 * This was moved here from nfs_vfsops.c because this procedure
    124 	 * would be quite different if someone decides to write (i.e.) a
    125 	 * BOOTP version of this file (might not use RARP, etc.)
    126 	 */
    127 
    128 	/*
    129 	 * Find a network interface.
    130 	 */
    131 	if (nfsbootdevname)
    132 		ifp = ifunit(nfsbootdevname);
    133 	else
    134 		for (ifp = ifnet; ifp; ifp = ifp->if_next)
    135 			if ((ifp->if_flags &
    136 			     (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
    137 				break;
    138 	if (ifp == NULL)
    139 		panic("nfs_boot: no suitable interface");
    140 	sprintf(ireq.ifr_name, "%s%d", ifp->if_name, ifp->if_unit);
    141 	printf("nfs_boot: using network interface '%s'\n",
    142 	    ireq.ifr_name);
    143 
    144 	/*
    145 	 * Bring up the interface.
    146 	 */
    147 	if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0)) != 0)
    148 		panic("nfs_boot: socreate, error=%d", error);
    149 	ireq.ifr_flags = IFF_UP;
    150 	error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)&ireq, procp);
    151 	if (error)
    152 		panic("nfs_boot: SIFFLAGS, error=%d", error);
    153 
    154 	/*
    155 	 * Do RARP for the interface address.
    156 	 */
    157 	if ((error = revarpwhoami(&my_ip, ifp)) != 0)
    158 		panic("revarp failed, error=%d", error);
    159 	printf("nfs_boot: client_addr=0x%x\n", ntohl(my_ip.s_addr));
    160 
    161 	/*
    162 	 * Do enough of ifconfig(8) so that the chosen interface
    163 	 * can talk to the servers.  (just set the address)
    164 	 */
    165 	sin = (struct sockaddr_in *)&ireq.ifr_addr;
    166 	bzero((caddr_t)sin, sizeof(*sin));
    167 	sin->sin_len = sizeof(*sin);
    168 	sin->sin_family = AF_INET;
    169 	sin->sin_addr.s_addr = my_ip.s_addr;
    170 	error = ifioctl(so, SIOCSIFADDR, (caddr_t)&ireq, procp);
    171 	if (error)
    172 		panic("nfs_boot: set if addr, error=%d", error);
    173 
    174 	soclose(so);
    175 
    176 	/*
    177 	 * Get client name and gateway address.
    178 	 * RPC: bootparam/whoami
    179 	 * Use the old broadcast address for the WHOAMI
    180 	 * call because we do not yet know our netmask.
    181 	 * The server address returned by the WHOAMI call
    182 	 * is used for all subsequent booptaram RPCs.
    183 	 */
    184 	bzero((caddr_t)&bp_sin, sizeof(bp_sin));
    185 	bp_sin.sin_len = sizeof(bp_sin);
    186 	bp_sin.sin_family = AF_INET;
    187 	bp_sin.sin_addr.s_addr = INADDR_BROADCAST;
    188 	hostnamelen = MAXHOSTNAMELEN;
    189 
    190 	/* this returns gateway IP address */
    191 	error = bp_whoami(&bp_sin, &my_ip, &gw_ip);
    192 	if (error)
    193 		panic("nfs_boot: bootparam whoami, error=%d", error);
    194 	printf("nfs_boot: server_addr=0x%x\n",
    195 		   ntohl(bp_sin.sin_addr.s_addr));
    196 	printf("nfs_boot: hostname=%s\n", hostname);
    197 
    198 #ifdef	NFS_BOOT_GATEWAY
    199 	/*
    200 	 * XXX - This code is conditionally compiled only because
    201 	 * many bootparam servers (in particular, SunOS 4.1.3)
    202 	 * always set the gateway address to their own address.
    203 	 * The bootparam server is not necessarily the gateway.
    204 	 * We could just believe the server, and at worst you would
    205 	 * need to delete the incorrect default route before adding
    206 	 * the correct one, but for simplicity, ignore the gateway.
    207 	 * If your server is OK, you can turn on this option.
    208 	 *
    209 	 * If the gateway address is set, add a default route.
    210 	 * (The mountd RPCs may go across a gateway.)
    211 	 */
    212 	if (gw_ip.s_addr) {
    213 		struct sockaddr dst, gw, mask;
    214 		/* Destination: (default) */
    215 		bzero((caddr_t)&dst, sizeof(dst));
    216 		dst.sa_len = sizeof(dst);
    217 		dst.sa_family = AF_INET;
    218 		/* Gateway: */
    219 		bzero((caddr_t)&gw, sizeof(gw));
    220 		sin = (struct sockaddr_in *)&gw;
    221 		sin->sin_len = sizeof(gw);
    222 		sin->sin_family = AF_INET;
    223 		sin->sin_addr.s_addr = gw_ip.s_addr;
    224 		/* Mask: (zero length) */
    225 		bzero(&mask, sizeof(mask));
    226 
    227 		printf("nfs_boot: gateway=0x%x\n", ntohl(gw_ip.s_addr));
    228 		/* add, dest, gw, mask, flags, 0 */
    229 		error = rtrequest(RTM_ADD, &dst, (struct sockaddr *)&gw,
    230 		    &mask, (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
    231 		if (error)
    232 			printf("nfs_boot: add route, error=%d\n", error);
    233 	}
    234 #endif
    235 
    236 	get_path_and_handle(&bp_sin, "root", &nd->nd_root);
    237 	get_path_and_handle(&bp_sin, "swap", &nd->nd_swap);
    238 
    239 	return (0);
    240 }
    241 
    242 static void
    243 get_path_and_handle(bpsin, key, ndmntp)
    244 	struct sockaddr_in *bpsin;	/* bootparam server */
    245 	char *key;			/* root or swap */
    246 	struct nfs_dlmount *ndmntp;	/* output */
    247 {
    248 	char pathname[MAXPATHLEN];
    249 	char *sp, *dp, *endp;
    250 	int error;
    251 
    252 	/*
    253 	 * Get server:pathname for "key" (root or swap)
    254 	 * using RPC to bootparam/getfile
    255 	 */
    256 	error = bp_getfile(bpsin, key, &ndmntp->ndm_saddr,
    257 	    ndmntp->ndm_host, pathname);
    258 	if (error)
    259 		panic("nfs_boot: bootparam get %s: %d", key, error);
    260 
    261 	/*
    262 	 * Get file handle for "key" (root or swap)
    263 	 * using RPC to mountd/mount
    264 	 */
    265 	error = md_mount(&ndmntp->ndm_saddr, pathname, ndmntp->ndm_fh);
    266 	if (error)
    267 		panic("nfs_boot: mountd %s, error=%d", key, error);
    268 
    269 	/* Construct remote path (for getmntinfo(3)) */
    270 	dp = ndmntp->ndm_host;
    271 	endp = dp + MNAMELEN - 1;
    272 	dp += strlen(dp);
    273 	*dp++ = ':';
    274 	for (sp = pathname; *sp && dp < endp;)
    275 		*dp++ = *sp++;
    276 	*dp = '\0';
    277 
    278 }
    279 
    280 
    281 /*
    282  * RPC: bootparam/whoami
    283  * Given client IP address, get:
    284  *	client name	(hostname)
    285  *	domain name (domainname)
    286  *	gateway address
    287  *
    288  * The hostname and domainname are set here for convenience.
    289  *
    290  * Note - bpsin is initialized to the broadcast address,
    291  * and will be replaced with the bootparam server address
    292  * after this call is complete.  Have to use PMAP_PROC_CALL
    293  * to make sure we get responses only from a servers that
    294  * know about us (don't want to broadcast a getport call).
    295  */
    296 static int
    297 bp_whoami(bpsin, my_ip, gw_ip)
    298 	struct sockaddr_in *bpsin;
    299 	struct in_addr *my_ip;
    300 	struct in_addr *gw_ip;
    301 {
    302 	/* RPC structures for PMAPPROC_CALLIT */
    303 	struct whoami_call {
    304 		u_int32_t call_prog;
    305 		u_int32_t call_vers;
    306 		u_int32_t call_proc;
    307 		u_int32_t call_arglen;
    308 	} *call;
    309 	struct callit_reply {
    310 		u_int32_t port;
    311 		u_int32_t encap_len;
    312 		/* encapsulated data here */
    313 	} *reply;
    314 
    315 	struct mbuf *m, *from;
    316 	struct sockaddr_in *sin;
    317 	int error, msg_len;
    318 	int16_t port;
    319 
    320 	/*
    321 	 * Build request message for PMAPPROC_CALLIT.
    322 	 */
    323 	m = m_get(M_WAIT, MT_DATA);
    324 	call = mtod(m, struct whoami_call *);
    325 	m->m_len = sizeof(*call);
    326 	call->call_prog = txdr_unsigned(BOOTPARAM_PROG);
    327 	call->call_vers = txdr_unsigned(BOOTPARAM_VERS);
    328 	call->call_proc = txdr_unsigned(BOOTPARAM_WHOAMI);
    329 
    330 	/*
    331 	 * append encapsulated data (client IP address)
    332 	 */
    333 	m->m_next = xdr_inaddr_encode(my_ip);
    334 	call->call_arglen = txdr_unsigned(m->m_next->m_len);
    335 
    336 	/* RPC: portmap/callit */
    337 	bpsin->sin_port = htons(PMAPPORT);
    338 	from = NULL;
    339 	error = krpc_call(bpsin, PMAPPROG, PMAPVERS,
    340 			PMAPPROC_CALLIT, &m, &from);
    341 	if (error)
    342 		return error;
    343 
    344 	/*
    345 	 * Parse result message.
    346 	 */
    347 	if (m->m_len < sizeof(*reply)) {
    348 		m = m_pullup(m, sizeof(*reply));
    349 		if (m == NULL)
    350 			goto bad;
    351 	}
    352 	reply = mtod(m, struct callit_reply *);
    353 	port = fxdr_unsigned(u_int32_t, reply->port);
    354 	msg_len = fxdr_unsigned(u_int32_t, reply->encap_len);
    355 	m_adj(m, sizeof(*reply));
    356 
    357 	/*
    358 	 * Save bootparam server address
    359 	 */
    360 	sin = mtod(from, struct sockaddr_in *);
    361 	bpsin->sin_port = htons(port);
    362 	bpsin->sin_addr.s_addr = sin->sin_addr.s_addr;
    363 
    364 	/* client name */
    365 	hostnamelen = MAXHOSTNAMELEN-1;
    366 	m = xdr_string_decode(m, hostname, &hostnamelen);
    367 	if (m == NULL)
    368 		goto bad;
    369 
    370 	/* domain name */
    371 	domainnamelen = MAXHOSTNAMELEN-1;
    372 	m = xdr_string_decode(m, domainname, &domainnamelen);
    373 	if (m == NULL)
    374 		goto bad;
    375 
    376 	/* gateway address */
    377 	m = xdr_inaddr_decode(m, gw_ip);
    378 	if (m == NULL)
    379 		goto bad;
    380 
    381 	/* success */
    382 	goto out;
    383 
    384 bad:
    385 	printf("nfs_boot: bootparam_whoami: bad reply\n");
    386 	error = EBADRPC;
    387 
    388 out:
    389 	if (from)
    390 		m_freem(from);
    391 	if (m)
    392 		m_freem(m);
    393 	return(error);
    394 }
    395 
    396 
    397 /*
    398  * RPC: bootparam/getfile
    399  * Given client name and file "key", get:
    400  *	server name
    401  *	server IP address
    402  *	server pathname
    403  */
    404 static int
    405 bp_getfile(bpsin, key, md_sin, serv_name, pathname)
    406 	struct sockaddr_in *bpsin;
    407 	char *key;
    408 	struct sockaddr_in *md_sin;
    409 	char *serv_name;
    410 	char *pathname;
    411 {
    412 	struct mbuf *m;
    413 	struct sockaddr_in *sin;
    414 	struct in_addr inaddr;
    415 	int error, sn_len, path_len;
    416 
    417 	/*
    418 	 * Build request message.
    419 	 */
    420 
    421 	/* client name (hostname) */
    422 	m  = xdr_string_encode(hostname, hostnamelen);
    423 
    424 	/* key name (root or swap) */
    425 	m->m_next = xdr_string_encode(key, strlen(key));
    426 
    427 	/* RPC: bootparam/getfile */
    428 	error = krpc_call(bpsin, BOOTPARAM_PROG, BOOTPARAM_VERS,
    429 			BOOTPARAM_GETFILE, &m, NULL);
    430 	if (error)
    431 		return error;
    432 
    433 	/*
    434 	 * Parse result message.
    435 	 */
    436 
    437 	/* server name */
    438 	sn_len = MNAMELEN-1;
    439 	m = xdr_string_decode(m, serv_name, &sn_len);
    440 	if (m == NULL)
    441 		goto bad;
    442 
    443 	/* server IP address (mountd/NFS) */
    444 	m = xdr_inaddr_decode(m, &inaddr);
    445 	if (m == NULL)
    446 		goto bad;
    447 
    448 	/* server pathname */
    449 	path_len = MAXPATHLEN-1;
    450 	m = xdr_string_decode(m, pathname, &path_len);
    451 	if (m == NULL)
    452 		goto bad;
    453 
    454 	/* setup server socket address */
    455 	sin = md_sin;
    456 	bzero((caddr_t)sin, sizeof(*sin));
    457 	sin->sin_len = sizeof(*sin);
    458 	sin->sin_family = AF_INET;
    459 	sin->sin_addr = inaddr;
    460 
    461 	/* success */
    462 	goto out;
    463 
    464 bad:
    465 	printf("nfs_boot: bootparam_getfile: bad reply\n");
    466 	error = EBADRPC;
    467 
    468 out:
    469 	m_freem(m);
    470 	return(0);
    471 }
    472 
    473 
    474 /*
    475  * RPC: mountd/mount
    476  * Given a server pathname, get an NFS file handle.
    477  * Also, sets sin->sin_port to the NFS service port.
    478  */
    479 static int
    480 md_mount(mdsin, path, fhp)
    481 	struct sockaddr_in *mdsin;		/* mountd server address */
    482 	char *path;
    483 	u_char *fhp;
    484 {
    485 	/* The RPC structures */
    486 	struct rdata {
    487 		u_int32_t	errno;
    488 		u_char	fh[NFS_FHSIZE];
    489 	} *rdata;
    490 	struct mbuf *m;
    491 	int error;
    492 
    493 	/* Get port number for MOUNTD. */
    494 	error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
    495 						 &mdsin->sin_port);
    496 	if (error) return error;
    497 
    498 	m = xdr_string_encode(path, strlen(path));
    499 
    500 	/* Do RPC to mountd. */
    501 	error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
    502 			RPCMNT_MOUNT, &m, NULL);
    503 	if (error)
    504 		return error;	/* message already freed */
    505 
    506 	if (m->m_len < sizeof(*rdata)) {
    507 		m = m_pullup(m, sizeof(*rdata));
    508 		if (m == NULL)
    509 			goto bad;
    510 	}
    511 	rdata = mtod(m, struct rdata *);
    512 	error = fxdr_unsigned(u_int32_t, rdata->errno);
    513 	if (error)
    514 		goto bad;
    515 	bcopy(rdata->fh, fhp, NFS_FHSIZE);
    516 
    517 	/* Set port number for NFS use. */
    518 	error = krpc_portmap(mdsin, NFS_PROG, NFS_VER2,
    519 						 &mdsin->sin_port);
    520 	goto out;
    521 
    522 bad:
    523 	error = EBADRPC;
    524 
    525 out:
    526 	m_freem(m);
    527 	return error;
    528 }
    529 
    530 #endif /* NETHER */
    531