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