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