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