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