]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/network.c
If an NFS server is only listening on TCP for portmap (as apparently
[nfs-utils.git] / utils / mount / network.c
1 /*
2  * network.c -- Provide common network functions for NFS mount/umount
3  *
4  * Copyright (C) 2007 Oracle.  All rights reserved.
5  * Copyright (C) 2007 Chuck Lever <chuck.lever@oracle.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public
18  * License along with this program; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 021110-1307, USA.
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <ctype.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <netdb.h>
35 #include <time.h>
36
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <rpc/rpc.h>
41 #include <rpc/pmap_prot.h>
42 #include <rpc/pmap_clnt.h>
43
44 #include "xcommon.h"
45 #include "mount.h"
46 #include "nls.h"
47 #include "nfs_mount.h"
48 #include "mount_constants.h"
49 #include "network.h"
50
51 #define PMAP_TIMEOUT    (10)
52 #define CONNECT_TIMEOUT (20)
53 #define MOUNT_TIMEOUT   (30)
54
55 #if SIZEOF_SOCKLEN_T - 0 == 0
56 #define socklen_t unsigned int
57 #endif
58
59 extern int nfs_mount_data_version;
60 extern char *progname;
61 extern int verbose;
62
63 static const unsigned long nfs_to_mnt[] = {
64         0,
65         0,
66         1,
67         3,
68 };
69
70 static const unsigned long mnt_to_nfs[] = {
71         0,
72         2,
73         2,
74         3,
75 };
76
77 /*
78  * Map an NFS version into the corresponding Mountd version
79  */
80 unsigned long nfsvers_to_mnt(const unsigned long vers)
81 {
82         if (vers <= 3)
83                 return nfs_to_mnt[vers];
84         return 0;
85 }
86
87 /*
88  * Map a Mountd version into the corresponding NFS version
89  */
90 static unsigned long mntvers_to_nfs(const unsigned long vers)
91 {
92         if (vers <= 3)
93                 return mnt_to_nfs[vers];
94         return 0;
95 }
96
97 static const unsigned int probe_udp_only[] = {
98         IPPROTO_UDP,
99         0,
100 };
101
102 static const unsigned int probe_udp_first[] = {
103         IPPROTO_UDP,
104         IPPROTO_TCP,
105         0,
106 };
107
108 static const unsigned int probe_tcp_first[] = {
109         IPPROTO_TCP,
110         IPPROTO_UDP,
111         0,
112 };
113
114 static const unsigned long probe_nfs2_only[] = {
115         2,
116         0,
117 };
118
119 static const unsigned long probe_nfs3_first[] = {
120         3,
121         2,
122         0,
123 };
124
125 static const unsigned long probe_mnt1_first[] = {
126         1,
127         2,
128         0,
129 };
130
131 static const unsigned long probe_mnt3_first[] = {
132         3,
133         1,
134         2,
135         0,
136 };
137
138 /**
139  * nfs_name_to_address - resolve hostname to an IPv4 or IPv6 socket address
140  * @hostname: pointer to C string containing DNS hostname to resolve
141  * @sap: pointer to buffer to fill with socket address
142  * @len: IN: size of buffer to fill; OUT: size of socket address
143  *
144  * Returns 1 and places a socket address at @sap if successful;
145  * otherwise zero.
146  */
147 int nfs_name_to_address(const char *hostname,
148                         const sa_family_t af_hint,
149                         struct sockaddr *sap, socklen_t *salen)
150 {
151         struct addrinfo *gai_results;
152         struct addrinfo gai_hint = {
153                 .ai_family      = af_hint,
154                 .ai_flags       = AI_ADDRCONFIG,
155         };
156         socklen_t len = *salen;
157         int error, ret = 0;
158
159         if (af_hint == AF_INET6)
160                 gai_hint.ai_flags |= AI_V4MAPPED|AI_ALL;
161
162         *salen = 0;
163
164         error = getaddrinfo(hostname, NULL, &gai_hint, &gai_results);
165         if (error) {
166                 nfs_error(_("%s: DNS resolution failed for %s: %s"),
167                         progname, hostname, (error == EAI_SYSTEM ?
168                                 strerror(errno) : gai_strerror(error)));
169                 return ret;
170         }
171
172         switch (gai_results->ai_addr->sa_family) {
173         case AF_INET:
174         case AF_INET6:
175                 if (len >= gai_results->ai_addrlen) {
176                         *salen = gai_results->ai_addrlen;
177                         memcpy(sap, gai_results->ai_addr, *salen);
178                         ret = 1;
179                 }
180                 break;
181         default:
182                 /* things are really broken if we get here, so warn */
183                 nfs_error(_("%s: unrecognized DNS resolution results for %s"),
184                                 progname, hostname);
185                 break;
186         }
187
188         freeaddrinfo(gai_results);
189         return ret;
190 }
191
192 /**
193  * nfs_gethostbyname - resolve a hostname to an IPv4 address
194  * @hostname: pointer to a C string containing a DNS hostname
195  * @saddr: returns an IPv4 address 
196  *
197  * Returns 1 if successful, otherwise zero.
198  */
199 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *sin)
200 {
201         socklen_t len = sizeof(*sin);
202
203         return nfs_name_to_address(hostname, AF_INET,
204                                         (struct sockaddr *)sin, &len);
205 }
206
207 /**
208  * nfs_string_to_sockaddr - convert string address to sockaddr
209  * @address:    pointer to presentation format address to convert
210  * @addrlen:    length of presentation address
211  * @sap:        pointer to socket address buffer to fill in
212  * @salen:      IN: length of address buffer
213  *              OUT: length of converted socket address
214  *
215  * Convert a presentation format address string to a socket address.
216  * Similar to nfs_name_to_address(), but the DNS query is squelched,
217  * and won't make any noise if the getaddrinfo() call fails.
218  *
219  * Returns 1 and fills in @sap and @salen if successful; otherwise zero.
220  *
221  * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
222  * on presenting IPv6 addresses as text strings.
223  */
224 int nfs_string_to_sockaddr(const char *address, const size_t addrlen,
225                            struct sockaddr *sap, socklen_t *salen)
226 {
227         struct addrinfo *gai_results;
228         struct addrinfo gai_hint = {
229                 .ai_flags       = AI_NUMERICHOST,
230         };
231         socklen_t len = *salen;
232         int ret = 0;
233
234         *salen = 0;
235
236         if (getaddrinfo(address, NULL, &gai_hint, &gai_results) == 0) {
237                 switch (gai_results->ai_addr->sa_family) {
238                 case AF_INET:
239                 case AF_INET6:
240                         if (len >= gai_results->ai_addrlen) {
241                                 *salen = gai_results->ai_addrlen;
242                                 memcpy(sap, gai_results->ai_addr, *salen);
243                                 ret = 1;
244                         }
245                         break;
246                 }
247                 freeaddrinfo(gai_results);
248         }
249
250         return ret;
251 }
252
253 /**
254  * nfs_present_sockaddr - convert sockaddr to string
255  * @sap: pointer to socket address to convert
256  * @salen: length of socket address
257  * @buf: pointer to buffer to fill in
258  * @buflen: length of buffer
259  *
260  * Convert the passed-in sockaddr-style address to presentation format.
261  * The presentation format address is placed in @buf and is
262  * '\0'-terminated.
263  *
264  * Returns 1 if successful; otherwise zero.
265  *
266  * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
267  * on presenting IPv6 addresses as text strings.
268  */
269 int nfs_present_sockaddr(const struct sockaddr *sap, const socklen_t salen,
270                          char *buf, const size_t buflen)
271 {
272 #ifdef HAVE_GETNAMEINFO
273         int result;
274
275         result = getnameinfo(sap, salen, buf, buflen,
276                                         NULL, 0, NI_NUMERICHOST);
277         if (!result)
278                 return 1;
279
280         nfs_error(_("%s: invalid server address: %s"), progname,
281                         gai_strerror(result));
282         return 0;
283 #else   /* HAVE_GETNAMEINFO */
284         char *addr;
285
286         if (sap->sa_family == AF_INET) {
287                 addr = inet_ntoa(((struct sockaddr_in *)sap)->sin_addr);
288                 if (addr && strlen(addr) < buflen) {
289                         strcpy(buf, addr);
290                         return 1;
291                 }
292         }
293
294         nfs_error(_("%s: invalid server address"), progname);
295         return 0;
296 #endif  /* HAVE_GETNAMEINFO */
297 }
298
299 /*
300  * Attempt to connect a socket, but time out after "timeout" seconds.
301  *
302  * On error return, caller closes the socket.
303  */
304 static int connect_to(int fd, struct sockaddr *addr,
305                         socklen_t addrlen, int timeout)
306 {
307         int ret, saved;
308         fd_set rset, wset;
309         struct timeval tv = {
310                 .tv_sec = timeout,
311         };
312
313         saved = fcntl(fd, F_GETFL, 0);
314         fcntl(fd, F_SETFL, saved | O_NONBLOCK);
315
316         ret = connect(fd, addr, addrlen);
317         if (ret < 0 && errno != EINPROGRESS)
318                 return -1;
319         if (ret == 0)
320                 goto out;
321
322         FD_ZERO(&rset);
323         FD_SET(fd, &rset);
324         wset = rset;
325         ret = select(fd + 1, &rset, &wset, NULL, &tv);
326         if (ret == 0) {
327                 errno = ETIMEDOUT;
328                 return -1;
329         }
330         if (FD_ISSET(fd, &rset) || FD_ISSET(fd, &wset)) {
331                 int error;
332                 socklen_t len = sizeof(error);
333                 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
334                         return -1;
335                 if (error) {
336                         errno = error;
337                         return -1;
338                 }
339         } else
340                 return -1;
341
342 out:
343         fcntl(fd, F_SETFL, saved);
344         return 0;
345 }
346
347 /*
348  * Create a socket that is locally bound to a reserved or non-reserved port.
349  *
350  * The caller should check rpc_createerr to determine the cause of any error.
351  */
352 static int get_socket(struct sockaddr_in *saddr, unsigned int p_prot,
353                         unsigned int timeout, int resvp, int conn)
354 {
355         int so, cc, type;
356         struct sockaddr_in laddr;
357         socklen_t namelen = sizeof(laddr);
358
359         type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
360         if ((so = socket (AF_INET, type, p_prot)) < 0)
361                 goto err_socket;
362
363         laddr.sin_family = AF_INET;
364         laddr.sin_port = 0;
365         laddr.sin_addr.s_addr = htonl(INADDR_ANY);
366         if (resvp) {
367                 if (bindresvport(so, &laddr) < 0)
368                         goto err_bindresvport;
369         } else {
370                 cc = bind(so, (struct sockaddr *)&laddr, namelen);
371                 if (cc < 0)
372                         goto err_bind;
373         }
374         if (type == SOCK_STREAM || (conn && type == SOCK_DGRAM)) {
375                 cc = connect_to(so, (struct sockaddr *)saddr, namelen,
376                                 timeout);
377                 if (cc < 0)
378                         goto err_connect;
379         }
380         return so;
381
382 err_socket:
383         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
384         rpc_createerr.cf_error.re_errno = errno;
385         if (verbose) {
386                 nfs_error(_("%s: Unable to create %s socket: errno %d (%s)\n"),
387                         progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
388                         errno, strerror(errno));
389         }
390         return RPC_ANYSOCK;
391
392 err_bindresvport:
393         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
394         rpc_createerr.cf_error.re_errno = errno;
395         if (verbose) {
396                 nfs_error(_("%s: Unable to bindresvport %s socket: errno %d"
397                                 " (%s)\n"),
398                         progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
399                         errno, strerror(errno));
400         }
401         close(so);
402         return RPC_ANYSOCK;
403
404 err_bind:
405         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
406         rpc_createerr.cf_error.re_errno = errno;
407         if (verbose) {
408                 nfs_error(_("%s: Unable to bind to %s socket: errno %d (%s)\n"),
409                         progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
410                         errno, strerror(errno));
411         }
412         close(so);
413         return RPC_ANYSOCK;
414
415 err_connect:
416         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
417         rpc_createerr.cf_error.re_errno = errno;
418         if (verbose) {
419                 nfs_error(_("%s: Unable to connect to %s:%d, errno %d (%s)\n"),
420                         progname, inet_ntoa(saddr->sin_addr),
421                         ntohs(saddr->sin_port), errno, strerror(errno));
422         }
423         close(so);
424         return RPC_ANYSOCK;
425 }
426
427 /*
428  * getport() is very similar to pmap_getport() with the exception that
429  * this version tries to use an ephemeral port, since reserved ports are
430  * not needed for GETPORT queries.  This conserves the very limited
431  * reserved port space, which helps reduce failed socket binds
432  * during mount storms.
433  *
434  * A side effect of calling this function is that rpccreateerr is set.
435  */
436 static unsigned short getport(struct sockaddr_in *saddr,
437                                 unsigned long program,
438                                 unsigned long version,
439                                 unsigned int proto)
440 {
441         struct sockaddr_in bind_saddr;
442         unsigned short port = 0;
443         int socket;
444         CLIENT *clnt = NULL;
445         enum clnt_stat stat;
446  
447         bind_saddr = *saddr;
448         bind_saddr.sin_port = htons(PMAPPORT);
449
450         socket = get_socket(&bind_saddr, proto, PMAP_TIMEOUT, FALSE, FALSE);
451         if (socket == RPC_ANYSOCK) {
452                 if (proto == IPPROTO_TCP &&
453                     rpc_createerr.cf_error.re_errno == ETIMEDOUT)
454                         rpc_createerr.cf_stat = RPC_TIMEDOUT;
455                 return 0;
456         }
457
458         switch (proto) {
459         case IPPROTO_UDP:
460                 clnt = clntudp_bufcreate(&bind_saddr,
461                                          PMAPPROG, PMAPVERS,
462                                          RETRY_TIMEOUT, &socket,
463                                          RPCSMALLMSGSIZE,
464                                          RPCSMALLMSGSIZE);
465                 break;
466         case IPPROTO_TCP:
467                 clnt = clnttcp_create(&bind_saddr,
468                                       PMAPPROG, PMAPVERS,
469                                       &socket,
470                                       RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
471                 break;
472         }
473         if (clnt != NULL) {
474                 struct pmap parms = {
475                         .pm_prog        = program,
476                         .pm_vers        = version,
477                         .pm_prot        = proto,
478                 };
479
480                 stat = clnt_call(clnt, PMAPPROC_GETPORT,
481                                  (xdrproc_t)xdr_pmap, (caddr_t)&parms,
482                                  (xdrproc_t)xdr_u_short, (caddr_t)&port,
483                                  TIMEOUT);
484                 if (stat) {
485                         clnt_geterr(clnt, &rpc_createerr.cf_error);
486                         rpc_createerr.cf_stat = stat;
487                 }
488                 clnt_destroy(clnt);
489                 if (stat != RPC_SUCCESS)
490                         port = 0;
491                 else if (port == 0)
492                         rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
493         }
494         close(socket);
495
496         return port;
497 }
498
499 /*
500  * Use the portmapper to discover whether or not the service we want is
501  * available. The lists 'versions' and 'protos' define ordered sequences
502  * of service versions and udp/tcp protocols to probe for.
503  */
504 static int probe_port(clnt_addr_t *server, const unsigned long *versions,
505                         const unsigned int *protos)
506 {
507         struct sockaddr_in *saddr = &server->saddr;
508         struct pmap *pmap = &server->pmap;
509         const unsigned long prog = pmap->pm_prog, *p_vers;
510         const unsigned int prot = (u_int)pmap->pm_prot, *p_prot;
511         const u_short port = (u_short) pmap->pm_port;
512         unsigned long vers = pmap->pm_vers;
513         unsigned short p_port;
514
515         p_prot = prot ? &prot : protos;
516         p_vers = vers ? &vers : versions;
517         rpc_createerr.cf_stat = 0;
518         for (;;) {
519                 p_port = getport(saddr, prog, *p_vers, *p_prot);
520                 if (p_port) {
521                         if (!port || port == p_port) {
522                                 saddr->sin_port = htons(p_port);
523                                 if (verbose) {
524                                         printf(_("%s: trying %s prog %ld vers "
525                                                 "%ld prot %s port %d\n"),
526                                                 progname,
527                                                 inet_ntoa(saddr->sin_addr),
528                                                 prog, *p_vers,
529                                                 *p_prot == IPPROTO_UDP ?
530                                                         _("UDP") : _("TCP"),
531                                                 p_port);
532                                 }
533                                 if (clnt_ping(saddr, prog, *p_vers, *p_prot, NULL))
534                                         goto out_ok;
535                         }
536                 }
537                 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
538                     rpc_createerr.cf_stat != RPC_TIMEDOUT &&
539                     rpc_createerr.cf_stat != RPC_PROGVERSMISMATCH)
540                         goto out_bad;
541
542                 if (!prot) {
543                         if (*++p_prot)
544                                 continue;
545                         p_prot = protos;
546                 }
547                 if (rpc_createerr.cf_stat == RPC_TIMEDOUT)
548                         goto out_bad;
549
550                 if (vers || !*++p_vers)
551                         break;
552         }
553
554 out_bad:
555         return 0;
556
557 out_ok:
558         if (!vers)
559                 pmap->pm_vers = *p_vers;
560         if (!prot)
561                 pmap->pm_prot = *p_prot;
562         if (!port)
563                 pmap->pm_port = p_port;
564         rpc_createerr.cf_stat = 0;
565         return 1;
566 }
567
568 static int probe_nfsport(clnt_addr_t *nfs_server)
569 {
570         struct pmap *pmap = &nfs_server->pmap;
571
572         if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
573                 return 1;
574
575         if (nfs_mount_data_version >= 4)
576                 return probe_port(nfs_server, probe_nfs3_first, probe_tcp_first);
577         else
578                 return probe_port(nfs_server, probe_nfs2_only, probe_udp_only);
579 }
580
581 static int probe_mntport(clnt_addr_t *mnt_server)
582 {
583         struct pmap *pmap = &mnt_server->pmap;
584
585         if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
586                 return 1;
587
588         if (nfs_mount_data_version >= 4)
589                 return probe_port(mnt_server, probe_mnt3_first, probe_udp_first);
590         else
591                 return probe_port(mnt_server, probe_mnt1_first, probe_udp_only);
592 }
593
594 /**
595  * probe_bothports - discover the RPC endpoints of mountd and NFS server
596  * @mnt_server: pointer to address and pmap argument for mountd results
597  * @nfs_server: pointer to address and pmap argument for NFS server
598  *
599  * Returns 1 if successful, otherwise zero if some error occurred.
600  * Note that the arguments are both input and output arguments.
601  *
602  * A side effect of calling this function is that rpccreateerr is set.
603  */
604 int probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
605 {
606         struct pmap *nfs_pmap = &nfs_server->pmap;
607         struct pmap *mnt_pmap = &mnt_server->pmap;
608         struct pmap save_nfs, save_mnt;
609         int res;
610         const unsigned long *probe_vers;
611
612         if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
613                 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
614         else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
615                 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
616         if (nfs_pmap->pm_vers)
617                 goto version_fixed;
618
619         memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
620         memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
621         probe_vers = (nfs_mount_data_version >= 4) ?
622                         probe_mnt3_first : probe_mnt1_first;
623
624         for (; *probe_vers; probe_vers++) {
625                 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
626                 if ((res = probe_nfsport(nfs_server) != 0)) {
627                         mnt_pmap->pm_vers = *probe_vers;
628                         if ((res = probe_mntport(mnt_server)) != 0)
629                                 return 1;
630                         memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
631                 }
632                 switch (rpc_createerr.cf_stat) {
633                 case RPC_PROGVERSMISMATCH:
634                 case RPC_PROGNOTREGISTERED:
635                         break;
636                 default:
637                         goto out_bad;
638                 }
639                 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
640         }
641
642 out_bad:
643         return 0;
644
645 version_fixed:
646         if (!probe_nfsport(nfs_server))
647                 goto out_bad;
648         return probe_mntport(mnt_server);
649 }
650
651 static int probe_statd(void)
652 {
653         struct sockaddr_in addr;
654         unsigned short port;
655
656         memset(&addr, 0, sizeof(addr));
657         addr.sin_family = AF_INET;
658         addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
659         port = getport(&addr, 100024, 1, IPPROTO_UDP);
660
661         if (port == 0)
662                 return 0;
663         addr.sin_port = htons(port);
664
665         if (clnt_ping(&addr, 100024, 1, IPPROTO_UDP, NULL) <= 0)
666                 return 0;
667
668         return 1;
669 }
670
671 /**
672  * start_statd - attempt to start rpc.statd
673  *
674  * Returns 1 if statd is running; otherwise zero.
675  */
676 int start_statd(void)
677 {
678 #ifdef START_STATD
679         struct stat stb;
680 #endif
681
682         if (probe_statd())
683                 return 1;
684
685 #ifdef START_STATD
686         if (stat(START_STATD, &stb) == 0) {
687                 if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {
688                         system(START_STATD);
689                         if (probe_statd())
690                                 return 1;
691                 }
692         }
693 #endif
694
695         return 0;
696 }
697
698 /**
699  * nfs_call_umount - ask the server to remove a share from it's rmtab
700  * @mnt_server: address of RPC MNT program server
701  * @argp: directory path of share to "unmount"
702  *
703  * Returns one if the unmount call succeeded; zero if the unmount
704  * failed for any reason.
705  *
706  * Note that a side effect of calling this function is that rpccreateerr
707  * is set.
708  */
709 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
710 {
711         CLIENT *clnt;
712         enum clnt_stat res = 0;
713         int msock;
714
715         if (!probe_mntport(mnt_server))
716                 return 0;
717         clnt = mnt_openclnt(mnt_server, &msock);
718         if (!clnt)
719                 return 0;
720         res = clnt_call(clnt, MOUNTPROC_UMNT,
721                         (xdrproc_t)xdr_dirpath, (caddr_t)argp,
722                         (xdrproc_t)xdr_void, NULL,
723                         TIMEOUT);
724         mnt_closeclnt(clnt, msock);
725
726         if (res == RPC_SUCCESS)
727                 return 1;
728         return 0;
729 }
730
731 /**
732  * mnt_openclnt - get a handle for a remote mountd service
733  * @mnt_server: address and pmap arguments of mountd service
734  * @msock: returns a file descriptor of the underlying transport socket
735  *
736  * Returns an active handle for the remote's mountd service
737  */
738 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
739 {
740         struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
741         struct pmap *mnt_pmap = &mnt_server->pmap;
742         CLIENT *clnt = NULL;
743
744         mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
745         *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, MOUNT_TIMEOUT,
746                                 TRUE, FALSE);
747         if (*msock == RPC_ANYSOCK) {
748                 if (rpc_createerr.cf_error.re_errno == EADDRINUSE)
749                         /*
750                          * Probably in-use by a TIME_WAIT connection,
751                          * It is worth waiting a while and trying again.
752                          */
753                         rpc_createerr.cf_stat = RPC_TIMEDOUT;
754                 return NULL;
755         }
756
757         switch (mnt_pmap->pm_prot) {
758         case IPPROTO_UDP:
759                 clnt = clntudp_bufcreate(mnt_saddr,
760                                          mnt_pmap->pm_prog, mnt_pmap->pm_vers,
761                                          RETRY_TIMEOUT, msock,
762                                          MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
763                 break;
764         case IPPROTO_TCP:
765                 clnt = clnttcp_create(mnt_saddr,
766                                       mnt_pmap->pm_prog, mnt_pmap->pm_vers,
767                                       msock,
768                                       MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
769                 break;
770         }
771         if (clnt) {
772                 /* try to mount hostname:dirname */
773                 clnt->cl_auth = authunix_create_default();
774                 return clnt;
775         }
776         return NULL;
777 }
778
779 /**
780  * mnt_closeclnt - terminate a handle for a remote mountd service
781  * @clnt: pointer to an active handle for a remote mountd service
782  * @msock: file descriptor of the underlying transport socket
783  *
784  */
785 void mnt_closeclnt(CLIENT *clnt, int msock)
786 {
787         auth_destroy(clnt->cl_auth);
788         clnt_destroy(clnt);
789         close(msock);
790 }
791
792 /**
793  * clnt_ping - send an RPC ping to the remote RPC service endpoint
794  * @saddr: server's address
795  * @prog: target RPC program number
796  * @vers: target RPC version number
797  * @prot: target RPC protocol
798  * @caddr: filled in with our network address
799  *
800  * Sigh... getport() doesn't actually check the version number.
801  * In order to make sure that the server actually supports the service
802  * we're requesting, we open and RPC client, and fire off a NULL
803  * RPC call.
804  *
805  * caddr is the network address that the server will use to call us back.
806  * On multi-homed clients, this address depends on which NIC we use to
807  * route requests to the server.
808  *
809  * Returns one if successful, otherwise zero.
810  */
811 int clnt_ping(struct sockaddr_in *saddr, const unsigned long prog,
812                 const unsigned long vers, const unsigned int prot,
813                 struct sockaddr_in *caddr)
814 {
815         CLIENT *clnt = NULL;
816         int sock, stat;
817         static char clnt_res;
818         struct sockaddr dissolve;
819
820         rpc_createerr.cf_stat = stat = 0;
821         sock = get_socket(saddr, prot, CONNECT_TIMEOUT, FALSE, TRUE);
822         if (sock == RPC_ANYSOCK) {
823                 if (rpc_createerr.cf_error.re_errno == ETIMEDOUT) {
824                         /*
825                          * TCP timeout. Bubble up the error to see 
826                          * how it should be handled.
827                          */
828                         rpc_createerr.cf_stat = RPC_TIMEDOUT;
829                 }
830                 return 0;
831         }
832
833         if (caddr) {
834                 /* Get the address of our end of this connection */
835                 socklen_t len = sizeof(*caddr);
836                 if (getsockname(sock, caddr, &len) != 0)
837                         caddr->sin_family = 0;
838         }
839
840         switch(prot) {
841         case IPPROTO_UDP:
842                 /* The socket is connected (so we could getsockname successfully),
843                  * but some servers on multi-homed hosts reply from
844                  * the wrong address, so if we stay connected, we lose the reply.
845                  */
846                 dissolve.sa_family = AF_UNSPEC;
847                 connect(sock, &dissolve, sizeof(dissolve));
848
849                 clnt = clntudp_bufcreate(saddr, prog, vers,
850                                          RETRY_TIMEOUT, &sock,
851                                          RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
852                 break;
853         case IPPROTO_TCP:
854                 clnt = clnttcp_create(saddr, prog, vers, &sock,
855                                       RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
856                 break;
857         }
858         if (!clnt) {
859                 close(sock);
860                 return 0;
861         }
862         memset(&clnt_res, 0, sizeof(clnt_res));
863         stat = clnt_call(clnt, NULLPROC,
864                          (xdrproc_t)xdr_void, (caddr_t)NULL,
865                          (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
866                          TIMEOUT);
867         if (stat) {
868                 clnt_geterr(clnt, &rpc_createerr.cf_error);
869                 rpc_createerr.cf_stat = stat;
870         }
871         clnt_destroy(clnt);
872         close(sock);
873
874         if (stat == RPC_SUCCESS)
875                 return 1;
876         else
877                 return 0;
878 }
879
880 /*
881  * Try a getsockname() on a connected datagram socket.
882  *
883  * Returns 1 and fills in @buf if successful; otherwise, zero.
884  *
885  * A connected datagram socket prevents leaving a socket in TIME_WAIT.
886  * This conserves the ephemeral port number space, helping reduce failed
887  * socket binds during mount storms.
888  */
889 static int nfs_ca_sockname(const struct sockaddr *sap, const socklen_t salen,
890                            struct sockaddr *buf, socklen_t *buflen)
891 {
892         struct sockaddr_in sin = {
893                 .sin_family             = AF_INET,
894                 .sin_addr.s_addr        = htonl(INADDR_ANY),
895         };
896         struct sockaddr_in6 sin6 = {
897                 .sin6_family            = AF_INET6,
898                 .sin6_addr              = IN6ADDR_ANY_INIT,
899         };
900         int sock;
901
902         sock = socket(sap->sa_family, SOCK_DGRAM, IPPROTO_UDP);
903         if (sock < 0)
904                 return 0;
905
906         switch (sap->sa_family) {
907         case AF_INET:
908                 if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
909                         close(sock);
910                         return 0;
911                 }
912                 break;
913         case AF_INET6:
914                 if (bind(sock, (struct sockaddr *)&sin6, sizeof(sin6)) < 0) {
915                         close(sock);
916                         return 0;
917                 }
918                 break;
919         default:
920                 errno = EAFNOSUPPORT;
921                 return 0;
922         }
923
924         if (connect(sock, sap, salen) < 0) {
925                 close(sock);
926                 return 0;
927         }
928
929         return !getsockname(sock, buf, buflen);
930 }
931
932 /*
933  * Try to generate an address that prevents the server from calling us.
934  *
935  * Returns 1 and fills in @buf if successful; otherwise, zero.
936  */
937 static int nfs_ca_gai(const struct sockaddr *sap, const socklen_t salen,
938                       struct sockaddr *buf, socklen_t *buflen)
939 {
940         struct addrinfo *gai_results;
941         struct addrinfo gai_hint = {
942                 .ai_family      = sap->sa_family,
943                 .ai_flags       = AI_PASSIVE,   /* ANYADDR */
944         };
945
946         if (getaddrinfo(NULL, "", &gai_hint, &gai_results))
947                 return 0;
948
949         *buflen = gai_results->ai_addrlen;
950         memcpy(buf, gai_results->ai_addr, *buflen);
951
952         freeaddrinfo(gai_results);
953
954         return 1;
955 }
956
957 /**
958  * nfs_callback_address - acquire our local network address
959  * @sap: pointer to address of remote
960  * @sap_len: length of address
961  * @buf: pointer to buffer to be filled in with local network address
962  * @buflen: IN: length of buffer to fill in; OUT: length of filled-in address
963  *
964  * Discover a network address that an NFSv4 server can use to call us back.
965  * On multi-homed clients, this address depends on which NIC we use to
966  * route requests to the server.
967  *
968  * Returns 1 and fills in @buf if an unambiguous local address is
969  * available; returns 1 and fills in an appropriate ANYADDR address
970  * if a local address isn't available; otherwise, returns zero.
971  */
972 int nfs_callback_address(const struct sockaddr *sap, const socklen_t salen,
973                          struct sockaddr *buf, socklen_t *buflen)
974 {
975         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
976
977         if (nfs_ca_sockname(sap, salen, buf, buflen) == 0)
978                 if (nfs_ca_gai(sap, salen, buf, buflen) == 0)
979                         goto out_failed;
980
981         /*
982          * The server can't use an interface ID that was generated
983          * here on the client, so always clear sin6_scope_id.
984          */
985         if (sin6->sin6_family == AF_INET6)
986                 sin6->sin6_scope_id = 0;
987
988         return 1;
989
990 out_failed:
991         *buflen = 0;
992         if (verbose)
993                 nfs_error(_("%s: failed to construct callback address"));
994         return 0;
995
996 }