nfs_bootparam.c revision 1.5 1 /* $NetBSD: nfs_bootparam.c,v 1.5 1998/01/09 15:16:55 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, Sun-style (RPC/bootparams)
41 */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/conf.h>
47 #include <sys/device.h>
48 #include <sys/ioctl.h>
49 #include <sys/proc.h>
50 #include <sys/mount.h>
51 #include <sys/mbuf.h>
52 #include <sys/reboot.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55
56 #include <net/if.h>
57 #include <net/if_types.h>
58 #include <net/route.h>
59 #include <net/if_ether.h>
60
61 #include <netinet/in.h>
62 #include <netinet/if_inarp.h>
63
64 #include <nfs/rpcv2.h>
65 #include <nfs/krpc.h>
66 #include <nfs/xdr_subs.h>
67
68 #include <nfs/nfsproto.h>
69 #include <nfs/nfsdiskless.h>
70
71 #include "arp.h"
72
73 /*
74 * There are two implementations of NFS diskless boot.
75 * This implementation uses Sun RPC/bootparams, and the
76 * the other uses BOOTP (RFC951 - see nfs_bootdhcp.c).
77 *
78 * The Sun-style boot sequence goes as follows:
79 * (1) Use RARP to get our interface address
80 * (2) Use RPC/bootparam/whoami to get our hostname,
81 * our IP address, and the server's IP address.
82 * (3) Use RPC/bootparam/getfile to get the root path
83 * (4) Use RPC/mountd to get the root file handle
84 * (5) Use RPC/bootparam/getfile to get the swap path
85 * (6) Use RPC/mountd to get the swap file handle
86 */
87
88 /* bootparam RPC */
89 static int bp_whoami __P((struct sockaddr_in *bpsin,
90 struct in_addr *my_ip, struct in_addr *gw_ip));
91 static int bp_getfile __P((struct sockaddr_in *bpsin, char *key,
92 struct nfs_dlmount *ndm));
93
94
95 /*
96 * Get client name, gateway address, then
97 * get root and swap server:pathname info.
98 * RPCs: bootparam/whoami, bootparam/getfile
99 *
100 * Use the old broadcast address for the WHOAMI
101 * call because we do not yet know our netmask.
102 * The server address returned by the WHOAMI call
103 * is used for all subsequent booptaram RPCs.
104 */
105 int
106 nfs_bootparam(ifp, nd, procp)
107 struct ifnet *ifp;
108 struct nfs_diskless *nd;
109 struct proc *procp;
110 {
111 struct ifreq ireq;
112 struct in_addr my_ip, gw_ip;
113 struct sockaddr_in bp_sin;
114 struct sockaddr_in *sin;
115 struct socket *so;
116 struct nfs_dlmount *gw_ndm;
117 #if 0 /* XXX - not yet */
118 char *p;
119 u_int32_t mask;
120 #endif /* XXX */
121 int error;
122
123 gw_ndm = 0;
124 bzero(&ireq, sizeof(ireq));
125 bcopy(ifp->if_xname, ireq.ifr_name, IFNAMSIZ);
126
127 /*
128 * Get a socket to use for various things in here.
129 * After this, use "goto out" to cleanup and return.
130 */
131 error = socreate(AF_INET, &so, SOCK_DGRAM, 0);
132 if (error) {
133 printf("nfs_boot: socreate, error=%d\n", error);
134 return (error);
135 }
136
137 /*
138 * Bring up the interface. (just set the "up" flag)
139 * Get the old interface flags and or IFF_UP into them so
140 * things like media selection flags are not clobbered.
141 */
142 error = ifioctl(so, SIOCGIFFLAGS, (caddr_t)&ireq, procp);
143 if (error) {
144 printf("nfs_boot: GIFFLAGS, error=%d\n", error);
145 goto out;
146 }
147 ireq.ifr_flags |= IFF_UP;
148 error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)&ireq, procp);
149 if (error) {
150 printf("nfs_boot: SIFFLAGS, error=%d\n", error);
151 goto out;
152 }
153
154 error = EADDRNOTAVAIL; /* ??? */
155 #if NARP > 0
156 if (ifp->if_type == IFT_ETHER || ifp->if_type == IFT_FDDI) {
157 /*
158 * Do RARP for the interface address.
159 */
160 error = revarpwhoami(&my_ip, ifp);
161 if (!error)
162 goto ok;
163 printf("revarp failed, error=%d\n", error);
164 }
165 #endif
166 if (0) goto ok; /* XXX stupid gcc */
167 goto out;
168
169 ok:
170 nd->nd_myip.s_addr = my_ip.s_addr;
171 printf("nfs_boot: client_addr=0x%x\n",
172 (u_int32_t)ntohl(my_ip.s_addr));
173
174 /*
175 * Do enough of ifconfig(8) so that the chosen interface
176 * can talk to the servers. (just set the address)
177 */
178 sin = (struct sockaddr_in *)&ireq.ifr_addr;
179 sin->sin_len = sizeof(*sin);
180 sin->sin_family = AF_INET;
181 sin->sin_addr = my_ip;
182 error = ifioctl(so, SIOCSIFADDR, (caddr_t)&ireq, procp);
183 if (error) {
184 printf("nfs_boot: set ifaddr, error=%d\n", error);
185 goto out;
186 }
187
188 /*
189 * Get client name and gateway address.
190 * RPC: bootparam/whoami
191 * Use the old broadcast address for the WHOAMI
192 * call because we do not yet know our netmask.
193 * The server address returned by the WHOAMI call
194 * is used for all subsequent booptaram RPCs.
195 */
196 sin = &bp_sin;
197 bzero((caddr_t)sin, sizeof(*sin));
198 sin->sin_len = sizeof(*sin);
199 sin->sin_family = AF_INET;
200 sin->sin_addr.s_addr = INADDR_BROADCAST;
201
202 /* Do the RPC/bootparam/whoami. */
203 error = bp_whoami(sin, &my_ip, &gw_ip);
204 if (error) {
205 printf("nfs_boot: bootparam whoami, error=%d\n", error);
206 goto out;
207 }
208 printf("nfs_boot: server_addr=0x%x\n",
209 (u_int32_t)ntohl(sin->sin_addr.s_addr));
210 printf("nfs_boot: hostname=%s\n", hostname);
211
212 /*
213 * Now fetch the server:pathname strings and server IP
214 * for root and swap. Missing swap is not fatal.
215 */
216 error = bp_getfile(sin, "root", &nd->nd_root);
217 if (error) {
218 printf("nfs_boot: bootparam get root: %d\n", error);
219 goto out;
220 }
221 #if 0
222 error = bp_getfile(sin, "swap", &nd->nd_swap);
223 if (error) {
224 printf("nfs_boot: bootparam get swap: %d\n", error);
225 error = 0;
226 }
227 #endif
228
229 #ifdef NFS_BOOT_GATEWAY
230 /*
231 * Note: we normally ignore the gateway address returned
232 * by the "bootparam/whoami" RPC above, because many old
233 * bootparam servers supply a bogus gateway value.
234 *
235 * These deficiencies in the bootparam RPC interface are
236 * circumvented by using the bootparam/getfile RPC. The
237 * parameter "gateway" is requested, and if its returned,
238 * we use the "server" part of the reply as the gateway,
239 * and use the "pathname" part of the reply as the mask.
240 * (The mask comes to us as a string.)
241 */
242 if (gw_ip.s_addr) {
243 /* Our caller will add the route. */
244 nd->nd_gwip = gw_ip;
245 }
246 #endif
247 #if 0 /* XXX - not yet */
248 gw_ndm = malloc(sizeof(*gw_ndm), M_NFSMNT, M_WAITOK);
249 bzero((caddr_t)gw_ndm, sizeof(*gw_ndm));
250 error = bp_getfile(sin, "gateway", gw_ndm);
251 if (error) {
252 /* Ignore the error. No gateway supplied. */
253 error = 0;
254 goto out;
255 }
256 printf("nfs_boot: gateway=%s\n", gw_ndm->ndm_host);
257 sin = (struct sockaddr_in *) &gw_ndm->ndm_saddr;
258 nd->nd_gwip = sin->sin_addr;
259 /* Find the pathname part of the "mounted-on" string. */
260 p = strchr(gw_ndm->ndm_host, ':');
261 if (p == 0)
262 goto out;
263 /* have pathname */
264 p++; /* skip ':' */
265 mask = inet_addr(p); /* libkern */
266 if (mask == 0) {
267 printf("nfs_boot: gateway netmask missing\n");
268 goto out;
269 }
270
271 /* Save our netmask and update the network interface. */
272 nd->nd_mask.s_addr = mask;
273 sin = (struct sockaddr_in *)&ireq.ifr_addr;
274 sin->sin_len = sizeof(*sin);
275 sin->sin_family = AF_INET;
276 sin->sin_addr = nd->nd_mask;
277 error = ifioctl(so, SIOCSIFNETMASK, (caddr_t)&ireq, procp);
278 if (error) {
279 printf("nfs_boot: set ifmask, error=%d\n", error);
280 error = 0; /* ignore it */
281 }
282 #endif /* XXX */
283
284 out:
285 if (gw_ndm)
286 free(gw_ndm, M_NFSMNT);
287 soclose(so);
288 return (error);
289 }
290
291
292 /*
293 * RPC: bootparam/whoami
294 * Given client IP address, get:
295 * client name (hostname)
296 * domain name (domainname)
297 * gateway address
298 *
299 * The hostname and domainname are set here for convenience.
300 *
301 * Note - bpsin is initialized to the broadcast address,
302 * and will be replaced with the bootparam server address
303 * after this call is complete. Have to use PMAP_PROC_CALL
304 * to make sure we get responses only from a servers that
305 * know about us (don't want to broadcast a getport call).
306 */
307 static int
308 bp_whoami(bpsin, my_ip, gw_ip)
309 struct sockaddr_in *bpsin;
310 struct in_addr *my_ip;
311 struct in_addr *gw_ip;
312 {
313 /* RPC structures for PMAPPROC_CALLIT */
314 struct whoami_call {
315 u_int32_t call_prog;
316 u_int32_t call_vers;
317 u_int32_t call_proc;
318 u_int32_t call_arglen;
319 } *call;
320 struct callit_reply {
321 u_int32_t port;
322 u_int32_t encap_len;
323 /* encapsulated data here */
324 } *reply;
325
326 struct mbuf *m, *from;
327 struct sockaddr_in *sin;
328 int error, msg_len;
329 int16_t port;
330
331 /*
332 * Build request message for PMAPPROC_CALLIT.
333 */
334 m = m_get(M_WAIT, MT_DATA);
335 call = mtod(m, struct whoami_call *);
336 m->m_len = sizeof(*call);
337 call->call_prog = txdr_unsigned(BOOTPARAM_PROG);
338 call->call_vers = txdr_unsigned(BOOTPARAM_VERS);
339 call->call_proc = txdr_unsigned(BOOTPARAM_WHOAMI);
340
341 /*
342 * append encapsulated data (client IP address)
343 */
344 m->m_next = xdr_inaddr_encode(my_ip);
345 call->call_arglen = txdr_unsigned(m->m_next->m_len);
346
347 /* RPC: portmap/callit */
348 bpsin->sin_port = htons(PMAPPORT);
349 from = NULL;
350 error = krpc_call(bpsin, PMAPPROG, PMAPVERS,
351 PMAPPROC_CALLIT, &m, &from);
352 if (error)
353 return error;
354
355 /*
356 * Parse result message.
357 */
358 if (m->m_len < sizeof(*reply)) {
359 m = m_pullup(m, sizeof(*reply));
360 if (m == NULL)
361 goto bad;
362 }
363 reply = mtod(m, struct callit_reply *);
364 port = fxdr_unsigned(u_int32_t, reply->port);
365 msg_len = fxdr_unsigned(u_int32_t, reply->encap_len);
366 m_adj(m, sizeof(*reply));
367
368 /*
369 * Save bootparam server address
370 */
371 sin = mtod(from, struct sockaddr_in *);
372 bpsin->sin_port = htons(port);
373 bpsin->sin_addr.s_addr = sin->sin_addr.s_addr;
374
375 /* client name */
376 hostnamelen = MAXHOSTNAMELEN-1;
377 m = xdr_string_decode(m, hostname, &hostnamelen);
378 if (m == NULL)
379 goto bad;
380
381 /* domain name */
382 domainnamelen = MAXHOSTNAMELEN-1;
383 m = xdr_string_decode(m, domainname, &domainnamelen);
384 if (m == NULL)
385 goto bad;
386
387 /* gateway address */
388 m = xdr_inaddr_decode(m, gw_ip);
389 if (m == NULL)
390 goto bad;
391
392 /* success */
393 goto out;
394
395 bad:
396 printf("nfs_boot: bootparam_whoami: bad reply\n");
397 error = EBADRPC;
398
399 out:
400 if (from)
401 m_freem(from);
402 if (m)
403 m_freem(m);
404 return(error);
405 }
406
407
408 /*
409 * RPC: bootparam/getfile
410 * Given client name and file "key", get:
411 * server name
412 * server IP address
413 * server pathname
414 */
415 static int
416 bp_getfile(bpsin, key, ndm)
417 struct sockaddr_in *bpsin;
418 char *key;
419 struct nfs_dlmount *ndm;
420 {
421 char pathname[MNAMELEN];
422 struct in_addr inaddr;
423 struct sockaddr_in *sin;
424 struct mbuf *m;
425 char *serv_name;
426 int error, sn_len, path_len;
427
428 /*
429 * Build request message.
430 */
431
432 /* client name (hostname) */
433 m = xdr_string_encode(hostname, hostnamelen);
434 if (m == NULL)
435 return (ENOMEM);
436
437 /* key name (root or swap) */
438 m->m_next = xdr_string_encode(key, strlen(key));
439 if (m->m_next == NULL)
440 return (ENOMEM);
441
442 /* RPC: bootparam/getfile */
443 error = krpc_call(bpsin, BOOTPARAM_PROG, BOOTPARAM_VERS,
444 BOOTPARAM_GETFILE, &m, NULL);
445 if (error)
446 return error;
447
448 /*
449 * Parse result message.
450 */
451
452 /* server name */
453 serv_name = &ndm->ndm_host[0];
454 sn_len = sizeof(ndm->ndm_host) - 1;
455 m = xdr_string_decode(m, serv_name, &sn_len);
456 if (m == NULL)
457 goto bad;
458
459 /* server IP address (mountd/NFS) */
460 m = xdr_inaddr_decode(m, &inaddr);
461 if (m == NULL)
462 goto bad;
463
464 /* server pathname */
465 path_len = sizeof(pathname) - 1;
466 m = xdr_string_decode(m, pathname, &path_len);
467 if (m == NULL)
468 goto bad;
469
470 /*
471 * Store the results in the nfs_dlmount.
472 * The strings become "server:pathname"
473 */
474 sin = (struct sockaddr_in *) &ndm->ndm_saddr;
475 bzero((caddr_t)sin, sizeof(*sin));
476 sin->sin_len = sizeof(*sin);
477 sin->sin_family = AF_INET;
478 sin->sin_addr = inaddr;
479 if ((sn_len + 1 + path_len + 1) > sizeof(ndm->ndm_host)) {
480 printf("nfs_boot: getfile name too long\n");
481 error = EIO;
482 goto out;
483 }
484 ndm->ndm_host[sn_len] = ':';
485 bcopy(pathname, ndm->ndm_host + sn_len + 1, path_len + 1);
486
487 /* success */
488 goto out;
489
490 bad:
491 printf("nfs_boot: bootparam_getfile: bad reply\n");
492 error = EBADRPC;
493
494 out:
495 m_freem(m);
496 return(0);
497 }
498
499
500