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.
100 xprt_register(SVCXPRT *xprt)
104 assert(xprt != NULL);
108 rwlock_wrlock(&svc_fd_lock);
109 if (__svc_xports == NULL) {
110 __svc_xports = (SVCXPRT **)
111 mem_alloc((FD_SETSIZE + 1) * sizeof(SVCXPRT *));
112 if (__svc_xports == NULL) {
113 rwlock_unlock(&svc_fd_lock);
116 memset(__svc_xports, '\0', (FD_SETSIZE + 1) * sizeof(SVCXPRT *));
118 if (sock < FD_SETSIZE) {
119 __svc_xports[sock] = xprt;
120 FD_SET(sock, &svc_fdset);
121 svc_maxfd = max(svc_maxfd, sock);
122 } else if (sock == FD_SETSIZE)
123 __svc_xports[sock] = xprt;
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(SVCXPRT *xprt, bool_t dolock)
147 assert(xprt != NULL);
152 rwlock_wrlock(&svc_fd_lock);
153 if ((sock < FD_SETSIZE) && (__svc_xports[sock] == xprt)) {
154 __svc_xports[sock] = NULL;
155 FD_CLR(sock, &svc_fdset);
156 if (sock >= svc_maxfd) {
157 for (svc_maxfd--; svc_maxfd>=0; svc_maxfd--)
158 if (__svc_xports[svc_maxfd])
161 } else if ((sock == FD_SETSIZE) && (__svc_xports[sock] == xprt))
162 __svc_xports[sock] = NULL;
164 rwlock_unlock(&svc_fd_lock);
168 * Add a service program to the callout list.
169 * The dispatch routine will be called when a rpc request for this
170 * program number comes in.
173 svc_reg(SVCXPRT *xprt, const rpcprog_t prog, const rpcvers_t vers,
174 void (*dispatch)(struct svc_req *, SVCXPRT *),
175 const struct netconfig *nconf)
178 struct svc_callout *prev;
179 struct svc_callout *s;
180 struct netconfig *tnconf;
184 /* VARIABLES PROTECTED BY svc_lock: s, prev, svc_head */
186 if (xprt->xp_netid) {
187 netid = strdup(xprt->xp_netid);
189 } else if (nconf && nconf->nc_netid) {
190 netid = strdup(nconf->nc_netid);
192 } else if ((tnconf = __rpcgettp(xprt->xp_fd)) != NULL) {
193 netid = strdup(tnconf->nc_netid);
195 freenetconfigent(tnconf);
196 } /* must have been created with svc_raw_create */
197 if ((netid == NULL) && (flag == 1)) {
201 rwlock_wrlock(&svc_lock);
202 if ((s = svc_find(prog, vers, &prev, netid)) != NULL) {
204 if (s->sc_dispatch == dispatch)
205 goto rpcb_it; /* he is registering another xptr */
206 rwlock_unlock(&svc_lock);
209 s = mem_alloc(sizeof (struct svc_callout));
212 rwlock_unlock(&svc_lock);
218 s->sc_dispatch = dispatch;
220 s->sc_next = svc_head;
223 if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
224 ((SVCXPRT *) xprt)->xp_netid = strdup(netid);
227 rwlock_unlock(&svc_lock);
228 /* now register the information with the local binder service */
230 /*LINTED const castaway*/
231 dummy = rpcb_set(prog, vers, (struct netconfig *) nconf,
232 &((SVCXPRT *) xprt)->xp_ltaddr);
239 * Remove a service program from the callout list.
242 svc_unreg(const rpcprog_t prog, const rpcvers_t vers)
244 struct svc_callout *prev;
245 struct svc_callout *s;
247 /* unregister the information anyway */
248 (void) rpcb_unset(prog, vers, NULL);
249 rwlock_wrlock(&svc_lock);
250 while ((s = svc_find(prog, vers, &prev, NULL)) != NULL) {
252 svc_head = s->sc_next;
254 prev->sc_next = s->sc_next;
258 mem_free(s->sc_netid, sizeof (s->sc_netid) + 1);
259 mem_free(s, sizeof (struct svc_callout));
261 rwlock_unlock(&svc_lock);
264 /* ********************** CALLOUT list related stuff ************* */
268 * Add a service program to the callout list.
269 * The dispatch routine will be called when a rpc request for this
270 * program number comes in.
273 svc_register(SVCXPRT *xprt, u_long prog, u_long vers,
274 void (*dispatch)(struct svc_req *, SVCXPRT *),
277 struct svc_callout *prev;
278 struct svc_callout *s;
280 assert(xprt != NULL);
281 assert(dispatch != NULL);
283 if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) !=
285 if (s->sc_dispatch == dispatch)
286 goto pmap_it; /* he is registering another xptr */
289 s = mem_alloc(sizeof(struct svc_callout));
293 s->sc_prog = (rpcprog_t)prog;
294 s->sc_vers = (rpcvers_t)vers;
295 s->sc_dispatch = dispatch;
296 s->sc_next = svc_head;
299 /* now register the information with the local binder service */
301 return (pmap_set(prog, vers, protocol, xprt->xp_port));
307 * Remove a service program from the callout list.
310 svc_unregister(u_long prog, u_long vers)
312 struct svc_callout *prev;
313 struct svc_callout *s;
315 if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) ==
319 svc_head = s->sc_next;
321 prev->sc_next = s->sc_next;
324 mem_free(s, sizeof(struct svc_callout));
325 /* now unregister the information with the local binder service */
326 (void)pmap_unset(prog, vers);
331 * Search the callout list for a program number, return the callout
334 static struct svc_callout *
335 svc_find(rpcprog_t prog, rpcvers_t vers, struct svc_callout **prev,
338 struct svc_callout *s, *p;
340 assert(prev != NULL);
343 for (s = svc_head; s != NULL; s = s->sc_next) {
344 if (((s->sc_prog == prog) && (s->sc_vers == vers)) &&
345 ((netid == NULL) || (s->sc_netid == NULL) ||
346 (strcmp(netid, s->sc_netid) == 0)))
354 /* ******************* REPLY GENERATION ROUTINES ************ */
357 * Send a reply to an rpc request
360 svc_sendreply(SVCXPRT *xprt, xdrproc_t xdr_results,
365 assert(xprt != NULL);
367 rply.rm_direction = REPLY;
368 rply.rm_reply.rp_stat = MSG_ACCEPTED;
369 rply.acpted_rply.ar_verf = xprt->xp_verf;
370 rply.acpted_rply.ar_stat = SUCCESS;
371 rply.acpted_rply.ar_results.where = xdr_location;
372 rply.acpted_rply.ar_results.proc = xdr_results;
373 return (SVC_REPLY(xprt, &rply));
377 * No procedure error reply
380 svcerr_noproc(SVCXPRT *xprt)
384 assert(xprt != NULL);
386 rply.rm_direction = REPLY;
387 rply.rm_reply.rp_stat = MSG_ACCEPTED;
388 rply.acpted_rply.ar_verf = xprt->xp_verf;
389 rply.acpted_rply.ar_stat = PROC_UNAVAIL;
390 SVC_REPLY(xprt, &rply);
394 * Can't decode args error reply
397 svcerr_decode(SVCXPRT *xprt)
401 assert(xprt != NULL);
403 rply.rm_direction = REPLY;
404 rply.rm_reply.rp_stat = MSG_ACCEPTED;
405 rply.acpted_rply.ar_verf = xprt->xp_verf;
406 rply.acpted_rply.ar_stat = GARBAGE_ARGS;
407 SVC_REPLY(xprt, &rply);
414 svcerr_systemerr(SVCXPRT *xprt)
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 = SYSTEM_ERR;
424 SVC_REPLY(xprt, &rply);
429 * Tell RPC package to not complain about version errors to the client. This
430 * is useful when revving broadcast protocols that sit on a fixed address.
431 * There is really one (or should be only one) example of this kind of
432 * protocol: the portmapper (or rpc binder).
435 __svc_versquiet_on(SVCXPRT *xprt)
438 SVC_EXT(xprt)->xp_flags |= SVC_VERSQUIET;
442 __svc_versquiet_off(SVCXPRT *xprt)
445 SVC_EXT(xprt)->xp_flags &= ~SVC_VERSQUIET;
449 svc_versquiet(SVCXPRT *xprt)
451 __svc_versquiet_on(xprt);
455 __svc_versquiet_get(SVCXPRT *xprt)
458 return (SVC_EXT(xprt)->xp_flags & SVC_VERSQUIET);
463 * Authentication error reply
466 svcerr_auth(SVCXPRT *xprt, enum auth_stat why)
470 assert(xprt != NULL);
472 rply.rm_direction = REPLY;
473 rply.rm_reply.rp_stat = MSG_DENIED;
474 rply.rjcted_rply.rj_stat = AUTH_ERROR;
475 rply.rjcted_rply.rj_why = why;
476 SVC_REPLY(xprt, &rply);
480 * Auth too weak error reply
483 svcerr_weakauth(SVCXPRT *xprt)
486 assert(xprt != NULL);
488 svcerr_auth(xprt, AUTH_TOOWEAK);
492 * Program unavailable error reply
495 svcerr_noprog(SVCXPRT *xprt)
499 assert(xprt != NULL);
501 rply.rm_direction = REPLY;
502 rply.rm_reply.rp_stat = MSG_ACCEPTED;
503 rply.acpted_rply.ar_verf = xprt->xp_verf;
504 rply.acpted_rply.ar_stat = PROG_UNAVAIL;
505 SVC_REPLY(xprt, &rply);
509 * Program version mismatch error reply
512 svcerr_progvers(SVCXPRT *xprt, rpcvers_t low_vers, rpcvers_t high_vers)
516 assert(xprt != NULL);
518 rply.rm_direction = REPLY;
519 rply.rm_reply.rp_stat = MSG_ACCEPTED;
520 rply.acpted_rply.ar_verf = xprt->xp_verf;
521 rply.acpted_rply.ar_stat = PROG_MISMATCH;
522 rply.acpted_rply.ar_vers.low = (u_int32_t)low_vers;
523 rply.acpted_rply.ar_vers.high = (u_int32_t)high_vers;
524 SVC_REPLY(xprt, &rply);
528 * Allocate a new server transport structure. All fields are
529 * initialized to zero and xp_p3 is initialized to point at an
530 * extension structure to hold various flags and authentication
539 xprt = mem_alloc(sizeof(SVCXPRT));
542 memset(xprt, 0, sizeof(SVCXPRT));
543 ext = mem_alloc(sizeof(SVCXPRT_EXT));
545 mem_free(xprt, sizeof(SVCXPRT));
548 memset(ext, 0, sizeof(SVCXPRT_EXT));
550 ext->xp_auth.svc_ah_ops = &svc_auth_null_ops;
556 * Free a server transport structure.
559 svc_xprt_free(SVCXPRT *xprt)
562 mem_free(xprt->xp_p3, sizeof(SVCXPRT_EXT));
563 mem_free(xprt, sizeof(SVCXPRT));
566 /* ******************* SERVER INPUT STUFF ******************* */
569 * Get server side input from some transport.
571 * Statement of authentication parameters management:
572 * This function owns and manages all authentication parameters, specifically
573 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
574 * the "cooked" credentials (rqst->rq_clntcred).
575 * However, this function does not know the structure of the cooked
576 * credentials, so it make the following assumptions:
577 * a) the structure is contiguous (no pointers), and
578 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
579 * In all events, all three parameters are freed upon exit from this routine.
580 * The storage is trivially management on the call stack in user land, but
581 * is mallocated in kernel land.
585 svc_getreq(int rdfds)
590 readfds.fds_bits[0] = rdfds;
591 svc_getreqset(&readfds);
595 svc_getreqset(fd_set *readfds)
598 fd_mask mask, *maskp;
601 assert(readfds != NULL);
603 maskp = readfds->fds_bits;
604 for (sock = 0; sock < FD_SETSIZE; sock += NFDBITS) {
605 for (mask = *maskp++; (bit = ffsl(mask)) != 0;
606 mask ^= (1ul << (bit - 1))) {
607 /* sock has input waiting */
609 svc_getreq_common(fd);
615 svc_getreq_common(int fd)
624 char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
626 msg.rm_call.cb_cred.oa_base = cred_area;
627 msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
628 r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
630 rwlock_rdlock(&svc_fd_lock);
631 xprt = __svc_xports[fd];
632 rwlock_unlock(&svc_fd_lock);
634 /* But do we control sock? */
636 /* now receive msgs from xprtprt (support batch calls) */
638 if (SVC_RECV(xprt, &msg)) {
640 /* now find the exported program and call it */
641 struct svc_callout *s;
645 r.rq_prog = msg.rm_call.cb_prog;
646 r.rq_vers = msg.rm_call.cb_vers;
647 r.rq_proc = msg.rm_call.cb_proc;
648 r.rq_cred = msg.rm_call.cb_cred;
649 /* first authenticate the message */
650 if ((why = _authenticate(&r, &msg)) != AUTH_OK) {
652 * RPCSEC_GSS uses this return code
653 * for requests that form part of its
654 * context establishment protocol and
655 * should not be dispatched to the
658 if (why != RPCSEC_GSS_NODISPATCH)
659 svcerr_auth(xprt, why);
662 /* now match message with a registered service*/
664 low_vers = (rpcvers_t) -1L;
665 high_vers = (rpcvers_t) 0L;
666 for (s = svc_head; s != NULL; s = s->sc_next) {
667 if (s->sc_prog == r.rq_prog) {
668 if (s->sc_vers == r.rq_vers) {
669 (*s->sc_dispatch)(&r, xprt);
671 } /* found correct version */
673 if (s->sc_vers < low_vers)
674 low_vers = s->sc_vers;
675 if (s->sc_vers > high_vers)
676 high_vers = s->sc_vers;
677 } /* found correct program */
680 * if we got here, the program or version
684 svcerr_progvers(xprt, low_vers, high_vers);
687 /* Fall through to ... */
690 * Check if the xprt has been disconnected in a
691 * recursive call in the service dispatch routine.
694 rwlock_rdlock(&svc_fd_lock);
695 if (xprt != __svc_xports[fd]) {
696 rwlock_unlock(&svc_fd_lock);
699 rwlock_unlock(&svc_fd_lock);
701 if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
705 } while (stat == XPRT_MOREREQS);
710 svc_getreq_poll(struct pollfd *pfdp, int pollretval)
715 for (i = fds_found = 0; fds_found < pollretval; i++) {
716 struct pollfd *p = &pfdp[i];
719 /* fd has input waiting */
722 * We assume that this function is only called
723 * via someone _select()ing from svc_fdset or
724 * _poll()ing from svc_pollset[]. Thus it's safe
725 * to handle the POLLNVAL event by simply turning
726 * the corresponding bit off in svc_fdset. The
727 * svc_pollset[] array is derived from svc_fdset
728 * and so will also be updated eventually.
730 * XXX Should we do an xprt_unregister() instead?
732 if (p->revents & POLLNVAL) {
733 rwlock_wrlock(&svc_fd_lock);
734 FD_CLR(p->fd, &svc_fdset);
735 rwlock_unlock(&svc_fd_lock);
737 svc_getreq_common(p->fd);
743 rpc_control(int what, void *arg)
748 case RPC_SVC_CONNMAXREC_SET:
754 case RPC_SVC_CONNMAXREC_GET:
755 *(int *)arg = __svc_maxrec;