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 * sizeof(SVCXPRT *));
112 if (__svc_xports == NULL) {
113 rwlock_unlock(&svc_fd_lock);
116 memset(__svc_xports, '\0', FD_SETSIZE * 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);
123 rwlock_unlock(&svc_fd_lock);
127 xprt_unregister(SVCXPRT *xprt)
129 __xprt_do_unregister(xprt, TRUE);
133 __xprt_unregister_unlocked(SVCXPRT *xprt)
135 __xprt_do_unregister(xprt, FALSE);
139 * De-activate a transport handle.
142 __xprt_do_unregister(SVCXPRT *xprt, bool_t 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(SVCXPRT *xprt, const rpcprog_t prog, const rpcvers_t vers,
172 void (*dispatch)(struct svc_req *, SVCXPRT *),
173 const struct netconfig *nconf)
176 struct svc_callout *prev;
177 struct svc_callout *s;
178 struct netconfig *tnconf;
182 /* VARIABLES PROTECTED BY svc_lock: s, prev, svc_head */
184 if (xprt->xp_netid) {
185 netid = strdup(xprt->xp_netid);
187 } else if (nconf && nconf->nc_netid) {
188 netid = strdup(nconf->nc_netid);
190 } else if ((tnconf = __rpcgettp(xprt->xp_fd)) != NULL) {
191 netid = strdup(tnconf->nc_netid);
193 freenetconfigent(tnconf);
194 } /* must have been created with svc_raw_create */
195 if ((netid == NULL) && (flag == 1)) {
199 rwlock_wrlock(&svc_lock);
200 if ((s = svc_find(prog, vers, &prev, netid)) != NULL) {
202 if (s->sc_dispatch == dispatch)
203 goto rpcb_it; /* he is registering another xptr */
204 rwlock_unlock(&svc_lock);
207 s = mem_alloc(sizeof (struct svc_callout));
210 rwlock_unlock(&svc_lock);
216 s->sc_dispatch = dispatch;
218 s->sc_next = svc_head;
221 if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
222 ((SVCXPRT *) xprt)->xp_netid = strdup(netid);
225 rwlock_unlock(&svc_lock);
226 /* now register the information with the local binder service */
228 /*LINTED const castaway*/
229 dummy = rpcb_set(prog, vers, (struct netconfig *) nconf,
230 &((SVCXPRT *) xprt)->xp_ltaddr);
237 * Remove a service program from the callout list.
240 svc_unreg(const rpcprog_t prog, const rpcvers_t vers)
242 struct svc_callout *prev;
243 struct svc_callout *s;
245 /* unregister the information anyway */
246 (void) rpcb_unset(prog, vers, NULL);
247 rwlock_wrlock(&svc_lock);
248 while ((s = svc_find(prog, vers, &prev, NULL)) != NULL) {
250 svc_head = s->sc_next;
252 prev->sc_next = s->sc_next;
256 mem_free(s->sc_netid, sizeof (s->sc_netid) + 1);
257 mem_free(s, sizeof (struct svc_callout));
259 rwlock_unlock(&svc_lock);
262 /* ********************** CALLOUT list related stuff ************* */
266 * Add a service program to the callout list.
267 * The dispatch routine will be called when a rpc request for this
268 * program number comes in.
271 svc_register(SVCXPRT *xprt, u_long prog, u_long vers,
272 void (*dispatch)(struct svc_req *, SVCXPRT *),
275 struct svc_callout *prev;
276 struct svc_callout *s;
278 assert(xprt != NULL);
279 assert(dispatch != NULL);
281 if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) !=
283 if (s->sc_dispatch == dispatch)
284 goto pmap_it; /* he is registering another xptr */
287 s = mem_alloc(sizeof(struct svc_callout));
291 s->sc_prog = (rpcprog_t)prog;
292 s->sc_vers = (rpcvers_t)vers;
293 s->sc_dispatch = dispatch;
294 s->sc_next = svc_head;
297 /* now register the information with the local binder service */
299 return (pmap_set(prog, vers, protocol, xprt->xp_port));
305 * Remove a service program from the callout list.
308 svc_unregister(u_long prog, u_long vers)
310 struct svc_callout *prev;
311 struct svc_callout *s;
313 if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) ==
317 svc_head = s->sc_next;
319 prev->sc_next = s->sc_next;
322 mem_free(s, sizeof(struct svc_callout));
323 /* now unregister the information with the local binder service */
324 (void)pmap_unset(prog, vers);
329 * Search the callout list for a program number, return the callout
332 static struct svc_callout *
333 svc_find(rpcprog_t prog, rpcvers_t vers, struct svc_callout **prev,
336 struct svc_callout *s, *p;
338 assert(prev != NULL);
341 for (s = svc_head; s != NULL; s = s->sc_next) {
342 if (((s->sc_prog == prog) && (s->sc_vers == vers)) &&
343 ((netid == NULL) || (s->sc_netid == NULL) ||
344 (strcmp(netid, s->sc_netid) == 0)))
352 /* ******************* REPLY GENERATION ROUTINES ************ */
355 * Send a reply to an rpc request
358 svc_sendreply(SVCXPRT *xprt, xdrproc_t xdr_results,
363 assert(xprt != NULL);
365 rply.rm_direction = REPLY;
366 rply.rm_reply.rp_stat = MSG_ACCEPTED;
367 rply.acpted_rply.ar_verf = xprt->xp_verf;
368 rply.acpted_rply.ar_stat = SUCCESS;
369 rply.acpted_rply.ar_results.where = xdr_location;
370 rply.acpted_rply.ar_results.proc = xdr_results;
371 return (SVC_REPLY(xprt, &rply));
375 * No procedure error reply
378 svcerr_noproc(SVCXPRT *xprt)
382 assert(xprt != NULL);
384 rply.rm_direction = REPLY;
385 rply.rm_reply.rp_stat = MSG_ACCEPTED;
386 rply.acpted_rply.ar_verf = xprt->xp_verf;
387 rply.acpted_rply.ar_stat = PROC_UNAVAIL;
388 SVC_REPLY(xprt, &rply);
392 * Can't decode args error reply
395 svcerr_decode(SVCXPRT *xprt)
399 assert(xprt != NULL);
401 rply.rm_direction = REPLY;
402 rply.rm_reply.rp_stat = MSG_ACCEPTED;
403 rply.acpted_rply.ar_verf = xprt->xp_verf;
404 rply.acpted_rply.ar_stat = GARBAGE_ARGS;
405 SVC_REPLY(xprt, &rply);
412 svcerr_systemerr(SVCXPRT *xprt)
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 = SYSTEM_ERR;
422 SVC_REPLY(xprt, &rply);
427 * Tell RPC package to not complain about version errors to the client. This
428 * is useful when revving broadcast protocols that sit on a fixed address.
429 * There is really one (or should be only one) example of this kind of
430 * protocol: the portmapper (or rpc binder).
433 __svc_versquiet_on(SVCXPRT *xprt)
436 SVC_EXT(xprt)->xp_flags |= SVC_VERSQUIET;
440 __svc_versquiet_off(SVCXPRT *xprt)
443 SVC_EXT(xprt)->xp_flags &= ~SVC_VERSQUIET;
447 svc_versquiet(SVCXPRT *xprt)
449 __svc_versquiet_on(xprt);
453 __svc_versquiet_get(SVCXPRT *xprt)
456 return (SVC_EXT(xprt)->xp_flags & SVC_VERSQUIET);
461 * Authentication error reply
464 svcerr_auth(SVCXPRT *xprt, enum auth_stat why)
468 assert(xprt != NULL);
470 rply.rm_direction = REPLY;
471 rply.rm_reply.rp_stat = MSG_DENIED;
472 rply.rjcted_rply.rj_stat = AUTH_ERROR;
473 rply.rjcted_rply.rj_why = why;
474 SVC_REPLY(xprt, &rply);
478 * Auth too weak error reply
481 svcerr_weakauth(SVCXPRT *xprt)
484 assert(xprt != NULL);
486 svcerr_auth(xprt, AUTH_TOOWEAK);
490 * Program unavailable error reply
493 svcerr_noprog(SVCXPRT *xprt)
497 assert(xprt != NULL);
499 rply.rm_direction = REPLY;
500 rply.rm_reply.rp_stat = MSG_ACCEPTED;
501 rply.acpted_rply.ar_verf = xprt->xp_verf;
502 rply.acpted_rply.ar_stat = PROG_UNAVAIL;
503 SVC_REPLY(xprt, &rply);
507 * Program version mismatch error reply
510 svcerr_progvers(SVCXPRT *xprt, rpcvers_t low_vers, rpcvers_t high_vers)
514 assert(xprt != NULL);
516 rply.rm_direction = REPLY;
517 rply.rm_reply.rp_stat = MSG_ACCEPTED;
518 rply.acpted_rply.ar_verf = xprt->xp_verf;
519 rply.acpted_rply.ar_stat = PROG_MISMATCH;
520 rply.acpted_rply.ar_vers.low = (u_int32_t)low_vers;
521 rply.acpted_rply.ar_vers.high = (u_int32_t)high_vers;
522 SVC_REPLY(xprt, &rply);
526 * Allocate a new server transport structure. All fields are
527 * initialized to zero and xp_p3 is initialized to point at an
528 * extension structure to hold various flags and authentication
537 xprt = mem_alloc(sizeof(SVCXPRT));
540 memset(xprt, 0, sizeof(SVCXPRT));
541 ext = mem_alloc(sizeof(SVCXPRT_EXT));
543 mem_free(xprt, sizeof(SVCXPRT));
546 memset(ext, 0, sizeof(SVCXPRT_EXT));
548 ext->xp_auth.svc_ah_ops = &svc_auth_null_ops;
554 * Free a server transport structure.
557 svc_xprt_free(SVCXPRT *xprt)
560 mem_free(xprt->xp_p3, sizeof(SVCXPRT_EXT));
561 mem_free(xprt, sizeof(SVCXPRT));
564 /* ******************* SERVER INPUT STUFF ******************* */
567 * Get server side input from some transport.
569 * Statement of authentication parameters management:
570 * This function owns and manages all authentication parameters, specifically
571 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
572 * the "cooked" credentials (rqst->rq_clntcred).
573 * However, this function does not know the structure of the cooked
574 * credentials, so it make the following assumptions:
575 * a) the structure is contiguous (no pointers), and
576 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
577 * In all events, all three parameters are freed upon exit from this routine.
578 * The storage is trivially management on the call stack in user land, but
579 * is mallocated in kernel land.
583 svc_getreq(int rdfds)
588 readfds.fds_bits[0] = rdfds;
589 svc_getreqset(&readfds);
593 svc_getreqset(fd_set *readfds)
596 fd_mask mask, *maskp;
599 assert(readfds != NULL);
601 maskp = readfds->fds_bits;
602 for (sock = 0; sock < FD_SETSIZE; sock += NFDBITS) {
603 for (mask = *maskp++; (bit = ffsl(mask)) != 0;
604 mask ^= (1ul << (bit - 1))) {
605 /* sock has input waiting */
607 svc_getreq_common(fd);
613 svc_getreq_common(int fd)
622 char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
624 msg.rm_call.cb_cred.oa_base = cred_area;
625 msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
626 r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
628 rwlock_rdlock(&svc_fd_lock);
629 xprt = __svc_xports[fd];
630 rwlock_unlock(&svc_fd_lock);
632 /* But do we control sock? */
634 /* now receive msgs from xprtprt (support batch calls) */
636 if (SVC_RECV(xprt, &msg)) {
638 /* now find the exported program and call it */
639 struct svc_callout *s;
643 r.rq_prog = msg.rm_call.cb_prog;
644 r.rq_vers = msg.rm_call.cb_vers;
645 r.rq_proc = msg.rm_call.cb_proc;
646 r.rq_cred = msg.rm_call.cb_cred;
647 /* first authenticate the message */
648 if ((why = _authenticate(&r, &msg)) != AUTH_OK) {
650 * RPCSEC_GSS uses this return code
651 * for requests that form part of its
652 * context establishment protocol and
653 * should not be dispatched to the
656 if (why != RPCSEC_GSS_NODISPATCH)
657 svcerr_auth(xprt, why);
660 /* now match message with a registered service*/
662 low_vers = (rpcvers_t) -1L;
663 high_vers = (rpcvers_t) 0L;
664 for (s = svc_head; s != NULL; s = s->sc_next) {
665 if (s->sc_prog == r.rq_prog) {
666 if (s->sc_vers == r.rq_vers) {
667 (*s->sc_dispatch)(&r, xprt);
669 } /* found correct version */
671 if (s->sc_vers < low_vers)
672 low_vers = s->sc_vers;
673 if (s->sc_vers > high_vers)
674 high_vers = s->sc_vers;
675 } /* found correct program */
678 * if we got here, the program or version
682 svcerr_progvers(xprt, low_vers, high_vers);
685 /* Fall through to ... */
688 * Check if the xprt has been disconnected in a
689 * recursive call in the service dispatch routine.
692 rwlock_rdlock(&svc_fd_lock);
693 if (xprt != __svc_xports[fd]) {
694 rwlock_unlock(&svc_fd_lock);
697 rwlock_unlock(&svc_fd_lock);
699 if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
703 } while (stat == XPRT_MOREREQS);
708 svc_getreq_poll(struct pollfd *pfdp, int pollretval)
713 for (i = fds_found = 0; fds_found < pollretval; i++) {
714 struct pollfd *p = &pfdp[i];
717 /* fd has input waiting */
720 * We assume that this function is only called
721 * via someone _select()ing from svc_fdset or
722 * _poll()ing from svc_pollset[]. Thus it's safe
723 * to handle the POLLNVAL event by simply turning
724 * the corresponding bit off in svc_fdset. The
725 * svc_pollset[] array is derived from svc_fdset
726 * and so will also be updated eventually.
728 * XXX Should we do an xprt_unregister() instead?
730 if (p->revents & POLLNVAL) {
731 rwlock_wrlock(&svc_fd_lock);
732 FD_CLR(p->fd, &svc_fdset);
733 rwlock_unlock(&svc_fd_lock);
735 svc_getreq_common(p->fd);
741 rpc_control(int what, void *arg)
746 case RPC_SVC_CONNMAXREC_SET:
752 case RPC_SVC_CONNMAXREC_GET:
753 *(int *)arg = __svc_maxrec;