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