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