]>
Commit | Line | Data |
---|---|---|
9bccf70c | 1 | /* |
6d2010ae | 2 | * Copyright (c) 2001-2010 Apple Inc. All rights reserved. |
9bccf70c | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
9bccf70c | 5 | * |
2d21ac55 A |
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 License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
9bccf70c A |
27 | */ |
28 | ||
29 | /* | |
30 | * History: | |
31 | * 14 December, 2001 Dieter Siegmund (dieter@apple.com) | |
32 | * - created | |
33 | */ | |
34 | #include <sys/param.h> | |
35 | #include <sys/systm.h> | |
36 | #include <sys/kernel.h> | |
37 | #include <sys/conf.h> | |
38 | #include <sys/ioctl.h> | |
91447636 A |
39 | #include <sys/proc_internal.h> |
40 | #include <sys/mount_internal.h> | |
9bccf70c A |
41 | #include <sys/mbuf.h> |
42 | #include <sys/filedesc.h> | |
91447636 | 43 | #include <sys/vnode_internal.h> |
9bccf70c A |
44 | #include <sys/malloc.h> |
45 | #include <sys/socket.h> | |
4a249263 | 46 | #include <sys/socketvar.h> |
9bccf70c | 47 | #include <sys/reboot.h> |
2d21ac55 | 48 | #include <sys/kauth.h> |
9bccf70c A |
49 | #include <net/if.h> |
50 | #include <net/if_dl.h> | |
51 | #include <net/if_types.h> | |
52 | #include <net/route.h> | |
53 | #include <netinet/in.h> | |
54 | #include <netinet/if_ether.h> | |
55 | #include <netinet/dhcp_options.h> | |
2d21ac55 | 56 | #include <netinet/in_dhcp.h> |
9bccf70c | 57 | |
91447636 A |
58 | #include <kern/kern_types.h> |
59 | #include <kern/kalloc.h> | |
6d2010ae A |
60 | #include <sys/netboot.h> |
61 | #include <sys/imageboot.h> | |
2d21ac55 A |
62 | #include <pexpert/pexpert.h> |
63 | ||
9bccf70c | 64 | //#include <libkern/libkern.h> |
9bccf70c A |
65 | extern struct filedesc filedesc0; |
66 | ||
4a249263 | 67 | extern int nfs_mountroot(void); /* nfs_vfsops.c */ |
9bccf70c A |
68 | extern int (*mountroot)(void); |
69 | ||
70 | extern unsigned char rootdevice[]; | |
71 | ||
72 | static int S_netboot = 0; | |
73 | static struct netboot_info * S_netboot_info_p; | |
74 | ||
75 | void * | |
4a249263 | 76 | IOBSDRegistryEntryForDeviceTree(const char * path); |
9bccf70c A |
77 | |
78 | void | |
79 | IOBSDRegistryEntryRelease(void * entry); | |
80 | ||
81 | const void * | |
4a249263 | 82 | IOBSDRegistryEntryGetData(void * entry, const char * property_name, |
9bccf70c A |
83 | int * packet_length); |
84 | ||
9bccf70c A |
85 | #define BOOTP_RESPONSE "bootp-response" |
86 | #define BSDP_RESPONSE "bsdp-response" | |
87 | #define DHCP_RESPONSE "dhcp-response" | |
88 | ||
4a249263 A |
89 | /* forward declarations */ |
90 | int inet_aton(char * cp, struct in_addr * pin); | |
91 | ||
9bccf70c A |
92 | #define IP_FORMAT "%d.%d.%d.%d" |
93 | #define IP_CH(ip) ((u_char *)ip) | |
94 | #define IP_LIST(ip) IP_CH(ip)[0],IP_CH(ip)[1],IP_CH(ip)[2],IP_CH(ip)[3] | |
95 | ||
96 | #define kNetBootRootPathPrefixNFS "nfs:" | |
97 | #define kNetBootRootPathPrefixHTTP "http:" | |
98 | ||
99 | typedef enum { | |
100 | kNetBootImageTypeUnknown = 0, | |
101 | kNetBootImageTypeNFS = 1, | |
102 | kNetBootImageTypeHTTP = 2, | |
103 | } NetBootImageType; | |
104 | ||
105 | struct netboot_info { | |
106 | struct in_addr client_ip; | |
107 | struct in_addr server_ip; | |
108 | char * server_name; | |
109 | int server_name_length; | |
110 | char * mount_point; | |
111 | int mount_point_length; | |
112 | char * image_path; | |
113 | int image_path_length; | |
114 | NetBootImageType image_type; | |
6d2010ae A |
115 | char * second_image_path; |
116 | int second_image_path_length; | |
9bccf70c A |
117 | }; |
118 | ||
9bccf70c A |
119 | /* |
120 | * Function: parse_booter_path | |
121 | * Purpose: | |
122 | * Parse a string of the form: | |
123 | * "<IP>:<host>:<mount>[:<image_path>]" | |
124 | * into the given ip address, host, mount point, and optionally, image_path. | |
125 | * | |
126 | * Note: | |
127 | * The passed in string is modified i.e. ':' is replaced by '\0'. | |
128 | * Example: | |
129 | * "17.202.16.17:seaport:/release/.images/Image9/CurrentHera" | |
130 | */ | |
131 | static __inline__ boolean_t | |
2d21ac55 | 132 | parse_booter_path(char * path, struct in_addr * iaddr_p, char const * * host, |
9bccf70c A |
133 | char * * mount_dir, char * * image_path) |
134 | { | |
135 | char * start; | |
136 | char * colon; | |
137 | ||
138 | /* IP address */ | |
139 | start = path; | |
140 | colon = strchr(start, ':'); | |
141 | if (colon == NULL) { | |
142 | return (FALSE); | |
143 | } | |
144 | *colon = '\0'; | |
145 | if (inet_aton(start, iaddr_p) != 1) { | |
146 | return (FALSE); | |
147 | } | |
148 | ||
149 | /* host */ | |
150 | start = colon + 1; | |
151 | colon = strchr(start, ':'); | |
152 | if (colon == NULL) { | |
153 | return (FALSE); | |
154 | } | |
155 | *colon = '\0'; | |
156 | *host = start; | |
157 | ||
158 | /* mount */ | |
159 | start = colon + 1; | |
160 | colon = strchr(start, ':'); | |
161 | *mount_dir = start; | |
162 | if (colon == NULL) { | |
163 | *image_path = NULL; | |
164 | } | |
165 | else { | |
166 | /* image path */ | |
167 | *colon = '\0'; | |
168 | start = colon + 1; | |
169 | *image_path = start; | |
170 | } | |
171 | return (TRUE); | |
172 | } | |
173 | ||
174 | /* | |
175 | * Function: find_colon | |
176 | * Purpose: | |
177 | * Find the next unescaped instance of the colon character. | |
178 | * If a colon is escaped (preceded by a backslash '\' character), | |
179 | * shift the string over by one character to overwrite the backslash. | |
180 | */ | |
181 | static __inline__ char * | |
182 | find_colon(char * str) | |
183 | { | |
184 | char * start = str; | |
185 | char * colon; | |
186 | ||
187 | while ((colon = strchr(start, ':')) != NULL) { | |
188 | char * dst; | |
189 | char * src; | |
190 | ||
191 | if (colon == start) { | |
192 | break; | |
193 | } | |
194 | if (colon[-1] != '\\') | |
195 | break; | |
196 | for (dst = colon - 1, src = colon; *dst != '\0'; dst++, src++) { | |
197 | *dst = *src; | |
198 | } | |
199 | start = colon; | |
200 | } | |
201 | return (colon); | |
202 | } | |
203 | ||
204 | /* | |
205 | * Function: parse_netboot_path | |
206 | * Purpose: | |
207 | * Parse a string of the form: | |
208 | * "nfs:<IP>:<mount>[:<image_path>]" | |
209 | * into the given ip address, host, mount point, and optionally, image_path. | |
210 | * Notes: | |
211 | * - the passed in string is modified i.e. ':' is replaced by '\0' | |
212 | * - literal colons must be escaped with a backslash | |
213 | * | |
214 | * Examples: | |
215 | * nfs:17.202.42.112:/Library/NetBoot/NetBootSP0:Jaguar/Jaguar.dmg | |
216 | * nfs:17.202.42.112:/Volumes/Foo\:/Library/NetBoot/NetBootSP0:Jaguar/Jaguar.dmg | |
217 | */ | |
218 | static __inline__ boolean_t | |
2d21ac55 | 219 | parse_netboot_path(char * path, struct in_addr * iaddr_p, char const * * host, |
9bccf70c A |
220 | char * * mount_dir, char * * image_path) |
221 | { | |
6d2010ae | 222 | static char tmp[MAX_IPv4_STR_LEN]; /* Danger - not thread safe */ |
9bccf70c A |
223 | char * start; |
224 | char * colon; | |
225 | ||
226 | if (strncmp(path, kNetBootRootPathPrefixNFS, | |
227 | strlen(kNetBootRootPathPrefixNFS)) != 0) { | |
228 | return (FALSE); | |
229 | } | |
230 | ||
231 | /* IP address */ | |
232 | start = path + strlen(kNetBootRootPathPrefixNFS); | |
233 | colon = strchr(start, ':'); | |
234 | if (colon == NULL) { | |
235 | return (FALSE); | |
236 | } | |
237 | *colon = '\0'; | |
238 | if (inet_aton(start, iaddr_p) != 1) { | |
239 | return (FALSE); | |
240 | } | |
241 | ||
242 | /* mount point */ | |
243 | start = colon + 1; | |
244 | colon = find_colon(start); | |
245 | *mount_dir = start; | |
246 | if (colon == NULL) { | |
247 | *image_path = NULL; | |
248 | } | |
249 | else { | |
250 | /* image path */ | |
251 | *colon = '\0'; | |
252 | start = colon + 1; | |
253 | (void)find_colon(start); | |
254 | *image_path = start; | |
255 | } | |
91447636 | 256 | *host = inet_ntop(AF_INET, iaddr_p, tmp, sizeof(tmp)); |
9bccf70c A |
257 | return (TRUE); |
258 | } | |
259 | ||
260 | static boolean_t | |
2d21ac55 | 261 | parse_image_path(char * path, struct in_addr * iaddr_p, char const * * host, |
9bccf70c A |
262 | char * * mount_dir, char * * image_path) |
263 | { | |
264 | if (path[0] >= '0' && path[0] <= '9') { | |
265 | return (parse_booter_path(path, iaddr_p, host, mount_dir, | |
266 | image_path)); | |
267 | } | |
268 | return (parse_netboot_path(path, iaddr_p, host, mount_dir, | |
269 | image_path)); | |
270 | } | |
271 | ||
272 | static boolean_t | |
273 | get_root_path(char * root_path) | |
274 | { | |
275 | void * entry; | |
276 | boolean_t found = FALSE; | |
277 | const void * pkt; | |
278 | int pkt_len; | |
279 | ||
280 | entry = IOBSDRegistryEntryForDeviceTree("/chosen"); | |
281 | if (entry == NULL) { | |
282 | return (FALSE); | |
283 | } | |
284 | pkt = IOBSDRegistryEntryGetData(entry, BSDP_RESPONSE, &pkt_len); | |
4a249263 | 285 | if (pkt != NULL && pkt_len >= (int)sizeof(struct dhcp)) { |
9bccf70c A |
286 | printf("netboot: retrieving root path from BSDP response\n"); |
287 | } | |
288 | else { | |
289 | pkt = IOBSDRegistryEntryGetData(entry, BOOTP_RESPONSE, | |
290 | &pkt_len); | |
4a249263 | 291 | if (pkt != NULL && pkt_len >= (int)sizeof(struct dhcp)) { |
9bccf70c A |
292 | printf("netboot: retrieving root path from BOOTP response\n"); |
293 | } | |
294 | } | |
295 | if (pkt != NULL) { | |
296 | int len; | |
297 | dhcpol_t options; | |
2d21ac55 A |
298 | const char * path; |
299 | const struct dhcp * reply; | |
9bccf70c | 300 | |
2d21ac55 A |
301 | reply = (const struct dhcp *)pkt; |
302 | (void)dhcpol_parse_packet(&options, reply, pkt_len); | |
9bccf70c | 303 | |
2d21ac55 A |
304 | path = (const char *)dhcpol_find(&options, |
305 | dhcptag_root_path_e, &len, NULL); | |
9bccf70c | 306 | if (path) { |
2d21ac55 | 307 | memcpy(root_path, path, len); |
9bccf70c A |
308 | root_path[len] = '\0'; |
309 | found = TRUE; | |
310 | } | |
311 | } | |
312 | IOBSDRegistryEntryRelease(entry); | |
313 | return (found); | |
314 | ||
315 | } | |
316 | ||
6d2010ae A |
317 | static void |
318 | save_path(char * * str_p, int * length_p, char * path) | |
319 | { | |
320 | *length_p = strlen(path) + 1; | |
321 | *str_p = (char *)kalloc(*length_p); | |
322 | strlcpy(*str_p, path, *length_p); | |
323 | return; | |
324 | } | |
325 | ||
9bccf70c A |
326 | static struct netboot_info * |
327 | netboot_info_init(struct in_addr iaddr) | |
328 | { | |
6d2010ae A |
329 | boolean_t have_root_path = FALSE; |
330 | struct netboot_info * info = NULL; | |
9bccf70c | 331 | char * root_path = NULL; |
9bccf70c A |
332 | |
333 | info = (struct netboot_info *)kalloc(sizeof(*info)); | |
334 | bzero(info, sizeof(*info)); | |
335 | info->client_ip = iaddr; | |
336 | info->image_type = kNetBootImageTypeUnknown; | |
9bccf70c A |
337 | |
338 | /* check for a booter-specified path then a NetBoot path */ | |
339 | MALLOC_ZONE(root_path, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK); | |
91447636 A |
340 | if (root_path == NULL) |
341 | panic("netboot_info_init: M_NAMEI zone exhausted"); | |
6d2010ae A |
342 | if (PE_parse_boot_argn("rp0", root_path, MAXPATHLEN) == TRUE |
343 | || PE_parse_boot_argn("rp", root_path, MAXPATHLEN) == TRUE | |
344 | || PE_parse_boot_argn("rootpath", root_path, MAXPATHLEN) == TRUE) { | |
345 | if (imageboot_format_is_valid(root_path)) { | |
346 | printf("netboot_info_init: rp0='%s' isn't a network path," | |
347 | " ignoring\n", root_path); | |
348 | } | |
349 | else { | |
350 | have_root_path = TRUE; | |
351 | } | |
352 | } | |
353 | if (have_root_path == FALSE) { | |
354 | have_root_path = get_root_path(root_path); | |
355 | } | |
356 | if (have_root_path) { | |
2d21ac55 | 357 | const char * server_name = NULL; |
9bccf70c A |
358 | char * mount_point = NULL; |
359 | char * image_path = NULL; | |
360 | struct in_addr server_ip; | |
361 | ||
362 | if (parse_image_path(root_path, &server_ip, &server_name, | |
363 | &mount_point, &image_path)) { | |
364 | info->image_type = kNetBootImageTypeNFS; | |
365 | info->server_ip = server_ip; | |
366 | info->server_name_length = strlen(server_name) + 1; | |
367 | info->server_name = (char *)kalloc(info->server_name_length); | |
368 | info->mount_point_length = strlen(mount_point) + 1; | |
369 | info->mount_point = (char *)kalloc(info->mount_point_length); | |
2d21ac55 A |
370 | strlcpy(info->server_name, server_name, info->server_name_length); |
371 | strlcpy(info->mount_point, mount_point, info->mount_point_length); | |
9bccf70c | 372 | |
6d2010ae | 373 | printf("netboot: NFS Server %s Mount %s", |
9bccf70c A |
374 | server_name, info->mount_point); |
375 | if (image_path != NULL) { | |
4a249263 | 376 | boolean_t needs_slash = FALSE; |
6d2010ae | 377 | |
9bccf70c A |
378 | info->image_path_length = strlen(image_path) + 1; |
379 | if (image_path[0] != '/') { | |
380 | needs_slash = TRUE; | |
381 | info->image_path_length++; | |
382 | } | |
383 | info->image_path = (char *)kalloc(info->image_path_length); | |
384 | if (needs_slash) { | |
2d21ac55 A |
385 | info->image_path[0] = '/'; |
386 | strlcpy(info->image_path + 1, image_path, | |
387 | info->image_path_length - 1); | |
388 | } else { | |
389 | strlcpy(info->image_path, image_path, | |
390 | info->image_path_length); | |
9bccf70c A |
391 | } |
392 | printf(" Image %s", info->image_path); | |
393 | } | |
394 | printf("\n"); | |
395 | } | |
396 | else if (strncmp(root_path, kNetBootRootPathPrefixHTTP, | |
397 | strlen(kNetBootRootPathPrefixHTTP)) == 0) { | |
9bccf70c | 398 | info->image_type = kNetBootImageTypeHTTP; |
6d2010ae A |
399 | save_path(&info->image_path, &info->image_path_length, |
400 | root_path); | |
401 | printf("netboot: HTTP URL %s\n", info->image_path); | |
9bccf70c A |
402 | } |
403 | else { | |
404 | printf("netboot: root path uses unrecognized format\n"); | |
405 | } | |
6d2010ae A |
406 | |
407 | /* check for image-within-image */ | |
408 | if (info->image_path != NULL) { | |
409 | if (PE_parse_boot_argn(IMAGEBOOT_ROOT_ARG, root_path, MAXPATHLEN) | |
410 | || PE_parse_boot_argn("rp1", root_path, MAXPATHLEN)) { | |
411 | /* rp1/root-dmg is the second-level image */ | |
412 | save_path(&info->second_image_path, &info->second_image_path_length, | |
413 | root_path); | |
414 | } | |
415 | } | |
416 | if (info->second_image_path != NULL) { | |
417 | printf("netboot: nested image %s\n", info->second_image_path); | |
418 | } | |
9bccf70c | 419 | } |
55e303ae | 420 | FREE_ZONE(root_path, MAXPATHLEN, M_NAMEI); |
9bccf70c A |
421 | return (info); |
422 | } | |
423 | ||
424 | static void | |
425 | netboot_info_free(struct netboot_info * * info_p) | |
426 | { | |
427 | struct netboot_info * info = *info_p; | |
428 | ||
429 | if (info) { | |
430 | if (info->mount_point) { | |
91447636 | 431 | kfree(info->mount_point, info->mount_point_length); |
9bccf70c A |
432 | } |
433 | if (info->server_name) { | |
91447636 | 434 | kfree(info->server_name, info->server_name_length); |
9bccf70c A |
435 | } |
436 | if (info->image_path) { | |
91447636 | 437 | kfree(info->image_path, info->image_path_length); |
9bccf70c | 438 | } |
6d2010ae A |
439 | if (info->second_image_path) { |
440 | kfree(info->second_image_path, info->second_image_path_length); | |
441 | } | |
91447636 | 442 | kfree(info, sizeof(*info)); |
9bccf70c A |
443 | } |
444 | *info_p = NULL; | |
445 | return; | |
446 | } | |
447 | ||
448 | boolean_t | |
449 | netboot_iaddr(struct in_addr * iaddr_p) | |
450 | { | |
451 | if (S_netboot_info_p == NULL) | |
452 | return (FALSE); | |
453 | ||
454 | *iaddr_p = S_netboot_info_p->client_ip; | |
455 | return (TRUE); | |
456 | } | |
457 | ||
458 | boolean_t | |
459 | netboot_rootpath(struct in_addr * server_ip, | |
460 | char * name, int name_len, | |
461 | char * path, int path_len) | |
462 | { | |
463 | if (S_netboot_info_p == NULL) | |
464 | return (FALSE); | |
465 | ||
466 | name[0] = '\0'; | |
467 | path[0] = '\0'; | |
468 | ||
469 | if (S_netboot_info_p->mount_point_length == 0) { | |
470 | return (FALSE); | |
471 | } | |
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); | |
475 | return (FALSE); | |
476 | } | |
2d21ac55 A |
477 | strlcpy(path, S_netboot_info_p->mount_point, path_len); |
478 | strlcpy(name, S_netboot_info_p->server_name, name_len); | |
9bccf70c A |
479 | *server_ip = S_netboot_info_p->server_ip; |
480 | return (TRUE); | |
481 | } | |
482 | ||
483 | ||
484 | static boolean_t | |
485 | get_ip_parameters(struct in_addr * iaddr_p, struct in_addr * netmask_p, | |
486 | struct in_addr * router_p) | |
487 | { | |
488 | void * entry; | |
489 | const void * pkt; | |
490 | int pkt_len; | |
491 | ||
492 | ||
493 | entry = IOBSDRegistryEntryForDeviceTree("/chosen"); | |
494 | if (entry == NULL) { | |
495 | return (FALSE); | |
496 | } | |
497 | pkt = IOBSDRegistryEntryGetData(entry, DHCP_RESPONSE, &pkt_len); | |
4a249263 | 498 | if (pkt != NULL && pkt_len >= (int)sizeof(struct dhcp)) { |
9bccf70c A |
499 | printf("netboot: retrieving IP information from DHCP response\n"); |
500 | } | |
501 | else { | |
502 | pkt = IOBSDRegistryEntryGetData(entry, BOOTP_RESPONSE, &pkt_len); | |
4a249263 | 503 | if (pkt != NULL && pkt_len >= (int)sizeof(struct dhcp)) { |
9bccf70c A |
504 | printf("netboot: retrieving IP information from BOOTP response\n"); |
505 | } | |
506 | } | |
507 | if (pkt != NULL) { | |
2d21ac55 | 508 | const struct in_addr * ip; |
9bccf70c A |
509 | int len; |
510 | dhcpol_t options; | |
2d21ac55 | 511 | const struct dhcp * reply; |
9bccf70c | 512 | |
2d21ac55 A |
513 | reply = (const struct dhcp *)pkt; |
514 | (void)dhcpol_parse_packet(&options, reply, pkt_len); | |
9bccf70c | 515 | *iaddr_p = reply->dp_yiaddr; |
2d21ac55 | 516 | ip = (const struct in_addr *) |
9bccf70c A |
517 | dhcpol_find(&options, |
518 | dhcptag_subnet_mask_e, &len, NULL); | |
519 | if (ip) { | |
520 | *netmask_p = *ip; | |
521 | } | |
2d21ac55 | 522 | ip = (const struct in_addr *) |
9bccf70c A |
523 | dhcpol_find(&options, dhcptag_router_e, &len, NULL); |
524 | if (ip) { | |
525 | *router_p = *ip; | |
526 | } | |
527 | } | |
528 | IOBSDRegistryEntryRelease(entry); | |
529 | return (pkt != NULL); | |
530 | } | |
531 | ||
9bccf70c | 532 | static int |
4a249263 | 533 | route_cmd(int cmd, struct in_addr d, struct in_addr g, |
b0d623f7 | 534 | struct in_addr m, uint32_t more_flags, unsigned int ifscope) |
9bccf70c A |
535 | { |
536 | struct sockaddr_in dst; | |
b0d623f7 A |
537 | int error; |
538 | uint32_t flags = RTF_UP | RTF_STATIC; | |
9bccf70c A |
539 | struct sockaddr_in gw; |
540 | struct sockaddr_in mask; | |
541 | ||
4a249263 | 542 | flags |= more_flags; |
9bccf70c | 543 | |
4a249263 | 544 | /* destination */ |
9bccf70c A |
545 | bzero((caddr_t)&dst, sizeof(dst)); |
546 | dst.sin_len = sizeof(dst); | |
547 | dst.sin_family = AF_INET; | |
4a249263 | 548 | dst.sin_addr = d; |
9bccf70c A |
549 | |
550 | /* gateway */ | |
551 | bzero((caddr_t)&gw, sizeof(gw)); | |
552 | gw.sin_len = sizeof(gw); | |
553 | gw.sin_family = AF_INET; | |
4a249263 | 554 | gw.sin_addr = g; |
9bccf70c | 555 | |
4a249263 | 556 | /* mask */ |
9bccf70c A |
557 | bzero(&mask, sizeof(mask)); |
558 | mask.sin_len = sizeof(mask); | |
559 | mask.sin_family = AF_INET; | |
4a249263 | 560 | mask.sin_addr = m; |
6d2010ae A |
561 | |
562 | error = rtrequest_scoped(cmd, (struct sockaddr *)&dst, | |
563 | (struct sockaddr *)&gw, (struct sockaddr *)&mask, flags, NULL, ifscope); | |
564 | ||
b0d623f7 | 565 | return (error); |
9bccf70c | 566 | |
9bccf70c A |
567 | } |
568 | ||
4a249263 A |
569 | static int |
570 | default_route_add(struct in_addr router, boolean_t proxy_arp) | |
571 | { | |
b0d623f7 | 572 | uint32_t flags = 0; |
4a249263 A |
573 | struct in_addr zeroes = { 0 }; |
574 | ||
575 | if (proxy_arp == FALSE) { | |
576 | flags |= RTF_GATEWAY; | |
577 | } | |
b0d623f7 | 578 | return (route_cmd(RTM_ADD, zeroes, router, zeroes, flags, IFSCOPE_NONE)); |
4a249263 A |
579 | } |
580 | ||
581 | static int | |
b0d623f7 | 582 | host_route_delete(struct in_addr host, unsigned int ifscope) |
4a249263 A |
583 | { |
584 | struct in_addr zeroes = { 0 }; | |
585 | ||
b0d623f7 | 586 | return (route_cmd(RTM_DELETE, host, zeroes, zeroes, RTF_HOST, ifscope)); |
4a249263 A |
587 | } |
588 | ||
9bccf70c | 589 | static struct ifnet * |
4a249263 | 590 | find_interface(void) |
9bccf70c A |
591 | { |
592 | struct ifnet * ifp = NULL; | |
593 | ||
594 | if (rootdevice[0]) { | |
2d21ac55 | 595 | ifp = ifunit((char *)rootdevice); |
9bccf70c A |
596 | } |
597 | if (ifp == NULL) { | |
91447636 A |
598 | ifnet_head_lock_shared(); |
599 | TAILQ_FOREACH(ifp, &ifnet_head, if_link) | |
600 | if ((ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0) | |
601 | break; | |
602 | ifnet_head_done(); | |
9bccf70c A |
603 | } |
604 | return (ifp); | |
605 | } | |
606 | ||
607 | int | |
4a249263 | 608 | netboot_mountroot(void) |
9bccf70c A |
609 | { |
610 | int error = 0; | |
611 | struct in_addr iaddr = { 0 }; | |
612 | struct ifreq ifr; | |
613 | struct ifnet * ifp; | |
614 | struct in_addr netmask = { 0 }; | |
2d21ac55 | 615 | proc_t procp = current_proc(); |
9bccf70c A |
616 | struct in_addr router = { 0 }; |
617 | struct socket * so = NULL; | |
4a249263 | 618 | unsigned int try; |
9bccf70c A |
619 | |
620 | bzero(&ifr, sizeof(ifr)); | |
621 | ||
9bccf70c A |
622 | /* find the interface */ |
623 | ifp = find_interface(); | |
624 | if (ifp == NULL) { | |
625 | printf("netboot: no suitable interface\n"); | |
626 | error = ENXIO; | |
627 | goto failed; | |
628 | } | |
2d21ac55 A |
629 | snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d", ifp->if_name, |
630 | ifp->if_unit); | |
9bccf70c A |
631 | printf("netboot: using network interface '%s'\n", ifr.ifr_name); |
632 | ||
633 | /* bring it up */ | |
634 | if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0)) != 0) { | |
635 | printf("netboot: socreate, error=%d\n", error); | |
636 | goto failed; | |
637 | } | |
638 | ifr.ifr_flags = ifp->if_flags | IFF_UP; | |
639 | error = ifioctl(so, SIOCSIFFLAGS, (caddr_t)&ifr, procp); | |
640 | if (error) { | |
641 | printf("netboot: SIFFLAGS, error=%d\n", error); | |
642 | goto failed; | |
643 | } | |
644 | ||
645 | /* grab information from the registry */ | |
646 | if (get_ip_parameters(&iaddr, &netmask, &router) == FALSE) { | |
2d21ac55 A |
647 | /* use DHCP to retrieve IP address, netmask and router */ |
648 | error = dhcp(ifp, &iaddr, 64, &netmask, &router, procp); | |
9bccf70c | 649 | if (error) { |
2d21ac55 | 650 | printf("netboot: DHCP failed %d\n", error); |
9bccf70c A |
651 | goto failed; |
652 | } | |
653 | } | |
654 | printf("netboot: IP address " IP_FORMAT, IP_LIST(&iaddr)); | |
655 | if (netmask.s_addr) { | |
656 | printf(" netmask " IP_FORMAT, IP_LIST(&netmask)); | |
657 | } | |
658 | if (router.s_addr) { | |
659 | printf(" router " IP_FORMAT, IP_LIST(&router)); | |
660 | } | |
661 | printf("\n"); | |
662 | error = inet_aifaddr(so, ifr.ifr_name, &iaddr, &netmask, NULL); | |
663 | if (error) { | |
664 | printf("netboot: inet_aifaddr failed, %d\n", error); | |
665 | goto failed; | |
666 | } | |
667 | if (router.s_addr == 0) { | |
668 | /* enable proxy arp if we don't have a router */ | |
669 | router.s_addr = iaddr.s_addr; | |
670 | } | |
4a249263 A |
671 | printf("netboot: adding default route " IP_FORMAT "\n", |
672 | IP_LIST(&router)); | |
9bccf70c A |
673 | error = default_route_add(router, router.s_addr == iaddr.s_addr); |
674 | if (error) { | |
675 | printf("netboot: default_route_add failed %d\n", error); | |
676 | } | |
677 | ||
678 | soclose(so); | |
9bccf70c A |
679 | |
680 | S_netboot_info_p = netboot_info_init(iaddr); | |
681 | switch (S_netboot_info_p->image_type) { | |
682 | default: | |
683 | case kNetBootImageTypeNFS: | |
4a249263 A |
684 | for (try = 1; TRUE; try++) { |
685 | error = nfs_mountroot(); | |
686 | if (error == 0) { | |
687 | break; | |
688 | } | |
689 | printf("netboot: nfs_mountroot() attempt %u failed; " | |
690 | "clearing ARP entry and trying again\n", try); | |
691 | /* | |
692 | * error is either EHOSTDOWN or EHOSTUNREACH, which likely means | |
693 | * that the port we're plugged into has spanning tree enabled, | |
694 | * and either the router or the server can't answer our ARP | |
695 | * requests. Clear the incomplete ARP entry by removing the | |
696 | * appropriate route, depending on the error code: | |
697 | * EHOSTDOWN NFS server's route | |
698 | * EHOSTUNREACH router's route | |
699 | */ | |
700 | switch (error) { | |
701 | default: | |
702 | /* NOT REACHED */ | |
703 | case EHOSTDOWN: | |
704 | /* remove the server's arp entry */ | |
b0d623f7 A |
705 | error = host_route_delete(S_netboot_info_p->server_ip, |
706 | ifp->if_index); | |
4a249263 A |
707 | if (error) { |
708 | printf("netboot: host_route_delete(" IP_FORMAT | |
709 | ") failed %d\n", | |
710 | IP_LIST(&S_netboot_info_p->server_ip), error); | |
711 | } | |
712 | break; | |
713 | case EHOSTUNREACH: | |
b0d623f7 | 714 | error = host_route_delete(router, ifp->if_index); |
4a249263 A |
715 | if (error) { |
716 | printf("netboot: host_route_delete(" IP_FORMAT | |
717 | ") failed %d\n", IP_LIST(&router), error); | |
718 | } | |
719 | break; | |
720 | } | |
721 | } | |
9bccf70c A |
722 | break; |
723 | case kNetBootImageTypeHTTP: | |
2d21ac55 | 724 | error = netboot_setup(); |
9bccf70c A |
725 | break; |
726 | } | |
727 | if (error == 0) { | |
728 | S_netboot = 1; | |
729 | } | |
730 | else { | |
731 | S_netboot = 0; | |
732 | } | |
733 | return (error); | |
734 | failed: | |
735 | if (so != NULL) { | |
736 | soclose(so); | |
737 | } | |
9bccf70c A |
738 | return (error); |
739 | } | |
740 | ||
741 | int | |
2d21ac55 | 742 | netboot_setup() |
9bccf70c | 743 | { |
9bccf70c A |
744 | int error = 0; |
745 | ||
746 | if (S_netboot_info_p == NULL | |
747 | || S_netboot_info_p->image_path == NULL) { | |
748 | goto done; | |
749 | } | |
6d2010ae A |
750 | printf("netboot_setup: calling imageboot_mount_image\n"); |
751 | error = imageboot_mount_image(S_netboot_info_p->image_path, -1); | |
752 | if (error != 0) { | |
753 | printf("netboot: failed to mount root image, %d\n", error); | |
9bccf70c | 754 | } |
6d2010ae A |
755 | else if (S_netboot_info_p->second_image_path != NULL) { |
756 | error = imageboot_mount_image(S_netboot_info_p->second_image_path, 0); | |
757 | if (error != 0) { | |
758 | printf("netboot: failed to mount second root image, %d\n", error); | |
9bccf70c A |
759 | } |
760 | } | |
6d2010ae | 761 | |
9bccf70c A |
762 | done: |
763 | netboot_info_free(&S_netboot_info_p); | |
764 | return (error); | |
765 | } | |
766 | ||
767 | int | |
4a249263 | 768 | netboot_root(void) |
9bccf70c A |
769 | { |
770 | return (S_netboot); | |
771 | } |