]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/network.c
mount.nfs: Fix some nfs_error() nits in network.c
[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  * @addrlen:    length of presentation address
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, const size_t addrlen,
273                            struct sockaddr *sap, 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 (verbose && res != RPC_SUCCESS)
853                 nfs_error(_("%s: UMNT call failed: %s"),
854                         progname, clnt_sperrno(res));
855
856         auth_destroy(client->cl_auth);
857         CLNT_DESTROY(client);
858
859         if (res != RPC_SUCCESS)
860                 return 0;
861         return 1;
862 }
863
864 /**
865  * nfs_call_umount - ask the server to remove a share from it's rmtab
866  * @mnt_server: address of RPC MNT program server
867  * @argp: directory path of share to "unmount"
868  *
869  * Returns one if the unmount call succeeded; zero if the unmount
870  * failed for any reason.
871  *
872  * Note that a side effect of calling this function is that rpccreateerr
873  * is set.
874  */
875 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
876 {
877         struct sockaddr *sap = (struct sockaddr *)&mnt_server->saddr;
878         socklen_t salen = sizeof(mnt_server->saddr);
879         struct pmap *pmap = &mnt_server->pmap;
880         CLIENT *clnt;
881         enum clnt_stat res = 0;
882         int msock;
883
884         if (!nfs_probe_mntport(sap, salen, pmap))
885                 return 0;
886         clnt = mnt_openclnt(mnt_server, &msock);
887         if (!clnt)
888                 return 0;
889         res = clnt_call(clnt, MOUNTPROC_UMNT,
890                         (xdrproc_t)xdr_dirpath, (caddr_t)argp,
891                         (xdrproc_t)xdr_void, NULL,
892                         TIMEOUT);
893         mnt_closeclnt(clnt, msock);
894
895         if (res == RPC_SUCCESS)
896                 return 1;
897         return 0;
898 }
899
900 /**
901  * mnt_openclnt - get a handle for a remote mountd service
902  * @mnt_server: address and pmap arguments of mountd service
903  * @msock: returns a file descriptor of the underlying transport socket
904  *
905  * Returns an active handle for the remote's mountd service
906  */
907 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
908 {
909         struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
910         struct pmap *mnt_pmap = &mnt_server->pmap;
911         CLIENT *clnt = NULL;
912
913         mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
914         *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, MOUNT_TIMEOUT,
915                                 TRUE, FALSE);
916         if (*msock == RPC_ANYSOCK) {
917                 if (rpc_createerr.cf_error.re_errno == EADDRINUSE)
918                         /*
919                          * Probably in-use by a TIME_WAIT connection,
920                          * It is worth waiting a while and trying again.
921                          */
922                         rpc_createerr.cf_stat = RPC_TIMEDOUT;
923                 return NULL;
924         }
925
926         switch (mnt_pmap->pm_prot) {
927         case IPPROTO_UDP:
928                 clnt = clntudp_bufcreate(mnt_saddr,
929                                          mnt_pmap->pm_prog, mnt_pmap->pm_vers,
930                                          RETRY_TIMEOUT, msock,
931                                          MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
932                 break;
933         case IPPROTO_TCP:
934                 clnt = clnttcp_create(mnt_saddr,
935                                       mnt_pmap->pm_prog, mnt_pmap->pm_vers,
936                                       msock,
937                                       MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
938                 break;
939         }
940         if (clnt) {
941                 /* try to mount hostname:dirname */
942                 clnt->cl_auth = authunix_create_default();
943                 return clnt;
944         }
945         return NULL;
946 }
947
948 /**
949  * mnt_closeclnt - terminate a handle for a remote mountd service
950  * @clnt: pointer to an active handle for a remote mountd service
951  * @msock: file descriptor of the underlying transport socket
952  *
953  */
954 void mnt_closeclnt(CLIENT *clnt, int msock)
955 {
956         auth_destroy(clnt->cl_auth);
957         clnt_destroy(clnt);
958         close(msock);
959 }
960
961 /**
962  * clnt_ping - send an RPC ping to the remote RPC service endpoint
963  * @saddr: server's address
964  * @prog: target RPC program number
965  * @vers: target RPC version number
966  * @prot: target RPC protocol
967  * @caddr: filled in with our network address
968  *
969  * Sigh... GETPORT queries don't actually check the version number.
970  * In order to make sure that the server actually supports the service
971  * we're requesting, we open an RPC client, and fire off a NULL
972  * RPC call.
973  *
974  * caddr is the network address that the server will use to call us back.
975  * On multi-homed clients, this address depends on which NIC we use to
976  * route requests to the server.
977  *
978  * Returns one if successful, otherwise zero.
979  */
980 int clnt_ping(struct sockaddr_in *saddr, const unsigned long prog,
981                 const unsigned long vers, const unsigned int prot,
982                 struct sockaddr_in *caddr)
983 {
984         CLIENT *clnt = NULL;
985         int sock, stat;
986         static char clnt_res;
987         struct sockaddr dissolve;
988
989         rpc_createerr.cf_stat = stat = 0;
990         sock = get_socket(saddr, prot, CONNECT_TIMEOUT, FALSE, TRUE);
991         if (sock == RPC_ANYSOCK) {
992                 if (rpc_createerr.cf_error.re_errno == ETIMEDOUT) {
993                         /*
994                          * TCP timeout. Bubble up the error to see 
995                          * how it should be handled.
996                          */
997                         rpc_createerr.cf_stat = RPC_TIMEDOUT;
998                 }
999                 return 0;
1000         }
1001
1002         if (caddr) {
1003                 /* Get the address of our end of this connection */
1004                 socklen_t len = sizeof(*caddr);
1005                 if (getsockname(sock, caddr, &len) != 0)
1006                         caddr->sin_family = 0;
1007         }
1008
1009         switch(prot) {
1010         case IPPROTO_UDP:
1011                 /* The socket is connected (so we could getsockname successfully),
1012                  * but some servers on multi-homed hosts reply from
1013                  * the wrong address, so if we stay connected, we lose the reply.
1014                  */
1015                 dissolve.sa_family = AF_UNSPEC;
1016                 connect(sock, &dissolve, sizeof(dissolve));
1017
1018                 clnt = clntudp_bufcreate(saddr, prog, vers,
1019                                          RETRY_TIMEOUT, &sock,
1020                                          RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
1021                 break;
1022         case IPPROTO_TCP:
1023                 clnt = clnttcp_create(saddr, prog, vers, &sock,
1024                                       RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
1025                 break;
1026         }
1027         if (!clnt) {
1028                 close(sock);
1029                 return 0;
1030         }
1031         memset(&clnt_res, 0, sizeof(clnt_res));
1032         stat = clnt_call(clnt, NULLPROC,
1033                          (xdrproc_t)xdr_void, (caddr_t)NULL,
1034                          (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
1035                          TIMEOUT);
1036         if (stat) {
1037                 clnt_geterr(clnt, &rpc_createerr.cf_error);
1038                 rpc_createerr.cf_stat = stat;
1039         }
1040         clnt_destroy(clnt);
1041         close(sock);
1042
1043         if (stat == RPC_SUCCESS)
1044                 return 1;
1045         else
1046                 return 0;
1047 }
1048
1049 /*
1050  * Try a getsockname() on a connected datagram socket.
1051  *
1052  * Returns 1 and fills in @buf if successful; otherwise, zero.
1053  *
1054  * A connected datagram socket prevents leaving a socket in TIME_WAIT.
1055  * This conserves the ephemeral port number space, helping reduce failed
1056  * socket binds during mount storms.
1057  */
1058 static int nfs_ca_sockname(const struct sockaddr *sap, const socklen_t salen,
1059                            struct sockaddr *buf, socklen_t *buflen)
1060 {
1061         struct sockaddr_in sin = {
1062                 .sin_family             = AF_INET,
1063                 .sin_addr.s_addr        = htonl(INADDR_ANY),
1064         };
1065         struct sockaddr_in6 sin6 = {
1066                 .sin6_family            = AF_INET6,
1067                 .sin6_addr              = IN6ADDR_ANY_INIT,
1068         };
1069         int sock;
1070
1071         sock = socket(sap->sa_family, SOCK_DGRAM, IPPROTO_UDP);
1072         if (sock < 0)
1073                 return 0;
1074
1075         switch (sap->sa_family) {
1076         case AF_INET:
1077                 if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
1078                         close(sock);
1079                         return 0;
1080                 }
1081                 break;
1082         case AF_INET6:
1083                 if (bind(sock, (struct sockaddr *)&sin6, sizeof(sin6)) < 0) {
1084                         close(sock);
1085                         return 0;
1086                 }
1087                 break;
1088         default:
1089                 errno = EAFNOSUPPORT;
1090                 return 0;
1091         }
1092
1093         if (connect(sock, sap, salen) < 0) {
1094                 close(sock);
1095                 return 0;
1096         }
1097
1098         return !getsockname(sock, buf, buflen);
1099 }
1100
1101 /*
1102  * Try to generate an address that prevents the server from calling us.
1103  *
1104  * Returns 1 and fills in @buf if successful; otherwise, zero.
1105  */
1106 static int nfs_ca_gai(const struct sockaddr *sap, const socklen_t salen,
1107                       struct sockaddr *buf, socklen_t *buflen)
1108 {
1109         struct addrinfo *gai_results;
1110         struct addrinfo gai_hint = {
1111                 .ai_family      = sap->sa_family,
1112                 .ai_flags       = AI_PASSIVE,   /* ANYADDR */
1113         };
1114
1115         if (getaddrinfo(NULL, "", &gai_hint, &gai_results))
1116                 return 0;
1117
1118         *buflen = gai_results->ai_addrlen;
1119         memcpy(buf, gai_results->ai_addr, *buflen);
1120
1121         freeaddrinfo(gai_results);
1122
1123         return 1;
1124 }
1125
1126 /**
1127  * nfs_callback_address - acquire our local network address
1128  * @sap: pointer to address of remote
1129  * @sap_len: length of address
1130  * @buf: pointer to buffer to be filled in with local network address
1131  * @buflen: IN: length of buffer to fill in; OUT: length of filled-in address
1132  *
1133  * Discover a network address that an NFSv4 server can use to call us back.
1134  * On multi-homed clients, this address depends on which NIC we use to
1135  * route requests to the server.
1136  *
1137  * Returns 1 and fills in @buf if an unambiguous local address is
1138  * available; returns 1 and fills in an appropriate ANYADDR address
1139  * if a local address isn't available; otherwise, returns zero.
1140  */
1141 int nfs_callback_address(const struct sockaddr *sap, const socklen_t salen,
1142                          struct sockaddr *buf, socklen_t *buflen)
1143 {
1144         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1145
1146         if (nfs_ca_sockname(sap, salen, buf, buflen) == 0)
1147                 if (nfs_ca_gai(sap, salen, buf, buflen) == 0)
1148                         goto out_failed;
1149
1150         /*
1151          * The server can't use an interface ID that was generated
1152          * here on the client, so always clear sin6_scope_id.
1153          */
1154         if (sin6->sin6_family == AF_INET6)
1155                 sin6->sin6_scope_id = 0;
1156
1157         return 1;
1158
1159 out_failed:
1160         *buflen = 0;
1161         if (verbose)
1162                 nfs_error(_("%s: failed to construct callback address"),
1163                                 progname);
1164         return 0;
1165 }
1166
1167 /*
1168  * "nfsprog" is supported only by the legacy mount command.  The
1169  * kernel mount client does not support this option.
1170  *
1171  * Returns TRUE if @program contains a valid value for this option,
1172  * or FALSE if the option was specified with an invalid value.
1173  */
1174 static int
1175 nfs_nfs_program(struct mount_options *options, unsigned long *program)
1176 {
1177         long tmp;
1178
1179         switch (po_get_numeric(options, "nfsprog", &tmp)) {
1180         case PO_NOT_FOUND:
1181                 break;
1182         case PO_FOUND:
1183                 if (tmp > 0) {
1184                         *program = tmp;
1185                         return 1;
1186                 }
1187         case PO_BAD_VALUE:
1188                 return 0;
1189         }
1190
1191         /*
1192          * NFS RPC program wasn't specified.  The RPC program
1193          * cannot be determined via an rpcbind query.
1194          */
1195         *program = nfs_getrpcbyname(NFSPROG, nfs_nfs_pgmtbl);
1196         return 1;
1197 }
1198
1199 /*
1200  * Returns TRUE if @version contains a valid value for this option,
1201  * or FALSE if the option was specified with an invalid value.
1202  */
1203 static int
1204 nfs_nfs_version(struct mount_options *options, unsigned long *version)
1205 {
1206         long tmp;
1207
1208         switch (po_rightmost(options, nfs_version_opttbl)) {
1209         case 0: /* v2 */
1210                 *version = 2;
1211                 return 1;
1212         case 1: /* v3 */
1213                 *version = 3;
1214                 return 1;
1215         case 2: /* vers */
1216                 switch (po_get_numeric(options, "vers", &tmp)) {
1217                 case PO_FOUND:
1218                         if (tmp >= 2 && tmp <= 3) {
1219                                 *version = tmp;
1220                                 return 1;
1221                         }
1222                         return 0;
1223                 case PO_NOT_FOUND:
1224                         nfs_error(_("%s: option parsing error\n"),
1225                                         progname);
1226                 case PO_BAD_VALUE:
1227                         return 0;
1228                 }
1229         case 3: /* nfsvers */
1230                 switch (po_get_numeric(options, "nfsvers", &tmp)) {
1231                 case PO_FOUND:
1232                         if (tmp >= 2 && tmp <= 3) {
1233                                 *version = tmp;
1234                                 return 1;
1235                         }
1236                         return 0;
1237                 case PO_NOT_FOUND:
1238                         nfs_error(_("%s: option parsing error\n"),
1239                                         progname);
1240                 case PO_BAD_VALUE:
1241                         return 0;
1242                 }
1243         }
1244
1245         /*
1246          * NFS version wasn't specified.  The pmap version value
1247          * will be filled in later by an rpcbind query in this case.
1248          */
1249         *version = 0;
1250         return 1;
1251 }
1252
1253 /*
1254  * Returns TRUE if @protocol contains a valid value for this option,
1255  * or FALSE if the option was specified with an invalid value.
1256  */
1257 static int
1258 nfs_nfs_protocol(struct mount_options *options, unsigned long *protocol)
1259 {
1260         char *option;
1261
1262         switch (po_rightmost(options, nfs_transport_opttbl)) {
1263         case 0: /* udp */
1264                 *protocol = IPPROTO_UDP;
1265                 return 1;
1266         case 1: /* tcp */
1267                 *protocol = IPPROTO_TCP;
1268                 return 1;
1269         case 2: /* proto */
1270                 option = po_get(options, "proto");
1271                 if (option) {
1272                         if (strcmp(option, "tcp") == 0) {
1273                                 *protocol = IPPROTO_TCP;
1274                                 return 1;
1275                         }
1276                         if (strcmp(option, "udp") == 0) {
1277                                 *protocol = IPPROTO_UDP;
1278                                 return 1;
1279                         }
1280                         return 0;
1281                 }
1282         }
1283
1284         /*
1285          * NFS transport protocol wasn't specified.  The pmap
1286          * protocol value will be filled in later by an rpcbind
1287          * query in this case.
1288          */
1289         *protocol = 0;
1290         return 1;
1291 }
1292
1293 /*
1294  * Returns TRUE if @port contains a valid value for this option,
1295  * or FALSE if the option was specified with an invalid value.
1296  */
1297 static int
1298 nfs_nfs_port(struct mount_options *options, unsigned long *port)
1299 {
1300         long tmp;
1301
1302         switch (po_get_numeric(options, "port", &tmp)) {
1303         case PO_NOT_FOUND:
1304                 break;
1305         case PO_FOUND:
1306                 if (tmp >= 1 && tmp <= 65535) {
1307                         *port = tmp;
1308                         return 1;
1309                 }
1310         case PO_BAD_VALUE:
1311                 return 0;
1312         }
1313
1314         /*
1315          * NFS service port wasn't specified.  The pmap port value
1316          * will be filled in later by an rpcbind query in this case.
1317          */
1318         *port = 0;
1319         return 1;
1320 }
1321
1322 /*
1323  * "mountprog" is supported only by the legacy mount command.  The
1324  * kernel mount client does not support this option.
1325  *
1326  * Returns TRUE if @program contains a valid value for this option,
1327  * or FALSE if the option was specified with an invalid value.
1328  */
1329 static int
1330 nfs_mount_program(struct mount_options *options, unsigned long *program)
1331 {
1332         long tmp;
1333
1334         switch (po_get_numeric(options, "mountprog", &tmp)) {
1335         case PO_NOT_FOUND:
1336                 break;
1337         case PO_FOUND:
1338                 if (tmp > 0) {
1339                         *program = tmp;
1340                         return 1;
1341                 }
1342         case PO_BAD_VALUE:
1343                 return 0;
1344         }
1345
1346         /*
1347          * MNT RPC program wasn't specified.  The RPC program
1348          * cannot be determined via an rpcbind query.
1349          */
1350         *program = nfs_getrpcbyname(MOUNTPROG, nfs_mnt_pgmtbl);
1351         return 1;
1352 }
1353
1354 /*
1355  * Returns TRUE if @version contains a valid value for this option,
1356  * or FALSE if the option was specified with an invalid value.
1357  */
1358 static int
1359 nfs_mount_version(struct mount_options *options, unsigned long *version)
1360 {
1361         long tmp;
1362
1363         switch (po_get_numeric(options, "mountvers", &tmp)) {
1364         case PO_NOT_FOUND:
1365                 break;
1366         case PO_FOUND:
1367                 if (tmp >= 1 && tmp <= 4) {
1368                         *version = tmp;
1369                         return 1;
1370                 }
1371         case PO_BAD_VALUE:
1372                 return 0;
1373         }
1374
1375         /*
1376          * MNT version wasn't specified.  The pmap version value
1377          * will be filled in later by an rpcbind query in this case.
1378          */
1379         *version = 0;
1380         return 1;
1381 }
1382
1383 /*
1384  * Returns TRUE if @protocol contains a valid value for this option,
1385  * or FALSE if the option was specified with an invalid value.
1386  */
1387 static int
1388 nfs_mount_protocol(struct mount_options *options, unsigned long *protocol)
1389 {
1390         char *option;
1391
1392         option = po_get(options, "mountproto");
1393         if (option) {
1394                 if (strcmp(option, "tcp") == 0) {
1395                         *protocol = IPPROTO_TCP;
1396                         return 1;
1397                 }
1398                 if (strcmp(option, "udp") == 0) {
1399                         *protocol = IPPROTO_UDP;
1400                         return 1;
1401                 }
1402                 return 0;
1403         }
1404
1405         /*
1406          * MNT transport protocol wasn't specified.  If the NFS
1407          * transport protocol was specified, use that; otherwise
1408          * set @protocol to zero.  The pmap protocol value will
1409          * be filled in later by an rpcbind query in this case.
1410          */
1411         return nfs_nfs_protocol(options, protocol);
1412 }
1413
1414 /*
1415  * Returns TRUE if @port contains a valid value for this option,
1416  * or FALSE if the option was specified with an invalid value.
1417  */
1418 static int
1419 nfs_mount_port(struct mount_options *options, unsigned long *port)
1420 {
1421         long tmp;
1422
1423         switch (po_get_numeric(options, "mountport", &tmp)) {
1424         case PO_NOT_FOUND:
1425                 break;
1426         case PO_FOUND:
1427                 if (tmp >= 1 && tmp <= 65535) {
1428                         *port = tmp;
1429                         return 1;
1430                 }
1431         case PO_BAD_VALUE:
1432                 return 0;
1433         }
1434
1435         /*
1436          * MNT service port wasn't specified.  The pmap port value
1437          * will be filled in later by an rpcbind query in this case.
1438          */
1439         *port = 0;
1440         return 1;
1441 }
1442
1443 /**
1444  * nfs_options2pmap - set up pmap structs based on mount options
1445  * @options: pointer to mount options
1446  * @nfs_pmap: OUT: pointer to pmap arguments for NFS server
1447  * @mnt_pmap: OUT: pointer to pmap arguments for mountd server
1448  *
1449  * Returns TRUE if the pmap options specified in @options have valid
1450  * values; otherwise FALSE is returned.
1451  */
1452 int nfs_options2pmap(struct mount_options *options,
1453                      struct pmap *nfs_pmap, struct pmap *mnt_pmap)
1454 {
1455         if (!nfs_nfs_program(options, &nfs_pmap->pm_prog))
1456                 return 0;
1457         if (!nfs_nfs_version(options, &nfs_pmap->pm_vers))
1458                 return 0;
1459         if (!nfs_nfs_protocol(options, &nfs_pmap->pm_prot))
1460                 return 0;
1461         if (!nfs_nfs_port(options, &nfs_pmap->pm_port))
1462                 return 0;
1463
1464         if (!nfs_mount_program(options, &mnt_pmap->pm_prog))
1465                 return 0;
1466         if (!nfs_mount_version(options, &mnt_pmap->pm_vers))
1467                 return 0;
1468         if (!nfs_mount_protocol(options, &mnt_pmap->pm_prot))
1469                 return 0;
1470         if (!nfs_mount_port(options, &mnt_pmap->pm_port))
1471                 return 0;
1472
1473         return 1;
1474 }