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>
60 #include <rpc/nettype.h>
62 #include <rpc/rpc_com.h>
64 #if __FreeBSD_version < 700000
65 #define strrchr rindex
69 NCONF_HANDLE *nhandle;
70 int nflag; /* Whether NETPATH or NETCONFIG */
74 static const struct _rpcnettype {
78 { "netpath", _RPC_NETPATH },
79 { "visible", _RPC_VISIBLE },
80 { "circuit_v", _RPC_CIRCUIT_V },
81 { "datagram_v", _RPC_DATAGRAM_V },
82 { "circuit_n", _RPC_CIRCUIT_N },
83 { "datagram_n", _RPC_DATAGRAM_N },
95 static const struct netid_af na_cvt[] = {
96 { "udp", AF_INET, IPPROTO_UDP },
97 { "tcp", AF_INET, IPPROTO_TCP },
99 { "udp6", AF_INET6, IPPROTO_UDP },
100 { "tcp6", AF_INET6, IPPROTO_TCP },
102 { "local", AF_LOCAL, 0 }
105 struct rpc_createerr rpc_createerr;
108 * Find the appropriate buffer size
112 __rpc_get_t_size(int af, int proto, int size)
114 int maxsize, defsize;
116 maxsize = 256 * 1024; /* XXX */
119 defsize = 64 * 1024; /* XXX */
122 defsize = UDPMSGSIZE;
125 defsize = RPC_MAXDATASIZE;
131 /* Check whether the value is within the upper max limit */
132 return (size > maxsize ? (u_int)maxsize : (u_int)size);
136 * Find the appropriate address buffer size
144 return sizeof (struct sockaddr_in);
147 return sizeof (struct sockaddr_in6);
150 return sizeof (struct sockaddr_un);
154 return ((u_int)RPC_MAXADDRSIZE);
160 * Used to ping the NULL procedure for clnt handle.
161 * Returns NULL if fails, else a non-NULL pointer.
167 struct timeval TIMEOUT = {25, 0};
169 if (clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void, NULL,
170 (xdrproc_t) xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) {
173 return ((void *) clnt);
179 __rpc_socket2sockinfo(struct socket *so, struct __rpc_sockinfo *sip)
187 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
191 sip->si_alen = sa->sa_len;
192 family = sa->sa_family;
195 opt.sopt_dir = SOPT_GET;
196 opt.sopt_level = SOL_SOCKET;
197 opt.sopt_name = SO_TYPE;
198 opt.sopt_val = &type;
199 opt.sopt_valsize = sizeof type;
201 error = sogetopt(so, &opt);
206 if (family != AF_LOCAL) {
207 if (type == SOCK_STREAM)
209 else if (type == SOCK_DGRAM)
217 sip->si_proto = proto;
218 sip->si_socktype = type;
224 * Linear search, but the number of entries is small.
227 __rpc_nconf2sockinfo(const struct netconfig *nconf, struct __rpc_sockinfo *sip)
231 for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++)
232 if (strcmp(na_cvt[i].netid, nconf->nc_netid) == 0 || (
233 strcmp(nconf->nc_netid, "unix") == 0 &&
234 strcmp(na_cvt[i].netid, "local") == 0)) {
235 sip->si_af = na_cvt[i].af;
236 sip->si_proto = na_cvt[i].protocol;
238 __rpc_seman2socktype((int)nconf->nc_semantics);
239 if (sip->si_socktype == -1)
241 sip->si_alen = __rpc_get_a_size(sip->si_af);
249 __rpc_nconf2socket(const struct netconfig *nconf)
251 struct __rpc_sockinfo si;
255 if (!__rpc_nconf2sockinfo(nconf, &si))
259 error = socreate(si.si_af, &so, si.si_socktype, si.si_proto,
260 curthread->td_ucred, curthread);
269 taddr2uaddr(const struct netconfig *nconf, const struct netbuf *nbuf)
271 struct __rpc_sockinfo si;
273 if (!__rpc_nconf2sockinfo(nconf, &si))
275 return __rpc_taddr2uaddr_af(si.si_af, nbuf);
279 uaddr2taddr(const struct netconfig *nconf, const char *uaddr)
281 struct __rpc_sockinfo si;
283 if (!__rpc_nconf2sockinfo(nconf, &si))
285 return __rpc_uaddr2taddr_af(si.si_af, uaddr);
289 __rpc_taddr2uaddr_af(int af, const struct netbuf *nbuf)
293 struct sockaddr_in *sin;
294 struct sockaddr_un *sun;
295 char namebuf[INET_ADDRSTRLEN];
297 struct sockaddr_in6 *sin6;
298 char namebuf6[INET6_ADDRSTRLEN];
302 sbuf_new(&sb, NULL, 0, SBUF_AUTOEXTEND);
307 if (__rpc_inet_ntop(af, &sin->sin_addr, namebuf, sizeof namebuf)
310 port = ntohs(sin->sin_port);
311 if (sbuf_printf(&sb, "%s.%u.%u", namebuf,
312 ((uint32_t)port) >> 8,
319 if (__rpc_inet_ntop(af, &sin6->sin6_addr, namebuf6, sizeof namebuf6)
322 port = ntohs(sin6->sin6_port);
323 if (sbuf_printf(&sb, "%s.%u.%u", namebuf6,
324 ((uint32_t)port) >> 8,
331 if (sbuf_printf(&sb, "%.*s", (int)(sun->sun_len -
332 offsetof(struct sockaddr_un, sun_path)),
341 ret = strdup(sbuf_data(&sb), M_RPC);
348 __rpc_uaddr2taddr_af(int af, const char *uaddr)
350 struct netbuf *ret = NULL;
352 unsigned port, portlo, porthi;
353 struct sockaddr_in *sin;
355 struct sockaddr_in6 *sin6;
357 struct sockaddr_un *sun;
361 addrstr = strdup(uaddr, M_RPC);
366 * AF_LOCAL addresses are expected to be absolute
367 * pathnames, anything else will be AF_INET or AF_INET6.
369 if (*addrstr != '/') {
370 p = strrchr(addrstr, '.');
373 portlo = (unsigned)strtol(p + 1, NULL, 10);
376 p = strrchr(addrstr, '.');
379 porthi = (unsigned)strtol(p + 1, NULL, 10);
381 port = (porthi << 8) | portlo;
384 ret = (struct netbuf *)malloc(sizeof *ret, M_RPC, M_WAITOK);
390 sin = (struct sockaddr_in *)malloc(sizeof *sin, M_RPC,
394 memset(sin, 0, sizeof *sin);
395 sin->sin_family = AF_INET;
396 sin->sin_port = htons(port);
397 if (__rpc_inet_pton(AF_INET, addrstr, &sin->sin_addr) <= 0) {
403 sin->sin_len = ret->maxlen = ret->len = sizeof *sin;
408 sin6 = (struct sockaddr_in6 *)malloc(sizeof *sin6, M_RPC,
412 memset(sin6, 0, sizeof *sin6);
413 sin6->sin6_family = AF_INET6;
414 sin6->sin6_port = htons(port);
415 if (__rpc_inet_pton(AF_INET6, addrstr, &sin6->sin6_addr) <= 0) {
421 sin6->sin6_len = ret->maxlen = ret->len = sizeof *sin6;
426 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 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
702 switch (sa->sa_family) {
704 bound = (((struct sockaddr_in *) sa)->sin_port != 0);
708 bound = (((struct sockaddr_in6 *) sa)->sin6_port != 0);
713 bound = (((struct sockaddr_un *) sa)->sun_path[0] != '\0');
726 * Implement XDR-style API for RPC call.
730 CLIENT *cl, /* client handle */
731 struct rpc_callextra *ext, /* call metadata */
732 rpcproc_t proc, /* procedure number */
733 xdrproc_t xargs, /* xdr routine for args */
734 void *argsp, /* pointer to args */
735 xdrproc_t xresults, /* xdr routine for results */
736 void *resultsp, /* pointer to results */
737 struct timeval utimeout) /* seconds to wait before giving up */
744 MGET(mreq, M_WAIT, MT_DATA);
745 MCLGET(mreq, M_WAIT);
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 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
794 memset(sa, 0, sa->sa_len);
803 portrange = IP_PORTRANGE;
804 portlow = IP_PORTRANGE_LOW;
805 sin = (struct sockaddr_in *)sa;
806 portp = &sin->sin_port;
810 proto = IPPROTO_IPV6;
811 portrange = IPV6_PORTRANGE;
812 portlow = IPV6_PORTRANGE_LOW;
813 sin6 = (struct sockaddr_in6 *)sa;
814 portp = &sin6->sin6_port;
818 return (EPFNOSUPPORT);
825 bzero(&opt, sizeof(opt));
826 opt.sopt_dir = SOPT_GET;
827 opt.sopt_level = proto;
828 opt.sopt_name = portrange;
830 opt.sopt_valsize = sizeof(old);
831 error = sogetopt(so, &opt);
835 opt.sopt_dir = SOPT_SET;
836 opt.sopt_val = &portlow;
837 error = sosetopt(so, &opt);
842 error = sobind(so, sa, curthread);
846 opt.sopt_dir = SOPT_SET;
862 krpc_modevent(module_t mod, int type, void *data)
867 static moduledata_t krpc_mod = {
872 DECLARE_MODULE(krpc, krpc_mod, SI_SUB_VFS, SI_ORDER_ANY);
874 /* So that loader and kldload(2) can find us, wherever we are.. */
875 MODULE_VERSION(krpc, 1);