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 $
41 * $DragonFly: src/sys/vfs/nfs/nfs_mountrpc.c,v 1.2 2006/12/23 00:41:29 swildner Exp $
44 * Procedures used by NFS_ROOT and BOOTP to do an NFS mount rpc to obtain
45 * the nfs root file handle for a NFS-based root mount point. This module
46 * is not used by normal operating code because the 'mount' command has a
47 * far more sophisticated implementation.
49 #include "opt_bootp.h"
50 #include "opt_nfsroot.h"
52 #if defined(BOOTP) || defined(NFS_ROOT)
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/kernel.h>
57 #include <sys/sockio.h>
59 #include <sys/malloc.h>
60 #include <sys/mount.h>
62 #include <sys/socket.h>
63 #include <sys/socketvar.h>
64 #include <sys/sysctl.h>
68 #include <net/route.h>
70 #include <netinet/in.h>
71 #include <net/if_types.h>
72 #include <net/if_dl.h>
77 #include "nfsdiskless.h"
80 #include "nfsmountrpc.h"
83 * What is the longest we will wait before re-sending a request?
84 * Note this is also the frequency of "RPC timeout" messages.
85 * The re-send loop count sup linearly to this maximum, so the
86 * first complaint will happen after (1+2+3+4+5)=15 seconds.
89 static int getdec(char **ptr);
90 static char *substr(char *a,char *b);
91 static int xdr_opaque_decode(struct mbuf **ptr, u_char *buf, int len);
92 static int xdr_int_decode(struct mbuf **ptr, int *iptr);
95 nfs_mountopts(struct nfs_args *args, char *p)
99 args->version = NFS_ARGSVERSION;
102 args->flags = NFSMNT_RSIZE | NFSMNT_WSIZE | NFSMNT_RESVPORT;
103 args->sotype = SOCK_STREAM;
106 if ((tmp = (char *)substr(p, "rsize=")))
107 args->rsize = getdec(&tmp);
108 if ((tmp = (char *)substr(p, "wsize=")))
109 args->wsize = getdec(&tmp);
110 if ((tmp = (char *)substr(p, "intr")))
111 args->flags |= NFSMNT_INT;
112 if ((tmp = (char *)substr(p, "soft")))
113 args->flags |= NFSMNT_SOFT;
114 if ((tmp = (char *)substr(p, "noconn")))
115 args->flags |= NFSMNT_NOCONN;
116 if ((tmp = (char *)substr(p, "udp")))
117 args->sotype = SOCK_DGRAM;
122 * Given a server pathname, get an NFS file handle.
123 * Also, sets sin->sin_port to the NFS service port.
126 md_mount(struct sockaddr_in *mdsin, /* mountd server address */
130 struct nfs_args *args,
139 /* First try NFS v3 */
140 /* Get port number for MOUNTD. */
141 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER3,
142 &mdsin->sin_port, td);
144 m = xdr_string_encode(path, strlen(path));
146 /* Do RPC to mountd. */
147 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER3,
148 RPCMNT_MOUNT, &m, NULL, td);
151 args->flags |= NFSMNT_NFSV3;
153 /* Fallback to NFS v2 */
155 /* Get port number for MOUNTD. */
156 error = krpc_portmap(mdsin, RPCPROG_MNT, RPCMNT_VER1,
157 &mdsin->sin_port, td);
161 m = xdr_string_encode(path, strlen(path));
163 /* Do RPC to mountd. */
164 error = krpc_call(mdsin, RPCPROG_MNT, RPCMNT_VER1,
165 RPCMNT_MOUNT, &m, NULL, td);
167 return error; /* message already freed */
168 args->flags &= ~NFSMNT_NFSV3;
171 if (xdr_int_decode(&m, &error) != 0 || error != 0)
174 if ((args->flags & NFSMNT_NFSV3) != 0) {
175 if (xdr_int_decode(&m, fhsizep) != 0 ||
176 *fhsizep > NFSX_V3FHMAX ||
180 *fhsizep = NFSX_V2FH;
182 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
185 if (args->flags & NFSMNT_NFSV3) {
186 if (xdr_int_decode(&m, &authcount) != 0)
189 if (authcount < 0 || authcount > 100)
191 while (authcount > 0) {
192 if (xdr_int_decode(&m, &authver) != 0)
194 if (authver == RPCAUTH_UNIX)
202 /* Set port number for NFS use. */
203 error = krpc_portmap(mdsin, NFS_PROG,
205 NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
206 &mdsin->sin_port, td);
219 md_lookup_swap(struct sockaddr_in *mdsin, /* mountd server address */
223 struct nfs_args *args,
236 m = m_get(MB_WAIT,MT_DATA);
240 if ((args->flags & NFSMNT_NFSV3) != 0) {
241 *mtod(m, u_int32_t *) = txdr_unsigned(*fhsizep);
242 bcopy(fhp, mtod(m, u_char *) + sizeof(u_int32_t), *fhsizep);
243 m->m_len = *fhsizep + sizeof(u_int32_t);
245 bcopy(fhp, mtod(m, u_char *), NFSX_V2FH);
246 m->m_len = NFSX_V2FH;
249 m->m_next = xdr_string_encode(path, strlen(path));
250 if (m->m_next == NULL) {
255 /* Do RPC to nfsd. */
256 if ((args->flags & NFSMNT_NFSV3) != 0)
257 error = krpc_call(mdsin, NFS_PROG, NFS_VER3,
258 NFSPROC_LOOKUP, &m, NULL, td);
260 error = krpc_call(mdsin, NFS_PROG, NFS_VER2,
261 NFSV2PROC_LOOKUP, &m, NULL, td);
263 return error; /* message already freed */
265 if (xdr_int_decode(&m, &status) != 0)
272 if ((args->flags & NFSMNT_NFSV3) != 0) {
273 if (xdr_int_decode(&m, fhsizep) != 0 ||
274 *fhsizep > NFSX_V3FHMAX ||
278 *fhsizep = NFSX_V2FH;
280 if (xdr_opaque_decode(&m, fhp, *fhsizep) != 0)
283 if ((args->flags & NFSMNT_NFSV3) != 0) {
284 if (xdr_int_decode(&m, &attribs_present) != 0)
286 if (attribs_present != 0) {
287 if (xdr_opaque_decode(&m, (u_char *) &fattribs.v3,
288 sizeof(u_int32_t) * 21) != 0)
290 size = fxdr_unsigned(u_int32_t, fattribs.v3[6]);
293 if (xdr_opaque_decode(&m,(u_char *) &fattribs.v2,
294 sizeof(u_int32_t) * 17) != 0)
296 size = fxdr_unsigned(u_int32_t, fattribs.v2[5]);
299 if (nfsv3_diskless.swap_nblks == 0 && size != -1) {
300 nfsv3_diskless.swap_nblks = size / 1024;
301 kprintf("md_lookup_swap: Swap size is %d KB\n",
302 nfsv3_diskless.swap_nblks);
316 setfs(struct sockaddr_in *addr, char *path, char *p)
322 if (((val = getdec(&p)) < 0) || (val > 255))
328 if (((val = getdec(&p)) < 0) || (val > 255))
334 if (((val = getdec(&p)) < 0) || (val > 255))
340 if (((val = getdec(&p)) < 0) || (val > 255))
347 addr->sin_addr.s_addr = htonl(ip);
348 addr->sin_len = sizeof(struct sockaddr_in);
349 addr->sin_family = AF_INET;
351 strncpy(path, p, MNAMELEN - 1);
363 if ((*p < '0') || (*p > '9'))
365 while ((*p >= '0') && (*p <= '9')) {
366 ret = ret * 10 + (*p - '0');
374 substr(char *a, char *b)
382 while (*loc1 == *loc2++) {
395 xdr_opaque_decode(struct mbuf **mptr, u_char *buf, int len)
401 alignedlen = ( len + 3 ) & ~3;
403 if (m->m_len < alignedlen) {
404 m = m_pullup(m, alignedlen);
410 bcopy(mtod(m, u_char *), buf, len);
411 m_adj(m, alignedlen);
417 xdr_int_decode(struct mbuf **mptr, int *iptr)
420 if (xdr_opaque_decode(mptr, (u_char *) &i, sizeof(u_int32_t)) != 0)
422 *iptr = fxdr_unsigned(u_int32_t, i);
426 #endif /* BOOTP && NFS_ROOT */