xref: /illumos-gate/usr/src/uts/common/io/strplumb.c (revision f48205be)
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 	 * 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 	/*
632 	 * Setup the TCP and SCTP default queues for the global stack.
633 	 * tcp/sctp_stack_init will do this for additional stack instances.
634 	 */
635 	if ((err = strplumb_sctpq(li)) != 0)
636 		goto done;
637 
638 	if ((err = strplumb_tcpq(li)) != 0)
639 		goto done;
640 
641 	if ((err = resolve_boot_path()) != 0)
642 		goto done;
643 
644 	DBG1("rootfs.bo_devname: %s\n", rootfs.bo_devname);
645 	DBG1("rootfs.bo_ifname: %s\n", rootfs.bo_ifname);
646 	DBG1("rootfs.bo_ppa: %d\n", rootfs.bo_ppa);
647 
648 	if ((err = strplumb_dev(li)) != 0)
649 		goto done;
650 
651 done:
652 	ldi_ident_release(li);
653 
654 	return (err);
655 }
656 
657 /* multiboot:  diskless boot interface discovery */
658 
659 #ifndef	__sparc
660 
661 static uchar_t boot_macaddr[16];
662 static int boot_maclen;
663 static uchar_t *getmacaddr(dev_info_t *dip, int *maclen);
664 static int matchmac(dev_info_t *dip, void *arg);
665 int dl_attach(ldi_handle_t lh, int unit);
666 int dl_bind(ldi_handle_t lh, uint_t sap, uint_t max_conn,
667     uint_t service, uint_t conn_mgmt);
668 int dl_phys_addr(ldi_handle_t lh, struct ether_addr *eaddr);
669 
670 #endif  /* !__sparc */
671 
672 char *
673 strplumb_get_netdev_path(void)
674 {
675 #ifndef	__sparc
676 	char *macstr, *devpath = NULL;
677 	uchar_t *bootp;
678 	uint_t bootp_len, len;
679 
680 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
681 	    DDI_PROP_DONTPASS, BP_BOOT_MAC, &macstr) == DDI_SUCCESS) {
682 		/*
683 		 * hard coded ether mac len for booting floppy on
684 		 * machines with old cards
685 		 */
686 		boot_maclen = ether_aton(macstr, boot_macaddr);
687 		if (boot_maclen != 6) {
688 			cmn_err(CE_WARN,
689 			    "malformed boot_mac property, %d bytes",
690 			    boot_maclen);
691 		}
692 		ddi_prop_free(macstr);
693 	} else if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, ddi_root_node(),
694 	    DDI_PROP_DONTPASS, BP_BOOTP_RESPONSE, &bootp, &bootp_len)
695 	    == DDI_SUCCESS) {
696 
697 		/*
698 		 * These offsets are defined by dhcp standard
699 		 * Should use structure offsets
700 		 */
701 		boot_maclen = *(bootp + 2);
702 		ASSERT(boot_maclen <= 16);
703 		(void) bcopy(bootp + 28, boot_macaddr, boot_maclen);
704 
705 		/* encode to ascii string to match what sparc OBP exports */
706 		dhcack = kmem_zalloc(bootp_len * 2 + IFNAMSIZ + 2, KM_SLEEP);
707 		(void) octet_to_hexascii(bootp, bootp_len, dhcack + IFNAMSIZ,
708 		    &len);
709 		ASSERT(len < bootp_len * 2 + 2);
710 		ddi_prop_free(bootp);
711 	} else
712 		return (NULL);
713 
714 	ddi_walk_devs(ddi_root_node(), matchmac, (void *)&devpath);
715 	return (devpath);
716 
717 #else
718 	return (NULL);
719 #endif  /* !__sparc */
720 }
721 
722 #ifndef __sparc
723 
724 /*
725  * Get boot path from the boot_mac address
726  */
727 /*ARGSUSED*/
728 static int
729 matchmac(dev_info_t *dip, void *arg)
730 {
731 	char **devpathp = (char **)arg;
732 	char *model_str;
733 	uchar_t *macaddr;
734 	int maclen;
735 
736 	/* XXX Should use "device-type" per IEEE 1275 */
737 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, 0,
738 	    "model", &model_str) != DDI_SUCCESS)
739 		return (DDI_WALK_CONTINUE);
740 
741 	if (strcmp(model_str, "Ethernet controller") != 0) {
742 		ddi_prop_free(model_str);
743 		return (DDI_WALK_CONTINUE);
744 	}
745 	ddi_prop_free(model_str);
746 
747 	/* We have a network device now */
748 	if (i_ddi_attach_node_hierarchy(dip) != DDI_SUCCESS) {
749 		return (DDI_WALK_CONTINUE);
750 	}
751 
752 	ASSERT(boot_maclen != 0);
753 	macaddr = getmacaddr(dip, &maclen);
754 	if (macaddr == NULL)
755 		return (DDI_WALK_CONTINUE);
756 
757 	if (maclen != boot_maclen ||
758 	    bcmp(macaddr, boot_macaddr, maclen) != 0) {
759 		kmem_free(macaddr, maclen);
760 		return (DDI_WALK_CONTINUE);
761 	}
762 
763 	/* found hardware with the mac address */
764 	(void) localetheraddr((struct ether_addr *)macaddr, NULL);
765 	kmem_free(macaddr, maclen);
766 
767 	*devpathp = kmem_alloc(MAXPATHLEN, KM_SLEEP);
768 	(void) ddi_pathname(dip, *devpathp);
769 
770 	/* fill in the name portion of dhcack */
771 	if (dhcack)
772 		(void) snprintf(dhcack, IFNAMSIZ, "%s%d",
773 		    ddi_driver_name(dip), i_ddi_devi_get_ppa(dip));
774 	return (DDI_WALK_TERMINATE);
775 }
776 
777 static uchar_t *
778 getmacaddr_gldv3(char *drv, int inst, int *maclenp)
779 {
780 	char ifname[16];
781 	mac_handle_t mh;
782 	uchar_t *macaddr;
783 
784 	(void) snprintf(ifname, sizeof (ifname), "%s%d", drv, inst);
785 	if (mac_open(ifname, inst, &mh) < 0) {
786 		return (NULL);
787 	}
788 	*maclenp = sizeof (struct ether_addr);
789 	macaddr = kmem_alloc(*maclenp, KM_SLEEP);
790 	mac_unicst_get(mh, macaddr);
791 	mac_close(mh);
792 
793 	return (macaddr);
794 }
795 
796 static uchar_t *
797 getmacaddr(dev_info_t *dip, int *maclenp)
798 {
799 	int rc, ppa;
800 	ldi_ident_t li;
801 	ldi_handle_t lh;
802 	char *drv_name = (char *)ddi_driver_name(dip);
803 	char *clonepath;
804 	uchar_t *macaddr = NULL;
805 
806 	/* a simpler way to get mac address for GLDv3 drivers */
807 	if (GLDV3_DRV(ddi_name_to_major(drv_name))) {
808 		return (getmacaddr_gldv3(drv_name, ddi_get_instance(dip),
809 		    maclenp));
810 	}
811 
812 	if (rc = ldi_ident_from_mod(&modlinkage, &li)) {
813 		cmn_err(CE_WARN,
814 		    "getmacaddr: ldi_ident_from_mod failed: %d\n", rc);
815 		return (NULL);
816 	}
817 
818 	clonepath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
819 	(void) snprintf(clonepath, MAXPATHLEN,
820 	    "/devices/pseudo/clone@0:%s", drv_name);
821 
822 	rc = ldi_open_by_name(clonepath, FREAD|FWRITE, CRED(), &lh, li);
823 	ldi_ident_release(li);
824 	if (rc) {
825 		cmn_err(CE_WARN,
826 		    "getmacaddr: ldi_open_by_name(%s) failed: %d\n",
827 		    clonepath, rc);
828 		kmem_free(clonepath, MAXPATHLEN);
829 		return (NULL);
830 	}
831 	kmem_free(clonepath, MAXPATHLEN);
832 
833 	ppa = i_ddi_devi_get_ppa(dip);
834 	if ((dl_attach(lh, ppa) != 0) ||
835 	    (dl_bind(lh, ETHERTYPE_IP, 0, DL_CLDLS, 0) != 0)) {
836 		(void) ldi_close(lh, FREAD|FWRITE, CRED());
837 		cmn_err(CE_WARN,
838 		    "getmacaddr: dl_attach/bind(%s%d) failed: %d\n",
839 		    drv_name, ppa, rc);
840 		return (NULL);
841 	}
842 	*maclenp = sizeof (struct ether_addr);
843 	macaddr = kmem_alloc(*maclenp, KM_SLEEP);
844 	if (dl_phys_addr(lh, (struct ether_addr *)macaddr) != 0) {
845 		kmem_free(macaddr, *maclenp);
846 		macaddr = NULL;
847 		*maclenp = 0;
848 		cmn_err(CE_WARN,
849 		    "getmacaddr: dl_macaddr(%s%d) failed: %d\n",
850 		    drv_name, ppa, rc);
851 	}
852 	(void) ldi_close(lh, FREAD|FWRITE, CRED());
853 	return (macaddr);
854 }
855 
856 #endif	/* !__sparc */
857 
858 int
859 dl_attach(ldi_handle_t lh, int unit)
860 {
861 	dl_attach_req_t *attach_req;
862 	dl_error_ack_t *error_ack;
863 	union DL_primitives *dl_prim;
864 	mblk_t *mp;
865 	int error;
866 
867 	if ((mp = allocb(sizeof (dl_attach_req_t), BPRI_MED)) == NULL) {
868 		cmn_err(CE_WARN, "dl_attach: allocb failed");
869 		return (ENOSR);
870 	}
871 	mp->b_datap->db_type = M_PROTO;
872 	mp->b_wptr += sizeof (dl_attach_req_t);
873 
874 	attach_req = (dl_attach_req_t *)mp->b_rptr;
875 	attach_req->dl_primitive = DL_ATTACH_REQ;
876 	attach_req->dl_ppa = unit;
877 
878 	(void) ldi_putmsg(lh, mp);
879 	if ((error = ldi_getmsg(lh, &mp, (timestruc_t *)NULL)) != 0) {
880 		printf("dl_attach: ldi_getmsg failed: %d\n", error);
881 		return (error);
882 	}
883 
884 	dl_prim = (union DL_primitives *)mp->b_rptr;
885 	switch (dl_prim->dl_primitive) {
886 	case DL_OK_ACK:
887 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_ok_ack_t)) {
888 			printf("dl_attach: DL_OK_ACK protocol error\n");
889 			break;
890 		}
891 		if (((dl_ok_ack_t *)dl_prim)->dl_correct_primitive !=
892 		    DL_ATTACH_REQ) {
893 			printf("dl_attach: DL_OK_ACK rtnd prim %u\n",
894 			    ((dl_ok_ack_t *)dl_prim)->dl_correct_primitive);
895 			break;
896 		}
897 		freemsg(mp);
898 		return (0);
899 
900 	case DL_ERROR_ACK:
901 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_error_ack_t)) {
902 			printf("dl_attach: DL_ERROR_ACK protocol error\n");
903 			break;
904 		}
905 
906 		error_ack = (dl_error_ack_t *)dl_prim;
907 		switch (error_ack->dl_errno) {
908 		case DL_BADPPA:
909 			printf("dl_attach: DL_ERROR_ACK bad PPA\n");
910 			break;
911 
912 		case DL_ACCESS:
913 			printf("dl_attach: DL_ERROR_ACK access error\n");
914 			break;
915 
916 		default:
917 			printf("dl_attach: DLPI error %u\n",
918 			    error_ack->dl_errno);
919 			break;
920 		}
921 		break;
922 
923 	default:
924 		printf("dl_attach: bad ACK header %u\n", dl_prim->dl_primitive);
925 		break;
926 	}
927 
928 	/*
929 	 * Error return only.
930 	 */
931 	freemsg(mp);
932 	return (-1);
933 }
934 
935 int
936 dl_bind(ldi_handle_t lh, uint_t sap, uint_t max_conn, uint_t service,
937 	uint_t conn_mgmt)
938 {
939 	dl_bind_req_t *bind_req;
940 	dl_error_ack_t *error_ack;
941 	union DL_primitives *dl_prim;
942 	mblk_t *mp;
943 	int error;
944 
945 	if ((mp = allocb(sizeof (dl_bind_req_t), BPRI_MED)) == NULL) {
946 		cmn_err(CE_WARN, "dl_bind: allocb failed");
947 		return (ENOSR);
948 	}
949 	mp->b_datap->db_type = M_PROTO;
950 
951 	bind_req = (dl_bind_req_t *)mp->b_wptr;
952 	mp->b_wptr += sizeof (dl_bind_req_t);
953 	bind_req->dl_primitive = DL_BIND_REQ;
954 	bind_req->dl_sap = sap;
955 	bind_req->dl_max_conind = max_conn;
956 	bind_req->dl_service_mode = service;
957 	bind_req->dl_conn_mgmt = conn_mgmt;
958 	bind_req->dl_xidtest_flg = 0;
959 
960 	(void) ldi_putmsg(lh, mp);
961 	if ((error = ldi_getmsg(lh, &mp, (timestruc_t *)NULL)) != 0) {
962 		printf("dl_bind: ldi_getmsg failed: %d\n", error);
963 		return (error);
964 	}
965 
966 	dl_prim = (union DL_primitives *)mp->b_rptr;
967 	switch (dl_prim->dl_primitive) {
968 	case DL_BIND_ACK:
969 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_bind_ack_t)) {
970 			printf("dl_bind: DL_BIND_ACK protocol error\n");
971 			break;
972 		}
973 		if (((dl_bind_ack_t *)dl_prim)->dl_sap != sap) {
974 			printf("dl_bind: DL_BIND_ACK bad sap %u\n",
975 			    ((dl_bind_ack_t *)dl_prim)->dl_sap);
976 			break;
977 		}
978 		freemsg(mp);
979 		return (0);
980 
981 	case DL_ERROR_ACK:
982 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_error_ack_t)) {
983 			printf("dl_bind: DL_ERROR_ACK protocol error\n");
984 			break;
985 		}
986 
987 		error_ack = (dl_error_ack_t *)dl_prim;
988 		printf("dl_bind: DLPI error %u\n", error_ack->dl_errno);
989 		break;
990 
991 	default:
992 		printf("dl_bind: bad ACK header %u\n", dl_prim->dl_primitive);
993 		break;
994 	}
995 
996 	/*
997 	 * Error return only.
998 	 */
999 	freemsg(mp);
1000 	return (-1);
1001 }
1002 
1003 int
1004 dl_phys_addr(ldi_handle_t lh, struct ether_addr *eaddr)
1005 {
1006 	dl_phys_addr_req_t *phys_addr_req;
1007 	dl_phys_addr_ack_t *phys_addr_ack;
1008 	dl_error_ack_t *error_ack;
1009 	union DL_primitives *dl_prim;
1010 	mblk_t *mp;
1011 	int error;
1012 	uchar_t *addrp;
1013 	timestruc_t tv;
1014 
1015 	if ((mp = allocb(sizeof (dl_phys_addr_req_t), BPRI_MED)) ==
1016 	    (mblk_t *)NULL) {
1017 		cmn_err(CE_WARN, "dl_phys_addr: allocb failed");
1018 		return (ENOSR);
1019 	}
1020 	mp->b_datap->db_type = M_PROTO;
1021 	mp->b_wptr += sizeof (dl_phys_addr_req_t);
1022 
1023 	phys_addr_req = (dl_phys_addr_req_t *)mp->b_rptr;
1024 	phys_addr_req->dl_primitive = DL_PHYS_ADDR_REQ;
1025 	phys_addr_req->dl_addr_type = DL_CURR_PHYS_ADDR;
1026 
1027 	/*
1028 	 * In case some provider doesn't implement or nack the
1029 	 * request just wait for 15 seconds.
1030 	 */
1031 	tv.tv_sec = 15;
1032 	tv.tv_nsec = 0;
1033 
1034 	(void) ldi_putmsg(lh, mp);
1035 	error = ldi_getmsg(lh, &mp, &tv);
1036 	if (error == ETIME) {
1037 		printf("dl_phys_addr: timed out\n");
1038 		return (-1);
1039 	} else if (error != 0) {
1040 		printf("dl_phys_addr: ldi_getmsg failed: %d\n", error);
1041 		return (error);
1042 	}
1043 
1044 	dl_prim = (union DL_primitives *)mp->b_rptr;
1045 	switch (dl_prim->dl_primitive) {
1046 	case DL_PHYS_ADDR_ACK:
1047 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_phys_addr_ack_t)) {
1048 			printf("dl_phys_addr: "
1049 				"DL_PHYS_ADDR_ACK protocol error\n");
1050 			break;
1051 		}
1052 		phys_addr_ack = &dl_prim->physaddr_ack;
1053 		if (phys_addr_ack->dl_addr_length != sizeof (*eaddr)) {
1054 			printf("dl_phys_addr: DL_PHYS_ADDR_ACK bad len %u\n",
1055 				phys_addr_ack->dl_addr_length);
1056 			break;
1057 		}
1058 		if (phys_addr_ack->dl_addr_length +
1059 		    phys_addr_ack->dl_addr_offset > (mp->b_wptr-mp->b_rptr)) {
1060 			printf("dl_phys_addr: DL_PHYS_ADDR_ACK bad len %u\n",
1061 				phys_addr_ack->dl_addr_length);
1062 			break;
1063 		}
1064 		addrp = mp->b_rptr + phys_addr_ack->dl_addr_offset;
1065 		bcopy(addrp, eaddr, sizeof (*eaddr));
1066 		freemsg(mp);
1067 		return (0);
1068 
1069 	case DL_ERROR_ACK:
1070 		if ((mp->b_wptr-mp->b_rptr) < sizeof (dl_error_ack_t)) {
1071 			printf("dl_phys_addr: DL_ERROR_ACK protocol error\n");
1072 			break;
1073 		}
1074 
1075 		error_ack = (dl_error_ack_t *)dl_prim;
1076 		printf("dl_phys_addr: DLPI error %u\n",
1077 		    error_ack->dl_errno);
1078 		break;
1079 
1080 	default:
1081 		printf("dl_phys_addr: bad ACK header %u\n",
1082 			dl_prim->dl_primitive);
1083 		break;
1084 	}
1085 
1086 	/*
1087 	 * Error return only.
1088 	 */
1089 	freemsg(mp);
1090 	return (-1);
1091 }
1092