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