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