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