2 * Copyright (c) 2001-2006 Apple Computer, 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>
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 extern int vndevice_root_image(const char * path
, char devname
[],
86 extern int di_root_image(const char *path
, char devname
[], dev_t
*dev_p
);
88 #define BOOTP_RESPONSE "bootp-response"
89 #define BSDP_RESPONSE "bsdp-response"
90 #define DHCP_RESPONSE "dhcp-response"
92 /* forward declarations */
93 int inet_aton(char * cp
, struct in_addr
* pin
);
95 boolean_t
netboot_iaddr(struct in_addr
* iaddr_p
);
96 boolean_t
netboot_rootpath(struct in_addr
* server_ip
,
97 char * name
, int name_len
,
98 char * path
, int path_len
);
99 int netboot_setup(void);
100 int netboot_mountroot(void);
101 int netboot_root(void);
105 #define IP_FORMAT "%d.%d.%d.%d"
106 #define IP_CH(ip) ((u_char *)ip)
107 #define IP_LIST(ip) IP_CH(ip)[0],IP_CH(ip)[1],IP_CH(ip)[2],IP_CH(ip)[3]
109 #define kNetBootRootPathPrefixNFS "nfs:"
110 #define kNetBootRootPathPrefixHTTP "http:"
113 kNetBootImageTypeUnknown
= 0,
114 kNetBootImageTypeNFS
= 1,
115 kNetBootImageTypeHTTP
= 2,
118 struct netboot_info
{
119 struct in_addr client_ip
;
120 struct in_addr server_ip
;
122 int server_name_length
;
124 int mount_point_length
;
126 int image_path_length
;
127 NetBootImageType image_type
;
132 inet_aton(char * cp
, struct in_addr
* pin
)
134 u_char
* b
= (u_char
*)pin
;
138 for (p
= cp
, i
= 0; i
< 4; i
++) {
139 u_long l
= strtoul(p
, 0, 0);
144 if (i
< 3 && p
== NULL
)
152 * Function: parse_booter_path
154 * Parse a string of the form:
155 * "<IP>:<host>:<mount>[:<image_path>]"
156 * into the given ip address, host, mount point, and optionally, image_path.
159 * The passed in string is modified i.e. ':' is replaced by '\0'.
161 * "17.202.16.17:seaport:/release/.images/Image9/CurrentHera"
163 static __inline__ boolean_t
164 parse_booter_path(char * path
, struct in_addr
* iaddr_p
, char const * * host
,
165 char * * mount_dir
, char * * image_path
)
172 colon
= strchr(start
, ':');
177 if (inet_aton(start
, iaddr_p
) != 1) {
183 colon
= strchr(start
, ':');
192 colon
= strchr(start
, ':');
207 * Function: find_colon
209 * Find the next unescaped instance of the colon character.
210 * If a colon is escaped (preceded by a backslash '\' character),
211 * shift the string over by one character to overwrite the backslash.
213 static __inline__
char *
214 find_colon(char * str
)
219 while ((colon
= strchr(start
, ':')) != NULL
) {
223 if (colon
== start
) {
226 if (colon
[-1] != '\\')
228 for (dst
= colon
- 1, src
= colon
; *dst
!= '\0'; dst
++, src
++) {
237 * Function: parse_netboot_path
239 * Parse a string of the form:
240 * "nfs:<IP>:<mount>[:<image_path>]"
241 * into the given ip address, host, mount point, and optionally, image_path.
243 * - the passed in string is modified i.e. ':' is replaced by '\0'
244 * - literal colons must be escaped with a backslash
247 * nfs:17.202.42.112:/Library/NetBoot/NetBootSP0:Jaguar/Jaguar.dmg
248 * nfs:17.202.42.112:/Volumes/Foo\:/Library/NetBoot/NetBootSP0:Jaguar/Jaguar.dmg
250 static __inline__ boolean_t
251 parse_netboot_path(char * path
, struct in_addr
* iaddr_p
, char const * * host
,
252 char * * mount_dir
, char * * image_path
)
254 static char tmp
[MAX_IPv4_STR_LEN
]; /* Danger - not thread safe */
258 if (strncmp(path
, kNetBootRootPathPrefixNFS
,
259 strlen(kNetBootRootPathPrefixNFS
)) != 0) {
264 start
= path
+ strlen(kNetBootRootPathPrefixNFS
);
265 colon
= strchr(start
, ':');
270 if (inet_aton(start
, iaddr_p
) != 1) {
276 colon
= find_colon(start
);
285 (void)find_colon(start
);
288 *host
= inet_ntop(AF_INET
, iaddr_p
, tmp
, sizeof(tmp
));
293 parse_image_path(char * path
, struct in_addr
* iaddr_p
, char const * * host
,
294 char * * mount_dir
, char * * image_path
)
296 if (path
[0] >= '0' && path
[0] <= '9') {
297 return (parse_booter_path(path
, iaddr_p
, host
, mount_dir
,
300 return (parse_netboot_path(path
, iaddr_p
, host
, mount_dir
,
305 get_root_path(char * root_path
)
308 boolean_t found
= FALSE
;
312 entry
= IOBSDRegistryEntryForDeviceTree("/chosen");
316 pkt
= IOBSDRegistryEntryGetData(entry
, BSDP_RESPONSE
, &pkt_len
);
317 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
318 printf("netboot: retrieving root path from BSDP response\n");
321 pkt
= IOBSDRegistryEntryGetData(entry
, BOOTP_RESPONSE
,
323 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
324 printf("netboot: retrieving root path from BOOTP response\n");
331 const struct dhcp
* reply
;
333 reply
= (const struct dhcp
*)pkt
;
334 (void)dhcpol_parse_packet(&options
, reply
, pkt_len
);
336 path
= (const char *)dhcpol_find(&options
,
337 dhcptag_root_path_e
, &len
, NULL
);
339 memcpy(root_path
, path
, len
);
340 root_path
[len
] = '\0';
344 IOBSDRegistryEntryRelease(entry
);
349 static struct netboot_info
*
350 netboot_info_init(struct in_addr iaddr
)
352 struct netboot_info
* info
;
353 char * root_path
= NULL
;
354 boolean_t use_hdix
= TRUE
;
355 char * vndevice
= NULL
;
357 MALLOC_ZONE(vndevice
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
358 if (vndevice
== NULL
)
359 panic("netboot_info_init: M_NAMEI zone exhausted");
360 if (PE_parse_boot_argn("vndevice", vndevice
, MAXPATHLEN
) == TRUE
) {
363 FREE_ZONE(vndevice
, MAXPATHLEN
, M_NAMEI
);
365 info
= (struct netboot_info
*)kalloc(sizeof(*info
));
366 bzero(info
, sizeof(*info
));
367 info
->client_ip
= iaddr
;
368 info
->image_type
= kNetBootImageTypeUnknown
;
369 info
->use_hdix
= use_hdix
;
371 /* check for a booter-specified path then a NetBoot path */
372 MALLOC_ZONE(root_path
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
373 if (root_path
== NULL
)
374 panic("netboot_info_init: M_NAMEI zone exhausted");
375 if (PE_parse_boot_argn("rp", root_path
, MAXPATHLEN
) == TRUE
376 || PE_parse_boot_argn("rootpath", root_path
, MAXPATHLEN
) == TRUE
377 || get_root_path(root_path
) == TRUE
) {
378 const char * server_name
= NULL
;
379 char * mount_point
= NULL
;
380 char * image_path
= NULL
;
381 struct in_addr server_ip
;
383 if (parse_image_path(root_path
, &server_ip
, &server_name
,
384 &mount_point
, &image_path
)) {
385 info
->image_type
= kNetBootImageTypeNFS
;
386 info
->server_ip
= server_ip
;
387 info
->server_name_length
= strlen(server_name
) + 1;
388 info
->server_name
= (char *)kalloc(info
->server_name_length
);
389 info
->mount_point_length
= strlen(mount_point
) + 1;
390 info
->mount_point
= (char *)kalloc(info
->mount_point_length
);
391 strlcpy(info
->server_name
, server_name
, info
->server_name_length
);
392 strlcpy(info
->mount_point
, mount_point
, info
->mount_point_length
);
394 printf("Server %s Mount %s",
395 server_name
, info
->mount_point
);
396 if (image_path
!= NULL
) {
397 boolean_t needs_slash
= FALSE
;
399 info
->image_path_length
= strlen(image_path
) + 1;
400 if (image_path
[0] != '/') {
402 info
->image_path_length
++;
404 info
->image_path
= (char *)kalloc(info
->image_path_length
);
406 info
->image_path
[0] = '/';
407 strlcpy(info
->image_path
+ 1, image_path
,
408 info
->image_path_length
- 1);
410 strlcpy(info
->image_path
, image_path
,
411 info
->image_path_length
);
413 printf(" Image %s", info
->image_path
);
417 else if (strncmp(root_path
, kNetBootRootPathPrefixHTTP
,
418 strlen(kNetBootRootPathPrefixHTTP
)) == 0) {
419 /* only HDIX supports HTTP */
420 info
->image_type
= kNetBootImageTypeHTTP
;
421 info
->use_hdix
= TRUE
;
422 info
->image_path_length
= strlen(root_path
) + 1;
423 info
->image_path
= (char *)kalloc(info
->image_path_length
);
424 strlcpy(info
->image_path
, root_path
, info
->image_path_length
);
427 printf("netboot: root path uses unrecognized format\n");
430 FREE_ZONE(root_path
, MAXPATHLEN
, M_NAMEI
);
435 netboot_info_free(struct netboot_info
* * info_p
)
437 struct netboot_info
* info
= *info_p
;
440 if (info
->mount_point
) {
441 kfree(info
->mount_point
, info
->mount_point_length
);
443 if (info
->server_name
) {
444 kfree(info
->server_name
, info
->server_name_length
);
446 if (info
->image_path
) {
447 kfree(info
->image_path
, info
->image_path_length
);
449 kfree(info
, sizeof(*info
));
456 netboot_iaddr(struct in_addr
* iaddr_p
)
458 if (S_netboot_info_p
== NULL
)
461 *iaddr_p
= S_netboot_info_p
->client_ip
;
466 netboot_rootpath(struct in_addr
* server_ip
,
467 char * name
, int name_len
,
468 char * path
, int path_len
)
470 if (S_netboot_info_p
== NULL
)
476 if (S_netboot_info_p
->mount_point_length
== 0) {
479 if (path_len
< S_netboot_info_p
->mount_point_length
) {
480 printf("netboot: path too small %d < %d\n",
481 path_len
, S_netboot_info_p
->mount_point_length
);
484 strlcpy(path
, S_netboot_info_p
->mount_point
, path_len
);
485 strlcpy(name
, S_netboot_info_p
->server_name
, name_len
);
486 *server_ip
= S_netboot_info_p
->server_ip
;
492 get_ip_parameters(struct in_addr
* iaddr_p
, struct in_addr
* netmask_p
,
493 struct in_addr
* router_p
)
500 entry
= IOBSDRegistryEntryForDeviceTree("/chosen");
504 pkt
= IOBSDRegistryEntryGetData(entry
, DHCP_RESPONSE
, &pkt_len
);
505 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
506 printf("netboot: retrieving IP information from DHCP response\n");
509 pkt
= IOBSDRegistryEntryGetData(entry
, BOOTP_RESPONSE
, &pkt_len
);
510 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
511 printf("netboot: retrieving IP information from BOOTP response\n");
515 const struct in_addr
* ip
;
518 const struct dhcp
* reply
;
520 reply
= (const struct dhcp
*)pkt
;
521 (void)dhcpol_parse_packet(&options
, reply
, pkt_len
);
522 *iaddr_p
= reply
->dp_yiaddr
;
523 ip
= (const struct in_addr
*)
524 dhcpol_find(&options
,
525 dhcptag_subnet_mask_e
, &len
, NULL
);
529 ip
= (const struct in_addr
*)
530 dhcpol_find(&options
, dhcptag_router_e
, &len
, NULL
);
535 IOBSDRegistryEntryRelease(entry
);
536 return (pkt
!= NULL
);
540 route_cmd(int cmd
, struct in_addr d
, struct in_addr g
,
541 struct in_addr m
, u_long more_flags
)
543 struct sockaddr_in dst
;
544 u_long flags
= RTF_UP
| RTF_STATIC
;
545 struct sockaddr_in gw
;
546 struct sockaddr_in mask
;
551 bzero((caddr_t
)&dst
, sizeof(dst
));
552 dst
.sin_len
= sizeof(dst
);
553 dst
.sin_family
= AF_INET
;
557 bzero((caddr_t
)&gw
, sizeof(gw
));
558 gw
.sin_len
= sizeof(gw
);
559 gw
.sin_family
= AF_INET
;
563 bzero(&mask
, sizeof(mask
));
564 mask
.sin_len
= sizeof(mask
);
565 mask
.sin_family
= AF_INET
;
568 return (rtrequest(cmd
, (struct sockaddr
*)&dst
, (struct sockaddr
*)&gw
,
569 (struct sockaddr
*)&mask
, flags
, NULL
));
573 default_route_add(struct in_addr router
, boolean_t proxy_arp
)
576 struct in_addr zeroes
= { 0 };
578 if (proxy_arp
== FALSE
) {
579 flags
|= RTF_GATEWAY
;
581 return (route_cmd(RTM_ADD
, zeroes
, router
, zeroes
, flags
));
585 host_route_delete(struct in_addr host
)
587 struct in_addr zeroes
= { 0 };
589 return (route_cmd(RTM_DELETE
, host
, zeroes
, zeroes
, RTF_HOST
));
592 static struct ifnet
*
595 struct ifnet
* ifp
= NULL
;
598 ifp
= ifunit((char *)rootdevice
);
601 ifnet_head_lock_shared();
602 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
)
603 if ((ifp
->if_flags
& (IFF_LOOPBACK
|IFF_POINTOPOINT
)) == 0)
611 netboot_mountroot(void)
614 struct in_addr iaddr
= { 0 };
617 struct in_addr netmask
= { 0 };
618 proc_t procp
= current_proc();
619 struct in_addr router
= { 0 };
620 struct socket
* so
= NULL
;
623 bzero(&ifr
, sizeof(ifr
));
625 /* find the interface */
626 ifp
= find_interface();
628 printf("netboot: no suitable interface\n");
632 snprintf(ifr
.ifr_name
, sizeof(ifr
.ifr_name
), "%s%d", ifp
->if_name
,
634 printf("netboot: using network interface '%s'\n", ifr
.ifr_name
);
637 if ((error
= socreate(AF_INET
, &so
, SOCK_DGRAM
, 0)) != 0) {
638 printf("netboot: socreate, error=%d\n", error
);
641 ifr
.ifr_flags
= ifp
->if_flags
| IFF_UP
;
642 error
= ifioctl(so
, SIOCSIFFLAGS
, (caddr_t
)&ifr
, procp
);
644 printf("netboot: SIFFLAGS, error=%d\n", error
);
648 /* grab information from the registry */
649 if (get_ip_parameters(&iaddr
, &netmask
, &router
) == FALSE
) {
650 /* use DHCP to retrieve IP address, netmask and router */
651 error
= dhcp(ifp
, &iaddr
, 64, &netmask
, &router
, procp
);
653 printf("netboot: DHCP failed %d\n", error
);
657 printf("netboot: IP address " IP_FORMAT
, IP_LIST(&iaddr
));
658 if (netmask
.s_addr
) {
659 printf(" netmask " IP_FORMAT
, IP_LIST(&netmask
));
662 printf(" router " IP_FORMAT
, IP_LIST(&router
));
665 error
= inet_aifaddr(so
, ifr
.ifr_name
, &iaddr
, &netmask
, NULL
);
667 printf("netboot: inet_aifaddr failed, %d\n", error
);
670 if (router
.s_addr
== 0) {
671 /* enable proxy arp if we don't have a router */
672 router
.s_addr
= iaddr
.s_addr
;
674 printf("netboot: adding default route " IP_FORMAT
"\n",
676 error
= default_route_add(router
, router
.s_addr
== iaddr
.s_addr
);
678 printf("netboot: default_route_add failed %d\n", error
);
683 S_netboot_info_p
= netboot_info_init(iaddr
);
684 switch (S_netboot_info_p
->image_type
) {
686 case kNetBootImageTypeNFS
:
687 for (try = 1; TRUE
; try++) {
688 error
= nfs_mountroot();
692 printf("netboot: nfs_mountroot() attempt %u failed; "
693 "clearing ARP entry and trying again\n", try);
695 * error is either EHOSTDOWN or EHOSTUNREACH, which likely means
696 * that the port we're plugged into has spanning tree enabled,
697 * and either the router or the server can't answer our ARP
698 * requests. Clear the incomplete ARP entry by removing the
699 * appropriate route, depending on the error code:
700 * EHOSTDOWN NFS server's route
701 * EHOSTUNREACH router's route
707 /* remove the server's arp entry */
708 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
);
718 printf("netboot: host_route_delete(" IP_FORMAT
719 ") failed %d\n", IP_LIST(&router
), error
);
725 case kNetBootImageTypeHTTP
:
726 error
= netboot_setup();
749 if (S_netboot_info_p
== NULL
750 || S_netboot_info_p
->image_path
== NULL
) {
753 if (S_netboot_info_p
->use_hdix
) {
754 printf("netboot_setup: calling di_root_image\n");
755 error
= di_root_image(S_netboot_info_p
->image_path
,
756 (char *)rootdevice
, &dev
);
758 printf("netboot_setup: di_root_image: failed %d\n", error
);
763 printf("netboot_setup: calling vndevice_root_image\n");
764 error
= vndevice_root_image(S_netboot_info_p
->image_path
,
765 (char *)rootdevice
, &dev
);
767 printf("netboot_setup: vndevice_root_image: failed %d\n", error
);
773 printf("netboot: root device 0x%x\n", rootdev
);
774 error
= vfs_mountroot();
775 if (error
== 0 && rootvnode
!= NULL
) {
779 /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */
780 if (VFS_ROOT(TAILQ_LAST(&mountlist
,mntlist
), &newdp
, vfs_context_kernel()))
781 panic("netboot_setup: cannot find root vnode");
786 filedesc0
.fd_cdir
= newdp
;
789 TAILQ_REMOVE(&mountlist
, TAILQ_FIRST(&mountlist
), mnt_list
);
791 mountlist
.tqh_first
->mnt_flag
|= MNT_ROOTFS
;
794 netboot_info_free(&S_netboot_info_p
);