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