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