Home | History | Annotate | Line # | Download | only in nfs
nfs_bootparam.c revision 1.1
      1 /*	$NetBSD: nfs_bootparam.c,v 1.1 1997/08/29 16:07:46 gwr 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 <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/conf.h>
     47 #include <sys/device.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/proc.h>
     50 #include <sys/mount.h>
     51 #include <sys/mbuf.h>
     52 #include <sys/reboot.h>
     53 #include <sys/socket.h>
     54 #include <sys/socketvar.h>
     55 
     56 #include <net/if.h>
     57 #include <net/route.h>
     58 #include <net/if_ether.h>
     59 
     60 #include <netinet/in.h>
     61 #include <netinet/if_inarp.h>
     62 
     63 #include <nfs/rpcv2.h>
     64 #include <nfs/krpc.h>
     65 #include <nfs/xdr_subs.h>
     66 
     67 #include <nfs/nfsproto.h>
     68 #include <nfs/nfsdiskless.h>
     69 
     70 /*
     71  * There are two implementations of NFS diskless boot.
     72  * This implementation uses Sun RPC/bootparams, and the
     73  * the other uses BOOTP (RFC951 - see nfs_bootdhcp.c).
     74  *
     75  * The Sun-style 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 
     85 /* bootparam RPC */
     86 static int bp_whoami __P((struct sockaddr_in *bpsin,
     87 	struct in_addr *my_ip, struct in_addr *gw_ip));
     88 static int bp_getfile __P((struct sockaddr_in *bpsin, char *key,
     89 	struct nfs_dlmount *ndm));
     90 
     91 
     92 /*
     93  * Get client name, gateway address, then
     94  * get root and swap server:pathname info.
     95  * RPCs: bootparam/whoami, bootparam/getfile
     96  *
     97  * Use the old broadcast address for the WHOAMI
     98  * call because we do not yet know our netmask.
     99  * The server address returned by the WHOAMI call
    100  * is used for all subsequent booptaram RPCs.
    101  */
    102 int
    103 nfs_bootparam(ifp, nd, procp)
    104 	struct ifnet *ifp;
    105 	struct nfs_diskless *nd;
    106 	struct proc *procp;
    107 {
    108 	struct ifreq ireq;
    109 	struct sockaddr_in bp_sin;
    110 	struct sockaddr_in *sin;
    111 	struct socket *so;
    112 	struct in_addr my_ip;
    113 	int error;
    114 
    115 	bzero(&ireq, sizeof(ireq));
    116 	bcopy(ifp->if_xname, ireq.ifr_name, IFNAMSIZ);
    117 
    118 	/*
    119 	 * Get a socket to use for various things in here.
    120 	 * After this, use "goto out" to cleanup and return.
    121 	 */
    122 	error = socreate(AF_INET, &so, SOCK_DGRAM, 0);
    123 	if (error) {
    124 		printf("nfs_boot: socreate, error=%d\n", error);
    125 		return (error);
    126 	}
    127 
    128 	/*
    129 	 * Bring up the interface. (just set the "up" flag)
    130 	 * Get the old interface flags and or IFF_UP into them so
    131 	 * things like media selection flags are not clobbered.
    132 	 */
    133 	error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)&ireq, procp);
    134 	if (error) {
    135 		printf("nfs_boot: GIFFLAGS, error=%d\n", error);
    136 		goto out;
    137 	}
    138 	ireq.ifr_flags |= IFF_UP;
    139 	error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)&ireq, procp);
    140 	if (error) {
    141 		printf("nfs_boot: SIFFLAGS, error=%d\n", error);
    142 		goto out;
    143 	}
    144 
    145 	/*
    146 	 * Do RARP for the interface address.
    147 	 */
    148 	error = revarpwhoami(&my_ip, ifp);
    149 	if (error) {
    150 		printf("revarp failed, error=%d\n", error);
    151 		goto out;
    152 	}
    153 	nd->nd_myip.s_addr = my_ip.s_addr;
    154 	printf("nfs_boot: client_addr=0x%x\n",
    155 	       (u_int32_t)ntohl(my_ip.s_addr));
    156 
    157 	/*
    158 	 * Do enough of ifconfig(8) so that the chosen interface
    159 	 * can talk to the servers.  (just set the address)
    160 	 */
    161 	sin = (struct sockaddr_in *)&ireq.ifr_addr;
    162 	sin->sin_len = sizeof(*sin);
    163 	sin->sin_family = AF_INET;
    164 	sin->sin_addr.s_addr = my_ip.s_addr;
    165 	error = ifioctl(so, SIOCSIFADDR, (caddr_t)&ireq, procp);
    166 	if (error) {
    167 		printf("nfs_boot: set ifaddr, error=%d\n", error);
    168 		goto out;
    169 	}
    170 
    171 	/*
    172 	 * Get client name and gateway address.
    173 	 * RPC: bootparam/whoami
    174 	 * Use the old broadcast address for the WHOAMI
    175 	 * call because we do not yet know our netmask.
    176 	 * The server address returned by the WHOAMI call
    177 	 * is used for all subsequent booptaram RPCs.
    178 	 */
    179 	sin = &bp_sin;
    180 	bzero((caddr_t)sin, sizeof(*sin));
    181 	sin->sin_len = sizeof(*sin);
    182 	sin->sin_family = AF_INET;
    183 	sin->sin_addr.s_addr = INADDR_BROADCAST;
    184 
    185 	/* Do the RPC/bootparam/whoami. */
    186 	error = bp_whoami(sin, &my_ip, &nd->nd_gwip);
    187 	if (error) {
    188 		printf("nfs_boot: bootparam whoami, error=%d\n", error);
    189 		goto out;
    190 	}
    191 	printf("nfs_boot: server_addr=0x%x\n",
    192 		   (u_int32_t)ntohl(sin->sin_addr.s_addr));
    193 	printf("nfs_boot: hostname=%s\n", hostname);
    194 
    195 	/*
    196 	 * Now fetch the server:pathname strings and server IP
    197 	 * for root and swap.  Missing swap is not fatal.
    198 	 */
    199 	error = bp_getfile(sin, "root", &nd->nd_root);
    200 	if (error) {
    201 		printf("nfs_boot: bootparam get root: %d\n", error);
    202 		goto out;
    203 	}
    204 #if 0
    205 	error = bp_getfile(sin, "swap", &nd->nd_swap);
    206 	if (error) {
    207 		printf("nfs_boot: bootparam get swap: %d\n", error);
    208 		error = 0;
    209 	}
    210 #endif
    211 
    212 	/*
    213 	 * XXX - Use bp_getfile(sin, "gate", &gw_ip)
    214 	 * to get the [router:mask] information, maybe?
    215 	 * Better still, just use BOOTP/DHCP instead.
    216 	 */
    217 
    218  out:
    219 	soclose(so);
    220 	return (error);
    221 }
    222 
    223 
    224 /*
    225  * RPC: bootparam/whoami
    226  * Given client IP address, get:
    227  *	client name	(hostname)
    228  *	domain name (domainname)
    229  *	gateway address
    230  *
    231  * The hostname and domainname are set here for convenience.
    232  *
    233  * Note - bpsin is initialized to the broadcast address,
    234  * and will be replaced with the bootparam server address
    235  * after this call is complete.  Have to use PMAP_PROC_CALL
    236  * to make sure we get responses only from a servers that
    237  * know about us (don't want to broadcast a getport call).
    238  */
    239 static int
    240 bp_whoami(bpsin, my_ip, gw_ip)
    241 	struct sockaddr_in *bpsin;
    242 	struct in_addr *my_ip;
    243 	struct in_addr *gw_ip;
    244 {
    245 	/* RPC structures for PMAPPROC_CALLIT */
    246 	struct whoami_call {
    247 		u_int32_t call_prog;
    248 		u_int32_t call_vers;
    249 		u_int32_t call_proc;
    250 		u_int32_t call_arglen;
    251 	} *call;
    252 	struct callit_reply {
    253 		u_int32_t port;
    254 		u_int32_t encap_len;
    255 		/* encapsulated data here */
    256 	} *reply;
    257 
    258 	struct mbuf *m, *from;
    259 	struct sockaddr_in *sin;
    260 	int error, msg_len;
    261 	int16_t port;
    262 
    263 	/*
    264 	 * Build request message for PMAPPROC_CALLIT.
    265 	 */
    266 	m = m_get(M_WAIT, MT_DATA);
    267 	call = mtod(m, struct whoami_call *);
    268 	m->m_len = sizeof(*call);
    269 	call->call_prog = txdr_unsigned(BOOTPARAM_PROG);
    270 	call->call_vers = txdr_unsigned(BOOTPARAM_VERS);
    271 	call->call_proc = txdr_unsigned(BOOTPARAM_WHOAMI);
    272 
    273 	/*
    274 	 * append encapsulated data (client IP address)
    275 	 */
    276 	m->m_next = xdr_inaddr_encode(my_ip);
    277 	call->call_arglen = txdr_unsigned(m->m_next->m_len);
    278 
    279 	/* RPC: portmap/callit */
    280 	bpsin->sin_port = htons(PMAPPORT);
    281 	from = NULL;
    282 	error = krpc_call(bpsin, PMAPPROG, PMAPVERS,
    283 			PMAPPROC_CALLIT, &m, &from);
    284 	if (error)
    285 		return error;
    286 
    287 	/*
    288 	 * Parse result message.
    289 	 */
    290 	if (m->m_len < sizeof(*reply)) {
    291 		m = m_pullup(m, sizeof(*reply));
    292 		if (m == NULL)
    293 			goto bad;
    294 	}
    295 	reply = mtod(m, struct callit_reply *);
    296 	port = fxdr_unsigned(u_int32_t, reply->port);
    297 	msg_len = fxdr_unsigned(u_int32_t, reply->encap_len);
    298 	m_adj(m, sizeof(*reply));
    299 
    300 	/*
    301 	 * Save bootparam server address
    302 	 */
    303 	sin = mtod(from, struct sockaddr_in *);
    304 	bpsin->sin_port = htons(port);
    305 	bpsin->sin_addr.s_addr = sin->sin_addr.s_addr;
    306 
    307 	/* client name */
    308 	hostnamelen = MAXHOSTNAMELEN-1;
    309 	m = xdr_string_decode(m, hostname, &hostnamelen);
    310 	if (m == NULL)
    311 		goto bad;
    312 
    313 	/* domain name */
    314 	domainnamelen = MAXHOSTNAMELEN-1;
    315 	m = xdr_string_decode(m, domainname, &domainnamelen);
    316 	if (m == NULL)
    317 		goto bad;
    318 
    319 	/* gateway address */
    320 	m = xdr_inaddr_decode(m, gw_ip);
    321 	if (m == NULL)
    322 		goto bad;
    323 
    324 	/* success */
    325 	goto out;
    326 
    327 bad:
    328 	printf("nfs_boot: bootparam_whoami: bad reply\n");
    329 	error = EBADRPC;
    330 
    331 out:
    332 	if (from)
    333 		m_freem(from);
    334 	if (m)
    335 		m_freem(m);
    336 	return(error);
    337 }
    338 
    339 
    340 /*
    341  * RPC: bootparam/getfile
    342  * Given client name and file "key", get:
    343  *	server name
    344  *	server IP address
    345  *	server pathname
    346  */
    347 static int
    348 bp_getfile(bpsin, key, ndm)
    349 	struct sockaddr_in *bpsin;
    350 	char *key;
    351 	struct nfs_dlmount *ndm;
    352 {
    353 	char pathname[MNAMELEN];
    354 	struct in_addr inaddr;
    355 	struct sockaddr_in *sin;
    356 	struct mbuf *m;
    357 	char *serv_name;
    358 	int error, sn_len, path_len;
    359 
    360 	/*
    361 	 * Build request message.
    362 	 */
    363 
    364 	/* client name (hostname) */
    365 	m  = xdr_string_encode(hostname, hostnamelen);
    366 	if (m == NULL)
    367 		return (ENOMEM);
    368 
    369 	/* key name (root or swap) */
    370 	m->m_next = xdr_string_encode(key, strlen(key));
    371 	if (m->m_next == NULL)
    372 		return (ENOMEM);
    373 
    374 	/* RPC: bootparam/getfile */
    375 	error = krpc_call(bpsin, BOOTPARAM_PROG, BOOTPARAM_VERS,
    376 	                  BOOTPARAM_GETFILE, &m, NULL);
    377 	if (error)
    378 		return error;
    379 
    380 	/*
    381 	 * Parse result message.
    382 	 */
    383 
    384 	/* server name */
    385 	serv_name = &ndm->ndm_host[0];
    386 	sn_len = sizeof(ndm->ndm_host) - 1;
    387 	m = xdr_string_decode(m, serv_name, &sn_len);
    388 	if (m == NULL)
    389 		goto bad;
    390 
    391 	/* server IP address (mountd/NFS) */
    392 	m = xdr_inaddr_decode(m, &inaddr);
    393 	if (m == NULL)
    394 		goto bad;
    395 
    396 	/* server pathname */
    397 	path_len = sizeof(pathname) - 1;
    398 	m = xdr_string_decode(m, pathname, &path_len);
    399 	if (m == NULL)
    400 		goto bad;
    401 
    402 	/*
    403 	 * Store the results in the nfs_dlmount.
    404 	 * The strings become "server:pathname"
    405 	 */
    406 	sin = (struct sockaddr_in *) &ndm->ndm_saddr;
    407 	bzero((caddr_t)sin, sizeof(*sin));
    408 	sin->sin_len = sizeof(*sin);
    409 	sin->sin_family = AF_INET;
    410 	sin->sin_addr = inaddr;
    411 	if ((sn_len + 1 + path_len + 1) > sizeof(ndm->ndm_host)) {
    412 		printf("nfs_boot: getfile name too long\n");
    413 		error = EIO;
    414 		goto out;
    415 	}
    416 	ndm->ndm_host[sn_len] = ':';
    417 	bcopy(pathname, ndm->ndm_host + sn_len + 1, path_len + 1);
    418 
    419 	/* success */
    420 	goto out;
    421 
    422 bad:
    423 	printf("nfs_boot: bootparam_getfile: bad reply\n");
    424 	error = EBADRPC;
    425 
    426 out:
    427 	m_freem(m);
    428 	return(0);
    429 }
    430 
    431 
    432