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