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