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