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[] = {
91 static const char *nfs_version_opttbl[] = {
100 static const unsigned long nfs_to_mnt[] = {
107 static const unsigned long mnt_to_nfs[] = {
115 * Map an NFS version into the corresponding Mountd version
117 unsigned long nfsvers_to_mnt(const unsigned long vers)
120 return nfs_to_mnt[vers];
125 * Map a Mountd version into the corresponding NFS version
127 static unsigned long mntvers_to_nfs(const unsigned long vers)
130 return mnt_to_nfs[vers];
134 static const unsigned int probe_udp_only[] = {
139 static const unsigned int probe_udp_first[] = {
145 static const unsigned int probe_tcp_first[] = {
151 static const unsigned long probe_nfs2_only[] = {
156 static const unsigned long probe_nfs3_first[] = {
162 static const unsigned long probe_mnt1_first[] = {
168 static const unsigned long probe_mnt3_first[] = {
175 static const unsigned int *nfs_default_proto(void);
177 static const unsigned int *nfs_default_proto()
179 extern unsigned long config_default_proto;
181 * If the default proto has been set and
182 * its not TCP, start with UDP
184 if (config_default_proto && config_default_proto != IPPROTO_TCP)
185 return probe_udp_first;
187 return probe_tcp_first;
190 static const unsigned int *nfs_default_proto()
192 return probe_tcp_first;
194 #endif /* MOUNT_CONFIG */
197 * nfs_lookup - resolve hostname to an IPv4 or IPv6 socket address
198 * @hostname: pointer to C string containing DNS hostname to resolve
199 * @family: address family hint
200 * @sap: pointer to buffer to fill with socket address
201 * @len: IN: size of buffer to fill; OUT: size of socket address
203 * Returns 1 and places a socket address at @sap if successful;
206 int nfs_lookup(const char *hostname, const sa_family_t family,
207 struct sockaddr *sap, socklen_t *salen)
209 struct addrinfo *gai_results;
210 struct addrinfo gai_hint = {
211 #ifdef HAVE_DECL_AI_ADDRCONFIG
212 .ai_flags = AI_ADDRCONFIG,
213 #endif /* HAVE_DECL_AI_ADDRCONFIG */
216 socklen_t len = *salen;
221 error = getaddrinfo(hostname, NULL, &gai_hint, &gai_results);
226 nfs_error(_("%s: DNS resolution failed for %s: %s"),
227 progname, hostname, strerror(errno));
230 nfs_error(_("%s: DNS resolution failed for %s: %s"),
231 progname, hostname, gai_strerror(error));
235 switch (gai_results->ai_addr->sa_family) {
238 if (len >= gai_results->ai_addrlen) {
239 *salen = gai_results->ai_addrlen;
240 memcpy(sap, gai_results->ai_addr, *salen);
245 /* things are really broken if we get here, so warn */
246 nfs_error(_("%s: unrecognized DNS resolution results for %s"),
251 freeaddrinfo(gai_results);
256 * nfs_gethostbyname - resolve a hostname to an IPv4 address
257 * @hostname: pointer to a C string containing a DNS hostname
258 * @sin: returns an IPv4 address
260 * Returns 1 if successful, otherwise zero.
262 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *sin)
264 socklen_t len = sizeof(*sin);
266 return nfs_lookup(hostname, AF_INET, (struct sockaddr *)sin, &len);
270 * nfs_string_to_sockaddr - convert string address to sockaddr
271 * @address: pointer to presentation format address to convert
272 * @sap: pointer to socket address buffer to fill in
273 * @salen: IN: length of address buffer
274 * OUT: length of converted socket address
276 * Convert a presentation format address string to a socket address.
277 * Similar to nfs_lookup(), but the DNS query is squelched, and it
278 * won't make any noise if the getaddrinfo() call fails.
280 * Returns 1 and fills in @sap and @salen if successful; otherwise zero.
282 * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
283 * on presenting IPv6 addresses as text strings.
285 int nfs_string_to_sockaddr(const char *address, struct sockaddr *sap,
288 struct addrinfo *gai_results;
289 struct addrinfo gai_hint = {
290 .ai_flags = AI_NUMERICHOST,
292 socklen_t len = *salen;
297 if (getaddrinfo(address, NULL, &gai_hint, &gai_results) == 0) {
298 switch (gai_results->ai_addr->sa_family) {
301 if (len >= gai_results->ai_addrlen) {
302 *salen = gai_results->ai_addrlen;
303 memcpy(sap, gai_results->ai_addr, *salen);
308 freeaddrinfo(gai_results);
315 * nfs_present_sockaddr - convert sockaddr to string
316 * @sap: pointer to socket address to convert
317 * @salen: length of socket address
318 * @buf: pointer to buffer to fill in
319 * @buflen: length of buffer
321 * Convert the passed-in sockaddr-style address to presentation format.
322 * The presentation format address is placed in @buf and is
325 * Returns 1 if successful; otherwise zero.
327 * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
328 * on presenting IPv6 addresses as text strings.
330 int nfs_present_sockaddr(const struct sockaddr *sap, const socklen_t salen,
331 char *buf, const size_t buflen)
333 #ifdef HAVE_GETNAMEINFO
336 result = getnameinfo(sap, salen, buf, buflen,
337 NULL, 0, NI_NUMERICHOST);
341 nfs_error(_("%s: invalid server address: %s"), progname,
342 gai_strerror(result));
344 #else /* HAVE_GETNAMEINFO */
347 if (sap->sa_family == AF_INET) {
348 addr = inet_ntoa(((struct sockaddr_in *)sap)->sin_addr);
349 if (addr && strlen(addr) < buflen) {
355 nfs_error(_("%s: invalid server address"), progname);
357 #endif /* HAVE_GETNAMEINFO */
361 * Attempt to connect a socket, but time out after "timeout" seconds.
363 * On error return, caller closes the socket.
365 static int connect_to(int fd, struct sockaddr *addr,
366 socklen_t addrlen, int timeout)
370 struct timeval tv = {
374 saved = fcntl(fd, F_GETFL, 0);
375 fcntl(fd, F_SETFL, saved | O_NONBLOCK);
377 ret = connect(fd, addr, addrlen);
378 if (ret < 0 && errno != EINPROGRESS)
386 ret = select(fd + 1, &rset, &wset, NULL, &tv);
391 if (FD_ISSET(fd, &rset) || FD_ISSET(fd, &wset)) {
393 socklen_t len = sizeof(error);
394 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
404 fcntl(fd, F_SETFL, saved);
409 * Create a socket that is locally bound to a reserved or non-reserved port.
411 * The caller should check rpc_createerr to determine the cause of any error.
413 static int get_socket(struct sockaddr_in *saddr, unsigned int p_prot,
414 unsigned int timeout, int resvp, int conn)
417 struct sockaddr_in laddr;
418 socklen_t namelen = sizeof(laddr);
420 type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
421 if ((so = socket (AF_INET, type, p_prot)) < 0)
424 laddr.sin_family = AF_INET;
426 laddr.sin_addr.s_addr = htonl(INADDR_ANY);
428 if (bindresvport(so, &laddr) < 0)
429 goto err_bindresvport;
431 cc = bind(so, (struct sockaddr *)&laddr, namelen);
435 if (type == SOCK_STREAM || (conn && type == SOCK_DGRAM)) {
436 cc = connect_to(so, (struct sockaddr *)saddr, namelen,
444 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
445 rpc_createerr.cf_error.re_errno = errno;
447 nfs_error(_("%s: Unable to create %s socket: errno %d (%s)\n"),
448 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
449 errno, strerror(errno));
454 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
455 rpc_createerr.cf_error.re_errno = errno;
457 nfs_error(_("%s: Unable to bindresvport %s socket: errno %d"
459 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
460 errno, strerror(errno));
466 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
467 rpc_createerr.cf_error.re_errno = errno;
469 nfs_error(_("%s: Unable to bind to %s socket: errno %d (%s)\n"),
470 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
471 errno, strerror(errno));
477 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
478 rpc_createerr.cf_error.re_errno = errno;
480 nfs_error(_("%s: Unable to connect to %s:%d, errno %d (%s)\n"),
481 progname, inet_ntoa(saddr->sin_addr),
482 ntohs(saddr->sin_port), errno, strerror(errno));
488 static void nfs_pp_debug(const struct sockaddr *sap, const socklen_t salen,
489 const rpcprog_t program, const rpcvers_t version,
490 const unsigned short protocol,
491 const unsigned short port)
493 char buf[NI_MAXHOST];
498 if (nfs_present_sockaddr(sap, salen, buf, sizeof(buf)) == 0) {
500 strcat(buf, "unknown host");
503 fprintf(stderr, _("%s: trying %s prog %lu vers %lu prot %s port %d\n"),
504 progname, buf, (unsigned long)program,
505 (unsigned long)version,
506 (protocol == IPPROTO_UDP ? _("UDP") : _("TCP")),
510 static void nfs_pp_debug2(const char *str)
515 if (rpc_createerr.cf_error.re_status == RPC_CANTRECV ||
516 rpc_createerr.cf_error.re_status == RPC_CANTSEND)
517 nfs_error(_("%s: portmap query %s%s - %s"),
518 progname, str, clnt_spcreateerror(""),
519 strerror(rpc_createerr.cf_error.re_errno));
521 nfs_error(_("%s: portmap query %s%s"),
522 progname, str, clnt_spcreateerror(""));
526 * Use the portmapper to discover whether or not the service we want is
527 * available. The lists 'versions' and 'protos' define ordered sequences
528 * of service versions and udp/tcp protocols to probe for.
530 * Returns 1 if the requested service port is unambiguous and pingable;
531 * @pmap is filled in with the version, port, and transport protocol used
532 * during the successful ping. Note that if a port is already specified
533 * in @pmap and it matches the rpcbind query result, nfs_probe_port() does
534 * not perform an RPC ping.
536 * If an error occurs or the requested service isn't available, zero is
537 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
539 static int nfs_probe_port(const struct sockaddr *sap, const socklen_t salen,
540 struct pmap *pmap, const unsigned long *versions,
541 const unsigned int *protos)
543 union nfs_sockaddr address;
544 struct sockaddr *saddr = &address.sa;
545 const unsigned long prog = pmap->pm_prog, *p_vers;
546 const unsigned int prot = (u_int)pmap->pm_prot, *p_prot;
547 const u_short port = (u_short) pmap->pm_port;
548 unsigned long vers = pmap->pm_vers;
549 unsigned short p_port;
551 memcpy(saddr, sap, salen);
552 p_prot = prot ? &prot : protos;
553 p_vers = vers ? &vers : versions;
557 printf(_("%s: prog %lu, trying vers=%lu, prot=%u\n"),
558 progname, prog, *p_vers, *p_prot);
559 p_port = nfs_getport(saddr, salen, prog, *p_vers, *p_prot);
561 if (!port || port == p_port) {
562 nfs_set_port(saddr, p_port);
563 nfs_pp_debug(saddr, salen, prog, *p_vers,
565 if (nfs_rpc_ping(saddr, salen, prog,
566 *p_vers, *p_prot, NULL))
569 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
571 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
572 rpc_createerr.cf_stat != RPC_TIMEDOUT &&
573 rpc_createerr.cf_stat != RPC_CANTRECV &&
574 rpc_createerr.cf_stat != RPC_PROGVERSMISMATCH)
579 nfs_pp_debug2("retrying");
584 if (rpc_createerr.cf_stat == RPC_TIMEDOUT ||
585 rpc_createerr.cf_stat == RPC_CANTRECV)
588 if (vers || !*++p_vers)
592 nfs_pp_debug2("failed");
597 pmap->pm_vers = *p_vers;
599 pmap->pm_prot = *p_prot;
601 pmap->pm_port = p_port;
602 nfs_clear_rpc_createerr();
606 * Probe a server's NFS service to determine which versions and
607 * transport protocols are supported.
609 * Returns 1 if the requested service port is unambiguous and pingable;
610 * @pmap is filled in with the version, port, and transport protocol used
611 * during the successful ping. If all three are already specified, simply
612 * return success without an rpcbind query or RPC ping (we may be trying
613 * to mount an NFS service that is not advertised via rpcbind).
615 * If an error occurs or the requested service isn't available, zero is
616 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
618 static int nfs_probe_nfsport(const struct sockaddr *sap, const socklen_t salen,
621 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
624 if (nfs_mount_data_version >= 4) {
625 const unsigned int *probe_proto;
627 probe_proto = nfs_default_proto();
629 return nfs_probe_port(sap, salen, pmap,
630 probe_nfs3_first, probe_proto);
632 return nfs_probe_port(sap, salen, pmap,
633 probe_nfs2_only, probe_udp_only);
637 * Probe a server's mountd service to determine which versions and
638 * transport protocols are supported.
640 * Returns 1 if the requested service port is unambiguous and pingable;
641 * @pmap is filled in with the version, port, and transport protocol used
642 * during the successful ping. If all three are already specified, simply
643 * return success without an rpcbind query or RPC ping (we may be trying
644 * to mount an NFS service that is not advertised via rpcbind).
646 * If an error occurs or the requested service isn't available, zero is
647 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
649 static int nfs_probe_mntport(const struct sockaddr *sap, const socklen_t salen,
652 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
655 if (nfs_mount_data_version >= 4)
656 return nfs_probe_port(sap, salen, pmap,
657 probe_mnt3_first, probe_udp_first);
659 return nfs_probe_port(sap, salen, pmap,
660 probe_mnt1_first, probe_udp_only);
664 * Probe a server's mountd service to determine which versions and
665 * transport protocols are supported. Invoked when the protocol
666 * version is already known for both the NFS and mountd service.
668 * Returns 1 and fills in both @pmap structs if the requested service
669 * ports are unambiguous and pingable. Otherwise zero is returned;
670 * rpccreateerr.cf_stat is set to reflect the nature of the error.
672 static int nfs_probe_version_fixed(const struct sockaddr *mnt_saddr,
673 const socklen_t mnt_salen,
674 struct pmap *mnt_pmap,
675 const struct sockaddr *nfs_saddr,
676 const socklen_t nfs_salen,
677 struct pmap *nfs_pmap)
679 if (!nfs_probe_nfsport(nfs_saddr, nfs_salen, nfs_pmap))
681 return nfs_probe_mntport(mnt_saddr, mnt_salen, mnt_pmap);
685 * nfs_probe_bothports - discover the RPC endpoints of mountd and NFS server
686 * @mnt_saddr: pointer to socket address of mountd server
687 * @mnt_salen: length of mountd server's address
688 * @mnt_pmap: IN: partially filled-in mountd RPC service tuple;
689 * OUT: fully filled-in mountd RPC service tuple
690 * @nfs_saddr: pointer to socket address of NFS server
691 * @nfs_salen: length of NFS server's address
692 * @nfs_pmap: IN: partially filled-in NFS RPC service tuple;
693 * OUT: fully filled-in NFS RPC service tuple
695 * Returns 1 and fills in both @pmap structs if the requested service
696 * ports are unambiguous and pingable. Otherwise zero is returned;
697 * rpccreateerr.cf_stat is set to reflect the nature of the error.
699 int nfs_probe_bothports(const struct sockaddr *mnt_saddr,
700 const socklen_t mnt_salen,
701 struct pmap *mnt_pmap,
702 const struct sockaddr *nfs_saddr,
703 const socklen_t nfs_salen,
704 struct pmap *nfs_pmap)
706 struct pmap save_nfs, save_mnt;
707 const unsigned long *probe_vers;
709 if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
710 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
711 else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
712 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
714 if (nfs_pmap->pm_vers)
715 return nfs_probe_version_fixed(mnt_saddr, mnt_salen, mnt_pmap,
716 nfs_saddr, nfs_salen, nfs_pmap);
718 memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
719 memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
720 probe_vers = (nfs_mount_data_version >= 4) ?
721 probe_mnt3_first : probe_mnt1_first;
723 for (; *probe_vers; probe_vers++) {
724 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
725 if (nfs_probe_nfsport(nfs_saddr, nfs_salen, nfs_pmap) != 0) {
726 mnt_pmap->pm_vers = *probe_vers;
727 if (nfs_probe_mntport(mnt_saddr, mnt_salen, mnt_pmap) != 0)
729 memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
731 switch (rpc_createerr.cf_stat) {
732 case RPC_PROGVERSMISMATCH:
733 case RPC_PROGNOTREGISTERED:
738 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
745 * probe_bothports - discover the RPC endpoints of mountd and NFS server
746 * @mnt_server: pointer to address and pmap argument for mountd results
747 * @nfs_server: pointer to address and pmap argument for NFS server
749 * This is the legacy API that takes "clnt_addr_t" for both servers,
750 * but supports only AF_INET addresses.
752 * Returns 1 and fills in the pmap field in both clnt_addr_t structs
753 * if the requested service ports are unambiguous and pingable.
754 * Otherwise zero is returned; rpccreateerr.cf_stat is set to reflect
755 * the nature of the error.
757 int probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
759 return nfs_probe_bothports((struct sockaddr *)&mnt_server->saddr,
760 sizeof(mnt_server->saddr),
762 (struct sockaddr *)&nfs_server->saddr,
763 sizeof(nfs_server->saddr),
767 static int nfs_probe_statd(void)
769 struct sockaddr_in addr = {
770 .sin_family = AF_INET,
771 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
773 rpcprog_t program = nfs_getrpcbyname(NSMPROG, nfs_ns_pgmtbl);
775 return nfs_getport_ping((struct sockaddr *)&addr, sizeof(addr),
776 program, (rpcvers_t)1, IPPROTO_UDP);
780 * start_statd - attempt to start rpc.statd
782 * Returns 1 if statd is running; otherwise zero.
784 int start_statd(void)
790 if (nfs_probe_statd())
794 if (stat(START_STATD, &stb) == 0) {
795 if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {
799 execl(START_STATD, START_STATD, NULL);
802 nfs_error(_("%s: fork failed: %s"),
803 progname, strerror(errno));
805 default: /* parent */
806 waitpid(pid, NULL,0);
809 if (nfs_probe_statd())
819 * nfs_advise_umount - ask the server to remove a share from it's rmtab
820 * @sap: pointer to IP address of server to call
821 * @salen: length of server address
822 * @pmap: partially filled-in mountd RPC service tuple
823 * @argp: directory path of share to "unmount"
825 * Returns one if the unmount call succeeded; zero if the unmount
826 * failed for any reason; rpccreateerr.cf_stat is set to reflect
827 * the nature of the error.
829 * We use a fast timeout since this call is advisory only.
831 int nfs_advise_umount(const struct sockaddr *sap, const socklen_t salen,
832 const struct pmap *pmap, const dirpath *argp)
834 union nfs_sockaddr address;
835 struct sockaddr *saddr = &address.sa;
836 struct pmap mnt_pmap = *pmap;
837 struct timeval timeout = {
838 .tv_sec = MOUNT_TIMEOUT >> 3,
841 enum clnt_stat res = 0;
843 memcpy(saddr, sap, salen);
844 if (nfs_probe_mntport(saddr, salen, &mnt_pmap) == 0) {
846 nfs_error(_("%s: Failed to discover mountd port%s"),
847 progname, clnt_spcreateerror(""));
850 nfs_set_port(saddr, mnt_pmap.pm_port);
852 client = nfs_get_priv_rpcclient(saddr, salen, mnt_pmap.pm_prot,
853 mnt_pmap.pm_prog, mnt_pmap.pm_vers,
855 if (client == NULL) {
857 nfs_error(_("%s: Failed to create RPC client%s"),
858 progname, clnt_spcreateerror(""));
862 client->cl_auth = nfs_authsys_create();
863 if (client->cl_auth == NULL) {
865 nfs_error(_("%s: Failed to create RPC auth handle"),
867 CLNT_DESTROY(client);
871 res = CLNT_CALL(client, MOUNTPROC_UMNT,
872 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
873 (xdrproc_t)xdr_void, NULL,
875 if (res != RPC_SUCCESS) {
876 rpc_createerr.cf_stat = res;
877 CLNT_GETERR(client, &rpc_createerr.cf_error);
879 nfs_error(_("%s: UMNT call failed: %s"),
880 progname, clnt_sperrno(res));
883 auth_destroy(client->cl_auth);
884 CLNT_DESTROY(client);
886 if (res != RPC_SUCCESS)
892 * nfs_call_umount - ask the server to remove a share from it's rmtab
893 * @mnt_server: address of RPC MNT program server
894 * @argp: directory path of share to "unmount"
896 * Returns one if the unmount call succeeded; zero if the unmount
897 * failed for any reason.
899 * Note that a side effect of calling this function is that rpccreateerr
902 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
904 struct sockaddr *sap = (struct sockaddr *)&mnt_server->saddr;
905 socklen_t salen = sizeof(mnt_server->saddr);
906 struct pmap *pmap = &mnt_server->pmap;
908 enum clnt_stat res = 0;
911 if (!nfs_probe_mntport(sap, salen, pmap))
913 clnt = mnt_openclnt(mnt_server, &msock);
916 res = clnt_call(clnt, MOUNTPROC_UMNT,
917 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
918 (xdrproc_t)xdr_void, NULL,
920 mnt_closeclnt(clnt, msock);
922 if (res == RPC_SUCCESS)
928 * mnt_openclnt - get a handle for a remote mountd service
929 * @mnt_server: address and pmap arguments of mountd service
930 * @msock: returns a file descriptor of the underlying transport socket
932 * Returns an active handle for the remote's mountd service
934 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
936 struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
937 struct pmap *mnt_pmap = &mnt_server->pmap;
940 mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
941 *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, MOUNT_TIMEOUT,
943 if (*msock == RPC_ANYSOCK) {
944 if (rpc_createerr.cf_error.re_errno == EADDRINUSE)
946 * Probably in-use by a TIME_WAIT connection,
947 * It is worth waiting a while and trying again.
949 rpc_createerr.cf_stat = RPC_TIMEDOUT;
953 switch (mnt_pmap->pm_prot) {
955 clnt = clntudp_bufcreate(mnt_saddr,
956 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
957 RETRY_TIMEOUT, msock,
958 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
961 clnt = clnttcp_create(mnt_saddr,
962 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
964 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
968 /* try to mount hostname:dirname */
969 clnt->cl_auth = nfs_authsys_create();
978 * mnt_closeclnt - terminate a handle for a remote mountd service
979 * @clnt: pointer to an active handle for a remote mountd service
980 * @msock: file descriptor of the underlying transport socket
983 void mnt_closeclnt(CLIENT *clnt, int msock)
985 auth_destroy(clnt->cl_auth);
991 * clnt_ping - send an RPC ping to the remote RPC service endpoint
992 * @saddr: server's address
993 * @prog: target RPC program number
994 * @vers: target RPC version number
995 * @prot: target RPC protocol
996 * @caddr: filled in with our network address
998 * Sigh... GETPORT queries don't actually check the version number.
999 * In order to make sure that the server actually supports the service
1000 * we're requesting, we open an RPC client, and fire off a NULL
1003 * caddr is the network address that the server will use to call us back.
1004 * On multi-homed clients, this address depends on which NIC we use to
1005 * route requests to the server.
1007 * Returns one if successful, otherwise zero.
1009 int clnt_ping(struct sockaddr_in *saddr, const unsigned long prog,
1010 const unsigned long vers, const unsigned int prot,
1011 struct sockaddr_in *caddr)
1013 CLIENT *clnt = NULL;
1015 static char clnt_res;
1016 struct sockaddr dissolve;
1018 rpc_createerr.cf_stat = stat = 0;
1019 sock = get_socket(saddr, prot, CONNECT_TIMEOUT, FALSE, TRUE);
1020 if (sock == RPC_ANYSOCK) {
1021 if (rpc_createerr.cf_error.re_errno == ETIMEDOUT) {
1023 * TCP timeout. Bubble up the error to see
1024 * how it should be handled.
1026 rpc_createerr.cf_stat = RPC_TIMEDOUT;
1032 /* Get the address of our end of this connection */
1033 socklen_t len = sizeof(*caddr);
1034 if (getsockname(sock, caddr, &len) != 0)
1035 caddr->sin_family = 0;
1040 /* The socket is connected (so we could getsockname successfully),
1041 * but some servers on multi-homed hosts reply from
1042 * the wrong address, so if we stay connected, we lose the reply.
1044 dissolve.sa_family = AF_UNSPEC;
1045 connect(sock, &dissolve, sizeof(dissolve));
1047 clnt = clntudp_bufcreate(saddr, prog, vers,
1048 RETRY_TIMEOUT, &sock,
1049 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
1052 clnt = clnttcp_create(saddr, prog, vers, &sock,
1053 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
1060 memset(&clnt_res, 0, sizeof(clnt_res));
1061 stat = clnt_call(clnt, NULLPROC,
1062 (xdrproc_t)xdr_void, (caddr_t)NULL,
1063 (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
1066 clnt_geterr(clnt, &rpc_createerr.cf_error);
1067 rpc_createerr.cf_stat = stat;
1072 if (stat == RPC_SUCCESS)
1079 * Try a getsockname() on a connected datagram socket.
1081 * Returns 1 and fills in @buf if successful; otherwise, zero.
1083 * A connected datagram socket prevents leaving a socket in TIME_WAIT.
1084 * This conserves the ephemeral port number space, helping reduce failed
1085 * socket binds during mount storms.
1087 static int nfs_ca_sockname(const struct sockaddr *sap, const socklen_t salen,
1088 struct sockaddr *buf, socklen_t *buflen)
1090 struct sockaddr_in sin = {
1091 .sin_family = AF_INET,
1092 .sin_addr.s_addr = htonl(INADDR_ANY),
1094 struct sockaddr_in6 sin6 = {
1095 .sin6_family = AF_INET6,
1096 .sin6_addr = IN6ADDR_ANY_INIT,
1100 sock = socket(sap->sa_family, SOCK_DGRAM, IPPROTO_UDP);
1104 switch (sap->sa_family) {
1106 if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
1112 if (bind(sock, (struct sockaddr *)&sin6, sizeof(sin6)) < 0) {
1118 errno = EAFNOSUPPORT;
1122 if (connect(sock, sap, salen) < 0) {
1127 return !getsockname(sock, buf, buflen);
1131 * Try to generate an address that prevents the server from calling us.
1133 * Returns 1 and fills in @buf if successful; otherwise, zero.
1135 static int nfs_ca_gai(const struct sockaddr *sap,
1136 struct sockaddr *buf, socklen_t *buflen)
1138 struct addrinfo *gai_results;
1139 struct addrinfo gai_hint = {
1140 .ai_family = sap->sa_family,
1141 .ai_flags = AI_PASSIVE, /* ANYADDR */
1144 if (getaddrinfo(NULL, "", &gai_hint, &gai_results))
1147 *buflen = gai_results->ai_addrlen;
1148 memcpy(buf, gai_results->ai_addr, *buflen);
1150 freeaddrinfo(gai_results);
1156 * nfs_callback_address - acquire our local network address
1157 * @sap: pointer to address of remote
1158 * @sap_len: length of address
1159 * @buf: pointer to buffer to be filled in with local network address
1160 * @buflen: IN: length of buffer to fill in; OUT: length of filled-in address
1162 * Discover a network address that an NFSv4 server can use to call us back.
1163 * On multi-homed clients, this address depends on which NIC we use to
1164 * route requests to the server.
1166 * Returns 1 and fills in @buf if an unambiguous local address is
1167 * available; returns 1 and fills in an appropriate ANYADDR address
1168 * if a local address isn't available; otherwise, returns zero.
1170 int nfs_callback_address(const struct sockaddr *sap, const socklen_t salen,
1171 struct sockaddr *buf, socklen_t *buflen)
1173 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1175 if (nfs_ca_sockname(sap, salen, buf, buflen) == 0)
1176 if (nfs_ca_gai(sap, buf, buflen) == 0)
1180 * The server can't use an interface ID that was generated
1181 * here on the client, so always clear sin6_scope_id.
1183 if (sin6->sin6_family == AF_INET6)
1184 sin6->sin6_scope_id = 0;
1191 nfs_error(_("%s: failed to construct callback address"),
1197 * "nfsprog" is supported only by the legacy mount command. The
1198 * kernel mount client does not support this option.
1200 * Returns TRUE if @program contains a valid value for this option,
1201 * or FALSE if the option was specified with an invalid value.
1204 nfs_nfs_program(struct mount_options *options, unsigned long *program)
1208 switch (po_get_numeric(options, "nfsprog", &tmp)) {
1217 nfs_error(_("%s: invalid value for 'nfsprog=' option"),
1223 * NFS RPC program wasn't specified. The RPC program
1224 * cannot be determined via an rpcbind query.
1226 *program = nfs_getrpcbyname(NFSPROG, nfs_nfs_pgmtbl);
1231 * Returns TRUE if @version contains a valid value for this option,
1232 * or FALSE if the option was specified with an invalid value.
1235 nfs_nfs_version(struct mount_options *options, unsigned long *version)
1239 switch (po_rightmost(options, nfs_version_opttbl)) {
1250 switch (po_get_numeric(options, "vers", &tmp)) {
1252 if (tmp >= 2 && tmp <= 4) {
1258 nfs_error(_("%s: parsing error on 'vers=' option\n"),
1262 nfs_error(_("%s: invalid value for 'vers=' option"),
1266 case 4: /* nfsvers */
1267 switch (po_get_numeric(options, "nfsvers", &tmp)) {
1269 if (tmp >= 2 && tmp <= 4) {
1275 nfs_error(_("%s: parsing error on 'nfsvers=' option\n"),
1279 nfs_error(_("%s: invalid value for 'nfsvers=' option"),
1286 * NFS version wasn't specified. The pmap version value
1287 * will be filled in later by an rpcbind query in this case.
1294 * Returns TRUE if @protocol contains a valid value for this option,
1295 * or FALSE if the option was specified with an invalid value. On
1296 * error, errno is set.
1299 nfs_nfs_protocol(struct mount_options *options, unsigned long *protocol)
1304 switch (po_rightmost(options, nfs_transport_opttbl)) {
1306 *protocol = IPPROTO_UDP;
1309 *protocol = IPPROTO_TCP;
1312 *protocol = NFSPROTO_RDMA;
1315 option = po_get(options, "proto");
1316 if (option != NULL) {
1317 if (!nfs_get_proto(option, &family, protocol)) {
1318 errno = EPROTONOSUPPORT;
1319 nfs_error(_("%s: Failed to find '%s' protocol"),
1328 * NFS transport protocol wasn't specified. The pmap
1329 * protocol value will be filled in later by an rpcbind
1330 * query in this case.
1337 * Returns TRUE if @port contains a valid value for this option,
1338 * or FALSE if the option was specified with an invalid value.
1341 nfs_nfs_port(struct mount_options *options, unsigned long *port)
1345 switch (po_get_numeric(options, "port", &tmp)) {
1349 if (tmp >= 1 && tmp <= 65535) {
1354 nfs_error(_("%s: invalid value for 'port=' option"),
1360 * NFS service port wasn't specified. The pmap port value
1361 * will be filled in later by an rpcbind query in this case.
1367 #ifdef IPV6_SUPPORTED
1368 sa_family_t config_default_family = AF_UNSPEC;
1371 nfs_verify_family(sa_family_t UNUSED(family))
1375 #else /* IPV6_SUPPORTED */
1376 sa_family_t config_default_family = AF_INET;
1379 nfs_verify_family(sa_family_t family)
1381 if (family != AF_INET)
1386 #endif /* IPV6_SUPPORTED */
1389 * Returns TRUE and fills in @family if a valid NFS protocol option
1390 * is found, or FALSE if the option was specified with an invalid value
1391 * or if the protocol family isn't supported. On error, errno is set.
1393 int nfs_nfs_proto_family(struct mount_options *options,
1394 sa_family_t *family)
1396 unsigned long protocol;
1398 sa_family_t tmp_family = config_default_family;
1400 switch (po_rightmost(options, nfs_transport_opttbl)) {
1404 /* for compatibility; these are always AF_INET */
1408 option = po_get(options, "proto");
1409 if (option != NULL &&
1410 !nfs_get_proto(option, &tmp_family, &protocol)) {
1412 nfs_error(_("%s: Failed to find '%s' protocol"),
1414 errno = EPROTONOSUPPORT;
1419 if (!nfs_verify_family(tmp_family))
1421 *family = tmp_family;
1424 errno = EAFNOSUPPORT;
1429 * "mountprog" is supported only by the legacy mount command. The
1430 * kernel mount client does not support this option.
1432 * Returns TRUE if @program contains a valid value for this option,
1433 * or FALSE if the option was specified with an invalid value.
1436 nfs_mount_program(struct mount_options *options, unsigned long *program)
1440 switch (po_get_numeric(options, "mountprog", &tmp)) {
1449 nfs_error(_("%s: invalid value for 'mountprog=' option"),
1455 * MNT RPC program wasn't specified. The RPC program
1456 * cannot be determined via an rpcbind query.
1458 *program = nfs_getrpcbyname(MOUNTPROG, nfs_mnt_pgmtbl);
1463 * Returns TRUE if @version contains a valid value for this option,
1464 * or FALSE if the option was specified with an invalid value.
1467 nfs_mount_version(struct mount_options *options, unsigned long *version)
1471 switch (po_get_numeric(options, "mountvers", &tmp)) {
1475 if (tmp >= 1 && tmp <= 4) {
1480 nfs_error(_("%s: invalid value for 'mountvers=' option"),
1486 * MNT version wasn't specified. The pmap version value
1487 * will be filled in later by an rpcbind query in this case.
1494 * Returns TRUE if @protocol contains a valid value for this option,
1495 * or FALSE if the option was specified with an invalid value. On
1496 * error, errno is set.
1499 nfs_mount_protocol(struct mount_options *options, unsigned long *protocol)
1504 option = po_get(options, "mountproto");
1505 if (option != NULL) {
1506 if (!nfs_get_proto(option, &family, protocol)) {
1507 errno = EPROTONOSUPPORT;
1508 nfs_error(_("%s: Failed to find '%s' protocol"),
1516 * MNT transport protocol wasn't specified. If the NFS
1517 * transport protocol was specified, use that; otherwise
1518 * set @protocol to zero. The pmap protocol value will
1519 * be filled in later by an rpcbind query in this case.
1521 return nfs_nfs_protocol(options, protocol);
1525 * Returns TRUE if @port contains a valid value for this option,
1526 * or FALSE if the option was specified with an invalid value.
1529 nfs_mount_port(struct mount_options *options, unsigned long *port)
1533 switch (po_get_numeric(options, "mountport", &tmp)) {
1537 if (tmp >= 1 && tmp <= 65535) {
1542 nfs_error(_("%s: invalid value for 'mountport=' option"),
1548 * MNT service port wasn't specified. The pmap port value
1549 * will be filled in later by an rpcbind query in this case.
1556 * Returns TRUE and fills in @family if a valid MNT protocol option
1557 * is found, or FALSE if the option was specified with an invalid value
1558 * or if the protocol family isn't supported. On error, errno is set.
1560 int nfs_mount_proto_family(struct mount_options *options,
1561 sa_family_t *family)
1563 unsigned long protocol;
1565 sa_family_t tmp_family = config_default_family;
1567 option = po_get(options, "mountproto");
1568 if (option != NULL) {
1569 if (!nfs_get_proto(option, &tmp_family, &protocol)) {
1570 nfs_error(_("%s: Failed to find '%s' protocol"),
1572 errno = EPROTONOSUPPORT;
1575 if (!nfs_verify_family(tmp_family))
1577 *family = tmp_family;
1582 * MNT transport protocol wasn't specified. If the NFS
1583 * transport protocol was specified, derive the family
1584 * from that; otherwise, return the default family for
1587 return nfs_nfs_proto_family(options, family);
1589 errno = EAFNOSUPPORT;
1594 * nfs_options2pmap - set up pmap structs based on mount options
1595 * @options: pointer to mount options
1596 * @nfs_pmap: OUT: pointer to pmap arguments for NFS server
1597 * @mnt_pmap: OUT: pointer to pmap arguments for mountd server
1599 * Returns TRUE if the pmap options specified in @options have valid
1600 * values; otherwise FALSE is returned.
1602 int nfs_options2pmap(struct mount_options *options,
1603 struct pmap *nfs_pmap, struct pmap *mnt_pmap)
1605 if (!nfs_nfs_program(options, &nfs_pmap->pm_prog))
1607 if (!nfs_nfs_version(options, &nfs_pmap->pm_vers))
1609 if (!nfs_nfs_protocol(options, &nfs_pmap->pm_prot))
1611 if (!nfs_nfs_port(options, &nfs_pmap->pm_port))
1614 if (!nfs_mount_program(options, &mnt_pmap->pm_prog))
1616 if (!nfs_mount_version(options, &mnt_pmap->pm_vers))
1618 if (!nfs_mount_protocol(options, &mnt_pmap->pm_prot))
1620 if (!nfs_mount_port(options, &mnt_pmap->pm_port))