]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/network.c
d1f91dc0b32c9c501d9253cbe6a576363f7e00f0
[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 <sys/wait.h>
40 #include <sys/stat.h>
41 #include <netinet/in.h>
42 #include <rpc/rpc.h>
43 #include <rpc/pmap_prot.h>
44 #include <rpc/pmap_clnt.h>
45
46 #include "sockaddr.h"
47 #include "xcommon.h"
48 #include "mount.h"
49 #include "nls.h"
50 #include "nfs_mount.h"
51 #include "mount_constants.h"
52 #include "nfsrpc.h"
53 #include "parse_opt.h"
54 #include "network.h"
55 #include "conffile.h"
56 #include "nfslib.h"
57
58 #define PMAP_TIMEOUT    (10)
59 #define CONNECT_TIMEOUT (20)
60 #define MOUNT_TIMEOUT   (30)
61
62 #define SAFE_SOCKADDR(x)        (struct sockaddr *)(char *)(x)
63
64 extern int nfs_mount_data_version;
65 extern char *progname;
66 extern int verbose;
67
68 static const char *nfs_ns_pgmtbl[] = {
69         "status",
70         NULL,
71 };
72
73 static const char *nfs_mnt_pgmtbl[] = {
74         "mount",
75         "mountd",
76         NULL,
77 };
78
79 static const char *nfs_nfs_pgmtbl[] = {
80         "nfs",
81         "nfsprog",
82         NULL,
83 };
84
85 static const char *nfs_transport_opttbl[] = {
86         "udp",
87         "tcp",
88         "rdma",
89         "proto",
90         NULL,
91 };
92
93 static const char *nfs_version_opttbl[] = {
94         "v2",
95         "v3",
96         "v4",
97         "vers",
98         "nfsvers",
99         NULL,
100 };
101
102 static const unsigned long nfs_to_mnt[] = {
103         0,
104         0,
105         1,
106         3,
107 };
108
109 static const unsigned long mnt_to_nfs[] = {
110         0,
111         2,
112         2,
113         3,
114 };
115
116 /*
117  * Map an NFS version into the corresponding Mountd version
118  */
119 unsigned long nfsvers_to_mnt(const unsigned long vers)
120 {
121         if (vers <= 3)
122                 return nfs_to_mnt[vers];
123         return 0;
124 }
125
126 /*
127  * Map a Mountd version into the corresponding NFS version
128  */
129 static unsigned long mntvers_to_nfs(const unsigned long vers)
130 {
131         if (vers <= 3)
132                 return mnt_to_nfs[vers];
133         return 0;
134 }
135
136 static const unsigned int probe_udp_only[] = {
137         IPPROTO_UDP,
138         0,
139 };
140
141 static const unsigned int probe_udp_first[] = {
142         IPPROTO_UDP,
143         IPPROTO_TCP,
144         0,
145 };
146
147 static const unsigned int probe_tcp_first[] = {
148         IPPROTO_TCP,
149         IPPROTO_UDP,
150         0,
151 };
152
153 static const unsigned long probe_nfs2_only[] = {
154         2,
155         0,
156 };
157
158 static const unsigned long probe_nfs3_first[] = {
159         3,
160         2,
161         0,
162 };
163
164 static const unsigned long probe_mnt1_first[] = {
165         1,
166         2,
167         0,
168 };
169
170 static const unsigned long probe_mnt3_first[] = {
171         3,
172         1,
173         2,
174         0,
175 };
176
177 static const unsigned int *nfs_default_proto(void);
178 #ifdef MOUNT_CONFIG
179 static const unsigned int *nfs_default_proto()
180 {
181         extern unsigned long config_default_proto;
182         /*
183          * If the default proto has been set and 
184          * its not TCP, start with UDP
185          */
186         if (config_default_proto && config_default_proto != IPPROTO_TCP)
187                 return probe_udp_first;
188
189         return probe_tcp_first; 
190 }
191 #else
192 static const unsigned int *nfs_default_proto() 
193 {
194         return probe_tcp_first; 
195 }
196 #endif /* MOUNT_CONFIG */
197
198 /**
199  * nfs_lookup - resolve hostname to an IPv4 or IPv6 socket address
200  * @hostname: pointer to C string containing DNS hostname to resolve
201  * @family: address family hint
202  * @sap: pointer to buffer to fill with socket address
203  * @len: IN: size of buffer to fill; OUT: size of socket address
204  *
205  * Returns 1 and places a socket address at @sap if successful;
206  * otherwise zero.
207  */
208 int nfs_lookup(const char *hostname, const sa_family_t family,
209                 struct sockaddr *sap, socklen_t *salen)
210 {
211         struct addrinfo *gai_results;
212         struct addrinfo gai_hint = {
213                 .ai_family      = family,
214         };
215         socklen_t len = *salen;
216         int error, ret = 0;
217
218         *salen = 0;
219
220         error = getaddrinfo(hostname, NULL, &gai_hint, &gai_results);
221         switch (error) {
222         case 0:
223                 break;
224         case EAI_SYSTEM:
225                 nfs_error(_("%s: DNS resolution failed for %s: %s"),
226                         progname, hostname, strerror(errno));
227                 return ret;
228         default:
229                 nfs_error(_("%s: DNS resolution failed for %s: %s"),
230                         progname, hostname, gai_strerror(error));
231                 return ret;
232         }
233
234         switch (gai_results->ai_addr->sa_family) {
235         case AF_INET:
236         case AF_INET6:
237                 if (len >= gai_results->ai_addrlen) {
238                         *salen = gai_results->ai_addrlen;
239                         memcpy(sap, gai_results->ai_addr, *salen);
240                         ret = 1;
241                 }
242                 break;
243         default:
244                 /* things are really broken if we get here, so warn */
245                 nfs_error(_("%s: unrecognized DNS resolution results for %s"),
246                                 progname, hostname);
247                 break;
248         }
249
250         freeaddrinfo(gai_results);
251         return ret;
252 }
253
254 /**
255  * nfs_gethostbyname - resolve a hostname to an IPv4 address
256  * @hostname: pointer to a C string containing a DNS hostname
257  * @sin: returns an IPv4 address 
258  *
259  * Returns 1 if successful, otherwise zero.
260  */
261 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *sin)
262 {
263         socklen_t len = sizeof(*sin);
264
265         return nfs_lookup(hostname, AF_INET, (struct sockaddr *)sin, &len);
266 }
267
268 /**
269  * nfs_string_to_sockaddr - convert string address to sockaddr
270  * @address:    pointer to presentation format address to convert
271  * @sap:        pointer to socket address buffer to fill in
272  * @salen:      IN: length of address buffer
273  *              OUT: length of converted socket address
274  *
275  * Convert a presentation format address string to a socket address.
276  * Similar to nfs_lookup(), but the DNS query is squelched, and it
277  * won't make any noise if the getaddrinfo() call fails.
278  *
279  * Returns 1 and fills in @sap and @salen if successful; otherwise zero.
280  *
281  * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
282  * on presenting IPv6 addresses as text strings.
283  */
284 int nfs_string_to_sockaddr(const char *address, struct sockaddr *sap,
285                            socklen_t *salen)
286 {
287         struct addrinfo *gai_results;
288         struct addrinfo gai_hint = {
289                 .ai_flags       = AI_NUMERICHOST,
290         };
291         socklen_t len = *salen;
292         int ret = 0;
293
294         *salen = 0;
295
296         if (getaddrinfo(address, NULL, &gai_hint, &gai_results) == 0) {
297                 switch (gai_results->ai_addr->sa_family) {
298                 case AF_INET:
299                 case AF_INET6:
300                         if (len >= gai_results->ai_addrlen) {
301                                 *salen = gai_results->ai_addrlen;
302                                 memcpy(sap, gai_results->ai_addr, *salen);
303                                 ret = 1;
304                         }
305                         break;
306                 }
307                 freeaddrinfo(gai_results);
308         }
309
310         return ret;
311 }
312
313 /**
314  * nfs_present_sockaddr - convert sockaddr to string
315  * @sap: pointer to socket address to convert
316  * @salen: length of socket address
317  * @buf: pointer to buffer to fill in
318  * @buflen: length of buffer
319  *
320  * Convert the passed-in sockaddr-style address to presentation format.
321  * The presentation format address is placed in @buf and is
322  * '\0'-terminated.
323  *
324  * Returns 1 if successful; otherwise zero.
325  *
326  * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
327  * on presenting IPv6 addresses as text strings.
328  */
329 int nfs_present_sockaddr(const struct sockaddr *sap, const socklen_t salen,
330                          char *buf, const size_t buflen)
331 {
332 #ifdef HAVE_GETNAMEINFO
333         int result;
334
335         result = getnameinfo(sap, salen, buf, buflen,
336                                         NULL, 0, NI_NUMERICHOST);
337         if (!result)
338                 return 1;
339
340         nfs_error(_("%s: invalid server address: %s"), progname,
341                         gai_strerror(result));
342         return 0;
343 #else   /* HAVE_GETNAMEINFO */
344         char *addr;
345
346         if (sap->sa_family == AF_INET) {
347                 addr = inet_ntoa(((struct sockaddr_in *)sap)->sin_addr);
348                 if (addr && strlen(addr) < buflen) {
349                         strcpy(buf, addr);
350                         return 1;
351                 }
352         }
353
354         nfs_error(_("%s: invalid server address"), progname);
355         return 0;
356 #endif  /* HAVE_GETNAMEINFO */
357 }
358
359 /*
360  * Attempt to connect a socket, but time out after "timeout" seconds.
361  *
362  * On error return, caller closes the socket.
363  */
364 static int connect_to(int fd, struct sockaddr *addr,
365                         socklen_t addrlen, int timeout)
366 {
367         int ret, saved;
368         fd_set rset, wset;
369         struct timeval tv = {
370                 .tv_sec = timeout,
371         };
372
373         saved = fcntl(fd, F_GETFL, 0);
374         fcntl(fd, F_SETFL, saved | O_NONBLOCK);
375
376         ret = connect(fd, addr, addrlen);
377         if (ret < 0 && errno != EINPROGRESS)
378                 return -1;
379         if (ret == 0)
380                 goto out;
381
382         FD_ZERO(&rset);
383         FD_SET(fd, &rset);
384         wset = rset;
385         ret = select(fd + 1, &rset, &wset, NULL, &tv);
386         if (ret == 0) {
387                 errno = ETIMEDOUT;
388                 return -1;
389         }
390         if (FD_ISSET(fd, &rset) || FD_ISSET(fd, &wset)) {
391                 int error;
392                 socklen_t len = sizeof(error);
393                 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
394                         return -1;
395                 if (error) {
396                         errno = error;
397                         return -1;
398                 }
399         } else
400                 return -1;
401
402 out:
403         fcntl(fd, F_SETFL, saved);
404         return 0;
405 }
406
407 /*
408  * Create a socket that is locally bound to a reserved or non-reserved port.
409  *
410  * The caller should check rpc_createerr to determine the cause of any error.
411  */
412 static int get_socket(struct sockaddr_in *saddr, unsigned int p_prot,
413                         unsigned int timeout, int resvp, int conn)
414 {
415         int so, cc, type;
416         struct sockaddr_in laddr;
417         socklen_t namelen = sizeof(laddr);
418
419         type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
420         if ((so = socket (AF_INET, type, p_prot)) < 0)
421                 goto err_socket;
422
423         laddr.sin_family = AF_INET;
424         laddr.sin_port = 0;
425         laddr.sin_addr.s_addr = htonl(INADDR_ANY);
426         if (resvp) {
427                 if (bindresvport(so, &laddr) < 0)
428                         goto err_bindresvport;
429         } else {
430                 cc = bind(so, SAFE_SOCKADDR(&laddr), namelen);
431                 if (cc < 0)
432                         goto err_bind;
433         }
434         if (type == SOCK_STREAM || (conn && type == SOCK_DGRAM)) {
435                 cc = connect_to(so, SAFE_SOCKADDR(saddr), namelen,
436                                 timeout);
437                 if (cc < 0)
438                         goto err_connect;
439         }
440         return so;
441
442 err_socket:
443         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
444         rpc_createerr.cf_error.re_errno = errno;
445         if (verbose) {
446                 nfs_error(_("%s: Unable to create %s socket: errno %d (%s)\n"),
447                         progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
448                         errno, strerror(errno));
449         }
450         return RPC_ANYSOCK;
451
452 err_bindresvport:
453         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
454         rpc_createerr.cf_error.re_errno = errno;
455         if (verbose) {
456                 nfs_error(_("%s: Unable to bindresvport %s socket: errno %d"
457                                 " (%s)\n"),
458                         progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
459                         errno, strerror(errno));
460         }
461         close(so);
462         return RPC_ANYSOCK;
463
464 err_bind:
465         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
466         rpc_createerr.cf_error.re_errno = errno;
467         if (verbose) {
468                 nfs_error(_("%s: Unable to bind to %s socket: errno %d (%s)\n"),
469                         progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
470                         errno, strerror(errno));
471         }
472         close(so);
473         return RPC_ANYSOCK;
474
475 err_connect:
476         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
477         rpc_createerr.cf_error.re_errno = errno;
478         if (verbose) {
479                 nfs_error(_("%s: Unable to connect to %s:%d, errno %d (%s)\n"),
480                         progname, inet_ntoa(saddr->sin_addr),
481                         ntohs(saddr->sin_port), errno, strerror(errno));
482         }
483         close(so);
484         return RPC_ANYSOCK;
485 }
486
487 static void nfs_pp_debug(const struct sockaddr *sap, const socklen_t salen,
488                          const rpcprog_t program, const rpcvers_t version,
489                          const unsigned short protocol,
490                          const unsigned short port)
491 {
492         char buf[NI_MAXHOST];
493
494         if (!verbose)
495                 return;
496
497         if (nfs_present_sockaddr(sap, salen, buf, sizeof(buf)) == 0) {
498                 buf[0] = '\0';
499                 strcat(buf, "unknown host");
500         }
501
502         fprintf(stderr, _("%s: trying %s prog %lu vers %lu prot %s port %d\n"),
503                         progname, buf, (unsigned long)program,
504                         (unsigned long)version,
505                         (protocol == IPPROTO_UDP ? _("UDP") : _("TCP")),
506                         port);
507 }
508
509 static void nfs_pp_debug2(const char *str)
510 {
511         if (!verbose)
512                 return;
513
514         if (rpc_createerr.cf_error.re_status == RPC_CANTRECV ||
515             rpc_createerr.cf_error.re_status == RPC_CANTSEND)
516                 nfs_error(_("%s: portmap query %s%s - %s"),
517                                 progname, str, clnt_spcreateerror(""),
518                                 strerror(rpc_createerr.cf_error.re_errno));
519         else
520                 nfs_error(_("%s: portmap query %s%s"),
521                                 progname, str, clnt_spcreateerror(""));
522 }
523
524 /*
525  * Use the portmapper to discover whether or not the service we want is
526  * available. The lists 'versions' and 'protos' define ordered sequences
527  * of service versions and udp/tcp protocols to probe for.
528  *
529  * Returns 1 if the requested service port is unambiguous and pingable;
530  * @pmap is filled in with the version, port, and transport protocol used
531  * during the successful ping.  Note that if a port is already specified
532  * in @pmap and it matches the rpcbind query result, nfs_probe_port() does
533  * not perform an RPC ping.
534  * 
535  * If an error occurs or the requested service isn't available, zero is
536  * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
537  */
538 static int nfs_probe_port(const struct sockaddr *sap, const socklen_t salen,
539                           struct pmap *pmap, const unsigned long *versions,
540                           const unsigned int *protos)
541 {
542         union nfs_sockaddr address;
543         struct sockaddr *saddr = &address.sa;
544         const unsigned long prog = pmap->pm_prog, *p_vers;
545         const unsigned int prot = (u_int)pmap->pm_prot, *p_prot;
546         const u_short port = (u_short) pmap->pm_port;
547         unsigned long vers = pmap->pm_vers;
548         unsigned short p_port;
549
550         memcpy(saddr, sap, salen);
551         p_prot = prot ? &prot : protos;
552         p_vers = vers ? &vers : versions;
553
554         for (;;) {
555                 if (verbose)
556                         printf(_("%s: prog %lu, trying vers=%lu, prot=%u\n"),
557                                 progname, prog, *p_vers, *p_prot);
558                 p_port = nfs_getport(saddr, salen, prog, *p_vers, *p_prot);
559                 if (p_port) {
560                         if (!port || port == p_port) {
561                                 nfs_set_port(saddr, p_port);
562                                 nfs_pp_debug(saddr, salen, prog, *p_vers,
563                                                 *p_prot, p_port);
564                                 if (nfs_rpc_ping(saddr, salen, prog,
565                                                         *p_vers, *p_prot, NULL))
566                                         goto out_ok;
567                         } else
568                                 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
569                 }
570                 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
571                     rpc_createerr.cf_stat != RPC_TIMEDOUT &&
572                     rpc_createerr.cf_stat != RPC_CANTRECV &&
573                     rpc_createerr.cf_stat != RPC_PROGVERSMISMATCH)
574                         break;
575
576                 if (!prot) {
577                         if (*++p_prot) {
578                                 nfs_pp_debug2("retrying");
579                                 continue;
580                         }
581                         p_prot = protos;
582                 }
583                 if (rpc_createerr.cf_stat == RPC_TIMEDOUT ||
584                     rpc_createerr.cf_stat == RPC_CANTRECV)
585                         break;
586
587                 if (vers || !*++p_vers)
588                         break;
589         }
590
591         nfs_pp_debug2("failed");
592         return 0;
593
594 out_ok:
595         if (!vers)
596                 pmap->pm_vers = *p_vers;
597         if (!prot)
598                 pmap->pm_prot = *p_prot;
599         if (!port)
600                 pmap->pm_port = p_port;
601         nfs_clear_rpc_createerr();
602         return 1;
603 }
604 /*
605  * Probe a server's NFS service to determine which versions and
606  * transport protocols are supported.
607  *
608  * Returns 1 if the requested service port is unambiguous and pingable;
609  * @pmap is filled in with the version, port, and transport protocol used
610  * during the successful ping.  If all three are already specified, simply
611  * return success without an rpcbind query or RPC ping (we may be trying
612  * to mount an NFS service that is not advertised via rpcbind).
613  *
614  * If an error occurs or the requested service isn't available, zero is
615  * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
616  */
617 static int nfs_probe_nfsport(const struct sockaddr *sap, const socklen_t salen,
618                                 struct pmap *pmap)
619 {
620         if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
621                 return 1;
622
623         if (nfs_mount_data_version >= 4) {
624                 const unsigned int *probe_proto;
625
626                 probe_proto = nfs_default_proto();
627
628                 return nfs_probe_port(sap, salen, pmap,
629                                         probe_nfs3_first, probe_proto);
630         } else
631                 return nfs_probe_port(sap, salen, pmap,
632                                         probe_nfs2_only, probe_udp_only);
633 }
634
635 /*
636  * Probe a server's mountd service to determine which versions and
637  * transport protocols are supported.
638  *
639  * Returns 1 if the requested service port is unambiguous and pingable;
640  * @pmap is filled in with the version, port, and transport protocol used
641  * during the successful ping.  If all three are already specified, simply
642  * return success without an rpcbind query or RPC ping (we may be trying
643  * to mount an NFS service that is not advertised via rpcbind).
644  * 
645  * If an error occurs or the requested service isn't available, zero is
646  * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
647  */
648 static int nfs_probe_mntport(const struct sockaddr *sap, const socklen_t salen,
649                                 struct pmap *pmap)
650 {
651         if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
652                 return 1;
653
654         if (nfs_mount_data_version >= 4)
655                 return nfs_probe_port(sap, salen, pmap,
656                                         probe_mnt3_first, probe_udp_first);
657         else
658                 return nfs_probe_port(sap, salen, pmap,
659                                         probe_mnt1_first, probe_udp_only);
660 }
661
662 /*
663  * Probe a server's mountd service to determine which versions and
664  * transport protocols are supported.  Invoked when the protocol
665  * version is already known for both the NFS and mountd service.
666  *
667  * Returns 1 and fills in both @pmap structs if the requested service
668  * ports are unambiguous and pingable.  Otherwise zero is returned;
669  * rpccreateerr.cf_stat is set to reflect the nature of the error.
670  */
671 static int nfs_probe_version_fixed(const struct sockaddr *mnt_saddr,
672                         const socklen_t mnt_salen,
673                         struct pmap *mnt_pmap,
674                         const struct sockaddr *nfs_saddr,
675                         const socklen_t nfs_salen,
676                         struct pmap *nfs_pmap)
677 {
678         if (!nfs_probe_nfsport(nfs_saddr, nfs_salen, nfs_pmap))
679                 return 0;
680         return nfs_probe_mntport(mnt_saddr, mnt_salen, mnt_pmap);
681 }
682
683 /**
684  * nfs_probe_bothports - discover the RPC endpoints of mountd and NFS server
685  * @mnt_saddr:  pointer to socket address of mountd server
686  * @mnt_salen:  length of mountd server's address
687  * @mnt_pmap:   IN: partially filled-in mountd RPC service tuple;
688  *              OUT: fully filled-in mountd RPC service tuple
689  * @nfs_saddr:  pointer to socket address of NFS server
690  * @nfs_salen:  length of NFS server's address
691  * @nfs_pmap:   IN: partially filled-in NFS RPC service tuple;
692  *              OUT: fully filled-in NFS RPC service tuple
693  *
694  * Returns 1 and fills in both @pmap structs if the requested service
695  * ports are unambiguous and pingable.  Otherwise zero is returned;
696  * rpccreateerr.cf_stat is set to reflect the nature of the error.
697  */
698 int nfs_probe_bothports(const struct sockaddr *mnt_saddr,
699                         const socklen_t mnt_salen,
700                         struct pmap *mnt_pmap,
701                         const struct sockaddr *nfs_saddr,
702                         const socklen_t nfs_salen,
703                         struct pmap *nfs_pmap)
704 {
705         struct pmap save_nfs, save_mnt;
706         const unsigned long *probe_vers;
707
708         if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
709                 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
710         else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
711                 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
712
713         if (nfs_pmap->pm_vers)
714                 return nfs_probe_version_fixed(mnt_saddr, mnt_salen, mnt_pmap,
715                                                nfs_saddr, nfs_salen, nfs_pmap);
716
717         memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
718         memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
719         probe_vers = (nfs_mount_data_version >= 4) ?
720                         probe_mnt3_first : probe_mnt1_first;
721
722         for (; *probe_vers; probe_vers++) {
723                 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
724                 if (nfs_probe_nfsport(nfs_saddr, nfs_salen, nfs_pmap) != 0) {
725                         mnt_pmap->pm_vers = *probe_vers;
726                         if (nfs_probe_mntport(mnt_saddr, mnt_salen, mnt_pmap) != 0)
727                                 return 1;
728                         memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
729                 }
730                 switch (rpc_createerr.cf_stat) {
731                 case RPC_PROGVERSMISMATCH:
732                 case RPC_PROGNOTREGISTERED:
733                         break;
734                 default:
735                         return 0;
736                 }
737                 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
738         }
739
740         return 0;
741 }
742
743 /**
744  * probe_bothports - discover the RPC endpoints of mountd and NFS server
745  * @mnt_server: pointer to address and pmap argument for mountd results
746  * @nfs_server: pointer to address and pmap argument for NFS server
747  *
748  * This is the legacy API that takes "clnt_addr_t" for both servers,
749  * but supports only AF_INET addresses.
750  *
751  * Returns 1 and fills in the pmap field in both clnt_addr_t structs
752  * if the requested service ports are unambiguous and pingable.
753  * Otherwise zero is returned; rpccreateerr.cf_stat is set to reflect
754  * the nature of the error.
755  */
756 int probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
757 {
758         struct sockaddr *mnt_addr = SAFE_SOCKADDR(&mnt_server->saddr);
759         struct sockaddr *nfs_addr = SAFE_SOCKADDR(&nfs_server->saddr);
760
761         return nfs_probe_bothports(mnt_addr, sizeof(mnt_server->saddr),
762                                         &mnt_server->pmap,
763                                         nfs_addr, sizeof(nfs_server->saddr),
764                                         &nfs_server->pmap);
765 }
766
767 static int nfs_probe_statd(void)
768 {
769         struct sockaddr_in addr = {
770                 .sin_family             = AF_INET,
771                 .sin_addr.s_addr        = htonl(INADDR_LOOPBACK),
772         };
773         rpcprog_t program = nfs_getrpcbyname(NSMPROG, nfs_ns_pgmtbl);
774
775         return nfs_getport_ping(SAFE_SOCKADDR(&addr), sizeof(addr),
776                                 program, (rpcvers_t)1, IPPROTO_UDP);
777 }
778
779 /**
780  * start_statd - attempt to start rpc.statd
781  *
782  * Returns 1 if statd is running; otherwise zero.
783  */
784 int start_statd(void)
785 {
786 #ifdef START_STATD
787         struct stat stb;
788 #endif
789
790         if (nfs_probe_statd())
791                 return 1;
792
793 #ifdef START_STATD
794         if (stat(START_STATD, &stb) == 0) {
795                 if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {
796                         pid_t pid = fork();
797                         switch (pid) {
798                         case 0: /* child */
799                                 execl(START_STATD, START_STATD, NULL);
800                                 exit(1);
801                         case -1: /* error */
802                                 nfs_error(_("%s: fork failed: %s"),
803                                                 progname, strerror(errno));
804                                 break;
805                         default: /* parent */
806                                 waitpid(pid, NULL,0);
807                                 break;
808                         }
809                         if (nfs_probe_statd())
810                                 return 1;
811                 }
812         }
813 #endif
814
815         return 0;
816 }
817
818 /**
819  * nfs_advise_umount - ask the server to remove a share from it's rmtab
820  * @sap: pointer to IP address of server to call
821  * @salen: length of server address
822  * @pmap: partially filled-in mountd RPC service tuple
823  * @argp: directory path of share to "unmount"
824  *
825  * Returns one if the unmount call succeeded; zero if the unmount
826  * failed for any reason;  rpccreateerr.cf_stat is set to reflect
827  * the nature of the error.
828  *
829  * We use a fast timeout since this call is advisory only.
830  */
831 int nfs_advise_umount(const struct sockaddr *sap, const socklen_t salen,
832                       const struct pmap *pmap, const dirpath *argp)
833 {
834         union nfs_sockaddr address;
835         struct sockaddr *saddr = &address.sa;
836         struct pmap mnt_pmap = *pmap;
837         struct timeval timeout = {
838                 .tv_sec         = MOUNT_TIMEOUT >> 3,
839         };
840         CLIENT *client;
841         enum clnt_stat res = 0;
842
843         memcpy(saddr, sap, salen);
844         if (nfs_probe_mntport(saddr, salen, &mnt_pmap) == 0) {
845                 if (verbose)
846                         nfs_error(_("%s: Failed to discover mountd port%s"),
847                                 progname, clnt_spcreateerror(""));
848                 return 0;
849         }
850         nfs_set_port(saddr, mnt_pmap.pm_port);
851
852         client = nfs_get_priv_rpcclient(saddr, salen, mnt_pmap.pm_prot,
853                                         mnt_pmap.pm_prog, mnt_pmap.pm_vers,
854                                         &timeout);
855         if (client == NULL) {
856                 if (verbose)
857                         nfs_error(_("%s: Failed to create RPC client%s"),
858                                 progname, clnt_spcreateerror(""));
859                 return 0;
860         }
861
862         client->cl_auth = nfs_authsys_create();
863         if (client->cl_auth == NULL) {
864                 if (verbose)
865                         nfs_error(_("%s: Failed to create RPC auth handle"),
866                                 progname);
867                 CLNT_DESTROY(client);
868                 return 0;
869         }
870
871         res = CLNT_CALL(client, MOUNTPROC_UMNT,
872                         (xdrproc_t)xdr_dirpath, (caddr_t)argp,
873                         (xdrproc_t)xdr_void, NULL,
874                         timeout);
875         if (res != RPC_SUCCESS) {
876                 rpc_createerr.cf_stat = res;
877                 CLNT_GETERR(client, &rpc_createerr.cf_error);
878                 if (verbose)
879                         nfs_error(_("%s: UMNT call failed: %s"),
880                                 progname, clnt_sperrno(res));
881
882         }
883         auth_destroy(client->cl_auth);
884         CLNT_DESTROY(client);
885
886         if (res != RPC_SUCCESS)
887                 return 0;
888         return 1;
889 }
890
891 /**
892  * nfs_call_umount - ask the server to remove a share from it's rmtab
893  * @mnt_server: address of RPC MNT program server
894  * @argp: directory path of share to "unmount"
895  *
896  * Returns one if the unmount call succeeded; zero if the unmount
897  * failed for any reason.
898  *
899  * Note that a side effect of calling this function is that rpccreateerr
900  * is set.
901  */
902 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
903 {
904         struct sockaddr *sap = SAFE_SOCKADDR(&mnt_server->saddr);
905         socklen_t salen = sizeof(mnt_server->saddr);
906         struct pmap *pmap = &mnt_server->pmap;
907         CLIENT *clnt;
908         enum clnt_stat res = 0;
909         int msock;
910
911         if (!nfs_probe_mntport(sap, salen, pmap))
912                 return 0;
913         clnt = mnt_openclnt(mnt_server, &msock);
914         if (!clnt)
915                 return 0;
916         res = clnt_call(clnt, MOUNTPROC_UMNT,
917                         (xdrproc_t)xdr_dirpath, (caddr_t)argp,
918                         (xdrproc_t)xdr_void, NULL,
919                         TIMEOUT);
920         mnt_closeclnt(clnt, msock);
921
922         if (res == RPC_SUCCESS)
923                 return 1;
924         return 0;
925 }
926
927 /**
928  * mnt_openclnt - get a handle for a remote mountd service
929  * @mnt_server: address and pmap arguments of mountd service
930  * @msock: returns a file descriptor of the underlying transport socket
931  *
932  * Returns an active handle for the remote's mountd service
933  */
934 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
935 {
936         struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
937         struct pmap *mnt_pmap = &mnt_server->pmap;
938         CLIENT *clnt = NULL;
939
940         mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
941         *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, MOUNT_TIMEOUT,
942                                 TRUE, FALSE);
943         if (*msock == RPC_ANYSOCK) {
944                 if (rpc_createerr.cf_error.re_errno == EADDRINUSE)
945                         /*
946                          * Probably in-use by a TIME_WAIT connection,
947                          * It is worth waiting a while and trying again.
948                          */
949                         rpc_createerr.cf_stat = RPC_TIMEDOUT;
950                 return NULL;
951         }
952
953         switch (mnt_pmap->pm_prot) {
954         case IPPROTO_UDP:
955                 clnt = clntudp_bufcreate(mnt_saddr,
956                                          mnt_pmap->pm_prog, mnt_pmap->pm_vers,
957                                          RETRY_TIMEOUT, msock,
958                                          MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
959                 break;
960         case IPPROTO_TCP:
961                 clnt = clnttcp_create(mnt_saddr,
962                                       mnt_pmap->pm_prog, mnt_pmap->pm_vers,
963                                       msock,
964                                       MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
965                 break;
966         }
967         if (clnt) {
968                 /* try to mount hostname:dirname */
969                 clnt->cl_auth = nfs_authsys_create();
970                 if (clnt->cl_auth)
971                         return clnt;
972                 CLNT_DESTROY(clnt);
973         }
974         return NULL;
975 }
976
977 /**
978  * mnt_closeclnt - terminate a handle for a remote mountd service
979  * @clnt: pointer to an active handle for a remote mountd service
980  * @msock: file descriptor of the underlying transport socket
981  *
982  */
983 void mnt_closeclnt(CLIENT *clnt, int msock)
984 {
985         auth_destroy(clnt->cl_auth);
986         clnt_destroy(clnt);
987         close(msock);
988 }
989
990 /**
991  * clnt_ping - send an RPC ping to the remote RPC service endpoint
992  * @saddr: server's address
993  * @prog: target RPC program number
994  * @vers: target RPC version number
995  * @prot: target RPC protocol
996  * @caddr: filled in with our network address
997  *
998  * Sigh... GETPORT queries don't actually check the version number.
999  * In order to make sure that the server actually supports the service
1000  * we're requesting, we open an RPC client, and fire off a NULL
1001  * RPC call.
1002  *
1003  * caddr is the network address that the server will use to call us back.
1004  * On multi-homed clients, this address depends on which NIC we use to
1005  * route requests to the server.
1006  *
1007  * Returns one if successful, otherwise zero.
1008  */
1009 int clnt_ping(struct sockaddr_in *saddr, const unsigned long prog,
1010                 const unsigned long vers, const unsigned int prot,
1011                 struct sockaddr_in *caddr)
1012 {
1013         CLIENT *clnt = NULL;
1014         int sock, status;
1015         static char clnt_res;
1016         struct sockaddr dissolve;
1017
1018         rpc_createerr.cf_stat = status = 0;
1019         sock = get_socket(saddr, prot, CONNECT_TIMEOUT, FALSE, TRUE);
1020         if (sock == RPC_ANYSOCK) {
1021                 if (rpc_createerr.cf_error.re_errno == ETIMEDOUT) {
1022                         /*
1023                          * TCP timeout. Bubble up the error to see 
1024                          * how it should be handled.
1025                          */
1026                         rpc_createerr.cf_stat = RPC_TIMEDOUT;
1027                 }
1028                 return 0;
1029         }
1030
1031         if (caddr) {
1032                 /* Get the address of our end of this connection */
1033                 socklen_t len = sizeof(*caddr);
1034                 if (getsockname(sock, caddr, &len) != 0)
1035                         caddr->sin_family = 0;
1036         }
1037
1038         switch(prot) {
1039         case IPPROTO_UDP:
1040                 /* The socket is connected (so we could getsockname successfully),
1041                  * but some servers on multi-homed hosts reply from
1042                  * the wrong address, so if we stay connected, we lose the reply.
1043                  */
1044                 dissolve.sa_family = AF_UNSPEC;
1045                 connect(sock, &dissolve, sizeof(dissolve));
1046
1047                 clnt = clntudp_bufcreate(saddr, prog, vers,
1048                                          RETRY_TIMEOUT, &sock,
1049                                          RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
1050                 break;
1051         case IPPROTO_TCP:
1052                 clnt = clnttcp_create(saddr, prog, vers, &sock,
1053                                       RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
1054                 break;
1055         }
1056         if (!clnt) {
1057                 close(sock);
1058                 return 0;
1059         }
1060         memset(&clnt_res, 0, sizeof(clnt_res));
1061         status = clnt_call(clnt, NULLPROC,
1062                          (xdrproc_t)xdr_void, (caddr_t)NULL,
1063                          (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
1064                          TIMEOUT);
1065         if (status) {
1066                 clnt_geterr(clnt, &rpc_createerr.cf_error);
1067                 rpc_createerr.cf_stat = status;
1068         }
1069         clnt_destroy(clnt);
1070         close(sock);
1071
1072         if (status == RPC_SUCCESS)
1073                 return 1;
1074         else
1075                 return 0;
1076 }
1077
1078 /*
1079  * Try a getsockname() on a connected datagram socket.
1080  *
1081  * Returns 1 and fills in @buf if successful; otherwise, zero.
1082  *
1083  * A connected datagram socket prevents leaving a socket in TIME_WAIT.
1084  * This conserves the ephemeral port number space, helping reduce failed
1085  * socket binds during mount storms.
1086  */
1087 static int nfs_ca_sockname(const struct sockaddr *sap, const socklen_t salen,
1088                            struct sockaddr *buf, socklen_t *buflen)
1089 {
1090         struct sockaddr_in sin = {
1091                 .sin_family             = AF_INET,
1092                 .sin_addr.s_addr        = htonl(INADDR_ANY),
1093         };
1094         struct sockaddr_in6 sin6 = {
1095                 .sin6_family            = AF_INET6,
1096                 .sin6_addr              = IN6ADDR_ANY_INIT,
1097         };
1098         int sock, result = 0;
1099
1100         sock = socket(sap->sa_family, SOCK_DGRAM, IPPROTO_UDP);
1101         if (sock < 0)
1102                 return 0;
1103
1104         switch (sap->sa_family) {
1105         case AF_INET:
1106                 if (bind(sock, SAFE_SOCKADDR(&sin), sizeof(sin)) < 0)
1107                         goto out;
1108                 break;
1109         case AF_INET6:
1110                 if (bind(sock, SAFE_SOCKADDR(&sin6), sizeof(sin6)) < 0)
1111                         goto out;
1112                 break;
1113         default:
1114                 errno = EAFNOSUPPORT;
1115                 goto out;
1116         }
1117
1118         if (connect(sock, sap, salen) < 0)
1119                 goto out;
1120
1121         result = !getsockname(sock, buf, buflen);
1122
1123 out:
1124         close(sock);
1125         return result;
1126 }
1127
1128 /*
1129  * Try to generate an address that prevents the server from calling us.
1130  *
1131  * Returns 1 and fills in @buf if successful; otherwise, zero.
1132  */
1133 static int nfs_ca_gai(const struct sockaddr *sap,
1134                       struct sockaddr *buf, socklen_t *buflen)
1135 {
1136         struct addrinfo *gai_results;
1137         struct addrinfo gai_hint = {
1138                 .ai_family      = sap->sa_family,
1139                 .ai_flags       = AI_PASSIVE,   /* ANYADDR */
1140         };
1141
1142         if (getaddrinfo(NULL, "", &gai_hint, &gai_results))
1143                 return 0;
1144
1145         *buflen = gai_results->ai_addrlen;
1146         memcpy(buf, gai_results->ai_addr, *buflen);
1147
1148         freeaddrinfo(gai_results);
1149
1150         return 1;
1151 }
1152
1153 /**
1154  * nfs_callback_address - acquire our local network address
1155  * @sap: pointer to address of remote
1156  * @sap_len: length of address
1157  * @buf: pointer to buffer to be filled in with local network address
1158  * @buflen: IN: length of buffer to fill in; OUT: length of filled-in address
1159  *
1160  * Discover a network address that an NFSv4 server can use to call us back.
1161  * On multi-homed clients, this address depends on which NIC we use to
1162  * route requests to the server.
1163  *
1164  * Returns 1 and fills in @buf if an unambiguous local address is
1165  * available; returns 1 and fills in an appropriate ANYADDR address
1166  * if a local address isn't available; otherwise, returns zero.
1167  */
1168 int nfs_callback_address(const struct sockaddr *sap, const socklen_t salen,
1169                          struct sockaddr *buf, socklen_t *buflen)
1170 {
1171         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1172
1173         if (nfs_ca_sockname(sap, salen, buf, buflen) == 0)
1174                 if (nfs_ca_gai(sap, buf, buflen) == 0)
1175                         goto out_failed;
1176
1177         /*
1178          * The server can't use an interface ID that was generated
1179          * here on the client, so always clear sin6_scope_id.
1180          */
1181         if (sin6->sin6_family == AF_INET6)
1182                 sin6->sin6_scope_id = 0;
1183
1184         return 1;
1185
1186 out_failed:
1187         *buflen = 0;
1188         if (verbose)
1189                 nfs_error(_("%s: failed to construct callback address"),
1190                                 progname);
1191         return 0;
1192 }
1193
1194 /*
1195  * "nfsprog" is supported only by the legacy mount command.  The
1196  * kernel mount client does not support this option.
1197  *
1198  * Returns TRUE if @program contains a valid value for this option,
1199  * or FALSE if the option was specified with an invalid value.
1200  */
1201 static int
1202 nfs_nfs_program(struct mount_options *options, unsigned long *program)
1203 {
1204         long tmp;
1205
1206         switch (po_get_numeric(options, "nfsprog", &tmp)) {
1207         case PO_NOT_FOUND:
1208                 break;
1209         case PO_FOUND:
1210                 if (tmp > 0) {
1211                         *program = tmp;
1212                         return 1;
1213                 }
1214         case PO_BAD_VALUE:
1215                 nfs_error(_("%s: invalid value for 'nfsprog=' option"),
1216                                 progname);
1217                 return 0;
1218         }
1219
1220         /*
1221          * NFS RPC program wasn't specified.  The RPC program
1222          * cannot be determined via an rpcbind query.
1223          */
1224         *program = nfs_getrpcbyname(NFSPROG, nfs_nfs_pgmtbl);
1225         return 1;
1226 }
1227
1228 /*
1229  * Returns TRUE if @version contains a valid value for this option,
1230  * or FALSE if the option was specified with an invalid value.
1231  */
1232 int
1233 nfs_nfs_version(struct mount_options *options, unsigned long *version)
1234 {
1235         long tmp;
1236
1237         switch (po_rightmost(options, nfs_version_opttbl)) {
1238         case 0: /* v2 */
1239                 *version = 2;
1240                 return 1;
1241         case 1: /* v3 */
1242                 *version = 3;
1243                 return 1;
1244         case 2: /* v4 */
1245                 *version = 4;
1246                 return 1;
1247         case 3: /* vers */
1248                 switch (po_get_numeric(options, "vers", &tmp)) {
1249                 case PO_FOUND:
1250                         if (tmp >= 2 && tmp <= 4) {
1251                                 *version = tmp;
1252                                 return 1;
1253                         }
1254                         return 0;
1255                 case PO_NOT_FOUND:
1256                         nfs_error(_("%s: parsing error on 'vers=' option\n"),
1257                                         progname);
1258                         return 0;
1259                 case PO_BAD_VALUE:
1260                         nfs_error(_("%s: invalid value for 'vers=' option"),
1261                                         progname);
1262                         return 0;
1263                 }
1264         case 4: /* nfsvers */
1265                 switch (po_get_numeric(options, "nfsvers", &tmp)) {
1266                 case PO_FOUND:
1267                         if (tmp >= 2 && tmp <= 4) {
1268                                 *version = tmp;
1269                                 return 1;
1270                         }
1271                         return 0;
1272                 case PO_NOT_FOUND:
1273                         nfs_error(_("%s: parsing error on 'nfsvers=' option\n"),
1274                                         progname);
1275                         return 0;
1276                 case PO_BAD_VALUE:
1277                         nfs_error(_("%s: invalid value for 'nfsvers=' option"),
1278                                         progname);
1279                         return 0;
1280                 }
1281         }
1282
1283         /*
1284          * NFS version wasn't specified.  The pmap version value
1285          * will be filled in later by an rpcbind query in this case.
1286          */
1287         *version = 0;
1288         return 1;
1289 }
1290
1291 /*
1292  * Returns TRUE if @protocol contains a valid value for this option,
1293  * or FALSE if the option was specified with an invalid value. On
1294  * error, errno is set.
1295  */
1296 int
1297 nfs_nfs_protocol(struct mount_options *options, unsigned long *protocol)
1298 {
1299         sa_family_t family;
1300         char *option;
1301
1302         switch (po_rightmost(options, nfs_transport_opttbl)) {
1303         case 0: /* udp */
1304                 *protocol = IPPROTO_UDP;
1305                 return 1;
1306         case 1: /* tcp */
1307                 *protocol = IPPROTO_TCP;
1308                 return 1;
1309         case 2: /* rdma */
1310                 *protocol = NFSPROTO_RDMA;
1311                 return 1;
1312         case 3: /* proto */
1313                 option = po_get(options, "proto");
1314                 if (option != NULL) {
1315                         if (!nfs_get_proto(option, &family, protocol)) {
1316                                 errno = EPROTONOSUPPORT;
1317                                 nfs_error(_("%s: Failed to find '%s' protocol"), 
1318                                         progname, option);
1319                                 return 0;
1320                         }
1321                         return 1;
1322                 }
1323         }
1324
1325         /*
1326          * NFS transport protocol wasn't specified.  The pmap
1327          * protocol value will be filled in later by an rpcbind
1328          * query in this case.
1329          */
1330         *protocol = 0;
1331         return 1;
1332 }
1333
1334 /*
1335  * Returns TRUE if @port contains a valid value for this option,
1336  * or FALSE if the option was specified with an invalid value.
1337  */
1338 static int
1339 nfs_nfs_port(struct mount_options *options, unsigned long *port)
1340 {
1341         long tmp;
1342
1343         switch (po_get_numeric(options, "port", &tmp)) {
1344         case PO_NOT_FOUND:
1345                 break;
1346         case PO_FOUND:
1347                 if (tmp >= 0 && tmp <= 65535) {
1348                         *port = tmp;
1349                         return 1;
1350                 }
1351         case PO_BAD_VALUE:
1352                 nfs_error(_("%s: invalid value for 'port=' option"),
1353                                 progname);
1354                 return 0;
1355         }
1356
1357         /*
1358          * NFS service port wasn't specified.  The pmap port value
1359          * will be filled in later by an rpcbind query in this case.
1360          */
1361         *port = 0;
1362         return 1;
1363 }
1364
1365 #ifdef IPV6_SUPPORTED
1366 sa_family_t     config_default_family = AF_UNSPEC;
1367
1368 static int
1369 nfs_verify_family(sa_family_t UNUSED(family))
1370 {
1371         return 1;
1372 }
1373 #else /* IPV6_SUPPORTED */
1374 sa_family_t     config_default_family = AF_INET;
1375
1376 static int
1377 nfs_verify_family(sa_family_t family)
1378 {
1379         if (family != AF_INET)
1380                 return 0;
1381
1382         return 1;
1383 }
1384 #endif /* IPV6_SUPPORTED */
1385
1386 /*
1387  * Returns TRUE and fills in @family if a valid NFS protocol option
1388  * is found, or FALSE if the option was specified with an invalid value
1389  * or if the protocol family isn't supported. On error, errno is set.
1390  */
1391 int nfs_nfs_proto_family(struct mount_options *options,
1392                                 sa_family_t *family)
1393 {
1394         unsigned long protocol;
1395         char *option;
1396         sa_family_t tmp_family = config_default_family;
1397
1398         switch (po_rightmost(options, nfs_transport_opttbl)) {
1399         case 0: /* udp */
1400         case 1: /* tcp */
1401         case 2: /* rdma */
1402                 /* for compatibility; these are always AF_INET */
1403                 *family = AF_INET;
1404                 return 1;
1405         case 3: /* proto */
1406                 option = po_get(options, "proto");
1407                 if (option != NULL &&
1408                     !nfs_get_proto(option, &tmp_family, &protocol)) {
1409
1410                         nfs_error(_("%s: Failed to find '%s' protocol"), 
1411                                 progname, option);
1412                         errno = EPROTONOSUPPORT;
1413                         return 0;
1414                 }
1415         }
1416
1417         if (!nfs_verify_family(tmp_family))
1418                 goto out_err;
1419         *family = tmp_family;
1420         return 1;
1421 out_err:
1422         errno = EAFNOSUPPORT;
1423         return 0;
1424 }
1425
1426 /*
1427  * "mountprog" is supported only by the legacy mount command.  The
1428  * kernel mount client does not support this option.
1429  *
1430  * Returns TRUE if @program contains a valid value for this option,
1431  * or FALSE if the option was specified with an invalid value.
1432  */
1433 static int
1434 nfs_mount_program(struct mount_options *options, unsigned long *program)
1435 {
1436         long tmp;
1437
1438         switch (po_get_numeric(options, "mountprog", &tmp)) {
1439         case PO_NOT_FOUND:
1440                 break;
1441         case PO_FOUND:
1442                 if (tmp > 0) {
1443                         *program = tmp;
1444                         return 1;
1445                 }
1446         case PO_BAD_VALUE:
1447                 nfs_error(_("%s: invalid value for 'mountprog=' option"),
1448                                 progname);
1449                 return 0;
1450         }
1451
1452         /*
1453          * MNT RPC program wasn't specified.  The RPC program
1454          * cannot be determined via an rpcbind query.
1455          */
1456         *program = nfs_getrpcbyname(MOUNTPROG, nfs_mnt_pgmtbl);
1457         return 1;
1458 }
1459
1460 /*
1461  * Returns TRUE if @version contains a valid value for this option,
1462  * or FALSE if the option was specified with an invalid value.
1463  */
1464 static int
1465 nfs_mount_version(struct mount_options *options, unsigned long *version)
1466 {
1467         long tmp;
1468
1469         switch (po_get_numeric(options, "mountvers", &tmp)) {
1470         case PO_NOT_FOUND:
1471                 break;
1472         case PO_FOUND:
1473                 if (tmp >= 1 && tmp <= 4) {
1474                         *version = tmp;
1475                         return 1;
1476                 }
1477         case PO_BAD_VALUE:
1478                 nfs_error(_("%s: invalid value for 'mountvers=' option"),
1479                                 progname);
1480                 return 0;
1481         }
1482
1483         /*
1484          * MNT version wasn't specified.  The pmap version value
1485          * will be filled in later by an rpcbind query in this case.
1486          */
1487         *version = 0;
1488         return 1;
1489 }
1490
1491 /*
1492  * Returns TRUE if @protocol contains a valid value for this option,
1493  * or FALSE if the option was specified with an invalid value. On
1494  * error, errno is set.
1495  */
1496 static int
1497 nfs_mount_protocol(struct mount_options *options, unsigned long *protocol)
1498 {
1499         sa_family_t family;
1500         char *option;
1501
1502         option = po_get(options, "mountproto");
1503         if (option != NULL) {
1504                 if (!nfs_get_proto(option, &family, protocol)) {
1505                         errno = EPROTONOSUPPORT;
1506                         nfs_error(_("%s: Failed to find '%s' protocol"), 
1507                                 progname, option);
1508                         return 0;
1509                 }
1510                 return 1;
1511         }
1512
1513         /*
1514          * MNT transport protocol wasn't specified.  If the NFS
1515          * transport protocol was specified, use that; otherwise
1516          * set @protocol to zero.  The pmap protocol value will
1517          * be filled in later by an rpcbind query in this case.
1518          */
1519         if (!nfs_nfs_protocol(options, protocol))
1520                 return 0;
1521         if (*protocol == NFSPROTO_RDMA)
1522                 *protocol = IPPROTO_TCP;
1523         return 1;
1524 }
1525
1526 /*
1527  * Returns TRUE if @port contains a valid value for this option,
1528  * or FALSE if the option was specified with an invalid value.
1529  */
1530 static int
1531 nfs_mount_port(struct mount_options *options, unsigned long *port)
1532 {
1533         long tmp;
1534
1535         switch (po_get_numeric(options, "mountport", &tmp)) {
1536         case PO_NOT_FOUND:
1537                 break;
1538         case PO_FOUND:
1539                 if (tmp >= 0 && tmp <= 65535) {
1540                         *port = tmp;
1541                         return 1;
1542                 }
1543         case PO_BAD_VALUE:
1544                 nfs_error(_("%s: invalid value for 'mountport=' option"),
1545                                 progname);
1546                 return 0;
1547         }
1548
1549         /*
1550          * MNT service port wasn't specified.  The pmap port value
1551          * will be filled in later by an rpcbind query in this case.
1552          */
1553         *port = 0;
1554         return 1;
1555 }
1556
1557 /*
1558  * Returns TRUE and fills in @family if a valid MNT protocol option
1559  * is found, or FALSE if the option was specified with an invalid value
1560  * or if the protocol family isn't supported. On error, errno is set.
1561  */
1562 int nfs_mount_proto_family(struct mount_options *options,
1563                                 sa_family_t *family)
1564 {
1565         unsigned long protocol;
1566         char *option;
1567         sa_family_t tmp_family = config_default_family;
1568
1569         option = po_get(options, "mountproto");
1570         if (option != NULL) {
1571                 if (!nfs_get_proto(option, &tmp_family, &protocol)) {
1572                         nfs_error(_("%s: Failed to find '%s' protocol"), 
1573                                 progname, option);
1574                         errno = EPROTONOSUPPORT;
1575                         goto out_err;
1576                 }
1577                 if (!nfs_verify_family(tmp_family))
1578                         goto out_err;
1579                 *family = tmp_family;
1580                 return 1;
1581         }
1582
1583         /*
1584          * MNT transport protocol wasn't specified.  If the NFS
1585          * transport protocol was specified, derive the family
1586          * from that; otherwise, return the default family for
1587          * NFS.
1588          */
1589         return nfs_nfs_proto_family(options, family);
1590 out_err:
1591         errno = EAFNOSUPPORT;
1592         return 0;
1593 }
1594
1595 /**
1596  * nfs_options2pmap - set up pmap structs based on mount options
1597  * @options: pointer to mount options
1598  * @nfs_pmap: OUT: pointer to pmap arguments for NFS server
1599  * @mnt_pmap: OUT: pointer to pmap arguments for mountd server
1600  *
1601  * Returns TRUE if the pmap options specified in @options have valid
1602  * values; otherwise FALSE is returned.
1603  */
1604 int nfs_options2pmap(struct mount_options *options,
1605                      struct pmap *nfs_pmap, struct pmap *mnt_pmap)
1606 {
1607         if (!nfs_nfs_program(options, &nfs_pmap->pm_prog))
1608                 return 0;
1609         if (!nfs_nfs_version(options, &nfs_pmap->pm_vers))
1610                 return 0;
1611         if (!nfs_nfs_protocol(options, &nfs_pmap->pm_prot))
1612                 return 0;
1613         if (!nfs_nfs_port(options, &nfs_pmap->pm_port))
1614                 return 0;
1615
1616         if (!nfs_mount_program(options, &mnt_pmap->pm_prog))
1617                 return 0;
1618         if (!nfs_mount_version(options, &mnt_pmap->pm_vers))
1619                 return 0;
1620         if (!nfs_mount_protocol(options, &mnt_pmap->pm_prot))
1621                 return 0;
1622         if (!nfs_mount_port(options, &mnt_pmap->pm_port))
1623                 return 0;
1624
1625         return 1;
1626 }
1627
1628 /*
1629  * Discover mount server's hostname/address by examining mount options
1630  *
1631  * Returns a pointer to a string that the caller must free, on
1632  * success; otherwise NULL is returned.
1633  */
1634 static char *nfs_umount_hostname(struct mount_options *options,
1635                                  char *hostname)
1636 {
1637         char *option;
1638
1639         option = po_get(options, "mountaddr");
1640         if (option)
1641                 goto out;
1642         option = po_get(options, "mounthost");
1643         if (option)
1644                 goto out;
1645         option = po_get(options, "addr");
1646         if (option)
1647                 goto out;
1648
1649         return hostname;
1650
1651 out:
1652         free(hostname);
1653         return strdup(option);
1654 }
1655
1656
1657 /*
1658  * Returns EX_SUCCESS if mount options and device name have been
1659  * parsed successfully; otherwise EX_FAIL.
1660  */
1661 int nfs_umount_do_umnt(struct mount_options *options,
1662                        char **hostname, char **dirname)
1663 {
1664         union nfs_sockaddr address;
1665         struct sockaddr *sap = &address.sa;
1666         socklen_t salen = sizeof(address);
1667         struct pmap nfs_pmap, mnt_pmap;
1668         sa_family_t family;
1669
1670         if (!nfs_options2pmap(options, &nfs_pmap, &mnt_pmap))
1671                 return EX_FAIL;
1672
1673         /* Skip UMNT call for vers=4 mounts */
1674         if (nfs_pmap.pm_vers == 4)
1675                 return EX_SUCCESS;
1676
1677         *hostname = nfs_umount_hostname(options, *hostname);
1678         if (!*hostname) {
1679                 nfs_error(_("%s: out of memory"), progname);
1680                 return EX_FAIL;
1681         }
1682
1683         if (!nfs_mount_proto_family(options, &family))
1684                 return 0;
1685         if (!nfs_lookup(*hostname, family, sap, &salen))
1686                 /* nfs_lookup reports any errors */
1687                 return EX_FAIL;
1688
1689         if (nfs_advise_umount(sap, salen, &mnt_pmap, dirname) == 0)
1690                 /* nfs_advise_umount reports any errors */
1691                 return EX_FAIL;
1692
1693         return EX_SUCCESS;
1694 }