xref: /onnv-gate/usr/src/uts/common/io/strplumb.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include	<sys/param.h>
30*0Sstevel@tonic-gate #include	<sys/types.h>
31*0Sstevel@tonic-gate #include	<sys/user.h>
32*0Sstevel@tonic-gate #include	<sys/vfs.h>
33*0Sstevel@tonic-gate #include	<sys/vnode.h>
34*0Sstevel@tonic-gate #include	<sys/file.h>
35*0Sstevel@tonic-gate #include	<sys/stream.h>
36*0Sstevel@tonic-gate #include	<sys/stropts.h>
37*0Sstevel@tonic-gate #include	<sys/strsubr.h>
38*0Sstevel@tonic-gate #include	<sys/dlpi.h>
39*0Sstevel@tonic-gate #include	<sys/vnode.h>
40*0Sstevel@tonic-gate #include	<sys/socket.h>
41*0Sstevel@tonic-gate #include	<sys/sockio.h>
42*0Sstevel@tonic-gate #include	<net/if.h>
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #include	<sys/cred.h>
45*0Sstevel@tonic-gate #include	<sys/sysmacros.h>
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate #include	<sys/sad.h>
48*0Sstevel@tonic-gate #include	<sys/kstr.h>
49*0Sstevel@tonic-gate #include	<sys/bootconf.h>
50*0Sstevel@tonic-gate #include	<sys/bootprops.h>
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate #include	<sys/errno.h>
53*0Sstevel@tonic-gate #include	<sys/modctl.h>
54*0Sstevel@tonic-gate #include	<sys/sunddi.h>
55*0Sstevel@tonic-gate #include	<sys/sunldi.h>
56*0Sstevel@tonic-gate #include	<sys/esunddi.h>
57*0Sstevel@tonic-gate #include	<sys/promif.h>
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #include	<netinet/in.h>
60*0Sstevel@tonic-gate #include	<netinet/ip6.h>
61*0Sstevel@tonic-gate #include	<netinet/icmp6.h>
62*0Sstevel@tonic-gate #include	<netinet/sctp.h>
63*0Sstevel@tonic-gate #include	<inet/common.h>
64*0Sstevel@tonic-gate #include	<inet/ip.h>
65*0Sstevel@tonic-gate #include	<inet/ip6.h>
66*0Sstevel@tonic-gate #include	<inet/tcp.h>
67*0Sstevel@tonic-gate #include	<inet/sctp_ip.h>
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate #include	<sys/strlog.h>
70*0Sstevel@tonic-gate #include	<sys/log.h>
71*0Sstevel@tonic-gate #include	<sys/ethernet.h>
72*0Sstevel@tonic-gate #include	<sys/ddi_implfuncs.h>
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate #include	<sys/dld.h>
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate /*
77*0Sstevel@tonic-gate  * Debug Macros
78*0Sstevel@tonic-gate  */
79*0Sstevel@tonic-gate int	strplumbdebug = 0;
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate #define	DBG0(_f) \
82*0Sstevel@tonic-gate 	if (strplumbdebug != 0) \
83*0Sstevel@tonic-gate 		printf("strplumb: " _f)
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate #define	DBG1(_f, _a) \
86*0Sstevel@tonic-gate 	if (strplumbdebug != 0) \
87*0Sstevel@tonic-gate 		printf("strplumb: " _f, (_a))
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate #define	DBG2(_f, _a, _b) \
90*0Sstevel@tonic-gate 	if (strplumbdebug != 0) \
91*0Sstevel@tonic-gate 		printf("strplumb: " _f, (_a), (_b))
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate #define	DBG3(_f, _a, _b, _c) \
94*0Sstevel@tonic-gate 	if (strplumbdebug != 0) \
95*0Sstevel@tonic-gate 		printf("strplumb: " _f, (_a), (_b), (_c))
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate /*
98*0Sstevel@tonic-gate  * Module linkage information for the kernel.
99*0Sstevel@tonic-gate  */
100*0Sstevel@tonic-gate #define	STRPLUMB_IDENT	"STREAMS Plumbing Module v%I%"
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate static struct modlmisc modlmisc = {
103*0Sstevel@tonic-gate 	&mod_miscops,
104*0Sstevel@tonic-gate 	STRPLUMB_IDENT
105*0Sstevel@tonic-gate };
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate static struct modlinkage modlinkage = {
108*0Sstevel@tonic-gate 	MODREV_1,
109*0Sstevel@tonic-gate 	&modlmisc,
110*0Sstevel@tonic-gate 	NULL
111*0Sstevel@tonic-gate };
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate int
114*0Sstevel@tonic-gate _init(void)
115*0Sstevel@tonic-gate {
116*0Sstevel@tonic-gate 	return (mod_install(&modlinkage));
117*0Sstevel@tonic-gate }
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate int
120*0Sstevel@tonic-gate _fini(void)
121*0Sstevel@tonic-gate {
122*0Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
123*0Sstevel@tonic-gate }
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate int
126*0Sstevel@tonic-gate _info(struct modinfo *modinfop)
127*0Sstevel@tonic-gate {
128*0Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
129*0Sstevel@tonic-gate }
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate #define	ARP		"arp"
132*0Sstevel@tonic-gate #define	TCP		"tcp"
133*0Sstevel@tonic-gate #define	TCP6		"tcp6"
134*0Sstevel@tonic-gate #define	UDP		"udp"
135*0Sstevel@tonic-gate #define	UDP6		"udp6"
136*0Sstevel@tonic-gate #define	SCTP		"sctp"
137*0Sstevel@tonic-gate #define	SCTP6		"sctp6"
138*0Sstevel@tonic-gate #define	ICMP		"icmp"
139*0Sstevel@tonic-gate #define	ICMP6		"icmp6"
140*0Sstevel@tonic-gate #define	IP		"ip"
141*0Sstevel@tonic-gate #define	IP6		"ip6"
142*0Sstevel@tonic-gate #define	TIMOD		"timod"
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate #define	UDPDEV		"/devices/pseudo/udp@0:udp"
145*0Sstevel@tonic-gate #define	TCP6DEV		"/devices/pseudo/tcp6@0:tcp6"
146*0Sstevel@tonic-gate #define	SCTP6DEV	"/devices/pseudo/sctp6@0:sctp6"
147*0Sstevel@tonic-gate #define	IP6DEV		"/devices/pseudo/ip6@0:ip6"
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate typedef struct strplumb_modspec {
150*0Sstevel@tonic-gate 	char	*sm_type;
151*0Sstevel@tonic-gate 	char	*sm_name;
152*0Sstevel@tonic-gate } strplumb_modspec_t;
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate static strplumb_modspec_t	strplumb_modlist[] = {
155*0Sstevel@tonic-gate 	{ "drv", DLD_DRIVER_NAME },
156*0Sstevel@tonic-gate 	{ "drv", IP },
157*0Sstevel@tonic-gate 	{ "drv", IP6 },
158*0Sstevel@tonic-gate 	{ "drv", TCP },
159*0Sstevel@tonic-gate 	{ "drv", TCP6 },
160*0Sstevel@tonic-gate 	{ "drv", UDP },
161*0Sstevel@tonic-gate 	{ "drv", UDP6 },
162*0Sstevel@tonic-gate 	{ "drv", SCTP },
163*0Sstevel@tonic-gate 	{ "drv", SCTP6 },
164*0Sstevel@tonic-gate 	{ "drv", ICMP },
165*0Sstevel@tonic-gate 	{ "drv", ICMP6 },
166*0Sstevel@tonic-gate 	{ "drv", ARP },
167*0Sstevel@tonic-gate 	{ "strmod", TIMOD }
168*0Sstevel@tonic-gate };
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate /*
171*0Sstevel@tonic-gate  * Called from swapgeneric.c:loadrootmodules() in the network boot case.
172*0Sstevel@tonic-gate  */
173*0Sstevel@tonic-gate int
174*0Sstevel@tonic-gate strplumb_load(void)
175*0Sstevel@tonic-gate {
176*0Sstevel@tonic-gate 	uint_t			i;
177*0Sstevel@tonic-gate 	strplumb_modspec_t	*p;
178*0Sstevel@tonic-gate 
179*0Sstevel@tonic-gate 	DBG0("loading modules\n");
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	for (i = 0, p = strplumb_modlist;
182*0Sstevel@tonic-gate 	    i < sizeof (strplumb_modlist) / sizeof (strplumb_modlist[0]);
183*0Sstevel@tonic-gate 	    i++, p++) {
184*0Sstevel@tonic-gate 		if (modloadonly(p->sm_type, p->sm_name) < 0) {
185*0Sstevel@tonic-gate 			printf("strplumb: failed to load %s/%s\n",
186*0Sstevel@tonic-gate 			    p->sm_type, p->sm_name);
187*0Sstevel@tonic-gate 			return (EFAULT);
188*0Sstevel@tonic-gate 		}
189*0Sstevel@tonic-gate 	}
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 	return (0);
192*0Sstevel@tonic-gate }
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate static int
195*0Sstevel@tonic-gate strplumb_init(void)
196*0Sstevel@tonic-gate {
197*0Sstevel@tonic-gate 	uint_t			i;
198*0Sstevel@tonic-gate 	strplumb_modspec_t	*p;
199*0Sstevel@tonic-gate 	int			err;
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	DBG0("initializing modules\n");
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 	for (i = 0, p = strplumb_modlist;
204*0Sstevel@tonic-gate 	    i < sizeof (strplumb_modlist) / sizeof (strplumb_modlist[0]);
205*0Sstevel@tonic-gate 	    i++, p++) {
206*0Sstevel@tonic-gate 		if (strcmp(p->sm_type, "drv") == 0)
207*0Sstevel@tonic-gate 			err = (i_ddi_attach_pseudo_node(p->sm_name) != NULL) ?
208*0Sstevel@tonic-gate 			    0 : EFAULT;
209*0Sstevel@tonic-gate 		else
210*0Sstevel@tonic-gate 			err = (modload(p->sm_type, p->sm_name) < 0) ?
211*0Sstevel@tonic-gate 			    EFAULT : 0;
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 		if (err != 0)  {
214*0Sstevel@tonic-gate 			printf("strplumb: failed to initialize %s/%s\n",
215*0Sstevel@tonic-gate 			    p->sm_type, p->sm_name);
216*0Sstevel@tonic-gate 			return (err);
217*0Sstevel@tonic-gate 		}
218*0Sstevel@tonic-gate 	}
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 	return (0);
221*0Sstevel@tonic-gate }
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate static int
224*0Sstevel@tonic-gate strplumb_autopush(void)
225*0Sstevel@tonic-gate {
226*0Sstevel@tonic-gate 	major_t		maj;
227*0Sstevel@tonic-gate 	minor_t		min;
228*0Sstevel@tonic-gate 	char		*mods[5];
229*0Sstevel@tonic-gate 	uint_t		anchor = 1;
230*0Sstevel@tonic-gate 	int		err;
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	min = (minor_t)-1;
233*0Sstevel@tonic-gate 	mods[1] = NULL;
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 	/*
236*0Sstevel@tonic-gate 	 * UDP
237*0Sstevel@tonic-gate 	 */
238*0Sstevel@tonic-gate 	DBG0("setting up udp autopush\n");
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 	mods[0] = UDP;
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate 	maj = ddi_name_to_major(UDP);
243*0Sstevel@tonic-gate 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, &anchor,
244*0Sstevel@tonic-gate 	    mods)) != 0) {
245*0Sstevel@tonic-gate 		printf("strplumb: kstr_autopush(SET/UDP) failed: %d\n", err);
246*0Sstevel@tonic-gate 		return (err);
247*0Sstevel@tonic-gate 	}
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate 	maj = ddi_name_to_major(UDP6);
250*0Sstevel@tonic-gate 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, &anchor,
251*0Sstevel@tonic-gate 	    mods)) != 0) {
252*0Sstevel@tonic-gate 		printf("strplumb: kstr_autopush(SET/UDP6) failed: %d\n", err);
253*0Sstevel@tonic-gate 		return (err);
254*0Sstevel@tonic-gate 	}
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 	/*
257*0Sstevel@tonic-gate 	 * ICMP
258*0Sstevel@tonic-gate 	 */
259*0Sstevel@tonic-gate 	DBG0("setting up icmp autopush\n");
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 	mods[0] = ICMP;
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate 	maj = ddi_name_to_major(ICMP);
264*0Sstevel@tonic-gate 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, NULL,
265*0Sstevel@tonic-gate 	    mods)) != 0) {
266*0Sstevel@tonic-gate 		printf("strplumb: kstr_autopush(SET/ICMP) failed: %d\n", err);
267*0Sstevel@tonic-gate 		return (err);
268*0Sstevel@tonic-gate 	}
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 	maj = ddi_name_to_major(ICMP6);
271*0Sstevel@tonic-gate 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, NULL,
272*0Sstevel@tonic-gate 	    mods)) != 0) {
273*0Sstevel@tonic-gate 		printf("strplumb: kstr_autopush(SET/ICMP6) failed: %d\n", err);
274*0Sstevel@tonic-gate 		return (err);
275*0Sstevel@tonic-gate 	}
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 	/*
278*0Sstevel@tonic-gate 	 * ARP
279*0Sstevel@tonic-gate 	 */
280*0Sstevel@tonic-gate 	DBG0("setting up arp autopush\n");
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	mods[0] = ARP;
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 	maj = ddi_name_to_major(ARP);
285*0Sstevel@tonic-gate 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, &anchor,
286*0Sstevel@tonic-gate 	    mods)) != 0) {
287*0Sstevel@tonic-gate 		printf("strplumb: kstr_autopush(SET/ARP) failed: %d\n", err);
288*0Sstevel@tonic-gate 		return (err);
289*0Sstevel@tonic-gate 	}
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate 	return (0);
292*0Sstevel@tonic-gate }
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate static int
295*0Sstevel@tonic-gate strplumb_sctpq(ldi_ident_t li)
296*0Sstevel@tonic-gate {
297*0Sstevel@tonic-gate 	ldi_handle_t	lh = NULL;
298*0Sstevel@tonic-gate 	int		err;
299*0Sstevel@tonic-gate 	int		rval;
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate 	DBG0("configuring SCTP default queue\n");
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate 	if ((err = ldi_open_by_name(SCTP6DEV, FREAD|FWRITE, CRED(), &lh,
304*0Sstevel@tonic-gate 	    li)) != 0) {
305*0Sstevel@tonic-gate 		printf("strplumb: open of SCTP6DEV failed: %d\n", err);
306*0Sstevel@tonic-gate 		return (err);
307*0Sstevel@tonic-gate 	}
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, SCTP_IOC_DEFAULT_Q, (intptr_t)0, FKIOCTL,
310*0Sstevel@tonic-gate 	    CRED(), &rval)) != 0) {
311*0Sstevel@tonic-gate 		printf("strplumb: failed to set SCTP default queue: %d\n",
312*0Sstevel@tonic-gate 		    err);
313*0Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
314*0Sstevel@tonic-gate 		return (err);
315*0Sstevel@tonic-gate 	}
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate 	return (0);
318*0Sstevel@tonic-gate }
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate static int
321*0Sstevel@tonic-gate strplumb_tcpq(ldi_ident_t li)
322*0Sstevel@tonic-gate {
323*0Sstevel@tonic-gate 	ldi_handle_t	lh = NULL;
324*0Sstevel@tonic-gate 	ldi_handle_t	ip_lh = NULL;
325*0Sstevel@tonic-gate 	int		err;
326*0Sstevel@tonic-gate 	int		rval;
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 	DBG0("configuring TCP default queue\n");
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 	/*
331*0Sstevel@tonic-gate 	 * We open IP6DEV here because we need to have it open to in
332*0Sstevel@tonic-gate 	 * order to open TCP6DEV successfully.
333*0Sstevel@tonic-gate 	 */
334*0Sstevel@tonic-gate 	if ((err = ldi_open_by_name(IP6DEV, FREAD|FWRITE, CRED(), &ip_lh,
335*0Sstevel@tonic-gate 	    li)) != 0) {
336*0Sstevel@tonic-gate 		printf("strplumb: open of IP6DEV failed: %d\n", err);
337*0Sstevel@tonic-gate 		return (err);
338*0Sstevel@tonic-gate 	}
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 	/*
341*0Sstevel@tonic-gate 	 * We set the tcp default queue to IPv6 because IPv4 falls back to
342*0Sstevel@tonic-gate 	 * IPv6 when it can't find a client, but IPv6 does not fall back to
343*0Sstevel@tonic-gate 	 * IPv4.
344*0Sstevel@tonic-gate 	 */
345*0Sstevel@tonic-gate 	if ((err = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, CRED(), &lh,
346*0Sstevel@tonic-gate 	    li)) != 0) {
347*0Sstevel@tonic-gate 		printf("strplumb: open of TCP6DEV failed: %d\n", err);
348*0Sstevel@tonic-gate 		goto done;
349*0Sstevel@tonic-gate 	}
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, (intptr_t)0, FKIOCTL,
352*0Sstevel@tonic-gate 	    CRED(), &rval)) != 0) {
353*0Sstevel@tonic-gate 		printf("strplumb: failed to set TCP default queue: %d\n",
354*0Sstevel@tonic-gate 		    err);
355*0Sstevel@tonic-gate 		goto done;
356*0Sstevel@tonic-gate 	}
357*0Sstevel@tonic-gate 
358*0Sstevel@tonic-gate done:
359*0Sstevel@tonic-gate 	(void) ldi_close(ip_lh, FREAD|FWRITE, CRED());
360*0Sstevel@tonic-gate 	return (err);
361*0Sstevel@tonic-gate }
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate static int
364*0Sstevel@tonic-gate create_gldv3_linkobj(ldi_ident_t li, char *driver, int instance)
365*0Sstevel@tonic-gate {
366*0Sstevel@tonic-gate 	ldi_handle_t		lh;
367*0Sstevel@tonic-gate 	int			err;
368*0Sstevel@tonic-gate 	struct strioctl		iocb;
369*0Sstevel@tonic-gate 	int			rval;
370*0Sstevel@tonic-gate 	dld_ioc_create_t	dic;
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 	/*
373*0Sstevel@tonic-gate 	 * gld v3 driver. open the DLD control node.
374*0Sstevel@tonic-gate 	 */
375*0Sstevel@tonic-gate 	if ((err = ldi_open_by_name(DLD_CONTROL_DEV, FREAD|FWRITE, CRED(), &lh,
376*0Sstevel@tonic-gate 	    li)) != 0) {
377*0Sstevel@tonic-gate 		printf("strplumb: open CTLDEV failed: %d\n", err);
378*0Sstevel@tonic-gate 		return (ENXIO);
379*0Sstevel@tonic-gate 	}
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate 	(void) snprintf(dic.dic_name, sizeof (dic.dic_name) - 1, "%s%d",
382*0Sstevel@tonic-gate 	    driver, instance);
383*0Sstevel@tonic-gate 	(void) snprintf(dic.dic_dev, sizeof (dic.dic_dev) - 1, "%s%d",
384*0Sstevel@tonic-gate 	    driver, instance);
385*0Sstevel@tonic-gate 	dic.dic_port = 0;
386*0Sstevel@tonic-gate 	dic.dic_vid = 0;
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate 	iocb.ic_cmd = DLDIOCCREATE;
389*0Sstevel@tonic-gate 	iocb.ic_timout = 15;
390*0Sstevel@tonic-gate 	iocb.ic_len = sizeof (dic);
391*0Sstevel@tonic-gate 	iocb.ic_dp = (char *)&dic;
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate 	/*
394*0Sstevel@tonic-gate 	 * Try to create a data-link interface with the same name as the
395*0Sstevel@tonic-gate 	 * device.
396*0Sstevel@tonic-gate 	 */
397*0Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(),
398*0Sstevel@tonic-gate 	    &rval)) != 0) {
399*0Sstevel@tonic-gate 		cmn_err(CE_WARN, "strplumb: DLDIOCCREATE failed: %d\n", err);
400*0Sstevel@tonic-gate 		return (ENXIO);
401*0Sstevel@tonic-gate 	}
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
404*0Sstevel@tonic-gate 	return (0);
405*0Sstevel@tonic-gate }
406*0Sstevel@tonic-gate 
407*0Sstevel@tonic-gate /*
408*0Sstevel@tonic-gate  * Can be set in /etc/system in the case of local booting. See comment below.
409*0Sstevel@tonic-gate  */
410*0Sstevel@tonic-gate char	*ndev_name = 0;
411*0Sstevel@tonic-gate int	ndev_unit = 0;
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate /*
414*0Sstevel@tonic-gate  * If we booted diskless then strplumb() will have been called from
415*0Sstevel@tonic-gate  * swapgeneric.c:rootconf(). All we can do in that case is plumb the
416*0Sstevel@tonic-gate  * network device that we booted from.
417*0Sstevel@tonic-gate  *
418*0Sstevel@tonic-gate  * If we booted from a local disk, we will have been called from main(),
419*0Sstevel@tonic-gate  * and normally we defer the plumbing of interfaces until network/physical.
420*0Sstevel@tonic-gate  * This can be overridden by setting "ndev_name" in /etc/system.
421*0Sstevel@tonic-gate  */
422*0Sstevel@tonic-gate static int
423*0Sstevel@tonic-gate resolve_boot_path(ldi_ident_t li)
424*0Sstevel@tonic-gate {
425*0Sstevel@tonic-gate 	char			*devpath = NULL;
426*0Sstevel@tonic-gate 	dev_info_t		*dip;
427*0Sstevel@tonic-gate 	const char		*driver;
428*0Sstevel@tonic-gate 	int			instance;
429*0Sstevel@tonic-gate 	int			err;
430*0Sstevel@tonic-gate 
431*0Sstevel@tonic-gate 	if (strncmp(rootfs.bo_fstype, "nfs", 3) == 0)
432*0Sstevel@tonic-gate 		devpath = rootfs.bo_name;
433*0Sstevel@tonic-gate #ifndef __sparc
434*0Sstevel@tonic-gate 	else
435*0Sstevel@tonic-gate 		devpath = strplumb_get_netdev_path();
436*0Sstevel@tonic-gate #endif
437*0Sstevel@tonic-gate 
438*0Sstevel@tonic-gate 	if (devpath != NULL) {
439*0Sstevel@tonic-gate 		DBG1("resolving boot-path: %s\n", devpath);
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate 		/*
442*0Sstevel@tonic-gate 		 * Hold the devi since this is the root device.
443*0Sstevel@tonic-gate 		 */
444*0Sstevel@tonic-gate 		if ((dip = e_ddi_hold_devi_by_path(devpath, 0)) == NULL) {
445*0Sstevel@tonic-gate 			printf("strplumb: unable to hold root device: %s\n",
446*0Sstevel@tonic-gate 			    devpath);
447*0Sstevel@tonic-gate 			return (ENXIO);
448*0Sstevel@tonic-gate 		}
449*0Sstevel@tonic-gate 
450*0Sstevel@tonic-gate 		driver = ddi_driver_name(dip);
451*0Sstevel@tonic-gate 		instance = ddi_get_instance(dip);
452*0Sstevel@tonic-gate 	} else {
453*0Sstevel@tonic-gate 		if (ndev_name == NULL)
454*0Sstevel@tonic-gate 			return (ENODEV);
455*0Sstevel@tonic-gate 
456*0Sstevel@tonic-gate 		DBG2("using ndev_name (%s) ndev_unit (%d)\n", ndev_name,
457*0Sstevel@tonic-gate 		    ndev_unit);
458*0Sstevel@tonic-gate 
459*0Sstevel@tonic-gate 		if (i_ddi_attach_hw_nodes(ndev_name) != DDI_SUCCESS) {
460*0Sstevel@tonic-gate 			printf("strplumb: cannot load ndev_name '%s'\n",
461*0Sstevel@tonic-gate 			    ndev_name);
462*0Sstevel@tonic-gate 			return (ENXIO);
463*0Sstevel@tonic-gate 		}
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate 		driver = ndev_name;
466*0Sstevel@tonic-gate 		instance = ndev_unit;
467*0Sstevel@tonic-gate 	}
468*0Sstevel@tonic-gate 
469*0Sstevel@tonic-gate 	/* legcy driver, use clone to open */
470*0Sstevel@tonic-gate 	if (!GLDV3_DRV(ddi_name_to_major((char *)driver))) {
471*0Sstevel@tonic-gate 		(void) snprintf(rootfs.bo_devname, BO_MAXOBJNAME,
472*0Sstevel@tonic-gate 		    "/devices/pseudo/clone@0:%s", driver);
473*0Sstevel@tonic-gate 		(void) snprintf(rootfs.bo_ifname, BO_MAXOBJNAME, "%s%d",
474*0Sstevel@tonic-gate 		    driver, instance);
475*0Sstevel@tonic-gate 		rootfs.bo_ppa = instance;
476*0Sstevel@tonic-gate 		return (0);
477*0Sstevel@tonic-gate 	}
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate 	/* GLDv3: create link object so open with succeed later */
480*0Sstevel@tonic-gate 	err = create_gldv3_linkobj(li, (char *)driver, instance);
481*0Sstevel@tonic-gate 	if (err != 0) {
482*0Sstevel@tonic-gate 		return (err);
483*0Sstevel@tonic-gate 	}
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate 	(void) snprintf(rootfs.bo_devname, BO_MAXOBJNAME,
486*0Sstevel@tonic-gate 	    "/devices/pseudo/dld@0:%s", driver);
487*0Sstevel@tonic-gate 	(void) snprintf(rootfs.bo_ifname, BO_MAXOBJNAME, "%s%d",
488*0Sstevel@tonic-gate 	    driver, instance);
489*0Sstevel@tonic-gate 	rootfs.bo_ppa = instance;
490*0Sstevel@tonic-gate 
491*0Sstevel@tonic-gate 	return (err);
492*0Sstevel@tonic-gate }
493*0Sstevel@tonic-gate 
494*0Sstevel@tonic-gate static int
495*0Sstevel@tonic-gate getifflags(ldi_handle_t lh, struct lifreq *lifrp)
496*0Sstevel@tonic-gate {
497*0Sstevel@tonic-gate 	struct strioctl	iocb;
498*0Sstevel@tonic-gate 	int		rval;
499*0Sstevel@tonic-gate 
500*0Sstevel@tonic-gate 	iocb.ic_cmd = SIOCGLIFFLAGS;
501*0Sstevel@tonic-gate 	iocb.ic_timout = 15;
502*0Sstevel@tonic-gate 	iocb.ic_len = sizeof (struct lifreq);
503*0Sstevel@tonic-gate 	iocb.ic_dp = (char *)lifrp;
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate }
508*0Sstevel@tonic-gate 
509*0Sstevel@tonic-gate static int
510*0Sstevel@tonic-gate setifname(ldi_handle_t lh, struct lifreq *lifrp)
511*0Sstevel@tonic-gate {
512*0Sstevel@tonic-gate 	struct strioctl	iocb;
513*0Sstevel@tonic-gate 	int		rval;
514*0Sstevel@tonic-gate 
515*0Sstevel@tonic-gate 	iocb.ic_cmd = SIOCSLIFNAME;
516*0Sstevel@tonic-gate 	iocb.ic_timout = 15;
517*0Sstevel@tonic-gate 	iocb.ic_len = sizeof (struct lifreq);
518*0Sstevel@tonic-gate 	iocb.ic_dp = (char *)lifrp;
519*0Sstevel@tonic-gate 
520*0Sstevel@tonic-gate 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
521*0Sstevel@tonic-gate }
522*0Sstevel@tonic-gate 
523*0Sstevel@tonic-gate static int
524*0Sstevel@tonic-gate strplumb_dev(ldi_ident_t li)
525*0Sstevel@tonic-gate {
526*0Sstevel@tonic-gate 	ldi_handle_t	lh = NULL;
527*0Sstevel@tonic-gate 	ldi_handle_t	mux_lh = NULL;
528*0Sstevel@tonic-gate 	int		err;
529*0Sstevel@tonic-gate 	struct lifreq	lifr;
530*0Sstevel@tonic-gate 	struct ifreq	ifr;
531*0Sstevel@tonic-gate 	int		rval;
532*0Sstevel@tonic-gate 
533*0Sstevel@tonic-gate 	bzero(&lifr, sizeof (struct lifreq));
534*0Sstevel@tonic-gate 	bzero(&ifr, sizeof (ifr));
535*0Sstevel@tonic-gate 
536*0Sstevel@tonic-gate 	/*
537*0Sstevel@tonic-gate 	 * Now set up the links. Ultimately, we should have two streams
538*0Sstevel@tonic-gate 	 * permanently linked underneath UDP (which is actually IP with UDP
539*0Sstevel@tonic-gate 	 * autopushed). One stream consists of the ARP-[ifname] combination,
540*0Sstevel@tonic-gate 	 * while the other consists of ARP-IP-[ifname]. The second combination
541*0Sstevel@tonic-gate 	 * seems a little weird, but is linked underneath UDP just to keep it
542*0Sstevel@tonic-gate 	 * around.
543*0Sstevel@tonic-gate 	 *
544*0Sstevel@tonic-gate 	 * We pin underneath UDP here to match what is done in ifconfig(1m);
545*0Sstevel@tonic-gate 	 * otherwise, ifconfig will be unable to unplumb the stream (the major
546*0Sstevel@tonic-gate 	 * number and mux id must both match for a successful I_PUNLINK).
547*0Sstevel@tonic-gate 	 *
548*0Sstevel@tonic-gate 	 * There are subtleties in the plumbing which make it essential to
549*0Sstevel@tonic-gate 	 * follow the logic used in ifconfig(1m) very closely.
550*0Sstevel@tonic-gate 	 */
551*0Sstevel@tonic-gate 
552*0Sstevel@tonic-gate 	/*
553*0Sstevel@tonic-gate 	 * Plumb UDP-ARP-IP-<dev>
554*0Sstevel@tonic-gate 	 */
555*0Sstevel@tonic-gate 
556*0Sstevel@tonic-gate 	if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
557*0Sstevel@tonic-gate 	    &lh, li)) != 0) {
558*0Sstevel@tonic-gate 		printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
559*0Sstevel@tonic-gate 		    err);
560*0Sstevel@tonic-gate 		goto done;
561*0Sstevel@tonic-gate 	}
562*0Sstevel@tonic-gate 
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)IP, FKIOCTL, CRED(),
565*0Sstevel@tonic-gate 	    &rval)) != 0) {
566*0Sstevel@tonic-gate 		printf("strplumb: push IP failed: %d\n", err);
567*0Sstevel@tonic-gate 		goto done;
568*0Sstevel@tonic-gate 	}
569*0Sstevel@tonic-gate 
570*0Sstevel@tonic-gate 	if ((err = getifflags(lh, &lifr)) != 0)
571*0Sstevel@tonic-gate 		goto done;
572*0Sstevel@tonic-gate 
573*0Sstevel@tonic-gate 	lifr.lifr_flags |= IFF_IPV4;
574*0Sstevel@tonic-gate 	lifr.lifr_flags &= ~IFF_IPV6;
575*0Sstevel@tonic-gate 
576*0Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)ARP, FKIOCTL, CRED(),
577*0Sstevel@tonic-gate 	    &rval)) != 0) {
578*0Sstevel@tonic-gate 		printf("strplumb: push ARP failed: %d\n", err);
579*0Sstevel@tonic-gate 		goto done;
580*0Sstevel@tonic-gate 	}
581*0Sstevel@tonic-gate 
582*0Sstevel@tonic-gate 	(void) strlcpy(lifr.lifr_name, rootfs.bo_ifname,
583*0Sstevel@tonic-gate 	    sizeof (lifr.lifr_name));
584*0Sstevel@tonic-gate 	lifr.lifr_ppa = rootfs.bo_ppa;
585*0Sstevel@tonic-gate 
586*0Sstevel@tonic-gate 	if ((err = setifname(lh, &lifr)) != 0)
587*0Sstevel@tonic-gate 		goto done;
588*0Sstevel@tonic-gate 
589*0Sstevel@tonic-gate 	/* Get the flags and check if ARP is needed */
590*0Sstevel@tonic-gate 	if ((err = getifflags(lh, &lifr)) != 0) {
591*0Sstevel@tonic-gate 		printf("strplumb: getifflags %s IP failed, error %d\n",
592*0Sstevel@tonic-gate 		    lifr.lifr_name, err);
593*0Sstevel@tonic-gate 		goto done;
594*0Sstevel@tonic-gate 	}
595*0Sstevel@tonic-gate 
596*0Sstevel@tonic-gate 	/* Pop out ARP if not needed */
597*0Sstevel@tonic-gate 	if (lifr.lifr_flags & IFF_NOARP) {
598*0Sstevel@tonic-gate 		err = ldi_ioctl(lh, I_POP, (intptr_t)0, FKIOCTL, CRED(),
599*0Sstevel@tonic-gate 		    &rval);
600*0Sstevel@tonic-gate 		if (err != 0) {
601*0Sstevel@tonic-gate 			printf("strplumb: pop ARP failed, error %d\n", err);
602*0Sstevel@tonic-gate 			goto done;
603*0Sstevel@tonic-gate 		}
604*0Sstevel@tonic-gate 	}
605*0Sstevel@tonic-gate 
606*0Sstevel@tonic-gate 	if ((err = ldi_open_by_name(UDPDEV, FREAD|FWRITE, CRED(), &mux_lh,
607*0Sstevel@tonic-gate 	    li)) != 0) {
608*0Sstevel@tonic-gate 		printf("strplumb: open of UDPDEV failed: %d\n", err);
609*0Sstevel@tonic-gate 		goto done;
610*0Sstevel@tonic-gate 	}
611*0Sstevel@tonic-gate 
612*0Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
613*0Sstevel@tonic-gate 	    FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
614*0Sstevel@tonic-gate 	    &(ifr.ifr_ip_muxid))) != 0) {
615*0Sstevel@tonic-gate 		printf("strplumb: plink UDP-ARP-IP-%s failed: %d\n",
616*0Sstevel@tonic-gate 		    rootfs.bo_ifname, err);
617*0Sstevel@tonic-gate 		goto done;
618*0Sstevel@tonic-gate 	}
619*0Sstevel@tonic-gate 
620*0Sstevel@tonic-gate 	DBG2("UDP-ARP-IP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_ip_muxid);
621*0Sstevel@tonic-gate 
622*0Sstevel@tonic-gate 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
623*0Sstevel@tonic-gate 	lh = NULL;
624*0Sstevel@tonic-gate 
625*0Sstevel@tonic-gate 	/*
626*0Sstevel@tonic-gate 	 * Plumb UDP-ARP-<dev>
627*0Sstevel@tonic-gate 	 */
628*0Sstevel@tonic-gate 
629*0Sstevel@tonic-gate 	if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
630*0Sstevel@tonic-gate 	    &lh, li)) != 0) {
631*0Sstevel@tonic-gate 		printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
632*0Sstevel@tonic-gate 		    err);
633*0Sstevel@tonic-gate 		goto done;
634*0Sstevel@tonic-gate 	}
635*0Sstevel@tonic-gate 
636*0Sstevel@tonic-gate 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)ARP, FKIOCTL, CRED(),
637*0Sstevel@tonic-gate 	    &rval)) != 0) {
638*0Sstevel@tonic-gate 		printf("strplumb: push ARP failed: %d\n", err);
639*0Sstevel@tonic-gate 		goto done;
640*0Sstevel@tonic-gate 	}
641*0Sstevel@tonic-gate 
642*0Sstevel@tonic-gate 	if ((err = setifname(lh, &lifr)) != 0)
643*0Sstevel@tonic-gate 		goto done;
644*0Sstevel@tonic-gate 
645*0Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
646*0Sstevel@tonic-gate 	    FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
647*0Sstevel@tonic-gate 	    &(ifr.ifr_arp_muxid))) != 0) {
648*0Sstevel@tonic-gate 		printf("strplumb: plink UDP-ARP-%s failed: %d\n",
649*0Sstevel@tonic-gate 		    rootfs.bo_ifname, err);
650*0Sstevel@tonic-gate 		goto done;
651*0Sstevel@tonic-gate 	}
652*0Sstevel@tonic-gate 
653*0Sstevel@tonic-gate 	DBG2("UDP-ARP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_arp_muxid);
654*0Sstevel@tonic-gate 
655*0Sstevel@tonic-gate 	/*
656*0Sstevel@tonic-gate 	 * Cache the mux ids.
657*0Sstevel@tonic-gate 	 */
658*0Sstevel@tonic-gate 	(void) strlcpy(ifr.ifr_name, rootfs.bo_ifname, sizeof (ifr.ifr_name));
659*0Sstevel@tonic-gate 
660*0Sstevel@tonic-gate 	if ((err = ldi_ioctl(mux_lh, SIOCSIFMUXID, (intptr_t)&ifr, FKIOCTL,
661*0Sstevel@tonic-gate 	    CRED(), &rval)) != 0) {
662*0Sstevel@tonic-gate 		printf("strplumb: SIOCSIFMUXID failed: %d\n", err);
663*0Sstevel@tonic-gate 		goto done;
664*0Sstevel@tonic-gate 	}
665*0Sstevel@tonic-gate 
666*0Sstevel@tonic-gate done:
667*0Sstevel@tonic-gate 	if (lh != NULL)
668*0Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
669*0Sstevel@tonic-gate 
670*0Sstevel@tonic-gate 	if (mux_lh != NULL)
671*0Sstevel@tonic-gate 		(void) ldi_close(mux_lh, FREAD|FWRITE, CRED());
672*0Sstevel@tonic-gate 
673*0Sstevel@tonic-gate 	return (err);
674*0Sstevel@tonic-gate }
675*0Sstevel@tonic-gate 
676*0Sstevel@tonic-gate /*
677*0Sstevel@tonic-gate  * Do streams plumbing for internet protocols.
678*0Sstevel@tonic-gate  */
679*0Sstevel@tonic-gate int
680*0Sstevel@tonic-gate strplumb(void)
681*0Sstevel@tonic-gate {
682*0Sstevel@tonic-gate 	ldi_ident_t	li;
683*0Sstevel@tonic-gate 	int		err;
684*0Sstevel@tonic-gate 
685*0Sstevel@tonic-gate 	if ((err = strplumb_init()) != 0)
686*0Sstevel@tonic-gate 		return (err);
687*0Sstevel@tonic-gate 
688*0Sstevel@tonic-gate 	if ((err = strplumb_autopush()) != 0)
689*0Sstevel@tonic-gate 		return (err);
690*0Sstevel@tonic-gate 
691*0Sstevel@tonic-gate 	if ((err = ldi_ident_from_mod(&modlinkage, &li)) != 0)
692*0Sstevel@tonic-gate 		return (err);
693*0Sstevel@tonic-gate 
694*0Sstevel@tonic-gate 	if ((err = strplumb_sctpq(li)) != 0)
695*0Sstevel@tonic-gate 		goto done;
696*0Sstevel@tonic-gate 
697*0Sstevel@tonic-gate 	if ((err = strplumb_tcpq(li)) != 0)
698*0Sstevel@tonic-gate 		goto done;
699*0Sstevel@tonic-gate 
700*0Sstevel@tonic-gate 	if ((err = resolve_boot_path(li)) != 0)
701*0Sstevel@tonic-gate 		goto done;
702*0Sstevel@tonic-gate 
703*0Sstevel@tonic-gate 	DBG1("rootfs.bo_devname: %s\n", rootfs.bo_devname);
704*0Sstevel@tonic-gate 	DBG1("rootfs.bo_ifname: %s\n", rootfs.bo_ifname);
705*0Sstevel@tonic-gate 	DBG1("rootfs.bo_ppa: %d\n", rootfs.bo_ppa);
706*0Sstevel@tonic-gate 
707*0Sstevel@tonic-gate 	if ((err = strplumb_dev(li)) != 0)
708*0Sstevel@tonic-gate 		goto done;
709*0Sstevel@tonic-gate 
710*0Sstevel@tonic-gate done:
711*0Sstevel@tonic-gate 	ldi_ident_release(li);
712*0Sstevel@tonic-gate 
713*0Sstevel@tonic-gate 	return (err);
714*0Sstevel@tonic-gate }
715*0Sstevel@tonic-gate 
716*0Sstevel@tonic-gate /* multiboot:  diskless boot interface discovery */
717*0Sstevel@tonic-gate 
718*0Sstevel@tonic-gate #ifndef	__sparc
719*0Sstevel@tonic-gate 
720*0Sstevel@tonic-gate static uchar_t boot_macaddr[16];
721*0Sstevel@tonic-gate static int boot_maclen;
722*0Sstevel@tonic-gate static uchar_t *getmacaddr(dev_info_t *dip, int *maclen);
723*0Sstevel@tonic-gate static int matchmac(dev_info_t *dip, void *arg);
724*0Sstevel@tonic-gate extern int dl_attach(ldi_handle_t lh, int unit);
725*0Sstevel@tonic-gate extern int dl_bind(ldi_handle_t lh, uint_t sap, uint_t max_conn,
726*0Sstevel@tonic-gate     uint_t service, uint_t conn_mgmt);
727*0Sstevel@tonic-gate extern int dl_phys_addr(ldi_handle_t lh, struct ether_addr *eaddr);
728*0Sstevel@tonic-gate 
729*0Sstevel@tonic-gate char *
730*0Sstevel@tonic-gate strplumb_get_netdev_path(void)
731*0Sstevel@tonic-gate {
732*0Sstevel@tonic-gate 	char *macstr, *devpath = NULL;
733*0Sstevel@tonic-gate 	uchar_t *bootp;
734*0Sstevel@tonic-gate 	uint_t bootp_len, len;
735*0Sstevel@tonic-gate 
736*0Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
737*0Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, BP_BOOT_MAC, &macstr) == DDI_SUCCESS) {
738*0Sstevel@tonic-gate 		/*
739*0Sstevel@tonic-gate 		 * hard coded ether mac len for booting floppy on
740*0Sstevel@tonic-gate 		 * machines with old cards
741*0Sstevel@tonic-gate 		 */
742*0Sstevel@tonic-gate 		boot_maclen = ether_aton(macstr, boot_macaddr);
743*0Sstevel@tonic-gate 		if (boot_maclen != 6) {
744*0Sstevel@tonic-gate 			cmn_err(CE_WARN,
745*0Sstevel@tonic-gate 			    "malformed boot_mac property, %d bytes",
746*0Sstevel@tonic-gate 			    boot_maclen);
747*0Sstevel@tonic-gate 		}
748*0Sstevel@tonic-gate 		ddi_prop_free(macstr);
749*0Sstevel@tonic-gate 	} else if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, ddi_root_node(),
750*0Sstevel@tonic-gate 	    DDI_PROP_DONTPASS, BP_BOOTP_RESPONSE, &bootp, &bootp_len)
751*0Sstevel@tonic-gate 	    == DDI_SUCCESS) {
752*0Sstevel@tonic-gate 
753*0Sstevel@tonic-gate 		/*
754*0Sstevel@tonic-gate 		 * These offsets are defined by dhcp standard
755*0Sstevel@tonic-gate 		 * Should use structure offsets
756*0Sstevel@tonic-gate 		 */
757*0Sstevel@tonic-gate 		boot_maclen = *(bootp + 2);
758*0Sstevel@tonic-gate 		ASSERT(boot_maclen <= 16);
759*0Sstevel@tonic-gate 		(void) bcopy(bootp + 28, boot_macaddr, boot_maclen);
760*0Sstevel@tonic-gate 
761*0Sstevel@tonic-gate 		/* encode to ascii string to match what sparc OBP exports */
762*0Sstevel@tonic-gate 		dhcack = kmem_alloc(bootp_len * 2 + IFNAMSIZ + 2, KM_SLEEP);
763*0Sstevel@tonic-gate 		(void) octet_to_hexascii(bootp, bootp_len, dhcack + IFNAMSIZ,
764*0Sstevel@tonic-gate 		    &len);
765*0Sstevel@tonic-gate 		ASSERT(len < bootp_len * 2 + 2);
766*0Sstevel@tonic-gate 		ddi_prop_free(bootp);
767*0Sstevel@tonic-gate 	} else
768*0Sstevel@tonic-gate 		return (NULL);
769*0Sstevel@tonic-gate 
770*0Sstevel@tonic-gate 	ddi_walk_devs(ddi_root_node(), matchmac, (void *)&devpath);
771*0Sstevel@tonic-gate 	return (devpath);
772*0Sstevel@tonic-gate }
773*0Sstevel@tonic-gate 
774*0Sstevel@tonic-gate /*
775*0Sstevel@tonic-gate  * Get boot path from the boot_mac address
776*0Sstevel@tonic-gate  */
777*0Sstevel@tonic-gate /*ARGSUSED*/
778*0Sstevel@tonic-gate static int
779*0Sstevel@tonic-gate matchmac(dev_info_t *dip, void *arg)
780*0Sstevel@tonic-gate {
781*0Sstevel@tonic-gate 	char **devpathp = (char **)arg;
782*0Sstevel@tonic-gate 	char *model_str;
783*0Sstevel@tonic-gate 	uchar_t *macaddr;
784*0Sstevel@tonic-gate 	int maclen;
785*0Sstevel@tonic-gate 
786*0Sstevel@tonic-gate 	/* XXX Should use "device-type" per IEEE 1275 */
787*0Sstevel@tonic-gate 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0,
788*0Sstevel@tonic-gate 	    "model", &model_str) != DDI_SUCCESS)
789*0Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
790*0Sstevel@tonic-gate 
791*0Sstevel@tonic-gate 	if (strcmp(model_str, "Ethernet controller") != 0) {
792*0Sstevel@tonic-gate 		ddi_prop_free(model_str);
793*0Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
794*0Sstevel@tonic-gate 	}
795*0Sstevel@tonic-gate 	ddi_prop_free(model_str);
796*0Sstevel@tonic-gate 
797*0Sstevel@tonic-gate 	/* We have a network device now */
798*0Sstevel@tonic-gate 	if (i_ddi_attach_node_hierarchy(dip) != DDI_SUCCESS) {
799*0Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
800*0Sstevel@tonic-gate 	}
801*0Sstevel@tonic-gate 
802*0Sstevel@tonic-gate 	ASSERT(boot_maclen != 0);
803*0Sstevel@tonic-gate 	macaddr = getmacaddr(dip, &maclen);
804*0Sstevel@tonic-gate 	if (macaddr == NULL)
805*0Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
806*0Sstevel@tonic-gate 
807*0Sstevel@tonic-gate 	if (maclen != boot_maclen ||
808*0Sstevel@tonic-gate 	    bcmp(macaddr, boot_macaddr, maclen) != 0) {
809*0Sstevel@tonic-gate 		kmem_free(macaddr, maclen);
810*0Sstevel@tonic-gate 		return (DDI_WALK_CONTINUE);
811*0Sstevel@tonic-gate 	}
812*0Sstevel@tonic-gate 
813*0Sstevel@tonic-gate 	/* found hardware with the mac address */
814*0Sstevel@tonic-gate 	(void) localetheraddr((struct ether_addr *)macaddr, NULL);
815*0Sstevel@tonic-gate 	kmem_free(macaddr, maclen);
816*0Sstevel@tonic-gate 
817*0Sstevel@tonic-gate 	*devpathp = kmem_alloc(MAXPATHLEN, KM_SLEEP);
818*0Sstevel@tonic-gate 	(void) ddi_pathname(dip, *devpathp);
819*0Sstevel@tonic-gate 
820*0Sstevel@tonic-gate 	/* fill in the name portion of dhcack */
821*0Sstevel@tonic-gate 	if (dhcack)
822*0Sstevel@tonic-gate 		(void) snprintf(dhcack, IFNAMSIZ, "%s%d",
823*0Sstevel@tonic-gate 		    ddi_driver_name(dip), i_ddi_devi_get_ppa(dip));
824*0Sstevel@tonic-gate 	return (DDI_WALK_TERMINATE);
825*0Sstevel@tonic-gate }
826*0Sstevel@tonic-gate 
827*0Sstevel@tonic-gate static uchar_t *
828*0Sstevel@tonic-gate getmacaddr_gldv3(char *drv, int inst, int *maclenp)
829*0Sstevel@tonic-gate {
830*0Sstevel@tonic-gate 	char ifname[16];
831*0Sstevel@tonic-gate 	mac_handle_t mh;
832*0Sstevel@tonic-gate 	uchar_t *macaddr;
833*0Sstevel@tonic-gate 
834*0Sstevel@tonic-gate 	(void) snprintf(ifname, sizeof (ifname), "%s%d", drv, inst);
835*0Sstevel@tonic-gate 	if (mac_open(ifname, 0, &mh) < 0) {
836*0Sstevel@tonic-gate 		return (NULL);
837*0Sstevel@tonic-gate 	}
838*0Sstevel@tonic-gate 	*maclenp = sizeof (struct ether_addr);
839*0Sstevel@tonic-gate 	macaddr = kmem_alloc(*maclenp, KM_SLEEP);
840*0Sstevel@tonic-gate 	mac_unicst_get(mh, macaddr);
841*0Sstevel@tonic-gate 	mac_close(mh);
842*0Sstevel@tonic-gate 
843*0Sstevel@tonic-gate 	return (macaddr);
844*0Sstevel@tonic-gate }
845*0Sstevel@tonic-gate 
846*0Sstevel@tonic-gate static uchar_t *
847*0Sstevel@tonic-gate getmacaddr(dev_info_t *dip, int *maclenp)
848*0Sstevel@tonic-gate {
849*0Sstevel@tonic-gate 	int rc, ppa;
850*0Sstevel@tonic-gate 	ldi_ident_t li;
851*0Sstevel@tonic-gate 	ldi_handle_t lh;
852*0Sstevel@tonic-gate 	char *drv_name = (char *)ddi_driver_name(dip);
853*0Sstevel@tonic-gate 	char *clonepath;
854*0Sstevel@tonic-gate 	uchar_t *macaddr = NULL;
855*0Sstevel@tonic-gate 
856*0Sstevel@tonic-gate 	/* a simpler way to get mac address for GLDv3 drivers */
857*0Sstevel@tonic-gate 	if (GLDV3_DRV(ddi_name_to_major(drv_name))) {
858*0Sstevel@tonic-gate 		return (getmacaddr_gldv3(drv_name, ddi_get_instance(dip),
859*0Sstevel@tonic-gate 		    maclenp));
860*0Sstevel@tonic-gate 	}
861*0Sstevel@tonic-gate 
862*0Sstevel@tonic-gate 	if (rc = ldi_ident_from_mod(&modlinkage, &li)) {
863*0Sstevel@tonic-gate 		cmn_err(CE_WARN,
864*0Sstevel@tonic-gate 		    "getmacaddr: ldi_ident_from_mod failed: %d\n", rc);
865*0Sstevel@tonic-gate 		return (NULL);
866*0Sstevel@tonic-gate 	}
867*0Sstevel@tonic-gate 
868*0Sstevel@tonic-gate 	clonepath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
869*0Sstevel@tonic-gate 	(void) snprintf(clonepath, MAXPATHLEN,
870*0Sstevel@tonic-gate 	    "/devices/pseudo/clone@0:%s", drv_name);
871*0Sstevel@tonic-gate 
872*0Sstevel@tonic-gate 	rc = ldi_open_by_name(clonepath, FREAD|FWRITE, CRED(), &lh, li);
873*0Sstevel@tonic-gate 	ldi_ident_release(li);
874*0Sstevel@tonic-gate 	if (rc) {
875*0Sstevel@tonic-gate 		cmn_err(CE_WARN,
876*0Sstevel@tonic-gate 		    "getmacaddr: ldi_open_by_name(%s) failed: %d\n",
877*0Sstevel@tonic-gate 		    clonepath, rc);
878*0Sstevel@tonic-gate 		kmem_free(clonepath, MAXPATHLEN);
879*0Sstevel@tonic-gate 		return (NULL);
880*0Sstevel@tonic-gate 	}
881*0Sstevel@tonic-gate 	kmem_free(clonepath, MAXPATHLEN);
882*0Sstevel@tonic-gate 
883*0Sstevel@tonic-gate 	ppa = i_ddi_devi_get_ppa(dip);
884*0Sstevel@tonic-gate 	if ((dl_attach(lh, ppa) != 0) ||
885*0Sstevel@tonic-gate 	    (dl_bind(lh, ETHERTYPE_IP, 0, DL_CLDLS, 0) != 0)) {
886*0Sstevel@tonic-gate 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
887*0Sstevel@tonic-gate 		cmn_err(CE_WARN,
888*0Sstevel@tonic-gate 		    "getmacaddr: dl_attach/bind(%s%d) failed: %d\n",
889*0Sstevel@tonic-gate 		    drv_name, ppa, rc);
890*0Sstevel@tonic-gate 		return (NULL);
891*0Sstevel@tonic-gate 	}
892*0Sstevel@tonic-gate 	*maclenp = sizeof (struct ether_addr);
893*0Sstevel@tonic-gate 	macaddr = kmem_alloc(*maclenp, KM_SLEEP);
894*0Sstevel@tonic-gate 	if (dl_phys_addr(lh, (struct ether_addr *)macaddr) != 0) {
895*0Sstevel@tonic-gate 		kmem_free(macaddr, *maclenp);
896*0Sstevel@tonic-gate 		macaddr = NULL;
897*0Sstevel@tonic-gate 		*maclenp = 0;
898*0Sstevel@tonic-gate 		cmn_err(CE_WARN,
899*0Sstevel@tonic-gate 		    "getmacaddr: dl_macaddr(%s%d) failed: %d\n",
900*0Sstevel@tonic-gate 		    drv_name, ppa, rc);
901*0Sstevel@tonic-gate 	}
902*0Sstevel@tonic-gate 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
903*0Sstevel@tonic-gate 	return (macaddr);
904*0Sstevel@tonic-gate }
905*0Sstevel@tonic-gate 
906*0Sstevel@tonic-gate #endif	/* !__sparc */
907