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