2 * Copyright (c) 2001-2012 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
31 * 14 December, 2001 Dieter Siegmund (dieter@apple.com)
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/ioctl.h>
39 #include <sys/proc_internal.h>
40 #include <sys/mount_internal.h>
42 #include <sys/filedesc.h>
43 #include <sys/vnode_internal.h>
44 #include <sys/malloc.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/reboot.h>
48 #include <sys/kauth.h>
50 #include <net/if_dl.h>
51 #include <net/if_types.h>
52 #include <net/route.h>
53 #include <netinet/in.h>
54 #include <netinet/if_ether.h>
55 #include <netinet/dhcp_options.h>
56 #include <netinet/in_dhcp.h>
58 #include <kern/kern_types.h>
59 #include <kern/kalloc.h>
60 #include <sys/netboot.h>
61 #include <sys/imageboot.h>
62 #include <pexpert/pexpert.h>
64 //#include <libkern/libkern.h>
65 extern struct filedesc filedesc0
;
67 extern int nfs_mountroot(void); /* nfs_vfsops.c */
68 extern int (*mountroot
)(void);
70 extern unsigned char rootdevice
[];
72 static int S_netboot
= 0;
73 static struct netboot_info
* S_netboot_info_p
;
76 IOBSDRegistryEntryForDeviceTree(const char * path
);
79 IOBSDRegistryEntryRelease(void * entry
);
82 IOBSDRegistryEntryGetData(void * entry
, const char * property_name
,
85 #define BOOTP_RESPONSE "bootp-response"
86 #define BSDP_RESPONSE "bsdp-response"
87 #define DHCP_RESPONSE "dhcp-response"
89 /* forward declarations */
90 int inet_aton(char * cp
, struct in_addr
* pin
);
92 #define IP_FORMAT "%d.%d.%d.%d"
93 #define IP_CH(ip) ((u_char *)ip)
94 #define IP_LIST(ip) IP_CH(ip)[0],IP_CH(ip)[1],IP_CH(ip)[2],IP_CH(ip)[3]
96 #define kNetBootRootPathPrefixNFS "nfs:"
97 #define kNetBootRootPathPrefixHTTP "http:"
100 kNetBootImageTypeUnknown
= 0,
101 kNetBootImageTypeNFS
= 1,
102 kNetBootImageTypeHTTP
= 2,
105 struct netboot_info
{
106 struct in_addr client_ip
;
107 struct in_addr server_ip
;
109 int server_name_length
;
111 int mount_point_length
;
113 int image_path_length
;
114 NetBootImageType image_type
;
115 char * second_image_path
;
116 int second_image_path_length
;
120 * Function: parse_booter_path
122 * Parse a string of the form:
123 * "<IP>:<host>:<mount>[:<image_path>]"
124 * into the given ip address, host, mount point, and optionally, image_path.
127 * The passed in string is modified i.e. ':' is replaced by '\0'.
129 * "17.202.16.17:seaport:/release/.images/Image9/CurrentHera"
131 static __inline__ boolean_t
132 parse_booter_path(char * path
, struct in_addr
* iaddr_p
, char const * * host
,
133 char * * mount_dir
, char * * image_path
)
140 colon
= strchr(start
, ':');
145 if (inet_aton(start
, iaddr_p
) != 1) {
151 colon
= strchr(start
, ':');
160 colon
= strchr(start
, ':');
175 * Function: find_colon
177 * Find the next unescaped instance of the colon character.
178 * If a colon is escaped (preceded by a backslash '\' character),
179 * shift the string over by one character to overwrite the backslash.
181 static __inline__
char *
182 find_colon(char * str
)
187 while ((colon
= strchr(start
, ':')) != NULL
) {
191 if (colon
== start
) {
194 if (colon
[-1] != '\\')
196 for (dst
= colon
- 1, src
= colon
; *dst
!= '\0'; dst
++, src
++) {
205 * Function: parse_netboot_path
207 * Parse a string of the form:
208 * "nfs:<IP>:<mount>[:<image_path>]"
209 * into the given ip address, host, mount point, and optionally, image_path.
211 * - the passed in string is modified i.e. ':' is replaced by '\0'
212 * - literal colons must be escaped with a backslash
215 * nfs:17.202.42.112:/Library/NetBoot/NetBootSP0:Jaguar/Jaguar.dmg
216 * nfs:17.202.42.112:/Volumes/Foo\:/Library/NetBoot/NetBootSP0:Jaguar/Jaguar.dmg
218 static __inline__ boolean_t
219 parse_netboot_path(char * path
, struct in_addr
* iaddr_p
, char const * * host
,
220 char * * mount_dir
, char * * image_path
)
222 static char tmp
[MAX_IPv4_STR_LEN
]; /* Danger - not thread safe */
226 if (strncmp(path
, kNetBootRootPathPrefixNFS
,
227 strlen(kNetBootRootPathPrefixNFS
)) != 0) {
232 start
= path
+ strlen(kNetBootRootPathPrefixNFS
);
233 colon
= strchr(start
, ':');
238 if (inet_aton(start
, iaddr_p
) != 1) {
244 colon
= find_colon(start
);
253 (void)find_colon(start
);
256 *host
= inet_ntop(AF_INET
, iaddr_p
, tmp
, sizeof(tmp
));
261 parse_image_path(char * path
, struct in_addr
* iaddr_p
, char const * * host
,
262 char * * mount_dir
, char * * image_path
)
264 if (path
[0] >= '0' && path
[0] <= '9') {
265 return (parse_booter_path(path
, iaddr_p
, host
, mount_dir
,
268 return (parse_netboot_path(path
, iaddr_p
, host
, mount_dir
,
273 get_root_path(char * root_path
)
276 boolean_t found
= FALSE
;
280 entry
= IOBSDRegistryEntryForDeviceTree("/chosen");
284 pkt
= IOBSDRegistryEntryGetData(entry
, BSDP_RESPONSE
, &pkt_len
);
285 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
286 printf("netboot: retrieving root path from BSDP response\n");
289 pkt
= IOBSDRegistryEntryGetData(entry
, BOOTP_RESPONSE
,
291 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
292 printf("netboot: retrieving root path from BOOTP response\n");
299 const struct dhcp
* reply
;
301 reply
= (const struct dhcp
*)pkt
;
302 (void)dhcpol_parse_packet(&options
, reply
, pkt_len
);
304 path
= (const char *)dhcpol_find(&options
,
305 dhcptag_root_path_e
, &len
, NULL
);
307 memcpy(root_path
, path
, len
);
308 root_path
[len
] = '\0';
312 IOBSDRegistryEntryRelease(entry
);
318 save_path(char * * str_p
, int * length_p
, char * path
)
320 *length_p
= strlen(path
) + 1;
321 *str_p
= (char *)kalloc(*length_p
);
322 strlcpy(*str_p
, path
, *length_p
);
326 static struct netboot_info
*
327 netboot_info_init(struct in_addr iaddr
)
329 boolean_t have_root_path
= FALSE
;
330 struct netboot_info
* info
= NULL
;
331 char * root_path
= NULL
;
333 info
= (struct netboot_info
*)kalloc(sizeof(*info
));
334 bzero(info
, sizeof(*info
));
335 info
->client_ip
= iaddr
;
336 info
->image_type
= kNetBootImageTypeUnknown
;
338 /* check for a booter-specified path then a NetBoot path */
339 MALLOC_ZONE(root_path
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
340 if (root_path
== NULL
)
341 panic("netboot_info_init: M_NAMEI zone exhausted");
342 if (PE_parse_boot_argn("rp0", root_path
, MAXPATHLEN
) == TRUE
343 || PE_parse_boot_argn("rp", root_path
, MAXPATHLEN
) == TRUE
344 || PE_parse_boot_argn("rootpath", root_path
, MAXPATHLEN
) == TRUE
) {
345 if (imageboot_format_is_valid(root_path
)) {
346 printf("netboot_info_init: rp0='%s' isn't a network path,"
347 " ignoring\n", root_path
);
350 have_root_path
= TRUE
;
353 if (have_root_path
== FALSE
) {
354 have_root_path
= get_root_path(root_path
);
356 if (have_root_path
) {
357 const char * server_name
= NULL
;
358 char * mount_point
= NULL
;
359 char * image_path
= NULL
;
360 struct in_addr server_ip
;
362 if (parse_image_path(root_path
, &server_ip
, &server_name
,
363 &mount_point
, &image_path
)) {
364 info
->image_type
= kNetBootImageTypeNFS
;
365 info
->server_ip
= server_ip
;
366 info
->server_name_length
= strlen(server_name
) + 1;
367 info
->server_name
= (char *)kalloc(info
->server_name_length
);
368 info
->mount_point_length
= strlen(mount_point
) + 1;
369 info
->mount_point
= (char *)kalloc(info
->mount_point_length
);
370 strlcpy(info
->server_name
, server_name
, info
->server_name_length
);
371 strlcpy(info
->mount_point
, mount_point
, info
->mount_point_length
);
373 printf("netboot: NFS Server %s Mount %s",
374 server_name
, info
->mount_point
);
375 if (image_path
!= NULL
) {
376 boolean_t needs_slash
= FALSE
;
378 info
->image_path_length
= strlen(image_path
) + 1;
379 if (image_path
[0] != '/') {
381 info
->image_path_length
++;
383 info
->image_path
= (char *)kalloc(info
->image_path_length
);
385 info
->image_path
[0] = '/';
386 strlcpy(info
->image_path
+ 1, image_path
,
387 info
->image_path_length
- 1);
389 strlcpy(info
->image_path
, image_path
,
390 info
->image_path_length
);
392 printf(" Image %s", info
->image_path
);
396 else if (strncmp(root_path
, kNetBootRootPathPrefixHTTP
,
397 strlen(kNetBootRootPathPrefixHTTP
)) == 0) {
398 info
->image_type
= kNetBootImageTypeHTTP
;
399 save_path(&info
->image_path
, &info
->image_path_length
,
401 printf("netboot: HTTP URL %s\n", info
->image_path
);
404 printf("netboot: root path uses unrecognized format\n");
407 /* check for image-within-image */
408 if (info
->image_path
!= NULL
) {
409 if (PE_parse_boot_argn(IMAGEBOOT_ROOT_ARG
, root_path
, MAXPATHLEN
)
410 || PE_parse_boot_argn("rp1", root_path
, MAXPATHLEN
)) {
411 /* rp1/root-dmg is the second-level image */
412 save_path(&info
->second_image_path
, &info
->second_image_path_length
,
416 if (info
->second_image_path
!= NULL
) {
417 printf("netboot: nested image %s\n", info
->second_image_path
);
420 FREE_ZONE(root_path
, MAXPATHLEN
, M_NAMEI
);
425 netboot_info_free(struct netboot_info
* * info_p
)
427 struct netboot_info
* info
= *info_p
;
430 if (info
->mount_point
) {
431 kfree(info
->mount_point
, info
->mount_point_length
);
433 if (info
->server_name
) {
434 kfree(info
->server_name
, info
->server_name_length
);
436 if (info
->image_path
) {
437 kfree(info
->image_path
, info
->image_path_length
);
439 if (info
->second_image_path
) {
440 kfree(info
->second_image_path
, info
->second_image_path_length
);
442 kfree(info
, sizeof(*info
));
449 netboot_iaddr(struct in_addr
* iaddr_p
)
451 if (S_netboot_info_p
== NULL
)
454 *iaddr_p
= S_netboot_info_p
->client_ip
;
459 netboot_rootpath(struct in_addr
* server_ip
,
460 char * name
, int name_len
,
461 char * path
, int path_len
)
463 if (S_netboot_info_p
== NULL
)
469 if (S_netboot_info_p
->mount_point_length
== 0) {
472 if (path_len
< S_netboot_info_p
->mount_point_length
) {
473 printf("netboot: path too small %d < %d\n",
474 path_len
, S_netboot_info_p
->mount_point_length
);
477 strlcpy(path
, S_netboot_info_p
->mount_point
, path_len
);
478 strlcpy(name
, S_netboot_info_p
->server_name
, name_len
);
479 *server_ip
= S_netboot_info_p
->server_ip
;
485 get_ip_parameters(struct in_addr
* iaddr_p
, struct in_addr
* netmask_p
,
486 struct in_addr
* router_p
)
493 entry
= IOBSDRegistryEntryForDeviceTree("/chosen");
497 pkt
= IOBSDRegistryEntryGetData(entry
, DHCP_RESPONSE
, &pkt_len
);
498 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
499 printf("netboot: retrieving IP information from DHCP response\n");
502 pkt
= IOBSDRegistryEntryGetData(entry
, BOOTP_RESPONSE
, &pkt_len
);
503 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
504 printf("netboot: retrieving IP information from BOOTP response\n");
508 const struct in_addr
* ip
;
511 const struct dhcp
* reply
;
513 reply
= (const struct dhcp
*)pkt
;
514 (void)dhcpol_parse_packet(&options
, reply
, pkt_len
);
515 *iaddr_p
= reply
->dp_yiaddr
;
516 ip
= (const struct in_addr
*)
517 dhcpol_find(&options
,
518 dhcptag_subnet_mask_e
, &len
, NULL
);
522 ip
= (const struct in_addr
*)
523 dhcpol_find(&options
, dhcptag_router_e
, &len
, NULL
);
528 IOBSDRegistryEntryRelease(entry
);
529 return (pkt
!= NULL
);
533 route_cmd(int cmd
, struct in_addr d
, struct in_addr g
,
534 struct in_addr m
, uint32_t more_flags
, unsigned int ifscope
)
536 struct sockaddr_in dst
;
538 uint32_t flags
= RTF_UP
| RTF_STATIC
;
539 struct sockaddr_in gw
;
540 struct sockaddr_in mask
;
545 bzero((caddr_t
)&dst
, sizeof(dst
));
546 dst
.sin_len
= sizeof(dst
);
547 dst
.sin_family
= AF_INET
;
551 bzero((caddr_t
)&gw
, sizeof(gw
));
552 gw
.sin_len
= sizeof(gw
);
553 gw
.sin_family
= AF_INET
;
557 bzero(&mask
, sizeof(mask
));
558 mask
.sin_len
= sizeof(mask
);
559 mask
.sin_family
= AF_INET
;
562 error
= rtrequest_scoped(cmd
, (struct sockaddr
*)&dst
,
563 (struct sockaddr
*)&gw
, (struct sockaddr
*)&mask
, flags
, NULL
, ifscope
);
570 default_route_add(struct in_addr router
, boolean_t proxy_arp
)
573 struct in_addr zeroes
= { 0 };
575 if (proxy_arp
== FALSE
) {
576 flags
|= RTF_GATEWAY
;
578 return (route_cmd(RTM_ADD
, zeroes
, router
, zeroes
, flags
, IFSCOPE_NONE
));
582 host_route_delete(struct in_addr host
, unsigned int ifscope
)
584 struct in_addr zeroes
= { 0 };
586 return (route_cmd(RTM_DELETE
, host
, zeroes
, zeroes
, RTF_HOST
, ifscope
));
589 static struct ifnet
*
592 struct ifnet
* ifp
= NULL
;
596 ifp
= ifunit((char *)rootdevice
);
599 ifnet_head_lock_shared();
600 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
)
601 if ((ifp
->if_flags
& (IFF_LOOPBACK
|IFF_POINTOPOINT
)) == 0)
610 netboot_mountroot(void)
613 struct in_addr iaddr
= { 0 };
616 struct in_addr netmask
= { 0 };
617 proc_t procp
= current_proc();
618 struct in_addr router
= { 0 };
619 struct socket
* so
= NULL
;
622 bzero(&ifr
, sizeof(ifr
));
624 /* find the interface */
625 ifp
= find_interface();
627 printf("netboot: no suitable interface\n");
631 snprintf(ifr
.ifr_name
, sizeof(ifr
.ifr_name
), "%s%d", ifp
->if_name
,
633 printf("netboot: using network interface '%s'\n", ifr
.ifr_name
);
636 if ((error
= socreate(AF_INET
, &so
, SOCK_DGRAM
, 0)) != 0) {
637 printf("netboot: socreate, error=%d\n", error
);
640 ifr
.ifr_flags
= ifp
->if_flags
| IFF_UP
;
641 error
= ifioctl(so
, SIOCSIFFLAGS
, (caddr_t
)&ifr
, procp
);
643 printf("netboot: SIFFLAGS, error=%d\n", error
);
647 /* grab information from the registry */
648 if (get_ip_parameters(&iaddr
, &netmask
, &router
) == FALSE
) {
649 /* use DHCP to retrieve IP address, netmask and router */
650 error
= dhcp(ifp
, &iaddr
, 64, &netmask
, &router
, procp
);
652 printf("netboot: DHCP failed %d\n", error
);
656 printf("netboot: IP address " IP_FORMAT
, IP_LIST(&iaddr
));
657 if (netmask
.s_addr
) {
658 printf(" netmask " IP_FORMAT
, IP_LIST(&netmask
));
661 printf(" router " IP_FORMAT
, IP_LIST(&router
));
664 error
= inet_aifaddr(so
, ifr
.ifr_name
, &iaddr
, &netmask
, NULL
);
666 printf("netboot: inet_aifaddr failed, %d\n", error
);
669 if (router
.s_addr
== 0) {
670 /* enable proxy arp if we don't have a router */
671 router
.s_addr
= iaddr
.s_addr
;
673 printf("netboot: adding default route " IP_FORMAT
"\n",
675 error
= default_route_add(router
, router
.s_addr
== iaddr
.s_addr
);
677 printf("netboot: default_route_add failed %d\n", error
);
682 S_netboot_info_p
= netboot_info_init(iaddr
);
683 switch (S_netboot_info_p
->image_type
) {
685 case kNetBootImageTypeNFS
:
686 for (try = 1; TRUE
; try++) {
687 error
= nfs_mountroot();
691 printf("netboot: nfs_mountroot() attempt %u failed; "
692 "clearing ARP entry and trying again\n", try);
694 * error is either EHOSTDOWN or EHOSTUNREACH, which likely means
695 * that the port we're plugged into has spanning tree enabled,
696 * and either the router or the server can't answer our ARP
697 * requests. Clear the incomplete ARP entry by removing the
698 * appropriate route, depending on the error code:
699 * EHOSTDOWN NFS server's route
700 * EHOSTUNREACH router's route
706 /* remove the server's arp entry */
707 error
= host_route_delete(S_netboot_info_p
->server_ip
,
710 printf("netboot: host_route_delete(" IP_FORMAT
712 IP_LIST(&S_netboot_info_p
->server_ip
), error
);
716 error
= host_route_delete(router
, ifp
->if_index
);
718 printf("netboot: host_route_delete(" IP_FORMAT
719 ") failed %d\n", IP_LIST(&router
), error
);
725 case kNetBootImageTypeHTTP
:
726 error
= netboot_setup();
748 if (S_netboot_info_p
== NULL
749 || S_netboot_info_p
->image_path
== NULL
) {
752 printf("netboot_setup: calling imageboot_mount_image\n");
753 error
= imageboot_mount_image(S_netboot_info_p
->image_path
, -1);
755 printf("netboot: failed to mount root image, %d\n", error
);
757 else if (S_netboot_info_p
->second_image_path
!= NULL
) {
758 error
= imageboot_mount_image(S_netboot_info_p
->second_image_path
, 0);
760 printf("netboot: failed to mount second root image, %d\n", error
);
765 netboot_info_free(&S_netboot_info_p
);