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[] = {
142 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *saddr)
146 saddr->sin_family = AF_INET;
147 if (!inet_aton(hostname, &saddr->sin_addr)) {
148 if ((hp = gethostbyname(hostname)) == NULL) {
149 nfs_error(_("%s: can't get address for %s\n"),
153 if (hp->h_length > sizeof(*saddr)) {
154 nfs_error(_("%s: got bad hp->h_length\n"),
156 hp->h_length = sizeof(*saddr);
158 memcpy(&saddr->sin_addr, hp->h_addr, hp->h_length);
165 * Attempt to connect a socket, but time out after "timeout" seconds.
167 * On error return, caller closes the socket.
169 static int connect_to(int fd, struct sockaddr *addr,
170 socklen_t addrlen, int timeout)
174 struct timeval tv = {
178 saved = fcntl(fd, F_GETFL, 0);
179 fcntl(fd, F_SETFL, saved | O_NONBLOCK);
181 ret = connect(fd, addr, addrlen);
182 if (ret < 0 && errno != EINPROGRESS)
190 ret = select(fd + 1, &rset, &wset, NULL, &tv);
195 if (FD_ISSET(fd, &rset) || FD_ISSET(fd, &wset)) {
197 socklen_t len = sizeof(error);
198 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
208 fcntl(fd, F_SETFL, saved);
213 * Create a socket that is locally bound to a reserved or non-reserved port.
215 * The caller should check rpc_createerr to determine the cause of any error.
217 static int get_socket(struct sockaddr_in *saddr, unsigned int p_prot,
218 unsigned int timeout, int resvp, int conn)
221 struct sockaddr_in laddr;
222 socklen_t namelen = sizeof(laddr);
224 type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
225 if ((so = socket (AF_INET, type, p_prot)) < 0)
228 laddr.sin_family = AF_INET;
230 laddr.sin_addr.s_addr = htonl(INADDR_ANY);
232 if (bindresvport(so, &laddr) < 0)
233 goto err_bindresvport;
235 cc = bind(so, (struct sockaddr *)&laddr, namelen);
239 if (type == SOCK_STREAM || (conn && type == SOCK_DGRAM)) {
240 cc = connect_to(so, (struct sockaddr *)saddr, namelen,
248 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
249 rpc_createerr.cf_error.re_errno = errno;
251 nfs_error(_("%s: Unable to create %s socket: errno %d (%s)\n"),
252 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
253 errno, strerror(errno));
258 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
259 rpc_createerr.cf_error.re_errno = errno;
261 nfs_error(_("%s: Unable to bindresvport %s socket: errno %d"
263 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
264 errno, strerror(errno));
270 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
271 rpc_createerr.cf_error.re_errno = errno;
273 nfs_error(_("%s: Unable to bind to %s socket: errno %d (%s)\n"),
274 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
275 errno, strerror(errno));
281 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
282 rpc_createerr.cf_error.re_errno = errno;
284 nfs_error(_("%s: Unable to connect to %s:%d, errno %d (%s)\n"),
285 progname, inet_ntoa(saddr->sin_addr),
286 ntohs(saddr->sin_port), errno, strerror(errno));
293 * getport() is very similar to pmap_getport() with the exception that
294 * this version tries to use an ephemeral port, since reserved ports are
295 * not needed for GETPORT queries. This conserves the very limited
296 * reserved port space, which helps reduce failed socket binds
297 * during mount storms.
299 * A side effect of calling this function is that rpccreateerr is set.
301 static unsigned short getport(struct sockaddr_in *saddr,
302 unsigned long program,
303 unsigned long version,
306 struct sockaddr_in bind_saddr;
307 unsigned short port = 0;
313 bind_saddr.sin_port = htons(PMAPPORT);
315 socket = get_socket(&bind_saddr, proto, PMAP_TIMEOUT, FALSE, FALSE);
316 if (socket == RPC_ANYSOCK) {
317 if (proto == IPPROTO_TCP &&
318 rpc_createerr.cf_error.re_errno == ETIMEDOUT)
319 rpc_createerr.cf_stat = RPC_TIMEDOUT;
325 clnt = clntudp_bufcreate(&bind_saddr,
327 RETRY_TIMEOUT, &socket,
332 clnt = clnttcp_create(&bind_saddr,
335 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
339 struct pmap parms = {
345 stat = clnt_call(clnt, PMAPPROC_GETPORT,
346 (xdrproc_t)xdr_pmap, (caddr_t)&parms,
347 (xdrproc_t)xdr_u_short, (caddr_t)&port,
350 clnt_geterr(clnt, &rpc_createerr.cf_error);
351 rpc_createerr.cf_stat = stat;
354 if (stat != RPC_SUCCESS)
357 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
365 * Use the portmapper to discover whether or not the service we want is
366 * available. The lists 'versions' and 'protos' define ordered sequences
367 * of service versions and udp/tcp protocols to probe for.
369 static int probe_port(clnt_addr_t *server, const unsigned long *versions,
370 const unsigned int *protos)
372 struct sockaddr_in *saddr = &server->saddr;
373 struct pmap *pmap = &server->pmap;
374 const unsigned long prog = pmap->pm_prog, *p_vers;
375 const unsigned int prot = (u_int)pmap->pm_prot, *p_prot;
376 const u_short port = (u_short) pmap->pm_port;
377 unsigned long vers = pmap->pm_vers;
378 unsigned short p_port;
380 p_prot = prot ? &prot : protos;
381 p_vers = vers ? &vers : versions;
382 rpc_createerr.cf_stat = 0;
384 p_port = getport(saddr, prog, *p_vers, *p_prot);
386 if (!port || port == p_port) {
387 saddr->sin_port = htons(p_port);
389 printf(_("%s: trying %s prog %ld vers "
390 "%ld prot %s port %d\n"),
392 inet_ntoa(saddr->sin_addr),
394 *p_prot == IPPROTO_UDP ?
398 if (clnt_ping(saddr, prog, *p_vers, *p_prot, NULL))
400 if (rpc_createerr.cf_stat == RPC_TIMEDOUT)
404 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED)
412 if (vers == pmap->pm_vers) {
416 if (vers || !*++p_vers)
425 pmap->pm_vers = *p_vers;
427 pmap->pm_prot = *p_prot;
429 pmap->pm_port = p_port;
430 rpc_createerr.cf_stat = 0;
434 static int probe_nfsport(clnt_addr_t *nfs_server)
436 struct pmap *pmap = &nfs_server->pmap;
438 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
441 if (nfs_mount_data_version >= 4)
442 return probe_port(nfs_server, probe_nfs3_first, probe_tcp_first);
444 return probe_port(nfs_server, probe_nfs2_only, probe_udp_only);
447 static int probe_mntport(clnt_addr_t *mnt_server)
449 struct pmap *pmap = &mnt_server->pmap;
451 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
454 if (nfs_mount_data_version >= 4)
455 return probe_port(mnt_server, probe_mnt3_first, probe_udp_first);
457 return probe_port(mnt_server, probe_mnt1_first, probe_udp_only);
460 int probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
462 struct pmap *nfs_pmap = &nfs_server->pmap;
463 struct pmap *mnt_pmap = &mnt_server->pmap;
464 struct pmap save_nfs, save_mnt;
466 const unsigned long *probe_vers;
468 if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
469 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
470 else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
471 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
472 if (nfs_pmap->pm_vers)
475 memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
476 memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
477 probe_vers = (nfs_mount_data_version >= 4) ?
478 probe_mnt3_first : probe_mnt1_first;
480 for (; *probe_vers; probe_vers++) {
481 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
482 if ((res = probe_nfsport(nfs_server) != 0)) {
483 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
484 if ((res = probe_mntport(mnt_server)) != 0)
486 memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
488 switch (rpc_createerr.cf_stat) {
489 case RPC_PROGVERSMISMATCH:
490 case RPC_PROGNOTREGISTERED:
495 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
502 if (!probe_nfsport(nfs_server))
504 return probe_mntport(mnt_server);
507 static int probe_statd(void)
509 struct sockaddr_in addr;
512 memset(&addr, 0, sizeof(addr));
513 addr.sin_family = AF_INET;
514 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
515 port = getport(&addr, 100024, 1, IPPROTO_UDP);
519 addr.sin_port = htons(port);
521 if (clnt_ping(&addr, 100024, 1, IPPROTO_UDP, NULL) <= 0)
528 * Attempt to start rpc.statd
530 int start_statd(void)
540 if (stat(START_STATD, &stb) == 0) {
541 if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {
553 * nfs_call_umount - ask the server to remove a share from it's rmtab
554 * @mnt_server: address of RPC MNT program server
555 * @argp: directory path of share to "unmount"
557 * Returns one if the unmount call succeeded; zero if the unmount
558 * failed for any reason.
560 * Note that a side effect of calling this function is that rpccreateerr
563 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
566 enum clnt_stat res = 0;
569 switch (mnt_server->pmap.pm_vers) {
573 if (!probe_mntport(mnt_server))
575 clnt = mnt_openclnt(mnt_server, &msock);
578 res = clnt_call(clnt, MOUNTPROC_UMNT,
579 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
580 (xdrproc_t)xdr_void, NULL,
582 mnt_closeclnt(clnt, msock);
583 if (res == RPC_SUCCESS)
591 if (res == RPC_SUCCESS)
596 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
598 struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
599 struct pmap *mnt_pmap = &mnt_server->pmap;
602 mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
603 *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, MOUNT_TIMEOUT,
605 if (*msock == RPC_ANYSOCK) {
606 if (rpc_createerr.cf_error.re_errno == EADDRINUSE)
608 * Probably in-use by a TIME_WAIT connection,
609 * It is worth waiting a while and trying again.
611 rpc_createerr.cf_stat = RPC_TIMEDOUT;
615 switch (mnt_pmap->pm_prot) {
617 clnt = clntudp_bufcreate(mnt_saddr,
618 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
619 RETRY_TIMEOUT, msock,
620 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
623 clnt = clnttcp_create(mnt_saddr,
624 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
626 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
630 /* try to mount hostname:dirname */
631 clnt->cl_auth = authunix_create_default();
637 void mnt_closeclnt(CLIENT *clnt, int msock)
639 auth_destroy(clnt->cl_auth);
645 * Sigh... getport() doesn't actually check the version number.
646 * In order to make sure that the server actually supports the service
647 * we're requesting, we open and RPC client, and fire off a NULL
650 int clnt_ping(struct sockaddr_in *saddr, const unsigned long prog,
651 const unsigned long vers, const unsigned int prot,
652 struct sockaddr_in *caddr)
656 static char clnt_res;
657 struct sockaddr dissolve;
659 rpc_createerr.cf_stat = stat = 0;
660 sock = get_socket(saddr, prot, CONNECT_TIMEOUT, FALSE, TRUE);
661 if (sock == RPC_ANYSOCK) {
662 if (rpc_createerr.cf_error.re_errno == ETIMEDOUT) {
664 * TCP timeout. Bubble up the error to see
665 * how it should be handled.
667 rpc_createerr.cf_stat = RPC_TIMEDOUT;
673 /* Get the address of our end of this connection */
674 socklen_t len = sizeof(*caddr);
675 if (getsockname(sock, caddr, &len) != 0)
676 caddr->sin_family = 0;
681 /* The socket is connected (so we could getsockname successfully),
682 * but some servers on multi-homed hosts reply from
683 * the wrong address, so if we stay connected, we lose the reply.
685 dissolve.sa_family = AF_UNSPEC;
686 connect(sock, &dissolve, sizeof(dissolve));
688 clnt = clntudp_bufcreate(saddr, prog, vers,
689 RETRY_TIMEOUT, &sock,
690 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
693 clnt = clnttcp_create(saddr, prog, vers, &sock,
694 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
701 memset(&clnt_res, 0, sizeof(clnt_res));
702 stat = clnt_call(clnt, NULLPROC,
703 (xdrproc_t)xdr_void, (caddr_t)NULL,
704 (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
707 clnt_geterr(clnt, &rpc_createerr.cf_error);
708 rpc_createerr.cf_stat = stat;
713 if (stat == RPC_SUCCESS)