1 /* $NetBSD: svc.c,v 1.21 2000/07/06 03:10:35 christos Exp $ */
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char *sccsid2 = "@(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro";
34 static char *sccsid = "@(#)svc.c 2.4 88/08/11 4.0 RPCSRC";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * svc.c, Server-side remote procedure call interface.
42 * There are two sets of procedures here. The xprt routines are
43 * for handling transport handles. The svc routines handle the
44 * list of service routines.
46 * Copyright (C) 1984, Sun Microsystems, Inc.
49 #include "namespace.h"
50 #include "reentrant.h"
51 #include <sys/types.h>
60 #include <rpc/pmap_clnt.h>
62 #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) ((u_long)(xp)->xp_p3 & 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 extern rwlock_t svc_lock;
88 extern rwlock_t svc_fd_lock;
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)
115 memset(__svc_xports, '\0', FD_SETSIZE * sizeof(SVCXPRT *));
117 if (sock < FD_SETSIZE) {
118 __svc_xports[sock] = xprt;
119 FD_SET(sock, &svc_fdset);
120 svc_maxfd = max(svc_maxfd, sock);
122 rwlock_unlock(&svc_fd_lock);
126 xprt_unregister(SVCXPRT *xprt)
128 __xprt_do_unregister(xprt, TRUE);
132 __xprt_unregister_unlocked(SVCXPRT *xprt)
134 __xprt_do_unregister(xprt, FALSE);
138 * De-activate a transport handle.
141 __xprt_do_unregister(xprt, 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])
163 rwlock_unlock(&svc_fd_lock);
167 * Add a service program to the callout list.
168 * The dispatch routine will be called when a rpc request for this
169 * program number comes in.
172 svc_reg(xprt, prog, vers, dispatch, nconf)
174 const rpcprog_t prog;
175 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) {
207 if (s->sc_dispatch == dispatch)
208 goto rpcb_it; /* he is registering another xptr */
209 rwlock_unlock(&svc_lock);
212 s = mem_alloc(sizeof (struct svc_callout));
216 rwlock_unlock(&svc_lock);
222 s->sc_dispatch = dispatch;
224 s->sc_next = svc_head;
227 if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
228 ((SVCXPRT *) xprt)->xp_netid = strdup(netid);
231 rwlock_unlock(&svc_lock);
232 /* now register the information with the local binder service */
234 /*LINTED const castaway*/
235 dummy = rpcb_set(prog, vers, (struct netconfig *) nconf,
236 &((SVCXPRT *) xprt)->xp_ltaddr);
243 * Remove a service program from the callout list.
246 svc_unreg(prog, vers)
247 const rpcprog_t prog;
248 const rpcvers_t vers;
250 struct svc_callout *prev;
251 struct svc_callout *s;
253 /* unregister the information anyway */
254 (void) rpcb_unset(prog, vers, NULL);
255 rwlock_wrlock(&svc_lock);
256 while ((s = svc_find(prog, vers, &prev, NULL)) != NULL) {
258 svc_head = s->sc_next;
260 prev->sc_next = s->sc_next;
264 mem_free(s->sc_netid, sizeof (s->sc_netid) + 1);
265 mem_free(s, sizeof (struct svc_callout));
267 rwlock_unlock(&svc_lock);
270 /* ********************** CALLOUT list related stuff ************* */
274 * Add a service program to the callout list.
275 * The dispatch routine will be called when a rpc request for this
276 * program number comes in.
279 svc_register(xprt, prog, vers, dispatch, protocol)
283 void (*dispatch)(struct svc_req *, SVCXPRT *);
286 struct svc_callout *prev;
287 struct svc_callout *s;
289 assert(xprt != NULL);
290 assert(dispatch != NULL);
292 if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) !=
294 if (s->sc_dispatch == dispatch)
295 goto pmap_it; /* he is registering another xptr */
298 s = mem_alloc(sizeof(struct svc_callout));
302 s->sc_prog = (rpcprog_t)prog;
303 s->sc_vers = (rpcvers_t)vers;
304 s->sc_dispatch = dispatch;
305 s->sc_next = svc_head;
308 /* now register the information with the local binder service */
310 return (pmap_set(prog, vers, protocol, xprt->xp_port));
316 * Remove a service program from the callout list.
319 svc_unregister(prog, vers)
323 struct svc_callout *prev;
324 struct svc_callout *s;
326 if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) ==
330 svc_head = s->sc_next;
332 prev->sc_next = s->sc_next;
335 mem_free(s, sizeof(struct svc_callout));
336 /* now unregister the information with the local binder service */
337 (void)pmap_unset(prog, vers);
342 * Search the callout list for a program number, return the callout
345 static struct svc_callout *
346 svc_find(prog, vers, prev, netid)
349 struct svc_callout **prev;
352 struct svc_callout *s, *p;
354 assert(prev != NULL);
357 for (s = svc_head; s != NULL; s = s->sc_next) {
358 if (((s->sc_prog == prog) && (s->sc_vers == vers)) &&
359 ((netid == NULL) || (s->sc_netid == NULL) ||
360 (strcmp(netid, s->sc_netid) == 0)))
368 /* ******************* REPLY GENERATION ROUTINES ************ */
371 * Send a reply to an rpc request
374 svc_sendreply(xprt, xdr_results, xdr_location)
376 xdrproc_t xdr_results;
381 assert(xprt != NULL);
383 rply.rm_direction = REPLY;
384 rply.rm_reply.rp_stat = MSG_ACCEPTED;
385 rply.acpted_rply.ar_verf = xprt->xp_verf;
386 rply.acpted_rply.ar_stat = SUCCESS;
387 rply.acpted_rply.ar_results.where = xdr_location;
388 rply.acpted_rply.ar_results.proc = xdr_results;
389 return (SVC_REPLY(xprt, &rply));
393 * No procedure error reply
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 = PROC_UNAVAIL;
407 SVC_REPLY(xprt, &rply);
411 * Can't decode args error reply
419 assert(xprt != NULL);
421 rply.rm_direction = REPLY;
422 rply.rm_reply.rp_stat = MSG_ACCEPTED;
423 rply.acpted_rply.ar_verf = xprt->xp_verf;
424 rply.acpted_rply.ar_stat = GARBAGE_ARGS;
425 SVC_REPLY(xprt, &rply);
432 svcerr_systemerr(xprt)
437 assert(xprt != NULL);
439 rply.rm_direction = REPLY;
440 rply.rm_reply.rp_stat = MSG_ACCEPTED;
441 rply.acpted_rply.ar_verf = xprt->xp_verf;
442 rply.acpted_rply.ar_stat = SYSTEM_ERR;
443 SVC_REPLY(xprt, &rply);
448 * Tell RPC package to not complain about version errors to the client. This
449 * is useful when revving broadcast protocols that sit on a fixed address.
450 * There is really one (or should be only one) example of this kind of
451 * protocol: the portmapper (or rpc binder).
454 __svc_versquiet_on(xprt)
459 tmp = ((u_long) xprt->xp_p3) | SVC_VERSQUIET;
464 __svc_versquiet_off(xprt)
469 tmp = ((u_long) xprt->xp_p3) & ~SVC_VERSQUIET;
477 __svc_versquiet_on(xprt);
481 __svc_versquiet_get(xprt)
484 return ((int) xprt->xp_p3) & SVC_VERSQUIET;
489 * Authentication error reply
492 svcerr_auth(xprt, why)
498 assert(xprt != NULL);
500 rply.rm_direction = REPLY;
501 rply.rm_reply.rp_stat = MSG_DENIED;
502 rply.rjcted_rply.rj_stat = AUTH_ERROR;
503 rply.rjcted_rply.rj_why = why;
504 SVC_REPLY(xprt, &rply);
508 * Auth too weak error reply
511 svcerr_weakauth(xprt)
515 assert(xprt != NULL);
517 svcerr_auth(xprt, AUTH_TOOWEAK);
521 * Program unavailable error reply
529 assert(xprt != NULL);
531 rply.rm_direction = REPLY;
532 rply.rm_reply.rp_stat = MSG_ACCEPTED;
533 rply.acpted_rply.ar_verf = xprt->xp_verf;
534 rply.acpted_rply.ar_stat = PROG_UNAVAIL;
535 SVC_REPLY(xprt, &rply);
539 * Program version mismatch error reply
542 svcerr_progvers(xprt, low_vers, high_vers)
549 assert(xprt != NULL);
551 rply.rm_direction = REPLY;
552 rply.rm_reply.rp_stat = MSG_ACCEPTED;
553 rply.acpted_rply.ar_verf = xprt->xp_verf;
554 rply.acpted_rply.ar_stat = PROG_MISMATCH;
555 rply.acpted_rply.ar_vers.low = (u_int32_t)low_vers;
556 rply.acpted_rply.ar_vers.high = (u_int32_t)high_vers;
557 SVC_REPLY(xprt, &rply);
560 /* ******************* SERVER INPUT STUFF ******************* */
563 * Get server side input from some transport.
565 * Statement of authentication parameters management:
566 * This function owns and manages all authentication parameters, specifically
567 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
568 * the "cooked" credentials (rqst->rq_clntcred).
569 * However, this function does not know the structure of the cooked
570 * credentials, so it make the following assumptions:
571 * a) the structure is contiguous (no pointers), and
572 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
573 * In all events, all three parameters are freed upon exit from this routine.
574 * The storage is trivially management on the call stack in user land, but
575 * is mallocated in kernel land.
585 readfds.fds_bits[0] = rdfds;
586 svc_getreqset(&readfds);
590 svc_getreqset(readfds)
594 fd_mask mask, *maskp;
597 assert(readfds != NULL);
599 maskp = readfds->fds_bits;
600 for (sock = 0; sock < FD_SETSIZE; sock += NFDBITS) {
601 for (mask = *maskp++; (bit = ffs(mask)) != 0;
602 mask ^= (1 << (bit - 1))) {
603 /* sock has input waiting */
605 svc_getreq_common(fd);
611 svc_getreq_common(fd)
621 char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
623 msg.rm_call.cb_cred.oa_base = cred_area;
624 msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
625 r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
627 rwlock_rdlock(&svc_fd_lock);
628 xprt = __svc_xports[fd];
629 rwlock_unlock(&svc_fd_lock);
631 /* But do we control sock? */
633 /* now receive msgs from xprtprt (support batch calls) */
635 if (SVC_RECV(xprt, &msg)) {
637 /* now find the exported program and call it */
638 struct svc_callout *s;
642 r.rq_prog = msg.rm_call.cb_prog;
643 r.rq_vers = msg.rm_call.cb_vers;
644 r.rq_proc = msg.rm_call.cb_proc;
645 r.rq_cred = msg.rm_call.cb_cred;
646 /* first authenticate the message */
647 if ((why = _authenticate(&r, &msg)) != AUTH_OK) {
648 svcerr_auth(xprt, why);
651 /* now match message with a registered service*/
653 low_vers = (rpcvers_t) -1L;
654 high_vers = (rpcvers_t) 0L;
655 for (s = svc_head; s != NULL; s = s->sc_next) {
656 if (s->sc_prog == r.rq_prog) {
657 if (s->sc_vers == r.rq_vers) {
658 (*s->sc_dispatch)(&r, xprt);
660 } /* found correct version */
662 if (s->sc_vers < low_vers)
663 low_vers = s->sc_vers;
664 if (s->sc_vers > high_vers)
665 high_vers = s->sc_vers;
666 } /* found correct program */
669 * if we got here, the program or version
673 svcerr_progvers(xprt, low_vers, high_vers);
676 /* Fall through to ... */
679 * Check if the xprt has been disconnected in a
680 * recursive call in the service dispatch routine.
683 rwlock_rdlock(&svc_fd_lock);
684 if (xprt != __svc_xports[fd]) {
685 rwlock_unlock(&svc_fd_lock);
688 rwlock_unlock(&svc_fd_lock);
690 if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
694 } while (stat == XPRT_MOREREQS);
699 svc_getreq_poll(pfdp, pollretval)
706 for (i = fds_found = 0; fds_found < pollretval; i++) {
707 struct pollfd *p = &pfdp[i];
710 /* fd has input waiting */
713 * We assume that this function is only called
714 * via someone _select()ing from svc_fdset or
715 * _poll()ing from svc_pollset[]. Thus it's safe
716 * to handle the POLLNVAL event by simply turning
717 * the corresponding bit off in svc_fdset. The
718 * svc_pollset[] array is derived from svc_fdset
719 * and so will also be updated eventually.
721 * XXX Should we do an xprt_unregister() instead?
723 if (p->revents & POLLNVAL) {
724 rwlock_wrlock(&svc_fd_lock);
725 FD_CLR(p->fd, &svc_fdset);
726 rwlock_unlock(&svc_fd_lock);
728 svc_getreq_common(p->fd);
734 rpc_control(int what, void *arg)
739 case RPC_SVC_CONNMAXREC_SET:
745 case RPC_SVC_CONNMAXREC_GET:
746 *(int *)arg = __svc_maxrec;