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) {
206 if (s->sc_dispatch == dispatch)
207 goto rpcb_it; /* he is registering another xptr */
208 rwlock_unlock(&svc_lock);
211 s = mem_alloc(sizeof (struct svc_callout));
215 rwlock_unlock(&svc_lock);
221 s->sc_dispatch = dispatch;
223 s->sc_next = svc_head;
226 if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
227 ((SVCXPRT *) xprt)->xp_netid = strdup(netid);
230 rwlock_unlock(&svc_lock);
231 /* now register the information with the local binder service */
233 /*LINTED const castaway*/
234 dummy = rpcb_set(prog, vers, (struct netconfig *) nconf,
235 &((SVCXPRT *) xprt)->xp_ltaddr);
242 * Remove a service program from the callout list.
245 svc_unreg(prog, vers)
246 const rpcprog_t prog;
247 const rpcvers_t vers;
249 struct svc_callout *prev;
250 struct svc_callout *s;
252 /* unregister the information anyway */
253 (void) rpcb_unset(prog, vers, NULL);
254 rwlock_wrlock(&svc_lock);
255 while ((s = svc_find(prog, vers, &prev, NULL)) != NULL) {
257 svc_head = s->sc_next;
259 prev->sc_next = s->sc_next;
263 mem_free(s->sc_netid, sizeof (s->sc_netid) + 1);
264 mem_free(s, sizeof (struct svc_callout));
266 rwlock_unlock(&svc_lock);
269 /* ********************** CALLOUT list related stuff ************* */
273 * Add a service program to the callout list.
274 * The dispatch routine will be called when a rpc request for this
275 * program number comes in.
278 svc_register(xprt, prog, vers, dispatch, protocol)
282 void (*dispatch)(struct svc_req *, SVCXPRT *);
285 struct svc_callout *prev;
286 struct svc_callout *s;
288 assert(xprt != NULL);
289 assert(dispatch != NULL);
291 if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) !=
293 if (s->sc_dispatch == dispatch)
294 goto pmap_it; /* he is registering another xptr */
297 s = mem_alloc(sizeof(struct svc_callout));
301 s->sc_prog = (rpcprog_t)prog;
302 s->sc_vers = (rpcvers_t)vers;
303 s->sc_dispatch = dispatch;
304 s->sc_next = svc_head;
307 /* now register the information with the local binder service */
309 return (pmap_set(prog, vers, protocol, xprt->xp_port));
315 * Remove a service program from the callout list.
318 svc_unregister(prog, vers)
322 struct svc_callout *prev;
323 struct svc_callout *s;
325 if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) ==
329 svc_head = s->sc_next;
331 prev->sc_next = s->sc_next;
334 mem_free(s, sizeof(struct svc_callout));
335 /* now unregister the information with the local binder service */
336 (void)pmap_unset(prog, vers);
341 * Search the callout list for a program number, return the callout
344 static struct svc_callout *
345 svc_find(prog, vers, prev, netid)
348 struct svc_callout **prev;
351 struct svc_callout *s, *p;
353 assert(prev != NULL);
356 for (s = svc_head; s != NULL; s = s->sc_next) {
357 if (((s->sc_prog == prog) && (s->sc_vers == vers)) &&
358 ((netid == NULL) || (s->sc_netid == NULL) ||
359 (strcmp(netid, s->sc_netid) == 0)))
367 /* ******************* REPLY GENERATION ROUTINES ************ */
370 * Send a reply to an rpc request
373 svc_sendreply(xprt, xdr_results, xdr_location)
375 xdrproc_t xdr_results;
380 assert(xprt != NULL);
382 rply.rm_direction = REPLY;
383 rply.rm_reply.rp_stat = MSG_ACCEPTED;
384 rply.acpted_rply.ar_verf = xprt->xp_verf;
385 rply.acpted_rply.ar_stat = SUCCESS;
386 rply.acpted_rply.ar_results.where = xdr_location;
387 rply.acpted_rply.ar_results.proc = xdr_results;
388 return (SVC_REPLY(xprt, &rply));
392 * No procedure error reply
400 assert(xprt != NULL);
402 rply.rm_direction = REPLY;
403 rply.rm_reply.rp_stat = MSG_ACCEPTED;
404 rply.acpted_rply.ar_verf = xprt->xp_verf;
405 rply.acpted_rply.ar_stat = PROC_UNAVAIL;
406 SVC_REPLY(xprt, &rply);
410 * Can't decode args error reply
418 assert(xprt != NULL);
420 rply.rm_direction = REPLY;
421 rply.rm_reply.rp_stat = MSG_ACCEPTED;
422 rply.acpted_rply.ar_verf = xprt->xp_verf;
423 rply.acpted_rply.ar_stat = GARBAGE_ARGS;
424 SVC_REPLY(xprt, &rply);
431 svcerr_systemerr(xprt)
436 assert(xprt != NULL);
438 rply.rm_direction = REPLY;
439 rply.rm_reply.rp_stat = MSG_ACCEPTED;
440 rply.acpted_rply.ar_verf = xprt->xp_verf;
441 rply.acpted_rply.ar_stat = SYSTEM_ERR;
442 SVC_REPLY(xprt, &rply);
447 * Tell RPC package to not complain about version errors to the client. This
448 * is useful when revving broadcast protocols that sit on a fixed address.
449 * There is really one (or should be only one) example of this kind of
450 * protocol: the portmapper (or rpc binder).
453 __svc_versquiet_on(xprt)
457 SVC_EXT(xprt)->xp_flags |= SVC_VERSQUIET;
461 __svc_versquiet_off(xprt)
465 SVC_EXT(xprt)->xp_flags &= ~SVC_VERSQUIET;
472 __svc_versquiet_on(xprt);
476 __svc_versquiet_get(xprt)
480 return (SVC_EXT(xprt)->xp_flags & SVC_VERSQUIET);
485 * Authentication error reply
488 svcerr_auth(xprt, why)
494 assert(xprt != NULL);
496 rply.rm_direction = REPLY;
497 rply.rm_reply.rp_stat = MSG_DENIED;
498 rply.rjcted_rply.rj_stat = AUTH_ERROR;
499 rply.rjcted_rply.rj_why = why;
500 SVC_REPLY(xprt, &rply);
504 * Auth too weak error reply
507 svcerr_weakauth(xprt)
511 assert(xprt != NULL);
513 svcerr_auth(xprt, AUTH_TOOWEAK);
517 * Program unavailable error reply
525 assert(xprt != NULL);
527 rply.rm_direction = REPLY;
528 rply.rm_reply.rp_stat = MSG_ACCEPTED;
529 rply.acpted_rply.ar_verf = xprt->xp_verf;
530 rply.acpted_rply.ar_stat = PROG_UNAVAIL;
531 SVC_REPLY(xprt, &rply);
535 * Program version mismatch error reply
538 svcerr_progvers(xprt, low_vers, high_vers)
545 assert(xprt != NULL);
547 rply.rm_direction = REPLY;
548 rply.rm_reply.rp_stat = MSG_ACCEPTED;
549 rply.acpted_rply.ar_verf = xprt->xp_verf;
550 rply.acpted_rply.ar_stat = PROG_MISMATCH;
551 rply.acpted_rply.ar_vers.low = (u_int32_t)low_vers;
552 rply.acpted_rply.ar_vers.high = (u_int32_t)high_vers;
553 SVC_REPLY(xprt, &rply);
557 * Allocate a new server transport structure. All fields are
558 * initialized to zero and xp_p3 is initialized to point at an
559 * extension structure to hold various flags and authentication
568 xprt = mem_alloc(sizeof(SVCXPRT));
571 memset(xprt, 0, sizeof(SVCXPRT));
572 ext = mem_alloc(sizeof(SVCXPRT_EXT));
574 mem_free(xprt, sizeof(SVCXPRT));
577 memset(ext, 0, sizeof(SVCXPRT_EXT));
579 ext->xp_auth.svc_ah_ops = &svc_auth_null_ops;
585 * Free a server transport structure.
592 mem_free(xprt->xp_p3, sizeof(SVCXPRT_EXT));
593 mem_free(xprt, sizeof(SVCXPRT));
596 /* ******************* SERVER INPUT STUFF ******************* */
599 * Get server side input from some transport.
601 * Statement of authentication parameters management:
602 * This function owns and manages all authentication parameters, specifically
603 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
604 * the "cooked" credentials (rqst->rq_clntcred).
605 * However, this function does not know the structure of the cooked
606 * credentials, so it make the following assumptions:
607 * a) the structure is contiguous (no pointers), and
608 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
609 * In all events, all three parameters are freed upon exit from this routine.
610 * The storage is trivially management on the call stack in user land, but
611 * is mallocated in kernel land.
621 readfds.fds_bits[0] = rdfds;
622 svc_getreqset(&readfds);
626 svc_getreqset(readfds)
630 fd_mask mask, *maskp;
633 assert(readfds != NULL);
635 maskp = readfds->fds_bits;
636 for (sock = 0; sock < FD_SETSIZE; sock += NFDBITS) {
637 for (mask = *maskp++; (bit = ffsl(mask)) != 0;
638 mask ^= (1ul << (bit - 1))) {
639 /* sock has input waiting */
641 svc_getreq_common(fd);
647 svc_getreq_common(fd)
657 char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
659 msg.rm_call.cb_cred.oa_base = cred_area;
660 msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
661 r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
663 rwlock_rdlock(&svc_fd_lock);
664 xprt = __svc_xports[fd];
665 rwlock_unlock(&svc_fd_lock);
667 /* But do we control sock? */
669 /* now receive msgs from xprtprt (support batch calls) */
671 if (SVC_RECV(xprt, &msg)) {
673 /* now find the exported program and call it */
674 struct svc_callout *s;
678 r.rq_prog = msg.rm_call.cb_prog;
679 r.rq_vers = msg.rm_call.cb_vers;
680 r.rq_proc = msg.rm_call.cb_proc;
681 r.rq_cred = msg.rm_call.cb_cred;
682 /* first authenticate the message */
683 if ((why = _authenticate(&r, &msg)) != AUTH_OK) {
685 * RPCSEC_GSS uses this return code
686 * for requests that form part of its
687 * context establishment protocol and
688 * should not be dispatched to the
691 if (why != RPCSEC_GSS_NODISPATCH)
692 svcerr_auth(xprt, why);
695 /* now match message with a registered service*/
697 low_vers = (rpcvers_t) -1L;
698 high_vers = (rpcvers_t) 0L;
699 for (s = svc_head; s != NULL; s = s->sc_next) {
700 if (s->sc_prog == r.rq_prog) {
701 if (s->sc_vers == r.rq_vers) {
702 (*s->sc_dispatch)(&r, xprt);
704 } /* found correct version */
706 if (s->sc_vers < low_vers)
707 low_vers = s->sc_vers;
708 if (s->sc_vers > high_vers)
709 high_vers = s->sc_vers;
710 } /* found correct program */
713 * if we got here, the program or version
717 svcerr_progvers(xprt, low_vers, high_vers);
720 /* Fall through to ... */
723 * Check if the xprt has been disconnected in a
724 * recursive call in the service dispatch routine.
727 rwlock_rdlock(&svc_fd_lock);
728 if (xprt != __svc_xports[fd]) {
729 rwlock_unlock(&svc_fd_lock);
732 rwlock_unlock(&svc_fd_lock);
734 if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
738 } while (stat == XPRT_MOREREQS);
743 svc_getreq_poll(pfdp, pollretval)
750 for (i = fds_found = 0; fds_found < pollretval; i++) {
751 struct pollfd *p = &pfdp[i];
754 /* fd has input waiting */
757 * We assume that this function is only called
758 * via someone _select()ing from svc_fdset or
759 * _poll()ing from svc_pollset[]. Thus it's safe
760 * to handle the POLLNVAL event by simply turning
761 * the corresponding bit off in svc_fdset. The
762 * svc_pollset[] array is derived from svc_fdset
763 * and so will also be updated eventually.
765 * XXX Should we do an xprt_unregister() instead?
767 if (p->revents & POLLNVAL) {
768 rwlock_wrlock(&svc_fd_lock);
769 FD_CLR(p->fd, &svc_fdset);
770 rwlock_unlock(&svc_fd_lock);
772 svc_getreq_common(p->fd);
778 rpc_control(int what, void *arg)
783 case RPC_SVC_CONNMAXREC_SET:
789 case RPC_SVC_CONNMAXREC_GET:
790 *(int *)arg = __svc_maxrec;