1 /* $NetBSD: rpc_generic.c,v 1.4 2000/09/28 09:07:04 kleink Exp $ */
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
35 /* #pragma ident "@(#)rpc_generic.c 1.17 94/04/24 SMI" */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * rpc_generic.c, Miscl routines for RPC.
44 #include "opt_inet6.h"
46 #include <sys/param.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
50 #include <sys/module.h>
52 #include <sys/protosw.h>
54 #include <sys/systm.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/syslog.h>
62 #include <rpc/nettype.h>
63 #include <rpc/rpcsec_gss.h>
65 #include <rpc/rpc_com.h>
67 extern u_long sb_max_adj; /* not defined in socketvar.h */
69 #if __FreeBSD_version < 700000
70 #define strrchr rindex
73 /* Provide an entry point hook for the rpcsec_gss module. */
74 struct rpc_gss_entries rpc_gss_entries;
77 NCONF_HANDLE *nhandle;
78 int nflag; /* Whether NETPATH or NETCONFIG */
82 static const struct _rpcnettype {
86 { "netpath", _RPC_NETPATH },
87 { "visible", _RPC_VISIBLE },
88 { "circuit_v", _RPC_CIRCUIT_V },
89 { "datagram_v", _RPC_DATAGRAM_V },
90 { "circuit_n", _RPC_CIRCUIT_N },
91 { "datagram_n", _RPC_DATAGRAM_N },
103 static const struct netid_af na_cvt[] = {
104 { "udp", AF_INET, IPPROTO_UDP },
105 { "tcp", AF_INET, IPPROTO_TCP },
107 { "udp6", AF_INET6, IPPROTO_UDP },
108 { "tcp6", AF_INET6, IPPROTO_TCP },
110 { "local", AF_LOCAL, 0 }
113 struct rpc_createerr rpc_createerr;
116 * Find the appropriate buffer size
120 __rpc_get_t_size(int af, int proto, int size)
126 defsize = 64 * 1024; /* XXX */
129 defsize = UDPMSGSIZE;
132 defsize = RPC_MAXDATASIZE;
138 /* Check whether the value is within the upper max limit */
139 return (size > sb_max_adj ? (u_int)sb_max_adj : (u_int)size);
143 * Find the appropriate address buffer size
151 return sizeof (struct sockaddr_in);
154 return sizeof (struct sockaddr_in6);
157 return sizeof (struct sockaddr_un);
161 return ((u_int)RPC_MAXADDRSIZE);
167 * Used to ping the NULL procedure for clnt handle.
168 * Returns NULL if fails, else a non-NULL pointer.
174 struct timeval TIMEOUT = {25, 0};
176 if (clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void, NULL,
177 (xdrproc_t) xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) {
180 return ((void *) clnt);
186 __rpc_socket2sockinfo(struct socket *so, struct __rpc_sockinfo *sip)
194 CURVNET_SET(so->so_vnet);
195 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
200 sip->si_alen = sa->sa_len;
201 family = sa->sa_family;
204 opt.sopt_dir = SOPT_GET;
205 opt.sopt_level = SOL_SOCKET;
206 opt.sopt_name = SO_TYPE;
207 opt.sopt_val = &type;
208 opt.sopt_valsize = sizeof type;
210 error = sogetopt(so, &opt);
215 if (family != AF_LOCAL) {
216 if (type == SOCK_STREAM)
218 else if (type == SOCK_DGRAM)
226 sip->si_proto = proto;
227 sip->si_socktype = type;
233 * Linear search, but the number of entries is small.
236 __rpc_nconf2sockinfo(const struct netconfig *nconf, struct __rpc_sockinfo *sip)
240 for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++)
241 if (strcmp(na_cvt[i].netid, nconf->nc_netid) == 0 || (
242 strcmp(nconf->nc_netid, "unix") == 0 &&
243 strcmp(na_cvt[i].netid, "local") == 0)) {
244 sip->si_af = na_cvt[i].af;
245 sip->si_proto = na_cvt[i].protocol;
247 __rpc_seman2socktype((int)nconf->nc_semantics);
248 if (sip->si_socktype == -1)
250 sip->si_alen = __rpc_get_a_size(sip->si_af);
258 __rpc_nconf2socket(const struct netconfig *nconf)
260 struct __rpc_sockinfo si;
264 if (!__rpc_nconf2sockinfo(nconf, &si))
268 error = socreate(si.si_af, &so, si.si_socktype, si.si_proto,
269 curthread->td_ucred, curthread);
278 taddr2uaddr(const struct netconfig *nconf, const struct netbuf *nbuf)
280 struct __rpc_sockinfo si;
282 if (!__rpc_nconf2sockinfo(nconf, &si))
284 return __rpc_taddr2uaddr_af(si.si_af, nbuf);
288 uaddr2taddr(const struct netconfig *nconf, const char *uaddr)
290 struct __rpc_sockinfo si;
292 if (!__rpc_nconf2sockinfo(nconf, &si))
294 return __rpc_uaddr2taddr_af(si.si_af, uaddr);
298 __rpc_taddr2uaddr_af(int af, const struct netbuf *nbuf)
302 struct sockaddr_in *sin;
303 struct sockaddr_un *sun;
304 char namebuf[INET_ADDRSTRLEN];
306 struct sockaddr_in6 *sin6;
307 char namebuf6[INET6_ADDRSTRLEN];
311 sbuf_new(&sb, NULL, 0, SBUF_AUTOEXTEND);
316 if (inet_ntop(af, &sin->sin_addr, namebuf, sizeof namebuf)
319 port = ntohs(sin->sin_port);
320 if (sbuf_printf(&sb, "%s.%u.%u", namebuf,
321 ((uint32_t)port) >> 8,
328 if (inet_ntop(af, &sin6->sin6_addr, namebuf6, sizeof namebuf6)
331 port = ntohs(sin6->sin6_port);
332 if (sbuf_printf(&sb, "%s.%u.%u", namebuf6,
333 ((uint32_t)port) >> 8,
340 if (sbuf_printf(&sb, "%.*s", (int)(sun->sun_len -
341 offsetof(struct sockaddr_un, sun_path)),
350 ret = strdup(sbuf_data(&sb), M_RPC);
357 __rpc_uaddr2taddr_af(int af, const char *uaddr)
359 struct netbuf *ret = NULL;
361 unsigned port, portlo, porthi;
362 struct sockaddr_in *sin;
364 struct sockaddr_in6 *sin6;
366 struct sockaddr_un *sun;
370 addrstr = strdup(uaddr, M_RPC);
375 * AF_LOCAL addresses are expected to be absolute
376 * pathnames, anything else will be AF_INET or AF_INET6.
378 if (*addrstr != '/') {
379 p = strrchr(addrstr, '.');
382 portlo = (unsigned)strtol(p + 1, NULL, 10);
385 p = strrchr(addrstr, '.');
388 porthi = (unsigned)strtol(p + 1, NULL, 10);
390 port = (porthi << 8) | portlo;
393 ret = (struct netbuf *)malloc(sizeof *ret, M_RPC, M_WAITOK);
399 sin = (struct sockaddr_in *)malloc(sizeof *sin, M_RPC,
403 memset(sin, 0, sizeof *sin);
404 sin->sin_family = AF_INET;
405 sin->sin_port = htons(port);
406 if (inet_pton(AF_INET, addrstr, &sin->sin_addr) <= 0) {
412 sin->sin_len = ret->maxlen = ret->len = sizeof *sin;
417 sin6 = (struct sockaddr_in6 *)malloc(sizeof *sin6, M_RPC,
421 memset(sin6, 0, sizeof *sin6);
422 sin6->sin6_family = AF_INET6;
423 sin6->sin6_port = htons(port);
424 if (inet_pton(AF_INET6, addrstr, &sin6->sin6_addr) <= 0) {
430 sin6->sin6_len = ret->maxlen = ret->len = sizeof *sin6;
435 sun = (struct sockaddr_un *)malloc(sizeof *sun, M_RPC,
439 memset(sun, 0, sizeof *sun);
440 sun->sun_family = AF_LOCAL;
441 strncpy(sun->sun_path, addrstr, sizeof(sun->sun_path) - 1);
442 ret->len = ret->maxlen = sun->sun_len = SUN_LEN(sun);
449 free(addrstr, M_RPC);
454 __rpc_seman2socktype(int semantics)
459 case NC_TPI_COTS_ORD:
471 __rpc_socktype2seman(int socktype)
477 return NC_TPI_COTS_ORD;
488 * Returns the type of the network as defined in <rpc/nettype.h>
489 * If nettype is NULL, it defaults to NETPATH.
492 getnettype(const char *nettype)
496 if ((nettype == NULL) || (nettype[0] == 0)) {
497 return (_RPC_NETPATH); /* Default */
501 nettype = strlocase(nettype);
503 for (i = 0; _rpctypelist[i].name; i++)
504 if (strcasecmp(nettype, _rpctypelist[i].name) == 0) {
505 return (_rpctypelist[i].type);
507 return (_rpctypelist[i].type);
511 * For the given nettype (tcp or udp only), return the first structure found.
512 * This should be freed by calling freenetconfigent()
515 __rpc_getconfip(const char *nettype)
518 static char *netid_tcp = (char *) NULL;
519 static char *netid_udp = (char *) NULL;
520 struct netconfig *dummy;
522 if (!netid_udp && !netid_tcp) {
523 struct netconfig *nconf;
526 if (!(confighandle = setnetconfig())) {
527 log(LOG_ERR, "rpc: failed to open " NETCONFIG);
530 while ((nconf = getnetconfig(confighandle)) != NULL) {
531 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
532 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
533 netid_tcp = strdup(nconf->nc_netid,
536 if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
537 netid_udp = strdup(nconf->nc_netid,
542 endnetconfig(confighandle);
544 if (strcmp(nettype, "udp") == 0)
546 else if (strcmp(nettype, "tcp") == 0)
551 if ((netid == NULL) || (netid[0] == 0)) {
554 dummy = getnetconfigent(netid);
559 * Returns the type of the nettype, which should then be used with
562 * For simplicity in the kernel, we don't support the NETPATH
563 * environment variable. We behave as userland would then NETPATH is
564 * unset, i.e. iterate over all visible entries in netconfig.
567 __rpc_setconf(nettype)
570 struct handle *handle;
572 handle = (struct handle *) malloc(sizeof (struct handle),
574 switch (handle->nettype = getnettype(nettype)) {
577 case _RPC_DATAGRAM_N:
578 if (!(handle->nhandle = setnetconfig()))
580 handle->nflag = TRUE;
584 case _RPC_DATAGRAM_V:
587 if (!(handle->nhandle = setnetconfig())) {
588 log(LOG_ERR, "rpc: failed to open " NETCONFIG);
591 handle->nflag = FALSE;
605 * Returns the next netconfig struct for the given "net" type.
606 * __rpc_setconf() should have been called previously.
609 __rpc_getconf(void *vhandle)
611 struct handle *handle;
612 struct netconfig *nconf;
614 handle = (struct handle *)vhandle;
615 if (handle == NULL) {
620 nconf = getnetconfig(handle->nhandle);
621 if (nconf && !(nconf->nc_flag & NC_VISIBLE))
624 nconf = getnetconfig(handle->nhandle);
628 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
629 (nconf->nc_semantics != NC_TPI_COTS) &&
630 (nconf->nc_semantics != NC_TPI_COTS_ORD))
632 switch (handle->nettype) {
634 if (!(nconf->nc_flag & NC_VISIBLE))
637 case _RPC_NETPATH: /* Be happy */
640 if (!(nconf->nc_flag & NC_VISIBLE))
644 if ((nconf->nc_semantics != NC_TPI_COTS) &&
645 (nconf->nc_semantics != NC_TPI_COTS_ORD))
648 case _RPC_DATAGRAM_V:
649 if (!(nconf->nc_flag & NC_VISIBLE))
652 case _RPC_DATAGRAM_N:
653 if (nconf->nc_semantics != NC_TPI_CLTS)
657 if (((nconf->nc_semantics != NC_TPI_COTS) &&
658 (nconf->nc_semantics != NC_TPI_COTS_ORD)) ||
659 (strcmp(nconf->nc_protofmly, NC_INET)
661 && strcmp(nconf->nc_protofmly, NC_INET6))
666 strcmp(nconf->nc_proto, NC_TCP))
670 if ((nconf->nc_semantics != NC_TPI_CLTS) ||
671 (strcmp(nconf->nc_protofmly, NC_INET)
673 && strcmp(nconf->nc_protofmly, NC_INET6))
678 strcmp(nconf->nc_proto, NC_UDP))
688 __rpc_endconf(vhandle)
691 struct handle *handle;
693 handle = (struct handle *) vhandle;
694 if (handle == NULL) {
697 endnetconfig(handle->nhandle);
702 __rpc_sockisbound(struct socket *so)
707 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
711 switch (sa->sa_family) {
713 bound = (((struct sockaddr_in *) sa)->sin_port != 0);
717 bound = (((struct sockaddr_in6 *) sa)->sin6_port != 0);
722 bound = (((struct sockaddr_un *) sa)->sun_path[0] != '\0');
735 * Implement XDR-style API for RPC call.
739 CLIENT *cl, /* client handle */
740 struct rpc_callextra *ext, /* call metadata */
741 rpcproc_t proc, /* procedure number */
742 xdrproc_t xargs, /* xdr routine for args */
743 void *argsp, /* pointer to args */
744 xdrproc_t xresults, /* xdr routine for results */
745 void *resultsp, /* pointer to results */
746 struct timeval utimeout) /* seconds to wait before giving up */
753 MGET(mreq, M_WAIT, MT_DATA);
754 MCLGET(mreq, M_WAIT);
757 xdrmbuf_create(&xdrs, mreq, XDR_ENCODE);
758 if (!xargs(&xdrs, argsp)) {
760 return (RPC_CANTENCODEARGS);
764 stat = CLNT_CALL_MBUF(cl, ext, proc, mreq, &mrep, utimeout);
767 if (stat == RPC_SUCCESS) {
768 xdrmbuf_create(&xdrs, mrep, XDR_DECODE);
769 if (!xresults(&xdrs, resultsp)) {
771 return (RPC_CANTDECODERES);
780 * Bind a socket to a privileged IP port
783 bindresvport(struct socket *so, struct sockaddr *sa)
786 bool_t freesa = FALSE;
787 struct sockaddr_in *sin;
789 struct sockaddr_in6 *sin6;
792 int proto, portrange, portlow;
797 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
803 memset(sa, 0, sa->sa_len);
812 portrange = IP_PORTRANGE;
813 portlow = IP_PORTRANGE_LOW;
814 sin = (struct sockaddr_in *)sa;
815 portp = &sin->sin_port;
819 proto = IPPROTO_IPV6;
820 portrange = IPV6_PORTRANGE;
821 portlow = IPV6_PORTRANGE_LOW;
822 sin6 = (struct sockaddr_in6 *)sa;
823 portp = &sin6->sin6_port;
827 return (EPFNOSUPPORT);
834 bzero(&opt, sizeof(opt));
835 opt.sopt_dir = SOPT_GET;
836 opt.sopt_level = proto;
837 opt.sopt_name = portrange;
839 opt.sopt_valsize = sizeof(old);
840 error = sogetopt(so, &opt);
845 opt.sopt_dir = SOPT_SET;
846 opt.sopt_val = &portlow;
847 error = sosetopt(so, &opt);
852 error = sobind(so, sa, curthread);
856 opt.sopt_dir = SOPT_SET;
872 krpc_modevent(module_t mod, int type, void *data)
877 static moduledata_t krpc_mod = {
882 DECLARE_MODULE(krpc, krpc_mod, SI_SUB_VFS, SI_ORDER_ANY);
884 /* So that loader and kldload(2) can find us, wherever we are.. */
885 MODULE_VERSION(krpc, 1);