]>
git.saurik.com Git - apple/xnu.git/blob - bsd/nfs/nfs_boot.c
cec8ac1e866a4b846d537cb004d582fe5ec7d6c1
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");
211 goto failed_noswitch
;
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
);
300 * Called with a partially initialized nfs_diskless struct
301 * with file handles to be filled in.
304 nfs_boot_getfh(nd
, procp
, v3
)
305 struct nfs_diskless
*nd
;
311 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
313 nd
->nd_root
.ndm_nfsv3
= v3
;
314 error
= get_file_handle(&nd
->nd_root
);
316 printf("nfs_boot: get_file_handle(v%d) root failed, %d\n",
321 #if !defined(NO_MOUNT_PRIVATE)
322 if (nd
->nd_private
.ndm_saddr
.sin_addr
.s_addr
) {
323 /* get private file handle */
324 nd
->nd_private
.ndm_nfsv3
= v3
;
325 error
= get_file_handle(&nd
->nd_private
);
327 printf("nfs_boot: get_file_handle(v%d) private failed, %d\n",
332 #endif NO_MOUNT_PRIVATE
334 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
339 get_file_handle(ndmntp
)
340 struct nfs_dlmount
*ndmntp
;
342 char *sp
, *dp
, *endp
;
346 * Get file handle for "key" (root or swap)
347 * using RPC to mountd/mount
349 error
= md_mount(&ndmntp
->ndm_saddr
, ndmntp
->ndm_path
, ndmntp
->ndm_nfsv3
,
350 ndmntp
->ndm_fh
, &ndmntp
->ndm_fhlen
);
354 /* Construct remote path (for getmntinfo(3)) */
355 dp
= ndmntp
->ndm_host
;
356 endp
= dp
+ MNAMELEN
- 1;
359 for (sp
= ndmntp
->ndm_path
; *sp
&& dp
< endp
;)
368 * Get an mbuf with the given length, and
369 * initialize the pkthdr length field.
372 m_get_len(int msg_len
)
375 m
= m_gethdr(M_WAIT
, MT_DATA
);
378 if (msg_len
> MHLEN
) {
379 if (msg_len
> MCLBYTES
)
380 panic("nfs_boot: msg_len > MCLBYTES");
386 m
->m_pkthdr
.len
= m
->m_len
;
392 * String representation for RPC.
395 u_long len
; /* length without null or padding */
396 u_char data
[4]; /* data (longer, of course) */
397 /* data is padded to a long-word boundary */
399 /* Compute space used given string length. */
400 #define RPC_STR_SIZE(slen) (4 + ((slen + 3) & ~3))
403 * Inet address in RPC messages
404 * (Note, really four longs, NOT chars. Blech.)
413 * RPC: bootparam/whoami
414 * Given client IP address, get:
415 * client name (hostname)
416 * domain name (domainname)
419 * The hostname and domainname are set here for convenience.
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).
428 bp_whoami(bpsin
, my_ip
, gw_ip
)
429 struct sockaddr_in
*bpsin
;
430 struct in_addr
*my_ip
;
431 struct in_addr
*gw_ip
;
433 /* RPC structures for PMAPPROC_CALLIT */
439 struct bp_inaddr call_ia
;
442 struct rpc_string
*str
;
443 struct bp_inaddr
*bia
;
445 struct sockaddr_in
*sin
;
452 * Get message buffer of sufficient size.
454 msg_len
= sizeof(*call
);
455 m
= m_get_len(msg_len
);
460 * Build request message for PMAPPROC_CALLIT.
462 call
= mtod(m
, struct whoami_call
*);
463 call
->call_prog
= htonl(BOOTPARAM_PROG
);
464 call
->call_vers
= htonl(BOOTPARAM_VERS
);
465 call
->call_proc
= htonl(BOOTPARAM_WHOAMI
);
466 call
->call_arglen
= htonl(sizeof(struct bp_inaddr
));
468 /* client IP address */
469 call
->call_ia
.atype
= htonl(1);
471 lp
= call
->call_ia
.addr
;
472 *lp
++ = htonl(*p
); p
++;
473 *lp
++ = htonl(*p
); p
++;
474 *lp
++ = htonl(*p
); p
++;
475 *lp
++ = htonl(*p
); p
++;
477 /* RPC: portmap/callit */
478 bpsin
->sin_port
= htons(PMAPPORT
);
480 error
= krpc_call(bpsin
, PMAPPROG
, PMAPVERS
,
481 PMAPPROC_CALLIT
, &m
, &sin
);
486 * Parse result message.
489 lp
= mtod(m
, long *);
491 /* bootparam server port (also grab from address). */
492 if (msg_len
< sizeof(*lp
))
494 msg_len
-= sizeof(*lp
);
495 bpsin
->sin_port
= htons((short)ntohl(*lp
++));
496 bpsin
->sin_addr
.s_addr
= sin
->sin_addr
.s_addr
;
498 /* length of encapsulated results */
499 if (msg_len
< (ntohl(*lp
) + sizeof(*lp
)))
501 msg_len
= ntohl(*lp
++);
505 if (msg_len
< sizeof(*str
))
507 str
= (struct rpc_string
*)p
;
508 cn_len
= ntohl(str
->len
);
509 if (msg_len
< cn_len
)
511 if (cn_len
>= MAXHOSTNAMELEN
)
513 bcopy(str
->data
, hostname
, cn_len
);
514 hostname
[cn_len
] = '\0';
515 hostnamelen
= cn_len
;
516 p
+= RPC_STR_SIZE(cn_len
);
517 msg_len
-= RPC_STR_SIZE(cn_len
);
520 if (msg_len
< sizeof(*str
))
522 str
= (struct rpc_string
*)p
;
523 dn_len
= ntohl(str
->len
);
524 if (msg_len
< dn_len
)
526 if (dn_len
>= MAXHOSTNAMELEN
)
528 bcopy(str
->data
, domainname
, dn_len
);
529 domainname
[dn_len
] = '\0';
530 domainnamelen
= dn_len
;
531 p
+= RPC_STR_SIZE(dn_len
);
532 msg_len
-= RPC_STR_SIZE(dn_len
);
534 /* gateway address */
535 if (msg_len
< sizeof(*bia
))
537 bia
= (struct bp_inaddr
*)p
;
538 if (bia
->atype
!= htonl(1))
541 *p
++ = ntohl(bia
->addr
[0]);
542 *p
++ = ntohl(bia
->addr
[1]);
543 *p
++ = ntohl(bia
->addr
[2]);
544 *p
++ = ntohl(bia
->addr
[3]);
548 printf("nfs_boot: bootparam_whoami: bad reply\n");
561 * RPC: bootparam/getfile
562 * Given client name and file "key", get:
568 bp_getfile(bpsin
, key
, md_sin
, serv_name
, pathname
)
569 struct sockaddr_in
*bpsin
;
571 struct sockaddr_in
*md_sin
;
575 struct rpc_string
*str
;
577 struct bp_inaddr
*bia
;
578 struct sockaddr_in
*sin
;
581 int cn_len
, key_len
, sn_len
, path_len
;
584 * Get message buffer of sufficient size.
586 cn_len
= hostnamelen
;
587 key_len
= strlen(key
);
589 msg_len
+= RPC_STR_SIZE(cn_len
);
590 msg_len
+= RPC_STR_SIZE(key_len
);
591 m
= m_get_len(msg_len
);
596 * Build request message.
598 p
= mtod(m
, u_char
*);
600 /* client name (hostname) */
601 str
= (struct rpc_string
*)p
;
602 str
->len
= htonl(cn_len
);
603 bcopy(hostname
, str
->data
, cn_len
);
604 p
+= RPC_STR_SIZE(cn_len
);
605 /* key name (root or swap) */
606 str
= (struct rpc_string
*)p
;
607 str
->len
= htonl(key_len
);
608 bcopy(key
, str
->data
, key_len
);
610 /* RPC: bootparam/getfile */
611 error
= krpc_call(bpsin
, BOOTPARAM_PROG
, BOOTPARAM_VERS
,
612 BOOTPARAM_GETFILE
, &m
, NULL
);
617 * Parse result message.
619 p
= mtod(m
, u_char
*);
623 if (msg_len
< sizeof(*str
))
625 str
= (struct rpc_string
*)p
;
626 sn_len
= ntohl(str
->len
);
627 if (msg_len
< sn_len
)
629 if (sn_len
>= MNAMELEN
)
631 bcopy(str
->data
, serv_name
, sn_len
);
632 serv_name
[sn_len
] = '\0';
633 p
+= RPC_STR_SIZE(sn_len
);
634 msg_len
-= RPC_STR_SIZE(sn_len
);
636 /* server IP address (mountd) */
637 if (msg_len
< sizeof(*bia
))
639 bia
= (struct bp_inaddr
*)p
;
640 if (bia
->atype
!= htonl(1))
643 bzero((caddr_t
)sin
, sizeof(*sin
));
644 sin
->sin_len
= sizeof(*sin
);
645 sin
->sin_family
= AF_INET
;
646 q
= (u_char
*) &sin
->sin_addr
;
647 *q
++ = ntohl(bia
->addr
[0]);
648 *q
++ = ntohl(bia
->addr
[1]);
649 *q
++ = ntohl(bia
->addr
[2]);
650 *q
++ = ntohl(bia
->addr
[3]);
652 msg_len
-= sizeof(*bia
);
654 /* server pathname */
655 if (msg_len
< sizeof(*str
))
657 str
= (struct rpc_string
*)p
;
658 path_len
= ntohl(str
->len
);
659 if (msg_len
< path_len
)
661 if (path_len
>= MAXPATHLEN
)
663 bcopy(str
->data
, pathname
, path_len
);
664 pathname
[path_len
] = '\0';
668 printf("nfs_boot: bootparam_getfile: bad reply\n");
679 * Given a server pathname, get an NFS file handle.
680 * Also, sets sin->sin_port to the NFS service port.
683 md_mount(mdsin
, path
, v3
, fhp
, fhlenp
)
684 struct sockaddr_in
*mdsin
; /* mountd server address */
690 /* The RPC structures */
691 struct rpc_string
*str
;
694 u_char data
[NFSX_V3FHMAX
+ sizeof(u_long
)];
697 int error
, mlen
, slen
;
698 int mntversion
= v3
? RPCMNT_VER3
: RPCMNT_VER1
;
700 /* Get port number for MOUNTD. */
701 error
= krpc_portmap(mdsin
, RPCPROG_MNT
, mntversion
,
703 if (error
) return error
;
706 mlen
= RPC_STR_SIZE(slen
);
711 str
= mtod(m
, struct rpc_string
*);
712 str
->len
= htonl(slen
);
713 bcopy(path
, str
->data
, slen
);
715 /* Do RPC to mountd. */
716 error
= krpc_call(mdsin
, RPCPROG_MNT
, mntversion
,
717 RPCMNT_MOUNT
, &m
, NULL
);
719 return error
; /* message already freed */
722 * the reply must be long enough to hold the errno plus either of:
724 * + a v3 filehandle length + a v3 filehandle
727 if (mlen
< sizeof(u_long
))
729 rdata
= mtod(m
, struct rdata
*);
730 error
= ntohl(rdata
->errno
);
736 if (mlen
< sizeof(u_long
)*2)
738 fhlen
= ntohl(*(u_long
*)rdata
->data
);
739 fh
= rdata
->data
+ sizeof(u_long
);
740 if (mlen
< (sizeof(u_long
)*2 + fhlen
))
742 bcopy(fh
, fhp
, fhlen
);
745 if (mlen
< (sizeof(u_long
) + NFSX_V2FH
))
747 bcopy(rdata
->data
, fhp
, NFSX_V2FH
);
751 /* Set port number for NFS use. */
752 error
= krpc_portmap(mdsin
, NFS_PROG
, v3
? NFS_VER3
: NFS_VER2
,