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