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