Home | History | Annotate | Line # | Download | only in nfs
nfs_bootparam.c revision 1.5
      1  1.5  drochner /*	$NetBSD: nfs_bootparam.c,v 1.5 1998/01/09 15:16:55 drochner 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  * 3. All advertising materials mentioning features or use of this software
     19  1.1       gwr  *    must display the following acknowledgement:
     20  1.1       gwr  *        This product includes software developed by the NetBSD
     21  1.1       gwr  *        Foundation, Inc. and its contributors.
     22  1.1       gwr  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1       gwr  *    contributors may be used to endorse or promote products derived
     24  1.1       gwr  *    from this software without specific prior written permission.
     25  1.1       gwr  *
     26  1.1       gwr  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1       gwr  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1       gwr  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1       gwr  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1       gwr  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1       gwr  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1       gwr  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1       gwr  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1       gwr  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1       gwr  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1       gwr  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1       gwr  */
     38  1.1       gwr 
     39  1.1       gwr /*
     40  1.1       gwr  * Support for NFS diskless booting, Sun-style (RPC/bootparams)
     41  1.1       gwr  */
     42  1.1       gwr 
     43  1.1       gwr #include <sys/param.h>
     44  1.1       gwr #include <sys/systm.h>
     45  1.1       gwr #include <sys/kernel.h>
     46  1.1       gwr #include <sys/conf.h>
     47  1.1       gwr #include <sys/device.h>
     48  1.1       gwr #include <sys/ioctl.h>
     49  1.1       gwr #include <sys/proc.h>
     50  1.1       gwr #include <sys/mount.h>
     51  1.1       gwr #include <sys/mbuf.h>
     52  1.1       gwr #include <sys/reboot.h>
     53  1.1       gwr #include <sys/socket.h>
     54  1.1       gwr #include <sys/socketvar.h>
     55  1.1       gwr 
     56  1.1       gwr #include <net/if.h>
     57  1.5  drochner #include <net/if_types.h>
     58  1.1       gwr #include <net/route.h>
     59  1.1       gwr #include <net/if_ether.h>
     60  1.1       gwr 
     61  1.1       gwr #include <netinet/in.h>
     62  1.1       gwr #include <netinet/if_inarp.h>
     63  1.1       gwr 
     64  1.1       gwr #include <nfs/rpcv2.h>
     65  1.1       gwr #include <nfs/krpc.h>
     66  1.1       gwr #include <nfs/xdr_subs.h>
     67  1.1       gwr 
     68  1.1       gwr #include <nfs/nfsproto.h>
     69  1.1       gwr #include <nfs/nfsdiskless.h>
     70  1.1       gwr 
     71  1.5  drochner #include "arp.h"
     72  1.5  drochner 
     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.1       gwr static int bp_whoami __P((struct sockaddr_in *bpsin,
     90  1.1       gwr 	struct in_addr *my_ip, struct in_addr *gw_ip));
     91  1.1       gwr static int bp_getfile __P((struct sockaddr_in *bpsin, char *key,
     92  1.1       gwr 	struct nfs_dlmount *ndm));
     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.1       gwr nfs_bootparam(ifp, nd, procp)
    107  1.1       gwr 	struct ifnet *ifp;
    108  1.1       gwr 	struct nfs_diskless *nd;
    109  1.1       gwr 	struct proc *procp;
    110  1.1       gwr {
    111  1.1       gwr 	struct ifreq ireq;
    112  1.2       gwr 	struct in_addr my_ip, gw_ip;
    113  1.1       gwr 	struct sockaddr_in bp_sin;
    114  1.1       gwr 	struct sockaddr_in *sin;
    115  1.1       gwr 	struct socket *so;
    116  1.2       gwr 	struct nfs_dlmount *gw_ndm;
    117  1.4       gwr #if 0	/* XXX - not yet */
    118  1.2       gwr 	char *p;
    119  1.3       gwr 	u_int32_t mask;
    120  1.4       gwr #endif	/* XXX */
    121  1.1       gwr 	int error;
    122  1.1       gwr 
    123  1.2       gwr 	gw_ndm = 0;
    124  1.1       gwr 	bzero(&ireq, sizeof(ireq));
    125  1.1       gwr 	bcopy(ifp->if_xname, ireq.ifr_name, IFNAMSIZ);
    126  1.1       gwr 
    127  1.1       gwr 	/*
    128  1.1       gwr 	 * Get a socket to use for various things in here.
    129  1.1       gwr 	 * After this, use "goto out" to cleanup and return.
    130  1.1       gwr 	 */
    131  1.1       gwr 	error = socreate(AF_INET, &so, SOCK_DGRAM, 0);
    132  1.1       gwr 	if (error) {
    133  1.1       gwr 		printf("nfs_boot: socreate, error=%d\n", error);
    134  1.1       gwr 		return (error);
    135  1.1       gwr 	}
    136  1.1       gwr 
    137  1.1       gwr 	/*
    138  1.1       gwr 	 * Bring up the interface. (just set the "up" flag)
    139  1.1       gwr 	 * Get the old interface flags and or IFF_UP into them so
    140  1.1       gwr 	 * things like media selection flags are not clobbered.
    141  1.1       gwr 	 */
    142  1.1       gwr 	error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)&ireq, procp);
    143  1.1       gwr 	if (error) {
    144  1.1       gwr 		printf("nfs_boot: GIFFLAGS, error=%d\n", error);
    145  1.1       gwr 		goto out;
    146  1.1       gwr 	}
    147  1.1       gwr 	ireq.ifr_flags |= IFF_UP;
    148  1.1       gwr 	error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)&ireq, procp);
    149  1.1       gwr 	if (error) {
    150  1.1       gwr 		printf("nfs_boot: SIFFLAGS, error=%d\n", error);
    151  1.1       gwr 		goto out;
    152  1.1       gwr 	}
    153  1.1       gwr 
    154  1.5  drochner 	error = EADDRNOTAVAIL; /* ??? */
    155  1.5  drochner #if NARP > 0
    156  1.5  drochner 	if (ifp->if_type == IFT_ETHER || ifp->if_type == IFT_FDDI) {
    157  1.5  drochner 		/*
    158  1.5  drochner 		 * Do RARP for the interface address.
    159  1.5  drochner 		 */
    160  1.5  drochner 		error = revarpwhoami(&my_ip, ifp);
    161  1.5  drochner 		if (!error)
    162  1.5  drochner 			goto ok;
    163  1.1       gwr 		printf("revarp failed, error=%d\n", error);
    164  1.1       gwr 	}
    165  1.5  drochner #endif
    166  1.5  drochner 	if (0) goto ok; /* XXX stupid gcc */
    167  1.5  drochner 	goto out;
    168  1.5  drochner 
    169  1.5  drochner ok:
    170  1.1       gwr 	nd->nd_myip.s_addr = my_ip.s_addr;
    171  1.1       gwr 	printf("nfs_boot: client_addr=0x%x\n",
    172  1.1       gwr 	       (u_int32_t)ntohl(my_ip.s_addr));
    173  1.1       gwr 
    174  1.1       gwr 	/*
    175  1.1       gwr 	 * Do enough of ifconfig(8) so that the chosen interface
    176  1.1       gwr 	 * can talk to the servers.  (just set the address)
    177  1.1       gwr 	 */
    178  1.1       gwr 	sin = (struct sockaddr_in *)&ireq.ifr_addr;
    179  1.1       gwr 	sin->sin_len = sizeof(*sin);
    180  1.1       gwr 	sin->sin_family = AF_INET;
    181  1.2       gwr 	sin->sin_addr = my_ip;
    182  1.1       gwr 	error = ifioctl(so, SIOCSIFADDR, (caddr_t)&ireq, procp);
    183  1.1       gwr 	if (error) {
    184  1.1       gwr 		printf("nfs_boot: set ifaddr, error=%d\n", error);
    185  1.1       gwr 		goto out;
    186  1.1       gwr 	}
    187  1.1       gwr 
    188  1.1       gwr 	/*
    189  1.1       gwr 	 * Get client name and gateway address.
    190  1.1       gwr 	 * RPC: bootparam/whoami
    191  1.1       gwr 	 * Use the old broadcast address for the WHOAMI
    192  1.1       gwr 	 * call because we do not yet know our netmask.
    193  1.1       gwr 	 * The server address returned by the WHOAMI call
    194  1.1       gwr 	 * is used for all subsequent booptaram RPCs.
    195  1.1       gwr 	 */
    196  1.1       gwr 	sin = &bp_sin;
    197  1.1       gwr 	bzero((caddr_t)sin, sizeof(*sin));
    198  1.1       gwr 	sin->sin_len = sizeof(*sin);
    199  1.1       gwr 	sin->sin_family = AF_INET;
    200  1.1       gwr 	sin->sin_addr.s_addr = INADDR_BROADCAST;
    201  1.1       gwr 
    202  1.1       gwr 	/* Do the RPC/bootparam/whoami. */
    203  1.2       gwr 	error = bp_whoami(sin, &my_ip, &gw_ip);
    204  1.1       gwr 	if (error) {
    205  1.1       gwr 		printf("nfs_boot: bootparam whoami, error=%d\n", error);
    206  1.1       gwr 		goto out;
    207  1.1       gwr 	}
    208  1.1       gwr 	printf("nfs_boot: server_addr=0x%x\n",
    209  1.1       gwr 		   (u_int32_t)ntohl(sin->sin_addr.s_addr));
    210  1.1       gwr 	printf("nfs_boot: hostname=%s\n", hostname);
    211  1.1       gwr 
    212  1.1       gwr 	/*
    213  1.1       gwr 	 * Now fetch the server:pathname strings and server IP
    214  1.1       gwr 	 * for root and swap.  Missing swap is not fatal.
    215  1.1       gwr 	 */
    216  1.1       gwr 	error = bp_getfile(sin, "root", &nd->nd_root);
    217  1.1       gwr 	if (error) {
    218  1.1       gwr 		printf("nfs_boot: bootparam get root: %d\n", error);
    219  1.1       gwr 		goto out;
    220  1.1       gwr 	}
    221  1.1       gwr #if 0
    222  1.1       gwr 	error = bp_getfile(sin, "swap", &nd->nd_swap);
    223  1.1       gwr 	if (error) {
    224  1.1       gwr 		printf("nfs_boot: bootparam get swap: %d\n", error);
    225  1.1       gwr 		error = 0;
    226  1.1       gwr 	}
    227  1.1       gwr #endif
    228  1.1       gwr 
    229  1.2       gwr #ifdef	NFS_BOOT_GATEWAY
    230  1.1       gwr 	/*
    231  1.2       gwr 	 * Note: we normally ignore the gateway address returned
    232  1.2       gwr 	 * by the "bootparam/whoami" RPC above, because many old
    233  1.2       gwr 	 * bootparam servers supply a bogus gateway value.
    234  1.2       gwr 	 *
    235  1.2       gwr 	 * These deficiencies in the bootparam RPC interface are
    236  1.2       gwr 	 * circumvented by using the bootparam/getfile RPC.  The
    237  1.2       gwr 	 * parameter "gateway" is requested, and if its returned,
    238  1.2       gwr 	 * we use the "server" part of the reply as the gateway,
    239  1.2       gwr 	 * and use the "pathname" part of the reply as the mask.
    240  1.4       gwr 	 * (The mask comes to us as a string.)
    241  1.1       gwr 	 */
    242  1.2       gwr 	if (gw_ip.s_addr) {
    243  1.2       gwr 		/* Our caller will add the route. */
    244  1.2       gwr 		nd->nd_gwip = gw_ip;
    245  1.2       gwr 	}
    246  1.2       gwr #endif
    247  1.4       gwr #if 0	/* XXX - not yet */
    248  1.2       gwr 	gw_ndm = malloc(sizeof(*gw_ndm), M_NFSMNT, M_WAITOK);
    249  1.2       gwr 	bzero((caddr_t)gw_ndm, sizeof(*gw_ndm));
    250  1.2       gwr 	error = bp_getfile(sin, "gateway", gw_ndm);
    251  1.2       gwr 	if (error) {
    252  1.2       gwr 		/* Ignore the error.  No gateway supplied. */
    253  1.2       gwr 		error = 0;
    254  1.2       gwr 		goto out;
    255  1.2       gwr 	}
    256  1.2       gwr 	printf("nfs_boot: gateway=%s\n", gw_ndm->ndm_host);
    257  1.2       gwr 	sin = (struct sockaddr_in *) &gw_ndm->ndm_saddr;
    258  1.2       gwr 	nd->nd_gwip = sin->sin_addr;
    259  1.2       gwr 	/* Find the pathname part of the "mounted-on" string. */
    260  1.2       gwr 	p = strchr(gw_ndm->ndm_host, ':');
    261  1.2       gwr 	if (p == 0)
    262  1.2       gwr 		goto out;
    263  1.2       gwr 	/* have pathname */
    264  1.3       gwr 	p++;	/* skip ':' */
    265  1.4       gwr 	mask = inet_addr(p);	/* libkern */
    266  1.4       gwr 	if (mask == 0) {
    267  1.4       gwr 		printf("nfs_boot: gateway netmask missing\n");
    268  1.2       gwr 		goto out;
    269  1.4       gwr 	}
    270  1.2       gwr 
    271  1.2       gwr 	/* Save our netmask and update the network interface. */
    272  1.3       gwr 	nd->nd_mask.s_addr = mask;
    273  1.2       gwr 	sin = (struct sockaddr_in *)&ireq.ifr_addr;
    274  1.2       gwr 	sin->sin_len = sizeof(*sin);
    275  1.2       gwr 	sin->sin_family = AF_INET;
    276  1.2       gwr 	sin->sin_addr = nd->nd_mask;
    277  1.2       gwr 	error = ifioctl(so, SIOCSIFNETMASK, (caddr_t)&ireq, procp);
    278  1.2       gwr 	if (error) {
    279  1.2       gwr 		printf("nfs_boot: set ifmask, error=%d\n", error);
    280  1.2       gwr 		error = 0;	/* ignore it */
    281  1.2       gwr 	}
    282  1.4       gwr #endif	/* XXX */
    283  1.1       gwr 
    284  1.1       gwr  out:
    285  1.2       gwr 	if (gw_ndm)
    286  1.2       gwr 		free(gw_ndm, M_NFSMNT);
    287  1.1       gwr 	soclose(so);
    288  1.1       gwr 	return (error);
    289  1.1       gwr }
    290  1.1       gwr 
    291  1.1       gwr 
    292  1.1       gwr /*
    293  1.1       gwr  * RPC: bootparam/whoami
    294  1.1       gwr  * Given client IP address, get:
    295  1.1       gwr  *	client name	(hostname)
    296  1.1       gwr  *	domain name (domainname)
    297  1.1       gwr  *	gateway address
    298  1.1       gwr  *
    299  1.1       gwr  * The hostname and domainname are set here for convenience.
    300  1.1       gwr  *
    301  1.1       gwr  * Note - bpsin is initialized to the broadcast address,
    302  1.1       gwr  * and will be replaced with the bootparam server address
    303  1.1       gwr  * after this call is complete.  Have to use PMAP_PROC_CALL
    304  1.1       gwr  * to make sure we get responses only from a servers that
    305  1.1       gwr  * know about us (don't want to broadcast a getport call).
    306  1.1       gwr  */
    307  1.1       gwr static int
    308  1.1       gwr bp_whoami(bpsin, my_ip, gw_ip)
    309  1.1       gwr 	struct sockaddr_in *bpsin;
    310  1.1       gwr 	struct in_addr *my_ip;
    311  1.1       gwr 	struct in_addr *gw_ip;
    312  1.1       gwr {
    313  1.1       gwr 	/* RPC structures for PMAPPROC_CALLIT */
    314  1.1       gwr 	struct whoami_call {
    315  1.1       gwr 		u_int32_t call_prog;
    316  1.1       gwr 		u_int32_t call_vers;
    317  1.1       gwr 		u_int32_t call_proc;
    318  1.1       gwr 		u_int32_t call_arglen;
    319  1.1       gwr 	} *call;
    320  1.1       gwr 	struct callit_reply {
    321  1.1       gwr 		u_int32_t port;
    322  1.1       gwr 		u_int32_t encap_len;
    323  1.1       gwr 		/* encapsulated data here */
    324  1.1       gwr 	} *reply;
    325  1.1       gwr 
    326  1.1       gwr 	struct mbuf *m, *from;
    327  1.1       gwr 	struct sockaddr_in *sin;
    328  1.1       gwr 	int error, msg_len;
    329  1.1       gwr 	int16_t port;
    330  1.1       gwr 
    331  1.1       gwr 	/*
    332  1.1       gwr 	 * Build request message for PMAPPROC_CALLIT.
    333  1.1       gwr 	 */
    334  1.1       gwr 	m = m_get(M_WAIT, MT_DATA);
    335  1.1       gwr 	call = mtod(m, struct whoami_call *);
    336  1.1       gwr 	m->m_len = sizeof(*call);
    337  1.1       gwr 	call->call_prog = txdr_unsigned(BOOTPARAM_PROG);
    338  1.1       gwr 	call->call_vers = txdr_unsigned(BOOTPARAM_VERS);
    339  1.1       gwr 	call->call_proc = txdr_unsigned(BOOTPARAM_WHOAMI);
    340  1.1       gwr 
    341  1.1       gwr 	/*
    342  1.1       gwr 	 * append encapsulated data (client IP address)
    343  1.1       gwr 	 */
    344  1.1       gwr 	m->m_next = xdr_inaddr_encode(my_ip);
    345  1.1       gwr 	call->call_arglen = txdr_unsigned(m->m_next->m_len);
    346  1.1       gwr 
    347  1.1       gwr 	/* RPC: portmap/callit */
    348  1.1       gwr 	bpsin->sin_port = htons(PMAPPORT);
    349  1.1       gwr 	from = NULL;
    350  1.1       gwr 	error = krpc_call(bpsin, PMAPPROG, PMAPVERS,
    351  1.1       gwr 			PMAPPROC_CALLIT, &m, &from);
    352  1.1       gwr 	if (error)
    353  1.1       gwr 		return error;
    354  1.1       gwr 
    355  1.1       gwr 	/*
    356  1.1       gwr 	 * Parse result message.
    357  1.1       gwr 	 */
    358  1.1       gwr 	if (m->m_len < sizeof(*reply)) {
    359  1.1       gwr 		m = m_pullup(m, sizeof(*reply));
    360  1.1       gwr 		if (m == NULL)
    361  1.1       gwr 			goto bad;
    362  1.1       gwr 	}
    363  1.1       gwr 	reply = mtod(m, struct callit_reply *);
    364  1.1       gwr 	port = fxdr_unsigned(u_int32_t, reply->port);
    365  1.1       gwr 	msg_len = fxdr_unsigned(u_int32_t, reply->encap_len);
    366  1.1       gwr 	m_adj(m, sizeof(*reply));
    367  1.1       gwr 
    368  1.1       gwr 	/*
    369  1.1       gwr 	 * Save bootparam server address
    370  1.1       gwr 	 */
    371  1.1       gwr 	sin = mtod(from, struct sockaddr_in *);
    372  1.1       gwr 	bpsin->sin_port = htons(port);
    373  1.1       gwr 	bpsin->sin_addr.s_addr = sin->sin_addr.s_addr;
    374  1.1       gwr 
    375  1.1       gwr 	/* client name */
    376  1.1       gwr 	hostnamelen = MAXHOSTNAMELEN-1;
    377  1.1       gwr 	m = xdr_string_decode(m, hostname, &hostnamelen);
    378  1.1       gwr 	if (m == NULL)
    379  1.1       gwr 		goto bad;
    380  1.1       gwr 
    381  1.1       gwr 	/* domain name */
    382  1.1       gwr 	domainnamelen = MAXHOSTNAMELEN-1;
    383  1.1       gwr 	m = xdr_string_decode(m, domainname, &domainnamelen);
    384  1.1       gwr 	if (m == NULL)
    385  1.1       gwr 		goto bad;
    386  1.1       gwr 
    387  1.1       gwr 	/* gateway address */
    388  1.1       gwr 	m = xdr_inaddr_decode(m, gw_ip);
    389  1.1       gwr 	if (m == NULL)
    390  1.1       gwr 		goto bad;
    391  1.1       gwr 
    392  1.1       gwr 	/* success */
    393  1.1       gwr 	goto out;
    394  1.1       gwr 
    395  1.1       gwr bad:
    396  1.1       gwr 	printf("nfs_boot: bootparam_whoami: bad reply\n");
    397  1.1       gwr 	error = EBADRPC;
    398  1.1       gwr 
    399  1.1       gwr out:
    400  1.1       gwr 	if (from)
    401  1.1       gwr 		m_freem(from);
    402  1.1       gwr 	if (m)
    403  1.1       gwr 		m_freem(m);
    404  1.1       gwr 	return(error);
    405  1.1       gwr }
    406  1.1       gwr 
    407  1.1       gwr 
    408  1.1       gwr /*
    409  1.1       gwr  * RPC: bootparam/getfile
    410  1.1       gwr  * Given client name and file "key", get:
    411  1.1       gwr  *	server name
    412  1.1       gwr  *	server IP address
    413  1.1       gwr  *	server pathname
    414  1.1       gwr  */
    415  1.1       gwr static int
    416  1.1       gwr bp_getfile(bpsin, key, ndm)
    417  1.1       gwr 	struct sockaddr_in *bpsin;
    418  1.1       gwr 	char *key;
    419  1.1       gwr 	struct nfs_dlmount *ndm;
    420  1.1       gwr {
    421  1.1       gwr 	char pathname[MNAMELEN];
    422  1.1       gwr 	struct in_addr inaddr;
    423  1.1       gwr 	struct sockaddr_in *sin;
    424  1.1       gwr 	struct mbuf *m;
    425  1.1       gwr 	char *serv_name;
    426  1.1       gwr 	int error, sn_len, path_len;
    427  1.1       gwr 
    428  1.1       gwr 	/*
    429  1.1       gwr 	 * Build request message.
    430  1.1       gwr 	 */
    431  1.1       gwr 
    432  1.1       gwr 	/* client name (hostname) */
    433  1.1       gwr 	m  = xdr_string_encode(hostname, hostnamelen);
    434  1.1       gwr 	if (m == NULL)
    435  1.1       gwr 		return (ENOMEM);
    436  1.1       gwr 
    437  1.1       gwr 	/* key name (root or swap) */
    438  1.1       gwr 	m->m_next = xdr_string_encode(key, strlen(key));
    439  1.1       gwr 	if (m->m_next == NULL)
    440  1.1       gwr 		return (ENOMEM);
    441  1.1       gwr 
    442  1.1       gwr 	/* RPC: bootparam/getfile */
    443  1.1       gwr 	error = krpc_call(bpsin, BOOTPARAM_PROG, BOOTPARAM_VERS,
    444  1.1       gwr 	                  BOOTPARAM_GETFILE, &m, NULL);
    445  1.1       gwr 	if (error)
    446  1.1       gwr 		return error;
    447  1.1       gwr 
    448  1.1       gwr 	/*
    449  1.1       gwr 	 * Parse result message.
    450  1.1       gwr 	 */
    451  1.1       gwr 
    452  1.1       gwr 	/* server name */
    453  1.1       gwr 	serv_name = &ndm->ndm_host[0];
    454  1.1       gwr 	sn_len = sizeof(ndm->ndm_host) - 1;
    455  1.1       gwr 	m = xdr_string_decode(m, serv_name, &sn_len);
    456  1.1       gwr 	if (m == NULL)
    457  1.1       gwr 		goto bad;
    458  1.1       gwr 
    459  1.1       gwr 	/* server IP address (mountd/NFS) */
    460  1.1       gwr 	m = xdr_inaddr_decode(m, &inaddr);
    461  1.1       gwr 	if (m == NULL)
    462  1.1       gwr 		goto bad;
    463  1.1       gwr 
    464  1.1       gwr 	/* server pathname */
    465  1.1       gwr 	path_len = sizeof(pathname) - 1;
    466  1.1       gwr 	m = xdr_string_decode(m, pathname, &path_len);
    467  1.1       gwr 	if (m == NULL)
    468  1.1       gwr 		goto bad;
    469  1.1       gwr 
    470  1.1       gwr 	/*
    471  1.1       gwr 	 * Store the results in the nfs_dlmount.
    472  1.1       gwr 	 * The strings become "server:pathname"
    473  1.1       gwr 	 */
    474  1.1       gwr 	sin = (struct sockaddr_in *) &ndm->ndm_saddr;
    475  1.1       gwr 	bzero((caddr_t)sin, sizeof(*sin));
    476  1.1       gwr 	sin->sin_len = sizeof(*sin);
    477  1.1       gwr 	sin->sin_family = AF_INET;
    478  1.1       gwr 	sin->sin_addr = inaddr;
    479  1.1       gwr 	if ((sn_len + 1 + path_len + 1) > sizeof(ndm->ndm_host)) {
    480  1.1       gwr 		printf("nfs_boot: getfile name too long\n");
    481  1.1       gwr 		error = EIO;
    482  1.1       gwr 		goto out;
    483  1.1       gwr 	}
    484  1.1       gwr 	ndm->ndm_host[sn_len] = ':';
    485  1.1       gwr 	bcopy(pathname, ndm->ndm_host + sn_len + 1, path_len + 1);
    486  1.1       gwr 
    487  1.1       gwr 	/* success */
    488  1.1       gwr 	goto out;
    489  1.1       gwr 
    490  1.1       gwr bad:
    491  1.1       gwr 	printf("nfs_boot: bootparam_getfile: bad reply\n");
    492  1.1       gwr 	error = EBADRPC;
    493  1.1       gwr 
    494  1.1       gwr out:
    495  1.1       gwr 	m_freem(m);
    496  1.1       gwr 	return(0);
    497  1.1       gwr }
    498  1.1       gwr 
    499  1.1       gwr 
    500