199767f8919635c4928607450d9e0abb932109ceToomas Soome/* $NetBSD: rpc.c,v 1.18 1998/01/23 19:27:45 thorpej Exp $ */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1992 Regents of the University of California.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This software was developed by the Computer Systems Engineering group
199767f8919635c4928607450d9e0abb932109ceToomas Soome * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
199767f8919635c4928607450d9e0abb932109ceToomas Soome * contributed to Berkeley.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 4. Neither the name of the University nor the names of its contributors
199767f8919635c4928607450d9e0abb932109ceToomas Soome * may be used to endorse or promote products derived from this software
199767f8919635c4928607450d9e0abb932109ceToomas Soome * without specific prior written permission.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * @(#) Header: rpc.c,v 1.12 93/09/28 08:31:56 leres Exp (LBL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * RPC functions used by NFS and bootparams.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Note that bootparams requires the ability to find out the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * address of the server from which its response has come.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This is supported by keeping the IP/UDP headers in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * buffer space provided by the caller. (See rpc_fromaddr)
199767f8919635c4928607450d9e0abb932109ceToomas Soome int32_t rp_astatus; /* accept status (0: accepted) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Local forwards */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic ssize_t recvrpc(struct iodesc *, void *, size_t, time_t);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int rpc_getport(struct iodesc *, n_long, n_long);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Make a rpc call; return length of answer
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Note: Caller must leave room for headers.
199767f8919635c4928607450d9e0abb932109ceToomas Soomerpc_call(struct iodesc *d, n_long prog, n_long vers, n_long proc,
199767f8919635c4928607450d9e0abb932109ceToomas Soome void *sdata, size_t slen, void *rdata, size_t rlen)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("rpc_call: prog=0x%x vers=%d proc=%d\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Prepend authorization stuff and headers.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Note, must prepend things in reverse order.
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Auth verifier is always auth_null */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Auth credentials: always auth unix (as root) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome auth->authlen = htonl(sizeof(struct auth_unix));
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Auth credentials: always auth_null (XXX OK?) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* RPC call structure. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Make room for the rpc_reply header. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("callrpc: cc=%ld rlen=%lu\n", (long)cc, (u_long)rlen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Check the RPC reply status.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The xid, dir, astatus were already checked.
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (x != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (x != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Returns true if packet is the one we're waiting for.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This just checks the XID, direction, acceptance.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Remaining checks are done by callrpc
199767f8919635c4928607450d9e0abb932109ceToomas Soomerecvrpc(struct iodesc *d, void *pkt, size_t len, time_t tleft)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("recvrpc: called len=%lu\n", (u_long)len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("recvrpc: rp_xid %d != xid %d\n", x, rpc_xid);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("recvrpc: rp_direction %d != REPLY\n", x);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("recvrpc: reject, astat=%d, errno=%d\n", x, errno);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Return data count (thus indicating success) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Given a pointer to a reply just received,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * dig out the IP address/port from the headers.
199767f8919635c4928607450d9e0abb932109ceToomas Soomerpc_fromaddr(void *pkt, struct in_addr *addr, u_short *port)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Tail of IP header: just IP addresses */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* UDP header: */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* RPC reply header: */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * RPC Portmapper cache
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define PMAP_NUM 8 /* need at most 5 pmap entries */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * return port number in host order, or -1.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * arguments are:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * addr .. server, net order.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * prog .. host order.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * vers .. host order.
199767f8919635c4928607450d9e0abb932109ceToomas Soomerpc_pmap_getcache(struct in_addr addr, u_int prog, u_int vers)
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (pl = rpc_pmap_list; pl < &rpc_pmap_list[rpc_pmap_num]; pl++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome * arguments are:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * addr .. server, net order.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * prog .. host order.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * vers .. host order.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * port .. host order.
199767f8919635c4928607450d9e0abb932109ceToomas Soomerpc_pmap_putcache(struct in_addr addr, u_int prog, u_int vers, int port)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Don't overflow cache... */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* ... just re-use the last entry. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Cache answer */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Request a port number from the port mapper.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Returns the port in host order.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * prog and vers are host order.
199767f8919635c4928607450d9e0abb932109ceToomas Soomerpc_getport(struct iodesc *d, n_long prog, n_long vers)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s: prog=0x%x vers=%d\n", __func__, prog, vers);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* This one is fixed forever. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Try for cached answer first */
199767f8919635c4928607450d9e0abb932109ceToomas Soome port = rpc_pmap_getcache(d->destip, prog, vers);
199767f8919635c4928607450d9e0abb932109ceToomas Soome cc = rpc_call(d, PMAPPROG, PMAPVERS, PMAPPROC_GETPORT,
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);