/*
* Copyright (c) 1995 Gordon Ross, Adam Glass
* Copyright (c) 1992 Regents of the University of California.
* All rights reserved.
*
* This software was developed by the Computer Systems Engineering group
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
* contributed to Berkeley.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Lawrence Berkeley Laboratory and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* partially based on:
* libnetboot/rpc.c
* @(#) Header: rpc.c,v 1.12 93/09/28 08:31:56 leres Exp (LBL)
*/
/* Do the RPC to get it. */
sdata->prog = txdr_unsigned(prog);
sdata->vers = txdr_unsigned(vers);
sdata->proto = txdr_unsigned(proto);
sdata->port = 0;
/* Does the reply contain at least a header? */
if (m->m_pkthdr.len < MIN_REPLY_HDR)
return(-1);
if (m->m_len < sizeof(struct rpc_reply)) {
m = *mp = m_pullup(m, sizeof(struct rpc_reply));
if (m == NULL)
return(-1);
}
reply = mtod(m, struct rpc_reply *);
/* Is it the right reply? */
if (reply->rp_direction != txdr_unsigned(RPC_REPLY))
return(-1);
if (reply->rp_xid != txdr_unsigned(*(u_int32_t*)context))
return(-1);
return(0);
}
/*
* Do a remote procedure call (RPC) and wait for its reply.
* If from_p is non-null, then we are doing broadcast, and
* the address from whence the response came is saved there.
*/
int
krpc_call(struct sockaddr_in *sa, u_int prog, u_int vers, u_int func, struct mbuf **data, struct mbuf **from_p, struct lwp *l)
/* data: input/output */
/* from_p: output */
{
struct socket *so;
struct sockaddr_in sin;
struct mbuf *m, *mhead, *from;
struct rpc_call *call;
struct rpc_reply *reply;
int error, len;
static u_int32_t xid = ~0xFF;
u_int16_t tport;
/*
* Validate address family.
* Sorry, this is INET specific...
*/
if (sa->sin_family != AF_INET)
return (EAFNOSUPPORT);
/* Free at end if not null. */
mhead = NULL;
from = NULL;
/*
* Create socket and set its receive timeout.
*/
if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0, l, NULL)))
return error;
if ((error = nfs_boot_setrecvtimo(so)))
goto out;
/*
* Enable broadcast if necessary.
*/
if (from_p) {
if ((error = nfs_boot_enbroadcast(so)))
goto out;
}
/*
* Bind the local endpoint to a reserved port,
* because some NFS servers refuse requests from
* non-reserved (non-privileged) ports.
*/
tport = IPPORT_RESERVED;
do {
tport--;
error = nfs_boot_sobind_ipport(so, tport, l);
} while (error == EADDRINUSE &&
tport > IPPORT_RESERVED / 2);
if (error) {
printf("bind failed\n");
goto out;
}
/*
* Setup socket address for the server.
*/
sin = *sa;
/* m_pullup() was done in krpccheck() */
reply = mtod(m, struct rpc_reply *);
/* Was RPC accepted? (authorization OK) */
if (reply->rp_astatus != 0) {
/* Note: This is NOT an error code! */
error = fxdr_unsigned(u_int32_t, reply->rp_rstat);
switch (error) {
case RPC_MISMATCH:
/* .re_status = RPC_VERSMISMATCH; */
error = ERPCMISMATCH;
break;
case RPC_AUTHERR:
/* .re_status = RPC_AUTHERROR; */
error = EAUTH;
break;
default:
/* unexpected */
error = EBADRPC;
break;
}
goto out;
}
/* Did the call succeed? */
if (reply->rp_status != 0) {
/* Note: This is NOT an error code! */
error = fxdr_unsigned(u_int32_t, reply->rp_status);
switch (error) {
case RPC_PROGUNAVAIL:
error = EPROGUNAVAIL;
break;
case RPC_PROGMISMATCH:
error = EPROGMISMATCH;
break;
case RPC_PROCUNAVAIL:
error = EPROCUNAVAIL;
break;
case RPC_GARBAGE:
default:
error = EBADRPC;
}
goto out;
}
/*
* OK, we have received a good reply!
* Get its length, then strip it off.
*/
len = sizeof(*reply);
if (reply->rp_auth.authtype != 0) {
len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen);
len = (len + 3) & ~3; /* XXX? */
}
m_adj(m, len);
/* result */
*data = m;
if (from_p && error == 0) {
*from_p = from;
from = NULL;
}
/*
* eXternal Data Representation routines.
* (but with non-standard args...)
*/
/*
* String representation for RPC.
*/
struct xdr_string {
u_int32_t len; /* length without null or padding */
char data[4]; /* data (longer, of course) */
/* data is padded to a long-word boundary */
};
struct mbuf *
xdr_string_encode(char *str, int len)
{
struct mbuf *m;
struct xdr_string *xs;
int dlen; /* padded string length */
int mlen; /* message length */
dlen = (len + 3) & ~3;
mlen = dlen + 4;
if (mlen > MCLBYTES) /* If too big, we just can't do it. */
return (NULL);
m = m_get(M_WAIT, MT_DATA);
if (mlen > MLEN) {
m_clget(m, M_WAIT);
if ((m->m_flags & M_EXT) == 0) {
(void) m_free(m); /* There can be only one. */
return (NULL);
}
}
xs = mtod(m, struct xdr_string *);
m->m_len = mlen;
xs->len = txdr_unsigned(len);
memcpy(xs->data, str, len);
return (m);
}