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