xref: /onnv-gate/usr/src/uts/common/io/strplumb.c (revision 5397:629d004fcf53)
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
52343Sseb  * Common Development and Distribution License (the "License").
62343Sseb  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
223448Sdh155122  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include	<sys/param.h>
290Sstevel@tonic-gate #include	<sys/types.h>
300Sstevel@tonic-gate #include	<sys/user.h>
310Sstevel@tonic-gate #include	<sys/vfs.h>
320Sstevel@tonic-gate #include	<sys/vnode.h>
330Sstevel@tonic-gate #include	<sys/file.h>
340Sstevel@tonic-gate #include	<sys/stream.h>
350Sstevel@tonic-gate #include	<sys/stropts.h>
360Sstevel@tonic-gate #include	<sys/strsubr.h>
370Sstevel@tonic-gate #include	<sys/dlpi.h>
380Sstevel@tonic-gate #include	<sys/vnode.h>
390Sstevel@tonic-gate #include	<sys/socket.h>
400Sstevel@tonic-gate #include	<sys/sockio.h>
410Sstevel@tonic-gate #include	<net/if.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include	<sys/cred.h>
440Sstevel@tonic-gate #include	<sys/sysmacros.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include	<sys/sad.h>
470Sstevel@tonic-gate #include	<sys/kstr.h>
480Sstevel@tonic-gate #include	<sys/bootconf.h>
490Sstevel@tonic-gate #include	<sys/bootprops.h>
500Sstevel@tonic-gate 
510Sstevel@tonic-gate #include	<sys/errno.h>
520Sstevel@tonic-gate #include	<sys/modctl.h>
530Sstevel@tonic-gate #include	<sys/sunddi.h>
540Sstevel@tonic-gate #include	<sys/sunldi.h>
550Sstevel@tonic-gate #include	<sys/esunddi.h>
560Sstevel@tonic-gate #include	<sys/promif.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #include	<netinet/in.h>
590Sstevel@tonic-gate #include	<netinet/ip6.h>
600Sstevel@tonic-gate #include	<netinet/icmp6.h>
610Sstevel@tonic-gate #include	<netinet/sctp.h>
620Sstevel@tonic-gate #include	<inet/common.h>
630Sstevel@tonic-gate #include	<inet/ip.h>
640Sstevel@tonic-gate #include	<inet/ip6.h>
650Sstevel@tonic-gate #include	<inet/tcp.h>
660Sstevel@tonic-gate #include	<inet/sctp_ip.h>
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #include	<sys/strlog.h>
690Sstevel@tonic-gate #include	<sys/log.h>
700Sstevel@tonic-gate #include	<sys/ethernet.h>
710Sstevel@tonic-gate #include	<sys/ddi_implfuncs.h>
720Sstevel@tonic-gate 
730Sstevel@tonic-gate #include	<sys/dld.h>
740Sstevel@tonic-gate 
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate  * Debug Macros
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate int	strplumbdebug = 0;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate #define	DBG0(_f) \
810Sstevel@tonic-gate 	if (strplumbdebug != 0) \
820Sstevel@tonic-gate 		printf("strplumb: " _f)
830Sstevel@tonic-gate 
840Sstevel@tonic-gate #define	DBG1(_f, _a) \
850Sstevel@tonic-gate 	if (strplumbdebug != 0) \
860Sstevel@tonic-gate 		printf("strplumb: " _f, (_a))
870Sstevel@tonic-gate 
880Sstevel@tonic-gate #define	DBG2(_f, _a, _b) \
890Sstevel@tonic-gate 	if (strplumbdebug != 0) \
900Sstevel@tonic-gate 		printf("strplumb: " _f, (_a), (_b))
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #define	DBG3(_f, _a, _b, _c) \
930Sstevel@tonic-gate 	if (strplumbdebug != 0) \
940Sstevel@tonic-gate 		printf("strplumb: " _f, (_a), (_b), (_c))
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate  * Module linkage information for the kernel.
980Sstevel@tonic-gate  */
990Sstevel@tonic-gate #define	STRPLUMB_IDENT	"STREAMS Plumbing Module v%I%"
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate static struct modlmisc modlmisc = {
1020Sstevel@tonic-gate 	&mod_miscops,
1030Sstevel@tonic-gate 	STRPLUMB_IDENT
1040Sstevel@tonic-gate };
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate static struct modlinkage modlinkage = {
1070Sstevel@tonic-gate 	MODREV_1,
1080Sstevel@tonic-gate 	&modlmisc,
1090Sstevel@tonic-gate 	NULL
1100Sstevel@tonic-gate };
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate int
1130Sstevel@tonic-gate _init(void)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate int
1190Sstevel@tonic-gate _fini(void)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate int
1250Sstevel@tonic-gate _info(struct modinfo *modinfop)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate #define	ARP		"arp"
1310Sstevel@tonic-gate #define	TCP		"tcp"
1320Sstevel@tonic-gate #define	TCP6		"tcp6"
1330Sstevel@tonic-gate #define	UDP		"udp"
1340Sstevel@tonic-gate #define	UDP6		"udp6"
1350Sstevel@tonic-gate #define	SCTP		"sctp"
1360Sstevel@tonic-gate #define	SCTP6		"sctp6"
1370Sstevel@tonic-gate #define	ICMP		"icmp"
1380Sstevel@tonic-gate #define	ICMP6		"icmp6"
1390Sstevel@tonic-gate #define	IP		"ip"
1400Sstevel@tonic-gate #define	IP6		"ip6"
1410Sstevel@tonic-gate #define	TIMOD		"timod"
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate #define	UDPDEV		"/devices/pseudo/udp@0:udp"
1440Sstevel@tonic-gate #define	TCP6DEV		"/devices/pseudo/tcp6@0:tcp6"
1450Sstevel@tonic-gate #define	SCTP6DEV	"/devices/pseudo/sctp6@0:sctp6"
1460Sstevel@tonic-gate #define	IP6DEV		"/devices/pseudo/ip6@0:ip6"
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate typedef struct strplumb_modspec {
1490Sstevel@tonic-gate 	char	*sm_type;
1500Sstevel@tonic-gate 	char	*sm_name;
1510Sstevel@tonic-gate } strplumb_modspec_t;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate static strplumb_modspec_t	strplumb_modlist[] = {
1540Sstevel@tonic-gate 	{ "drv", DLD_DRIVER_NAME },
1550Sstevel@tonic-gate 	{ "drv", IP },
1560Sstevel@tonic-gate 	{ "drv", IP6 },
1570Sstevel@tonic-gate 	{ "drv", TCP },
1580Sstevel@tonic-gate 	{ "drv", TCP6 },
1590Sstevel@tonic-gate 	{ "drv", UDP },
1600Sstevel@tonic-gate 	{ "drv", UDP6 },
1610Sstevel@tonic-gate 	{ "drv", SCTP },
1620Sstevel@tonic-gate 	{ "drv", SCTP6 },
1630Sstevel@tonic-gate 	{ "drv", ICMP },
1640Sstevel@tonic-gate 	{ "drv", ICMP6 },
1650Sstevel@tonic-gate 	{ "drv", ARP },
1660Sstevel@tonic-gate 	{ "strmod", TIMOD }
1670Sstevel@tonic-gate };
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate  * Called from swapgeneric.c:loadrootmodules() in the network boot case.
1710Sstevel@tonic-gate  */
1720Sstevel@tonic-gate int
1730Sstevel@tonic-gate strplumb_load(void)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate 	uint_t			i;
1760Sstevel@tonic-gate 	strplumb_modspec_t	*p;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	DBG0("loading modules\n");
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	for (i = 0, p = strplumb_modlist;
1810Sstevel@tonic-gate 	    i < sizeof (strplumb_modlist) / sizeof (strplumb_modlist[0]);
1820Sstevel@tonic-gate 	    i++, p++) {
1830Sstevel@tonic-gate 		if (modloadonly(p->sm_type, p->sm_name) < 0) {
1840Sstevel@tonic-gate 			printf("strplumb: failed to load %s/%s\n",
1850Sstevel@tonic-gate 			    p->sm_type, p->sm_name);
1860Sstevel@tonic-gate 			return (EFAULT);
1870Sstevel@tonic-gate 		}
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	return (0);
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate static int
1940Sstevel@tonic-gate strplumb_init(void)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate 	uint_t			i;
1970Sstevel@tonic-gate 	strplumb_modspec_t	*p;
1980Sstevel@tonic-gate 	int			err;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	DBG0("initializing modules\n");
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	for (i = 0, p = strplumb_modlist;
2030Sstevel@tonic-gate 	    i < sizeof (strplumb_modlist) / sizeof (strplumb_modlist[0]);
2040Sstevel@tonic-gate 	    i++, p++) {
2050Sstevel@tonic-gate 		if (strcmp(p->sm_type, "drv") == 0)
2060Sstevel@tonic-gate 			err = (i_ddi_attach_pseudo_node(p->sm_name) != NULL) ?
2070Sstevel@tonic-gate 			    0 : EFAULT;
2080Sstevel@tonic-gate 		else
2090Sstevel@tonic-gate 			err = (modload(p->sm_type, p->sm_name) < 0) ?
2100Sstevel@tonic-gate 			    EFAULT : 0;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 		if (err != 0)  {
2130Sstevel@tonic-gate 			printf("strplumb: failed to initialize %s/%s\n",
2140Sstevel@tonic-gate 			    p->sm_type, p->sm_name);
2150Sstevel@tonic-gate 			return (err);
2160Sstevel@tonic-gate 		}
2170Sstevel@tonic-gate 	}
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	return (0);
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate static int
2230Sstevel@tonic-gate strplumb_autopush(void)
2240Sstevel@tonic-gate {
2250Sstevel@tonic-gate 	major_t		maj;
2260Sstevel@tonic-gate 	minor_t		min;
2270Sstevel@tonic-gate 	char		*mods[5];
2280Sstevel@tonic-gate 	uint_t		anchor = 1;
2290Sstevel@tonic-gate 	int		err;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	min = (minor_t)-1;
2320Sstevel@tonic-gate 	mods[1] = NULL;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	/*
2350Sstevel@tonic-gate 	 * ARP
2360Sstevel@tonic-gate 	 */
2370Sstevel@tonic-gate 	DBG0("setting up arp autopush\n");
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	mods[0] = ARP;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 	maj = ddi_name_to_major(ARP);
2420Sstevel@tonic-gate 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, &anchor,
2430Sstevel@tonic-gate 	    mods)) != 0) {
2440Sstevel@tonic-gate 		printf("strplumb: kstr_autopush(SET/ARP) failed: %d\n", err);
2450Sstevel@tonic-gate 		return (err);
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	return (0);
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate static int
2520Sstevel@tonic-gate strplumb_sctpq(ldi_ident_t li)
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate 	ldi_handle_t	lh = NULL;
2550Sstevel@tonic-gate 	int		err;
2560Sstevel@tonic-gate 	int		rval;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	DBG0("configuring SCTP default queue\n");
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	if ((err = ldi_open_by_name(SCTP6DEV, FREAD|FWRITE, CRED(), &lh,
2610Sstevel@tonic-gate 	    li)) != 0) {
2620Sstevel@tonic-gate 		printf("strplumb: open of SCTP6DEV failed: %d\n", err);
2630Sstevel@tonic-gate 		return (err);
2640Sstevel@tonic-gate 	}
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, SCTP_IOC_DEFAULT_Q, (intptr_t)0, FKIOCTL,
2670Sstevel@tonic-gate 	    CRED(), &rval)) != 0) {
2680Sstevel@tonic-gate 		printf("strplumb: failed to set SCTP default queue: %d\n",
2690Sstevel@tonic-gate 		    err);
2700Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
2710Sstevel@tonic-gate 		return (err);
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	return (0);
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate static int
2780Sstevel@tonic-gate strplumb_tcpq(ldi_ident_t li)
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate 	ldi_handle_t	lh = NULL;
2810Sstevel@tonic-gate 	ldi_handle_t	ip_lh = NULL;
2820Sstevel@tonic-gate 	int		err;
2830Sstevel@tonic-gate 	int		rval;
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	DBG0("configuring TCP default queue\n");
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	/*
2880Sstevel@tonic-gate 	 * We open IP6DEV here because we need to have it open to in
2890Sstevel@tonic-gate 	 * order to open TCP6DEV successfully.
2900Sstevel@tonic-gate 	 */
2910Sstevel@tonic-gate 	if ((err = ldi_open_by_name(IP6DEV, FREAD|FWRITE, CRED(), &ip_lh,
2920Sstevel@tonic-gate 	    li)) != 0) {
2930Sstevel@tonic-gate 		printf("strplumb: open of IP6DEV failed: %d\n", err);
2940Sstevel@tonic-gate 		return (err);
2950Sstevel@tonic-gate 	}
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	/*
2980Sstevel@tonic-gate 	 * We set the tcp default queue to IPv6 because IPv4 falls back to
2990Sstevel@tonic-gate 	 * IPv6 when it can't find a client, but IPv6 does not fall back to
3000Sstevel@tonic-gate 	 * IPv4.
3010Sstevel@tonic-gate 	 */
3020Sstevel@tonic-gate 	if ((err = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, CRED(), &lh,
3030Sstevel@tonic-gate 	    li)) != 0) {
3040Sstevel@tonic-gate 		printf("strplumb: open of TCP6DEV failed: %d\n", err);
3050Sstevel@tonic-gate 		goto done;
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, (intptr_t)0, FKIOCTL,
3090Sstevel@tonic-gate 	    CRED(), &rval)) != 0) {
3100Sstevel@tonic-gate 		printf("strplumb: failed to set TCP default queue: %d\n",
3110Sstevel@tonic-gate 		    err);
3120Sstevel@tonic-gate 		goto done;
3130Sstevel@tonic-gate 	}
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate done:
3160Sstevel@tonic-gate 	(void) ldi_close(ip_lh, FREAD|FWRITE, CRED());
3170Sstevel@tonic-gate 	return (err);
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate /*
3210Sstevel@tonic-gate  * Can be set in /etc/system in the case of local booting. See comment below.
3220Sstevel@tonic-gate  */
3230Sstevel@tonic-gate char	*ndev_name = 0;
3240Sstevel@tonic-gate int	ndev_unit = 0;
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate /*
3270Sstevel@tonic-gate  * If we booted diskless then strplumb() will have been called from
3280Sstevel@tonic-gate  * swapgeneric.c:rootconf(). All we can do in that case is plumb the
3290Sstevel@tonic-gate  * network device that we booted from.
3300Sstevel@tonic-gate  *
3310Sstevel@tonic-gate  * If we booted from a local disk, we will have been called from main(),
3320Sstevel@tonic-gate  * and normally we defer the plumbing of interfaces until network/physical.
3330Sstevel@tonic-gate  * This can be overridden by setting "ndev_name" in /etc/system.
3340Sstevel@tonic-gate  */
3350Sstevel@tonic-gate static int
336269Sericheng resolve_boot_path(void)
3370Sstevel@tonic-gate {
3380Sstevel@tonic-gate 	char			*devpath = NULL;
3390Sstevel@tonic-gate 	dev_info_t		*dip;
3400Sstevel@tonic-gate 	const char		*driver;
3410Sstevel@tonic-gate 	int			instance;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	if (strncmp(rootfs.bo_fstype, "nfs", 3) == 0)
3440Sstevel@tonic-gate 		devpath = rootfs.bo_name;
3450Sstevel@tonic-gate #ifndef __sparc
3460Sstevel@tonic-gate 	else
3470Sstevel@tonic-gate 		devpath = strplumb_get_netdev_path();
3480Sstevel@tonic-gate #endif
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	if (devpath != NULL) {
3510Sstevel@tonic-gate 		DBG1("resolving boot-path: %s\n", devpath);
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 		/*
3540Sstevel@tonic-gate 		 * Hold the devi since this is the root device.
3550Sstevel@tonic-gate 		 */
3560Sstevel@tonic-gate 		if ((dip = e_ddi_hold_devi_by_path(devpath, 0)) == NULL) {
3570Sstevel@tonic-gate 			printf("strplumb: unable to hold root device: %s\n",
3580Sstevel@tonic-gate 			    devpath);
3590Sstevel@tonic-gate 			return (ENXIO);
3600Sstevel@tonic-gate 		}
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 		driver = ddi_driver_name(dip);
3630Sstevel@tonic-gate 		instance = ddi_get_instance(dip);
3640Sstevel@tonic-gate 	} else {
3650Sstevel@tonic-gate 		if (ndev_name == NULL)
3660Sstevel@tonic-gate 			return (ENODEV);
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 		DBG2("using ndev_name (%s) ndev_unit (%d)\n", ndev_name,
3690Sstevel@tonic-gate 		    ndev_unit);
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 		if (i_ddi_attach_hw_nodes(ndev_name) != DDI_SUCCESS) {
3720Sstevel@tonic-gate 			printf("strplumb: cannot load ndev_name '%s'\n",
3730Sstevel@tonic-gate 			    ndev_name);
3740Sstevel@tonic-gate 			return (ENXIO);
3750Sstevel@tonic-gate 		}
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 		driver = ndev_name;
3780Sstevel@tonic-gate 		instance = ndev_unit;
3790Sstevel@tonic-gate 	}
3800Sstevel@tonic-gate 
3810Sstevel@tonic-gate 	(void) snprintf(rootfs.bo_devname, BO_MAXOBJNAME,
382269Sericheng 	    "/devices/pseudo/clone@0:%s", driver);
3830Sstevel@tonic-gate 	(void) snprintf(rootfs.bo_ifname, BO_MAXOBJNAME, "%s%d",
3840Sstevel@tonic-gate 	    driver, instance);
3850Sstevel@tonic-gate 	rootfs.bo_ppa = instance;
386269Sericheng 	return (0);
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate static int
3900Sstevel@tonic-gate getifflags(ldi_handle_t lh, struct lifreq *lifrp)
3910Sstevel@tonic-gate {
3920Sstevel@tonic-gate 	struct strioctl	iocb;
3930Sstevel@tonic-gate 	int		rval;
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	iocb.ic_cmd = SIOCGLIFFLAGS;
3960Sstevel@tonic-gate 	iocb.ic_timout = 15;
3970Sstevel@tonic-gate 	iocb.ic_len = sizeof (struct lifreq);
3980Sstevel@tonic-gate 	iocb.ic_dp = (char *)lifrp;
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate }
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate static int
4050Sstevel@tonic-gate setifname(ldi_handle_t lh, struct lifreq *lifrp)
4060Sstevel@tonic-gate {
4070Sstevel@tonic-gate 	struct strioctl	iocb;
4080Sstevel@tonic-gate 	int		rval;
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	iocb.ic_cmd = SIOCSLIFNAME;
4110Sstevel@tonic-gate 	iocb.ic_timout = 15;
4120Sstevel@tonic-gate 	iocb.ic_len = sizeof (struct lifreq);
4130Sstevel@tonic-gate 	iocb.ic_dp = (char *)lifrp;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
4160Sstevel@tonic-gate }
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate static int
4190Sstevel@tonic-gate strplumb_dev(ldi_ident_t li)
4200Sstevel@tonic-gate {
4210Sstevel@tonic-gate 	ldi_handle_t	lh = NULL;
4220Sstevel@tonic-gate 	ldi_handle_t	mux_lh = NULL;
4230Sstevel@tonic-gate 	int		err;
4240Sstevel@tonic-gate 	struct lifreq	lifr;
4250Sstevel@tonic-gate 	struct ifreq	ifr;
4260Sstevel@tonic-gate 	int		rval;
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	bzero(&lifr, sizeof (struct lifreq));
4290Sstevel@tonic-gate 	bzero(&ifr, sizeof (ifr));
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	/*
4320Sstevel@tonic-gate 	 * Now set up the links. Ultimately, we should have two streams
4330Sstevel@tonic-gate 	 * permanently linked underneath UDP (which is actually IP with UDP
4340Sstevel@tonic-gate 	 * autopushed). One stream consists of the ARP-[ifname] combination,
4350Sstevel@tonic-gate 	 * while the other consists of ARP-IP-[ifname]. The second combination
4360Sstevel@tonic-gate 	 * seems a little weird, but is linked underneath UDP just to keep it
4370Sstevel@tonic-gate 	 * around.
4380Sstevel@tonic-gate 	 *
4390Sstevel@tonic-gate 	 * We pin underneath UDP here to match what is done in ifconfig(1m);
4400Sstevel@tonic-gate 	 * otherwise, ifconfig will be unable to unplumb the stream (the major
4410Sstevel@tonic-gate 	 * number and mux id must both match for a successful I_PUNLINK).
4420Sstevel@tonic-gate 	 *
4430Sstevel@tonic-gate 	 * There are subtleties in the plumbing which make it essential to
4440Sstevel@tonic-gate 	 * follow the logic used in ifconfig(1m) very closely.
4450Sstevel@tonic-gate 	 */
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	/*
4480Sstevel@tonic-gate 	 * Plumb UDP-ARP-IP-<dev>
4490Sstevel@tonic-gate 	 */
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
4520Sstevel@tonic-gate 	    &lh, li)) != 0) {
4530Sstevel@tonic-gate 		printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
4540Sstevel@tonic-gate 		    err);
4550Sstevel@tonic-gate 		goto done;
4560Sstevel@tonic-gate 	}
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)IP, FKIOCTL, CRED(),
4600Sstevel@tonic-gate 	    &rval)) != 0) {
4610Sstevel@tonic-gate 		printf("strplumb: push IP failed: %d\n", err);
4620Sstevel@tonic-gate 		goto done;
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	if ((err = getifflags(lh, &lifr)) != 0)
4660Sstevel@tonic-gate 		goto done;
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	lifr.lifr_flags |= IFF_IPV4;
4690Sstevel@tonic-gate 	lifr.lifr_flags &= ~IFF_IPV6;
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)ARP, FKIOCTL, CRED(),
4720Sstevel@tonic-gate 	    &rval)) != 0) {
4730Sstevel@tonic-gate 		printf("strplumb: push ARP failed: %d\n", err);
4740Sstevel@tonic-gate 		goto done;
4750Sstevel@tonic-gate 	}
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, rootfs.bo_ifname,
4780Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
4790Sstevel@tonic-gate 	lifr.lifr_ppa = rootfs.bo_ppa;
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	if ((err = setifname(lh, &lifr)) != 0)
4820Sstevel@tonic-gate 		goto done;
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	/* Get the flags and check if ARP is needed */
4850Sstevel@tonic-gate 	if ((err = getifflags(lh, &lifr)) != 0) {
4860Sstevel@tonic-gate 		printf("strplumb: getifflags %s IP failed, error %d\n",
4870Sstevel@tonic-gate 		    lifr.lifr_name, err);
4880Sstevel@tonic-gate 		goto done;
4890Sstevel@tonic-gate 	}
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	/* Pop out ARP if not needed */
4920Sstevel@tonic-gate 	if (lifr.lifr_flags & IFF_NOARP) {
4930Sstevel@tonic-gate 		err = ldi_ioctl(lh, I_POP, (intptr_t)0, FKIOCTL, CRED(),
4940Sstevel@tonic-gate 		    &rval);
4950Sstevel@tonic-gate 		if (err != 0) {
4960Sstevel@tonic-gate 			printf("strplumb: pop ARP failed, error %d\n", err);
4970Sstevel@tonic-gate 			goto done;
4980Sstevel@tonic-gate 		}
4990Sstevel@tonic-gate 	}
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	if ((err = ldi_open_by_name(UDPDEV, FREAD|FWRITE, CRED(), &mux_lh,
5020Sstevel@tonic-gate 	    li)) != 0) {
5030Sstevel@tonic-gate 		printf("strplumb: open of UDPDEV failed: %d\n", err);
5040Sstevel@tonic-gate 		goto done;
5050Sstevel@tonic-gate 	}
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
5080Sstevel@tonic-gate 	    FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
5090Sstevel@tonic-gate 	    &(ifr.ifr_ip_muxid))) != 0) {
5100Sstevel@tonic-gate 		printf("strplumb: plink UDP-ARP-IP-%s failed: %d\n",
5110Sstevel@tonic-gate 		    rootfs.bo_ifname, err);
5120Sstevel@tonic-gate 		goto done;
5130Sstevel@tonic-gate 	}
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	DBG2("UDP-ARP-IP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_ip_muxid);
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
5180Sstevel@tonic-gate 	lh = NULL;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	/*
5210Sstevel@tonic-gate 	 * Plumb UDP-ARP-<dev>
5220Sstevel@tonic-gate 	 */
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
5250Sstevel@tonic-gate 	    &lh, li)) != 0) {
5260Sstevel@tonic-gate 		printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
5270Sstevel@tonic-gate 		    err);
5280Sstevel@tonic-gate 		goto done;
5290Sstevel@tonic-gate 	}
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)ARP, FKIOCTL, CRED(),
5320Sstevel@tonic-gate 	    &rval)) != 0) {
5330Sstevel@tonic-gate 		printf("strplumb: push ARP failed: %d\n", err);
5340Sstevel@tonic-gate 		goto done;
5350Sstevel@tonic-gate 	}
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	if ((err = setifname(lh, &lifr)) != 0)
5380Sstevel@tonic-gate 		goto done;
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
5410Sstevel@tonic-gate 	    FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
5420Sstevel@tonic-gate 	    &(ifr.ifr_arp_muxid))) != 0) {
5430Sstevel@tonic-gate 		printf("strplumb: plink UDP-ARP-%s failed: %d\n",
5440Sstevel@tonic-gate 		    rootfs.bo_ifname, err);
5450Sstevel@tonic-gate 		goto done;
5460Sstevel@tonic-gate 	}
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	DBG2("UDP-ARP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_arp_muxid);
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	/*
5510Sstevel@tonic-gate 	 * Cache the mux ids.
5520Sstevel@tonic-gate 	 */
5530Sstevel@tonic-gate 	(void) strlcpy(ifr.ifr_name, rootfs.bo_ifname, sizeof (ifr.ifr_name));
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, SIOCSIFMUXID, (intptr_t)&ifr, FKIOCTL,
5560Sstevel@tonic-gate 	    CRED(), &rval)) != 0) {
5570Sstevel@tonic-gate 		printf("strplumb: SIOCSIFMUXID failed: %d\n", err);
5580Sstevel@tonic-gate 		goto done;
5590Sstevel@tonic-gate 	}
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate done:
5620Sstevel@tonic-gate 	if (lh != NULL)
5630Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	if (mux_lh != NULL)
5660Sstevel@tonic-gate 		(void) ldi_close(mux_lh, FREAD|FWRITE, CRED());
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	return (err);
5690Sstevel@tonic-gate }
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate /*
5720Sstevel@tonic-gate  * Do streams plumbing for internet protocols.
5730Sstevel@tonic-gate  */
5740Sstevel@tonic-gate int
5750Sstevel@tonic-gate strplumb(void)
5760Sstevel@tonic-gate {
5770Sstevel@tonic-gate 	ldi_ident_t	li;
5780Sstevel@tonic-gate 	int		err;
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	if ((err = strplumb_init()) != 0)
5810Sstevel@tonic-gate 		return (err);
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	if ((err = strplumb_autopush()) != 0)
5840Sstevel@tonic-gate 		return (err);
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	if ((err = ldi_ident_from_mod(&modlinkage, &li)) != 0)
5870Sstevel@tonic-gate 		return (err);
5880Sstevel@tonic-gate 
5893448Sdh155122 	/*
5903448Sdh155122 	 * Setup the TCP and SCTP default queues for the global stack.
5913448Sdh155122 	 * tcp/sctp_stack_init will do this for additional stack instances.
5923448Sdh155122 	 */
5930Sstevel@tonic-gate 	if ((err = strplumb_sctpq(li)) != 0)
5940Sstevel@tonic-gate 		goto done;
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 	if ((err = strplumb_tcpq(li)) != 0)
5970Sstevel@tonic-gate 		goto done;
5980Sstevel@tonic-gate 
599269Sericheng 	if ((err = resolve_boot_path()) != 0)
6000Sstevel@tonic-gate 		goto done;
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	DBG1("rootfs.bo_devname: %s\n", rootfs.bo_devname);
6030Sstevel@tonic-gate 	DBG1("rootfs.bo_ifname: %s\n", rootfs.bo_ifname);
6040Sstevel@tonic-gate 	DBG1("rootfs.bo_ppa: %d\n", rootfs.bo_ppa);
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	if ((err = strplumb_dev(li)) != 0)
6070Sstevel@tonic-gate 		goto done;
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate done:
6100Sstevel@tonic-gate 	ldi_ident_release(li);
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	return (err);
6130Sstevel@tonic-gate }
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate /* multiboot:  diskless boot interface discovery */
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate #ifndef	__sparc
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate static uchar_t boot_macaddr[16];
6200Sstevel@tonic-gate static int boot_maclen;
6210Sstevel@tonic-gate static uchar_t *getmacaddr(dev_info_t *dip, int *maclen);
6220Sstevel@tonic-gate static int matchmac(dev_info_t *dip, void *arg);
623307Sszhou int dl_attach(ldi_handle_t lh, int unit);
624307Sszhou int dl_bind(ldi_handle_t lh, uint_t sap, uint_t max_conn,
6250Sstevel@tonic-gate     uint_t service, uint_t conn_mgmt);
626307Sszhou int dl_phys_addr(ldi_handle_t lh, struct ether_addr *eaddr);
6270Sstevel@tonic-gate 
6284172Ssetje #endif  /* !__sparc */
6294172Ssetje 
6300Sstevel@tonic-gate char *
6310Sstevel@tonic-gate strplumb_get_netdev_path(void)
6320Sstevel@tonic-gate {
6334172Ssetje #ifndef	__sparc
6340Sstevel@tonic-gate 	char *macstr, *devpath = NULL;
6350Sstevel@tonic-gate 	uchar_t *bootp;
6360Sstevel@tonic-gate 	uint_t bootp_len, len;
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
6390Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, BP_BOOT_MAC, &macstr) == DDI_SUCCESS) {
6400Sstevel@tonic-gate 		/*
6410Sstevel@tonic-gate 		 * hard coded ether mac len for booting floppy on
6420Sstevel@tonic-gate 		 * machines with old cards
6430Sstevel@tonic-gate 		 */
6440Sstevel@tonic-gate 		boot_maclen = ether_aton(macstr, boot_macaddr);
6450Sstevel@tonic-gate 		if (boot_maclen != 6) {
6460Sstevel@tonic-gate 			cmn_err(CE_WARN,
6470Sstevel@tonic-gate 			    "malformed boot_mac property, %d bytes",
6480Sstevel@tonic-gate 			    boot_maclen);
6490Sstevel@tonic-gate 		}
6500Sstevel@tonic-gate 		ddi_prop_free(macstr);
6510Sstevel@tonic-gate 	} else if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, ddi_root_node(),
6520Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, BP_BOOTP_RESPONSE, &bootp, &bootp_len)
6530Sstevel@tonic-gate 	    == DDI_SUCCESS) {
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 		/*
6560Sstevel@tonic-gate 		 * These offsets are defined by dhcp standard
6570Sstevel@tonic-gate 		 * Should use structure offsets
6580Sstevel@tonic-gate 		 */
6590Sstevel@tonic-gate 		boot_maclen = *(bootp + 2);
6600Sstevel@tonic-gate 		ASSERT(boot_maclen <= 16);
6610Sstevel@tonic-gate 		(void) bcopy(bootp + 28, boot_macaddr, boot_maclen);
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 		/* encode to ascii string to match what sparc OBP exports */
664129Ssetje 		dhcack = kmem_zalloc(bootp_len * 2 + IFNAMSIZ + 2, KM_SLEEP);
6654562Ssetje 		len = bootp_len * 2 + 2;
6660Sstevel@tonic-gate 		(void) octet_to_hexascii(bootp, bootp_len, dhcack + IFNAMSIZ,
6670Sstevel@tonic-gate 		    &len);
6680Sstevel@tonic-gate 		ASSERT(len < bootp_len * 2 + 2);
6690Sstevel@tonic-gate 		ddi_prop_free(bootp);
6700Sstevel@tonic-gate 	} else
6710Sstevel@tonic-gate 		return (NULL);
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	ddi_walk_devs(ddi_root_node(), matchmac, (void *)&devpath);
6740Sstevel@tonic-gate 	return (devpath);
6754172Ssetje 
6764172Ssetje #else
6774172Ssetje 	return (NULL);
6784172Ssetje #endif  /* !__sparc */
6790Sstevel@tonic-gate }
6800Sstevel@tonic-gate 
6814172Ssetje #ifndef __sparc
6824172Ssetje 
6830Sstevel@tonic-gate /*
6840Sstevel@tonic-gate  * Get boot path from the boot_mac address
6850Sstevel@tonic-gate  */
6860Sstevel@tonic-gate /*ARGSUSED*/
6870Sstevel@tonic-gate static int
6880Sstevel@tonic-gate matchmac(dev_info_t *dip, void *arg)
6890Sstevel@tonic-gate {
6900Sstevel@tonic-gate 	char **devpathp = (char **)arg;
6910Sstevel@tonic-gate 	char *model_str;
6920Sstevel@tonic-gate 	uchar_t *macaddr;
6930Sstevel@tonic-gate 	int maclen;
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	/* XXX Should use "device-type" per IEEE 1275 */
6960Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0,
6970Sstevel@tonic-gate 	    "model", &model_str) != DDI_SUCCESS)
6980Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	if (strcmp(model_str, "Ethernet controller") != 0) {
7010Sstevel@tonic-gate 		ddi_prop_free(model_str);
7020Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7030Sstevel@tonic-gate 	}
7040Sstevel@tonic-gate 	ddi_prop_free(model_str);
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	/* We have a network device now */
7070Sstevel@tonic-gate 	if (i_ddi_attach_node_hierarchy(dip) != DDI_SUCCESS) {
7080Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7090Sstevel@tonic-gate 	}
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	ASSERT(boot_maclen != 0);
7120Sstevel@tonic-gate 	macaddr = getmacaddr(dip, &maclen);
7130Sstevel@tonic-gate 	if (macaddr == NULL)
7140Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	if (maclen != boot_maclen ||
7170Sstevel@tonic-gate 	    bcmp(macaddr, boot_macaddr, maclen) != 0) {
7180Sstevel@tonic-gate 		kmem_free(macaddr, maclen);
7190Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7200Sstevel@tonic-gate 	}
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	/* found hardware with the mac address */
7230Sstevel@tonic-gate 	(void) localetheraddr((struct ether_addr *)macaddr, NULL);
7240Sstevel@tonic-gate 	kmem_free(macaddr, maclen);
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	*devpathp = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7270Sstevel@tonic-gate 	(void) ddi_pathname(dip, *devpathp);
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 	/* fill in the name portion of dhcack */
7300Sstevel@tonic-gate 	if (dhcack)
7310Sstevel@tonic-gate 		(void) snprintf(dhcack, IFNAMSIZ, "%s%d",
7320Sstevel@tonic-gate 		    ddi_driver_name(dip), i_ddi_devi_get_ppa(dip));
7330Sstevel@tonic-gate 	return (DDI_WALK_TERMINATE);
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate static uchar_t *
7370Sstevel@tonic-gate getmacaddr_gldv3(char *drv, int inst, int *maclenp)
7380Sstevel@tonic-gate {
7390Sstevel@tonic-gate 	char ifname[16];
7400Sstevel@tonic-gate 	mac_handle_t mh;
7410Sstevel@tonic-gate 	uchar_t *macaddr;
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 	(void) snprintf(ifname, sizeof (ifname), "%s%d", drv, inst);
7442343Sseb 	if (mac_open(ifname, inst, &mh) < 0) {
7450Sstevel@tonic-gate 		return (NULL);
7460Sstevel@tonic-gate 	}
7470Sstevel@tonic-gate 	*maclenp = sizeof (struct ether_addr);
7480Sstevel@tonic-gate 	macaddr = kmem_alloc(*maclenp, KM_SLEEP);
7490Sstevel@tonic-gate 	mac_unicst_get(mh, macaddr);
7500Sstevel@tonic-gate 	mac_close(mh);
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	return (macaddr);
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate static uchar_t *
7560Sstevel@tonic-gate getmacaddr(dev_info_t *dip, int *maclenp)
7570Sstevel@tonic-gate {
7580Sstevel@tonic-gate 	int rc, ppa;
7590Sstevel@tonic-gate 	ldi_ident_t li;
7600Sstevel@tonic-gate 	ldi_handle_t lh;
7610Sstevel@tonic-gate 	char *drv_name = (char *)ddi_driver_name(dip);
7620Sstevel@tonic-gate 	char *clonepath;
7630Sstevel@tonic-gate 	uchar_t *macaddr = NULL;
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	/* a simpler way to get mac address for GLDv3 drivers */
7660Sstevel@tonic-gate 	if (GLDV3_DRV(ddi_name_to_major(drv_name))) {
7670Sstevel@tonic-gate 		return (getmacaddr_gldv3(drv_name, ddi_get_instance(dip),
7680Sstevel@tonic-gate 		    maclenp));
7690Sstevel@tonic-gate 	}
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	if (rc = ldi_ident_from_mod(&modlinkage, &li)) {
7720Sstevel@tonic-gate 		cmn_err(CE_WARN,
7730Sstevel@tonic-gate 		    "getmacaddr: ldi_ident_from_mod failed: %d\n", rc);
7740Sstevel@tonic-gate 		return (NULL);
7750Sstevel@tonic-gate 	}
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	clonepath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7780Sstevel@tonic-gate 	(void) snprintf(clonepath, MAXPATHLEN,
7790Sstevel@tonic-gate 	    "/devices/pseudo/clone@0:%s", drv_name);
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	rc = ldi_open_by_name(clonepath, FREAD|FWRITE, CRED(), &lh, li);
7820Sstevel@tonic-gate 	ldi_ident_release(li);
7830Sstevel@tonic-gate 	if (rc) {
7840Sstevel@tonic-gate 		cmn_err(CE_WARN,
7850Sstevel@tonic-gate 		    "getmacaddr: ldi_open_by_name(%s) failed: %d\n",
7860Sstevel@tonic-gate 		    clonepath, rc);
7870Sstevel@tonic-gate 		kmem_free(clonepath, MAXPATHLEN);
7880Sstevel@tonic-gate 		return (NULL);
7890Sstevel@tonic-gate 	}
7900Sstevel@tonic-gate 	kmem_free(clonepath, MAXPATHLEN);
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	ppa = i_ddi_devi_get_ppa(dip);
7930Sstevel@tonic-gate 	if ((dl_attach(lh, ppa) != 0) ||
7940Sstevel@tonic-gate 	    (dl_bind(lh, ETHERTYPE_IP, 0, DL_CLDLS, 0) != 0)) {
7950Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
7960Sstevel@tonic-gate 		cmn_err(CE_WARN,
7970Sstevel@tonic-gate 		    "getmacaddr: dl_attach/bind(%s%d) failed: %d\n",
7980Sstevel@tonic-gate 		    drv_name, ppa, rc);
7990Sstevel@tonic-gate 		return (NULL);
8000Sstevel@tonic-gate 	}
8010Sstevel@tonic-gate 	*maclenp = sizeof (struct ether_addr);
8020Sstevel@tonic-gate 	macaddr = kmem_alloc(*maclenp, KM_SLEEP);
8030Sstevel@tonic-gate 	if (dl_phys_addr(lh, (struct ether_addr *)macaddr) != 0) {
8040Sstevel@tonic-gate 		kmem_free(macaddr, *maclenp);
8050Sstevel@tonic-gate 		macaddr = NULL;
8060Sstevel@tonic-gate 		*maclenp = 0;
8070Sstevel@tonic-gate 		cmn_err(CE_WARN,
8080Sstevel@tonic-gate 		    "getmacaddr: dl_macaddr(%s%d) failed: %d\n",
8090Sstevel@tonic-gate 		    drv_name, ppa, rc);
8100Sstevel@tonic-gate 	}
8110Sstevel@tonic-gate 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
8120Sstevel@tonic-gate 	return (macaddr);
8130Sstevel@tonic-gate }
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate #endif	/* !__sparc */
816307Sszhou 
817307Sszhou int
818307Sszhou dl_attach(ldi_handle_t lh, int unit)
819307Sszhou {
820307Sszhou 	dl_attach_req_t *attach_req;
821307Sszhou 	union DL_primitives *dl_prim;
822307Sszhou 	mblk_t *mp;
823307Sszhou 	int error;
824307Sszhou 
825307Sszhou 	if ((mp = allocb(sizeof (dl_attach_req_t), BPRI_MED)) == NULL) {
826307Sszhou 		cmn_err(CE_WARN, "dl_attach: allocb failed");
827307Sszhou 		return (ENOSR);
828307Sszhou 	}
829307Sszhou 	mp->b_datap->db_type = M_PROTO;
830307Sszhou 	mp->b_wptr += sizeof (dl_attach_req_t);
831307Sszhou 
832307Sszhou 	attach_req = (dl_attach_req_t *)mp->b_rptr;
833307Sszhou 	attach_req->dl_primitive = DL_ATTACH_REQ;
834307Sszhou 	attach_req->dl_ppa = unit;
835307Sszhou 
836307Sszhou 	(void) ldi_putmsg(lh, mp);
837307Sszhou 	if ((error = ldi_getmsg(lh, &mp, (timestruc_t *)NULL)) != 0) {
838*5397Syz147064 		cmn_err(CE_NOTE, "!dl_attach: ldi_getmsg failed: %d", error);
839307Sszhou 		return (error);
840307Sszhou 	}
841307Sszhou 
842307Sszhou 	dl_prim = (union DL_primitives *)mp->b_rptr;
843307Sszhou 	switch (dl_prim->dl_primitive) {
844307Sszhou 	case DL_OK_ACK:
845307Sszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_ok_ack_t)) {
846*5397Syz147064 			cmn_err(CE_NOTE,
847*5397Syz147064 			    "!dl_attach: DL_OK_ACK protocol error");
848307Sszhou 			break;
849307Sszhou 		}
850307Sszhou 		if (((dl_ok_ack_t *)dl_prim)->dl_correct_primitive !=
851307Sszhou 		    DL_ATTACH_REQ) {
852*5397Syz147064 			cmn_err(CE_NOTE, "!dl_attach: DL_OK_ACK rtnd prim %u",
853307Sszhou 			    ((dl_ok_ack_t *)dl_prim)->dl_correct_primitive);
854307Sszhou 			break;
855307Sszhou 		}
856307Sszhou 		freemsg(mp);
857307Sszhou 		return (0);
858307Sszhou 
859307Sszhou 	case DL_ERROR_ACK:
860307Sszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_error_ack_t)) {
861*5397Syz147064 			cmn_err(CE_NOTE,
862*5397Syz147064 			    "!dl_attach: DL_ERROR_ACK protocol error");
863307Sszhou 			break;
864307Sszhou 		}
865307Sszhou 		break;
866307Sszhou 
867307Sszhou 	default:
868*5397Syz147064 		cmn_err(CE_NOTE, "!dl_attach: bad ACK header %u",
869*5397Syz147064 		    dl_prim->dl_primitive);
870307Sszhou 		break;
871307Sszhou 	}
872307Sszhou 
873307Sszhou 	/*
874307Sszhou 	 * Error return only.
875307Sszhou 	 */
876307Sszhou 	freemsg(mp);
877307Sszhou 	return (-1);
878307Sszhou }
879307Sszhou 
880307Sszhou int
881307Sszhou dl_bind(ldi_handle_t lh, uint_t sap, uint_t max_conn, uint_t service,
882307Sszhou 	uint_t conn_mgmt)
883307Sszhou {
884307Sszhou 	dl_bind_req_t *bind_req;
885307Sszhou 	union DL_primitives *dl_prim;
886307Sszhou 	mblk_t *mp;
887307Sszhou 	int error;
888307Sszhou 
889307Sszhou 	if ((mp = allocb(sizeof (dl_bind_req_t), BPRI_MED)) == NULL) {
890307Sszhou 		cmn_err(CE_WARN, "dl_bind: allocb failed");
891307Sszhou 		return (ENOSR);
892307Sszhou 	}
893307Sszhou 	mp->b_datap->db_type = M_PROTO;
894307Sszhou 
895307Sszhou 	bind_req = (dl_bind_req_t *)mp->b_wptr;
896307Sszhou 	mp->b_wptr += sizeof (dl_bind_req_t);
897307Sszhou 	bind_req->dl_primitive = DL_BIND_REQ;
898307Sszhou 	bind_req->dl_sap = sap;
899307Sszhou 	bind_req->dl_max_conind = max_conn;
900307Sszhou 	bind_req->dl_service_mode = service;
901307Sszhou 	bind_req->dl_conn_mgmt = conn_mgmt;
902307Sszhou 	bind_req->dl_xidtest_flg = 0;
903307Sszhou 
904307Sszhou 	(void) ldi_putmsg(lh, mp);
905307Sszhou 	if ((error = ldi_getmsg(lh, &mp, (timestruc_t *)NULL)) != 0) {
906*5397Syz147064 		cmn_err(CE_NOTE, "!dl_bind: ldi_getmsg failed: %d", error);
907307Sszhou 		return (error);
908307Sszhou 	}
909307Sszhou 
910307Sszhou 	dl_prim = (union DL_primitives *)mp->b_rptr;
911307Sszhou 	switch (dl_prim->dl_primitive) {
912307Sszhou 	case DL_BIND_ACK:
913307Sszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_bind_ack_t)) {
914*5397Syz147064 			cmn_err(CE_NOTE,
915*5397Syz147064 			    "!dl_bind: DL_BIND_ACK protocol error");
916307Sszhou 			break;
917307Sszhou 		}
918307Sszhou 		if (((dl_bind_ack_t *)dl_prim)->dl_sap != sap) {
919*5397Syz147064 			cmn_err(CE_NOTE, "!dl_bind: DL_BIND_ACK bad sap %u",
920307Sszhou 			    ((dl_bind_ack_t *)dl_prim)->dl_sap);
921307Sszhou 			break;
922307Sszhou 		}
923307Sszhou 		freemsg(mp);
924307Sszhou 		return (0);
925307Sszhou 
926307Sszhou 	case DL_ERROR_ACK:
927307Sszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_error_ack_t)) {
928*5397Syz147064 			cmn_err(CE_NOTE,
929*5397Syz147064 			    "!dl_bind: DL_ERROR_ACK protocol error");
930307Sszhou 			break;
931307Sszhou 		}
932307Sszhou 		break;
933307Sszhou 
934307Sszhou 	default:
935*5397Syz147064 		cmn_err(CE_NOTE, "!dl_bind: bad ACK header %u",
936*5397Syz147064 		    dl_prim->dl_primitive);
937307Sszhou 		break;
938307Sszhou 	}
939307Sszhou 
940307Sszhou 	/*
941307Sszhou 	 * Error return only.
942307Sszhou 	 */
943307Sszhou 	freemsg(mp);
944307Sszhou 	return (-1);
945307Sszhou }
946307Sszhou 
947307Sszhou int
948307Sszhou dl_phys_addr(ldi_handle_t lh, struct ether_addr *eaddr)
949307Sszhou {
950307Sszhou 	dl_phys_addr_req_t *phys_addr_req;
951307Sszhou 	dl_phys_addr_ack_t *phys_addr_ack;
952307Sszhou 	union DL_primitives *dl_prim;
953307Sszhou 	mblk_t *mp;
954307Sszhou 	int error;
955307Sszhou 	uchar_t *addrp;
956307Sszhou 	timestruc_t tv;
957307Sszhou 
958307Sszhou 	if ((mp = allocb(sizeof (dl_phys_addr_req_t), BPRI_MED)) ==
959307Sszhou 	    (mblk_t *)NULL) {
960307Sszhou 		cmn_err(CE_WARN, "dl_phys_addr: allocb failed");
961307Sszhou 		return (ENOSR);
962307Sszhou 	}
963307Sszhou 	mp->b_datap->db_type = M_PROTO;
964307Sszhou 	mp->b_wptr += sizeof (dl_phys_addr_req_t);
965307Sszhou 
966307Sszhou 	phys_addr_req = (dl_phys_addr_req_t *)mp->b_rptr;
967307Sszhou 	phys_addr_req->dl_primitive = DL_PHYS_ADDR_REQ;
968307Sszhou 	phys_addr_req->dl_addr_type = DL_CURR_PHYS_ADDR;
969307Sszhou 
970307Sszhou 	/*
971307Sszhou 	 * In case some provider doesn't implement or nack the
972307Sszhou 	 * request just wait for 15 seconds.
973307Sszhou 	 */
974307Sszhou 	tv.tv_sec = 15;
975307Sszhou 	tv.tv_nsec = 0;
976307Sszhou 
977307Sszhou 	(void) ldi_putmsg(lh, mp);
978307Sszhou 	error = ldi_getmsg(lh, &mp, &tv);
979307Sszhou 	if (error == ETIME) {
980*5397Syz147064 		cmn_err(CE_NOTE, "!dl_phys_addr: timed out");
981307Sszhou 		return (-1);
982307Sszhou 	} else if (error != 0) {
983*5397Syz147064 		cmn_err(CE_NOTE, "!dl_phys_addr: ldi_getmsg failed: %d", error);
984307Sszhou 		return (error);
985307Sszhou 	}
986307Sszhou 
987307Sszhou 	dl_prim = (union DL_primitives *)mp->b_rptr;
988307Sszhou 	switch (dl_prim->dl_primitive) {
989307Sszhou 	case DL_PHYS_ADDR_ACK:
990307Sszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_phys_addr_ack_t)) {
991*5397Syz147064 			cmn_err(CE_NOTE, "!dl_phys_addr: "
992*5397Syz147064 			    "DL_PHYS_ADDR_ACK protocol error");
993307Sszhou 			break;
994307Sszhou 		}
995307Sszhou 		phys_addr_ack = &dl_prim->physaddr_ack;
996307Sszhou 		if (phys_addr_ack->dl_addr_length != sizeof (*eaddr)) {
997*5397Syz147064 			cmn_err(CE_NOTE,
998*5397Syz147064 			    "!dl_phys_addr: DL_PHYS_ADDR_ACK bad len %u",
9995240Snordmark 			    phys_addr_ack->dl_addr_length);
1000307Sszhou 			break;
1001307Sszhou 		}
1002307Sszhou 		if (phys_addr_ack->dl_addr_length +
1003307Sszhou 		    phys_addr_ack->dl_addr_offset > (mp->b_wptr-mp->b_rptr)) {
1004*5397Syz147064 			cmn_err(CE_NOTE,
1005*5397Syz147064 			    "!dl_phys_addr: DL_PHYS_ADDR_ACK bad len %u",
10065240Snordmark 			    phys_addr_ack->dl_addr_length);
1007307Sszhou 			break;
1008307Sszhou 		}
1009307Sszhou 		addrp = mp->b_rptr + phys_addr_ack->dl_addr_offset;
1010307Sszhou 		bcopy(addrp, eaddr, sizeof (*eaddr));
1011307Sszhou 		freemsg(mp);
1012307Sszhou 		return (0);
1013307Sszhou 
1014307Sszhou 	case DL_ERROR_ACK:
1015307Sszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_error_ack_t)) {
1016*5397Syz147064 			cmn_err(CE_NOTE,
1017*5397Syz147064 			    "!dl_phys_addr: DL_ERROR_ACK protocol error");
1018307Sszhou 			break;
1019307Sszhou 		}
1020307Sszhou 
1021307Sszhou 		break;
1022307Sszhou 
1023307Sszhou 	default:
1024*5397Syz147064 		cmn_err(CE_NOTE, "!dl_phys_addr: bad ACK header %u",
10255240Snordmark 		    dl_prim->dl_primitive);
1026307Sszhou 		break;
1027307Sszhou 	}
1028307Sszhou 
1029307Sszhou 	/*
1030307Sszhou 	 * Error return only.
1031307Sszhou 	 */
1032307Sszhou 	freemsg(mp);
1033307Sszhou 	return (-1);
1034307Sszhou }
1035