]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/network.c
08b1f99f458f0baa894c98fc82bb152f2a69b028
[nfs-utils.git] / utils / mount / network.c
1 /*
2  * network.c -- Provide common network functions for NFS mount/umount
3  *
4  * Copyright (C) 2007 Oracle.  All rights reserved.
5  * Copyright (C) 2007 Chuck Lever <chuck.lever@oracle.com>
6  *
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.
11  *
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.
16  *
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.
21  *
22  */
23
24 #include <ctype.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <netdb.h>
31 #include <time.h>
32 #include <rpc/rpc.h>
33 #include <rpc/pmap_prot.h>
34 #include <rpc/pmap_clnt.h>
35 #include <sys/socket.h>
36
37 #include "conn.h"
38 #include "xcommon.h"
39 #include "mount.h"
40 #include "nls.h"
41 #include "nfs_mount.h"
42 #include "mount_constants.h"
43 #include "network.h"
44
45 #ifdef HAVE_RPCSVC_NFS_PROT_H
46 #include <rpcsvc/nfs_prot.h>
47 #else
48 #include <linux/nfs.h>
49 #define nfsstat nfs_stat
50 #endif
51
52 #ifndef NFS_PORT
53 #define NFS_PORT 2049
54 #endif
55
56 extern int nfs_mount_data_version;
57 extern char *progname;
58 extern int verbose;
59
60 static const unsigned int probe_udp_only[] = {
61         IPPROTO_UDP,
62         0,
63 };
64
65 static const unsigned int probe_udp_first[] = {
66         IPPROTO_UDP,
67         IPPROTO_TCP,
68         0,
69 };
70
71 static const unsigned int probe_tcp_first[] = {
72         IPPROTO_TCP,
73         IPPROTO_UDP,
74         0,
75 };
76
77 static const unsigned long probe_nfs2_only[] = {
78         2,
79         0,
80 };
81
82 static const unsigned long probe_nfs3_first[] = {
83         3,
84         2,
85         0,
86 };
87
88 static const unsigned long probe_mnt1_first[] = {
89         1,
90         2,
91         0,
92 };
93
94 static const unsigned long probe_mnt3_first[] = {
95         3,
96         1,
97         2,
98         0,
99 };
100
101 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *saddr)
102 {
103         struct hostent *hp;
104
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"),
109                                         progname, hostname);
110                         return 0;
111                 } else {
112                         if (hp->h_length > sizeof(*saddr)) {
113                                 nfs_error(_("%s: got bad hp->h_length\n"),
114                                                 progname);
115                                 hp->h_length = sizeof(*saddr);
116                         }
117                         memcpy(&saddr->sin_addr, hp->h_addr, hp->h_length);
118                 }
119         }
120         return 1;
121 }
122
123 /*
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.
129  *
130  * A side effect of calling this function is that rpccreateerr is set.
131  */
132 static unsigned short getport(struct sockaddr_in *saddr,
133                                 unsigned long program,
134                                 unsigned long version,
135                                 unsigned int proto)
136 {
137         unsigned short port = 0;
138         int socket;
139         CLIENT *clnt = NULL;
140         enum clnt_stat stat;
141
142         saddr->sin_port = htons(PMAPPORT);
143
144         /*
145          * Try to get a socket with a non-privileged port.
146          * clnt*create() will create one anyway if this
147          * fails.
148          */
149         socket = get_socket(saddr, proto, FALSE, FALSE);
150         if (socket == RPC_ANYSOCK) {
151                 if (proto == IPPROTO_TCP && errno == ETIMEDOUT) {
152                         /*
153                          * TCP SYN timed out, so exit now.
154                          */
155                         rpc_createerr.cf_stat = RPC_TIMEDOUT;
156                 }
157                 return 0;
158         }
159
160         switch (proto) {
161         case IPPROTO_UDP:
162                 clnt = clntudp_bufcreate(saddr,
163                                          PMAPPROG, PMAPVERS,
164                                          RETRY_TIMEOUT, &socket,
165                                          RPCSMALLMSGSIZE,
166                                          RPCSMALLMSGSIZE);
167                 break;
168         case IPPROTO_TCP:
169                 clnt = clnttcp_create(saddr, PMAPPROG, PMAPVERS, &socket,
170                                       RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
171                 break;
172         }
173         if (clnt != NULL) {
174                 struct pmap parms = {
175                         .pm_prog        = program,
176                         .pm_vers        = version,
177                         .pm_prot        = proto,
178                 };
179
180                 stat = clnt_call(clnt, PMAPPROC_GETPORT,
181                                  (xdrproc_t)xdr_pmap, (caddr_t)&parms,
182                                  (xdrproc_t)xdr_u_short, (caddr_t)&port,
183                                  TIMEOUT);
184                 if (stat) {
185                         clnt_geterr(clnt, &rpc_createerr.cf_error);
186                         rpc_createerr.cf_stat = stat;
187                 }
188                 clnt_destroy(clnt);
189                 if (stat != RPC_SUCCESS)
190                         port = 0;
191                 else if (port == 0)
192                         rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
193         }
194         if (socket != 1)
195                 close(socket);
196
197         return port;
198 }
199
200 /*
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.
204  */
205 static int probe_port(clnt_addr_t *server, const unsigned long *versions,
206                         const unsigned int *protos)
207 {
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;
215
216         p_prot = prot ? &prot : protos;
217         p_vers = vers ? &vers : versions;
218         rpc_createerr.cf_stat = 0;
219         for (;;) {
220                 saddr->sin_port = htons(PMAPPORT);
221                 p_port = getport(saddr, prog, *p_vers, *p_prot);
222                 if (p_port) {
223                         if (!port || port == p_port) {
224                                 saddr->sin_port = htons(p_port);
225                                 if (verbose) {
226                                         printf(_("%s: trying %s prog %ld vers "
227                                                 "%ld prot %s port %d\n"),
228                                                 progname,
229                                                 inet_ntoa(saddr->sin_addr),
230                                                 prog, *p_vers,
231                                                 *p_prot == IPPROTO_UDP ?
232                                                         "udp" : "tcp",
233                                                 p_port);
234                                 }
235                                 if (clnt_ping(saddr, prog, *p_vers, *p_prot, NULL))
236                                         goto out_ok;
237                                 if (rpc_createerr.cf_stat == RPC_TIMEDOUT)
238                                         goto out_bad;
239                         }
240                 }
241                 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED)
242                         goto out_bad;
243
244                 if (!prot) {
245                         if (*++p_prot)
246                                 continue;
247                         p_prot = protos;
248                 }
249                 if (vers == pmap->pm_vers) {
250                         p_vers = versions;
251                         vers = 0;
252                 }
253                 if (vers || !*++p_vers)
254                         break;
255         }
256
257 out_bad:
258         return 0;
259
260 out_ok:
261         if (!vers)
262                 pmap->pm_vers = *p_vers;
263         if (!prot)
264                 pmap->pm_prot = *p_prot;
265         if (!port)
266                 pmap->pm_port = p_port;
267         rpc_createerr.cf_stat = 0;
268         return 1;
269 }
270
271 static int probe_nfsport(clnt_addr_t *nfs_server)
272 {
273         struct pmap *pmap = &nfs_server->pmap;
274
275         if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
276                 return 1;
277
278         if (nfs_mount_data_version >= 4)
279                 return probe_port(nfs_server, probe_nfs3_first, probe_tcp_first);
280         else
281                 return probe_port(nfs_server, probe_nfs2_only, probe_udp_only);
282 }
283
284 static int probe_mntport(clnt_addr_t *mnt_server)
285 {
286         struct pmap *pmap = &mnt_server->pmap;
287
288         if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
289                 return 1;
290
291         if (nfs_mount_data_version >= 4)
292                 return probe_port(mnt_server, probe_mnt3_first, probe_udp_first);
293         else
294                 return probe_port(mnt_server, probe_mnt1_first, probe_udp_only);
295 }
296
297 int probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
298 {
299         struct pmap *nfs_pmap = &nfs_server->pmap;
300         struct pmap *mnt_pmap = &mnt_server->pmap;
301         struct pmap save_nfs, save_mnt;
302         int res;
303         const unsigned long *probe_vers;
304
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)
310                 goto version_fixed;
311
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;
316
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)
322                                 return 1;
323                         memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
324                 }
325                 switch (rpc_createerr.cf_stat) {
326                 case RPC_PROGVERSMISMATCH:
327                 case RPC_PROGNOTREGISTERED:
328                         break;
329                 default:
330                         goto out_bad;
331                 }
332                 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
333         }
334
335 out_bad:
336         return 0;
337
338 version_fixed:
339         if (!probe_nfsport(nfs_server))
340                 goto out_bad;
341         return probe_mntport(mnt_server);
342 }
343
344 static int probe_statd(void)
345 {
346         struct sockaddr_in addr;
347         unsigned short port;
348
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);
353
354         if (port == 0)
355                 return 0;
356         addr.sin_port = htons(port);
357
358         if (clnt_ping(&addr, 100024, 1, IPPROTO_UDP, NULL) <= 0)
359                 return 0;
360
361         return 1;
362 }
363
364 /*
365  * Attempt to start rpc.statd
366  */
367 int start_statd(void)
368 {
369 #ifdef START_STATD
370         struct stat stb;
371 #endif
372
373         if (probe_statd())
374                 return 1;
375
376 #ifdef START_STATD
377         if (stat(START_STATD, &stb) == 0) {
378                 if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {
379                         system(START_STATD);
380                         if (probe_statd())
381                                 return 1;
382                 }
383         }
384 #endif
385
386         return 0;
387 }
388
389 /*
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"
393  *
394  * Returns one if the unmount call succeeded; zero if the unmount
395  * failed for any reason.
396  *
397  * Note that a side effect of calling this function is that rpccreateerr
398  * is set.
399  */
400 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
401 {
402         CLIENT *clnt;
403         enum clnt_stat res = 0;
404         int msock;
405
406         switch (mnt_server->pmap.pm_vers) {
407         case 3:
408         case 2:
409         case 1:
410                 if (!probe_mntport(mnt_server))
411                         return 0;
412                 clnt = mnt_openclnt(mnt_server, &msock);
413                 if (!clnt)
414                         return 0;
415                 res = clnt_call(clnt, MOUNTPROC_UMNT,
416                                 (xdrproc_t)xdr_dirpath, (caddr_t)argp,
417                                 (xdrproc_t)xdr_void, NULL,
418                                 TIMEOUT);
419                 mnt_closeclnt(clnt, msock);
420                 if (res == RPC_SUCCESS)
421                         return 1;
422                 break;
423         default:
424                 res = RPC_SUCCESS;
425                 break;
426         }
427
428         if (res == RPC_SUCCESS)
429                 return 1;
430         return 0;
431 }
432
433 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
434 {
435         struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
436         struct pmap *mnt_pmap = &mnt_server->pmap;
437         CLIENT *clnt = NULL;
438
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)
443                         /*
444                          * Probably in-use by a TIME_WAIT connection,
445                          * It is worth waiting a while and trying again.
446                          */
447                         rpc_createerr.cf_stat = RPC_TIMEDOUT;
448                 return NULL;
449         }
450
451         switch (mnt_pmap->pm_prot) {
452         case IPPROTO_UDP:
453                 clnt = clntudp_bufcreate(mnt_saddr,
454                                          mnt_pmap->pm_prog, mnt_pmap->pm_vers,
455                                          RETRY_TIMEOUT, msock,
456                                          MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
457                 break;
458         case IPPROTO_TCP:
459                 clnt = clnttcp_create(mnt_saddr,
460                                       mnt_pmap->pm_prog, mnt_pmap->pm_vers,
461                                       msock,
462                                       MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
463                 break;
464         }
465         if (clnt) {
466                 /* try to mount hostname:dirname */
467                 clnt->cl_auth = authunix_create_default();
468                 return clnt;
469         }
470         return NULL;
471 }
472
473 void mnt_closeclnt(CLIENT *clnt, int msock)
474 {
475         auth_destroy(clnt->cl_auth);
476         clnt_destroy(clnt);
477         close(msock);
478 }