Home | History | Annotate | Line # | Download | only in rpc
      1  1.41  christos /*	$NetBSD: svc.c,v 1.41 2024/01/23 17:24:38 christos 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.41  christos __RCSID("$NetBSD: svc.c,v 1.41 2024/01/23 17:24:38 christos 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.35  christos /* __svc_xports[-1] is reserved for raw */
     94  1.23      fvdl SVCXPRT **__svc_xports;
     95  1.35  christos int __svc_maxxports;
     96  1.23      fvdl int __svc_maxrec;
     97   1.1       cgd 
     98   1.1       cgd #define	RQCRED_SIZE	400		/* this size is excessive */
     99   1.1       cgd 
    100  1.20      fvdl #define SVC_VERSQUIET 0x0001		/* keep quiet about vers mismatch */
    101  1.20      fvdl #define version_keepquiet(xp) ((u_long)(xp)->xp_p3 & SVC_VERSQUIET)
    102  1.20      fvdl 
    103   1.2   deraadt #define max(a, b) (a > b ? a : b)
    104   1.2   deraadt 
    105   1.1       cgd /*
    106   1.1       cgd  * The services list
    107   1.1       cgd  * Each entry represents a set of procedures (an rpc program).
    108   1.1       cgd  * The dispatch routine takes request structs and runs the
    109  1.40    andvar  * appropriate procedure.
    110   1.1       cgd  */
    111   1.1       cgd static struct svc_callout {
    112   1.1       cgd 	struct svc_callout *sc_next;
    113  1.20      fvdl 	rpcprog_t	    sc_prog;
    114  1.20      fvdl 	rpcvers_t	    sc_vers;
    115  1.20      fvdl 	char		   *sc_netid;
    116  1.31      matt 	void		    (*sc_dispatch)(struct svc_req *, SVCXPRT *);
    117   1.1       cgd } *svc_head;
    118   1.1       cgd 
    119  1.31      matt static struct svc_callout *svc_find(rpcprog_t, rpcvers_t,
    120  1.31      matt 					 struct svc_callout **, char *);
    121  1.31      matt static void __xprt_do_unregister(SVCXPRT *xprt, bool_t dolock);
    122   1.1       cgd 
    123   1.1       cgd /* ***************  SVCXPRT related stuff **************** */
    124   1.3   deraadt 
    125  1.35  christos static bool_t
    126  1.35  christos xprt_alloc(int sock)
    127  1.35  christos {
    128  1.39  christos 	int oldmaxxports, newmaxxports;
    129  1.37      tron 	SVCXPRT **oldxports, **newxports;
    130  1.35  christos 
    131  1.35  christos 	if (++sock < 0)
    132  1.35  christos 		return FALSE;
    133  1.35  christos 
    134  1.39  christos 	newmaxxports = svc_fdset_getsize(sock);
    135  1.39  christos 	if (newmaxxports == -1)
    136  1.35  christos 		return FALSE;
    137  1.35  christos 
    138  1.39  christos 	if (__svc_xports != NULL && newmaxxports < __svc_maxxports)
    139  1.35  christos 		return TRUE;
    140  1.35  christos 
    141  1.37      tron 	oldxports = __svc_xports;
    142  1.39  christos 	oldmaxxports = __svc_maxxports;
    143  1.39  christos 	if (oldxports != NULL) {
    144  1.39  christos 		/* revert saving [-1] slot */
    145  1.37      tron 		--oldxports;
    146  1.39  christos 		++oldmaxxports;
    147  1.39  christos 	}
    148  1.39  christos 
    149  1.39  christos 	/* reserve an extra slot for [-1] */
    150  1.39  christos 	newmaxxports++;
    151  1.39  christos 	newxports = realloc(oldxports, newmaxxports * sizeof(SVCXPRT *));
    152  1.35  christos 	if (newxports == NULL) {
    153  1.35  christos 		warn("%s: out of memory", __func__);
    154  1.35  christos 		return FALSE;
    155  1.35  christos 	}
    156  1.35  christos 
    157  1.39  christos 	memset(&newxports[oldmaxxports], 0,
    158  1.39  christos 	    (newmaxxports - oldmaxxports) * sizeof(SVCXPRT *));
    159  1.35  christos 
    160  1.39  christos 	/* save one slot for [-1] */
    161  1.39  christos 	__svc_xports = newxports + 1;
    162  1.39  christos 	__svc_maxxports = newmaxxports - 1;
    163  1.35  christos 
    164  1.35  christos 	return TRUE;
    165  1.35  christos }
    166  1.35  christos 
    167   1.1       cgd /*
    168   1.1       cgd  * Activate a transport handle.
    169   1.1       cgd  */
    170  1.32  christos bool_t
    171  1.31      matt xprt_register(SVCXPRT *xprt)
    172   1.1       cgd {
    173  1.17     lukem 	int sock;
    174  1.17     lukem 
    175  1.17     lukem 	_DIAGASSERT(xprt != NULL);
    176  1.17     lukem 
    177  1.35  christos 	rwlock_wrlock(&svc_fd_lock);
    178  1.20      fvdl 	sock = xprt->xp_fd;
    179   1.1       cgd 
    180  1.35  christos 	if (!xprt_alloc(sock))
    181  1.32  christos 		goto out;
    182  1.35  christos 
    183  1.35  christos 	__svc_xports[sock] = xprt;
    184  1.35  christos 	if (sock != -1) {
    185  1.36  christos 		if (svc_fdset_set(sock) == -1)
    186  1.36  christos 			return FALSE;
    187  1.32  christos 	}
    188  1.32  christos 	rwlock_unlock(&svc_fd_lock);
    189  1.32  christos 	return (TRUE);
    190  1.32  christos 
    191  1.28  christos out:
    192  1.20      fvdl 	rwlock_unlock(&svc_fd_lock);
    193  1.32  christos 	return (FALSE);
    194   1.1       cgd }
    195   1.1       cgd 
    196  1.23      fvdl void
    197  1.23      fvdl xprt_unregister(SVCXPRT *xprt)
    198  1.23      fvdl {
    199  1.23      fvdl 	__xprt_do_unregister(xprt, TRUE);
    200  1.23      fvdl }
    201  1.23      fvdl 
    202  1.23      fvdl void
    203  1.23      fvdl __xprt_unregister_unlocked(SVCXPRT *xprt)
    204  1.23      fvdl {
    205  1.23      fvdl 	__xprt_do_unregister(xprt, FALSE);
    206  1.23      fvdl }
    207  1.23      fvdl 
    208   1.1       cgd /*
    209   1.1       cgd  * De-activate a transport handle.
    210   1.1       cgd  */
    211  1.23      fvdl static void
    212  1.31      matt __xprt_do_unregister(SVCXPRT *xprt, bool_t dolock)
    213   1.1       cgd {
    214  1.35  christos 	int sock, *fdmax;
    215  1.17     lukem 
    216  1.17     lukem 	_DIAGASSERT(xprt != NULL);
    217  1.17     lukem 
    218  1.35  christos 	if (dolock)
    219  1.35  christos 		rwlock_wrlock(&svc_fd_lock);
    220  1.35  christos 
    221  1.20      fvdl 	sock = xprt->xp_fd;
    222  1.35  christos 	if (sock >= __svc_maxxports || __svc_xports[sock] != xprt)
    223  1.35  christos 		goto out;
    224  1.35  christos 
    225  1.35  christos 	__svc_xports[sock] = NULL;
    226  1.35  christos 	if (sock == -1)
    227  1.35  christos 		goto out;
    228  1.35  christos 	fdmax = svc_fdset_getmax();
    229  1.36  christos 	if (fdmax == NULL || sock < *fdmax)
    230  1.35  christos 		goto clr;
    231   1.1       cgd 
    232  1.35  christos 	for ((*fdmax)--; *fdmax >= 0; (*fdmax)--)
    233  1.35  christos 		if (__svc_xports[*fdmax])
    234  1.35  christos 			break;
    235  1.35  christos clr:
    236  1.35  christos 	svc_fdset_clr(sock);
    237  1.35  christos out:
    238  1.23      fvdl 	if (dolock)
    239  1.23      fvdl 		rwlock_unlock(&svc_fd_lock);
    240   1.1       cgd }
    241   1.1       cgd 
    242  1.20      fvdl /*
    243  1.20      fvdl  * Add a service program to the callout list.
    244  1.20      fvdl  * The dispatch routine will be called when a rpc request for this
    245  1.20      fvdl  * program number comes in.
    246  1.20      fvdl  */
    247  1.20      fvdl bool_t
    248  1.31      matt svc_reg(SVCXPRT *xprt, const rpcprog_t prog, const rpcvers_t vers,
    249  1.31      matt 	void (*dispatch)(struct svc_req *, SVCXPRT *),
    250  1.31      matt 	const struct netconfig *nconf)
    251  1.20      fvdl {
    252  1.20      fvdl 	bool_t dummy;
    253  1.20      fvdl 	struct svc_callout *prev;
    254  1.21  christos 	struct svc_callout *s;
    255  1.20      fvdl 	struct netconfig *tnconf;
    256  1.21  christos 	char *netid = NULL;
    257  1.20      fvdl 	int flag = 0;
    258  1.20      fvdl 
    259  1.22     lukem 	_DIAGASSERT(xprt != NULL);
    260  1.22     lukem 	/* XXX: dispatch may be NULL ??? */
    261  1.22     lukem 
    262  1.20      fvdl /* VARIABLES PROTECTED BY svc_lock: s, prev, svc_head */
    263  1.20      fvdl 
    264  1.20      fvdl 	if (xprt->xp_netid) {
    265  1.20      fvdl 		netid = strdup(xprt->xp_netid);
    266  1.20      fvdl 		flag = 1;
    267  1.20      fvdl 	} else if (nconf && nconf->nc_netid) {
    268  1.20      fvdl 		netid = strdup(nconf->nc_netid);
    269  1.20      fvdl 		flag = 1;
    270  1.20      fvdl 	} else if ((tnconf = __rpcgettp(xprt->xp_fd)) != NULL) {
    271  1.20      fvdl 		netid = strdup(tnconf->nc_netid);
    272  1.20      fvdl 		flag = 1;
    273  1.20      fvdl 		freenetconfigent(tnconf);
    274  1.20      fvdl 	} /* must have been created with svc_raw_create */
    275  1.20      fvdl 	if ((netid == NULL) && (flag == 1)) {
    276  1.20      fvdl 		return (FALSE);
    277  1.20      fvdl 	}
    278  1.20      fvdl 
    279  1.20      fvdl 	rwlock_wrlock(&svc_lock);
    280  1.21  christos 	if ((s = svc_find(prog, vers, &prev, netid)) != NULL) {
    281  1.20      fvdl 		if (netid)
    282  1.20      fvdl 			free(netid);
    283  1.20      fvdl 		if (s->sc_dispatch == dispatch)
    284  1.20      fvdl 			goto rpcb_it; /* he is registering another xptr */
    285  1.20      fvdl 		rwlock_unlock(&svc_lock);
    286  1.20      fvdl 		return (FALSE);
    287  1.20      fvdl 	}
    288  1.21  christos 	s = mem_alloc(sizeof (struct svc_callout));
    289  1.21  christos 	if (s == NULL) {
    290  1.20      fvdl 		if (netid)
    291  1.20      fvdl 			free(netid);
    292  1.20      fvdl 		rwlock_unlock(&svc_lock);
    293  1.20      fvdl 		return (FALSE);
    294  1.20      fvdl 	}
    295  1.20      fvdl 
    296  1.28  christos 	if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
    297  1.28  christos 		if ((((SVCXPRT *) xprt)->xp_netid = strdup(netid)) == NULL) {
    298  1.28  christos 			warn("svc_reg");
    299  1.28  christos 			mem_free(s, sizeof(struct svc_callout));
    300  1.28  christos 			rwlock_unlock(&svc_lock);
    301  1.28  christos 			return FALSE;
    302  1.28  christos 		}
    303  1.28  christos 
    304  1.20      fvdl 	s->sc_prog = prog;
    305  1.20      fvdl 	s->sc_vers = vers;
    306  1.20      fvdl 	s->sc_dispatch = dispatch;
    307  1.20      fvdl 	s->sc_netid = netid;
    308  1.20      fvdl 	s->sc_next = svc_head;
    309  1.20      fvdl 	svc_head = s;
    310  1.20      fvdl 
    311  1.20      fvdl rpcb_it:
    312  1.20      fvdl 	rwlock_unlock(&svc_lock);
    313  1.20      fvdl 	/* now register the information with the local binder service */
    314  1.20      fvdl 	if (nconf) {
    315  1.26  christos 		dummy = rpcb_set(prog, vers, __UNCONST(nconf),
    316  1.20      fvdl 		&((SVCXPRT *) xprt)->xp_ltaddr);
    317  1.20      fvdl 		return (dummy);
    318  1.20      fvdl 	}
    319  1.20      fvdl 	return (TRUE);
    320  1.20      fvdl }
    321  1.20      fvdl 
    322  1.20      fvdl /*
    323  1.20      fvdl  * Remove a service program from the callout list.
    324  1.20      fvdl  */
    325  1.20      fvdl void
    326  1.31      matt svc_unreg(const rpcprog_t prog, const rpcvers_t vers)
    327  1.20      fvdl {
    328  1.20      fvdl 	struct svc_callout *prev;
    329  1.21  christos 	struct svc_callout *s;
    330  1.20      fvdl 
    331  1.20      fvdl 	/* unregister the information anyway */
    332  1.20      fvdl 	(void) rpcb_unset(prog, vers, NULL);
    333  1.20      fvdl 	rwlock_wrlock(&svc_lock);
    334  1.21  christos 	while ((s = svc_find(prog, vers, &prev, NULL)) != NULL) {
    335  1.21  christos 		if (prev == NULL) {
    336  1.20      fvdl 			svc_head = s->sc_next;
    337  1.20      fvdl 		} else {
    338  1.20      fvdl 			prev->sc_next = s->sc_next;
    339  1.20      fvdl 		}
    340  1.21  christos 		s->sc_next = NULL;
    341  1.20      fvdl 		if (s->sc_netid)
    342  1.21  christos 			mem_free(s->sc_netid, sizeof (s->sc_netid) + 1);
    343  1.21  christos 		mem_free(s, sizeof (struct svc_callout));
    344  1.20      fvdl 	}
    345  1.20      fvdl 	rwlock_unlock(&svc_lock);
    346  1.20      fvdl }
    347   1.1       cgd 
    348   1.1       cgd /* ********************** CALLOUT list related stuff ************* */
    349   1.1       cgd 
    350  1.20      fvdl #ifdef PORTMAP
    351   1.1       cgd /*
    352   1.1       cgd  * Add a service program to the callout list.
    353   1.1       cgd  * The dispatch routine will be called when a rpc request for this
    354   1.1       cgd  * program number comes in.
    355   1.1       cgd  */
    356   1.1       cgd bool_t
    357  1.31      matt svc_register(SVCXPRT *xprt, u_long prog, u_long vers,
    358  1.31      matt 	void (*dispatch)(struct svc_req *, SVCXPRT *), int protocol)
    359   1.1       cgd {
    360   1.1       cgd 	struct svc_callout *prev;
    361  1.14     lukem 	struct svc_callout *s;
    362   1.1       cgd 
    363  1.17     lukem 	_DIAGASSERT(xprt != NULL);
    364  1.17     lukem 	_DIAGASSERT(dispatch != NULL);
    365  1.17     lukem 
    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 		if (s->sc_dispatch == dispatch)
    369   1.1       cgd 			goto pmap_it;  /* he is registering another xptr */
    370   1.1       cgd 		return (FALSE);
    371   1.1       cgd 	}
    372  1.21  christos 	s = mem_alloc(sizeof(struct svc_callout));
    373  1.21  christos 	if (s == NULL) {
    374   1.1       cgd 		return (FALSE);
    375   1.1       cgd 	}
    376  1.21  christos 	s->sc_prog = (rpcprog_t)prog;
    377  1.21  christos 	s->sc_vers = (rpcvers_t)vers;
    378   1.1       cgd 	s->sc_dispatch = dispatch;
    379   1.1       cgd 	s->sc_next = svc_head;
    380   1.1       cgd 	svc_head = s;
    381   1.1       cgd pmap_it:
    382   1.1       cgd 	/* now register the information with the local binder service */
    383   1.1       cgd 	if (protocol) {
    384   1.1       cgd 		return (pmap_set(prog, vers, protocol, xprt->xp_port));
    385   1.1       cgd 	}
    386   1.1       cgd 	return (TRUE);
    387   1.1       cgd }
    388   1.1       cgd 
    389   1.1       cgd /*
    390   1.1       cgd  * Remove a service program from the callout list.
    391   1.1       cgd  */
    392   1.1       cgd void
    393  1.31      matt svc_unregister(u_long prog, u_long vers)
    394   1.1       cgd {
    395   1.1       cgd 	struct svc_callout *prev;
    396  1.14     lukem 	struct svc_callout *s;
    397   1.1       cgd 
    398  1.21  christos 	if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) ==
    399  1.21  christos 	    NULL)
    400   1.1       cgd 		return;
    401  1.21  christos 	if (prev == NULL) {
    402   1.1       cgd 		svc_head = s->sc_next;
    403   1.1       cgd 	} else {
    404   1.1       cgd 		prev->sc_next = s->sc_next;
    405   1.1       cgd 	}
    406  1.21  christos 	s->sc_next = NULL;
    407  1.15  christos 	mem_free(s, sizeof(struct svc_callout));
    408   1.1       cgd 	/* now unregister the information with the local binder service */
    409   1.1       cgd 	(void)pmap_unset(prog, vers);
    410   1.1       cgd }
    411  1.20      fvdl #endif /* PORTMAP */
    412   1.1       cgd 
    413   1.1       cgd /*
    414   1.1       cgd  * Search the callout list for a program number, return the callout
    415   1.1       cgd  * struct.
    416   1.1       cgd  */
    417   1.1       cgd static struct svc_callout *
    418  1.31      matt svc_find(rpcprog_t prog, rpcvers_t vers, struct svc_callout **prev, char *netid)
    419   1.1       cgd {
    420  1.14     lukem 	struct svc_callout *s, *p;
    421   1.1       cgd 
    422  1.17     lukem 	_DIAGASSERT(prev != NULL);
    423  1.22     lukem 	/* netid is handled below */
    424  1.17     lukem 
    425  1.21  christos 	p = NULL;
    426  1.21  christos 	for (s = svc_head; s != NULL; s = s->sc_next) {
    427  1.20      fvdl 		if (((s->sc_prog == prog) && (s->sc_vers == vers)) &&
    428  1.20      fvdl 		    ((netid == NULL) || (s->sc_netid == NULL) ||
    429  1.20      fvdl 		    (strcmp(netid, s->sc_netid) == 0)))
    430  1.20      fvdl 			break;
    431   1.1       cgd 		p = s;
    432   1.1       cgd 	}
    433   1.1       cgd 	*prev = p;
    434   1.1       cgd 	return (s);
    435   1.1       cgd }
    436   1.1       cgd 
    437   1.1       cgd /* ******************* REPLY GENERATION ROUTINES  ************ */
    438   1.1       cgd 
    439   1.1       cgd /*
    440   1.1       cgd  * Send a reply to an rpc request
    441   1.1       cgd  */
    442   1.1       cgd bool_t
    443  1.31      matt svc_sendreply(SVCXPRT *xprt, xdrproc_t xdr_results, const char *xdr_location)
    444   1.1       cgd {
    445   1.1       cgd 	struct rpc_msg rply;
    446   1.1       cgd 
    447  1.17     lukem 	_DIAGASSERT(xprt != NULL);
    448  1.17     lukem 
    449   1.1       cgd 	rply.rm_direction = REPLY;
    450   1.1       cgd 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    451   1.1       cgd 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    452   1.1       cgd 	rply.acpted_rply.ar_stat = SUCCESS;
    453   1.1       cgd 	rply.acpted_rply.ar_results.where = xdr_location;
    454   1.1       cgd 	rply.acpted_rply.ar_results.proc = xdr_results;
    455   1.1       cgd 	return (SVC_REPLY(xprt, &rply));
    456   1.1       cgd }
    457   1.1       cgd 
    458   1.1       cgd /*
    459   1.1       cgd  * No procedure error reply
    460   1.1       cgd  */
    461   1.1       cgd void
    462  1.31      matt svcerr_noproc(SVCXPRT *xprt)
    463   1.1       cgd {
    464   1.1       cgd 	struct rpc_msg rply;
    465   1.1       cgd 
    466  1.17     lukem 	_DIAGASSERT(xprt != NULL);
    467  1.17     lukem 
    468   1.1       cgd 	rply.rm_direction = REPLY;
    469   1.1       cgd 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    470   1.1       cgd 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    471   1.1       cgd 	rply.acpted_rply.ar_stat = PROC_UNAVAIL;
    472   1.1       cgd 	SVC_REPLY(xprt, &rply);
    473   1.1       cgd }
    474   1.1       cgd 
    475   1.1       cgd /*
    476   1.1       cgd  * Can't decode args error reply
    477   1.1       cgd  */
    478   1.1       cgd void
    479  1.31      matt svcerr_decode(SVCXPRT *xprt)
    480   1.1       cgd {
    481   1.1       cgd 	struct rpc_msg rply;
    482   1.1       cgd 
    483  1.17     lukem 	_DIAGASSERT(xprt != NULL);
    484  1.17     lukem 
    485   1.1       cgd 	rply.rm_direction = REPLY;
    486   1.1       cgd 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    487   1.1       cgd 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    488   1.1       cgd 	rply.acpted_rply.ar_stat = GARBAGE_ARGS;
    489   1.1       cgd 	SVC_REPLY(xprt, &rply);
    490   1.1       cgd }
    491   1.1       cgd 
    492   1.1       cgd /*
    493   1.1       cgd  * Some system error
    494   1.1       cgd  */
    495   1.1       cgd void
    496  1.31      matt svcerr_systemerr(SVCXPRT *xprt)
    497   1.1       cgd {
    498   1.1       cgd 	struct rpc_msg rply;
    499   1.1       cgd 
    500  1.17     lukem 	_DIAGASSERT(xprt != NULL);
    501  1.17     lukem 
    502   1.1       cgd 	rply.rm_direction = REPLY;
    503   1.1       cgd 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    504   1.1       cgd 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    505   1.1       cgd 	rply.acpted_rply.ar_stat = SYSTEM_ERR;
    506   1.1       cgd 	SVC_REPLY(xprt, &rply);
    507   1.1       cgd }
    508   1.1       cgd 
    509  1.20      fvdl #if 0
    510  1.20      fvdl /*
    511  1.20      fvdl  * Tell RPC package to not complain about version errors to the client.	 This
    512  1.20      fvdl  * is useful when revving broadcast protocols that sit on a fixed address.
    513  1.20      fvdl  * There is really one (or should be only one) example of this kind of
    514  1.20      fvdl  * protocol: the portmapper (or rpc binder).
    515  1.20      fvdl  */
    516  1.20      fvdl void
    517  1.31      matt __svc_versquiet_on(SVCXPRT *xprt)
    518  1.20      fvdl {
    519  1.20      fvdl 	u_long	tmp;
    520  1.20      fvdl 
    521  1.22     lukem 	_DIAGASSERT(xprt != NULL);
    522  1.22     lukem 
    523  1.20      fvdl 	tmp = ((u_long) xprt->xp_p3) | SVC_VERSQUIET;
    524  1.20      fvdl 	xprt->xp_p3 = (caddr_t) tmp;
    525  1.20      fvdl }
    526  1.20      fvdl 
    527  1.20      fvdl void
    528  1.31      matt __svc_versquiet_off(SVCXPRT *xprt)
    529  1.20      fvdl {
    530  1.20      fvdl 	u_long	tmp;
    531  1.20      fvdl 
    532  1.22     lukem 	_DIAGASSERT(xprt != NULL);
    533  1.22     lukem 
    534  1.20      fvdl 	tmp = ((u_long) xprt->xp_p3) & ~SVC_VERSQUIET;
    535  1.20      fvdl 	xprt->xp_p3 = (caddr_t) tmp;
    536  1.20      fvdl }
    537  1.20      fvdl 
    538  1.20      fvdl void
    539  1.31      matt svc_versquiet(SVCXPRT *xprt)
    540  1.20      fvdl {
    541  1.20      fvdl 	__svc_versquiet_on(xprt);
    542  1.20      fvdl }
    543  1.20      fvdl 
    544  1.20      fvdl int
    545  1.31      matt __svc_versquiet_get(SVCXPRT *xprt)
    546  1.20      fvdl {
    547  1.22     lukem 
    548  1.22     lukem 	_DIAGASSERT(xprt != NULL);
    549  1.22     lukem 
    550  1.20      fvdl 	return ((int) xprt->xp_p3) & SVC_VERSQUIET;
    551  1.20      fvdl }
    552  1.20      fvdl #endif
    553  1.20      fvdl 
    554   1.1       cgd /*
    555   1.1       cgd  * Authentication error reply
    556   1.1       cgd  */
    557   1.1       cgd void
    558  1.31      matt svcerr_auth(SVCXPRT *xprt, enum auth_stat why)
    559   1.1       cgd {
    560   1.1       cgd 	struct rpc_msg rply;
    561   1.1       cgd 
    562  1.17     lukem 	_DIAGASSERT(xprt != NULL);
    563  1.17     lukem 
    564   1.1       cgd 	rply.rm_direction = REPLY;
    565   1.1       cgd 	rply.rm_reply.rp_stat = MSG_DENIED;
    566   1.1       cgd 	rply.rjcted_rply.rj_stat = AUTH_ERROR;
    567   1.1       cgd 	rply.rjcted_rply.rj_why = why;
    568   1.1       cgd 	SVC_REPLY(xprt, &rply);
    569   1.1       cgd }
    570   1.1       cgd 
    571   1.1       cgd /*
    572   1.1       cgd  * Auth too weak error reply
    573   1.1       cgd  */
    574   1.1       cgd void
    575  1.31      matt svcerr_weakauth(SVCXPRT *xprt)
    576   1.1       cgd {
    577   1.1       cgd 
    578  1.17     lukem 	_DIAGASSERT(xprt != NULL);
    579  1.17     lukem 
    580   1.1       cgd 	svcerr_auth(xprt, AUTH_TOOWEAK);
    581   1.1       cgd }
    582   1.1       cgd 
    583   1.1       cgd /*
    584   1.1       cgd  * Program unavailable error reply
    585   1.1       cgd  */
    586   1.1       cgd void
    587  1.31      matt svcerr_noprog(SVCXPRT *xprt)
    588   1.1       cgd {
    589   1.1       cgd 	struct rpc_msg rply;
    590   1.1       cgd 
    591  1.17     lukem 	_DIAGASSERT(xprt != NULL);
    592  1.17     lukem 
    593   1.1       cgd 	rply.rm_direction = REPLY;
    594   1.1       cgd 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    595   1.1       cgd 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    596   1.1       cgd 	rply.acpted_rply.ar_stat = PROG_UNAVAIL;
    597   1.1       cgd 	SVC_REPLY(xprt, &rply);
    598   1.1       cgd }
    599   1.1       cgd 
    600   1.1       cgd /*
    601   1.1       cgd  * Program version mismatch error reply
    602   1.1       cgd  */
    603   1.1       cgd void
    604  1.31      matt svcerr_progvers(SVCXPRT *xprt, rpcvers_t low_vers, rpcvers_t high_vers)
    605   1.1       cgd {
    606   1.1       cgd 	struct rpc_msg rply;
    607   1.1       cgd 
    608  1.17     lukem 	_DIAGASSERT(xprt != NULL);
    609  1.17     lukem 
    610   1.1       cgd 	rply.rm_direction = REPLY;
    611   1.1       cgd 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
    612   1.1       cgd 	rply.acpted_rply.ar_verf = xprt->xp_verf;
    613   1.1       cgd 	rply.acpted_rply.ar_stat = PROG_MISMATCH;
    614  1.15  christos 	rply.acpted_rply.ar_vers.low = (u_int32_t)low_vers;
    615  1.15  christos 	rply.acpted_rply.ar_vers.high = (u_int32_t)high_vers;
    616   1.1       cgd 	SVC_REPLY(xprt, &rply);
    617   1.1       cgd }
    618   1.1       cgd 
    619   1.1       cgd /* ******************* SERVER INPUT STUFF ******************* */
    620   1.1       cgd 
    621   1.1       cgd /*
    622   1.1       cgd  * Get server side input from some transport.
    623   1.1       cgd  *
    624   1.1       cgd  * Statement of authentication parameters management:
    625   1.1       cgd  * This function owns and manages all authentication parameters, specifically
    626   1.1       cgd  * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
    627   1.1       cgd  * the "cooked" credentials (rqst->rq_clntcred).
    628   1.1       cgd  * However, this function does not know the structure of the cooked
    629   1.1       cgd  * credentials, so it make the following assumptions:
    630   1.1       cgd  *   a) the structure is contiguous (no pointers), and
    631   1.1       cgd  *   b) the cred structure size does not exceed RQCRED_SIZE bytes.
    632   1.1       cgd  * In all events, all three parameters are freed upon exit from this routine.
    633   1.1       cgd  * The storage is trivially management on the call stack in user land, but
    634   1.1       cgd  * is mallocated in kernel land.
    635   1.1       cgd  */
    636   1.1       cgd 
    637   1.1       cgd void
    638  1.31      matt svc_getreq(int rdfds)
    639   1.1       cgd {
    640  1.35  christos 	fd_set *readfds = svc_fdset_copy(NULL);
    641  1.36  christos 	if (readfds == NULL)
    642  1.36  christos 		return;
    643   1.1       cgd 
    644  1.35  christos 	readfds->fds_bits[0] = (unsigned int)rdfds;
    645  1.35  christos 	svc_getreqset(readfds);
    646  1.35  christos 	free(readfds);
    647   1.1       cgd }
    648   1.1       cgd 
    649   1.1       cgd void
    650  1.35  christos svc_getreqset2(fd_set *readfds, int maxsize)
    651   1.1       cgd {
    652  1.29     rmind 	uint32_t mask, *maskp;
    653  1.29     rmind 	int sock, bit, fd;
    654   1.1       cgd 
    655  1.17     lukem 	_DIAGASSERT(readfds != NULL);
    656   1.1       cgd 
    657   1.4       cgd 	maskp = readfds->fds_bits;
    658  1.35  christos 	for (sock = 0; sock < maxsize; sock += NFDBITS) {
    659  1.30      tron 	    for (mask = *maskp++; (bit = ffs((int)mask)) != 0;
    660  1.10  christos 		mask ^= (1 << (bit - 1))) {
    661   1.1       cgd 		/* sock has input waiting */
    662  1.20      fvdl 		fd = sock + bit - 1;
    663  1.20      fvdl 		svc_getreq_common(fd);
    664  1.20      fvdl 	    }
    665  1.20      fvdl 	}
    666  1.20      fvdl }
    667  1.20      fvdl 
    668  1.20      fvdl void
    669  1.35  christos svc_getreqset(fd_set *readfds)
    670  1.35  christos {
    671  1.35  christos 	svc_getreqset2(readfds, FD_SETSIZE);
    672  1.35  christos }
    673  1.35  christos 
    674  1.35  christos void
    675  1.31      matt svc_getreq_common(int fd)
    676  1.20      fvdl {
    677  1.20      fvdl 	SVCXPRT *xprt;
    678  1.20      fvdl 	struct svc_req r;
    679  1.20      fvdl 	struct rpc_msg msg;
    680  1.20      fvdl 	int prog_found;
    681  1.20      fvdl 	rpcvers_t low_vers;
    682  1.20      fvdl 	rpcvers_t high_vers;
    683  1.20      fvdl 	enum xprt_stat stat;
    684  1.20      fvdl 	char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
    685  1.20      fvdl 
    686  1.20      fvdl 	msg.rm_call.cb_cred.oa_base = cred_area;
    687  1.20      fvdl 	msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
    688  1.20      fvdl 	r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
    689  1.20      fvdl 
    690  1.20      fvdl 	rwlock_rdlock(&svc_fd_lock);
    691  1.23      fvdl 	xprt = __svc_xports[fd];
    692  1.20      fvdl 	rwlock_unlock(&svc_fd_lock);
    693  1.20      fvdl 	if (xprt == NULL)
    694  1.20      fvdl 		/* But do we control sock? */
    695  1.20      fvdl 		return;
    696  1.20      fvdl 	/* now receive msgs from xprtprt (support batch calls) */
    697  1.20      fvdl 	do {
    698  1.20      fvdl 		if (SVC_RECV(xprt, &msg)) {
    699  1.20      fvdl 
    700  1.20      fvdl 			/* now find the exported program and call it */
    701  1.20      fvdl 			struct svc_callout *s;
    702  1.20      fvdl 			enum auth_stat why;
    703  1.20      fvdl 
    704  1.20      fvdl 			r.rq_xprt = xprt;
    705  1.20      fvdl 			r.rq_prog = msg.rm_call.cb_prog;
    706  1.20      fvdl 			r.rq_vers = msg.rm_call.cb_vers;
    707  1.20      fvdl 			r.rq_proc = msg.rm_call.cb_proc;
    708  1.20      fvdl 			r.rq_cred = msg.rm_call.cb_cred;
    709  1.20      fvdl 			/* first authenticate the message */
    710  1.20      fvdl 			if ((why = _authenticate(&r, &msg)) != AUTH_OK) {
    711  1.20      fvdl 				svcerr_auth(xprt, why);
    712  1.20      fvdl 				goto call_done;
    713   1.1       cgd 			}
    714  1.20      fvdl 			/* now match message with a registered service*/
    715  1.20      fvdl 			prog_found = FALSE;
    716  1.20      fvdl 			low_vers = (rpcvers_t) -1L;
    717  1.20      fvdl 			high_vers = (rpcvers_t) 0L;
    718  1.21  christos 			for (s = svc_head; s != NULL; s = s->sc_next) {
    719  1.20      fvdl 				if (s->sc_prog == r.rq_prog) {
    720  1.20      fvdl 					if (s->sc_vers == r.rq_vers) {
    721  1.20      fvdl 						(*s->sc_dispatch)(&r, xprt);
    722  1.20      fvdl 						goto call_done;
    723  1.20      fvdl 					}  /* found correct version */
    724  1.20      fvdl 					prog_found = TRUE;
    725  1.20      fvdl 					if (s->sc_vers < low_vers)
    726  1.20      fvdl 						low_vers = s->sc_vers;
    727  1.20      fvdl 					if (s->sc_vers > high_vers)
    728  1.20      fvdl 						high_vers = s->sc_vers;
    729  1.20      fvdl 				}   /* found correct program */
    730   1.1       cgd 			}
    731  1.20      fvdl 			/*
    732  1.20      fvdl 			 * if we got here, the program or version
    733  1.20      fvdl 			 * is not served ...
    734  1.20      fvdl 			 */
    735  1.20      fvdl 			if (prog_found)
    736  1.20      fvdl 				svcerr_progvers(xprt, low_vers, high_vers);
    737  1.20      fvdl 			else
    738  1.20      fvdl 				 svcerr_noprog(xprt);
    739  1.20      fvdl 			/* Fall through to ... */
    740  1.20      fvdl 		}
    741  1.20      fvdl 		/*
    742  1.20      fvdl 		 * Check if the xprt has been disconnected in a
    743  1.20      fvdl 		 * recursive call in the service dispatch routine.
    744  1.20      fvdl 		 * If so, then break.
    745  1.20      fvdl 		 */
    746  1.20      fvdl 		rwlock_rdlock(&svc_fd_lock);
    747  1.23      fvdl 		if (xprt != __svc_xports[fd]) {
    748  1.20      fvdl 			rwlock_unlock(&svc_fd_lock);
    749  1.20      fvdl 			break;
    750  1.20      fvdl 		}
    751  1.20      fvdl 		rwlock_unlock(&svc_fd_lock);
    752  1.20      fvdl call_done:
    753  1.20      fvdl 		if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
    754  1.20      fvdl 			SVC_DESTROY(xprt);
    755  1.20      fvdl 			break;
    756  1.20      fvdl 		}
    757  1.20      fvdl 	} while (stat == XPRT_MOREREQS);
    758  1.20      fvdl }
    759  1.20      fvdl 
    760  1.20      fvdl 
    761  1.20      fvdl void
    762  1.31      matt svc_getreq_poll(struct pollfd *pfdp, int pollretval)
    763  1.20      fvdl {
    764  1.20      fvdl 	int i;
    765  1.20      fvdl 	int fds_found;
    766  1.22     lukem 
    767  1.22     lukem 	_DIAGASSERT(pfdp != NULL);
    768  1.20      fvdl 
    769  1.20      fvdl 	for (i = fds_found = 0; fds_found < pollretval; i++) {
    770  1.21  christos 		struct pollfd *p = &pfdp[i];
    771  1.20      fvdl 
    772  1.20      fvdl 		if (p->revents) {
    773  1.20      fvdl 			/* fd has input waiting */
    774  1.20      fvdl 			fds_found++;
    775  1.20      fvdl 			/*
    776  1.20      fvdl 			 *	We assume that this function is only called
    777  1.20      fvdl 			 *	via someone select()ing from svc_fdset or
    778  1.25  christos 			 *	pollts()ing from svc_pollset[].  Thus it's safe
    779  1.20      fvdl 			 *	to handle the POLLNVAL event by simply turning
    780  1.36  christos 			 *	the corresponding bit off in the fdset.  The
    781  1.20      fvdl 			 *	svc_pollset[] array is derived from svc_fdset
    782  1.20      fvdl 			 *	and so will also be updated eventually.
    783  1.20      fvdl 			 *
    784  1.20      fvdl 			 *	XXX Should we do an xprt_unregister() instead?
    785  1.20      fvdl 			 */
    786  1.20      fvdl 			if (p->revents & POLLNVAL) {
    787  1.20      fvdl 				rwlock_wrlock(&svc_fd_lock);
    788  1.35  christos 				svc_fdset_clr(p->fd);
    789  1.20      fvdl 				rwlock_unlock(&svc_fd_lock);
    790  1.20      fvdl 			} else
    791  1.20      fvdl 				svc_getreq_common(p->fd);
    792  1.20      fvdl 		}
    793   1.1       cgd 	}
    794  1.23      fvdl }
    795  1.23      fvdl 
    796  1.23      fvdl bool_t
    797  1.23      fvdl rpc_control(int what, void *arg)
    798  1.23      fvdl {
    799  1.23      fvdl 	int val;
    800  1.23      fvdl 
    801  1.23      fvdl 	switch (what) {
    802  1.23      fvdl 	case RPC_SVC_CONNMAXREC_SET:
    803  1.23      fvdl 		val = *(int *)arg;
    804  1.23      fvdl 		if (val <= 0)
    805  1.23      fvdl 			return FALSE;
    806  1.23      fvdl 		__svc_maxrec = val;
    807  1.23      fvdl 		return TRUE;
    808  1.23      fvdl 	case RPC_SVC_CONNMAXREC_GET:
    809  1.23      fvdl 		*(int *)arg = __svc_maxrec;
    810  1.23      fvdl 		return TRUE;
    811  1.23      fvdl 	default:
    812  1.23      fvdl 		break;
    813  1.23      fvdl 	}
    814  1.23      fvdl 	return FALSE;
    815   1.1       cgd }
    816