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