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