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