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>
41 #include <netinet/in.h>
43 #include <rpc/pmap_prot.h>
44 #include <rpc/pmap_clnt.h>
50 #include "nfs_mount.h"
51 #include "mount_constants.h"
53 #include "parse_opt.h"
58 #define PMAP_TIMEOUT (10)
59 #define CONNECT_TIMEOUT (20)
60 #define MOUNT_TIMEOUT (30)
62 #define SAFE_SOCKADDR(x) (struct sockaddr *)(char *)(x)
64 extern int nfs_mount_data_version;
65 extern char *progname;
68 static const char *nfs_ns_pgmtbl[] = {
73 static const char *nfs_mnt_pgmtbl[] = {
79 static const char *nfs_nfs_pgmtbl[] = {
85 static const char *nfs_transport_opttbl[] = {
93 static const char *nfs_version_opttbl[] = {
102 static const unsigned long nfs_to_mnt[] = {
109 static const unsigned long mnt_to_nfs[] = {
117 * Map an NFS version into the corresponding Mountd version
119 unsigned long nfsvers_to_mnt(const unsigned long vers)
122 return nfs_to_mnt[vers];
127 * Map a Mountd version into the corresponding NFS version
129 static unsigned long mntvers_to_nfs(const unsigned long vers)
132 return mnt_to_nfs[vers];
136 static const unsigned int probe_udp_only[] = {
141 static const unsigned int probe_udp_first[] = {
147 static const unsigned int probe_tcp_first[] = {
153 static const unsigned long probe_nfs2_only[] = {
158 static const unsigned long probe_nfs3_first[] = {
164 static const unsigned long probe_mnt1_first[] = {
170 static const unsigned long probe_mnt3_first[] = {
177 static const unsigned int *nfs_default_proto(void);
179 static const unsigned int *nfs_default_proto()
181 extern unsigned long config_default_proto;
183 * If the default proto has been set and
184 * its not TCP, start with UDP
186 if (config_default_proto && config_default_proto != IPPROTO_TCP)
187 return probe_udp_first;
189 return probe_tcp_first;
192 static const unsigned int *nfs_default_proto()
194 return probe_tcp_first;
196 #endif /* MOUNT_CONFIG */
199 * nfs_lookup - resolve hostname to an IPv4 or IPv6 socket address
200 * @hostname: pointer to C string containing DNS hostname to resolve
201 * @family: address family hint
202 * @sap: pointer to buffer to fill with socket address
203 * @len: IN: size of buffer to fill; OUT: size of socket address
205 * Returns 1 and places a socket address at @sap if successful;
208 int nfs_lookup(const char *hostname, const sa_family_t family,
209 struct sockaddr *sap, socklen_t *salen)
211 struct addrinfo *gai_results;
212 struct addrinfo gai_hint = {
213 #ifdef HAVE_DECL_AI_ADDRCONFIG
214 .ai_flags = AI_ADDRCONFIG,
215 #endif /* HAVE_DECL_AI_ADDRCONFIG */
218 socklen_t len = *salen;
223 error = getaddrinfo(hostname, NULL, &gai_hint, &gai_results);
228 nfs_error(_("%s: DNS resolution failed for %s: %s"),
229 progname, hostname, strerror(errno));
232 nfs_error(_("%s: DNS resolution failed for %s: %s"),
233 progname, hostname, gai_strerror(error));
237 switch (gai_results->ai_addr->sa_family) {
240 if (len >= gai_results->ai_addrlen) {
241 *salen = gai_results->ai_addrlen;
242 memcpy(sap, gai_results->ai_addr, *salen);
247 /* things are really broken if we get here, so warn */
248 nfs_error(_("%s: unrecognized DNS resolution results for %s"),
253 freeaddrinfo(gai_results);
258 * nfs_gethostbyname - resolve a hostname to an IPv4 address
259 * @hostname: pointer to a C string containing a DNS hostname
260 * @sin: returns an IPv4 address
262 * Returns 1 if successful, otherwise zero.
264 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *sin)
266 socklen_t len = sizeof(*sin);
268 return nfs_lookup(hostname, AF_INET, (struct sockaddr *)sin, &len);
272 * nfs_string_to_sockaddr - convert string address to sockaddr
273 * @address: pointer to presentation format address to convert
274 * @sap: pointer to socket address buffer to fill in
275 * @salen: IN: length of address buffer
276 * OUT: length of converted socket address
278 * Convert a presentation format address string to a socket address.
279 * Similar to nfs_lookup(), but the DNS query is squelched, and it
280 * won't make any noise if the getaddrinfo() call fails.
282 * Returns 1 and fills in @sap and @salen if successful; otherwise zero.
284 * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
285 * on presenting IPv6 addresses as text strings.
287 int nfs_string_to_sockaddr(const char *address, struct sockaddr *sap,
290 struct addrinfo *gai_results;
291 struct addrinfo gai_hint = {
292 .ai_flags = AI_NUMERICHOST,
294 socklen_t len = *salen;
299 if (getaddrinfo(address, NULL, &gai_hint, &gai_results) == 0) {
300 switch (gai_results->ai_addr->sa_family) {
303 if (len >= gai_results->ai_addrlen) {
304 *salen = gai_results->ai_addrlen;
305 memcpy(sap, gai_results->ai_addr, *salen);
310 freeaddrinfo(gai_results);
317 * nfs_present_sockaddr - convert sockaddr to string
318 * @sap: pointer to socket address to convert
319 * @salen: length of socket address
320 * @buf: pointer to buffer to fill in
321 * @buflen: length of buffer
323 * Convert the passed-in sockaddr-style address to presentation format.
324 * The presentation format address is placed in @buf and is
327 * Returns 1 if successful; otherwise zero.
329 * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
330 * on presenting IPv6 addresses as text strings.
332 int nfs_present_sockaddr(const struct sockaddr *sap, const socklen_t salen,
333 char *buf, const size_t buflen)
335 #ifdef HAVE_GETNAMEINFO
338 result = getnameinfo(sap, salen, buf, buflen,
339 NULL, 0, NI_NUMERICHOST);
343 nfs_error(_("%s: invalid server address: %s"), progname,
344 gai_strerror(result));
346 #else /* HAVE_GETNAMEINFO */
349 if (sap->sa_family == AF_INET) {
350 addr = inet_ntoa(((struct sockaddr_in *)sap)->sin_addr);
351 if (addr && strlen(addr) < buflen) {
357 nfs_error(_("%s: invalid server address"), progname);
359 #endif /* HAVE_GETNAMEINFO */
363 * Attempt to connect a socket, but time out after "timeout" seconds.
365 * On error return, caller closes the socket.
367 static int connect_to(int fd, struct sockaddr *addr,
368 socklen_t addrlen, int timeout)
372 struct timeval tv = {
376 saved = fcntl(fd, F_GETFL, 0);
377 fcntl(fd, F_SETFL, saved | O_NONBLOCK);
379 ret = connect(fd, addr, addrlen);
380 if (ret < 0 && errno != EINPROGRESS)
388 ret = select(fd + 1, &rset, &wset, NULL, &tv);
393 if (FD_ISSET(fd, &rset) || FD_ISSET(fd, &wset)) {
395 socklen_t len = sizeof(error);
396 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
406 fcntl(fd, F_SETFL, saved);
411 * Create a socket that is locally bound to a reserved or non-reserved port.
413 * The caller should check rpc_createerr to determine the cause of any error.
415 static int get_socket(struct sockaddr_in *saddr, unsigned int p_prot,
416 unsigned int timeout, int resvp, int conn)
419 struct sockaddr_in laddr;
420 socklen_t namelen = sizeof(laddr);
422 type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
423 if ((so = socket (AF_INET, type, p_prot)) < 0)
426 laddr.sin_family = AF_INET;
428 laddr.sin_addr.s_addr = htonl(INADDR_ANY);
430 if (bindresvport(so, &laddr) < 0)
431 goto err_bindresvport;
433 cc = bind(so, SAFE_SOCKADDR(&laddr), namelen);
437 if (type == SOCK_STREAM || (conn && type == SOCK_DGRAM)) {
438 cc = connect_to(so, SAFE_SOCKADDR(saddr), namelen,
446 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
447 rpc_createerr.cf_error.re_errno = errno;
449 nfs_error(_("%s: Unable to create %s socket: errno %d (%s)\n"),
450 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
451 errno, strerror(errno));
456 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
457 rpc_createerr.cf_error.re_errno = errno;
459 nfs_error(_("%s: Unable to bindresvport %s socket: errno %d"
461 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
462 errno, strerror(errno));
468 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
469 rpc_createerr.cf_error.re_errno = errno;
471 nfs_error(_("%s: Unable to bind to %s socket: errno %d (%s)\n"),
472 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
473 errno, strerror(errno));
479 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
480 rpc_createerr.cf_error.re_errno = errno;
482 nfs_error(_("%s: Unable to connect to %s:%d, errno %d (%s)\n"),
483 progname, inet_ntoa(saddr->sin_addr),
484 ntohs(saddr->sin_port), errno, strerror(errno));
490 static void nfs_pp_debug(const struct sockaddr *sap, const socklen_t salen,
491 const rpcprog_t program, const rpcvers_t version,
492 const unsigned short protocol,
493 const unsigned short port)
495 char buf[NI_MAXHOST];
500 if (nfs_present_sockaddr(sap, salen, buf, sizeof(buf)) == 0) {
502 strcat(buf, "unknown host");
505 fprintf(stderr, _("%s: trying %s prog %lu vers %lu prot %s port %d\n"),
506 progname, buf, (unsigned long)program,
507 (unsigned long)version,
508 (protocol == IPPROTO_UDP ? _("UDP") : _("TCP")),
512 static void nfs_pp_debug2(const char *str)
517 if (rpc_createerr.cf_error.re_status == RPC_CANTRECV ||
518 rpc_createerr.cf_error.re_status == RPC_CANTSEND)
519 nfs_error(_("%s: portmap query %s%s - %s"),
520 progname, str, clnt_spcreateerror(""),
521 strerror(rpc_createerr.cf_error.re_errno));
523 nfs_error(_("%s: portmap query %s%s"),
524 progname, str, clnt_spcreateerror(""));
528 * Use the portmapper to discover whether or not the service we want is
529 * available. The lists 'versions' and 'protos' define ordered sequences
530 * of service versions and udp/tcp protocols to probe for.
532 * Returns 1 if the requested service port is unambiguous and pingable;
533 * @pmap is filled in with the version, port, and transport protocol used
534 * during the successful ping. Note that if a port is already specified
535 * in @pmap and it matches the rpcbind query result, nfs_probe_port() does
536 * not perform an RPC ping.
538 * If an error occurs or the requested service isn't available, zero is
539 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
541 static int nfs_probe_port(const struct sockaddr *sap, const socklen_t salen,
542 struct pmap *pmap, const unsigned long *versions,
543 const unsigned int *protos)
545 union nfs_sockaddr address;
546 struct sockaddr *saddr = &address.sa;
547 const unsigned long prog = pmap->pm_prog, *p_vers;
548 const unsigned int prot = (u_int)pmap->pm_prot, *p_prot;
549 const u_short port = (u_short) pmap->pm_port;
550 unsigned long vers = pmap->pm_vers;
551 unsigned short p_port;
553 memcpy(saddr, sap, salen);
554 p_prot = prot ? &prot : protos;
555 p_vers = vers ? &vers : versions;
559 printf(_("%s: prog %lu, trying vers=%lu, prot=%u\n"),
560 progname, prog, *p_vers, *p_prot);
561 p_port = nfs_getport(saddr, salen, prog, *p_vers, *p_prot);
563 if (!port || port == p_port) {
564 nfs_set_port(saddr, p_port);
565 nfs_pp_debug(saddr, salen, prog, *p_vers,
567 if (nfs_rpc_ping(saddr, salen, prog,
568 *p_vers, *p_prot, NULL))
571 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
573 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
574 rpc_createerr.cf_stat != RPC_TIMEDOUT &&
575 rpc_createerr.cf_stat != RPC_CANTRECV &&
576 rpc_createerr.cf_stat != RPC_PROGVERSMISMATCH)
581 nfs_pp_debug2("retrying");
586 if (rpc_createerr.cf_stat == RPC_TIMEDOUT ||
587 rpc_createerr.cf_stat == RPC_CANTRECV)
590 if (vers || !*++p_vers)
594 nfs_pp_debug2("failed");
599 pmap->pm_vers = *p_vers;
601 pmap->pm_prot = *p_prot;
603 pmap->pm_port = p_port;
604 nfs_clear_rpc_createerr();
608 * Probe a server's NFS service to determine which versions and
609 * transport protocols are supported.
611 * Returns 1 if the requested service port is unambiguous and pingable;
612 * @pmap is filled in with the version, port, and transport protocol used
613 * during the successful ping. If all three are already specified, simply
614 * return success without an rpcbind query or RPC ping (we may be trying
615 * to mount an NFS service that is not advertised via rpcbind).
617 * If an error occurs or the requested service isn't available, zero is
618 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
620 static int nfs_probe_nfsport(const struct sockaddr *sap, const socklen_t salen,
623 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
626 if (nfs_mount_data_version >= 4) {
627 const unsigned int *probe_proto;
629 probe_proto = nfs_default_proto();
631 return nfs_probe_port(sap, salen, pmap,
632 probe_nfs3_first, probe_proto);
634 return nfs_probe_port(sap, salen, pmap,
635 probe_nfs2_only, probe_udp_only);
639 * Probe a server's mountd service to determine which versions and
640 * transport protocols are supported.
642 * Returns 1 if the requested service port is unambiguous and pingable;
643 * @pmap is filled in with the version, port, and transport protocol used
644 * during the successful ping. If all three are already specified, simply
645 * return success without an rpcbind query or RPC ping (we may be trying
646 * to mount an NFS service that is not advertised via rpcbind).
648 * If an error occurs or the requested service isn't available, zero is
649 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
651 static int nfs_probe_mntport(const struct sockaddr *sap, const socklen_t salen,
654 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
657 if (nfs_mount_data_version >= 4)
658 return nfs_probe_port(sap, salen, pmap,
659 probe_mnt3_first, probe_udp_first);
661 return nfs_probe_port(sap, salen, pmap,
662 probe_mnt1_first, probe_udp_only);
666 * Probe a server's mountd service to determine which versions and
667 * transport protocols are supported. Invoked when the protocol
668 * version is already known for both the NFS and mountd service.
670 * Returns 1 and fills in both @pmap structs if the requested service
671 * ports are unambiguous and pingable. Otherwise zero is returned;
672 * rpccreateerr.cf_stat is set to reflect the nature of the error.
674 static int nfs_probe_version_fixed(const struct sockaddr *mnt_saddr,
675 const socklen_t mnt_salen,
676 struct pmap *mnt_pmap,
677 const struct sockaddr *nfs_saddr,
678 const socklen_t nfs_salen,
679 struct pmap *nfs_pmap)
681 if (!nfs_probe_nfsport(nfs_saddr, nfs_salen, nfs_pmap))
683 return nfs_probe_mntport(mnt_saddr, mnt_salen, mnt_pmap);
687 * nfs_probe_bothports - discover the RPC endpoints of mountd and NFS server
688 * @mnt_saddr: pointer to socket address of mountd server
689 * @mnt_salen: length of mountd server's address
690 * @mnt_pmap: IN: partially filled-in mountd RPC service tuple;
691 * OUT: fully filled-in mountd RPC service tuple
692 * @nfs_saddr: pointer to socket address of NFS server
693 * @nfs_salen: length of NFS server's address
694 * @nfs_pmap: IN: partially filled-in NFS RPC service tuple;
695 * OUT: fully filled-in NFS RPC service tuple
697 * Returns 1 and fills in both @pmap structs if the requested service
698 * ports are unambiguous and pingable. Otherwise zero is returned;
699 * rpccreateerr.cf_stat is set to reflect the nature of the error.
701 int nfs_probe_bothports(const struct sockaddr *mnt_saddr,
702 const socklen_t mnt_salen,
703 struct pmap *mnt_pmap,
704 const struct sockaddr *nfs_saddr,
705 const socklen_t nfs_salen,
706 struct pmap *nfs_pmap)
708 struct pmap save_nfs, save_mnt;
709 const unsigned long *probe_vers;
711 if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
712 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
713 else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
714 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
716 if (nfs_pmap->pm_vers)
717 return nfs_probe_version_fixed(mnt_saddr, mnt_salen, mnt_pmap,
718 nfs_saddr, nfs_salen, nfs_pmap);
720 memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
721 memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
722 probe_vers = (nfs_mount_data_version >= 4) ?
723 probe_mnt3_first : probe_mnt1_first;
725 for (; *probe_vers; probe_vers++) {
726 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
727 if (nfs_probe_nfsport(nfs_saddr, nfs_salen, nfs_pmap) != 0) {
728 mnt_pmap->pm_vers = *probe_vers;
729 if (nfs_probe_mntport(mnt_saddr, mnt_salen, mnt_pmap) != 0)
731 memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
733 switch (rpc_createerr.cf_stat) {
734 case RPC_PROGVERSMISMATCH:
735 case RPC_PROGNOTREGISTERED:
740 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
747 * probe_bothports - discover the RPC endpoints of mountd and NFS server
748 * @mnt_server: pointer to address and pmap argument for mountd results
749 * @nfs_server: pointer to address and pmap argument for NFS server
751 * This is the legacy API that takes "clnt_addr_t" for both servers,
752 * but supports only AF_INET addresses.
754 * Returns 1 and fills in the pmap field in both clnt_addr_t structs
755 * if the requested service ports are unambiguous and pingable.
756 * Otherwise zero is returned; rpccreateerr.cf_stat is set to reflect
757 * the nature of the error.
759 int probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
761 struct sockaddr *mnt_addr = SAFE_SOCKADDR(&mnt_server->saddr);
762 struct sockaddr *nfs_addr = SAFE_SOCKADDR(&nfs_server->saddr);
764 return nfs_probe_bothports(mnt_addr, sizeof(mnt_server->saddr),
766 nfs_addr, sizeof(nfs_server->saddr),
770 static int nfs_probe_statd(void)
772 struct sockaddr_in addr = {
773 .sin_family = AF_INET,
774 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
776 rpcprog_t program = nfs_getrpcbyname(NSMPROG, nfs_ns_pgmtbl);
778 return nfs_getport_ping(SAFE_SOCKADDR(&addr), sizeof(addr),
779 program, (rpcvers_t)1, IPPROTO_UDP);
783 * start_statd - attempt to start rpc.statd
785 * Returns 1 if statd is running; otherwise zero.
787 int start_statd(void)
793 if (nfs_probe_statd())
797 if (stat(START_STATD, &stb) == 0) {
798 if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {
802 execl(START_STATD, START_STATD, NULL);
805 nfs_error(_("%s: fork failed: %s"),
806 progname, strerror(errno));
808 default: /* parent */
809 waitpid(pid, NULL,0);
812 if (nfs_probe_statd())
822 * nfs_advise_umount - ask the server to remove a share from it's rmtab
823 * @sap: pointer to IP address of server to call
824 * @salen: length of server address
825 * @pmap: partially filled-in mountd RPC service tuple
826 * @argp: directory path of share to "unmount"
828 * Returns one if the unmount call succeeded; zero if the unmount
829 * failed for any reason; rpccreateerr.cf_stat is set to reflect
830 * the nature of the error.
832 * We use a fast timeout since this call is advisory only.
834 int nfs_advise_umount(const struct sockaddr *sap, const socklen_t salen,
835 const struct pmap *pmap, const dirpath *argp)
837 union nfs_sockaddr address;
838 struct sockaddr *saddr = &address.sa;
839 struct pmap mnt_pmap = *pmap;
840 struct timeval timeout = {
841 .tv_sec = MOUNT_TIMEOUT >> 3,
844 enum clnt_stat res = 0;
846 memcpy(saddr, sap, salen);
847 if (nfs_probe_mntport(saddr, salen, &mnt_pmap) == 0) {
849 nfs_error(_("%s: Failed to discover mountd port%s"),
850 progname, clnt_spcreateerror(""));
853 nfs_set_port(saddr, mnt_pmap.pm_port);
855 client = nfs_get_priv_rpcclient(saddr, salen, mnt_pmap.pm_prot,
856 mnt_pmap.pm_prog, mnt_pmap.pm_vers,
858 if (client == NULL) {
860 nfs_error(_("%s: Failed to create RPC client%s"),
861 progname, clnt_spcreateerror(""));
865 client->cl_auth = nfs_authsys_create();
866 if (client->cl_auth == NULL) {
868 nfs_error(_("%s: Failed to create RPC auth handle"),
870 CLNT_DESTROY(client);
874 res = CLNT_CALL(client, MOUNTPROC_UMNT,
875 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
876 (xdrproc_t)xdr_void, NULL,
878 if (res != RPC_SUCCESS) {
879 rpc_createerr.cf_stat = res;
880 CLNT_GETERR(client, &rpc_createerr.cf_error);
882 nfs_error(_("%s: UMNT call failed: %s"),
883 progname, clnt_sperrno(res));
886 auth_destroy(client->cl_auth);
887 CLNT_DESTROY(client);
889 if (res != RPC_SUCCESS)
895 * nfs_call_umount - ask the server to remove a share from it's rmtab
896 * @mnt_server: address of RPC MNT program server
897 * @argp: directory path of share to "unmount"
899 * Returns one if the unmount call succeeded; zero if the unmount
900 * failed for any reason.
902 * Note that a side effect of calling this function is that rpccreateerr
905 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
907 struct sockaddr *sap = SAFE_SOCKADDR(&mnt_server->saddr);
908 socklen_t salen = sizeof(mnt_server->saddr);
909 struct pmap *pmap = &mnt_server->pmap;
911 enum clnt_stat res = 0;
914 if (!nfs_probe_mntport(sap, salen, pmap))
916 clnt = mnt_openclnt(mnt_server, &msock);
919 res = clnt_call(clnt, MOUNTPROC_UMNT,
920 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
921 (xdrproc_t)xdr_void, NULL,
923 mnt_closeclnt(clnt, msock);
925 if (res == RPC_SUCCESS)
931 * mnt_openclnt - get a handle for a remote mountd service
932 * @mnt_server: address and pmap arguments of mountd service
933 * @msock: returns a file descriptor of the underlying transport socket
935 * Returns an active handle for the remote's mountd service
937 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
939 struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
940 struct pmap *mnt_pmap = &mnt_server->pmap;
943 mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
944 *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, MOUNT_TIMEOUT,
946 if (*msock == RPC_ANYSOCK) {
947 if (rpc_createerr.cf_error.re_errno == EADDRINUSE)
949 * Probably in-use by a TIME_WAIT connection,
950 * It is worth waiting a while and trying again.
952 rpc_createerr.cf_stat = RPC_TIMEDOUT;
956 switch (mnt_pmap->pm_prot) {
958 clnt = clntudp_bufcreate(mnt_saddr,
959 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
960 RETRY_TIMEOUT, msock,
961 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
964 clnt = clnttcp_create(mnt_saddr,
965 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
967 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
971 /* try to mount hostname:dirname */
972 clnt->cl_auth = nfs_authsys_create();
981 * mnt_closeclnt - terminate a handle for a remote mountd service
982 * @clnt: pointer to an active handle for a remote mountd service
983 * @msock: file descriptor of the underlying transport socket
986 void mnt_closeclnt(CLIENT *clnt, int msock)
988 auth_destroy(clnt->cl_auth);
994 * clnt_ping - send an RPC ping to the remote RPC service endpoint
995 * @saddr: server's address
996 * @prog: target RPC program number
997 * @vers: target RPC version number
998 * @prot: target RPC protocol
999 * @caddr: filled in with our network address
1001 * Sigh... GETPORT queries don't actually check the version number.
1002 * In order to make sure that the server actually supports the service
1003 * we're requesting, we open an RPC client, and fire off a NULL
1006 * caddr is the network address that the server will use to call us back.
1007 * On multi-homed clients, this address depends on which NIC we use to
1008 * route requests to the server.
1010 * Returns one if successful, otherwise zero.
1012 int clnt_ping(struct sockaddr_in *saddr, const unsigned long prog,
1013 const unsigned long vers, const unsigned int prot,
1014 struct sockaddr_in *caddr)
1016 CLIENT *clnt = NULL;
1018 static char clnt_res;
1019 struct sockaddr dissolve;
1021 rpc_createerr.cf_stat = status = 0;
1022 sock = get_socket(saddr, prot, CONNECT_TIMEOUT, FALSE, TRUE);
1023 if (sock == RPC_ANYSOCK) {
1024 if (rpc_createerr.cf_error.re_errno == ETIMEDOUT) {
1026 * TCP timeout. Bubble up the error to see
1027 * how it should be handled.
1029 rpc_createerr.cf_stat = RPC_TIMEDOUT;
1035 /* Get the address of our end of this connection */
1036 socklen_t len = sizeof(*caddr);
1037 if (getsockname(sock, caddr, &len) != 0)
1038 caddr->sin_family = 0;
1043 /* The socket is connected (so we could getsockname successfully),
1044 * but some servers on multi-homed hosts reply from
1045 * the wrong address, so if we stay connected, we lose the reply.
1047 dissolve.sa_family = AF_UNSPEC;
1048 connect(sock, &dissolve, sizeof(dissolve));
1050 clnt = clntudp_bufcreate(saddr, prog, vers,
1051 RETRY_TIMEOUT, &sock,
1052 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
1055 clnt = clnttcp_create(saddr, prog, vers, &sock,
1056 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
1063 memset(&clnt_res, 0, sizeof(clnt_res));
1064 status = clnt_call(clnt, NULLPROC,
1065 (xdrproc_t)xdr_void, (caddr_t)NULL,
1066 (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
1069 clnt_geterr(clnt, &rpc_createerr.cf_error);
1070 rpc_createerr.cf_stat = status;
1075 if (status == RPC_SUCCESS)
1082 * Try a getsockname() on a connected datagram socket.
1084 * Returns 1 and fills in @buf if successful; otherwise, zero.
1086 * A connected datagram socket prevents leaving a socket in TIME_WAIT.
1087 * This conserves the ephemeral port number space, helping reduce failed
1088 * socket binds during mount storms.
1090 static int nfs_ca_sockname(const struct sockaddr *sap, const socklen_t salen,
1091 struct sockaddr *buf, socklen_t *buflen)
1093 struct sockaddr_in sin = {
1094 .sin_family = AF_INET,
1095 .sin_addr.s_addr = htonl(INADDR_ANY),
1097 struct sockaddr_in6 sin6 = {
1098 .sin6_family = AF_INET6,
1099 .sin6_addr = IN6ADDR_ANY_INIT,
1103 sock = socket(sap->sa_family, SOCK_DGRAM, IPPROTO_UDP);
1107 switch (sap->sa_family) {
1109 if (bind(sock, SAFE_SOCKADDR(&sin), sizeof(sin)) < 0) {
1115 if (bind(sock, SAFE_SOCKADDR(&sin6), sizeof(sin6)) < 0) {
1121 errno = EAFNOSUPPORT;
1125 if (connect(sock, sap, salen) < 0) {
1130 return !getsockname(sock, buf, buflen);
1134 * Try to generate an address that prevents the server from calling us.
1136 * Returns 1 and fills in @buf if successful; otherwise, zero.
1138 static int nfs_ca_gai(const struct sockaddr *sap,
1139 struct sockaddr *buf, socklen_t *buflen)
1141 struct addrinfo *gai_results;
1142 struct addrinfo gai_hint = {
1143 .ai_family = sap->sa_family,
1144 .ai_flags = AI_PASSIVE, /* ANYADDR */
1147 if (getaddrinfo(NULL, "", &gai_hint, &gai_results))
1150 *buflen = gai_results->ai_addrlen;
1151 memcpy(buf, gai_results->ai_addr, *buflen);
1153 freeaddrinfo(gai_results);
1159 * nfs_callback_address - acquire our local network address
1160 * @sap: pointer to address of remote
1161 * @sap_len: length of address
1162 * @buf: pointer to buffer to be filled in with local network address
1163 * @buflen: IN: length of buffer to fill in; OUT: length of filled-in address
1165 * Discover a network address that an NFSv4 server can use to call us back.
1166 * On multi-homed clients, this address depends on which NIC we use to
1167 * route requests to the server.
1169 * Returns 1 and fills in @buf if an unambiguous local address is
1170 * available; returns 1 and fills in an appropriate ANYADDR address
1171 * if a local address isn't available; otherwise, returns zero.
1173 int nfs_callback_address(const struct sockaddr *sap, const socklen_t salen,
1174 struct sockaddr *buf, socklen_t *buflen)
1176 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1178 if (nfs_ca_sockname(sap, salen, buf, buflen) == 0)
1179 if (nfs_ca_gai(sap, buf, buflen) == 0)
1183 * The server can't use an interface ID that was generated
1184 * here on the client, so always clear sin6_scope_id.
1186 if (sin6->sin6_family == AF_INET6)
1187 sin6->sin6_scope_id = 0;
1194 nfs_error(_("%s: failed to construct callback address"),
1200 * "nfsprog" is supported only by the legacy mount command. The
1201 * kernel mount client does not support this option.
1203 * Returns TRUE if @program contains a valid value for this option,
1204 * or FALSE if the option was specified with an invalid value.
1207 nfs_nfs_program(struct mount_options *options, unsigned long *program)
1211 switch (po_get_numeric(options, "nfsprog", &tmp)) {
1220 nfs_error(_("%s: invalid value for 'nfsprog=' option"),
1226 * NFS RPC program wasn't specified. The RPC program
1227 * cannot be determined via an rpcbind query.
1229 *program = nfs_getrpcbyname(NFSPROG, nfs_nfs_pgmtbl);
1234 * Returns TRUE if @version contains a valid value for this option,
1235 * or FALSE if the option was specified with an invalid value.
1238 nfs_nfs_version(struct mount_options *options, unsigned long *version)
1242 switch (po_rightmost(options, nfs_version_opttbl)) {
1253 switch (po_get_numeric(options, "vers", &tmp)) {
1255 if (tmp >= 2 && tmp <= 4) {
1261 nfs_error(_("%s: parsing error on 'vers=' option\n"),
1265 nfs_error(_("%s: invalid value for 'vers=' option"),
1269 case 4: /* nfsvers */
1270 switch (po_get_numeric(options, "nfsvers", &tmp)) {
1272 if (tmp >= 2 && tmp <= 4) {
1278 nfs_error(_("%s: parsing error on 'nfsvers=' option\n"),
1282 nfs_error(_("%s: invalid value for 'nfsvers=' option"),
1289 * NFS version wasn't specified. The pmap version value
1290 * will be filled in later by an rpcbind query in this case.
1297 * Returns TRUE if @protocol contains a valid value for this option,
1298 * or FALSE if the option was specified with an invalid value. On
1299 * error, errno is set.
1302 nfs_nfs_protocol(struct mount_options *options, unsigned long *protocol)
1307 switch (po_rightmost(options, nfs_transport_opttbl)) {
1309 *protocol = IPPROTO_UDP;
1312 *protocol = IPPROTO_TCP;
1315 *protocol = NFSPROTO_RDMA;
1318 option = po_get(options, "proto");
1319 if (option != NULL) {
1320 if (!nfs_get_proto(option, &family, protocol)) {
1321 errno = EPROTONOSUPPORT;
1322 nfs_error(_("%s: Failed to find '%s' protocol"),
1331 * NFS transport protocol wasn't specified. The pmap
1332 * protocol value will be filled in later by an rpcbind
1333 * query in this case.
1340 * Returns TRUE if @port contains a valid value for this option,
1341 * or FALSE if the option was specified with an invalid value.
1344 nfs_nfs_port(struct mount_options *options, unsigned long *port)
1348 switch (po_get_numeric(options, "port", &tmp)) {
1352 if (tmp >= 1 && tmp <= 65535) {
1357 nfs_error(_("%s: invalid value for 'port=' option"),
1363 * NFS service port wasn't specified. The pmap port value
1364 * will be filled in later by an rpcbind query in this case.
1370 #ifdef IPV6_SUPPORTED
1371 sa_family_t config_default_family = AF_UNSPEC;
1374 nfs_verify_family(sa_family_t UNUSED(family))
1378 #else /* IPV6_SUPPORTED */
1379 sa_family_t config_default_family = AF_INET;
1382 nfs_verify_family(sa_family_t family)
1384 if (family != AF_INET)
1389 #endif /* IPV6_SUPPORTED */
1392 * Returns TRUE and fills in @family if a valid NFS protocol option
1393 * is found, or FALSE if the option was specified with an invalid value
1394 * or if the protocol family isn't supported. On error, errno is set.
1396 int nfs_nfs_proto_family(struct mount_options *options,
1397 sa_family_t *family)
1399 unsigned long protocol;
1401 sa_family_t tmp_family = config_default_family;
1403 switch (po_rightmost(options, nfs_transport_opttbl)) {
1407 /* for compatibility; these are always AF_INET */
1411 option = po_get(options, "proto");
1412 if (option != NULL &&
1413 !nfs_get_proto(option, &tmp_family, &protocol)) {
1415 nfs_error(_("%s: Failed to find '%s' protocol"),
1417 errno = EPROTONOSUPPORT;
1422 if (!nfs_verify_family(tmp_family))
1424 *family = tmp_family;
1427 errno = EAFNOSUPPORT;
1432 * "mountprog" is supported only by the legacy mount command. The
1433 * kernel mount client does not support this option.
1435 * Returns TRUE if @program contains a valid value for this option,
1436 * or FALSE if the option was specified with an invalid value.
1439 nfs_mount_program(struct mount_options *options, unsigned long *program)
1443 switch (po_get_numeric(options, "mountprog", &tmp)) {
1452 nfs_error(_("%s: invalid value for 'mountprog=' option"),
1458 * MNT RPC program wasn't specified. The RPC program
1459 * cannot be determined via an rpcbind query.
1461 *program = nfs_getrpcbyname(MOUNTPROG, nfs_mnt_pgmtbl);
1466 * Returns TRUE if @version contains a valid value for this option,
1467 * or FALSE if the option was specified with an invalid value.
1470 nfs_mount_version(struct mount_options *options, unsigned long *version)
1474 switch (po_get_numeric(options, "mountvers", &tmp)) {
1478 if (tmp >= 1 && tmp <= 4) {
1483 nfs_error(_("%s: invalid value for 'mountvers=' option"),
1489 * MNT version wasn't specified. The pmap version value
1490 * will be filled in later by an rpcbind query in this case.
1497 * Returns TRUE if @protocol contains a valid value for this option,
1498 * or FALSE if the option was specified with an invalid value. On
1499 * error, errno is set.
1502 nfs_mount_protocol(struct mount_options *options, unsigned long *protocol)
1507 option = po_get(options, "mountproto");
1508 if (option != NULL) {
1509 if (!nfs_get_proto(option, &family, protocol)) {
1510 errno = EPROTONOSUPPORT;
1511 nfs_error(_("%s: Failed to find '%s' protocol"),
1519 * MNT transport protocol wasn't specified. If the NFS
1520 * transport protocol was specified, use that; otherwise
1521 * set @protocol to zero. The pmap protocol value will
1522 * be filled in later by an rpcbind query in this case.
1524 if (!nfs_nfs_protocol(options, protocol))
1526 if (*protocol == NFSPROTO_RDMA)
1527 *protocol = IPPROTO_TCP;
1532 * Returns TRUE if @port contains a valid value for this option,
1533 * or FALSE if the option was specified with an invalid value.
1536 nfs_mount_port(struct mount_options *options, unsigned long *port)
1540 switch (po_get_numeric(options, "mountport", &tmp)) {
1544 if (tmp >= 1 && tmp <= 65535) {
1549 nfs_error(_("%s: invalid value for 'mountport=' option"),
1555 * MNT service port wasn't specified. The pmap port value
1556 * will be filled in later by an rpcbind query in this case.
1563 * Returns TRUE and fills in @family if a valid MNT protocol option
1564 * is found, or FALSE if the option was specified with an invalid value
1565 * or if the protocol family isn't supported. On error, errno is set.
1567 int nfs_mount_proto_family(struct mount_options *options,
1568 sa_family_t *family)
1570 unsigned long protocol;
1572 sa_family_t tmp_family = config_default_family;
1574 option = po_get(options, "mountproto");
1575 if (option != NULL) {
1576 if (!nfs_get_proto(option, &tmp_family, &protocol)) {
1577 nfs_error(_("%s: Failed to find '%s' protocol"),
1579 errno = EPROTONOSUPPORT;
1582 if (!nfs_verify_family(tmp_family))
1584 *family = tmp_family;
1589 * MNT transport protocol wasn't specified. If the NFS
1590 * transport protocol was specified, derive the family
1591 * from that; otherwise, return the default family for
1594 return nfs_nfs_proto_family(options, family);
1596 errno = EAFNOSUPPORT;
1601 * nfs_options2pmap - set up pmap structs based on mount options
1602 * @options: pointer to mount options
1603 * @nfs_pmap: OUT: pointer to pmap arguments for NFS server
1604 * @mnt_pmap: OUT: pointer to pmap arguments for mountd server
1606 * Returns TRUE if the pmap options specified in @options have valid
1607 * values; otherwise FALSE is returned.
1609 int nfs_options2pmap(struct mount_options *options,
1610 struct pmap *nfs_pmap, struct pmap *mnt_pmap)
1612 if (!nfs_nfs_program(options, &nfs_pmap->pm_prog))
1614 if (!nfs_nfs_version(options, &nfs_pmap->pm_vers))
1616 if (!nfs_nfs_protocol(options, &nfs_pmap->pm_prot))
1618 if (!nfs_nfs_port(options, &nfs_pmap->pm_port))
1621 if (!nfs_mount_program(options, &mnt_pmap->pm_prog))
1623 if (!nfs_mount_version(options, &mnt_pmap->pm_vers))
1625 if (!nfs_mount_protocol(options, &mnt_pmap->pm_prot))
1627 if (!nfs_mount_port(options, &mnt_pmap->pm_port))