2 * Provide a variety of APIs that query an rpcbind daemon to
3 * discover RPC service ports and allowed protocol version
6 * Copyright (C) 2008 Oracle Corporation. All rights reserved.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 021110-1307, USA.
29 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
38 #include <arpa/inet.h>
41 #include <rpc/pmap_prot.h>
44 #include <netconfig.h>
45 #include <rpc/rpcb_prot.h>
52 * Try a local socket first to access the local rpcbind daemon
54 * Rpcbind's local socket service does not seem to be working.
55 * Disable this logic for now.
59 #else /* !HAVE_LIBTIRPC */
61 #endif /* !HAVE_LIBTIRPC */
64 static const rpcvers_t default_rpcb_version = RPCBVERS_4;
65 #else /* !HAVE_LIBTIRPC */
66 static const rpcvers_t default_rpcb_version = PMAPVERS;
67 #endif /* !HAVE_LIBTIRPC */
70 * Historical: Map TCP connect timeouts to timeout
71 * error code used by UDP.
74 nfs_gp_map_tcp_errorcodes(const unsigned short protocol)
76 if (protocol != IPPROTO_TCP)
79 switch (rpc_createerr.cf_error.re_errno) {
81 rpc_createerr.cf_stat = RPC_TIMEDOUT;
84 rpc_createerr.cf_stat = RPC_CANTRECV;
90 * There's no easy way to tell how the local system's networking
91 * and rpcbind is configured (ie. whether we want to use IPv6 or
92 * IPv4 loopback to contact RPC services on the local host). We
93 * punt and simply try to look up "localhost".
95 * Returns TRUE on success.
97 static int nfs_gp_loopback_address(struct sockaddr *sap, socklen_t *salen)
99 struct addrinfo *gai_results;
102 if (getaddrinfo("localhost", NULL, NULL, &gai_results))
105 if (*salen >= gai_results->ai_addrlen) {
106 memcpy(sap, gai_results->ai_addr,
107 gai_results->ai_addrlen);
108 *salen = gai_results->ai_addrlen;
112 freeaddrinfo(gai_results);
117 * Look up a network service in /etc/services and return the
118 * network-order port number of that service.
120 static in_port_t nfs_gp_getservbyname(const char *service,
121 const unsigned short protocol)
123 const struct addrinfo gai_hint = {
124 .ai_family = AF_INET,
125 .ai_protocol = protocol,
126 .ai_flags = AI_PASSIVE,
128 struct addrinfo *gai_results;
129 const struct sockaddr_in *sin;
132 if (getaddrinfo(NULL, service, &gai_hint, &gai_results) != 0)
135 sin = (const struct sockaddr_in *)gai_results->ai_addr;
136 port = sin->sin_port;
138 freeaddrinfo(gai_results);
143 * Discover the port number that should be used to contact an
144 * rpcbind service. This will detect if the port has a local
145 * value that may have been set in /etc/services.
147 * Returns network byte-order port number of rpcbind service
150 static in_port_t nfs_gp_get_rpcb_port(const unsigned short protocol)
152 static const char *rpcb_netnametbl[] = {
160 for (i = 0; rpcb_netnametbl[i] != NULL; i++) {
163 port = nfs_gp_getservbyname(rpcb_netnametbl[i], protocol);
168 return (in_port_t)htons((uint16_t)PMAPPORT);
172 * Set up an RPC client for communicating with an rpcbind daemon at
173 * @sap over @transport with protocol version @version.
175 * Returns a pointer to a prepared RPC client if successful, and
176 * @timeout is initialized; caller must destroy a non-NULL returned RPC
177 * client. Otherwise returns NULL, and rpc_createerr.cf_stat is set to
180 static CLIENT *nfs_gp_get_rpcbclient(struct sockaddr *sap,
181 const socklen_t salen,
182 const unsigned short transport,
183 const rpcvers_t version,
184 struct timeval *timeout)
186 static const char *rpcb_pgmtbl[] = {
193 rpcprog_t rpcb_prog = nfs_getrpcbyname(RPCBPROG, rpcb_pgmtbl);
196 nfs_set_port(sap, ntohs(nfs_gp_get_rpcb_port(transport)));
197 clnt = nfs_get_rpcclient(sap, salen, transport, rpcb_prog,
199 nfs_gp_map_tcp_errorcodes(transport);
204 * nfs_get_proto - Convert a netid to an address family and protocol number
205 * @netid: C string containing a netid
206 * @family: OUT: address family
207 * @protocol: OUT: protocol number
209 * Returns 1 and fills in @protocol if the netid was recognized;
210 * otherwise zero is returned.
214 nfs_get_proto(const char *netid, sa_family_t *family, unsigned long *protocol)
216 struct netconfig *nconf;
217 struct protoent *proto;
219 nconf = getnetconfigent(netid);
223 proto = getprotobyname(nconf->nc_proto);
225 freenetconfigent(nconf);
230 if (strcmp(nconf->nc_protofmly, NC_INET) == 0)
232 if (strcmp(nconf->nc_protofmly, NC_INET6) == 0)
234 freenetconfigent(nconf);
236 *protocol = (unsigned long)proto->p_proto;
239 #else /* !HAVE_LIBTIRPC */
241 nfs_get_proto(const char *netid, sa_family_t *family, unsigned long *protocol)
243 struct protoent *proto;
245 proto = getprotobyname(netid);
250 *protocol = (unsigned long)proto->p_proto;
253 #endif /* !HAVE_LIBTIRPC */
256 * nfs_get_netid - Convert a protocol family and protocol name to a netid
257 * @family: protocol family
258 * @protocol: protocol number
260 * One of the arguments passed when querying remote rpcbind services
261 * via rpcbind v3 or v4 is a netid string. This replaces the pm_prot
262 * field used in legacy PMAP_GETPORT calls.
264 * RFC 1833 says netids are not standard but rather defined on the local
265 * host. There are, however, standard definitions for nc_protofmly and
266 * nc_proto that can be used to derive a netid string on the local host,
267 * based on the contents of /etc/netconfig.
269 * Walk through the local netconfig database and grab the netid of the
270 * first entry that matches @family and @protocol and whose netid string
271 * fits in the provided buffer.
273 * Returns a '\0'-terminated string if successful. Caller must
274 * free the returned string. Otherwise NULL is returned, and
275 * rpc_createerr.cf_stat is set to reflect the error.
278 char *nfs_get_netid(const sa_family_t family, const unsigned long protocol)
280 char *nc_protofmly, *nc_proto, *nc_netid;
281 struct netconfig *nconf;
282 struct protoent *proto;
288 nc_protofmly = NC_INET;
291 nc_protofmly = NC_INET6;
297 proto = getprotobynumber(protocol);
300 nc_proto = proto->p_name;
302 handle = setnetconfig();
303 while ((nconf = getnetconfig(handle)) != NULL) {
305 if (nconf->nc_protofmly != NULL &&
306 strcmp(nconf->nc_protofmly, nc_protofmly) != 0)
308 if (nconf->nc_proto != NULL &&
309 strcmp(nconf->nc_proto, nc_proto) != 0)
312 nc_netid = strdup(nconf->nc_netid);
313 endnetconfig(handle);
315 if (nc_netid == NULL)
316 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
319 endnetconfig(handle);
322 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
325 #else /* !HAVE_LIBTIRPC */
326 char *nfs_get_netid(const sa_family_t family, const unsigned long protocol)
328 struct protoent *proto;
331 if (family != AF_INET)
333 proto = getprotobynumber((int)protocol);
337 netid = strdup(proto->p_name);
339 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
343 rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
346 #endif /* !HAVE_LIBTIRPC */
349 * Extract a port number from a universal address, and terminate the
350 * string in @addrstr just after the address part.
352 * Returns -1 if unsuccesful; otherwise a decoded port number (possibly 0)
355 static int nfs_gp_universal_porthelper(char *addrstr)
358 unsigned long portlo, porthi;
361 p = strrchr(addrstr, '.');
364 portlo = strtoul(p + 1, &endptr, 10);
365 if (*endptr != '\0' || portlo > 255)
369 p = strrchr(addrstr, '.');
372 porthi = strtoul(p + 1, &endptr, 10);
373 if (*endptr != '\0' || porthi > 255)
376 port = (porthi << 8) | portlo;
383 * nfs_universal2port - extract port number from a "universal address"
384 * @uaddr: '\0'-terminated C string containing a universal address
386 * Universal addresses (defined in RFC 1833) are used when calling an
387 * rpcbind daemon via protocol versions 3 or 4..
389 * Returns -1 if unsuccesful; otherwise a decoded port number (possibly 0)
392 int nfs_universal2port(const char *uaddr)
397 addrstr = strdup(uaddr);
398 if (addrstr != NULL) {
399 port = nfs_gp_universal_porthelper(addrstr);
406 * nfs_sockaddr2universal - convert a sockaddr to a "universal address"
407 * @sap: pointer to a socket address
409 * Universal addresses (defined in RFC 1833) are used when calling an
410 * rpcbind daemon via protocol versions 3 or 4..
412 * Returns a '\0'-terminated string if successful; caller must free
413 * the returned string. Otherwise NULL is returned and
414 * rpc_createerr.cf_stat is set to reflect the error.
416 * inet_ntop(3) is used here, since getnameinfo(3) is not available
417 * in some earlier glibc releases, and we don't require support for
418 * scope IDs for universal addresses.
420 char *nfs_sockaddr2universal(const struct sockaddr *sap)
422 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sap;
423 const struct sockaddr_un *sun = (const struct sockaddr_un *)sap;
424 const struct sockaddr_in *sin = (const struct sockaddr_in *)sap;
425 char buf[INET6_ADDRSTRLEN + 8 /* for port information */];
431 switch (sap->sa_family) {
433 return strndup(sun->sun_path, sizeof(sun->sun_path));
435 if (inet_ntop(AF_INET, (const void *)&sin->sin_addr.s_addr,
436 buf, (socklen_t)sizeof(buf)) == NULL)
438 port = ntohs(sin->sin_port);
441 if (inet_ntop(AF_INET6, (const void *)&sin6->sin6_addr,
442 buf, (socklen_t)sizeof(buf)) == NULL)
444 port = ntohs(sin6->sin6_port);
450 count = sizeof(buf) - strlen(buf);
451 len = snprintf(buf + strlen(buf), count, ".%u.%u",
452 (unsigned)(port >> 8), (unsigned)(port & 0xff));
453 /* before glibc 2.0.6, snprintf(3) could return -1 */
454 if (len < 0 || (size_t)len > count)
457 result = strdup(buf);
462 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
467 * Send a NULL request to the indicated RPC service.
469 * Returns 1 if the service responded; otherwise 0;
471 static int nfs_gp_ping(CLIENT *client, struct timeval timeout)
473 enum clnt_stat status;
475 status = CLNT_CALL(client, NULLPROC,
476 (xdrproc_t)xdr_void, NULL,
477 (xdrproc_t)xdr_void, NULL,
480 if (status != RPC_SUCCESS) {
481 rpc_createerr.cf_stat = status;
482 CLNT_GETERR(client, &rpc_createerr.cf_error);
484 return (int)(status == RPC_SUCCESS);
490 * Initialize the rpcb argument for a GETADDR request.
492 * Returns 1 if successful, and caller must free strings pointed
493 * to by r_netid and r_addr; otherwise 0.
495 static int nfs_gp_init_rpcb_parms(const struct sockaddr *sap,
496 const rpcprog_t program,
497 const rpcvers_t version,
498 const unsigned short protocol,
503 netid = nfs_get_netid(sap->sa_family, protocol);
507 addr = nfs_sockaddr2universal(sap);
513 memset(parms, 0, sizeof(*parms));
514 parms->r_prog = program;
515 parms->r_vers = version;
516 parms->r_netid = netid;
517 parms->r_addr = addr;
523 static void nfs_gp_free_rpcb_parms(struct rpcb *parms)
525 free(parms->r_netid);
530 * Try rpcbind GETADDR via version 4. If that fails, try same
531 * request via version 3.
533 * Returns non-zero port number on success; otherwise returns
534 * zero. rpccreateerr is set to reflect the nature of the error.
536 static unsigned short nfs_gp_rpcb_getaddr(CLIENT *client,
538 struct timeval timeout)
540 rpcvers_t rpcb_version;
541 struct rpc_err rpcerr;
544 for (rpcb_version = RPCBVERS_4;
545 rpcb_version >= RPCBVERS_3;
547 enum clnt_stat status;
550 CLNT_CONTROL(client, CLSET_VERS, (void *)&rpcb_version);
551 status = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDR,
552 (xdrproc_t)xdr_rpcb, (void *)parms,
553 (xdrproc_t)xdr_wrapstring, (void *)&uaddr,
558 if ((uaddr == NULL) || (uaddr[0] == '\0')) {
559 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
563 port = nfs_universal2port(uaddr);
564 xdr_free((xdrproc_t)xdr_wrapstring, (char *)&uaddr);
566 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
569 return (unsigned short)port;
570 case RPC_PROGVERSMISMATCH:
571 clnt_geterr(client, &rpcerr);
572 if (rpcerr.re_vers.low > RPCBVERS4)
575 case RPC_PROCUNAVAIL:
576 case RPC_PROGUNAVAIL:
579 /* Most likely RPC_TIMEDOUT or RPC_CANTRECV */
580 rpc_createerr.cf_stat = status;
581 clnt_geterr(client, &rpc_createerr.cf_error);
588 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
589 clnt_geterr(client, &rpc_createerr.cf_error);
594 #endif /* HAVE_LIBTIRPC */
597 * Try GETPORT request via rpcbind version 2.
599 * Returns non-zero port number on success; otherwise returns
600 * zero. rpccreateerr is set to reflect the nature of the error.
602 static unsigned long nfs_gp_pmap_getport(CLIENT *client,
604 struct timeval timeout)
606 enum clnt_stat status;
609 status = CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT,
610 (xdrproc_t)xdr_pmap, (void *)parms,
611 (xdrproc_t)xdr_u_long, (void *)&port,
614 if (status != RPC_SUCCESS) {
615 rpc_createerr.cf_stat = status;
616 CLNT_GETERR(client, &rpc_createerr.cf_error);
618 } else if (port == 0)
619 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
626 static unsigned short nfs_gp_getport_rpcb(CLIENT *client,
627 const struct sockaddr *sap,
628 const rpcprog_t program,
629 const rpcvers_t version,
630 const unsigned short protocol,
631 struct timeval timeout)
633 unsigned short port = 0;
636 if (nfs_gp_init_rpcb_parms(sap, program, version,
637 protocol, &parms) != 0) {
638 port = nfs_gp_rpcb_getaddr(client, &parms, timeout);
639 nfs_gp_free_rpcb_parms(&parms);
645 #endif /* HAVE_LIBTIRPC */
647 static unsigned long nfs_gp_getport_pmap(CLIENT *client,
648 const rpcprog_t program,
649 const rpcvers_t version,
650 const unsigned short protocol,
651 struct timeval timeout)
653 struct pmap parms = {
658 rpcvers_t pmap_version = PMAPVERS;
660 CLNT_CONTROL(client, CLSET_VERS, (void *)&pmap_version);
661 return nfs_gp_pmap_getport(client, &parms, timeout);
665 * Try an AF_INET6 request via rpcbind v4/v3; try an AF_INET
666 * request via rpcbind v2.
668 * Returns non-zero port number on success; otherwise returns
669 * zero. rpccreateerr is set to reflect the nature of the error.
671 static unsigned short nfs_gp_getport(CLIENT *client,
672 const struct sockaddr *sap,
673 const rpcprog_t program,
674 const rpcvers_t version,
675 const unsigned short protocol,
676 struct timeval timeout)
678 switch (sap->sa_family) {
681 return nfs_gp_getport_rpcb(client, sap, program,
682 version, protocol, timeout);
683 #endif /* HAVE_LIBTIRPC */
685 return nfs_gp_getport_pmap(client, program, version,
689 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
694 * nfs_rpc_ping - Determine if RPC service is responding to requests
695 * @sap: pointer to address of server to query (port is already filled in)
696 * @salen: length of server address
697 * @program: requested RPC program number
698 * @version: requested RPC version number
699 * @protocol: requested IPPROTO_ value of transport protocol
700 * @timeout: pointer to request timeout (NULL means use default timeout)
702 * Returns 1 if the remote service responded without an error; otherwise
705 int nfs_rpc_ping(const struct sockaddr *sap, const socklen_t salen,
706 const rpcprog_t program, const rpcvers_t version,
707 const unsigned short protocol, const struct timeval *timeout)
709 union nfs_sockaddr address;
710 struct sockaddr *saddr = &address.sa;
712 struct timeval tout = { -1, 0 };
718 nfs_clear_rpc_createerr();
720 memcpy(saddr, sap, (size_t)salen);
721 client = nfs_get_rpcclient(saddr, salen, protocol,
722 program, version, &tout);
723 if (client != NULL) {
724 result = nfs_gp_ping(client, tout);
725 nfs_gp_map_tcp_errorcodes(protocol);
726 CLNT_DESTROY(client);
733 * nfs_getport - query server's rpcbind to get port number for an RPC service
734 * @sap: pointer to address of server to query
735 * @salen: length of server's address
736 * @program: requested RPC program number
737 * @version: requested RPC version number
738 * @protocol: IPPROTO_ value of requested transport protocol
740 * Uses any acceptable rpcbind version to discover the port number for the
741 * RPC service described by the given [program, version, transport] tuple.
742 * Uses a quick timeout and an ephemeral source port. Supports AF_INET and
743 * AF_INET6 server addresses.
745 * Returns a positive integer representing the port number of the RPC
746 * service advertised by the server (in host byte order), or zero if the
747 * service is not advertised or there was some problem querying the server's
748 * rpcbind daemon. rpccreateerr is set to reflect the underlying cause of
751 * There are a variety of ways to choose which transport and rpcbind versions
752 * to use. We chose to conserve local resources and try to avoid incurring
756 * To provide rudimentary support for traversing firewalls, query the remote
757 * using the same transport as the requested service. This provides some
758 * guarantee that the requested transport is available between this client
759 * and the server, and if the caller specifically requests TCP, for example,
760 * this may be becuase a firewall is in place that blocks UDP traffic. We
761 * could try both, but that could involve a lengthy timeout in several cases,
762 * and would often consume an extra ephemeral port.
765 * To avoid using up too many ephemeral ports, AF_INET queries use tried-and-
766 * true rpcbindv2, and don't try the newer versions; and AF_INET6 queries use
767 * rpcbindv4, then rpcbindv3 on the same socket. The newer rpcbind protocol
768 * versions can adequately detect if a remote RPC service does not support
769 * AF_INET6 at all. The rpcbind socket is re-used in an attempt to keep the
770 * overall number of consumed ephemeral ports low.
772 unsigned short nfs_getport(const struct sockaddr *sap,
773 const socklen_t salen,
774 const rpcprog_t program,
775 const rpcvers_t version,
776 const unsigned short protocol)
778 union nfs_sockaddr address;
779 struct sockaddr *saddr = &address.sa;
780 struct timeval timeout = { -1, 0 };
781 unsigned short port = 0;
784 nfs_clear_rpc_createerr();
786 memcpy(saddr, sap, (size_t)salen);
787 client = nfs_gp_get_rpcbclient(saddr, salen, protocol,
788 default_rpcb_version, &timeout);
789 if (client != NULL) {
790 port = nfs_gp_getport(client, saddr, program,
791 version, protocol, timeout);
792 CLNT_DESTROY(client);
799 * nfs_getport_ping - query server's rpcbind and do RPC ping to verify result
800 * @sap: IN: pointer to address of server to query;
801 * OUT: pointer to updated address
802 * @salen: length of server's address
803 * @program: requested RPC program number
804 * @version: requested RPC version number
805 * @protocol: IPPROTO_ value of requested transport protocol
807 * Uses any acceptable rpcbind version to discover the port number for the
808 * RPC service described by the given [program, version, transport] tuple.
809 * Uses a quick timeout and an ephemeral source port. Supports AF_INET and
810 * AF_INET6 server addresses.
812 * Returns a 1 and sets the port number in the passed-in server address
813 * if both the query and the ping were successful; otherwise zero.
814 * rpccreateerr is set to reflect the underlying cause of the error.
816 int nfs_getport_ping(struct sockaddr *sap, const socklen_t salen,
817 const rpcprog_t program, const rpcvers_t version,
818 const unsigned short protocol)
820 struct timeval timeout = { -1, 0 };
821 unsigned short port = 0;
825 nfs_clear_rpc_createerr();
827 client = nfs_gp_get_rpcbclient(sap, salen, protocol,
828 default_rpcb_version, &timeout);
829 if (client != NULL) {
830 port = nfs_gp_getport(client, sap, program,
831 version, protocol, timeout);
832 CLNT_DESTROY(client);
837 union nfs_sockaddr address;
838 struct sockaddr *saddr = &address.sa;
840 memcpy(saddr, sap, (size_t)salen);
841 nfs_set_port(saddr, port);
843 nfs_clear_rpc_createerr();
845 client = nfs_get_rpcclient(saddr, salen, protocol,
846 program, version, &timeout);
847 if (client != NULL) {
848 result = nfs_gp_ping(client, timeout);
849 nfs_gp_map_tcp_errorcodes(protocol);
850 CLNT_DESTROY(client);
855 nfs_set_port(sap, port);
861 * nfs_getlocalport - query local rpcbind to get port number for an RPC service
862 * @program: requested RPC program number
863 * @version: requested RPC version number
864 * @protocol: IPPROTO_ value of requested transport protocol
866 * Uses any acceptable rpcbind version to discover the port number for the
867 * RPC service described by the given [program, version, transport] tuple.
868 * Uses a quick timeout and an ephemeral source port. Supports AF_INET and
869 * AF_INET6 local addresses.
871 * Returns a positive integer representing the port number of the RPC
872 * service advertised by the server (in host byte order), or zero if the
873 * service is not advertised or there was some problem querying the server's
874 * rpcbind daemon. rpccreateerr is set to reflect the underlying cause of
877 * Try an AF_LOCAL connection first. The rpcbind daemon implementation should
878 * listen on AF_LOCAL.
880 * If that doesn't work (for example, if portmapper is running, or rpcbind
881 * isn't listening on /var/run/rpcbind.sock), send a query via UDP to localhost
882 * (UDP doesn't leave a socket in TIME_WAIT, and the timeout is a relatively
885 unsigned short nfs_getlocalport(const rpcprot_t program,
886 const rpcvers_t version,
887 const unsigned short protocol)
889 union nfs_sockaddr address;
890 struct sockaddr *lb_addr = &address.sa;
891 socklen_t lb_len = sizeof(*lb_addr);
892 unsigned short port = 0;
895 const struct sockaddr_un sun = {
896 .sun_family = AF_LOCAL,
897 .sun_path = _PATH_RPCBINDSOCK,
899 const struct sockaddr *sap = (struct sockaddr *)&sun;
900 const socklen_t salen = SUN_LEN(&sun);
902 struct timeval timeout = { -1, 0 };
904 nfs_clear_rpc_createerr();
906 client = nfs_gp_get_rpcbclient(sap, salen, 0, RPCBVERS_4, &timeout);
907 if (client != NULL) {
910 if (nfs_gp_init_rpcb_parms(sap, program, version,
911 protocol, &parms) != 0) {
912 port = nfs_gp_rpcb_getaddr(client, &parms, timeout);
913 nfs_gp_free_rpcb_parms(&parms);
915 CLNT_DESTROY(client);
917 #endif /* NFS_GP_LOCAL */
920 nfs_clear_rpc_createerr();
922 if (nfs_gp_loopback_address(lb_addr, &lb_len)) {
923 port = nfs_getport(lb_addr, lb_len,
924 program, version, protocol);
926 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
933 * nfs_rpcb_getaddr - query rpcbind via rpcbind versions 4 and 3
934 * @sap: pointer to address of server to query
935 * @salen: length of server address
936 * @transport: transport protocol to use for the query
937 * @addr: pointer to r_addr address
938 * @program: requested RPC program number
939 * @version: requested RPC version number
940 * @protocol: requested IPPROTO_ value of transport protocol
941 * @timeout: pointer to request timeout (NULL means use default timeout)
943 * Returns a positive integer representing the port number of the RPC
944 * service advertised by the server (in host byte order), or zero if the
945 * service is not advertised or there was some problem querying the
946 * server's rpcbind daemon. rpccreateerr is set to reflect the
947 * underlying cause of the error.
949 * This function provides similar functionality to nfs_pmap_getport(),
950 * but performs the rpcbind lookup via rpcbind version 4. If the server
951 * doesn't support rpcbind version 4, it will retry with version 3.
952 * The GETADDR procedure is exactly the same in these two versions of
953 * the rpcbind protocol, so the socket, RPC client, and arguments are
954 * re-used when retrying, saving ephemeral port space.
956 * These RPC procedures take a universal address as an argument, so the
957 * query will fail if the remote rpcbind daemon doesn't find an entry
958 * with a matching address. A matching address includes an ANYADDR
959 * address of the same address family. In this way an RPC server can
960 * advertise via rpcbind that it does not support AF_INET6.
964 unsigned short nfs_rpcb_getaddr(const struct sockaddr *sap,
965 const socklen_t salen,
966 const unsigned short transport,
967 const struct sockaddr *addr,
968 const rpcprog_t program,
969 const rpcvers_t version,
970 const unsigned short protocol,
971 const struct timeval *timeout)
973 union nfs_sockaddr address;
974 struct sockaddr *saddr = &address.sa;
977 struct timeval tout = { -1, 0 };
978 unsigned short port = 0;
983 nfs_clear_rpc_createerr();
985 memcpy(saddr, sap, (size_t)salen);
986 client = nfs_gp_get_rpcbclient(saddr, salen, transport,
988 if (client != NULL) {
989 if (nfs_gp_init_rpcb_parms(addr, program, version,
990 protocol, &parms) != 0) {
991 port = nfs_gp_rpcb_getaddr(client, &parms, tout);
992 nfs_gp_free_rpcb_parms(&parms);
994 CLNT_DESTROY(client);
1000 #else /* !HAVE_LIBTIRPC */
1002 unsigned short nfs_rpcb_getaddr(__attribute__((unused)) const struct sockaddr *sap,
1003 __attribute__((unused)) const socklen_t salen,
1004 __attribute__((unused)) const unsigned short transport,
1005 __attribute__((unused)) const struct sockaddr *addr,
1006 __attribute__((unused)) const rpcprog_t program,
1007 __attribute__((unused)) const rpcvers_t version,
1008 __attribute__((unused)) const unsigned short protocol,
1009 __attribute__((unused)) const struct timeval *timeout)
1011 nfs_clear_rpc_createerr();
1013 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
1017 #endif /* !HAVE_LIBTIRPC */
1020 * nfs_pmap_getport - query rpcbind via the portmap protocol (rpcbindv2)
1021 * @sin: pointer to AF_INET address of server to query
1022 * @transport: transport protocol to use for the query
1023 * @program: requested RPC program number
1024 * @version: requested RPC version number
1025 * @protocol: requested IPPROTO_ value of transport protocol
1026 * @timeout: pointer to request timeout (NULL means use default timeout)
1028 * Returns a positive integer representing the port number of the RPC service
1029 * advertised by the server (in host byte order), or zero if the service is
1030 * not advertised or there was some problem querying the server's rpcbind
1031 * daemon. rpccreateerr is set to reflect the underlying cause of the error.
1033 * nfs_pmap_getport() is very similar to pmap_getport(), except that:
1035 * 1. This version always tries to use an ephemeral port, since reserved
1036 * ports are not needed for GETPORT queries. This conserves the very
1037 * limited reserved port space, helping reduce failed socket binds
1038 * during mount storms.
1040 * 2. This version times out quickly by default. It time-limits the
1041 * connect process as well as the actual RPC call, and even allows the
1042 * caller to specify the timeout.
1044 * 3. This version shares code with the rpcbindv3 and rpcbindv4 query
1045 * functions. It can use a TI-RPC generated CLIENT.
1047 unsigned long nfs_pmap_getport(const struct sockaddr_in *sin,
1048 const unsigned short transport,
1049 const unsigned long program,
1050 const unsigned long version,
1051 const unsigned long protocol,
1052 const struct timeval *timeout)
1054 struct sockaddr_in address;
1055 struct sockaddr *saddr = (struct sockaddr *)&address;
1057 struct pmap parms = {
1060 .pm_prot = protocol,
1062 struct timeval tout = { -1, 0 };
1063 unsigned long port = 0;
1065 if (timeout != NULL)
1068 nfs_clear_rpc_createerr();
1070 memcpy(saddr, sin, sizeof(address));
1071 client = nfs_gp_get_rpcbclient(saddr, (socklen_t)sizeof(*sin),
1072 transport, PMAPVERS, &tout);
1073 if (client != NULL) {
1074 port = nfs_gp_pmap_getport(client, &parms, tout);
1075 CLNT_DESTROY(client);