nfs_bootparam.c revision 1.33 1 /* $NetBSD: nfs_bootparam.c,v 1.33 2008/10/24 17:17:12 cegger 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Support for NFS diskless booting, Sun-style (RPC/bootparams)
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: nfs_bootparam.c,v 1.33 2008/10/24 17:17:12 cegger Exp $");
38
39 #include "opt_nfs_boot.h"
40 #include "opt_inet.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/ioctl.h>
46 #include <sys/proc.h>
47 #include <sys/mount.h>
48 #include <sys/mbuf.h>
49 #include <sys/reboot.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52
53 #include <net/if.h>
54 #include <net/if_types.h>
55 #include <net/route.h>
56 #include <net/if_ether.h>
57
58 #include <netinet/in.h>
59 #include <netinet/if_inarp.h>
60
61 #include <nfs/rpcv2.h>
62 #include <nfs/krpc.h>
63 #include <nfs/xdr_subs.h>
64
65 #include <nfs/nfsproto.h>
66 #include <nfs/nfs.h>
67 #include <nfs/nfsmount.h>
68 #include <nfs/nfsdiskless.h>
69 #include <nfs/nfs_var.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 (struct sockaddr_in *bpsin,
90 struct in_addr *my_ip, struct in_addr *gw_ip, struct lwp *l);
91 static int bp_getfile (struct sockaddr_in *bpsin, const char *key,
92 struct nfs_dlmount *ndm, struct lwp *l);
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(struct nfs_diskless *nd, struct lwp *lwp)
107 {
108 struct ifnet *ifp = nd->nd_ifp;
109 struct in_addr my_ip, arps_ip, gw_ip;
110 struct sockaddr_in bp_sin;
111 struct sockaddr_in *sin;
112 #ifndef NFS_BOOTPARAM_NOGATEWAY
113 struct nfs_dlmount *gw_ndm = 0;
114 char *p;
115 u_int32_t mask;
116 #endif
117 int error;
118
119 /*
120 * Bring up the interface. (just set the "up" flag)
121 */
122 error = nfs_boot_ifupdown(ifp, lwp, 1);
123 if (error) {
124 printf("nfs_boot: SIFFLAGS, error=%d\n", error);
125 return (error);
126 }
127
128 error = EADDRNOTAVAIL;
129 #ifdef INET
130 #if NARP > 0
131 if (ifp->if_type == IFT_ETHER || ifp->if_type == IFT_FDDI) {
132 /*
133 * Do RARP for the interface address.
134 */
135 error = revarpwhoarewe(ifp, &arps_ip, &my_ip);
136 }
137 #endif
138 #endif
139 if (error) {
140 printf("revarp failed, error=%d\n", error);
141 goto out;
142 }
143
144 nd->nd_myip.s_addr = my_ip.s_addr;
145 printf("nfs_boot: client_addr=%s", inet_ntoa(my_ip));
146 printf(" (RARP from %s)\n", inet_ntoa(arps_ip));
147
148 /*
149 * Do enough of ifconfig(8) so that the chosen interface
150 * can talk to the servers. (just set the address)
151 */
152 error = nfs_boot_setaddress(ifp, lwp, my_ip.s_addr,
153 INADDR_ANY, INADDR_ANY);
154 if (error) {
155 printf("nfs_boot: set ifaddr, error=%d\n", error);
156 goto out;
157 }
158
159 /*
160 * Get client name and gateway address.
161 * RPC: bootparam/whoami
162 * Use the old broadcast address for the WHOAMI
163 * call because we do not yet know our netmask.
164 * The server address returned by the WHOAMI call
165 * is used for all subsequent booptaram RPCs.
166 */
167 sin = &bp_sin;
168 memset((void *)sin, 0, sizeof(*sin));
169 sin->sin_len = sizeof(*sin);
170 sin->sin_family = AF_INET;
171 sin->sin_addr.s_addr = INADDR_BROADCAST;
172
173 /* Do the RPC/bootparam/whoami. */
174 error = bp_whoami(sin, &my_ip, &gw_ip, lwp);
175 if (error) {
176 printf("nfs_boot: bootparam whoami, error=%d\n", error);
177 goto delout;
178 }
179 printf("nfs_boot: server_addr=%s\n", inet_ntoa(sin->sin_addr));
180 printf("nfs_boot: hostname=%s\n", hostname);
181
182 /*
183 * Now fetch the server:pathname strings and server IP
184 * for root and swap. Missing swap is not fatal.
185 */
186 error = bp_getfile(sin, "root", &nd->nd_root, lwp);
187 if (error) {
188 printf("nfs_boot: bootparam get root: %d\n", error);
189 goto delout;
190 }
191
192 #ifndef NFS_BOOTPARAM_NOGATEWAY
193 gw_ndm = kmem_alloc(sizeof(*gw_ndm), KM_SLEEP);
194 memset((void *)gw_ndm, 0, sizeof(*gw_ndm));
195 error = bp_getfile(sin, "gateway", gw_ndm, lwp);
196 if (error) {
197 /* No gateway supplied. No error, but try fallback. */
198 error = 0;
199 goto nogwrepl;
200 }
201 sin = (struct sockaddr_in *) &gw_ndm->ndm_saddr;
202 if (sin->sin_addr.s_addr == 0)
203 goto out; /* no gateway */
204
205 /* OK, we have a gateway! */
206 printf("nfs_boot: gateway=%s\n", inet_ntoa(sin->sin_addr));
207 /* Just save it. Caller adds the route. */
208 nd->nd_gwip = sin->sin_addr;
209
210 /* Look for a mask string after the colon. */
211 p = strchr(gw_ndm->ndm_host, ':');
212 if (p == 0)
213 goto out; /* no netmask */
214 /* have pathname */
215 p++; /* skip ':' */
216 mask = inet_addr(p); /* libkern */
217 if (mask == 0)
218 goto out; /* no netmask */
219
220 /* Have a netmask too! Save it; update the I/F. */
221 nd->nd_mask.s_addr = mask;
222 printf("nfs_boot: my_mask=%s\n", inet_ntoa(nd->nd_mask));
223 (void) nfs_boot_deladdress(ifp, lwp, my_ip.s_addr);
224 error = nfs_boot_setaddress(ifp, lwp, my_ip.s_addr,
225 mask, INADDR_ANY);
226 if (error) {
227 printf("nfs_boot: set ifmask, error=%d\n", error);
228 goto out;
229 }
230 goto gwok;
231 nogwrepl:
232 #endif
233 #ifdef NFS_BOOT_GATEWAY
234 /*
235 * Note: we normally ignore the gateway address returned
236 * by the "bootparam/whoami" RPC above, because many old
237 * bootparam servers supply a bogus gateway value.
238 *
239 * These deficiencies in the bootparam RPC interface are
240 * circumvented by using the bootparam/getfile RPC. The
241 * parameter "gateway" is requested, and if its returned,
242 * we use the "server" part of the reply as the gateway,
243 * and use the "pathname" part of the reply as the mask.
244 * (The mask comes to us as a string.)
245 */
246 if (gw_ip.s_addr) {
247 /* Our caller will add the route. */
248 nd->nd_gwip = gw_ip;
249 }
250 #endif
251
252 delout:
253 if (error)
254 (void) nfs_boot_deladdress(ifp, lwp, my_ip.s_addr);
255 out:
256 if (error) {
257 (void) nfs_boot_ifupdown(ifp, lwp, 0);
258 nfs_boot_flushrt(ifp);
259 }
260 #ifndef NFS_BOOTPARAM_NOGATEWAY
261 gwok:
262 if (gw_ndm)
263 kmem_free(gw_ndm, sizeof(*gw_ndm));
264 #endif
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(struct sockaddr_in *bpsin, struct in_addr *my_ip,
286 struct in_addr *gw_ip, struct lwp *l)
287 {
288 /* RPC structures for PMAPPROC_CALLIT */
289 struct whoami_call {
290 u_int32_t call_prog;
291 u_int32_t call_vers;
292 u_int32_t call_proc;
293 u_int32_t call_arglen;
294 } *call;
295 struct callit_reply {
296 u_int32_t port;
297 u_int32_t encap_len;
298 /* encapsulated data here */
299 } *reply;
300
301 struct mbuf *m, *from;
302 struct sockaddr_in *sin;
303 int error;
304 int16_t port;
305
306 /*
307 * Build request message for PMAPPROC_CALLIT.
308 */
309 m = m_get(M_WAIT, MT_DATA);
310 call = mtod(m, struct whoami_call *);
311 m->m_len = sizeof(*call);
312 call->call_prog = txdr_unsigned(BOOTPARAM_PROG);
313 call->call_vers = txdr_unsigned(BOOTPARAM_VERS);
314 call->call_proc = txdr_unsigned(BOOTPARAM_WHOAMI);
315
316 /*
317 * append encapsulated data (client IP address)
318 */
319 m->m_next = xdr_inaddr_encode(my_ip);
320 call->call_arglen = txdr_unsigned(m->m_next->m_len);
321
322 /* RPC: portmap/callit */
323 bpsin->sin_port = htons(PMAPPORT);
324 error = krpc_call(bpsin, PMAPPROG, PMAPVERS,
325 PMAPPROC_CALLIT, &m, &from, l);
326 if (error) {
327 m_freem(m);
328 return error;
329 }
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 m_adj(m, sizeof(*reply));
342
343 /*
344 * Save bootparam server address
345 */
346 sin = mtod(from, struct sockaddr_in *);
347 bpsin->sin_port = htons(port);
348 bpsin->sin_addr.s_addr = sin->sin_addr.s_addr;
349
350 /* client name */
351 hostnamelen = MAXHOSTNAMELEN-1;
352 m = xdr_string_decode(m, hostname, &hostnamelen);
353 if (m == NULL)
354 goto bad;
355
356 /* domain name */
357 domainnamelen = MAXHOSTNAMELEN-1;
358 m = xdr_string_decode(m, domainname, &domainnamelen);
359 if (m == NULL)
360 goto bad;
361
362 /* gateway address */
363 m = xdr_inaddr_decode(m, gw_ip);
364 if (m == NULL)
365 goto bad;
366
367 /* success */
368 goto out;
369
370 bad:
371 printf("nfs_boot: bootparam_whoami: bad reply\n");
372 error = EBADRPC;
373
374 out:
375 m_freem(from);
376 if (m)
377 m_freem(m);
378 return(error);
379 }
380
381
382 /*
383 * RPC: bootparam/getfile
384 * Given client name and file "key", get:
385 * server name
386 * server IP address
387 * server pathname
388 */
389 static int
390 bp_getfile(struct sockaddr_in *bpsin, const char *key,
391 struct nfs_dlmount *ndm, struct lwp *l)
392 {
393 char pathname[MNAMELEN];
394 struct in_addr inaddr;
395 struct sockaddr_in *sin;
396 struct mbuf *m;
397 char *serv_name;
398 int error, sn_len, path_len;
399
400 /*
401 * Build request message.
402 */
403
404 /* client name (hostname) */
405 m = xdr_string_encode(hostname, hostnamelen);
406 if (m == NULL)
407 return (ENOMEM);
408
409 /* key name (root or swap) */
410 /*XXXUNCONST*/
411 m->m_next = xdr_string_encode(__UNCONST(key), strlen(key));
412 if (m->m_next == NULL)
413 return (ENOMEM);
414
415 /* RPC: bootparam/getfile */
416 error = krpc_call(bpsin, BOOTPARAM_PROG, BOOTPARAM_VERS,
417 BOOTPARAM_GETFILE, &m, NULL, l);
418 if (error)
419 return error;
420
421 /*
422 * Parse result message.
423 */
424
425 /* server name */
426 serv_name = &ndm->ndm_host[0];
427 sn_len = sizeof(ndm->ndm_host) - 1;
428 m = xdr_string_decode(m, serv_name, &sn_len);
429 if (m == NULL)
430 goto bad;
431
432 /* server IP address (mountd/NFS) */
433 m = xdr_inaddr_decode(m, &inaddr);
434 if (m == NULL)
435 goto bad;
436
437 /* server pathname */
438 path_len = sizeof(pathname) - 1;
439 m = xdr_string_decode(m, pathname, &path_len);
440 if (m == NULL)
441 goto bad;
442
443 /*
444 * Store the results in the nfs_dlmount.
445 * The strings become "server:pathname"
446 */
447 sin = (struct sockaddr_in *) &ndm->ndm_saddr;
448 memset((void *)sin, 0, sizeof(*sin));
449 sin->sin_len = sizeof(*sin);
450 sin->sin_family = AF_INET;
451 sin->sin_addr = inaddr;
452 if ((sn_len + 1 + path_len + 1) > sizeof(ndm->ndm_host)) {
453 printf("nfs_boot: getfile name too long\n");
454 error = EIO;
455 goto out;
456 }
457 ndm->ndm_host[sn_len] = ':';
458 memcpy(ndm->ndm_host + sn_len + 1, pathname, path_len + 1);
459
460 /* success */
461 goto out;
462
463 bad:
464 printf("nfs_boot: bootparam_getfile: bad reply\n");
465 error = EBADRPC;
466
467 out:
468 m_freem(m);
469 return(0);
470 }
471