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