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 "nfsumount.h"
42 #include "nfs_mount.h"
43 #include "mount_constants.h"
46 #ifdef HAVE_RPCSVC_NFS_PROT_H
47 #include <rpcsvc/nfs_prot.h>
49 #include <linux/nfs.h>
50 #define nfsstat nfs_stat
57 extern int nfs_mount_data_version;
58 extern char *progname;
61 static const unsigned int probe_udp_only[] = {
66 static const unsigned int probe_udp_first[] = {
72 static const unsigned int probe_tcp_first[] = {
78 static const unsigned long probe_nfs2_only[] = {
83 static const unsigned long probe_nfs3_first[] = {
89 static const unsigned long probe_mnt1_first[] = {
95 static const unsigned long probe_mnt3_first[] = {
102 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *saddr)
106 saddr->sin_family = AF_INET;
107 if (!inet_aton(hostname, &saddr->sin_addr)) {
108 if ((hp = gethostbyname(hostname)) == NULL) {
109 nfs_error(_("%s: can't get address for %s\n"),
113 if (hp->h_length > sizeof(*saddr)) {
114 nfs_error(_("%s: got bad hp->h_length\n"),
116 hp->h_length = sizeof(*saddr);
118 memcpy(&saddr->sin_addr, hp->h_addr, hp->h_length);
125 * getport() is very similar to pmap_getport() with
126 * the exception this version uses a non-reserve ports
127 * instead of reserve ports since reserve ports
128 * are not needed for pmap requests.
130 unsigned short getport(struct sockaddr_in *saddr, unsigned long prog,
131 unsigned long vers, unsigned int prot)
133 unsigned short port = 0;
139 saddr->sin_port = htons (PMAPPORT);
140 socket = get_socket(saddr, prot, FALSE, FALSE);
144 clnt = clntudp_bufcreate(saddr,
145 PMAPPROG, PMAPVERS, TIMEOUT, &socket,
146 UDPMSGSIZE, UDPMSGSIZE);
149 clnt = clnttcp_create(saddr,
150 PMAPPROG, PMAPVERS, &socket, 50, 500);
154 parms.pm_prog = prog;
155 parms.pm_vers = vers;
156 parms.pm_prot = prot;
157 parms.pm_port = 0; /* not needed or used */
159 stat = clnt_call(clnt, PMAPPROC_GETPORT, (xdrproc_t)xdr_pmap,
160 (caddr_t)&parms, (xdrproc_t)xdr_u_short, (caddr_t)&port, TIMEOUT);
162 clnt_geterr(clnt, &rpc_createerr.cf_error);
163 rpc_createerr.cf_stat = stat;
166 if (stat != RPC_SUCCESS)
169 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
178 * Use the portmapper to discover whether or not the service we want is
179 * available. The lists 'versions' and 'protos' define ordered sequences
180 * of service versions and udp/tcp protocols to probe for.
182 static int probe_port(clnt_addr_t *server, const unsigned long *versions,
183 const unsigned int *protos)
185 struct sockaddr_in *saddr = &server->saddr;
186 struct pmap *pmap = &server->pmap;
187 const unsigned long prog = pmap->pm_prog, *p_vers;
188 const unsigned int prot = (u_int)pmap->pm_prot, *p_prot;
189 const u_short port = (u_short) pmap->pm_port;
190 unsigned long vers = pmap->pm_vers;
191 unsigned short p_port;
193 p_prot = prot ? &prot : protos;
194 p_vers = vers ? &vers : versions;
195 rpc_createerr.cf_stat = 0;
197 saddr->sin_port = htons(PMAPPORT);
198 p_port = getport(saddr, prog, *p_vers, *p_prot);
200 if (!port || port == p_port) {
201 saddr->sin_port = htons(p_port);
203 printf(_("%s: trying %s prog %ld vers "
204 "%ld prot %s port %d\n"),
206 inet_ntoa(saddr->sin_addr),
208 *p_prot == IPPROTO_UDP ?
212 if (clnt_ping(saddr, prog, *p_vers, *p_prot, NULL))
214 if (rpc_createerr.cf_stat == RPC_TIMEDOUT)
218 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED)
226 if (vers == pmap->pm_vers) {
230 if (vers || !*++p_vers)
239 pmap->pm_vers = *p_vers;
241 pmap->pm_prot = *p_prot;
243 pmap->pm_port = p_port;
244 rpc_createerr.cf_stat = 0;
248 int probe_nfsport(clnt_addr_t *nfs_server)
250 struct pmap *pmap = &nfs_server->pmap;
252 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
255 if (nfs_mount_data_version >= 4)
256 return probe_port(nfs_server, probe_nfs3_first, probe_tcp_first);
258 return probe_port(nfs_server, probe_nfs2_only, probe_udp_only);
261 int probe_mntport(clnt_addr_t *mnt_server)
263 struct pmap *pmap = &mnt_server->pmap;
265 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
268 if (nfs_mount_data_version >= 4)
269 return probe_port(mnt_server, probe_mnt3_first, probe_udp_first);
271 return probe_port(mnt_server, probe_mnt1_first, probe_udp_only);
274 int probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
276 struct pmap *nfs_pmap = &nfs_server->pmap;
277 struct pmap *mnt_pmap = &mnt_server->pmap;
278 struct pmap save_nfs, save_mnt;
280 const unsigned long *probe_vers;
282 if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
283 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
284 else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
285 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
286 if (nfs_pmap->pm_vers)
289 memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
290 memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
291 probe_vers = (nfs_mount_data_version >= 4) ?
292 probe_mnt3_first : probe_mnt1_first;
294 for (; *probe_vers; probe_vers++) {
295 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
296 if ((res = probe_nfsport(nfs_server) != 0)) {
297 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
298 if ((res = probe_mntport(mnt_server)) != 0)
300 memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
302 switch (rpc_createerr.cf_stat) {
303 case RPC_PROGVERSMISMATCH:
304 case RPC_PROGNOTREGISTERED:
309 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
316 if (!probe_nfsport(nfs_server))
318 return probe_mntport(mnt_server);
321 static int probe_statd(void)
323 struct sockaddr_in addr;
326 memset(&addr, 0, sizeof(addr));
327 addr.sin_family = AF_INET;
328 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
329 port = getport(&addr, 100024, 1, IPPROTO_UDP);
333 addr.sin_port = htons(port);
335 if (clnt_ping(&addr, 100024, 1, IPPROTO_UDP, NULL) <= 0)
342 * Attempt to start rpc.statd
344 int start_statd(void)
354 if (stat(START_STATD, &stb) == 0) {
355 if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {