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