1 /* $NetBSD: svc.c,v 1.21 2000/07/06 03:10:35 christos Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 2009, Sun Microsystems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * - Neither the name of Sun Microsystems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char *sccsid2 = "@(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro";
35 static char *sccsid = "@(#)svc.c 2.4 88/08/11 4.0 RPCSRC";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 * svc.c, Server-side remote procedure call interface.
43 * There are two sets of procedures here. The xprt routines are
44 * for handling transport handles. The svc routines handle the
45 * list of service routines.
47 * Copyright (C) 1984, Sun Microsystems, Inc.
50 #include "namespace.h"
51 #include "reentrant.h"
52 #include <sys/types.h>
61 #include <rpc/pmap_clnt.h>
63 #include "un-namespace.h"
68 #define RQCRED_SIZE 400 /* this size is excessive */
70 #define SVC_VERSQUIET 0x0001 /* keep quiet about vers mismatch */
71 #define version_keepquiet(xp) (SVC_EXT(xp)->xp_flags & SVC_VERSQUIET)
73 #define max(a, b) (a > b ? a : b)
77 * Each entry represents a set of procedures (an rpc program).
78 * The dispatch routine takes request structs and runs the
79 * appropriate procedure.
81 static struct svc_callout {
82 struct svc_callout *sc_next;
86 void (*sc_dispatch)(struct svc_req *, SVCXPRT *);
89 SVCXPRT **__svc_xports;
92 static struct svc_callout *svc_find(rpcprog_t, rpcvers_t,
93 struct svc_callout **, char *);
94 static void __xprt_do_unregister (SVCXPRT *xprt, bool_t dolock);
96 /* *************** SVCXPRT related stuff **************** */
99 * Activate a transport handle.
102 xprt_register(SVCXPRT *xprt)
106 assert(xprt != NULL);
110 rwlock_wrlock(&svc_fd_lock);
111 if (__svc_xports == NULL) {
112 __svc_xports = (SVCXPRT **)
113 mem_alloc((FD_SETSIZE + 1) * sizeof(SVCXPRT *));
114 if (__svc_xports == NULL) {
115 rwlock_unlock(&svc_fd_lock);
118 memset(__svc_xports, '\0', (FD_SETSIZE + 1) * sizeof(SVCXPRT *));
120 if (sock < FD_SETSIZE) {
121 __svc_xports[sock] = xprt;
122 FD_SET(sock, &svc_fdset);
123 svc_maxfd = max(svc_maxfd, sock);
124 } else if (sock == FD_SETSIZE)
125 __svc_xports[sock] = xprt;
126 rwlock_unlock(&svc_fd_lock);
130 xprt_unregister(SVCXPRT *xprt)
132 __xprt_do_unregister(xprt, TRUE);
136 __xprt_unregister_unlocked(SVCXPRT *xprt)
138 __xprt_do_unregister(xprt, FALSE);
142 * De-activate a transport handle.
145 __xprt_do_unregister(SVCXPRT *xprt, bool_t 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])
163 } else if ((sock == FD_SETSIZE) && (__svc_xports[sock] == xprt))
164 __svc_xports[sock] = NULL;
166 rwlock_unlock(&svc_fd_lock);
170 * Add a service program to the callout list.
171 * The dispatch routine will be called when a rpc request for this
172 * program number comes in.
175 svc_reg(SVCXPRT *xprt, const rpcprog_t prog, const rpcvers_t vers,
176 void (*dispatch)(struct svc_req *, SVCXPRT *),
177 const struct netconfig *nconf)
180 struct svc_callout *prev;
181 struct svc_callout *s;
182 struct netconfig *tnconf;
186 /* VARIABLES PROTECTED BY svc_lock: s, prev, svc_head */
188 if (xprt->xp_netid) {
189 netid = strdup(xprt->xp_netid);
191 } else if (nconf && nconf->nc_netid) {
192 netid = strdup(nconf->nc_netid);
194 } else if ((tnconf = __rpcgettp(xprt->xp_fd)) != NULL) {
195 netid = strdup(tnconf->nc_netid);
197 freenetconfigent(tnconf);
198 } /* must have been created with svc_raw_create */
199 if ((netid == NULL) && (flag == 1)) {
203 rwlock_wrlock(&svc_lock);
204 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));
214 rwlock_unlock(&svc_lock);
220 s->sc_dispatch = dispatch;
222 s->sc_next = svc_head;
225 if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
226 ((SVCXPRT *) xprt)->xp_netid = strdup(netid);
229 rwlock_unlock(&svc_lock);
230 /* now register the information with the local binder service */
232 /*LINTED const castaway*/
233 dummy = rpcb_set(prog, vers, (struct netconfig *) nconf,
234 &((SVCXPRT *) xprt)->xp_ltaddr);
241 * Remove a service program from the callout list.
244 svc_unreg(const rpcprog_t prog, const rpcvers_t vers)
246 struct svc_callout *prev;
247 struct svc_callout *s;
249 /* unregister the information anyway */
250 (void) rpcb_unset(prog, vers, NULL);
251 rwlock_wrlock(&svc_lock);
252 while ((s = svc_find(prog, vers, &prev, NULL)) != NULL) {
254 svc_head = s->sc_next;
256 prev->sc_next = s->sc_next;
260 mem_free(s->sc_netid, sizeof (s->sc_netid) + 1);
261 mem_free(s, sizeof (struct svc_callout));
263 rwlock_unlock(&svc_lock);
266 /* ********************** CALLOUT list related stuff ************* */
270 * Add a service program to the callout list.
271 * The dispatch routine will be called when a rpc request for this
272 * program number comes in.
275 svc_register(SVCXPRT *xprt, u_long prog, u_long vers,
276 void (*dispatch)(struct svc_req *, SVCXPRT *),
279 struct svc_callout *prev;
280 struct svc_callout *s;
282 assert(xprt != NULL);
283 assert(dispatch != NULL);
285 if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) !=
287 if (s->sc_dispatch == dispatch)
288 goto pmap_it; /* he is registering another xptr */
291 s = mem_alloc(sizeof(struct svc_callout));
295 s->sc_prog = (rpcprog_t)prog;
296 s->sc_vers = (rpcvers_t)vers;
297 s->sc_dispatch = dispatch;
298 s->sc_next = svc_head;
301 /* now register the information with the local binder service */
303 return (pmap_set(prog, vers, protocol, xprt->xp_port));
309 * Remove a service program from the callout list.
312 svc_unregister(u_long prog, u_long vers)
314 struct svc_callout *prev;
315 struct svc_callout *s;
317 if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) ==
321 svc_head = s->sc_next;
323 prev->sc_next = s->sc_next;
326 mem_free(s, sizeof(struct svc_callout));
327 /* now unregister the information with the local binder service */
328 (void)pmap_unset(prog, vers);
333 * Search the callout list for a program number, return the callout
336 static struct svc_callout *
337 svc_find(rpcprog_t prog, rpcvers_t vers, struct svc_callout **prev,
340 struct svc_callout *s, *p;
342 assert(prev != NULL);
345 for (s = svc_head; s != NULL; s = s->sc_next) {
346 if (((s->sc_prog == prog) && (s->sc_vers == vers)) &&
347 ((netid == NULL) || (s->sc_netid == NULL) ||
348 (strcmp(netid, s->sc_netid) == 0)))
356 /* ******************* REPLY GENERATION ROUTINES ************ */
359 * Send a reply to an rpc request
362 svc_sendreply(SVCXPRT *xprt, xdrproc_t xdr_results,
367 assert(xprt != NULL);
369 rply.rm_direction = REPLY;
370 rply.rm_reply.rp_stat = MSG_ACCEPTED;
371 rply.acpted_rply.ar_verf = xprt->xp_verf;
372 rply.acpted_rply.ar_stat = SUCCESS;
373 rply.acpted_rply.ar_results.where = xdr_location;
374 rply.acpted_rply.ar_results.proc = xdr_results;
375 return (SVC_REPLY(xprt, &rply));
379 * No procedure error reply
382 svcerr_noproc(SVCXPRT *xprt)
386 assert(xprt != NULL);
388 rply.rm_direction = REPLY;
389 rply.rm_reply.rp_stat = MSG_ACCEPTED;
390 rply.acpted_rply.ar_verf = xprt->xp_verf;
391 rply.acpted_rply.ar_stat = PROC_UNAVAIL;
392 SVC_REPLY(xprt, &rply);
396 * Can't decode args error reply
399 svcerr_decode(SVCXPRT *xprt)
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 = GARBAGE_ARGS;
409 SVC_REPLY(xprt, &rply);
416 svcerr_systemerr(SVCXPRT *xprt)
420 assert(xprt != NULL);
422 rply.rm_direction = REPLY;
423 rply.rm_reply.rp_stat = MSG_ACCEPTED;
424 rply.acpted_rply.ar_verf = xprt->xp_verf;
425 rply.acpted_rply.ar_stat = SYSTEM_ERR;
426 SVC_REPLY(xprt, &rply);
431 * Tell RPC package to not complain about version errors to the client. This
432 * is useful when revving broadcast protocols that sit on a fixed address.
433 * There is really one (or should be only one) example of this kind of
434 * protocol: the portmapper (or rpc binder).
437 __svc_versquiet_on(SVCXPRT *xprt)
440 SVC_EXT(xprt)->xp_flags |= SVC_VERSQUIET;
444 __svc_versquiet_off(SVCXPRT *xprt)
447 SVC_EXT(xprt)->xp_flags &= ~SVC_VERSQUIET;
451 svc_versquiet(SVCXPRT *xprt)
453 __svc_versquiet_on(xprt);
457 __svc_versquiet_get(SVCXPRT *xprt)
460 return (SVC_EXT(xprt)->xp_flags & SVC_VERSQUIET);
465 * Authentication error reply
468 svcerr_auth(SVCXPRT *xprt, enum auth_stat why)
472 assert(xprt != NULL);
474 rply.rm_direction = REPLY;
475 rply.rm_reply.rp_stat = MSG_DENIED;
476 rply.rjcted_rply.rj_stat = AUTH_ERROR;
477 rply.rjcted_rply.rj_why = why;
478 SVC_REPLY(xprt, &rply);
482 * Auth too weak error reply
485 svcerr_weakauth(SVCXPRT *xprt)
488 assert(xprt != NULL);
490 svcerr_auth(xprt, AUTH_TOOWEAK);
494 * Program unavailable error reply
497 svcerr_noprog(SVCXPRT *xprt)
501 assert(xprt != NULL);
503 rply.rm_direction = REPLY;
504 rply.rm_reply.rp_stat = MSG_ACCEPTED;
505 rply.acpted_rply.ar_verf = xprt->xp_verf;
506 rply.acpted_rply.ar_stat = PROG_UNAVAIL;
507 SVC_REPLY(xprt, &rply);
511 * Program version mismatch error reply
514 svcerr_progvers(SVCXPRT *xprt, rpcvers_t low_vers, rpcvers_t high_vers)
518 assert(xprt != NULL);
520 rply.rm_direction = REPLY;
521 rply.rm_reply.rp_stat = MSG_ACCEPTED;
522 rply.acpted_rply.ar_verf = xprt->xp_verf;
523 rply.acpted_rply.ar_stat = PROG_MISMATCH;
524 rply.acpted_rply.ar_vers.low = (u_int32_t)low_vers;
525 rply.acpted_rply.ar_vers.high = (u_int32_t)high_vers;
526 SVC_REPLY(xprt, &rply);
530 * Allocate a new server transport structure. All fields are
531 * initialized to zero and xp_p3 is initialized to point at an
532 * extension structure to hold various flags and authentication
541 xprt = mem_alloc(sizeof(SVCXPRT));
544 memset(xprt, 0, sizeof(SVCXPRT));
545 ext = mem_alloc(sizeof(SVCXPRT_EXT));
547 mem_free(xprt, sizeof(SVCXPRT));
550 memset(ext, 0, sizeof(SVCXPRT_EXT));
552 ext->xp_auth.svc_ah_ops = &svc_auth_null_ops;
558 * Free a server transport structure.
561 svc_xprt_free(SVCXPRT *xprt)
564 mem_free(xprt->xp_p3, sizeof(SVCXPRT_EXT));
565 mem_free(xprt, sizeof(SVCXPRT));
568 /* ******************* SERVER INPUT STUFF ******************* */
571 * Get server side input from some transport.
573 * Statement of authentication parameters management:
574 * This function owns and manages all authentication parameters, specifically
575 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
576 * the "cooked" credentials (rqst->rq_clntcred).
577 * However, this function does not know the structure of the cooked
578 * credentials, so it make the following assumptions:
579 * a) the structure is contiguous (no pointers), and
580 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
581 * In all events, all three parameters are freed upon exit from this routine.
582 * The storage is trivially management on the call stack in user land, but
583 * is mallocated in kernel land.
587 svc_getreq(int rdfds)
592 readfds.fds_bits[0] = rdfds;
593 svc_getreqset(&readfds);
597 svc_getreqset(fd_set *readfds)
600 fd_mask mask, *maskp;
603 assert(readfds != NULL);
605 maskp = readfds->fds_bits;
606 for (sock = 0; sock < FD_SETSIZE; sock += NFDBITS) {
607 for (mask = *maskp++; (bit = ffsl(mask)) != 0;
608 mask ^= (1ul << (bit - 1))) {
609 /* sock has input waiting */
611 svc_getreq_common(fd);
617 svc_getreq_common(int fd)
626 char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
628 msg.rm_call.cb_cred.oa_base = cred_area;
629 msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
630 r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
632 rwlock_rdlock(&svc_fd_lock);
633 xprt = __svc_xports[fd];
634 rwlock_unlock(&svc_fd_lock);
636 /* But do we control sock? */
638 /* now receive msgs from xprtprt (support batch calls) */
640 if (SVC_RECV(xprt, &msg)) {
642 /* now find the exported program and call it */
643 struct svc_callout *s;
647 r.rq_prog = msg.rm_call.cb_prog;
648 r.rq_vers = msg.rm_call.cb_vers;
649 r.rq_proc = msg.rm_call.cb_proc;
650 r.rq_cred = msg.rm_call.cb_cred;
651 /* first authenticate the message */
652 if ((why = _authenticate(&r, &msg)) != AUTH_OK) {
654 * RPCSEC_GSS uses this return code
655 * for requests that form part of its
656 * context establishment protocol and
657 * should not be dispatched to the
660 if (why != RPCSEC_GSS_NODISPATCH)
661 svcerr_auth(xprt, why);
664 /* now match message with a registered service*/
666 low_vers = (rpcvers_t) -1L;
667 high_vers = (rpcvers_t) 0L;
668 for (s = svc_head; s != NULL; s = s->sc_next) {
669 if (s->sc_prog == r.rq_prog) {
670 if (s->sc_vers == r.rq_vers) {
671 (*s->sc_dispatch)(&r, xprt);
673 } /* found correct version */
675 if (s->sc_vers < low_vers)
676 low_vers = s->sc_vers;
677 if (s->sc_vers > high_vers)
678 high_vers = s->sc_vers;
679 } /* found correct program */
682 * if we got here, the program or version
686 svcerr_progvers(xprt, low_vers, high_vers);
689 /* Fall through to ... */
692 * Check if the xprt has been disconnected in a
693 * recursive call in the service dispatch routine.
696 rwlock_rdlock(&svc_fd_lock);
697 if (xprt != __svc_xports[fd]) {
698 rwlock_unlock(&svc_fd_lock);
701 rwlock_unlock(&svc_fd_lock);
703 if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
707 } while (stat == XPRT_MOREREQS);
712 svc_getreq_poll(struct pollfd *pfdp, int pollretval)
717 for (i = fds_found = 0; fds_found < pollretval; i++) {
718 struct pollfd *p = &pfdp[i];
721 /* fd has input waiting */
724 * We assume that this function is only called
725 * via someone _select()ing from svc_fdset or
726 * _poll()ing from svc_pollset[]. Thus it's safe
727 * to handle the POLLNVAL event by simply turning
728 * the corresponding bit off in svc_fdset. The
729 * svc_pollset[] array is derived from svc_fdset
730 * and so will also be updated eventually.
732 * XXX Should we do an xprt_unregister() instead?
734 if (p->revents & POLLNVAL) {
735 rwlock_wrlock(&svc_fd_lock);
736 FD_CLR(p->fd, &svc_fdset);
737 rwlock_unlock(&svc_fd_lock);
739 svc_getreq_common(p->fd);
745 rpc_control(int what, void *arg)
750 case RPC_SVC_CONNMAXREC_SET:
756 case RPC_SVC_CONNMAXREC_GET:
757 *(int *)arg = __svc_maxrec;