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