1/*- 2 * Copyright (c) 1980, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)if.c 8.5 (Berkeley) 1/9/95 30 * $FreeBSD: releng/10.3/sys/net/if.c 294502 2016-01-21 14:08:40Z bz $ 31 */ 32 33#include "opt_compat.h" 34#include "opt_inet6.h" 35#include "opt_inet.h" 36 37#include <sys/param.h> 38#include <sys/types.h> 39#include <sys/conf.h> 40#include <sys/malloc.h> 41#include <sys/sbuf.h> 42#include <sys/bus.h> 43#include <sys/mbuf.h> 44#include <sys/systm.h> 45#include <sys/priv.h> 46#include <sys/proc.h> 47#include <sys/socket.h> 48#include <sys/socketvar.h> 49#include <sys/protosw.h> 50#include <sys/kernel.h> 51#include <sys/lock.h> 52#include <sys/refcount.h> 53#include <sys/module.h> 54#include <sys/rwlock.h> 55#include <sys/sockio.h> 56#include <sys/syslog.h> 57#include <sys/sysctl.h> 58#include <sys/taskqueue.h> 59#include <sys/domain.h> 60#include <sys/jail.h> 61#include <sys/priv.h> 62 63#include <machine/stdarg.h> 64#include <vm/uma.h> 65 66#include <net/if.h> 67#include <net/if_arp.h> 68#include <net/if_clone.h> 69#include <net/if_dl.h> 70#include <net/if_types.h> 71#include <net/if_var.h> 72#include <net/radix.h> 73#include <net/route.h> 74#include <net/vnet.h> 75 76#if defined(INET) || defined(INET6) 77#include <net/ethernet.h> 78#include <netinet/in.h> 79#include <netinet/in_var.h> 80#include <netinet/ip.h> 81#include <netinet/ip_carp.h> 82#ifdef INET 83#include <netinet/if_ether.h> 84#endif /* INET */ 85#ifdef INET6 86#include <netinet6/in6_var.h> 87#include <netinet6/in6_ifattach.h> 88#endif /* INET6 */ 89#endif /* INET || INET6 */ 90 91#include <security/mac/mac_framework.h> 92 93#ifdef COMPAT_FREEBSD32 94#include <sys/mount.h> 95#include <compat/freebsd32/freebsd32.h> 96#endif 97 98struct ifindex_entry { 99 struct ifnet *ife_ifnet; 100}; 101 102SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 103SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); 104 105TUNABLE_INT("net.link.ifqmaxlen", &ifqmaxlen); 106SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN, 107 &ifqmaxlen, 0, "max send queue size"); 108 109/* Log link state change events */ 110static int log_link_state_change = 1; 111 112SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW, 113 &log_link_state_change, 0, 114 "log interface link state change events"); 115 116/* Interface description */ 117static unsigned int ifdescr_maxlen = 1024; 118SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW, 119 &ifdescr_maxlen, 0, 120 "administrative maximum length for interface description"); 121 122static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions"); 123 124/* global sx for non-critical path ifdescr */ 125static struct sx ifdescr_sx; 126SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr"); 127 128void (*bridge_linkstate_p)(struct ifnet *ifp); 129void (*ng_ether_link_state_p)(struct ifnet *ifp, int state); 130void (*lagg_linkstate_p)(struct ifnet *ifp, int state); 131/* These are external hooks for CARP. */ 132void (*carp_linkstate_p)(struct ifnet *ifp); 133void (*carp_demote_adj_p)(int, char *); 134int (*carp_master_p)(struct ifaddr *); 135#if defined(INET) || defined(INET6) 136int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost); 137int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m, 138 const struct sockaddr *sa); 139int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *); 140int (*carp_attach_p)(struct ifaddr *, int); 141void (*carp_detach_p)(struct ifaddr *); 142#endif 143#ifdef INET 144int (*carp_iamatch_p)(struct ifaddr *, uint8_t **); 145#endif 146#ifdef INET6 147struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6); 148caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m, 149 const struct in6_addr *taddr); 150#endif 151 152struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL; 153 154/* 155 * XXX: Style; these should be sorted alphabetically, and unprototyped 156 * static functions should be prototyped. Currently they are sorted by 157 * declaration order. 158 */ 159static void if_attachdomain(void *); 160static void if_attachdomain1(struct ifnet *); 161static int ifconf(u_long, caddr_t); 162static void if_freemulti(struct ifmultiaddr *); 163static void if_init(void *); 164static void if_grow(void); 165static void if_input_default(struct ifnet *, struct mbuf *); 166static void if_route(struct ifnet *, int flag, int fam); 167static int if_setflag(struct ifnet *, int, int, int *, int); 168static int if_transmit(struct ifnet *ifp, struct mbuf *m); 169static void if_unroute(struct ifnet *, int flag, int fam); 170static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 171static int if_rtdel(struct radix_node *, void *); 172static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *); 173static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int); 174static void do_link_state_change(void *, int); 175static int if_getgroup(struct ifgroupreq *, struct ifnet *); 176static int if_getgroupmembers(struct ifgroupreq *); 177static void if_delgroups(struct ifnet *); 178static void if_attach_internal(struct ifnet *, int, struct if_clone *); 179static int if_detach_internal(struct ifnet *, int, struct if_clone **); 180 181#ifdef INET6 182/* 183 * XXX: declare here to avoid to include many inet6 related files.. 184 * should be more generalized? 185 */ 186extern void nd6_setmtu(struct ifnet *); 187#endif 188 189VNET_DEFINE(int, if_index); 190int ifqmaxlen = IFQ_MAXLEN; 191VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */ 192VNET_DEFINE(struct ifgrouphead, ifg_head); 193 194static VNET_DEFINE(int, if_indexlim) = 8; 195 196/* Table of ifnet by index. */ 197VNET_DEFINE(struct ifindex_entry *, ifindex_table); 198 199#define V_if_indexlim VNET(if_indexlim) 200#define V_ifindex_table VNET(ifindex_table) 201 202/* 203 * The global network interface list (V_ifnet) and related state (such as 204 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and 205 * an rwlock. Either may be acquired shared to stablize the list, but both 206 * must be acquired writable to modify the list. This model allows us to 207 * both stablize the interface list during interrupt thread processing, but 208 * also to stablize it over long-running ioctls, without introducing priority 209 * inversions and deadlocks. 210 */ 211struct rwlock ifnet_rwlock; 212struct sx ifnet_sxlock; 213 214/* 215 * The allocation of network interfaces is a rather non-atomic affair; we 216 * need to select an index before we are ready to expose the interface for 217 * use, so will use this pointer value to indicate reservation. 218 */ 219#define IFNET_HOLD (void *)(uintptr_t)(-1) 220 221static if_com_alloc_t *if_com_alloc[256]; 222static if_com_free_t *if_com_free[256]; 223 224static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals"); 225MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 226MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 227 228struct ifnet * 229ifnet_byindex_locked(u_short idx) 230{ 231 232 if (idx > V_if_index) 233 return (NULL); 234 if (V_ifindex_table[idx].ife_ifnet == IFNET_HOLD) 235 return (NULL); 236 return (V_ifindex_table[idx].ife_ifnet); 237} 238 239struct ifnet * 240ifnet_byindex(u_short idx) 241{ 242 struct ifnet *ifp; 243 244 IFNET_RLOCK_NOSLEEP(); 245 ifp = ifnet_byindex_locked(idx); 246 IFNET_RUNLOCK_NOSLEEP(); 247 return (ifp); 248} 249 250struct ifnet * 251ifnet_byindex_ref(u_short idx) 252{ 253 struct ifnet *ifp; 254 255 IFNET_RLOCK_NOSLEEP(); 256 ifp = ifnet_byindex_locked(idx); 257 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) { 258 IFNET_RUNLOCK_NOSLEEP(); 259 return (NULL); 260 } 261 if_ref(ifp); 262 IFNET_RUNLOCK_NOSLEEP(); 263 return (ifp); 264} 265 266/* 267 * Allocate an ifindex array entry; return 0 on success or an error on 268 * failure. 269 */ 270static int 271ifindex_alloc_locked(u_short *idxp) 272{ 273 u_short idx; 274 275 IFNET_WLOCK_ASSERT(); 276 277retry: 278 /* 279 * Try to find an empty slot below V_if_index. If we fail, take the 280 * next slot. 281 */ 282 for (idx = 1; idx <= V_if_index; idx++) { 283 if (V_ifindex_table[idx].ife_ifnet == NULL) 284 break; 285 } 286 287 /* Catch if_index overflow. */ 288 if (idx < 1) 289 return (ENOSPC); 290 if (idx >= V_if_indexlim) { 291 if_grow(); 292 goto retry; 293 } 294 if (idx > V_if_index) 295 V_if_index = idx; 296 *idxp = idx; 297 return (0); 298} 299 300static void 301ifindex_free_locked(u_short idx) 302{ 303 304 IFNET_WLOCK_ASSERT(); 305 306 V_ifindex_table[idx].ife_ifnet = NULL; 307 while (V_if_index > 0 && 308 V_ifindex_table[V_if_index].ife_ifnet == NULL) 309 V_if_index--; 310} 311 312static void 313ifindex_free(u_short idx) 314{ 315 316 IFNET_WLOCK(); 317 ifindex_free_locked(idx); 318 IFNET_WUNLOCK(); 319} 320 321static void 322ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp) 323{ 324 325 IFNET_WLOCK_ASSERT(); 326 327 V_ifindex_table[idx].ife_ifnet = ifp; 328} 329 330static void 331ifnet_setbyindex(u_short idx, struct ifnet *ifp) 332{ 333 334 IFNET_WLOCK(); 335 ifnet_setbyindex_locked(idx, ifp); 336 IFNET_WUNLOCK(); 337} 338 339struct ifaddr * 340ifaddr_byindex(u_short idx) 341{ 342 struct ifaddr *ifa; 343 344 IFNET_RLOCK_NOSLEEP(); 345 ifa = ifnet_byindex_locked(idx)->if_addr; 346 if (ifa != NULL) 347 ifa_ref(ifa); 348 IFNET_RUNLOCK_NOSLEEP(); 349 return (ifa); 350} 351 352/* 353 * Network interface utility routines. 354 * 355 * Routines with ifa_ifwith* names take sockaddr *'s as 356 * parameters. 357 */ 358 359static void 360vnet_if_init(const void *unused __unused) 361{ 362 363 TAILQ_INIT(&V_ifnet); 364 TAILQ_INIT(&V_ifg_head); 365 IFNET_WLOCK(); 366 if_grow(); /* create initial table */ 367 IFNET_WUNLOCK(); 368 vnet_if_clone_init(); 369} 370VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init, 371 NULL); 372 373/* ARGSUSED*/ 374static void 375if_init(void *dummy __unused) 376{ 377 378 IFNET_LOCK_INIT(); 379 if_clone_init(); 380} 381SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL); 382 383 384#ifdef VIMAGE 385static void 386vnet_if_uninit(const void *unused __unused) 387{ 388 389 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p " 390 "not empty", __func__, __LINE__, &V_ifnet)); 391 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p " 392 "not empty", __func__, __LINE__, &V_ifg_head)); 393 394 free((caddr_t)V_ifindex_table, M_IFNET); 395} 396VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST, 397 vnet_if_uninit, NULL); 398#endif 399 400static void 401if_grow(void) 402{ 403 int oldlim; 404 u_int n; 405 struct ifindex_entry *e; 406 407 IFNET_WLOCK_ASSERT(); 408 oldlim = V_if_indexlim; 409 IFNET_WUNLOCK(); 410 n = (oldlim << 1) * sizeof(*e); 411 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO); 412 IFNET_WLOCK(); 413 if (V_if_indexlim != oldlim) { 414 free(e, M_IFNET); 415 return; 416 } 417 if (V_ifindex_table != NULL) { 418 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2); 419 free((caddr_t)V_ifindex_table, M_IFNET); 420 } 421 V_if_indexlim <<= 1; 422 V_ifindex_table = e; 423} 424 425/* 426 * Allocate a struct ifnet and an index for an interface. A layer 2 427 * common structure will also be allocated if an allocation routine is 428 * registered for the passed type. 429 */ 430struct ifnet * 431if_alloc(u_char type) 432{ 433 struct ifnet *ifp; 434 u_short idx; 435 436 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO); 437 IFNET_WLOCK(); 438 if (ifindex_alloc_locked(&idx) != 0) { 439 IFNET_WUNLOCK(); 440 free(ifp, M_IFNET); 441 return (NULL); 442 } 443 ifnet_setbyindex_locked(idx, IFNET_HOLD); 444 IFNET_WUNLOCK(); 445 ifp->if_index = idx; 446 ifp->if_type = type; 447 ifp->if_alloctype = type; 448 if (if_com_alloc[type] != NULL) { 449 ifp->if_l2com = if_com_alloc[type](type, ifp); 450 if (ifp->if_l2com == NULL) { 451 free(ifp, M_IFNET); 452 ifindex_free(idx); 453 return (NULL); 454 } 455 } 456 457 IF_ADDR_LOCK_INIT(ifp); 458 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp); 459 ifp->if_afdata_initialized = 0; 460 IF_AFDATA_LOCK_INIT(ifp); 461 TAILQ_INIT(&ifp->if_addrhead); 462 TAILQ_INIT(&ifp->if_multiaddrs); 463 TAILQ_INIT(&ifp->if_groups); 464#ifdef MAC 465 mac_ifnet_init(ifp); 466#endif 467 ifq_init(&ifp->if_snd, ifp); 468 469 refcount_init(&ifp->if_refcount, 1); /* Index reference. */ 470 ifnet_setbyindex(ifp->if_index, ifp); 471 return (ifp); 472} 473 474/* 475 * Do the actual work of freeing a struct ifnet, and layer 2 common 476 * structure. This call is made when the last reference to an 477 * interface is released. 478 */ 479static void 480if_free_internal(struct ifnet *ifp) 481{ 482 483 KASSERT((ifp->if_flags & IFF_DYING), 484 ("if_free_internal: interface not dying")); 485 486 if (if_com_free[ifp->if_alloctype] != NULL) 487 if_com_free[ifp->if_alloctype](ifp->if_l2com, 488 ifp->if_alloctype); 489 490#ifdef MAC 491 mac_ifnet_destroy(ifp); 492#endif /* MAC */ 493 if (ifp->if_description != NULL) 494 free(ifp->if_description, M_IFDESCR); 495 IF_AFDATA_DESTROY(ifp); 496 IF_ADDR_LOCK_DESTROY(ifp); 497 ifq_delete(&ifp->if_snd); 498 free(ifp, M_IFNET); 499} 500 501/* 502 * Deregister an interface and free the associated storage. 503 */ 504void 505if_free(struct ifnet *ifp) 506{ 507 508 ifp->if_flags |= IFF_DYING; /* XXX: Locking */ 509 510 CURVNET_SET_QUIET(ifp->if_vnet); 511 IFNET_WLOCK(); 512 KASSERT(ifp == ifnet_byindex_locked(ifp->if_index), 513 ("%s: freeing unallocated ifnet", ifp->if_xname)); 514 515 ifindex_free_locked(ifp->if_index); 516 IFNET_WUNLOCK(); 517 518 if (refcount_release(&ifp->if_refcount)) 519 if_free_internal(ifp); 520 CURVNET_RESTORE(); 521} 522 523/* 524 * Interfaces to keep an ifnet type-stable despite the possibility of the 525 * driver calling if_free(). If there are additional references, we defer 526 * freeing the underlying data structure. 527 */ 528void 529if_ref(struct ifnet *ifp) 530{ 531 532 /* We don't assert the ifnet list lock here, but arguably should. */ 533 refcount_acquire(&ifp->if_refcount); 534} 535 536void 537if_rele(struct ifnet *ifp) 538{ 539 540 if (!refcount_release(&ifp->if_refcount)) 541 return; 542 if_free_internal(ifp); 543} 544 545void 546ifq_init(struct ifaltq *ifq, struct ifnet *ifp) 547{ 548 549 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF); 550 551 if (ifq->ifq_maxlen == 0) 552 ifq->ifq_maxlen = ifqmaxlen; 553 554 ifq->altq_type = 0; 555 ifq->altq_disc = NULL; 556 ifq->altq_flags &= ALTQF_CANTCHANGE; 557 ifq->altq_tbr = NULL; 558 ifq->altq_ifp = ifp; 559} 560 561void 562ifq_delete(struct ifaltq *ifq) 563{ 564 mtx_destroy(&ifq->ifq_mtx); 565} 566 567/* 568 * Perform generic interface initalization tasks and attach the interface 569 * to the list of "active" interfaces. If vmove flag is set on entry 570 * to if_attach_internal(), perform only a limited subset of initialization 571 * tasks, given that we are moving from one vnet to another an ifnet which 572 * has already been fully initialized. 573 * 574 * Note that if_detach_internal() removes group membership unconditionally 575 * even when vmove flag is set, and if_attach_internal() adds only IFG_ALL. 576 * Thus, when if_vmove() is applied to a cloned interface, group membership 577 * is lost while a cloned one always joins a group whose name is 578 * ifc->ifc_name. To recover this after if_detach_internal() and 579 * if_attach_internal(), the cloner should be specified to 580 * if_attach_internal() via ifc. If it is non-NULL, if_attach_internal() 581 * attempts to join a group whose name is ifc->ifc_name. 582 * 583 * XXX: 584 * - The decision to return void and thus require this function to 585 * succeed is questionable. 586 * - We should probably do more sanity checking. For instance we don't 587 * do anything to insure if_xname is unique or non-empty. 588 */ 589void 590if_attach(struct ifnet *ifp) 591{ 592 593 if_attach_internal(ifp, 0, NULL); 594} 595 596/* 597 * Compute the least common TSO limit. 598 */ 599void 600if_hw_tsomax_common(struct ifnet *ifp, struct ifnet_hw_tsomax *pmax) 601{ 602 /* 603 * 1) If there is no limit currently, take the limit from 604 * the network adapter. 605 * 606 * 2) If the network adapter has a limit below the current 607 * limit, apply it. 608 */ 609 if (pmax->tsomaxbytes == 0 || (ifp->if_hw_tsomax != 0 && 610 ifp->if_hw_tsomax < pmax->tsomaxbytes)) { 611 pmax->tsomaxbytes = ifp->if_hw_tsomax; 612 } 613 if (pmax->tsomaxsegcount == 0 || (ifp->if_hw_tsomaxsegcount != 0 && 614 ifp->if_hw_tsomaxsegcount < pmax->tsomaxsegcount)) { 615 pmax->tsomaxsegcount = ifp->if_hw_tsomaxsegcount; 616 } 617 if (pmax->tsomaxsegsize == 0 || (ifp->if_hw_tsomaxsegsize != 0 && 618 ifp->if_hw_tsomaxsegsize < pmax->tsomaxsegsize)) { 619 pmax->tsomaxsegsize = ifp->if_hw_tsomaxsegsize; 620 } 621} 622 623/* 624 * Update TSO limit of a network adapter. 625 * 626 * Returns zero if no change. Else non-zero. 627 */ 628int 629if_hw_tsomax_update(struct ifnet *ifp, struct ifnet_hw_tsomax *pmax) 630{ 631 int retval = 0; 632 if (ifp->if_hw_tsomax != pmax->tsomaxbytes) { 633 ifp->if_hw_tsomax = pmax->tsomaxbytes; 634 retval++; 635 } 636 if (ifp->if_hw_tsomaxsegsize != pmax->tsomaxsegsize) { 637 ifp->if_hw_tsomaxsegsize = pmax->tsomaxsegsize; 638 retval++; 639 } 640 if (ifp->if_hw_tsomaxsegcount != pmax->tsomaxsegcount) { 641 ifp->if_hw_tsomaxsegcount = pmax->tsomaxsegcount; 642 retval++; 643 } 644 return (retval); 645} 646 647static void 648if_attach_internal(struct ifnet *ifp, int vmove, struct if_clone *ifc) 649{ 650 unsigned socksize, ifasize; 651 int namelen, masklen; 652 struct sockaddr_dl *sdl; 653 struct ifaddr *ifa; 654 655 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index)) 656 panic ("%s: BUG: if_attach called without if_alloc'd input()\n", 657 ifp->if_xname); 658 659#ifdef VIMAGE 660 ifp->if_vnet = curvnet; 661 if (ifp->if_home_vnet == NULL) 662 ifp->if_home_vnet = curvnet; 663#endif 664 665 if_addgroup(ifp, IFG_ALL); 666 667 /* Restore group membership for cloned interfaces. */ 668 if (vmove && ifc != NULL) 669 if_clone_addgroup(ifp, ifc); 670 671 getmicrotime(&ifp->if_lastchange); 672 ifp->if_data.ifi_epoch = time_uptime; 673 ifp->if_data.ifi_datalen = sizeof(struct if_data); 674 675 KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) || 676 (ifp->if_transmit != NULL && ifp->if_qflush != NULL), 677 ("transmit and qflush must both either be set or both be NULL")); 678 if (ifp->if_transmit == NULL) { 679 ifp->if_transmit = if_transmit; 680 ifp->if_qflush = if_qflush; 681 } 682 if (ifp->if_input == NULL) 683 ifp->if_input = if_input_default; 684 685 if (!vmove) { 686#ifdef MAC 687 mac_ifnet_create(ifp); 688#endif 689 690 /* 691 * Create a Link Level name for this device. 692 */ 693 namelen = strlen(ifp->if_xname); 694 /* 695 * Always save enough space for any possiable name so we 696 * can do a rename in place later. 697 */ 698 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ; 699 socksize = masklen + ifp->if_addrlen; 700 if (socksize < sizeof(*sdl)) 701 socksize = sizeof(*sdl); 702 socksize = roundup2(socksize, sizeof(long)); 703 ifasize = sizeof(*ifa) + 2 * socksize; 704 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO); 705 ifa_init(ifa); 706 sdl = (struct sockaddr_dl *)(ifa + 1); 707 sdl->sdl_len = socksize; 708 sdl->sdl_family = AF_LINK; 709 bcopy(ifp->if_xname, sdl->sdl_data, namelen); 710 sdl->sdl_nlen = namelen; 711 sdl->sdl_index = ifp->if_index; 712 sdl->sdl_type = ifp->if_type; 713 ifp->if_addr = ifa; 714 ifa->ifa_ifp = ifp; 715 ifa->ifa_rtrequest = link_rtrequest; 716 ifa->ifa_addr = (struct sockaddr *)sdl; 717 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 718 ifa->ifa_netmask = (struct sockaddr *)sdl; 719 sdl->sdl_len = masklen; 720 while (namelen != 0) 721 sdl->sdl_data[--namelen] = 0xff; 722 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link); 723 /* Reliably crash if used uninitialized. */ 724 ifp->if_broadcastaddr = NULL; 725 726#if defined(INET) || defined(INET6) 727 /* Use defaults for TSO, if nothing is set */ 728 if (ifp->if_hw_tsomax == 0 && 729 ifp->if_hw_tsomaxsegcount == 0 && 730 ifp->if_hw_tsomaxsegsize == 0) { 731 /* 732 * The TSO defaults needs to be such that an 733 * NFS mbuf list of 35 mbufs totalling just 734 * below 64K works and that a chain of mbufs 735 * can be defragged into at most 32 segments: 736 */ 737 ifp->if_hw_tsomax = min(IP_MAXPACKET, (32 * MCLBYTES) - 738 (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN)); 739 ifp->if_hw_tsomaxsegcount = 35; 740 ifp->if_hw_tsomaxsegsize = 2048; /* 2K */ 741 742 /* XXX some drivers set IFCAP_TSO after ethernet attach */ 743 if (ifp->if_capabilities & IFCAP_TSO) { 744 if_printf(ifp, "Using defaults for TSO: %u/%u/%u\n", 745 ifp->if_hw_tsomax, 746 ifp->if_hw_tsomaxsegcount, 747 ifp->if_hw_tsomaxsegsize); 748 } 749 } 750#endif 751 } 752#ifdef VIMAGE 753 else { 754 /* 755 * Update the interface index in the link layer address 756 * of the interface. 757 */ 758 for (ifa = ifp->if_addr; ifa != NULL; 759 ifa = TAILQ_NEXT(ifa, ifa_link)) { 760 if (ifa->ifa_addr->sa_family == AF_LINK) { 761 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 762 sdl->sdl_index = ifp->if_index; 763 } 764 } 765 } 766#endif 767 768 IFNET_WLOCK(); 769 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link); 770#ifdef VIMAGE 771 curvnet->vnet_ifcnt++; 772#endif 773 IFNET_WUNLOCK(); 774 775 if (domain_init_status >= 2) 776 if_attachdomain1(ifp); 777 778 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 779 if (IS_DEFAULT_VNET(curvnet)) 780 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL); 781 782 /* Announce the interface. */ 783 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 784} 785 786static void 787if_attachdomain(void *dummy) 788{ 789 struct ifnet *ifp; 790 791 TAILQ_FOREACH(ifp, &V_ifnet, if_link) 792 if_attachdomain1(ifp); 793} 794SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND, 795 if_attachdomain, NULL); 796 797static void 798if_attachdomain1(struct ifnet *ifp) 799{ 800 struct domain *dp; 801 802 /* 803 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we 804 * cannot lock ifp->if_afdata initialization, entirely. 805 */ 806 if (IF_AFDATA_TRYLOCK(ifp) == 0) 807 return; 808 if (ifp->if_afdata_initialized >= domain_init_status) { 809 IF_AFDATA_UNLOCK(ifp); 810 log(LOG_WARNING, "%s called more than once on %s\n", 811 __func__, ifp->if_xname); 812 return; 813 } 814 ifp->if_afdata_initialized = domain_init_status; 815 IF_AFDATA_UNLOCK(ifp); 816 817 /* address family dependent data region */ 818 bzero(ifp->if_afdata, sizeof(ifp->if_afdata)); 819 for (dp = domains; dp; dp = dp->dom_next) { 820 if (dp->dom_ifattach) 821 ifp->if_afdata[dp->dom_family] = 822 (*dp->dom_ifattach)(ifp); 823 } 824} 825 826/* 827 * Remove any unicast or broadcast network addresses from an interface. 828 */ 829void 830if_purgeaddrs(struct ifnet *ifp) 831{ 832 struct ifaddr *ifa, *next; 833 834 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) { 835 if (ifa->ifa_addr->sa_family == AF_LINK) 836 continue; 837#ifdef INET 838 /* XXX: Ugly!! ad hoc just for INET */ 839 if (ifa->ifa_addr->sa_family == AF_INET) { 840 struct ifaliasreq ifr; 841 842 bzero(&ifr, sizeof(ifr)); 843 ifr.ifra_addr = *ifa->ifa_addr; 844 if (ifa->ifa_dstaddr) 845 ifr.ifra_broadaddr = *ifa->ifa_dstaddr; 846 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp, 847 NULL) == 0) 848 continue; 849 } 850#endif /* INET */ 851#ifdef INET6 852 if (ifa->ifa_addr->sa_family == AF_INET6) { 853 in6_purgeaddr(ifa); 854 /* ifp_addrhead is already updated */ 855 continue; 856 } 857#endif /* INET6 */ 858 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 859 ifa_free(ifa); 860 } 861} 862 863/* 864 * Remove any multicast network addresses from an interface when an ifnet 865 * is going away. 866 */ 867static void 868if_purgemaddrs(struct ifnet *ifp) 869{ 870 struct ifmultiaddr *ifma; 871 struct ifmultiaddr *next; 872 873 IF_ADDR_WLOCK(ifp); 874 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) 875 if_delmulti_locked(ifp, ifma, 1); 876 IF_ADDR_WUNLOCK(ifp); 877} 878 879/* 880 * Detach an interface, removing it from the list of "active" interfaces. 881 * If vmove flag is set on entry to if_detach_internal(), perform only a 882 * limited subset of cleanup tasks, given that we are moving an ifnet from 883 * one vnet to another, where it must be fully operational. 884 * 885 * XXXRW: There are some significant questions about event ordering, and 886 * how to prevent things from starting to use the interface during detach. 887 */ 888void 889if_detach(struct ifnet *ifp) 890{ 891 892 CURVNET_SET_QUIET(ifp->if_vnet); 893 if_detach_internal(ifp, 0, NULL); 894 CURVNET_RESTORE(); 895} 896 897static int 898if_detach_internal(struct ifnet *ifp, int vmove, struct if_clone **ifcp) 899{ 900 struct ifaddr *ifa; 901 struct radix_node_head *rnh; 902 int i, j; 903 struct domain *dp; 904 struct ifnet *iter; 905 int found = 0; 906 907 IFNET_WLOCK(); 908 TAILQ_FOREACH(iter, &V_ifnet, if_link) 909 if (iter == ifp) { 910 TAILQ_REMOVE(&V_ifnet, ifp, if_link); 911 found = 1; 912 break; 913 } 914#ifdef VIMAGE 915 if (found) 916 curvnet->vnet_ifcnt--; 917#endif 918 IFNET_WUNLOCK(); 919 if (!found) { 920 /* 921 * While we would want to panic here, we cannot 922 * guarantee that the interface is indeed still on 923 * the list given we don't hold locks all the way. 924 */ 925 return (ENOENT); 926#if 0 927 if (vmove) 928 panic("%s: ifp=%p not on the ifnet tailq %p", 929 __func__, ifp, &V_ifnet); 930 else 931 return; /* XXX this should panic as well? */ 932#endif 933 } 934 935 /* Check if this is a cloned interface or not. */ 936 if (vmove && ifcp != NULL) 937 *ifcp = if_clone_findifc(ifp); 938 939 /* 940 * Remove/wait for pending events. 941 */ 942 taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 943 944 /* 945 * Remove routes and flush queues. 946 */ 947 if_down(ifp); 948#ifdef ALTQ 949 if (ALTQ_IS_ENABLED(&ifp->if_snd)) 950 altq_disable(&ifp->if_snd); 951 if (ALTQ_IS_ATTACHED(&ifp->if_snd)) 952 altq_detach(&ifp->if_snd); 953#endif 954 955 if_purgeaddrs(ifp); 956 957#ifdef INET 958 in_ifdetach(ifp); 959#endif 960 961#ifdef INET6 962 /* 963 * Remove all IPv6 kernel structs related to ifp. This should be done 964 * before removing routing entries below, since IPv6 interface direct 965 * routes are expected to be removed by the IPv6-specific kernel API. 966 * Otherwise, the kernel will detect some inconsistency and bark it. 967 */ 968 in6_ifdetach(ifp); 969#endif 970 if_purgemaddrs(ifp); 971 972 if (!vmove) { 973 /* 974 * Prevent further calls into the device driver via ifnet. 975 */ 976 if_dead(ifp); 977 978 /* 979 * Remove link ifaddr pointer and maybe decrement if_index. 980 * Clean up all addresses. 981 */ 982 ifp->if_addr = NULL; 983 984 /* We can now free link ifaddr. */ 985 if (!TAILQ_EMPTY(&ifp->if_addrhead)) { 986 ifa = TAILQ_FIRST(&ifp->if_addrhead); 987 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 988 ifa_free(ifa); 989 } 990 } 991 992 /* 993 * Delete all remaining routes using this interface 994 * Unfortuneatly the only way to do this is to slog through 995 * the entire routing table looking for routes which point 996 * to this interface...oh well... 997 */ 998 for (i = 1; i <= AF_MAX; i++) { 999 for (j = 0; j < rt_numfibs; j++) { 1000 rnh = rt_tables_get_rnh(j, i); 1001 if (rnh == NULL) 1002 continue; 1003 RADIX_NODE_HEAD_LOCK(rnh); 1004 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp); 1005 RADIX_NODE_HEAD_UNLOCK(rnh); 1006 } 1007 } 1008 1009 /* Announce that the interface is gone. */ 1010 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 1011 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 1012 if (IS_DEFAULT_VNET(curvnet)) 1013 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL); 1014 if_delgroups(ifp); 1015 1016 /* 1017 * We cannot hold the lock over dom_ifdetach calls as they might 1018 * sleep, for example trying to drain a callout, thus open up the 1019 * theoretical race with re-attaching. 1020 */ 1021 IF_AFDATA_LOCK(ifp); 1022 i = ifp->if_afdata_initialized; 1023 ifp->if_afdata_initialized = 0; 1024 IF_AFDATA_UNLOCK(ifp); 1025 for (dp = domains; i > 0 && dp; dp = dp->dom_next) { 1026 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) 1027 (*dp->dom_ifdetach)(ifp, 1028 ifp->if_afdata[dp->dom_family]); 1029 } 1030 1031 return (0); 1032} 1033 1034#ifdef VIMAGE 1035/* 1036 * if_vmove() performs a limited version of if_detach() in current 1037 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg. 1038 * An attempt is made to shrink if_index in current vnet, find an 1039 * unused if_index in target vnet and calls if_grow() if necessary, 1040 * and finally find an unused if_xname for the target vnet. 1041 */ 1042void 1043if_vmove(struct ifnet *ifp, struct vnet *new_vnet) 1044{ 1045 u_short idx; 1046 struct if_clone *ifc; 1047 int rc; 1048 1049 /* 1050 * Detach from current vnet, but preserve LLADDR info, do not 1051 * mark as dead etc. so that the ifnet can be reattached later. 1052 * If we cannot find it, we lost the race to someone else. 1053 */ 1054 rc = if_detach_internal(ifp, 1, &ifc); 1055 if (rc != 0) 1056 return; 1057 1058 /* 1059 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink 1060 * the if_index for that vnet if possible. 1061 * 1062 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized, 1063 * or we'd lock on one vnet and unlock on another. 1064 */ 1065 IFNET_WLOCK(); 1066 ifindex_free_locked(ifp->if_index); 1067 IFNET_WUNLOCK(); 1068 1069 /* 1070 * Perform interface-specific reassignment tasks, if provided by 1071 * the driver. 1072 */ 1073 if (ifp->if_reassign != NULL) 1074 ifp->if_reassign(ifp, new_vnet, NULL); 1075 1076 /* 1077 * Switch to the context of the target vnet. 1078 */ 1079 CURVNET_SET_QUIET(new_vnet); 1080 1081 IFNET_WLOCK(); 1082 if (ifindex_alloc_locked(&idx) != 0) { 1083 IFNET_WUNLOCK(); 1084 panic("if_index overflow"); 1085 } 1086 ifp->if_index = idx; 1087 ifnet_setbyindex_locked(ifp->if_index, ifp); 1088 IFNET_WUNLOCK(); 1089 1090 if_attach_internal(ifp, 1, ifc); 1091 1092 CURVNET_RESTORE(); 1093} 1094 1095/* 1096 * Move an ifnet to or from another child prison/vnet, specified by the jail id. 1097 */ 1098static int 1099if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid) 1100{ 1101 struct prison *pr; 1102 struct ifnet *difp; 1103 1104 /* Try to find the prison within our visibility. */ 1105 sx_slock(&allprison_lock); 1106 pr = prison_find_child(td->td_ucred->cr_prison, jid); 1107 sx_sunlock(&allprison_lock); 1108 if (pr == NULL) 1109 return (ENXIO); 1110 prison_hold_locked(pr); 1111 mtx_unlock(&pr->pr_mtx); 1112 1113 /* Do not try to move the iface from and to the same prison. */ 1114 if (pr->pr_vnet == ifp->if_vnet) { 1115 prison_free(pr); 1116 return (EEXIST); 1117 } 1118 1119 /* Make sure the named iface does not exists in the dst. prison/vnet. */ 1120 /* XXX Lock interfaces to avoid races. */ 1121 CURVNET_SET_QUIET(pr->pr_vnet); 1122 difp = ifunit(ifname); 1123 CURVNET_RESTORE(); 1124 if (difp != NULL) { 1125 prison_free(pr); 1126 return (EEXIST); 1127 } 1128 1129 /* Move the interface into the child jail/vnet. */ 1130 if_vmove(ifp, pr->pr_vnet); 1131 1132 /* Report the new if_xname back to the userland. */ 1133 sprintf(ifname, "%s", ifp->if_xname); 1134 1135 prison_free(pr); 1136 return (0); 1137} 1138 1139static int 1140if_vmove_reclaim(struct thread *td, char *ifname, int jid) 1141{ 1142 struct prison *pr; 1143 struct vnet *vnet_dst; 1144 struct ifnet *ifp; 1145 1146 /* Try to find the prison within our visibility. */ 1147 sx_slock(&allprison_lock); 1148 pr = prison_find_child(td->td_ucred->cr_prison, jid); 1149 sx_sunlock(&allprison_lock); 1150 if (pr == NULL) 1151 return (ENXIO); 1152 prison_hold_locked(pr); 1153 mtx_unlock(&pr->pr_mtx); 1154 1155 /* Make sure the named iface exists in the source prison/vnet. */ 1156 CURVNET_SET(pr->pr_vnet); 1157 ifp = ifunit(ifname); /* XXX Lock to avoid races. */ 1158 if (ifp == NULL) { 1159 CURVNET_RESTORE(); 1160 prison_free(pr); 1161 return (ENXIO); 1162 } 1163 1164 /* Do not try to move the iface from and to the same prison. */ 1165 vnet_dst = TD_TO_VNET(td); 1166 if (vnet_dst == ifp->if_vnet) { 1167 CURVNET_RESTORE(); 1168 prison_free(pr); 1169 return (EEXIST); 1170 } 1171 1172 /* Get interface back from child jail/vnet. */ 1173 if_vmove(ifp, vnet_dst); 1174 CURVNET_RESTORE(); 1175 1176 /* Report the new if_xname back to the userland. */ 1177 sprintf(ifname, "%s", ifp->if_xname); 1178 1179 prison_free(pr); 1180 return (0); 1181} 1182#endif /* VIMAGE */ 1183 1184/* 1185 * Add a group to an interface 1186 */ 1187int 1188if_addgroup(struct ifnet *ifp, const char *groupname) 1189{ 1190 struct ifg_list *ifgl; 1191 struct ifg_group *ifg = NULL; 1192 struct ifg_member *ifgm; 1193 int new = 0; 1194 1195 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' && 1196 groupname[strlen(groupname) - 1] <= '9') 1197 return (EINVAL); 1198 1199 IFNET_WLOCK(); 1200 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 1201 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) { 1202 IFNET_WUNLOCK(); 1203 return (EEXIST); 1204 } 1205 1206 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP, 1207 M_NOWAIT)) == NULL) { 1208 IFNET_WUNLOCK(); 1209 return (ENOMEM); 1210 } 1211 1212 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member), 1213 M_TEMP, M_NOWAIT)) == NULL) { 1214 free(ifgl, M_TEMP); 1215 IFNET_WUNLOCK(); 1216 return (ENOMEM); 1217 } 1218 1219 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 1220 if (!strcmp(ifg->ifg_group, groupname)) 1221 break; 1222 1223 if (ifg == NULL) { 1224 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group), 1225 M_TEMP, M_NOWAIT)) == NULL) { 1226 free(ifgl, M_TEMP); 1227 free(ifgm, M_TEMP); 1228 IFNET_WUNLOCK(); 1229 return (ENOMEM); 1230 } 1231 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group)); 1232 ifg->ifg_refcnt = 0; 1233 TAILQ_INIT(&ifg->ifg_members); 1234 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next); 1235 new = 1; 1236 } 1237 1238 ifg->ifg_refcnt++; 1239 ifgl->ifgl_group = ifg; 1240 ifgm->ifgm_ifp = ifp; 1241 1242 IF_ADDR_WLOCK(ifp); 1243 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next); 1244 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next); 1245 IF_ADDR_WUNLOCK(ifp); 1246 1247 IFNET_WUNLOCK(); 1248 1249 if (new) 1250 EVENTHANDLER_INVOKE(group_attach_event, ifg); 1251 EVENTHANDLER_INVOKE(group_change_event, groupname); 1252 1253 return (0); 1254} 1255 1256/* 1257 * Remove a group from an interface 1258 */ 1259int 1260if_delgroup(struct ifnet *ifp, const char *groupname) 1261{ 1262 struct ifg_list *ifgl; 1263 struct ifg_member *ifgm; 1264 1265 IFNET_WLOCK(); 1266 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 1267 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) 1268 break; 1269 if (ifgl == NULL) { 1270 IFNET_WUNLOCK(); 1271 return (ENOENT); 1272 } 1273 1274 IF_ADDR_WLOCK(ifp); 1275 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next); 1276 IF_ADDR_WUNLOCK(ifp); 1277 1278 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) 1279 if (ifgm->ifgm_ifp == ifp) 1280 break; 1281 1282 if (ifgm != NULL) { 1283 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next); 1284 free(ifgm, M_TEMP); 1285 } 1286 1287 if (--ifgl->ifgl_group->ifg_refcnt == 0) { 1288 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next); 1289 IFNET_WUNLOCK(); 1290 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group); 1291 free(ifgl->ifgl_group, M_TEMP); 1292 } else 1293 IFNET_WUNLOCK(); 1294 1295 free(ifgl, M_TEMP); 1296 1297 EVENTHANDLER_INVOKE(group_change_event, groupname); 1298 1299 return (0); 1300} 1301 1302/* 1303 * Remove an interface from all groups 1304 */ 1305static void 1306if_delgroups(struct ifnet *ifp) 1307{ 1308 struct ifg_list *ifgl; 1309 struct ifg_member *ifgm; 1310 char groupname[IFNAMSIZ]; 1311 1312 IFNET_WLOCK(); 1313 while (!TAILQ_EMPTY(&ifp->if_groups)) { 1314 ifgl = TAILQ_FIRST(&ifp->if_groups); 1315 1316 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ); 1317 1318 IF_ADDR_WLOCK(ifp); 1319 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next); 1320 IF_ADDR_WUNLOCK(ifp); 1321 1322 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) 1323 if (ifgm->ifgm_ifp == ifp) 1324 break; 1325 1326 if (ifgm != NULL) { 1327 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, 1328 ifgm_next); 1329 free(ifgm, M_TEMP); 1330 } 1331 1332 if (--ifgl->ifgl_group->ifg_refcnt == 0) { 1333 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next); 1334 IFNET_WUNLOCK(); 1335 EVENTHANDLER_INVOKE(group_detach_event, 1336 ifgl->ifgl_group); 1337 free(ifgl->ifgl_group, M_TEMP); 1338 } else 1339 IFNET_WUNLOCK(); 1340 1341 free(ifgl, M_TEMP); 1342 1343 EVENTHANDLER_INVOKE(group_change_event, groupname); 1344 1345 IFNET_WLOCK(); 1346 } 1347 IFNET_WUNLOCK(); 1348} 1349 1350/* 1351 * Stores all groups from an interface in memory pointed 1352 * to by data 1353 */ 1354static int 1355if_getgroup(struct ifgroupreq *data, struct ifnet *ifp) 1356{ 1357 int len, error; 1358 struct ifg_list *ifgl; 1359 struct ifg_req ifgrq, *ifgp; 1360 struct ifgroupreq *ifgr = data; 1361 1362 if (ifgr->ifgr_len == 0) { 1363 IF_ADDR_RLOCK(ifp); 1364 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 1365 ifgr->ifgr_len += sizeof(struct ifg_req); 1366 IF_ADDR_RUNLOCK(ifp); 1367 return (0); 1368 } 1369 1370 len = ifgr->ifgr_len; 1371 ifgp = ifgr->ifgr_groups; 1372 /* XXX: wire */ 1373 IF_ADDR_RLOCK(ifp); 1374 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) { 1375 if (len < sizeof(ifgrq)) { 1376 IF_ADDR_RUNLOCK(ifp); 1377 return (EINVAL); 1378 } 1379 bzero(&ifgrq, sizeof ifgrq); 1380 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group, 1381 sizeof(ifgrq.ifgrq_group)); 1382 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 1383 IF_ADDR_RUNLOCK(ifp); 1384 return (error); 1385 } 1386 len -= sizeof(ifgrq); 1387 ifgp++; 1388 } 1389 IF_ADDR_RUNLOCK(ifp); 1390 1391 return (0); 1392} 1393 1394/* 1395 * Stores all members of a group in memory pointed to by data 1396 */ 1397static int 1398if_getgroupmembers(struct ifgroupreq *data) 1399{ 1400 struct ifgroupreq *ifgr = data; 1401 struct ifg_group *ifg; 1402 struct ifg_member *ifgm; 1403 struct ifg_req ifgrq, *ifgp; 1404 int len, error; 1405 1406 IFNET_RLOCK(); 1407 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 1408 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name)) 1409 break; 1410 if (ifg == NULL) { 1411 IFNET_RUNLOCK(); 1412 return (ENOENT); 1413 } 1414 1415 if (ifgr->ifgr_len == 0) { 1416 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) 1417 ifgr->ifgr_len += sizeof(ifgrq); 1418 IFNET_RUNLOCK(); 1419 return (0); 1420 } 1421 1422 len = ifgr->ifgr_len; 1423 ifgp = ifgr->ifgr_groups; 1424 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) { 1425 if (len < sizeof(ifgrq)) { 1426 IFNET_RUNLOCK(); 1427 return (EINVAL); 1428 } 1429 bzero(&ifgrq, sizeof ifgrq); 1430 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname, 1431 sizeof(ifgrq.ifgrq_member)); 1432 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 1433 IFNET_RUNLOCK(); 1434 return (error); 1435 } 1436 len -= sizeof(ifgrq); 1437 ifgp++; 1438 } 1439 IFNET_RUNLOCK(); 1440 1441 return (0); 1442} 1443 1444/* 1445 * Delete Routes for a Network Interface 1446 * 1447 * Called for each routing entry via the rnh->rnh_walktree() call above 1448 * to delete all route entries referencing a detaching network interface. 1449 * 1450 * Arguments: 1451 * rn pointer to node in the routing table 1452 * arg argument passed to rnh->rnh_walktree() - detaching interface 1453 * 1454 * Returns: 1455 * 0 successful 1456 * errno failed - reason indicated 1457 * 1458 */ 1459static int 1460if_rtdel(struct radix_node *rn, void *arg) 1461{ 1462 struct rtentry *rt = (struct rtentry *)rn; 1463 struct ifnet *ifp = arg; 1464 int err; 1465 1466 if (rt->rt_ifp == ifp) { 1467 1468 /* 1469 * Protect (sorta) against walktree recursion problems 1470 * with cloned routes 1471 */ 1472 if ((rt->rt_flags & RTF_UP) == 0) 1473 return (0); 1474 1475 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway, 1476 rt_mask(rt), 1477 rt->rt_flags|RTF_RNH_LOCKED|RTF_PINNED, 1478 (struct rtentry **) NULL, rt->rt_fibnum); 1479 if (err) { 1480 log(LOG_WARNING, "if_rtdel: error %d\n", err); 1481 } 1482 } 1483 1484 return (0); 1485} 1486 1487/* 1488 * A compatibility function returns ifnet counter values. 1489 */ 1490uint64_t 1491if_get_counter_default(struct ifnet *ifp, ift_counter cnt) 1492{ 1493 1494 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt)); 1495 switch (cnt) { 1496 case IFCOUNTER_IPACKETS: 1497 return (ifp->if_ipackets); 1498 case IFCOUNTER_IERRORS: 1499 return (ifp->if_ierrors); 1500 case IFCOUNTER_OPACKETS: 1501 return (ifp->if_opackets); 1502 case IFCOUNTER_OERRORS: 1503 return (ifp->if_oerrors); 1504 case IFCOUNTER_COLLISIONS: 1505 return (ifp->if_collisions); 1506 case IFCOUNTER_IBYTES: 1507 return (ifp->if_ibytes); 1508 case IFCOUNTER_OBYTES: 1509 return (ifp->if_obytes); 1510 case IFCOUNTER_IMCASTS: 1511 return (ifp->if_imcasts); 1512 case IFCOUNTER_OMCASTS: 1513 return (ifp->if_omcasts); 1514 case IFCOUNTER_IQDROPS: 1515 return (ifp->if_iqdrops); 1516#ifdef _IFI_OQDROPS 1517 case IFCOUNTER_OQDROPS: 1518 return (ifp->if_oqdrops); 1519#endif 1520 case IFCOUNTER_NOPROTO: 1521 return (ifp->if_noproto); 1522 default: 1523 break; 1524 }; 1525 return (0); 1526} 1527 1528/* 1529 * Increase an ifnet counter. Usually used for counters shared 1530 * between the stack and a driver, but function supports them all. 1531 */ 1532void 1533if_inc_counter(struct ifnet *ifp, ift_counter cnt, int64_t inc) 1534{ 1535 1536 KASSERT(cnt < IFCOUNTERS, ("%s: invalid cnt %d", __func__, cnt)); 1537 switch (cnt) { 1538 case IFCOUNTER_IPACKETS: 1539 ifp->if_ipackets += inc; 1540 break; 1541 case IFCOUNTER_IERRORS: 1542 ifp->if_ierrors += inc; 1543 break; 1544 case IFCOUNTER_OPACKETS: 1545 ifp->if_opackets += inc; 1546 break; 1547 case IFCOUNTER_OERRORS: 1548 ifp->if_oerrors += inc; 1549 break; 1550 case IFCOUNTER_COLLISIONS: 1551 ifp->if_collisions += inc; 1552 break; 1553 case IFCOUNTER_IBYTES: 1554 ifp->if_ibytes += inc; 1555 break; 1556 case IFCOUNTER_OBYTES: 1557 ifp->if_obytes += inc; 1558 break; 1559 case IFCOUNTER_IMCASTS: 1560 ifp->if_imcasts += inc; 1561 break; 1562 case IFCOUNTER_OMCASTS: 1563 ifp->if_omcasts += inc; 1564 break; 1565 case IFCOUNTER_IQDROPS: 1566 ifp->if_iqdrops += inc; 1567 break; 1568#ifdef _IFI_OQDROPS 1569 case IFCOUNTER_OQDROPS: 1570 ifp->if_oqdrops += inc; 1571 break; 1572#endif 1573 case IFCOUNTER_NOPROTO: 1574 ifp->if_noproto += inc; 1575 break; 1576 default: 1577 break; 1578 }; 1579} 1580 1581/* 1582 * Wrapper functions for struct ifnet address list locking macros. These are 1583 * used by kernel modules to avoid encoding programming interface or binary 1584 * interface assumptions that may be violated when kernel-internal locking 1585 * approaches change. 1586 */ 1587void 1588if_addr_rlock(struct ifnet *ifp) 1589{ 1590 1591 IF_ADDR_RLOCK(ifp); 1592} 1593 1594void 1595if_addr_runlock(struct ifnet *ifp) 1596{ 1597 1598 IF_ADDR_RUNLOCK(ifp); 1599} 1600 1601void 1602if_maddr_rlock(struct ifnet *ifp) 1603{ 1604 1605 IF_ADDR_RLOCK(ifp); 1606} 1607 1608void 1609if_maddr_runlock(struct ifnet *ifp) 1610{ 1611 1612 IF_ADDR_RUNLOCK(ifp); 1613} 1614 1615/* 1616 * Initialization, destruction and refcounting functions for ifaddrs. 1617 */ 1618void 1619ifa_init(struct ifaddr *ifa) 1620{ 1621 1622 mtx_init(&ifa->ifa_mtx, "ifaddr", NULL, MTX_DEF); 1623 refcount_init(&ifa->ifa_refcnt, 1); 1624 ifa->if_data.ifi_datalen = sizeof(ifa->if_data); 1625} 1626 1627void 1628ifa_ref(struct ifaddr *ifa) 1629{ 1630 1631 refcount_acquire(&ifa->ifa_refcnt); 1632} 1633 1634void 1635ifa_free(struct ifaddr *ifa) 1636{ 1637 1638 if (refcount_release(&ifa->ifa_refcnt)) { 1639 mtx_destroy(&ifa->ifa_mtx); 1640 free(ifa, M_IFADDR); 1641 } 1642} 1643 1644int 1645ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia) 1646{ 1647 int error = 0; 1648 struct rtentry *rt = NULL; 1649 struct rt_addrinfo info; 1650 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 1651 1652 bzero(&info, sizeof(info)); 1653 info.rti_ifp = V_loif; 1654 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC; 1655 info.rti_info[RTAX_DST] = ia; 1656 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl; 1657 error = rtrequest1_fib(RTM_ADD, &info, &rt, ifa->ifa_ifp->if_fib); 1658 1659 if (error == 0 && rt != NULL) { 1660 RT_LOCK(rt); 1661 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type = 1662 ifa->ifa_ifp->if_type; 1663 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index = 1664 ifa->ifa_ifp->if_index; 1665 RT_REMREF(rt); 1666 RT_UNLOCK(rt); 1667 } else if (error != 0) 1668 log(LOG_DEBUG, "%s: insertion failed: %u\n", __func__, error); 1669 1670 return (error); 1671} 1672 1673int 1674ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia) 1675{ 1676 int error = 0; 1677 struct rt_addrinfo info; 1678 struct sockaddr_dl null_sdl; 1679 1680 bzero(&null_sdl, sizeof(null_sdl)); 1681 null_sdl.sdl_len = sizeof(null_sdl); 1682 null_sdl.sdl_family = AF_LINK; 1683 null_sdl.sdl_type = ifa->ifa_ifp->if_type; 1684 null_sdl.sdl_index = ifa->ifa_ifp->if_index; 1685 bzero(&info, sizeof(info)); 1686 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC; 1687 info.rti_info[RTAX_DST] = ia; 1688 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl; 1689 error = rtrequest1_fib(RTM_DELETE, &info, NULL, ifa->ifa_ifp->if_fib); 1690 1691 if (error != 0) 1692 log(LOG_DEBUG, "%s: deletion failed: %u\n", __func__, error); 1693 1694 return (error); 1695} 1696 1697/* 1698 * XXX: Because sockaddr_dl has deeper structure than the sockaddr 1699 * structs used to represent other address families, it is necessary 1700 * to perform a different comparison. 1701 */ 1702 1703#define sa_equal(a1, a2) \ 1704 (bcmp((a1), (a2), ((a1))->sa_len) == 0) 1705 1706#define sa_dl_equal(a1, a2) \ 1707 ((((struct sockaddr_dl *)(a1))->sdl_len == \ 1708 ((struct sockaddr_dl *)(a2))->sdl_len) && \ 1709 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \ 1710 LLADDR((struct sockaddr_dl *)(a2)), \ 1711 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0)) 1712 1713/* 1714 * Locate an interface based on a complete address. 1715 */ 1716/*ARGSUSED*/ 1717static struct ifaddr * 1718ifa_ifwithaddr_internal(struct sockaddr *addr, int getref) 1719{ 1720 struct ifnet *ifp; 1721 struct ifaddr *ifa; 1722 1723 IFNET_RLOCK_NOSLEEP(); 1724 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1725 IF_ADDR_RLOCK(ifp); 1726 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1727 if (ifa->ifa_addr->sa_family != addr->sa_family) 1728 continue; 1729 if (sa_equal(addr, ifa->ifa_addr)) { 1730 if (getref) 1731 ifa_ref(ifa); 1732 IF_ADDR_RUNLOCK(ifp); 1733 goto done; 1734 } 1735 /* IP6 doesn't have broadcast */ 1736 if ((ifp->if_flags & IFF_BROADCAST) && 1737 ifa->ifa_broadaddr && 1738 ifa->ifa_broadaddr->sa_len != 0 && 1739 sa_equal(ifa->ifa_broadaddr, addr)) { 1740 if (getref) 1741 ifa_ref(ifa); 1742 IF_ADDR_RUNLOCK(ifp); 1743 goto done; 1744 } 1745 } 1746 IF_ADDR_RUNLOCK(ifp); 1747 } 1748 ifa = NULL; 1749done: 1750 IFNET_RUNLOCK_NOSLEEP(); 1751 return (ifa); 1752} 1753 1754struct ifaddr * 1755ifa_ifwithaddr(struct sockaddr *addr) 1756{ 1757 1758 return (ifa_ifwithaddr_internal(addr, 1)); 1759} 1760 1761int 1762ifa_ifwithaddr_check(struct sockaddr *addr) 1763{ 1764 1765 return (ifa_ifwithaddr_internal(addr, 0) != NULL); 1766} 1767 1768/* 1769 * Locate an interface based on the broadcast address. 1770 */ 1771/* ARGSUSED */ 1772struct ifaddr * 1773ifa_ifwithbroadaddr(struct sockaddr *addr) 1774{ 1775 struct ifnet *ifp; 1776 struct ifaddr *ifa; 1777 1778 IFNET_RLOCK_NOSLEEP(); 1779 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1780 IF_ADDR_RLOCK(ifp); 1781 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1782 if (ifa->ifa_addr->sa_family != addr->sa_family) 1783 continue; 1784 if ((ifp->if_flags & IFF_BROADCAST) && 1785 ifa->ifa_broadaddr && 1786 ifa->ifa_broadaddr->sa_len != 0 && 1787 sa_equal(ifa->ifa_broadaddr, addr)) { 1788 ifa_ref(ifa); 1789 IF_ADDR_RUNLOCK(ifp); 1790 goto done; 1791 } 1792 } 1793 IF_ADDR_RUNLOCK(ifp); 1794 } 1795 ifa = NULL; 1796done: 1797 IFNET_RUNLOCK_NOSLEEP(); 1798 return (ifa); 1799} 1800 1801/* 1802 * Locate the point to point interface with a given destination address. 1803 */ 1804/*ARGSUSED*/ 1805struct ifaddr * 1806ifa_ifwithdstaddr_fib(struct sockaddr *addr, int fibnum) 1807{ 1808 struct ifnet *ifp; 1809 struct ifaddr *ifa; 1810 1811 IFNET_RLOCK_NOSLEEP(); 1812 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1813 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 1814 continue; 1815 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum)) 1816 continue; 1817 IF_ADDR_RLOCK(ifp); 1818 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1819 if (ifa->ifa_addr->sa_family != addr->sa_family) 1820 continue; 1821 if (ifa->ifa_dstaddr != NULL && 1822 sa_equal(addr, ifa->ifa_dstaddr)) { 1823 ifa_ref(ifa); 1824 IF_ADDR_RUNLOCK(ifp); 1825 goto done; 1826 } 1827 } 1828 IF_ADDR_RUNLOCK(ifp); 1829 } 1830 ifa = NULL; 1831done: 1832 IFNET_RUNLOCK_NOSLEEP(); 1833 return (ifa); 1834} 1835 1836struct ifaddr * 1837ifa_ifwithdstaddr(struct sockaddr *addr) 1838{ 1839 1840 return (ifa_ifwithdstaddr_fib(addr, RT_ALL_FIBS)); 1841} 1842 1843/* 1844 * Find an interface on a specific network. If many, choice 1845 * is most specific found. 1846 */ 1847struct ifaddr * 1848ifa_ifwithnet_fib(struct sockaddr *addr, int ignore_ptp, int fibnum) 1849{ 1850 struct ifnet *ifp; 1851 struct ifaddr *ifa; 1852 struct ifaddr *ifa_maybe = NULL; 1853 u_int af = addr->sa_family; 1854 char *addr_data = addr->sa_data, *cplim; 1855 1856 /* 1857 * AF_LINK addresses can be looked up directly by their index number, 1858 * so do that if we can. 1859 */ 1860 if (af == AF_LINK) { 1861 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; 1862 if (sdl->sdl_index && sdl->sdl_index <= V_if_index) 1863 return (ifaddr_byindex(sdl->sdl_index)); 1864 } 1865 1866 /* 1867 * Scan though each interface, looking for ones that have addresses 1868 * in this address family and the requested fib. Maintain a reference 1869 * on ifa_maybe once we find one, as we release the IF_ADDR_RLOCK() that 1870 * kept it stable when we move onto the next interface. 1871 */ 1872 IFNET_RLOCK_NOSLEEP(); 1873 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1874 if ((fibnum != RT_ALL_FIBS) && (ifp->if_fib != fibnum)) 1875 continue; 1876 IF_ADDR_RLOCK(ifp); 1877 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1878 char *cp, *cp2, *cp3; 1879 1880 if (ifa->ifa_addr->sa_family != af) 1881next: continue; 1882 if (af == AF_INET && 1883 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) { 1884 /* 1885 * This is a bit broken as it doesn't 1886 * take into account that the remote end may 1887 * be a single node in the network we are 1888 * looking for. 1889 * The trouble is that we don't know the 1890 * netmask for the remote end. 1891 */ 1892 if (ifa->ifa_dstaddr != NULL && 1893 sa_equal(addr, ifa->ifa_dstaddr)) { 1894 ifa_ref(ifa); 1895 IF_ADDR_RUNLOCK(ifp); 1896 goto done; 1897 } 1898 } else { 1899 /* 1900 * if we have a special address handler, 1901 * then use it instead of the generic one. 1902 */ 1903 if (ifa->ifa_claim_addr) { 1904 if ((*ifa->ifa_claim_addr)(ifa, addr)) { 1905 ifa_ref(ifa); 1906 IF_ADDR_RUNLOCK(ifp); 1907 goto done; 1908 } 1909 continue; 1910 } 1911 1912 /* 1913 * Scan all the bits in the ifa's address. 1914 * If a bit dissagrees with what we are 1915 * looking for, mask it with the netmask 1916 * to see if it really matters. 1917 * (A byte at a time) 1918 */ 1919 if (ifa->ifa_netmask == 0) 1920 continue; 1921 cp = addr_data; 1922 cp2 = ifa->ifa_addr->sa_data; 1923 cp3 = ifa->ifa_netmask->sa_data; 1924 cplim = ifa->ifa_netmask->sa_len 1925 + (char *)ifa->ifa_netmask; 1926 while (cp3 < cplim) 1927 if ((*cp++ ^ *cp2++) & *cp3++) 1928 goto next; /* next address! */ 1929 /* 1930 * If the netmask of what we just found 1931 * is more specific than what we had before 1932 * (if we had one), or if the virtual status 1933 * of new prefix is better than of the old one, 1934 * then remember the new one before continuing 1935 * to search for an even better one. 1936 */ 1937 if (ifa_maybe == NULL || 1938 ifa_preferred(ifa_maybe, ifa) || 1939 rn_refines((caddr_t)ifa->ifa_netmask, 1940 (caddr_t)ifa_maybe->ifa_netmask)) { 1941 if (ifa_maybe != NULL) 1942 ifa_free(ifa_maybe); 1943 ifa_maybe = ifa; 1944 ifa_ref(ifa_maybe); 1945 } 1946 } 1947 } 1948 IF_ADDR_RUNLOCK(ifp); 1949 } 1950 ifa = ifa_maybe; 1951 ifa_maybe = NULL; 1952done: 1953 IFNET_RUNLOCK_NOSLEEP(); 1954 if (ifa_maybe != NULL) 1955 ifa_free(ifa_maybe); 1956 return (ifa); 1957} 1958 1959struct ifaddr * 1960ifa_ifwithnet(struct sockaddr *addr, int ignore_ptp) 1961{ 1962 1963 return (ifa_ifwithnet_fib(addr, ignore_ptp, RT_ALL_FIBS)); 1964} 1965 1966/* 1967 * Find an interface address specific to an interface best matching 1968 * a given address. 1969 */ 1970struct ifaddr * 1971ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp) 1972{ 1973 struct ifaddr *ifa; 1974 char *cp, *cp2, *cp3; 1975 char *cplim; 1976 struct ifaddr *ifa_maybe = NULL; 1977 u_int af = addr->sa_family; 1978 1979 if (af >= AF_MAX) 1980 return (NULL); 1981 IF_ADDR_RLOCK(ifp); 1982 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1983 if (ifa->ifa_addr->sa_family != af) 1984 continue; 1985 if (ifa_maybe == NULL) 1986 ifa_maybe = ifa; 1987 if (ifa->ifa_netmask == 0) { 1988 if (sa_equal(addr, ifa->ifa_addr) || 1989 (ifa->ifa_dstaddr && 1990 sa_equal(addr, ifa->ifa_dstaddr))) 1991 goto done; 1992 continue; 1993 } 1994 if (ifp->if_flags & IFF_POINTOPOINT) { 1995 if (sa_equal(addr, ifa->ifa_dstaddr)) 1996 goto done; 1997 } else { 1998 cp = addr->sa_data; 1999 cp2 = ifa->ifa_addr->sa_data; 2000 cp3 = ifa->ifa_netmask->sa_data; 2001 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 2002 for (; cp3 < cplim; cp3++) 2003 if ((*cp++ ^ *cp2++) & *cp3) 2004 break; 2005 if (cp3 == cplim) 2006 goto done; 2007 } 2008 } 2009 ifa = ifa_maybe; 2010done: 2011 if (ifa != NULL) 2012 ifa_ref(ifa); 2013 IF_ADDR_RUNLOCK(ifp); 2014 return (ifa); 2015} 2016 2017/* 2018 * See whether new ifa is better than current one: 2019 * 1) A non-virtual one is preferred over virtual. 2020 * 2) A virtual in master state preferred over any other state. 2021 * 2022 * Used in several address selecting functions. 2023 */ 2024int 2025ifa_preferred(struct ifaddr *cur, struct ifaddr *next) 2026{ 2027 2028 return (cur->ifa_carp && (!next->ifa_carp || 2029 ((*carp_master_p)(next) && !(*carp_master_p)(cur)))); 2030} 2031 2032#include <net/if_llatbl.h> 2033 2034/* 2035 * Default action when installing a route with a Link Level gateway. 2036 * Lookup an appropriate real ifa to point to. 2037 * This should be moved to /sys/net/link.c eventually. 2038 */ 2039static void 2040link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 2041{ 2042 struct ifaddr *ifa, *oifa; 2043 struct sockaddr *dst; 2044 struct ifnet *ifp; 2045 2046 RT_LOCK_ASSERT(rt); 2047 2048 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 2049 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 2050 return; 2051 ifa = ifaof_ifpforaddr(dst, ifp); 2052 if (ifa) { 2053 oifa = rt->rt_ifa; 2054 rt->rt_ifa = ifa; 2055 ifa_free(oifa); 2056 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 2057 ifa->ifa_rtrequest(cmd, rt, info); 2058 } 2059} 2060 2061/* 2062 * Mark an interface down and notify protocols of 2063 * the transition. 2064 */ 2065static void 2066if_unroute(struct ifnet *ifp, int flag, int fam) 2067{ 2068 struct ifaddr *ifa; 2069 2070 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP")); 2071 2072 ifp->if_flags &= ~flag; 2073 getmicrotime(&ifp->if_lastchange); 2074 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 2075 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 2076 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 2077 ifp->if_qflush(ifp); 2078 2079 if (ifp->if_carp) 2080 (*carp_linkstate_p)(ifp); 2081 rt_ifmsg(ifp); 2082} 2083 2084/* 2085 * Mark an interface up and notify protocols of 2086 * the transition. 2087 */ 2088static void 2089if_route(struct ifnet *ifp, int flag, int fam) 2090{ 2091 struct ifaddr *ifa; 2092 2093 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP")); 2094 2095 ifp->if_flags |= flag; 2096 getmicrotime(&ifp->if_lastchange); 2097 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 2098 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 2099 pfctlinput(PRC_IFUP, ifa->ifa_addr); 2100 if (ifp->if_carp) 2101 (*carp_linkstate_p)(ifp); 2102 rt_ifmsg(ifp); 2103#ifdef INET6 2104 in6_if_up(ifp); 2105#endif 2106} 2107 2108void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */ 2109void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */ 2110struct ifnet *(*vlan_trunkdev_p)(struct ifnet *); 2111struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t); 2112int (*vlan_tag_p)(struct ifnet *, uint16_t *); 2113int (*vlan_setcookie_p)(struct ifnet *, void *); 2114void *(*vlan_cookie_p)(struct ifnet *); 2115 2116/* 2117 * Handle a change in the interface link state. To avoid LORs 2118 * between driver lock and upper layer locks, as well as possible 2119 * recursions, we post event to taskqueue, and all job 2120 * is done in static do_link_state_change(). 2121 */ 2122void 2123if_link_state_change(struct ifnet *ifp, int link_state) 2124{ 2125 /* Return if state hasn't changed. */ 2126 if (ifp->if_link_state == link_state) 2127 return; 2128 2129 ifp->if_link_state = link_state; 2130 2131 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask); 2132} 2133 2134static void 2135do_link_state_change(void *arg, int pending) 2136{ 2137 struct ifnet *ifp = (struct ifnet *)arg; 2138 int link_state = ifp->if_link_state; 2139 CURVNET_SET(ifp->if_vnet); 2140 2141 /* Notify that the link state has changed. */ 2142 rt_ifmsg(ifp); 2143 if (ifp->if_vlantrunk != NULL) 2144 (*vlan_link_state_p)(ifp); 2145 2146 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) && 2147 IFP2AC(ifp)->ac_netgraph != NULL) 2148 (*ng_ether_link_state_p)(ifp, link_state); 2149 if (ifp->if_carp) 2150 (*carp_linkstate_p)(ifp); 2151 if (ifp->if_bridge) 2152 (*bridge_linkstate_p)(ifp); 2153 if (ifp->if_lagg) 2154 (*lagg_linkstate_p)(ifp, link_state); 2155 2156 if (IS_DEFAULT_VNET(curvnet)) 2157 devctl_notify("IFNET", ifp->if_xname, 2158 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", 2159 NULL); 2160 if (pending > 1) 2161 if_printf(ifp, "%d link states coalesced\n", pending); 2162 if (log_link_state_change) 2163 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname, 2164 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" ); 2165 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, ifp->if_link_state); 2166 CURVNET_RESTORE(); 2167} 2168 2169/* 2170 * Mark an interface down and notify protocols of 2171 * the transition. 2172 */ 2173void 2174if_down(struct ifnet *ifp) 2175{ 2176 2177 if_unroute(ifp, IFF_UP, AF_UNSPEC); 2178} 2179 2180/* 2181 * Mark an interface up and notify protocols of 2182 * the transition. 2183 */ 2184void 2185if_up(struct ifnet *ifp) 2186{ 2187 2188 if_route(ifp, IFF_UP, AF_UNSPEC); 2189} 2190 2191/* 2192 * Flush an interface queue. 2193 */ 2194void 2195if_qflush(struct ifnet *ifp) 2196{ 2197 struct mbuf *m, *n; 2198 struct ifaltq *ifq; 2199 2200 ifq = &ifp->if_snd; 2201 IFQ_LOCK(ifq); 2202#ifdef ALTQ 2203 if (ALTQ_IS_ENABLED(ifq)) 2204 ALTQ_PURGE(ifq); 2205#endif 2206 n = ifq->ifq_head; 2207 while ((m = n) != 0) { 2208 n = m->m_nextpkt; 2209 m_freem(m); 2210 } 2211 ifq->ifq_head = 0; 2212 ifq->ifq_tail = 0; 2213 ifq->ifq_len = 0; 2214 IFQ_UNLOCK(ifq); 2215} 2216 2217/* 2218 * Map interface name to interface structure pointer, with or without 2219 * returning a reference. 2220 */ 2221struct ifnet * 2222ifunit_ref(const char *name) 2223{ 2224 struct ifnet *ifp; 2225 2226 IFNET_RLOCK_NOSLEEP(); 2227 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2228 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 && 2229 !(ifp->if_flags & IFF_DYING)) 2230 break; 2231 } 2232 if (ifp != NULL) 2233 if_ref(ifp); 2234 IFNET_RUNLOCK_NOSLEEP(); 2235 return (ifp); 2236} 2237 2238struct ifnet * 2239ifunit(const char *name) 2240{ 2241 struct ifnet *ifp; 2242 2243 IFNET_RLOCK_NOSLEEP(); 2244 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2245 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0) 2246 break; 2247 } 2248 IFNET_RUNLOCK_NOSLEEP(); 2249 return (ifp); 2250} 2251 2252/* 2253 * Hardware specific interface ioctls. 2254 */ 2255static int 2256ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td) 2257{ 2258 struct ifreq *ifr; 2259 struct ifstat *ifs; 2260 int error = 0; 2261 int new_flags, temp_flags; 2262 size_t namelen, onamelen; 2263 size_t descrlen; 2264 char *descrbuf, *odescrbuf; 2265 char new_name[IFNAMSIZ]; 2266 struct ifaddr *ifa; 2267 struct sockaddr_dl *sdl; 2268 2269 ifr = (struct ifreq *)data; 2270 switch (cmd) { 2271 case SIOCGIFINDEX: 2272 ifr->ifr_index = ifp->if_index; 2273 break; 2274 2275 case SIOCGIFFLAGS: 2276 temp_flags = ifp->if_flags | ifp->if_drv_flags; 2277 ifr->ifr_flags = temp_flags & 0xffff; 2278 ifr->ifr_flagshigh = temp_flags >> 16; 2279 break; 2280 2281 case SIOCGIFCAP: 2282 ifr->ifr_reqcap = ifp->if_capabilities; 2283 ifr->ifr_curcap = ifp->if_capenable; 2284 break; 2285 2286#ifdef MAC 2287 case SIOCGIFMAC: 2288 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp); 2289 break; 2290#endif 2291 2292 case SIOCGIFMETRIC: 2293 ifr->ifr_metric = ifp->if_metric; 2294 break; 2295 2296 case SIOCGIFMTU: 2297 ifr->ifr_mtu = ifp->if_mtu; 2298 break; 2299 2300 case SIOCGIFPHYS: 2301 ifr->ifr_phys = ifp->if_physical; 2302 break; 2303 2304 case SIOCGIFDESCR: 2305 error = 0; 2306 sx_slock(&ifdescr_sx); 2307 if (ifp->if_description == NULL) 2308 error = ENOMSG; 2309 else { 2310 /* space for terminating nul */ 2311 descrlen = strlen(ifp->if_description) + 1; 2312 if (ifr->ifr_buffer.length < descrlen) 2313 ifr->ifr_buffer.buffer = NULL; 2314 else 2315 error = copyout(ifp->if_description, 2316 ifr->ifr_buffer.buffer, descrlen); 2317 ifr->ifr_buffer.length = descrlen; 2318 } 2319 sx_sunlock(&ifdescr_sx); 2320 break; 2321 2322 case SIOCSIFDESCR: 2323 error = priv_check(td, PRIV_NET_SETIFDESCR); 2324 if (error) 2325 return (error); 2326 2327 /* 2328 * Copy only (length-1) bytes to make sure that 2329 * if_description is always nul terminated. The 2330 * length parameter is supposed to count the 2331 * terminating nul in. 2332 */ 2333 if (ifr->ifr_buffer.length > ifdescr_maxlen) 2334 return (ENAMETOOLONG); 2335 else if (ifr->ifr_buffer.length == 0) 2336 descrbuf = NULL; 2337 else { 2338 descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR, 2339 M_WAITOK | M_ZERO); 2340 error = copyin(ifr->ifr_buffer.buffer, descrbuf, 2341 ifr->ifr_buffer.length - 1); 2342 if (error) { 2343 free(descrbuf, M_IFDESCR); 2344 break; 2345 } 2346 } 2347 2348 sx_xlock(&ifdescr_sx); 2349 odescrbuf = ifp->if_description; 2350 ifp->if_description = descrbuf; 2351 sx_xunlock(&ifdescr_sx); 2352 2353 getmicrotime(&ifp->if_lastchange); 2354 free(odescrbuf, M_IFDESCR); 2355 break; 2356 2357 case SIOCGIFFIB: 2358 ifr->ifr_fib = ifp->if_fib; 2359 break; 2360 2361 case SIOCSIFFIB: 2362 error = priv_check(td, PRIV_NET_SETIFFIB); 2363 if (error) 2364 return (error); 2365 if (ifr->ifr_fib >= rt_numfibs) 2366 return (EINVAL); 2367 2368 ifp->if_fib = ifr->ifr_fib; 2369 break; 2370 2371 case SIOCSIFFLAGS: 2372 error = priv_check(td, PRIV_NET_SETIFFLAGS); 2373 if (error) 2374 return (error); 2375 /* 2376 * Currently, no driver owned flags pass the IFF_CANTCHANGE 2377 * check, so we don't need special handling here yet. 2378 */ 2379 new_flags = (ifr->ifr_flags & 0xffff) | 2380 (ifr->ifr_flagshigh << 16); 2381 if (ifp->if_flags & IFF_SMART) { 2382 /* Smart drivers twiddle their own routes */ 2383 } else if (ifp->if_flags & IFF_UP && 2384 (new_flags & IFF_UP) == 0) { 2385 if_down(ifp); 2386 } else if (new_flags & IFF_UP && 2387 (ifp->if_flags & IFF_UP) == 0) { 2388 if_up(ifp); 2389 } 2390 /* See if permanently promiscuous mode bit is about to flip */ 2391 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) { 2392 if (new_flags & IFF_PPROMISC) 2393 ifp->if_flags |= IFF_PROMISC; 2394 else if (ifp->if_pcount == 0) 2395 ifp->if_flags &= ~IFF_PROMISC; 2396 log(LOG_INFO, "%s: permanently promiscuous mode %s\n", 2397 ifp->if_xname, 2398 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled"); 2399 } 2400 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 2401 (new_flags &~ IFF_CANTCHANGE); 2402 if (ifp->if_ioctl) { 2403 (void) (*ifp->if_ioctl)(ifp, cmd, data); 2404 } 2405 getmicrotime(&ifp->if_lastchange); 2406 break; 2407 2408 case SIOCSIFCAP: 2409 error = priv_check(td, PRIV_NET_SETIFCAP); 2410 if (error) 2411 return (error); 2412 if (ifp->if_ioctl == NULL) 2413 return (EOPNOTSUPP); 2414 if (ifr->ifr_reqcap & ~ifp->if_capabilities) 2415 return (EINVAL); 2416 error = (*ifp->if_ioctl)(ifp, cmd, data); 2417 if (error == 0) 2418 getmicrotime(&ifp->if_lastchange); 2419 break; 2420 2421#ifdef MAC 2422 case SIOCSIFMAC: 2423 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp); 2424 break; 2425#endif 2426 2427 case SIOCSIFNAME: 2428 error = priv_check(td, PRIV_NET_SETIFNAME); 2429 if (error) 2430 return (error); 2431 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL); 2432 if (error != 0) 2433 return (error); 2434 if (new_name[0] == '\0') 2435 return (EINVAL); 2436 if (ifunit(new_name) != NULL) 2437 return (EEXIST); 2438 2439 /* 2440 * XXX: Locking. Nothing else seems to lock if_flags, 2441 * and there are numerous other races with the 2442 * ifunit() checks not being atomic with namespace 2443 * changes (renames, vmoves, if_attach, etc). 2444 */ 2445 ifp->if_flags |= IFF_RENAMING; 2446 2447 /* Announce the departure of the interface. */ 2448 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 2449 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 2450 2451 log(LOG_INFO, "%s: changing name to '%s'\n", 2452 ifp->if_xname, new_name); 2453 2454 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname)); 2455 ifa = ifp->if_addr; 2456 IFA_LOCK(ifa); 2457 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 2458 namelen = strlen(new_name); 2459 onamelen = sdl->sdl_nlen; 2460 /* 2461 * Move the address if needed. This is safe because we 2462 * allocate space for a name of length IFNAMSIZ when we 2463 * create this in if_attach(). 2464 */ 2465 if (namelen != onamelen) { 2466 bcopy(sdl->sdl_data + onamelen, 2467 sdl->sdl_data + namelen, sdl->sdl_alen); 2468 } 2469 bcopy(new_name, sdl->sdl_data, namelen); 2470 sdl->sdl_nlen = namelen; 2471 sdl = (struct sockaddr_dl *)ifa->ifa_netmask; 2472 bzero(sdl->sdl_data, onamelen); 2473 while (namelen != 0) 2474 sdl->sdl_data[--namelen] = 0xff; 2475 IFA_UNLOCK(ifa); 2476 2477 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 2478 /* Announce the return of the interface. */ 2479 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 2480 2481 ifp->if_flags &= ~IFF_RENAMING; 2482 break; 2483 2484#ifdef VIMAGE 2485 case SIOCSIFVNET: 2486 error = priv_check(td, PRIV_NET_SETIFVNET); 2487 if (error) 2488 return (error); 2489 error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid); 2490 break; 2491#endif 2492 2493 case SIOCSIFMETRIC: 2494 error = priv_check(td, PRIV_NET_SETIFMETRIC); 2495 if (error) 2496 return (error); 2497 ifp->if_metric = ifr->ifr_metric; 2498 getmicrotime(&ifp->if_lastchange); 2499 break; 2500 2501 case SIOCSIFPHYS: 2502 error = priv_check(td, PRIV_NET_SETIFPHYS); 2503 if (error) 2504 return (error); 2505 if (ifp->if_ioctl == NULL) 2506 return (EOPNOTSUPP); 2507 error = (*ifp->if_ioctl)(ifp, cmd, data); 2508 if (error == 0) 2509 getmicrotime(&ifp->if_lastchange); 2510 break; 2511 2512 case SIOCSIFMTU: 2513 { 2514 u_long oldmtu = ifp->if_mtu; 2515 2516 error = priv_check(td, PRIV_NET_SETIFMTU); 2517 if (error) 2518 return (error); 2519 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) 2520 return (EINVAL); 2521 if (ifp->if_ioctl == NULL) 2522 return (EOPNOTSUPP); 2523 error = (*ifp->if_ioctl)(ifp, cmd, data); 2524 if (error == 0) { 2525 getmicrotime(&ifp->if_lastchange); 2526 rt_ifmsg(ifp); 2527 } 2528 /* 2529 * If the link MTU changed, do network layer specific procedure. 2530 */ 2531 if (ifp->if_mtu != oldmtu) { 2532#ifdef INET6 2533 nd6_setmtu(ifp); 2534#endif 2535 } 2536 break; 2537 } 2538 2539 case SIOCADDMULTI: 2540 case SIOCDELMULTI: 2541 if (cmd == SIOCADDMULTI) 2542 error = priv_check(td, PRIV_NET_ADDMULTI); 2543 else 2544 error = priv_check(td, PRIV_NET_DELMULTI); 2545 if (error) 2546 return (error); 2547 2548 /* Don't allow group membership on non-multicast interfaces. */ 2549 if ((ifp->if_flags & IFF_MULTICAST) == 0) 2550 return (EOPNOTSUPP); 2551 2552 /* Don't let users screw up protocols' entries. */ 2553 if (ifr->ifr_addr.sa_family != AF_LINK) 2554 return (EINVAL); 2555 2556 if (cmd == SIOCADDMULTI) { 2557 struct ifmultiaddr *ifma; 2558 2559 /* 2560 * Userland is only permitted to join groups once 2561 * via the if_addmulti() KPI, because it cannot hold 2562 * struct ifmultiaddr * between calls. It may also 2563 * lose a race while we check if the membership 2564 * already exists. 2565 */ 2566 IF_ADDR_RLOCK(ifp); 2567 ifma = if_findmulti(ifp, &ifr->ifr_addr); 2568 IF_ADDR_RUNLOCK(ifp); 2569 if (ifma != NULL) 2570 error = EADDRINUSE; 2571 else 2572 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 2573 } else { 2574 error = if_delmulti(ifp, &ifr->ifr_addr); 2575 } 2576 if (error == 0) 2577 getmicrotime(&ifp->if_lastchange); 2578 break; 2579 2580 case SIOCSIFPHYADDR: 2581 case SIOCDIFPHYADDR: 2582#ifdef INET6 2583 case SIOCSIFPHYADDR_IN6: 2584#endif 2585 case SIOCSLIFPHYADDR: 2586 case SIOCSIFMEDIA: 2587 case SIOCSIFGENERIC: 2588 error = priv_check(td, PRIV_NET_HWIOCTL); 2589 if (error) 2590 return (error); 2591 if (ifp->if_ioctl == NULL) 2592 return (EOPNOTSUPP); 2593 error = (*ifp->if_ioctl)(ifp, cmd, data); 2594 if (error == 0) 2595 getmicrotime(&ifp->if_lastchange); 2596 break; 2597 2598 case SIOCGIFSTATUS: 2599 ifs = (struct ifstat *)data; 2600 ifs->ascii[0] = '\0'; 2601 2602 case SIOCGIFPSRCADDR: 2603 case SIOCGIFPDSTADDR: 2604 case SIOCGLIFPHYADDR: 2605 case SIOCGIFMEDIA: 2606 case SIOCGIFXMEDIA: 2607 case SIOCGIFGENERIC: 2608 if (ifp->if_ioctl == NULL) 2609 return (EOPNOTSUPP); 2610 error = (*ifp->if_ioctl)(ifp, cmd, data); 2611 break; 2612 2613 case SIOCSIFLLADDR: 2614 error = priv_check(td, PRIV_NET_SETLLADDR); 2615 if (error) 2616 return (error); 2617 error = if_setlladdr(ifp, 2618 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); 2619 EVENTHANDLER_INVOKE(iflladdr_event, ifp); 2620 break; 2621 2622 case SIOCAIFGROUP: 2623 { 2624 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; 2625 2626 error = priv_check(td, PRIV_NET_ADDIFGROUP); 2627 if (error) 2628 return (error); 2629 if ((error = if_addgroup(ifp, ifgr->ifgr_group))) 2630 return (error); 2631 break; 2632 } 2633 2634 case SIOCGIFGROUP: 2635 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp))) 2636 return (error); 2637 break; 2638 2639 case SIOCDIFGROUP: 2640 { 2641 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; 2642 2643 error = priv_check(td, PRIV_NET_DELIFGROUP); 2644 if (error) 2645 return (error); 2646 if ((error = if_delgroup(ifp, ifgr->ifgr_group))) 2647 return (error); 2648 break; 2649 } 2650 2651 default: 2652 error = ENOIOCTL; 2653 break; 2654 } 2655 return (error); 2656} 2657 2658#ifdef COMPAT_FREEBSD32 2659struct ifconf32 { 2660 int32_t ifc_len; 2661 union { 2662 uint32_t ifcu_buf; 2663 uint32_t ifcu_req; 2664 } ifc_ifcu; 2665}; 2666#define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32) 2667#endif 2668 2669/* 2670 * Interface ioctls. 2671 */ 2672int 2673ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td) 2674{ 2675 struct ifnet *ifp; 2676 struct ifreq *ifr; 2677 int error; 2678 int oif_flags; 2679 2680 CURVNET_SET(so->so_vnet); 2681 switch (cmd) { 2682 case SIOCGIFCONF: 2683 case OSIOCGIFCONF: 2684 error = ifconf(cmd, data); 2685 CURVNET_RESTORE(); 2686 return (error); 2687 2688#ifdef COMPAT_FREEBSD32 2689 case SIOCGIFCONF32: 2690 { 2691 struct ifconf32 *ifc32; 2692 struct ifconf ifc; 2693 2694 ifc32 = (struct ifconf32 *)data; 2695 ifc.ifc_len = ifc32->ifc_len; 2696 ifc.ifc_buf = PTRIN(ifc32->ifc_buf); 2697 2698 error = ifconf(SIOCGIFCONF, (void *)&ifc); 2699 CURVNET_RESTORE(); 2700 if (error == 0) 2701 ifc32->ifc_len = ifc.ifc_len; 2702 return (error); 2703 } 2704#endif 2705 } 2706 ifr = (struct ifreq *)data; 2707 2708 switch (cmd) { 2709#ifdef VIMAGE 2710 case SIOCSIFRVNET: 2711 error = priv_check(td, PRIV_NET_SETIFVNET); 2712 if (error == 0) 2713 error = if_vmove_reclaim(td, ifr->ifr_name, 2714 ifr->ifr_jid); 2715 CURVNET_RESTORE(); 2716 return (error); 2717#endif 2718 case SIOCIFCREATE: 2719 case SIOCIFCREATE2: 2720 error = priv_check(td, PRIV_NET_IFCREATE); 2721 if (error == 0) 2722 error = if_clone_create(ifr->ifr_name, 2723 sizeof(ifr->ifr_name), 2724 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL); 2725 CURVNET_RESTORE(); 2726 return (error); 2727 case SIOCIFDESTROY: 2728 error = priv_check(td, PRIV_NET_IFDESTROY); 2729 if (error == 0) 2730 error = if_clone_destroy(ifr->ifr_name); 2731 CURVNET_RESTORE(); 2732 return (error); 2733 2734 case SIOCIFGCLONERS: 2735 error = if_clone_list((struct if_clonereq *)data); 2736 CURVNET_RESTORE(); 2737 return (error); 2738 case SIOCGIFGMEMB: 2739 error = if_getgroupmembers((struct ifgroupreq *)data); 2740 CURVNET_RESTORE(); 2741 return (error); 2742#if defined(INET) || defined(INET6) 2743 case SIOCSVH: 2744 case SIOCGVH: 2745 if (carp_ioctl_p == NULL) 2746 error = EPROTONOSUPPORT; 2747 else 2748 error = (*carp_ioctl_p)(ifr, cmd, td); 2749 CURVNET_RESTORE(); 2750 return (error); 2751#endif 2752 } 2753 2754 ifp = ifunit_ref(ifr->ifr_name); 2755 if (ifp == NULL) { 2756 CURVNET_RESTORE(); 2757 return (ENXIO); 2758 } 2759 2760 error = ifhwioctl(cmd, ifp, data, td); 2761 if (error != ENOIOCTL) { 2762 if_rele(ifp); 2763 CURVNET_RESTORE(); 2764 return (error); 2765 } 2766 2767 oif_flags = ifp->if_flags; 2768 if (so->so_proto == NULL) { 2769 if_rele(ifp); 2770 CURVNET_RESTORE(); 2771 return (EOPNOTSUPP); 2772 } 2773 2774 /* 2775 * Pass the request on to the socket control method, and if the 2776 * latter returns EOPNOTSUPP, directly to the interface. 2777 * 2778 * Make an exception for the legacy SIOCSIF* requests. Drivers 2779 * trust SIOCSIFADDR et al to come from an already privileged 2780 * layer, and do not perform any credentials checks or input 2781 * validation. 2782 */ 2783#ifndef COMPAT_43 2784 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, 2785 data, 2786 ifp, td)); 2787 if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL && 2788 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR && 2789 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK) 2790 error = (*ifp->if_ioctl)(ifp, cmd, data); 2791#else 2792 { 2793 u_long ocmd = cmd; 2794 2795 switch (cmd) { 2796 2797 case SIOCSIFDSTADDR: 2798 case SIOCSIFADDR: 2799 case SIOCSIFBRDADDR: 2800 case SIOCSIFNETMASK: 2801#if BYTE_ORDER != BIG_ENDIAN 2802 if (ifr->ifr_addr.sa_family == 0 && 2803 ifr->ifr_addr.sa_len < 16) { 2804 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 2805 ifr->ifr_addr.sa_len = 16; 2806 } 2807#else 2808 if (ifr->ifr_addr.sa_len == 0) 2809 ifr->ifr_addr.sa_len = 16; 2810#endif 2811 break; 2812 2813 case OSIOCGIFADDR: 2814 cmd = SIOCGIFADDR; 2815 break; 2816 2817 case OSIOCGIFDSTADDR: 2818 cmd = SIOCGIFDSTADDR; 2819 break; 2820 2821 case OSIOCGIFBRDADDR: 2822 cmd = SIOCGIFBRDADDR; 2823 break; 2824 2825 case OSIOCGIFNETMASK: 2826 cmd = SIOCGIFNETMASK; 2827 } 2828 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, 2829 cmd, 2830 data, 2831 ifp, td)); 2832 if (error == EOPNOTSUPP && ifp != NULL && 2833 ifp->if_ioctl != NULL && 2834 cmd != SIOCSIFADDR && cmd != SIOCSIFBRDADDR && 2835 cmd != SIOCSIFDSTADDR && cmd != SIOCSIFNETMASK) 2836 error = (*ifp->if_ioctl)(ifp, cmd, data); 2837 switch (ocmd) { 2838 2839 case OSIOCGIFADDR: 2840 case OSIOCGIFDSTADDR: 2841 case OSIOCGIFBRDADDR: 2842 case OSIOCGIFNETMASK: 2843 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 2844 2845 } 2846 } 2847#endif /* COMPAT_43 */ 2848 2849 if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 2850#ifdef INET6 2851 if (ifp->if_flags & IFF_UP) 2852 in6_if_up(ifp); 2853#endif 2854 } 2855 if_rele(ifp); 2856 CURVNET_RESTORE(); 2857 return (error); 2858} 2859 2860/* 2861 * The code common to handling reference counted flags, 2862 * e.g., in ifpromisc() and if_allmulti(). 2863 * The "pflag" argument can specify a permanent mode flag to check, 2864 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none. 2865 * 2866 * Only to be used on stack-owned flags, not driver-owned flags. 2867 */ 2868static int 2869if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch) 2870{ 2871 struct ifreq ifr; 2872 int error; 2873 int oldflags, oldcount; 2874 2875 /* Sanity checks to catch programming errors */ 2876 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0, 2877 ("%s: setting driver-owned flag %d", __func__, flag)); 2878 2879 if (onswitch) 2880 KASSERT(*refcount >= 0, 2881 ("%s: increment negative refcount %d for flag %d", 2882 __func__, *refcount, flag)); 2883 else 2884 KASSERT(*refcount > 0, 2885 ("%s: decrement non-positive refcount %d for flag %d", 2886 __func__, *refcount, flag)); 2887 2888 /* In case this mode is permanent, just touch refcount */ 2889 if (ifp->if_flags & pflag) { 2890 *refcount += onswitch ? 1 : -1; 2891 return (0); 2892 } 2893 2894 /* Save ifnet parameters for if_ioctl() may fail */ 2895 oldcount = *refcount; 2896 oldflags = ifp->if_flags; 2897 2898 /* 2899 * See if we aren't the only and touching refcount is enough. 2900 * Actually toggle interface flag if we are the first or last. 2901 */ 2902 if (onswitch) { 2903 if ((*refcount)++) 2904 return (0); 2905 ifp->if_flags |= flag; 2906 } else { 2907 if (--(*refcount)) 2908 return (0); 2909 ifp->if_flags &= ~flag; 2910 } 2911 2912 /* Call down the driver since we've changed interface flags */ 2913 if (ifp->if_ioctl == NULL) { 2914 error = EOPNOTSUPP; 2915 goto recover; 2916 } 2917 ifr.ifr_flags = ifp->if_flags & 0xffff; 2918 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2919 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2920 if (error) 2921 goto recover; 2922 /* Notify userland that interface flags have changed */ 2923 rt_ifmsg(ifp); 2924 return (0); 2925 2926recover: 2927 /* Recover after driver error */ 2928 *refcount = oldcount; 2929 ifp->if_flags = oldflags; 2930 return (error); 2931} 2932 2933/* 2934 * Set/clear promiscuous mode on interface ifp based on the truth value 2935 * of pswitch. The calls are reference counted so that only the first 2936 * "on" request actually has an effect, as does the final "off" request. 2937 * Results are undefined if the "off" and "on" requests are not matched. 2938 */ 2939int 2940ifpromisc(struct ifnet *ifp, int pswitch) 2941{ 2942 int error; 2943 int oldflags = ifp->if_flags; 2944 2945 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC, 2946 &ifp->if_pcount, pswitch); 2947 /* If promiscuous mode status has changed, log a message */ 2948 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC)) 2949 log(LOG_INFO, "%s: promiscuous mode %s\n", 2950 ifp->if_xname, 2951 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); 2952 return (error); 2953} 2954 2955/* 2956 * Return interface configuration 2957 * of system. List may be used 2958 * in later ioctl's (above) to get 2959 * other information. 2960 */ 2961/*ARGSUSED*/ 2962static int 2963ifconf(u_long cmd, caddr_t data) 2964{ 2965 struct ifconf *ifc = (struct ifconf *)data; 2966 struct ifnet *ifp; 2967 struct ifaddr *ifa; 2968 struct ifreq ifr; 2969 struct sbuf *sb; 2970 int error, full = 0, valid_len, max_len; 2971 2972 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */ 2973 max_len = MAXPHYS - 1; 2974 2975 /* Prevent hostile input from being able to crash the system */ 2976 if (ifc->ifc_len <= 0) 2977 return (EINVAL); 2978 2979again: 2980 if (ifc->ifc_len <= max_len) { 2981 max_len = ifc->ifc_len; 2982 full = 1; 2983 } 2984 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN); 2985 max_len = 0; 2986 valid_len = 0; 2987 2988 IFNET_RLOCK(); 2989 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2990 int addrs; 2991 2992 /* 2993 * Zero the ifr_name buffer to make sure we don't 2994 * disclose the contents of the stack. 2995 */ 2996 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name)); 2997 2998 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)) 2999 >= sizeof(ifr.ifr_name)) { 3000 sbuf_delete(sb); 3001 IFNET_RUNLOCK(); 3002 return (ENAMETOOLONG); 3003 } 3004 3005 addrs = 0; 3006 IF_ADDR_RLOCK(ifp); 3007 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 3008 struct sockaddr *sa = ifa->ifa_addr; 3009 3010 if (prison_if(curthread->td_ucred, sa) != 0) 3011 continue; 3012 addrs++; 3013#ifdef COMPAT_43 3014 if (cmd == OSIOCGIFCONF) { 3015 struct osockaddr *osa = 3016 (struct osockaddr *)&ifr.ifr_addr; 3017 ifr.ifr_addr = *sa; 3018 osa->sa_family = sa->sa_family; 3019 sbuf_bcat(sb, &ifr, sizeof(ifr)); 3020 max_len += sizeof(ifr); 3021 } else 3022#endif 3023 if (sa->sa_len <= sizeof(*sa)) { 3024 ifr.ifr_addr = *sa; 3025 sbuf_bcat(sb, &ifr, sizeof(ifr)); 3026 max_len += sizeof(ifr); 3027 } else { 3028 sbuf_bcat(sb, &ifr, 3029 offsetof(struct ifreq, ifr_addr)); 3030 max_len += offsetof(struct ifreq, ifr_addr); 3031 sbuf_bcat(sb, sa, sa->sa_len); 3032 max_len += sa->sa_len; 3033 } 3034 3035 if (sbuf_error(sb) == 0) 3036 valid_len = sbuf_len(sb); 3037 } 3038 IF_ADDR_RUNLOCK(ifp); 3039 if (addrs == 0) { 3040 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 3041 sbuf_bcat(sb, &ifr, sizeof(ifr)); 3042 max_len += sizeof(ifr); 3043 3044 if (sbuf_error(sb) == 0) 3045 valid_len = sbuf_len(sb); 3046 } 3047 } 3048 IFNET_RUNLOCK(); 3049 3050 /* 3051 * If we didn't allocate enough space (uncommon), try again. If 3052 * we have already allocated as much space as we are allowed, 3053 * return what we've got. 3054 */ 3055 if (valid_len != max_len && !full) { 3056 sbuf_delete(sb); 3057 goto again; 3058 } 3059 3060 ifc->ifc_len = valid_len; 3061 sbuf_finish(sb); 3062 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len); 3063 sbuf_delete(sb); 3064 return (error); 3065} 3066 3067/* 3068 * Just like ifpromisc(), but for all-multicast-reception mode. 3069 */ 3070int 3071if_allmulti(struct ifnet *ifp, int onswitch) 3072{ 3073 3074 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch)); 3075} 3076 3077struct ifmultiaddr * 3078if_findmulti(struct ifnet *ifp, struct sockaddr *sa) 3079{ 3080 struct ifmultiaddr *ifma; 3081 3082 IF_ADDR_LOCK_ASSERT(ifp); 3083 3084 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 3085 if (sa->sa_family == AF_LINK) { 3086 if (sa_dl_equal(ifma->ifma_addr, sa)) 3087 break; 3088 } else { 3089 if (sa_equal(ifma->ifma_addr, sa)) 3090 break; 3091 } 3092 } 3093 3094 return ifma; 3095} 3096 3097/* 3098 * Allocate a new ifmultiaddr and initialize based on passed arguments. We 3099 * make copies of passed sockaddrs. The ifmultiaddr will not be added to 3100 * the ifnet multicast address list here, so the caller must do that and 3101 * other setup work (such as notifying the device driver). The reference 3102 * count is initialized to 1. 3103 */ 3104static struct ifmultiaddr * 3105if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa, 3106 int mflags) 3107{ 3108 struct ifmultiaddr *ifma; 3109 struct sockaddr *dupsa; 3110 3111 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags | 3112 M_ZERO); 3113 if (ifma == NULL) 3114 return (NULL); 3115 3116 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags); 3117 if (dupsa == NULL) { 3118 free(ifma, M_IFMADDR); 3119 return (NULL); 3120 } 3121 bcopy(sa, dupsa, sa->sa_len); 3122 ifma->ifma_addr = dupsa; 3123 3124 ifma->ifma_ifp = ifp; 3125 ifma->ifma_refcount = 1; 3126 ifma->ifma_protospec = NULL; 3127 3128 if (llsa == NULL) { 3129 ifma->ifma_lladdr = NULL; 3130 return (ifma); 3131 } 3132 3133 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags); 3134 if (dupsa == NULL) { 3135 free(ifma->ifma_addr, M_IFMADDR); 3136 free(ifma, M_IFMADDR); 3137 return (NULL); 3138 } 3139 bcopy(llsa, dupsa, llsa->sa_len); 3140 ifma->ifma_lladdr = dupsa; 3141 3142 return (ifma); 3143} 3144 3145/* 3146 * if_freemulti: free ifmultiaddr structure and possibly attached related 3147 * addresses. The caller is responsible for implementing reference 3148 * counting, notifying the driver, handling routing messages, and releasing 3149 * any dependent link layer state. 3150 */ 3151static void 3152if_freemulti(struct ifmultiaddr *ifma) 3153{ 3154 3155 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d", 3156 ifma->ifma_refcount)); 3157 KASSERT(ifma->ifma_protospec == NULL, 3158 ("if_freemulti: protospec not NULL")); 3159 3160 if (ifma->ifma_lladdr != NULL) 3161 free(ifma->ifma_lladdr, M_IFMADDR); 3162 free(ifma->ifma_addr, M_IFMADDR); 3163 free(ifma, M_IFMADDR); 3164} 3165 3166/* 3167 * Register an additional multicast address with a network interface. 3168 * 3169 * - If the address is already present, bump the reference count on the 3170 * address and return. 3171 * - If the address is not link-layer, look up a link layer address. 3172 * - Allocate address structures for one or both addresses, and attach to the 3173 * multicast address list on the interface. If automatically adding a link 3174 * layer address, the protocol address will own a reference to the link 3175 * layer address, to be freed when it is freed. 3176 * - Notify the network device driver of an addition to the multicast address 3177 * list. 3178 * 3179 * 'sa' points to caller-owned memory with the desired multicast address. 3180 * 3181 * 'retifma' will be used to return a pointer to the resulting multicast 3182 * address reference, if desired. 3183 */ 3184int 3185if_addmulti(struct ifnet *ifp, struct sockaddr *sa, 3186 struct ifmultiaddr **retifma) 3187{ 3188 struct ifmultiaddr *ifma, *ll_ifma; 3189 struct sockaddr *llsa; 3190 int error; 3191 3192 /* 3193 * If the address is already present, return a new reference to it; 3194 * otherwise, allocate storage and set up a new address. 3195 */ 3196 IF_ADDR_WLOCK(ifp); 3197 ifma = if_findmulti(ifp, sa); 3198 if (ifma != NULL) { 3199 ifma->ifma_refcount++; 3200 if (retifma != NULL) 3201 *retifma = ifma; 3202 IF_ADDR_WUNLOCK(ifp); 3203 return (0); 3204 } 3205 3206 /* 3207 * The address isn't already present; resolve the protocol address 3208 * into a link layer address, and then look that up, bump its 3209 * refcount or allocate an ifma for that also. If 'llsa' was 3210 * returned, we will need to free it later. 3211 */ 3212 llsa = NULL; 3213 ll_ifma = NULL; 3214 if (ifp->if_resolvemulti != NULL) { 3215 error = ifp->if_resolvemulti(ifp, &llsa, sa); 3216 if (error) 3217 goto unlock_out; 3218 } 3219 3220 /* 3221 * Allocate the new address. Don't hook it up yet, as we may also 3222 * need to allocate a link layer multicast address. 3223 */ 3224 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT); 3225 if (ifma == NULL) { 3226 error = ENOMEM; 3227 goto free_llsa_out; 3228 } 3229 3230 /* 3231 * If a link layer address is found, we'll need to see if it's 3232 * already present in the address list, or allocate is as well. 3233 * When this block finishes, the link layer address will be on the 3234 * list. 3235 */ 3236 if (llsa != NULL) { 3237 ll_ifma = if_findmulti(ifp, llsa); 3238 if (ll_ifma == NULL) { 3239 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT); 3240 if (ll_ifma == NULL) { 3241 --ifma->ifma_refcount; 3242 if_freemulti(ifma); 3243 error = ENOMEM; 3244 goto free_llsa_out; 3245 } 3246 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma, 3247 ifma_link); 3248 } else 3249 ll_ifma->ifma_refcount++; 3250 ifma->ifma_llifma = ll_ifma; 3251 } 3252 3253 /* 3254 * We now have a new multicast address, ifma, and possibly a new or 3255 * referenced link layer address. Add the primary address to the 3256 * ifnet address list. 3257 */ 3258 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 3259 3260 if (retifma != NULL) 3261 *retifma = ifma; 3262 3263 /* 3264 * Must generate the message while holding the lock so that 'ifma' 3265 * pointer is still valid. 3266 */ 3267 rt_newmaddrmsg(RTM_NEWMADDR, ifma); 3268 IF_ADDR_WUNLOCK(ifp); 3269 3270 /* 3271 * We are certain we have added something, so call down to the 3272 * interface to let them know about it. 3273 */ 3274 if (ifp->if_ioctl != NULL) { 3275 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0); 3276 } 3277 3278 if (llsa != NULL) 3279 free(llsa, M_IFMADDR); 3280 3281 return (0); 3282 3283free_llsa_out: 3284 if (llsa != NULL) 3285 free(llsa, M_IFMADDR); 3286 3287unlock_out: 3288 IF_ADDR_WUNLOCK(ifp); 3289 return (error); 3290} 3291 3292/* 3293 * Delete a multicast group membership by network-layer group address. 3294 * 3295 * Returns ENOENT if the entry could not be found. If ifp no longer 3296 * exists, results are undefined. This entry point should only be used 3297 * from subsystems which do appropriate locking to hold ifp for the 3298 * duration of the call. 3299 * Network-layer protocol domains must use if_delmulti_ifma(). 3300 */ 3301int 3302if_delmulti(struct ifnet *ifp, struct sockaddr *sa) 3303{ 3304 struct ifmultiaddr *ifma; 3305 int lastref; 3306#ifdef INVARIANTS 3307 struct ifnet *oifp; 3308 3309 IFNET_RLOCK_NOSLEEP(); 3310 TAILQ_FOREACH(oifp, &V_ifnet, if_link) 3311 if (ifp == oifp) 3312 break; 3313 if (ifp != oifp) 3314 ifp = NULL; 3315 IFNET_RUNLOCK_NOSLEEP(); 3316 3317 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__)); 3318#endif 3319 if (ifp == NULL) 3320 return (ENOENT); 3321 3322 IF_ADDR_WLOCK(ifp); 3323 lastref = 0; 3324 ifma = if_findmulti(ifp, sa); 3325 if (ifma != NULL) 3326 lastref = if_delmulti_locked(ifp, ifma, 0); 3327 IF_ADDR_WUNLOCK(ifp); 3328 3329 if (ifma == NULL) 3330 return (ENOENT); 3331 3332 if (lastref && ifp->if_ioctl != NULL) { 3333 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 3334 } 3335 3336 return (0); 3337} 3338 3339/* 3340 * Delete all multicast group membership for an interface. 3341 * Should be used to quickly flush all multicast filters. 3342 */ 3343void 3344if_delallmulti(struct ifnet *ifp) 3345{ 3346 struct ifmultiaddr *ifma; 3347 struct ifmultiaddr *next; 3348 3349 IF_ADDR_WLOCK(ifp); 3350 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) 3351 if_delmulti_locked(ifp, ifma, 0); 3352 IF_ADDR_WUNLOCK(ifp); 3353} 3354 3355/* 3356 * Delete a multicast group membership by group membership pointer. 3357 * Network-layer protocol domains must use this routine. 3358 * 3359 * It is safe to call this routine if the ifp disappeared. 3360 */ 3361void 3362if_delmulti_ifma(struct ifmultiaddr *ifma) 3363{ 3364 struct ifnet *ifp; 3365 int lastref; 3366 3367 ifp = ifma->ifma_ifp; 3368#ifdef DIAGNOSTIC 3369 if (ifp == NULL) { 3370 printf("%s: ifma_ifp seems to be detached\n", __func__); 3371 } else { 3372 struct ifnet *oifp; 3373 3374 IFNET_RLOCK_NOSLEEP(); 3375 TAILQ_FOREACH(oifp, &V_ifnet, if_link) 3376 if (ifp == oifp) 3377 break; 3378 if (ifp != oifp) { 3379 printf("%s: ifnet %p disappeared\n", __func__, ifp); 3380 ifp = NULL; 3381 } 3382 IFNET_RUNLOCK_NOSLEEP(); 3383 } 3384#endif 3385 /* 3386 * If and only if the ifnet instance exists: Acquire the address lock. 3387 */ 3388 if (ifp != NULL) 3389 IF_ADDR_WLOCK(ifp); 3390 3391 lastref = if_delmulti_locked(ifp, ifma, 0); 3392 3393 if (ifp != NULL) { 3394 /* 3395 * If and only if the ifnet instance exists: 3396 * Release the address lock. 3397 * If the group was left: update the hardware hash filter. 3398 */ 3399 IF_ADDR_WUNLOCK(ifp); 3400 if (lastref && ifp->if_ioctl != NULL) { 3401 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 3402 } 3403 } 3404} 3405 3406/* 3407 * Perform deletion of network-layer and/or link-layer multicast address. 3408 * 3409 * Return 0 if the reference count was decremented. 3410 * Return 1 if the final reference was released, indicating that the 3411 * hardware hash filter should be reprogrammed. 3412 */ 3413static int 3414if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching) 3415{ 3416 struct ifmultiaddr *ll_ifma; 3417 3418 if (ifp != NULL && ifma->ifma_ifp != NULL) { 3419 KASSERT(ifma->ifma_ifp == ifp, 3420 ("%s: inconsistent ifp %p", __func__, ifp)); 3421 IF_ADDR_WLOCK_ASSERT(ifp); 3422 } 3423 3424 ifp = ifma->ifma_ifp; 3425 3426 /* 3427 * If the ifnet is detaching, null out references to ifnet, 3428 * so that upper protocol layers will notice, and not attempt 3429 * to obtain locks for an ifnet which no longer exists. The 3430 * routing socket announcement must happen before the ifnet 3431 * instance is detached from the system. 3432 */ 3433 if (detaching) { 3434#ifdef DIAGNOSTIC 3435 printf("%s: detaching ifnet instance %p\n", __func__, ifp); 3436#endif 3437 /* 3438 * ifp may already be nulled out if we are being reentered 3439 * to delete the ll_ifma. 3440 */ 3441 if (ifp != NULL) { 3442 rt_newmaddrmsg(RTM_DELMADDR, ifma); 3443 ifma->ifma_ifp = NULL; 3444 } 3445 } 3446 3447 if (--ifma->ifma_refcount > 0) 3448 return 0; 3449 3450 /* 3451 * If this ifma is a network-layer ifma, a link-layer ifma may 3452 * have been associated with it. Release it first if so. 3453 */ 3454 ll_ifma = ifma->ifma_llifma; 3455 if (ll_ifma != NULL) { 3456 KASSERT(ifma->ifma_lladdr != NULL, 3457 ("%s: llifma w/o lladdr", __func__)); 3458 if (detaching) 3459 ll_ifma->ifma_ifp = NULL; /* XXX */ 3460 if (--ll_ifma->ifma_refcount == 0) { 3461 if (ifp != NULL) { 3462 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, 3463 ifma_link); 3464 } 3465 if_freemulti(ll_ifma); 3466 } 3467 } 3468 3469 if (ifp != NULL) 3470 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 3471 3472 if_freemulti(ifma); 3473 3474 /* 3475 * The last reference to this instance of struct ifmultiaddr 3476 * was released; the hardware should be notified of this change. 3477 */ 3478 return 1; 3479} 3480 3481/* 3482 * Set the link layer address on an interface. 3483 * 3484 * At this time we only support certain types of interfaces, 3485 * and we don't allow the length of the address to change. 3486 */ 3487int 3488if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 3489{ 3490 struct sockaddr_dl *sdl; 3491 struct ifaddr *ifa; 3492 struct ifreq ifr; 3493 3494 IF_ADDR_RLOCK(ifp); 3495 ifa = ifp->if_addr; 3496 if (ifa == NULL) { 3497 IF_ADDR_RUNLOCK(ifp); 3498 return (EINVAL); 3499 } 3500 ifa_ref(ifa); 3501 IF_ADDR_RUNLOCK(ifp); 3502 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 3503 if (sdl == NULL) { 3504 ifa_free(ifa); 3505 return (EINVAL); 3506 } 3507 if (len != sdl->sdl_alen) { /* don't allow length to change */ 3508 ifa_free(ifa); 3509 return (EINVAL); 3510 } 3511 switch (ifp->if_type) { 3512 case IFT_ETHER: 3513 case IFT_FDDI: 3514 case IFT_XETHER: 3515 case IFT_ISO88025: 3516 case IFT_L2VLAN: 3517 case IFT_BRIDGE: 3518 case IFT_ARCNET: 3519 case IFT_IEEE8023ADLAG: 3520 case IFT_IEEE80211: 3521 bcopy(lladdr, LLADDR(sdl), len); 3522 ifa_free(ifa); 3523 break; 3524 default: 3525 ifa_free(ifa); 3526 return (ENODEV); 3527 } 3528 3529 /* 3530 * If the interface is already up, we need 3531 * to re-init it in order to reprogram its 3532 * address filter. 3533 */ 3534 if ((ifp->if_flags & IFF_UP) != 0) { 3535 if (ifp->if_ioctl) { 3536 ifp->if_flags &= ~IFF_UP; 3537 ifr.ifr_flags = ifp->if_flags & 0xffff; 3538 ifr.ifr_flagshigh = ifp->if_flags >> 16; 3539 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 3540 ifp->if_flags |= IFF_UP; 3541 ifr.ifr_flags = ifp->if_flags & 0xffff; 3542 ifr.ifr_flagshigh = ifp->if_flags >> 16; 3543 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 3544 } 3545#ifdef INET 3546 /* 3547 * Also send gratuitous ARPs to notify other nodes about 3548 * the address change. 3549 */ 3550 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 3551 if (ifa->ifa_addr->sa_family == AF_INET) 3552 arp_ifinit(ifp, ifa); 3553 } 3554#endif 3555 } 3556 return (0); 3557} 3558 3559/* 3560 * The name argument must be a pointer to storage which will last as 3561 * long as the interface does. For physical devices, the result of 3562 * device_get_name(dev) is a good choice and for pseudo-devices a 3563 * static string works well. 3564 */ 3565void 3566if_initname(struct ifnet *ifp, const char *name, int unit) 3567{ 3568 ifp->if_dname = name; 3569 ifp->if_dunit = unit; 3570 if (unit != IF_DUNIT_NONE) 3571 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit); 3572 else 3573 strlcpy(ifp->if_xname, name, IFNAMSIZ); 3574} 3575 3576int 3577if_printf(struct ifnet *ifp, const char * fmt, ...) 3578{ 3579 va_list ap; 3580 int retval; 3581 3582 retval = printf("%s: ", ifp->if_xname); 3583 va_start(ap, fmt); 3584 retval += vprintf(fmt, ap); 3585 va_end(ap); 3586 return (retval); 3587} 3588 3589void 3590if_start(struct ifnet *ifp) 3591{ 3592 3593 (*(ifp)->if_start)(ifp); 3594} 3595 3596/* 3597 * Backwards compatibility interface for drivers 3598 * that have not implemented it 3599 */ 3600static int 3601if_transmit(struct ifnet *ifp, struct mbuf *m) 3602{ 3603 int error; 3604 3605 IFQ_HANDOFF(ifp, m, error); 3606 return (error); 3607} 3608 3609static void 3610if_input_default(struct ifnet *ifp __unused, struct mbuf *m) 3611{ 3612 3613 m_freem(m); 3614} 3615 3616int 3617if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust) 3618{ 3619 int active = 0; 3620 3621 IF_LOCK(ifq); 3622 if (_IF_QFULL(ifq)) { 3623 _IF_DROP(ifq); 3624 IF_UNLOCK(ifq); 3625 m_freem(m); 3626 return (0); 3627 } 3628 if (ifp != NULL) { 3629 ifp->if_obytes += m->m_pkthdr.len + adjust; 3630 if (m->m_flags & (M_BCAST|M_MCAST)) 3631 ifp->if_omcasts++; 3632 active = ifp->if_drv_flags & IFF_DRV_OACTIVE; 3633 } 3634 _IF_ENQUEUE(ifq, m); 3635 IF_UNLOCK(ifq); 3636 if (ifp != NULL && !active) 3637 (*(ifp)->if_start)(ifp); 3638 return (1); 3639} 3640 3641void 3642if_register_com_alloc(u_char type, 3643 if_com_alloc_t *a, if_com_free_t *f) 3644{ 3645 3646 KASSERT(if_com_alloc[type] == NULL, 3647 ("if_register_com_alloc: %d already registered", type)); 3648 KASSERT(if_com_free[type] == NULL, 3649 ("if_register_com_alloc: %d free already registered", type)); 3650 3651 if_com_alloc[type] = a; 3652 if_com_free[type] = f; 3653} 3654 3655void 3656if_deregister_com_alloc(u_char type) 3657{ 3658 3659 KASSERT(if_com_alloc[type] != NULL, 3660 ("if_deregister_com_alloc: %d not registered", type)); 3661 KASSERT(if_com_free[type] != NULL, 3662 ("if_deregister_com_alloc: %d free not registered", type)); 3663 if_com_alloc[type] = NULL; 3664 if_com_free[type] = NULL; 3665} 3666