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