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