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