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