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