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 extern int nfs_mount_data_version;
57 extern char *progname;
60 static const unsigned int probe_udp_only[] = {
65 static const unsigned int probe_udp_first[] = {
71 static const unsigned int probe_tcp_first[] = {
77 static const unsigned long probe_nfs2_only[] = {
82 static const unsigned long probe_nfs3_first[] = {
88 static const unsigned long probe_mnt1_first[] = {
94 static const unsigned long probe_mnt3_first[] = {
101 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *saddr)
105 saddr->sin_family = AF_INET;
106 if (!inet_aton(hostname, &saddr->sin_addr)) {
107 if ((hp = gethostbyname(hostname)) == NULL) {
108 nfs_error(_("%s: can't get address for %s\n"),
112 if (hp->h_length > sizeof(*saddr)) {
113 nfs_error(_("%s: got bad hp->h_length\n"),
115 hp->h_length = sizeof(*saddr);
117 memcpy(&saddr->sin_addr, hp->h_addr, hp->h_length);
124 * getport() is very similar to pmap_getport() with the exception that
125 * this version tries to use an ephemeral port, since reserved ports are
126 * not needed for GETPORT queries. This conserves the very limited
127 * reserved port space, which helps reduce failed socket binds
128 * during mount storms.
130 * A side effect of calling this function is that rpccreateerr is set.
132 static unsigned short getport(struct sockaddr_in *saddr,
133 unsigned long program,
134 unsigned long version,
137 unsigned short port = 0;
142 saddr->sin_port = htons(PMAPPORT);
145 * Try to get a socket with a non-privileged port.
146 * clnt*create() will create one anyway if this
149 socket = get_socket(saddr, proto, FALSE, FALSE);
150 if (socket == RPC_ANYSOCK) {
151 if (proto == IPPROTO_TCP && errno == ETIMEDOUT) {
153 * TCP SYN timed out, so exit now.
155 rpc_createerr.cf_stat = RPC_TIMEDOUT;
162 clnt = clntudp_bufcreate(saddr,
164 RETRY_TIMEOUT, &socket,
169 clnt = clnttcp_create(saddr, PMAPPROG, PMAPVERS, &socket,
170 RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
174 struct pmap parms = {
180 stat = clnt_call(clnt, PMAPPROC_GETPORT,
181 (xdrproc_t)xdr_pmap, (caddr_t)&parms,
182 (xdrproc_t)xdr_u_short, (caddr_t)&port,
185 clnt_geterr(clnt, &rpc_createerr.cf_error);
186 rpc_createerr.cf_stat = stat;
189 if (stat != RPC_SUCCESS)
192 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
201 * Use the portmapper to discover whether or not the service we want is
202 * available. The lists 'versions' and 'protos' define ordered sequences
203 * of service versions and udp/tcp protocols to probe for.
205 static int probe_port(clnt_addr_t *server, const unsigned long *versions,
206 const unsigned int *protos)
208 struct sockaddr_in *saddr = &server->saddr;
209 struct pmap *pmap = &server->pmap;
210 const unsigned long prog = pmap->pm_prog, *p_vers;
211 const unsigned int prot = (u_int)pmap->pm_prot, *p_prot;
212 const u_short port = (u_short) pmap->pm_port;
213 unsigned long vers = pmap->pm_vers;
214 unsigned short p_port;
216 p_prot = prot ? &prot : protos;
217 p_vers = vers ? &vers : versions;
218 rpc_createerr.cf_stat = 0;
220 saddr->sin_port = htons(PMAPPORT);
221 p_port = getport(saddr, prog, *p_vers, *p_prot);
223 if (!port || port == p_port) {
224 saddr->sin_port = htons(p_port);
226 printf(_("%s: trying %s prog %ld vers "
227 "%ld prot %s port %d\n"),
229 inet_ntoa(saddr->sin_addr),
231 *p_prot == IPPROTO_UDP ?
235 if (clnt_ping(saddr, prog, *p_vers, *p_prot, NULL))
237 if (rpc_createerr.cf_stat == RPC_TIMEDOUT)
241 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED)
249 if (vers == pmap->pm_vers) {
253 if (vers || !*++p_vers)
262 pmap->pm_vers = *p_vers;
264 pmap->pm_prot = *p_prot;
266 pmap->pm_port = p_port;
267 rpc_createerr.cf_stat = 0;
271 static int probe_nfsport(clnt_addr_t *nfs_server)
273 struct pmap *pmap = &nfs_server->pmap;
275 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
278 if (nfs_mount_data_version >= 4)
279 return probe_port(nfs_server, probe_nfs3_first, probe_tcp_first);
281 return probe_port(nfs_server, probe_nfs2_only, probe_udp_only);
284 static int probe_mntport(clnt_addr_t *mnt_server)
286 struct pmap *pmap = &mnt_server->pmap;
288 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
291 if (nfs_mount_data_version >= 4)
292 return probe_port(mnt_server, probe_mnt3_first, probe_udp_first);
294 return probe_port(mnt_server, probe_mnt1_first, probe_udp_only);
297 int probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
299 struct pmap *nfs_pmap = &nfs_server->pmap;
300 struct pmap *mnt_pmap = &mnt_server->pmap;
301 struct pmap save_nfs, save_mnt;
303 const unsigned long *probe_vers;
305 if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
306 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
307 else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
308 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
309 if (nfs_pmap->pm_vers)
312 memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
313 memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
314 probe_vers = (nfs_mount_data_version >= 4) ?
315 probe_mnt3_first : probe_mnt1_first;
317 for (; *probe_vers; probe_vers++) {
318 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
319 if ((res = probe_nfsport(nfs_server) != 0)) {
320 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
321 if ((res = probe_mntport(mnt_server)) != 0)
323 memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
325 switch (rpc_createerr.cf_stat) {
326 case RPC_PROGVERSMISMATCH:
327 case RPC_PROGNOTREGISTERED:
332 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
339 if (!probe_nfsport(nfs_server))
341 return probe_mntport(mnt_server);
344 static int probe_statd(void)
346 struct sockaddr_in addr;
349 memset(&addr, 0, sizeof(addr));
350 addr.sin_family = AF_INET;
351 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
352 port = getport(&addr, 100024, 1, IPPROTO_UDP);
356 addr.sin_port = htons(port);
358 if (clnt_ping(&addr, 100024, 1, IPPROTO_UDP, NULL) <= 0)
365 * Attempt to start rpc.statd
367 int start_statd(void)
377 if (stat(START_STATD, &stb) == 0) {
378 if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {
390 * nfs_call_umount - ask the server to remove a share from it's rmtab
391 * @mnt_server: address of RPC MNT program server
392 * @argp: directory path of share to "unmount"
394 * Returns one if the unmount call succeeded; zero if the unmount
395 * failed for any reason.
397 * Note that a side effect of calling this function is that rpccreateerr
400 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
403 enum clnt_stat res = 0;
406 switch (mnt_server->pmap.pm_vers) {
410 if (!probe_mntport(mnt_server))
412 clnt = mnt_openclnt(mnt_server, &msock);
415 res = clnt_call(clnt, MOUNTPROC_UMNT,
416 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
417 (xdrproc_t)xdr_void, NULL,
419 mnt_closeclnt(clnt, msock);
420 if (res == RPC_SUCCESS)
428 if (res == RPC_SUCCESS)
433 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
435 struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
436 struct pmap *mnt_pmap = &mnt_server->pmap;
439 mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
440 *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, TRUE, FALSE);
441 if (*msock == RPC_ANYSOCK) {
442 if (rpc_createerr.cf_error.re_errno == EADDRINUSE)
444 * Probably in-use by a TIME_WAIT connection,
445 * It is worth waiting a while and trying again.
447 rpc_createerr.cf_stat = RPC_TIMEDOUT;
451 switch (mnt_pmap->pm_prot) {
453 clnt = clntudp_bufcreate(mnt_saddr,
454 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
455 RETRY_TIMEOUT, msock,
456 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
459 clnt = clnttcp_create(mnt_saddr,
460 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
462 MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
466 /* try to mount hostname:dirname */
467 clnt->cl_auth = authunix_create_default();
473 void mnt_closeclnt(CLIENT *clnt, int msock)
475 auth_destroy(clnt->cl_auth);