Home | History | Annotate | Line # | Download | only in rpc
svc.c revision 1.30.8.1
      1  1.30.8.1       riz /*	$NetBSD: svc.c,v 1.30.8.1 2013/03/14 22:03:12 riz Exp $	*/
      2       1.7       cgd 
      3       1.1       cgd /*
      4  1.30.8.1       riz  * Copyright (c) 2010, Oracle America, Inc.
      5  1.30.8.1       riz  *
      6  1.30.8.1       riz  * Redistribution and use in source and binary forms, with or without
      7  1.30.8.1       riz  * modification, are permitted provided that the following conditions are
      8  1.30.8.1       riz  * met:
      9  1.30.8.1       riz  *
     10  1.30.8.1       riz  *     * Redistributions of source code must retain the above copyright
     11  1.30.8.1       riz  *       notice, this list of conditions and the following disclaimer.
     12  1.30.8.1       riz  *     * Redistributions in binary form must reproduce the above
     13  1.30.8.1       riz  *       copyright notice, this list of conditions and the following
     14  1.30.8.1       riz  *       disclaimer in the documentation and/or other materials
     15  1.30.8.1       riz  *       provided with the distribution.
     16  1.30.8.1       riz  *     * Neither the name of the "Oracle America, Inc." nor the names of its
     17  1.30.8.1       riz  *       contributors may be used to endorse or promote products derived
     18  1.30.8.1       riz  *       from this software without specific prior written permission.
     19  1.30.8.1       riz  *
     20  1.30.8.1       riz  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  1.30.8.1       riz  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22  1.30.8.1       riz  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     23  1.30.8.1       riz  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     24  1.30.8.1       riz  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     25  1.30.8.1       riz  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.30.8.1       riz  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     27  1.30.8.1       riz  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  1.30.8.1       riz  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  1.30.8.1       riz  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     30  1.30.8.1       riz  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     31  1.30.8.1       riz  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32       1.1       cgd  */
     33       1.1       cgd 
     34      1.10  christos #include <sys/cdefs.h>
     35       1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     36      1.10  christos #if 0
     37      1.10  christos static char *sccsid = "@(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro";
     38      1.10  christos static char *sccsid = "@(#)svc.c	2.4 88/08/11 4.0 RPCSRC";
     39      1.10  christos #else
     40  1.30.8.1       riz __RCSID("$NetBSD: svc.c,v 1.30.8.1 2013/03/14 22:03:12 riz Exp $");
     41      1.10  christos #endif
     42       1.1       cgd #endif
     43       1.1       cgd 
     44       1.1       cgd /*
     45       1.1       cgd  * svc.c, Server-side remote procedure call interface.
     46       1.1       cgd  *
     47       1.1       cgd  * There are two sets of procedures here.  The xprt routines are
     48       1.1       cgd  * for handling transport handles.  The svc routines handle the
     49       1.1       cgd  * list of service routines.
     50       1.1       cgd  *
     51       1.1       cgd  * Copyright (C) 1984, Sun Microsystems, Inc.
     52       1.1       cgd  */
     53       1.1       cgd 
     54      1.11       jtc #include "namespace.h"
     55      1.20      fvdl #include "reentrant.h"
     56      1.20      fvdl #include <sys/types.h>
     57      1.20      fvdl #include <sys/poll.h>
     58      1.17     lukem #include <assert.h>
     59      1.14     lukem #include <errno.h>
     60       1.4       cgd #include <stdlib.h>
     61       1.8        pk #include <string.h>
     62      1.28  christos #include <err.h>
     63       1.4       cgd 
     64       1.1       cgd #include <rpc/rpc.h>
     65      1.20      fvdl #ifdef PORTMAP
     66       1.1       cgd #include <rpc/pmap_clnt.h>
     67      1.20      fvdl #endif
     68      1.20      fvdl 
     69      1.23      fvdl #include "rpc_internal.h"
     70      1.11       jtc 
     71      1.11       jtc #ifdef __weak_alias
     72      1.19   mycroft __weak_alias(svc_getreq,_svc_getreq)
     73      1.19   mycroft __weak_alias(svc_getreqset,_svc_getreqset)
     74      1.20      fvdl __weak_alias(svc_getreq_common,_svc_getreq_common)
     75      1.19   mycroft __weak_alias(svc_register,_svc_register)
     76      1.20      fvdl __weak_alias(svc_reg,_svc_reg)
     77      1.20      fvdl __weak_alias(svc_unreg,_svc_unreg)
     78      1.19   mycroft __weak_alias(svc_sendreply,_svc_sendreply)
     79      1.19   mycroft __weak_alias(svc_unregister,_svc_unregister)
     80      1.19   mycroft __weak_alias(svcerr_auth,_svcerr_auth)
     81      1.19   mycroft __weak_alias(svcerr_decode,_svcerr_decode)
     82      1.19   mycroft __weak_alias(svcerr_noproc,_svcerr_noproc)
     83      1.19   mycroft __weak_alias(svcerr_noprog,_svcerr_noprog)
     84      1.19   mycroft __weak_alias(svcerr_progvers,_svcerr_progvers)
     85      1.19   mycroft __weak_alias(svcerr_systemerr,_svcerr_systemerr)
     86      1.19   mycroft __weak_alias(svcerr_weakauth,_svcerr_weakauth)
     87      1.19   mycroft __weak_alias(xprt_register,_xprt_register)
     88      1.19   mycroft __weak_alias(xprt_unregister,_xprt_unregister)
     89      1.23      fvdl __weak_alias(rpc_control,_rpc_control)
     90      1.11       jtc #endif
     91       1.1       cgd 
     92      1.23      fvdl SVCXPRT **__svc_xports;
     93      1.23      fvdl int __svc_maxrec;
     94       1.1       cgd 
     95       1.1       cgd #define	RQCRED_SIZE	400		/* this size is excessive */
     96       1.1       cgd 
     97      1.20      fvdl #define SVC_VERSQUIET 0x0001		/* keep quiet about vers mismatch */
     98      1.20      fvdl #define version_keepquiet(xp) ((u_long)(xp)->xp_p3 & SVC_VERSQUIET)
     99      1.20      fvdl 
    100       1.2   deraadt #define max(a, b) (a > b ? a : b)
    101       1.2   deraadt 
    102       1.1       cgd /*
    103       1.1       cgd  * The services list
    104       1.1       cgd  * Each entry represents a set of procedures (an rpc program).
    105       1.1       cgd  * The dispatch routine takes request structs and runs the
    106       1.1       cgd  * apropriate procedure.
    107       1.1       cgd  */
    108       1.1       cgd static struct svc_callout {
    109       1.1       cgd 	struct svc_callout *sc_next;
    110      1.20      fvdl 	rpcprog_t	    sc_prog;
    111      1.20      fvdl 	rpcvers_t	    sc_vers;
    112      1.20      fvdl 	char		   *sc_netid;
    113      1.10  christos 	void		    (*sc_dispatch) __P((struct svc_req *, SVCXPRT *));
    114       1.1       cgd } *svc_head;
    115       1.1       cgd 
    116      1.24   thorpej #ifdef _REENTRANT
    117      1.20      fvdl extern rwlock_t svc_lock;
    118      1.20      fvdl extern rwlock_t svc_fd_lock;
    119      1.20      fvdl #endif
    120      1.20      fvdl 
    121      1.20      fvdl static struct svc_callout *svc_find __P((rpcprog_t, rpcvers_t,
    122      1.20      fvdl 					 struct svc_callout **, char *));
    123      1.23      fvdl static void __xprt_do_unregister __P((SVCXPRT *xprt, bool_t dolock));
    124       1.1       cgd 
    125       1.1       cgd /* ***************  SVCXPRT related stuff **************** */
    126       1.3   deraadt 
    127       1.1       cgd /*
    128       1.1       cgd  * Activate a transport handle.
    129       1.1       cgd  */
    130       1.1       cgd void
    131       1.1       cgd xprt_register(xprt)
    132       1.1       cgd 	SVCXPRT *xprt;
    133       1.1       cgd {
    134      1.17     lukem 	int sock;
    135      1.17     lukem 
    136      1.17     lukem 	_DIAGASSERT(xprt != NULL);
    137      1.17     lukem 
    138      1.20      fvdl 	sock = xprt->xp_fd;
    139       1.1       cgd 
    140      1.20      fvdl 	rwlock_wrlock(&svc_fd_lock);
    141      1.23      fvdl 	if (__svc_xports == NULL) {
    142      1.28  christos 		__svc_xports = mem_alloc(FD_SETSIZE * sizeof(SVCXPRT *));
    143      1.28  christos 		if (__svc_xports == NULL) {
    144      1.28  christos 			warn("xprt_register");
    145      1.28  christos 			goto out;
    146      1.28  christos 		}
    147      1.23      fvdl 		memset(__svc_xports, '\0', FD_SETSIZE * sizeof(SVCXPRT *));
    148       1.1       cgd 	}
    149       1.2   deraadt 	if (sock < FD_SETSIZE) {
    150      1.23      fvdl 		__svc_xports[sock] = xprt;
    151       1.1       cgd 		FD_SET(sock, &svc_fdset);
    152       1.2   deraadt 		svc_maxfd = max(svc_maxfd, sock);
    153       1.1       cgd 	}
    154      1.28  christos out:
    155      1.20      fvdl 	rwlock_unlock(&svc_fd_lock);
    156       1.1       cgd }
    157       1.1       cgd 
    158      1.23      fvdl void
    159      1.23      fvdl xprt_unregister(SVCXPRT *xprt)
    160      1.23      fvdl {
    161      1.23      fvdl 	__xprt_do_unregister(xprt, TRUE);
    162      1.23      fvdl }
    163      1.23      fvdl 
    164      1.23      fvdl void
    165      1.23      fvdl __xprt_unregister_unlocked(SVCXPRT *xprt)
    166      1.23      fvdl {
    167      1.23      fvdl 	__xprt_do_unregister(xprt, FALSE);
    168      1.23      fvdl }
    169      1.23      fvdl 
    170       1.1       cgd /*
    171       1.1       cgd  * De-activate a transport handle.
    172       1.1       cgd  */
    173      1.23      fvdl static void
    174      1.23      fvdl __xprt_do_unregister(xprt, dolock)
    175       1.1       cgd 	SVCXPRT *xprt;
    176      1.23      fvdl 	bool_t dolock;
    177       1.1       cgd {
    178      1.17     lukem 	int sock;
    179      1.17     lukem 
    180      1.17     lukem 	_DIAGASSERT(xprt != NULL);
    181      1.17     lukem 
    182      1.20      fvdl 	sock = xprt->xp_fd;
    183       1.1       cgd 
    184      1.23      fvdl 	if (dolock)
    185      1.23      fvdl 		rwlock_wrlock(&svc_fd_lock);
    186      1.23      fvdl 	if ((sock < FD_SETSIZE) && (__svc_xports[sock] == xprt)) {
    187      1.23      fvdl 		__svc_xports[sock] = NULL;
    188       1.1       cgd 		FD_CLR(sock, &svc_fdset);
    189      1.20      fvdl 		if (sock >= svc_maxfd) {
    190       1.3   deraadt 			for (svc_maxfd--; svc_maxfd>=0; svc_maxfd--)
    191      1.23      fvdl 				if (__svc_xports[svc_maxfd])
    192       1.3   deraadt 					break;
    193       1.3   deraadt 		}
    194       1.1       cgd 	}
    195      1.23      fvdl 	if (dolock)
    196      1.23      fvdl 		rwlock_unlock(&svc_fd_lock);
    197       1.1       cgd }
    198       1.1       cgd 
    199      1.20      fvdl /*
    200      1.20      fvdl  * Add a service program to the callout list.
    201      1.20      fvdl  * The dispatch routine will be called when a rpc request for this
    202      1.20      fvdl  * program number comes in.
    203      1.20      fvdl  */
    204      1.20      fvdl bool_t
    205      1.20      fvdl svc_reg(xprt, prog, vers, dispatch, nconf)
    206      1.20      fvdl 	SVCXPRT *xprt;
    207      1.20      fvdl 	const rpcprog_t prog;
    208      1.20      fvdl 	const rpcvers_t vers;
    209      1.20      fvdl 	void (*dispatch) __P((struct svc_req *, SVCXPRT *));
    210      1.20      fvdl 	const struct netconfig *nconf;
    211      1.20      fvdl {
    212      1.20      fvdl 	bool_t dummy;
    213      1.20      fvdl 	struct svc_callout *prev;
    214      1.21  christos 	struct svc_callout *s;
    215      1.20      fvdl 	struct netconfig *tnconf;
    216      1.21  christos 	char *netid = NULL;
    217      1.20      fvdl 	int flag = 0;
    218      1.20      fvdl 
    219      1.22     lukem 	_DIAGASSERT(xprt != NULL);
    220      1.22     lukem 	/* XXX: dispatch may be NULL ??? */
    221      1.22     lukem 
    222      1.20      fvdl /* VARIABLES PROTECTED BY svc_lock: s, prev, svc_head */
    223      1.20      fvdl 
    224      1.20      fvdl 	if (xprt->xp_netid) {
    225      1.20      fvdl 		netid = strdup(xprt->xp_netid);
    226      1.20      fvdl 		flag = 1;
    227      1.20      fvdl 	} else if (nconf && nconf->nc_netid) {
    228      1.20      fvdl 		netid = strdup(nconf->nc_netid);
    229      1.20      fvdl 		flag = 1;
    230      1.20      fvdl 	} else if ((tnconf = __rpcgettp(xprt->xp_fd)) != NULL) {
    231      1.20      fvdl 		netid = strdup(tnconf->nc_netid);
    232      1.20      fvdl 		flag = 1;
    233      1.20      fvdl 		freenetconfigent(tnconf);
    234      1.20      fvdl 	} /* must have been created with svc_raw_create */
    235      1.20      fvdl 	if ((netid == NULL) && (flag == 1)) {
    236      1.20      fvdl 		return (FALSE);
    237      1.20      fvdl 	}
    238      1.20      fvdl 
    239      1.20      fvdl 	rwlock_wrlock(&svc_lock);
    240      1.21  christos 	if ((s = svc_find(prog, vers, &prev, netid)) != NULL) {
    241      1.20      fvdl 		if (netid)
    242      1.20      fvdl 			free(netid);
    243      1.20      fvdl 		if (s->sc_dispatch == dispatch)
    244      1.20      fvdl 			goto rpcb_it; /* he is registering another xptr */
    245      1.20      fvdl 		rwlock_unlock(&svc_lock);
    246      1.20      fvdl 		return (FALSE);
    247      1.20      fvdl 	}
    248      1.21  christos 	s = mem_alloc(sizeof (struct svc_callout));
    249      1.21  christos 	if (s == NULL) {
    250      1.20      fvdl 		if (netid)
    251      1.20      fvdl 			free(netid);
    252      1.20      fvdl 		rwlock_unlock(&svc_lock);
    253      1.20      fvdl 		return (FALSE);
    254      1.20      fvdl 	}
    255      1.20      fvdl 
    256      1.28  christos 	if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
    257      1.28  christos 		if ((((SVCXPRT *) xprt)->xp_netid = strdup(netid)) == NULL) {
    258      1.28  christos 			warn("svc_reg");
    259      1.28  christos 			mem_free(s, sizeof(struct svc_callout));
    260      1.28  christos 			rwlock_unlock(&svc_lock);
    261      1.28  christos 			return FALSE;
    262      1.28  christos 		}
    263      1.28  christos 
    264      1.20      fvdl 	s->sc_prog = prog;
    265      1.20      fvdl 	s->sc_vers = vers;
    266      1.20      fvdl 	s->sc_dispatch = dispatch;
    267      1.20      fvdl 	s->sc_netid = netid;
    268      1.20      fvdl 	s->sc_next = svc_head;
    269      1.20      fvdl 	svc_head = s;
    270      1.20      fvdl 
    271      1.20      fvdl rpcb_it:
    272      1.20      fvdl 	rwlock_unlock(&svc_lock);
    273      1.20      fvdl 	/* now register the information with the local binder service */
    274      1.20      fvdl 	if (nconf) {
    275      1.26  christos 		dummy = rpcb_set(prog, vers, __UNCONST(nconf),
    276      1.20      fvdl 		&((SVCXPRT *) xprt)->xp_ltaddr);
    277      1.20      fvdl 		return (dummy);
    278      1.20      fvdl 	}
    279      1.20      fvdl 	return (TRUE);
    280      1.20      fvdl }
    281      1.20      fvdl 
    282      1.20      fvdl /*
    283      1.20      fvdl  * Remove a service program from the callout list.
    284      1.20      fvdl  */
    285      1.20      fvdl void
    286      1.20      fvdl svc_unreg(prog, vers)
    287      1.20      fvdl 	const rpcprog_t prog;
    288      1.20      fvdl 	const rpcvers_t vers;
    289      1.20      fvdl {
    290      1.20      fvdl 	struct svc_callout *prev;
    291      1.21  christos 	struct svc_callout *s;
    292      1.20      fvdl 
    293      1.20      fvdl 	/* unregister the information anyway */
    294      1.20      fvdl 	(void) rpcb_unset(prog, vers, NULL);
    295      1.20      fvdl 	rwlock_wrlock(&svc_lock);
    296      1.21  christos 	while ((s = svc_find(prog, vers, &prev, NULL)) != NULL) {
    297      1.21  christos 		if (prev == NULL) {
    298      1.20      fvdl 			svc_head = s->sc_next;
    299      1.20      fvdl 		} else {
    300      1.20      fvdl 			prev->sc_next = s->sc_next;
    301      1.20      fvdl 		}
    302      1.21  christos 		s->sc_next = NULL;
    303      1.20      fvdl 		if (s->sc_netid)
    304      1.21  christos 			mem_free(s->sc_netid, sizeof (s->sc_netid) + 1);
    305      1.21  christos 		mem_free(s, sizeof (struct svc_callout));
    306      1.20      fvdl 	}
    307      1.20      fvdl 	rwlock_unlock(&svc_lock);
    308      1.20      fvdl }
    309       1.1       cgd 
    310       1.1       cgd /* ********************** CALLOUT list related stuff ************* */
    311       1.1       cgd 
    312      1.20      fvdl #ifdef PORTMAP
    313       1.1       cgd /*
    314       1.1       cgd  * Add a service program to the callout list.
    315       1.1       cgd  * The dispatch routine will be called when a rpc request for this
    316       1.1       cgd  * program number comes in.
    317       1.1       cgd  */
    318       1.1       cgd bool_t
    319       1.1       cgd svc_register(xprt, prog, vers, dispatch, protocol)
    320       1.1       cgd 	SVCXPRT *xprt;
    321      1.13     lukem 	u_long prog;
    322      1.13     lukem 	u_long vers;
    323      1.10  christos 	void (*dispatch) __P((struct svc_req *, SVCXPRT *));
    324       1.1       cgd 	int protocol;
    325       1.1       cgd {
    326       1.1       cgd 	struct svc_callout *prev;
    327      1.14     lukem 	struct svc_callout *s;
    328       1.1       cgd 
    329      1.17     lukem 	_DIAGASSERT(xprt != NULL);
    330      1.17     lukem 	_DIAGASSERT(dispatch != NULL);
    331      1.17     lukem 
    332      1.21  christos 	if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) !=
    333      1.21  christos 	    NULL) {
    334       1.1       cgd 		if (s->sc_dispatch == dispatch)
    335       1.1       cgd 			goto pmap_it;  /* he is registering another xptr */
    336       1.1       cgd 		return (FALSE);
    337       1.1       cgd 	}
    338      1.21  christos 	s = mem_alloc(sizeof(struct svc_callout));
    339      1.21  christos 	if (s == NULL) {
    340       1.1       cgd 		return (FALSE);
    341       1.1       cgd 	}
    342      1.21  christos 	s->sc_prog = (rpcprog_t)prog;
    343      1.21  christos 	s->sc_vers = (rpcvers_t)vers;
    344       1.1       cgd 	s->sc_dispatch = dispatch;
    345       1.1       cgd 	s->sc_next = svc_head;
    346       1.1       cgd 	svc_head = s;
    347       1.1       cgd pmap_it:
    348       1.1       cgd 	/* now register the information with the local binder service */
    349       1.1       cgd 	if (protocol) {
    350       1.1       cgd 		return (pmap_set(prog, vers, protocol, xprt->xp_port));
    351       1.1       cgd 	}
    352       1.1       cgd 	return (TRUE);
    353       1.1       cgd }
    354       1.1       cgd 
    355       1.1       cgd /*
    356       1.1       cgd  * Remove a service program from the callout list.
    357       1.1       cgd  */
    358       1.1       cgd void
    359       1.1       cgd svc_unregister(prog, vers)
    360      1.13     lukem 	u_long prog;
    361      1.13     lukem 	u_long vers;
    362       1.1       cgd {
    363       1.1       cgd 	struct svc_callout *prev;
    364      1.14     lukem 	struct svc_callout *s;
    365       1.1       cgd 
    366      1.21  christos 	if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) ==
    367      1.21  christos 	    NULL)
    368       1.1       cgd 		return;
    369      1.21  christos 	if (prev == NULL) {
    370       1.1       cgd 		svc_head = s->sc_next;
    371       1.1       cgd 	} else {
    372       1.1       cgd 		prev->sc_next = s->sc_next;
    373       1.1       cgd 	}
    374      1.21  christos 	s->sc_next = NULL;
    375      1.15  christos 	mem_free(s, sizeof(struct svc_callout));
    376       1.1       cgd 	/* now unregister the information with the local binder service */
    377       1.1       cgd 	(void)pmap_unset(prog, vers);
    378       1.1       cgd }
    379      1.20      fvdl #endif /* PORTMAP */
    380       1.1       cgd 
    381       1.1       cgd /*
    382       1.1       cgd  * Search the callout list for a program number, return the callout
    383       1.1       cgd  * struct.
    384       1.1       cgd  */
    385       1.1       cgd static struct svc_callout *
    386      1.20      fvdl svc_find(prog, vers, prev, netid)
    387      1.20      fvdl 	rpcprog_t prog;
    388      1.20      fvdl 	rpcvers_t vers;
    389       1.1       cgd 	struct svc_callout **prev;
    390      1.20      fvdl 	char *netid;
    391       1.1       cgd {
    392      1.14     lukem 	struct svc_callout *s, *p;
    393       1.1       cgd 
    394      1.17     lukem 	_DIAGASSERT(prev != NULL);
    395      1.22     lukem 	/* netid is handled below */
    396      1.17     lukem 
    397      1.21  christos 	p = NULL;
    398      1.21  christos 	for (s = svc_head; s != NULL; s = s->sc_next) {
    399      1.20      fvdl 		if (((s->sc_prog == prog) && (s->sc_vers == vers)) &&
    400      1.20      fvdl 		    ((netid == NULL) || (s->sc_netid == NULL) ||
    401      1.20      fvdl 		    (strcmp(netid, s->sc_netid) == 0)))
    402      1.20      fvdl 			break;
    403       1.1       cgd 		p = s;
    404       1.1       cgd 	}
    405       1.1       cgd 	*prev = p;
    406       1.1       cgd 	return (s);
    407       1.1       cgd }
    408       1.1       cgd 
    409       1.1       cgd /* ******************* REPLY GENERATION ROUTINES  ************ */
    410       1.1       cgd 
    411       1.1       cgd /*
    412       1.1       cgd  * Send a reply to an rpc request
    413       1.1       cgd  */
    414       1.1       cgd bool_t
    415       1.1       cgd svc_sendreply(xprt, xdr_results, xdr_location)
    416      1.14     lukem 	SVCXPRT *xprt;
    417       1.1       cgd 	xdrproc_t xdr_results;
    418      1.27      yamt 	const char *xdr_location;
    419       1.1       cgd {
    420       1.1       cgd 	struct rpc_msg rply;
    421       1.1       cgd 
    422      1.17     lukem 	_DIAGASSERT(xprt != NULL);
    423      1.17     lukem 
    424       1.1       cgd 	rply.rm_direction = REPLY;
    425       1.1       cgd 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    426       1.1       cgd 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    427       1.1       cgd 	rply.acpted_rply.ar_stat = SUCCESS;
    428       1.1       cgd 	rply.acpted_rply.ar_results.where = xdr_location;
    429       1.1       cgd 	rply.acpted_rply.ar_results.proc = xdr_results;
    430       1.1       cgd 	return (SVC_REPLY(xprt, &rply));
    431       1.1       cgd }
    432       1.1       cgd 
    433       1.1       cgd /*
    434       1.1       cgd  * No procedure error reply
    435       1.1       cgd  */
    436       1.1       cgd void
    437       1.1       cgd svcerr_noproc(xprt)
    438      1.14     lukem 	SVCXPRT *xprt;
    439       1.1       cgd {
    440       1.1       cgd 	struct rpc_msg rply;
    441       1.1       cgd 
    442      1.17     lukem 	_DIAGASSERT(xprt != NULL);
    443      1.17     lukem 
    444       1.1       cgd 	rply.rm_direction = REPLY;
    445       1.1       cgd 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    446       1.1       cgd 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    447       1.1       cgd 	rply.acpted_rply.ar_stat = PROC_UNAVAIL;
    448       1.1       cgd 	SVC_REPLY(xprt, &rply);
    449       1.1       cgd }
    450       1.1       cgd 
    451       1.1       cgd /*
    452       1.1       cgd  * Can't decode args error reply
    453       1.1       cgd  */
    454       1.1       cgd void
    455       1.1       cgd svcerr_decode(xprt)
    456      1.14     lukem 	SVCXPRT *xprt;
    457       1.1       cgd {
    458       1.1       cgd 	struct rpc_msg rply;
    459       1.1       cgd 
    460      1.17     lukem 	_DIAGASSERT(xprt != NULL);
    461      1.17     lukem 
    462       1.1       cgd 	rply.rm_direction = REPLY;
    463       1.1       cgd 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    464       1.1       cgd 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    465       1.1       cgd 	rply.acpted_rply.ar_stat = GARBAGE_ARGS;
    466       1.1       cgd 	SVC_REPLY(xprt, &rply);
    467       1.1       cgd }
    468       1.1       cgd 
    469       1.1       cgd /*
    470       1.1       cgd  * Some system error
    471       1.1       cgd  */
    472       1.1       cgd void
    473       1.1       cgd svcerr_systemerr(xprt)
    474      1.14     lukem 	SVCXPRT *xprt;
    475       1.1       cgd {
    476       1.1       cgd 	struct rpc_msg rply;
    477       1.1       cgd 
    478      1.17     lukem 	_DIAGASSERT(xprt != NULL);
    479      1.17     lukem 
    480       1.1       cgd 	rply.rm_direction = REPLY;
    481       1.1       cgd 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    482       1.1       cgd 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    483       1.1       cgd 	rply.acpted_rply.ar_stat = SYSTEM_ERR;
    484       1.1       cgd 	SVC_REPLY(xprt, &rply);
    485       1.1       cgd }
    486       1.1       cgd 
    487      1.20      fvdl #if 0
    488      1.20      fvdl /*
    489      1.20      fvdl  * Tell RPC package to not complain about version errors to the client.	 This
    490      1.20      fvdl  * is useful when revving broadcast protocols that sit on a fixed address.
    491      1.20      fvdl  * There is really one (or should be only one) example of this kind of
    492      1.20      fvdl  * protocol: the portmapper (or rpc binder).
    493      1.20      fvdl  */
    494      1.20      fvdl void
    495      1.20      fvdl __svc_versquiet_on(xprt)
    496      1.21  christos 	SVCXPRT *xprt;
    497      1.20      fvdl {
    498      1.20      fvdl 	u_long	tmp;
    499      1.20      fvdl 
    500      1.22     lukem 	_DIAGASSERT(xprt != NULL);
    501      1.22     lukem 
    502      1.20      fvdl 	tmp = ((u_long) xprt->xp_p3) | SVC_VERSQUIET;
    503      1.20      fvdl 	xprt->xp_p3 = (caddr_t) tmp;
    504      1.20      fvdl }
    505      1.20      fvdl 
    506      1.20      fvdl void
    507      1.20      fvdl __svc_versquiet_off(xprt)
    508      1.21  christos 	SVCXPRT *xprt;
    509      1.20      fvdl {
    510      1.20      fvdl 	u_long	tmp;
    511      1.20      fvdl 
    512      1.22     lukem 	_DIAGASSERT(xprt != NULL);
    513      1.22     lukem 
    514      1.20      fvdl 	tmp = ((u_long) xprt->xp_p3) & ~SVC_VERSQUIET;
    515      1.20      fvdl 	xprt->xp_p3 = (caddr_t) tmp;
    516      1.20      fvdl }
    517      1.20      fvdl 
    518      1.20      fvdl void
    519      1.20      fvdl svc_versquiet(xprt)
    520      1.21  christos 	SVCXPRT *xprt;
    521      1.20      fvdl {
    522      1.20      fvdl 	__svc_versquiet_on(xprt);
    523      1.20      fvdl }
    524      1.20      fvdl 
    525      1.20      fvdl int
    526      1.20      fvdl __svc_versquiet_get(xprt)
    527      1.21  christos 	SVCXPRT *xprt;
    528      1.20      fvdl {
    529      1.22     lukem 
    530      1.22     lukem 	_DIAGASSERT(xprt != NULL);
    531      1.22     lukem 
    532      1.20      fvdl 	return ((int) xprt->xp_p3) & SVC_VERSQUIET;
    533      1.20      fvdl }
    534      1.20      fvdl #endif
    535      1.20      fvdl 
    536       1.1       cgd /*
    537       1.1       cgd  * Authentication error reply
    538       1.1       cgd  */
    539       1.1       cgd void
    540       1.1       cgd svcerr_auth(xprt, why)
    541       1.1       cgd 	SVCXPRT *xprt;
    542       1.1       cgd 	enum auth_stat why;
    543       1.1       cgd {
    544       1.1       cgd 	struct rpc_msg rply;
    545       1.1       cgd 
    546      1.17     lukem 	_DIAGASSERT(xprt != NULL);
    547      1.17     lukem 
    548       1.1       cgd 	rply.rm_direction = REPLY;
    549       1.1       cgd 	rply.rm_reply.rp_stat = MSG_DENIED;
    550       1.1       cgd 	rply.rjcted_rply.rj_stat = AUTH_ERROR;
    551       1.1       cgd 	rply.rjcted_rply.rj_why = why;
    552       1.1       cgd 	SVC_REPLY(xprt, &rply);
    553       1.1       cgd }
    554       1.1       cgd 
    555       1.1       cgd /*
    556       1.1       cgd  * Auth too weak error reply
    557       1.1       cgd  */
    558       1.1       cgd void
    559       1.1       cgd svcerr_weakauth(xprt)
    560       1.1       cgd 	SVCXPRT *xprt;
    561       1.1       cgd {
    562       1.1       cgd 
    563      1.17     lukem 	_DIAGASSERT(xprt != NULL);
    564      1.17     lukem 
    565       1.1       cgd 	svcerr_auth(xprt, AUTH_TOOWEAK);
    566       1.1       cgd }
    567       1.1       cgd 
    568       1.1       cgd /*
    569       1.1       cgd  * Program unavailable error reply
    570       1.1       cgd  */
    571       1.1       cgd void
    572       1.1       cgd svcerr_noprog(xprt)
    573      1.14     lukem 	SVCXPRT *xprt;
    574       1.1       cgd {
    575       1.1       cgd 	struct rpc_msg rply;
    576       1.1       cgd 
    577      1.17     lukem 	_DIAGASSERT(xprt != NULL);
    578      1.17     lukem 
    579       1.1       cgd 	rply.rm_direction = REPLY;
    580       1.1       cgd 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    581       1.1       cgd 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    582       1.1       cgd 	rply.acpted_rply.ar_stat = PROG_UNAVAIL;
    583       1.1       cgd 	SVC_REPLY(xprt, &rply);
    584       1.1       cgd }
    585       1.1       cgd 
    586       1.1       cgd /*
    587       1.1       cgd  * Program version mismatch error reply
    588       1.1       cgd  */
    589       1.1       cgd void
    590       1.1       cgd svcerr_progvers(xprt, low_vers, high_vers)
    591      1.14     lukem 	SVCXPRT *xprt;
    592      1.20      fvdl 	rpcvers_t low_vers;
    593      1.20      fvdl 	rpcvers_t high_vers;
    594       1.1       cgd {
    595       1.1       cgd 	struct rpc_msg rply;
    596       1.1       cgd 
    597      1.17     lukem 	_DIAGASSERT(xprt != NULL);
    598      1.17     lukem 
    599       1.1       cgd 	rply.rm_direction = REPLY;
    600       1.1       cgd 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    601       1.1       cgd 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    602       1.1       cgd 	rply.acpted_rply.ar_stat = PROG_MISMATCH;
    603      1.15  christos 	rply.acpted_rply.ar_vers.low = (u_int32_t)low_vers;
    604      1.15  christos 	rply.acpted_rply.ar_vers.high = (u_int32_t)high_vers;
    605       1.1       cgd 	SVC_REPLY(xprt, &rply);
    606       1.1       cgd }
    607       1.1       cgd 
    608       1.1       cgd /* ******************* SERVER INPUT STUFF ******************* */
    609       1.1       cgd 
    610       1.1       cgd /*
    611       1.1       cgd  * Get server side input from some transport.
    612       1.1       cgd  *
    613       1.1       cgd  * Statement of authentication parameters management:
    614       1.1       cgd  * This function owns and manages all authentication parameters, specifically
    615       1.1       cgd  * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
    616       1.1       cgd  * the "cooked" credentials (rqst->rq_clntcred).
    617       1.1       cgd  * However, this function does not know the structure of the cooked
    618       1.1       cgd  * credentials, so it make the following assumptions:
    619       1.1       cgd  *   a) the structure is contiguous (no pointers), and
    620       1.1       cgd  *   b) the cred structure size does not exceed RQCRED_SIZE bytes.
    621       1.1       cgd  * In all events, all three parameters are freed upon exit from this routine.
    622       1.1       cgd  * The storage is trivially management on the call stack in user land, but
    623       1.1       cgd  * is mallocated in kernel land.
    624       1.1       cgd  */
    625       1.1       cgd 
    626       1.1       cgd void
    627       1.1       cgd svc_getreq(rdfds)
    628       1.1       cgd 	int rdfds;
    629       1.1       cgd {
    630       1.1       cgd 	fd_set readfds;
    631       1.1       cgd 
    632       1.1       cgd 	FD_ZERO(&readfds);
    633      1.29     rmind 	readfds.fds_bits[0] = (unsigned int)rdfds;
    634       1.1       cgd 	svc_getreqset(&readfds);
    635       1.1       cgd }
    636       1.1       cgd 
    637       1.1       cgd void
    638       1.1       cgd svc_getreqset(readfds)
    639       1.1       cgd 	fd_set *readfds;
    640       1.1       cgd {
    641      1.29     rmind 	uint32_t mask, *maskp;
    642      1.29     rmind 	int sock, bit, fd;
    643       1.1       cgd 
    644      1.17     lukem 	_DIAGASSERT(readfds != NULL);
    645       1.1       cgd 
    646       1.4       cgd 	maskp = readfds->fds_bits;
    647       1.2   deraadt 	for (sock = 0; sock < FD_SETSIZE; sock += NFDBITS) {
    648      1.30      tron 	    for (mask = *maskp++; (bit = ffs((int)mask)) != 0;
    649      1.10  christos 		mask ^= (1 << (bit - 1))) {
    650       1.1       cgd 		/* sock has input waiting */
    651      1.20      fvdl 		fd = sock + bit - 1;
    652      1.20      fvdl 		svc_getreq_common(fd);
    653      1.20      fvdl 	    }
    654      1.20      fvdl 	}
    655      1.20      fvdl }
    656      1.20      fvdl 
    657      1.20      fvdl void
    658      1.20      fvdl svc_getreq_common(fd)
    659      1.20      fvdl 	int fd;
    660      1.20      fvdl {
    661      1.20      fvdl 	SVCXPRT *xprt;
    662      1.20      fvdl 	struct svc_req r;
    663      1.20      fvdl 	struct rpc_msg msg;
    664      1.20      fvdl 	int prog_found;
    665      1.20      fvdl 	rpcvers_t low_vers;
    666      1.20      fvdl 	rpcvers_t high_vers;
    667      1.20      fvdl 	enum xprt_stat stat;
    668      1.20      fvdl 	char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
    669      1.20      fvdl 
    670      1.20      fvdl 	msg.rm_call.cb_cred.oa_base = cred_area;
    671      1.20      fvdl 	msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
    672      1.20      fvdl 	r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
    673      1.20      fvdl 
    674      1.20      fvdl 	rwlock_rdlock(&svc_fd_lock);
    675      1.23      fvdl 	xprt = __svc_xports[fd];
    676      1.20      fvdl 	rwlock_unlock(&svc_fd_lock);
    677      1.20      fvdl 	if (xprt == NULL)
    678      1.20      fvdl 		/* But do we control sock? */
    679      1.20      fvdl 		return;
    680      1.20      fvdl 	/* now receive msgs from xprtprt (support batch calls) */
    681      1.20      fvdl 	do {
    682      1.20      fvdl 		if (SVC_RECV(xprt, &msg)) {
    683      1.20      fvdl 
    684      1.20      fvdl 			/* now find the exported program and call it */
    685      1.20      fvdl 			struct svc_callout *s;
    686      1.20      fvdl 			enum auth_stat why;
    687      1.20      fvdl 
    688      1.20      fvdl 			r.rq_xprt = xprt;
    689      1.20      fvdl 			r.rq_prog = msg.rm_call.cb_prog;
    690      1.20      fvdl 			r.rq_vers = msg.rm_call.cb_vers;
    691      1.20      fvdl 			r.rq_proc = msg.rm_call.cb_proc;
    692      1.20      fvdl 			r.rq_cred = msg.rm_call.cb_cred;
    693      1.20      fvdl 			/* first authenticate the message */
    694      1.20      fvdl 			if ((why = _authenticate(&r, &msg)) != AUTH_OK) {
    695      1.20      fvdl 				svcerr_auth(xprt, why);
    696      1.20      fvdl 				goto call_done;
    697       1.1       cgd 			}
    698      1.20      fvdl 			/* now match message with a registered service*/
    699      1.20      fvdl 			prog_found = FALSE;
    700      1.20      fvdl 			low_vers = (rpcvers_t) -1L;
    701      1.20      fvdl 			high_vers = (rpcvers_t) 0L;
    702      1.21  christos 			for (s = svc_head; s != NULL; s = s->sc_next) {
    703      1.20      fvdl 				if (s->sc_prog == r.rq_prog) {
    704      1.20      fvdl 					if (s->sc_vers == r.rq_vers) {
    705      1.20      fvdl 						(*s->sc_dispatch)(&r, xprt);
    706      1.20      fvdl 						goto call_done;
    707      1.20      fvdl 					}  /* found correct version */
    708      1.20      fvdl 					prog_found = TRUE;
    709      1.20      fvdl 					if (s->sc_vers < low_vers)
    710      1.20      fvdl 						low_vers = s->sc_vers;
    711      1.20      fvdl 					if (s->sc_vers > high_vers)
    712      1.20      fvdl 						high_vers = s->sc_vers;
    713      1.20      fvdl 				}   /* found correct program */
    714       1.1       cgd 			}
    715      1.20      fvdl 			/*
    716      1.20      fvdl 			 * if we got here, the program or version
    717      1.20      fvdl 			 * is not served ...
    718      1.20      fvdl 			 */
    719      1.20      fvdl 			if (prog_found)
    720      1.20      fvdl 				svcerr_progvers(xprt, low_vers, high_vers);
    721      1.20      fvdl 			else
    722      1.20      fvdl 				 svcerr_noprog(xprt);
    723      1.20      fvdl 			/* Fall through to ... */
    724      1.20      fvdl 		}
    725      1.20      fvdl 		/*
    726      1.20      fvdl 		 * Check if the xprt has been disconnected in a
    727      1.20      fvdl 		 * recursive call in the service dispatch routine.
    728      1.20      fvdl 		 * If so, then break.
    729      1.20      fvdl 		 */
    730      1.20      fvdl 		rwlock_rdlock(&svc_fd_lock);
    731      1.23      fvdl 		if (xprt != __svc_xports[fd]) {
    732      1.20      fvdl 			rwlock_unlock(&svc_fd_lock);
    733      1.20      fvdl 			break;
    734      1.20      fvdl 		}
    735      1.20      fvdl 		rwlock_unlock(&svc_fd_lock);
    736      1.20      fvdl call_done:
    737      1.20      fvdl 		if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
    738      1.20      fvdl 			SVC_DESTROY(xprt);
    739      1.20      fvdl 			break;
    740      1.20      fvdl 		}
    741      1.20      fvdl 	} while (stat == XPRT_MOREREQS);
    742      1.20      fvdl }
    743      1.20      fvdl 
    744      1.20      fvdl 
    745      1.20      fvdl void
    746      1.20      fvdl svc_getreq_poll(pfdp, pollretval)
    747      1.20      fvdl 	struct pollfd	*pfdp;
    748      1.20      fvdl 	int	pollretval;
    749      1.20      fvdl {
    750      1.20      fvdl 	int i;
    751      1.20      fvdl 	int fds_found;
    752      1.22     lukem 
    753      1.22     lukem 	_DIAGASSERT(pfdp != NULL);
    754      1.20      fvdl 
    755      1.20      fvdl 	for (i = fds_found = 0; fds_found < pollretval; i++) {
    756      1.21  christos 		struct pollfd *p = &pfdp[i];
    757      1.20      fvdl 
    758      1.20      fvdl 		if (p->revents) {
    759      1.20      fvdl 			/* fd has input waiting */
    760      1.20      fvdl 			fds_found++;
    761      1.20      fvdl 			/*
    762      1.20      fvdl 			 *	We assume that this function is only called
    763      1.20      fvdl 			 *	via someone select()ing from svc_fdset or
    764      1.25  christos 			 *	pollts()ing from svc_pollset[].  Thus it's safe
    765      1.20      fvdl 			 *	to handle the POLLNVAL event by simply turning
    766      1.20      fvdl 			 *	the corresponding bit off in svc_fdset.  The
    767      1.20      fvdl 			 *	svc_pollset[] array is derived from svc_fdset
    768      1.20      fvdl 			 *	and so will also be updated eventually.
    769      1.20      fvdl 			 *
    770      1.20      fvdl 			 *	XXX Should we do an xprt_unregister() instead?
    771      1.20      fvdl 			 */
    772      1.20      fvdl 			if (p->revents & POLLNVAL) {
    773      1.20      fvdl 				rwlock_wrlock(&svc_fd_lock);
    774      1.20      fvdl 				FD_CLR(p->fd, &svc_fdset);
    775      1.20      fvdl 				rwlock_unlock(&svc_fd_lock);
    776      1.20      fvdl 			} else
    777      1.20      fvdl 				svc_getreq_common(p->fd);
    778      1.20      fvdl 		}
    779       1.1       cgd 	}
    780      1.23      fvdl }
    781      1.23      fvdl 
    782      1.23      fvdl bool_t
    783      1.23      fvdl rpc_control(int what, void *arg)
    784      1.23      fvdl {
    785      1.23      fvdl 	int val;
    786      1.23      fvdl 
    787      1.23      fvdl 	switch (what) {
    788      1.23      fvdl 	case RPC_SVC_CONNMAXREC_SET:
    789      1.23      fvdl 		val = *(int *)arg;
    790      1.23      fvdl 		if (val <= 0)
    791      1.23      fvdl 			return FALSE;
    792      1.23      fvdl 		__svc_maxrec = val;
    793      1.23      fvdl 		return TRUE;
    794      1.23      fvdl 	case RPC_SVC_CONNMAXREC_GET:
    795      1.23      fvdl 		*(int *)arg = __svc_maxrec;
    796      1.23      fvdl 		return TRUE;
    797      1.23      fvdl 	default:
    798      1.23      fvdl 		break;
    799      1.23      fvdl 	}
    800      1.23      fvdl 	return FALSE;
    801       1.1       cgd }
    802