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