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