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 SVCXPRT **__svc_xports;
90 static struct svc_callout *svc_find(rpcprog_t, rpcvers_t,
91 struct svc_callout **, char *);
92 static void __xprt_do_unregister (SVCXPRT *xprt, bool_t dolock);
94 /* *************** SVCXPRT related stuff **************** */
97 * Activate a transport handle.
105 assert(xprt != NULL);
109 rwlock_wrlock(&svc_fd_lock);
110 if (__svc_xports == NULL) {
111 __svc_xports = (SVCXPRT **)
112 mem_alloc(FD_SETSIZE * sizeof(SVCXPRT *));
113 if (__svc_xports == NULL) {
114 rwlock_unlock(&svc_fd_lock);
117 memset(__svc_xports, '\0', FD_SETSIZE * sizeof(SVCXPRT *));
119 if (sock < FD_SETSIZE) {
120 __svc_xports[sock] = xprt;
121 FD_SET(sock, &svc_fdset);
122 svc_maxfd = max(svc_maxfd, sock);
124 rwlock_unlock(&svc_fd_lock);
128 xprt_unregister(SVCXPRT *xprt)
130 __xprt_do_unregister(xprt, TRUE);
134 __xprt_unregister_unlocked(SVCXPRT *xprt)
136 __xprt_do_unregister(xprt, FALSE);
140 * De-activate a transport handle.
143 __xprt_do_unregister(xprt, dolock)
149 assert(xprt != NULL);
154 rwlock_wrlock(&svc_fd_lock);
155 if ((sock < FD_SETSIZE) && (__svc_xports[sock] == xprt)) {
156 __svc_xports[sock] = NULL;
157 FD_CLR(sock, &svc_fdset);
158 if (sock >= svc_maxfd) {
159 for (svc_maxfd--; svc_maxfd>=0; svc_maxfd--)
160 if (__svc_xports[svc_maxfd])
165 rwlock_unlock(&svc_fd_lock);
169 * Add a service program to the callout list.
170 * The dispatch routine will be called when a rpc request for this
171 * program number comes in.
174 svc_reg(xprt, prog, vers, dispatch, nconf)
176 const rpcprog_t prog;
177 const rpcvers_t vers;
178 void (*dispatch)(struct svc_req *, SVCXPRT *);
179 const struct netconfig *nconf;
182 struct svc_callout *prev;
183 struct svc_callout *s;
184 struct netconfig *tnconf;
188 /* VARIABLES PROTECTED BY svc_lock: s, prev, svc_head */
190 if (xprt->xp_netid) {
191 netid = strdup(xprt->xp_netid);
193 } else if (nconf && nconf->nc_netid) {
194 netid = strdup(nconf->nc_netid);
196 } else if ((tnconf = __rpcgettp(xprt->xp_fd)) != NULL) {
197 netid = strdup(tnconf->nc_netid);
199 freenetconfigent(tnconf);
200 } /* must have been created with svc_raw_create */
201 if ((netid == NULL) && (flag == 1)) {
205 rwlock_wrlock(&svc_lock);
206 if ((s = svc_find(prog, vers, &prev, netid)) != NULL) {
209 if (s->sc_dispatch == dispatch)
210 goto rpcb_it; /* he is registering another xptr */
211 rwlock_unlock(&svc_lock);
214 s = mem_alloc(sizeof (struct svc_callout));
218 rwlock_unlock(&svc_lock);
224 s->sc_dispatch = dispatch;
226 s->sc_next = svc_head;
229 if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
230 ((SVCXPRT *) xprt)->xp_netid = strdup(netid);
233 rwlock_unlock(&svc_lock);
234 /* now register the information with the local binder service */
236 /*LINTED const castaway*/
237 dummy = rpcb_set(prog, vers, (struct netconfig *) nconf,
238 &((SVCXPRT *) xprt)->xp_ltaddr);
245 * Remove a service program from the callout list.
248 svc_unreg(prog, vers)
249 const rpcprog_t prog;
250 const rpcvers_t vers;
252 struct svc_callout *prev;
253 struct svc_callout *s;
255 /* unregister the information anyway */
256 (void) rpcb_unset(prog, vers, NULL);
257 rwlock_wrlock(&svc_lock);
258 while ((s = svc_find(prog, vers, &prev, NULL)) != NULL) {
260 svc_head = s->sc_next;
262 prev->sc_next = s->sc_next;
266 mem_free(s->sc_netid, sizeof (s->sc_netid) + 1);
267 mem_free(s, sizeof (struct svc_callout));
269 rwlock_unlock(&svc_lock);
272 /* ********************** CALLOUT list related stuff ************* */
276 * Add a service program to the callout list.
277 * The dispatch routine will be called when a rpc request for this
278 * program number comes in.
281 svc_register(xprt, prog, vers, dispatch, protocol)
285 void (*dispatch)(struct svc_req *, SVCXPRT *);
288 struct svc_callout *prev;
289 struct svc_callout *s;
291 assert(xprt != NULL);
292 assert(dispatch != NULL);
294 if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) !=
296 if (s->sc_dispatch == dispatch)
297 goto pmap_it; /* he is registering another xptr */
300 s = mem_alloc(sizeof(struct svc_callout));
304 s->sc_prog = (rpcprog_t)prog;
305 s->sc_vers = (rpcvers_t)vers;
306 s->sc_dispatch = dispatch;
307 s->sc_next = svc_head;
310 /* now register the information with the local binder service */
312 return (pmap_set(prog, vers, protocol, xprt->xp_port));
318 * Remove a service program from the callout list.
321 svc_unregister(prog, vers)
325 struct svc_callout *prev;
326 struct svc_callout *s;
328 if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) ==
332 svc_head = s->sc_next;
334 prev->sc_next = s->sc_next;
337 mem_free(s, sizeof(struct svc_callout));
338 /* now unregister the information with the local binder service */
339 (void)pmap_unset(prog, vers);
344 * Search the callout list for a program number, return the callout
347 static struct svc_callout *
348 svc_find(prog, vers, prev, netid)
351 struct svc_callout **prev;
354 struct svc_callout *s, *p;
356 assert(prev != NULL);
359 for (s = svc_head; s != NULL; s = s->sc_next) {
360 if (((s->sc_prog == prog) && (s->sc_vers == vers)) &&
361 ((netid == NULL) || (s->sc_netid == NULL) ||
362 (strcmp(netid, s->sc_netid) == 0)))
370 /* ******************* REPLY GENERATION ROUTINES ************ */
373 * Send a reply to an rpc request
376 svc_sendreply(xprt, xdr_results, xdr_location)
378 xdrproc_t xdr_results;
383 assert(xprt != NULL);
385 rply.rm_direction = REPLY;
386 rply.rm_reply.rp_stat = MSG_ACCEPTED;
387 rply.acpted_rply.ar_verf = xprt->xp_verf;
388 rply.acpted_rply.ar_stat = SUCCESS;
389 rply.acpted_rply.ar_results.where = xdr_location;
390 rply.acpted_rply.ar_results.proc = xdr_results;
391 return (SVC_REPLY(xprt, &rply));
395 * No procedure error reply
403 assert(xprt != NULL);
405 rply.rm_direction = REPLY;
406 rply.rm_reply.rp_stat = MSG_ACCEPTED;
407 rply.acpted_rply.ar_verf = xprt->xp_verf;
408 rply.acpted_rply.ar_stat = PROC_UNAVAIL;
409 SVC_REPLY(xprt, &rply);
413 * Can't decode args error reply
421 assert(xprt != NULL);
423 rply.rm_direction = REPLY;
424 rply.rm_reply.rp_stat = MSG_ACCEPTED;
425 rply.acpted_rply.ar_verf = xprt->xp_verf;
426 rply.acpted_rply.ar_stat = GARBAGE_ARGS;
427 SVC_REPLY(xprt, &rply);
434 svcerr_systemerr(xprt)
439 assert(xprt != NULL);
441 rply.rm_direction = REPLY;
442 rply.rm_reply.rp_stat = MSG_ACCEPTED;
443 rply.acpted_rply.ar_verf = xprt->xp_verf;
444 rply.acpted_rply.ar_stat = SYSTEM_ERR;
445 SVC_REPLY(xprt, &rply);
450 * Tell RPC package to not complain about version errors to the client. This
451 * is useful when revving broadcast protocols that sit on a fixed address.
452 * There is really one (or should be only one) example of this kind of
453 * protocol: the portmapper (or rpc binder).
456 __svc_versquiet_on(xprt)
460 SVC_EXT(xprt)->xp_flags |= SVC_VERSQUIET;
464 __svc_versquiet_off(xprt)
468 SVC_EXT(xprt)->xp_flags &= ~SVC_VERSQUIET;
475 __svc_versquiet_on(xprt);
479 __svc_versquiet_get(xprt)
483 return (SVC_EXT(xprt)->xp_flags & SVC_VERSQUIET);
488 * Authentication error reply
491 svcerr_auth(xprt, why)
497 assert(xprt != NULL);
499 rply.rm_direction = REPLY;
500 rply.rm_reply.rp_stat = MSG_DENIED;
501 rply.rjcted_rply.rj_stat = AUTH_ERROR;
502 rply.rjcted_rply.rj_why = why;
503 SVC_REPLY(xprt, &rply);
507 * Auth too weak error reply
510 svcerr_weakauth(xprt)
514 assert(xprt != NULL);
516 svcerr_auth(xprt, AUTH_TOOWEAK);
520 * Program unavailable error reply
528 assert(xprt != NULL);
530 rply.rm_direction = REPLY;
531 rply.rm_reply.rp_stat = MSG_ACCEPTED;
532 rply.acpted_rply.ar_verf = xprt->xp_verf;
533 rply.acpted_rply.ar_stat = PROG_UNAVAIL;
534 SVC_REPLY(xprt, &rply);
538 * Program version mismatch error reply
541 svcerr_progvers(xprt, low_vers, high_vers)
548 assert(xprt != NULL);
550 rply.rm_direction = REPLY;
551 rply.rm_reply.rp_stat = MSG_ACCEPTED;
552 rply.acpted_rply.ar_verf = xprt->xp_verf;
553 rply.acpted_rply.ar_stat = PROG_MISMATCH;
554 rply.acpted_rply.ar_vers.low = (u_int32_t)low_vers;
555 rply.acpted_rply.ar_vers.high = (u_int32_t)high_vers;
556 SVC_REPLY(xprt, &rply);
560 * Allocate a new server transport structure. All fields are
561 * initialized to zero and xp_p3 is initialized to point at an
562 * extension structure to hold various flags and authentication
571 xprt = mem_alloc(sizeof(SVCXPRT));
574 memset(xprt, 0, sizeof(SVCXPRT));
575 ext = mem_alloc(sizeof(SVCXPRT_EXT));
577 mem_free(xprt, sizeof(SVCXPRT));
580 memset(ext, 0, sizeof(SVCXPRT_EXT));
582 ext->xp_auth.svc_ah_ops = &svc_auth_null_ops;
588 * Free a server transport structure.
595 mem_free(xprt->xp_p3, sizeof(SVCXPRT_EXT));
596 mem_free(xprt, sizeof(SVCXPRT));
599 /* ******************* SERVER INPUT STUFF ******************* */
602 * Get server side input from some transport.
604 * Statement of authentication parameters management:
605 * This function owns and manages all authentication parameters, specifically
606 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
607 * the "cooked" credentials (rqst->rq_clntcred).
608 * However, this function does not know the structure of the cooked
609 * credentials, so it make the following assumptions:
610 * a) the structure is contiguous (no pointers), and
611 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
612 * In all events, all three parameters are freed upon exit from this routine.
613 * The storage is trivially management on the call stack in user land, but
614 * is mallocated in kernel land.
624 readfds.fds_bits[0] = rdfds;
625 svc_getreqset(&readfds);
629 svc_getreqset(readfds)
633 fd_mask mask, *maskp;
636 assert(readfds != NULL);
638 maskp = readfds->fds_bits;
639 for (sock = 0; sock < FD_SETSIZE; sock += NFDBITS) {
640 for (mask = *maskp++; (bit = ffsl(mask)) != 0;
641 mask ^= (1ul << (bit - 1))) {
642 /* sock has input waiting */
644 svc_getreq_common(fd);
650 svc_getreq_common(fd)
660 char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
662 msg.rm_call.cb_cred.oa_base = cred_area;
663 msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
664 r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
666 rwlock_rdlock(&svc_fd_lock);
667 xprt = __svc_xports[fd];
668 rwlock_unlock(&svc_fd_lock);
670 /* But do we control sock? */
672 /* now receive msgs from xprtprt (support batch calls) */
674 if (SVC_RECV(xprt, &msg)) {
676 /* now find the exported program and call it */
677 struct svc_callout *s;
681 r.rq_prog = msg.rm_call.cb_prog;
682 r.rq_vers = msg.rm_call.cb_vers;
683 r.rq_proc = msg.rm_call.cb_proc;
684 r.rq_cred = msg.rm_call.cb_cred;
685 /* first authenticate the message */
686 if ((why = _authenticate(&r, &msg)) != AUTH_OK) {
688 * RPCSEC_GSS uses this return code
689 * for requests that form part of its
690 * context establishment protocol and
691 * should not be dispatched to the
694 if (why != RPCSEC_GSS_NODISPATCH)
695 svcerr_auth(xprt, why);
698 /* now match message with a registered service*/
700 low_vers = (rpcvers_t) -1L;
701 high_vers = (rpcvers_t) 0L;
702 for (s = svc_head; s != NULL; s = s->sc_next) {
703 if (s->sc_prog == r.rq_prog) {
704 if (s->sc_vers == r.rq_vers) {
705 (*s->sc_dispatch)(&r, xprt);
707 } /* found correct version */
709 if (s->sc_vers < low_vers)
710 low_vers = s->sc_vers;
711 if (s->sc_vers > high_vers)
712 high_vers = s->sc_vers;
713 } /* found correct program */
716 * if we got here, the program or version
720 svcerr_progvers(xprt, low_vers, high_vers);
723 /* Fall through to ... */
726 * Check if the xprt has been disconnected in a
727 * recursive call in the service dispatch routine.
730 rwlock_rdlock(&svc_fd_lock);
731 if (xprt != __svc_xports[fd]) {
732 rwlock_unlock(&svc_fd_lock);
735 rwlock_unlock(&svc_fd_lock);
737 if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
741 } while (stat == XPRT_MOREREQS);
746 svc_getreq_poll(pfdp, pollretval)
753 for (i = fds_found = 0; fds_found < pollretval; i++) {
754 struct pollfd *p = &pfdp[i];
757 /* fd has input waiting */
760 * We assume that this function is only called
761 * via someone _select()ing from svc_fdset or
762 * _poll()ing from svc_pollset[]. Thus it's safe
763 * to handle the POLLNVAL event by simply turning
764 * the corresponding bit off in svc_fdset. The
765 * svc_pollset[] array is derived from svc_fdset
766 * and so will also be updated eventually.
768 * XXX Should we do an xprt_unregister() instead?
770 if (p->revents & POLLNVAL) {
771 rwlock_wrlock(&svc_fd_lock);
772 FD_CLR(p->fd, &svc_fdset);
773 rwlock_unlock(&svc_fd_lock);
775 svc_getreq_common(p->fd);
781 rpc_control(int what, void *arg)
786 case RPC_SVC_CONNMAXREC_SET:
792 case RPC_SVC_CONNMAXREC_GET:
793 *(int *)arg = __svc_maxrec;