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