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"
53 #define PMAP_TIMEOUT (10)
54 #define CONNECT_TIMEOUT (20)
55 #define MOUNT_TIMEOUT (30)
57 #if SIZEOF_SOCKLEN_T - 0 == 0
58 #define socklen_t unsigned int
61 extern int nfs_mount_data_version;
62 extern char *progname;
65 static const char *nfs_ns_pgmtbl[] = {
70 static const unsigned long nfs_to_mnt[] = {
77 static const unsigned long mnt_to_nfs[] = {
85 * Map an NFS version into the corresponding Mountd version
87 unsigned long nfsvers_to_mnt(const unsigned long vers)
90 return nfs_to_mnt[vers];
95 * Map a Mountd version into the corresponding NFS version
97 static unsigned long mntvers_to_nfs(const unsigned long vers)
100 return mnt_to_nfs[vers];
104 static const unsigned int probe_udp_only[] = {
109 static const unsigned int probe_udp_first[] = {
115 static const unsigned int probe_tcp_first[] = {
121 static const unsigned long probe_nfs2_only[] = {
126 static const unsigned long probe_nfs3_first[] = {
132 static const unsigned long probe_mnt1_first[] = {
138 static const unsigned long probe_mnt3_first[] = {
145 static void nfs_set_port(struct sockaddr *sap, const unsigned short port)
147 switch (sap->sa_family) {
149 ((struct sockaddr_in *)sap)->sin_port = htons(port);
152 ((struct sockaddr_in6 *)sap)->sin6_port = htons(port);
155 nfs_error(_("%s: unrecognized address family in %s"),
160 #ifdef HAVE_DECL_AI_ADDRCONFIG
162 * nfs_name_to_address - resolve hostname to an IPv4 or IPv6 socket address
163 * @hostname: pointer to C string containing DNS hostname to resolve
164 * @sap: pointer to buffer to fill with socket address
165 * @len: IN: size of buffer to fill; OUT: size of socket address
167 * Returns 1 and places a socket address at @sap if successful;
170 int nfs_name_to_address(const char *hostname,
171 const sa_family_t af_hint,
172 struct sockaddr *sap, socklen_t *salen)
174 struct addrinfo *gai_results;
175 struct addrinfo gai_hint = {
176 .ai_family = af_hint,
177 .ai_flags = AI_ADDRCONFIG,
179 socklen_t len = *salen;
182 if (af_hint == AF_INET6)
183 gai_hint.ai_flags |= AI_V4MAPPED|AI_ALL;
187 error = getaddrinfo(hostname, NULL, &gai_hint, &gai_results);
189 nfs_error(_("%s: DNS resolution failed for %s: %s"),
190 progname, hostname, (error == EAI_SYSTEM ?
191 strerror(errno) : gai_strerror(error)));
195 switch (gai_results->ai_addr->sa_family) {
198 if (len >= gai_results->ai_addrlen) {
199 *salen = gai_results->ai_addrlen;
200 memcpy(sap, gai_results->ai_addr, *salen);
205 /* things are really broken if we get here, so warn */
206 nfs_error(_("%s: unrecognized DNS resolution results for %s"),
211 freeaddrinfo(gai_results);
214 #else /* HAVE_DECL_AI_ADDRCONFIG */
216 * nfs_name_to_address - resolve hostname to an IPv4 socket address
217 * @hostname: pointer to C string containing DNS hostname to resolve
218 * @af_hint: hint to restrict resolution to one address family
219 * @sap: pointer to buffer to fill with socket address
220 * @len: IN: size of buffer to fill; OUT: size of socket address
222 * Returns 1 and places a socket address at @sap if successful;
225 * Some older getaddrinfo(3) implementations don't support
226 * AI_ADDRCONFIG or AI_V4MAPPED properly. For those cases, a DNS
227 * resolver based on the traditional gethostbyname(3) is provided.
229 int nfs_name_to_address(const char *hostname,
230 const sa_family_t af_hint,
231 struct sockaddr *sap, socklen_t *salen)
233 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
234 socklen_t len = *salen;
239 if (af_hint != AF_INET) {
240 nfs_error(_("%s: address family not supported by DNS resolver\n"),
245 sin->sin_family = AF_INET;
246 if (inet_aton(hostname, &sin->sin_addr)) {
247 *salen = sizeof(*sin);
251 hp = gethostbyname(hostname);
253 nfs_error(_("%s: DNS resolution failed for %s: %s"),
254 progname, hostname, hstrerror(h_errno));
258 if (hp->h_length > len) {
259 nfs_error(_("%s: DNS resolution results too long for buffer\n"),
264 memcpy(&sin->sin_addr, hp->h_addr, hp->h_length);
265 *salen = hp->h_length;
268 #endif /* HAVE_DECL_AI_ADDRCONFIG */
271 * nfs_gethostbyname - resolve a hostname to an IPv4 address
272 * @hostname: pointer to a C string containing a DNS hostname
273 * @saddr: returns an IPv4 address
275 * Returns 1 if successful, otherwise zero.
277 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *sin)
279 socklen_t len = sizeof(*sin);
281 return nfs_name_to_address(hostname, AF_INET,
282 (struct sockaddr *)sin, &len);
286 * nfs_string_to_sockaddr - convert string address to sockaddr
287 * @address: pointer to presentation format address to convert
288 * @addrlen: length of presentation address
289 * @sap: pointer to socket address buffer to fill in
290 * @salen: IN: length of address buffer
291 * OUT: length of converted socket address
293 * Convert a presentation format address string to a socket address.
294 * Similar to nfs_name_to_address(), but the DNS query is squelched,
295 * and won't make any noise if the getaddrinfo() call fails.
297 * Returns 1 and fills in @sap and @salen if successful; otherwise zero.
299 * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
300 * on presenting IPv6 addresses as text strings.
302 int nfs_string_to_sockaddr(const char *address, const size_t addrlen,
303 struct sockaddr *sap, socklen_t *salen)
305 struct addrinfo *gai_results;
306 struct addrinfo gai_hint = {
307 .ai_flags = AI_NUMERICHOST,
309 socklen_t len = *salen;
314 if (getaddrinfo(address, NULL, &gai_hint, &gai_results) == 0) {
315 switch (gai_results->ai_addr->sa_family) {
318 if (len >= gai_results->ai_addrlen) {
319 *salen = gai_results->ai_addrlen;
320 memcpy(sap, gai_results->ai_addr, *salen);
325 freeaddrinfo(gai_results);
332 * nfs_present_sockaddr - convert sockaddr to string
333 * @sap: pointer to socket address to convert
334 * @salen: length of socket address
335 * @buf: pointer to buffer to fill in
336 * @buflen: length of buffer
338 * Convert the passed-in sockaddr-style address to presentation format.
339 * The presentation format address is placed in @buf and is
342 * Returns 1 if successful; otherwise zero.
344 * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
345 * on presenting IPv6 addresses as text strings.
347 int nfs_present_sockaddr(const struct sockaddr *sap, const socklen_t salen,
348 char *buf, const size_t buflen)
350 #ifdef HAVE_GETNAMEINFO
353 result = getnameinfo(sap, salen, buf, buflen,
354 NULL, 0, NI_NUMERICHOST);
358 nfs_error(_("%s: invalid server address: %s"), progname,
359 gai_strerror(result));
361 #else /* HAVE_GETNAMEINFO */
364 if (sap->sa_family == AF_INET) {
365 addr = inet_ntoa(((struct sockaddr_in *)sap)->sin_addr);
366 if (addr && strlen(addr) < buflen) {
372 nfs_error(_("%s: invalid server address"), progname);
374 #endif /* HAVE_GETNAMEINFO */
378 * Attempt to connect a socket, but time out after "timeout" seconds.
380 * On error return, caller closes the socket.
382 static int connect_to(int fd, struct sockaddr *addr,
383 socklen_t addrlen, int timeout)
387 struct timeval tv = {
391 saved = fcntl(fd, F_GETFL, 0);
392 fcntl(fd, F_SETFL, saved | O_NONBLOCK);
394 ret = connect(fd, addr, addrlen);
395 if (ret < 0 && errno != EINPROGRESS)
403 ret = select(fd + 1, &rset, &wset, NULL, &tv);
408 if (FD_ISSET(fd, &rset) || FD_ISSET(fd, &wset)) {
410 socklen_t len = sizeof(error);
411 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
421 fcntl(fd, F_SETFL, saved);
426 * Create a socket that is locally bound to a reserved or non-reserved port.
428 * The caller should check rpc_createerr to determine the cause of any error.
430 static int get_socket(struct sockaddr_in *saddr, unsigned int p_prot,
431 unsigned int timeout, int resvp, int conn)
434 struct sockaddr_in laddr;
435 socklen_t namelen = sizeof(laddr);
437 type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
438 if ((so = socket (AF_INET, type, p_prot)) < 0)
441 laddr.sin_family = AF_INET;
443 laddr.sin_addr.s_addr = htonl(INADDR_ANY);
445 if (bindresvport(so, &laddr) < 0)
446 goto err_bindresvport;
448 cc = bind(so, (struct sockaddr *)&laddr, namelen);
452 if (type == SOCK_STREAM || (conn && type == SOCK_DGRAM)) {
453 cc = connect_to(so, (struct sockaddr *)saddr, namelen,
461 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
462 rpc_createerr.cf_error.re_errno = errno;
464 nfs_error(_("%s: Unable to create %s socket: errno %d (%s)\n"),
465 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
466 errno, strerror(errno));
471 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
472 rpc_createerr.cf_error.re_errno = errno;
474 nfs_error(_("%s: Unable to bindresvport %s socket: errno %d"
476 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
477 errno, strerror(errno));
483 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
484 rpc_createerr.cf_error.re_errno = errno;
486 nfs_error(_("%s: Unable to bind to %s socket: errno %d (%s)\n"),
487 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
488 errno, strerror(errno));
494 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
495 rpc_createerr.cf_error.re_errno = errno;
497 nfs_error(_("%s: Unable to connect to %s:%d, errno %d (%s)\n"),
498 progname, inet_ntoa(saddr->sin_addr),
499 ntohs(saddr->sin_port), errno, strerror(errno));
505 static void nfs_pp_debug(const struct sockaddr *sap, const socklen_t salen,
506 const rpcprog_t program, const rpcvers_t version,
507 const unsigned short protocol,
508 const unsigned short port)
510 char buf[NI_MAXHOST];
515 if (nfs_present_sockaddr(sap, salen, buf, sizeof(buf)) == 0) {
517 strcat(buf, "unknown host");
520 fprintf(stderr, _("%s: trying %s prog %ld vers %ld prot %s port %d\n"),
521 progname, buf, program, version,
522 (protocol == IPPROTO_UDP ? _("UDP") : _("TCP")),
527 * Use the portmapper to discover whether or not the service we want is
528 * available. The lists 'versions' and 'protos' define ordered sequences
529 * of service versions and udp/tcp protocols to probe for.
531 * Returns 1 if the requested service port is unambiguous and pingable;
532 * @pmap is filled in with the version, port, and transport protocol used
533 * during the successful ping. Note that if a port is already specified
534 * in @pmap and it matches the rpcbind query result, nfs_probe_port() does
535 * not perform an RPC ping.
537 * If an error occurs or the requested service isn't available, zero is
538 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
540 static int nfs_probe_port(const struct sockaddr *sap, const socklen_t salen,
541 struct pmap *pmap, const unsigned long *versions,
542 const unsigned int *protos)
544 struct sockaddr_storage address;
545 struct sockaddr *saddr = (struct sockaddr *)&address;
546 const unsigned long prog = pmap->pm_prog, *p_vers;
547 const unsigned int prot = (u_int)pmap->pm_prot, *p_prot;
548 const u_short port = (u_short) pmap->pm_port;
549 unsigned long vers = pmap->pm_vers;
550 unsigned short p_port;
552 memcpy(saddr, sap, salen);
553 p_prot = prot ? &prot : protos;
554 p_vers = vers ? &vers : versions;
555 rpc_createerr.cf_stat = 0;
558 p_port = nfs_getport(saddr, salen, prog, *p_vers, *p_prot);
560 if (!port || port == p_port) {
561 nfs_set_port(saddr, p_port);
562 nfs_pp_debug(saddr, salen, prog, *p_vers,
564 if (nfs_rpc_ping(saddr, salen, prog,
565 *p_vers, *p_prot, NULL))
569 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
570 rpc_createerr.cf_stat != RPC_TIMEDOUT &&
571 rpc_createerr.cf_stat != RPC_CANTRECV &&
572 rpc_createerr.cf_stat != RPC_PROGVERSMISMATCH)
580 if (rpc_createerr.cf_stat == RPC_TIMEDOUT ||
581 rpc_createerr.cf_stat == RPC_CANTRECV)
584 if (vers || !*++p_vers)
593 pmap->pm_vers = *p_vers;
595 pmap->pm_prot = *p_prot;
597 pmap->pm_port = p_port;
598 rpc_createerr.cf_stat = 0;
603 * Probe a server's NFS service to determine which versions and
604 * transport protocols are supported.
606 * Returns 1 if the requested service port is unambiguous and pingable;
607 * @pmap is filled in with the version, port, and transport protocol used
608 * during the successful ping. If all three are already specified, simply
609 * return success without an rpcbind query or RPC ping (we may be trying
610 * to mount an NFS service that is not advertised via rpcbind).
612 * If an error occurs or the requested service isn't available, zero is
613 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
615 static int nfs_probe_nfsport(const struct sockaddr *sap, const socklen_t salen,
618 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
621 if (nfs_mount_data_version >= 4)
622 return nfs_probe_port(sap, salen, pmap,
623 probe_nfs3_first, probe_tcp_first);
625 return nfs_probe_port(sap, salen, pmap,
626 probe_nfs2_only, probe_udp_only);
630 * Probe a server's mountd service to determine which versions and
631 * transport protocols are supported.
633 * Returns 1 if the requested service port is unambiguous and pingable;
634 * @pmap is filled in with the version, port, and transport protocol used
635 * during the successful ping. If all three are already specified, simply
636 * return success without an rpcbind query or RPC ping (we may be trying
637 * to mount an NFS service that is not advertised via rpcbind).
639 * If an error occurs or the requested service isn't available, zero is
640 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
642 static int nfs_probe_mntport(const struct sockaddr *sap, const socklen_t salen,
645 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
648 if (nfs_mount_data_version >= 4)
649 return nfs_probe_port(sap, salen, pmap,
650 probe_mnt3_first, probe_udp_first);
652 return nfs_probe_port(sap, salen, pmap,
653 probe_mnt1_first, probe_udp_only);
657 * Probe a server's mountd service to determine which versions and
658 * transport protocols are supported. Invoked when the protocol
659 * version is already known for both the NFS and mountd service.
661 * Returns 1 and fills in both @pmap structs if the requested service
662 * ports are unambiguous and pingable. Otherwise zero is returned;
663 * rpccreateerr.cf_stat is set to reflect the nature of the error.
665 static int nfs_probe_version_fixed(const struct sockaddr *mnt_saddr,
666 const socklen_t mnt_salen,
667 struct pmap *mnt_pmap,
668 const struct sockaddr *nfs_saddr,
669 const socklen_t nfs_salen,
670 struct pmap *nfs_pmap)
672 if (!nfs_probe_nfsport(nfs_saddr, nfs_salen, nfs_pmap))
674 return nfs_probe_mntport(mnt_saddr, mnt_salen, mnt_pmap);
678 * nfs_probe_bothports - discover the RPC endpoints of mountd and NFS server
679 * @mnt_saddr: pointer to socket address of mountd server
680 * @mnt_salen: length of mountd server's address
681 * @mnt_pmap: IN: partially filled-in mountd RPC service tuple;
682 * OUT: fully filled-in mountd RPC service tuple
683 * @nfs_saddr: pointer to socket address of NFS server
684 * @nfs_salen: length of NFS server's address
685 * @nfs_pmap: IN: partially filled-in NFS RPC service tuple;
686 * OUT: fully filled-in NFS RPC service tuple
688 * Returns 1 and fills in both @pmap structs if the requested service
689 * ports are unambiguous and pingable. Otherwise zero is returned;
690 * rpccreateerr.cf_stat is set to reflect the nature of the error.
692 int nfs_probe_bothports(const struct sockaddr *mnt_saddr,
693 const socklen_t mnt_salen,
694 struct pmap *mnt_pmap,
695 const struct sockaddr *nfs_saddr,
696 const socklen_t nfs_salen,
697 struct pmap *nfs_pmap)
699 struct pmap save_nfs, save_mnt;
700 const unsigned long *probe_vers;
702 if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
703 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
704 else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
705 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
707 if (nfs_pmap->pm_vers)
708 return nfs_probe_version_fixed(mnt_saddr, mnt_salen, mnt_pmap,
709 nfs_saddr, nfs_salen, nfs_pmap);
711 memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
712 memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
713 probe_vers = (nfs_mount_data_version >= 4) ?
714 probe_mnt3_first : probe_mnt1_first;
716 for (; *probe_vers; probe_vers++) {
717 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
718 if (nfs_probe_nfsport(nfs_saddr, nfs_salen, nfs_pmap) != 0) {
719 mnt_pmap->pm_vers = *probe_vers;
720 if (nfs_probe_mntport(mnt_saddr, mnt_salen, mnt_pmap) != 0)
722 memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
724 switch (rpc_createerr.cf_stat) {
725 case RPC_PROGVERSMISMATCH:
726 case RPC_PROGNOTREGISTERED:
731 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
738 * probe_bothports - discover the RPC endpoints of mountd and NFS server
739 * @mnt_server: pointer to address and pmap argument for mountd results
740 * @nfs_server: pointer to address and pmap argument for NFS server
742 * This is the legacy API that takes "clnt_addr_t" for both servers,
743 * but supports only AF_INET addresses.
745 * Returns 1 and fills in the pmap field in both clnt_addr_t structs
746 * if the requested service ports are unambiguous and pingable.
747 * Otherwise zero is returned; rpccreateerr.cf_stat is set to reflect
748 * the nature of the error.
750 int probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
752 return nfs_probe_bothports((struct sockaddr *)&mnt_server->saddr,
753 sizeof(mnt_server->saddr),
755 (struct sockaddr *)&nfs_server->saddr,
756 sizeof(nfs_server->saddr),
760 static int nfs_probe_statd(void)
762 struct sockaddr_in addr = {
763 .sin_family = AF_INET,
764 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
766 rpcprog_t program = nfs_getrpcbyname(NSMPROG, nfs_ns_pgmtbl);
768 return nfs_getport_ping((struct sockaddr *)&addr, sizeof(addr),
769 program, (rpcvers_t)1, IPPROTO_UDP);
773 * start_statd - attempt to start rpc.statd
775 * Returns 1 if statd is running; otherwise zero.
777 int start_statd(void)
783 if (nfs_probe_statd())
787 if (stat(START_STATD, &stb) == 0) {
788 if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {
792 execl(START_STATD, START_STATD, NULL);
795 nfs_error(_("fork failed: %s"),
798 default: /* parent */
799 waitpid(pid, NULL,0);
802 if (nfs_probe_statd())
812 * nfs_call_umount - ask the server to remove a share from it's rmtab
813 * @mnt_server: address of RPC MNT program server
814 * @argp: directory path of share to "unmount"
816 * Returns one if the unmount call succeeded; zero if the unmount
817 * failed for any reason.
819 * Note that a side effect of calling this function is that rpccreateerr
822 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
824 struct sockaddr *sap = (struct sockaddr *)&mnt_server->saddr;
825 socklen_t salen = sizeof(mnt_server->saddr);
826 struct pmap *pmap = &mnt_server->pmap;
828 enum clnt_stat res = 0;
831 if (!nfs_probe_mntport(sap, salen, pmap))
833 clnt = mnt_openclnt(mnt_server, &msock);
836 res = clnt_call(clnt, MOUNTPROC_UMNT,
837 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
838 (xdrproc_t)xdr_void, NULL,
840 mnt_closeclnt(clnt, msock);
842 if (res == RPC_SUCCESS)
848 * mnt_openclnt - get a handle for a remote mountd service
849 * @mnt_server: address and pmap arguments of mountd service
850 * @msock: returns a file descriptor of the underlying transport socket
852 * Returns an active handle for the remote's mountd service
854 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
856 struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
857 struct pmap *mnt_pmap = &mnt_server->pmap;
860 mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
861 *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, MOUNT_TIMEOUT,
863 if (*msock == RPC_ANYSOCK) {
864 if (rpc_createerr.cf_error.re_errno == EADDRINUSE)
866 * Probably in-use by a TIME_WAIT connection,
867 * It is worth waiting a while and trying again.
869 rpc_createerr.cf_stat = RPC_TIMEDOUT;
873 switch (mnt_pmap->pm_prot) {
875 clnt = clntudp_bufcreate(mnt_saddr,
876 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
877 RETRY_TIMEOUT, msock,
878 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
881 clnt = clnttcp_create(mnt_saddr,
882 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
884 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
888 /* try to mount hostname:dirname */
889 clnt->cl_auth = authunix_create_default();
896 * mnt_closeclnt - terminate a handle for a remote mountd service
897 * @clnt: pointer to an active handle for a remote mountd service
898 * @msock: file descriptor of the underlying transport socket
901 void mnt_closeclnt(CLIENT *clnt, int msock)
903 auth_destroy(clnt->cl_auth);
909 * clnt_ping - send an RPC ping to the remote RPC service endpoint
910 * @saddr: server's address
911 * @prog: target RPC program number
912 * @vers: target RPC version number
913 * @prot: target RPC protocol
914 * @caddr: filled in with our network address
916 * Sigh... GETPORT queries don't actually check the version number.
917 * In order to make sure that the server actually supports the service
918 * we're requesting, we open an RPC client, and fire off a NULL
921 * caddr is the network address that the server will use to call us back.
922 * On multi-homed clients, this address depends on which NIC we use to
923 * route requests to the server.
925 * Returns one if successful, otherwise zero.
927 int clnt_ping(struct sockaddr_in *saddr, const unsigned long prog,
928 const unsigned long vers, const unsigned int prot,
929 struct sockaddr_in *caddr)
933 static char clnt_res;
934 struct sockaddr dissolve;
936 rpc_createerr.cf_stat = stat = 0;
937 sock = get_socket(saddr, prot, CONNECT_TIMEOUT, FALSE, TRUE);
938 if (sock == RPC_ANYSOCK) {
939 if (rpc_createerr.cf_error.re_errno == ETIMEDOUT) {
941 * TCP timeout. Bubble up the error to see
942 * how it should be handled.
944 rpc_createerr.cf_stat = RPC_TIMEDOUT;
950 /* Get the address of our end of this connection */
951 socklen_t len = sizeof(*caddr);
952 if (getsockname(sock, caddr, &len) != 0)
953 caddr->sin_family = 0;
958 /* The socket is connected (so we could getsockname successfully),
959 * but some servers on multi-homed hosts reply from
960 * the wrong address, so if we stay connected, we lose the reply.
962 dissolve.sa_family = AF_UNSPEC;
963 connect(sock, &dissolve, sizeof(dissolve));
965 clnt = clntudp_bufcreate(saddr, prog, vers,
966 RETRY_TIMEOUT, &sock,
967 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
970 clnt = clnttcp_create(saddr, prog, vers, &sock,
971 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
978 memset(&clnt_res, 0, sizeof(clnt_res));
979 stat = clnt_call(clnt, NULLPROC,
980 (xdrproc_t)xdr_void, (caddr_t)NULL,
981 (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
984 clnt_geterr(clnt, &rpc_createerr.cf_error);
985 rpc_createerr.cf_stat = stat;
990 if (stat == RPC_SUCCESS)
997 * Try a getsockname() on a connected datagram socket.
999 * Returns 1 and fills in @buf if successful; otherwise, zero.
1001 * A connected datagram socket prevents leaving a socket in TIME_WAIT.
1002 * This conserves the ephemeral port number space, helping reduce failed
1003 * socket binds during mount storms.
1005 static int nfs_ca_sockname(const struct sockaddr *sap, const socklen_t salen,
1006 struct sockaddr *buf, socklen_t *buflen)
1008 struct sockaddr_in sin = {
1009 .sin_family = AF_INET,
1010 .sin_addr.s_addr = htonl(INADDR_ANY),
1012 struct sockaddr_in6 sin6 = {
1013 .sin6_family = AF_INET6,
1014 .sin6_addr = IN6ADDR_ANY_INIT,
1018 sock = socket(sap->sa_family, SOCK_DGRAM, IPPROTO_UDP);
1022 switch (sap->sa_family) {
1024 if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
1030 if (bind(sock, (struct sockaddr *)&sin6, sizeof(sin6)) < 0) {
1036 errno = EAFNOSUPPORT;
1040 if (connect(sock, sap, salen) < 0) {
1045 return !getsockname(sock, buf, buflen);
1049 * Try to generate an address that prevents the server from calling us.
1051 * Returns 1 and fills in @buf if successful; otherwise, zero.
1053 static int nfs_ca_gai(const struct sockaddr *sap, const socklen_t salen,
1054 struct sockaddr *buf, socklen_t *buflen)
1056 struct addrinfo *gai_results;
1057 struct addrinfo gai_hint = {
1058 .ai_family = sap->sa_family,
1059 .ai_flags = AI_PASSIVE, /* ANYADDR */
1062 if (getaddrinfo(NULL, "", &gai_hint, &gai_results))
1065 *buflen = gai_results->ai_addrlen;
1066 memcpy(buf, gai_results->ai_addr, *buflen);
1068 freeaddrinfo(gai_results);
1074 * nfs_callback_address - acquire our local network address
1075 * @sap: pointer to address of remote
1076 * @sap_len: length of address
1077 * @buf: pointer to buffer to be filled in with local network address
1078 * @buflen: IN: length of buffer to fill in; OUT: length of filled-in address
1080 * Discover a network address that an NFSv4 server can use to call us back.
1081 * On multi-homed clients, this address depends on which NIC we use to
1082 * route requests to the server.
1084 * Returns 1 and fills in @buf if an unambiguous local address is
1085 * available; returns 1 and fills in an appropriate ANYADDR address
1086 * if a local address isn't available; otherwise, returns zero.
1088 int nfs_callback_address(const struct sockaddr *sap, const socklen_t salen,
1089 struct sockaddr *buf, socklen_t *buflen)
1091 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1093 if (nfs_ca_sockname(sap, salen, buf, buflen) == 0)
1094 if (nfs_ca_gai(sap, salen, buf, buflen) == 0)
1098 * The server can't use an interface ID that was generated
1099 * here on the client, so always clear sin6_scope_id.
1101 if (sin6->sin6_family == AF_INET6)
1102 sin6->sin6_scope_id = 0;
1109 nfs_error(_("%s: failed to construct callback address"));