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