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