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.
33 #include <rpc/pmap_prot.h>
34 #include <rpc/pmap_clnt.h>
35 #include <sys/socket.h>
40 #include "nfs_mount.h"
41 #include "mount_constants.h"
44 #ifdef HAVE_RPCSVC_NFS_PROT_H
45 #include <rpcsvc/nfs_prot.h>
47 #include <linux/nfs.h>
48 #define nfsstat nfs_stat
55 #define PMAP_TIMEOUT (10)
56 #define CONNECT_TIMEOUT (20)
57 #define MOUNT_TIMEOUT (30)
59 #if SIZEOF_SOCKLEN_T - 0 == 0
60 #define socklen_t unsigned int
63 extern int nfs_mount_data_version;
64 extern char *progname;
67 static const unsigned long nfs_to_mnt[] = {
74 static const unsigned long mnt_to_nfs[] = {
82 * Map an NFS version into the corresponding Mountd version
84 unsigned long nfsvers_to_mnt(const unsigned long vers)
87 return nfs_to_mnt[vers];
92 * Map a Mountd version into the corresponding NFS version
94 static unsigned long mntvers_to_nfs(const unsigned long vers)
97 return mnt_to_nfs[vers];
101 static const unsigned int probe_udp_only[] = {
106 static const unsigned int probe_udp_first[] = {
112 static const unsigned int probe_tcp_first[] = {
118 static const unsigned long probe_nfs2_only[] = {
123 static const unsigned long probe_nfs3_first[] = {
129 static const unsigned long probe_mnt1_first[] = {
135 static const unsigned long probe_mnt3_first[] = {
143 * nfs_gethostbyname - resolve a hostname to an IPv4 address
144 * @hostname: pointer to a C string containing a DNS hostname
145 * @saddr: returns an IPv4 address
147 * Returns 1 if successful, otherwise zero.
149 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *saddr)
153 saddr->sin_family = AF_INET;
154 if (!inet_aton(hostname, &saddr->sin_addr)) {
155 if ((hp = gethostbyname(hostname)) == NULL) {
156 nfs_error(_("%s: can't get address for %s\n"),
160 if (hp->h_length > sizeof(*saddr)) {
161 nfs_error(_("%s: got bad hp->h_length\n"),
163 hp->h_length = sizeof(*saddr);
165 memcpy(&saddr->sin_addr, hp->h_addr, hp->h_length);
172 * Attempt to connect a socket, but time out after "timeout" seconds.
174 * On error return, caller closes the socket.
176 static int connect_to(int fd, struct sockaddr *addr,
177 socklen_t addrlen, int timeout)
181 struct timeval tv = {
185 saved = fcntl(fd, F_GETFL, 0);
186 fcntl(fd, F_SETFL, saved | O_NONBLOCK);
188 ret = connect(fd, addr, addrlen);
189 if (ret < 0 && errno != EINPROGRESS)
197 ret = select(fd + 1, &rset, &wset, NULL, &tv);
202 if (FD_ISSET(fd, &rset) || FD_ISSET(fd, &wset)) {
204 socklen_t len = sizeof(error);
205 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
215 fcntl(fd, F_SETFL, saved);
220 * Create a socket that is locally bound to a reserved or non-reserved port.
222 * The caller should check rpc_createerr to determine the cause of any error.
224 static int get_socket(struct sockaddr_in *saddr, unsigned int p_prot,
225 unsigned int timeout, int resvp, int conn)
228 struct sockaddr_in laddr;
229 socklen_t namelen = sizeof(laddr);
231 type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
232 if ((so = socket (AF_INET, type, p_prot)) < 0)
235 laddr.sin_family = AF_INET;
237 laddr.sin_addr.s_addr = htonl(INADDR_ANY);
239 if (bindresvport(so, &laddr) < 0)
240 goto err_bindresvport;
242 cc = bind(so, (struct sockaddr *)&laddr, namelen);
246 if (type == SOCK_STREAM || (conn && type == SOCK_DGRAM)) {
247 cc = connect_to(so, (struct sockaddr *)saddr, namelen,
255 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
256 rpc_createerr.cf_error.re_errno = errno;
258 nfs_error(_("%s: Unable to create %s socket: errno %d (%s)\n"),
259 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
260 errno, strerror(errno));
265 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
266 rpc_createerr.cf_error.re_errno = errno;
268 nfs_error(_("%s: Unable to bindresvport %s socket: errno %d"
270 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
271 errno, strerror(errno));
277 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
278 rpc_createerr.cf_error.re_errno = errno;
280 nfs_error(_("%s: Unable to bind to %s socket: errno %d (%s)\n"),
281 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
282 errno, strerror(errno));
288 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
289 rpc_createerr.cf_error.re_errno = errno;
291 nfs_error(_("%s: Unable to connect to %s:%d, errno %d (%s)\n"),
292 progname, inet_ntoa(saddr->sin_addr),
293 ntohs(saddr->sin_port), errno, strerror(errno));
300 * getport() is very similar to pmap_getport() with the exception that
301 * this version tries to use an ephemeral port, since reserved ports are
302 * not needed for GETPORT queries. This conserves the very limited
303 * reserved port space, which helps reduce failed socket binds
304 * during mount storms.
306 * A side effect of calling this function is that rpccreateerr is set.
308 static unsigned short getport(struct sockaddr_in *saddr,
309 unsigned long program,
310 unsigned long version,
313 struct sockaddr_in bind_saddr;
314 unsigned short port = 0;
320 bind_saddr.sin_port = htons(PMAPPORT);
322 socket = get_socket(&bind_saddr, proto, PMAP_TIMEOUT, FALSE, FALSE);
323 if (socket == RPC_ANYSOCK) {
324 if (proto == IPPROTO_TCP &&
325 rpc_createerr.cf_error.re_errno == ETIMEDOUT)
326 rpc_createerr.cf_stat = RPC_TIMEDOUT;
332 clnt = clntudp_bufcreate(&bind_saddr,
334 RETRY_TIMEOUT, &socket,
339 clnt = clnttcp_create(&bind_saddr,
342 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
346 struct pmap parms = {
352 stat = clnt_call(clnt, PMAPPROC_GETPORT,
353 (xdrproc_t)xdr_pmap, (caddr_t)&parms,
354 (xdrproc_t)xdr_u_short, (caddr_t)&port,
357 clnt_geterr(clnt, &rpc_createerr.cf_error);
358 rpc_createerr.cf_stat = stat;
361 if (stat != RPC_SUCCESS)
364 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
372 * Use the portmapper to discover whether or not the service we want is
373 * available. The lists 'versions' and 'protos' define ordered sequences
374 * of service versions and udp/tcp protocols to probe for.
376 static int probe_port(clnt_addr_t *server, const unsigned long *versions,
377 const unsigned int *protos)
379 struct sockaddr_in *saddr = &server->saddr;
380 struct pmap *pmap = &server->pmap;
381 const unsigned long prog = pmap->pm_prog, *p_vers;
382 const unsigned int prot = (u_int)pmap->pm_prot, *p_prot;
383 const u_short port = (u_short) pmap->pm_port;
384 unsigned long vers = pmap->pm_vers;
385 unsigned short p_port;
387 p_prot = prot ? &prot : protos;
388 p_vers = vers ? &vers : versions;
389 rpc_createerr.cf_stat = 0;
391 p_port = getport(saddr, prog, *p_vers, *p_prot);
393 if (!port || port == p_port) {
394 saddr->sin_port = htons(p_port);
396 printf(_("%s: trying %s prog %ld vers "
397 "%ld prot %s port %d\n"),
399 inet_ntoa(saddr->sin_addr),
401 *p_prot == IPPROTO_UDP ?
405 if (clnt_ping(saddr, prog, *p_vers, *p_prot, NULL))
407 if (rpc_createerr.cf_stat == RPC_TIMEDOUT)
411 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
412 rpc_createerr.cf_stat != RPC_PROGVERSMISMATCH)
420 if (vers == pmap->pm_vers) {
424 if (vers || !*++p_vers)
433 pmap->pm_vers = *p_vers;
435 pmap->pm_prot = *p_prot;
437 pmap->pm_port = p_port;
438 rpc_createerr.cf_stat = 0;
442 static int probe_nfsport(clnt_addr_t *nfs_server)
444 struct pmap *pmap = &nfs_server->pmap;
446 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
449 if (nfs_mount_data_version >= 4)
450 return probe_port(nfs_server, probe_nfs3_first, probe_tcp_first);
452 return probe_port(nfs_server, probe_nfs2_only, probe_udp_only);
455 static int probe_mntport(clnt_addr_t *mnt_server)
457 struct pmap *pmap = &mnt_server->pmap;
459 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
462 if (nfs_mount_data_version >= 4)
463 return probe_port(mnt_server, probe_mnt3_first, probe_udp_first);
465 return probe_port(mnt_server, probe_mnt1_first, probe_udp_only);
469 * probe_bothports - discover the RPC endpoints of mountd and NFS server
470 * @mnt_server: pointer to address and pmap argument for mountd results
471 * @nfs_server: pointer to address and pmap argument for NFS server
473 * Returns 1 if successful, otherwise zero if some error occurred.
474 * Note that the arguments are both input and output arguments.
476 * A side effect of calling this function is that rpccreateerr is set.
478 int probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
480 struct pmap *nfs_pmap = &nfs_server->pmap;
481 struct pmap *mnt_pmap = &mnt_server->pmap;
482 struct pmap save_nfs, save_mnt;
484 const unsigned long *probe_vers;
486 if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
487 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
488 else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
489 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
490 if (nfs_pmap->pm_vers)
493 memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
494 memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
495 probe_vers = (nfs_mount_data_version >= 4) ?
496 probe_mnt3_first : probe_mnt1_first;
498 for (; *probe_vers; probe_vers++) {
499 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
500 if ((res = probe_nfsport(nfs_server) != 0)) {
501 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
502 if ((res = probe_mntport(mnt_server)) != 0)
504 memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
506 switch (rpc_createerr.cf_stat) {
507 case RPC_PROGVERSMISMATCH:
508 case RPC_PROGNOTREGISTERED:
513 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
520 if (!probe_nfsport(nfs_server))
522 return probe_mntport(mnt_server);
525 static int probe_statd(void)
527 struct sockaddr_in addr;
530 memset(&addr, 0, sizeof(addr));
531 addr.sin_family = AF_INET;
532 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
533 port = getport(&addr, 100024, 1, IPPROTO_UDP);
537 addr.sin_port = htons(port);
539 if (clnt_ping(&addr, 100024, 1, IPPROTO_UDP, NULL) <= 0)
546 * start_statd - attempt to start rpc.statd
548 * Returns 1 if statd is running; otherwise zero.
550 int start_statd(void)
560 if (stat(START_STATD, &stb) == 0) {
561 if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {
573 * nfs_call_umount - ask the server to remove a share from it's rmtab
574 * @mnt_server: address of RPC MNT program server
575 * @argp: directory path of share to "unmount"
577 * Returns one if the unmount call succeeded; zero if the unmount
578 * failed for any reason.
580 * Note that a side effect of calling this function is that rpccreateerr
583 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
586 enum clnt_stat res = 0;
589 if (!probe_mntport(mnt_server))
591 clnt = mnt_openclnt(mnt_server, &msock);
594 res = clnt_call(clnt, MOUNTPROC_UMNT,
595 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
596 (xdrproc_t)xdr_void, NULL,
598 mnt_closeclnt(clnt, msock);
600 if (res == RPC_SUCCESS)
606 * mnt_openclnt - get a handle for a remote mountd service
607 * @mnt_server: address and pmap arguments of mountd service
608 * @msock: returns a file descriptor of the underlying transport socket
610 * Returns an active handle for the remote's mountd service
612 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
614 struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
615 struct pmap *mnt_pmap = &mnt_server->pmap;
618 mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
619 *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, MOUNT_TIMEOUT,
621 if (*msock == RPC_ANYSOCK) {
622 if (rpc_createerr.cf_error.re_errno == EADDRINUSE)
624 * Probably in-use by a TIME_WAIT connection,
625 * It is worth waiting a while and trying again.
627 rpc_createerr.cf_stat = RPC_TIMEDOUT;
631 switch (mnt_pmap->pm_prot) {
633 clnt = clntudp_bufcreate(mnt_saddr,
634 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
635 RETRY_TIMEOUT, msock,
636 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
639 clnt = clnttcp_create(mnt_saddr,
640 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
642 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
646 /* try to mount hostname:dirname */
647 clnt->cl_auth = authunix_create_default();
654 * mnt_closeclnt - terminate a handle for a remote mountd service
655 * @clnt: pointer to an active handle for a remote mountd service
656 * @msock: file descriptor of the underlying transport socket
659 void mnt_closeclnt(CLIENT *clnt, int msock)
661 auth_destroy(clnt->cl_auth);
667 * clnt_ping - send an RPC ping to the remote RPC service endpoint
668 * @saddr: server's address
669 * @prog: target RPC program number
670 * @vers: target RPC version number
671 * @prot: target RPC protocol
672 * @caddr: filled in with our network address
674 * Sigh... getport() doesn't actually check the version number.
675 * In order to make sure that the server actually supports the service
676 * we're requesting, we open and RPC client, and fire off a NULL
679 * caddr is the network address that the server will use to call us back.
680 * On multi-homed clients, this address depends on which NIC we use to
681 * route requests to the server.
683 * Returns one if successful, otherwise zero.
685 int clnt_ping(struct sockaddr_in *saddr, const unsigned long prog,
686 const unsigned long vers, const unsigned int prot,
687 struct sockaddr_in *caddr)
691 static char clnt_res;
692 struct sockaddr dissolve;
694 rpc_createerr.cf_stat = stat = 0;
695 sock = get_socket(saddr, prot, CONNECT_TIMEOUT, FALSE, TRUE);
696 if (sock == RPC_ANYSOCK) {
697 if (rpc_createerr.cf_error.re_errno == ETIMEDOUT) {
699 * TCP timeout. Bubble up the error to see
700 * how it should be handled.
702 rpc_createerr.cf_stat = RPC_TIMEDOUT;
708 /* Get the address of our end of this connection */
709 socklen_t len = sizeof(*caddr);
710 if (getsockname(sock, caddr, &len) != 0)
711 caddr->sin_family = 0;
716 /* The socket is connected (so we could getsockname successfully),
717 * but some servers on multi-homed hosts reply from
718 * the wrong address, so if we stay connected, we lose the reply.
720 dissolve.sa_family = AF_UNSPEC;
721 connect(sock, &dissolve, sizeof(dissolve));
723 clnt = clntudp_bufcreate(saddr, prog, vers,
724 RETRY_TIMEOUT, &sock,
725 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
728 clnt = clnttcp_create(saddr, prog, vers, &sock,
729 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
736 memset(&clnt_res, 0, sizeof(clnt_res));
737 stat = clnt_call(clnt, NULLPROC,
738 (xdrproc_t)xdr_void, (caddr_t)NULL,
739 (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
742 clnt_geterr(clnt, &rpc_createerr.cf_error);
743 rpc_createerr.cf_stat = stat;
748 if (stat == RPC_SUCCESS)
755 * get_client_address - acquire our local network address
756 * @saddr: server's address
757 * @caddr: filled in with our network address
759 * Discover a network address that the server will use to call us back.
760 * On multi-homed clients, this address depends on which NIC we use to
761 * route requests to the server.
763 * Use a connected datagram socket so as not to leave a socket in TIME_WAIT.
765 * Returns one if successful, otherwise zero.
767 int get_client_address(struct sockaddr_in *saddr, struct sockaddr_in *caddr)
769 socklen_t len = sizeof(*caddr);
772 socket = get_socket(saddr, IPPROTO_UDP, CONNECT_TIMEOUT, FALSE, TRUE);
773 if (socket == RPC_ANYSOCK)
776 err = getsockname(socket, caddr, &len);
779 if (err && verbose) {
780 nfs_error(_("%s: getsockname failed: %s"),
781 progname, strerror(errno));