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