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