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 #if SIZEOF_SOCKLEN_T - 0 == 0
56 #define socklen_t unsigned int
59 extern int nfs_mount_data_version;
60 extern char *progname;
63 static const unsigned long nfs_to_mnt[] = {
70 static const unsigned long mnt_to_nfs[] = {
78 * Map an NFS version into the corresponding Mountd version
80 unsigned long nfsvers_to_mnt(const unsigned long vers)
83 return nfs_to_mnt[vers];
88 * Map a Mountd version into the corresponding NFS version
90 static unsigned long mntvers_to_nfs(const unsigned long vers)
93 return mnt_to_nfs[vers];
97 static const unsigned int probe_udp_only[] = {
102 static const unsigned int probe_udp_first[] = {
108 static const unsigned int probe_tcp_first[] = {
114 static const unsigned long probe_nfs2_only[] = {
119 static const unsigned long probe_nfs3_first[] = {
125 static const unsigned long probe_mnt1_first[] = {
131 static const unsigned long probe_mnt3_first[] = {
138 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *saddr)
142 saddr->sin_family = AF_INET;
143 if (!inet_aton(hostname, &saddr->sin_addr)) {
144 if ((hp = gethostbyname(hostname)) == NULL) {
145 nfs_error(_("%s: can't get address for %s\n"),
149 if (hp->h_length > sizeof(*saddr)) {
150 nfs_error(_("%s: got bad hp->h_length\n"),
152 hp->h_length = sizeof(*saddr);
154 memcpy(&saddr->sin_addr, hp->h_addr, hp->h_length);
161 * Create a socket that is locally bound to a reserved or non-reserved
162 * port. For any failures, RPC_ANYSOCK is returned which will cause
163 * the RPC code to create the socket instead.
165 static int get_socket(struct sockaddr_in *saddr, unsigned int p_prot,
169 struct sockaddr_in laddr;
170 socklen_t namelen = sizeof(laddr);
172 type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
173 if ((so = socket (AF_INET, type, p_prot)) < 0)
176 laddr.sin_family = AF_INET;
178 laddr.sin_addr.s_addr = htonl(INADDR_ANY);
180 if (bindresvport(so, &laddr) < 0)
181 goto err_bindresvport;
183 cc = bind(so, (struct sockaddr *)&laddr, namelen);
187 if (type == SOCK_STREAM || (conn && type == SOCK_DGRAM)) {
188 cc = connect(so, (struct sockaddr *)saddr, namelen);
195 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
196 rpc_createerr.cf_error.re_errno = errno;
198 nfs_error(_("%s: Unable to create %s socket: errno %d (%s)\n"),
199 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
200 errno, strerror(errno));
205 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
206 rpc_createerr.cf_error.re_errno = errno;
208 nfs_error(_("%s: Unable to bindresvport %s socket: errno %d"
210 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
211 errno, strerror(errno));
217 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
218 rpc_createerr.cf_error.re_errno = errno;
220 nfs_error(_("%s: Unable to bind to %s socket: errno %d (%s)\n"),
221 progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
222 errno, strerror(errno));
228 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
229 rpc_createerr.cf_error.re_errno = errno;
231 nfs_error(_("%s: Unable to connect to %s:%d, errno %d (%s)\n"),
232 progname, inet_ntoa(saddr->sin_addr),
233 ntohs(saddr->sin_port), errno, strerror(errno));
240 * getport() is very similar to pmap_getport() with the exception that
241 * this version tries to use an ephemeral port, since reserved ports are
242 * not needed for GETPORT queries. This conserves the very limited
243 * reserved port space, which helps reduce failed socket binds
244 * during mount storms.
246 * A side effect of calling this function is that rpccreateerr is set.
248 static unsigned short getport(struct sockaddr_in *saddr,
249 unsigned long program,
250 unsigned long version,
253 unsigned short port = 0;
258 saddr->sin_port = htons(PMAPPORT);
261 * Try to get a socket with a non-privileged port.
262 * clnt*create() will create one anyway if this
265 socket = get_socket(saddr, proto, FALSE, FALSE);
266 if (socket == RPC_ANYSOCK) {
267 if (proto == IPPROTO_TCP && errno == ETIMEDOUT) {
269 * TCP SYN timed out, so exit now.
271 rpc_createerr.cf_stat = RPC_TIMEDOUT;
278 clnt = clntudp_bufcreate(saddr,
280 RETRY_TIMEOUT, &socket,
285 clnt = clnttcp_create(saddr, PMAPPROG, PMAPVERS, &socket,
286 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
290 struct pmap parms = {
296 stat = clnt_call(clnt, PMAPPROC_GETPORT,
297 (xdrproc_t)xdr_pmap, (caddr_t)&parms,
298 (xdrproc_t)xdr_u_short, (caddr_t)&port,
301 clnt_geterr(clnt, &rpc_createerr.cf_error);
302 rpc_createerr.cf_stat = stat;
305 if (stat != RPC_SUCCESS)
308 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
317 * Use the portmapper to discover whether or not the service we want is
318 * available. The lists 'versions' and 'protos' define ordered sequences
319 * of service versions and udp/tcp protocols to probe for.
321 static int probe_port(clnt_addr_t *server, const unsigned long *versions,
322 const unsigned int *protos)
324 struct sockaddr_in *saddr = &server->saddr;
325 struct pmap *pmap = &server->pmap;
326 const unsigned long prog = pmap->pm_prog, *p_vers;
327 const unsigned int prot = (u_int)pmap->pm_prot, *p_prot;
328 const u_short port = (u_short) pmap->pm_port;
329 unsigned long vers = pmap->pm_vers;
330 unsigned short p_port;
332 p_prot = prot ? &prot : protos;
333 p_vers = vers ? &vers : versions;
334 rpc_createerr.cf_stat = 0;
336 saddr->sin_port = htons(PMAPPORT);
337 p_port = getport(saddr, prog, *p_vers, *p_prot);
339 if (!port || port == p_port) {
340 saddr->sin_port = htons(p_port);
342 printf(_("%s: trying %s prog %ld vers "
343 "%ld prot %s port %d\n"),
345 inet_ntoa(saddr->sin_addr),
347 *p_prot == IPPROTO_UDP ?
351 if (clnt_ping(saddr, prog, *p_vers, *p_prot, NULL))
353 if (rpc_createerr.cf_stat == RPC_TIMEDOUT)
357 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED)
365 if (vers == pmap->pm_vers) {
369 if (vers || !*++p_vers)
378 pmap->pm_vers = *p_vers;
380 pmap->pm_prot = *p_prot;
382 pmap->pm_port = p_port;
383 rpc_createerr.cf_stat = 0;
387 static int probe_nfsport(clnt_addr_t *nfs_server)
389 struct pmap *pmap = &nfs_server->pmap;
391 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
394 if (nfs_mount_data_version >= 4)
395 return probe_port(nfs_server, probe_nfs3_first, probe_tcp_first);
397 return probe_port(nfs_server, probe_nfs2_only, probe_udp_only);
400 static int probe_mntport(clnt_addr_t *mnt_server)
402 struct pmap *pmap = &mnt_server->pmap;
404 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
407 if (nfs_mount_data_version >= 4)
408 return probe_port(mnt_server, probe_mnt3_first, probe_udp_first);
410 return probe_port(mnt_server, probe_mnt1_first, probe_udp_only);
413 int probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
415 struct pmap *nfs_pmap = &nfs_server->pmap;
416 struct pmap *mnt_pmap = &mnt_server->pmap;
417 struct pmap save_nfs, save_mnt;
419 const unsigned long *probe_vers;
421 if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
422 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
423 else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
424 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
425 if (nfs_pmap->pm_vers)
428 memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
429 memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
430 probe_vers = (nfs_mount_data_version >= 4) ?
431 probe_mnt3_first : probe_mnt1_first;
433 for (; *probe_vers; probe_vers++) {
434 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
435 if ((res = probe_nfsport(nfs_server) != 0)) {
436 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
437 if ((res = probe_mntport(mnt_server)) != 0)
439 memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
441 switch (rpc_createerr.cf_stat) {
442 case RPC_PROGVERSMISMATCH:
443 case RPC_PROGNOTREGISTERED:
448 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
455 if (!probe_nfsport(nfs_server))
457 return probe_mntport(mnt_server);
460 static int probe_statd(void)
462 struct sockaddr_in addr;
465 memset(&addr, 0, sizeof(addr));
466 addr.sin_family = AF_INET;
467 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
468 port = getport(&addr, 100024, 1, IPPROTO_UDP);
472 addr.sin_port = htons(port);
474 if (clnt_ping(&addr, 100024, 1, IPPROTO_UDP, NULL) <= 0)
481 * Attempt to start rpc.statd
483 int start_statd(void)
493 if (stat(START_STATD, &stb) == 0) {
494 if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {
506 * nfs_call_umount - ask the server to remove a share from it's rmtab
507 * @mnt_server: address of RPC MNT program server
508 * @argp: directory path of share to "unmount"
510 * Returns one if the unmount call succeeded; zero if the unmount
511 * failed for any reason.
513 * Note that a side effect of calling this function is that rpccreateerr
516 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
519 enum clnt_stat res = 0;
522 switch (mnt_server->pmap.pm_vers) {
526 if (!probe_mntport(mnt_server))
528 clnt = mnt_openclnt(mnt_server, &msock);
531 res = clnt_call(clnt, MOUNTPROC_UMNT,
532 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
533 (xdrproc_t)xdr_void, NULL,
535 mnt_closeclnt(clnt, msock);
536 if (res == RPC_SUCCESS)
544 if (res == RPC_SUCCESS)
549 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
551 struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
552 struct pmap *mnt_pmap = &mnt_server->pmap;
555 mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
556 *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, TRUE, FALSE);
557 if (*msock == RPC_ANYSOCK) {
558 if (rpc_createerr.cf_error.re_errno == EADDRINUSE)
560 * Probably in-use by a TIME_WAIT connection,
561 * It is worth waiting a while and trying again.
563 rpc_createerr.cf_stat = RPC_TIMEDOUT;
567 switch (mnt_pmap->pm_prot) {
569 clnt = clntudp_bufcreate(mnt_saddr,
570 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
571 RETRY_TIMEOUT, msock,
572 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
575 clnt = clnttcp_create(mnt_saddr,
576 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
578 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
582 /* try to mount hostname:dirname */
583 clnt->cl_auth = authunix_create_default();
589 void mnt_closeclnt(CLIENT *clnt, int msock)
591 auth_destroy(clnt->cl_auth);
597 * Sigh... getport() doesn't actually check the version number.
598 * In order to make sure that the server actually supports the service
599 * we're requesting, we open and RPC client, and fire off a NULL
602 int clnt_ping(struct sockaddr_in *saddr, const unsigned long prog,
603 const unsigned long vers, const unsigned int prot,
604 struct sockaddr_in *caddr)
608 static char clnt_res;
609 struct sockaddr dissolve;
611 rpc_createerr.cf_stat = stat = errno = 0;
612 sock = get_socket(saddr, prot, FALSE, TRUE);
613 if (sock == RPC_ANYSOCK) {
614 if (errno == ETIMEDOUT) {
616 * TCP timeout. Bubble up the error to see
617 * how it should be handled.
619 rpc_createerr.cf_stat = RPC_TIMEDOUT;
625 /* Get the address of our end of this connection */
626 socklen_t len = sizeof(*caddr);
627 if (getsockname(sock, caddr, &len) != 0)
628 caddr->sin_family = 0;
633 /* The socket is connected (so we could getsockname successfully),
634 * but some servers on multi-homed hosts reply from
635 * the wrong address, so if we stay connected, we lose the reply.
637 dissolve.sa_family = AF_UNSPEC;
638 connect(sock, &dissolve, sizeof(dissolve));
640 clnt = clntudp_bufcreate(saddr, prog, vers,
641 RETRY_TIMEOUT, &sock,
642 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
645 clnt = clnttcp_create(saddr, prog, vers, &sock,
646 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
653 memset(&clnt_res, 0, sizeof(clnt_res));
654 stat = clnt_call(clnt, NULLPROC,
655 (xdrproc_t)xdr_void, (caddr_t)NULL,
656 (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
659 clnt_geterr(clnt, &rpc_createerr.cf_error);
660 rpc_createerr.cf_stat = stat;
665 if (stat == RPC_SUCCESS)