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