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 #ifdef HAVE_DECL_AI_ADDRCONFIG
190 * nfs_name_to_address - resolve hostname to an IPv4 or IPv6 socket address
191 * @hostname: pointer to C string containing DNS hostname to resolve
192 * @af_hint: hint to restrict resolution to one address family
193 * @sap: pointer to buffer to fill with socket address
194 * @len: IN: size of buffer to fill; OUT: size of socket address
196 * Returns 1 and places a socket address at @sap if successful;
199 int nfs_name_to_address(const char *hostname,
200 const sa_family_t af_hint,
201 struct sockaddr *sap, socklen_t *salen)
203 struct addrinfo *gai_results;
204 struct addrinfo gai_hint = {
205 .ai_family = af_hint,
206 .ai_flags = AI_ADDRCONFIG,
208 socklen_t len = *salen;
211 if (af_hint == AF_INET6)
212 gai_hint.ai_flags |= AI_V4MAPPED|AI_ALL;
216 error = getaddrinfo(hostname, NULL, &gai_hint, &gai_results);
218 nfs_error(_("%s: DNS resolution failed for %s: %s"),
219 progname, hostname, (error == EAI_SYSTEM ?
220 strerror(errno) : gai_strerror(error)));
224 switch (gai_results->ai_addr->sa_family) {
227 if (len >= gai_results->ai_addrlen) {
228 *salen = gai_results->ai_addrlen;
229 memcpy(sap, gai_results->ai_addr, *salen);
234 /* things are really broken if we get here, so warn */
235 nfs_error(_("%s: unrecognized DNS resolution results for %s"),
240 freeaddrinfo(gai_results);
243 #else /* HAVE_DECL_AI_ADDRCONFIG */
245 * nfs_name_to_address - resolve hostname to an IPv4 socket address
246 * @hostname: pointer to C string containing DNS hostname to resolve
247 * @af_hint: hint to restrict resolution to one address family
248 * @sap: pointer to buffer to fill with socket address
249 * @len: IN: size of buffer to fill; OUT: size of socket address
251 * Returns 1 and places a socket address at @sap if successful;
254 * Some older getaddrinfo(3) implementations don't support
255 * AI_ADDRCONFIG or AI_V4MAPPED properly. For those cases, a DNS
256 * resolver based on the traditional gethostbyname(3) is provided.
258 int nfs_name_to_address(const char *hostname,
259 const sa_family_t af_hint,
260 struct sockaddr *sap, socklen_t *salen)
262 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
263 socklen_t len = *salen;
268 if (af_hint != AF_INET) {
269 nfs_error(_("%s: address family not supported by DNS resolver\n"),
274 sin->sin_family = AF_INET;
275 if (inet_aton(hostname, &sin->sin_addr)) {
276 *salen = sizeof(*sin);
280 hp = gethostbyname(hostname);
282 nfs_error(_("%s: DNS resolution failed for %s: %s"),
283 progname, hostname, hstrerror(h_errno));
287 if (hp->h_length > len) {
288 nfs_error(_("%s: DNS resolution results too long for buffer\n"),
293 memcpy(&sin->sin_addr, hp->h_addr, hp->h_length);
294 *salen = sizeof(struct sockaddr_in);
297 #endif /* HAVE_DECL_AI_ADDRCONFIG */
300 * nfs_gethostbyname - resolve a hostname to an IPv4 address
301 * @hostname: pointer to a C string containing a DNS hostname
302 * @sin: returns an IPv4 address
304 * Returns 1 if successful, otherwise zero.
306 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *sin)
308 socklen_t len = sizeof(*sin);
310 return nfs_name_to_address(hostname, AF_INET,
311 (struct sockaddr *)sin, &len);
315 * nfs_string_to_sockaddr - convert string address to sockaddr
316 * @address: pointer to presentation format address to convert
317 * @addrlen: length of presentation address
318 * @sap: pointer to socket address buffer to fill in
319 * @salen: IN: length of address buffer
320 * OUT: length of converted socket address
322 * Convert a presentation format address string to a socket address.
323 * Similar to nfs_name_to_address(), but the DNS query is squelched,
324 * and won't make any noise if the getaddrinfo() call fails.
326 * Returns 1 and fills in @sap and @salen if successful; otherwise zero.
328 * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
329 * on presenting IPv6 addresses as text strings.
331 int nfs_string_to_sockaddr(const char *address, const size_t addrlen,
332 struct sockaddr *sap, socklen_t *salen)
334 struct addrinfo *gai_results;
335 struct addrinfo gai_hint = {
336 .ai_flags = AI_NUMERICHOST,
338 socklen_t len = *salen;
343 if (getaddrinfo(address, NULL, &gai_hint, &gai_results) == 0) {
344 switch (gai_results->ai_addr->sa_family) {
347 if (len >= gai_results->ai_addrlen) {
348 *salen = gai_results->ai_addrlen;
349 memcpy(sap, gai_results->ai_addr, *salen);
354 freeaddrinfo(gai_results);
361 * nfs_present_sockaddr - convert sockaddr to string
362 * @sap: pointer to socket address to convert
363 * @salen: length of socket address
364 * @buf: pointer to buffer to fill in
365 * @buflen: length of buffer
367 * Convert the passed-in sockaddr-style address to presentation format.
368 * The presentation format address is placed in @buf and is
371 * Returns 1 if successful; otherwise zero.
373 * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
374 * on presenting IPv6 addresses as text strings.
376 int nfs_present_sockaddr(const struct sockaddr *sap, const socklen_t salen,
377 char *buf, const size_t buflen)
379 #ifdef HAVE_GETNAMEINFO
382 result = getnameinfo(sap, salen, buf, buflen,
383 NULL, 0, NI_NUMERICHOST);
387 nfs_error(_("%s: invalid server address: %s"), progname,
388 gai_strerror(result));
390 #else /* HAVE_GETNAMEINFO */
393 if (sap->sa_family == AF_INET) {
394 addr = inet_ntoa(((struct sockaddr_in *)sap)->sin_addr);
395 if (addr && strlen(addr) < buflen) {
401 nfs_error(_("%s: invalid server address"), progname);
403 #endif /* HAVE_GETNAMEINFO */
407 * Attempt to connect a socket, but time out after "timeout" seconds.
409 * On error return, caller closes the socket.
411 static int connect_to(int fd, struct sockaddr *addr,
412 socklen_t addrlen, int timeout)
416 struct timeval tv = {
420 saved = fcntl(fd, F_GETFL, 0);
421 fcntl(fd, F_SETFL, saved | O_NONBLOCK);
423 ret = connect(fd, addr, addrlen);
424 if (ret < 0 && errno != EINPROGRESS)
432 ret = select(fd + 1, &rset, &wset, NULL, &tv);
437 if (FD_ISSET(fd, &rset) || FD_ISSET(fd, &wset)) {
439 socklen_t len = sizeof(error);
440 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
450 fcntl(fd, F_SETFL, saved);
455 * Create a socket that is locally bound to a reserved or non-reserved port.
457 * The caller should check rpc_createerr to determine the cause of any error.
459 static int get_socket(struct sockaddr_in *saddr, unsigned int p_prot,
460 unsigned int timeout, int resvp, int conn)
463 struct sockaddr_in laddr;
464 socklen_t namelen = sizeof(laddr);
466 type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
467 if ((so = socket (AF_INET, type, p_prot)) < 0)
470 laddr.sin_family = AF_INET;
472 laddr.sin_addr.s_addr = htonl(INADDR_ANY);
474 if (bindresvport(so, &laddr) < 0)
475 goto err_bindresvport;
477 cc = bind(so, (struct sockaddr *)&laddr, namelen);
481 if (type == SOCK_STREAM || (conn && type == SOCK_DGRAM)) {
482 cc = connect_to(so, (struct sockaddr *)saddr, namelen,
490 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
491 rpc_createerr.cf_error.re_errno = errno;
493 nfs_error(_("%s: Unable to create %s socket: errno %d (%s)\n"),
494 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
495 errno, strerror(errno));
500 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
501 rpc_createerr.cf_error.re_errno = errno;
503 nfs_error(_("%s: Unable to bindresvport %s socket: errno %d"
505 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
506 errno, strerror(errno));
512 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
513 rpc_createerr.cf_error.re_errno = errno;
515 nfs_error(_("%s: Unable to bind to %s socket: errno %d (%s)\n"),
516 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
517 errno, strerror(errno));
523 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
524 rpc_createerr.cf_error.re_errno = errno;
526 nfs_error(_("%s: Unable to connect to %s:%d, errno %d (%s)\n"),
527 progname, inet_ntoa(saddr->sin_addr),
528 ntohs(saddr->sin_port), errno, strerror(errno));
534 static void nfs_pp_debug(const struct sockaddr *sap, const socklen_t salen,
535 const rpcprog_t program, const rpcvers_t version,
536 const unsigned short protocol,
537 const unsigned short port)
539 char buf[NI_MAXHOST];
544 if (nfs_present_sockaddr(sap, salen, buf, sizeof(buf)) == 0) {
546 strcat(buf, "unknown host");
549 fprintf(stderr, _("%s: trying %s prog %ld vers %ld prot %s port %d\n"),
550 progname, buf, program, version,
551 (protocol == IPPROTO_UDP ? _("UDP") : _("TCP")),
556 * Use the portmapper to discover whether or not the service we want is
557 * available. The lists 'versions' and 'protos' define ordered sequences
558 * of service versions and udp/tcp protocols to probe for.
560 * Returns 1 if the requested service port is unambiguous and pingable;
561 * @pmap is filled in with the version, port, and transport protocol used
562 * during the successful ping. Note that if a port is already specified
563 * in @pmap and it matches the rpcbind query result, nfs_probe_port() does
564 * not perform an RPC ping.
566 * If an error occurs or the requested service isn't available, zero is
567 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
569 static int nfs_probe_port(const struct sockaddr *sap, const socklen_t salen,
570 struct pmap *pmap, const unsigned long *versions,
571 const unsigned int *protos)
573 struct sockaddr_storage address;
574 struct sockaddr *saddr = (struct sockaddr *)&address;
575 const unsigned long prog = pmap->pm_prog, *p_vers;
576 const unsigned int prot = (u_int)pmap->pm_prot, *p_prot;
577 const u_short port = (u_short) pmap->pm_port;
578 unsigned long vers = pmap->pm_vers;
579 unsigned short p_port;
581 memcpy(saddr, sap, salen);
582 p_prot = prot ? &prot : protos;
583 p_vers = vers ? &vers : versions;
584 rpc_createerr.cf_stat = 0;
587 p_port = nfs_getport(saddr, salen, prog, *p_vers, *p_prot);
589 if (!port || port == p_port) {
590 nfs_set_port(saddr, p_port);
591 nfs_pp_debug(saddr, salen, prog, *p_vers,
593 if (nfs_rpc_ping(saddr, salen, prog,
594 *p_vers, *p_prot, NULL))
598 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
599 rpc_createerr.cf_stat != RPC_TIMEDOUT &&
600 rpc_createerr.cf_stat != RPC_CANTRECV &&
601 rpc_createerr.cf_stat != RPC_PROGVERSMISMATCH)
609 if (rpc_createerr.cf_stat == RPC_TIMEDOUT ||
610 rpc_createerr.cf_stat == RPC_CANTRECV)
613 if (vers || !*++p_vers)
622 pmap->pm_vers = *p_vers;
624 pmap->pm_prot = *p_prot;
626 pmap->pm_port = p_port;
627 rpc_createerr.cf_stat = 0;
632 * Probe a server's NFS service to determine which versions and
633 * transport protocols are supported.
635 * Returns 1 if the requested service port is unambiguous and pingable;
636 * @pmap is filled in with the version, port, and transport protocol used
637 * during the successful ping. If all three are already specified, simply
638 * return success without an rpcbind query or RPC ping (we may be trying
639 * to mount an NFS service that is not advertised via rpcbind).
641 * If an error occurs or the requested service isn't available, zero is
642 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
644 static int nfs_probe_nfsport(const struct sockaddr *sap, const socklen_t salen,
647 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
650 if (nfs_mount_data_version >= 4)
651 return nfs_probe_port(sap, salen, pmap,
652 probe_nfs3_first, probe_tcp_first);
654 return nfs_probe_port(sap, salen, pmap,
655 probe_nfs2_only, probe_udp_only);
659 * Probe a server's mountd service to determine which versions and
660 * transport protocols are supported.
662 * Returns 1 if the requested service port is unambiguous and pingable;
663 * @pmap is filled in with the version, port, and transport protocol used
664 * during the successful ping. If all three are already specified, simply
665 * return success without an rpcbind query or RPC ping (we may be trying
666 * to mount an NFS service that is not advertised via rpcbind).
668 * If an error occurs or the requested service isn't available, zero is
669 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
671 static int nfs_probe_mntport(const struct sockaddr *sap, const socklen_t salen,
674 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
677 if (nfs_mount_data_version >= 4)
678 return nfs_probe_port(sap, salen, pmap,
679 probe_mnt3_first, probe_udp_first);
681 return nfs_probe_port(sap, salen, pmap,
682 probe_mnt1_first, probe_udp_only);
686 * Probe a server's mountd service to determine which versions and
687 * transport protocols are supported. Invoked when the protocol
688 * version is already known for both the NFS and mountd service.
690 * Returns 1 and fills in both @pmap structs if the requested service
691 * ports are unambiguous and pingable. Otherwise zero is returned;
692 * rpccreateerr.cf_stat is set to reflect the nature of the error.
694 static int nfs_probe_version_fixed(const struct sockaddr *mnt_saddr,
695 const socklen_t mnt_salen,
696 struct pmap *mnt_pmap,
697 const struct sockaddr *nfs_saddr,
698 const socklen_t nfs_salen,
699 struct pmap *nfs_pmap)
701 if (!nfs_probe_nfsport(nfs_saddr, nfs_salen, nfs_pmap))
703 return nfs_probe_mntport(mnt_saddr, mnt_salen, mnt_pmap);
707 * nfs_probe_bothports - discover the RPC endpoints of mountd and NFS server
708 * @mnt_saddr: pointer to socket address of mountd server
709 * @mnt_salen: length of mountd server's address
710 * @mnt_pmap: IN: partially filled-in mountd RPC service tuple;
711 * OUT: fully filled-in mountd RPC service tuple
712 * @nfs_saddr: pointer to socket address of NFS server
713 * @nfs_salen: length of NFS server's address
714 * @nfs_pmap: IN: partially filled-in NFS RPC service tuple;
715 * OUT: fully filled-in NFS RPC service tuple
717 * Returns 1 and fills in both @pmap structs if the requested service
718 * ports are unambiguous and pingable. Otherwise zero is returned;
719 * rpccreateerr.cf_stat is set to reflect the nature of the error.
721 int nfs_probe_bothports(const struct sockaddr *mnt_saddr,
722 const socklen_t mnt_salen,
723 struct pmap *mnt_pmap,
724 const struct sockaddr *nfs_saddr,
725 const socklen_t nfs_salen,
726 struct pmap *nfs_pmap)
728 struct pmap save_nfs, save_mnt;
729 const unsigned long *probe_vers;
731 if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
732 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
733 else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
734 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
736 if (nfs_pmap->pm_vers)
737 return nfs_probe_version_fixed(mnt_saddr, mnt_salen, mnt_pmap,
738 nfs_saddr, nfs_salen, nfs_pmap);
740 memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
741 memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
742 probe_vers = (nfs_mount_data_version >= 4) ?
743 probe_mnt3_first : probe_mnt1_first;
745 for (; *probe_vers; probe_vers++) {
746 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
747 if (nfs_probe_nfsport(nfs_saddr, nfs_salen, nfs_pmap) != 0) {
748 mnt_pmap->pm_vers = *probe_vers;
749 if (nfs_probe_mntport(mnt_saddr, mnt_salen, mnt_pmap) != 0)
751 memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
753 switch (rpc_createerr.cf_stat) {
754 case RPC_PROGVERSMISMATCH:
755 case RPC_PROGNOTREGISTERED:
760 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
767 * probe_bothports - discover the RPC endpoints of mountd and NFS server
768 * @mnt_server: pointer to address and pmap argument for mountd results
769 * @nfs_server: pointer to address and pmap argument for NFS server
771 * This is the legacy API that takes "clnt_addr_t" for both servers,
772 * but supports only AF_INET addresses.
774 * Returns 1 and fills in the pmap field in both clnt_addr_t structs
775 * if the requested service ports are unambiguous and pingable.
776 * Otherwise zero is returned; rpccreateerr.cf_stat is set to reflect
777 * the nature of the error.
779 int probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
781 return nfs_probe_bothports((struct sockaddr *)&mnt_server->saddr,
782 sizeof(mnt_server->saddr),
784 (struct sockaddr *)&nfs_server->saddr,
785 sizeof(nfs_server->saddr),
789 static int nfs_probe_statd(void)
791 struct sockaddr_in addr = {
792 .sin_family = AF_INET,
793 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
795 rpcprog_t program = nfs_getrpcbyname(NSMPROG, nfs_ns_pgmtbl);
797 return nfs_getport_ping((struct sockaddr *)&addr, sizeof(addr),
798 program, (rpcvers_t)1, IPPROTO_UDP);
802 * start_statd - attempt to start rpc.statd
804 * Returns 1 if statd is running; otherwise zero.
806 int start_statd(void)
812 if (nfs_probe_statd())
816 if (stat(START_STATD, &stb) == 0) {
817 if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {
821 execl(START_STATD, START_STATD, NULL);
824 nfs_error(_("fork failed: %s"),
827 default: /* parent */
828 waitpid(pid, NULL,0);
831 if (nfs_probe_statd())
841 * nfs_advise_umount - ask the server to remove a share from it's rmtab
842 * @sap: pointer to IP address of server to call
843 * @salen: length of server address
844 * @pmap: partially filled-in mountd RPC service tuple
845 * @argp: directory path of share to "unmount"
847 * Returns one if the unmount call succeeded; zero if the unmount
848 * failed for any reason; rpccreateerr.cf_stat is set to reflect
849 * the nature of the error.
851 * We use a fast timeout since this call is advisory only.
853 int nfs_advise_umount(const struct sockaddr *sap, const socklen_t salen,
854 const struct pmap *pmap, const dirpath *argp)
856 struct sockaddr_storage address;
857 struct sockaddr *saddr = (struct sockaddr *)&address;
858 struct pmap mnt_pmap = *pmap;
859 struct timeval timeout = {
860 .tv_sec = MOUNT_TIMEOUT >> 3,
863 enum clnt_stat res = 0;
865 if (nfs_probe_mntport(sap, salen, &mnt_pmap) == 0)
868 memcpy(saddr, sap, salen);
869 nfs_set_port(saddr, mnt_pmap.pm_port);
871 client = nfs_get_rpcclient(saddr, salen, mnt_pmap.pm_prot,
872 mnt_pmap.pm_prog, mnt_pmap.pm_vers,
877 client->cl_auth = authunix_create_default();
879 res = CLNT_CALL(client, MOUNTPROC_UMNT,
880 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
881 (xdrproc_t)xdr_void, NULL,
884 auth_destroy(client->cl_auth);
885 CLNT_DESTROY(client);
887 if (res != RPC_SUCCESS)
894 * nfs_call_umount - ask the server to remove a share from it's rmtab
895 * @mnt_server: address of RPC MNT program server
896 * @argp: directory path of share to "unmount"
898 * Returns one if the unmount call succeeded; zero if the unmount
899 * failed for any reason.
901 * Note that a side effect of calling this function is that rpccreateerr
904 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
906 struct sockaddr *sap = (struct sockaddr *)&mnt_server->saddr;
907 socklen_t salen = sizeof(mnt_server->saddr);
908 struct pmap *pmap = &mnt_server->pmap;
910 enum clnt_stat res = 0;
913 if (!nfs_probe_mntport(sap, salen, pmap))
915 clnt = mnt_openclnt(mnt_server, &msock);
918 res = clnt_call(clnt, MOUNTPROC_UMNT,
919 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
920 (xdrproc_t)xdr_void, NULL,
922 mnt_closeclnt(clnt, msock);
924 if (res == RPC_SUCCESS)
930 * mnt_openclnt - get a handle for a remote mountd service
931 * @mnt_server: address and pmap arguments of mountd service
932 * @msock: returns a file descriptor of the underlying transport socket
934 * Returns an active handle for the remote's mountd service
936 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
938 struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
939 struct pmap *mnt_pmap = &mnt_server->pmap;
942 mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
943 *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, MOUNT_TIMEOUT,
945 if (*msock == RPC_ANYSOCK) {
946 if (rpc_createerr.cf_error.re_errno == EADDRINUSE)
948 * Probably in-use by a TIME_WAIT connection,
949 * It is worth waiting a while and trying again.
951 rpc_createerr.cf_stat = RPC_TIMEDOUT;
955 switch (mnt_pmap->pm_prot) {
957 clnt = clntudp_bufcreate(mnt_saddr,
958 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
959 RETRY_TIMEOUT, msock,
960 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
963 clnt = clnttcp_create(mnt_saddr,
964 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
966 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
970 /* try to mount hostname:dirname */
971 clnt->cl_auth = authunix_create_default();
978 * mnt_closeclnt - terminate a handle for a remote mountd service
979 * @clnt: pointer to an active handle for a remote mountd service
980 * @msock: file descriptor of the underlying transport socket
983 void mnt_closeclnt(CLIENT *clnt, int msock)
985 auth_destroy(clnt->cl_auth);
991 * clnt_ping - send an RPC ping to the remote RPC service endpoint
992 * @saddr: server's address
993 * @prog: target RPC program number
994 * @vers: target RPC version number
995 * @prot: target RPC protocol
996 * @caddr: filled in with our network address
998 * Sigh... GETPORT queries don't actually check the version number.
999 * In order to make sure that the server actually supports the service
1000 * we're requesting, we open an RPC client, and fire off a NULL
1003 * caddr is the network address that the server will use to call us back.
1004 * On multi-homed clients, this address depends on which NIC we use to
1005 * route requests to the server.
1007 * Returns one if successful, otherwise zero.
1009 int clnt_ping(struct sockaddr_in *saddr, const unsigned long prog,
1010 const unsigned long vers, const unsigned int prot,
1011 struct sockaddr_in *caddr)
1013 CLIENT *clnt = NULL;
1015 static char clnt_res;
1016 struct sockaddr dissolve;
1018 rpc_createerr.cf_stat = stat = 0;
1019 sock = get_socket(saddr, prot, CONNECT_TIMEOUT, FALSE, TRUE);
1020 if (sock == RPC_ANYSOCK) {
1021 if (rpc_createerr.cf_error.re_errno == ETIMEDOUT) {
1023 * TCP timeout. Bubble up the error to see
1024 * how it should be handled.
1026 rpc_createerr.cf_stat = RPC_TIMEDOUT;
1032 /* Get the address of our end of this connection */
1033 socklen_t len = sizeof(*caddr);
1034 if (getsockname(sock, caddr, &len) != 0)
1035 caddr->sin_family = 0;
1040 /* The socket is connected (so we could getsockname successfully),
1041 * but some servers on multi-homed hosts reply from
1042 * the wrong address, so if we stay connected, we lose the reply.
1044 dissolve.sa_family = AF_UNSPEC;
1045 connect(sock, &dissolve, sizeof(dissolve));
1047 clnt = clntudp_bufcreate(saddr, prog, vers,
1048 RETRY_TIMEOUT, &sock,
1049 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
1052 clnt = clnttcp_create(saddr, prog, vers, &sock,
1053 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
1060 memset(&clnt_res, 0, sizeof(clnt_res));
1061 stat = clnt_call(clnt, NULLPROC,
1062 (xdrproc_t)xdr_void, (caddr_t)NULL,
1063 (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
1066 clnt_geterr(clnt, &rpc_createerr.cf_error);
1067 rpc_createerr.cf_stat = stat;
1072 if (stat == RPC_SUCCESS)
1079 * Try a getsockname() on a connected datagram socket.
1081 * Returns 1 and fills in @buf if successful; otherwise, zero.
1083 * A connected datagram socket prevents leaving a socket in TIME_WAIT.
1084 * This conserves the ephemeral port number space, helping reduce failed
1085 * socket binds during mount storms.
1087 static int nfs_ca_sockname(const struct sockaddr *sap, const socklen_t salen,
1088 struct sockaddr *buf, socklen_t *buflen)
1090 struct sockaddr_in sin = {
1091 .sin_family = AF_INET,
1092 .sin_addr.s_addr = htonl(INADDR_ANY),
1094 struct sockaddr_in6 sin6 = {
1095 .sin6_family = AF_INET6,
1096 .sin6_addr = IN6ADDR_ANY_INIT,
1100 sock = socket(sap->sa_family, SOCK_DGRAM, IPPROTO_UDP);
1104 switch (sap->sa_family) {
1106 if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
1112 if (bind(sock, (struct sockaddr *)&sin6, sizeof(sin6)) < 0) {
1118 errno = EAFNOSUPPORT;
1122 if (connect(sock, sap, salen) < 0) {
1127 return !getsockname(sock, buf, buflen);
1131 * Try to generate an address that prevents the server from calling us.
1133 * Returns 1 and fills in @buf if successful; otherwise, zero.
1135 static int nfs_ca_gai(const struct sockaddr *sap, const socklen_t salen,
1136 struct sockaddr *buf, socklen_t *buflen)
1138 struct addrinfo *gai_results;
1139 struct addrinfo gai_hint = {
1140 .ai_family = sap->sa_family,
1141 .ai_flags = AI_PASSIVE, /* ANYADDR */
1144 if (getaddrinfo(NULL, "", &gai_hint, &gai_results))
1147 *buflen = gai_results->ai_addrlen;
1148 memcpy(buf, gai_results->ai_addr, *buflen);
1150 freeaddrinfo(gai_results);
1156 * nfs_callback_address - acquire our local network address
1157 * @sap: pointer to address of remote
1158 * @sap_len: length of address
1159 * @buf: pointer to buffer to be filled in with local network address
1160 * @buflen: IN: length of buffer to fill in; OUT: length of filled-in address
1162 * Discover a network address that an NFSv4 server can use to call us back.
1163 * On multi-homed clients, this address depends on which NIC we use to
1164 * route requests to the server.
1166 * Returns 1 and fills in @buf if an unambiguous local address is
1167 * available; returns 1 and fills in an appropriate ANYADDR address
1168 * if a local address isn't available; otherwise, returns zero.
1170 int nfs_callback_address(const struct sockaddr *sap, const socklen_t salen,
1171 struct sockaddr *buf, socklen_t *buflen)
1173 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1175 if (nfs_ca_sockname(sap, salen, buf, buflen) == 0)
1176 if (nfs_ca_gai(sap, salen, buf, buflen) == 0)
1180 * The server can't use an interface ID that was generated
1181 * here on the client, so always clear sin6_scope_id.
1183 if (sin6->sin6_family == AF_INET6)
1184 sin6->sin6_scope_id = 0;
1191 nfs_error(_("%s: failed to construct callback address"));
1197 * "nfsprog" is only supported by the legacy mount command. The
1198 * kernel mount client does not support this option.
1200 * Returns the value set by the nfsprog= option, the value of
1201 * the RPC NFS program specified in /etc/rpc, or a baked-in
1202 * default program number, if all fails.
1204 static rpcprog_t nfs_nfs_program(struct mount_options *options)
1208 if (po_get_numeric(options, "nfsprog", &tmp) == PO_FOUND)
1211 return nfs_getrpcbyname(NFSPROG, nfs_nfs_pgmtbl);
1216 * Returns the RPC version number specified by the given mount
1217 * options for the NFS service, or zero if all fails.
1219 static rpcvers_t nfs_nfs_version(struct mount_options *options)
1223 switch (po_rightmost(options, nfs_version_opttbl)) {
1229 if (po_get_numeric(options, "vers", &tmp) == PO_FOUND)
1230 if (tmp >= 2 && tmp <= 3)
1233 case 3: /* nfsvers */
1234 if (po_get_numeric(options, "nfsvers", &tmp) == PO_FOUND)
1235 if (tmp >= 2 && tmp <= 3)
1244 * Returns the NFS transport protocol specified by the given mount options
1246 * Returns the IPPROTO_ value specified by the given mount options, or
1247 * IPPROTO_UDP if all fails.
1249 static unsigned short nfs_nfs_protocol(struct mount_options *options)
1253 switch (po_rightmost(options, nfs_transport_opttbl)) {
1257 option = po_get(options, "proto");
1259 if (strcmp(option, "tcp") == 0)
1261 if (strcmp(option, "udp") == 0)
1270 * Returns the NFS server's port number specified by the given
1271 * mount options, or zero if all fails. Zero results in a portmap
1272 * query to discover the server's mountd service port.
1274 * port=0 will guarantee an rpcbind request precedes the first
1275 * NFS RPC so the client can determine the server's port number.
1277 static unsigned short nfs_nfs_port(struct mount_options *options)
1281 if (po_get_numeric(options, "port", &tmp) == PO_FOUND)
1282 if (tmp >= 0 && tmp <= 65535)
1288 * "mountprog" is only supported by the legacy mount command. The
1289 * kernel mount client does not support this option.
1291 * Returns the value set by the mountprog= option, the value of
1292 * the RPC mount program specified in /etc/rpc, or a baked-in
1293 * default program number, if all fails.
1295 static rpcprog_t nfs_mount_program(struct mount_options *options)
1299 if (po_get_numeric(options, "mountprog", &tmp) == PO_FOUND)
1302 return nfs_getrpcbyname(MOUNTPROG, nfs_mnt_pgmtbl);
1306 * Returns the RPC version number specified by the given mount options,
1307 * or the version "3" if all fails.
1309 static rpcvers_t nfs_mount_version(struct mount_options *options)
1313 if (po_get_numeric(options, "mountvers", &tmp) == PO_FOUND)
1314 if (tmp >= 1 && tmp <= 4)
1317 return nfsvers_to_mnt(nfs_nfs_version(options));
1321 * Returns the transport protocol to use for the mount service
1323 * Returns the IPPROTO_ value specified by the mountproto option, or
1324 * if that doesn't exist, the IPPROTO_ value specified for NFS
1327 static unsigned short nfs_mount_protocol(struct mount_options *options)
1331 option = po_get(options, "mountproto");
1333 if (strcmp(option, "tcp") == 0)
1335 if (strcmp(option, "udp") == 0)
1339 return nfs_nfs_version(options);
1343 * Returns the mountd server's port number specified by the given
1344 * mount options, or zero if all fails. Zero results in a portmap
1345 * query to discover the server's mountd service port.
1347 * port=0 will guarantee an rpcbind request precedes the mount
1348 * RPC so the client can determine the server's port number.
1350 static unsigned short nfs_mount_port(struct mount_options *options)
1354 if (po_get_numeric(options, "mountport", &tmp) == PO_FOUND)
1355 if (tmp >= 0 && tmp <= 65535)
1361 * nfs_options2pmap - set up pmap structs based on mount options
1362 * @options: pointer to mount options
1363 * @nfs_pmap: OUT: pointer to pmap arguments for NFS server
1364 * @mnt_pmap: OUT: pointer to pmap arguments for mountd server
1367 void nfs_options2pmap(struct mount_options *options,
1368 struct pmap *nfs_pmap, struct pmap *mnt_pmap)
1370 nfs_pmap->pm_prog = nfs_nfs_program(options);
1371 nfs_pmap->pm_vers = nfs_nfs_version(options);
1372 nfs_pmap->pm_prot = nfs_nfs_protocol(options);
1373 nfs_pmap->pm_port = nfs_nfs_port(options);
1375 mnt_pmap->pm_prog = nfs_mount_program(options);
1376 mnt_pmap->pm_vers = nfs_mount_version(options);
1377 mnt_pmap->pm_prot = nfs_mount_protocol(options);
1378 mnt_pmap->pm_port = nfs_mount_port(options);