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