2 * Copyright (c) 2009, Sun Microsystems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
28 * @(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro
29 * @(#)svc.c 2.4 88/08/11 4.0 RPCSRC
30 * $NetBSD: svc.c,v 1.21 2000/07/06 03:10:35 christos Exp $
31 * $FreeBSD: src/lib/libc/rpc/svc.c,v 1.24 2006/02/27 22:10:59 deischen Exp $
32 * $DragonFly: src/lib/libc/rpc/svc.c,v 1.4 2005/11/13 12:27:04 swildner Exp $
36 * svc.c, Server-side remote procedure call interface.
38 * There are two sets of procedures here. The xprt routines are
39 * for handling transport handles. The svc routines handle the
40 * list of service routines.
42 * Copyright (C) 1984, Sun Microsystems, Inc.
45 #include "namespace.h"
46 #include "reentrant.h"
47 #include <sys/types.h>
56 #include <rpc/pmap_clnt.h>
58 #include "un-namespace.h"
63 #define RQCRED_SIZE 400 /* this size is excessive */
65 #define SVC_VERSQUIET 0x0001 /* keep quiet about vers mismatch */
66 #define version_keepquiet(xp) ((u_long)(xp)->xp_p3 & SVC_VERSQUIET)
68 #define max(a, b) (a > b ? a : b)
72 * Each entry represents a set of procedures (an rpc program).
73 * The dispatch routine takes request structs and runs the
74 * apropriate procedure.
76 static struct svc_callout {
77 struct svc_callout *sc_next;
81 void (*sc_dispatch)(struct svc_req *, SVCXPRT *);
84 SVCXPRT **__svc_xports;
87 static struct svc_callout *svc_find(rpcprog_t, rpcvers_t,
88 struct svc_callout **, char *);
89 static void __xprt_do_unregister (SVCXPRT *xprt, bool_t dolock);
91 /* *************** SVCXPRT related stuff **************** */
94 * Activate a transport handle.
97 xprt_register(SVCXPRT *xprt)
101 assert(xprt != NULL);
105 rwlock_wrlock(&svc_fd_lock);
106 if (__svc_xports == NULL) {
107 __svc_xports = (SVCXPRT **)
108 mem_alloc(FD_SETSIZE * sizeof(SVCXPRT *));
109 if (__svc_xports == NULL)
111 memset(__svc_xports, '\0', FD_SETSIZE * 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);
118 rwlock_unlock(&svc_fd_lock);
122 xprt_unregister(SVCXPRT *xprt)
124 __xprt_do_unregister(xprt, TRUE);
128 __xprt_unregister_unlocked(SVCXPRT *xprt)
130 __xprt_do_unregister(xprt, FALSE);
134 * De-activate a transport handle.
137 __xprt_do_unregister(SVCXPRT *xprt, bool_t dolock)
141 assert(xprt != NULL);
146 rwlock_wrlock(&svc_fd_lock);
147 if ((sock < FD_SETSIZE) && (__svc_xports[sock] == xprt)) {
148 __svc_xports[sock] = NULL;
149 FD_CLR(sock, &svc_fdset);
150 if (sock >= svc_maxfd) {
151 for (svc_maxfd--; svc_maxfd>=0; svc_maxfd--)
152 if (__svc_xports[svc_maxfd])
157 rwlock_unlock(&svc_fd_lock);
161 * Add a service program to the callout list.
162 * The dispatch routine will be called when a rpc request for this
163 * program number comes in.
166 svc_reg(SVCXPRT *xprt, const rpcprog_t prog, const rpcvers_t vers,
167 void (*dispatch)(struct svc_req *, SVCXPRT *),
168 const struct netconfig *nconf)
171 struct svc_callout *prev;
172 struct svc_callout *s;
173 struct netconfig *tnconf;
177 /* VARIABLES PROTECTED BY svc_lock: s, prev, svc_head */
179 if (xprt->xp_netid) {
180 netid = strdup(xprt->xp_netid);
182 } else if (nconf && nconf->nc_netid) {
183 netid = strdup(nconf->nc_netid);
185 } else if ((tnconf = __rpcgettp(xprt->xp_fd)) != NULL) {
186 netid = strdup(tnconf->nc_netid);
188 freenetconfigent(tnconf);
189 } /* must have been created with svc_raw_create */
190 if ((netid == NULL) && (flag == 1)) {
194 rwlock_wrlock(&svc_lock);
195 if ((s = svc_find(prog, vers, &prev, netid)) != NULL) {
198 if (s->sc_dispatch == dispatch)
199 goto rpcb_it; /* he is registering another xptr */
200 rwlock_unlock(&svc_lock);
203 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 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 *), int protocol)
271 struct svc_callout *prev;
272 struct svc_callout *s;
274 assert(xprt != NULL);
275 assert(dispatch != NULL);
277 if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) !=
279 if (s->sc_dispatch == dispatch)
280 goto pmap_it; /* he is registering another xptr */
283 s = mem_alloc(sizeof(struct svc_callout));
287 s->sc_prog = (rpcprog_t)prog;
288 s->sc_vers = (rpcvers_t)vers;
289 s->sc_dispatch = dispatch;
290 s->sc_next = svc_head;
293 /* now register the information with the local binder service */
295 return (pmap_set(prog, vers, protocol, xprt->xp_port));
301 * Remove a service program from the callout list.
304 svc_unregister(u_long prog, u_long vers)
306 struct svc_callout *prev;
307 struct svc_callout *s;
309 if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) ==
313 svc_head = s->sc_next;
315 prev->sc_next = s->sc_next;
318 mem_free(s, sizeof(struct svc_callout));
319 /* now unregister the information with the local binder service */
320 pmap_unset(prog, vers);
325 * Search the callout list for a program number, return the callout
328 static struct svc_callout *
329 svc_find(rpcprog_t prog, rpcvers_t vers, struct svc_callout **prev, char *netid)
331 struct svc_callout *s, *p;
333 assert(prev != NULL);
336 for (s = svc_head; s != NULL; s = s->sc_next) {
337 if (((s->sc_prog == prog) && (s->sc_vers == vers)) &&
338 ((netid == NULL) || (s->sc_netid == NULL) ||
339 (strcmp(netid, s->sc_netid) == 0)))
347 /* ******************* REPLY GENERATION ROUTINES ************ */
350 * Send a reply to an rpc request
353 svc_sendreply(SVCXPRT *xprt, xdrproc_t xdr_results, void *xdr_location)
357 assert(xprt != NULL);
359 rply.rm_direction = REPLY;
360 rply.rm_reply.rp_stat = MSG_ACCEPTED;
361 rply.acpted_rply.ar_verf = xprt->xp_verf;
362 rply.acpted_rply.ar_stat = SUCCESS;
363 rply.acpted_rply.ar_results.where = xdr_location;
364 rply.acpted_rply.ar_results.proc = xdr_results;
365 return (SVC_REPLY(xprt, &rply));
369 * No procedure error reply
372 svcerr_noproc(SVCXPRT *xprt)
376 assert(xprt != NULL);
378 rply.rm_direction = REPLY;
379 rply.rm_reply.rp_stat = MSG_ACCEPTED;
380 rply.acpted_rply.ar_verf = xprt->xp_verf;
381 rply.acpted_rply.ar_stat = PROC_UNAVAIL;
382 SVC_REPLY(xprt, &rply);
386 * Can't decode args error reply
389 svcerr_decode(SVCXPRT *xprt)
393 assert(xprt != NULL);
395 rply.rm_direction = REPLY;
396 rply.rm_reply.rp_stat = MSG_ACCEPTED;
397 rply.acpted_rply.ar_verf = xprt->xp_verf;
398 rply.acpted_rply.ar_stat = GARBAGE_ARGS;
399 SVC_REPLY(xprt, &rply);
406 svcerr_systemerr(SVCXPRT *xprt)
410 assert(xprt != NULL);
412 rply.rm_direction = REPLY;
413 rply.rm_reply.rp_stat = MSG_ACCEPTED;
414 rply.acpted_rply.ar_verf = xprt->xp_verf;
415 rply.acpted_rply.ar_stat = SYSTEM_ERR;
416 SVC_REPLY(xprt, &rply);
420 * Authentication error reply
423 svcerr_auth(SVCXPRT *xprt, enum auth_stat why)
427 assert(xprt != NULL);
429 rply.rm_direction = REPLY;
430 rply.rm_reply.rp_stat = MSG_DENIED;
431 rply.rjcted_rply.rj_stat = AUTH_ERROR;
432 rply.rjcted_rply.rj_why = why;
433 SVC_REPLY(xprt, &rply);
437 * Auth too weak error reply
440 svcerr_weakauth(SVCXPRT *xprt)
443 assert(xprt != NULL);
445 svcerr_auth(xprt, AUTH_TOOWEAK);
449 * Program unavailable error reply
452 svcerr_noprog(SVCXPRT *xprt)
456 assert(xprt != NULL);
458 rply.rm_direction = REPLY;
459 rply.rm_reply.rp_stat = MSG_ACCEPTED;
460 rply.acpted_rply.ar_verf = xprt->xp_verf;
461 rply.acpted_rply.ar_stat = PROG_UNAVAIL;
462 SVC_REPLY(xprt, &rply);
466 * Program version mismatch error reply
469 svcerr_progvers(SVCXPRT *xprt, rpcvers_t low_vers, rpcvers_t high_vers)
473 assert(xprt != NULL);
475 rply.rm_direction = REPLY;
476 rply.rm_reply.rp_stat = MSG_ACCEPTED;
477 rply.acpted_rply.ar_verf = xprt->xp_verf;
478 rply.acpted_rply.ar_stat = PROG_MISMATCH;
479 rply.acpted_rply.ar_vers.low = (u_int32_t)low_vers;
480 rply.acpted_rply.ar_vers.high = (u_int32_t)high_vers;
481 SVC_REPLY(xprt, &rply);
484 /* ******************* SERVER INPUT STUFF ******************* */
487 * Get server side input from some transport.
489 * Statement of authentication parameters management:
490 * This function owns and manages all authentication parameters, specifically
491 * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
492 * the "cooked" credentials (rqst->rq_clntcred).
493 * However, this function does not know the structure of the cooked
494 * credentials, so it make the following assumptions:
495 * a) the structure is contiguous (no pointers), and
496 * b) the cred structure size does not exceed RQCRED_SIZE bytes.
497 * In all events, all three parameters are freed upon exit from this routine.
498 * The storage is trivially management on the call stack in user land, but
499 * is mallocated in kernel land.
503 svc_getreq(int rdfds)
508 readfds.fds_bits[0] = rdfds;
509 svc_getreqset(&readfds);
513 svc_getreqset(fd_set *readfds)
516 fd_mask mask, *maskp;
519 assert(readfds != NULL);
521 maskp = readfds->fds_bits;
522 for (sock = 0; sock < FD_SETSIZE; sock += NFDBITS) {
523 for (mask = *maskp++; (bit = ffs(mask)) != 0;
524 mask ^= (1 << (bit - 1))) {
525 /* sock has input waiting */
527 svc_getreq_common(fd);
533 svc_getreq_common(int fd)
542 char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
544 msg.rm_call.cb_cred.oa_base = cred_area;
545 msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
546 r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
548 rwlock_rdlock(&svc_fd_lock);
549 xprt = __svc_xports[fd];
550 rwlock_unlock(&svc_fd_lock);
552 /* But do we control sock? */
554 /* now receive msgs from xprtprt (support batch calls) */
556 if (SVC_RECV(xprt, &msg)) {
558 /* now find the exported program and call it */
559 struct svc_callout *s;
563 r.rq_prog = msg.rm_call.cb_prog;
564 r.rq_vers = msg.rm_call.cb_vers;
565 r.rq_proc = msg.rm_call.cb_proc;
566 r.rq_cred = msg.rm_call.cb_cred;
567 /* first authenticate the message */
568 if ((why = _authenticate(&r, &msg)) != AUTH_OK) {
569 svcerr_auth(xprt, why);
572 /* now match message with a registered service*/
574 low_vers = (rpcvers_t) -1L;
575 high_vers = (rpcvers_t) 0L;
576 for (s = svc_head; s != NULL; s = s->sc_next) {
577 if (s->sc_prog == r.rq_prog) {
578 if (s->sc_vers == r.rq_vers) {
579 (*s->sc_dispatch)(&r, xprt);
581 } /* found correct version */
583 if (s->sc_vers < low_vers)
584 low_vers = s->sc_vers;
585 if (s->sc_vers > high_vers)
586 high_vers = s->sc_vers;
587 } /* found correct program */
590 * if we got here, the program or version
594 svcerr_progvers(xprt, low_vers, high_vers);
597 /* Fall through to ... */
600 * Check if the xprt has been disconnected in a
601 * recursive call in the service dispatch routine.
604 rwlock_rdlock(&svc_fd_lock);
605 if (xprt != __svc_xports[fd]) {
606 rwlock_unlock(&svc_fd_lock);
609 rwlock_unlock(&svc_fd_lock);
611 if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
615 } while (stat == XPRT_MOREREQS);
620 svc_getreq_poll(struct pollfd *pfdp, int pollretval)
625 for (i = fds_found = 0; fds_found < pollretval; i++) {
626 struct pollfd *p = &pfdp[i];
629 /* fd has input waiting */
632 * We assume that this function is only called
633 * via someone _select()ing from svc_fdset or
634 * _poll()ing from svc_pollset[]. Thus it's safe
635 * to handle the POLLNVAL event by simply turning
636 * the corresponding bit off in svc_fdset. The
637 * svc_pollset[] array is derived from svc_fdset
638 * and so will also be updated eventually.
640 * XXX Should we do an xprt_unregister() instead?
642 if (p->revents & POLLNVAL) {
643 rwlock_wrlock(&svc_fd_lock);
644 FD_CLR(p->fd, &svc_fdset);
645 rwlock_unlock(&svc_fd_lock);
647 svc_getreq_common(p->fd);
653 rpc_control(int what, void *arg)
658 case RPC_SVC_CONNMAXREC_SET:
664 case RPC_SVC_CONNMAXREC_GET:
665 *(int *)arg = __svc_maxrec;