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(_("mount: can't get address for %s\n"),
113 if (hp->h_length > sizeof(*saddr)) {
114 nfs_error(_("mount: 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
125 * the exception this version uses a non-reserve ports
126 * instead of reserve ports since reserve ports
127 * are not needed for pmap requests.
129 unsigned short getport(struct sockaddr_in *saddr, unsigned long prog,
130 unsigned long vers, unsigned int prot)
132 unsigned short port = 0;
138 saddr->sin_port = htons (PMAPPORT);
139 socket = get_socket(saddr, prot, FALSE, FALSE);
143 clnt = clntudp_bufcreate(saddr,
144 PMAPPROG, PMAPVERS, TIMEOUT, &socket,
145 UDPMSGSIZE, UDPMSGSIZE);
148 clnt = clnttcp_create(saddr,
149 PMAPPROG, PMAPVERS, &socket, 50, 500);
153 parms.pm_prog = prog;
154 parms.pm_vers = vers;
155 parms.pm_prot = prot;
156 parms.pm_port = 0; /* not needed or used */
158 stat = clnt_call(clnt, PMAPPROC_GETPORT, (xdrproc_t)xdr_pmap,
159 (caddr_t)&parms, (xdrproc_t)xdr_u_short, (caddr_t)&port, TIMEOUT);
161 clnt_geterr(clnt, &rpc_createerr.cf_error);
162 rpc_createerr.cf_stat = stat;
165 if (stat != RPC_SUCCESS)
168 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
177 * Use the portmapper to discover whether or not the service we want is
178 * available. The lists 'versions' and 'protos' define ordered sequences
179 * of service versions and udp/tcp protocols to probe for.
181 static int probe_port(clnt_addr_t *server, const unsigned long *versions,
182 const unsigned int *protos)
184 struct sockaddr_in *saddr = &server->saddr;
185 struct pmap *pmap = &server->pmap;
186 const unsigned long prog = pmap->pm_prog, *p_vers;
187 const unsigned int prot = (u_int)pmap->pm_prot, *p_prot;
188 const u_short port = (u_short) pmap->pm_port;
189 unsigned long vers = pmap->pm_vers;
190 unsigned short p_port;
192 p_prot = prot ? &prot : protos;
193 p_vers = vers ? &vers : versions;
194 rpc_createerr.cf_stat = 0;
196 saddr->sin_port = htons(PMAPPORT);
197 p_port = getport(saddr, prog, *p_vers, *p_prot);
199 if (!port || port == p_port) {
200 saddr->sin_port = htons(p_port);
202 printf(_("%s: trying %s prog %ld vers "
203 "%ld prot %s port %d\n"),
205 inet_ntoa(saddr->sin_addr),
207 *p_prot == IPPROTO_UDP ?
211 if (clnt_ping(saddr, prog, *p_vers, *p_prot, NULL))
213 if (rpc_createerr.cf_stat == RPC_TIMEDOUT)
217 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED)
225 if (vers == pmap->pm_vers) {
229 if (vers || !*++p_vers)
238 pmap->pm_vers = *p_vers;
240 pmap->pm_prot = *p_prot;
242 pmap->pm_port = p_port;
243 rpc_createerr.cf_stat = 0;
247 int probe_nfsport(clnt_addr_t *nfs_server)
249 struct pmap *pmap = &nfs_server->pmap;
251 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
254 if (nfs_mount_data_version >= 4)
255 return probe_port(nfs_server, probe_nfs3_first, probe_tcp_first);
257 return probe_port(nfs_server, probe_nfs2_only, probe_udp_only);
260 int probe_mntport(clnt_addr_t *mnt_server)
262 struct pmap *pmap = &mnt_server->pmap;
264 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
267 if (nfs_mount_data_version >= 4)
268 return probe_port(mnt_server, probe_mnt3_first, probe_udp_first);
270 return probe_port(mnt_server, probe_mnt1_first, probe_udp_only);
273 int probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
275 struct pmap *nfs_pmap = &nfs_server->pmap;
276 struct pmap *mnt_pmap = &mnt_server->pmap;
277 struct pmap save_nfs, save_mnt;
279 const unsigned long *probe_vers;
281 if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
282 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
283 else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
284 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
285 if (nfs_pmap->pm_vers)
288 memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
289 memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
290 probe_vers = (nfs_mount_data_version >= 4) ?
291 probe_mnt3_first : probe_mnt1_first;
293 for (; *probe_vers; probe_vers++) {
294 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
295 if ((res = probe_nfsport(nfs_server) != 0)) {
296 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
297 if ((res = probe_mntport(mnt_server)) != 0)
299 memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
301 switch (rpc_createerr.cf_stat) {
302 case RPC_PROGVERSMISMATCH:
303 case RPC_PROGNOTREGISTERED:
308 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
315 if (!probe_nfsport(nfs_server))
317 return probe_mntport(mnt_server);
320 static int probe_statd(void)
322 struct sockaddr_in addr;
325 memset(&addr, 0, sizeof(addr));
326 addr.sin_family = AF_INET;
327 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
328 port = getport(&addr, 100024, 1, IPPROTO_UDP);
332 addr.sin_port = htons(port);
334 if (clnt_ping(&addr, 100024, 1, IPPROTO_UDP, NULL) <= 0)
341 * Attempt to start rpc.statd
343 int start_statd(void)
353 if (stat(START_STATD, &stb) == 0) {
354 if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {