2 * Copyright (c) 2001-2004 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>
33 #include <sys/proc_internal.h>
34 #include <sys/mount_internal.h>
36 #include <sys/filedesc.h>
37 #include <sys/vnode_internal.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 <pexpert/pexpert.h>
51 #include <kern/kern_types.h>
52 #include <kern/kalloc.h>
54 //#include <libkern/libkern.h>
55 extern struct filedesc filedesc0
;
57 extern int strncmp(const char *,const char *, size_t);
58 extern unsigned long strtoul(const char *, char **, int);
59 extern char * strchr(const char *str
, int ch
);
61 extern int nfs_mountroot(void); /* nfs_vfsops.c */
62 extern int (*mountroot
)(void);
64 extern unsigned char rootdevice
[];
66 static int S_netboot
= 0;
67 static struct netboot_info
* S_netboot_info_p
;
70 IOBSDRegistryEntryForDeviceTree(const char * path
);
73 IOBSDRegistryEntryRelease(void * entry
);
76 IOBSDRegistryEntryGetData(void * entry
, const char * property_name
,
79 extern int vndevice_root_image(const char * path
, char devname
[],
81 extern int di_root_image(const char *path
, char devname
[], dev_t
*dev_p
);
83 #define BOOTP_RESPONSE "bootp-response"
84 #define BSDP_RESPONSE "bsdp-response"
85 #define DHCP_RESPONSE "dhcp-response"
88 bootp(struct ifnet
* ifp
, struct in_addr
* iaddr_p
, int max_retry
,
89 struct in_addr
* netmask_p
, struct in_addr
* router_p
,
93 /* forward declarations */
94 int inet_aton(char * cp
, struct in_addr
* pin
);
96 boolean_t
netboot_iaddr(struct in_addr
* iaddr_p
);
97 boolean_t
netboot_rootpath(struct in_addr
* server_ip
,
98 char * name
, int name_len
,
99 char * path
, int path_len
);
100 int netboot_setup(struct proc
* p
);
101 int netboot_mountroot(void);
102 int netboot_root(void);
106 #define IP_FORMAT "%d.%d.%d.%d"
107 #define IP_CH(ip) ((u_char *)ip)
108 #define IP_LIST(ip) IP_CH(ip)[0],IP_CH(ip)[1],IP_CH(ip)[2],IP_CH(ip)[3]
110 #define kNetBootRootPathPrefixNFS "nfs:"
111 #define kNetBootRootPathPrefixHTTP "http:"
114 kNetBootImageTypeUnknown
= 0,
115 kNetBootImageTypeNFS
= 1,
116 kNetBootImageTypeHTTP
= 2,
119 struct netboot_info
{
120 struct in_addr client_ip
;
121 struct in_addr server_ip
;
123 int server_name_length
;
125 int mount_point_length
;
127 int image_path_length
;
128 NetBootImageType image_type
;
133 inet_aton(char * cp
, struct in_addr
* pin
)
135 u_char
* b
= (char *)pin
;
139 for (p
= cp
, i
= 0; i
< 4; i
++) {
140 u_long l
= strtoul(p
, 0, 0);
145 if (i
< 3 && p
== NULL
)
153 * Function: parse_booter_path
155 * Parse a string of the form:
156 * "<IP>:<host>:<mount>[:<image_path>]"
157 * into the given ip address, host, mount point, and optionally, image_path.
160 * The passed in string is modified i.e. ':' is replaced by '\0'.
162 * "17.202.16.17:seaport:/release/.images/Image9/CurrentHera"
164 static __inline__ boolean_t
165 parse_booter_path(char * path
, struct in_addr
* iaddr_p
, char * * host
,
166 char * * mount_dir
, char * * image_path
)
173 colon
= strchr(start
, ':');
178 if (inet_aton(start
, iaddr_p
) != 1) {
184 colon
= strchr(start
, ':');
193 colon
= strchr(start
, ':');
208 * Function: find_colon
210 * Find the next unescaped instance of the colon character.
211 * If a colon is escaped (preceded by a backslash '\' character),
212 * shift the string over by one character to overwrite the backslash.
214 static __inline__
char *
215 find_colon(char * str
)
220 while ((colon
= strchr(start
, ':')) != NULL
) {
224 if (colon
== start
) {
227 if (colon
[-1] != '\\')
229 for (dst
= colon
- 1, src
= colon
; *dst
!= '\0'; dst
++, src
++) {
238 * Function: parse_netboot_path
240 * Parse a string of the form:
241 * "nfs:<IP>:<mount>[:<image_path>]"
242 * into the given ip address, host, mount point, and optionally, image_path.
244 * - the passed in string is modified i.e. ':' is replaced by '\0'
245 * - literal colons must be escaped with a backslash
248 * nfs:17.202.42.112:/Library/NetBoot/NetBootSP0:Jaguar/Jaguar.dmg
249 * nfs:17.202.42.112:/Volumes/Foo\:/Library/NetBoot/NetBootSP0:Jaguar/Jaguar.dmg
251 static __inline__ boolean_t
252 parse_netboot_path(char * path
, struct in_addr
* iaddr_p
, char * * host
,
253 char * * mount_dir
, char * * image_path
)
255 static char tmp
[MAX_IPv4_STR_LEN
]; /* Danger - not thread safe */
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_ntop(AF_INET
, iaddr_p
, tmp
, sizeof(tmp
));
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 (vndevice
== NULL
)
360 panic("netboot_info_init: M_NAMEI zone exhausted");
361 if (PE_parse_boot_arg("vndevice", vndevice
) == TRUE
) {
364 FREE_ZONE(vndevice
, MAXPATHLEN
, M_NAMEI
);
366 info
= (struct netboot_info
*)kalloc(sizeof(*info
));
367 bzero(info
, sizeof(*info
));
368 info
->client_ip
= iaddr
;
369 info
->image_type
= kNetBootImageTypeUnknown
;
370 info
->use_hdix
= use_hdix
;
372 /* check for a booter-specified path then a NetBoot path */
373 MALLOC_ZONE(root_path
, caddr_t
, MAXPATHLEN
, M_NAMEI
, M_WAITOK
);
374 if (root_path
== NULL
)
375 panic("netboot_info_init: M_NAMEI zone exhausted");
376 if (PE_parse_boot_arg("rp", root_path
) == TRUE
377 || PE_parse_boot_arg("rootpath", root_path
) == TRUE
378 || get_root_path(root_path
) == TRUE
) {
379 char * server_name
= NULL
;
380 char * mount_point
= NULL
;
381 char * image_path
= NULL
;
382 struct in_addr server_ip
;
384 if (parse_image_path(root_path
, &server_ip
, &server_name
,
385 &mount_point
, &image_path
)) {
386 info
->image_type
= kNetBootImageTypeNFS
;
387 info
->server_ip
= server_ip
;
388 info
->server_name_length
= strlen(server_name
) + 1;
389 info
->server_name
= (char *)kalloc(info
->server_name_length
);
390 info
->mount_point_length
= strlen(mount_point
) + 1;
391 info
->mount_point
= (char *)kalloc(info
->mount_point_length
);
392 strcpy(info
->server_name
, server_name
);
393 strcpy(info
->mount_point
, mount_point
);
395 printf("Server %s Mount %s",
396 server_name
, info
->mount_point
);
397 if (image_path
!= NULL
) {
398 boolean_t needs_slash
= FALSE
;
400 info
->image_path_length
= strlen(image_path
) + 1;
401 if (image_path
[0] != '/') {
403 info
->image_path_length
++;
405 info
->image_path
= (char *)kalloc(info
->image_path_length
);
407 info
->image_path
[0] = '/';
408 strcpy(info
->image_path
+ 1, image_path
);
411 strcpy(info
->image_path
, image_path
);
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 strcpy(info
->image_path
, root_path
);
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 strcpy(path
, S_netboot_info_p
->mount_point
);
485 strncpy(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");
520 reply
= (struct dhcp
*)pkt
;
521 (void)dhcpol_parse_packet(&options
, reply
, pkt_len
, NULL
);
522 *iaddr_p
= reply
->dp_yiaddr
;
523 ip
= (struct in_addr
*)
524 dhcpol_find(&options
,
525 dhcptag_subnet_mask_e
, &len
, NULL
);
529 ip
= (struct in_addr
*)
530 dhcpol_find(&options
, dhcptag_router_e
, &len
, NULL
);
535 IOBSDRegistryEntryRelease(entry
);
536 return (pkt
!= NULL
);
540 inet_aifaddr(struct socket
* so
, char * name
, const struct in_addr
* addr
,
541 const struct in_addr
* mask
,
542 const struct in_addr
* broadcast
)
544 struct sockaddr blank_sin
;
545 struct ifaliasreq ifra
;
547 bzero(&blank_sin
, sizeof(blank_sin
));
548 blank_sin
.sa_len
= sizeof(blank_sin
);
549 blank_sin
.sa_family
= AF_INET
;
551 bzero(&ifra
, sizeof(ifra
));
552 strncpy(ifra
.ifra_name
, name
, sizeof(ifra
.ifra_name
));
554 ifra
.ifra_addr
= blank_sin
;
555 ((struct sockaddr_in
*)&ifra
.ifra_addr
)->sin_addr
= *addr
;
558 ifra
.ifra_mask
= blank_sin
;
559 ((struct sockaddr_in
*)&ifra
.ifra_mask
)->sin_addr
= *mask
;
562 ifra
.ifra_broadaddr
= blank_sin
;
563 ((struct sockaddr_in
*)&ifra
.ifra_broadaddr
)->sin_addr
= *broadcast
;
565 return (ifioctl(so
, SIOCAIFADDR
, (caddr_t
)&ifra
, current_proc()));
569 route_cmd(int cmd
, struct in_addr d
, struct in_addr g
,
570 struct in_addr m
, u_long more_flags
)
572 struct sockaddr_in dst
;
573 u_long flags
= RTF_UP
| RTF_STATIC
;
574 struct sockaddr_in gw
;
575 struct sockaddr_in mask
;
580 bzero((caddr_t
)&dst
, sizeof(dst
));
581 dst
.sin_len
= sizeof(dst
);
582 dst
.sin_family
= AF_INET
;
586 bzero((caddr_t
)&gw
, sizeof(gw
));
587 gw
.sin_len
= sizeof(gw
);
588 gw
.sin_family
= AF_INET
;
592 bzero(&mask
, sizeof(mask
));
593 mask
.sin_len
= sizeof(mask
);
594 mask
.sin_family
= AF_INET
;
597 return (rtrequest(cmd
, (struct sockaddr
*)&dst
, (struct sockaddr
*)&gw
,
598 (struct sockaddr
*)&mask
, flags
, NULL
));
602 default_route_add(struct in_addr router
, boolean_t proxy_arp
)
605 struct in_addr zeroes
= { 0 };
607 if (proxy_arp
== FALSE
) {
608 flags
|= RTF_GATEWAY
;
610 return (route_cmd(RTM_ADD
, zeroes
, router
, zeroes
, flags
));
614 host_route_delete(struct in_addr host
)
616 struct in_addr zeroes
= { 0 };
618 return (route_cmd(RTM_DELETE
, host
, zeroes
, zeroes
, RTF_HOST
));
621 static struct ifnet
*
624 struct ifnet
* ifp
= NULL
;
627 ifp
= ifunit(rootdevice
);
630 ifnet_head_lock_shared();
631 TAILQ_FOREACH(ifp
, &ifnet_head
, if_link
)
632 if ((ifp
->if_flags
& (IFF_LOOPBACK
|IFF_POINTOPOINT
)) == 0)
640 netboot_mountroot(void)
643 struct in_addr iaddr
= { 0 };
646 struct in_addr netmask
= { 0 };
647 struct proc
* procp
= current_proc();
648 struct in_addr router
= { 0 };
649 struct socket
* so
= NULL
;
652 bzero(&ifr
, sizeof(ifr
));
655 /* find the interface */
656 ifp
= find_interface();
658 printf("netboot: no suitable interface\n");
662 sprintf(ifr
.ifr_name
, "%s%d", ifp
->if_name
, ifp
->if_unit
);
663 printf("netboot: using network interface '%s'\n", ifr
.ifr_name
);
666 if ((error
= socreate(AF_INET
, &so
, SOCK_DGRAM
, 0)) != 0) {
667 printf("netboot: socreate, error=%d\n", error
);
670 ifr
.ifr_flags
= ifp
->if_flags
| IFF_UP
;
671 error
= ifioctl(so
, SIOCSIFFLAGS
, (caddr_t
)&ifr
, procp
);
673 printf("netboot: SIFFLAGS, error=%d\n", error
);
677 /* grab information from the registry */
678 if (get_ip_parameters(&iaddr
, &netmask
, &router
) == FALSE
) {
679 /* use BOOTP to retrieve IP address, netmask and router */
680 error
= bootp(ifp
, &iaddr
, 32, &netmask
, &router
, procp
);
682 printf("netboot: BOOTP failed %d\n", error
);
686 printf("netboot: IP address " IP_FORMAT
, IP_LIST(&iaddr
));
687 if (netmask
.s_addr
) {
688 printf(" netmask " IP_FORMAT
, IP_LIST(&netmask
));
691 printf(" router " IP_FORMAT
, IP_LIST(&router
));
694 error
= inet_aifaddr(so
, ifr
.ifr_name
, &iaddr
, &netmask
, NULL
);
696 printf("netboot: inet_aifaddr failed, %d\n", error
);
699 if (router
.s_addr
== 0) {
700 /* enable proxy arp if we don't have a router */
701 router
.s_addr
= iaddr
.s_addr
;
703 printf("netboot: adding default route " IP_FORMAT
"\n",
705 error
= default_route_add(router
, router
.s_addr
== iaddr
.s_addr
);
707 printf("netboot: default_route_add failed %d\n", error
);
712 S_netboot_info_p
= netboot_info_init(iaddr
);
713 switch (S_netboot_info_p
->image_type
) {
715 case kNetBootImageTypeNFS
:
716 for (try = 1; TRUE
; try++) {
717 error
= nfs_mountroot();
721 printf("netboot: nfs_mountroot() attempt %u failed; "
722 "clearing ARP entry and trying again\n", try);
724 * error is either EHOSTDOWN or EHOSTUNREACH, which likely means
725 * that the port we're plugged into has spanning tree enabled,
726 * and either the router or the server can't answer our ARP
727 * requests. Clear the incomplete ARP entry by removing the
728 * appropriate route, depending on the error code:
729 * EHOSTDOWN NFS server's route
730 * EHOSTUNREACH router's route
736 /* remove the server's arp entry */
737 error
= host_route_delete(S_netboot_info_p
->server_ip
);
739 printf("netboot: host_route_delete(" IP_FORMAT
741 IP_LIST(&S_netboot_info_p
->server_ip
), error
);
745 error
= host_route_delete(router
);
747 printf("netboot: host_route_delete(" IP_FORMAT
748 ") failed %d\n", IP_LIST(&router
), error
);
754 case kNetBootImageTypeHTTP
:
755 error
= netboot_setup(procp
);
773 netboot_setup(struct proc
* p
)
778 if (S_netboot_info_p
== NULL
779 || S_netboot_info_p
->image_path
== NULL
) {
782 if (S_netboot_info_p
->use_hdix
) {
783 printf("netboot_setup: calling di_root_image\n");
784 error
= di_root_image(S_netboot_info_p
->image_path
,
787 printf("netboot_setup: di_root_image: failed %d\n", error
);
792 printf("netboot_setup: calling vndevice_root_image\n");
793 error
= vndevice_root_image(S_netboot_info_p
->image_path
,
796 printf("netboot_setup: vndevice_root_image: failed %d\n", error
);
802 printf("netboot: root device 0x%x\n", rootdev
);
803 error
= vfs_mountroot();
804 if (error
== 0 && rootvnode
!= NULL
) {
807 struct vfs_context context
;
810 context
.vc_ucred
= proc_ucred(p
); /* XXX kauth_cred_get() ??? proxy */
812 /* Get the vnode for '/'. Set fdp->fd_fd.fd_cdir to reference it. */
813 if (VFS_ROOT(mountlist
.tqh_last
, &newdp
, &context
))
814 panic("netboot_setup: cannot find root vnode");
819 filedesc0
.fd_cdir
= newdp
;
822 TAILQ_REMOVE(&mountlist
, TAILQ_FIRST(&mountlist
), mnt_list
);
824 mountlist
.tqh_first
->mnt_flag
|= MNT_ROOTFS
;
827 netboot_info_free(&S_netboot_info_p
);