2 * Copyright (c) 2009, Sun Microsystems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
32 #include <sys/cdefs.h>
35 * rpc_generic.c, Miscl routines for RPC.
39 #include <reentrant.h>
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/socket.h>
45 #include <sys/resource.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
53 #include <netconfig.h>
57 #include <rpc/nettype.h>
61 NCONF_HANDLE *nhandle;
62 int nflag; /* Whether NETPATH or NETCONFIG */
66 static const struct _rpcnettype {
70 { "netpath", _RPC_NETPATH },
71 { "visible", _RPC_VISIBLE },
72 { "circuit_v", _RPC_CIRCUIT_V },
73 { "datagram_v", _RPC_DATAGRAM_V },
74 { "circuit_n", _RPC_CIRCUIT_N },
75 { "datagram_n", _RPC_DATAGRAM_N },
87 static const struct netid_af na_cvt[] = {
88 { "udp", AF_INET, IPPROTO_UDP },
89 { "tcp", AF_INET, IPPROTO_TCP },
91 { "udp6", AF_INET6, IPPROTO_UDP },
92 { "tcp6", AF_INET6, IPPROTO_TCP },
94 { "local", AF_LOCAL, 0 }
98 static char *strlocase(char *);
100 static int getnettype(const char *);
103 * Cache the result of getrlimit(), so we don't have to do an
104 * expensive call every time.
115 if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
116 return (tbsize = (int)rl.rlim_max);
119 * Something wrong. I'll try to save face by returning a
120 * pessimistic number.
127 * Find the appropriate buffer size
131 __rpc_get_t_size(af, proto, size)
133 int size; /* Size requested */
135 int maxsize, defsize;
137 maxsize = 256 * 1024; /* XXX */
140 defsize = 64 * 1024; /* XXX */
143 defsize = UDPMSGSIZE;
146 defsize = RPC_MAXDATASIZE;
152 /* Check whether the value is within the upper max limit */
153 return (size > maxsize ? (u_int)maxsize : (u_int)size);
157 * Find the appropriate address buffer size
165 return sizeof (struct sockaddr_in);
168 return sizeof (struct sockaddr_in6);
171 return sizeof (struct sockaddr_un);
175 return ((u_int)RPC_MAXADDRSIZE);
193 * Returns the type of the network as defined in <rpc/nettype.h>
194 * If nettype is NULL, it defaults to NETPATH.
202 if ((nettype == NULL) || (nettype[0] == 0)) {
203 return (_RPC_NETPATH); /* Default */
207 nettype = strlocase(nettype);
209 for (i = 0; _rpctypelist[i].name; i++)
210 if (strcasecmp(nettype, _rpctypelist[i].name) == 0) {
211 return (_rpctypelist[i].type);
213 return (_rpctypelist[i].type);
217 * For the given nettype (tcp or udp only), return the first structure found.
218 * This should be freed by calling freenetconfigent()
221 __rpc_getconfip(nettype)
225 char *netid_tcp = (char *) NULL;
226 char *netid_udp = (char *) NULL;
227 struct netconfig *dummy;
228 extern thread_key_t tcp_key, udp_key;
229 extern mutex_t tsd_lock;
231 if (tcp_key == KEY_INITIALIZER) {
232 mutex_lock(&tsd_lock);
233 if (tcp_key == KEY_INITIALIZER)
234 thr_keycreate(&tcp_key, free);
235 mutex_unlock(&tsd_lock);
237 netid_tcp = (char *)thr_getspecific(tcp_key);
238 if (udp_key == KEY_INITIALIZER) {
239 mutex_lock(&tsd_lock);
240 if (udp_key == KEY_INITIALIZER)
241 thr_keycreate(&udp_key, free);
242 mutex_unlock(&tsd_lock);
244 netid_udp = (char *)thr_getspecific(udp_key);
245 if (!netid_udp && !netid_tcp) {
246 struct netconfig *nconf;
249 if (!(confighandle = setnetconfig())) {
250 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
253 while ((nconf = getnetconfig(confighandle)) != NULL) {
254 if (strcmp(nconf->nc_protofmly, NC_INET) == 0 ||
255 strcmp(nconf->nc_protofmly, NC_INET6) == 0) {
256 if (strcmp(nconf->nc_proto, NC_TCP) == 0 &&
258 netid_tcp = strdup(nconf->nc_netid);
259 thr_setspecific(tcp_key,
262 if (strcmp(nconf->nc_proto, NC_UDP) == 0 &&
264 netid_udp = strdup(nconf->nc_netid);
265 thr_setspecific(udp_key,
270 endnetconfig(confighandle);
272 if (strcmp(nettype, "udp") == 0)
274 else if (strcmp(nettype, "tcp") == 0)
279 if ((netid == NULL) || (netid[0] == 0)) {
282 dummy = getnetconfigent(netid);
287 * Returns the type of the nettype, which should then be used with
291 __rpc_setconf(nettype)
294 struct handle *handle;
296 handle = (struct handle *) malloc(sizeof (struct handle));
297 if (handle == NULL) {
300 switch (handle->nettype = getnettype(nettype)) {
303 case _RPC_DATAGRAM_N:
304 if (!(handle->nhandle = setnetpath())) {
308 handle->nflag = TRUE;
312 case _RPC_DATAGRAM_V:
315 if (!(handle->nhandle = setnetconfig())) {
316 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
320 handle->nflag = FALSE;
330 * Returns the next netconfig struct for the given "net" type.
331 * __rpc_setconf() should have been called previously.
334 __rpc_getconf(vhandle)
337 struct handle *handle;
338 struct netconfig *nconf;
340 handle = (struct handle *)vhandle;
341 if (handle == NULL) {
346 nconf = getnetpath(handle->nhandle);
348 nconf = getnetconfig(handle->nhandle);
351 if ((nconf->nc_semantics != NC_TPI_CLTS) &&
352 (nconf->nc_semantics != NC_TPI_COTS) &&
353 (nconf->nc_semantics != NC_TPI_COTS_ORD))
355 switch (handle->nettype) {
357 if (!(nconf->nc_flag & NC_VISIBLE))
360 case _RPC_NETPATH: /* Be happy */
363 if (!(nconf->nc_flag & NC_VISIBLE))
367 if ((nconf->nc_semantics != NC_TPI_COTS) &&
368 (nconf->nc_semantics != NC_TPI_COTS_ORD))
371 case _RPC_DATAGRAM_V:
372 if (!(nconf->nc_flag & NC_VISIBLE))
375 case _RPC_DATAGRAM_N:
376 if (nconf->nc_semantics != NC_TPI_CLTS)
380 if (((nconf->nc_semantics != NC_TPI_COTS) &&
381 (nconf->nc_semantics != NC_TPI_COTS_ORD)) ||
382 (strcmp(nconf->nc_protofmly, NC_INET)
384 && strcmp(nconf->nc_protofmly, NC_INET6))
389 strcmp(nconf->nc_proto, NC_TCP))
393 if ((nconf->nc_semantics != NC_TPI_CLTS) ||
394 (strcmp(nconf->nc_protofmly, NC_INET)
396 && strcmp(nconf->nc_protofmly, NC_INET6))
401 strcmp(nconf->nc_proto, NC_UDP))
411 __rpc_endconf(vhandle)
414 struct handle *handle;
416 handle = (struct handle *) vhandle;
417 if (handle == NULL) {
421 endnetpath(handle->nhandle);
423 endnetconfig(handle->nhandle);
429 * Used to ping the NULL procedure for clnt handle.
430 * Returns NULL if fails, else a non-NULL pointer.
436 struct timeval TIMEOUT = {25, 0};
438 if (clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void, NULL,
439 (xdrproc_t) xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) {
442 return ((void *) clnt);
446 * Try all possible transports until
447 * one succeeds in finding the netconf for the given fd.
454 struct __rpc_sockinfo si;
456 if (!__rpc_fd2sockinfo(fd, &si))
459 if (!__rpc_sockinfo2netid(&si, &netid))
462 /*LINTED const castaway*/
463 return getnetconfigent((char *)netid);
467 __rpc_fd2sockinfo(int fd, struct __rpc_sockinfo *sip)
471 struct sockaddr_storage ss;
474 if (getsockname(fd, (struct sockaddr *)&ss, &len) < 0)
479 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &len) < 0)
483 if (ss.ss_family != AF_LOCAL) {
484 if (type == SOCK_STREAM)
486 else if (type == SOCK_DGRAM)
493 sip->si_af = ss.ss_family;
494 sip->si_proto = proto;
495 sip->si_socktype = type;
501 * Linear search, but the number of entries is small.
504 __rpc_nconf2sockinfo(const struct netconfig *nconf, struct __rpc_sockinfo *sip)
508 for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++)
509 if (strcmp(na_cvt[i].netid, nconf->nc_netid) == 0 || (
510 strcmp(nconf->nc_netid, "unix") == 0 &&
511 strcmp(na_cvt[i].netid, "local") == 0)) {
512 sip->si_af = na_cvt[i].af;
513 sip->si_proto = na_cvt[i].protocol;
515 __rpc_seman2socktype((int)nconf->nc_semantics);
516 if (sip->si_socktype == -1)
518 sip->si_alen = __rpc_get_a_size(sip->si_af);
526 __rpc_nconf2fd_flags(const struct netconfig *nconf, int flags)
528 struct __rpc_sockinfo si;
531 if (!__rpc_nconf2sockinfo(nconf, &si))
534 if ((fd = socket(si.si_af, si.si_socktype | flags, si.si_proto)) >= 0 &&
535 si.si_af == AF_INET6) {
539 setsockopt(fd, SOL_IPV6, IPV6_V6ONLY, &val, sizeof(val));
546 __rpc_nconf2fd(const struct netconfig *nconf)
548 return __rpc_nconf2fd_flags(nconf, 0);
552 __rpc_sockinfo2netid(struct __rpc_sockinfo *sip, const char **netid)
555 struct netconfig *nconf;
557 nconf = getnetconfigent("local");
559 for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++) {
560 if (na_cvt[i].af == sip->si_af &&
561 na_cvt[i].protocol == sip->si_proto) {
562 if (strcmp(na_cvt[i].netid, "local") == 0 && nconf == NULL) {
567 *netid = na_cvt[i].netid;
570 freenetconfigent(nconf);
575 freenetconfigent(nconf);
581 taddr2uaddr(const struct netconfig *nconf, const struct netbuf *nbuf)
583 struct __rpc_sockinfo si;
585 if (!__rpc_nconf2sockinfo(nconf, &si))
587 return __rpc_taddr2uaddr_af(si.si_af, nbuf);
591 uaddr2taddr(const struct netconfig *nconf, const char *uaddr)
593 struct __rpc_sockinfo si;
595 if (!__rpc_nconf2sockinfo(nconf, &si))
597 return __rpc_uaddr2taddr_af(si.si_af, uaddr);
601 __rpc_taddr2uaddr_af(int af, const struct netbuf *nbuf)
604 struct sockaddr_in *sin;
605 struct sockaddr_un *sun;
606 char namebuf[INET_ADDRSTRLEN];
608 struct sockaddr_in6 *sin6;
609 char namebuf6[INET6_ADDRSTRLEN];
619 if (inet_ntop(af, &sin->sin_addr, namebuf, sizeof namebuf)
622 port = ntohs(sin->sin_port);
623 if (asprintf(&ret, "%s.%u.%u", namebuf, ((u_int32_t)port) >> 8,
630 if (inet_ntop(af, &sin6->sin6_addr, namebuf6, sizeof namebuf6)
633 port = ntohs(sin6->sin6_port);
634 if (asprintf(&ret, "%s.%u.%u", namebuf6, ((u_int32_t)port) >> 8,
641 /* if (asprintf(&ret, "%.*s", (int)(sun->sun_len -
642 offsetof(struct sockaddr_un, sun_path)),
643 sun->sun_path) < 0)*/
644 if (asprintf(&ret, "%.*s", (int)(sizeof(*sun) -
645 offsetof(struct sockaddr_un, sun_path)),
658 __rpc_uaddr2taddr_af(int af, const char *uaddr)
660 struct netbuf *ret = NULL;
662 unsigned port, portlo, porthi;
663 struct sockaddr_in *sin;
665 struct sockaddr_in6 *sin6;
667 struct sockaddr_un *sun;
671 addrstr = strdup(uaddr);
676 * AF_LOCAL addresses are expected to be absolute
677 * pathnames, anything else will be AF_INET or AF_INET6.
679 if (*addrstr != '/') {
680 p = strrchr(addrstr, '.');
683 portlo = (unsigned)atoi(p + 1);
686 p = strrchr(addrstr, '.');
689 porthi = (unsigned)atoi(p + 1);
691 port = (porthi << 8) | portlo;
694 ret = (struct netbuf *)malloc(sizeof *ret);
700 sin = (struct sockaddr_in *)malloc(sizeof *sin);
703 memset(sin, 0, sizeof *sin);
704 sin->sin_family = AF_INET;
705 sin->sin_port = htons(port);
706 if (inet_pton(AF_INET, addrstr, &sin->sin_addr) <= 0) {
712 ret->maxlen = ret->len = sizeof *sin;
717 sin6 = (struct sockaddr_in6 *)malloc(sizeof *sin6);
720 memset(sin6, 0, sizeof *sin6);
721 sin6->sin6_family = AF_INET6;
722 sin6->sin6_port = htons(port);
723 if (inet_pton(AF_INET6, addrstr, &sin6->sin6_addr) <= 0) {
729 ret->maxlen = ret->len = sizeof *sin6;
734 sun = (struct sockaddr_un *)malloc(sizeof *sun);
737 memset(sun, 0, sizeof *sun);
738 sun->sun_family = AF_LOCAL;
739 strncpy(sun->sun_path, addrstr, sizeof(sun->sun_path) - 1);
740 ret->len = SUN_LEN(sun);
741 ret->maxlen = sizeof(struct sockaddr_un);
753 __rpc_seman2socktype(int semantics)
758 case NC_TPI_COTS_ORD:
770 __rpc_socktype2seman(int socktype)
776 return NC_TPI_COTS_ORD;
787 * XXXX - IPv6 scope IDs can't be handled in universal addresses.
788 * Here, we compare the original server address to that of the RPC
789 * service we just received back from a call to rpcbind on the remote
790 * machine. If they are both "link local" or "site local", copy
791 * the scope id of the server address over to the service address.
794 __rpc_fixup_addr(struct netbuf *new, const struct netbuf *svc)
797 struct sockaddr *sa_new, *sa_svc;
798 struct sockaddr_in6 *sin6_new, *sin6_svc;
800 sa_svc = (struct sockaddr *)svc->buf;
801 sa_new = (struct sockaddr *)new->buf;
803 if (sa_new->sa_family == sa_svc->sa_family &&
804 sa_new->sa_family == AF_INET6) {
805 sin6_new = (struct sockaddr_in6 *)new->buf;
806 sin6_svc = (struct sockaddr_in6 *)svc->buf;
808 if ((IN6_IS_ADDR_LINKLOCAL(&sin6_new->sin6_addr) &&
809 IN6_IS_ADDR_LINKLOCAL(&sin6_svc->sin6_addr)) ||
810 (IN6_IS_ADDR_SITELOCAL(&sin6_new->sin6_addr) &&
811 IN6_IS_ADDR_SITELOCAL(&sin6_svc->sin6_addr))) {
812 sin6_new->sin6_scope_id = sin6_svc->sin6_scope_id;
820 __rpc_sockisbound(int fd)
822 struct sockaddr_storage ss;
824 struct sockaddr_in sin;
825 struct sockaddr_in6 sin6;
826 struct sockaddr_un usin;
830 slen = sizeof (struct sockaddr_storage);
831 if (getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0)
834 switch (ss.ss_family) {
836 memcpy(&u_addr.sin, &ss, sizeof(u_addr.sin));
837 return (u_addr.sin.sin_port != 0);
840 memcpy(&u_addr.sin6, &ss, sizeof(u_addr.sin6));
841 return (u_addr.sin6.sin6_port != 0);
845 memcpy(&u_addr.usin, &ss, sizeof(u_addr.usin));
846 return (u_addr.usin.sun_path[0] != 0);
855 * Helper function to set up a netbuf
858 __rpc_set_netbuf(struct netbuf *nb, const void *ptr, size_t len)
860 if (nb->len != len) {
862 mem_free(nb->buf, nb->len);
863 nb->buf = mem_alloc(len);
867 nb->maxlen = nb->len = len;
869 memcpy(nb->buf, ptr, len);