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 int nfs_lookup(const char *hostname, const sa_family_t family,
174 struct sockaddr *sap, socklen_t *salen)
176 struct addrinfo *gai_results;
177 struct addrinfo gai_hint = {
178 #ifdef HAVE_DECL_AI_ADDRCONFIG
179 .ai_flags = AI_ADDRCONFIG,
180 #endif /* HAVE_DECL_AI_ADDRCONFIG */
183 socklen_t len = *salen;
188 error = getaddrinfo(hostname, NULL, &gai_hint, &gai_results);
193 nfs_error(_("%s: DNS resolution failed for %s: %s"),
194 progname, hostname, strerror(errno));
197 nfs_error(_("%s: DNS resolution failed for %s: %s"),
198 progname, hostname, gai_strerror(error));
202 switch (gai_results->ai_addr->sa_family) {
205 if (len >= gai_results->ai_addrlen) {
206 *salen = gai_results->ai_addrlen;
207 memcpy(sap, gai_results->ai_addr, *salen);
212 /* things are really broken if we get here, so warn */
213 nfs_error(_("%s: unrecognized DNS resolution results for %s"),
218 freeaddrinfo(gai_results);
223 * nfs_name_to_address - resolve hostname to an IPv4 or IPv6 socket address
224 * @hostname: pointer to C string containing DNS hostname to resolve
225 * @sap: pointer to buffer to fill with socket address
226 * @len: IN: size of buffer to fill; OUT: size of socket address
228 * Returns 1 and places a socket address at @sap if successful;
231 int nfs_name_to_address(const char *hostname,
232 struct sockaddr *sap, socklen_t *salen)
234 #ifdef IPV6_SUPPORTED
235 return nfs_lookup(hostname, AF_UNSPEC, sap, salen);
236 #else /* !IPV6_SUPPORTED */
237 return nfs_lookup(hostname, AF_INET, sap, salen);
238 #endif /* !IPV6_SUPPORTED */
242 * nfs_gethostbyname - resolve a hostname to an IPv4 address
243 * @hostname: pointer to a C string containing a DNS hostname
244 * @sin: returns an IPv4 address
246 * Returns 1 if successful, otherwise zero.
248 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *sin)
250 socklen_t len = sizeof(*sin);
252 return nfs_lookup(hostname, AF_INET, (struct sockaddr *)sin, &len);
256 * nfs_string_to_sockaddr - convert string address to sockaddr
257 * @address: pointer to presentation format address to convert
258 * @sap: pointer to socket address buffer to fill in
259 * @salen: IN: length of address buffer
260 * OUT: length of converted socket address
262 * Convert a presentation format address string to a socket address.
263 * Similar to nfs_name_to_address(), but the DNS query is squelched,
264 * and won't make any noise if the getaddrinfo() call fails.
266 * Returns 1 and fills in @sap and @salen if successful; otherwise zero.
268 * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
269 * on presenting IPv6 addresses as text strings.
271 int nfs_string_to_sockaddr(const char *address, struct sockaddr *sap,
274 struct addrinfo *gai_results;
275 struct addrinfo gai_hint = {
276 .ai_flags = AI_NUMERICHOST,
278 socklen_t len = *salen;
283 if (getaddrinfo(address, NULL, &gai_hint, &gai_results) == 0) {
284 switch (gai_results->ai_addr->sa_family) {
287 if (len >= gai_results->ai_addrlen) {
288 *salen = gai_results->ai_addrlen;
289 memcpy(sap, gai_results->ai_addr, *salen);
294 freeaddrinfo(gai_results);
301 * nfs_present_sockaddr - convert sockaddr to string
302 * @sap: pointer to socket address to convert
303 * @salen: length of socket address
304 * @buf: pointer to buffer to fill in
305 * @buflen: length of buffer
307 * Convert the passed-in sockaddr-style address to presentation format.
308 * The presentation format address is placed in @buf and is
311 * Returns 1 if successful; otherwise zero.
313 * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
314 * on presenting IPv6 addresses as text strings.
316 int nfs_present_sockaddr(const struct sockaddr *sap, const socklen_t salen,
317 char *buf, const size_t buflen)
319 #ifdef HAVE_GETNAMEINFO
322 result = getnameinfo(sap, salen, buf, buflen,
323 NULL, 0, NI_NUMERICHOST);
327 nfs_error(_("%s: invalid server address: %s"), progname,
328 gai_strerror(result));
330 #else /* HAVE_GETNAMEINFO */
333 if (sap->sa_family == AF_INET) {
334 addr = inet_ntoa(((struct sockaddr_in *)sap)->sin_addr);
335 if (addr && strlen(addr) < buflen) {
341 nfs_error(_("%s: invalid server address"), progname);
343 #endif /* HAVE_GETNAMEINFO */
347 * Attempt to connect a socket, but time out after "timeout" seconds.
349 * On error return, caller closes the socket.
351 static int connect_to(int fd, struct sockaddr *addr,
352 socklen_t addrlen, int timeout)
356 struct timeval tv = {
360 saved = fcntl(fd, F_GETFL, 0);
361 fcntl(fd, F_SETFL, saved | O_NONBLOCK);
363 ret = connect(fd, addr, addrlen);
364 if (ret < 0 && errno != EINPROGRESS)
372 ret = select(fd + 1, &rset, &wset, NULL, &tv);
377 if (FD_ISSET(fd, &rset) || FD_ISSET(fd, &wset)) {
379 socklen_t len = sizeof(error);
380 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
390 fcntl(fd, F_SETFL, saved);
395 * Create a socket that is locally bound to a reserved or non-reserved port.
397 * The caller should check rpc_createerr to determine the cause of any error.
399 static int get_socket(struct sockaddr_in *saddr, unsigned int p_prot,
400 unsigned int timeout, int resvp, int conn)
403 struct sockaddr_in laddr;
404 socklen_t namelen = sizeof(laddr);
406 type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
407 if ((so = socket (AF_INET, type, p_prot)) < 0)
410 laddr.sin_family = AF_INET;
412 laddr.sin_addr.s_addr = htonl(INADDR_ANY);
414 if (bindresvport(so, &laddr) < 0)
415 goto err_bindresvport;
417 cc = bind(so, (struct sockaddr *)&laddr, namelen);
421 if (type == SOCK_STREAM || (conn && type == SOCK_DGRAM)) {
422 cc = connect_to(so, (struct sockaddr *)saddr, namelen,
430 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
431 rpc_createerr.cf_error.re_errno = errno;
433 nfs_error(_("%s: Unable to create %s socket: errno %d (%s)\n"),
434 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
435 errno, strerror(errno));
440 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
441 rpc_createerr.cf_error.re_errno = errno;
443 nfs_error(_("%s: Unable to bindresvport %s socket: errno %d"
445 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
446 errno, strerror(errno));
452 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
453 rpc_createerr.cf_error.re_errno = errno;
455 nfs_error(_("%s: Unable to bind to %s socket: errno %d (%s)\n"),
456 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
457 errno, strerror(errno));
463 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
464 rpc_createerr.cf_error.re_errno = errno;
466 nfs_error(_("%s: Unable to connect to %s:%d, errno %d (%s)\n"),
467 progname, inet_ntoa(saddr->sin_addr),
468 ntohs(saddr->sin_port), errno, strerror(errno));
474 static void nfs_pp_debug(const struct sockaddr *sap, const socklen_t salen,
475 const rpcprog_t program, const rpcvers_t version,
476 const unsigned short protocol,
477 const unsigned short port)
479 char buf[NI_MAXHOST];
484 if (nfs_present_sockaddr(sap, salen, buf, sizeof(buf)) == 0) {
486 strcat(buf, "unknown host");
489 fprintf(stderr, _("%s: trying %s prog %lu vers %lu prot %s port %d\n"),
490 progname, buf, (unsigned long)program,
491 (unsigned long)version,
492 (protocol == IPPROTO_UDP ? _("UDP") : _("TCP")),
496 static void nfs_pp_debug2(const char *str)
501 if (rpc_createerr.cf_error.re_status == RPC_CANTRECV ||
502 rpc_createerr.cf_error.re_status == RPC_CANTSEND)
503 nfs_error(_("%s: portmap query %s%s - %s"),
504 progname, str, clnt_spcreateerror(""),
505 strerror(rpc_createerr.cf_error.re_errno));
507 nfs_error(_("%s: portmap query %s%s"),
508 progname, str, clnt_spcreateerror(""));
512 * Use the portmapper to discover whether or not the service we want is
513 * available. The lists 'versions' and 'protos' define ordered sequences
514 * of service versions and udp/tcp protocols to probe for.
516 * Returns 1 if the requested service port is unambiguous and pingable;
517 * @pmap is filled in with the version, port, and transport protocol used
518 * during the successful ping. Note that if a port is already specified
519 * in @pmap and it matches the rpcbind query result, nfs_probe_port() does
520 * not perform an RPC ping.
522 * If an error occurs or the requested service isn't available, zero is
523 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
525 static int nfs_probe_port(const struct sockaddr *sap, const socklen_t salen,
526 struct pmap *pmap, const unsigned long *versions,
527 const unsigned int *protos)
529 struct sockaddr_storage address;
530 struct sockaddr *saddr = (struct sockaddr *)&address;
531 const unsigned long prog = pmap->pm_prog, *p_vers;
532 const unsigned int prot = (u_int)pmap->pm_prot, *p_prot;
533 const u_short port = (u_short) pmap->pm_port;
534 unsigned long vers = pmap->pm_vers;
535 unsigned short p_port;
537 memcpy(saddr, sap, salen);
538 p_prot = prot ? &prot : protos;
539 p_vers = vers ? &vers : versions;
543 printf(_("%s: prog %lu, trying vers=%lu, prot=%u\n"),
544 progname, prog, *p_vers, *p_prot);
545 p_port = nfs_getport(saddr, salen, prog, *p_vers, *p_prot);
547 if (!port || port == p_port) {
548 nfs_set_port(saddr, p_port);
549 nfs_pp_debug(saddr, salen, prog, *p_vers,
551 if (nfs_rpc_ping(saddr, salen, prog,
552 *p_vers, *p_prot, NULL))
555 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
557 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
558 rpc_createerr.cf_stat != RPC_TIMEDOUT &&
559 rpc_createerr.cf_stat != RPC_CANTRECV &&
560 rpc_createerr.cf_stat != RPC_PROGVERSMISMATCH)
565 nfs_pp_debug2("retrying");
570 if (rpc_createerr.cf_stat == RPC_TIMEDOUT ||
571 rpc_createerr.cf_stat == RPC_CANTRECV)
574 if (vers || !*++p_vers)
578 nfs_pp_debug2("failed");
583 pmap->pm_vers = *p_vers;
585 pmap->pm_prot = *p_prot;
587 pmap->pm_port = p_port;
588 nfs_clear_rpc_createerr();
593 * Probe a server's NFS service to determine which versions and
594 * transport protocols are supported.
596 * Returns 1 if the requested service port is unambiguous and pingable;
597 * @pmap is filled in with the version, port, and transport protocol used
598 * during the successful ping. If all three are already specified, simply
599 * return success without an rpcbind query or RPC ping (we may be trying
600 * to mount an NFS service that is not advertised via rpcbind).
602 * If an error occurs or the requested service isn't available, zero is
603 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
605 static int nfs_probe_nfsport(const struct sockaddr *sap, const socklen_t salen,
608 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
611 if (nfs_mount_data_version >= 4)
612 return nfs_probe_port(sap, salen, pmap,
613 probe_nfs3_first, probe_tcp_first);
615 return nfs_probe_port(sap, salen, pmap,
616 probe_nfs2_only, probe_udp_only);
620 * Probe a server's mountd service to determine which versions and
621 * transport protocols are supported.
623 * Returns 1 if the requested service port is unambiguous and pingable;
624 * @pmap is filled in with the version, port, and transport protocol used
625 * during the successful ping. If all three are already specified, simply
626 * return success without an rpcbind query or RPC ping (we may be trying
627 * to mount an NFS service that is not advertised via rpcbind).
629 * If an error occurs or the requested service isn't available, zero is
630 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
632 static int nfs_probe_mntport(const struct sockaddr *sap, const socklen_t salen,
635 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
638 if (nfs_mount_data_version >= 4)
639 return nfs_probe_port(sap, salen, pmap,
640 probe_mnt3_first, probe_udp_first);
642 return nfs_probe_port(sap, salen, pmap,
643 probe_mnt1_first, probe_udp_only);
647 * Probe a server's mountd service to determine which versions and
648 * transport protocols are supported. Invoked when the protocol
649 * version is already known for both the NFS and mountd service.
651 * Returns 1 and fills in both @pmap structs if the requested service
652 * ports are unambiguous and pingable. Otherwise zero is returned;
653 * rpccreateerr.cf_stat is set to reflect the nature of the error.
655 static int nfs_probe_version_fixed(const struct sockaddr *mnt_saddr,
656 const socklen_t mnt_salen,
657 struct pmap *mnt_pmap,
658 const struct sockaddr *nfs_saddr,
659 const socklen_t nfs_salen,
660 struct pmap *nfs_pmap)
662 if (!nfs_probe_nfsport(nfs_saddr, nfs_salen, nfs_pmap))
664 return nfs_probe_mntport(mnt_saddr, mnt_salen, mnt_pmap);
668 * nfs_probe_bothports - discover the RPC endpoints of mountd and NFS server
669 * @mnt_saddr: pointer to socket address of mountd server
670 * @mnt_salen: length of mountd server's address
671 * @mnt_pmap: IN: partially filled-in mountd RPC service tuple;
672 * OUT: fully filled-in mountd RPC service tuple
673 * @nfs_saddr: pointer to socket address of NFS server
674 * @nfs_salen: length of NFS server's address
675 * @nfs_pmap: IN: partially filled-in NFS RPC service tuple;
676 * OUT: fully filled-in NFS RPC service tuple
678 * Returns 1 and fills in both @pmap structs if the requested service
679 * ports are unambiguous and pingable. Otherwise zero is returned;
680 * rpccreateerr.cf_stat is set to reflect the nature of the error.
682 int nfs_probe_bothports(const struct sockaddr *mnt_saddr,
683 const socklen_t mnt_salen,
684 struct pmap *mnt_pmap,
685 const struct sockaddr *nfs_saddr,
686 const socklen_t nfs_salen,
687 struct pmap *nfs_pmap)
689 struct pmap save_nfs, save_mnt;
690 const unsigned long *probe_vers;
692 if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
693 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
694 else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
695 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
697 if (nfs_pmap->pm_vers)
698 return nfs_probe_version_fixed(mnt_saddr, mnt_salen, mnt_pmap,
699 nfs_saddr, nfs_salen, nfs_pmap);
701 memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
702 memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
703 probe_vers = (nfs_mount_data_version >= 4) ?
704 probe_mnt3_first : probe_mnt1_first;
706 for (; *probe_vers; probe_vers++) {
707 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
708 if (nfs_probe_nfsport(nfs_saddr, nfs_salen, nfs_pmap) != 0) {
709 mnt_pmap->pm_vers = *probe_vers;
710 if (nfs_probe_mntport(mnt_saddr, mnt_salen, mnt_pmap) != 0)
712 memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
714 switch (rpc_createerr.cf_stat) {
715 case RPC_PROGVERSMISMATCH:
716 case RPC_PROGNOTREGISTERED:
721 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
728 * probe_bothports - discover the RPC endpoints of mountd and NFS server
729 * @mnt_server: pointer to address and pmap argument for mountd results
730 * @nfs_server: pointer to address and pmap argument for NFS server
732 * This is the legacy API that takes "clnt_addr_t" for both servers,
733 * but supports only AF_INET addresses.
735 * Returns 1 and fills in the pmap field in both clnt_addr_t structs
736 * if the requested service ports are unambiguous and pingable.
737 * Otherwise zero is returned; rpccreateerr.cf_stat is set to reflect
738 * the nature of the error.
740 int probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
742 return nfs_probe_bothports((struct sockaddr *)&mnt_server->saddr,
743 sizeof(mnt_server->saddr),
745 (struct sockaddr *)&nfs_server->saddr,
746 sizeof(nfs_server->saddr),
750 static int nfs_probe_statd(void)
752 struct sockaddr_in addr = {
753 .sin_family = AF_INET,
754 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
756 rpcprog_t program = nfs_getrpcbyname(NSMPROG, nfs_ns_pgmtbl);
758 return nfs_getport_ping((struct sockaddr *)&addr, sizeof(addr),
759 program, (rpcvers_t)1, IPPROTO_UDP);
763 * start_statd - attempt to start rpc.statd
765 * Returns 1 if statd is running; otherwise zero.
767 int start_statd(void)
773 if (nfs_probe_statd())
777 if (stat(START_STATD, &stb) == 0) {
778 if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {
782 execl(START_STATD, START_STATD, NULL);
785 nfs_error(_("%s: fork failed: %s"),
786 progname, strerror(errno));
788 default: /* parent */
789 waitpid(pid, NULL,0);
792 if (nfs_probe_statd())
802 * nfs_advise_umount - ask the server to remove a share from it's rmtab
803 * @sap: pointer to IP address of server to call
804 * @salen: length of server address
805 * @pmap: partially filled-in mountd RPC service tuple
806 * @argp: directory path of share to "unmount"
808 * Returns one if the unmount call succeeded; zero if the unmount
809 * failed for any reason; rpccreateerr.cf_stat is set to reflect
810 * the nature of the error.
812 * We use a fast timeout since this call is advisory only.
814 int nfs_advise_umount(const struct sockaddr *sap, const socklen_t salen,
815 const struct pmap *pmap, const dirpath *argp)
817 struct sockaddr_storage address;
818 struct sockaddr *saddr = (struct sockaddr *)&address;
819 struct pmap mnt_pmap = *pmap;
820 struct timeval timeout = {
821 .tv_sec = MOUNT_TIMEOUT >> 3,
824 enum clnt_stat res = 0;
826 memcpy(saddr, sap, salen);
827 if (nfs_probe_mntport(saddr, salen, &mnt_pmap) == 0) {
829 nfs_error(_("%s: Failed to discover mountd port%s"),
830 progname, clnt_spcreateerror(""));
833 nfs_set_port(saddr, mnt_pmap.pm_port);
835 client = nfs_get_priv_rpcclient(saddr, salen, mnt_pmap.pm_prot,
836 mnt_pmap.pm_prog, mnt_pmap.pm_vers,
838 if (client == NULL) {
840 nfs_error(_("%s: Failed to create RPC client%s"),
841 progname, clnt_spcreateerror(""));
845 client->cl_auth = authunix_create_default();
847 res = CLNT_CALL(client, MOUNTPROC_UMNT,
848 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
849 (xdrproc_t)xdr_void, NULL,
851 if (res != RPC_SUCCESS) {
852 rpc_createerr.cf_stat = res;
853 CLNT_GETERR(client, &rpc_createerr.cf_error);
855 nfs_error(_("%s: UMNT call failed: %s"),
856 progname, clnt_sperrno(res));
859 auth_destroy(client->cl_auth);
860 CLNT_DESTROY(client);
862 if (res != RPC_SUCCESS)
868 * nfs_call_umount - ask the server to remove a share from it's rmtab
869 * @mnt_server: address of RPC MNT program server
870 * @argp: directory path of share to "unmount"
872 * Returns one if the unmount call succeeded; zero if the unmount
873 * failed for any reason.
875 * Note that a side effect of calling this function is that rpccreateerr
878 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
880 struct sockaddr *sap = (struct sockaddr *)&mnt_server->saddr;
881 socklen_t salen = sizeof(mnt_server->saddr);
882 struct pmap *pmap = &mnt_server->pmap;
884 enum clnt_stat res = 0;
887 if (!nfs_probe_mntport(sap, salen, pmap))
889 clnt = mnt_openclnt(mnt_server, &msock);
892 res = clnt_call(clnt, MOUNTPROC_UMNT,
893 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
894 (xdrproc_t)xdr_void, NULL,
896 mnt_closeclnt(clnt, msock);
898 if (res == RPC_SUCCESS)
904 * mnt_openclnt - get a handle for a remote mountd service
905 * @mnt_server: address and pmap arguments of mountd service
906 * @msock: returns a file descriptor of the underlying transport socket
908 * Returns an active handle for the remote's mountd service
910 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
912 struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
913 struct pmap *mnt_pmap = &mnt_server->pmap;
916 mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
917 *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, MOUNT_TIMEOUT,
919 if (*msock == RPC_ANYSOCK) {
920 if (rpc_createerr.cf_error.re_errno == EADDRINUSE)
922 * Probably in-use by a TIME_WAIT connection,
923 * It is worth waiting a while and trying again.
925 rpc_createerr.cf_stat = RPC_TIMEDOUT;
929 switch (mnt_pmap->pm_prot) {
931 clnt = clntudp_bufcreate(mnt_saddr,
932 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
933 RETRY_TIMEOUT, msock,
934 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
937 clnt = clnttcp_create(mnt_saddr,
938 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
940 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
944 /* try to mount hostname:dirname */
945 clnt->cl_auth = authunix_create_default();
952 * mnt_closeclnt - terminate a handle for a remote mountd service
953 * @clnt: pointer to an active handle for a remote mountd service
954 * @msock: file descriptor of the underlying transport socket
957 void mnt_closeclnt(CLIENT *clnt, int msock)
959 auth_destroy(clnt->cl_auth);
965 * clnt_ping - send an RPC ping to the remote RPC service endpoint
966 * @saddr: server's address
967 * @prog: target RPC program number
968 * @vers: target RPC version number
969 * @prot: target RPC protocol
970 * @caddr: filled in with our network address
972 * Sigh... GETPORT queries don't actually check the version number.
973 * In order to make sure that the server actually supports the service
974 * we're requesting, we open an RPC client, and fire off a NULL
977 * caddr is the network address that the server will use to call us back.
978 * On multi-homed clients, this address depends on which NIC we use to
979 * route requests to the server.
981 * Returns one if successful, otherwise zero.
983 int clnt_ping(struct sockaddr_in *saddr, const unsigned long prog,
984 const unsigned long vers, const unsigned int prot,
985 struct sockaddr_in *caddr)
989 static char clnt_res;
990 struct sockaddr dissolve;
992 rpc_createerr.cf_stat = stat = 0;
993 sock = get_socket(saddr, prot, CONNECT_TIMEOUT, FALSE, TRUE);
994 if (sock == RPC_ANYSOCK) {
995 if (rpc_createerr.cf_error.re_errno == ETIMEDOUT) {
997 * TCP timeout. Bubble up the error to see
998 * how it should be handled.
1000 rpc_createerr.cf_stat = RPC_TIMEDOUT;
1006 /* Get the address of our end of this connection */
1007 socklen_t len = sizeof(*caddr);
1008 if (getsockname(sock, caddr, &len) != 0)
1009 caddr->sin_family = 0;
1014 /* The socket is connected (so we could getsockname successfully),
1015 * but some servers on multi-homed hosts reply from
1016 * the wrong address, so if we stay connected, we lose the reply.
1018 dissolve.sa_family = AF_UNSPEC;
1019 connect(sock, &dissolve, sizeof(dissolve));
1021 clnt = clntudp_bufcreate(saddr, prog, vers,
1022 RETRY_TIMEOUT, &sock,
1023 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
1026 clnt = clnttcp_create(saddr, prog, vers, &sock,
1027 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
1034 memset(&clnt_res, 0, sizeof(clnt_res));
1035 stat = clnt_call(clnt, NULLPROC,
1036 (xdrproc_t)xdr_void, (caddr_t)NULL,
1037 (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
1040 clnt_geterr(clnt, &rpc_createerr.cf_error);
1041 rpc_createerr.cf_stat = stat;
1046 if (stat == RPC_SUCCESS)
1053 * Try a getsockname() on a connected datagram socket.
1055 * Returns 1 and fills in @buf if successful; otherwise, zero.
1057 * A connected datagram socket prevents leaving a socket in TIME_WAIT.
1058 * This conserves the ephemeral port number space, helping reduce failed
1059 * socket binds during mount storms.
1061 static int nfs_ca_sockname(const struct sockaddr *sap, const socklen_t salen,
1062 struct sockaddr *buf, socklen_t *buflen)
1064 struct sockaddr_in sin = {
1065 .sin_family = AF_INET,
1066 .sin_addr.s_addr = htonl(INADDR_ANY),
1068 struct sockaddr_in6 sin6 = {
1069 .sin6_family = AF_INET6,
1070 .sin6_addr = IN6ADDR_ANY_INIT,
1074 sock = socket(sap->sa_family, SOCK_DGRAM, IPPROTO_UDP);
1078 switch (sap->sa_family) {
1080 if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
1086 if (bind(sock, (struct sockaddr *)&sin6, sizeof(sin6)) < 0) {
1092 errno = EAFNOSUPPORT;
1096 if (connect(sock, sap, salen) < 0) {
1101 return !getsockname(sock, buf, buflen);
1105 * Try to generate an address that prevents the server from calling us.
1107 * Returns 1 and fills in @buf if successful; otherwise, zero.
1109 static int nfs_ca_gai(const struct sockaddr *sap,
1110 struct sockaddr *buf, socklen_t *buflen)
1112 struct addrinfo *gai_results;
1113 struct addrinfo gai_hint = {
1114 .ai_family = sap->sa_family,
1115 .ai_flags = AI_PASSIVE, /* ANYADDR */
1118 if (getaddrinfo(NULL, "", &gai_hint, &gai_results))
1121 *buflen = gai_results->ai_addrlen;
1122 memcpy(buf, gai_results->ai_addr, *buflen);
1124 freeaddrinfo(gai_results);
1130 * nfs_callback_address - acquire our local network address
1131 * @sap: pointer to address of remote
1132 * @sap_len: length of address
1133 * @buf: pointer to buffer to be filled in with local network address
1134 * @buflen: IN: length of buffer to fill in; OUT: length of filled-in address
1136 * Discover a network address that an NFSv4 server can use to call us back.
1137 * On multi-homed clients, this address depends on which NIC we use to
1138 * route requests to the server.
1140 * Returns 1 and fills in @buf if an unambiguous local address is
1141 * available; returns 1 and fills in an appropriate ANYADDR address
1142 * if a local address isn't available; otherwise, returns zero.
1144 int nfs_callback_address(const struct sockaddr *sap, const socklen_t salen,
1145 struct sockaddr *buf, socklen_t *buflen)
1147 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1149 if (nfs_ca_sockname(sap, salen, buf, buflen) == 0)
1150 if (nfs_ca_gai(sap, buf, buflen) == 0)
1154 * The server can't use an interface ID that was generated
1155 * here on the client, so always clear sin6_scope_id.
1157 if (sin6->sin6_family == AF_INET6)
1158 sin6->sin6_scope_id = 0;
1165 nfs_error(_("%s: failed to construct callback address"),
1171 * "nfsprog" is supported only by the legacy mount command. The
1172 * kernel mount client does not support this option.
1174 * Returns TRUE if @program contains a valid value for this option,
1175 * or FALSE if the option was specified with an invalid value.
1178 nfs_nfs_program(struct mount_options *options, unsigned long *program)
1182 switch (po_get_numeric(options, "nfsprog", &tmp)) {
1195 * NFS RPC program wasn't specified. The RPC program
1196 * cannot be determined via an rpcbind query.
1198 *program = nfs_getrpcbyname(NFSPROG, nfs_nfs_pgmtbl);
1203 * Returns TRUE if @version contains a valid value for this option,
1204 * or FALSE if the option was specified with an invalid value.
1207 nfs_nfs_version(struct mount_options *options, unsigned long *version)
1211 switch (po_rightmost(options, nfs_version_opttbl)) {
1219 switch (po_get_numeric(options, "vers", &tmp)) {
1221 if (tmp >= 2 && tmp <= 3) {
1227 nfs_error(_("%s: option parsing error\n"),
1232 case 3: /* nfsvers */
1233 switch (po_get_numeric(options, "nfsvers", &tmp)) {
1235 if (tmp >= 2 && tmp <= 3) {
1241 nfs_error(_("%s: option parsing error\n"),
1249 * NFS version wasn't specified. The pmap version value
1250 * will be filled in later by an rpcbind query in this case.
1257 * Returns TRUE if @protocol contains a valid value for this option,
1258 * or FALSE if the option was specified with an invalid value.
1261 nfs_nfs_protocol(struct mount_options *options, unsigned long *protocol)
1265 switch (po_rightmost(options, nfs_transport_opttbl)) {
1267 *protocol = IPPROTO_UDP;
1270 *protocol = IPPROTO_TCP;
1273 option = po_get(options, "proto");
1275 if (strcmp(option, "tcp") == 0) {
1276 *protocol = IPPROTO_TCP;
1279 if (strcmp(option, "udp") == 0) {
1280 *protocol = IPPROTO_UDP;
1288 * NFS transport protocol wasn't specified. The pmap
1289 * protocol value will be filled in later by an rpcbind
1290 * query in this case.
1297 * Returns TRUE if @port contains a valid value for this option,
1298 * or FALSE if the option was specified with an invalid value.
1301 nfs_nfs_port(struct mount_options *options, unsigned long *port)
1305 switch (po_get_numeric(options, "port", &tmp)) {
1309 if (tmp >= 1 && tmp <= 65535) {
1318 * NFS service port wasn't specified. The pmap port value
1319 * will be filled in later by an rpcbind query in this case.
1326 * "mountprog" is supported only by the legacy mount command. The
1327 * kernel mount client does not support this option.
1329 * Returns TRUE if @program contains a valid value for this option,
1330 * or FALSE if the option was specified with an invalid value.
1333 nfs_mount_program(struct mount_options *options, unsigned long *program)
1337 switch (po_get_numeric(options, "mountprog", &tmp)) {
1350 * MNT RPC program wasn't specified. The RPC program
1351 * cannot be determined via an rpcbind query.
1353 *program = nfs_getrpcbyname(MOUNTPROG, nfs_mnt_pgmtbl);
1358 * Returns TRUE if @version contains a valid value for this option,
1359 * or FALSE if the option was specified with an invalid value.
1362 nfs_mount_version(struct mount_options *options, unsigned long *version)
1366 switch (po_get_numeric(options, "mountvers", &tmp)) {
1370 if (tmp >= 1 && tmp <= 4) {
1379 * MNT version wasn't specified. The pmap version value
1380 * will be filled in later by an rpcbind query in this case.
1387 * Returns TRUE if @protocol contains a valid value for this option,
1388 * or FALSE if the option was specified with an invalid value.
1391 nfs_mount_protocol(struct mount_options *options, unsigned long *protocol)
1395 option = po_get(options, "mountproto");
1397 if (strcmp(option, "tcp") == 0) {
1398 *protocol = IPPROTO_TCP;
1401 if (strcmp(option, "udp") == 0) {
1402 *protocol = IPPROTO_UDP;
1409 * MNT transport protocol wasn't specified. If the NFS
1410 * transport protocol was specified, use that; otherwise
1411 * set @protocol to zero. The pmap protocol value will
1412 * be filled in later by an rpcbind query in this case.
1414 return nfs_nfs_protocol(options, protocol);
1418 * Returns TRUE if @port contains a valid value for this option,
1419 * or FALSE if the option was specified with an invalid value.
1422 nfs_mount_port(struct mount_options *options, unsigned long *port)
1426 switch (po_get_numeric(options, "mountport", &tmp)) {
1430 if (tmp >= 1 && tmp <= 65535) {
1439 * MNT service port wasn't specified. The pmap port value
1440 * will be filled in later by an rpcbind query in this case.
1447 * nfs_options2pmap - set up pmap structs based on mount options
1448 * @options: pointer to mount options
1449 * @nfs_pmap: OUT: pointer to pmap arguments for NFS server
1450 * @mnt_pmap: OUT: pointer to pmap arguments for mountd server
1452 * Returns TRUE if the pmap options specified in @options have valid
1453 * values; otherwise FALSE is returned.
1455 int nfs_options2pmap(struct mount_options *options,
1456 struct pmap *nfs_pmap, struct pmap *mnt_pmap)
1458 if (!nfs_nfs_program(options, &nfs_pmap->pm_prog))
1460 if (!nfs_nfs_version(options, &nfs_pmap->pm_vers))
1462 if (!nfs_nfs_protocol(options, &nfs_pmap->pm_prot))
1464 if (!nfs_nfs_port(options, &nfs_pmap->pm_port))
1467 if (!nfs_mount_program(options, &mnt_pmap->pm_prog))
1469 if (!nfs_mount_version(options, &mnt_pmap->pm_vers))
1471 if (!nfs_mount_protocol(options, &mnt_pmap->pm_prot))
1473 if (!nfs_mount_port(options, &mnt_pmap->pm_port))