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