xref: /titanic_51/usr/src/uts/common/io/strplumb.c (revision ccbf80fa3b6bf6b986dca9037e5ad9d6c9f9fa65)
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 2006 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 	 * UDP
236 	 */
237 	DBG0("setting up udp autopush\n");
238 
239 	mods[0] = UDP;
240 
241 	maj = ddi_name_to_major(UDP);
242 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, &anchor,
243 	    mods)) != 0) {
244 		printf("strplumb: kstr_autopush(SET/UDP) failed: %d\n", err);
245 		return (err);
246 	}
247 
248 	maj = ddi_name_to_major(UDP6);
249 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, &anchor,
250 	    mods)) != 0) {
251 		printf("strplumb: kstr_autopush(SET/UDP6) failed: %d\n", err);
252 		return (err);
253 	}
254 
255 	/*
256 	 * ICMP
257 	 */
258 	DBG0("setting up icmp autopush\n");
259 
260 	mods[0] = ICMP;
261 
262 	maj = ddi_name_to_major(ICMP);
263 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, NULL,
264 	    mods)) != 0) {
265 		printf("strplumb: kstr_autopush(SET/ICMP) failed: %d\n", err);
266 		return (err);
267 	}
268 
269 	maj = ddi_name_to_major(ICMP6);
270 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, NULL,
271 	    mods)) != 0) {
272 		printf("strplumb: kstr_autopush(SET/ICMP6) failed: %d\n", err);
273 		return (err);
274 	}
275 
276 	/*
277 	 * ARP
278 	 */
279 	DBG0("setting up arp autopush\n");
280 
281 	mods[0] = ARP;
282 
283 	maj = ddi_name_to_major(ARP);
284 	if ((err = kstr_autopush(SET_AUTOPUSH, &maj, &min, NULL, &anchor,
285 	    mods)) != 0) {
286 		printf("strplumb: kstr_autopush(SET/ARP) failed: %d\n", err);
287 		return (err);
288 	}
289 
290 	return (0);
291 }
292 
293 static int
294 strplumb_sctpq(ldi_ident_t li)
295 {
296 	ldi_handle_t	lh = NULL;
297 	int		err;
298 	int		rval;
299 
300 	DBG0("configuring SCTP default queue\n");
301 
302 	if ((err = ldi_open_by_name(SCTP6DEV, FREAD|FWRITE, CRED(), &lh,
303 	    li)) != 0) {
304 		printf("strplumb: open of SCTP6DEV failed: %d\n", err);
305 		return (err);
306 	}
307 
308 	if ((err = ldi_ioctl(lh, SCTP_IOC_DEFAULT_Q, (intptr_t)0, FKIOCTL,
309 	    CRED(), &rval)) != 0) {
310 		printf("strplumb: failed to set SCTP default queue: %d\n",
311 		    err);
312 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
313 		return (err);
314 	}
315 
316 	return (0);
317 }
318 
319 static int
320 strplumb_tcpq(ldi_ident_t li)
321 {
322 	ldi_handle_t	lh = NULL;
323 	ldi_handle_t	ip_lh = NULL;
324 	int		err;
325 	int		rval;
326 
327 	DBG0("configuring TCP default queue\n");
328 
329 	/*
330 	 * We open IP6DEV here because we need to have it open to in
331 	 * order to open TCP6DEV successfully.
332 	 */
333 	if ((err = ldi_open_by_name(IP6DEV, FREAD|FWRITE, CRED(), &ip_lh,
334 	    li)) != 0) {
335 		printf("strplumb: open of IP6DEV failed: %d\n", err);
336 		return (err);
337 	}
338 
339 	/*
340 	 * We set the tcp default queue to IPv6 because IPv4 falls back to
341 	 * IPv6 when it can't find a client, but IPv6 does not fall back to
342 	 * IPv4.
343 	 */
344 	if ((err = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, CRED(), &lh,
345 	    li)) != 0) {
346 		printf("strplumb: open of TCP6DEV failed: %d\n", err);
347 		goto done;
348 	}
349 
350 	if ((err = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, (intptr_t)0, FKIOCTL,
351 	    CRED(), &rval)) != 0) {
352 		printf("strplumb: failed to set TCP default queue: %d\n",
353 		    err);
354 		goto done;
355 	}
356 
357 done:
358 	(void) ldi_close(ip_lh, FREAD|FWRITE, CRED());
359 	return (err);
360 }
361 
362 /*
363  * Can be set in /etc/system in the case of local booting. See comment below.
364  */
365 char	*ndev_name = 0;
366 int	ndev_unit = 0;
367 
368 /*
369  * If we booted diskless then strplumb() will have been called from
370  * swapgeneric.c:rootconf(). All we can do in that case is plumb the
371  * network device that we booted from.
372  *
373  * If we booted from a local disk, we will have been called from main(),
374  * and normally we defer the plumbing of interfaces until network/physical.
375  * This can be overridden by setting "ndev_name" in /etc/system.
376  */
377 static int
378 resolve_boot_path(void)
379 {
380 	char			*devpath = NULL;
381 	dev_info_t		*dip;
382 	const char		*driver;
383 	int			instance;
384 
385 	if (strncmp(rootfs.bo_fstype, "nfs", 3) == 0)
386 		devpath = rootfs.bo_name;
387 #ifndef __sparc
388 	else
389 		devpath = strplumb_get_netdev_path();
390 #endif
391 
392 	if (devpath != NULL) {
393 		DBG1("resolving boot-path: %s\n", devpath);
394 
395 		/*
396 		 * Hold the devi since this is the root device.
397 		 */
398 		if ((dip = e_ddi_hold_devi_by_path(devpath, 0)) == NULL) {
399 			printf("strplumb: unable to hold root device: %s\n",
400 			    devpath);
401 			return (ENXIO);
402 		}
403 
404 		driver = ddi_driver_name(dip);
405 		instance = ddi_get_instance(dip);
406 	} else {
407 		if (ndev_name == NULL)
408 			return (ENODEV);
409 
410 		DBG2("using ndev_name (%s) ndev_unit (%d)\n", ndev_name,
411 		    ndev_unit);
412 
413 		if (i_ddi_attach_hw_nodes(ndev_name) != DDI_SUCCESS) {
414 			printf("strplumb: cannot load ndev_name '%s'\n",
415 			    ndev_name);
416 			return (ENXIO);
417 		}
418 
419 		driver = ndev_name;
420 		instance = ndev_unit;
421 	}
422 
423 	(void) snprintf(rootfs.bo_devname, BO_MAXOBJNAME,
424 	    "/devices/pseudo/clone@0:%s", driver);
425 	(void) snprintf(rootfs.bo_ifname, BO_MAXOBJNAME, "%s%d",
426 	    driver, instance);
427 	rootfs.bo_ppa = instance;
428 	return (0);
429 }
430 
431 static int
432 getifflags(ldi_handle_t lh, struct lifreq *lifrp)
433 {
434 	struct strioctl	iocb;
435 	int		rval;
436 
437 	iocb.ic_cmd = SIOCGLIFFLAGS;
438 	iocb.ic_timout = 15;
439 	iocb.ic_len = sizeof (struct lifreq);
440 	iocb.ic_dp = (char *)lifrp;
441 
442 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
443 
444 }
445 
446 static int
447 setifname(ldi_handle_t lh, struct lifreq *lifrp)
448 {
449 	struct strioctl	iocb;
450 	int		rval;
451 
452 	iocb.ic_cmd = SIOCSLIFNAME;
453 	iocb.ic_timout = 15;
454 	iocb.ic_len = sizeof (struct lifreq);
455 	iocb.ic_dp = (char *)lifrp;
456 
457 	return (ldi_ioctl(lh, I_STR, (intptr_t)&iocb, FKIOCTL, CRED(), &rval));
458 }
459 
460 static int
461 strplumb_dev(ldi_ident_t li)
462 {
463 	ldi_handle_t	lh = NULL;
464 	ldi_handle_t	mux_lh = NULL;
465 	int		err;
466 	struct lifreq	lifr;
467 	struct ifreq	ifr;
468 	int		rval;
469 
470 	bzero(&lifr, sizeof (struct lifreq));
471 	bzero(&ifr, sizeof (ifr));
472 
473 	/*
474 	 * Now set up the links. Ultimately, we should have two streams
475 	 * permanently linked underneath UDP (which is actually IP with UDP
476 	 * autopushed). One stream consists of the ARP-[ifname] combination,
477 	 * while the other consists of ARP-IP-[ifname]. The second combination
478 	 * seems a little weird, but is linked underneath UDP just to keep it
479 	 * around.
480 	 *
481 	 * We pin underneath UDP here to match what is done in ifconfig(1m);
482 	 * otherwise, ifconfig will be unable to unplumb the stream (the major
483 	 * number and mux id must both match for a successful I_PUNLINK).
484 	 *
485 	 * There are subtleties in the plumbing which make it essential to
486 	 * follow the logic used in ifconfig(1m) very closely.
487 	 */
488 
489 	/*
490 	 * Plumb UDP-ARP-IP-<dev>
491 	 */
492 
493 	if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
494 	    &lh, li)) != 0) {
495 		printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
496 		    err);
497 		goto done;
498 	}
499 
500 
501 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)IP, FKIOCTL, CRED(),
502 	    &rval)) != 0) {
503 		printf("strplumb: push IP failed: %d\n", err);
504 		goto done;
505 	}
506 
507 	if ((err = getifflags(lh, &lifr)) != 0)
508 		goto done;
509 
510 	lifr.lifr_flags |= IFF_IPV4;
511 	lifr.lifr_flags &= ~IFF_IPV6;
512 
513 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)ARP, FKIOCTL, CRED(),
514 	    &rval)) != 0) {
515 		printf("strplumb: push ARP failed: %d\n", err);
516 		goto done;
517 	}
518 
519 	(void) strlcpy(lifr.lifr_name, rootfs.bo_ifname,
520 	    sizeof (lifr.lifr_name));
521 	lifr.lifr_ppa = rootfs.bo_ppa;
522 
523 	if ((err = setifname(lh, &lifr)) != 0)
524 		goto done;
525 
526 	/* Get the flags and check if ARP is needed */
527 	if ((err = getifflags(lh, &lifr)) != 0) {
528 		printf("strplumb: getifflags %s IP failed, error %d\n",
529 		    lifr.lifr_name, err);
530 		goto done;
531 	}
532 
533 	/* Pop out ARP if not needed */
534 	if (lifr.lifr_flags & IFF_NOARP) {
535 		err = ldi_ioctl(lh, I_POP, (intptr_t)0, FKIOCTL, CRED(),
536 		    &rval);
537 		if (err != 0) {
538 			printf("strplumb: pop ARP failed, error %d\n", err);
539 			goto done;
540 		}
541 	}
542 
543 	if ((err = ldi_open_by_name(UDPDEV, FREAD|FWRITE, CRED(), &mux_lh,
544 	    li)) != 0) {
545 		printf("strplumb: open of UDPDEV failed: %d\n", err);
546 		goto done;
547 	}
548 
549 	if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
550 	    FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
551 	    &(ifr.ifr_ip_muxid))) != 0) {
552 		printf("strplumb: plink UDP-ARP-IP-%s failed: %d\n",
553 		    rootfs.bo_ifname, err);
554 		goto done;
555 	}
556 
557 	DBG2("UDP-ARP-IP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_ip_muxid);
558 
559 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
560 	lh = NULL;
561 
562 	/*
563 	 * Plumb UDP-ARP-<dev>
564 	 */
565 
566 	if ((err = ldi_open_by_name(rootfs.bo_devname, FREAD|FWRITE, CRED(),
567 	    &lh, li)) != 0) {
568 		printf("strplumb: open %s failed: %d\n", rootfs.bo_devname,
569 		    err);
570 		goto done;
571 	}
572 
573 	if ((err = ldi_ioctl(lh, I_PUSH, (intptr_t)ARP, FKIOCTL, CRED(),
574 	    &rval)) != 0) {
575 		printf("strplumb: push ARP failed: %d\n", err);
576 		goto done;
577 	}
578 
579 	if ((err = setifname(lh, &lifr)) != 0)
580 		goto done;
581 
582 	if ((err = ldi_ioctl(mux_lh, I_PLINK, (intptr_t)lh,
583 	    FREAD|FWRITE|FNOCTTY|FKIOCTL, CRED(),
584 	    &(ifr.ifr_arp_muxid))) != 0) {
585 		printf("strplumb: plink UDP-ARP-%s failed: %d\n",
586 		    rootfs.bo_ifname, err);
587 		goto done;
588 	}
589 
590 	DBG2("UDP-ARP-%s muxid: %d\n", rootfs.bo_ifname, ifr.ifr_arp_muxid);
591 
592 	/*
593 	 * Cache the mux ids.
594 	 */
595 	(void) strlcpy(ifr.ifr_name, rootfs.bo_ifname, sizeof (ifr.ifr_name));
596 
597 	if ((err = ldi_ioctl(mux_lh, SIOCSIFMUXID, (intptr_t)&ifr, FKIOCTL,
598 	    CRED(), &rval)) != 0) {
599 		printf("strplumb: SIOCSIFMUXID failed: %d\n", err);
600 		goto done;
601 	}
602 
603 done:
604 	if (lh != NULL)
605 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
606 
607 	if (mux_lh != NULL)
608 		(void) ldi_close(mux_lh, FREAD|FWRITE, CRED());
609 
610 	return (err);
611 }
612 
613 /*
614  * Do streams plumbing for internet protocols.
615  */
616 int
617 strplumb(void)
618 {
619 	ldi_ident_t	li;
620 	int		err;
621 
622 	if ((err = strplumb_init()) != 0)
623 		return (err);
624 
625 	if ((err = strplumb_autopush()) != 0)
626 		return (err);
627 
628 	if ((err = ldi_ident_from_mod(&modlinkage, &li)) != 0)
629 		return (err);
630 
631 	if ((err = strplumb_sctpq(li)) != 0)
632 		goto done;
633 
634 	if ((err = strplumb_tcpq(li)) != 0)
635 		goto done;
636 
637 	if ((err = resolve_boot_path()) != 0)
638 		goto done;
639 
640 	DBG1("rootfs.bo_devname: %s\n", rootfs.bo_devname);
641 	DBG1("rootfs.bo_ifname: %s\n", rootfs.bo_ifname);
642 	DBG1("rootfs.bo_ppa: %d\n", rootfs.bo_ppa);
643 
644 	if ((err = strplumb_dev(li)) != 0)
645 		goto done;
646 
647 done:
648 	ldi_ident_release(li);
649 
650 	return (err);
651 }
652 
653 /* multiboot:  diskless boot interface discovery */
654 
655 #ifndef	__sparc
656 
657 static uchar_t boot_macaddr[16];
658 static int boot_maclen;
659 static uchar_t *getmacaddr(dev_info_t *dip, int *maclen);
660 static int matchmac(dev_info_t *dip, void *arg);
661 int dl_attach(ldi_handle_t lh, int unit);
662 int dl_bind(ldi_handle_t lh, uint_t sap, uint_t max_conn,
663     uint_t service, uint_t conn_mgmt);
664 int dl_phys_addr(ldi_handle_t lh, struct ether_addr *eaddr);
665 
666 char *
667 strplumb_get_netdev_path(void)
668 {
669 	char *macstr, *devpath = NULL;
670 	uchar_t *bootp;
671 	uint_t bootp_len, len;
672 
673 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
674 	    DDI_PROP_DONTPASS, BP_BOOT_MAC, &macstr) == DDI_SUCCESS) {
675 		/*
676 		 * hard coded ether mac len for booting floppy on
677 		 * machines with old cards
678 		 */
679 		boot_maclen = ether_aton(macstr, boot_macaddr);
680 		if (boot_maclen != 6) {
681 			cmn_err(CE_WARN,
682 			    "malformed boot_mac property, %d bytes",
683 			    boot_maclen);
684 		}
685 		ddi_prop_free(macstr);
686 	} else if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, ddi_root_node(),
687 	    DDI_PROP_DONTPASS, BP_BOOTP_RESPONSE, &bootp, &bootp_len)
688 	    == DDI_SUCCESS) {
689 
690 		/*
691 		 * These offsets are defined by dhcp standard
692 		 * Should use structure offsets
693 		 */
694 		boot_maclen = *(bootp + 2);
695 		ASSERT(boot_maclen <= 16);
696 		(void) bcopy(bootp + 28, boot_macaddr, boot_maclen);
697 
698 		/* encode to ascii string to match what sparc OBP exports */
699 		dhcack = kmem_zalloc(bootp_len * 2 + IFNAMSIZ + 2, KM_SLEEP);
700 		(void) octet_to_hexascii(bootp, bootp_len, dhcack + IFNAMSIZ,
701 		    &len);
702 		ASSERT(len < bootp_len * 2 + 2);
703 		ddi_prop_free(bootp);
704 	} else
705 		return (NULL);
706 
707 	ddi_walk_devs(ddi_root_node(), matchmac, (void *)&devpath);
708 	return (devpath);
709 }
710 
711 /*
712  * Get boot path from the boot_mac address
713  */
714 /*ARGSUSED*/
715 static int
716 matchmac(dev_info_t *dip, void *arg)
717 {
718 	char **devpathp = (char **)arg;
719 	char *model_str;
720 	uchar_t *macaddr;
721 	int maclen;
722 
723 	/* XXX Should use "device-type" per IEEE 1275 */
724 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0,
725 	    "model", &model_str) != DDI_SUCCESS)
726 		return (DDI_WALK_CONTINUE);
727 
728 	if (strcmp(model_str, "Ethernet controller") != 0) {
729 		ddi_prop_free(model_str);
730 		return (DDI_WALK_CONTINUE);
731 	}
732 	ddi_prop_free(model_str);
733 
734 	/* We have a network device now */
735 	if (i_ddi_attach_node_hierarchy(dip) != DDI_SUCCESS) {
736 		return (DDI_WALK_CONTINUE);
737 	}
738 
739 	ASSERT(boot_maclen != 0);
740 	macaddr = getmacaddr(dip, &maclen);
741 	if (macaddr == NULL)
742 		return (DDI_WALK_CONTINUE);
743 
744 	if (maclen != boot_maclen ||
745 	    bcmp(macaddr, boot_macaddr, maclen) != 0) {
746 		kmem_free(macaddr, maclen);
747 		return (DDI_WALK_CONTINUE);
748 	}
749 
750 	/* found hardware with the mac address */
751 	(void) localetheraddr((struct ether_addr *)macaddr, NULL);
752 	kmem_free(macaddr, maclen);
753 
754 	*devpathp = kmem_alloc(MAXPATHLEN, KM_SLEEP);
755 	(void) ddi_pathname(dip, *devpathp);
756 
757 	/* fill in the name portion of dhcack */
758 	if (dhcack)
759 		(void) snprintf(dhcack, IFNAMSIZ, "%s%d",
760 		    ddi_driver_name(dip), i_ddi_devi_get_ppa(dip));
761 	return (DDI_WALK_TERMINATE);
762 }
763 
764 static uchar_t *
765 getmacaddr_gldv3(char *drv, int inst, int *maclenp)
766 {
767 	char ifname[16];
768 	mac_handle_t mh;
769 	uchar_t *macaddr;
770 
771 	(void) snprintf(ifname, sizeof (ifname), "%s%d", drv, inst);
772 	if (mac_open(ifname, inst, &mh) < 0) {
773 		return (NULL);
774 	}
775 	*maclenp = sizeof (struct ether_addr);
776 	macaddr = kmem_alloc(*maclenp, KM_SLEEP);
777 	mac_unicst_get(mh, macaddr);
778 	mac_close(mh);
779 
780 	return (macaddr);
781 }
782 
783 static uchar_t *
784 getmacaddr(dev_info_t *dip, int *maclenp)
785 {
786 	int rc, ppa;
787 	ldi_ident_t li;
788 	ldi_handle_t lh;
789 	char *drv_name = (char *)ddi_driver_name(dip);
790 	char *clonepath;
791 	uchar_t *macaddr = NULL;
792 
793 	/* a simpler way to get mac address for GLDv3 drivers */
794 	if (GLDV3_DRV(ddi_name_to_major(drv_name))) {
795 		return (getmacaddr_gldv3(drv_name, ddi_get_instance(dip),
796 		    maclenp));
797 	}
798 
799 	if (rc = ldi_ident_from_mod(&modlinkage, &li)) {
800 		cmn_err(CE_WARN,
801 		    "getmacaddr: ldi_ident_from_mod failed: %d\n", rc);
802 		return (NULL);
803 	}
804 
805 	clonepath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
806 	(void) snprintf(clonepath, MAXPATHLEN,
807 	    "/devices/pseudo/clone@0:%s", drv_name);
808 
809 	rc = ldi_open_by_name(clonepath, FREAD|FWRITE, CRED(), &lh, li);
810 	ldi_ident_release(li);
811 	if (rc) {
812 		cmn_err(CE_WARN,
813 		    "getmacaddr: ldi_open_by_name(%s) failed: %d\n",
814 		    clonepath, rc);
815 		kmem_free(clonepath, MAXPATHLEN);
816 		return (NULL);
817 	}
818 	kmem_free(clonepath, MAXPATHLEN);
819 
820 	ppa = i_ddi_devi_get_ppa(dip);
821 	if ((dl_attach(lh, ppa) != 0) ||
822 	    (dl_bind(lh, ETHERTYPE_IP, 0, DL_CLDLS, 0) != 0)) {
823 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
824 		cmn_err(CE_WARN,
825 		    "getmacaddr: dl_attach/bind(%s%d) failed: %d\n",
826 		    drv_name, ppa, rc);
827 		return (NULL);
828 	}
829 	*maclenp = sizeof (struct ether_addr);
830 	macaddr = kmem_alloc(*maclenp, KM_SLEEP);
831 	if (dl_phys_addr(lh, (struct ether_addr *)macaddr) != 0) {
832 		kmem_free(macaddr, *maclenp);
833 		macaddr = NULL;
834 		*maclenp = 0;
835 		cmn_err(CE_WARN,
836 		    "getmacaddr: dl_macaddr(%s%d) failed: %d\n",
837 		    drv_name, ppa, rc);
838 	}
839 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
840 	return (macaddr);
841 }
842 
843 #endif	/* !__sparc */
844 
845 int
846 dl_attach(ldi_handle_t lh, int unit)
847 {
848 	dl_attach_req_t *attach_req;
849 	dl_error_ack_t *error_ack;
850 	union DL_primitives *dl_prim;
851 	mblk_t *mp;
852 	int error;
853 
854 	if ((mp = allocb(sizeof (dl_attach_req_t), BPRI_MED)) == NULL) {
855 		cmn_err(CE_WARN, "dl_attach: allocb failed");
856 		return (ENOSR);
857 	}
858 	mp->b_datap->db_type = M_PROTO;
859 	mp->b_wptr += sizeof (dl_attach_req_t);
860 
861 	attach_req = (dl_attach_req_t *)mp->b_rptr;
862 	attach_req->dl_primitive = DL_ATTACH_REQ;
863 	attach_req->dl_ppa = unit;
864 
865 	(void) ldi_putmsg(lh, mp);
866 	if ((error = ldi_getmsg(lh, &mp, (timestruc_t *)NULL)) != 0) {
867 		printf("dl_attach: ldi_getmsg failed: %d\n", error);
868 		return (error);
869 	}
870 
871 	dl_prim = (union DL_primitives *)mp->b_rptr;
872 	switch (dl_prim->dl_primitive) {
873 	case DL_OK_ACK:
874 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_ok_ack_t)) {
875 			printf("dl_attach: DL_OK_ACK protocol error\n");
876 			break;
877 		}
878 		if (((dl_ok_ack_t *)dl_prim)->dl_correct_primitive !=
879 		    DL_ATTACH_REQ) {
880 			printf("dl_attach: DL_OK_ACK rtnd prim %u\n",
881 			    ((dl_ok_ack_t *)dl_prim)->dl_correct_primitive);
882 			break;
883 		}
884 		freemsg(mp);
885 		return (0);
886 
887 	case DL_ERROR_ACK:
888 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_error_ack_t)) {
889 			printf("dl_attach: DL_ERROR_ACK protocol error\n");
890 			break;
891 		}
892 
893 		error_ack = (dl_error_ack_t *)dl_prim;
894 		switch (error_ack->dl_errno) {
895 		case DL_BADPPA:
896 			printf("dl_attach: DL_ERROR_ACK bad PPA\n");
897 			break;
898 
899 		case DL_ACCESS:
900 			printf("dl_attach: DL_ERROR_ACK access error\n");
901 			break;
902 
903 		default:
904 			printf("dl_attach: DLPI error %u\n",
905 			    error_ack->dl_errno);
906 			break;
907 		}
908 		break;
909 
910 	default:
911 		printf("dl_attach: bad ACK header %u\n", dl_prim->dl_primitive);
912 		break;
913 	}
914 
915 	/*
916 	 * Error return only.
917 	 */
918 	freemsg(mp);
919 	return (-1);
920 }
921 
922 int
923 dl_bind(ldi_handle_t lh, uint_t sap, uint_t max_conn, uint_t service,
924 	uint_t conn_mgmt)
925 {
926 	dl_bind_req_t *bind_req;
927 	dl_error_ack_t *error_ack;
928 	union DL_primitives *dl_prim;
929 	mblk_t *mp;
930 	int error;
931 
932 	if ((mp = allocb(sizeof (dl_bind_req_t), BPRI_MED)) == NULL) {
933 		cmn_err(CE_WARN, "dl_bind: allocb failed");
934 		return (ENOSR);
935 	}
936 	mp->b_datap->db_type = M_PROTO;
937 
938 	bind_req = (dl_bind_req_t *)mp->b_wptr;
939 	mp->b_wptr += sizeof (dl_bind_req_t);
940 	bind_req->dl_primitive = DL_BIND_REQ;
941 	bind_req->dl_sap = sap;
942 	bind_req->dl_max_conind = max_conn;
943 	bind_req->dl_service_mode = service;
944 	bind_req->dl_conn_mgmt = conn_mgmt;
945 	bind_req->dl_xidtest_flg = 0;
946 
947 	(void) ldi_putmsg(lh, mp);
948 	if ((error = ldi_getmsg(lh, &mp, (timestruc_t *)NULL)) != 0) {
949 		printf("dl_bind: ldi_getmsg failed: %d\n", error);
950 		return (error);
951 	}
952 
953 	dl_prim = (union DL_primitives *)mp->b_rptr;
954 	switch (dl_prim->dl_primitive) {
955 	case DL_BIND_ACK:
956 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_bind_ack_t)) {
957 			printf("dl_bind: DL_BIND_ACK protocol error\n");
958 			break;
959 		}
960 		if (((dl_bind_ack_t *)dl_prim)->dl_sap != sap) {
961 			printf("dl_bind: DL_BIND_ACK bad sap %u\n",
962 			    ((dl_bind_ack_t *)dl_prim)->dl_sap);
963 			break;
964 		}
965 		freemsg(mp);
966 		return (0);
967 
968 	case DL_ERROR_ACK:
969 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_error_ack_t)) {
970 			printf("dl_bind: DL_ERROR_ACK protocol error\n");
971 			break;
972 		}
973 
974 		error_ack = (dl_error_ack_t *)dl_prim;
975 		printf("dl_bind: DLPI error %u\n", error_ack->dl_errno);
976 		break;
977 
978 	default:
979 		printf("dl_bind: bad ACK header %u\n", dl_prim->dl_primitive);
980 		break;
981 	}
982 
983 	/*
984 	 * Error return only.
985 	 */
986 	freemsg(mp);
987 	return (-1);
988 }
989 
990 int
991 dl_phys_addr(ldi_handle_t lh, struct ether_addr *eaddr)
992 {
993 	dl_phys_addr_req_t *phys_addr_req;
994 	dl_phys_addr_ack_t *phys_addr_ack;
995 	dl_error_ack_t *error_ack;
996 	union DL_primitives *dl_prim;
997 	mblk_t *mp;
998 	int error;
999 	uchar_t *addrp;
1000 	timestruc_t tv;
1001 
1002 	if ((mp = allocb(sizeof (dl_phys_addr_req_t), BPRI_MED)) ==
1003 	    (mblk_t *)NULL) {
1004 		cmn_err(CE_WARN, "dl_phys_addr: allocb failed");
1005 		return (ENOSR);
1006 	}
1007 	mp->b_datap->db_type = M_PROTO;
1008 	mp->b_wptr += sizeof (dl_phys_addr_req_t);
1009 
1010 	phys_addr_req = (dl_phys_addr_req_t *)mp->b_rptr;
1011 	phys_addr_req->dl_primitive = DL_PHYS_ADDR_REQ;
1012 	phys_addr_req->dl_addr_type = DL_CURR_PHYS_ADDR;
1013 
1014 	/*
1015 	 * In case some provider doesn't implement or nack the
1016 	 * request just wait for 15 seconds.
1017 	 */
1018 	tv.tv_sec = 15;
1019 	tv.tv_nsec = 0;
1020 
1021 	(void) ldi_putmsg(lh, mp);
1022 	error = ldi_getmsg(lh, &mp, &tv);
1023 	if (error == ETIME) {
1024 		printf("dl_phys_addr: timed out\n");
1025 		return (-1);
1026 	} else if (error != 0) {
1027 		printf("dl_phys_addr: ldi_getmsg failed: %d\n", error);
1028 		return (error);
1029 	}
1030 
1031 	dl_prim = (union DL_primitives *)mp->b_rptr;
1032 	switch (dl_prim->dl_primitive) {
1033 	case DL_PHYS_ADDR_ACK:
1034 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_phys_addr_ack_t)) {
1035 			printf("dl_phys_addr: "
1036 				"DL_PHYS_ADDR_ACK protocol error\n");
1037 			break;
1038 		}
1039 		phys_addr_ack = &dl_prim->physaddr_ack;
1040 		if (phys_addr_ack->dl_addr_length != sizeof (*eaddr)) {
1041 			printf("dl_phys_addr: DL_PHYS_ADDR_ACK bad len %u\n",
1042 				phys_addr_ack->dl_addr_length);
1043 			break;
1044 		}
1045 		if (phys_addr_ack->dl_addr_length +
1046 		    phys_addr_ack->dl_addr_offset > (mp->b_wptr-mp->b_rptr)) {
1047 			printf("dl_phys_addr: DL_PHYS_ADDR_ACK bad len %u\n",
1048 				phys_addr_ack->dl_addr_length);
1049 			break;
1050 		}
1051 		addrp = mp->b_rptr + phys_addr_ack->dl_addr_offset;
1052 		bcopy(addrp, eaddr, sizeof (*eaddr));
1053 		freemsg(mp);
1054 		return (0);
1055 
1056 	case DL_ERROR_ACK:
1057 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_error_ack_t)) {
1058 			printf("dl_phys_addr: DL_ERROR_ACK protocol error\n");
1059 			break;
1060 		}
1061 
1062 		error_ack = (dl_error_ack_t *)dl_prim;
1063 		printf("dl_phys_addr: DLPI error %u\n",
1064 		    error_ack->dl_errno);
1065 		break;
1066 
1067 	default:
1068 		printf("dl_phys_addr: bad ACK header %u\n",
1069 			dl_prim->dl_primitive);
1070 		break;
1071 	}
1072 
1073 	/*
1074 	 * Error return only.
1075 	 */
1076 	freemsg(mp);
1077 	return (-1);
1078 }
1079