Home | History | Annotate | Line # | Download | only in nfs
nfs_boot.c revision 1.11
      1  1.11      gwr /*    $NetBSD: nfs_boot.c,v 1.11 1994/09/26 16:42:33 gwr Exp $ */
      2   1.5      cgd 
      3   1.1    glass /*
      4   1.1    glass  * Copyright (c) 1994 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.1    glass #include <sys/socket.h>
     39   1.1    glass #include <sys/reboot.h>
     40   1.1    glass 
     41   1.1    glass #include <net/if.h>
     42   1.3      gwr #include <net/route.h>
     43   1.3      gwr 
     44   1.1    glass #include <netinet/in.h>
     45   1.3      gwr #include <netinet/if_ether.h>
     46   1.1    glass 
     47   1.1    glass #include <nfs/rpcv2.h>
     48   1.1    glass #include <nfs/nfsv2.h>
     49   1.1    glass #include <nfs/nfs.h>
     50   1.1    glass #include <nfs/nfsdiskless.h>
     51  1.11      gwr #include <nfs/krpc.h>
     52   1.1    glass 
     53   1.7   paulus #include "ether.h"
     54  1.11      gwr #if NETHER == 0
     55  1.11      gwr 
     56  1.11      gwr int nfs_boot_init(nd, procp)
     57  1.11      gwr 	struct nfs_diskless *nd;
     58  1.11      gwr 	struct proc *procp;
     59  1.11      gwr {
     60  1.11      gwr 	panic("nfs_boot_init: no ether");
     61  1.11      gwr }
     62  1.11      gwr 
     63  1.11      gwr #else /* NETHER */
     64   1.7   paulus 
     65   1.1    glass /*
     66   1.1    glass  * Support for NFS diskless booting, specifically getting information
     67   1.1    glass  * about where to boot from, what pathnames, etc.
     68   1.1    glass  *
     69   1.3      gwr  * This implememtation uses RARP and the bootparam RPC.
     70   1.3      gwr  * We are forced to implement RPC anyway (to get file handles)
     71   1.3      gwr  * so we might as well take advantage of it for bootparam too.
     72   1.1    glass  *
     73   1.3      gwr  * The diskless boot sequence goes as follows:
     74   1.3      gwr  * (1) Get our interface address using RARP
     75   1.3      gwr  *     (also save the address of the RARP server)
     76   1.3      gwr  * (2) Get our hostname using RPC/bootparam/whoami
     77   1.3      gwr  *     (all boopararms RPCs to the RARP server)
     78   1.3      gwr  * (3) Get the root path using RPC/bootparam/getfile
     79   1.3      gwr  * (4) Get the root file handle using RPC/mountd
     80   1.3      gwr  * (5) Get the swap path using RPC/bootparam/getfile
     81   1.3      gwr  * (6) Get the swap file handle using RPC/mountd
     82   1.1    glass  *
     83   1.3      gwr  * (This happens to be the way Sun does it too.)
     84   1.1    glass  */
     85   1.1    glass 
     86   1.3      gwr /* bootparam RPC */
     87   1.6  deraadt static int bp_whoami __P((struct sockaddr_in *bpsin,
     88   1.6  deraadt 	struct in_addr *my_ip, struct in_addr *gw_ip));
     89   1.6  deraadt static int bp_getfile __P((struct sockaddr_in *bpsin, char *key,
     90   1.6  deraadt 	struct sockaddr_in *mdsin, char *servname, char *path));
     91   1.3      gwr 
     92   1.3      gwr /* mountd RPC */
     93   1.6  deraadt static int md_mount __P((struct sockaddr_in *mdsin, char *path,
     94   1.6  deraadt 	u_char *fh));
     95   1.3      gwr 
     96   1.3      gwr /* other helpers */
     97   1.6  deraadt static void get_path_and_handle __P((struct sockaddr_in *bpsin,
     98   1.6  deraadt 	char *key, struct nfs_dlmount *ndmntp));
     99   1.1    glass 
    100   1.1    glass /*
    101   1.3      gwr  * Called with an empty nfs_diskless struct to be filled in.
    102   1.1    glass  */
    103   1.6  deraadt int
    104   1.6  deraadt nfs_boot_init(nd, procp)
    105   1.3      gwr 	struct nfs_diskless *nd;
    106   1.3      gwr 	struct proc *procp;
    107   1.1    glass {
    108   1.3      gwr 	struct ifreq ireq;
    109  1.11      gwr 	struct in_addr my_ip, gw_ip;
    110   1.3      gwr 	struct sockaddr_in bp_sin;
    111   1.1    glass 	struct sockaddr_in *sin;
    112   1.3      gwr 	struct ifnet *ifp;
    113   1.3      gwr 	struct socket *so;
    114   1.3      gwr 	int error, len;
    115   1.3      gwr 	u_short port;
    116   1.3      gwr 
    117   1.3      gwr #if 0
    118   1.3      gwr 	/*
    119   1.3      gwr 	 * XXX time must be non-zero when we init the interface or else
    120   1.3      gwr 	 * the arp code will wedge... (Fixed in if_ether.c -gwr)
    121   1.3      gwr 	 */
    122   1.3      gwr 	if (time.tv_sec == 0)
    123   1.3      gwr 		time.tv_sec = 1;
    124   1.3      gwr #endif
    125   1.3      gwr 
    126   1.3      gwr 	/*
    127   1.3      gwr 	 * Find an interface, rarp for its ip address, stuff it, the
    128   1.3      gwr 	 * implied broadcast addr, and netmask into a nfs_diskless struct.
    129   1.3      gwr 	 *
    130   1.3      gwr 	 * This was moved here from nfs_vfsops.c because this procedure
    131   1.3      gwr 	 * would be quite different if someone decides to write (i.e.) a
    132   1.3      gwr 	 * BOOTP version of this file (might not use RARP, etc.) -gwr
    133   1.3      gwr 	 */
    134   1.3      gwr 
    135   1.3      gwr 	/*
    136   1.3      gwr 	 * Find a network interface.
    137   1.3      gwr 	 * XXX - This should use the specified boot device.
    138   1.3      gwr 	 */
    139   1.3      gwr 	for (ifp = ifnet; ifp; ifp = ifp->if_next)
    140   1.3      gwr 		if ((ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
    141   1.3      gwr 			break;
    142   1.3      gwr 	if (ifp == NULL)
    143   1.3      gwr 		panic("nfs_boot: no suitable interface");
    144   1.3      gwr 	sprintf(ireq.ifr_name, "%s%d", ifp->if_name, ifp->if_unit);
    145   1.3      gwr 	printf("nfs_boot: using network interface '%s'\n",
    146   1.6  deraadt 	    ireq.ifr_name);
    147   1.3      gwr 
    148   1.3      gwr 	/*
    149   1.3      gwr 	 * Bring up the interface.
    150   1.3      gwr 	 */
    151   1.3      gwr 	if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0)) != 0)
    152   1.3      gwr 		panic("nfs_boot: socreate, error=%d", error);
    153   1.3      gwr 	ireq.ifr_flags = IFF_UP;
    154   1.3      gwr 	error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)&ireq, procp);
    155   1.6  deraadt 	if (error)
    156   1.6  deraadt 		panic("nfs_boot: SIFFLAGS, error=%d", error);
    157   1.3      gwr 
    158   1.3      gwr 	/*
    159   1.3      gwr 	 * Do RARP for the interface address.  Also
    160   1.3      gwr 	 * save the server address for bootparam RPC.
    161   1.3      gwr 	 */
    162  1.11      gwr 	if ((error = revarpwhoami(&my_ip, ifp)) != 0)
    163   1.3      gwr 		panic("revarp failed, error=%d", error);
    164  1.11      gwr 	printf("nfs_boot: client_addr=0x%x\n", ntohl(my_ip.s_addr));
    165   1.3      gwr 
    166   1.3      gwr 	/*
    167   1.3      gwr 	 * Do enough of ifconfig(8) so that the chosen interface can
    168   1.3      gwr 	 * talk to the server(s).  (also get brcast addr and netmask)
    169   1.3      gwr 	 */
    170   1.3      gwr 	/* Set interface address. */
    171   1.3      gwr 	sin = (struct sockaddr_in *)&ireq.ifr_addr;
    172  1.10  mycroft 	bzero((caddr_t)sin, sizeof(*sin));
    173   1.3      gwr 	sin->sin_len = sizeof(*sin);
    174   1.3      gwr 	sin->sin_family = AF_INET;
    175   1.3      gwr 	sin->sin_addr.s_addr = my_ip.s_addr;
    176   1.3      gwr 	error = ifioctl(so, SIOCSIFADDR, (caddr_t)&ireq, procp);
    177   1.6  deraadt 	if (error)
    178   1.6  deraadt 		panic("nfs_boot: set if addr, error=%d", error);
    179   1.3      gwr 
    180   1.3      gwr 	soclose(so);
    181   1.3      gwr 
    182   1.3      gwr 	/*
    183   1.3      gwr 	 * Get client name and gateway address.
    184   1.3      gwr 	 * RPC: bootparam/whoami
    185  1.11      gwr 	 * XXX - Using old broadcast addr. for WHOAMI,
    186  1.11      gwr 	 * then it is replaced with the BP server addr.
    187   1.3      gwr 	 */
    188  1.10  mycroft 	bzero((caddr_t)&bp_sin, sizeof(bp_sin));
    189   1.3      gwr 	bp_sin.sin_len = sizeof(bp_sin);
    190   1.3      gwr 	bp_sin.sin_family = AF_INET;
    191  1.11      gwr 	bp_sin.sin_addr.s_addr = ~0;	/* XXX */
    192   1.3      gwr 	hostnamelen = MAXHOSTNAMELEN;
    193   1.6  deraadt 
    194   1.6  deraadt 	/* this returns gateway IP address */
    195   1.6  deraadt 	error = bp_whoami(&bp_sin, &my_ip, &gw_ip);
    196   1.3      gwr 	if (error)
    197   1.3      gwr 		panic("nfs_boot: bootparam whoami, error=%d", error);
    198  1.11      gwr 	printf("nfs_boot: server_addr=0x%x\n",
    199  1.11      gwr 		   ntohl(bp_sin.sin_addr.s_addr));
    200   1.3      gwr 	printf("nfs_boot: hostname=%s\n", hostname);
    201   1.1    glass 
    202   1.3      gwr #ifdef	NFS_BOOT_GATEWAY
    203   1.1    glass 	/*
    204   1.8      gwr 	 * XXX - This code is conditionally compiled only because
    205   1.8      gwr 	 * many bootparam servers (in particular, SunOS 4.1.3)
    206   1.8      gwr 	 * always set the gateway address to their own address.
    207   1.8      gwr 	 * The bootparam server is not necessarily the gateway.
    208   1.8      gwr 	 * We could just believe the server, and at worst you would
    209   1.8      gwr 	 * need to delete the incorrect default route before adding
    210   1.8      gwr 	 * the correct one, but for simplicity, ignore the gateway.
    211   1.3      gwr 	 * If your server is OK, you can turn on this option.
    212   1.3      gwr 	 *
    213   1.3      gwr 	 * If the gateway address is set, add a default route.
    214   1.3      gwr 	 * (The mountd RPCs may go across a gateway.)
    215   1.1    glass 	 */
    216   1.3      gwr 	if (gw_ip.s_addr) {
    217   1.8      gwr 		struct sockaddr dst, gw, mask;
    218   1.3      gwr 		/* Destination: (default) */
    219  1.10  mycroft 		bzero((caddr_t)&dst, sizeof(dst));
    220   1.8      gwr 		dst.sa_len = sizeof(dst);
    221   1.8      gwr 		dst.sa_family = AF_INET;
    222   1.3      gwr 		/* Gateway: */
    223  1.10  mycroft 		bzero((caddr_t)&gw, sizeof(gw));
    224   1.8      gwr 		sin = (struct sockaddr_in *)&gw;
    225   1.8      gwr 		sin->sin_len = sizeof(gw);
    226   1.8      gwr 		sin->sin_family = AF_INET;
    227   1.8      gwr 		sin->sin_addr.s_addr = gw_ip.s_addr;
    228   1.8      gwr 		/* Mask: (zero length) */
    229   1.8      gwr 		bzero(&mask, sizeof(mask));
    230   1.6  deraadt 
    231   1.8      gwr 		printf("nfs_boot: gateway=0x%x\n", ntohl(gw_ip.s_addr));
    232   1.3      gwr 		/* add, dest, gw, mask, flags, 0 */
    233   1.8      gwr 		error = rtrequest(RTM_ADD, &dst, (struct sockaddr *)&gw,
    234   1.8      gwr 		    &mask, (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
    235   1.3      gwr 		if (error)
    236   1.3      gwr 			printf("nfs_boot: add route, error=%d\n", error);
    237   1.3      gwr 	}
    238   1.3      gwr #endif
    239   1.3      gwr 
    240   1.3      gwr 	get_path_and_handle(&bp_sin, "root", &nd->nd_root);
    241   1.3      gwr 	get_path_and_handle(&bp_sin, "swap", &nd->nd_swap);
    242   1.1    glass 
    243   1.3      gwr 	return (0);
    244   1.3      gwr }
    245   1.1    glass 
    246   1.3      gwr static void
    247   1.3      gwr get_path_and_handle(bpsin, key, ndmntp)
    248   1.3      gwr 	struct sockaddr_in *bpsin;	/* bootparam server */
    249   1.6  deraadt 	char *key;			/* root or swap */
    250   1.6  deraadt 	struct nfs_dlmount *ndmntp;	/* output */
    251   1.3      gwr {
    252   1.3      gwr 	char pathname[MAXPATHLEN];
    253   1.4       pk 	char *sp, *dp, *endp;
    254   1.3      gwr 	int error;
    255   1.1    glass 
    256   1.3      gwr 	/*
    257   1.3      gwr 	 * Get server:pathname for "key" (root or swap)
    258   1.3      gwr 	 * using RPC to bootparam/getfile
    259   1.3      gwr 	 */
    260   1.6  deraadt 	error = bp_getfile(bpsin, key, &ndmntp->ndm_saddr,
    261   1.6  deraadt 	    ndmntp->ndm_host, pathname);
    262   1.3      gwr 	if (error)
    263   1.3      gwr 		panic("nfs_boot: bootparam get %s: %d", key, error);
    264   1.3      gwr 	printf("%s on %s:%s\n", key, ndmntp->ndm_host, pathname);
    265   1.1    glass 
    266   1.3      gwr 	/*
    267   1.3      gwr 	 * Get file handle for "key" (root or swap)
    268   1.3      gwr 	 * using RPC to mountd/mount
    269   1.3      gwr 	 */
    270   1.6  deraadt 	error = md_mount(&ndmntp->ndm_saddr, pathname, ndmntp->ndm_fh);
    271   1.3      gwr 	if (error)
    272   1.3      gwr 		panic("nfs_boot: mountd %s, error=%d", key, error);
    273   1.4       pk 
    274   1.4       pk 	/* Construct remote path (for getmntinfo(3)) */
    275   1.4       pk 	dp = ndmntp->ndm_host;
    276   1.4       pk 	endp = dp + MNAMELEN - 1;
    277   1.4       pk 	dp += strlen(dp);
    278   1.4       pk 	*dp++ = ':';
    279   1.4       pk 	for (sp = pathname; *sp && dp < endp;)
    280   1.4       pk 		*dp++ = *sp++;
    281   1.4       pk 	*dp = '\0';
    282   1.6  deraadt 
    283   1.1    glass }
    284   1.1    glass 
    285   1.3      gwr 
    286   1.1    glass /*
    287   1.3      gwr  * Get an mbuf with the given length, and
    288   1.3      gwr  * initialize the pkthdr length field.
    289   1.1    glass  */
    290   1.3      gwr static struct mbuf *
    291   1.3      gwr m_get_len(int msg_len)
    292   1.1    glass {
    293   1.1    glass 	struct mbuf *m;
    294   1.3      gwr 	m = m_gethdr(M_WAIT, MT_DATA);
    295   1.3      gwr 	if (m == NULL)
    296   1.3      gwr 		return NULL;
    297   1.3      gwr 	if (msg_len > MHLEN) {
    298   1.3      gwr 		if (msg_len > MCLBYTES)
    299   1.3      gwr 			panic("nfs_boot: msg_len > MCLBYTES");
    300   1.3      gwr 		MCLGET(m, M_WAIT);
    301   1.1    glass 		if (m == NULL)
    302   1.3      gwr 			return NULL;
    303   1.1    glass 	}
    304   1.3      gwr 	m->m_len = msg_len;
    305   1.3      gwr 	m->m_pkthdr.len = m->m_len;
    306   1.3      gwr 	return (m);
    307   1.1    glass }
    308   1.1    glass 
    309   1.3      gwr 
    310   1.3      gwr /*
    311   1.3      gwr  * String representation for RPC.
    312   1.3      gwr  */
    313   1.3      gwr struct rpc_string {
    314   1.3      gwr 	u_long len;		/* length without null or padding */
    315   1.3      gwr 	u_char data[4];	/* data (longer, of course) */
    316   1.3      gwr     /* data is padded to a long-word boundary */
    317   1.3      gwr };
    318   1.3      gwr /* Compute space used given string length. */
    319   1.3      gwr #define	RPC_STR_SIZE(slen) (4 + ((slen + 3) & ~3))
    320   1.3      gwr 
    321   1.3      gwr /*
    322   1.3      gwr  * Inet address in RPC messages
    323   1.3      gwr  * (Note, really four longs, NOT chars.  Blech.)
    324   1.3      gwr  */
    325   1.3      gwr struct bp_inaddr {
    326   1.3      gwr 	u_long  atype;
    327   1.3      gwr 	long	addr[4];
    328   1.3      gwr };
    329   1.3      gwr 
    330   1.3      gwr 
    331   1.1    glass /*
    332   1.3      gwr  * RPC: bootparam/whoami
    333   1.3      gwr  * Given client IP address, get:
    334   1.3      gwr  *	client name	(hostname)
    335   1.3      gwr  *	domain name (domainname)
    336   1.3      gwr  *	gateway address
    337   1.3      gwr  *
    338   1.3      gwr  * Setting the hostname and domainname here may be somewhat
    339   1.3      gwr  * controvercial, but it is so easy to do it here. -gwr
    340  1.11      gwr  *
    341  1.11      gwr  * Note - bpsin is initialized to the broadcast address,
    342  1.11      gwr  * and will be replaced with the bootparam server address
    343  1.11      gwr  * after this call is complete.  Have to use PMAP_PROC_CALL
    344  1.11      gwr  * to make sure we get responses only from a servers that
    345  1.11      gwr  * know about us (don't want to broadcast a getport call).
    346   1.3      gwr  */
    347   1.3      gwr static int
    348   1.6  deraadt bp_whoami(bpsin, my_ip, gw_ip)
    349   1.6  deraadt 	struct sockaddr_in *bpsin;
    350   1.6  deraadt 	struct in_addr *my_ip;
    351   1.6  deraadt 	struct in_addr *gw_ip;
    352   1.1    glass {
    353  1.11      gwr 	/* RPC structures for PMAPPROC_CALLIT */
    354  1.11      gwr 	struct whoami_call {
    355  1.11      gwr 		u_long call_prog;
    356  1.11      gwr 		u_long call_vers;
    357  1.11      gwr 		u_long call_proc;
    358  1.11      gwr 		u_long call_arglen;
    359  1.11      gwr 		struct bp_inaddr call_ia;
    360  1.11      gwr 	} *call;
    361  1.11      gwr 
    362  1.11      gwr 	struct rpc_string *str;
    363   1.3      gwr 	struct bp_inaddr *bia;
    364  1.11      gwr 	struct mbuf *m, *from;
    365   1.3      gwr 	struct sockaddr_in *sin;
    366   1.3      gwr 	int error, msg_len;
    367   1.3      gwr 	int cn_len, dn_len;
    368   1.3      gwr 	u_char *p;
    369  1.11      gwr 	long *lp;
    370   1.1    glass 
    371   1.3      gwr 	/*
    372   1.3      gwr 	 * Get message buffer of sufficient size.
    373   1.3      gwr 	 */
    374  1.11      gwr 	msg_len = sizeof(*call);
    375   1.3      gwr 	m = m_get_len(msg_len);
    376   1.3      gwr 	if (m == NULL)
    377   1.3      gwr 		return ENOBUFS;
    378   1.1    glass 
    379   1.3      gwr 	/*
    380  1.11      gwr 	 * Build request message for PMAPPROC_CALLIT.
    381   1.3      gwr 	 */
    382  1.11      gwr 	call = mtod(m, struct whoami_call *);
    383  1.11      gwr 	call->call_prog = BOOTPARAM_PROG;
    384  1.11      gwr 	call->call_vers = BOOTPARAM_VERS;
    385  1.11      gwr 	call->call_proc = BOOTPARAM_WHOAMI;
    386  1.11      gwr 	call->call_arglen = sizeof(struct bp_inaddr);
    387  1.11      gwr 
    388   1.3      gwr 	/* client IP address */
    389  1.11      gwr 	call->call_ia.atype = htonl(1);
    390  1.11      gwr 	p = (u_char*)my_ip;
    391  1.11      gwr 	lp = call->call_ia.addr;
    392  1.11      gwr 	*lp++ = htonl(*p);	p++;
    393  1.11      gwr 	*lp++ = htonl(*p);	p++;
    394  1.11      gwr 	*lp++ = htonl(*p);	p++;
    395  1.11      gwr 	*lp++ = htonl(*p);	p++;
    396  1.11      gwr 
    397  1.11      gwr 	/* RPC: portmap/callit */
    398  1.11      gwr 	bpsin->sin_port = htons(PMAPPORT);
    399  1.11      gwr 	from = NULL;
    400  1.11      gwr 	error = krpc_call(bpsin, PMAPPROG, PMAPVERS,
    401  1.11      gwr 			PMAPPROC_CALLIT, &m, &from);
    402   1.1    glass 	if (error)
    403   1.1    glass 		return error;
    404   1.3      gwr 
    405   1.3      gwr 	/*
    406   1.3      gwr 	 * Parse result message.
    407   1.3      gwr 	 */
    408   1.3      gwr 	msg_len = m->m_len;
    409  1.11      gwr 	lp = mtod(m, long *);
    410  1.11      gwr 
    411  1.11      gwr 	/* bootparam server port (also grab from address). */
    412  1.11      gwr 	if (msg_len < sizeof(*lp))
    413  1.11      gwr 		goto bad;
    414  1.11      gwr 	msg_len -= sizeof(*lp);
    415  1.11      gwr 	bpsin->sin_port = (short) *lp++;
    416  1.11      gwr 	sin = mtod(from, struct sockaddr_in *);
    417  1.11      gwr 	bpsin->sin_addr.s_addr = sin->sin_addr.s_addr;
    418  1.11      gwr 
    419  1.11      gwr 	/* length of encapsulated results */
    420  1.11      gwr 	if (msg_len < (*lp + sizeof(*lp)))
    421  1.11      gwr 		goto bad;
    422  1.11      gwr 	msg_len = *lp++;
    423  1.11      gwr 	p = (char*)lp;
    424   1.3      gwr 
    425   1.3      gwr 	/* client name */
    426   1.3      gwr 	if (msg_len < sizeof(*str))
    427   1.3      gwr 		goto bad;
    428   1.3      gwr 	str = (struct rpc_string *)p;
    429   1.3      gwr 	cn_len = ntohl(str->len);
    430   1.3      gwr 	if (msg_len < cn_len)
    431   1.3      gwr 		goto bad;
    432   1.3      gwr 	if (cn_len >= MAXHOSTNAMELEN)
    433   1.3      gwr 		goto bad;
    434   1.3      gwr 	bcopy(str->data, hostname, cn_len);
    435   1.3      gwr 	hostname[cn_len] = '\0';
    436   1.3      gwr 	hostnamelen = cn_len;
    437   1.3      gwr 	p += RPC_STR_SIZE(cn_len);
    438   1.3      gwr 	msg_len -= RPC_STR_SIZE(cn_len);
    439   1.3      gwr 
    440   1.3      gwr 	/* domain name */
    441   1.3      gwr 	if (msg_len < sizeof(*str))
    442   1.3      gwr 		goto bad;
    443   1.3      gwr 	str = (struct rpc_string *)p;
    444   1.3      gwr 	dn_len = ntohl(str->len);
    445   1.3      gwr 	if (msg_len < dn_len)
    446   1.3      gwr 		goto bad;
    447   1.3      gwr 	if (dn_len >= MAXHOSTNAMELEN)
    448   1.3      gwr 		goto bad;
    449   1.3      gwr 	bcopy(str->data, domainname, dn_len);
    450   1.3      gwr 	domainname[dn_len] = '\0';
    451   1.3      gwr 	domainnamelen = dn_len;
    452   1.3      gwr 	p += RPC_STR_SIZE(dn_len);
    453   1.3      gwr 	msg_len -= RPC_STR_SIZE(dn_len);
    454   1.3      gwr 
    455   1.3      gwr 	/* gateway address */
    456   1.3      gwr 	if (msg_len < sizeof(*bia))
    457   1.3      gwr 		goto bad;
    458   1.3      gwr 	bia = (struct bp_inaddr *)p;
    459   1.3      gwr 	if (bia->atype != htonl(1))
    460   1.3      gwr 		goto bad;
    461   1.3      gwr 	p = (u_char*)gw_ip;
    462   1.3      gwr 	*p++ = ntohl(bia->addr[0]);
    463   1.3      gwr 	*p++ = ntohl(bia->addr[1]);
    464   1.3      gwr 	*p++ = ntohl(bia->addr[2]);
    465   1.3      gwr 	*p++ = ntohl(bia->addr[3]);
    466   1.3      gwr 	goto out;
    467   1.3      gwr 
    468   1.6  deraadt bad:
    469   1.3      gwr 	printf("nfs_boot: bootparam_whoami: bad reply\n");
    470   1.3      gwr 	error = EBADRPC;
    471   1.3      gwr 
    472   1.6  deraadt out:
    473  1.11      gwr 	if (from)
    474  1.11      gwr 		m_freem(from);
    475   1.3      gwr 	m_freem(m);
    476   1.3      gwr 	return(error);
    477   1.1    glass }
    478   1.1    glass 
    479   1.3      gwr 
    480   1.1    glass /*
    481   1.3      gwr  * RPC: bootparam/getfile
    482   1.3      gwr  * Given client name and file "key", get:
    483   1.3      gwr  *	server name
    484   1.3      gwr  *	server IP address
    485   1.3      gwr  *	server pathname
    486   1.1    glass  */
    487   1.3      gwr static int
    488   1.6  deraadt bp_getfile(bpsin, key, md_sin, serv_name, pathname)
    489   1.6  deraadt 	struct sockaddr_in *bpsin;
    490   1.6  deraadt 	char *key;
    491   1.6  deraadt 	struct sockaddr_in *md_sin;
    492   1.6  deraadt 	char *serv_name;
    493   1.6  deraadt 	char *pathname;
    494   1.1    glass {
    495   1.3      gwr 	struct rpc_string *str;
    496   1.3      gwr 	struct mbuf *m;
    497   1.3      gwr 	struct bp_inaddr *bia;
    498   1.1    glass 	struct sockaddr_in *sin;
    499   1.3      gwr 	u_char *p, *q;
    500   1.3      gwr 	int error, msg_len;
    501   1.3      gwr 	int cn_len, key_len, sn_len, path_len;
    502   1.1    glass 
    503   1.3      gwr 	/*
    504   1.3      gwr 	 * Get message buffer of sufficient size.
    505   1.3      gwr 	 */
    506   1.3      gwr 	cn_len = hostnamelen;
    507   1.3      gwr 	key_len = strlen(key);
    508   1.3      gwr 	msg_len = 0;
    509   1.3      gwr 	msg_len += RPC_STR_SIZE(cn_len);
    510   1.3      gwr 	msg_len += RPC_STR_SIZE(key_len);
    511   1.3      gwr 	m = m_get_len(msg_len);
    512   1.3      gwr 	if (m == NULL)
    513   1.3      gwr 		return ENOBUFS;
    514   1.1    glass 
    515   1.1    glass 	/*
    516   1.3      gwr 	 * Build request message.
    517   1.1    glass 	 */
    518   1.3      gwr 	p = mtod(m, u_char *);
    519   1.3      gwr 	bzero(p, msg_len);
    520   1.3      gwr 	/* client name (hostname) */
    521   1.3      gwr 	str = (struct rpc_string *)p;
    522   1.3      gwr 	str->len = htonl(cn_len);
    523   1.3      gwr 	bcopy(hostname, str->data, cn_len);
    524   1.3      gwr 	p += RPC_STR_SIZE(cn_len);
    525   1.3      gwr 	/* key name (root or swap) */
    526   1.3      gwr 	str = (struct rpc_string *)p;
    527   1.3      gwr 	str->len = htonl(key_len);
    528   1.3      gwr 	bcopy(key, str->data, key_len);
    529   1.3      gwr 
    530   1.3      gwr 	/* RPC: bootparam/getfile */
    531  1.11      gwr 	error = krpc_call(bpsin, BOOTPARAM_PROG, BOOTPARAM_VERS,
    532  1.11      gwr 			BOOTPARAM_GETFILE, &m, NULL);
    533   1.3      gwr 	if (error)
    534   1.1    glass 		return error;
    535   1.3      gwr 
    536   1.3      gwr 	/*
    537   1.3      gwr 	 * Parse result message.
    538   1.3      gwr 	 */
    539   1.3      gwr 	p = mtod(m, u_char *);
    540   1.3      gwr 	msg_len = m->m_len;
    541   1.3      gwr 
    542   1.3      gwr 	/* server name */
    543   1.3      gwr 	if (msg_len < sizeof(*str))
    544   1.3      gwr 		goto bad;
    545   1.3      gwr 	str = (struct rpc_string *)p;
    546   1.3      gwr 	sn_len = ntohl(str->len);
    547   1.3      gwr 	if (msg_len < sn_len)
    548   1.3      gwr 		goto bad;
    549   1.3      gwr 	if (sn_len >= MNAMELEN)
    550   1.3      gwr 		goto bad;
    551   1.3      gwr 	bcopy(str->data, serv_name, sn_len);
    552   1.3      gwr 	serv_name[sn_len] = '\0';
    553   1.3      gwr 	p += RPC_STR_SIZE(sn_len);
    554   1.3      gwr 	msg_len -= RPC_STR_SIZE(sn_len);
    555   1.3      gwr 
    556   1.3      gwr 	/* server IP address (mountd) */
    557   1.3      gwr 	if (msg_len < sizeof(*bia))
    558   1.3      gwr 		goto bad;
    559   1.3      gwr 	bia = (struct bp_inaddr *)p;
    560   1.3      gwr 	if (bia->atype != htonl(1))
    561   1.3      gwr 		goto bad;
    562   1.3      gwr 	sin = md_sin;
    563  1.10  mycroft 	bzero((caddr_t)sin, sizeof(*sin));
    564   1.3      gwr 	sin->sin_len = sizeof(*sin);
    565   1.1    glass 	sin->sin_family = AF_INET;
    566   1.3      gwr 	q = (u_char*) &sin->sin_addr;
    567   1.3      gwr 	*q++ = ntohl(bia->addr[0]);
    568   1.3      gwr 	*q++ = ntohl(bia->addr[1]);
    569   1.3      gwr 	*q++ = ntohl(bia->addr[2]);
    570   1.3      gwr 	*q++ = ntohl(bia->addr[3]);
    571   1.3      gwr 	p += sizeof(*bia);
    572   1.3      gwr 	msg_len -= sizeof(*bia);
    573   1.3      gwr 
    574   1.3      gwr 	/* server pathname */
    575   1.3      gwr 	if (msg_len < sizeof(*str))
    576   1.3      gwr 		goto bad;
    577   1.3      gwr 	str = (struct rpc_string *)p;
    578   1.3      gwr 	path_len = ntohl(str->len);
    579   1.3      gwr 	if (msg_len < path_len)
    580   1.3      gwr 		goto bad;
    581   1.3      gwr 	if (path_len >= MAXPATHLEN)
    582   1.3      gwr 		goto bad;
    583   1.3      gwr 	bcopy(str->data, pathname, path_len);
    584   1.3      gwr 	pathname[path_len] = '\0';
    585   1.3      gwr 	goto out;
    586   1.3      gwr 
    587   1.6  deraadt bad:
    588   1.3      gwr 	printf("nfs_boot: bootparam_getfile: bad reply\n");
    589   1.3      gwr 	error = EBADRPC;
    590   1.1    glass 
    591   1.6  deraadt out:
    592   1.3      gwr 	m_freem(m);
    593   1.3      gwr 	return(0);
    594   1.3      gwr }
    595   1.1    glass 
    596   1.1    glass 
    597   1.1    glass /*
    598   1.3      gwr  * RPC: mountd/mount
    599   1.3      gwr  * Given a server pathname, get an NFS file handle.
    600   1.3      gwr  * Also, sets sin->sin_port to the NFS service port.
    601   1.1    glass  */
    602   1.3      gwr static int
    603   1.3      gwr md_mount(mdsin, path, fhp)
    604   1.3      gwr 	struct sockaddr_in *mdsin;		/* mountd server address */
    605   1.3      gwr 	char *path;
    606   1.3      gwr 	u_char *fhp;
    607   1.1    glass {
    608   1.3      gwr 	/* The RPC structures */
    609   1.3      gwr 	struct rpc_string *str;
    610   1.3      gwr 	struct rdata {
    611   1.3      gwr 		u_long	errno;
    612   1.3      gwr 		u_char	fh[NFS_FHSIZE];
    613   1.3      gwr 	} *rdata;
    614   1.3      gwr 	struct mbuf *m;
    615   1.3      gwr 	int error, mlen, slen;
    616   1.3      gwr 
    617  1.11      gwr 	/* Get port number for MOUNTD. */
    618  1.11      gwr 	error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
    619  1.11      gwr 						 &mdsin->sin_port);
    620  1.11      gwr 	if (error) return error;
    621  1.11      gwr 
    622   1.3      gwr 	slen = strlen(path);
    623   1.3      gwr 	mlen = RPC_STR_SIZE(slen);
    624   1.3      gwr 
    625   1.3      gwr 	m = m_get_len(mlen);
    626   1.3      gwr 	if (m == NULL)
    627   1.3      gwr 		return ENOBUFS;
    628   1.3      gwr 	str = mtod(m, struct rpc_string *);
    629   1.3      gwr 	str->len = htonl(slen);
    630   1.3      gwr 	bcopy(path, str->data, slen);
    631   1.3      gwr 
    632   1.3      gwr 	/* Do RPC to mountd. */
    633  1.11      gwr 	error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
    634  1.11      gwr 			RPCMNT_MOUNT, &m, NULL);
    635   1.6  deraadt 	if (error)
    636   1.3      gwr 		return error;	/* message already freed */
    637   1.1    glass 
    638   1.3      gwr 	mlen = m->m_len;
    639   1.3      gwr 	if (mlen < sizeof(*rdata))
    640   1.3      gwr 		goto bad;
    641   1.3      gwr 	rdata = mtod(m, struct rdata *);
    642   1.3      gwr 	error = ntohl(rdata->errno);
    643   1.1    glass 	if (error)
    644   1.3      gwr 		goto bad;
    645   1.3      gwr 	bcopy(rdata->fh, fhp, NFS_FHSIZE);
    646   1.1    glass 
    647   1.3      gwr 	/* Set port number for NFS use. */
    648  1.11      gwr 	error = krpc_portmap(mdsin, NFS_PROG, NFS_VER2,
    649  1.11      gwr 						 &mdsin->sin_port);
    650   1.3      gwr 	goto out;
    651   1.1    glass 
    652   1.6  deraadt bad:
    653   1.3      gwr 	error = EBADRPC;
    654   1.1    glass 
    655   1.6  deraadt out:
    656   1.3      gwr 	m_freem(m);
    657   1.3      gwr 	return error;
    658   1.7   paulus }
    659   1.7   paulus 
    660   1.7   paulus #endif /* NETHER */
    661