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>
41 #include "nfs_mount.h"
42 #include "mount_constants.h"
45 #ifdef HAVE_RPCSVC_NFS_PROT_H
46 #include <rpcsvc/nfs_prot.h>
48 #include <linux/nfs.h>
49 #define nfsstat nfs_stat
56 #if SIZEOF_SOCKLEN_T - 0 == 0
57 #define socklen_t unsigned int
60 extern int nfs_mount_data_version;
61 extern char *progname;
64 static const unsigned long nfs_to_mnt[] = {
71 static const unsigned long mnt_to_nfs[] = {
79 * Map an NFS version into the corresponding Mountd version
81 unsigned long nfsvers_to_mnt(const unsigned long vers)
84 return nfs_to_mnt[vers];
89 * Map a Mountd version into the corresponding NFS version
91 static unsigned long mntvers_to_nfs(const unsigned long vers)
94 return mnt_to_nfs[vers];
98 static const unsigned int probe_udp_only[] = {
103 static const unsigned int probe_udp_first[] = {
109 static const unsigned int probe_tcp_first[] = {
115 static const unsigned long probe_nfs2_only[] = {
120 static const unsigned long probe_nfs3_first[] = {
126 static const unsigned long probe_mnt1_first[] = {
132 static const unsigned long probe_mnt3_first[] = {
139 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *saddr)
143 saddr->sin_family = AF_INET;
144 if (!inet_aton(hostname, &saddr->sin_addr)) {
145 if ((hp = gethostbyname(hostname)) == NULL) {
146 nfs_error(_("%s: can't get address for %s\n"),
150 if (hp->h_length > sizeof(*saddr)) {
151 nfs_error(_("%s: got bad hp->h_length\n"),
153 hp->h_length = sizeof(*saddr);
155 memcpy(&saddr->sin_addr, hp->h_addr, hp->h_length);
162 * Create a socket that is locally bound to a reserved or non-reserved
163 * port. For any failures, RPC_ANYSOCK is returned which will cause
164 * the RPC code to create the socket instead.
166 static int get_socket(struct sockaddr_in *saddr, unsigned int p_prot,
170 struct sockaddr_in laddr;
171 socklen_t namelen = sizeof(laddr);
173 type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
174 if ((so = socket (AF_INET, type, p_prot)) < 0)
177 laddr.sin_family = AF_INET;
179 laddr.sin_addr.s_addr = htonl(INADDR_ANY);
181 if (bindresvport(so, &laddr) < 0)
182 goto err_bindresvport;
184 cc = bind(so, (struct sockaddr *)&laddr, namelen);
188 if (type == SOCK_STREAM || (conn && type == SOCK_DGRAM)) {
189 cc = connect(so, (struct sockaddr *)saddr, namelen);
196 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
197 rpc_createerr.cf_error.re_errno = errno;
199 nfs_error(_("%s: Unable to create %s socket: errno %d (%s)\n"),
200 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
201 errno, strerror(errno));
206 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
207 rpc_createerr.cf_error.re_errno = errno;
209 nfs_error(_("%s: Unable to bindresvport %s socket: errno %d"
211 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
212 errno, strerror(errno));
218 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
219 rpc_createerr.cf_error.re_errno = errno;
221 nfs_error(_("%s: Unable to bind to %s socket: errno %d (%s)\n"),
222 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
223 errno, strerror(errno));
229 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
230 rpc_createerr.cf_error.re_errno = errno;
232 nfs_error(_("%s: Unable to connect to %s:%d, errno %d (%s)\n"),
233 progname, inet_ntoa(saddr->sin_addr),
234 ntohs(saddr->sin_port), errno, strerror(errno));
241 * getport() is very similar to pmap_getport() with the exception that
242 * this version tries to use an ephemeral port, since reserved ports are
243 * not needed for GETPORT queries. This conserves the very limited
244 * reserved port space, which helps reduce failed socket binds
245 * during mount storms.
247 * A side effect of calling this function is that rpccreateerr is set.
249 static unsigned short getport(struct sockaddr_in *saddr,
250 unsigned long program,
251 unsigned long version,
254 unsigned short port = 0;
259 saddr->sin_port = htons(PMAPPORT);
262 * Try to get a socket with a non-privileged port.
263 * clnt*create() will create one anyway if this
266 socket = get_socket(saddr, proto, FALSE, FALSE);
267 if (socket == RPC_ANYSOCK) {
268 if (proto == IPPROTO_TCP && errno == ETIMEDOUT) {
270 * TCP SYN timed out, so exit now.
272 rpc_createerr.cf_stat = RPC_TIMEDOUT;
279 clnt = clntudp_bufcreate(saddr,
281 RETRY_TIMEOUT, &socket,
286 clnt = clnttcp_create(saddr, PMAPPROG, PMAPVERS, &socket,
287 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
291 struct pmap parms = {
297 stat = clnt_call(clnt, PMAPPROC_GETPORT,
298 (xdrproc_t)xdr_pmap, (caddr_t)&parms,
299 (xdrproc_t)xdr_u_short, (caddr_t)&port,
302 clnt_geterr(clnt, &rpc_createerr.cf_error);
303 rpc_createerr.cf_stat = stat;
306 if (stat != RPC_SUCCESS)
309 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
318 * Use the portmapper to discover whether or not the service we want is
319 * available. The lists 'versions' and 'protos' define ordered sequences
320 * of service versions and udp/tcp protocols to probe for.
322 static int probe_port(clnt_addr_t *server, const unsigned long *versions,
323 const unsigned int *protos)
325 struct sockaddr_in *saddr = &server->saddr;
326 struct pmap *pmap = &server->pmap;
327 const unsigned long prog = pmap->pm_prog, *p_vers;
328 const unsigned int prot = (u_int)pmap->pm_prot, *p_prot;
329 const u_short port = (u_short) pmap->pm_port;
330 unsigned long vers = pmap->pm_vers;
331 unsigned short p_port;
333 p_prot = prot ? &prot : protos;
334 p_vers = vers ? &vers : versions;
335 rpc_createerr.cf_stat = 0;
337 saddr->sin_port = htons(PMAPPORT);
338 p_port = getport(saddr, prog, *p_vers, *p_prot);
340 if (!port || port == p_port) {
341 saddr->sin_port = htons(p_port);
343 printf(_("%s: trying %s prog %ld vers "
344 "%ld prot %s port %d\n"),
346 inet_ntoa(saddr->sin_addr),
348 *p_prot == IPPROTO_UDP ?
352 if (clnt_ping(saddr, prog, *p_vers, *p_prot, NULL))
354 if (rpc_createerr.cf_stat == RPC_TIMEDOUT)
358 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED)
366 if (vers == pmap->pm_vers) {
370 if (vers || !*++p_vers)
379 pmap->pm_vers = *p_vers;
381 pmap->pm_prot = *p_prot;
383 pmap->pm_port = p_port;
384 rpc_createerr.cf_stat = 0;
388 static int probe_nfsport(clnt_addr_t *nfs_server)
390 struct pmap *pmap = &nfs_server->pmap;
392 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
395 if (nfs_mount_data_version >= 4)
396 return probe_port(nfs_server, probe_nfs3_first, probe_tcp_first);
398 return probe_port(nfs_server, probe_nfs2_only, probe_udp_only);
401 static int probe_mntport(clnt_addr_t *mnt_server)
403 struct pmap *pmap = &mnt_server->pmap;
405 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
408 if (nfs_mount_data_version >= 4)
409 return probe_port(mnt_server, probe_mnt3_first, probe_udp_first);
411 return probe_port(mnt_server, probe_mnt1_first, probe_udp_only);
414 int probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
416 struct pmap *nfs_pmap = &nfs_server->pmap;
417 struct pmap *mnt_pmap = &mnt_server->pmap;
418 struct pmap save_nfs, save_mnt;
420 const unsigned long *probe_vers;
422 if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
423 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
424 else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
425 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
426 if (nfs_pmap->pm_vers)
429 memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
430 memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
431 probe_vers = (nfs_mount_data_version >= 4) ?
432 probe_mnt3_first : probe_mnt1_first;
434 for (; *probe_vers; probe_vers++) {
435 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
436 if ((res = probe_nfsport(nfs_server) != 0)) {
437 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
438 if ((res = probe_mntport(mnt_server)) != 0)
440 memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
442 switch (rpc_createerr.cf_stat) {
443 case RPC_PROGVERSMISMATCH:
444 case RPC_PROGNOTREGISTERED:
449 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
456 if (!probe_nfsport(nfs_server))
458 return probe_mntport(mnt_server);
461 static int probe_statd(void)
463 struct sockaddr_in addr;
466 memset(&addr, 0, sizeof(addr));
467 addr.sin_family = AF_INET;
468 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
469 port = getport(&addr, 100024, 1, IPPROTO_UDP);
473 addr.sin_port = htons(port);
475 if (clnt_ping(&addr, 100024, 1, IPPROTO_UDP, NULL) <= 0)
482 * Attempt to start rpc.statd
484 int start_statd(void)
494 if (stat(START_STATD, &stb) == 0) {
495 if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {
507 * nfs_call_umount - ask the server to remove a share from it's rmtab
508 * @mnt_server: address of RPC MNT program server
509 * @argp: directory path of share to "unmount"
511 * Returns one if the unmount call succeeded; zero if the unmount
512 * failed for any reason.
514 * Note that a side effect of calling this function is that rpccreateerr
517 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
520 enum clnt_stat res = 0;
523 switch (mnt_server->pmap.pm_vers) {
527 if (!probe_mntport(mnt_server))
529 clnt = mnt_openclnt(mnt_server, &msock);
532 res = clnt_call(clnt, MOUNTPROC_UMNT,
533 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
534 (xdrproc_t)xdr_void, NULL,
536 mnt_closeclnt(clnt, msock);
537 if (res == RPC_SUCCESS)
545 if (res == RPC_SUCCESS)
550 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
552 struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
553 struct pmap *mnt_pmap = &mnt_server->pmap;
556 mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
557 *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, TRUE, FALSE);
558 if (*msock == RPC_ANYSOCK) {
559 if (rpc_createerr.cf_error.re_errno == EADDRINUSE)
561 * Probably in-use by a TIME_WAIT connection,
562 * It is worth waiting a while and trying again.
564 rpc_createerr.cf_stat = RPC_TIMEDOUT;
568 switch (mnt_pmap->pm_prot) {
570 clnt = clntudp_bufcreate(mnt_saddr,
571 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
572 RETRY_TIMEOUT, msock,
573 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
576 clnt = clnttcp_create(mnt_saddr,
577 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
579 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
583 /* try to mount hostname:dirname */
584 clnt->cl_auth = authunix_create_default();
590 void mnt_closeclnt(CLIENT *clnt, int msock)
592 auth_destroy(clnt->cl_auth);
598 * Sigh... getport() doesn't actually check the version number.
599 * In order to make sure that the server actually supports the service
600 * we're requesting, we open and RPC client, and fire off a NULL
603 int clnt_ping(struct sockaddr_in *saddr, const unsigned long prog,
604 const unsigned long vers, const unsigned int prot,
605 struct sockaddr_in *caddr)
609 static char clnt_res;
610 struct sockaddr dissolve;
612 rpc_createerr.cf_stat = stat = errno = 0;
613 sock = get_socket(saddr, prot, FALSE, TRUE);
614 if (sock == RPC_ANYSOCK) {
615 if (errno == ETIMEDOUT) {
617 * TCP timeout. Bubble up the error to see
618 * how it should be handled.
620 rpc_createerr.cf_stat = RPC_TIMEDOUT;
626 /* Get the address of our end of this connection */
627 socklen_t len = sizeof(*caddr);
628 if (getsockname(sock, caddr, &len) != 0)
629 caddr->sin_family = 0;
634 /* The socket is connected (so we could getsockname successfully),
635 * but some servers on multi-homed hosts reply from
636 * the wrong address, so if we stay connected, we lose the reply.
638 dissolve.sa_family = AF_UNSPEC;
639 connect(sock, &dissolve, sizeof(dissolve));
641 clnt = clntudp_bufcreate(saddr, prog, vers,
642 RETRY_TIMEOUT, &sock,
643 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
646 clnt = clnttcp_create(saddr, prog, vers, &sock,
647 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
654 memset(&clnt_res, 0, sizeof(clnt_res));
655 stat = clnt_call(clnt, NULLPROC,
656 (xdrproc_t)xdr_void, (caddr_t)NULL,
657 (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
660 clnt_geterr(clnt, &rpc_createerr.cf_error);
661 rpc_createerr.cf_stat = stat;
666 if (stat == RPC_SUCCESS)