xref: /onnv-gate/usr/src/uts/common/io/strplumb.c (revision 5710:3296988cc8c2)
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  */
995648Ssetje #define	STRPLUMB_IDENT	"STREAMS Plumbing Module"
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 {
338*5710Ssetje 	char			*devpath;
3390Sstevel@tonic-gate 	dev_info_t		*dip;
3400Sstevel@tonic-gate 	const char		*driver;
3410Sstevel@tonic-gate 	int			instance;
3425648Ssetje #ifdef	_OBP
3435648Ssetje 	char			stripped_path[OBP_MAXPATHLEN];
3445648Ssetje #endif
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	if (strncmp(rootfs.bo_fstype, "nfs", 3) == 0)
3470Sstevel@tonic-gate 		devpath = rootfs.bo_name;
3480Sstevel@tonic-gate 	else
3490Sstevel@tonic-gate 		devpath = strplumb_get_netdev_path();
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	if (devpath != NULL) {
3520Sstevel@tonic-gate 		DBG1("resolving boot-path: %s\n", devpath);
353*5710Ssetje #ifdef _OBP
354*5710Ssetje 		/*
355*5710Ssetje 		 * OBP passes options e.g, "net:dhcp"
356*5710Ssetje 		 * remove them here
357*5710Ssetje 		 */
358*5710Ssetje 		prom_strip_options(devpath, stripped_path);
359*5710Ssetje 		devpath = stripped_path;
360*5710Ssetje #endif
3610Sstevel@tonic-gate 		/*
3620Sstevel@tonic-gate 		 * Hold the devi since this is the root device.
3630Sstevel@tonic-gate 		 */
3640Sstevel@tonic-gate 		if ((dip = e_ddi_hold_devi_by_path(devpath, 0)) == NULL) {
3650Sstevel@tonic-gate 			printf("strplumb: unable to hold root device: %s\n",
3660Sstevel@tonic-gate 			    devpath);
3670Sstevel@tonic-gate 			return (ENXIO);
3680Sstevel@tonic-gate 		}
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 		driver = ddi_driver_name(dip);
3710Sstevel@tonic-gate 		instance = ddi_get_instance(dip);
3720Sstevel@tonic-gate 	} else {
3730Sstevel@tonic-gate 		if (ndev_name == NULL)
3740Sstevel@tonic-gate 			return (ENODEV);
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 		DBG2("using ndev_name (%s) ndev_unit (%d)\n", ndev_name,
3770Sstevel@tonic-gate 		    ndev_unit);
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 		if (i_ddi_attach_hw_nodes(ndev_name) != DDI_SUCCESS) {
3800Sstevel@tonic-gate 			printf("strplumb: cannot load ndev_name '%s'\n",
3810Sstevel@tonic-gate 			    ndev_name);
3820Sstevel@tonic-gate 			return (ENXIO);
3830Sstevel@tonic-gate 		}
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 		driver = ndev_name;
3860Sstevel@tonic-gate 		instance = ndev_unit;
3870Sstevel@tonic-gate 	}
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	(void) snprintf(rootfs.bo_devname, BO_MAXOBJNAME,
390269Sericheng 	    "/devices/pseudo/clone@0:%s", driver);
3910Sstevel@tonic-gate 	(void) snprintf(rootfs.bo_ifname, BO_MAXOBJNAME, "%s%d",
3920Sstevel@tonic-gate 	    driver, instance);
3930Sstevel@tonic-gate 	rootfs.bo_ppa = instance;
394269Sericheng 	return (0);
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate static int
3980Sstevel@tonic-gate getifflags(ldi_handle_t lh, struct lifreq *lifrp)
3990Sstevel@tonic-gate {
4000Sstevel@tonic-gate 	struct strioctl	iocb;
4010Sstevel@tonic-gate 	int		rval;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	iocb.ic_cmd = SIOCGLIFFLAGS;
4040Sstevel@tonic-gate 	iocb.ic_timout = 15;
4050Sstevel@tonic-gate 	iocb.ic_len = sizeof (struct lifreq);
4060Sstevel@tonic-gate 	iocb.ic_dp = (char *)lifrp;
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate static int
4130Sstevel@tonic-gate setifname(ldi_handle_t lh, struct lifreq *lifrp)
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate 	struct strioctl	iocb;
4160Sstevel@tonic-gate 	int		rval;
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	iocb.ic_cmd = SIOCSLIFNAME;
4190Sstevel@tonic-gate 	iocb.ic_timout = 15;
4200Sstevel@tonic-gate 	iocb.ic_len = sizeof (struct lifreq);
4210Sstevel@tonic-gate 	iocb.ic_dp = (char *)lifrp;
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
4240Sstevel@tonic-gate }
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate static int
4270Sstevel@tonic-gate strplumb_dev(ldi_ident_t li)
4280Sstevel@tonic-gate {
4290Sstevel@tonic-gate 	ldi_handle_t	lh = NULL;
4300Sstevel@tonic-gate 	ldi_handle_t	mux_lh = NULL;
4310Sstevel@tonic-gate 	int		err;
4320Sstevel@tonic-gate 	struct lifreq	lifr;
4330Sstevel@tonic-gate 	struct ifreq	ifr;
4340Sstevel@tonic-gate 	int		rval;
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	bzero(&lifr, sizeof (struct lifreq));
4370Sstevel@tonic-gate 	bzero(&ifr, sizeof (ifr));
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	/*
4400Sstevel@tonic-gate 	 * Now set up the links. Ultimately, we should have two streams
4410Sstevel@tonic-gate 	 * permanently linked underneath UDP (which is actually IP with UDP
4420Sstevel@tonic-gate 	 * autopushed). One stream consists of the ARP-[ifname] combination,
4430Sstevel@tonic-gate 	 * while the other consists of ARP-IP-[ifname]. The second combination
4440Sstevel@tonic-gate 	 * seems a little weird, but is linked underneath UDP just to keep it
4450Sstevel@tonic-gate 	 * around.
4460Sstevel@tonic-gate 	 *
4470Sstevel@tonic-gate 	 * We pin underneath UDP here to match what is done in ifconfig(1m);
4480Sstevel@tonic-gate 	 * otherwise, ifconfig will be unable to unplumb the stream (the major
4490Sstevel@tonic-gate 	 * number and mux id must both match for a successful I_PUNLINK).
4500Sstevel@tonic-gate 	 *
4510Sstevel@tonic-gate 	 * There are subtleties in the plumbing which make it essential to
4520Sstevel@tonic-gate 	 * follow the logic used in ifconfig(1m) very closely.
4530Sstevel@tonic-gate 	 */
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	/*
4560Sstevel@tonic-gate 	 * Plumb UDP-ARP-IP-<dev>
4570Sstevel@tonic-gate 	 */
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
4600Sstevel@tonic-gate 	    &lh, li)) != 0) {
4610Sstevel@tonic-gate 		printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
4620Sstevel@tonic-gate 		    err);
4630Sstevel@tonic-gate 		goto done;
4640Sstevel@tonic-gate 	}
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)IP, FKIOCTL, CRED(),
4680Sstevel@tonic-gate 	    &rval)) != 0) {
4690Sstevel@tonic-gate 		printf("strplumb: push IP failed: %d\n", err);
4700Sstevel@tonic-gate 		goto done;
4710Sstevel@tonic-gate 	}
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	if ((err = getifflags(lh, &lifr)) != 0)
4740Sstevel@tonic-gate 		goto done;
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	lifr.lifr_flags |= IFF_IPV4;
4770Sstevel@tonic-gate 	lifr.lifr_flags &= ~IFF_IPV6;
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)ARP, FKIOCTL, CRED(),
4800Sstevel@tonic-gate 	    &rval)) != 0) {
4810Sstevel@tonic-gate 		printf("strplumb: push ARP failed: %d\n", err);
4820Sstevel@tonic-gate 		goto done;
4830Sstevel@tonic-gate 	}
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, rootfs.bo_ifname,
4860Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
4870Sstevel@tonic-gate 	lifr.lifr_ppa = rootfs.bo_ppa;
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	if ((err = setifname(lh, &lifr)) != 0)
4900Sstevel@tonic-gate 		goto done;
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	/* Get the flags and check if ARP is needed */
4930Sstevel@tonic-gate 	if ((err = getifflags(lh, &lifr)) != 0) {
4940Sstevel@tonic-gate 		printf("strplumb: getifflags %s IP failed, error %d\n",
4950Sstevel@tonic-gate 		    lifr.lifr_name, err);
4960Sstevel@tonic-gate 		goto done;
4970Sstevel@tonic-gate 	}
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	/* Pop out ARP if not needed */
5000Sstevel@tonic-gate 	if (lifr.lifr_flags & IFF_NOARP) {
5010Sstevel@tonic-gate 		err = ldi_ioctl(lh, I_POP, (intptr_t)0, FKIOCTL, CRED(),
5020Sstevel@tonic-gate 		    &rval);
5030Sstevel@tonic-gate 		if (err != 0) {
5040Sstevel@tonic-gate 			printf("strplumb: pop ARP failed, error %d\n", err);
5050Sstevel@tonic-gate 			goto done;
5060Sstevel@tonic-gate 		}
5070Sstevel@tonic-gate 	}
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	if ((err = ldi_open_by_name(UDPDEV, FREAD|FWRITE, CRED(), &mux_lh,
5100Sstevel@tonic-gate 	    li)) != 0) {
5110Sstevel@tonic-gate 		printf("strplumb: open of UDPDEV failed: %d\n", err);
5120Sstevel@tonic-gate 		goto done;
5130Sstevel@tonic-gate 	}
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
5160Sstevel@tonic-gate 	    FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
5170Sstevel@tonic-gate 	    &(ifr.ifr_ip_muxid))) != 0) {
5180Sstevel@tonic-gate 		printf("strplumb: plink UDP-ARP-IP-%s failed: %d\n",
5190Sstevel@tonic-gate 		    rootfs.bo_ifname, err);
5200Sstevel@tonic-gate 		goto done;
5210Sstevel@tonic-gate 	}
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	DBG2("UDP-ARP-IP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_ip_muxid);
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
5260Sstevel@tonic-gate 	lh = NULL;
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	/*
5290Sstevel@tonic-gate 	 * Plumb UDP-ARP-<dev>
5300Sstevel@tonic-gate 	 */
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
5330Sstevel@tonic-gate 	    &lh, li)) != 0) {
5340Sstevel@tonic-gate 		printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
5350Sstevel@tonic-gate 		    err);
5360Sstevel@tonic-gate 		goto done;
5370Sstevel@tonic-gate 	}
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)ARP, FKIOCTL, CRED(),
5400Sstevel@tonic-gate 	    &rval)) != 0) {
5410Sstevel@tonic-gate 		printf("strplumb: push ARP failed: %d\n", err);
5420Sstevel@tonic-gate 		goto done;
5430Sstevel@tonic-gate 	}
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	if ((err = setifname(lh, &lifr)) != 0)
5460Sstevel@tonic-gate 		goto done;
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
5490Sstevel@tonic-gate 	    FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
5500Sstevel@tonic-gate 	    &(ifr.ifr_arp_muxid))) != 0) {
5510Sstevel@tonic-gate 		printf("strplumb: plink UDP-ARP-%s failed: %d\n",
5520Sstevel@tonic-gate 		    rootfs.bo_ifname, err);
5530Sstevel@tonic-gate 		goto done;
5540Sstevel@tonic-gate 	}
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	DBG2("UDP-ARP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_arp_muxid);
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	/*
5590Sstevel@tonic-gate 	 * Cache the mux ids.
5600Sstevel@tonic-gate 	 */
5610Sstevel@tonic-gate 	(void) strlcpy(ifr.ifr_name, rootfs.bo_ifname, sizeof (ifr.ifr_name));
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, SIOCSIFMUXID, (intptr_t)&ifr, FKIOCTL,
5640Sstevel@tonic-gate 	    CRED(), &rval)) != 0) {
5650Sstevel@tonic-gate 		printf("strplumb: SIOCSIFMUXID failed: %d\n", err);
5660Sstevel@tonic-gate 		goto done;
5670Sstevel@tonic-gate 	}
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate done:
5700Sstevel@tonic-gate 	if (lh != NULL)
5710Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	if (mux_lh != NULL)
5740Sstevel@tonic-gate 		(void) ldi_close(mux_lh, FREAD|FWRITE, CRED());
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	return (err);
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate /*
5800Sstevel@tonic-gate  * Do streams plumbing for internet protocols.
5810Sstevel@tonic-gate  */
5820Sstevel@tonic-gate int
5830Sstevel@tonic-gate strplumb(void)
5840Sstevel@tonic-gate {
5850Sstevel@tonic-gate 	ldi_ident_t	li;
5860Sstevel@tonic-gate 	int		err;
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	if ((err = strplumb_init()) != 0)
5890Sstevel@tonic-gate 		return (err);
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	if ((err = strplumb_autopush()) != 0)
5920Sstevel@tonic-gate 		return (err);
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	if ((err = ldi_ident_from_mod(&modlinkage, &li)) != 0)
5950Sstevel@tonic-gate 		return (err);
5960Sstevel@tonic-gate 
5973448Sdh155122 	/*
5983448Sdh155122 	 * Setup the TCP and SCTP default queues for the global stack.
5993448Sdh155122 	 * tcp/sctp_stack_init will do this for additional stack instances.
6003448Sdh155122 	 */
6010Sstevel@tonic-gate 	if ((err = strplumb_sctpq(li)) != 0)
6020Sstevel@tonic-gate 		goto done;
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	if ((err = strplumb_tcpq(li)) != 0)
6050Sstevel@tonic-gate 		goto done;
6060Sstevel@tonic-gate 
607269Sericheng 	if ((err = resolve_boot_path()) != 0)
6080Sstevel@tonic-gate 		goto done;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	DBG1("rootfs.bo_devname: %s\n", rootfs.bo_devname);
6110Sstevel@tonic-gate 	DBG1("rootfs.bo_ifname: %s\n", rootfs.bo_ifname);
6120Sstevel@tonic-gate 	DBG1("rootfs.bo_ppa: %d\n", rootfs.bo_ppa);
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	if ((err = strplumb_dev(li)) != 0)
6150Sstevel@tonic-gate 		goto done;
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate done:
6180Sstevel@tonic-gate 	ldi_ident_release(li);
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	return (err);
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate /* multiboot:  diskless boot interface discovery */
6240Sstevel@tonic-gate 
6255648Ssetje #ifndef	_OBP
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate static uchar_t boot_macaddr[16];
6280Sstevel@tonic-gate static int boot_maclen;
6290Sstevel@tonic-gate static uchar_t *getmacaddr(dev_info_t *dip, int *maclen);
6300Sstevel@tonic-gate static int matchmac(dev_info_t *dip, void *arg);
631307Sszhou int dl_attach(ldi_handle_t lh, int unit);
632307Sszhou int dl_bind(ldi_handle_t lh, uint_t sap, uint_t max_conn,
6330Sstevel@tonic-gate     uint_t service, uint_t conn_mgmt);
634307Sszhou int dl_phys_addr(ldi_handle_t lh, struct ether_addr *eaddr);
6350Sstevel@tonic-gate 
6365648Ssetje #endif  /* !_OBP */
6374172Ssetje 
6380Sstevel@tonic-gate char *
6390Sstevel@tonic-gate strplumb_get_netdev_path(void)
6400Sstevel@tonic-gate {
641*5710Ssetje #ifdef	_OBP
642*5710Ssetje 	char fstype[OBP_MAXPROPNAME];
643*5710Ssetje 
644*5710Ssetje 	if (bop_getprop("fstype", fstype) == -1)
645*5710Ssetje 		return (NULL);
646*5710Ssetje 
647*5710Ssetje 	if (strncmp(fstype, "nfs", 3) == 0)
648*5710Ssetje 		return (prom_bootpath());
649*5710Ssetje 	else
650*5710Ssetje 		return (NULL);
651*5710Ssetje #else
652*5710Ssetje 
6530Sstevel@tonic-gate 	char *macstr, *devpath = NULL;
6540Sstevel@tonic-gate 	uchar_t *bootp;
6555648Ssetje 	uint_t bootp_len;
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
6580Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, BP_BOOT_MAC, &macstr) == DDI_SUCCESS) {
6590Sstevel@tonic-gate 		/*
6600Sstevel@tonic-gate 		 * hard coded ether mac len for booting floppy on
6610Sstevel@tonic-gate 		 * machines with old cards
6620Sstevel@tonic-gate 		 */
6630Sstevel@tonic-gate 		boot_maclen = ether_aton(macstr, boot_macaddr);
6640Sstevel@tonic-gate 		if (boot_maclen != 6) {
6650Sstevel@tonic-gate 			cmn_err(CE_WARN,
6660Sstevel@tonic-gate 			    "malformed boot_mac property, %d bytes",
6670Sstevel@tonic-gate 			    boot_maclen);
6680Sstevel@tonic-gate 		}
6690Sstevel@tonic-gate 		ddi_prop_free(macstr);
6700Sstevel@tonic-gate 	} else if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, ddi_root_node(),
6710Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, BP_BOOTP_RESPONSE, &bootp, &bootp_len)
6720Sstevel@tonic-gate 	    == DDI_SUCCESS) {
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 		/*
6750Sstevel@tonic-gate 		 * These offsets are defined by dhcp standard
6760Sstevel@tonic-gate 		 * Should use structure offsets
6770Sstevel@tonic-gate 		 */
6780Sstevel@tonic-gate 		boot_maclen = *(bootp + 2);
6790Sstevel@tonic-gate 		ASSERT(boot_maclen <= 16);
6805648Ssetje 		bcopy(bootp + 28, boot_macaddr, boot_maclen);
6810Sstevel@tonic-gate 
6825648Ssetje 		dhcack = kmem_alloc(bootp_len, KM_SLEEP);
6835648Ssetje 		bcopy(bootp, dhcack, bootp_len);
6845648Ssetje 		dhcacklen = bootp_len;
6855648Ssetje 
6860Sstevel@tonic-gate 		ddi_prop_free(bootp);
6870Sstevel@tonic-gate 	} else
6880Sstevel@tonic-gate 		return (NULL);
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	ddi_walk_devs(ddi_root_node(), matchmac, (void *)&devpath);
6910Sstevel@tonic-gate 	return (devpath);
6924172Ssetje 
693*5710Ssetje #endif  /* _OBP */
6940Sstevel@tonic-gate }
6950Sstevel@tonic-gate 
6965648Ssetje #ifndef _OBP
6974172Ssetje 
6980Sstevel@tonic-gate /*
6990Sstevel@tonic-gate  * Get boot path from the boot_mac address
7000Sstevel@tonic-gate  */
7010Sstevel@tonic-gate /*ARGSUSED*/
7020Sstevel@tonic-gate static int
7030Sstevel@tonic-gate matchmac(dev_info_t *dip, void *arg)
7040Sstevel@tonic-gate {
7050Sstevel@tonic-gate 	char **devpathp = (char **)arg;
7060Sstevel@tonic-gate 	char *model_str;
7070Sstevel@tonic-gate 	uchar_t *macaddr;
7080Sstevel@tonic-gate 	int maclen;
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	/* XXX Should use "device-type" per IEEE 1275 */
7110Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0,
7120Sstevel@tonic-gate 	    "model", &model_str) != DDI_SUCCESS)
7130Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	if (strcmp(model_str, "Ethernet controller") != 0) {
7160Sstevel@tonic-gate 		ddi_prop_free(model_str);
7170Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7180Sstevel@tonic-gate 	}
7190Sstevel@tonic-gate 	ddi_prop_free(model_str);
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	/* We have a network device now */
7220Sstevel@tonic-gate 	if (i_ddi_attach_node_hierarchy(dip) != DDI_SUCCESS) {
7230Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7240Sstevel@tonic-gate 	}
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	ASSERT(boot_maclen != 0);
7270Sstevel@tonic-gate 	macaddr = getmacaddr(dip, &maclen);
7280Sstevel@tonic-gate 	if (macaddr == NULL)
7290Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	if (maclen != boot_maclen ||
7320Sstevel@tonic-gate 	    bcmp(macaddr, boot_macaddr, maclen) != 0) {
7330Sstevel@tonic-gate 		kmem_free(macaddr, maclen);
7340Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
7350Sstevel@tonic-gate 	}
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	/* found hardware with the mac address */
7380Sstevel@tonic-gate 	(void) localetheraddr((struct ether_addr *)macaddr, NULL);
7390Sstevel@tonic-gate 	kmem_free(macaddr, maclen);
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	*devpathp = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7420Sstevel@tonic-gate 	(void) ddi_pathname(dip, *devpathp);
7430Sstevel@tonic-gate 
7445648Ssetje 	/* fill in dhcifname */
7455648Ssetje 	if (dhcack) {
7465648Ssetje 		(void) snprintf(dhcifname, IFNAMSIZ, "%s%d",
7470Sstevel@tonic-gate 		    ddi_driver_name(dip), i_ddi_devi_get_ppa(dip));
7485648Ssetje 	}
7490Sstevel@tonic-gate 	return (DDI_WALK_TERMINATE);
7500Sstevel@tonic-gate }
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate static uchar_t *
7530Sstevel@tonic-gate getmacaddr_gldv3(char *drv, int inst, int *maclenp)
7540Sstevel@tonic-gate {
7550Sstevel@tonic-gate 	char ifname[16];
7560Sstevel@tonic-gate 	mac_handle_t mh;
7570Sstevel@tonic-gate 	uchar_t *macaddr;
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	(void) snprintf(ifname, sizeof (ifname), "%s%d", drv, inst);
7602343Sseb 	if (mac_open(ifname, inst, &mh) < 0) {
7610Sstevel@tonic-gate 		return (NULL);
7620Sstevel@tonic-gate 	}
7630Sstevel@tonic-gate 	*maclenp = sizeof (struct ether_addr);
7640Sstevel@tonic-gate 	macaddr = kmem_alloc(*maclenp, KM_SLEEP);
7650Sstevel@tonic-gate 	mac_unicst_get(mh, macaddr);
7660Sstevel@tonic-gate 	mac_close(mh);
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	return (macaddr);
7690Sstevel@tonic-gate }
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate static uchar_t *
7720Sstevel@tonic-gate getmacaddr(dev_info_t *dip, int *maclenp)
7730Sstevel@tonic-gate {
7740Sstevel@tonic-gate 	int rc, ppa;
7750Sstevel@tonic-gate 	ldi_ident_t li;
7760Sstevel@tonic-gate 	ldi_handle_t lh;
7770Sstevel@tonic-gate 	char *drv_name = (char *)ddi_driver_name(dip);
7780Sstevel@tonic-gate 	char *clonepath;
7790Sstevel@tonic-gate 	uchar_t *macaddr = NULL;
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	/* a simpler way to get mac address for GLDv3 drivers */
7820Sstevel@tonic-gate 	if (GLDV3_DRV(ddi_name_to_major(drv_name))) {
7830Sstevel@tonic-gate 		return (getmacaddr_gldv3(drv_name, ddi_get_instance(dip),
7840Sstevel@tonic-gate 		    maclenp));
7850Sstevel@tonic-gate 	}
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	if (rc = ldi_ident_from_mod(&modlinkage, &li)) {
7880Sstevel@tonic-gate 		cmn_err(CE_WARN,
7890Sstevel@tonic-gate 		    "getmacaddr: ldi_ident_from_mod failed: %d\n", rc);
7900Sstevel@tonic-gate 		return (NULL);
7910Sstevel@tonic-gate 	}
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	clonepath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
7940Sstevel@tonic-gate 	(void) snprintf(clonepath, MAXPATHLEN,
7950Sstevel@tonic-gate 	    "/devices/pseudo/clone@0:%s", drv_name);
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	rc = ldi_open_by_name(clonepath, FREAD|FWRITE, CRED(), &lh, li);
7980Sstevel@tonic-gate 	ldi_ident_release(li);
7990Sstevel@tonic-gate 	if (rc) {
8000Sstevel@tonic-gate 		cmn_err(CE_WARN,
8010Sstevel@tonic-gate 		    "getmacaddr: ldi_open_by_name(%s) failed: %d\n",
8020Sstevel@tonic-gate 		    clonepath, rc);
8030Sstevel@tonic-gate 		kmem_free(clonepath, MAXPATHLEN);
8040Sstevel@tonic-gate 		return (NULL);
8050Sstevel@tonic-gate 	}
8060Sstevel@tonic-gate 	kmem_free(clonepath, MAXPATHLEN);
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	ppa = i_ddi_devi_get_ppa(dip);
8090Sstevel@tonic-gate 	if ((dl_attach(lh, ppa) != 0) ||
8100Sstevel@tonic-gate 	    (dl_bind(lh, ETHERTYPE_IP, 0, DL_CLDLS, 0) != 0)) {
8110Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
8120Sstevel@tonic-gate 		cmn_err(CE_WARN,
8130Sstevel@tonic-gate 		    "getmacaddr: dl_attach/bind(%s%d) failed: %d\n",
8140Sstevel@tonic-gate 		    drv_name, ppa, rc);
8150Sstevel@tonic-gate 		return (NULL);
8160Sstevel@tonic-gate 	}
8170Sstevel@tonic-gate 	*maclenp = sizeof (struct ether_addr);
8180Sstevel@tonic-gate 	macaddr = kmem_alloc(*maclenp, KM_SLEEP);
8190Sstevel@tonic-gate 	if (dl_phys_addr(lh, (struct ether_addr *)macaddr) != 0) {
8200Sstevel@tonic-gate 		kmem_free(macaddr, *maclenp);
8210Sstevel@tonic-gate 		macaddr = NULL;
8220Sstevel@tonic-gate 		*maclenp = 0;
8230Sstevel@tonic-gate 		cmn_err(CE_WARN,
8240Sstevel@tonic-gate 		    "getmacaddr: dl_macaddr(%s%d) failed: %d\n",
8250Sstevel@tonic-gate 		    drv_name, ppa, rc);
8260Sstevel@tonic-gate 	}
8270Sstevel@tonic-gate 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
8280Sstevel@tonic-gate 	return (macaddr);
8290Sstevel@tonic-gate }
8300Sstevel@tonic-gate 
8315648Ssetje #endif	/* !_OBP */
832307Sszhou 
833307Sszhou int
834307Sszhou dl_attach(ldi_handle_t lh, int unit)
835307Sszhou {
836307Sszhou 	dl_attach_req_t *attach_req;
837307Sszhou 	union DL_primitives *dl_prim;
838307Sszhou 	mblk_t *mp;
839307Sszhou 	int error;
840307Sszhou 
841307Sszhou 	if ((mp = allocb(sizeof (dl_attach_req_t), BPRI_MED)) == NULL) {
842307Sszhou 		cmn_err(CE_WARN, "dl_attach: allocb failed");
843307Sszhou 		return (ENOSR);
844307Sszhou 	}
845307Sszhou 	mp->b_datap->db_type = M_PROTO;
846307Sszhou 	mp->b_wptr += sizeof (dl_attach_req_t);
847307Sszhou 
848307Sszhou 	attach_req = (dl_attach_req_t *)mp->b_rptr;
849307Sszhou 	attach_req->dl_primitive = DL_ATTACH_REQ;
850307Sszhou 	attach_req->dl_ppa = unit;
851307Sszhou 
852307Sszhou 	(void) ldi_putmsg(lh, mp);
853307Sszhou 	if ((error = ldi_getmsg(lh, &mp, (timestruc_t *)NULL)) != 0) {
8545397Syz147064 		cmn_err(CE_NOTE, "!dl_attach: ldi_getmsg failed: %d", error);
855307Sszhou 		return (error);
856307Sszhou 	}
857307Sszhou 
858307Sszhou 	dl_prim = (union DL_primitives *)mp->b_rptr;
859307Sszhou 	switch (dl_prim->dl_primitive) {
860307Sszhou 	case DL_OK_ACK:
861307Sszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_ok_ack_t)) {
8625397Syz147064 			cmn_err(CE_NOTE,
8635397Syz147064 			    "!dl_attach: DL_OK_ACK protocol error");
864307Sszhou 			break;
865307Sszhou 		}
866307Sszhou 		if (((dl_ok_ack_t *)dl_prim)->dl_correct_primitive !=
867307Sszhou 		    DL_ATTACH_REQ) {
8685397Syz147064 			cmn_err(CE_NOTE, "!dl_attach: DL_OK_ACK rtnd prim %u",
869307Sszhou 			    ((dl_ok_ack_t *)dl_prim)->dl_correct_primitive);
870307Sszhou 			break;
871307Sszhou 		}
872307Sszhou 		freemsg(mp);
873307Sszhou 		return (0);
874307Sszhou 
875307Sszhou 	case DL_ERROR_ACK:
876307Sszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_error_ack_t)) {
8775397Syz147064 			cmn_err(CE_NOTE,
8785397Syz147064 			    "!dl_attach: DL_ERROR_ACK protocol error");
879307Sszhou 			break;
880307Sszhou 		}
881307Sszhou 		break;
882307Sszhou 
883307Sszhou 	default:
8845397Syz147064 		cmn_err(CE_NOTE, "!dl_attach: bad ACK header %u",
8855397Syz147064 		    dl_prim->dl_primitive);
886307Sszhou 		break;
887307Sszhou 	}
888307Sszhou 
889307Sszhou 	/*
890307Sszhou 	 * Error return only.
891307Sszhou 	 */
892307Sszhou 	freemsg(mp);
893307Sszhou 	return (-1);
894307Sszhou }
895307Sszhou 
896307Sszhou int
897307Sszhou dl_bind(ldi_handle_t lh, uint_t sap, uint_t max_conn, uint_t service,
898307Sszhou 	uint_t conn_mgmt)
899307Sszhou {
900307Sszhou 	dl_bind_req_t *bind_req;
901307Sszhou 	union DL_primitives *dl_prim;
902307Sszhou 	mblk_t *mp;
903307Sszhou 	int error;
904307Sszhou 
905307Sszhou 	if ((mp = allocb(sizeof (dl_bind_req_t), BPRI_MED)) == NULL) {
906307Sszhou 		cmn_err(CE_WARN, "dl_bind: allocb failed");
907307Sszhou 		return (ENOSR);
908307Sszhou 	}
909307Sszhou 	mp->b_datap->db_type = M_PROTO;
910307Sszhou 
911307Sszhou 	bind_req = (dl_bind_req_t *)mp->b_wptr;
912307Sszhou 	mp->b_wptr += sizeof (dl_bind_req_t);
913307Sszhou 	bind_req->dl_primitive = DL_BIND_REQ;
914307Sszhou 	bind_req->dl_sap = sap;
915307Sszhou 	bind_req->dl_max_conind = max_conn;
916307Sszhou 	bind_req->dl_service_mode = service;
917307Sszhou 	bind_req->dl_conn_mgmt = conn_mgmt;
918307Sszhou 	bind_req->dl_xidtest_flg = 0;
919307Sszhou 
920307Sszhou 	(void) ldi_putmsg(lh, mp);
921307Sszhou 	if ((error = ldi_getmsg(lh, &mp, (timestruc_t *)NULL)) != 0) {
9225397Syz147064 		cmn_err(CE_NOTE, "!dl_bind: ldi_getmsg failed: %d", error);
923307Sszhou 		return (error);
924307Sszhou 	}
925307Sszhou 
926307Sszhou 	dl_prim = (union DL_primitives *)mp->b_rptr;
927307Sszhou 	switch (dl_prim->dl_primitive) {
928307Sszhou 	case DL_BIND_ACK:
929307Sszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_bind_ack_t)) {
9305397Syz147064 			cmn_err(CE_NOTE,
9315397Syz147064 			    "!dl_bind: DL_BIND_ACK protocol error");
932307Sszhou 			break;
933307Sszhou 		}
934307Sszhou 		if (((dl_bind_ack_t *)dl_prim)->dl_sap != sap) {
9355397Syz147064 			cmn_err(CE_NOTE, "!dl_bind: DL_BIND_ACK bad sap %u",
936307Sszhou 			    ((dl_bind_ack_t *)dl_prim)->dl_sap);
937307Sszhou 			break;
938307Sszhou 		}
939307Sszhou 		freemsg(mp);
940307Sszhou 		return (0);
941307Sszhou 
942307Sszhou 	case DL_ERROR_ACK:
943307Sszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_error_ack_t)) {
9445397Syz147064 			cmn_err(CE_NOTE,
9455397Syz147064 			    "!dl_bind: DL_ERROR_ACK protocol error");
946307Sszhou 			break;
947307Sszhou 		}
948307Sszhou 		break;
949307Sszhou 
950307Sszhou 	default:
9515397Syz147064 		cmn_err(CE_NOTE, "!dl_bind: bad ACK header %u",
9525397Syz147064 		    dl_prim->dl_primitive);
953307Sszhou 		break;
954307Sszhou 	}
955307Sszhou 
956307Sszhou 	/*
957307Sszhou 	 * Error return only.
958307Sszhou 	 */
959307Sszhou 	freemsg(mp);
960307Sszhou 	return (-1);
961307Sszhou }
962307Sszhou 
963307Sszhou int
964307Sszhou dl_phys_addr(ldi_handle_t lh, struct ether_addr *eaddr)
965307Sszhou {
966307Sszhou 	dl_phys_addr_req_t *phys_addr_req;
967307Sszhou 	dl_phys_addr_ack_t *phys_addr_ack;
968307Sszhou 	union DL_primitives *dl_prim;
969307Sszhou 	mblk_t *mp;
970307Sszhou 	int error;
971307Sszhou 	uchar_t *addrp;
972307Sszhou 	timestruc_t tv;
973307Sszhou 
974307Sszhou 	if ((mp = allocb(sizeof (dl_phys_addr_req_t), BPRI_MED)) ==
975307Sszhou 	    (mblk_t *)NULL) {
976307Sszhou 		cmn_err(CE_WARN, "dl_phys_addr: allocb failed");
977307Sszhou 		return (ENOSR);
978307Sszhou 	}
979307Sszhou 	mp->b_datap->db_type = M_PROTO;
980307Sszhou 	mp->b_wptr += sizeof (dl_phys_addr_req_t);
981307Sszhou 
982307Sszhou 	phys_addr_req = (dl_phys_addr_req_t *)mp->b_rptr;
983307Sszhou 	phys_addr_req->dl_primitive = DL_PHYS_ADDR_REQ;
984307Sszhou 	phys_addr_req->dl_addr_type = DL_CURR_PHYS_ADDR;
985307Sszhou 
986307Sszhou 	/*
987307Sszhou 	 * In case some provider doesn't implement or nack the
988307Sszhou 	 * request just wait for 15 seconds.
989307Sszhou 	 */
990307Sszhou 	tv.tv_sec = 15;
991307Sszhou 	tv.tv_nsec = 0;
992307Sszhou 
993307Sszhou 	(void) ldi_putmsg(lh, mp);
994307Sszhou 	error = ldi_getmsg(lh, &mp, &tv);
995307Sszhou 	if (error == ETIME) {
9965397Syz147064 		cmn_err(CE_NOTE, "!dl_phys_addr: timed out");
997307Sszhou 		return (-1);
998307Sszhou 	} else if (error != 0) {
9995397Syz147064 		cmn_err(CE_NOTE, "!dl_phys_addr: ldi_getmsg failed: %d", error);
1000307Sszhou 		return (error);
1001307Sszhou 	}
1002307Sszhou 
1003307Sszhou 	dl_prim = (union DL_primitives *)mp->b_rptr;
1004307Sszhou 	switch (dl_prim->dl_primitive) {
1005307Sszhou 	case DL_PHYS_ADDR_ACK:
1006307Sszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_phys_addr_ack_t)) {
10075397Syz147064 			cmn_err(CE_NOTE, "!dl_phys_addr: "
10085397Syz147064 			    "DL_PHYS_ADDR_ACK protocol error");
1009307Sszhou 			break;
1010307Sszhou 		}
1011307Sszhou 		phys_addr_ack = &dl_prim->physaddr_ack;
1012307Sszhou 		if (phys_addr_ack->dl_addr_length != sizeof (*eaddr)) {
10135397Syz147064 			cmn_err(CE_NOTE,
10145397Syz147064 			    "!dl_phys_addr: DL_PHYS_ADDR_ACK bad len %u",
10155240Snordmark 			    phys_addr_ack->dl_addr_length);
1016307Sszhou 			break;
1017307Sszhou 		}
1018307Sszhou 		if (phys_addr_ack->dl_addr_length +
1019307Sszhou 		    phys_addr_ack->dl_addr_offset > (mp->b_wptr-mp->b_rptr)) {
10205397Syz147064 			cmn_err(CE_NOTE,
10215397Syz147064 			    "!dl_phys_addr: DL_PHYS_ADDR_ACK bad len %u",
10225240Snordmark 			    phys_addr_ack->dl_addr_length);
1023307Sszhou 			break;
1024307Sszhou 		}
1025307Sszhou 		addrp = mp->b_rptr + phys_addr_ack->dl_addr_offset;
1026307Sszhou 		bcopy(addrp, eaddr, sizeof (*eaddr));
1027307Sszhou 		freemsg(mp);
1028307Sszhou 		return (0);
1029307Sszhou 
1030307Sszhou 	case DL_ERROR_ACK:
1031307Sszhou 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_error_ack_t)) {
10325397Syz147064 			cmn_err(CE_NOTE,
10335397Syz147064 			    "!dl_phys_addr: DL_ERROR_ACK protocol error");
1034307Sszhou 			break;
1035307Sszhou 		}
1036307Sszhou 
1037307Sszhou 		break;
1038307Sszhou 
1039307Sszhou 	default:
10405397Syz147064 		cmn_err(CE_NOTE, "!dl_phys_addr: bad ACK header %u",
10415240Snordmark 		    dl_prim->dl_primitive);
1042307Sszhou 		break;
1043307Sszhou 	}
1044307Sszhou 
1045307Sszhou 	/*
1046307Sszhou 	 * Error return only.
1047307Sszhou 	 */
1048307Sszhou 	freemsg(mp);
1049307Sszhou 	return (-1);
1050307Sszhou }
1051