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