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