2 * network.c -- Provide common network functions for NFS mount/umount
4 * Copyright (C) 2007 Oracle. All rights reserved.
5 * Copyright (C) 2007 Chuck Lever <chuck.lever@oracle.com>
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.
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.
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.
37 #include <sys/types.h>
38 #include <sys/socket.h>
40 #include <netinet/in.h>
42 #include <rpc/pmap_prot.h>
43 #include <rpc/pmap_clnt.h>
48 #include "nfs_mount.h"
49 #include "mount_constants.h"
51 #include "parse_opt.h"
54 #define PMAP_TIMEOUT (10)
55 #define CONNECT_TIMEOUT (20)
56 #define MOUNT_TIMEOUT (30)
58 #if SIZEOF_SOCKLEN_T - 0 == 0
59 #define socklen_t unsigned int
62 extern int nfs_mount_data_version;
63 extern char *progname;
66 static const char *nfs_ns_pgmtbl[] = {
71 static const char *nfs_mnt_pgmtbl[] = {
77 static const char *nfs_nfs_pgmtbl[] = {
83 static const char *nfs_transport_opttbl[] = {
90 static const char *nfs_version_opttbl[] = {
98 static const unsigned long nfs_to_mnt[] = {
105 static const unsigned long mnt_to_nfs[] = {
113 * Map an NFS version into the corresponding Mountd version
115 unsigned long nfsvers_to_mnt(const unsigned long vers)
118 return nfs_to_mnt[vers];
123 * Map a Mountd version into the corresponding NFS version
125 static unsigned long mntvers_to_nfs(const unsigned long vers)
128 return mnt_to_nfs[vers];
132 static const unsigned int probe_udp_only[] = {
137 static const unsigned int probe_udp_first[] = {
143 static const unsigned int probe_tcp_first[] = {
149 static const unsigned long probe_nfs2_only[] = {
154 static const unsigned long probe_nfs3_first[] = {
160 static const unsigned long probe_mnt1_first[] = {
166 static const unsigned long probe_mnt3_first[] = {
173 static void nfs_set_port(struct sockaddr *sap, const unsigned short port)
175 switch (sap->sa_family) {
177 ((struct sockaddr_in *)sap)->sin_port = htons(port);
180 ((struct sockaddr_in6 *)sap)->sin6_port = htons(port);
183 nfs_error(_("%s: unrecognized address family in %s"),
188 static int nfs_lookup(const char *hostname, const sa_family_t family,
189 struct sockaddr *sap, socklen_t *salen)
191 struct addrinfo *gai_results;
192 struct addrinfo gai_hint = {
193 #ifdef HAVE_DECL_AI_ADDRCONFIG
194 .ai_flags = AI_ADDRCONFIG,
195 #endif /* HAVE_DECL_AI_ADDRCONFIG */
198 socklen_t len = *salen;
203 error = getaddrinfo(hostname, NULL, &gai_hint, &gai_results);
208 nfs_error(_("%s: DNS resolution failed for %s: %s"),
209 progname, hostname, strerror(errno));
212 nfs_error(_("%s: DNS resolution failed for %s: %s"),
213 progname, hostname, gai_strerror(error));
217 switch (gai_results->ai_addr->sa_family) {
220 if (len >= gai_results->ai_addrlen) {
221 *salen = gai_results->ai_addrlen;
222 memcpy(sap, gai_results->ai_addr, *salen);
227 /* things are really broken if we get here, so warn */
228 nfs_error(_("%s: unrecognized DNS resolution results for %s"),
233 freeaddrinfo(gai_results);
238 * nfs_name_to_address - resolve hostname to an IPv4 or IPv6 socket address
239 * @hostname: pointer to C string containing DNS hostname to resolve
240 * @sap: pointer to buffer to fill with socket address
241 * @len: IN: size of buffer to fill; OUT: size of socket address
243 * Returns 1 and places a socket address at @sap if successful;
246 int nfs_name_to_address(const char *hostname,
247 struct sockaddr *sap, socklen_t *salen)
249 #ifdef IPV6_SUPPORTED
250 return nfs_lookup(hostname, AF_UNSPEC, sap, salen);
251 #else /* !IPV6_SUPPORTED */
252 return nfs_lookup(hostname, AF_INET, sap, salen);
253 #endif /* !IPV6_SUPPORTED */
257 * nfs_gethostbyname - resolve a hostname to an IPv4 address
258 * @hostname: pointer to a C string containing a DNS hostname
259 * @sin: returns an IPv4 address
261 * Returns 1 if successful, otherwise zero.
263 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *sin)
265 socklen_t len = sizeof(*sin);
267 return nfs_lookup(hostname, AF_INET, (struct sockaddr *)sin, &len);
271 * nfs_string_to_sockaddr - convert string address to sockaddr
272 * @address: pointer to presentation format address to convert
273 * @addrlen: length of presentation address
274 * @sap: pointer to socket address buffer to fill in
275 * @salen: IN: length of address buffer
276 * OUT: length of converted socket address
278 * Convert a presentation format address string to a socket address.
279 * Similar to nfs_name_to_address(), but the DNS query is squelched,
280 * and won't make any noise if the getaddrinfo() call fails.
282 * Returns 1 and fills in @sap and @salen if successful; otherwise zero.
284 * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
285 * on presenting IPv6 addresses as text strings.
287 int nfs_string_to_sockaddr(const char *address, const size_t addrlen,
288 struct sockaddr *sap, socklen_t *salen)
290 struct addrinfo *gai_results;
291 struct addrinfo gai_hint = {
292 .ai_flags = AI_NUMERICHOST,
294 socklen_t len = *salen;
299 if (getaddrinfo(address, NULL, &gai_hint, &gai_results) == 0) {
300 switch (gai_results->ai_addr->sa_family) {
303 if (len >= gai_results->ai_addrlen) {
304 *salen = gai_results->ai_addrlen;
305 memcpy(sap, gai_results->ai_addr, *salen);
310 freeaddrinfo(gai_results);
317 * nfs_present_sockaddr - convert sockaddr to string
318 * @sap: pointer to socket address to convert
319 * @salen: length of socket address
320 * @buf: pointer to buffer to fill in
321 * @buflen: length of buffer
323 * Convert the passed-in sockaddr-style address to presentation format.
324 * The presentation format address is placed in @buf and is
327 * Returns 1 if successful; otherwise zero.
329 * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
330 * on presenting IPv6 addresses as text strings.
332 int nfs_present_sockaddr(const struct sockaddr *sap, const socklen_t salen,
333 char *buf, const size_t buflen)
335 #ifdef HAVE_GETNAMEINFO
338 result = getnameinfo(sap, salen, buf, buflen,
339 NULL, 0, NI_NUMERICHOST);
343 nfs_error(_("%s: invalid server address: %s"), progname,
344 gai_strerror(result));
346 #else /* HAVE_GETNAMEINFO */
349 if (sap->sa_family == AF_INET) {
350 addr = inet_ntoa(((struct sockaddr_in *)sap)->sin_addr);
351 if (addr && strlen(addr) < buflen) {
357 nfs_error(_("%s: invalid server address"), progname);
359 #endif /* HAVE_GETNAMEINFO */
363 * Attempt to connect a socket, but time out after "timeout" seconds.
365 * On error return, caller closes the socket.
367 static int connect_to(int fd, struct sockaddr *addr,
368 socklen_t addrlen, int timeout)
372 struct timeval tv = {
376 saved = fcntl(fd, F_GETFL, 0);
377 fcntl(fd, F_SETFL, saved | O_NONBLOCK);
379 ret = connect(fd, addr, addrlen);
380 if (ret < 0 && errno != EINPROGRESS)
388 ret = select(fd + 1, &rset, &wset, NULL, &tv);
393 if (FD_ISSET(fd, &rset) || FD_ISSET(fd, &wset)) {
395 socklen_t len = sizeof(error);
396 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
406 fcntl(fd, F_SETFL, saved);
411 * Create a socket that is locally bound to a reserved or non-reserved port.
413 * The caller should check rpc_createerr to determine the cause of any error.
415 static int get_socket(struct sockaddr_in *saddr, unsigned int p_prot,
416 unsigned int timeout, int resvp, int conn)
419 struct sockaddr_in laddr;
420 socklen_t namelen = sizeof(laddr);
422 type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
423 if ((so = socket (AF_INET, type, p_prot)) < 0)
426 laddr.sin_family = AF_INET;
428 laddr.sin_addr.s_addr = htonl(INADDR_ANY);
430 if (bindresvport(so, &laddr) < 0)
431 goto err_bindresvport;
433 cc = bind(so, (struct sockaddr *)&laddr, namelen);
437 if (type == SOCK_STREAM || (conn && type == SOCK_DGRAM)) {
438 cc = connect_to(so, (struct sockaddr *)saddr, namelen,
446 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
447 rpc_createerr.cf_error.re_errno = errno;
449 nfs_error(_("%s: Unable to create %s socket: errno %d (%s)\n"),
450 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
451 errno, strerror(errno));
456 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
457 rpc_createerr.cf_error.re_errno = errno;
459 nfs_error(_("%s: Unable to bindresvport %s socket: errno %d"
461 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
462 errno, strerror(errno));
468 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
469 rpc_createerr.cf_error.re_errno = errno;
471 nfs_error(_("%s: Unable to bind to %s socket: errno %d (%s)\n"),
472 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
473 errno, strerror(errno));
479 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
480 rpc_createerr.cf_error.re_errno = errno;
482 nfs_error(_("%s: Unable to connect to %s:%d, errno %d (%s)\n"),
483 progname, inet_ntoa(saddr->sin_addr),
484 ntohs(saddr->sin_port), errno, strerror(errno));
490 static void nfs_pp_debug(const struct sockaddr *sap, const socklen_t salen,
491 const rpcprog_t program, const rpcvers_t version,
492 const unsigned short protocol,
493 const unsigned short port)
495 char buf[NI_MAXHOST];
500 if (nfs_present_sockaddr(sap, salen, buf, sizeof(buf)) == 0) {
502 strcat(buf, "unknown host");
505 fprintf(stderr, _("%s: trying %s prog %lu vers %lu prot %s port %d\n"),
506 progname, buf, (unsigned long)program,
507 (unsigned long)version,
508 (protocol == IPPROTO_UDP ? _("UDP") : _("TCP")),
512 static void nfs_pp_debug2(const char *str)
517 if (rpc_createerr.cf_error.re_status == RPC_CANTRECV ||
518 rpc_createerr.cf_error.re_status == RPC_CANTSEND)
519 nfs_error(_("%s: portmap query %s%s - %s"),
520 progname, str, clnt_spcreateerror(""),
521 strerror(rpc_createerr.cf_error.re_errno));
523 nfs_error(_("%s: portmap query %s%s"),
524 progname, str, clnt_spcreateerror(""));
528 * Use the portmapper to discover whether or not the service we want is
529 * available. The lists 'versions' and 'protos' define ordered sequences
530 * of service versions and udp/tcp protocols to probe for.
532 * Returns 1 if the requested service port is unambiguous and pingable;
533 * @pmap is filled in with the version, port, and transport protocol used
534 * during the successful ping. Note that if a port is already specified
535 * in @pmap and it matches the rpcbind query result, nfs_probe_port() does
536 * not perform an RPC ping.
538 * If an error occurs or the requested service isn't available, zero is
539 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
541 static int nfs_probe_port(const struct sockaddr *sap, const socklen_t salen,
542 struct pmap *pmap, const unsigned long *versions,
543 const unsigned int *protos)
545 struct sockaddr_storage address;
546 struct sockaddr *saddr = (struct sockaddr *)&address;
547 const unsigned long prog = pmap->pm_prog, *p_vers;
548 const unsigned int prot = (u_int)pmap->pm_prot, *p_prot;
549 const u_short port = (u_short) pmap->pm_port;
550 unsigned long vers = pmap->pm_vers;
551 unsigned short p_port;
553 memcpy(saddr, sap, salen);
554 p_prot = prot ? &prot : protos;
555 p_vers = vers ? &vers : versions;
559 printf(_("%s: prog %lu, trying vers=%lu, prot=%u\n"),
560 progname, prog, *p_vers, *p_prot);
561 p_port = nfs_getport(saddr, salen, prog, *p_vers, *p_prot);
563 if (!port || port == p_port) {
564 nfs_set_port(saddr, p_port);
565 nfs_pp_debug(saddr, salen, prog, *p_vers,
567 if (nfs_rpc_ping(saddr, salen, prog,
568 *p_vers, *p_prot, NULL))
571 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
573 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
574 rpc_createerr.cf_stat != RPC_TIMEDOUT &&
575 rpc_createerr.cf_stat != RPC_CANTRECV &&
576 rpc_createerr.cf_stat != RPC_PROGVERSMISMATCH)
581 nfs_pp_debug2("retrying");
586 if (rpc_createerr.cf_stat == RPC_TIMEDOUT ||
587 rpc_createerr.cf_stat == RPC_CANTRECV)
590 if (vers || !*++p_vers)
594 nfs_pp_debug2("failed");
599 pmap->pm_vers = *p_vers;
601 pmap->pm_prot = *p_prot;
603 pmap->pm_port = p_port;
604 nfs_clear_rpc_createerr();
609 * Probe a server's NFS service to determine which versions and
610 * transport protocols are supported.
612 * Returns 1 if the requested service port is unambiguous and pingable;
613 * @pmap is filled in with the version, port, and transport protocol used
614 * during the successful ping. If all three are already specified, simply
615 * return success without an rpcbind query or RPC ping (we may be trying
616 * to mount an NFS service that is not advertised via rpcbind).
618 * If an error occurs or the requested service isn't available, zero is
619 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
621 static int nfs_probe_nfsport(const struct sockaddr *sap, const socklen_t salen,
624 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
627 if (nfs_mount_data_version >= 4)
628 return nfs_probe_port(sap, salen, pmap,
629 probe_nfs3_first, probe_tcp_first);
631 return nfs_probe_port(sap, salen, pmap,
632 probe_nfs2_only, probe_udp_only);
636 * Probe a server's mountd service to determine which versions and
637 * transport protocols are supported.
639 * Returns 1 if the requested service port is unambiguous and pingable;
640 * @pmap is filled in with the version, port, and transport protocol used
641 * during the successful ping. If all three are already specified, simply
642 * return success without an rpcbind query or RPC ping (we may be trying
643 * to mount an NFS service that is not advertised via rpcbind).
645 * If an error occurs or the requested service isn't available, zero is
646 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
648 static int nfs_probe_mntport(const struct sockaddr *sap, const socklen_t salen,
651 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
654 if (nfs_mount_data_version >= 4)
655 return nfs_probe_port(sap, salen, pmap,
656 probe_mnt3_first, probe_udp_first);
658 return nfs_probe_port(sap, salen, pmap,
659 probe_mnt1_first, probe_udp_only);
663 * Probe a server's mountd service to determine which versions and
664 * transport protocols are supported. Invoked when the protocol
665 * version is already known for both the NFS and mountd service.
667 * Returns 1 and fills in both @pmap structs if the requested service
668 * ports are unambiguous and pingable. Otherwise zero is returned;
669 * rpccreateerr.cf_stat is set to reflect the nature of the error.
671 static int nfs_probe_version_fixed(const struct sockaddr *mnt_saddr,
672 const socklen_t mnt_salen,
673 struct pmap *mnt_pmap,
674 const struct sockaddr *nfs_saddr,
675 const socklen_t nfs_salen,
676 struct pmap *nfs_pmap)
678 if (!nfs_probe_nfsport(nfs_saddr, nfs_salen, nfs_pmap))
680 return nfs_probe_mntport(mnt_saddr, mnt_salen, mnt_pmap);
684 * nfs_probe_bothports - discover the RPC endpoints of mountd and NFS server
685 * @mnt_saddr: pointer to socket address of mountd server
686 * @mnt_salen: length of mountd server's address
687 * @mnt_pmap: IN: partially filled-in mountd RPC service tuple;
688 * OUT: fully filled-in mountd RPC service tuple
689 * @nfs_saddr: pointer to socket address of NFS server
690 * @nfs_salen: length of NFS server's address
691 * @nfs_pmap: IN: partially filled-in NFS RPC service tuple;
692 * OUT: fully filled-in NFS RPC service tuple
694 * Returns 1 and fills in both @pmap structs if the requested service
695 * ports are unambiguous and pingable. Otherwise zero is returned;
696 * rpccreateerr.cf_stat is set to reflect the nature of the error.
698 int nfs_probe_bothports(const struct sockaddr *mnt_saddr,
699 const socklen_t mnt_salen,
700 struct pmap *mnt_pmap,
701 const struct sockaddr *nfs_saddr,
702 const socklen_t nfs_salen,
703 struct pmap *nfs_pmap)
705 struct pmap save_nfs, save_mnt;
706 const unsigned long *probe_vers;
708 if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
709 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
710 else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
711 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
713 if (nfs_pmap->pm_vers)
714 return nfs_probe_version_fixed(mnt_saddr, mnt_salen, mnt_pmap,
715 nfs_saddr, nfs_salen, nfs_pmap);
717 memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
718 memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
719 probe_vers = (nfs_mount_data_version >= 4) ?
720 probe_mnt3_first : probe_mnt1_first;
722 for (; *probe_vers; probe_vers++) {
723 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
724 if (nfs_probe_nfsport(nfs_saddr, nfs_salen, nfs_pmap) != 0) {
725 mnt_pmap->pm_vers = *probe_vers;
726 if (nfs_probe_mntport(mnt_saddr, mnt_salen, mnt_pmap) != 0)
728 memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
730 switch (rpc_createerr.cf_stat) {
731 case RPC_PROGVERSMISMATCH:
732 case RPC_PROGNOTREGISTERED:
737 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
744 * probe_bothports - discover the RPC endpoints of mountd and NFS server
745 * @mnt_server: pointer to address and pmap argument for mountd results
746 * @nfs_server: pointer to address and pmap argument for NFS server
748 * This is the legacy API that takes "clnt_addr_t" for both servers,
749 * but supports only AF_INET addresses.
751 * Returns 1 and fills in the pmap field in both clnt_addr_t structs
752 * if the requested service ports are unambiguous and pingable.
753 * Otherwise zero is returned; rpccreateerr.cf_stat is set to reflect
754 * the nature of the error.
756 int probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
758 return nfs_probe_bothports((struct sockaddr *)&mnt_server->saddr,
759 sizeof(mnt_server->saddr),
761 (struct sockaddr *)&nfs_server->saddr,
762 sizeof(nfs_server->saddr),
766 static int nfs_probe_statd(void)
768 struct sockaddr_in addr = {
769 .sin_family = AF_INET,
770 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
772 rpcprog_t program = nfs_getrpcbyname(NSMPROG, nfs_ns_pgmtbl);
774 return nfs_getport_ping((struct sockaddr *)&addr, sizeof(addr),
775 program, (rpcvers_t)1, IPPROTO_UDP);
779 * start_statd - attempt to start rpc.statd
781 * Returns 1 if statd is running; otherwise zero.
783 int start_statd(void)
789 if (nfs_probe_statd())
793 if (stat(START_STATD, &stb) == 0) {
794 if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {
798 execl(START_STATD, START_STATD, NULL);
801 nfs_error(_("fork failed: %s"),
804 default: /* parent */
805 waitpid(pid, NULL,0);
808 if (nfs_probe_statd())
818 * nfs_advise_umount - ask the server to remove a share from it's rmtab
819 * @sap: pointer to IP address of server to call
820 * @salen: length of server address
821 * @pmap: partially filled-in mountd RPC service tuple
822 * @argp: directory path of share to "unmount"
824 * Returns one if the unmount call succeeded; zero if the unmount
825 * failed for any reason; rpccreateerr.cf_stat is set to reflect
826 * the nature of the error.
828 * We use a fast timeout since this call is advisory only.
830 int nfs_advise_umount(const struct sockaddr *sap, const socklen_t salen,
831 const struct pmap *pmap, const dirpath *argp)
833 struct sockaddr_storage address;
834 struct sockaddr *saddr = (struct sockaddr *)&address;
835 struct pmap mnt_pmap = *pmap;
836 struct timeval timeout = {
837 .tv_sec = MOUNT_TIMEOUT >> 3,
840 enum clnt_stat res = 0;
842 memcpy(saddr, sap, salen);
843 if (nfs_probe_mntport(saddr, salen, &mnt_pmap) == 0) {
845 nfs_error(_("%s: Failed to discover mountd port%s"),
846 progname, clnt_spcreateerror(""));
849 nfs_set_port(saddr, mnt_pmap.pm_port);
851 client = nfs_get_priv_rpcclient(saddr, salen, mnt_pmap.pm_prot,
852 mnt_pmap.pm_prog, mnt_pmap.pm_vers,
854 if (client == NULL) {
856 nfs_error(_("%s: Failed to create RPC client%s"),
857 progname, clnt_spcreateerror(""));
861 client->cl_auth = authunix_create_default();
863 res = CLNT_CALL(client, MOUNTPROC_UMNT,
864 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
865 (xdrproc_t)xdr_void, NULL,
867 if (verbose && res != RPC_SUCCESS)
868 nfs_error(_("%s: UMNT call failed: %s"),
869 progname, clnt_sperrno(res));
871 auth_destroy(client->cl_auth);
872 CLNT_DESTROY(client);
874 if (res != RPC_SUCCESS)
880 * nfs_call_umount - ask the server to remove a share from it's rmtab
881 * @mnt_server: address of RPC MNT program server
882 * @argp: directory path of share to "unmount"
884 * Returns one if the unmount call succeeded; zero if the unmount
885 * failed for any reason.
887 * Note that a side effect of calling this function is that rpccreateerr
890 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
892 struct sockaddr *sap = (struct sockaddr *)&mnt_server->saddr;
893 socklen_t salen = sizeof(mnt_server->saddr);
894 struct pmap *pmap = &mnt_server->pmap;
896 enum clnt_stat res = 0;
899 if (!nfs_probe_mntport(sap, salen, pmap))
901 clnt = mnt_openclnt(mnt_server, &msock);
904 res = clnt_call(clnt, MOUNTPROC_UMNT,
905 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
906 (xdrproc_t)xdr_void, NULL,
908 mnt_closeclnt(clnt, msock);
910 if (res == RPC_SUCCESS)
916 * mnt_openclnt - get a handle for a remote mountd service
917 * @mnt_server: address and pmap arguments of mountd service
918 * @msock: returns a file descriptor of the underlying transport socket
920 * Returns an active handle for the remote's mountd service
922 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
924 struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
925 struct pmap *mnt_pmap = &mnt_server->pmap;
928 mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
929 *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, MOUNT_TIMEOUT,
931 if (*msock == RPC_ANYSOCK) {
932 if (rpc_createerr.cf_error.re_errno == EADDRINUSE)
934 * Probably in-use by a TIME_WAIT connection,
935 * It is worth waiting a while and trying again.
937 rpc_createerr.cf_stat = RPC_TIMEDOUT;
941 switch (mnt_pmap->pm_prot) {
943 clnt = clntudp_bufcreate(mnt_saddr,
944 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
945 RETRY_TIMEOUT, msock,
946 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
949 clnt = clnttcp_create(mnt_saddr,
950 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
952 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
956 /* try to mount hostname:dirname */
957 clnt->cl_auth = authunix_create_default();
964 * mnt_closeclnt - terminate a handle for a remote mountd service
965 * @clnt: pointer to an active handle for a remote mountd service
966 * @msock: file descriptor of the underlying transport socket
969 void mnt_closeclnt(CLIENT *clnt, int msock)
971 auth_destroy(clnt->cl_auth);
977 * clnt_ping - send an RPC ping to the remote RPC service endpoint
978 * @saddr: server's address
979 * @prog: target RPC program number
980 * @vers: target RPC version number
981 * @prot: target RPC protocol
982 * @caddr: filled in with our network address
984 * Sigh... GETPORT queries don't actually check the version number.
985 * In order to make sure that the server actually supports the service
986 * we're requesting, we open an RPC client, and fire off a NULL
989 * caddr is the network address that the server will use to call us back.
990 * On multi-homed clients, this address depends on which NIC we use to
991 * route requests to the server.
993 * Returns one if successful, otherwise zero.
995 int clnt_ping(struct sockaddr_in *saddr, const unsigned long prog,
996 const unsigned long vers, const unsigned int prot,
997 struct sockaddr_in *caddr)
1001 static char clnt_res;
1002 struct sockaddr dissolve;
1004 rpc_createerr.cf_stat = stat = 0;
1005 sock = get_socket(saddr, prot, CONNECT_TIMEOUT, FALSE, TRUE);
1006 if (sock == RPC_ANYSOCK) {
1007 if (rpc_createerr.cf_error.re_errno == ETIMEDOUT) {
1009 * TCP timeout. Bubble up the error to see
1010 * how it should be handled.
1012 rpc_createerr.cf_stat = RPC_TIMEDOUT;
1018 /* Get the address of our end of this connection */
1019 socklen_t len = sizeof(*caddr);
1020 if (getsockname(sock, caddr, &len) != 0)
1021 caddr->sin_family = 0;
1026 /* The socket is connected (so we could getsockname successfully),
1027 * but some servers on multi-homed hosts reply from
1028 * the wrong address, so if we stay connected, we lose the reply.
1030 dissolve.sa_family = AF_UNSPEC;
1031 connect(sock, &dissolve, sizeof(dissolve));
1033 clnt = clntudp_bufcreate(saddr, prog, vers,
1034 RETRY_TIMEOUT, &sock,
1035 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
1038 clnt = clnttcp_create(saddr, prog, vers, &sock,
1039 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
1046 memset(&clnt_res, 0, sizeof(clnt_res));
1047 stat = clnt_call(clnt, NULLPROC,
1048 (xdrproc_t)xdr_void, (caddr_t)NULL,
1049 (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
1052 clnt_geterr(clnt, &rpc_createerr.cf_error);
1053 rpc_createerr.cf_stat = stat;
1058 if (stat == RPC_SUCCESS)
1065 * Try a getsockname() on a connected datagram socket.
1067 * Returns 1 and fills in @buf if successful; otherwise, zero.
1069 * A connected datagram socket prevents leaving a socket in TIME_WAIT.
1070 * This conserves the ephemeral port number space, helping reduce failed
1071 * socket binds during mount storms.
1073 static int nfs_ca_sockname(const struct sockaddr *sap, const socklen_t salen,
1074 struct sockaddr *buf, socklen_t *buflen)
1076 struct sockaddr_in sin = {
1077 .sin_family = AF_INET,
1078 .sin_addr.s_addr = htonl(INADDR_ANY),
1080 struct sockaddr_in6 sin6 = {
1081 .sin6_family = AF_INET6,
1082 .sin6_addr = IN6ADDR_ANY_INIT,
1086 sock = socket(sap->sa_family, SOCK_DGRAM, IPPROTO_UDP);
1090 switch (sap->sa_family) {
1092 if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
1098 if (bind(sock, (struct sockaddr *)&sin6, sizeof(sin6)) < 0) {
1104 errno = EAFNOSUPPORT;
1108 if (connect(sock, sap, salen) < 0) {
1113 return !getsockname(sock, buf, buflen);
1117 * Try to generate an address that prevents the server from calling us.
1119 * Returns 1 and fills in @buf if successful; otherwise, zero.
1121 static int nfs_ca_gai(const struct sockaddr *sap, const socklen_t salen,
1122 struct sockaddr *buf, socklen_t *buflen)
1124 struct addrinfo *gai_results;
1125 struct addrinfo gai_hint = {
1126 .ai_family = sap->sa_family,
1127 .ai_flags = AI_PASSIVE, /* ANYADDR */
1130 if (getaddrinfo(NULL, "", &gai_hint, &gai_results))
1133 *buflen = gai_results->ai_addrlen;
1134 memcpy(buf, gai_results->ai_addr, *buflen);
1136 freeaddrinfo(gai_results);
1142 * nfs_callback_address - acquire our local network address
1143 * @sap: pointer to address of remote
1144 * @sap_len: length of address
1145 * @buf: pointer to buffer to be filled in with local network address
1146 * @buflen: IN: length of buffer to fill in; OUT: length of filled-in address
1148 * Discover a network address that an NFSv4 server can use to call us back.
1149 * On multi-homed clients, this address depends on which NIC we use to
1150 * route requests to the server.
1152 * Returns 1 and fills in @buf if an unambiguous local address is
1153 * available; returns 1 and fills in an appropriate ANYADDR address
1154 * if a local address isn't available; otherwise, returns zero.
1156 int nfs_callback_address(const struct sockaddr *sap, const socklen_t salen,
1157 struct sockaddr *buf, socklen_t *buflen)
1159 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1161 if (nfs_ca_sockname(sap, salen, buf, buflen) == 0)
1162 if (nfs_ca_gai(sap, salen, buf, buflen) == 0)
1166 * The server can't use an interface ID that was generated
1167 * here on the client, so always clear sin6_scope_id.
1169 if (sin6->sin6_family == AF_INET6)
1170 sin6->sin6_scope_id = 0;
1177 nfs_error(_("%s: failed to construct callback address"));
1182 * "nfsprog" is supported only by the legacy mount command. The
1183 * kernel mount client does not support this option.
1185 * Returns TRUE if @program contains a valid value for this option,
1186 * or FALSE if the option was specified with an invalid value.
1189 nfs_nfs_program(struct mount_options *options, unsigned long *program)
1193 switch (po_get_numeric(options, "nfsprog", &tmp)) {
1206 * NFS RPC program wasn't specified. The RPC program
1207 * cannot be determined via an rpcbind query.
1209 *program = nfs_getrpcbyname(NFSPROG, nfs_nfs_pgmtbl);
1214 * Returns TRUE if @version contains a valid value for this option,
1215 * or FALSE if the option was specified with an invalid value.
1218 nfs_nfs_version(struct mount_options *options, unsigned long *version)
1222 switch (po_rightmost(options, nfs_version_opttbl)) {
1230 switch (po_get_numeric(options, "vers", &tmp)) {
1232 if (tmp >= 2 && tmp <= 3) {
1238 nfs_error(_("%s: option parsing error\n"),
1243 case 3: /* nfsvers */
1244 switch (po_get_numeric(options, "nfsvers", &tmp)) {
1246 if (tmp >= 2 && tmp <= 3) {
1252 nfs_error(_("%s: option parsing error\n"),
1260 * NFS version wasn't specified. The pmap version value
1261 * will be filled in later by an rpcbind query in this case.
1268 * Returns TRUE if @protocol contains a valid value for this option,
1269 * or FALSE if the option was specified with an invalid value.
1272 nfs_nfs_protocol(struct mount_options *options, unsigned long *protocol)
1276 switch (po_rightmost(options, nfs_transport_opttbl)) {
1278 *protocol = IPPROTO_UDP;
1281 *protocol = IPPROTO_TCP;
1284 option = po_get(options, "proto");
1286 if (strcmp(option, "tcp") == 0) {
1287 *protocol = IPPROTO_TCP;
1290 if (strcmp(option, "udp") == 0) {
1291 *protocol = IPPROTO_UDP;
1299 * NFS transport protocol wasn't specified. The pmap
1300 * protocol value will be filled in later by an rpcbind
1301 * query in this case.
1308 * Returns TRUE if @port contains a valid value for this option,
1309 * or FALSE if the option was specified with an invalid value.
1312 nfs_nfs_port(struct mount_options *options, unsigned long *port)
1316 switch (po_get_numeric(options, "port", &tmp)) {
1320 if (tmp >= 1 && tmp <= 65535) {
1329 * NFS service port wasn't specified. The pmap port value
1330 * will be filled in later by an rpcbind query in this case.
1337 * "mountprog" is supported only by the legacy mount command. The
1338 * kernel mount client does not support this option.
1340 * Returns TRUE if @program contains a valid value for this option,
1341 * or FALSE if the option was specified with an invalid value.
1344 nfs_mount_program(struct mount_options *options, unsigned long *program)
1348 switch (po_get_numeric(options, "mountprog", &tmp)) {
1361 * MNT RPC program wasn't specified. The RPC program
1362 * cannot be determined via an rpcbind query.
1364 *program = nfs_getrpcbyname(MOUNTPROG, nfs_mnt_pgmtbl);
1369 * Returns TRUE if @version contains a valid value for this option,
1370 * or FALSE if the option was specified with an invalid value.
1373 nfs_mount_version(struct mount_options *options, unsigned long *version)
1377 switch (po_get_numeric(options, "mountvers", &tmp)) {
1381 if (tmp >= 1 && tmp <= 4) {
1390 * MNT version wasn't specified. The pmap version value
1391 * will be filled in later by an rpcbind query in this case.
1398 * Returns TRUE if @protocol contains a valid value for this option,
1399 * or FALSE if the option was specified with an invalid value.
1402 nfs_mount_protocol(struct mount_options *options, unsigned long *protocol)
1406 option = po_get(options, "mountproto");
1408 if (strcmp(option, "tcp") == 0) {
1409 *protocol = IPPROTO_TCP;
1412 if (strcmp(option, "udp") == 0) {
1413 *protocol = IPPROTO_UDP;
1420 * MNT transport protocol wasn't specified. If the NFS
1421 * transport protocol was specified, use that; otherwise
1422 * set @protocol to zero. The pmap protocol value will
1423 * be filled in later by an rpcbind query in this case.
1425 return nfs_nfs_protocol(options, protocol);
1429 * Returns TRUE if @port contains a valid value for this option,
1430 * or FALSE if the option was specified with an invalid value.
1433 nfs_mount_port(struct mount_options *options, unsigned long *port)
1437 switch (po_get_numeric(options, "mountport", &tmp)) {
1441 if (tmp >= 1 && tmp <= 65535) {
1450 * MNT service port wasn't specified. The pmap port value
1451 * will be filled in later by an rpcbind query in this case.
1458 * nfs_options2pmap - set up pmap structs based on mount options
1459 * @options: pointer to mount options
1460 * @nfs_pmap: OUT: pointer to pmap arguments for NFS server
1461 * @mnt_pmap: OUT: pointer to pmap arguments for mountd server
1463 * Returns TRUE if the pmap options specified in @options have valid
1464 * values; otherwise FALSE is returned.
1466 int nfs_options2pmap(struct mount_options *options,
1467 struct pmap *nfs_pmap, struct pmap *mnt_pmap)
1469 if (!nfs_nfs_program(options, &nfs_pmap->pm_prog))
1471 if (!nfs_nfs_version(options, &nfs_pmap->pm_vers))
1473 if (!nfs_nfs_protocol(options, &nfs_pmap->pm_prot))
1475 if (!nfs_nfs_port(options, &nfs_pmap->pm_port))
1478 if (!nfs_mount_program(options, &mnt_pmap->pm_prog))
1480 if (!nfs_mount_version(options, &mnt_pmap->pm_vers))
1482 if (!nfs_mount_protocol(options, &mnt_pmap->pm_prot))
1484 if (!nfs_mount_port(options, &mnt_pmap->pm_port))