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