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