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 extern u_long sb_max_adj; /* not defined in socketvar.h */
68 #if __FreeBSD_version < 700000
69 #define strrchr rindex
73 NCONF_HANDLE *nhandle;
74 int nflag; /* Whether NETPATH or NETCONFIG */
78 static const struct _rpcnettype {
82 { "netpath", _RPC_NETPATH },
83 { "visible", _RPC_VISIBLE },
84 { "circuit_v", _RPC_CIRCUIT_V },
85 { "datagram_v", _RPC_DATAGRAM_V },
86 { "circuit_n", _RPC_CIRCUIT_N },
87 { "datagram_n", _RPC_DATAGRAM_N },
99 static const struct netid_af na_cvt[] = {
100 { "udp", AF_INET, IPPROTO_UDP },
101 { "tcp", AF_INET, IPPROTO_TCP },
103 { "udp6", AF_INET6, IPPROTO_UDP },
104 { "tcp6", AF_INET6, IPPROTO_TCP },
106 { "local", AF_LOCAL, 0 }
109 struct rpc_createerr rpc_createerr;
112 * Find the appropriate buffer size
116 __rpc_get_t_size(int af, int proto, int size)
122 defsize = 64 * 1024; /* XXX */
125 defsize = UDPMSGSIZE;
128 defsize = RPC_MAXDATASIZE;
134 /* Check whether the value is within the upper max limit */
135 return (size > sb_max_adj ? (u_int)sb_max_adj : (u_int)size);
139 * Find the appropriate address buffer size
147 return sizeof (struct sockaddr_in);
150 return sizeof (struct sockaddr_in6);
153 return sizeof (struct sockaddr_un);
157 return ((u_int)RPC_MAXADDRSIZE);
163 * Used to ping the NULL procedure for clnt handle.
164 * Returns NULL if fails, else a non-NULL pointer.
170 struct timeval TIMEOUT = {25, 0};
172 if (clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void, NULL,
173 (xdrproc_t) xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) {
176 return ((void *) clnt);
182 __rpc_socket2sockinfo(struct socket *so, struct __rpc_sockinfo *sip)
190 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
194 sip->si_alen = sa->sa_len;
195 family = sa->sa_family;
198 opt.sopt_dir = SOPT_GET;
199 opt.sopt_level = SOL_SOCKET;
200 opt.sopt_name = SO_TYPE;
201 opt.sopt_val = &type;
202 opt.sopt_valsize = sizeof type;
204 error = sogetopt(so, &opt);
209 if (family != AF_LOCAL) {
210 if (type == SOCK_STREAM)
212 else if (type == SOCK_DGRAM)
220 sip->si_proto = proto;
221 sip->si_socktype = type;
227 * Linear search, but the number of entries is small.
230 __rpc_nconf2sockinfo(const struct netconfig *nconf, struct __rpc_sockinfo *sip)
234 for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++)
235 if (strcmp(na_cvt[i].netid, nconf->nc_netid) == 0 || (
236 strcmp(nconf->nc_netid, "unix") == 0 &&
237 strcmp(na_cvt[i].netid, "local") == 0)) {
238 sip->si_af = na_cvt[i].af;
239 sip->si_proto = na_cvt[i].protocol;
241 __rpc_seman2socktype((int)nconf->nc_semantics);
242 if (sip->si_socktype == -1)
244 sip->si_alen = __rpc_get_a_size(sip->si_af);
252 __rpc_nconf2socket(const struct netconfig *nconf)
254 struct __rpc_sockinfo si;
258 if (!__rpc_nconf2sockinfo(nconf, &si))
262 error = socreate(si.si_af, &so, si.si_socktype, si.si_proto,
263 curthread->td_ucred, curthread);
272 taddr2uaddr(const struct netconfig *nconf, const struct netbuf *nbuf)
274 struct __rpc_sockinfo si;
276 if (!__rpc_nconf2sockinfo(nconf, &si))
278 return __rpc_taddr2uaddr_af(si.si_af, nbuf);
282 uaddr2taddr(const struct netconfig *nconf, const char *uaddr)
284 struct __rpc_sockinfo si;
286 if (!__rpc_nconf2sockinfo(nconf, &si))
288 return __rpc_uaddr2taddr_af(si.si_af, uaddr);
292 __rpc_taddr2uaddr_af(int af, const struct netbuf *nbuf)
296 struct sockaddr_in *sin;
297 struct sockaddr_un *sun;
298 char namebuf[INET_ADDRSTRLEN];
300 struct sockaddr_in6 *sin6;
301 char namebuf6[INET6_ADDRSTRLEN];
305 sbuf_new(&sb, NULL, 0, SBUF_AUTOEXTEND);
310 if (inet_ntop(af, &sin->sin_addr, namebuf, sizeof namebuf)
313 port = ntohs(sin->sin_port);
314 if (sbuf_printf(&sb, "%s.%u.%u", namebuf,
315 ((uint32_t)port) >> 8,
322 if (inet_ntop(af, &sin6->sin6_addr, namebuf6, sizeof namebuf6)
325 port = ntohs(sin6->sin6_port);
326 if (sbuf_printf(&sb, "%s.%u.%u", namebuf6,
327 ((uint32_t)port) >> 8,
334 if (sbuf_printf(&sb, "%.*s", (int)(sun->sun_len -
335 offsetof(struct sockaddr_un, sun_path)),
344 ret = strdup(sbuf_data(&sb), M_RPC);
351 __rpc_uaddr2taddr_af(int af, const char *uaddr)
353 struct netbuf *ret = NULL;
355 unsigned port, portlo, porthi;
356 struct sockaddr_in *sin;
358 struct sockaddr_in6 *sin6;
360 struct sockaddr_un *sun;
364 addrstr = strdup(uaddr, M_RPC);
369 * AF_LOCAL addresses are expected to be absolute
370 * pathnames, anything else will be AF_INET or AF_INET6.
372 if (*addrstr != '/') {
373 p = strrchr(addrstr, '.');
376 portlo = (unsigned)strtol(p + 1, NULL, 10);
379 p = strrchr(addrstr, '.');
382 porthi = (unsigned)strtol(p + 1, NULL, 10);
384 port = (porthi << 8) | portlo;
387 ret = (struct netbuf *)malloc(sizeof *ret, M_RPC, M_WAITOK);
393 sin = (struct sockaddr_in *)malloc(sizeof *sin, M_RPC,
397 memset(sin, 0, sizeof *sin);
398 sin->sin_family = AF_INET;
399 sin->sin_port = htons(port);
400 if (inet_pton(AF_INET, addrstr, &sin->sin_addr) <= 0) {
406 sin->sin_len = ret->maxlen = ret->len = sizeof *sin;
411 sin6 = (struct sockaddr_in6 *)malloc(sizeof *sin6, M_RPC,
415 memset(sin6, 0, sizeof *sin6);
416 sin6->sin6_family = AF_INET6;
417 sin6->sin6_port = htons(port);
418 if (inet_pton(AF_INET6, addrstr, &sin6->sin6_addr) <= 0) {
424 sin6->sin6_len = ret->maxlen = ret->len = sizeof *sin6;
429 sun = (struct sockaddr_un *)malloc(sizeof *sun, M_RPC,
433 memset(sun, 0, sizeof *sun);
434 sun->sun_family = AF_LOCAL;
435 strncpy(sun->sun_path, addrstr, sizeof(sun->sun_path) - 1);
436 ret->len = ret->maxlen = sun->sun_len = SUN_LEN(sun);
443 free(addrstr, M_RPC);
448 __rpc_seman2socktype(int semantics)
453 case NC_TPI_COTS_ORD:
465 __rpc_socktype2seman(int socktype)
471 return NC_TPI_COTS_ORD;
482 * Returns the type of the network as defined in <rpc/nettype.h>
483 * If nettype is NULL, it defaults to NETPATH.
486 getnettype(const char *nettype)
490 if ((nettype == NULL) || (nettype[0] == 0)) {
491 return (_RPC_NETPATH); /* Default */
495 nettype = strlocase(nettype);
497 for (i = 0; _rpctypelist[i].name; i++)
498 if (strcasecmp(nettype, _rpctypelist[i].name) == 0) {
499 return (_rpctypelist[i].type);
501 return (_rpctypelist[i].type);
505 * For the given nettype (tcp or udp only), return the first structure found.
506 * This should be freed by calling freenetconfigent()
509 __rpc_getconfip(const char *nettype)
512 static char *netid_tcp = (char *) NULL;
513 static char *netid_udp = (char *) NULL;
514 struct netconfig *dummy;
516 if (!netid_udp && !netid_tcp) {
517 struct netconfig *nconf;
520 if (!(confighandle = setnetconfig())) {
521 log(LOG_ERR, "rpc: failed to open " NETCONFIG);
524 while ((nconf = getnetconfig(confighandle)) != NULL) {
525 if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
526 if (strcmp(nconf->nc_proto, NC_TCP) == 0) {
527 netid_tcp = strdup(nconf->nc_netid,
530 if (strcmp(nconf->nc_proto, NC_UDP) == 0) {
531 netid_udp = strdup(nconf->nc_netid,
536 endnetconfig(confighandle);
538 if (strcmp(nettype, "udp") == 0)
540 else if (strcmp(nettype, "tcp") == 0)
545 if ((netid == NULL) || (netid[0] == 0)) {
548 dummy = getnetconfigent(netid);
553 * Returns the type of the nettype, which should then be used with
556 * For simplicity in the kernel, we don't support the NETPATH
557 * environment variable. We behave as userland would then NETPATH is
558 * unset, i.e. iterate over all visible entries in netconfig.
561 __rpc_setconf(nettype)
564 struct handle *handle;
566 handle = (struct handle *) malloc(sizeof (struct handle),
568 switch (handle->nettype = getnettype(nettype)) {
571 case _RPC_DATAGRAM_N:
572 if (!(handle->nhandle = setnetconfig()))
574 handle->nflag = TRUE;
578 case _RPC_DATAGRAM_V:
581 if (!(handle->nhandle = setnetconfig())) {
582 log(LOG_ERR, "rpc: failed to open " NETCONFIG);
585 handle->nflag = FALSE;
599 * Returns the next netconfig struct for the given "net" type.
600 * __rpc_setconf() should have been called previously.
603 __rpc_getconf(void *vhandle)
605 struct handle *handle;
606 struct netconfig *nconf;
608 handle = (struct handle *)vhandle;
609 if (handle == NULL) {
614 nconf = getnetconfig(handle->nhandle);
615 if (nconf && !(nconf->nc_flag & NC_VISIBLE))
618 nconf = getnetconfig(handle->nhandle);
622 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
623 (nconf->nc_semantics != NC_TPI_COTS) &&
624 (nconf->nc_semantics != NC_TPI_COTS_ORD))
626 switch (handle->nettype) {
628 if (!(nconf->nc_flag & NC_VISIBLE))
631 case _RPC_NETPATH: /* Be happy */
634 if (!(nconf->nc_flag & NC_VISIBLE))
638 if ((nconf->nc_semantics != NC_TPI_COTS) &&
639 (nconf->nc_semantics != NC_TPI_COTS_ORD))
642 case _RPC_DATAGRAM_V:
643 if (!(nconf->nc_flag & NC_VISIBLE))
646 case _RPC_DATAGRAM_N:
647 if (nconf->nc_semantics != NC_TPI_CLTS)
651 if (((nconf->nc_semantics != NC_TPI_COTS) &&
652 (nconf->nc_semantics != NC_TPI_COTS_ORD)) ||
653 (strcmp(nconf->nc_protofmly, NC_INET)
655 && strcmp(nconf->nc_protofmly, NC_INET6))
660 strcmp(nconf->nc_proto, NC_TCP))
664 if ((nconf->nc_semantics != NC_TPI_CLTS) ||
665 (strcmp(nconf->nc_protofmly, NC_INET)
667 && strcmp(nconf->nc_protofmly, NC_INET6))
672 strcmp(nconf->nc_proto, NC_UDP))
682 __rpc_endconf(vhandle)
685 struct handle *handle;
687 handle = (struct handle *) vhandle;
688 if (handle == NULL) {
691 endnetconfig(handle->nhandle);
696 __rpc_sockisbound(struct socket *so)
701 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
705 switch (sa->sa_family) {
707 bound = (((struct sockaddr_in *) sa)->sin_port != 0);
711 bound = (((struct sockaddr_in6 *) sa)->sin6_port != 0);
716 bound = (((struct sockaddr_un *) sa)->sun_path[0] != '\0');
729 * Implement XDR-style API for RPC call.
733 CLIENT *cl, /* client handle */
734 struct rpc_callextra *ext, /* call metadata */
735 rpcproc_t proc, /* procedure number */
736 xdrproc_t xargs, /* xdr routine for args */
737 void *argsp, /* pointer to args */
738 xdrproc_t xresults, /* xdr routine for results */
739 void *resultsp, /* pointer to results */
740 struct timeval utimeout) /* seconds to wait before giving up */
747 MGET(mreq, M_WAIT, MT_DATA);
748 MCLGET(mreq, M_WAIT);
751 xdrmbuf_create(&xdrs, mreq, XDR_ENCODE);
752 if (!xargs(&xdrs, argsp)) {
754 return (RPC_CANTENCODEARGS);
758 stat = CLNT_CALL_MBUF(cl, ext, proc, mreq, &mrep, utimeout);
761 if (stat == RPC_SUCCESS) {
762 xdrmbuf_create(&xdrs, mrep, XDR_DECODE);
763 if (!xresults(&xdrs, resultsp)) {
765 return (RPC_CANTDECODERES);
774 * Bind a socket to a privileged IP port
777 bindresvport(struct socket *so, struct sockaddr *sa)
780 bool_t freesa = FALSE;
781 struct sockaddr_in *sin;
783 struct sockaddr_in6 *sin6;
786 int proto, portrange, portlow;
791 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
797 memset(sa, 0, sa->sa_len);
806 portrange = IP_PORTRANGE;
807 portlow = IP_PORTRANGE_LOW;
808 sin = (struct sockaddr_in *)sa;
809 portp = &sin->sin_port;
813 proto = IPPROTO_IPV6;
814 portrange = IPV6_PORTRANGE;
815 portlow = IPV6_PORTRANGE_LOW;
816 sin6 = (struct sockaddr_in6 *)sa;
817 portp = &sin6->sin6_port;
821 return (EPFNOSUPPORT);
828 CURVNET_SET(so->so_vnet);
829 bzero(&opt, sizeof(opt));
830 opt.sopt_dir = SOPT_GET;
831 opt.sopt_level = proto;
832 opt.sopt_name = portrange;
834 opt.sopt_valsize = sizeof(old);
835 error = sogetopt(so, &opt);
841 opt.sopt_dir = SOPT_SET;
842 opt.sopt_val = &portlow;
843 error = sosetopt(so, &opt);
849 error = sobind(so, sa, curthread);
853 opt.sopt_dir = SOPT_SET;
855 CURVNET_SET(so->so_vnet);
871 krpc_modevent(module_t mod, int type, void *data)
876 static moduledata_t krpc_mod = {
881 DECLARE_MODULE(krpc, krpc_mod, SI_SUB_VFS, SI_ORDER_ANY);
883 /* So that loader and kldload(2) can find us, wherever we are.. */
884 MODULE_VERSION(krpc, 1);