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