#include <sys/param.h>
#include <sys/systm.h>
#include <sys/jail.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/proc.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/uio.h>
#include <net/if.h>
#include <net/vnet.h>
#include <netinet/in.h>
#include <rpc/types.h>
#include <rpc/auth.h>
#include <rpc/rpc_msg.h>
#include <nfs/krpc.h>
#include <nfs/xdr_subs.h>
struct auth_info {
u_int32_t authtype;
u_int32_t authlen;
};
struct auth_unix {
int32_t ua_time;
int32_t ua_hostname;
int32_t ua_uid;
int32_t ua_gid;
int32_t ua_gidlist;
};
struct krpc_call {
u_int32_t rp_xid;
int32_t rp_direction;
u_int32_t rp_rpcvers;
u_int32_t rp_prog;
u_int32_t rp_vers;
u_int32_t rp_proc;
struct auth_info rpc_auth;
struct auth_unix rpc_unix;
struct auth_info rpc_verf;
};
struct krpc_reply {
u_int32_t rp_xid;
int32_t rp_direction;
int32_t rp_astatus;
union {
u_int32_t rpu_errno;
struct {
struct auth_info rok_auth;
u_int32_t rok_status;
} rpu_rok;
} rp_u;
};
#define rp_errno rp_u.rpu_errno
#define rp_auth rp_u.rpu_rok.rok_auth
#define rp_status rp_u.rpu_rok.rok_status
#define MIN_REPLY_HDR 16
#define MAX_RESEND_DELAY 5
int
krpc_portmap(struct sockaddr_in *sin, u_int prog, u_int vers, u_int16_t *portp,
struct thread *td)
{
struct sdata {
u_int32_t prog;
u_int32_t vers;
u_int32_t proto;
u_int32_t port;
} *sdata;
struct rdata {
u_int16_t pad;
u_int16_t port;
} *rdata;
struct mbuf *m;
int error;
if (prog == PMAPPROG) {
*portp = htons(PMAPPORT);
return 0;
}
m = m_get(M_WAITOK, MT_DATA);
sdata = mtod(m, struct sdata *);
m->m_len = sizeof(*sdata);
sdata->prog = txdr_unsigned(prog);
sdata->vers = txdr_unsigned(vers);
sdata->proto = txdr_unsigned(IPPROTO_UDP);
sdata->port = 0;
sin->sin_port = htons(PMAPPORT);
error = krpc_call(sin, PMAPPROG, PMAPVERS,
PMAPPROC_GETPORT, &m, NULL, td);
if (error)
return error;
if (m->m_len < sizeof(*rdata)) {
m = m_pullup(m, sizeof(*rdata));
if (m == NULL)
return ENOBUFS;
}
rdata = mtod(m, struct rdata *);
*portp = rdata->port;
m_freem(m);
return 0;
}
int
krpc_call(struct sockaddr_in *sa, u_int prog, u_int vers, u_int func,
struct mbuf **data, struct sockaddr **from_p, struct thread *td)
{
struct socket *so;
struct sockaddr_in *sin, ssin;
struct sockaddr *from;
struct mbuf *m, *mhead;
struct krpc_call *call;
struct krpc_reply *reply;
struct sockopt sopt;
struct timeval tv;
struct uio auio;
int error, rcvflg, timo, secs, len;
static u_int32_t xid = ~0xFF;
u_int16_t tport;
u_int32_t saddr;
if (sa->sin_family != AF_INET)
return (EAFNOSUPPORT);
mhead = NULL;
from = NULL;
if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0, td->td_ucred, td)))
return error;
tv.tv_sec = 1;
tv.tv_usec = 0;
bzero(&sopt, sizeof sopt);
sopt.sopt_dir = SOPT_SET;
sopt.sopt_level = SOL_SOCKET;
sopt.sopt_name = SO_RCVTIMEO;
sopt.sopt_val = &tv;
sopt.sopt_valsize = sizeof tv;
if ((error = sosetopt(so, &sopt)) != 0)
goto out;
if (from_p) {
int on = 1;
sopt.sopt_name = SO_BROADCAST;
sopt.sopt_val = &on;
sopt.sopt_valsize = sizeof on;
if ((error = sosetopt(so, &sopt)) != 0)
goto out;
}
sin = &ssin;
bzero(sin, sizeof *sin);
sin->sin_len = sizeof(*sin);
sin->sin_family = AF_INET;
sin->sin_addr.s_addr = INADDR_ANY;
tport = IPPORT_RESERVED;
do {
tport--;
sin->sin_port = htons(tport);
error = sobind(so, (struct sockaddr *)sin, td);
} while (error == EADDRINUSE &&
tport > IPPORT_RESERVED / 2);
if (error) {
printf("bind failed\n");
goto out;
}
mhead = m_gethdr(M_WAITOK, MT_DATA);
mhead->m_next = *data;
call = mtod(mhead, struct krpc_call *);
mhead->m_len = sizeof(*call);
bzero((caddr_t)call, sizeof(*call));
xid++;
call->rp_xid = txdr_unsigned(xid);
call->rp_rpcvers = txdr_unsigned(2);
call->rp_prog = txdr_unsigned(prog);
call->rp_vers = txdr_unsigned(vers);
call->rp_proc = txdr_unsigned(func);
call->rpc_auth.authtype = txdr_unsigned(AUTH_UNIX);
call->rpc_auth.authlen = txdr_unsigned(sizeof(struct auth_unix));
call->rpc_verf.authtype = 0;
call->rpc_verf.authlen = 0;
m_fixhdr(mhead);
mhead->m_pkthdr.rcvif = NULL;
timo = 0;
for (;;) {
m = m_copym(mhead, 0, M_COPYALL, M_WAITOK);
error = sosend(so, (struct sockaddr *)sa, NULL, m,
NULL, 0, td);
if (error) {
printf("krpc_call: sosend: %d\n", error);
goto out;
}
m = NULL;
if (timo < MAX_RESEND_DELAY)
timo++;
else {
saddr = ntohl(sa->sin_addr.s_addr);
printf("RPC timeout for server %d.%d.%d.%d\n",
(saddr >> 24) & 255,
(saddr >> 16) & 255,
(saddr >> 8) & 255,
saddr & 255);
}
secs = timo;
while (secs > 0) {
if (from) {
free(from, M_SONAME);
from = NULL;
}
if (m) {
m_freem(m);
m = NULL;
}
bzero(&auio, sizeof(auio));
auio.uio_resid = len = 1<<16;
rcvflg = 0;
error = soreceive(so, &from, &auio, &m, NULL, &rcvflg);
if (error == EWOULDBLOCK) {
secs--;
continue;
}
if (error)
goto out;
len -= auio.uio_resid;
if (len < MIN_REPLY_HDR)
continue;
if (m->m_len < MIN_REPLY_HDR)
continue;
reply = mtod(m, struct krpc_reply *);
if (reply->rp_direction != txdr_unsigned(REPLY))
continue;
if (reply->rp_xid != txdr_unsigned(xid))
continue;
if (reply->rp_astatus != 0) {
error = fxdr_unsigned(u_int32_t, reply->rp_errno);
printf("rpc denied, error=%d\n", error);
continue;
}
if (reply->rp_status != 0) {
error = fxdr_unsigned(u_int32_t, reply->rp_status);
if (error == PROG_MISMATCH) {
error = EBADRPC;
goto out;
}
printf("rpc denied, status=%d\n", error);
continue;
}
goto gotreply;
}
}
error = ETIMEDOUT;
goto out;
gotreply:
len = sizeof(*reply);
if (m->m_len < len) {
m = m_pullup(m, len);
if (m == NULL) {
error = ENOBUFS;
goto out;
}
}
reply = mtod(m, struct krpc_reply *);
if (reply->rp_auth.authtype != 0) {
len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen);
len = (len + 3) & ~3;
}
m_adj(m, len);
*data = m;
if (from_p) {
*from_p = from;
from = NULL;
}
out:
if (mhead) m_freem(mhead);
if (from) free(from, M_SONAME);
soclose(so);
return error;
}
struct xdr_string {
u_int32_t len;
char data[4];
};
struct mbuf *
xdr_string_encode(char *str, int len)
{
struct mbuf *m;
struct xdr_string *xs;
int dlen;
int mlen;
dlen = (len + 3) & ~3;
mlen = dlen + 4;
if (mlen > MCLBYTES)
return (NULL);
m = m_get2(mlen, M_WAITOK, MT_DATA, 0);
xs = mtod(m, struct xdr_string *);
m->m_len = mlen;
xs->len = txdr_unsigned(len);
bcopy(str, xs->data, len);
return (m);
}