xref: /netbsd-src/sys/nfs/nfs_bootparam.c (revision 27578b9aac214cc7796ead81dcc5427e79d5f2a0)
1 /*	$NetBSD: nfs_bootparam.c,v 1.17 2000/10/03 17:18:15 chs 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 "opt_nfs_boot.h"
44 #include "opt_inet.h"
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/conf.h>
50 #include <sys/device.h>
51 #include <sys/ioctl.h>
52 #include <sys/proc.h>
53 #include <sys/mount.h>
54 #include <sys/mbuf.h>
55 #include <sys/reboot.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 
59 #include <net/if.h>
60 #include <net/if_types.h>
61 #include <net/route.h>
62 #include <net/if_ether.h>
63 
64 #include <netinet/in.h>
65 #include <netinet/if_inarp.h>
66 
67 #include <nfs/rpcv2.h>
68 #include <nfs/krpc.h>
69 #include <nfs/xdr_subs.h>
70 
71 #include <nfs/nfsproto.h>
72 #include <nfs/nfs.h>
73 #include <nfs/nfsmount.h>
74 #include <nfs/nfsdiskless.h>
75 
76 #include "arp.h"
77 
78 /*
79  * There are two implementations of NFS diskless boot.
80  * This implementation uses Sun RPC/bootparams, and the
81  * the other uses BOOTP (RFC951 - see nfs_bootdhcp.c).
82  *
83  * The Sun-style boot sequence goes as follows:
84  * (1) Use RARP to get our interface address
85  * (2) Use RPC/bootparam/whoami to get our hostname,
86  *     our IP address, and the server's IP address.
87  * (3) Use RPC/bootparam/getfile to get the root path
88  * (4) Use RPC/mountd to get the root file handle
89  * (5) Use RPC/bootparam/getfile to get the swap path
90  * (6) Use RPC/mountd to get the swap file handle
91  */
92 
93 /* bootparam RPC */
94 static int bp_whoami __P((struct sockaddr_in *bpsin,
95 	struct in_addr *my_ip, struct in_addr *gw_ip));
96 static int bp_getfile __P((struct sockaddr_in *bpsin, char *key,
97 	struct nfs_dlmount *ndm));
98 
99 
100 /*
101  * Get client name, gateway address, then
102  * get root and swap server:pathname info.
103  * RPCs: bootparam/whoami, bootparam/getfile
104  *
105  * Use the old broadcast address for the WHOAMI
106  * call because we do not yet know our netmask.
107  * The server address returned by the WHOAMI call
108  * is used for all subsequent booptaram RPCs.
109  */
110 int
111 nfs_bootparam(nd, procp)
112 	struct nfs_diskless *nd;
113 	struct proc *procp;
114 {
115 	struct ifnet *ifp = nd->nd_ifp;
116 	struct in_addr my_ip, arps_ip, gw_ip;
117 	struct sockaddr_in bp_sin;
118 	struct sockaddr_in *sin;
119 #ifndef NFS_BOOTPARAM_NOGATEWAY
120 	struct nfs_dlmount *gw_ndm = 0;
121 	char *p;
122 	u_int32_t mask;
123 #endif
124 	int error;
125 
126 	/*
127 	 * Bring up the interface. (just set the "up" flag)
128 	 */
129 	error = nfs_boot_ifupdown(ifp, procp, 1);
130 	if (error) {
131 		printf("nfs_boot: SIFFLAGS, error=%d\n", error);
132 		return (error);
133 	}
134 
135 	error = EADDRNOTAVAIL;
136 #ifdef INET
137 #if NARP > 0
138 	if (ifp->if_type == IFT_ETHER || ifp->if_type == IFT_FDDI) {
139 		/*
140 		 * Do RARP for the interface address.
141 		 */
142 		error = revarpwhoarewe(ifp, &arps_ip, &my_ip);
143 	}
144 #endif
145 #endif
146 	if (error) {
147 		printf("revarp failed, error=%d\n", error);
148 		goto out;
149 	}
150 
151 	nd->nd_myip.s_addr = my_ip.s_addr;
152 	printf("nfs_boot: client_addr=%s", inet_ntoa(my_ip));
153 	printf(" (RARP from %s)\n", inet_ntoa(arps_ip));
154 
155 	/*
156 	 * Do enough of ifconfig(8) so that the chosen interface
157 	 * can talk to the servers.  (just set the address)
158 	 */
159 	error = nfs_boot_setaddress(ifp, procp, my_ip.s_addr,
160 				    INADDR_ANY, INADDR_ANY);
161 	if (error) {
162 		printf("nfs_boot: set ifaddr, error=%d\n", error);
163 		goto out;
164 	}
165 
166 	/*
167 	 * Get client name and gateway address.
168 	 * RPC: bootparam/whoami
169 	 * Use the old broadcast address for the WHOAMI
170 	 * call because we do not yet know our netmask.
171 	 * The server address returned by the WHOAMI call
172 	 * is used for all subsequent booptaram RPCs.
173 	 */
174 	sin = &bp_sin;
175 	memset((caddr_t)sin, 0, sizeof(*sin));
176 	sin->sin_len = sizeof(*sin);
177 	sin->sin_family = AF_INET;
178 	sin->sin_addr.s_addr = INADDR_BROADCAST;
179 
180 	/* Do the RPC/bootparam/whoami. */
181 	error = bp_whoami(sin, &my_ip, &gw_ip);
182 	if (error) {
183 		printf("nfs_boot: bootparam whoami, error=%d\n", error);
184 		goto delout;
185 	}
186 	printf("nfs_boot: server_addr=%s\n", inet_ntoa(sin->sin_addr));
187 	printf("nfs_boot: hostname=%s\n", hostname);
188 
189 	/*
190 	 * Now fetch the server:pathname strings and server IP
191 	 * for root and swap.  Missing swap is not fatal.
192 	 */
193 	error = bp_getfile(sin, "root", &nd->nd_root);
194 	if (error) {
195 		printf("nfs_boot: bootparam get root: %d\n", error);
196 		goto delout;
197 	}
198 
199 #ifndef NFS_BOOTPARAM_NOGATEWAY
200 	gw_ndm = malloc(sizeof(*gw_ndm), M_NFSMNT, M_WAITOK);
201 	memset((caddr_t)gw_ndm, 0, sizeof(*gw_ndm));
202 	error = bp_getfile(sin, "gateway", gw_ndm);
203 	if (error) {
204 		/* No gateway supplied. No error, but try fallback. */
205 		error = 0;
206 		goto nogwrepl;
207 	}
208 	sin = (struct sockaddr_in *) &gw_ndm->ndm_saddr;
209 	if (sin->sin_addr.s_addr == 0)
210 		goto out;	/* no gateway */
211 
212 	/* OK, we have a gateway! */
213 	printf("nfs_boot: gateway=%s\n", inet_ntoa(sin->sin_addr));
214 	/* Just save it.  Caller adds the route. */
215 	nd->nd_gwip = sin->sin_addr;
216 
217 	/* Look for a mask string after the colon. */
218 	p = strchr(gw_ndm->ndm_host, ':');
219 	if (p == 0)
220 		goto out;	/* no netmask */
221 	/* have pathname */
222 	p++;	/* skip ':' */
223 	mask = inet_addr(p);	/* libkern */
224 	if (mask == 0)
225 		goto out;	/* no netmask */
226 
227 	/* Have a netmask too!  Save it; update the I/F. */
228 	nd->nd_mask.s_addr = mask;
229 	printf("nfs_boot: my_mask=%s\n", inet_ntoa(nd->nd_mask));
230 	(void)  nfs_boot_deladdress(ifp, procp, my_ip.s_addr);
231 	error = nfs_boot_setaddress(ifp, procp, my_ip.s_addr,
232 				    mask, INADDR_ANY);
233 	if (error) {
234 		printf("nfs_boot: set ifmask, error=%d\n", error);
235 		goto out;
236 	}
237 	goto gwok;
238 nogwrepl:
239 #endif
240 #ifdef NFS_BOOT_GATEWAY
241 	/*
242 	 * Note: we normally ignore the gateway address returned
243 	 * by the "bootparam/whoami" RPC above, because many old
244 	 * bootparam servers supply a bogus gateway value.
245 	 *
246 	 * These deficiencies in the bootparam RPC interface are
247 	 * circumvented by using the bootparam/getfile RPC.  The
248 	 * parameter "gateway" is requested, and if its returned,
249 	 * we use the "server" part of the reply as the gateway,
250 	 * and use the "pathname" part of the reply as the mask.
251 	 * (The mask comes to us as a string.)
252 	 */
253 	if (gw_ip.s_addr) {
254 		/* Our caller will add the route. */
255 		nd->nd_gwip = gw_ip;
256 	}
257 #endif
258 
259 delout:
260 	if (error)
261 		(void) nfs_boot_deladdress(ifp, procp, my_ip.s_addr);
262 out:
263 	if (error) {
264 		(void) nfs_boot_ifupdown(ifp, procp, 0);
265 		nfs_boot_flushrt(ifp);
266 	}
267 #ifndef NFS_BOOTPARAM_NOGATEWAY
268 gwok:
269 	if (gw_ndm)
270 		free(gw_ndm, M_NFSMNT);
271 #endif
272 	return (error);
273 }
274 
275 
276 /*
277  * RPC: bootparam/whoami
278  * Given client IP address, get:
279  *	client name	(hostname)
280  *	domain name (domainname)
281  *	gateway address
282  *
283  * The hostname and domainname are set here for convenience.
284  *
285  * Note - bpsin is initialized to the broadcast address,
286  * and will be replaced with the bootparam server address
287  * after this call is complete.  Have to use PMAP_PROC_CALL
288  * to make sure we get responses only from a servers that
289  * know about us (don't want to broadcast a getport call).
290  */
291 static int
292 bp_whoami(bpsin, my_ip, gw_ip)
293 	struct sockaddr_in *bpsin;
294 	struct in_addr *my_ip;
295 	struct in_addr *gw_ip;
296 {
297 	/* RPC structures for PMAPPROC_CALLIT */
298 	struct whoami_call {
299 		u_int32_t call_prog;
300 		u_int32_t call_vers;
301 		u_int32_t call_proc;
302 		u_int32_t call_arglen;
303 	} *call;
304 	struct callit_reply {
305 		u_int32_t port;
306 		u_int32_t encap_len;
307 		/* encapsulated data here */
308 	} *reply;
309 
310 	struct mbuf *m, *from;
311 	struct sockaddr_in *sin;
312 	int error, msg_len;
313 	int16_t port;
314 
315 	/*
316 	 * Build request message for PMAPPROC_CALLIT.
317 	 */
318 	m = m_get(M_WAIT, MT_DATA);
319 	call = mtod(m, struct whoami_call *);
320 	m->m_len = sizeof(*call);
321 	call->call_prog = txdr_unsigned(BOOTPARAM_PROG);
322 	call->call_vers = txdr_unsigned(BOOTPARAM_VERS);
323 	call->call_proc = txdr_unsigned(BOOTPARAM_WHOAMI);
324 
325 	/*
326 	 * append encapsulated data (client IP address)
327 	 */
328 	m->m_next = xdr_inaddr_encode(my_ip);
329 	call->call_arglen = txdr_unsigned(m->m_next->m_len);
330 
331 	/* RPC: portmap/callit */
332 	bpsin->sin_port = htons(PMAPPORT);
333 	from = NULL;
334 	error = krpc_call(bpsin, PMAPPROG, PMAPVERS,
335 			PMAPPROC_CALLIT, &m, &from);
336 	if (error)
337 		return error;
338 
339 	/*
340 	 * Parse result message.
341 	 */
342 	if (m->m_len < sizeof(*reply)) {
343 		m = m_pullup(m, sizeof(*reply));
344 		if (m == NULL)
345 			goto bad;
346 	}
347 	reply = mtod(m, struct callit_reply *);
348 	port = fxdr_unsigned(u_int32_t, reply->port);
349 	msg_len = fxdr_unsigned(u_int32_t, reply->encap_len);
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