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 37 #include <sys/param.h> 38 #include <sys/types.h> 39 #include <sys/conf.h> 40 #include <sys/malloc.h> 41 #include <sys/sbuf.h> 42 #include <sys/bus.h> 43 #include <sys/mbuf.h> 44 #include <sys/systm.h> 45 #include <sys/priv.h> 46 #include <sys/proc.h> 47 #include <sys/socket.h> 48 #include <sys/socketvar.h> 49 #include <sys/protosw.h> 50 #include <sys/kernel.h> 51 #include <sys/lock.h> 52 #include <sys/refcount.h> 53 #include <sys/module.h> 54 #include <sys/rwlock.h> 55 #include <sys/sockio.h> 56 #include <sys/syslog.h> 57 #include <sys/sysctl.h> 58 #include <sys/taskqueue.h> 59 #include <sys/domain.h> 60 #include <sys/jail.h> 61 #include <sys/priv.h> 62 63 #include <machine/stdarg.h> 64 #include <vm/uma.h> 65 66 #include <net/bpf.h> 67 #include <net/ethernet.h> 68 #include <net/if.h> 69 #include <net/if_arp.h> 70 #include <net/if_clone.h> 71 #include <net/if_dl.h> 72 #include <net/if_types.h> 73 #include <net/if_var.h> 74 #include <net/if_media.h> 75 #include <net/if_vlan_var.h> 76 #include <net/radix.h> 77 #include <net/route.h> 78 #include <net/vnet.h> 79 80 #if defined(INET) || defined(INET6) 81 #include <net/ethernet.h> 82 #include <netinet/in.h> 83 #include <netinet/in_var.h> 84 #include <netinet/ip.h> 85 #include <netinet/ip_carp.h> 86 #ifdef INET 87 #include <netinet/if_ether.h> 88 #endif /* INET */ 89 #ifdef INET6 90 #include <netinet6/in6_var.h> 91 #include <netinet6/in6_ifattach.h> 92 #endif /* INET6 */ 93 #endif /* INET || INET6 */ 94 95 #include <security/mac/mac_framework.h> 96 97 #ifdef COMPAT_FREEBSD32 98 #include <sys/mount.h> 99 #include <compat/freebsd32/freebsd32.h> 100 #endif 101 102 struct ifindex_entry { 103 struct ifnet *ife_ifnet; 104 }; 105 106 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 107 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); 108 109 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN, 110 &ifqmaxlen, 0, "max send queue size"); 111 112 /* Log link state change events */ 113 static int log_link_state_change = 1; 114 115 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW, 116 &log_link_state_change, 0, 117 "log interface link state change events"); 118 119 /* Interface description */ 120 static unsigned int ifdescr_maxlen = 1024; 121 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW, 122 &ifdescr_maxlen, 0, 123 "administrative maximum length for interface description"); 124 125 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions"); 126 127 /* global sx for non-critical path ifdescr */ 128 static struct sx ifdescr_sx; 129 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr"); 130 131 void (*bridge_linkstate_p)(struct ifnet *ifp); 132 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state); 133 void (*lagg_linkstate_p)(struct ifnet *ifp, int state); 134 /* These are external hooks for CARP. */ 135 void (*carp_linkstate_p)(struct ifnet *ifp); 136 void (*carp_demote_adj_p)(int, char *); 137 int (*carp_master_p)(struct ifaddr *); 138 #if defined(INET) || defined(INET6) 139 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost); 140 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m, 141 const struct sockaddr *sa); 142 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *); 143 int (*carp_attach_p)(struct ifaddr *, int); 144 void (*carp_detach_p)(struct ifaddr *); 145 #endif 146 #ifdef INET 147 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **); 148 #endif 149 #ifdef INET6 150 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6); 151 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m, 152 const struct in6_addr *taddr); 153 #endif 154 155 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL; 156 157 /* 158 * XXX: Style; these should be sorted alphabetically, and unprototyped 159 * static functions should be prototyped. Currently they are sorted by 160 * declaration order. 161 */ 162 static void if_attachdomain(void *); 163 static void if_attachdomain1(struct ifnet *); 164 static int ifconf(u_long, caddr_t); 165 static void if_freemulti(struct ifmultiaddr *); 166 static void if_init(void *); 167 static void if_grow(void); 168 static void if_route(struct ifnet *, int flag, int fam); 169 static int if_setflag(struct ifnet *, int, int, int *, int); 170 static int if_transmit(struct ifnet *ifp, struct mbuf *m); 171 static void if_unroute(struct ifnet *, int flag, int fam); 172 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 173 static int if_rtdel(struct radix_node *, void *); 174 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *); 175 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int); 176 static void do_link_state_change(void *, int); 177 static int if_getgroup(struct ifgroupreq *, struct ifnet *); 178 static int if_getgroupmembers(struct ifgroupreq *); 179 static void if_delgroups(struct ifnet *); 180 static void if_attach_internal(struct ifnet *, int); 181 static void if_detach_internal(struct ifnet *, int); 182 183 #ifdef INET6 184 /* 185 * XXX: declare here to avoid to include many inet6 related files.. 186 * should be more generalized? 187 */ 188 extern void nd6_setmtu(struct ifnet *); 189 #endif 190 191 VNET_DEFINE(int, if_index); 192 int ifqmaxlen = IFQ_MAXLEN; 193 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */ 194 VNET_DEFINE(struct ifgrouphead, ifg_head); 195 196 static VNET_DEFINE(int, if_indexlim) = 8; 197 198 /* Table of ifnet by index. */ 199 VNET_DEFINE(struct ifindex_entry *, ifindex_table); 200 201 #define V_if_indexlim VNET(if_indexlim) 202 #define V_ifindex_table VNET(ifindex_table) 203 204 /* 205 * The global network interface list (V_ifnet) and related state (such as 206 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and 207 * an rwlock. Either may be acquired shared to stablize the list, but both 208 * must be acquired writable to modify the list. This model allows us to 209 * both stablize the interface list during interrupt thread processing, but 210 * also to stablize it over long-running ioctls, without introducing priority 211 * inversions and deadlocks. 212 */ 213 struct rwlock ifnet_rwlock; 214 struct sx ifnet_sxlock; 215 216 /* 217 * The allocation of network interfaces is a rather non-atomic affair; we 218 * need to select an index before we are ready to expose the interface for 219 * use, so will use this pointer value to indicate reservation. 220 */ 221 #define IFNET_HOLD (void *)(uintptr_t)(-1) 222 223 static if_com_alloc_t *if_com_alloc[256]; 224 static if_com_free_t *if_com_free[256]; 225 226 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals"); 227 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 228 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 229 230 struct ifnet * 231 ifnet_byindex_locked(u_short idx) 232 { 233 234 if (idx > V_if_index) 235 return (NULL); 236 if (V_ifindex_table[idx].ife_ifnet == IFNET_HOLD) 237 return (NULL); 238 return (V_ifindex_table[idx].ife_ifnet); 239 } 240 241 struct ifnet * 242 ifnet_byindex(u_short idx) 243 { 244 struct ifnet *ifp; 245 246 IFNET_RLOCK_NOSLEEP(); 247 ifp = ifnet_byindex_locked(idx); 248 IFNET_RUNLOCK_NOSLEEP(); 249 return (ifp); 250 } 251 252 struct ifnet * 253 ifnet_byindex_ref(u_short idx) 254 { 255 struct ifnet *ifp; 256 257 IFNET_RLOCK_NOSLEEP(); 258 ifp = ifnet_byindex_locked(idx); 259 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) { 260 IFNET_RUNLOCK_NOSLEEP(); 261 return (NULL); 262 } 263 if_ref(ifp); 264 IFNET_RUNLOCK_NOSLEEP(); 265 return (ifp); 266 } 267 268 /* 269 * Allocate an ifindex array entry; return 0 on success or an error on 270 * failure. 271 */ 272 static int 273 ifindex_alloc_locked(u_short *idxp) 274 { 275 u_short idx; 276 277 IFNET_WLOCK_ASSERT(); 278 279 retry: 280 /* 281 * Try to find an empty slot below V_if_index. If we fail, take the 282 * next slot. 283 */ 284 for (idx = 1; idx <= V_if_index; idx++) { 285 if (V_ifindex_table[idx].ife_ifnet == NULL) 286 break; 287 } 288 289 /* Catch if_index overflow. */ 290 if (idx >= V_if_indexlim) { 291 if_grow(); 292 goto retry; 293 } 294 if (idx > V_if_index) 295 V_if_index = idx; 296 *idxp = idx; 297 return (0); 298 } 299 300 static void 301 ifindex_free_locked(u_short idx) 302 { 303 304 IFNET_WLOCK_ASSERT(); 305 306 V_ifindex_table[idx].ife_ifnet = NULL; 307 while (V_if_index > 0 && 308 V_ifindex_table[V_if_index].ife_ifnet == NULL) 309 V_if_index--; 310 } 311 312 static void 313 ifindex_free(u_short idx) 314 { 315 316 IFNET_WLOCK(); 317 ifindex_free_locked(idx); 318 IFNET_WUNLOCK(); 319 } 320 321 static void 322 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp) 323 { 324 325 IFNET_WLOCK_ASSERT(); 326 327 V_ifindex_table[idx].ife_ifnet = ifp; 328 } 329 330 static void 331 ifnet_setbyindex(u_short idx, struct ifnet *ifp) 332 { 333 334 IFNET_WLOCK(); 335 ifnet_setbyindex_locked(idx, ifp); 336 IFNET_WUNLOCK(); 337 } 338 339 struct ifaddr * 340 ifaddr_byindex(u_short idx) 341 { 342 struct ifaddr *ifa; 343 344 IFNET_RLOCK_NOSLEEP(); 345 ifa = ifnet_byindex_locked(idx)->if_addr; 346 if (ifa != NULL) 347 ifa_ref(ifa); 348 IFNET_RUNLOCK_NOSLEEP(); 349 return (ifa); 350 } 351 352 /* 353 * Network interface utility routines. 354 * 355 * Routines with ifa_ifwith* names take sockaddr *'s as 356 * parameters. 357 */ 358 359 static void 360 vnet_if_init(const void *unused __unused) 361 { 362 363 TAILQ_INIT(&V_ifnet); 364 TAILQ_INIT(&V_ifg_head); 365 IFNET_WLOCK(); 366 if_grow(); /* create initial table */ 367 IFNET_WUNLOCK(); 368 vnet_if_clone_init(); 369 } 370 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init, 371 NULL); 372 373 /* ARGSUSED*/ 374 static void 375 if_init(void *dummy __unused) 376 { 377 378 IFNET_LOCK_INIT(); 379 if_clone_init(); 380 } 381 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL); 382 383 384 #ifdef VIMAGE 385 static void 386 vnet_if_uninit(const void *unused __unused) 387 { 388 389 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p " 390 "not empty", __func__, __LINE__, &V_ifnet)); 391 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p " 392 "not empty", __func__, __LINE__, &V_ifg_head)); 393 394 free((caddr_t)V_ifindex_table, M_IFNET); 395 } 396 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST, 397 vnet_if_uninit, NULL); 398 #endif 399 400 static void 401 if_grow(void) 402 { 403 int oldlim; 404 u_int n; 405 struct ifindex_entry *e; 406 407 IFNET_WLOCK_ASSERT(); 408 oldlim = V_if_indexlim; 409 IFNET_WUNLOCK(); 410 n = (oldlim << 1) * sizeof(*e); 411 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO); 412 IFNET_WLOCK(); 413 if (V_if_indexlim != oldlim) { 414 free(e, M_IFNET); 415 return; 416 } 417 if (V_ifindex_table != NULL) { 418 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2); 419 free((caddr_t)V_ifindex_table, M_IFNET); 420 } 421 V_if_indexlim <<= 1; 422 V_ifindex_table = e; 423 } 424 425 /* 426 * Allocate a struct ifnet and an index for an interface. A layer 2 427 * common structure will also be allocated if an allocation routine is 428 * registered for the passed type. 429 */ 430 struct ifnet * 431 if_alloc(u_char type) 432 { 433 struct ifnet *ifp; 434 u_short idx; 435 436 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO); 437 IFNET_WLOCK(); 438 if (ifindex_alloc_locked(&idx) != 0) { 439 IFNET_WUNLOCK(); 440 free(ifp, M_IFNET); 441 return (NULL); 442 } 443 ifnet_setbyindex_locked(idx, IFNET_HOLD); 444 IFNET_WUNLOCK(); 445 ifp->if_index = idx; 446 ifp->if_type = type; 447 ifp->if_alloctype = type; 448 if (if_com_alloc[type] != NULL) { 449 ifp->if_l2com = if_com_alloc[type](type, ifp); 450 if (ifp->if_l2com == NULL) { 451 free(ifp, M_IFNET); 452 ifindex_free(idx); 453 return (NULL); 454 } 455 } 456 457 IF_ADDR_LOCK_INIT(ifp); 458 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp); 459 ifp->if_afdata_initialized = 0; 460 IF_AFDATA_LOCK_INIT(ifp); 461 TAILQ_INIT(&ifp->if_addrhead); 462 TAILQ_INIT(&ifp->if_multiaddrs); 463 TAILQ_INIT(&ifp->if_groups); 464 #ifdef MAC 465 mac_ifnet_init(ifp); 466 #endif 467 ifq_init(&ifp->if_snd, ifp); 468 469 refcount_init(&ifp->if_refcount, 1); /* Index reference. */ 470 ifnet_setbyindex(ifp->if_index, ifp); 471 return (ifp); 472 } 473 474 /* 475 * Do the actual work of freeing a struct ifnet, and layer 2 common 476 * structure. This call is made when the last reference to an 477 * interface is released. 478 */ 479 static void 480 if_free_internal(struct ifnet *ifp) 481 { 482 483 KASSERT((ifp->if_flags & IFF_DYING), 484 ("if_free_internal: interface not dying")); 485 486 if (if_com_free[ifp->if_alloctype] != NULL) 487 if_com_free[ifp->if_alloctype](ifp->if_l2com, 488 ifp->if_alloctype); 489 490 #ifdef MAC 491 mac_ifnet_destroy(ifp); 492 #endif /* MAC */ 493 if (ifp->if_description != NULL) 494 free(ifp->if_description, M_IFDESCR); 495 IF_AFDATA_DESTROY(ifp); 496 IF_ADDR_LOCK_DESTROY(ifp); 497 ifq_delete(&ifp->if_snd); 498 free(ifp, M_IFNET); 499 } 500 501 /* 502 * Deregister an interface and free the associated storage. 503 */ 504 void 505 if_free(struct ifnet *ifp) 506 { 507 508 ifp->if_flags |= IFF_DYING; /* XXX: Locking */ 509 510 CURVNET_SET_QUIET(ifp->if_vnet); 511 IFNET_WLOCK(); 512 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index), 513 ("%s: freeing unallocated ifnet", ifp->if_xname)); 514 515 ifindex_free_locked(ifp->if_index); 516 IFNET_WUNLOCK(); 517 518 if (refcount_release(&ifp->if_refcount)) 519 if_free_internal(ifp); 520 CURVNET_RESTORE(); 521 } 522 523 /* 524 * Interfaces to keep an ifnet type-stable despite the possibility of the 525 * driver calling if_free(). If there are additional references, we defer 526 * freeing the underlying data structure. 527 */ 528 void 529 if_ref(struct ifnet *ifp) 530 { 531 532 /* We don't assert the ifnet list lock here, but arguably should. */ 533 refcount_acquire(&ifp->if_refcount); 534 } 535 536 void 537 if_rele(struct ifnet *ifp) 538 { 539 540 if (!refcount_release(&ifp->if_refcount)) 541 return; 542 if_free_internal(ifp); 543 } 544 545 void 546 ifq_init(struct ifaltq *ifq, struct ifnet *ifp) 547 { 548 549 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF); 550 551 if (ifq->ifq_maxlen == 0) 552 ifq->ifq_maxlen = ifqmaxlen; 553 554 ifq->altq_type = 0; 555 ifq->altq_disc = NULL; 556 ifq->altq_flags &= ALTQF_CANTCHANGE; 557 ifq->altq_tbr = NULL; 558 ifq->altq_ifp = ifp; 559 } 560 561 void 562 ifq_delete(struct ifaltq *ifq) 563 { 564 mtx_destroy(&ifq->ifq_mtx); 565 } 566 567 /* 568 * Perform generic interface initalization tasks and attach the interface 569 * to the list of "active" interfaces. If vmove flag is set on entry 570 * to if_attach_internal(), perform only a limited subset of initialization 571 * tasks, given that we are moving from one vnet to another an ifnet which 572 * has already been fully initialized. 573 * 574 * XXX: 575 * - The decision to return void and thus require this function to 576 * succeed is questionable. 577 * - We should probably do more sanity checking. For instance we don't 578 * do anything to insure if_xname is unique or non-empty. 579 */ 580 void 581 if_attach(struct ifnet *ifp) 582 { 583 584 if_attach_internal(ifp, 0); 585 } 586 587 static void 588 if_attach_internal(struct ifnet *ifp, int vmove) 589 { 590 unsigned socksize, ifasize; 591 int namelen, masklen; 592 struct sockaddr_dl *sdl; 593 struct ifaddr *ifa; 594 595 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index)) 596 panic ("%s: BUG: if_attach called without if_alloc'd input()\n", 597 ifp->if_xname); 598 599 #ifdef VIMAGE 600 ifp->if_vnet = curvnet; 601 if (ifp->if_home_vnet == NULL) 602 ifp->if_home_vnet = curvnet; 603 #endif 604 605 if_addgroup(ifp, IFG_ALL); 606 607 getmicrotime(&ifp->if_lastchange); 608 ifp->if_data.ifi_epoch = time_uptime; 609 ifp->if_data.ifi_datalen = sizeof(struct if_data); 610 611 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) || 612 (ifp->if_transmit != NULL && ifp->if_qflush != NULL), 613 ("transmit and qflush must both either be set or both be NULL")); 614 if (ifp->if_transmit == NULL) { 615 ifp->if_transmit = if_transmit; 616 ifp->if_qflush = if_qflush; 617 } 618 619 if (!vmove) { 620 #ifdef MAC 621 mac_ifnet_create(ifp); 622 #endif 623 624 /* 625 * Create a Link Level name for this device. 626 */ 627 namelen = strlen(ifp->if_xname); 628 /* 629 * Always save enough space for any possiable name so we 630 * can do a rename in place later. 631 */ 632 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ; 633 socksize = masklen + ifp->if_addrlen; 634 if (socksize < sizeof(*sdl)) 635 socksize = sizeof(*sdl); 636 socksize = roundup2(socksize, sizeof(long)); 637 ifasize = sizeof(*ifa) + 2 * socksize; 638 ifa = ifa_alloc(ifasize, M_WAITOK); 639 sdl = (struct sockaddr_dl *)(ifa + 1); 640 sdl->sdl_len = socksize; 641 sdl->sdl_family = AF_LINK; 642 bcopy(ifp->if_xname, sdl->sdl_data, namelen); 643 sdl->sdl_nlen = namelen; 644 sdl->sdl_index = ifp->if_index; 645 sdl->sdl_type = ifp->if_type; 646 ifp->if_addr = ifa; 647 ifa->ifa_ifp = ifp; 648 ifa->ifa_rtrequest = link_rtrequest; 649 ifa->ifa_addr = (struct sockaddr *)sdl; 650 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 651 ifa->ifa_netmask = (struct sockaddr *)sdl; 652 sdl->sdl_len = masklen; 653 while (namelen != 0) 654 sdl->sdl_data[--namelen] = 0xff; 655 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link); 656 /* Reliably crash if used uninitialized. */ 657 ifp->if_broadcastaddr = NULL; 658 659 #if defined(INET) || defined(INET6) 660 /* Initialize to max value. */ 661 if (ifp->if_hw_tsomax == 0) 662 ifp->if_hw_tsomax = min(IP_MAXPACKET, 32 * MCLBYTES - 663 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN)); 664 KASSERT(ifp->if_hw_tsomax <= IP_MAXPACKET && 665 ifp->if_hw_tsomax >= IP_MAXPACKET / 8, 666 ("%s: tsomax outside of range", __func__)); 667 #endif 668 } 669 #ifdef VIMAGE 670 else { 671 /* 672 * Update the interface index in the link layer address 673 * of the interface. 674 */ 675 for (ifa = ifp->if_addr; ifa != NULL; 676 ifa = TAILQ_NEXT(ifa, ifa_link)) { 677 if (ifa->ifa_addr->sa_family == AF_LINK) { 678 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 679 sdl->sdl_index = ifp->if_index; 680 } 681 } 682 } 683 #endif 684 685 IFNET_WLOCK(); 686 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link); 687 #ifdef VIMAGE 688 curvnet->vnet_ifcnt++; 689 #endif 690 IFNET_WUNLOCK(); 691 692 if (domain_init_status >= 2) 693 if_attachdomain1(ifp); 694 695 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 696 if (IS_DEFAULT_VNET(curvnet)) 697 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL); 698 699 /* Announce the interface. */ 700 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 701 } 702 703 static void 704 if_attachdomain(void *dummy) 705 { 706 struct ifnet *ifp; 707 708 TAILQ_FOREACH(ifp, &V_ifnet, if_link) 709 if_attachdomain1(ifp); 710 } 711 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND, 712 if_attachdomain, NULL); 713 714 static void 715 if_attachdomain1(struct ifnet *ifp) 716 { 717 struct domain *dp; 718 719 /* 720 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we 721 * cannot lock ifp->if_afdata initialization, entirely. 722 */ 723 if (IF_AFDATA_TRYLOCK(ifp) == 0) 724 return; 725 if (ifp->if_afdata_initialized >= domain_init_status) { 726 IF_AFDATA_UNLOCK(ifp); 727 log(LOG_WARNING, "%s called more than once on %s\n", 728 __func__, ifp->if_xname); 729 return; 730 } 731 ifp->if_afdata_initialized = domain_init_status; 732 IF_AFDATA_UNLOCK(ifp); 733 734 /* address family dependent data region */ 735 bzero(ifp->if_afdata, sizeof(ifp->if_afdata)); 736 for (dp = domains; dp; dp = dp->dom_next) { 737 if (dp->dom_ifattach) 738 ifp->if_afdata[dp->dom_family] = 739 (*dp->dom_ifattach)(ifp); 740 } 741 } 742 743 /* 744 * Remove any unicast or broadcast network addresses from an interface. 745 */ 746 void 747 if_purgeaddrs(struct ifnet *ifp) 748 { 749 struct ifaddr *ifa, *next; 750 751 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) { 752 if (ifa->ifa_addr->sa_family == AF_LINK) 753 continue; 754 #ifdef INET 755 /* XXX: Ugly!! ad hoc just for INET */ 756 if (ifa->ifa_addr->sa_family == AF_INET) { 757 struct ifaliasreq ifr; 758 759 bzero(&ifr, sizeof(ifr)); 760 ifr.ifra_addr = *ifa->ifa_addr; 761 if (ifa->ifa_dstaddr) 762 ifr.ifra_broadaddr = *ifa->ifa_dstaddr; 763 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp, 764 NULL) == 0) 765 continue; 766 } 767 #endif /* INET */ 768 #ifdef INET6 769 if (ifa->ifa_addr->sa_family == AF_INET6) { 770 in6_purgeaddr(ifa); 771 /* ifp_addrhead is already updated */ 772 continue; 773 } 774 #endif /* INET6 */ 775 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 776 ifa_free(ifa); 777 } 778 } 779 780 /* 781 * Remove any multicast network addresses from an interface when an ifnet 782 * is going away. 783 */ 784 static void 785 if_purgemaddrs(struct ifnet *ifp) 786 { 787 struct ifmultiaddr *ifma; 788 struct ifmultiaddr *next; 789 790 IF_ADDR_WLOCK(ifp); 791 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) 792 if_delmulti_locked(ifp, ifma, 1); 793 IF_ADDR_WUNLOCK(ifp); 794 } 795 796 /* 797 * Detach an interface, removing it from the list of "active" interfaces. 798 * If vmove flag is set on entry to if_detach_internal(), perform only a 799 * limited subset of cleanup tasks, given that we are moving an ifnet from 800 * one vnet to another, where it must be fully operational. 801 * 802 * XXXRW: There are some significant questions about event ordering, and 803 * how to prevent things from starting to use the interface during detach. 804 */ 805 void 806 if_detach(struct ifnet *ifp) 807 { 808 809 CURVNET_SET_QUIET(ifp->if_vnet); 810 if_detach_internal(ifp, 0); 811 CURVNET_RESTORE(); 812 } 813 814 static void 815 if_detach_internal(struct ifnet *ifp, int vmove) 816 { 817 struct ifaddr *ifa; 818 struct radix_node_head *rnh; 819 int i, j; 820 struct domain *dp; 821 struct ifnet *iter; 822 int found = 0; 823 824 IFNET_WLOCK(); 825 TAILQ_FOREACH(iter, &V_ifnet, if_link) 826 if (iter == ifp) { 827 TAILQ_REMOVE(&V_ifnet, ifp, if_link); 828 found = 1; 829 break; 830 } 831 #ifdef VIMAGE 832 if (found) 833 curvnet->vnet_ifcnt--; 834 #endif 835 IFNET_WUNLOCK(); 836 if (!found) { 837 if (vmove) 838 panic("%s: ifp=%p not on the ifnet tailq %p", 839 __func__, ifp, &V_ifnet); 840 else 841 return; /* XXX this should panic as well? */ 842 } 843 844 /* 845 * Remove/wait for pending events. 846 */ 847 taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 848 849 /* 850 * Remove routes and flush queues. 851 */ 852 if_down(ifp); 853 #ifdef ALTQ 854 if (ALTQ_IS_ENABLED(&ifp->if_snd)) 855 altq_disable(&ifp->if_snd); 856 if (ALTQ_IS_ATTACHED(&ifp->if_snd)) 857 altq_detach(&ifp->if_snd); 858 #endif 859 860 if_purgeaddrs(ifp); 861 862 #ifdef INET 863 in_ifdetach(ifp); 864 #endif 865 866 #ifdef INET6 867 /* 868 * Remove all IPv6 kernel structs related to ifp. This should be done 869 * before removing routing entries below, since IPv6 interface direct 870 * routes are expected to be removed by the IPv6-specific kernel API. 871 * Otherwise, the kernel will detect some inconsistency and bark it. 872 */ 873 in6_ifdetach(ifp); 874 #endif 875 if_purgemaddrs(ifp); 876 877 if (!vmove) { 878 /* 879 * Prevent further calls into the device driver via ifnet. 880 */ 881 if_dead(ifp); 882 883 /* 884 * Remove link ifaddr pointer and maybe decrement if_index. 885 * Clean up all addresses. 886 */ 887 ifp->if_addr = NULL; 888 889 /* We can now free link ifaddr. */ 890 if (!TAILQ_EMPTY(&ifp->if_addrhead)) { 891 ifa = TAILQ_FIRST(&ifp->if_addrhead); 892 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 893 ifa_free(ifa); 894 } 895 } 896 897 /* 898 * Delete all remaining routes using this interface 899 * Unfortuneatly the only way to do this is to slog through 900 * the entire routing table looking for routes which point 901 * to this interface...oh well... 902 */ 903 for (i = 1; i <= AF_MAX; i++) { 904 for (j = 0; j < rt_numfibs; j++) { 905 rnh = rt_tables_get_rnh(j, i); 906 if (rnh == NULL) 907 continue; 908 RADIX_NODE_HEAD_LOCK(rnh); 909 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp); 910 RADIX_NODE_HEAD_UNLOCK(rnh); 911 } 912 } 913 914 /* Announce that the interface is gone. */ 915 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 916 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 917 if (IS_DEFAULT_VNET(curvnet)) 918 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL); 919 if_delgroups(ifp); 920 921 /* 922 * We cannot hold the lock over dom_ifdetach calls as they might 923 * sleep, for example trying to drain a callout, thus open up the 924 * theoretical race with re-attaching. 925 */ 926 IF_AFDATA_LOCK(ifp); 927 i = ifp->if_afdata_initialized; 928 ifp->if_afdata_initialized = 0; 929 IF_AFDATA_UNLOCK(ifp); 930 for (dp = domains; i > 0 && dp; dp = dp->dom_next) { 931 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) 932 (*dp->dom_ifdetach)(ifp, 933 ifp->if_afdata[dp->dom_family]); 934 } 935 } 936 937 #ifdef VIMAGE 938 /* 939 * if_vmove() performs a limited version of if_detach() in current 940 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg. 941 * An attempt is made to shrink if_index in current vnet, find an 942 * unused if_index in target vnet and calls if_grow() if necessary, 943 * and finally find an unused if_xname for the target vnet. 944 */ 945 void 946 if_vmove(struct ifnet *ifp, struct vnet *new_vnet) 947 { 948 u_short idx; 949 950 /* 951 * Detach from current vnet, but preserve LLADDR info, do not 952 * mark as dead etc. so that the ifnet can be reattached later. 953 */ 954 if_detach_internal(ifp, 1); 955 956 /* 957 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink 958 * the if_index for that vnet if possible. 959 * 960 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized, 961 * or we'd lock on one vnet and unlock on another. 962 */ 963 IFNET_WLOCK(); 964 ifindex_free_locked(ifp->if_index); 965 IFNET_WUNLOCK(); 966 967 /* 968 * Perform interface-specific reassignment tasks, if provided by 969 * the driver. 970 */ 971 if (ifp->if_reassign != NULL) 972 ifp->if_reassign(ifp, new_vnet, NULL); 973 974 /* 975 * Switch to the context of the target vnet. 976 */ 977 CURVNET_SET_QUIET(new_vnet); 978 979 IFNET_WLOCK(); 980 if (ifindex_alloc_locked(&idx) != 0) { 981 IFNET_WUNLOCK(); 982 panic("if_index overflow"); 983 } 984 ifp->if_index = idx; 985 ifnet_setbyindex_locked(ifp->if_index, ifp); 986 IFNET_WUNLOCK(); 987 988 if_attach_internal(ifp, 1); 989 990 CURVNET_RESTORE(); 991 } 992 993 /* 994 * Move an ifnet to or from another child prison/vnet, specified by the jail id. 995 */ 996 static int 997 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid) 998 { 999 struct prison *pr; 1000 struct ifnet *difp; 1001 1002 /* Try to find the prison within our visibility. */ 1003 sx_slock(&allprison_lock); 1004 pr = prison_find_child(td->td_ucred->cr_prison, jid); 1005 sx_sunlock(&allprison_lock); 1006 if (pr == NULL) 1007 return (ENXIO); 1008 prison_hold_locked(pr); 1009 mtx_unlock(&pr->pr_mtx); 1010 1011 /* Do not try to move the iface from and to the same prison. */ 1012 if (pr->pr_vnet == ifp->if_vnet) { 1013 prison_free(pr); 1014 return (EEXIST); 1015 } 1016 1017 /* Make sure the named iface does not exists in the dst. prison/vnet. */ 1018 /* XXX Lock interfaces to avoid races. */ 1019 CURVNET_SET_QUIET(pr->pr_vnet); 1020 difp = ifunit(ifname); 1021 CURVNET_RESTORE(); 1022 if (difp != NULL) { 1023 prison_free(pr); 1024 return (EEXIST); 1025 } 1026 1027 /* Move the interface into the child jail/vnet. */ 1028 if_vmove(ifp, pr->pr_vnet); 1029 1030 /* Report the new if_xname back to the userland. */ 1031 sprintf(ifname, "%s", ifp->if_xname); 1032 1033 prison_free(pr); 1034 return (0); 1035 } 1036 1037 static int 1038 if_vmove_reclaim(struct thread *td, char *ifname, int jid) 1039 { 1040 struct prison *pr; 1041 struct vnet *vnet_dst; 1042 struct ifnet *ifp; 1043 1044 /* Try to find the prison within our visibility. */ 1045 sx_slock(&allprison_lock); 1046 pr = prison_find_child(td->td_ucred->cr_prison, jid); 1047 sx_sunlock(&allprison_lock); 1048 if (pr == NULL) 1049 return (ENXIO); 1050 prison_hold_locked(pr); 1051 mtx_unlock(&pr->pr_mtx); 1052 1053 /* Make sure the named iface exists in the source prison/vnet. */ 1054 CURVNET_SET(pr->pr_vnet); 1055 ifp = ifunit(ifname); /* XXX Lock to avoid races. */ 1056 if (ifp == NULL) { 1057 CURVNET_RESTORE(); 1058 prison_free(pr); 1059 return (ENXIO); 1060 } 1061 1062 /* Do not try to move the iface from and to the same prison. */ 1063 vnet_dst = TD_TO_VNET(td); 1064 if (vnet_dst == ifp->if_vnet) { 1065 CURVNET_RESTORE(); 1066 prison_free(pr); 1067 return (EEXIST); 1068 } 1069 1070 /* Get interface back from child jail/vnet. */ 1071 if_vmove(ifp, vnet_dst); 1072 CURVNET_RESTORE(); 1073 1074 /* Report the new if_xname back to the userland. */ 1075 sprintf(ifname, "%s", ifp->if_xname); 1076 1077 prison_free(pr); 1078 return (0); 1079 } 1080 #endif /* VIMAGE */ 1081 1082 /* 1083 * Add a group to an interface 1084 */ 1085 int 1086 if_addgroup(struct ifnet *ifp, const char *groupname) 1087 { 1088 struct ifg_list *ifgl; 1089 struct ifg_group *ifg = NULL; 1090 struct ifg_member *ifgm; 1091 int new = 0; 1092 1093 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' && 1094 groupname[strlen(groupname) - 1] <= '9') 1095 return (EINVAL); 1096 1097 IFNET_WLOCK(); 1098 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 1099 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) { 1100 IFNET_WUNLOCK(); 1101 return (EEXIST); 1102 } 1103 1104 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP, 1105 M_NOWAIT)) == NULL) { 1106 IFNET_WUNLOCK(); 1107 return (ENOMEM); 1108 } 1109 1110 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member), 1111 M_TEMP, M_NOWAIT)) == NULL) { 1112 free(ifgl, M_TEMP); 1113 IFNET_WUNLOCK(); 1114 return (ENOMEM); 1115 } 1116 1117 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 1118 if (!strcmp(ifg->ifg_group, groupname)) 1119 break; 1120 1121 if (ifg == NULL) { 1122 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group), 1123 M_TEMP, M_NOWAIT)) == NULL) { 1124 free(ifgl, M_TEMP); 1125 free(ifgm, M_TEMP); 1126 IFNET_WUNLOCK(); 1127 return (ENOMEM); 1128 } 1129 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group)); 1130 ifg->ifg_refcnt = 0; 1131 TAILQ_INIT(&ifg->ifg_members); 1132 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next); 1133 new = 1; 1134 } 1135 1136 ifg->ifg_refcnt++; 1137 ifgl->ifgl_group = ifg; 1138 ifgm->ifgm_ifp = ifp; 1139 1140 IF_ADDR_WLOCK(ifp); 1141 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next); 1142 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next); 1143 IF_ADDR_WUNLOCK(ifp); 1144 1145 IFNET_WUNLOCK(); 1146 1147 if (new) 1148 EVENTHANDLER_INVOKE(group_attach_event, ifg); 1149 EVENTHANDLER_INVOKE(group_change_event, groupname); 1150 1151 return (0); 1152 } 1153 1154 /* 1155 * Remove a group from an interface 1156 */ 1157 int 1158 if_delgroup(struct ifnet *ifp, const char *groupname) 1159 { 1160 struct ifg_list *ifgl; 1161 struct ifg_member *ifgm; 1162 1163 IFNET_WLOCK(); 1164 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 1165 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) 1166 break; 1167 if (ifgl == NULL) { 1168 IFNET_WUNLOCK(); 1169 return (ENOENT); 1170 } 1171 1172 IF_ADDR_WLOCK(ifp); 1173 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next); 1174 IF_ADDR_WUNLOCK(ifp); 1175 1176 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) 1177 if (ifgm->ifgm_ifp == ifp) 1178 break; 1179 1180 if (ifgm != NULL) { 1181 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next); 1182 free(ifgm, M_TEMP); 1183 } 1184 1185 if (--ifgl->ifgl_group->ifg_refcnt == 0) { 1186 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next); 1187 IFNET_WUNLOCK(); 1188 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group); 1189 free(ifgl->ifgl_group, M_TEMP); 1190 } else 1191 IFNET_WUNLOCK(); 1192 1193 free(ifgl, M_TEMP); 1194 1195 EVENTHANDLER_INVOKE(group_change_event, groupname); 1196 1197 return (0); 1198 } 1199 1200 /* 1201 * Remove an interface from all groups 1202 */ 1203 static void 1204 if_delgroups(struct ifnet *ifp) 1205 { 1206 struct ifg_list *ifgl; 1207 struct ifg_member *ifgm; 1208 char groupname[IFNAMSIZ]; 1209 1210 IFNET_WLOCK(); 1211 while (!TAILQ_EMPTY(&ifp->if_groups)) { 1212 ifgl = TAILQ_FIRST(&ifp->if_groups); 1213 1214 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ); 1215 1216 IF_ADDR_WLOCK(ifp); 1217 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next); 1218 IF_ADDR_WUNLOCK(ifp); 1219 1220 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) 1221 if (ifgm->ifgm_ifp == ifp) 1222 break; 1223 1224 if (ifgm != NULL) { 1225 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, 1226 ifgm_next); 1227 free(ifgm, M_TEMP); 1228 } 1229 1230 if (--ifgl->ifgl_group->ifg_refcnt == 0) { 1231 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next); 1232 IFNET_WUNLOCK(); 1233 EVENTHANDLER_INVOKE(group_detach_event, 1234 ifgl->ifgl_group); 1235 free(ifgl->ifgl_group, M_TEMP); 1236 } else 1237 IFNET_WUNLOCK(); 1238 1239 free(ifgl, M_TEMP); 1240 1241 EVENTHANDLER_INVOKE(group_change_event, groupname); 1242 1243 IFNET_WLOCK(); 1244 } 1245 IFNET_WUNLOCK(); 1246 } 1247 1248 /* 1249 * Stores all groups from an interface in memory pointed 1250 * to by data 1251 */ 1252 static int 1253 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp) 1254 { 1255 int len, error; 1256 struct ifg_list *ifgl; 1257 struct ifg_req ifgrq, *ifgp; 1258 struct ifgroupreq *ifgr = data; 1259 1260 if (ifgr->ifgr_len == 0) { 1261 IF_ADDR_RLOCK(ifp); 1262 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 1263 ifgr->ifgr_len += sizeof(struct ifg_req); 1264 IF_ADDR_RUNLOCK(ifp); 1265 return (0); 1266 } 1267 1268 len = ifgr->ifgr_len; 1269 ifgp = ifgr->ifgr_groups; 1270 /* XXX: wire */ 1271 IF_ADDR_RLOCK(ifp); 1272 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) { 1273 if (len < sizeof(ifgrq)) { 1274 IF_ADDR_RUNLOCK(ifp); 1275 return (EINVAL); 1276 } 1277 bzero(&ifgrq, sizeof ifgrq); 1278 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group, 1279 sizeof(ifgrq.ifgrq_group)); 1280 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 1281 IF_ADDR_RUNLOCK(ifp); 1282 return (error); 1283 } 1284 len -= sizeof(ifgrq); 1285 ifgp++; 1286 } 1287 IF_ADDR_RUNLOCK(ifp); 1288 1289 return (0); 1290 } 1291 1292 /* 1293 * Stores all members of a group in memory pointed to by data 1294 */ 1295 static int 1296 if_getgroupmembers(struct ifgroupreq *data) 1297 { 1298 struct ifgroupreq *ifgr = data; 1299 struct ifg_group *ifg; 1300 struct ifg_member *ifgm; 1301 struct ifg_req ifgrq, *ifgp; 1302 int len, error; 1303 1304 IFNET_RLOCK(); 1305 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 1306 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name)) 1307 break; 1308 if (ifg == NULL) { 1309 IFNET_RUNLOCK(); 1310 return (ENOENT); 1311 } 1312 1313 if (ifgr->ifgr_len == 0) { 1314 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) 1315 ifgr->ifgr_len += sizeof(ifgrq); 1316 IFNET_RUNLOCK(); 1317 return (0); 1318 } 1319 1320 len = ifgr->ifgr_len; 1321 ifgp = ifgr->ifgr_groups; 1322 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) { 1323 if (len < sizeof(ifgrq)) { 1324 IFNET_RUNLOCK(); 1325 return (EINVAL); 1326 } 1327 bzero(&ifgrq, sizeof ifgrq); 1328 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname, 1329 sizeof(ifgrq.ifgrq_member)); 1330 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 1331 IFNET_RUNLOCK(); 1332 return (error); 1333 } 1334 len -= sizeof(ifgrq); 1335 ifgp++; 1336 } 1337 IFNET_RUNLOCK(); 1338 1339 return (0); 1340 } 1341 1342 /* 1343 * Delete Routes for a Network Interface 1344 * 1345 * Called for each routing entry via the rnh->rnh_walktree() call above 1346 * to delete all route entries referencing a detaching network interface. 1347 * 1348 * Arguments: 1349 * rn pointer to node in the routing table 1350 * arg argument passed to rnh->rnh_walktree() - detaching interface 1351 * 1352 * Returns: 1353 * 0 successful 1354 * errno failed - reason indicated 1355 * 1356 */ 1357 static int 1358 if_rtdel(struct radix_node *rn, void *arg) 1359 { 1360 struct rtentry *rt = (struct rtentry *)rn; 1361 struct ifnet *ifp = arg; 1362 int err; 1363 1364 if (rt->rt_ifp == ifp) { 1365 1366 /* 1367 * Protect (sorta) against walktree recursion problems 1368 * with cloned routes 1369 */ 1370 if ((rt->rt_flags & RTF_UP) == 0) 1371 return (0); 1372 1373 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway, 1374 rt_mask(rt), 1375 rt->rt_flags|RTF_RNH_LOCKED|RTF_PINNED, 1376 (struct rtentry **) NULL, rt->rt_fibnum); 1377 if (err) { 1378 log(LOG_WARNING, "if_rtdel: error %d\n", err); 1379 } 1380 } 1381 1382 return (0); 1383 } 1384 1385 /* 1386 * Wrapper functions for struct ifnet address list locking macros. These are 1387 * used by kernel modules to avoid encoding programming interface or binary 1388 * interface assumptions that may be violated when kernel-internal locking 1389 * approaches change. 1390 */ 1391 void 1392 if_addr_rlock(struct ifnet *ifp) 1393 { 1394 1395 IF_ADDR_RLOCK(ifp); 1396 } 1397 1398 void 1399 if_addr_runlock(struct ifnet *ifp) 1400 { 1401 1402 IF_ADDR_RUNLOCK(ifp); 1403 } 1404 1405 void 1406 if_maddr_rlock(if_t ifp) 1407 { 1408 1409 IF_ADDR_RLOCK((struct ifnet *)ifp); 1410 } 1411 1412 void 1413 if_maddr_runlock(if_t ifp) 1414 { 1415 1416 IF_ADDR_RUNLOCK((struct ifnet *)ifp); 1417 } 1418 1419 /* 1420 * Initialization, destruction and refcounting functions for ifaddrs. 1421 */ 1422 struct ifaddr * 1423 ifa_alloc(size_t size, int flags) 1424 { 1425 struct ifaddr *ifa; 1426 1427 KASSERT(size >= sizeof(struct ifaddr), 1428 ("%s: invalid size %zu", __func__, size)); 1429 1430 ifa = malloc(size, M_IFADDR, M_ZERO | flags); 1431 if (ifa == NULL) 1432 return (NULL); 1433 1434 if ((ifa->ifa_opackets = counter_u64_alloc(flags)) == NULL) 1435 goto fail; 1436 if ((ifa->ifa_ipackets = counter_u64_alloc(flags)) == NULL) 1437 goto fail; 1438 if ((ifa->ifa_obytes = counter_u64_alloc(flags)) == NULL) 1439 goto fail; 1440 if ((ifa->ifa_ibytes = counter_u64_alloc(flags)) == NULL) 1441 goto fail; 1442 1443 refcount_init(&ifa->ifa_refcnt, 1); 1444 1445 return (ifa); 1446 1447 fail: 1448 /* free(NULL) is okay */ 1449 counter_u64_free(ifa->ifa_opackets); 1450 counter_u64_free(ifa->ifa_ipackets); 1451 counter_u64_free(ifa->ifa_obytes); 1452 counter_u64_free(ifa->ifa_ibytes); 1453 free(ifa, M_IFADDR); 1454 1455 return (NULL); 1456 } 1457 1458 void 1459 ifa_ref(struct ifaddr *ifa) 1460 { 1461 1462 refcount_acquire(&ifa->ifa_refcnt); 1463 } 1464 1465 void 1466 ifa_free(struct ifaddr *ifa) 1467 { 1468 1469 if (refcount_release(&ifa->ifa_refcnt)) { 1470 counter_u64_free(ifa->ifa_opackets); 1471 counter_u64_free(ifa->ifa_ipackets); 1472 counter_u64_free(ifa->ifa_obytes); 1473 counter_u64_free(ifa->ifa_ibytes); 1474 free(ifa, M_IFADDR); 1475 } 1476 } 1477 1478 int 1479 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia) 1480 { 1481 int error = 0; 1482 struct rtentry *rt = NULL; 1483 struct rt_addrinfo info; 1484 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 1485 1486 bzero(&info, sizeof(info)); 1487 info.rti_ifp = V_loif; 1488 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC; 1489 info.rti_info[RTAX_DST] = ia; 1490 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl; 1491 error = rtrequest1_fib(RTM_ADD, &info, &rt, ifa->ifa_ifp->if_fib); 1492 1493 if (error == 0 && rt != NULL) { 1494 RT_LOCK(rt); 1495 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type = 1496 ifa->ifa_ifp->if_type; 1497 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index = 1498 ifa->ifa_ifp->if_index; 1499 RT_REMREF(rt); 1500 RT_UNLOCK(rt); 1501 } else if (error != 0) 1502 log(LOG_DEBUG, "%s: insertion failed: %u\n", __func__, error); 1503 1504 return (error); 1505 } 1506 1507 int 1508 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia) 1509 { 1510 int error = 0; 1511 struct rt_addrinfo info; 1512 struct sockaddr_dl null_sdl; 1513 1514 bzero(&null_sdl, sizeof(null_sdl)); 1515 null_sdl.sdl_len = sizeof(null_sdl); 1516 null_sdl.sdl_family = AF_LINK; 1517 null_sdl.sdl_type = ifa->ifa_ifp->if_type; 1518 null_sdl.sdl_index = ifa->ifa_ifp->if_index; 1519 bzero(&info, sizeof(info)); 1520 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC; 1521 info.rti_info[RTAX_DST] = ia; 1522 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl; 1523 error = rtrequest1_fib(RTM_DELETE, &info, NULL, ifa->ifa_ifp->if_fib); 1524 1525 if (error != 0) 1526 log(LOG_DEBUG, "%s: deletion failed: %u\n", __func__, error); 1527 1528 return (error); 1529 } 1530 1531 int 1532 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *sa, int fib) 1533 { 1534 struct rtentry *rt; 1535 1536 rt = rtalloc1_fib(sa, 0, 0, fib); 1537 if (rt == NULL) { 1538 log(LOG_DEBUG, "%s: fail", __func__); 1539 return (EHOSTUNREACH); 1540 } 1541 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type = 1542 ifa->ifa_ifp->if_type; 1543 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index = 1544 ifa->ifa_ifp->if_index; 1545 RTFREE_LOCKED(rt); 1546 1547 return (0); 1548 } 1549 1550 /* 1551 * XXX: Because sockaddr_dl has deeper structure than the sockaddr 1552 * structs used to represent other address families, it is necessary 1553 * to perform a different comparison. 1554 */ 1555 1556 #define sa_dl_equal(a1, a2) \ 1557 ((((struct sockaddr_dl *)(a1))->sdl_len == \ 1558 ((struct sockaddr_dl *)(a2))->sdl_len) && \ 1559 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \ 1560 LLADDR((struct sockaddr_dl *)(a2)), \ 1561 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0)) 1562 1563 /* 1564 * Locate an interface based on a complete address. 1565 */ 1566 /*ARGSUSED*/ 1567 static struct ifaddr * 1568 ifa_ifwithaddr_internal(struct sockaddr *addr, int getref) 1569 { 1570 struct ifnet *ifp; 1571 struct ifaddr *ifa; 1572 1573 IFNET_RLOCK_NOSLEEP(); 1574 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1575 IF_ADDR_RLOCK(ifp); 1576 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1577 if (ifa->ifa_addr->sa_family != addr->sa_family) 1578 continue; 1579 if (sa_equal(addr, ifa->ifa_addr)) { 1580 if (getref) 1581 ifa_ref(ifa); 1582 IF_ADDR_RUNLOCK(ifp); 1583 goto done; 1584 } 1585 /* IP6 doesn't have broadcast */ 1586 if ((ifp->if_flags & IFF_BROADCAST) && 1587 ifa->ifa_broadaddr && 1588 ifa->ifa_broadaddr->sa_len != 0 && 1589 sa_equal(ifa->ifa_broadaddr, addr)) { 1590 if (getref) 1591 ifa_ref(ifa); 1592 IF_ADDR_RUNLOCK(ifp); 1593 goto done; 1594 } 1595 } 1596 IF_ADDR_RUNLOCK(ifp); 1597 } 1598 ifa = NULL; 1599 done: 1600 IFNET_RUNLOCK_NOSLEEP(); 1601 return (ifa); 1602 } 1603 1604 struct ifaddr * 1605 ifa_ifwithaddr(struct sockaddr *addr) 1606 { 1607 1608 return (ifa_ifwithaddr_internal(addr, 1)); 1609 } 1610 1611 int 1612 ifa_ifwithaddr_check(struct sockaddr *addr) 1613 { 1614 1615 return (ifa_ifwithaddr_internal(addr, 0) != NULL); 1616 } 1617 1618 /* 1619 * Locate an interface based on the broadcast address. 1620 */ 1621 /* ARGSUSED */ 1622 struct ifaddr * 1623 ifa_ifwithbroadaddr(struct sockaddr *addr) 1624 { 1625 struct ifnet *ifp; 1626 struct ifaddr *ifa; 1627 1628 IFNET_RLOCK_NOSLEEP(); 1629 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1630 IF_ADDR_RLOCK(ifp); 1631 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1632 if (ifa->ifa_addr->sa_family != addr->sa_family) 1633 continue; 1634 if ((ifp->if_flags & IFF_BROADCAST) && 1635 ifa->ifa_broadaddr && 1636 ifa->ifa_broadaddr->sa_len != 0 && 1637 sa_equal(ifa->ifa_broadaddr, addr)) { 1638 ifa_ref(ifa); 1639 IF_ADDR_RUNLOCK(ifp); 1640 goto done; 1641 } 1642 } 1643 IF_ADDR_RUNLOCK(ifp); 1644 } 1645 ifa = NULL; 1646 done: 1647 IFNET_RUNLOCK_NOSLEEP(); 1648 return (ifa); 1649 } 1650 1651 /* 1652 * Locate the point to point interface with a given destination address. 1653 */ 1654 /*ARGSUSED*/ 1655 struct ifaddr * 1656 ifa_ifwithdstaddr_fib(struct sockaddr *addr, int fibnum) 1657 { 1658 struct ifnet *ifp; 1659 struct ifaddr *ifa; 1660 1661 IFNET_RLOCK_NOSLEEP(); 1662 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1663 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 1664 continue; 1665 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum)) 1666 continue; 1667 IF_ADDR_RLOCK(ifp); 1668 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1669 if (ifa->ifa_addr->sa_family != addr->sa_family) 1670 continue; 1671 if (ifa->ifa_dstaddr != NULL && 1672 sa_equal(addr, ifa->ifa_dstaddr)) { 1673 ifa_ref(ifa); 1674 IF_ADDR_RUNLOCK(ifp); 1675 goto done; 1676 } 1677 } 1678 IF_ADDR_RUNLOCK(ifp); 1679 } 1680 ifa = NULL; 1681 done: 1682 IFNET_RUNLOCK_NOSLEEP(); 1683 return (ifa); 1684 } 1685 1686 struct ifaddr * 1687 ifa_ifwithdstaddr(struct sockaddr *addr) 1688 { 1689 1690 return (ifa_ifwithdstaddr_fib(addr, RT_ALL_FIBS)); 1691 } 1692 1693 /* 1694 * Find an interface on a specific network. If many, choice 1695 * is most specific found. 1696 */ 1697 struct ifaddr * 1698 ifa_ifwithnet_fib(struct sockaddr *addr, int ignore_ptp, int fibnum) 1699 { 1700 struct ifnet *ifp; 1701 struct ifaddr *ifa; 1702 struct ifaddr *ifa_maybe = NULL; 1703 u_int af = addr->sa_family; 1704 char *addr_data = addr->sa_data, *cplim; 1705 1706 /* 1707 * AF_LINK addresses can be looked up directly by their index number, 1708 * so do that if we can. 1709 */ 1710 if (af == AF_LINK) { 1711 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; 1712 if (sdl->sdl_index && sdl->sdl_index <= V_if_index) 1713 return (ifaddr_byindex(sdl->sdl_index)); 1714 } 1715 1716 /* 1717 * Scan though each interface, looking for ones that have addresses 1718 * in this address family and the requested fib. Maintain a reference 1719 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that 1720 * kept it stable when we move onto the next interface. 1721 */ 1722 IFNET_RLOCK_NOSLEEP(); 1723 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1724 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum)) 1725 continue; 1726 IF_ADDR_RLOCK(ifp); 1727 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1728 char *cp, *cp2, *cp3; 1729 1730 if (ifa->ifa_addr->sa_family != af) 1731 next: continue; 1732 if (af == AF_INET && 1733 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) { 1734 /* 1735 * This is a bit broken as it doesn't 1736 * take into account that the remote end may 1737 * be a single node in the network we are 1738 * looking for. 1739 * The trouble is that we don't know the 1740 * netmask for the remote end. 1741 */ 1742 if (ifa->ifa_dstaddr != NULL && 1743 sa_equal(addr, ifa->ifa_dstaddr)) { 1744 ifa_ref(ifa); 1745 IF_ADDR_RUNLOCK(ifp); 1746 goto done; 1747 } 1748 } else { 1749 /* 1750 * Scan all the bits in the ifa's address. 1751 * If a bit dissagrees with what we are 1752 * looking for, mask it with the netmask 1753 * to see if it really matters. 1754 * (A byte at a time) 1755 */ 1756 if (ifa->ifa_netmask == 0) 1757 continue; 1758 cp = addr_data; 1759 cp2 = ifa->ifa_addr->sa_data; 1760 cp3 = ifa->ifa_netmask->sa_data; 1761 cplim = ifa->ifa_netmask->sa_len 1762 + (char *)ifa->ifa_netmask; 1763 while (cp3 < cplim) 1764 if ((*cp++ ^ *cp2++) & *cp3++) 1765 goto next; /* next address! */ 1766 /* 1767 * If the netmask of what we just found 1768 * is more specific than what we had before 1769 * (if we had one), or if the virtual status 1770 * of new prefix is better than of the old one, 1771 * then remember the new one before continuing 1772 * to search for an even better one. 1773 */ 1774 if (ifa_maybe == NULL || 1775 ifa_preferred(ifa_maybe, ifa) || 1776 rn_refines((caddr_t)ifa->ifa_netmask, 1777 (caddr_t)ifa_maybe->ifa_netmask)) { 1778 if (ifa_maybe != NULL) 1779 ifa_free(ifa_maybe); 1780 ifa_maybe = ifa; 1781 ifa_ref(ifa_maybe); 1782 } 1783 } 1784 } 1785 IF_ADDR_RUNLOCK(ifp); 1786 } 1787 ifa = ifa_maybe; 1788 ifa_maybe = NULL; 1789 done: 1790 IFNET_RUNLOCK_NOSLEEP(); 1791 if (ifa_maybe != NULL) 1792 ifa_free(ifa_maybe); 1793 return (ifa); 1794 } 1795 1796 struct ifaddr * 1797 ifa_ifwithnet(struct sockaddr *addr, int ignore_ptp) 1798 { 1799 1800 return (ifa_ifwithnet_fib(addr, ignore_ptp, RT_ALL_FIBS)); 1801 } 1802 1803 /* 1804 * Find an interface address specific to an interface best matching 1805 * a given address. 1806 */ 1807 struct ifaddr * 1808 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp) 1809 { 1810 struct ifaddr *ifa; 1811 char *cp, *cp2, *cp3; 1812 char *cplim; 1813 struct ifaddr *ifa_maybe = NULL; 1814 u_int af = addr->sa_family; 1815 1816 if (af >= AF_MAX) 1817 return (NULL); 1818 IF_ADDR_RLOCK(ifp); 1819 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1820 if (ifa->ifa_addr->sa_family != af) 1821 continue; 1822 if (ifa_maybe == NULL) 1823 ifa_maybe = ifa; 1824 if (ifa->ifa_netmask == 0) { 1825 if (sa_equal(addr, ifa->ifa_addr) || 1826 (ifa->ifa_dstaddr && 1827 sa_equal(addr, ifa->ifa_dstaddr))) 1828 goto done; 1829 continue; 1830 } 1831 if (ifp->if_flags & IFF_POINTOPOINT) { 1832 if (sa_equal(addr, ifa->ifa_dstaddr)) 1833 goto done; 1834 } else { 1835 cp = addr->sa_data; 1836 cp2 = ifa->ifa_addr->sa_data; 1837 cp3 = ifa->ifa_netmask->sa_data; 1838 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 1839 for (; cp3 < cplim; cp3++) 1840 if ((*cp++ ^ *cp2++) & *cp3) 1841 break; 1842 if (cp3 == cplim) 1843 goto done; 1844 } 1845 } 1846 ifa = ifa_maybe; 1847 done: 1848 if (ifa != NULL) 1849 ifa_ref(ifa); 1850 IF_ADDR_RUNLOCK(ifp); 1851 return (ifa); 1852 } 1853 1854 /* 1855 * See whether new ifa is better than current one: 1856 * 1) A non-virtual one is preferred over virtual. 1857 * 2) A virtual in master state preferred over any other state. 1858 * 1859 * Used in several address selecting functions. 1860 */ 1861 int 1862 ifa_preferred(struct ifaddr *cur, struct ifaddr *next) 1863 { 1864 1865 return (cur->ifa_carp && (!next->ifa_carp || 1866 ((*carp_master_p)(next) && !(*carp_master_p)(cur)))); 1867 } 1868 1869 #include <net/if_llatbl.h> 1870 1871 /* 1872 * Default action when installing a route with a Link Level gateway. 1873 * Lookup an appropriate real ifa to point to. 1874 * This should be moved to /sys/net/link.c eventually. 1875 */ 1876 static void 1877 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 1878 { 1879 struct ifaddr *ifa, *oifa; 1880 struct sockaddr *dst; 1881 struct ifnet *ifp; 1882 1883 RT_LOCK_ASSERT(rt); 1884 1885 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 1886 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 1887 return; 1888 ifa = ifaof_ifpforaddr(dst, ifp); 1889 if (ifa) { 1890 oifa = rt->rt_ifa; 1891 rt->rt_ifa = ifa; 1892 ifa_free(oifa); 1893 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 1894 ifa->ifa_rtrequest(cmd, rt, info); 1895 } 1896 } 1897 1898 struct sockaddr_dl * 1899 link_alloc_sdl(size_t size, int flags) 1900 { 1901 1902 return (malloc(size, M_TEMP, flags)); 1903 } 1904 1905 void 1906 link_free_sdl(struct sockaddr *sa) 1907 { 1908 free(sa, M_TEMP); 1909 } 1910 1911 /* 1912 * Fills in given sdl with interface basic info. 1913 * Returns pointer to filled sdl. 1914 */ 1915 struct sockaddr_dl * 1916 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype) 1917 { 1918 struct sockaddr_dl *sdl; 1919 1920 sdl = (struct sockaddr_dl *)paddr; 1921 memset(sdl, 0, sizeof(struct sockaddr_dl)); 1922 sdl->sdl_len = sizeof(struct sockaddr_dl); 1923 sdl->sdl_family = AF_LINK; 1924 sdl->sdl_index = ifp->if_index; 1925 sdl->sdl_type = iftype; 1926 1927 return (sdl); 1928 } 1929 1930 /* 1931 * Mark an interface down and notify protocols of 1932 * the transition. 1933 */ 1934 static void 1935 if_unroute(struct ifnet *ifp, int flag, int fam) 1936 { 1937 struct ifaddr *ifa; 1938 1939 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP")); 1940 1941 ifp->if_flags &= ~flag; 1942 getmicrotime(&ifp->if_lastchange); 1943 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1944 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1945 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 1946 ifp->if_qflush(ifp); 1947 1948 if (ifp->if_carp) 1949 (*carp_linkstate_p)(ifp); 1950 rt_ifmsg(ifp); 1951 } 1952 1953 /* 1954 * Mark an interface up and notify protocols of 1955 * the transition. 1956 */ 1957 static void 1958 if_route(struct ifnet *ifp, int flag, int fam) 1959 { 1960 struct ifaddr *ifa; 1961 1962 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP")); 1963 1964 ifp->if_flags |= flag; 1965 getmicrotime(&ifp->if_lastchange); 1966 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1967 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1968 pfctlinput(PRC_IFUP, ifa->ifa_addr); 1969 if (ifp->if_carp) 1970 (*carp_linkstate_p)(ifp); 1971 rt_ifmsg(ifp); 1972 #ifdef INET6 1973 in6_if_up(ifp); 1974 #endif 1975 } 1976 1977 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */ 1978 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */ 1979 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *); 1980 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t); 1981 int (*vlan_tag_p)(struct ifnet *, uint16_t *); 1982 int (*vlan_setcookie_p)(struct ifnet *, void *); 1983 void *(*vlan_cookie_p)(struct ifnet *); 1984 1985 /* 1986 * Handle a change in the interface link state. To avoid LORs 1987 * between driver lock and upper layer locks, as well as possible 1988 * recursions, we post event to taskqueue, and all job 1989 * is done in static do_link_state_change(). 1990 */ 1991 void 1992 if_link_state_change(struct ifnet *ifp, int link_state) 1993 { 1994 /* Return if state hasn't changed. */ 1995 if (ifp->if_link_state == link_state) 1996 return; 1997 1998 ifp->if_link_state = link_state; 1999 2000 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask); 2001 } 2002 2003 static void 2004 do_link_state_change(void *arg, int pending) 2005 { 2006 struct ifnet *ifp = (struct ifnet *)arg; 2007 int link_state = ifp->if_link_state; 2008 CURVNET_SET(ifp->if_vnet); 2009 2010 /* Notify that the link state has changed. */ 2011 rt_ifmsg(ifp); 2012 if (ifp->if_vlantrunk != NULL) 2013 (*vlan_link_state_p)(ifp); 2014 2015 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) && 2016 IFP2AC(ifp)->ac_netgraph != NULL) 2017 (*ng_ether_link_state_p)(ifp, link_state); 2018 if (ifp->if_carp) 2019 (*carp_linkstate_p)(ifp); 2020 if (ifp->if_bridge) 2021 (*bridge_linkstate_p)(ifp); 2022 if (ifp->if_lagg) 2023 (*lagg_linkstate_p)(ifp, link_state); 2024 2025 if (IS_DEFAULT_VNET(curvnet)) 2026 devctl_notify("IFNET", ifp->if_xname, 2027 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", 2028 NULL); 2029 if (pending > 1) 2030 if_printf(ifp, "%d link states coalesced\n", pending); 2031 if (log_link_state_change) 2032 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname, 2033 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" ); 2034 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, ifp->if_link_state); 2035 CURVNET_RESTORE(); 2036 } 2037 2038 /* 2039 * Mark an interface down and notify protocols of 2040 * the transition. 2041 */ 2042 void 2043 if_down(struct ifnet *ifp) 2044 { 2045 2046 if_unroute(ifp, IFF_UP, AF_UNSPEC); 2047 } 2048 2049 /* 2050 * Mark an interface up and notify protocols of 2051 * the transition. 2052 */ 2053 void 2054 if_up(struct ifnet *ifp) 2055 { 2056 2057 if_route(ifp, IFF_UP, AF_UNSPEC); 2058 } 2059 2060 /* 2061 * Flush an interface queue. 2062 */ 2063 void 2064 if_qflush(struct ifnet *ifp) 2065 { 2066 struct mbuf *m, *n; 2067 struct ifaltq *ifq; 2068 2069 ifq = &ifp->if_snd; 2070 IFQ_LOCK(ifq); 2071 #ifdef ALTQ 2072 if (ALTQ_IS_ENABLED(ifq)) 2073 ALTQ_PURGE(ifq); 2074 #endif 2075 n = ifq->ifq_head; 2076 while ((m = n) != 0) { 2077 n = m->m_nextpkt; 2078 m_freem(m); 2079 } 2080 ifq->ifq_head = 0; 2081 ifq->ifq_tail = 0; 2082 ifq->ifq_len = 0; 2083 IFQ_UNLOCK(ifq); 2084 } 2085 2086 /* 2087 * Map interface name to interface structure pointer, with or without 2088 * returning a reference. 2089 */ 2090 struct ifnet * 2091 ifunit_ref(const char *name) 2092 { 2093 struct ifnet *ifp; 2094 2095 IFNET_RLOCK_NOSLEEP(); 2096 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2097 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 && 2098 !(ifp->if_flags & IFF_DYING)) 2099 break; 2100 } 2101 if (ifp != NULL) 2102 if_ref(ifp); 2103 IFNET_RUNLOCK_NOSLEEP(); 2104 return (ifp); 2105 } 2106 2107 struct ifnet * 2108 ifunit(const char *name) 2109 { 2110 struct ifnet *ifp; 2111 2112 IFNET_RLOCK_NOSLEEP(); 2113 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2114 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0) 2115 break; 2116 } 2117 IFNET_RUNLOCK_NOSLEEP(); 2118 return (ifp); 2119 } 2120 2121 /* 2122 * Hardware specific interface ioctls. 2123 */ 2124 static int 2125 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td) 2126 { 2127 struct ifreq *ifr; 2128 int error = 0; 2129 int new_flags, temp_flags; 2130 size_t namelen, onamelen; 2131 size_t descrlen; 2132 char *descrbuf, *odescrbuf; 2133 char new_name[IFNAMSIZ]; 2134 struct ifaddr *ifa; 2135 struct sockaddr_dl *sdl; 2136 2137 ifr = (struct ifreq *)data; 2138 switch (cmd) { 2139 case SIOCGIFINDEX: 2140 ifr->ifr_index = ifp->if_index; 2141 break; 2142 2143 case SIOCGIFFLAGS: 2144 temp_flags = ifp->if_flags | ifp->if_drv_flags; 2145 ifr->ifr_flags = temp_flags & 0xffff; 2146 ifr->ifr_flagshigh = temp_flags >> 16; 2147 break; 2148 2149 case SIOCGIFCAP: 2150 ifr->ifr_reqcap = ifp->if_capabilities; 2151 ifr->ifr_curcap = ifp->if_capenable; 2152 break; 2153 2154 #ifdef MAC 2155 case SIOCGIFMAC: 2156 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp); 2157 break; 2158 #endif 2159 2160 case SIOCGIFMETRIC: 2161 ifr->ifr_metric = ifp->if_metric; 2162 break; 2163 2164 case SIOCGIFMTU: 2165 ifr->ifr_mtu = ifp->if_mtu; 2166 break; 2167 2168 case SIOCGIFPHYS: 2169 ifr->ifr_phys = ifp->if_physical; 2170 break; 2171 2172 case SIOCGIFDESCR: 2173 error = 0; 2174 sx_slock(&ifdescr_sx); 2175 if (ifp->if_description == NULL) 2176 error = ENOMSG; 2177 else { 2178 /* space for terminating nul */ 2179 descrlen = strlen(ifp->if_description) + 1; 2180 if (ifr->ifr_buffer.length < descrlen) 2181 ifr->ifr_buffer.buffer = NULL; 2182 else 2183 error = copyout(ifp->if_description, 2184 ifr->ifr_buffer.buffer, descrlen); 2185 ifr->ifr_buffer.length = descrlen; 2186 } 2187 sx_sunlock(&ifdescr_sx); 2188 break; 2189 2190 case SIOCSIFDESCR: 2191 error = priv_check(td, PRIV_NET_SETIFDESCR); 2192 if (error) 2193 return (error); 2194 2195 /* 2196 * Copy only (length-1) bytes to make sure that 2197 * if_description is always nul terminated. The 2198 * length parameter is supposed to count the 2199 * terminating nul in. 2200 */ 2201 if (ifr->ifr_buffer.length > ifdescr_maxlen) 2202 return (ENAMETOOLONG); 2203 else if (ifr->ifr_buffer.length == 0) 2204 descrbuf = NULL; 2205 else { 2206 descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR, 2207 M_WAITOK | M_ZERO); 2208 error = copyin(ifr->ifr_buffer.buffer, descrbuf, 2209 ifr->ifr_buffer.length - 1); 2210 if (error) { 2211 free(descrbuf, M_IFDESCR); 2212 break; 2213 } 2214 } 2215 2216 sx_xlock(&ifdescr_sx); 2217 odescrbuf = ifp->if_description; 2218 ifp->if_description = descrbuf; 2219 sx_xunlock(&ifdescr_sx); 2220 2221 getmicrotime(&ifp->if_lastchange); 2222 free(odescrbuf, M_IFDESCR); 2223 break; 2224 2225 case SIOCGIFFIB: 2226 ifr->ifr_fib = ifp->if_fib; 2227 break; 2228 2229 case SIOCSIFFIB: 2230 error = priv_check(td, PRIV_NET_SETIFFIB); 2231 if (error) 2232 return (error); 2233 if (ifr->ifr_fib >= rt_numfibs) 2234 return (EINVAL); 2235 2236 ifp->if_fib = ifr->ifr_fib; 2237 break; 2238 2239 case SIOCSIFFLAGS: 2240 error = priv_check(td, PRIV_NET_SETIFFLAGS); 2241 if (error) 2242 return (error); 2243 /* 2244 * Currently, no driver owned flags pass the IFF_CANTCHANGE 2245 * check, so we don't need special handling here yet. 2246 */ 2247 new_flags = (ifr->ifr_flags & 0xffff) | 2248 (ifr->ifr_flagshigh << 16); 2249 if (ifp->if_flags & IFF_UP && 2250 (new_flags & IFF_UP) == 0) { 2251 if_down(ifp); 2252 } else if (new_flags & IFF_UP && 2253 (ifp->if_flags & IFF_UP) == 0) { 2254 if_up(ifp); 2255 } 2256 /* See if permanently promiscuous mode bit is about to flip */ 2257 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) { 2258 if (new_flags & IFF_PPROMISC) 2259 ifp->if_flags |= IFF_PROMISC; 2260 else if (ifp->if_pcount == 0) 2261 ifp->if_flags &= ~IFF_PROMISC; 2262 log(LOG_INFO, "%s: permanently promiscuous mode %s\n", 2263 ifp->if_xname, 2264 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled"); 2265 } 2266 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 2267 (new_flags &~ IFF_CANTCHANGE); 2268 if (ifp->if_ioctl) { 2269 (void) (*ifp->if_ioctl)(ifp, cmd, data); 2270 } 2271 getmicrotime(&ifp->if_lastchange); 2272 break; 2273 2274 case SIOCSIFCAP: 2275 error = priv_check(td, PRIV_NET_SETIFCAP); 2276 if (error) 2277 return (error); 2278 if (ifp->if_ioctl == NULL) 2279 return (EOPNOTSUPP); 2280 if (ifr->ifr_reqcap & ~ifp->if_capabilities) 2281 return (EINVAL); 2282 error = (*ifp->if_ioctl)(ifp, cmd, data); 2283 if (error == 0) 2284 getmicrotime(&ifp->if_lastchange); 2285 break; 2286 2287 #ifdef MAC 2288 case SIOCSIFMAC: 2289 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp); 2290 break; 2291 #endif 2292 2293 case SIOCSIFNAME: 2294 error = priv_check(td, PRIV_NET_SETIFNAME); 2295 if (error) 2296 return (error); 2297 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL); 2298 if (error != 0) 2299 return (error); 2300 if (new_name[0] == '\0') 2301 return (EINVAL); 2302 if (ifunit(new_name) != NULL) 2303 return (EEXIST); 2304 2305 /* 2306 * XXX: Locking. Nothing else seems to lock if_flags, 2307 * and there are numerous other races with the 2308 * ifunit() checks not being atomic with namespace 2309 * changes (renames, vmoves, if_attach, etc). 2310 */ 2311 ifp->if_flags |= IFF_RENAMING; 2312 2313 /* Announce the departure of the interface. */ 2314 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 2315 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 2316 2317 log(LOG_INFO, "%s: changing name to '%s'\n", 2318 ifp->if_xname, new_name); 2319 2320 IF_ADDR_WLOCK(ifp); 2321 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname)); 2322 ifa = ifp->if_addr; 2323 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 2324 namelen = strlen(new_name); 2325 onamelen = sdl->sdl_nlen; 2326 /* 2327 * Move the address if needed. This is safe because we 2328 * allocate space for a name of length IFNAMSIZ when we 2329 * create this in if_attach(). 2330 */ 2331 if (namelen != onamelen) { 2332 bcopy(sdl->sdl_data + onamelen, 2333 sdl->sdl_data + namelen, sdl->sdl_alen); 2334 } 2335 bcopy(new_name, sdl->sdl_data, namelen); 2336 sdl->sdl_nlen = namelen; 2337 sdl = (struct sockaddr_dl *)ifa->ifa_netmask; 2338 bzero(sdl->sdl_data, onamelen); 2339 while (namelen != 0) 2340 sdl->sdl_data[--namelen] = 0xff; 2341 IF_ADDR_WUNLOCK(ifp); 2342 2343 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 2344 /* Announce the return of the interface. */ 2345 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 2346 2347 ifp->if_flags &= ~IFF_RENAMING; 2348 break; 2349 2350 #ifdef VIMAGE 2351 case SIOCSIFVNET: 2352 error = priv_check(td, PRIV_NET_SETIFVNET); 2353 if (error) 2354 return (error); 2355 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid); 2356 break; 2357 #endif 2358 2359 case SIOCSIFMETRIC: 2360 error = priv_check(td, PRIV_NET_SETIFMETRIC); 2361 if (error) 2362 return (error); 2363 ifp->if_metric = ifr->ifr_metric; 2364 getmicrotime(&ifp->if_lastchange); 2365 break; 2366 2367 case SIOCSIFPHYS: 2368 error = priv_check(td, PRIV_NET_SETIFPHYS); 2369 if (error) 2370 return (error); 2371 if (ifp->if_ioctl == NULL) 2372 return (EOPNOTSUPP); 2373 error = (*ifp->if_ioctl)(ifp, cmd, data); 2374 if (error == 0) 2375 getmicrotime(&ifp->if_lastchange); 2376 break; 2377 2378 case SIOCSIFMTU: 2379 { 2380 u_long oldmtu = ifp->if_mtu; 2381 2382 error = priv_check(td, PRIV_NET_SETIFMTU); 2383 if (error) 2384 return (error); 2385 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) 2386 return (EINVAL); 2387 if (ifp->if_ioctl == NULL) 2388 return (EOPNOTSUPP); 2389 error = (*ifp->if_ioctl)(ifp, cmd, data); 2390 if (error == 0) { 2391 getmicrotime(&ifp->if_lastchange); 2392 rt_ifmsg(ifp); 2393 } 2394 /* 2395 * If the link MTU changed, do network layer specific procedure. 2396 */ 2397 if (ifp->if_mtu != oldmtu) { 2398 #ifdef INET6 2399 nd6_setmtu(ifp); 2400 #endif 2401 } 2402 break; 2403 } 2404 2405 case SIOCADDMULTI: 2406 case SIOCDELMULTI: 2407 if (cmd == SIOCADDMULTI) 2408 error = priv_check(td, PRIV_NET_ADDMULTI); 2409 else 2410 error = priv_check(td, PRIV_NET_DELMULTI); 2411 if (error) 2412 return (error); 2413 2414 /* Don't allow group membership on non-multicast interfaces. */ 2415 if ((ifp->if_flags & IFF_MULTICAST) == 0) 2416 return (EOPNOTSUPP); 2417 2418 /* Don't let users screw up protocols' entries. */ 2419 if (ifr->ifr_addr.sa_family != AF_LINK) 2420 return (EINVAL); 2421 2422 if (cmd == SIOCADDMULTI) { 2423 struct ifmultiaddr *ifma; 2424 2425 /* 2426 * Userland is only permitted to join groups once 2427 * via the if_addmulti() KPI, because it cannot hold 2428 * struct ifmultiaddr * between calls. It may also 2429 * lose a race while we check if the membership 2430 * already exists. 2431 */ 2432 IF_ADDR_RLOCK(ifp); 2433 ifma = if_findmulti(ifp, &ifr->ifr_addr); 2434 IF_ADDR_RUNLOCK(ifp); 2435 if (ifma != NULL) 2436 error = EADDRINUSE; 2437 else 2438 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 2439 } else { 2440 error = if_delmulti(ifp, &ifr->ifr_addr); 2441 } 2442 if (error == 0) 2443 getmicrotime(&ifp->if_lastchange); 2444 break; 2445 2446 case SIOCSIFPHYADDR: 2447 case SIOCDIFPHYADDR: 2448 #ifdef INET6 2449 case SIOCSIFPHYADDR_IN6: 2450 #endif 2451 case SIOCSIFMEDIA: 2452 case SIOCSIFGENERIC: 2453 error = priv_check(td, PRIV_NET_HWIOCTL); 2454 if (error) 2455 return (error); 2456 if (ifp->if_ioctl == NULL) 2457 return (EOPNOTSUPP); 2458 error = (*ifp->if_ioctl)(ifp, cmd, data); 2459 if (error == 0) 2460 getmicrotime(&ifp->if_lastchange); 2461 break; 2462 2463 case SIOCGIFSTATUS: 2464 case SIOCGIFPSRCADDR: 2465 case SIOCGIFPDSTADDR: 2466 case SIOCGIFMEDIA: 2467 case SIOCGIFGENERIC: 2468 if (ifp->if_ioctl == NULL) 2469 return (EOPNOTSUPP); 2470 error = (*ifp->if_ioctl)(ifp, cmd, data); 2471 break; 2472 2473 case SIOCSIFLLADDR: 2474 error = priv_check(td, PRIV_NET_SETLLADDR); 2475 if (error) 2476 return (error); 2477 error = if_setlladdr(ifp, 2478 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); 2479 EVENTHANDLER_INVOKE(iflladdr_event, ifp); 2480 break; 2481 2482 case SIOCAIFGROUP: 2483 { 2484 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; 2485 2486 error = priv_check(td, PRIV_NET_ADDIFGROUP); 2487 if (error) 2488 return (error); 2489 if ((error = if_addgroup(ifp, ifgr->ifgr_group))) 2490 return (error); 2491 break; 2492 } 2493 2494 case SIOCGIFGROUP: 2495 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp))) 2496 return (error); 2497 break; 2498 2499 case SIOCDIFGROUP: 2500 { 2501 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; 2502 2503 error = priv_check(td, PRIV_NET_DELIFGROUP); 2504 if (error) 2505 return (error); 2506 if ((error = if_delgroup(ifp, ifgr->ifgr_group))) 2507 return (error); 2508 break; 2509 } 2510 2511 default: 2512 error = ENOIOCTL; 2513 break; 2514 } 2515 return (error); 2516 } 2517 2518 #ifdef COMPAT_FREEBSD32 2519 struct ifconf32 { 2520 int32_t ifc_len; 2521 union { 2522 uint32_t ifcu_buf; 2523 uint32_t ifcu_req; 2524 } ifc_ifcu; 2525 }; 2526 #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32) 2527 #endif 2528 2529 /* 2530 * Interface ioctls. 2531 */ 2532 int 2533 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td) 2534 { 2535 struct ifnet *ifp; 2536 struct ifreq *ifr; 2537 int error; 2538 int oif_flags; 2539 2540 CURVNET_SET(so->so_vnet); 2541 switch (cmd) { 2542 case SIOCGIFCONF: 2543 error = ifconf(cmd, data); 2544 CURVNET_RESTORE(); 2545 return (error); 2546 2547 #ifdef COMPAT_FREEBSD32 2548 case SIOCGIFCONF32: 2549 { 2550 struct ifconf32 *ifc32; 2551 struct ifconf ifc; 2552 2553 ifc32 = (struct ifconf32 *)data; 2554 ifc.ifc_len = ifc32->ifc_len; 2555 ifc.ifc_buf = PTRIN(ifc32->ifc_buf); 2556 2557 error = ifconf(SIOCGIFCONF, (void *)&ifc); 2558 CURVNET_RESTORE(); 2559 if (error == 0) 2560 ifc32->ifc_len = ifc.ifc_len; 2561 return (error); 2562 } 2563 #endif 2564 } 2565 ifr = (struct ifreq *)data; 2566 2567 switch (cmd) { 2568 #ifdef VIMAGE 2569 case SIOCSIFRVNET: 2570 error = priv_check(td, PRIV_NET_SETIFVNET); 2571 if (error == 0) 2572 error = if_vmove_reclaim(td, ifr->ifr_name, 2573 ifr->ifr_jid); 2574 CURVNET_RESTORE(); 2575 return (error); 2576 #endif 2577 case SIOCIFCREATE: 2578 case SIOCIFCREATE2: 2579 error = priv_check(td, PRIV_NET_IFCREATE); 2580 if (error == 0) 2581 error = if_clone_create(ifr->ifr_name, 2582 sizeof(ifr->ifr_name), 2583 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL); 2584 CURVNET_RESTORE(); 2585 return (error); 2586 case SIOCIFDESTROY: 2587 error = priv_check(td, PRIV_NET_IFDESTROY); 2588 if (error == 0) 2589 error = if_clone_destroy(ifr->ifr_name); 2590 CURVNET_RESTORE(); 2591 return (error); 2592 2593 case SIOCIFGCLONERS: 2594 error = if_clone_list((struct if_clonereq *)data); 2595 CURVNET_RESTORE(); 2596 return (error); 2597 case SIOCGIFGMEMB: 2598 error = if_getgroupmembers((struct ifgroupreq *)data); 2599 CURVNET_RESTORE(); 2600 return (error); 2601 #if defined(INET) || defined(INET6) 2602 case SIOCSVH: 2603 case SIOCGVH: 2604 if (carp_ioctl_p == NULL) 2605 error = EPROTONOSUPPORT; 2606 else 2607 error = (*carp_ioctl_p)(ifr, cmd, td); 2608 CURVNET_RESTORE(); 2609 return (error); 2610 #endif 2611 } 2612 2613 ifp = ifunit_ref(ifr->ifr_name); 2614 if (ifp == NULL) { 2615 CURVNET_RESTORE(); 2616 return (ENXIO); 2617 } 2618 2619 error = ifhwioctl(cmd, ifp, data, td); 2620 if (error != ENOIOCTL) { 2621 if_rele(ifp); 2622 CURVNET_RESTORE(); 2623 return (error); 2624 } 2625 2626 oif_flags = ifp->if_flags; 2627 if (so->so_proto == NULL) { 2628 if_rele(ifp); 2629 CURVNET_RESTORE(); 2630 return (EOPNOTSUPP); 2631 } 2632 2633 /* 2634 * Pass the request on to the socket control method, and if the 2635 * latter returns EOPNOTSUPP, directly to the interface. 2636 * 2637 * Make an exception for the legacy SIOCSIF* requests. Drivers 2638 * trust SIOCSIFADDR et al to come from an already privileged 2639 * layer, and do not perform any credentials checks or input 2640 * validation. 2641 */ 2642 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data, 2643 ifp, td)); 2644 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL && 2645 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR && 2646 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK) 2647 error = (*ifp->if_ioctl)(ifp, cmd, data); 2648 2649 if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 2650 #ifdef INET6 2651 if (ifp->if_flags & IFF_UP) 2652 in6_if_up(ifp); 2653 #endif 2654 } 2655 if_rele(ifp); 2656 CURVNET_RESTORE(); 2657 return (error); 2658 } 2659 2660 /* 2661 * The code common to handling reference counted flags, 2662 * e.g., in ifpromisc() and if_allmulti(). 2663 * The "pflag" argument can specify a permanent mode flag to check, 2664 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none. 2665 * 2666 * Only to be used on stack-owned flags, not driver-owned flags. 2667 */ 2668 static int 2669 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch) 2670 { 2671 struct ifreq ifr; 2672 int error; 2673 int oldflags, oldcount; 2674 2675 /* Sanity checks to catch programming errors */ 2676 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0, 2677 ("%s: setting driver-owned flag %d", __func__, flag)); 2678 2679 if (onswitch) 2680 KASSERT(*refcount >= 0, 2681 ("%s: increment negative refcount %d for flag %d", 2682 __func__, *refcount, flag)); 2683 else 2684 KASSERT(*refcount > 0, 2685 ("%s: decrement non-positive refcount %d for flag %d", 2686 __func__, *refcount, flag)); 2687 2688 /* In case this mode is permanent, just touch refcount */ 2689 if (ifp->if_flags & pflag) { 2690 *refcount += onswitch ? 1 : -1; 2691 return (0); 2692 } 2693 2694 /* Save ifnet parameters for if_ioctl() may fail */ 2695 oldcount = *refcount; 2696 oldflags = ifp->if_flags; 2697 2698 /* 2699 * See if we aren't the only and touching refcount is enough. 2700 * Actually toggle interface flag if we are the first or last. 2701 */ 2702 if (onswitch) { 2703 if ((*refcount)++) 2704 return (0); 2705 ifp->if_flags |= flag; 2706 } else { 2707 if (--(*refcount)) 2708 return (0); 2709 ifp->if_flags &= ~flag; 2710 } 2711 2712 /* Call down the driver since we've changed interface flags */ 2713 if (ifp->if_ioctl == NULL) { 2714 error = EOPNOTSUPP; 2715 goto recover; 2716 } 2717 ifr.ifr_flags = ifp->if_flags & 0xffff; 2718 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2719 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2720 if (error) 2721 goto recover; 2722 /* Notify userland that interface flags have changed */ 2723 rt_ifmsg(ifp); 2724 return (0); 2725 2726 recover: 2727 /* Recover after driver error */ 2728 *refcount = oldcount; 2729 ifp->if_flags = oldflags; 2730 return (error); 2731 } 2732 2733 /* 2734 * Set/clear promiscuous mode on interface ifp based on the truth value 2735 * of pswitch. The calls are reference counted so that only the first 2736 * "on" request actually has an effect, as does the final "off" request. 2737 * Results are undefined if the "off" and "on" requests are not matched. 2738 */ 2739 int 2740 ifpromisc(struct ifnet *ifp, int pswitch) 2741 { 2742 int error; 2743 int oldflags = ifp->if_flags; 2744 2745 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC, 2746 &ifp->if_pcount, pswitch); 2747 /* If promiscuous mode status has changed, log a message */ 2748 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC)) 2749 log(LOG_INFO, "%s: promiscuous mode %s\n", 2750 ifp->if_xname, 2751 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); 2752 return (error); 2753 } 2754 2755 /* 2756 * Return interface configuration 2757 * of system. List may be used 2758 * in later ioctl's (above) to get 2759 * other information. 2760 */ 2761 /*ARGSUSED*/ 2762 static int 2763 ifconf(u_long cmd, caddr_t data) 2764 { 2765 struct ifconf *ifc = (struct ifconf *)data; 2766 struct ifnet *ifp; 2767 struct ifaddr *ifa; 2768 struct ifreq ifr; 2769 struct sbuf *sb; 2770 int error, full = 0, valid_len, max_len; 2771 2772 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */ 2773 max_len = MAXPHYS - 1; 2774 2775 /* Prevent hostile input from being able to crash the system */ 2776 if (ifc->ifc_len <= 0) 2777 return (EINVAL); 2778 2779 again: 2780 if (ifc->ifc_len <= max_len) { 2781 max_len = ifc->ifc_len; 2782 full = 1; 2783 } 2784 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN); 2785 max_len = 0; 2786 valid_len = 0; 2787 2788 IFNET_RLOCK(); 2789 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2790 int addrs; 2791 2792 /* 2793 * Zero the ifr_name buffer to make sure we don't 2794 * disclose the contents of the stack. 2795 */ 2796 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name)); 2797 2798 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)) 2799 >= sizeof(ifr.ifr_name)) { 2800 sbuf_delete(sb); 2801 IFNET_RUNLOCK(); 2802 return (ENAMETOOLONG); 2803 } 2804 2805 addrs = 0; 2806 IF_ADDR_RLOCK(ifp); 2807 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2808 struct sockaddr *sa = ifa->ifa_addr; 2809 2810 if (prison_if(curthread->td_ucred, sa) != 0) 2811 continue; 2812 addrs++; 2813 if (sa->sa_len <= sizeof(*sa)) { 2814 ifr.ifr_addr = *sa; 2815 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2816 max_len += sizeof(ifr); 2817 } else { 2818 sbuf_bcat(sb, &ifr, 2819 offsetof(struct ifreq, ifr_addr)); 2820 max_len += offsetof(struct ifreq, ifr_addr); 2821 sbuf_bcat(sb, sa, sa->sa_len); 2822 max_len += sa->sa_len; 2823 } 2824 2825 if (sbuf_error(sb) == 0) 2826 valid_len = sbuf_len(sb); 2827 } 2828 IF_ADDR_RUNLOCK(ifp); 2829 if (addrs == 0) { 2830 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 2831 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2832 max_len += sizeof(ifr); 2833 2834 if (sbuf_error(sb) == 0) 2835 valid_len = sbuf_len(sb); 2836 } 2837 } 2838 IFNET_RUNLOCK(); 2839 2840 /* 2841 * If we didn't allocate enough space (uncommon), try again. If 2842 * we have already allocated as much space as we are allowed, 2843 * return what we've got. 2844 */ 2845 if (valid_len != max_len && !full) { 2846 sbuf_delete(sb); 2847 goto again; 2848 } 2849 2850 ifc->ifc_len = valid_len; 2851 sbuf_finish(sb); 2852 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len); 2853 sbuf_delete(sb); 2854 return (error); 2855 } 2856 2857 /* 2858 * Just like ifpromisc(), but for all-multicast-reception mode. 2859 */ 2860 int 2861 if_allmulti(struct ifnet *ifp, int onswitch) 2862 { 2863 2864 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch)); 2865 } 2866 2867 struct ifmultiaddr * 2868 if_findmulti(struct ifnet *ifp, struct sockaddr *sa) 2869 { 2870 struct ifmultiaddr *ifma; 2871 2872 IF_ADDR_LOCK_ASSERT(ifp); 2873 2874 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 2875 if (sa->sa_family == AF_LINK) { 2876 if (sa_dl_equal(ifma->ifma_addr, sa)) 2877 break; 2878 } else { 2879 if (sa_equal(ifma->ifma_addr, sa)) 2880 break; 2881 } 2882 } 2883 2884 return ifma; 2885 } 2886 2887 /* 2888 * Allocate a new ifmultiaddr and initialize based on passed arguments. We 2889 * make copies of passed sockaddrs. The ifmultiaddr will not be added to 2890 * the ifnet multicast address list here, so the caller must do that and 2891 * other setup work (such as notifying the device driver). The reference 2892 * count is initialized to 1. 2893 */ 2894 static struct ifmultiaddr * 2895 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa, 2896 int mflags) 2897 { 2898 struct ifmultiaddr *ifma; 2899 struct sockaddr *dupsa; 2900 2901 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags | 2902 M_ZERO); 2903 if (ifma == NULL) 2904 return (NULL); 2905 2906 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags); 2907 if (dupsa == NULL) { 2908 free(ifma, M_IFMADDR); 2909 return (NULL); 2910 } 2911 bcopy(sa, dupsa, sa->sa_len); 2912 ifma->ifma_addr = dupsa; 2913 2914 ifma->ifma_ifp = ifp; 2915 ifma->ifma_refcount = 1; 2916 ifma->ifma_protospec = NULL; 2917 2918 if (llsa == NULL) { 2919 ifma->ifma_lladdr = NULL; 2920 return (ifma); 2921 } 2922 2923 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags); 2924 if (dupsa == NULL) { 2925 free(ifma->ifma_addr, M_IFMADDR); 2926 free(ifma, M_IFMADDR); 2927 return (NULL); 2928 } 2929 bcopy(llsa, dupsa, llsa->sa_len); 2930 ifma->ifma_lladdr = dupsa; 2931 2932 return (ifma); 2933 } 2934 2935 /* 2936 * if_freemulti: free ifmultiaddr structure and possibly attached related 2937 * addresses. The caller is responsible for implementing reference 2938 * counting, notifying the driver, handling routing messages, and releasing 2939 * any dependent link layer state. 2940 */ 2941 static void 2942 if_freemulti(struct ifmultiaddr *ifma) 2943 { 2944 2945 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d", 2946 ifma->ifma_refcount)); 2947 2948 if (ifma->ifma_lladdr != NULL) 2949 free(ifma->ifma_lladdr, M_IFMADDR); 2950 free(ifma->ifma_addr, M_IFMADDR); 2951 free(ifma, M_IFMADDR); 2952 } 2953 2954 /* 2955 * Register an additional multicast address with a network interface. 2956 * 2957 * - If the address is already present, bump the reference count on the 2958 * address and return. 2959 * - If the address is not link-layer, look up a link layer address. 2960 * - Allocate address structures for one or both addresses, and attach to the 2961 * multicast address list on the interface. If automatically adding a link 2962 * layer address, the protocol address will own a reference to the link 2963 * layer address, to be freed when it is freed. 2964 * - Notify the network device driver of an addition to the multicast address 2965 * list. 2966 * 2967 * 'sa' points to caller-owned memory with the desired multicast address. 2968 * 2969 * 'retifma' will be used to return a pointer to the resulting multicast 2970 * address reference, if desired. 2971 */ 2972 int 2973 if_addmulti(struct ifnet *ifp, struct sockaddr *sa, 2974 struct ifmultiaddr **retifma) 2975 { 2976 struct ifmultiaddr *ifma, *ll_ifma; 2977 struct sockaddr *llsa; 2978 struct sockaddr_dl sdl; 2979 int error; 2980 2981 /* 2982 * If the address is already present, return a new reference to it; 2983 * otherwise, allocate storage and set up a new address. 2984 */ 2985 IF_ADDR_WLOCK(ifp); 2986 ifma = if_findmulti(ifp, sa); 2987 if (ifma != NULL) { 2988 ifma->ifma_refcount++; 2989 if (retifma != NULL) 2990 *retifma = ifma; 2991 IF_ADDR_WUNLOCK(ifp); 2992 return (0); 2993 } 2994 2995 /* 2996 * The address isn't already present; resolve the protocol address 2997 * into a link layer address, and then look that up, bump its 2998 * refcount or allocate an ifma for that also. 2999 * Most link layer resolving functions returns address data which 3000 * fits inside default sockaddr_dl structure. However callback 3001 * can allocate another sockaddr structure, in that case we need to 3002 * free it later. 3003 */ 3004 llsa = NULL; 3005 ll_ifma = NULL; 3006 if (ifp->if_resolvemulti != NULL) { 3007 /* Provide called function with buffer size information */ 3008 sdl.sdl_len = sizeof(sdl); 3009 llsa = (struct sockaddr *)&sdl; 3010 error = ifp->if_resolvemulti(ifp, &llsa, sa); 3011 if (error) 3012 goto unlock_out; 3013 } 3014 3015 /* 3016 * Allocate the new address. Don't hook it up yet, as we may also 3017 * need to allocate a link layer multicast address. 3018 */ 3019 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT); 3020 if (ifma == NULL) { 3021 error = ENOMEM; 3022 goto free_llsa_out; 3023 } 3024 3025 /* 3026 * If a link layer address is found, we'll need to see if it's 3027 * already present in the address list, or allocate is as well. 3028 * When this block finishes, the link layer address will be on the 3029 * list. 3030 */ 3031 if (llsa != NULL) { 3032 ll_ifma = if_findmulti(ifp, llsa); 3033 if (ll_ifma == NULL) { 3034 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT); 3035 if (ll_ifma == NULL) { 3036 --ifma->ifma_refcount; 3037 if_freemulti(ifma); 3038 error = ENOMEM; 3039 goto free_llsa_out; 3040 } 3041 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma, 3042 ifma_link); 3043 } else 3044 ll_ifma->ifma_refcount++; 3045 ifma->ifma_llifma = ll_ifma; 3046 } 3047 3048 /* 3049 * We now have a new multicast address, ifma, and possibly a new or 3050 * referenced link layer address. Add the primary address to the 3051 * ifnet address list. 3052 */ 3053 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 3054 3055 if (retifma != NULL) 3056 *retifma = ifma; 3057 3058 /* 3059 * Must generate the message while holding the lock so that 'ifma' 3060 * pointer is still valid. 3061 */ 3062 rt_newmaddrmsg(RTM_NEWMADDR, ifma); 3063 IF_ADDR_WUNLOCK(ifp); 3064 3065 /* 3066 * We are certain we have added something, so call down to the 3067 * interface to let them know about it. 3068 */ 3069 if (ifp->if_ioctl != NULL) { 3070 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0); 3071 } 3072 3073 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl)) 3074 link_free_sdl(llsa); 3075 3076 return (0); 3077 3078 free_llsa_out: 3079 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl)) 3080 link_free_sdl(llsa); 3081 3082 unlock_out: 3083 IF_ADDR_WUNLOCK(ifp); 3084 return (error); 3085 } 3086 3087 /* 3088 * Delete a multicast group membership by network-layer group address. 3089 * 3090 * Returns ENOENT if the entry could not be found. If ifp no longer 3091 * exists, results are undefined. This entry point should only be used 3092 * from subsystems which do appropriate locking to hold ifp for the 3093 * duration of the call. 3094 * Network-layer protocol domains must use if_delmulti_ifma(). 3095 */ 3096 int 3097 if_delmulti(struct ifnet *ifp, struct sockaddr *sa) 3098 { 3099 struct ifmultiaddr *ifma; 3100 int lastref; 3101 #ifdef INVARIANTS 3102 struct ifnet *oifp; 3103 3104 IFNET_RLOCK_NOSLEEP(); 3105 TAILQ_FOREACH(oifp, &V_ifnet, if_link) 3106 if (ifp == oifp) 3107 break; 3108 if (ifp != oifp) 3109 ifp = NULL; 3110 IFNET_RUNLOCK_NOSLEEP(); 3111 3112 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__)); 3113 #endif 3114 if (ifp == NULL) 3115 return (ENOENT); 3116 3117 IF_ADDR_WLOCK(ifp); 3118 lastref = 0; 3119 ifma = if_findmulti(ifp, sa); 3120 if (ifma != NULL) 3121 lastref = if_delmulti_locked(ifp, ifma, 0); 3122 IF_ADDR_WUNLOCK(ifp); 3123 3124 if (ifma == NULL) 3125 return (ENOENT); 3126 3127 if (lastref && ifp->if_ioctl != NULL) { 3128 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 3129 } 3130 3131 return (0); 3132 } 3133 3134 /* 3135 * Delete all multicast group membership for an interface. 3136 * Should be used to quickly flush all multicast filters. 3137 */ 3138 void 3139 if_delallmulti(struct ifnet *ifp) 3140 { 3141 struct ifmultiaddr *ifma; 3142 struct ifmultiaddr *next; 3143 3144 IF_ADDR_WLOCK(ifp); 3145 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) 3146 if_delmulti_locked(ifp, ifma, 0); 3147 IF_ADDR_WUNLOCK(ifp); 3148 } 3149 3150 /* 3151 * Delete a multicast group membership by group membership pointer. 3152 * Network-layer protocol domains must use this routine. 3153 * 3154 * It is safe to call this routine if the ifp disappeared. 3155 */ 3156 void 3157 if_delmulti_ifma(struct ifmultiaddr *ifma) 3158 { 3159 struct ifnet *ifp; 3160 int lastref; 3161 3162 ifp = ifma->ifma_ifp; 3163 #ifdef DIAGNOSTIC 3164 if (ifp == NULL) { 3165 printf("%s: ifma_ifp seems to be detached\n", __func__); 3166 } else { 3167 struct ifnet *oifp; 3168 3169 IFNET_RLOCK_NOSLEEP(); 3170 TAILQ_FOREACH(oifp, &V_ifnet, if_link) 3171 if (ifp == oifp) 3172 break; 3173 if (ifp != oifp) { 3174 printf("%s: ifnet %p disappeared\n", __func__, ifp); 3175 ifp = NULL; 3176 } 3177 IFNET_RUNLOCK_NOSLEEP(); 3178 } 3179 #endif 3180 /* 3181 * If and only if the ifnet instance exists: Acquire the address lock. 3182 */ 3183 if (ifp != NULL) 3184 IF_ADDR_WLOCK(ifp); 3185 3186 lastref = if_delmulti_locked(ifp, ifma, 0); 3187 3188 if (ifp != NULL) { 3189 /* 3190 * If and only if the ifnet instance exists: 3191 * Release the address lock. 3192 * If the group was left: update the hardware hash filter. 3193 */ 3194 IF_ADDR_WUNLOCK(ifp); 3195 if (lastref && ifp->if_ioctl != NULL) { 3196 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 3197 } 3198 } 3199 } 3200 3201 /* 3202 * Perform deletion of network-layer and/or link-layer multicast address. 3203 * 3204 * Return 0 if the reference count was decremented. 3205 * Return 1 if the final reference was released, indicating that the 3206 * hardware hash filter should be reprogrammed. 3207 */ 3208 static int 3209 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching) 3210 { 3211 struct ifmultiaddr *ll_ifma; 3212 3213 if (ifp != NULL && ifma->ifma_ifp != NULL) { 3214 KASSERT(ifma->ifma_ifp == ifp, 3215 ("%s: inconsistent ifp %p", __func__, ifp)); 3216 IF_ADDR_WLOCK_ASSERT(ifp); 3217 } 3218 3219 ifp = ifma->ifma_ifp; 3220 3221 /* 3222 * If the ifnet is detaching, null out references to ifnet, 3223 * so that upper protocol layers will notice, and not attempt 3224 * to obtain locks for an ifnet which no longer exists. The 3225 * routing socket announcement must happen before the ifnet 3226 * instance is detached from the system. 3227 */ 3228 if (detaching) { 3229 #ifdef DIAGNOSTIC 3230 printf("%s: detaching ifnet instance %p\n", __func__, ifp); 3231 #endif 3232 /* 3233 * ifp may already be nulled out if we are being reentered 3234 * to delete the ll_ifma. 3235 */ 3236 if (ifp != NULL) { 3237 rt_newmaddrmsg(RTM_DELMADDR, ifma); 3238 ifma->ifma_ifp = NULL; 3239 } 3240 } 3241 3242 if (--ifma->ifma_refcount > 0) 3243 return 0; 3244 3245 /* 3246 * If this ifma is a network-layer ifma, a link-layer ifma may 3247 * have been associated with it. Release it first if so. 3248 */ 3249 ll_ifma = ifma->ifma_llifma; 3250 if (ll_ifma != NULL) { 3251 KASSERT(ifma->ifma_lladdr != NULL, 3252 ("%s: llifma w/o lladdr", __func__)); 3253 if (detaching) 3254 ll_ifma->ifma_ifp = NULL; /* XXX */ 3255 if (--ll_ifma->ifma_refcount == 0) { 3256 if (ifp != NULL) { 3257 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, 3258 ifma_link); 3259 } 3260 if_freemulti(ll_ifma); 3261 } 3262 } 3263 3264 if (ifp != NULL) 3265 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 3266 3267 if_freemulti(ifma); 3268 3269 /* 3270 * The last reference to this instance of struct ifmultiaddr 3271 * was released; the hardware should be notified of this change. 3272 */ 3273 return 1; 3274 } 3275 3276 /* 3277 * Set the link layer address on an interface. 3278 * 3279 * At this time we only support certain types of interfaces, 3280 * and we don't allow the length of the address to change. 3281 */ 3282 int 3283 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 3284 { 3285 struct sockaddr_dl *sdl; 3286 struct ifaddr *ifa; 3287 struct ifreq ifr; 3288 3289 IF_ADDR_RLOCK(ifp); 3290 ifa = ifp->if_addr; 3291 if (ifa == NULL) { 3292 IF_ADDR_RUNLOCK(ifp); 3293 return (EINVAL); 3294 } 3295 ifa_ref(ifa); 3296 IF_ADDR_RUNLOCK(ifp); 3297 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 3298 if (sdl == NULL) { 3299 ifa_free(ifa); 3300 return (EINVAL); 3301 } 3302 if (len != sdl->sdl_alen) { /* don't allow length to change */ 3303 ifa_free(ifa); 3304 return (EINVAL); 3305 } 3306 switch (ifp->if_type) { 3307 case IFT_ETHER: 3308 case IFT_FDDI: 3309 case IFT_XETHER: 3310 case IFT_ISO88025: 3311 case IFT_L2VLAN: 3312 case IFT_BRIDGE: 3313 case IFT_ARCNET: 3314 case IFT_IEEE8023ADLAG: 3315 case IFT_IEEE80211: 3316 bcopy(lladdr, LLADDR(sdl), len); 3317 ifa_free(ifa); 3318 break; 3319 default: 3320 ifa_free(ifa); 3321 return (ENODEV); 3322 } 3323 3324 /* 3325 * If the interface is already up, we need 3326 * to re-init it in order to reprogram its 3327 * address filter. 3328 */ 3329 if ((ifp->if_flags & IFF_UP) != 0) { 3330 if (ifp->if_ioctl) { 3331 ifp->if_flags &= ~IFF_UP; 3332 ifr.ifr_flags = ifp->if_flags & 0xffff; 3333 ifr.ifr_flagshigh = ifp->if_flags >> 16; 3334 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 3335 ifp->if_flags |= IFF_UP; 3336 ifr.ifr_flags = ifp->if_flags & 0xffff; 3337 ifr.ifr_flagshigh = ifp->if_flags >> 16; 3338 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 3339 } 3340 #ifdef INET 3341 /* 3342 * Also send gratuitous ARPs to notify other nodes about 3343 * the address change. 3344 */ 3345 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 3346 if (ifa->ifa_addr->sa_family == AF_INET) 3347 arp_ifinit(ifp, ifa); 3348 } 3349 #endif 3350 } 3351 return (0); 3352 } 3353 3354 /* 3355 * The name argument must be a pointer to storage which will last as 3356 * long as the interface does. For physical devices, the result of 3357 * device_get_name(dev) is a good choice and for pseudo-devices a 3358 * static string works well. 3359 */ 3360 void 3361 if_initname(struct ifnet *ifp, const char *name, int unit) 3362 { 3363 ifp->if_dname = name; 3364 ifp->if_dunit = unit; 3365 if (unit != IF_DUNIT_NONE) 3366 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit); 3367 else 3368 strlcpy(ifp->if_xname, name, IFNAMSIZ); 3369 } 3370 3371 int 3372 if_printf(struct ifnet *ifp, const char * fmt, ...) 3373 { 3374 va_list ap; 3375 int retval; 3376 3377 retval = printf("%s: ", ifp->if_xname); 3378 va_start(ap, fmt); 3379 retval += vprintf(fmt, ap); 3380 va_end(ap); 3381 return (retval); 3382 } 3383 3384 void 3385 if_start(struct ifnet *ifp) 3386 { 3387 3388 (*(ifp)->if_start)(ifp); 3389 } 3390 3391 /* 3392 * Backwards compatibility interface for drivers 3393 * that have not implemented it 3394 */ 3395 static int 3396 if_transmit(struct ifnet *ifp, struct mbuf *m) 3397 { 3398 int error; 3399 3400 IFQ_HANDOFF(ifp, m, error); 3401 return (error); 3402 } 3403 3404 int 3405 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust) 3406 { 3407 int active = 0; 3408 3409 IF_LOCK(ifq); 3410 if (_IF_QFULL(ifq)) { 3411 _IF_DROP(ifq); 3412 IF_UNLOCK(ifq); 3413 m_freem(m); 3414 return (0); 3415 } 3416 if (ifp != NULL) { 3417 ifp->if_obytes += m->m_pkthdr.len + adjust; 3418 if (m->m_flags & (M_BCAST|M_MCAST)) 3419 ifp->if_omcasts++; 3420 active = ifp->if_drv_flags & IFF_DRV_OACTIVE; 3421 } 3422 _IF_ENQUEUE(ifq, m); 3423 IF_UNLOCK(ifq); 3424 if (ifp != NULL && !active) 3425 (*(ifp)->if_start)(ifp); 3426 return (1); 3427 } 3428 3429 void 3430 if_register_com_alloc(u_char type, 3431 if_com_alloc_t *a, if_com_free_t *f) 3432 { 3433 3434 KASSERT(if_com_alloc[type] == NULL, 3435 ("if_register_com_alloc: %d already registered", type)); 3436 KASSERT(if_com_free[type] == NULL, 3437 ("if_register_com_alloc: %d free already registered", type)); 3438 3439 if_com_alloc[type] = a; 3440 if_com_free[type] = f; 3441 } 3442 3443 void 3444 if_deregister_com_alloc(u_char type) 3445 { 3446 3447 KASSERT(if_com_alloc[type] != NULL, 3448 ("if_deregister_com_alloc: %d not registered", type)); 3449 KASSERT(if_com_free[type] != NULL, 3450 ("if_deregister_com_alloc: %d free not registered", type)); 3451 if_com_alloc[type] = NULL; 3452 if_com_free[type] = NULL; 3453 } 3454 3455 /* API for driver access to network stack owned ifnet.*/ 3456 uint64_t 3457 if_setbaudrate(void *arg, uint64_t baudrate) 3458 { 3459 struct ifnet *ifp = arg; 3460 uint64_t oldbrate; 3461 3462 oldbrate = ifp->if_baudrate; 3463 ifp->if_baudrate = baudrate; 3464 return (oldbrate); 3465 } 3466 3467 uint64_t 3468 if_getbaudrate(if_t ifp) 3469 { 3470 3471 return (((struct ifnet *)ifp)->if_baudrate); 3472 } 3473 3474 int 3475 if_setcapabilities(if_t ifp, int capabilities) 3476 { 3477 ((struct ifnet *)ifp)->if_capabilities = capabilities; 3478 return (0); 3479 } 3480 3481 int 3482 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit) 3483 { 3484 ((struct ifnet *)ifp)->if_capabilities |= setbit; 3485 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit; 3486 3487 return (0); 3488 } 3489 3490 int 3491 if_getcapabilities(if_t ifp) 3492 { 3493 return ((struct ifnet *)ifp)->if_capabilities; 3494 } 3495 3496 int 3497 if_setcapenable(if_t ifp, int capabilities) 3498 { 3499 ((struct ifnet *)ifp)->if_capenable = capabilities; 3500 return (0); 3501 } 3502 3503 int 3504 if_setcapenablebit(if_t ifp, int setcap, int clearcap) 3505 { 3506 if(setcap) 3507 ((struct ifnet *)ifp)->if_capenable |= setcap; 3508 if(clearcap) 3509 ((struct ifnet *)ifp)->if_capenable &= ~clearcap; 3510 3511 return (0); 3512 } 3513 3514 const char * 3515 if_getdname(if_t ifp) 3516 { 3517 return ((struct ifnet *)ifp)->if_dname; 3518 } 3519 3520 int 3521 if_togglecapenable(if_t ifp, int togglecap) 3522 { 3523 ((struct ifnet *)ifp)->if_capenable ^= togglecap; 3524 return (0); 3525 } 3526 3527 int 3528 if_getcapenable(if_t ifp) 3529 { 3530 return ((struct ifnet *)ifp)->if_capenable; 3531 } 3532 3533 /* 3534 * This is largely undesirable because it ties ifnet to a device, but does 3535 * provide flexiblity for an embedded product vendor. Should be used with 3536 * the understanding that it violates the interface boundaries, and should be 3537 * a last resort only. 3538 */ 3539 int 3540 if_setdev(if_t ifp, void *dev) 3541 { 3542 return (0); 3543 } 3544 3545 int 3546 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags) 3547 { 3548 ((struct ifnet *)ifp)->if_drv_flags |= set_flags; 3549 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags; 3550 3551 return (0); 3552 } 3553 3554 int 3555 if_getdrvflags(if_t ifp) 3556 { 3557 return ((struct ifnet *)ifp)->if_drv_flags; 3558 } 3559 3560 int 3561 if_setdrvflags(if_t ifp, int flags) 3562 { 3563 ((struct ifnet *)ifp)->if_drv_flags = flags; 3564 return (0); 3565 } 3566 3567 3568 int 3569 if_setflags(if_t ifp, int flags) 3570 { 3571 ((struct ifnet *)ifp)->if_flags = flags; 3572 return (0); 3573 } 3574 3575 int 3576 if_setflagbits(if_t ifp, int set, int clear) 3577 { 3578 ((struct ifnet *)ifp)->if_flags |= set; 3579 ((struct ifnet *)ifp)->if_flags &= ~clear; 3580 3581 return (0); 3582 } 3583 3584 int 3585 if_getflags(if_t ifp) 3586 { 3587 return ((struct ifnet *)ifp)->if_flags; 3588 } 3589 3590 int 3591 if_clearhwassist(if_t ifp) 3592 { 3593 ((struct ifnet *)ifp)->if_hwassist = 0; 3594 return (0); 3595 } 3596 3597 int 3598 if_sethwassistbits(if_t ifp, int toset, int toclear) 3599 { 3600 ((struct ifnet *)ifp)->if_hwassist |= toset; 3601 ((struct ifnet *)ifp)->if_hwassist &= ~toclear; 3602 3603 return (0); 3604 } 3605 3606 int 3607 if_sethwassist(if_t ifp, int hwassist_bit) 3608 { 3609 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit; 3610 return (0); 3611 } 3612 3613 int 3614 if_gethwassist(if_t ifp) 3615 { 3616 return ((struct ifnet *)ifp)->if_hwassist; 3617 } 3618 3619 int 3620 if_setmtu(if_t ifp, int mtu) 3621 { 3622 ((struct ifnet *)ifp)->if_mtu = mtu; 3623 return (0); 3624 } 3625 3626 int 3627 if_getmtu(if_t ifp) 3628 { 3629 return ((struct ifnet *)ifp)->if_mtu; 3630 } 3631 3632 int 3633 if_setsoftc(if_t ifp, void *softc) 3634 { 3635 ((struct ifnet *)ifp)->if_softc = softc; 3636 return (0); 3637 } 3638 3639 void * 3640 if_getsoftc(if_t ifp) 3641 { 3642 return ((struct ifnet *)ifp)->if_softc; 3643 } 3644 3645 void 3646 if_setrcvif(struct mbuf *m, if_t ifp) 3647 { 3648 m->m_pkthdr.rcvif = (struct ifnet *)ifp; 3649 } 3650 3651 void 3652 if_setvtag(struct mbuf *m, uint16_t tag) 3653 { 3654 m->m_pkthdr.ether_vtag = tag; 3655 } 3656 3657 uint16_t 3658 if_getvtag(struct mbuf *m) 3659 { 3660 3661 return (m->m_pkthdr.ether_vtag); 3662 } 3663 3664 /* Statistics */ 3665 int 3666 if_incipackets(if_t ifp, int pkts) 3667 { 3668 ((struct ifnet *)ifp)->if_ipackets += pkts; 3669 return (0); 3670 } 3671 3672 int 3673 if_incopackets(if_t ifp, int pkts) 3674 { 3675 ((struct ifnet *)ifp)->if_opackets += pkts; 3676 return (0); 3677 } 3678 3679 int 3680 if_incierrors(if_t ifp, int ierrors) 3681 { 3682 ((struct ifnet *)ifp)->if_ierrors += ierrors; 3683 return (0); 3684 } 3685 3686 3687 int 3688 if_setierrors(if_t ifp, int ierrors) 3689 { 3690 ((struct ifnet *)ifp)->if_ierrors = ierrors; 3691 return (0); 3692 } 3693 3694 int 3695 if_setoerrors(if_t ifp, int oerrors) 3696 { 3697 ((struct ifnet *)ifp)->if_oerrors = oerrors; 3698 return (0); 3699 } 3700 3701 int if_incoerrors(if_t ifp, int oerrors) 3702 { 3703 ((struct ifnet *)ifp)->if_oerrors += oerrors; 3704 return (0); 3705 } 3706 3707 int if_inciqdrops(if_t ifp, int val) 3708 { 3709 ((struct ifnet *)ifp)->if_iqdrops += val; 3710 return (0); 3711 } 3712 3713 int 3714 if_setcollisions(if_t ifp, int collisions) 3715 { 3716 ((struct ifnet *)ifp)->if_collisions = collisions; 3717 return (0); 3718 } 3719 3720 int 3721 if_inccollisions(if_t ifp, int collisions) 3722 { 3723 ((struct ifnet *)ifp)->if_collisions += collisions; 3724 return (0); 3725 } 3726 3727 int 3728 if_setipackets(if_t ifp, int pkts) 3729 { 3730 ((struct ifnet *)ifp)->if_ipackets = pkts; 3731 return (0); 3732 } 3733 3734 int 3735 if_setopackets(if_t ifp, int pkts) 3736 { 3737 ((struct ifnet *)ifp)->if_opackets = pkts; 3738 return (0); 3739 } 3740 3741 int 3742 if_incobytes(if_t ifp, int bytes) 3743 { 3744 ((struct ifnet *)ifp)->if_obytes += bytes; 3745 return (0); 3746 } 3747 3748 int 3749 if_setibytes(if_t ifp, int bytes) 3750 { 3751 ((struct ifnet *)ifp)->if_ibytes = bytes; 3752 return (0); 3753 } 3754 3755 int 3756 if_setobytes(if_t ifp, int bytes) 3757 { 3758 ((struct ifnet *)ifp)->if_obytes = bytes; 3759 return (0); 3760 } 3761 3762 3763 int 3764 if_sendq_empty(if_t ifp) 3765 { 3766 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd); 3767 } 3768 3769 int if_getiqdrops(if_t ifp) 3770 { 3771 return ((struct ifnet *)ifp)->if_iqdrops; 3772 } 3773 3774 int 3775 if_incimcasts(if_t ifp, int mcast) 3776 { 3777 ((struct ifnet *)ifp)->if_imcasts += mcast; 3778 return (0); 3779 } 3780 3781 3782 int 3783 if_incomcasts(if_t ifp, int mcast) 3784 { 3785 ((struct ifnet *)ifp)->if_omcasts += mcast; 3786 return (0); 3787 } 3788 3789 int 3790 if_setimcasts(if_t ifp, int mcast) 3791 { 3792 ((struct ifnet *)ifp)->if_imcasts = mcast; 3793 return (0); 3794 } 3795 3796 3797 struct ifaddr * 3798 if_getifaddr(if_t ifp) 3799 { 3800 return ((struct ifnet *)ifp)->if_addr; 3801 } 3802 3803 int 3804 if_getamcount(if_t ifp) 3805 { 3806 return ((struct ifnet *)ifp)->if_amcount; 3807 } 3808 3809 3810 int 3811 if_setsendqready(if_t ifp) 3812 { 3813 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd); 3814 return (0); 3815 } 3816 3817 int 3818 if_setsendqlen(if_t ifp, int tx_desc_count) 3819 { 3820 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count); 3821 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count; 3822 3823 return (0); 3824 } 3825 3826 int 3827 if_vlantrunkinuse(if_t ifp) 3828 { 3829 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0; 3830 } 3831 3832 int 3833 if_input(if_t ifp, struct mbuf* sendmp) 3834 { 3835 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp); 3836 return (0); 3837 3838 } 3839 3840 /* XXX */ 3841 #ifndef ETH_ADDR_LEN 3842 #define ETH_ADDR_LEN 6 3843 #endif 3844 3845 int 3846 if_setupmultiaddr(if_t ifp, void *mta, int *cnt, int max) 3847 { 3848 struct ifmultiaddr *ifma; 3849 uint8_t *lmta = (uint8_t *)mta; 3850 int mcnt = 0; 3851 3852 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) { 3853 if (ifma->ifma_addr->sa_family != AF_LINK) 3854 continue; 3855 3856 if (mcnt == max) 3857 break; 3858 3859 bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 3860 &lmta[mcnt * ETH_ADDR_LEN], ETH_ADDR_LEN); 3861 mcnt++; 3862 } 3863 *cnt = mcnt; 3864 3865 return (0); 3866 } 3867 3868 int 3869 if_multiaddr_array(if_t ifp, void *mta, int *cnt, int max) 3870 { 3871 int error; 3872 3873 if_maddr_rlock(ifp); 3874 error = if_setupmultiaddr(ifp, mta, cnt, max); 3875 if_maddr_runlock(ifp); 3876 return (error); 3877 } 3878 3879 int 3880 if_multiaddr_count(if_t ifp, int max) 3881 { 3882 struct ifmultiaddr *ifma; 3883 int count; 3884 3885 count = 0; 3886 if_maddr_rlock(ifp); 3887 TAILQ_FOREACH(ifma, &((struct ifnet *)ifp)->if_multiaddrs, ifma_link) { 3888 if (ifma->ifma_addr->sa_family != AF_LINK) 3889 continue; 3890 count++; 3891 if (count == max) 3892 break; 3893 } 3894 if_maddr_runlock(ifp); 3895 return (count); 3896 } 3897 3898 struct mbuf * 3899 if_dequeue(if_t ifp) 3900 { 3901 struct mbuf *m; 3902 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m); 3903 3904 return (m); 3905 } 3906 3907 int 3908 if_sendq_prepend(if_t ifp, struct mbuf *m) 3909 { 3910 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m); 3911 return (0); 3912 } 3913 3914 int 3915 if_setifheaderlen(if_t ifp, int len) 3916 { 3917 ((struct ifnet *)ifp)->if_data.ifi_hdrlen = len; 3918 return (0); 3919 } 3920 3921 caddr_t 3922 if_getlladdr(if_t ifp) 3923 { 3924 return (IF_LLADDR((struct ifnet *)ifp)); 3925 } 3926 3927 void * 3928 if_gethandle(u_char type) 3929 { 3930 return (if_alloc(type)); 3931 } 3932 3933 void 3934 if_bpfmtap(if_t ifh, struct mbuf *m) 3935 { 3936 struct ifnet *ifp = (struct ifnet *)ifh; 3937 3938 BPF_MTAP(ifp, m); 3939 } 3940 3941 void 3942 if_etherbpfmtap(if_t ifh, struct mbuf *m) 3943 { 3944 struct ifnet *ifp = (struct ifnet *)ifh; 3945 3946 ETHER_BPF_MTAP(ifp, m); 3947 } 3948 3949 void 3950 if_vlancap(if_t ifh) 3951 { 3952 struct ifnet *ifp = (struct ifnet *)ifh; 3953 VLAN_CAPABILITIES(ifp); 3954 } 3955 3956 void 3957 if_setinitfn(if_t ifp, void (*init_fn)(void *)) 3958 { 3959 ((struct ifnet *)ifp)->if_init = init_fn; 3960 } 3961 3962 void 3963 if_setioctlfn(if_t ifp, int (*ioctl_fn)(void *, u_long, caddr_t)) 3964 { 3965 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn; 3966 } 3967 3968 void 3969 if_setstartfn(if_t ifp, void (*start_fn)(void *)) 3970 { 3971 ((struct ifnet *)ifp)->if_start = (void *)start_fn; 3972 } 3973 3974 void 3975 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn) 3976 { 3977 ((struct ifnet *)ifp)->if_transmit = start_fn; 3978 } 3979 3980 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn) 3981 { 3982 ((struct ifnet *)ifp)->if_qflush = flush_fn; 3983 3984 } 3985 3986 /* These wrappers are hopefully temporary, till all drivers use drvapi */ 3987 #ifdef INET 3988 void 3989 arp_ifinit_drv(if_t ifh, struct ifaddr *ifa) 3990 { 3991 arp_ifinit((struct ifnet *)ifh, ifa); 3992 } 3993 #endif 3994 3995 void 3996 ether_ifattach_drv(if_t ifh, const u_int8_t *lla) 3997 { 3998 ether_ifattach((struct ifnet *)ifh, lla); 3999 } 4000 4001 void 4002 ether_ifdetach_drv(if_t ifh) 4003 { 4004 ether_ifdetach((struct ifnet *)ifh); 4005 } 4006 4007 int 4008 ether_ioctl_drv(if_t ifh, u_long cmd, caddr_t data) 4009 { 4010 struct ifnet *ifp = (struct ifnet *)ifh; 4011 4012 return (ether_ioctl(ifp, cmd, data)); 4013 } 4014 4015 int 4016 ifmedia_ioctl_drv(if_t ifh, struct ifreq *ifr, struct ifmedia *ifm, 4017 u_long cmd) 4018 { 4019 struct ifnet *ifp = (struct ifnet *)ifh; 4020 4021 return (ifmedia_ioctl(ifp, ifr, ifm, cmd)); 4022 } 4023 4024 void 4025 if_free_drv(if_t ifh) 4026 { 4027 if_free((struct ifnet *)ifh); 4028 } 4029 4030 void 4031 if_initname_drv(if_t ifh, const char *name, int unit) 4032 { 4033 if_initname((struct ifnet *)ifh, name, unit); 4034 } 4035 4036 void 4037 if_linkstate_change_drv(if_t ifh, int link_state) 4038 { 4039 if_link_state_change((struct ifnet *)ifh, link_state); 4040 } 4041 4042 void 4043 ifmedia_init_drv(struct ifmedia *ifm, int ncmask, int (*chg_cb)(void *), 4044 void (*sts_cb)(void *, struct ifmediareq *)) 4045 { 4046 ifmedia_init(ifm, ncmask, (ifm_change_cb_t)chg_cb, 4047 (ifm_stat_cb_t)sts_cb); 4048 } 4049 4050 void 4051 if_addr_rlock_drv(if_t ifh) 4052 { 4053 4054 if_addr_runlock((struct ifnet *)ifh); 4055 } 4056 4057 void 4058 if_addr_runlock_drv(if_t ifh) 4059 { 4060 if_addr_runlock((struct ifnet *)ifh); 4061 } 4062 4063 void 4064 if_qflush_drv(if_t ifh) 4065 { 4066 if_qflush((struct ifnet *)ifh); 4067 4068 } 4069 4070 /* Revisit these - These are inline functions originally. */ 4071 int 4072 drbr_inuse_drv(if_t ifh, struct buf_ring *br) 4073 { 4074 return drbr_inuse_drv(ifh, br); 4075 } 4076 4077 struct mbuf* 4078 drbr_dequeue_drv(if_t ifh, struct buf_ring *br) 4079 { 4080 return drbr_dequeue(ifh, br); 4081 } 4082 4083 int 4084 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br) 4085 { 4086 return drbr_needs_enqueue(ifh, br); 4087 } 4088 4089 int 4090 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m) 4091 { 4092 return drbr_enqueue(ifh, br, m); 4093 4094 } 4095