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