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