xref: /onnv-gate/usr/src/uts/common/io/strplumb.c (revision 11042:2d6e217af1b4)
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 /*
2210822SJack.Meng@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include	<sys/param.h>
270Sstevel@tonic-gate #include	<sys/types.h>
280Sstevel@tonic-gate #include	<sys/user.h>
290Sstevel@tonic-gate #include	<sys/vfs.h>
300Sstevel@tonic-gate #include	<sys/vnode.h>
310Sstevel@tonic-gate #include	<sys/file.h>
320Sstevel@tonic-gate #include	<sys/stream.h>
330Sstevel@tonic-gate #include	<sys/stropts.h>
340Sstevel@tonic-gate #include	<sys/strsubr.h>
350Sstevel@tonic-gate #include	<sys/dlpi.h>
360Sstevel@tonic-gate #include	<sys/vnode.h>
370Sstevel@tonic-gate #include	<sys/socket.h>
380Sstevel@tonic-gate #include	<sys/sockio.h>
390Sstevel@tonic-gate #include	<net/if.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include	<sys/cred.h>
420Sstevel@tonic-gate #include	<sys/sysmacros.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include	<sys/sad.h>
450Sstevel@tonic-gate #include	<sys/kstr.h>
460Sstevel@tonic-gate #include	<sys/bootconf.h>
470Sstevel@tonic-gate #include	<sys/bootprops.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #include	<sys/errno.h>
500Sstevel@tonic-gate #include	<sys/modctl.h>
510Sstevel@tonic-gate #include	<sys/sunddi.h>
520Sstevel@tonic-gate #include	<sys/sunldi.h>
530Sstevel@tonic-gate #include	<sys/esunddi.h>
540Sstevel@tonic-gate #include	<sys/promif.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #include	<sys/strlog.h>
570Sstevel@tonic-gate #include	<sys/log.h>
580Sstevel@tonic-gate #include	<sys/ethernet.h>
590Sstevel@tonic-gate #include	<sys/ddi_implfuncs.h>
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #include	<sys/dld.h>
628275SEric Cheng #include	<sys/mac_client.h>
630Sstevel@tonic-gate 
640Sstevel@tonic-gate /*
650Sstevel@tonic-gate  * Debug Macros
660Sstevel@tonic-gate  */
670Sstevel@tonic-gate int	strplumbdebug = 0;
680Sstevel@tonic-gate 
698194SJack.Meng@Sun.COM extern ib_boot_prop_t *iscsiboot_prop;
708194SJack.Meng@Sun.COM 
710Sstevel@tonic-gate #define	DBG0(_f) \
720Sstevel@tonic-gate 	if (strplumbdebug != 0) \
730Sstevel@tonic-gate 		printf("strplumb: " _f)
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #define	DBG1(_f, _a) \
760Sstevel@tonic-gate 	if (strplumbdebug != 0) \
770Sstevel@tonic-gate 		printf("strplumb: " _f, (_a))
780Sstevel@tonic-gate 
790Sstevel@tonic-gate #define	DBG2(_f, _a, _b) \
800Sstevel@tonic-gate 	if (strplumbdebug != 0) \
810Sstevel@tonic-gate 		printf("strplumb: " _f, (_a), (_b))
820Sstevel@tonic-gate 
830Sstevel@tonic-gate #define	DBG3(_f, _a, _b, _c) \
840Sstevel@tonic-gate 	if (strplumbdebug != 0) \
850Sstevel@tonic-gate 		printf("strplumb: " _f, (_a), (_b), (_c))
860Sstevel@tonic-gate 
870Sstevel@tonic-gate /*
880Sstevel@tonic-gate  * Module linkage information for the kernel.
890Sstevel@tonic-gate  */
905648Ssetje #define	STRPLUMB_IDENT	"STREAMS Plumbing Module"
910Sstevel@tonic-gate 
920Sstevel@tonic-gate static struct modlmisc modlmisc = {
930Sstevel@tonic-gate 	&mod_miscops,
940Sstevel@tonic-gate 	STRPLUMB_IDENT
950Sstevel@tonic-gate };
960Sstevel@tonic-gate 
970Sstevel@tonic-gate static struct modlinkage modlinkage = {
980Sstevel@tonic-gate 	MODREV_1,
990Sstevel@tonic-gate 	&modlmisc,
1000Sstevel@tonic-gate 	NULL
1010Sstevel@tonic-gate };
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate int
1040Sstevel@tonic-gate _init(void)
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate int
1100Sstevel@tonic-gate _fini(void)
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate int
1160Sstevel@tonic-gate _info(struct modinfo *modinfop)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate #define	ARP		"arp"
1220Sstevel@tonic-gate #define	TCP		"tcp"
1230Sstevel@tonic-gate #define	TCP6		"tcp6"
1240Sstevel@tonic-gate #define	UDP		"udp"
1250Sstevel@tonic-gate #define	UDP6		"udp6"
1260Sstevel@tonic-gate #define	SCTP		"sctp"
1270Sstevel@tonic-gate #define	SCTP6		"sctp6"
1280Sstevel@tonic-gate #define	ICMP		"icmp"
1290Sstevel@tonic-gate #define	ICMP6		"icmp6"
1300Sstevel@tonic-gate #define	IP		"ip"
1310Sstevel@tonic-gate #define	IP6		"ip6"
1320Sstevel@tonic-gate #define	TIMOD		"timod"
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate #define	UDPDEV		"/devices/pseudo/udp@0:udp"
1350Sstevel@tonic-gate #define	TCP6DEV		"/devices/pseudo/tcp6@0:tcp6"
1368194SJack.Meng@Sun.COM #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
1370Sstevel@tonic-gate #define	SCTP6DEV	"/devices/pseudo/sctp6@0:sctp6"
1380Sstevel@tonic-gate #define	IP6DEV		"/devices/pseudo/ip6@0:ip6"
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate typedef struct strplumb_modspec {
1410Sstevel@tonic-gate 	char	*sm_type;
1420Sstevel@tonic-gate 	char	*sm_name;
1430Sstevel@tonic-gate } strplumb_modspec_t;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate static strplumb_modspec_t	strplumb_modlist[] = {
1460Sstevel@tonic-gate 	{ "drv", DLD_DRIVER_NAME },
1470Sstevel@tonic-gate 	{ "drv", IP },
1480Sstevel@tonic-gate 	{ "drv", IP6 },
1490Sstevel@tonic-gate 	{ "drv", TCP },
1500Sstevel@tonic-gate 	{ "drv", TCP6 },
1510Sstevel@tonic-gate 	{ "drv", UDP },
1520Sstevel@tonic-gate 	{ "drv", UDP6 },
1530Sstevel@tonic-gate 	{ "drv", SCTP },
1540Sstevel@tonic-gate 	{ "drv", SCTP6 },
1550Sstevel@tonic-gate 	{ "drv", ICMP },
1560Sstevel@tonic-gate 	{ "drv", ICMP6 },
1570Sstevel@tonic-gate 	{ "drv", ARP },
1580Sstevel@tonic-gate 	{ "strmod", TIMOD }
1590Sstevel@tonic-gate };
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate /*
1620Sstevel@tonic-gate  * Called from swapgeneric.c:loadrootmodules() in the network boot case.
1630Sstevel@tonic-gate  */
1640Sstevel@tonic-gate int
1650Sstevel@tonic-gate strplumb_load(void)
1660Sstevel@tonic-gate {
1670Sstevel@tonic-gate 	uint_t			i;
1680Sstevel@tonic-gate 	strplumb_modspec_t	*p;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	DBG0("loading modules\n");
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	for (i = 0, p = strplumb_modlist;
1730Sstevel@tonic-gate 	    i < sizeof (strplumb_modlist) / sizeof (strplumb_modlist[0]);
1740Sstevel@tonic-gate 	    i++, p++) {
1750Sstevel@tonic-gate 		if (modloadonly(p->sm_type, p->sm_name) < 0) {
1760Sstevel@tonic-gate 			printf("strplumb: failed to load %s/%s\n",
1770Sstevel@tonic-gate 			    p->sm_type, p->sm_name);
1780Sstevel@tonic-gate 			return (EFAULT);
1790Sstevel@tonic-gate 		}
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	return (0);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate static int
1860Sstevel@tonic-gate strplumb_init(void)
1870Sstevel@tonic-gate {
1880Sstevel@tonic-gate 	uint_t			i;
1890Sstevel@tonic-gate 	strplumb_modspec_t	*p;
1900Sstevel@tonic-gate 	int			err;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	DBG0("initializing modules\n");
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	for (i = 0, p = strplumb_modlist;
1950Sstevel@tonic-gate 	    i < sizeof (strplumb_modlist) / sizeof (strplumb_modlist[0]);
1960Sstevel@tonic-gate 	    i++, p++) {
1970Sstevel@tonic-gate 		if (strcmp(p->sm_type, "drv") == 0)
1980Sstevel@tonic-gate 			err = (i_ddi_attach_pseudo_node(p->sm_name) != NULL) ?
1990Sstevel@tonic-gate 			    0 : EFAULT;
2000Sstevel@tonic-gate 		else
2010Sstevel@tonic-gate 			err = (modload(p->sm_type, p->sm_name) < 0) ?
2020Sstevel@tonic-gate 			    EFAULT : 0;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 		if (err != 0)  {
2050Sstevel@tonic-gate 			printf("strplumb: failed to initialize %s/%s\n",
2060Sstevel@tonic-gate 			    p->sm_type, p->sm_name);
2070Sstevel@tonic-gate 			return (err);
2080Sstevel@tonic-gate 		}
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	return (0);
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate /*
2150Sstevel@tonic-gate  * Can be set in /etc/system in the case of local booting. See comment below.
2160Sstevel@tonic-gate  */
2170Sstevel@tonic-gate char	*ndev_name = 0;
2180Sstevel@tonic-gate int	ndev_unit = 0;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate /*
2210Sstevel@tonic-gate  * If we booted diskless then strplumb() will have been called from
2220Sstevel@tonic-gate  * swapgeneric.c:rootconf(). All we can do in that case is plumb the
2230Sstevel@tonic-gate  * network device that we booted from.
2240Sstevel@tonic-gate  *
2250Sstevel@tonic-gate  * If we booted from a local disk, we will have been called from main(),
2260Sstevel@tonic-gate  * and normally we defer the plumbing of interfaces until network/physical.
2270Sstevel@tonic-gate  * This can be overridden by setting "ndev_name" in /etc/system.
2280Sstevel@tonic-gate  */
2290Sstevel@tonic-gate static int
230269Sericheng resolve_boot_path(void)
2310Sstevel@tonic-gate {
2325710Ssetje 	char			*devpath;
2330Sstevel@tonic-gate 	dev_info_t		*dip;
2340Sstevel@tonic-gate 	const char		*driver;
2350Sstevel@tonic-gate 	int			instance;
2365648Ssetje #ifdef	_OBP
2375648Ssetje 	char			stripped_path[OBP_MAXPATHLEN];
2385648Ssetje #endif
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	if (strncmp(rootfs.bo_fstype, "nfs", 3) == 0)
2410Sstevel@tonic-gate 		devpath = rootfs.bo_name;
2420Sstevel@tonic-gate 	else
2430Sstevel@tonic-gate 		devpath = strplumb_get_netdev_path();
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	if (devpath != NULL) {
2460Sstevel@tonic-gate 		DBG1("resolving boot-path: %s\n", devpath);
2475710Ssetje #ifdef _OBP
2485710Ssetje 		/*
2495710Ssetje 		 * OBP passes options e.g, "net:dhcp"
2505710Ssetje 		 * remove them here
2515710Ssetje 		 */
2525710Ssetje 		prom_strip_options(devpath, stripped_path);
2535710Ssetje 		devpath = stripped_path;
2545710Ssetje #endif
2550Sstevel@tonic-gate 		/*
2560Sstevel@tonic-gate 		 * Hold the devi since this is the root device.
2570Sstevel@tonic-gate 		 */
2580Sstevel@tonic-gate 		if ((dip = e_ddi_hold_devi_by_path(devpath, 0)) == NULL) {
2590Sstevel@tonic-gate 			printf("strplumb: unable to hold root device: %s\n",
2600Sstevel@tonic-gate 			    devpath);
2610Sstevel@tonic-gate 			return (ENXIO);
2620Sstevel@tonic-gate 		}
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 		driver = ddi_driver_name(dip);
2650Sstevel@tonic-gate 		instance = ddi_get_instance(dip);
2660Sstevel@tonic-gate 	} else {
2670Sstevel@tonic-gate 		if (ndev_name == NULL)
2680Sstevel@tonic-gate 			return (ENODEV);
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 		DBG2("using ndev_name (%s) ndev_unit (%d)\n", ndev_name,
2710Sstevel@tonic-gate 		    ndev_unit);
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 		if (i_ddi_attach_hw_nodes(ndev_name) != DDI_SUCCESS) {
2740Sstevel@tonic-gate 			printf("strplumb: cannot load ndev_name '%s'\n",
2750Sstevel@tonic-gate 			    ndev_name);
2760Sstevel@tonic-gate 			return (ENXIO);
2770Sstevel@tonic-gate 		}
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 		driver = ndev_name;
2800Sstevel@tonic-gate 		instance = ndev_unit;
2810Sstevel@tonic-gate 	}
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	(void) snprintf(rootfs.bo_devname, BO_MAXOBJNAME,
284269Sericheng 	    "/devices/pseudo/clone@0:%s", driver);
2850Sstevel@tonic-gate 	(void) snprintf(rootfs.bo_ifname, BO_MAXOBJNAME, "%s%d",
2860Sstevel@tonic-gate 	    driver, instance);
2870Sstevel@tonic-gate 	rootfs.bo_ppa = instance;
288269Sericheng 	return (0);
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate static int
2920Sstevel@tonic-gate getifflags(ldi_handle_t lh, struct lifreq *lifrp)
2930Sstevel@tonic-gate {
2940Sstevel@tonic-gate 	struct strioctl	iocb;
2950Sstevel@tonic-gate 	int		rval;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	iocb.ic_cmd = SIOCGLIFFLAGS;
2980Sstevel@tonic-gate 	iocb.ic_timout = 15;
2990Sstevel@tonic-gate 	iocb.ic_len = sizeof (struct lifreq);
3000Sstevel@tonic-gate 	iocb.ic_dp = (char *)lifrp;
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate static int
3070Sstevel@tonic-gate setifname(ldi_handle_t lh, struct lifreq *lifrp)
3080Sstevel@tonic-gate {
3090Sstevel@tonic-gate 	struct strioctl	iocb;
3100Sstevel@tonic-gate 	int		rval;
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	iocb.ic_cmd = SIOCSLIFNAME;
3130Sstevel@tonic-gate 	iocb.ic_timout = 15;
3140Sstevel@tonic-gate 	iocb.ic_len = sizeof (struct lifreq);
3150Sstevel@tonic-gate 	iocb.ic_dp = (char *)lifrp;
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate static int
3210Sstevel@tonic-gate strplumb_dev(ldi_ident_t li)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate 	ldi_handle_t	lh = NULL;
3240Sstevel@tonic-gate 	ldi_handle_t	mux_lh = NULL;
3250Sstevel@tonic-gate 	int		err;
3260Sstevel@tonic-gate 	struct lifreq	lifr;
3270Sstevel@tonic-gate 	struct ifreq	ifr;
3280Sstevel@tonic-gate 	int		rval;
3298194SJack.Meng@Sun.COM 	int		af = 0;
3308194SJack.Meng@Sun.COM 	char		*name = NULL;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	bzero(&lifr, sizeof (struct lifreq));
3330Sstevel@tonic-gate 	bzero(&ifr, sizeof (ifr));
3340Sstevel@tonic-gate 
3358194SJack.Meng@Sun.COM 	if (iscsiboot_prop != NULL) {
3368194SJack.Meng@Sun.COM 		af = iscsiboot_prop->boot_nic.sin_family;
3378194SJack.Meng@Sun.COM 	}
3388194SJack.Meng@Sun.COM 
3390Sstevel@tonic-gate 	/*
3400Sstevel@tonic-gate 	 * Now set up the links. Ultimately, we should have two streams
341*11042SErik.Nordmark@Sun.COM 	 * permanently linked under UDP.  One stream consists of the
342*11042SErik.Nordmark@Sun.COM 	 * ARP-[ifname] combination, while the other consists of IP-[ifname].
3430Sstevel@tonic-gate 	 *
3440Sstevel@tonic-gate 	 * We pin underneath UDP here to match what is done in ifconfig(1m);
3450Sstevel@tonic-gate 	 * otherwise, ifconfig will be unable to unplumb the stream (the major
3460Sstevel@tonic-gate 	 * number and mux id must both match for a successful I_PUNLINK).
3470Sstevel@tonic-gate 	 *
3480Sstevel@tonic-gate 	 * There are subtleties in the plumbing which make it essential to
3490Sstevel@tonic-gate 	 * follow the logic used in ifconfig(1m) very closely.
3500Sstevel@tonic-gate 	 */
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	/*
353*11042SErik.Nordmark@Sun.COM 	 * Plumb UDP-IP-<dev>
3540Sstevel@tonic-gate 	 */
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
3570Sstevel@tonic-gate 	    &lh, li)) != 0) {
3580Sstevel@tonic-gate 		printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
3590Sstevel@tonic-gate 		    err);
3600Sstevel@tonic-gate 		goto done;
3610Sstevel@tonic-gate 	}
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)IP, FKIOCTL, CRED(),
3650Sstevel@tonic-gate 	    &rval)) != 0) {
3660Sstevel@tonic-gate 		printf("strplumb: push IP failed: %d\n", err);
3670Sstevel@tonic-gate 		goto done;
3680Sstevel@tonic-gate 	}
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	if ((err = getifflags(lh, &lifr)) != 0)
3710Sstevel@tonic-gate 		goto done;
3720Sstevel@tonic-gate 
3738194SJack.Meng@Sun.COM 	if (af == 0 || af == AF_INET) {
3748194SJack.Meng@Sun.COM 		lifr.lifr_flags |= IFF_IPV4;
3758194SJack.Meng@Sun.COM 		lifr.lifr_flags &= ~IFF_IPV6;
3768194SJack.Meng@Sun.COM 		name = UDPDEV;
3778194SJack.Meng@Sun.COM 	} else {
3788194SJack.Meng@Sun.COM 		/*
3798194SJack.Meng@Sun.COM 		 * iscsi boot is used with ipv6 enabled
3808194SJack.Meng@Sun.COM 		 */
3818194SJack.Meng@Sun.COM 		lifr.lifr_flags |= IFF_IPV6;
3828194SJack.Meng@Sun.COM 		lifr.lifr_flags &= ~IFF_IPV4;
3838194SJack.Meng@Sun.COM 		name = UDP6DEV;
3848194SJack.Meng@Sun.COM 	}
3850Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, rootfs.bo_ifname,
3860Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
3870Sstevel@tonic-gate 	lifr.lifr_ppa = rootfs.bo_ppa;
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	if ((err = setifname(lh, &lifr)) != 0)
3900Sstevel@tonic-gate 		goto done;
3910Sstevel@tonic-gate 
392*11042SErik.Nordmark@Sun.COM 	/* get the flags and check if ARP is needed */
3930Sstevel@tonic-gate 	if ((err = getifflags(lh, &lifr)) != 0) {
3940Sstevel@tonic-gate 		printf("strplumb: getifflags %s IP failed, error %d\n",
3950Sstevel@tonic-gate 		    lifr.lifr_name, err);
3960Sstevel@tonic-gate 		goto done;
3970Sstevel@tonic-gate 	}
3988194SJack.Meng@Sun.COM 	if ((err = ldi_open_by_name(name, FREAD|FWRITE, CRED(), &mux_lh,
3990Sstevel@tonic-gate 	    li)) != 0) {
4008194SJack.Meng@Sun.COM 		printf("strplumb: open of %s failed: %d\n", name, err);
4010Sstevel@tonic-gate 		goto done;
4020Sstevel@tonic-gate 	}
4030Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
4040Sstevel@tonic-gate 	    FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
4050Sstevel@tonic-gate 	    &(ifr.ifr_ip_muxid))) != 0) {
4060Sstevel@tonic-gate 		printf("strplumb: plink UDP-ARP-IP-%s failed: %d\n",
4070Sstevel@tonic-gate 		    rootfs.bo_ifname, err);
4080Sstevel@tonic-gate 		goto done;
4090Sstevel@tonic-gate 	}
4100Sstevel@tonic-gate 
411*11042SErik.Nordmark@Sun.COM 	/* if ARP is not needed, we are done */
412*11042SErik.Nordmark@Sun.COM 	if (lifr.lifr_flags & (IFF_NOARP | IFF_IPV6))
4138194SJack.Meng@Sun.COM 		goto done;
4148194SJack.Meng@Sun.COM 
4150Sstevel@tonic-gate 	DBG2("UDP-ARP-IP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_ip_muxid);
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
4180Sstevel@tonic-gate 	lh = NULL;
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	/*
4210Sstevel@tonic-gate 	 * Plumb UDP-ARP-<dev>
4220Sstevel@tonic-gate 	 */
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
4250Sstevel@tonic-gate 	    &lh, li)) != 0) {
4260Sstevel@tonic-gate 		printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
4270Sstevel@tonic-gate 		    err);
4280Sstevel@tonic-gate 		goto done;
4290Sstevel@tonic-gate 	}
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)ARP, FKIOCTL, CRED(),
4320Sstevel@tonic-gate 	    &rval)) != 0) {
4330Sstevel@tonic-gate 		printf("strplumb: push ARP failed: %d\n", err);
4340Sstevel@tonic-gate 		goto done;
4350Sstevel@tonic-gate 	}
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	if ((err = setifname(lh, &lifr)) != 0)
4380Sstevel@tonic-gate 		goto done;
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
4410Sstevel@tonic-gate 	    FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
4420Sstevel@tonic-gate 	    &(ifr.ifr_arp_muxid))) != 0) {
4430Sstevel@tonic-gate 		printf("strplumb: plink UDP-ARP-%s failed: %d\n",
4440Sstevel@tonic-gate 		    rootfs.bo_ifname, err);
4450Sstevel@tonic-gate 		goto done;
4460Sstevel@tonic-gate 	}
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	DBG2("UDP-ARP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_arp_muxid);
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	/*
4510Sstevel@tonic-gate 	 * Cache the mux ids.
4520Sstevel@tonic-gate 	 */
4530Sstevel@tonic-gate 	(void) strlcpy(ifr.ifr_name, rootfs.bo_ifname, sizeof (ifr.ifr_name));
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, SIOCSIFMUXID, (intptr_t)&ifr, FKIOCTL,
4560Sstevel@tonic-gate 	    CRED(), &rval)) != 0) {
4570Sstevel@tonic-gate 		printf("strplumb: SIOCSIFMUXID failed: %d\n", err);
4580Sstevel@tonic-gate 		goto done;
4590Sstevel@tonic-gate 	}
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate done:
4620Sstevel@tonic-gate 	if (lh != NULL)
4630Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	if (mux_lh != NULL)
4660Sstevel@tonic-gate 		(void) ldi_close(mux_lh, FREAD|FWRITE, CRED());
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	return (err);
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate /*
4720Sstevel@tonic-gate  * Do streams plumbing for internet protocols.
4730Sstevel@tonic-gate  */
4740Sstevel@tonic-gate int
4750Sstevel@tonic-gate strplumb(void)
4760Sstevel@tonic-gate {
4770Sstevel@tonic-gate 	ldi_ident_t	li;
4780Sstevel@tonic-gate 	int		err;
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	if ((err = strplumb_init()) != 0)
4810Sstevel@tonic-gate 		return (err);
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	if ((err = ldi_ident_from_mod(&modlinkage, &li)) != 0)
4840Sstevel@tonic-gate 		return (err);
4850Sstevel@tonic-gate 
486269Sericheng 	if ((err = resolve_boot_path()) != 0)
4870Sstevel@tonic-gate 		goto done;
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	DBG1("rootfs.bo_devname: %s\n", rootfs.bo_devname);
4900Sstevel@tonic-gate 	DBG1("rootfs.bo_ifname: %s\n", rootfs.bo_ifname);
4910Sstevel@tonic-gate 	DBG1("rootfs.bo_ppa: %d\n", rootfs.bo_ppa);
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	if ((err = strplumb_dev(li)) != 0)
4940Sstevel@tonic-gate 		goto done;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate done:
4970Sstevel@tonic-gate 	ldi_ident_release(li);
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	return (err);
5000Sstevel@tonic-gate }
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate /* multiboot:  diskless boot interface discovery */
5030Sstevel@tonic-gate 
5045648Ssetje #ifndef	_OBP
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate static uchar_t boot_macaddr[16];
5070Sstevel@tonic-gate static int boot_maclen;
5085895Syz147064 static uchar_t *getmacaddr(dev_info_t *dip, size_t *maclenp);
5090Sstevel@tonic-gate static int matchmac(dev_info_t *dip, void *arg);
5100Sstevel@tonic-gate 
5115648Ssetje #endif  /* !_OBP */
5124172Ssetje 
5130Sstevel@tonic-gate char *
5140Sstevel@tonic-gate strplumb_get_netdev_path(void)
5150Sstevel@tonic-gate {
5165710Ssetje #ifdef	_OBP
51710822SJack.Meng@Sun.COM 	char		fstype[OBP_MAXPROPNAME];
51810822SJack.Meng@Sun.COM 	static char	iscsi_network_path[BO_MAXOBJNAME]	= {0};
51910822SJack.Meng@Sun.COM 	int		proplen;
52010822SJack.Meng@Sun.COM 	char		*p	= NULL;
5215710Ssetje 
5225710Ssetje 	if (bop_getprop("fstype", fstype) == -1)
5235710Ssetje 		return (NULL);
5245710Ssetje 
5255710Ssetje 	if (strncmp(fstype, "nfs", 3) == 0)
5265710Ssetje 		return (prom_bootpath());
52710822SJack.Meng@Sun.COM 	else if (iscsiboot_prop != NULL) {
52810822SJack.Meng@Sun.COM 		proplen =  BOP_GETPROPLEN(bootops,
52910822SJack.Meng@Sun.COM 		    BP_ISCSI_NETWORK_BOOTPATH);
53010822SJack.Meng@Sun.COM 		if (proplen > 0) {
53110822SJack.Meng@Sun.COM 			if (BOP_GETPROP(bootops,
53210822SJack.Meng@Sun.COM 			    BP_ISCSI_NETWORK_BOOTPATH,
53310822SJack.Meng@Sun.COM 			    iscsi_network_path) > 0) {
53410822SJack.Meng@Sun.COM 				p = strchr(iscsi_network_path, ':');
53510822SJack.Meng@Sun.COM 				if (p != NULL) {
53610822SJack.Meng@Sun.COM 					*p = '\0';
53710822SJack.Meng@Sun.COM 				}
53810822SJack.Meng@Sun.COM 				return (iscsi_network_path);
53910822SJack.Meng@Sun.COM 			}
54010822SJack.Meng@Sun.COM 		}
54110822SJack.Meng@Sun.COM 	}
54210822SJack.Meng@Sun.COM 	return (NULL);
5435710Ssetje #else
5445710Ssetje 
5450Sstevel@tonic-gate 	char *macstr, *devpath = NULL;
5460Sstevel@tonic-gate 	uchar_t *bootp;
5475648Ssetje 	uint_t bootp_len;
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
5500Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, BP_BOOT_MAC, &macstr) == DDI_SUCCESS) {
5510Sstevel@tonic-gate 		/*
5520Sstevel@tonic-gate 		 * hard coded ether mac len for booting floppy on
5530Sstevel@tonic-gate 		 * machines with old cards
5540Sstevel@tonic-gate 		 */
5550Sstevel@tonic-gate 		boot_maclen = ether_aton(macstr, boot_macaddr);
5560Sstevel@tonic-gate 		if (boot_maclen != 6) {
5570Sstevel@tonic-gate 			cmn_err(CE_WARN,
5580Sstevel@tonic-gate 			    "malformed boot_mac property, %d bytes",
5590Sstevel@tonic-gate 			    boot_maclen);
5600Sstevel@tonic-gate 		}
5610Sstevel@tonic-gate 		ddi_prop_free(macstr);
5620Sstevel@tonic-gate 	} else if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, ddi_root_node(),
5630Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, BP_BOOTP_RESPONSE, &bootp, &bootp_len)
5640Sstevel@tonic-gate 	    == DDI_SUCCESS) {
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 		/*
5670Sstevel@tonic-gate 		 * These offsets are defined by dhcp standard
5680Sstevel@tonic-gate 		 * Should use structure offsets
5690Sstevel@tonic-gate 		 */
5700Sstevel@tonic-gate 		boot_maclen = *(bootp + 2);
5710Sstevel@tonic-gate 		ASSERT(boot_maclen <= 16);
5725648Ssetje 		bcopy(bootp + 28, boot_macaddr, boot_maclen);
5730Sstevel@tonic-gate 
5745648Ssetje 		dhcack = kmem_alloc(bootp_len, KM_SLEEP);
5755648Ssetje 		bcopy(bootp, dhcack, bootp_len);
5765648Ssetje 		dhcacklen = bootp_len;
5775648Ssetje 
5780Sstevel@tonic-gate 		ddi_prop_free(bootp);
5798194SJack.Meng@Sun.COM 	} else  if (iscsiboot_prop != NULL) {
5808194SJack.Meng@Sun.COM 		bcopy(iscsiboot_prop->boot_nic.nic_mac,
5818194SJack.Meng@Sun.COM 		    boot_macaddr, IB_BOOT_MACLEN);
5828194SJack.Meng@Sun.COM 		boot_maclen = IB_BOOT_MACLEN;
5838194SJack.Meng@Sun.COM 	} else {
5840Sstevel@tonic-gate 		return (NULL);
5858194SJack.Meng@Sun.COM 	}
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	ddi_walk_devs(ddi_root_node(), matchmac, (void *)&devpath);
5880Sstevel@tonic-gate 	return (devpath);
5894172Ssetje 
5905710Ssetje #endif  /* _OBP */
5910Sstevel@tonic-gate }
5920Sstevel@tonic-gate 
5935648Ssetje #ifndef _OBP
5944172Ssetje 
5950Sstevel@tonic-gate /*
5960Sstevel@tonic-gate  * Get boot path from the boot_mac address
5970Sstevel@tonic-gate  */
5980Sstevel@tonic-gate /*ARGSUSED*/
5990Sstevel@tonic-gate static int
6000Sstevel@tonic-gate matchmac(dev_info_t *dip, void *arg)
6010Sstevel@tonic-gate {
6020Sstevel@tonic-gate 	char **devpathp = (char **)arg;
6030Sstevel@tonic-gate 	char *model_str;
6040Sstevel@tonic-gate 	uchar_t *macaddr;
6055895Syz147064 	size_t maclen;
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	/* XXX Should use "device-type" per IEEE 1275 */
6080Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0,
6090Sstevel@tonic-gate 	    "model", &model_str) != DDI_SUCCESS)
6100Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	if (strcmp(model_str, "Ethernet controller") != 0) {
6130Sstevel@tonic-gate 		ddi_prop_free(model_str);
6140Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
6150Sstevel@tonic-gate 	}
6160Sstevel@tonic-gate 	ddi_prop_free(model_str);
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	/* We have a network device now */
6190Sstevel@tonic-gate 	if (i_ddi_attach_node_hierarchy(dip) != DDI_SUCCESS) {
6200Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
6210Sstevel@tonic-gate 	}
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	ASSERT(boot_maclen != 0);
6240Sstevel@tonic-gate 	macaddr = getmacaddr(dip, &maclen);
6250Sstevel@tonic-gate 	if (macaddr == NULL)
6260Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	if (maclen != boot_maclen ||
6290Sstevel@tonic-gate 	    bcmp(macaddr, boot_macaddr, maclen) != 0) {
6300Sstevel@tonic-gate 		kmem_free(macaddr, maclen);
6310Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
6320Sstevel@tonic-gate 	}
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	/* found hardware with the mac address */
6350Sstevel@tonic-gate 	(void) localetheraddr((struct ether_addr *)macaddr, NULL);
6360Sstevel@tonic-gate 	kmem_free(macaddr, maclen);
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	*devpathp = kmem_alloc(MAXPATHLEN, KM_SLEEP);
6390Sstevel@tonic-gate 	(void) ddi_pathname(dip, *devpathp);
6400Sstevel@tonic-gate 
6415648Ssetje 	/* fill in dhcifname */
6425648Ssetje 	if (dhcack) {
6435648Ssetje 		(void) snprintf(dhcifname, IFNAMSIZ, "%s%d",
6440Sstevel@tonic-gate 		    ddi_driver_name(dip), i_ddi_devi_get_ppa(dip));
6455648Ssetje 	}
6460Sstevel@tonic-gate 	return (DDI_WALK_TERMINATE);
6470Sstevel@tonic-gate }
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate static uchar_t *
6505895Syz147064 getmacaddr(dev_info_t *dip, size_t *maclenp)
6510Sstevel@tonic-gate {
6520Sstevel@tonic-gate 	int rc, ppa;
6530Sstevel@tonic-gate 	ldi_ident_t li;
6540Sstevel@tonic-gate 	ldi_handle_t lh;
6555895Syz147064 	const char *drv_name = ddi_driver_name(dip);
6560Sstevel@tonic-gate 	char *clonepath;
6570Sstevel@tonic-gate 	uchar_t *macaddr = NULL;
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate 	if (rc = ldi_ident_from_mod(&modlinkage, &li)) {
6600Sstevel@tonic-gate 		cmn_err(CE_WARN,
6610Sstevel@tonic-gate 		    "getmacaddr: ldi_ident_from_mod failed: %d\n", rc);
6620Sstevel@tonic-gate 		return (NULL);
6630Sstevel@tonic-gate 	}
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	clonepath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
6660Sstevel@tonic-gate 	(void) snprintf(clonepath, MAXPATHLEN,
6670Sstevel@tonic-gate 	    "/devices/pseudo/clone@0:%s", drv_name);
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	rc = ldi_open_by_name(clonepath, FREAD|FWRITE, CRED(), &lh, li);
6700Sstevel@tonic-gate 	ldi_ident_release(li);
6710Sstevel@tonic-gate 	if (rc) {
6720Sstevel@tonic-gate 		cmn_err(CE_WARN,
6730Sstevel@tonic-gate 		    "getmacaddr: ldi_open_by_name(%s) failed: %d\n",
6740Sstevel@tonic-gate 		    clonepath, rc);
6750Sstevel@tonic-gate 		kmem_free(clonepath, MAXPATHLEN);
6760Sstevel@tonic-gate 		return (NULL);
6770Sstevel@tonic-gate 	}
6780Sstevel@tonic-gate 	kmem_free(clonepath, MAXPATHLEN);
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	ppa = i_ddi_devi_get_ppa(dip);
6815895Syz147064 	if ((dl_attach(lh, ppa, NULL) != 0) ||
6825895Syz147064 	    (dl_bind(lh, ETHERTYPE_IP, NULL) != 0)) {
6830Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
6840Sstevel@tonic-gate 		cmn_err(CE_WARN,
6850Sstevel@tonic-gate 		    "getmacaddr: dl_attach/bind(%s%d) failed: %d\n",
6860Sstevel@tonic-gate 		    drv_name, ppa, rc);
6870Sstevel@tonic-gate 		return (NULL);
6880Sstevel@tonic-gate 	}
6895895Syz147064 
6905895Syz147064 	*maclenp = ETHERADDRL;
6915895Syz147064 	macaddr = kmem_alloc(ETHERADDRL, KM_SLEEP);
6925895Syz147064 	if (dl_phys_addr(lh, macaddr, maclenp, NULL) != 0 ||
6935895Syz147064 	    *maclenp != ETHERADDRL) {
6945895Syz147064 		kmem_free(macaddr, ETHERADDRL);
6950Sstevel@tonic-gate 		macaddr = NULL;
6960Sstevel@tonic-gate 		*maclenp = 0;
6970Sstevel@tonic-gate 		cmn_err(CE_WARN,
6985895Syz147064 		    "getmacaddr: dl_phys_addr(%s%d) failed: %d\n",
6990Sstevel@tonic-gate 		    drv_name, ppa, rc);
7000Sstevel@tonic-gate 	}
7010Sstevel@tonic-gate 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
7020Sstevel@tonic-gate 	return (macaddr);
7030Sstevel@tonic-gate }
7045648Ssetje #endif	/* !_OBP */
705