nfs_boot.c revision 1.33.4.1 1 /* $NetBSD: nfs_boot.c,v 1.33.4.1 1997/08/23 07:14:22 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1995 Adam Glass, Gordon Ross
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the authors may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/conf.h>
34 #include <sys/device.h>
35 #include <sys/ioctl.h>
36 #include <sys/proc.h>
37 #include <sys/mount.h>
38 #include <sys/mbuf.h>
39 #include <sys/reboot.h>
40 #include <sys/socket.h>
41 #include <sys/socketvar.h>
42
43 #include <net/if.h>
44 #include <net/route.h>
45
46 #include <net/if_ether.h>
47
48 #include <netinet/in.h>
49 #include <netinet/if_inarp.h>
50
51 #include <nfs/rpcv2.h>
52 #include <nfs/nfsproto.h>
53 #include <nfs/nfs.h>
54 #include <nfs/nfsdiskless.h>
55 #include <nfs/krpc.h>
56 #include <nfs/xdr_subs.h>
57 #include <nfs/nfs_var.h>
58
59 #include "ether.h"
60 #if NETHER == 0
61
62 int nfs_boot_init(nd, procp)
63 struct nfs_diskless *nd;
64 struct proc *procp;
65 {
66 printf("nfs_boot: NETHER == 0\n");
67 return (ENXIO);
68 }
69
70 int
71 nfs_boot_getfh(ndm)
72 struct nfs_dlmount *ndm;
73 {
74 return (ENXIO);
75 }
76
77 #else /* NETHER */
78
79 /*
80 * Support for NFS diskless booting, specifically getting information
81 * about where to boot from, what pathnames, etc.
82 *
83 * This implememtation uses RARP and the bootparam RPC.
84 * We are forced to implement RPC anyway (to get file handles)
85 * so we might as well take advantage of it for bootparam too.
86 *
87 * The diskless boot sequence goes as follows:
88 * (1) Use RARP to get our interface address
89 * (2) Use RPC/bootparam/whoami to get our hostname,
90 * our IP address, and the server's IP address.
91 * (3) Use RPC/bootparam/getfile to get the root path
92 * (4) Use RPC/mountd to get the root file handle
93 * (5) Use RPC/bootparam/getfile to get the swap path
94 * (6) Use RPC/mountd to get the swap file handle
95 *
96 * (This happens to be the way Sun does it too.)
97 */
98
99 /* bootparam RPC */
100 static int bp_whoami __P((struct sockaddr_in *bpsin,
101 struct in_addr *my_ip, struct in_addr *gw_ip));
102 static int bp_getfile __P((struct sockaddr_in *bpsin, char *key,
103 struct nfs_dlmount *ndm));
104
105 /* mountd RPC */
106 static int md_mount __P((struct sockaddr_in *mdsin, char *path,
107 struct nfs_args *argp));
108
109 /*
110 * Called with an empty nfs_diskless struct to be filled in.
111 */
112 int
113 nfs_boot_init(nd, procp)
114 struct nfs_diskless *nd;
115 struct proc *procp;
116 {
117 struct ifreq ireq;
118 struct ifaliasreq iareq;
119 struct in_addr my_ip, gw_ip;
120 struct sockaddr_in bp_sin;
121 struct sockaddr_in *sin;
122 struct ifnet *ifp;
123 struct socket *so;
124 int error;
125
126 /*
127 * Find an interface, rarp for its ip address, stuff it, the
128 * implied broadcast addr, and netmask into a nfs_diskless struct.
129 *
130 * This was moved here from nfs_vfsops.c because this procedure
131 * would be quite different if someone decides to write (i.e.) a
132 * BOOTP version of this file (might not use RARP, etc.)
133 */
134
135 /*
136 * Find a network interface.
137 */
138 ifp = ifunit(root_device->dv_xname);
139 if (ifp == NULL) {
140 printf("nfs_boot: no suitable interface\n");
141 return (ENXIO);
142 }
143 bcopy(ifp->if_xname, ireq.ifr_name, IFNAMSIZ);
144 printf("nfs_boot: using network interface '%s'\n",
145 ireq.ifr_name);
146
147 /*
148 * Bring up the interface.
149 *
150 * Get the old interface flags and or IFF_UP into them; if
151 * IFF_UP set blindly, interface selection can be clobbered.
152 */
153 so = 0;
154 error = socreate(AF_INET, &so, SOCK_DGRAM, 0);
155 if (error) {
156 printf("nfs_boot: socreate, error=%d\n", error);
157 so = 0;
158 goto out;
159 }
160 error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)&ireq, procp);
161 if (error) {
162 printf("nfs_boot: GIFFLAGS, error=%d\n", error);
163 goto out;
164 }
165 ireq.ifr_flags |= IFF_UP;
166 error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)&ireq, procp);
167 if (error) {
168 printf("nfs_boot: SIFFLAGS, error=%d\n", error);
169 goto out;
170 }
171
172 /*
173 * Do RARP for the interface address.
174 */
175 if ((error = revarpwhoami(&my_ip, ifp)) != 0) {
176 printf("revarp failed, error=%d\n", error);
177 error = EIO; /* XXX */
178 goto out;
179 }
180 printf("nfs_boot: client_addr=0x%x\n",
181 (u_int32_t)ntohl(my_ip.s_addr));
182
183 /*
184 * Do enough of ifconfig(8) so that the chosen interface
185 * can talk to the servers. (just set the address)
186 */
187 bzero(&iareq, sizeof(iareq));
188 bcopy(ifp->if_xname, iareq.ifra_name, IFNAMSIZ);
189 sin = (struct sockaddr_in *)&iareq.ifra_addr;
190 sin->sin_len = sizeof(*sin);
191 sin->sin_family = AF_INET;
192 sin->sin_addr.s_addr = my_ip.s_addr;
193 #ifdef NFS_BOOT_NETMASK
194 sin = (struct sockaddr_in *)&iareq.ifra_mask;
195 sin->sin_len = sizeof(*sin);
196 sin->sin_family = AF_INET;
197 sin->sin_addr.s_addr = htonl(NFS_BOOT_NETMASK);
198 #endif
199 error = ifioctl(so, SIOCAIFADDR, (caddr_t)&iareq, procp);
200 if (error) {
201 printf("nfs_boot: set if addr, error=%d\n", error);
202 goto out;
203 }
204
205 /*
206 * Get client name and gateway address.
207 * RPC: bootparam/whoami
208 * Use the old broadcast address for the WHOAMI
209 * call because we do not yet know our netmask.
210 * The server address returned by the WHOAMI call
211 * is used for all subsequent booptaram RPCs.
212 */
213 bzero((caddr_t)&bp_sin, sizeof(bp_sin));
214 bp_sin.sin_len = sizeof(bp_sin);
215 bp_sin.sin_family = AF_INET;
216 bp_sin.sin_addr.s_addr = INADDR_BROADCAST;
217 hostnamelen = MAXHOSTNAMELEN;
218
219 /* Do the RPC/bootparam/whoami. */
220 error = bp_whoami(&bp_sin, &my_ip, &gw_ip);
221 if (error) {
222 printf("nfs_boot: bootparam whoami, error=%d\n", error);
223 goto out;
224 }
225 printf("nfs_boot: server_addr=0x%x\n",
226 (u_int32_t)ntohl(bp_sin.sin_addr.s_addr));
227 printf("nfs_boot: hostname=%s\n", hostname);
228
229 #ifdef NFS_BOOT_GATEWAY
230 /*
231 * XXX - This code is conditionally compiled only because
232 * many bootparam servers (in particular, SunOS 4.1.3)
233 * always set the gateway address to their own address.
234 * The bootparam server is not necessarily the gateway.
235 * We could just believe the server, and at worst you would
236 * need to delete the incorrect default route before adding
237 * the correct one, but for simplicity, ignore the gateway.
238 * If your server is OK, you can turn on this option.
239 *
240 * If the gateway address is set, add a default route.
241 * (The mountd RPCs may go across a gateway.)
242 */
243 if (gw_ip.s_addr) {
244 struct sockaddr dst, gw, mask;
245 /* Destination: (default) */
246 bzero((caddr_t)&dst, sizeof(dst));
247 dst.sa_len = sizeof(dst);
248 dst.sa_family = AF_INET;
249 /* Gateway: */
250 bzero((caddr_t)&gw, sizeof(gw));
251 sin = (struct sockaddr_in *)&gw;
252 sin->sin_len = sizeof(gw);
253 sin->sin_family = AF_INET;
254 sin->sin_addr.s_addr = gw_ip.s_addr;
255 /* Mask: (zero length) */
256 bzero(&mask, sizeof(mask));
257
258 printf("nfs_boot: gateway=0x%x\n", ntohl(gw_ip.s_addr));
259 /* add, dest, gw, mask, flags, 0 */
260 error = rtrequest(RTM_ADD, &dst, (struct sockaddr *)&gw,
261 &mask, (RTF_UP | RTF_GATEWAY | RTF_STATIC), NULL);
262 if (error)
263 printf("nfs_boot: add route, error=%d\n", error);
264 }
265 #endif
266
267 bcopy(&bp_sin, &nd->nd_boot, sizeof(bp_sin));
268
269 /*
270 * Now fetch the server:pathname strings and server IP
271 * for root and swap. Missing swap is not fatal.
272 */
273 error = bp_getfile(&nd->nd_boot, "root", &nd->nd_root);
274 if (error) {
275 printf("nfs_boot: bootparam get root: %d\n", error);
276 goto out;
277 }
278
279 #if 0
280 error = bp_getfile(&nd->nd_boot, "swap", &nd->nd_swap);
281 if (error) {
282 printf("nfs_boot: bootparam get swap: %d", error);
283 error = 0;
284 }
285 #endif
286
287 out:
288 if (so) soclose(so);
289
290 return (error);
291 }
292
293 int
294 nfs_boot_getfh(ndm)
295 struct nfs_dlmount *ndm; /* output */
296 {
297 struct nfs_args *args;
298 struct sockaddr_in *sin;
299 char *pathname;
300 int error;
301
302 args = &ndm->ndm_args;
303
304 /* Initialize mount args. */
305 bzero((caddr_t) args, sizeof(*args));
306 args->addr = &ndm->ndm_saddr;
307 args->addrlen = args->addr->sa_len;
308 #ifdef NFS_BOOT_TCP
309 args->sotype = SOCK_STREAM;
310 #else
311 args->sotype = SOCK_DGRAM;
312 #endif
313 args->fh = ndm->ndm_fh;
314 args->hostname = ndm->ndm_host;
315 args->flags = NFSMNT_RESVPORT | NFSMNT_NFSV3;
316
317 #ifdef NFS_BOOT_OPTIONS
318 args->flags |= NFS_BOOT_OPTIONS;
319 #endif
320 #ifdef NFS_BOOT_RWSIZE
321 /*
322 * Reduce rsize,wsize for interfaces that consistently
323 * drop fragments of long UDP messages. (i.e. wd8003).
324 * You can always change these later via remount.
325 */
326 args->flags |= NFSMNT_WSIZE | NFSMNT_RSIZE;
327 args->wsize = NFS_BOOT_RWSIZE;
328 args->rsize = NFS_BOOT_RWSIZE;
329 #endif
330
331 /*
332 * Find the pathname part of the "server:pathname"
333 * string left in ndm->ndm_host by nfs_boot_init.
334 */
335 pathname = strchr(ndm->ndm_host, ':');
336 if (pathname == 0) {
337 printf("nfs_boot: getfh - no pathname\n");
338 return (EIO);
339 }
340 pathname++;
341
342 /*
343 * Get file handle using RPC to mountd/mount
344 */
345 sin = (struct sockaddr_in *)&ndm->ndm_saddr;
346 error = md_mount(sin, pathname, args);
347 if (error) {
348 printf("nfs_boot: mountd `%s', error=%d\n",
349 ndm->ndm_host, error);
350 return (error);
351 }
352
353 /* Set port number for NFS use. */
354 /* XXX: NFS port is always 2049, right? */
355 #ifdef NFS_BOOT_TCP
356 retry:
357 #endif
358 error = krpc_portmap(sin, NFS_PROG,
359 (args->flags & NFSMNT_NFSV3) ? NFS_VER3 : NFS_VER2,
360 (args->sotype == SOCK_STREAM) ? IPPROTO_TCP : IPPROTO_UDP,
361 &sin->sin_port);
362 if (sin->sin_port == htons(0))
363 error = EIO;
364 if (error) {
365 #ifdef NFS_BOOT_TCP
366 if (args->sotype == SOCK_STREAM) {
367 args->sotype = SOCK_DGRAM;
368 goto retry;
369 }
370 #endif
371 printf("nfs_boot: portmap NFS, error=%d\n", error);
372 }
373
374 return (error);
375 }
376
377
378 /*
379 * RPC: bootparam/whoami
380 * Given client IP address, get:
381 * client name (hostname)
382 * domain name (domainname)
383 * gateway address
384 *
385 * The hostname and domainname are set here for convenience.
386 *
387 * Note - bpsin is initialized to the broadcast address,
388 * and will be replaced with the bootparam server address
389 * after this call is complete. Have to use PMAP_PROC_CALL
390 * to make sure we get responses only from a servers that
391 * know about us (don't want to broadcast a getport call).
392 */
393 static int
394 bp_whoami(bpsin, my_ip, gw_ip)
395 struct sockaddr_in *bpsin;
396 struct in_addr *my_ip;
397 struct in_addr *gw_ip;
398 {
399 /* RPC structures for PMAPPROC_CALLIT */
400 struct whoami_call {
401 u_int32_t call_prog;
402 u_int32_t call_vers;
403 u_int32_t call_proc;
404 u_int32_t call_arglen;
405 } *call;
406 struct callit_reply {
407 u_int32_t port;
408 u_int32_t encap_len;
409 /* encapsulated data here */
410 } *reply;
411
412 struct mbuf *m, *from;
413 struct sockaddr_in *sin;
414 int error, msg_len;
415 int16_t port;
416
417 /*
418 * Build request message for PMAPPROC_CALLIT.
419 */
420 m = m_get(M_WAIT, MT_DATA);
421 call = mtod(m, struct whoami_call *);
422 m->m_len = sizeof(*call);
423 call->call_prog = txdr_unsigned(BOOTPARAM_PROG);
424 call->call_vers = txdr_unsigned(BOOTPARAM_VERS);
425 call->call_proc = txdr_unsigned(BOOTPARAM_WHOAMI);
426
427 /*
428 * append encapsulated data (client IP address)
429 */
430 m->m_next = xdr_inaddr_encode(my_ip);
431 call->call_arglen = txdr_unsigned(m->m_next->m_len);
432
433 /* RPC: portmap/callit */
434 bpsin->sin_port = htons(PMAPPORT);
435 from = NULL;
436 error = krpc_call(bpsin, PMAPPROG, PMAPVERS,
437 PMAPPROC_CALLIT, &m, &from);
438 if (error)
439 return error;
440
441 /*
442 * Parse result message.
443 */
444 if (m->m_len < sizeof(*reply)) {
445 m = m_pullup(m, sizeof(*reply));
446 if (m == NULL)
447 goto bad;
448 }
449 reply = mtod(m, struct callit_reply *);
450 port = fxdr_unsigned(u_int32_t, reply->port);
451 msg_len = fxdr_unsigned(u_int32_t, reply->encap_len);
452 m_adj(m, sizeof(*reply));
453
454 /*
455 * Save bootparam server address
456 */
457 sin = mtod(from, struct sockaddr_in *);
458 bpsin->sin_port = htons(port);
459 bpsin->sin_addr.s_addr = sin->sin_addr.s_addr;
460
461 /* client name */
462 hostnamelen = MAXHOSTNAMELEN-1;
463 m = xdr_string_decode(m, hostname, &hostnamelen);
464 if (m == NULL)
465 goto bad;
466
467 /* domain name */
468 domainnamelen = MAXHOSTNAMELEN-1;
469 m = xdr_string_decode(m, domainname, &domainnamelen);
470 if (m == NULL)
471 goto bad;
472
473 /* gateway address */
474 m = xdr_inaddr_decode(m, gw_ip);
475 if (m == NULL)
476 goto bad;
477
478 /* success */
479 goto out;
480
481 bad:
482 printf("nfs_boot: bootparam_whoami: bad reply\n");
483 error = EBADRPC;
484
485 out:
486 if (from)
487 m_freem(from);
488 if (m)
489 m_freem(m);
490 return(error);
491 }
492
493
494 /*
495 * RPC: bootparam/getfile
496 * Given client name and file "key", get:
497 * server name
498 * server IP address
499 * server pathname
500 */
501 static int
502 bp_getfile(bpsin, key, ndm)
503 struct sockaddr_in *bpsin;
504 char *key;
505 struct nfs_dlmount *ndm;
506 {
507 char pathname[MNAMELEN];
508 struct in_addr inaddr;
509 struct sockaddr_in *sin;
510 struct mbuf *m;
511 char *serv_name;
512 int error, sn_len, path_len;
513
514 /*
515 * Build request message.
516 */
517
518 /* client name (hostname) */
519 m = xdr_string_encode(hostname, hostnamelen);
520 if (m == NULL)
521 return (ENOMEM);
522
523 /* key name (root or swap) */
524 m->m_next = xdr_string_encode(key, strlen(key));
525 if (m->m_next == NULL)
526 return (ENOMEM);
527
528 /* RPC: bootparam/getfile */
529 error = krpc_call(bpsin, BOOTPARAM_PROG, BOOTPARAM_VERS,
530 BOOTPARAM_GETFILE, &m, NULL);
531 if (error)
532 return error;
533
534 /*
535 * Parse result message.
536 */
537
538 /* server name */
539 serv_name = &ndm->ndm_host[0];
540 sn_len = sizeof(ndm->ndm_host) - 1;
541 m = xdr_string_decode(m, serv_name, &sn_len);
542 if (m == NULL)
543 goto bad;
544
545 /* server IP address (mountd/NFS) */
546 m = xdr_inaddr_decode(m, &inaddr);
547 if (m == NULL)
548 goto bad;
549
550 /* server pathname */
551 path_len = sizeof(pathname) - 1;
552 m = xdr_string_decode(m, pathname, &path_len);
553 if (m == NULL)
554 goto bad;
555
556 /*
557 * Store the results in the nfs_dlmount.
558 * The strings become "server:pathname"
559 */
560 sin = (struct sockaddr_in *) &ndm->ndm_saddr;
561 bzero((caddr_t)sin, sizeof(*sin));
562 sin->sin_len = sizeof(*sin);
563 sin->sin_family = AF_INET;
564 sin->sin_addr = inaddr;
565 if ((sn_len + 1 + path_len + 1) > sizeof(ndm->ndm_host)) {
566 printf("nfs_boot: getfile name too long\n");
567 error = EIO;
568 goto out;
569 }
570 ndm->ndm_host[sn_len] = ':';
571 bcopy(pathname, ndm->ndm_host + sn_len + 1, path_len + 1);
572
573 /* success */
574 goto out;
575
576 bad:
577 printf("nfs_boot: bootparam_getfile: bad reply\n");
578 error = EBADRPC;
579
580 out:
581 m_freem(m);
582 return(0);
583 }
584
585
586 /*
587 * RPC: mountd/mount
588 * Given a server pathname, get an NFS file handle.
589 * Also, sets sin->sin_port to the NFS service port.
590 */
591 static int
592 md_mount(mdsin, path, argp)
593 struct sockaddr_in *mdsin; /* mountd server address */
594 char *path;
595 struct nfs_args *argp;
596 {
597 /* The RPC structures */
598 struct rdata {
599 u_int32_t errno;
600 union {
601 u_int8_t v2fh[NFSX_V2FH];
602 struct {
603 u_int32_t fhlen;
604 u_int8_t fh[1];
605 } v3fh;
606 } fh;
607 } *rdata;
608 struct mbuf *m;
609 u_int8_t *fh;
610 int minlen, error;
611
612 retry:
613 /* Get port number for MOUNTD. */
614 error = krpc_portmap(mdsin, RPCPROG_MNT,
615 (argp->flags & NFSMNT_NFSV3) ? RPCMNT_VER3 : RPCMNT_VER1,
616 IPPROTO_UDP, &mdsin->sin_port);
617 if (error)
618 return error;
619
620 m = xdr_string_encode(path, strlen(path));
621 if (m == NULL)
622 return ENOMEM;
623
624 /* Do RPC to mountd. */
625 error = krpc_call(mdsin, RPCPROG_MNT, (argp->flags & NFSMNT_NFSV3) ?
626 RPCMNT_VER3 : RPCMNT_VER1, RPCMNT_MOUNT, &m, NULL);
627 if (error) {
628 if ((error==RPC_PROGMISMATCH) && (argp->flags & NFSMNT_NFSV3)) {
629 argp->flags &= ~NFSMNT_NFSV3;
630 goto retry;
631 }
632 return error; /* message already freed */
633 }
634
635 /* The reply might have only the errno. */
636 if (m->m_len < 4)
637 goto bad;
638 /* Have at least errno, so check that. */
639 rdata = mtod(m, struct rdata *);
640 error = fxdr_unsigned(u_int32_t, rdata->errno);
641 if (error)
642 goto out;
643
644 /* Have errno==0, so the fh must be there. */
645 if (argp->flags & NFSMNT_NFSV3){
646 argp->fhsize = fxdr_unsigned(u_int32_t, rdata->fh.v3fh.fhlen);
647 if (argp->fhsize > NFSX_V3FHMAX)
648 goto bad;
649 minlen = 2 * sizeof(u_int32_t) + argp->fhsize;
650 } else {
651 argp->fhsize = NFSX_V2FH;
652 minlen = sizeof(u_int32_t) + argp->fhsize;
653 }
654
655 if (m->m_len < minlen) {
656 m = m_pullup(m, minlen);
657 if (m == NULL)
658 return(EBADRPC);
659 rdata = mtod(m, struct rdata *);
660 }
661
662 fh = (argp->flags & NFSMNT_NFSV3) ? rdata->fh.v3fh.fh : rdata->fh.v2fh;
663 bcopy(fh, argp->fh, argp->fhsize);
664
665 goto out;
666
667 bad:
668 error = EBADRPC;
669
670 out:
671 m_freem(m);
672 return error;
673 }
674
675 #endif /* NETHER */
676