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