2 * Copyright (c) 2001-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
25 * 14 December, 2001 Dieter Siegmund (dieter@apple.com)
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
32 #include <sys/ioctl.h>
34 #include <sys/mount.h>
36 #include <sys/filedesc.h>
37 #include <sys/vnode.h>
38 #include <sys/malloc.h>
39 #include <sys/socket.h>
40 #include <sys/socketvar.h>
41 #include <sys/reboot.h>
43 #include <net/if_dl.h>
44 #include <net/if_types.h>
45 #include <net/route.h>
46 #include <netinet/in.h>
47 #include <netinet/if_ether.h>
48 #include <netinet/dhcp_options.h>
49 #include <kern/kalloc.h>
50 #include <pexpert/pexpert.h>
52 //#include <libkern/libkern.h>
53 extern struct filedesc filedesc0
;
55 extern int strncmp(const char *,const char *, size_t);
56 extern unsigned long strtoul(const char *, char **, int);
57 extern char * strchr(const char *str
, int ch
);
59 extern int nfs_mountroot(void); /* nfs_vfsops.c */
60 extern int (*mountroot
)(void);
62 extern unsigned char rootdevice
[];
64 static int S_netboot
= 0;
65 static struct netboot_info
* S_netboot_info_p
;
68 IOBSDRegistryEntryForDeviceTree(const char * path
);
71 IOBSDRegistryEntryRelease(void * entry
);
74 IOBSDRegistryEntryGetData(void * entry
, const char * property_name
,
77 extern int vndevice_root_image(const char * path
, char devname
[],
79 extern int di_root_image(const char *path
, char devname
[], dev_t
*dev_p
);
81 #define BOOTP_RESPONSE "bootp-response"
82 #define BSDP_RESPONSE "bsdp-response"
83 #define DHCP_RESPONSE "dhcp-response"
86 bootp(struct ifnet
* ifp
, struct in_addr
* iaddr_p
, int max_retry
,
87 struct in_addr
* netmask_p
, struct in_addr
* router_p
,
91 /* forward declarations */
92 int inet_aton(char * cp
, struct in_addr
* pin
);
94 boolean_t
netboot_iaddr(struct in_addr
* iaddr_p
);
95 boolean_t
netboot_rootpath(struct in_addr
* server_ip
,
96 char * name
, int name_len
,
97 char * path
, int path_len
);
98 int netboot_setup(struct proc
* p
);
99 int netboot_mountroot(void);
100 int netboot_root(void);
104 #define IP_FORMAT "%d.%d.%d.%d"
105 #define IP_CH(ip) ((u_char *)ip)
106 #define IP_LIST(ip) IP_CH(ip)[0],IP_CH(ip)[1],IP_CH(ip)[2],IP_CH(ip)[3]
108 #define kNetBootRootPathPrefixNFS "nfs:"
109 #define kNetBootRootPathPrefixHTTP "http:"
112 kNetBootImageTypeUnknown
= 0,
113 kNetBootImageTypeNFS
= 1,
114 kNetBootImageTypeHTTP
= 2,
117 struct netboot_info
{
118 struct in_addr client_ip
;
119 struct in_addr server_ip
;
121 int server_name_length
;
123 int mount_point_length
;
125 int image_path_length
;
126 NetBootImageType image_type
;
131 inet_aton(char * cp
, struct in_addr
* pin
)
133 u_char
* b
= (char *)pin
;
137 for (p
= cp
, i
= 0; i
< 4; i
++) {
138 u_long l
= strtoul(p
, 0, 0);
143 if (i
< 3 && p
== NULL
)
151 * Function: parse_booter_path
153 * Parse a string of the form:
154 * "<IP>:<host>:<mount>[:<image_path>]"
155 * into the given ip address, host, mount point, and optionally, image_path.
158 * The passed in string is modified i.e. ':' is replaced by '\0'.
160 * "17.202.16.17:seaport:/release/.images/Image9/CurrentHera"
162 static __inline__ boolean_t
163 parse_booter_path(char * path
, struct in_addr
* iaddr_p
, char * * host
,
164 char * * mount_dir
, char * * image_path
)
171 colon
= strchr(start
, ':');
176 if (inet_aton(start
, iaddr_p
) != 1) {
182 colon
= strchr(start
, ':');
191 colon
= strchr(start
, ':');
206 * Function: find_colon
208 * Find the next unescaped instance of the colon character.
209 * If a colon is escaped (preceded by a backslash '\' character),
210 * shift the string over by one character to overwrite the backslash.
212 static __inline__
char *
213 find_colon(char * str
)
218 while ((colon
= strchr(start
, ':')) != NULL
) {
222 if (colon
== start
) {
225 if (colon
[-1] != '\\')
227 for (dst
= colon
- 1, src
= colon
; *dst
!= '\0'; dst
++, src
++) {
236 * Function: parse_netboot_path
238 * Parse a string of the form:
239 * "nfs:<IP>:<mount>[:<image_path>]"
240 * into the given ip address, host, mount point, and optionally, image_path.
242 * - the passed in string is modified i.e. ':' is replaced by '\0'
243 * - literal colons must be escaped with a backslash
246 * nfs:17.202.42.112:/Library/NetBoot/NetBootSP0:Jaguar/Jaguar.dmg
247 * nfs:17.202.42.112:/Volumes/Foo\:/Library/NetBoot/NetBootSP0:Jaguar/Jaguar.dmg
249 static __inline__ boolean_t
250 parse_netboot_path(char * path
, struct in_addr
* iaddr_p
, char * * host
,
251 char * * mount_dir
, char * * image_path
)
256 if (strncmp(path
, kNetBootRootPathPrefixNFS
,
257 strlen(kNetBootRootPathPrefixNFS
)) != 0) {
262 start
= path
+ strlen(kNetBootRootPathPrefixNFS
);
263 colon
= strchr(start
, ':');
268 if (inet_aton(start
, iaddr_p
) != 1) {
274 colon
= find_colon(start
);
283 (void)find_colon(start
);
286 *host
= inet_ntoa(*iaddr_p
);
291 parse_image_path(char * path
, struct in_addr
* iaddr_p
, char * * host
,
292 char * * mount_dir
, char * * image_path
)
294 if (path
[0] >= '0' && path
[0] <= '9') {
295 return (parse_booter_path(path
, iaddr_p
, host
, mount_dir
,
298 return (parse_netboot_path(path
, iaddr_p
, host
, mount_dir
,
303 get_root_path(char * root_path
)
306 boolean_t found
= FALSE
;
310 entry
= IOBSDRegistryEntryForDeviceTree("/chosen");
314 pkt
= IOBSDRegistryEntryGetData(entry
, BSDP_RESPONSE
, &pkt_len
);
315 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
316 printf("netboot: retrieving root path from BSDP response\n");
319 pkt
= IOBSDRegistryEntryGetData(entry
, BOOTP_RESPONSE
,
321 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
322 printf("netboot: retrieving root path from BOOTP response\n");
331 reply
= (struct dhcp
*)pkt
;
332 (void)dhcpol_parse_packet(&options
, reply
, pkt_len
, NULL
);
334 path
= (char *)dhcpol_find(&options
,
335 dhcptag_root_path_e
, &len
, NULL
);
337 bcopy(path
, root_path
, len
);
338 root_path
[len
] = '\0';
342 IOBSDRegistryEntryRelease(entry
);
347 static struct netboot_info
*
348 netboot_info_init(struct in_addr iaddr
)
350 struct netboot_info
* info
;
351 char * root_path
= NULL
;
352 boolean_t use_hdix
= TRUE
;
353 char * vndevice
= NULL
;
355 MALLOC_ZONE(vndevice
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
356 if (PE_parse_boot_arg("vndevice", vndevice
) == TRUE
) {
359 FREE_ZONE(vndevice
, MAXPATHLEN
, M_NAMEI
);
361 info
= (struct netboot_info
*)kalloc(sizeof(*info
));
362 bzero(info
, sizeof(*info
));
363 info
->client_ip
= iaddr
;
364 info
->image_type
= kNetBootImageTypeUnknown
;
365 info
->use_hdix
= use_hdix
;
367 /* check for a booter-specified path then a NetBoot path */
368 MALLOC_ZONE(root_path
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
369 if (PE_parse_boot_arg("rp", root_path
) == TRUE
370 || PE_parse_boot_arg("rootpath", root_path
) == TRUE
371 || get_root_path(root_path
) == TRUE
) {
372 char * server_name
= NULL
;
373 char * mount_point
= NULL
;
374 char * image_path
= NULL
;
375 struct in_addr server_ip
;
377 if (parse_image_path(root_path
, &server_ip
, &server_name
,
378 &mount_point
, &image_path
)) {
379 info
->image_type
= kNetBootImageTypeNFS
;
380 info
->server_ip
= server_ip
;
381 info
->server_name_length
= strlen(server_name
) + 1;
382 info
->server_name
= (char *)kalloc(info
->server_name_length
);
383 info
->mount_point_length
= strlen(mount_point
) + 1;
384 info
->mount_point
= (char *)kalloc(info
->mount_point_length
);
385 strcpy(info
->server_name
, server_name
);
386 strcpy(info
->mount_point
, mount_point
);
388 printf("Server %s Mount %s",
389 server_name
, info
->mount_point
);
390 if (image_path
!= NULL
) {
391 boolean_t needs_slash
= FALSE
;
393 info
->image_path_length
= strlen(image_path
) + 1;
394 if (image_path
[0] != '/') {
396 info
->image_path_length
++;
398 info
->image_path
= (char *)kalloc(info
->image_path_length
);
400 info
->image_path
[0] = '/';
401 strcpy(info
->image_path
+ 1, image_path
);
404 strcpy(info
->image_path
, image_path
);
406 printf(" Image %s", info
->image_path
);
410 else if (strncmp(root_path
, kNetBootRootPathPrefixHTTP
,
411 strlen(kNetBootRootPathPrefixHTTP
)) == 0) {
412 /* only HDIX supports HTTP */
413 info
->image_type
= kNetBootImageTypeHTTP
;
414 info
->use_hdix
= TRUE
;
415 info
->image_path_length
= strlen(root_path
) + 1;
416 info
->image_path
= (char *)kalloc(info
->image_path_length
);
417 strcpy(info
->image_path
, root_path
);
420 printf("netboot: root path uses unrecognized format\n");
423 FREE_ZONE(root_path
, MAXPATHLEN
, M_NAMEI
);
428 netboot_info_free(struct netboot_info
* * info_p
)
430 struct netboot_info
* info
= *info_p
;
433 if (info
->mount_point
) {
434 kfree((vm_offset_t
)info
->mount_point
, info
->mount_point_length
);
436 if (info
->server_name
) {
437 kfree((vm_offset_t
)info
->server_name
, info
->server_name_length
);
439 if (info
->image_path
) {
440 kfree((vm_offset_t
)info
->image_path
, info
->image_path_length
);
442 kfree((vm_offset_t
)info
, sizeof(*info
));
449 netboot_iaddr(struct in_addr
* iaddr_p
)
451 if (S_netboot_info_p
== NULL
)
454 *iaddr_p
= S_netboot_info_p
->client_ip
;
459 netboot_rootpath(struct in_addr
* server_ip
,
460 char * name
, int name_len
,
461 char * path
, int path_len
)
463 if (S_netboot_info_p
== NULL
)
469 if (S_netboot_info_p
->mount_point_length
== 0) {
472 if (path_len
< S_netboot_info_p
->mount_point_length
) {
473 printf("netboot: path too small %d < %d\n",
474 path_len
, S_netboot_info_p
->mount_point_length
);
477 strcpy(path
, S_netboot_info_p
->mount_point
);
478 strncpy(name
, S_netboot_info_p
->server_name
, name_len
);
479 *server_ip
= S_netboot_info_p
->server_ip
;
485 get_ip_parameters(struct in_addr
* iaddr_p
, struct in_addr
* netmask_p
,
486 struct in_addr
* router_p
)
493 entry
= IOBSDRegistryEntryForDeviceTree("/chosen");
497 pkt
= IOBSDRegistryEntryGetData(entry
, DHCP_RESPONSE
, &pkt_len
);
498 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
499 printf("netboot: retrieving IP information from DHCP response\n");
502 pkt
= IOBSDRegistryEntryGetData(entry
, BOOTP_RESPONSE
, &pkt_len
);
503 if (pkt
!= NULL
&& pkt_len
>= (int)sizeof(struct dhcp
)) {
504 printf("netboot: retrieving IP information from BOOTP response\n");
513 reply
= (struct dhcp
*)pkt
;
514 (void)dhcpol_parse_packet(&options
, reply
, pkt_len
, NULL
);
515 *iaddr_p
= reply
->dp_yiaddr
;
516 ip
= (struct in_addr
*)
517 dhcpol_find(&options
,
518 dhcptag_subnet_mask_e
, &len
, NULL
);
522 ip
= (struct in_addr
*)
523 dhcpol_find(&options
, dhcptag_router_e
, &len
, NULL
);
528 IOBSDRegistryEntryRelease(entry
);
529 return (pkt
!= NULL
);
533 inet_aifaddr(struct socket
* so
, char * name
, const struct in_addr
* addr
,
534 const struct in_addr
* mask
,
535 const struct in_addr
* broadcast
)
537 struct sockaddr blank_sin
;
538 struct ifaliasreq ifra
;
540 bzero(&blank_sin
, sizeof(blank_sin
));
541 blank_sin
.sa_len
= sizeof(blank_sin
);
542 blank_sin
.sa_family
= AF_INET
;
544 bzero(&ifra
, sizeof(ifra
));
545 strncpy(ifra
.ifra_name
, name
, sizeof(ifra
.ifra_name
));
547 ifra
.ifra_addr
= blank_sin
;
548 ((struct sockaddr_in
*)&ifra
.ifra_addr
)->sin_addr
= *addr
;
551 ifra
.ifra_mask
= blank_sin
;
552 ((struct sockaddr_in
*)&ifra
.ifra_mask
)->sin_addr
= *mask
;
555 ifra
.ifra_broadaddr
= blank_sin
;
556 ((struct sockaddr_in
*)&ifra
.ifra_broadaddr
)->sin_addr
= *broadcast
;
558 return (ifioctl(so
, SIOCAIFADDR
, (caddr_t
)&ifra
, current_proc()));
562 route_cmd(int cmd
, struct in_addr d
, struct in_addr g
,
563 struct in_addr m
, u_long more_flags
)
565 struct sockaddr_in dst
;
566 u_long flags
= RTF_UP
| RTF_STATIC
;
567 struct sockaddr_in gw
;
568 struct sockaddr_in mask
;
573 bzero((caddr_t
)&dst
, sizeof(dst
));
574 dst
.sin_len
= sizeof(dst
);
575 dst
.sin_family
= AF_INET
;
579 bzero((caddr_t
)&gw
, sizeof(gw
));
580 gw
.sin_len
= sizeof(gw
);
581 gw
.sin_family
= AF_INET
;
585 bzero(&mask
, sizeof(mask
));
586 mask
.sin_len
= sizeof(mask
);
587 mask
.sin_family
= AF_INET
;
590 return (rtrequest(cmd
, (struct sockaddr
*)&dst
, (struct sockaddr
*)&gw
,
591 (struct sockaddr
*)&mask
, flags
, NULL
));
595 default_route_add(struct in_addr router
, boolean_t proxy_arp
)
598 struct in_addr zeroes
= { 0 };
600 if (proxy_arp
== FALSE
) {
601 flags
|= RTF_GATEWAY
;
603 return (route_cmd(RTM_ADD
, zeroes
, router
, zeroes
, flags
));
607 host_route_delete(struct in_addr host
)
609 struct in_addr zeroes
= { 0 };
611 return (route_cmd(RTM_DELETE
, host
, zeroes
, zeroes
, RTF_HOST
));
614 static struct ifnet
*
617 struct ifnet
* ifp
= NULL
;
620 ifp
= ifunit(rootdevice
);
623 TAILQ_FOREACH(ifp
, &ifnet
, if_link
)
625 (IFF_LOOPBACK
|IFF_POINTOPOINT
)) == 0)
632 netboot_mountroot(void)
635 struct in_addr iaddr
= { 0 };
638 struct in_addr netmask
= { 0 };
639 struct proc
* procp
= current_proc();
640 struct in_addr router
= { 0 };
641 struct socket
* so
= NULL
;
644 bzero(&ifr
, sizeof(ifr
));
646 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
648 /* find the interface */
649 ifp
= find_interface();
651 printf("netboot: no suitable interface\n");
655 sprintf(ifr
.ifr_name
, "%s%d", ifp
->if_name
, ifp
->if_unit
);
656 printf("netboot: using network interface '%s'\n", ifr
.ifr_name
);
659 if ((error
= socreate(AF_INET
, &so
, SOCK_DGRAM
, 0)) != 0) {
660 printf("netboot: socreate, error=%d\n", error
);
663 ifr
.ifr_flags
= ifp
->if_flags
| IFF_UP
;
664 error
= ifioctl(so
, SIOCSIFFLAGS
, (caddr_t
)&ifr
, procp
);
666 printf("netboot: SIFFLAGS, error=%d\n", error
);
670 /* grab information from the registry */
671 if (get_ip_parameters(&iaddr
, &netmask
, &router
) == FALSE
) {
672 /* use BOOTP to retrieve IP address, netmask and router */
673 error
= bootp(ifp
, &iaddr
, 32, &netmask
, &router
, procp
);
675 printf("netboot: BOOTP failed %d\n", error
);
679 printf("netboot: IP address " IP_FORMAT
, IP_LIST(&iaddr
));
680 if (netmask
.s_addr
) {
681 printf(" netmask " IP_FORMAT
, IP_LIST(&netmask
));
684 printf(" router " IP_FORMAT
, IP_LIST(&router
));
687 error
= inet_aifaddr(so
, ifr
.ifr_name
, &iaddr
, &netmask
, NULL
);
689 printf("netboot: inet_aifaddr failed, %d\n", error
);
692 if (router
.s_addr
== 0) {
693 /* enable proxy arp if we don't have a router */
694 router
.s_addr
= iaddr
.s_addr
;
696 printf("netboot: adding default route " IP_FORMAT
"\n",
698 error
= default_route_add(router
, router
.s_addr
== iaddr
.s_addr
);
700 printf("netboot: default_route_add failed %d\n", error
);
704 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
706 S_netboot_info_p
= netboot_info_init(iaddr
);
707 switch (S_netboot_info_p
->image_type
) {
709 case kNetBootImageTypeNFS
:
710 for (try = 1; TRUE
; try++) {
711 error
= nfs_mountroot();
715 printf("netboot: nfs_mountroot() attempt %u failed; "
716 "clearing ARP entry and trying again\n", try);
718 * error is either EHOSTDOWN or EHOSTUNREACH, which likely means
719 * that the port we're plugged into has spanning tree enabled,
720 * and either the router or the server can't answer our ARP
721 * requests. Clear the incomplete ARP entry by removing the
722 * appropriate route, depending on the error code:
723 * EHOSTDOWN NFS server's route
724 * EHOSTUNREACH router's route
730 /* remove the server's arp entry */
731 error
= host_route_delete(S_netboot_info_p
->server_ip
);
733 printf("netboot: host_route_delete(" IP_FORMAT
735 IP_LIST(&S_netboot_info_p
->server_ip
), error
);
739 error
= host_route_delete(router
);
741 printf("netboot: host_route_delete(" IP_FORMAT
742 ") failed %d\n", IP_LIST(&router
), error
);
748 case kNetBootImageTypeHTTP
:
749 error
= netboot_setup(procp
);
763 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
768 netboot_setup(struct proc
* p
)
773 if (S_netboot_info_p
== NULL
774 || S_netboot_info_p
->image_path
== NULL
) {
777 if (S_netboot_info_p
->use_hdix
) {
778 printf("netboot_setup: calling di_root_image\n");
779 error
= di_root_image(S_netboot_info_p
->image_path
,
782 printf("netboot_setup: di_root_image: failed %d\n", error
);
787 printf("netboot_setup: calling vndevice_root_image\n");
788 error
= vndevice_root_image(S_netboot_info_p
->image_path
,
791 printf("netboot_setup: vndevice_root_image: failed %d\n", error
);
797 printf("netboot: root device 0x%x\n", rootdev
);
798 error
= vfs_mountroot();
799 if (error
== 0 && rootvnode
!= NULL
) {
803 /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */
804 if (VFS_ROOT(mountlist
.cqh_last
, &newdp
))
805 panic("netboot_setup: cannot find root vnode");
809 filedesc0
.fd_cdir
= newdp
;
811 simple_lock(&mountlist_slock
);
812 CIRCLEQ_REMOVE(&mountlist
, CIRCLEQ_FIRST(&mountlist
), mnt_list
);
813 simple_unlock(&mountlist_slock
);
814 VOP_UNLOCK(rootvnode
, 0, p
);
815 mountlist
.cqh_first
->mnt_flag
|= MNT_ROOTFS
;
818 netboot_info_free(&S_netboot_info_p
);