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>
64 #include <rpc/rpc_com.h>
66 #if __FreeBSD_version < 700000
67 #define strrchr rindex
71 NCONF_HANDLE *nhandle;
72 int nflag; /* Whether NETPATH or NETCONFIG */
76 static const struct _rpcnettype {
80 { "netpath", _RPC_NETPATH },
81 { "visible", _RPC_VISIBLE },
82 { "circuit_v", _RPC_CIRCUIT_V },
83 { "datagram_v", _RPC_DATAGRAM_V },
84 { "circuit_n", _RPC_CIRCUIT_N },
85 { "datagram_n", _RPC_DATAGRAM_N },
97 static const struct netid_af na_cvt[] = {
98 { "udp", AF_INET, IPPROTO_UDP },
99 { "tcp", AF_INET, IPPROTO_TCP },
101 { "udp6", AF_INET6, IPPROTO_UDP },
102 { "tcp6", AF_INET6, IPPROTO_TCP },
104 { "local", AF_LOCAL, 0 }
107 struct rpc_createerr rpc_createerr;
110 * Find the appropriate buffer size
114 __rpc_get_t_size(int af, int proto, int size)
116 int maxsize, defsize;
118 maxsize = 256 * 1024; /* XXX */
121 defsize = 64 * 1024; /* XXX */
124 defsize = UDPMSGSIZE;
127 defsize = RPC_MAXDATASIZE;
133 /* Check whether the value is within the upper max limit */
134 return (size > maxsize ? (u_int)maxsize : (u_int)size);
138 * Find the appropriate address buffer size
146 return sizeof (struct sockaddr_in);
149 return sizeof (struct sockaddr_in6);
152 return sizeof (struct sockaddr_un);
156 return ((u_int)RPC_MAXADDRSIZE);
162 * Used to ping the NULL procedure for clnt handle.
163 * Returns NULL if fails, else a non-NULL pointer.
169 struct timeval TIMEOUT = {25, 0};
171 if (clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void, NULL,
172 (xdrproc_t) xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) {
175 return ((void *) clnt);
181 __rpc_socket2sockinfo(struct socket *so, struct __rpc_sockinfo *sip)
189 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
193 sip->si_alen = sa->sa_len;
194 family = sa->sa_family;
197 opt.sopt_dir = SOPT_GET;
198 opt.sopt_level = SOL_SOCKET;
199 opt.sopt_name = SO_TYPE;
200 opt.sopt_val = &type;
201 opt.sopt_valsize = sizeof type;
203 error = sogetopt(so, &opt);
208 if (family != AF_LOCAL) {
209 if (type == SOCK_STREAM)
211 else if (type == SOCK_DGRAM)
219 sip->si_proto = proto;
220 sip->si_socktype = type;
226 * Linear search, but the number of entries is small.
229 __rpc_nconf2sockinfo(const struct netconfig *nconf, struct __rpc_sockinfo *sip)
233 for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++)
234 if (strcmp(na_cvt[i].netid, nconf->nc_netid) == 0 || (
235 strcmp(nconf->nc_netid, "unix") == 0 &&
236 strcmp(na_cvt[i].netid, "local") == 0)) {
237 sip->si_af = na_cvt[i].af;
238 sip->si_proto = na_cvt[i].protocol;
240 __rpc_seman2socktype((int)nconf->nc_semantics);
241 if (sip->si_socktype == -1)
243 sip->si_alen = __rpc_get_a_size(sip->si_af);
251 __rpc_nconf2socket(const struct netconfig *nconf)
253 struct __rpc_sockinfo si;
257 if (!__rpc_nconf2sockinfo(nconf, &si))
261 error = socreate(si.si_af, &so, si.si_socktype, si.si_proto,
262 curthread->td_ucred, curthread);
271 taddr2uaddr(const struct netconfig *nconf, const struct netbuf *nbuf)
273 struct __rpc_sockinfo si;
275 if (!__rpc_nconf2sockinfo(nconf, &si))
277 return __rpc_taddr2uaddr_af(si.si_af, nbuf);
281 uaddr2taddr(const struct netconfig *nconf, const char *uaddr)
283 struct __rpc_sockinfo si;
285 if (!__rpc_nconf2sockinfo(nconf, &si))
287 return __rpc_uaddr2taddr_af(si.si_af, uaddr);
291 __rpc_taddr2uaddr_af(int af, const struct netbuf *nbuf)
295 struct sockaddr_in *sin;
296 struct sockaddr_un *sun;
297 char namebuf[INET_ADDRSTRLEN];
299 struct sockaddr_in6 *sin6;
300 char namebuf6[INET6_ADDRSTRLEN];
304 sbuf_new(&sb, NULL, 0, SBUF_AUTOEXTEND);
309 if (__rpc_inet_ntop(af, &sin->sin_addr, namebuf, sizeof namebuf)
312 port = ntohs(sin->sin_port);
313 if (sbuf_printf(&sb, "%s.%u.%u", namebuf,
314 ((uint32_t)port) >> 8,
321 if (__rpc_inet_ntop(af, &sin6->sin6_addr, namebuf6, sizeof namebuf6)
324 port = ntohs(sin6->sin6_port);
325 if (sbuf_printf(&sb, "%s.%u.%u", namebuf6,
326 ((uint32_t)port) >> 8,
333 if (sbuf_printf(&sb, "%.*s", (int)(sun->sun_len -
334 offsetof(struct sockaddr_un, sun_path)),
343 ret = strdup(sbuf_data(&sb), M_RPC);
350 __rpc_uaddr2taddr_af(int af, const char *uaddr)
352 struct netbuf *ret = NULL;
354 unsigned port, portlo, porthi;
355 struct sockaddr_in *sin;
357 struct sockaddr_in6 *sin6;
359 struct sockaddr_un *sun;
363 addrstr = strdup(uaddr, M_RPC);
368 * AF_LOCAL addresses are expected to be absolute
369 * pathnames, anything else will be AF_INET or AF_INET6.
371 if (*addrstr != '/') {
372 p = strrchr(addrstr, '.');
375 portlo = (unsigned)strtol(p + 1, NULL, 10);
378 p = strrchr(addrstr, '.');
381 porthi = (unsigned)strtol(p + 1, NULL, 10);
383 port = (porthi << 8) | portlo;
386 ret = (struct netbuf *)malloc(sizeof *ret, M_RPC, M_WAITOK);
392 sin = (struct sockaddr_in *)malloc(sizeof *sin, M_RPC,
396 memset(sin, 0, sizeof *sin);
397 sin->sin_family = AF_INET;
398 sin->sin_port = htons(port);
399 if (__rpc_inet_pton(AF_INET, addrstr, &sin->sin_addr) <= 0) {
405 sin->sin_len = ret->maxlen = ret->len = sizeof *sin;
410 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 (__rpc_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,
432 memset(sun, 0, sizeof *sun);
433 sun->sun_family = AF_LOCAL;
434 strncpy(sun->sun_path, addrstr, sizeof(sun->sun_path) - 1);
435 ret->len = ret->maxlen = sun->sun_len = SUN_LEN(sun);
442 free(addrstr, M_RPC);
447 __rpc_seman2socktype(int semantics)
452 case NC_TPI_COTS_ORD:
464 __rpc_socktype2seman(int socktype)
470 return NC_TPI_COTS_ORD;
481 * Returns the type of the network as defined in <rpc/nettype.h>
482 * If nettype is NULL, it defaults to NETPATH.
485 getnettype(const char *nettype)
489 if ((nettype == NULL) || (nettype[0] == 0)) {
490 return (_RPC_NETPATH); /* Default */
494 nettype = strlocase(nettype);
496 for (i = 0; _rpctypelist[i].name; i++)
497 if (strcasecmp(nettype, _rpctypelist[i].name) == 0) {
498 return (_rpctypelist[i].type);
500 return (_rpctypelist[i].type);
504 * For the given nettype (tcp or udp only), return the first structure found.
505 * This should be freed by calling freenetconfigent()
508 __rpc_getconfip(const char *nettype)
511 static char *netid_tcp = (char *) NULL;
512 static char *netid_udp = (char *) NULL;
513 struct netconfig *dummy;
515 if (!netid_udp && !netid_tcp) {
516 struct netconfig *nconf;
519 if (!(confighandle = setnetconfig())) {
520 log(LOG_ERR, "rpc: failed to open " NETCONFIG);
523 while ((nconf = getnetconfig(confighandle)) != NULL) {
524 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
525 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
526 netid_tcp = strdup(nconf->nc_netid,
529 if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
530 netid_udp = strdup(nconf->nc_netid,
535 endnetconfig(confighandle);
537 if (strcmp(nettype, "udp") == 0)
539 else if (strcmp(nettype, "tcp") == 0)
544 if ((netid == NULL) || (netid[0] == 0)) {
547 dummy = getnetconfigent(netid);
552 * Returns the type of the nettype, which should then be used with
555 * For simplicity in the kernel, we don't support the NETPATH
556 * environment variable. We behave as userland would then NETPATH is
557 * unset, i.e. iterate over all visible entries in netconfig.
560 __rpc_setconf(nettype)
563 struct handle *handle;
565 handle = (struct handle *) malloc(sizeof (struct handle),
567 switch (handle->nettype = getnettype(nettype)) {
570 case _RPC_DATAGRAM_N:
571 if (!(handle->nhandle = setnetconfig()))
573 handle->nflag = TRUE;
577 case _RPC_DATAGRAM_V:
580 if (!(handle->nhandle = setnetconfig())) {
581 log(LOG_ERR, "rpc: failed to open " NETCONFIG);
584 handle->nflag = FALSE;
598 * Returns the next netconfig struct for the given "net" type.
599 * __rpc_setconf() should have been called previously.
602 __rpc_getconf(void *vhandle)
604 struct handle *handle;
605 struct netconfig *nconf;
607 handle = (struct handle *)vhandle;
608 if (handle == NULL) {
613 nconf = getnetconfig(handle->nhandle);
614 if (nconf && !(nconf->nc_flag & NC_VISIBLE))
617 nconf = getnetconfig(handle->nhandle);
621 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
622 (nconf->nc_semantics != NC_TPI_COTS) &&
623 (nconf->nc_semantics != NC_TPI_COTS_ORD))
625 switch (handle->nettype) {
627 if (!(nconf->nc_flag & NC_VISIBLE))
630 case _RPC_NETPATH: /* Be happy */
633 if (!(nconf->nc_flag & NC_VISIBLE))
637 if ((nconf->nc_semantics != NC_TPI_COTS) &&
638 (nconf->nc_semantics != NC_TPI_COTS_ORD))
641 case _RPC_DATAGRAM_V:
642 if (!(nconf->nc_flag & NC_VISIBLE))
645 case _RPC_DATAGRAM_N:
646 if (nconf->nc_semantics != NC_TPI_CLTS)
650 if (((nconf->nc_semantics != NC_TPI_COTS) &&
651 (nconf->nc_semantics != NC_TPI_COTS_ORD)) ||
652 (strcmp(nconf->nc_protofmly, NC_INET)
654 && strcmp(nconf->nc_protofmly, NC_INET6))
659 strcmp(nconf->nc_proto, NC_TCP))
663 if ((nconf->nc_semantics != NC_TPI_CLTS) ||
664 (strcmp(nconf->nc_protofmly, NC_INET)
666 && strcmp(nconf->nc_protofmly, NC_INET6))
671 strcmp(nconf->nc_proto, NC_UDP))
681 __rpc_endconf(vhandle)
684 struct handle *handle;
686 handle = (struct handle *) vhandle;
687 if (handle == NULL) {
690 endnetconfig(handle->nhandle);
695 __rpc_sockisbound(struct socket *so)
700 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 MGET(mreq, M_WAIT, MT_DATA);
747 MCLGET(mreq, M_WAIT);
750 xdrmbuf_create(&xdrs, mreq, XDR_ENCODE);
751 if (!xargs(&xdrs, argsp)) {
753 return (RPC_CANTENCODEARGS);
757 stat = CLNT_CALL_MBUF(cl, ext, proc, mreq, &mrep, utimeout);
760 if (stat == RPC_SUCCESS) {
761 xdrmbuf_create(&xdrs, mrep, XDR_DECODE);
762 if (!xresults(&xdrs, resultsp)) {
764 return (RPC_CANTDECODERES);
773 * Bind a socket to a privileged IP port
776 bindresvport(struct socket *so, struct sockaddr *sa)
779 bool_t freesa = FALSE;
780 struct sockaddr_in *sin;
782 struct sockaddr_in6 *sin6;
785 int proto, portrange, portlow;
790 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 CURVNET_SET(so->so_vnet);
828 bzero(&opt, sizeof(opt));
829 opt.sopt_dir = SOPT_GET;
830 opt.sopt_level = proto;
831 opt.sopt_name = portrange;
833 opt.sopt_valsize = sizeof(old);
834 error = sogetopt(so, &opt);
840 opt.sopt_dir = SOPT_SET;
841 opt.sopt_val = &portlow;
842 error = sosetopt(so, &opt);
848 error = sobind(so, sa, curthread);
852 opt.sopt_dir = SOPT_SET;
854 CURVNET_SET(so->so_vnet);
870 krpc_modevent(module_t mod, int type, void *data)
875 static moduledata_t krpc_mod = {
880 DECLARE_MODULE(krpc, krpc_mod, SI_SUB_VFS, SI_ORDER_ANY);
882 /* So that loader and kldload(2) can find us, wherever we are.. */
883 MODULE_VERSION(krpc, 1);