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