Home | History | Annotate | Line # | Download | only in nfs
krpc_subr.c revision 1.5
      1 /*	$NetBSD: krpc_subr.c,v 1.5 1994/08/11 22:25:32 gwr Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994 Gordon Ross, Adam Glass
      5  * Copyright (c) 1992 Regents of the University of California.
      6  * All rights reserved.
      7  *
      8  * This software was developed by the Computer Systems Engineering group
      9  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     10  * contributed to Berkeley.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Lawrence Berkeley Laboratory and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  * partially based on:
     41  *      libnetboot/rpc.c
     42  *               @(#) Header: rpc.c,v 1.12 93/09/28 08:31:56 leres Exp  (LBL)
     43  */
     44 
     45 #include <sys/param.h>
     46 #include <sys/conf.h>
     47 #include <sys/ioctl.h>
     48 #include <sys/proc.h>
     49 #include <sys/mount.h>
     50 #include <sys/mbuf.h>
     51 #include <sys/socket.h>
     52 #include <sys/systm.h>
     53 #include <sys/reboot.h>
     54 
     55 #include <net/if.h>
     56 #include <netinet/in.h>
     57 
     58 #include <nfs/rpcv2.h>
     59 
     60 /*
     61  * Kernel support for Sun RPC
     62  *
     63  * Used currently for bootstrapping in nfs diskless configurations.
     64  *
     65  * Note: will not work on variable-sized rpc args/results.
     66  *       implicit size-limit of an mbuf.
     67  */
     68 
     69 #define	PMAPPORT		111
     70 #define	PMAPPROG		100000
     71 #define	PMAPVERS		2
     72 #define	PMAPPROC_GETPORT	3
     73 
     74 /*
     75  * Generic RPC headers
     76  */
     77 
     78 struct auth_info {
     79 	int	rp_atype;		/* auth type */
     80 	u_long	rp_alen;		/* auth length */
     81 };
     82 
     83 struct rpc_call {
     84 	u_long	rp_xid;			/* request transaction id */
     85 	int 	rp_direction;	        /* call direction (0) */
     86 	u_long	rp_rpcvers;		/* rpc version (2) */
     87 	u_long	rp_prog;		/* program */
     88 	u_long	rp_vers;		/* version */
     89 	u_long	rp_proc;		/* procedure */
     90 	struct	auth_info rp_auth;
     91 	struct	auth_info rp_verf;
     92 };
     93 
     94 struct rpc_reply {
     95 	u_long	rp_xid;			/* request transaction id */
     96 	int	rp_direction;		/* call direction (1) */
     97 	int	rp_astatus;		/* accept status (0: accepted) */
     98 	union {
     99 		u_long	rpu_errno;
    100 		struct {
    101 			struct auth_info rp_auth;
    102 			u_long	rp_rstatus;		/* reply status */
    103 		} rpu_ok;
    104 	} rp_u;
    105 };
    106 
    107 #define MIN_REPLY_HDR 16	/* xid, dir, astat, errno */
    108 
    109 /*
    110  * What is the longest we will wait before re-sending a request?
    111  * Note this is also the frequency of "RPC timeout" messages.
    112  * The re-send loop count sup linearly to this maximum, so the
    113  * first complaint will happen after (1+2+3+4+5)=15 seconds.
    114  */
    115 #define	MAX_RESEND_DELAY 5	/* seconds */
    116 
    117 /*
    118  * Call portmap to lookup a port number for a particular rpc program
    119  * Returns non-zero error on failure.
    120  */
    121 int
    122 krpc_portmap(sa,  prog, vers, portp)
    123 	struct sockaddr *sa;		/* server address */
    124 	u_long prog, vers;	/* host order */
    125 	u_short *portp;		/* network order */
    126 {
    127 	struct sdata {
    128 		u_long	prog;		/* call program */
    129 		u_long	vers;		/* call version */
    130 		u_long	proto;		/* call protocol */
    131 		u_long	port;		/* call port (unused) */
    132 	} *sdata;
    133 	struct rdata {
    134 		u_short pad;
    135 		u_short port;
    136 	} *rdata;
    137 	struct mbuf *m;
    138 	int error;
    139 
    140 	/* The portmapper port is fixed. */
    141 	if (prog == PMAPPROG) {
    142 		*portp = htons(PMAPPORT);
    143 		return 0;
    144 	}
    145 
    146 	m = m_gethdr(M_WAIT, MT_DATA);
    147 	if (m == NULL)
    148 		return ENOBUFS;
    149 	m->m_len = sizeof(*sdata);
    150 	m->m_pkthdr.len = m->m_len;
    151 	sdata = mtod(m, struct sdata *);
    152 
    153 	/* Do the RPC to get it. */
    154 	sdata->prog = htonl(prog);
    155 	sdata->vers = htonl(vers);
    156 	sdata->proto = htonl(IPPROTO_UDP);
    157 	sdata->port = 0;
    158 
    159 	error = krpc_call(sa, PMAPPROG, PMAPVERS,
    160 					  PMAPPROC_GETPORT, &m);
    161 	if (error)
    162 		return error;
    163 
    164 	rdata = mtod(m, struct rdata *);
    165 	*portp = rdata->port;
    166 
    167 	m_freem(m);
    168 	return 0;
    169 }
    170 
    171 /*
    172  * Do a remote procedure call (RPC) and wait for its reply.
    173  */
    174 int
    175 krpc_call(sa, prog, vers, func, data)
    176 	struct sockaddr *sa;
    177 	u_long prog, vers, func;
    178 	struct mbuf **data;	/* input/output */
    179 {
    180 	struct socket *so;
    181 	struct sockaddr_in *sin;
    182 	struct timeval *tv;
    183 	struct mbuf *m, *nam, *mhead;
    184 	struct rpc_call *call;
    185 	struct rpc_reply *reply;
    186 	struct uio auio;
    187 	int error, rcvflg, timo, secs, len;
    188 	static u_long xid = ~0xFF;
    189 	u_short tport;
    190 
    191 	/*
    192 	 * Validate address family.
    193 	 * Sorry, this is INET specific...
    194 	 */
    195 	if (sa->sa_family != AF_INET)
    196 		return (EAFNOSUPPORT);
    197 
    198 	/* Free at end if not null. */
    199 	nam = mhead = NULL;
    200 
    201 	/*
    202 	 * Create socket and set its recieve timeout.
    203 	 */
    204 	if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0)))
    205 		goto out;
    206 
    207 	m = m_get(M_WAIT, MT_SOOPTS);
    208 	if (m == NULL) {
    209 		error = ENOBUFS;
    210 		goto out;
    211 	}
    212 	tv = mtod(m, struct timeval *);
    213 	m->m_len = sizeof(*tv);
    214 	tv->tv_sec = 1;
    215 	tv->tv_usec = 0;
    216 	if ((error = sosetopt(so, SOL_SOCKET, SO_RCVTIMEO, m)))
    217 		goto out;
    218 
    219 	/*
    220 	 * Bind the local endpoint to a reserved port,
    221 	 * because some NFS servers refuse requests from
    222 	 * non-reserved (non-privileged) ports.
    223 	 */
    224 	m = m_getclr(M_WAIT, MT_SONAME);
    225 	sin = mtod(m, struct sockaddr_in *);
    226 	sin->sin_len = m->m_len = sizeof(*sin);
    227 	sin->sin_family = AF_INET;
    228 	sin->sin_addr.s_addr = INADDR_ANY;
    229 	tport = IPPORT_RESERVED;
    230 	do {
    231 		tport--;
    232 		sin->sin_port = htons(tport);
    233 		error = sobind(so, m);
    234 	} while (error == EADDRINUSE &&
    235 			 tport > IPPORT_RESERVED / 2);
    236 	m_freem(m);
    237 	if (error) {
    238 		printf("bind failed\n");
    239 		goto out;
    240 	}
    241 
    242 	/*
    243 	 * Setup socket address for the server.
    244 	 */
    245 	nam = m_get(M_WAIT, MT_SONAME);
    246 	if (nam == NULL) {
    247 		error = ENOBUFS;
    248 		goto out;
    249 	}
    250 	sin = mtod(nam, struct sockaddr_in *);
    251 	bcopy((caddr_t)sa, (caddr_t)sin, (nam->m_len = sa->sa_len));
    252 
    253 	/*
    254 	 * Set the port number that the request will use.
    255 	 */
    256 	if ((error = krpc_portmap(sa, prog, vers, &sin->sin_port)))
    257 		goto out;
    258 
    259 	/*
    260 	 * Prepend RPC message header.
    261 	 */
    262 	m = *data;
    263 	*data = NULL;
    264 #ifdef	DIAGNOSTIC
    265 	if ((m->m_flags & M_PKTHDR) == 0)
    266 		panic("krpc_call: send data w/o pkthdr");
    267 	if (m->m_pkthdr.len < m->m_len)
    268 		panic("krpc_call: pkthdr.len not set");
    269 #endif
    270 	mhead = m_prepend(m, sizeof(*call), M_WAIT);
    271 	if (mhead == NULL) {
    272 		error = ENOBUFS;
    273 		goto out;
    274 	}
    275 	mhead->m_pkthdr.len += sizeof(*call);
    276 	mhead->m_pkthdr.rcvif = NULL;
    277 
    278 	/*
    279 	 * Fill in the RPC header
    280 	 */
    281 	call = mtod(mhead, struct rpc_call *);
    282 	bzero((caddr_t)call, sizeof(*call));
    283 	call->rp_xid = ++xid;	/* no need to put in network order */
    284 	/* call->rp_direction = 0; */
    285 	call->rp_rpcvers = htonl(2);
    286 	call->rp_prog = htonl(prog);
    287 	call->rp_vers = htonl(vers);
    288 	call->rp_proc = htonl(func);
    289 	/* call->rp_auth = 0; */
    290 	/* call->rp_verf = 0; */
    291 
    292 	/*
    293 	 * Send it, repeatedly, until a reply is received,
    294 	 * but delay each re-send by an increasing amount.
    295 	 * If the delay hits the maximum, start complaining.
    296 	 */
    297 	timo = 0;
    298 	for (;;) {
    299 		/* Send RPC request (or re-send). */
    300 		m = m_copym(mhead, 0, M_COPYALL, M_WAIT);
    301 		if (m == NULL) {
    302 			error = ENOBUFS;
    303 			goto out;
    304 		}
    305 		error = sosend(so, nam, NULL, m, NULL, 0);
    306 		if (error) {
    307 			printf("krpc_call: sosend: %d\n", error);
    308 			goto out;
    309 		}
    310 		m = NULL;
    311 
    312 		/* Determine new timeout. */
    313 		if (timo < MAX_RESEND_DELAY)
    314 			timo++;
    315 		else
    316 			printf("RPC timeout for server 0x%x\n",
    317 			       ntohl(sin->sin_addr.s_addr));
    318 
    319 		/*
    320 		 * Wait for up to timo seconds for a reply.
    321 		 * The socket receive timeout was set to 1 second.
    322 		 */
    323 		secs = timo;
    324 		while (secs > 0) {
    325 			auio.uio_resid = len = 1<<16;
    326 			rcvflg = 0;
    327 			error = soreceive(so, NULL, &auio, &m, NULL, &rcvflg);
    328 			if (error == EWOULDBLOCK) {
    329 				secs--;
    330 				continue;
    331 			}
    332 			if (error)
    333 				goto out;
    334 			len -= auio.uio_resid;
    335 
    336 			/* Is the reply complete and the right one? */
    337 			if (len < MIN_REPLY_HDR) {
    338 				m_freem(m);
    339 				continue;
    340 			}
    341 			if (m->m_len < MIN_REPLY_HDR) {
    342 				m = m_pullup(m, MIN_REPLY_HDR);
    343 				if (!m)
    344 					continue;
    345 			}
    346 			reply = mtod(m, struct rpc_reply *);
    347 			if ((reply->rp_direction == htonl(RPC_REPLY)) &&
    348 				(reply->rp_xid == xid))
    349 				goto gotreply;	/* break two levels */
    350 		} /* while secs */
    351 	} /* forever send/receive */
    352 
    353 	error = ETIMEDOUT;
    354 	goto out;
    355 
    356  gotreply:
    357 
    358 	/*
    359 	 * Pull as much as we can into first mbuf, to make
    360 	 * result buffer contiguous.  Note that if the entire
    361 	 * resulte won't fit into one mbuf, you're out of luck.
    362 	 * XXX - Should not rely on making the entire reply
    363 	 * contiguous (fix callers instead). -gwr
    364 	 */
    365 #ifdef	DIAGNOSTIC
    366 	if ((m->m_flags & M_PKTHDR) == 0)
    367 		panic("krpc_call: received pkt w/o header?");
    368 #endif
    369 	len = m->m_pkthdr.len;
    370 	if (m->m_len < len) {
    371 		m = m_pullup(m, len);
    372 		if (m == NULL) {
    373 			error = ENOBUFS;
    374 			goto out;
    375 		}
    376 	}
    377 	reply = mtod(m, struct rpc_reply *);
    378 
    379 	/*
    380 	 * Check RPC acceptance and status.
    381 	 */
    382 	if (reply->rp_astatus != 0) {
    383 		error = ntohl(reply->rp_u.rpu_errno);
    384 		printf("rpc denied, error=%d\n", error);
    385 		m_freem(m);
    386 		goto out;
    387 	}
    388 	if ((error = ntohl(reply->rp_u.rpu_ok.rp_rstatus)) != 0) {
    389 		printf("rpc status=%d\n", error);
    390 		m_freem(m);
    391 		goto out;
    392 	}
    393 
    394 	/*
    395 	 * Strip RPC header
    396 	 */
    397 	len = sizeof(*reply);
    398 	if (reply->rp_u.rpu_ok.rp_auth.rp_atype != 0) {
    399 		len += ntohl(reply->rp_u.rpu_ok.rp_auth.rp_alen);
    400 		len = (len + 3) & ~3; /* XXX? */
    401 	}
    402 	m_adj(m, len);
    403 
    404 	/* result */
    405 	*data = m;
    406 
    407  out:
    408 	if (nam) m_freem(nam);
    409 	if (mhead) m_freem(mhead);
    410 	soclose(so);
    411 	return error;
    412 }
    413