xref: /titanic_41/usr/src/uts/common/io/strplumb.c (revision 2b24ab6b3865caeede9eeb9db6b83e1d89dcd1ea)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include	<sys/param.h>
27 #include	<sys/types.h>
28 #include	<sys/user.h>
29 #include	<sys/vfs.h>
30 #include	<sys/vnode.h>
31 #include	<sys/file.h>
32 #include	<sys/stream.h>
33 #include	<sys/stropts.h>
34 #include	<sys/strsubr.h>
35 #include	<sys/dlpi.h>
36 #include	<sys/vnode.h>
37 #include	<sys/socket.h>
38 #include	<sys/sockio.h>
39 #include	<net/if.h>
40 
41 #include	<sys/cred.h>
42 #include	<sys/sysmacros.h>
43 
44 #include	<sys/sad.h>
45 #include	<sys/kstr.h>
46 #include	<sys/bootconf.h>
47 #include	<sys/bootprops.h>
48 
49 #include	<sys/errno.h>
50 #include	<sys/modctl.h>
51 #include	<sys/sunddi.h>
52 #include	<sys/sunldi.h>
53 #include	<sys/esunddi.h>
54 #include	<sys/promif.h>
55 
56 #include	<netinet/in.h>
57 #include	<netinet/ip6.h>
58 #include	<netinet/icmp6.h>
59 #include	<netinet/sctp.h>
60 #include	<inet/common.h>
61 #include	<inet/ip.h>
62 #include	<inet/ip6.h>
63 #include	<inet/tcp.h>
64 #include	<inet/sctp_ip.h>
65 #include	<inet/udp_impl.h>
66 
67 #include	<sys/strlog.h>
68 #include	<sys/log.h>
69 #include	<sys/ethernet.h>
70 #include	<sys/ddi_implfuncs.h>
71 
72 #include	<sys/dld.h>
73 #include	<sys/mac_client.h>
74 
75 /*
76  * Debug Macros
77  */
78 int	strplumbdebug = 0;
79 
80 extern ib_boot_prop_t *iscsiboot_prop;
81 
82 #define	DBG0(_f) \
83 	if (strplumbdebug != 0) \
84 		printf("strplumb: " _f)
85 
86 #define	DBG1(_f, _a) \
87 	if (strplumbdebug != 0) \
88 		printf("strplumb: " _f, (_a))
89 
90 #define	DBG2(_f, _a, _b) \
91 	if (strplumbdebug != 0) \
92 		printf("strplumb: " _f, (_a), (_b))
93 
94 #define	DBG3(_f, _a, _b, _c) \
95 	if (strplumbdebug != 0) \
96 		printf("strplumb: " _f, (_a), (_b), (_c))
97 
98 /*
99  * Module linkage information for the kernel.
100  */
101 #define	STRPLUMB_IDENT	"STREAMS Plumbing Module"
102 
103 static struct modlmisc modlmisc = {
104 	&mod_miscops,
105 	STRPLUMB_IDENT
106 };
107 
108 static struct modlinkage modlinkage = {
109 	MODREV_1,
110 	&modlmisc,
111 	NULL
112 };
113 
114 int
115 _init(void)
116 {
117 	return (mod_install(&modlinkage));
118 }
119 
120 int
121 _fini(void)
122 {
123 	return (mod_remove(&modlinkage));
124 }
125 
126 int
127 _info(struct modinfo *modinfop)
128 {
129 	return (mod_info(&modlinkage, modinfop));
130 }
131 
132 #define	ARP		"arp"
133 #define	TCP		"tcp"
134 #define	TCP6		"tcp6"
135 #define	UDP		"udp"
136 #define	UDP6		"udp6"
137 #define	SCTP		"sctp"
138 #define	SCTP6		"sctp6"
139 #define	ICMP		"icmp"
140 #define	ICMP6		"icmp6"
141 #define	IP		"ip"
142 #define	IP6		"ip6"
143 #define	TIMOD		"timod"
144 
145 #define	UDPDEV		"/devices/pseudo/udp@0:udp"
146 #define	TCP6DEV		"/devices/pseudo/tcp6@0:tcp6"
147 #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
148 #define	SCTP6DEV	"/devices/pseudo/sctp6@0:sctp6"
149 #define	IP6DEV		"/devices/pseudo/ip6@0:ip6"
150 
151 typedef struct strplumb_modspec {
152 	char	*sm_type;
153 	char	*sm_name;
154 } strplumb_modspec_t;
155 
156 static strplumb_modspec_t	strplumb_modlist[] = {
157 	{ "drv", DLD_DRIVER_NAME },
158 	{ "drv", IP },
159 	{ "drv", IP6 },
160 	{ "drv", TCP },
161 	{ "drv", TCP6 },
162 	{ "drv", UDP },
163 	{ "drv", UDP6 },
164 	{ "drv", SCTP },
165 	{ "drv", SCTP6 },
166 	{ "drv", ICMP },
167 	{ "drv", ICMP6 },
168 	{ "drv", ARP },
169 	{ "strmod", TIMOD }
170 };
171 
172 /*
173  * Called from swapgeneric.c:loadrootmodules() in the network boot case.
174  */
175 int
176 strplumb_load(void)
177 {
178 	uint_t			i;
179 	strplumb_modspec_t	*p;
180 
181 	DBG0("loading modules\n");
182 
183 	for (i = 0, p = strplumb_modlist;
184 	    i < sizeof (strplumb_modlist) / sizeof (strplumb_modlist[0]);
185 	    i++, p++) {
186 		if (modloadonly(p->sm_type, p->sm_name) < 0) {
187 			printf("strplumb: failed to load %s/%s\n",
188 			    p->sm_type, p->sm_name);
189 			return (EFAULT);
190 		}
191 	}
192 
193 	return (0);
194 }
195 
196 static int
197 strplumb_init(void)
198 {
199 	uint_t			i;
200 	strplumb_modspec_t	*p;
201 	int			err;
202 
203 	DBG0("initializing modules\n");
204 
205 	for (i = 0, p = strplumb_modlist;
206 	    i < sizeof (strplumb_modlist) / sizeof (strplumb_modlist[0]);
207 	    i++, p++) {
208 		if (strcmp(p->sm_type, "drv") == 0)
209 			err = (i_ddi_attach_pseudo_node(p->sm_name) != NULL) ?
210 			    0 : EFAULT;
211 		else
212 			err = (modload(p->sm_type, p->sm_name) < 0) ?
213 			    EFAULT : 0;
214 
215 		if (err != 0)  {
216 			printf("strplumb: failed to initialize %s/%s\n",
217 			    p->sm_type, p->sm_name);
218 			return (err);
219 		}
220 	}
221 
222 	return (0);
223 }
224 
225 static int
226 strplumb_autopush(void)
227 {
228 	major_t		maj;
229 	minor_t		min;
230 	char		*mods[5];
231 	uint_t		anchor = 1;
232 	int		err;
233 
234 	min = (minor_t)-1;
235 	mods[1] = NULL;
236 
237 	/*
238 	 * ARP
239 	 */
240 	DBG0("setting up arp autopush\n");
241 
242 	mods[0] = ARP;
243 
244 	maj = ddi_name_to_major(ARP);
245 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, &anchor,
246 	    mods)) != 0) {
247 		printf("strplumb: kstr_autopush(SET/ARP) failed: %d\n", err);
248 		return (err);
249 	}
250 
251 	return (0);
252 }
253 
254 static int
255 strplumb_sctpq(ldi_ident_t li)
256 {
257 	ldi_handle_t	lh = NULL;
258 	int		err;
259 	int		rval;
260 
261 	DBG0("configuring SCTP default queue\n");
262 
263 	if ((err = ldi_open_by_name(SCTP6DEV, FREAD|FWRITE, CRED(), &lh,
264 	    li)) != 0) {
265 		printf("strplumb: open of SCTP6DEV failed: %d\n", err);
266 		return (err);
267 	}
268 
269 	if ((err = ldi_ioctl(lh, SCTP_IOC_DEFAULT_Q, (intptr_t)0, FKIOCTL,
270 	    CRED(), &rval)) != 0) {
271 		printf("strplumb: failed to set SCTP default queue: %d\n",
272 		    err);
273 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
274 		return (err);
275 	}
276 
277 	return (0);
278 }
279 
280 static int
281 strplumb_tcpq(ldi_ident_t li)
282 {
283 	ldi_handle_t	lh = NULL;
284 	ldi_handle_t	ip_lh = NULL;
285 	int		err;
286 	int		rval;
287 
288 	DBG0("configuring TCP default queue\n");
289 
290 	/*
291 	 * We open IP6DEV here because we need to have it open to in
292 	 * order to open TCP6DEV successfully.
293 	 */
294 	if ((err = ldi_open_by_name(IP6DEV, FREAD|FWRITE, CRED(), &ip_lh,
295 	    li)) != 0) {
296 		printf("strplumb: open of IP6DEV failed: %d\n", err);
297 		return (err);
298 	}
299 
300 	/*
301 	 * We set the tcp default queue to IPv6 because IPv4 falls back to
302 	 * IPv6 when it can't find a client, but IPv6 does not fall back to
303 	 * IPv4.
304 	 */
305 	if ((err = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, CRED(), &lh,
306 	    li)) != 0) {
307 		printf("strplumb: open of TCP6DEV failed: %d\n", err);
308 		goto done;
309 	}
310 
311 	if ((err = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, (intptr_t)0, FKIOCTL,
312 	    CRED(), &rval)) != 0) {
313 		printf("strplumb: failed to set TCP default queue: %d\n",
314 		    err);
315 		goto done;
316 	}
317 
318 done:
319 	(void) ldi_close(ip_lh, FREAD|FWRITE, CRED());
320 	return (err);
321 }
322 
323 /*
324  * Can be set in /etc/system in the case of local booting. See comment below.
325  */
326 char	*ndev_name = 0;
327 int	ndev_unit = 0;
328 
329 /*
330  * If we booted diskless then strplumb() will have been called from
331  * swapgeneric.c:rootconf(). All we can do in that case is plumb the
332  * network device that we booted from.
333  *
334  * If we booted from a local disk, we will have been called from main(),
335  * and normally we defer the plumbing of interfaces until network/physical.
336  * This can be overridden by setting "ndev_name" in /etc/system.
337  */
338 static int
339 resolve_boot_path(void)
340 {
341 	char			*devpath;
342 	dev_info_t		*dip;
343 	const char		*driver;
344 	int			instance;
345 #ifdef	_OBP
346 	char			stripped_path[OBP_MAXPATHLEN];
347 #endif
348 
349 	if (strncmp(rootfs.bo_fstype, "nfs", 3) == 0)
350 		devpath = rootfs.bo_name;
351 	else
352 		devpath = strplumb_get_netdev_path();
353 
354 	if (devpath != NULL) {
355 		DBG1("resolving boot-path: %s\n", devpath);
356 #ifdef _OBP
357 		/*
358 		 * OBP passes options e.g, "net:dhcp"
359 		 * remove them here
360 		 */
361 		prom_strip_options(devpath, stripped_path);
362 		devpath = stripped_path;
363 #endif
364 		/*
365 		 * Hold the devi since this is the root device.
366 		 */
367 		if ((dip = e_ddi_hold_devi_by_path(devpath, 0)) == NULL) {
368 			printf("strplumb: unable to hold root device: %s\n",
369 			    devpath);
370 			return (ENXIO);
371 		}
372 
373 		driver = ddi_driver_name(dip);
374 		instance = ddi_get_instance(dip);
375 	} else {
376 		if (ndev_name == NULL)
377 			return (ENODEV);
378 
379 		DBG2("using ndev_name (%s) ndev_unit (%d)\n", ndev_name,
380 		    ndev_unit);
381 
382 		if (i_ddi_attach_hw_nodes(ndev_name) != DDI_SUCCESS) {
383 			printf("strplumb: cannot load ndev_name '%s'\n",
384 			    ndev_name);
385 			return (ENXIO);
386 		}
387 
388 		driver = ndev_name;
389 		instance = ndev_unit;
390 	}
391 
392 	(void) snprintf(rootfs.bo_devname, BO_MAXOBJNAME,
393 	    "/devices/pseudo/clone@0:%s", driver);
394 	(void) snprintf(rootfs.bo_ifname, BO_MAXOBJNAME, "%s%d",
395 	    driver, instance);
396 	rootfs.bo_ppa = instance;
397 	return (0);
398 }
399 
400 static int
401 getifflags(ldi_handle_t lh, struct lifreq *lifrp)
402 {
403 	struct strioctl	iocb;
404 	int		rval;
405 
406 	iocb.ic_cmd = SIOCGLIFFLAGS;
407 	iocb.ic_timout = 15;
408 	iocb.ic_len = sizeof (struct lifreq);
409 	iocb.ic_dp = (char *)lifrp;
410 
411 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
412 
413 }
414 
415 static int
416 setifname(ldi_handle_t lh, struct lifreq *lifrp)
417 {
418 	struct strioctl	iocb;
419 	int		rval;
420 
421 	iocb.ic_cmd = SIOCSLIFNAME;
422 	iocb.ic_timout = 15;
423 	iocb.ic_len = sizeof (struct lifreq);
424 	iocb.ic_dp = (char *)lifrp;
425 
426 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
427 }
428 
429 extern ib_boot_prop_t	*iscsiboot_prop;
430 
431 static int
432 strplumb_dev(ldi_ident_t li)
433 {
434 	ldi_handle_t	lh = NULL;
435 	ldi_handle_t	mux_lh = NULL;
436 	int		err;
437 	struct lifreq	lifr;
438 	struct ifreq	ifr;
439 	int		rval;
440 	int		af = 0;
441 	char		*name = NULL;
442 
443 	bzero(&lifr, sizeof (struct lifreq));
444 	bzero(&ifr, sizeof (ifr));
445 
446 	if (iscsiboot_prop != NULL) {
447 		af = iscsiboot_prop->boot_nic.sin_family;
448 	}
449 
450 	/*
451 	 * Now set up the links. Ultimately, we should have two streams
452 	 * permanently linked underneath UDP (which is actually IP with UDP
453 	 * autopushed). One stream consists of the ARP-[ifname] combination,
454 	 * while the other consists of ARP-IP-[ifname]. The second combination
455 	 * seems a little weird, but is linked underneath UDP just to keep it
456 	 * around.
457 	 *
458 	 * We pin underneath UDP here to match what is done in ifconfig(1m);
459 	 * otherwise, ifconfig will be unable to unplumb the stream (the major
460 	 * number and mux id must both match for a successful I_PUNLINK).
461 	 *
462 	 * There are subtleties in the plumbing which make it essential to
463 	 * follow the logic used in ifconfig(1m) very closely.
464 	 */
465 
466 	/*
467 	 * Plumb UDP-ARP-IP-<dev>
468 	 */
469 
470 	if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
471 	    &lh, li)) != 0) {
472 		printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
473 		    err);
474 		goto done;
475 	}
476 
477 
478 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)IP, FKIOCTL, CRED(),
479 	    &rval)) != 0) {
480 		printf("strplumb: push IP failed: %d\n", err);
481 		goto done;
482 	}
483 
484 	if ((err = getifflags(lh, &lifr)) != 0)
485 		goto done;
486 
487 	if (af == 0 || af == AF_INET) {
488 		lifr.lifr_flags |= IFF_IPV4;
489 		lifr.lifr_flags &= ~IFF_IPV6;
490 		name = UDPDEV;
491 	} else {
492 		/*
493 		 * iscsi boot is used with ipv6 enabled
494 		 */
495 		lifr.lifr_flags |= IFF_IPV6;
496 		lifr.lifr_flags &= ~IFF_IPV4;
497 		name = UDP6DEV;
498 	}
499 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)ARP, FKIOCTL, CRED(),
500 	    &rval)) != 0) {
501 		printf("strplumb: push ARP failed: %d\n", err);
502 		goto done;
503 	}
504 
505 	(void) strlcpy(lifr.lifr_name, rootfs.bo_ifname,
506 	    sizeof (lifr.lifr_name));
507 	lifr.lifr_ppa = rootfs.bo_ppa;
508 
509 	if ((err = setifname(lh, &lifr)) != 0)
510 		goto done;
511 
512 	/* Get the flags and check if ARP is needed */
513 	if ((err = getifflags(lh, &lifr)) != 0) {
514 		printf("strplumb: getifflags %s IP failed, error %d\n",
515 		    lifr.lifr_name, err);
516 		goto done;
517 	}
518 
519 	/* Pop out ARP if not needed */
520 	if (lifr.lifr_flags & (IFF_NOARP | IFF_IPV6)) {
521 		err = ldi_ioctl(lh, I_POP, (intptr_t)0, FKIOCTL, CRED(),
522 		    &rval);
523 		if (err != 0) {
524 			printf("strplumb: pop ARP failed, error %d\n", err);
525 			goto done;
526 		}
527 	}
528 
529 	if ((err = ldi_open_by_name(name, FREAD|FWRITE, CRED(), &mux_lh,
530 	    li)) != 0) {
531 		printf("strplumb: open of %s failed: %d\n", name, err);
532 		goto done;
533 	}
534 
535 	if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
536 	    FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
537 	    &(ifr.ifr_ip_muxid))) != 0) {
538 		printf("strplumb: plink UDP-ARP-IP-%s failed: %d\n",
539 		    rootfs.bo_ifname, err);
540 		goto done;
541 	}
542 
543 	if (af == AF_INET6) {
544 		goto done;
545 	}
546 
547 	DBG2("UDP-ARP-IP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_ip_muxid);
548 
549 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
550 	lh = NULL;
551 
552 	/*
553 	 * Plumb UDP-ARP-<dev>
554 	 */
555 
556 	if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
557 	    &lh, li)) != 0) {
558 		printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
559 		    err);
560 		goto done;
561 	}
562 
563 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)ARP, FKIOCTL, CRED(),
564 	    &rval)) != 0) {
565 		printf("strplumb: push ARP failed: %d\n", err);
566 		goto done;
567 	}
568 
569 	if ((err = setifname(lh, &lifr)) != 0)
570 		goto done;
571 
572 	if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
573 	    FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
574 	    &(ifr.ifr_arp_muxid))) != 0) {
575 		printf("strplumb: plink UDP-ARP-%s failed: %d\n",
576 		    rootfs.bo_ifname, err);
577 		goto done;
578 	}
579 
580 	DBG2("UDP-ARP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_arp_muxid);
581 
582 	/*
583 	 * Cache the mux ids.
584 	 */
585 	(void) strlcpy(ifr.ifr_name, rootfs.bo_ifname, sizeof (ifr.ifr_name));
586 
587 	if ((err = ldi_ioctl(mux_lh, SIOCSIFMUXID, (intptr_t)&ifr, FKIOCTL,
588 	    CRED(), &rval)) != 0) {
589 		printf("strplumb: SIOCSIFMUXID failed: %d\n", err);
590 		goto done;
591 	}
592 
593 done:
594 	if (lh != NULL)
595 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
596 
597 	if (mux_lh != NULL)
598 		(void) ldi_close(mux_lh, FREAD|FWRITE, CRED());
599 
600 	return (err);
601 }
602 
603 /*
604  * Do streams plumbing for internet protocols.
605  */
606 int
607 strplumb(void)
608 {
609 	ldi_ident_t	li;
610 	int		err;
611 
612 	if ((err = strplumb_init()) != 0)
613 		return (err);
614 
615 	if ((err = strplumb_autopush()) != 0)
616 		return (err);
617 
618 	if ((err = ldi_ident_from_mod(&modlinkage, &li)) != 0)
619 		return (err);
620 
621 	/*
622 	 * Setup the TCP and SCTP default queues for the global stack.
623 	 * tcp/sctp_stack_init will do this for additional stack instances.
624 	 */
625 	if ((err = strplumb_sctpq(li)) != 0)
626 		goto done;
627 
628 	if ((err = strplumb_tcpq(li)) != 0)
629 		goto done;
630 
631 	if ((err = resolve_boot_path()) != 0)
632 		goto done;
633 
634 	DBG1("rootfs.bo_devname: %s\n", rootfs.bo_devname);
635 	DBG1("rootfs.bo_ifname: %s\n", rootfs.bo_ifname);
636 	DBG1("rootfs.bo_ppa: %d\n", rootfs.bo_ppa);
637 
638 	if ((err = strplumb_dev(li)) != 0)
639 		goto done;
640 
641 done:
642 	ldi_ident_release(li);
643 
644 	return (err);
645 }
646 
647 /* multiboot:  diskless boot interface discovery */
648 
649 #ifndef	_OBP
650 
651 static uchar_t boot_macaddr[16];
652 static int boot_maclen;
653 static uchar_t *getmacaddr(dev_info_t *dip, size_t *maclenp);
654 static int matchmac(dev_info_t *dip, void *arg);
655 
656 #endif  /* !_OBP */
657 
658 char *
659 strplumb_get_netdev_path(void)
660 {
661 #ifdef	_OBP
662 	char fstype[OBP_MAXPROPNAME];
663 
664 	if (bop_getprop("fstype", fstype) == -1)
665 		return (NULL);
666 
667 	if (strncmp(fstype, "nfs", 3) == 0)
668 		return (prom_bootpath());
669 	else
670 		return (NULL);
671 #else
672 
673 	char *macstr, *devpath = NULL;
674 	uchar_t *bootp;
675 	uint_t bootp_len;
676 
677 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
678 	    DDI_PROP_DONTPASS, BP_BOOT_MAC, &macstr) == DDI_SUCCESS) {
679 		/*
680 		 * hard coded ether mac len for booting floppy on
681 		 * machines with old cards
682 		 */
683 		boot_maclen = ether_aton(macstr, boot_macaddr);
684 		if (boot_maclen != 6) {
685 			cmn_err(CE_WARN,
686 			    "malformed boot_mac property, %d bytes",
687 			    boot_maclen);
688 		}
689 		ddi_prop_free(macstr);
690 	} else if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, ddi_root_node(),
691 	    DDI_PROP_DONTPASS, BP_BOOTP_RESPONSE, &bootp, &bootp_len)
692 	    == DDI_SUCCESS) {
693 
694 		/*
695 		 * These offsets are defined by dhcp standard
696 		 * Should use structure offsets
697 		 */
698 		boot_maclen = *(bootp + 2);
699 		ASSERT(boot_maclen <= 16);
700 		bcopy(bootp + 28, boot_macaddr, boot_maclen);
701 
702 		dhcack = kmem_alloc(bootp_len, KM_SLEEP);
703 		bcopy(bootp, dhcack, bootp_len);
704 		dhcacklen = bootp_len;
705 
706 		ddi_prop_free(bootp);
707 	} else  if (iscsiboot_prop != NULL) {
708 		bcopy(iscsiboot_prop->boot_nic.nic_mac,
709 		    boot_macaddr, IB_BOOT_MACLEN);
710 		boot_maclen = IB_BOOT_MACLEN;
711 	} else {
712 		return (NULL);
713 	}
714 
715 	ddi_walk_devs(ddi_root_node(), matchmac, (void *)&devpath);
716 	return (devpath);
717 
718 #endif  /* _OBP */
719 }
720 
721 #ifndef _OBP
722 
723 /*
724  * Get boot path from the boot_mac address
725  */
726 /*ARGSUSED*/
727 static int
728 matchmac(dev_info_t *dip, void *arg)
729 {
730 	char **devpathp = (char **)arg;
731 	char *model_str;
732 	uchar_t *macaddr;
733 	size_t maclen;
734 
735 	/* XXX Should use "device-type" per IEEE 1275 */
736 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0,
737 	    "model", &model_str) != DDI_SUCCESS)
738 		return (DDI_WALK_CONTINUE);
739 
740 	if (strcmp(model_str, "Ethernet controller") != 0) {
741 		ddi_prop_free(model_str);
742 		return (DDI_WALK_CONTINUE);
743 	}
744 	ddi_prop_free(model_str);
745 
746 	/* We have a network device now */
747 	if (i_ddi_attach_node_hierarchy(dip) != DDI_SUCCESS) {
748 		return (DDI_WALK_CONTINUE);
749 	}
750 
751 	ASSERT(boot_maclen != 0);
752 	macaddr = getmacaddr(dip, &maclen);
753 	if (macaddr == NULL)
754 		return (DDI_WALK_CONTINUE);
755 
756 	if (maclen != boot_maclen ||
757 	    bcmp(macaddr, boot_macaddr, maclen) != 0) {
758 		kmem_free(macaddr, maclen);
759 		return (DDI_WALK_CONTINUE);
760 	}
761 
762 	/* found hardware with the mac address */
763 	(void) localetheraddr((struct ether_addr *)macaddr, NULL);
764 	kmem_free(macaddr, maclen);
765 
766 	*devpathp = kmem_alloc(MAXPATHLEN, KM_SLEEP);
767 	(void) ddi_pathname(dip, *devpathp);
768 
769 	/* fill in dhcifname */
770 	if (dhcack) {
771 		(void) snprintf(dhcifname, IFNAMSIZ, "%s%d",
772 		    ddi_driver_name(dip), i_ddi_devi_get_ppa(dip));
773 	}
774 	return (DDI_WALK_TERMINATE);
775 }
776 
777 static uchar_t *
778 getmacaddr(dev_info_t *dip, size_t *maclenp)
779 {
780 	int rc, ppa;
781 	ldi_ident_t li;
782 	ldi_handle_t lh;
783 	const char *drv_name = ddi_driver_name(dip);
784 	char *clonepath;
785 	uchar_t *macaddr = NULL;
786 
787 	if (rc = ldi_ident_from_mod(&modlinkage, &li)) {
788 		cmn_err(CE_WARN,
789 		    "getmacaddr: ldi_ident_from_mod failed: %d\n", rc);
790 		return (NULL);
791 	}
792 
793 	clonepath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
794 	(void) snprintf(clonepath, MAXPATHLEN,
795 	    "/devices/pseudo/clone@0:%s", drv_name);
796 
797 	rc = ldi_open_by_name(clonepath, FREAD|FWRITE, CRED(), &lh, li);
798 	ldi_ident_release(li);
799 	if (rc) {
800 		cmn_err(CE_WARN,
801 		    "getmacaddr: ldi_open_by_name(%s) failed: %d\n",
802 		    clonepath, rc);
803 		kmem_free(clonepath, MAXPATHLEN);
804 		return (NULL);
805 	}
806 	kmem_free(clonepath, MAXPATHLEN);
807 
808 	ppa = i_ddi_devi_get_ppa(dip);
809 	if ((dl_attach(lh, ppa, NULL) != 0) ||
810 	    (dl_bind(lh, ETHERTYPE_IP, NULL) != 0)) {
811 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
812 		cmn_err(CE_WARN,
813 		    "getmacaddr: dl_attach/bind(%s%d) failed: %d\n",
814 		    drv_name, ppa, rc);
815 		return (NULL);
816 	}
817 
818 	*maclenp = ETHERADDRL;
819 	macaddr = kmem_alloc(ETHERADDRL, KM_SLEEP);
820 	if (dl_phys_addr(lh, macaddr, maclenp, NULL) != 0 ||
821 	    *maclenp != ETHERADDRL) {
822 		kmem_free(macaddr, ETHERADDRL);
823 		macaddr = NULL;
824 		*maclenp = 0;
825 		cmn_err(CE_WARN,
826 		    "getmacaddr: dl_phys_addr(%s%d) failed: %d\n",
827 		    drv_name, ppa, rc);
828 	}
829 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
830 	return (macaddr);
831 }
832 #endif	/* !_OBP */
833