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