2 * Copyright (c) 2001-2013 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 #define IP_FORMAT "%d.%d.%d.%d"
90 #define IP_CH(ip) ((u_char *)ip)
91 #define IP_LIST(ip) IP_CH(ip)[0],IP_CH(ip)[1],IP_CH(ip)[2],IP_CH(ip)[3]
93 #define kNetBootRootPathPrefixNFS "nfs:"
94 #define kNetBootRootPathPrefixHTTP "http:"
97 kNetBootImageTypeUnknown
= 0,
98 kNetBootImageTypeNFS
= 1,
99 kNetBootImageTypeHTTP
= 2,
102 struct netboot_info
{
103 struct in_addr client_ip
;
104 struct in_addr server_ip
;
106 int server_name_length
;
108 int mount_point_length
;
110 int image_path_length
;
111 NetBootImageType image_type
;
112 char * second_image_path
;
113 int second_image_path_length
;
117 * Function: parse_booter_path
119 * Parse a string of the form:
120 * "<IP>:<host>:<mount>[:<image_path>]"
121 * into the given ip address, host, mount point, and optionally, image_path.
124 * The passed in string is modified i.e. ':' is replaced by '\0'.
126 * "17.202.16.17:seaport:/release/.images/Image9/CurrentHera"
128 static __inline__ boolean_t
129 parse_booter_path(char * path
, struct in_addr
* iaddr_p
, char const * * host
,
130 char * * mount_dir
, char * * image_path
)
137 colon
= strchr(start
, ':');
142 if (inet_aton(start
, iaddr_p
) != 1) {
148 colon
= strchr(start
, ':');
157 colon
= strchr(start
, ':');
172 * Function: find_colon
174 * Find the next unescaped instance of the colon character.
175 * If a colon is escaped (preceded by a backslash '\' character),
176 * shift the string over by one character to overwrite the backslash.
178 static __inline__
char *
179 find_colon(char * str
)
184 while ((colon
= strchr(start
, ':')) != NULL
) {
188 if (colon
== start
) {
191 if (colon
[-1] != '\\')
193 for (dst
= colon
- 1, src
= colon
; *dst
!= '\0'; dst
++, src
++) {
202 * Function: parse_netboot_path
204 * Parse a string of the form:
205 * "nfs:<IP>:<mount>[:<image_path>]"
206 * into the given ip address, host, mount point, and optionally, image_path.
208 * - the passed in string is modified i.e. ':' is replaced by '\0'
209 * - literal colons must be escaped with a backslash
212 * nfs:17.202.42.112:/Library/NetBoot/NetBootSP0:Jaguar/Jaguar.dmg
213 * nfs:17.202.42.112:/Volumes/Foo\:/Library/NetBoot/NetBootSP0:Jaguar/Jaguar.dmg
215 static __inline__ boolean_t
216 parse_netboot_path(char * path
, struct in_addr
* iaddr_p
, char const * * host
,
217 char * * mount_dir
, char * * image_path
)
219 static char tmp
[MAX_IPv4_STR_LEN
]; /* Danger - not thread safe */
223 if (strncmp(path
, kNetBootRootPathPrefixNFS
,
224 strlen(kNetBootRootPathPrefixNFS
)) != 0) {
229 start
= path
+ strlen(kNetBootRootPathPrefixNFS
);
230 colon
= strchr(start
, ':');
235 if (inet_aton(start
, iaddr_p
) != 1) {
241 colon
= find_colon(start
);
250 (void)find_colon(start
);
253 *host
= inet_ntop(AF_INET
, iaddr_p
, tmp
, sizeof(tmp
));
258 parse_image_path(char * path
, struct in_addr
* iaddr_p
, char const * * host
,
259 char * * mount_dir
, char * * image_path
)
261 if (path
[0] >= '0' && path
[0] <= '9') {
262 return (parse_booter_path(path
, iaddr_p
, host
, mount_dir
,
265 return (parse_netboot_path(path
, iaddr_p
, host
, mount_dir
,
270 get_root_path(char * root_path
)
273 boolean_t found
= FALSE
;
277 entry
= IOBSDRegistryEntryForDeviceTree("/chosen");
281 pkt
= IOBSDRegistryEntryGetData(entry
, BSDP_RESPONSE
, &pkt_len
);
282 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
283 printf("netboot: retrieving root path from BSDP response\n");
286 pkt
= IOBSDRegistryEntryGetData(entry
, BOOTP_RESPONSE
,
288 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
289 printf("netboot: retrieving root path from BOOTP response\n");
296 const struct dhcp
* reply
;
298 reply
= (const struct dhcp
*)pkt
;
299 (void)dhcpol_parse_packet(&options
, reply
, pkt_len
);
301 path
= (const char *)dhcpol_find(&options
,
302 dhcptag_root_path_e
, &len
, NULL
);
304 memcpy(root_path
, path
, len
);
305 root_path
[len
] = '\0';
309 IOBSDRegistryEntryRelease(entry
);
315 save_path(char * * str_p
, int * length_p
, char * path
)
317 *length_p
= strlen(path
) + 1;
318 *str_p
= (char *)kalloc(*length_p
);
319 strlcpy(*str_p
, path
, *length_p
);
323 static struct netboot_info
*
324 netboot_info_init(struct in_addr iaddr
)
326 boolean_t have_root_path
= FALSE
;
327 struct netboot_info
* info
= NULL
;
328 char * root_path
= NULL
;
330 info
= (struct netboot_info
*)kalloc(sizeof(*info
));
331 bzero(info
, sizeof(*info
));
332 info
->client_ip
= iaddr
;
333 info
->image_type
= kNetBootImageTypeUnknown
;
335 /* check for a booter-specified path then a NetBoot path */
336 MALLOC_ZONE(root_path
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
337 if (root_path
== NULL
)
338 panic("netboot_info_init: M_NAMEI zone exhausted");
339 if (PE_parse_boot_argn("rp0", root_path
, MAXPATHLEN
) == TRUE
340 || PE_parse_boot_argn("rp", root_path
, MAXPATHLEN
) == TRUE
341 || PE_parse_boot_argn("rootpath", root_path
, MAXPATHLEN
) == TRUE
) {
342 if (imageboot_format_is_valid(root_path
)) {
343 printf("netboot_info_init: rp0='%s' isn't a network path,"
344 " ignoring\n", root_path
);
347 have_root_path
= TRUE
;
350 if (have_root_path
== FALSE
) {
351 have_root_path
= get_root_path(root_path
);
353 if (have_root_path
) {
354 const char * server_name
= NULL
;
355 char * mount_point
= NULL
;
356 char * image_path
= NULL
;
357 struct in_addr server_ip
;
359 if (parse_image_path(root_path
, &server_ip
, &server_name
,
360 &mount_point
, &image_path
)) {
361 info
->image_type
= kNetBootImageTypeNFS
;
362 info
->server_ip
= server_ip
;
363 info
->server_name_length
= strlen(server_name
) + 1;
364 info
->server_name
= (char *)kalloc(info
->server_name_length
);
365 info
->mount_point_length
= strlen(mount_point
) + 1;
366 info
->mount_point
= (char *)kalloc(info
->mount_point_length
);
367 strlcpy(info
->server_name
, server_name
, info
->server_name_length
);
368 strlcpy(info
->mount_point
, mount_point
, info
->mount_point_length
);
370 printf("netboot: NFS Server %s Mount %s",
371 server_name
, info
->mount_point
);
372 if (image_path
!= NULL
) {
373 boolean_t needs_slash
= FALSE
;
375 info
->image_path_length
= strlen(image_path
) + 1;
376 if (image_path
[0] != '/') {
378 info
->image_path_length
++;
380 info
->image_path
= (char *)kalloc(info
->image_path_length
);
382 info
->image_path
[0] = '/';
383 strlcpy(info
->image_path
+ 1, image_path
,
384 info
->image_path_length
- 1);
386 strlcpy(info
->image_path
, image_path
,
387 info
->image_path_length
);
389 printf(" Image %s", info
->image_path
);
393 else if (strncmp(root_path
, kNetBootRootPathPrefixHTTP
,
394 strlen(kNetBootRootPathPrefixHTTP
)) == 0) {
395 info
->image_type
= kNetBootImageTypeHTTP
;
396 save_path(&info
->image_path
, &info
->image_path_length
,
398 printf("netboot: HTTP URL %s\n", info
->image_path
);
401 printf("netboot: root path uses unrecognized format\n");
404 /* check for image-within-image */
405 if (info
->image_path
!= NULL
) {
406 if (PE_parse_boot_argn(IMAGEBOOT_ROOT_ARG
, root_path
, MAXPATHLEN
)
407 || PE_parse_boot_argn("rp1", root_path
, MAXPATHLEN
)) {
408 /* rp1/root-dmg is the second-level image */
409 save_path(&info
->second_image_path
, &info
->second_image_path_length
,
413 if (info
->second_image_path
!= NULL
) {
414 printf("netboot: nested image %s\n", info
->second_image_path
);
417 FREE_ZONE(root_path
, MAXPATHLEN
, M_NAMEI
);
422 netboot_info_free(struct netboot_info
* * info_p
)
424 struct netboot_info
* info
= *info_p
;
427 if (info
->mount_point
) {
428 kfree(info
->mount_point
, info
->mount_point_length
);
430 if (info
->server_name
) {
431 kfree(info
->server_name
, info
->server_name_length
);
433 if (info
->image_path
) {
434 kfree(info
->image_path
, info
->image_path_length
);
436 if (info
->second_image_path
) {
437 kfree(info
->second_image_path
, info
->second_image_path_length
);
439 kfree(info
, sizeof(*info
));
446 netboot_iaddr(struct in_addr
* iaddr_p
)
448 if (S_netboot_info_p
== NULL
)
451 *iaddr_p
= S_netboot_info_p
->client_ip
;
456 netboot_rootpath(struct in_addr
* server_ip
,
457 char * name
, int name_len
,
458 char * path
, int path_len
)
460 if (S_netboot_info_p
== NULL
)
466 if (S_netboot_info_p
->mount_point_length
== 0) {
469 if (path_len
< S_netboot_info_p
->mount_point_length
) {
470 printf("netboot: path too small %d < %d\n",
471 path_len
, S_netboot_info_p
->mount_point_length
);
474 strlcpy(path
, S_netboot_info_p
->mount_point
, path_len
);
475 strlcpy(name
, S_netboot_info_p
->server_name
, name_len
);
476 *server_ip
= S_netboot_info_p
->server_ip
;
482 get_ip_parameters(struct in_addr
* iaddr_p
, struct in_addr
* netmask_p
,
483 struct in_addr
* router_p
)
490 entry
= IOBSDRegistryEntryForDeviceTree("/chosen");
494 pkt
= IOBSDRegistryEntryGetData(entry
, DHCP_RESPONSE
, &pkt_len
);
495 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
496 printf("netboot: retrieving IP information from DHCP response\n");
499 pkt
= IOBSDRegistryEntryGetData(entry
, BOOTP_RESPONSE
, &pkt_len
);
500 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
501 printf("netboot: retrieving IP information from BOOTP response\n");
505 const struct in_addr
* ip
;
508 const struct dhcp
* reply
;
510 reply
= (const struct dhcp
*)pkt
;
511 (void)dhcpol_parse_packet(&options
, reply
, pkt_len
);
512 *iaddr_p
= reply
->dp_yiaddr
;
513 ip
= (const struct in_addr
*)
514 dhcpol_find(&options
,
515 dhcptag_subnet_mask_e
, &len
, NULL
);
519 ip
= (const struct in_addr
*)
520 dhcpol_find(&options
, dhcptag_router_e
, &len
, NULL
);
525 IOBSDRegistryEntryRelease(entry
);
526 return (pkt
!= NULL
);
530 route_cmd(int cmd
, struct in_addr d
, struct in_addr g
,
531 struct in_addr m
, uint32_t more_flags
, unsigned int ifscope
)
533 struct sockaddr_in dst
;
535 uint32_t flags
= RTF_UP
| RTF_STATIC
;
536 struct sockaddr_in gw
;
537 struct sockaddr_in mask
;
542 bzero((caddr_t
)&dst
, sizeof(dst
));
543 dst
.sin_len
= sizeof(dst
);
544 dst
.sin_family
= AF_INET
;
548 bzero((caddr_t
)&gw
, sizeof(gw
));
549 gw
.sin_len
= sizeof(gw
);
550 gw
.sin_family
= AF_INET
;
554 bzero(&mask
, sizeof(mask
));
555 mask
.sin_len
= sizeof(mask
);
556 mask
.sin_family
= AF_INET
;
559 error
= rtrequest_scoped(cmd
, (struct sockaddr
*)&dst
,
560 (struct sockaddr
*)&gw
, (struct sockaddr
*)&mask
, flags
, NULL
, ifscope
);
567 default_route_add(struct in_addr router
, boolean_t proxy_arp
)
570 struct in_addr zeroes
= { 0 };
572 if (proxy_arp
== FALSE
) {
573 flags
|= RTF_GATEWAY
;
575 return (route_cmd(RTM_ADD
, zeroes
, router
, zeroes
, flags
, IFSCOPE_NONE
));
579 host_route_delete(struct in_addr host
, unsigned int ifscope
)
581 struct in_addr zeroes
= { 0 };
583 return (route_cmd(RTM_DELETE
, host
, zeroes
, zeroes
, RTF_HOST
, ifscope
));
586 static struct ifnet
*
589 struct ifnet
* ifp
= NULL
;
593 ifp
= ifunit((char *)rootdevice
);
596 ifnet_head_lock_shared();
597 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
)
598 if ((ifp
->if_flags
& (IFF_LOOPBACK
|IFF_POINTOPOINT
)) == 0)
607 netboot_mountroot(void)
610 struct in_addr iaddr
= { 0 };
613 struct in_addr netmask
= { 0 };
614 proc_t procp
= current_proc();
615 struct in_addr router
= { 0 };
616 struct socket
* so
= NULL
;
619 bzero(&ifr
, sizeof(ifr
));
621 /* find the interface */
622 ifp
= find_interface();
624 printf("netboot: no suitable interface\n");
628 snprintf(ifr
.ifr_name
, sizeof(ifr
.ifr_name
), "%s", if_name(ifp
));
629 printf("netboot: using network interface '%s'\n", ifr
.ifr_name
);
632 if ((error
= socreate(AF_INET
, &so
, SOCK_DGRAM
, 0)) != 0) {
633 printf("netboot: socreate, error=%d\n", error
);
636 ifr
.ifr_flags
= ifp
->if_flags
| IFF_UP
;
637 error
= ifioctl(so
, SIOCSIFFLAGS
, (caddr_t
)&ifr
, procp
);
639 printf("netboot: SIFFLAGS, error=%d\n", error
);
643 /* grab information from the registry */
644 if (get_ip_parameters(&iaddr
, &netmask
, &router
) == FALSE
) {
645 /* use DHCP to retrieve IP address, netmask and router */
646 error
= dhcp(ifp
, &iaddr
, 64, &netmask
, &router
, procp
);
648 printf("netboot: DHCP failed %d\n", error
);
652 printf("netboot: IP address " IP_FORMAT
, IP_LIST(&iaddr
));
653 if (netmask
.s_addr
) {
654 printf(" netmask " IP_FORMAT
, IP_LIST(&netmask
));
657 printf(" router " IP_FORMAT
, IP_LIST(&router
));
660 error
= inet_aifaddr(so
, ifr
.ifr_name
, &iaddr
, &netmask
, NULL
);
662 printf("netboot: inet_aifaddr failed, %d\n", error
);
665 if (router
.s_addr
== 0) {
666 /* enable proxy arp if we don't have a router */
667 router
.s_addr
= iaddr
.s_addr
;
669 printf("netboot: adding default route " IP_FORMAT
"\n",
671 error
= default_route_add(router
, router
.s_addr
== iaddr
.s_addr
);
673 printf("netboot: default_route_add failed %d\n", error
);
678 S_netboot_info_p
= netboot_info_init(iaddr
);
679 switch (S_netboot_info_p
->image_type
) {
681 case kNetBootImageTypeNFS
:
682 for (try = 1; TRUE
; try++) {
683 error
= nfs_mountroot();
687 printf("netboot: nfs_mountroot() attempt %u failed; "
688 "clearing ARP entry and trying again\n", try);
690 * error is either EHOSTDOWN or EHOSTUNREACH, which likely means
691 * that the port we're plugged into has spanning tree enabled,
692 * and either the router or the server can't answer our ARP
693 * requests. Clear the incomplete ARP entry by removing the
694 * appropriate route, depending on the error code:
695 * EHOSTDOWN NFS server's route
696 * EHOSTUNREACH router's route
702 /* remove the server's arp entry */
703 error
= host_route_delete(S_netboot_info_p
->server_ip
,
706 printf("netboot: host_route_delete(" IP_FORMAT
708 IP_LIST(&S_netboot_info_p
->server_ip
), error
);
712 error
= host_route_delete(router
, ifp
->if_index
);
714 printf("netboot: host_route_delete(" IP_FORMAT
715 ") failed %d\n", IP_LIST(&router
), error
);
721 case kNetBootImageTypeHTTP
:
722 error
= netboot_setup();
744 if (S_netboot_info_p
== NULL
745 || S_netboot_info_p
->image_path
== NULL
) {
748 printf("netboot_setup: calling imageboot_mount_image\n");
749 error
= imageboot_mount_image(S_netboot_info_p
->image_path
, -1);
751 printf("netboot: failed to mount root image, %d\n", error
);
753 else if (S_netboot_info_p
->second_image_path
!= NULL
) {
754 error
= imageboot_mount_image(S_netboot_info_p
->second_image_path
, 0);
756 printf("netboot: failed to mount second root image, %d\n", error
);
761 netboot_info_free(&S_netboot_info_p
);