]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/network.c
7d3d09aacdfca9ce9992caf788fc6b214e68a61f
[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                                 if (rpc_createerr.cf_stat == RPC_TIMEDOUT)
536                                         goto out_bad;
537                         }
538                 }
539                 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
540                     rpc_createerr.cf_stat != RPC_PROGVERSMISMATCH)
541                         goto out_bad;
542
543                 if (!prot) {
544                         if (*++p_prot)
545                                 continue;
546                         p_prot = protos;
547                 }
548                 if (vers || !*++p_vers)
549                         break;
550         }
551
552 out_bad:
553         return 0;
554
555 out_ok:
556         if (!vers)
557                 pmap->pm_vers = *p_vers;
558         if (!prot)
559                 pmap->pm_prot = *p_prot;
560         if (!port)
561                 pmap->pm_port = p_port;
562         rpc_createerr.cf_stat = 0;
563         return 1;
564 }
565
566 static int probe_nfsport(clnt_addr_t *nfs_server)
567 {
568         struct pmap *pmap = &nfs_server->pmap;
569
570         if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
571                 return 1;
572
573         if (nfs_mount_data_version >= 4)
574                 return probe_port(nfs_server, probe_nfs3_first, probe_tcp_first);
575         else
576                 return probe_port(nfs_server, probe_nfs2_only, probe_udp_only);
577 }
578
579 static int probe_mntport(clnt_addr_t *mnt_server)
580 {
581         struct pmap *pmap = &mnt_server->pmap;
582
583         if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
584                 return 1;
585
586         if (nfs_mount_data_version >= 4)
587                 return probe_port(mnt_server, probe_mnt3_first, probe_udp_first);
588         else
589                 return probe_port(mnt_server, probe_mnt1_first, probe_udp_only);
590 }
591
592 /**
593  * probe_bothports - discover the RPC endpoints of mountd and NFS server
594  * @mnt_server: pointer to address and pmap argument for mountd results
595  * @nfs_server: pointer to address and pmap argument for NFS server
596  *
597  * Returns 1 if successful, otherwise zero if some error occurred.
598  * Note that the arguments are both input and output arguments.
599  *
600  * A side effect of calling this function is that rpccreateerr is set.
601  */
602 int probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
603 {
604         struct pmap *nfs_pmap = &nfs_server->pmap;
605         struct pmap *mnt_pmap = &mnt_server->pmap;
606         struct pmap save_nfs, save_mnt;
607         int res;
608         const unsigned long *probe_vers;
609
610         if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
611                 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
612         else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
613                 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
614         if (nfs_pmap->pm_vers)
615                 goto version_fixed;
616
617         memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
618         memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
619         probe_vers = (nfs_mount_data_version >= 4) ?
620                         probe_mnt3_first : probe_mnt1_first;
621
622         for (; *probe_vers; probe_vers++) {
623                 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
624                 if ((res = probe_nfsport(nfs_server) != 0)) {
625                         mnt_pmap->pm_vers = *probe_vers;
626                         if ((res = probe_mntport(mnt_server)) != 0)
627                                 return 1;
628                         memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
629                 }
630                 switch (rpc_createerr.cf_stat) {
631                 case RPC_PROGVERSMISMATCH:
632                 case RPC_PROGNOTREGISTERED:
633                         break;
634                 default:
635                         goto out_bad;
636                 }
637                 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
638         }
639
640 out_bad:
641         return 0;
642
643 version_fixed:
644         if (!probe_nfsport(nfs_server))
645                 goto out_bad;
646         return probe_mntport(mnt_server);
647 }
648
649 static int probe_statd(void)
650 {
651         struct sockaddr_in addr;
652         unsigned short port;
653
654         memset(&addr, 0, sizeof(addr));
655         addr.sin_family = AF_INET;
656         addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
657         port = getport(&addr, 100024, 1, IPPROTO_UDP);
658
659         if (port == 0)
660                 return 0;
661         addr.sin_port = htons(port);
662
663         if (clnt_ping(&addr, 100024, 1, IPPROTO_UDP, NULL) <= 0)
664                 return 0;
665
666         return 1;
667 }
668
669 /**
670  * start_statd - attempt to start rpc.statd
671  *
672  * Returns 1 if statd is running; otherwise zero.
673  */
674 int start_statd(void)
675 {
676 #ifdef START_STATD
677         struct stat stb;
678 #endif
679
680         if (probe_statd())
681                 return 1;
682
683 #ifdef START_STATD
684         if (stat(START_STATD, &stb) == 0) {
685                 if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {
686                         system(START_STATD);
687                         if (probe_statd())
688                                 return 1;
689                 }
690         }
691 #endif
692
693         return 0;
694 }
695
696 /**
697  * nfs_call_umount - ask the server to remove a share from it's rmtab
698  * @mnt_server: address of RPC MNT program server
699  * @argp: directory path of share to "unmount"
700  *
701  * Returns one if the unmount call succeeded; zero if the unmount
702  * failed for any reason.
703  *
704  * Note that a side effect of calling this function is that rpccreateerr
705  * is set.
706  */
707 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
708 {
709         CLIENT *clnt;
710         enum clnt_stat res = 0;
711         int msock;
712
713         if (!probe_mntport(mnt_server))
714                 return 0;
715         clnt = mnt_openclnt(mnt_server, &msock);
716         if (!clnt)
717                 return 0;
718         res = clnt_call(clnt, MOUNTPROC_UMNT,
719                         (xdrproc_t)xdr_dirpath, (caddr_t)argp,
720                         (xdrproc_t)xdr_void, NULL,
721                         TIMEOUT);
722         mnt_closeclnt(clnt, msock);
723
724         if (res == RPC_SUCCESS)
725                 return 1;
726         return 0;
727 }
728
729 /**
730  * mnt_openclnt - get a handle for a remote mountd service
731  * @mnt_server: address and pmap arguments of mountd service
732  * @msock: returns a file descriptor of the underlying transport socket
733  *
734  * Returns an active handle for the remote's mountd service
735  */
736 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
737 {
738         struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
739         struct pmap *mnt_pmap = &mnt_server->pmap;
740         CLIENT *clnt = NULL;
741
742         mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
743         *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, MOUNT_TIMEOUT,
744                                 TRUE, FALSE);
745         if (*msock == RPC_ANYSOCK) {
746                 if (rpc_createerr.cf_error.re_errno == EADDRINUSE)
747                         /*
748                          * Probably in-use by a TIME_WAIT connection,
749                          * It is worth waiting a while and trying again.
750                          */
751                         rpc_createerr.cf_stat = RPC_TIMEDOUT;
752                 return NULL;
753         }
754
755         switch (mnt_pmap->pm_prot) {
756         case IPPROTO_UDP:
757                 clnt = clntudp_bufcreate(mnt_saddr,
758                                          mnt_pmap->pm_prog, mnt_pmap->pm_vers,
759                                          RETRY_TIMEOUT, msock,
760                                          MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
761                 break;
762         case IPPROTO_TCP:
763                 clnt = clnttcp_create(mnt_saddr,
764                                       mnt_pmap->pm_prog, mnt_pmap->pm_vers,
765                                       msock,
766                                       MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
767                 break;
768         }
769         if (clnt) {
770                 /* try to mount hostname:dirname */
771                 clnt->cl_auth = authunix_create_default();
772                 return clnt;
773         }
774         return NULL;
775 }
776
777 /**
778  * mnt_closeclnt - terminate a handle for a remote mountd service
779  * @clnt: pointer to an active handle for a remote mountd service
780  * @msock: file descriptor of the underlying transport socket
781  *
782  */
783 void mnt_closeclnt(CLIENT *clnt, int msock)
784 {
785         auth_destroy(clnt->cl_auth);
786         clnt_destroy(clnt);
787         close(msock);
788 }
789
790 /**
791  * clnt_ping - send an RPC ping to the remote RPC service endpoint
792  * @saddr: server's address
793  * @prog: target RPC program number
794  * @vers: target RPC version number
795  * @prot: target RPC protocol
796  * @caddr: filled in with our network address
797  *
798  * Sigh... getport() doesn't actually check the version number.
799  * In order to make sure that the server actually supports the service
800  * we're requesting, we open and RPC client, and fire off a NULL
801  * RPC call.
802  *
803  * caddr is the network address that the server will use to call us back.
804  * On multi-homed clients, this address depends on which NIC we use to
805  * route requests to the server.
806  *
807  * Returns one if successful, otherwise zero.
808  */
809 int clnt_ping(struct sockaddr_in *saddr, const unsigned long prog,
810                 const unsigned long vers, const unsigned int prot,
811                 struct sockaddr_in *caddr)
812 {
813         CLIENT *clnt = NULL;
814         int sock, stat;
815         static char clnt_res;
816         struct sockaddr dissolve;
817
818         rpc_createerr.cf_stat = stat = 0;
819         sock = get_socket(saddr, prot, CONNECT_TIMEOUT, FALSE, TRUE);
820         if (sock == RPC_ANYSOCK) {
821                 if (rpc_createerr.cf_error.re_errno == ETIMEDOUT) {
822                         /*
823                          * TCP timeout. Bubble up the error to see 
824                          * how it should be handled.
825                          */
826                         rpc_createerr.cf_stat = RPC_TIMEDOUT;
827                 }
828                 return 0;
829         }
830
831         if (caddr) {
832                 /* Get the address of our end of this connection */
833                 socklen_t len = sizeof(*caddr);
834                 if (getsockname(sock, caddr, &len) != 0)
835                         caddr->sin_family = 0;
836         }
837
838         switch(prot) {
839         case IPPROTO_UDP:
840                 /* The socket is connected (so we could getsockname successfully),
841                  * but some servers on multi-homed hosts reply from
842                  * the wrong address, so if we stay connected, we lose the reply.
843                  */
844                 dissolve.sa_family = AF_UNSPEC;
845                 connect(sock, &dissolve, sizeof(dissolve));
846
847                 clnt = clntudp_bufcreate(saddr, prog, vers,
848                                          RETRY_TIMEOUT, &sock,
849                                          RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
850                 break;
851         case IPPROTO_TCP:
852                 clnt = clnttcp_create(saddr, prog, vers, &sock,
853                                       RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
854                 break;
855         }
856         if (!clnt) {
857                 close(sock);
858                 return 0;
859         }
860         memset(&clnt_res, 0, sizeof(clnt_res));
861         stat = clnt_call(clnt, NULLPROC,
862                          (xdrproc_t)xdr_void, (caddr_t)NULL,
863                          (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
864                          TIMEOUT);
865         if (stat) {
866                 clnt_geterr(clnt, &rpc_createerr.cf_error);
867                 rpc_createerr.cf_stat = stat;
868         }
869         clnt_destroy(clnt);
870         close(sock);
871
872         if (stat == RPC_SUCCESS)
873                 return 1;
874         else
875                 return 0;
876 }
877
878 /*
879  * Try a getsockname() on a connected datagram socket.
880  *
881  * Returns 1 and fills in @buf if successful; otherwise, zero.
882  *
883  * A connected datagram socket prevents leaving a socket in TIME_WAIT.
884  * This conserves the ephemeral port number space, helping reduce failed
885  * socket binds during mount storms.
886  */
887 static int nfs_ca_sockname(const struct sockaddr *sap, const socklen_t salen,
888                            struct sockaddr *buf, socklen_t *buflen)
889 {
890         struct sockaddr_in sin = {
891                 .sin_family             = AF_INET,
892                 .sin_addr.s_addr        = htonl(INADDR_ANY),
893         };
894         struct sockaddr_in6 sin6 = {
895                 .sin6_family            = AF_INET6,
896                 .sin6_addr              = IN6ADDR_ANY_INIT,
897         };
898         int sock;
899
900         sock = socket(sap->sa_family, SOCK_DGRAM, IPPROTO_UDP);
901         if (sock < 0)
902                 return 0;
903
904         switch (sap->sa_family) {
905         case AF_INET:
906                 if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
907                         close(sock);
908                         return 0;
909                 }
910                 break;
911         case AF_INET6:
912                 if (bind(sock, (struct sockaddr *)&sin6, sizeof(sin6)) < 0) {
913                         close(sock);
914                         return 0;
915                 }
916                 break;
917         default:
918                 errno = EAFNOSUPPORT;
919                 return 0;
920         }
921
922         if (connect(sock, sap, salen) < 0) {
923                 close(sock);
924                 return 0;
925         }
926
927         return !getsockname(sock, buf, buflen);
928 }
929
930 /*
931  * Try to generate an address that prevents the server from calling us.
932  *
933  * Returns 1 and fills in @buf if successful; otherwise, zero.
934  */
935 static int nfs_ca_gai(const struct sockaddr *sap, const socklen_t salen,
936                       struct sockaddr *buf, socklen_t *buflen)
937 {
938         struct addrinfo *gai_results;
939         struct addrinfo gai_hint = {
940                 .ai_family      = sap->sa_family,
941                 .ai_flags       = AI_PASSIVE,   /* ANYADDR */
942         };
943
944         if (getaddrinfo(NULL, "", &gai_hint, &gai_results))
945                 return 0;
946
947         *buflen = gai_results->ai_addrlen;
948         memcpy(buf, gai_results->ai_addr, *buflen);
949
950         freeaddrinfo(gai_results);
951
952         return 1;
953 }
954
955 /**
956  * nfs_callback_address - acquire our local network address
957  * @sap: pointer to address of remote
958  * @sap_len: length of address
959  * @buf: pointer to buffer to be filled in with local network address
960  * @buflen: IN: length of buffer to fill in; OUT: length of filled-in address
961  *
962  * Discover a network address that an NFSv4 server can use to call us back.
963  * On multi-homed clients, this address depends on which NIC we use to
964  * route requests to the server.
965  *
966  * Returns 1 and fills in @buf if an unambiguous local address is
967  * available; returns 1 and fills in an appropriate ANYADDR address
968  * if a local address isn't available; otherwise, returns zero.
969  */
970 int nfs_callback_address(const struct sockaddr *sap, const socklen_t salen,
971                          struct sockaddr *buf, socklen_t *buflen)
972 {
973         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
974
975         if (nfs_ca_sockname(sap, salen, buf, buflen) == 0)
976                 if (nfs_ca_gai(sap, salen, buf, buflen) == 0)
977                         goto out_failed;
978
979         /*
980          * The server can't use an interface ID that was generated
981          * here on the client, so always clear sin6_scope_id.
982          */
983         if (sin6->sin6_family == AF_INET6)
984                 sin6->sin6_scope_id = 0;
985
986         return 1;
987
988 out_failed:
989         *buflen = 0;
990         if (verbose)
991                 nfs_error(_("%s: failed to construct callback address"));
992         return 0;
993
994 }