nfs_boot.c revision 1.37 1 /* $NetBSD: nfs_boot.c,v 1.37 1997/09/09 21:39:17 gwr Exp $ */
2
3 /*-
4 * Copyright (c) 1995, 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Adam Glass and Gordon W. Ross.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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 NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Support for NFS diskless booting, specifically getting information
41 * about where to mount root from, what pathnames, etc.
42 */
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/conf.h>
48 #include <sys/device.h>
49 #include <sys/ioctl.h>
50 #include <sys/proc.h>
51 #include <sys/mount.h>
52 #include <sys/mbuf.h>
53 #include <sys/reboot.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56
57 #include <net/if.h>
58 #include <net/route.h>
59 #include <net/if_ether.h>
60 #include <net/if_types.h>
61
62 #include <netinet/in.h>
63 #include <netinet/if_inarp.h>
64
65 #include <nfs/rpcv2.h>
66 #include <nfs/krpc.h>
67 #include <nfs/xdr_subs.h>
68
69 #include <nfs/nfsproto.h>
70 #include <nfs/nfsdiskless.h>
71
72 #include "arp.h"
73 #if NARP == 0
74
75 int nfs_boot_init(nd, procp)
76 struct nfs_diskless *nd;
77 struct proc *procp;
78 {
79 printf("nfs_boot: NARP == 0\n");
80 return (ENXIO);
81 }
82
83 #else /* NARP */
84
85 /*
86 * There are two implementations of NFS diskless boot.
87 * One implementation uses BOOTP (RFC951, RFC1048),
88 * the other uses Sun RPC/bootparams. See the files:
89 * nfs_bootp.c: BOOTP (RFC951, RFC1048)
90 * nfs_bootsun.c: Sun RPC/bootparams
91 *
92 * The variable nfs_boot_rfc951 selects which to use.
93 * This is defined as BSS so machine-dependent code
94 * may provide a data definition to override this.
95 */
96 int nfs_boot_rfc951; /* 0: BOOTP. 1: RARP/SUNRPC */
97
98 /* mountd RPC */
99 static int md_mount __P((struct sockaddr_in *mdsin, char *path,
100 struct nfs_args *argp));
101
102 static void nfs_boot_defrt __P((struct in_addr *));
103 static int nfs_boot_getfh __P((struct nfs_dlmount *ndm));
104
105
106 /*
107 * Called with an empty nfs_diskless struct to be filled in.
108 * Find an interface, determine its ip address (etc.) and
109 * save all the boot parameters in the nfs_diskless struct.
110 */
111 int
112 nfs_boot_init(nd, procp)
113 struct nfs_diskless *nd;
114 struct proc *procp;
115 {
116 struct ifnet *ifp;
117 int error;
118
119 /*
120 * Find the network interface.
121 */
122 ifp = ifunit(root_device->dv_xname);
123 if (ifp == NULL) {
124 printf("nfs_boot: '%s' not found\n",
125 root_device->dv_xname);
126 return (ENXIO);
127 }
128 if (ifp->if_type != IFT_ETHER) {
129 printf("nfs_boot: unknown I/F type %d\n", ifp->if_type);
130 return (ENODEV); /* Op. not supported by device */
131 }
132
133 if (nfs_boot_rfc951) {
134 printf("nfs_boot: trying BOOTP/DHCP\n");
135 error = nfs_bootdhcp(ifp, nd, procp);
136 } else {
137 printf("nfs_boot: trying RARP (and RPC/bootparam)\n");
138 error = nfs_bootparam(ifp, nd, procp);
139 }
140 if (error)
141 return (error);
142
143 /*
144 * If the gateway address is set, add a default route.
145 * (The mountd RPCs may go across a gateway.)
146 */
147 if (nd->nd_gwip.s_addr)
148 nfs_boot_defrt(&nd->nd_gwip);
149
150 /*
151 * Now fetch the NFS file handles as appropriate.
152 */
153 error = nfs_boot_getfh(&nd->nd_root);
154 if (error)
155 return (error);
156
157 #if 0 /* swap now comes in from swapctl(2) */
158 if (nd->nd_swap.ndm_saddr.sa_len) {
159 error = nfs_boot_getfh(&nd->nd_swap);
160 if (error) {
161 printf("nfs_boot: warning: getfh(swap), error=%d\n", error);
162 /* Just ignore the error */
163 error = 0;
164 }
165 }
166 #endif
167
168 return (error);
169 }
170
171 /*
172 * Install a default route to the passed IP address.
173 */
174 static void
175 nfs_boot_defrt(gw_ip)
176 struct in_addr *gw_ip;
177 {
178 struct sockaddr dst, gw, mask;
179 struct sockaddr_in *sin;
180 int error;
181
182 /* Destination: (default) */
183 bzero((caddr_t)&dst, sizeof(dst));
184 dst.sa_len = sizeof(dst);
185 dst.sa_family = AF_INET;
186 /* Gateway: */
187 bzero((caddr_t)&gw, sizeof(gw));
188 sin = (struct sockaddr_in *)&gw;
189 sin->sin_len = sizeof(*sin);
190 sin->sin_family = AF_INET;
191 sin->sin_addr.s_addr = gw_ip->s_addr;
192 /* Mask: (zero length) */
193 /* XXX - Just pass a null pointer? */
194 bzero(&mask, sizeof(mask));
195
196 /* add, dest, gw, mask, flags, 0 */
197 error = rtrequest(RTM_ADD, &dst, &gw, &mask,
198 (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
199 if (error) {
200 printf("nfs_boot: add route, error=%d\n", error);
201 error = 0;
202 }
203 }
204
205 /*
206 * Get an initial NFS file handle using Sun RPC/mountd.
207 * Separate function because we used to call it twice.
208 * (once for root and once for swap)
209 */
210 static int
211 nfs_boot_getfh(ndm)
212 struct nfs_dlmount *ndm; /* output */
213 {
214 struct nfs_args *args;
215 struct sockaddr_in *sin;
216 char *pathname;
217 int error;
218 u_int16_t port;
219
220 args = &ndm->ndm_args;
221
222 /* Initialize mount args. */
223 bzero((caddr_t) args, sizeof(*args));
224 args->addr = &ndm->ndm_saddr;
225 args->addrlen = args->addr->sa_len;
226 #ifdef NFS_BOOT_TCP
227 args->sotype = SOCK_STREAM;
228 #else
229 args->sotype = SOCK_DGRAM;
230 #endif
231 args->fh = ndm->ndm_fh;
232 args->hostname = ndm->ndm_host;
233 args->flags = NFSMNT_RESVPORT | NFSMNT_NFSV3;
234
235 #ifdef NFS_BOOT_OPTIONS
236 args->flags |= NFS_BOOT_OPTIONS;
237 #endif
238 #ifdef NFS_BOOT_RWSIZE
239 /*
240 * Reduce rsize,wsize for interfaces that consistently
241 * drop fragments of long UDP messages. (i.e. wd8003).
242 * You can always change these later via remount.
243 */
244 args->flags |= NFSMNT_WSIZE | NFSMNT_RSIZE;
245 args->wsize = NFS_BOOT_RWSIZE;
246 args->rsize = NFS_BOOT_RWSIZE;
247 #endif
248
249 /*
250 * Find the pathname part of the "server:pathname"
251 * string left in ndm->ndm_host by nfs_boot_init.
252 */
253 pathname = strchr(ndm->ndm_host, ':');
254 if (pathname == 0) {
255 printf("nfs_boot: getfh - no pathname\n");
256 return (EIO);
257 }
258 pathname++;
259
260 /*
261 * Get file handle using RPC to mountd/mount
262 */
263 sin = (struct sockaddr_in *)&ndm->ndm_saddr;
264 error = md_mount(sin, pathname, args);
265 if (error) {
266 printf("nfs_boot: mountd `%s', error=%d\n",
267 ndm->ndm_host, error);
268 return (error);
269 }
270
271 /* Set port number for NFS use. */
272 /* XXX: NFS port is always 2049, right? */
273 #ifdef NFS_BOOT_TCP
274 retry:
275 #endif
276 error = krpc_portmap(sin, NFS_PROG,
277 (args->flags & NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
278 (args->sotype == SOCK_STREAM) ? IPPROTO_TCP : IPPROTO_UDP,
279 &port);
280 if (port == htons(0))
281 error = EIO;
282 if (error) {
283 #ifdef NFS_BOOT_TCP
284 if (args->sotype == SOCK_STREAM) {
285 args->sotype = SOCK_DGRAM;
286 goto retry;
287 }
288 #endif
289 printf("nfs_boot: portmap NFS, error=%d\n", error);
290 return (error);
291 }
292 sin->sin_port = port;
293 return (0);
294 }
295
296
297 /*
298 * RPC: mountd/mount
299 * Given a server pathname, get an NFS file handle.
300 * Also, sets sin->sin_port to the NFS service port.
301 */
302 static int
303 md_mount(mdsin, path, argp)
304 struct sockaddr_in *mdsin; /* mountd server address */
305 char *path;
306 struct nfs_args *argp;
307 {
308 /* The RPC structures */
309 struct rdata {
310 u_int32_t errno;
311 union {
312 u_int8_t v2fh[NFSX_V2FH];
313 struct {
314 u_int32_t fhlen;
315 u_int8_t fh[1];
316 } v3fh;
317 } fh;
318 } *rdata;
319 struct mbuf *m;
320 u_int8_t *fh;
321 int minlen, error;
322 int mntver;
323
324 mntver = (argp->flags & NFSMNT_NFSV3) ? 3 : 2;
325 do {
326 /*
327 * Get port number for MOUNTD.
328 */
329 error = krpc_portmap(mdsin, RPCPROG_MNT, mntver,
330 IPPROTO_UDP, &mdsin->sin_port);
331 if (error)
332 continue;
333
334 /* This mbuf is consumed by krpc_call. */
335 m = xdr_string_encode(path, strlen(path));
336 if (m == NULL)
337 return ENOMEM;
338
339 /* Do RPC to mountd. */
340 error = krpc_call(mdsin, RPCPROG_MNT, mntver,
341 RPCMNT_MOUNT, &m, NULL);
342 if (error != EPROGMISMATCH)
343 break;
344 /* Try lower version of mountd. */
345 } while (--mntver >= 1);
346 if (error) {
347 printf("nfs_boot: mountd error=%d\n", error);
348 return error;
349 }
350 if (mntver != 3)
351 argp->flags &= ~NFSMNT_NFSV3;
352
353 /* The reply might have only the errno. */
354 if (m->m_len < 4)
355 goto bad;
356 /* Have at least errno, so check that. */
357 rdata = mtod(m, struct rdata *);
358 error = fxdr_unsigned(u_int32_t, rdata->errno);
359 if (error)
360 goto out;
361
362 /* Have errno==0, so the fh must be there. */
363 if (mntver == 3) {
364 argp->fhsize = fxdr_unsigned(u_int32_t, rdata->fh.v3fh.fhlen);
365 if (argp->fhsize > NFSX_V3FHMAX)
366 goto bad;
367 minlen = 2 * sizeof(u_int32_t) + argp->fhsize;
368 } else {
369 argp->fhsize = NFSX_V2FH;
370 minlen = sizeof(u_int32_t) + argp->fhsize;
371 }
372
373 if (m->m_len < minlen) {
374 m = m_pullup(m, minlen);
375 if (m == NULL)
376 return(EBADRPC);
377 rdata = mtod(m, struct rdata *);
378 }
379
380 fh = (mntver == 3) ?
381 rdata->fh.v3fh.fh : rdata->fh.v2fh;
382 bcopy(fh, argp->fh, argp->fhsize);
383
384 goto out;
385
386 bad:
387 error = EBADRPC;
388
389 out:
390 m_freem(m);
391 return error;
392 }
393
394 #endif /* NARP */
395