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