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 /* Disallow MTU changes on bridge member interfaces. */ 2750 if (ifp->if_bridge) 2751 return (EOPNOTSUPP); 2752 error = (*ifp->if_ioctl)(ifp, cmd, data); 2753 if (error == 0) { 2754 getmicrotime(&ifp->if_lastchange); 2755 rt_ifmsg(ifp); 2756 #ifdef INET 2757 DEBUGNET_NOTIFY_MTU(ifp); 2758 #endif 2759 } 2760 /* 2761 * If the link MTU changed, do network layer specific procedure. 2762 */ 2763 if (ifp->if_mtu != oldmtu) { 2764 #ifdef INET6 2765 nd6_setmtu(ifp); 2766 #endif 2767 rt_updatemtu(ifp); 2768 } 2769 break; 2770 } 2771 2772 case SIOCADDMULTI: 2773 case SIOCDELMULTI: 2774 if (cmd == SIOCADDMULTI) 2775 error = priv_check(td, PRIV_NET_ADDMULTI); 2776 else 2777 error = priv_check(td, PRIV_NET_DELMULTI); 2778 if (error) 2779 return (error); 2780 2781 /* Don't allow group membership on non-multicast interfaces. */ 2782 if ((ifp->if_flags & IFF_MULTICAST) == 0) 2783 return (EOPNOTSUPP); 2784 2785 /* Don't let users screw up protocols' entries. */ 2786 if (ifr->ifr_addr.sa_family != AF_LINK) 2787 return (EINVAL); 2788 2789 if (cmd == SIOCADDMULTI) { 2790 struct epoch_tracker et; 2791 struct ifmultiaddr *ifma; 2792 2793 /* 2794 * Userland is only permitted to join groups once 2795 * via the if_addmulti() KPI, because it cannot hold 2796 * struct ifmultiaddr * between calls. It may also 2797 * lose a race while we check if the membership 2798 * already exists. 2799 */ 2800 NET_EPOCH_ENTER(et); 2801 ifma = if_findmulti(ifp, &ifr->ifr_addr); 2802 NET_EPOCH_EXIT(et); 2803 if (ifma != NULL) 2804 error = EADDRINUSE; 2805 else 2806 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 2807 } else { 2808 error = if_delmulti(ifp, &ifr->ifr_addr); 2809 } 2810 if (error == 0) 2811 getmicrotime(&ifp->if_lastchange); 2812 break; 2813 2814 case SIOCSIFPHYADDR: 2815 case SIOCDIFPHYADDR: 2816 #ifdef INET6 2817 case SIOCSIFPHYADDR_IN6: 2818 #endif 2819 case SIOCSIFMEDIA: 2820 case SIOCSIFGENERIC: 2821 error = priv_check(td, PRIV_NET_HWIOCTL); 2822 if (error) 2823 return (error); 2824 if (ifp->if_ioctl == NULL) 2825 return (EOPNOTSUPP); 2826 error = (*ifp->if_ioctl)(ifp, cmd, data); 2827 if (error == 0) 2828 getmicrotime(&ifp->if_lastchange); 2829 break; 2830 2831 case SIOCGIFSTATUS: 2832 case SIOCGIFPSRCADDR: 2833 case SIOCGIFPDSTADDR: 2834 case SIOCGIFMEDIA: 2835 case SIOCGIFXMEDIA: 2836 case SIOCGIFGENERIC: 2837 case SIOCGIFRSSKEY: 2838 case SIOCGIFRSSHASH: 2839 case SIOCGIFDOWNREASON: 2840 if (ifp->if_ioctl == NULL) 2841 return (EOPNOTSUPP); 2842 error = (*ifp->if_ioctl)(ifp, cmd, data); 2843 break; 2844 2845 case SIOCSIFLLADDR: 2846 error = priv_check(td, PRIV_NET_SETLLADDR); 2847 if (error) 2848 return (error); 2849 error = if_setlladdr(ifp, 2850 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); 2851 break; 2852 2853 case SIOCGHWADDR: 2854 error = if_gethwaddr(ifp, ifr); 2855 break; 2856 2857 case SIOCAIFGROUP: 2858 error = priv_check(td, PRIV_NET_ADDIFGROUP); 2859 if (error) 2860 return (error); 2861 error = if_addgroup(ifp, 2862 ((struct ifgroupreq *)data)->ifgr_group); 2863 if (error != 0) 2864 return (error); 2865 break; 2866 2867 case SIOCGIFGROUP: 2868 { 2869 struct epoch_tracker et; 2870 2871 NET_EPOCH_ENTER(et); 2872 error = if_getgroup((struct ifgroupreq *)data, ifp); 2873 NET_EPOCH_EXIT(et); 2874 break; 2875 } 2876 2877 case SIOCDIFGROUP: 2878 error = priv_check(td, PRIV_NET_DELIFGROUP); 2879 if (error) 2880 return (error); 2881 error = if_delgroup(ifp, 2882 ((struct ifgroupreq *)data)->ifgr_group); 2883 if (error != 0) 2884 return (error); 2885 break; 2886 2887 default: 2888 error = ENOIOCTL; 2889 break; 2890 } 2891 return (error); 2892 } 2893 2894 /* 2895 * Interface ioctls. 2896 */ 2897 int 2898 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td) 2899 { 2900 #ifdef COMPAT_FREEBSD32 2901 union { 2902 struct ifconf ifc; 2903 struct ifdrv ifd; 2904 struct ifgroupreq ifgr; 2905 struct ifmediareq ifmr; 2906 } thunk; 2907 u_long saved_cmd; 2908 struct ifconf32 *ifc32; 2909 struct ifdrv32 *ifd32; 2910 struct ifgroupreq32 *ifgr32; 2911 struct ifmediareq32 *ifmr32; 2912 #endif 2913 struct ifnet *ifp; 2914 struct ifreq *ifr; 2915 int error; 2916 int oif_flags; 2917 #ifdef VIMAGE 2918 bool shutdown; 2919 #endif 2920 2921 CURVNET_SET(so->so_vnet); 2922 #ifdef VIMAGE 2923 /* Make sure the VNET is stable. */ 2924 shutdown = VNET_IS_SHUTTING_DOWN(so->so_vnet); 2925 if (shutdown) { 2926 CURVNET_RESTORE(); 2927 return (EBUSY); 2928 } 2929 #endif 2930 2931 #ifdef COMPAT_FREEBSD32 2932 saved_cmd = cmd; 2933 switch (cmd) { 2934 case SIOCGIFCONF32: 2935 ifc32 = (struct ifconf32 *)data; 2936 thunk.ifc.ifc_len = ifc32->ifc_len; 2937 thunk.ifc.ifc_buf = PTRIN(ifc32->ifc_buf); 2938 data = (caddr_t)&thunk.ifc; 2939 cmd = SIOCGIFCONF; 2940 break; 2941 case SIOCGDRVSPEC32: 2942 case SIOCSDRVSPEC32: 2943 ifd32 = (struct ifdrv32 *)data; 2944 memcpy(thunk.ifd.ifd_name, ifd32->ifd_name, 2945 sizeof(thunk.ifd.ifd_name)); 2946 thunk.ifd.ifd_cmd = ifd32->ifd_cmd; 2947 thunk.ifd.ifd_len = ifd32->ifd_len; 2948 thunk.ifd.ifd_data = PTRIN(ifd32->ifd_data); 2949 data = (caddr_t)&thunk.ifd; 2950 cmd = _IOC_NEWTYPE(cmd, struct ifdrv); 2951 break; 2952 case SIOCAIFGROUP32: 2953 case SIOCGIFGROUP32: 2954 case SIOCDIFGROUP32: 2955 case SIOCGIFGMEMB32: 2956 ifgr32 = (struct ifgroupreq32 *)data; 2957 memcpy(thunk.ifgr.ifgr_name, ifgr32->ifgr_name, 2958 sizeof(thunk.ifgr.ifgr_name)); 2959 thunk.ifgr.ifgr_len = ifgr32->ifgr_len; 2960 switch (cmd) { 2961 case SIOCAIFGROUP32: 2962 case SIOCDIFGROUP32: 2963 memcpy(thunk.ifgr.ifgr_group, ifgr32->ifgr_group, 2964 sizeof(thunk.ifgr.ifgr_group)); 2965 break; 2966 case SIOCGIFGROUP32: 2967 case SIOCGIFGMEMB32: 2968 thunk.ifgr.ifgr_groups = PTRIN(ifgr32->ifgr_groups); 2969 break; 2970 } 2971 data = (caddr_t)&thunk.ifgr; 2972 cmd = _IOC_NEWTYPE(cmd, struct ifgroupreq); 2973 break; 2974 case SIOCGIFMEDIA32: 2975 case SIOCGIFXMEDIA32: 2976 ifmr32 = (struct ifmediareq32 *)data; 2977 memcpy(thunk.ifmr.ifm_name, ifmr32->ifm_name, 2978 sizeof(thunk.ifmr.ifm_name)); 2979 thunk.ifmr.ifm_current = ifmr32->ifm_current; 2980 thunk.ifmr.ifm_mask = ifmr32->ifm_mask; 2981 thunk.ifmr.ifm_status = ifmr32->ifm_status; 2982 thunk.ifmr.ifm_active = ifmr32->ifm_active; 2983 thunk.ifmr.ifm_count = ifmr32->ifm_count; 2984 thunk.ifmr.ifm_ulist = PTRIN(ifmr32->ifm_ulist); 2985 data = (caddr_t)&thunk.ifmr; 2986 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq); 2987 break; 2988 } 2989 #endif 2990 2991 switch (cmd) { 2992 case SIOCGIFCONF: 2993 error = ifconf(cmd, data); 2994 goto out_noref; 2995 } 2996 2997 ifr = (struct ifreq *)data; 2998 switch (cmd) { 2999 #ifdef VIMAGE 3000 case SIOCSIFRVNET: 3001 error = priv_check(td, PRIV_NET_SETIFVNET); 3002 if (error == 0) 3003 error = if_vmove_reclaim(td, ifr->ifr_name, 3004 ifr->ifr_jid); 3005 goto out_noref; 3006 #endif 3007 case SIOCIFCREATE: 3008 case SIOCIFCREATE2: 3009 error = priv_check(td, PRIV_NET_IFCREATE); 3010 if (error == 0) 3011 error = if_clone_create(ifr->ifr_name, 3012 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ? 3013 ifr_data_get_ptr(ifr) : NULL); 3014 goto out_noref; 3015 case SIOCIFDESTROY: 3016 error = priv_check(td, PRIV_NET_IFDESTROY); 3017 3018 if (error == 0) { 3019 sx_xlock(&ifnet_detach_sxlock); 3020 error = if_clone_destroy(ifr->ifr_name); 3021 sx_xunlock(&ifnet_detach_sxlock); 3022 } 3023 goto out_noref; 3024 3025 case SIOCIFGCLONERS: 3026 error = if_clone_list((struct if_clonereq *)data); 3027 goto out_noref; 3028 3029 case SIOCGIFGMEMB: 3030 error = if_getgroupmembers((struct ifgroupreq *)data); 3031 goto out_noref; 3032 3033 #if defined(INET) || defined(INET6) 3034 case SIOCSVH: 3035 case SIOCGVH: 3036 if (carp_ioctl_p == NULL) 3037 error = EPROTONOSUPPORT; 3038 else 3039 error = (*carp_ioctl_p)(ifr, cmd, td); 3040 goto out_noref; 3041 #endif 3042 } 3043 3044 ifp = ifunit_ref(ifr->ifr_name); 3045 if (ifp == NULL) { 3046 error = ENXIO; 3047 goto out_noref; 3048 } 3049 3050 error = ifhwioctl(cmd, ifp, data, td); 3051 if (error != ENOIOCTL) 3052 goto out_ref; 3053 3054 oif_flags = ifp->if_flags; 3055 if (so->so_proto == NULL) { 3056 error = EOPNOTSUPP; 3057 goto out_ref; 3058 } 3059 3060 /* 3061 * Pass the request on to the socket control method, and if the 3062 * latter returns EOPNOTSUPP, directly to the interface. 3063 * 3064 * Make an exception for the legacy SIOCSIF* requests. Drivers 3065 * trust SIOCSIFADDR et al to come from an already privileged 3066 * layer, and do not perform any credentials checks or input 3067 * validation. 3068 */ 3069 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data, 3070 ifp, td)); 3071 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL && 3072 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR && 3073 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK) 3074 error = (*ifp->if_ioctl)(ifp, cmd, data); 3075 3076 if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 3077 #ifdef INET6 3078 if (ifp->if_flags & IFF_UP) 3079 in6_if_up(ifp); 3080 #endif 3081 } 3082 3083 out_ref: 3084 if_rele(ifp); 3085 out_noref: 3086 CURVNET_RESTORE(); 3087 #ifdef COMPAT_FREEBSD32 3088 if (error != 0) 3089 return (error); 3090 switch (saved_cmd) { 3091 case SIOCGIFCONF32: 3092 ifc32->ifc_len = thunk.ifc.ifc_len; 3093 break; 3094 case SIOCGDRVSPEC32: 3095 /* 3096 * SIOCGDRVSPEC is IOWR, but nothing actually touches 3097 * the struct so just assert that ifd_len (the only 3098 * field it might make sense to update) hasn't 3099 * changed. 3100 */ 3101 KASSERT(thunk.ifd.ifd_len == ifd32->ifd_len, 3102 ("ifd_len was updated %u -> %zu", ifd32->ifd_len, 3103 thunk.ifd.ifd_len)); 3104 break; 3105 case SIOCGIFGROUP32: 3106 case SIOCGIFGMEMB32: 3107 ifgr32->ifgr_len = thunk.ifgr.ifgr_len; 3108 break; 3109 case SIOCGIFMEDIA32: 3110 case SIOCGIFXMEDIA32: 3111 ifmr32->ifm_current = thunk.ifmr.ifm_current; 3112 ifmr32->ifm_mask = thunk.ifmr.ifm_mask; 3113 ifmr32->ifm_status = thunk.ifmr.ifm_status; 3114 ifmr32->ifm_active = thunk.ifmr.ifm_active; 3115 ifmr32->ifm_count = thunk.ifmr.ifm_count; 3116 break; 3117 } 3118 #endif 3119 return (error); 3120 } 3121 3122 /* 3123 * The code common to handling reference counted flags, 3124 * e.g., in ifpromisc() and if_allmulti(). 3125 * The "pflag" argument can specify a permanent mode flag to check, 3126 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none. 3127 * 3128 * Only to be used on stack-owned flags, not driver-owned flags. 3129 */ 3130 static int 3131 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch) 3132 { 3133 struct ifreq ifr; 3134 int error; 3135 int oldflags, oldcount; 3136 3137 /* Sanity checks to catch programming errors */ 3138 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0, 3139 ("%s: setting driver-owned flag %d", __func__, flag)); 3140 3141 if (onswitch) 3142 KASSERT(*refcount >= 0, 3143 ("%s: increment negative refcount %d for flag %d", 3144 __func__, *refcount, flag)); 3145 else 3146 KASSERT(*refcount > 0, 3147 ("%s: decrement non-positive refcount %d for flag %d", 3148 __func__, *refcount, flag)); 3149 3150 /* In case this mode is permanent, just touch refcount */ 3151 if (ifp->if_flags & pflag) { 3152 *refcount += onswitch ? 1 : -1; 3153 return (0); 3154 } 3155 3156 /* Save ifnet parameters for if_ioctl() may fail */ 3157 oldcount = *refcount; 3158 oldflags = ifp->if_flags; 3159 3160 /* 3161 * See if we aren't the only and touching refcount is enough. 3162 * Actually toggle interface flag if we are the first or last. 3163 */ 3164 if (onswitch) { 3165 if ((*refcount)++) 3166 return (0); 3167 ifp->if_flags |= flag; 3168 } else { 3169 if (--(*refcount)) 3170 return (0); 3171 ifp->if_flags &= ~flag; 3172 } 3173 3174 /* Call down the driver since we've changed interface flags */ 3175 if (ifp->if_ioctl == NULL) { 3176 error = EOPNOTSUPP; 3177 goto recover; 3178 } 3179 ifr.ifr_flags = ifp->if_flags & 0xffff; 3180 ifr.ifr_flagshigh = ifp->if_flags >> 16; 3181 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 3182 if (error) 3183 goto recover; 3184 /* Notify userland that interface flags have changed */ 3185 rt_ifmsg(ifp); 3186 return (0); 3187 3188 recover: 3189 /* Recover after driver error */ 3190 *refcount = oldcount; 3191 ifp->if_flags = oldflags; 3192 return (error); 3193 } 3194 3195 /* 3196 * Set/clear promiscuous mode on interface ifp based on the truth value 3197 * of pswitch. The calls are reference counted so that only the first 3198 * "on" request actually has an effect, as does the final "off" request. 3199 * Results are undefined if the "off" and "on" requests are not matched. 3200 */ 3201 int 3202 ifpromisc(struct ifnet *ifp, int pswitch) 3203 { 3204 int error; 3205 int oldflags = ifp->if_flags; 3206 3207 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC, 3208 &ifp->if_pcount, pswitch); 3209 /* If promiscuous mode status has changed, log a message */ 3210 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) && 3211 log_promisc_mode_change) 3212 if_printf(ifp, "promiscuous mode %s\n", 3213 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); 3214 return (error); 3215 } 3216 3217 /* 3218 * Return interface configuration 3219 * of system. List may be used 3220 * in later ioctl's (above) to get 3221 * other information. 3222 */ 3223 /*ARGSUSED*/ 3224 static int 3225 ifconf(u_long cmd, caddr_t data) 3226 { 3227 struct ifconf *ifc = (struct ifconf *)data; 3228 struct ifnet *ifp; 3229 struct ifaddr *ifa; 3230 struct ifreq ifr; 3231 struct sbuf *sb; 3232 int error, full = 0, valid_len, max_len; 3233 3234 /* Limit initial buffer size to maxphys to avoid DoS from userspace. */ 3235 max_len = maxphys - 1; 3236 3237 /* Prevent hostile input from being able to crash the system */ 3238 if (ifc->ifc_len <= 0) 3239 return (EINVAL); 3240 3241 again: 3242 if (ifc->ifc_len <= max_len) { 3243 max_len = ifc->ifc_len; 3244 full = 1; 3245 } 3246 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN); 3247 max_len = 0; 3248 valid_len = 0; 3249 3250 IFNET_RLOCK(); 3251 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 3252 struct epoch_tracker et; 3253 int addrs; 3254 3255 /* 3256 * Zero the ifr to make sure we don't disclose the contents 3257 * of the stack. 3258 */ 3259 memset(&ifr, 0, sizeof(ifr)); 3260 3261 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)) 3262 >= sizeof(ifr.ifr_name)) { 3263 sbuf_delete(sb); 3264 IFNET_RUNLOCK(); 3265 return (ENAMETOOLONG); 3266 } 3267 3268 addrs = 0; 3269 NET_EPOCH_ENTER(et); 3270 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 3271 struct sockaddr *sa = ifa->ifa_addr; 3272 3273 if (prison_if(curthread->td_ucred, sa) != 0) 3274 continue; 3275 addrs++; 3276 if (sa->sa_len <= sizeof(*sa)) { 3277 if (sa->sa_len < sizeof(*sa)) { 3278 memset(&ifr.ifr_ifru.ifru_addr, 0, 3279 sizeof(ifr.ifr_ifru.ifru_addr)); 3280 memcpy(&ifr.ifr_ifru.ifru_addr, sa, 3281 sa->sa_len); 3282 } else 3283 ifr.ifr_ifru.ifru_addr = *sa; 3284 sbuf_bcat(sb, &ifr, sizeof(ifr)); 3285 max_len += sizeof(ifr); 3286 } else { 3287 sbuf_bcat(sb, &ifr, 3288 offsetof(struct ifreq, ifr_addr)); 3289 max_len += offsetof(struct ifreq, ifr_addr); 3290 sbuf_bcat(sb, sa, sa->sa_len); 3291 max_len += sa->sa_len; 3292 } 3293 3294 if (sbuf_error(sb) == 0) 3295 valid_len = sbuf_len(sb); 3296 } 3297 NET_EPOCH_EXIT(et); 3298 if (addrs == 0) { 3299 sbuf_bcat(sb, &ifr, sizeof(ifr)); 3300 max_len += sizeof(ifr); 3301 3302 if (sbuf_error(sb) == 0) 3303 valid_len = sbuf_len(sb); 3304 } 3305 } 3306 IFNET_RUNLOCK(); 3307 3308 /* 3309 * If we didn't allocate enough space (uncommon), try again. If 3310 * we have already allocated as much space as we are allowed, 3311 * return what we've got. 3312 */ 3313 if (valid_len != max_len && !full) { 3314 sbuf_delete(sb); 3315 goto again; 3316 } 3317 3318 ifc->ifc_len = valid_len; 3319 sbuf_finish(sb); 3320 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len); 3321 sbuf_delete(sb); 3322 return (error); 3323 } 3324 3325 /* 3326 * Just like ifpromisc(), but for all-multicast-reception mode. 3327 */ 3328 int 3329 if_allmulti(struct ifnet *ifp, int onswitch) 3330 { 3331 3332 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch)); 3333 } 3334 3335 struct ifmultiaddr * 3336 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa) 3337 { 3338 struct ifmultiaddr *ifma; 3339 3340 IF_ADDR_LOCK_ASSERT(ifp); 3341 3342 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 3343 if (sa->sa_family == AF_LINK) { 3344 if (sa_dl_equal(ifma->ifma_addr, sa)) 3345 break; 3346 } else { 3347 if (sa_equal(ifma->ifma_addr, sa)) 3348 break; 3349 } 3350 } 3351 3352 return ifma; 3353 } 3354 3355 /* 3356 * Allocate a new ifmultiaddr and initialize based on passed arguments. We 3357 * make copies of passed sockaddrs. The ifmultiaddr will not be added to 3358 * the ifnet multicast address list here, so the caller must do that and 3359 * other setup work (such as notifying the device driver). The reference 3360 * count is initialized to 1. 3361 */ 3362 static struct ifmultiaddr * 3363 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa, 3364 int mflags) 3365 { 3366 struct ifmultiaddr *ifma; 3367 struct sockaddr *dupsa; 3368 3369 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags | 3370 M_ZERO); 3371 if (ifma == NULL) 3372 return (NULL); 3373 3374 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags); 3375 if (dupsa == NULL) { 3376 free(ifma, M_IFMADDR); 3377 return (NULL); 3378 } 3379 bcopy(sa, dupsa, sa->sa_len); 3380 ifma->ifma_addr = dupsa; 3381 3382 ifma->ifma_ifp = ifp; 3383 ifma->ifma_refcount = 1; 3384 ifma->ifma_protospec = NULL; 3385 3386 if (llsa == NULL) { 3387 ifma->ifma_lladdr = NULL; 3388 return (ifma); 3389 } 3390 3391 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags); 3392 if (dupsa == NULL) { 3393 free(ifma->ifma_addr, M_IFMADDR); 3394 free(ifma, M_IFMADDR); 3395 return (NULL); 3396 } 3397 bcopy(llsa, dupsa, llsa->sa_len); 3398 ifma->ifma_lladdr = dupsa; 3399 3400 return (ifma); 3401 } 3402 3403 /* 3404 * if_freemulti: free ifmultiaddr structure and possibly attached related 3405 * addresses. The caller is responsible for implementing reference 3406 * counting, notifying the driver, handling routing messages, and releasing 3407 * any dependent link layer state. 3408 */ 3409 #ifdef MCAST_VERBOSE 3410 extern void kdb_backtrace(void); 3411 #endif 3412 static void 3413 if_freemulti_internal(struct ifmultiaddr *ifma) 3414 { 3415 3416 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d", 3417 ifma->ifma_refcount)); 3418 3419 if (ifma->ifma_lladdr != NULL) 3420 free(ifma->ifma_lladdr, M_IFMADDR); 3421 #ifdef MCAST_VERBOSE 3422 kdb_backtrace(); 3423 printf("%s freeing ifma: %p\n", __func__, ifma); 3424 #endif 3425 free(ifma->ifma_addr, M_IFMADDR); 3426 free(ifma, M_IFMADDR); 3427 } 3428 3429 static void 3430 if_destroymulti(epoch_context_t ctx) 3431 { 3432 struct ifmultiaddr *ifma; 3433 3434 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx); 3435 if_freemulti_internal(ifma); 3436 } 3437 3438 void 3439 if_freemulti(struct ifmultiaddr *ifma) 3440 { 3441 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d", 3442 ifma->ifma_refcount)); 3443 3444 NET_EPOCH_CALL(if_destroymulti, &ifma->ifma_epoch_ctx); 3445 } 3446 3447 /* 3448 * Register an additional multicast address with a network interface. 3449 * 3450 * - If the address is already present, bump the reference count on the 3451 * address and return. 3452 * - If the address is not link-layer, look up a link layer address. 3453 * - Allocate address structures for one or both addresses, and attach to the 3454 * multicast address list on the interface. If automatically adding a link 3455 * layer address, the protocol address will own a reference to the link 3456 * layer address, to be freed when it is freed. 3457 * - Notify the network device driver of an addition to the multicast address 3458 * list. 3459 * 3460 * 'sa' points to caller-owned memory with the desired multicast address. 3461 * 3462 * 'retifma' will be used to return a pointer to the resulting multicast 3463 * address reference, if desired. 3464 */ 3465 int 3466 if_addmulti(struct ifnet *ifp, struct sockaddr *sa, 3467 struct ifmultiaddr **retifma) 3468 { 3469 struct ifmultiaddr *ifma, *ll_ifma; 3470 struct sockaddr *llsa; 3471 struct sockaddr_dl sdl; 3472 int error; 3473 3474 #ifdef INET 3475 IN_MULTI_LIST_UNLOCK_ASSERT(); 3476 #endif 3477 #ifdef INET6 3478 IN6_MULTI_LIST_UNLOCK_ASSERT(); 3479 #endif 3480 /* 3481 * If the address is already present, return a new reference to it; 3482 * otherwise, allocate storage and set up a new address. 3483 */ 3484 IF_ADDR_WLOCK(ifp); 3485 ifma = if_findmulti(ifp, sa); 3486 if (ifma != NULL) { 3487 ifma->ifma_refcount++; 3488 if (retifma != NULL) 3489 *retifma = ifma; 3490 IF_ADDR_WUNLOCK(ifp); 3491 return (0); 3492 } 3493 3494 /* 3495 * The address isn't already present; resolve the protocol address 3496 * into a link layer address, and then look that up, bump its 3497 * refcount or allocate an ifma for that also. 3498 * Most link layer resolving functions returns address data which 3499 * fits inside default sockaddr_dl structure. However callback 3500 * can allocate another sockaddr structure, in that case we need to 3501 * free it later. 3502 */ 3503 llsa = NULL; 3504 ll_ifma = NULL; 3505 if (ifp->if_resolvemulti != NULL) { 3506 /* Provide called function with buffer size information */ 3507 sdl.sdl_len = sizeof(sdl); 3508 llsa = (struct sockaddr *)&sdl; 3509 error = ifp->if_resolvemulti(ifp, &llsa, sa); 3510 if (error) 3511 goto unlock_out; 3512 } 3513 3514 /* 3515 * Allocate the new address. Don't hook it up yet, as we may also 3516 * need to allocate a link layer multicast address. 3517 */ 3518 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT); 3519 if (ifma == NULL) { 3520 error = ENOMEM; 3521 goto free_llsa_out; 3522 } 3523 3524 /* 3525 * If a link layer address is found, we'll need to see if it's 3526 * already present in the address list, or allocate is as well. 3527 * When this block finishes, the link layer address will be on the 3528 * list. 3529 */ 3530 if (llsa != NULL) { 3531 ll_ifma = if_findmulti(ifp, llsa); 3532 if (ll_ifma == NULL) { 3533 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT); 3534 if (ll_ifma == NULL) { 3535 --ifma->ifma_refcount; 3536 if_freemulti(ifma); 3537 error = ENOMEM; 3538 goto free_llsa_out; 3539 } 3540 ll_ifma->ifma_flags |= IFMA_F_ENQUEUED; 3541 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma, 3542 ifma_link); 3543 } else 3544 ll_ifma->ifma_refcount++; 3545 ifma->ifma_llifma = ll_ifma; 3546 } 3547 3548 /* 3549 * We now have a new multicast address, ifma, and possibly a new or 3550 * referenced link layer address. Add the primary address to the 3551 * ifnet address list. 3552 */ 3553 ifma->ifma_flags |= IFMA_F_ENQUEUED; 3554 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 3555 3556 if (retifma != NULL) 3557 *retifma = ifma; 3558 3559 /* 3560 * Must generate the message while holding the lock so that 'ifma' 3561 * pointer is still valid. 3562 */ 3563 rt_newmaddrmsg(RTM_NEWMADDR, ifma); 3564 IF_ADDR_WUNLOCK(ifp); 3565 3566 /* 3567 * We are certain we have added something, so call down to the 3568 * interface to let them know about it. 3569 */ 3570 if (ifp->if_ioctl != NULL) { 3571 if (THREAD_CAN_SLEEP()) 3572 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0); 3573 else 3574 taskqueue_enqueue(taskqueue_swi, &ifp->if_addmultitask); 3575 } 3576 3577 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl)) 3578 link_free_sdl(llsa); 3579 3580 return (0); 3581 3582 free_llsa_out: 3583 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl)) 3584 link_free_sdl(llsa); 3585 3586 unlock_out: 3587 IF_ADDR_WUNLOCK(ifp); 3588 return (error); 3589 } 3590 3591 static void 3592 if_siocaddmulti(void *arg, int pending) 3593 { 3594 struct ifnet *ifp; 3595 3596 ifp = arg; 3597 #ifdef DIAGNOSTIC 3598 if (pending > 1) 3599 if_printf(ifp, "%d SIOCADDMULTI coalesced\n", pending); 3600 #endif 3601 CURVNET_SET(ifp->if_vnet); 3602 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0); 3603 CURVNET_RESTORE(); 3604 } 3605 3606 /* 3607 * Delete a multicast group membership by network-layer group address. 3608 * 3609 * Returns ENOENT if the entry could not be found. If ifp no longer 3610 * exists, results are undefined. This entry point should only be used 3611 * from subsystems which do appropriate locking to hold ifp for the 3612 * duration of the call. 3613 * Network-layer protocol domains must use if_delmulti_ifma(). 3614 */ 3615 int 3616 if_delmulti(struct ifnet *ifp, struct sockaddr *sa) 3617 { 3618 struct ifmultiaddr *ifma; 3619 int lastref; 3620 3621 KASSERT(ifp, ("%s: NULL ifp", __func__)); 3622 3623 IF_ADDR_WLOCK(ifp); 3624 lastref = 0; 3625 ifma = if_findmulti(ifp, sa); 3626 if (ifma != NULL) 3627 lastref = if_delmulti_locked(ifp, ifma, 0); 3628 IF_ADDR_WUNLOCK(ifp); 3629 3630 if (ifma == NULL) 3631 return (ENOENT); 3632 3633 if (lastref && ifp->if_ioctl != NULL) { 3634 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 3635 } 3636 3637 return (0); 3638 } 3639 3640 /* 3641 * Delete all multicast group membership for an interface. 3642 * Should be used to quickly flush all multicast filters. 3643 */ 3644 void 3645 if_delallmulti(struct ifnet *ifp) 3646 { 3647 struct ifmultiaddr *ifma; 3648 struct ifmultiaddr *next; 3649 3650 IF_ADDR_WLOCK(ifp); 3651 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) 3652 if_delmulti_locked(ifp, ifma, 0); 3653 IF_ADDR_WUNLOCK(ifp); 3654 } 3655 3656 void 3657 if_delmulti_ifma(struct ifmultiaddr *ifma) 3658 { 3659 if_delmulti_ifma_flags(ifma, 0); 3660 } 3661 3662 /* 3663 * Delete a multicast group membership by group membership pointer. 3664 * Network-layer protocol domains must use this routine. 3665 * 3666 * It is safe to call this routine if the ifp disappeared. 3667 */ 3668 void 3669 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags) 3670 { 3671 struct ifnet *ifp; 3672 int lastref; 3673 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma); 3674 #ifdef INET 3675 IN_MULTI_LIST_UNLOCK_ASSERT(); 3676 #endif 3677 ifp = ifma->ifma_ifp; 3678 #ifdef DIAGNOSTIC 3679 if (ifp == NULL) { 3680 printf("%s: ifma_ifp seems to be detached\n", __func__); 3681 } else { 3682 struct epoch_tracker et; 3683 struct ifnet *oifp; 3684 3685 NET_EPOCH_ENTER(et); 3686 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link) 3687 if (ifp == oifp) 3688 break; 3689 NET_EPOCH_EXIT(et); 3690 if (ifp != oifp) 3691 ifp = NULL; 3692 } 3693 #endif 3694 /* 3695 * If and only if the ifnet instance exists: Acquire the address lock. 3696 */ 3697 if (ifp != NULL) 3698 IF_ADDR_WLOCK(ifp); 3699 3700 lastref = if_delmulti_locked(ifp, ifma, flags); 3701 3702 if (ifp != NULL) { 3703 /* 3704 * If and only if the ifnet instance exists: 3705 * Release the address lock. 3706 * If the group was left: update the hardware hash filter. 3707 */ 3708 IF_ADDR_WUNLOCK(ifp); 3709 if (lastref && ifp->if_ioctl != NULL) { 3710 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 3711 } 3712 } 3713 } 3714 3715 /* 3716 * Perform deletion of network-layer and/or link-layer multicast address. 3717 * 3718 * Return 0 if the reference count was decremented. 3719 * Return 1 if the final reference was released, indicating that the 3720 * hardware hash filter should be reprogrammed. 3721 */ 3722 static int 3723 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching) 3724 { 3725 struct ifmultiaddr *ll_ifma; 3726 3727 if (ifp != NULL && ifma->ifma_ifp != NULL) { 3728 KASSERT(ifma->ifma_ifp == ifp, 3729 ("%s: inconsistent ifp %p", __func__, ifp)); 3730 IF_ADDR_WLOCK_ASSERT(ifp); 3731 } 3732 3733 ifp = ifma->ifma_ifp; 3734 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : ""); 3735 3736 /* 3737 * If the ifnet is detaching, null out references to ifnet, 3738 * so that upper protocol layers will notice, and not attempt 3739 * to obtain locks for an ifnet which no longer exists. The 3740 * routing socket announcement must happen before the ifnet 3741 * instance is detached from the system. 3742 */ 3743 if (detaching) { 3744 #ifdef DIAGNOSTIC 3745 printf("%s: detaching ifnet instance %p\n", __func__, ifp); 3746 #endif 3747 /* 3748 * ifp may already be nulled out if we are being reentered 3749 * to delete the ll_ifma. 3750 */ 3751 if (ifp != NULL) { 3752 rt_newmaddrmsg(RTM_DELMADDR, ifma); 3753 ifma->ifma_ifp = NULL; 3754 } 3755 } 3756 3757 if (--ifma->ifma_refcount > 0) 3758 return 0; 3759 3760 if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) { 3761 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link); 3762 ifma->ifma_flags &= ~IFMA_F_ENQUEUED; 3763 } 3764 /* 3765 * If this ifma is a network-layer ifma, a link-layer ifma may 3766 * have been associated with it. Release it first if so. 3767 */ 3768 ll_ifma = ifma->ifma_llifma; 3769 if (ll_ifma != NULL) { 3770 KASSERT(ifma->ifma_lladdr != NULL, 3771 ("%s: llifma w/o lladdr", __func__)); 3772 if (detaching) 3773 ll_ifma->ifma_ifp = NULL; /* XXX */ 3774 if (--ll_ifma->ifma_refcount == 0) { 3775 if (ifp != NULL) { 3776 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) { 3777 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr, 3778 ifma_link); 3779 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED; 3780 } 3781 } 3782 if_freemulti(ll_ifma); 3783 } 3784 } 3785 #ifdef INVARIANTS 3786 if (ifp) { 3787 struct ifmultiaddr *ifmatmp; 3788 3789 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link) 3790 MPASS(ifma != ifmatmp); 3791 } 3792 #endif 3793 if_freemulti(ifma); 3794 /* 3795 * The last reference to this instance of struct ifmultiaddr 3796 * was released; the hardware should be notified of this change. 3797 */ 3798 return 1; 3799 } 3800 3801 /* 3802 * Set the link layer address on an interface. 3803 * 3804 * At this time we only support certain types of interfaces, 3805 * and we don't allow the length of the address to change. 3806 * 3807 * Set noinline to be dtrace-friendly 3808 */ 3809 __noinline int 3810 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 3811 { 3812 struct sockaddr_dl *sdl; 3813 struct ifaddr *ifa; 3814 struct ifreq ifr; 3815 3816 ifa = ifp->if_addr; 3817 if (ifa == NULL) 3818 return (EINVAL); 3819 3820 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 3821 if (sdl == NULL) 3822 return (EINVAL); 3823 3824 if (len != sdl->sdl_alen) /* don't allow length to change */ 3825 return (EINVAL); 3826 3827 switch (ifp->if_type) { 3828 case IFT_ETHER: 3829 case IFT_XETHER: 3830 case IFT_L2VLAN: 3831 case IFT_BRIDGE: 3832 case IFT_IEEE8023ADLAG: 3833 bcopy(lladdr, LLADDR(sdl), len); 3834 break; 3835 default: 3836 return (ENODEV); 3837 } 3838 3839 /* 3840 * If the interface is already up, we need 3841 * to re-init it in order to reprogram its 3842 * address filter. 3843 */ 3844 if ((ifp->if_flags & IFF_UP) != 0) { 3845 if (ifp->if_ioctl) { 3846 ifp->if_flags &= ~IFF_UP; 3847 ifr.ifr_flags = ifp->if_flags & 0xffff; 3848 ifr.ifr_flagshigh = ifp->if_flags >> 16; 3849 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 3850 ifp->if_flags |= IFF_UP; 3851 ifr.ifr_flags = ifp->if_flags & 0xffff; 3852 ifr.ifr_flagshigh = ifp->if_flags >> 16; 3853 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 3854 } 3855 } 3856 EVENTHANDLER_INVOKE(iflladdr_event, ifp); 3857 3858 return (0); 3859 } 3860 3861 /* 3862 * Compat function for handling basic encapsulation requests. 3863 * Not converted stacks (FDDI, IB, ..) supports traditional 3864 * output model: ARP (and other similar L2 protocols) are handled 3865 * inside output routine, arpresolve/nd6_resolve() returns MAC 3866 * address instead of full prepend. 3867 * 3868 * This function creates calculated header==MAC for IPv4/IPv6 and 3869 * returns EAFNOSUPPORT (which is then handled in ARP code) for other 3870 * address families. 3871 */ 3872 static int 3873 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req) 3874 { 3875 3876 if (req->rtype != IFENCAP_LL) 3877 return (EOPNOTSUPP); 3878 3879 if (req->bufsize < req->lladdr_len) 3880 return (ENOMEM); 3881 3882 switch (req->family) { 3883 case AF_INET: 3884 case AF_INET6: 3885 break; 3886 default: 3887 return (EAFNOSUPPORT); 3888 } 3889 3890 /* Copy lladdr to storage as is */ 3891 memmove(req->buf, req->lladdr, req->lladdr_len); 3892 req->bufsize = req->lladdr_len; 3893 req->lladdr_off = 0; 3894 3895 return (0); 3896 } 3897 3898 /* 3899 * Tunnel interfaces can nest, also they may cause infinite recursion 3900 * calls when misconfigured. We'll prevent this by detecting loops. 3901 * High nesting level may cause stack exhaustion. We'll prevent this 3902 * by introducing upper limit. 3903 * 3904 * Return 0, if tunnel nesting count is equal or less than limit. 3905 */ 3906 int 3907 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie, 3908 int limit) 3909 { 3910 struct m_tag *mtag; 3911 int count; 3912 3913 count = 1; 3914 mtag = NULL; 3915 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) { 3916 if (*(struct ifnet **)(mtag + 1) == ifp) { 3917 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp)); 3918 return (EIO); 3919 } 3920 count++; 3921 } 3922 if (count > limit) { 3923 log(LOG_NOTICE, 3924 "%s: if_output recursively called too many times(%d)\n", 3925 if_name(ifp), count); 3926 return (EIO); 3927 } 3928 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT); 3929 if (mtag == NULL) 3930 return (ENOMEM); 3931 *(struct ifnet **)(mtag + 1) = ifp; 3932 m_tag_prepend(m, mtag); 3933 return (0); 3934 } 3935 3936 /* 3937 * Get the link layer address that was read from the hardware at attach. 3938 * 3939 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type 3940 * their component interfaces as IFT_IEEE8023ADLAG. 3941 */ 3942 int 3943 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr) 3944 { 3945 3946 if (ifp->if_hw_addr == NULL) 3947 return (ENODEV); 3948 3949 switch (ifp->if_type) { 3950 case IFT_ETHER: 3951 case IFT_IEEE8023ADLAG: 3952 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen); 3953 return (0); 3954 default: 3955 return (ENODEV); 3956 } 3957 } 3958 3959 /* 3960 * The name argument must be a pointer to storage which will last as 3961 * long as the interface does. For physical devices, the result of 3962 * device_get_name(dev) is a good choice and for pseudo-devices a 3963 * static string works well. 3964 */ 3965 void 3966 if_initname(struct ifnet *ifp, const char *name, int unit) 3967 { 3968 ifp->if_dname = name; 3969 ifp->if_dunit = unit; 3970 if (unit != IF_DUNIT_NONE) 3971 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit); 3972 else 3973 strlcpy(ifp->if_xname, name, IFNAMSIZ); 3974 } 3975 3976 static int 3977 if_vlog(struct ifnet *ifp, int pri, const char *fmt, va_list ap) 3978 { 3979 char if_fmt[256]; 3980 3981 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt); 3982 vlog(pri, if_fmt, ap); 3983 return (0); 3984 } 3985 3986 3987 int 3988 if_printf(struct ifnet *ifp, const char *fmt, ...) 3989 { 3990 va_list ap; 3991 3992 va_start(ap, fmt); 3993 if_vlog(ifp, LOG_INFO, fmt, ap); 3994 va_end(ap); 3995 return (0); 3996 } 3997 3998 int 3999 if_log(struct ifnet *ifp, int pri, const char *fmt, ...) 4000 { 4001 va_list ap; 4002 4003 va_start(ap, fmt); 4004 if_vlog(ifp, pri, fmt, ap); 4005 va_end(ap); 4006 return (0); 4007 } 4008 4009 void 4010 if_start(struct ifnet *ifp) 4011 { 4012 4013 (*(ifp)->if_start)(ifp); 4014 } 4015 4016 /* 4017 * Backwards compatibility interface for drivers 4018 * that have not implemented it 4019 */ 4020 static int 4021 if_transmit(struct ifnet *ifp, struct mbuf *m) 4022 { 4023 int error; 4024 4025 IFQ_HANDOFF(ifp, m, error); 4026 return (error); 4027 } 4028 4029 static void 4030 if_input_default(struct ifnet *ifp __unused, struct mbuf *m) 4031 { 4032 4033 m_freem(m); 4034 } 4035 4036 int 4037 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust) 4038 { 4039 int active = 0; 4040 4041 IF_LOCK(ifq); 4042 if (_IF_QFULL(ifq)) { 4043 IF_UNLOCK(ifq); 4044 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1); 4045 m_freem(m); 4046 return (0); 4047 } 4048 if (ifp != NULL) { 4049 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust); 4050 if (m->m_flags & (M_BCAST|M_MCAST)) 4051 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1); 4052 active = ifp->if_drv_flags & IFF_DRV_OACTIVE; 4053 } 4054 _IF_ENQUEUE(ifq, m); 4055 IF_UNLOCK(ifq); 4056 if (ifp != NULL && !active) 4057 (*(ifp)->if_start)(ifp); 4058 return (1); 4059 } 4060 4061 void 4062 if_register_com_alloc(u_char type, 4063 if_com_alloc_t *a, if_com_free_t *f) 4064 { 4065 4066 KASSERT(if_com_alloc[type] == NULL, 4067 ("if_register_com_alloc: %d already registered", type)); 4068 KASSERT(if_com_free[type] == NULL, 4069 ("if_register_com_alloc: %d free already registered", type)); 4070 4071 if_com_alloc[type] = a; 4072 if_com_free[type] = f; 4073 } 4074 4075 void 4076 if_deregister_com_alloc(u_char type) 4077 { 4078 4079 KASSERT(if_com_alloc[type] != NULL, 4080 ("if_deregister_com_alloc: %d not registered", type)); 4081 KASSERT(if_com_free[type] != NULL, 4082 ("if_deregister_com_alloc: %d free not registered", type)); 4083 4084 /* 4085 * Ensure all pending EPOCH(9) callbacks have been executed. This 4086 * fixes issues about late invocation of if_destroy(), which leads 4087 * to memory leak from if_com_alloc[type] allocated if_l2com. 4088 */ 4089 epoch_drain_callbacks(net_epoch_preempt); 4090 4091 if_com_alloc[type] = NULL; 4092 if_com_free[type] = NULL; 4093 } 4094 4095 /* API for driver access to network stack owned ifnet.*/ 4096 uint64_t 4097 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate) 4098 { 4099 uint64_t oldbrate; 4100 4101 oldbrate = ifp->if_baudrate; 4102 ifp->if_baudrate = baudrate; 4103 return (oldbrate); 4104 } 4105 4106 uint64_t 4107 if_getbaudrate(if_t ifp) 4108 { 4109 4110 return (((struct ifnet *)ifp)->if_baudrate); 4111 } 4112 4113 int 4114 if_setcapabilities(if_t ifp, int capabilities) 4115 { 4116 ((struct ifnet *)ifp)->if_capabilities = capabilities; 4117 return (0); 4118 } 4119 4120 int 4121 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit) 4122 { 4123 ((struct ifnet *)ifp)->if_capabilities |= setbit; 4124 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit; 4125 4126 return (0); 4127 } 4128 4129 int 4130 if_getcapabilities(if_t ifp) 4131 { 4132 return ((struct ifnet *)ifp)->if_capabilities; 4133 } 4134 4135 int 4136 if_setcapenable(if_t ifp, int capabilities) 4137 { 4138 ((struct ifnet *)ifp)->if_capenable = capabilities; 4139 return (0); 4140 } 4141 4142 int 4143 if_setcapenablebit(if_t ifp, int setcap, int clearcap) 4144 { 4145 if(setcap) 4146 ((struct ifnet *)ifp)->if_capenable |= setcap; 4147 if(clearcap) 4148 ((struct ifnet *)ifp)->if_capenable &= ~clearcap; 4149 4150 return (0); 4151 } 4152 4153 const char * 4154 if_getdname(if_t ifp) 4155 { 4156 return ((struct ifnet *)ifp)->if_dname; 4157 } 4158 4159 int 4160 if_togglecapenable(if_t ifp, int togglecap) 4161 { 4162 ((struct ifnet *)ifp)->if_capenable ^= togglecap; 4163 return (0); 4164 } 4165 4166 int 4167 if_getcapenable(if_t ifp) 4168 { 4169 return ((struct ifnet *)ifp)->if_capenable; 4170 } 4171 4172 /* 4173 * This is largely undesirable because it ties ifnet to a device, but does 4174 * provide flexiblity for an embedded product vendor. Should be used with 4175 * the understanding that it violates the interface boundaries, and should be 4176 * a last resort only. 4177 */ 4178 int 4179 if_setdev(if_t ifp, void *dev) 4180 { 4181 return (0); 4182 } 4183 4184 int 4185 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags) 4186 { 4187 ((struct ifnet *)ifp)->if_drv_flags |= set_flags; 4188 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags; 4189 4190 return (0); 4191 } 4192 4193 int 4194 if_getdrvflags(if_t ifp) 4195 { 4196 return ((struct ifnet *)ifp)->if_drv_flags; 4197 } 4198 4199 int 4200 if_setdrvflags(if_t ifp, int flags) 4201 { 4202 ((struct ifnet *)ifp)->if_drv_flags = flags; 4203 return (0); 4204 } 4205 4206 int 4207 if_setflags(if_t ifp, int flags) 4208 { 4209 4210 ifp->if_flags = flags; 4211 return (0); 4212 } 4213 4214 int 4215 if_setflagbits(if_t ifp, int set, int clear) 4216 { 4217 ((struct ifnet *)ifp)->if_flags |= set; 4218 ((struct ifnet *)ifp)->if_flags &= ~clear; 4219 4220 return (0); 4221 } 4222 4223 int 4224 if_getflags(if_t ifp) 4225 { 4226 return ((struct ifnet *)ifp)->if_flags; 4227 } 4228 4229 int 4230 if_clearhwassist(if_t ifp) 4231 { 4232 ((struct ifnet *)ifp)->if_hwassist = 0; 4233 return (0); 4234 } 4235 4236 int 4237 if_sethwassistbits(if_t ifp, int toset, int toclear) 4238 { 4239 ((struct ifnet *)ifp)->if_hwassist |= toset; 4240 ((struct ifnet *)ifp)->if_hwassist &= ~toclear; 4241 4242 return (0); 4243 } 4244 4245 int 4246 if_sethwassist(if_t ifp, int hwassist_bit) 4247 { 4248 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit; 4249 return (0); 4250 } 4251 4252 int 4253 if_gethwassist(if_t ifp) 4254 { 4255 return ((struct ifnet *)ifp)->if_hwassist; 4256 } 4257 4258 int 4259 if_setmtu(if_t ifp, int mtu) 4260 { 4261 ((struct ifnet *)ifp)->if_mtu = mtu; 4262 return (0); 4263 } 4264 4265 int 4266 if_getmtu(if_t ifp) 4267 { 4268 return ((struct ifnet *)ifp)->if_mtu; 4269 } 4270 4271 int 4272 if_getmtu_family(if_t ifp, int family) 4273 { 4274 struct domain *dp; 4275 4276 for (dp = domains; dp; dp = dp->dom_next) { 4277 if (dp->dom_family == family && dp->dom_ifmtu != NULL) 4278 return (dp->dom_ifmtu((struct ifnet *)ifp)); 4279 } 4280 4281 return (((struct ifnet *)ifp)->if_mtu); 4282 } 4283 4284 /* 4285 * Methods for drivers to access interface unicast and multicast 4286 * link level addresses. Driver shall not know 'struct ifaddr' neither 4287 * 'struct ifmultiaddr'. 4288 */ 4289 u_int 4290 if_lladdr_count(if_t ifp) 4291 { 4292 struct epoch_tracker et; 4293 struct ifaddr *ifa; 4294 u_int count; 4295 4296 count = 0; 4297 NET_EPOCH_ENTER(et); 4298 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 4299 if (ifa->ifa_addr->sa_family == AF_LINK) 4300 count++; 4301 NET_EPOCH_EXIT(et); 4302 4303 return (count); 4304 } 4305 4306 u_int 4307 if_foreach_lladdr(if_t ifp, iflladdr_cb_t cb, void *cb_arg) 4308 { 4309 struct epoch_tracker et; 4310 struct ifaddr *ifa; 4311 u_int count; 4312 4313 MPASS(cb); 4314 4315 count = 0; 4316 NET_EPOCH_ENTER(et); 4317 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 4318 if (ifa->ifa_addr->sa_family != AF_LINK) 4319 continue; 4320 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifa->ifa_addr, 4321 count); 4322 } 4323 NET_EPOCH_EXIT(et); 4324 4325 return (count); 4326 } 4327 4328 u_int 4329 if_llmaddr_count(if_t ifp) 4330 { 4331 struct epoch_tracker et; 4332 struct ifmultiaddr *ifma; 4333 int count; 4334 4335 count = 0; 4336 NET_EPOCH_ENTER(et); 4337 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 4338 if (ifma->ifma_addr->sa_family == AF_LINK) 4339 count++; 4340 NET_EPOCH_EXIT(et); 4341 4342 return (count); 4343 } 4344 4345 u_int 4346 if_foreach_llmaddr(if_t ifp, iflladdr_cb_t cb, void *cb_arg) 4347 { 4348 struct epoch_tracker et; 4349 struct ifmultiaddr *ifma; 4350 u_int count; 4351 4352 MPASS(cb); 4353 4354 count = 0; 4355 NET_EPOCH_ENTER(et); 4356 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 4357 if (ifma->ifma_addr->sa_family != AF_LINK) 4358 continue; 4359 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifma->ifma_addr, 4360 count); 4361 } 4362 NET_EPOCH_EXIT(et); 4363 4364 return (count); 4365 } 4366 4367 int 4368 if_setsoftc(if_t ifp, void *softc) 4369 { 4370 ((struct ifnet *)ifp)->if_softc = softc; 4371 return (0); 4372 } 4373 4374 void * 4375 if_getsoftc(if_t ifp) 4376 { 4377 return ((struct ifnet *)ifp)->if_softc; 4378 } 4379 4380 void 4381 if_setrcvif(struct mbuf *m, if_t ifp) 4382 { 4383 4384 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0); 4385 m->m_pkthdr.rcvif = (struct ifnet *)ifp; 4386 } 4387 4388 void 4389 if_setvtag(struct mbuf *m, uint16_t tag) 4390 { 4391 m->m_pkthdr.ether_vtag = tag; 4392 } 4393 4394 uint16_t 4395 if_getvtag(struct mbuf *m) 4396 { 4397 4398 return (m->m_pkthdr.ether_vtag); 4399 } 4400 4401 int 4402 if_sendq_empty(if_t ifp) 4403 { 4404 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd); 4405 } 4406 4407 struct ifaddr * 4408 if_getifaddr(if_t ifp) 4409 { 4410 return ((struct ifnet *)ifp)->if_addr; 4411 } 4412 4413 int 4414 if_getamcount(if_t ifp) 4415 { 4416 return ((struct ifnet *)ifp)->if_amcount; 4417 } 4418 4419 int 4420 if_setsendqready(if_t ifp) 4421 { 4422 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd); 4423 return (0); 4424 } 4425 4426 int 4427 if_setsendqlen(if_t ifp, int tx_desc_count) 4428 { 4429 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count); 4430 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count; 4431 4432 return (0); 4433 } 4434 4435 int 4436 if_vlantrunkinuse(if_t ifp) 4437 { 4438 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0; 4439 } 4440 4441 int 4442 if_input(if_t ifp, struct mbuf* sendmp) 4443 { 4444 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp); 4445 return (0); 4446 4447 } 4448 4449 struct mbuf * 4450 if_dequeue(if_t ifp) 4451 { 4452 struct mbuf *m; 4453 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m); 4454 4455 return (m); 4456 } 4457 4458 int 4459 if_sendq_prepend(if_t ifp, struct mbuf *m) 4460 { 4461 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m); 4462 return (0); 4463 } 4464 4465 int 4466 if_setifheaderlen(if_t ifp, int len) 4467 { 4468 ((struct ifnet *)ifp)->if_hdrlen = len; 4469 return (0); 4470 } 4471 4472 caddr_t 4473 if_getlladdr(if_t ifp) 4474 { 4475 return (IF_LLADDR((struct ifnet *)ifp)); 4476 } 4477 4478 void * 4479 if_gethandle(u_char type) 4480 { 4481 return (if_alloc(type)); 4482 } 4483 4484 void 4485 if_bpfmtap(if_t ifh, struct mbuf *m) 4486 { 4487 struct ifnet *ifp = (struct ifnet *)ifh; 4488 4489 BPF_MTAP(ifp, m); 4490 } 4491 4492 void 4493 if_etherbpfmtap(if_t ifh, struct mbuf *m) 4494 { 4495 struct ifnet *ifp = (struct ifnet *)ifh; 4496 4497 ETHER_BPF_MTAP(ifp, m); 4498 } 4499 4500 void 4501 if_vlancap(if_t ifh) 4502 { 4503 struct ifnet *ifp = (struct ifnet *)ifh; 4504 VLAN_CAPABILITIES(ifp); 4505 } 4506 4507 int 4508 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax) 4509 { 4510 4511 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax; 4512 return (0); 4513 } 4514 4515 int 4516 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount) 4517 { 4518 4519 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount; 4520 return (0); 4521 } 4522 4523 int 4524 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize) 4525 { 4526 4527 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize; 4528 return (0); 4529 } 4530 4531 u_int 4532 if_gethwtsomax(if_t ifp) 4533 { 4534 4535 return (((struct ifnet *)ifp)->if_hw_tsomax); 4536 } 4537 4538 u_int 4539 if_gethwtsomaxsegcount(if_t ifp) 4540 { 4541 4542 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount); 4543 } 4544 4545 u_int 4546 if_gethwtsomaxsegsize(if_t ifp) 4547 { 4548 4549 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize); 4550 } 4551 4552 void 4553 if_setinitfn(if_t ifp, void (*init_fn)(void *)) 4554 { 4555 ((struct ifnet *)ifp)->if_init = init_fn; 4556 } 4557 4558 void 4559 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t)) 4560 { 4561 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn; 4562 } 4563 4564 void 4565 if_setstartfn(if_t ifp, void (*start_fn)(if_t)) 4566 { 4567 ((struct ifnet *)ifp)->if_start = (void *)start_fn; 4568 } 4569 4570 void 4571 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn) 4572 { 4573 ((struct ifnet *)ifp)->if_transmit = start_fn; 4574 } 4575 4576 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn) 4577 { 4578 ((struct ifnet *)ifp)->if_qflush = flush_fn; 4579 4580 } 4581 4582 void 4583 if_setgetcounterfn(if_t ifp, if_get_counter_t fn) 4584 { 4585 4586 ifp->if_get_counter = fn; 4587 } 4588 4589 /* Revisit these - These are inline functions originally. */ 4590 int 4591 drbr_inuse_drv(if_t ifh, struct buf_ring *br) 4592 { 4593 return drbr_inuse(ifh, br); 4594 } 4595 4596 struct mbuf* 4597 drbr_dequeue_drv(if_t ifh, struct buf_ring *br) 4598 { 4599 return drbr_dequeue(ifh, br); 4600 } 4601 4602 int 4603 drbr_needs_enqueue_drv(if_t ifh, struct buf_ring *br) 4604 { 4605 return drbr_needs_enqueue(ifh, br); 4606 } 4607 4608 int 4609 drbr_enqueue_drv(if_t ifh, struct buf_ring *br, struct mbuf *m) 4610 { 4611 return drbr_enqueue(ifh, br, m); 4612 4613 } 4614