2009-03-15 20:14:40 +03:00
|
|
|
/* $NetBSD: krpc_subr.c,v 1.37 2009/03/15 17:20:09 cegger Exp $ */
|
1994-06-29 10:39:25 +04:00
|
|
|
|
1994-04-18 10:18:05 +04:00
|
|
|
/*
|
1995-04-25 01:54:56 +04:00
|
|
|
* Copyright (c) 1995 Gordon Ross, Adam Glass
|
1994-04-18 10:18:05 +04:00
|
|
|
* 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)
|
|
|
|
*/
|
|
|
|
|
2001-11-10 13:59:08 +03:00
|
|
|
#include <sys/cdefs.h>
|
2009-03-15 20:14:40 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: krpc_subr.c,v 1.37 2009/03/15 17:20:09 cegger Exp $");
|
2001-11-10 13:59:08 +03:00
|
|
|
|
1994-04-18 10:18:05 +04:00
|
|
|
#include <sys/param.h>
|
1995-04-25 01:54:56 +04:00
|
|
|
#include <sys/systm.h>
|
1994-04-18 10:18:05 +04:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/reboot.h>
|
1995-04-25 01:54:56 +04:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
1994-04-18 10:18:05 +04:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <nfs/rpcv2.h>
|
1994-09-26 19:42:29 +03:00
|
|
|
#include <nfs/krpc.h>
|
1995-05-20 05:52:49 +04:00
|
|
|
#include <nfs/xdr_subs.h>
|
1997-10-01 00:46:17 +04:00
|
|
|
#include <nfs/nfsproto.h> /* XXX NFSX_V3FHMAX for next */
|
1998-03-01 05:20:01 +03:00
|
|
|
#include <nfs/nfs.h>
|
|
|
|
#include <nfs/nfsmount.h>
|
1997-10-01 00:46:17 +04:00
|
|
|
#include <nfs/nfsdiskless.h> /* XXX decl nfs_boot_sendrecv */
|
1994-04-18 10:18:05 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Kernel support for Sun RPC
|
|
|
|
*
|
|
|
|
* Used currently for bootstrapping in nfs diskless configurations.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generic RPC headers
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct auth_info {
|
1995-08-09 00:43:43 +04:00
|
|
|
u_int32_t authtype; /* auth type */
|
|
|
|
u_int32_t authlen; /* auth length */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct auth_unix {
|
|
|
|
int32_t ua_time;
|
|
|
|
int32_t ua_hostname; /* null */
|
|
|
|
int32_t ua_uid;
|
|
|
|
int32_t ua_gid;
|
|
|
|
int32_t ua_gidlist; /* null */
|
1994-04-18 10:18:05 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct rpc_call {
|
1995-04-25 01:54:56 +04:00
|
|
|
u_int32_t rp_xid; /* request transaction id */
|
|
|
|
int32_t rp_direction; /* call direction (0) */
|
|
|
|
u_int32_t rp_rpcvers; /* rpc version (2) */
|
|
|
|
u_int32_t rp_prog; /* program */
|
|
|
|
u_int32_t rp_vers; /* version */
|
|
|
|
u_int32_t rp_proc; /* procedure */
|
1995-08-09 00:43:43 +04:00
|
|
|
struct auth_info rpc_auth;
|
|
|
|
struct auth_unix rpc_unix;
|
|
|
|
struct auth_info rpc_verf;
|
1994-04-18 10:18:05 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct rpc_reply {
|
1995-12-20 02:07:11 +03:00
|
|
|
u_int32_t rp_xid; /* request transaction id */
|
1996-02-18 14:53:36 +03:00
|
|
|
int32_t rp_direction; /* call direction (1) */
|
|
|
|
int32_t rp_astatus; /* accept status (0: accepted) */
|
1994-04-18 10:18:05 +04:00
|
|
|
union {
|
1997-08-29 20:12:49 +04:00
|
|
|
/* rejected */
|
|
|
|
struct {
|
|
|
|
u_int32_t rej_stat;
|
|
|
|
u_int32_t rej_val1;
|
|
|
|
u_int32_t rej_val2;
|
|
|
|
} rpu_rej;
|
|
|
|
/* accepted */
|
1994-04-18 10:18:05 +04:00
|
|
|
struct {
|
1995-08-09 00:43:43 +04:00
|
|
|
struct auth_info rok_auth;
|
|
|
|
u_int32_t rok_status;
|
|
|
|
} rpu_rok;
|
1994-04-18 10:18:05 +04:00
|
|
|
} rp_u;
|
|
|
|
};
|
1997-08-29 20:12:49 +04:00
|
|
|
#define rp_rstat rp_u.rpu_rej.rej_stat
|
1995-08-09 00:43:43 +04:00
|
|
|
#define rp_auth rp_u.rpu_rok.rok_auth
|
|
|
|
#define rp_status rp_u.rpu_rok.rok_status
|
1994-04-18 10:18:05 +04:00
|
|
|
|
|
|
|
#define MIN_REPLY_HDR 16 /* xid, dir, astat, errno */
|
|
|
|
|
2009-03-14 17:45:51 +03:00
|
|
|
static int krpccheck(struct mbuf*, void*);
|
1994-06-13 19:28:55 +04:00
|
|
|
|
1994-04-18 10:18:05 +04:00
|
|
|
/*
|
|
|
|
* Call portmap to lookup a port number for a particular rpc program
|
1994-06-13 19:28:55 +04:00
|
|
|
* Returns non-zero error on failure.
|
1994-04-18 10:18:05 +04:00
|
|
|
*/
|
|
|
|
int
|
2009-03-15 00:04:01 +03:00
|
|
|
krpc_portmap(struct sockaddr_in *sin, u_int prog, u_int vers, u_int proto, u_int16_t *portp, struct lwp *l)
|
|
|
|
/* sin: server address */
|
|
|
|
/* prog, vers, proto: host order */
|
|
|
|
/* portp: network order */
|
1994-04-18 10:18:05 +04:00
|
|
|
{
|
|
|
|
struct sdata {
|
1995-12-20 02:07:11 +03:00
|
|
|
u_int32_t prog; /* call program */
|
|
|
|
u_int32_t vers; /* call version */
|
|
|
|
u_int32_t proto; /* call protocol */
|
|
|
|
u_int32_t port; /* call port (unused) */
|
1994-04-18 10:18:05 +04:00
|
|
|
} *sdata;
|
|
|
|
struct rdata {
|
1995-04-25 01:54:56 +04:00
|
|
|
u_int16_t pad;
|
|
|
|
u_int16_t port;
|
1994-04-18 10:18:05 +04:00
|
|
|
} *rdata;
|
|
|
|
struct mbuf *m;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/* The portmapper port is fixed. */
|
|
|
|
if (prog == PMAPPROG) {
|
|
|
|
*portp = htons(PMAPPORT);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1995-04-25 01:54:56 +04:00
|
|
|
m = m_get(M_WAIT, MT_DATA);
|
1994-04-18 10:18:05 +04:00
|
|
|
sdata = mtod(m, struct sdata *);
|
1995-04-25 01:54:56 +04:00
|
|
|
m->m_len = sizeof(*sdata);
|
1994-04-18 10:18:05 +04:00
|
|
|
|
|
|
|
/* Do the RPC to get it. */
|
1995-05-20 05:52:49 +04:00
|
|
|
sdata->prog = txdr_unsigned(prog);
|
|
|
|
sdata->vers = txdr_unsigned(vers);
|
1996-10-20 17:13:22 +04:00
|
|
|
sdata->proto = txdr_unsigned(proto);
|
1994-04-18 10:18:05 +04:00
|
|
|
sdata->port = 0;
|
|
|
|
|
1994-09-26 19:42:29 +03:00
|
|
|
sin->sin_port = htons(PMAPPORT);
|
|
|
|
error = krpc_call(sin, PMAPPROG, PMAPVERS,
|
2005-12-11 15:16:03 +03:00
|
|
|
PMAPPROC_GETPORT, &m, NULL, l);
|
2005-02-27 01:31:44 +03:00
|
|
|
if (error)
|
1994-04-18 10:18:05 +04:00
|
|
|
return error;
|
|
|
|
|
1995-04-25 01:54:56 +04:00
|
|
|
if (m->m_len < sizeof(*rdata)) {
|
|
|
|
m = m_pullup(m, sizeof(*rdata));
|
|
|
|
if (m == NULL)
|
|
|
|
return ENOBUFS;
|
|
|
|
}
|
1994-04-18 10:18:05 +04:00
|
|
|
rdata = mtod(m, struct rdata *);
|
|
|
|
*portp = rdata->port;
|
|
|
|
|
|
|
|
m_freem(m);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-15 20:14:40 +03:00
|
|
|
static int krpccheck(struct mbuf *m, void *context)
|
1997-10-01 00:46:17 +04:00
|
|
|
{
|
|
|
|
struct rpc_reply *reply;
|
|
|
|
|
|
|
|
/* 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 = 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);
|
|
|
|
}
|
|
|
|
|
1994-04-18 10:18:05 +04:00
|
|
|
/*
|
|
|
|
* Do a remote procedure call (RPC) and wait for its reply.
|
1994-09-26 19:42:29 +03:00
|
|
|
* If from_p is non-null, then we are doing broadcast, and
|
|
|
|
* the address from whence the response came is saved there.
|
1994-04-18 10:18:05 +04:00
|
|
|
*/
|
|
|
|
int
|
2009-03-15 00:04:01 +03:00
|
|
|
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 */
|
1994-04-18 10:18:05 +04:00
|
|
|
{
|
|
|
|
struct socket *so;
|
|
|
|
struct sockaddr_in *sin;
|
1994-09-26 19:42:29 +03:00
|
|
|
struct mbuf *m, *nam, *mhead, *from;
|
1994-04-18 10:18:05 +04:00
|
|
|
struct rpc_call *call;
|
|
|
|
struct rpc_reply *reply;
|
1997-10-01 00:46:17 +04:00
|
|
|
int error, len;
|
1995-04-25 01:54:56 +04:00
|
|
|
static u_int32_t xid = ~0xFF;
|
1995-12-20 02:07:11 +03:00
|
|
|
u_int16_t tport;
|
1994-04-18 10:18:05 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Validate address family.
|
|
|
|
* Sorry, this is INET specific...
|
|
|
|
*/
|
1994-09-26 19:42:29 +03:00
|
|
|
if (sa->sin_family != AF_INET)
|
1994-04-18 10:18:05 +04:00
|
|
|
return (EAFNOSUPPORT);
|
|
|
|
|
|
|
|
/* Free at end if not null. */
|
|
|
|
nam = mhead = NULL;
|
1994-09-26 19:42:29 +03:00
|
|
|
from = NULL;
|
1994-04-18 10:18:05 +04:00
|
|
|
|
|
|
|
/*
|
2001-06-12 19:17:10 +04:00
|
|
|
* Create socket and set its receive timeout.
|
1994-04-18 10:18:05 +04:00
|
|
|
*/
|
2008-04-24 15:38:36 +04:00
|
|
|
if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0, l, NULL)))
|
1994-04-18 10:18:05 +04:00
|
|
|
goto out;
|
|
|
|
|
1997-10-01 00:46:17 +04:00
|
|
|
if ((error = nfs_boot_setrecvtimo(so)))
|
1994-04-18 10:18:05 +04:00
|
|
|
goto out;
|
1994-09-26 19:42:29 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Enable broadcast if necessary.
|
|
|
|
*/
|
|
|
|
if (from_p) {
|
1997-10-01 00:46:17 +04:00
|
|
|
if ((error = nfs_boot_enbroadcast(so)))
|
1994-09-26 19:42:29 +03:00
|
|
|
goto out;
|
1994-04-18 10:18:05 +04:00
|
|
|
}
|
|
|
|
|
1994-08-12 02:25:32 +04:00
|
|
|
/*
|
|
|
|
* 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--;
|
2005-12-11 15:16:03 +03:00
|
|
|
error = nfs_boot_sobind_ipport(so, tport, l);
|
1994-08-12 02:25:32 +04:00
|
|
|
} while (error == EADDRINUSE &&
|
|
|
|
tport > IPPORT_RESERVED / 2);
|
|
|
|
if (error) {
|
1996-10-13 05:39:03 +04:00
|
|
|
printf("bind failed\n");
|
1994-08-12 02:25:32 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
1994-04-18 10:18:05 +04:00
|
|
|
/*
|
|
|
|
* Setup socket address for the server.
|
|
|
|
*/
|
|
|
|
nam = m_get(M_WAIT, MT_SONAME);
|
|
|
|
sin = mtod(nam, struct sockaddr_in *);
|
2007-03-04 08:59:00 +03:00
|
|
|
memcpy((void *)sin, (void *)sa,
|
1995-04-25 01:54:56 +04:00
|
|
|
(nam->m_len = sa->sin_len));
|
1994-04-18 10:18:05 +04:00
|
|
|
|
|
|
|
/*
|
1994-06-13 19:28:55 +04:00
|
|
|
* Prepend RPC message header.
|
1994-04-18 10:18:05 +04:00
|
|
|
*/
|
1995-04-25 01:54:56 +04:00
|
|
|
mhead = m_gethdr(M_WAIT, MT_DATA);
|
|
|
|
mhead->m_next = *data;
|
1994-04-18 10:18:05 +04:00
|
|
|
call = mtod(mhead, struct rpc_call *);
|
1995-04-25 01:54:56 +04:00
|
|
|
mhead->m_len = sizeof(*call);
|
2007-03-04 08:59:00 +03:00
|
|
|
memset((void *)call, 0, sizeof(*call));
|
1995-08-09 00:43:43 +04:00
|
|
|
/* rpc_call part */
|
1994-09-26 19:42:29 +03:00
|
|
|
xid++;
|
1995-05-20 05:52:49 +04:00
|
|
|
call->rp_xid = txdr_unsigned(xid);
|
1994-04-18 10:18:05 +04:00
|
|
|
/* call->rp_direction = 0; */
|
1995-05-20 05:52:49 +04:00
|
|
|
call->rp_rpcvers = txdr_unsigned(2);
|
|
|
|
call->rp_prog = txdr_unsigned(prog);
|
|
|
|
call->rp_vers = txdr_unsigned(vers);
|
|
|
|
call->rp_proc = txdr_unsigned(func);
|
1995-08-09 00:43:43 +04:00
|
|
|
/* rpc_auth part (auth_unix as root) */
|
|
|
|
call->rpc_auth.authtype = txdr_unsigned(RPCAUTH_UNIX);
|
|
|
|
call->rpc_auth.authlen = txdr_unsigned(sizeof(struct auth_unix));
|
|
|
|
/* rpc_verf part (auth_null) */
|
|
|
|
call->rpc_verf.authtype = 0;
|
|
|
|
call->rpc_verf.authlen = 0;
|
1994-04-18 10:18:05 +04:00
|
|
|
|
1995-04-25 01:54:56 +04:00
|
|
|
/*
|
|
|
|
* Setup packet header
|
|
|
|
*/
|
|
|
|
len = 0;
|
|
|
|
m = mhead;
|
|
|
|
while (m) {
|
|
|
|
len += m->m_len;
|
|
|
|
m = m->m_next;
|
|
|
|
}
|
|
|
|
mhead->m_pkthdr.len = len;
|
|
|
|
mhead->m_pkthdr.rcvif = NULL;
|
|
|
|
|
2004-05-23 02:52:13 +04:00
|
|
|
error = nfs_boot_sendrecv(so, nam, 0, mhead, krpccheck, &m, &from,
|
2005-12-11 15:16:03 +03:00
|
|
|
&xid, l);
|
1997-10-01 00:46:17 +04:00
|
|
|
if (error)
|
1997-08-29 20:12:49 +04:00
|
|
|
goto out;
|
|
|
|
|
1997-10-01 00:46:17 +04:00
|
|
|
/* 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;
|
1997-08-29 20:12:49 +04:00
|
|
|
}
|
1997-10-01 00:46:17 +04:00
|
|
|
goto out;
|
|
|
|
}
|
1994-09-26 19:42:29 +03:00
|
|
|
|
1997-10-01 00:46:17 +04:00
|
|
|
/* 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;
|
1997-08-29 20:12:49 +04:00
|
|
|
}
|
1997-10-01 00:46:17 +04:00
|
|
|
goto out;
|
|
|
|
}
|
1994-04-18 10:18:05 +04:00
|
|
|
|
|
|
|
/*
|
1997-08-29 20:12:49 +04:00
|
|
|
* OK, we have received a good reply!
|
1997-10-01 00:46:17 +04:00
|
|
|
* Get its length, then strip it off.
|
1994-06-13 19:28:55 +04:00
|
|
|
*/
|
1995-04-25 01:54:56 +04:00
|
|
|
len = sizeof(*reply);
|
1995-08-09 00:43:43 +04:00
|
|
|
if (reply->rp_auth.authtype != 0) {
|
|
|
|
len += fxdr_unsigned(u_int32_t, reply->rp_auth.authlen);
|
1994-04-18 10:18:05 +04:00
|
|
|
len = (len + 3) & ~3; /* XXX? */
|
|
|
|
}
|
|
|
|
m_adj(m, len);
|
1994-06-13 19:28:55 +04:00
|
|
|
|
1994-04-18 10:18:05 +04:00
|
|
|
/* result */
|
|
|
|
*data = m;
|
2006-04-15 05:25:54 +04:00
|
|
|
if (from_p && error == 0) {
|
1994-09-26 19:42:29 +03:00
|
|
|
*from_p = from;
|
|
|
|
from = NULL;
|
|
|
|
}
|
1994-04-18 10:18:05 +04:00
|
|
|
|
|
|
|
out:
|
|
|
|
if (nam) m_freem(nam);
|
|
|
|
if (mhead) m_freem(mhead);
|
1994-09-26 19:42:29 +03:00
|
|
|
if (from) m_freem(from);
|
1994-04-18 10:18:05 +04:00
|
|
|
soclose(so);
|
|
|
|
return error;
|
|
|
|
}
|
1995-04-25 01:54:56 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 *
|
2009-03-14 18:35:58 +03:00
|
|
|
xdr_string_encode(char *str, int len)
|
1995-04-25 01:54:56 +04:00
|
|
|
{
|
|
|
|
struct mbuf *m;
|
|
|
|
struct xdr_string *xs;
|
|
|
|
int dlen; /* padded string length */
|
|
|
|
int mlen; /* message length */
|
|
|
|
|
|
|
|
dlen = (len + 3) & ~3;
|
|
|
|
mlen = dlen + 4;
|
|
|
|
|
1996-06-07 04:48:10 +04:00
|
|
|
if (mlen > MCLBYTES) /* If too big, we just can't do it. */
|
|
|
|
return (NULL);
|
|
|
|
|
1995-04-25 01:54:56 +04:00
|
|
|
m = m_get(M_WAIT, MT_DATA);
|
|
|
|
if (mlen > MLEN) {
|
2003-02-26 09:31:08 +03:00
|
|
|
m_clget(m, M_WAIT);
|
1996-06-07 04:48:10 +04:00
|
|
|
if ((m->m_flags & M_EXT) == 0) {
|
|
|
|
(void) m_free(m); /* There can be only one. */
|
|
|
|
return (NULL);
|
|
|
|
}
|
1995-04-25 01:54:56 +04:00
|
|
|
}
|
|
|
|
xs = mtod(m, struct xdr_string *);
|
|
|
|
m->m_len = mlen;
|
1995-05-20 05:52:49 +04:00
|
|
|
xs->len = txdr_unsigned(len);
|
1998-08-10 00:51:08 +04:00
|
|
|
memcpy(xs->data, str, len);
|
1995-04-25 01:54:56 +04:00
|
|
|
return (m);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct mbuf *
|
2009-03-15 00:04:01 +03:00
|
|
|
xdr_string_decode(struct mbuf *m, char *str, int *len_p)
|
|
|
|
/* len_p: bufsize - 1 */
|
1995-04-25 01:54:56 +04:00
|
|
|
{
|
|
|
|
struct xdr_string *xs;
|
|
|
|
int mlen; /* message length */
|
|
|
|
int slen; /* string length */
|
|
|
|
|
|
|
|
if (m->m_len < 4) {
|
|
|
|
m = m_pullup(m, 4);
|
|
|
|
if (m == NULL)
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
xs = mtod(m, struct xdr_string *);
|
1995-05-20 05:52:49 +04:00
|
|
|
slen = fxdr_unsigned(u_int32_t, xs->len);
|
1995-04-25 01:54:56 +04:00
|
|
|
mlen = 4 + ((slen + 3) & ~3);
|
|
|
|
|
|
|
|
if (slen > *len_p)
|
|
|
|
slen = *len_p;
|
|
|
|
m_copydata(m, 4, slen, str);
|
|
|
|
m_adj(m, mlen);
|
|
|
|
|
|
|
|
str[slen] = '\0';
|
|
|
|
*len_p = slen;
|
|
|
|
|
|
|
|
return (m);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Inet address in RPC messages
|
|
|
|
* (Note, really four ints, NOT chars. Blech.)
|
|
|
|
*/
|
|
|
|
struct xdr_inaddr {
|
1995-05-20 05:52:49 +04:00
|
|
|
u_int32_t atype;
|
|
|
|
u_int32_t addr[4];
|
1995-04-25 01:54:56 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct mbuf *
|
2009-03-15 00:04:01 +03:00
|
|
|
xdr_inaddr_encode(struct in_addr *ia)
|
|
|
|
/* ia: already in network order */
|
1995-04-25 01:54:56 +04:00
|
|
|
{
|
|
|
|
struct mbuf *m;
|
|
|
|
struct xdr_inaddr *xi;
|
1995-05-20 05:52:49 +04:00
|
|
|
u_int8_t *cp;
|
|
|
|
u_int32_t *ip;
|
1995-04-25 01:54:56 +04:00
|
|
|
|
|
|
|
m = m_get(M_WAIT, MT_DATA);
|
|
|
|
xi = mtod(m, struct xdr_inaddr *);
|
|
|
|
m->m_len = sizeof(*xi);
|
1995-05-20 05:52:49 +04:00
|
|
|
xi->atype = txdr_unsigned(1);
|
1995-04-25 01:54:56 +04:00
|
|
|
ip = xi->addr;
|
1995-05-20 05:52:49 +04:00
|
|
|
cp = (u_int8_t *)&ia->s_addr;
|
|
|
|
*ip++ = txdr_unsigned(*cp++);
|
|
|
|
*ip++ = txdr_unsigned(*cp++);
|
|
|
|
*ip++ = txdr_unsigned(*cp++);
|
|
|
|
*ip++ = txdr_unsigned(*cp++);
|
1995-04-25 01:54:56 +04:00
|
|
|
|
|
|
|
return (m);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct mbuf *
|
2009-03-15 00:04:01 +03:00
|
|
|
xdr_inaddr_decode(struct mbuf *m, struct in_addr *ia)
|
|
|
|
/* ia: already in network order */
|
1995-04-25 01:54:56 +04:00
|
|
|
{
|
|
|
|
struct xdr_inaddr *xi;
|
1995-05-20 05:52:49 +04:00
|
|
|
u_int8_t *cp;
|
|
|
|
u_int32_t *ip;
|
1995-04-25 01:54:56 +04:00
|
|
|
|
|
|
|
if (m->m_len < sizeof(*xi)) {
|
|
|
|
m = m_pullup(m, sizeof(*xi));
|
|
|
|
if (m == NULL)
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
xi = mtod(m, struct xdr_inaddr *);
|
1995-05-20 05:52:49 +04:00
|
|
|
if (xi->atype != txdr_unsigned(1)) {
|
1995-04-25 01:54:56 +04:00
|
|
|
ia->s_addr = INADDR_ANY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
ip = xi->addr;
|
1995-05-20 05:52:49 +04:00
|
|
|
cp = (u_int8_t *)&ia->s_addr;
|
|
|
|
*cp++ = fxdr_unsigned(u_int8_t, *ip++);
|
|
|
|
*cp++ = fxdr_unsigned(u_int8_t, *ip++);
|
|
|
|
*cp++ = fxdr_unsigned(u_int8_t, *ip++);
|
|
|
|
*cp++ = fxdr_unsigned(u_int8_t, *ip++);
|
1995-04-25 01:54:56 +04:00
|
|
|
|
|
|
|
out:
|
|
|
|
m_adj(m, sizeof(*xi));
|
|
|
|
return (m);
|
|
|
|
}
|