]> git.saurik.com Git - apple/xnu.git/blame - bsd/nfs/nfs_boot.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / nfs / nfs_boot.c
CommitLineData
1c79356b 1/*
cb323159 2 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
39037602 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
39037602 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
39037602 17 *
2d21ac55
A
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
39037602 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/* Copyright (c) 1995, 1997 NeXT Computer, Inc. All Rights Reserved */
29/*
30 * Copyright (c) 1994 Adam Glass, Gordon Ross
31 * All rights reserved.
32 *
33 * This software was developed by the Computer Systems Engineering group
34 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
35 * contributed to Berkeley.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Lawrence Berkeley Laboratory and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * History:
66 * 14-March-97 Dieter Siegmund (dieter@next.com)
67 * - Use BOOTP instead of RARP to get the IP address at boot time
68 *
69 * 23-May-97 Umesh Vaishampayan (umeshv@apple.com)
70 * - Added the ability to mount "/private" separately.
71 *
72 * 30-May-97 Dieter Siegmund (dieter@next.com)
73 * - Clear out the ireq structure before using it to prevent
74 * our sending using a bogus source IP address, we should use
75 * an IP address of all zeroes
76 * - Right after BOOTP, get the correct netmask using AUTONETMASK
77 * 18-Jul-97 Dieter Siegmund (dieter@apple.com)
78 * - we can't restrict the netmask until we have a default route,
79 * removed AUTONETMASK call (ifdef'd out)
80 * 5-Aug-97 Dieter Siegmund (dieter@apple.com)
81 * - use the default route from the bpwhoami call, enabled autonetmask
82 * again
83 * 19-Feb-1999 Dieter Siegmund (dieter@apple.com)
84 * - use new BOOTP routine to get the subnet mask and router
85 * and stop using SIOCAUTOADDR
86 * - don't bother mounting private separately if it's not
87 * specified or not required because they are substrings of
88 * one another ie. root=host:/A and private=host:/A/private
89 * - allow the root path to be specified in the boot variable
90 * "rp" (AKA "rootpath")
91 * 19-Jul-1999 Dieter Siegmund (dieter@apple.com)
92 * - replaced big automatic arrays with MALLOC'd data
93 */
94
ea3f0419
A
95#include <nfs/nfs_conf.h>
96#if CONFIG_NFS_CLIENT
97
1c79356b
A
98#include <sys/param.h>
99#include <sys/systm.h>
100#include <sys/kernel.h>
101#include <sys/conf.h>
102#include <sys/ioctl.h>
103#include <sys/proc.h>
91447636
A
104#include <sys/mount_internal.h>
105#include <sys/kpi_mbuf.h>
1c79356b
A
106
107#include <sys/malloc.h>
108#include <sys/socket.h>
1c79356b
A
109
110#include <net/if.h>
111#include <net/if_dl.h>
112#include <net/if_types.h>
113#include <net/route.h>
114
115#include <netinet/in.h>
116#include <netinet/if_ether.h>
117
118#include <nfs/rpcv2.h>
119#include <nfs/nfsproto.h>
120#include <nfs/nfs.h>
121#include <nfs/nfsdiskless.h>
122#include <nfs/krpc.h>
123
124#include <pexpert/pexpert.h>
125
126#include "ether.h"
127
128#include <libkern/libkern.h>
129
1c79356b
A
130
131#if NETHER == 0
132
0a7de745
A
133int
134nfs_boot_init(__unused struct nfs_diskless *nd)
1c79356b
A
135{
136 panic("nfs_boot_init: no ether");
137}
138
0a7de745
A
139int
140nfs_boot_getfh(__unused struct nfs_diskless *nd, __unused int v3, __unused int sotype)
90556fb8
A
141{
142 panic("nfs_boot_getfh: no ether");
143}
144
1c79356b
A
145#else /* NETHER */
146
147/*
148 * Support for NFS diskless booting, specifically getting information
149 * about where to boot from, what pathnames, etc.
150 *
151 * This implememtation uses RARP and the bootparam RPC.
152 * We are forced to implement RPC anyway (to get file handles)
153 * so we might as well take advantage of it for bootparam too.
154 *
155 * The diskless boot sequence goes as follows:
156 * (1) Use RARP to get our interface address
157 * (2) Use RPC/bootparam/whoami to get our hostname,
158 * our IP address, and the server's IP address.
159 * (3) Use RPC/bootparam/getfile to get the root path
160 * (4) Use RPC/mountd to get the root file handle
161 * (5) Use RPC/bootparam/getfile to get the swap path
162 * (6) Use RPC/mountd to get the swap file handle
163 *
164 * (This happens to be the way Sun does it too.)
165 */
166
1c79356b 167/* bootparam RPC */
91447636 168static int bp_whoami(struct sockaddr_in *bpsin,
0a7de745 169 struct in_addr *my_ip, struct in_addr *gw_ip);
91447636 170static int bp_getfile(struct sockaddr_in *bpsin, const char *key,
0a7de745 171 struct sockaddr_in *mdsin, char *servname, char *path);
1c79356b 172
1c79356b 173/* mountd RPC */
91447636 174static int md_mount(struct sockaddr_in *mdsin, char *path, int v3, int sotype,
0a7de745 175 u_char *fhp, u_int32_t *fhlenp);
1c79356b
A
176
177/* other helpers */
91447636 178static int get_file_handle(struct nfs_dlmount *ndmntp);
9bccf70c 179
1c79356b 180
0a7de745
A
181#define IP_FORMAT "%d.%d.%d.%d"
182#define IP_CH(ip) ((u_char *)ip)
183#define IP_LIST(ip) IP_CH(ip)[0],IP_CH(ip)[1],IP_CH(ip)[2],IP_CH(ip)[3]
9bccf70c 184
6d2010ae 185#include <sys/netboot.h>
9bccf70c 186
1c79356b
A
187/*
188 * Called with an empty nfs_diskless struct to be filled in.
189 */
190int
2d21ac55 191nfs_boot_init(struct nfs_diskless *nd)
1c79356b 192{
0a7de745
A
193 struct sockaddr_in bp_sin;
194 boolean_t do_bpwhoami = TRUE;
195 boolean_t do_bpgetfile = TRUE;
196 int error = 0;
197 struct in_addr my_ip;
198 struct sockaddr_in * sin_p;
9bccf70c 199
91447636 200 /* make sure mbuf constants are set up */
0a7de745 201 if (!nfs_mbuf_mhlen) {
91447636 202 nfs_mbuf_init();
0a7de745 203 }
91447636 204
9bccf70c
A
205 /* by this point, networking must already have been configured */
206 if (netboot_iaddr(&my_ip) == FALSE) {
0a7de745
A
207 printf("nfs_boot: networking is not initialized\n");
208 error = ENXIO;
209 goto failed;
9bccf70c 210 }
1c79356b 211
9bccf70c 212 /* get the root path information */
f427ee49
A
213 nd->nd_root.ndm_path = zalloc(ZV_NAMEI);
214 nd->nd_root.ndm_mntfrom = zalloc(ZV_NAMEI);
215
9bccf70c
A
216 sin_p = &nd->nd_root.ndm_saddr;
217 bzero((caddr_t)sin_p, sizeof(*sin_p));
218 sin_p->sin_len = sizeof(*sin_p);
219 sin_p->sin_family = AF_INET;
b0d623f7 220 if (netboot_rootpath(&sin_p->sin_addr, nd->nd_root.ndm_host,
0a7de745
A
221 sizeof(nd->nd_root.ndm_host),
222 nd->nd_root.ndm_path, MAXPATHLEN) == TRUE) {
223 do_bpgetfile = FALSE;
224 do_bpwhoami = FALSE;
1c79356b 225 }
9bccf70c 226 nd->nd_private.ndm_saddr.sin_addr.s_addr = 0;
1c79356b 227
1c79356b 228 if (do_bpwhoami) {
1c79356b 229 struct in_addr router;
9bccf70c
A
230 /*
231 * Get client name and gateway address.
232 * RPC: bootparam/whoami
233 * Use the old broadcast address for the WHOAMI
234 * call because we do not yet know our netmask.
235 * The server address returned by the WHOAMI call
236 * is used for all subsequent booptaram RPCs.
237 */
238 bzero((caddr_t)&bp_sin, sizeof(bp_sin));
239 bp_sin.sin_len = sizeof(bp_sin);
240 bp_sin.sin_family = AF_INET;
241 bp_sin.sin_addr.s_addr = INADDR_BROADCAST;
1c79356b
A
242 router.s_addr = 0;
243 error = bp_whoami(&bp_sin, &my_ip, &router);
244 if (error) {
9bccf70c
A
245 printf("nfs_boot: bootparam whoami, error=%d", error);
246 goto failed;
1c79356b 247 }
0a7de745
A
248 printf("nfs_boot: BOOTPARAMS server " IP_FORMAT "\n",
249 IP_LIST(&bp_sin.sin_addr));
cb323159 250 lck_mtx_lock(&hostname_lock);
9bccf70c 251 printf("nfs_boot: hostname %s\n", hostname);
cb323159 252 lck_mtx_unlock(&hostname_lock);
1c79356b 253 }
1c79356b 254 if (do_bpgetfile) {
9bccf70c 255 error = bp_getfile(&bp_sin, "root", &nd->nd_root.ndm_saddr,
0a7de745 256 nd->nd_root.ndm_host, nd->nd_root.ndm_path);
9bccf70c
A
257 if (error) {
258 printf("nfs_boot: bootparam get root: %d\n", error);
259 goto failed;
260 }
261 }
1c79356b 262
0a7de745 263#if !defined(NO_MOUNT_PRIVATE)
1c79356b 264 if (do_bpgetfile) { /* get private path */
f427ee49
A
265 nd->nd_private.ndm_path = zalloc(ZV_NAMEI);
266 nd->nd_private.ndm_mntfrom = zalloc(ZV_NAMEI);
0a7de745
A
267 error = bp_getfile(&bp_sin, "private",
268 &nd->nd_private.ndm_saddr,
269 nd->nd_private.ndm_host,
270 nd->nd_private.ndm_path);
9bccf70c
A
271 if (!error) {
272 char * check_path = NULL;
0a7de745 273
f427ee49 274 check_path = zalloc(ZV_NAMEI);
90556fb8 275 snprintf(check_path, MAXPATHLEN, "%s/private", nd->nd_root.ndm_path);
0a7de745
A
276 if ((nd->nd_root.ndm_saddr.sin_addr.s_addr
277 == nd->nd_private.ndm_saddr.sin_addr.s_addr)
2d21ac55 278 && (strncmp(check_path, nd->nd_private.ndm_path, MAXPATHLEN) == 0)) {
9bccf70c
A
279 /* private path is prefix of root path, don't mount */
280 nd->nd_private.ndm_saddr.sin_addr.s_addr = 0;
281 }
f427ee49 282 NFS_ZFREE(ZV_NAMEI, check_path);
0a7de745 283 } else {
9bccf70c
A
284 /* private key not defined, don't mount */
285 nd->nd_private.ndm_saddr.sin_addr.s_addr = 0;
1c79356b 286 }
0a7de745 287 } else {
9bccf70c 288 error = 0;
1c79356b 289 }
55e303ae
A
290#endif /* NO_MOUNT_PRIVATE */
291failed:
0a7de745 292 return error;
90556fb8
A
293}
294
295/*
296 * Called with a partially initialized nfs_diskless struct
297 * with file handles to be filled in.
298 */
299int
2d21ac55 300nfs_boot_getfh(struct nfs_diskless *nd, int v3, int sotype)
90556fb8
A
301{
302 int error = 0;
303
90556fb8 304 nd->nd_root.ndm_nfsv3 = v3;
91447636 305 nd->nd_root.ndm_sotype = sotype;
90556fb8
A
306 error = get_file_handle(&nd->nd_root);
307 if (error) {
308 printf("nfs_boot: get_file_handle(v%d) root failed, %d\n",
0a7de745 309 v3 ? 3 : 2, error);
90556fb8
A
310 goto failed;
311 }
312
0a7de745 313#if !defined(NO_MOUNT_PRIVATE)
90556fb8
A
314 if (nd->nd_private.ndm_saddr.sin_addr.s_addr) {
315 /* get private file handle */
316 nd->nd_private.ndm_nfsv3 = v3;
91447636 317 nd->nd_private.ndm_sotype = sotype;
90556fb8
A
318 error = get_file_handle(&nd->nd_private);
319 if (error) {
320 printf("nfs_boot: get_file_handle(v%d) private failed, %d\n",
0a7de745 321 v3 ? 3 : 2, error);
90556fb8
A
322 goto failed;
323 }
324 }
55e303ae 325#endif /* NO_MOUNT_PRIVATE */
91447636 326failed:
0a7de745 327 return error;
1c79356b
A
328}
329
9bccf70c 330static int
39037602 331get_file_handle(struct nfs_dlmount *ndmntp)
1c79356b
A
332{
333 char *sp, *dp, *endp;
334 int error;
335
336 /*
337 * Get file handle for "key" (root or swap)
338 * using RPC to mountd/mount
339 */
90556fb8 340 error = md_mount(&ndmntp->ndm_saddr, ndmntp->ndm_path, ndmntp->ndm_nfsv3,
0a7de745
A
341 ndmntp->ndm_sotype, ndmntp->ndm_fh, &ndmntp->ndm_fhlen);
342 if (error) {
343 return error;
344 }
1c79356b
A
345
346 /* Construct remote path (for getmntinfo(3)) */
b0d623f7
A
347 dp = ndmntp->ndm_mntfrom;
348 endp = dp + MAXPATHLEN - 1;
0a7de745 349 for (sp = ndmntp->ndm_host; *sp && dp < endp;) {
b0d623f7 350 *dp++ = *sp++;
0a7de745
A
351 }
352 if (dp < endp) {
b0d623f7 353 *dp++ = ':';
0a7de745
A
354 }
355 for (sp = ndmntp->ndm_path; *sp && dp < endp;) {
1c79356b 356 *dp++ = *sp++;
0a7de745 357 }
1c79356b 358 *dp = '\0';
0a7de745 359 return 0;
1c79356b
A
360}
361
362
363/*
364 * Get an mbuf with the given length, and
365 * initialize the pkthdr length field.
366 */
91447636 367static int
2d21ac55 368mbuf_get_with_len(size_t msg_len, mbuf_t *m)
1c79356b 369{
91447636
A
370 int error;
371 error = mbuf_gethdr(MBUF_WAITOK, MBUF_TYPE_DATA, m);
0a7de745
A
372 if (error) {
373 return error;
374 }
91447636
A
375 if (msg_len > mbuf_maxlen(*m)) {
376 error = mbuf_mclget(MBUF_WAITOK, MBUF_TYPE_DATA, m);
377 if (error) {
378 mbuf_freem(*m);
0a7de745 379 return error;
91447636 380 }
0a7de745 381 if (msg_len > mbuf_maxlen(*m)) {
1c79356b 382 panic("nfs_boot: msg_len > MCLBYTES");
0a7de745 383 }
1c79356b 384 }
91447636
A
385 mbuf_setlen(*m, msg_len);
386 mbuf_pkthdr_setlen(*m, msg_len);
0a7de745 387 return 0;
1c79356b
A
388}
389
390
391/*
392 * String representation for RPC.
393 */
394struct rpc_string {
f427ee49 395 size_t len; /* length without null or padding */
0a7de745
A
396 u_char data[4]; /* data (longer, of course) */
397 /* data is padded to a long-word boundary */
1c79356b
A
398};
399/* Compute space used given string length. */
0a7de745 400#define RPC_STR_SIZE(slen) (4 + ((slen + 3) & ~3))
1c79356b
A
401
402/*
403 * Inet address in RPC messages
b0d623f7 404 * (Note, really four 32-bit ints, NOT chars. Blech.)
1c79356b
A
405 */
406struct bp_inaddr {
b0d623f7 407 u_int32_t atype;
0a7de745 408 int32_t addr[4];
1c79356b
A
409};
410
411
412/*
413 * RPC: bootparam/whoami
414 * Given client IP address, get:
415 * client name (hostname)
416 * domain name (domainname)
417 * gateway address
418 *
419 * The hostname and domainname are set here for convenience.
420 *
421 * Note - bpsin is initialized to the broadcast address,
422 * and will be replaced with the bootparam server address
423 * after this call is complete. Have to use PMAP_PROC_CALL
424 * to make sure we get responses only from a servers that
425 * know about us (don't want to broadcast a getport call).
426 */
427static int
39037602 428bp_whoami(struct sockaddr_in *bpsin,
0a7de745
A
429 struct in_addr *my_ip,
430 struct in_addr *gw_ip)
1c79356b
A
431{
432 /* RPC structures for PMAPPROC_CALLIT */
433 struct whoami_call {
b0d623f7
A
434 u_int32_t call_prog;
435 u_int32_t call_vers;
436 u_int32_t call_proc;
437 u_int32_t call_arglen;
1c79356b
A
438 struct bp_inaddr call_ia;
439 } *call;
440
441 struct rpc_string *str;
442 struct bp_inaddr *bia;
91447636
A
443 mbuf_t m;
444 struct sockaddr_in sin;
2d21ac55
A
445 int error;
446 size_t msg_len, cn_len, dn_len;
1c79356b 447 u_char *p;
b0d623f7 448 int32_t *lp;
0a7de745 449 size_t encapsulated_size;
1c79356b
A
450
451 /*
452 * Get message buffer of sufficient size.
453 */
454 msg_len = sizeof(*call);
91447636 455 error = mbuf_get_with_len(msg_len, &m);
0a7de745 456 if (error) {
91447636 457 return error;
0a7de745 458 }
1c79356b
A
459
460 /*
461 * Build request message for PMAPPROC_CALLIT.
462 */
91447636 463 call = mbuf_data(m);
1c79356b
A
464 call->call_prog = htonl(BOOTPARAM_PROG);
465 call->call_vers = htonl(BOOTPARAM_VERS);
466 call->call_proc = htonl(BOOTPARAM_WHOAMI);
467 call->call_arglen = htonl(sizeof(struct bp_inaddr));
468
469 /* client IP address */
470 call->call_ia.atype = htonl(1);
471 p = (u_char*)my_ip;
472 lp = call->call_ia.addr;
0a7de745
A
473 *lp++ = htonl(*p); p++;
474 *lp++ = htonl(*p); p++;
475 *lp++ = htonl(*p); p++;
476 *lp++ = htonl(*p); p++;
1c79356b
A
477
478 /* RPC: portmap/callit */
479 bpsin->sin_port = htons(PMAPPORT);
480
91447636 481 error = krpc_call(bpsin, SOCK_DGRAM, PMAPPROG, PMAPVERS, PMAPPROC_CALLIT, &m, &sin);
0a7de745 482 if (error) {
1c79356b 483 return error;
0a7de745 484 }
1c79356b
A
485
486 /*
487 * Parse result message.
488 */
91447636
A
489 msg_len = mbuf_len(m);
490 lp = mbuf_data(m);
1c79356b
A
491
492 /* bootparam server port (also grab from address). */
0a7de745 493 if (msg_len < sizeof(*lp)) {
1c79356b 494 goto bad;
0a7de745 495 }
1c79356b
A
496 msg_len -= sizeof(*lp);
497 bpsin->sin_port = htons((short)ntohl(*lp++));
91447636 498 bpsin->sin_addr.s_addr = sin.sin_addr.s_addr;
1c79356b
A
499
500 /* length of encapsulated results */
a39ff7e2 501 if (os_add_overflow((size_t) ntohl(*lp), sizeof(*lp), &encapsulated_size)
0a7de745 502 || msg_len < encapsulated_size) {
1c79356b 503 goto bad;
a39ff7e2 504 }
1c79356b 505 msg_len = ntohl(*lp++);
2d21ac55 506 p = (u_char*)lp;
1c79356b
A
507
508 /* client name */
0a7de745 509 if (msg_len < sizeof(*str)) {
1c79356b 510 goto bad;
0a7de745 511 }
1c79356b 512 str = (struct rpc_string *)p;
f427ee49 513 cn_len = ntohll(str->len);
0a7de745 514 if ((msg_len - 4) < cn_len) {
1c79356b 515 goto bad;
0a7de745
A
516 }
517 if (cn_len >= MAXHOSTNAMELEN) {
1c79356b 518 goto bad;
0a7de745 519 }
cb323159 520 lck_mtx_lock(&hostname_lock);
1c79356b
A
521 bcopy(str->data, hostname, cn_len);
522 hostname[cn_len] = '\0';
cb323159 523 lck_mtx_unlock(&hostname_lock);
1c79356b
A
524 p += RPC_STR_SIZE(cn_len);
525 msg_len -= RPC_STR_SIZE(cn_len);
526
527 /* domain name */
0a7de745 528 if (msg_len < sizeof(*str)) {
1c79356b 529 goto bad;
0a7de745 530 }
1c79356b 531 str = (struct rpc_string *)p;
f427ee49 532 dn_len = ntohll(str->len);
0a7de745 533 if ((msg_len - 4) < dn_len) {
1c79356b 534 goto bad;
0a7de745
A
535 }
536 if (dn_len >= MAXHOSTNAMELEN) {
1c79356b 537 goto bad;
0a7de745 538 }
cb323159 539 lck_mtx_lock(&domainname_lock);
1c79356b
A
540 bcopy(str->data, domainname, dn_len);
541 domainname[dn_len] = '\0';
cb323159 542 lck_mtx_unlock(&domainname_lock);
1c79356b
A
543 p += RPC_STR_SIZE(dn_len);
544 msg_len -= RPC_STR_SIZE(dn_len);
545
546 /* gateway address */
0a7de745 547 if (msg_len < sizeof(*bia)) {
1c79356b 548 goto bad;
0a7de745 549 }
1c79356b 550 bia = (struct bp_inaddr *)p;
0a7de745 551 if (bia->atype != htonl(1)) {
1c79356b 552 goto bad;
0a7de745 553 }
1c79356b 554 p = (u_char*)gw_ip;
f427ee49
A
555 *p++ = ntohl(bia->addr[0]) & 0xff;
556 *p++ = ntohl(bia->addr[1]) & 0xff;
557 *p++ = ntohl(bia->addr[2]) & 0xff;
558 *p++ = ntohl(bia->addr[3]) & 0xff;
1c79356b
A
559 goto out;
560
561bad:
562 printf("nfs_boot: bootparam_whoami: bad reply\n");
563 error = EBADRPC;
564
565out:
91447636 566 mbuf_freem(m);
0a7de745 567 return error;
1c79356b
A
568}
569
570
571/*
572 * RPC: bootparam/getfile
573 * Given client name and file "key", get:
574 * server name
575 * server IP address
576 * server pathname
577 */
578static int
39037602 579bp_getfile(struct sockaddr_in *bpsin,
0a7de745
A
580 const char *key,
581 struct sockaddr_in *md_sin,
582 char *serv_name,
583 char *pathname)
1c79356b
A
584{
585 struct rpc_string *str;
91447636 586 mbuf_t m;
1c79356b
A
587 struct bp_inaddr *bia;
588 struct sockaddr_in *sin;
589 u_char *p, *q;
a39ff7e2
A
590 int error;
591 size_t msg_len, cn_len, key_len, sn_len, path_len;
1c79356b
A
592
593 /*
594 * Get message buffer of sufficient size.
595 */
cb323159
A
596 lck_mtx_lock(&hostname_lock);
597 cn_len = strlen(hostname);
598 lck_mtx_unlock(&hostname_lock);
1c79356b
A
599 key_len = strlen(key);
600 msg_len = 0;
601 msg_len += RPC_STR_SIZE(cn_len);
602 msg_len += RPC_STR_SIZE(key_len);
91447636 603 error = mbuf_get_with_len(msg_len, &m);
0a7de745 604 if (error) {
91447636 605 return error;
0a7de745 606 }
1c79356b
A
607
608 /*
609 * Build request message.
610 */
91447636 611 p = mbuf_data(m);
1c79356b
A
612 bzero(p, msg_len);
613 /* client name (hostname) */
614 str = (struct rpc_string *)p;
f427ee49 615 str->len = htonll(cn_len);
cb323159 616 lck_mtx_lock(&hostname_lock);
1c79356b 617 bcopy(hostname, str->data, cn_len);
cb323159 618 lck_mtx_unlock(&hostname_lock);
1c79356b
A
619 p += RPC_STR_SIZE(cn_len);
620 /* key name (root or swap) */
621 str = (struct rpc_string *)p;
f427ee49 622 str->len = htonll(key_len);
1c79356b
A
623 bcopy(key, str->data, key_len);
624
625 /* RPC: bootparam/getfile */
91447636 626 error = krpc_call(bpsin, SOCK_DGRAM, BOOTPARAM_PROG, BOOTPARAM_VERS,
0a7de745
A
627 BOOTPARAM_GETFILE, &m, NULL);
628 if (error) {
1c79356b 629 return error;
0a7de745 630 }
1c79356b
A
631
632 /*
633 * Parse result message.
634 */
91447636
A
635 p = mbuf_data(m);
636 msg_len = mbuf_len(m);
1c79356b
A
637
638 /* server name */
0a7de745 639 if (msg_len < sizeof(*str)) {
1c79356b 640 goto bad;
0a7de745 641 }
1c79356b 642 str = (struct rpc_string *)p;
f427ee49 643 sn_len = ntohll(str->len);
0a7de745 644 if ((msg_len - 4) < sn_len) {
1c79356b 645 goto bad;
0a7de745
A
646 }
647 if (sn_len >= MAXHOSTNAMELEN) {
1c79356b 648 goto bad;
0a7de745 649 }
1c79356b
A
650 bcopy(str->data, serv_name, sn_len);
651 serv_name[sn_len] = '\0';
652 p += RPC_STR_SIZE(sn_len);
653 msg_len -= RPC_STR_SIZE(sn_len);
654
655 /* server IP address (mountd) */
0a7de745 656 if (msg_len < sizeof(*bia)) {
1c79356b 657 goto bad;
0a7de745 658 }
1c79356b 659 bia = (struct bp_inaddr *)p;
0a7de745 660 if (bia->atype != htonl(1)) {
1c79356b 661 goto bad;
0a7de745 662 }
1c79356b
A
663 sin = md_sin;
664 bzero((caddr_t)sin, sizeof(*sin));
665 sin->sin_len = sizeof(*sin);
666 sin->sin_family = AF_INET;
667 q = (u_char*) &sin->sin_addr;
f427ee49
A
668 *q++ = ntohl(bia->addr[0]) & 0xff;
669 *q++ = ntohl(bia->addr[1]) & 0xff;
670 *q++ = ntohl(bia->addr[2]) & 0xff;
671 *q++ = ntohl(bia->addr[3]) & 0xff;
1c79356b
A
672 p += sizeof(*bia);
673 msg_len -= sizeof(*bia);
674
675 /* server pathname */
0a7de745 676 if (msg_len < sizeof(*str)) {
1c79356b 677 goto bad;
0a7de745 678 }
1c79356b 679 str = (struct rpc_string *)p;
f427ee49 680 path_len = ntohll(str->len);
0a7de745 681 if ((msg_len - 4) < path_len) {
1c79356b 682 goto bad;
0a7de745
A
683 }
684 if (path_len >= MAXPATHLEN) {
1c79356b 685 goto bad;
0a7de745 686 }
1c79356b
A
687 bcopy(str->data, pathname, path_len);
688 pathname[path_len] = '\0';
689 goto out;
690
691bad:
692 printf("nfs_boot: bootparam_getfile: bad reply\n");
693 error = EBADRPC;
694
695out:
91447636 696 mbuf_freem(m);
0a7de745 697 return 0;
1c79356b
A
698}
699
700
701/*
702 * RPC: mountd/mount
703 * Given a server pathname, get an NFS file handle.
704 * Also, sets sin->sin_port to the NFS service port.
705 */
706static int
0a7de745
A
707md_mount(struct sockaddr_in *mdsin, /* mountd server address */
708 char *path,
709 int v3,
710 int sotype,
711 u_char *fhp,
712 u_int32_t *fhlenp)
1c79356b
A
713{
714 /* The RPC structures */
715 struct rpc_string *str;
716 struct rdata {
0a7de745
A
717 u_int32_t errno;
718 u_char data[NFSX_V3FHMAX + sizeof(u_int32_t)];
1c79356b 719 } *rdata;
91447636 720 mbuf_t m;
f427ee49
A
721 size_t mlen, slen;
722 int error;
90556fb8 723 int mntversion = v3 ? RPCMNT_VER3 : RPCMNT_VER1;
91447636
A
724 int proto = (sotype == SOCK_STREAM) ? IPPROTO_TCP : IPPROTO_UDP;
725 in_port_t mntport, nfsport;
1c79356b
A
726
727 /* Get port number for MOUNTD. */
91447636 728 error = krpc_portmap(mdsin, RPCPROG_MNT, mntversion, proto, &mntport);
0a7de745 729 if (error) {
91447636 730 return error;
0a7de745 731 }
91447636
A
732
733 /* Get port number for NFS use. */
734 /* (If NFS/proto unavailable, don't bother with the mount call) */
735 error = krpc_portmap(mdsin, NFS_PROG, v3 ? NFS_VER3 : NFS_VER2, proto, &nfsport);
0a7de745 736 if (error) {
91447636 737 return error;
0a7de745 738 }
91447636
A
739
740 /* Set port number for MOUNTD */
741 mdsin->sin_port = mntport;
1c79356b
A
742
743 slen = strlen(path);
744 mlen = RPC_STR_SIZE(slen);
745
91447636 746 error = mbuf_get_with_len(mlen, &m);
0a7de745 747 if (error) {
91447636 748 return error;
0a7de745 749 }
91447636 750 str = mbuf_data(m);
f427ee49 751 str->len = htonll(slen);
1c79356b
A
752 bcopy(path, str->data, slen);
753
754 /* Do RPC to mountd. */
91447636 755 error = krpc_call(mdsin, sotype, RPCPROG_MNT, mntversion, RPCMNT_MOUNT, &m, NULL);
0a7de745
A
756 if (error) {
757 return error; /* message already freed */
758 }
90556fb8
A
759 /*
760 * the reply must be long enough to hold the errno plus either of:
761 * + a v2 filehandle
762 * + a v3 filehandle length + a v3 filehandle
763 */
91447636 764 mlen = mbuf_len(m);
0a7de745 765 if (mlen < sizeof(u_int32_t)) {
1c79356b 766 goto bad;
0a7de745 767 }
91447636 768 rdata = mbuf_data(m);
1c79356b 769 error = ntohl(rdata->errno);
0a7de745 770 if (error) {
90556fb8 771 goto out;
0a7de745 772 }
90556fb8 773 if (v3) {
b0d623f7 774 u_int32_t fhlen;
90556fb8 775 u_char *fh;
0a7de745 776 if (mlen < sizeof(u_int32_t) * 2) {
90556fb8 777 goto bad;
0a7de745 778 }
b0d623f7
A
779 fhlen = ntohl(*(u_int32_t*)rdata->data);
780 fh = rdata->data + sizeof(u_int32_t);
0a7de745
A
781 if (mlen < (sizeof(u_int32_t) * 2 + fhlen)
782 || fhlen >= (NFSX_V3FHMAX + sizeof(u_int32_t))) {
90556fb8 783 goto bad;
0a7de745 784 }
90556fb8
A
785 bcopy(fh, fhp, fhlen);
786 *fhlenp = fhlen;
787 } else {
0a7de745 788 if (mlen < (sizeof(u_int32_t) + NFSX_V2FH)) {
90556fb8 789 goto bad;
0a7de745 790 }
90556fb8
A
791 bcopy(rdata->data, fhp, NFSX_V2FH);
792 *fhlenp = NFSX_V2FH;
793 }
1c79356b
A
794
795 /* Set port number for NFS use. */
91447636 796 mdsin->sin_port = nfsport;
1c79356b
A
797 goto out;
798
799bad:
800 error = EBADRPC;
801
802out:
91447636 803 mbuf_freem(m);
1c79356b
A
804 return error;
805}
806
807#endif /* NETHER */
ea3f0419
A
808
809#endif /* CONFIG_NFS_CLIENT */