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