2 * Copyright (c) 2001-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
33 * 14 December, 2001 Dieter Siegmund (dieter@apple.com)
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/ioctl.h>
41 #include <sys/proc_internal.h>
42 #include <sys/mount_internal.h>
44 #include <sys/filedesc.h>
45 #include <sys/vnode_internal.h>
46 #include <sys/malloc.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/reboot.h>
51 #include <net/if_dl.h>
52 #include <net/if_types.h>
53 #include <net/route.h>
54 #include <netinet/in.h>
55 #include <netinet/if_ether.h>
56 #include <netinet/dhcp_options.h>
57 #include <pexpert/pexpert.h>
59 #include <kern/kern_types.h>
60 #include <kern/kalloc.h>
62 //#include <libkern/libkern.h>
63 extern struct filedesc filedesc0
;
65 extern int strncmp(const char *,const char *, size_t);
66 extern unsigned long strtoul(const char *, char **, int);
67 extern char * strchr(const char *str
, int ch
);
69 extern int nfs_mountroot(void); /* nfs_vfsops.c */
70 extern int (*mountroot
)(void);
72 extern unsigned char rootdevice
[];
74 static int S_netboot
= 0;
75 static struct netboot_info
* S_netboot_info_p
;
78 IOBSDRegistryEntryForDeviceTree(const char * path
);
81 IOBSDRegistryEntryRelease(void * entry
);
84 IOBSDRegistryEntryGetData(void * entry
, const char * property_name
,
87 extern int vndevice_root_image(const char * path
, char devname
[],
89 extern int di_root_image(const char *path
, char devname
[], dev_t
*dev_p
);
91 #define BOOTP_RESPONSE "bootp-response"
92 #define BSDP_RESPONSE "bsdp-response"
93 #define DHCP_RESPONSE "dhcp-response"
96 bootp(struct ifnet
* ifp
, struct in_addr
* iaddr_p
, int max_retry
,
97 struct in_addr
* netmask_p
, struct in_addr
* router_p
,
101 /* forward declarations */
102 int inet_aton(char * cp
, struct in_addr
* pin
);
104 boolean_t
netboot_iaddr(struct in_addr
* iaddr_p
);
105 boolean_t
netboot_rootpath(struct in_addr
* server_ip
,
106 char * name
, int name_len
,
107 char * path
, int path_len
);
108 int netboot_setup(struct proc
* p
);
109 int netboot_mountroot(void);
110 int netboot_root(void);
114 #define IP_FORMAT "%d.%d.%d.%d"
115 #define IP_CH(ip) ((u_char *)ip)
116 #define IP_LIST(ip) IP_CH(ip)[0],IP_CH(ip)[1],IP_CH(ip)[2],IP_CH(ip)[3]
118 #define kNetBootRootPathPrefixNFS "nfs:"
119 #define kNetBootRootPathPrefixHTTP "http:"
122 kNetBootImageTypeUnknown
= 0,
123 kNetBootImageTypeNFS
= 1,
124 kNetBootImageTypeHTTP
= 2,
127 struct netboot_info
{
128 struct in_addr client_ip
;
129 struct in_addr server_ip
;
131 int server_name_length
;
133 int mount_point_length
;
135 int image_path_length
;
136 NetBootImageType image_type
;
141 inet_aton(char * cp
, struct in_addr
* pin
)
143 u_char
* b
= (char *)pin
;
147 for (p
= cp
, i
= 0; i
< 4; i
++) {
148 u_long l
= strtoul(p
, 0, 0);
153 if (i
< 3 && p
== NULL
)
161 * Function: parse_booter_path
163 * Parse a string of the form:
164 * "<IP>:<host>:<mount>[:<image_path>]"
165 * into the given ip address, host, mount point, and optionally, image_path.
168 * The passed in string is modified i.e. ':' is replaced by '\0'.
170 * "17.202.16.17:seaport:/release/.images/Image9/CurrentHera"
172 static __inline__ boolean_t
173 parse_booter_path(char * path
, struct in_addr
* iaddr_p
, char * * host
,
174 char * * mount_dir
, char * * image_path
)
181 colon
= strchr(start
, ':');
186 if (inet_aton(start
, iaddr_p
) != 1) {
192 colon
= strchr(start
, ':');
201 colon
= strchr(start
, ':');
216 * Function: find_colon
218 * Find the next unescaped instance of the colon character.
219 * If a colon is escaped (preceded by a backslash '\' character),
220 * shift the string over by one character to overwrite the backslash.
222 static __inline__
char *
223 find_colon(char * str
)
228 while ((colon
= strchr(start
, ':')) != NULL
) {
232 if (colon
== start
) {
235 if (colon
[-1] != '\\')
237 for (dst
= colon
- 1, src
= colon
; *dst
!= '\0'; dst
++, src
++) {
246 * Function: parse_netboot_path
248 * Parse a string of the form:
249 * "nfs:<IP>:<mount>[:<image_path>]"
250 * into the given ip address, host, mount point, and optionally, image_path.
252 * - the passed in string is modified i.e. ':' is replaced by '\0'
253 * - literal colons must be escaped with a backslash
256 * nfs:17.202.42.112:/Library/NetBoot/NetBootSP0:Jaguar/Jaguar.dmg
257 * nfs:17.202.42.112:/Volumes/Foo\:/Library/NetBoot/NetBootSP0:Jaguar/Jaguar.dmg
259 static __inline__ boolean_t
260 parse_netboot_path(char * path
, struct in_addr
* iaddr_p
, char * * host
,
261 char * * mount_dir
, char * * image_path
)
263 static char tmp
[MAX_IPv4_STR_LEN
]; /* Danger - not thread safe */
267 if (strncmp(path
, kNetBootRootPathPrefixNFS
,
268 strlen(kNetBootRootPathPrefixNFS
)) != 0) {
273 start
= path
+ strlen(kNetBootRootPathPrefixNFS
);
274 colon
= strchr(start
, ':');
279 if (inet_aton(start
, iaddr_p
) != 1) {
285 colon
= find_colon(start
);
294 (void)find_colon(start
);
297 *host
= inet_ntop(AF_INET
, iaddr_p
, tmp
, sizeof(tmp
));
302 parse_image_path(char * path
, struct in_addr
* iaddr_p
, char * * host
,
303 char * * mount_dir
, char * * image_path
)
305 if (path
[0] >= '0' && path
[0] <= '9') {
306 return (parse_booter_path(path
, iaddr_p
, host
, mount_dir
,
309 return (parse_netboot_path(path
, iaddr_p
, host
, mount_dir
,
314 get_root_path(char * root_path
)
317 boolean_t found
= FALSE
;
321 entry
= IOBSDRegistryEntryForDeviceTree("/chosen");
325 pkt
= IOBSDRegistryEntryGetData(entry
, BSDP_RESPONSE
, &pkt_len
);
326 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
327 printf("netboot: retrieving root path from BSDP response\n");
330 pkt
= IOBSDRegistryEntryGetData(entry
, BOOTP_RESPONSE
,
332 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
333 printf("netboot: retrieving root path from BOOTP response\n");
342 reply
= (struct dhcp
*)pkt
;
343 (void)dhcpol_parse_packet(&options
, reply
, pkt_len
, NULL
);
345 path
= (char *)dhcpol_find(&options
,
346 dhcptag_root_path_e
, &len
, NULL
);
348 bcopy(path
, root_path
, len
);
349 root_path
[len
] = '\0';
353 IOBSDRegistryEntryRelease(entry
);
358 static struct netboot_info
*
359 netboot_info_init(struct in_addr iaddr
)
361 struct netboot_info
* info
;
362 char * root_path
= NULL
;
363 boolean_t use_hdix
= TRUE
;
364 char * vndevice
= NULL
;
366 MALLOC_ZONE(vndevice
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
367 if (vndevice
== NULL
)
368 panic("netboot_info_init: M_NAMEI zone exhausted");
369 if (PE_parse_boot_arg("vndevice", vndevice
) == TRUE
) {
372 FREE_ZONE(vndevice
, MAXPATHLEN
, M_NAMEI
);
374 info
= (struct netboot_info
*)kalloc(sizeof(*info
));
375 bzero(info
, sizeof(*info
));
376 info
->client_ip
= iaddr
;
377 info
->image_type
= kNetBootImageTypeUnknown
;
378 info
->use_hdix
= use_hdix
;
380 /* check for a booter-specified path then a NetBoot path */
381 MALLOC_ZONE(root_path
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
382 if (root_path
== NULL
)
383 panic("netboot_info_init: M_NAMEI zone exhausted");
384 if (PE_parse_boot_arg("rp", root_path
) == TRUE
385 || PE_parse_boot_arg("rootpath", root_path
) == TRUE
386 || get_root_path(root_path
) == TRUE
) {
387 char * server_name
= NULL
;
388 char * mount_point
= NULL
;
389 char * image_path
= NULL
;
390 struct in_addr server_ip
;
392 if (parse_image_path(root_path
, &server_ip
, &server_name
,
393 &mount_point
, &image_path
)) {
394 info
->image_type
= kNetBootImageTypeNFS
;
395 info
->server_ip
= server_ip
;
396 info
->server_name_length
= strlen(server_name
) + 1;
397 info
->server_name
= (char *)kalloc(info
->server_name_length
);
398 info
->mount_point_length
= strlen(mount_point
) + 1;
399 info
->mount_point
= (char *)kalloc(info
->mount_point_length
);
400 strcpy(info
->server_name
, server_name
);
401 strcpy(info
->mount_point
, mount_point
);
403 printf("Server %s Mount %s",
404 server_name
, info
->mount_point
);
405 if (image_path
!= NULL
) {
406 boolean_t needs_slash
= FALSE
;
408 info
->image_path_length
= strlen(image_path
) + 1;
409 if (image_path
[0] != '/') {
411 info
->image_path_length
++;
413 info
->image_path
= (char *)kalloc(info
->image_path_length
);
415 info
->image_path
[0] = '/';
416 strcpy(info
->image_path
+ 1, image_path
);
419 strcpy(info
->image_path
, image_path
);
421 printf(" Image %s", info
->image_path
);
425 else if (strncmp(root_path
, kNetBootRootPathPrefixHTTP
,
426 strlen(kNetBootRootPathPrefixHTTP
)) == 0) {
427 /* only HDIX supports HTTP */
428 info
->image_type
= kNetBootImageTypeHTTP
;
429 info
->use_hdix
= TRUE
;
430 info
->image_path_length
= strlen(root_path
) + 1;
431 info
->image_path
= (char *)kalloc(info
->image_path_length
);
432 strcpy(info
->image_path
, root_path
);
435 printf("netboot: root path uses unrecognized format\n");
438 FREE_ZONE(root_path
, MAXPATHLEN
, M_NAMEI
);
443 netboot_info_free(struct netboot_info
* * info_p
)
445 struct netboot_info
* info
= *info_p
;
448 if (info
->mount_point
) {
449 kfree(info
->mount_point
, info
->mount_point_length
);
451 if (info
->server_name
) {
452 kfree(info
->server_name
, info
->server_name_length
);
454 if (info
->image_path
) {
455 kfree(info
->image_path
, info
->image_path_length
);
457 kfree(info
, sizeof(*info
));
464 netboot_iaddr(struct in_addr
* iaddr_p
)
466 if (S_netboot_info_p
== NULL
)
469 *iaddr_p
= S_netboot_info_p
->client_ip
;
474 netboot_rootpath(struct in_addr
* server_ip
,
475 char * name
, int name_len
,
476 char * path
, int path_len
)
478 if (S_netboot_info_p
== NULL
)
484 if (S_netboot_info_p
->mount_point_length
== 0) {
487 if (path_len
< S_netboot_info_p
->mount_point_length
) {
488 printf("netboot: path too small %d < %d\n",
489 path_len
, S_netboot_info_p
->mount_point_length
);
492 strcpy(path
, S_netboot_info_p
->mount_point
);
493 strncpy(name
, S_netboot_info_p
->server_name
, name_len
);
494 *server_ip
= S_netboot_info_p
->server_ip
;
500 get_ip_parameters(struct in_addr
* iaddr_p
, struct in_addr
* netmask_p
,
501 struct in_addr
* router_p
)
508 entry
= IOBSDRegistryEntryForDeviceTree("/chosen");
512 pkt
= IOBSDRegistryEntryGetData(entry
, DHCP_RESPONSE
, &pkt_len
);
513 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
514 printf("netboot: retrieving IP information from DHCP response\n");
517 pkt
= IOBSDRegistryEntryGetData(entry
, BOOTP_RESPONSE
, &pkt_len
);
518 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
519 printf("netboot: retrieving IP information from BOOTP response\n");
528 reply
= (struct dhcp
*)pkt
;
529 (void)dhcpol_parse_packet(&options
, reply
, pkt_len
, NULL
);
530 *iaddr_p
= reply
->dp_yiaddr
;
531 ip
= (struct in_addr
*)
532 dhcpol_find(&options
,
533 dhcptag_subnet_mask_e
, &len
, NULL
);
537 ip
= (struct in_addr
*)
538 dhcpol_find(&options
, dhcptag_router_e
, &len
, NULL
);
543 IOBSDRegistryEntryRelease(entry
);
544 return (pkt
!= NULL
);
548 inet_aifaddr(struct socket
* so
, char * name
, const struct in_addr
* addr
,
549 const struct in_addr
* mask
,
550 const struct in_addr
* broadcast
)
552 struct sockaddr blank_sin
;
553 struct ifaliasreq ifra
;
555 bzero(&blank_sin
, sizeof(blank_sin
));
556 blank_sin
.sa_len
= sizeof(blank_sin
);
557 blank_sin
.sa_family
= AF_INET
;
559 bzero(&ifra
, sizeof(ifra
));
560 strncpy(ifra
.ifra_name
, name
, sizeof(ifra
.ifra_name
));
562 ifra
.ifra_addr
= blank_sin
;
563 ((struct sockaddr_in
*)&ifra
.ifra_addr
)->sin_addr
= *addr
;
566 ifra
.ifra_mask
= blank_sin
;
567 ((struct sockaddr_in
*)&ifra
.ifra_mask
)->sin_addr
= *mask
;
570 ifra
.ifra_broadaddr
= blank_sin
;
571 ((struct sockaddr_in
*)&ifra
.ifra_broadaddr
)->sin_addr
= *broadcast
;
573 return (ifioctl(so
, SIOCAIFADDR
, (caddr_t
)&ifra
, current_proc()));
577 route_cmd(int cmd
, struct in_addr d
, struct in_addr g
,
578 struct in_addr m
, u_long more_flags
)
580 struct sockaddr_in dst
;
581 u_long flags
= RTF_UP
| RTF_STATIC
;
582 struct sockaddr_in gw
;
583 struct sockaddr_in mask
;
588 bzero((caddr_t
)&dst
, sizeof(dst
));
589 dst
.sin_len
= sizeof(dst
);
590 dst
.sin_family
= AF_INET
;
594 bzero((caddr_t
)&gw
, sizeof(gw
));
595 gw
.sin_len
= sizeof(gw
);
596 gw
.sin_family
= AF_INET
;
600 bzero(&mask
, sizeof(mask
));
601 mask
.sin_len
= sizeof(mask
);
602 mask
.sin_family
= AF_INET
;
605 return (rtrequest(cmd
, (struct sockaddr
*)&dst
, (struct sockaddr
*)&gw
,
606 (struct sockaddr
*)&mask
, flags
, NULL
));
610 default_route_add(struct in_addr router
, boolean_t proxy_arp
)
613 struct in_addr zeroes
= { 0 };
615 if (proxy_arp
== FALSE
) {
616 flags
|= RTF_GATEWAY
;
618 return (route_cmd(RTM_ADD
, zeroes
, router
, zeroes
, flags
));
622 host_route_delete(struct in_addr host
)
624 struct in_addr zeroes
= { 0 };
626 return (route_cmd(RTM_DELETE
, host
, zeroes
, zeroes
, RTF_HOST
));
629 static struct ifnet
*
632 struct ifnet
* ifp
= NULL
;
635 ifp
= ifunit(rootdevice
);
638 ifnet_head_lock_shared();
639 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
)
640 if ((ifp
->if_flags
& (IFF_LOOPBACK
|IFF_POINTOPOINT
)) == 0)
648 netboot_mountroot(void)
651 struct in_addr iaddr
= { 0 };
654 struct in_addr netmask
= { 0 };
655 struct proc
* procp
= current_proc();
656 struct in_addr router
= { 0 };
657 struct socket
* so
= NULL
;
660 bzero(&ifr
, sizeof(ifr
));
663 /* find the interface */
664 ifp
= find_interface();
666 printf("netboot: no suitable interface\n");
670 sprintf(ifr
.ifr_name
, "%s%d", ifp
->if_name
, ifp
->if_unit
);
671 printf("netboot: using network interface '%s'\n", ifr
.ifr_name
);
674 if ((error
= socreate(AF_INET
, &so
, SOCK_DGRAM
, 0)) != 0) {
675 printf("netboot: socreate, error=%d\n", error
);
678 ifr
.ifr_flags
= ifp
->if_flags
| IFF_UP
;
679 error
= ifioctl(so
, SIOCSIFFLAGS
, (caddr_t
)&ifr
, procp
);
681 printf("netboot: SIFFLAGS, error=%d\n", error
);
685 /* grab information from the registry */
686 if (get_ip_parameters(&iaddr
, &netmask
, &router
) == FALSE
) {
687 /* use BOOTP to retrieve IP address, netmask and router */
688 error
= bootp(ifp
, &iaddr
, 32, &netmask
, &router
, procp
);
690 printf("netboot: BOOTP failed %d\n", error
);
694 printf("netboot: IP address " IP_FORMAT
, IP_LIST(&iaddr
));
695 if (netmask
.s_addr
) {
696 printf(" netmask " IP_FORMAT
, IP_LIST(&netmask
));
699 printf(" router " IP_FORMAT
, IP_LIST(&router
));
702 error
= inet_aifaddr(so
, ifr
.ifr_name
, &iaddr
, &netmask
, NULL
);
704 printf("netboot: inet_aifaddr failed, %d\n", error
);
707 if (router
.s_addr
== 0) {
708 /* enable proxy arp if we don't have a router */
709 router
.s_addr
= iaddr
.s_addr
;
711 printf("netboot: adding default route " IP_FORMAT
"\n",
713 error
= default_route_add(router
, router
.s_addr
== iaddr
.s_addr
);
715 printf("netboot: default_route_add failed %d\n", error
);
720 S_netboot_info_p
= netboot_info_init(iaddr
);
721 switch (S_netboot_info_p
->image_type
) {
723 case kNetBootImageTypeNFS
:
724 for (try = 1; TRUE
; try++) {
725 error
= nfs_mountroot();
729 printf("netboot: nfs_mountroot() attempt %u failed; "
730 "clearing ARP entry and trying again\n", try);
732 * error is either EHOSTDOWN or EHOSTUNREACH, which likely means
733 * that the port we're plugged into has spanning tree enabled,
734 * and either the router or the server can't answer our ARP
735 * requests. Clear the incomplete ARP entry by removing the
736 * appropriate route, depending on the error code:
737 * EHOSTDOWN NFS server's route
738 * EHOSTUNREACH router's route
744 /* remove the server's arp entry */
745 error
= host_route_delete(S_netboot_info_p
->server_ip
);
747 printf("netboot: host_route_delete(" IP_FORMAT
749 IP_LIST(&S_netboot_info_p
->server_ip
), error
);
753 error
= host_route_delete(router
);
755 printf("netboot: host_route_delete(" IP_FORMAT
756 ") failed %d\n", IP_LIST(&router
), error
);
762 case kNetBootImageTypeHTTP
:
763 error
= netboot_setup(procp
);
781 netboot_setup(struct proc
* p
)
786 if (S_netboot_info_p
== NULL
787 || S_netboot_info_p
->image_path
== NULL
) {
790 if (S_netboot_info_p
->use_hdix
) {
791 printf("netboot_setup: calling di_root_image\n");
792 error
= di_root_image(S_netboot_info_p
->image_path
,
795 printf("netboot_setup: di_root_image: failed %d\n", error
);
800 printf("netboot_setup: calling vndevice_root_image\n");
801 error
= vndevice_root_image(S_netboot_info_p
->image_path
,
804 printf("netboot_setup: vndevice_root_image: failed %d\n", error
);
810 printf("netboot: root device 0x%x\n", rootdev
);
811 error
= vfs_mountroot();
812 if (error
== 0 && rootvnode
!= NULL
) {
815 struct vfs_context context
;
818 context
.vc_ucred
= proc_ucred(p
); /* XXX kauth_cred_get() ??? proxy */
820 /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */
821 if (VFS_ROOT(mountlist
.tqh_last
, &newdp
, &context
))
822 panic("netboot_setup: cannot find root vnode");
827 filedesc0
.fd_cdir
= newdp
;
830 TAILQ_REMOVE(&mountlist
, TAILQ_FIRST(&mountlist
), mnt_list
);
832 mountlist
.tqh_first
->mnt_flag
|= MNT_ROOTFS
;
835 netboot_info_free(&S_netboot_info_p
);