148}; 149 150static int 151netopen(dev_t dev, int flag, int mode, struct thread *td) 152{ 153 return (0); 154} 155 156static int 157netclose(dev_t dev, int flags, int fmt, struct thread *td) 158{ 159 return (0); 160} 161 162static int 163netioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td) 164{ 165 struct ifnet *ifp; 166 int error, idx; 167 168 /* only support interface specific ioctls */ 169 if (IOCGROUP(cmd) != 'i') 170 return (EOPNOTSUPP); 171 idx = minor(dev); 172 if (idx == 0) { 173 /* 174 * special network device, not interface. 175 */ 176 if (cmd == SIOCGIFCONF) 177 return (ifconf(cmd, data)); /* XXX remove cmd */ 178 return (EOPNOTSUPP); 179 } 180 181 ifp = ifnet_byindex(idx); 182 if (ifp == NULL) 183 return (ENXIO); 184 185 error = ifhwioctl(cmd, ifp, data, td); 186 if (error == ENOIOCTL) 187 error = EOPNOTSUPP; 188 return (error); 189} 190 191static int 192netkqfilter(dev_t dev, struct knote *kn) 193{ 194 struct klist *klist; 195 struct ifnet *ifp; 196 int idx; 197 198 idx = minor(dev); 199 if (idx == 0) { 200 klist = &ifklist; 201 } else { 202 ifp = ifnet_byindex(idx); 203 if (ifp == NULL) 204 return (1); 205 klist = &ifp->if_klist; 206 } 207 208 switch (kn->kn_filter) { 209 case EVFILT_NETDEV: 210 kn->kn_fop = &netdev_filtops; 211 break; 212 default: 213 return (1); 214 } 215 216 kn->kn_hook = (caddr_t)klist; 217 218 /* XXX locking? */ 219 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 220 221 return (0); 222} 223 224static void 225filt_netdetach(struct knote *kn) 226{ 227 struct klist *klist = (struct klist *)kn->kn_hook; 228 229 if (kn->kn_status & KN_DETACHED) 230 return; 231 SLIST_REMOVE(klist, kn, knote, kn_selnext); 232} 233 234static int 235filt_netdev(struct knote *kn, long hint) 236{ 237 238 /* 239 * Currently NOTE_EXIT is abused to indicate device detach. 240 */ 241 if (hint == NOTE_EXIT) { 242 kn->kn_data = NOTE_LINKINV; 243 kn->kn_status |= KN_DETACHED; 244 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 245 return (1); 246 } 247 kn->kn_data = hint; /* current status */ 248 if (kn->kn_sfflags & hint) 249 kn->kn_fflags |= hint; 250 return (kn->kn_fflags != 0); 251} 252 253/* 254 * Network interface utility routines. 255 * 256 * Routines with ifa_ifwith* names take sockaddr *'s as 257 * parameters. 258 */ 259/* ARGSUSED*/ 260static void 261if_init(dummy) 262 void *dummy; 263{ 264 265 IFNET_LOCK_INIT(); 266 TAILQ_INIT(&ifnet); 267 SLIST_INIT(&ifklist); 268 if_grow(); /* create initial table */ 269 ifdev_byindex(0) = make_dev(&net_cdevsw, 0, 270 UID_ROOT, GID_WHEEL, 0600, "network"); 271} 272 273static void 274if_grow(void) 275{ 276 u_int n; 277 struct ifindex_entry *e; 278 279 if_indexlim <<= 1; 280 n = if_indexlim * sizeof(*e); 281 e = malloc(n, M_IFADDR, M_WAITOK | M_ZERO); 282 if (ifindex_table != NULL) { 283 memcpy((caddr_t)e, (caddr_t)ifindex_table, n/2); 284 free((caddr_t)ifindex_table, M_IFADDR); 285 } 286 ifindex_table = e; 287} 288 289/* ARGSUSED*/ 290static void 291if_check(dummy) 292 void *dummy; 293{ 294 struct ifnet *ifp; 295 int s; 296 297 s = splimp(); 298 IFNET_RLOCK(); /* could sleep on rare error; mostly okay XXX */ 299 TAILQ_FOREACH(ifp, &ifnet, if_link) { 300 if (ifp->if_snd.ifq_maxlen == 0) { 301 printf("%s%d XXX: driver didn't set ifq_maxlen\n", 302 ifp->if_name, ifp->if_unit); 303 ifp->if_snd.ifq_maxlen = ifqmaxlen; 304 } 305 if (!mtx_initialized(&ifp->if_snd.ifq_mtx)) { 306 printf("%s%d XXX: driver didn't initialize queue mtx\n", 307 ifp->if_name, ifp->if_unit); 308 mtx_init(&ifp->if_snd.ifq_mtx, "unknown", 309 MTX_NETWORK_LOCK, MTX_DEF); 310 } 311 } 312 IFNET_RUNLOCK(); 313 splx(s); 314 if_slowtimo(0); 315} 316 317static int 318if_findindex(struct ifnet *ifp) 319{ 320 int i, unit; 321 char eaddr[18], devname[32]; 322 const char *name, *p; 323 324 switch (ifp->if_type) { 325 case IFT_ETHER: /* these types use struct arpcom */ 326 case IFT_FDDI: 327 case IFT_XETHER: 328 case IFT_ISO88025: 329 case IFT_L2VLAN: 330 snprintf(eaddr, 18, "%6D", 331 ((struct arpcom *)ifp->if_softc)->ac_enaddr, ":"); 332 break; 333 default: 334 eaddr[0] = '\0'; 335 break; 336 } 337 snprintf(devname, 32, "%s%d", ifp->if_name, ifp->if_unit); 338 name = net_cdevsw.d_name; 339 i = 0; 340 while ((resource_find_dev(&i, name, &unit, NULL, NULL)) == 0) { 341 if (resource_string_value(name, unit, "ether", &p) == 0) 342 if (strcmp(p, eaddr) == 0) 343 goto found; 344 if (resource_string_value(name, unit, "dev", &p) == 0) 345 if (strcmp(p, devname) == 0) 346 goto found; 347 } 348 unit = 0; 349found: 350 if (unit != 0) { 351 if (ifaddr_byindex(unit) == NULL) 352 return (unit); 353 printf("%s%d in use, cannot hardwire it to %s.\n", 354 name, unit, devname); 355 } 356 for (unit = 1; ; unit++) { 357 if (unit <= if_index && ifaddr_byindex(unit) != NULL) 358 continue; 359 if (resource_string_value(name, unit, "ether", &p) == 0 || 360 resource_string_value(name, unit, "dev", &p) == 0) 361 continue; 362 break; 363 } 364 return (unit); 365} 366 367/* 368 * Attach an interface to the 369 * list of "active" interfaces. 370 */ 371void 372if_attach(ifp) 373 struct ifnet *ifp; 374{ 375 unsigned socksize, ifasize; 376 int namelen, masklen; 377 char workbuf[64]; 378 register struct sockaddr_dl *sdl; 379 register struct ifaddr *ifa; 380 381 IFNET_WLOCK(); 382 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link); 383 IFNET_WUNLOCK(); 384 /* 385 * XXX - 386 * The old code would work if the interface passed a pre-existing 387 * chain of ifaddrs to this code. We don't trust our callers to 388 * properly initialize the tailq, however, so we no longer allow 389 * this unlikely case. 390 */ 391 TAILQ_INIT(&ifp->if_addrhead); 392 TAILQ_INIT(&ifp->if_prefixhead); 393 TAILQ_INIT(&ifp->if_multiaddrs); 394 SLIST_INIT(&ifp->if_klist); 395 getmicrotime(&ifp->if_lastchange); 396 397#ifdef MAC 398 mac_init_ifnet(ifp); 399 mac_create_ifnet(ifp); 400#endif 401 402 ifp->if_index = if_findindex(ifp); 403 if (ifp->if_index > if_index) 404 if_index = ifp->if_index; 405 if (if_index >= if_indexlim) 406 if_grow(); 407 408 ifnet_byindex(ifp->if_index) = ifp; 409 ifdev_byindex(ifp->if_index) = make_dev(&net_cdevsw, ifp->if_index, 410 UID_ROOT, GID_WHEEL, 0600, "%s/%s%d", 411 net_cdevsw.d_name, ifp->if_name, ifp->if_unit); 412 make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d", 413 net_cdevsw.d_name, ifp->if_index); 414 415 mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_name, "if send queue", MTX_DEF); 416 417 /* 418 * create a Link Level name for this device 419 */ 420 namelen = snprintf(workbuf, sizeof(workbuf), 421 "%s%d", ifp->if_name, ifp->if_unit); 422#define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m)) 423 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen; 424 socksize = masklen + ifp->if_addrlen; 425#define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1))) 426 if (socksize < sizeof(*sdl)) 427 socksize = sizeof(*sdl); 428 socksize = ROUNDUP(socksize); 429 ifasize = sizeof(*ifa) + 2 * socksize; 430 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO); 431 if (ifa) { 432 IFA_LOCK_INIT(ifa); 433 sdl = (struct sockaddr_dl *)(ifa + 1); 434 sdl->sdl_len = socksize; 435 sdl->sdl_family = AF_LINK; 436 bcopy(workbuf, sdl->sdl_data, namelen); 437 sdl->sdl_nlen = namelen; 438 sdl->sdl_index = ifp->if_index; 439 sdl->sdl_type = ifp->if_type; 440 ifaddr_byindex(ifp->if_index) = ifa; 441 ifa->ifa_ifp = ifp; 442 ifa->ifa_rtrequest = link_rtrequest; 443 ifa->ifa_addr = (struct sockaddr *)sdl; 444 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 445 ifa->ifa_netmask = (struct sockaddr *)sdl; 446 sdl->sdl_len = masklen; 447 while (namelen != 0) 448 sdl->sdl_data[--namelen] = 0xff; 449 ifa->ifa_refcnt = 1; 450 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link); 451 } 452 ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */ 453 454 /* Announce the interface. */ 455 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 456} 457 458/* 459 * Detach an interface, removing it from the 460 * list of "active" interfaces. 461 */ 462void 463if_detach(ifp) 464 struct ifnet *ifp; 465{ 466 struct ifaddr *ifa; 467 struct radix_node_head *rnh; 468 int s; 469 int i; 470 471 /* 472 * Remove routes and flush queues. 473 */ 474 s = splnet(); 475 if_down(ifp); 476 477 /* 478 * Remove address from ifindex_table[] and maybe decrement if_index. 479 * Clean up all addresses. 480 */ 481 ifaddr_byindex(ifp->if_index) = NULL; 482 revoke_and_destroy_dev(ifdev_byindex(ifp->if_index)); 483 ifdev_byindex(ifp->if_index) = NULL; 484 485 while (if_index > 0 && ifaddr_byindex(if_index) == NULL) 486 if_index--; 487 488 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa; 489 ifa = TAILQ_FIRST(&ifp->if_addrhead)) { 490#ifdef INET 491 /* XXX: Ugly!! ad hoc just for INET */ 492 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { 493 struct ifaliasreq ifr; 494 495 bzero(&ifr, sizeof(ifr)); 496 ifr.ifra_addr = *ifa->ifa_addr; 497 if (ifa->ifa_dstaddr) 498 ifr.ifra_broadaddr = *ifa->ifa_dstaddr; 499 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp, 500 NULL) == 0) 501 continue; 502 } 503#endif /* INET */ 504#ifdef INET6 505 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) { 506 in6_purgeaddr(ifa); 507 /* ifp_addrhead is already updated */ 508 continue; 509 } 510#endif /* INET6 */ 511 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 512 IFAFREE(ifa); 513 } 514 515#ifdef INET6 516 /* 517 * Remove all IPv6 kernel structs related to ifp. This should be done 518 * before removing routing entries below, since IPv6 interface direct 519 * routes are expected to be removed by the IPv6-specific kernel API. 520 * Otherwise, the kernel will detect some inconsistency and bark it. 521 */ 522 in6_ifdetach(ifp); 523#endif 524 525 /* 526 * Delete all remaining routes using this interface 527 * Unfortuneatly the only way to do this is to slog through 528 * the entire routing table looking for routes which point 529 * to this interface...oh well... 530 */ 531 for (i = 1; i <= AF_MAX; i++) { 532 if ((rnh = rt_tables[i]) == NULL) 533 continue; 534 RADIX_NODE_HEAD_LOCK(rnh); 535 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp); 536 RADIX_NODE_HEAD_UNLOCK(rnh); 537 } 538 539 /* Announce that the interface is gone. */ 540 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 541 542#ifdef MAC 543 mac_destroy_ifnet(ifp); 544#endif /* MAC */ 545 KNOTE(&ifp->if_klist, NOTE_EXIT); 546 IFNET_WLOCK(); 547 TAILQ_REMOVE(&ifnet, ifp, if_link); 548 IFNET_WUNLOCK(); 549 mtx_destroy(&ifp->if_snd.ifq_mtx); 550 splx(s); 551} 552 553/* 554 * Delete Routes for a Network Interface 555 * 556 * Called for each routing entry via the rnh->rnh_walktree() call above 557 * to delete all route entries referencing a detaching network interface. 558 * 559 * Arguments: 560 * rn pointer to node in the routing table 561 * arg argument passed to rnh->rnh_walktree() - detaching interface 562 * 563 * Returns: 564 * 0 successful 565 * errno failed - reason indicated 566 * 567 */ 568static int 569if_rtdel(rn, arg) 570 struct radix_node *rn; 571 void *arg; 572{ 573 struct rtentry *rt = (struct rtentry *)rn; 574 struct ifnet *ifp = arg; 575 int err; 576 577 if (rt->rt_ifp == ifp) { 578 579 /* 580 * Protect (sorta) against walktree recursion problems 581 * with cloned routes 582 */ 583 if ((rt->rt_flags & RTF_UP) == 0) 584 return (0); 585 586 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, 587 rt_mask(rt), rt->rt_flags, 588 (struct rtentry **) NULL); 589 if (err) { 590 log(LOG_WARNING, "if_rtdel: error %d\n", err); 591 } 592 } 593 594 return (0); 595} 596 597/* 598 * Create a clone network interface. 599 */ 600int 601if_clone_create(name, len) 602 char *name; 603 int len; 604{ 605 struct if_clone *ifc; 606 char *dp; 607 int wildcard, bytoff, bitoff; 608 int unit; 609 int err; 610 611 ifc = if_clone_lookup(name, &unit); 612 if (ifc == NULL) 613 return (EINVAL); 614 615 if (ifunit(name) != NULL) 616 return (EEXIST); 617 618 bytoff = bitoff = 0; 619 wildcard = (unit < 0); 620 /* 621 * Find a free unit if none was given. 622 */ 623 if (wildcard) { 624 while ((bytoff < ifc->ifc_bmlen) 625 && (ifc->ifc_units[bytoff] == 0xff)) 626 bytoff++; 627 if (bytoff >= ifc->ifc_bmlen) 628 return (ENOSPC); 629 while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0) 630 bitoff++; 631 unit = (bytoff << 3) + bitoff; 632 } 633 634 if (unit > ifc->ifc_maxunit) 635 return (ENXIO); 636 637 err = (*ifc->ifc_create)(ifc, unit); 638 if (err != 0) 639 return (err); 640 641 if (!wildcard) { 642 bytoff = unit >> 3; 643 bitoff = unit - (bytoff << 3); 644 } 645 646 /* 647 * Allocate the unit in the bitmap. 648 */ 649 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0, 650 ("%s: bit is already set", __func__)); 651 ifc->ifc_units[bytoff] |= (1 << bitoff); 652 653 /* In the wildcard case, we need to update the name. */ 654 if (wildcard) { 655 for (dp = name; *dp != '\0'; dp++); 656 if (snprintf(dp, len - (dp-name), "%d", unit) > 657 len - (dp-name) - 1) { 658 /* 659 * This can only be a programmer error and 660 * there's no straightforward way to recover if 661 * it happens. 662 */ 663 panic("if_clone_create(): interface name too long"); 664 } 665 666 } 667 668 return (0); 669} 670 671/* 672 * Destroy a clone network interface. 673 */ 674int 675if_clone_destroy(name) 676 const char *name; 677{ 678 struct if_clone *ifc; 679 struct ifnet *ifp; 680 int bytoff, bitoff; 681 int unit; 682 683 ifc = if_clone_lookup(name, &unit); 684 if (ifc == NULL) 685 return (EINVAL); 686 687 if (unit < ifc->ifc_minifs) 688 return (EINVAL); 689 690 ifp = ifunit(name); 691 if (ifp == NULL) 692 return (ENXIO); 693 694 if (ifc->ifc_destroy == NULL) 695 return (EOPNOTSUPP); 696 697 (*ifc->ifc_destroy)(ifp); 698 699 /* 700 * Compute offset in the bitmap and deallocate the unit. 701 */ 702 bytoff = unit >> 3; 703 bitoff = unit - (bytoff << 3); 704 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0, 705 ("%s: bit is already cleared", __func__)); 706 ifc->ifc_units[bytoff] &= ~(1 << bitoff); 707 return (0); 708} 709 710/* 711 * Look up a network interface cloner. 712 */ 713static struct if_clone * 714if_clone_lookup(name, unitp) 715 const char *name; 716 int *unitp; 717{ 718 struct if_clone *ifc; 719 const char *cp; 720 int i; 721 722 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) { 723 for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) { 724 if (ifc->ifc_name[i] != *cp) 725 goto next_ifc; 726 } 727 goto found_name; 728 next_ifc: 729 ifc = LIST_NEXT(ifc, ifc_list); 730 } 731 732 /* No match. */ 733 return ((struct if_clone *)NULL); 734 735 found_name: 736 if (*cp == '\0') { 737 i = -1; 738 } else { 739 for (i = 0; *cp != '\0'; cp++) { 740 if (*cp < '0' || *cp > '9') { 741 /* Bogus unit number. */ 742 return (NULL); 743 } 744 i = (i * 10) + (*cp - '0'); 745 } 746 } 747 748 if (unitp != NULL) 749 *unitp = i; 750 return (ifc); 751} 752 753/* 754 * Register a network interface cloner. 755 */ 756void 757if_clone_attach(ifc) 758 struct if_clone *ifc; 759{ 760 int bytoff, bitoff; 761 int err; 762 int len, maxclone; 763 int unit; 764 765 KASSERT(ifc->ifc_minifs - 1 <= ifc->ifc_maxunit, 766 ("%s: %s requested more units then allowed (%d > %d)", 767 __func__, ifc->ifc_name, ifc->ifc_minifs, 768 ifc->ifc_maxunit + 1)); 769 /* 770 * Compute bitmap size and allocate it. 771 */ 772 maxclone = ifc->ifc_maxunit + 1; 773 len = maxclone >> 3; 774 if ((len << 3) < maxclone) 775 len++; 776 ifc->ifc_units = malloc(len, M_CLONE, M_WAITOK | M_ZERO); 777 ifc->ifc_bmlen = len; 778 779 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list); 780 if_cloners_count++; 781 782 for (unit = 0; unit < ifc->ifc_minifs; unit++) { 783 err = (*ifc->ifc_create)(ifc, unit); 784 KASSERT(err == 0, 785 ("%s: failed to create required interface %s%d", 786 __func__, ifc->ifc_name, unit)); 787 788 /* Allocate the unit in the bitmap. */ 789 bytoff = unit >> 3; 790 bitoff = unit - (bytoff << 3); 791 ifc->ifc_units[bytoff] |= (1 << bitoff); 792 } 793} 794 795/* 796 * Unregister a network interface cloner. 797 */ 798void 799if_clone_detach(ifc) 800 struct if_clone *ifc; 801{ 802 803 LIST_REMOVE(ifc, ifc_list); 804 free(ifc->ifc_units, M_CLONE); 805 if_cloners_count--; 806} 807 808/* 809 * Provide list of interface cloners to userspace. 810 */ 811static int 812if_clone_list(ifcr) 813 struct if_clonereq *ifcr; 814{ 815 char outbuf[IFNAMSIZ], *dst; 816 struct if_clone *ifc; 817 int count, error = 0; 818 819 ifcr->ifcr_total = if_cloners_count; 820 if ((dst = ifcr->ifcr_buffer) == NULL) { 821 /* Just asking how many there are. */ 822 return (0); 823 } 824 825 if (ifcr->ifcr_count < 0) 826 return (EINVAL); 827 828 count = (if_cloners_count < ifcr->ifcr_count) ? 829 if_cloners_count : ifcr->ifcr_count; 830 831 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0; 832 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) { 833 strncpy(outbuf, ifc->ifc_name, IFNAMSIZ); 834 outbuf[IFNAMSIZ - 1] = '\0'; /* sanity */ 835 error = copyout(outbuf, dst, IFNAMSIZ); 836 if (error) 837 break; 838 } 839 840 return (error); 841} 842 843#define equal(a1, a2) \ 844 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0) 845 846/* 847 * Locate an interface based on a complete address. 848 */ 849/*ARGSUSED*/ 850struct ifaddr * 851ifa_ifwithaddr(addr) 852 struct sockaddr *addr; 853{ 854 struct ifnet *ifp; 855 struct ifaddr *ifa; 856 857 IFNET_RLOCK(); 858 TAILQ_FOREACH(ifp, &ifnet, if_link) 859 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 860 if (ifa->ifa_addr->sa_family != addr->sa_family) 861 continue; 862 if (equal(addr, ifa->ifa_addr)) 863 goto done; 864 /* IP6 doesn't have broadcast */ 865 if ((ifp->if_flags & IFF_BROADCAST) && 866 ifa->ifa_broadaddr && 867 ifa->ifa_broadaddr->sa_len != 0 && 868 equal(ifa->ifa_broadaddr, addr)) 869 goto done; 870 } 871 ifa = NULL; 872done: 873 IFNET_RUNLOCK(); 874 return (ifa); 875} 876 877/* 878 * Locate the point to point interface with a given destination address. 879 */ 880/*ARGSUSED*/ 881struct ifaddr * 882ifa_ifwithdstaddr(addr) 883 struct sockaddr *addr; 884{ 885 struct ifnet *ifp; 886 struct ifaddr *ifa; 887 888 IFNET_RLOCK(); 889 TAILQ_FOREACH(ifp, &ifnet, if_link) { 890 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 891 continue; 892 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 893 if (ifa->ifa_addr->sa_family != addr->sa_family) 894 continue; 895 if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)) 896 goto done; 897 } 898 } 899 ifa = NULL; 900done: 901 IFNET_RUNLOCK(); 902 return (ifa); 903} 904 905/* 906 * Find an interface on a specific network. If many, choice 907 * is most specific found. 908 */ 909struct ifaddr * 910ifa_ifwithnet(addr) 911 struct sockaddr *addr; 912{ 913 register struct ifnet *ifp; 914 register struct ifaddr *ifa; 915 struct ifaddr *ifa_maybe = (struct ifaddr *) 0; 916 u_int af = addr->sa_family; 917 char *addr_data = addr->sa_data, *cplim; 918 919 /* 920 * AF_LINK addresses can be looked up directly by their index number, 921 * so do that if we can. 922 */ 923 if (af == AF_LINK) { 924 register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; 925 if (sdl->sdl_index && sdl->sdl_index <= if_index) 926 return (ifaddr_byindex(sdl->sdl_index)); 927 } 928 929 /* 930 * Scan though each interface, looking for ones that have 931 * addresses in this address family. 932 */ 933 IFNET_RLOCK(); 934 TAILQ_FOREACH(ifp, &ifnet, if_link) { 935 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 936 register char *cp, *cp2, *cp3; 937 938 if (ifa->ifa_addr->sa_family != af) 939next: continue; 940 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) { 941 /* 942 * This is a bit broken as it doesn't 943 * take into account that the remote end may 944 * be a single node in the network we are 945 * looking for. 946 * The trouble is that we don't know the 947 * netmask for the remote end. 948 */ 949 if (ifa->ifa_dstaddr != 0 950 && equal(addr, ifa->ifa_dstaddr)) 951 goto done; 952 } else { 953 /* 954 * if we have a special address handler, 955 * then use it instead of the generic one. 956 */ 957 if (ifa->ifa_claim_addr) { 958 if ((*ifa->ifa_claim_addr)(ifa, addr)) 959 goto done; 960 continue; 961 } 962 963 /* 964 * Scan all the bits in the ifa's address. 965 * If a bit dissagrees with what we are 966 * looking for, mask it with the netmask 967 * to see if it really matters. 968 * (A byte at a time) 969 */ 970 if (ifa->ifa_netmask == 0) 971 continue; 972 cp = addr_data; 973 cp2 = ifa->ifa_addr->sa_data; 974 cp3 = ifa->ifa_netmask->sa_data; 975 cplim = ifa->ifa_netmask->sa_len 976 + (char *)ifa->ifa_netmask; 977 while (cp3 < cplim) 978 if ((*cp++ ^ *cp2++) & *cp3++) 979 goto next; /* next address! */ 980 /* 981 * If the netmask of what we just found 982 * is more specific than what we had before 983 * (if we had one) then remember the new one 984 * before continuing to search 985 * for an even better one. 986 */ 987 if (ifa_maybe == 0 || 988 rn_refines((caddr_t)ifa->ifa_netmask, 989 (caddr_t)ifa_maybe->ifa_netmask)) 990 ifa_maybe = ifa; 991 } 992 } 993 } 994 ifa = ifa_maybe; 995done: 996 IFNET_RUNLOCK(); 997 return (ifa); 998} 999 1000/* 1001 * Find an interface address specific to an interface best matching 1002 * a given address. 1003 */ 1004struct ifaddr * 1005ifaof_ifpforaddr(addr, ifp) 1006 struct sockaddr *addr; 1007 register struct ifnet *ifp; 1008{ 1009 register struct ifaddr *ifa; 1010 register char *cp, *cp2, *cp3; 1011 register char *cplim; 1012 struct ifaddr *ifa_maybe = 0; 1013 u_int af = addr->sa_family; 1014 1015 if (af >= AF_MAX) 1016 return (0); 1017 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1018 if (ifa->ifa_addr->sa_family != af) 1019 continue; 1020 if (ifa_maybe == 0) 1021 ifa_maybe = ifa; 1022 if (ifa->ifa_netmask == 0) { 1023 if (equal(addr, ifa->ifa_addr) || 1024 (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))) 1025 goto done; 1026 continue; 1027 } 1028 if (ifp->if_flags & IFF_POINTOPOINT) { 1029 if (equal(addr, ifa->ifa_dstaddr)) 1030 goto done; 1031 } else { 1032 cp = addr->sa_data; 1033 cp2 = ifa->ifa_addr->sa_data; 1034 cp3 = ifa->ifa_netmask->sa_data; 1035 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 1036 for (; cp3 < cplim; cp3++) 1037 if ((*cp++ ^ *cp2++) & *cp3) 1038 break; 1039 if (cp3 == cplim) 1040 goto done; 1041 } 1042 } 1043 ifa = ifa_maybe; 1044done: 1045 return (ifa); 1046} 1047 1048#include <net/route.h> 1049 1050/* 1051 * Default action when installing a route with a Link Level gateway. 1052 * Lookup an appropriate real ifa to point to. 1053 * This should be moved to /sys/net/link.c eventually. 1054 */ 1055static void 1056link_rtrequest(cmd, rt, info) 1057 int cmd; 1058 register struct rtentry *rt; 1059 struct rt_addrinfo *info; 1060{ 1061 register struct ifaddr *ifa; 1062 struct sockaddr *dst; 1063 struct ifnet *ifp; 1064 1065 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 1066 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 1067 return; 1068 ifa = ifaof_ifpforaddr(dst, ifp); 1069 if (ifa) { 1070 IFAFREE(rt->rt_ifa); 1071 IFAREF(ifa); /* XXX */ 1072 rt->rt_ifa = ifa; 1073 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 1074 ifa->ifa_rtrequest(cmd, rt, info); 1075 } 1076} 1077 1078/* 1079 * Mark an interface down and notify protocols of 1080 * the transition. 1081 * NOTE: must be called at splnet or eqivalent. 1082 */ 1083void 1084if_unroute(ifp, flag, fam) 1085 register struct ifnet *ifp; 1086 int flag, fam; 1087{ 1088 register struct ifaddr *ifa; 1089 1090 ifp->if_flags &= ~flag; 1091 getmicrotime(&ifp->if_lastchange); 1092 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1093 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1094 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 1095 if_qflush(&ifp->if_snd); 1096 rt_ifmsg(ifp); 1097} 1098 1099/* 1100 * Mark an interface up and notify protocols of 1101 * the transition. 1102 * NOTE: must be called at splnet or eqivalent. 1103 */ 1104void 1105if_route(ifp, flag, fam) 1106 register struct ifnet *ifp; 1107 int flag, fam; 1108{ 1109 register struct ifaddr *ifa; 1110 1111 ifp->if_flags |= flag; 1112 getmicrotime(&ifp->if_lastchange); 1113 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1114 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1115 pfctlinput(PRC_IFUP, ifa->ifa_addr); 1116 rt_ifmsg(ifp); 1117#ifdef INET6 1118 in6_if_up(ifp); 1119#endif 1120} 1121 1122/* 1123 * Mark an interface down and notify protocols of 1124 * the transition. 1125 * NOTE: must be called at splnet or eqivalent. 1126 */ 1127void 1128if_down(ifp) 1129 register struct ifnet *ifp; 1130{ 1131 1132 if_unroute(ifp, IFF_UP, AF_UNSPEC); 1133} 1134 1135/* 1136 * Mark an interface up and notify protocols of 1137 * the transition. 1138 * NOTE: must be called at splnet or eqivalent. 1139 */ 1140void 1141if_up(ifp) 1142 register struct ifnet *ifp; 1143{ 1144 1145 if_route(ifp, IFF_UP, AF_UNSPEC); 1146} 1147 1148/* 1149 * Flush an interface queue. 1150 */ 1151static void 1152if_qflush(ifq) 1153 register struct ifqueue *ifq; 1154{ 1155 register struct mbuf *m, *n; 1156 1157 n = ifq->ifq_head; 1158 while ((m = n) != 0) { 1159 n = m->m_act; 1160 m_freem(m); 1161 } 1162 ifq->ifq_head = 0; 1163 ifq->ifq_tail = 0; 1164 ifq->ifq_len = 0; 1165} 1166 1167/* 1168 * Handle interface watchdog timer routines. Called 1169 * from softclock, we decrement timers (if set) and 1170 * call the appropriate interface routine on expiration. 1171 */ 1172static void 1173if_slowtimo(arg) 1174 void *arg; 1175{ 1176 register struct ifnet *ifp; 1177 int s = splimp(); 1178 1179 IFNET_RLOCK(); 1180 TAILQ_FOREACH(ifp, &ifnet, if_link) { 1181 if (ifp->if_timer == 0 || --ifp->if_timer) 1182 continue; 1183 if (ifp->if_watchdog) 1184 (*ifp->if_watchdog)(ifp); 1185 } 1186 IFNET_RUNLOCK(); 1187 splx(s); 1188 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ); 1189} 1190 1191/* 1192 * Map interface name to 1193 * interface structure pointer. 1194 */ 1195struct ifnet * 1196ifunit(const char *name) 1197{ 1198 char namebuf[IFNAMSIZ + 1]; 1199 struct ifnet *ifp; 1200 dev_t dev; 1201 1202 /* 1203 * Now search all the interfaces for this name/number 1204 */ 1205 1206 /* 1207 * XXX 1208 * Devices should really be known as /dev/fooN, not /dev/net/fooN. 1209 */ 1210 snprintf(namebuf, IFNAMSIZ, "%s/%s", net_cdevsw.d_name, name); 1211 IFNET_RLOCK(); 1212 TAILQ_FOREACH(ifp, &ifnet, if_link) { 1213 dev = ifdev_byindex(ifp->if_index); 1214 if (strcmp(devtoname(dev), namebuf) == 0) 1215 break; 1216 if (dev_named(dev, name)) 1217 break; 1218 } 1219 IFNET_RUNLOCK(); 1220 return (ifp); 1221} 1222 1223/* 1224 * Map interface name in a sockaddr_dl to 1225 * interface structure pointer. 1226 */ 1227struct ifnet * 1228if_withname(sa) 1229 struct sockaddr *sa; 1230{ 1231 char ifname[IFNAMSIZ+1]; 1232 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa; 1233 1234 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) || 1235 (sdl->sdl_nlen > IFNAMSIZ) ) 1236 return NULL; 1237 1238 /* 1239 * ifunit wants a NUL-terminated string. It may not be NUL-terminated 1240 * in the sockaddr, and we don't want to change the caller's sockaddr 1241 * (there might not be room to add the trailing NUL anyway), so we make 1242 * a local copy that we know we can NUL-terminate safely. 1243 */ 1244 1245 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen); 1246 ifname[sdl->sdl_nlen] = '\0'; 1247 return ifunit(ifname); 1248} 1249 1250/* 1251 * Hardware specific interface ioctls. 1252 */ 1253static int 1254ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td) 1255{ 1256 struct ifreq *ifr; 1257 struct ifstat *ifs; 1258 int error = 0; 1259 int new_flags; 1260 1261 ifr = (struct ifreq *)data; 1262 switch (cmd) { 1263 case SIOCGIFINDEX: 1264 ifr->ifr_index = ifp->if_index; 1265 break; 1266 1267 case SIOCGIFFLAGS: 1268 ifr->ifr_flags = ifp->if_flags & 0xffff; 1269 ifr->ifr_flagshigh = ifp->if_flags >> 16; 1270 break; 1271 1272 case SIOCGIFCAP: 1273 ifr->ifr_reqcap = ifp->if_capabilities; 1274 ifr->ifr_curcap = ifp->if_capenable; 1275 break; 1276 1277#ifdef MAC 1278 case SIOCGIFMAC: 1279 error = mac_ioctl_ifnet_get(td->td_proc->p_ucred, ifr, ifp); 1280 break; 1281#endif 1282 1283 case SIOCGIFMETRIC: 1284 ifr->ifr_metric = ifp->if_metric; 1285 break; 1286 1287 case SIOCGIFMTU: 1288 ifr->ifr_mtu = ifp->if_mtu; 1289 break; 1290 1291 case SIOCGIFPHYS: 1292 ifr->ifr_phys = ifp->if_physical; 1293 break; 1294 1295 case SIOCSIFFLAGS: 1296 error = suser(td); 1297 if (error) 1298 return (error); 1299 new_flags = (ifr->ifr_flags & 0xffff) | 1300 (ifr->ifr_flagshigh << 16); 1301 if (ifp->if_flags & IFF_SMART) { 1302 /* Smart drivers twiddle their own routes */ 1303 } else if (ifp->if_flags & IFF_UP && 1304 (new_flags & IFF_UP) == 0) { 1305 int s = splimp(); 1306 if_down(ifp); 1307 splx(s); 1308 } else if (new_flags & IFF_UP && 1309 (ifp->if_flags & IFF_UP) == 0) { 1310 int s = splimp(); 1311 if_up(ifp); 1312 splx(s); 1313 } 1314 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 1315 (new_flags &~ IFF_CANTCHANGE); 1316 if (new_flags & IFF_PPROMISC) { 1317 /* Permanently promiscuous mode requested */ 1318 ifp->if_flags |= IFF_PROMISC; 1319 } else if (ifp->if_pcount == 0) { 1320 ifp->if_flags &= ~IFF_PROMISC; 1321 } 1322 if (ifp->if_ioctl) 1323 (void) (*ifp->if_ioctl)(ifp, cmd, data); 1324 getmicrotime(&ifp->if_lastchange); 1325 break; 1326 1327 case SIOCSIFCAP: 1328 error = suser(td); 1329 if (error) 1330 return (error); 1331 if (ifr->ifr_reqcap & ~ifp->if_capabilities) 1332 return (EINVAL); 1333 (void) (*ifp->if_ioctl)(ifp, cmd, data); 1334 break; 1335 1336#ifdef MAC 1337 case SIOCSIFMAC: 1338 error = mac_ioctl_ifnet_set(td->td_proc->p_ucred, ifr, ifp); 1339 break; 1340#endif 1341 1342 case SIOCSIFMETRIC: 1343 error = suser(td); 1344 if (error) 1345 return (error); 1346 ifp->if_metric = ifr->ifr_metric; 1347 getmicrotime(&ifp->if_lastchange); 1348 break; 1349 1350 case SIOCSIFPHYS: 1351 error = suser(td); 1352 if (error) 1353 return error; 1354 if (!ifp->if_ioctl) 1355 return EOPNOTSUPP; 1356 error = (*ifp->if_ioctl)(ifp, cmd, data); 1357 if (error == 0) 1358 getmicrotime(&ifp->if_lastchange); 1359 return(error); 1360 1361 case SIOCSIFMTU: 1362 { 1363 u_long oldmtu = ifp->if_mtu; 1364 1365 error = suser(td); 1366 if (error) 1367 return (error); 1368 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) 1369 return (EINVAL); 1370 if (ifp->if_ioctl == NULL) 1371 return (EOPNOTSUPP); 1372 error = (*ifp->if_ioctl)(ifp, cmd, data); 1373 if (error == 0) { 1374 getmicrotime(&ifp->if_lastchange); 1375 rt_ifmsg(ifp); 1376 } 1377 /* 1378 * If the link MTU changed, do network layer specific procedure. 1379 */ 1380 if (ifp->if_mtu != oldmtu) { 1381#ifdef INET6 1382 nd6_setmtu(ifp); 1383#endif 1384 } 1385 break; 1386 } 1387 1388 case SIOCADDMULTI: 1389 case SIOCDELMULTI: 1390 error = suser(td); 1391 if (error) 1392 return (error); 1393 1394 /* Don't allow group membership on non-multicast interfaces. */ 1395 if ((ifp->if_flags & IFF_MULTICAST) == 0) 1396 return (EOPNOTSUPP); 1397 1398 /* Don't let users screw up protocols' entries. */ 1399 if (ifr->ifr_addr.sa_family != AF_LINK) 1400 return (EINVAL); 1401 1402 if (cmd == SIOCADDMULTI) { 1403 struct ifmultiaddr *ifma; 1404 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 1405 } else { 1406 error = if_delmulti(ifp, &ifr->ifr_addr); 1407 } 1408 if (error == 0) 1409 getmicrotime(&ifp->if_lastchange); 1410 break; 1411 1412 case SIOCSIFPHYADDR: 1413 case SIOCDIFPHYADDR: 1414#ifdef INET6 1415 case SIOCSIFPHYADDR_IN6: 1416#endif 1417 case SIOCSLIFPHYADDR: 1418 case SIOCSIFMEDIA: 1419 case SIOCSIFGENERIC: 1420 error = suser(td); 1421 if (error) 1422 return (error); 1423 if (ifp->if_ioctl == NULL) 1424 return (EOPNOTSUPP); 1425 error = (*ifp->if_ioctl)(ifp, cmd, data); 1426 if (error == 0) 1427 getmicrotime(&ifp->if_lastchange); 1428 break; 1429 1430 case SIOCGIFSTATUS: 1431 ifs = (struct ifstat *)data; 1432 ifs->ascii[0] = '\0'; 1433 1434 case SIOCGIFPSRCADDR: 1435 case SIOCGIFPDSTADDR: 1436 case SIOCGLIFPHYADDR: 1437 case SIOCGIFMEDIA: 1438 case SIOCGIFGENERIC: 1439 if (ifp->if_ioctl == 0) 1440 return (EOPNOTSUPP); 1441 error = (*ifp->if_ioctl)(ifp, cmd, data); 1442 break; 1443 1444 case SIOCSIFLLADDR: 1445 error = suser(td); 1446 if (error) 1447 return (error); 1448 error = if_setlladdr(ifp, 1449 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); 1450 break; 1451 1452 default: 1453 error = ENOIOCTL; 1454 break; 1455 } 1456 return (error); 1457} 1458 1459/* 1460 * Interface ioctls. 1461 */ 1462int 1463ifioctl(so, cmd, data, td) 1464 struct socket *so; 1465 u_long cmd; 1466 caddr_t data; 1467 struct thread *td; 1468{ 1469 struct ifnet *ifp; 1470 struct ifreq *ifr; 1471 int error; 1472 int oif_flags; 1473 1474 switch (cmd) { 1475 case SIOCGIFCONF: 1476 case OSIOCGIFCONF: 1477 return (ifconf(cmd, data)); 1478 } 1479 ifr = (struct ifreq *)data; 1480 1481 switch (cmd) { 1482 case SIOCIFCREATE: 1483 case SIOCIFDESTROY: 1484 if ((error = suser(td)) != 0) 1485 return (error); 1486 return ((cmd == SIOCIFCREATE) ? 1487 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) : 1488 if_clone_destroy(ifr->ifr_name)); 1489 1490 case SIOCIFGCLONERS: 1491 return (if_clone_list((struct if_clonereq *)data)); 1492 } 1493 1494 ifp = ifunit(ifr->ifr_name); 1495 if (ifp == 0) 1496 return (ENXIO); 1497 1498 error = ifhwioctl(cmd, ifp, data, td); 1499 if (error != ENOIOCTL) 1500 return (error); 1501 1502 oif_flags = ifp->if_flags; 1503 if (so->so_proto == 0) 1504 return (EOPNOTSUPP); 1505#ifndef COMPAT_43 1506 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, 1507 data, 1508 ifp, td)); 1509#else 1510 { 1511 int ocmd = cmd; 1512 1513 switch (cmd) { 1514 1515 case SIOCSIFDSTADDR: 1516 case SIOCSIFADDR: 1517 case SIOCSIFBRDADDR: 1518 case SIOCSIFNETMASK: 1519#if BYTE_ORDER != BIG_ENDIAN 1520 if (ifr->ifr_addr.sa_family == 0 && 1521 ifr->ifr_addr.sa_len < 16) { 1522 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 1523 ifr->ifr_addr.sa_len = 16; 1524 } 1525#else 1526 if (ifr->ifr_addr.sa_len == 0) 1527 ifr->ifr_addr.sa_len = 16; 1528#endif 1529 break; 1530 1531 case OSIOCGIFADDR: 1532 cmd = SIOCGIFADDR; 1533 break; 1534 1535 case OSIOCGIFDSTADDR: 1536 cmd = SIOCGIFDSTADDR; 1537 break; 1538 1539 case OSIOCGIFBRDADDR: 1540 cmd = SIOCGIFBRDADDR; 1541 break; 1542 1543 case OSIOCGIFNETMASK: 1544 cmd = SIOCGIFNETMASK; 1545 } 1546 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, 1547 cmd, 1548 data, 1549 ifp, td)); 1550 switch (ocmd) { 1551 1552 case OSIOCGIFADDR: 1553 case OSIOCGIFDSTADDR: 1554 case OSIOCGIFBRDADDR: 1555 case OSIOCGIFNETMASK: 1556 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 1557 1558 } 1559 } 1560#endif /* COMPAT_43 */ 1561 1562 if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 1563#ifdef INET6 1564 DELAY(100);/* XXX: temporary workaround for fxp issue*/ 1565 if (ifp->if_flags & IFF_UP) { 1566 int s = splimp(); 1567 in6_if_up(ifp); 1568 splx(s); 1569 } 1570#endif 1571 } 1572 return (error); 1573} 1574 1575/* 1576 * Set/clear promiscuous mode on interface ifp based on the truth value 1577 * of pswitch. The calls are reference counted so that only the first 1578 * "on" request actually has an effect, as does the final "off" request. 1579 * Results are undefined if the "off" and "on" requests are not matched. 1580 */ 1581int 1582ifpromisc(ifp, pswitch) 1583 struct ifnet *ifp; 1584 int pswitch; 1585{ 1586 struct ifreq ifr; 1587 int error; 1588 int oldflags, oldpcount; 1589 1590 oldpcount = ifp->if_pcount; 1591 oldflags = ifp->if_flags; 1592 if (ifp->if_flags & IFF_PPROMISC) { 1593 /* Do nothing if device is in permanently promiscuous mode */ 1594 ifp->if_pcount += pswitch ? 1 : -1; 1595 return (0); 1596 } 1597 if (pswitch) { 1598 /* 1599 * If the device is not configured up, we cannot put it in 1600 * promiscuous mode. 1601 */ 1602 if ((ifp->if_flags & IFF_UP) == 0) 1603 return (ENETDOWN); 1604 if (ifp->if_pcount++ != 0) 1605 return (0); 1606 ifp->if_flags |= IFF_PROMISC; 1607 } else { 1608 if (--ifp->if_pcount > 0) 1609 return (0); 1610 ifp->if_flags &= ~IFF_PROMISC; 1611 } 1612 ifr.ifr_flags = ifp->if_flags & 0xffff; 1613 ifr.ifr_flagshigh = ifp->if_flags >> 16; 1614 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 1615 if (error == 0) { 1616 log(LOG_INFO, "%s%d: promiscuous mode %s\n", 1617 ifp->if_name, ifp->if_unit, 1618 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); 1619 rt_ifmsg(ifp); 1620 } else { 1621 ifp->if_pcount = oldpcount; 1622 ifp->if_flags = oldflags; 1623 } 1624 return error; 1625} 1626 1627/* 1628 * Return interface configuration 1629 * of system. List may be used 1630 * in later ioctl's (above) to get 1631 * other information. 1632 */ 1633/*ARGSUSED*/ 1634static int 1635ifconf(cmd, data) 1636 u_long cmd; 1637 caddr_t data; 1638{ 1639 struct ifconf *ifc = (struct ifconf *)data; 1640 struct ifnet *ifp; 1641 struct ifaddr *ifa; 1642 struct ifreq ifr, *ifrp; 1643 int space = ifc->ifc_len, error = 0; 1644 1645 ifrp = ifc->ifc_req; 1646 IFNET_RLOCK(); /* could sleep XXX */ 1647 TAILQ_FOREACH(ifp, &ifnet, if_link) { 1648 char workbuf[64]; 1649 int ifnlen, addrs; 1650 1651 if (space < sizeof(ifr)) 1652 break; 1653 ifnlen = snprintf(workbuf, sizeof(workbuf), 1654 "%s%d", ifp->if_name, ifp->if_unit); 1655 if(ifnlen + 1 > sizeof ifr.ifr_name) { 1656 error = ENAMETOOLONG; 1657 break; 1658 } else { 1659 strcpy(ifr.ifr_name, workbuf); 1660 } 1661 1662 addrs = 0; 1663 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1664 struct sockaddr *sa = ifa->ifa_addr; 1665 1666 if (space < sizeof(ifr)) 1667 break; 1668 if (jailed(curthread->td_ucred) && 1669 prison_if(curthread->td_ucred, sa)) 1670 continue; 1671 addrs++; 1672#ifdef COMPAT_43 1673 if (cmd == OSIOCGIFCONF) { 1674 struct osockaddr *osa = 1675 (struct osockaddr *)&ifr.ifr_addr; 1676 ifr.ifr_addr = *sa; 1677 osa->sa_family = sa->sa_family; 1678 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 1679 sizeof (ifr)); 1680 ifrp++; 1681 } else 1682#endif 1683 if (sa->sa_len <= sizeof(*sa)) { 1684 ifr.ifr_addr = *sa; 1685 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 1686 sizeof (ifr)); 1687 ifrp++; 1688 } else { 1689 if (space < sizeof (ifr) + sa->sa_len - 1690 sizeof(*sa)) 1691 break; 1692 space -= sa->sa_len - sizeof(*sa); 1693 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 1694 sizeof (ifr.ifr_name)); 1695 if (error == 0) 1696 error = copyout((caddr_t)sa, 1697 (caddr_t)&ifrp->ifr_addr, sa->sa_len); 1698 ifrp = (struct ifreq *) 1699 (sa->sa_len + (caddr_t)&ifrp->ifr_addr); 1700 } 1701 if (error) 1702 break; 1703 space -= sizeof (ifr); 1704 } 1705 if (error) 1706 break; 1707 if (!addrs) { 1708 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 1709 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 1710 sizeof (ifr)); 1711 if (error) 1712 break; 1713 space -= sizeof (ifr); 1714 ifrp++; 1715 } 1716 } 1717 IFNET_RUNLOCK(); 1718 ifc->ifc_len -= space; 1719 return (error); 1720} 1721 1722/* 1723 * Just like if_promisc(), but for all-multicast-reception mode. 1724 */ 1725int 1726if_allmulti(ifp, onswitch) 1727 struct ifnet *ifp; 1728 int onswitch; 1729{ 1730 int error = 0; 1731 int s = splimp(); 1732 struct ifreq ifr; 1733 1734 if (onswitch) { 1735 if (ifp->if_amcount++ == 0) { 1736 ifp->if_flags |= IFF_ALLMULTI; 1737 ifr.ifr_flags = ifp->if_flags & 0xffff; 1738 ifr.ifr_flagshigh = ifp->if_flags >> 16; 1739 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 1740 } 1741 } else { 1742 if (ifp->if_amcount > 1) { 1743 ifp->if_amcount--; 1744 } else { 1745 ifp->if_amcount = 0; 1746 ifp->if_flags &= ~IFF_ALLMULTI; 1747 ifr.ifr_flags = ifp->if_flags & 0xffff;; 1748 ifr.ifr_flagshigh = ifp->if_flags >> 16; 1749 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 1750 } 1751 } 1752 splx(s); 1753 1754 if (error == 0) 1755 rt_ifmsg(ifp); 1756 return error; 1757} 1758 1759/* 1760 * Add a multicast listenership to the interface in question. 1761 * The link layer provides a routine which converts 1762 */ 1763int 1764if_addmulti(ifp, sa, retifma) 1765 struct ifnet *ifp; /* interface to manipulate */ 1766 struct sockaddr *sa; /* address to add */ 1767 struct ifmultiaddr **retifma; 1768{ 1769 struct sockaddr *llsa, *dupsa; 1770 int error, s; 1771 struct ifmultiaddr *ifma; 1772 1773 /* 1774 * If the matching multicast address already exists 1775 * then don't add a new one, just add a reference 1776 */ 1777 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1778 if (equal(sa, ifma->ifma_addr)) { 1779 ifma->ifma_refcount++; 1780 if (retifma) 1781 *retifma = ifma; 1782 return 0; 1783 } 1784 } 1785 1786 /* 1787 * Give the link layer a chance to accept/reject it, and also 1788 * find out which AF_LINK address this maps to, if it isn't one 1789 * already. 1790 */ 1791 if (ifp->if_resolvemulti) { 1792 error = ifp->if_resolvemulti(ifp, &llsa, sa); 1793 if (error) return error; 1794 } else { 1795 llsa = 0; 1796 } 1797 1798 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK); 1799 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK); 1800 bcopy(sa, dupsa, sa->sa_len); 1801 1802 ifma->ifma_addr = dupsa; 1803 ifma->ifma_lladdr = llsa; 1804 ifma->ifma_ifp = ifp; 1805 ifma->ifma_refcount = 1; 1806 ifma->ifma_protospec = 0; 1807 rt_newmaddrmsg(RTM_NEWMADDR, ifma); 1808 1809 /* 1810 * Some network interfaces can scan the address list at 1811 * interrupt time; lock them out. 1812 */ 1813 s = splimp(); 1814 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 1815 splx(s); 1816 if (retifma != NULL) 1817 *retifma = ifma; 1818 1819 if (llsa != 0) { 1820 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1821 if (equal(ifma->ifma_addr, llsa)) 1822 break; 1823 } 1824 if (ifma) { 1825 ifma->ifma_refcount++; 1826 } else { 1827 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, 1828 M_IFMADDR, M_WAITOK); 1829 MALLOC(dupsa, struct sockaddr *, llsa->sa_len, 1830 M_IFMADDR, M_WAITOK); 1831 bcopy(llsa, dupsa, llsa->sa_len); 1832 ifma->ifma_addr = dupsa; 1833 ifma->ifma_ifp = ifp; 1834 ifma->ifma_refcount = 1; 1835 s = splimp(); 1836 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 1837 splx(s); 1838 } 1839 } 1840 /* 1841 * We are certain we have added something, so call down to the 1842 * interface to let them know about it. 1843 */ 1844 s = splimp(); 1845 ifp->if_ioctl(ifp, SIOCADDMULTI, 0); 1846 splx(s); 1847 1848 return 0; 1849} 1850 1851/* 1852 * Remove a reference to a multicast address on this interface. Yell 1853 * if the request does not match an existing membership. 1854 */ 1855int 1856if_delmulti(ifp, sa) 1857 struct ifnet *ifp; 1858 struct sockaddr *sa; 1859{ 1860 struct ifmultiaddr *ifma; 1861 int s; 1862 1863 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 1864 if (equal(sa, ifma->ifma_addr)) 1865 break; 1866 if (ifma == 0) 1867 return ENOENT; 1868 1869 if (ifma->ifma_refcount > 1) { 1870 ifma->ifma_refcount--; 1871 return 0; 1872 } 1873 1874 rt_newmaddrmsg(RTM_DELMADDR, ifma); 1875 sa = ifma->ifma_lladdr; 1876 s = splimp(); 1877 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 1878 /* 1879 * Make sure the interface driver is notified 1880 * in the case of a link layer mcast group being left. 1881 */ 1882 if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0) 1883 ifp->if_ioctl(ifp, SIOCDELMULTI, 0); 1884 splx(s); 1885 free(ifma->ifma_addr, M_IFMADDR); 1886 free(ifma, M_IFMADDR); 1887 if (sa == 0) 1888 return 0; 1889 1890 /* 1891 * Now look for the link-layer address which corresponds to 1892 * this network address. It had been squirreled away in 1893 * ifma->ifma_lladdr for this purpose (so we don't have 1894 * to call ifp->if_resolvemulti() again), and we saved that 1895 * value in sa above. If some nasty deleted the 1896 * link-layer address out from underneath us, we can deal because 1897 * the address we stored was is not the same as the one which was 1898 * in the record for the link-layer address. (So we don't complain 1899 * in that case.) 1900 */ 1901 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 1902 if (equal(sa, ifma->ifma_addr)) 1903 break; 1904 if (ifma == 0) 1905 return 0; 1906 1907 if (ifma->ifma_refcount > 1) { 1908 ifma->ifma_refcount--; 1909 return 0; 1910 } 1911 1912 s = splimp(); 1913 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 1914 ifp->if_ioctl(ifp, SIOCDELMULTI, 0); 1915 splx(s); 1916 free(ifma->ifma_addr, M_IFMADDR); 1917 free(sa, M_IFMADDR); 1918 free(ifma, M_IFMADDR); 1919 1920 return 0; 1921} 1922 1923/* 1924 * Set the link layer address on an interface. 1925 * 1926 * At this time we only support certain types of interfaces, 1927 * and we don't allow the length of the address to change. 1928 */ 1929int 1930if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 1931{ 1932 struct sockaddr_dl *sdl; 1933 struct ifaddr *ifa; 1934 struct ifreq ifr; 1935 1936 ifa = ifaddr_byindex(ifp->if_index); 1937 if (ifa == NULL) 1938 return (EINVAL); 1939 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 1940 if (sdl == NULL) 1941 return (EINVAL); 1942 if (len != sdl->sdl_alen) /* don't allow length to change */ 1943 return (EINVAL); 1944 switch (ifp->if_type) { 1945 case IFT_ETHER: /* these types use struct arpcom */ 1946 case IFT_FDDI: 1947 case IFT_XETHER: 1948 case IFT_ISO88025: 1949 case IFT_L2VLAN: 1950 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len); 1951 /* FALLTHROUGH */ 1952 case IFT_ARCNET: 1953 bcopy(lladdr, LLADDR(sdl), len); 1954 break; 1955 default: 1956 return (ENODEV); 1957 } 1958 /* 1959 * If the interface is already up, we need 1960 * to re-init it in order to reprogram its 1961 * address filter. 1962 */ 1963 if ((ifp->if_flags & IFF_UP) != 0) { 1964 ifp->if_flags &= ~IFF_UP; 1965 ifr.ifr_flags = ifp->if_flags & 0xffff; 1966 ifr.ifr_flagshigh = ifp->if_flags >> 16; 1967 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 1968 ifp->if_flags |= IFF_UP; 1969 ifr.ifr_flags = ifp->if_flags & 0xffff; 1970 ifr.ifr_flagshigh = ifp->if_flags >> 16; 1971 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 1972#ifdef INET 1973 /* 1974 * Also send gratuitous ARPs to notify other nodes about 1975 * the address change. 1976 */ 1977 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1978 if (ifa->ifa_addr != NULL && 1979 ifa->ifa_addr->sa_family == AF_INET) 1980 arp_ifinit(ifp, ifa); 1981 } 1982#endif 1983 } 1984 return (0); 1985} 1986 1987struct ifmultiaddr * 1988ifmaof_ifpforaddr(sa, ifp) 1989 struct sockaddr *sa; 1990 struct ifnet *ifp; 1991{ 1992 struct ifmultiaddr *ifma; 1993 1994 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 1995 if (equal(ifma->ifma_addr, sa)) 1996 break; 1997 1998 return ifma; 1999} 2000 2001int 2002if_printf(struct ifnet *ifp, const char * fmt, ...) 2003{ 2004 va_list ap; 2005 int retval; 2006 2007 retval = printf("%s%d: ", ifp->if_name, ifp->if_unit); 2008 va_start(ap, fmt); 2009 retval += vprintf(fmt, ap); 2010 va_end(ap); 2011 return (retval); 2012} 2013 2014SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 2015SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
| 141}; 142 143static int 144netopen(dev_t dev, int flag, int mode, struct thread *td) 145{ 146 return (0); 147} 148 149static int 150netclose(dev_t dev, int flags, int fmt, struct thread *td) 151{ 152 return (0); 153} 154 155static int 156netioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td) 157{ 158 struct ifnet *ifp; 159 int error, idx; 160 161 /* only support interface specific ioctls */ 162 if (IOCGROUP(cmd) != 'i') 163 return (EOPNOTSUPP); 164 idx = minor(dev); 165 if (idx == 0) { 166 /* 167 * special network device, not interface. 168 */ 169 if (cmd == SIOCGIFCONF) 170 return (ifconf(cmd, data)); /* XXX remove cmd */ 171 return (EOPNOTSUPP); 172 } 173 174 ifp = ifnet_byindex(idx); 175 if (ifp == NULL) 176 return (ENXIO); 177 178 error = ifhwioctl(cmd, ifp, data, td); 179 if (error == ENOIOCTL) 180 error = EOPNOTSUPP; 181 return (error); 182} 183 184static int 185netkqfilter(dev_t dev, struct knote *kn) 186{ 187 struct klist *klist; 188 struct ifnet *ifp; 189 int idx; 190 191 idx = minor(dev); 192 if (idx == 0) { 193 klist = &ifklist; 194 } else { 195 ifp = ifnet_byindex(idx); 196 if (ifp == NULL) 197 return (1); 198 klist = &ifp->if_klist; 199 } 200 201 switch (kn->kn_filter) { 202 case EVFILT_NETDEV: 203 kn->kn_fop = &netdev_filtops; 204 break; 205 default: 206 return (1); 207 } 208 209 kn->kn_hook = (caddr_t)klist; 210 211 /* XXX locking? */ 212 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 213 214 return (0); 215} 216 217static void 218filt_netdetach(struct knote *kn) 219{ 220 struct klist *klist = (struct klist *)kn->kn_hook; 221 222 if (kn->kn_status & KN_DETACHED) 223 return; 224 SLIST_REMOVE(klist, kn, knote, kn_selnext); 225} 226 227static int 228filt_netdev(struct knote *kn, long hint) 229{ 230 231 /* 232 * Currently NOTE_EXIT is abused to indicate device detach. 233 */ 234 if (hint == NOTE_EXIT) { 235 kn->kn_data = NOTE_LINKINV; 236 kn->kn_status |= KN_DETACHED; 237 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 238 return (1); 239 } 240 kn->kn_data = hint; /* current status */ 241 if (kn->kn_sfflags & hint) 242 kn->kn_fflags |= hint; 243 return (kn->kn_fflags != 0); 244} 245 246/* 247 * Network interface utility routines. 248 * 249 * Routines with ifa_ifwith* names take sockaddr *'s as 250 * parameters. 251 */ 252/* ARGSUSED*/ 253static void 254if_init(dummy) 255 void *dummy; 256{ 257 258 IFNET_LOCK_INIT(); 259 TAILQ_INIT(&ifnet); 260 SLIST_INIT(&ifklist); 261 if_grow(); /* create initial table */ 262 ifdev_byindex(0) = make_dev(&net_cdevsw, 0, 263 UID_ROOT, GID_WHEEL, 0600, "network"); 264} 265 266static void 267if_grow(void) 268{ 269 u_int n; 270 struct ifindex_entry *e; 271 272 if_indexlim <<= 1; 273 n = if_indexlim * sizeof(*e); 274 e = malloc(n, M_IFADDR, M_WAITOK | M_ZERO); 275 if (ifindex_table != NULL) { 276 memcpy((caddr_t)e, (caddr_t)ifindex_table, n/2); 277 free((caddr_t)ifindex_table, M_IFADDR); 278 } 279 ifindex_table = e; 280} 281 282/* ARGSUSED*/ 283static void 284if_check(dummy) 285 void *dummy; 286{ 287 struct ifnet *ifp; 288 int s; 289 290 s = splimp(); 291 IFNET_RLOCK(); /* could sleep on rare error; mostly okay XXX */ 292 TAILQ_FOREACH(ifp, &ifnet, if_link) { 293 if (ifp->if_snd.ifq_maxlen == 0) { 294 printf("%s%d XXX: driver didn't set ifq_maxlen\n", 295 ifp->if_name, ifp->if_unit); 296 ifp->if_snd.ifq_maxlen = ifqmaxlen; 297 } 298 if (!mtx_initialized(&ifp->if_snd.ifq_mtx)) { 299 printf("%s%d XXX: driver didn't initialize queue mtx\n", 300 ifp->if_name, ifp->if_unit); 301 mtx_init(&ifp->if_snd.ifq_mtx, "unknown", 302 MTX_NETWORK_LOCK, MTX_DEF); 303 } 304 } 305 IFNET_RUNLOCK(); 306 splx(s); 307 if_slowtimo(0); 308} 309 310static int 311if_findindex(struct ifnet *ifp) 312{ 313 int i, unit; 314 char eaddr[18], devname[32]; 315 const char *name, *p; 316 317 switch (ifp->if_type) { 318 case IFT_ETHER: /* these types use struct arpcom */ 319 case IFT_FDDI: 320 case IFT_XETHER: 321 case IFT_ISO88025: 322 case IFT_L2VLAN: 323 snprintf(eaddr, 18, "%6D", 324 ((struct arpcom *)ifp->if_softc)->ac_enaddr, ":"); 325 break; 326 default: 327 eaddr[0] = '\0'; 328 break; 329 } 330 snprintf(devname, 32, "%s%d", ifp->if_name, ifp->if_unit); 331 name = net_cdevsw.d_name; 332 i = 0; 333 while ((resource_find_dev(&i, name, &unit, NULL, NULL)) == 0) { 334 if (resource_string_value(name, unit, "ether", &p) == 0) 335 if (strcmp(p, eaddr) == 0) 336 goto found; 337 if (resource_string_value(name, unit, "dev", &p) == 0) 338 if (strcmp(p, devname) == 0) 339 goto found; 340 } 341 unit = 0; 342found: 343 if (unit != 0) { 344 if (ifaddr_byindex(unit) == NULL) 345 return (unit); 346 printf("%s%d in use, cannot hardwire it to %s.\n", 347 name, unit, devname); 348 } 349 for (unit = 1; ; unit++) { 350 if (unit <= if_index && ifaddr_byindex(unit) != NULL) 351 continue; 352 if (resource_string_value(name, unit, "ether", &p) == 0 || 353 resource_string_value(name, unit, "dev", &p) == 0) 354 continue; 355 break; 356 } 357 return (unit); 358} 359 360/* 361 * Attach an interface to the 362 * list of "active" interfaces. 363 */ 364void 365if_attach(ifp) 366 struct ifnet *ifp; 367{ 368 unsigned socksize, ifasize; 369 int namelen, masklen; 370 char workbuf[64]; 371 register struct sockaddr_dl *sdl; 372 register struct ifaddr *ifa; 373 374 IFNET_WLOCK(); 375 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link); 376 IFNET_WUNLOCK(); 377 /* 378 * XXX - 379 * The old code would work if the interface passed a pre-existing 380 * chain of ifaddrs to this code. We don't trust our callers to 381 * properly initialize the tailq, however, so we no longer allow 382 * this unlikely case. 383 */ 384 TAILQ_INIT(&ifp->if_addrhead); 385 TAILQ_INIT(&ifp->if_prefixhead); 386 TAILQ_INIT(&ifp->if_multiaddrs); 387 SLIST_INIT(&ifp->if_klist); 388 getmicrotime(&ifp->if_lastchange); 389 390#ifdef MAC 391 mac_init_ifnet(ifp); 392 mac_create_ifnet(ifp); 393#endif 394 395 ifp->if_index = if_findindex(ifp); 396 if (ifp->if_index > if_index) 397 if_index = ifp->if_index; 398 if (if_index >= if_indexlim) 399 if_grow(); 400 401 ifnet_byindex(ifp->if_index) = ifp; 402 ifdev_byindex(ifp->if_index) = make_dev(&net_cdevsw, ifp->if_index, 403 UID_ROOT, GID_WHEEL, 0600, "%s/%s%d", 404 net_cdevsw.d_name, ifp->if_name, ifp->if_unit); 405 make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d", 406 net_cdevsw.d_name, ifp->if_index); 407 408 mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_name, "if send queue", MTX_DEF); 409 410 /* 411 * create a Link Level name for this device 412 */ 413 namelen = snprintf(workbuf, sizeof(workbuf), 414 "%s%d", ifp->if_name, ifp->if_unit); 415#define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m)) 416 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen; 417 socksize = masklen + ifp->if_addrlen; 418#define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1))) 419 if (socksize < sizeof(*sdl)) 420 socksize = sizeof(*sdl); 421 socksize = ROUNDUP(socksize); 422 ifasize = sizeof(*ifa) + 2 * socksize; 423 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO); 424 if (ifa) { 425 IFA_LOCK_INIT(ifa); 426 sdl = (struct sockaddr_dl *)(ifa + 1); 427 sdl->sdl_len = socksize; 428 sdl->sdl_family = AF_LINK; 429 bcopy(workbuf, sdl->sdl_data, namelen); 430 sdl->sdl_nlen = namelen; 431 sdl->sdl_index = ifp->if_index; 432 sdl->sdl_type = ifp->if_type; 433 ifaddr_byindex(ifp->if_index) = ifa; 434 ifa->ifa_ifp = ifp; 435 ifa->ifa_rtrequest = link_rtrequest; 436 ifa->ifa_addr = (struct sockaddr *)sdl; 437 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 438 ifa->ifa_netmask = (struct sockaddr *)sdl; 439 sdl->sdl_len = masklen; 440 while (namelen != 0) 441 sdl->sdl_data[--namelen] = 0xff; 442 ifa->ifa_refcnt = 1; 443 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link); 444 } 445 ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */ 446 447 /* Announce the interface. */ 448 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 449} 450 451/* 452 * Detach an interface, removing it from the 453 * list of "active" interfaces. 454 */ 455void 456if_detach(ifp) 457 struct ifnet *ifp; 458{ 459 struct ifaddr *ifa; 460 struct radix_node_head *rnh; 461 int s; 462 int i; 463 464 /* 465 * Remove routes and flush queues. 466 */ 467 s = splnet(); 468 if_down(ifp); 469 470 /* 471 * Remove address from ifindex_table[] and maybe decrement if_index. 472 * Clean up all addresses. 473 */ 474 ifaddr_byindex(ifp->if_index) = NULL; 475 revoke_and_destroy_dev(ifdev_byindex(ifp->if_index)); 476 ifdev_byindex(ifp->if_index) = NULL; 477 478 while (if_index > 0 && ifaddr_byindex(if_index) == NULL) 479 if_index--; 480 481 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa; 482 ifa = TAILQ_FIRST(&ifp->if_addrhead)) { 483#ifdef INET 484 /* XXX: Ugly!! ad hoc just for INET */ 485 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { 486 struct ifaliasreq ifr; 487 488 bzero(&ifr, sizeof(ifr)); 489 ifr.ifra_addr = *ifa->ifa_addr; 490 if (ifa->ifa_dstaddr) 491 ifr.ifra_broadaddr = *ifa->ifa_dstaddr; 492 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp, 493 NULL) == 0) 494 continue; 495 } 496#endif /* INET */ 497#ifdef INET6 498 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) { 499 in6_purgeaddr(ifa); 500 /* ifp_addrhead is already updated */ 501 continue; 502 } 503#endif /* INET6 */ 504 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 505 IFAFREE(ifa); 506 } 507 508#ifdef INET6 509 /* 510 * Remove all IPv6 kernel structs related to ifp. This should be done 511 * before removing routing entries below, since IPv6 interface direct 512 * routes are expected to be removed by the IPv6-specific kernel API. 513 * Otherwise, the kernel will detect some inconsistency and bark it. 514 */ 515 in6_ifdetach(ifp); 516#endif 517 518 /* 519 * Delete all remaining routes using this interface 520 * Unfortuneatly the only way to do this is to slog through 521 * the entire routing table looking for routes which point 522 * to this interface...oh well... 523 */ 524 for (i = 1; i <= AF_MAX; i++) { 525 if ((rnh = rt_tables[i]) == NULL) 526 continue; 527 RADIX_NODE_HEAD_LOCK(rnh); 528 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp); 529 RADIX_NODE_HEAD_UNLOCK(rnh); 530 } 531 532 /* Announce that the interface is gone. */ 533 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 534 535#ifdef MAC 536 mac_destroy_ifnet(ifp); 537#endif /* MAC */ 538 KNOTE(&ifp->if_klist, NOTE_EXIT); 539 IFNET_WLOCK(); 540 TAILQ_REMOVE(&ifnet, ifp, if_link); 541 IFNET_WUNLOCK(); 542 mtx_destroy(&ifp->if_snd.ifq_mtx); 543 splx(s); 544} 545 546/* 547 * Delete Routes for a Network Interface 548 * 549 * Called for each routing entry via the rnh->rnh_walktree() call above 550 * to delete all route entries referencing a detaching network interface. 551 * 552 * Arguments: 553 * rn pointer to node in the routing table 554 * arg argument passed to rnh->rnh_walktree() - detaching interface 555 * 556 * Returns: 557 * 0 successful 558 * errno failed - reason indicated 559 * 560 */ 561static int 562if_rtdel(rn, arg) 563 struct radix_node *rn; 564 void *arg; 565{ 566 struct rtentry *rt = (struct rtentry *)rn; 567 struct ifnet *ifp = arg; 568 int err; 569 570 if (rt->rt_ifp == ifp) { 571 572 /* 573 * Protect (sorta) against walktree recursion problems 574 * with cloned routes 575 */ 576 if ((rt->rt_flags & RTF_UP) == 0) 577 return (0); 578 579 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, 580 rt_mask(rt), rt->rt_flags, 581 (struct rtentry **) NULL); 582 if (err) { 583 log(LOG_WARNING, "if_rtdel: error %d\n", err); 584 } 585 } 586 587 return (0); 588} 589 590/* 591 * Create a clone network interface. 592 */ 593int 594if_clone_create(name, len) 595 char *name; 596 int len; 597{ 598 struct if_clone *ifc; 599 char *dp; 600 int wildcard, bytoff, bitoff; 601 int unit; 602 int err; 603 604 ifc = if_clone_lookup(name, &unit); 605 if (ifc == NULL) 606 return (EINVAL); 607 608 if (ifunit(name) != NULL) 609 return (EEXIST); 610 611 bytoff = bitoff = 0; 612 wildcard = (unit < 0); 613 /* 614 * Find a free unit if none was given. 615 */ 616 if (wildcard) { 617 while ((bytoff < ifc->ifc_bmlen) 618 && (ifc->ifc_units[bytoff] == 0xff)) 619 bytoff++; 620 if (bytoff >= ifc->ifc_bmlen) 621 return (ENOSPC); 622 while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0) 623 bitoff++; 624 unit = (bytoff << 3) + bitoff; 625 } 626 627 if (unit > ifc->ifc_maxunit) 628 return (ENXIO); 629 630 err = (*ifc->ifc_create)(ifc, unit); 631 if (err != 0) 632 return (err); 633 634 if (!wildcard) { 635 bytoff = unit >> 3; 636 bitoff = unit - (bytoff << 3); 637 } 638 639 /* 640 * Allocate the unit in the bitmap. 641 */ 642 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0, 643 ("%s: bit is already set", __func__)); 644 ifc->ifc_units[bytoff] |= (1 << bitoff); 645 646 /* In the wildcard case, we need to update the name. */ 647 if (wildcard) { 648 for (dp = name; *dp != '\0'; dp++); 649 if (snprintf(dp, len - (dp-name), "%d", unit) > 650 len - (dp-name) - 1) { 651 /* 652 * This can only be a programmer error and 653 * there's no straightforward way to recover if 654 * it happens. 655 */ 656 panic("if_clone_create(): interface name too long"); 657 } 658 659 } 660 661 return (0); 662} 663 664/* 665 * Destroy a clone network interface. 666 */ 667int 668if_clone_destroy(name) 669 const char *name; 670{ 671 struct if_clone *ifc; 672 struct ifnet *ifp; 673 int bytoff, bitoff; 674 int unit; 675 676 ifc = if_clone_lookup(name, &unit); 677 if (ifc == NULL) 678 return (EINVAL); 679 680 if (unit < ifc->ifc_minifs) 681 return (EINVAL); 682 683 ifp = ifunit(name); 684 if (ifp == NULL) 685 return (ENXIO); 686 687 if (ifc->ifc_destroy == NULL) 688 return (EOPNOTSUPP); 689 690 (*ifc->ifc_destroy)(ifp); 691 692 /* 693 * Compute offset in the bitmap and deallocate the unit. 694 */ 695 bytoff = unit >> 3; 696 bitoff = unit - (bytoff << 3); 697 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0, 698 ("%s: bit is already cleared", __func__)); 699 ifc->ifc_units[bytoff] &= ~(1 << bitoff); 700 return (0); 701} 702 703/* 704 * Look up a network interface cloner. 705 */ 706static struct if_clone * 707if_clone_lookup(name, unitp) 708 const char *name; 709 int *unitp; 710{ 711 struct if_clone *ifc; 712 const char *cp; 713 int i; 714 715 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) { 716 for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) { 717 if (ifc->ifc_name[i] != *cp) 718 goto next_ifc; 719 } 720 goto found_name; 721 next_ifc: 722 ifc = LIST_NEXT(ifc, ifc_list); 723 } 724 725 /* No match. */ 726 return ((struct if_clone *)NULL); 727 728 found_name: 729 if (*cp == '\0') { 730 i = -1; 731 } else { 732 for (i = 0; *cp != '\0'; cp++) { 733 if (*cp < '0' || *cp > '9') { 734 /* Bogus unit number. */ 735 return (NULL); 736 } 737 i = (i * 10) + (*cp - '0'); 738 } 739 } 740 741 if (unitp != NULL) 742 *unitp = i; 743 return (ifc); 744} 745 746/* 747 * Register a network interface cloner. 748 */ 749void 750if_clone_attach(ifc) 751 struct if_clone *ifc; 752{ 753 int bytoff, bitoff; 754 int err; 755 int len, maxclone; 756 int unit; 757 758 KASSERT(ifc->ifc_minifs - 1 <= ifc->ifc_maxunit, 759 ("%s: %s requested more units then allowed (%d > %d)", 760 __func__, ifc->ifc_name, ifc->ifc_minifs, 761 ifc->ifc_maxunit + 1)); 762 /* 763 * Compute bitmap size and allocate it. 764 */ 765 maxclone = ifc->ifc_maxunit + 1; 766 len = maxclone >> 3; 767 if ((len << 3) < maxclone) 768 len++; 769 ifc->ifc_units = malloc(len, M_CLONE, M_WAITOK | M_ZERO); 770 ifc->ifc_bmlen = len; 771 772 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list); 773 if_cloners_count++; 774 775 for (unit = 0; unit < ifc->ifc_minifs; unit++) { 776 err = (*ifc->ifc_create)(ifc, unit); 777 KASSERT(err == 0, 778 ("%s: failed to create required interface %s%d", 779 __func__, ifc->ifc_name, unit)); 780 781 /* Allocate the unit in the bitmap. */ 782 bytoff = unit >> 3; 783 bitoff = unit - (bytoff << 3); 784 ifc->ifc_units[bytoff] |= (1 << bitoff); 785 } 786} 787 788/* 789 * Unregister a network interface cloner. 790 */ 791void 792if_clone_detach(ifc) 793 struct if_clone *ifc; 794{ 795 796 LIST_REMOVE(ifc, ifc_list); 797 free(ifc->ifc_units, M_CLONE); 798 if_cloners_count--; 799} 800 801/* 802 * Provide list of interface cloners to userspace. 803 */ 804static int 805if_clone_list(ifcr) 806 struct if_clonereq *ifcr; 807{ 808 char outbuf[IFNAMSIZ], *dst; 809 struct if_clone *ifc; 810 int count, error = 0; 811 812 ifcr->ifcr_total = if_cloners_count; 813 if ((dst = ifcr->ifcr_buffer) == NULL) { 814 /* Just asking how many there are. */ 815 return (0); 816 } 817 818 if (ifcr->ifcr_count < 0) 819 return (EINVAL); 820 821 count = (if_cloners_count < ifcr->ifcr_count) ? 822 if_cloners_count : ifcr->ifcr_count; 823 824 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0; 825 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) { 826 strncpy(outbuf, ifc->ifc_name, IFNAMSIZ); 827 outbuf[IFNAMSIZ - 1] = '\0'; /* sanity */ 828 error = copyout(outbuf, dst, IFNAMSIZ); 829 if (error) 830 break; 831 } 832 833 return (error); 834} 835 836#define equal(a1, a2) \ 837 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0) 838 839/* 840 * Locate an interface based on a complete address. 841 */ 842/*ARGSUSED*/ 843struct ifaddr * 844ifa_ifwithaddr(addr) 845 struct sockaddr *addr; 846{ 847 struct ifnet *ifp; 848 struct ifaddr *ifa; 849 850 IFNET_RLOCK(); 851 TAILQ_FOREACH(ifp, &ifnet, if_link) 852 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 853 if (ifa->ifa_addr->sa_family != addr->sa_family) 854 continue; 855 if (equal(addr, ifa->ifa_addr)) 856 goto done; 857 /* IP6 doesn't have broadcast */ 858 if ((ifp->if_flags & IFF_BROADCAST) && 859 ifa->ifa_broadaddr && 860 ifa->ifa_broadaddr->sa_len != 0 && 861 equal(ifa->ifa_broadaddr, addr)) 862 goto done; 863 } 864 ifa = NULL; 865done: 866 IFNET_RUNLOCK(); 867 return (ifa); 868} 869 870/* 871 * Locate the point to point interface with a given destination address. 872 */ 873/*ARGSUSED*/ 874struct ifaddr * 875ifa_ifwithdstaddr(addr) 876 struct sockaddr *addr; 877{ 878 struct ifnet *ifp; 879 struct ifaddr *ifa; 880 881 IFNET_RLOCK(); 882 TAILQ_FOREACH(ifp, &ifnet, if_link) { 883 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 884 continue; 885 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 886 if (ifa->ifa_addr->sa_family != addr->sa_family) 887 continue; 888 if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)) 889 goto done; 890 } 891 } 892 ifa = NULL; 893done: 894 IFNET_RUNLOCK(); 895 return (ifa); 896} 897 898/* 899 * Find an interface on a specific network. If many, choice 900 * is most specific found. 901 */ 902struct ifaddr * 903ifa_ifwithnet(addr) 904 struct sockaddr *addr; 905{ 906 register struct ifnet *ifp; 907 register struct ifaddr *ifa; 908 struct ifaddr *ifa_maybe = (struct ifaddr *) 0; 909 u_int af = addr->sa_family; 910 char *addr_data = addr->sa_data, *cplim; 911 912 /* 913 * AF_LINK addresses can be looked up directly by their index number, 914 * so do that if we can. 915 */ 916 if (af == AF_LINK) { 917 register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; 918 if (sdl->sdl_index && sdl->sdl_index <= if_index) 919 return (ifaddr_byindex(sdl->sdl_index)); 920 } 921 922 /* 923 * Scan though each interface, looking for ones that have 924 * addresses in this address family. 925 */ 926 IFNET_RLOCK(); 927 TAILQ_FOREACH(ifp, &ifnet, if_link) { 928 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 929 register char *cp, *cp2, *cp3; 930 931 if (ifa->ifa_addr->sa_family != af) 932next: continue; 933 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) { 934 /* 935 * This is a bit broken as it doesn't 936 * take into account that the remote end may 937 * be a single node in the network we are 938 * looking for. 939 * The trouble is that we don't know the 940 * netmask for the remote end. 941 */ 942 if (ifa->ifa_dstaddr != 0 943 && equal(addr, ifa->ifa_dstaddr)) 944 goto done; 945 } else { 946 /* 947 * if we have a special address handler, 948 * then use it instead of the generic one. 949 */ 950 if (ifa->ifa_claim_addr) { 951 if ((*ifa->ifa_claim_addr)(ifa, addr)) 952 goto done; 953 continue; 954 } 955 956 /* 957 * Scan all the bits in the ifa's address. 958 * If a bit dissagrees with what we are 959 * looking for, mask it with the netmask 960 * to see if it really matters. 961 * (A byte at a time) 962 */ 963 if (ifa->ifa_netmask == 0) 964 continue; 965 cp = addr_data; 966 cp2 = ifa->ifa_addr->sa_data; 967 cp3 = ifa->ifa_netmask->sa_data; 968 cplim = ifa->ifa_netmask->sa_len 969 + (char *)ifa->ifa_netmask; 970 while (cp3 < cplim) 971 if ((*cp++ ^ *cp2++) & *cp3++) 972 goto next; /* next address! */ 973 /* 974 * If the netmask of what we just found 975 * is more specific than what we had before 976 * (if we had one) then remember the new one 977 * before continuing to search 978 * for an even better one. 979 */ 980 if (ifa_maybe == 0 || 981 rn_refines((caddr_t)ifa->ifa_netmask, 982 (caddr_t)ifa_maybe->ifa_netmask)) 983 ifa_maybe = ifa; 984 } 985 } 986 } 987 ifa = ifa_maybe; 988done: 989 IFNET_RUNLOCK(); 990 return (ifa); 991} 992 993/* 994 * Find an interface address specific to an interface best matching 995 * a given address. 996 */ 997struct ifaddr * 998ifaof_ifpforaddr(addr, ifp) 999 struct sockaddr *addr; 1000 register struct ifnet *ifp; 1001{ 1002 register struct ifaddr *ifa; 1003 register char *cp, *cp2, *cp3; 1004 register char *cplim; 1005 struct ifaddr *ifa_maybe = 0; 1006 u_int af = addr->sa_family; 1007 1008 if (af >= AF_MAX) 1009 return (0); 1010 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1011 if (ifa->ifa_addr->sa_family != af) 1012 continue; 1013 if (ifa_maybe == 0) 1014 ifa_maybe = ifa; 1015 if (ifa->ifa_netmask == 0) { 1016 if (equal(addr, ifa->ifa_addr) || 1017 (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))) 1018 goto done; 1019 continue; 1020 } 1021 if (ifp->if_flags & IFF_POINTOPOINT) { 1022 if (equal(addr, ifa->ifa_dstaddr)) 1023 goto done; 1024 } else { 1025 cp = addr->sa_data; 1026 cp2 = ifa->ifa_addr->sa_data; 1027 cp3 = ifa->ifa_netmask->sa_data; 1028 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 1029 for (; cp3 < cplim; cp3++) 1030 if ((*cp++ ^ *cp2++) & *cp3) 1031 break; 1032 if (cp3 == cplim) 1033 goto done; 1034 } 1035 } 1036 ifa = ifa_maybe; 1037done: 1038 return (ifa); 1039} 1040 1041#include <net/route.h> 1042 1043/* 1044 * Default action when installing a route with a Link Level gateway. 1045 * Lookup an appropriate real ifa to point to. 1046 * This should be moved to /sys/net/link.c eventually. 1047 */ 1048static void 1049link_rtrequest(cmd, rt, info) 1050 int cmd; 1051 register struct rtentry *rt; 1052 struct rt_addrinfo *info; 1053{ 1054 register struct ifaddr *ifa; 1055 struct sockaddr *dst; 1056 struct ifnet *ifp; 1057 1058 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 1059 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 1060 return; 1061 ifa = ifaof_ifpforaddr(dst, ifp); 1062 if (ifa) { 1063 IFAFREE(rt->rt_ifa); 1064 IFAREF(ifa); /* XXX */ 1065 rt->rt_ifa = ifa; 1066 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 1067 ifa->ifa_rtrequest(cmd, rt, info); 1068 } 1069} 1070 1071/* 1072 * Mark an interface down and notify protocols of 1073 * the transition. 1074 * NOTE: must be called at splnet or eqivalent. 1075 */ 1076void 1077if_unroute(ifp, flag, fam) 1078 register struct ifnet *ifp; 1079 int flag, fam; 1080{ 1081 register struct ifaddr *ifa; 1082 1083 ifp->if_flags &= ~flag; 1084 getmicrotime(&ifp->if_lastchange); 1085 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1086 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1087 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 1088 if_qflush(&ifp->if_snd); 1089 rt_ifmsg(ifp); 1090} 1091 1092/* 1093 * Mark an interface up and notify protocols of 1094 * the transition. 1095 * NOTE: must be called at splnet or eqivalent. 1096 */ 1097void 1098if_route(ifp, flag, fam) 1099 register struct ifnet *ifp; 1100 int flag, fam; 1101{ 1102 register struct ifaddr *ifa; 1103 1104 ifp->if_flags |= flag; 1105 getmicrotime(&ifp->if_lastchange); 1106 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1107 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1108 pfctlinput(PRC_IFUP, ifa->ifa_addr); 1109 rt_ifmsg(ifp); 1110#ifdef INET6 1111 in6_if_up(ifp); 1112#endif 1113} 1114 1115/* 1116 * Mark an interface down and notify protocols of 1117 * the transition. 1118 * NOTE: must be called at splnet or eqivalent. 1119 */ 1120void 1121if_down(ifp) 1122 register struct ifnet *ifp; 1123{ 1124 1125 if_unroute(ifp, IFF_UP, AF_UNSPEC); 1126} 1127 1128/* 1129 * Mark an interface up and notify protocols of 1130 * the transition. 1131 * NOTE: must be called at splnet or eqivalent. 1132 */ 1133void 1134if_up(ifp) 1135 register struct ifnet *ifp; 1136{ 1137 1138 if_route(ifp, IFF_UP, AF_UNSPEC); 1139} 1140 1141/* 1142 * Flush an interface queue. 1143 */ 1144static void 1145if_qflush(ifq) 1146 register struct ifqueue *ifq; 1147{ 1148 register struct mbuf *m, *n; 1149 1150 n = ifq->ifq_head; 1151 while ((m = n) != 0) { 1152 n = m->m_act; 1153 m_freem(m); 1154 } 1155 ifq->ifq_head = 0; 1156 ifq->ifq_tail = 0; 1157 ifq->ifq_len = 0; 1158} 1159 1160/* 1161 * Handle interface watchdog timer routines. Called 1162 * from softclock, we decrement timers (if set) and 1163 * call the appropriate interface routine on expiration. 1164 */ 1165static void 1166if_slowtimo(arg) 1167 void *arg; 1168{ 1169 register struct ifnet *ifp; 1170 int s = splimp(); 1171 1172 IFNET_RLOCK(); 1173 TAILQ_FOREACH(ifp, &ifnet, if_link) { 1174 if (ifp->if_timer == 0 || --ifp->if_timer) 1175 continue; 1176 if (ifp->if_watchdog) 1177 (*ifp->if_watchdog)(ifp); 1178 } 1179 IFNET_RUNLOCK(); 1180 splx(s); 1181 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ); 1182} 1183 1184/* 1185 * Map interface name to 1186 * interface structure pointer. 1187 */ 1188struct ifnet * 1189ifunit(const char *name) 1190{ 1191 char namebuf[IFNAMSIZ + 1]; 1192 struct ifnet *ifp; 1193 dev_t dev; 1194 1195 /* 1196 * Now search all the interfaces for this name/number 1197 */ 1198 1199 /* 1200 * XXX 1201 * Devices should really be known as /dev/fooN, not /dev/net/fooN. 1202 */ 1203 snprintf(namebuf, IFNAMSIZ, "%s/%s", net_cdevsw.d_name, name); 1204 IFNET_RLOCK(); 1205 TAILQ_FOREACH(ifp, &ifnet, if_link) { 1206 dev = ifdev_byindex(ifp->if_index); 1207 if (strcmp(devtoname(dev), namebuf) == 0) 1208 break; 1209 if (dev_named(dev, name)) 1210 break; 1211 } 1212 IFNET_RUNLOCK(); 1213 return (ifp); 1214} 1215 1216/* 1217 * Map interface name in a sockaddr_dl to 1218 * interface structure pointer. 1219 */ 1220struct ifnet * 1221if_withname(sa) 1222 struct sockaddr *sa; 1223{ 1224 char ifname[IFNAMSIZ+1]; 1225 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa; 1226 1227 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) || 1228 (sdl->sdl_nlen > IFNAMSIZ) ) 1229 return NULL; 1230 1231 /* 1232 * ifunit wants a NUL-terminated string. It may not be NUL-terminated 1233 * in the sockaddr, and we don't want to change the caller's sockaddr 1234 * (there might not be room to add the trailing NUL anyway), so we make 1235 * a local copy that we know we can NUL-terminate safely. 1236 */ 1237 1238 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen); 1239 ifname[sdl->sdl_nlen] = '\0'; 1240 return ifunit(ifname); 1241} 1242 1243/* 1244 * Hardware specific interface ioctls. 1245 */ 1246static int 1247ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td) 1248{ 1249 struct ifreq *ifr; 1250 struct ifstat *ifs; 1251 int error = 0; 1252 int new_flags; 1253 1254 ifr = (struct ifreq *)data; 1255 switch (cmd) { 1256 case SIOCGIFINDEX: 1257 ifr->ifr_index = ifp->if_index; 1258 break; 1259 1260 case SIOCGIFFLAGS: 1261 ifr->ifr_flags = ifp->if_flags & 0xffff; 1262 ifr->ifr_flagshigh = ifp->if_flags >> 16; 1263 break; 1264 1265 case SIOCGIFCAP: 1266 ifr->ifr_reqcap = ifp->if_capabilities; 1267 ifr->ifr_curcap = ifp->if_capenable; 1268 break; 1269 1270#ifdef MAC 1271 case SIOCGIFMAC: 1272 error = mac_ioctl_ifnet_get(td->td_proc->p_ucred, ifr, ifp); 1273 break; 1274#endif 1275 1276 case SIOCGIFMETRIC: 1277 ifr->ifr_metric = ifp->if_metric; 1278 break; 1279 1280 case SIOCGIFMTU: 1281 ifr->ifr_mtu = ifp->if_mtu; 1282 break; 1283 1284 case SIOCGIFPHYS: 1285 ifr->ifr_phys = ifp->if_physical; 1286 break; 1287 1288 case SIOCSIFFLAGS: 1289 error = suser(td); 1290 if (error) 1291 return (error); 1292 new_flags = (ifr->ifr_flags & 0xffff) | 1293 (ifr->ifr_flagshigh << 16); 1294 if (ifp->if_flags & IFF_SMART) { 1295 /* Smart drivers twiddle their own routes */ 1296 } else if (ifp->if_flags & IFF_UP && 1297 (new_flags & IFF_UP) == 0) { 1298 int s = splimp(); 1299 if_down(ifp); 1300 splx(s); 1301 } else if (new_flags & IFF_UP && 1302 (ifp->if_flags & IFF_UP) == 0) { 1303 int s = splimp(); 1304 if_up(ifp); 1305 splx(s); 1306 } 1307 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 1308 (new_flags &~ IFF_CANTCHANGE); 1309 if (new_flags & IFF_PPROMISC) { 1310 /* Permanently promiscuous mode requested */ 1311 ifp->if_flags |= IFF_PROMISC; 1312 } else if (ifp->if_pcount == 0) { 1313 ifp->if_flags &= ~IFF_PROMISC; 1314 } 1315 if (ifp->if_ioctl) 1316 (void) (*ifp->if_ioctl)(ifp, cmd, data); 1317 getmicrotime(&ifp->if_lastchange); 1318 break; 1319 1320 case SIOCSIFCAP: 1321 error = suser(td); 1322 if (error) 1323 return (error); 1324 if (ifr->ifr_reqcap & ~ifp->if_capabilities) 1325 return (EINVAL); 1326 (void) (*ifp->if_ioctl)(ifp, cmd, data); 1327 break; 1328 1329#ifdef MAC 1330 case SIOCSIFMAC: 1331 error = mac_ioctl_ifnet_set(td->td_proc->p_ucred, ifr, ifp); 1332 break; 1333#endif 1334 1335 case SIOCSIFMETRIC: 1336 error = suser(td); 1337 if (error) 1338 return (error); 1339 ifp->if_metric = ifr->ifr_metric; 1340 getmicrotime(&ifp->if_lastchange); 1341 break; 1342 1343 case SIOCSIFPHYS: 1344 error = suser(td); 1345 if (error) 1346 return error; 1347 if (!ifp->if_ioctl) 1348 return EOPNOTSUPP; 1349 error = (*ifp->if_ioctl)(ifp, cmd, data); 1350 if (error == 0) 1351 getmicrotime(&ifp->if_lastchange); 1352 return(error); 1353 1354 case SIOCSIFMTU: 1355 { 1356 u_long oldmtu = ifp->if_mtu; 1357 1358 error = suser(td); 1359 if (error) 1360 return (error); 1361 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) 1362 return (EINVAL); 1363 if (ifp->if_ioctl == NULL) 1364 return (EOPNOTSUPP); 1365 error = (*ifp->if_ioctl)(ifp, cmd, data); 1366 if (error == 0) { 1367 getmicrotime(&ifp->if_lastchange); 1368 rt_ifmsg(ifp); 1369 } 1370 /* 1371 * If the link MTU changed, do network layer specific procedure. 1372 */ 1373 if (ifp->if_mtu != oldmtu) { 1374#ifdef INET6 1375 nd6_setmtu(ifp); 1376#endif 1377 } 1378 break; 1379 } 1380 1381 case SIOCADDMULTI: 1382 case SIOCDELMULTI: 1383 error = suser(td); 1384 if (error) 1385 return (error); 1386 1387 /* Don't allow group membership on non-multicast interfaces. */ 1388 if ((ifp->if_flags & IFF_MULTICAST) == 0) 1389 return (EOPNOTSUPP); 1390 1391 /* Don't let users screw up protocols' entries. */ 1392 if (ifr->ifr_addr.sa_family != AF_LINK) 1393 return (EINVAL); 1394 1395 if (cmd == SIOCADDMULTI) { 1396 struct ifmultiaddr *ifma; 1397 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 1398 } else { 1399 error = if_delmulti(ifp, &ifr->ifr_addr); 1400 } 1401 if (error == 0) 1402 getmicrotime(&ifp->if_lastchange); 1403 break; 1404 1405 case SIOCSIFPHYADDR: 1406 case SIOCDIFPHYADDR: 1407#ifdef INET6 1408 case SIOCSIFPHYADDR_IN6: 1409#endif 1410 case SIOCSLIFPHYADDR: 1411 case SIOCSIFMEDIA: 1412 case SIOCSIFGENERIC: 1413 error = suser(td); 1414 if (error) 1415 return (error); 1416 if (ifp->if_ioctl == NULL) 1417 return (EOPNOTSUPP); 1418 error = (*ifp->if_ioctl)(ifp, cmd, data); 1419 if (error == 0) 1420 getmicrotime(&ifp->if_lastchange); 1421 break; 1422 1423 case SIOCGIFSTATUS: 1424 ifs = (struct ifstat *)data; 1425 ifs->ascii[0] = '\0'; 1426 1427 case SIOCGIFPSRCADDR: 1428 case SIOCGIFPDSTADDR: 1429 case SIOCGLIFPHYADDR: 1430 case SIOCGIFMEDIA: 1431 case SIOCGIFGENERIC: 1432 if (ifp->if_ioctl == 0) 1433 return (EOPNOTSUPP); 1434 error = (*ifp->if_ioctl)(ifp, cmd, data); 1435 break; 1436 1437 case SIOCSIFLLADDR: 1438 error = suser(td); 1439 if (error) 1440 return (error); 1441 error = if_setlladdr(ifp, 1442 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); 1443 break; 1444 1445 default: 1446 error = ENOIOCTL; 1447 break; 1448 } 1449 return (error); 1450} 1451 1452/* 1453 * Interface ioctls. 1454 */ 1455int 1456ifioctl(so, cmd, data, td) 1457 struct socket *so; 1458 u_long cmd; 1459 caddr_t data; 1460 struct thread *td; 1461{ 1462 struct ifnet *ifp; 1463 struct ifreq *ifr; 1464 int error; 1465 int oif_flags; 1466 1467 switch (cmd) { 1468 case SIOCGIFCONF: 1469 case OSIOCGIFCONF: 1470 return (ifconf(cmd, data)); 1471 } 1472 ifr = (struct ifreq *)data; 1473 1474 switch (cmd) { 1475 case SIOCIFCREATE: 1476 case SIOCIFDESTROY: 1477 if ((error = suser(td)) != 0) 1478 return (error); 1479 return ((cmd == SIOCIFCREATE) ? 1480 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) : 1481 if_clone_destroy(ifr->ifr_name)); 1482 1483 case SIOCIFGCLONERS: 1484 return (if_clone_list((struct if_clonereq *)data)); 1485 } 1486 1487 ifp = ifunit(ifr->ifr_name); 1488 if (ifp == 0) 1489 return (ENXIO); 1490 1491 error = ifhwioctl(cmd, ifp, data, td); 1492 if (error != ENOIOCTL) 1493 return (error); 1494 1495 oif_flags = ifp->if_flags; 1496 if (so->so_proto == 0) 1497 return (EOPNOTSUPP); 1498#ifndef COMPAT_43 1499 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, 1500 data, 1501 ifp, td)); 1502#else 1503 { 1504 int ocmd = cmd; 1505 1506 switch (cmd) { 1507 1508 case SIOCSIFDSTADDR: 1509 case SIOCSIFADDR: 1510 case SIOCSIFBRDADDR: 1511 case SIOCSIFNETMASK: 1512#if BYTE_ORDER != BIG_ENDIAN 1513 if (ifr->ifr_addr.sa_family == 0 && 1514 ifr->ifr_addr.sa_len < 16) { 1515 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 1516 ifr->ifr_addr.sa_len = 16; 1517 } 1518#else 1519 if (ifr->ifr_addr.sa_len == 0) 1520 ifr->ifr_addr.sa_len = 16; 1521#endif 1522 break; 1523 1524 case OSIOCGIFADDR: 1525 cmd = SIOCGIFADDR; 1526 break; 1527 1528 case OSIOCGIFDSTADDR: 1529 cmd = SIOCGIFDSTADDR; 1530 break; 1531 1532 case OSIOCGIFBRDADDR: 1533 cmd = SIOCGIFBRDADDR; 1534 break; 1535 1536 case OSIOCGIFNETMASK: 1537 cmd = SIOCGIFNETMASK; 1538 } 1539 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, 1540 cmd, 1541 data, 1542 ifp, td)); 1543 switch (ocmd) { 1544 1545 case OSIOCGIFADDR: 1546 case OSIOCGIFDSTADDR: 1547 case OSIOCGIFBRDADDR: 1548 case OSIOCGIFNETMASK: 1549 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 1550 1551 } 1552 } 1553#endif /* COMPAT_43 */ 1554 1555 if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 1556#ifdef INET6 1557 DELAY(100);/* XXX: temporary workaround for fxp issue*/ 1558 if (ifp->if_flags & IFF_UP) { 1559 int s = splimp(); 1560 in6_if_up(ifp); 1561 splx(s); 1562 } 1563#endif 1564 } 1565 return (error); 1566} 1567 1568/* 1569 * Set/clear promiscuous mode on interface ifp based on the truth value 1570 * of pswitch. The calls are reference counted so that only the first 1571 * "on" request actually has an effect, as does the final "off" request. 1572 * Results are undefined if the "off" and "on" requests are not matched. 1573 */ 1574int 1575ifpromisc(ifp, pswitch) 1576 struct ifnet *ifp; 1577 int pswitch; 1578{ 1579 struct ifreq ifr; 1580 int error; 1581 int oldflags, oldpcount; 1582 1583 oldpcount = ifp->if_pcount; 1584 oldflags = ifp->if_flags; 1585 if (ifp->if_flags & IFF_PPROMISC) { 1586 /* Do nothing if device is in permanently promiscuous mode */ 1587 ifp->if_pcount += pswitch ? 1 : -1; 1588 return (0); 1589 } 1590 if (pswitch) { 1591 /* 1592 * If the device is not configured up, we cannot put it in 1593 * promiscuous mode. 1594 */ 1595 if ((ifp->if_flags & IFF_UP) == 0) 1596 return (ENETDOWN); 1597 if (ifp->if_pcount++ != 0) 1598 return (0); 1599 ifp->if_flags |= IFF_PROMISC; 1600 } else { 1601 if (--ifp->if_pcount > 0) 1602 return (0); 1603 ifp->if_flags &= ~IFF_PROMISC; 1604 } 1605 ifr.ifr_flags = ifp->if_flags & 0xffff; 1606 ifr.ifr_flagshigh = ifp->if_flags >> 16; 1607 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 1608 if (error == 0) { 1609 log(LOG_INFO, "%s%d: promiscuous mode %s\n", 1610 ifp->if_name, ifp->if_unit, 1611 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); 1612 rt_ifmsg(ifp); 1613 } else { 1614 ifp->if_pcount = oldpcount; 1615 ifp->if_flags = oldflags; 1616 } 1617 return error; 1618} 1619 1620/* 1621 * Return interface configuration 1622 * of system. List may be used 1623 * in later ioctl's (above) to get 1624 * other information. 1625 */ 1626/*ARGSUSED*/ 1627static int 1628ifconf(cmd, data) 1629 u_long cmd; 1630 caddr_t data; 1631{ 1632 struct ifconf *ifc = (struct ifconf *)data; 1633 struct ifnet *ifp; 1634 struct ifaddr *ifa; 1635 struct ifreq ifr, *ifrp; 1636 int space = ifc->ifc_len, error = 0; 1637 1638 ifrp = ifc->ifc_req; 1639 IFNET_RLOCK(); /* could sleep XXX */ 1640 TAILQ_FOREACH(ifp, &ifnet, if_link) { 1641 char workbuf[64]; 1642 int ifnlen, addrs; 1643 1644 if (space < sizeof(ifr)) 1645 break; 1646 ifnlen = snprintf(workbuf, sizeof(workbuf), 1647 "%s%d", ifp->if_name, ifp->if_unit); 1648 if(ifnlen + 1 > sizeof ifr.ifr_name) { 1649 error = ENAMETOOLONG; 1650 break; 1651 } else { 1652 strcpy(ifr.ifr_name, workbuf); 1653 } 1654 1655 addrs = 0; 1656 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1657 struct sockaddr *sa = ifa->ifa_addr; 1658 1659 if (space < sizeof(ifr)) 1660 break; 1661 if (jailed(curthread->td_ucred) && 1662 prison_if(curthread->td_ucred, sa)) 1663 continue; 1664 addrs++; 1665#ifdef COMPAT_43 1666 if (cmd == OSIOCGIFCONF) { 1667 struct osockaddr *osa = 1668 (struct osockaddr *)&ifr.ifr_addr; 1669 ifr.ifr_addr = *sa; 1670 osa->sa_family = sa->sa_family; 1671 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 1672 sizeof (ifr)); 1673 ifrp++; 1674 } else 1675#endif 1676 if (sa->sa_len <= sizeof(*sa)) { 1677 ifr.ifr_addr = *sa; 1678 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 1679 sizeof (ifr)); 1680 ifrp++; 1681 } else { 1682 if (space < sizeof (ifr) + sa->sa_len - 1683 sizeof(*sa)) 1684 break; 1685 space -= sa->sa_len - sizeof(*sa); 1686 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 1687 sizeof (ifr.ifr_name)); 1688 if (error == 0) 1689 error = copyout((caddr_t)sa, 1690 (caddr_t)&ifrp->ifr_addr, sa->sa_len); 1691 ifrp = (struct ifreq *) 1692 (sa->sa_len + (caddr_t)&ifrp->ifr_addr); 1693 } 1694 if (error) 1695 break; 1696 space -= sizeof (ifr); 1697 } 1698 if (error) 1699 break; 1700 if (!addrs) { 1701 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 1702 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 1703 sizeof (ifr)); 1704 if (error) 1705 break; 1706 space -= sizeof (ifr); 1707 ifrp++; 1708 } 1709 } 1710 IFNET_RUNLOCK(); 1711 ifc->ifc_len -= space; 1712 return (error); 1713} 1714 1715/* 1716 * Just like if_promisc(), but for all-multicast-reception mode. 1717 */ 1718int 1719if_allmulti(ifp, onswitch) 1720 struct ifnet *ifp; 1721 int onswitch; 1722{ 1723 int error = 0; 1724 int s = splimp(); 1725 struct ifreq ifr; 1726 1727 if (onswitch) { 1728 if (ifp->if_amcount++ == 0) { 1729 ifp->if_flags |= IFF_ALLMULTI; 1730 ifr.ifr_flags = ifp->if_flags & 0xffff; 1731 ifr.ifr_flagshigh = ifp->if_flags >> 16; 1732 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 1733 } 1734 } else { 1735 if (ifp->if_amcount > 1) { 1736 ifp->if_amcount--; 1737 } else { 1738 ifp->if_amcount = 0; 1739 ifp->if_flags &= ~IFF_ALLMULTI; 1740 ifr.ifr_flags = ifp->if_flags & 0xffff;; 1741 ifr.ifr_flagshigh = ifp->if_flags >> 16; 1742 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 1743 } 1744 } 1745 splx(s); 1746 1747 if (error == 0) 1748 rt_ifmsg(ifp); 1749 return error; 1750} 1751 1752/* 1753 * Add a multicast listenership to the interface in question. 1754 * The link layer provides a routine which converts 1755 */ 1756int 1757if_addmulti(ifp, sa, retifma) 1758 struct ifnet *ifp; /* interface to manipulate */ 1759 struct sockaddr *sa; /* address to add */ 1760 struct ifmultiaddr **retifma; 1761{ 1762 struct sockaddr *llsa, *dupsa; 1763 int error, s; 1764 struct ifmultiaddr *ifma; 1765 1766 /* 1767 * If the matching multicast address already exists 1768 * then don't add a new one, just add a reference 1769 */ 1770 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1771 if (equal(sa, ifma->ifma_addr)) { 1772 ifma->ifma_refcount++; 1773 if (retifma) 1774 *retifma = ifma; 1775 return 0; 1776 } 1777 } 1778 1779 /* 1780 * Give the link layer a chance to accept/reject it, and also 1781 * find out which AF_LINK address this maps to, if it isn't one 1782 * already. 1783 */ 1784 if (ifp->if_resolvemulti) { 1785 error = ifp->if_resolvemulti(ifp, &llsa, sa); 1786 if (error) return error; 1787 } else { 1788 llsa = 0; 1789 } 1790 1791 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK); 1792 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK); 1793 bcopy(sa, dupsa, sa->sa_len); 1794 1795 ifma->ifma_addr = dupsa; 1796 ifma->ifma_lladdr = llsa; 1797 ifma->ifma_ifp = ifp; 1798 ifma->ifma_refcount = 1; 1799 ifma->ifma_protospec = 0; 1800 rt_newmaddrmsg(RTM_NEWMADDR, ifma); 1801 1802 /* 1803 * Some network interfaces can scan the address list at 1804 * interrupt time; lock them out. 1805 */ 1806 s = splimp(); 1807 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 1808 splx(s); 1809 if (retifma != NULL) 1810 *retifma = ifma; 1811 1812 if (llsa != 0) { 1813 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1814 if (equal(ifma->ifma_addr, llsa)) 1815 break; 1816 } 1817 if (ifma) { 1818 ifma->ifma_refcount++; 1819 } else { 1820 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, 1821 M_IFMADDR, M_WAITOK); 1822 MALLOC(dupsa, struct sockaddr *, llsa->sa_len, 1823 M_IFMADDR, M_WAITOK); 1824 bcopy(llsa, dupsa, llsa->sa_len); 1825 ifma->ifma_addr = dupsa; 1826 ifma->ifma_ifp = ifp; 1827 ifma->ifma_refcount = 1; 1828 s = splimp(); 1829 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 1830 splx(s); 1831 } 1832 } 1833 /* 1834 * We are certain we have added something, so call down to the 1835 * interface to let them know about it. 1836 */ 1837 s = splimp(); 1838 ifp->if_ioctl(ifp, SIOCADDMULTI, 0); 1839 splx(s); 1840 1841 return 0; 1842} 1843 1844/* 1845 * Remove a reference to a multicast address on this interface. Yell 1846 * if the request does not match an existing membership. 1847 */ 1848int 1849if_delmulti(ifp, sa) 1850 struct ifnet *ifp; 1851 struct sockaddr *sa; 1852{ 1853 struct ifmultiaddr *ifma; 1854 int s; 1855 1856 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 1857 if (equal(sa, ifma->ifma_addr)) 1858 break; 1859 if (ifma == 0) 1860 return ENOENT; 1861 1862 if (ifma->ifma_refcount > 1) { 1863 ifma->ifma_refcount--; 1864 return 0; 1865 } 1866 1867 rt_newmaddrmsg(RTM_DELMADDR, ifma); 1868 sa = ifma->ifma_lladdr; 1869 s = splimp(); 1870 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 1871 /* 1872 * Make sure the interface driver is notified 1873 * in the case of a link layer mcast group being left. 1874 */ 1875 if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0) 1876 ifp->if_ioctl(ifp, SIOCDELMULTI, 0); 1877 splx(s); 1878 free(ifma->ifma_addr, M_IFMADDR); 1879 free(ifma, M_IFMADDR); 1880 if (sa == 0) 1881 return 0; 1882 1883 /* 1884 * Now look for the link-layer address which corresponds to 1885 * this network address. It had been squirreled away in 1886 * ifma->ifma_lladdr for this purpose (so we don't have 1887 * to call ifp->if_resolvemulti() again), and we saved that 1888 * value in sa above. If some nasty deleted the 1889 * link-layer address out from underneath us, we can deal because 1890 * the address we stored was is not the same as the one which was 1891 * in the record for the link-layer address. (So we don't complain 1892 * in that case.) 1893 */ 1894 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 1895 if (equal(sa, ifma->ifma_addr)) 1896 break; 1897 if (ifma == 0) 1898 return 0; 1899 1900 if (ifma->ifma_refcount > 1) { 1901 ifma->ifma_refcount--; 1902 return 0; 1903 } 1904 1905 s = splimp(); 1906 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 1907 ifp->if_ioctl(ifp, SIOCDELMULTI, 0); 1908 splx(s); 1909 free(ifma->ifma_addr, M_IFMADDR); 1910 free(sa, M_IFMADDR); 1911 free(ifma, M_IFMADDR); 1912 1913 return 0; 1914} 1915 1916/* 1917 * Set the link layer address on an interface. 1918 * 1919 * At this time we only support certain types of interfaces, 1920 * and we don't allow the length of the address to change. 1921 */ 1922int 1923if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 1924{ 1925 struct sockaddr_dl *sdl; 1926 struct ifaddr *ifa; 1927 struct ifreq ifr; 1928 1929 ifa = ifaddr_byindex(ifp->if_index); 1930 if (ifa == NULL) 1931 return (EINVAL); 1932 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 1933 if (sdl == NULL) 1934 return (EINVAL); 1935 if (len != sdl->sdl_alen) /* don't allow length to change */ 1936 return (EINVAL); 1937 switch (ifp->if_type) { 1938 case IFT_ETHER: /* these types use struct arpcom */ 1939 case IFT_FDDI: 1940 case IFT_XETHER: 1941 case IFT_ISO88025: 1942 case IFT_L2VLAN: 1943 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len); 1944 /* FALLTHROUGH */ 1945 case IFT_ARCNET: 1946 bcopy(lladdr, LLADDR(sdl), len); 1947 break; 1948 default: 1949 return (ENODEV); 1950 } 1951 /* 1952 * If the interface is already up, we need 1953 * to re-init it in order to reprogram its 1954 * address filter. 1955 */ 1956 if ((ifp->if_flags & IFF_UP) != 0) { 1957 ifp->if_flags &= ~IFF_UP; 1958 ifr.ifr_flags = ifp->if_flags & 0xffff; 1959 ifr.ifr_flagshigh = ifp->if_flags >> 16; 1960 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 1961 ifp->if_flags |= IFF_UP; 1962 ifr.ifr_flags = ifp->if_flags & 0xffff; 1963 ifr.ifr_flagshigh = ifp->if_flags >> 16; 1964 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 1965#ifdef INET 1966 /* 1967 * Also send gratuitous ARPs to notify other nodes about 1968 * the address change. 1969 */ 1970 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1971 if (ifa->ifa_addr != NULL && 1972 ifa->ifa_addr->sa_family == AF_INET) 1973 arp_ifinit(ifp, ifa); 1974 } 1975#endif 1976 } 1977 return (0); 1978} 1979 1980struct ifmultiaddr * 1981ifmaof_ifpforaddr(sa, ifp) 1982 struct sockaddr *sa; 1983 struct ifnet *ifp; 1984{ 1985 struct ifmultiaddr *ifma; 1986 1987 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 1988 if (equal(ifma->ifma_addr, sa)) 1989 break; 1990 1991 return ifma; 1992} 1993 1994int 1995if_printf(struct ifnet *ifp, const char * fmt, ...) 1996{ 1997 va_list ap; 1998 int retval; 1999 2000 retval = printf("%s%d: ", ifp->if_name, ifp->if_unit); 2001 va_start(ap, fmt); 2002 retval += vprintf(fmt, ap); 2003 va_end(ap); 2004 return (retval); 2005} 2006 2007SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 2008SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
|