Home | History | Annotate | Line # | Download | only in nfs
nfs_boot.c revision 1.25
      1  1.25      fvdl /*	$NetBSD: nfs_boot.c,v 1.25 1996/02/18 11:53:41 fvdl Exp $	*/
      2   1.5       cgd 
      3   1.1     glass /*
      4  1.16       gwr  * Copyright (c) 1995 Adam Glass, Gordon Ross
      5   1.1     glass  * All rights reserved.
      6   1.1     glass  *
      7   1.1     glass  * Redistribution and use in source and binary forms, with or without
      8   1.1     glass  * modification, are permitted provided that the following conditions
      9   1.1     glass  * are met:
     10   1.1     glass  * 1. Redistributions of source code must retain the above copyright
     11   1.1     glass  *    notice, this list of conditions and the following disclaimer.
     12   1.1     glass  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1     glass  *    notice, this list of conditions and the following disclaimer in the
     14   1.1     glass  *    documentation and/or other materials provided with the distribution.
     15   1.3       gwr  * 3. The name of the authors may not be used to endorse or promote products
     16   1.1     glass  *    derived from this software without specific prior written permission.
     17   1.1     glass  *
     18   1.3       gwr  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
     19   1.3       gwr  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20   1.3       gwr  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21   1.3       gwr  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     22   1.3       gwr  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23   1.3       gwr  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24   1.3       gwr  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25   1.3       gwr  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26   1.3       gwr  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27   1.3       gwr  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28   1.1     glass  */
     29   1.1     glass 
     30   1.2       cgd #include <sys/param.h>
     31   1.1     glass #include <sys/systm.h>
     32   1.1     glass #include <sys/kernel.h>
     33   1.1     glass #include <sys/conf.h>
     34   1.1     glass #include <sys/ioctl.h>
     35   1.1     glass #include <sys/proc.h>
     36   1.1     glass #include <sys/mount.h>
     37   1.1     glass #include <sys/mbuf.h>
     38  1.16       gwr #include <sys/reboot.h>
     39   1.1     glass #include <sys/socket.h>
     40  1.16       gwr #include <sys/socketvar.h>
     41   1.1     glass 
     42   1.1     glass #include <net/if.h>
     43   1.3       gwr #include <net/route.h>
     44   1.3       gwr 
     45   1.1     glass #include <netinet/in.h>
     46   1.3       gwr #include <netinet/if_ether.h>
     47   1.1     glass 
     48   1.1     glass #include <nfs/rpcv2.h>
     49  1.25      fvdl #include <nfs/nfsproto.h>
     50   1.1     glass #include <nfs/nfs.h>
     51   1.1     glass #include <nfs/nfsdiskless.h>
     52  1.11       gwr #include <nfs/krpc.h>
     53  1.17   mycroft #include <nfs/xdr_subs.h>
     54  1.21  christos #include <nfs/nfs_var.h>
     55   1.1     glass 
     56   1.7    paulus #include "ether.h"
     57  1.11       gwr #if NETHER == 0
     58  1.11       gwr 
     59  1.11       gwr int nfs_boot_init(nd, procp)
     60  1.11       gwr 	struct nfs_diskless *nd;
     61  1.11       gwr 	struct proc *procp;
     62  1.11       gwr {
     63  1.11       gwr 	panic("nfs_boot_init: no ether");
     64  1.24       gwr }
     65  1.24       gwr 
     66  1.24       gwr void
     67  1.24       gwr nfs_boot_getfh(bpsin, key, ndmntp)
     68  1.24       gwr 	struct sockaddr_in *bpsin;
     69  1.24       gwr 	char *key;
     70  1.24       gwr 	struct nfs_dlmount *ndmntp;
     71  1.24       gwr {
     72  1.24       gwr 	/* can not get here */
     73  1.11       gwr }
     74  1.11       gwr 
     75  1.11       gwr #else /* NETHER */
     76   1.7    paulus 
     77   1.1     glass /*
     78   1.1     glass  * Support for NFS diskless booting, specifically getting information
     79   1.1     glass  * about where to boot from, what pathnames, etc.
     80   1.1     glass  *
     81   1.3       gwr  * This implememtation uses RARP and the bootparam RPC.
     82   1.3       gwr  * We are forced to implement RPC anyway (to get file handles)
     83   1.3       gwr  * so we might as well take advantage of it for bootparam too.
     84   1.1     glass  *
     85   1.3       gwr  * The diskless boot sequence goes as follows:
     86  1.15       gwr  * (1) Use RARP to get our interface address
     87  1.15       gwr  * (2) Use RPC/bootparam/whoami to get our hostname,
     88  1.15       gwr  *     our IP address, and the server's IP address.
     89  1.15       gwr  * (3) Use RPC/bootparam/getfile to get the root path
     90  1.15       gwr  * (4) Use RPC/mountd to get the root file handle
     91  1.15       gwr  * (5) Use RPC/bootparam/getfile to get the swap path
     92  1.15       gwr  * (6) Use RPC/mountd to get the swap file handle
     93   1.1     glass  *
     94   1.3       gwr  * (This happens to be the way Sun does it too.)
     95   1.1     glass  */
     96   1.1     glass 
     97   1.3       gwr /* bootparam RPC */
     98   1.6   deraadt static int bp_whoami __P((struct sockaddr_in *bpsin,
     99   1.6   deraadt 	struct in_addr *my_ip, struct in_addr *gw_ip));
    100   1.6   deraadt static int bp_getfile __P((struct sockaddr_in *bpsin, char *key,
    101   1.6   deraadt 	struct sockaddr_in *mdsin, char *servname, char *path));
    102   1.3       gwr 
    103   1.3       gwr /* mountd RPC */
    104   1.6   deraadt static int md_mount __P((struct sockaddr_in *mdsin, char *path,
    105   1.6   deraadt 	u_char *fh));
    106   1.3       gwr 
    107  1.13        pk char	*nfsbootdevname;
    108  1.13        pk 
    109   1.1     glass /*
    110   1.3       gwr  * Called with an empty nfs_diskless struct to be filled in.
    111   1.1     glass  */
    112   1.6   deraadt int
    113   1.6   deraadt nfs_boot_init(nd, procp)
    114   1.3       gwr 	struct nfs_diskless *nd;
    115   1.3       gwr 	struct proc *procp;
    116   1.1     glass {
    117   1.3       gwr 	struct ifreq ireq;
    118  1.11       gwr 	struct in_addr my_ip, gw_ip;
    119   1.3       gwr 	struct sockaddr_in bp_sin;
    120   1.1     glass 	struct sockaddr_in *sin;
    121   1.3       gwr 	struct ifnet *ifp;
    122   1.3       gwr 	struct socket *so;
    123  1.16       gwr 	int error;
    124   1.3       gwr 
    125   1.3       gwr 	/*
    126   1.3       gwr 	 * Find an interface, rarp for its ip address, stuff it, the
    127   1.3       gwr 	 * implied broadcast addr, and netmask into a nfs_diskless struct.
    128   1.3       gwr 	 *
    129   1.3       gwr 	 * This was moved here from nfs_vfsops.c because this procedure
    130   1.3       gwr 	 * would be quite different if someone decides to write (i.e.) a
    131  1.15       gwr 	 * BOOTP version of this file (might not use RARP, etc.)
    132   1.3       gwr 	 */
    133   1.3       gwr 
    134   1.3       gwr 	/*
    135   1.3       gwr 	 * Find a network interface.
    136   1.3       gwr 	 */
    137  1.13        pk 	if (nfsbootdevname)
    138  1.13        pk 		ifp = ifunit(nfsbootdevname);
    139  1.13        pk 	else
    140  1.19   mycroft 		for (ifp = ifnet.tqh_first; ifp != 0; ifp = ifp->if_list.tqe_next)
    141  1.13        pk 			if ((ifp->if_flags &
    142  1.13        pk 			     (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
    143  1.13        pk 				break;
    144   1.3       gwr 	if (ifp == NULL)
    145   1.3       gwr 		panic("nfs_boot: no suitable interface");
    146   1.3       gwr 	sprintf(ireq.ifr_name, "%s%d", ifp->if_name, ifp->if_unit);
    147   1.3       gwr 	printf("nfs_boot: using network interface '%s'\n",
    148   1.6   deraadt 	    ireq.ifr_name);
    149   1.3       gwr 
    150   1.3       gwr 	/*
    151   1.3       gwr 	 * Bring up the interface.
    152  1.18       cgd 	 *
    153  1.18       cgd 	 * Get the old interface flags and or IFF_UP into them; if
    154  1.18       cgd 	 * IFF_UP set blindly, interface selection can be clobbered.
    155   1.3       gwr 	 */
    156   1.3       gwr 	if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0)) != 0)
    157   1.3       gwr 		panic("nfs_boot: socreate, error=%d", error);
    158  1.18       cgd 	error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)&ireq, procp);
    159  1.18       cgd 	if (error)
    160  1.18       cgd 		panic("nfs_boot: GIFFLAGS, error=%d", error);
    161  1.18       cgd 	ireq.ifr_flags |= IFF_UP;
    162   1.3       gwr 	error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)&ireq, procp);
    163   1.6   deraadt 	if (error)
    164   1.6   deraadt 		panic("nfs_boot: SIFFLAGS, error=%d", error);
    165   1.3       gwr 
    166   1.3       gwr 	/*
    167  1.15       gwr 	 * Do RARP for the interface address.
    168   1.3       gwr 	 */
    169  1.11       gwr 	if ((error = revarpwhoami(&my_ip, ifp)) != 0)
    170   1.3       gwr 		panic("revarp failed, error=%d", error);
    171  1.25      fvdl 	printf("nfs_boot: client_addr=0x%x\n", (u_int32_t)ntohl(my_ip.s_addr));
    172   1.3       gwr 
    173   1.3       gwr 	/*
    174  1.15       gwr 	 * Do enough of ifconfig(8) so that the chosen interface
    175  1.15       gwr 	 * can talk to the servers.  (just set the address)
    176   1.3       gwr 	 */
    177   1.3       gwr 	sin = (struct sockaddr_in *)&ireq.ifr_addr;
    178  1.10   mycroft 	bzero((caddr_t)sin, sizeof(*sin));
    179   1.3       gwr 	sin->sin_len = sizeof(*sin);
    180   1.3       gwr 	sin->sin_family = AF_INET;
    181   1.3       gwr 	sin->sin_addr.s_addr = my_ip.s_addr;
    182   1.3       gwr 	error = ifioctl(so, SIOCSIFADDR, (caddr_t)&ireq, procp);
    183   1.6   deraadt 	if (error)
    184   1.6   deraadt 		panic("nfs_boot: set if addr, error=%d", error);
    185   1.3       gwr 
    186   1.3       gwr 	soclose(so);
    187   1.3       gwr 
    188   1.3       gwr 	/*
    189   1.3       gwr 	 * Get client name and gateway address.
    190   1.3       gwr 	 * RPC: bootparam/whoami
    191  1.15       gwr 	 * Use the old broadcast address for the WHOAMI
    192  1.15       gwr 	 * call because we do not yet know our netmask.
    193  1.15       gwr 	 * The server address returned by the WHOAMI call
    194  1.15       gwr 	 * is used for all subsequent booptaram RPCs.
    195   1.3       gwr 	 */
    196  1.10   mycroft 	bzero((caddr_t)&bp_sin, sizeof(bp_sin));
    197   1.3       gwr 	bp_sin.sin_len = sizeof(bp_sin);
    198   1.3       gwr 	bp_sin.sin_family = AF_INET;
    199  1.15       gwr 	bp_sin.sin_addr.s_addr = INADDR_BROADCAST;
    200   1.3       gwr 	hostnamelen = MAXHOSTNAMELEN;
    201   1.6   deraadt 
    202   1.6   deraadt 	/* this returns gateway IP address */
    203   1.6   deraadt 	error = bp_whoami(&bp_sin, &my_ip, &gw_ip);
    204   1.3       gwr 	if (error)
    205   1.3       gwr 		panic("nfs_boot: bootparam whoami, error=%d", error);
    206  1.11       gwr 	printf("nfs_boot: server_addr=0x%x\n",
    207  1.25      fvdl 		   (u_int32_t)ntohl(bp_sin.sin_addr.s_addr));
    208   1.3       gwr 	printf("nfs_boot: hostname=%s\n", hostname);
    209   1.1     glass 
    210   1.3       gwr #ifdef	NFS_BOOT_GATEWAY
    211   1.1     glass 	/*
    212   1.8       gwr 	 * XXX - This code is conditionally compiled only because
    213   1.8       gwr 	 * many bootparam servers (in particular, SunOS 4.1.3)
    214   1.8       gwr 	 * always set the gateway address to their own address.
    215   1.8       gwr 	 * The bootparam server is not necessarily the gateway.
    216   1.8       gwr 	 * We could just believe the server, and at worst you would
    217   1.8       gwr 	 * need to delete the incorrect default route before adding
    218   1.8       gwr 	 * the correct one, but for simplicity, ignore the gateway.
    219   1.3       gwr 	 * If your server is OK, you can turn on this option.
    220   1.3       gwr 	 *
    221   1.3       gwr 	 * If the gateway address is set, add a default route.
    222   1.3       gwr 	 * (The mountd RPCs may go across a gateway.)
    223   1.1     glass 	 */
    224   1.3       gwr 	if (gw_ip.s_addr) {
    225   1.8       gwr 		struct sockaddr dst, gw, mask;
    226   1.3       gwr 		/* Destination: (default) */
    227  1.10   mycroft 		bzero((caddr_t)&dst, sizeof(dst));
    228   1.8       gwr 		dst.sa_len = sizeof(dst);
    229   1.8       gwr 		dst.sa_family = AF_INET;
    230   1.3       gwr 		/* Gateway: */
    231  1.10   mycroft 		bzero((caddr_t)&gw, sizeof(gw));
    232   1.8       gwr 		sin = (struct sockaddr_in *)&gw;
    233   1.8       gwr 		sin->sin_len = sizeof(gw);
    234   1.8       gwr 		sin->sin_family = AF_INET;
    235   1.8       gwr 		sin->sin_addr.s_addr = gw_ip.s_addr;
    236   1.8       gwr 		/* Mask: (zero length) */
    237   1.8       gwr 		bzero(&mask, sizeof(mask));
    238   1.6   deraadt 
    239   1.8       gwr 		printf("nfs_boot: gateway=0x%x\n", ntohl(gw_ip.s_addr));
    240   1.3       gwr 		/* add, dest, gw, mask, flags, 0 */
    241   1.8       gwr 		error = rtrequest(RTM_ADD, &dst, (struct sockaddr *)&gw,
    242   1.8       gwr 		    &mask, (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
    243   1.3       gwr 		if (error)
    244   1.3       gwr 			printf("nfs_boot: add route, error=%d\n", error);
    245   1.3       gwr 	}
    246   1.3       gwr #endif
    247   1.3       gwr 
    248  1.23       gwr 	bcopy(&bp_sin, &nd->nd_boot, sizeof(bp_sin));
    249   1.1     glass 
    250   1.3       gwr 	return (0);
    251   1.3       gwr }
    252   1.1     glass 
    253  1.23       gwr void
    254  1.23       gwr nfs_boot_getfh(bpsin, key, ndmntp)
    255   1.3       gwr 	struct sockaddr_in *bpsin;	/* bootparam server */
    256   1.6   deraadt 	char *key;			/* root or swap */
    257   1.6   deraadt 	struct nfs_dlmount *ndmntp;	/* output */
    258   1.3       gwr {
    259   1.3       gwr 	char pathname[MAXPATHLEN];
    260   1.4        pk 	char *sp, *dp, *endp;
    261  1.23       gwr 	struct sockaddr_in *sin;
    262   1.3       gwr 	int error;
    263   1.1     glass 
    264  1.23       gwr 	sin = &ndmntp->ndm_saddr;
    265  1.23       gwr 
    266   1.3       gwr 	/*
    267   1.3       gwr 	 * Get server:pathname for "key" (root or swap)
    268   1.3       gwr 	 * using RPC to bootparam/getfile
    269   1.3       gwr 	 */
    270  1.25      fvdl 	error = bp_getfile(bpsin, key, sin, ndmntp->ndm_host, pathname);
    271   1.3       gwr 	if (error)
    272   1.3       gwr 		panic("nfs_boot: bootparam get %s: %d", key, error);
    273   1.1     glass 
    274   1.3       gwr 	/*
    275   1.3       gwr 	 * Get file handle for "key" (root or swap)
    276   1.3       gwr 	 * using RPC to mountd/mount
    277   1.3       gwr 	 */
    278  1.23       gwr 	error = md_mount(sin, pathname, ndmntp->ndm_fh);
    279   1.3       gwr 	if (error)
    280   1.3       gwr 		panic("nfs_boot: mountd %s, error=%d", key, error);
    281   1.4        pk 
    282  1.23       gwr 	/* Set port number for NFS use. */
    283  1.23       gwr 	/* XXX: NFS port is always 2049, right? */
    284  1.23       gwr 	error = krpc_portmap(sin, NFS_PROG, NFS_VER2, &sin->sin_port);
    285  1.23       gwr 	if (error)
    286  1.23       gwr 		panic("nfs_boot: portmap NFS/v2, error=%d", error);
    287  1.23       gwr 
    288   1.4        pk 	/* Construct remote path (for getmntinfo(3)) */
    289   1.4        pk 	dp = ndmntp->ndm_host;
    290   1.4        pk 	endp = dp + MNAMELEN - 1;
    291   1.4        pk 	dp += strlen(dp);
    292   1.4        pk 	*dp++ = ':';
    293   1.4        pk 	for (sp = pathname; *sp && dp < endp;)
    294   1.4        pk 		*dp++ = *sp++;
    295   1.4        pk 	*dp = '\0';
    296   1.6   deraadt 
    297   1.1     glass }
    298   1.1     glass 
    299   1.3       gwr 
    300   1.1     glass /*
    301   1.3       gwr  * RPC: bootparam/whoami
    302   1.3       gwr  * Given client IP address, get:
    303   1.3       gwr  *	client name	(hostname)
    304   1.3       gwr  *	domain name (domainname)
    305   1.3       gwr  *	gateway address
    306   1.3       gwr  *
    307  1.15       gwr  * The hostname and domainname are set here for convenience.
    308  1.11       gwr  *
    309  1.11       gwr  * Note - bpsin is initialized to the broadcast address,
    310  1.11       gwr  * and will be replaced with the bootparam server address
    311  1.11       gwr  * after this call is complete.  Have to use PMAP_PROC_CALL
    312  1.11       gwr  * to make sure we get responses only from a servers that
    313  1.11       gwr  * know about us (don't want to broadcast a getport call).
    314   1.3       gwr  */
    315   1.3       gwr static int
    316   1.6   deraadt bp_whoami(bpsin, my_ip, gw_ip)
    317   1.6   deraadt 	struct sockaddr_in *bpsin;
    318   1.6   deraadt 	struct in_addr *my_ip;
    319   1.6   deraadt 	struct in_addr *gw_ip;
    320   1.1     glass {
    321  1.11       gwr 	/* RPC structures for PMAPPROC_CALLIT */
    322  1.11       gwr 	struct whoami_call {
    323  1.16       gwr 		u_int32_t call_prog;
    324  1.16       gwr 		u_int32_t call_vers;
    325  1.16       gwr 		u_int32_t call_proc;
    326  1.16       gwr 		u_int32_t call_arglen;
    327  1.11       gwr 	} *call;
    328  1.16       gwr 	struct callit_reply {
    329  1.16       gwr 		u_int32_t port;
    330  1.16       gwr 		u_int32_t encap_len;
    331  1.16       gwr 		/* encapsulated data here */
    332  1.16       gwr 	} *reply;
    333  1.11       gwr 
    334  1.11       gwr 	struct mbuf *m, *from;
    335   1.3       gwr 	struct sockaddr_in *sin;
    336   1.3       gwr 	int error, msg_len;
    337  1.16       gwr 	int16_t port;
    338   1.1     glass 
    339   1.3       gwr 	/*
    340  1.11       gwr 	 * Build request message for PMAPPROC_CALLIT.
    341   1.3       gwr 	 */
    342  1.16       gwr 	m = m_get(M_WAIT, MT_DATA);
    343  1.11       gwr 	call = mtod(m, struct whoami_call *);
    344  1.16       gwr 	m->m_len = sizeof(*call);
    345  1.17   mycroft 	call->call_prog = txdr_unsigned(BOOTPARAM_PROG);
    346  1.17   mycroft 	call->call_vers = txdr_unsigned(BOOTPARAM_VERS);
    347  1.17   mycroft 	call->call_proc = txdr_unsigned(BOOTPARAM_WHOAMI);
    348  1.11       gwr 
    349  1.16       gwr 	/*
    350  1.16       gwr 	 * append encapsulated data (client IP address)
    351  1.16       gwr 	 */
    352  1.16       gwr 	m->m_next = xdr_inaddr_encode(my_ip);
    353  1.17   mycroft 	call->call_arglen = txdr_unsigned(m->m_next->m_len);
    354  1.11       gwr 
    355  1.11       gwr 	/* RPC: portmap/callit */
    356  1.11       gwr 	bpsin->sin_port = htons(PMAPPORT);
    357  1.11       gwr 	from = NULL;
    358  1.11       gwr 	error = krpc_call(bpsin, PMAPPROG, PMAPVERS,
    359  1.11       gwr 			PMAPPROC_CALLIT, &m, &from);
    360   1.1     glass 	if (error)
    361   1.1     glass 		return error;
    362   1.3       gwr 
    363   1.3       gwr 	/*
    364   1.3       gwr 	 * Parse result message.
    365   1.3       gwr 	 */
    366  1.16       gwr 	if (m->m_len < sizeof(*reply)) {
    367  1.16       gwr 		m = m_pullup(m, sizeof(*reply));
    368  1.16       gwr 		if (m == NULL)
    369  1.16       gwr 			goto bad;
    370  1.16       gwr 	}
    371  1.16       gwr 	reply = mtod(m, struct callit_reply *);
    372  1.17   mycroft 	port = fxdr_unsigned(u_int32_t, reply->port);
    373  1.17   mycroft 	msg_len = fxdr_unsigned(u_int32_t, reply->encap_len);
    374  1.16       gwr 	m_adj(m, sizeof(*reply));
    375  1.11       gwr 
    376  1.16       gwr 	/*
    377  1.16       gwr 	 * Save bootparam server address
    378  1.16       gwr 	 */
    379  1.11       gwr 	sin = mtod(from, struct sockaddr_in *);
    380  1.16       gwr 	bpsin->sin_port = htons(port);
    381  1.11       gwr 	bpsin->sin_addr.s_addr = sin->sin_addr.s_addr;
    382  1.11       gwr 
    383   1.3       gwr 	/* client name */
    384  1.16       gwr 	hostnamelen = MAXHOSTNAMELEN-1;
    385  1.16       gwr 	m = xdr_string_decode(m, hostname, &hostnamelen);
    386  1.16       gwr 	if (m == NULL)
    387   1.3       gwr 		goto bad;
    388   1.3       gwr 
    389   1.3       gwr 	/* domain name */
    390  1.16       gwr 	domainnamelen = MAXHOSTNAMELEN-1;
    391  1.16       gwr 	m = xdr_string_decode(m, domainname, &domainnamelen);
    392  1.16       gwr 	if (m == NULL)
    393   1.3       gwr 		goto bad;
    394   1.3       gwr 
    395   1.3       gwr 	/* gateway address */
    396  1.16       gwr 	m = xdr_inaddr_decode(m, gw_ip);
    397  1.16       gwr 	if (m == NULL)
    398   1.3       gwr 		goto bad;
    399  1.16       gwr 
    400  1.16       gwr 	/* success */
    401   1.3       gwr 	goto out;
    402   1.3       gwr 
    403   1.6   deraadt bad:
    404   1.3       gwr 	printf("nfs_boot: bootparam_whoami: bad reply\n");
    405   1.3       gwr 	error = EBADRPC;
    406   1.3       gwr 
    407   1.6   deraadt out:
    408  1.11       gwr 	if (from)
    409  1.11       gwr 		m_freem(from);
    410  1.16       gwr 	if (m)
    411  1.16       gwr 		m_freem(m);
    412   1.3       gwr 	return(error);
    413   1.1     glass }
    414   1.1     glass 
    415   1.3       gwr 
    416   1.1     glass /*
    417   1.3       gwr  * RPC: bootparam/getfile
    418   1.3       gwr  * Given client name and file "key", get:
    419   1.3       gwr  *	server name
    420   1.3       gwr  *	server IP address
    421   1.3       gwr  *	server pathname
    422   1.1     glass  */
    423   1.3       gwr static int
    424   1.6   deraadt bp_getfile(bpsin, key, md_sin, serv_name, pathname)
    425   1.6   deraadt 	struct sockaddr_in *bpsin;
    426   1.6   deraadt 	char *key;
    427   1.6   deraadt 	struct sockaddr_in *md_sin;
    428   1.6   deraadt 	char *serv_name;
    429   1.6   deraadt 	char *pathname;
    430   1.1     glass {
    431   1.3       gwr 	struct mbuf *m;
    432   1.1     glass 	struct sockaddr_in *sin;
    433  1.16       gwr 	struct in_addr inaddr;
    434  1.16       gwr 	int error, sn_len, path_len;
    435   1.1     glass 
    436   1.3       gwr 	/*
    437  1.16       gwr 	 * Build request message.
    438   1.3       gwr 	 */
    439   1.1     glass 
    440   1.3       gwr 	/* client name (hostname) */
    441  1.16       gwr 	m  = xdr_string_encode(hostname, hostnamelen);
    442  1.22        pk 	if (m == NULL)
    443  1.22        pk 		return (ENOMEM);
    444  1.16       gwr 
    445   1.3       gwr 	/* key name (root or swap) */
    446  1.16       gwr 	m->m_next = xdr_string_encode(key, strlen(key));
    447  1.22        pk 	if (m->m_next == NULL)
    448  1.22        pk 		return (ENOMEM);
    449   1.3       gwr 
    450   1.3       gwr 	/* RPC: bootparam/getfile */
    451  1.11       gwr 	error = krpc_call(bpsin, BOOTPARAM_PROG, BOOTPARAM_VERS,
    452  1.11       gwr 			BOOTPARAM_GETFILE, &m, NULL);
    453   1.3       gwr 	if (error)
    454   1.1     glass 		return error;
    455   1.3       gwr 
    456   1.3       gwr 	/*
    457   1.3       gwr 	 * Parse result message.
    458   1.3       gwr 	 */
    459   1.3       gwr 
    460   1.3       gwr 	/* server name */
    461  1.16       gwr 	sn_len = MNAMELEN-1;
    462  1.16       gwr 	m = xdr_string_decode(m, serv_name, &sn_len);
    463  1.16       gwr 	if (m == NULL)
    464   1.3       gwr 		goto bad;
    465  1.16       gwr 
    466  1.16       gwr 	/* server IP address (mountd/NFS) */
    467  1.16       gwr 	m = xdr_inaddr_decode(m, &inaddr);
    468  1.16       gwr 	if (m == NULL)
    469   1.3       gwr 		goto bad;
    470  1.16       gwr 
    471  1.16       gwr 	/* server pathname */
    472  1.16       gwr 	path_len = MAXPATHLEN-1;
    473  1.16       gwr 	m = xdr_string_decode(m, pathname, &path_len);
    474  1.16       gwr 	if (m == NULL)
    475   1.3       gwr 		goto bad;
    476   1.3       gwr 
    477  1.16       gwr 	/* setup server socket address */
    478   1.3       gwr 	sin = md_sin;
    479  1.10   mycroft 	bzero((caddr_t)sin, sizeof(*sin));
    480   1.3       gwr 	sin->sin_len = sizeof(*sin);
    481   1.1     glass 	sin->sin_family = AF_INET;
    482  1.16       gwr 	sin->sin_addr = inaddr;
    483   1.3       gwr 
    484  1.16       gwr 	/* success */
    485   1.3       gwr 	goto out;
    486   1.3       gwr 
    487   1.6   deraadt bad:
    488   1.3       gwr 	printf("nfs_boot: bootparam_getfile: bad reply\n");
    489   1.3       gwr 	error = EBADRPC;
    490   1.1     glass 
    491   1.6   deraadt out:
    492   1.3       gwr 	m_freem(m);
    493   1.3       gwr 	return(0);
    494   1.3       gwr }
    495   1.1     glass 
    496   1.1     glass 
    497   1.1     glass /*
    498   1.3       gwr  * RPC: mountd/mount
    499   1.3       gwr  * Given a server pathname, get an NFS file handle.
    500   1.3       gwr  * Also, sets sin->sin_port to the NFS service port.
    501   1.1     glass  */
    502   1.3       gwr static int
    503   1.3       gwr md_mount(mdsin, path, fhp)
    504   1.3       gwr 	struct sockaddr_in *mdsin;		/* mountd server address */
    505   1.3       gwr 	char *path;
    506   1.3       gwr 	u_char *fhp;
    507   1.1     glass {
    508   1.3       gwr 	/* The RPC structures */
    509   1.3       gwr 	struct rdata {
    510  1.20       cgd 		u_int32_t errno;
    511  1.25      fvdl 		u_int8_t  fh[NFSX_V2FH];
    512   1.3       gwr 	} *rdata;
    513   1.3       gwr 	struct mbuf *m;
    514  1.16       gwr 	int error;
    515   1.3       gwr 
    516  1.11       gwr 	/* Get port number for MOUNTD. */
    517  1.11       gwr 	error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
    518  1.11       gwr 						 &mdsin->sin_port);
    519  1.11       gwr 	if (error) return error;
    520  1.11       gwr 
    521  1.16       gwr 	m = xdr_string_encode(path, strlen(path));
    522  1.22        pk 	if (m == NULL)
    523  1.25      fvdl 		return ENOMEM;
    524   1.3       gwr 
    525   1.3       gwr 	/* Do RPC to mountd. */
    526  1.11       gwr 	error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
    527  1.11       gwr 			RPCMNT_MOUNT, &m, NULL);
    528   1.6   deraadt 	if (error)
    529   1.3       gwr 		return error;	/* message already freed */
    530   1.1     glass 
    531  1.23       gwr 	/* The reply might have only the errno. */
    532  1.23       gwr 	if (m->m_len < 4)
    533  1.23       gwr 		goto bad;
    534  1.23       gwr 	/* Have at least errno, so check that. */
    535  1.23       gwr 	rdata = mtod(m, struct rdata *);
    536  1.23       gwr 	error = fxdr_unsigned(u_int32_t, rdata->errno);
    537  1.23       gwr 	if (error)
    538  1.23       gwr 		goto out;
    539  1.23       gwr 
    540  1.25      fvdl 	 /* Have errno==0, so the fh must be there. */
    541  1.16       gwr 	if (m->m_len < sizeof(*rdata)) {
    542  1.16       gwr 		m = m_pullup(m, sizeof(*rdata));
    543  1.16       gwr 		if (m == NULL)
    544  1.16       gwr 			goto bad;
    545  1.23       gwr 		rdata = mtod(m, struct rdata *);
    546  1.16       gwr 	}
    547  1.25      fvdl 	bcopy(rdata->fh, fhp, NFSX_V2FH);
    548   1.3       gwr 	goto out;
    549   1.1     glass 
    550   1.6   deraadt bad:
    551   1.3       gwr 	error = EBADRPC;
    552   1.1     glass 
    553   1.6   deraadt out:
    554   1.3       gwr 	m_freem(m);
    555   1.3       gwr 	return error;
    556   1.7    paulus }
    557   1.7    paulus 
    558   1.7    paulus #endif /* NETHER */
    559