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/malloc.h> 43 #include <sys/sbuf.h> 44 #include <sys/bus.h> 45 #include <sys/mbuf.h> 46 #include <sys/systm.h> 47 #include <sys/priv.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_clone.h> 63 #include <net/if_dl.h> 64 #include <net/if_types.h> 65 #include <net/if_var.h> 66 #include <net/radix.h> 67 #include <net/route.h> 68 69 #if defined(INET) || defined(INET6) 70 /*XXX*/ 71 #include <netinet/in.h> 72 #include <netinet/in_var.h> 73 #ifdef INET6 74 #include <netinet6/in6_var.h> 75 #include <netinet6/in6_ifattach.h> 76 #endif 77 #endif 78 #ifdef INET 79 #include <netinet/if_ether.h> 80 #endif 81 #ifdef DEV_CARP 82 #include <netinet/ip_carp.h> 83 #endif 84 85 #include <security/mac/mac_framework.h> 86 87 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 88 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); 89 90 /* Log link state change events */ 91 static int log_link_state_change = 1; 92 93 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW, 94 &log_link_state_change, 0, 95 "log interface link state change events"); 96 97 void (*bstp_linkstate_p)(struct ifnet *ifp, int state); 98 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state); 99 100 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL; 101 102 /* 103 * XXX: Style; these should be sorted alphabetically, and unprototyped 104 * static functions should be prototyped. Currently they are sorted by 105 * declaration order. 106 */ 107 static void if_attachdomain(void *); 108 static void if_attachdomain1(struct ifnet *); 109 static void if_purgemaddrs(struct ifnet *); 110 static int ifconf(u_long, caddr_t); 111 static void if_freemulti(struct ifmultiaddr *); 112 static void if_grow(void); 113 static void if_init(void *); 114 static void if_check(void *); 115 static void if_qflush(struct ifaltq *); 116 static void if_route(struct ifnet *, int flag, int fam); 117 static int if_setflag(struct ifnet *, int, int, int *, int); 118 static void if_slowtimo(void *); 119 static void if_unroute(struct ifnet *, int flag, int fam); 120 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 121 static int if_rtdel(struct radix_node *, void *); 122 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *); 123 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int); 124 static void if_start_deferred(void *context, int pending); 125 static void do_link_state_change(void *, int); 126 static int if_getgroup(struct ifgroupreq *, struct ifnet *); 127 static int if_getgroupmembers(struct ifgroupreq *); 128 #ifdef INET6 129 /* 130 * XXX: declare here to avoid to include many inet6 related files.. 131 * should be more generalized? 132 */ 133 extern void nd6_setmtu(struct ifnet *); 134 #endif 135 136 int if_index = 0; 137 struct ifindex_entry *ifindex_table = NULL; 138 int ifqmaxlen = IFQ_MAXLEN; 139 struct ifnethead ifnet; /* depend on static init XXX */ 140 struct ifgrouphead ifg_head; 141 struct mtx ifnet_lock; 142 static if_com_alloc_t *if_com_alloc[256]; 143 static if_com_free_t *if_com_free[256]; 144 145 static int if_indexlim = 8; 146 static struct knlist ifklist; 147 148 static void filt_netdetach(struct knote *kn); 149 static int filt_netdev(struct knote *kn, long hint); 150 151 static struct filterops netdev_filtops = 152 { 1, NULL, filt_netdetach, filt_netdev }; 153 154 /* 155 * System initialization 156 */ 157 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL) 158 SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL) 159 160 MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals"); 161 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 162 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 163 164 static d_open_t netopen; 165 static d_close_t netclose; 166 static d_ioctl_t netioctl; 167 static d_kqfilter_t netkqfilter; 168 169 static struct cdevsw net_cdevsw = { 170 .d_version = D_VERSION, 171 .d_flags = D_NEEDGIANT, 172 .d_open = netopen, 173 .d_close = netclose, 174 .d_ioctl = netioctl, 175 .d_name = "net", 176 .d_kqfilter = netkqfilter, 177 }; 178 179 static int 180 netopen(struct cdev *dev, int flag, int mode, struct thread *td) 181 { 182 return (0); 183 } 184 185 static int 186 netclose(struct cdev *dev, int flags, int fmt, struct thread *td) 187 { 188 return (0); 189 } 190 191 static int 192 netioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 193 { 194 struct ifnet *ifp; 195 int error, idx; 196 197 /* only support interface specific ioctls */ 198 if (IOCGROUP(cmd) != 'i') 199 return (EOPNOTSUPP); 200 idx = minor(dev); 201 if (idx == 0) { 202 /* 203 * special network device, not interface. 204 */ 205 if (cmd == SIOCGIFCONF) 206 return (ifconf(cmd, data)); /* XXX remove cmd */ 207 #ifdef __amd64__ 208 if (cmd == SIOCGIFCONF32) 209 return (ifconf(cmd, data)); /* XXX remove cmd */ 210 #endif 211 return (EOPNOTSUPP); 212 } 213 214 ifp = ifnet_byindex(idx); 215 if (ifp == NULL) 216 return (ENXIO); 217 218 error = ifhwioctl(cmd, ifp, data, td); 219 if (error == ENOIOCTL) 220 error = EOPNOTSUPP; 221 return (error); 222 } 223 224 static int 225 netkqfilter(struct cdev *dev, struct knote *kn) 226 { 227 struct knlist *klist; 228 struct ifnet *ifp; 229 int idx; 230 231 switch (kn->kn_filter) { 232 case EVFILT_NETDEV: 233 kn->kn_fop = &netdev_filtops; 234 break; 235 default: 236 return (EINVAL); 237 } 238 239 idx = minor(dev); 240 if (idx == 0) { 241 klist = &ifklist; 242 } else { 243 ifp = ifnet_byindex(idx); 244 if (ifp == NULL) 245 return (1); 246 klist = &ifp->if_klist; 247 } 248 249 kn->kn_hook = (caddr_t)klist; 250 251 knlist_add(klist, kn, 0); 252 253 return (0); 254 } 255 256 static void 257 filt_netdetach(struct knote *kn) 258 { 259 struct knlist *klist = (struct knlist *)kn->kn_hook; 260 261 knlist_remove(klist, kn, 0); 262 } 263 264 static int 265 filt_netdev(struct knote *kn, long hint) 266 { 267 struct knlist *klist = (struct knlist *)kn->kn_hook; 268 269 /* 270 * Currently NOTE_EXIT is abused to indicate device detach. 271 */ 272 if (hint == NOTE_EXIT) { 273 kn->kn_data = NOTE_LINKINV; 274 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 275 knlist_remove_inevent(klist, kn); 276 return (1); 277 } 278 if (hint != 0) 279 kn->kn_data = hint; /* current status */ 280 if (kn->kn_sfflags & hint) 281 kn->kn_fflags |= hint; 282 return (kn->kn_fflags != 0); 283 } 284 285 /* 286 * Network interface utility routines. 287 * 288 * Routines with ifa_ifwith* names take sockaddr *'s as 289 * parameters. 290 */ 291 /* ARGSUSED*/ 292 static void 293 if_init(void *dummy __unused) 294 { 295 296 IFNET_LOCK_INIT(); 297 TAILQ_INIT(&ifnet); 298 TAILQ_INIT(&ifg_head); 299 knlist_init(&ifklist, NULL, NULL, NULL, NULL); 300 if_grow(); /* create initial table */ 301 ifdev_byindex(0) = make_dev(&net_cdevsw, 0, 302 UID_ROOT, GID_WHEEL, 0600, "network"); 303 if_clone_init(); 304 } 305 306 static void 307 if_grow(void) 308 { 309 u_int n; 310 struct ifindex_entry *e; 311 312 if_indexlim <<= 1; 313 n = if_indexlim * sizeof(*e); 314 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO); 315 if (ifindex_table != NULL) { 316 memcpy((caddr_t)e, (caddr_t)ifindex_table, n/2); 317 free((caddr_t)ifindex_table, M_IFNET); 318 } 319 ifindex_table = e; 320 } 321 322 /* ARGSUSED*/ 323 static void 324 if_check(void *dummy __unused) 325 { 326 struct ifnet *ifp; 327 int s; 328 329 s = splimp(); 330 IFNET_RLOCK(); /* could sleep on rare error; mostly okay XXX */ 331 TAILQ_FOREACH(ifp, &ifnet, if_link) { 332 if (ifp->if_snd.ifq_maxlen == 0) { 333 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n"); 334 ifp->if_snd.ifq_maxlen = ifqmaxlen; 335 } 336 if (!mtx_initialized(&ifp->if_snd.ifq_mtx)) { 337 if_printf(ifp, 338 "XXX: driver didn't initialize queue mtx\n"); 339 mtx_init(&ifp->if_snd.ifq_mtx, "unknown", 340 MTX_NETWORK_LOCK, MTX_DEF); 341 } 342 } 343 IFNET_RUNLOCK(); 344 splx(s); 345 if_slowtimo(0); 346 } 347 348 /* 349 * Allocate a struct ifnet and in index for an interface. 350 */ 351 struct ifnet* 352 if_alloc(u_char type) 353 { 354 struct ifnet *ifp; 355 356 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO); 357 358 /* 359 * Try to find an empty slot below if_index. If we fail, take 360 * the next slot. 361 * 362 * XXX: should be locked! 363 */ 364 for (ifp->if_index = 1; ifp->if_index <= if_index; ifp->if_index++) { 365 if (ifnet_byindex(ifp->if_index) == NULL) 366 break; 367 } 368 /* Catch if_index overflow. */ 369 if (ifp->if_index < 1) { 370 free(ifp, M_IFNET); 371 return (NULL); 372 } 373 if (ifp->if_index > if_index) 374 if_index = ifp->if_index; 375 if (if_index >= if_indexlim) 376 if_grow(); 377 ifnet_byindex(ifp->if_index) = ifp; 378 379 ifp->if_type = type; 380 381 if (if_com_alloc[type] != NULL) { 382 ifp->if_l2com = if_com_alloc[type](type, ifp); 383 if (ifp->if_l2com == NULL) { 384 free(ifp, M_IFNET); 385 return (NULL); 386 } 387 } 388 IF_ADDR_LOCK_INIT(ifp); 389 390 return (ifp); 391 } 392 393 void 394 if_free(struct ifnet *ifp) 395 { 396 397 /* Do not add code to this function! Add it to if_free_type(). */ 398 if_free_type(ifp, ifp->if_type); 399 } 400 401 void 402 if_free_type(struct ifnet *ifp, u_char type) 403 { 404 405 if (ifp != ifnet_byindex(ifp->if_index)) { 406 if_printf(ifp, "%s: value was not if_alloced, skipping\n", 407 __func__); 408 return; 409 } 410 411 IF_ADDR_LOCK_DESTROY(ifp); 412 413 ifnet_byindex(ifp->if_index) = NULL; 414 415 /* XXX: should be locked with if_findindex() */ 416 while (if_index > 0 && ifnet_byindex(if_index) == NULL) 417 if_index--; 418 419 if (if_com_free[type] != NULL) 420 if_com_free[type](ifp->if_l2com, type); 421 422 free(ifp, M_IFNET); 423 }; 424 425 /* 426 * Attach an interface to the 427 * list of "active" interfaces. 428 */ 429 void 430 if_attach(struct ifnet *ifp) 431 { 432 unsigned socksize, ifasize; 433 int namelen, masklen; 434 struct sockaddr_dl *sdl; 435 struct ifaddr *ifa; 436 437 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index)) 438 panic ("%s: BUG: if_attach called without if_alloc'd input()\n", 439 ifp->if_xname); 440 441 TASK_INIT(&ifp->if_starttask, 0, if_start_deferred, ifp); 442 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp); 443 IF_AFDATA_LOCK_INIT(ifp); 444 ifp->if_afdata_initialized = 0; 445 /* 446 * XXX - 447 * The old code would work if the interface passed a pre-existing 448 * chain of ifaddrs to this code. We don't trust our callers to 449 * properly initialize the tailq, however, so we no longer allow 450 * this unlikely case. 451 */ 452 TAILQ_INIT(&ifp->if_addrhead); 453 TAILQ_INIT(&ifp->if_prefixhead); 454 TAILQ_INIT(&ifp->if_multiaddrs); 455 TAILQ_INIT(&ifp->if_groups); 456 457 if_addgroup(ifp, IFG_ALL); 458 459 knlist_init(&ifp->if_klist, NULL, NULL, NULL, NULL); 460 getmicrotime(&ifp->if_lastchange); 461 ifp->if_data.ifi_epoch = time_uptime; 462 ifp->if_data.ifi_datalen = sizeof(struct if_data); 463 464 #ifdef MAC 465 mac_init_ifnet(ifp); 466 mac_create_ifnet(ifp); 467 #endif 468 469 ifdev_byindex(ifp->if_index) = make_dev(&net_cdevsw, 470 unit2minor(ifp->if_index), 471 UID_ROOT, GID_WHEEL, 0600, "%s/%s", 472 net_cdevsw.d_name, ifp->if_xname); 473 make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d", 474 net_cdevsw.d_name, ifp->if_index); 475 476 mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF); 477 478 /* 479 * create a Link Level name for this device 480 */ 481 namelen = strlen(ifp->if_xname); 482 /* 483 * Always save enough space for any possiable name so we can do 484 * a rename in place later. 485 */ 486 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ; 487 socksize = masklen + ifp->if_addrlen; 488 if (socksize < sizeof(*sdl)) 489 socksize = sizeof(*sdl); 490 socksize = roundup2(socksize, sizeof(long)); 491 ifasize = sizeof(*ifa) + 2 * socksize; 492 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO); 493 IFA_LOCK_INIT(ifa); 494 sdl = (struct sockaddr_dl *)(ifa + 1); 495 sdl->sdl_len = socksize; 496 sdl->sdl_family = AF_LINK; 497 bcopy(ifp->if_xname, sdl->sdl_data, namelen); 498 sdl->sdl_nlen = namelen; 499 sdl->sdl_index = ifp->if_index; 500 sdl->sdl_type = ifp->if_type; 501 ifp->if_addr = ifa; 502 ifa->ifa_ifp = ifp; 503 ifa->ifa_rtrequest = link_rtrequest; 504 ifa->ifa_addr = (struct sockaddr *)sdl; 505 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 506 ifa->ifa_netmask = (struct sockaddr *)sdl; 507 sdl->sdl_len = masklen; 508 while (namelen != 0) 509 sdl->sdl_data[--namelen] = 0xff; 510 ifa->ifa_refcnt = 1; 511 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link); 512 ifp->if_broadcastaddr = NULL; /* reliably crash if used uninitialized */ 513 ifp->if_snd.altq_type = 0; 514 ifp->if_snd.altq_disc = NULL; 515 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE; 516 ifp->if_snd.altq_tbr = NULL; 517 ifp->if_snd.altq_ifp = ifp; 518 519 IFNET_WLOCK(); 520 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link); 521 IFNET_WUNLOCK(); 522 523 if (domain_init_status >= 2) 524 if_attachdomain1(ifp); 525 526 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 527 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL); 528 529 /* Announce the interface. */ 530 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 531 532 if (ifp->if_watchdog != NULL) 533 if_printf(ifp, "using obsoleted if_watchdog interface\n"); 534 } 535 536 static void 537 if_attachdomain(void *dummy) 538 { 539 struct ifnet *ifp; 540 int s; 541 542 s = splnet(); 543 TAILQ_FOREACH(ifp, &ifnet, if_link) 544 if_attachdomain1(ifp); 545 splx(s); 546 } 547 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND, 548 if_attachdomain, NULL); 549 550 static void 551 if_attachdomain1(struct ifnet *ifp) 552 { 553 struct domain *dp; 554 int s; 555 556 s = splnet(); 557 558 /* 559 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we 560 * cannot lock ifp->if_afdata initialization, entirely. 561 */ 562 if (IF_AFDATA_TRYLOCK(ifp) == 0) { 563 splx(s); 564 return; 565 } 566 if (ifp->if_afdata_initialized >= domain_init_status) { 567 IF_AFDATA_UNLOCK(ifp); 568 splx(s); 569 printf("if_attachdomain called more than once on %s\n", 570 ifp->if_xname); 571 return; 572 } 573 ifp->if_afdata_initialized = domain_init_status; 574 IF_AFDATA_UNLOCK(ifp); 575 576 /* address family dependent data region */ 577 bzero(ifp->if_afdata, sizeof(ifp->if_afdata)); 578 for (dp = domains; dp; dp = dp->dom_next) { 579 if (dp->dom_ifattach) 580 ifp->if_afdata[dp->dom_family] = 581 (*dp->dom_ifattach)(ifp); 582 } 583 584 splx(s); 585 } 586 587 /* 588 * Remove any unicast or broadcast network addresses from an interface. 589 */ 590 591 void 592 if_purgeaddrs(struct ifnet *ifp) 593 { 594 struct ifaddr *ifa, *next; 595 596 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) { 597 if (ifa->ifa_addr->sa_family == AF_LINK) 598 continue; 599 #ifdef INET 600 /* XXX: Ugly!! ad hoc just for INET */ 601 if (ifa->ifa_addr->sa_family == AF_INET) { 602 struct ifaliasreq ifr; 603 604 bzero(&ifr, sizeof(ifr)); 605 ifr.ifra_addr = *ifa->ifa_addr; 606 if (ifa->ifa_dstaddr) 607 ifr.ifra_broadaddr = *ifa->ifa_dstaddr; 608 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp, 609 NULL) == 0) 610 continue; 611 } 612 #endif /* INET */ 613 #ifdef INET6 614 if (ifa->ifa_addr->sa_family == AF_INET6) { 615 in6_purgeaddr(ifa); 616 /* ifp_addrhead is already updated */ 617 continue; 618 } 619 #endif /* INET6 */ 620 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 621 IFAFREE(ifa); 622 } 623 } 624 625 /* 626 * Remove any multicast network addresses from an interface. 627 */ 628 static void 629 if_purgemaddrs(struct ifnet *ifp) 630 { 631 struct ifmultiaddr *ifma; 632 struct ifmultiaddr *next; 633 634 IF_ADDR_LOCK(ifp); 635 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) 636 if_delmulti_locked(ifp, ifma, 1); 637 IF_ADDR_UNLOCK(ifp); 638 } 639 640 /* 641 * Detach an interface, removing it from the 642 * list of "active" interfaces. 643 * 644 * XXXRW: There are some significant questions about event ordering, and 645 * how to prevent things from starting to use the interface during detach. 646 */ 647 void 648 if_detach(struct ifnet *ifp) 649 { 650 struct ifaddr *ifa; 651 struct radix_node_head *rnh; 652 int s; 653 int i; 654 struct domain *dp; 655 struct ifnet *iter; 656 int found = 0; 657 658 IFNET_WLOCK(); 659 TAILQ_FOREACH(iter, &ifnet, if_link) 660 if (iter == ifp) { 661 TAILQ_REMOVE(&ifnet, ifp, if_link); 662 found = 1; 663 break; 664 } 665 IFNET_WUNLOCK(); 666 if (!found) 667 return; 668 669 /* 670 * Remove/wait for pending events. 671 */ 672 taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 673 674 /* 675 * Remove routes and flush queues. 676 */ 677 s = splnet(); 678 if_down(ifp); 679 #ifdef ALTQ 680 if (ALTQ_IS_ENABLED(&ifp->if_snd)) 681 altq_disable(&ifp->if_snd); 682 if (ALTQ_IS_ATTACHED(&ifp->if_snd)) 683 altq_detach(&ifp->if_snd); 684 #endif 685 686 if_purgeaddrs(ifp); 687 688 #ifdef INET 689 in_ifdetach(ifp); 690 #endif 691 692 #ifdef INET6 693 /* 694 * Remove all IPv6 kernel structs related to ifp. This should be done 695 * before removing routing entries below, since IPv6 interface direct 696 * routes are expected to be removed by the IPv6-specific kernel API. 697 * Otherwise, the kernel will detect some inconsistency and bark it. 698 */ 699 in6_ifdetach(ifp); 700 #endif 701 if_purgemaddrs(ifp); 702 703 /* 704 * Remove link ifaddr pointer and maybe decrement if_index. 705 * Clean up all addresses. 706 */ 707 ifp->if_addr = NULL; 708 destroy_dev(ifdev_byindex(ifp->if_index)); 709 ifdev_byindex(ifp->if_index) = NULL; 710 711 /* We can now free link ifaddr. */ 712 if (!TAILQ_EMPTY(&ifp->if_addrhead)) { 713 ifa = TAILQ_FIRST(&ifp->if_addrhead); 714 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 715 IFAFREE(ifa); 716 } 717 718 /* 719 * Delete all remaining routes using this interface 720 * Unfortuneatly the only way to do this is to slog through 721 * the entire routing table looking for routes which point 722 * to this interface...oh well... 723 */ 724 for (i = 1; i <= AF_MAX; i++) { 725 if ((rnh = rt_tables[i]) == NULL) 726 continue; 727 RADIX_NODE_HEAD_LOCK(rnh); 728 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp); 729 RADIX_NODE_HEAD_UNLOCK(rnh); 730 } 731 732 /* Announce that the interface is gone. */ 733 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 734 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 735 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL); 736 737 IF_AFDATA_LOCK(ifp); 738 for (dp = domains; dp; dp = dp->dom_next) { 739 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) 740 (*dp->dom_ifdetach)(ifp, 741 ifp->if_afdata[dp->dom_family]); 742 } 743 IF_AFDATA_UNLOCK(ifp); 744 745 #ifdef MAC 746 mac_destroy_ifnet(ifp); 747 #endif /* MAC */ 748 KNOTE_UNLOCKED(&ifp->if_klist, NOTE_EXIT); 749 knlist_clear(&ifp->if_klist, 0); 750 knlist_destroy(&ifp->if_klist); 751 mtx_destroy(&ifp->if_snd.ifq_mtx); 752 IF_AFDATA_DESTROY(ifp); 753 splx(s); 754 } 755 756 /* 757 * Add a group to an interface 758 */ 759 int 760 if_addgroup(struct ifnet *ifp, const char *groupname) 761 { 762 struct ifg_list *ifgl; 763 struct ifg_group *ifg = NULL; 764 struct ifg_member *ifgm; 765 766 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' && 767 groupname[strlen(groupname) - 1] <= '9') 768 return (EINVAL); 769 770 IFNET_WLOCK(); 771 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 772 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) { 773 IFNET_WUNLOCK(); 774 return (EEXIST); 775 } 776 777 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP, 778 M_NOWAIT)) == NULL) { 779 IFNET_WUNLOCK(); 780 return (ENOMEM); 781 } 782 783 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member), 784 M_TEMP, M_NOWAIT)) == NULL) { 785 free(ifgl, M_TEMP); 786 IFNET_WUNLOCK(); 787 return (ENOMEM); 788 } 789 790 TAILQ_FOREACH(ifg, &ifg_head, ifg_next) 791 if (!strcmp(ifg->ifg_group, groupname)) 792 break; 793 794 if (ifg == NULL) { 795 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group), 796 M_TEMP, M_NOWAIT)) == NULL) { 797 free(ifgl, M_TEMP); 798 free(ifgm, M_TEMP); 799 IFNET_WUNLOCK(); 800 return (ENOMEM); 801 } 802 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group)); 803 ifg->ifg_refcnt = 0; 804 TAILQ_INIT(&ifg->ifg_members); 805 EVENTHANDLER_INVOKE(group_attach_event, ifg); 806 TAILQ_INSERT_TAIL(&ifg_head, ifg, ifg_next); 807 } 808 809 ifg->ifg_refcnt++; 810 ifgl->ifgl_group = ifg; 811 ifgm->ifgm_ifp = ifp; 812 813 IF_ADDR_LOCK(ifp); 814 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next); 815 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next); 816 IF_ADDR_UNLOCK(ifp); 817 818 IFNET_WUNLOCK(); 819 820 EVENTHANDLER_INVOKE(group_change_event, groupname); 821 822 return (0); 823 } 824 825 /* 826 * Remove a group from an interface 827 */ 828 int 829 if_delgroup(struct ifnet *ifp, const char *groupname) 830 { 831 struct ifg_list *ifgl; 832 struct ifg_member *ifgm; 833 834 IFNET_WLOCK(); 835 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 836 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) 837 break; 838 if (ifgl == NULL) { 839 IFNET_WUNLOCK(); 840 return (ENOENT); 841 } 842 843 IF_ADDR_LOCK(ifp); 844 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next); 845 IF_ADDR_UNLOCK(ifp); 846 847 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) 848 if (ifgm->ifgm_ifp == ifp) 849 break; 850 851 if (ifgm != NULL) { 852 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next); 853 free(ifgm, M_TEMP); 854 } 855 856 if (--ifgl->ifgl_group->ifg_refcnt == 0) { 857 TAILQ_REMOVE(&ifg_head, ifgl->ifgl_group, ifg_next); 858 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group); 859 free(ifgl->ifgl_group, M_TEMP); 860 } 861 IFNET_WUNLOCK(); 862 863 free(ifgl, M_TEMP); 864 865 EVENTHANDLER_INVOKE(group_change_event, groupname); 866 867 return (0); 868 } 869 870 /* 871 * Stores all groups from an interface in memory pointed 872 * to by data 873 */ 874 static int 875 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp) 876 { 877 int len, error; 878 struct ifg_list *ifgl; 879 struct ifg_req ifgrq, *ifgp; 880 struct ifgroupreq *ifgr = data; 881 882 if (ifgr->ifgr_len == 0) { 883 IF_ADDR_LOCK(ifp); 884 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 885 ifgr->ifgr_len += sizeof(struct ifg_req); 886 IF_ADDR_UNLOCK(ifp); 887 return (0); 888 } 889 890 len = ifgr->ifgr_len; 891 ifgp = ifgr->ifgr_groups; 892 /* XXX: wire */ 893 IF_ADDR_LOCK(ifp); 894 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) { 895 if (len < sizeof(ifgrq)) { 896 IF_ADDR_UNLOCK(ifp); 897 return (EINVAL); 898 } 899 bzero(&ifgrq, sizeof ifgrq); 900 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group, 901 sizeof(ifgrq.ifgrq_group)); 902 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 903 IF_ADDR_UNLOCK(ifp); 904 return (error); 905 } 906 len -= sizeof(ifgrq); 907 ifgp++; 908 } 909 IF_ADDR_UNLOCK(ifp); 910 911 return (0); 912 } 913 914 /* 915 * Stores all members of a group in memory pointed to by data 916 */ 917 static int 918 if_getgroupmembers(struct ifgroupreq *data) 919 { 920 struct ifgroupreq *ifgr = data; 921 struct ifg_group *ifg; 922 struct ifg_member *ifgm; 923 struct ifg_req ifgrq, *ifgp; 924 int len, error; 925 926 IFNET_RLOCK(); 927 TAILQ_FOREACH(ifg, &ifg_head, ifg_next) 928 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name)) 929 break; 930 if (ifg == NULL) { 931 IFNET_RUNLOCK(); 932 return (ENOENT); 933 } 934 935 if (ifgr->ifgr_len == 0) { 936 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) 937 ifgr->ifgr_len += sizeof(ifgrq); 938 IFNET_RUNLOCK(); 939 return (0); 940 } 941 942 len = ifgr->ifgr_len; 943 ifgp = ifgr->ifgr_groups; 944 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) { 945 if (len < sizeof(ifgrq)) { 946 IFNET_RUNLOCK(); 947 return (EINVAL); 948 } 949 bzero(&ifgrq, sizeof ifgrq); 950 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname, 951 sizeof(ifgrq.ifgrq_member)); 952 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 953 IFNET_RUNLOCK(); 954 return (error); 955 } 956 len -= sizeof(ifgrq); 957 ifgp++; 958 } 959 IFNET_RUNLOCK(); 960 961 return (0); 962 } 963 964 /* 965 * Delete Routes for a Network Interface 966 * 967 * Called for each routing entry via the rnh->rnh_walktree() call above 968 * to delete all route entries referencing a detaching network interface. 969 * 970 * Arguments: 971 * rn pointer to node in the routing table 972 * arg argument passed to rnh->rnh_walktree() - detaching interface 973 * 974 * Returns: 975 * 0 successful 976 * errno failed - reason indicated 977 * 978 */ 979 static int 980 if_rtdel(struct radix_node *rn, void *arg) 981 { 982 struct rtentry *rt = (struct rtentry *)rn; 983 struct ifnet *ifp = arg; 984 int err; 985 986 if (rt->rt_ifp == ifp) { 987 988 /* 989 * Protect (sorta) against walktree recursion problems 990 * with cloned routes 991 */ 992 if ((rt->rt_flags & RTF_UP) == 0) 993 return (0); 994 995 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, 996 rt_mask(rt), rt->rt_flags, 997 (struct rtentry **) NULL); 998 if (err) { 999 log(LOG_WARNING, "if_rtdel: error %d\n", err); 1000 } 1001 } 1002 1003 return (0); 1004 } 1005 1006 /* 1007 * XXX: Because sockaddr_dl has deeper structure than the sockaddr 1008 * structs used to represent other address families, it is necessary 1009 * to perform a different comparison. 1010 */ 1011 1012 #define sa_equal(a1, a2) \ 1013 (bcmp((a1), (a2), ((a1))->sa_len) == 0) 1014 1015 #define sa_dl_equal(a1, a2) \ 1016 ((((struct sockaddr_dl *)(a1))->sdl_len == \ 1017 ((struct sockaddr_dl *)(a2))->sdl_len) && \ 1018 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \ 1019 LLADDR((struct sockaddr_dl *)(a2)), \ 1020 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0)) 1021 1022 /* 1023 * Locate an interface based on a complete address. 1024 */ 1025 /*ARGSUSED*/ 1026 struct ifaddr * 1027 ifa_ifwithaddr(struct sockaddr *addr) 1028 { 1029 struct ifnet *ifp; 1030 struct ifaddr *ifa; 1031 1032 IFNET_RLOCK(); 1033 TAILQ_FOREACH(ifp, &ifnet, if_link) 1034 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1035 if (ifa->ifa_addr->sa_family != addr->sa_family) 1036 continue; 1037 if (sa_equal(addr, ifa->ifa_addr)) 1038 goto done; 1039 /* IP6 doesn't have broadcast */ 1040 if ((ifp->if_flags & IFF_BROADCAST) && 1041 ifa->ifa_broadaddr && 1042 ifa->ifa_broadaddr->sa_len != 0 && 1043 sa_equal(ifa->ifa_broadaddr, addr)) 1044 goto done; 1045 } 1046 ifa = NULL; 1047 done: 1048 IFNET_RUNLOCK(); 1049 return (ifa); 1050 } 1051 1052 /* 1053 * Locate an interface based on the broadcast address. 1054 */ 1055 /* ARGSUSED */ 1056 struct ifaddr * 1057 ifa_ifwithbroadaddr(struct sockaddr *addr) 1058 { 1059 struct ifnet *ifp; 1060 struct ifaddr *ifa; 1061 1062 IFNET_RLOCK(); 1063 TAILQ_FOREACH(ifp, &ifnet, if_link) 1064 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1065 if (ifa->ifa_addr->sa_family != addr->sa_family) 1066 continue; 1067 if ((ifp->if_flags & IFF_BROADCAST) && 1068 ifa->ifa_broadaddr && 1069 ifa->ifa_broadaddr->sa_len != 0 && 1070 sa_equal(ifa->ifa_broadaddr, addr)) 1071 goto done; 1072 } 1073 ifa = NULL; 1074 done: 1075 IFNET_RUNLOCK(); 1076 return (ifa); 1077 } 1078 1079 /* 1080 * Locate the point to point interface with a given destination address. 1081 */ 1082 /*ARGSUSED*/ 1083 struct ifaddr * 1084 ifa_ifwithdstaddr(struct sockaddr *addr) 1085 { 1086 struct ifnet *ifp; 1087 struct ifaddr *ifa; 1088 1089 IFNET_RLOCK(); 1090 TAILQ_FOREACH(ifp, &ifnet, if_link) { 1091 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 1092 continue; 1093 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1094 if (ifa->ifa_addr->sa_family != addr->sa_family) 1095 continue; 1096 if (ifa->ifa_dstaddr && 1097 sa_equal(addr, ifa->ifa_dstaddr)) 1098 goto done; 1099 } 1100 } 1101 ifa = NULL; 1102 done: 1103 IFNET_RUNLOCK(); 1104 return (ifa); 1105 } 1106 1107 /* 1108 * Find an interface on a specific network. If many, choice 1109 * is most specific found. 1110 */ 1111 struct ifaddr * 1112 ifa_ifwithnet(struct sockaddr *addr) 1113 { 1114 struct ifnet *ifp; 1115 struct ifaddr *ifa; 1116 struct ifaddr *ifa_maybe = (struct ifaddr *) 0; 1117 u_int af = addr->sa_family; 1118 char *addr_data = addr->sa_data, *cplim; 1119 1120 /* 1121 * AF_LINK addresses can be looked up directly by their index number, 1122 * so do that if we can. 1123 */ 1124 if (af == AF_LINK) { 1125 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; 1126 if (sdl->sdl_index && sdl->sdl_index <= if_index) 1127 return (ifaddr_byindex(sdl->sdl_index)); 1128 } 1129 1130 /* 1131 * Scan though each interface, looking for ones that have 1132 * addresses in this address family. 1133 */ 1134 IFNET_RLOCK(); 1135 TAILQ_FOREACH(ifp, &ifnet, if_link) { 1136 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1137 char *cp, *cp2, *cp3; 1138 1139 if (ifa->ifa_addr->sa_family != af) 1140 next: continue; 1141 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) { 1142 /* 1143 * This is a bit broken as it doesn't 1144 * take into account that the remote end may 1145 * be a single node in the network we are 1146 * looking for. 1147 * The trouble is that we don't know the 1148 * netmask for the remote end. 1149 */ 1150 if (ifa->ifa_dstaddr != 0 && 1151 sa_equal(addr, ifa->ifa_dstaddr)) 1152 goto done; 1153 } else { 1154 /* 1155 * if we have a special address handler, 1156 * then use it instead of the generic one. 1157 */ 1158 if (ifa->ifa_claim_addr) { 1159 if ((*ifa->ifa_claim_addr)(ifa, addr)) 1160 goto done; 1161 continue; 1162 } 1163 1164 /* 1165 * Scan all the bits in the ifa's address. 1166 * If a bit dissagrees with what we are 1167 * looking for, mask it with the netmask 1168 * to see if it really matters. 1169 * (A byte at a time) 1170 */ 1171 if (ifa->ifa_netmask == 0) 1172 continue; 1173 cp = addr_data; 1174 cp2 = ifa->ifa_addr->sa_data; 1175 cp3 = ifa->ifa_netmask->sa_data; 1176 cplim = ifa->ifa_netmask->sa_len 1177 + (char *)ifa->ifa_netmask; 1178 while (cp3 < cplim) 1179 if ((*cp++ ^ *cp2++) & *cp3++) 1180 goto next; /* next address! */ 1181 /* 1182 * If the netmask of what we just found 1183 * is more specific than what we had before 1184 * (if we had one) then remember the new one 1185 * before continuing to search 1186 * for an even better one. 1187 */ 1188 if (ifa_maybe == 0 || 1189 rn_refines((caddr_t)ifa->ifa_netmask, 1190 (caddr_t)ifa_maybe->ifa_netmask)) 1191 ifa_maybe = ifa; 1192 } 1193 } 1194 } 1195 ifa = ifa_maybe; 1196 done: 1197 IFNET_RUNLOCK(); 1198 return (ifa); 1199 } 1200 1201 /* 1202 * Find an interface address specific to an interface best matching 1203 * a given address. 1204 */ 1205 struct ifaddr * 1206 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp) 1207 { 1208 struct ifaddr *ifa; 1209 char *cp, *cp2, *cp3; 1210 char *cplim; 1211 struct ifaddr *ifa_maybe = 0; 1212 u_int af = addr->sa_family; 1213 1214 if (af >= AF_MAX) 1215 return (0); 1216 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1217 if (ifa->ifa_addr->sa_family != af) 1218 continue; 1219 if (ifa_maybe == 0) 1220 ifa_maybe = ifa; 1221 if (ifa->ifa_netmask == 0) { 1222 if (sa_equal(addr, ifa->ifa_addr) || 1223 (ifa->ifa_dstaddr && 1224 sa_equal(addr, ifa->ifa_dstaddr))) 1225 goto done; 1226 continue; 1227 } 1228 if (ifp->if_flags & IFF_POINTOPOINT) { 1229 if (sa_equal(addr, ifa->ifa_dstaddr)) 1230 goto done; 1231 } else { 1232 cp = addr->sa_data; 1233 cp2 = ifa->ifa_addr->sa_data; 1234 cp3 = ifa->ifa_netmask->sa_data; 1235 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 1236 for (; cp3 < cplim; cp3++) 1237 if ((*cp++ ^ *cp2++) & *cp3) 1238 break; 1239 if (cp3 == cplim) 1240 goto done; 1241 } 1242 } 1243 ifa = ifa_maybe; 1244 done: 1245 return (ifa); 1246 } 1247 1248 #include <net/route.h> 1249 1250 /* 1251 * Default action when installing a route with a Link Level gateway. 1252 * Lookup an appropriate real ifa to point to. 1253 * This should be moved to /sys/net/link.c eventually. 1254 */ 1255 static void 1256 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 1257 { 1258 struct ifaddr *ifa, *oifa; 1259 struct sockaddr *dst; 1260 struct ifnet *ifp; 1261 1262 RT_LOCK_ASSERT(rt); 1263 1264 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 1265 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 1266 return; 1267 ifa = ifaof_ifpforaddr(dst, ifp); 1268 if (ifa) { 1269 IFAREF(ifa); /* XXX */ 1270 oifa = rt->rt_ifa; 1271 rt->rt_ifa = ifa; 1272 IFAFREE(oifa); 1273 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 1274 ifa->ifa_rtrequest(cmd, rt, info); 1275 } 1276 } 1277 1278 /* 1279 * Mark an interface down and notify protocols of 1280 * the transition. 1281 * NOTE: must be called at splnet or eqivalent. 1282 */ 1283 static void 1284 if_unroute(struct ifnet *ifp, int flag, int fam) 1285 { 1286 struct ifaddr *ifa; 1287 1288 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP")); 1289 1290 ifp->if_flags &= ~flag; 1291 getmicrotime(&ifp->if_lastchange); 1292 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1293 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1294 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 1295 if_qflush(&ifp->if_snd); 1296 #ifdef DEV_CARP 1297 if (ifp->if_carp) 1298 carp_carpdev_state(ifp->if_carp); 1299 #endif 1300 rt_ifmsg(ifp); 1301 } 1302 1303 /* 1304 * Mark an interface up and notify protocols of 1305 * the transition. 1306 * NOTE: must be called at splnet or eqivalent. 1307 */ 1308 static void 1309 if_route(struct ifnet *ifp, int flag, int fam) 1310 { 1311 struct ifaddr *ifa; 1312 1313 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP")); 1314 1315 ifp->if_flags |= flag; 1316 getmicrotime(&ifp->if_lastchange); 1317 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1318 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1319 pfctlinput(PRC_IFUP, ifa->ifa_addr); 1320 #ifdef DEV_CARP 1321 if (ifp->if_carp) 1322 carp_carpdev_state(ifp->if_carp); 1323 #endif 1324 rt_ifmsg(ifp); 1325 #ifdef INET6 1326 in6_if_up(ifp); 1327 #endif 1328 } 1329 1330 void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */ 1331 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */ 1332 1333 /* 1334 * Handle a change in the interface link state. To avoid LORs 1335 * between driver lock and upper layer locks, as well as possible 1336 * recursions, we post event to taskqueue, and all job 1337 * is done in static do_link_state_change(). 1338 */ 1339 void 1340 if_link_state_change(struct ifnet *ifp, int link_state) 1341 { 1342 /* Return if state hasn't changed. */ 1343 if (ifp->if_link_state == link_state) 1344 return; 1345 1346 ifp->if_link_state = link_state; 1347 1348 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask); 1349 } 1350 1351 static void 1352 do_link_state_change(void *arg, int pending) 1353 { 1354 struct ifnet *ifp = (struct ifnet *)arg; 1355 int link_state = ifp->if_link_state; 1356 int link; 1357 1358 /* Notify that the link state has changed. */ 1359 rt_ifmsg(ifp); 1360 if (link_state == LINK_STATE_UP) 1361 link = NOTE_LINKUP; 1362 else if (link_state == LINK_STATE_DOWN) 1363 link = NOTE_LINKDOWN; 1364 else 1365 link = NOTE_LINKINV; 1366 KNOTE_UNLOCKED(&ifp->if_klist, link); 1367 if (ifp->if_vlantrunk != NULL) 1368 (*vlan_link_state_p)(ifp, link); 1369 1370 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) && 1371 IFP2AC(ifp)->ac_netgraph != NULL) 1372 (*ng_ether_link_state_p)(ifp, link_state); 1373 #ifdef DEV_CARP 1374 if (ifp->if_carp) 1375 carp_carpdev_state(ifp->if_carp); 1376 #endif 1377 if (ifp->if_bridge) { 1378 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!")); 1379 (*bstp_linkstate_p)(ifp, link_state); 1380 } 1381 1382 devctl_notify("IFNET", ifp->if_xname, 1383 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL); 1384 if (pending > 1) 1385 if_printf(ifp, "%d link states coalesced\n", pending); 1386 if (log_link_state_change) 1387 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname, 1388 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" ); 1389 } 1390 1391 /* 1392 * Mark an interface down and notify protocols of 1393 * the transition. 1394 * NOTE: must be called at splnet or eqivalent. 1395 */ 1396 void 1397 if_down(struct ifnet *ifp) 1398 { 1399 1400 if_unroute(ifp, IFF_UP, AF_UNSPEC); 1401 } 1402 1403 /* 1404 * Mark an interface up and notify protocols of 1405 * the transition. 1406 * NOTE: must be called at splnet or eqivalent. 1407 */ 1408 void 1409 if_up(struct ifnet *ifp) 1410 { 1411 1412 if_route(ifp, IFF_UP, AF_UNSPEC); 1413 } 1414 1415 /* 1416 * Flush an interface queue. 1417 */ 1418 static void 1419 if_qflush(struct ifaltq *ifq) 1420 { 1421 struct mbuf *m, *n; 1422 1423 IFQ_LOCK(ifq); 1424 #ifdef ALTQ 1425 if (ALTQ_IS_ENABLED(ifq)) 1426 ALTQ_PURGE(ifq); 1427 #endif 1428 n = ifq->ifq_head; 1429 while ((m = n) != 0) { 1430 n = m->m_act; 1431 m_freem(m); 1432 } 1433 ifq->ifq_head = 0; 1434 ifq->ifq_tail = 0; 1435 ifq->ifq_len = 0; 1436 IFQ_UNLOCK(ifq); 1437 } 1438 1439 /* 1440 * Handle interface watchdog timer routines. Called 1441 * from softclock, we decrement timers (if set) and 1442 * call the appropriate interface routine on expiration. 1443 * 1444 * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called 1445 * holding Giant. If we switch to an MPSAFE callout, we likely need to grab 1446 * Giant before entering if_watchdog() on an IFF_NEEDSGIANT interface. 1447 */ 1448 static void 1449 if_slowtimo(void *arg) 1450 { 1451 struct ifnet *ifp; 1452 int s = splimp(); 1453 1454 IFNET_RLOCK(); 1455 TAILQ_FOREACH(ifp, &ifnet, if_link) { 1456 if (ifp->if_timer == 0 || --ifp->if_timer) 1457 continue; 1458 if (ifp->if_watchdog) 1459 (*ifp->if_watchdog)(ifp); 1460 } 1461 IFNET_RUNLOCK(); 1462 splx(s); 1463 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ); 1464 } 1465 1466 /* 1467 * Map interface name to 1468 * interface structure pointer. 1469 */ 1470 struct ifnet * 1471 ifunit(const char *name) 1472 { 1473 struct ifnet *ifp; 1474 1475 IFNET_RLOCK(); 1476 TAILQ_FOREACH(ifp, &ifnet, if_link) { 1477 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0) 1478 break; 1479 } 1480 IFNET_RUNLOCK(); 1481 return (ifp); 1482 } 1483 1484 /* 1485 * Hardware specific interface ioctls. 1486 */ 1487 static int 1488 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td) 1489 { 1490 struct ifreq *ifr; 1491 struct ifstat *ifs; 1492 int error = 0; 1493 int new_flags, temp_flags; 1494 size_t namelen, onamelen; 1495 char new_name[IFNAMSIZ]; 1496 struct ifaddr *ifa; 1497 struct sockaddr_dl *sdl; 1498 1499 ifr = (struct ifreq *)data; 1500 switch (cmd) { 1501 case SIOCGIFINDEX: 1502 ifr->ifr_index = ifp->if_index; 1503 break; 1504 1505 case SIOCGIFFLAGS: 1506 temp_flags = ifp->if_flags | ifp->if_drv_flags; 1507 ifr->ifr_flags = temp_flags & 0xffff; 1508 ifr->ifr_flagshigh = temp_flags >> 16; 1509 break; 1510 1511 case SIOCGIFCAP: 1512 ifr->ifr_reqcap = ifp->if_capabilities; 1513 ifr->ifr_curcap = ifp->if_capenable; 1514 break; 1515 1516 #ifdef MAC 1517 case SIOCGIFMAC: 1518 error = mac_ioctl_ifnet_get(td->td_ucred, ifr, ifp); 1519 break; 1520 #endif 1521 1522 case SIOCGIFMETRIC: 1523 ifr->ifr_metric = ifp->if_metric; 1524 break; 1525 1526 case SIOCGIFMTU: 1527 ifr->ifr_mtu = ifp->if_mtu; 1528 break; 1529 1530 case SIOCGIFPHYS: 1531 ifr->ifr_phys = ifp->if_physical; 1532 break; 1533 1534 case SIOCSIFFLAGS: 1535 error = priv_check(td, PRIV_NET_SETIFFLAGS); 1536 if (error) 1537 return (error); 1538 /* 1539 * Currently, no driver owned flags pass the IFF_CANTCHANGE 1540 * check, so we don't need special handling here yet. 1541 */ 1542 new_flags = (ifr->ifr_flags & 0xffff) | 1543 (ifr->ifr_flagshigh << 16); 1544 if (ifp->if_flags & IFF_SMART) { 1545 /* Smart drivers twiddle their own routes */ 1546 } else if (ifp->if_flags & IFF_UP && 1547 (new_flags & IFF_UP) == 0) { 1548 int s = splimp(); 1549 if_down(ifp); 1550 splx(s); 1551 } else if (new_flags & IFF_UP && 1552 (ifp->if_flags & IFF_UP) == 0) { 1553 int s = splimp(); 1554 if_up(ifp); 1555 splx(s); 1556 } 1557 /* See if permanently promiscuous mode bit is about to flip */ 1558 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) { 1559 if (new_flags & IFF_PPROMISC) 1560 ifp->if_flags |= IFF_PROMISC; 1561 else if (ifp->if_pcount == 0) 1562 ifp->if_flags &= ~IFF_PROMISC; 1563 log(LOG_INFO, "%s: permanently promiscuous mode %s\n", 1564 ifp->if_xname, 1565 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled"); 1566 } 1567 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 1568 (new_flags &~ IFF_CANTCHANGE); 1569 if (ifp->if_ioctl) { 1570 IFF_LOCKGIANT(ifp); 1571 (void) (*ifp->if_ioctl)(ifp, cmd, data); 1572 IFF_UNLOCKGIANT(ifp); 1573 } 1574 getmicrotime(&ifp->if_lastchange); 1575 break; 1576 1577 case SIOCSIFCAP: 1578 error = priv_check(td, PRIV_NET_SETIFCAP); 1579 if (error) 1580 return (error); 1581 if (ifp->if_ioctl == NULL) 1582 return (EOPNOTSUPP); 1583 if (ifr->ifr_reqcap & ~ifp->if_capabilities) 1584 return (EINVAL); 1585 IFF_LOCKGIANT(ifp); 1586 error = (*ifp->if_ioctl)(ifp, cmd, data); 1587 IFF_UNLOCKGIANT(ifp); 1588 if (error == 0) 1589 getmicrotime(&ifp->if_lastchange); 1590 break; 1591 1592 #ifdef MAC 1593 case SIOCSIFMAC: 1594 error = mac_ioctl_ifnet_set(td->td_ucred, ifr, ifp); 1595 break; 1596 #endif 1597 1598 case SIOCSIFNAME: 1599 error = priv_check(td, PRIV_NET_SETIFNAME); 1600 if (error) 1601 return (error); 1602 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL); 1603 if (error != 0) 1604 return (error); 1605 if (new_name[0] == '\0') 1606 return (EINVAL); 1607 if (ifunit(new_name) != NULL) 1608 return (EEXIST); 1609 1610 /* Announce the departure of the interface. */ 1611 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 1612 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 1613 1614 log(LOG_INFO, "%s: changing name to '%s'\n", 1615 ifp->if_xname, new_name); 1616 1617 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname)); 1618 ifa = ifp->if_addr; 1619 IFA_LOCK(ifa); 1620 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 1621 namelen = strlen(new_name); 1622 onamelen = sdl->sdl_nlen; 1623 /* 1624 * Move the address if needed. This is safe because we 1625 * allocate space for a name of length IFNAMSIZ when we 1626 * create this in if_attach(). 1627 */ 1628 if (namelen != onamelen) { 1629 bcopy(sdl->sdl_data + onamelen, 1630 sdl->sdl_data + namelen, sdl->sdl_alen); 1631 } 1632 bcopy(new_name, sdl->sdl_data, namelen); 1633 sdl->sdl_nlen = namelen; 1634 sdl = (struct sockaddr_dl *)ifa->ifa_netmask; 1635 bzero(sdl->sdl_data, onamelen); 1636 while (namelen != 0) 1637 sdl->sdl_data[--namelen] = 0xff; 1638 IFA_UNLOCK(ifa); 1639 1640 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 1641 /* Announce the return of the interface. */ 1642 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 1643 break; 1644 1645 case SIOCSIFMETRIC: 1646 error = priv_check(td, PRIV_NET_SETIFMETRIC); 1647 if (error) 1648 return (error); 1649 ifp->if_metric = ifr->ifr_metric; 1650 getmicrotime(&ifp->if_lastchange); 1651 break; 1652 1653 case SIOCSIFPHYS: 1654 error = priv_check(td, PRIV_NET_SETIFPHYS); 1655 if (error) 1656 return (error); 1657 if (ifp->if_ioctl == NULL) 1658 return (EOPNOTSUPP); 1659 IFF_LOCKGIANT(ifp); 1660 error = (*ifp->if_ioctl)(ifp, cmd, data); 1661 IFF_UNLOCKGIANT(ifp); 1662 if (error == 0) 1663 getmicrotime(&ifp->if_lastchange); 1664 break; 1665 1666 case SIOCSIFMTU: 1667 { 1668 u_long oldmtu = ifp->if_mtu; 1669 1670 error = priv_check(td, PRIV_NET_SETIFMTU); 1671 if (error) 1672 return (error); 1673 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) 1674 return (EINVAL); 1675 if (ifp->if_ioctl == NULL) 1676 return (EOPNOTSUPP); 1677 IFF_LOCKGIANT(ifp); 1678 error = (*ifp->if_ioctl)(ifp, cmd, data); 1679 IFF_UNLOCKGIANT(ifp); 1680 if (error == 0) { 1681 getmicrotime(&ifp->if_lastchange); 1682 rt_ifmsg(ifp); 1683 } 1684 /* 1685 * If the link MTU changed, do network layer specific procedure. 1686 */ 1687 if (ifp->if_mtu != oldmtu) { 1688 #ifdef INET6 1689 nd6_setmtu(ifp); 1690 #endif 1691 } 1692 break; 1693 } 1694 1695 case SIOCADDMULTI: 1696 case SIOCDELMULTI: 1697 if (cmd == SIOCADDMULTI) 1698 error = priv_check(td, PRIV_NET_ADDMULTI); 1699 else 1700 error = priv_check(td, PRIV_NET_DELMULTI); 1701 if (error) 1702 return (error); 1703 1704 /* Don't allow group membership on non-multicast interfaces. */ 1705 if ((ifp->if_flags & IFF_MULTICAST) == 0) 1706 return (EOPNOTSUPP); 1707 1708 /* Don't let users screw up protocols' entries. */ 1709 if (ifr->ifr_addr.sa_family != AF_LINK) 1710 return (EINVAL); 1711 1712 if (cmd == SIOCADDMULTI) { 1713 struct ifmultiaddr *ifma; 1714 1715 /* 1716 * Userland is only permitted to join groups once 1717 * via the if_addmulti() KPI, because it cannot hold 1718 * struct ifmultiaddr * between calls. It may also 1719 * lose a race while we check if the membership 1720 * already exists. 1721 */ 1722 IF_ADDR_LOCK(ifp); 1723 ifma = if_findmulti(ifp, &ifr->ifr_addr); 1724 IF_ADDR_UNLOCK(ifp); 1725 if (ifma != NULL) 1726 error = EADDRINUSE; 1727 else 1728 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 1729 } else { 1730 error = if_delmulti(ifp, &ifr->ifr_addr); 1731 } 1732 if (error == 0) 1733 getmicrotime(&ifp->if_lastchange); 1734 break; 1735 1736 case SIOCSIFPHYADDR: 1737 case SIOCDIFPHYADDR: 1738 #ifdef INET6 1739 case SIOCSIFPHYADDR_IN6: 1740 #endif 1741 case SIOCSLIFPHYADDR: 1742 case SIOCSIFMEDIA: 1743 case SIOCSIFGENERIC: 1744 error = priv_check(td, PRIV_NET_HWIOCTL); 1745 if (error) 1746 return (error); 1747 if (ifp->if_ioctl == NULL) 1748 return (EOPNOTSUPP); 1749 IFF_LOCKGIANT(ifp); 1750 error = (*ifp->if_ioctl)(ifp, cmd, data); 1751 IFF_UNLOCKGIANT(ifp); 1752 if (error == 0) 1753 getmicrotime(&ifp->if_lastchange); 1754 break; 1755 1756 case SIOCGIFSTATUS: 1757 ifs = (struct ifstat *)data; 1758 ifs->ascii[0] = '\0'; 1759 1760 case SIOCGIFPSRCADDR: 1761 case SIOCGIFPDSTADDR: 1762 case SIOCGLIFPHYADDR: 1763 case SIOCGIFMEDIA: 1764 case SIOCGIFGENERIC: 1765 if (ifp->if_ioctl == NULL) 1766 return (EOPNOTSUPP); 1767 IFF_LOCKGIANT(ifp); 1768 error = (*ifp->if_ioctl)(ifp, cmd, data); 1769 IFF_UNLOCKGIANT(ifp); 1770 break; 1771 1772 case SIOCSIFLLADDR: 1773 error = priv_check(td, PRIV_NET_SETLLADDR); 1774 if (error) 1775 return (error); 1776 error = if_setlladdr(ifp, 1777 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); 1778 break; 1779 1780 case SIOCAIFGROUP: 1781 { 1782 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; 1783 1784 error = priv_check(td, PRIV_NET_ADDIFGROUP); 1785 if (error) 1786 return (error); 1787 if ((error = if_addgroup(ifp, ifgr->ifgr_group))) 1788 return (error); 1789 break; 1790 } 1791 1792 case SIOCGIFGROUP: 1793 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp))) 1794 return (error); 1795 break; 1796 1797 case SIOCDIFGROUP: 1798 { 1799 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; 1800 1801 error = priv_check(td, PRIV_NET_DELIFGROUP); 1802 if (error) 1803 return (error); 1804 if ((error = if_delgroup(ifp, ifgr->ifgr_group))) 1805 return (error); 1806 break; 1807 } 1808 1809 default: 1810 error = ENOIOCTL; 1811 break; 1812 } 1813 return (error); 1814 } 1815 1816 /* 1817 * Interface ioctls. 1818 */ 1819 int 1820 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td) 1821 { 1822 struct ifnet *ifp; 1823 struct ifreq *ifr; 1824 int error; 1825 int oif_flags; 1826 1827 switch (cmd) { 1828 case SIOCGIFCONF: 1829 case OSIOCGIFCONF: 1830 #ifdef __amd64__ 1831 case SIOCGIFCONF32: 1832 #endif 1833 return (ifconf(cmd, data)); 1834 } 1835 ifr = (struct ifreq *)data; 1836 1837 switch (cmd) { 1838 case SIOCIFCREATE: 1839 case SIOCIFCREATE2: 1840 error = priv_check(td, PRIV_NET_IFCREATE); 1841 if (error) 1842 return (error); 1843 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name), 1844 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL)); 1845 case SIOCIFDESTROY: 1846 error = priv_check(td, PRIV_NET_IFDESTROY); 1847 if (error) 1848 return (error); 1849 return if_clone_destroy(ifr->ifr_name); 1850 1851 case SIOCIFGCLONERS: 1852 return (if_clone_list((struct if_clonereq *)data)); 1853 case SIOCGIFGMEMB: 1854 return (if_getgroupmembers((struct ifgroupreq *)data)); 1855 } 1856 1857 ifp = ifunit(ifr->ifr_name); 1858 if (ifp == 0) 1859 return (ENXIO); 1860 1861 error = ifhwioctl(cmd, ifp, data, td); 1862 if (error != ENOIOCTL) 1863 return (error); 1864 1865 oif_flags = ifp->if_flags; 1866 if (so->so_proto == 0) 1867 return (EOPNOTSUPP); 1868 #ifndef COMPAT_43 1869 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, 1870 data, 1871 ifp, td)); 1872 #else 1873 { 1874 int ocmd = cmd; 1875 1876 switch (cmd) { 1877 1878 case SIOCSIFDSTADDR: 1879 case SIOCSIFADDR: 1880 case SIOCSIFBRDADDR: 1881 case SIOCSIFNETMASK: 1882 #if BYTE_ORDER != BIG_ENDIAN 1883 if (ifr->ifr_addr.sa_family == 0 && 1884 ifr->ifr_addr.sa_len < 16) { 1885 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 1886 ifr->ifr_addr.sa_len = 16; 1887 } 1888 #else 1889 if (ifr->ifr_addr.sa_len == 0) 1890 ifr->ifr_addr.sa_len = 16; 1891 #endif 1892 break; 1893 1894 case OSIOCGIFADDR: 1895 cmd = SIOCGIFADDR; 1896 break; 1897 1898 case OSIOCGIFDSTADDR: 1899 cmd = SIOCGIFDSTADDR; 1900 break; 1901 1902 case OSIOCGIFBRDADDR: 1903 cmd = SIOCGIFBRDADDR; 1904 break; 1905 1906 case OSIOCGIFNETMASK: 1907 cmd = SIOCGIFNETMASK; 1908 } 1909 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, 1910 cmd, 1911 data, 1912 ifp, td)); 1913 switch (ocmd) { 1914 1915 case OSIOCGIFADDR: 1916 case OSIOCGIFDSTADDR: 1917 case OSIOCGIFBRDADDR: 1918 case OSIOCGIFNETMASK: 1919 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 1920 1921 } 1922 } 1923 #endif /* COMPAT_43 */ 1924 1925 if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 1926 #ifdef INET6 1927 DELAY(100);/* XXX: temporary workaround for fxp issue*/ 1928 if (ifp->if_flags & IFF_UP) { 1929 int s = splimp(); 1930 in6_if_up(ifp); 1931 splx(s); 1932 } 1933 #endif 1934 } 1935 return (error); 1936 } 1937 1938 /* 1939 * The code common to handling reference counted flags, 1940 * e.g., in ifpromisc() and if_allmulti(). 1941 * The "pflag" argument can specify a permanent mode flag to check, 1942 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none. 1943 * 1944 * Only to be used on stack-owned flags, not driver-owned flags. 1945 */ 1946 static int 1947 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch) 1948 { 1949 struct ifreq ifr; 1950 int error; 1951 int oldflags, oldcount; 1952 1953 /* Sanity checks to catch programming errors */ 1954 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0, 1955 ("%s: setting driver-owned flag %d", __func__, flag)); 1956 1957 if (onswitch) 1958 KASSERT(*refcount >= 0, 1959 ("%s: increment negative refcount %d for flag %d", 1960 __func__, *refcount, flag)); 1961 else 1962 KASSERT(*refcount > 0, 1963 ("%s: decrement non-positive refcount %d for flag %d", 1964 __func__, *refcount, flag)); 1965 1966 /* In case this mode is permanent, just touch refcount */ 1967 if (ifp->if_flags & pflag) { 1968 *refcount += onswitch ? 1 : -1; 1969 return (0); 1970 } 1971 1972 /* Save ifnet parameters for if_ioctl() may fail */ 1973 oldcount = *refcount; 1974 oldflags = ifp->if_flags; 1975 1976 /* 1977 * See if we aren't the only and touching refcount is enough. 1978 * Actually toggle interface flag if we are the first or last. 1979 */ 1980 if (onswitch) { 1981 if ((*refcount)++) 1982 return (0); 1983 ifp->if_flags |= flag; 1984 } else { 1985 if (--(*refcount)) 1986 return (0); 1987 ifp->if_flags &= ~flag; 1988 } 1989 1990 /* Call down the driver since we've changed interface flags */ 1991 if (ifp->if_ioctl == NULL) { 1992 error = EOPNOTSUPP; 1993 goto recover; 1994 } 1995 ifr.ifr_flags = ifp->if_flags & 0xffff; 1996 ifr.ifr_flagshigh = ifp->if_flags >> 16; 1997 IFF_LOCKGIANT(ifp); 1998 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 1999 IFF_UNLOCKGIANT(ifp); 2000 if (error) 2001 goto recover; 2002 /* Notify userland that interface flags have changed */ 2003 rt_ifmsg(ifp); 2004 return (0); 2005 2006 recover: 2007 /* Recover after driver error */ 2008 *refcount = oldcount; 2009 ifp->if_flags = oldflags; 2010 return (error); 2011 } 2012 2013 /* 2014 * Set/clear promiscuous mode on interface ifp based on the truth value 2015 * of pswitch. The calls are reference counted so that only the first 2016 * "on" request actually has an effect, as does the final "off" request. 2017 * Results are undefined if the "off" and "on" requests are not matched. 2018 */ 2019 int 2020 ifpromisc(struct ifnet *ifp, int pswitch) 2021 { 2022 int error; 2023 int oldflags = ifp->if_flags; 2024 2025 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC, 2026 &ifp->if_pcount, pswitch); 2027 /* If promiscuous mode status has changed, log a message */ 2028 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC)) 2029 log(LOG_INFO, "%s: promiscuous mode %s\n", 2030 ifp->if_xname, 2031 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); 2032 return (error); 2033 } 2034 2035 /* 2036 * Return interface configuration 2037 * of system. List may be used 2038 * in later ioctl's (above) to get 2039 * other information. 2040 */ 2041 /*ARGSUSED*/ 2042 static int 2043 ifconf(u_long cmd, caddr_t data) 2044 { 2045 struct ifconf *ifc = (struct ifconf *)data; 2046 #ifdef __amd64__ 2047 struct ifconf32 *ifc32 = (struct ifconf32 *)data; 2048 struct ifconf ifc_swab; 2049 #endif 2050 struct ifnet *ifp; 2051 struct ifaddr *ifa; 2052 struct ifreq ifr; 2053 struct sbuf *sb; 2054 int error, full = 0, valid_len, max_len; 2055 2056 #ifdef __amd64__ 2057 if (cmd == SIOCGIFCONF32) { 2058 ifc_swab.ifc_len = ifc32->ifc_len; 2059 ifc_swab.ifc_buf = (caddr_t)(uintptr_t)ifc32->ifc_buf; 2060 ifc = &ifc_swab; 2061 } 2062 #endif 2063 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */ 2064 max_len = MAXPHYS - 1; 2065 2066 /* Prevent hostile input from being able to crash the system */ 2067 if (ifc->ifc_len <= 0) 2068 return (EINVAL); 2069 2070 again: 2071 if (ifc->ifc_len <= max_len) { 2072 max_len = ifc->ifc_len; 2073 full = 1; 2074 } 2075 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN); 2076 max_len = 0; 2077 valid_len = 0; 2078 2079 IFNET_RLOCK(); /* could sleep XXX */ 2080 TAILQ_FOREACH(ifp, &ifnet, if_link) { 2081 int addrs; 2082 2083 /* 2084 * Zero the ifr_name buffer to make sure we don't 2085 * disclose the contents of the stack. 2086 */ 2087 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name)); 2088 2089 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)) 2090 >= sizeof(ifr.ifr_name)) { 2091 sbuf_delete(sb); 2092 IFNET_RUNLOCK(); 2093 return (ENAMETOOLONG); 2094 } 2095 2096 addrs = 0; 2097 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2098 struct sockaddr *sa = ifa->ifa_addr; 2099 2100 if (jailed(curthread->td_ucred) && 2101 prison_if(curthread->td_ucred, sa)) 2102 continue; 2103 addrs++; 2104 #ifdef COMPAT_43 2105 if (cmd == OSIOCGIFCONF) { 2106 struct osockaddr *osa = 2107 (struct osockaddr *)&ifr.ifr_addr; 2108 ifr.ifr_addr = *sa; 2109 osa->sa_family = sa->sa_family; 2110 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2111 max_len += sizeof(ifr); 2112 } else 2113 #endif 2114 if (sa->sa_len <= sizeof(*sa)) { 2115 ifr.ifr_addr = *sa; 2116 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2117 max_len += sizeof(ifr); 2118 } else { 2119 sbuf_bcat(sb, &ifr, 2120 offsetof(struct ifreq, ifr_addr)); 2121 max_len += offsetof(struct ifreq, ifr_addr); 2122 sbuf_bcat(sb, sa, sa->sa_len); 2123 max_len += sa->sa_len; 2124 } 2125 2126 if (!sbuf_overflowed(sb)) 2127 valid_len = sbuf_len(sb); 2128 } 2129 if (addrs == 0) { 2130 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 2131 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2132 max_len += sizeof(ifr); 2133 2134 if (!sbuf_overflowed(sb)) 2135 valid_len = sbuf_len(sb); 2136 } 2137 } 2138 IFNET_RUNLOCK(); 2139 2140 /* 2141 * If we didn't allocate enough space (uncommon), try again. If 2142 * we have already allocated as much space as we are allowed, 2143 * return what we've got. 2144 */ 2145 if (valid_len != max_len && !full) { 2146 sbuf_delete(sb); 2147 goto again; 2148 } 2149 2150 ifc->ifc_len = valid_len; 2151 #ifdef __amd64__ 2152 if (cmd == SIOCGIFCONF32) 2153 ifc32->ifc_len = valid_len; 2154 #endif 2155 sbuf_finish(sb); 2156 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len); 2157 sbuf_delete(sb); 2158 return (error); 2159 } 2160 2161 /* 2162 * Just like ifpromisc(), but for all-multicast-reception mode. 2163 */ 2164 int 2165 if_allmulti(struct ifnet *ifp, int onswitch) 2166 { 2167 2168 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch)); 2169 } 2170 2171 struct ifmultiaddr * 2172 if_findmulti(struct ifnet *ifp, struct sockaddr *sa) 2173 { 2174 struct ifmultiaddr *ifma; 2175 2176 IF_ADDR_LOCK_ASSERT(ifp); 2177 2178 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 2179 if (sa->sa_family == AF_LINK) { 2180 if (sa_dl_equal(ifma->ifma_addr, sa)) 2181 break; 2182 } else { 2183 if (sa_equal(ifma->ifma_addr, sa)) 2184 break; 2185 } 2186 } 2187 2188 return ifma; 2189 } 2190 2191 /* 2192 * Allocate a new ifmultiaddr and initialize based on passed arguments. We 2193 * make copies of passed sockaddrs. The ifmultiaddr will not be added to 2194 * the ifnet multicast address list here, so the caller must do that and 2195 * other setup work (such as notifying the device driver). The reference 2196 * count is initialized to 1. 2197 */ 2198 static struct ifmultiaddr * 2199 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa, 2200 int mflags) 2201 { 2202 struct ifmultiaddr *ifma; 2203 struct sockaddr *dupsa; 2204 2205 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, mflags | 2206 M_ZERO); 2207 if (ifma == NULL) 2208 return (NULL); 2209 2210 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, mflags); 2211 if (dupsa == NULL) { 2212 FREE(ifma, M_IFMADDR); 2213 return (NULL); 2214 } 2215 bcopy(sa, dupsa, sa->sa_len); 2216 ifma->ifma_addr = dupsa; 2217 2218 ifma->ifma_ifp = ifp; 2219 ifma->ifma_refcount = 1; 2220 ifma->ifma_protospec = NULL; 2221 2222 if (llsa == NULL) { 2223 ifma->ifma_lladdr = NULL; 2224 return (ifma); 2225 } 2226 2227 MALLOC(dupsa, struct sockaddr *, llsa->sa_len, M_IFMADDR, mflags); 2228 if (dupsa == NULL) { 2229 FREE(ifma->ifma_addr, M_IFMADDR); 2230 FREE(ifma, M_IFMADDR); 2231 return (NULL); 2232 } 2233 bcopy(llsa, dupsa, llsa->sa_len); 2234 ifma->ifma_lladdr = dupsa; 2235 2236 return (ifma); 2237 } 2238 2239 /* 2240 * if_freemulti: free ifmultiaddr structure and possibly attached related 2241 * addresses. The caller is responsible for implementing reference 2242 * counting, notifying the driver, handling routing messages, and releasing 2243 * any dependent link layer state. 2244 */ 2245 static void 2246 if_freemulti(struct ifmultiaddr *ifma) 2247 { 2248 2249 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d", 2250 ifma->ifma_refcount)); 2251 KASSERT(ifma->ifma_protospec == NULL, 2252 ("if_freemulti: protospec not NULL")); 2253 2254 if (ifma->ifma_lladdr != NULL) 2255 FREE(ifma->ifma_lladdr, M_IFMADDR); 2256 FREE(ifma->ifma_addr, M_IFMADDR); 2257 FREE(ifma, M_IFMADDR); 2258 } 2259 2260 /* 2261 * Register an additional multicast address with a network interface. 2262 * 2263 * - If the address is already present, bump the reference count on the 2264 * address and return. 2265 * - If the address is not link-layer, look up a link layer address. 2266 * - Allocate address structures for one or both addresses, and attach to the 2267 * multicast address list on the interface. If automatically adding a link 2268 * layer address, the protocol address will own a reference to the link 2269 * layer address, to be freed when it is freed. 2270 * - Notify the network device driver of an addition to the multicast address 2271 * list. 2272 * 2273 * 'sa' points to caller-owned memory with the desired multicast address. 2274 * 2275 * 'retifma' will be used to return a pointer to the resulting multicast 2276 * address reference, if desired. 2277 */ 2278 int 2279 if_addmulti(struct ifnet *ifp, struct sockaddr *sa, 2280 struct ifmultiaddr **retifma) 2281 { 2282 struct ifmultiaddr *ifma, *ll_ifma; 2283 struct sockaddr *llsa; 2284 int error; 2285 2286 /* 2287 * If the address is already present, return a new reference to it; 2288 * otherwise, allocate storage and set up a new address. 2289 */ 2290 IF_ADDR_LOCK(ifp); 2291 ifma = if_findmulti(ifp, sa); 2292 if (ifma != NULL) { 2293 ifma->ifma_refcount++; 2294 if (retifma != NULL) 2295 *retifma = ifma; 2296 IF_ADDR_UNLOCK(ifp); 2297 return (0); 2298 } 2299 2300 /* 2301 * The address isn't already present; resolve the protocol address 2302 * into a link layer address, and then look that up, bump its 2303 * refcount or allocate an ifma for that also. If 'llsa' was 2304 * returned, we will need to free it later. 2305 */ 2306 llsa = NULL; 2307 ll_ifma = NULL; 2308 if (ifp->if_resolvemulti != NULL) { 2309 error = ifp->if_resolvemulti(ifp, &llsa, sa); 2310 if (error) 2311 goto unlock_out; 2312 } 2313 2314 /* 2315 * Allocate the new address. Don't hook it up yet, as we may also 2316 * need to allocate a link layer multicast address. 2317 */ 2318 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT); 2319 if (ifma == NULL) { 2320 error = ENOMEM; 2321 goto free_llsa_out; 2322 } 2323 2324 /* 2325 * If a link layer address is found, we'll need to see if it's 2326 * already present in the address list, or allocate is as well. 2327 * When this block finishes, the link layer address will be on the 2328 * list. 2329 */ 2330 if (llsa != NULL) { 2331 ll_ifma = if_findmulti(ifp, llsa); 2332 if (ll_ifma == NULL) { 2333 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT); 2334 if (ll_ifma == NULL) { 2335 --ifma->ifma_refcount; 2336 if_freemulti(ifma); 2337 error = ENOMEM; 2338 goto free_llsa_out; 2339 } 2340 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma, 2341 ifma_link); 2342 } else 2343 ll_ifma->ifma_refcount++; 2344 ifma->ifma_llifma = ll_ifma; 2345 } 2346 2347 /* 2348 * We now have a new multicast address, ifma, and possibly a new or 2349 * referenced link layer address. Add the primary address to the 2350 * ifnet address list. 2351 */ 2352 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 2353 2354 if (retifma != NULL) 2355 *retifma = ifma; 2356 2357 /* 2358 * Must generate the message while holding the lock so that 'ifma' 2359 * pointer is still valid. 2360 */ 2361 rt_newmaddrmsg(RTM_NEWMADDR, ifma); 2362 IF_ADDR_UNLOCK(ifp); 2363 2364 /* 2365 * We are certain we have added something, so call down to the 2366 * interface to let them know about it. 2367 */ 2368 if (ifp->if_ioctl != NULL) { 2369 IFF_LOCKGIANT(ifp); 2370 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0); 2371 IFF_UNLOCKGIANT(ifp); 2372 } 2373 2374 if (llsa != NULL) 2375 FREE(llsa, M_IFMADDR); 2376 2377 return (0); 2378 2379 free_llsa_out: 2380 if (llsa != NULL) 2381 FREE(llsa, M_IFMADDR); 2382 2383 unlock_out: 2384 IF_ADDR_UNLOCK(ifp); 2385 return (error); 2386 } 2387 2388 /* 2389 * Delete a multicast group membership by network-layer group address. 2390 * 2391 * Returns ENOENT if the entry could not be found. If ifp no longer 2392 * exists, results are undefined. This entry point should only be used 2393 * from subsystems which do appropriate locking to hold ifp for the 2394 * duration of the call. 2395 * Network-layer protocol domains must use if_delmulti_ifma(). 2396 */ 2397 int 2398 if_delmulti(struct ifnet *ifp, struct sockaddr *sa) 2399 { 2400 struct ifmultiaddr *ifma; 2401 int lastref; 2402 #ifdef INVARIANTS 2403 struct ifnet *oifp; 2404 2405 IFNET_RLOCK(); 2406 TAILQ_FOREACH(oifp, &ifnet, if_link) 2407 if (ifp == oifp) 2408 break; 2409 if (ifp != oifp) 2410 ifp = NULL; 2411 IFNET_RUNLOCK(); 2412 2413 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__)); 2414 #endif 2415 if (ifp == NULL) 2416 return (ENOENT); 2417 2418 IF_ADDR_LOCK(ifp); 2419 lastref = 0; 2420 ifma = if_findmulti(ifp, sa); 2421 if (ifma != NULL) 2422 lastref = if_delmulti_locked(ifp, ifma, 0); 2423 IF_ADDR_UNLOCK(ifp); 2424 2425 if (ifma == NULL) 2426 return (ENOENT); 2427 2428 if (lastref && ifp->if_ioctl != NULL) { 2429 IFF_LOCKGIANT(ifp); 2430 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 2431 IFF_UNLOCKGIANT(ifp); 2432 } 2433 2434 return (0); 2435 } 2436 2437 /* 2438 * Delete a multicast group membership by group membership pointer. 2439 * Network-layer protocol domains must use this routine. 2440 * 2441 * It is safe to call this routine if the ifp disappeared. Callers should 2442 * hold IFF_LOCKGIANT() to avoid a LOR in case the hardware needs to be 2443 * reconfigured. 2444 */ 2445 void 2446 if_delmulti_ifma(struct ifmultiaddr *ifma) 2447 { 2448 struct ifnet *ifp; 2449 int lastref; 2450 2451 ifp = ifma->ifma_ifp; 2452 #ifdef DIAGNOSTIC 2453 if (ifp == NULL) { 2454 printf("%s: ifma_ifp seems to be detached\n", __func__); 2455 } else { 2456 struct ifnet *oifp; 2457 2458 IFNET_RLOCK(); 2459 TAILQ_FOREACH(oifp, &ifnet, if_link) 2460 if (ifp == oifp) 2461 break; 2462 if (ifp != oifp) { 2463 printf("%s: ifnet %p disappeared\n", __func__, ifp); 2464 ifp = NULL; 2465 } 2466 IFNET_RUNLOCK(); 2467 } 2468 #endif 2469 /* 2470 * If and only if the ifnet instance exists: Acquire the address lock. 2471 */ 2472 if (ifp != NULL) 2473 IF_ADDR_LOCK(ifp); 2474 2475 lastref = if_delmulti_locked(ifp, ifma, 0); 2476 2477 if (ifp != NULL) { 2478 /* 2479 * If and only if the ifnet instance exists: 2480 * Release the address lock. 2481 * If the group was left: update the hardware hash filter. 2482 */ 2483 IF_ADDR_UNLOCK(ifp); 2484 if (lastref && ifp->if_ioctl != NULL) { 2485 IFF_LOCKGIANT(ifp); 2486 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 2487 IFF_UNLOCKGIANT(ifp); 2488 } 2489 } 2490 } 2491 2492 /* 2493 * Perform deletion of network-layer and/or link-layer multicast address. 2494 * 2495 * Return 0 if the reference count was decremented. 2496 * Return 1 if the final reference was released, indicating that the 2497 * hardware hash filter should be reprogrammed. 2498 */ 2499 static int 2500 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching) 2501 { 2502 struct ifmultiaddr *ll_ifma; 2503 2504 if (ifp != NULL && ifma->ifma_ifp != NULL) { 2505 KASSERT(ifma->ifma_ifp == ifp, 2506 ("%s: inconsistent ifp %p", __func__, ifp)); 2507 IF_ADDR_LOCK_ASSERT(ifp); 2508 } 2509 2510 ifp = ifma->ifma_ifp; 2511 2512 /* 2513 * If the ifnet is detaching, null out references to ifnet, 2514 * so that upper protocol layers will notice, and not attempt 2515 * to obtain locks for an ifnet which no longer exists. The 2516 * routing socket announcement must happen before the ifnet 2517 * instance is detached from the system. 2518 */ 2519 if (detaching) { 2520 #ifdef DIAGNOSTIC 2521 printf("%s: detaching ifnet instance %p\n", __func__, ifp); 2522 #endif 2523 /* 2524 * ifp may already be nulled out if we are being reentered 2525 * to delete the ll_ifma. 2526 */ 2527 if (ifp != NULL) { 2528 rt_newmaddrmsg(RTM_DELMADDR, ifma); 2529 ifma->ifma_ifp = NULL; 2530 } 2531 } 2532 2533 if (--ifma->ifma_refcount > 0) 2534 return 0; 2535 2536 /* 2537 * If this ifma is a network-layer ifma, a link-layer ifma may 2538 * have been associated with it. Release it first if so. 2539 */ 2540 ll_ifma = ifma->ifma_llifma; 2541 if (ll_ifma != NULL) { 2542 KASSERT(ifma->ifma_lladdr != NULL, 2543 ("%s: llifma w/o lladdr", __func__)); 2544 if (detaching) 2545 ll_ifma->ifma_ifp = NULL; /* XXX */ 2546 if (--ll_ifma->ifma_refcount == 0) { 2547 if (ifp != NULL) { 2548 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, 2549 ifma_link); 2550 } 2551 if_freemulti(ll_ifma); 2552 } 2553 } 2554 2555 if (ifp != NULL) 2556 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 2557 2558 if_freemulti(ifma); 2559 2560 /* 2561 * The last reference to this instance of struct ifmultiaddr 2562 * was released; the hardware should be notified of this change. 2563 */ 2564 return 1; 2565 } 2566 2567 /* 2568 * Set the link layer address on an interface. 2569 * 2570 * At this time we only support certain types of interfaces, 2571 * and we don't allow the length of the address to change. 2572 */ 2573 int 2574 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 2575 { 2576 struct sockaddr_dl *sdl; 2577 struct ifaddr *ifa; 2578 struct ifreq ifr; 2579 2580 ifa = ifp->if_addr; 2581 if (ifa == NULL) 2582 return (EINVAL); 2583 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 2584 if (sdl == NULL) 2585 return (EINVAL); 2586 if (len != sdl->sdl_alen) /* don't allow length to change */ 2587 return (EINVAL); 2588 switch (ifp->if_type) { 2589 case IFT_ETHER: 2590 case IFT_FDDI: 2591 case IFT_XETHER: 2592 case IFT_ISO88025: 2593 case IFT_L2VLAN: 2594 case IFT_BRIDGE: 2595 case IFT_ARCNET: 2596 bcopy(lladdr, LLADDR(sdl), len); 2597 break; 2598 default: 2599 return (ENODEV); 2600 } 2601 /* 2602 * If the interface is already up, we need 2603 * to re-init it in order to reprogram its 2604 * address filter. 2605 */ 2606 if ((ifp->if_flags & IFF_UP) != 0) { 2607 if (ifp->if_ioctl) { 2608 IFF_LOCKGIANT(ifp); 2609 ifp->if_flags &= ~IFF_UP; 2610 ifr.ifr_flags = ifp->if_flags & 0xffff; 2611 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2612 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2613 ifp->if_flags |= IFF_UP; 2614 ifr.ifr_flags = ifp->if_flags & 0xffff; 2615 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2616 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2617 IFF_UNLOCKGIANT(ifp); 2618 } 2619 #ifdef INET 2620 /* 2621 * Also send gratuitous ARPs to notify other nodes about 2622 * the address change. 2623 */ 2624 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2625 if (ifa->ifa_addr->sa_family == AF_INET) 2626 arp_ifinit(ifp, ifa); 2627 } 2628 #endif 2629 } 2630 return (0); 2631 } 2632 2633 /* 2634 * The name argument must be a pointer to storage which will last as 2635 * long as the interface does. For physical devices, the result of 2636 * device_get_name(dev) is a good choice and for pseudo-devices a 2637 * static string works well. 2638 */ 2639 void 2640 if_initname(struct ifnet *ifp, const char *name, int unit) 2641 { 2642 ifp->if_dname = name; 2643 ifp->if_dunit = unit; 2644 if (unit != IF_DUNIT_NONE) 2645 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit); 2646 else 2647 strlcpy(ifp->if_xname, name, IFNAMSIZ); 2648 } 2649 2650 int 2651 if_printf(struct ifnet *ifp, const char * fmt, ...) 2652 { 2653 va_list ap; 2654 int retval; 2655 2656 retval = printf("%s: ", ifp->if_xname); 2657 va_start(ap, fmt); 2658 retval += vprintf(fmt, ap); 2659 va_end(ap); 2660 return (retval); 2661 } 2662 2663 /* 2664 * When an interface is marked IFF_NEEDSGIANT, its if_start() routine cannot 2665 * be called without Giant. However, we often can't acquire the Giant lock 2666 * at those points; instead, we run it via a task queue that holds Giant via 2667 * if_start_deferred. 2668 * 2669 * XXXRW: We need to make sure that the ifnet isn't fully detached until any 2670 * outstanding if_start_deferred() tasks that will run after the free. This 2671 * probably means waiting in if_detach(). 2672 */ 2673 void 2674 if_start(struct ifnet *ifp) 2675 { 2676 2677 NET_ASSERT_GIANT(); 2678 2679 if ((ifp->if_flags & IFF_NEEDSGIANT) != 0 && debug_mpsafenet != 0) { 2680 if (mtx_owned(&Giant)) 2681 (*(ifp)->if_start)(ifp); 2682 else 2683 taskqueue_enqueue(taskqueue_swi_giant, 2684 &ifp->if_starttask); 2685 } else 2686 (*(ifp)->if_start)(ifp); 2687 } 2688 2689 static void 2690 if_start_deferred(void *context, int pending) 2691 { 2692 struct ifnet *ifp; 2693 2694 /* 2695 * This code must be entered with Giant, and should never run if 2696 * we're not running with debug.mpsafenet. 2697 */ 2698 KASSERT(debug_mpsafenet != 0, ("if_start_deferred: debug.mpsafenet")); 2699 GIANT_REQUIRED; 2700 2701 ifp = context; 2702 (ifp->if_start)(ifp); 2703 } 2704 2705 int 2706 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust) 2707 { 2708 int active = 0; 2709 2710 IF_LOCK(ifq); 2711 if (_IF_QFULL(ifq)) { 2712 _IF_DROP(ifq); 2713 IF_UNLOCK(ifq); 2714 m_freem(m); 2715 return (0); 2716 } 2717 if (ifp != NULL) { 2718 ifp->if_obytes += m->m_pkthdr.len + adjust; 2719 if (m->m_flags & (M_BCAST|M_MCAST)) 2720 ifp->if_omcasts++; 2721 active = ifp->if_drv_flags & IFF_DRV_OACTIVE; 2722 } 2723 _IF_ENQUEUE(ifq, m); 2724 IF_UNLOCK(ifq); 2725 if (ifp != NULL && !active) 2726 if_start(ifp); 2727 return (1); 2728 } 2729 2730 void 2731 if_register_com_alloc(u_char type, 2732 if_com_alloc_t *a, if_com_free_t *f) 2733 { 2734 2735 KASSERT(if_com_alloc[type] == NULL, 2736 ("if_register_com_alloc: %d already registered", type)); 2737 KASSERT(if_com_free[type] == NULL, 2738 ("if_register_com_alloc: %d free already registered", type)); 2739 2740 if_com_alloc[type] = a; 2741 if_com_free[type] = f; 2742 } 2743 2744 void 2745 if_deregister_com_alloc(u_char type) 2746 { 2747 2748 KASSERT(if_com_alloc[type] != NULL, 2749 ("if_deregister_com_alloc: %d not registered", type)); 2750 KASSERT(if_com_free[type] != NULL, 2751 ("if_deregister_com_alloc: %d free not registered", type)); 2752 if_com_alloc[type] = NULL; 2753 if_com_free[type] = NULL; 2754 } 2755