xref: /onnv-gate/usr/src/uts/common/io/strplumb.c (revision 129:896d8c535003)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include	<sys/param.h>
300Sstevel@tonic-gate #include	<sys/types.h>
310Sstevel@tonic-gate #include	<sys/user.h>
320Sstevel@tonic-gate #include	<sys/vfs.h>
330Sstevel@tonic-gate #include	<sys/vnode.h>
340Sstevel@tonic-gate #include	<sys/file.h>
350Sstevel@tonic-gate #include	<sys/stream.h>
360Sstevel@tonic-gate #include	<sys/stropts.h>
370Sstevel@tonic-gate #include	<sys/strsubr.h>
380Sstevel@tonic-gate #include	<sys/dlpi.h>
390Sstevel@tonic-gate #include	<sys/vnode.h>
400Sstevel@tonic-gate #include	<sys/socket.h>
410Sstevel@tonic-gate #include	<sys/sockio.h>
420Sstevel@tonic-gate #include	<net/if.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include	<sys/cred.h>
450Sstevel@tonic-gate #include	<sys/sysmacros.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #include	<sys/sad.h>
480Sstevel@tonic-gate #include	<sys/kstr.h>
490Sstevel@tonic-gate #include	<sys/bootconf.h>
500Sstevel@tonic-gate #include	<sys/bootprops.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #include	<sys/errno.h>
530Sstevel@tonic-gate #include	<sys/modctl.h>
540Sstevel@tonic-gate #include	<sys/sunddi.h>
550Sstevel@tonic-gate #include	<sys/sunldi.h>
560Sstevel@tonic-gate #include	<sys/esunddi.h>
570Sstevel@tonic-gate #include	<sys/promif.h>
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include	<netinet/in.h>
600Sstevel@tonic-gate #include	<netinet/ip6.h>
610Sstevel@tonic-gate #include	<netinet/icmp6.h>
620Sstevel@tonic-gate #include	<netinet/sctp.h>
630Sstevel@tonic-gate #include	<inet/common.h>
640Sstevel@tonic-gate #include	<inet/ip.h>
650Sstevel@tonic-gate #include	<inet/ip6.h>
660Sstevel@tonic-gate #include	<inet/tcp.h>
670Sstevel@tonic-gate #include	<inet/sctp_ip.h>
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #include	<sys/strlog.h>
700Sstevel@tonic-gate #include	<sys/log.h>
710Sstevel@tonic-gate #include	<sys/ethernet.h>
720Sstevel@tonic-gate #include	<sys/ddi_implfuncs.h>
730Sstevel@tonic-gate 
740Sstevel@tonic-gate #include	<sys/dld.h>
750Sstevel@tonic-gate 
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate  * Debug Macros
780Sstevel@tonic-gate  */
790Sstevel@tonic-gate int	strplumbdebug = 0;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate #define	DBG0(_f) \
820Sstevel@tonic-gate 	if (strplumbdebug != 0) \
830Sstevel@tonic-gate 		printf("strplumb: " _f)
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #define	DBG1(_f, _a) \
860Sstevel@tonic-gate 	if (strplumbdebug != 0) \
870Sstevel@tonic-gate 		printf("strplumb: " _f, (_a))
880Sstevel@tonic-gate 
890Sstevel@tonic-gate #define	DBG2(_f, _a, _b) \
900Sstevel@tonic-gate 	if (strplumbdebug != 0) \
910Sstevel@tonic-gate 		printf("strplumb: " _f, (_a), (_b))
920Sstevel@tonic-gate 
930Sstevel@tonic-gate #define	DBG3(_f, _a, _b, _c) \
940Sstevel@tonic-gate 	if (strplumbdebug != 0) \
950Sstevel@tonic-gate 		printf("strplumb: " _f, (_a), (_b), (_c))
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate  * Module linkage information for the kernel.
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate #define	STRPLUMB_IDENT	"STREAMS Plumbing Module v%I%"
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate static struct modlmisc modlmisc = {
1030Sstevel@tonic-gate 	&mod_miscops,
1040Sstevel@tonic-gate 	STRPLUMB_IDENT
1050Sstevel@tonic-gate };
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate static struct modlinkage modlinkage = {
1080Sstevel@tonic-gate 	MODREV_1,
1090Sstevel@tonic-gate 	&modlmisc,
1100Sstevel@tonic-gate 	NULL
1110Sstevel@tonic-gate };
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate int
1140Sstevel@tonic-gate _init(void)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate int
1200Sstevel@tonic-gate _fini(void)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate int
1260Sstevel@tonic-gate _info(struct modinfo *modinfop)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate #define	ARP		"arp"
1320Sstevel@tonic-gate #define	TCP		"tcp"
1330Sstevel@tonic-gate #define	TCP6		"tcp6"
1340Sstevel@tonic-gate #define	UDP		"udp"
1350Sstevel@tonic-gate #define	UDP6		"udp6"
1360Sstevel@tonic-gate #define	SCTP		"sctp"
1370Sstevel@tonic-gate #define	SCTP6		"sctp6"
1380Sstevel@tonic-gate #define	ICMP		"icmp"
1390Sstevel@tonic-gate #define	ICMP6		"icmp6"
1400Sstevel@tonic-gate #define	IP		"ip"
1410Sstevel@tonic-gate #define	IP6		"ip6"
1420Sstevel@tonic-gate #define	TIMOD		"timod"
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate #define	UDPDEV		"/devices/pseudo/udp@0:udp"
1450Sstevel@tonic-gate #define	TCP6DEV		"/devices/pseudo/tcp6@0:tcp6"
1460Sstevel@tonic-gate #define	SCTP6DEV	"/devices/pseudo/sctp6@0:sctp6"
1470Sstevel@tonic-gate #define	IP6DEV		"/devices/pseudo/ip6@0:ip6"
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate typedef struct strplumb_modspec {
1500Sstevel@tonic-gate 	char	*sm_type;
1510Sstevel@tonic-gate 	char	*sm_name;
1520Sstevel@tonic-gate } strplumb_modspec_t;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate static strplumb_modspec_t	strplumb_modlist[] = {
1550Sstevel@tonic-gate 	{ "drv", DLD_DRIVER_NAME },
1560Sstevel@tonic-gate 	{ "drv", IP },
1570Sstevel@tonic-gate 	{ "drv", IP6 },
1580Sstevel@tonic-gate 	{ "drv", TCP },
1590Sstevel@tonic-gate 	{ "drv", TCP6 },
1600Sstevel@tonic-gate 	{ "drv", UDP },
1610Sstevel@tonic-gate 	{ "drv", UDP6 },
1620Sstevel@tonic-gate 	{ "drv", SCTP },
1630Sstevel@tonic-gate 	{ "drv", SCTP6 },
1640Sstevel@tonic-gate 	{ "drv", ICMP },
1650Sstevel@tonic-gate 	{ "drv", ICMP6 },
1660Sstevel@tonic-gate 	{ "drv", ARP },
1670Sstevel@tonic-gate 	{ "strmod", TIMOD }
1680Sstevel@tonic-gate };
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate  * Called from swapgeneric.c:loadrootmodules() in the network boot case.
1720Sstevel@tonic-gate  */
1730Sstevel@tonic-gate int
1740Sstevel@tonic-gate strplumb_load(void)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate 	uint_t			i;
1770Sstevel@tonic-gate 	strplumb_modspec_t	*p;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	DBG0("loading modules\n");
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	for (i = 0, p = strplumb_modlist;
1820Sstevel@tonic-gate 	    i < sizeof (strplumb_modlist) / sizeof (strplumb_modlist[0]);
1830Sstevel@tonic-gate 	    i++, p++) {
1840Sstevel@tonic-gate 		if (modloadonly(p->sm_type, p->sm_name) < 0) {
1850Sstevel@tonic-gate 			printf("strplumb: failed to load %s/%s\n",
1860Sstevel@tonic-gate 			    p->sm_type, p->sm_name);
1870Sstevel@tonic-gate 			return (EFAULT);
1880Sstevel@tonic-gate 		}
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	return (0);
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate static int
1950Sstevel@tonic-gate strplumb_init(void)
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate 	uint_t			i;
1980Sstevel@tonic-gate 	strplumb_modspec_t	*p;
1990Sstevel@tonic-gate 	int			err;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	DBG0("initializing modules\n");
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	for (i = 0, p = strplumb_modlist;
2040Sstevel@tonic-gate 	    i < sizeof (strplumb_modlist) / sizeof (strplumb_modlist[0]);
2050Sstevel@tonic-gate 	    i++, p++) {
2060Sstevel@tonic-gate 		if (strcmp(p->sm_type, "drv") == 0)
2070Sstevel@tonic-gate 			err = (i_ddi_attach_pseudo_node(p->sm_name) != NULL) ?
2080Sstevel@tonic-gate 			    0 : EFAULT;
2090Sstevel@tonic-gate 		else
2100Sstevel@tonic-gate 			err = (modload(p->sm_type, p->sm_name) < 0) ?
2110Sstevel@tonic-gate 			    EFAULT : 0;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 		if (err != 0)  {
2140Sstevel@tonic-gate 			printf("strplumb: failed to initialize %s/%s\n",
2150Sstevel@tonic-gate 			    p->sm_type, p->sm_name);
2160Sstevel@tonic-gate 			return (err);
2170Sstevel@tonic-gate 		}
2180Sstevel@tonic-gate 	}
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	return (0);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate static int
2240Sstevel@tonic-gate strplumb_autopush(void)
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate 	major_t		maj;
2270Sstevel@tonic-gate 	minor_t		min;
2280Sstevel@tonic-gate 	char		*mods[5];
2290Sstevel@tonic-gate 	uint_t		anchor = 1;
2300Sstevel@tonic-gate 	int		err;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	min = (minor_t)-1;
2330Sstevel@tonic-gate 	mods[1] = NULL;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	/*
2360Sstevel@tonic-gate 	 * UDP
2370Sstevel@tonic-gate 	 */
2380Sstevel@tonic-gate 	DBG0("setting up udp autopush\n");
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	mods[0] = UDP;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	maj = ddi_name_to_major(UDP);
2430Sstevel@tonic-gate 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, &anchor,
2440Sstevel@tonic-gate 	    mods)) != 0) {
2450Sstevel@tonic-gate 		printf("strplumb: kstr_autopush(SET/UDP) failed: %d\n", err);
2460Sstevel@tonic-gate 		return (err);
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	maj = ddi_name_to_major(UDP6);
2500Sstevel@tonic-gate 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, &anchor,
2510Sstevel@tonic-gate 	    mods)) != 0) {
2520Sstevel@tonic-gate 		printf("strplumb: kstr_autopush(SET/UDP6) failed: %d\n", err);
2530Sstevel@tonic-gate 		return (err);
2540Sstevel@tonic-gate 	}
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	/*
2570Sstevel@tonic-gate 	 * ICMP
2580Sstevel@tonic-gate 	 */
2590Sstevel@tonic-gate 	DBG0("setting up icmp autopush\n");
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	mods[0] = ICMP;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	maj = ddi_name_to_major(ICMP);
2640Sstevel@tonic-gate 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, NULL,
2650Sstevel@tonic-gate 	    mods)) != 0) {
2660Sstevel@tonic-gate 		printf("strplumb: kstr_autopush(SET/ICMP) failed: %d\n", err);
2670Sstevel@tonic-gate 		return (err);
2680Sstevel@tonic-gate 	}
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	maj = ddi_name_to_major(ICMP6);
2710Sstevel@tonic-gate 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, NULL,
2720Sstevel@tonic-gate 	    mods)) != 0) {
2730Sstevel@tonic-gate 		printf("strplumb: kstr_autopush(SET/ICMP6) failed: %d\n", err);
2740Sstevel@tonic-gate 		return (err);
2750Sstevel@tonic-gate 	}
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	/*
2780Sstevel@tonic-gate 	 * ARP
2790Sstevel@tonic-gate 	 */
2800Sstevel@tonic-gate 	DBG0("setting up arp autopush\n");
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	mods[0] = ARP;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	maj = ddi_name_to_major(ARP);
2850Sstevel@tonic-gate 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, &anchor,
2860Sstevel@tonic-gate 	    mods)) != 0) {
2870Sstevel@tonic-gate 		printf("strplumb: kstr_autopush(SET/ARP) failed: %d\n", err);
2880Sstevel@tonic-gate 		return (err);
2890Sstevel@tonic-gate 	}
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	return (0);
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate static int
2950Sstevel@tonic-gate strplumb_sctpq(ldi_ident_t li)
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate 	ldi_handle_t	lh = NULL;
2980Sstevel@tonic-gate 	int		err;
2990Sstevel@tonic-gate 	int		rval;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	DBG0("configuring SCTP default queue\n");
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	if ((err = ldi_open_by_name(SCTP6DEV, FREAD|FWRITE, CRED(), &lh,
3040Sstevel@tonic-gate 	    li)) != 0) {
3050Sstevel@tonic-gate 		printf("strplumb: open of SCTP6DEV failed: %d\n", err);
3060Sstevel@tonic-gate 		return (err);
3070Sstevel@tonic-gate 	}
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, SCTP_IOC_DEFAULT_Q, (intptr_t)0, FKIOCTL,
3100Sstevel@tonic-gate 	    CRED(), &rval)) != 0) {
3110Sstevel@tonic-gate 		printf("strplumb: failed to set SCTP default queue: %d\n",
3120Sstevel@tonic-gate 		    err);
3130Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
3140Sstevel@tonic-gate 		return (err);
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	return (0);
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate static int
3210Sstevel@tonic-gate strplumb_tcpq(ldi_ident_t li)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate 	ldi_handle_t	lh = NULL;
3240Sstevel@tonic-gate 	ldi_handle_t	ip_lh = NULL;
3250Sstevel@tonic-gate 	int		err;
3260Sstevel@tonic-gate 	int		rval;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	DBG0("configuring TCP default queue\n");
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	/*
3310Sstevel@tonic-gate 	 * We open IP6DEV here because we need to have it open to in
3320Sstevel@tonic-gate 	 * order to open TCP6DEV successfully.
3330Sstevel@tonic-gate 	 */
3340Sstevel@tonic-gate 	if ((err = ldi_open_by_name(IP6DEV, FREAD|FWRITE, CRED(), &ip_lh,
3350Sstevel@tonic-gate 	    li)) != 0) {
3360Sstevel@tonic-gate 		printf("strplumb: open of IP6DEV failed: %d\n", err);
3370Sstevel@tonic-gate 		return (err);
3380Sstevel@tonic-gate 	}
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	/*
3410Sstevel@tonic-gate 	 * We set the tcp default queue to IPv6 because IPv4 falls back to
3420Sstevel@tonic-gate 	 * IPv6 when it can't find a client, but IPv6 does not fall back to
3430Sstevel@tonic-gate 	 * IPv4.
3440Sstevel@tonic-gate 	 */
3450Sstevel@tonic-gate 	if ((err = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, CRED(), &lh,
3460Sstevel@tonic-gate 	    li)) != 0) {
3470Sstevel@tonic-gate 		printf("strplumb: open of TCP6DEV failed: %d\n", err);
3480Sstevel@tonic-gate 		goto done;
3490Sstevel@tonic-gate 	}
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, (intptr_t)0, FKIOCTL,
3520Sstevel@tonic-gate 	    CRED(), &rval)) != 0) {
3530Sstevel@tonic-gate 		printf("strplumb: failed to set TCP default queue: %d\n",
3540Sstevel@tonic-gate 		    err);
3550Sstevel@tonic-gate 		goto done;
3560Sstevel@tonic-gate 	}
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate done:
3590Sstevel@tonic-gate 	(void) ldi_close(ip_lh, FREAD|FWRITE, CRED());
3600Sstevel@tonic-gate 	return (err);
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate static int
3640Sstevel@tonic-gate create_gldv3_linkobj(ldi_ident_t li, char *driver, int instance)
3650Sstevel@tonic-gate {
3660Sstevel@tonic-gate 	ldi_handle_t		lh;
3670Sstevel@tonic-gate 	int			err;
3680Sstevel@tonic-gate 	struct strioctl		iocb;
3690Sstevel@tonic-gate 	int			rval;
3700Sstevel@tonic-gate 	dld_ioc_create_t	dic;
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	/*
3730Sstevel@tonic-gate 	 * gld v3 driver. open the DLD control node.
3740Sstevel@tonic-gate 	 */
3750Sstevel@tonic-gate 	if ((err = ldi_open_by_name(DLD_CONTROL_DEV, FREAD|FWRITE, CRED(), &lh,
3760Sstevel@tonic-gate 	    li)) != 0) {
3770Sstevel@tonic-gate 		printf("strplumb: open CTLDEV failed: %d\n", err);
3780Sstevel@tonic-gate 		return (ENXIO);
3790Sstevel@tonic-gate 	}
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	(void) snprintf(dic.dic_name, sizeof (dic.dic_name) - 1, "%s%d",
3820Sstevel@tonic-gate 	    driver, instance);
3830Sstevel@tonic-gate 	(void) snprintf(dic.dic_dev, sizeof (dic.dic_dev) - 1, "%s%d",
3840Sstevel@tonic-gate 	    driver, instance);
3850Sstevel@tonic-gate 	dic.dic_port = 0;
3860Sstevel@tonic-gate 	dic.dic_vid = 0;
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	iocb.ic_cmd = DLDIOCCREATE;
3890Sstevel@tonic-gate 	iocb.ic_timout = 15;
3900Sstevel@tonic-gate 	iocb.ic_len = sizeof (dic);
3910Sstevel@tonic-gate 	iocb.ic_dp = (char *)&dic;
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	/*
3940Sstevel@tonic-gate 	 * Try to create a data-link interface with the same name as the
3950Sstevel@tonic-gate 	 * device.
3960Sstevel@tonic-gate 	 */
3970Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(),
3980Sstevel@tonic-gate 	    &rval)) != 0) {
3990Sstevel@tonic-gate 		cmn_err(CE_WARN, "strplumb: DLDIOCCREATE failed: %d\n", err);
4000Sstevel@tonic-gate 		return (ENXIO);
4010Sstevel@tonic-gate 	}
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
4040Sstevel@tonic-gate 	return (0);
4050Sstevel@tonic-gate }
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate /*
4080Sstevel@tonic-gate  * Can be set in /etc/system in the case of local booting. See comment below.
4090Sstevel@tonic-gate  */
4100Sstevel@tonic-gate char	*ndev_name = 0;
4110Sstevel@tonic-gate int	ndev_unit = 0;
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate /*
4140Sstevel@tonic-gate  * If we booted diskless then strplumb() will have been called from
4150Sstevel@tonic-gate  * swapgeneric.c:rootconf(). All we can do in that case is plumb the
4160Sstevel@tonic-gate  * network device that we booted from.
4170Sstevel@tonic-gate  *
4180Sstevel@tonic-gate  * If we booted from a local disk, we will have been called from main(),
4190Sstevel@tonic-gate  * and normally we defer the plumbing of interfaces until network/physical.
4200Sstevel@tonic-gate  * This can be overridden by setting "ndev_name" in /etc/system.
4210Sstevel@tonic-gate  */
4220Sstevel@tonic-gate static int
4230Sstevel@tonic-gate resolve_boot_path(ldi_ident_t li)
4240Sstevel@tonic-gate {
4250Sstevel@tonic-gate 	char			*devpath = NULL;
4260Sstevel@tonic-gate 	dev_info_t		*dip;
4270Sstevel@tonic-gate 	const char		*driver;
4280Sstevel@tonic-gate 	int			instance;
4290Sstevel@tonic-gate 	int			err;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	if (strncmp(rootfs.bo_fstype, "nfs", 3) == 0)
4320Sstevel@tonic-gate 		devpath = rootfs.bo_name;
4330Sstevel@tonic-gate #ifndef __sparc
4340Sstevel@tonic-gate 	else
4350Sstevel@tonic-gate 		devpath = strplumb_get_netdev_path();
4360Sstevel@tonic-gate #endif
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	if (devpath != NULL) {
4390Sstevel@tonic-gate 		DBG1("resolving boot-path: %s\n", devpath);
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 		/*
4420Sstevel@tonic-gate 		 * Hold the devi since this is the root device.
4430Sstevel@tonic-gate 		 */
4440Sstevel@tonic-gate 		if ((dip = e_ddi_hold_devi_by_path(devpath, 0)) == NULL) {
4450Sstevel@tonic-gate 			printf("strplumb: unable to hold root device: %s\n",
4460Sstevel@tonic-gate 			    devpath);
4470Sstevel@tonic-gate 			return (ENXIO);
4480Sstevel@tonic-gate 		}
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 		driver = ddi_driver_name(dip);
4510Sstevel@tonic-gate 		instance = ddi_get_instance(dip);
4520Sstevel@tonic-gate 	} else {
4530Sstevel@tonic-gate 		if (ndev_name == NULL)
4540Sstevel@tonic-gate 			return (ENODEV);
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 		DBG2("using ndev_name (%s) ndev_unit (%d)\n", ndev_name,
4570Sstevel@tonic-gate 		    ndev_unit);
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 		if (i_ddi_attach_hw_nodes(ndev_name) != DDI_SUCCESS) {
4600Sstevel@tonic-gate 			printf("strplumb: cannot load ndev_name '%s'\n",
4610Sstevel@tonic-gate 			    ndev_name);
4620Sstevel@tonic-gate 			return (ENXIO);
4630Sstevel@tonic-gate 		}
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 		driver = ndev_name;
4660Sstevel@tonic-gate 		instance = ndev_unit;
4670Sstevel@tonic-gate 	}
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	/* legcy driver, use clone to open */
4700Sstevel@tonic-gate 	if (!GLDV3_DRV(ddi_name_to_major((char *)driver))) {
4710Sstevel@tonic-gate 		(void) snprintf(rootfs.bo_devname, BO_MAXOBJNAME,
4720Sstevel@tonic-gate 		    "/devices/pseudo/clone@0:%s", driver);
4730Sstevel@tonic-gate 		(void) snprintf(rootfs.bo_ifname, BO_MAXOBJNAME, "%s%d",
4740Sstevel@tonic-gate 		    driver, instance);
4750Sstevel@tonic-gate 		rootfs.bo_ppa = instance;
4760Sstevel@tonic-gate 		return (0);
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	/* GLDv3: create link object so open with succeed later */
4800Sstevel@tonic-gate 	err = create_gldv3_linkobj(li, (char *)driver, instance);
4810Sstevel@tonic-gate 	if (err != 0) {
4820Sstevel@tonic-gate 		return (err);
4830Sstevel@tonic-gate 	}
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	(void) snprintf(rootfs.bo_devname, BO_MAXOBJNAME,
4860Sstevel@tonic-gate 	    "/devices/pseudo/dld@0:%s", driver);
4870Sstevel@tonic-gate 	(void) snprintf(rootfs.bo_ifname, BO_MAXOBJNAME, "%s%d",
4880Sstevel@tonic-gate 	    driver, instance);
4890Sstevel@tonic-gate 	rootfs.bo_ppa = instance;
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	return (err);
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate static int
4950Sstevel@tonic-gate getifflags(ldi_handle_t lh, struct lifreq *lifrp)
4960Sstevel@tonic-gate {
4970Sstevel@tonic-gate 	struct strioctl	iocb;
4980Sstevel@tonic-gate 	int		rval;
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	iocb.ic_cmd = SIOCGLIFFLAGS;
5010Sstevel@tonic-gate 	iocb.ic_timout = 15;
5020Sstevel@tonic-gate 	iocb.ic_len = sizeof (struct lifreq);
5030Sstevel@tonic-gate 	iocb.ic_dp = (char *)lifrp;
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate }
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate static int
5100Sstevel@tonic-gate setifname(ldi_handle_t lh, struct lifreq *lifrp)
5110Sstevel@tonic-gate {
5120Sstevel@tonic-gate 	struct strioctl	iocb;
5130Sstevel@tonic-gate 	int		rval;
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	iocb.ic_cmd = SIOCSLIFNAME;
5160Sstevel@tonic-gate 	iocb.ic_timout = 15;
5170Sstevel@tonic-gate 	iocb.ic_len = sizeof (struct lifreq);
5180Sstevel@tonic-gate 	iocb.ic_dp = (char *)lifrp;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
5210Sstevel@tonic-gate }
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate static int
5240Sstevel@tonic-gate strplumb_dev(ldi_ident_t li)
5250Sstevel@tonic-gate {
5260Sstevel@tonic-gate 	ldi_handle_t	lh = NULL;
5270Sstevel@tonic-gate 	ldi_handle_t	mux_lh = NULL;
5280Sstevel@tonic-gate 	int		err;
5290Sstevel@tonic-gate 	struct lifreq	lifr;
5300Sstevel@tonic-gate 	struct ifreq	ifr;
5310Sstevel@tonic-gate 	int		rval;
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	bzero(&lifr, sizeof (struct lifreq));
5340Sstevel@tonic-gate 	bzero(&ifr, sizeof (ifr));
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	/*
5370Sstevel@tonic-gate 	 * Now set up the links. Ultimately, we should have two streams
5380Sstevel@tonic-gate 	 * permanently linked underneath UDP (which is actually IP with UDP
5390Sstevel@tonic-gate 	 * autopushed). One stream consists of the ARP-[ifname] combination,
5400Sstevel@tonic-gate 	 * while the other consists of ARP-IP-[ifname]. The second combination
5410Sstevel@tonic-gate 	 * seems a little weird, but is linked underneath UDP just to keep it
5420Sstevel@tonic-gate 	 * around.
5430Sstevel@tonic-gate 	 *
5440Sstevel@tonic-gate 	 * We pin underneath UDP here to match what is done in ifconfig(1m);
5450Sstevel@tonic-gate 	 * otherwise, ifconfig will be unable to unplumb the stream (the major
5460Sstevel@tonic-gate 	 * number and mux id must both match for a successful I_PUNLINK).
5470Sstevel@tonic-gate 	 *
5480Sstevel@tonic-gate 	 * There are subtleties in the plumbing which make it essential to
5490Sstevel@tonic-gate 	 * follow the logic used in ifconfig(1m) very closely.
5500Sstevel@tonic-gate 	 */
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	/*
5530Sstevel@tonic-gate 	 * Plumb UDP-ARP-IP-<dev>
5540Sstevel@tonic-gate 	 */
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
5570Sstevel@tonic-gate 	    &lh, li)) != 0) {
5580Sstevel@tonic-gate 		printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
5590Sstevel@tonic-gate 		    err);
5600Sstevel@tonic-gate 		goto done;
5610Sstevel@tonic-gate 	}
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)IP, FKIOCTL, CRED(),
5650Sstevel@tonic-gate 	    &rval)) != 0) {
5660Sstevel@tonic-gate 		printf("strplumb: push IP failed: %d\n", err);
5670Sstevel@tonic-gate 		goto done;
5680Sstevel@tonic-gate 	}
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	if ((err = getifflags(lh, &lifr)) != 0)
5710Sstevel@tonic-gate 		goto done;
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	lifr.lifr_flags |= IFF_IPV4;
5740Sstevel@tonic-gate 	lifr.lifr_flags &= ~IFF_IPV6;
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)ARP, FKIOCTL, CRED(),
5770Sstevel@tonic-gate 	    &rval)) != 0) {
5780Sstevel@tonic-gate 		printf("strplumb: push ARP failed: %d\n", err);
5790Sstevel@tonic-gate 		goto done;
5800Sstevel@tonic-gate 	}
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, rootfs.bo_ifname,
5830Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
5840Sstevel@tonic-gate 	lifr.lifr_ppa = rootfs.bo_ppa;
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	if ((err = setifname(lh, &lifr)) != 0)
5870Sstevel@tonic-gate 		goto done;
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 	/* Get the flags and check if ARP is needed */
5900Sstevel@tonic-gate 	if ((err = getifflags(lh, &lifr)) != 0) {
5910Sstevel@tonic-gate 		printf("strplumb: getifflags %s IP failed, error %d\n",
5920Sstevel@tonic-gate 		    lifr.lifr_name, err);
5930Sstevel@tonic-gate 		goto done;
5940Sstevel@tonic-gate 	}
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 	/* Pop out ARP if not needed */
5970Sstevel@tonic-gate 	if (lifr.lifr_flags & IFF_NOARP) {
5980Sstevel@tonic-gate 		err = ldi_ioctl(lh, I_POP, (intptr_t)0, FKIOCTL, CRED(),
5990Sstevel@tonic-gate 		    &rval);
6000Sstevel@tonic-gate 		if (err != 0) {
6010Sstevel@tonic-gate 			printf("strplumb: pop ARP failed, error %d\n", err);
6020Sstevel@tonic-gate 			goto done;
6030Sstevel@tonic-gate 		}
6040Sstevel@tonic-gate 	}
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	if ((err = ldi_open_by_name(UDPDEV, FREAD|FWRITE, CRED(), &mux_lh,
6070Sstevel@tonic-gate 	    li)) != 0) {
6080Sstevel@tonic-gate 		printf("strplumb: open of UDPDEV failed: %d\n", err);
6090Sstevel@tonic-gate 		goto done;
6100Sstevel@tonic-gate 	}
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
6130Sstevel@tonic-gate 	    FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
6140Sstevel@tonic-gate 	    &(ifr.ifr_ip_muxid))) != 0) {
6150Sstevel@tonic-gate 		printf("strplumb: plink UDP-ARP-IP-%s failed: %d\n",
6160Sstevel@tonic-gate 		    rootfs.bo_ifname, err);
6170Sstevel@tonic-gate 		goto done;
6180Sstevel@tonic-gate 	}
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	DBG2("UDP-ARP-IP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_ip_muxid);
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
6230Sstevel@tonic-gate 	lh = NULL;
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	/*
6260Sstevel@tonic-gate 	 * Plumb UDP-ARP-<dev>
6270Sstevel@tonic-gate 	 */
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
6300Sstevel@tonic-gate 	    &lh, li)) != 0) {
6310Sstevel@tonic-gate 		printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
6320Sstevel@tonic-gate 		    err);
6330Sstevel@tonic-gate 		goto done;
6340Sstevel@tonic-gate 	}
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)ARP, FKIOCTL, CRED(),
6370Sstevel@tonic-gate 	    &rval)) != 0) {
6380Sstevel@tonic-gate 		printf("strplumb: push ARP failed: %d\n", err);
6390Sstevel@tonic-gate 		goto done;
6400Sstevel@tonic-gate 	}
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	if ((err = setifname(lh, &lifr)) != 0)
6430Sstevel@tonic-gate 		goto done;
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
6460Sstevel@tonic-gate 	    FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
6470Sstevel@tonic-gate 	    &(ifr.ifr_arp_muxid))) != 0) {
6480Sstevel@tonic-gate 		printf("strplumb: plink UDP-ARP-%s failed: %d\n",
6490Sstevel@tonic-gate 		    rootfs.bo_ifname, err);
6500Sstevel@tonic-gate 		goto done;
6510Sstevel@tonic-gate 	}
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	DBG2("UDP-ARP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_arp_muxid);
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	/*
6560Sstevel@tonic-gate 	 * Cache the mux ids.
6570Sstevel@tonic-gate 	 */
6580Sstevel@tonic-gate 	(void) strlcpy(ifr.ifr_name, rootfs.bo_ifname, sizeof (ifr.ifr_name));
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, SIOCSIFMUXID, (intptr_t)&ifr, FKIOCTL,
6610Sstevel@tonic-gate 	    CRED(), &rval)) != 0) {
6620Sstevel@tonic-gate 		printf("strplumb: SIOCSIFMUXID failed: %d\n", err);
6630Sstevel@tonic-gate 		goto done;
6640Sstevel@tonic-gate 	}
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate done:
6670Sstevel@tonic-gate 	if (lh != NULL)
6680Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	if (mux_lh != NULL)
6710Sstevel@tonic-gate 		(void) ldi_close(mux_lh, FREAD|FWRITE, CRED());
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	return (err);
6740Sstevel@tonic-gate }
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate /*
6770Sstevel@tonic-gate  * Do streams plumbing for internet protocols.
6780Sstevel@tonic-gate  */
6790Sstevel@tonic-gate int
6800Sstevel@tonic-gate strplumb(void)
6810Sstevel@tonic-gate {
6820Sstevel@tonic-gate 	ldi_ident_t	li;
6830Sstevel@tonic-gate 	int		err;
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	if ((err = strplumb_init()) != 0)
6860Sstevel@tonic-gate 		return (err);
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	if ((err = strplumb_autopush()) != 0)
6890Sstevel@tonic-gate 		return (err);
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	if ((err = ldi_ident_from_mod(&modlinkage, &li)) != 0)
6920Sstevel@tonic-gate 		return (err);
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	if ((err = strplumb_sctpq(li)) != 0)
6950Sstevel@tonic-gate 		goto done;
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	if ((err = strplumb_tcpq(li)) != 0)
6980Sstevel@tonic-gate 		goto done;
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	if ((err = resolve_boot_path(li)) != 0)
7010Sstevel@tonic-gate 		goto done;
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	DBG1("rootfs.bo_devname: %s\n", rootfs.bo_devname);
7040Sstevel@tonic-gate 	DBG1("rootfs.bo_ifname: %s\n", rootfs.bo_ifname);
7050Sstevel@tonic-gate 	DBG1("rootfs.bo_ppa: %d\n", rootfs.bo_ppa);
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	if ((err = strplumb_dev(li)) != 0)
7080Sstevel@tonic-gate 		goto done;
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate done:
7110Sstevel@tonic-gate 	ldi_ident_release(li);
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	return (err);
7140Sstevel@tonic-gate }
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate /* multiboot:  diskless boot interface discovery */
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate #ifndef	__sparc
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate static uchar_t boot_macaddr[16];
7210Sstevel@tonic-gate static int boot_maclen;
7220Sstevel@tonic-gate static uchar_t *getmacaddr(dev_info_t *dip, int *maclen);
7230Sstevel@tonic-gate static int matchmac(dev_info_t *dip, void *arg);
7240Sstevel@tonic-gate extern int dl_attach(ldi_handle_t lh, int unit);
7250Sstevel@tonic-gate extern int dl_bind(ldi_handle_t lh, uint_t sap, uint_t max_conn,
7260Sstevel@tonic-gate     uint_t service, uint_t conn_mgmt);
7270Sstevel@tonic-gate extern int dl_phys_addr(ldi_handle_t lh, struct ether_addr *eaddr);
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate char *
7300Sstevel@tonic-gate strplumb_get_netdev_path(void)
7310Sstevel@tonic-gate {
7320Sstevel@tonic-gate 	char *macstr, *devpath = NULL;
7330Sstevel@tonic-gate 	uchar_t *bootp;
7340Sstevel@tonic-gate 	uint_t bootp_len, len;
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
7370Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, BP_BOOT_MAC, &macstr) == DDI_SUCCESS) {
7380Sstevel@tonic-gate 		/*
7390Sstevel@tonic-gate 		 * hard coded ether mac len for booting floppy on
7400Sstevel@tonic-gate 		 * machines with old cards
7410Sstevel@tonic-gate 		 */
7420Sstevel@tonic-gate 		boot_maclen = ether_aton(macstr, boot_macaddr);
7430Sstevel@tonic-gate 		if (boot_maclen != 6) {
7440Sstevel@tonic-gate 			cmn_err(CE_WARN,
7450Sstevel@tonic-gate 			    "malformed boot_mac property, %d bytes",
7460Sstevel@tonic-gate 			    boot_maclen);
7470Sstevel@tonic-gate 		}
7480Sstevel@tonic-gate 		ddi_prop_free(macstr);
7490Sstevel@tonic-gate 	} else if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, ddi_root_node(),
7500Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, BP_BOOTP_RESPONSE, &bootp, &bootp_len)
7510Sstevel@tonic-gate 	    == DDI_SUCCESS) {
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 		/*
7540Sstevel@tonic-gate 		 * These offsets are defined by dhcp standard
7550Sstevel@tonic-gate 		 * Should use structure offsets
7560Sstevel@tonic-gate 		 */
7570Sstevel@tonic-gate 		boot_maclen = *(bootp + 2);
7580Sstevel@tonic-gate 		ASSERT(boot_maclen <= 16);
7590Sstevel@tonic-gate 		(void) bcopy(bootp + 28, boot_macaddr, boot_maclen);
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 		/* encode to ascii string to match what sparc OBP exports */
762*129Ssetje 		dhcack = kmem_zalloc(bootp_len * 2 + IFNAMSIZ + 2, KM_SLEEP);
7630Sstevel@tonic-gate 		(void) octet_to_hexascii(bootp, bootp_len, dhcack + IFNAMSIZ,
7640Sstevel@tonic-gate 		    &len);
7650Sstevel@tonic-gate 		ASSERT(len < bootp_len * 2 + 2);
7660Sstevel@tonic-gate 		ddi_prop_free(bootp);
7670Sstevel@tonic-gate 	} else
7680Sstevel@tonic-gate 		return (NULL);
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	ddi_walk_devs(ddi_root_node(), matchmac, (void *)&devpath);
7710Sstevel@tonic-gate 	return (devpath);
7720Sstevel@tonic-gate }
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate /*
7750Sstevel@tonic-gate  * Get boot path from the boot_mac address
7760Sstevel@tonic-gate  */
7770Sstevel@tonic-gate /*ARGSUSED*/
7780Sstevel@tonic-gate static int
7790Sstevel@tonic-gate matchmac(dev_info_t *dip, void *arg)
7800Sstevel@tonic-gate {
7810Sstevel@tonic-gate 	char **devpathp = (char **)arg;
7820Sstevel@tonic-gate 	char *model_str;
7830Sstevel@tonic-gate 	uchar_t *macaddr;
7840Sstevel@tonic-gate 	int maclen;
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 	/* XXX Should use "device-type" per IEEE 1275 */
7870Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0,
7880Sstevel@tonic-gate 	    "model", &model_str) != DDI_SUCCESS)
7890Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	if (strcmp(model_str, "Ethernet controller") != 0) {
7920Sstevel@tonic-gate 		ddi_prop_free(model_str);
7930Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7940Sstevel@tonic-gate 	}
7950Sstevel@tonic-gate 	ddi_prop_free(model_str);
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	/* We have a network device now */
7980Sstevel@tonic-gate 	if (i_ddi_attach_node_hierarchy(dip) != DDI_SUCCESS) {
7990Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
8000Sstevel@tonic-gate 	}
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	ASSERT(boot_maclen != 0);
8030Sstevel@tonic-gate 	macaddr = getmacaddr(dip, &maclen);
8040Sstevel@tonic-gate 	if (macaddr == NULL)
8050Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	if (maclen != boot_maclen ||
8080Sstevel@tonic-gate 	    bcmp(macaddr, boot_macaddr, maclen) != 0) {
8090Sstevel@tonic-gate 		kmem_free(macaddr, maclen);
8100Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
8110Sstevel@tonic-gate 	}
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	/* found hardware with the mac address */
8140Sstevel@tonic-gate 	(void) localetheraddr((struct ether_addr *)macaddr, NULL);
8150Sstevel@tonic-gate 	kmem_free(macaddr, maclen);
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	*devpathp = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8180Sstevel@tonic-gate 	(void) ddi_pathname(dip, *devpathp);
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	/* fill in the name portion of dhcack */
8210Sstevel@tonic-gate 	if (dhcack)
8220Sstevel@tonic-gate 		(void) snprintf(dhcack, IFNAMSIZ, "%s%d",
8230Sstevel@tonic-gate 		    ddi_driver_name(dip), i_ddi_devi_get_ppa(dip));
8240Sstevel@tonic-gate 	return (DDI_WALK_TERMINATE);
8250Sstevel@tonic-gate }
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate static uchar_t *
8280Sstevel@tonic-gate getmacaddr_gldv3(char *drv, int inst, int *maclenp)
8290Sstevel@tonic-gate {
8300Sstevel@tonic-gate 	char ifname[16];
8310Sstevel@tonic-gate 	mac_handle_t mh;
8320Sstevel@tonic-gate 	uchar_t *macaddr;
8330Sstevel@tonic-gate 
8340Sstevel@tonic-gate 	(void) snprintf(ifname, sizeof (ifname), "%s%d", drv, inst);
8350Sstevel@tonic-gate 	if (mac_open(ifname, 0, &mh) < 0) {
8360Sstevel@tonic-gate 		return (NULL);
8370Sstevel@tonic-gate 	}
8380Sstevel@tonic-gate 	*maclenp = sizeof (struct ether_addr);
8390Sstevel@tonic-gate 	macaddr = kmem_alloc(*maclenp, KM_SLEEP);
8400Sstevel@tonic-gate 	mac_unicst_get(mh, macaddr);
8410Sstevel@tonic-gate 	mac_close(mh);
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	return (macaddr);
8440Sstevel@tonic-gate }
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate static uchar_t *
8470Sstevel@tonic-gate getmacaddr(dev_info_t *dip, int *maclenp)
8480Sstevel@tonic-gate {
8490Sstevel@tonic-gate 	int rc, ppa;
8500Sstevel@tonic-gate 	ldi_ident_t li;
8510Sstevel@tonic-gate 	ldi_handle_t lh;
8520Sstevel@tonic-gate 	char *drv_name = (char *)ddi_driver_name(dip);
8530Sstevel@tonic-gate 	char *clonepath;
8540Sstevel@tonic-gate 	uchar_t *macaddr = NULL;
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 	/* a simpler way to get mac address for GLDv3 drivers */
8570Sstevel@tonic-gate 	if (GLDV3_DRV(ddi_name_to_major(drv_name))) {
8580Sstevel@tonic-gate 		return (getmacaddr_gldv3(drv_name, ddi_get_instance(dip),
8590Sstevel@tonic-gate 		    maclenp));
8600Sstevel@tonic-gate 	}
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	if (rc = ldi_ident_from_mod(&modlinkage, &li)) {
8630Sstevel@tonic-gate 		cmn_err(CE_WARN,
8640Sstevel@tonic-gate 		    "getmacaddr: ldi_ident_from_mod failed: %d\n", rc);
8650Sstevel@tonic-gate 		return (NULL);
8660Sstevel@tonic-gate 	}
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 	clonepath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
8690Sstevel@tonic-gate 	(void) snprintf(clonepath, MAXPATHLEN,
8700Sstevel@tonic-gate 	    "/devices/pseudo/clone@0:%s", drv_name);
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 	rc = ldi_open_by_name(clonepath, FREAD|FWRITE, CRED(), &lh, li);
8730Sstevel@tonic-gate 	ldi_ident_release(li);
8740Sstevel@tonic-gate 	if (rc) {
8750Sstevel@tonic-gate 		cmn_err(CE_WARN,
8760Sstevel@tonic-gate 		    "getmacaddr: ldi_open_by_name(%s) failed: %d\n",
8770Sstevel@tonic-gate 		    clonepath, rc);
8780Sstevel@tonic-gate 		kmem_free(clonepath, MAXPATHLEN);
8790Sstevel@tonic-gate 		return (NULL);
8800Sstevel@tonic-gate 	}
8810Sstevel@tonic-gate 	kmem_free(clonepath, MAXPATHLEN);
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	ppa = i_ddi_devi_get_ppa(dip);
8840Sstevel@tonic-gate 	if ((dl_attach(lh, ppa) != 0) ||
8850Sstevel@tonic-gate 	    (dl_bind(lh, ETHERTYPE_IP, 0, DL_CLDLS, 0) != 0)) {
8860Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
8870Sstevel@tonic-gate 		cmn_err(CE_WARN,
8880Sstevel@tonic-gate 		    "getmacaddr: dl_attach/bind(%s%d) failed: %d\n",
8890Sstevel@tonic-gate 		    drv_name, ppa, rc);
8900Sstevel@tonic-gate 		return (NULL);
8910Sstevel@tonic-gate 	}
8920Sstevel@tonic-gate 	*maclenp = sizeof (struct ether_addr);
8930Sstevel@tonic-gate 	macaddr = kmem_alloc(*maclenp, KM_SLEEP);
8940Sstevel@tonic-gate 	if (dl_phys_addr(lh, (struct ether_addr *)macaddr) != 0) {
8950Sstevel@tonic-gate 		kmem_free(macaddr, *maclenp);
8960Sstevel@tonic-gate 		macaddr = NULL;
8970Sstevel@tonic-gate 		*maclenp = 0;
8980Sstevel@tonic-gate 		cmn_err(CE_WARN,
8990Sstevel@tonic-gate 		    "getmacaddr: dl_macaddr(%s%d) failed: %d\n",
9000Sstevel@tonic-gate 		    drv_name, ppa, rc);
9010Sstevel@tonic-gate 	}
9020Sstevel@tonic-gate 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
9030Sstevel@tonic-gate 	return (macaddr);
9040Sstevel@tonic-gate }
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate #endif	/* !__sparc */
907