1 /* $NetBSD: rpc_generic.c,v 1.4 2000/09/28 09:07:04 kleink Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 2009, Sun Microsystems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * - Neither the name of Sun Microsystems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
36 /* #pragma ident "@(#)rpc_generic.c 1.17 94/04/24 SMI" */
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 * rpc_generic.c, Miscl routines for RPC.
45 #include "opt_inet6.h"
47 #include <sys/param.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
51 #include <sys/module.h>
53 #include <sys/protosw.h>
55 #include <sys/systm.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/syslog.h>
63 #include <rpc/nettype.h>
64 #include <rpc/rpcsec_gss.h>
65 #include <rpc/rpcsec_tls.h>
67 #include <rpc/rpc_com.h>
72 #include <vm/vm_param.h>
74 extern u_long sb_max_adj; /* not defined in socketvar.h */
76 /* Provide an entry point hook for the rpcsec_gss module. */
77 struct rpc_gss_entries rpc_gss_entries;
80 NCONF_HANDLE *nhandle;
81 int nflag; /* Whether NETPATH or NETCONFIG */
85 static const struct _rpcnettype {
89 { "netpath", _RPC_NETPATH },
90 { "visible", _RPC_VISIBLE },
91 { "circuit_v", _RPC_CIRCUIT_V },
92 { "datagram_v", _RPC_DATAGRAM_V },
93 { "circuit_n", _RPC_CIRCUIT_N },
94 { "datagram_n", _RPC_DATAGRAM_N },
106 static const struct netid_af na_cvt[] = {
107 { "udp", AF_INET, IPPROTO_UDP },
108 { "tcp", AF_INET, IPPROTO_TCP },
110 { "udp6", AF_INET6, IPPROTO_UDP },
111 { "tcp6", AF_INET6, IPPROTO_TCP },
113 { "local", AF_LOCAL, 0 }
116 struct rpc_createerr rpc_createerr;
119 * Find the appropriate buffer size
123 __rpc_get_t_size(int af, int proto, int size)
129 defsize = 64 * 1024; /* XXX */
132 defsize = UDPMSGSIZE;
135 defsize = RPC_MAXDATASIZE;
141 /* Check whether the value is within the upper max limit */
142 return (size > sb_max_adj ? (u_int)sb_max_adj : (u_int)size);
146 * Find the appropriate address buffer size
149 __rpc_get_a_size(int af)
153 return sizeof (struct sockaddr_in);
156 return sizeof (struct sockaddr_in6);
159 return sizeof (struct sockaddr_un);
163 return ((u_int)RPC_MAXADDRSIZE);
169 * Used to ping the NULL procedure for clnt handle.
170 * Returns NULL if fails, else a non-NULL pointer.
176 struct timeval TIMEOUT = {25, 0};
178 if (clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void, NULL,
179 (xdrproc_t) xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) {
182 return ((void *) clnt);
188 __rpc_socket2sockinfo(struct socket *so, struct __rpc_sockinfo *sip)
196 CURVNET_SET(so->so_vnet);
197 error = so->so_proto->pr_sockaddr(so, &sa);
202 sip->si_alen = sa->sa_len;
203 family = sa->sa_family;
206 opt.sopt_dir = SOPT_GET;
207 opt.sopt_level = SOL_SOCKET;
208 opt.sopt_name = SO_TYPE;
209 opt.sopt_val = &type;
210 opt.sopt_valsize = sizeof type;
212 error = sogetopt(so, &opt);
217 if (family != AF_LOCAL) {
218 if (type == SOCK_STREAM)
220 else if (type == SOCK_DGRAM)
228 sip->si_proto = proto;
229 sip->si_socktype = type;
235 * Linear search, but the number of entries is small.
238 __rpc_nconf2sockinfo(const struct netconfig *nconf, struct __rpc_sockinfo *sip)
242 for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++)
243 if (strcmp(na_cvt[i].netid, nconf->nc_netid) == 0 || (
244 strcmp(nconf->nc_netid, "unix") == 0 &&
245 strcmp(na_cvt[i].netid, "local") == 0)) {
246 sip->si_af = na_cvt[i].af;
247 sip->si_proto = na_cvt[i].protocol;
249 __rpc_seman2socktype((int)nconf->nc_semantics);
250 if (sip->si_socktype == -1)
252 sip->si_alen = __rpc_get_a_size(sip->si_af);
260 __rpc_nconf2socket(const struct netconfig *nconf)
262 struct __rpc_sockinfo si;
266 if (!__rpc_nconf2sockinfo(nconf, &si))
270 error = socreate(si.si_af, &so, si.si_socktype, si.si_proto,
271 curthread->td_ucred, curthread);
280 taddr2uaddr(const struct netconfig *nconf, const struct netbuf *nbuf)
282 struct __rpc_sockinfo si;
284 if (!__rpc_nconf2sockinfo(nconf, &si))
286 return __rpc_taddr2uaddr_af(si.si_af, nbuf);
290 uaddr2taddr(const struct netconfig *nconf, const char *uaddr)
292 struct __rpc_sockinfo si;
294 if (!__rpc_nconf2sockinfo(nconf, &si))
296 return __rpc_uaddr2taddr_af(si.si_af, uaddr);
300 __rpc_taddr2uaddr_af(int af, const struct netbuf *nbuf)
304 struct sockaddr_in *sin;
305 struct sockaddr_un *sun;
306 char namebuf[INET_ADDRSTRLEN];
308 struct sockaddr_in6 *sin6;
309 char namebuf6[INET6_ADDRSTRLEN];
313 sbuf_new(&sb, NULL, 0, SBUF_AUTOEXTEND);
317 if (nbuf->len < sizeof(*sin))
320 if (inet_ntop(af, &sin->sin_addr, namebuf, sizeof namebuf)
323 port = ntohs(sin->sin_port);
324 if (sbuf_printf(&sb, "%s.%u.%u", namebuf,
325 ((uint32_t)port) >> 8,
331 if (nbuf->len < sizeof(*sin6))
334 if (inet_ntop(af, &sin6->sin6_addr, namebuf6, sizeof namebuf6)
337 port = ntohs(sin6->sin6_port);
338 if (sbuf_printf(&sb, "%s.%u.%u", namebuf6,
339 ((uint32_t)port) >> 8,
346 if (sbuf_printf(&sb, "%.*s", (int)(sun->sun_len -
347 offsetof(struct sockaddr_un, sun_path)),
356 ret = strdup(sbuf_data(&sb), M_RPC);
363 __rpc_uaddr2taddr_af(int af, const char *uaddr)
365 struct netbuf *ret = NULL;
367 unsigned port, portlo, porthi;
368 struct sockaddr_in *sin;
370 struct sockaddr_in6 *sin6;
372 struct sockaddr_un *sun;
380 addrstr = strdup(uaddr, M_RPC);
385 * AF_LOCAL addresses are expected to be absolute
386 * pathnames, anything else will be AF_INET or AF_INET6.
388 if (*addrstr != '/') {
389 p = strrchr(addrstr, '.');
392 portlo = (unsigned)strtol(p + 1, NULL, 10);
395 p = strrchr(addrstr, '.');
398 porthi = (unsigned)strtol(p + 1, NULL, 10);
400 port = (porthi << 8) | portlo;
403 ret = (struct netbuf *)malloc(sizeof *ret, M_RPC, M_WAITOK);
407 sin = (struct sockaddr_in *)malloc(sizeof *sin, M_RPC,
409 memset(sin, 0, sizeof *sin);
410 sin->sin_family = AF_INET;
411 sin->sin_port = htons(port);
412 if (inet_pton(AF_INET, addrstr, &sin->sin_addr) <= 0) {
418 sin->sin_len = ret->maxlen = ret->len = sizeof *sin;
423 sin6 = (struct sockaddr_in6 *)malloc(sizeof *sin6, M_RPC,
425 memset(sin6, 0, sizeof *sin6);
426 sin6->sin6_family = AF_INET6;
427 sin6->sin6_port = htons(port);
428 if (inet_pton(AF_INET6, addrstr, &sin6->sin6_addr) <= 0) {
434 sin6->sin6_len = ret->maxlen = ret->len = sizeof *sin6;
439 sun = (struct sockaddr_un *)malloc(sizeof *sun, M_RPC,
441 memset(sun, 0, sizeof *sun);
442 sun->sun_family = AF_LOCAL;
443 strncpy(sun->sun_path, addrstr, sizeof(sun->sun_path) - 1);
444 ret->len = ret->maxlen = sun->sun_len = SUN_LEN(sun);
451 free(addrstr, M_RPC);
456 __rpc_seman2socktype(int semantics)
461 case NC_TPI_COTS_ORD:
473 __rpc_socktype2seman(int socktype)
479 return NC_TPI_COTS_ORD;
490 * Returns the type of the network as defined in <rpc/nettype.h>
491 * If nettype is NULL, it defaults to NETPATH.
494 getnettype(const char *nettype)
498 if ((nettype == NULL) || (nettype[0] == 0)) {
499 return (_RPC_NETPATH); /* Default */
503 nettype = strlocase(nettype);
505 for (i = 0; _rpctypelist[i].name; i++)
506 if (strcasecmp(nettype, _rpctypelist[i].name) == 0) {
507 return (_rpctypelist[i].type);
509 return (_rpctypelist[i].type);
513 * For the given nettype (tcp or udp only), return the first structure found.
514 * This should be freed by calling freenetconfigent()
517 __rpc_getconfip(const char *nettype)
520 static char *netid_tcp = (char *) NULL;
521 static char *netid_udp = (char *) NULL;
522 struct netconfig *dummy;
524 if (!netid_udp && !netid_tcp) {
525 struct netconfig *nconf;
528 if (!(confighandle = setnetconfig())) {
529 log(LOG_ERR, "rpc: failed to open " NETCONFIG);
532 while ((nconf = getnetconfig(confighandle)) != NULL) {
533 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
534 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
535 netid_tcp = strdup(nconf->nc_netid,
538 if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
539 netid_udp = strdup(nconf->nc_netid,
544 endnetconfig(confighandle);
546 if (strcmp(nettype, "udp") == 0)
548 else if (strcmp(nettype, "tcp") == 0)
553 if ((netid == NULL) || (netid[0] == 0)) {
556 dummy = getnetconfigent(netid);
561 * Returns the type of the nettype, which should then be used with
564 * For simplicity in the kernel, we don't support the NETPATH
565 * environment variable. We behave as userland would then NETPATH is
566 * unset, i.e. iterate over all visible entries in netconfig.
569 __rpc_setconf(const char *nettype)
571 struct handle *handle;
573 handle = (struct handle *) malloc(sizeof (struct handle),
575 switch (handle->nettype = getnettype(nettype)) {
578 case _RPC_DATAGRAM_N:
579 if (!(handle->nhandle = setnetconfig()))
581 handle->nflag = TRUE;
585 case _RPC_DATAGRAM_V:
588 if (!(handle->nhandle = setnetconfig())) {
589 log(LOG_ERR, "rpc: failed to open " NETCONFIG);
592 handle->nflag = FALSE;
606 * Returns the next netconfig struct for the given "net" type.
607 * __rpc_setconf() should have been called previously.
610 __rpc_getconf(void *vhandle)
612 struct handle *handle;
613 struct netconfig *nconf;
615 handle = (struct handle *)vhandle;
616 if (handle == NULL) {
621 nconf = getnetconfig(handle->nhandle);
622 if (nconf && !(nconf->nc_flag & NC_VISIBLE))
625 nconf = getnetconfig(handle->nhandle);
629 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
630 (nconf->nc_semantics != NC_TPI_COTS) &&
631 (nconf->nc_semantics != NC_TPI_COTS_ORD))
633 switch (handle->nettype) {
635 if (!(nconf->nc_flag & NC_VISIBLE))
638 case _RPC_NETPATH: /* Be happy */
641 if (!(nconf->nc_flag & NC_VISIBLE))
645 if ((nconf->nc_semantics != NC_TPI_COTS) &&
646 (nconf->nc_semantics != NC_TPI_COTS_ORD))
649 case _RPC_DATAGRAM_V:
650 if (!(nconf->nc_flag & NC_VISIBLE))
653 case _RPC_DATAGRAM_N:
654 if (nconf->nc_semantics != NC_TPI_CLTS)
658 if (((nconf->nc_semantics != NC_TPI_COTS) &&
659 (nconf->nc_semantics != NC_TPI_COTS_ORD)) ||
660 (strcmp(nconf->nc_protofmly, NC_INET)
662 && strcmp(nconf->nc_protofmly, NC_INET6))
667 strcmp(nconf->nc_proto, NC_TCP))
671 if ((nconf->nc_semantics != NC_TPI_CLTS) ||
672 (strcmp(nconf->nc_protofmly, NC_INET)
674 && strcmp(nconf->nc_protofmly, NC_INET6))
679 strcmp(nconf->nc_proto, NC_UDP))
689 __rpc_endconf(void *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 CURVNET_SET(so->so_vnet);
708 error = so->so_proto->pr_sockaddr(so, &sa);
713 switch (sa->sa_family) {
715 bound = (((struct sockaddr_in *) sa)->sin_port != 0);
719 bound = (((struct sockaddr_in6 *) sa)->sin6_port != 0);
724 bound = (((struct sockaddr_un *) sa)->sun_path[0] != '\0');
737 * Implement XDR-style API for RPC call.
741 CLIENT *cl, /* client handle */
742 struct rpc_callextra *ext, /* call metadata */
743 rpcproc_t proc, /* procedure number */
744 xdrproc_t xargs, /* xdr routine for args */
745 void *argsp, /* pointer to args */
746 xdrproc_t xresults, /* xdr routine for results */
747 void *resultsp, /* pointer to results */
748 struct timeval utimeout) /* seconds to wait before giving up */
755 mreq = m_getcl(M_WAITOK, MT_DATA, 0);
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 CURVNET_SET(so->so_vnet);
798 error = so->so_proto->pr_sockaddr(so, &sa);
805 memset(sa, 0, sa->sa_len);
814 portrange = IP_PORTRANGE;
815 portlow = IP_PORTRANGE_LOW;
816 sin = (struct sockaddr_in *)sa;
817 portp = &sin->sin_port;
821 proto = IPPROTO_IPV6;
822 portrange = IPV6_PORTRANGE;
823 portlow = IPV6_PORTRANGE_LOW;
824 sin6 = (struct sockaddr_in6 *)sa;
825 portp = &sin6->sin6_port;
829 return (EPFNOSUPPORT);
836 bzero(&opt, sizeof(opt));
837 opt.sopt_dir = SOPT_GET;
838 opt.sopt_level = proto;
839 opt.sopt_name = portrange;
841 opt.sopt_valsize = sizeof(old);
842 error = sogetopt(so, &opt);
847 opt.sopt_dir = SOPT_SET;
848 opt.sopt_val = &portlow;
849 error = sosetopt(so, &opt);
854 error = sobind(so, sa, curthread);
858 opt.sopt_dir = SOPT_SET;
871 * Make sure an mbuf list is made up entirely of ext_pgs mbufs.
872 * This is needed for sosend() when KERN_TLS is being used.
873 * (There might also be a performance improvement for certain
874 * network interfaces that handle ext_pgs mbufs efficiently.)
875 * It expects at least one non-ext_pgs mbuf followed by zero
876 * or more ext_pgs mbufs. It does not handle the case where
877 * non-ext_pgs mbuf(s) follow ext_pgs ones.
878 * It also performs sanity checks on the resultant list.
879 * The "mp" argument list is consumed.
880 * The "maxextsiz" argument is the upper bound on the data
881 * size for each mbuf (usually 16K for KERN_TLS).
884 _rpc_copym_into_ext_pgs(struct mbuf *mp, int maxextsiz)
886 struct mbuf *m, *m2, *m3, *mhead;
889 KASSERT((mp->m_flags & (M_EXT | M_EXTPG)) !=
890 (M_EXT | M_EXTPG), ("_rpc_copym_into_ext_pgs:"
891 " first mbuf is an ext_pgs"));
893 * Find the last non-ext_pgs mbuf and the total
894 * length of the non-ext_pgs mbuf(s).
895 * The first mbuf must always be a non-ext_pgs
900 for (m = mp->m_next; m != NULL; m = m->m_next) {
901 if ((m->m_flags & M_EXTPG) != 0)
908 * Copy the non-ext_pgs mbuf(s) into an ext_pgs
912 mhead = mb_mapped_to_unmapped(mp, tlen, maxextsiz,
916 * Link the ext_pgs list onto the newly copied
917 * list and free up the non-ext_pgs mbuf(s).
923 * Sanity check the resultant mbuf list. Check for and
924 * remove any 0 length mbufs in the list, since the
925 * KERN_TLS code does not expect any 0 length mbuf(s)
932 KASSERT(m2->m_len >= 0, ("_rpc_copym_into_ext_pgs:"
934 KASSERT((m2->m_flags & (M_EXT | M_EXTPG)) ==
935 (M_EXT | M_EXTPG), ("_rpc_copym_into_ext_pgs:"
936 " non-nomap mbuf in list"));
937 if (m2->m_len == 0) {
939 m3->m_next = m2->m_next;
949 MBUF_EXT_PGS_ASSERT_SANITY(m2);
962 krpc_modevent(module_t mod, int type, void *data)
968 error = rpctls_init();
972 * Cannot be unloaded, since the rpctlssd or rpctlscd daemons
973 * might be performing a rpctls syscall.
981 static moduledata_t krpc_mod = {
986 DECLARE_MODULE(krpc, krpc_mod, SI_SUB_VFS, SI_ORDER_ANY);
988 /* So that loader and kldload(2) can find us, wherever we are.. */
989 MODULE_VERSION(krpc, 1);
990 MODULE_DEPEND(krpc, xdr, 1, 1, 1);