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