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.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 0211-1301 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_only[] = {
163 static const unsigned long probe_mnt1_first[] = {
169 static const unsigned long probe_mnt3_only[] = {
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 = {
212 socklen_t len = *salen;
217 error = getaddrinfo(hostname, NULL, &gai_hint, &gai_results);
222 nfs_error(_("%s: DNS resolution failed for %s: %s"),
223 progname, hostname, strerror(errno));
226 nfs_error(_("%s: DNS resolution failed for %s: %s"),
227 progname, hostname, gai_strerror(error));
231 switch (gai_results->ai_addr->sa_family) {
234 if (len >= gai_results->ai_addrlen) {
235 *salen = gai_results->ai_addrlen;
236 memcpy(sap, gai_results->ai_addr, *salen);
241 /* things are really broken if we get here, so warn */
242 nfs_error(_("%s: unrecognized DNS resolution results for %s"),
247 freeaddrinfo(gai_results);
252 * nfs_gethostbyname - resolve a hostname to an IPv4 address
253 * @hostname: pointer to a C string containing a DNS hostname
254 * @sin: returns an IPv4 address
256 * Returns 1 if successful, otherwise zero.
258 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *sin)
260 socklen_t len = sizeof(*sin);
262 return nfs_lookup(hostname, AF_INET, (struct sockaddr *)sin, &len);
266 * nfs_string_to_sockaddr - convert string address to sockaddr
267 * @address: pointer to presentation format address to convert
268 * @sap: pointer to socket address buffer to fill in
269 * @salen: IN: length of address buffer
270 * OUT: length of converted socket address
272 * Convert a presentation format address string to a socket address.
273 * Similar to nfs_lookup(), but the DNS query is squelched, and it
274 * won't make any noise if the getaddrinfo() call fails.
276 * Returns 1 and fills in @sap and @salen if successful; otherwise zero.
278 * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
279 * on presenting IPv6 addresses as text strings.
281 int nfs_string_to_sockaddr(const char *address, struct sockaddr *sap,
284 struct addrinfo *gai_results;
285 struct addrinfo gai_hint = {
286 .ai_flags = AI_NUMERICHOST,
288 socklen_t len = *salen;
293 if (getaddrinfo(address, NULL, &gai_hint, &gai_results) == 0) {
294 switch (gai_results->ai_addr->sa_family) {
297 if (len >= gai_results->ai_addrlen) {
298 *salen = gai_results->ai_addrlen;
299 memcpy(sap, gai_results->ai_addr, *salen);
304 freeaddrinfo(gai_results);
311 * nfs_present_sockaddr - convert sockaddr to string
312 * @sap: pointer to socket address to convert
313 * @salen: length of socket address
314 * @buf: pointer to buffer to fill in
315 * @buflen: length of buffer
317 * Convert the passed-in sockaddr-style address to presentation format.
318 * The presentation format address is placed in @buf and is
321 * Returns 1 if successful; otherwise zero.
323 * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
324 * on presenting IPv6 addresses as text strings.
326 int nfs_present_sockaddr(const struct sockaddr *sap, const socklen_t salen,
327 char *buf, const size_t buflen)
329 #ifdef HAVE_GETNAMEINFO
332 result = getnameinfo(sap, salen, buf, buflen,
333 NULL, 0, NI_NUMERICHOST);
337 nfs_error(_("%s: invalid server address: %s"), progname,
338 gai_strerror(result));
340 #else /* HAVE_GETNAMEINFO */
343 if (sap->sa_family == AF_INET) {
344 addr = inet_ntoa(((struct sockaddr_in *)sap)->sin_addr);
345 if (addr && strlen(addr) < buflen) {
351 nfs_error(_("%s: invalid server address"), progname);
353 #endif /* HAVE_GETNAMEINFO */
357 * Attempt to connect a socket, but time out after "timeout" seconds.
359 * On error return, caller closes the socket.
361 static int connect_to(int fd, struct sockaddr *addr,
362 socklen_t addrlen, int timeout)
366 struct timeval tv = {
370 saved = fcntl(fd, F_GETFL, 0);
371 fcntl(fd, F_SETFL, saved | O_NONBLOCK);
373 ret = connect(fd, addr, addrlen);
374 if (ret < 0 && errno != EINPROGRESS)
382 ret = select(fd + 1, &rset, &wset, NULL, &tv);
387 if (FD_ISSET(fd, &rset) || FD_ISSET(fd, &wset)) {
389 socklen_t len = sizeof(error);
390 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
400 fcntl(fd, F_SETFL, saved);
405 * Create a socket that is locally bound to a reserved or non-reserved port.
407 * The caller should check rpc_createerr to determine the cause of any error.
409 static int get_socket(struct sockaddr_in *saddr, unsigned int p_prot,
410 unsigned int timeout, int resvp, int conn)
413 struct sockaddr_in laddr;
414 socklen_t namelen = sizeof(laddr);
416 type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
417 if ((so = socket (AF_INET, type, p_prot)) < 0)
420 laddr.sin_family = AF_INET;
422 laddr.sin_addr.s_addr = htonl(INADDR_ANY);
424 if (bindresvport(so, &laddr) < 0)
425 goto err_bindresvport;
427 cc = bind(so, SAFE_SOCKADDR(&laddr), namelen);
431 if (type == SOCK_STREAM || (conn && type == SOCK_DGRAM)) {
432 cc = connect_to(so, SAFE_SOCKADDR(saddr), namelen,
440 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
441 rpc_createerr.cf_error.re_errno = errno;
443 nfs_error(_("%s: Unable to create %s socket: errno %d (%s)\n"),
444 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
445 errno, strerror(errno));
450 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
451 rpc_createerr.cf_error.re_errno = errno;
453 nfs_error(_("%s: Unable to bindresvport %s socket: errno %d"
455 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
456 errno, strerror(errno));
462 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
463 rpc_createerr.cf_error.re_errno = errno;
465 nfs_error(_("%s: Unable to bind to %s socket: errno %d (%s)\n"),
466 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
467 errno, strerror(errno));
473 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
474 rpc_createerr.cf_error.re_errno = errno;
476 nfs_error(_("%s: Unable to connect to %s:%d, errno %d (%s)\n"),
477 progname, inet_ntoa(saddr->sin_addr),
478 ntohs(saddr->sin_port), errno, strerror(errno));
484 static void nfs_pp_debug(const struct sockaddr *sap, const socklen_t salen,
485 const rpcprog_t program, const rpcvers_t version,
486 const unsigned short protocol,
487 const unsigned short port)
489 char buf[NI_MAXHOST];
494 if (nfs_present_sockaddr(sap, salen, buf, sizeof(buf)) == 0) {
496 strcat(buf, "unknown host");
499 fprintf(stderr, _("%s: trying %s prog %lu vers %lu prot %s port %d\n"),
500 progname, buf, (unsigned long)program,
501 (unsigned long)version,
502 (protocol == IPPROTO_UDP ? _("UDP") : _("TCP")),
506 static void nfs_pp_debug2(const char *str)
511 if (rpc_createerr.cf_error.re_status == RPC_CANTRECV ||
512 rpc_createerr.cf_error.re_status == RPC_CANTSEND)
513 nfs_error(_("%s: portmap query %s%s - %s"),
514 progname, str, clnt_spcreateerror(""),
515 strerror(rpc_createerr.cf_error.re_errno));
517 nfs_error(_("%s: portmap query %s%s"),
518 progname, str, clnt_spcreateerror(""));
522 * Use the portmapper to discover whether or not the service we want is
523 * available. The lists 'versions' and 'protos' define ordered sequences
524 * of service versions and udp/tcp protocols to probe for.
526 * Returns 1 if the requested service port is unambiguous and pingable;
527 * @pmap is filled in with the version, port, and transport protocol used
528 * during the successful ping. Note that if a port is already specified
529 * in @pmap and it matches the rpcbind query result, nfs_probe_port() does
530 * not perform an RPC ping.
532 * If an error occurs or the requested service isn't available, zero is
533 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
535 static int nfs_probe_port(const struct sockaddr *sap, const socklen_t salen,
536 struct pmap *pmap, const unsigned long *versions,
537 const unsigned int *protos)
539 union nfs_sockaddr address;
540 struct sockaddr *saddr = &address.sa;
541 const unsigned long prog = pmap->pm_prog, *p_vers;
542 const unsigned int prot = (u_int)pmap->pm_prot, *p_prot;
543 const u_short port = (u_short) pmap->pm_port;
544 unsigned long vers = pmap->pm_vers;
545 unsigned short p_port;
547 memcpy(saddr, sap, salen);
548 p_prot = prot ? &prot : protos;
549 p_vers = vers ? &vers : versions;
553 printf(_("%s: prog %lu, trying vers=%lu, prot=%u\n"),
554 progname, prog, *p_vers, *p_prot);
555 p_port = nfs_getport(saddr, salen, prog, *p_vers, *p_prot);
557 if (!port || port == p_port) {
558 nfs_set_port(saddr, p_port);
559 nfs_pp_debug(saddr, salen, prog, *p_vers,
561 if (nfs_rpc_ping(saddr, salen, prog,
562 *p_vers, *p_prot, NULL))
565 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
567 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
568 rpc_createerr.cf_stat != RPC_TIMEDOUT &&
569 rpc_createerr.cf_stat != RPC_CANTRECV &&
570 rpc_createerr.cf_stat != RPC_PROGVERSMISMATCH)
575 nfs_pp_debug2("retrying");
580 if (rpc_createerr.cf_stat == RPC_TIMEDOUT ||
581 rpc_createerr.cf_stat == RPC_CANTRECV)
584 if (vers || !*++p_vers)
588 nfs_pp_debug2("failed");
593 pmap->pm_vers = *p_vers;
595 pmap->pm_prot = *p_prot;
597 pmap->pm_port = p_port;
598 nfs_clear_rpc_createerr();
602 * Probe a server's NFS service to determine which versions and
603 * transport protocols are supported.
605 * Returns 1 if the requested service port is unambiguous and pingable;
606 * @pmap is filled in with the version, port, and transport protocol used
607 * during the successful ping. If all three are already specified, simply
608 * return success without an rpcbind query or RPC ping (we may be trying
609 * to mount an NFS service that is not advertised via rpcbind).
611 * If an error occurs or the requested service isn't available, zero is
612 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
614 static int nfs_probe_nfsport(const struct sockaddr *sap, const socklen_t salen,
617 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
620 if (nfs_mount_data_version >= 4) {
621 const unsigned int *probe_proto;
623 probe_proto = nfs_default_proto();
625 return nfs_probe_port(sap, salen, pmap,
626 probe_nfs3_only, probe_proto);
628 return nfs_probe_port(sap, salen, pmap,
629 probe_nfs2_only, probe_udp_only);
633 * Probe a server's mountd service to determine which versions and
634 * transport protocols are supported.
636 * Returns 1 if the requested service port is unambiguous and pingable;
637 * @pmap is filled in with the version, port, and transport protocol used
638 * during the successful ping. If all three are already specified, simply
639 * return success without an rpcbind query or RPC ping (we may be trying
640 * to mount an NFS service that is not advertised via rpcbind).
642 * If an error occurs or the requested service isn't available, zero is
643 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
645 static int nfs_probe_mntport(const struct sockaddr *sap, const socklen_t salen,
648 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
651 if (nfs_mount_data_version >= 4)
652 return nfs_probe_port(sap, salen, pmap,
653 probe_mnt3_only, probe_udp_first);
655 return nfs_probe_port(sap, salen, pmap,
656 probe_mnt1_first, probe_udp_only);
660 * Probe a server's mountd service to determine which versions and
661 * transport protocols are supported. Invoked when the protocol
662 * version is already known for both the NFS and mountd service.
664 * Returns 1 and fills in both @pmap structs if the requested service
665 * ports are unambiguous and pingable. Otherwise zero is returned;
666 * rpccreateerr.cf_stat is set to reflect the nature of the error.
668 static int nfs_probe_version_fixed(const struct sockaddr *mnt_saddr,
669 const socklen_t mnt_salen,
670 struct pmap *mnt_pmap,
671 const struct sockaddr *nfs_saddr,
672 const socklen_t nfs_salen,
673 struct pmap *nfs_pmap)
675 if (!nfs_probe_nfsport(nfs_saddr, nfs_salen, nfs_pmap))
677 return nfs_probe_mntport(mnt_saddr, mnt_salen, mnt_pmap);
681 * nfs_probe_bothports - discover the RPC endpoints of mountd and NFS server
682 * @mnt_saddr: pointer to socket address of mountd server
683 * @mnt_salen: length of mountd server's address
684 * @mnt_pmap: IN: partially filled-in mountd RPC service tuple;
685 * OUT: fully filled-in mountd RPC service tuple
686 * @nfs_saddr: pointer to socket address of NFS server
687 * @nfs_salen: length of NFS server's address
688 * @nfs_pmap: IN: partially filled-in NFS RPC service tuple;
689 * OUT: fully filled-in NFS RPC service tuple
691 * Returns 1 and fills in both @pmap structs if the requested service
692 * ports are unambiguous and pingable. Otherwise zero is returned;
693 * rpccreateerr.cf_stat is set to reflect the nature of the error.
695 int nfs_probe_bothports(const struct sockaddr *mnt_saddr,
696 const socklen_t mnt_salen,
697 struct pmap *mnt_pmap,
698 const struct sockaddr *nfs_saddr,
699 const socklen_t nfs_salen,
700 struct pmap *nfs_pmap)
702 struct pmap save_nfs, save_mnt;
703 const unsigned long *probe_vers;
705 if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
706 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
707 else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
708 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
710 if (nfs_pmap->pm_vers)
711 return nfs_probe_version_fixed(mnt_saddr, mnt_salen, mnt_pmap,
712 nfs_saddr, nfs_salen, nfs_pmap);
714 memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
715 memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
716 probe_vers = (nfs_mount_data_version >= 4) ?
717 probe_mnt3_only : probe_mnt1_first;
719 for (; *probe_vers; probe_vers++) {
720 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
721 if (nfs_probe_nfsport(nfs_saddr, nfs_salen, nfs_pmap) != 0) {
722 mnt_pmap->pm_vers = *probe_vers;
723 if (nfs_probe_mntport(mnt_saddr, mnt_salen, mnt_pmap) != 0)
725 memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
727 switch (rpc_createerr.cf_stat) {
728 case RPC_PROGVERSMISMATCH:
729 case RPC_PROGNOTREGISTERED:
734 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
741 * probe_bothports - discover the RPC endpoints of mountd and NFS server
742 * @mnt_server: pointer to address and pmap argument for mountd results
743 * @nfs_server: pointer to address and pmap argument for NFS server
745 * This is the legacy API that takes "clnt_addr_t" for both servers,
746 * but supports only AF_INET addresses.
748 * Returns 1 and fills in the pmap field in both clnt_addr_t structs
749 * if the requested service ports are unambiguous and pingable.
750 * Otherwise zero is returned; rpccreateerr.cf_stat is set to reflect
751 * the nature of the error.
753 int probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
755 struct sockaddr *mnt_addr = SAFE_SOCKADDR(&mnt_server->saddr);
756 struct sockaddr *nfs_addr = SAFE_SOCKADDR(&nfs_server->saddr);
758 return nfs_probe_bothports(mnt_addr, sizeof(mnt_server->saddr),
760 nfs_addr, sizeof(nfs_server->saddr),
764 static int nfs_probe_statd(void)
766 struct sockaddr_in addr = {
767 .sin_family = AF_INET,
768 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
770 rpcprog_t program = nfs_getrpcbyname(NSMPROG, nfs_ns_pgmtbl);
772 return nfs_getport_ping(SAFE_SOCKADDR(&addr), sizeof(addr),
773 program, (rpcvers_t)1, IPPROTO_UDP);
777 * start_statd - attempt to start rpc.statd
779 * Returns 1 if statd is running; otherwise zero.
781 int start_statd(void)
787 if (nfs_probe_statd())
791 if (stat(START_STATD, &stb) == 0) {
792 if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {
796 execl(START_STATD, START_STATD, NULL);
799 nfs_error(_("%s: fork failed: %s"),
800 progname, strerror(errno));
802 default: /* parent */
803 waitpid(pid, NULL,0);
806 if (nfs_probe_statd())
816 * nfs_advise_umount - ask the server to remove a share from it's rmtab
817 * @sap: pointer to IP address of server to call
818 * @salen: length of server address
819 * @pmap: partially filled-in mountd RPC service tuple
820 * @argp: directory path of share to "unmount"
822 * Returns one if the unmount call succeeded; zero if the unmount
823 * failed for any reason; rpccreateerr.cf_stat is set to reflect
824 * the nature of the error.
826 * We use a fast timeout since this call is advisory only.
828 int nfs_advise_umount(const struct sockaddr *sap, const socklen_t salen,
829 const struct pmap *pmap, const dirpath *argp)
831 union nfs_sockaddr address;
832 struct sockaddr *saddr = &address.sa;
833 struct pmap mnt_pmap = *pmap;
834 struct timeval timeout = {
835 .tv_sec = MOUNT_TIMEOUT >> 3,
838 enum clnt_stat res = 0;
840 memcpy(saddr, sap, salen);
841 if (nfs_probe_mntport(saddr, salen, &mnt_pmap) == 0) {
843 nfs_error(_("%s: Failed to discover mountd port%s"),
844 progname, clnt_spcreateerror(""));
847 nfs_set_port(saddr, mnt_pmap.pm_port);
849 client = nfs_get_priv_rpcclient(saddr, salen, mnt_pmap.pm_prot,
850 mnt_pmap.pm_prog, mnt_pmap.pm_vers,
852 if (client == NULL) {
854 nfs_error(_("%s: Failed to create RPC client%s"),
855 progname, clnt_spcreateerror(""));
859 client->cl_auth = nfs_authsys_create();
860 if (client->cl_auth == NULL) {
862 nfs_error(_("%s: Failed to create RPC auth handle"),
864 CLNT_DESTROY(client);
868 res = CLNT_CALL(client, MOUNTPROC_UMNT,
869 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
870 (xdrproc_t)xdr_void, NULL,
872 if (res != RPC_SUCCESS) {
873 rpc_createerr.cf_stat = res;
874 CLNT_GETERR(client, &rpc_createerr.cf_error);
876 nfs_error(_("%s: UMNT call failed: %s"),
877 progname, clnt_sperrno(res));
880 auth_destroy(client->cl_auth);
881 CLNT_DESTROY(client);
883 if (res != RPC_SUCCESS)
889 * nfs_call_umount - ask the server to remove a share from it's rmtab
890 * @mnt_server: address of RPC MNT program server
891 * @argp: directory path of share to "unmount"
893 * Returns one if the unmount call succeeded; zero if the unmount
894 * failed for any reason.
896 * Note that a side effect of calling this function is that rpccreateerr
899 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
901 struct sockaddr *sap = SAFE_SOCKADDR(&mnt_server->saddr);
902 socklen_t salen = sizeof(mnt_server->saddr);
903 struct pmap *pmap = &mnt_server->pmap;
905 enum clnt_stat res = 0;
908 if (!nfs_probe_mntport(sap, salen, pmap))
910 clnt = mnt_openclnt(mnt_server, &msock);
913 res = clnt_call(clnt, MOUNTPROC_UMNT,
914 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
915 (xdrproc_t)xdr_void, NULL,
917 mnt_closeclnt(clnt, msock);
919 if (res == RPC_SUCCESS)
925 * mnt_openclnt - get a handle for a remote mountd service
926 * @mnt_server: address and pmap arguments of mountd service
927 * @msock: returns a file descriptor of the underlying transport socket
929 * Returns an active handle for the remote's mountd service
931 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
933 struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
934 struct pmap *mnt_pmap = &mnt_server->pmap;
937 mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
938 *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, MOUNT_TIMEOUT,
940 if (*msock == RPC_ANYSOCK) {
941 if (rpc_createerr.cf_error.re_errno == EADDRINUSE)
943 * Probably in-use by a TIME_WAIT connection,
944 * It is worth waiting a while and trying again.
946 rpc_createerr.cf_stat = RPC_TIMEDOUT;
950 switch (mnt_pmap->pm_prot) {
952 clnt = clntudp_bufcreate(mnt_saddr,
953 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
954 RETRY_TIMEOUT, msock,
955 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
958 clnt = clnttcp_create(mnt_saddr,
959 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
961 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
965 /* try to mount hostname:dirname */
966 clnt->cl_auth = nfs_authsys_create();
975 * mnt_closeclnt - terminate a handle for a remote mountd service
976 * @clnt: pointer to an active handle for a remote mountd service
977 * @msock: file descriptor of the underlying transport socket
980 void mnt_closeclnt(CLIENT *clnt, int msock)
982 auth_destroy(clnt->cl_auth);
988 * clnt_ping - send an RPC ping to the remote RPC service endpoint
989 * @saddr: server's address
990 * @prog: target RPC program number
991 * @vers: target RPC version number
992 * @prot: target RPC protocol
993 * @caddr: filled in with our network address
995 * Sigh... GETPORT queries don't actually check the version number.
996 * In order to make sure that the server actually supports the service
997 * we're requesting, we open an RPC client, and fire off a NULL
1000 * caddr is the network address that the server will use to call us back.
1001 * On multi-homed clients, this address depends on which NIC we use to
1002 * route requests to the server.
1004 * Returns one if successful, otherwise zero.
1006 int clnt_ping(struct sockaddr_in *saddr, const unsigned long prog,
1007 const unsigned long vers, const unsigned int prot,
1008 struct sockaddr_in *caddr)
1010 CLIENT *clnt = NULL;
1012 static char clnt_res;
1013 struct sockaddr dissolve;
1015 rpc_createerr.cf_stat = status = 0;
1016 sock = get_socket(saddr, prot, CONNECT_TIMEOUT, FALSE, TRUE);
1017 if (sock == RPC_ANYSOCK) {
1018 if (rpc_createerr.cf_error.re_errno == ETIMEDOUT) {
1020 * TCP timeout. Bubble up the error to see
1021 * how it should be handled.
1023 rpc_createerr.cf_stat = RPC_TIMEDOUT;
1029 /* Get the address of our end of this connection */
1030 socklen_t len = sizeof(*caddr);
1031 if (getsockname(sock, caddr, &len) != 0)
1032 caddr->sin_family = 0;
1037 /* The socket is connected (so we could getsockname successfully),
1038 * but some servers on multi-homed hosts reply from
1039 * the wrong address, so if we stay connected, we lose the reply.
1041 dissolve.sa_family = AF_UNSPEC;
1042 connect(sock, &dissolve, sizeof(dissolve));
1044 clnt = clntudp_bufcreate(saddr, prog, vers,
1045 RETRY_TIMEOUT, &sock,
1046 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
1049 clnt = clnttcp_create(saddr, prog, vers, &sock,
1050 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
1057 memset(&clnt_res, 0, sizeof(clnt_res));
1058 status = clnt_call(clnt, NULLPROC,
1059 (xdrproc_t)xdr_void, (caddr_t)NULL,
1060 (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
1063 clnt_geterr(clnt, &rpc_createerr.cf_error);
1064 rpc_createerr.cf_stat = status;
1069 if (status == RPC_SUCCESS)
1076 * Try a getsockname() on a connected datagram socket.
1078 * Returns 1 and fills in @buf if successful; otherwise, zero.
1080 * A connected datagram socket prevents leaving a socket in TIME_WAIT.
1081 * This conserves the ephemeral port number space, helping reduce failed
1082 * socket binds during mount storms.
1084 static int nfs_ca_sockname(const struct sockaddr *sap, const socklen_t salen,
1085 struct sockaddr *buf, socklen_t *buflen)
1087 struct sockaddr_in sin = {
1088 .sin_family = AF_INET,
1089 .sin_addr.s_addr = htonl(INADDR_ANY),
1091 struct sockaddr_in6 sin6 = {
1092 .sin6_family = AF_INET6,
1093 .sin6_addr = IN6ADDR_ANY_INIT,
1095 int sock, result = 0;
1097 sock = socket(sap->sa_family, SOCK_DGRAM, IPPROTO_UDP);
1101 switch (sap->sa_family) {
1103 if (bind(sock, SAFE_SOCKADDR(&sin), sizeof(sin)) < 0)
1107 if (bind(sock, SAFE_SOCKADDR(&sin6), sizeof(sin6)) < 0)
1111 errno = EAFNOSUPPORT;
1115 if (connect(sock, sap, salen) < 0)
1118 result = !getsockname(sock, buf, buflen);
1126 * Try to generate an address that prevents the server from calling us.
1128 * Returns 1 and fills in @buf if successful; otherwise, zero.
1130 static int nfs_ca_gai(const struct sockaddr *sap,
1131 struct sockaddr *buf, socklen_t *buflen)
1133 struct addrinfo *gai_results;
1134 struct addrinfo gai_hint = {
1135 .ai_family = sap->sa_family,
1136 .ai_flags = AI_PASSIVE, /* ANYADDR */
1139 if (getaddrinfo(NULL, "", &gai_hint, &gai_results))
1142 *buflen = gai_results->ai_addrlen;
1143 memcpy(buf, gai_results->ai_addr, *buflen);
1145 freeaddrinfo(gai_results);
1151 * nfs_callback_address - acquire our local network address
1152 * @sap: pointer to address of remote
1153 * @sap_len: length of address
1154 * @buf: pointer to buffer to be filled in with local network address
1155 * @buflen: IN: length of buffer to fill in; OUT: length of filled-in address
1157 * Discover a network address that an NFSv4 server can use to call us back.
1158 * On multi-homed clients, this address depends on which NIC we use to
1159 * route requests to the server.
1161 * Returns 1 and fills in @buf if an unambiguous local address is
1162 * available; returns 1 and fills in an appropriate ANYADDR address
1163 * if a local address isn't available; otherwise, returns zero.
1165 int nfs_callback_address(const struct sockaddr *sap, const socklen_t salen,
1166 struct sockaddr *buf, socklen_t *buflen)
1168 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1170 if (nfs_ca_sockname(sap, salen, buf, buflen) == 0)
1171 if (nfs_ca_gai(sap, buf, buflen) == 0)
1175 * The server can't use an interface ID that was generated
1176 * here on the client, so always clear sin6_scope_id.
1178 if (sin6->sin6_family == AF_INET6)
1179 sin6->sin6_scope_id = 0;
1186 nfs_error(_("%s: failed to construct callback address"),
1192 * "nfsprog" is supported only by the legacy mount command. The
1193 * kernel mount client does not support this option.
1195 * Returns TRUE if @program contains a valid value for this option,
1196 * or FALSE if the option was specified with an invalid value.
1199 nfs_nfs_program(struct mount_options *options, unsigned long *program)
1203 switch (po_get_numeric(options, "nfsprog", &tmp)) {
1212 nfs_error(_("%s: invalid value for 'nfsprog=' option"),
1218 * NFS RPC program wasn't specified. The RPC program
1219 * cannot be determined via an rpcbind query.
1221 *program = nfs_getrpcbyname(NFSPROG, nfs_nfs_pgmtbl);
1226 * Returns TRUE if @version contains a valid value for this option,
1227 * or FALSE if the option was specified with an invalid value.
1230 nfs_nfs_version(struct mount_options *options, unsigned long *version)
1234 switch (po_rightmost(options, nfs_version_opttbl)) {
1245 switch (po_get_numeric(options, "vers", &tmp)) {
1247 if (tmp >= 2 && tmp <= 4) {
1253 nfs_error(_("%s: parsing error on 'vers=' option\n"),
1257 nfs_error(_("%s: invalid value for 'vers=' option"),
1261 case 4: /* nfsvers */
1262 switch (po_get_numeric(options, "nfsvers", &tmp)) {
1264 if (tmp >= 2 && tmp <= 4) {
1270 nfs_error(_("%s: parsing error on 'nfsvers=' option\n"),
1274 nfs_error(_("%s: invalid value for 'nfsvers=' option"),
1281 * NFS version wasn't specified. The pmap version value
1282 * will be filled in later by an rpcbind query in this case.
1289 * Returns TRUE if @protocol contains a valid value for this option,
1290 * or FALSE if the option was specified with an invalid value. On
1291 * error, errno is set.
1294 nfs_nfs_protocol(struct mount_options *options, unsigned long *protocol)
1299 switch (po_rightmost(options, nfs_transport_opttbl)) {
1301 *protocol = IPPROTO_UDP;
1304 *protocol = IPPROTO_TCP;
1307 *protocol = NFSPROTO_RDMA;
1310 option = po_get(options, "proto");
1311 if (option != NULL) {
1312 if (!nfs_get_proto(option, &family, protocol)) {
1313 errno = EPROTONOSUPPORT;
1314 nfs_error(_("%s: Failed to find '%s' protocol"),
1323 * NFS transport protocol wasn't specified. The pmap
1324 * protocol value will be filled in later by an rpcbind
1325 * query in this case.
1332 * Returns TRUE if @port contains a valid value for this option,
1333 * or FALSE if the option was specified with an invalid value.
1336 nfs_nfs_port(struct mount_options *options, unsigned long *port)
1340 switch (po_get_numeric(options, "port", &tmp)) {
1344 if (tmp >= 0 && tmp <= 65535) {
1349 nfs_error(_("%s: invalid value for 'port=' option"),
1355 * NFS service port wasn't specified. The pmap port value
1356 * will be filled in later by an rpcbind query in this case.
1362 #ifdef IPV6_SUPPORTED
1363 sa_family_t config_default_family = AF_UNSPEC;
1366 nfs_verify_family(sa_family_t UNUSED(family))
1370 #else /* IPV6_SUPPORTED */
1371 sa_family_t config_default_family = AF_INET;
1374 nfs_verify_family(sa_family_t family)
1376 if (family != AF_INET)
1381 #endif /* IPV6_SUPPORTED */
1384 * Returns TRUE and fills in @family if a valid NFS protocol option
1385 * is found, or FALSE if the option was specified with an invalid value
1386 * or if the protocol family isn't supported. On error, errno is set.
1388 int nfs_nfs_proto_family(struct mount_options *options,
1389 sa_family_t *family)
1391 unsigned long protocol;
1393 sa_family_t tmp_family = config_default_family;
1395 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 if (!nfs_nfs_protocol(options, protocol))
1518 if (*protocol == NFSPROTO_RDMA)
1519 *protocol = IPPROTO_TCP;
1524 * Returns TRUE if @port contains a valid value for this option,
1525 * or FALSE if the option was specified with an invalid value.
1528 nfs_mount_port(struct mount_options *options, unsigned long *port)
1532 switch (po_get_numeric(options, "mountport", &tmp)) {
1536 if (tmp >= 0 && tmp <= 65535) {
1541 nfs_error(_("%s: invalid value for 'mountport=' option"),
1547 * MNT service port wasn't specified. The pmap port value
1548 * will be filled in later by an rpcbind query in this case.
1555 * Returns TRUE and fills in @family if a valid MNT protocol option
1556 * is found, or FALSE if the option was specified with an invalid value
1557 * or if the protocol family isn't supported. On error, errno is set.
1559 int nfs_mount_proto_family(struct mount_options *options,
1560 sa_family_t *family)
1562 unsigned long protocol;
1564 sa_family_t tmp_family = config_default_family;
1566 option = po_get(options, "mountproto");
1567 if (option != NULL) {
1568 if (!nfs_get_proto(option, &tmp_family, &protocol)) {
1569 nfs_error(_("%s: Failed to find '%s' protocol"),
1571 errno = EPROTONOSUPPORT;
1574 if (!nfs_verify_family(tmp_family))
1576 *family = tmp_family;
1581 * MNT transport protocol wasn't specified. If the NFS
1582 * transport protocol was specified, derive the family
1583 * from that; otherwise, return the default family for
1586 return nfs_nfs_proto_family(options, family);
1588 errno = EAFNOSUPPORT;
1593 * nfs_options2pmap - set up pmap structs based on mount options
1594 * @options: pointer to mount options
1595 * @nfs_pmap: OUT: pointer to pmap arguments for NFS server
1596 * @mnt_pmap: OUT: pointer to pmap arguments for mountd server
1598 * Returns TRUE if the pmap options specified in @options have valid
1599 * values; otherwise FALSE is returned.
1601 int nfs_options2pmap(struct mount_options *options,
1602 struct pmap *nfs_pmap, struct pmap *mnt_pmap)
1604 if (!nfs_nfs_program(options, &nfs_pmap->pm_prog))
1606 if (!nfs_nfs_version(options, &nfs_pmap->pm_vers))
1608 if (!nfs_nfs_protocol(options, &nfs_pmap->pm_prot))
1610 if (!nfs_nfs_port(options, &nfs_pmap->pm_port))
1613 if (!nfs_mount_program(options, &mnt_pmap->pm_prog))
1615 if (!nfs_mount_version(options, &mnt_pmap->pm_vers))
1617 if (!nfs_mount_protocol(options, &mnt_pmap->pm_prot))
1619 if (!nfs_mount_port(options, &mnt_pmap->pm_port))
1626 * Discover mount server's hostname/address by examining mount options
1628 * Returns a pointer to a string that the caller must free, on
1629 * success; otherwise NULL is returned.
1631 static char *nfs_umount_hostname(struct mount_options *options,
1636 option = po_get(options, "mountaddr");
1639 option = po_get(options, "mounthost");
1642 option = po_get(options, "addr");
1650 return strdup(option);
1655 * Returns EX_SUCCESS if mount options and device name have been
1656 * parsed successfully; otherwise EX_FAIL.
1658 int nfs_umount_do_umnt(struct mount_options *options,
1659 char **hostname, char **dirname)
1661 union nfs_sockaddr address;
1662 struct sockaddr *sap = &address.sa;
1663 socklen_t salen = sizeof(address);
1664 struct pmap nfs_pmap, mnt_pmap;
1667 if (!nfs_options2pmap(options, &nfs_pmap, &mnt_pmap))
1670 /* Skip UMNT call for vers=4 mounts */
1671 if (nfs_pmap.pm_vers == 4)
1674 *hostname = nfs_umount_hostname(options, *hostname);
1676 nfs_error(_("%s: out of memory"), progname);
1680 if (!nfs_mount_proto_family(options, &family))
1682 if (!nfs_lookup(*hostname, family, sap, &salen))
1683 /* nfs_lookup reports any errors */
1686 if (nfs_advise_umount(sap, salen, &mnt_pmap, dirname) == 0)
1687 /* nfs_advise_umount reports any errors */