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