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