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"
57 #define PMAP_TIMEOUT (10)
58 #define CONNECT_TIMEOUT (20)
59 #define MOUNT_TIMEOUT (30)
61 extern int nfs_mount_data_version;
62 extern char *progname;
65 static const char *nfs_ns_pgmtbl[] = {
70 static const char *nfs_mnt_pgmtbl[] = {
76 static const char *nfs_nfs_pgmtbl[] = {
82 static const char *nfs_transport_opttbl[] = {
89 static const char *nfs_version_opttbl[] = {
98 static const unsigned long nfs_to_mnt[] = {
105 static const unsigned long mnt_to_nfs[] = {
113 * Map an NFS version into the corresponding Mountd version
115 unsigned long nfsvers_to_mnt(const unsigned long vers)
118 return nfs_to_mnt[vers];
123 * Map a Mountd version into the corresponding NFS version
125 static unsigned long mntvers_to_nfs(const unsigned long vers)
128 return mnt_to_nfs[vers];
132 static const unsigned int probe_udp_only[] = {
137 static const unsigned int probe_udp_first[] = {
143 static const unsigned int probe_tcp_first[] = {
149 static const unsigned long probe_nfs2_only[] = {
154 static const unsigned long probe_nfs3_first[] = {
160 static const unsigned long probe_mnt1_first[] = {
166 static const unsigned long probe_mnt3_first[] = {
173 static const unsigned int *nfs_default_proto(void);
175 static const unsigned int *nfs_default_proto()
177 extern unsigned long config_default_proto;
179 * If the default proto has been set and
180 * its not TCP, start with UDP
182 if (config_default_proto && config_default_proto != IPPROTO_TCP)
183 return probe_udp_first;
185 return probe_tcp_first;
188 static const unsigned int *nfs_default_proto()
190 return probe_tcp_first;
192 #endif /* MOUNT_CONFIG */
195 * nfs_lookup - resolve hostname to an IPv4 or IPv6 socket address
196 * @hostname: pointer to C string containing DNS hostname to resolve
197 * @family: address family hint
198 * @sap: pointer to buffer to fill with socket address
199 * @len: IN: size of buffer to fill; OUT: size of socket address
201 * Returns 1 and places a socket address at @sap if successful;
204 int nfs_lookup(const char *hostname, const sa_family_t family,
205 struct sockaddr *sap, socklen_t *salen)
207 struct addrinfo *gai_results;
208 struct addrinfo gai_hint = {
209 #ifdef HAVE_DECL_AI_ADDRCONFIG
210 .ai_flags = AI_ADDRCONFIG,
211 #endif /* HAVE_DECL_AI_ADDRCONFIG */
214 socklen_t len = *salen;
219 error = getaddrinfo(hostname, NULL, &gai_hint, &gai_results);
224 nfs_error(_("%s: DNS resolution failed for %s: %s"),
225 progname, hostname, strerror(errno));
228 nfs_error(_("%s: DNS resolution failed for %s: %s"),
229 progname, hostname, gai_strerror(error));
233 switch (gai_results->ai_addr->sa_family) {
236 if (len >= gai_results->ai_addrlen) {
237 *salen = gai_results->ai_addrlen;
238 memcpy(sap, gai_results->ai_addr, *salen);
243 /* things are really broken if we get here, so warn */
244 nfs_error(_("%s: unrecognized DNS resolution results for %s"),
249 freeaddrinfo(gai_results);
254 * nfs_gethostbyname - resolve a hostname to an IPv4 address
255 * @hostname: pointer to a C string containing a DNS hostname
256 * @sin: returns an IPv4 address
258 * Returns 1 if successful, otherwise zero.
260 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *sin)
262 socklen_t len = sizeof(*sin);
264 return nfs_lookup(hostname, AF_INET, (struct sockaddr *)sin, &len);
268 * nfs_string_to_sockaddr - convert string address to sockaddr
269 * @address: pointer to presentation format address to convert
270 * @sap: pointer to socket address buffer to fill in
271 * @salen: IN: length of address buffer
272 * OUT: length of converted socket address
274 * Convert a presentation format address string to a socket address.
275 * Similar to nfs_lookup(), but the DNS query is squelched, and it
276 * won't make any noise if the getaddrinfo() call fails.
278 * Returns 1 and fills in @sap and @salen if successful; otherwise zero.
280 * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
281 * on presenting IPv6 addresses as text strings.
283 int nfs_string_to_sockaddr(const char *address, struct sockaddr *sap,
286 struct addrinfo *gai_results;
287 struct addrinfo gai_hint = {
288 .ai_flags = AI_NUMERICHOST,
290 socklen_t len = *salen;
295 if (getaddrinfo(address, NULL, &gai_hint, &gai_results) == 0) {
296 switch (gai_results->ai_addr->sa_family) {
299 if (len >= gai_results->ai_addrlen) {
300 *salen = gai_results->ai_addrlen;
301 memcpy(sap, gai_results->ai_addr, *salen);
306 freeaddrinfo(gai_results);
313 * nfs_present_sockaddr - convert sockaddr to string
314 * @sap: pointer to socket address to convert
315 * @salen: length of socket address
316 * @buf: pointer to buffer to fill in
317 * @buflen: length of buffer
319 * Convert the passed-in sockaddr-style address to presentation format.
320 * The presentation format address is placed in @buf and is
323 * Returns 1 if successful; otherwise zero.
325 * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
326 * on presenting IPv6 addresses as text strings.
328 int nfs_present_sockaddr(const struct sockaddr *sap, const socklen_t salen,
329 char *buf, const size_t buflen)
331 #ifdef HAVE_GETNAMEINFO
334 result = getnameinfo(sap, salen, buf, buflen,
335 NULL, 0, NI_NUMERICHOST);
339 nfs_error(_("%s: invalid server address: %s"), progname,
340 gai_strerror(result));
342 #else /* HAVE_GETNAMEINFO */
345 if (sap->sa_family == AF_INET) {
346 addr = inet_ntoa(((struct sockaddr_in *)sap)->sin_addr);
347 if (addr && strlen(addr) < buflen) {
353 nfs_error(_("%s: invalid server address"), progname);
355 #endif /* HAVE_GETNAMEINFO */
359 * Attempt to connect a socket, but time out after "timeout" seconds.
361 * On error return, caller closes the socket.
363 static int connect_to(int fd, struct sockaddr *addr,
364 socklen_t addrlen, int timeout)
368 struct timeval tv = {
372 saved = fcntl(fd, F_GETFL, 0);
373 fcntl(fd, F_SETFL, saved | O_NONBLOCK);
375 ret = connect(fd, addr, addrlen);
376 if (ret < 0 && errno != EINPROGRESS)
384 ret = select(fd + 1, &rset, &wset, NULL, &tv);
389 if (FD_ISSET(fd, &rset) || FD_ISSET(fd, &wset)) {
391 socklen_t len = sizeof(error);
392 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
402 fcntl(fd, F_SETFL, saved);
407 * Create a socket that is locally bound to a reserved or non-reserved port.
409 * The caller should check rpc_createerr to determine the cause of any error.
411 static int get_socket(struct sockaddr_in *saddr, unsigned int p_prot,
412 unsigned int timeout, int resvp, int conn)
415 struct sockaddr_in laddr;
416 socklen_t namelen = sizeof(laddr);
418 type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
419 if ((so = socket (AF_INET, type, p_prot)) < 0)
422 laddr.sin_family = AF_INET;
424 laddr.sin_addr.s_addr = htonl(INADDR_ANY);
426 if (bindresvport(so, &laddr) < 0)
427 goto err_bindresvport;
429 cc = bind(so, (struct sockaddr *)&laddr, namelen);
433 if (type == SOCK_STREAM || (conn && type == SOCK_DGRAM)) {
434 cc = connect_to(so, (struct sockaddr *)saddr, namelen,
442 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
443 rpc_createerr.cf_error.re_errno = errno;
445 nfs_error(_("%s: Unable to create %s socket: errno %d (%s)\n"),
446 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
447 errno, strerror(errno));
452 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
453 rpc_createerr.cf_error.re_errno = errno;
455 nfs_error(_("%s: Unable to bindresvport %s socket: errno %d"
457 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
458 errno, strerror(errno));
464 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
465 rpc_createerr.cf_error.re_errno = errno;
467 nfs_error(_("%s: Unable to bind to %s socket: errno %d (%s)\n"),
468 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
469 errno, strerror(errno));
475 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
476 rpc_createerr.cf_error.re_errno = errno;
478 nfs_error(_("%s: Unable to connect to %s:%d, errno %d (%s)\n"),
479 progname, inet_ntoa(saddr->sin_addr),
480 ntohs(saddr->sin_port), errno, strerror(errno));
486 static void nfs_pp_debug(const struct sockaddr *sap, const socklen_t salen,
487 const rpcprog_t program, const rpcvers_t version,
488 const unsigned short protocol,
489 const unsigned short port)
491 char buf[NI_MAXHOST];
496 if (nfs_present_sockaddr(sap, salen, buf, sizeof(buf)) == 0) {
498 strcat(buf, "unknown host");
501 fprintf(stderr, _("%s: trying %s prog %lu vers %lu prot %s port %d\n"),
502 progname, buf, (unsigned long)program,
503 (unsigned long)version,
504 (protocol == IPPROTO_UDP ? _("UDP") : _("TCP")),
508 static void nfs_pp_debug2(const char *str)
513 if (rpc_createerr.cf_error.re_status == RPC_CANTRECV ||
514 rpc_createerr.cf_error.re_status == RPC_CANTSEND)
515 nfs_error(_("%s: portmap query %s%s - %s"),
516 progname, str, clnt_spcreateerror(""),
517 strerror(rpc_createerr.cf_error.re_errno));
519 nfs_error(_("%s: portmap query %s%s"),
520 progname, str, clnt_spcreateerror(""));
524 * Use the portmapper to discover whether or not the service we want is
525 * available. The lists 'versions' and 'protos' define ordered sequences
526 * of service versions and udp/tcp protocols to probe for.
528 * Returns 1 if the requested service port is unambiguous and pingable;
529 * @pmap is filled in with the version, port, and transport protocol used
530 * during the successful ping. Note that if a port is already specified
531 * in @pmap and it matches the rpcbind query result, nfs_probe_port() does
532 * not perform an RPC ping.
534 * If an error occurs or the requested service isn't available, zero is
535 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
537 static int nfs_probe_port(const struct sockaddr *sap, const socklen_t salen,
538 struct pmap *pmap, const unsigned long *versions,
539 const unsigned int *protos)
541 union nfs_sockaddr address;
542 struct sockaddr *saddr = &address.sa;
543 const unsigned long prog = pmap->pm_prog, *p_vers;
544 const unsigned int prot = (u_int)pmap->pm_prot, *p_prot;
545 const u_short port = (u_short) pmap->pm_port;
546 unsigned long vers = pmap->pm_vers;
547 unsigned short p_port;
549 memcpy(saddr, sap, salen);
550 p_prot = prot ? &prot : protos;
551 p_vers = vers ? &vers : versions;
555 printf(_("%s: prog %lu, trying vers=%lu, prot=%u\n"),
556 progname, prog, *p_vers, *p_prot);
557 p_port = nfs_getport(saddr, salen, prog, *p_vers, *p_prot);
559 if (!port || port == p_port) {
560 nfs_set_port(saddr, p_port);
561 nfs_pp_debug(saddr, salen, prog, *p_vers,
563 if (nfs_rpc_ping(saddr, salen, prog,
564 *p_vers, *p_prot, NULL))
567 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
569 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
570 rpc_createerr.cf_stat != RPC_TIMEDOUT &&
571 rpc_createerr.cf_stat != RPC_CANTRECV &&
572 rpc_createerr.cf_stat != RPC_PROGVERSMISMATCH)
577 nfs_pp_debug2("retrying");
582 if (rpc_createerr.cf_stat == RPC_TIMEDOUT ||
583 rpc_createerr.cf_stat == RPC_CANTRECV)
586 if (vers || !*++p_vers)
590 nfs_pp_debug2("failed");
595 pmap->pm_vers = *p_vers;
597 pmap->pm_prot = *p_prot;
599 pmap->pm_port = p_port;
600 nfs_clear_rpc_createerr();
604 * Probe a server's NFS service to determine which versions and
605 * transport protocols are supported.
607 * Returns 1 if the requested service port is unambiguous and pingable;
608 * @pmap is filled in with the version, port, and transport protocol used
609 * during the successful ping. If all three are already specified, simply
610 * return success without an rpcbind query or RPC ping (we may be trying
611 * to mount an NFS service that is not advertised via rpcbind).
613 * If an error occurs or the requested service isn't available, zero is
614 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
616 static int nfs_probe_nfsport(const struct sockaddr *sap, const socklen_t salen,
619 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
622 if (nfs_mount_data_version >= 4) {
623 const unsigned int *probe_proto;
625 probe_proto = nfs_default_proto();
627 return nfs_probe_port(sap, salen, pmap,
628 probe_nfs3_first, probe_proto);
630 return nfs_probe_port(sap, salen, pmap,
631 probe_nfs2_only, probe_udp_only);
635 * Probe a server's mountd service to determine which versions and
636 * transport protocols are supported.
638 * Returns 1 if the requested service port is unambiguous and pingable;
639 * @pmap is filled in with the version, port, and transport protocol used
640 * during the successful ping. If all three are already specified, simply
641 * return success without an rpcbind query or RPC ping (we may be trying
642 * to mount an NFS service that is not advertised via rpcbind).
644 * If an error occurs or the requested service isn't available, zero is
645 * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
647 static int nfs_probe_mntport(const struct sockaddr *sap, const socklen_t salen,
650 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
653 if (nfs_mount_data_version >= 4)
654 return nfs_probe_port(sap, salen, pmap,
655 probe_mnt3_first, probe_udp_first);
657 return nfs_probe_port(sap, salen, pmap,
658 probe_mnt1_first, probe_udp_only);
662 * Probe a server's mountd service to determine which versions and
663 * transport protocols are supported. Invoked when the protocol
664 * version is already known for both the NFS and mountd service.
666 * Returns 1 and fills in both @pmap structs if the requested service
667 * ports are unambiguous and pingable. Otherwise zero is returned;
668 * rpccreateerr.cf_stat is set to reflect the nature of the error.
670 static int nfs_probe_version_fixed(const struct sockaddr *mnt_saddr,
671 const socklen_t mnt_salen,
672 struct pmap *mnt_pmap,
673 const struct sockaddr *nfs_saddr,
674 const socklen_t nfs_salen,
675 struct pmap *nfs_pmap)
677 if (!nfs_probe_nfsport(nfs_saddr, nfs_salen, nfs_pmap))
679 return nfs_probe_mntport(mnt_saddr, mnt_salen, mnt_pmap);
683 * nfs_probe_bothports - discover the RPC endpoints of mountd and NFS server
684 * @mnt_saddr: pointer to socket address of mountd server
685 * @mnt_salen: length of mountd server's address
686 * @mnt_pmap: IN: partially filled-in mountd RPC service tuple;
687 * OUT: fully filled-in mountd RPC service tuple
688 * @nfs_saddr: pointer to socket address of NFS server
689 * @nfs_salen: length of NFS server's address
690 * @nfs_pmap: IN: partially filled-in NFS RPC service tuple;
691 * OUT: fully filled-in NFS RPC service tuple
693 * Returns 1 and fills in both @pmap structs if the requested service
694 * ports are unambiguous and pingable. Otherwise zero is returned;
695 * rpccreateerr.cf_stat is set to reflect the nature of the error.
697 int nfs_probe_bothports(const struct sockaddr *mnt_saddr,
698 const socklen_t mnt_salen,
699 struct pmap *mnt_pmap,
700 const struct sockaddr *nfs_saddr,
701 const socklen_t nfs_salen,
702 struct pmap *nfs_pmap)
704 struct pmap save_nfs, save_mnt;
705 const unsigned long *probe_vers;
707 if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
708 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
709 else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
710 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
712 if (nfs_pmap->pm_vers)
713 return nfs_probe_version_fixed(mnt_saddr, mnt_salen, mnt_pmap,
714 nfs_saddr, nfs_salen, nfs_pmap);
716 memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
717 memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
718 probe_vers = (nfs_mount_data_version >= 4) ?
719 probe_mnt3_first : probe_mnt1_first;
721 for (; *probe_vers; probe_vers++) {
722 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
723 if (nfs_probe_nfsport(nfs_saddr, nfs_salen, nfs_pmap) != 0) {
724 mnt_pmap->pm_vers = *probe_vers;
725 if (nfs_probe_mntport(mnt_saddr, mnt_salen, mnt_pmap) != 0)
727 memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
729 switch (rpc_createerr.cf_stat) {
730 case RPC_PROGVERSMISMATCH:
731 case RPC_PROGNOTREGISTERED:
736 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
743 * probe_bothports - discover the RPC endpoints of mountd and NFS server
744 * @mnt_server: pointer to address and pmap argument for mountd results
745 * @nfs_server: pointer to address and pmap argument for NFS server
747 * This is the legacy API that takes "clnt_addr_t" for both servers,
748 * but supports only AF_INET addresses.
750 * Returns 1 and fills in the pmap field in both clnt_addr_t structs
751 * if the requested service ports are unambiguous and pingable.
752 * Otherwise zero is returned; rpccreateerr.cf_stat is set to reflect
753 * the nature of the error.
755 int probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
757 return nfs_probe_bothports((struct sockaddr *)&mnt_server->saddr,
758 sizeof(mnt_server->saddr),
760 (struct sockaddr *)&nfs_server->saddr,
761 sizeof(nfs_server->saddr),
765 static int nfs_probe_statd(void)
767 struct sockaddr_in addr = {
768 .sin_family = AF_INET,
769 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
771 rpcprog_t program = nfs_getrpcbyname(NSMPROG, nfs_ns_pgmtbl);
773 return nfs_getport_ping((struct sockaddr *)&addr, sizeof(addr),
774 program, (rpcvers_t)1, IPPROTO_UDP);
778 * start_statd - attempt to start rpc.statd
780 * Returns 1 if statd is running; otherwise zero.
782 int start_statd(void)
788 if (nfs_probe_statd())
792 if (stat(START_STATD, &stb) == 0) {
793 if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {
797 execl(START_STATD, START_STATD, NULL);
800 nfs_error(_("%s: fork failed: %s"),
801 progname, strerror(errno));
803 default: /* parent */
804 waitpid(pid, NULL,0);
807 if (nfs_probe_statd())
817 * nfs_advise_umount - ask the server to remove a share from it's rmtab
818 * @sap: pointer to IP address of server to call
819 * @salen: length of server address
820 * @pmap: partially filled-in mountd RPC service tuple
821 * @argp: directory path of share to "unmount"
823 * Returns one if the unmount call succeeded; zero if the unmount
824 * failed for any reason; rpccreateerr.cf_stat is set to reflect
825 * the nature of the error.
827 * We use a fast timeout since this call is advisory only.
829 int nfs_advise_umount(const struct sockaddr *sap, const socklen_t salen,
830 const struct pmap *pmap, const dirpath *argp)
832 union nfs_sockaddr address;
833 struct sockaddr *saddr = &address.sa;
834 struct pmap mnt_pmap = *pmap;
835 struct timeval timeout = {
836 .tv_sec = MOUNT_TIMEOUT >> 3,
839 enum clnt_stat res = 0;
841 memcpy(saddr, sap, salen);
842 if (nfs_probe_mntport(saddr, salen, &mnt_pmap) == 0) {
844 nfs_error(_("%s: Failed to discover mountd port%s"),
845 progname, clnt_spcreateerror(""));
848 nfs_set_port(saddr, mnt_pmap.pm_port);
850 client = nfs_get_priv_rpcclient(saddr, salen, mnt_pmap.pm_prot,
851 mnt_pmap.pm_prog, mnt_pmap.pm_vers,
853 if (client == NULL) {
855 nfs_error(_("%s: Failed to create RPC client%s"),
856 progname, clnt_spcreateerror(""));
860 client->cl_auth = authunix_create_default();
862 res = CLNT_CALL(client, MOUNTPROC_UMNT,
863 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
864 (xdrproc_t)xdr_void, NULL,
866 if (res != RPC_SUCCESS) {
867 rpc_createerr.cf_stat = res;
868 CLNT_GETERR(client, &rpc_createerr.cf_error);
870 nfs_error(_("%s: UMNT call failed: %s"),
871 progname, clnt_sperrno(res));
874 auth_destroy(client->cl_auth);
875 CLNT_DESTROY(client);
877 if (res != RPC_SUCCESS)
883 * nfs_call_umount - ask the server to remove a share from it's rmtab
884 * @mnt_server: address of RPC MNT program server
885 * @argp: directory path of share to "unmount"
887 * Returns one if the unmount call succeeded; zero if the unmount
888 * failed for any reason.
890 * Note that a side effect of calling this function is that rpccreateerr
893 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
895 struct sockaddr *sap = (struct sockaddr *)&mnt_server->saddr;
896 socklen_t salen = sizeof(mnt_server->saddr);
897 struct pmap *pmap = &mnt_server->pmap;
899 enum clnt_stat res = 0;
902 if (!nfs_probe_mntport(sap, salen, pmap))
904 clnt = mnt_openclnt(mnt_server, &msock);
907 res = clnt_call(clnt, MOUNTPROC_UMNT,
908 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
909 (xdrproc_t)xdr_void, NULL,
911 mnt_closeclnt(clnt, msock);
913 if (res == RPC_SUCCESS)
919 * mnt_openclnt - get a handle for a remote mountd service
920 * @mnt_server: address and pmap arguments of mountd service
921 * @msock: returns a file descriptor of the underlying transport socket
923 * Returns an active handle for the remote's mountd service
925 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
927 struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
928 struct pmap *mnt_pmap = &mnt_server->pmap;
931 mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
932 *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, MOUNT_TIMEOUT,
934 if (*msock == RPC_ANYSOCK) {
935 if (rpc_createerr.cf_error.re_errno == EADDRINUSE)
937 * Probably in-use by a TIME_WAIT connection,
938 * It is worth waiting a while and trying again.
940 rpc_createerr.cf_stat = RPC_TIMEDOUT;
944 switch (mnt_pmap->pm_prot) {
946 clnt = clntudp_bufcreate(mnt_saddr,
947 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
948 RETRY_TIMEOUT, msock,
949 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
952 clnt = clnttcp_create(mnt_saddr,
953 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
955 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
959 /* try to mount hostname:dirname */
960 clnt->cl_auth = authunix_create_default();
967 * mnt_closeclnt - terminate a handle for a remote mountd service
968 * @clnt: pointer to an active handle for a remote mountd service
969 * @msock: file descriptor of the underlying transport socket
972 void mnt_closeclnt(CLIENT *clnt, int msock)
974 auth_destroy(clnt->cl_auth);
980 * clnt_ping - send an RPC ping to the remote RPC service endpoint
981 * @saddr: server's address
982 * @prog: target RPC program number
983 * @vers: target RPC version number
984 * @prot: target RPC protocol
985 * @caddr: filled in with our network address
987 * Sigh... GETPORT queries don't actually check the version number.
988 * In order to make sure that the server actually supports the service
989 * we're requesting, we open an RPC client, and fire off a NULL
992 * caddr is the network address that the server will use to call us back.
993 * On multi-homed clients, this address depends on which NIC we use to
994 * route requests to the server.
996 * Returns one if successful, otherwise zero.
998 int clnt_ping(struct sockaddr_in *saddr, const unsigned long prog,
999 const unsigned long vers, const unsigned int prot,
1000 struct sockaddr_in *caddr)
1002 CLIENT *clnt = NULL;
1004 static char clnt_res;
1005 struct sockaddr dissolve;
1007 rpc_createerr.cf_stat = stat = 0;
1008 sock = get_socket(saddr, prot, CONNECT_TIMEOUT, FALSE, TRUE);
1009 if (sock == RPC_ANYSOCK) {
1010 if (rpc_createerr.cf_error.re_errno == ETIMEDOUT) {
1012 * TCP timeout. Bubble up the error to see
1013 * how it should be handled.
1015 rpc_createerr.cf_stat = RPC_TIMEDOUT;
1021 /* Get the address of our end of this connection */
1022 socklen_t len = sizeof(*caddr);
1023 if (getsockname(sock, caddr, &len) != 0)
1024 caddr->sin_family = 0;
1029 /* The socket is connected (so we could getsockname successfully),
1030 * but some servers on multi-homed hosts reply from
1031 * the wrong address, so if we stay connected, we lose the reply.
1033 dissolve.sa_family = AF_UNSPEC;
1034 connect(sock, &dissolve, sizeof(dissolve));
1036 clnt = clntudp_bufcreate(saddr, prog, vers,
1037 RETRY_TIMEOUT, &sock,
1038 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
1041 clnt = clnttcp_create(saddr, prog, vers, &sock,
1042 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
1049 memset(&clnt_res, 0, sizeof(clnt_res));
1050 stat = clnt_call(clnt, NULLPROC,
1051 (xdrproc_t)xdr_void, (caddr_t)NULL,
1052 (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
1055 clnt_geterr(clnt, &rpc_createerr.cf_error);
1056 rpc_createerr.cf_stat = stat;
1061 if (stat == RPC_SUCCESS)
1068 * Try a getsockname() on a connected datagram socket.
1070 * Returns 1 and fills in @buf if successful; otherwise, zero.
1072 * A connected datagram socket prevents leaving a socket in TIME_WAIT.
1073 * This conserves the ephemeral port number space, helping reduce failed
1074 * socket binds during mount storms.
1076 static int nfs_ca_sockname(const struct sockaddr *sap, const socklen_t salen,
1077 struct sockaddr *buf, socklen_t *buflen)
1079 struct sockaddr_in sin = {
1080 .sin_family = AF_INET,
1081 .sin_addr.s_addr = htonl(INADDR_ANY),
1083 struct sockaddr_in6 sin6 = {
1084 .sin6_family = AF_INET6,
1085 .sin6_addr = IN6ADDR_ANY_INIT,
1089 sock = socket(sap->sa_family, SOCK_DGRAM, IPPROTO_UDP);
1093 switch (sap->sa_family) {
1095 if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
1101 if (bind(sock, (struct sockaddr *)&sin6, sizeof(sin6)) < 0) {
1107 errno = EAFNOSUPPORT;
1111 if (connect(sock, sap, salen) < 0) {
1116 return !getsockname(sock, buf, buflen);
1120 * Try to generate an address that prevents the server from calling us.
1122 * Returns 1 and fills in @buf if successful; otherwise, zero.
1124 static int nfs_ca_gai(const struct sockaddr *sap,
1125 struct sockaddr *buf, socklen_t *buflen)
1127 struct addrinfo *gai_results;
1128 struct addrinfo gai_hint = {
1129 .ai_family = sap->sa_family,
1130 .ai_flags = AI_PASSIVE, /* ANYADDR */
1133 if (getaddrinfo(NULL, "", &gai_hint, &gai_results))
1136 *buflen = gai_results->ai_addrlen;
1137 memcpy(buf, gai_results->ai_addr, *buflen);
1139 freeaddrinfo(gai_results);
1145 * nfs_callback_address - acquire our local network address
1146 * @sap: pointer to address of remote
1147 * @sap_len: length of address
1148 * @buf: pointer to buffer to be filled in with local network address
1149 * @buflen: IN: length of buffer to fill in; OUT: length of filled-in address
1151 * Discover a network address that an NFSv4 server can use to call us back.
1152 * On multi-homed clients, this address depends on which NIC we use to
1153 * route requests to the server.
1155 * Returns 1 and fills in @buf if an unambiguous local address is
1156 * available; returns 1 and fills in an appropriate ANYADDR address
1157 * if a local address isn't available; otherwise, returns zero.
1159 int nfs_callback_address(const struct sockaddr *sap, const socklen_t salen,
1160 struct sockaddr *buf, socklen_t *buflen)
1162 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1164 if (nfs_ca_sockname(sap, salen, buf, buflen) == 0)
1165 if (nfs_ca_gai(sap, buf, buflen) == 0)
1169 * The server can't use an interface ID that was generated
1170 * here on the client, so always clear sin6_scope_id.
1172 if (sin6->sin6_family == AF_INET6)
1173 sin6->sin6_scope_id = 0;
1180 nfs_error(_("%s: failed to construct callback address"),
1186 * "nfsprog" is supported only by the legacy mount command. The
1187 * kernel mount client does not support this option.
1189 * Returns TRUE if @program contains a valid value for this option,
1190 * or FALSE if the option was specified with an invalid value.
1193 nfs_nfs_program(struct mount_options *options, unsigned long *program)
1197 switch (po_get_numeric(options, "nfsprog", &tmp)) {
1210 * NFS RPC program wasn't specified. The RPC program
1211 * cannot be determined via an rpcbind query.
1213 *program = nfs_getrpcbyname(NFSPROG, nfs_nfs_pgmtbl);
1218 * Returns TRUE if @version contains a valid value for this option,
1219 * or FALSE if the option was specified with an invalid value.
1222 nfs_nfs_version(struct mount_options *options, unsigned long *version)
1226 switch (po_rightmost(options, nfs_version_opttbl)) {
1237 switch (po_get_numeric(options, "vers", &tmp)) {
1239 if (tmp >= 2 && tmp <= 4) {
1245 nfs_error(_("%s: option parsing error\n"),
1250 case 4: /* nfsvers */
1251 switch (po_get_numeric(options, "nfsvers", &tmp)) {
1253 if (tmp >= 2 && tmp <= 4) {
1259 nfs_error(_("%s: option parsing error\n"),
1267 * NFS version wasn't specified. The pmap version value
1268 * will be filled in later by an rpcbind query in this case.
1275 * Returns TRUE if @protocol contains a valid value for this option,
1276 * or FALSE if the option was specified with an invalid value. On
1277 * error, errno is set.
1280 nfs_nfs_protocol(struct mount_options *options, unsigned long *protocol)
1285 switch (po_rightmost(options, nfs_transport_opttbl)) {
1287 *protocol = IPPROTO_UDP;
1290 *protocol = IPPROTO_TCP;
1293 option = po_get(options, "proto");
1294 if (option != NULL) {
1295 if (!nfs_get_proto(option, &family, protocol)) {
1296 errno = EPROTONOSUPPORT;
1304 * NFS transport protocol wasn't specified. The pmap
1305 * protocol value will be filled in later by an rpcbind
1306 * query in this case.
1313 * Returns TRUE if @port contains a valid value for this option,
1314 * or FALSE if the option was specified with an invalid value.
1317 nfs_nfs_port(struct mount_options *options, unsigned long *port)
1321 switch (po_get_numeric(options, "port", &tmp)) {
1325 if (tmp >= 1 && tmp <= 65535) {
1334 * NFS service port wasn't specified. The pmap port value
1335 * will be filled in later by an rpcbind query in this case.
1341 #ifdef IPV6_SUPPORTED
1342 sa_family_t config_default_family = AF_UNSPEC;
1345 nfs_verify_family(sa_family_t family)
1349 #else /* IPV6_SUPPORTED */
1350 sa_family_t config_default_family = AF_INET;
1353 nfs_verify_family(sa_family_t family)
1355 if (family != AF_INET)
1360 #endif /* IPV6_SUPPORTED */
1363 * Returns TRUE and fills in @family if a valid NFS protocol option
1364 * is found, or FALSE if the option was specified with an invalid value
1365 * or if the protocol family isn't supported. On error, errno is set.
1367 int nfs_nfs_proto_family(struct mount_options *options,
1368 sa_family_t *family)
1370 unsigned long protocol;
1372 sa_family_t tmp_family = config_default_family;
1374 switch (po_rightmost(options, nfs_transport_opttbl)) {
1377 /* for compatibility; these are always AF_INET */
1381 option = po_get(options, "proto");
1382 if (option != NULL &&
1383 !nfs_get_proto(option, &tmp_family, &protocol))
1387 if (!nfs_verify_family(tmp_family))
1389 *family = tmp_family;
1392 errno = EAFNOSUPPORT;
1397 * "mountprog" is supported only by the legacy mount command. The
1398 * kernel mount client does not support this option.
1400 * Returns TRUE if @program contains a valid value for this option,
1401 * or FALSE if the option was specified with an invalid value.
1404 nfs_mount_program(struct mount_options *options, unsigned long *program)
1408 switch (po_get_numeric(options, "mountprog", &tmp)) {
1421 * MNT RPC program wasn't specified. The RPC program
1422 * cannot be determined via an rpcbind query.
1424 *program = nfs_getrpcbyname(MOUNTPROG, nfs_mnt_pgmtbl);
1429 * Returns TRUE if @version contains a valid value for this option,
1430 * or FALSE if the option was specified with an invalid value.
1433 nfs_mount_version(struct mount_options *options, unsigned long *version)
1437 switch (po_get_numeric(options, "mountvers", &tmp)) {
1441 if (tmp >= 1 && tmp <= 4) {
1450 * MNT version wasn't specified. The pmap version value
1451 * will be filled in later by an rpcbind query in this case.
1458 * Returns TRUE if @protocol contains a valid value for this option,
1459 * or FALSE if the option was specified with an invalid value. On
1460 * error, errno is set.
1463 nfs_mount_protocol(struct mount_options *options, unsigned long *protocol)
1468 option = po_get(options, "mountproto");
1469 if (option != NULL) {
1470 if (!nfs_get_proto(option, &family, protocol)) {
1471 errno = EPROTONOSUPPORT;
1478 * MNT transport protocol wasn't specified. If the NFS
1479 * transport protocol was specified, use that; otherwise
1480 * set @protocol to zero. The pmap protocol value will
1481 * be filled in later by an rpcbind query in this case.
1483 return nfs_nfs_protocol(options, protocol);
1487 * Returns TRUE if @port contains a valid value for this option,
1488 * or FALSE if the option was specified with an invalid value.
1491 nfs_mount_port(struct mount_options *options, unsigned long *port)
1495 switch (po_get_numeric(options, "mountport", &tmp)) {
1499 if (tmp >= 1 && tmp <= 65535) {
1508 * MNT service port wasn't specified. The pmap port value
1509 * will be filled in later by an rpcbind query in this case.
1516 * Returns TRUE and fills in @family if a valid MNT protocol option
1517 * is found, or FALSE if the option was specified with an invalid value
1518 * or if the protocol family isn't supported. On error, errno is set.
1520 int nfs_mount_proto_family(struct mount_options *options,
1521 sa_family_t *family)
1523 unsigned long protocol;
1525 sa_family_t tmp_family = config_default_family;
1527 option = po_get(options, "mountproto");
1528 if (option != NULL) {
1529 if (!nfs_get_proto(option, &tmp_family, &protocol))
1531 if (!nfs_verify_family(tmp_family))
1533 *family = tmp_family;
1538 * MNT transport protocol wasn't specified. If the NFS
1539 * transport protocol was specified, derive the family
1540 * from that; otherwise, return the default family for
1543 return nfs_nfs_proto_family(options, family);
1545 errno = EAFNOSUPPORT;
1550 * nfs_options2pmap - set up pmap structs based on mount options
1551 * @options: pointer to mount options
1552 * @nfs_pmap: OUT: pointer to pmap arguments for NFS server
1553 * @mnt_pmap: OUT: pointer to pmap arguments for mountd server
1555 * Returns TRUE if the pmap options specified in @options have valid
1556 * values; otherwise FALSE is returned.
1558 int nfs_options2pmap(struct mount_options *options,
1559 struct pmap *nfs_pmap, struct pmap *mnt_pmap)
1561 if (!nfs_nfs_program(options, &nfs_pmap->pm_prog))
1563 if (!nfs_nfs_version(options, &nfs_pmap->pm_vers))
1565 if (!nfs_nfs_protocol(options, &nfs_pmap->pm_prot))
1567 if (!nfs_nfs_port(options, &nfs_pmap->pm_port))
1570 if (!nfs_mount_program(options, &mnt_pmap->pm_prog))
1572 if (!nfs_mount_version(options, &mnt_pmap->pm_vers))
1574 if (!nfs_mount_protocol(options, &mnt_pmap->pm_prot))
1576 if (!nfs_mount_port(options, &mnt_pmap->pm_port))