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