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