]>
git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_boot.c
1a21bbee009f832a192d1cd77f48a3ba15503302
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995, 1997 NeXT Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1994 Adam Glass, Gordon Ross
28 * All rights reserved.
30 * This software was developed by the Computer Systems Engineering group
31 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
32 * contributed to Berkeley.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Lawrence Berkeley Laboratory and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * 14-March-97 Dieter Siegmund (dieter@next.com)
64 * - Use BOOTP instead of RARP to get the IP address at boot time
66 * 23-May-97 Umesh Vaishampayan (umeshv@apple.com)
67 * - Added the ability to mount "/private" separately.
69 * 30-May-97 Dieter Siegmund (dieter@next.com)
70 * - Clear out the ireq structure before using it to prevent
71 * our sending using a bogus source IP address, we should use
72 * an IP address of all zeroes
73 * - Right after BOOTP, get the correct netmask using AUTONETMASK
74 * 18-Jul-97 Dieter Siegmund (dieter@apple.com)
75 * - we can't restrict the netmask until we have a default route,
76 * removed AUTONETMASK call (ifdef'd out)
77 * 5-Aug-97 Dieter Siegmund (dieter@apple.com)
78 * - use the default route from the bpwhoami call, enabled autonetmask
80 * 19-Feb-1999 Dieter Siegmund (dieter@apple.com)
81 * - use new BOOTP routine to get the subnet mask and router
82 * and stop using SIOCAUTOADDR
83 * - don't bother mounting private separately if it's not
84 * specified or not required because they are substrings of
85 * one another ie. root=host:/A and private=host:/A/private
86 * - allow the root path to be specified in the boot variable
87 * "rp" (AKA "rootpath")
88 * 19-Jul-1999 Dieter Siegmund (dieter@apple.com)
89 * - replaced big automatic arrays with MALLOC'd data
92 #include <sys/param.h>
93 #include <sys/systm.h>
94 #include <sys/kernel.h>
96 #include <sys/ioctl.h>
98 #include <sys/mount.h>
101 #include <sys/malloc.h>
102 #include <sys/socket.h>
103 #include <sys/reboot.h>
106 #include <net/if_dl.h>
107 #include <net/if_types.h>
108 #include <net/route.h>
110 #include <netinet/in.h>
111 #include <netinet/if_ether.h>
113 #include <nfs/rpcv2.h>
114 #include <nfs/nfsproto.h>
116 #include <nfs/nfsdiskless.h>
117 #include <nfs/krpc.h>
119 #include <pexpert/pexpert.h>
123 #include <libkern/libkern.h>
125 extern char *strchr(const char *str
, int ch
);
129 int nfs_boot_init(nd
, procp
)
130 struct nfs_diskless
*nd
;
133 panic("nfs_boot_init: no ether");
136 int nfs_boot_getfh(nd
, procp
, v3
)
137 struct nfs_diskless
*nd
;
141 panic("nfs_boot_getfh: no ether");
147 * Support for NFS diskless booting, specifically getting information
148 * about where to boot from, what pathnames, etc.
150 * This implememtation uses RARP and the bootparam RPC.
151 * We are forced to implement RPC anyway (to get file handles)
152 * so we might as well take advantage of it for bootparam too.
154 * The diskless boot sequence goes as follows:
155 * (1) Use RARP to get our interface address
156 * (2) Use RPC/bootparam/whoami to get our hostname,
157 * our IP address, and the server's IP address.
158 * (3) Use RPC/bootparam/getfile to get the root path
159 * (4) Use RPC/mountd to get the root file handle
160 * (5) Use RPC/bootparam/getfile to get the swap path
161 * (6) Use RPC/mountd to get the swap file handle
163 * (This happens to be the way Sun does it too.)
167 static int bp_whoami
__P((struct sockaddr_in
*bpsin
,
168 struct in_addr
*my_ip
, struct in_addr
*gw_ip
));
169 static int bp_getfile
__P((struct sockaddr_in
*bpsin
, char *key
,
170 struct sockaddr_in
*mdsin
, char *servname
, char *path
));
173 static int md_mount
__P((struct sockaddr_in
*mdsin
, char *path
, int v3
,
174 u_char
*fhp
, u_long
*fhlenp
));
177 static int get_file_handle
__P((struct nfs_dlmount
*ndmntp
));
180 #define IP_FORMAT "%d.%d.%d.%d"
181 #define IP_CH(ip) ((u_char *)ip)
182 #define IP_LIST(ip) IP_CH(ip)[0],IP_CH(ip)[1],IP_CH(ip)[2],IP_CH(ip)[3]
185 netboot_iaddr(struct in_addr
* iaddr_p
);
188 netboot_rootpath(struct in_addr
* server_ip
,
189 char * name
, int name_len
,
190 char * path
, int path_len
);
193 * Called with an empty nfs_diskless struct to be filled in.
196 nfs_boot_init(nd
, procp
)
197 struct nfs_diskless
*nd
;
200 struct sockaddr_in bp_sin
;
201 boolean_t do_bpwhoami
= TRUE
;
202 boolean_t do_bpgetfile
= TRUE
;
204 struct in_addr my_ip
;
205 struct sockaddr_in
* sin_p
;
207 /* by this point, networking must already have been configured */
208 if (netboot_iaddr(&my_ip
) == FALSE
) {
209 printf("nfs_boot: networking is not initialized\n");
214 /* get the root path information */
215 MALLOC_ZONE(nd
->nd_root
.ndm_path
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
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
;
220 if (netboot_rootpath(&sin_p
->sin_addr
, nd
->nd_root
.ndm_host
,
221 sizeof(nd
->nd_root
.ndm_host
),
222 nd
->nd_root
.ndm_path
, MAXPATHLEN
) == TRUE
) {
223 do_bpgetfile
= FALSE
;
226 nd
->nd_private
.ndm_saddr
.sin_addr
.s_addr
= 0;
228 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
231 struct in_addr router
;
233 * Get client name and gateway address.
234 * RPC: bootparam/whoami
235 * Use the old broadcast address for the WHOAMI
236 * call because we do not yet know our netmask.
237 * The server address returned by the WHOAMI call
238 * is used for all subsequent booptaram RPCs.
240 bzero((caddr_t
)&bp_sin
, sizeof(bp_sin
));
241 bp_sin
.sin_len
= sizeof(bp_sin
);
242 bp_sin
.sin_family
= AF_INET
;
243 bp_sin
.sin_addr
.s_addr
= INADDR_BROADCAST
;
244 hostnamelen
= MAXHOSTNAMELEN
;
246 error
= bp_whoami(&bp_sin
, &my_ip
, &router
);
248 printf("nfs_boot: bootparam whoami, error=%d", error
);
251 printf("nfs_boot: BOOTPARAMS server " IP_FORMAT
"\n",
252 IP_LIST(&bp_sin
.sin_addr
));
253 printf("nfs_boot: hostname %s\n", hostname
);
256 error
= bp_getfile(&bp_sin
, "root", &nd
->nd_root
.ndm_saddr
,
257 nd
->nd_root
.ndm_host
, nd
->nd_root
.ndm_path
);
259 printf("nfs_boot: bootparam get root: %d\n", error
);
264 #if !defined(NO_MOUNT_PRIVATE)
265 if (do_bpgetfile
) { /* get private path */
266 MALLOC_ZONE(nd
->nd_private
.ndm_path
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
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
);
272 char * check_path
= NULL
;
274 MALLOC_ZONE(check_path
, char *, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
275 snprintf(check_path
, MAXPATHLEN
, "%s/private", nd
->nd_root
.ndm_path
);
276 if ((nd
->nd_root
.ndm_saddr
.sin_addr
.s_addr
277 == nd
->nd_private
.ndm_saddr
.sin_addr
.s_addr
)
278 && (strcmp(check_path
, nd
->nd_private
.ndm_path
) == 0)) {
279 /* private path is prefix of root path, don't mount */
280 nd
->nd_private
.ndm_saddr
.sin_addr
.s_addr
= 0;
282 FREE_ZONE(check_path
, MAXPATHLEN
, M_NAMEI
);
285 /* private key not defined, don't mount */
286 nd
->nd_private
.ndm_saddr
.sin_addr
.s_addr
= 0;
292 #endif NO_MOUNT_PRIVATE
294 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
299 * Called with a partially initialized nfs_diskless struct
300 * with file handles to be filled in.
303 nfs_boot_getfh(nd
, procp
, v3
)
304 struct nfs_diskless
*nd
;
310 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
312 nd
->nd_root
.ndm_nfsv3
= v3
;
313 error
= get_file_handle(&nd
->nd_root
);
315 printf("nfs_boot: get_file_handle(v%d) root failed, %d\n",
320 #if !defined(NO_MOUNT_PRIVATE)
321 if (nd
->nd_private
.ndm_saddr
.sin_addr
.s_addr
) {
322 /* get private file handle */
323 nd
->nd_private
.ndm_nfsv3
= v3
;
324 error
= get_file_handle(&nd
->nd_private
);
326 printf("nfs_boot: get_file_handle(v%d) private failed, %d\n",
331 #endif NO_MOUNT_PRIVATE
333 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
338 get_file_handle(ndmntp
)
339 struct nfs_dlmount
*ndmntp
;
341 char *sp
, *dp
, *endp
;
345 * Get file handle for "key" (root or swap)
346 * using RPC to mountd/mount
348 error
= md_mount(&ndmntp
->ndm_saddr
, ndmntp
->ndm_path
, ndmntp
->ndm_nfsv3
,
349 ndmntp
->ndm_fh
, &ndmntp
->ndm_fhlen
);
353 /* Construct remote path (for getmntinfo(3)) */
354 dp
= ndmntp
->ndm_host
;
355 endp
= dp
+ MNAMELEN
- 1;
358 for (sp
= ndmntp
->ndm_path
; *sp
&& dp
< endp
;)
367 * Get an mbuf with the given length, and
368 * initialize the pkthdr length field.
371 m_get_len(int msg_len
)
374 m
= m_gethdr(M_WAIT
, MT_DATA
);
377 if (msg_len
> MHLEN
) {
378 if (msg_len
> MCLBYTES
)
379 panic("nfs_boot: msg_len > MCLBYTES");
385 m
->m_pkthdr
.len
= m
->m_len
;
391 * String representation for RPC.
394 u_long len
; /* length without null or padding */
395 u_char data
[4]; /* data (longer, of course) */
396 /* data is padded to a long-word boundary */
398 /* Compute space used given string length. */
399 #define RPC_STR_SIZE(slen) (4 + ((slen + 3) & ~3))
402 * Inet address in RPC messages
403 * (Note, really four longs, NOT chars. Blech.)
412 * RPC: bootparam/whoami
413 * Given client IP address, get:
414 * client name (hostname)
415 * domain name (domainname)
418 * The hostname and domainname are set here for convenience.
420 * Note - bpsin is initialized to the broadcast address,
421 * and will be replaced with the bootparam server address
422 * after this call is complete. Have to use PMAP_PROC_CALL
423 * to make sure we get responses only from a servers that
424 * know about us (don't want to broadcast a getport call).
427 bp_whoami(bpsin
, my_ip
, gw_ip
)
428 struct sockaddr_in
*bpsin
;
429 struct in_addr
*my_ip
;
430 struct in_addr
*gw_ip
;
432 /* RPC structures for PMAPPROC_CALLIT */
438 struct bp_inaddr call_ia
;
441 struct rpc_string
*str
;
442 struct bp_inaddr
*bia
;
444 struct sockaddr_in
*sin
;
451 * Get message buffer of sufficient size.
453 msg_len
= sizeof(*call
);
454 m
= m_get_len(msg_len
);
459 * Build request message for PMAPPROC_CALLIT.
461 call
= mtod(m
, struct whoami_call
*);
462 call
->call_prog
= htonl(BOOTPARAM_PROG
);
463 call
->call_vers
= htonl(BOOTPARAM_VERS
);
464 call
->call_proc
= htonl(BOOTPARAM_WHOAMI
);
465 call
->call_arglen
= htonl(sizeof(struct bp_inaddr
));
467 /* client IP address */
468 call
->call_ia
.atype
= htonl(1);
470 lp
= call
->call_ia
.addr
;
471 *lp
++ = htonl(*p
); p
++;
472 *lp
++ = htonl(*p
); p
++;
473 *lp
++ = htonl(*p
); p
++;
474 *lp
++ = htonl(*p
); p
++;
476 /* RPC: portmap/callit */
477 bpsin
->sin_port
= htons(PMAPPORT
);
479 error
= krpc_call(bpsin
, PMAPPROG
, PMAPVERS
,
480 PMAPPROC_CALLIT
, &m
, &sin
);
485 * Parse result message.
488 lp
= mtod(m
, long *);
490 /* bootparam server port (also grab from address). */
491 if (msg_len
< sizeof(*lp
))
493 msg_len
-= sizeof(*lp
);
494 bpsin
->sin_port
= htons((short)ntohl(*lp
++));
495 bpsin
->sin_addr
.s_addr
= sin
->sin_addr
.s_addr
;
497 /* length of encapsulated results */
498 if (msg_len
< (ntohl(*lp
) + sizeof(*lp
)))
500 msg_len
= ntohl(*lp
++);
504 if (msg_len
< sizeof(*str
))
506 str
= (struct rpc_string
*)p
;
507 cn_len
= ntohl(str
->len
);
508 if (msg_len
< cn_len
)
510 if (cn_len
>= MAXHOSTNAMELEN
)
512 bcopy(str
->data
, hostname
, cn_len
);
513 hostname
[cn_len
] = '\0';
514 hostnamelen
= cn_len
;
515 p
+= RPC_STR_SIZE(cn_len
);
516 msg_len
-= RPC_STR_SIZE(cn_len
);
519 if (msg_len
< sizeof(*str
))
521 str
= (struct rpc_string
*)p
;
522 dn_len
= ntohl(str
->len
);
523 if (msg_len
< dn_len
)
525 if (dn_len
>= MAXHOSTNAMELEN
)
527 bcopy(str
->data
, domainname
, dn_len
);
528 domainname
[dn_len
] = '\0';
529 domainnamelen
= dn_len
;
530 p
+= RPC_STR_SIZE(dn_len
);
531 msg_len
-= RPC_STR_SIZE(dn_len
);
533 /* gateway address */
534 if (msg_len
< sizeof(*bia
))
536 bia
= (struct bp_inaddr
*)p
;
537 if (bia
->atype
!= htonl(1))
540 *p
++ = ntohl(bia
->addr
[0]);
541 *p
++ = ntohl(bia
->addr
[1]);
542 *p
++ = ntohl(bia
->addr
[2]);
543 *p
++ = ntohl(bia
->addr
[3]);
547 printf("nfs_boot: bootparam_whoami: bad reply\n");
560 * RPC: bootparam/getfile
561 * Given client name and file "key", get:
567 bp_getfile(bpsin
, key
, md_sin
, serv_name
, pathname
)
568 struct sockaddr_in
*bpsin
;
570 struct sockaddr_in
*md_sin
;
574 struct rpc_string
*str
;
576 struct bp_inaddr
*bia
;
577 struct sockaddr_in
*sin
;
580 int cn_len
, key_len
, sn_len
, path_len
;
583 * Get message buffer of sufficient size.
585 cn_len
= hostnamelen
;
586 key_len
= strlen(key
);
588 msg_len
+= RPC_STR_SIZE(cn_len
);
589 msg_len
+= RPC_STR_SIZE(key_len
);
590 m
= m_get_len(msg_len
);
595 * Build request message.
597 p
= mtod(m
, u_char
*);
599 /* client name (hostname) */
600 str
= (struct rpc_string
*)p
;
601 str
->len
= htonl(cn_len
);
602 bcopy(hostname
, str
->data
, cn_len
);
603 p
+= RPC_STR_SIZE(cn_len
);
604 /* key name (root or swap) */
605 str
= (struct rpc_string
*)p
;
606 str
->len
= htonl(key_len
);
607 bcopy(key
, str
->data
, key_len
);
609 /* RPC: bootparam/getfile */
610 error
= krpc_call(bpsin
, BOOTPARAM_PROG
, BOOTPARAM_VERS
,
611 BOOTPARAM_GETFILE
, &m
, NULL
);
616 * Parse result message.
618 p
= mtod(m
, u_char
*);
622 if (msg_len
< sizeof(*str
))
624 str
= (struct rpc_string
*)p
;
625 sn_len
= ntohl(str
->len
);
626 if (msg_len
< sn_len
)
628 if (sn_len
>= MNAMELEN
)
630 bcopy(str
->data
, serv_name
, sn_len
);
631 serv_name
[sn_len
] = '\0';
632 p
+= RPC_STR_SIZE(sn_len
);
633 msg_len
-= RPC_STR_SIZE(sn_len
);
635 /* server IP address (mountd) */
636 if (msg_len
< sizeof(*bia
))
638 bia
= (struct bp_inaddr
*)p
;
639 if (bia
->atype
!= htonl(1))
642 bzero((caddr_t
)sin
, sizeof(*sin
));
643 sin
->sin_len
= sizeof(*sin
);
644 sin
->sin_family
= AF_INET
;
645 q
= (u_char
*) &sin
->sin_addr
;
646 *q
++ = ntohl(bia
->addr
[0]);
647 *q
++ = ntohl(bia
->addr
[1]);
648 *q
++ = ntohl(bia
->addr
[2]);
649 *q
++ = ntohl(bia
->addr
[3]);
651 msg_len
-= sizeof(*bia
);
653 /* server pathname */
654 if (msg_len
< sizeof(*str
))
656 str
= (struct rpc_string
*)p
;
657 path_len
= ntohl(str
->len
);
658 if (msg_len
< path_len
)
660 if (path_len
>= MAXPATHLEN
)
662 bcopy(str
->data
, pathname
, path_len
);
663 pathname
[path_len
] = '\0';
667 printf("nfs_boot: bootparam_getfile: bad reply\n");
678 * Given a server pathname, get an NFS file handle.
679 * Also, sets sin->sin_port to the NFS service port.
682 md_mount(mdsin
, path
, v3
, fhp
, fhlenp
)
683 struct sockaddr_in
*mdsin
; /* mountd server address */
689 /* The RPC structures */
690 struct rpc_string
*str
;
693 u_char data
[NFSX_V3FHMAX
+ sizeof(u_long
)];
696 int error
, mlen
, slen
;
697 int mntversion
= v3
? RPCMNT_VER3
: RPCMNT_VER1
;
699 /* Get port number for MOUNTD. */
700 error
= krpc_portmap(mdsin
, RPCPROG_MNT
, mntversion
,
702 if (error
) return error
;
705 mlen
= RPC_STR_SIZE(slen
);
710 str
= mtod(m
, struct rpc_string
*);
711 str
->len
= htonl(slen
);
712 bcopy(path
, str
->data
, slen
);
714 /* Do RPC to mountd. */
715 error
= krpc_call(mdsin
, RPCPROG_MNT
, mntversion
,
716 RPCMNT_MOUNT
, &m
, NULL
);
718 return error
; /* message already freed */
721 * the reply must be long enough to hold the errno plus either of:
723 * + a v3 filehandle length + a v3 filehandle
726 if (mlen
< sizeof(u_long
))
728 rdata
= mtod(m
, struct rdata
*);
729 error
= ntohl(rdata
->errno
);
735 if (mlen
< sizeof(u_long
)*2)
737 fhlen
= ntohl(*(u_long
*)rdata
->data
);
738 fh
= rdata
->data
+ sizeof(u_long
);
739 if (mlen
< (sizeof(u_long
)*2 + fhlen
))
741 bcopy(fh
, fhp
, fhlen
);
744 if (mlen
< (sizeof(u_long
) + NFSX_V2FH
))
746 bcopy(rdata
->data
, fhp
, NFSX_V2FH
);
750 /* Set port number for NFS use. */
751 error
= krpc_portmap(mdsin
, NFS_PROG
, v3
? NFS_VER3
: NFS_VER2
,