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