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);
315 if (inet_ntop(af, &sin->sin_addr, namebuf, sizeof namebuf)
318 port = ntohs(sin->sin_port);
319 if (sbuf_printf(&sb, "%s.%u.%u", namebuf,
320 ((uint32_t)port) >> 8,
327 if (inet_ntop(af, &sin6->sin6_addr, namebuf6, sizeof namebuf6)
330 port = ntohs(sin6->sin6_port);
331 if (sbuf_printf(&sb, "%s.%u.%u", namebuf6,
332 ((uint32_t)port) >> 8,
339 if (sbuf_printf(&sb, "%.*s", (int)(sun->sun_len -
340 offsetof(struct sockaddr_un, sun_path)),
349 ret = strdup(sbuf_data(&sb), M_RPC);
356 __rpc_uaddr2taddr_af(int af, const char *uaddr)
358 struct netbuf *ret = NULL;
360 unsigned port, portlo, porthi;
361 struct sockaddr_in *sin;
363 struct sockaddr_in6 *sin6;
365 struct sockaddr_un *sun;
369 addrstr = strdup(uaddr, M_RPC);
374 * AF_LOCAL addresses are expected to be absolute
375 * pathnames, anything else will be AF_INET or AF_INET6.
377 if (*addrstr != '/') {
378 p = strrchr(addrstr, '.');
381 portlo = (unsigned)strtol(p + 1, NULL, 10);
384 p = strrchr(addrstr, '.');
387 porthi = (unsigned)strtol(p + 1, NULL, 10);
389 port = (porthi << 8) | portlo;
392 ret = (struct netbuf *)malloc(sizeof *ret, M_RPC, M_WAITOK);
396 sin = (struct sockaddr_in *)malloc(sizeof *sin, M_RPC,
398 memset(sin, 0, sizeof *sin);
399 sin->sin_family = AF_INET;
400 sin->sin_port = htons(port);
401 if (inet_pton(AF_INET, addrstr, &sin->sin_addr) <= 0) {
407 sin->sin_len = ret->maxlen = ret->len = sizeof *sin;
412 sin6 = (struct sockaddr_in6 *)malloc(sizeof *sin6, M_RPC,
414 memset(sin6, 0, sizeof *sin6);
415 sin6->sin6_family = AF_INET6;
416 sin6->sin6_port = htons(port);
417 if (inet_pton(AF_INET6, addrstr, &sin6->sin6_addr) <= 0) {
423 sin6->sin6_len = ret->maxlen = ret->len = sizeof *sin6;
428 sun = (struct sockaddr_un *)malloc(sizeof *sun, M_RPC,
430 memset(sun, 0, sizeof *sun);
431 sun->sun_family = AF_LOCAL;
432 strncpy(sun->sun_path, addrstr, sizeof(sun->sun_path) - 1);
433 ret->len = ret->maxlen = sun->sun_len = SUN_LEN(sun);
440 free(addrstr, M_RPC);
445 __rpc_seman2socktype(int semantics)
450 case NC_TPI_COTS_ORD:
462 __rpc_socktype2seman(int socktype)
468 return NC_TPI_COTS_ORD;
479 * Returns the type of the network as defined in <rpc/nettype.h>
480 * If nettype is NULL, it defaults to NETPATH.
483 getnettype(const char *nettype)
487 if ((nettype == NULL) || (nettype[0] == 0)) {
488 return (_RPC_NETPATH); /* Default */
492 nettype = strlocase(nettype);
494 for (i = 0; _rpctypelist[i].name; i++)
495 if (strcasecmp(nettype, _rpctypelist[i].name) == 0) {
496 return (_rpctypelist[i].type);
498 return (_rpctypelist[i].type);
502 * For the given nettype (tcp or udp only), return the first structure found.
503 * This should be freed by calling freenetconfigent()
506 __rpc_getconfip(const char *nettype)
509 static char *netid_tcp = (char *) NULL;
510 static char *netid_udp = (char *) NULL;
511 struct netconfig *dummy;
513 if (!netid_udp && !netid_tcp) {
514 struct netconfig *nconf;
517 if (!(confighandle = setnetconfig())) {
518 log(LOG_ERR, "rpc: failed to open " NETCONFIG);
521 while ((nconf = getnetconfig(confighandle)) != NULL) {
522 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
523 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
524 netid_tcp = strdup(nconf->nc_netid,
527 if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
528 netid_udp = strdup(nconf->nc_netid,
533 endnetconfig(confighandle);
535 if (strcmp(nettype, "udp") == 0)
537 else if (strcmp(nettype, "tcp") == 0)
542 if ((netid == NULL) || (netid[0] == 0)) {
545 dummy = getnetconfigent(netid);
550 * Returns the type of the nettype, which should then be used with
553 * For simplicity in the kernel, we don't support the NETPATH
554 * environment variable. We behave as userland would then NETPATH is
555 * unset, i.e. iterate over all visible entries in netconfig.
558 __rpc_setconf(nettype)
561 struct handle *handle;
563 handle = (struct handle *) malloc(sizeof (struct handle),
565 switch (handle->nettype = getnettype(nettype)) {
568 case _RPC_DATAGRAM_N:
569 if (!(handle->nhandle = setnetconfig()))
571 handle->nflag = TRUE;
575 case _RPC_DATAGRAM_V:
578 if (!(handle->nhandle = setnetconfig())) {
579 log(LOG_ERR, "rpc: failed to open " NETCONFIG);
582 handle->nflag = FALSE;
596 * Returns the next netconfig struct for the given "net" type.
597 * __rpc_setconf() should have been called previously.
600 __rpc_getconf(void *vhandle)
602 struct handle *handle;
603 struct netconfig *nconf;
605 handle = (struct handle *)vhandle;
606 if (handle == NULL) {
611 nconf = getnetconfig(handle->nhandle);
612 if (nconf && !(nconf->nc_flag & NC_VISIBLE))
615 nconf = getnetconfig(handle->nhandle);
619 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
620 (nconf->nc_semantics != NC_TPI_COTS) &&
621 (nconf->nc_semantics != NC_TPI_COTS_ORD))
623 switch (handle->nettype) {
625 if (!(nconf->nc_flag & NC_VISIBLE))
628 case _RPC_NETPATH: /* Be happy */
631 if (!(nconf->nc_flag & NC_VISIBLE))
635 if ((nconf->nc_semantics != NC_TPI_COTS) &&
636 (nconf->nc_semantics != NC_TPI_COTS_ORD))
639 case _RPC_DATAGRAM_V:
640 if (!(nconf->nc_flag & NC_VISIBLE))
643 case _RPC_DATAGRAM_N:
644 if (nconf->nc_semantics != NC_TPI_CLTS)
648 if (((nconf->nc_semantics != NC_TPI_COTS) &&
649 (nconf->nc_semantics != NC_TPI_COTS_ORD)) ||
650 (strcmp(nconf->nc_protofmly, NC_INET)
652 && strcmp(nconf->nc_protofmly, NC_INET6))
657 strcmp(nconf->nc_proto, NC_TCP))
661 if ((nconf->nc_semantics != NC_TPI_CLTS) ||
662 (strcmp(nconf->nc_protofmly, NC_INET)
664 && strcmp(nconf->nc_protofmly, NC_INET6))
669 strcmp(nconf->nc_proto, NC_UDP))
679 __rpc_endconf(vhandle)
682 struct handle *handle;
684 handle = (struct handle *) vhandle;
685 if (handle == NULL) {
688 endnetconfig(handle->nhandle);
693 __rpc_sockisbound(struct socket *so)
698 CURVNET_SET(so->so_vnet);
699 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
704 switch (sa->sa_family) {
706 bound = (((struct sockaddr_in *) sa)->sin_port != 0);
710 bound = (((struct sockaddr_in6 *) sa)->sin6_port != 0);
715 bound = (((struct sockaddr_un *) sa)->sun_path[0] != '\0');
728 * Implement XDR-style API for RPC call.
732 CLIENT *cl, /* client handle */
733 struct rpc_callextra *ext, /* call metadata */
734 rpcproc_t proc, /* procedure number */
735 xdrproc_t xargs, /* xdr routine for args */
736 void *argsp, /* pointer to args */
737 xdrproc_t xresults, /* xdr routine for results */
738 void *resultsp, /* pointer to results */
739 struct timeval utimeout) /* seconds to wait before giving up */
746 mreq = m_getcl(M_WAITOK, MT_DATA, 0);
748 xdrmbuf_create(&xdrs, mreq, XDR_ENCODE);
749 if (!xargs(&xdrs, argsp)) {
751 return (RPC_CANTENCODEARGS);
755 stat = CLNT_CALL_MBUF(cl, ext, proc, mreq, &mrep, utimeout);
758 if (stat == RPC_SUCCESS) {
759 xdrmbuf_create(&xdrs, mrep, XDR_DECODE);
760 if (!xresults(&xdrs, resultsp)) {
762 return (RPC_CANTDECODERES);
771 * Bind a socket to a privileged IP port
774 bindresvport(struct socket *so, struct sockaddr *sa)
777 bool_t freesa = FALSE;
778 struct sockaddr_in *sin;
780 struct sockaddr_in6 *sin6;
783 int proto, portrange, portlow;
788 CURVNET_SET(so->so_vnet);
789 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
796 memset(sa, 0, sa->sa_len);
805 portrange = IP_PORTRANGE;
806 portlow = IP_PORTRANGE_LOW;
807 sin = (struct sockaddr_in *)sa;
808 portp = &sin->sin_port;
812 proto = IPPROTO_IPV6;
813 portrange = IPV6_PORTRANGE;
814 portlow = IPV6_PORTRANGE_LOW;
815 sin6 = (struct sockaddr_in6 *)sa;
816 portp = &sin6->sin6_port;
820 return (EPFNOSUPPORT);
827 bzero(&opt, sizeof(opt));
828 opt.sopt_dir = SOPT_GET;
829 opt.sopt_level = proto;
830 opt.sopt_name = portrange;
832 opt.sopt_valsize = sizeof(old);
833 error = sogetopt(so, &opt);
838 opt.sopt_dir = SOPT_SET;
839 opt.sopt_val = &portlow;
840 error = sosetopt(so, &opt);
845 error = sobind(so, sa, curthread);
849 opt.sopt_dir = SOPT_SET;
865 krpc_modevent(module_t mod, int type, void *data)
870 static moduledata_t krpc_mod = {
875 DECLARE_MODULE(krpc, krpc_mod, SI_SUB_VFS, SI_ORDER_ANY);
877 /* So that loader and kldload(2) can find us, wherever we are.. */
878 MODULE_VERSION(krpc, 1);