1 /* $NetBSD: rpc_generic.c,v 1.4 2000/09/28 09:07:04 kleink Exp $ */
4 * Copyright (c) 2009, Sun Microsystems, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * - Neither the name of Sun Microsystems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
34 /* #pragma ident "@(#)rpc_generic.c 1.17 94/04/24 SMI" */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 * rpc_generic.c, Miscl routines for RPC.
43 #include "opt_inet6.h"
45 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
49 #include <sys/module.h>
51 #include <sys/protosw.h>
53 #include <sys/systm.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/syslog.h>
61 #include <rpc/nettype.h>
62 #include <rpc/rpcsec_gss.h>
64 #include <rpc/rpc_com.h>
66 extern u_long sb_max_adj; /* not defined in socketvar.h */
68 #if __FreeBSD_version < 700000
69 #define strrchr rindex
72 /* Provide an entry point hook for the rpcsec_gss module. */
73 struct rpc_gss_entries rpc_gss_entries;
76 NCONF_HANDLE *nhandle;
77 int nflag; /* Whether NETPATH or NETCONFIG */
81 static const struct _rpcnettype {
85 { "netpath", _RPC_NETPATH },
86 { "visible", _RPC_VISIBLE },
87 { "circuit_v", _RPC_CIRCUIT_V },
88 { "datagram_v", _RPC_DATAGRAM_V },
89 { "circuit_n", _RPC_CIRCUIT_N },
90 { "datagram_n", _RPC_DATAGRAM_N },
102 static const struct netid_af na_cvt[] = {
103 { "udp", AF_INET, IPPROTO_UDP },
104 { "tcp", AF_INET, IPPROTO_TCP },
106 { "udp6", AF_INET6, IPPROTO_UDP },
107 { "tcp6", AF_INET6, IPPROTO_TCP },
109 { "local", AF_LOCAL, 0 }
112 struct rpc_createerr rpc_createerr;
115 * Find the appropriate buffer size
119 __rpc_get_t_size(int af, int proto, int size)
125 defsize = 64 * 1024; /* XXX */
128 defsize = UDPMSGSIZE;
131 defsize = RPC_MAXDATASIZE;
137 /* Check whether the value is within the upper max limit */
138 return (size > sb_max_adj ? (u_int)sb_max_adj : (u_int)size);
142 * Find the appropriate address buffer size
150 return sizeof (struct sockaddr_in);
153 return sizeof (struct sockaddr_in6);
156 return sizeof (struct sockaddr_un);
160 return ((u_int)RPC_MAXADDRSIZE);
166 * Used to ping the NULL procedure for clnt handle.
167 * Returns NULL if fails, else a non-NULL pointer.
173 struct timeval TIMEOUT = {25, 0};
175 if (clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void, NULL,
176 (xdrproc_t) xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) {
179 return ((void *) clnt);
185 __rpc_socket2sockinfo(struct socket *so, struct __rpc_sockinfo *sip)
193 CURVNET_SET(so->so_vnet);
194 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
199 sip->si_alen = sa->sa_len;
200 family = sa->sa_family;
203 opt.sopt_dir = SOPT_GET;
204 opt.sopt_level = SOL_SOCKET;
205 opt.sopt_name = SO_TYPE;
206 opt.sopt_val = &type;
207 opt.sopt_valsize = sizeof type;
209 error = sogetopt(so, &opt);
214 if (family != AF_LOCAL) {
215 if (type == SOCK_STREAM)
217 else if (type == SOCK_DGRAM)
225 sip->si_proto = proto;
226 sip->si_socktype = type;
232 * Linear search, but the number of entries is small.
235 __rpc_nconf2sockinfo(const struct netconfig *nconf, struct __rpc_sockinfo *sip)
239 for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++)
240 if (strcmp(na_cvt[i].netid, nconf->nc_netid) == 0 || (
241 strcmp(nconf->nc_netid, "unix") == 0 &&
242 strcmp(na_cvt[i].netid, "local") == 0)) {
243 sip->si_af = na_cvt[i].af;
244 sip->si_proto = na_cvt[i].protocol;
246 __rpc_seman2socktype((int)nconf->nc_semantics);
247 if (sip->si_socktype == -1)
249 sip->si_alen = __rpc_get_a_size(sip->si_af);
257 __rpc_nconf2socket(const struct netconfig *nconf)
259 struct __rpc_sockinfo si;
263 if (!__rpc_nconf2sockinfo(nconf, &si))
267 error = socreate(si.si_af, &so, si.si_socktype, si.si_proto,
268 curthread->td_ucred, curthread);
277 taddr2uaddr(const struct netconfig *nconf, const struct netbuf *nbuf)
279 struct __rpc_sockinfo si;
281 if (!__rpc_nconf2sockinfo(nconf, &si))
283 return __rpc_taddr2uaddr_af(si.si_af, nbuf);
287 uaddr2taddr(const struct netconfig *nconf, const char *uaddr)
289 struct __rpc_sockinfo si;
291 if (!__rpc_nconf2sockinfo(nconf, &si))
293 return __rpc_uaddr2taddr_af(si.si_af, uaddr);
297 __rpc_taddr2uaddr_af(int af, const struct netbuf *nbuf)
301 struct sockaddr_in *sin;
302 struct sockaddr_un *sun;
303 char namebuf[INET_ADDRSTRLEN];
305 struct sockaddr_in6 *sin6;
306 char namebuf6[INET6_ADDRSTRLEN];
310 sbuf_new(&sb, NULL, 0, SBUF_AUTOEXTEND);
314 if (nbuf->len < sizeof(*sin))
317 if (inet_ntop(af, &sin->sin_addr, namebuf, sizeof namebuf)
320 port = ntohs(sin->sin_port);
321 if (sbuf_printf(&sb, "%s.%u.%u", namebuf,
322 ((uint32_t)port) >> 8,
328 if (nbuf->len < sizeof(*sin6))
331 if (inet_ntop(af, &sin6->sin6_addr, namebuf6, sizeof namebuf6)
334 port = ntohs(sin6->sin6_port);
335 if (sbuf_printf(&sb, "%s.%u.%u", namebuf6,
336 ((uint32_t)port) >> 8,
343 if (sbuf_printf(&sb, "%.*s", (int)(sun->sun_len -
344 offsetof(struct sockaddr_un, sun_path)),
353 ret = strdup(sbuf_data(&sb), M_RPC);
360 __rpc_uaddr2taddr_af(int af, const char *uaddr)
362 struct netbuf *ret = NULL;
364 unsigned port, portlo, porthi;
365 struct sockaddr_in *sin;
367 struct sockaddr_in6 *sin6;
369 struct sockaddr_un *sun;
377 addrstr = strdup(uaddr, M_RPC);
382 * AF_LOCAL addresses are expected to be absolute
383 * pathnames, anything else will be AF_INET or AF_INET6.
385 if (*addrstr != '/') {
386 p = strrchr(addrstr, '.');
389 portlo = (unsigned)strtol(p + 1, NULL, 10);
392 p = strrchr(addrstr, '.');
395 porthi = (unsigned)strtol(p + 1, NULL, 10);
397 port = (porthi << 8) | portlo;
400 ret = (struct netbuf *)malloc(sizeof *ret, M_RPC, M_WAITOK);
404 sin = (struct sockaddr_in *)malloc(sizeof *sin, M_RPC,
406 memset(sin, 0, sizeof *sin);
407 sin->sin_family = AF_INET;
408 sin->sin_port = htons(port);
409 if (inet_pton(AF_INET, addrstr, &sin->sin_addr) <= 0) {
415 sin->sin_len = ret->maxlen = ret->len = sizeof *sin;
420 sin6 = (struct sockaddr_in6 *)malloc(sizeof *sin6, M_RPC,
422 memset(sin6, 0, sizeof *sin6);
423 sin6->sin6_family = AF_INET6;
424 sin6->sin6_port = htons(port);
425 if (inet_pton(AF_INET6, addrstr, &sin6->sin6_addr) <= 0) {
431 sin6->sin6_len = ret->maxlen = ret->len = sizeof *sin6;
436 sun = (struct sockaddr_un *)malloc(sizeof *sun, M_RPC,
438 memset(sun, 0, sizeof *sun);
439 sun->sun_family = AF_LOCAL;
440 strncpy(sun->sun_path, addrstr, sizeof(sun->sun_path) - 1);
441 ret->len = ret->maxlen = sun->sun_len = SUN_LEN(sun);
448 free(addrstr, M_RPC);
453 __rpc_seman2socktype(int semantics)
458 case NC_TPI_COTS_ORD:
470 __rpc_socktype2seman(int socktype)
476 return NC_TPI_COTS_ORD;
487 * Returns the type of the network as defined in <rpc/nettype.h>
488 * If nettype is NULL, it defaults to NETPATH.
491 getnettype(const char *nettype)
495 if ((nettype == NULL) || (nettype[0] == 0)) {
496 return (_RPC_NETPATH); /* Default */
500 nettype = strlocase(nettype);
502 for (i = 0; _rpctypelist[i].name; i++)
503 if (strcasecmp(nettype, _rpctypelist[i].name) == 0) {
504 return (_rpctypelist[i].type);
506 return (_rpctypelist[i].type);
510 * For the given nettype (tcp or udp only), return the first structure found.
511 * This should be freed by calling freenetconfigent()
514 __rpc_getconfip(const char *nettype)
517 static char *netid_tcp = (char *) NULL;
518 static char *netid_udp = (char *) NULL;
519 struct netconfig *dummy;
521 if (!netid_udp && !netid_tcp) {
522 struct netconfig *nconf;
525 if (!(confighandle = setnetconfig())) {
526 log(LOG_ERR, "rpc: failed to open " NETCONFIG);
529 while ((nconf = getnetconfig(confighandle)) != NULL) {
530 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
531 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
532 netid_tcp = strdup(nconf->nc_netid,
535 if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
536 netid_udp = strdup(nconf->nc_netid,
541 endnetconfig(confighandle);
543 if (strcmp(nettype, "udp") == 0)
545 else if (strcmp(nettype, "tcp") == 0)
550 if ((netid == NULL) || (netid[0] == 0)) {
553 dummy = getnetconfigent(netid);
558 * Returns the type of the nettype, which should then be used with
561 * For simplicity in the kernel, we don't support the NETPATH
562 * environment variable. We behave as userland would then NETPATH is
563 * unset, i.e. iterate over all visible entries in netconfig.
566 __rpc_setconf(nettype)
569 struct handle *handle;
571 handle = (struct handle *) malloc(sizeof (struct handle),
573 switch (handle->nettype = getnettype(nettype)) {
576 case _RPC_DATAGRAM_N:
577 if (!(handle->nhandle = setnetconfig()))
579 handle->nflag = TRUE;
583 case _RPC_DATAGRAM_V:
586 if (!(handle->nhandle = setnetconfig())) {
587 log(LOG_ERR, "rpc: failed to open " NETCONFIG);
590 handle->nflag = FALSE;
604 * Returns the next netconfig struct for the given "net" type.
605 * __rpc_setconf() should have been called previously.
608 __rpc_getconf(void *vhandle)
610 struct handle *handle;
611 struct netconfig *nconf;
613 handle = (struct handle *)vhandle;
614 if (handle == NULL) {
619 nconf = getnetconfig(handle->nhandle);
620 if (nconf && !(nconf->nc_flag & NC_VISIBLE))
623 nconf = getnetconfig(handle->nhandle);
627 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
628 (nconf->nc_semantics != NC_TPI_COTS) &&
629 (nconf->nc_semantics != NC_TPI_COTS_ORD))
631 switch (handle->nettype) {
633 if (!(nconf->nc_flag & NC_VISIBLE))
636 case _RPC_NETPATH: /* Be happy */
639 if (!(nconf->nc_flag & NC_VISIBLE))
643 if ((nconf->nc_semantics != NC_TPI_COTS) &&
644 (nconf->nc_semantics != NC_TPI_COTS_ORD))
647 case _RPC_DATAGRAM_V:
648 if (!(nconf->nc_flag & NC_VISIBLE))
651 case _RPC_DATAGRAM_N:
652 if (nconf->nc_semantics != NC_TPI_CLTS)
656 if (((nconf->nc_semantics != NC_TPI_COTS) &&
657 (nconf->nc_semantics != NC_TPI_COTS_ORD)) ||
658 (strcmp(nconf->nc_protofmly, NC_INET)
660 && strcmp(nconf->nc_protofmly, NC_INET6))
665 strcmp(nconf->nc_proto, NC_TCP))
669 if ((nconf->nc_semantics != NC_TPI_CLTS) ||
670 (strcmp(nconf->nc_protofmly, NC_INET)
672 && strcmp(nconf->nc_protofmly, NC_INET6))
677 strcmp(nconf->nc_proto, NC_UDP))
687 __rpc_endconf(vhandle)
690 struct handle *handle;
692 handle = (struct handle *) vhandle;
693 if (handle == NULL) {
696 endnetconfig(handle->nhandle);
701 __rpc_sockisbound(struct socket *so)
706 CURVNET_SET(so->so_vnet);
707 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
712 switch (sa->sa_family) {
714 bound = (((struct sockaddr_in *) sa)->sin_port != 0);
718 bound = (((struct sockaddr_in6 *) sa)->sin6_port != 0);
723 bound = (((struct sockaddr_un *) sa)->sun_path[0] != '\0');
736 * Implement XDR-style API for RPC call.
740 CLIENT *cl, /* client handle */
741 struct rpc_callextra *ext, /* call metadata */
742 rpcproc_t proc, /* procedure number */
743 xdrproc_t xargs, /* xdr routine for args */
744 void *argsp, /* pointer to args */
745 xdrproc_t xresults, /* xdr routine for results */
746 void *resultsp, /* pointer to results */
747 struct timeval utimeout) /* seconds to wait before giving up */
754 mreq = m_getcl(M_WAITOK, MT_DATA, 0);
756 xdrmbuf_create(&xdrs, mreq, XDR_ENCODE);
757 if (!xargs(&xdrs, argsp)) {
759 return (RPC_CANTENCODEARGS);
763 stat = CLNT_CALL_MBUF(cl, ext, proc, mreq, &mrep, utimeout);
766 if (stat == RPC_SUCCESS) {
767 xdrmbuf_create(&xdrs, mrep, XDR_DECODE);
768 if (!xresults(&xdrs, resultsp)) {
770 return (RPC_CANTDECODERES);
779 * Bind a socket to a privileged IP port
782 bindresvport(struct socket *so, struct sockaddr *sa)
785 bool_t freesa = FALSE;
786 struct sockaddr_in *sin;
788 struct sockaddr_in6 *sin6;
791 int proto, portrange, portlow;
796 CURVNET_SET(so->so_vnet);
797 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
804 memset(sa, 0, sa->sa_len);
813 portrange = IP_PORTRANGE;
814 portlow = IP_PORTRANGE_LOW;
815 sin = (struct sockaddr_in *)sa;
816 portp = &sin->sin_port;
820 proto = IPPROTO_IPV6;
821 portrange = IPV6_PORTRANGE;
822 portlow = IPV6_PORTRANGE_LOW;
823 sin6 = (struct sockaddr_in6 *)sa;
824 portp = &sin6->sin6_port;
828 return (EPFNOSUPPORT);
835 bzero(&opt, sizeof(opt));
836 opt.sopt_dir = SOPT_GET;
837 opt.sopt_level = proto;
838 opt.sopt_name = portrange;
840 opt.sopt_valsize = sizeof(old);
841 error = sogetopt(so, &opt);
846 opt.sopt_dir = SOPT_SET;
847 opt.sopt_val = &portlow;
848 error = sosetopt(so, &opt);
853 error = sobind(so, sa, curthread);
857 opt.sopt_dir = SOPT_SET;
873 krpc_modevent(module_t mod, int type, void *data)
878 static moduledata_t krpc_mod = {
883 DECLARE_MODULE(krpc, krpc_mod, SI_SUB_VFS, SI_ORDER_ANY);
885 /* So that loader and kldload(2) can find us, wherever we are.. */
886 MODULE_VERSION(krpc, 1);