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