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