2 * Copyright (c) 2001-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
28 * 14 December, 2001 Dieter Siegmund (dieter@apple.com)
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
35 #include <sys/ioctl.h>
37 #include <sys/mount.h>
39 #include <sys/filedesc.h>
40 #include <sys/vnode.h>
41 #include <sys/malloc.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/reboot.h>
46 #include <net/if_dl.h>
47 #include <net/if_types.h>
48 #include <net/route.h>
49 #include <netinet/in.h>
50 #include <netinet/if_ether.h>
51 #include <netinet/dhcp_options.h>
52 #include <kern/kalloc.h>
53 #include <pexpert/pexpert.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
)
259 if (strncmp(path
, kNetBootRootPathPrefixNFS
,
260 strlen(kNetBootRootPathPrefixNFS
)) != 0) {
265 start
= path
+ strlen(kNetBootRootPathPrefixNFS
);
266 colon
= strchr(start
, ':');
271 if (inet_aton(start
, iaddr_p
) != 1) {
277 colon
= find_colon(start
);
286 (void)find_colon(start
);
289 *host
= inet_ntoa(*iaddr_p
);
294 parse_image_path(char * path
, struct in_addr
* iaddr_p
, char * * host
,
295 char * * mount_dir
, char * * image_path
)
297 if (path
[0] >= '0' && path
[0] <= '9') {
298 return (parse_booter_path(path
, iaddr_p
, host
, mount_dir
,
301 return (parse_netboot_path(path
, iaddr_p
, host
, mount_dir
,
306 get_root_path(char * root_path
)
309 boolean_t found
= FALSE
;
313 entry
= IOBSDRegistryEntryForDeviceTree("/chosen");
317 pkt
= IOBSDRegistryEntryGetData(entry
, BSDP_RESPONSE
, &pkt_len
);
318 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
319 printf("netboot: retrieving root path from BSDP response\n");
322 pkt
= IOBSDRegistryEntryGetData(entry
, BOOTP_RESPONSE
,
324 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
325 printf("netboot: retrieving root path from BOOTP response\n");
334 reply
= (struct dhcp
*)pkt
;
335 (void)dhcpol_parse_packet(&options
, reply
, pkt_len
, NULL
);
337 path
= (char *)dhcpol_find(&options
,
338 dhcptag_root_path_e
, &len
, NULL
);
340 bcopy(path
, root_path
, len
);
341 root_path
[len
] = '\0';
345 IOBSDRegistryEntryRelease(entry
);
350 static struct netboot_info
*
351 netboot_info_init(struct in_addr iaddr
)
353 struct netboot_info
* info
;
354 char * root_path
= NULL
;
355 boolean_t use_hdix
= TRUE
;
356 char * vndevice
= NULL
;
358 MALLOC_ZONE(vndevice
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
359 if (PE_parse_boot_arg("vndevice", vndevice
) == TRUE
) {
362 FREE_ZONE(vndevice
, MAXPATHLEN
, M_NAMEI
);
364 info
= (struct netboot_info
*)kalloc(sizeof(*info
));
365 bzero(info
, sizeof(*info
));
366 info
->client_ip
= iaddr
;
367 info
->image_type
= kNetBootImageTypeUnknown
;
368 info
->use_hdix
= use_hdix
;
370 /* check for a booter-specified path then a NetBoot path */
371 MALLOC_ZONE(root_path
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
372 if (PE_parse_boot_arg("rp", root_path
) == TRUE
373 || PE_parse_boot_arg("rootpath", root_path
) == TRUE
374 || get_root_path(root_path
) == TRUE
) {
375 char * server_name
= NULL
;
376 char * mount_point
= NULL
;
377 char * image_path
= NULL
;
378 struct in_addr server_ip
;
380 if (parse_image_path(root_path
, &server_ip
, &server_name
,
381 &mount_point
, &image_path
)) {
382 info
->image_type
= kNetBootImageTypeNFS
;
383 info
->server_ip
= server_ip
;
384 info
->server_name_length
= strlen(server_name
) + 1;
385 info
->server_name
= (char *)kalloc(info
->server_name_length
);
386 info
->mount_point_length
= strlen(mount_point
) + 1;
387 info
->mount_point
= (char *)kalloc(info
->mount_point_length
);
388 strcpy(info
->server_name
, server_name
);
389 strcpy(info
->mount_point
, mount_point
);
391 printf("Server %s Mount %s",
392 server_name
, info
->mount_point
);
393 if (image_path
!= NULL
) {
394 boolean_t needs_slash
= FALSE
;
396 info
->image_path_length
= strlen(image_path
) + 1;
397 if (image_path
[0] != '/') {
399 info
->image_path_length
++;
401 info
->image_path
= (char *)kalloc(info
->image_path_length
);
403 info
->image_path
[0] = '/';
404 strcpy(info
->image_path
+ 1, image_path
);
407 strcpy(info
->image_path
, image_path
);
409 printf(" Image %s", info
->image_path
);
413 else if (strncmp(root_path
, kNetBootRootPathPrefixHTTP
,
414 strlen(kNetBootRootPathPrefixHTTP
)) == 0) {
415 /* only HDIX supports HTTP */
416 info
->image_type
= kNetBootImageTypeHTTP
;
417 info
->use_hdix
= TRUE
;
418 info
->image_path_length
= strlen(root_path
) + 1;
419 info
->image_path
= (char *)kalloc(info
->image_path_length
);
420 strcpy(info
->image_path
, root_path
);
423 printf("netboot: root path uses unrecognized format\n");
426 FREE_ZONE(root_path
, MAXPATHLEN
, M_NAMEI
);
431 netboot_info_free(struct netboot_info
* * info_p
)
433 struct netboot_info
* info
= *info_p
;
436 if (info
->mount_point
) {
437 kfree((vm_offset_t
)info
->mount_point
, info
->mount_point_length
);
439 if (info
->server_name
) {
440 kfree((vm_offset_t
)info
->server_name
, info
->server_name_length
);
442 if (info
->image_path
) {
443 kfree((vm_offset_t
)info
->image_path
, info
->image_path_length
);
445 kfree((vm_offset_t
)info
, sizeof(*info
));
452 netboot_iaddr(struct in_addr
* iaddr_p
)
454 if (S_netboot_info_p
== NULL
)
457 *iaddr_p
= S_netboot_info_p
->client_ip
;
462 netboot_rootpath(struct in_addr
* server_ip
,
463 char * name
, int name_len
,
464 char * path
, int path_len
)
466 if (S_netboot_info_p
== NULL
)
472 if (S_netboot_info_p
->mount_point_length
== 0) {
475 if (path_len
< S_netboot_info_p
->mount_point_length
) {
476 printf("netboot: path too small %d < %d\n",
477 path_len
, S_netboot_info_p
->mount_point_length
);
480 strcpy(path
, S_netboot_info_p
->mount_point
);
481 strncpy(name
, S_netboot_info_p
->server_name
, name_len
);
482 *server_ip
= S_netboot_info_p
->server_ip
;
488 get_ip_parameters(struct in_addr
* iaddr_p
, struct in_addr
* netmask_p
,
489 struct in_addr
* router_p
)
496 entry
= IOBSDRegistryEntryForDeviceTree("/chosen");
500 pkt
= IOBSDRegistryEntryGetData(entry
, DHCP_RESPONSE
, &pkt_len
);
501 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
502 printf("netboot: retrieving IP information from DHCP response\n");
505 pkt
= IOBSDRegistryEntryGetData(entry
, BOOTP_RESPONSE
, &pkt_len
);
506 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
507 printf("netboot: retrieving IP information from BOOTP response\n");
516 reply
= (struct dhcp
*)pkt
;
517 (void)dhcpol_parse_packet(&options
, reply
, pkt_len
, NULL
);
518 *iaddr_p
= reply
->dp_yiaddr
;
519 ip
= (struct in_addr
*)
520 dhcpol_find(&options
,
521 dhcptag_subnet_mask_e
, &len
, NULL
);
525 ip
= (struct in_addr
*)
526 dhcpol_find(&options
, dhcptag_router_e
, &len
, NULL
);
531 IOBSDRegistryEntryRelease(entry
);
532 return (pkt
!= NULL
);
536 inet_aifaddr(struct socket
* so
, char * name
, const struct in_addr
* addr
,
537 const struct in_addr
* mask
,
538 const struct in_addr
* broadcast
)
540 struct sockaddr blank_sin
;
541 struct ifaliasreq ifra
;
543 bzero(&blank_sin
, sizeof(blank_sin
));
544 blank_sin
.sa_len
= sizeof(blank_sin
);
545 blank_sin
.sa_family
= AF_INET
;
547 bzero(&ifra
, sizeof(ifra
));
548 strncpy(ifra
.ifra_name
, name
, sizeof(ifra
.ifra_name
));
550 ifra
.ifra_addr
= blank_sin
;
551 ((struct sockaddr_in
*)&ifra
.ifra_addr
)->sin_addr
= *addr
;
554 ifra
.ifra_mask
= blank_sin
;
555 ((struct sockaddr_in
*)&ifra
.ifra_mask
)->sin_addr
= *mask
;
558 ifra
.ifra_broadaddr
= blank_sin
;
559 ((struct sockaddr_in
*)&ifra
.ifra_broadaddr
)->sin_addr
= *broadcast
;
561 return (ifioctl(so
, SIOCAIFADDR
, (caddr_t
)&ifra
, current_proc()));
565 route_cmd(int cmd
, struct in_addr d
, struct in_addr g
,
566 struct in_addr m
, u_long more_flags
)
568 struct sockaddr_in dst
;
569 u_long flags
= RTF_UP
| RTF_STATIC
;
570 struct sockaddr_in gw
;
571 struct sockaddr_in mask
;
576 bzero((caddr_t
)&dst
, sizeof(dst
));
577 dst
.sin_len
= sizeof(dst
);
578 dst
.sin_family
= AF_INET
;
582 bzero((caddr_t
)&gw
, sizeof(gw
));
583 gw
.sin_len
= sizeof(gw
);
584 gw
.sin_family
= AF_INET
;
588 bzero(&mask
, sizeof(mask
));
589 mask
.sin_len
= sizeof(mask
);
590 mask
.sin_family
= AF_INET
;
593 return (rtrequest(cmd
, (struct sockaddr
*)&dst
, (struct sockaddr
*)&gw
,
594 (struct sockaddr
*)&mask
, flags
, NULL
));
598 default_route_add(struct in_addr router
, boolean_t proxy_arp
)
601 struct in_addr zeroes
= { 0 };
603 if (proxy_arp
== FALSE
) {
604 flags
|= RTF_GATEWAY
;
606 return (route_cmd(RTM_ADD
, zeroes
, router
, zeroes
, flags
));
610 host_route_delete(struct in_addr host
)
612 struct in_addr zeroes
= { 0 };
614 return (route_cmd(RTM_DELETE
, host
, zeroes
, zeroes
, RTF_HOST
));
617 static struct ifnet
*
620 struct ifnet
* ifp
= NULL
;
623 ifp
= ifunit(rootdevice
);
626 TAILQ_FOREACH(ifp
, &ifnet
, if_link
)
628 (IFF_LOOPBACK
|IFF_POINTOPOINT
)) == 0)
635 netboot_mountroot(void)
638 struct in_addr iaddr
= { 0 };
641 struct in_addr netmask
= { 0 };
642 struct proc
* procp
= current_proc();
643 struct in_addr router
= { 0 };
644 struct socket
* so
= NULL
;
647 bzero(&ifr
, sizeof(ifr
));
649 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
651 /* find the interface */
652 ifp
= find_interface();
654 printf("netboot: no suitable interface\n");
658 sprintf(ifr
.ifr_name
, "%s%d", ifp
->if_name
, ifp
->if_unit
);
659 printf("netboot: using network interface '%s'\n", ifr
.ifr_name
);
662 if ((error
= socreate(AF_INET
, &so
, SOCK_DGRAM
, 0)) != 0) {
663 printf("netboot: socreate, error=%d\n", error
);
666 ifr
.ifr_flags
= ifp
->if_flags
| IFF_UP
;
667 error
= ifioctl(so
, SIOCSIFFLAGS
, (caddr_t
)&ifr
, procp
);
669 printf("netboot: SIFFLAGS, error=%d\n", error
);
673 /* grab information from the registry */
674 if (get_ip_parameters(&iaddr
, &netmask
, &router
) == FALSE
) {
675 /* use BOOTP to retrieve IP address, netmask and router */
676 error
= bootp(ifp
, &iaddr
, 32, &netmask
, &router
, procp
);
678 printf("netboot: BOOTP failed %d\n", error
);
682 printf("netboot: IP address " IP_FORMAT
, IP_LIST(&iaddr
));
683 if (netmask
.s_addr
) {
684 printf(" netmask " IP_FORMAT
, IP_LIST(&netmask
));
687 printf(" router " IP_FORMAT
, IP_LIST(&router
));
690 error
= inet_aifaddr(so
, ifr
.ifr_name
, &iaddr
, &netmask
, NULL
);
692 printf("netboot: inet_aifaddr failed, %d\n", error
);
695 if (router
.s_addr
== 0) {
696 /* enable proxy arp if we don't have a router */
697 router
.s_addr
= iaddr
.s_addr
;
699 printf("netboot: adding default route " IP_FORMAT
"\n",
701 error
= default_route_add(router
, router
.s_addr
== iaddr
.s_addr
);
703 printf("netboot: default_route_add failed %d\n", error
);
707 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
709 S_netboot_info_p
= netboot_info_init(iaddr
);
710 switch (S_netboot_info_p
->image_type
) {
712 case kNetBootImageTypeNFS
:
713 for (try = 1; TRUE
; try++) {
714 error
= nfs_mountroot();
718 printf("netboot: nfs_mountroot() attempt %u failed; "
719 "clearing ARP entry and trying again\n", try);
721 * error is either EHOSTDOWN or EHOSTUNREACH, which likely means
722 * that the port we're plugged into has spanning tree enabled,
723 * and either the router or the server can't answer our ARP
724 * requests. Clear the incomplete ARP entry by removing the
725 * appropriate route, depending on the error code:
726 * EHOSTDOWN NFS server's route
727 * EHOSTUNREACH router's route
733 /* remove the server's arp entry */
734 error
= host_route_delete(S_netboot_info_p
->server_ip
);
736 printf("netboot: host_route_delete(" IP_FORMAT
738 IP_LIST(&S_netboot_info_p
->server_ip
), error
);
742 error
= host_route_delete(router
);
744 printf("netboot: host_route_delete(" IP_FORMAT
745 ") failed %d\n", IP_LIST(&router
), error
);
751 case kNetBootImageTypeHTTP
:
752 error
= netboot_setup(procp
);
766 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
771 netboot_setup(struct proc
* p
)
776 if (S_netboot_info_p
== NULL
777 || S_netboot_info_p
->image_path
== NULL
) {
780 if (S_netboot_info_p
->use_hdix
) {
781 printf("netboot_setup: calling di_root_image\n");
782 error
= di_root_image(S_netboot_info_p
->image_path
,
785 printf("netboot_setup: di_root_image: failed %d\n", error
);
790 printf("netboot_setup: calling vndevice_root_image\n");
791 error
= vndevice_root_image(S_netboot_info_p
->image_path
,
794 printf("netboot_setup: vndevice_root_image: failed %d\n", error
);
800 printf("netboot: root device 0x%x\n", rootdev
);
801 error
= vfs_mountroot();
802 if (error
== 0 && rootvnode
!= NULL
) {
806 /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */
807 if (VFS_ROOT(mountlist
.cqh_last
, &newdp
))
808 panic("netboot_setup: cannot find root vnode");
812 filedesc0
.fd_cdir
= newdp
;
814 simple_lock(&mountlist_slock
);
815 CIRCLEQ_REMOVE(&mountlist
, CIRCLEQ_FIRST(&mountlist
), mnt_list
);
816 simple_unlock(&mountlist_slock
);
817 VOP_UNLOCK(rootvnode
, 0, p
);
818 mountlist
.cqh_first
->mnt_flag
|= MNT_ROOTFS
;
821 netboot_info_free(&S_netboot_info_p
);