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[] = {
99 static const unsigned long nfs_to_mnt[] = {
106 static const unsigned long mnt_to_nfs[] = {
114 * Map an NFS version into the corresponding Mountd version
116 unsigned long nfsvers_to_mnt(const unsigned long vers)
119 return nfs_to_mnt[vers];
124 * Map a Mountd version into the corresponding NFS version
126 static unsigned long mntvers_to_nfs(const unsigned long vers)
129 return mnt_to_nfs[vers];
133 static const unsigned int probe_udp_only[] = {
138 static const unsigned int probe_udp_first[] = {
144 static const unsigned int probe_tcp_first[] = {
150 static const unsigned long probe_nfs2_only[] = {
155 static const unsigned long probe_nfs3_first[] = {
161 static const unsigned long probe_mnt1_first[] = {
167 static const unsigned long probe_mnt3_first[] = {
174 static int nfs_lookup(const char *hostname, const sa_family_t family,
175 struct sockaddr *sap, socklen_t *salen)
177 struct addrinfo *gai_results;
178 struct addrinfo gai_hint = {
179 #ifdef HAVE_DECL_AI_ADDRCONFIG
180 .ai_flags = AI_ADDRCONFIG,
181 #endif /* HAVE_DECL_AI_ADDRCONFIG */
184 socklen_t len = *salen;
189 error = getaddrinfo(hostname, NULL, &gai_hint, &gai_results);
194 nfs_error(_("%s: DNS resolution failed for %s: %s"),
195 progname, hostname, strerror(errno));
198 nfs_error(_("%s: DNS resolution failed for %s: %s"),
199 progname, hostname, gai_strerror(error));
203 switch (gai_results->ai_addr->sa_family) {
206 if (len >= gai_results->ai_addrlen) {
207 *salen = gai_results->ai_addrlen;
208 memcpy(sap, gai_results->ai_addr, *salen);
213 /* things are really broken if we get here, so warn */
214 nfs_error(_("%s: unrecognized DNS resolution results for %s"),
219 freeaddrinfo(gai_results);
224 * nfs_name_to_address - resolve hostname to an IPv4 or IPv6 socket address
225 * @hostname: pointer to C string containing DNS hostname to resolve
226 * @sap: pointer to buffer to fill with socket address
227 * @len: IN: size of buffer to fill; OUT: size of socket address
229 * Returns 1 and places a socket address at @sap if successful;
232 int nfs_name_to_address(const char *hostname,
233 struct sockaddr *sap, socklen_t *salen)
235 #ifdef IPV6_SUPPORTED
236 return nfs_lookup(hostname, AF_UNSPEC, sap, salen);
237 #else /* !IPV6_SUPPORTED */
238 return nfs_lookup(hostname, AF_INET, sap, salen);
239 #endif /* !IPV6_SUPPORTED */
243 * nfs_gethostbyname - resolve a hostname to an IPv4 address
244 * @hostname: pointer to a C string containing a DNS hostname
245 * @sin: returns an IPv4 address
247 * Returns 1 if successful, otherwise zero.
249 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *sin)
251 socklen_t len = sizeof(*sin);
253 return nfs_lookup(hostname, AF_INET, (struct sockaddr *)sin, &len);
257 * nfs_string_to_sockaddr - convert string address to sockaddr
258 * @address: pointer to presentation format address to convert
259 * @sap: pointer to socket address buffer to fill in
260 * @salen: IN: length of address buffer
261 * OUT: length of converted socket address
263 * Convert a presentation format address string to a socket address.
264 * Similar to nfs_name_to_address(), but the DNS query is squelched,
265 * and won't make any noise if the getaddrinfo() call fails.
267 * Returns 1 and fills in @sap and @salen if successful; otherwise zero.
269 * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
270 * on presenting IPv6 addresses as text strings.
272 int nfs_string_to_sockaddr(const char *address, struct sockaddr *sap,
275 struct addrinfo *gai_results;
276 struct addrinfo gai_hint = {
277 .ai_flags = AI_NUMERICHOST,
279 socklen_t len = *salen;
284 if (getaddrinfo(address, NULL, &gai_hint, &gai_results) == 0) {
285 switch (gai_results->ai_addr->sa_family) {
288 if (len >= gai_results->ai_addrlen) {
289 *salen = gai_results->ai_addrlen;
290 memcpy(sap, gai_results->ai_addr, *salen);
295 freeaddrinfo(gai_results);
302 * nfs_present_sockaddr - convert sockaddr to string
303 * @sap: pointer to socket address to convert
304 * @salen: length of socket address
305 * @buf: pointer to buffer to fill in
306 * @buflen: length of buffer
308 * Convert the passed-in sockaddr-style address to presentation format.
309 * The presentation format address is placed in @buf and is
312 * Returns 1 if successful; otherwise zero.
314 * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
315 * on presenting IPv6 addresses as text strings.
317 int nfs_present_sockaddr(const struct sockaddr *sap, const socklen_t salen,
318 char *buf, const size_t buflen)
320 #ifdef HAVE_GETNAMEINFO
323 result = getnameinfo(sap, salen, buf, buflen,
324 NULL, 0, NI_NUMERICHOST);
328 nfs_error(_("%s: invalid server address: %s"), progname,
329 gai_strerror(result));
331 #else /* HAVE_GETNAMEINFO */
334 if (sap->sa_family == AF_INET) {
335 addr = inet_ntoa(((struct sockaddr_in *)sap)->sin_addr);
336 if (addr && strlen(addr) < buflen) {
342 nfs_error(_("%s: invalid server address"), progname);
344 #endif /* HAVE_GETNAMEINFO */
348 * Attempt to connect a socket, but time out after "timeout" seconds.
350 * On error return, caller closes the socket.
352 static int connect_to(int fd, struct sockaddr *addr,
353 socklen_t addrlen, int timeout)
357 struct timeval tv = {
361 saved = fcntl(fd, F_GETFL, 0);
362 fcntl(fd, F_SETFL, saved | O_NONBLOCK);
364 ret = connect(fd, addr, addrlen);
365 if (ret < 0 && errno != EINPROGRESS)
373 ret = select(fd + 1, &rset, &wset, NULL, &tv);
378 if (FD_ISSET(fd, &rset) || FD_ISSET(fd, &wset)) {
380 socklen_t len = sizeof(error);
381 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
391 fcntl(fd, F_SETFL, saved);
396 * Create a socket that is locally bound to a reserved or non-reserved port.
398 * The caller should check rpc_createerr to determine the cause of any error.
400 static int get_socket(struct sockaddr_in *saddr, unsigned int p_prot,
401 unsigned int timeout, int resvp, int conn)
404 struct sockaddr_in laddr;
405 socklen_t namelen = sizeof(laddr);
407 type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
408 if ((so = socket (AF_INET, type, p_prot)) < 0)
411 laddr.sin_family = AF_INET;
413 laddr.sin_addr.s_addr = htonl(INADDR_ANY);
415 if (bindresvport(so, &laddr) < 0)
416 goto err_bindresvport;
418 cc = bind(so, (struct sockaddr *)&laddr, namelen);
422 if (type == SOCK_STREAM || (conn && type == SOCK_DGRAM)) {
423 cc = connect_to(so, (struct sockaddr *)saddr, namelen,
431 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
432 rpc_createerr.cf_error.re_errno = errno;
434 nfs_error(_("%s: Unable to create %s socket: errno %d (%s)\n"),
435 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
436 errno, strerror(errno));
441 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
442 rpc_createerr.cf_error.re_errno = errno;
444 nfs_error(_("%s: Unable to bindresvport %s socket: errno %d"
446 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
447 errno, strerror(errno));
453 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
454 rpc_createerr.cf_error.re_errno = errno;
456 nfs_error(_("%s: Unable to bind to %s socket: errno %d (%s)\n"),
457 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
458 errno, strerror(errno));
464 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
465 rpc_createerr.cf_error.re_errno = errno;
467 nfs_error(_("%s: Unable to connect to %s:%d, errno %d (%s)\n"),
468 progname, inet_ntoa(saddr->sin_addr),
469 ntohs(saddr->sin_port), errno, strerror(errno));
475 static void nfs_pp_debug(const struct sockaddr *sap, const socklen_t salen,
476 const rpcprog_t program, const rpcvers_t version,
477 const unsigned short protocol,
478 const unsigned short port)
480 char buf[NI_MAXHOST];
485 if (nfs_present_sockaddr(sap, salen, buf, sizeof(buf)) == 0) {
487 strcat(buf, "unknown host");
490 fprintf(stderr, _("%s: trying %s prog %lu vers %lu prot %s port %d\n"),
491 progname, buf, (unsigned long)program,
492 (unsigned long)version,
493 (protocol == IPPROTO_UDP ? _("UDP") : _("TCP")),
497 static void nfs_pp_debug2(const char *str)
502 if (rpc_createerr.cf_error.re_status == RPC_CANTRECV ||
503 rpc_createerr.cf_error.re_status == RPC_CANTSEND)
504 nfs_error(_("%s: portmap query %s%s - %s"),
505 progname, str, clnt_spcreateerror(""),
506 strerror(rpc_createerr.cf_error.re_errno));
508 nfs_error(_("%s: portmap query %s%s"),
509 progname, str, clnt_spcreateerror(""));
513 * Use the portmapper to discover whether or not the service we want is
514 * available. The lists 'versions' and 'protos' define ordered sequences
515 * of service versions and udp/tcp protocols to probe for.
517 * Returns 1 if the requested service port is unambiguous and pingable;
518 * @pmap is filled in with the version, port, and transport protocol used
519 * during the successful ping. Note that if a port is already specified
520 * in @pmap and it matches the rpcbind query result, nfs_probe_port() does
521 * not perform an RPC ping.
523 * If an error occurs or the requested service isn't available, zero is
524 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
526 static int nfs_probe_port(const struct sockaddr *sap, const socklen_t salen,
527 struct pmap *pmap, const unsigned long *versions,
528 const unsigned int *protos)
530 struct sockaddr_storage address;
531 struct sockaddr *saddr = (struct sockaddr *)&address;
532 const unsigned long prog = pmap->pm_prog, *p_vers;
533 const unsigned int prot = (u_int)pmap->pm_prot, *p_prot;
534 const u_short port = (u_short) pmap->pm_port;
535 unsigned long vers = pmap->pm_vers;
536 unsigned short p_port;
538 memcpy(saddr, sap, salen);
539 p_prot = prot ? &prot : protos;
540 p_vers = vers ? &vers : versions;
544 printf(_("%s: prog %lu, trying vers=%lu, prot=%u\n"),
545 progname, prog, *p_vers, *p_prot);
546 p_port = nfs_getport(saddr, salen, prog, *p_vers, *p_prot);
548 if (!port || port == p_port) {
549 nfs_set_port(saddr, p_port);
550 nfs_pp_debug(saddr, salen, prog, *p_vers,
552 if (nfs_rpc_ping(saddr, salen, prog,
553 *p_vers, *p_prot, NULL))
556 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
558 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
559 rpc_createerr.cf_stat != RPC_TIMEDOUT &&
560 rpc_createerr.cf_stat != RPC_CANTRECV &&
561 rpc_createerr.cf_stat != RPC_PROGVERSMISMATCH)
566 nfs_pp_debug2("retrying");
571 if (rpc_createerr.cf_stat == RPC_TIMEDOUT ||
572 rpc_createerr.cf_stat == RPC_CANTRECV)
575 if (vers || !*++p_vers)
579 nfs_pp_debug2("failed");
584 pmap->pm_vers = *p_vers;
586 pmap->pm_prot = *p_prot;
588 pmap->pm_port = p_port;
589 nfs_clear_rpc_createerr();
594 * Probe a server's NFS service to determine which versions and
595 * transport protocols are supported.
597 * Returns 1 if the requested service port is unambiguous and pingable;
598 * @pmap is filled in with the version, port, and transport protocol used
599 * during the successful ping. If all three are already specified, simply
600 * return success without an rpcbind query or RPC ping (we may be trying
601 * to mount an NFS service that is not advertised via rpcbind).
603 * If an error occurs or the requested service isn't available, zero is
604 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
606 static int nfs_probe_nfsport(const struct sockaddr *sap, const socklen_t salen,
609 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
612 if (nfs_mount_data_version >= 4)
613 return nfs_probe_port(sap, salen, pmap,
614 probe_nfs3_first, probe_tcp_first);
616 return nfs_probe_port(sap, salen, pmap,
617 probe_nfs2_only, probe_udp_only);
621 * Probe a server's mountd service to determine which versions and
622 * transport protocols are supported.
624 * Returns 1 if the requested service port is unambiguous and pingable;
625 * @pmap is filled in with the version, port, and transport protocol used
626 * during the successful ping. If all three are already specified, simply
627 * return success without an rpcbind query or RPC ping (we may be trying
628 * to mount an NFS service that is not advertised via rpcbind).
630 * If an error occurs or the requested service isn't available, zero is
631 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
633 static int nfs_probe_mntport(const struct sockaddr *sap, const socklen_t salen,
636 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
639 if (nfs_mount_data_version >= 4)
640 return nfs_probe_port(sap, salen, pmap,
641 probe_mnt3_first, probe_udp_first);
643 return nfs_probe_port(sap, salen, pmap,
644 probe_mnt1_first, probe_udp_only);
648 * Probe a server's mountd service to determine which versions and
649 * transport protocols are supported. Invoked when the protocol
650 * version is already known for both the NFS and mountd service.
652 * Returns 1 and fills in both @pmap structs if the requested service
653 * ports are unambiguous and pingable. Otherwise zero is returned;
654 * rpccreateerr.cf_stat is set to reflect the nature of the error.
656 static int nfs_probe_version_fixed(const struct sockaddr *mnt_saddr,
657 const socklen_t mnt_salen,
658 struct pmap *mnt_pmap,
659 const struct sockaddr *nfs_saddr,
660 const socklen_t nfs_salen,
661 struct pmap *nfs_pmap)
663 if (!nfs_probe_nfsport(nfs_saddr, nfs_salen, nfs_pmap))
665 return nfs_probe_mntport(mnt_saddr, mnt_salen, mnt_pmap);
669 * nfs_probe_bothports - discover the RPC endpoints of mountd and NFS server
670 * @mnt_saddr: pointer to socket address of mountd server
671 * @mnt_salen: length of mountd server's address
672 * @mnt_pmap: IN: partially filled-in mountd RPC service tuple;
673 * OUT: fully filled-in mountd RPC service tuple
674 * @nfs_saddr: pointer to socket address of NFS server
675 * @nfs_salen: length of NFS server's address
676 * @nfs_pmap: IN: partially filled-in NFS RPC service tuple;
677 * OUT: fully filled-in NFS RPC service tuple
679 * Returns 1 and fills in both @pmap structs if the requested service
680 * ports are unambiguous and pingable. Otherwise zero is returned;
681 * rpccreateerr.cf_stat is set to reflect the nature of the error.
683 int nfs_probe_bothports(const struct sockaddr *mnt_saddr,
684 const socklen_t mnt_salen,
685 struct pmap *mnt_pmap,
686 const struct sockaddr *nfs_saddr,
687 const socklen_t nfs_salen,
688 struct pmap *nfs_pmap)
690 struct pmap save_nfs, save_mnt;
691 const unsigned long *probe_vers;
693 if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
694 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
695 else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
696 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
698 if (nfs_pmap->pm_vers)
699 return nfs_probe_version_fixed(mnt_saddr, mnt_salen, mnt_pmap,
700 nfs_saddr, nfs_salen, nfs_pmap);
702 memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
703 memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
704 probe_vers = (nfs_mount_data_version >= 4) ?
705 probe_mnt3_first : probe_mnt1_first;
707 for (; *probe_vers; probe_vers++) {
708 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
709 if (nfs_probe_nfsport(nfs_saddr, nfs_salen, nfs_pmap) != 0) {
710 mnt_pmap->pm_vers = *probe_vers;
711 if (nfs_probe_mntport(mnt_saddr, mnt_salen, mnt_pmap) != 0)
713 memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
715 switch (rpc_createerr.cf_stat) {
716 case RPC_PROGVERSMISMATCH:
717 case RPC_PROGNOTREGISTERED:
722 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
729 * probe_bothports - discover the RPC endpoints of mountd and NFS server
730 * @mnt_server: pointer to address and pmap argument for mountd results
731 * @nfs_server: pointer to address and pmap argument for NFS server
733 * This is the legacy API that takes "clnt_addr_t" for both servers,
734 * but supports only AF_INET addresses.
736 * Returns 1 and fills in the pmap field in both clnt_addr_t structs
737 * if the requested service ports are unambiguous and pingable.
738 * Otherwise zero is returned; rpccreateerr.cf_stat is set to reflect
739 * the nature of the error.
741 int probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
743 return nfs_probe_bothports((struct sockaddr *)&mnt_server->saddr,
744 sizeof(mnt_server->saddr),
746 (struct sockaddr *)&nfs_server->saddr,
747 sizeof(nfs_server->saddr),
751 static int nfs_probe_statd(void)
753 struct sockaddr_in addr = {
754 .sin_family = AF_INET,
755 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
757 rpcprog_t program = nfs_getrpcbyname(NSMPROG, nfs_ns_pgmtbl);
759 return nfs_getport_ping((struct sockaddr *)&addr, sizeof(addr),
760 program, (rpcvers_t)1, IPPROTO_UDP);
764 * start_statd - attempt to start rpc.statd
766 * Returns 1 if statd is running; otherwise zero.
768 int start_statd(void)
774 if (nfs_probe_statd())
778 if (stat(START_STATD, &stb) == 0) {
779 if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {
783 execl(START_STATD, START_STATD, NULL);
786 nfs_error(_("%s: fork failed: %s"),
787 progname, strerror(errno));
789 default: /* parent */
790 waitpid(pid, NULL,0);
793 if (nfs_probe_statd())
803 * nfs_advise_umount - ask the server to remove a share from it's rmtab
804 * @sap: pointer to IP address of server to call
805 * @salen: length of server address
806 * @pmap: partially filled-in mountd RPC service tuple
807 * @argp: directory path of share to "unmount"
809 * Returns one if the unmount call succeeded; zero if the unmount
810 * failed for any reason; rpccreateerr.cf_stat is set to reflect
811 * the nature of the error.
813 * We use a fast timeout since this call is advisory only.
815 int nfs_advise_umount(const struct sockaddr *sap, const socklen_t salen,
816 const struct pmap *pmap, const dirpath *argp)
818 struct sockaddr_storage address;
819 struct sockaddr *saddr = (struct sockaddr *)&address;
820 struct pmap mnt_pmap = *pmap;
821 struct timeval timeout = {
822 .tv_sec = MOUNT_TIMEOUT >> 3,
825 enum clnt_stat res = 0;
827 memcpy(saddr, sap, salen);
828 if (nfs_probe_mntport(saddr, salen, &mnt_pmap) == 0) {
830 nfs_error(_("%s: Failed to discover mountd port%s"),
831 progname, clnt_spcreateerror(""));
834 nfs_set_port(saddr, mnt_pmap.pm_port);
836 client = nfs_get_priv_rpcclient(saddr, salen, mnt_pmap.pm_prot,
837 mnt_pmap.pm_prog, mnt_pmap.pm_vers,
839 if (client == NULL) {
841 nfs_error(_("%s: Failed to create RPC client%s"),
842 progname, clnt_spcreateerror(""));
846 client->cl_auth = authunix_create_default();
848 res = CLNT_CALL(client, MOUNTPROC_UMNT,
849 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
850 (xdrproc_t)xdr_void, NULL,
852 if (res != RPC_SUCCESS) {
853 rpc_createerr.cf_stat = res;
854 CLNT_GETERR(client, &rpc_createerr.cf_error);
856 nfs_error(_("%s: UMNT call failed: %s"),
857 progname, clnt_sperrno(res));
860 auth_destroy(client->cl_auth);
861 CLNT_DESTROY(client);
863 if (res != RPC_SUCCESS)
869 * nfs_call_umount - ask the server to remove a share from it's rmtab
870 * @mnt_server: address of RPC MNT program server
871 * @argp: directory path of share to "unmount"
873 * Returns one if the unmount call succeeded; zero if the unmount
874 * failed for any reason.
876 * Note that a side effect of calling this function is that rpccreateerr
879 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
881 struct sockaddr *sap = (struct sockaddr *)&mnt_server->saddr;
882 socklen_t salen = sizeof(mnt_server->saddr);
883 struct pmap *pmap = &mnt_server->pmap;
885 enum clnt_stat res = 0;
888 if (!nfs_probe_mntport(sap, salen, pmap))
890 clnt = mnt_openclnt(mnt_server, &msock);
893 res = clnt_call(clnt, MOUNTPROC_UMNT,
894 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
895 (xdrproc_t)xdr_void, NULL,
897 mnt_closeclnt(clnt, msock);
899 if (res == RPC_SUCCESS)
905 * mnt_openclnt - get a handle for a remote mountd service
906 * @mnt_server: address and pmap arguments of mountd service
907 * @msock: returns a file descriptor of the underlying transport socket
909 * Returns an active handle for the remote's mountd service
911 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
913 struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
914 struct pmap *mnt_pmap = &mnt_server->pmap;
917 mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
918 *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, MOUNT_TIMEOUT,
920 if (*msock == RPC_ANYSOCK) {
921 if (rpc_createerr.cf_error.re_errno == EADDRINUSE)
923 * Probably in-use by a TIME_WAIT connection,
924 * It is worth waiting a while and trying again.
926 rpc_createerr.cf_stat = RPC_TIMEDOUT;
930 switch (mnt_pmap->pm_prot) {
932 clnt = clntudp_bufcreate(mnt_saddr,
933 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
934 RETRY_TIMEOUT, msock,
935 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
938 clnt = clnttcp_create(mnt_saddr,
939 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
941 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
945 /* try to mount hostname:dirname */
946 clnt->cl_auth = authunix_create_default();
953 * mnt_closeclnt - terminate a handle for a remote mountd service
954 * @clnt: pointer to an active handle for a remote mountd service
955 * @msock: file descriptor of the underlying transport socket
958 void mnt_closeclnt(CLIENT *clnt, int msock)
960 auth_destroy(clnt->cl_auth);
966 * clnt_ping - send an RPC ping to the remote RPC service endpoint
967 * @saddr: server's address
968 * @prog: target RPC program number
969 * @vers: target RPC version number
970 * @prot: target RPC protocol
971 * @caddr: filled in with our network address
973 * Sigh... GETPORT queries don't actually check the version number.
974 * In order to make sure that the server actually supports the service
975 * we're requesting, we open an RPC client, and fire off a NULL
978 * caddr is the network address that the server will use to call us back.
979 * On multi-homed clients, this address depends on which NIC we use to
980 * route requests to the server.
982 * Returns one if successful, otherwise zero.
984 int clnt_ping(struct sockaddr_in *saddr, const unsigned long prog,
985 const unsigned long vers, const unsigned int prot,
986 struct sockaddr_in *caddr)
990 static char clnt_res;
991 struct sockaddr dissolve;
993 rpc_createerr.cf_stat = stat = 0;
994 sock = get_socket(saddr, prot, CONNECT_TIMEOUT, FALSE, TRUE);
995 if (sock == RPC_ANYSOCK) {
996 if (rpc_createerr.cf_error.re_errno == ETIMEDOUT) {
998 * TCP timeout. Bubble up the error to see
999 * how it should be handled.
1001 rpc_createerr.cf_stat = RPC_TIMEDOUT;
1007 /* Get the address of our end of this connection */
1008 socklen_t len = sizeof(*caddr);
1009 if (getsockname(sock, caddr, &len) != 0)
1010 caddr->sin_family = 0;
1015 /* The socket is connected (so we could getsockname successfully),
1016 * but some servers on multi-homed hosts reply from
1017 * the wrong address, so if we stay connected, we lose the reply.
1019 dissolve.sa_family = AF_UNSPEC;
1020 connect(sock, &dissolve, sizeof(dissolve));
1022 clnt = clntudp_bufcreate(saddr, prog, vers,
1023 RETRY_TIMEOUT, &sock,
1024 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
1027 clnt = clnttcp_create(saddr, prog, vers, &sock,
1028 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
1035 memset(&clnt_res, 0, sizeof(clnt_res));
1036 stat = clnt_call(clnt, NULLPROC,
1037 (xdrproc_t)xdr_void, (caddr_t)NULL,
1038 (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
1041 clnt_geterr(clnt, &rpc_createerr.cf_error);
1042 rpc_createerr.cf_stat = stat;
1047 if (stat == RPC_SUCCESS)
1054 * Try a getsockname() on a connected datagram socket.
1056 * Returns 1 and fills in @buf if successful; otherwise, zero.
1058 * A connected datagram socket prevents leaving a socket in TIME_WAIT.
1059 * This conserves the ephemeral port number space, helping reduce failed
1060 * socket binds during mount storms.
1062 static int nfs_ca_sockname(const struct sockaddr *sap, const socklen_t salen,
1063 struct sockaddr *buf, socklen_t *buflen)
1065 struct sockaddr_in sin = {
1066 .sin_family = AF_INET,
1067 .sin_addr.s_addr = htonl(INADDR_ANY),
1069 struct sockaddr_in6 sin6 = {
1070 .sin6_family = AF_INET6,
1071 .sin6_addr = IN6ADDR_ANY_INIT,
1075 sock = socket(sap->sa_family, SOCK_DGRAM, IPPROTO_UDP);
1079 switch (sap->sa_family) {
1081 if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
1087 if (bind(sock, (struct sockaddr *)&sin6, sizeof(sin6)) < 0) {
1093 errno = EAFNOSUPPORT;
1097 if (connect(sock, sap, salen) < 0) {
1102 return !getsockname(sock, buf, buflen);
1106 * Try to generate an address that prevents the server from calling us.
1108 * Returns 1 and fills in @buf if successful; otherwise, zero.
1110 static int nfs_ca_gai(const struct sockaddr *sap,
1111 struct sockaddr *buf, socklen_t *buflen)
1113 struct addrinfo *gai_results;
1114 struct addrinfo gai_hint = {
1115 .ai_family = sap->sa_family,
1116 .ai_flags = AI_PASSIVE, /* ANYADDR */
1119 if (getaddrinfo(NULL, "", &gai_hint, &gai_results))
1122 *buflen = gai_results->ai_addrlen;
1123 memcpy(buf, gai_results->ai_addr, *buflen);
1125 freeaddrinfo(gai_results);
1131 * nfs_callback_address - acquire our local network address
1132 * @sap: pointer to address of remote
1133 * @sap_len: length of address
1134 * @buf: pointer to buffer to be filled in with local network address
1135 * @buflen: IN: length of buffer to fill in; OUT: length of filled-in address
1137 * Discover a network address that an NFSv4 server can use to call us back.
1138 * On multi-homed clients, this address depends on which NIC we use to
1139 * route requests to the server.
1141 * Returns 1 and fills in @buf if an unambiguous local address is
1142 * available; returns 1 and fills in an appropriate ANYADDR address
1143 * if a local address isn't available; otherwise, returns zero.
1145 int nfs_callback_address(const struct sockaddr *sap, const socklen_t salen,
1146 struct sockaddr *buf, socklen_t *buflen)
1148 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1150 if (nfs_ca_sockname(sap, salen, buf, buflen) == 0)
1151 if (nfs_ca_gai(sap, buf, buflen) == 0)
1155 * The server can't use an interface ID that was generated
1156 * here on the client, so always clear sin6_scope_id.
1158 if (sin6->sin6_family == AF_INET6)
1159 sin6->sin6_scope_id = 0;
1166 nfs_error(_("%s: failed to construct callback address"),
1172 * "nfsprog" is supported only by the legacy mount command. The
1173 * kernel mount client does not support this option.
1175 * Returns TRUE if @program contains a valid value for this option,
1176 * or FALSE if the option was specified with an invalid value.
1179 nfs_nfs_program(struct mount_options *options, unsigned long *program)
1183 switch (po_get_numeric(options, "nfsprog", &tmp)) {
1196 * NFS RPC program wasn't specified. The RPC program
1197 * cannot be determined via an rpcbind query.
1199 *program = nfs_getrpcbyname(NFSPROG, nfs_nfs_pgmtbl);
1204 * Returns TRUE if @version contains a valid value for this option,
1205 * or FALSE if the option was specified with an invalid value.
1208 nfs_nfs_version(struct mount_options *options, unsigned long *version)
1212 switch (po_rightmost(options, nfs_version_opttbl)) {
1223 switch (po_get_numeric(options, "vers", &tmp)) {
1225 if (tmp >= 2 && tmp <= 4) {
1231 nfs_error(_("%s: option parsing error\n"),
1236 case 4: /* nfsvers */
1237 switch (po_get_numeric(options, "nfsvers", &tmp)) {
1239 if (tmp >= 2 && tmp <= 4) {
1245 nfs_error(_("%s: option parsing error\n"),
1253 * NFS version wasn't specified. The pmap version value
1254 * will be filled in later by an rpcbind query in this case.
1261 * Returns TRUE if @protocol contains a valid value for this option,
1262 * or FALSE if the option was specified with an invalid value.
1265 nfs_nfs_protocol(struct mount_options *options, unsigned long *protocol)
1269 switch (po_rightmost(options, nfs_transport_opttbl)) {
1271 *protocol = IPPROTO_UDP;
1274 *protocol = IPPROTO_TCP;
1277 option = po_get(options, "proto");
1279 if (strcmp(option, "tcp") == 0) {
1280 *protocol = IPPROTO_TCP;
1283 if (strcmp(option, "udp") == 0) {
1284 *protocol = IPPROTO_UDP;
1292 * NFS transport protocol wasn't specified. The pmap
1293 * protocol value will be filled in later by an rpcbind
1294 * query in this case.
1301 * Returns TRUE if @port contains a valid value for this option,
1302 * or FALSE if the option was specified with an invalid value.
1305 nfs_nfs_port(struct mount_options *options, unsigned long *port)
1309 switch (po_get_numeric(options, "port", &tmp)) {
1313 if (tmp >= 1 && tmp <= 65535) {
1322 * NFS service port wasn't specified. The pmap port value
1323 * will be filled in later by an rpcbind query in this case.
1330 * "mountprog" is supported only by the legacy mount command. The
1331 * kernel mount client does not support this option.
1333 * Returns TRUE if @program contains a valid value for this option,
1334 * or FALSE if the option was specified with an invalid value.
1337 nfs_mount_program(struct mount_options *options, unsigned long *program)
1341 switch (po_get_numeric(options, "mountprog", &tmp)) {
1354 * MNT RPC program wasn't specified. The RPC program
1355 * cannot be determined via an rpcbind query.
1357 *program = nfs_getrpcbyname(MOUNTPROG, nfs_mnt_pgmtbl);
1362 * Returns TRUE if @version contains a valid value for this option,
1363 * or FALSE if the option was specified with an invalid value.
1366 nfs_mount_version(struct mount_options *options, unsigned long *version)
1370 switch (po_get_numeric(options, "mountvers", &tmp)) {
1374 if (tmp >= 1 && tmp <= 4) {
1383 * MNT version wasn't specified. The pmap version value
1384 * will be filled in later by an rpcbind query in this case.
1391 * Returns TRUE if @protocol contains a valid value for this option,
1392 * or FALSE if the option was specified with an invalid value.
1395 nfs_mount_protocol(struct mount_options *options, unsigned long *protocol)
1399 option = po_get(options, "mountproto");
1401 if (strcmp(option, "tcp") == 0) {
1402 *protocol = IPPROTO_TCP;
1405 if (strcmp(option, "udp") == 0) {
1406 *protocol = IPPROTO_UDP;
1413 * MNT transport protocol wasn't specified. If the NFS
1414 * transport protocol was specified, use that; otherwise
1415 * set @protocol to zero. The pmap protocol value will
1416 * be filled in later by an rpcbind query in this case.
1418 return nfs_nfs_protocol(options, protocol);
1422 * Returns TRUE if @port contains a valid value for this option,
1423 * or FALSE if the option was specified with an invalid value.
1426 nfs_mount_port(struct mount_options *options, unsigned long *port)
1430 switch (po_get_numeric(options, "mountport", &tmp)) {
1434 if (tmp >= 1 && tmp <= 65535) {
1443 * MNT service port wasn't specified. The pmap port value
1444 * will be filled in later by an rpcbind query in this case.
1451 * nfs_options2pmap - set up pmap structs based on mount options
1452 * @options: pointer to mount options
1453 * @nfs_pmap: OUT: pointer to pmap arguments for NFS server
1454 * @mnt_pmap: OUT: pointer to pmap arguments for mountd server
1456 * Returns TRUE if the pmap options specified in @options have valid
1457 * values; otherwise FALSE is returned.
1459 int nfs_options2pmap(struct mount_options *options,
1460 struct pmap *nfs_pmap, struct pmap *mnt_pmap)
1462 if (!nfs_nfs_program(options, &nfs_pmap->pm_prog))
1464 if (!nfs_nfs_version(options, &nfs_pmap->pm_vers))
1466 if (!nfs_nfs_protocol(options, &nfs_pmap->pm_prot))
1468 if (!nfs_nfs_port(options, &nfs_pmap->pm_port))
1471 if (!nfs_mount_program(options, &mnt_pmap->pm_prog))
1473 if (!nfs_mount_version(options, &mnt_pmap->pm_vers))
1475 if (!nfs_mount_protocol(options, &mnt_pmap->pm_prot))
1477 if (!nfs_mount_port(options, &mnt_pmap->pm_port))