Home | History | Annotate | Line # | Download | only in nfs
nfs_bootparam.c revision 1.14
      1 /*	$NetBSD: nfs_bootparam.c,v 1.14 1999/05/07 15:17:26 drochner 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, Sun-style (RPC/bootparams)
     41  */
     42 
     43 #include "opt_nfs_boot.h"
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/kernel.h>
     48 #include <sys/conf.h>
     49 #include <sys/device.h>
     50 #include <sys/ioctl.h>
     51 #include <sys/proc.h>
     52 #include <sys/mount.h>
     53 #include <sys/mbuf.h>
     54 #include <sys/reboot.h>
     55 #include <sys/socket.h>
     56 #include <sys/socketvar.h>
     57 
     58 #include <net/if.h>
     59 #include <net/if_types.h>
     60 #include <net/route.h>
     61 #include <net/if_ether.h>
     62 
     63 #include <netinet/in.h>
     64 #include <netinet/if_inarp.h>
     65 
     66 #include <nfs/rpcv2.h>
     67 #include <nfs/krpc.h>
     68 #include <nfs/xdr_subs.h>
     69 
     70 #include <nfs/nfsproto.h>
     71 #include <nfs/nfs.h>
     72 #include <nfs/nfsmount.h>
     73 #include <nfs/nfsdiskless.h>
     74 
     75 #include "arp.h"
     76 
     77 /*
     78  * There are two implementations of NFS diskless boot.
     79  * This implementation uses Sun RPC/bootparams, and the
     80  * the other uses BOOTP (RFC951 - see nfs_bootdhcp.c).
     81  *
     82  * The Sun-style boot sequence goes as follows:
     83  * (1) Use RARP to get our interface address
     84  * (2) Use RPC/bootparam/whoami to get our hostname,
     85  *     our IP address, and the server's IP address.
     86  * (3) Use RPC/bootparam/getfile to get the root path
     87  * (4) Use RPC/mountd to get the root file handle
     88  * (5) Use RPC/bootparam/getfile to get the swap path
     89  * (6) Use RPC/mountd to get the swap file handle
     90  */
     91 
     92 /* bootparam RPC */
     93 static int bp_whoami __P((struct sockaddr_in *bpsin,
     94 	struct in_addr *my_ip, struct in_addr *gw_ip));
     95 static int bp_getfile __P((struct sockaddr_in *bpsin, char *key,
     96 	struct nfs_dlmount *ndm));
     97 
     98 
     99 /*
    100  * Get client name, gateway address, then
    101  * get root and swap server:pathname info.
    102  * RPCs: bootparam/whoami, bootparam/getfile
    103  *
    104  * Use the old broadcast address for the WHOAMI
    105  * call because we do not yet know our netmask.
    106  * The server address returned by the WHOAMI call
    107  * is used for all subsequent booptaram RPCs.
    108  */
    109 int
    110 nfs_bootparam(nd, procp)
    111 	struct nfs_diskless *nd;
    112 	struct proc *procp;
    113 {
    114 	struct ifnet *ifp = nd->nd_ifp;
    115 	struct in_addr my_ip, arps_ip, gw_ip;
    116 	struct sockaddr_in bp_sin;
    117 	struct sockaddr_in *sin;
    118 #ifndef NFS_BOOTPARAM_NOGATEWAY
    119 	struct nfs_dlmount *gw_ndm = 0;
    120 	char *p;
    121 	u_int32_t mask;
    122 #endif
    123 	int error;
    124 
    125 	/*
    126 	 * Bring up the interface. (just set the "up" flag)
    127 	 */
    128 	error = nfs_boot_ifupdown(ifp, procp, 1);
    129 	if (error) {
    130 		printf("nfs_boot: SIFFLAGS, error=%d\n", error);
    131 		return (error);
    132 	}
    133 
    134 	error = EADDRNOTAVAIL;
    135 #if NARP > 0
    136 	if (ifp->if_type == IFT_ETHER || ifp->if_type == IFT_FDDI) {
    137 		/*
    138 		 * Do RARP for the interface address.
    139 		 */
    140 		error = revarpwhoarewe(ifp, &arps_ip, &my_ip);
    141 	}
    142 #endif
    143 	if (error) {
    144 		printf("revarp failed, error=%d\n", error);
    145 		goto out;
    146 	}
    147 
    148 	nd->nd_myip.s_addr = my_ip.s_addr;
    149 	printf("nfs_boot: client_addr=%s (RARP from %s)\n",
    150 	       inet_ntoa(my_ip), inet_ntoa(arps_ip));
    151 
    152 	/*
    153 	 * Do enough of ifconfig(8) so that the chosen interface
    154 	 * can talk to the servers.  (just set the address)
    155 	 */
    156 	error = nfs_boot_setaddress(ifp, procp, my_ip.s_addr,
    157 				    INADDR_ANY, INADDR_ANY);
    158 	if (error) {
    159 		printf("nfs_boot: set ifaddr, error=%d\n", error);
    160 		goto out;
    161 	}
    162 
    163 	/*
    164 	 * Get client name and gateway address.
    165 	 * RPC: bootparam/whoami
    166 	 * Use the old broadcast address for the WHOAMI
    167 	 * call because we do not yet know our netmask.
    168 	 * The server address returned by the WHOAMI call
    169 	 * is used for all subsequent booptaram RPCs.
    170 	 */
    171 	sin = &bp_sin;
    172 	memset((caddr_t)sin, 0, sizeof(*sin));
    173 	sin->sin_len = sizeof(*sin);
    174 	sin->sin_family = AF_INET;
    175 	sin->sin_addr.s_addr = INADDR_BROADCAST;
    176 
    177 	/* Do the RPC/bootparam/whoami. */
    178 	error = bp_whoami(sin, &my_ip, &gw_ip);
    179 	if (error) {
    180 		printf("nfs_boot: bootparam whoami, error=%d\n", error);
    181 		goto delout;
    182 	}
    183 	printf("nfs_boot: server_addr=%s\n", inet_ntoa(sin->sin_addr));
    184 	printf("nfs_boot: hostname=%s\n", hostname);
    185 
    186 	/*
    187 	 * Now fetch the server:pathname strings and server IP
    188 	 * for root and swap.  Missing swap is not fatal.
    189 	 */
    190 	error = bp_getfile(sin, "root", &nd->nd_root);
    191 	if (error) {
    192 		printf("nfs_boot: bootparam get root: %d\n", error);
    193 		goto delout;
    194 	}
    195 
    196 #ifndef NFS_BOOTPARAM_NOGATEWAY
    197 	gw_ndm = malloc(sizeof(*gw_ndm), M_NFSMNT, M_WAITOK);
    198 	memset((caddr_t)gw_ndm, 0, sizeof(*gw_ndm));
    199 	error = bp_getfile(sin, "gateway", gw_ndm);
    200 	if (error) {
    201 		/* No gateway supplied. No error, but try fallback. */
    202 		error = 0;
    203 		goto nogwrepl;
    204 	}
    205 	sin = (struct sockaddr_in *) &gw_ndm->ndm_saddr;
    206 	if (sin->sin_addr.s_addr == 0)
    207 		goto out;	/* no gateway */
    208 
    209 	/* OK, we have a gateway! */
    210 	printf("nfs_boot: gateway=%s\n", inet_ntoa(sin->sin_addr));
    211 	/* Just save it.  Caller adds the route. */
    212 	nd->nd_gwip = sin->sin_addr;
    213 
    214 	/* Look for a mask string after the colon. */
    215 	p = strchr(gw_ndm->ndm_host, ':');
    216 	if (p == 0)
    217 		goto out;	/* no netmask */
    218 	/* have pathname */
    219 	p++;	/* skip ':' */
    220 	mask = inet_addr(p);	/* libkern */
    221 	if (mask == 0)
    222 		goto out;	/* no netmask */
    223 
    224 	/* Have a netmask too!  Save it; update the I/F. */
    225 	nd->nd_mask.s_addr = mask;
    226 	printf("nfs_boot: my_mask=%s\n", inet_ntoa(nd->nd_mask));
    227 	(void)  nfs_boot_deladdress(ifp, procp, my_ip.s_addr);
    228 	error = nfs_boot_setaddress(ifp, procp, my_ip.s_addr,
    229 				    mask, INADDR_ANY);
    230 	if (error) {
    231 		printf("nfs_boot: set ifmask, error=%d\n", error);
    232 		goto out;
    233 	}
    234 	goto gwok;
    235 nogwrepl:
    236 #endif
    237 #ifdef NFS_BOOT_GATEWAY
    238 	/*
    239 	 * Note: we normally ignore the gateway address returned
    240 	 * by the "bootparam/whoami" RPC above, because many old
    241 	 * bootparam servers supply a bogus gateway value.
    242 	 *
    243 	 * These deficiencies in the bootparam RPC interface are
    244 	 * circumvented by using the bootparam/getfile RPC.  The
    245 	 * parameter "gateway" is requested, and if its returned,
    246 	 * we use the "server" part of the reply as the gateway,
    247 	 * and use the "pathname" part of the reply as the mask.
    248 	 * (The mask comes to us as a string.)
    249 	 */
    250 	if (gw_ip.s_addr) {
    251 		/* Our caller will add the route. */
    252 		nd->nd_gwip = gw_ip;
    253 	}
    254 #endif
    255 
    256 delout:
    257 	if (error)
    258 		(void) nfs_boot_deladdress(ifp, procp, my_ip.s_addr);
    259 out:
    260 	if (error) {
    261 		(void) nfs_boot_ifupdown(ifp, procp, 0);
    262 		nfs_boot_flushrt(ifp);
    263 	}
    264 #ifndef NFS_BOOTPARAM_NOGATEWAY
    265 gwok:
    266 	if (gw_ndm)
    267 		free(gw_ndm, M_NFSMNT);
    268 #endif
    269 	return (error);
    270 }
    271 
    272 
    273 /*
    274  * RPC: bootparam/whoami
    275  * Given client IP address, get:
    276  *	client name	(hostname)
    277  *	domain name (domainname)
    278  *	gateway address
    279  *
    280  * The hostname and domainname are set here for convenience.
    281  *
    282  * Note - bpsin is initialized to the broadcast address,
    283  * and will be replaced with the bootparam server address
    284  * after this call is complete.  Have to use PMAP_PROC_CALL
    285  * to make sure we get responses only from a servers that
    286  * know about us (don't want to broadcast a getport call).
    287  */
    288 static int
    289 bp_whoami(bpsin, my_ip, gw_ip)
    290 	struct sockaddr_in *bpsin;
    291 	struct in_addr *my_ip;
    292 	struct in_addr *gw_ip;
    293 {
    294 	/* RPC structures for PMAPPROC_CALLIT */
    295 	struct whoami_call {
    296 		u_int32_t call_prog;
    297 		u_int32_t call_vers;
    298 		u_int32_t call_proc;
    299 		u_int32_t call_arglen;
    300 	} *call;
    301 	struct callit_reply {
    302 		u_int32_t port;
    303 		u_int32_t encap_len;
    304 		/* encapsulated data here */
    305 	} *reply;
    306 
    307 	struct mbuf *m, *from;
    308 	struct sockaddr_in *sin;
    309 	int error, msg_len;
    310 	int16_t port;
    311 
    312 	/*
    313 	 * Build request message for PMAPPROC_CALLIT.
    314 	 */
    315 	m = m_get(M_WAIT, MT_DATA);
    316 	call = mtod(m, struct whoami_call *);
    317 	m->m_len = sizeof(*call);
    318 	call->call_prog = txdr_unsigned(BOOTPARAM_PROG);
    319 	call->call_vers = txdr_unsigned(BOOTPARAM_VERS);
    320 	call->call_proc = txdr_unsigned(BOOTPARAM_WHOAMI);
    321 
    322 	/*
    323 	 * append encapsulated data (client IP address)
    324 	 */
    325 	m->m_next = xdr_inaddr_encode(my_ip);
    326 	call->call_arglen = txdr_unsigned(m->m_next->m_len);
    327 
    328 	/* RPC: portmap/callit */
    329 	bpsin->sin_port = htons(PMAPPORT);
    330 	from = NULL;
    331 	error = krpc_call(bpsin, PMAPPROG, PMAPVERS,
    332 			PMAPPROC_CALLIT, &m, &from);
    333 	if (error)
    334 		return error;
    335 
    336 	/*
    337 	 * Parse result message.
    338 	 */
    339 	if (m->m_len < sizeof(*reply)) {
    340 		m = m_pullup(m, sizeof(*reply));
    341 		if (m == NULL)
    342 			goto bad;
    343 	}
    344 	reply = mtod(m, struct callit_reply *);
    345 	port = fxdr_unsigned(u_int32_t, reply->port);
    346 	msg_len = fxdr_unsigned(u_int32_t, reply->encap_len);
    347 	m_adj(m, sizeof(*reply));
    348 
    349 	/*
    350 	 * Save bootparam server address
    351 	 */
    352 	sin = mtod(from, struct sockaddr_in *);
    353 	bpsin->sin_port = htons(port);
    354 	bpsin->sin_addr.s_addr = sin->sin_addr.s_addr;
    355 
    356 	/* client name */
    357 	hostnamelen = MAXHOSTNAMELEN-1;
    358 	m = xdr_string_decode(m, hostname, &hostnamelen);
    359 	if (m == NULL)
    360 		goto bad;
    361 
    362 	/* domain name */
    363 	domainnamelen = MAXHOSTNAMELEN-1;
    364 	m = xdr_string_decode(m, domainname, &domainnamelen);
    365 	if (m == NULL)
    366 		goto bad;
    367 
    368 	/* gateway address */
    369 	m = xdr_inaddr_decode(m, gw_ip);
    370 	if (m == NULL)
    371 		goto bad;
    372 
    373 	/* success */
    374 	goto out;
    375 
    376 bad:
    377 	printf("nfs_boot: bootparam_whoami: bad reply\n");
    378 	error = EBADRPC;
    379 
    380 out:
    381 	if (from)
    382 		m_freem(from);
    383 	if (m)
    384 		m_freem(m);
    385 	return(error);
    386 }
    387 
    388 
    389 /*
    390  * RPC: bootparam/getfile
    391  * Given client name and file "key", get:
    392  *	server name
    393  *	server IP address
    394  *	server pathname
    395  */
    396 static int
    397 bp_getfile(bpsin, key, ndm)
    398 	struct sockaddr_in *bpsin;
    399 	char *key;
    400 	struct nfs_dlmount *ndm;
    401 {
    402 	char pathname[MNAMELEN];
    403 	struct in_addr inaddr;
    404 	struct sockaddr_in *sin;
    405 	struct mbuf *m;
    406 	char *serv_name;
    407 	int error, sn_len, path_len;
    408 
    409 	/*
    410 	 * Build request message.
    411 	 */
    412 
    413 	/* client name (hostname) */
    414 	m  = xdr_string_encode(hostname, hostnamelen);
    415 	if (m == NULL)
    416 		return (ENOMEM);
    417 
    418 	/* key name (root or swap) */
    419 	m->m_next = xdr_string_encode(key, strlen(key));
    420 	if (m->m_next == NULL)
    421 		return (ENOMEM);
    422 
    423 	/* RPC: bootparam/getfile */
    424 	error = krpc_call(bpsin, BOOTPARAM_PROG, BOOTPARAM_VERS,
    425 	                  BOOTPARAM_GETFILE, &m, NULL);
    426 	if (error)
    427 		return error;
    428 
    429 	/*
    430 	 * Parse result message.
    431 	 */
    432 
    433 	/* server name */
    434 	serv_name = &ndm->ndm_host[0];
    435 	sn_len = sizeof(ndm->ndm_host) - 1;
    436 	m = xdr_string_decode(m, serv_name, &sn_len);
    437 	if (m == NULL)
    438 		goto bad;
    439 
    440 	/* server IP address (mountd/NFS) */
    441 	m = xdr_inaddr_decode(m, &inaddr);
    442 	if (m == NULL)
    443 		goto bad;
    444 
    445 	/* server pathname */
    446 	path_len = sizeof(pathname) - 1;
    447 	m = xdr_string_decode(m, pathname, &path_len);
    448 	if (m == NULL)
    449 		goto bad;
    450 
    451 	/*
    452 	 * Store the results in the nfs_dlmount.
    453 	 * The strings become "server:pathname"
    454 	 */
    455 	sin = (struct sockaddr_in *) &ndm->ndm_saddr;
    456 	memset((caddr_t)sin, 0, sizeof(*sin));
    457 	sin->sin_len = sizeof(*sin);
    458 	sin->sin_family = AF_INET;
    459 	sin->sin_addr = inaddr;
    460 	if ((sn_len + 1 + path_len + 1) > sizeof(ndm->ndm_host)) {
    461 		printf("nfs_boot: getfile name too long\n");
    462 		error = EIO;
    463 		goto out;
    464 	}
    465 	ndm->ndm_host[sn_len] = ':';
    466 	memcpy(ndm->ndm_host + sn_len + 1, pathname, path_len + 1);
    467 
    468 	/* success */
    469 	goto out;
    470 
    471 bad:
    472 	printf("nfs_boot: bootparam_getfile: bad reply\n");
    473 	error = EBADRPC;
    474 
    475 out:
    476 	m_freem(m);
    477 	return(0);
    478 }
    479