2 * Copyright (c) 1995 Gordon Ross, Adam Glass
3 * Copyright (c) 1992 Regents of the University of California.
6 * This software was developed by the Computer Systems Engineering group
7 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
8 * contributed to Berkeley.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Lawrence Berkeley Laboratory and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * $NetBSD: krpc_subr.c,v 1.10 1995/08/08 20:43:43 gwr Exp $
40 * $FreeBSD: src/sys/nfs/bootp_subr.c,v 1.20.2.9 2003/04/24 16:51:08 ambrisko Exp $
43 * Procedures used by NFS_ROOT and BOOTP to do an NFS mount rpc to obtain
44 * the nfs root file handle for a NFS-based root mount point. This module
45 * is not used by normal operating code because the 'mount' command has a
46 * far more sophisticated implementation.
48 #include "opt_bootp.h"
49 #include "opt_nfsroot.h"
51 #if defined(BOOTP) || defined(NFS_ROOT)
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/sockio.h>
58 #include <sys/malloc.h>
59 #include <sys/mount.h>
61 #include <sys/socket.h>
62 #include <sys/socketvar.h>
63 #include <sys/sysctl.h>
67 #include <net/route.h>
69 #include <netinet/in.h>
70 #include <net/if_types.h>
71 #include <net/if_dl.h>
76 #include "nfsdiskless.h"
79 #include "nfsmountrpc.h"
82 * What is the longest we will wait before re-sending a request?
83 * Note this is also the frequency of "RPC timeout" messages.
84 * The re-send loop count sup linearly to this maximum, so the
85 * first complaint will happen after (1+2+3+4+5)=15 seconds.
88 static int getdec(char **ptr);
89 static char *substr(char *a,char *b);
90 static int xdr_opaque_decode(struct mbuf **ptr, u_char *buf, int len);
91 static int xdr_int_decode(struct mbuf **ptr, int *iptr);
94 nfs_mountopts(struct nfs_args *args, char *p)
98 args->version = NFS_ARGSVERSION;
101 args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
102 args->sotype = SOCK_STREAM;
105 if ((tmp = substr(p, "rsize=")))
106 args->rsize = getdec(&tmp);
107 if ((tmp = substr(p, "wsize=")))
108 args->wsize = getdec(&tmp);
109 if ((tmp = substr(p, "intr")))
110 args->flags |= NFSMNT_INT;
111 if ((tmp = substr(p, "soft")))
112 args->flags |= NFSMNT_SOFT;
113 if ((tmp = substr(p, "noconn")))
114 args->flags |= NFSMNT_NOCONN;
115 if ((tmp = substr(p, "udp")))
116 args->sotype = SOCK_DGRAM;
121 * Given a server pathname, get an NFS file handle.
122 * Also, sets sin->sin_port to the NFS service port.
125 md_mount(struct sockaddr_in *mdsin, /* mountd server address */
129 struct nfs_args *args,
138 /* First try NFS v3 */
139 /* Get port number for MOUNTD. */
140 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
141 &mdsin->sin_port, td);
143 m = xdr_string_encode(path, strlen(path));
145 /* Do RPC to mountd. */
146 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
147 RPCMNT_MOUNT, &m, NULL, td);
150 args->flags |= NFSMNT_NFSV3;
152 /* Fallback to NFS v2 */
154 /* Get port number for MOUNTD. */
155 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
156 &mdsin->sin_port, td);
160 m = xdr_string_encode(path, strlen(path));
162 /* Do RPC to mountd. */
163 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
164 RPCMNT_MOUNT, &m, NULL, td);
166 return error; /* message already freed */
167 args->flags &= ~NFSMNT_NFSV3;
170 if (xdr_int_decode(&m, &error) != 0 || error != 0)
173 if ((args->flags & NFSMNT_NFSV3) != 0) {
174 if (xdr_int_decode(&m, fhsizep) != 0 ||
175 *fhsizep > NFSX_V3FHMAX ||
179 *fhsizep = NFSX_V2FH;
181 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
184 if (args->flags & NFSMNT_NFSV3) {
185 if (xdr_int_decode(&m, &authcount) != 0)
188 if (authcount < 0 || authcount > 100)
190 while (authcount > 0) {
191 if (xdr_int_decode(&m, &authver) != 0)
193 if (authver == RPCAUTH_UNIX)
201 /* Set port number for NFS use. */
202 error = krpc_portmap(mdsin, NFS_PROG,
204 NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
205 &mdsin->sin_port, td);
218 md_lookup_swap(struct sockaddr_in *mdsin, /* mountd server address */
222 struct nfs_args *args,
235 m = m_get(M_WAITOK,MT_DATA);
239 if ((args->flags & NFSMNT_NFSV3) != 0) {
240 *mtod(m, u_int32_t *) = txdr_unsigned(*fhsizep);
241 bcopy(fhp, mtod(m, u_char *) + sizeof(u_int32_t), *fhsizep);
242 m->m_len = *fhsizep + sizeof(u_int32_t);
244 bcopy(fhp, mtod(m, u_char *), NFSX_V2FH);
245 m->m_len = NFSX_V2FH;
248 m->m_next = xdr_string_encode(path, strlen(path));
249 if (m->m_next == NULL) {
254 /* Do RPC to nfsd. */
255 if ((args->flags & NFSMNT_NFSV3) != 0)
256 error = krpc_call(mdsin, NFS_PROG, NFS_VER3,
257 NFSPROC_LOOKUP, &m, NULL, td);
259 error = krpc_call(mdsin, NFS_PROG, NFS_VER2,
260 NFSV2PROC_LOOKUP, &m, NULL, td);
262 return error; /* message already freed */
264 if (xdr_int_decode(&m, &status) != 0)
271 if ((args->flags & NFSMNT_NFSV3) != 0) {
272 if (xdr_int_decode(&m, fhsizep) != 0 ||
273 *fhsizep > NFSX_V3FHMAX ||
277 *fhsizep = NFSX_V2FH;
279 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
282 if ((args->flags & NFSMNT_NFSV3) != 0) {
283 if (xdr_int_decode(&m, &attribs_present) != 0)
285 if (attribs_present != 0) {
286 if (xdr_opaque_decode(&m, (u_char *) &fattribs.v3,
287 sizeof(u_int32_t) * 21) != 0)
289 size = fxdr_unsigned(u_int32_t, fattribs.v3[6]);
292 if (xdr_opaque_decode(&m,(u_char *) &fattribs.v2,
293 sizeof(u_int32_t) * 17) != 0)
295 size = fxdr_unsigned(u_int32_t, fattribs.v2[5]);
298 if (nfsv3_diskless.swap_nblks == 0 && size != -1) {
299 nfsv3_diskless.swap_nblks = size / 1024;
300 kprintf("md_lookup_swap: Swap size is %d KB\n",
301 nfsv3_diskless.swap_nblks);
315 setfs(struct sockaddr_in *addr, char *path, char *p)
321 if (((val = getdec(&p)) < 0) || (val > 255))
327 if (((val = getdec(&p)) < 0) || (val > 255))
333 if (((val = getdec(&p)) < 0) || (val > 255))
339 if (((val = getdec(&p)) < 0) || (val > 255))
346 addr->sin_addr.s_addr = htonl(ip);
347 addr->sin_len = sizeof(struct sockaddr_in);
348 addr->sin_family = AF_INET;
350 strncpy(path, p, MNAMELEN - 1);
362 if ((*p < '0') || (*p > '9'))
364 while ((*p >= '0') && (*p <= '9')) {
365 ret = ret * 10 + (*p - '0');
373 substr(char *a, char *b)
381 while (*loc1 == *loc2++) {
394 xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
400 alignedlen = ( len + 3 ) & ~3;
402 if (m->m_len < alignedlen) {
403 m = m_pullup(m, alignedlen);
409 bcopy(mtod(m, u_char *), buf, len);
410 m_adj(m, alignedlen);
416 xdr_int_decode(struct mbuf **mptr, int *iptr)
419 if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
421 *iptr = fxdr_unsigned(u_int32_t, i);
425 #endif /* BOOTP && NFS_ROOT */