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
, ':');
171 * Function: find_colon
173 * Find the next unescaped instance of the colon character.
174 * If a colon is escaped (preceded by a backslash '\' character),
175 * shift the string over by one character to overwrite the backslash.
177 static __inline__
char *
178 find_colon(char * str
)
183 while ((colon
= strchr(start
, ':')) != NULL
) {
187 if (colon
== start
) {
190 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
);
249 (void)find_colon(start
);
252 *host
= inet_ntop(AF_INET
, iaddr_p
, tmp
, sizeof(tmp
));
257 parse_image_path(char * path
, struct in_addr
* iaddr_p
, char const * * host
,
258 char * * mount_dir
, char * * image_path
)
260 if (path
[0] >= '0' && path
[0] <= '9') {
261 return (parse_booter_path(path
, iaddr_p
, host
, mount_dir
,
264 return (parse_netboot_path(path
, iaddr_p
, host
, mount_dir
,
269 get_root_path(char * root_path
)
272 boolean_t found
= FALSE
;
276 entry
= IOBSDRegistryEntryForDeviceTree("/chosen");
280 pkt
= IOBSDRegistryEntryGetData(entry
, BSDP_RESPONSE
, &pkt_len
);
281 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
282 printf("netboot: retrieving root path from BSDP response\n");
285 pkt
= IOBSDRegistryEntryGetData(entry
, BOOTP_RESPONSE
,
287 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
288 printf("netboot: retrieving root path from BOOTP response\n");
295 const struct dhcp
* reply
;
297 reply
= (const struct dhcp
*)pkt
;
298 (void)dhcpol_parse_packet(&options
, reply
, pkt_len
);
300 path
= (const char *)dhcpol_find(&options
,
301 dhcptag_root_path_e
, &len
, NULL
);
303 memcpy(root_path
, path
, len
);
304 root_path
[len
] = '\0';
308 IOBSDRegistryEntryRelease(entry
);
314 save_path(char * * str_p
, int * length_p
, char * path
)
316 *length_p
= strlen(path
) + 1;
317 *str_p
= (char *)kalloc(*length_p
);
318 strlcpy(*str_p
, path
, *length_p
);
322 static struct netboot_info
*
323 netboot_info_init(struct in_addr iaddr
)
325 boolean_t have_root_path
= FALSE
;
326 struct netboot_info
* info
= NULL
;
327 char * root_path
= NULL
;
329 info
= (struct netboot_info
*)kalloc(sizeof(*info
));
330 bzero(info
, sizeof(*info
));
331 info
->client_ip
= iaddr
;
332 info
->image_type
= kNetBootImageTypeUnknown
;
334 /* check for a booter-specified path then a NetBoot path */
335 MALLOC_ZONE(root_path
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
336 if (root_path
== NULL
)
337 panic("netboot_info_init: M_NAMEI zone exhausted");
338 if (PE_parse_boot_argn("rp0", root_path
, MAXPATHLEN
) == TRUE
339 || PE_parse_boot_argn("rp", root_path
, MAXPATHLEN
) == TRUE
340 || PE_parse_boot_argn("rootpath", root_path
, MAXPATHLEN
) == TRUE
) {
341 if (imageboot_format_is_valid(root_path
)) {
342 printf("netboot_info_init: rp0='%s' isn't a network path,"
343 " ignoring\n", root_path
);
346 have_root_path
= TRUE
;
349 if (have_root_path
== FALSE
) {
350 have_root_path
= get_root_path(root_path
);
352 if (have_root_path
) {
353 const char * server_name
= NULL
;
354 char * mount_point
= NULL
;
355 char * image_path
= NULL
;
356 struct in_addr server_ip
;
358 if (parse_image_path(root_path
, &server_ip
, &server_name
,
359 &mount_point
, &image_path
)) {
360 info
->image_type
= kNetBootImageTypeNFS
;
361 info
->server_ip
= server_ip
;
362 info
->server_name_length
= strlen(server_name
) + 1;
363 info
->server_name
= (char *)kalloc(info
->server_name_length
);
364 info
->mount_point_length
= strlen(mount_point
) + 1;
365 info
->mount_point
= (char *)kalloc(info
->mount_point_length
);
366 strlcpy(info
->server_name
, server_name
, info
->server_name_length
);
367 strlcpy(info
->mount_point
, mount_point
, info
->mount_point_length
);
369 printf("netboot: NFS Server %s Mount %s",
370 server_name
, info
->mount_point
);
371 if (image_path
!= NULL
) {
372 boolean_t needs_slash
= FALSE
;
374 info
->image_path_length
= strlen(image_path
) + 1;
375 if (image_path
[0] != '/') {
377 info
->image_path_length
++;
379 info
->image_path
= (char *)kalloc(info
->image_path_length
);
381 info
->image_path
[0] = '/';
382 strlcpy(info
->image_path
+ 1, image_path
,
383 info
->image_path_length
- 1);
385 strlcpy(info
->image_path
, image_path
,
386 info
->image_path_length
);
388 printf(" Image %s", info
->image_path
);
392 else if (strncmp(root_path
, kNetBootRootPathPrefixHTTP
,
393 strlen(kNetBootRootPathPrefixHTTP
)) == 0) {
394 info
->image_type
= kNetBootImageTypeHTTP
;
395 save_path(&info
->image_path
, &info
->image_path_length
,
397 printf("netboot: HTTP URL %s\n", info
->image_path
);
400 printf("netboot: root path uses unrecognized format\n");
403 /* check for image-within-image */
404 if (info
->image_path
!= NULL
) {
405 if (PE_parse_boot_argn(IMAGEBOOT_ROOT_ARG
, root_path
, MAXPATHLEN
)
406 || PE_parse_boot_argn("rp1", root_path
, MAXPATHLEN
)) {
407 /* rp1/root-dmg is the second-level image */
408 save_path(&info
->second_image_path
, &info
->second_image_path_length
,
412 if (info
->second_image_path
!= NULL
) {
413 printf("netboot: nested image %s\n", info
->second_image_path
);
416 FREE_ZONE(root_path
, MAXPATHLEN
, M_NAMEI
);
421 netboot_info_free(struct netboot_info
* * info_p
)
423 struct netboot_info
* info
= *info_p
;
426 if (info
->mount_point
) {
427 kfree(info
->mount_point
, info
->mount_point_length
);
429 if (info
->server_name
) {
430 kfree(info
->server_name
, info
->server_name_length
);
432 if (info
->image_path
) {
433 kfree(info
->image_path
, info
->image_path_length
);
435 if (info
->second_image_path
) {
436 kfree(info
->second_image_path
, info
->second_image_path_length
);
438 kfree(info
, sizeof(*info
));
445 netboot_iaddr(struct in_addr
* iaddr_p
)
447 if (S_netboot_info_p
== NULL
)
450 *iaddr_p
= S_netboot_info_p
->client_ip
;
455 netboot_rootpath(struct in_addr
* server_ip
,
456 char * name
, int name_len
,
457 char * path
, int path_len
)
459 if (S_netboot_info_p
== NULL
)
465 if (S_netboot_info_p
->mount_point_length
== 0) {
468 if (path_len
< S_netboot_info_p
->mount_point_length
) {
469 printf("netboot: path too small %d < %d\n",
470 path_len
, S_netboot_info_p
->mount_point_length
);
473 strlcpy(path
, S_netboot_info_p
->mount_point
, path_len
);
474 strlcpy(name
, S_netboot_info_p
->server_name
, name_len
);
475 *server_ip
= S_netboot_info_p
->server_ip
;
481 get_ip_parameters(struct in_addr
* iaddr_p
, struct in_addr
* netmask_p
,
482 struct in_addr
* router_p
)
489 entry
= IOBSDRegistryEntryForDeviceTree("/chosen");
493 pkt
= IOBSDRegistryEntryGetData(entry
, DHCP_RESPONSE
, &pkt_len
);
494 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
495 printf("netboot: retrieving IP information from DHCP response\n");
498 pkt
= IOBSDRegistryEntryGetData(entry
, BOOTP_RESPONSE
, &pkt_len
);
499 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
500 printf("netboot: retrieving IP information from BOOTP response\n");
504 const struct in_addr
* ip
;
507 const struct dhcp
* reply
;
509 reply
= (const struct dhcp
*)pkt
;
510 (void)dhcpol_parse_packet(&options
, reply
, pkt_len
);
511 *iaddr_p
= reply
->dp_yiaddr
;
512 ip
= (const struct in_addr
*)
513 dhcpol_find(&options
,
514 dhcptag_subnet_mask_e
, &len
, NULL
);
518 ip
= (const struct in_addr
*)
519 dhcpol_find(&options
, dhcptag_router_e
, &len
, NULL
);
524 IOBSDRegistryEntryRelease(entry
);
525 return (pkt
!= NULL
);
529 route_cmd(int cmd
, struct in_addr d
, struct in_addr g
,
530 struct in_addr m
, uint32_t more_flags
, unsigned int ifscope
)
532 struct sockaddr_in dst
;
534 uint32_t flags
= RTF_UP
| RTF_STATIC
;
535 struct sockaddr_in gw
;
536 struct sockaddr_in mask
;
541 bzero((caddr_t
)&dst
, sizeof(dst
));
542 dst
.sin_len
= sizeof(dst
);
543 dst
.sin_family
= AF_INET
;
547 bzero((caddr_t
)&gw
, sizeof(gw
));
548 gw
.sin_len
= sizeof(gw
);
549 gw
.sin_family
= AF_INET
;
553 bzero(&mask
, sizeof(mask
));
554 mask
.sin_len
= sizeof(mask
);
555 mask
.sin_family
= AF_INET
;
558 error
= rtrequest_scoped(cmd
, (struct sockaddr
*)&dst
,
559 (struct sockaddr
*)&gw
, (struct sockaddr
*)&mask
, flags
, NULL
, ifscope
);
566 default_route_add(struct in_addr router
, boolean_t proxy_arp
)
569 struct in_addr zeroes
= { 0 };
571 if (proxy_arp
== FALSE
) {
572 flags
|= RTF_GATEWAY
;
574 return (route_cmd(RTM_ADD
, zeroes
, router
, zeroes
, flags
, IFSCOPE_NONE
));
578 host_route_delete(struct in_addr host
, unsigned int ifscope
)
580 struct in_addr zeroes
= { 0 };
582 return (route_cmd(RTM_DELETE
, host
, zeroes
, zeroes
, RTF_HOST
, ifscope
));
585 static struct ifnet
*
588 struct ifnet
* ifp
= NULL
;
592 ifp
= ifunit((char *)rootdevice
);
595 ifnet_head_lock_shared();
596 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
)
597 if ((ifp
->if_flags
& (IFF_LOOPBACK
|IFF_POINTOPOINT
)) == 0)
605 static const struct sockaddr_in blank_sin
= {
606 sizeof(struct sockaddr_in
),
610 { 0, 0, 0, 0, 0, 0, 0, 0 }
614 inet_aifaddr(struct socket
* so
, const char * name
,
615 const struct in_addr
* addr
,
616 const struct in_addr
* mask
,
617 const struct in_addr
* broadcast
)
619 struct ifaliasreq ifra
;
621 bzero(&ifra
, sizeof(ifra
));
622 strlcpy(ifra
.ifra_name
, name
, sizeof(ifra
.ifra_name
));
624 *((struct sockaddr_in
*)(void *)&ifra
.ifra_addr
) = blank_sin
;
625 ((struct sockaddr_in
*)(void *)&ifra
.ifra_addr
)->sin_addr
= *addr
;
628 *((struct sockaddr_in
*)(void *)&ifra
.ifra_mask
) = blank_sin
;
629 ((struct sockaddr_in
*)(void *)&ifra
.ifra_mask
)->sin_addr
= *mask
;
632 *((struct sockaddr_in
*)(void *)&ifra
.ifra_broadaddr
) = blank_sin
;
633 ((struct sockaddr_in
*)(void *)&ifra
.ifra_broadaddr
)->sin_addr
= *broadcast
;
635 return (ifioctl(so
, SIOCAIFADDR
, (caddr_t
)&ifra
, current_proc()));
640 netboot_mountroot(void)
643 struct in_addr iaddr
= { 0 };
646 struct in_addr netmask
= { 0 };
647 proc_t procp
= current_proc();
648 struct in_addr router
= { 0 };
649 struct socket
* so
= NULL
;
652 bzero(&ifr
, sizeof(ifr
));
654 /* find the interface */
655 ifp
= find_interface();
657 printf("netboot: no suitable interface\n");
661 snprintf(ifr
.ifr_name
, sizeof(ifr
.ifr_name
), "%s", if_name(ifp
));
662 printf("netboot: using network interface '%s'\n", ifr
.ifr_name
);
665 if ((error
= socreate(AF_INET
, &so
, SOCK_DGRAM
, 0)) != 0) {
666 printf("netboot: socreate, error=%d\n", error
);
669 ifr
.ifr_flags
= ifp
->if_flags
| IFF_UP
;
670 error
= ifioctl(so
, SIOCSIFFLAGS
, (caddr_t
)&ifr
, procp
);
672 printf("netboot: SIFFLAGS, error=%d\n", error
);
676 /* grab information from the registry */
677 if (get_ip_parameters(&iaddr
, &netmask
, &router
) == FALSE
) {
678 printf("netboot: can't retrieve IP parameters\n");
681 printf("netboot: IP address " IP_FORMAT
, IP_LIST(&iaddr
));
682 if (netmask
.s_addr
) {
683 printf(" netmask " IP_FORMAT
, IP_LIST(&netmask
));
686 printf(" router " IP_FORMAT
, IP_LIST(&router
));
689 error
= inet_aifaddr(so
, ifr
.ifr_name
, &iaddr
, &netmask
, NULL
);
691 printf("netboot: inet_aifaddr failed, %d\n", error
);
694 if (router
.s_addr
== 0) {
695 /* enable proxy arp if we don't have a router */
696 router
.s_addr
= iaddr
.s_addr
;
698 printf("netboot: adding default route " IP_FORMAT
"\n",
700 error
= default_route_add(router
, router
.s_addr
== iaddr
.s_addr
);
702 printf("netboot: default_route_add failed %d\n", error
);
707 S_netboot_info_p
= netboot_info_init(iaddr
);
708 switch (S_netboot_info_p
->image_type
) {
710 case kNetBootImageTypeNFS
:
711 for (try = 1; TRUE
; try++) {
712 error
= nfs_mountroot();
716 printf("netboot: nfs_mountroot() attempt %u failed; "
717 "clearing ARP entry and trying again\n", try);
719 * error is either EHOSTDOWN or EHOSTUNREACH, which likely means
720 * that the port we're plugged into has spanning tree enabled,
721 * and either the router or the server can't answer our ARP
722 * requests. Clear the incomplete ARP entry by removing the
723 * appropriate route, depending on the error code:
724 * EHOSTDOWN NFS server's route
725 * EHOSTUNREACH router's route
731 /* remove the server's arp entry */
732 error
= host_route_delete(S_netboot_info_p
->server_ip
,
735 printf("netboot: host_route_delete(" IP_FORMAT
737 IP_LIST(&S_netboot_info_p
->server_ip
), error
);
741 error
= host_route_delete(router
, ifp
->if_index
);
743 printf("netboot: host_route_delete(" IP_FORMAT
744 ") failed %d\n", IP_LIST(&router
), error
);
750 case kNetBootImageTypeHTTP
:
751 error
= netboot_setup();
773 if (S_netboot_info_p
== NULL
774 || S_netboot_info_p
->image_path
== NULL
) {
777 printf("netboot_setup: calling imageboot_mount_image\n");
778 error
= imageboot_mount_image(S_netboot_info_p
->image_path
, -1);
780 printf("netboot: failed to mount root image, %d\n", error
);
782 else if (S_netboot_info_p
->second_image_path
!= NULL
) {
783 error
= imageboot_mount_image(S_netboot_info_p
->second_image_path
, 0);
785 printf("netboot: failed to mount second root image, %d\n", error
);
790 netboot_info_free(&S_netboot_info_p
);