nfs_boot.c revision 1.7 1 /* $NetBSD: nfs_boot.c,v 1.7 1994/07/16 11:43:12 paulus Exp $ */
2
3 /*
4 * Copyright (c) 1994 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/ioctl.h>
35 #include <sys/proc.h>
36 #include <sys/mount.h>
37 #include <sys/mbuf.h>
38 #include <sys/socket.h>
39 #include <sys/reboot.h>
40
41 #include <net/if.h>
42 #include <net/route.h>
43
44 #include <netinet/in.h>
45 #include <netinet/if_ether.h>
46
47 #include <nfs/rpcv2.h>
48 #include <nfs/nfsv2.h>
49 #include <nfs/nfs.h>
50 #include <nfs/nfsdiskless.h>
51
52 #include "ether.h"
53 #if NETHER > 0
54
55 /*
56 * Support for NFS diskless booting, specifically getting information
57 * about where to boot from, what pathnames, etc.
58 *
59 * This implememtation uses RARP and the bootparam RPC.
60 * We are forced to implement RPC anyway (to get file handles)
61 * so we might as well take advantage of it for bootparam too.
62 *
63 * The diskless boot sequence goes as follows:
64 * (1) Get our interface address using RARP
65 * (also save the address of the RARP server)
66 * (2) Get our hostname using RPC/bootparam/whoami
67 * (all boopararms RPCs to the RARP server)
68 * (3) Get the root path using RPC/bootparam/getfile
69 * (4) Get the root file handle using RPC/mountd
70 * (5) Get the swap path using RPC/bootparam/getfile
71 * (6) Get the swap file handle using RPC/mountd
72 *
73 * (This happens to be the way Sun does it too.)
74 */
75
76 /* bootparam RPC */
77 static int bp_whoami __P((struct sockaddr_in *bpsin,
78 struct in_addr *my_ip, struct in_addr *gw_ip));
79 static int bp_getfile __P((struct sockaddr_in *bpsin, char *key,
80 struct sockaddr_in *mdsin, char *servname, char *path));
81
82 /* mountd RPC */
83 static int md_mount __P((struct sockaddr_in *mdsin, char *path,
84 u_char *fh));
85
86 /* other helpers */
87 static void get_path_and_handle __P((struct sockaddr_in *bpsin,
88 char *key, struct nfs_dlmount *ndmntp));
89
90 /*
91 * Called with an empty nfs_diskless struct to be filled in.
92 */
93 int
94 nfs_boot_init(nd, procp)
95 struct nfs_diskless *nd;
96 struct proc *procp;
97 {
98 struct ifreq ireq;
99 struct in_addr my_ip, srv_ip, gw_ip;
100 struct sockaddr_in bp_sin;
101 struct sockaddr_in *sin;
102 struct ifnet *ifp;
103 struct socket *so;
104 int error, len;
105 u_short port;
106
107 #if 0
108 /*
109 * XXX time must be non-zero when we init the interface or else
110 * the arp code will wedge... (Fixed in if_ether.c -gwr)
111 */
112 if (time.tv_sec == 0)
113 time.tv_sec = 1;
114 #endif
115
116 /*
117 * Find an interface, rarp for its ip address, stuff it, the
118 * implied broadcast addr, and netmask into a nfs_diskless struct.
119 *
120 * This was moved here from nfs_vfsops.c because this procedure
121 * would be quite different if someone decides to write (i.e.) a
122 * BOOTP version of this file (might not use RARP, etc.) -gwr
123 */
124
125 /*
126 * Find a network interface.
127 * XXX - This should use the specified boot device.
128 */
129 for (ifp = ifnet; ifp; ifp = ifp->if_next)
130 if ((ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
131 break;
132 if (ifp == NULL)
133 panic("nfs_boot: no suitable interface");
134 sprintf(ireq.ifr_name, "%s%d", ifp->if_name, ifp->if_unit);
135 printf("nfs_boot: using network interface '%s'\n",
136 ireq.ifr_name);
137
138 /*
139 * Bring up the interface.
140 */
141 if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0)) != 0)
142 panic("nfs_boot: socreate, error=%d", error);
143 ireq.ifr_flags = IFF_UP;
144 error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)&ireq, procp);
145 if (error)
146 panic("nfs_boot: SIFFLAGS, error=%d", error);
147
148 /*
149 * Do RARP for the interface address. Also
150 * save the server address for bootparam RPC.
151 */
152 if ((error = revarpwhoarewe(ifp, &srv_ip, &my_ip)) != 0)
153 panic("revarp failed, error=%d", error);
154 printf("nfs_boot: client=0x%x, server=0x%x\n",
155 my_ip.s_addr, srv_ip.s_addr);
156
157 /*
158 * Do enough of ifconfig(8) so that the chosen interface can
159 * talk to the server(s). (also get brcast addr and netmask)
160 */
161 /* Set interface address. */
162 sin = (struct sockaddr_in *)&ireq.ifr_addr;
163 sin->sin_len = sizeof(*sin);
164 sin->sin_family = AF_INET;
165 sin->sin_addr.s_addr = my_ip.s_addr;
166 error = ifioctl(so, SIOCSIFADDR, (caddr_t)&ireq, procp);
167 if (error)
168 panic("nfs_boot: set if addr, error=%d", error);
169
170 soclose(so);
171
172 /*
173 * Get client name and gateway address.
174 * RPC: bootparam/whoami
175 */
176 bp_sin.sin_len = sizeof(bp_sin);
177 bp_sin.sin_family = AF_INET;
178 bp_sin.sin_addr.s_addr = srv_ip.s_addr;
179 hostnamelen = MAXHOSTNAMELEN;
180
181 /* this returns gateway IP address */
182 error = bp_whoami(&bp_sin, &my_ip, &gw_ip);
183 if (error)
184 panic("nfs_boot: bootparam whoami, error=%d", error);
185 printf("nfs_boot: hostname=%s\n", hostname);
186
187 #ifdef NFS_BOOT_GATEWAY
188 /*
189 * XXX - Server supplied gateway is usually bogus...
190 * (At least for SunOS 4.1.3 servers it is.)
191 * If your server is OK, you can turn on this option.
192 *
193 * If the gateway address is set, add a default route.
194 * (The mountd RPCs may go across a gateway.)
195 */
196 if (gw_ip.s_addr) {
197 /* Destination: (default) */
198 struct sockaddr_in dst, gw;
199 bzero(&dst, sizeof(dst));
200 dst.sin_len = sizeof(dst);
201 dst.sin_family = AF_INET;
202 /* Gateway: */
203 bzero(&gw, sizeof(gw));
204 gw.sin_len = sizeof(gw);
205 gw.sin_family = AF_INET;
206 gw.sin_addr.s_addr = gw_ip.s_addr;
207 /* Netmask: */
208 error = ifioctl(so, SIOCGIFNETMASK, (caddr_t)&ireq, procp);
209 if (error)
210 panic("nfs_boot: get netmask, error=%d", error);
211
212 /* add, dest, gw, mask, flags, 0 */
213 error = rtrequest(RTM_ADD, &dst, &gw, &ifr.ifr_addr,
214 (RTF_UP | RTF_GATEWAY), NULL);
215 if (error)
216 printf("nfs_boot: add route, error=%d\n", error);
217 }
218 #endif
219
220 get_path_and_handle(&bp_sin, "root", &nd->nd_root);
221 get_path_and_handle(&bp_sin, "swap", &nd->nd_swap);
222
223 return (0);
224 }
225
226 static void
227 get_path_and_handle(bpsin, key, ndmntp)
228 struct sockaddr_in *bpsin; /* bootparam server */
229 char *key; /* root or swap */
230 struct nfs_dlmount *ndmntp; /* output */
231 {
232 char pathname[MAXPATHLEN];
233 char *sp, *dp, *endp;
234 int error;
235
236 /*
237 * Get server:pathname for "key" (root or swap)
238 * using RPC to bootparam/getfile
239 */
240 error = bp_getfile(bpsin, key, &ndmntp->ndm_saddr,
241 ndmntp->ndm_host, pathname);
242 if (error)
243 panic("nfs_boot: bootparam get %s: %d", key, error);
244 printf("%s on %s:%s\n", key, ndmntp->ndm_host, pathname);
245
246 /*
247 * Get file handle for "key" (root or swap)
248 * using RPC to mountd/mount
249 */
250 error = md_mount(&ndmntp->ndm_saddr, pathname, ndmntp->ndm_fh);
251 if (error)
252 panic("nfs_boot: mountd %s, error=%d", key, error);
253
254 /* Construct remote path (for getmntinfo(3)) */
255 dp = ndmntp->ndm_host;
256 endp = dp + MNAMELEN - 1;
257 dp += strlen(dp);
258 *dp++ = ':';
259 for (sp = pathname; *sp && dp < endp;)
260 *dp++ = *sp++;
261 *dp = '\0';
262
263 }
264
265
266 /*
267 * Get an mbuf with the given length, and
268 * initialize the pkthdr length field.
269 */
270 static struct mbuf *
271 m_get_len(int msg_len)
272 {
273 struct mbuf *m;
274 m = m_gethdr(M_WAIT, MT_DATA);
275 if (m == NULL)
276 return NULL;
277 if (msg_len > MHLEN) {
278 if (msg_len > MCLBYTES)
279 panic("nfs_boot: msg_len > MCLBYTES");
280 MCLGET(m, M_WAIT);
281 if (m == NULL)
282 return NULL;
283 }
284 m->m_len = msg_len;
285 m->m_pkthdr.len = m->m_len;
286 return (m);
287 }
288
289
290 /*
291 * String representation for RPC.
292 */
293 struct rpc_string {
294 u_long len; /* length without null or padding */
295 u_char data[4]; /* data (longer, of course) */
296 /* data is padded to a long-word boundary */
297 };
298 /* Compute space used given string length. */
299 #define RPC_STR_SIZE(slen) (4 + ((slen + 3) & ~3))
300
301 /*
302 * Inet address in RPC messages
303 * (Note, really four longs, NOT chars. Blech.)
304 */
305 struct bp_inaddr {
306 u_long atype;
307 long addr[4];
308 };
309
310
311 /*
312 * RPC definitions for bootparamd
313 * (XXX - move to a header file?)
314 */
315 #define BOOTPARAM_PROG 100026
316 #define BOOTPARAM_VERS 1
317 #define BOOTPARAM_WHOAMI 1
318 #define BOOTPARAM_GETFILE 2
319
320
321 /*
322 * RPC: bootparam/whoami
323 * Given client IP address, get:
324 * client name (hostname)
325 * domain name (domainname)
326 * gateway address
327 *
328 * Setting the hostname and domainname here may be somewhat
329 * controvercial, but it is so easy to do it here. -gwr
330 */
331 static int
332 bp_whoami(bpsin, my_ip, gw_ip)
333 struct sockaddr_in *bpsin;
334 struct in_addr *my_ip;
335 struct in_addr *gw_ip;
336 {
337 /* The RPC structures */
338 struct bp_inaddr *bia;
339 struct rpc_string *str;
340 struct mbuf *m;
341 struct sockaddr_in *sin;
342 int error, msg_len;
343 int cn_len, dn_len;
344 u_char *p;
345
346 /*
347 * Get message buffer of sufficient size.
348 */
349 msg_len = sizeof(*bia);
350 m = m_get_len(msg_len);
351 if (m == NULL)
352 return ENOBUFS;
353
354 /*
355 * Build request message.
356 */
357 /* client IP address */
358 bia = mtod(m, struct bp_inaddr *);
359 bia->atype = htonl(1);
360 p = (u_char*)my_ip; /* ugh! */
361 bia->addr[0] = htonl(*p);
362 p++;
363 bia->addr[1] = htonl(*p);
364 p++;
365 bia->addr[2] = htonl(*p);
366 p++;
367 bia->addr[3] = htonl(*p);
368 p++;
369
370 /* RPC: bootparam/whoami */
371 error = krpc_call((struct sockaddr *)bpsin,
372 BOOTPARAM_PROG, BOOTPARAM_VERS, BOOTPARAM_WHOAMI, &m);
373 if (error)
374 return error;
375
376 /*
377 * Parse result message.
378 */
379 msg_len = m->m_len;
380 p = mtod(m, char *);
381
382 /* client name */
383 if (msg_len < sizeof(*str))
384 goto bad;
385 str = (struct rpc_string *)p;
386 cn_len = ntohl(str->len);
387 if (msg_len < cn_len)
388 goto bad;
389 if (cn_len >= MAXHOSTNAMELEN)
390 goto bad;
391 bcopy(str->data, hostname, cn_len);
392 hostname[cn_len] = '\0';
393 hostnamelen = cn_len;
394 p += RPC_STR_SIZE(cn_len);
395 msg_len -= RPC_STR_SIZE(cn_len);
396
397 /* domain name */
398 if (msg_len < sizeof(*str))
399 goto bad;
400 str = (struct rpc_string *)p;
401 dn_len = ntohl(str->len);
402 if (msg_len < dn_len)
403 goto bad;
404 if (dn_len >= MAXHOSTNAMELEN)
405 goto bad;
406 bcopy(str->data, domainname, dn_len);
407 domainname[dn_len] = '\0';
408 domainnamelen = dn_len;
409 p += RPC_STR_SIZE(dn_len);
410 msg_len -= RPC_STR_SIZE(dn_len);
411
412 /* gateway address */
413 if (msg_len < sizeof(*bia))
414 goto bad;
415 bia = (struct bp_inaddr *)p;
416 if (bia->atype != htonl(1))
417 goto bad;
418 p = (u_char*)gw_ip;
419 *p++ = ntohl(bia->addr[0]);
420 *p++ = ntohl(bia->addr[1]);
421 *p++ = ntohl(bia->addr[2]);
422 *p++ = ntohl(bia->addr[3]);
423 goto out;
424
425 bad:
426 printf("nfs_boot: bootparam_whoami: bad reply\n");
427 error = EBADRPC;
428
429 out:
430 m_freem(m);
431 return(error);
432 }
433
434
435 /*
436 * RPC: bootparam/getfile
437 * Given client name and file "key", get:
438 * server name
439 * server IP address
440 * server pathname
441 */
442 static int
443 bp_getfile(bpsin, key, md_sin, serv_name, pathname)
444 struct sockaddr_in *bpsin;
445 char *key;
446 struct sockaddr_in *md_sin;
447 char *serv_name;
448 char *pathname;
449 {
450 struct rpc_string *str;
451 struct mbuf *m;
452 struct bp_inaddr *bia;
453 struct sockaddr_in *sin;
454 u_char *p, *q;
455 int error, msg_len;
456 int cn_len, key_len, sn_len, path_len;
457
458 /*
459 * Get message buffer of sufficient size.
460 */
461 cn_len = hostnamelen;
462 key_len = strlen(key);
463 msg_len = 0;
464 msg_len += RPC_STR_SIZE(cn_len);
465 msg_len += RPC_STR_SIZE(key_len);
466 m = m_get_len(msg_len);
467 if (m == NULL)
468 return ENOBUFS;
469
470 /*
471 * Build request message.
472 */
473 p = mtod(m, u_char *);
474 bzero(p, msg_len);
475 /* client name (hostname) */
476 str = (struct rpc_string *)p;
477 str->len = htonl(cn_len);
478 bcopy(hostname, str->data, cn_len);
479 p += RPC_STR_SIZE(cn_len);
480 /* key name (root or swap) */
481 str = (struct rpc_string *)p;
482 str->len = htonl(key_len);
483 bcopy(key, str->data, key_len);
484
485 /* RPC: bootparam/getfile */
486 error = krpc_call((struct sockaddr *)bpsin,
487 BOOTPARAM_PROG, BOOTPARAM_VERS, BOOTPARAM_GETFILE, &m);
488 if (error)
489 return error;
490
491 /*
492 * Parse result message.
493 */
494 p = mtod(m, u_char *);
495 msg_len = m->m_len;
496
497 /* server name */
498 if (msg_len < sizeof(*str))
499 goto bad;
500 str = (struct rpc_string *)p;
501 sn_len = ntohl(str->len);
502 if (msg_len < sn_len)
503 goto bad;
504 if (sn_len >= MNAMELEN)
505 goto bad;
506 bcopy(str->data, serv_name, sn_len);
507 serv_name[sn_len] = '\0';
508 p += RPC_STR_SIZE(sn_len);
509 msg_len -= RPC_STR_SIZE(sn_len);
510
511 /* server IP address (mountd) */
512 if (msg_len < sizeof(*bia))
513 goto bad;
514 bia = (struct bp_inaddr *)p;
515 if (bia->atype != htonl(1))
516 goto bad;
517 sin = md_sin;
518 sin->sin_len = sizeof(*sin);
519 sin->sin_family = AF_INET;
520 q = (u_char*) &sin->sin_addr;
521 *q++ = ntohl(bia->addr[0]);
522 *q++ = ntohl(bia->addr[1]);
523 *q++ = ntohl(bia->addr[2]);
524 *q++ = ntohl(bia->addr[3]);
525 p += sizeof(*bia);
526 msg_len -= sizeof(*bia);
527
528 /* server pathname */
529 if (msg_len < sizeof(*str))
530 goto bad;
531 str = (struct rpc_string *)p;
532 path_len = ntohl(str->len);
533 if (msg_len < path_len)
534 goto bad;
535 if (path_len >= MAXPATHLEN)
536 goto bad;
537 bcopy(str->data, pathname, path_len);
538 pathname[path_len] = '\0';
539 goto out;
540
541 bad:
542 printf("nfs_boot: bootparam_getfile: bad reply\n");
543 error = EBADRPC;
544
545 out:
546 m_freem(m);
547 return(0);
548 }
549
550
551 /*
552 * RPC: mountd/mount
553 * Given a server pathname, get an NFS file handle.
554 * Also, sets sin->sin_port to the NFS service port.
555 */
556 static int
557 md_mount(mdsin, path, fhp)
558 struct sockaddr_in *mdsin; /* mountd server address */
559 char *path;
560 u_char *fhp;
561 {
562 /* The RPC structures */
563 struct rpc_string *str;
564 struct rdata {
565 u_long errno;
566 u_char fh[NFS_FHSIZE];
567 } *rdata;
568 struct mbuf *m;
569 int error, mlen, slen;
570
571 slen = strlen(path);
572 mlen = RPC_STR_SIZE(slen);
573
574 m = m_get_len(mlen);
575 if (m == NULL)
576 return ENOBUFS;
577 str = mtod(m, struct rpc_string *);
578 str->len = htonl(slen);
579 bcopy(path, str->data, slen);
580
581 /* Do RPC to mountd. */
582 error = krpc_call((struct sockaddr *)mdsin,
583 RPCPROG_MNT, RPCMNT_VER1, RPCMNT_MOUNT, &m);
584 if (error)
585 return error; /* message already freed */
586
587 mlen = m->m_len;
588 if (mlen < sizeof(*rdata))
589 goto bad;
590 rdata = mtod(m, struct rdata *);
591 error = ntohl(rdata->errno);
592 if (error)
593 goto bad;
594 bcopy(rdata->fh, fhp, NFS_FHSIZE);
595
596 /* Set port number for NFS use. */
597 error = krpc_portmap((struct sockaddr *)mdsin,
598 NFS_PROG, NFS_VER2, &mdsin->sin_port);
599 goto out;
600
601 bad:
602 error = EBADRPC;
603
604 out:
605 m_freem(m);
606 return error;
607 }
608
609 #else /* NETHER */
610
611 int nfs_boot_init(nd, procp)
612 struct nfs_diskless *nd;
613 struct proc *procp;
614 {
615 panic("nfs_boot_init: no ether");
616 }
617
618 #endif /* NETHER */
619