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