]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/getport.c
getport: RPC_PROGNOTREGISTERED is a permanent error
[nfs-utils.git] / support / nfs / getport.c
1 /*
2  * Provide a variety of APIs that query an rpcbind daemon to
3  * discover RPC service ports and allowed protocol version
4  * numbers.
5  *
6  * Copyright (C) 2008 Oracle Corporation.  All rights reserved.
7  *
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.
12  *
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.
17  *
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.
22  *
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <sys/types.h>
30 #include <sys/time.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <errno.h>
34
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <netdb.h>
38 #include <arpa/inet.h>
39
40 #include <rpc/rpc.h>
41 #include <rpc/pmap_prot.h>
42
43 #ifdef HAVE_LIBTIRPC
44 #include <netconfig.h>
45 #include <rpc/rpcb_prot.h>
46 #endif
47
48 #include "nfsrpc.h"
49
50 /*
51  * Try a local socket first to access the local rpcbind daemon
52  *
53  * Rpcbind's local socket service does not seem to be working.
54  * Disable this logic for now.
55  */
56 #ifdef HAVE_LIBTIRPC
57 #undef NFS_GP_LOCAL
58 #else   /* !HAVE_LIBTIRPC */
59 #undef NFS_GP_LOCAL
60 #endif  /* !HAVE_LIBTIRPC */
61
62 #ifdef HAVE_LIBTIRPC
63 static const rpcvers_t default_rpcb_version = RPCBVERS_4;
64 #else   /* !HAVE_LIBTIRPC */
65 static const rpcvers_t default_rpcb_version = PMAPVERS;
66 #endif  /* !HAVE_LIBTIRPC */
67
68 /*
69  * There's no easy way to tell how the local system's networking
70  * and rpcbind is configured (ie. whether we want to use IPv6 or
71  * IPv4 loopback to contact RPC services on the local host).  We
72  * punt and simply try to look up "localhost".
73  *
74  * Returns TRUE on success.
75  */
76 static int nfs_gp_loopback_address(struct sockaddr *sap, socklen_t *salen)
77 {
78         struct addrinfo *gai_results;
79         int ret = 0;
80
81         if (getaddrinfo("localhost", NULL, NULL, &gai_results))
82                 return 0;
83
84         if (*salen >= gai_results->ai_addrlen) {
85                 memcpy(sap, gai_results->ai_addr,
86                                 gai_results->ai_addrlen);
87                 *salen = gai_results->ai_addrlen;
88                 ret = 1;
89         }
90
91         freeaddrinfo(gai_results);
92         return ret;
93 }
94
95 /*
96  * Plant port number in @sap.  @port is already in network byte order.
97  */
98 static void nfs_gp_set_port(struct sockaddr *sap, const in_port_t port)
99 {
100         struct sockaddr_in *sin = (struct sockaddr_in *)sap;
101         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
102
103         switch (sap->sa_family) {
104         case AF_INET:
105                 sin->sin_port = port;
106                 break;
107         case AF_INET6:
108                 sin6->sin6_port = port;
109                 break;
110         }
111 }
112
113 /*
114  * Look up a network service in /etc/services and return the
115  * network-order port number of that service.
116  */
117 static in_port_t nfs_gp_getservbyname(const char *service,
118                                       const unsigned short protocol)
119 {
120         const struct addrinfo gai_hint = {
121                 .ai_family      = AF_INET,
122                 .ai_protocol    = protocol,
123                 .ai_flags       = AI_PASSIVE,
124         };
125         struct addrinfo *gai_results;
126         const struct sockaddr_in *sin;
127         in_port_t port;
128
129         if (getaddrinfo(NULL, service, &gai_hint, &gai_results) != 0)
130                 return 0;
131
132         sin = (const struct sockaddr_in *)gai_results->ai_addr;
133         port = sin->sin_port;
134         
135         freeaddrinfo(gai_results);
136         return port;
137 }
138
139 /*
140  * Discover the port number that should be used to contact an
141  * rpcbind service.  This will detect if the port has a local
142  * value that may have been set in /etc/services.
143  *
144  * Returns network byte-order port number of rpcbind service
145  * on this system.
146  */
147 static in_port_t nfs_gp_get_rpcb_port(const unsigned short protocol)
148 {
149         static const char *rpcb_netnametbl[] = {
150                 "rpcbind",
151                 "portmapper",
152                 "sunrpc",
153                 NULL,
154         };
155         unsigned int i;
156
157         for (i = 0; rpcb_netnametbl[i] != NULL; i++) {
158                 in_port_t port;
159
160                 port = nfs_gp_getservbyname(rpcb_netnametbl[i], protocol);
161                 if (port != 0)
162                         return port;
163         }
164
165         return (in_port_t)htons((uint16_t)PMAPPORT);
166 }
167
168 /*
169  * Set up an RPC client for communicating with an rpcbind daemon at
170  * @sap over @transport with protocol version @version.
171  *
172  * Returns a pointer to a prepared RPC client if successful, and
173  * @timeout is initialized; caller must destroy a non-NULL returned RPC
174  * client.  Otherwise returns NULL, and rpc_createerr.cf_stat is set to
175  * reflect the error.
176  */
177 static CLIENT *nfs_gp_get_rpcbclient(struct sockaddr *sap,
178                                      const socklen_t salen,
179                                      const unsigned short transport,
180                                      const rpcvers_t version,
181                                      struct timeval *timeout)
182 {
183         static const char *rpcb_pgmtbl[] = {
184                 "rpcbind",
185                 "portmap",
186                 "portmapper",
187                 "sunrpc",
188                 NULL,
189         };
190         rpcprog_t rpcb_prog = nfs_getrpcbyname(RPCBPROG, rpcb_pgmtbl);
191
192         nfs_gp_set_port(sap, nfs_gp_get_rpcb_port(transport));
193         return nfs_get_rpcclient(sap, salen, transport, rpcb_prog,
194                                         version, timeout);
195 }
196
197 /*
198  * One of the arguments passed when querying remote rpcbind services
199  * via rpcbind v3 or v4 is a netid string.  This replaces the pm_prot
200  * field used in legacy PMAP_GETPORT calls.
201  *
202  * RFC 1833 says netids are not standard but rather defined on the local
203  * host.  There are, however, standard definitions for nc_protofmly and
204  * nc_proto that can be used to derive a netid string on the local host,
205  * based on the contents of /etc/netconfig.
206  *
207  * Walk through the local netconfig database and grab the netid of the
208  * first entry that matches @family and @protocol and whose netid string
209  * fits in the provided buffer.
210  *
211  * Returns a '\0'-terminated string if successful; otherwise NULL.
212  * rpc_createerr.cf_stat is set to reflect the error.
213  */
214 #ifdef HAVE_LIBTIRPC
215
216 static char *nfs_gp_get_netid(const sa_family_t family,
217                               const unsigned short protocol)
218 {
219         char *nc_protofmly, *nc_proto, *nc_netid;
220         struct netconfig *nconf;
221         struct protoent *proto;
222         void *handle;
223
224         switch (family) {
225         case AF_LOCAL:
226         case AF_INET:
227                 nc_protofmly = NC_INET;
228                 break;
229         case AF_INET6:
230                 nc_protofmly = NC_INET6;
231                 break;
232         default:
233                 goto out;
234         }
235
236         proto = getprotobynumber(protocol);
237         if (proto == NULL)
238                 goto out;
239         nc_proto = proto->p_name;
240
241         handle = setnetconfig();
242         while ((nconf = getnetconfig(handle)) != NULL) {
243
244                 if (nconf->nc_protofmly != NULL &&
245                     strcmp(nconf->nc_protofmly, nc_protofmly) != 0)
246                         continue;
247                 if (nconf->nc_proto != NULL &&
248                     strcmp(nconf->nc_proto, nc_proto) != 0)
249                         continue;
250
251                 nc_netid = strdup(nconf->nc_netid);
252                 endnetconfig(handle);
253                 return nc_netid;
254         }
255         endnetconfig(handle);
256
257 out:
258         rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
259         return NULL;
260 }
261
262 #endif  /* HAVE_LIBTIRPC */
263
264 /*
265  * Extract a port number from a universal address, and terminate the
266  * string in @addrstr just after the address part.
267  *
268  * Returns -1 if unsuccesful; otherwise a decoded port number (possibly 0)
269  * is returned.
270  */
271 static int nfs_gp_universal_porthelper(char *addrstr)
272 {
273         char *p, *endptr;
274         unsigned long portlo, porthi;
275         int port = -1;
276
277         p = strrchr(addrstr, '.');
278         if (p == NULL)
279                 goto out;
280         portlo = strtoul(p + 1, &endptr, 10);
281         if (*endptr != '\0' || portlo > 255)
282                 goto out;
283         *p = '\0';
284
285         p = strrchr(addrstr, '.');
286         if (p == NULL)
287                 goto out;
288         porthi = strtoul(p + 1, &endptr, 10);
289         if (*endptr != '\0' || porthi > 255)
290                 goto out;
291         *p = '\0';
292         port = (porthi << 8) | portlo;
293
294 out:
295         return port;
296 }
297
298 /**
299  * nfs_universal2port - extract port number from a "universal address"
300  * @uaddr: '\0'-terminated C string containing a universal address
301  *
302  * Universal addresses (defined in RFC 1833) are used when calling an
303  * rpcbind daemon via protocol versions 3 or 4..
304  *
305  * Returns -1 if unsuccesful; otherwise a decoded port number (possibly 0)
306  * is returned.
307  */
308 int nfs_universal2port(const char *uaddr)
309 {
310         char *addrstr;
311         int port = -1;
312
313         addrstr = strdup(uaddr);
314         if (addrstr != NULL) {
315                 port = nfs_gp_universal_porthelper(addrstr);
316                 free(addrstr);
317         }
318         return port;
319 }
320
321 /**
322  * nfs_sockaddr2universal - convert a sockaddr to a "universal address"
323  * @sap: pointer to a socket address
324  *
325  * Universal addresses (defined in RFC 1833) are used when calling an
326  * rpcbind daemon via protocol versions 3 or 4..
327  *
328  * Returns a '\0'-terminated string if successful; caller must free
329  * the returned string.  Otherwise NULL is returned and
330  * rpc_createerr.cf_stat is set to reflect the error.
331  *
332  * inet_ntop(3) is used here, since getnameinfo(3) is not available
333  * in some earlier glibc releases, and we don't require support for
334  * scope IDs for universal addresses.
335  */
336 char *nfs_sockaddr2universal(const struct sockaddr *sap)
337 {
338         const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sap;
339         const struct sockaddr_un *sun = (const struct sockaddr_un *)sap;
340         const struct sockaddr_in *sin = (const struct sockaddr_in *)sap;
341         char buf[INET6_ADDRSTRLEN + 8 /* for port information */];
342         uint16_t port;
343         size_t count;
344         char *result;
345         int len;
346
347         switch (sap->sa_family) {
348         case AF_LOCAL:
349                 return strndup(sun->sun_path, sizeof(sun->sun_path));
350         case AF_INET:
351                 if (inet_ntop(AF_INET, (const void *)&sin->sin_addr.s_addr,
352                                         buf, (socklen_t)sizeof(buf)) == NULL)
353                         goto out_err;
354                 port = ntohs(sin->sin_port);
355                 break;
356         case AF_INET6:
357                 if (inet_ntop(AF_INET6, (const void *)&sin6->sin6_addr,
358                                         buf, (socklen_t)sizeof(buf)) == NULL)
359                         goto out_err;
360                 port = ntohs(sin6->sin6_port);
361                 break;
362         default:
363                 goto out_err;
364         }
365
366         count = sizeof(buf) - strlen(buf);
367         len = snprintf(buf + strlen(buf), count, ".%u.%u",
368                         (unsigned)(port >> 8), (unsigned)(port & 0xff));
369         /* before glibc 2.0.6, snprintf(3) could return -1 */
370         if (len < 0 || (size_t)len > count)
371                 goto out_err;
372
373         result = strdup(buf);
374         if (result != NULL)
375                 return result;
376
377 out_err:
378         rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
379         return NULL;
380 }
381
382 /*
383  * Send a NULL request to the indicated RPC service.
384  *
385  * Returns 1 if the service responded; otherwise 0;
386  */
387 static int nfs_gp_ping(CLIENT *client, struct timeval timeout)
388 {
389         enum clnt_stat status;
390
391         status = CLNT_CALL(client, NULLPROC,
392                            (xdrproc_t)xdr_void, NULL,
393                            (xdrproc_t)xdr_void, NULL,
394                            timeout);
395
396         return (int)(status == RPC_SUCCESS);
397 }
398
399 #ifdef HAVE_LIBTIRPC
400
401 /*
402  * Initialize the rpcb argument for a GETADDR request.
403  *
404  * Returns 1 if successful, and caller must free strings pointed
405  * to by r_netid and r_addr; otherwise 0.
406  */
407 static int nfs_gp_init_rpcb_parms(const struct sockaddr *sap,
408                                   const rpcprog_t program,
409                                   const rpcvers_t version,
410                                   const unsigned short protocol,
411                                   struct rpcb *parms)
412 {
413         char *netid, *addr;
414
415         netid = nfs_gp_get_netid(sap->sa_family, protocol);
416         if (netid == NULL)
417                 return 0;
418
419         addr = nfs_sockaddr2universal(sap);
420         if (addr == NULL) {
421                 free(netid);
422                 return 0;
423         }
424
425         memset(parms, 0, sizeof(*parms));
426         parms->r_prog   = program;
427         parms->r_vers   = version;
428         parms->r_netid  = netid;
429         parms->r_addr   = addr;
430         parms->r_owner  = "";
431
432         return 1;
433 }
434
435 static void nfs_gp_free_rpcb_parms(struct rpcb *parms)
436 {
437         free(parms->r_netid);
438         free(parms->r_addr);
439 }
440
441 /*
442  * Try rpcbind GETADDR via version 4.  If that fails, try same
443  * request via version 3.
444  *
445  * Returns non-zero port number on success; otherwise returns
446  * zero.  rpccreateerr is set to reflect the nature of the error.
447  */
448 static unsigned short nfs_gp_rpcb_getaddr(CLIENT *client,
449                                           struct rpcb *parms,
450                                           struct timeval timeout)
451 {
452         rpcvers_t rpcb_version;
453         struct rpc_err rpcerr;
454         int port = 0;
455
456         for (rpcb_version = RPCBVERS_4;
457              rpcb_version >= RPCBVERS_3;
458              rpcb_version--) {
459                 enum clnt_stat status;
460                 char *uaddr = NULL;
461
462                 CLNT_CONTROL(client, CLSET_VERS, (void *)&rpcb_version);
463                 status = CLNT_CALL(client, (rpcproc_t)RPCBPROC_GETADDR,
464                                    (xdrproc_t)xdr_rpcb, (void *)parms,
465                                    (xdrproc_t)xdr_wrapstring, (void *)&uaddr,
466                                    timeout);
467
468                 switch (status) {
469                 case RPC_SUCCESS:
470                         if ((uaddr == NULL) || (uaddr[0] == '\0')) {
471                                 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
472                                 return 0;
473                         }
474
475                         port = nfs_universal2port(uaddr);
476                         xdr_free((xdrproc_t)xdr_wrapstring, (char *)&uaddr);
477                         if (port == -1) {
478                                 rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
479                                 return 0;
480                         }
481                         return (unsigned short)port;
482                 case RPC_PROGVERSMISMATCH:
483                         clnt_geterr(client, &rpcerr);
484                         if (rpcerr.re_vers.low > RPCBVERS4)
485                                 return 0;
486                         continue;
487                 case RPC_PROCUNAVAIL:
488                 case RPC_PROGUNAVAIL:
489                         continue;
490                 default:
491                         /* Most likely RPC_TIMEDOUT or RPC_CANTRECV */
492                         rpc_createerr.cf_stat = status;
493                         clnt_geterr(client, &rpc_createerr.cf_error);
494                         return 0;
495                 }
496
497         }
498
499         if (port == 0) {
500                 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
501                 clnt_geterr(client, &rpc_createerr.cf_error);
502         }
503         return port;
504 }
505
506 #endif  /* HAVE_LIBTIRPC */
507
508 /*
509  * Try GETPORT request via rpcbind version 2.
510  *
511  * Returns non-zero port number on success; otherwise returns
512  * zero.  rpccreateerr is set to reflect the nature of the error.
513  */
514 static unsigned long nfs_gp_pmap_getport(CLIENT *client,
515                                          struct pmap *parms,
516                                          struct timeval timeout)
517 {
518         enum clnt_stat status;
519         unsigned long port;
520
521         status = CLNT_CALL(client, (rpcproc_t)PMAPPROC_GETPORT,
522                            (xdrproc_t)xdr_pmap, (void *)parms,
523                            (xdrproc_t)xdr_u_long, (void *)&port,
524                            timeout);
525
526         if (status != RPC_SUCCESS) {
527                 rpc_createerr.cf_stat = status;
528                 clnt_geterr(client, &rpc_createerr.cf_error);
529                 port = 0;
530         } else if (port == 0)
531                 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
532
533         return port;
534 }
535
536 #ifdef HAVE_LIBTIRPC
537
538 static unsigned short nfs_gp_getport_rpcb(CLIENT *client,
539                                           const struct sockaddr *sap,
540                                           const rpcprog_t program,
541                                           const rpcvers_t version,
542                                           const unsigned short protocol,
543                                           struct timeval timeout)
544 {
545         unsigned short port = 0;
546         struct rpcb parms;
547
548         if (nfs_gp_init_rpcb_parms(sap, program, version,
549                                         protocol, &parms) != 0) {
550                 port = nfs_gp_rpcb_getaddr(client, &parms, timeout);
551                 nfs_gp_free_rpcb_parms(&parms);
552         }
553
554         return port;
555 }
556
557 #endif  /* HAVE_LIBTIRPC */
558
559 static unsigned long nfs_gp_getport_pmap(CLIENT *client,
560                                          const rpcprog_t program,
561                                          const rpcvers_t version,
562                                          const unsigned short protocol,
563                                          struct timeval timeout)
564 {
565         struct pmap parms = {
566                 .pm_prog        = program,
567                 .pm_vers        = version,
568                 .pm_prot        = protocol,
569         };
570         rpcvers_t pmap_version = PMAPVERS;
571
572         CLNT_CONTROL(client, CLSET_VERS, (void *)&pmap_version);
573         return nfs_gp_pmap_getport(client, &parms, timeout);
574 }
575
576 /*
577  * Try an AF_INET6 request via rpcbind v4/v3; try an AF_INET
578  * request via rpcbind v2.
579  *
580  * Returns non-zero port number on success; otherwise returns
581  * zero.  rpccreateerr is set to reflect the nature of the error.
582  */
583 static unsigned short nfs_gp_getport(CLIENT *client,
584                                      const struct sockaddr *sap,
585                                      const rpcprog_t program,
586                                      const rpcvers_t version,
587                                      const unsigned short protocol,
588                                      struct timeval timeout)
589 {
590         switch (sap->sa_family) {
591 #ifdef HAVE_LIBTIRPC
592         case AF_INET6:
593                 return nfs_gp_getport_rpcb(client, sap, program,
594                                                 version, protocol, timeout);
595 #endif  /* HAVE_LIBTIRPC */
596         case AF_INET:
597                 return nfs_gp_getport_pmap(client, program, version,
598                                                         protocol, timeout);
599         }
600
601         rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
602         return 0;
603 }
604
605 /**
606  * nfs_rpc_ping - Determine if RPC service is responding to requests
607  * @sap: pointer to address of server to query (port is already filled in)
608  * @salen: length of server address
609  * @program: requested RPC program number
610  * @version: requested RPC version number
611  * @protocol: requested IPPROTO_ value of transport protocol
612  * @timeout: pointer to request timeout (NULL means use default timeout)
613  *
614  * Returns 1 if the remote service responded without an error; otherwise
615  * zero.
616  */
617 int nfs_rpc_ping(const struct sockaddr *sap, const socklen_t salen,
618                  const rpcprog_t program, const rpcvers_t version,
619                  const unsigned short protocol, const struct timeval *timeout)
620 {
621         struct sockaddr_storage address;
622         struct sockaddr *saddr = (struct sockaddr *)&address;
623         CLIENT *client;
624         struct timeval tout = { -1, 0 };
625         int result = 0;
626
627         if (timeout != NULL)
628                 tout = *timeout;
629
630         memcpy(saddr, sap, (size_t)salen);
631         client = nfs_get_rpcclient(saddr, salen, protocol,
632                                                 program, version, &tout);
633         if (client != NULL) {
634                 result = nfs_gp_ping(client, tout);
635                 CLNT_DESTROY(client);
636         }
637
638         return result;
639 }
640
641 /**
642  * nfs_getport - query server's rpcbind to get port number for an RPC service
643  * @sap: pointer to address of server to query
644  * @salen: length of server's address
645  * @program: requested RPC program number
646  * @version: requested RPC version number
647  * @protocol: IPPROTO_ value of requested transport protocol
648  *
649  * Uses any acceptable rpcbind version to discover the port number for the
650  * RPC service described by the given [program, version, transport] tuple.
651  * Uses a quick timeout and an ephemeral source port.  Supports AF_INET and
652  * AF_INET6 server addresses.
653  *
654  * Returns a positive integer representing the port number of the RPC
655  * service advertised by the server (in host byte order), or zero if the
656  * service is not advertised or there was some problem querying the server's
657  * rpcbind daemon.  rpccreateerr is set to reflect the underlying cause of
658  * the error.
659  *
660  * There are a variety of ways to choose which transport and rpcbind versions
661  * to use.  We chose to conserve local resources and try to avoid incurring
662  * timeouts.
663  *
664  * Transport
665  * To provide rudimentary support for traversing firewalls, query the remote
666  * using the same transport as the requested service.  This provides some
667  * guarantee that the requested transport is available between this client
668  * and the server, and if the caller specifically requests TCP, for example,
669  * this may be becuase a firewall is in place that blocks UDP traffic.  We
670  * could try both, but that could involve a lengthy timeout in several cases,
671  * and would often consume an extra ephemeral port.
672  *
673  * Rpcbind version
674  * To avoid using up too many ephemeral ports, AF_INET queries use tried-and-
675  * true rpcbindv2, and don't try the newer versions; and AF_INET6 queries use
676  * rpcbindv4, then rpcbindv3 on the same socket.  The newer rpcbind protocol
677  * versions can adequately detect if a remote RPC service does not support
678  * AF_INET6 at all.  The rpcbind socket is re-used in an attempt to keep the
679  * overall number of consumed ephemeral ports low.
680  */
681 unsigned short nfs_getport(const struct sockaddr *sap,
682                            const socklen_t salen,
683                            const rpcprog_t program,
684                            const rpcvers_t version,
685                            const unsigned short protocol)
686 {
687         struct sockaddr_storage address;
688         struct sockaddr *saddr = (struct sockaddr *)&address;
689         struct timeval timeout = { -1, 0 };
690         unsigned short port = 0;
691         CLIENT *client;
692
693         memcpy(saddr, sap, (size_t)salen);
694         client = nfs_gp_get_rpcbclient(saddr, salen, protocol,
695                                                 default_rpcb_version, &timeout);
696         if (client != NULL) {
697                 port = nfs_gp_getport(client, saddr, program,
698                                         version, protocol, timeout);
699                 CLNT_DESTROY(client);
700         }
701
702         return port;
703 }
704
705 /**
706  * nfs_getport_ping - query server's rpcbind and do RPC ping to verify result
707  * @sap: IN: pointer to address of server to query;
708  *       OUT: pointer to updated address
709  * @salen: length of server's address
710  * @program: requested RPC program number
711  * @version: requested RPC version number
712  * @protocol: IPPROTO_ value of requested transport protocol
713  *
714  * Uses any acceptable rpcbind version to discover the port number for the
715  * RPC service described by the given [program, version, transport] tuple.
716  * Uses a quick timeout and an ephemeral source port.  Supports AF_INET and
717  * AF_INET6 server addresses.
718  *
719  * Returns a 1 and sets the port number in the passed-in server address
720  * if both the query and the ping were successful; otherwise zero.
721  * rpccreateerr is set to reflect the underlying cause of the error.
722  */
723 int nfs_getport_ping(struct sockaddr *sap, const socklen_t salen,
724                      const rpcprog_t program, const rpcvers_t version,
725                      const unsigned short protocol)
726 {
727         struct timeval timeout = { -1, 0 };
728         unsigned short port = 0;
729         CLIENT *client;
730         int result = 0;
731         
732         client = nfs_gp_get_rpcbclient(sap, salen, protocol,
733                                                 default_rpcb_version, &timeout);
734         if (client != NULL) {
735                 port = nfs_gp_getport(client, sap, program,
736                                         version, protocol, timeout);
737                 CLNT_DESTROY(client);
738                 client = NULL;
739         }
740
741         if (port != 0) {
742                 struct sockaddr_storage address;
743                 struct sockaddr *saddr = (struct sockaddr *)&address;
744
745                 memcpy(saddr, sap, (size_t)salen);
746                 nfs_gp_set_port(saddr, htons(port));
747
748                 client = nfs_get_rpcclient(saddr, salen, protocol,
749                                                 program, version, &timeout);
750                 if (client != NULL) {
751                         result = nfs_gp_ping(client, timeout);
752                         CLNT_DESTROY(client);
753                 }
754         }
755
756         if (result)
757                 nfs_gp_set_port(sap, htons(port));
758
759         return result;
760 }
761
762 /**
763  * nfs_getlocalport - query local rpcbind to get port number for an RPC service
764  * @program: requested RPC program number
765  * @version: requested RPC version number
766  * @protocol: IPPROTO_ value of requested transport protocol
767  *
768  * Uses any acceptable rpcbind version to discover the port number for the
769  * RPC service described by the given [program, version, transport] tuple.
770  * Uses a quick timeout and an ephemeral source port.  Supports AF_INET and
771  * AF_INET6 local addresses.
772  *
773  * Returns a positive integer representing the port number of the RPC
774  * service advertised by the server (in host byte order), or zero if the
775  * service is not advertised or there was some problem querying the server's
776  * rpcbind daemon.  rpccreateerr is set to reflect the underlying cause of
777  * the error.
778  *
779  * Try an AF_LOCAL connection first.  The rpcbind daemon implementation should
780  * listen on AF_LOCAL.
781  *
782  * If that doesn't work (for example, if portmapper is running, or rpcbind
783  * isn't listening on /var/run/rpcbind.sock), send a query via UDP to localhost
784  * (UDP doesn't leave a socket in TIME_WAIT, and the timeout is a relatively
785  * short 3 seconds).
786  */
787 unsigned short nfs_getlocalport(const rpcprot_t program,
788                                 const rpcvers_t version,
789                                 const unsigned short protocol)
790 {
791         struct sockaddr_storage address;
792         struct sockaddr *lb_addr = (struct sockaddr *)&address;
793         socklen_t lb_len = sizeof(*lb_addr);
794         unsigned short port = 0;
795
796 #ifdef NFS_GP_LOCAL
797         const struct sockaddr_un sun = {
798                 .sun_family     = AF_LOCAL,
799                 .sun_path       = _PATH_RPCBINDSOCK,
800         };
801         const struct sockaddr *sap = (struct sockaddr *)&sun;
802         const socklen_t salen = SUN_LEN(&sun);
803         CLIENT *client;
804         struct timeval timeout = { -1, 0 };
805
806         client = nfs_gp_get_rpcbclient(sap, salen, 0, RPCBVERS_4, &timeout);
807         if (client != NULL) {
808                 struct rpcb parms;
809
810                 if (nfs_gp_init_rpcb_parms(sap, program, version,
811                                                 protocol, &parms) != 0) {
812                         port = nfs_gp_rpcb_getaddr(client, &parms, timeout);
813                         nfs_gp_free_rpcb_parms(&parms);
814                 }
815                 CLNT_DESTROY(client);
816         }
817 #endif  /* NFS_GP_LOCAL */
818
819         if (port == 0) {
820                 if (nfs_gp_loopback_address(lb_addr, &lb_len)) {
821                         port = nfs_getport(lb_addr, lb_len,
822                                                 program, version, protocol);
823                 } else
824                         rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
825         }
826
827         return port;
828 }
829
830 /**
831  * nfs_rpcb_getaddr - query rpcbind via rpcbind versions 4 and 3
832  * @sap: pointer to address of server to query
833  * @salen: length of server address
834  * @transport: transport protocol to use for the query
835  * @addr: pointer to r_addr address
836  * @program: requested RPC program number
837  * @version: requested RPC version number
838  * @protocol: requested IPPROTO_ value of transport protocol
839  * @timeout: pointer to request timeout (NULL means use default timeout)
840  *
841  * Returns a positive integer representing the port number of the RPC
842  * service advertised by the server (in host byte order), or zero if the
843  * service is not advertised or there was some problem querying the
844  * server's rpcbind daemon.  rpccreateerr is set to reflect the
845  * underlying cause of the error.
846  *
847  * This function provides similar functionality to nfs_pmap_getport(),
848  * but performs the rpcbind lookup via rpcbind version 4.  If the server
849  * doesn't support rpcbind version 4, it will retry with version 3.
850  * The GETADDR procedure is exactly the same in these two versions of
851  * the rpcbind protocol, so the socket, RPC client, and arguments are
852  * re-used when retrying, saving ephemeral port space.
853  *
854  * These RPC procedures take a universal address as an argument, so the
855  * query will fail if the remote rpcbind daemon doesn't find an entry
856  * with a matching address.  A matching address includes an ANYADDR
857  * address of the same address family.  In this way an RPC server can
858  * advertise via rpcbind that it does not support AF_INET6.
859  */
860 #ifdef HAVE_LIBTIRPC
861
862 unsigned short nfs_rpcb_getaddr(const struct sockaddr *sap,
863                                 const socklen_t salen,
864                                 const unsigned short transport,
865                                 const struct sockaddr *addr,
866                                 const rpcprog_t program,
867                                 const rpcvers_t version,
868                                 const unsigned short protocol,
869                                 const struct timeval *timeout)
870 {
871         struct sockaddr_storage address;
872         struct sockaddr *saddr = (struct sockaddr *)&address;
873         CLIENT *client;
874         struct rpcb parms;
875         struct timeval tout = { -1, 0 };
876         unsigned short port = 0;
877
878         if (timeout != NULL)
879                 tout = *timeout;
880
881         memcpy(saddr, sap, (size_t)salen);
882         client = nfs_gp_get_rpcbclient(saddr, salen, transport,
883                                                         RPCBVERS_4, &tout);
884         if (client != NULL) {
885                 if (nfs_gp_init_rpcb_parms(addr, program, version,
886                                                 protocol, &parms) != 0) {
887                         port = nfs_gp_rpcb_getaddr(client, &parms, tout);
888                         nfs_gp_free_rpcb_parms(&parms);
889                 }
890                 CLNT_DESTROY(client);
891         }
892
893         return port;
894 }
895
896 #else   /* !HAVE_LIBTIRPC */
897
898 unsigned short nfs_rpcb_getaddr(__attribute__((unused)) const struct sockaddr *sap,
899                                 __attribute__((unused)) const socklen_t salen,
900                                 __attribute__((unused)) const unsigned short transport,
901                                 __attribute__((unused)) const struct sockaddr *addr,
902                                 __attribute__((unused)) const rpcprog_t program,
903                                 __attribute__((unused)) const rpcvers_t version,
904                                 __attribute__((unused)) const unsigned short protocol,
905                                 __attribute__((unused)) const struct timeval *timeout)
906 {
907         rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
908         return 0;
909 }
910
911 #endif  /* !HAVE_LIBTIRPC */
912
913 /**
914  * nfs_pmap_getport - query rpcbind via the portmap protocol (rpcbindv2)
915  * @sin: pointer to AF_INET address of server to query
916  * @transport: transport protocol to use for the query
917  * @program: requested RPC program number
918  * @version: requested RPC version number
919  * @protocol: requested IPPROTO_ value of transport protocol
920  * @timeout: pointer to request timeout (NULL means use default timeout)
921  *
922  * Returns a positive integer representing the port number of the RPC service
923  * advertised by the server (in host byte order), or zero if the service is
924  * not advertised or there was some problem querying the server's rpcbind
925  * daemon.  rpccreateerr is set to reflect the underlying cause of the error.
926  *
927  * nfs_pmap_getport() is very similar to pmap_getport(), except that:
928  *
929  *  1.  This version always tries to use an ephemeral port, since reserved
930  *      ports are not needed for GETPORT queries.  This conserves the very
931  *      limited reserved port space, helping reduce failed socket binds
932  *      during mount storms.
933  *
934  *  2.  This version times out quickly by default.  It time-limits the
935  *      connect process as well as the actual RPC call, and even allows the
936  *      caller to specify the timeout.
937  *
938  *  3.  This version shares code with the rpcbindv3 and rpcbindv4 query
939  *      functions.  It can use a TI-RPC generated CLIENT.
940  */
941 unsigned long nfs_pmap_getport(const struct sockaddr_in *sin,
942                                const unsigned short transport,
943                                const unsigned long program,
944                                const unsigned long version,
945                                const unsigned long protocol,
946                                const struct timeval *timeout)
947 {
948         struct sockaddr_in address;
949         struct sockaddr *saddr = (struct sockaddr *)&address;
950         CLIENT *client;
951         struct pmap parms = {
952                 .pm_prog        = program,
953                 .pm_vers        = version,
954                 .pm_prot        = protocol,
955         };
956         struct timeval tout = { -1, 0 };
957         unsigned long port = 0;
958
959         if (timeout != NULL)
960                 tout = *timeout;
961
962         memcpy(saddr, sin, sizeof(address));
963         client = nfs_gp_get_rpcbclient(saddr, (socklen_t)sizeof(*sin),
964                                         transport, PMAPVERS, &tout);
965         if (client != NULL) {
966                 port = nfs_gp_pmap_getport(client, &parms, tout);
967                 CLNT_DESTROY(client);
968         }
969
970         return port;
971 }