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