nfs_boot.c revision 1.40 1 /* $NetBSD: nfs_boot.c,v 1.40 1998/01/09 15:10:37 drochner 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 "opt_nfs_boot_bootp.h"
73 #include "opt_nfs_boot_bootparam.h"
74
75 /*
76 * There are two implementations of NFS diskless boot.
77 * One implementation uses BOOTP (RFC951, RFC1048),
78 * the other uses Sun RPC/bootparams. See the files:
79 * nfs_bootp.c: BOOTP (RFC951, RFC1048)
80 * nfs_bootsun.c: Sun RPC/bootparams
81 */
82 #ifdef NFS_BOOT_BOOTP
83 int nfs_boot_rfc951 = 1; /* BOOTP enabled (default) */
84 #endif
85 #ifdef NFS_BOOT_BOOTPARAM
86 int nfs_boot_bootparam = 1; /* BOOTPARAM enabled (default) */
87 #endif
88
89 /* mountd RPC */
90 static int md_mount __P((struct sockaddr_in *mdsin, char *path,
91 struct nfs_args *argp));
92
93 static void nfs_boot_defrt __P((struct in_addr *));
94 static int nfs_boot_getfh __P((struct nfs_dlmount *ndm));
95
96
97 /*
98 * Called with an empty nfs_diskless struct to be filled in.
99 * Find an interface, determine its ip address (etc.) and
100 * save all the boot parameters in the nfs_diskless struct.
101 */
102 int
103 nfs_boot_init(nd, procp)
104 struct nfs_diskless *nd;
105 struct proc *procp;
106 {
107 struct ifnet *ifp;
108 int error;
109
110 /*
111 * Find the network interface.
112 */
113 ifp = ifunit(root_device->dv_xname);
114 if (ifp == NULL) {
115 printf("nfs_boot: '%s' not found\n",
116 root_device->dv_xname);
117 return (ENXIO);
118 }
119
120 error = EADDRNOTAVAIL; /* ??? */
121 #ifdef NFS_BOOT_BOOTP
122 if (nfs_boot_rfc951) {
123 printf("nfs_boot: trying BOOTP/DHCP\n");
124 error = nfs_bootdhcp(ifp, nd, procp);
125 if (!error)
126 goto ok;
127 }
128 #endif
129 #ifdef NFS_BOOT_BOOTPARAM
130 if (nfs_boot_bootparam) {
131 printf("nfs_boot: trying RARP (and RPC/bootparam)\n");
132 error = nfs_bootparam(ifp, nd, procp);
133 if (!error)
134 goto ok;
135 }
136 #endif
137 if (0) goto ok; /* XXX stupid gcc */
138 return (error);
139
140 ok:
141 /*
142 * If the gateway address is set, add a default route.
143 * (The mountd RPCs may go across a gateway.)
144 */
145 if (nd->nd_gwip.s_addr)
146 nfs_boot_defrt(&nd->nd_gwip);
147
148 /*
149 * Now fetch the NFS file handles as appropriate.
150 */
151 error = nfs_boot_getfh(&nd->nd_root);
152 if (error)
153 return (error);
154
155 return (error);
156 }
157
158 int nfs_boot_setrecvtimo(so)
159 struct socket *so;
160 {
161 struct mbuf *m;
162 struct timeval *tv;
163
164 m = m_get(M_WAIT, MT_SOOPTS);
165 tv = mtod(m, struct timeval *);
166 m->m_len = sizeof(*tv);
167 tv->tv_sec = 1;
168 tv->tv_usec = 0;
169 return(sosetopt(so, SOL_SOCKET, SO_RCVTIMEO, m));
170 }
171
172 int nfs_boot_enbroadcast(so)
173 struct socket *so;
174 {
175 struct mbuf *m;
176 int32_t *on;
177
178 m = m_get(M_WAIT, MT_SOOPTS);
179 on = mtod(m, int32_t *);
180 m->m_len = sizeof(*on);
181 *on = 1;
182 return(sosetopt(so, SOL_SOCKET, SO_BROADCAST, m));
183 }
184
185 int nfs_boot_sobind_ipport(so, port)
186 struct socket *so;
187 u_int16_t port;
188 {
189 struct mbuf *m;
190 struct sockaddr_in *sin;
191 int error;
192
193 m = m_getclr(M_WAIT, MT_SONAME);
194 sin = mtod(m, struct sockaddr_in *);
195 sin->sin_len = m->m_len = sizeof(*sin);
196 sin->sin_family = AF_INET;
197 sin->sin_addr.s_addr = INADDR_ANY;
198 sin->sin_port = htons(port);
199 error = sobind(so, m);
200 m_freem(m);
201 return(error);
202 }
203
204 /*
205 * What is the longest we will wait before re-sending a request?
206 * Note this is also the frequency of "timeout" messages.
207 * The re-send loop counts up linearly to this maximum, so the
208 * first complaint will happen after (1+2+3+4+5)=15 seconds.
209 */
210 #define MAX_RESEND_DELAY 5 /* seconds */
211 #define TOTAL_TIMEOUT 30 /* seconds */
212
213 int nfs_boot_sendrecv(so, nam, sndproc, snd, rcvproc, rcv,
214 from_p, context)
215 struct socket *so;
216 struct mbuf *nam;
217 int (*sndproc) __P((struct mbuf*, void*, int));
218 struct mbuf *snd;
219 int (*rcvproc) __P((struct mbuf*, void*));
220 struct mbuf **rcv, **from_p;
221 void *context;
222 {
223 int error, rcvflg, timo, secs, waited;
224 struct mbuf *m, *from;
225 struct uio uio;
226
227 /* Free at end if not null. */
228 from = NULL;
229
230 /*
231 * Send it, repeatedly, until a reply is received,
232 * but delay each re-send by an increasing amount.
233 * If the delay hits the maximum, start complaining.
234 */
235 waited = timo = 0;
236 send_again:
237 waited += timo;
238 if (waited >= TOTAL_TIMEOUT)
239 return(ETIMEDOUT);
240
241 /* Determine new timeout. */
242 if (timo < MAX_RESEND_DELAY)
243 timo++;
244 else
245 printf("nfs_boot: timeout...\n");
246
247 if (sndproc) {
248 error = (*sndproc)(snd, context, waited);
249 if (error)
250 goto out;
251 }
252
253 /* Send request (or re-send). */
254 m = m_copypacket(snd, M_WAIT);
255 if (m == NULL) {
256 error = ENOBUFS;
257 goto out;
258 }
259 error = sosend(so, nam, NULL, m, NULL, 0);
260 if (error) {
261 printf("nfs_boot: sosend: %d\n", error);
262 goto out;
263 }
264 m = NULL;
265
266 /*
267 * Wait for up to timo seconds for a reply.
268 * The socket receive timeout was set to 1 second.
269 */
270
271 secs = timo;
272 for (;;) {
273 if (from) {
274 m_freem(from);
275 from = NULL;
276 }
277 if (m) {
278 m_freem(m);
279 m = NULL;
280 }
281 uio.uio_resid = 1 << 16; /* ??? */
282 rcvflg = 0;
283 error = soreceive(so, &from, &uio, &m, NULL, &rcvflg);
284 if (error == EWOULDBLOCK) {
285 if (--secs <= 0)
286 goto send_again;
287 continue;
288 }
289 if (error)
290 goto out;
291 #ifdef DIAGNOSTIC
292 if (!m || !(m->m_flags & M_PKTHDR)
293 || (1 << 16) - uio.uio_resid != m->m_pkthdr.len)
294 panic("nfs_boot_sendrecv: return size");
295 #endif
296
297 if ((*rcvproc)(m, context))
298 continue;
299
300 if (rcv)
301 *rcv = m;
302 else
303 m_freem(m);
304 if (from_p) {
305 *from_p = from;
306 from = NULL;
307 }
308 break;
309 }
310 out:
311 if (from) m_freem(from);
312 return(error);
313 }
314
315 /*
316 * Install a default route to the passed IP address.
317 */
318 static void
319 nfs_boot_defrt(gw_ip)
320 struct in_addr *gw_ip;
321 {
322 struct sockaddr dst, gw, mask;
323 struct sockaddr_in *sin;
324 int error;
325
326 /* Destination: (default) */
327 bzero((caddr_t)&dst, sizeof(dst));
328 dst.sa_len = sizeof(dst);
329 dst.sa_family = AF_INET;
330 /* Gateway: */
331 bzero((caddr_t)&gw, sizeof(gw));
332 sin = (struct sockaddr_in *)&gw;
333 sin->sin_len = sizeof(*sin);
334 sin->sin_family = AF_INET;
335 sin->sin_addr.s_addr = gw_ip->s_addr;
336 /* Mask: (zero length) */
337 /* XXX - Just pass a null pointer? */
338 bzero(&mask, sizeof(mask));
339
340 /* add, dest, gw, mask, flags, 0 */
341 error = rtrequest(RTM_ADD, &dst, &gw, &mask,
342 (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
343 if (error) {
344 printf("nfs_boot: add route, error=%d\n", error);
345 error = 0;
346 }
347 }
348
349 /*
350 * Get an initial NFS file handle using Sun RPC/mountd.
351 * Separate function because we used to call it twice.
352 * (once for root and once for swap)
353 */
354 static int
355 nfs_boot_getfh(ndm)
356 struct nfs_dlmount *ndm; /* output */
357 {
358 struct nfs_args *args;
359 struct sockaddr_in *sin;
360 char *pathname;
361 int error;
362 u_int16_t port;
363
364 args = &ndm->ndm_args;
365
366 /* Initialize mount args. */
367 bzero((caddr_t) args, sizeof(*args));
368 args->addr = &ndm->ndm_saddr;
369 args->addrlen = args->addr->sa_len;
370 #ifdef NFS_BOOT_TCP
371 args->sotype = SOCK_STREAM;
372 #else
373 args->sotype = SOCK_DGRAM;
374 #endif
375 args->fh = ndm->ndm_fh;
376 args->hostname = ndm->ndm_host;
377 args->flags = NFSMNT_RESVPORT | NFSMNT_NFSV3;
378
379 #ifdef NFS_BOOT_OPTIONS
380 args->flags |= NFS_BOOT_OPTIONS;
381 #endif
382 #ifdef NFS_BOOT_RWSIZE
383 /*
384 * Reduce rsize,wsize for interfaces that consistently
385 * drop fragments of long UDP messages. (i.e. wd8003).
386 * You can always change these later via remount.
387 */
388 args->flags |= NFSMNT_WSIZE | NFSMNT_RSIZE;
389 args->wsize = NFS_BOOT_RWSIZE;
390 args->rsize = NFS_BOOT_RWSIZE;
391 #endif
392
393 /*
394 * Find the pathname part of the "server:pathname"
395 * string left in ndm->ndm_host by nfs_boot_init.
396 */
397 pathname = strchr(ndm->ndm_host, ':');
398 if (pathname == 0) {
399 printf("nfs_boot: getfh - no pathname\n");
400 return (EIO);
401 }
402 pathname++;
403
404 /*
405 * Get file handle using RPC to mountd/mount
406 */
407 sin = (struct sockaddr_in *)&ndm->ndm_saddr;
408 error = md_mount(sin, pathname, args);
409 if (error) {
410 printf("nfs_boot: mountd `%s', error=%d\n",
411 ndm->ndm_host, error);
412 return (error);
413 }
414
415 /* Set port number for NFS use. */
416 /* XXX: NFS port is always 2049, right? */
417 #ifdef NFS_BOOT_TCP
418 retry:
419 #endif
420 error = krpc_portmap(sin, NFS_PROG,
421 (args->flags & NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
422 (args->sotype == SOCK_STREAM) ? IPPROTO_TCP : IPPROTO_UDP,
423 &port);
424 if (port == htons(0))
425 error = EIO;
426 if (error) {
427 #ifdef NFS_BOOT_TCP
428 if (args->sotype == SOCK_STREAM) {
429 args->sotype = SOCK_DGRAM;
430 goto retry;
431 }
432 #endif
433 printf("nfs_boot: portmap NFS, error=%d\n", error);
434 return (error);
435 }
436 sin->sin_port = port;
437 return (0);
438 }
439
440
441 /*
442 * RPC: mountd/mount
443 * Given a server pathname, get an NFS file handle.
444 * Also, sets sin->sin_port to the NFS service port.
445 */
446 static int
447 md_mount(mdsin, path, argp)
448 struct sockaddr_in *mdsin; /* mountd server address */
449 char *path;
450 struct nfs_args *argp;
451 {
452 /* The RPC structures */
453 struct rdata {
454 u_int32_t errno;
455 union {
456 u_int8_t v2fh[NFSX_V2FH];
457 struct {
458 u_int32_t fhlen;
459 u_int8_t fh[1];
460 } v3fh;
461 } fh;
462 } *rdata;
463 struct mbuf *m;
464 u_int8_t *fh;
465 int minlen, error;
466 int mntver;
467
468 mntver = (argp->flags & NFSMNT_NFSV3) ? 3 : 2;
469 do {
470 /*
471 * Get port number for MOUNTD.
472 */
473 error = krpc_portmap(mdsin, RPCPROG_MNT, mntver,
474 IPPROTO_UDP, &mdsin->sin_port);
475 if (error)
476 continue;
477
478 /* This mbuf is consumed by krpc_call. */
479 m = xdr_string_encode(path, strlen(path));
480 if (m == NULL)
481 return ENOMEM;
482
483 /* Do RPC to mountd. */
484 error = krpc_call(mdsin, RPCPROG_MNT, mntver,
485 RPCMNT_MOUNT, &m, NULL);
486 if (error != EPROGMISMATCH)
487 break;
488 /* Try lower version of mountd. */
489 } while (--mntver >= 1);
490 if (error) {
491 printf("nfs_boot: mountd error=%d\n", error);
492 return error;
493 }
494 if (mntver != 3)
495 argp->flags &= ~NFSMNT_NFSV3;
496
497 /* The reply might have only the errno. */
498 if (m->m_len < 4)
499 goto bad;
500 /* Have at least errno, so check that. */
501 rdata = mtod(m, struct rdata *);
502 error = fxdr_unsigned(u_int32_t, rdata->errno);
503 if (error)
504 goto out;
505
506 /* Have errno==0, so the fh must be there. */
507 if (mntver == 3) {
508 argp->fhsize = fxdr_unsigned(u_int32_t, rdata->fh.v3fh.fhlen);
509 if (argp->fhsize > NFSX_V3FHMAX)
510 goto bad;
511 minlen = 2 * sizeof(u_int32_t) + argp->fhsize;
512 } else {
513 argp->fhsize = NFSX_V2FH;
514 minlen = sizeof(u_int32_t) + argp->fhsize;
515 }
516
517 if (m->m_len < minlen) {
518 m = m_pullup(m, minlen);
519 if (m == NULL)
520 return(EBADRPC);
521 rdata = mtod(m, struct rdata *);
522 }
523
524 fh = (mntver == 3) ?
525 rdata->fh.v3fh.fh : rdata->fh.v2fh;
526 bcopy(fh, argp->fh, argp->fhsize);
527
528 goto out;
529
530 bad:
531 error = EBADRPC;
532
533 out:
534 m_freem(m);
535 return error;
536 }
537