DragonFlyBSD Kernel Audit
sys/vfs/nfs/nfs_mountrpc.c
← back
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
 * Copyright (c) 1995 Gordon Ross, Adam Glass
 * Copyright (c) 1992 Regents of the University of California.
 * All rights reserved.
 *
 * This software was developed by the Computer Systems Engineering group
 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
 * contributed to Berkeley.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Lawrence Berkeley Laboratory and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * nfs/krpc_subr.c
 * $NetBSD: krpc_subr.c,v 1.10 1995/08/08 20:43:43 gwr Exp $
 * $FreeBSD: src/sys/nfs/bootp_subr.c,v 1.20.2.9 2003/04/24 16:51:08 ambrisko Exp $
 */
/*
 * Procedures used by NFS_ROOT and BOOTP to do an NFS mount rpc to obtain
 * the nfs root file handle for a NFS-based root mount point.  This module
 * is not used by normal operating code because the 'mount' command has a
 * far more sophisticated implementation.
 */
#include "opt_bootp.h"
#include "opt_nfsroot.h"

#if defined(BOOTP) || defined(NFS_ROOT)

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/sockio.h>
#include <sys/proc.h>
#include <sys/malloc.h>
#include <sys/mount.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/sysctl.h>
#include <sys/uio.h>

#include <net/if.h>
#include <net/route.h>

#include <netinet/in.h>
#include <net/if_types.h>
#include <net/if_dl.h>

#include "rpcv2.h"
#include "nfsproto.h"
#include "nfs.h"
#include "nfsdiskless.h"
#include "krpc.h"
#include "xdr_subs.h"
#include "nfsmountrpc.h"

/*
 * What is the longest we will wait before re-sending a request?
 * Note this is also the frequency of "RPC timeout" messages.
 * The re-send loop count sup linearly to this maximum, so the
 * first complaint will happen after (1+2+3+4+5)=15 seconds.
 */

static int getdec(char **ptr);
static char *substr(char *a,char *b);
static int xdr_opaque_decode(struct mbuf **ptr, u_char *buf, int len);
static int xdr_int_decode(struct mbuf **ptr, int *iptr);

void
nfs_mountopts(struct nfs_args *args, char *p)
{
	char *tmp;

	args->version = NFS_ARGSVERSION;
	args->rsize = 8192;
	args->wsize = 8192;
	args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
	args->sotype = SOCK_STREAM;
	if (p == NULL)
		return;
	if ((tmp = substr(p, "rsize=")))
		args->rsize = getdec(&tmp);
	if ((tmp = substr(p, "wsize=")))
		args->wsize = getdec(&tmp);
	if ((tmp = substr(p, "intr")))
		args->flags |= NFSMNT_INT;
	if ((tmp = substr(p, "soft")))
		args->flags |= NFSMNT_SOFT;
	if ((tmp = substr(p, "noconn")))
		args->flags |= NFSMNT_NOCONN;
	if ((tmp = substr(p, "udp")))
		args->sotype = SOCK_DGRAM;
}

/*
 * RPC: mountd/mount
 * Given a server pathname, get an NFS file handle.
 * Also, sets sin->sin_port to the NFS service port.
 */
int
md_mount(struct sockaddr_in *mdsin,		/* mountd server address */
	 char *path,
	 u_char *fhp,
	 int *fhsizep,
	 struct nfs_args *args,
	 struct thread *td)
{
	struct mbuf *m;
	int error;
	int authunixok;
	int authcount;
	int authver;

	/* First try NFS v3 */
	/* Get port number for MOUNTD. */
	error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
			     &mdsin->sin_port, td);
	if (error == 0) {
		m = xdr_string_encode(path, strlen(path));

		/* Do RPC to mountd. */
		error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
				  RPCMNT_MOUNT, &m, NULL, td);
	}
	if (error == 0) {
		args->flags |= NFSMNT_NFSV3;
	} else {
		/* Fallback to NFS v2 */

		/* Get port number for MOUNTD. */
		error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
				     &mdsin->sin_port, td);
		if (error != 0)
			return error;

		m = xdr_string_encode(path, strlen(path));

		/* Do RPC to mountd. */
		error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
				  RPCMNT_MOUNT, &m, NULL, td);
		if (error != 0)
			return error;	/* message already freed */
		args->flags &= ~NFSMNT_NFSV3;
	}

	if (xdr_int_decode(&m, &error) != 0 || error != 0)
		goto bad;

	if ((args->flags & NFSMNT_NFSV3) != 0) {
		if (xdr_int_decode(&m, fhsizep) != 0 ||
		    *fhsizep > NFSX_V3FHMAX ||
		    *fhsizep <= 0)
			goto bad;
	} else
		*fhsizep = NFSX_V2FH;

	if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
		goto bad;

	if (args->flags & NFSMNT_NFSV3) {
		if (xdr_int_decode(&m, &authcount) != 0)
			goto bad;
		authunixok = 0;
		if (authcount < 0 || authcount > 100)
			goto bad;
		while (authcount > 0) {
			if (xdr_int_decode(&m, &authver) != 0)
				goto bad;
			if (authver == RPCAUTH_UNIX)
				authunixok = 1;
			authcount--;
		}
		if (authunixok == 0)
			goto bad;
	}

	/* Set port number for NFS use. */
	error = krpc_portmap(mdsin, NFS_PROG,
			     (args->flags &
			      NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
			     &mdsin->sin_port, td);

	goto out;

bad:
	error = EBADRPC;

out:
	m_freem(m);
	return error;
}

int
md_lookup_swap(struct sockaddr_in *mdsin,	/* mountd server address */
	       char *path,
	       u_char *fhp,
	       int *fhsizep,
	       struct nfs_args *args,
	       struct thread *td)
{
	struct mbuf *m;
	int error;
	int size = -1;
	int attribs_present;
	int status;
	union {
		u_int32_t v2[17];
		u_int32_t v3[21];
	} fattribs;

	m = m_get(M_WAITOK, MT_DATA);
	if ((args->flags & NFSMNT_NFSV3) != 0) {
		*mtod(m, u_int32_t *) = txdr_unsigned(*fhsizep);
		bcopy(fhp, mtod(m, u_char *) + sizeof(u_int32_t), *fhsizep);
		m->m_len = *fhsizep + sizeof(u_int32_t);
	} else {
		bcopy(fhp, mtod(m, u_char *), NFSX_V2FH);
		m->m_len = NFSX_V2FH;
	}

	m->m_next = xdr_string_encode(path, strlen(path));
	if (m->m_next == NULL) {
		error = ENOBUFS;
		goto out;
	}

	/* Do RPC to nfsd. */
	if ((args->flags & NFSMNT_NFSV3) != 0)
		error = krpc_call(mdsin, NFS_PROG, NFS_VER3,
				  NFSPROC_LOOKUP, &m, NULL, td);
	else
		error = krpc_call(mdsin, NFS_PROG, NFS_VER2,
				  NFSV2PROC_LOOKUP, &m, NULL, td);
	if (error != 0)
		return error;	/* message already freed */

	if (xdr_int_decode(&m, &status) != 0)
		goto bad;
	if (status != 0) {
		error = ENOENT;
		goto out;
	}

	if ((args->flags & NFSMNT_NFSV3) != 0) {
		if (xdr_int_decode(&m, fhsizep) != 0 ||
		    *fhsizep > NFSX_V3FHMAX ||
		    *fhsizep <= 0)
			goto bad;
	} else
		*fhsizep = NFSX_V2FH;

	if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
		goto bad;

	if ((args->flags & NFSMNT_NFSV3) != 0) {
		if (xdr_int_decode(&m, &attribs_present) != 0)
			goto bad;
		if (attribs_present != 0) {
			if (xdr_opaque_decode(&m, (u_char *) &fattribs.v3,
					      sizeof(u_int32_t) * 21) != 0)
				goto bad;
			size = fxdr_unsigned(u_int32_t, fattribs.v3[6]);
		}
	} else {
		if (xdr_opaque_decode(&m,(u_char *) &fattribs.v2,
				      sizeof(u_int32_t) * 17) != 0)
			goto bad;
		size = fxdr_unsigned(u_int32_t, fattribs.v2[5]);
	}

	if (nfsv3_diskless.swap_nblks == 0 && size != -1) {
		nfsv3_diskless.swap_nblks = size / 1024;
		kprintf("md_lookup_swap: Swap size is %d KB\n",
		       nfsv3_diskless.swap_nblks);
	}

	goto out;

bad:
	error = EBADRPC;

out:
	m_freem(m);
	return error;
}

int
setfs(struct sockaddr_in *addr, char *path, char *p)
{
	unsigned int ip;
	int val;

	ip = 0;
	if (((val = getdec(&p)) < 0) || (val > 255))
		return 0;
	ip = val << 24;
	if (*p != '.')
		return 0;
	p++;
	if (((val = getdec(&p)) < 0) || (val > 255))
		return 0;
	ip |= (val << 16);
	if (*p != '.')
		return 0;
	p++;
	if (((val = getdec(&p)) < 0) || (val > 255))
		return 0;
	ip |= (val << 8);
	if (*p != '.')
		return 0;
	p++;
	if (((val = getdec(&p)) < 0) || (val > 255))
		return 0;
	ip |= val;
	if (*p != ':')
		return 0;
	p++;

	addr->sin_addr.s_addr = htonl(ip);
	addr->sin_len = sizeof(struct sockaddr_in);
	addr->sin_family = AF_INET;

	strncpy(path, p, MNAMELEN - 1);
	return 1;
}

static int
getdec(char **ptr)
{
	char *p;
	int ret;

	p = *ptr;
	ret = 0;
	if ((*p < '0') || (*p > '9'))
		return -1;
	while ((*p >= '0') && (*p <= '9')) {
		ret = ret * 10 + (*p - '0');
		p++;
	}
	*ptr = p;
	return ret;
}

static char *
substr(char *a, char *b)
{
	char *loc1;
	char *loc2;

        while (*a != '\0') {
                loc1 = a;
                loc2 = b;
                while (*loc1 == *loc2++) {
                        if (*loc1 == '\0')
				return 0;
                        loc1++;
                        if (*loc2 == '\0')
				return loc1;
                }
		a++;
        }
        return 0;
}

static int
xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
{
	struct mbuf *m;
	int alignedlen;

	m = *mptr;
	alignedlen = ( len + 3 ) & ~3;

	if (m->m_len < alignedlen) {
		m = m_pullup(m, alignedlen);
		if (m == NULL) {
			*mptr = NULL;
			return EBADRPC;
		}
	}
	bcopy(mtod(m, u_char *), buf, len);
	m_adj(m, alignedlen);
	*mptr = m;
	return 0;
}

static int
xdr_int_decode(struct mbuf **mptr, int *iptr)
{
	u_int32_t i;
	if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
		return EBADRPC;
	*iptr = fxdr_unsigned(u_int32_t, i);
	return 0;
}

#endif	/* BOOTP && NFS_ROOT */