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