xref: /illumos-gate/usr/src/uts/common/io/strplumb.c (revision 9d0cba7b65910f9d2d2808c710d20770e64a29ff)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ca45b50aSseb  * Common Development and Distribution License (the "License").
6ca45b50aSseb  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
226e91bba0SGirish Moodalbail  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include	<sys/param.h>
277c478bd9Sstevel@tonic-gate #include	<sys/types.h>
287c478bd9Sstevel@tonic-gate #include	<sys/user.h>
297c478bd9Sstevel@tonic-gate #include	<sys/vfs.h>
307c478bd9Sstevel@tonic-gate #include	<sys/vnode.h>
317c478bd9Sstevel@tonic-gate #include	<sys/file.h>
327c478bd9Sstevel@tonic-gate #include	<sys/stream.h>
337c478bd9Sstevel@tonic-gate #include	<sys/stropts.h>
347c478bd9Sstevel@tonic-gate #include	<sys/strsubr.h>
357c478bd9Sstevel@tonic-gate #include	<sys/dlpi.h>
367c478bd9Sstevel@tonic-gate #include	<sys/vnode.h>
377c478bd9Sstevel@tonic-gate #include	<sys/socket.h>
387c478bd9Sstevel@tonic-gate #include	<sys/sockio.h>
397c478bd9Sstevel@tonic-gate #include	<net/if.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include	<sys/cred.h>
427c478bd9Sstevel@tonic-gate #include	<sys/sysmacros.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include	<sys/sad.h>
457c478bd9Sstevel@tonic-gate #include	<sys/kstr.h>
467c478bd9Sstevel@tonic-gate #include	<sys/bootconf.h>
477c478bd9Sstevel@tonic-gate #include	<sys/bootprops.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include	<sys/errno.h>
507c478bd9Sstevel@tonic-gate #include	<sys/modctl.h>
517c478bd9Sstevel@tonic-gate #include	<sys/sunddi.h>
527c478bd9Sstevel@tonic-gate #include	<sys/sunldi.h>
537c478bd9Sstevel@tonic-gate #include	<sys/esunddi.h>
547c478bd9Sstevel@tonic-gate #include	<sys/promif.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #include	<sys/strlog.h>
577c478bd9Sstevel@tonic-gate #include	<sys/log.h>
587c478bd9Sstevel@tonic-gate #include	<sys/ethernet.h>
597c478bd9Sstevel@tonic-gate #include	<sys/ddi_implfuncs.h>
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #include	<sys/dld.h>
62da14cebeSEric Cheng #include	<sys/mac_client.h>
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  * Debug Macros
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate int	strplumbdebug = 0;
687c478bd9Sstevel@tonic-gate 
696cefaae1SJack Meng extern ib_boot_prop_t *iscsiboot_prop;
706cefaae1SJack Meng 
717c478bd9Sstevel@tonic-gate #define	DBG0(_f) \
727c478bd9Sstevel@tonic-gate 	if (strplumbdebug != 0) \
737c478bd9Sstevel@tonic-gate 		printf("strplumb: " _f)
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #define	DBG1(_f, _a) \
767c478bd9Sstevel@tonic-gate 	if (strplumbdebug != 0) \
777c478bd9Sstevel@tonic-gate 		printf("strplumb: " _f, (_a))
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #define	DBG2(_f, _a, _b) \
807c478bd9Sstevel@tonic-gate 	if (strplumbdebug != 0) \
817c478bd9Sstevel@tonic-gate 		printf("strplumb: " _f, (_a), (_b))
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #define	DBG3(_f, _a, _b, _c) \
847c478bd9Sstevel@tonic-gate 	if (strplumbdebug != 0) \
857c478bd9Sstevel@tonic-gate 		printf("strplumb: " _f, (_a), (_b), (_c))
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
897c478bd9Sstevel@tonic-gate  */
90986fd29aSsetje #define	STRPLUMB_IDENT	"STREAMS Plumbing Module"
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate static struct modlmisc modlmisc = {
937c478bd9Sstevel@tonic-gate 	&mod_miscops,
947c478bd9Sstevel@tonic-gate 	STRPLUMB_IDENT
957c478bd9Sstevel@tonic-gate };
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
987c478bd9Sstevel@tonic-gate 	MODREV_1,
997c478bd9Sstevel@tonic-gate 	&modlmisc,
1007c478bd9Sstevel@tonic-gate 	NULL
1017c478bd9Sstevel@tonic-gate };
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate int
_init(void)1047c478bd9Sstevel@tonic-gate _init(void)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate int
_fini(void)1107c478bd9Sstevel@tonic-gate _fini(void)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1167c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate #define	ARP		"arp"
1227c478bd9Sstevel@tonic-gate #define	TCP		"tcp"
1237c478bd9Sstevel@tonic-gate #define	TCP6		"tcp6"
1247c478bd9Sstevel@tonic-gate #define	UDP		"udp"
1257c478bd9Sstevel@tonic-gate #define	UDP6		"udp6"
1267c478bd9Sstevel@tonic-gate #define	ICMP		"icmp"
1277c478bd9Sstevel@tonic-gate #define	ICMP6		"icmp6"
1287c478bd9Sstevel@tonic-gate #define	IP		"ip"
1297c478bd9Sstevel@tonic-gate #define	IP6		"ip6"
1307c478bd9Sstevel@tonic-gate #define	TIMOD		"timod"
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate #define	UDPDEV		"/devices/pseudo/udp@0:udp"
1337c478bd9Sstevel@tonic-gate #define	TCP6DEV		"/devices/pseudo/tcp6@0:tcp6"
1346cefaae1SJack Meng #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
1357c478bd9Sstevel@tonic-gate #define	IP6DEV		"/devices/pseudo/ip6@0:ip6"
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate typedef struct strplumb_modspec {
1387c478bd9Sstevel@tonic-gate 	char	*sm_type;
1397c478bd9Sstevel@tonic-gate 	char	*sm_name;
1407c478bd9Sstevel@tonic-gate } strplumb_modspec_t;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate static strplumb_modspec_t	strplumb_modlist[] = {
1437c478bd9Sstevel@tonic-gate 	{ "drv", DLD_DRIVER_NAME },
1447c478bd9Sstevel@tonic-gate 	{ "drv", IP },
1457c478bd9Sstevel@tonic-gate 	{ "drv", IP6 },
1467c478bd9Sstevel@tonic-gate 	{ "drv", TCP },
1477c478bd9Sstevel@tonic-gate 	{ "drv", TCP6 },
1487c478bd9Sstevel@tonic-gate 	{ "drv", UDP },
1497c478bd9Sstevel@tonic-gate 	{ "drv", UDP6 },
1507c478bd9Sstevel@tonic-gate 	{ "drv", ICMP },
1517c478bd9Sstevel@tonic-gate 	{ "drv", ICMP6 },
1527c478bd9Sstevel@tonic-gate 	{ "drv", ARP },
1537c478bd9Sstevel@tonic-gate 	{ "strmod", TIMOD }
1547c478bd9Sstevel@tonic-gate };
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate  * Called from swapgeneric.c:loadrootmodules() in the network boot case.
1587c478bd9Sstevel@tonic-gate  */
1597c478bd9Sstevel@tonic-gate int
strplumb_load(void)1607c478bd9Sstevel@tonic-gate strplumb_load(void)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	uint_t			i;
1637c478bd9Sstevel@tonic-gate 	strplumb_modspec_t	*p;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	DBG0("loading modules\n");
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	for (i = 0, p = strplumb_modlist;
1687c478bd9Sstevel@tonic-gate 	    i < sizeof (strplumb_modlist) / sizeof (strplumb_modlist[0]);
1697c478bd9Sstevel@tonic-gate 	    i++, p++) {
1707c478bd9Sstevel@tonic-gate 		if (modloadonly(p->sm_type, p->sm_name) < 0) {
1717c478bd9Sstevel@tonic-gate 			printf("strplumb: failed to load %s/%s\n",
1727c478bd9Sstevel@tonic-gate 			    p->sm_type, p->sm_name);
1737c478bd9Sstevel@tonic-gate 			return (EFAULT);
1747c478bd9Sstevel@tonic-gate 		}
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	return (0);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate static int
strplumb_init(void)1817c478bd9Sstevel@tonic-gate strplumb_init(void)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	uint_t			i;
1847c478bd9Sstevel@tonic-gate 	strplumb_modspec_t	*p;
1857c478bd9Sstevel@tonic-gate 	int			err;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	DBG0("initializing modules\n");
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	for (i = 0, p = strplumb_modlist;
1907c478bd9Sstevel@tonic-gate 	    i < sizeof (strplumb_modlist) / sizeof (strplumb_modlist[0]);
1917c478bd9Sstevel@tonic-gate 	    i++, p++) {
1927c478bd9Sstevel@tonic-gate 		if (strcmp(p->sm_type, "drv") == 0)
1937c478bd9Sstevel@tonic-gate 			err = (i_ddi_attach_pseudo_node(p->sm_name) != NULL) ?
1947c478bd9Sstevel@tonic-gate 			    0 : EFAULT;
1957c478bd9Sstevel@tonic-gate 		else
1967c478bd9Sstevel@tonic-gate 			err = (modload(p->sm_type, p->sm_name) < 0) ?
1977c478bd9Sstevel@tonic-gate 			    EFAULT : 0;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 		if (err != 0)  {
2007c478bd9Sstevel@tonic-gate 			printf("strplumb: failed to initialize %s/%s\n",
2017c478bd9Sstevel@tonic-gate 			    p->sm_type, p->sm_name);
2027c478bd9Sstevel@tonic-gate 			return (err);
2037c478bd9Sstevel@tonic-gate 		}
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	return (0);
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate /*
2107c478bd9Sstevel@tonic-gate  * Can be set in /etc/system in the case of local booting. See comment below.
2117c478bd9Sstevel@tonic-gate  */
2127c478bd9Sstevel@tonic-gate char	*ndev_name = 0;
2137c478bd9Sstevel@tonic-gate int	ndev_unit = 0;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate  * If we booted diskless then strplumb() will have been called from
217c2c0835aSToomas Soome  * either:
218c2c0835aSToomas Soome  *	in case of x86 NEWBOOT: vfs.c:rootconf()
219c2c0835aSToomas Soome  *	in case of nfs root, the rootfs.bo_name is reset from /ramdisk:a
220c2c0835aSToomas Soome  *	to empty string and we will copy netdev_path there.
221c2c0835aSToomas Soome  * or
222c2c0835aSToomas Soome  *	in case of sparc: swapgeneric.c:rootconf().
223c2c0835aSToomas Soome  * All we can do in that case is plumb the network device that we booted from.
2247c478bd9Sstevel@tonic-gate  *
2257c478bd9Sstevel@tonic-gate  * If we booted from a local disk, we will have been called from main(),
2267c478bd9Sstevel@tonic-gate  * and normally we defer the plumbing of interfaces until network/physical.
2277c478bd9Sstevel@tonic-gate  * This can be overridden by setting "ndev_name" in /etc/system.
2287c478bd9Sstevel@tonic-gate  */
2297c478bd9Sstevel@tonic-gate static int
resolve_boot_path(void)230210db224Sericheng resolve_boot_path(void)
2317c478bd9Sstevel@tonic-gate {
232a88cd0f6Ssetje 	char			*devpath;
2337c478bd9Sstevel@tonic-gate 	dev_info_t		*dip;
2347c478bd9Sstevel@tonic-gate 	const char		*driver;
2357c478bd9Sstevel@tonic-gate 	int			instance;
236986fd29aSsetje #ifdef	_OBP
237986fd29aSsetje 	char			stripped_path[OBP_MAXPATHLEN];
238986fd29aSsetje #endif
2397c478bd9Sstevel@tonic-gate 
240c2c0835aSToomas Soome 	if (strncmp(rootfs.bo_fstype, "nfs", 3) == 0 &&
241c2c0835aSToomas Soome 	    rootfs.bo_name[0] != '\0') {
242a88cd0f6Ssetje 		devpath = rootfs.bo_name;
243c2c0835aSToomas Soome 	} else {
244a88cd0f6Ssetje 		devpath = strplumb_get_netdev_path();
245c2c0835aSToomas Soome 		netdev_path = devpath;
246c2c0835aSToomas Soome 		if (netdev_path != NULL) {
247c2c0835aSToomas Soome 			(void) strncpy(rootfs.bo_name, netdev_path,
248c2c0835aSToomas Soome 			    BO_MAXOBJNAME);
249c2c0835aSToomas Soome 		}
250c2c0835aSToomas Soome 	}
251a88cd0f6Ssetje 
252a88cd0f6Ssetje 	if (devpath != NULL) {
253a88cd0f6Ssetje 		DBG1("resolving boot-path: %s\n", devpath);
254986fd29aSsetje #ifdef _OBP
255986fd29aSsetje 		/*
256986fd29aSsetje 		 * OBP passes options e.g, "net:dhcp"
257986fd29aSsetje 		 * remove them here
258986fd29aSsetje 		 */
259a88cd0f6Ssetje 		prom_strip_options(devpath, stripped_path);
260986fd29aSsetje 		devpath = stripped_path;
2617c478bd9Sstevel@tonic-gate #endif
2627c478bd9Sstevel@tonic-gate 		/*
2637c478bd9Sstevel@tonic-gate 		 * Hold the devi since this is the root device.
2647c478bd9Sstevel@tonic-gate 		 */
2657c478bd9Sstevel@tonic-gate 		if ((dip = e_ddi_hold_devi_by_path(devpath, 0)) == NULL) {
2667c478bd9Sstevel@tonic-gate 			printf("strplumb: unable to hold root device: %s\n",
2677c478bd9Sstevel@tonic-gate 			    devpath);
2687c478bd9Sstevel@tonic-gate 			return (ENXIO);
2697c478bd9Sstevel@tonic-gate 		}
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 		driver = ddi_driver_name(dip);
2727c478bd9Sstevel@tonic-gate 		instance = ddi_get_instance(dip);
2737c478bd9Sstevel@tonic-gate 	} else {
2747c478bd9Sstevel@tonic-gate 		if (ndev_name == NULL)
2757c478bd9Sstevel@tonic-gate 			return (ENODEV);
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 		DBG2("using ndev_name (%s) ndev_unit (%d)\n", ndev_name,
2787c478bd9Sstevel@tonic-gate 		    ndev_unit);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 		if (i_ddi_attach_hw_nodes(ndev_name) != DDI_SUCCESS) {
2817c478bd9Sstevel@tonic-gate 			printf("strplumb: cannot load ndev_name '%s'\n",
2827c478bd9Sstevel@tonic-gate 			    ndev_name);
2837c478bd9Sstevel@tonic-gate 			return (ENXIO);
2847c478bd9Sstevel@tonic-gate 		}
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 		driver = ndev_name;
2877c478bd9Sstevel@tonic-gate 		instance = ndev_unit;
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	(void) snprintf(rootfs.bo_devname, BO_MAXOBJNAME,
2917c478bd9Sstevel@tonic-gate 	    "/devices/pseudo/clone@0:%s", driver);
2927c478bd9Sstevel@tonic-gate 	(void) snprintf(rootfs.bo_ifname, BO_MAXOBJNAME, "%s%d",
2937c478bd9Sstevel@tonic-gate 	    driver, instance);
2947c478bd9Sstevel@tonic-gate 	rootfs.bo_ppa = instance;
2957c478bd9Sstevel@tonic-gate 	return (0);
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate static int
getifflags(ldi_handle_t lh,struct lifreq * lifrp)2997c478bd9Sstevel@tonic-gate getifflags(ldi_handle_t lh, struct lifreq *lifrp)
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate 	struct strioctl	iocb;
3027c478bd9Sstevel@tonic-gate 	int		rval;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	iocb.ic_cmd = SIOCGLIFFLAGS;
3057c478bd9Sstevel@tonic-gate 	iocb.ic_timout = 15;
3067c478bd9Sstevel@tonic-gate 	iocb.ic_len = sizeof (struct lifreq);
3077c478bd9Sstevel@tonic-gate 	iocb.ic_dp = (char *)lifrp;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate static int
setifname(ldi_handle_t lh,struct lifreq * lifrp)3147c478bd9Sstevel@tonic-gate setifname(ldi_handle_t lh, struct lifreq *lifrp)
3157c478bd9Sstevel@tonic-gate {
3167c478bd9Sstevel@tonic-gate 	struct strioctl	iocb;
3177c478bd9Sstevel@tonic-gate 	int		rval;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	iocb.ic_cmd = SIOCSLIFNAME;
3207c478bd9Sstevel@tonic-gate 	iocb.ic_timout = 15;
3217c478bd9Sstevel@tonic-gate 	iocb.ic_len = sizeof (struct lifreq);
3227c478bd9Sstevel@tonic-gate 	iocb.ic_dp = (char *)lifrp;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate static int
strplumb_dev(ldi_ident_t li)3287c478bd9Sstevel@tonic-gate strplumb_dev(ldi_ident_t li)
3297c478bd9Sstevel@tonic-gate {
3307c478bd9Sstevel@tonic-gate 	ldi_handle_t	lh = NULL;
3317c478bd9Sstevel@tonic-gate 	ldi_handle_t	mux_lh = NULL;
3327c478bd9Sstevel@tonic-gate 	int		err;
3337c478bd9Sstevel@tonic-gate 	struct lifreq	lifr;
3347c478bd9Sstevel@tonic-gate 	struct ifreq	ifr;
3357c478bd9Sstevel@tonic-gate 	int		rval;
3366cefaae1SJack Meng 	int		af = 0;
3376cefaae1SJack Meng 	char		*name = NULL;
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	bzero(&lifr, sizeof (struct lifreq));
3407c478bd9Sstevel@tonic-gate 	bzero(&ifr, sizeof (ifr));
3417c478bd9Sstevel@tonic-gate 
3426cefaae1SJack Meng 	if (iscsiboot_prop != NULL) {
3436cefaae1SJack Meng 		af = iscsiboot_prop->boot_nic.sin_family;
3446cefaae1SJack Meng 	}
3456cefaae1SJack Meng 
3467c478bd9Sstevel@tonic-gate 	/*
3477c478bd9Sstevel@tonic-gate 	 * Now set up the links. Ultimately, we should have two streams
348bd670b35SErik Nordmark 	 * permanently linked under UDP.  One stream consists of the
349bd670b35SErik Nordmark 	 * ARP-[ifname] combination, while the other consists of IP-[ifname].
3507c478bd9Sstevel@tonic-gate 	 *
351bbf21555SRichard Lowe 	 * We pin underneath UDP here to match what is done in ifconfig(8);
3527c478bd9Sstevel@tonic-gate 	 * otherwise, ifconfig will be unable to unplumb the stream (the major
3537c478bd9Sstevel@tonic-gate 	 * number and mux id must both match for a successful I_PUNLINK).
3547c478bd9Sstevel@tonic-gate 	 *
3557c478bd9Sstevel@tonic-gate 	 * There are subtleties in the plumbing which make it essential to
356bbf21555SRichard Lowe 	 * follow the logic used in ifconfig(8) very closely.
3577c478bd9Sstevel@tonic-gate 	 */
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	/*
360bd670b35SErik Nordmark 	 * Plumb UDP-IP-<dev>
3617c478bd9Sstevel@tonic-gate 	 */
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
3647c478bd9Sstevel@tonic-gate 	    &lh, li)) != 0) {
3657c478bd9Sstevel@tonic-gate 		printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
3667c478bd9Sstevel@tonic-gate 		    err);
3677c478bd9Sstevel@tonic-gate 		goto done;
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)IP, FKIOCTL, CRED(),
3727c478bd9Sstevel@tonic-gate 	    &rval)) != 0) {
3737c478bd9Sstevel@tonic-gate 		printf("strplumb: push IP failed: %d\n", err);
3747c478bd9Sstevel@tonic-gate 		goto done;
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	if ((err = getifflags(lh, &lifr)) != 0)
3787c478bd9Sstevel@tonic-gate 		goto done;
3797c478bd9Sstevel@tonic-gate 
3806cefaae1SJack Meng 	if (af == 0 || af == AF_INET) {
3817c478bd9Sstevel@tonic-gate 		lifr.lifr_flags |= IFF_IPV4;
3827c478bd9Sstevel@tonic-gate 		lifr.lifr_flags &= ~IFF_IPV6;
3836cefaae1SJack Meng 		name = UDPDEV;
3846cefaae1SJack Meng 	} else {
3856cefaae1SJack Meng 		/*
3866cefaae1SJack Meng 		 * iscsi boot is used with ipv6 enabled
3876cefaae1SJack Meng 		 */
3886cefaae1SJack Meng 		lifr.lifr_flags |= IFF_IPV6;
3896cefaae1SJack Meng 		lifr.lifr_flags &= ~IFF_IPV4;
3906cefaae1SJack Meng 		name = UDP6DEV;
3916cefaae1SJack Meng 	}
3927c478bd9Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, rootfs.bo_ifname,
3937c478bd9Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
3947c478bd9Sstevel@tonic-gate 	lifr.lifr_ppa = rootfs.bo_ppa;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	if ((err = setifname(lh, &lifr)) != 0)
3977c478bd9Sstevel@tonic-gate 		goto done;
3987c478bd9Sstevel@tonic-gate 
399bd670b35SErik Nordmark 	/* get the flags and check if ARP is needed */
4007c478bd9Sstevel@tonic-gate 	if ((err = getifflags(lh, &lifr)) != 0) {
4017c478bd9Sstevel@tonic-gate 		printf("strplumb: getifflags %s IP failed, error %d\n",
4027c478bd9Sstevel@tonic-gate 		    lifr.lifr_name, err);
4037c478bd9Sstevel@tonic-gate 		goto done;
4047c478bd9Sstevel@tonic-gate 	}
4056cefaae1SJack Meng 	if ((err = ldi_open_by_name(name, FREAD|FWRITE, CRED(), &mux_lh,
4067c478bd9Sstevel@tonic-gate 	    li)) != 0) {
4076cefaae1SJack Meng 		printf("strplumb: open of %s failed: %d\n", name, err);
4087c478bd9Sstevel@tonic-gate 		goto done;
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
4117c478bd9Sstevel@tonic-gate 	    FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
4127c478bd9Sstevel@tonic-gate 	    &(ifr.ifr_ip_muxid))) != 0) {
4137c478bd9Sstevel@tonic-gate 		printf("strplumb: plink UDP-ARP-IP-%s failed: %d\n",
4147c478bd9Sstevel@tonic-gate 		    rootfs.bo_ifname, err);
4157c478bd9Sstevel@tonic-gate 		goto done;
4167c478bd9Sstevel@tonic-gate 	}
4177c478bd9Sstevel@tonic-gate 
418bd670b35SErik Nordmark 	/* if ARP is not needed, we are done */
419bd670b35SErik Nordmark 	if (lifr.lifr_flags & (IFF_NOARP | IFF_IPV6))
4206cefaae1SJack Meng 		goto done;
4216cefaae1SJack Meng 
4227c478bd9Sstevel@tonic-gate 	DBG2("UDP-ARP-IP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_ip_muxid);
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
4257c478bd9Sstevel@tonic-gate 	lh = NULL;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	/*
4287c478bd9Sstevel@tonic-gate 	 * Plumb UDP-ARP-<dev>
4297c478bd9Sstevel@tonic-gate 	 */
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
4327c478bd9Sstevel@tonic-gate 	    &lh, li)) != 0) {
4337c478bd9Sstevel@tonic-gate 		printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
4347c478bd9Sstevel@tonic-gate 		    err);
4357c478bd9Sstevel@tonic-gate 		goto done;
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)ARP, FKIOCTL, CRED(),
4397c478bd9Sstevel@tonic-gate 	    &rval)) != 0) {
4407c478bd9Sstevel@tonic-gate 		printf("strplumb: push ARP failed: %d\n", err);
4417c478bd9Sstevel@tonic-gate 		goto done;
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	if ((err = setifname(lh, &lifr)) != 0)
4457c478bd9Sstevel@tonic-gate 		goto done;
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
4487c478bd9Sstevel@tonic-gate 	    FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
4497c478bd9Sstevel@tonic-gate 	    &(ifr.ifr_arp_muxid))) != 0) {
4507c478bd9Sstevel@tonic-gate 		printf("strplumb: plink UDP-ARP-%s failed: %d\n",
4517c478bd9Sstevel@tonic-gate 		    rootfs.bo_ifname, err);
4527c478bd9Sstevel@tonic-gate 		goto done;
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	DBG2("UDP-ARP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_arp_muxid);
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	/*
4587c478bd9Sstevel@tonic-gate 	 * Cache the mux ids.
4597c478bd9Sstevel@tonic-gate 	 */
4607c478bd9Sstevel@tonic-gate 	(void) strlcpy(ifr.ifr_name, rootfs.bo_ifname, sizeof (ifr.ifr_name));
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, SIOCSIFMUXID, (intptr_t)&ifr, FKIOCTL,
4637c478bd9Sstevel@tonic-gate 	    CRED(), &rval)) != 0) {
4647c478bd9Sstevel@tonic-gate 		printf("strplumb: SIOCSIFMUXID failed: %d\n", err);
4657c478bd9Sstevel@tonic-gate 		goto done;
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate done:
4697c478bd9Sstevel@tonic-gate 	if (lh != NULL)
4707c478bd9Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	if (mux_lh != NULL)
4737c478bd9Sstevel@tonic-gate 		(void) ldi_close(mux_lh, FREAD|FWRITE, CRED());
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	return (err);
4767c478bd9Sstevel@tonic-gate }
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate /*
4797c478bd9Sstevel@tonic-gate  * Do streams plumbing for internet protocols.
4807c478bd9Sstevel@tonic-gate  */
4817c478bd9Sstevel@tonic-gate int
strplumb(void)4827c478bd9Sstevel@tonic-gate strplumb(void)
4837c478bd9Sstevel@tonic-gate {
4847c478bd9Sstevel@tonic-gate 	ldi_ident_t	li;
4857c478bd9Sstevel@tonic-gate 	int		err;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	if ((err = strplumb_init()) != 0)
4887c478bd9Sstevel@tonic-gate 		return (err);
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	if ((err = ldi_ident_from_mod(&modlinkage, &li)) != 0)
4917c478bd9Sstevel@tonic-gate 		return (err);
4927c478bd9Sstevel@tonic-gate 
493210db224Sericheng 	if ((err = resolve_boot_path()) != 0)
4947c478bd9Sstevel@tonic-gate 		goto done;
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	DBG1("rootfs.bo_devname: %s\n", rootfs.bo_devname);
4977c478bd9Sstevel@tonic-gate 	DBG1("rootfs.bo_ifname: %s\n", rootfs.bo_ifname);
4987c478bd9Sstevel@tonic-gate 	DBG1("rootfs.bo_ppa: %d\n", rootfs.bo_ppa);
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	if ((err = strplumb_dev(li)) != 0)
5017c478bd9Sstevel@tonic-gate 		goto done;
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate done:
5047c478bd9Sstevel@tonic-gate 	ldi_ident_release(li);
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	return (err);
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate /* multiboot:  diskless boot interface discovery */
5107c478bd9Sstevel@tonic-gate 
511986fd29aSsetje #ifndef	_OBP
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate static uchar_t boot_macaddr[16];
5147c478bd9Sstevel@tonic-gate static int boot_maclen;
515d62bc4baSyz147064 static uchar_t *getmacaddr(dev_info_t *dip, size_t *maclenp);
5167c478bd9Sstevel@tonic-gate static int matchmac(dev_info_t *dip, void *arg);
5177c478bd9Sstevel@tonic-gate 
518986fd29aSsetje #endif  /* !_OBP */
519a9ddc71cSsetje 
5207c478bd9Sstevel@tonic-gate char *
strplumb_get_netdev_path(void)5217c478bd9Sstevel@tonic-gate strplumb_get_netdev_path(void)
5227c478bd9Sstevel@tonic-gate {
523a88cd0f6Ssetje #ifdef	_OBP
524a88cd0f6Ssetje 	char		fstype[OBP_MAXPROPNAME];
525dedec472SJack Meng 	static char	iscsi_network_path[BO_MAXOBJNAME]	= {0};
526dedec472SJack Meng 	int		proplen;
527dedec472SJack Meng 	char		*p	= NULL;
528a88cd0f6Ssetje 
529a88cd0f6Ssetje 	if (bop_getprop("fstype", fstype) == -1)
530a88cd0f6Ssetje 		return (NULL);
531a88cd0f6Ssetje 
532a88cd0f6Ssetje 	if (strncmp(fstype, "nfs", 3) == 0)
533a88cd0f6Ssetje 		return (prom_bootpath());
534dedec472SJack Meng 	else if (iscsiboot_prop != NULL) {
535dedec472SJack Meng 		proplen =  BOP_GETPROPLEN(bootops,
536dedec472SJack Meng 		    BP_ISCSI_NETWORK_BOOTPATH);
537dedec472SJack Meng 		if (proplen > 0) {
538dedec472SJack Meng 			if (BOP_GETPROP(bootops,
539dedec472SJack Meng 			    BP_ISCSI_NETWORK_BOOTPATH,
540dedec472SJack Meng 			    iscsi_network_path) > 0) {
541dedec472SJack Meng 				p = strchr(iscsi_network_path, ':');
542dedec472SJack Meng 				if (p != NULL) {
543dedec472SJack Meng 					*p = '\0';
544dedec472SJack Meng 				}
545dedec472SJack Meng 				return (iscsi_network_path);
546dedec472SJack Meng 			}
547dedec472SJack Meng 		}
548dedec472SJack Meng 	}
549a88cd0f6Ssetje 	return (NULL);
550a88cd0f6Ssetje #else
551a88cd0f6Ssetje 
5527c478bd9Sstevel@tonic-gate 	char *macstr, *devpath = NULL;
5537c478bd9Sstevel@tonic-gate 	uchar_t *bootp;
554986fd29aSsetje 	uint_t bootp_len;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
5577c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, BP_BOOT_MAC, &macstr) == DDI_SUCCESS) {
5587c478bd9Sstevel@tonic-gate 		/*
5597c478bd9Sstevel@tonic-gate 		 * hard coded ether mac len for booting floppy on
5607c478bd9Sstevel@tonic-gate 		 * machines with old cards
5617c478bd9Sstevel@tonic-gate 		 */
5627c478bd9Sstevel@tonic-gate 		boot_maclen = ether_aton(macstr, boot_macaddr);
5637c478bd9Sstevel@tonic-gate 		if (boot_maclen != 6) {
5647c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
5657c478bd9Sstevel@tonic-gate 			    "malformed boot_mac property, %d bytes",
5667c478bd9Sstevel@tonic-gate 			    boot_maclen);
5677c478bd9Sstevel@tonic-gate 		}
5687c478bd9Sstevel@tonic-gate 		ddi_prop_free(macstr);
5697c478bd9Sstevel@tonic-gate 	} else if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, ddi_root_node(),
5707c478bd9Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, BP_BOOTP_RESPONSE, &bootp, &bootp_len)
5717c478bd9Sstevel@tonic-gate 	    == DDI_SUCCESS) {
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 		/*
5747c478bd9Sstevel@tonic-gate 		 * These offsets are defined by dhcp standard
5757c478bd9Sstevel@tonic-gate 		 * Should use structure offsets
5767c478bd9Sstevel@tonic-gate 		 */
5777c478bd9Sstevel@tonic-gate 		boot_maclen = *(bootp + 2);
5787c478bd9Sstevel@tonic-gate 		ASSERT(boot_maclen <= 16);
579986fd29aSsetje 		bcopy(bootp + 28, boot_macaddr, boot_maclen);
5807c478bd9Sstevel@tonic-gate 
581986fd29aSsetje 		dhcack = kmem_alloc(bootp_len, KM_SLEEP);
582986fd29aSsetje 		bcopy(bootp, dhcack, bootp_len);
583986fd29aSsetje 		dhcacklen = bootp_len;
584986fd29aSsetje 
5857c478bd9Sstevel@tonic-gate 		ddi_prop_free(bootp);
5866cefaae1SJack Meng 	} else  if (iscsiboot_prop != NULL) {
5876cefaae1SJack Meng 		bcopy(iscsiboot_prop->boot_nic.nic_mac,
5886cefaae1SJack Meng 		    boot_macaddr, IB_BOOT_MACLEN);
5896cefaae1SJack Meng 		boot_maclen = IB_BOOT_MACLEN;
5906cefaae1SJack Meng 	} else {
5917c478bd9Sstevel@tonic-gate 		return (NULL);
5926cefaae1SJack Meng 	}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	ddi_walk_devs(ddi_root_node(), matchmac, (void *)&devpath);
5957c478bd9Sstevel@tonic-gate 	return (devpath);
596a9ddc71cSsetje 
597a88cd0f6Ssetje #endif  /* _OBP */
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate 
600986fd29aSsetje #ifndef _OBP
601a9ddc71cSsetje 
6027c478bd9Sstevel@tonic-gate /*
6037c478bd9Sstevel@tonic-gate  * Get boot path from the boot_mac address
6047c478bd9Sstevel@tonic-gate  */
6057c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6067c478bd9Sstevel@tonic-gate static int
matchmac(dev_info_t * dip,void * arg)6077c478bd9Sstevel@tonic-gate matchmac(dev_info_t *dip, void *arg)
6087c478bd9Sstevel@tonic-gate {
6097c478bd9Sstevel@tonic-gate 	char **devpathp = (char **)arg;
6107c478bd9Sstevel@tonic-gate 	char *model_str;
6117c478bd9Sstevel@tonic-gate 	uchar_t *macaddr;
612d62bc4baSyz147064 	size_t maclen;
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	/* XXX Should use "device-type" per IEEE 1275 */
6157c478bd9Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0,
6167c478bd9Sstevel@tonic-gate 	    "model", &model_str) != DDI_SUCCESS)
6177c478bd9Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	if (strcmp(model_str, "Ethernet controller") != 0) {
6207c478bd9Sstevel@tonic-gate 		ddi_prop_free(model_str);
6217c478bd9Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
6227c478bd9Sstevel@tonic-gate 	}
6237c478bd9Sstevel@tonic-gate 	ddi_prop_free(model_str);
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	/* We have a network device now */
6267c478bd9Sstevel@tonic-gate 	if (i_ddi_attach_node_hierarchy(dip) != DDI_SUCCESS) {
6277c478bd9Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	ASSERT(boot_maclen != 0);
6317c478bd9Sstevel@tonic-gate 	macaddr = getmacaddr(dip, &maclen);
6327c478bd9Sstevel@tonic-gate 	if (macaddr == NULL)
6337c478bd9Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	if (maclen != boot_maclen ||
6367c478bd9Sstevel@tonic-gate 	    bcmp(macaddr, boot_macaddr, maclen) != 0) {
6377c478bd9Sstevel@tonic-gate 		kmem_free(macaddr, maclen);
6387c478bd9Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
6397c478bd9Sstevel@tonic-gate 	}
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	/* found hardware with the mac address */
6427c478bd9Sstevel@tonic-gate 	(void) localetheraddr((struct ether_addr *)macaddr, NULL);
6437c478bd9Sstevel@tonic-gate 	kmem_free(macaddr, maclen);
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 	*devpathp = kmem_alloc(MAXPATHLEN, KM_SLEEP);
6467c478bd9Sstevel@tonic-gate 	(void) ddi_pathname(dip, *devpathp);
6477c478bd9Sstevel@tonic-gate 
648986fd29aSsetje 	/* fill in dhcifname */
649986fd29aSsetje 	if (dhcack) {
650986fd29aSsetje 		(void) snprintf(dhcifname, IFNAMSIZ, "%s%d",
6517c478bd9Sstevel@tonic-gate 		    ddi_driver_name(dip), i_ddi_devi_get_ppa(dip));
652986fd29aSsetje 	}
6537c478bd9Sstevel@tonic-gate 	return (DDI_WALK_TERMINATE);
6547c478bd9Sstevel@tonic-gate }
6557c478bd9Sstevel@tonic-gate 
656*9d0cba7bSToomas Soome static void
print_error(const char * fn,int rc,const char * drv_name,int ppa,dl_error_ack_t * dleap)657*9d0cba7bSToomas Soome print_error(const char *fn, int rc, const char *drv_name,
658*9d0cba7bSToomas Soome     int ppa, dl_error_ack_t *dleap)
659*9d0cba7bSToomas Soome {
660*9d0cba7bSToomas Soome 	if (rc == ENOTSUP) {
661*9d0cba7bSToomas Soome 		cmn_err(CE_WARN,
662*9d0cba7bSToomas Soome 		    "getmacaddr: %s(%s%d) failed: DL_ERROR_ACK "
663*9d0cba7bSToomas Soome 		    "DLPI errno 0x%x, UNIX errno %d", fn,
664*9d0cba7bSToomas Soome 		    drv_name, ppa, dleap->dl_errno, dleap->dl_unix_errno);
665*9d0cba7bSToomas Soome 	} else {
666*9d0cba7bSToomas Soome 		cmn_err(CE_WARN,
667*9d0cba7bSToomas Soome 		    "getmacaddr: %s(%s%d) failed: %d", fn, drv_name, ppa, rc);
668*9d0cba7bSToomas Soome 	}
669*9d0cba7bSToomas Soome }
670*9d0cba7bSToomas Soome 
6717c478bd9Sstevel@tonic-gate static uchar_t *
getmacaddr(dev_info_t * dip,size_t * maclenp)672d62bc4baSyz147064 getmacaddr(dev_info_t *dip, size_t *maclenp)
6737c478bd9Sstevel@tonic-gate {
6747c478bd9Sstevel@tonic-gate 	int rc, ppa;
6757c478bd9Sstevel@tonic-gate 	ldi_ident_t li;
6767c478bd9Sstevel@tonic-gate 	ldi_handle_t lh;
677d62bc4baSyz147064 	const char *drv_name = ddi_driver_name(dip);
6787c478bd9Sstevel@tonic-gate 	char *clonepath;
6797c478bd9Sstevel@tonic-gate 	uchar_t *macaddr = NULL;
680*9d0cba7bSToomas Soome 	dl_error_ack_t dlea;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	if (rc = ldi_ident_from_mod(&modlinkage, &li)) {
6837c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
684*9d0cba7bSToomas Soome 		    "%s: ldi_ident_from_mod failed: %d", __func__, rc);
6857c478bd9Sstevel@tonic-gate 		return (NULL);
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	clonepath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
6897c478bd9Sstevel@tonic-gate 	(void) snprintf(clonepath, MAXPATHLEN,
6907c478bd9Sstevel@tonic-gate 	    "/devices/pseudo/clone@0:%s", drv_name);
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	rc = ldi_open_by_name(clonepath, FREAD|FWRITE, CRED(), &lh, li);
6937c478bd9Sstevel@tonic-gate 	ldi_ident_release(li);
6947c478bd9Sstevel@tonic-gate 	if (rc) {
6957c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN,
696*9d0cba7bSToomas Soome 		    "%s: ldi_open_by_name(%s) failed: %d",
697*9d0cba7bSToomas Soome 		    __func__, clonepath, rc);
6987c478bd9Sstevel@tonic-gate 		kmem_free(clonepath, MAXPATHLEN);
6997c478bd9Sstevel@tonic-gate 		return (NULL);
7007c478bd9Sstevel@tonic-gate 	}
7017c478bd9Sstevel@tonic-gate 	kmem_free(clonepath, MAXPATHLEN);
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	ppa = i_ddi_devi_get_ppa(dip);
704*9d0cba7bSToomas Soome 	rc = dl_attach(lh, ppa, &dlea);
705*9d0cba7bSToomas Soome 	if (rc != 0) {
7067c478bd9Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
707*9d0cba7bSToomas Soome 		print_error("dl_attach", rc, drv_name, ppa, &dlea);
7087c478bd9Sstevel@tonic-gate 		return (NULL);
7097c478bd9Sstevel@tonic-gate 	}
710*9d0cba7bSToomas Soome 	rc = dl_bind(lh, ETHERTYPE_IP, &dlea);
711*9d0cba7bSToomas Soome 	if (rc != 0) {
712*9d0cba7bSToomas Soome 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
713*9d0cba7bSToomas Soome 		print_error("dl_bind", rc, drv_name, ppa, &dlea);
714*9d0cba7bSToomas Soome 	}
715d62bc4baSyz147064 
716d62bc4baSyz147064 	*maclenp = ETHERADDRL;
717d62bc4baSyz147064 	macaddr = kmem_alloc(ETHERADDRL, KM_SLEEP);
718*9d0cba7bSToomas Soome 	rc = dl_phys_addr(lh, macaddr, maclenp, &dlea);
719*9d0cba7bSToomas Soome 	if (rc != 0 || *maclenp != ETHERADDRL) {
720d62bc4baSyz147064 		kmem_free(macaddr, ETHERADDRL);
7217c478bd9Sstevel@tonic-gate 		macaddr = NULL;
7227c478bd9Sstevel@tonic-gate 		*maclenp = 0;
723*9d0cba7bSToomas Soome 		print_error("dl_phys_addr", rc, drv_name, ppa, &dlea);
7247c478bd9Sstevel@tonic-gate 	}
7257c478bd9Sstevel@tonic-gate 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
7267c478bd9Sstevel@tonic-gate 	return (macaddr);
7277c478bd9Sstevel@tonic-gate }
728986fd29aSsetje #endif	/* !_OBP */
729