]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/rpc/rpc_prot.c
Fix build with DRM and INVARIANTS enabled.
[FreeBSD/FreeBSD.git] / sys / rpc / rpc_prot.c
1 /*      $NetBSD: rpc_prot.c,v 1.16 2000/06/02 23:11:13 fvdl Exp $       */
2
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 2009, Sun Microsystems, Inc.
7  * All rights reserved.
8  *
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.
19  * 
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.
31  */
32
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";
36 #endif
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39
40 /*
41  * rpc_prot.c
42  *
43  * Copyright (C) 1984, Sun Microsystems, Inc.
44  *
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.
51  */
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/malloc.h>
57
58 #include <rpc/types.h>
59 #include <rpc/xdr.h>
60 #include <rpc/auth.h>
61 #include <rpc/clnt.h>
62 #include <rpc/rpc_msg.h>
63
64 MALLOC_DEFINE(M_RPC, "rpc", "Remote Procedure Call");
65
66 #define assert(exp)     KASSERT(exp, ("bad arguments"))
67
68 static enum clnt_stat accepted(enum accept_stat, struct rpc_err *);
69 static enum clnt_stat rejected(enum reject_stat, struct rpc_err *);
70
71 /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
72
73 struct opaque_auth _null_auth;
74
75 /*
76  * XDR an opaque authentication struct
77  * (see auth.h)
78  */
79 bool_t
80 xdr_opaque_auth(XDR *xdrs, struct opaque_auth *ap)
81 {
82
83         assert(xdrs != NULL);
84         assert(ap != NULL);
85
86         if (xdr_enum(xdrs, &(ap->oa_flavor)))
87                 return (xdr_bytes(xdrs, &ap->oa_base,
88                         &ap->oa_length, MAX_AUTH_BYTES));
89         return (FALSE);
90 }
91
92 /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
93
94 /*
95  * XDR the MSG_ACCEPTED part of a reply message union
96  */
97 bool_t
98 xdr_accepted_reply(XDR *xdrs, struct accepted_reply *ar)
99 {
100         enum accept_stat *par_stat;
101
102         assert(xdrs != NULL);
103         assert(ar != NULL);
104
105         par_stat = &ar->ar_stat;
106
107         /* personalized union, rather than calling xdr_union */
108         if (! xdr_opaque_auth(xdrs, &(ar->ar_verf)))
109                 return (FALSE);
110         if (! xdr_enum(xdrs, (enum_t *) par_stat))
111                 return (FALSE);
112         switch (ar->ar_stat) {
113
114         case SUCCESS:
115                 if (ar->ar_results.proc != (xdrproc_t) xdr_void)
116                         return ((*(ar->ar_results.proc))(xdrs,
117                                 ar->ar_results.where));
118                 else
119                         return (TRUE);
120
121         case PROG_MISMATCH:
122                 if (! xdr_uint32_t(xdrs, &(ar->ar_vers.low)))
123                         return (FALSE);
124                 return (xdr_uint32_t(xdrs, &(ar->ar_vers.high)));
125
126         case GARBAGE_ARGS:
127         case SYSTEM_ERR:
128         case PROC_UNAVAIL:
129         case PROG_UNAVAIL:
130                 break;
131         }
132         return (TRUE);  /* TRUE => open ended set of problems */
133 }
134
135 /*
136  * XDR the MSG_DENIED part of a reply message union
137  */
138 bool_t 
139 xdr_rejected_reply(XDR *xdrs, struct rejected_reply *rr)
140 {
141         enum reject_stat *prj_stat;
142         enum auth_stat *prj_why;
143
144         assert(xdrs != NULL);
145         assert(rr != NULL);
146
147         prj_stat = &rr->rj_stat;
148
149         /* personalized union, rather than calling xdr_union */
150         if (! xdr_enum(xdrs, (enum_t *) prj_stat))
151                 return (FALSE);
152         switch (rr->rj_stat) {
153
154         case RPC_MISMATCH:
155                 if (! xdr_uint32_t(xdrs, &(rr->rj_vers.low)))
156                         return (FALSE);
157                 return (xdr_uint32_t(xdrs, &(rr->rj_vers.high)));
158
159         case AUTH_ERROR:
160                 prj_why = &rr->rj_why;
161                 return (xdr_enum(xdrs, (enum_t *) prj_why));
162         }
163         /* NOTREACHED */
164         assert(0);
165         return (FALSE);
166 }
167
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 } };
172
173 /*
174  * XDR a reply message
175  */
176 bool_t
177 xdr_replymsg(XDR *xdrs, struct rpc_msg *rmsg)
178 {
179         int32_t *buf;
180         enum msg_type *prm_direction;
181         enum reply_stat *prp_stat;
182
183         assert(xdrs != NULL);
184         assert(rmsg != NULL);
185
186         if (xdrs->x_op == XDR_DECODE) {
187                 buf = XDR_INLINE(xdrs, 3 * BYTES_PER_XDR_UNIT);
188                 if (buf != NULL) {
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) {
192                                 return (FALSE);
193                         }
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));
202                         else
203                                 return (FALSE);
204                 }
205         }
206
207         prm_direction = &rmsg->rm_direction;
208         prp_stat = &rmsg->rm_reply.rp_stat;
209
210         if (
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,
216                    NULL_xdrproc_t));
217         return (FALSE);
218 }
219
220
221 /*
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.
225  */
226 bool_t
227 xdr_callhdr(XDR *xdrs, struct rpc_msg *cmsg)
228 {
229         enum msg_type *prm_direction;
230
231         assert(xdrs != NULL);
232         assert(cmsg != NULL);
233
234         prm_direction = &cmsg->rm_direction;
235
236         cmsg->rm_direction = CALL;
237         cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
238         if (
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)));
245         return (FALSE);
246 }
247
248 /* ************************** Client utility routine ************* */
249
250 static enum clnt_stat
251 accepted(enum accept_stat acpt_stat, struct rpc_err *error)
252 {
253
254         assert(error != NULL);
255
256         switch (acpt_stat) {
257
258         case PROG_UNAVAIL:
259                 error->re_status = RPC_PROGUNAVAIL;
260                 return (RPC_PROGUNAVAIL);
261
262         case PROG_MISMATCH:
263                 error->re_status = RPC_PROGVERSMISMATCH;
264                 return (RPC_PROGVERSMISMATCH);
265
266         case PROC_UNAVAIL:
267                 return (RPC_PROCUNAVAIL);
268
269         case GARBAGE_ARGS:
270                 return (RPC_CANTDECODEARGS);
271
272         case SYSTEM_ERR:
273                 return (RPC_SYSTEMERROR);
274
275         case SUCCESS:
276                 return (RPC_SUCCESS);
277         }
278         /* NOTREACHED */
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;
282         return (RPC_FAILED);
283 }
284
285 static enum clnt_stat
286 rejected(enum reject_stat rjct_stat, struct rpc_err *error)
287 {
288
289         assert(error != NULL);
290
291         switch (rjct_stat) {
292         case RPC_MISMATCH:
293                 return (RPC_VERSMISMATCH);
294
295         case AUTH_ERROR:
296                 return (RPC_AUTHERROR);
297         }
298         /* something's wrong, but we don't know what ... */
299         /* NOTREACHED */
300         error->re_lb.s1 = (int32_t)MSG_DENIED;
301         error->re_lb.s2 = (int32_t)rjct_stat;
302         return (RPC_FAILED);
303 }
304
305 /*
306  * given a reply message, fills in the error
307  */
308 enum clnt_stat
309 _seterr_reply(struct rpc_msg *msg, struct rpc_err *error)
310 {
311         enum clnt_stat stat;
312
313         assert(msg != NULL);
314         assert(error != NULL);
315
316         /* optimized for normal, SUCCESSful case */
317         switch (msg->rm_reply.rp_stat) {
318
319         case MSG_ACCEPTED:
320                 if (msg->acpted_rply.ar_stat == SUCCESS) {
321                         stat = RPC_SUCCESS;
322                         return (stat);
323                 }
324                 stat = accepted(msg->acpted_rply.ar_stat, error);
325                 break;
326
327         case MSG_DENIED:
328                 stat = rejected(msg->rjcted_rply.rj_stat, error);
329                 break;
330
331         default:
332                 stat = RPC_FAILED;
333                 error->re_lb.s1 = (int32_t)(msg->rm_reply.rp_stat);
334                 break;
335         }
336         error->re_status = stat;
337
338         switch (stat) {
339
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;
343                 break;
344
345         case RPC_AUTHERROR:
346                 error->re_why = msg->rjcted_rply.rj_why;
347                 break;
348
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;
352                 break;
353
354         case RPC_FAILED:
355         case RPC_SUCCESS:
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:
364         case RPC_TIMEDOUT:
365         case RPC_CANTRECV:
366         case RPC_CANTSEND:
367         case RPC_CANTDECODERES:
368         case RPC_CANTENCODEARGS:
369         default:
370                 break;
371         }
372
373         return (stat);
374 }