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