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