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 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 const unsigned int *nfs_default_proto(void);
176 static const unsigned int *nfs_default_proto()
178 extern unsigned long config_default_proto;
180 * If the default proto has been set and
181 * its not TCP, start with UDP
183 if (config_default_proto && config_default_proto != IPPROTO_TCP)
184 return probe_udp_first;
186 return probe_tcp_first;
189 static const unsigned int *nfs_default_proto()
191 return probe_tcp_first;
193 #endif /* MOUNT_CONFIG */
196 * nfs_lookup - resolve hostname to an IPv4 or IPv6 socket address
197 * @hostname: pointer to C string containing DNS hostname to resolve
198 * @family: address family hint
199 * @sap: pointer to buffer to fill with socket address
200 * @len: IN: size of buffer to fill; OUT: size of socket address
202 * Returns 1 and places a socket address at @sap if successful;
205 int nfs_lookup(const char *hostname, const sa_family_t family,
206 struct sockaddr *sap, socklen_t *salen)
208 struct addrinfo *gai_results;
209 struct addrinfo gai_hint = {
210 #ifdef HAVE_DECL_AI_ADDRCONFIG
211 .ai_flags = AI_ADDRCONFIG,
212 #endif /* HAVE_DECL_AI_ADDRCONFIG */
215 socklen_t len = *salen;
220 error = getaddrinfo(hostname, NULL, &gai_hint, &gai_results);
225 nfs_error(_("%s: DNS resolution failed for %s: %s"),
226 progname, hostname, strerror(errno));
229 nfs_error(_("%s: DNS resolution failed for %s: %s"),
230 progname, hostname, gai_strerror(error));
234 switch (gai_results->ai_addr->sa_family) {
237 if (len >= gai_results->ai_addrlen) {
238 *salen = gai_results->ai_addrlen;
239 memcpy(sap, gai_results->ai_addr, *salen);
244 /* things are really broken if we get here, so warn */
245 nfs_error(_("%s: unrecognized DNS resolution results for %s"),
250 freeaddrinfo(gai_results);
255 * nfs_gethostbyname - resolve a hostname to an IPv4 address
256 * @hostname: pointer to a C string containing a DNS hostname
257 * @sin: returns an IPv4 address
259 * Returns 1 if successful, otherwise zero.
261 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *sin)
263 socklen_t len = sizeof(*sin);
265 return nfs_lookup(hostname, AF_INET, (struct sockaddr *)sin, &len);
269 * nfs_string_to_sockaddr - convert string address to sockaddr
270 * @address: pointer to presentation format address to convert
271 * @sap: pointer to socket address buffer to fill in
272 * @salen: IN: length of address buffer
273 * OUT: length of converted socket address
275 * Convert a presentation format address string to a socket address.
276 * Similar to nfs_lookup(), but the DNS query is squelched, and it
277 * won't make any noise if the getaddrinfo() call fails.
279 * Returns 1 and fills in @sap and @salen if successful; otherwise zero.
281 * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
282 * on presenting IPv6 addresses as text strings.
284 int nfs_string_to_sockaddr(const char *address, struct sockaddr *sap,
287 struct addrinfo *gai_results;
288 struct addrinfo gai_hint = {
289 .ai_flags = AI_NUMERICHOST,
291 socklen_t len = *salen;
296 if (getaddrinfo(address, NULL, &gai_hint, &gai_results) == 0) {
297 switch (gai_results->ai_addr->sa_family) {
300 if (len >= gai_results->ai_addrlen) {
301 *salen = gai_results->ai_addrlen;
302 memcpy(sap, gai_results->ai_addr, *salen);
307 freeaddrinfo(gai_results);
314 * nfs_present_sockaddr - convert sockaddr to string
315 * @sap: pointer to socket address to convert
316 * @salen: length of socket address
317 * @buf: pointer to buffer to fill in
318 * @buflen: length of buffer
320 * Convert the passed-in sockaddr-style address to presentation format.
321 * The presentation format address is placed in @buf and is
324 * Returns 1 if successful; otherwise zero.
326 * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
327 * on presenting IPv6 addresses as text strings.
329 int nfs_present_sockaddr(const struct sockaddr *sap, const socklen_t salen,
330 char *buf, const size_t buflen)
332 #ifdef HAVE_GETNAMEINFO
335 result = getnameinfo(sap, salen, buf, buflen,
336 NULL, 0, NI_NUMERICHOST);
340 nfs_error(_("%s: invalid server address: %s"), progname,
341 gai_strerror(result));
343 #else /* HAVE_GETNAMEINFO */
346 if (sap->sa_family == AF_INET) {
347 addr = inet_ntoa(((struct sockaddr_in *)sap)->sin_addr);
348 if (addr && strlen(addr) < buflen) {
354 nfs_error(_("%s: invalid server address"), progname);
356 #endif /* HAVE_GETNAMEINFO */
360 * Attempt to connect a socket, but time out after "timeout" seconds.
362 * On error return, caller closes the socket.
364 static int connect_to(int fd, struct sockaddr *addr,
365 socklen_t addrlen, int timeout)
369 struct timeval tv = {
373 saved = fcntl(fd, F_GETFL, 0);
374 fcntl(fd, F_SETFL, saved | O_NONBLOCK);
376 ret = connect(fd, addr, addrlen);
377 if (ret < 0 && errno != EINPROGRESS)
385 ret = select(fd + 1, &rset, &wset, NULL, &tv);
390 if (FD_ISSET(fd, &rset) || FD_ISSET(fd, &wset)) {
392 socklen_t len = sizeof(error);
393 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
403 fcntl(fd, F_SETFL, saved);
408 * Create a socket that is locally bound to a reserved or non-reserved port.
410 * The caller should check rpc_createerr to determine the cause of any error.
412 static int get_socket(struct sockaddr_in *saddr, unsigned int p_prot,
413 unsigned int timeout, int resvp, int conn)
416 struct sockaddr_in laddr;
417 socklen_t namelen = sizeof(laddr);
419 type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
420 if ((so = socket (AF_INET, type, p_prot)) < 0)
423 laddr.sin_family = AF_INET;
425 laddr.sin_addr.s_addr = htonl(INADDR_ANY);
427 if (bindresvport(so, &laddr) < 0)
428 goto err_bindresvport;
430 cc = bind(so, (struct sockaddr *)&laddr, namelen);
434 if (type == SOCK_STREAM || (conn && type == SOCK_DGRAM)) {
435 cc = connect_to(so, (struct sockaddr *)saddr, namelen,
443 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
444 rpc_createerr.cf_error.re_errno = errno;
446 nfs_error(_("%s: Unable to create %s socket: errno %d (%s)\n"),
447 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
448 errno, strerror(errno));
453 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
454 rpc_createerr.cf_error.re_errno = errno;
456 nfs_error(_("%s: Unable to bindresvport %s socket: errno %d"
458 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
459 errno, strerror(errno));
465 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
466 rpc_createerr.cf_error.re_errno = errno;
468 nfs_error(_("%s: Unable to bind to %s socket: errno %d (%s)\n"),
469 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
470 errno, strerror(errno));
476 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
477 rpc_createerr.cf_error.re_errno = errno;
479 nfs_error(_("%s: Unable to connect to %s:%d, errno %d (%s)\n"),
480 progname, inet_ntoa(saddr->sin_addr),
481 ntohs(saddr->sin_port), errno, strerror(errno));
487 static void nfs_pp_debug(const struct sockaddr *sap, const socklen_t salen,
488 const rpcprog_t program, const rpcvers_t version,
489 const unsigned short protocol,
490 const unsigned short port)
492 char buf[NI_MAXHOST];
497 if (nfs_present_sockaddr(sap, salen, buf, sizeof(buf)) == 0) {
499 strcat(buf, "unknown host");
502 fprintf(stderr, _("%s: trying %s prog %lu vers %lu prot %s port %d\n"),
503 progname, buf, (unsigned long)program,
504 (unsigned long)version,
505 (protocol == IPPROTO_UDP ? _("UDP") : _("TCP")),
509 static void nfs_pp_debug2(const char *str)
514 if (rpc_createerr.cf_error.re_status == RPC_CANTRECV ||
515 rpc_createerr.cf_error.re_status == RPC_CANTSEND)
516 nfs_error(_("%s: portmap query %s%s - %s"),
517 progname, str, clnt_spcreateerror(""),
518 strerror(rpc_createerr.cf_error.re_errno));
520 nfs_error(_("%s: portmap query %s%s"),
521 progname, str, clnt_spcreateerror(""));
525 * Use the portmapper to discover whether or not the service we want is
526 * available. The lists 'versions' and 'protos' define ordered sequences
527 * of service versions and udp/tcp protocols to probe for.
529 * Returns 1 if the requested service port is unambiguous and pingable;
530 * @pmap is filled in with the version, port, and transport protocol used
531 * during the successful ping. Note that if a port is already specified
532 * in @pmap and it matches the rpcbind query result, nfs_probe_port() does
533 * not perform an RPC ping.
535 * If an error occurs or the requested service isn't available, zero is
536 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
538 static int nfs_probe_port(const struct sockaddr *sap, const socklen_t salen,
539 struct pmap *pmap, const unsigned long *versions,
540 const unsigned int *protos)
542 union nfs_sockaddr address;
543 struct sockaddr *saddr = &address.sa;
544 const unsigned long prog = pmap->pm_prog, *p_vers;
545 const unsigned int prot = (u_int)pmap->pm_prot, *p_prot;
546 const u_short port = (u_short) pmap->pm_port;
547 unsigned long vers = pmap->pm_vers;
548 unsigned short p_port;
550 memcpy(saddr, sap, salen);
551 p_prot = prot ? &prot : protos;
552 p_vers = vers ? &vers : versions;
556 printf(_("%s: prog %lu, trying vers=%lu, prot=%u\n"),
557 progname, prog, *p_vers, *p_prot);
558 p_port = nfs_getport(saddr, salen, prog, *p_vers, *p_prot);
560 if (!port || port == p_port) {
561 nfs_set_port(saddr, p_port);
562 nfs_pp_debug(saddr, salen, prog, *p_vers,
564 if (nfs_rpc_ping(saddr, salen, prog,
565 *p_vers, *p_prot, NULL))
568 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
570 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
571 rpc_createerr.cf_stat != RPC_TIMEDOUT &&
572 rpc_createerr.cf_stat != RPC_CANTRECV &&
573 rpc_createerr.cf_stat != RPC_PROGVERSMISMATCH)
578 nfs_pp_debug2("retrying");
583 if (rpc_createerr.cf_stat == RPC_TIMEDOUT ||
584 rpc_createerr.cf_stat == RPC_CANTRECV)
587 if (vers || !*++p_vers)
591 nfs_pp_debug2("failed");
596 pmap->pm_vers = *p_vers;
598 pmap->pm_prot = *p_prot;
600 pmap->pm_port = p_port;
601 nfs_clear_rpc_createerr();
605 * Probe a server's NFS service to determine which versions and
606 * transport protocols are supported.
608 * Returns 1 if the requested service port is unambiguous and pingable;
609 * @pmap is filled in with the version, port, and transport protocol used
610 * during the successful ping. If all three are already specified, simply
611 * return success without an rpcbind query or RPC ping (we may be trying
612 * to mount an NFS service that is not advertised via rpcbind).
614 * If an error occurs or the requested service isn't available, zero is
615 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
617 static int nfs_probe_nfsport(const struct sockaddr *sap, const socklen_t salen,
620 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
623 if (nfs_mount_data_version >= 4) {
624 const unsigned int *probe_proto;
626 probe_proto = nfs_default_proto();
628 return nfs_probe_port(sap, salen, pmap,
629 probe_nfs3_first, probe_proto);
631 return nfs_probe_port(sap, salen, pmap,
632 probe_nfs2_only, probe_udp_only);
636 * Probe a server's mountd service to determine which versions and
637 * transport protocols are supported.
639 * Returns 1 if the requested service port is unambiguous and pingable;
640 * @pmap is filled in with the version, port, and transport protocol used
641 * during the successful ping. If all three are already specified, simply
642 * return success without an rpcbind query or RPC ping (we may be trying
643 * to mount an NFS service that is not advertised via rpcbind).
645 * If an error occurs or the requested service isn't available, zero is
646 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
648 static int nfs_probe_mntport(const struct sockaddr *sap, const socklen_t salen,
651 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
654 if (nfs_mount_data_version >= 4)
655 return nfs_probe_port(sap, salen, pmap,
656 probe_mnt3_first, probe_udp_first);
658 return nfs_probe_port(sap, salen, pmap,
659 probe_mnt1_first, probe_udp_only);
663 * Probe a server's mountd service to determine which versions and
664 * transport protocols are supported. Invoked when the protocol
665 * version is already known for both the NFS and mountd service.
667 * Returns 1 and fills in both @pmap structs if the requested service
668 * ports are unambiguous and pingable. Otherwise zero is returned;
669 * rpccreateerr.cf_stat is set to reflect the nature of the error.
671 static int nfs_probe_version_fixed(const struct sockaddr *mnt_saddr,
672 const socklen_t mnt_salen,
673 struct pmap *mnt_pmap,
674 const struct sockaddr *nfs_saddr,
675 const socklen_t nfs_salen,
676 struct pmap *nfs_pmap)
678 if (!nfs_probe_nfsport(nfs_saddr, nfs_salen, nfs_pmap))
680 return nfs_probe_mntport(mnt_saddr, mnt_salen, mnt_pmap);
684 * nfs_probe_bothports - discover the RPC endpoints of mountd and NFS server
685 * @mnt_saddr: pointer to socket address of mountd server
686 * @mnt_salen: length of mountd server's address
687 * @mnt_pmap: IN: partially filled-in mountd RPC service tuple;
688 * OUT: fully filled-in mountd RPC service tuple
689 * @nfs_saddr: pointer to socket address of NFS server
690 * @nfs_salen: length of NFS server's address
691 * @nfs_pmap: IN: partially filled-in NFS RPC service tuple;
692 * OUT: fully filled-in NFS RPC service tuple
694 * Returns 1 and fills in both @pmap structs if the requested service
695 * ports are unambiguous and pingable. Otherwise zero is returned;
696 * rpccreateerr.cf_stat is set to reflect the nature of the error.
698 int nfs_probe_bothports(const struct sockaddr *mnt_saddr,
699 const socklen_t mnt_salen,
700 struct pmap *mnt_pmap,
701 const struct sockaddr *nfs_saddr,
702 const socklen_t nfs_salen,
703 struct pmap *nfs_pmap)
705 struct pmap save_nfs, save_mnt;
706 const unsigned long *probe_vers;
708 if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
709 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
710 else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
711 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
713 if (nfs_pmap->pm_vers)
714 return nfs_probe_version_fixed(mnt_saddr, mnt_salen, mnt_pmap,
715 nfs_saddr, nfs_salen, nfs_pmap);
717 memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
718 memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
719 probe_vers = (nfs_mount_data_version >= 4) ?
720 probe_mnt3_first : probe_mnt1_first;
722 for (; *probe_vers; probe_vers++) {
723 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
724 if (nfs_probe_nfsport(nfs_saddr, nfs_salen, nfs_pmap) != 0) {
725 mnt_pmap->pm_vers = *probe_vers;
726 if (nfs_probe_mntport(mnt_saddr, mnt_salen, mnt_pmap) != 0)
728 memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
730 switch (rpc_createerr.cf_stat) {
731 case RPC_PROGVERSMISMATCH:
732 case RPC_PROGNOTREGISTERED:
737 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
744 * probe_bothports - discover the RPC endpoints of mountd and NFS server
745 * @mnt_server: pointer to address and pmap argument for mountd results
746 * @nfs_server: pointer to address and pmap argument for NFS server
748 * This is the legacy API that takes "clnt_addr_t" for both servers,
749 * but supports only AF_INET addresses.
751 * Returns 1 and fills in the pmap field in both clnt_addr_t structs
752 * if the requested service ports are unambiguous and pingable.
753 * Otherwise zero is returned; rpccreateerr.cf_stat is set to reflect
754 * the nature of the error.
756 int probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
758 return nfs_probe_bothports((struct sockaddr *)&mnt_server->saddr,
759 sizeof(mnt_server->saddr),
761 (struct sockaddr *)&nfs_server->saddr,
762 sizeof(nfs_server->saddr),
766 static int nfs_probe_statd(void)
768 struct sockaddr_in addr = {
769 .sin_family = AF_INET,
770 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
772 rpcprog_t program = nfs_getrpcbyname(NSMPROG, nfs_ns_pgmtbl);
774 return nfs_getport_ping((struct sockaddr *)&addr, sizeof(addr),
775 program, (rpcvers_t)1, IPPROTO_UDP);
779 * start_statd - attempt to start rpc.statd
781 * Returns 1 if statd is running; otherwise zero.
783 int start_statd(void)
789 if (nfs_probe_statd())
793 if (stat(START_STATD, &stb) == 0) {
794 if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {
798 execl(START_STATD, START_STATD, NULL);
801 nfs_error(_("%s: fork failed: %s"),
802 progname, strerror(errno));
804 default: /* parent */
805 waitpid(pid, NULL,0);
808 if (nfs_probe_statd())
818 * nfs_advise_umount - ask the server to remove a share from it's rmtab
819 * @sap: pointer to IP address of server to call
820 * @salen: length of server address
821 * @pmap: partially filled-in mountd RPC service tuple
822 * @argp: directory path of share to "unmount"
824 * Returns one if the unmount call succeeded; zero if the unmount
825 * failed for any reason; rpccreateerr.cf_stat is set to reflect
826 * the nature of the error.
828 * We use a fast timeout since this call is advisory only.
830 int nfs_advise_umount(const struct sockaddr *sap, const socklen_t salen,
831 const struct pmap *pmap, const dirpath *argp)
833 union nfs_sockaddr address;
834 struct sockaddr *saddr = &address.sa;
835 struct pmap mnt_pmap = *pmap;
836 struct timeval timeout = {
837 .tv_sec = MOUNT_TIMEOUT >> 3,
840 enum clnt_stat res = 0;
842 memcpy(saddr, sap, salen);
843 if (nfs_probe_mntport(saddr, salen, &mnt_pmap) == 0) {
845 nfs_error(_("%s: Failed to discover mountd port%s"),
846 progname, clnt_spcreateerror(""));
849 nfs_set_port(saddr, mnt_pmap.pm_port);
851 client = nfs_get_priv_rpcclient(saddr, salen, mnt_pmap.pm_prot,
852 mnt_pmap.pm_prog, mnt_pmap.pm_vers,
854 if (client == NULL) {
856 nfs_error(_("%s: Failed to create RPC client%s"),
857 progname, clnt_spcreateerror(""));
861 client->cl_auth = nfs_authsys_create();
862 if (client->cl_auth == NULL) {
864 nfs_error(_("%s: Failed to create RPC auth handle"),
866 CLNT_DESTROY(client);
870 res = CLNT_CALL(client, MOUNTPROC_UMNT,
871 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
872 (xdrproc_t)xdr_void, NULL,
874 if (res != RPC_SUCCESS) {
875 rpc_createerr.cf_stat = res;
876 CLNT_GETERR(client, &rpc_createerr.cf_error);
878 nfs_error(_("%s: UMNT call failed: %s"),
879 progname, clnt_sperrno(res));
882 auth_destroy(client->cl_auth);
883 CLNT_DESTROY(client);
885 if (res != RPC_SUCCESS)
891 * nfs_call_umount - ask the server to remove a share from it's rmtab
892 * @mnt_server: address of RPC MNT program server
893 * @argp: directory path of share to "unmount"
895 * Returns one if the unmount call succeeded; zero if the unmount
896 * failed for any reason.
898 * Note that a side effect of calling this function is that rpccreateerr
901 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
903 struct sockaddr *sap = (struct sockaddr *)&mnt_server->saddr;
904 socklen_t salen = sizeof(mnt_server->saddr);
905 struct pmap *pmap = &mnt_server->pmap;
907 enum clnt_stat res = 0;
910 if (!nfs_probe_mntport(sap, salen, pmap))
912 clnt = mnt_openclnt(mnt_server, &msock);
915 res = clnt_call(clnt, MOUNTPROC_UMNT,
916 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
917 (xdrproc_t)xdr_void, NULL,
919 mnt_closeclnt(clnt, msock);
921 if (res == RPC_SUCCESS)
927 * mnt_openclnt - get a handle for a remote mountd service
928 * @mnt_server: address and pmap arguments of mountd service
929 * @msock: returns a file descriptor of the underlying transport socket
931 * Returns an active handle for the remote's mountd service
933 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
935 struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
936 struct pmap *mnt_pmap = &mnt_server->pmap;
939 mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
940 *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, MOUNT_TIMEOUT,
942 if (*msock == RPC_ANYSOCK) {
943 if (rpc_createerr.cf_error.re_errno == EADDRINUSE)
945 * Probably in-use by a TIME_WAIT connection,
946 * It is worth waiting a while and trying again.
948 rpc_createerr.cf_stat = RPC_TIMEDOUT;
952 switch (mnt_pmap->pm_prot) {
954 clnt = clntudp_bufcreate(mnt_saddr,
955 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
956 RETRY_TIMEOUT, msock,
957 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
960 clnt = clnttcp_create(mnt_saddr,
961 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
963 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
967 /* try to mount hostname:dirname */
968 clnt->cl_auth = nfs_authsys_create();
977 * mnt_closeclnt - terminate a handle for a remote mountd service
978 * @clnt: pointer to an active handle for a remote mountd service
979 * @msock: file descriptor of the underlying transport socket
982 void mnt_closeclnt(CLIENT *clnt, int msock)
984 auth_destroy(clnt->cl_auth);
990 * clnt_ping - send an RPC ping to the remote RPC service endpoint
991 * @saddr: server's address
992 * @prog: target RPC program number
993 * @vers: target RPC version number
994 * @prot: target RPC protocol
995 * @caddr: filled in with our network address
997 * Sigh... GETPORT queries don't actually check the version number.
998 * In order to make sure that the server actually supports the service
999 * we're requesting, we open an RPC client, and fire off a NULL
1002 * caddr is the network address that the server will use to call us back.
1003 * On multi-homed clients, this address depends on which NIC we use to
1004 * route requests to the server.
1006 * Returns one if successful, otherwise zero.
1008 int clnt_ping(struct sockaddr_in *saddr, const unsigned long prog,
1009 const unsigned long vers, const unsigned int prot,
1010 struct sockaddr_in *caddr)
1012 CLIENT *clnt = NULL;
1014 static char clnt_res;
1015 struct sockaddr dissolve;
1017 rpc_createerr.cf_stat = stat = 0;
1018 sock = get_socket(saddr, prot, CONNECT_TIMEOUT, FALSE, TRUE);
1019 if (sock == RPC_ANYSOCK) {
1020 if (rpc_createerr.cf_error.re_errno == ETIMEDOUT) {
1022 * TCP timeout. Bubble up the error to see
1023 * how it should be handled.
1025 rpc_createerr.cf_stat = RPC_TIMEDOUT;
1031 /* Get the address of our end of this connection */
1032 socklen_t len = sizeof(*caddr);
1033 if (getsockname(sock, caddr, &len) != 0)
1034 caddr->sin_family = 0;
1039 /* The socket is connected (so we could getsockname successfully),
1040 * but some servers on multi-homed hosts reply from
1041 * the wrong address, so if we stay connected, we lose the reply.
1043 dissolve.sa_family = AF_UNSPEC;
1044 connect(sock, &dissolve, sizeof(dissolve));
1046 clnt = clntudp_bufcreate(saddr, prog, vers,
1047 RETRY_TIMEOUT, &sock,
1048 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
1051 clnt = clnttcp_create(saddr, prog, vers, &sock,
1052 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
1059 memset(&clnt_res, 0, sizeof(clnt_res));
1060 stat = clnt_call(clnt, NULLPROC,
1061 (xdrproc_t)xdr_void, (caddr_t)NULL,
1062 (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
1065 clnt_geterr(clnt, &rpc_createerr.cf_error);
1066 rpc_createerr.cf_stat = stat;
1071 if (stat == RPC_SUCCESS)
1078 * Try a getsockname() on a connected datagram socket.
1080 * Returns 1 and fills in @buf if successful; otherwise, zero.
1082 * A connected datagram socket prevents leaving a socket in TIME_WAIT.
1083 * This conserves the ephemeral port number space, helping reduce failed
1084 * socket binds during mount storms.
1086 static int nfs_ca_sockname(const struct sockaddr *sap, const socklen_t salen,
1087 struct sockaddr *buf, socklen_t *buflen)
1089 struct sockaddr_in sin = {
1090 .sin_family = AF_INET,
1091 .sin_addr.s_addr = htonl(INADDR_ANY),
1093 struct sockaddr_in6 sin6 = {
1094 .sin6_family = AF_INET6,
1095 .sin6_addr = IN6ADDR_ANY_INIT,
1099 sock = socket(sap->sa_family, SOCK_DGRAM, IPPROTO_UDP);
1103 switch (sap->sa_family) {
1105 if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
1111 if (bind(sock, (struct sockaddr *)&sin6, sizeof(sin6)) < 0) {
1117 errno = EAFNOSUPPORT;
1121 if (connect(sock, sap, salen) < 0) {
1126 return !getsockname(sock, buf, buflen);
1130 * Try to generate an address that prevents the server from calling us.
1132 * Returns 1 and fills in @buf if successful; otherwise, zero.
1134 static int nfs_ca_gai(const struct sockaddr *sap,
1135 struct sockaddr *buf, socklen_t *buflen)
1137 struct addrinfo *gai_results;
1138 struct addrinfo gai_hint = {
1139 .ai_family = sap->sa_family,
1140 .ai_flags = AI_PASSIVE, /* ANYADDR */
1143 if (getaddrinfo(NULL, "", &gai_hint, &gai_results))
1146 *buflen = gai_results->ai_addrlen;
1147 memcpy(buf, gai_results->ai_addr, *buflen);
1149 freeaddrinfo(gai_results);
1155 * nfs_callback_address - acquire our local network address
1156 * @sap: pointer to address of remote
1157 * @sap_len: length of address
1158 * @buf: pointer to buffer to be filled in with local network address
1159 * @buflen: IN: length of buffer to fill in; OUT: length of filled-in address
1161 * Discover a network address that an NFSv4 server can use to call us back.
1162 * On multi-homed clients, this address depends on which NIC we use to
1163 * route requests to the server.
1165 * Returns 1 and fills in @buf if an unambiguous local address is
1166 * available; returns 1 and fills in an appropriate ANYADDR address
1167 * if a local address isn't available; otherwise, returns zero.
1169 int nfs_callback_address(const struct sockaddr *sap, const socklen_t salen,
1170 struct sockaddr *buf, socklen_t *buflen)
1172 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1174 if (nfs_ca_sockname(sap, salen, buf, buflen) == 0)
1175 if (nfs_ca_gai(sap, buf, buflen) == 0)
1179 * The server can't use an interface ID that was generated
1180 * here on the client, so always clear sin6_scope_id.
1182 if (sin6->sin6_family == AF_INET6)
1183 sin6->sin6_scope_id = 0;
1190 nfs_error(_("%s: failed to construct callback address"),
1196 * "nfsprog" is supported only by the legacy mount command. The
1197 * kernel mount client does not support this option.
1199 * Returns TRUE if @program contains a valid value for this option,
1200 * or FALSE if the option was specified with an invalid value.
1203 nfs_nfs_program(struct mount_options *options, unsigned long *program)
1207 switch (po_get_numeric(options, "nfsprog", &tmp)) {
1216 nfs_error(_("%s: invalid value for 'nfsprog=' option"),
1222 * NFS RPC program wasn't specified. The RPC program
1223 * cannot be determined via an rpcbind query.
1225 *program = nfs_getrpcbyname(NFSPROG, nfs_nfs_pgmtbl);
1230 * Returns TRUE if @version contains a valid value for this option,
1231 * or FALSE if the option was specified with an invalid value.
1234 nfs_nfs_version(struct mount_options *options, unsigned long *version)
1238 switch (po_rightmost(options, nfs_version_opttbl)) {
1249 switch (po_get_numeric(options, "vers", &tmp)) {
1251 if (tmp >= 2 && tmp <= 4) {
1257 nfs_error(_("%s: parsing error on 'vers=' option\n"),
1261 nfs_error(_("%s: invalid value for 'vers=' option"),
1265 case 4: /* nfsvers */
1266 switch (po_get_numeric(options, "nfsvers", &tmp)) {
1268 if (tmp >= 2 && tmp <= 4) {
1274 nfs_error(_("%s: parsing error on 'nfsvers=' option\n"),
1278 nfs_error(_("%s: invalid value for 'nfsvers=' option"),
1285 * NFS version wasn't specified. The pmap version value
1286 * will be filled in later by an rpcbind query in this case.
1293 * Returns TRUE if @protocol contains a valid value for this option,
1294 * or FALSE if the option was specified with an invalid value. On
1295 * error, errno is set.
1298 nfs_nfs_protocol(struct mount_options *options, unsigned long *protocol)
1303 switch (po_rightmost(options, nfs_transport_opttbl)) {
1305 *protocol = IPPROTO_UDP;
1308 *protocol = IPPROTO_TCP;
1311 option = po_get(options, "proto");
1312 if (option != NULL) {
1313 if (!nfs_get_proto(option, &family, protocol)) {
1314 errno = EPROTONOSUPPORT;
1315 nfs_error(_("%s: Failed to find '%s' protocol"),
1324 * NFS transport protocol wasn't specified. The pmap
1325 * protocol value will be filled in later by an rpcbind
1326 * query in this case.
1333 * Returns TRUE if @port contains a valid value for this option,
1334 * or FALSE if the option was specified with an invalid value.
1337 nfs_nfs_port(struct mount_options *options, unsigned long *port)
1341 switch (po_get_numeric(options, "port", &tmp)) {
1345 if (tmp >= 1 && tmp <= 65535) {
1350 nfs_error(_("%s: invalid value for 'port=' option"),
1356 * NFS service port wasn't specified. The pmap port value
1357 * will be filled in later by an rpcbind query in this case.
1363 #ifdef IPV6_SUPPORTED
1364 sa_family_t config_default_family = AF_UNSPEC;
1367 nfs_verify_family(sa_family_t UNUSED(family))
1371 #else /* IPV6_SUPPORTED */
1372 sa_family_t config_default_family = AF_INET;
1375 nfs_verify_family(sa_family_t family)
1377 if (family != AF_INET)
1382 #endif /* IPV6_SUPPORTED */
1385 * Returns TRUE and fills in @family if a valid NFS protocol option
1386 * is found, or FALSE if the option was specified with an invalid value
1387 * or if the protocol family isn't supported. On error, errno is set.
1389 int nfs_nfs_proto_family(struct mount_options *options,
1390 sa_family_t *family)
1392 unsigned long protocol;
1394 sa_family_t tmp_family = config_default_family;
1396 switch (po_rightmost(options, nfs_transport_opttbl)) {
1399 /* for compatibility; these are always AF_INET */
1403 option = po_get(options, "proto");
1404 if (option != NULL &&
1405 !nfs_get_proto(option, &tmp_family, &protocol)) {
1407 nfs_error(_("%s: Failed to find '%s' protocol"),
1409 errno = EPROTONOSUPPORT;
1414 if (!nfs_verify_family(tmp_family))
1416 *family = tmp_family;
1419 errno = EAFNOSUPPORT;
1424 * "mountprog" is supported only by the legacy mount command. The
1425 * kernel mount client does not support this option.
1427 * Returns TRUE if @program contains a valid value for this option,
1428 * or FALSE if the option was specified with an invalid value.
1431 nfs_mount_program(struct mount_options *options, unsigned long *program)
1435 switch (po_get_numeric(options, "mountprog", &tmp)) {
1444 nfs_error(_("%s: invalid value for 'mountprog=' option"),
1450 * MNT RPC program wasn't specified. The RPC program
1451 * cannot be determined via an rpcbind query.
1453 *program = nfs_getrpcbyname(MOUNTPROG, nfs_mnt_pgmtbl);
1458 * Returns TRUE if @version contains a valid value for this option,
1459 * or FALSE if the option was specified with an invalid value.
1462 nfs_mount_version(struct mount_options *options, unsigned long *version)
1466 switch (po_get_numeric(options, "mountvers", &tmp)) {
1470 if (tmp >= 1 && tmp <= 4) {
1475 nfs_error(_("%s: invalid value for 'mountvers=' option"),
1481 * MNT version wasn't specified. The pmap version value
1482 * will be filled in later by an rpcbind query in this case.
1489 * Returns TRUE if @protocol contains a valid value for this option,
1490 * or FALSE if the option was specified with an invalid value. On
1491 * error, errno is set.
1494 nfs_mount_protocol(struct mount_options *options, unsigned long *protocol)
1499 option = po_get(options, "mountproto");
1500 if (option != NULL) {
1501 if (!nfs_get_proto(option, &family, protocol)) {
1502 errno = EPROTONOSUPPORT;
1503 nfs_error(_("%s: Failed to find '%s' protocol"),
1511 * MNT transport protocol wasn't specified. If the NFS
1512 * transport protocol was specified, use that; otherwise
1513 * set @protocol to zero. The pmap protocol value will
1514 * be filled in later by an rpcbind query in this case.
1516 return nfs_nfs_protocol(options, protocol);
1520 * Returns TRUE if @port contains a valid value for this option,
1521 * or FALSE if the option was specified with an invalid value.
1524 nfs_mount_port(struct mount_options *options, unsigned long *port)
1528 switch (po_get_numeric(options, "mountport", &tmp)) {
1532 if (tmp >= 1 && tmp <= 65535) {
1537 nfs_error(_("%s: invalid value for 'mountport=' option"),
1543 * MNT service port wasn't specified. The pmap port value
1544 * will be filled in later by an rpcbind query in this case.
1551 * Returns TRUE and fills in @family if a valid MNT protocol option
1552 * is found, or FALSE if the option was specified with an invalid value
1553 * or if the protocol family isn't supported. On error, errno is set.
1555 int nfs_mount_proto_family(struct mount_options *options,
1556 sa_family_t *family)
1558 unsigned long protocol;
1560 sa_family_t tmp_family = config_default_family;
1562 option = po_get(options, "mountproto");
1563 if (option != NULL) {
1564 if (!nfs_get_proto(option, &tmp_family, &protocol)) {
1565 nfs_error(_("%s: Failed to find '%s' protocol"),
1567 errno = EPROTONOSUPPORT;
1570 if (!nfs_verify_family(tmp_family))
1572 *family = tmp_family;
1577 * MNT transport protocol wasn't specified. If the NFS
1578 * transport protocol was specified, derive the family
1579 * from that; otherwise, return the default family for
1582 return nfs_nfs_proto_family(options, family);
1584 errno = EAFNOSUPPORT;
1589 * nfs_options2pmap - set up pmap structs based on mount options
1590 * @options: pointer to mount options
1591 * @nfs_pmap: OUT: pointer to pmap arguments for NFS server
1592 * @mnt_pmap: OUT: pointer to pmap arguments for mountd server
1594 * Returns TRUE if the pmap options specified in @options have valid
1595 * values; otherwise FALSE is returned.
1597 int nfs_options2pmap(struct mount_options *options,
1598 struct pmap *nfs_pmap, struct pmap *mnt_pmap)
1600 if (!nfs_nfs_program(options, &nfs_pmap->pm_prog))
1602 if (!nfs_nfs_version(options, &nfs_pmap->pm_vers))
1604 if (!nfs_nfs_protocol(options, &nfs_pmap->pm_prot))
1606 if (!nfs_nfs_port(options, &nfs_pmap->pm_port))
1609 if (!nfs_mount_program(options, &mnt_pmap->pm_prog))
1611 if (!nfs_mount_version(options, &mnt_pmap->pm_vers))
1613 if (!nfs_mount_protocol(options, &mnt_pmap->pm_prot))
1615 if (!nfs_mount_port(options, &mnt_pmap->pm_port))