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