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_usrreqs->pru_control)(so, cmd, data, 3167 ifp, td)); 3168 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL && 3169 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR && 3170 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK) 3171 error = (*ifp->if_ioctl)(ifp, cmd, data); 3172 3173 if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 3174 #ifdef INET6 3175 if (ifp->if_flags & IFF_UP) 3176 in6_if_up(ifp); 3177 #endif 3178 } 3179 3180 out_ref: 3181 if_rele(ifp); 3182 out_noref: 3183 CURVNET_RESTORE(); 3184 #ifdef COMPAT_FREEBSD32 3185 if (error != 0) 3186 return (error); 3187 switch (saved_cmd) { 3188 case SIOCGIFCONF32: 3189 ifc32->ifc_len = thunk.ifc.ifc_len; 3190 break; 3191 case SIOCGDRVSPEC32: 3192 /* 3193 * SIOCGDRVSPEC is IOWR, but nothing actually touches 3194 * the struct so just assert that ifd_len (the only 3195 * field it might make sense to update) hasn't 3196 * changed. 3197 */ 3198 KASSERT(thunk.ifd.ifd_len == ifd32->ifd_len, 3199 ("ifd_len was updated %u -> %zu", ifd32->ifd_len, 3200 thunk.ifd.ifd_len)); 3201 break; 3202 case SIOCGIFGROUP32: 3203 case SIOCGIFGMEMB32: 3204 ifgr32->ifgr_len = thunk.ifgr.ifgr_len; 3205 break; 3206 case SIOCGIFMEDIA32: 3207 case SIOCGIFXMEDIA32: 3208 ifmr32->ifm_current = thunk.ifmr.ifm_current; 3209 ifmr32->ifm_mask = thunk.ifmr.ifm_mask; 3210 ifmr32->ifm_status = thunk.ifmr.ifm_status; 3211 ifmr32->ifm_active = thunk.ifmr.ifm_active; 3212 ifmr32->ifm_count = thunk.ifmr.ifm_count; 3213 break; 3214 } 3215 #endif 3216 return (error); 3217 } 3218 3219 /* 3220 * The code common to handling reference counted flags, 3221 * e.g., in ifpromisc() and if_allmulti(). 3222 * The "pflag" argument can specify a permanent mode flag to check, 3223 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none. 3224 * 3225 * Only to be used on stack-owned flags, not driver-owned flags. 3226 */ 3227 static int 3228 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch) 3229 { 3230 struct ifreq ifr; 3231 int error; 3232 int oldflags, oldcount; 3233 3234 /* Sanity checks to catch programming errors */ 3235 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0, 3236 ("%s: setting driver-owned flag %d", __func__, flag)); 3237 3238 if (onswitch) 3239 KASSERT(*refcount >= 0, 3240 ("%s: increment negative refcount %d for flag %d", 3241 __func__, *refcount, flag)); 3242 else 3243 KASSERT(*refcount > 0, 3244 ("%s: decrement non-positive refcount %d for flag %d", 3245 __func__, *refcount, flag)); 3246 3247 /* In case this mode is permanent, just touch refcount */ 3248 if (ifp->if_flags & pflag) { 3249 *refcount += onswitch ? 1 : -1; 3250 return (0); 3251 } 3252 3253 /* Save ifnet parameters for if_ioctl() may fail */ 3254 oldcount = *refcount; 3255 oldflags = ifp->if_flags; 3256 3257 /* 3258 * See if we aren't the only and touching refcount is enough. 3259 * Actually toggle interface flag if we are the first or last. 3260 */ 3261 if (onswitch) { 3262 if ((*refcount)++) 3263 return (0); 3264 ifp->if_flags |= flag; 3265 } else { 3266 if (--(*refcount)) 3267 return (0); 3268 ifp->if_flags &= ~flag; 3269 } 3270 3271 /* Call down the driver since we've changed interface flags */ 3272 if (ifp->if_ioctl == NULL) { 3273 error = EOPNOTSUPP; 3274 goto recover; 3275 } 3276 ifr.ifr_flags = ifp->if_flags & 0xffff; 3277 ifr.ifr_flagshigh = ifp->if_flags >> 16; 3278 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 3279 if (error) 3280 goto recover; 3281 /* Notify userland that interface flags have changed */ 3282 rt_ifmsg(ifp); 3283 return (0); 3284 3285 recover: 3286 /* Recover after driver error */ 3287 *refcount = oldcount; 3288 ifp->if_flags = oldflags; 3289 return (error); 3290 } 3291 3292 /* 3293 * Set/clear promiscuous mode on interface ifp based on the truth value 3294 * of pswitch. The calls are reference counted so that only the first 3295 * "on" request actually has an effect, as does the final "off" request. 3296 * Results are undefined if the "off" and "on" requests are not matched. 3297 */ 3298 int 3299 ifpromisc(struct ifnet *ifp, int pswitch) 3300 { 3301 int error; 3302 int oldflags = ifp->if_flags; 3303 3304 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC, 3305 &ifp->if_pcount, pswitch); 3306 /* If promiscuous mode status has changed, log a message */ 3307 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC) && 3308 log_promisc_mode_change) 3309 if_printf(ifp, "promiscuous mode %s\n", 3310 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); 3311 return (error); 3312 } 3313 3314 /* 3315 * Return interface configuration 3316 * of system. List may be used 3317 * in later ioctl's (above) to get 3318 * other information. 3319 */ 3320 /*ARGSUSED*/ 3321 static int 3322 ifconf(u_long cmd, caddr_t data) 3323 { 3324 struct ifconf *ifc = (struct ifconf *)data; 3325 struct ifnet *ifp; 3326 struct ifaddr *ifa; 3327 struct ifreq ifr; 3328 struct sbuf *sb; 3329 int error, full = 0, valid_len, max_len; 3330 3331 /* Limit initial buffer size to maxphys to avoid DoS from userspace. */ 3332 max_len = maxphys - 1; 3333 3334 /* Prevent hostile input from being able to crash the system */ 3335 if (ifc->ifc_len <= 0) 3336 return (EINVAL); 3337 3338 again: 3339 if (ifc->ifc_len <= max_len) { 3340 max_len = ifc->ifc_len; 3341 full = 1; 3342 } 3343 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN); 3344 max_len = 0; 3345 valid_len = 0; 3346 3347 IFNET_RLOCK(); 3348 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 3349 struct epoch_tracker et; 3350 int addrs; 3351 3352 /* 3353 * Zero the ifr to make sure we don't disclose the contents 3354 * of the stack. 3355 */ 3356 memset(&ifr, 0, sizeof(ifr)); 3357 3358 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)) 3359 >= sizeof(ifr.ifr_name)) { 3360 sbuf_delete(sb); 3361 IFNET_RUNLOCK(); 3362 return (ENAMETOOLONG); 3363 } 3364 3365 addrs = 0; 3366 NET_EPOCH_ENTER(et); 3367 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 3368 struct sockaddr *sa = ifa->ifa_addr; 3369 3370 if (prison_if(curthread->td_ucred, sa) != 0) 3371 continue; 3372 addrs++; 3373 if (sa->sa_len <= sizeof(*sa)) { 3374 if (sa->sa_len < sizeof(*sa)) { 3375 memset(&ifr.ifr_ifru.ifru_addr, 0, 3376 sizeof(ifr.ifr_ifru.ifru_addr)); 3377 memcpy(&ifr.ifr_ifru.ifru_addr, sa, 3378 sa->sa_len); 3379 } else 3380 ifr.ifr_ifru.ifru_addr = *sa; 3381 sbuf_bcat(sb, &ifr, sizeof(ifr)); 3382 max_len += sizeof(ifr); 3383 } else { 3384 sbuf_bcat(sb, &ifr, 3385 offsetof(struct ifreq, ifr_addr)); 3386 max_len += offsetof(struct ifreq, ifr_addr); 3387 sbuf_bcat(sb, sa, sa->sa_len); 3388 max_len += sa->sa_len; 3389 } 3390 3391 if (sbuf_error(sb) == 0) 3392 valid_len = sbuf_len(sb); 3393 } 3394 NET_EPOCH_EXIT(et); 3395 if (addrs == 0) { 3396 sbuf_bcat(sb, &ifr, sizeof(ifr)); 3397 max_len += sizeof(ifr); 3398 3399 if (sbuf_error(sb) == 0) 3400 valid_len = sbuf_len(sb); 3401 } 3402 } 3403 IFNET_RUNLOCK(); 3404 3405 /* 3406 * If we didn't allocate enough space (uncommon), try again. If 3407 * we have already allocated as much space as we are allowed, 3408 * return what we've got. 3409 */ 3410 if (valid_len != max_len && !full) { 3411 sbuf_delete(sb); 3412 goto again; 3413 } 3414 3415 ifc->ifc_len = valid_len; 3416 sbuf_finish(sb); 3417 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len); 3418 sbuf_delete(sb); 3419 return (error); 3420 } 3421 3422 /* 3423 * Just like ifpromisc(), but for all-multicast-reception mode. 3424 */ 3425 int 3426 if_allmulti(struct ifnet *ifp, int onswitch) 3427 { 3428 3429 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch)); 3430 } 3431 3432 struct ifmultiaddr * 3433 if_findmulti(struct ifnet *ifp, const struct sockaddr *sa) 3434 { 3435 struct ifmultiaddr *ifma; 3436 3437 IF_ADDR_LOCK_ASSERT(ifp); 3438 3439 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 3440 if (sa->sa_family == AF_LINK) { 3441 if (sa_dl_equal(ifma->ifma_addr, sa)) 3442 break; 3443 } else { 3444 if (sa_equal(ifma->ifma_addr, sa)) 3445 break; 3446 } 3447 } 3448 3449 return ifma; 3450 } 3451 3452 /* 3453 * Allocate a new ifmultiaddr and initialize based on passed arguments. We 3454 * make copies of passed sockaddrs. The ifmultiaddr will not be added to 3455 * the ifnet multicast address list here, so the caller must do that and 3456 * other setup work (such as notifying the device driver). The reference 3457 * count is initialized to 1. 3458 */ 3459 static struct ifmultiaddr * 3460 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa, 3461 int mflags) 3462 { 3463 struct ifmultiaddr *ifma; 3464 struct sockaddr *dupsa; 3465 3466 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags | 3467 M_ZERO); 3468 if (ifma == NULL) 3469 return (NULL); 3470 3471 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags); 3472 if (dupsa == NULL) { 3473 free(ifma, M_IFMADDR); 3474 return (NULL); 3475 } 3476 bcopy(sa, dupsa, sa->sa_len); 3477 ifma->ifma_addr = dupsa; 3478 3479 ifma->ifma_ifp = ifp; 3480 ifma->ifma_refcount = 1; 3481 ifma->ifma_protospec = NULL; 3482 3483 if (llsa == NULL) { 3484 ifma->ifma_lladdr = NULL; 3485 return (ifma); 3486 } 3487 3488 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags); 3489 if (dupsa == NULL) { 3490 free(ifma->ifma_addr, M_IFMADDR); 3491 free(ifma, M_IFMADDR); 3492 return (NULL); 3493 } 3494 bcopy(llsa, dupsa, llsa->sa_len); 3495 ifma->ifma_lladdr = dupsa; 3496 3497 return (ifma); 3498 } 3499 3500 /* 3501 * if_freemulti: free ifmultiaddr structure and possibly attached related 3502 * addresses. The caller is responsible for implementing reference 3503 * counting, notifying the driver, handling routing messages, and releasing 3504 * any dependent link layer state. 3505 */ 3506 #ifdef MCAST_VERBOSE 3507 extern void kdb_backtrace(void); 3508 #endif 3509 static void 3510 if_freemulti_internal(struct ifmultiaddr *ifma) 3511 { 3512 3513 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d", 3514 ifma->ifma_refcount)); 3515 3516 if (ifma->ifma_lladdr != NULL) 3517 free(ifma->ifma_lladdr, M_IFMADDR); 3518 #ifdef MCAST_VERBOSE 3519 kdb_backtrace(); 3520 printf("%s freeing ifma: %p\n", __func__, ifma); 3521 #endif 3522 free(ifma->ifma_addr, M_IFMADDR); 3523 free(ifma, M_IFMADDR); 3524 } 3525 3526 static void 3527 if_destroymulti(epoch_context_t ctx) 3528 { 3529 struct ifmultiaddr *ifma; 3530 3531 ifma = __containerof(ctx, struct ifmultiaddr, ifma_epoch_ctx); 3532 if_freemulti_internal(ifma); 3533 } 3534 3535 void 3536 if_freemulti(struct ifmultiaddr *ifma) 3537 { 3538 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti_epoch: refcount %d", 3539 ifma->ifma_refcount)); 3540 3541 NET_EPOCH_CALL(if_destroymulti, &ifma->ifma_epoch_ctx); 3542 } 3543 3544 /* 3545 * Register an additional multicast address with a network interface. 3546 * 3547 * - If the address is already present, bump the reference count on the 3548 * address and return. 3549 * - If the address is not link-layer, look up a link layer address. 3550 * - Allocate address structures for one or both addresses, and attach to the 3551 * multicast address list on the interface. If automatically adding a link 3552 * layer address, the protocol address will own a reference to the link 3553 * layer address, to be freed when it is freed. 3554 * - Notify the network device driver of an addition to the multicast address 3555 * list. 3556 * 3557 * 'sa' points to caller-owned memory with the desired multicast address. 3558 * 3559 * 'retifma' will be used to return a pointer to the resulting multicast 3560 * address reference, if desired. 3561 */ 3562 int 3563 if_addmulti(struct ifnet *ifp, struct sockaddr *sa, 3564 struct ifmultiaddr **retifma) 3565 { 3566 struct ifmultiaddr *ifma, *ll_ifma; 3567 struct sockaddr *llsa; 3568 struct sockaddr_dl sdl; 3569 int error; 3570 3571 #ifdef INET 3572 IN_MULTI_LIST_UNLOCK_ASSERT(); 3573 #endif 3574 #ifdef INET6 3575 IN6_MULTI_LIST_UNLOCK_ASSERT(); 3576 #endif 3577 /* 3578 * If the address is already present, return a new reference to it; 3579 * otherwise, allocate storage and set up a new address. 3580 */ 3581 IF_ADDR_WLOCK(ifp); 3582 ifma = if_findmulti(ifp, sa); 3583 if (ifma != NULL) { 3584 ifma->ifma_refcount++; 3585 if (retifma != NULL) 3586 *retifma = ifma; 3587 IF_ADDR_WUNLOCK(ifp); 3588 return (0); 3589 } 3590 3591 /* 3592 * The address isn't already present; resolve the protocol address 3593 * into a link layer address, and then look that up, bump its 3594 * refcount or allocate an ifma for that also. 3595 * Most link layer resolving functions returns address data which 3596 * fits inside default sockaddr_dl structure. However callback 3597 * can allocate another sockaddr structure, in that case we need to 3598 * free it later. 3599 */ 3600 llsa = NULL; 3601 ll_ifma = NULL; 3602 if (ifp->if_resolvemulti != NULL) { 3603 /* Provide called function with buffer size information */ 3604 sdl.sdl_len = sizeof(sdl); 3605 llsa = (struct sockaddr *)&sdl; 3606 error = ifp->if_resolvemulti(ifp, &llsa, sa); 3607 if (error) 3608 goto unlock_out; 3609 } 3610 3611 /* 3612 * Allocate the new address. Don't hook it up yet, as we may also 3613 * need to allocate a link layer multicast address. 3614 */ 3615 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT); 3616 if (ifma == NULL) { 3617 error = ENOMEM; 3618 goto free_llsa_out; 3619 } 3620 3621 /* 3622 * If a link layer address is found, we'll need to see if it's 3623 * already present in the address list, or allocate is as well. 3624 * When this block finishes, the link layer address will be on the 3625 * list. 3626 */ 3627 if (llsa != NULL) { 3628 ll_ifma = if_findmulti(ifp, llsa); 3629 if (ll_ifma == NULL) { 3630 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT); 3631 if (ll_ifma == NULL) { 3632 --ifma->ifma_refcount; 3633 if_freemulti(ifma); 3634 error = ENOMEM; 3635 goto free_llsa_out; 3636 } 3637 ll_ifma->ifma_flags |= IFMA_F_ENQUEUED; 3638 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma, 3639 ifma_link); 3640 } else 3641 ll_ifma->ifma_refcount++; 3642 ifma->ifma_llifma = ll_ifma; 3643 } 3644 3645 /* 3646 * We now have a new multicast address, ifma, and possibly a new or 3647 * referenced link layer address. Add the primary address to the 3648 * ifnet address list. 3649 */ 3650 ifma->ifma_flags |= IFMA_F_ENQUEUED; 3651 CK_STAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 3652 3653 if (retifma != NULL) 3654 *retifma = ifma; 3655 3656 /* 3657 * Must generate the message while holding the lock so that 'ifma' 3658 * pointer is still valid. 3659 */ 3660 rt_newmaddrmsg(RTM_NEWMADDR, ifma); 3661 IF_ADDR_WUNLOCK(ifp); 3662 3663 /* 3664 * We are certain we have added something, so call down to the 3665 * interface to let them know about it. 3666 */ 3667 if (ifp->if_ioctl != NULL) { 3668 if (THREAD_CAN_SLEEP()) 3669 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0); 3670 else 3671 taskqueue_enqueue(taskqueue_swi, &ifp->if_addmultitask); 3672 } 3673 3674 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl)) 3675 link_free_sdl(llsa); 3676 3677 return (0); 3678 3679 free_llsa_out: 3680 if ((llsa != NULL) && (llsa != (struct sockaddr *)&sdl)) 3681 link_free_sdl(llsa); 3682 3683 unlock_out: 3684 IF_ADDR_WUNLOCK(ifp); 3685 return (error); 3686 } 3687 3688 static void 3689 if_siocaddmulti(void *arg, int pending) 3690 { 3691 struct ifnet *ifp; 3692 3693 ifp = arg; 3694 #ifdef DIAGNOSTIC 3695 if (pending > 1) 3696 if_printf(ifp, "%d SIOCADDMULTI coalesced\n", pending); 3697 #endif 3698 CURVNET_SET(ifp->if_vnet); 3699 (void )(*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0); 3700 CURVNET_RESTORE(); 3701 } 3702 3703 /* 3704 * Delete a multicast group membership by network-layer group address. 3705 * 3706 * Returns ENOENT if the entry could not be found. If ifp no longer 3707 * exists, results are undefined. This entry point should only be used 3708 * from subsystems which do appropriate locking to hold ifp for the 3709 * duration of the call. 3710 * Network-layer protocol domains must use if_delmulti_ifma(). 3711 */ 3712 int 3713 if_delmulti(struct ifnet *ifp, struct sockaddr *sa) 3714 { 3715 struct ifmultiaddr *ifma; 3716 int lastref; 3717 3718 KASSERT(ifp, ("%s: NULL ifp", __func__)); 3719 3720 IF_ADDR_WLOCK(ifp); 3721 lastref = 0; 3722 ifma = if_findmulti(ifp, sa); 3723 if (ifma != NULL) 3724 lastref = if_delmulti_locked(ifp, ifma, 0); 3725 IF_ADDR_WUNLOCK(ifp); 3726 3727 if (ifma == NULL) 3728 return (ENOENT); 3729 3730 if (lastref && ifp->if_ioctl != NULL) { 3731 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 3732 } 3733 3734 return (0); 3735 } 3736 3737 /* 3738 * Delete all multicast group membership for an interface. 3739 * Should be used to quickly flush all multicast filters. 3740 */ 3741 void 3742 if_delallmulti(struct ifnet *ifp) 3743 { 3744 struct ifmultiaddr *ifma; 3745 struct ifmultiaddr *next; 3746 3747 IF_ADDR_WLOCK(ifp); 3748 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) 3749 if_delmulti_locked(ifp, ifma, 0); 3750 IF_ADDR_WUNLOCK(ifp); 3751 } 3752 3753 void 3754 if_delmulti_ifma(struct ifmultiaddr *ifma) 3755 { 3756 if_delmulti_ifma_flags(ifma, 0); 3757 } 3758 3759 /* 3760 * Delete a multicast group membership by group membership pointer. 3761 * Network-layer protocol domains must use this routine. 3762 * 3763 * It is safe to call this routine if the ifp disappeared. 3764 */ 3765 void 3766 if_delmulti_ifma_flags(struct ifmultiaddr *ifma, int flags) 3767 { 3768 struct ifnet *ifp; 3769 int lastref; 3770 MCDPRINTF("%s freeing ifma: %p\n", __func__, ifma); 3771 #ifdef INET 3772 IN_MULTI_LIST_UNLOCK_ASSERT(); 3773 #endif 3774 ifp = ifma->ifma_ifp; 3775 #ifdef DIAGNOSTIC 3776 if (ifp == NULL) { 3777 printf("%s: ifma_ifp seems to be detached\n", __func__); 3778 } else { 3779 struct epoch_tracker et; 3780 struct ifnet *oifp; 3781 3782 NET_EPOCH_ENTER(et); 3783 CK_STAILQ_FOREACH(oifp, &V_ifnet, if_link) 3784 if (ifp == oifp) 3785 break; 3786 NET_EPOCH_EXIT(et); 3787 if (ifp != oifp) 3788 ifp = NULL; 3789 } 3790 #endif 3791 /* 3792 * If and only if the ifnet instance exists: Acquire the address lock. 3793 */ 3794 if (ifp != NULL) 3795 IF_ADDR_WLOCK(ifp); 3796 3797 lastref = if_delmulti_locked(ifp, ifma, flags); 3798 3799 if (ifp != NULL) { 3800 /* 3801 * If and only if the ifnet instance exists: 3802 * Release the address lock. 3803 * If the group was left: update the hardware hash filter. 3804 */ 3805 IF_ADDR_WUNLOCK(ifp); 3806 if (lastref && ifp->if_ioctl != NULL) { 3807 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 3808 } 3809 } 3810 } 3811 3812 /* 3813 * Perform deletion of network-layer and/or link-layer multicast address. 3814 * 3815 * Return 0 if the reference count was decremented. 3816 * Return 1 if the final reference was released, indicating that the 3817 * hardware hash filter should be reprogrammed. 3818 */ 3819 static int 3820 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching) 3821 { 3822 struct ifmultiaddr *ll_ifma; 3823 3824 if (ifp != NULL && ifma->ifma_ifp != NULL) { 3825 KASSERT(ifma->ifma_ifp == ifp, 3826 ("%s: inconsistent ifp %p", __func__, ifp)); 3827 IF_ADDR_WLOCK_ASSERT(ifp); 3828 } 3829 3830 ifp = ifma->ifma_ifp; 3831 MCDPRINTF("%s freeing %p from %s \n", __func__, ifma, ifp ? ifp->if_xname : ""); 3832 3833 /* 3834 * If the ifnet is detaching, null out references to ifnet, 3835 * so that upper protocol layers will notice, and not attempt 3836 * to obtain locks for an ifnet which no longer exists. The 3837 * routing socket announcement must happen before the ifnet 3838 * instance is detached from the system. 3839 */ 3840 if (detaching) { 3841 #ifdef DIAGNOSTIC 3842 printf("%s: detaching ifnet instance %p\n", __func__, ifp); 3843 #endif 3844 /* 3845 * ifp may already be nulled out if we are being reentered 3846 * to delete the ll_ifma. 3847 */ 3848 if (ifp != NULL) { 3849 rt_newmaddrmsg(RTM_DELMADDR, ifma); 3850 ifma->ifma_ifp = NULL; 3851 } 3852 } 3853 3854 if (--ifma->ifma_refcount > 0) 3855 return 0; 3856 3857 if (ifp != NULL && detaching == 0 && (ifma->ifma_flags & IFMA_F_ENQUEUED)) { 3858 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link); 3859 ifma->ifma_flags &= ~IFMA_F_ENQUEUED; 3860 } 3861 /* 3862 * If this ifma is a network-layer ifma, a link-layer ifma may 3863 * have been associated with it. Release it first if so. 3864 */ 3865 ll_ifma = ifma->ifma_llifma; 3866 if (ll_ifma != NULL) { 3867 KASSERT(ifma->ifma_lladdr != NULL, 3868 ("%s: llifma w/o lladdr", __func__)); 3869 if (detaching) 3870 ll_ifma->ifma_ifp = NULL; /* XXX */ 3871 if (--ll_ifma->ifma_refcount == 0) { 3872 if (ifp != NULL) { 3873 if (ll_ifma->ifma_flags & IFMA_F_ENQUEUED) { 3874 CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr, 3875 ifma_link); 3876 ll_ifma->ifma_flags &= ~IFMA_F_ENQUEUED; 3877 } 3878 } 3879 if_freemulti(ll_ifma); 3880 } 3881 } 3882 #ifdef INVARIANTS 3883 if (ifp) { 3884 struct ifmultiaddr *ifmatmp; 3885 3886 CK_STAILQ_FOREACH(ifmatmp, &ifp->if_multiaddrs, ifma_link) 3887 MPASS(ifma != ifmatmp); 3888 } 3889 #endif 3890 if_freemulti(ifma); 3891 /* 3892 * The last reference to this instance of struct ifmultiaddr 3893 * was released; the hardware should be notified of this change. 3894 */ 3895 return 1; 3896 } 3897 3898 /* 3899 * Set the link layer address on an interface. 3900 * 3901 * At this time we only support certain types of interfaces, 3902 * and we don't allow the length of the address to change. 3903 * 3904 * Set noinline to be dtrace-friendly 3905 */ 3906 __noinline int 3907 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 3908 { 3909 struct sockaddr_dl *sdl; 3910 struct ifaddr *ifa; 3911 struct ifreq ifr; 3912 3913 ifa = ifp->if_addr; 3914 if (ifa == NULL) 3915 return (EINVAL); 3916 3917 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 3918 if (sdl == NULL) 3919 return (EINVAL); 3920 3921 if (len != sdl->sdl_alen) /* don't allow length to change */ 3922 return (EINVAL); 3923 3924 switch (ifp->if_type) { 3925 case IFT_ETHER: 3926 case IFT_XETHER: 3927 case IFT_L2VLAN: 3928 case IFT_BRIDGE: 3929 case IFT_IEEE8023ADLAG: 3930 bcopy(lladdr, LLADDR(sdl), len); 3931 break; 3932 default: 3933 return (ENODEV); 3934 } 3935 3936 /* 3937 * If the interface is already up, we need 3938 * to re-init it in order to reprogram its 3939 * address filter. 3940 */ 3941 if ((ifp->if_flags & IFF_UP) != 0) { 3942 if (ifp->if_ioctl) { 3943 ifp->if_flags &= ~IFF_UP; 3944 ifr.ifr_flags = ifp->if_flags & 0xffff; 3945 ifr.ifr_flagshigh = ifp->if_flags >> 16; 3946 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 3947 ifp->if_flags |= IFF_UP; 3948 ifr.ifr_flags = ifp->if_flags & 0xffff; 3949 ifr.ifr_flagshigh = ifp->if_flags >> 16; 3950 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 3951 } 3952 } 3953 EVENTHANDLER_INVOKE(iflladdr_event, ifp); 3954 3955 return (0); 3956 } 3957 3958 /* 3959 * Compat function for handling basic encapsulation requests. 3960 * Not converted stacks (FDDI, IB, ..) supports traditional 3961 * output model: ARP (and other similar L2 protocols) are handled 3962 * inside output routine, arpresolve/nd6_resolve() returns MAC 3963 * address instead of full prepend. 3964 * 3965 * This function creates calculated header==MAC for IPv4/IPv6 and 3966 * returns EAFNOSUPPORT (which is then handled in ARP code) for other 3967 * address families. 3968 */ 3969 static int 3970 if_requestencap_default(struct ifnet *ifp, struct if_encap_req *req) 3971 { 3972 3973 if (req->rtype != IFENCAP_LL) 3974 return (EOPNOTSUPP); 3975 3976 if (req->bufsize < req->lladdr_len) 3977 return (ENOMEM); 3978 3979 switch (req->family) { 3980 case AF_INET: 3981 case AF_INET6: 3982 break; 3983 default: 3984 return (EAFNOSUPPORT); 3985 } 3986 3987 /* Copy lladdr to storage as is */ 3988 memmove(req->buf, req->lladdr, req->lladdr_len); 3989 req->bufsize = req->lladdr_len; 3990 req->lladdr_off = 0; 3991 3992 return (0); 3993 } 3994 3995 /* 3996 * Tunnel interfaces can nest, also they may cause infinite recursion 3997 * calls when misconfigured. We'll prevent this by detecting loops. 3998 * High nesting level may cause stack exhaustion. We'll prevent this 3999 * by introducing upper limit. 4000 * 4001 * Return 0, if tunnel nesting count is equal or less than limit. 4002 */ 4003 int 4004 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, uint32_t cookie, 4005 int limit) 4006 { 4007 struct m_tag *mtag; 4008 int count; 4009 4010 count = 1; 4011 mtag = NULL; 4012 while ((mtag = m_tag_locate(m, cookie, 0, mtag)) != NULL) { 4013 if (*(struct ifnet **)(mtag + 1) == ifp) { 4014 log(LOG_NOTICE, "%s: loop detected\n", if_name(ifp)); 4015 return (EIO); 4016 } 4017 count++; 4018 } 4019 if (count > limit) { 4020 log(LOG_NOTICE, 4021 "%s: if_output recursively called too many times(%d)\n", 4022 if_name(ifp), count); 4023 return (EIO); 4024 } 4025 mtag = m_tag_alloc(cookie, 0, sizeof(struct ifnet *), M_NOWAIT); 4026 if (mtag == NULL) 4027 return (ENOMEM); 4028 *(struct ifnet **)(mtag + 1) = ifp; 4029 m_tag_prepend(m, mtag); 4030 return (0); 4031 } 4032 4033 /* 4034 * Get the link layer address that was read from the hardware at attach. 4035 * 4036 * This is only set by Ethernet NICs (IFT_ETHER), but laggX interfaces re-type 4037 * their component interfaces as IFT_IEEE8023ADLAG. 4038 */ 4039 int 4040 if_gethwaddr(struct ifnet *ifp, struct ifreq *ifr) 4041 { 4042 4043 if (ifp->if_hw_addr == NULL) 4044 return (ENODEV); 4045 4046 switch (ifp->if_type) { 4047 case IFT_ETHER: 4048 case IFT_IEEE8023ADLAG: 4049 bcopy(ifp->if_hw_addr, ifr->ifr_addr.sa_data, ifp->if_addrlen); 4050 return (0); 4051 default: 4052 return (ENODEV); 4053 } 4054 } 4055 4056 /* 4057 * The name argument must be a pointer to storage which will last as 4058 * long as the interface does. For physical devices, the result of 4059 * device_get_name(dev) is a good choice and for pseudo-devices a 4060 * static string works well. 4061 */ 4062 void 4063 if_initname(struct ifnet *ifp, const char *name, int unit) 4064 { 4065 ifp->if_dname = name; 4066 ifp->if_dunit = unit; 4067 if (unit != IF_DUNIT_NONE) 4068 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit); 4069 else 4070 strlcpy(ifp->if_xname, name, IFNAMSIZ); 4071 } 4072 4073 static int 4074 if_vlog(struct ifnet *ifp, int pri, const char *fmt, va_list ap) 4075 { 4076 char if_fmt[256]; 4077 4078 snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt); 4079 vlog(pri, if_fmt, ap); 4080 return (0); 4081 } 4082 4083 4084 int 4085 if_printf(struct ifnet *ifp, const char *fmt, ...) 4086 { 4087 va_list ap; 4088 4089 va_start(ap, fmt); 4090 if_vlog(ifp, LOG_INFO, fmt, ap); 4091 va_end(ap); 4092 return (0); 4093 } 4094 4095 int 4096 if_log(struct ifnet *ifp, int pri, const char *fmt, ...) 4097 { 4098 va_list ap; 4099 4100 va_start(ap, fmt); 4101 if_vlog(ifp, pri, fmt, ap); 4102 va_end(ap); 4103 return (0); 4104 } 4105 4106 void 4107 if_start(struct ifnet *ifp) 4108 { 4109 4110 (*(ifp)->if_start)(ifp); 4111 } 4112 4113 /* 4114 * Backwards compatibility interface for drivers 4115 * that have not implemented it 4116 */ 4117 static int 4118 if_transmit(struct ifnet *ifp, struct mbuf *m) 4119 { 4120 int error; 4121 4122 IFQ_HANDOFF(ifp, m, error); 4123 return (error); 4124 } 4125 4126 static void 4127 if_input_default(struct ifnet *ifp __unused, struct mbuf *m) 4128 { 4129 4130 m_freem(m); 4131 } 4132 4133 int 4134 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust) 4135 { 4136 int active = 0; 4137 4138 IF_LOCK(ifq); 4139 if (_IF_QFULL(ifq)) { 4140 IF_UNLOCK(ifq); 4141 if_inc_counter(ifp, IFCOUNTER_OQDROPS, 1); 4142 m_freem(m); 4143 return (0); 4144 } 4145 if (ifp != NULL) { 4146 if_inc_counter(ifp, IFCOUNTER_OBYTES, m->m_pkthdr.len + adjust); 4147 if (m->m_flags & (M_BCAST|M_MCAST)) 4148 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1); 4149 active = ifp->if_drv_flags & IFF_DRV_OACTIVE; 4150 } 4151 _IF_ENQUEUE(ifq, m); 4152 IF_UNLOCK(ifq); 4153 if (ifp != NULL && !active) 4154 (*(ifp)->if_start)(ifp); 4155 return (1); 4156 } 4157 4158 void 4159 if_register_com_alloc(u_char type, 4160 if_com_alloc_t *a, if_com_free_t *f) 4161 { 4162 4163 KASSERT(if_com_alloc[type] == NULL, 4164 ("if_register_com_alloc: %d already registered", type)); 4165 KASSERT(if_com_free[type] == NULL, 4166 ("if_register_com_alloc: %d free already registered", type)); 4167 4168 if_com_alloc[type] = a; 4169 if_com_free[type] = f; 4170 } 4171 4172 void 4173 if_deregister_com_alloc(u_char type) 4174 { 4175 4176 KASSERT(if_com_alloc[type] != NULL, 4177 ("if_deregister_com_alloc: %d not registered", type)); 4178 KASSERT(if_com_free[type] != NULL, 4179 ("if_deregister_com_alloc: %d free not registered", type)); 4180 4181 /* 4182 * Ensure all pending EPOCH(9) callbacks have been executed. This 4183 * fixes issues about late invocation of if_destroy(), which leads 4184 * to memory leak from if_com_alloc[type] allocated if_l2com. 4185 */ 4186 NET_EPOCH_DRAIN_CALLBACKS(); 4187 4188 if_com_alloc[type] = NULL; 4189 if_com_free[type] = NULL; 4190 } 4191 4192 /* API for driver access to network stack owned ifnet.*/ 4193 uint64_t 4194 if_setbaudrate(struct ifnet *ifp, uint64_t baudrate) 4195 { 4196 uint64_t oldbrate; 4197 4198 oldbrate = ifp->if_baudrate; 4199 ifp->if_baudrate = baudrate; 4200 return (oldbrate); 4201 } 4202 4203 uint64_t 4204 if_getbaudrate(if_t ifp) 4205 { 4206 4207 return (((struct ifnet *)ifp)->if_baudrate); 4208 } 4209 4210 int 4211 if_setcapabilities(if_t ifp, int capabilities) 4212 { 4213 ((struct ifnet *)ifp)->if_capabilities = capabilities; 4214 return (0); 4215 } 4216 4217 int 4218 if_setcapabilitiesbit(if_t ifp, int setbit, int clearbit) 4219 { 4220 ((struct ifnet *)ifp)->if_capabilities |= setbit; 4221 ((struct ifnet *)ifp)->if_capabilities &= ~clearbit; 4222 4223 return (0); 4224 } 4225 4226 int 4227 if_getcapabilities(if_t ifp) 4228 { 4229 return ((struct ifnet *)ifp)->if_capabilities; 4230 } 4231 4232 int 4233 if_setcapenable(if_t ifp, int capabilities) 4234 { 4235 ((struct ifnet *)ifp)->if_capenable = capabilities; 4236 return (0); 4237 } 4238 4239 int 4240 if_setcapenablebit(if_t ifp, int setcap, int clearcap) 4241 { 4242 if(setcap) 4243 ((struct ifnet *)ifp)->if_capenable |= setcap; 4244 if(clearcap) 4245 ((struct ifnet *)ifp)->if_capenable &= ~clearcap; 4246 4247 return (0); 4248 } 4249 4250 const char * 4251 if_getdname(if_t ifp) 4252 { 4253 return ((struct ifnet *)ifp)->if_dname; 4254 } 4255 4256 int 4257 if_togglecapenable(if_t ifp, int togglecap) 4258 { 4259 ((struct ifnet *)ifp)->if_capenable ^= togglecap; 4260 return (0); 4261 } 4262 4263 int 4264 if_getcapenable(if_t ifp) 4265 { 4266 return ((struct ifnet *)ifp)->if_capenable; 4267 } 4268 4269 /* 4270 * This is largely undesirable because it ties ifnet to a device, but does 4271 * provide flexiblity for an embedded product vendor. Should be used with 4272 * the understanding that it violates the interface boundaries, and should be 4273 * a last resort only. 4274 */ 4275 int 4276 if_setdev(if_t ifp, void *dev) 4277 { 4278 return (0); 4279 } 4280 4281 int 4282 if_setdrvflagbits(if_t ifp, int set_flags, int clear_flags) 4283 { 4284 ((struct ifnet *)ifp)->if_drv_flags |= set_flags; 4285 ((struct ifnet *)ifp)->if_drv_flags &= ~clear_flags; 4286 4287 return (0); 4288 } 4289 4290 int 4291 if_getdrvflags(if_t ifp) 4292 { 4293 return ((struct ifnet *)ifp)->if_drv_flags; 4294 } 4295 4296 int 4297 if_setdrvflags(if_t ifp, int flags) 4298 { 4299 ((struct ifnet *)ifp)->if_drv_flags = flags; 4300 return (0); 4301 } 4302 4303 int 4304 if_setflags(if_t ifp, int flags) 4305 { 4306 4307 ifp->if_flags = flags; 4308 return (0); 4309 } 4310 4311 int 4312 if_setflagbits(if_t ifp, int set, int clear) 4313 { 4314 ((struct ifnet *)ifp)->if_flags |= set; 4315 ((struct ifnet *)ifp)->if_flags &= ~clear; 4316 4317 return (0); 4318 } 4319 4320 int 4321 if_getflags(if_t ifp) 4322 { 4323 return ((struct ifnet *)ifp)->if_flags; 4324 } 4325 4326 int 4327 if_clearhwassist(if_t ifp) 4328 { 4329 ((struct ifnet *)ifp)->if_hwassist = 0; 4330 return (0); 4331 } 4332 4333 int 4334 if_sethwassistbits(if_t ifp, int toset, int toclear) 4335 { 4336 ((struct ifnet *)ifp)->if_hwassist |= toset; 4337 ((struct ifnet *)ifp)->if_hwassist &= ~toclear; 4338 4339 return (0); 4340 } 4341 4342 int 4343 if_sethwassist(if_t ifp, int hwassist_bit) 4344 { 4345 ((struct ifnet *)ifp)->if_hwassist = hwassist_bit; 4346 return (0); 4347 } 4348 4349 int 4350 if_gethwassist(if_t ifp) 4351 { 4352 return ((struct ifnet *)ifp)->if_hwassist; 4353 } 4354 4355 int 4356 if_setmtu(if_t ifp, int mtu) 4357 { 4358 ((struct ifnet *)ifp)->if_mtu = mtu; 4359 return (0); 4360 } 4361 4362 int 4363 if_getmtu(if_t ifp) 4364 { 4365 return ((struct ifnet *)ifp)->if_mtu; 4366 } 4367 4368 int 4369 if_getmtu_family(if_t ifp, int family) 4370 { 4371 struct domain *dp; 4372 4373 for (dp = domains; dp; dp = dp->dom_next) { 4374 if (dp->dom_family == family && dp->dom_ifmtu != NULL) 4375 return (dp->dom_ifmtu((struct ifnet *)ifp)); 4376 } 4377 4378 return (((struct ifnet *)ifp)->if_mtu); 4379 } 4380 4381 /* 4382 * Methods for drivers to access interface unicast and multicast 4383 * link level addresses. Driver shall not know 'struct ifaddr' neither 4384 * 'struct ifmultiaddr'. 4385 */ 4386 u_int 4387 if_lladdr_count(if_t ifp) 4388 { 4389 struct epoch_tracker et; 4390 struct ifaddr *ifa; 4391 u_int count; 4392 4393 count = 0; 4394 NET_EPOCH_ENTER(et); 4395 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 4396 if (ifa->ifa_addr->sa_family == AF_LINK) 4397 count++; 4398 NET_EPOCH_EXIT(et); 4399 4400 return (count); 4401 } 4402 4403 u_int 4404 if_foreach_lladdr(if_t ifp, iflladdr_cb_t cb, void *cb_arg) 4405 { 4406 struct epoch_tracker et; 4407 struct ifaddr *ifa; 4408 u_int count; 4409 4410 MPASS(cb); 4411 4412 count = 0; 4413 NET_EPOCH_ENTER(et); 4414 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 4415 if (ifa->ifa_addr->sa_family != AF_LINK) 4416 continue; 4417 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifa->ifa_addr, 4418 count); 4419 } 4420 NET_EPOCH_EXIT(et); 4421 4422 return (count); 4423 } 4424 4425 u_int 4426 if_llmaddr_count(if_t ifp) 4427 { 4428 struct epoch_tracker et; 4429 struct ifmultiaddr *ifma; 4430 int count; 4431 4432 count = 0; 4433 NET_EPOCH_ENTER(et); 4434 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 4435 if (ifma->ifma_addr->sa_family == AF_LINK) 4436 count++; 4437 NET_EPOCH_EXIT(et); 4438 4439 return (count); 4440 } 4441 4442 u_int 4443 if_foreach_llmaddr(if_t ifp, iflladdr_cb_t cb, void *cb_arg) 4444 { 4445 struct epoch_tracker et; 4446 struct ifmultiaddr *ifma; 4447 u_int count; 4448 4449 MPASS(cb); 4450 4451 count = 0; 4452 NET_EPOCH_ENTER(et); 4453 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 4454 if (ifma->ifma_addr->sa_family != AF_LINK) 4455 continue; 4456 count += (*cb)(cb_arg, (struct sockaddr_dl *)ifma->ifma_addr, 4457 count); 4458 } 4459 NET_EPOCH_EXIT(et); 4460 4461 return (count); 4462 } 4463 4464 int 4465 if_setsoftc(if_t ifp, void *softc) 4466 { 4467 ((struct ifnet *)ifp)->if_softc = softc; 4468 return (0); 4469 } 4470 4471 void * 4472 if_getsoftc(if_t ifp) 4473 { 4474 return ((struct ifnet *)ifp)->if_softc; 4475 } 4476 4477 void 4478 if_setrcvif(struct mbuf *m, if_t ifp) 4479 { 4480 4481 MPASS((m->m_pkthdr.csum_flags & CSUM_SND_TAG) == 0); 4482 m->m_pkthdr.rcvif = (struct ifnet *)ifp; 4483 } 4484 4485 void 4486 if_setvtag(struct mbuf *m, uint16_t tag) 4487 { 4488 m->m_pkthdr.ether_vtag = tag; 4489 } 4490 4491 uint16_t 4492 if_getvtag(struct mbuf *m) 4493 { 4494 4495 return (m->m_pkthdr.ether_vtag); 4496 } 4497 4498 int 4499 if_sendq_empty(if_t ifp) 4500 { 4501 return IFQ_DRV_IS_EMPTY(&((struct ifnet *)ifp)->if_snd); 4502 } 4503 4504 struct ifaddr * 4505 if_getifaddr(if_t ifp) 4506 { 4507 return ((struct ifnet *)ifp)->if_addr; 4508 } 4509 4510 int 4511 if_getamcount(if_t ifp) 4512 { 4513 return ((struct ifnet *)ifp)->if_amcount; 4514 } 4515 4516 int 4517 if_setsendqready(if_t ifp) 4518 { 4519 IFQ_SET_READY(&((struct ifnet *)ifp)->if_snd); 4520 return (0); 4521 } 4522 4523 int 4524 if_setsendqlen(if_t ifp, int tx_desc_count) 4525 { 4526 IFQ_SET_MAXLEN(&((struct ifnet *)ifp)->if_snd, tx_desc_count); 4527 ((struct ifnet *)ifp)->if_snd.ifq_drv_maxlen = tx_desc_count; 4528 4529 return (0); 4530 } 4531 4532 int 4533 if_vlantrunkinuse(if_t ifp) 4534 { 4535 return ((struct ifnet *)ifp)->if_vlantrunk != NULL?1:0; 4536 } 4537 4538 int 4539 if_input(if_t ifp, struct mbuf* sendmp) 4540 { 4541 (*((struct ifnet *)ifp)->if_input)((struct ifnet *)ifp, sendmp); 4542 return (0); 4543 4544 } 4545 4546 struct mbuf * 4547 if_dequeue(if_t ifp) 4548 { 4549 struct mbuf *m; 4550 IFQ_DRV_DEQUEUE(&((struct ifnet *)ifp)->if_snd, m); 4551 4552 return (m); 4553 } 4554 4555 int 4556 if_sendq_prepend(if_t ifp, struct mbuf *m) 4557 { 4558 IFQ_DRV_PREPEND(&((struct ifnet *)ifp)->if_snd, m); 4559 return (0); 4560 } 4561 4562 int 4563 if_setifheaderlen(if_t ifp, int len) 4564 { 4565 ((struct ifnet *)ifp)->if_hdrlen = len; 4566 return (0); 4567 } 4568 4569 caddr_t 4570 if_getlladdr(if_t ifp) 4571 { 4572 return (IF_LLADDR((struct ifnet *)ifp)); 4573 } 4574 4575 void * 4576 if_gethandle(u_char type) 4577 { 4578 return (if_alloc(type)); 4579 } 4580 4581 void 4582 if_bpfmtap(if_t ifh, struct mbuf *m) 4583 { 4584 struct ifnet *ifp = (struct ifnet *)ifh; 4585 4586 BPF_MTAP(ifp, m); 4587 } 4588 4589 void 4590 if_etherbpfmtap(if_t ifh, struct mbuf *m) 4591 { 4592 struct ifnet *ifp = (struct ifnet *)ifh; 4593 4594 ETHER_BPF_MTAP(ifp, m); 4595 } 4596 4597 void 4598 if_vlancap(if_t ifh) 4599 { 4600 struct ifnet *ifp = (struct ifnet *)ifh; 4601 VLAN_CAPABILITIES(ifp); 4602 } 4603 4604 int 4605 if_sethwtsomax(if_t ifp, u_int if_hw_tsomax) 4606 { 4607 4608 ((struct ifnet *)ifp)->if_hw_tsomax = if_hw_tsomax; 4609 return (0); 4610 } 4611 4612 int 4613 if_sethwtsomaxsegcount(if_t ifp, u_int if_hw_tsomaxsegcount) 4614 { 4615 4616 ((struct ifnet *)ifp)->if_hw_tsomaxsegcount = if_hw_tsomaxsegcount; 4617 return (0); 4618 } 4619 4620 int 4621 if_sethwtsomaxsegsize(if_t ifp, u_int if_hw_tsomaxsegsize) 4622 { 4623 4624 ((struct ifnet *)ifp)->if_hw_tsomaxsegsize = if_hw_tsomaxsegsize; 4625 return (0); 4626 } 4627 4628 u_int 4629 if_gethwtsomax(if_t ifp) 4630 { 4631 4632 return (((struct ifnet *)ifp)->if_hw_tsomax); 4633 } 4634 4635 u_int 4636 if_gethwtsomaxsegcount(if_t ifp) 4637 { 4638 4639 return (((struct ifnet *)ifp)->if_hw_tsomaxsegcount); 4640 } 4641 4642 u_int 4643 if_gethwtsomaxsegsize(if_t ifp) 4644 { 4645 4646 return (((struct ifnet *)ifp)->if_hw_tsomaxsegsize); 4647 } 4648 4649 void 4650 if_setinitfn(if_t ifp, void (*init_fn)(void *)) 4651 { 4652 ((struct ifnet *)ifp)->if_init = init_fn; 4653 } 4654 4655 void 4656 if_setioctlfn(if_t ifp, int (*ioctl_fn)(if_t, u_long, caddr_t)) 4657 { 4658 ((struct ifnet *)ifp)->if_ioctl = (void *)ioctl_fn; 4659 } 4660 4661 void 4662 if_setstartfn(if_t ifp, void (*start_fn)(if_t)) 4663 { 4664 ((struct ifnet *)ifp)->if_start = (void *)start_fn; 4665 } 4666 4667 void 4668 if_settransmitfn(if_t ifp, if_transmit_fn_t start_fn) 4669 { 4670 ((struct ifnet *)ifp)->if_transmit = start_fn; 4671 } 4672 4673 void if_setqflushfn(if_t ifp, if_qflush_fn_t flush_fn) 4674 { 4675 ((struct ifnet *)ifp)->if_qflush = flush_fn; 4676 4677 } 4678 4679 void 4680 if_setgetcounterfn(if_t ifp, if_get_counter_t fn) 4681 { 4682 4683 ifp->if_get_counter = fn; 4684 } 4685 4686 #ifdef DDB 4687 static void 4688 if_show_ifnet(struct ifnet *ifp) 4689 { 4690 4691 if (ifp == NULL) 4692 return; 4693 db_printf("%s:\n", ifp->if_xname); 4694 #define IF_DB_PRINTF(f, e) db_printf(" %s = " f "\n", #e, ifp->e); 4695 IF_DB_PRINTF("%s", if_dname); 4696 IF_DB_PRINTF("%d", if_dunit); 4697 IF_DB_PRINTF("%s", if_description); 4698 IF_DB_PRINTF("%u", if_index); 4699 IF_DB_PRINTF("%d", if_idxgen); 4700 IF_DB_PRINTF("%u", if_refcount); 4701 IF_DB_PRINTF("%p", if_softc); 4702 IF_DB_PRINTF("%p", if_l2com); 4703 IF_DB_PRINTF("%p", if_llsoftc); 4704 IF_DB_PRINTF("%d", if_amcount); 4705 IF_DB_PRINTF("%p", if_addr); 4706 IF_DB_PRINTF("%p", if_broadcastaddr); 4707 IF_DB_PRINTF("%p", if_afdata); 4708 IF_DB_PRINTF("%d", if_afdata_initialized); 4709 IF_DB_PRINTF("%u", if_fib); 4710 IF_DB_PRINTF("%p", if_vnet); 4711 IF_DB_PRINTF("%p", if_home_vnet); 4712 IF_DB_PRINTF("%p", if_vlantrunk); 4713 IF_DB_PRINTF("%p", if_bpf); 4714 IF_DB_PRINTF("%u", if_pcount); 4715 IF_DB_PRINTF("%p", if_bridge); 4716 IF_DB_PRINTF("%p", if_lagg); 4717 IF_DB_PRINTF("%p", if_pf_kif); 4718 IF_DB_PRINTF("%p", if_carp); 4719 IF_DB_PRINTF("%p", if_label); 4720 IF_DB_PRINTF("%p", if_netmap); 4721 IF_DB_PRINTF("0x%08x", if_flags); 4722 IF_DB_PRINTF("0x%08x", if_drv_flags); 4723 IF_DB_PRINTF("0x%08x", if_capabilities); 4724 IF_DB_PRINTF("0x%08x", if_capenable); 4725 IF_DB_PRINTF("%p", if_snd.ifq_head); 4726 IF_DB_PRINTF("%p", if_snd.ifq_tail); 4727 IF_DB_PRINTF("%d", if_snd.ifq_len); 4728 IF_DB_PRINTF("%d", if_snd.ifq_maxlen); 4729 IF_DB_PRINTF("%p", if_snd.ifq_drv_head); 4730 IF_DB_PRINTF("%p", if_snd.ifq_drv_tail); 4731 IF_DB_PRINTF("%d", if_snd.ifq_drv_len); 4732 IF_DB_PRINTF("%d", if_snd.ifq_drv_maxlen); 4733 IF_DB_PRINTF("%d", if_snd.altq_type); 4734 IF_DB_PRINTF("%x", if_snd.altq_flags); 4735 #undef IF_DB_PRINTF 4736 } 4737 4738 DB_SHOW_COMMAND(ifnet, db_show_ifnet) 4739 { 4740 4741 if (!have_addr) { 4742 db_printf("usage: show ifnet <struct ifnet *>\n"); 4743 return; 4744 } 4745 4746 if_show_ifnet((struct ifnet *)addr); 4747 } 4748 4749 DB_SHOW_ALL_COMMAND(ifnets, db_show_all_ifnets) 4750 { 4751 struct ifnet *ifp; 4752 u_short idx; 4753 4754 for (idx = 1; idx <= if_index; idx++) { 4755 ifp = ifindex_table[idx].ife_ifnet; 4756 if (ifp == NULL) 4757 continue; 4758 db_printf( "%20s ifp=%p\n", ifp->if_xname, ifp); 4759 if (db_pager_quit) 4760 break; 4761 } 4762 } 4763 #endif /* DDB */ 4764