199767f8919635c4928607450d9e0abb932109ceToomas Soome/* $NetBSD: rpc.c,v 1.18 1998/01/23 19:27:45 thorpej Exp $ */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1992 Regents of the University of California.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 *
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 * are met:
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 *
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 *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * @(#) Header: rpc.c,v 1.12 93/09/28 08:31:56 leres Exp (LBL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome__FBSDID("$FreeBSD$");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
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 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/param.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/socket.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <netinet/in.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <netinet/in_systm.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <string.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "rpcv2.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "stand.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "net.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "netif.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "rpc.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct auth_info {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int32_t authtype; /* auth type */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t authlen; /* auth length */
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct auth_unix {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int32_t ua_time;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int32_t ua_hostname; /* null */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int32_t ua_uid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int32_t ua_gid;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int32_t ua_gidlist; /* null */
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct rpc_call {
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t rp_xid; /* request transaction id */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int32_t rp_direction; /* call direction (0) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t rp_rpcvers; /* rpc version (2) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t rp_prog; /* program */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t rp_vers; /* version */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t rp_proc; /* procedure */
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct rpc_reply {
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t rp_xid; /* request transaction id */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int32_t rp_direction; /* call direction (1) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int32_t rp_astatus; /* accept status (0: accepted) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome union {
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t rpu_errno;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct auth_info rok_auth;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t rok_status;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } rpu_rok;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } rp_u;
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint rpc_xid;
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint rpc_port = 0x400; /* predecrement */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Make a rpc call; return length of answer
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Note: Caller must leave room for headers.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomessize_t
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{
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssize_t cc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct auth_info *auth;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct rpc_call *call;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct rpc_reply *reply;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *send_head, *send_tail;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *recv_head, *recv_tail;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long x;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int port; /* host order */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef RPC_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("rpc_call: prog=0x%x vers=%d proc=%d\n",
199767f8919635c4928607450d9e0abb932109ceToomas Soome prog, vers, proc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome port = rpc_getport(d, prog, vers);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (port == -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome d->destport = htons(port);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Prepend authorization stuff and headers.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Note, must prepend things in reverse order.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_head = sdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_tail = (char *)sdata + slen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Auth verifier is always auth_null */
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_head -= sizeof(*auth);
199767f8919635c4928607450d9e0abb932109ceToomas Soome auth = (struct auth_info *)send_head;
199767f8919635c4928607450d9e0abb932109ceToomas Soome auth->authtype = htonl(RPCAUTH_NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome auth->authlen = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if 1
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Auth credentials: always auth unix (as root) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_head -= sizeof(struct auth_unix);
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(send_head, sizeof(struct auth_unix));
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_head -= sizeof(*auth);
199767f8919635c4928607450d9e0abb932109ceToomas Soome auth = (struct auth_info *)send_head;
199767f8919635c4928607450d9e0abb932109ceToomas Soome auth->authtype = htonl(RPCAUTH_UNIX);
199767f8919635c4928607450d9e0abb932109ceToomas Soome auth->authlen = htonl(sizeof(struct auth_unix));
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Auth credentials: always auth_null (XXX OK?) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_head -= sizeof(*auth);
199767f8919635c4928607450d9e0abb932109ceToomas Soome auth = send_head;
199767f8919635c4928607450d9e0abb932109ceToomas Soome auth->authtype = htonl(RPCAUTH_NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome auth->authlen = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* RPC call structure. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome send_head -= sizeof(*call);
199767f8919635c4928607450d9e0abb932109ceToomas Soome call = (struct rpc_call *)send_head;
199767f8919635c4928607450d9e0abb932109ceToomas Soome rpc_xid++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome call->rp_xid = htonl(rpc_xid);
199767f8919635c4928607450d9e0abb932109ceToomas Soome call->rp_direction = htonl(RPC_CALL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome call->rp_rpcvers = htonl(RPC_VER2);
199767f8919635c4928607450d9e0abb932109ceToomas Soome call->rp_prog = htonl(prog);
199767f8919635c4928607450d9e0abb932109ceToomas Soome call->rp_vers = htonl(vers);
199767f8919635c4928607450d9e0abb932109ceToomas Soome call->rp_proc = htonl(proc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Make room for the rpc_reply header. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome recv_head = rdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome recv_tail = (char *)rdata + rlen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome recv_head -= sizeof(*reply);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome cc = sendrecv(d,
199767f8919635c4928607450d9e0abb932109ceToomas Soome sendudp, send_head, send_tail - send_head,
199767f8919635c4928607450d9e0abb932109ceToomas Soome recvrpc, recv_head, recv_tail - recv_head);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef RPC_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("callrpc: cc=%ld rlen=%lu\n", (long)cc, (u_long)rlen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc == -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc <= sizeof(*reply)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = EBADRPC;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome recv_tail = recv_head + cc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Check the RPC reply status.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The xid, dir, astatus were already checked.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome reply = (struct rpc_reply *)recv_head;
199767f8919635c4928607450d9e0abb932109ceToomas Soome auth = &reply->rp_u.rpu_rok.rok_auth;
199767f8919635c4928607450d9e0abb932109ceToomas Soome x = ntohl(auth->authlen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (x != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef RPC_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("callrpc: reply auth != NULL\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = EBADRPC;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome x = ntohl(reply->rp_u.rpu_rok.rok_status);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (x != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("callrpc: error = %ld\n", (long)x);
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = EBADRPC;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome recv_head += sizeof(*reply);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (ssize_t)(recv_tail - recv_head);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
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 Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic ssize_t
199767f8919635c4928607450d9e0abb932109ceToomas Soomerecvrpc(struct iodesc *d, void *pkt, size_t len, time_t tleft)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct rpc_reply *reply;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssize_t n;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int x;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef RPC_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("recvrpc: called len=%lu\n", (u_long)len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome n = readudp(d, pkt, len, tleft);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (n <= (4 * 4))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome reply = (struct rpc_reply *)pkt;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome x = ntohl(reply->rp_xid);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (x != rpc_xid) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef RPC_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("recvrpc: rp_xid %d != xid %d\n", x, rpc_xid);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome return -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome x = ntohl(reply->rp_direction);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (x != RPC_REPLY) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef RPC_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("recvrpc: rp_direction %d != REPLY\n", x);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome return -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome x = ntohl(reply->rp_astatus);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (x != RPC_MSGACCEPTED) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = ntohl(reply->rp_u.rpu_errno);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("recvrpc: reject, astat=%d, errno=%d\n", x, errno);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Return data count (thus indicating success) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (n);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Given a pointer to a reply just received,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * dig out the IP address/port from the headers.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomerpc_fromaddr(void *pkt, struct in_addr *addr, u_short *port)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct hackhdr {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Tail of IP header: just IP addresses */
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long ip_src;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long ip_dst;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* UDP header: */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t uh_sport; /* source port */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t uh_dport; /* destination port */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int16_t uh_ulen; /* udp length */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t uh_sum; /* udp checksum */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* RPC reply header: */
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct rpc_reply rpc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } *hhdr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome hhdr = ((struct hackhdr *)pkt) - 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr->s_addr = hhdr->ip_src;
199767f8919635c4928607450d9e0abb932109ceToomas Soome *port = hhdr->uh_sport;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * RPC Portmapper cache
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define PMAP_NUM 8 /* need at most 5 pmap entries */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint rpc_pmap_num;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct pmap_list {
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct in_addr addr; /* server, net order */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int prog; /* host order */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int vers; /* host order */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int port; /* host order */
199767f8919635c4928607450d9e0abb932109ceToomas Soome} rpc_pmap_list[PMAP_NUM];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
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 Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomerpc_pmap_getcache(struct in_addr addr, u_int prog, u_int vers)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct pmap_list *pl;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (pl = rpc_pmap_list; pl < &rpc_pmap_list[rpc_pmap_num]; pl++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (pl->addr.s_addr == addr.s_addr &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome pl->prog == prog && pl->vers == vers )
199767f8919635c4928607450d9e0abb932109ceToomas Soome {
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (pl->port);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
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 Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomerpc_pmap_putcache(struct in_addr addr, u_int prog, u_int vers, int port)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct pmap_list *pl;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Don't overflow cache... */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rpc_pmap_num >= PMAP_NUM) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* ... just re-use the last entry. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome rpc_pmap_num = PMAP_NUM - 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef RPC_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("rpc_pmap_putcache: cache overflow\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome pl = &rpc_pmap_list[rpc_pmap_num];
199767f8919635c4928607450d9e0abb932109ceToomas Soome rpc_pmap_num++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Cache answer */
199767f8919635c4928607450d9e0abb932109ceToomas Soome pl->addr = addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pl->prog = prog;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pl->vers = vers;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pl->port = port;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
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 Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomerpc_getport(struct iodesc *d, n_long prog, n_long vers)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct args {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long prog; /* call program */
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long vers; /* call version */
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long proto; /* call protocol */
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long port; /* call port (unused) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome } *args;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct res {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long port;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } *res;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct args d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } sdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long h[RPC_HEADER_WORDS];
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct res d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome n_long pad;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } rdata;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ssize_t cc;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int port;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef RPC_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s: prog=0x%x vers=%d\n", __func__, prog, vers);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* This one is fixed forever. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (prog == PMAPPROG) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome port = PMAPPORT;
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Try for cached answer first */
199767f8919635c4928607450d9e0abb932109ceToomas Soome port = rpc_pmap_getcache(d->destip, prog, vers);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (port != -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome goto out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome args = &sdata.d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->prog = htonl(prog);
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->vers = htonl(vers);
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->proto = htonl(IPPROTO_UDP);
199767f8919635c4928607450d9e0abb932109ceToomas Soome args->port = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome res = &rdata.d;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome cc = rpc_call(d, PMAPPROG, PMAPVERS, PMAPPROC_GETPORT,
199767f8919635c4928607450d9e0abb932109ceToomas Soome args, sizeof(*args), res, sizeof(*res));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cc < sizeof(*res)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("getport: %s", strerror(errno));
199767f8919635c4928607450d9e0abb932109ceToomas Soome errno = EBADRPC;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome port = (int)ntohl(res->port);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome rpc_pmap_putcache(d->destip, prog, vers, port);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeout:
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifdef RPC_DEBUG
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (debug)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("%s: port=%u\n", __func__, port);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (port);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}