2 * Copyright (c) 2001-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 * 14 December, 2001 Dieter Siegmund (dieter@apple.com)
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
33 #include <sys/ioctl.h>
34 #include <sys/proc_internal.h>
35 #include <sys/mount_internal.h>
37 #include <sys/filedesc.h>
38 #include <sys/vnode_internal.h>
39 #include <sys/malloc.h>
40 #include <sys/socket.h>
41 #include <sys/socketvar.h>
42 #include <sys/reboot.h>
44 #include <net/if_dl.h>
45 #include <net/if_types.h>
46 #include <net/route.h>
47 #include <netinet/in.h>
48 #include <netinet/if_ether.h>
49 #include <netinet/dhcp_options.h>
50 #include <pexpert/pexpert.h>
52 #include <kern/kern_types.h>
53 #include <kern/kalloc.h>
55 //#include <libkern/libkern.h>
56 extern struct filedesc filedesc0
;
58 extern int strncmp(const char *,const char *, size_t);
59 extern unsigned long strtoul(const char *, char **, int);
60 extern char * strchr(const char *str
, int ch
);
62 extern int nfs_mountroot(void); /* nfs_vfsops.c */
63 extern int (*mountroot
)(void);
65 extern unsigned char rootdevice
[];
67 static int S_netboot
= 0;
68 static struct netboot_info
* S_netboot_info_p
;
71 IOBSDRegistryEntryForDeviceTree(const char * path
);
74 IOBSDRegistryEntryRelease(void * entry
);
77 IOBSDRegistryEntryGetData(void * entry
, const char * property_name
,
80 extern int vndevice_root_image(const char * path
, char devname
[],
82 extern int di_root_image(const char *path
, char devname
[], dev_t
*dev_p
);
84 #define BOOTP_RESPONSE "bootp-response"
85 #define BSDP_RESPONSE "bsdp-response"
86 #define DHCP_RESPONSE "dhcp-response"
89 bootp(struct ifnet
* ifp
, struct in_addr
* iaddr_p
, int max_retry
,
90 struct in_addr
* netmask_p
, struct in_addr
* router_p
,
94 /* forward declarations */
95 int inet_aton(char * cp
, struct in_addr
* pin
);
97 boolean_t
netboot_iaddr(struct in_addr
* iaddr_p
);
98 boolean_t
netboot_rootpath(struct in_addr
* server_ip
,
99 char * name
, int name_len
,
100 char * path
, int path_len
);
101 int netboot_setup(struct proc
* p
);
102 int netboot_mountroot(void);
103 int netboot_root(void);
107 #define IP_FORMAT "%d.%d.%d.%d"
108 #define IP_CH(ip) ((u_char *)ip)
109 #define IP_LIST(ip) IP_CH(ip)[0],IP_CH(ip)[1],IP_CH(ip)[2],IP_CH(ip)[3]
111 #define kNetBootRootPathPrefixNFS "nfs:"
112 #define kNetBootRootPathPrefixHTTP "http:"
115 kNetBootImageTypeUnknown
= 0,
116 kNetBootImageTypeNFS
= 1,
117 kNetBootImageTypeHTTP
= 2,
120 struct netboot_info
{
121 struct in_addr client_ip
;
122 struct in_addr server_ip
;
124 int server_name_length
;
126 int mount_point_length
;
128 int image_path_length
;
129 NetBootImageType image_type
;
134 inet_aton(char * cp
, struct in_addr
* pin
)
136 u_char
* b
= (char *)pin
;
140 for (p
= cp
, i
= 0; i
< 4; i
++) {
141 u_long l
= strtoul(p
, 0, 0);
146 if (i
< 3 && p
== NULL
)
154 * Function: parse_booter_path
156 * Parse a string of the form:
157 * "<IP>:<host>:<mount>[:<image_path>]"
158 * into the given ip address, host, mount point, and optionally, image_path.
161 * The passed in string is modified i.e. ':' is replaced by '\0'.
163 * "17.202.16.17:seaport:/release/.images/Image9/CurrentHera"
165 static __inline__ boolean_t
166 parse_booter_path(char * path
, struct in_addr
* iaddr_p
, char * * host
,
167 char * * mount_dir
, char * * image_path
)
174 colon
= strchr(start
, ':');
179 if (inet_aton(start
, iaddr_p
) != 1) {
185 colon
= strchr(start
, ':');
194 colon
= strchr(start
, ':');
209 * Function: find_colon
211 * Find the next unescaped instance of the colon character.
212 * If a colon is escaped (preceded by a backslash '\' character),
213 * shift the string over by one character to overwrite the backslash.
215 static __inline__
char *
216 find_colon(char * str
)
221 while ((colon
= strchr(start
, ':')) != NULL
) {
225 if (colon
== start
) {
228 if (colon
[-1] != '\\')
230 for (dst
= colon
- 1, src
= colon
; *dst
!= '\0'; dst
++, src
++) {
239 * Function: parse_netboot_path
241 * Parse a string of the form:
242 * "nfs:<IP>:<mount>[:<image_path>]"
243 * into the given ip address, host, mount point, and optionally, image_path.
245 * - the passed in string is modified i.e. ':' is replaced by '\0'
246 * - literal colons must be escaped with a backslash
249 * nfs:17.202.42.112:/Library/NetBoot/NetBootSP0:Jaguar/Jaguar.dmg
250 * nfs:17.202.42.112:/Volumes/Foo\:/Library/NetBoot/NetBootSP0:Jaguar/Jaguar.dmg
252 static __inline__ boolean_t
253 parse_netboot_path(char * path
, struct in_addr
* iaddr_p
, char * * host
,
254 char * * mount_dir
, char * * image_path
)
256 static char tmp
[MAX_IPv4_STR_LEN
]; /* Danger - not thread safe */
260 if (strncmp(path
, kNetBootRootPathPrefixNFS
,
261 strlen(kNetBootRootPathPrefixNFS
)) != 0) {
266 start
= path
+ strlen(kNetBootRootPathPrefixNFS
);
267 colon
= strchr(start
, ':');
272 if (inet_aton(start
, iaddr_p
) != 1) {
278 colon
= find_colon(start
);
287 (void)find_colon(start
);
290 *host
= inet_ntop(AF_INET
, iaddr_p
, tmp
, sizeof(tmp
));
295 parse_image_path(char * path
, struct in_addr
* iaddr_p
, char * * host
,
296 char * * mount_dir
, char * * image_path
)
298 if (path
[0] >= '0' && path
[0] <= '9') {
299 return (parse_booter_path(path
, iaddr_p
, host
, mount_dir
,
302 return (parse_netboot_path(path
, iaddr_p
, host
, mount_dir
,
307 get_root_path(char * root_path
)
310 boolean_t found
= FALSE
;
314 entry
= IOBSDRegistryEntryForDeviceTree("/chosen");
318 pkt
= IOBSDRegistryEntryGetData(entry
, BSDP_RESPONSE
, &pkt_len
);
319 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
320 printf("netboot: retrieving root path from BSDP response\n");
323 pkt
= IOBSDRegistryEntryGetData(entry
, BOOTP_RESPONSE
,
325 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
326 printf("netboot: retrieving root path from BOOTP response\n");
335 reply
= (struct dhcp
*)pkt
;
336 (void)dhcpol_parse_packet(&options
, reply
, pkt_len
, NULL
);
338 path
= (char *)dhcpol_find(&options
,
339 dhcptag_root_path_e
, &len
, NULL
);
341 bcopy(path
, root_path
, len
);
342 root_path
[len
] = '\0';
346 IOBSDRegistryEntryRelease(entry
);
351 static struct netboot_info
*
352 netboot_info_init(struct in_addr iaddr
)
354 struct netboot_info
* info
;
355 char * root_path
= NULL
;
356 boolean_t use_hdix
= TRUE
;
357 char * vndevice
= NULL
;
359 MALLOC_ZONE(vndevice
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
360 if (vndevice
== NULL
)
361 panic("netboot_info_init: M_NAMEI zone exhausted");
362 if (PE_parse_boot_arg("vndevice", vndevice
) == TRUE
) {
365 FREE_ZONE(vndevice
, MAXPATHLEN
, M_NAMEI
);
367 info
= (struct netboot_info
*)kalloc(sizeof(*info
));
368 bzero(info
, sizeof(*info
));
369 info
->client_ip
= iaddr
;
370 info
->image_type
= kNetBootImageTypeUnknown
;
371 info
->use_hdix
= use_hdix
;
373 /* check for a booter-specified path then a NetBoot path */
374 MALLOC_ZONE(root_path
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
375 if (root_path
== NULL
)
376 panic("netboot_info_init: M_NAMEI zone exhausted");
377 if (PE_parse_boot_arg("rp", root_path
) == TRUE
378 || PE_parse_boot_arg("rootpath", root_path
) == TRUE
379 || get_root_path(root_path
) == TRUE
) {
380 char * server_name
= NULL
;
381 char * mount_point
= NULL
;
382 char * image_path
= NULL
;
383 struct in_addr server_ip
;
385 if (parse_image_path(root_path
, &server_ip
, &server_name
,
386 &mount_point
, &image_path
)) {
387 info
->image_type
= kNetBootImageTypeNFS
;
388 info
->server_ip
= server_ip
;
389 info
->server_name_length
= strlen(server_name
) + 1;
390 info
->server_name
= (char *)kalloc(info
->server_name_length
);
391 info
->mount_point_length
= strlen(mount_point
) + 1;
392 info
->mount_point
= (char *)kalloc(info
->mount_point_length
);
393 strcpy(info
->server_name
, server_name
);
394 strcpy(info
->mount_point
, mount_point
);
396 printf("Server %s Mount %s",
397 server_name
, info
->mount_point
);
398 if (image_path
!= NULL
) {
399 boolean_t needs_slash
= FALSE
;
401 info
->image_path_length
= strlen(image_path
) + 1;
402 if (image_path
[0] != '/') {
404 info
->image_path_length
++;
406 info
->image_path
= (char *)kalloc(info
->image_path_length
);
408 info
->image_path
[0] = '/';
409 strcpy(info
->image_path
+ 1, image_path
);
412 strcpy(info
->image_path
, image_path
);
414 printf(" Image %s", info
->image_path
);
418 else if (strncmp(root_path
, kNetBootRootPathPrefixHTTP
,
419 strlen(kNetBootRootPathPrefixHTTP
)) == 0) {
420 /* only HDIX supports HTTP */
421 info
->image_type
= kNetBootImageTypeHTTP
;
422 info
->use_hdix
= TRUE
;
423 info
->image_path_length
= strlen(root_path
) + 1;
424 info
->image_path
= (char *)kalloc(info
->image_path_length
);
425 strcpy(info
->image_path
, root_path
);
428 printf("netboot: root path uses unrecognized format\n");
431 FREE_ZONE(root_path
, MAXPATHLEN
, M_NAMEI
);
436 netboot_info_free(struct netboot_info
* * info_p
)
438 struct netboot_info
* info
= *info_p
;
441 if (info
->mount_point
) {
442 kfree(info
->mount_point
, info
->mount_point_length
);
444 if (info
->server_name
) {
445 kfree(info
->server_name
, info
->server_name_length
);
447 if (info
->image_path
) {
448 kfree(info
->image_path
, info
->image_path_length
);
450 kfree(info
, sizeof(*info
));
457 netboot_iaddr(struct in_addr
* iaddr_p
)
459 if (S_netboot_info_p
== NULL
)
462 *iaddr_p
= S_netboot_info_p
->client_ip
;
467 netboot_rootpath(struct in_addr
* server_ip
,
468 char * name
, int name_len
,
469 char * path
, int path_len
)
471 if (S_netboot_info_p
== NULL
)
477 if (S_netboot_info_p
->mount_point_length
== 0) {
480 if (path_len
< S_netboot_info_p
->mount_point_length
) {
481 printf("netboot: path too small %d < %d\n",
482 path_len
, S_netboot_info_p
->mount_point_length
);
485 strcpy(path
, S_netboot_info_p
->mount_point
);
486 strncpy(name
, S_netboot_info_p
->server_name
, name_len
);
487 *server_ip
= S_netboot_info_p
->server_ip
;
493 get_ip_parameters(struct in_addr
* iaddr_p
, struct in_addr
* netmask_p
,
494 struct in_addr
* router_p
)
501 entry
= IOBSDRegistryEntryForDeviceTree("/chosen");
505 pkt
= IOBSDRegistryEntryGetData(entry
, DHCP_RESPONSE
, &pkt_len
);
506 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
507 printf("netboot: retrieving IP information from DHCP response\n");
510 pkt
= IOBSDRegistryEntryGetData(entry
, BOOTP_RESPONSE
, &pkt_len
);
511 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
512 printf("netboot: retrieving IP information from BOOTP response\n");
521 reply
= (struct dhcp
*)pkt
;
522 (void)dhcpol_parse_packet(&options
, reply
, pkt_len
, NULL
);
523 *iaddr_p
= reply
->dp_yiaddr
;
524 ip
= (struct in_addr
*)
525 dhcpol_find(&options
,
526 dhcptag_subnet_mask_e
, &len
, NULL
);
530 ip
= (struct in_addr
*)
531 dhcpol_find(&options
, dhcptag_router_e
, &len
, NULL
);
536 IOBSDRegistryEntryRelease(entry
);
537 return (pkt
!= NULL
);
541 inet_aifaddr(struct socket
* so
, char * name
, const struct in_addr
* addr
,
542 const struct in_addr
* mask
,
543 const struct in_addr
* broadcast
)
545 struct sockaddr blank_sin
;
546 struct ifaliasreq ifra
;
548 bzero(&blank_sin
, sizeof(blank_sin
));
549 blank_sin
.sa_len
= sizeof(blank_sin
);
550 blank_sin
.sa_family
= AF_INET
;
552 bzero(&ifra
, sizeof(ifra
));
553 strncpy(ifra
.ifra_name
, name
, sizeof(ifra
.ifra_name
));
555 ifra
.ifra_addr
= blank_sin
;
556 ((struct sockaddr_in
*)&ifra
.ifra_addr
)->sin_addr
= *addr
;
559 ifra
.ifra_mask
= blank_sin
;
560 ((struct sockaddr_in
*)&ifra
.ifra_mask
)->sin_addr
= *mask
;
563 ifra
.ifra_broadaddr
= blank_sin
;
564 ((struct sockaddr_in
*)&ifra
.ifra_broadaddr
)->sin_addr
= *broadcast
;
566 return (ifioctl(so
, SIOCAIFADDR
, (caddr_t
)&ifra
, current_proc()));
570 route_cmd(int cmd
, struct in_addr d
, struct in_addr g
,
571 struct in_addr m
, u_long more_flags
)
573 struct sockaddr_in dst
;
574 u_long flags
= RTF_UP
| RTF_STATIC
;
575 struct sockaddr_in gw
;
576 struct sockaddr_in mask
;
581 bzero((caddr_t
)&dst
, sizeof(dst
));
582 dst
.sin_len
= sizeof(dst
);
583 dst
.sin_family
= AF_INET
;
587 bzero((caddr_t
)&gw
, sizeof(gw
));
588 gw
.sin_len
= sizeof(gw
);
589 gw
.sin_family
= AF_INET
;
593 bzero(&mask
, sizeof(mask
));
594 mask
.sin_len
= sizeof(mask
);
595 mask
.sin_family
= AF_INET
;
598 return (rtrequest(cmd
, (struct sockaddr
*)&dst
, (struct sockaddr
*)&gw
,
599 (struct sockaddr
*)&mask
, flags
, NULL
));
603 default_route_add(struct in_addr router
, boolean_t proxy_arp
)
606 struct in_addr zeroes
= { 0 };
608 if (proxy_arp
== FALSE
) {
609 flags
|= RTF_GATEWAY
;
611 return (route_cmd(RTM_ADD
, zeroes
, router
, zeroes
, flags
));
615 host_route_delete(struct in_addr host
)
617 struct in_addr zeroes
= { 0 };
619 return (route_cmd(RTM_DELETE
, host
, zeroes
, zeroes
, RTF_HOST
));
622 static struct ifnet
*
625 struct ifnet
* ifp
= NULL
;
628 ifp
= ifunit(rootdevice
);
631 ifnet_head_lock_shared();
632 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
)
633 if ((ifp
->if_flags
& (IFF_LOOPBACK
|IFF_POINTOPOINT
)) == 0)
641 netboot_mountroot(void)
644 struct in_addr iaddr
= { 0 };
647 struct in_addr netmask
= { 0 };
648 struct proc
* procp
= current_proc();
649 struct in_addr router
= { 0 };
650 struct socket
* so
= NULL
;
653 bzero(&ifr
, sizeof(ifr
));
656 /* find the interface */
657 ifp
= find_interface();
659 printf("netboot: no suitable interface\n");
663 sprintf(ifr
.ifr_name
, "%s%d", ifp
->if_name
, ifp
->if_unit
);
664 printf("netboot: using network interface '%s'\n", ifr
.ifr_name
);
667 if ((error
= socreate(AF_INET
, &so
, SOCK_DGRAM
, 0)) != 0) {
668 printf("netboot: socreate, error=%d\n", error
);
671 ifr
.ifr_flags
= ifp
->if_flags
| IFF_UP
;
672 error
= ifioctl(so
, SIOCSIFFLAGS
, (caddr_t
)&ifr
, procp
);
674 printf("netboot: SIFFLAGS, error=%d\n", error
);
678 /* grab information from the registry */
679 if (get_ip_parameters(&iaddr
, &netmask
, &router
) == FALSE
) {
680 /* use BOOTP to retrieve IP address, netmask and router */
681 error
= bootp(ifp
, &iaddr
, 32, &netmask
, &router
, procp
);
683 printf("netboot: BOOTP failed %d\n", error
);
687 printf("netboot: IP address " IP_FORMAT
, IP_LIST(&iaddr
));
688 if (netmask
.s_addr
) {
689 printf(" netmask " IP_FORMAT
, IP_LIST(&netmask
));
692 printf(" router " IP_FORMAT
, IP_LIST(&router
));
695 error
= inet_aifaddr(so
, ifr
.ifr_name
, &iaddr
, &netmask
, NULL
);
697 printf("netboot: inet_aifaddr failed, %d\n", error
);
700 if (router
.s_addr
== 0) {
701 /* enable proxy arp if we don't have a router */
702 router
.s_addr
= iaddr
.s_addr
;
704 printf("netboot: adding default route " IP_FORMAT
"\n",
706 error
= default_route_add(router
, router
.s_addr
== iaddr
.s_addr
);
708 printf("netboot: default_route_add failed %d\n", error
);
713 S_netboot_info_p
= netboot_info_init(iaddr
);
714 switch (S_netboot_info_p
->image_type
) {
716 case kNetBootImageTypeNFS
:
717 for (try = 1; TRUE
; try++) {
718 error
= nfs_mountroot();
722 printf("netboot: nfs_mountroot() attempt %u failed; "
723 "clearing ARP entry and trying again\n", try);
725 * error is either EHOSTDOWN or EHOSTUNREACH, which likely means
726 * that the port we're plugged into has spanning tree enabled,
727 * and either the router or the server can't answer our ARP
728 * requests. Clear the incomplete ARP entry by removing the
729 * appropriate route, depending on the error code:
730 * EHOSTDOWN NFS server's route
731 * EHOSTUNREACH router's route
737 /* remove the server's arp entry */
738 error
= host_route_delete(S_netboot_info_p
->server_ip
);
740 printf("netboot: host_route_delete(" IP_FORMAT
742 IP_LIST(&S_netboot_info_p
->server_ip
), error
);
746 error
= host_route_delete(router
);
748 printf("netboot: host_route_delete(" IP_FORMAT
749 ") failed %d\n", IP_LIST(&router
), error
);
755 case kNetBootImageTypeHTTP
:
756 error
= netboot_setup(procp
);
774 netboot_setup(struct proc
* p
)
779 if (S_netboot_info_p
== NULL
780 || S_netboot_info_p
->image_path
== NULL
) {
783 if (S_netboot_info_p
->use_hdix
) {
784 printf("netboot_setup: calling di_root_image\n");
785 error
= di_root_image(S_netboot_info_p
->image_path
,
788 printf("netboot_setup: di_root_image: failed %d\n", error
);
793 printf("netboot_setup: calling vndevice_root_image\n");
794 error
= vndevice_root_image(S_netboot_info_p
->image_path
,
797 printf("netboot_setup: vndevice_root_image: failed %d\n", error
);
803 printf("netboot: root device 0x%x\n", rootdev
);
804 error
= vfs_mountroot();
805 if (error
== 0 && rootvnode
!= NULL
) {
808 struct vfs_context context
;
811 context
.vc_ucred
= proc_ucred(p
); /* XXX kauth_cred_get() ??? proxy */
813 /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */
814 if (VFS_ROOT(mountlist
.tqh_last
, &newdp
, &context
))
815 panic("netboot_setup: cannot find root vnode");
820 filedesc0
.fd_cdir
= newdp
;
823 TAILQ_REMOVE(&mountlist
, TAILQ_FIRST(&mountlist
), mnt_list
);
825 mountlist
.tqh_first
->mnt_flag
|= MNT_ROOTFS
;
828 netboot_info_free(&S_netboot_info_p
);