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