2 * Copyright (c) 2009, Sun Microsystems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
28 * @(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro
29 * @(#)svc.c 2.4 88/08/11 4.0 RPCSRC
30 * $NetBSD: svc.c,v 1.21 2000/07/06 03:10:35 christos Exp $
31 * $FreeBSD: src/lib/libc/rpc/svc.c,v 1.24 2006/02/27 22:10:59 deischen Exp $
32 * $DragonFly: src/lib/libc/rpc/svc.c,v 1.4 2005/11/13 12:27:04 swildner Exp $
36 * svc.c, Server-side remote procedure call interface.
38 * There are two sets of procedures here. The xprt routines are
39 * for handling transport handles. The svc routines handle the
40 * list of service routines.
42 * Copyright (C) 1984, Sun Microsystems, Inc.
45 #include "namespace.h"
46 #include "reentrant.h"
47 #include <sys/types.h>
56 #include <rpc/pmap_clnt.h>
58 #include "un-namespace.h"
63 #define RQCRED_SIZE 400 /* this size is excessive */
65 #define SVC_VERSQUIET 0x0001 /* keep quiet about vers mismatch */
66 #define version_keepquiet(xp) ((u_long)(xp)->xp_p3 & SVC_VERSQUIET)
68 #define max(a, b) (a > b ? a : b)
72 * Each entry represents a set of procedures (an rpc program).
73 * The dispatch routine takes request structs and runs the
74 * apropriate procedure.
76 static struct svc_callout
{
77 struct svc_callout
*sc_next
;
81 void (*sc_dispatch
)(struct svc_req
*, SVCXPRT
*);
84 static struct svc_callout
*svc_find(rpcprog_t
, rpcvers_t
,
85 struct svc_callout
**, char *);
86 static void __xprt_do_unregister (SVCXPRT
*xprt
, bool_t dolock
);
88 /* *************** SVCXPRT related stuff **************** */
91 * Activate a transport handle.
94 xprt_register(SVCXPRT
*xprt
)
102 rwlock_wrlock(&svc_fd_lock
);
103 if (__svc_xports
== NULL
) {
104 __svc_xports
= (SVCXPRT
**)
105 mem_alloc(FD_SETSIZE
* sizeof(SVCXPRT
*));
106 if (__svc_xports
== NULL
)
108 memset(__svc_xports
, '\0', FD_SETSIZE
* sizeof(SVCXPRT
*));
110 if (sock
< FD_SETSIZE
) {
111 __svc_xports
[sock
] = xprt
;
112 FD_SET(sock
, &svc_fdset
);
113 svc_maxfd
= max(svc_maxfd
, sock
);
115 rwlock_unlock(&svc_fd_lock
);
119 xprt_unregister(SVCXPRT
*xprt
)
121 __xprt_do_unregister(xprt
, TRUE
);
125 __xprt_unregister_unlocked(SVCXPRT
*xprt
)
127 __xprt_do_unregister(xprt
, FALSE
);
131 * De-activate a transport handle.
134 __xprt_do_unregister(SVCXPRT
*xprt
, bool_t dolock
)
138 assert(xprt
!= NULL
);
143 rwlock_wrlock(&svc_fd_lock
);
144 if ((sock
< FD_SETSIZE
) && (__svc_xports
[sock
] == xprt
)) {
145 __svc_xports
[sock
] = NULL
;
146 FD_CLR(sock
, &svc_fdset
);
147 if (sock
>= svc_maxfd
) {
148 for (svc_maxfd
--; svc_maxfd
>=0; svc_maxfd
--)
149 if (__svc_xports
[svc_maxfd
])
154 rwlock_unlock(&svc_fd_lock
);
158 * Add a service program to the callout list.
159 * The dispatch routine will be called when a rpc request for this
160 * program number comes in.
163 svc_reg(SVCXPRT
*xprt
, const rpcprog_t prog
, const rpcvers_t vers
,
164 void (*dispatch
)(struct svc_req
*, SVCXPRT
*),
165 const struct netconfig
*nconf
)
168 struct svc_callout
*prev
;
169 struct svc_callout
*s
;
170 struct netconfig
*tnconf
;
174 /* VARIABLES PROTECTED BY svc_lock: s, prev, svc_head */
176 if (xprt
->xp_netid
) {
177 netid
= strdup(xprt
->xp_netid
);
179 } else if (nconf
&& nconf
->nc_netid
) {
180 netid
= strdup(nconf
->nc_netid
);
182 } else if ((tnconf
= __rpcgettp(xprt
->xp_fd
)) != NULL
) {
183 netid
= strdup(tnconf
->nc_netid
);
185 freenetconfigent(tnconf
);
186 } /* must have been created with svc_raw_create */
187 if ((netid
== NULL
) && (flag
== 1)) {
191 rwlock_wrlock(&svc_lock
);
192 if ((s
= svc_find(prog
, vers
, &prev
, netid
)) != NULL
) {
195 if (s
->sc_dispatch
== dispatch
)
196 goto rpcb_it
; /* he is registering another xptr */
197 rwlock_unlock(&svc_lock
);
200 s
= mem_alloc(sizeof (struct svc_callout
));
204 rwlock_unlock(&svc_lock
);
210 s
->sc_dispatch
= dispatch
;
212 s
->sc_next
= svc_head
;
215 if ((xprt
->xp_netid
== NULL
) && (flag
== 1) && netid
)
216 ((SVCXPRT
*) xprt
)->xp_netid
= strdup(netid
);
219 rwlock_unlock(&svc_lock
);
220 /* now register the information with the local binder service */
222 /*LINTED const castaway*/
223 dummy
= rpcb_set(prog
, vers
, (struct netconfig
*) nconf
,
224 &((SVCXPRT
*) xprt
)->xp_ltaddr
);
231 * Remove a service program from the callout list.
234 svc_unreg(const rpcprog_t prog
, const rpcvers_t vers
)
236 struct svc_callout
*prev
;
237 struct svc_callout
*s
;
239 /* unregister the information anyway */
240 rpcb_unset(prog
, vers
, NULL
);
241 rwlock_wrlock(&svc_lock
);
242 while ((s
= svc_find(prog
, vers
, &prev
, NULL
)) != NULL
) {
244 svc_head
= s
->sc_next
;
246 prev
->sc_next
= s
->sc_next
;
250 mem_free(s
->sc_netid
, sizeof (s
->sc_netid
) + 1);
251 mem_free(s
, sizeof (struct svc_callout
));
253 rwlock_unlock(&svc_lock
);
256 /* ********************** CALLOUT list related stuff ************* */
260 * Add a service program to the callout list.
261 * The dispatch routine will be called when a rpc request for this
262 * program number comes in.
265 svc_register(SVCXPRT
*xprt
, u_long prog
, u_long vers
,
266 void (*dispatch
)(struct svc_req
*, SVCXPRT
*), int protocol
)
268 struct svc_callout
*prev
;
269 struct svc_callout
*s
;
271 assert(xprt
!= NULL
);
272 assert(dispatch
!= NULL
);
274 if ((s
= svc_find((rpcprog_t
)prog
, (rpcvers_t
)vers
, &prev
, NULL
)) !=
276 if (s
->sc_dispatch
== dispatch
)
277 goto pmap_it
; /* he is registering another xptr */
280 s
= mem_alloc(sizeof(struct svc_callout
));
284 s
->sc_prog
= (rpcprog_t
)prog
;
285 s
->sc_vers
= (rpcvers_t
)vers
;
286 s
->sc_dispatch
= dispatch
;
287 s
->sc_next
= svc_head
;
290 /* now register the information with the local binder service */
292 return (pmap_set(prog
, vers
, protocol
, xprt
->xp_port
));
298 * Remove a service program from the callout list.
301 svc_unregister(u_long prog
, u_long vers
)
303 struct svc_callout
*prev
;
304 struct svc_callout
*s
;
306 if ((s
= svc_find((rpcprog_t
)prog
, (rpcvers_t
)vers
, &prev
, NULL
)) ==
310 svc_head
= s
->sc_next
;
312 prev
->sc_next
= s
->sc_next
;
315 mem_free(s
, sizeof(struct svc_callout
));
316 /* now unregister the information with the local binder service */
317 pmap_unset(prog
, vers
);
322 * Search the callout list for a program number, return the callout
325 static struct svc_callout
*
326 svc_find(rpcprog_t prog
, rpcvers_t vers
, struct svc_callout
**prev
, char *netid
)
328 struct svc_callout
*s
, *p
;
330 assert(prev
!= NULL
);
333 for (s
= svc_head
; s
!= NULL
; s
= s
->sc_next
) {
334 if (((s
->sc_prog
== prog
) && (s
->sc_vers
== vers
)) &&
335 ((netid
== NULL
) || (s
->sc_netid
== NULL
) ||
336 (strcmp(netid
, s
->sc_netid
) == 0)))
344 /* ******************* REPLY GENERATION ROUTINES ************ */
347 * Send a reply to an rpc request
350 svc_sendreply(SVCXPRT
*xprt
, xdrproc_t xdr_results
, void *xdr_location
)
354 assert(xprt
!= NULL
);
356 rply
.rm_direction
= REPLY
;
357 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
358 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
359 rply
.acpted_rply
.ar_stat
= SUCCESS
;
360 rply
.acpted_rply
.ar_results
.where
= xdr_location
;
361 rply
.acpted_rply
.ar_results
.proc
= xdr_results
;
362 return (SVC_REPLY(xprt
, &rply
));
366 * No procedure error reply
369 svcerr_noproc(SVCXPRT
*xprt
)
373 assert(xprt
!= NULL
);
375 rply
.rm_direction
= REPLY
;
376 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
377 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
378 rply
.acpted_rply
.ar_stat
= PROC_UNAVAIL
;
379 SVC_REPLY(xprt
, &rply
);
383 * Can't decode args error reply
386 svcerr_decode(SVCXPRT
*xprt
)
390 assert(xprt
!= NULL
);
392 rply
.rm_direction
= REPLY
;
393 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
394 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
395 rply
.acpted_rply
.ar_stat
= GARBAGE_ARGS
;
396 SVC_REPLY(xprt
, &rply
);
403 svcerr_systemerr(SVCXPRT
*xprt
)
407 assert(xprt
!= NULL
);
409 rply
.rm_direction
= REPLY
;
410 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
411 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
412 rply
.acpted_rply
.ar_stat
= SYSTEM_ERR
;
413 SVC_REPLY(xprt
, &rply
);
417 * Authentication error reply
420 svcerr_auth(SVCXPRT
*xprt
, enum auth_stat why
)
424 assert(xprt
!= NULL
);
426 rply
.rm_direction
= REPLY
;
427 rply
.rm_reply
.rp_stat
= MSG_DENIED
;
428 rply
.rjcted_rply
.rj_stat
= AUTH_ERROR
;
429 rply
.rjcted_rply
.rj_why
= why
;
430 SVC_REPLY(xprt
, &rply
);
434 * Auth too weak error reply
437 svcerr_weakauth(SVCXPRT
*xprt
)
440 assert(xprt
!= NULL
);
442 svcerr_auth(xprt
, AUTH_TOOWEAK
);
446 * Program unavailable error reply
449 svcerr_noprog(SVCXPRT
*xprt
)
453 assert(xprt
!= NULL
);
455 rply
.rm_direction
= REPLY
;
456 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
457 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
458 rply
.acpted_rply
.ar_stat
= PROG_UNAVAIL
;
459 SVC_REPLY(xprt
, &rply
);
463 * Program version mismatch error reply
466 svcerr_progvers(SVCXPRT
*xprt
, rpcvers_t low_vers
, rpcvers_t high_vers
)
470 assert(xprt
!= NULL
);
472 rply
.rm_direction
= REPLY
;
473 rply
.rm_reply
.rp_stat
= MSG_ACCEPTED
;
474 rply
.acpted_rply
.ar_verf
= xprt
->xp_verf
;
475 rply
.acpted_rply
.ar_stat
= PROG_MISMATCH
;
476 rply
.acpted_rply
.ar_vers
.low
= (u_int32_t
)low_vers
;
477 rply
.acpted_rply
.ar_vers
.high
= (u_int32_t
)high_vers
;
478 SVC_REPLY(xprt
, &rply
);
481 /* ******************* SERVER INPUT STUFF ******************* */
484 * Get server side input from some transport.
486 * Statement of authentication parameters management:
487 * This function owns and manages all authentication parameters, specifically
488 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
489 * the "cooked" credentials (rqst->rq_clntcred).
490 * However, this function does not know the structure of the cooked
491 * credentials, so it make the following assumptions:
492 * a) the structure is contiguous (no pointers), and
493 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
494 * In all events, all three parameters are freed upon exit from this routine.
495 * The storage is trivially management on the call stack in user land, but
496 * is mallocated in kernel land.
500 svc_getreq(int rdfds
)
505 readfds
.fds_bits
[0] = rdfds
;
506 svc_getreqset(&readfds
);
510 svc_getreqset(fd_set
*readfds
)
513 fd_mask mask
, *maskp
;
516 assert(readfds
!= NULL
);
518 maskp
= readfds
->fds_bits
;
519 for (sock
= 0; sock
< FD_SETSIZE
; sock
+= NFDBITS
) {
520 for (mask
= *maskp
++; (bit
= ffs(mask
)) != 0;
521 mask
^= (1 << (bit
- 1))) {
522 /* sock has input waiting */
524 svc_getreq_common(fd
);
530 svc_getreq_common(int fd
)
539 char cred_area
[2*MAX_AUTH_BYTES
+ RQCRED_SIZE
];
541 msg
.rm_call
.cb_cred
.oa_base
= cred_area
;
542 msg
.rm_call
.cb_verf
.oa_base
= &(cred_area
[MAX_AUTH_BYTES
]);
543 r
.rq_clntcred
= &(cred_area
[2*MAX_AUTH_BYTES
]);
545 rwlock_rdlock(&svc_fd_lock
);
546 xprt
= __svc_xports
[fd
];
547 rwlock_unlock(&svc_fd_lock
);
549 /* But do we control sock? */
551 /* now receive msgs from xprtprt (support batch calls) */
553 if (SVC_RECV(xprt
, &msg
)) {
555 /* now find the exported program and call it */
556 struct svc_callout
*s
;
560 r
.rq_prog
= msg
.rm_call
.cb_prog
;
561 r
.rq_vers
= msg
.rm_call
.cb_vers
;
562 r
.rq_proc
= msg
.rm_call
.cb_proc
;
563 r
.rq_cred
= msg
.rm_call
.cb_cred
;
564 /* first authenticate the message */
565 if ((why
= _authenticate(&r
, &msg
)) != AUTH_OK
) {
566 svcerr_auth(xprt
, why
);
569 /* now match message with a registered service*/
571 low_vers
= (rpcvers_t
) -1L;
572 high_vers
= (rpcvers_t
) 0L;
573 for (s
= svc_head
; s
!= NULL
; s
= s
->sc_next
) {
574 if (s
->sc_prog
== r
.rq_prog
) {
575 if (s
->sc_vers
== r
.rq_vers
) {
576 (*s
->sc_dispatch
)(&r
, xprt
);
578 } /* found correct version */
580 if (s
->sc_vers
< low_vers
)
581 low_vers
= s
->sc_vers
;
582 if (s
->sc_vers
> high_vers
)
583 high_vers
= s
->sc_vers
;
584 } /* found correct program */
587 * if we got here, the program or version
591 svcerr_progvers(xprt
, low_vers
, high_vers
);
594 /* Fall through to ... */
597 * Check if the xprt has been disconnected in a
598 * recursive call in the service dispatch routine.
601 rwlock_rdlock(&svc_fd_lock
);
602 if (xprt
!= __svc_xports
[fd
]) {
603 rwlock_unlock(&svc_fd_lock
);
606 rwlock_unlock(&svc_fd_lock
);
608 if ((stat
= SVC_STAT(xprt
)) == XPRT_DIED
){
612 } while (stat
== XPRT_MOREREQS
);
617 svc_getreq_poll(struct pollfd
*pfdp
, int pollretval
)
622 for (i
= fds_found
= 0; fds_found
< pollretval
; i
++) {
623 struct pollfd
*p
= &pfdp
[i
];
626 /* fd has input waiting */
629 * We assume that this function is only called
630 * via someone _select()ing from svc_fdset or
631 * _poll()ing from svc_pollset[]. Thus it's safe
632 * to handle the POLLNVAL event by simply turning
633 * the corresponding bit off in svc_fdset. The
634 * svc_pollset[] array is derived from svc_fdset
635 * and so will also be updated eventually.
637 * XXX Should we do an xprt_unregister() instead?
639 if (p
->revents
& POLLNVAL
) {
640 rwlock_wrlock(&svc_fd_lock
);
641 FD_CLR(p
->fd
, &svc_fdset
);
642 rwlock_unlock(&svc_fd_lock
);
644 svc_getreq_common(p
->fd
);
650 rpc_control(int what
, void *arg
)
655 case RPC_SVC_CONNMAXREC_SET
:
661 case RPC_SVC_CONNMAXREC_GET
:
662 *(int *)arg
= __svc_maxrec
;