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