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