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