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