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>
57 #include <kern/kern_types.h>
58 #include <kern/kalloc.h>
59 #include <sys/netboot.h>
60 #include <sys/imageboot.h>
61 #include <pexpert/pexpert.h>
63 //#include <libkern/libkern.h>
64 extern struct filedesc filedesc0
;
66 extern int nfs_mountroot(void); /* nfs_vfsops.c */
67 extern int (*mountroot
)(void);
69 extern unsigned char rootdevice
[];
71 static int S_netboot
= 0;
72 static struct netboot_info
* S_netboot_info_p
;
75 IOBSDRegistryEntryForDeviceTree(const char * path
);
78 IOBSDRegistryEntryRelease(void * entry
);
81 IOBSDRegistryEntryGetData(void * entry
, const char * property_name
,
84 #define BOOTP_RESPONSE "bootp-response"
85 #define BSDP_RESPONSE "bsdp-response"
86 #define DHCP_RESPONSE "dhcp-response"
88 #define IP_FORMAT "%d.%d.%d.%d"
89 #define IP_CH(ip) ((u_char *)ip)
90 #define IP_LIST(ip) IP_CH(ip)[0],IP_CH(ip)[1],IP_CH(ip)[2],IP_CH(ip)[3]
92 #define kNetBootRootPathPrefixNFS "nfs:"
93 #define kNetBootRootPathPrefixHTTP "http:"
96 kNetBootImageTypeUnknown
= 0,
97 kNetBootImageTypeNFS
= 1,
98 kNetBootImageTypeHTTP
= 2,
101 struct netboot_info
{
102 struct in_addr client_ip
;
103 struct in_addr server_ip
;
105 int server_name_length
;
107 int mount_point_length
;
109 int image_path_length
;
110 NetBootImageType image_type
;
111 char * second_image_path
;
112 int second_image_path_length
;
116 * Function: parse_booter_path
118 * Parse a string of the form:
119 * "<IP>:<host>:<mount>[:<image_path>]"
120 * into the given ip address, host, mount point, and optionally, image_path.
123 * The passed in string is modified i.e. ':' is replaced by '\0'.
125 * "17.202.16.17:seaport:/release/.images/Image9/CurrentHera"
127 static __inline__ boolean_t
128 parse_booter_path(char * path
, struct in_addr
* iaddr_p
, char const * * host
,
129 char * * mount_dir
, char * * image_path
)
136 colon
= strchr(start
, ':');
141 if (inet_aton(start
, iaddr_p
) != 1) {
147 colon
= strchr(start
, ':');
156 colon
= strchr(start
, ':');
170 * Function: find_colon
172 * Find the next unescaped instance of the colon character.
173 * If a colon is escaped (preceded by a backslash '\' character),
174 * shift the string over by one character to overwrite the backslash.
176 static __inline__
char *
177 find_colon(char * str
)
182 while ((colon
= strchr(start
, ':')) != NULL
) {
186 if (colon
== start
) {
189 if (colon
[-1] != '\\') {
192 for (dst
= colon
- 1, src
= colon
; *dst
!= '\0'; dst
++, src
++) {
201 * Function: parse_netboot_path
203 * Parse a string of the form:
204 * "nfs:<IP>:<mount>[:<image_path>]"
205 * into the given ip address, host, mount point, and optionally, image_path.
207 * - the passed in string is modified i.e. ':' is replaced by '\0'
208 * - literal colons must be escaped with a backslash
211 * nfs:17.202.42.112:/Library/NetBoot/NetBootSP0:Jaguar/Jaguar.dmg
212 * nfs:17.202.42.112:/Volumes/Foo\:/Library/NetBoot/NetBootSP0:Jaguar/Jaguar.dmg
214 static __inline__ boolean_t
215 parse_netboot_path(char * path
, struct in_addr
* iaddr_p
, char const * * host
,
216 char * * mount_dir
, char * * image_path
)
218 static char tmp
[MAX_IPv4_STR_LEN
]; /* Danger - not thread safe */
222 if (strncmp(path
, kNetBootRootPathPrefixNFS
,
223 strlen(kNetBootRootPathPrefixNFS
)) != 0) {
228 start
= path
+ strlen(kNetBootRootPathPrefixNFS
);
229 colon
= strchr(start
, ':');
234 if (inet_aton(start
, iaddr_p
) != 1) {
240 colon
= find_colon(start
);
248 (void)find_colon(start
);
251 *host
= inet_ntop(AF_INET
, iaddr_p
, tmp
, sizeof(tmp
));
256 parse_image_path(char * path
, struct in_addr
* iaddr_p
, char const * * host
,
257 char * * mount_dir
, char * * image_path
)
259 if (path
[0] >= '0' && path
[0] <= '9') {
260 return parse_booter_path(path
, iaddr_p
, host
, mount_dir
,
263 return parse_netboot_path(path
, iaddr_p
, host
, mount_dir
,
268 get_root_path(char * root_path
)
271 boolean_t found
= FALSE
;
275 entry
= IOBSDRegistryEntryForDeviceTree("/chosen");
279 pkt
= IOBSDRegistryEntryGetData(entry
, BSDP_RESPONSE
, &pkt_len
);
280 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
281 printf("netboot: retrieving root path from BSDP response\n");
283 pkt
= IOBSDRegistryEntryGetData(entry
, BOOTP_RESPONSE
,
285 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
286 printf("netboot: retrieving root path from BOOTP response\n");
293 const struct dhcp
* reply
;
295 reply
= (const struct dhcp
*)pkt
;
296 (void)dhcpol_parse_packet(&options
, reply
, pkt_len
);
298 path
= (const char *)dhcpol_find(&options
,
299 dhcptag_root_path_e
, &len
, NULL
);
301 memcpy(root_path
, path
, len
);
302 root_path
[len
] = '\0';
306 IOBSDRegistryEntryRelease(entry
);
311 save_path(char * * str_p
, int * length_p
, char * path
)
313 *length_p
= strlen(path
) + 1;
314 *str_p
= (char *)kalloc(*length_p
);
315 strlcpy(*str_p
, path
, *length_p
);
319 static struct netboot_info
*
320 netboot_info_init(struct in_addr iaddr
)
322 boolean_t have_root_path
= FALSE
;
323 struct netboot_info
* info
= NULL
;
324 char * root_path
= NULL
;
326 info
= (struct netboot_info
*)kalloc(sizeof(*info
));
327 bzero(info
, sizeof(*info
));
328 info
->client_ip
= iaddr
;
329 info
->image_type
= kNetBootImageTypeUnknown
;
331 /* check for a booter-specified path then a NetBoot path */
332 MALLOC_ZONE(root_path
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
333 if (root_path
== NULL
) {
334 panic("netboot_info_init: M_NAMEI zone exhausted");
336 if (PE_parse_boot_argn("rp0", root_path
, MAXPATHLEN
) == TRUE
337 || PE_parse_boot_argn("rp", root_path
, MAXPATHLEN
) == TRUE
338 || PE_parse_boot_argn("rootpath", root_path
, MAXPATHLEN
) == TRUE
) {
339 if (imageboot_format_is_valid(root_path
)) {
340 printf("netboot_info_init: rp0='%s' isn't a network path,"
341 " ignoring\n", root_path
);
343 have_root_path
= TRUE
;
346 if (have_root_path
== FALSE
) {
347 have_root_path
= get_root_path(root_path
);
349 if (have_root_path
) {
350 const char * server_name
= NULL
;
351 char * mount_point
= NULL
;
352 char * image_path
= NULL
;
353 struct in_addr server_ip
;
355 if (parse_image_path(root_path
, &server_ip
, &server_name
,
356 &mount_point
, &image_path
)) {
357 info
->image_type
= kNetBootImageTypeNFS
;
358 info
->server_ip
= server_ip
;
359 info
->server_name_length
= strlen(server_name
) + 1;
360 info
->server_name
= (char *)kalloc(info
->server_name_length
);
361 info
->mount_point_length
= strlen(mount_point
) + 1;
362 info
->mount_point
= (char *)kalloc(info
->mount_point_length
);
363 strlcpy(info
->server_name
, server_name
, info
->server_name_length
);
364 strlcpy(info
->mount_point
, mount_point
, info
->mount_point_length
);
366 printf("netboot: NFS Server %s Mount %s",
367 server_name
, info
->mount_point
);
368 if (image_path
!= NULL
) {
369 boolean_t needs_slash
= FALSE
;
371 info
->image_path_length
= strlen(image_path
) + 1;
372 if (image_path
[0] != '/') {
374 info
->image_path_length
++;
376 info
->image_path
= (char *)kalloc(info
->image_path_length
);
378 info
->image_path
[0] = '/';
379 strlcpy(info
->image_path
+ 1, image_path
,
380 info
->image_path_length
- 1);
382 strlcpy(info
->image_path
, image_path
,
383 info
->image_path_length
);
385 printf(" Image %s", info
->image_path
);
388 } else if (strncmp(root_path
, kNetBootRootPathPrefixHTTP
,
389 strlen(kNetBootRootPathPrefixHTTP
)) == 0) {
390 info
->image_type
= kNetBootImageTypeHTTP
;
391 save_path(&info
->image_path
, &info
->image_path_length
,
393 printf("netboot: HTTP URL %s\n", info
->image_path
);
395 printf("netboot: root path uses unrecognized format\n");
398 /* check for image-within-image */
399 if (info
->image_path
!= NULL
) {
400 if (PE_parse_boot_argn(IMAGEBOOT_ROOT_ARG
, root_path
, MAXPATHLEN
)
401 || PE_parse_boot_argn("rp1", root_path
, MAXPATHLEN
)) {
402 /* rp1/root-dmg is the second-level image */
403 save_path(&info
->second_image_path
, &info
->second_image_path_length
,
407 if (info
->second_image_path
!= NULL
) {
408 printf("netboot: nested image %s\n", info
->second_image_path
);
411 FREE_ZONE(root_path
, MAXPATHLEN
, M_NAMEI
);
416 netboot_info_free(struct netboot_info
* * info_p
)
418 struct netboot_info
* info
= *info_p
;
421 if (info
->mount_point
) {
422 kfree(info
->mount_point
, info
->mount_point_length
);
424 if (info
->server_name
) {
425 kfree(info
->server_name
, info
->server_name_length
);
427 if (info
->image_path
) {
428 kfree(info
->image_path
, info
->image_path_length
);
430 if (info
->second_image_path
) {
431 kfree(info
->second_image_path
, info
->second_image_path_length
);
433 kfree(info
, sizeof(*info
));
440 netboot_iaddr(struct in_addr
* iaddr_p
)
442 if (S_netboot_info_p
== NULL
) {
446 *iaddr_p
= S_netboot_info_p
->client_ip
;
451 netboot_rootpath(struct in_addr
* server_ip
,
452 char * name
, int name_len
,
453 char * path
, int path_len
)
455 if (S_netboot_info_p
== NULL
) {
462 if (S_netboot_info_p
->mount_point_length
== 0) {
465 if (path_len
< S_netboot_info_p
->mount_point_length
) {
466 printf("netboot: path too small %d < %d\n",
467 path_len
, S_netboot_info_p
->mount_point_length
);
470 strlcpy(path
, S_netboot_info_p
->mount_point
, path_len
);
471 strlcpy(name
, S_netboot_info_p
->server_name
, name_len
);
472 *server_ip
= S_netboot_info_p
->server_ip
;
478 get_ip_parameters(struct in_addr
* iaddr_p
, struct in_addr
* netmask_p
,
479 struct in_addr
* router_p
)
486 entry
= IOBSDRegistryEntryForDeviceTree("/chosen");
490 pkt
= IOBSDRegistryEntryGetData(entry
, DHCP_RESPONSE
, &pkt_len
);
491 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
492 printf("netboot: retrieving IP information from DHCP response\n");
494 pkt
= IOBSDRegistryEntryGetData(entry
, BOOTP_RESPONSE
, &pkt_len
);
495 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
496 printf("netboot: retrieving IP information from BOOTP response\n");
500 const struct in_addr
* ip
;
503 const struct dhcp
* reply
;
505 reply
= (const struct dhcp
*)pkt
;
506 (void)dhcpol_parse_packet(&options
, reply
, pkt_len
);
507 *iaddr_p
= reply
->dp_yiaddr
;
508 ip
= (const struct in_addr
*)
509 dhcpol_find(&options
,
510 dhcptag_subnet_mask_e
, &len
, NULL
);
514 ip
= (const struct in_addr
*)
515 dhcpol_find(&options
, dhcptag_router_e
, &len
, NULL
);
520 IOBSDRegistryEntryRelease(entry
);
525 route_cmd(int cmd
, struct in_addr d
, struct in_addr g
,
526 struct in_addr m
, uint32_t more_flags
, unsigned int ifscope
)
528 struct sockaddr_in dst
;
530 uint32_t flags
= RTF_UP
| RTF_STATIC
;
531 struct sockaddr_in gw
;
532 struct sockaddr_in mask
;
537 bzero((caddr_t
)&dst
, sizeof(dst
));
538 dst
.sin_len
= sizeof(dst
);
539 dst
.sin_family
= AF_INET
;
543 bzero((caddr_t
)&gw
, sizeof(gw
));
544 gw
.sin_len
= sizeof(gw
);
545 gw
.sin_family
= AF_INET
;
549 bzero(&mask
, sizeof(mask
));
550 mask
.sin_len
= sizeof(mask
);
551 mask
.sin_family
= AF_INET
;
554 error
= rtrequest_scoped(cmd
, (struct sockaddr
*)&dst
,
555 (struct sockaddr
*)&gw
, (struct sockaddr
*)&mask
, flags
, NULL
, ifscope
);
561 default_route_add(struct in_addr router
, boolean_t proxy_arp
)
564 struct in_addr zeroes
= { 0 };
566 if (proxy_arp
== FALSE
) {
567 flags
|= RTF_GATEWAY
;
569 return route_cmd(RTM_ADD
, zeroes
, router
, zeroes
, flags
, IFSCOPE_NONE
);
573 host_route_delete(struct in_addr host
, unsigned int ifscope
)
575 struct in_addr zeroes
= { 0 };
577 return route_cmd(RTM_DELETE
, host
, zeroes
, zeroes
, RTF_HOST
, ifscope
);
580 static struct ifnet
*
583 struct ifnet
* ifp
= NULL
;
587 ifp
= ifunit((char *)rootdevice
);
590 ifnet_head_lock_shared();
591 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
)
592 if ((ifp
->if_flags
& (IFF_LOOPBACK
| IFF_POINTOPOINT
)) == 0) {
601 static const struct sockaddr_in blank_sin
= {
602 sizeof(struct sockaddr_in
),
606 { 0, 0, 0, 0, 0, 0, 0, 0 }
610 inet_aifaddr(struct socket
* so
, const char * name
,
611 const struct in_addr
* addr
,
612 const struct in_addr
* mask
,
613 const struct in_addr
* broadcast
)
615 struct ifaliasreq ifra
;
617 bzero(&ifra
, sizeof(ifra
));
618 strlcpy(ifra
.ifra_name
, name
, sizeof(ifra
.ifra_name
));
620 *((struct sockaddr_in
*)(void *)&ifra
.ifra_addr
) = blank_sin
;
621 ((struct sockaddr_in
*)(void *)&ifra
.ifra_addr
)->sin_addr
= *addr
;
624 *((struct sockaddr_in
*)(void *)&ifra
.ifra_mask
) = blank_sin
;
625 ((struct sockaddr_in
*)(void *)&ifra
.ifra_mask
)->sin_addr
= *mask
;
628 *((struct sockaddr_in
*)(void *)&ifra
.ifra_broadaddr
) = blank_sin
;
629 ((struct sockaddr_in
*)(void *)&ifra
.ifra_broadaddr
)->sin_addr
= *broadcast
;
631 return ifioctl(so
, SIOCAIFADDR
, (caddr_t
)&ifra
, current_proc());
636 netboot_mountroot(void)
639 struct in_addr iaddr
= { 0 };
642 struct in_addr netmask
= { 0 };
643 proc_t procp
= current_proc();
644 struct in_addr router
= { 0 };
645 struct socket
* so
= NULL
;
648 bzero(&ifr
, sizeof(ifr
));
650 /* find the interface */
651 ifp
= find_interface();
653 printf("netboot: no suitable interface\n");
657 snprintf(ifr
.ifr_name
, sizeof(ifr
.ifr_name
), "%s", if_name(ifp
));
658 printf("netboot: using network interface '%s'\n", ifr
.ifr_name
);
661 if ((error
= socreate(AF_INET
, &so
, SOCK_DGRAM
, 0)) != 0) {
662 printf("netboot: socreate, error=%d\n", error
);
665 ifr
.ifr_flags
= ifp
->if_flags
| IFF_UP
;
666 error
= ifioctl(so
, SIOCSIFFLAGS
, (caddr_t
)&ifr
, procp
);
668 printf("netboot: SIFFLAGS, error=%d\n", error
);
672 /* grab information from the registry */
673 if (get_ip_parameters(&iaddr
, &netmask
, &router
) == FALSE
) {
674 printf("netboot: can't retrieve IP parameters\n");
677 printf("netboot: IP address " IP_FORMAT
, IP_LIST(&iaddr
));
678 if (netmask
.s_addr
) {
679 printf(" netmask " IP_FORMAT
, IP_LIST(&netmask
));
682 printf(" router " IP_FORMAT
, IP_LIST(&router
));
685 error
= inet_aifaddr(so
, ifr
.ifr_name
, &iaddr
, &netmask
, NULL
);
687 printf("netboot: inet_aifaddr failed, %d\n", error
);
690 if (router
.s_addr
== 0) {
691 /* enable proxy arp if we don't have a router */
692 router
.s_addr
= iaddr
.s_addr
;
694 printf("netboot: adding default route " IP_FORMAT
"\n",
696 error
= default_route_add(router
, router
.s_addr
== iaddr
.s_addr
);
698 printf("netboot: default_route_add failed %d\n", error
);
703 S_netboot_info_p
= netboot_info_init(iaddr
);
704 switch (S_netboot_info_p
->image_type
) {
706 case kNetBootImageTypeNFS
:
707 for (try = 1; TRUE
; try++) {
708 error
= nfs_mountroot();
712 printf("netboot: nfs_mountroot() attempt %u failed; "
713 "clearing ARP entry and trying again\n", try);
715 * error is either EHOSTDOWN or EHOSTUNREACH, which likely means
716 * that the port we're plugged into has spanning tree enabled,
717 * and either the router or the server can't answer our ARP
718 * requests. Clear the incomplete ARP entry by removing the
719 * appropriate route, depending on the error code:
720 * EHOSTDOWN NFS server's route
721 * EHOSTUNREACH router's route
727 /* remove the server's arp entry */
728 error
= host_route_delete(S_netboot_info_p
->server_ip
,
731 printf("netboot: host_route_delete(" IP_FORMAT
733 IP_LIST(&S_netboot_info_p
->server_ip
), error
);
737 error
= host_route_delete(router
, ifp
->if_index
);
739 printf("netboot: host_route_delete(" IP_FORMAT
740 ") failed %d\n", IP_LIST(&router
), error
);
746 case kNetBootImageTypeHTTP
:
747 error
= netboot_setup();
768 if (S_netboot_info_p
== NULL
769 || S_netboot_info_p
->image_path
== NULL
) {
772 printf("netboot_setup: calling imageboot_mount_image\n");
773 error
= imageboot_mount_image(S_netboot_info_p
->image_path
, -1);
775 printf("netboot: failed to mount root image, %d\n", error
);
776 } else if (S_netboot_info_p
->second_image_path
!= NULL
) {
777 error
= imageboot_mount_image(S_netboot_info_p
->second_image_path
, 0);
779 printf("netboot: failed to mount second root image, %d\n", error
);
784 netboot_info_free(&S_netboot_info_p
);