]> git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_boot.c
xnu-3789.41.3.tar.gz
[apple/xnu.git] / bsd / nfs / nfs_boot.c
1 /*
2 * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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
95 #include <sys/param.h>
96 #include <sys/systm.h>
97 #include <sys/kernel.h>
98 #include <sys/conf.h>
99 #include <sys/ioctl.h>
100 #include <sys/proc.h>
101 #include <sys/mount_internal.h>
102 #include <sys/kpi_mbuf.h>
103
104 #include <sys/malloc.h>
105 #include <sys/socket.h>
106
107 #include <net/if.h>
108 #include <net/if_dl.h>
109 #include <net/if_types.h>
110 #include <net/route.h>
111
112 #include <netinet/in.h>
113 #include <netinet/if_ether.h>
114
115 #include <nfs/rpcv2.h>
116 #include <nfs/nfsproto.h>
117 #include <nfs/nfs.h>
118 #include <nfs/nfsdiskless.h>
119 #include <nfs/krpc.h>
120
121 #include <pexpert/pexpert.h>
122
123 #include "ether.h"
124
125 #include <libkern/libkern.h>
126
127
128 #if NETHER == 0
129
130 int nfs_boot_init(__unused struct nfs_diskless *nd)
131 {
132 panic("nfs_boot_init: no ether");
133 }
134
135 int nfs_boot_getfh(__unused struct nfs_diskless *nd, __unused int v3, __unused int sotype)
136 {
137 panic("nfs_boot_getfh: no ether");
138 }
139
140 #else /* NETHER */
141
142 /*
143 * Support for NFS diskless booting, specifically getting information
144 * about where to boot from, what pathnames, etc.
145 *
146 * This implememtation uses RARP and the bootparam RPC.
147 * We are forced to implement RPC anyway (to get file handles)
148 * so we might as well take advantage of it for bootparam too.
149 *
150 * The diskless boot sequence goes as follows:
151 * (1) Use RARP to get our interface address
152 * (2) Use RPC/bootparam/whoami to get our hostname,
153 * our IP address, and the server's IP address.
154 * (3) Use RPC/bootparam/getfile to get the root path
155 * (4) Use RPC/mountd to get the root file handle
156 * (5) Use RPC/bootparam/getfile to get the swap path
157 * (6) Use RPC/mountd to get the swap file handle
158 *
159 * (This happens to be the way Sun does it too.)
160 */
161
162 /* bootparam RPC */
163 static int bp_whoami(struct sockaddr_in *bpsin,
164 struct in_addr *my_ip, struct in_addr *gw_ip);
165 static int bp_getfile(struct sockaddr_in *bpsin, const char *key,
166 struct sockaddr_in *mdsin, char *servname, char *path);
167
168 /* mountd RPC */
169 static int md_mount(struct sockaddr_in *mdsin, char *path, int v3, int sotype,
170 u_char *fhp, u_int32_t *fhlenp);
171
172 /* other helpers */
173 static int get_file_handle(struct nfs_dlmount *ndmntp);
174
175
176 #define IP_FORMAT "%d.%d.%d.%d"
177 #define IP_CH(ip) ((u_char *)ip)
178 #define IP_LIST(ip) IP_CH(ip)[0],IP_CH(ip)[1],IP_CH(ip)[2],IP_CH(ip)[3]
179
180 #include <sys/netboot.h>
181
182 /*
183 * Called with an empty nfs_diskless struct to be filled in.
184 */
185 int
186 nfs_boot_init(struct nfs_diskless *nd)
187 {
188 struct sockaddr_in bp_sin;
189 boolean_t do_bpwhoami = TRUE;
190 boolean_t do_bpgetfile = TRUE;
191 int error = 0;
192 struct in_addr my_ip;
193 struct sockaddr_in * sin_p;
194
195 /* make sure mbuf constants are set up */
196 if (!nfs_mbuf_mhlen)
197 nfs_mbuf_init();
198
199 /* by this point, networking must already have been configured */
200 if (netboot_iaddr(&my_ip) == FALSE) {
201 printf("nfs_boot: networking is not initialized\n");
202 error = ENXIO;
203 goto failed;
204 }
205
206 /* get the root path information */
207 MALLOC_ZONE(nd->nd_root.ndm_path, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
208 if (!nd->nd_root.ndm_path) {
209 printf("nfs_boot: can't allocate root path buffer\n");
210 error = ENOMEM;
211 goto failed;
212 }
213 MALLOC_ZONE(nd->nd_root.ndm_mntfrom, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
214 if (!nd->nd_root.ndm_mntfrom) {
215 printf("nfs_boot: can't allocate root mntfrom buffer\n");
216 error = ENOMEM;
217 goto failed;
218 }
219 sin_p = &nd->nd_root.ndm_saddr;
220 bzero((caddr_t)sin_p, sizeof(*sin_p));
221 sin_p->sin_len = sizeof(*sin_p);
222 sin_p->sin_family = AF_INET;
223 if (netboot_rootpath(&sin_p->sin_addr, nd->nd_root.ndm_host,
224 sizeof(nd->nd_root.ndm_host),
225 nd->nd_root.ndm_path, MAXPATHLEN) == TRUE) {
226 do_bpgetfile = FALSE;
227 do_bpwhoami = FALSE;
228 }
229 nd->nd_private.ndm_saddr.sin_addr.s_addr = 0;
230
231 if (do_bpwhoami) {
232 struct in_addr router;
233 /*
234 * Get client name and gateway address.
235 * RPC: bootparam/whoami
236 * Use the old broadcast address for the WHOAMI
237 * call because we do not yet know our netmask.
238 * The server address returned by the WHOAMI call
239 * is used for all subsequent booptaram RPCs.
240 */
241 bzero((caddr_t)&bp_sin, sizeof(bp_sin));
242 bp_sin.sin_len = sizeof(bp_sin);
243 bp_sin.sin_family = AF_INET;
244 bp_sin.sin_addr.s_addr = INADDR_BROADCAST;
245 hostnamelen = MAXHOSTNAMELEN;
246 router.s_addr = 0;
247 error = bp_whoami(&bp_sin, &my_ip, &router);
248 if (error) {
249 printf("nfs_boot: bootparam whoami, error=%d", error);
250 goto failed;
251 }
252 printf("nfs_boot: BOOTPARAMS server " IP_FORMAT "\n",
253 IP_LIST(&bp_sin.sin_addr));
254 printf("nfs_boot: hostname %s\n", hostname);
255 }
256 if (do_bpgetfile) {
257 error = bp_getfile(&bp_sin, "root", &nd->nd_root.ndm_saddr,
258 nd->nd_root.ndm_host, nd->nd_root.ndm_path);
259 if (error) {
260 printf("nfs_boot: bootparam get root: %d\n", error);
261 goto failed;
262 }
263 }
264
265 #if !defined(NO_MOUNT_PRIVATE)
266 if (do_bpgetfile) { /* get private path */
267 MALLOC_ZONE(nd->nd_private.ndm_path, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
268 if (!nd->nd_private.ndm_path) {
269 printf("nfs_boot: can't allocate private path buffer\n");
270 error = ENOMEM;
271 goto failed;
272 }
273 MALLOC_ZONE(nd->nd_private.ndm_mntfrom, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
274 if (!nd->nd_private.ndm_mntfrom) {
275 printf("nfs_boot: can't allocate private host buffer\n");
276 error = ENOMEM;
277 goto failed;
278 }
279 error = bp_getfile(&bp_sin, "private",
280 &nd->nd_private.ndm_saddr,
281 nd->nd_private.ndm_host,
282 nd->nd_private.ndm_path);
283 if (!error) {
284 char * check_path = NULL;
285
286 MALLOC_ZONE(check_path, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
287 if (!check_path) {
288 printf("nfs_boot: can't allocate check_path buffer\n");
289 error = ENOMEM;
290 goto failed;
291 }
292 snprintf(check_path, MAXPATHLEN, "%s/private", nd->nd_root.ndm_path);
293 if ((nd->nd_root.ndm_saddr.sin_addr.s_addr
294 == nd->nd_private.ndm_saddr.sin_addr.s_addr)
295 && (strncmp(check_path, nd->nd_private.ndm_path, MAXPATHLEN) == 0)) {
296 /* private path is prefix of root path, don't mount */
297 nd->nd_private.ndm_saddr.sin_addr.s_addr = 0;
298 }
299 FREE_ZONE(check_path, MAXPATHLEN, M_NAMEI);
300 }
301 else {
302 /* private key not defined, don't mount */
303 nd->nd_private.ndm_saddr.sin_addr.s_addr = 0;
304 }
305 }
306 else {
307 error = 0;
308 }
309 #endif /* NO_MOUNT_PRIVATE */
310 failed:
311 return (error);
312 }
313
314 /*
315 * Called with a partially initialized nfs_diskless struct
316 * with file handles to be filled in.
317 */
318 int
319 nfs_boot_getfh(struct nfs_diskless *nd, int v3, int sotype)
320 {
321 int error = 0;
322
323 nd->nd_root.ndm_nfsv3 = v3;
324 nd->nd_root.ndm_sotype = sotype;
325 error = get_file_handle(&nd->nd_root);
326 if (error) {
327 printf("nfs_boot: get_file_handle(v%d) root failed, %d\n",
328 v3 ? 3 : 2, error);
329 goto failed;
330 }
331
332 #if !defined(NO_MOUNT_PRIVATE)
333 if (nd->nd_private.ndm_saddr.sin_addr.s_addr) {
334 /* get private file handle */
335 nd->nd_private.ndm_nfsv3 = v3;
336 nd->nd_private.ndm_sotype = sotype;
337 error = get_file_handle(&nd->nd_private);
338 if (error) {
339 printf("nfs_boot: get_file_handle(v%d) private failed, %d\n",
340 v3 ? 3 : 2, error);
341 goto failed;
342 }
343 }
344 #endif /* NO_MOUNT_PRIVATE */
345 failed:
346 return (error);
347 }
348
349 static int
350 get_file_handle(struct nfs_dlmount *ndmntp)
351 {
352 char *sp, *dp, *endp;
353 int error;
354
355 /*
356 * Get file handle for "key" (root or swap)
357 * using RPC to mountd/mount
358 */
359 error = md_mount(&ndmntp->ndm_saddr, ndmntp->ndm_path, ndmntp->ndm_nfsv3,
360 ndmntp->ndm_sotype, ndmntp->ndm_fh, &ndmntp->ndm_fhlen);
361 if (error)
362 return (error);
363
364 /* Construct remote path (for getmntinfo(3)) */
365 dp = ndmntp->ndm_mntfrom;
366 endp = dp + MAXPATHLEN - 1;
367 for (sp = ndmntp->ndm_host; *sp && dp < endp;)
368 *dp++ = *sp++;
369 if (dp < endp)
370 *dp++ = ':';
371 for (sp = ndmntp->ndm_path; *sp && dp < endp;)
372 *dp++ = *sp++;
373 *dp = '\0';
374 return (0);
375
376 }
377
378
379 /*
380 * Get an mbuf with the given length, and
381 * initialize the pkthdr length field.
382 */
383 static int
384 mbuf_get_with_len(size_t msg_len, mbuf_t *m)
385 {
386 int error;
387 error = mbuf_gethdr(MBUF_WAITOK, MBUF_TYPE_DATA, m);
388 if (error)
389 return (error);
390 if (msg_len > mbuf_maxlen(*m)) {
391 error = mbuf_mclget(MBUF_WAITOK, MBUF_TYPE_DATA, m);
392 if (error) {
393 mbuf_freem(*m);
394 return (error);
395 }
396 if (msg_len > mbuf_maxlen(*m))
397 panic("nfs_boot: msg_len > MCLBYTES");
398 }
399 mbuf_setlen(*m, msg_len);
400 mbuf_pkthdr_setlen(*m, msg_len);
401 return (0);
402 }
403
404
405 /*
406 * String representation for RPC.
407 */
408 struct rpc_string {
409 u_int32_t len; /* length without null or padding */
410 u_char data[4]; /* data (longer, of course) */
411 /* data is padded to a long-word boundary */
412 };
413 /* Compute space used given string length. */
414 #define RPC_STR_SIZE(slen) (4 + ((slen + 3) & ~3))
415
416 /*
417 * Inet address in RPC messages
418 * (Note, really four 32-bit ints, NOT chars. Blech.)
419 */
420 struct bp_inaddr {
421 u_int32_t atype;
422 int32_t addr[4];
423 };
424
425
426 /*
427 * RPC: bootparam/whoami
428 * Given client IP address, get:
429 * client name (hostname)
430 * domain name (domainname)
431 * gateway address
432 *
433 * The hostname and domainname are set here for convenience.
434 *
435 * Note - bpsin is initialized to the broadcast address,
436 * and will be replaced with the bootparam server address
437 * after this call is complete. Have to use PMAP_PROC_CALL
438 * to make sure we get responses only from a servers that
439 * know about us (don't want to broadcast a getport call).
440 */
441 static int
442 bp_whoami(struct sockaddr_in *bpsin,
443 struct in_addr *my_ip,
444 struct in_addr *gw_ip)
445 {
446 /* RPC structures for PMAPPROC_CALLIT */
447 struct whoami_call {
448 u_int32_t call_prog;
449 u_int32_t call_vers;
450 u_int32_t call_proc;
451 u_int32_t call_arglen;
452 struct bp_inaddr call_ia;
453 } *call;
454
455 struct rpc_string *str;
456 struct bp_inaddr *bia;
457 mbuf_t m;
458 struct sockaddr_in sin;
459 int error;
460 size_t msg_len, cn_len, dn_len;
461 u_char *p;
462 int32_t *lp;
463
464 /*
465 * Get message buffer of sufficient size.
466 */
467 msg_len = sizeof(*call);
468 error = mbuf_get_with_len(msg_len, &m);
469 if (error)
470 return error;
471
472 /*
473 * Build request message for PMAPPROC_CALLIT.
474 */
475 call = mbuf_data(m);
476 call->call_prog = htonl(BOOTPARAM_PROG);
477 call->call_vers = htonl(BOOTPARAM_VERS);
478 call->call_proc = htonl(BOOTPARAM_WHOAMI);
479 call->call_arglen = htonl(sizeof(struct bp_inaddr));
480
481 /* client IP address */
482 call->call_ia.atype = htonl(1);
483 p = (u_char*)my_ip;
484 lp = call->call_ia.addr;
485 *lp++ = htonl(*p); p++;
486 *lp++ = htonl(*p); p++;
487 *lp++ = htonl(*p); p++;
488 *lp++ = htonl(*p); p++;
489
490 /* RPC: portmap/callit */
491 bpsin->sin_port = htons(PMAPPORT);
492
493 error = krpc_call(bpsin, SOCK_DGRAM, PMAPPROG, PMAPVERS, PMAPPROC_CALLIT, &m, &sin);
494 if (error)
495 return error;
496
497 /*
498 * Parse result message.
499 */
500 msg_len = mbuf_len(m);
501 lp = mbuf_data(m);
502
503 /* bootparam server port (also grab from address). */
504 if (msg_len < sizeof(*lp))
505 goto bad;
506 msg_len -= sizeof(*lp);
507 bpsin->sin_port = htons((short)ntohl(*lp++));
508 bpsin->sin_addr.s_addr = sin.sin_addr.s_addr;
509
510 /* length of encapsulated results */
511 if (msg_len < (ntohl(*lp) + sizeof(*lp)))
512 goto bad;
513 msg_len = ntohl(*lp++);
514 p = (u_char*)lp;
515
516 /* client name */
517 if (msg_len < sizeof(*str))
518 goto bad;
519 str = (struct rpc_string *)p;
520 cn_len = ntohl(str->len);
521 if (msg_len < cn_len)
522 goto bad;
523 if (cn_len >= MAXHOSTNAMELEN)
524 goto bad;
525 bcopy(str->data, hostname, cn_len);
526 hostname[cn_len] = '\0';
527 hostnamelen = cn_len;
528 p += RPC_STR_SIZE(cn_len);
529 msg_len -= RPC_STR_SIZE(cn_len);
530
531 /* domain name */
532 if (msg_len < sizeof(*str))
533 goto bad;
534 str = (struct rpc_string *)p;
535 dn_len = ntohl(str->len);
536 if (msg_len < dn_len)
537 goto bad;
538 if (dn_len >= MAXHOSTNAMELEN)
539 goto bad;
540 bcopy(str->data, domainname, dn_len);
541 domainname[dn_len] = '\0';
542 domainnamelen = dn_len;
543 p += RPC_STR_SIZE(dn_len);
544 msg_len -= RPC_STR_SIZE(dn_len);
545
546 /* gateway address */
547 if (msg_len < sizeof(*bia))
548 goto bad;
549 bia = (struct bp_inaddr *)p;
550 if (bia->atype != htonl(1))
551 goto bad;
552 p = (u_char*)gw_ip;
553 *p++ = ntohl(bia->addr[0]);
554 *p++ = ntohl(bia->addr[1]);
555 *p++ = ntohl(bia->addr[2]);
556 *p++ = ntohl(bia->addr[3]);
557 goto out;
558
559 bad:
560 printf("nfs_boot: bootparam_whoami: bad reply\n");
561 error = EBADRPC;
562
563 out:
564 mbuf_freem(m);
565 return(error);
566 }
567
568
569 /*
570 * RPC: bootparam/getfile
571 * Given client name and file "key", get:
572 * server name
573 * server IP address
574 * server pathname
575 */
576 static int
577 bp_getfile(struct sockaddr_in *bpsin,
578 const char *key,
579 struct sockaddr_in *md_sin,
580 char *serv_name,
581 char *pathname)
582 {
583 struct rpc_string *str;
584 mbuf_t m;
585 struct bp_inaddr *bia;
586 struct sockaddr_in *sin;
587 u_char *p, *q;
588 int error, msg_len;
589 int cn_len, key_len, sn_len, path_len;
590
591 /*
592 * Get message buffer of sufficient size.
593 */
594 cn_len = hostnamelen;
595 key_len = strlen(key);
596 msg_len = 0;
597 msg_len += RPC_STR_SIZE(cn_len);
598 msg_len += RPC_STR_SIZE(key_len);
599 error = mbuf_get_with_len(msg_len, &m);
600 if (error)
601 return error;
602
603 /*
604 * Build request message.
605 */
606 p = mbuf_data(m);
607 bzero(p, msg_len);
608 /* client name (hostname) */
609 str = (struct rpc_string *)p;
610 str->len = htonl(cn_len);
611 bcopy(hostname, str->data, cn_len);
612 p += RPC_STR_SIZE(cn_len);
613 /* key name (root or swap) */
614 str = (struct rpc_string *)p;
615 str->len = htonl(key_len);
616 bcopy(key, str->data, key_len);
617
618 /* RPC: bootparam/getfile */
619 error = krpc_call(bpsin, SOCK_DGRAM, BOOTPARAM_PROG, BOOTPARAM_VERS,
620 BOOTPARAM_GETFILE, &m, NULL);
621 if (error)
622 return error;
623
624 /*
625 * Parse result message.
626 */
627 p = mbuf_data(m);
628 msg_len = mbuf_len(m);
629
630 /* server name */
631 if (msg_len < (int)sizeof(*str))
632 goto bad;
633 str = (struct rpc_string *)p;
634 sn_len = ntohl(str->len);
635 if (msg_len < sn_len)
636 goto bad;
637 if (sn_len >= MAXHOSTNAMELEN)
638 goto bad;
639 bcopy(str->data, serv_name, sn_len);
640 serv_name[sn_len] = '\0';
641 p += RPC_STR_SIZE(sn_len);
642 msg_len -= RPC_STR_SIZE(sn_len);
643
644 /* server IP address (mountd) */
645 if (msg_len < (int)sizeof(*bia))
646 goto bad;
647 bia = (struct bp_inaddr *)p;
648 if (bia->atype != htonl(1))
649 goto bad;
650 sin = md_sin;
651 bzero((caddr_t)sin, sizeof(*sin));
652 sin->sin_len = sizeof(*sin);
653 sin->sin_family = AF_INET;
654 q = (u_char*) &sin->sin_addr;
655 *q++ = ntohl(bia->addr[0]);
656 *q++ = ntohl(bia->addr[1]);
657 *q++ = ntohl(bia->addr[2]);
658 *q++ = ntohl(bia->addr[3]);
659 p += sizeof(*bia);
660 msg_len -= sizeof(*bia);
661
662 /* server pathname */
663 if (msg_len < (int)sizeof(*str))
664 goto bad;
665 str = (struct rpc_string *)p;
666 path_len = ntohl(str->len);
667 if (msg_len < path_len)
668 goto bad;
669 if (path_len >= MAXPATHLEN)
670 goto bad;
671 bcopy(str->data, pathname, path_len);
672 pathname[path_len] = '\0';
673 goto out;
674
675 bad:
676 printf("nfs_boot: bootparam_getfile: bad reply\n");
677 error = EBADRPC;
678
679 out:
680 mbuf_freem(m);
681 return(0);
682 }
683
684
685 /*
686 * RPC: mountd/mount
687 * Given a server pathname, get an NFS file handle.
688 * Also, sets sin->sin_port to the NFS service port.
689 */
690 static int
691 md_mount(struct sockaddr_in *mdsin, /* mountd server address */
692 char *path,
693 int v3,
694 int sotype,
695 u_char *fhp,
696 u_int32_t *fhlenp)
697 {
698 /* The RPC structures */
699 struct rpc_string *str;
700 struct rdata {
701 u_int32_t errno;
702 u_char data[NFSX_V3FHMAX + sizeof(u_int32_t)];
703 } *rdata;
704 mbuf_t m;
705 int error, mlen, slen;
706 int mntversion = v3 ? RPCMNT_VER3 : RPCMNT_VER1;
707 int proto = (sotype == SOCK_STREAM) ? IPPROTO_TCP : IPPROTO_UDP;
708 in_port_t mntport, nfsport;
709
710 /* Get port number for MOUNTD. */
711 error = krpc_portmap(mdsin, RPCPROG_MNT, mntversion, proto, &mntport);
712 if (error)
713 return error;
714
715 /* Get port number for NFS use. */
716 /* (If NFS/proto unavailable, don't bother with the mount call) */
717 error = krpc_portmap(mdsin, NFS_PROG, v3 ? NFS_VER3 : NFS_VER2, proto, &nfsport);
718 if (error)
719 return error;
720
721 /* Set port number for MOUNTD */
722 mdsin->sin_port = mntport;
723
724 slen = strlen(path);
725 mlen = RPC_STR_SIZE(slen);
726
727 error = mbuf_get_with_len(mlen, &m);
728 if (error)
729 return error;
730 str = mbuf_data(m);
731 str->len = htonl(slen);
732 bcopy(path, str->data, slen);
733
734 /* Do RPC to mountd. */
735 error = krpc_call(mdsin, sotype, RPCPROG_MNT, mntversion, RPCMNT_MOUNT, &m, NULL);
736 if (error)
737 return error; /* message already freed */
738
739 /*
740 * the reply must be long enough to hold the errno plus either of:
741 * + a v2 filehandle
742 * + a v3 filehandle length + a v3 filehandle
743 */
744 mlen = mbuf_len(m);
745 if (mlen < (int)sizeof(u_int32_t))
746 goto bad;
747 rdata = mbuf_data(m);
748 error = ntohl(rdata->errno);
749 if (error)
750 goto out;
751 if (v3) {
752 u_int32_t fhlen;
753 u_char *fh;
754 if (mlen < (int)sizeof(u_int32_t)*2)
755 goto bad;
756 fhlen = ntohl(*(u_int32_t*)rdata->data);
757 fh = rdata->data + sizeof(u_int32_t);
758 if (mlen < (int)(sizeof(u_int32_t)*2 + fhlen))
759 goto bad;
760 bcopy(fh, fhp, fhlen);
761 *fhlenp = fhlen;
762 } else {
763 if (mlen < ((int)sizeof(u_int32_t) + NFSX_V2FH))
764 goto bad;
765 bcopy(rdata->data, fhp, NFSX_V2FH);
766 *fhlenp = NFSX_V2FH;
767 }
768
769 /* Set port number for NFS use. */
770 mdsin->sin_port = nfsport;
771 goto out;
772
773 bad:
774 error = EBADRPC;
775
776 out:
777 mbuf_freem(m);
778 return error;
779 }
780
781 #endif /* NETHER */