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