1 /* $NetBSD: rpc_prot.c,v 1.16 2000/06/02 23:11:13 fvdl Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 2009, Sun Microsystems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * - Neither the name of Sun Microsystems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char *sccsid2 = "@(#)rpc_prot.c 1.36 87/08/11 Copyr 1984 Sun Micro";
35 static char *sccsid = "@(#)rpc_prot.c 2.3 88/08/07 4.0 RPCSRC";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
43 * Copyright (C) 1984, Sun Microsystems, Inc.
45 * This set of routines implements the rpc message definition,
46 * its serializer and some common rpc utility routines.
47 * The routines are meant for various implementations of rpc -
48 * they are NOT for the rpc client or rpc service implementations!
49 * Because authentication stuff is easy and is part of rpc, the opaque
50 * routines are also in this program.
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/malloc.h>
58 #include <rpc/types.h>
62 #include <rpc/rpc_msg.h>
64 MALLOC_DEFINE(M_RPC, "rpc", "Remote Procedure Call");
66 #define assert(exp) KASSERT(exp, ("bad arguments"))
68 static enum clnt_stat accepted(enum accept_stat, struct rpc_err *);
69 static enum clnt_stat rejected(enum reject_stat, struct rpc_err *);
71 /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
73 struct opaque_auth _null_auth;
76 * XDR an opaque authentication struct
80 xdr_opaque_auth(XDR *xdrs, struct opaque_auth *ap)
86 if (xdr_enum(xdrs, &(ap->oa_flavor)))
87 return (xdr_bytes(xdrs, &ap->oa_base,
88 &ap->oa_length, MAX_AUTH_BYTES));
92 /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
95 * XDR the MSG_ACCEPTED part of a reply message union
98 xdr_accepted_reply(XDR *xdrs, struct accepted_reply *ar)
100 enum accept_stat *par_stat;
102 assert(xdrs != NULL);
105 par_stat = &ar->ar_stat;
107 /* personalized union, rather than calling xdr_union */
108 if (! xdr_opaque_auth(xdrs, &(ar->ar_verf)))
110 if (! xdr_enum(xdrs, (enum_t *) par_stat))
112 switch (ar->ar_stat) {
115 if (ar->ar_results.proc != (xdrproc_t) xdr_void)
116 return ((*(ar->ar_results.proc))(xdrs,
117 ar->ar_results.where));
122 if (! xdr_uint32_t(xdrs, &(ar->ar_vers.low)))
124 return (xdr_uint32_t(xdrs, &(ar->ar_vers.high)));
132 return (TRUE); /* TRUE => open ended set of problems */
136 * XDR the MSG_DENIED part of a reply message union
139 xdr_rejected_reply(XDR *xdrs, struct rejected_reply *rr)
141 enum reject_stat *prj_stat;
142 enum auth_stat *prj_why;
144 assert(xdrs != NULL);
147 prj_stat = &rr->rj_stat;
149 /* personalized union, rather than calling xdr_union */
150 if (! xdr_enum(xdrs, (enum_t *) prj_stat))
152 switch (rr->rj_stat) {
155 if (! xdr_uint32_t(xdrs, &(rr->rj_vers.low)))
157 return (xdr_uint32_t(xdrs, &(rr->rj_vers.high)));
160 prj_why = &rr->rj_why;
161 return (xdr_enum(xdrs, (enum_t *) prj_why));
168 static const struct xdr_discrim reply_dscrm[3] = {
169 { (int)MSG_ACCEPTED, (xdrproc_t)xdr_accepted_reply },
170 { (int)MSG_DENIED, (xdrproc_t)xdr_rejected_reply },
171 { __dontcare__, NULL_xdrproc_t } };
174 * XDR a reply message
177 xdr_replymsg(XDR *xdrs, struct rpc_msg *rmsg)
180 enum msg_type *prm_direction;
181 enum reply_stat *prp_stat;
183 assert(xdrs != NULL);
184 assert(rmsg != NULL);
186 if (xdrs->x_op == XDR_DECODE) {
187 buf = XDR_INLINE(xdrs, 3 * BYTES_PER_XDR_UNIT);
189 rmsg->rm_xid = IXDR_GET_UINT32(buf);
190 rmsg->rm_direction = IXDR_GET_ENUM(buf, enum msg_type);
191 if (rmsg->rm_direction != REPLY) {
194 rmsg->rm_reply.rp_stat =
195 IXDR_GET_ENUM(buf, enum reply_stat);
196 if (rmsg->rm_reply.rp_stat == MSG_ACCEPTED)
197 return (xdr_accepted_reply(xdrs,
198 &rmsg->acpted_rply));
199 else if (rmsg->rm_reply.rp_stat == MSG_DENIED)
200 return (xdr_rejected_reply(xdrs,
201 &rmsg->rjcted_rply));
207 prm_direction = &rmsg->rm_direction;
208 prp_stat = &rmsg->rm_reply.rp_stat;
211 xdr_uint32_t(xdrs, &(rmsg->rm_xid)) &&
212 xdr_enum(xdrs, (enum_t *) prm_direction) &&
213 (rmsg->rm_direction == REPLY) )
214 return (xdr_union(xdrs, (enum_t *) prp_stat,
215 (caddr_t)(void *)&(rmsg->rm_reply.ru), reply_dscrm,
222 * Serializes the "static part" of a call message header.
223 * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
224 * The rm_xid is not really static, but the user can easily munge on the fly.
227 xdr_callhdr(XDR *xdrs, struct rpc_msg *cmsg)
229 enum msg_type *prm_direction;
231 assert(xdrs != NULL);
232 assert(cmsg != NULL);
234 prm_direction = &cmsg->rm_direction;
236 cmsg->rm_direction = CALL;
237 cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
239 (xdrs->x_op == XDR_ENCODE) &&
240 xdr_uint32_t(xdrs, &(cmsg->rm_xid)) &&
241 xdr_enum(xdrs, (enum_t *) prm_direction) &&
242 xdr_uint32_t(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
243 xdr_uint32_t(xdrs, &(cmsg->rm_call.cb_prog)) )
244 return (xdr_uint32_t(xdrs, &(cmsg->rm_call.cb_vers)));
248 /* ************************** Client utility routine ************* */
250 static enum clnt_stat
251 accepted(enum accept_stat acpt_stat, struct rpc_err *error)
254 assert(error != NULL);
259 error->re_status = RPC_PROGUNAVAIL;
260 return (RPC_PROGUNAVAIL);
263 error->re_status = RPC_PROGVERSMISMATCH;
264 return (RPC_PROGVERSMISMATCH);
267 return (RPC_PROCUNAVAIL);
270 return (RPC_CANTDECODEARGS);
273 return (RPC_SYSTEMERROR);
276 return (RPC_SUCCESS);
279 /* something's wrong, but we don't know what ... */
280 error->re_lb.s1 = (int32_t)MSG_ACCEPTED;
281 error->re_lb.s2 = (int32_t)acpt_stat;
285 static enum clnt_stat
286 rejected(enum reject_stat rjct_stat, struct rpc_err *error)
289 assert(error != NULL);
293 return (RPC_VERSMISMATCH);
296 return (RPC_AUTHERROR);
298 /* something's wrong, but we don't know what ... */
300 error->re_lb.s1 = (int32_t)MSG_DENIED;
301 error->re_lb.s2 = (int32_t)rjct_stat;
306 * given a reply message, fills in the error
309 _seterr_reply(struct rpc_msg *msg, struct rpc_err *error)
314 assert(error != NULL);
316 /* optimized for normal, SUCCESSful case */
317 switch (msg->rm_reply.rp_stat) {
320 if (msg->acpted_rply.ar_stat == SUCCESS) {
324 stat = accepted(msg->acpted_rply.ar_stat, error);
328 stat = rejected(msg->rjcted_rply.rj_stat, error);
333 error->re_lb.s1 = (int32_t)(msg->rm_reply.rp_stat);
336 error->re_status = stat;
340 case RPC_VERSMISMATCH:
341 error->re_vers.low = msg->rjcted_rply.rj_vers.low;
342 error->re_vers.high = msg->rjcted_rply.rj_vers.high;
346 error->re_why = msg->rjcted_rply.rj_why;
349 case RPC_PROGVERSMISMATCH:
350 error->re_vers.low = msg->acpted_rply.ar_vers.low;
351 error->re_vers.high = msg->acpted_rply.ar_vers.high;
356 case RPC_PROGNOTREGISTERED:
357 case RPC_PMAPFAILURE:
358 case RPC_UNKNOWNPROTO:
359 case RPC_UNKNOWNHOST:
360 case RPC_SYSTEMERROR:
361 case RPC_CANTDECODEARGS:
362 case RPC_PROCUNAVAIL:
363 case RPC_PROGUNAVAIL:
367 case RPC_CANTDECODERES:
368 case RPC_CANTENCODEARGS: