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