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.
34 * svc.c, Server-side remote procedure call interface.
36 * There are two sets of procedures here. The xprt routines are
37 * for handling transport handles. The svc routines handle the
38 * list of service routines.
40 * Copyright (C) 1984, Sun Microsystems, Inc.
43 #include "namespace.h"
44 #include "reentrant.h"
45 #include <sys/types.h>
54 #include <rpc/pmap_clnt.h>
56 #include "un-namespace.h"
61 #define RQCRED_SIZE 400 /* this size is excessive */
63 #define SVC_VERSQUIET 0x0001 /* keep quiet about vers mismatch */
64 #define version_keepquiet(xp) (SVC_EXT(xp)->xp_flags & SVC_VERSQUIET)
66 #define max(a, b) (a > b ? a : b)
70 * Each entry represents a set of procedures (an rpc program).
71 * The dispatch routine takes request structs and runs the
72 * appropriate procedure.
74 static struct svc_callout {
75 struct svc_callout *sc_next;
79 void (*sc_dispatch)(struct svc_req *, SVCXPRT *);
82 SVCXPRT **__svc_xports;
85 static struct svc_callout *svc_find(rpcprog_t, rpcvers_t,
86 struct svc_callout **, char *);
87 static void __xprt_do_unregister (SVCXPRT *xprt, bool_t dolock);
89 /* *************** SVCXPRT related stuff **************** */
92 * Activate a transport handle.
95 xprt_register(SVCXPRT *xprt)
103 rwlock_wrlock(&svc_fd_lock);
104 if (__svc_xports == NULL) {
105 __svc_xports = (SVCXPRT **)
106 mem_alloc((FD_SETSIZE + 1) * sizeof(SVCXPRT *));
107 if (__svc_xports == NULL) {
108 rwlock_unlock(&svc_fd_lock);
111 memset(__svc_xports, '\0', (FD_SETSIZE + 1) * sizeof(SVCXPRT *));
113 if (sock < FD_SETSIZE) {
114 __svc_xports[sock] = xprt;
115 FD_SET(sock, &svc_fdset);
116 svc_maxfd = max(svc_maxfd, sock);
117 } else if (sock == FD_SETSIZE)
118 __svc_xports[sock] = xprt;
119 rwlock_unlock(&svc_fd_lock);
123 xprt_unregister(SVCXPRT *xprt)
125 __xprt_do_unregister(xprt, TRUE);
129 __xprt_unregister_unlocked(SVCXPRT *xprt)
131 __xprt_do_unregister(xprt, FALSE);
135 * De-activate a transport handle.
138 __xprt_do_unregister(SVCXPRT *xprt, bool_t dolock)
142 assert(xprt != NULL);
147 rwlock_wrlock(&svc_fd_lock);
148 if ((sock < FD_SETSIZE) && (__svc_xports[sock] == xprt)) {
149 __svc_xports[sock] = NULL;
150 FD_CLR(sock, &svc_fdset);
151 if (sock >= svc_maxfd) {
152 for (svc_maxfd--; svc_maxfd>=0; svc_maxfd--)
153 if (__svc_xports[svc_maxfd])
156 } else if ((sock == FD_SETSIZE) && (__svc_xports[sock] == xprt))
157 __svc_xports[sock] = NULL;
159 rwlock_unlock(&svc_fd_lock);
163 * Add a service program to the callout list.
164 * The dispatch routine will be called when a rpc request for this
165 * program number comes in.
168 svc_reg(SVCXPRT *xprt, const rpcprog_t prog, const rpcvers_t vers,
169 void (*dispatch)(struct svc_req *, SVCXPRT *),
170 const struct netconfig *nconf)
173 struct svc_callout *prev;
174 struct svc_callout *s;
175 struct netconfig *tnconf;
179 /* VARIABLES PROTECTED BY svc_lock: s, prev, svc_head */
181 if (xprt->xp_netid) {
182 netid = strdup(xprt->xp_netid);
184 } else if (nconf && nconf->nc_netid) {
185 netid = strdup(nconf->nc_netid);
187 } else if ((tnconf = __rpcgettp(xprt->xp_fd)) != NULL) {
188 netid = strdup(tnconf->nc_netid);
190 freenetconfigent(tnconf);
191 } /* must have been created with svc_raw_create */
192 if ((netid == NULL) && (flag == 1)) {
196 rwlock_wrlock(&svc_lock);
197 if ((s = svc_find(prog, vers, &prev, netid)) != NULL) {
199 if (s->sc_dispatch == dispatch)
200 goto rpcb_it; /* he is registering another xptr */
201 rwlock_unlock(&svc_lock);
204 s = mem_alloc(sizeof (struct svc_callout));
207 rwlock_unlock(&svc_lock);
213 s->sc_dispatch = dispatch;
215 s->sc_next = svc_head;
218 if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
219 ((SVCXPRT *) xprt)->xp_netid = strdup(netid);
222 rwlock_unlock(&svc_lock);
223 /* now register the information with the local binder service */
225 /*LINTED const castaway*/
226 dummy = rpcb_set(prog, vers, (struct netconfig *) nconf,
227 &((SVCXPRT *) xprt)->xp_ltaddr);
234 * Remove a service program from the callout list.
237 svc_unreg(const rpcprog_t prog, const rpcvers_t vers)
239 struct svc_callout *prev;
240 struct svc_callout *s;
242 /* unregister the information anyway */
243 (void) rpcb_unset(prog, vers, NULL);
244 rwlock_wrlock(&svc_lock);
245 while ((s = svc_find(prog, vers, &prev, NULL)) != NULL) {
247 svc_head = s->sc_next;
249 prev->sc_next = s->sc_next;
253 mem_free(s->sc_netid, sizeof (s->sc_netid) + 1);
254 mem_free(s, sizeof (struct svc_callout));
256 rwlock_unlock(&svc_lock);
259 /* ********************** CALLOUT list related stuff ************* */
263 * Add a service program to the callout list.
264 * The dispatch routine will be called when a rpc request for this
265 * program number comes in.
268 svc_register(SVCXPRT *xprt, u_long prog, u_long vers,
269 void (*dispatch)(struct svc_req *, SVCXPRT *),
272 struct svc_callout *prev;
273 struct svc_callout *s;
275 assert(xprt != NULL);
276 assert(dispatch != NULL);
278 if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) !=
280 if (s->sc_dispatch == dispatch)
281 goto pmap_it; /* he is registering another xptr */
284 s = mem_alloc(sizeof(struct svc_callout));
288 s->sc_prog = (rpcprog_t)prog;
289 s->sc_vers = (rpcvers_t)vers;
290 s->sc_dispatch = dispatch;
291 s->sc_next = svc_head;
294 /* now register the information with the local binder service */
296 return (pmap_set(prog, vers, protocol, xprt->xp_port));
302 * Remove a service program from the callout list.
305 svc_unregister(u_long prog, u_long vers)
307 struct svc_callout *prev;
308 struct svc_callout *s;
310 if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) ==
314 svc_head = s->sc_next;
316 prev->sc_next = s->sc_next;
319 mem_free(s, sizeof(struct svc_callout));
320 /* now unregister the information with the local binder service */
321 (void)pmap_unset(prog, vers);
326 * Search the callout list for a program number, return the callout
329 static struct svc_callout *
330 svc_find(rpcprog_t prog, rpcvers_t vers, struct svc_callout **prev,
333 struct svc_callout *s, *p;
335 assert(prev != NULL);
338 for (s = svc_head; s != NULL; s = s->sc_next) {
339 if (((s->sc_prog == prog) && (s->sc_vers == vers)) &&
340 ((netid == NULL) || (s->sc_netid == NULL) ||
341 (strcmp(netid, s->sc_netid) == 0)))
349 /* ******************* REPLY GENERATION ROUTINES ************ */
352 * Send a reply to an rpc request
355 svc_sendreply(SVCXPRT *xprt, xdrproc_t xdr_results,
360 assert(xprt != NULL);
362 rply.rm_direction = REPLY;
363 rply.rm_reply.rp_stat = MSG_ACCEPTED;
364 rply.acpted_rply.ar_verf = xprt->xp_verf;
365 rply.acpted_rply.ar_stat = SUCCESS;
366 rply.acpted_rply.ar_results.where = xdr_location;
367 rply.acpted_rply.ar_results.proc = xdr_results;
368 return (SVC_REPLY(xprt, &rply));
372 * No procedure error reply
375 svcerr_noproc(SVCXPRT *xprt)
379 assert(xprt != NULL);
381 rply.rm_direction = REPLY;
382 rply.rm_reply.rp_stat = MSG_ACCEPTED;
383 rply.acpted_rply.ar_verf = xprt->xp_verf;
384 rply.acpted_rply.ar_stat = PROC_UNAVAIL;
385 SVC_REPLY(xprt, &rply);
389 * Can't decode args error reply
392 svcerr_decode(SVCXPRT *xprt)
396 assert(xprt != NULL);
398 rply.rm_direction = REPLY;
399 rply.rm_reply.rp_stat = MSG_ACCEPTED;
400 rply.acpted_rply.ar_verf = xprt->xp_verf;
401 rply.acpted_rply.ar_stat = GARBAGE_ARGS;
402 SVC_REPLY(xprt, &rply);
409 svcerr_systemerr(SVCXPRT *xprt)
413 assert(xprt != NULL);
415 rply.rm_direction = REPLY;
416 rply.rm_reply.rp_stat = MSG_ACCEPTED;
417 rply.acpted_rply.ar_verf = xprt->xp_verf;
418 rply.acpted_rply.ar_stat = SYSTEM_ERR;
419 SVC_REPLY(xprt, &rply);
424 * Tell RPC package to not complain about version errors to the client. This
425 * is useful when revving broadcast protocols that sit on a fixed address.
426 * There is really one (or should be only one) example of this kind of
427 * protocol: the portmapper (or rpc binder).
430 __svc_versquiet_on(SVCXPRT *xprt)
433 SVC_EXT(xprt)->xp_flags |= SVC_VERSQUIET;
437 __svc_versquiet_off(SVCXPRT *xprt)
440 SVC_EXT(xprt)->xp_flags &= ~SVC_VERSQUIET;
444 svc_versquiet(SVCXPRT *xprt)
446 __svc_versquiet_on(xprt);
450 __svc_versquiet_get(SVCXPRT *xprt)
453 return (SVC_EXT(xprt)->xp_flags & SVC_VERSQUIET);
458 * Authentication error reply
461 svcerr_auth(SVCXPRT *xprt, enum auth_stat why)
465 assert(xprt != NULL);
467 rply.rm_direction = REPLY;
468 rply.rm_reply.rp_stat = MSG_DENIED;
469 rply.rjcted_rply.rj_stat = AUTH_ERROR;
470 rply.rjcted_rply.rj_why = why;
471 SVC_REPLY(xprt, &rply);
475 * Auth too weak error reply
478 svcerr_weakauth(SVCXPRT *xprt)
481 assert(xprt != NULL);
483 svcerr_auth(xprt, AUTH_TOOWEAK);
487 * Program unavailable error reply
490 svcerr_noprog(SVCXPRT *xprt)
494 assert(xprt != NULL);
496 rply.rm_direction = REPLY;
497 rply.rm_reply.rp_stat = MSG_ACCEPTED;
498 rply.acpted_rply.ar_verf = xprt->xp_verf;
499 rply.acpted_rply.ar_stat = PROG_UNAVAIL;
500 SVC_REPLY(xprt, &rply);
504 * Program version mismatch error reply
507 svcerr_progvers(SVCXPRT *xprt, rpcvers_t low_vers, rpcvers_t high_vers)
511 assert(xprt != NULL);
513 rply.rm_direction = REPLY;
514 rply.rm_reply.rp_stat = MSG_ACCEPTED;
515 rply.acpted_rply.ar_verf = xprt->xp_verf;
516 rply.acpted_rply.ar_stat = PROG_MISMATCH;
517 rply.acpted_rply.ar_vers.low = (u_int32_t)low_vers;
518 rply.acpted_rply.ar_vers.high = (u_int32_t)high_vers;
519 SVC_REPLY(xprt, &rply);
523 * Allocate a new server transport structure. All fields are
524 * initialized to zero and xp_p3 is initialized to point at an
525 * extension structure to hold various flags and authentication
534 xprt = mem_alloc(sizeof(SVCXPRT));
537 memset(xprt, 0, sizeof(SVCXPRT));
538 ext = mem_alloc(sizeof(SVCXPRT_EXT));
540 mem_free(xprt, sizeof(SVCXPRT));
543 memset(ext, 0, sizeof(SVCXPRT_EXT));
545 ext->xp_auth.svc_ah_ops = &svc_auth_null_ops;
551 * Free a server transport structure.
554 svc_xprt_free(SVCXPRT *xprt)
557 mem_free(xprt->xp_p3, sizeof(SVCXPRT_EXT));
558 mem_free(xprt, sizeof(SVCXPRT));
561 /* ******************* SERVER INPUT STUFF ******************* */
564 * Get server side input from some transport.
566 * Statement of authentication parameters management:
567 * This function owns and manages all authentication parameters, specifically
568 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
569 * the "cooked" credentials (rqst->rq_clntcred).
570 * However, this function does not know the structure of the cooked
571 * credentials, so it make the following assumptions:
572 * a) the structure is contiguous (no pointers), and
573 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
574 * In all events, all three parameters are freed upon exit from this routine.
575 * The storage is trivially management on the call stack in user land, but
576 * is mallocated in kernel land.
580 svc_getreq(int rdfds)
585 readfds.fds_bits[0] = rdfds;
586 svc_getreqset(&readfds);
590 svc_getreqset(fd_set *readfds)
593 fd_mask mask, *maskp;
596 assert(readfds != NULL);
598 maskp = readfds->fds_bits;
599 for (sock = 0; sock < FD_SETSIZE; sock += NFDBITS) {
600 for (mask = *maskp++; (bit = ffsl(mask)) != 0;
601 mask ^= (1ul << (bit - 1))) {
602 /* sock has input waiting */
604 svc_getreq_common(fd);
610 svc_getreq_common(int fd)
619 char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
621 msg.rm_call.cb_cred.oa_base = cred_area;
622 msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
623 r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
625 rwlock_rdlock(&svc_fd_lock);
626 xprt = __svc_xports[fd];
627 rwlock_unlock(&svc_fd_lock);
629 /* But do we control sock? */
631 /* now receive msgs from xprtprt (support batch calls) */
633 if (SVC_RECV(xprt, &msg)) {
635 /* now find the exported program and call it */
636 struct svc_callout *s;
640 r.rq_prog = msg.rm_call.cb_prog;
641 r.rq_vers = msg.rm_call.cb_vers;
642 r.rq_proc = msg.rm_call.cb_proc;
643 r.rq_cred = msg.rm_call.cb_cred;
644 /* first authenticate the message */
645 if ((why = _authenticate(&r, &msg)) != AUTH_OK) {
647 * RPCSEC_GSS uses this return code
648 * for requests that form part of its
649 * context establishment protocol and
650 * should not be dispatched to the
653 if (why != RPCSEC_GSS_NODISPATCH)
654 svcerr_auth(xprt, why);
657 /* now match message with a registered service*/
659 low_vers = (rpcvers_t) -1L;
660 high_vers = (rpcvers_t) 0L;
661 for (s = svc_head; s != NULL; s = s->sc_next) {
662 if (s->sc_prog == r.rq_prog) {
663 if (s->sc_vers == r.rq_vers) {
664 (*s->sc_dispatch)(&r, xprt);
666 } /* found correct version */
668 if (s->sc_vers < low_vers)
669 low_vers = s->sc_vers;
670 if (s->sc_vers > high_vers)
671 high_vers = s->sc_vers;
672 } /* found correct program */
675 * if we got here, the program or version
679 svcerr_progvers(xprt, low_vers, high_vers);
682 /* Fall through to ... */
685 * Check if the xprt has been disconnected in a
686 * recursive call in the service dispatch routine.
689 rwlock_rdlock(&svc_fd_lock);
690 if (xprt != __svc_xports[fd]) {
691 rwlock_unlock(&svc_fd_lock);
694 rwlock_unlock(&svc_fd_lock);
696 if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
700 } while (stat == XPRT_MOREREQS);
705 svc_getreq_poll(struct pollfd *pfdp, int pollretval)
710 for (i = fds_found = 0; fds_found < pollretval; i++) {
711 struct pollfd *p = &pfdp[i];
714 /* fd has input waiting */
717 * We assume that this function is only called
718 * via someone _select()ing from svc_fdset or
719 * _poll()ing from svc_pollset[]. Thus it's safe
720 * to handle the POLLNVAL event by simply turning
721 * the corresponding bit off in svc_fdset. The
722 * svc_pollset[] array is derived from svc_fdset
723 * and so will also be updated eventually.
725 * XXX Should we do an xprt_unregister() instead?
727 if (p->revents & POLLNVAL) {
728 rwlock_wrlock(&svc_fd_lock);
729 FD_CLR(p->fd, &svc_fdset);
730 rwlock_unlock(&svc_fd_lock);
732 svc_getreq_common(p->fd);
738 rpc_control(int what, void *arg)
743 case RPC_SVC_CONNMAXREC_SET:
749 case RPC_SVC_CONNMAXREC_GET:
750 *(int *)arg = __svc_maxrec;