xref: /onnv-gate/usr/src/uts/common/io/strplumb.c (revision 12016:0248e987199b)
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 /*
22*12016SGirish.Moodalbail@Sun.COM  * Copyright 2010 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
_init(void)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
_fini(void)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
_info(struct modinfo * modinfop)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	ICMP		"icmp"
1270Sstevel@tonic-gate #define	ICMP6		"icmp6"
1280Sstevel@tonic-gate #define	IP		"ip"
1290Sstevel@tonic-gate #define	IP6		"ip6"
1300Sstevel@tonic-gate #define	TIMOD		"timod"
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate #define	UDPDEV		"/devices/pseudo/udp@0:udp"
1330Sstevel@tonic-gate #define	TCP6DEV		"/devices/pseudo/tcp6@0:tcp6"
1348194SJack.Meng@Sun.COM #define	UDP6DEV		"/devices/pseudo/udp6@0:udp6"
1350Sstevel@tonic-gate #define	IP6DEV		"/devices/pseudo/ip6@0:ip6"
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate typedef struct strplumb_modspec {
1380Sstevel@tonic-gate 	char	*sm_type;
1390Sstevel@tonic-gate 	char	*sm_name;
1400Sstevel@tonic-gate } strplumb_modspec_t;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate static strplumb_modspec_t	strplumb_modlist[] = {
1430Sstevel@tonic-gate 	{ "drv", DLD_DRIVER_NAME },
1440Sstevel@tonic-gate 	{ "drv", IP },
1450Sstevel@tonic-gate 	{ "drv", IP6 },
1460Sstevel@tonic-gate 	{ "drv", TCP },
1470Sstevel@tonic-gate 	{ "drv", TCP6 },
1480Sstevel@tonic-gate 	{ "drv", UDP },
1490Sstevel@tonic-gate 	{ "drv", UDP6 },
1500Sstevel@tonic-gate 	{ "drv", ICMP },
1510Sstevel@tonic-gate 	{ "drv", ICMP6 },
1520Sstevel@tonic-gate 	{ "drv", ARP },
1530Sstevel@tonic-gate 	{ "strmod", TIMOD }
1540Sstevel@tonic-gate };
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate /*
1570Sstevel@tonic-gate  * Called from swapgeneric.c:loadrootmodules() in the network boot case.
1580Sstevel@tonic-gate  */
1590Sstevel@tonic-gate int
strplumb_load(void)1600Sstevel@tonic-gate strplumb_load(void)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate 	uint_t			i;
1630Sstevel@tonic-gate 	strplumb_modspec_t	*p;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	DBG0("loading modules\n");
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	for (i = 0, p = strplumb_modlist;
1680Sstevel@tonic-gate 	    i < sizeof (strplumb_modlist) / sizeof (strplumb_modlist[0]);
1690Sstevel@tonic-gate 	    i++, p++) {
1700Sstevel@tonic-gate 		if (modloadonly(p->sm_type, p->sm_name) < 0) {
1710Sstevel@tonic-gate 			printf("strplumb: failed to load %s/%s\n",
1720Sstevel@tonic-gate 			    p->sm_type, p->sm_name);
1730Sstevel@tonic-gate 			return (EFAULT);
1740Sstevel@tonic-gate 		}
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	return (0);
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate static int
strplumb_init(void)1810Sstevel@tonic-gate strplumb_init(void)
1820Sstevel@tonic-gate {
1830Sstevel@tonic-gate 	uint_t			i;
1840Sstevel@tonic-gate 	strplumb_modspec_t	*p;
1850Sstevel@tonic-gate 	int			err;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	DBG0("initializing modules\n");
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	for (i = 0, p = strplumb_modlist;
1900Sstevel@tonic-gate 	    i < sizeof (strplumb_modlist) / sizeof (strplumb_modlist[0]);
1910Sstevel@tonic-gate 	    i++, p++) {
1920Sstevel@tonic-gate 		if (strcmp(p->sm_type, "drv") == 0)
1930Sstevel@tonic-gate 			err = (i_ddi_attach_pseudo_node(p->sm_name) != NULL) ?
1940Sstevel@tonic-gate 			    0 : EFAULT;
1950Sstevel@tonic-gate 		else
1960Sstevel@tonic-gate 			err = (modload(p->sm_type, p->sm_name) < 0) ?
1970Sstevel@tonic-gate 			    EFAULT : 0;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 		if (err != 0)  {
2000Sstevel@tonic-gate 			printf("strplumb: failed to initialize %s/%s\n",
2010Sstevel@tonic-gate 			    p->sm_type, p->sm_name);
2020Sstevel@tonic-gate 			return (err);
2030Sstevel@tonic-gate 		}
2040Sstevel@tonic-gate 	}
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	return (0);
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate /*
2100Sstevel@tonic-gate  * Can be set in /etc/system in the case of local booting. See comment below.
2110Sstevel@tonic-gate  */
2120Sstevel@tonic-gate char	*ndev_name = 0;
2130Sstevel@tonic-gate int	ndev_unit = 0;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate /*
2160Sstevel@tonic-gate  * If we booted diskless then strplumb() will have been called from
2170Sstevel@tonic-gate  * swapgeneric.c:rootconf(). All we can do in that case is plumb the
2180Sstevel@tonic-gate  * network device that we booted from.
2190Sstevel@tonic-gate  *
2200Sstevel@tonic-gate  * If we booted from a local disk, we will have been called from main(),
2210Sstevel@tonic-gate  * and normally we defer the plumbing of interfaces until network/physical.
2220Sstevel@tonic-gate  * This can be overridden by setting "ndev_name" in /etc/system.
2230Sstevel@tonic-gate  */
2240Sstevel@tonic-gate static int
resolve_boot_path(void)225269Sericheng resolve_boot_path(void)
2260Sstevel@tonic-gate {
2275710Ssetje 	char			*devpath;
2280Sstevel@tonic-gate 	dev_info_t		*dip;
2290Sstevel@tonic-gate 	const char		*driver;
2300Sstevel@tonic-gate 	int			instance;
2315648Ssetje #ifdef	_OBP
2325648Ssetje 	char			stripped_path[OBP_MAXPATHLEN];
2335648Ssetje #endif
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	if (strncmp(rootfs.bo_fstype, "nfs", 3) == 0)
2360Sstevel@tonic-gate 		devpath = rootfs.bo_name;
2370Sstevel@tonic-gate 	else
2380Sstevel@tonic-gate 		devpath = strplumb_get_netdev_path();
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	if (devpath != NULL) {
2410Sstevel@tonic-gate 		DBG1("resolving boot-path: %s\n", devpath);
2425710Ssetje #ifdef _OBP
2435710Ssetje 		/*
2445710Ssetje 		 * OBP passes options e.g, "net:dhcp"
2455710Ssetje 		 * remove them here
2465710Ssetje 		 */
2475710Ssetje 		prom_strip_options(devpath, stripped_path);
2485710Ssetje 		devpath = stripped_path;
2495710Ssetje #endif
2500Sstevel@tonic-gate 		/*
2510Sstevel@tonic-gate 		 * Hold the devi since this is the root device.
2520Sstevel@tonic-gate 		 */
2530Sstevel@tonic-gate 		if ((dip = e_ddi_hold_devi_by_path(devpath, 0)) == NULL) {
2540Sstevel@tonic-gate 			printf("strplumb: unable to hold root device: %s\n",
2550Sstevel@tonic-gate 			    devpath);
2560Sstevel@tonic-gate 			return (ENXIO);
2570Sstevel@tonic-gate 		}
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 		driver = ddi_driver_name(dip);
2600Sstevel@tonic-gate 		instance = ddi_get_instance(dip);
2610Sstevel@tonic-gate 	} else {
2620Sstevel@tonic-gate 		if (ndev_name == NULL)
2630Sstevel@tonic-gate 			return (ENODEV);
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 		DBG2("using ndev_name (%s) ndev_unit (%d)\n", ndev_name,
2660Sstevel@tonic-gate 		    ndev_unit);
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 		if (i_ddi_attach_hw_nodes(ndev_name) != DDI_SUCCESS) {
2690Sstevel@tonic-gate 			printf("strplumb: cannot load ndev_name '%s'\n",
2700Sstevel@tonic-gate 			    ndev_name);
2710Sstevel@tonic-gate 			return (ENXIO);
2720Sstevel@tonic-gate 		}
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 		driver = ndev_name;
2750Sstevel@tonic-gate 		instance = ndev_unit;
2760Sstevel@tonic-gate 	}
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	(void) snprintf(rootfs.bo_devname, BO_MAXOBJNAME,
279269Sericheng 	    "/devices/pseudo/clone@0:%s", driver);
2800Sstevel@tonic-gate 	(void) snprintf(rootfs.bo_ifname, BO_MAXOBJNAME, "%s%d",
2810Sstevel@tonic-gate 	    driver, instance);
2820Sstevel@tonic-gate 	rootfs.bo_ppa = instance;
283269Sericheng 	return (0);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate static int
getifflags(ldi_handle_t lh,struct lifreq * lifrp)2870Sstevel@tonic-gate getifflags(ldi_handle_t lh, struct lifreq *lifrp)
2880Sstevel@tonic-gate {
2890Sstevel@tonic-gate 	struct strioctl	iocb;
2900Sstevel@tonic-gate 	int		rval;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	iocb.ic_cmd = SIOCGLIFFLAGS;
2930Sstevel@tonic-gate 	iocb.ic_timout = 15;
2940Sstevel@tonic-gate 	iocb.ic_len = sizeof (struct lifreq);
2950Sstevel@tonic-gate 	iocb.ic_dp = (char *)lifrp;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate static int
setifname(ldi_handle_t lh,struct lifreq * lifrp)3020Sstevel@tonic-gate setifname(ldi_handle_t lh, struct lifreq *lifrp)
3030Sstevel@tonic-gate {
3040Sstevel@tonic-gate 	struct strioctl	iocb;
3050Sstevel@tonic-gate 	int		rval;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	iocb.ic_cmd = SIOCSLIFNAME;
3080Sstevel@tonic-gate 	iocb.ic_timout = 15;
3090Sstevel@tonic-gate 	iocb.ic_len = sizeof (struct lifreq);
3100Sstevel@tonic-gate 	iocb.ic_dp = (char *)lifrp;
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate static int
strplumb_dev(ldi_ident_t li)3160Sstevel@tonic-gate strplumb_dev(ldi_ident_t li)
3170Sstevel@tonic-gate {
3180Sstevel@tonic-gate 	ldi_handle_t	lh = NULL;
3190Sstevel@tonic-gate 	ldi_handle_t	mux_lh = NULL;
3200Sstevel@tonic-gate 	int		err;
3210Sstevel@tonic-gate 	struct lifreq	lifr;
3220Sstevel@tonic-gate 	struct ifreq	ifr;
3230Sstevel@tonic-gate 	int		rval;
3248194SJack.Meng@Sun.COM 	int		af = 0;
3258194SJack.Meng@Sun.COM 	char		*name = NULL;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	bzero(&lifr, sizeof (struct lifreq));
3280Sstevel@tonic-gate 	bzero(&ifr, sizeof (ifr));
3290Sstevel@tonic-gate 
3308194SJack.Meng@Sun.COM 	if (iscsiboot_prop != NULL) {
3318194SJack.Meng@Sun.COM 		af = iscsiboot_prop->boot_nic.sin_family;
3328194SJack.Meng@Sun.COM 	}
3338194SJack.Meng@Sun.COM 
3340Sstevel@tonic-gate 	/*
3350Sstevel@tonic-gate 	 * Now set up the links. Ultimately, we should have two streams
33611042SErik.Nordmark@Sun.COM 	 * permanently linked under UDP.  One stream consists of the
33711042SErik.Nordmark@Sun.COM 	 * ARP-[ifname] combination, while the other consists of IP-[ifname].
3380Sstevel@tonic-gate 	 *
3390Sstevel@tonic-gate 	 * We pin underneath UDP here to match what is done in ifconfig(1m);
3400Sstevel@tonic-gate 	 * otherwise, ifconfig will be unable to unplumb the stream (the major
3410Sstevel@tonic-gate 	 * number and mux id must both match for a successful I_PUNLINK).
3420Sstevel@tonic-gate 	 *
3430Sstevel@tonic-gate 	 * There are subtleties in the plumbing which make it essential to
3440Sstevel@tonic-gate 	 * follow the logic used in ifconfig(1m) very closely.
3450Sstevel@tonic-gate 	 */
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	/*
34811042SErik.Nordmark@Sun.COM 	 * Plumb UDP-IP-<dev>
3490Sstevel@tonic-gate 	 */
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
3520Sstevel@tonic-gate 	    &lh, li)) != 0) {
3530Sstevel@tonic-gate 		printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
3540Sstevel@tonic-gate 		    err);
3550Sstevel@tonic-gate 		goto done;
3560Sstevel@tonic-gate 	}
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)IP, FKIOCTL, CRED(),
3600Sstevel@tonic-gate 	    &rval)) != 0) {
3610Sstevel@tonic-gate 		printf("strplumb: push IP failed: %d\n", err);
3620Sstevel@tonic-gate 		goto done;
3630Sstevel@tonic-gate 	}
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	if ((err = getifflags(lh, &lifr)) != 0)
3660Sstevel@tonic-gate 		goto done;
3670Sstevel@tonic-gate 
3688194SJack.Meng@Sun.COM 	if (af == 0 || af == AF_INET) {
3698194SJack.Meng@Sun.COM 		lifr.lifr_flags |= IFF_IPV4;
3708194SJack.Meng@Sun.COM 		lifr.lifr_flags &= ~IFF_IPV6;
3718194SJack.Meng@Sun.COM 		name = UDPDEV;
3728194SJack.Meng@Sun.COM 	} else {
3738194SJack.Meng@Sun.COM 		/*
3748194SJack.Meng@Sun.COM 		 * iscsi boot is used with ipv6 enabled
3758194SJack.Meng@Sun.COM 		 */
3768194SJack.Meng@Sun.COM 		lifr.lifr_flags |= IFF_IPV6;
3778194SJack.Meng@Sun.COM 		lifr.lifr_flags &= ~IFF_IPV4;
3788194SJack.Meng@Sun.COM 		name = UDP6DEV;
3798194SJack.Meng@Sun.COM 	}
3800Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, rootfs.bo_ifname,
3810Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
3820Sstevel@tonic-gate 	lifr.lifr_ppa = rootfs.bo_ppa;
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	if ((err = setifname(lh, &lifr)) != 0)
3850Sstevel@tonic-gate 		goto done;
3860Sstevel@tonic-gate 
38711042SErik.Nordmark@Sun.COM 	/* get the flags and check if ARP is needed */
3880Sstevel@tonic-gate 	if ((err = getifflags(lh, &lifr)) != 0) {
3890Sstevel@tonic-gate 		printf("strplumb: getifflags %s IP failed, error %d\n",
3900Sstevel@tonic-gate 		    lifr.lifr_name, err);
3910Sstevel@tonic-gate 		goto done;
3920Sstevel@tonic-gate 	}
3938194SJack.Meng@Sun.COM 	if ((err = ldi_open_by_name(name, FREAD|FWRITE, CRED(), &mux_lh,
3940Sstevel@tonic-gate 	    li)) != 0) {
3958194SJack.Meng@Sun.COM 		printf("strplumb: open of %s failed: %d\n", name, err);
3960Sstevel@tonic-gate 		goto done;
3970Sstevel@tonic-gate 	}
3980Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
3990Sstevel@tonic-gate 	    FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
4000Sstevel@tonic-gate 	    &(ifr.ifr_ip_muxid))) != 0) {
4010Sstevel@tonic-gate 		printf("strplumb: plink UDP-ARP-IP-%s failed: %d\n",
4020Sstevel@tonic-gate 		    rootfs.bo_ifname, err);
4030Sstevel@tonic-gate 		goto done;
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate 
40611042SErik.Nordmark@Sun.COM 	/* if ARP is not needed, we are done */
40711042SErik.Nordmark@Sun.COM 	if (lifr.lifr_flags & (IFF_NOARP | IFF_IPV6))
4088194SJack.Meng@Sun.COM 		goto done;
4098194SJack.Meng@Sun.COM 
4100Sstevel@tonic-gate 	DBG2("UDP-ARP-IP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_ip_muxid);
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
4130Sstevel@tonic-gate 	lh = NULL;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	/*
4160Sstevel@tonic-gate 	 * Plumb UDP-ARP-<dev>
4170Sstevel@tonic-gate 	 */
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
4200Sstevel@tonic-gate 	    &lh, li)) != 0) {
4210Sstevel@tonic-gate 		printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
4220Sstevel@tonic-gate 		    err);
4230Sstevel@tonic-gate 		goto done;
4240Sstevel@tonic-gate 	}
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)ARP, FKIOCTL, CRED(),
4270Sstevel@tonic-gate 	    &rval)) != 0) {
4280Sstevel@tonic-gate 		printf("strplumb: push ARP failed: %d\n", err);
4290Sstevel@tonic-gate 		goto done;
4300Sstevel@tonic-gate 	}
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	if ((err = setifname(lh, &lifr)) != 0)
4330Sstevel@tonic-gate 		goto done;
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
4360Sstevel@tonic-gate 	    FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
4370Sstevel@tonic-gate 	    &(ifr.ifr_arp_muxid))) != 0) {
4380Sstevel@tonic-gate 		printf("strplumb: plink UDP-ARP-%s failed: %d\n",
4390Sstevel@tonic-gate 		    rootfs.bo_ifname, err);
4400Sstevel@tonic-gate 		goto done;
4410Sstevel@tonic-gate 	}
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	DBG2("UDP-ARP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_arp_muxid);
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	/*
4460Sstevel@tonic-gate 	 * Cache the mux ids.
4470Sstevel@tonic-gate 	 */
4480Sstevel@tonic-gate 	(void) strlcpy(ifr.ifr_name, rootfs.bo_ifname, sizeof (ifr.ifr_name));
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, SIOCSIFMUXID, (intptr_t)&ifr, FKIOCTL,
4510Sstevel@tonic-gate 	    CRED(), &rval)) != 0) {
4520Sstevel@tonic-gate 		printf("strplumb: SIOCSIFMUXID failed: %d\n", err);
4530Sstevel@tonic-gate 		goto done;
4540Sstevel@tonic-gate 	}
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate done:
4570Sstevel@tonic-gate 	if (lh != NULL)
4580Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	if (mux_lh != NULL)
4610Sstevel@tonic-gate 		(void) ldi_close(mux_lh, FREAD|FWRITE, CRED());
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	return (err);
4640Sstevel@tonic-gate }
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate /*
4670Sstevel@tonic-gate  * Do streams plumbing for internet protocols.
4680Sstevel@tonic-gate  */
4690Sstevel@tonic-gate int
strplumb(void)4700Sstevel@tonic-gate strplumb(void)
4710Sstevel@tonic-gate {
4720Sstevel@tonic-gate 	ldi_ident_t	li;
4730Sstevel@tonic-gate 	int		err;
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	if ((err = strplumb_init()) != 0)
4760Sstevel@tonic-gate 		return (err);
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	if ((err = ldi_ident_from_mod(&modlinkage, &li)) != 0)
4790Sstevel@tonic-gate 		return (err);
4800Sstevel@tonic-gate 
481269Sericheng 	if ((err = resolve_boot_path()) != 0)
4820Sstevel@tonic-gate 		goto done;
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	DBG1("rootfs.bo_devname: %s\n", rootfs.bo_devname);
4850Sstevel@tonic-gate 	DBG1("rootfs.bo_ifname: %s\n", rootfs.bo_ifname);
4860Sstevel@tonic-gate 	DBG1("rootfs.bo_ppa: %d\n", rootfs.bo_ppa);
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	if ((err = strplumb_dev(li)) != 0)
4890Sstevel@tonic-gate 		goto done;
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate done:
4920Sstevel@tonic-gate 	ldi_ident_release(li);
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	return (err);
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate /* multiboot:  diskless boot interface discovery */
4980Sstevel@tonic-gate 
4995648Ssetje #ifndef	_OBP
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate static uchar_t boot_macaddr[16];
5020Sstevel@tonic-gate static int boot_maclen;
5035895Syz147064 static uchar_t *getmacaddr(dev_info_t *dip, size_t *maclenp);
5040Sstevel@tonic-gate static int matchmac(dev_info_t *dip, void *arg);
5050Sstevel@tonic-gate 
5065648Ssetje #endif  /* !_OBP */
5074172Ssetje 
5080Sstevel@tonic-gate char *
strplumb_get_netdev_path(void)5090Sstevel@tonic-gate strplumb_get_netdev_path(void)
5100Sstevel@tonic-gate {
5115710Ssetje #ifdef	_OBP
51210822SJack.Meng@Sun.COM 	char		fstype[OBP_MAXPROPNAME];
51310822SJack.Meng@Sun.COM 	static char	iscsi_network_path[BO_MAXOBJNAME]	= {0};
51410822SJack.Meng@Sun.COM 	int		proplen;
51510822SJack.Meng@Sun.COM 	char		*p	= NULL;
5165710Ssetje 
5175710Ssetje 	if (bop_getprop("fstype", fstype) == -1)
5185710Ssetje 		return (NULL);
5195710Ssetje 
5205710Ssetje 	if (strncmp(fstype, "nfs", 3) == 0)
5215710Ssetje 		return (prom_bootpath());
52210822SJack.Meng@Sun.COM 	else if (iscsiboot_prop != NULL) {
52310822SJack.Meng@Sun.COM 		proplen =  BOP_GETPROPLEN(bootops,
52410822SJack.Meng@Sun.COM 		    BP_ISCSI_NETWORK_BOOTPATH);
52510822SJack.Meng@Sun.COM 		if (proplen > 0) {
52610822SJack.Meng@Sun.COM 			if (BOP_GETPROP(bootops,
52710822SJack.Meng@Sun.COM 			    BP_ISCSI_NETWORK_BOOTPATH,
52810822SJack.Meng@Sun.COM 			    iscsi_network_path) > 0) {
52910822SJack.Meng@Sun.COM 				p = strchr(iscsi_network_path, ':');
53010822SJack.Meng@Sun.COM 				if (p != NULL) {
53110822SJack.Meng@Sun.COM 					*p = '\0';
53210822SJack.Meng@Sun.COM 				}
53310822SJack.Meng@Sun.COM 				return (iscsi_network_path);
53410822SJack.Meng@Sun.COM 			}
53510822SJack.Meng@Sun.COM 		}
53610822SJack.Meng@Sun.COM 	}
53710822SJack.Meng@Sun.COM 	return (NULL);
5385710Ssetje #else
5395710Ssetje 
5400Sstevel@tonic-gate 	char *macstr, *devpath = NULL;
5410Sstevel@tonic-gate 	uchar_t *bootp;
5425648Ssetje 	uint_t bootp_len;
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
5450Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, BP_BOOT_MAC, &macstr) == DDI_SUCCESS) {
5460Sstevel@tonic-gate 		/*
5470Sstevel@tonic-gate 		 * hard coded ether mac len for booting floppy on
5480Sstevel@tonic-gate 		 * machines with old cards
5490Sstevel@tonic-gate 		 */
5500Sstevel@tonic-gate 		boot_maclen = ether_aton(macstr, boot_macaddr);
5510Sstevel@tonic-gate 		if (boot_maclen != 6) {
5520Sstevel@tonic-gate 			cmn_err(CE_WARN,
5530Sstevel@tonic-gate 			    "malformed boot_mac property, %d bytes",
5540Sstevel@tonic-gate 			    boot_maclen);
5550Sstevel@tonic-gate 		}
5560Sstevel@tonic-gate 		ddi_prop_free(macstr);
5570Sstevel@tonic-gate 	} else if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, ddi_root_node(),
5580Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, BP_BOOTP_RESPONSE, &bootp, &bootp_len)
5590Sstevel@tonic-gate 	    == DDI_SUCCESS) {
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 		/*
5620Sstevel@tonic-gate 		 * These offsets are defined by dhcp standard
5630Sstevel@tonic-gate 		 * Should use structure offsets
5640Sstevel@tonic-gate 		 */
5650Sstevel@tonic-gate 		boot_maclen = *(bootp + 2);
5660Sstevel@tonic-gate 		ASSERT(boot_maclen <= 16);
5675648Ssetje 		bcopy(bootp + 28, boot_macaddr, boot_maclen);
5680Sstevel@tonic-gate 
5695648Ssetje 		dhcack = kmem_alloc(bootp_len, KM_SLEEP);
5705648Ssetje 		bcopy(bootp, dhcack, bootp_len);
5715648Ssetje 		dhcacklen = bootp_len;
5725648Ssetje 
5730Sstevel@tonic-gate 		ddi_prop_free(bootp);
5748194SJack.Meng@Sun.COM 	} else  if (iscsiboot_prop != NULL) {
5758194SJack.Meng@Sun.COM 		bcopy(iscsiboot_prop->boot_nic.nic_mac,
5768194SJack.Meng@Sun.COM 		    boot_macaddr, IB_BOOT_MACLEN);
5778194SJack.Meng@Sun.COM 		boot_maclen = IB_BOOT_MACLEN;
5788194SJack.Meng@Sun.COM 	} else {
5790Sstevel@tonic-gate 		return (NULL);
5808194SJack.Meng@Sun.COM 	}
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	ddi_walk_devs(ddi_root_node(), matchmac, (void *)&devpath);
5830Sstevel@tonic-gate 	return (devpath);
5844172Ssetje 
5855710Ssetje #endif  /* _OBP */
5860Sstevel@tonic-gate }
5870Sstevel@tonic-gate 
5885648Ssetje #ifndef _OBP
5894172Ssetje 
5900Sstevel@tonic-gate /*
5910Sstevel@tonic-gate  * Get boot path from the boot_mac address
5920Sstevel@tonic-gate  */
5930Sstevel@tonic-gate /*ARGSUSED*/
5940Sstevel@tonic-gate static int
matchmac(dev_info_t * dip,void * arg)5950Sstevel@tonic-gate matchmac(dev_info_t *dip, void *arg)
5960Sstevel@tonic-gate {
5970Sstevel@tonic-gate 	char **devpathp = (char **)arg;
5980Sstevel@tonic-gate 	char *model_str;
5990Sstevel@tonic-gate 	uchar_t *macaddr;
6005895Syz147064 	size_t maclen;
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	/* XXX Should use "device-type" per IEEE 1275 */
6030Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0,
6040Sstevel@tonic-gate 	    "model", &model_str) != DDI_SUCCESS)
6050Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	if (strcmp(model_str, "Ethernet controller") != 0) {
6080Sstevel@tonic-gate 		ddi_prop_free(model_str);
6090Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
6100Sstevel@tonic-gate 	}
6110Sstevel@tonic-gate 	ddi_prop_free(model_str);
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	/* We have a network device now */
6140Sstevel@tonic-gate 	if (i_ddi_attach_node_hierarchy(dip) != DDI_SUCCESS) {
6150Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
6160Sstevel@tonic-gate 	}
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	ASSERT(boot_maclen != 0);
6190Sstevel@tonic-gate 	macaddr = getmacaddr(dip, &maclen);
6200Sstevel@tonic-gate 	if (macaddr == NULL)
6210Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	if (maclen != boot_maclen ||
6240Sstevel@tonic-gate 	    bcmp(macaddr, boot_macaddr, maclen) != 0) {
6250Sstevel@tonic-gate 		kmem_free(macaddr, maclen);
6260Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
6270Sstevel@tonic-gate 	}
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	/* found hardware with the mac address */
6300Sstevel@tonic-gate 	(void) localetheraddr((struct ether_addr *)macaddr, NULL);
6310Sstevel@tonic-gate 	kmem_free(macaddr, maclen);
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	*devpathp = kmem_alloc(MAXPATHLEN, KM_SLEEP);
6340Sstevel@tonic-gate 	(void) ddi_pathname(dip, *devpathp);
6350Sstevel@tonic-gate 
6365648Ssetje 	/* fill in dhcifname */
6375648Ssetje 	if (dhcack) {
6385648Ssetje 		(void) snprintf(dhcifname, IFNAMSIZ, "%s%d",
6390Sstevel@tonic-gate 		    ddi_driver_name(dip), i_ddi_devi_get_ppa(dip));
6405648Ssetje 	}
6410Sstevel@tonic-gate 	return (DDI_WALK_TERMINATE);
6420Sstevel@tonic-gate }
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate static uchar_t *
getmacaddr(dev_info_t * dip,size_t * maclenp)6455895Syz147064 getmacaddr(dev_info_t *dip, size_t *maclenp)
6460Sstevel@tonic-gate {
6470Sstevel@tonic-gate 	int rc, ppa;
6480Sstevel@tonic-gate 	ldi_ident_t li;
6490Sstevel@tonic-gate 	ldi_handle_t lh;
6505895Syz147064 	const char *drv_name = ddi_driver_name(dip);
6510Sstevel@tonic-gate 	char *clonepath;
6520Sstevel@tonic-gate 	uchar_t *macaddr = NULL;
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	if (rc = ldi_ident_from_mod(&modlinkage, &li)) {
6550Sstevel@tonic-gate 		cmn_err(CE_WARN,
6560Sstevel@tonic-gate 		    "getmacaddr: ldi_ident_from_mod failed: %d\n", rc);
6570Sstevel@tonic-gate 		return (NULL);
6580Sstevel@tonic-gate 	}
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	clonepath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
6610Sstevel@tonic-gate 	(void) snprintf(clonepath, MAXPATHLEN,
6620Sstevel@tonic-gate 	    "/devices/pseudo/clone@0:%s", drv_name);
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	rc = ldi_open_by_name(clonepath, FREAD|FWRITE, CRED(), &lh, li);
6650Sstevel@tonic-gate 	ldi_ident_release(li);
6660Sstevel@tonic-gate 	if (rc) {
6670Sstevel@tonic-gate 		cmn_err(CE_WARN,
6680Sstevel@tonic-gate 		    "getmacaddr: ldi_open_by_name(%s) failed: %d\n",
6690Sstevel@tonic-gate 		    clonepath, rc);
6700Sstevel@tonic-gate 		kmem_free(clonepath, MAXPATHLEN);
6710Sstevel@tonic-gate 		return (NULL);
6720Sstevel@tonic-gate 	}
6730Sstevel@tonic-gate 	kmem_free(clonepath, MAXPATHLEN);
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	ppa = i_ddi_devi_get_ppa(dip);
6765895Syz147064 	if ((dl_attach(lh, ppa, NULL) != 0) ||
6775895Syz147064 	    (dl_bind(lh, ETHERTYPE_IP, NULL) != 0)) {
6780Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
6790Sstevel@tonic-gate 		cmn_err(CE_WARN,
6800Sstevel@tonic-gate 		    "getmacaddr: dl_attach/bind(%s%d) failed: %d\n",
6810Sstevel@tonic-gate 		    drv_name, ppa, rc);
6820Sstevel@tonic-gate 		return (NULL);
6830Sstevel@tonic-gate 	}
6845895Syz147064 
6855895Syz147064 	*maclenp = ETHERADDRL;
6865895Syz147064 	macaddr = kmem_alloc(ETHERADDRL, KM_SLEEP);
6875895Syz147064 	if (dl_phys_addr(lh, macaddr, maclenp, NULL) != 0 ||
6885895Syz147064 	    *maclenp != ETHERADDRL) {
6895895Syz147064 		kmem_free(macaddr, ETHERADDRL);
6900Sstevel@tonic-gate 		macaddr = NULL;
6910Sstevel@tonic-gate 		*maclenp = 0;
6920Sstevel@tonic-gate 		cmn_err(CE_WARN,
6935895Syz147064 		    "getmacaddr: dl_phys_addr(%s%d) failed: %d\n",
6940Sstevel@tonic-gate 		    drv_name, ppa, rc);
6950Sstevel@tonic-gate 	}
6960Sstevel@tonic-gate 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
6970Sstevel@tonic-gate 	return (macaddr);
6980Sstevel@tonic-gate }
6995648Ssetje #endif	/* !_OBP */
700