1 /* $NetBSD: nfs_bootparam.c,v 1.34 2008/10/27 10:58:22 cegger Exp $ */
4 * Copyright (c) 1995, 1997 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Adam Glass and Gordon W. Ross.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
33 * Support for NFS diskless booting, Sun-style (RPC/bootparams)
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: nfs_bootparam.c,v 1.34 2008/10/27 10:58:22 cegger Exp $");
40 #include "opt_nfs_boot.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/ioctl.h>
49 #include <sys/mount.h>
51 #include <sys/reboot.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
56 #include <net/if_types.h>
57 #include <net/route.h>
58 #include <net/if_ether.h>
60 #include <netinet/in.h>
61 #include <netinet/if_inarp.h>
63 #include <nfs/rpcv2.h>
65 #include <nfs/xdr_subs.h>
67 #include <nfs/nfsproto.h>
69 #include <nfs/nfsmount.h>
70 #include <nfs/nfsdiskless.h>
71 #include <nfs/nfs_var.h>
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).
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
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
);
96 * Get client name, gateway address, then
97 * get root and swap server:pathname info.
98 * RPCs: bootparam/whoami, bootparam/getfile
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.
106 nfs_bootparam(struct nfs_diskless
*nd
, struct lwp
*lwp
, int *flags
)
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;
120 * Bring up the interface. (just set the "up" flag)
122 error
= nfs_boot_ifupdown(ifp
, lwp
, 1);
124 printf("nfs_boot: SIFFLAGS, error=%d\n", error
);
128 error
= EADDRNOTAVAIL
;
130 if (ifp
->if_type
== IFT_ETHER
|| ifp
->if_type
== IFT_FDDI
) {
132 * Do RARP for the interface address.
134 error
= revarpwhoarewe(ifp
, &arps_ip
, &my_ip
);
138 printf("revarp failed, error=%d\n", error
);
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
;
150 * Do enough of ifconfig(8) so that the chosen interface
151 * can talk to the servers. (just set the address)
153 error
= nfs_boot_setaddress(ifp
, lwp
, my_ip
.s_addr
,
154 INADDR_ANY
, INADDR_ANY
);
156 printf("nfs_boot: set ifaddr, error=%d\n", error
);
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.
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
;
174 /* Do the RPC/bootparam/whoami. */
175 error
= bp_whoami(sin
, &my_ip
, &gw_ip
, lwp
);
177 printf("nfs_boot: bootparam whoami, error=%d\n", error
);
180 printf("nfs_boot: server_addr=%s\n", inet_ntoa(sin
->sin_addr
));
181 printf("nfs_boot: hostname=%s\n", hostname
);
184 * Now fetch the server:pathname strings and server IP
185 * for root and swap. Missing swap is not fatal.
187 error
= bp_getfile(sin
, "root", &nd
->nd_root
, lwp
);
189 printf("nfs_boot: bootparam get root: %d\n", error
);
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
);
198 /* No gateway supplied. No error, but try fallback. */
202 sin
= (struct sockaddr_in
*) &gw_ndm
->ndm_saddr
;
203 if (sin
->sin_addr
.s_addr
== 0)
204 goto out
; /* no gateway */
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
;
211 /* Look for a mask string after the colon. */
212 p
= strchr(gw_ndm
->ndm_host
, ':');
214 goto out
; /* no netmask */
217 mask
= inet_addr(p
); /* libkern */
219 goto out
; /* no netmask */
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
,
228 printf("nfs_boot: set ifmask, error=%d\n", error
);
234 #ifdef NFS_BOOT_GATEWAY
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.
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.)
248 /* Our caller will add the route. */
255 (void) nfs_boot_deladdress(ifp
, lwp
, my_ip
.s_addr
);
258 (void) nfs_boot_ifupdown(ifp
, lwp
, 0);
259 nfs_boot_flushrt(ifp
);
261 #ifndef NFS_BOOTPARAM_NOGATEWAY
264 kmem_free(gw_ndm
, sizeof(*gw_ndm
));
266 if ((*flags
& NFS_BOOT_ALLINFO
) != NFS_BOOT_ALLINFO
)
267 return error
? error
: EADDRNOTAVAIL
;
274 * RPC: bootparam/whoami
275 * Given client IP address, get:
276 * client name (hostname)
277 * domain name (domainname)
280 * The hostname and domainname are set here for convenience.
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).
289 bp_whoami(struct sockaddr_in
*bpsin
, struct in_addr
*my_ip
,
290 struct in_addr
*gw_ip
, struct lwp
*l
)
292 /* RPC structures for PMAPPROC_CALLIT */
297 u_int32_t call_arglen
;
299 struct callit_reply
{
302 /* encapsulated data here */
305 struct mbuf
*m
, *from
;
306 struct sockaddr_in
*sin
;
311 * Build request message for PMAPPROC_CALLIT.
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
);
321 * append encapsulated data (client IP address)
323 m
->m_next
= xdr_inaddr_encode(my_ip
);
324 call
->call_arglen
= txdr_unsigned(m
->m_next
->m_len
);
326 /* RPC: portmap/callit */
327 bpsin
->sin_port
= htons(PMAPPORT
);
328 error
= krpc_call(bpsin
, PMAPPROG
, PMAPVERS
,
329 PMAPPROC_CALLIT
, &m
, &from
, l
);
336 * Parse result message.
338 if (m
->m_len
< sizeof(*reply
)) {
339 m
= m_pullup(m
, sizeof(*reply
));
343 reply
= mtod(m
, struct callit_reply
*);
344 port
= fxdr_unsigned(u_int32_t
, reply
->port
);
345 m_adj(m
, sizeof(*reply
));
348 * Save bootparam server address
350 sin
= mtod(from
, struct sockaddr_in
*);
351 bpsin
->sin_port
= htons(port
);
352 bpsin
->sin_addr
.s_addr
= sin
->sin_addr
.s_addr
;
355 hostnamelen
= MAXHOSTNAMELEN
-1;
356 m
= xdr_string_decode(m
, hostname
, &hostnamelen
);
361 domainnamelen
= MAXHOSTNAMELEN
-1;
362 m
= xdr_string_decode(m
, domainname
, &domainnamelen
);
366 /* gateway address */
367 m
= xdr_inaddr_decode(m
, gw_ip
);
375 printf("nfs_boot: bootparam_whoami: bad reply\n");
387 * RPC: bootparam/getfile
388 * Given client name and file "key", get:
394 bp_getfile(struct sockaddr_in
*bpsin
, const char *key
,
395 struct nfs_dlmount
*ndm
, struct lwp
*l
)
397 char pathname
[MNAMELEN
];
398 struct in_addr inaddr
;
399 struct sockaddr_in
*sin
;
402 int error
, sn_len
, path_len
;
405 * Build request message.
408 /* client name (hostname) */
409 m
= xdr_string_encode(hostname
, hostnamelen
);
413 /* key name (root or swap) */
415 m
->m_next
= xdr_string_encode(__UNCONST(key
), strlen(key
));
416 if (m
->m_next
== NULL
)
419 /* RPC: bootparam/getfile */
420 error
= krpc_call(bpsin
, BOOTPARAM_PROG
, BOOTPARAM_VERS
,
421 BOOTPARAM_GETFILE
, &m
, NULL
, l
);
426 * Parse result message.
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
);
436 /* server IP address (mountd/NFS) */
437 m
= xdr_inaddr_decode(m
, &inaddr
);
441 /* server pathname */
442 path_len
= sizeof(pathname
) - 1;
443 m
= xdr_string_decode(m
, pathname
, &path_len
);
448 * Store the results in the nfs_dlmount.
449 * The strings become "server:pathname"
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");
461 ndm
->ndm_host
[sn_len
] = ':';
462 memcpy(ndm
->ndm_host
+ sn_len
+ 1, pathname
, path_len
+ 1);
468 printf("nfs_boot: bootparam_getfile: bad reply\n");