1 /* $NetBSD: svc.c,v 1.21 2000/07/06 03:10:35 christos Exp $ */
4 * Copyright (c) 2009, Sun Microsystems, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * - Neither the name of Sun Microsystems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #if defined(LIBC_SCCS) && !defined(lint)
32 static char *sccsid2 = "@(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro";
33 static char *sccsid = "@(#)svc.c 2.4 88/08/11 4.0 RPCSRC";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 * svc.c, Server-side remote procedure call interface.
41 * There are two sets of procedures here. The xprt routines are
42 * for handling transport handles. The svc routines handle the
43 * list of service routines.
45 * Copyright (C) 1984, Sun Microsystems, Inc.
48 #include "namespace.h"
49 #include "reentrant.h"
50 #include <sys/types.h>
59 #include <rpc/pmap_clnt.h>
61 #include "un-namespace.h"
66 #define RQCRED_SIZE 400 /* this size is excessive */
68 #define SVC_VERSQUIET 0x0001 /* keep quiet about vers mismatch */
69 #define version_keepquiet(xp) (SVC_EXT(xp)->xp_flags & SVC_VERSQUIET)
71 #define max(a, b) (a > b ? a : b)
75 * Each entry represents a set of procedures (an rpc program).
76 * The dispatch routine takes request structs and runs the
77 * apropriate procedure.
79 static struct svc_callout {
80 struct svc_callout *sc_next;
84 void (*sc_dispatch)(struct svc_req *, SVCXPRT *);
87 static struct svc_callout *svc_find(rpcprog_t, rpcvers_t,
88 struct svc_callout **, char *);
89 static void __xprt_do_unregister (SVCXPRT *xprt, bool_t dolock);
91 /* *************** SVCXPRT related stuff **************** */
94 * Activate a transport handle.
102 assert(xprt != NULL);
106 rwlock_wrlock(&svc_fd_lock);
107 if (__svc_xports == NULL) {
108 __svc_xports = (SVCXPRT **)
109 mem_alloc(FD_SETSIZE * sizeof(SVCXPRT *));
110 if (__svc_xports == NULL) {
111 rwlock_unlock(&svc_fd_lock);
114 memset(__svc_xports, '\0', FD_SETSIZE * sizeof(SVCXPRT *));
116 if (sock < FD_SETSIZE) {
117 __svc_xports[sock] = xprt;
118 FD_SET(sock, &svc_fdset);
119 svc_maxfd = max(svc_maxfd, sock);
121 rwlock_unlock(&svc_fd_lock);
125 xprt_unregister(SVCXPRT *xprt)
127 __xprt_do_unregister(xprt, TRUE);
131 __xprt_unregister_unlocked(SVCXPRT *xprt)
133 __xprt_do_unregister(xprt, FALSE);
137 * De-activate a transport handle.
140 __xprt_do_unregister(xprt, dolock)
146 assert(xprt != NULL);
151 rwlock_wrlock(&svc_fd_lock);
152 if ((sock < FD_SETSIZE) && (__svc_xports[sock] == xprt)) {
153 __svc_xports[sock] = NULL;
154 FD_CLR(sock, &svc_fdset);
155 if (sock >= svc_maxfd) {
156 for (svc_maxfd--; svc_maxfd>=0; svc_maxfd--)
157 if (__svc_xports[svc_maxfd])
162 rwlock_unlock(&svc_fd_lock);
166 * Add a service program to the callout list.
167 * The dispatch routine will be called when a rpc request for this
168 * program number comes in.
171 svc_reg(xprt, prog, vers, dispatch, nconf)
173 const rpcprog_t prog;
174 const rpcvers_t vers;
175 void (*dispatch)(struct svc_req *, SVCXPRT *);
176 const struct netconfig *nconf;
179 struct svc_callout *prev;
180 struct svc_callout *s;
181 struct netconfig *tnconf;
185 /* VARIABLES PROTECTED BY svc_lock: s, prev, svc_head */
187 if (xprt->xp_netid) {
188 netid = strdup(xprt->xp_netid);
190 } else if (nconf && nconf->nc_netid) {
191 netid = strdup(nconf->nc_netid);
193 } else if ((tnconf = __rpcgettp(xprt->xp_fd)) != NULL) {
194 netid = strdup(tnconf->nc_netid);
196 freenetconfigent(tnconf);
197 } /* must have been created with svc_raw_create */
198 if ((netid == NULL) && (flag == 1)) {
202 rwlock_wrlock(&svc_lock);
203 if ((s = svc_find(prog, vers, &prev, netid)) != NULL) {
205 if (s->sc_dispatch == dispatch)
206 goto rpcb_it; /* he is registering another xptr */
207 rwlock_unlock(&svc_lock);
210 s = mem_alloc(sizeof (struct svc_callout));
213 rwlock_unlock(&svc_lock);
219 s->sc_dispatch = dispatch;
221 s->sc_next = svc_head;
224 if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
225 ((SVCXPRT *) xprt)->xp_netid = strdup(netid);
228 rwlock_unlock(&svc_lock);
229 /* now register the information with the local binder service */
231 /*LINTED const castaway*/
232 dummy = rpcb_set(prog, vers, (struct netconfig *) nconf,
233 &((SVCXPRT *) xprt)->xp_ltaddr);
240 * Remove a service program from the callout list.
243 svc_unreg(prog, vers)
244 const rpcprog_t prog;
245 const rpcvers_t vers;
247 struct svc_callout *prev;
248 struct svc_callout *s;
250 /* unregister the information anyway */
251 (void) rpcb_unset(prog, vers, NULL);
252 rwlock_wrlock(&svc_lock);
253 while ((s = svc_find(prog, vers, &prev, NULL)) != NULL) {
255 svc_head = s->sc_next;
257 prev->sc_next = s->sc_next;
261 mem_free(s->sc_netid, sizeof (s->sc_netid) + 1);
262 mem_free(s, sizeof (struct svc_callout));
264 rwlock_unlock(&svc_lock);
267 /* ********************** CALLOUT list related stuff ************* */
271 * Add a service program to the callout list.
272 * The dispatch routine will be called when a rpc request for this
273 * program number comes in.
276 svc_register(xprt, prog, vers, dispatch, protocol)
280 void (*dispatch)(struct svc_req *, SVCXPRT *);
283 struct svc_callout *prev;
284 struct svc_callout *s;
286 assert(xprt != NULL);
287 assert(dispatch != NULL);
289 if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) !=
291 if (s->sc_dispatch == dispatch)
292 goto pmap_it; /* he is registering another xptr */
295 s = mem_alloc(sizeof(struct svc_callout));
299 s->sc_prog = (rpcprog_t)prog;
300 s->sc_vers = (rpcvers_t)vers;
301 s->sc_dispatch = dispatch;
302 s->sc_next = svc_head;
305 /* now register the information with the local binder service */
307 return (pmap_set(prog, vers, protocol, xprt->xp_port));
313 * Remove a service program from the callout list.
316 svc_unregister(prog, vers)
320 struct svc_callout *prev;
321 struct svc_callout *s;
323 if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) ==
327 svc_head = s->sc_next;
329 prev->sc_next = s->sc_next;
332 mem_free(s, sizeof(struct svc_callout));
333 /* now unregister the information with the local binder service */
334 (void)pmap_unset(prog, vers);
339 * Search the callout list for a program number, return the callout
342 static struct svc_callout *
343 svc_find(prog, vers, prev, netid)
346 struct svc_callout **prev;
349 struct svc_callout *s, *p;
351 assert(prev != NULL);
354 for (s = svc_head; s != NULL; s = s->sc_next) {
355 if (((s->sc_prog == prog) && (s->sc_vers == vers)) &&
356 ((netid == NULL) || (s->sc_netid == NULL) ||
357 (strcmp(netid, s->sc_netid) == 0)))
365 /* ******************* REPLY GENERATION ROUTINES ************ */
368 * Send a reply to an rpc request
371 svc_sendreply(xprt, xdr_results, xdr_location)
373 xdrproc_t xdr_results;
378 assert(xprt != NULL);
380 rply.rm_direction = REPLY;
381 rply.rm_reply.rp_stat = MSG_ACCEPTED;
382 rply.acpted_rply.ar_verf = xprt->xp_verf;
383 rply.acpted_rply.ar_stat = SUCCESS;
384 rply.acpted_rply.ar_results.where = xdr_location;
385 rply.acpted_rply.ar_results.proc = xdr_results;
386 return (SVC_REPLY(xprt, &rply));
390 * No procedure error reply
398 assert(xprt != NULL);
400 rply.rm_direction = REPLY;
401 rply.rm_reply.rp_stat = MSG_ACCEPTED;
402 rply.acpted_rply.ar_verf = xprt->xp_verf;
403 rply.acpted_rply.ar_stat = PROC_UNAVAIL;
404 SVC_REPLY(xprt, &rply);
408 * Can't decode args error reply
416 assert(xprt != NULL);
418 rply.rm_direction = REPLY;
419 rply.rm_reply.rp_stat = MSG_ACCEPTED;
420 rply.acpted_rply.ar_verf = xprt->xp_verf;
421 rply.acpted_rply.ar_stat = GARBAGE_ARGS;
422 SVC_REPLY(xprt, &rply);
429 svcerr_systemerr(xprt)
434 assert(xprt != NULL);
436 rply.rm_direction = REPLY;
437 rply.rm_reply.rp_stat = MSG_ACCEPTED;
438 rply.acpted_rply.ar_verf = xprt->xp_verf;
439 rply.acpted_rply.ar_stat = SYSTEM_ERR;
440 SVC_REPLY(xprt, &rply);
445 * Tell RPC package to not complain about version errors to the client. This
446 * is useful when revving broadcast protocols that sit on a fixed address.
447 * There is really one (or should be only one) example of this kind of
448 * protocol: the portmapper (or rpc binder).
451 __svc_versquiet_on(xprt)
455 SVC_EXT(xprt)->xp_flags |= SVC_VERSQUIET;
459 __svc_versquiet_off(xprt)
463 SVC_EXT(xprt)->xp_flags &= ~SVC_VERSQUIET;
470 __svc_versquiet_on(xprt);
474 __svc_versquiet_get(xprt)
478 return (SVC_EXT(xprt)->xp_flags & SVC_VERSQUIET);
483 * Authentication error reply
486 svcerr_auth(xprt, why)
492 assert(xprt != NULL);
494 rply.rm_direction = REPLY;
495 rply.rm_reply.rp_stat = MSG_DENIED;
496 rply.rjcted_rply.rj_stat = AUTH_ERROR;
497 rply.rjcted_rply.rj_why = why;
498 SVC_REPLY(xprt, &rply);
502 * Auth too weak error reply
505 svcerr_weakauth(xprt)
509 assert(xprt != NULL);
511 svcerr_auth(xprt, AUTH_TOOWEAK);
515 * Program unavailable error reply
523 assert(xprt != NULL);
525 rply.rm_direction = REPLY;
526 rply.rm_reply.rp_stat = MSG_ACCEPTED;
527 rply.acpted_rply.ar_verf = xprt->xp_verf;
528 rply.acpted_rply.ar_stat = PROG_UNAVAIL;
529 SVC_REPLY(xprt, &rply);
533 * Program version mismatch error reply
536 svcerr_progvers(xprt, low_vers, high_vers)
543 assert(xprt != NULL);
545 rply.rm_direction = REPLY;
546 rply.rm_reply.rp_stat = MSG_ACCEPTED;
547 rply.acpted_rply.ar_verf = xprt->xp_verf;
548 rply.acpted_rply.ar_stat = PROG_MISMATCH;
549 rply.acpted_rply.ar_vers.low = (u_int32_t)low_vers;
550 rply.acpted_rply.ar_vers.high = (u_int32_t)high_vers;
551 SVC_REPLY(xprt, &rply);
555 * Allocate a new server transport structure. All fields are
556 * initialized to zero and xp_p3 is initialized to point at an
557 * extension structure to hold various flags and authentication
566 xprt = mem_alloc(sizeof(SVCXPRT));
569 memset(xprt, 0, sizeof(SVCXPRT));
570 ext = mem_alloc(sizeof(SVCXPRT_EXT));
572 mem_free(xprt, sizeof(SVCXPRT));
575 memset(ext, 0, sizeof(SVCXPRT_EXT));
577 ext->xp_auth.svc_ah_ops = &svc_auth_null_ops;
583 * Free a server transport structure.
590 mem_free(xprt->xp_p3, sizeof(SVCXPRT_EXT));
591 mem_free(xprt, sizeof(SVCXPRT));
594 /* ******************* SERVER INPUT STUFF ******************* */
597 * Get server side input from some transport.
599 * Statement of authentication parameters management:
600 * This function owns and manages all authentication parameters, specifically
601 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
602 * the "cooked" credentials (rqst->rq_clntcred).
603 * However, this function does not know the structure of the cooked
604 * credentials, so it make the following assumptions:
605 * a) the structure is contiguous (no pointers), and
606 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
607 * In all events, all three parameters are freed upon exit from this routine.
608 * The storage is trivially management on the call stack in user land, but
609 * is mallocated in kernel land.
619 readfds.fds_bits[0] = rdfds;
620 svc_getreqset(&readfds);
624 svc_getreqset(readfds)
628 fd_mask mask, *maskp;
631 assert(readfds != NULL);
633 maskp = readfds->fds_bits;
634 for (sock = 0; sock < FD_SETSIZE; sock += NFDBITS) {
635 for (mask = *maskp++; (bit = ffsl(mask)) != 0;
636 mask ^= (1ul << (bit - 1))) {
637 /* sock has input waiting */
639 svc_getreq_common(fd);
645 svc_getreq_common(fd)
655 char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
657 msg.rm_call.cb_cred.oa_base = cred_area;
658 msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
659 r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
661 rwlock_rdlock(&svc_fd_lock);
662 xprt = __svc_xports[fd];
663 rwlock_unlock(&svc_fd_lock);
665 /* But do we control sock? */
667 /* now receive msgs from xprtprt (support batch calls) */
669 if (SVC_RECV(xprt, &msg)) {
671 /* now find the exported program and call it */
672 struct svc_callout *s;
676 r.rq_prog = msg.rm_call.cb_prog;
677 r.rq_vers = msg.rm_call.cb_vers;
678 r.rq_proc = msg.rm_call.cb_proc;
679 r.rq_cred = msg.rm_call.cb_cred;
680 /* first authenticate the message */
681 if ((why = _authenticate(&r, &msg)) != AUTH_OK) {
683 * RPCSEC_GSS uses this return code
684 * for requests that form part of its
685 * context establishment protocol and
686 * should not be dispatched to the
689 if (why != RPCSEC_GSS_NODISPATCH)
690 svcerr_auth(xprt, why);
693 /* now match message with a registered service*/
695 low_vers = (rpcvers_t) -1L;
696 high_vers = (rpcvers_t) 0L;
697 for (s = svc_head; s != NULL; s = s->sc_next) {
698 if (s->sc_prog == r.rq_prog) {
699 if (s->sc_vers == r.rq_vers) {
700 (*s->sc_dispatch)(&r, xprt);
702 } /* found correct version */
704 if (s->sc_vers < low_vers)
705 low_vers = s->sc_vers;
706 if (s->sc_vers > high_vers)
707 high_vers = s->sc_vers;
708 } /* found correct program */
711 * if we got here, the program or version
715 svcerr_progvers(xprt, low_vers, high_vers);
718 /* Fall through to ... */
721 * Check if the xprt has been disconnected in a
722 * recursive call in the service dispatch routine.
725 rwlock_rdlock(&svc_fd_lock);
726 if (xprt != __svc_xports[fd]) {
727 rwlock_unlock(&svc_fd_lock);
730 rwlock_unlock(&svc_fd_lock);
732 if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
736 } while (stat == XPRT_MOREREQS);
741 svc_getreq_poll(pfdp, pollretval)
748 for (i = fds_found = 0; fds_found < pollretval; i++) {
749 struct pollfd *p = &pfdp[i];
752 /* fd has input waiting */
755 * We assume that this function is only called
756 * via someone _select()ing from svc_fdset or
757 * _poll()ing from svc_pollset[]. Thus it's safe
758 * to handle the POLLNVAL event by simply turning
759 * the corresponding bit off in svc_fdset. The
760 * svc_pollset[] array is derived from svc_fdset
761 * and so will also be updated eventually.
763 * XXX Should we do an xprt_unregister() instead?
765 if (p->revents & POLLNVAL) {
766 rwlock_wrlock(&svc_fd_lock);
767 FD_CLR(p->fd, &svc_fdset);
768 rwlock_unlock(&svc_fd_lock);
770 svc_getreq_common(p->fd);
776 rpc_control(int what, void *arg)
781 case RPC_SVC_CONNMAXREC_SET:
787 case RPC_SVC_CONNMAXREC_GET:
788 *(int *)arg = __svc_maxrec;