Home | History | Annotate | Line # | Download | only in nfs
nfs_boot.c revision 1.20
      1 /*    $NetBSD: nfs_boot.c,v 1.20 1995/12/19 23:07:24 cgd 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.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_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 	 * Get the old interface flags and or IFF_UP into them; if
    148 	 * IFF_UP set blindly, interface selection can be clobbered.
    149 	 */
    150 	if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0)) != 0)
    151 		panic("nfs_boot: socreate, error=%d", error);
    152 	error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)&ireq, procp);
    153 	if (error)
    154 		panic("nfs_boot: GIFFLAGS, error=%d", error);
    155 	ireq.ifr_flags |= IFF_UP;
    156 	error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)&ireq, procp);
    157 	if (error)
    158 		panic("nfs_boot: SIFFLAGS, error=%d", error);
    159 
    160 	/*
    161 	 * Do RARP for the interface address.
    162 	 */
    163 	if ((error = revarpwhoami(&my_ip, ifp)) != 0)
    164 		panic("revarp failed, error=%d", error);
    165 	printf("nfs_boot: client_addr=0x%x\n", ntohl(my_ip.s_addr));
    166 
    167 	/*
    168 	 * Do enough of ifconfig(8) so that the chosen interface
    169 	 * can talk to the servers.  (just set the address)
    170 	 */
    171 	sin = (struct sockaddr_in *)&ireq.ifr_addr;
    172 	bzero((caddr_t)sin, sizeof(*sin));
    173 	sin->sin_len = sizeof(*sin);
    174 	sin->sin_family = AF_INET;
    175 	sin->sin_addr.s_addr = my_ip.s_addr;
    176 	error = ifioctl(so, SIOCSIFADDR, (caddr_t)&ireq, procp);
    177 	if (error)
    178 		panic("nfs_boot: set if addr, error=%d", error);
    179 
    180 	soclose(so);
    181 
    182 	/*
    183 	 * Get client name and gateway address.
    184 	 * RPC: bootparam/whoami
    185 	 * Use the old broadcast address for the WHOAMI
    186 	 * call because we do not yet know our netmask.
    187 	 * The server address returned by the WHOAMI call
    188 	 * is used for all subsequent booptaram RPCs.
    189 	 */
    190 	bzero((caddr_t)&bp_sin, sizeof(bp_sin));
    191 	bp_sin.sin_len = sizeof(bp_sin);
    192 	bp_sin.sin_family = AF_INET;
    193 	bp_sin.sin_addr.s_addr = INADDR_BROADCAST;
    194 	hostnamelen = MAXHOSTNAMELEN;
    195 
    196 	/* this returns gateway IP address */
    197 	error = bp_whoami(&bp_sin, &my_ip, &gw_ip);
    198 	if (error)
    199 		panic("nfs_boot: bootparam whoami, error=%d", error);
    200 	printf("nfs_boot: server_addr=0x%x\n",
    201 		   ntohl(bp_sin.sin_addr.s_addr));
    202 	printf("nfs_boot: hostname=%s\n", hostname);
    203 
    204 #ifdef	NFS_BOOT_GATEWAY
    205 	/*
    206 	 * XXX - This code is conditionally compiled only because
    207 	 * many bootparam servers (in particular, SunOS 4.1.3)
    208 	 * always set the gateway address to their own address.
    209 	 * The bootparam server is not necessarily the gateway.
    210 	 * We could just believe the server, and at worst you would
    211 	 * need to delete the incorrect default route before adding
    212 	 * the correct one, but for simplicity, ignore the gateway.
    213 	 * If your server is OK, you can turn on this option.
    214 	 *
    215 	 * If the gateway address is set, add a default route.
    216 	 * (The mountd RPCs may go across a gateway.)
    217 	 */
    218 	if (gw_ip.s_addr) {
    219 		struct sockaddr dst, gw, mask;
    220 		/* Destination: (default) */
    221 		bzero((caddr_t)&dst, sizeof(dst));
    222 		dst.sa_len = sizeof(dst);
    223 		dst.sa_family = AF_INET;
    224 		/* Gateway: */
    225 		bzero((caddr_t)&gw, sizeof(gw));
    226 		sin = (struct sockaddr_in *)&gw;
    227 		sin->sin_len = sizeof(gw);
    228 		sin->sin_family = AF_INET;
    229 		sin->sin_addr.s_addr = gw_ip.s_addr;
    230 		/* Mask: (zero length) */
    231 		bzero(&mask, sizeof(mask));
    232 
    233 		printf("nfs_boot: gateway=0x%x\n", ntohl(gw_ip.s_addr));
    234 		/* add, dest, gw, mask, flags, 0 */
    235 		error = rtrequest(RTM_ADD, &dst, (struct sockaddr *)&gw,
    236 		    &mask, (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
    237 		if (error)
    238 			printf("nfs_boot: add route, error=%d\n", error);
    239 	}
    240 #endif
    241 
    242 	get_path_and_handle(&bp_sin, "root", &nd->nd_root);
    243 	get_path_and_handle(&bp_sin, "swap", &nd->nd_swap);
    244 
    245 	return (0);
    246 }
    247 
    248 static void
    249 get_path_and_handle(bpsin, key, ndmntp)
    250 	struct sockaddr_in *bpsin;	/* bootparam server */
    251 	char *key;			/* root or swap */
    252 	struct nfs_dlmount *ndmntp;	/* output */
    253 {
    254 	char pathname[MAXPATHLEN];
    255 	char *sp, *dp, *endp;
    256 	int error;
    257 
    258 	/*
    259 	 * Get server:pathname for "key" (root or swap)
    260 	 * using RPC to bootparam/getfile
    261 	 */
    262 	error = bp_getfile(bpsin, key, &ndmntp->ndm_saddr,
    263 	    ndmntp->ndm_host, pathname);
    264 	if (error)
    265 		panic("nfs_boot: bootparam get %s: %d", key, error);
    266 
    267 	/*
    268 	 * Get file handle for "key" (root or swap)
    269 	 * using RPC to mountd/mount
    270 	 */
    271 	error = md_mount(&ndmntp->ndm_saddr, pathname, ndmntp->ndm_fh);
    272 	if (error)
    273 		panic("nfs_boot: mountd %s, error=%d", key, error);
    274 
    275 	/* Construct remote path (for getmntinfo(3)) */
    276 	dp = ndmntp->ndm_host;
    277 	endp = dp + MNAMELEN - 1;
    278 	dp += strlen(dp);
    279 	*dp++ = ':';
    280 	for (sp = pathname; *sp && dp < endp;)
    281 		*dp++ = *sp++;
    282 	*dp = '\0';
    283 
    284 }
    285 
    286 
    287 /*
    288  * RPC: bootparam/whoami
    289  * Given client IP address, get:
    290  *	client name	(hostname)
    291  *	domain name (domainname)
    292  *	gateway address
    293  *
    294  * The hostname and domainname are set here for convenience.
    295  *
    296  * Note - bpsin is initialized to the broadcast address,
    297  * and will be replaced with the bootparam server address
    298  * after this call is complete.  Have to use PMAP_PROC_CALL
    299  * to make sure we get responses only from a servers that
    300  * know about us (don't want to broadcast a getport call).
    301  */
    302 static int
    303 bp_whoami(bpsin, my_ip, gw_ip)
    304 	struct sockaddr_in *bpsin;
    305 	struct in_addr *my_ip;
    306 	struct in_addr *gw_ip;
    307 {
    308 	/* RPC structures for PMAPPROC_CALLIT */
    309 	struct whoami_call {
    310 		u_int32_t call_prog;
    311 		u_int32_t call_vers;
    312 		u_int32_t call_proc;
    313 		u_int32_t call_arglen;
    314 	} *call;
    315 	struct callit_reply {
    316 		u_int32_t port;
    317 		u_int32_t encap_len;
    318 		/* encapsulated data here */
    319 	} *reply;
    320 
    321 	struct mbuf *m, *from;
    322 	struct sockaddr_in *sin;
    323 	int error, msg_len;
    324 	int16_t port;
    325 
    326 	/*
    327 	 * Build request message for PMAPPROC_CALLIT.
    328 	 */
    329 	m = m_get(M_WAIT, MT_DATA);
    330 	call = mtod(m, struct whoami_call *);
    331 	m->m_len = sizeof(*call);
    332 	call->call_prog = txdr_unsigned(BOOTPARAM_PROG);
    333 	call->call_vers = txdr_unsigned(BOOTPARAM_VERS);
    334 	call->call_proc = txdr_unsigned(BOOTPARAM_WHOAMI);
    335 
    336 	/*
    337 	 * append encapsulated data (client IP address)
    338 	 */
    339 	m->m_next = xdr_inaddr_encode(my_ip);
    340 	call->call_arglen = txdr_unsigned(m->m_next->m_len);
    341 
    342 	/* RPC: portmap/callit */
    343 	bpsin->sin_port = htons(PMAPPORT);
    344 	from = NULL;
    345 	error = krpc_call(bpsin, PMAPPROG, PMAPVERS,
    346 			PMAPPROC_CALLIT, &m, &from);
    347 	if (error)
    348 		return error;
    349 
    350 	/*
    351 	 * Parse result message.
    352 	 */
    353 	if (m->m_len < sizeof(*reply)) {
    354 		m = m_pullup(m, sizeof(*reply));
    355 		if (m == NULL)
    356 			goto bad;
    357 	}
    358 	reply = mtod(m, struct callit_reply *);
    359 	port = fxdr_unsigned(u_int32_t, reply->port);
    360 	msg_len = fxdr_unsigned(u_int32_t, reply->encap_len);
    361 	m_adj(m, sizeof(*reply));
    362 
    363 	/*
    364 	 * Save bootparam server address
    365 	 */
    366 	sin = mtod(from, struct sockaddr_in *);
    367 	bpsin->sin_port = htons(port);
    368 	bpsin->sin_addr.s_addr = sin->sin_addr.s_addr;
    369 
    370 	/* client name */
    371 	hostnamelen = MAXHOSTNAMELEN-1;
    372 	m = xdr_string_decode(m, hostname, &hostnamelen);
    373 	if (m == NULL)
    374 		goto bad;
    375 
    376 	/* domain name */
    377 	domainnamelen = MAXHOSTNAMELEN-1;
    378 	m = xdr_string_decode(m, domainname, &domainnamelen);
    379 	if (m == NULL)
    380 		goto bad;
    381 
    382 	/* gateway address */
    383 	m = xdr_inaddr_decode(m, gw_ip);
    384 	if (m == NULL)
    385 		goto bad;
    386 
    387 	/* success */
    388 	goto out;
    389 
    390 bad:
    391 	printf("nfs_boot: bootparam_whoami: bad reply\n");
    392 	error = EBADRPC;
    393 
    394 out:
    395 	if (from)
    396 		m_freem(from);
    397 	if (m)
    398 		m_freem(m);
    399 	return(error);
    400 }
    401 
    402 
    403 /*
    404  * RPC: bootparam/getfile
    405  * Given client name and file "key", get:
    406  *	server name
    407  *	server IP address
    408  *	server pathname
    409  */
    410 static int
    411 bp_getfile(bpsin, key, md_sin, serv_name, pathname)
    412 	struct sockaddr_in *bpsin;
    413 	char *key;
    414 	struct sockaddr_in *md_sin;
    415 	char *serv_name;
    416 	char *pathname;
    417 {
    418 	struct mbuf *m;
    419 	struct sockaddr_in *sin;
    420 	struct in_addr inaddr;
    421 	int error, sn_len, path_len;
    422 
    423 	/*
    424 	 * Build request message.
    425 	 */
    426 
    427 	/* client name (hostname) */
    428 	m  = xdr_string_encode(hostname, hostnamelen);
    429 
    430 	/* key name (root or swap) */
    431 	m->m_next = xdr_string_encode(key, strlen(key));
    432 
    433 	/* RPC: bootparam/getfile */
    434 	error = krpc_call(bpsin, BOOTPARAM_PROG, BOOTPARAM_VERS,
    435 			BOOTPARAM_GETFILE, &m, NULL);
    436 	if (error)
    437 		return error;
    438 
    439 	/*
    440 	 * Parse result message.
    441 	 */
    442 
    443 	/* server name */
    444 	sn_len = MNAMELEN-1;
    445 	m = xdr_string_decode(m, serv_name, &sn_len);
    446 	if (m == NULL)
    447 		goto bad;
    448 
    449 	/* server IP address (mountd/NFS) */
    450 	m = xdr_inaddr_decode(m, &inaddr);
    451 	if (m == NULL)
    452 		goto bad;
    453 
    454 	/* server pathname */
    455 	path_len = MAXPATHLEN-1;
    456 	m = xdr_string_decode(m, pathname, &path_len);
    457 	if (m == NULL)
    458 		goto bad;
    459 
    460 	/* setup server socket address */
    461 	sin = md_sin;
    462 	bzero((caddr_t)sin, sizeof(*sin));
    463 	sin->sin_len = sizeof(*sin);
    464 	sin->sin_family = AF_INET;
    465 	sin->sin_addr = inaddr;
    466 
    467 	/* success */
    468 	goto out;
    469 
    470 bad:
    471 	printf("nfs_boot: bootparam_getfile: bad reply\n");
    472 	error = EBADRPC;
    473 
    474 out:
    475 	m_freem(m);
    476 	return(0);
    477 }
    478 
    479 
    480 /*
    481  * RPC: mountd/mount
    482  * Given a server pathname, get an NFS file handle.
    483  * Also, sets sin->sin_port to the NFS service port.
    484  */
    485 static int
    486 md_mount(mdsin, path, fhp)
    487 	struct sockaddr_in *mdsin;		/* mountd server address */
    488 	char *path;
    489 	u_char *fhp;
    490 {
    491 	/* The RPC structures */
    492 	struct rdata {
    493 		u_int32_t errno;
    494 		u_int8_t  fh[NFS_FHSIZE];
    495 	} *rdata;
    496 	struct mbuf *m;
    497 	int error;
    498 
    499 	/* Get port number for MOUNTD. */
    500 	error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
    501 						 &mdsin->sin_port);
    502 	if (error) return error;
    503 
    504 	m = xdr_string_encode(path, strlen(path));
    505 
    506 	/* Do RPC to mountd. */
    507 	error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
    508 			RPCMNT_MOUNT, &m, NULL);
    509 	if (error)
    510 		return error;	/* message already freed */
    511 
    512 	if (m->m_len < sizeof(*rdata)) {
    513 		m = m_pullup(m, sizeof(*rdata));
    514 		if (m == NULL)
    515 			goto bad;
    516 	}
    517 	rdata = mtod(m, struct rdata *);
    518 	error = fxdr_unsigned(u_int32_t, rdata->errno);
    519 	if (error)
    520 		goto bad;
    521 	bcopy(rdata->fh, fhp, NFS_FHSIZE);
    522 
    523 	/* Set port number for NFS use. */
    524 	error = krpc_portmap(mdsin, NFS_PROG, NFS_VER2,
    525 						 &mdsin->sin_port);
    526 	goto out;
    527 
    528 bad:
    529 	error = EBADRPC;
    530 
    531 out:
    532 	m_freem(m);
    533 	return error;
    534 }
    535 
    536 #endif /* NETHER */
    537