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 1957 NET_EPOCH_ASSERT(); 1958 /* 1959 * AF_LINK addresses can be looked up directly by their index number, 1960 * so do that if we can. 1961 */ 1962 if (af == AF_LINK) { 1963 const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)addr; 1964 if (sdl->sdl_index && sdl->sdl_index <= V_if_index) 1965 return (ifaddr_byindex(sdl->sdl_index)); 1966 } 1967 1968 /* 1969 * Scan though each interface, looking for ones that have addresses 1970 * in this address family and the requested fib. 1971 */ 1972 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1973 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum)) 1974 continue; 1975 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1976 const char *cp, *cp2, *cp3; 1977 1978 if (ifa->ifa_addr->sa_family != af) 1979 next: continue; 1980 if (af == AF_INET && 1981 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) { 1982 /* 1983 * This is a bit broken as it doesn't 1984 * take into account that the remote end may 1985 * be a single node in the network we are 1986 * looking for. 1987 * The trouble is that we don't know the 1988 * netmask for the remote end. 1989 */ 1990 if (ifa->ifa_dstaddr != NULL && 1991 sa_equal(addr, ifa->ifa_dstaddr)) { 1992 goto done; 1993 } 1994 } else { 1995 /* 1996 * Scan all the bits in the ifa's address. 1997 * If a bit dissagrees with what we are 1998 * looking for, mask it with the netmask 1999 * to see if it really matters. 2000 * (A byte at a time) 2001 */ 2002 if (ifa->ifa_netmask == 0) 2003 continue; 2004 cp = addr_data; 2005 cp2 = ifa->ifa_addr->sa_data; 2006 cp3 = ifa->ifa_netmask->sa_data; 2007 cplim = ifa->ifa_netmask->sa_len 2008 + (char *)ifa->ifa_netmask; 2009 while (cp3 < cplim) 2010 if ((*cp++ ^ *cp2++) & *cp3++) 2011 goto next; /* next address! */ 2012 /* 2013 * If the netmask of what we just found 2014 * is more specific than what we had before 2015 * (if we had one), or if the virtual status 2016 * of new prefix is better than of the old one, 2017 * then remember the new one before continuing 2018 * to search for an even better one. 2019 */ 2020 if (ifa_maybe == NULL || 2021 ifa_preferred(ifa_maybe, ifa) || 2022 rn_refines((caddr_t)ifa->ifa_netmask, 2023 (caddr_t)ifa_maybe->ifa_netmask)) { 2024 ifa_maybe = ifa; 2025 } 2026 } 2027 } 2028 } 2029 ifa = ifa_maybe; 2030 ifa_maybe = NULL; 2031 done: 2032 return (ifa); 2033 } 2034 2035 /* 2036 * Find an interface address specific to an interface best matching 2037 * a given address. 2038 */ 2039 struct ifaddr * 2040 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp) 2041 { 2042 struct ifaddr *ifa; 2043 const char *cp, *cp2, *cp3; 2044 char *cplim; 2045 struct ifaddr *ifa_maybe = NULL; 2046 u_int af = addr->sa_family; 2047 2048 if (af >= AF_MAX) 2049 return (NULL); 2050 2051 NET_EPOCH_ASSERT(); 2052 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2053 if (ifa->ifa_addr->sa_family != af) 2054 continue; 2055 if (ifa_maybe == NULL) 2056 ifa_maybe = ifa; 2057 if (ifa->ifa_netmask == 0) { 2058 if (sa_equal(addr, ifa->ifa_addr) || 2059 (ifa->ifa_dstaddr && 2060 sa_equal(addr, ifa->ifa_dstaddr))) 2061 goto done; 2062 continue; 2063 } 2064 if (ifp->if_flags & IFF_POINTOPOINT) { 2065 if (sa_equal(addr, ifa->ifa_dstaddr)) 2066 goto done; 2067 } else { 2068 cp = addr->sa_data; 2069 cp2 = ifa->ifa_addr->sa_data; 2070 cp3 = ifa->ifa_netmask->sa_data; 2071 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 2072 for (; cp3 < cplim; cp3++) 2073 if ((*cp++ ^ *cp2++) & *cp3) 2074 break; 2075 if (cp3 == cplim) 2076 goto done; 2077 } 2078 } 2079 ifa = ifa_maybe; 2080 done: 2081 return (ifa); 2082 } 2083 2084 /* 2085 * See whether new ifa is better than current one: 2086 * 1) A non-virtual one is preferred over virtual. 2087 * 2) A virtual in master state preferred over any other state. 2088 * 2089 * Used in several address selecting functions. 2090 */ 2091 int 2092 ifa_preferred(struct ifaddr *cur, struct ifaddr *next) 2093 { 2094 2095 return (cur->ifa_carp && (!next->ifa_carp || 2096 ((*carp_master_p)(next) && !(*carp_master_p)(cur)))); 2097 } 2098 2099 struct sockaddr_dl * 2100 link_alloc_sdl(size_t size, int flags) 2101 { 2102 2103 return (malloc(size, M_TEMP, flags)); 2104 } 2105 2106 void 2107 link_free_sdl(struct sockaddr *sa) 2108 { 2109 free(sa, M_TEMP); 2110 } 2111 2112 /* 2113 * Fills in given sdl with interface basic info. 2114 * Returns pointer to filled sdl. 2115 */ 2116 struct sockaddr_dl * 2117 link_init_sdl(struct ifnet *ifp, struct sockaddr *paddr, u_char iftype) 2118 { 2119 struct sockaddr_dl *sdl; 2120 2121 sdl = (struct sockaddr_dl *)paddr; 2122 memset(sdl, 0, sizeof(struct sockaddr_dl)); 2123 sdl->sdl_len = sizeof(struct sockaddr_dl); 2124 sdl->sdl_family = AF_LINK; 2125 sdl->sdl_index = ifp->if_index; 2126 sdl->sdl_type = iftype; 2127 2128 return (sdl); 2129 } 2130 2131 /* 2132 * Mark an interface down and notify protocols of 2133 * the transition. 2134 */ 2135 static void 2136 if_unroute(struct ifnet *ifp, int flag, int fam) 2137 { 2138 struct ifaddr *ifa; 2139 struct epoch_tracker et; 2140 2141 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP")); 2142 2143 ifp->if_flags &= ~flag; 2144 getmicrotime(&ifp->if_lastchange); 2145 NET_EPOCH_ENTER(et); 2146 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 2147 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 2148 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 2149 NET_EPOCH_EXIT(et); 2150 ifp->if_qflush(ifp); 2151 2152 if (ifp->if_carp) 2153 (*carp_linkstate_p)(ifp); 2154 rt_ifmsg(ifp); 2155 } 2156 2157 /* 2158 * Mark an interface up and notify protocols of 2159 * the transition. 2160 */ 2161 static void 2162 if_route(struct ifnet *ifp, int flag, int fam) 2163 { 2164 struct ifaddr *ifa; 2165 struct epoch_tracker et; 2166 2167 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP")); 2168 2169 ifp->if_flags |= flag; 2170 getmicrotime(&ifp->if_lastchange); 2171 NET_EPOCH_ENTER(et); 2172 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 2173 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 2174 pfctlinput(PRC_IFUP, ifa->ifa_addr); 2175 NET_EPOCH_EXIT(et); 2176 if (ifp->if_carp) 2177 (*carp_linkstate_p)(ifp); 2178 rt_ifmsg(ifp); 2179 #ifdef INET6 2180 in6_if_up(ifp); 2181 #endif 2182 } 2183 2184 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */ 2185 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */ 2186 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *); 2187 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t); 2188 int (*vlan_tag_p)(struct ifnet *, uint16_t *); 2189 int (*vlan_pcp_p)(struct ifnet *, uint16_t *); 2190 int (*vlan_setcookie_p)(struct ifnet *, void *); 2191 void *(*vlan_cookie_p)(struct ifnet *); 2192 2193 /* 2194 * Handle a change in the interface link state. To avoid LORs 2195 * between driver lock and upper layer locks, as well as possible 2196 * recursions, we post event to taskqueue, and all job 2197 * is done in static do_link_state_change(). 2198 */ 2199 void 2200 if_link_state_change(struct ifnet *ifp, int link_state) 2201 { 2202 /* Return if state hasn't changed. */ 2203 if (ifp->if_link_state == link_state) 2204 return; 2205 2206 ifp->if_link_state = link_state; 2207 2208 /* XXXGL: reference ifp? */ 2209 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask); 2210 } 2211 2212 static void 2213 do_link_state_change(void *arg, int pending) 2214 { 2215 struct ifnet *ifp; 2216 int link_state; 2217 2218 ifp = arg; 2219 link_state = ifp->if_link_state; 2220 2221 CURVNET_SET(ifp->if_vnet); 2222 rt_ifmsg(ifp); 2223 if (ifp->if_vlantrunk != NULL) 2224 (*vlan_link_state_p)(ifp); 2225 2226 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) && 2227 ifp->if_l2com != NULL) 2228 (*ng_ether_link_state_p)(ifp, link_state); 2229 if (ifp->if_carp) 2230 (*carp_linkstate_p)(ifp); 2231 if (ifp->if_bridge) 2232 ifp->if_bridge_linkstate(ifp); 2233 if (ifp->if_lagg) 2234 (*lagg_linkstate_p)(ifp, link_state); 2235 2236 if (IS_DEFAULT_VNET(curvnet)) 2237 devctl_notify("IFNET", ifp->if_xname, 2238 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", 2239 NULL); 2240 if (pending > 1) 2241 if_printf(ifp, "%d link states coalesced\n", pending); 2242 if (log_link_state_change) 2243 if_printf(ifp, "link state changed to %s\n", 2244 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" ); 2245 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, link_state); 2246 CURVNET_RESTORE(); 2247 } 2248 2249 /* 2250 * Mark an interface down and notify protocols of 2251 * the transition. 2252 */ 2253 void 2254 if_down(struct ifnet *ifp) 2255 { 2256 2257 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_DOWN); 2258 if_unroute(ifp, IFF_UP, AF_UNSPEC); 2259 } 2260 2261 /* 2262 * Mark an interface up and notify protocols of 2263 * the transition. 2264 */ 2265 void 2266 if_up(struct ifnet *ifp) 2267 { 2268 2269 if_route(ifp, IFF_UP, AF_UNSPEC); 2270 EVENTHANDLER_INVOKE(ifnet_event, ifp, IFNET_EVENT_UP); 2271 } 2272 2273 /* 2274 * Flush an interface queue. 2275 */ 2276 void 2277 if_qflush(struct ifnet *ifp) 2278 { 2279 struct mbuf *m, *n; 2280 struct ifaltq *ifq; 2281 2282 ifq = &ifp->if_snd; 2283 IFQ_LOCK(ifq); 2284 #ifdef ALTQ 2285 if (ALTQ_IS_ENABLED(ifq)) 2286 ALTQ_PURGE(ifq); 2287 #endif 2288 n = ifq->ifq_head; 2289 while ((m = n) != NULL) { 2290 n = m->m_nextpkt; 2291 m_freem(m); 2292 } 2293 ifq->ifq_head = 0; 2294 ifq->ifq_tail = 0; 2295 ifq->ifq_len = 0; 2296 IFQ_UNLOCK(ifq); 2297 } 2298 2299 /* 2300 * Map interface name to interface structure pointer, with or without 2301 * returning a reference. 2302 */ 2303 struct ifnet * 2304 ifunit_ref(const char *name) 2305 { 2306 struct epoch_tracker et; 2307 struct ifnet *ifp; 2308 2309 NET_EPOCH_ENTER(et); 2310 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2311 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 && 2312 !(ifp->if_flags & IFF_DYING)) 2313 break; 2314 } 2315 if (ifp != NULL) 2316 if_ref(ifp); 2317 NET_EPOCH_EXIT(et); 2318 return (ifp); 2319 } 2320 2321 struct ifnet * 2322 ifunit(const char *name) 2323 { 2324 struct epoch_tracker et; 2325 struct ifnet *ifp; 2326 2327 NET_EPOCH_ENTER(et); 2328 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2329 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0) 2330 break; 2331 } 2332 NET_EPOCH_EXIT(et); 2333 return (ifp); 2334 } 2335 2336 void * 2337 ifr_buffer_get_buffer(void *data) 2338 { 2339 union ifreq_union *ifrup; 2340 2341 ifrup = data; 2342 #ifdef COMPAT_FREEBSD32 2343 if (SV_CURPROC_FLAG(SV_ILP32)) 2344 return ((void *)(uintptr_t) 2345 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer); 2346 #endif 2347 return (ifrup->ifr.ifr_ifru.ifru_buffer.buffer); 2348 } 2349 2350 static void 2351 ifr_buffer_set_buffer_null(void *data) 2352 { 2353 union ifreq_union *ifrup; 2354 2355 ifrup = data; 2356 #ifdef COMPAT_FREEBSD32 2357 if (SV_CURPROC_FLAG(SV_ILP32)) 2358 ifrup->ifr32.ifr_ifru.ifru_buffer.buffer = 0; 2359 else 2360 #endif 2361 ifrup->ifr.ifr_ifru.ifru_buffer.buffer = NULL; 2362 } 2363 2364 size_t 2365 ifr_buffer_get_length(void *data) 2366 { 2367 union ifreq_union *ifrup; 2368 2369 ifrup = data; 2370 #ifdef COMPAT_FREEBSD32 2371 if (SV_CURPROC_FLAG(SV_ILP32)) 2372 return (ifrup->ifr32.ifr_ifru.ifru_buffer.length); 2373 #endif 2374 return (ifrup->ifr.ifr_ifru.ifru_buffer.length); 2375 } 2376 2377 static void 2378 ifr_buffer_set_length(void *data, size_t len) 2379 { 2380 union ifreq_union *ifrup; 2381 2382 ifrup = data; 2383 #ifdef COMPAT_FREEBSD32 2384 if (SV_CURPROC_FLAG(SV_ILP32)) 2385 ifrup->ifr32.ifr_ifru.ifru_buffer.length = len; 2386 else 2387 #endif 2388 ifrup->ifr.ifr_ifru.ifru_buffer.length = len; 2389 } 2390 2391 void * 2392 ifr_data_get_ptr(void *ifrp) 2393 { 2394 union ifreq_union *ifrup; 2395 2396 ifrup = ifrp; 2397 #ifdef COMPAT_FREEBSD32 2398 if (SV_CURPROC_FLAG(SV_ILP32)) 2399 return ((void *)(uintptr_t) 2400 ifrup->ifr32.ifr_ifru.ifru_data); 2401 #endif 2402 return (ifrup->ifr.ifr_ifru.ifru_data); 2403 } 2404 2405 /* 2406 * Hardware specific interface ioctls. 2407 */ 2408 int 2409 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td) 2410 { 2411 struct ifreq *ifr; 2412 int error = 0, do_ifup = 0; 2413 int new_flags, temp_flags; 2414 size_t namelen, onamelen; 2415 size_t descrlen; 2416 char *descrbuf, *odescrbuf; 2417 char new_name[IFNAMSIZ]; 2418 char old_name[IFNAMSIZ], strbuf[IFNAMSIZ + 8]; 2419 struct ifaddr *ifa; 2420 struct sockaddr_dl *sdl; 2421 2422 ifr = (struct ifreq *)data; 2423 switch (cmd) { 2424 case SIOCGIFINDEX: 2425 ifr->ifr_index = ifp->if_index; 2426 break; 2427 2428 case SIOCGIFFLAGS: 2429 temp_flags = ifp->if_flags | ifp->if_drv_flags; 2430 ifr->ifr_flags = temp_flags & 0xffff; 2431 ifr->ifr_flagshigh = temp_flags >> 16; 2432 break; 2433 2434 case SIOCGIFCAP: 2435 ifr->ifr_reqcap = ifp->if_capabilities; 2436 ifr->ifr_curcap = ifp->if_capenable; 2437 break; 2438 2439 case SIOCGIFDATA: 2440 { 2441 struct if_data ifd; 2442 2443 /* Ensure uninitialised padding is not leaked. */ 2444 memset(&ifd, 0, sizeof(ifd)); 2445 2446 if_data_copy(ifp, &ifd); 2447 error = copyout(&ifd, ifr_data_get_ptr(ifr), sizeof(ifd)); 2448 break; 2449 } 2450 2451 #ifdef MAC 2452 case SIOCGIFMAC: 2453 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp); 2454 break; 2455 #endif 2456 2457 case SIOCGIFMETRIC: 2458 ifr->ifr_metric = ifp->if_metric; 2459 break; 2460 2461 case SIOCGIFMTU: 2462 ifr->ifr_mtu = ifp->if_mtu; 2463 break; 2464 2465 case SIOCGIFPHYS: 2466 /* XXXGL: did this ever worked? */ 2467 ifr->ifr_phys = 0; 2468 break; 2469 2470 case SIOCGIFDESCR: 2471 error = 0; 2472 sx_slock(&ifdescr_sx); 2473 if (ifp->if_description == NULL) 2474 error = ENOMSG; 2475 else { 2476 /* space for terminating nul */ 2477 descrlen = strlen(ifp->if_description) + 1; 2478 if (ifr_buffer_get_length(ifr) < descrlen) 2479 ifr_buffer_set_buffer_null(ifr); 2480 else 2481 error = copyout(ifp->if_description, 2482 ifr_buffer_get_buffer(ifr), descrlen); 2483 ifr_buffer_set_length(ifr, descrlen); 2484 } 2485 sx_sunlock(&ifdescr_sx); 2486 break; 2487 2488 case SIOCSIFDESCR: 2489 error = priv_check(td, PRIV_NET_SETIFDESCR); 2490 if (error) 2491 return (error); 2492 2493 /* 2494 * Copy only (length-1) bytes to make sure that 2495 * if_description is always nul terminated. The 2496 * length parameter is supposed to count the 2497 * terminating nul in. 2498 */ 2499 if (ifr_buffer_get_length(ifr) > ifdescr_maxlen) 2500 return (ENAMETOOLONG); 2501 else if (ifr_buffer_get_length(ifr) == 0) 2502 descrbuf = NULL; 2503 else { 2504 descrbuf = malloc(ifr_buffer_get_length(ifr), 2505 M_IFDESCR, M_WAITOK | M_ZERO); 2506 error = copyin(ifr_buffer_get_buffer(ifr), descrbuf, 2507 ifr_buffer_get_length(ifr) - 1); 2508 if (error) { 2509 free(descrbuf, M_IFDESCR); 2510 break; 2511 } 2512 } 2513 2514 sx_xlock(&ifdescr_sx); 2515 odescrbuf = ifp->if_description; 2516 ifp->if_description = descrbuf; 2517 sx_xunlock(&ifdescr_sx); 2518 2519 getmicrotime(&ifp->if_lastchange); 2520 free(odescrbuf, M_IFDESCR); 2521 break; 2522 2523 case SIOCGIFFIB: 2524 ifr->ifr_fib = ifp->if_fib; 2525 break; 2526 2527 case SIOCSIFFIB: 2528 error = priv_check(td, PRIV_NET_SETIFFIB); 2529 if (error) 2530 return (error); 2531 if (ifr->ifr_fib >= rt_numfibs) 2532 return (EINVAL); 2533 2534 ifp->if_fib = ifr->ifr_fib; 2535 break; 2536 2537 case SIOCSIFFLAGS: 2538 error = priv_check(td, PRIV_NET_SETIFFLAGS); 2539 if (error) 2540 return (error); 2541 /* 2542 * Currently, no driver owned flags pass the IFF_CANTCHANGE 2543 * check, so we don't need special handling here yet. 2544 */ 2545 new_flags = (ifr->ifr_flags & 0xffff) | 2546 (ifr->ifr_flagshigh << 16); 2547 if (ifp->if_flags & IFF_UP && 2548 (new_flags & IFF_UP) == 0) { 2549 if_down(ifp); 2550 } else if (new_flags & IFF_UP && 2551 (ifp->if_flags & IFF_UP) == 0) { 2552 do_ifup = 1; 2553 } 2554 /* See if permanently promiscuous mode bit is about to flip */ 2555 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) { 2556 if (new_flags & IFF_PPROMISC) 2557 ifp->if_flags |= IFF_PROMISC; 2558 else if (ifp->if_pcount == 0) 2559 ifp->if_flags &= ~IFF_PROMISC; 2560 if (log_promisc_mode_change) 2561 if_printf(ifp, "permanently promiscuous mode %s\n", 2562 ((new_flags & IFF_PPROMISC) ? 2563 "enabled" : "disabled")); 2564 } 2565 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 2566 (new_flags &~ IFF_CANTCHANGE); 2567 if (ifp->if_ioctl) { 2568 (void) (*ifp->if_ioctl)(ifp, cmd, data); 2569 } 2570 if (do_ifup) 2571 if_up(ifp); 2572 getmicrotime(&ifp->if_lastchange); 2573 break; 2574 2575 case SIOCSIFCAP: 2576 error = priv_check(td, PRIV_NET_SETIFCAP); 2577 if (error) 2578 return (error); 2579 if (ifp->if_ioctl == NULL) 2580 return (EOPNOTSUPP); 2581 if (ifr->ifr_reqcap & ~ifp->if_capabilities) 2582 return (EINVAL); 2583 error = (*ifp->if_ioctl)(ifp, cmd, data); 2584 if (error == 0) 2585 getmicrotime(&ifp->if_lastchange); 2586 break; 2587 2588 #ifdef MAC 2589 case SIOCSIFMAC: 2590 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp); 2591 break; 2592 #endif 2593 2594 case SIOCSIFNAME: 2595 error = priv_check(td, PRIV_NET_SETIFNAME); 2596 if (error) 2597 return (error); 2598 error = copyinstr(ifr_data_get_ptr(ifr), new_name, IFNAMSIZ, 2599 NULL); 2600 if (error != 0) 2601 return (error); 2602 if (new_name[0] == '\0') 2603 return (EINVAL); 2604 if (strcmp(new_name, ifp->if_xname) == 0) 2605 break; 2606 if (ifunit(new_name) != NULL) 2607 return (EEXIST); 2608 2609 /* 2610 * XXX: Locking. Nothing else seems to lock if_flags, 2611 * and there are numerous other races with the 2612 * ifunit() checks not being atomic with namespace 2613 * changes (renames, vmoves, if_attach, etc). 2614 */ 2615 ifp->if_flags |= IFF_RENAMING; 2616 2617 /* Announce the departure of the interface. */ 2618 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 2619 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 2620 2621 if_printf(ifp, "changing name to '%s'\n", new_name); 2622 2623 IF_ADDR_WLOCK(ifp); 2624 strlcpy(old_name, ifp->if_xname, sizeof(old_name)); 2625 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname)); 2626 ifa = ifp->if_addr; 2627 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 2628 namelen = strlen(new_name); 2629 onamelen = sdl->sdl_nlen; 2630 /* 2631 * Move the address if needed. This is safe because we 2632 * allocate space for a name of length IFNAMSIZ when we 2633 * create this in if_attach(). 2634 */ 2635 if (namelen != onamelen) { 2636 bcopy(sdl->sdl_data + onamelen, 2637 sdl->sdl_data + namelen, sdl->sdl_alen); 2638 } 2639 bcopy(new_name, sdl->sdl_data, namelen); 2640 sdl->sdl_nlen = namelen; 2641 sdl = (struct sockaddr_dl *)ifa->ifa_netmask; 2642 bzero(sdl->sdl_data, onamelen); 2643 while (namelen != 0) 2644 sdl->sdl_data[--namelen] = 0xff; 2645 IF_ADDR_WUNLOCK(ifp); 2646 2647 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 2648 /* Announce the return of the interface. */ 2649 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 2650 2651 ifp->if_flags &= ~IFF_RENAMING; 2652 2653 snprintf(strbuf, sizeof(strbuf), "name=%s", new_name); 2654 devctl_notify("IFNET", old_name, "RENAME", strbuf); 2655 break; 2656 2657 #ifdef VIMAGE 2658 case SIOCSIFVNET: 2659 error = priv_check(td, PRIV_NET_SETIFVNET); 2660 if (error) 2661 return (error); 2662 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid); 2663 break; 2664 #endif 2665 2666 case SIOCSIFMETRIC: 2667 error = priv_check(td, PRIV_NET_SETIFMETRIC); 2668 if (error) 2669 return (error); 2670 ifp->if_metric = ifr->ifr_metric; 2671 getmicrotime(&ifp->if_lastchange); 2672 break; 2673 2674 case SIOCSIFPHYS: 2675 error = priv_check(td, PRIV_NET_SETIFPHYS); 2676 if (error) 2677 return (error); 2678 if (ifp->if_ioctl == NULL) 2679 return (EOPNOTSUPP); 2680 error = (*ifp->if_ioctl)(ifp, cmd, data); 2681 if (error == 0) 2682 getmicrotime(&ifp->if_lastchange); 2683 break; 2684 2685 case SIOCSIFMTU: 2686 { 2687 u_long oldmtu = ifp->if_mtu; 2688 2689 error = priv_check(td, PRIV_NET_SETIFMTU); 2690 if (error) 2691 return (error); 2692 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) 2693 return (EINVAL); 2694 if (ifp->if_ioctl == NULL) 2695 return (EOPNOTSUPP); 2696 /* Disallow MTU changes on bridge member interfaces. */ 2697 if (ifp->if_bridge) 2698 return (EOPNOTSUPP); 2699 error = (*ifp->if_ioctl)(ifp, cmd, data); 2700 if (error == 0) { 2701 getmicrotime(&ifp->if_lastchange); 2702 rt_ifmsg(ifp); 2703 #ifdef INET 2704 DEBUGNET_NOTIFY_MTU(ifp); 2705 #endif 2706 } 2707 /* 2708 * If the link MTU changed, do network layer specific procedure. 2709 */ 2710 if (ifp->if_mtu != oldmtu) { 2711 #ifdef INET6 2712 nd6_setmtu(ifp); 2713 #endif 2714 rt_updatemtu(ifp); 2715 } 2716 break; 2717 } 2718 2719 case SIOCADDMULTI: 2720 case SIOCDELMULTI: 2721 if (cmd == SIOCADDMULTI) 2722 error = priv_check(td, PRIV_NET_ADDMULTI); 2723 else 2724 error = priv_check(td, PRIV_NET_DELMULTI); 2725 if (error) 2726 return (error); 2727 2728 /* Don't allow group membership on non-multicast interfaces. */ 2729 if ((ifp->if_flags & IFF_MULTICAST) == 0) 2730 return (EOPNOTSUPP); 2731 2732 /* Don't let users screw up protocols' entries. */ 2733 if (ifr->ifr_addr.sa_family != AF_LINK) 2734 return (EINVAL); 2735 2736 if (cmd == SIOCADDMULTI) { 2737 struct epoch_tracker et; 2738 struct ifmultiaddr *ifma; 2739 2740 /* 2741 * Userland is only permitted to join groups once 2742 * via the if_addmulti() KPI, because it cannot hold 2743 * struct ifmultiaddr * between calls. It may also 2744 * lose a race while we check if the membership 2745 * already exists. 2746 */ 2747 NET_EPOCH_ENTER(et); 2748 ifma = if_findmulti(ifp, &ifr->ifr_addr); 2749 NET_EPOCH_EXIT(et); 2750 if (ifma != NULL) 2751 error = EADDRINUSE; 2752 else 2753 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 2754 } else { 2755 error = if_delmulti(ifp, &ifr->ifr_addr); 2756 } 2757 if (error == 0) 2758 getmicrotime(&ifp->if_lastchange); 2759 break; 2760 2761 case SIOCSIFPHYADDR: 2762 case SIOCDIFPHYADDR: 2763 #ifdef INET6 2764 case SIOCSIFPHYADDR_IN6: 2765 #endif 2766 case SIOCSIFMEDIA: 2767 case SIOCSIFGENERIC: 2768 error = priv_check(td, PRIV_NET_HWIOCTL); 2769 if (error) 2770 return (error); 2771 if (ifp->if_ioctl == NULL) 2772 return (EOPNOTSUPP); 2773 error = (*ifp->if_ioctl)(ifp, cmd, data); 2774 if (error == 0) 2775 getmicrotime(&ifp->if_lastchange); 2776 break; 2777 2778 case SIOCGIFSTATUS: 2779 case SIOCGIFPSRCADDR: 2780 case SIOCGIFPDSTADDR: 2781 case SIOCGIFMEDIA: 2782 case SIOCGIFXMEDIA: 2783 case SIOCGIFGENERIC: 2784 case SIOCGIFRSSKEY: 2785 case SIOCGIFRSSHASH: 2786 case SIOCGIFDOWNREASON: 2787 if (ifp->if_ioctl == NULL) 2788 return (EOPNOTSUPP); 2789 error = (*ifp->if_ioctl)(ifp, cmd, data); 2790 break; 2791 2792 case SIOCSIFLLADDR: 2793 error = priv_check(td, PRIV_NET_SETLLADDR); 2794 if (error) 2795 return (error); 2796 error = if_setlladdr(ifp, 2797 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); 2798 break; 2799 2800 case SIOCGHWADDR: 2801 error = if_gethwaddr(ifp, ifr); 2802 break; 2803 2804 case SIOCAIFGROUP: 2805 error = priv_check(td, PRIV_NET_ADDIFGROUP); 2806 if (error) 2807 return (error); 2808 error = if_addgroup(ifp, 2809 ((struct ifgroupreq *)data)->ifgr_group); 2810 if (error != 0) 2811 return (error); 2812 break; 2813 2814 case SIOCGIFGROUP: 2815 { 2816 struct epoch_tracker et; 2817 2818 NET_EPOCH_ENTER(et); 2819 error = if_getgroup((struct ifgroupreq *)data, ifp); 2820 NET_EPOCH_EXIT(et); 2821 break; 2822 } 2823 2824 case SIOCDIFGROUP: 2825 error = priv_check(td, PRIV_NET_DELIFGROUP); 2826 if (error) 2827 return (error); 2828 error = if_delgroup(ifp, 2829 ((struct ifgroupreq *)data)->ifgr_group); 2830 if (error != 0) 2831 return (error); 2832 break; 2833 2834 default: 2835 error = ENOIOCTL; 2836 break; 2837 } 2838 return (error); 2839 } 2840 2841 /* 2842 * Interface ioctls. 2843 */ 2844 int 2845 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td) 2846 { 2847 #ifdef COMPAT_FREEBSD32 2848 union { 2849 struct ifconf ifc; 2850 struct ifdrv ifd; 2851 struct ifgroupreq ifgr; 2852 struct ifmediareq ifmr; 2853 } thunk; 2854 u_long saved_cmd; 2855 struct ifconf32 *ifc32; 2856 struct ifdrv32 *ifd32; 2857 struct ifgroupreq32 *ifgr32; 2858 struct ifmediareq32 *ifmr32; 2859 #endif 2860 struct ifnet *ifp; 2861 struct ifreq *ifr; 2862 int error; 2863 int oif_flags; 2864 #ifdef VIMAGE 2865 bool shutdown; 2866 #endif 2867 2868 CURVNET_SET(so->so_vnet); 2869 #ifdef VIMAGE 2870 /* Make sure the VNET is stable. */ 2871 shutdown = VNET_IS_SHUTTING_DOWN(so->so_vnet); 2872 if (shutdown) { 2873 CURVNET_RESTORE(); 2874 return (EBUSY); 2875 } 2876 #endif 2877 2878 #ifdef COMPAT_FREEBSD32 2879 saved_cmd = cmd; 2880 switch (cmd) { 2881 case SIOCGIFCONF32: 2882 ifc32 = (struct ifconf32 *)data; 2883 thunk.ifc.ifc_len = ifc32->ifc_len; 2884 thunk.ifc.ifc_buf = PTRIN(ifc32->ifc_buf); 2885 data = (caddr_t)&thunk.ifc; 2886 cmd = SIOCGIFCONF; 2887 break; 2888 case SIOCGDRVSPEC32: 2889 case SIOCSDRVSPEC32: 2890 ifd32 = (struct ifdrv32 *)data; 2891 memcpy(thunk.ifd.ifd_name, ifd32->ifd_name, 2892 sizeof(thunk.ifd.ifd_name)); 2893 thunk.ifd.ifd_cmd = ifd32->ifd_cmd; 2894 thunk.ifd.ifd_len = ifd32->ifd_len; 2895 thunk.ifd.ifd_data = PTRIN(ifd32->ifd_data); 2896 data = (caddr_t)&thunk.ifd; 2897 cmd = _IOC_NEWTYPE(cmd, struct ifdrv); 2898 break; 2899 case SIOCAIFGROUP32: 2900 case SIOCGIFGROUP32: 2901 case SIOCDIFGROUP32: 2902 case SIOCGIFGMEMB32: 2903 ifgr32 = (struct ifgroupreq32 *)data; 2904 memcpy(thunk.ifgr.ifgr_name, ifgr32->ifgr_name, 2905 sizeof(thunk.ifgr.ifgr_name)); 2906 thunk.ifgr.ifgr_len = ifgr32->ifgr_len; 2907 switch (cmd) { 2908 case SIOCAIFGROUP32: 2909 case SIOCDIFGROUP32: 2910 memcpy(thunk.ifgr.ifgr_group, ifgr32->ifgr_group, 2911 sizeof(thunk.ifgr.ifgr_group)); 2912 break; 2913 case SIOCGIFGROUP32: 2914 case SIOCGIFGMEMB32: 2915 thunk.ifgr.ifgr_groups = PTRIN(ifgr32->ifgr_groups); 2916 break; 2917 } 2918 data = (caddr_t)&thunk.ifgr; 2919 cmd = _IOC_NEWTYPE(cmd, struct ifgroupreq); 2920 break; 2921 case SIOCGIFMEDIA32: 2922 case SIOCGIFXMEDIA32: 2923 ifmr32 = (struct ifmediareq32 *)data; 2924 memcpy(thunk.ifmr.ifm_name, ifmr32->ifm_name, 2925 sizeof(thunk.ifmr.ifm_name)); 2926 thunk.ifmr.ifm_current = ifmr32->ifm_current; 2927 thunk.ifmr.ifm_mask = ifmr32->ifm_mask; 2928 thunk.ifmr.ifm_status = ifmr32->ifm_status; 2929 thunk.ifmr.ifm_active = ifmr32->ifm_active; 2930 thunk.ifmr.ifm_count = ifmr32->ifm_count; 2931 thunk.ifmr.ifm_ulist = PTRIN(ifmr32->ifm_ulist); 2932 data = (caddr_t)&thunk.ifmr; 2933 cmd = _IOC_NEWTYPE(cmd, struct ifmediareq); 2934 break; 2935 } 2936 #endif 2937 2938 switch (cmd) { 2939 case SIOCGIFCONF: 2940 error = ifconf(cmd, data); 2941 goto out_noref; 2942 } 2943 2944 ifr = (struct ifreq *)data; 2945 switch (cmd) { 2946 #ifdef VIMAGE 2947 case SIOCSIFRVNET: 2948 error = priv_check(td, PRIV_NET_SETIFVNET); 2949 if (error == 0) 2950 error = if_vmove_reclaim(td, ifr->ifr_name, 2951 ifr->ifr_jid); 2952 goto out_noref; 2953 #endif 2954 case SIOCIFCREATE: 2955 case SIOCIFCREATE2: 2956 error = priv_check(td, PRIV_NET_IFCREATE); 2957 if (error == 0) 2958 error = if_clone_create(ifr->ifr_name, 2959 sizeof(ifr->ifr_name), cmd == SIOCIFCREATE2 ? 2960 ifr_data_get_ptr(ifr) : NULL); 2961 goto out_noref; 2962 case SIOCIFDESTROY: 2963 error = priv_check(td, PRIV_NET_IFDESTROY); 2964 2965 if (error == 0) { 2966 sx_xlock(&ifnet_detach_sxlock); 2967 error = if_clone_destroy(ifr->ifr_name); 2968 sx_xunlock(&ifnet_detach_sxlock); 2969 } 2970 goto out_noref; 2971 2972 case SIOCIFGCLONERS: 2973 error = if_clone_list((struct if_clonereq *)data); 2974 goto out_noref; 2975 2976 case SIOCGIFGMEMB: 2977 error = if_getgroupmembers((struct ifgroupreq *)data); 2978 goto out_noref; 2979 2980 #if defined(INET) || defined(INET6) 2981 case SIOCSVH: 2982 case SIOCGVH: 2983 if (carp_ioctl_p == NULL) 2984 error = EPROTONOSUPPORT; 2985 else 2986 error = (*carp_ioctl_p)(ifr, cmd, td); 2987 goto out_noref; 2988 #endif 2989 } 2990 2991 ifp = ifunit_ref(ifr->ifr_name); 2992 if (ifp == NULL) { 2993 error = ENXIO; 2994 goto out_noref; 2995 } 2996 2997 error = ifhwioctl(cmd, ifp, data, td); 2998 if (error != ENOIOCTL) 2999 goto out_ref; 3000 3001 oif_flags = ifp->if_flags; 3002 if (so->so_proto == NULL) { 3003 error = EOPNOTSUPP; 3004 goto out_ref; 3005 } 3006 3007 /* 3008 * Pass the request on to the socket control method, and if the 3009 * latter returns EOPNOTSUPP, directly to the interface. 3010 * 3011 * Make an exception for the legacy SIOCSIF* requests. Drivers 3012 * trust SIOCSIFADDR et al to come from an already privileged 3013 * layer, and do not perform any credentials checks or input 3014 * validation. 3015 */ 3016 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, data, 3017 ifp, td)); 3018 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL && 3019 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR && 3020 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK) 3021 error = (*ifp->if_ioctl)(ifp, cmd, data); 3022 3023 if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 3024 #ifdef INET6 3025 if (ifp->if_flags & IFF_UP) 3026 in6_if_up(ifp); 3027 #endif 3028 } 3029 3030 out_ref: 3031 if_rele(ifp); 3032 out_noref: 3033 CURVNET_RESTORE(); 3034 #ifdef COMPAT_FREEBSD32 3035 if (error != 0) 3036 return (error); 3037 switch (saved_cmd) { 3038 case SIOCGIFCONF32: 3039 ifc32->ifc_len = thunk.ifc.ifc_len; 3040 break; 3041 case SIOCGDRVSPEC32: 3042 /* 3043 * SIOCGDRVSPEC is IOWR, but nothing actually touches 3044 * the struct so just assert that ifd_len (the only 3045 * field it might make sense to update) hasn't 3046 * changed. 3047 */ 3048 KASSERT(thunk.ifd.ifd_len == ifd32->ifd_len, 3049 ("ifd_len was updated %u -> %zu", ifd32->ifd_len, 3050 thunk.ifd.ifd_len)); 3051 break; 3052 case SIOCGIFGROUP32: 3053 case SIOCGIFGMEMB32: 3054 ifgr32->ifgr_len = thunk.ifgr.ifgr_len; 3055 break; 3056 case SIOCGIFMEDIA32: 3057 case SIOCGIFXMEDIA32: 3058 ifmr32->ifm_current = thunk.ifmr.ifm_current; 3059 ifmr32->ifm_mask = thunk.ifmr.ifm_mask; 3060 ifmr32->ifm_status = thunk.ifmr.ifm_status; 3061 ifmr32->ifm_active = thunk.ifmr.ifm_active; 3062 ifmr32->ifm_count = thunk.ifmr.ifm_count; 3063 break; 3064 } 3065 #endif 3066 return (error); 3067 } 3068 3069 /* 3070 * The code common to handling reference counted flags, 3071 * e.g., in ifpromisc() and if_allmulti(). 3072 * The "pflag" argument can specify a permanent mode flag to check, 3073 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none. 3074 * 3075 * Only to be used on stack-owned flags, not driver-owned flags. 3076 */ 3077 static int 3078 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch) 3079 { 3080 struct ifreq ifr; 3081 int error; 3082 int oldflags, oldcount; 3083 3084 /* Sanity checks to catch programming errors */ 3085 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0, 3086 ("%s: setting driver-owned flag %d", __func__, flag)); 3087 3088 if (onswitch) 3089 KASSERT(*refcount >= 0, 3090 ("%s: increment negative refcount %d for flag %d", 3091 __func__, *refcount, flag)); 3092 else 3093 KASSERT(*refcount > 0, 3094 ("%s: decrement non-positive refcount %d for flag %d", 3095 __func__, *refcount, flag)); 3096 3097 /* In case this mode is permanent, just touch refcount */ 3098 if (ifp->if_flags & pflag) { 3099 *refcount += onswitch ? 1 : -1; 3100 return (0); 3101 } 3102 3103 /* Save ifnet parameters for if_ioctl() may fail */ 3104 oldcount = *refcount; 3105 oldflags = ifp->if_flags; 3106 3107 /* 3108 * See if we aren't the only and touching refcount is enough. 3109 * Actually toggle interface flag if we are the first or last. 3110 */ 3111 if (onswitch) { 3112 if ((*refcount)++) 3113 return (0); 3114 ifp->if_flags |= flag; 3115 } else { 3116 if (--(*refcount)) 3117 return (0); 3118 ifp->if_flags &= ~flag; 3119 } 3120 3121 /* Call down the driver since we've changed interface flags */ 3122 if (ifp->if_ioctl == NULL) { 3123 error = EOPNOTSUPP; 3124 goto recover; 3125 } 3126 ifr.ifr_flags = ifp->if_flags & 0xffff; 3127 ifr.ifr_flagshigh = ifp->if_flags >> 16; 3128 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 3129 if (error) 3130 goto recover; 3131 /* Notify userland that interface flags have changed */ 3132 rt_ifmsg(ifp); 3133 return (0); 3134 3135 recover: 3136 /* Recover after driver error */ 3137 *refcount = oldcount; 3138 ifp->if_flags = oldflags; 3139 return (error); 3140 } 3141 3142 /* 3143 * Set/clear promiscuous mode on interface ifp based on the truth value 3144 * of pswitch. The calls are reference counted so that only the first 3145 * "on" request actually has an effect, as does the final "off" request. 3146 * Results are undefined if the "off" and "on" requests are not matched. 3147 */ 3148 int 3149 ifpromisc(struct ifnet *ifp, int pswitch) 3150 { 3151 int error; 3152 int oldflags = ifp->if_flags; 3153 3154 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC, 3155 &ifp->if_pcount, pswitch); 3156 /* If promiscuous mode status has changed, log a message */ 3157 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) && 3158 log_promisc_mode_change) 3159 if_printf(ifp, "promiscuous mode %s\n", 3160 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); 3161 return (error); 3162 } 3163 3164 /* 3165 * Return interface configuration 3166 * of system. List may be used 3167 * in later ioctl's (above) to get 3168 * other information. 3169 */ 3170 /*ARGSUSED*/ 3171 static int 3172 ifconf(u_long cmd, caddr_t data) 3173 { 3174 struct ifconf *ifc = (struct ifconf *)data; 3175 struct ifnet *ifp; 3176 struct ifaddr *ifa; 3177 struct ifreq ifr; 3178 struct sbuf *sb; 3179 int error, full = 0, valid_len, max_len; 3180 3181 /* Limit initial buffer size to maxphys to avoid DoS from userspace. */ 3182 max_len = maxphys - 1; 3183 3184 /* Prevent hostile input from being able to crash the system */ 3185 if (ifc->ifc_len <= 0) 3186 return (EINVAL); 3187 3188 again: 3189 if (ifc->ifc_len <= max_len) { 3190 max_len = ifc->ifc_len; 3191 full = 1; 3192 } 3193 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN); 3194 max_len = 0; 3195 valid_len = 0; 3196 3197 IFNET_RLOCK(); 3198 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 3199 struct epoch_tracker et; 3200 int addrs; 3201 3202 /* 3203 * Zero the ifr to make sure we don't disclose the contents 3204 * of the stack. 3205 */ 3206 memset(&ifr, 0, sizeof(ifr)); 3207 3208 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)) 3209 >= sizeof(ifr.ifr_name)) { 3210 sbuf_delete(sb); 3211 IFNET_RUNLOCK(); 3212 return (ENAMETOOLONG); 3213 } 3214 3215 addrs = 0; 3216 NET_EPOCH_ENTER(et); 3217 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 3218 struct sockaddr *sa = ifa->ifa_addr; 3219 3220 if (prison_if(curthread->td_ucred, sa) != 0) 3221 continue; 3222 addrs++; 3223 if (sa->sa_len <= sizeof(*sa)) { 3224 if (sa->sa_len < sizeof(*sa)) { 3225 memset(&ifr.ifr_ifru.ifru_addr, 0, 3226 sizeof(ifr.ifr_ifru.ifru_addr)); 3227 memcpy(&ifr.ifr_ifru.ifru_addr, sa, 3228 sa->sa_len); 3229 } else 3230 ifr.ifr_ifru.ifru_addr = *sa; 3231 sbuf_bcat(sb, &ifr, sizeof(ifr)); 3232 max_len += sizeof(ifr); 3233 } else { 3234 sbuf_bcat(sb, &ifr, 3235 offsetof(struct ifreq, ifr_addr)); 3236 max_len += offsetof(struct ifreq, ifr_addr); 3237 sbuf_bcat(sb, sa, sa->sa_len); 3238 max_len += sa->sa_len; 3239 } 3240 3241 if (sbuf_error(sb) == 0) 3242 valid_len = sbuf_len(sb); 3243 } 3244 NET_EPOCH_EXIT(et); 3245 if (addrs == 0) { 3246 sbuf_bcat(sb, &ifr, sizeof(ifr)); 3247 max_len += sizeof(ifr); 3248 3249 if (sbuf_error(sb) == 0) 3250 valid_len = sbuf_len(sb); 3251 } 3252 } 3253 IFNET_RUNLOCK(); 3254 3255 /* 3256 * If we didn't allocate enough space (uncommon), try again. If 3257 * we have already allocated as much space as we are allowed, 3258 * return what we've got. 3259 */ 3260 if (valid_len != max_len && !full) { 3261 sbuf_delete(sb); 3262 goto again; 3263 } 3264 3265 ifc->ifc_len = valid_len; 3266 sbuf_finish(sb); 3267 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len); 3268 sbuf_delete(sb); 3269 return (error); 3270 } 3271 3272 /* 3273 * Just like ifpromisc(), but for all-multicast-reception mode. 3274 */ 3275 int 3276 if_allmulti(struct ifnet *ifp, int onswitch) 3277 { 3278 3279 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch)); 3280 } 3281 3282 struct ifmultiaddr * 3283 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa) 3284 { 3285 struct ifmultiaddr *ifma; 3286 3287 IF_ADDR_LOCK_ASSERT(ifp); 3288 3289 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 3290 if (sa->sa_family == AF_LINK) { 3291 if (sa_dl_equal(ifma->ifma_addr, sa)) 3292 break; 3293 } else { 3294 if (sa_equal(ifma->ifma_addr, sa)) 3295 break; 3296 } 3297 } 3298 3299 return ifma; 3300 } 3301 3302 /* 3303 * Allocate a new ifmultiaddr and initialize based on passed arguments. We 3304 * make copies of passed sockaddrs. The ifmultiaddr will not be added to 3305 * the ifnet multicast address list here, so the caller must do that and 3306 * other setup work (such as notifying the device driver). The reference 3307 * count is initialized to 1. 3308 */ 3309 static struct ifmultiaddr * 3310 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa, 3311 int mflags) 3312 { 3313 struct ifmultiaddr *ifma; 3314 struct sockaddr *dupsa; 3315 3316 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags | 3317 M_ZERO); 3318 if (ifma == NULL) 3319 return (NULL); 3320 3321 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags); 3322 if (dupsa == NULL) { 3323 free(ifma, M_IFMADDR); 3324 return (NULL); 3325 } 3326 bcopy(sa, dupsa, sa->sa_len); 3327 ifma->ifma_addr = dupsa; 3328 3329 ifma->ifma_ifp = ifp; 3330 ifma->ifma_refcount = 1; 3331 ifma->ifma_protospec = NULL; 3332 3333 if (llsa == NULL) { 3334 ifma->ifma_lladdr = NULL; 3335 return (ifma); 3336 } 3337 3338 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags); 3339 if (dupsa == NULL) { 3340 free(ifma->ifma_addr, M_IFMADDR); 3341 free(ifma, M_IFMADDR); 3342 return (NULL); 3343 } 3344 bcopy(llsa, dupsa, llsa->sa_len); 3345 ifma->ifma_lladdr = dupsa; 3346 3347 return (ifma); 3348 } 3349 3350 /* 3351 * if_freemulti: free ifmultiaddr structure and possibly attached related 3352 * addresses. The caller is responsible for implementing reference 3353 * counting, notifying the driver, handling routing messages, and releasing 3354 * any dependent link layer state. 3355 */ 3356 #ifdef MCAST_VERBOSE 3357 extern void kdb_backtrace(void); 3358 #endif 3359 static void 3360 if_freemulti_internal(struct ifmultiaddr *ifma) 3361 { 3362 3363 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d", 3364 ifma->ifma_refcount)); 3365 3366 if (ifma->ifma_lladdr != NULL) 3367 free(ifma->ifma_lladdr, M_IFMADDR); 3368 #ifdef MCAST_VERBOSE 3369 kdb_backtrace(); 3370 printf("%s freeing ifma: %p\n", __func__, ifma); 3371 #endif 3372 free(ifma->ifma_addr, M_IFMADDR); 3373 free(ifma, M_IFMADDR); 3374 } 3375 3376 static void 3377 if_destroymulti(epoch_context_t ctx) 3378 { 3379 struct ifmultiaddr *ifma; 3380 3381 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx); 3382 if_freemulti_internal(ifma); 3383 } 3384 3385 void 3386 if_freemulti(struct ifmultiaddr *ifma) 3387 { 3388 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d", 3389 ifma->ifma_refcount)); 3390 3391 NET_EPOCH_CALL(if_destroymulti, &ifma->ifma_epoch_ctx); 3392 } 3393 3394 /* 3395 * Register an additional multicast address with a network interface. 3396 * 3397 * - If the address is already present, bump the reference count on the 3398 * address and return. 3399 * - If the address is not link-layer, look up a link layer address. 3400 * - Allocate address structures for one or both addresses, and attach to the 3401 * multicast address list on the interface. If automatically adding a link 3402 * layer address, the protocol address will own a reference to the link 3403 * layer address, to be freed when it is freed. 3404 * - Notify the network device driver of an addition to the multicast address 3405 * list. 3406 * 3407 * 'sa' points to caller-owned memory with the desired multicast address. 3408 * 3409 * 'retifma' will be used to return a pointer to the resulting multicast 3410 * address reference, if desired. 3411 */ 3412 int 3413 if_addmulti(struct ifnet *ifp, struct sockaddr *sa, 3414 struct ifmultiaddr **retifma) 3415 { 3416 struct ifmultiaddr *ifma, *ll_ifma; 3417 struct sockaddr *llsa; 3418 struct sockaddr_dl sdl; 3419 int error; 3420 3421 #ifdef INET 3422 IN_MULTI_LIST_UNLOCK_ASSERT(); 3423 #endif 3424 #ifdef INET6 3425 IN6_MULTI_LIST_UNLOCK_ASSERT(); 3426 #endif 3427 /* 3428 * If the address is already present, return a new reference to it; 3429 * otherwise, allocate storage and set up a new address. 3430 */ 3431 IF_ADDR_WLOCK(ifp); 3432 ifma = if_findmulti(ifp, sa); 3433 if (ifma != NULL) { 3434 ifma->ifma_refcount++; 3435 if (retifma != NULL) 3436 *retifma = ifma; 3437 IF_ADDR_WUNLOCK(ifp); 3438 return (0); 3439 } 3440 3441 /* 3442 * The address isn't already present; resolve the protocol address 3443 * into a link layer address, and then look that up, bump its 3444 * refcount or allocate an ifma for that also. 3445 * Most link layer resolving functions returns address data which 3446 * fits inside default sockaddr_dl structure. However callback 3447 * can allocate another sockaddr structure, in that case we need to 3448 * free it later. 3449 */ 3450 llsa = NULL; 3451 ll_ifma = NULL; 3452 if (ifp->if_resolvemulti != NULL) { 3453 /* Provide called function with buffer size information */ 3454 sdl.sdl_len = sizeof(sdl); 3455 llsa = (struct sockaddr *)&sdl; 3456 error = ifp->if_resolvemulti(ifp, &llsa, sa); 3457 if (error) 3458 goto unlock_out; 3459 } 3460 3461 /* 3462 * Allocate the new address. Don't hook it up yet, as we may also 3463 * need to allocate a link layer multicast address. 3464 */ 3465 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT); 3466 if (ifma == NULL) { 3467 error = ENOMEM; 3468 goto free_llsa_out; 3469 } 3470 3471 /* 3472 * If a link layer address is found, we'll need to see if it's 3473 * already present in the address list, or allocate is as well. 3474 * When this block finishes, the link layer address will be on the 3475 * list. 3476 */ 3477 if (llsa != NULL) { 3478 ll_ifma = if_findmulti(ifp, llsa); 3479 if (ll_ifma == NULL) { 3480 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT); 3481 if (ll_ifma == NULL) { 3482 --ifma->ifma_refcount; 3483 if_freemulti(ifma); 3484 error = ENOMEM; 3485 goto free_llsa_out; 3486 } 3487 ll_ifma->ifma_flags |= IFMA_F_ENQUEUED; 3488 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma, 3489 ifma_link); 3490 } else 3491 ll_ifma->ifma_refcount++; 3492 ifma->ifma_llifma = ll_ifma; 3493 } 3494 3495 /* 3496 * We now have a new multicast address, ifma, and possibly a new or 3497 * referenced link layer address. Add the primary address to the 3498 * ifnet address list. 3499 */ 3500 ifma->ifma_flags |= IFMA_F_ENQUEUED; 3501 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 3502 3503 if (retifma != NULL) 3504 *retifma = ifma; 3505 3506 /* 3507 * Must generate the message while holding the lock so that 'ifma' 3508 * pointer is still valid. 3509 */ 3510 rt_newmaddrmsg(RTM_NEWMADDR, ifma); 3511 IF_ADDR_WUNLOCK(ifp); 3512 3513 /* 3514 * We are certain we have added something, so call down to the 3515 * interface to let them know about it. 3516 */ 3517 if (ifp->if_ioctl != NULL) { 3518 if (THREAD_CAN_SLEEP()) 3519 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0); 3520 else 3521 taskqueue_enqueue(taskqueue_swi, &ifp->if_addmultitask); 3522 } 3523 3524 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl)) 3525 link_free_sdl(llsa); 3526 3527 return (0); 3528 3529 free_llsa_out: 3530 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl)) 3531 link_free_sdl(llsa); 3532 3533 unlock_out: 3534 IF_ADDR_WUNLOCK(ifp); 3535 return (error); 3536 } 3537 3538 static void 3539 if_siocaddmulti(void *arg, int pending) 3540 { 3541 struct ifnet *ifp; 3542 3543 ifp = arg; 3544 #ifdef DIAGNOSTIC 3545 if (pending > 1) 3546 if_printf(ifp, "%d SIOCADDMULTI coalesced\n", pending); 3547 #endif 3548 CURVNET_SET(ifp->if_vnet); 3549 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0); 3550 CURVNET_RESTORE(); 3551 } 3552 3553 /* 3554 * Delete a multicast group membership by network-layer group address. 3555 * 3556 * Returns ENOENT if the entry could not be found. If ifp no longer 3557 * exists, results are undefined. This entry point should only be used 3558 * from subsystems which do appropriate locking to hold ifp for the 3559 * duration of the call. 3560 * Network-layer protocol domains must use if_delmulti_ifma(). 3561 */ 3562 int 3563 if_delmulti(struct ifnet *ifp, struct sockaddr *sa) 3564 { 3565 struct ifmultiaddr *ifma; 3566 int lastref; 3567 3568 KASSERT(ifp, ("%s: NULL ifp", __func__)); 3569 3570 IF_ADDR_WLOCK(ifp); 3571 lastref = 0; 3572 ifma = if_findmulti(ifp, sa); 3573 if (ifma != NULL) 3574 lastref = if_delmulti_locked(ifp, ifma, 0); 3575 IF_ADDR_WUNLOCK(ifp); 3576 3577 if (ifma == NULL) 3578 return (ENOENT); 3579 3580 if (lastref && ifp->if_ioctl != NULL) { 3581 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 3582 } 3583 3584 return (0); 3585 } 3586 3587 /* 3588 * Delete all multicast group membership for an interface. 3589 * Should be used to quickly flush all multicast filters. 3590 */ 3591 void 3592 if_delallmulti(struct ifnet *ifp) 3593 { 3594 struct ifmultiaddr *ifma; 3595 struct ifmultiaddr *next; 3596 3597 IF_ADDR_WLOCK(ifp); 3598 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) 3599 if_delmulti_locked(ifp, ifma, 0); 3600 IF_ADDR_WUNLOCK(ifp); 3601 } 3602 3603 void 3604 if_delmulti_ifma(struct ifmultiaddr *ifma) 3605 { 3606 if_delmulti_ifma_flags(ifma, 0); 3607 } 3608 3609 /* 3610 * Delete a multicast group membership by group membership pointer. 3611 * Network-layer protocol domains must use this routine. 3612 * 3613 * It is safe to call this routine if the ifp disappeared. 3614 */ 3615 void 3616 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags) 3617 { 3618 struct ifnet *ifp; 3619 int lastref; 3620 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma); 3621 #ifdef INET 3622 IN_MULTI_LIST_UNLOCK_ASSERT(); 3623 #endif 3624 ifp = ifma->ifma_ifp; 3625 #ifdef DIAGNOSTIC 3626 if (ifp == NULL) { 3627 printf("%s: ifma_ifp seems to be detached\n", __func__); 3628 } else { 3629 struct epoch_tracker et; 3630 struct ifnet *oifp; 3631 3632 NET_EPOCH_ENTER(et); 3633 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link) 3634 if (ifp == oifp) 3635 break; 3636 NET_EPOCH_EXIT(et); 3637 if (ifp != oifp) 3638 ifp = NULL; 3639 } 3640 #endif 3641 /* 3642 * If and only if the ifnet instance exists: Acquire the address lock. 3643 */ 3644 if (ifp != NULL) 3645 IF_ADDR_WLOCK(ifp); 3646 3647 lastref = if_delmulti_locked(ifp, ifma, flags); 3648 3649 if (ifp != NULL) { 3650 /* 3651 * If and only if the ifnet instance exists: 3652 * Release the address lock. 3653 * If the group was left: update the hardware hash filter. 3654 */ 3655 IF_ADDR_WUNLOCK(ifp); 3656 if (lastref && ifp->if_ioctl != NULL) { 3657 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 3658 } 3659 } 3660 } 3661 3662 /* 3663 * Perform deletion of network-layer and/or link-layer multicast address. 3664 * 3665 * Return 0 if the reference count was decremented. 3666 * Return 1 if the final reference was released, indicating that the 3667 * hardware hash filter should be reprogrammed. 3668 */ 3669 static int 3670 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching) 3671 { 3672 struct ifmultiaddr *ll_ifma; 3673 3674 if (ifp != NULL && ifma->ifma_ifp != NULL) { 3675 KASSERT(ifma->ifma_ifp == ifp, 3676 ("%s: inconsistent ifp %p", __func__, ifp)); 3677 IF_ADDR_WLOCK_ASSERT(ifp); 3678 } 3679 3680 ifp = ifma->ifma_ifp; 3681 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : ""); 3682 3683 /* 3684 * If the ifnet is detaching, null out references to ifnet, 3685 * so that upper protocol layers will notice, and not attempt 3686 * to obtain locks for an ifnet which no longer exists. The 3687 * routing socket announcement must happen before the ifnet 3688 * instance is detached from the system. 3689 */ 3690 if (detaching) { 3691 #ifdef DIAGNOSTIC 3692 printf("%s: detaching ifnet instance %p\n", __func__, ifp); 3693 #endif 3694 /* 3695 * ifp may already be nulled out if we are being reentered 3696 * to delete the ll_ifma. 3697 */ 3698 if (ifp != NULL) { 3699 rt_newmaddrmsg(RTM_DELMADDR, ifma); 3700 ifma->ifma_ifp = NULL; 3701 } 3702 } 3703 3704 if (--ifma->ifma_refcount > 0) 3705 return 0; 3706 3707 if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) { 3708 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link); 3709 ifma->ifma_flags &= ~IFMA_F_ENQUEUED; 3710 } 3711 /* 3712 * If this ifma is a network-layer ifma, a link-layer ifma may 3713 * have been associated with it. Release it first if so. 3714 */ 3715 ll_ifma = ifma->ifma_llifma; 3716 if (ll_ifma != NULL) { 3717 KASSERT(ifma->ifma_lladdr != NULL, 3718 ("%s: llifma w/o lladdr", __func__)); 3719 if (detaching) 3720 ll_ifma->ifma_ifp = NULL; /* XXX */ 3721 if (--ll_ifma->ifma_refcount == 0) { 3722 if (ifp != NULL) { 3723 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) { 3724 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr, 3725 ifma_link); 3726 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED; 3727 } 3728 } 3729 if_freemulti(ll_ifma); 3730 } 3731 } 3732 #ifdef INVARIANTS 3733 if (ifp) { 3734 struct ifmultiaddr *ifmatmp; 3735 3736 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link) 3737 MPASS(ifma != ifmatmp); 3738 } 3739 #endif 3740 if_freemulti(ifma); 3741 /* 3742 * The last reference to this instance of struct ifmultiaddr 3743 * was released; the hardware should be notified of this change. 3744 */ 3745 return 1; 3746 } 3747 3748 /* 3749 * Set the link layer address on an interface. 3750 * 3751 * At this time we only support certain types of interfaces, 3752 * and we don't allow the length of the address to change. 3753 * 3754 * Set noinline to be dtrace-friendly 3755 */ 3756 __noinline int 3757 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 3758 { 3759 struct sockaddr_dl *sdl; 3760 struct ifaddr *ifa; 3761 struct ifreq ifr; 3762 3763 ifa = ifp->if_addr; 3764 if (ifa == NULL) 3765 return (EINVAL); 3766 3767 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 3768 if (sdl == NULL) 3769 return (EINVAL); 3770 3771 if (len != sdl->sdl_alen) /* don't allow length to change */ 3772 return (EINVAL); 3773 3774 switch (ifp->if_type) { 3775 case IFT_ETHER: 3776 case IFT_XETHER: 3777 case IFT_L2VLAN: 3778 case IFT_BRIDGE: 3779 case IFT_IEEE8023ADLAG: 3780 bcopy(lladdr, LLADDR(sdl), len); 3781 break; 3782 default: 3783 return (ENODEV); 3784 } 3785 3786 /* 3787 * If the interface is already up, we need 3788 * to re-init it in order to reprogram its 3789 * address filter. 3790 */ 3791 if ((ifp->if_flags & IFF_UP) != 0) { 3792 if (ifp->if_ioctl) { 3793 ifp->if_flags &= ~IFF_UP; 3794 ifr.ifr_flags = ifp->if_flags & 0xffff; 3795 ifr.ifr_flagshigh = ifp->if_flags >> 16; 3796 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 3797 ifp->if_flags |= IFF_UP; 3798 ifr.ifr_flags = ifp->if_flags & 0xffff; 3799 ifr.ifr_flagshigh = ifp->if_flags >> 16; 3800 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 3801 } 3802 } 3803 EVENTHANDLER_INVOKE(iflladdr_event, ifp); 3804 3805 return (0); 3806 } 3807 3808 /* 3809 * Compat function for handling basic encapsulation requests. 3810 * Not converted stacks (FDDI, IB, ..) supports traditional 3811 * output model: ARP (and other similar L2 protocols) are handled 3812 * inside output routine, arpresolve/nd6_resolve() returns MAC 3813 * address instead of full prepend. 3814 * 3815 * This function creates calculated header==MAC for IPv4/IPv6 and 3816 * returns EAFNOSUPPORT (which is then handled in ARP code) for other 3817 * address families. 3818 */ 3819 static int 3820 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req) 3821 { 3822 3823 if (req->rtype != IFENCAP_LL) 3824 return (EOPNOTSUPP); 3825 3826 if (req->bufsize < req->lladdr_len) 3827 return (ENOMEM); 3828 3829 switch (req->family) { 3830 case AF_INET: 3831 case AF_INET6: 3832 break; 3833 default: 3834 return (EAFNOSUPPORT); 3835 } 3836 3837 /* Copy lladdr to storage as is */ 3838 memmove(req->buf, req->lladdr, req->lladdr_len); 3839 req->bufsize = req->lladdr_len; 3840 req->lladdr_off = 0; 3841 3842 return (0); 3843 } 3844 3845 /* 3846 * Tunnel interfaces can nest, also they may cause infinite recursion 3847 * calls when misconfigured. We'll prevent this by detecting loops. 3848 * High nesting level may cause stack exhaustion. We'll prevent this 3849 * by introducing upper limit. 3850 * 3851 * Return 0, if tunnel nesting count is equal or less than limit. 3852 */ 3853 int 3854 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie, 3855 int limit) 3856 { 3857 struct m_tag *mtag; 3858 int count; 3859 3860 count = 1; 3861 mtag = NULL; 3862 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) { 3863 if (*(struct ifnet **)(mtag + 1) == ifp) { 3864 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp)); 3865 return (EIO); 3866 } 3867 count++; 3868 } 3869 if (count > limit) { 3870 log(LOG_NOTICE, 3871 "%s: if_output recursively called too many times(%d)\n", 3872 if_name(ifp), count); 3873 return (EIO); 3874 } 3875 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT); 3876 if (mtag == NULL) 3877 return (ENOMEM); 3878 *(struct ifnet **)(mtag + 1) = ifp; 3879 m_tag_prepend(m, mtag); 3880 return (0); 3881 } 3882 3883 /* 3884 * Get the link layer address that was read from the hardware at attach. 3885 * 3886 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type 3887 * their component interfaces as IFT_IEEE8023ADLAG. 3888 */ 3889 int 3890 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr) 3891 { 3892 3893 if (ifp->if_hw_addr == NULL) 3894 return (ENODEV); 3895 3896 switch (ifp->if_type) { 3897 case IFT_ETHER: 3898 case IFT_IEEE8023ADLAG: 3899 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen); 3900 return (0); 3901 default: 3902 return (ENODEV); 3903 } 3904 } 3905 3906 /* 3907 * The name argument must be a pointer to storage which will last as 3908 * long as the interface does. For physical devices, the result of 3909 * device_get_name(dev) is a good choice and for pseudo-devices a 3910 * static string works well. 3911 */ 3912 void 3913 if_initname(struct ifnet *ifp, const char *name, int unit) 3914 { 3915 ifp->if_dname = name; 3916 ifp->if_dunit = unit; 3917 if (unit != IF_DUNIT_NONE) 3918 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit); 3919 else 3920 strlcpy(ifp->if_xname, name, IFNAMSIZ); 3921 } 3922 3923 static int 3924 if_vlog(struct ifnet *ifp, int pri, const char *fmt, va_list ap) 3925 { 3926 char if_fmt[256]; 3927 3928 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt); 3929 vlog(pri, if_fmt, ap); 3930 return (0); 3931 } 3932 3933 3934 int 3935 if_printf(struct ifnet *ifp, const char *fmt, ...) 3936 { 3937 va_list ap; 3938 3939 va_start(ap, fmt); 3940 if_vlog(ifp, LOG_INFO, fmt, ap); 3941 va_end(ap); 3942 return (0); 3943 } 3944 3945 int 3946 if_log(struct ifnet *ifp, int pri, const char *fmt, ...) 3947 { 3948 va_list ap; 3949 3950 va_start(ap, fmt); 3951 if_vlog(ifp, pri, fmt, ap); 3952 va_end(ap); 3953 return (0); 3954 } 3955 3956 void 3957 if_start(struct ifnet *ifp) 3958 { 3959 3960 (*(ifp)->if_start)(ifp); 3961 } 3962 3963 /* 3964 * Backwards compatibility interface for drivers 3965 * that have not implemented it 3966 */ 3967 static int 3968 if_transmit(struct ifnet *ifp, struct mbuf *m) 3969 { 3970 int error; 3971 3972 IFQ_HANDOFF(ifp, m, error); 3973 return (error); 3974 } 3975 3976 static void 3977 if_input_default(struct ifnet *ifp __unused, struct mbuf *m) 3978 { 3979 3980 m_freem(m); 3981 } 3982 3983 int 3984 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust) 3985 { 3986 int active = 0; 3987 3988 IF_LOCK(ifq); 3989 if (_IF_QFULL(ifq)) { 3990 IF_UNLOCK(ifq); 3991 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1); 3992 m_freem(m); 3993 return (0); 3994 } 3995 if (ifp != NULL) { 3996 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust); 3997 if (m->m_flags & (M_BCAST|M_MCAST)) 3998 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1); 3999 active = ifp->if_drv_flags & IFF_DRV_OACTIVE; 4000 } 4001 _IF_ENQUEUE(ifq, m); 4002 IF_UNLOCK(ifq); 4003 if (ifp != NULL && !active) 4004 (*(ifp)->if_start)(ifp); 4005 return (1); 4006 } 4007 4008 void 4009 if_register_com_alloc(u_char type, 4010 if_com_alloc_t *a, if_com_free_t *f) 4011 { 4012 4013 KASSERT(if_com_alloc[type] == NULL, 4014 ("if_register_com_alloc: %d already registered", type)); 4015 KASSERT(if_com_free[type] == NULL, 4016 ("if_register_com_alloc: %d free already registered", type)); 4017 4018 if_com_alloc[type] = a; 4019 if_com_free[type] = f; 4020 } 4021 4022 void 4023 if_deregister_com_alloc(u_char type) 4024 { 4025 4026 KASSERT(if_com_alloc[type] != NULL, 4027 ("if_deregister_com_alloc: %d not registered", type)); 4028 KASSERT(if_com_free[type] != NULL, 4029 ("if_deregister_com_alloc: %d free not registered", type)); 4030 4031 /* 4032 * Ensure all pending EPOCH(9) callbacks have been executed. This 4033 * fixes issues about late invocation of if_destroy(), which leads 4034 * to memory leak from if_com_alloc[type] allocated if_l2com. 4035 */ 4036 epoch_drain_callbacks(net_epoch_preempt); 4037 4038 if_com_alloc[type] = NULL; 4039 if_com_free[type] = NULL; 4040 } 4041 4042 /* API for driver access to network stack owned ifnet.*/ 4043 uint64_t 4044 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate) 4045 { 4046 uint64_t oldbrate; 4047 4048 oldbrate = ifp->if_baudrate; 4049 ifp->if_baudrate = baudrate; 4050 return (oldbrate); 4051 } 4052 4053 uint64_t 4054 if_getbaudrate(if_t ifp) 4055 { 4056 4057 return (((struct ifnet *)ifp)->if_baudrate); 4058 } 4059 4060 int 4061 if_setcapabilities(if_t ifp, int capabilities) 4062 { 4063 ((struct ifnet *)ifp)->if_capabilities = capabilities; 4064 return (0); 4065 } 4066 4067 int 4068 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit) 4069 { 4070 ((struct ifnet *)ifp)->if_capabilities |= setbit; 4071 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit; 4072 4073 return (0); 4074 } 4075 4076 int 4077 if_getcapabilities(if_t ifp) 4078 { 4079 return ((struct ifnet *)ifp)->if_capabilities; 4080 } 4081 4082 int 4083 if_setcapenable(if_t ifp, int capabilities) 4084 { 4085 ((struct ifnet *)ifp)->if_capenable = capabilities; 4086 return (0); 4087 } 4088 4089 int 4090 if_setcapenablebit(if_t ifp, int setcap, int clearcap) 4091 { 4092 if(setcap) 4093 ((struct ifnet *)ifp)->if_capenable |= setcap; 4094 if(clearcap) 4095 ((struct ifnet *)ifp)->if_capenable &= ~clearcap; 4096 4097 return (0); 4098 } 4099 4100 const char * 4101 if_getdname(if_t ifp) 4102 { 4103 return ((struct ifnet *)ifp)->if_dname; 4104 } 4105 4106 int 4107 if_togglecapenable(if_t ifp, int togglecap) 4108 { 4109 ((struct ifnet *)ifp)->if_capenable ^= togglecap; 4110 return (0); 4111 } 4112 4113 int 4114 if_getcapenable(if_t ifp) 4115 { 4116 return ((struct ifnet *)ifp)->if_capenable; 4117 } 4118 4119 /* 4120 * This is largely undesirable because it ties ifnet to a device, but does 4121 * provide flexiblity for an embedded product vendor. Should be used with 4122 * the understanding that it violates the interface boundaries, and should be 4123 * a last resort only. 4124 */ 4125 int 4126 if_setdev(if_t ifp, void *dev) 4127 { 4128 return (0); 4129 } 4130 4131 int 4132 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags) 4133 { 4134 ((struct ifnet *)ifp)->if_drv_flags |= set_flags; 4135 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags; 4136 4137 return (0); 4138 } 4139 4140 int 4141 if_getdrvflags(if_t ifp) 4142 { 4143 return ((struct ifnet *)ifp)->if_drv_flags; 4144 } 4145 4146 int 4147 if_setdrvflags(if_t ifp, int flags) 4148 { 4149 ((struct ifnet *)ifp)->if_drv_flags = flags; 4150 return (0); 4151 } 4152 4153 int 4154 if_setflags(if_t ifp, int flags) 4155 { 4156 4157 ifp->if_flags = flags; 4158 return (0); 4159 } 4160 4161 int 4162 if_setflagbits(if_t ifp, int set, int clear) 4163 { 4164 ((struct ifnet *)ifp)->if_flags |= set; 4165 ((struct ifnet *)ifp)->if_flags &= ~clear; 4166 4167 return (0); 4168 } 4169 4170 int 4171 if_getflags(if_t ifp) 4172 { 4173 return ((struct ifnet *)ifp)->if_flags; 4174 } 4175 4176 int 4177 if_clearhwassist(if_t ifp) 4178 { 4179 ((struct ifnet *)ifp)->if_hwassist = 0; 4180 return (0); 4181 } 4182 4183 int 4184 if_sethwassistbits(if_t ifp, int toset, int toclear) 4185 { 4186 ((struct ifnet *)ifp)->if_hwassist |= toset; 4187 ((struct ifnet *)ifp)->if_hwassist &= ~toclear; 4188 4189 return (0); 4190 } 4191 4192 int 4193 if_sethwassist(if_t ifp, int hwassist_bit) 4194 { 4195 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit; 4196 return (0); 4197 } 4198 4199 int 4200 if_gethwassist(if_t ifp) 4201 { 4202 return ((struct ifnet *)ifp)->if_hwassist; 4203 } 4204 4205 int 4206 if_setmtu(if_t ifp, int mtu) 4207 { 4208 ((struct ifnet *)ifp)->if_mtu = mtu; 4209 return (0); 4210 } 4211 4212 int 4213 if_getmtu(if_t ifp) 4214 { 4215 return ((struct ifnet *)ifp)->if_mtu; 4216 } 4217 4218 int 4219 if_getmtu_family(if_t ifp, int family) 4220 { 4221 struct domain *dp; 4222 4223 for (dp = domains; dp; dp = dp->dom_next) { 4224 if (dp->dom_family == family && dp->dom_ifmtu != NULL) 4225 return (dp->dom_ifmtu((struct ifnet *)ifp)); 4226 } 4227 4228 return (((struct ifnet *)ifp)->if_mtu); 4229 } 4230 4231 /* 4232 * Methods for drivers to access interface unicast and multicast 4233 * link level addresses. Driver shall not know 'struct ifaddr' neither 4234 * 'struct ifmultiaddr'. 4235 */ 4236 u_int 4237 if_lladdr_count(if_t ifp) 4238 { 4239 struct epoch_tracker et; 4240 struct ifaddr *ifa; 4241 u_int count; 4242 4243 count = 0; 4244 NET_EPOCH_ENTER(et); 4245 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 4246 if (ifa->ifa_addr->sa_family == AF_LINK) 4247 count++; 4248 NET_EPOCH_EXIT(et); 4249 4250 return (count); 4251 } 4252 4253 u_int 4254 if_foreach_lladdr(if_t ifp, iflladdr_cb_t cb, void *cb_arg) 4255 { 4256 struct epoch_tracker et; 4257 struct ifaddr *ifa; 4258 u_int count; 4259 4260 MPASS(cb); 4261 4262 count = 0; 4263 NET_EPOCH_ENTER(et); 4264 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 4265 if (ifa->ifa_addr->sa_family != AF_LINK) 4266 continue; 4267 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifa->ifa_addr, 4268 count); 4269 } 4270 NET_EPOCH_EXIT(et); 4271 4272 return (count); 4273 } 4274 4275 u_int 4276 if_llmaddr_count(if_t ifp) 4277 { 4278 struct epoch_tracker et; 4279 struct ifmultiaddr *ifma; 4280 int count; 4281 4282 count = 0; 4283 NET_EPOCH_ENTER(et); 4284 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 4285 if (ifma->ifma_addr->sa_family == AF_LINK) 4286 count++; 4287 NET_EPOCH_EXIT(et); 4288 4289 return (count); 4290 } 4291 4292 u_int 4293 if_foreach_llmaddr(if_t ifp, iflladdr_cb_t cb, void *cb_arg) 4294 { 4295 struct epoch_tracker et; 4296 struct ifmultiaddr *ifma; 4297 u_int count; 4298 4299 MPASS(cb); 4300 4301 count = 0; 4302 NET_EPOCH_ENTER(et); 4303 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 4304 if (ifma->ifma_addr->sa_family != AF_LINK) 4305 continue; 4306 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifma->ifma_addr, 4307 count); 4308 } 4309 NET_EPOCH_EXIT(et); 4310 4311 return (count); 4312 } 4313 4314 int 4315 if_setsoftc(if_t ifp, void *softc) 4316 { 4317 ((struct ifnet *)ifp)->if_softc = softc; 4318 return (0); 4319 } 4320 4321 void * 4322 if_getsoftc(if_t ifp) 4323 { 4324 return ((struct ifnet *)ifp)->if_softc; 4325 } 4326 4327 void 4328 if_setrcvif(struct mbuf *m, if_t ifp) 4329 { 4330 4331 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0); 4332 m->m_pkthdr.rcvif = (struct ifnet *)ifp; 4333 } 4334 4335 void 4336 if_setvtag(struct mbuf *m, uint16_t tag) 4337 { 4338 m->m_pkthdr.ether_vtag = tag; 4339 } 4340 4341 uint16_t 4342 if_getvtag(struct mbuf *m) 4343 { 4344 4345 return (m->m_pkthdr.ether_vtag); 4346 } 4347 4348 int 4349 if_sendq_empty(if_t ifp) 4350 { 4351 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd); 4352 } 4353 4354 struct ifaddr * 4355 if_getifaddr(if_t ifp) 4356 { 4357 return ((struct ifnet *)ifp)->if_addr; 4358 } 4359 4360 int 4361 if_getamcount(if_t ifp) 4362 { 4363 return ((struct ifnet *)ifp)->if_amcount; 4364 } 4365 4366 int 4367 if_setsendqready(if_t ifp) 4368 { 4369 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd); 4370 return (0); 4371 } 4372 4373 int 4374 if_setsendqlen(if_t ifp, int tx_desc_count) 4375 { 4376 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count); 4377 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count; 4378 4379 return (0); 4380 } 4381 4382 int 4383 if_vlantrunkinuse(if_t ifp) 4384 { 4385 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0; 4386 } 4387 4388 int 4389 if_input(if_t ifp, struct mbuf* sendmp) 4390 { 4391 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp); 4392 return (0); 4393 4394 } 4395 4396 struct mbuf * 4397 if_dequeue(if_t ifp) 4398 { 4399 struct mbuf *m; 4400 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m); 4401 4402 return (m); 4403 } 4404 4405 int 4406 if_sendq_prepend(if_t ifp, struct mbuf *m) 4407 { 4408 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m); 4409 return (0); 4410 } 4411 4412 int 4413 if_setifheaderlen(if_t ifp, int len) 4414 { 4415 ((struct ifnet *)ifp)->if_hdrlen = len; 4416 return (0); 4417 } 4418 4419 caddr_t 4420 if_getlladdr(if_t ifp) 4421 { 4422 return (IF_LLADDR((struct ifnet *)ifp)); 4423 } 4424 4425 void * 4426 if_gethandle(u_char type) 4427 { 4428 return (if_alloc(type)); 4429 } 4430 4431 void 4432 if_bpfmtap(if_t ifh, struct mbuf *m) 4433 { 4434 struct ifnet *ifp = (struct ifnet *)ifh; 4435 4436 BPF_MTAP(ifp, m); 4437 } 4438 4439 void 4440 if_etherbpfmtap(if_t ifh, struct mbuf *m) 4441 { 4442 struct ifnet *ifp = (struct ifnet *)ifh; 4443 4444 ETHER_BPF_MTAP(ifp, m); 4445 } 4446 4447 void 4448 if_vlancap(if_t ifh) 4449 { 4450 struct ifnet *ifp = (struct ifnet *)ifh; 4451 VLAN_CAPABILITIES(ifp); 4452 } 4453 4454 int 4455 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax) 4456 { 4457 4458 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax; 4459 return (0); 4460 } 4461 4462 int 4463 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount) 4464 { 4465 4466 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount; 4467 return (0); 4468 } 4469 4470 int 4471 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize) 4472 { 4473 4474 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize; 4475 return (0); 4476 } 4477 4478 u_int 4479 if_gethwtsomax(if_t ifp) 4480 { 4481 4482 return (((struct ifnet *)ifp)->if_hw_tsomax); 4483 } 4484 4485 u_int 4486 if_gethwtsomaxsegcount(if_t ifp) 4487 { 4488 4489 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount); 4490 } 4491 4492 u_int 4493 if_gethwtsomaxsegsize(if_t ifp) 4494 { 4495 4496 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize); 4497 } 4498 4499 void 4500 if_setinitfn(if_t ifp, void (*init_fn)(void *)) 4501 { 4502 ((struct ifnet *)ifp)->if_init = init_fn; 4503 } 4504 4505 void 4506 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t)) 4507 { 4508 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn; 4509 } 4510 4511 void 4512 if_setstartfn(if_t ifp, void (*start_fn)(if_t)) 4513 { 4514 ((struct ifnet *)ifp)->if_start = (void *)start_fn; 4515 } 4516 4517 void 4518 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn) 4519 { 4520 ((struct ifnet *)ifp)->if_transmit = start_fn; 4521 } 4522 4523 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn) 4524 { 4525 ((struct ifnet *)ifp)->if_qflush = flush_fn; 4526 4527 } 4528 4529 void 4530 if_setgetcounterfn(if_t ifp, if_get_counter_t fn) 4531 { 4532 4533 ifp->if_get_counter = fn; 4534 } 4535 4536 #ifdef DDB 4537 static void 4538 if_show_ifnet(struct ifnet *ifp) 4539 { 4540 4541 if (ifp == NULL) 4542 return; 4543 db_printf("%s:\n", ifp->if_xname); 4544 #define IF_DB_PRINTF(f, e) db_printf(" %s = " f "\n", #e, ifp->e); 4545 IF_DB_PRINTF("%s", if_dname); 4546 IF_DB_PRINTF("%d", if_dunit); 4547 IF_DB_PRINTF("%s", if_description); 4548 IF_DB_PRINTF("%u", if_index); 4549 IF_DB_PRINTF("%u", if_refcount); 4550 IF_DB_PRINTF("%d", if_index_reserved); 4551 IF_DB_PRINTF("%p", if_softc); 4552 IF_DB_PRINTF("%p", if_l2com); 4553 IF_DB_PRINTF("%p", if_llsoftc); 4554 IF_DB_PRINTF("%d", if_amcount); 4555 IF_DB_PRINTF("%p", if_addr); 4556 IF_DB_PRINTF("%p", if_broadcastaddr); 4557 IF_DB_PRINTF("%p", if_afdata); 4558 IF_DB_PRINTF("%d", if_afdata_initialized); 4559 IF_DB_PRINTF("%u", if_fib); 4560 IF_DB_PRINTF("%p", if_vnet); 4561 IF_DB_PRINTF("%p", if_home_vnet); 4562 IF_DB_PRINTF("%p", if_vlantrunk); 4563 IF_DB_PRINTF("%p", if_bpf); 4564 IF_DB_PRINTF("%u", if_pcount); 4565 IF_DB_PRINTF("%p", if_bridge); 4566 IF_DB_PRINTF("%p", if_lagg); 4567 IF_DB_PRINTF("%p", if_pf_kif); 4568 IF_DB_PRINTF("%p", if_carp); 4569 IF_DB_PRINTF("%p", if_label); 4570 IF_DB_PRINTF("%p", if_netmap); 4571 IF_DB_PRINTF("0x%08x", if_flags); 4572 IF_DB_PRINTF("0x%08x", if_drv_flags); 4573 IF_DB_PRINTF("0x%08x", if_capabilities); 4574 IF_DB_PRINTF("0x%08x", if_capenable); 4575 IF_DB_PRINTF("%p", if_snd.ifq_head); 4576 IF_DB_PRINTF("%p", if_snd.ifq_tail); 4577 IF_DB_PRINTF("%d", if_snd.ifq_len); 4578 IF_DB_PRINTF("%d", if_snd.ifq_maxlen); 4579 IF_DB_PRINTF("%p", if_snd.ifq_drv_head); 4580 IF_DB_PRINTF("%p", if_snd.ifq_drv_tail); 4581 IF_DB_PRINTF("%d", if_snd.ifq_drv_len); 4582 IF_DB_PRINTF("%d", if_snd.ifq_drv_maxlen); 4583 IF_DB_PRINTF("%d", if_snd.altq_type); 4584 IF_DB_PRINTF("%x", if_snd.altq_flags); 4585 #undef IF_DB_PRINTF 4586 } 4587 4588 DB_SHOW_COMMAND(ifnet, db_show_ifnet) 4589 { 4590 4591 if (!have_addr) { 4592 db_printf("usage: show ifnet <struct ifnet *>\n"); 4593 return; 4594 } 4595 4596 if_show_ifnet((struct ifnet *)addr); 4597 } 4598 4599 DB_SHOW_ALL_COMMAND(ifnets, db_show_all_ifnets) 4600 { 4601 VNET_ITERATOR_DECL(vnet_iter); 4602 struct ifnet *ifp; 4603 u_short idx; 4604 4605 VNET_FOREACH(vnet_iter) { 4606 CURVNET_SET_QUIET(vnet_iter); 4607 #ifdef VIMAGE 4608 db_printf("vnet=%p\n", curvnet); 4609 #endif 4610 for (idx = 1; idx <= V_if_index; idx++) { 4611 ifp = V_ifindex_table[idx]; 4612 if (ifp == NULL) 4613 continue; 4614 db_printf( "%20s ifp=%p\n", ifp->if_xname, ifp); 4615 if (db_pager_quit) 4616 break; 4617 } 4618 CURVNET_RESTORE(); 4619 } 4620 } 4621 #endif /* DDB */ 4622