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