1 /* $NetBSD: if.c,v 1.487 2021/07/01 22:08:13 blymn Exp $ */ 2 3 /*- 4 * Copyright (c) 1999, 2000, 2001, 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by William Studenmund and Jason R. Thorpe. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 34 * All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. Neither the name of the project nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 */ 60 61 /* 62 * Copyright (c) 1980, 1986, 1993 63 * The Regents of the University of California. All rights reserved. 64 * 65 * Redistribution and use in source and binary forms, with or without 66 * modification, are permitted provided that the following conditions 67 * are met: 68 * 1. Redistributions of source code must retain the above copyright 69 * notice, this list of conditions and the following disclaimer. 70 * 2. Redistributions in binary form must reproduce the above copyright 71 * notice, this list of conditions and the following disclaimer in the 72 * documentation and/or other materials provided with the distribution. 73 * 3. Neither the name of the University nor the names of its contributors 74 * may be used to endorse or promote products derived from this software 75 * without specific prior written permission. 76 * 77 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 78 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 79 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 80 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 81 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 83 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 84 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 85 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 86 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 87 * SUCH DAMAGE. 88 * 89 * @(#)if.c 8.5 (Berkeley) 1/9/95 90 */ 91 92 #include <sys/cdefs.h> 93 __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.487 2021/07/01 22:08:13 blymn Exp $"); 94 95 #if defined(_KERNEL_OPT) 96 #include "opt_inet.h" 97 #include "opt_ipsec.h" 98 #include "opt_atalk.h" 99 #include "opt_wlan.h" 100 #include "opt_net_mpsafe.h" 101 #include "opt_mrouting.h" 102 #endif 103 104 #include <sys/param.h> 105 #include <sys/mbuf.h> 106 #include <sys/systm.h> 107 #include <sys/callout.h> 108 #include <sys/proc.h> 109 #include <sys/socket.h> 110 #include <sys/socketvar.h> 111 #include <sys/domain.h> 112 #include <sys/protosw.h> 113 #include <sys/kernel.h> 114 #include <sys/ioctl.h> 115 #include <sys/sysctl.h> 116 #include <sys/syslog.h> 117 #include <sys/kauth.h> 118 #include <sys/kmem.h> 119 #include <sys/xcall.h> 120 #include <sys/cpu.h> 121 #include <sys/intr.h> 122 #include <sys/module_hook.h> 123 #include <sys/compat_stub.h> 124 #include <sys/msan.h> 125 126 #include <net/if.h> 127 #include <net/if_dl.h> 128 #include <net/if_ether.h> 129 #include <net/if_media.h> 130 #include <net80211/ieee80211.h> 131 #include <net80211/ieee80211_ioctl.h> 132 #include <net/if_types.h> 133 #include <net/route.h> 134 #include <net/netisr.h> 135 #include <sys/module.h> 136 #ifdef NETATALK 137 #include <netatalk/at_extern.h> 138 #include <netatalk/at.h> 139 #endif 140 #include <net/pfil.h> 141 #include <netinet/in.h> 142 #include <netinet/in_var.h> 143 #include <netinet/ip_encap.h> 144 #include <net/bpf.h> 145 146 #ifdef INET6 147 #include <netinet6/in6_var.h> 148 #include <netinet6/nd6.h> 149 #endif 150 151 #include "ether.h" 152 153 #include "bridge.h" 154 #if NBRIDGE > 0 155 #include <net/if_bridgevar.h> 156 #endif 157 158 #include "carp.h" 159 #if NCARP > 0 160 #include <netinet/ip_carp.h> 161 #endif 162 163 #include "lagg.h" 164 #if NLAGG > 0 165 #include <net/lagg/if_laggvar.h> 166 #endif 167 168 #include <compat/sys/sockio.h> 169 170 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 171 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 172 173 /* 174 * XXX reusing (ifp)->if_snd->ifq_lock rather than having another spin mutex 175 * for each ifnet. It doesn't matter because: 176 * - if IFEF_MPSAFE is enabled, if_snd isn't used and lock contentions on 177 * ifq_lock don't happen 178 * - if IFEF_MPSAFE is disabled, there is no lock contention on ifq_lock 179 * because if_snd, if_link_state_change and if_link_state_change_process 180 * are all called with KERNEL_LOCK 181 */ 182 #define IF_LINK_STATE_CHANGE_LOCK(ifp) \ 183 mutex_enter((ifp)->if_snd.ifq_lock) 184 #define IF_LINK_STATE_CHANGE_UNLOCK(ifp) \ 185 mutex_exit((ifp)->if_snd.ifq_lock) 186 187 /* 188 * Global list of interfaces. 189 */ 190 /* DEPRECATED. Remove it once kvm(3) users disappeared */ 191 struct ifnet_head ifnet_list; 192 193 struct pslist_head ifnet_pslist; 194 static ifnet_t ** ifindex2ifnet = NULL; 195 static u_int if_index = 1; 196 static size_t if_indexlim = 0; 197 static uint64_t index_gen; 198 /* Mutex to protect the above objects. */ 199 kmutex_t ifnet_mtx __cacheline_aligned; 200 static struct psref_class *ifnet_psref_class __read_mostly; 201 static pserialize_t ifnet_psz; 202 static struct workqueue *ifnet_link_state_wq __read_mostly; 203 204 static kmutex_t if_clone_mtx; 205 206 struct ifnet *lo0ifp; 207 int ifqmaxlen = IFQ_MAXLEN; 208 209 struct psref_class *ifa_psref_class __read_mostly; 210 211 static int if_delroute_matcher(struct rtentry *, void *); 212 213 static bool if_is_unit(const char *); 214 static struct if_clone *if_clone_lookup(const char *, int *); 215 216 static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners); 217 static int if_cloners_count; 218 219 /* Packet filtering hook for interfaces. */ 220 pfil_head_t * if_pfil __read_mostly; 221 222 static kauth_listener_t if_listener; 223 224 static int doifioctl(struct socket *, u_long, void *, struct lwp *); 225 static void if_detach_queues(struct ifnet *, struct ifqueue *); 226 static void sysctl_sndq_setup(struct sysctllog **, const char *, 227 struct ifaltq *); 228 static void if_slowtimo(void *); 229 static void if_attachdomain1(struct ifnet *); 230 static int ifconf(u_long, void *); 231 static int if_transmit(struct ifnet *, struct mbuf *); 232 static int if_clone_create(const char *); 233 static int if_clone_destroy(const char *); 234 static void if_link_state_change_work(struct work *, void *); 235 static void if_up_locked(struct ifnet *); 236 static void _if_down(struct ifnet *); 237 static void if_down_deactivated(struct ifnet *); 238 239 struct if_percpuq { 240 struct ifnet *ipq_ifp; 241 void *ipq_si; 242 struct percpu *ipq_ifqs; /* struct ifqueue */ 243 }; 244 245 static struct mbuf *if_percpuq_dequeue(struct if_percpuq *); 246 247 static void if_percpuq_drops(void *, void *, struct cpu_info *); 248 static int sysctl_percpuq_drops_handler(SYSCTLFN_PROTO); 249 static void sysctl_percpuq_setup(struct sysctllog **, const char *, 250 struct if_percpuq *); 251 252 struct if_deferred_start { 253 struct ifnet *ids_ifp; 254 void (*ids_if_start)(struct ifnet *); 255 void *ids_si; 256 }; 257 258 static void if_deferred_start_softint(void *); 259 static void if_deferred_start_common(struct ifnet *); 260 static void if_deferred_start_destroy(struct ifnet *); 261 262 #if defined(INET) || defined(INET6) 263 static void sysctl_net_pktq_setup(struct sysctllog **, int); 264 #endif 265 266 /* 267 * Hook for if_vlan - needed by if_agr 268 */ 269 struct if_vlan_vlan_input_hook_t if_vlan_vlan_input_hook; 270 271 static void if_sysctl_setup(struct sysctllog **); 272 273 static int 274 if_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie, 275 void *arg0, void *arg1, void *arg2, void *arg3) 276 { 277 int result; 278 enum kauth_network_req req; 279 280 result = KAUTH_RESULT_DEFER; 281 req = (enum kauth_network_req)(uintptr_t)arg1; 282 283 if (action != KAUTH_NETWORK_INTERFACE) 284 return result; 285 286 if ((req == KAUTH_REQ_NETWORK_INTERFACE_GET) || 287 (req == KAUTH_REQ_NETWORK_INTERFACE_SET)) 288 result = KAUTH_RESULT_ALLOW; 289 290 return result; 291 } 292 293 /* 294 * Network interface utility routines. 295 * 296 * Routines with ifa_ifwith* names take sockaddr *'s as 297 * parameters. 298 */ 299 void 300 ifinit(void) 301 { 302 303 #if (defined(INET) || defined(INET6)) 304 encapinit(); 305 #endif 306 307 if_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK, 308 if_listener_cb, NULL); 309 310 /* interfaces are available, inform socket code */ 311 ifioctl = doifioctl; 312 } 313 314 /* 315 * XXX Initialization before configure(). 316 * XXX hack to get pfil_add_hook working in autoconf. 317 */ 318 void 319 ifinit1(void) 320 { 321 int error __diagused; 322 323 #ifdef NET_MPSAFE 324 printf("NET_MPSAFE enabled\n"); 325 #endif 326 327 mutex_init(&if_clone_mtx, MUTEX_DEFAULT, IPL_NONE); 328 329 TAILQ_INIT(&ifnet_list); 330 mutex_init(&ifnet_mtx, MUTEX_DEFAULT, IPL_NONE); 331 ifnet_psz = pserialize_create(); 332 ifnet_psref_class = psref_class_create("ifnet", IPL_SOFTNET); 333 ifa_psref_class = psref_class_create("ifa", IPL_SOFTNET); 334 error = workqueue_create(&ifnet_link_state_wq, "iflnkst", 335 if_link_state_change_work, NULL, PRI_SOFTNET, IPL_NET, 336 WQ_MPSAFE); 337 KASSERT(error == 0); 338 PSLIST_INIT(&ifnet_pslist); 339 340 if_indexlim = 8; 341 342 if_pfil = pfil_head_create(PFIL_TYPE_IFNET, NULL); 343 KASSERT(if_pfil != NULL); 344 345 #if NETHER > 0 || defined(NETATALK) || defined(WLAN) 346 etherinit(); 347 #endif 348 } 349 350 /* XXX must be after domaininit() */ 351 void 352 ifinit_post(void) 353 { 354 355 if_sysctl_setup(NULL); 356 } 357 358 ifnet_t * 359 if_alloc(u_char type) 360 { 361 return kmem_zalloc(sizeof(ifnet_t), KM_SLEEP); 362 } 363 364 void 365 if_free(ifnet_t *ifp) 366 { 367 kmem_free(ifp, sizeof(ifnet_t)); 368 } 369 370 void 371 if_initname(struct ifnet *ifp, const char *name, int unit) 372 { 373 (void)snprintf(ifp->if_xname, sizeof(ifp->if_xname), 374 "%s%d", name, unit); 375 } 376 377 /* 378 * Null routines used while an interface is going away. These routines 379 * just return an error. 380 */ 381 382 int 383 if_nulloutput(struct ifnet *ifp, struct mbuf *m, 384 const struct sockaddr *so, const struct rtentry *rt) 385 { 386 387 return ENXIO; 388 } 389 390 void 391 if_nullinput(struct ifnet *ifp, struct mbuf *m) 392 { 393 394 /* Nothing. */ 395 } 396 397 void 398 if_nullstart(struct ifnet *ifp) 399 { 400 401 /* Nothing. */ 402 } 403 404 int 405 if_nulltransmit(struct ifnet *ifp, struct mbuf *m) 406 { 407 408 m_freem(m); 409 return ENXIO; 410 } 411 412 int 413 if_nullioctl(struct ifnet *ifp, u_long cmd, void *data) 414 { 415 416 return ENXIO; 417 } 418 419 int 420 if_nullinit(struct ifnet *ifp) 421 { 422 423 return ENXIO; 424 } 425 426 void 427 if_nullstop(struct ifnet *ifp, int disable) 428 { 429 430 /* Nothing. */ 431 } 432 433 void 434 if_nullslowtimo(struct ifnet *ifp) 435 { 436 437 /* Nothing. */ 438 } 439 440 void 441 if_nulldrain(struct ifnet *ifp) 442 { 443 444 /* Nothing. */ 445 } 446 447 void 448 if_set_sadl(struct ifnet *ifp, const void *lla, u_char addrlen, bool factory) 449 { 450 struct ifaddr *ifa; 451 struct sockaddr_dl *sdl; 452 453 ifp->if_addrlen = addrlen; 454 if_alloc_sadl(ifp); 455 ifa = ifp->if_dl; 456 sdl = satosdl(ifa->ifa_addr); 457 458 (void)sockaddr_dl_setaddr(sdl, sdl->sdl_len, lla, ifp->if_addrlen); 459 if (factory) { 460 KASSERT(ifp->if_hwdl == NULL); 461 ifp->if_hwdl = ifp->if_dl; 462 ifaref(ifp->if_hwdl); 463 } 464 /* TBD routing socket */ 465 } 466 467 struct ifaddr * 468 if_dl_create(const struct ifnet *ifp, const struct sockaddr_dl **sdlp) 469 { 470 unsigned socksize, ifasize; 471 int addrlen, namelen; 472 struct sockaddr_dl *mask, *sdl; 473 struct ifaddr *ifa; 474 475 namelen = strlen(ifp->if_xname); 476 addrlen = ifp->if_addrlen; 477 socksize = roundup(sockaddr_dl_measure(namelen, addrlen), sizeof(long)); 478 ifasize = sizeof(*ifa) + 2 * socksize; 479 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO); 480 481 sdl = (struct sockaddr_dl *)(ifa + 1); 482 mask = (struct sockaddr_dl *)(socksize + (char *)sdl); 483 484 sockaddr_dl_init(sdl, socksize, ifp->if_index, ifp->if_type, 485 ifp->if_xname, namelen, NULL, addrlen); 486 mask->sdl_family = AF_LINK; 487 mask->sdl_len = sockaddr_dl_measure(namelen, 0); 488 memset(&mask->sdl_data[0], 0xff, namelen); 489 ifa->ifa_rtrequest = link_rtrequest; 490 ifa->ifa_addr = (struct sockaddr *)sdl; 491 ifa->ifa_netmask = (struct sockaddr *)mask; 492 ifa_psref_init(ifa); 493 494 *sdlp = sdl; 495 496 return ifa; 497 } 498 499 static void 500 if_sadl_setrefs(struct ifnet *ifp, struct ifaddr *ifa) 501 { 502 const struct sockaddr_dl *sdl; 503 504 ifp->if_dl = ifa; 505 ifaref(ifa); 506 sdl = satosdl(ifa->ifa_addr); 507 ifp->if_sadl = sdl; 508 } 509 510 /* 511 * Allocate the link level name for the specified interface. This 512 * is an attachment helper. It must be called after ifp->if_addrlen 513 * is initialized, which may not be the case when if_attach() is 514 * called. 515 */ 516 void 517 if_alloc_sadl(struct ifnet *ifp) 518 { 519 struct ifaddr *ifa; 520 const struct sockaddr_dl *sdl; 521 522 /* 523 * If the interface already has a link name, release it 524 * now. This is useful for interfaces that can change 525 * link types, and thus switch link names often. 526 */ 527 if (ifp->if_sadl != NULL) 528 if_free_sadl(ifp, 0); 529 530 ifa = if_dl_create(ifp, &sdl); 531 532 ifa_insert(ifp, ifa); 533 if_sadl_setrefs(ifp, ifa); 534 } 535 536 static void 537 if_deactivate_sadl(struct ifnet *ifp) 538 { 539 struct ifaddr *ifa; 540 541 KASSERT(ifp->if_dl != NULL); 542 543 ifa = ifp->if_dl; 544 545 ifp->if_sadl = NULL; 546 547 ifp->if_dl = NULL; 548 ifafree(ifa); 549 } 550 551 static void 552 if_replace_sadl(struct ifnet *ifp, struct ifaddr *ifa) 553 { 554 struct ifaddr *old; 555 556 KASSERT(ifp->if_dl != NULL); 557 558 old = ifp->if_dl; 559 560 ifaref(ifa); 561 /* XXX Update if_dl and if_sadl atomically */ 562 ifp->if_dl = ifa; 563 ifp->if_sadl = satosdl(ifa->ifa_addr); 564 565 ifafree(old); 566 } 567 568 void 569 if_activate_sadl(struct ifnet *ifp, struct ifaddr *ifa0, 570 const struct sockaddr_dl *sdl) 571 { 572 int s, ss; 573 struct ifaddr *ifa; 574 int bound = curlwp_bind(); 575 576 KASSERT(ifa_held(ifa0)); 577 578 s = splsoftnet(); 579 580 if_replace_sadl(ifp, ifa0); 581 582 ss = pserialize_read_enter(); 583 IFADDR_READER_FOREACH(ifa, ifp) { 584 struct psref psref; 585 ifa_acquire(ifa, &psref); 586 pserialize_read_exit(ss); 587 588 rtinit(ifa, RTM_LLINFO_UPD, 0); 589 590 ss = pserialize_read_enter(); 591 ifa_release(ifa, &psref); 592 } 593 pserialize_read_exit(ss); 594 595 splx(s); 596 curlwp_bindx(bound); 597 } 598 599 /* 600 * Free the link level name for the specified interface. This is 601 * a detach helper. This is called from if_detach(). 602 */ 603 void 604 if_free_sadl(struct ifnet *ifp, int factory) 605 { 606 struct ifaddr *ifa; 607 int s; 608 609 if (factory && ifp->if_hwdl != NULL) { 610 ifa = ifp->if_hwdl; 611 ifp->if_hwdl = NULL; 612 ifafree(ifa); 613 } 614 615 ifa = ifp->if_dl; 616 if (ifa == NULL) { 617 KASSERT(ifp->if_sadl == NULL); 618 return; 619 } 620 621 KASSERT(ifp->if_sadl != NULL); 622 623 s = splsoftnet(); 624 KASSERT(ifa->ifa_addr->sa_family == AF_LINK); 625 ifa_remove(ifp, ifa); 626 if_deactivate_sadl(ifp); 627 splx(s); 628 } 629 630 static void 631 if_getindex(ifnet_t *ifp) 632 { 633 bool hitlimit = false; 634 635 ifp->if_index_gen = index_gen++; 636 637 ifp->if_index = if_index; 638 if (ifindex2ifnet == NULL) { 639 if_index++; 640 goto skip; 641 } 642 while (if_byindex(ifp->if_index)) { 643 /* 644 * If we hit USHRT_MAX, we skip back to 0 since 645 * there are a number of places where the value 646 * of if_index or if_index itself is compared 647 * to or stored in an unsigned short. By 648 * jumping back, we won't botch those assignments 649 * or comparisons. 650 */ 651 if (++if_index == 0) { 652 if_index = 1; 653 } else if (if_index == USHRT_MAX) { 654 /* 655 * However, if we have to jump back to 656 * zero *twice* without finding an empty 657 * slot in ifindex2ifnet[], then there 658 * there are too many (>65535) interfaces. 659 */ 660 if (hitlimit) { 661 panic("too many interfaces"); 662 } 663 hitlimit = true; 664 if_index = 1; 665 } 666 ifp->if_index = if_index; 667 } 668 skip: 669 /* 670 * ifindex2ifnet is indexed by if_index. Since if_index will 671 * grow dynamically, it should grow too. 672 */ 673 if (ifindex2ifnet == NULL || ifp->if_index >= if_indexlim) { 674 size_t m, n, oldlim; 675 void *q; 676 677 oldlim = if_indexlim; 678 while (ifp->if_index >= if_indexlim) 679 if_indexlim <<= 1; 680 681 /* grow ifindex2ifnet */ 682 m = oldlim * sizeof(struct ifnet *); 683 n = if_indexlim * sizeof(struct ifnet *); 684 q = malloc(n, M_IFADDR, M_WAITOK | M_ZERO); 685 if (ifindex2ifnet != NULL) { 686 memcpy(q, ifindex2ifnet, m); 687 free(ifindex2ifnet, M_IFADDR); 688 } 689 ifindex2ifnet = (struct ifnet **)q; 690 } 691 ifindex2ifnet[ifp->if_index] = ifp; 692 } 693 694 /* 695 * Initialize an interface and assign an index for it. 696 * 697 * It must be called prior to a device specific attach routine 698 * (e.g., ether_ifattach and ieee80211_ifattach) or if_alloc_sadl, 699 * and be followed by if_register: 700 * 701 * if_initialize(ifp); 702 * ether_ifattach(ifp, enaddr); 703 * if_register(ifp); 704 */ 705 void 706 if_initialize(ifnet_t *ifp) 707 { 708 709 KASSERT(if_indexlim > 0); 710 TAILQ_INIT(&ifp->if_addrlist); 711 712 /* 713 * Link level name is allocated later by a separate call to 714 * if_alloc_sadl(). 715 */ 716 717 if (ifp->if_snd.ifq_maxlen == 0) 718 ifp->if_snd.ifq_maxlen = ifqmaxlen; 719 720 ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */ 721 722 ifp->if_link_state = LINK_STATE_UNKNOWN; 723 ifp->if_link_queue = -1; /* all bits set, see link_state_change() */ 724 ifp->if_link_scheduled = false; 725 726 ifp->if_capenable = 0; 727 ifp->if_csum_flags_tx = 0; 728 ifp->if_csum_flags_rx = 0; 729 730 #ifdef ALTQ 731 ifp->if_snd.altq_type = 0; 732 ifp->if_snd.altq_disc = NULL; 733 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE; 734 ifp->if_snd.altq_tbr = NULL; 735 ifp->if_snd.altq_ifp = ifp; 736 #endif 737 738 IFQ_LOCK_INIT(&ifp->if_snd); 739 740 ifp->if_pfil = pfil_head_create(PFIL_TYPE_IFNET, ifp); 741 pfil_run_ifhooks(if_pfil, PFIL_IFNET_ATTACH, ifp); 742 743 IF_AFDATA_LOCK_INIT(ifp); 744 745 PSLIST_ENTRY_INIT(ifp, if_pslist_entry); 746 PSLIST_INIT(&ifp->if_addr_pslist); 747 psref_target_init(&ifp->if_psref, ifnet_psref_class); 748 ifp->if_ioctl_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); 749 LIST_INIT(&ifp->if_multiaddrs); 750 if_stats_init(ifp); 751 752 IFNET_GLOBAL_LOCK(); 753 if_getindex(ifp); 754 IFNET_GLOBAL_UNLOCK(); 755 } 756 757 /* 758 * Register an interface to the list of "active" interfaces. 759 */ 760 void 761 if_register(ifnet_t *ifp) 762 { 763 /* 764 * If the driver has not supplied its own if_ioctl or if_stop, 765 * then supply the default. 766 */ 767 if (ifp->if_ioctl == NULL) 768 ifp->if_ioctl = ifioctl_common; 769 if (ifp->if_stop == NULL) 770 ifp->if_stop = if_nullstop; 771 772 sysctl_sndq_setup(&ifp->if_sysctl_log, ifp->if_xname, &ifp->if_snd); 773 774 if (!STAILQ_EMPTY(&domains)) 775 if_attachdomain1(ifp); 776 777 /* Announce the interface. */ 778 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 779 780 if (ifp->if_slowtimo != NULL) { 781 ifp->if_slowtimo_ch = 782 kmem_zalloc(sizeof(*ifp->if_slowtimo_ch), KM_SLEEP); 783 callout_init(ifp->if_slowtimo_ch, 0); 784 callout_setfunc(ifp->if_slowtimo_ch, if_slowtimo, ifp); 785 if_slowtimo(ifp); 786 } 787 788 if (ifp->if_transmit == NULL || ifp->if_transmit == if_nulltransmit) 789 ifp->if_transmit = if_transmit; 790 791 IFNET_GLOBAL_LOCK(); 792 TAILQ_INSERT_TAIL(&ifnet_list, ifp, if_list); 793 IFNET_WRITER_INSERT_TAIL(ifp); 794 IFNET_GLOBAL_UNLOCK(); 795 } 796 797 /* 798 * The if_percpuq framework 799 * 800 * It allows network device drivers to execute the network stack 801 * in softint (so called softint-based if_input). It utilizes 802 * softint and percpu ifqueue. It doesn't distribute any packets 803 * between CPUs, unlike pktqueue(9). 804 * 805 * Currently we support two options for device drivers to apply the framework: 806 * - Use it implicitly with less changes 807 * - If you use if_attach in driver's _attach function and if_input in 808 * driver's Rx interrupt handler, a packet is queued and a softint handles 809 * the packet implicitly 810 * - Use it explicitly in each driver (recommended) 811 * - You can use if_percpuq_* directly in your driver 812 * - In this case, you need to allocate struct if_percpuq in driver's softc 813 * - See wm(4) as a reference implementation 814 */ 815 816 static void 817 if_percpuq_softint(void *arg) 818 { 819 struct if_percpuq *ipq = arg; 820 struct ifnet *ifp = ipq->ipq_ifp; 821 struct mbuf *m; 822 823 while ((m = if_percpuq_dequeue(ipq)) != NULL) { 824 if_statinc(ifp, if_ipackets); 825 bpf_mtap(ifp, m, BPF_D_IN); 826 827 ifp->_if_input(ifp, m); 828 } 829 } 830 831 static void 832 if_percpuq_init_ifq(void *p, void *arg __unused, struct cpu_info *ci __unused) 833 { 834 struct ifqueue *const ifq = p; 835 836 memset(ifq, 0, sizeof(*ifq)); 837 ifq->ifq_maxlen = IFQ_MAXLEN; 838 } 839 840 struct if_percpuq * 841 if_percpuq_create(struct ifnet *ifp) 842 { 843 struct if_percpuq *ipq; 844 u_int flags = SOFTINT_NET; 845 846 flags |= if_is_mpsafe(ifp) ? SOFTINT_MPSAFE : 0; 847 848 ipq = kmem_zalloc(sizeof(*ipq), KM_SLEEP); 849 ipq->ipq_ifp = ifp; 850 ipq->ipq_si = softint_establish(flags, if_percpuq_softint, ipq); 851 ipq->ipq_ifqs = percpu_alloc(sizeof(struct ifqueue)); 852 percpu_foreach(ipq->ipq_ifqs, &if_percpuq_init_ifq, NULL); 853 854 sysctl_percpuq_setup(&ifp->if_sysctl_log, ifp->if_xname, ipq); 855 856 return ipq; 857 } 858 859 static struct mbuf * 860 if_percpuq_dequeue(struct if_percpuq *ipq) 861 { 862 struct mbuf *m; 863 struct ifqueue *ifq; 864 int s; 865 866 s = splnet(); 867 ifq = percpu_getref(ipq->ipq_ifqs); 868 IF_DEQUEUE(ifq, m); 869 percpu_putref(ipq->ipq_ifqs); 870 splx(s); 871 872 return m; 873 } 874 875 static void 876 if_percpuq_purge_ifq(void *p, void *arg __unused, struct cpu_info *ci __unused) 877 { 878 struct ifqueue *const ifq = p; 879 880 IF_PURGE(ifq); 881 } 882 883 void 884 if_percpuq_destroy(struct if_percpuq *ipq) 885 { 886 887 /* if_detach may already destroy it */ 888 if (ipq == NULL) 889 return; 890 891 softint_disestablish(ipq->ipq_si); 892 percpu_foreach(ipq->ipq_ifqs, &if_percpuq_purge_ifq, NULL); 893 percpu_free(ipq->ipq_ifqs, sizeof(struct ifqueue)); 894 kmem_free(ipq, sizeof(*ipq)); 895 } 896 897 void 898 if_percpuq_enqueue(struct if_percpuq *ipq, struct mbuf *m) 899 { 900 struct ifqueue *ifq; 901 int s; 902 903 KASSERT(ipq != NULL); 904 905 s = splnet(); 906 ifq = percpu_getref(ipq->ipq_ifqs); 907 if (IF_QFULL(ifq)) { 908 IF_DROP(ifq); 909 percpu_putref(ipq->ipq_ifqs); 910 m_freem(m); 911 goto out; 912 } 913 IF_ENQUEUE(ifq, m); 914 percpu_putref(ipq->ipq_ifqs); 915 916 softint_schedule(ipq->ipq_si); 917 out: 918 splx(s); 919 } 920 921 static void 922 if_percpuq_drops(void *p, void *arg, struct cpu_info *ci __unused) 923 { 924 struct ifqueue *const ifq = p; 925 int *sum = arg; 926 927 *sum += ifq->ifq_drops; 928 } 929 930 static int 931 sysctl_percpuq_drops_handler(SYSCTLFN_ARGS) 932 { 933 struct sysctlnode node; 934 struct if_percpuq *ipq; 935 int sum = 0; 936 int error; 937 938 node = *rnode; 939 ipq = node.sysctl_data; 940 941 percpu_foreach(ipq->ipq_ifqs, if_percpuq_drops, &sum); 942 943 node.sysctl_data = ∑ 944 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 945 if (error != 0 || newp == NULL) 946 return error; 947 948 return 0; 949 } 950 951 static void 952 sysctl_percpuq_setup(struct sysctllog **clog, const char* ifname, 953 struct if_percpuq *ipq) 954 { 955 const struct sysctlnode *cnode, *rnode; 956 957 if (sysctl_createv(clog, 0, NULL, &rnode, 958 CTLFLAG_PERMANENT, 959 CTLTYPE_NODE, "interfaces", 960 SYSCTL_DESCR("Per-interface controls"), 961 NULL, 0, NULL, 0, 962 CTL_NET, CTL_CREATE, CTL_EOL) != 0) 963 goto bad; 964 965 if (sysctl_createv(clog, 0, &rnode, &rnode, 966 CTLFLAG_PERMANENT, 967 CTLTYPE_NODE, ifname, 968 SYSCTL_DESCR("Interface controls"), 969 NULL, 0, NULL, 0, 970 CTL_CREATE, CTL_EOL) != 0) 971 goto bad; 972 973 if (sysctl_createv(clog, 0, &rnode, &rnode, 974 CTLFLAG_PERMANENT, 975 CTLTYPE_NODE, "rcvq", 976 SYSCTL_DESCR("Interface input queue controls"), 977 NULL, 0, NULL, 0, 978 CTL_CREATE, CTL_EOL) != 0) 979 goto bad; 980 981 #ifdef NOTYET 982 /* XXX Should show each per-CPU queue length? */ 983 if (sysctl_createv(clog, 0, &rnode, &rnode, 984 CTLFLAG_PERMANENT, 985 CTLTYPE_INT, "len", 986 SYSCTL_DESCR("Current input queue length"), 987 sysctl_percpuq_len, 0, NULL, 0, 988 CTL_CREATE, CTL_EOL) != 0) 989 goto bad; 990 991 if (sysctl_createv(clog, 0, &rnode, &cnode, 992 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 993 CTLTYPE_INT, "maxlen", 994 SYSCTL_DESCR("Maximum allowed input queue length"), 995 sysctl_percpuq_maxlen_handler, 0, (void *)ipq, 0, 996 CTL_CREATE, CTL_EOL) != 0) 997 goto bad; 998 #endif 999 1000 if (sysctl_createv(clog, 0, &rnode, &cnode, 1001 CTLFLAG_PERMANENT, 1002 CTLTYPE_INT, "drops", 1003 SYSCTL_DESCR("Total packets dropped due to full input queue"), 1004 sysctl_percpuq_drops_handler, 0, (void *)ipq, 0, 1005 CTL_CREATE, CTL_EOL) != 0) 1006 goto bad; 1007 1008 return; 1009 bad: 1010 printf("%s: could not attach sysctl nodes\n", ifname); 1011 return; 1012 } 1013 1014 /* 1015 * The deferred if_start framework 1016 * 1017 * The common APIs to defer if_start to softint when if_start is requested 1018 * from a device driver running in hardware interrupt context. 1019 */ 1020 /* 1021 * Call ifp->if_start (or equivalent) in a dedicated softint for 1022 * deferred if_start. 1023 */ 1024 static void 1025 if_deferred_start_softint(void *arg) 1026 { 1027 struct if_deferred_start *ids = arg; 1028 struct ifnet *ifp = ids->ids_ifp; 1029 1030 ids->ids_if_start(ifp); 1031 } 1032 1033 /* 1034 * The default callback function for deferred if_start. 1035 */ 1036 static void 1037 if_deferred_start_common(struct ifnet *ifp) 1038 { 1039 int s; 1040 1041 s = splnet(); 1042 if_start_lock(ifp); 1043 splx(s); 1044 } 1045 1046 static inline bool 1047 if_snd_is_used(struct ifnet *ifp) 1048 { 1049 1050 return ALTQ_IS_ENABLED(&ifp->if_snd) || 1051 ifp->if_transmit == if_transmit || 1052 ifp->if_transmit == NULL || ifp->if_transmit == if_nulltransmit; 1053 } 1054 1055 /* 1056 * Schedule deferred if_start. 1057 */ 1058 void 1059 if_schedule_deferred_start(struct ifnet *ifp) 1060 { 1061 1062 KASSERT(ifp->if_deferred_start != NULL); 1063 1064 if (if_snd_is_used(ifp) && IFQ_IS_EMPTY(&ifp->if_snd)) 1065 return; 1066 1067 softint_schedule(ifp->if_deferred_start->ids_si); 1068 } 1069 1070 /* 1071 * Create an instance of deferred if_start. A driver should call the function 1072 * only if the driver needs deferred if_start. Drivers can setup their own 1073 * deferred if_start function via 2nd argument. 1074 */ 1075 void 1076 if_deferred_start_init(struct ifnet *ifp, void (*func)(struct ifnet *)) 1077 { 1078 struct if_deferred_start *ids; 1079 u_int flags = SOFTINT_NET; 1080 1081 flags |= if_is_mpsafe(ifp) ? SOFTINT_MPSAFE : 0; 1082 1083 ids = kmem_zalloc(sizeof(*ids), KM_SLEEP); 1084 ids->ids_ifp = ifp; 1085 ids->ids_si = softint_establish(flags, if_deferred_start_softint, ids); 1086 if (func != NULL) 1087 ids->ids_if_start = func; 1088 else 1089 ids->ids_if_start = if_deferred_start_common; 1090 1091 ifp->if_deferred_start = ids; 1092 } 1093 1094 static void 1095 if_deferred_start_destroy(struct ifnet *ifp) 1096 { 1097 1098 if (ifp->if_deferred_start == NULL) 1099 return; 1100 1101 softint_disestablish(ifp->if_deferred_start->ids_si); 1102 kmem_free(ifp->if_deferred_start, sizeof(*ifp->if_deferred_start)); 1103 ifp->if_deferred_start = NULL; 1104 } 1105 1106 /* 1107 * The common interface input routine that is called by device drivers, 1108 * which should be used only when the driver's rx handler already runs 1109 * in softint. 1110 */ 1111 void 1112 if_input(struct ifnet *ifp, struct mbuf *m) 1113 { 1114 1115 KASSERT(ifp->if_percpuq == NULL); 1116 KASSERT(!cpu_intr_p()); 1117 1118 if_statinc(ifp, if_ipackets); 1119 bpf_mtap(ifp, m, BPF_D_IN); 1120 1121 ifp->_if_input(ifp, m); 1122 } 1123 1124 /* 1125 * DEPRECATED. Use if_initialize and if_register instead. 1126 * See the above comment of if_initialize. 1127 * 1128 * Note that it implicitly enables if_percpuq to make drivers easy to 1129 * migrate softint-based if_input without much changes. If you don't 1130 * want to enable it, use if_initialize instead. 1131 */ 1132 void 1133 if_attach(ifnet_t *ifp) 1134 { 1135 1136 if_initialize(ifp); 1137 ifp->if_percpuq = if_percpuq_create(ifp); 1138 if_register(ifp); 1139 } 1140 1141 void 1142 if_attachdomain(void) 1143 { 1144 struct ifnet *ifp; 1145 int s; 1146 int bound = curlwp_bind(); 1147 1148 s = pserialize_read_enter(); 1149 IFNET_READER_FOREACH(ifp) { 1150 struct psref psref; 1151 psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class); 1152 pserialize_read_exit(s); 1153 if_attachdomain1(ifp); 1154 s = pserialize_read_enter(); 1155 psref_release(&psref, &ifp->if_psref, ifnet_psref_class); 1156 } 1157 pserialize_read_exit(s); 1158 curlwp_bindx(bound); 1159 } 1160 1161 static void 1162 if_attachdomain1(struct ifnet *ifp) 1163 { 1164 struct domain *dp; 1165 int s; 1166 1167 s = splsoftnet(); 1168 1169 /* address family dependent data region */ 1170 memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata)); 1171 DOMAIN_FOREACH(dp) { 1172 if (dp->dom_ifattach != NULL) 1173 ifp->if_afdata[dp->dom_family] = 1174 (*dp->dom_ifattach)(ifp); 1175 } 1176 1177 splx(s); 1178 } 1179 1180 /* 1181 * Deactivate an interface. This points all of the procedure 1182 * handles at error stubs. May be called from interrupt context. 1183 */ 1184 void 1185 if_deactivate(struct ifnet *ifp) 1186 { 1187 int s; 1188 1189 s = splsoftnet(); 1190 1191 ifp->if_output = if_nulloutput; 1192 ifp->_if_input = if_nullinput; 1193 ifp->if_start = if_nullstart; 1194 ifp->if_transmit = if_nulltransmit; 1195 ifp->if_ioctl = if_nullioctl; 1196 ifp->if_init = if_nullinit; 1197 ifp->if_stop = if_nullstop; 1198 ifp->if_slowtimo = if_nullslowtimo; 1199 ifp->if_drain = if_nulldrain; 1200 1201 ifp->if_link_state_changed = NULL; 1202 1203 /* No more packets may be enqueued. */ 1204 ifp->if_snd.ifq_maxlen = 0; 1205 1206 splx(s); 1207 } 1208 1209 bool 1210 if_is_deactivated(const struct ifnet *ifp) 1211 { 1212 1213 return ifp->if_output == if_nulloutput; 1214 } 1215 1216 void 1217 if_purgeaddrs(struct ifnet *ifp, int family, void (*purgeaddr)(struct ifaddr *)) 1218 { 1219 struct ifaddr *ifa, *nifa; 1220 int s; 1221 1222 s = pserialize_read_enter(); 1223 for (ifa = IFADDR_READER_FIRST(ifp); ifa; ifa = nifa) { 1224 nifa = IFADDR_READER_NEXT(ifa); 1225 if (ifa->ifa_addr->sa_family != family) 1226 continue; 1227 pserialize_read_exit(s); 1228 1229 (*purgeaddr)(ifa); 1230 1231 s = pserialize_read_enter(); 1232 } 1233 pserialize_read_exit(s); 1234 } 1235 1236 #ifdef IFAREF_DEBUG 1237 static struct ifaddr **ifa_list; 1238 static int ifa_list_size; 1239 1240 /* Depends on only one if_attach runs at once */ 1241 static void 1242 if_build_ifa_list(struct ifnet *ifp) 1243 { 1244 struct ifaddr *ifa; 1245 int i; 1246 1247 KASSERT(ifa_list == NULL); 1248 KASSERT(ifa_list_size == 0); 1249 1250 IFADDR_READER_FOREACH(ifa, ifp) 1251 ifa_list_size++; 1252 1253 ifa_list = kmem_alloc(sizeof(*ifa) * ifa_list_size, KM_SLEEP); 1254 i = 0; 1255 IFADDR_READER_FOREACH(ifa, ifp) { 1256 ifa_list[i++] = ifa; 1257 ifaref(ifa); 1258 } 1259 } 1260 1261 static void 1262 if_check_and_free_ifa_list(struct ifnet *ifp) 1263 { 1264 int i; 1265 struct ifaddr *ifa; 1266 1267 if (ifa_list == NULL) 1268 return; 1269 1270 for (i = 0; i < ifa_list_size; i++) { 1271 char buf[64]; 1272 1273 ifa = ifa_list[i]; 1274 sockaddr_format(ifa->ifa_addr, buf, sizeof(buf)); 1275 if (ifa->ifa_refcnt > 1) { 1276 log(LOG_WARNING, 1277 "ifa(%s) still referenced (refcnt=%d)\n", 1278 buf, ifa->ifa_refcnt - 1); 1279 } else 1280 log(LOG_DEBUG, 1281 "ifa(%s) not referenced (refcnt=%d)\n", 1282 buf, ifa->ifa_refcnt - 1); 1283 ifafree(ifa); 1284 } 1285 1286 kmem_free(ifa_list, sizeof(*ifa) * ifa_list_size); 1287 ifa_list = NULL; 1288 ifa_list_size = 0; 1289 } 1290 #endif 1291 1292 /* 1293 * Detach an interface from the list of "active" interfaces, 1294 * freeing any resources as we go along. 1295 * 1296 * NOTE: This routine must be called with a valid thread context, 1297 * as it may block. 1298 */ 1299 void 1300 if_detach(struct ifnet *ifp) 1301 { 1302 struct socket so; 1303 struct ifaddr *ifa; 1304 #ifdef IFAREF_DEBUG 1305 struct ifaddr *last_ifa = NULL; 1306 #endif 1307 struct domain *dp; 1308 const struct protosw *pr; 1309 int s, i, family, purged; 1310 1311 #ifdef IFAREF_DEBUG 1312 if_build_ifa_list(ifp); 1313 #endif 1314 /* 1315 * XXX It's kind of lame that we have to have the 1316 * XXX socket structure... 1317 */ 1318 memset(&so, 0, sizeof(so)); 1319 1320 s = splnet(); 1321 1322 sysctl_teardown(&ifp->if_sysctl_log); 1323 1324 IFNET_LOCK(ifp); 1325 1326 /* 1327 * Unset all queued link states and pretend a 1328 * link state change is scheduled. 1329 * This stops any more link state changes occuring for this 1330 * interface while it's being detached so it's safe 1331 * to drain the workqueue. 1332 */ 1333 IF_LINK_STATE_CHANGE_LOCK(ifp); 1334 ifp->if_link_queue = -1; /* all bits set, see link_state_change() */ 1335 ifp->if_link_scheduled = true; 1336 IF_LINK_STATE_CHANGE_UNLOCK(ifp); 1337 workqueue_wait(ifnet_link_state_wq, &ifp->if_link_work); 1338 1339 if_deactivate(ifp); 1340 IFNET_UNLOCK(ifp); 1341 1342 /* 1343 * Unlink from the list and wait for all readers to leave 1344 * from pserialize read sections. Note that we can't do 1345 * psref_target_destroy here. See below. 1346 */ 1347 IFNET_GLOBAL_LOCK(); 1348 ifindex2ifnet[ifp->if_index] = NULL; 1349 TAILQ_REMOVE(&ifnet_list, ifp, if_list); 1350 IFNET_WRITER_REMOVE(ifp); 1351 pserialize_perform(ifnet_psz); 1352 IFNET_GLOBAL_UNLOCK(); 1353 1354 if (ifp->if_slowtimo != NULL && ifp->if_slowtimo_ch != NULL) { 1355 ifp->if_slowtimo = NULL; 1356 callout_halt(ifp->if_slowtimo_ch, NULL); 1357 callout_destroy(ifp->if_slowtimo_ch); 1358 kmem_free(ifp->if_slowtimo_ch, sizeof(*ifp->if_slowtimo_ch)); 1359 } 1360 if_deferred_start_destroy(ifp); 1361 1362 /* 1363 * Do an if_down() to give protocols a chance to do something. 1364 */ 1365 if_down_deactivated(ifp); 1366 1367 #ifdef ALTQ 1368 if (ALTQ_IS_ENABLED(&ifp->if_snd)) 1369 altq_disable(&ifp->if_snd); 1370 if (ALTQ_IS_ATTACHED(&ifp->if_snd)) 1371 altq_detach(&ifp->if_snd); 1372 #endif 1373 1374 #if NCARP > 0 1375 /* Remove the interface from any carp group it is a part of. */ 1376 if (ifp->if_carp != NULL && ifp->if_type != IFT_CARP) 1377 carp_ifdetach(ifp); 1378 #endif 1379 1380 /* 1381 * Rip all the addresses off the interface. This should make 1382 * all of the routes go away. 1383 * 1384 * pr_usrreq calls can remove an arbitrary number of ifaddrs 1385 * from the list, including our "cursor", ifa. For safety, 1386 * and to honor the TAILQ abstraction, I just restart the 1387 * loop after each removal. Note that the loop will exit 1388 * when all of the remaining ifaddrs belong to the AF_LINK 1389 * family. I am counting on the historical fact that at 1390 * least one pr_usrreq in each address domain removes at 1391 * least one ifaddr. 1392 */ 1393 again: 1394 /* 1395 * At this point, no other one tries to remove ifa in the list, 1396 * so we don't need to take a lock or psref. Avoid using 1397 * IFADDR_READER_FOREACH to pass over an inspection of contract 1398 * violations of pserialize. 1399 */ 1400 IFADDR_WRITER_FOREACH(ifa, ifp) { 1401 family = ifa->ifa_addr->sa_family; 1402 #ifdef IFAREF_DEBUG 1403 printf("if_detach: ifaddr %p, family %d, refcnt %d\n", 1404 ifa, family, ifa->ifa_refcnt); 1405 if (last_ifa != NULL && ifa == last_ifa) 1406 panic("if_detach: loop detected"); 1407 last_ifa = ifa; 1408 #endif 1409 if (family == AF_LINK) 1410 continue; 1411 dp = pffinddomain(family); 1412 KASSERTMSG(dp != NULL, "no domain for AF %d", family); 1413 /* 1414 * XXX These PURGEIF calls are redundant with the 1415 * purge-all-families calls below, but are left in for 1416 * now both to make a smaller change, and to avoid 1417 * unplanned interactions with clearing of 1418 * ifp->if_addrlist. 1419 */ 1420 purged = 0; 1421 for (pr = dp->dom_protosw; 1422 pr < dp->dom_protoswNPROTOSW; pr++) { 1423 so.so_proto = pr; 1424 if (pr->pr_usrreqs) { 1425 (void) (*pr->pr_usrreqs->pr_purgeif)(&so, ifp); 1426 purged = 1; 1427 } 1428 } 1429 if (purged == 0) { 1430 /* 1431 * XXX What's really the best thing to do 1432 * XXX here? --thorpej@NetBSD.org 1433 */ 1434 printf("if_detach: WARNING: AF %d not purged\n", 1435 family); 1436 ifa_remove(ifp, ifa); 1437 } 1438 goto again; 1439 } 1440 1441 if_free_sadl(ifp, 1); 1442 1443 restart: 1444 IFADDR_WRITER_FOREACH(ifa, ifp) { 1445 family = ifa->ifa_addr->sa_family; 1446 KASSERT(family == AF_LINK); 1447 ifa_remove(ifp, ifa); 1448 goto restart; 1449 } 1450 1451 /* Delete stray routes from the routing table. */ 1452 for (i = 0; i <= AF_MAX; i++) 1453 rt_delete_matched_entries(i, if_delroute_matcher, ifp); 1454 1455 DOMAIN_FOREACH(dp) { 1456 if (dp->dom_ifdetach != NULL && ifp->if_afdata[dp->dom_family]) 1457 { 1458 void *p = ifp->if_afdata[dp->dom_family]; 1459 if (p) { 1460 ifp->if_afdata[dp->dom_family] = NULL; 1461 (*dp->dom_ifdetach)(ifp, p); 1462 } 1463 } 1464 1465 /* 1466 * One would expect multicast memberships (INET and 1467 * INET6) on UDP sockets to be purged by the PURGEIF 1468 * calls above, but if all addresses were removed from 1469 * the interface prior to destruction, the calls will 1470 * not be made (e.g. ppp, for which pppd(8) generally 1471 * removes addresses before destroying the interface). 1472 * Because there is no invariant that multicast 1473 * memberships only exist for interfaces with IPv4 1474 * addresses, we must call PURGEIF regardless of 1475 * addresses. (Protocols which might store ifnet 1476 * pointers are marked with PR_PURGEIF.) 1477 */ 1478 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) { 1479 so.so_proto = pr; 1480 if (pr->pr_usrreqs && pr->pr_flags & PR_PURGEIF) 1481 (void)(*pr->pr_usrreqs->pr_purgeif)(&so, ifp); 1482 } 1483 } 1484 1485 /* 1486 * Must be done after the above pr_purgeif because if_psref may be 1487 * still used in pr_purgeif. 1488 */ 1489 psref_target_destroy(&ifp->if_psref, ifnet_psref_class); 1490 PSLIST_ENTRY_DESTROY(ifp, if_pslist_entry); 1491 1492 pfil_run_ifhooks(if_pfil, PFIL_IFNET_DETACH, ifp); 1493 (void)pfil_head_destroy(ifp->if_pfil); 1494 1495 /* Announce that the interface is gone. */ 1496 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 1497 1498 IF_AFDATA_LOCK_DESTROY(ifp); 1499 1500 /* 1501 * remove packets that came from ifp, from software interrupt queues. 1502 */ 1503 DOMAIN_FOREACH(dp) { 1504 for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) { 1505 struct ifqueue *iq = dp->dom_ifqueues[i]; 1506 if (iq == NULL) 1507 break; 1508 dp->dom_ifqueues[i] = NULL; 1509 if_detach_queues(ifp, iq); 1510 } 1511 } 1512 1513 /* 1514 * IP queues have to be processed separately: net-queue barrier 1515 * ensures that the packets are dequeued while a cross-call will 1516 * ensure that the interrupts have completed. FIXME: not quite.. 1517 */ 1518 #ifdef INET 1519 pktq_barrier(ip_pktq); 1520 #endif 1521 #ifdef INET6 1522 if (in6_present) 1523 pktq_barrier(ip6_pktq); 1524 #endif 1525 xc_barrier(0); 1526 1527 if (ifp->if_percpuq != NULL) { 1528 if_percpuq_destroy(ifp->if_percpuq); 1529 ifp->if_percpuq = NULL; 1530 } 1531 1532 mutex_obj_free(ifp->if_ioctl_lock); 1533 ifp->if_ioctl_lock = NULL; 1534 mutex_obj_free(ifp->if_snd.ifq_lock); 1535 if_stats_fini(ifp); 1536 1537 splx(s); 1538 1539 #ifdef IFAREF_DEBUG 1540 if_check_and_free_ifa_list(ifp); 1541 #endif 1542 } 1543 1544 static void 1545 if_detach_queues(struct ifnet *ifp, struct ifqueue *q) 1546 { 1547 struct mbuf *m, *prev, *next; 1548 1549 prev = NULL; 1550 for (m = q->ifq_head; m != NULL; m = next) { 1551 KASSERT((m->m_flags & M_PKTHDR) != 0); 1552 1553 next = m->m_nextpkt; 1554 if (m->m_pkthdr.rcvif_index != ifp->if_index) { 1555 prev = m; 1556 continue; 1557 } 1558 1559 if (prev != NULL) 1560 prev->m_nextpkt = m->m_nextpkt; 1561 else 1562 q->ifq_head = m->m_nextpkt; 1563 if (q->ifq_tail == m) 1564 q->ifq_tail = prev; 1565 q->ifq_len--; 1566 1567 m->m_nextpkt = NULL; 1568 m_freem(m); 1569 IF_DROP(q); 1570 } 1571 } 1572 1573 /* 1574 * Callback for a radix tree walk to delete all references to an 1575 * ifnet. 1576 */ 1577 static int 1578 if_delroute_matcher(struct rtentry *rt, void *v) 1579 { 1580 struct ifnet *ifp = (struct ifnet *)v; 1581 1582 if (rt->rt_ifp == ifp) 1583 return 1; 1584 else 1585 return 0; 1586 } 1587 1588 /* 1589 * Create a clone network interface. 1590 */ 1591 static int 1592 if_clone_create(const char *name) 1593 { 1594 struct if_clone *ifc; 1595 int unit; 1596 struct ifnet *ifp; 1597 struct psref psref; 1598 1599 KASSERT(mutex_owned(&if_clone_mtx)); 1600 1601 ifc = if_clone_lookup(name, &unit); 1602 if (ifc == NULL) 1603 return EINVAL; 1604 1605 ifp = if_get(name, &psref); 1606 if (ifp != NULL) { 1607 if_put(ifp, &psref); 1608 return EEXIST; 1609 } 1610 1611 return (*ifc->ifc_create)(ifc, unit); 1612 } 1613 1614 /* 1615 * Destroy a clone network interface. 1616 */ 1617 static int 1618 if_clone_destroy(const char *name) 1619 { 1620 struct if_clone *ifc; 1621 struct ifnet *ifp; 1622 struct psref psref; 1623 int error; 1624 int (*if_ioctl)(struct ifnet *, u_long, void *); 1625 1626 KASSERT(mutex_owned(&if_clone_mtx)); 1627 1628 ifc = if_clone_lookup(name, NULL); 1629 if (ifc == NULL) 1630 return EINVAL; 1631 1632 if (ifc->ifc_destroy == NULL) 1633 return EOPNOTSUPP; 1634 1635 ifp = if_get(name, &psref); 1636 if (ifp == NULL) 1637 return ENXIO; 1638 1639 /* We have to disable ioctls here */ 1640 IFNET_LOCK(ifp); 1641 if_ioctl = ifp->if_ioctl; 1642 ifp->if_ioctl = if_nullioctl; 1643 IFNET_UNLOCK(ifp); 1644 1645 /* 1646 * We cannot call ifc_destroy with holding ifp. 1647 * Releasing ifp here is safe thanks to if_clone_mtx. 1648 */ 1649 if_put(ifp, &psref); 1650 1651 error = (*ifc->ifc_destroy)(ifp); 1652 1653 if (error != 0) { 1654 /* We have to restore if_ioctl on error */ 1655 IFNET_LOCK(ifp); 1656 ifp->if_ioctl = if_ioctl; 1657 IFNET_UNLOCK(ifp); 1658 } 1659 1660 return error; 1661 } 1662 1663 static bool 1664 if_is_unit(const char *name) 1665 { 1666 1667 while (*name != '\0') { 1668 if (*name < '0' || *name > '9') 1669 return false; 1670 name++; 1671 } 1672 1673 return true; 1674 } 1675 1676 /* 1677 * Look up a network interface cloner. 1678 */ 1679 static struct if_clone * 1680 if_clone_lookup(const char *name, int *unitp) 1681 { 1682 struct if_clone *ifc; 1683 const char *cp; 1684 char *dp, ifname[IFNAMSIZ + 3]; 1685 int unit; 1686 1687 KASSERT(mutex_owned(&if_clone_mtx)); 1688 1689 strcpy(ifname, "if_"); 1690 /* separate interface name from unit */ 1691 /* TODO: search unit number from backward */ 1692 for (dp = ifname + 3, cp = name; cp - name < IFNAMSIZ && 1693 *cp && !if_is_unit(cp);) 1694 *dp++ = *cp++; 1695 1696 if (cp == name || cp - name == IFNAMSIZ || !*cp) 1697 return NULL; /* No name or unit number */ 1698 *dp++ = '\0'; 1699 1700 again: 1701 LIST_FOREACH(ifc, &if_cloners, ifc_list) { 1702 if (strcmp(ifname + 3, ifc->ifc_name) == 0) 1703 break; 1704 } 1705 1706 if (ifc == NULL) { 1707 int error; 1708 if (*ifname == '\0') 1709 return NULL; 1710 mutex_exit(&if_clone_mtx); 1711 error = module_autoload(ifname, MODULE_CLASS_DRIVER); 1712 mutex_enter(&if_clone_mtx); 1713 if (error) 1714 return NULL; 1715 *ifname = '\0'; 1716 goto again; 1717 } 1718 1719 unit = 0; 1720 while (cp - name < IFNAMSIZ && *cp) { 1721 if (*cp < '0' || *cp > '9' || unit >= INT_MAX / 10) { 1722 /* Bogus unit number. */ 1723 return NULL; 1724 } 1725 unit = (unit * 10) + (*cp++ - '0'); 1726 } 1727 1728 if (unitp != NULL) 1729 *unitp = unit; 1730 return ifc; 1731 } 1732 1733 /* 1734 * Register a network interface cloner. 1735 */ 1736 void 1737 if_clone_attach(struct if_clone *ifc) 1738 { 1739 1740 mutex_enter(&if_clone_mtx); 1741 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list); 1742 if_cloners_count++; 1743 mutex_exit(&if_clone_mtx); 1744 } 1745 1746 /* 1747 * Unregister a network interface cloner. 1748 */ 1749 void 1750 if_clone_detach(struct if_clone *ifc) 1751 { 1752 1753 mutex_enter(&if_clone_mtx); 1754 LIST_REMOVE(ifc, ifc_list); 1755 if_cloners_count--; 1756 mutex_exit(&if_clone_mtx); 1757 } 1758 1759 /* 1760 * Provide list of interface cloners to userspace. 1761 */ 1762 int 1763 if_clone_list(int buf_count, char *buffer, int *total) 1764 { 1765 char outbuf[IFNAMSIZ], *dst; 1766 struct if_clone *ifc; 1767 int count, error = 0; 1768 1769 mutex_enter(&if_clone_mtx); 1770 *total = if_cloners_count; 1771 if ((dst = buffer) == NULL) { 1772 /* Just asking how many there are. */ 1773 goto out; 1774 } 1775 1776 if (buf_count < 0) { 1777 error = EINVAL; 1778 goto out; 1779 } 1780 1781 count = (if_cloners_count < buf_count) ? 1782 if_cloners_count : buf_count; 1783 1784 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0; 1785 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) { 1786 (void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf)); 1787 if (outbuf[sizeof(outbuf) - 1] != '\0') { 1788 error = ENAMETOOLONG; 1789 goto out; 1790 } 1791 error = copyout(outbuf, dst, sizeof(outbuf)); 1792 if (error != 0) 1793 break; 1794 } 1795 1796 out: 1797 mutex_exit(&if_clone_mtx); 1798 return error; 1799 } 1800 1801 void 1802 ifa_psref_init(struct ifaddr *ifa) 1803 { 1804 1805 psref_target_init(&ifa->ifa_psref, ifa_psref_class); 1806 } 1807 1808 void 1809 ifaref(struct ifaddr *ifa) 1810 { 1811 1812 atomic_inc_uint(&ifa->ifa_refcnt); 1813 } 1814 1815 void 1816 ifafree(struct ifaddr *ifa) 1817 { 1818 KASSERT(ifa != NULL); 1819 KASSERTMSG(ifa->ifa_refcnt > 0, "ifa_refcnt=%d", ifa->ifa_refcnt); 1820 1821 if (atomic_dec_uint_nv(&ifa->ifa_refcnt) == 0) { 1822 free(ifa, M_IFADDR); 1823 } 1824 } 1825 1826 bool 1827 ifa_is_destroying(struct ifaddr *ifa) 1828 { 1829 1830 return ISSET(ifa->ifa_flags, IFA_DESTROYING); 1831 } 1832 1833 void 1834 ifa_insert(struct ifnet *ifp, struct ifaddr *ifa) 1835 { 1836 1837 ifa->ifa_ifp = ifp; 1838 1839 /* 1840 * Check MP-safety for IFEF_MPSAFE drivers. 1841 * Check !IFF_RUNNING for initialization routines that normally don't 1842 * take IFNET_LOCK but it's safe because there is no competitor. 1843 * XXX there are false positive cases because IFF_RUNNING can be off on 1844 * if_stop. 1845 */ 1846 KASSERT(!if_is_mpsafe(ifp) || !ISSET(ifp->if_flags, IFF_RUNNING) || 1847 IFNET_LOCKED(ifp)); 1848 1849 TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list); 1850 IFADDR_ENTRY_INIT(ifa); 1851 IFADDR_WRITER_INSERT_TAIL(ifp, ifa); 1852 1853 ifaref(ifa); 1854 } 1855 1856 void 1857 ifa_remove(struct ifnet *ifp, struct ifaddr *ifa) 1858 { 1859 1860 KASSERT(ifa->ifa_ifp == ifp); 1861 /* 1862 * Check MP-safety for IFEF_MPSAFE drivers. 1863 * if_is_deactivated indicates ifa_remove is called form if_detach 1864 * where is safe even if IFNET_LOCK isn't held. 1865 */ 1866 KASSERT(!if_is_mpsafe(ifp) || if_is_deactivated(ifp) || IFNET_LOCKED(ifp)); 1867 1868 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list); 1869 IFADDR_WRITER_REMOVE(ifa); 1870 #ifdef NET_MPSAFE 1871 IFNET_GLOBAL_LOCK(); 1872 pserialize_perform(ifnet_psz); 1873 IFNET_GLOBAL_UNLOCK(); 1874 #endif 1875 1876 #ifdef NET_MPSAFE 1877 psref_target_destroy(&ifa->ifa_psref, ifa_psref_class); 1878 #endif 1879 IFADDR_ENTRY_DESTROY(ifa); 1880 ifafree(ifa); 1881 } 1882 1883 void 1884 ifa_acquire(struct ifaddr *ifa, struct psref *psref) 1885 { 1886 1887 PSREF_DEBUG_FILL_RETURN_ADDRESS(psref); 1888 psref_acquire(psref, &ifa->ifa_psref, ifa_psref_class); 1889 } 1890 1891 void 1892 ifa_release(struct ifaddr *ifa, struct psref *psref) 1893 { 1894 1895 if (ifa == NULL) 1896 return; 1897 1898 psref_release(psref, &ifa->ifa_psref, ifa_psref_class); 1899 } 1900 1901 bool 1902 ifa_held(struct ifaddr *ifa) 1903 { 1904 1905 return psref_held(&ifa->ifa_psref, ifa_psref_class); 1906 } 1907 1908 static inline int 1909 equal(const struct sockaddr *sa1, const struct sockaddr *sa2) 1910 { 1911 return sockaddr_cmp(sa1, sa2) == 0; 1912 } 1913 1914 /* 1915 * Locate an interface based on a complete address. 1916 */ 1917 /*ARGSUSED*/ 1918 struct ifaddr * 1919 ifa_ifwithaddr(const struct sockaddr *addr) 1920 { 1921 struct ifnet *ifp; 1922 struct ifaddr *ifa; 1923 1924 IFNET_READER_FOREACH(ifp) { 1925 if (if_is_deactivated(ifp)) 1926 continue; 1927 IFADDR_READER_FOREACH(ifa, ifp) { 1928 if (ifa->ifa_addr->sa_family != addr->sa_family) 1929 continue; 1930 if (equal(addr, ifa->ifa_addr)) 1931 return ifa; 1932 if ((ifp->if_flags & IFF_BROADCAST) && 1933 ifa->ifa_broadaddr && 1934 /* IP6 doesn't have broadcast */ 1935 ifa->ifa_broadaddr->sa_len != 0 && 1936 equal(ifa->ifa_broadaddr, addr)) 1937 return ifa; 1938 } 1939 } 1940 return NULL; 1941 } 1942 1943 struct ifaddr * 1944 ifa_ifwithaddr_psref(const struct sockaddr *addr, struct psref *psref) 1945 { 1946 struct ifaddr *ifa; 1947 int s = pserialize_read_enter(); 1948 1949 ifa = ifa_ifwithaddr(addr); 1950 if (ifa != NULL) 1951 ifa_acquire(ifa, psref); 1952 pserialize_read_exit(s); 1953 1954 return ifa; 1955 } 1956 1957 /* 1958 * Locate the point to point interface with a given destination address. 1959 */ 1960 /*ARGSUSED*/ 1961 struct ifaddr * 1962 ifa_ifwithdstaddr(const struct sockaddr *addr) 1963 { 1964 struct ifnet *ifp; 1965 struct ifaddr *ifa; 1966 1967 IFNET_READER_FOREACH(ifp) { 1968 if (if_is_deactivated(ifp)) 1969 continue; 1970 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 1971 continue; 1972 IFADDR_READER_FOREACH(ifa, ifp) { 1973 if (ifa->ifa_addr->sa_family != addr->sa_family || 1974 ifa->ifa_dstaddr == NULL) 1975 continue; 1976 if (equal(addr, ifa->ifa_dstaddr)) 1977 return ifa; 1978 } 1979 } 1980 1981 return NULL; 1982 } 1983 1984 struct ifaddr * 1985 ifa_ifwithdstaddr_psref(const struct sockaddr *addr, struct psref *psref) 1986 { 1987 struct ifaddr *ifa; 1988 int s; 1989 1990 s = pserialize_read_enter(); 1991 ifa = ifa_ifwithdstaddr(addr); 1992 if (ifa != NULL) 1993 ifa_acquire(ifa, psref); 1994 pserialize_read_exit(s); 1995 1996 return ifa; 1997 } 1998 1999 /* 2000 * Find an interface on a specific network. If many, choice 2001 * is most specific found. 2002 */ 2003 struct ifaddr * 2004 ifa_ifwithnet(const struct sockaddr *addr) 2005 { 2006 struct ifnet *ifp; 2007 struct ifaddr *ifa, *ifa_maybe = NULL; 2008 const struct sockaddr_dl *sdl; 2009 u_int af = addr->sa_family; 2010 const char *addr_data = addr->sa_data, *cplim; 2011 2012 if (af == AF_LINK) { 2013 sdl = satocsdl(addr); 2014 if (sdl->sdl_index && sdl->sdl_index < if_indexlim && 2015 ifindex2ifnet[sdl->sdl_index] && 2016 !if_is_deactivated(ifindex2ifnet[sdl->sdl_index])) { 2017 return ifindex2ifnet[sdl->sdl_index]->if_dl; 2018 } 2019 } 2020 #ifdef NETATALK 2021 if (af == AF_APPLETALK) { 2022 const struct sockaddr_at *sat, *sat2; 2023 sat = (const struct sockaddr_at *)addr; 2024 IFNET_READER_FOREACH(ifp) { 2025 if (if_is_deactivated(ifp)) 2026 continue; 2027 ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp); 2028 if (ifa == NULL) 2029 continue; 2030 sat2 = (struct sockaddr_at *)ifa->ifa_addr; 2031 if (sat2->sat_addr.s_net == sat->sat_addr.s_net) 2032 return ifa; /* exact match */ 2033 if (ifa_maybe == NULL) { 2034 /* else keep the if with the right range */ 2035 ifa_maybe = ifa; 2036 } 2037 } 2038 return ifa_maybe; 2039 } 2040 #endif 2041 IFNET_READER_FOREACH(ifp) { 2042 if (if_is_deactivated(ifp)) 2043 continue; 2044 IFADDR_READER_FOREACH(ifa, ifp) { 2045 const char *cp, *cp2, *cp3; 2046 2047 if (ifa->ifa_addr->sa_family != af || 2048 ifa->ifa_netmask == NULL) 2049 next: continue; 2050 cp = addr_data; 2051 cp2 = ifa->ifa_addr->sa_data; 2052 cp3 = ifa->ifa_netmask->sa_data; 2053 cplim = (const char *)ifa->ifa_netmask + 2054 ifa->ifa_netmask->sa_len; 2055 while (cp3 < cplim) { 2056 if ((*cp++ ^ *cp2++) & *cp3++) { 2057 /* want to continue for() loop */ 2058 goto next; 2059 } 2060 } 2061 if (ifa_maybe == NULL || 2062 rt_refines(ifa->ifa_netmask, 2063 ifa_maybe->ifa_netmask)) 2064 ifa_maybe = ifa; 2065 } 2066 } 2067 return ifa_maybe; 2068 } 2069 2070 struct ifaddr * 2071 ifa_ifwithnet_psref(const struct sockaddr *addr, struct psref *psref) 2072 { 2073 struct ifaddr *ifa; 2074 int s; 2075 2076 s = pserialize_read_enter(); 2077 ifa = ifa_ifwithnet(addr); 2078 if (ifa != NULL) 2079 ifa_acquire(ifa, psref); 2080 pserialize_read_exit(s); 2081 2082 return ifa; 2083 } 2084 2085 /* 2086 * Find the interface of the addresss. 2087 */ 2088 struct ifaddr * 2089 ifa_ifwithladdr(const struct sockaddr *addr) 2090 { 2091 struct ifaddr *ia; 2092 2093 if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) || 2094 (ia = ifa_ifwithnet(addr))) 2095 return ia; 2096 return NULL; 2097 } 2098 2099 struct ifaddr * 2100 ifa_ifwithladdr_psref(const struct sockaddr *addr, struct psref *psref) 2101 { 2102 struct ifaddr *ifa; 2103 int s; 2104 2105 s = pserialize_read_enter(); 2106 ifa = ifa_ifwithladdr(addr); 2107 if (ifa != NULL) 2108 ifa_acquire(ifa, psref); 2109 pserialize_read_exit(s); 2110 2111 return ifa; 2112 } 2113 2114 /* 2115 * Find an interface using a specific address family 2116 */ 2117 struct ifaddr * 2118 ifa_ifwithaf(int af) 2119 { 2120 struct ifnet *ifp; 2121 struct ifaddr *ifa = NULL; 2122 int s; 2123 2124 s = pserialize_read_enter(); 2125 IFNET_READER_FOREACH(ifp) { 2126 if (if_is_deactivated(ifp)) 2127 continue; 2128 IFADDR_READER_FOREACH(ifa, ifp) { 2129 if (ifa->ifa_addr->sa_family == af) 2130 goto out; 2131 } 2132 } 2133 out: 2134 pserialize_read_exit(s); 2135 return ifa; 2136 } 2137 2138 /* 2139 * Find an interface address specific to an interface best matching 2140 * a given address. 2141 */ 2142 struct ifaddr * 2143 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp) 2144 { 2145 struct ifaddr *ifa; 2146 const char *cp, *cp2, *cp3; 2147 const char *cplim; 2148 struct ifaddr *ifa_maybe = 0; 2149 u_int af = addr->sa_family; 2150 2151 if (if_is_deactivated(ifp)) 2152 return NULL; 2153 2154 if (af >= AF_MAX) 2155 return NULL; 2156 2157 IFADDR_READER_FOREACH(ifa, ifp) { 2158 if (ifa->ifa_addr->sa_family != af) 2159 continue; 2160 ifa_maybe = ifa; 2161 if (ifa->ifa_netmask == NULL) { 2162 if (equal(addr, ifa->ifa_addr) || 2163 (ifa->ifa_dstaddr && 2164 equal(addr, ifa->ifa_dstaddr))) 2165 return ifa; 2166 continue; 2167 } 2168 cp = addr->sa_data; 2169 cp2 = ifa->ifa_addr->sa_data; 2170 cp3 = ifa->ifa_netmask->sa_data; 2171 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 2172 for (; cp3 < cplim; cp3++) { 2173 if ((*cp++ ^ *cp2++) & *cp3) 2174 break; 2175 } 2176 if (cp3 == cplim) 2177 return ifa; 2178 } 2179 return ifa_maybe; 2180 } 2181 2182 struct ifaddr * 2183 ifaof_ifpforaddr_psref(const struct sockaddr *addr, struct ifnet *ifp, 2184 struct psref *psref) 2185 { 2186 struct ifaddr *ifa; 2187 int s; 2188 2189 s = pserialize_read_enter(); 2190 ifa = ifaof_ifpforaddr(addr, ifp); 2191 if (ifa != NULL) 2192 ifa_acquire(ifa, psref); 2193 pserialize_read_exit(s); 2194 2195 return ifa; 2196 } 2197 2198 /* 2199 * Default action when installing a route with a Link Level gateway. 2200 * Lookup an appropriate real ifa to point to. 2201 * This should be moved to /sys/net/link.c eventually. 2202 */ 2203 void 2204 link_rtrequest(int cmd, struct rtentry *rt, const struct rt_addrinfo *info) 2205 { 2206 struct ifaddr *ifa; 2207 const struct sockaddr *dst; 2208 struct ifnet *ifp; 2209 struct psref psref; 2210 2211 if (cmd != RTM_ADD || ISSET(info->rti_flags, RTF_DONTCHANGEIFA)) 2212 return; 2213 ifp = rt->rt_ifa->ifa_ifp; 2214 dst = rt_getkey(rt); 2215 if ((ifa = ifaof_ifpforaddr_psref(dst, ifp, &psref)) != NULL) { 2216 rt_replace_ifa(rt, ifa); 2217 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 2218 ifa->ifa_rtrequest(cmd, rt, info); 2219 ifa_release(ifa, &psref); 2220 } 2221 } 2222 2223 /* 2224 * bitmask macros to manage a densely packed link_state change queue. 2225 * Because we need to store LINK_STATE_UNKNOWN(0), LINK_STATE_DOWN(1) and 2226 * LINK_STATE_UP(2) we need 2 bits for each state change. 2227 * As a state change to store is 0, treat all bits set as an unset item. 2228 */ 2229 #define LQ_ITEM_BITS 2 2230 #define LQ_ITEM_MASK ((1 << LQ_ITEM_BITS) - 1) 2231 #define LQ_MASK(i) (LQ_ITEM_MASK << (i) * LQ_ITEM_BITS) 2232 #define LINK_STATE_UNSET LQ_ITEM_MASK 2233 #define LQ_ITEM(q, i) (((q) & LQ_MASK((i))) >> (i) * LQ_ITEM_BITS) 2234 #define LQ_STORE(q, i, v) \ 2235 do { \ 2236 (q) &= ~LQ_MASK((i)); \ 2237 (q) |= (v) << (i) * LQ_ITEM_BITS; \ 2238 } while (0 /* CONSTCOND */) 2239 #define LQ_MAX(q) ((sizeof((q)) * NBBY) / LQ_ITEM_BITS) 2240 #define LQ_POP(q, v) \ 2241 do { \ 2242 (v) = LQ_ITEM((q), 0); \ 2243 (q) >>= LQ_ITEM_BITS; \ 2244 (q) |= LINK_STATE_UNSET << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS; \ 2245 } while (0 /* CONSTCOND */) 2246 #define LQ_PUSH(q, v) \ 2247 do { \ 2248 (q) >>= LQ_ITEM_BITS; \ 2249 (q) |= (v) << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS; \ 2250 } while (0 /* CONSTCOND */) 2251 #define LQ_FIND_UNSET(q, i) \ 2252 for ((i) = 0; i < LQ_MAX((q)); (i)++) { \ 2253 if (LQ_ITEM((q), (i)) == LINK_STATE_UNSET) \ 2254 break; \ 2255 } 2256 2257 /* 2258 * Handle a change in the interface link state and 2259 * queue notifications. 2260 */ 2261 void 2262 if_link_state_change(struct ifnet *ifp, int link_state) 2263 { 2264 int idx; 2265 2266 /* Ensure change is to a valid state */ 2267 switch (link_state) { 2268 case LINK_STATE_UNKNOWN: /* FALLTHROUGH */ 2269 case LINK_STATE_DOWN: /* FALLTHROUGH */ 2270 case LINK_STATE_UP: 2271 break; 2272 default: 2273 #ifdef DEBUG 2274 printf("%s: invalid link state %d\n", 2275 ifp->if_xname, link_state); 2276 #endif 2277 return; 2278 } 2279 2280 IF_LINK_STATE_CHANGE_LOCK(ifp); 2281 2282 /* Find the last unset event in the queue. */ 2283 LQ_FIND_UNSET(ifp->if_link_queue, idx); 2284 2285 if (idx == 0) { 2286 /* 2287 * There is no queue of link state changes. 2288 * As we have the lock we can safely compare against the 2289 * current link state and return if the same. 2290 * Otherwise, if scheduled is true then the interface is being 2291 * detached and the queue is being drained so we need 2292 * to avoid queuing more work. 2293 */ 2294 if (ifp->if_link_state == link_state || ifp->if_link_scheduled) 2295 goto out; 2296 } else { 2297 /* Ensure link_state doesn't match the last queued state. */ 2298 if (LQ_ITEM(ifp->if_link_queue, idx - 1) == (uint8_t)link_state) 2299 goto out; 2300 } 2301 2302 /* Handle queue overflow. */ 2303 if (idx == LQ_MAX(ifp->if_link_queue)) { 2304 uint8_t lost; 2305 2306 /* 2307 * The DOWN state must be protected from being pushed off 2308 * the queue to ensure that userland will always be 2309 * in a sane state. 2310 * Because DOWN is protected, there is no need to protect 2311 * UNKNOWN. 2312 * It should be invalid to change from any other state to 2313 * UNKNOWN anyway ... 2314 */ 2315 lost = LQ_ITEM(ifp->if_link_queue, 0); 2316 LQ_PUSH(ifp->if_link_queue, (uint8_t)link_state); 2317 if (lost == LINK_STATE_DOWN) { 2318 lost = LQ_ITEM(ifp->if_link_queue, 0); 2319 LQ_STORE(ifp->if_link_queue, 0, LINK_STATE_DOWN); 2320 } 2321 printf("%s: lost link state change %s\n", 2322 ifp->if_xname, 2323 lost == LINK_STATE_UP ? "UP" : 2324 lost == LINK_STATE_DOWN ? "DOWN" : 2325 "UNKNOWN"); 2326 } else 2327 LQ_STORE(ifp->if_link_queue, idx, (uint8_t)link_state); 2328 2329 if (ifp->if_link_scheduled) 2330 goto out; 2331 2332 ifp->if_link_scheduled = true; 2333 workqueue_enqueue(ifnet_link_state_wq, &ifp->if_link_work, NULL); 2334 2335 out: 2336 IF_LINK_STATE_CHANGE_UNLOCK(ifp); 2337 } 2338 2339 /* 2340 * Handle interface link state change notifications. 2341 */ 2342 static void 2343 if_link_state_change_process(struct ifnet *ifp, int link_state) 2344 { 2345 struct domain *dp; 2346 int s = splnet(); 2347 bool notify; 2348 2349 KASSERT(!cpu_intr_p()); 2350 2351 IF_LINK_STATE_CHANGE_LOCK(ifp); 2352 2353 /* Ensure the change is still valid. */ 2354 if (ifp->if_link_state == link_state) { 2355 IF_LINK_STATE_CHANGE_UNLOCK(ifp); 2356 splx(s); 2357 return; 2358 } 2359 2360 #ifdef DEBUG 2361 log(LOG_DEBUG, "%s: link state %s (was %s)\n", ifp->if_xname, 2362 link_state == LINK_STATE_UP ? "UP" : 2363 link_state == LINK_STATE_DOWN ? "DOWN" : 2364 "UNKNOWN", 2365 ifp->if_link_state == LINK_STATE_UP ? "UP" : 2366 ifp->if_link_state == LINK_STATE_DOWN ? "DOWN" : 2367 "UNKNOWN"); 2368 #endif 2369 2370 /* 2371 * When going from UNKNOWN to UP, we need to mark existing 2372 * addresses as tentative and restart DAD as we may have 2373 * erroneously not found a duplicate. 2374 * 2375 * This needs to happen before rt_ifmsg to avoid a race where 2376 * listeners would have an address and expect it to work right 2377 * away. 2378 */ 2379 notify = (link_state == LINK_STATE_UP && 2380 ifp->if_link_state == LINK_STATE_UNKNOWN); 2381 ifp->if_link_state = link_state; 2382 /* The following routines may sleep so release the spin mutex */ 2383 IF_LINK_STATE_CHANGE_UNLOCK(ifp); 2384 2385 KERNEL_LOCK_UNLESS_NET_MPSAFE(); 2386 if (notify) { 2387 DOMAIN_FOREACH(dp) { 2388 if (dp->dom_if_link_state_change != NULL) 2389 dp->dom_if_link_state_change(ifp, 2390 LINK_STATE_DOWN); 2391 } 2392 } 2393 2394 /* Notify that the link state has changed. */ 2395 rt_ifmsg(ifp); 2396 2397 #if NCARP > 0 2398 if (ifp->if_carp) 2399 carp_carpdev_state(ifp); 2400 #endif 2401 2402 if (ifp->if_link_state_changed != NULL) 2403 ifp->if_link_state_changed(ifp, link_state); 2404 2405 #if NBRIDGE > 0 2406 if (ifp->if_bridge != NULL) 2407 bridge_calc_link_state(ifp->if_bridge); 2408 #endif 2409 2410 #if NLAGG > 0 2411 if (ifp->if_lagg != NULL) 2412 lagg_linkstate_changed(ifp); 2413 #endif 2414 2415 DOMAIN_FOREACH(dp) { 2416 if (dp->dom_if_link_state_change != NULL) 2417 dp->dom_if_link_state_change(ifp, link_state); 2418 } 2419 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 2420 splx(s); 2421 } 2422 2423 /* 2424 * Process the interface link state change queue. 2425 */ 2426 static void 2427 if_link_state_change_work(struct work *work, void *arg) 2428 { 2429 struct ifnet *ifp = container_of(work, struct ifnet, if_link_work); 2430 int s; 2431 uint8_t state; 2432 2433 KERNEL_LOCK_UNLESS_NET_MPSAFE(); 2434 s = splnet(); 2435 2436 /* Pop a link state change from the queue and process it. 2437 * If there is nothing to process then if_detach() has been called. 2438 * We keep if_link_scheduled = true so the queue can safely drain 2439 * without more work being queued. */ 2440 IF_LINK_STATE_CHANGE_LOCK(ifp); 2441 LQ_POP(ifp->if_link_queue, state); 2442 IF_LINK_STATE_CHANGE_UNLOCK(ifp); 2443 if (state == LINK_STATE_UNSET) 2444 goto out; 2445 2446 if_link_state_change_process(ifp, state); 2447 2448 /* If there is a link state change to come, schedule it. */ 2449 IF_LINK_STATE_CHANGE_LOCK(ifp); 2450 if (LQ_ITEM(ifp->if_link_queue, 0) != LINK_STATE_UNSET) { 2451 ifp->if_link_scheduled = true; 2452 workqueue_enqueue(ifnet_link_state_wq, &ifp->if_link_work, NULL); 2453 } else 2454 ifp->if_link_scheduled = false; 2455 IF_LINK_STATE_CHANGE_UNLOCK(ifp); 2456 2457 out: 2458 splx(s); 2459 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 2460 } 2461 2462 /* 2463 * Used to mark addresses on an interface as DETATCHED or TENTATIVE 2464 * and thus start Duplicate Address Detection without changing the 2465 * real link state. 2466 */ 2467 void 2468 if_domain_link_state_change(struct ifnet *ifp, int link_state) 2469 { 2470 struct domain *dp; 2471 int s = splnet(); 2472 2473 KERNEL_LOCK_UNLESS_NET_MPSAFE(); 2474 2475 DOMAIN_FOREACH(dp) { 2476 if (dp->dom_if_link_state_change != NULL) 2477 dp->dom_if_link_state_change(ifp, link_state); 2478 } 2479 2480 splx(s); 2481 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 2482 } 2483 2484 /* 2485 * Default action when installing a local route on a point-to-point 2486 * interface. 2487 */ 2488 void 2489 p2p_rtrequest(int req, struct rtentry *rt, 2490 __unused const struct rt_addrinfo *info) 2491 { 2492 struct ifnet *ifp = rt->rt_ifp; 2493 struct ifaddr *ifa, *lo0ifa; 2494 int s = pserialize_read_enter(); 2495 2496 switch (req) { 2497 case RTM_ADD: 2498 if ((rt->rt_flags & RTF_LOCAL) == 0) 2499 break; 2500 2501 rt->rt_ifp = lo0ifp; 2502 2503 if (ISSET(info->rti_flags, RTF_DONTCHANGEIFA)) 2504 break; 2505 2506 IFADDR_READER_FOREACH(ifa, ifp) { 2507 if (equal(rt_getkey(rt), ifa->ifa_addr)) 2508 break; 2509 } 2510 if (ifa == NULL) 2511 break; 2512 2513 /* 2514 * Ensure lo0 has an address of the same family. 2515 */ 2516 IFADDR_READER_FOREACH(lo0ifa, lo0ifp) { 2517 if (lo0ifa->ifa_addr->sa_family == 2518 ifa->ifa_addr->sa_family) 2519 break; 2520 } 2521 if (lo0ifa == NULL) 2522 break; 2523 2524 /* 2525 * Make sure to set rt->rt_ifa to the interface 2526 * address we are using, otherwise we will have trouble 2527 * with source address selection. 2528 */ 2529 if (ifa != rt->rt_ifa) 2530 rt_replace_ifa(rt, ifa); 2531 break; 2532 case RTM_DELETE: 2533 default: 2534 break; 2535 } 2536 pserialize_read_exit(s); 2537 } 2538 2539 static void 2540 _if_down(struct ifnet *ifp) 2541 { 2542 struct ifaddr *ifa; 2543 struct domain *dp; 2544 int s, bound; 2545 struct psref psref; 2546 2547 ifp->if_flags &= ~IFF_UP; 2548 nanotime(&ifp->if_lastchange); 2549 2550 bound = curlwp_bind(); 2551 s = pserialize_read_enter(); 2552 IFADDR_READER_FOREACH(ifa, ifp) { 2553 ifa_acquire(ifa, &psref); 2554 pserialize_read_exit(s); 2555 2556 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 2557 2558 s = pserialize_read_enter(); 2559 ifa_release(ifa, &psref); 2560 } 2561 pserialize_read_exit(s); 2562 curlwp_bindx(bound); 2563 2564 IFQ_PURGE(&ifp->if_snd); 2565 #if NCARP > 0 2566 if (ifp->if_carp) 2567 carp_carpdev_state(ifp); 2568 #endif 2569 rt_ifmsg(ifp); 2570 DOMAIN_FOREACH(dp) { 2571 if (dp->dom_if_down) 2572 dp->dom_if_down(ifp); 2573 } 2574 } 2575 2576 static void 2577 if_down_deactivated(struct ifnet *ifp) 2578 { 2579 2580 KASSERT(if_is_deactivated(ifp)); 2581 _if_down(ifp); 2582 } 2583 2584 void 2585 if_down_locked(struct ifnet *ifp) 2586 { 2587 2588 KASSERT(IFNET_LOCKED(ifp)); 2589 _if_down(ifp); 2590 } 2591 2592 /* 2593 * Mark an interface down and notify protocols of 2594 * the transition. 2595 * NOTE: must be called at splsoftnet or equivalent. 2596 */ 2597 void 2598 if_down(struct ifnet *ifp) 2599 { 2600 2601 IFNET_LOCK(ifp); 2602 if_down_locked(ifp); 2603 IFNET_UNLOCK(ifp); 2604 } 2605 2606 /* 2607 * Must be called with holding if_ioctl_lock. 2608 */ 2609 static void 2610 if_up_locked(struct ifnet *ifp) 2611 { 2612 #ifdef notyet 2613 struct ifaddr *ifa; 2614 #endif 2615 struct domain *dp; 2616 2617 KASSERT(IFNET_LOCKED(ifp)); 2618 2619 KASSERT(!if_is_deactivated(ifp)); 2620 ifp->if_flags |= IFF_UP; 2621 nanotime(&ifp->if_lastchange); 2622 #ifdef notyet 2623 /* this has no effect on IP, and will kill all ISO connections XXX */ 2624 IFADDR_READER_FOREACH(ifa, ifp) 2625 pfctlinput(PRC_IFUP, ifa->ifa_addr); 2626 #endif 2627 #if NCARP > 0 2628 if (ifp->if_carp) 2629 carp_carpdev_state(ifp); 2630 #endif 2631 rt_ifmsg(ifp); 2632 DOMAIN_FOREACH(dp) { 2633 if (dp->dom_if_up) 2634 dp->dom_if_up(ifp); 2635 } 2636 } 2637 2638 /* 2639 * Handle interface slowtimo timer routine. Called 2640 * from softclock, we decrement timer (if set) and 2641 * call the appropriate interface routine on expiration. 2642 */ 2643 static void 2644 if_slowtimo(void *arg) 2645 { 2646 void (*slowtimo)(struct ifnet *); 2647 struct ifnet *ifp = arg; 2648 int s; 2649 2650 slowtimo = ifp->if_slowtimo; 2651 if (__predict_false(slowtimo == NULL)) 2652 return; 2653 2654 s = splnet(); 2655 if (ifp->if_timer != 0 && --ifp->if_timer == 0) 2656 (*slowtimo)(ifp); 2657 2658 splx(s); 2659 2660 if (__predict_true(ifp->if_slowtimo != NULL)) 2661 callout_schedule(ifp->if_slowtimo_ch, hz / IFNET_SLOWHZ); 2662 } 2663 2664 /* 2665 * Mark an interface up and notify protocols of 2666 * the transition. 2667 * NOTE: must be called at splsoftnet or equivalent. 2668 */ 2669 void 2670 if_up(struct ifnet *ifp) 2671 { 2672 2673 IFNET_LOCK(ifp); 2674 if_up_locked(ifp); 2675 IFNET_UNLOCK(ifp); 2676 } 2677 2678 /* 2679 * Set/clear promiscuous mode on interface ifp based on the truth value 2680 * of pswitch. The calls are reference counted so that only the first 2681 * "on" request actually has an effect, as does the final "off" request. 2682 * Results are undefined if the "off" and "on" requests are not matched. 2683 */ 2684 int 2685 ifpromisc_locked(struct ifnet *ifp, int pswitch) 2686 { 2687 int pcount, ret = 0; 2688 u_short nflags; 2689 2690 KASSERT(IFNET_LOCKED(ifp)); 2691 2692 pcount = ifp->if_pcount; 2693 if (pswitch) { 2694 /* 2695 * Allow the device to be "placed" into promiscuous 2696 * mode even if it is not configured up. It will 2697 * consult IFF_PROMISC when it is brought up. 2698 */ 2699 if (ifp->if_pcount++ != 0) 2700 goto out; 2701 nflags = ifp->if_flags | IFF_PROMISC; 2702 } else { 2703 if (--ifp->if_pcount > 0) 2704 goto out; 2705 nflags = ifp->if_flags & ~IFF_PROMISC; 2706 } 2707 ret = if_flags_set(ifp, nflags); 2708 /* Restore interface state if not successful. */ 2709 if (ret != 0) { 2710 ifp->if_pcount = pcount; 2711 } 2712 out: 2713 return ret; 2714 } 2715 2716 int 2717 ifpromisc(struct ifnet *ifp, int pswitch) 2718 { 2719 int e; 2720 2721 IFNET_LOCK(ifp); 2722 e = ifpromisc_locked(ifp, pswitch); 2723 IFNET_UNLOCK(ifp); 2724 2725 return e; 2726 } 2727 2728 /* 2729 * Map interface name to 2730 * interface structure pointer. 2731 */ 2732 struct ifnet * 2733 ifunit(const char *name) 2734 { 2735 struct ifnet *ifp; 2736 const char *cp = name; 2737 u_int unit = 0; 2738 u_int i; 2739 int s; 2740 2741 /* 2742 * If the entire name is a number, treat it as an ifindex. 2743 */ 2744 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) { 2745 unit = unit * 10 + (*cp - '0'); 2746 } 2747 2748 /* 2749 * If the number took all of the name, then it's a valid ifindex. 2750 */ 2751 if (i == IFNAMSIZ || (cp != name && *cp == '\0')) 2752 return if_byindex(unit); 2753 2754 ifp = NULL; 2755 s = pserialize_read_enter(); 2756 IFNET_READER_FOREACH(ifp) { 2757 if (if_is_deactivated(ifp)) 2758 continue; 2759 if (strcmp(ifp->if_xname, name) == 0) 2760 goto out; 2761 } 2762 out: 2763 pserialize_read_exit(s); 2764 return ifp; 2765 } 2766 2767 /* 2768 * Get a reference of an ifnet object by an interface name. 2769 * The returned reference is protected by psref(9). The caller 2770 * must release a returned reference by if_put after use. 2771 */ 2772 struct ifnet * 2773 if_get(const char *name, struct psref *psref) 2774 { 2775 struct ifnet *ifp; 2776 const char *cp = name; 2777 u_int unit = 0; 2778 u_int i; 2779 int s; 2780 2781 /* 2782 * If the entire name is a number, treat it as an ifindex. 2783 */ 2784 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) { 2785 unit = unit * 10 + (*cp - '0'); 2786 } 2787 2788 /* 2789 * If the number took all of the name, then it's a valid ifindex. 2790 */ 2791 if (i == IFNAMSIZ || (cp != name && *cp == '\0')) 2792 return if_get_byindex(unit, psref); 2793 2794 ifp = NULL; 2795 s = pserialize_read_enter(); 2796 IFNET_READER_FOREACH(ifp) { 2797 if (if_is_deactivated(ifp)) 2798 continue; 2799 if (strcmp(ifp->if_xname, name) == 0) { 2800 PSREF_DEBUG_FILL_RETURN_ADDRESS(psref); 2801 psref_acquire(psref, &ifp->if_psref, 2802 ifnet_psref_class); 2803 goto out; 2804 } 2805 } 2806 out: 2807 pserialize_read_exit(s); 2808 return ifp; 2809 } 2810 2811 /* 2812 * Release a reference of an ifnet object given by if_get, if_get_byindex 2813 * or if_get_bylla. 2814 */ 2815 void 2816 if_put(const struct ifnet *ifp, struct psref *psref) 2817 { 2818 2819 if (ifp == NULL) 2820 return; 2821 2822 psref_release(psref, &ifp->if_psref, ifnet_psref_class); 2823 } 2824 2825 /* 2826 * Return ifp having idx. Return NULL if not found. Normally if_byindex 2827 * should be used. 2828 */ 2829 ifnet_t * 2830 _if_byindex(u_int idx) 2831 { 2832 2833 return (__predict_true(idx < if_indexlim)) ? ifindex2ifnet[idx] : NULL; 2834 } 2835 2836 /* 2837 * Return ifp having idx. Return NULL if not found or the found ifp is 2838 * already deactivated. 2839 */ 2840 ifnet_t * 2841 if_byindex(u_int idx) 2842 { 2843 ifnet_t *ifp; 2844 2845 ifp = _if_byindex(idx); 2846 if (ifp != NULL && if_is_deactivated(ifp)) 2847 ifp = NULL; 2848 return ifp; 2849 } 2850 2851 /* 2852 * Get a reference of an ifnet object by an interface index. 2853 * The returned reference is protected by psref(9). The caller 2854 * must release a returned reference by if_put after use. 2855 */ 2856 ifnet_t * 2857 if_get_byindex(u_int idx, struct psref *psref) 2858 { 2859 ifnet_t *ifp; 2860 int s; 2861 2862 s = pserialize_read_enter(); 2863 ifp = if_byindex(idx); 2864 if (__predict_true(ifp != NULL)) { 2865 PSREF_DEBUG_FILL_RETURN_ADDRESS(psref); 2866 psref_acquire(psref, &ifp->if_psref, ifnet_psref_class); 2867 } 2868 pserialize_read_exit(s); 2869 2870 return ifp; 2871 } 2872 2873 ifnet_t * 2874 if_get_bylla(const void *lla, unsigned char lla_len, struct psref *psref) 2875 { 2876 ifnet_t *ifp; 2877 int s; 2878 2879 s = pserialize_read_enter(); 2880 IFNET_READER_FOREACH(ifp) { 2881 if (if_is_deactivated(ifp)) 2882 continue; 2883 if (ifp->if_addrlen != lla_len) 2884 continue; 2885 if (memcmp(lla, CLLADDR(ifp->if_sadl), lla_len) == 0) { 2886 psref_acquire(psref, &ifp->if_psref, 2887 ifnet_psref_class); 2888 break; 2889 } 2890 } 2891 pserialize_read_exit(s); 2892 2893 return ifp; 2894 } 2895 2896 /* 2897 * Note that it's safe only if the passed ifp is guaranteed to not be freed, 2898 * for example using pserialize or the ifp is already held or some other 2899 * object is held which guarantes the ifp to not be freed indirectly. 2900 */ 2901 void 2902 if_acquire(struct ifnet *ifp, struct psref *psref) 2903 { 2904 2905 KASSERT(ifp->if_index != 0); 2906 psref_acquire(psref, &ifp->if_psref, ifnet_psref_class); 2907 } 2908 2909 bool 2910 if_held(struct ifnet *ifp) 2911 { 2912 2913 return psref_held(&ifp->if_psref, ifnet_psref_class); 2914 } 2915 2916 /* 2917 * Some tunnel interfaces can nest, e.g. IPv4 over IPv4 gif(4) tunnel over IPv4. 2918 * Check the tunnel nesting count. 2919 * Return > 0, if tunnel nesting count is more than limit. 2920 * Return 0, if tunnel nesting count is equal or less than limit. 2921 */ 2922 int 2923 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, int limit) 2924 { 2925 struct m_tag *mtag; 2926 int *count; 2927 2928 mtag = m_tag_find(m, PACKET_TAG_TUNNEL_INFO); 2929 if (mtag != NULL) { 2930 count = (int *)(mtag + 1); 2931 if (++(*count) > limit) { 2932 log(LOG_NOTICE, 2933 "%s: recursively called too many times(%d)\n", 2934 ifp->if_xname, *count); 2935 return EIO; 2936 } 2937 } else { 2938 mtag = m_tag_get(PACKET_TAG_TUNNEL_INFO, sizeof(*count), 2939 M_NOWAIT); 2940 if (mtag != NULL) { 2941 m_tag_prepend(m, mtag); 2942 count = (int *)(mtag + 1); 2943 *count = 0; 2944 } else { 2945 log(LOG_DEBUG, 2946 "%s: m_tag_get() failed, recursion calls are not prevented.\n", 2947 ifp->if_xname); 2948 } 2949 } 2950 2951 return 0; 2952 } 2953 2954 static void 2955 if_tunnel_ro_init_pc(void *p, void *arg __unused, struct cpu_info *ci __unused) 2956 { 2957 struct tunnel_ro *tro = p; 2958 2959 tro->tr_ro = kmem_zalloc(sizeof(*tro->tr_ro), KM_SLEEP); 2960 tro->tr_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); 2961 } 2962 2963 static void 2964 if_tunnel_ro_fini_pc(void *p, void *arg __unused, struct cpu_info *ci __unused) 2965 { 2966 struct tunnel_ro *tro = p; 2967 2968 rtcache_free(tro->tr_ro); 2969 kmem_free(tro->tr_ro, sizeof(*tro->tr_ro)); 2970 2971 mutex_obj_free(tro->tr_lock); 2972 } 2973 2974 percpu_t * 2975 if_tunnel_alloc_ro_percpu(void) 2976 { 2977 2978 return percpu_create(sizeof(struct tunnel_ro), 2979 if_tunnel_ro_init_pc, if_tunnel_ro_fini_pc, NULL); 2980 } 2981 2982 void 2983 if_tunnel_free_ro_percpu(percpu_t *ro_percpu) 2984 { 2985 2986 percpu_free(ro_percpu, sizeof(struct tunnel_ro)); 2987 } 2988 2989 2990 static void 2991 if_tunnel_rtcache_free_pc(void *p, void *arg __unused, struct cpu_info *ci __unused) 2992 { 2993 struct tunnel_ro *tro = p; 2994 2995 mutex_enter(tro->tr_lock); 2996 rtcache_free(tro->tr_ro); 2997 mutex_exit(tro->tr_lock); 2998 } 2999 3000 void if_tunnel_ro_percpu_rtcache_free(percpu_t *ro_percpu) 3001 { 3002 3003 percpu_foreach(ro_percpu, if_tunnel_rtcache_free_pc, NULL); 3004 } 3005 3006 void 3007 if_export_if_data(ifnet_t * const ifp, struct if_data *ifi, bool zero_stats) 3008 { 3009 3010 /* Collet the volatile stats first; this zeros *ifi. */ 3011 if_stats_to_if_data(ifp, ifi, zero_stats); 3012 3013 ifi->ifi_type = ifp->if_type; 3014 ifi->ifi_addrlen = ifp->if_addrlen; 3015 ifi->ifi_hdrlen = ifp->if_hdrlen; 3016 ifi->ifi_link_state = ifp->if_link_state; 3017 ifi->ifi_mtu = ifp->if_mtu; 3018 ifi->ifi_metric = ifp->if_metric; 3019 ifi->ifi_baudrate = ifp->if_baudrate; 3020 ifi->ifi_lastchange = ifp->if_lastchange; 3021 } 3022 3023 /* common */ 3024 int 3025 ifioctl_common(struct ifnet *ifp, u_long cmd, void *data) 3026 { 3027 int s; 3028 struct ifreq *ifr; 3029 struct ifcapreq *ifcr; 3030 struct ifdatareq *ifdr; 3031 unsigned short flags; 3032 char *descr; 3033 int error; 3034 3035 switch (cmd) { 3036 case SIOCSIFCAP: 3037 ifcr = data; 3038 if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0) 3039 return EINVAL; 3040 3041 if (ifcr->ifcr_capenable == ifp->if_capenable) 3042 return 0; 3043 3044 ifp->if_capenable = ifcr->ifcr_capenable; 3045 3046 /* Pre-compute the checksum flags mask. */ 3047 ifp->if_csum_flags_tx = 0; 3048 ifp->if_csum_flags_rx = 0; 3049 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) 3050 ifp->if_csum_flags_tx |= M_CSUM_IPv4; 3051 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) 3052 ifp->if_csum_flags_rx |= M_CSUM_IPv4; 3053 3054 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) 3055 ifp->if_csum_flags_tx |= M_CSUM_TCPv4; 3056 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) 3057 ifp->if_csum_flags_rx |= M_CSUM_TCPv4; 3058 3059 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) 3060 ifp->if_csum_flags_tx |= M_CSUM_UDPv4; 3061 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) 3062 ifp->if_csum_flags_rx |= M_CSUM_UDPv4; 3063 3064 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) 3065 ifp->if_csum_flags_tx |= M_CSUM_TCPv6; 3066 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) 3067 ifp->if_csum_flags_rx |= M_CSUM_TCPv6; 3068 3069 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) 3070 ifp->if_csum_flags_tx |= M_CSUM_UDPv6; 3071 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) 3072 ifp->if_csum_flags_rx |= M_CSUM_UDPv6; 3073 3074 if (ifp->if_capenable & IFCAP_TSOv4) 3075 ifp->if_csum_flags_tx |= M_CSUM_TSOv4; 3076 if (ifp->if_capenable & IFCAP_TSOv6) 3077 ifp->if_csum_flags_tx |= M_CSUM_TSOv6; 3078 3079 #if NBRIDGE > 0 3080 if (ifp->if_bridge != NULL) 3081 bridge_calc_csum_flags(ifp->if_bridge); 3082 #endif 3083 3084 if (ifp->if_flags & IFF_UP) 3085 return ENETRESET; 3086 return 0; 3087 case SIOCSIFFLAGS: 3088 ifr = data; 3089 /* 3090 * If if_is_mpsafe(ifp), KERNEL_LOCK isn't held here, but if_up 3091 * and if_down aren't MP-safe yet, so we must hold the lock. 3092 */ 3093 KERNEL_LOCK_IF_IFP_MPSAFE(ifp); 3094 if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) { 3095 s = splsoftnet(); 3096 if_down_locked(ifp); 3097 splx(s); 3098 } 3099 if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) { 3100 s = splsoftnet(); 3101 if_up_locked(ifp); 3102 splx(s); 3103 } 3104 KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp); 3105 flags = (ifp->if_flags & IFF_CANTCHANGE) | 3106 (ifr->ifr_flags &~ IFF_CANTCHANGE); 3107 if (ifp->if_flags != flags) { 3108 ifp->if_flags = flags; 3109 /* Notify that the flags have changed. */ 3110 rt_ifmsg(ifp); 3111 } 3112 break; 3113 case SIOCGIFFLAGS: 3114 ifr = data; 3115 ifr->ifr_flags = ifp->if_flags; 3116 break; 3117 3118 case SIOCGIFMETRIC: 3119 ifr = data; 3120 ifr->ifr_metric = ifp->if_metric; 3121 break; 3122 3123 case SIOCGIFMTU: 3124 ifr = data; 3125 ifr->ifr_mtu = ifp->if_mtu; 3126 break; 3127 3128 case SIOCGIFDLT: 3129 ifr = data; 3130 ifr->ifr_dlt = ifp->if_dlt; 3131 break; 3132 3133 case SIOCGIFCAP: 3134 ifcr = data; 3135 ifcr->ifcr_capabilities = ifp->if_capabilities; 3136 ifcr->ifcr_capenable = ifp->if_capenable; 3137 break; 3138 3139 case SIOCSIFMETRIC: 3140 ifr = data; 3141 ifp->if_metric = ifr->ifr_metric; 3142 break; 3143 3144 case SIOCGIFDATA: 3145 ifdr = data; 3146 if_export_if_data(ifp, &ifdr->ifdr_data, false); 3147 break; 3148 3149 case SIOCGIFINDEX: 3150 ifr = data; 3151 ifr->ifr_index = ifp->if_index; 3152 break; 3153 3154 case SIOCZIFDATA: 3155 ifdr = data; 3156 if_export_if_data(ifp, &ifdr->ifdr_data, true); 3157 getnanotime(&ifp->if_lastchange); 3158 break; 3159 case SIOCSIFMTU: 3160 ifr = data; 3161 if (ifp->if_mtu == ifr->ifr_mtu) 3162 break; 3163 ifp->if_mtu = ifr->ifr_mtu; 3164 return ENETRESET; 3165 case SIOCSIFDESCR: 3166 error = kauth_authorize_network(curlwp->l_cred, 3167 KAUTH_NETWORK_INTERFACE, 3168 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, KAUTH_ARG(cmd), 3169 NULL); 3170 if (error) 3171 return error; 3172 3173 ifr = data; 3174 3175 if (ifr->ifr_buflen > IFDESCRSIZE) 3176 return ENAMETOOLONG; 3177 3178 if (ifr->ifr_buf == NULL || ifr->ifr_buflen == 0) { 3179 /* unset description */ 3180 descr = NULL; 3181 } else { 3182 descr = kmem_zalloc(IFDESCRSIZE, KM_SLEEP); 3183 /* 3184 * copy (IFDESCRSIZE - 1) bytes to ensure 3185 * terminating nul 3186 */ 3187 error = copyin(ifr->ifr_buf, descr, IFDESCRSIZE - 1); 3188 if (error) { 3189 kmem_free(descr, IFDESCRSIZE); 3190 return error; 3191 } 3192 } 3193 3194 if (ifp->if_description != NULL) 3195 kmem_free(ifp->if_description, IFDESCRSIZE); 3196 3197 ifp->if_description = descr; 3198 break; 3199 3200 case SIOCGIFDESCR: 3201 ifr = data; 3202 descr = ifp->if_description; 3203 3204 if (descr == NULL) 3205 return ENOMSG; 3206 3207 if (ifr->ifr_buflen < IFDESCRSIZE) 3208 return EINVAL; 3209 3210 error = copyout(descr, ifr->ifr_buf, IFDESCRSIZE); 3211 if (error) 3212 return error; 3213 break; 3214 3215 default: 3216 return ENOTTY; 3217 } 3218 return 0; 3219 } 3220 3221 int 3222 ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data, struct ifnet *ifp) 3223 { 3224 struct if_addrprefreq *ifap = (struct if_addrprefreq *)data; 3225 struct ifaddr *ifa; 3226 const struct sockaddr *any, *sa; 3227 union { 3228 struct sockaddr sa; 3229 struct sockaddr_storage ss; 3230 } u, v; 3231 int s, error = 0; 3232 3233 switch (cmd) { 3234 case SIOCSIFADDRPREF: 3235 error = kauth_authorize_network(curlwp->l_cred, 3236 KAUTH_NETWORK_INTERFACE, 3237 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, KAUTH_ARG(cmd), 3238 NULL); 3239 if (error) 3240 return error; 3241 break; 3242 case SIOCGIFADDRPREF: 3243 break; 3244 default: 3245 return EOPNOTSUPP; 3246 } 3247 3248 /* sanity checks */ 3249 if (data == NULL || ifp == NULL) { 3250 panic("invalid argument to %s", __func__); 3251 /*NOTREACHED*/ 3252 } 3253 3254 /* address must be specified on ADD and DELETE */ 3255 sa = sstocsa(&ifap->ifap_addr); 3256 if (sa->sa_family != sofamily(so)) 3257 return EINVAL; 3258 if ((any = sockaddr_any(sa)) == NULL || sa->sa_len != any->sa_len) 3259 return EINVAL; 3260 3261 sockaddr_externalize(&v.sa, sizeof(v.ss), sa); 3262 3263 s = pserialize_read_enter(); 3264 IFADDR_READER_FOREACH(ifa, ifp) { 3265 if (ifa->ifa_addr->sa_family != sa->sa_family) 3266 continue; 3267 sockaddr_externalize(&u.sa, sizeof(u.ss), ifa->ifa_addr); 3268 if (sockaddr_cmp(&u.sa, &v.sa) == 0) 3269 break; 3270 } 3271 if (ifa == NULL) { 3272 error = EADDRNOTAVAIL; 3273 goto out; 3274 } 3275 3276 switch (cmd) { 3277 case SIOCSIFADDRPREF: 3278 ifa->ifa_preference = ifap->ifap_preference; 3279 goto out; 3280 case SIOCGIFADDRPREF: 3281 /* fill in the if_laddrreq structure */ 3282 (void)sockaddr_copy(sstosa(&ifap->ifap_addr), 3283 sizeof(ifap->ifap_addr), ifa->ifa_addr); 3284 ifap->ifap_preference = ifa->ifa_preference; 3285 goto out; 3286 default: 3287 error = EOPNOTSUPP; 3288 } 3289 out: 3290 pserialize_read_exit(s); 3291 return error; 3292 } 3293 3294 /* 3295 * Interface ioctls. 3296 */ 3297 static int 3298 doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l) 3299 { 3300 struct ifnet *ifp; 3301 struct ifreq *ifr; 3302 int error = 0; 3303 u_long ocmd = cmd; 3304 u_short oif_flags; 3305 struct ifreq ifrb; 3306 struct oifreq *oifr = NULL; 3307 int r; 3308 struct psref psref; 3309 int bound; 3310 bool do_if43_post = false; 3311 bool do_ifm80_post = false; 3312 3313 switch (cmd) { 3314 case SIOCGIFCONF: 3315 return ifconf(cmd, data); 3316 case SIOCINITIFADDR: 3317 return EPERM; 3318 default: 3319 MODULE_HOOK_CALL(uipc_syscalls_40_hook, (cmd, data), enosys(), 3320 error); 3321 if (error != ENOSYS) 3322 return error; 3323 MODULE_HOOK_CALL(uipc_syscalls_50_hook, (l, cmd, data), 3324 enosys(), error); 3325 if (error != ENOSYS) 3326 return error; 3327 error = 0; 3328 break; 3329 } 3330 3331 ifr = data; 3332 /* Pre-conversion */ 3333 MODULE_HOOK_CALL(if_cvtcmd_43_hook, (&cmd, ocmd), enosys(), error); 3334 if (cmd != ocmd) { 3335 oifr = data; 3336 data = ifr = &ifrb; 3337 IFREQO2N_43(oifr, ifr); 3338 do_if43_post = true; 3339 } 3340 MODULE_HOOK_CALL(ifmedia_80_pre_hook, (ifr, &cmd, &do_ifm80_post), 3341 enosys(), error); 3342 3343 switch (cmd) { 3344 case SIOCIFCREATE: 3345 case SIOCIFDESTROY: 3346 bound = curlwp_bind(); 3347 if (l != NULL) { 3348 ifp = if_get(ifr->ifr_name, &psref); 3349 error = kauth_authorize_network(l->l_cred, 3350 KAUTH_NETWORK_INTERFACE, 3351 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, 3352 KAUTH_ARG(cmd), NULL); 3353 if (ifp != NULL) 3354 if_put(ifp, &psref); 3355 if (error != 0) { 3356 curlwp_bindx(bound); 3357 return error; 3358 } 3359 } 3360 KERNEL_LOCK_UNLESS_NET_MPSAFE(); 3361 mutex_enter(&if_clone_mtx); 3362 r = (cmd == SIOCIFCREATE) ? 3363 if_clone_create(ifr->ifr_name) : 3364 if_clone_destroy(ifr->ifr_name); 3365 mutex_exit(&if_clone_mtx); 3366 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 3367 curlwp_bindx(bound); 3368 return r; 3369 3370 case SIOCIFGCLONERS: 3371 { 3372 struct if_clonereq *req = (struct if_clonereq *)data; 3373 return if_clone_list(req->ifcr_count, req->ifcr_buffer, 3374 &req->ifcr_total); 3375 } 3376 } 3377 3378 bound = curlwp_bind(); 3379 ifp = if_get(ifr->ifr_name, &psref); 3380 if (ifp == NULL) { 3381 curlwp_bindx(bound); 3382 return ENXIO; 3383 } 3384 3385 switch (cmd) { 3386 case SIOCALIFADDR: 3387 case SIOCDLIFADDR: 3388 case SIOCSIFADDRPREF: 3389 case SIOCSIFFLAGS: 3390 case SIOCSIFCAP: 3391 case SIOCSIFMETRIC: 3392 case SIOCZIFDATA: 3393 case SIOCSIFMTU: 3394 case SIOCSIFPHYADDR: 3395 case SIOCDIFPHYADDR: 3396 #ifdef INET6 3397 case SIOCSIFPHYADDR_IN6: 3398 #endif 3399 case SIOCSLIFPHYADDR: 3400 case SIOCADDMULTI: 3401 case SIOCDELMULTI: 3402 case SIOCSETHERCAP: 3403 case SIOCSIFMEDIA: 3404 case SIOCSDRVSPEC: 3405 case SIOCG80211: 3406 case SIOCS80211: 3407 case SIOCS80211NWID: 3408 case SIOCS80211NWKEY: 3409 case SIOCS80211POWER: 3410 case SIOCS80211BSSID: 3411 case SIOCS80211CHANNEL: 3412 case SIOCSLINKSTR: 3413 if (l != NULL) { 3414 error = kauth_authorize_network(l->l_cred, 3415 KAUTH_NETWORK_INTERFACE, 3416 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, 3417 KAUTH_ARG(cmd), NULL); 3418 if (error != 0) 3419 goto out; 3420 } 3421 } 3422 3423 oif_flags = ifp->if_flags; 3424 3425 KERNEL_LOCK_UNLESS_IFP_MPSAFE(ifp); 3426 IFNET_LOCK(ifp); 3427 3428 error = (*ifp->if_ioctl)(ifp, cmd, data); 3429 if (error != ENOTTY) 3430 ; 3431 else if (so->so_proto == NULL) 3432 error = EOPNOTSUPP; 3433 else { 3434 KERNEL_LOCK_IF_IFP_MPSAFE(ifp); 3435 MODULE_HOOK_CALL(if_ifioctl_43_hook, 3436 (so, ocmd, cmd, data, l), enosys(), error); 3437 if (error == ENOSYS) 3438 error = (*so->so_proto->pr_usrreqs->pr_ioctl)(so, 3439 cmd, data, ifp); 3440 KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp); 3441 } 3442 3443 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) { 3444 if ((ifp->if_flags & IFF_UP) != 0) { 3445 int s = splsoftnet(); 3446 if_up_locked(ifp); 3447 splx(s); 3448 } 3449 } 3450 3451 /* Post-conversion */ 3452 if (do_ifm80_post && (error == 0)) 3453 MODULE_HOOK_CALL(ifmedia_80_post_hook, (ifr, cmd), 3454 enosys(), error); 3455 if (do_if43_post) 3456 IFREQN2O_43(oifr, ifr); 3457 3458 IFNET_UNLOCK(ifp); 3459 KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(ifp); 3460 out: 3461 if_put(ifp, &psref); 3462 curlwp_bindx(bound); 3463 return error; 3464 } 3465 3466 /* 3467 * Return interface configuration 3468 * of system. List may be used 3469 * in later ioctl's (above) to get 3470 * other information. 3471 * 3472 * Each record is a struct ifreq. Before the addition of 3473 * sockaddr_storage, the API rule was that sockaddr flavors that did 3474 * not fit would extend beyond the struct ifreq, with the next struct 3475 * ifreq starting sa_len beyond the struct sockaddr. Because the 3476 * union in struct ifreq includes struct sockaddr_storage, every kind 3477 * of sockaddr must fit. Thus, there are no longer any overlength 3478 * records. 3479 * 3480 * Records are added to the user buffer if they fit, and ifc_len is 3481 * adjusted to the length that was written. Thus, the user is only 3482 * assured of getting the complete list if ifc_len on return is at 3483 * least sizeof(struct ifreq) less than it was on entry. 3484 * 3485 * If the user buffer pointer is NULL, this routine copies no data and 3486 * returns the amount of space that would be needed. 3487 * 3488 * Invariants: 3489 * ifrp points to the next part of the user's buffer to be used. If 3490 * ifrp != NULL, space holds the number of bytes remaining that we may 3491 * write at ifrp. Otherwise, space holds the number of bytes that 3492 * would have been written had there been adequate space. 3493 */ 3494 /*ARGSUSED*/ 3495 static int 3496 ifconf(u_long cmd, void *data) 3497 { 3498 struct ifconf *ifc = (struct ifconf *)data; 3499 struct ifnet *ifp; 3500 struct ifaddr *ifa; 3501 struct ifreq ifr, *ifrp = NULL; 3502 int space = 0, error = 0; 3503 const int sz = (int)sizeof(struct ifreq); 3504 const bool docopy = ifc->ifc_req != NULL; 3505 int s; 3506 int bound; 3507 struct psref psref; 3508 3509 if (docopy) { 3510 if (ifc->ifc_len < 0) 3511 return EINVAL; 3512 3513 space = ifc->ifc_len; 3514 ifrp = ifc->ifc_req; 3515 } 3516 memset(&ifr, 0, sizeof(ifr)); 3517 3518 bound = curlwp_bind(); 3519 s = pserialize_read_enter(); 3520 IFNET_READER_FOREACH(ifp) { 3521 psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class); 3522 pserialize_read_exit(s); 3523 3524 (void)strncpy(ifr.ifr_name, ifp->if_xname, 3525 sizeof(ifr.ifr_name)); 3526 if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0') { 3527 error = ENAMETOOLONG; 3528 goto release_exit; 3529 } 3530 if (IFADDR_READER_EMPTY(ifp)) { 3531 /* Interface with no addresses - send zero sockaddr. */ 3532 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr)); 3533 if (!docopy) { 3534 space += sz; 3535 goto next; 3536 } 3537 if (space >= sz) { 3538 error = copyout(&ifr, ifrp, sz); 3539 if (error != 0) 3540 goto release_exit; 3541 ifrp++; 3542 space -= sz; 3543 } 3544 } 3545 3546 s = pserialize_read_enter(); 3547 IFADDR_READER_FOREACH(ifa, ifp) { 3548 struct sockaddr *sa = ifa->ifa_addr; 3549 /* all sockaddrs must fit in sockaddr_storage */ 3550 KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru)); 3551 3552 if (!docopy) { 3553 space += sz; 3554 continue; 3555 } 3556 memcpy(&ifr.ifr_space, sa, sa->sa_len); 3557 pserialize_read_exit(s); 3558 3559 if (space >= sz) { 3560 error = copyout(&ifr, ifrp, sz); 3561 if (error != 0) 3562 goto release_exit; 3563 ifrp++; space -= sz; 3564 } 3565 s = pserialize_read_enter(); 3566 } 3567 pserialize_read_exit(s); 3568 3569 next: 3570 s = pserialize_read_enter(); 3571 psref_release(&psref, &ifp->if_psref, ifnet_psref_class); 3572 } 3573 pserialize_read_exit(s); 3574 curlwp_bindx(bound); 3575 3576 if (docopy) { 3577 KASSERT(0 <= space && space <= ifc->ifc_len); 3578 ifc->ifc_len -= space; 3579 } else { 3580 KASSERT(space >= 0); 3581 ifc->ifc_len = space; 3582 } 3583 return (0); 3584 3585 release_exit: 3586 psref_release(&psref, &ifp->if_psref, ifnet_psref_class); 3587 curlwp_bindx(bound); 3588 return error; 3589 } 3590 3591 int 3592 ifreq_setaddr(u_long cmd, struct ifreq *ifr, const struct sockaddr *sa) 3593 { 3594 uint8_t len = sizeof(ifr->ifr_ifru.ifru_space); 3595 struct ifreq ifrb; 3596 struct oifreq *oifr = NULL; 3597 u_long ocmd = cmd; 3598 int hook; 3599 3600 MODULE_HOOK_CALL(if_cvtcmd_43_hook, (&cmd, ocmd), enosys(), hook); 3601 if (hook != ENOSYS) { 3602 if (cmd != ocmd) { 3603 oifr = (struct oifreq *)(void *)ifr; 3604 ifr = &ifrb; 3605 IFREQO2N_43(oifr, ifr); 3606 len = sizeof(oifr->ifr_addr); 3607 } 3608 } 3609 3610 if (len < sa->sa_len) 3611 return EFBIG; 3612 3613 memset(&ifr->ifr_addr, 0, len); 3614 sockaddr_copy(&ifr->ifr_addr, len, sa); 3615 3616 if (cmd != ocmd) 3617 IFREQN2O_43(oifr, ifr); 3618 return 0; 3619 } 3620 3621 /* 3622 * wrapper function for the drivers which doesn't have if_transmit(). 3623 */ 3624 static int 3625 if_transmit(struct ifnet *ifp, struct mbuf *m) 3626 { 3627 int s, error; 3628 size_t pktlen = m->m_pkthdr.len; 3629 bool mcast = (m->m_flags & M_MCAST) != 0; 3630 3631 s = splnet(); 3632 3633 IFQ_ENQUEUE(&ifp->if_snd, m, error); 3634 if (error != 0) { 3635 /* mbuf is already freed */ 3636 goto out; 3637 } 3638 3639 net_stat_ref_t nsr = IF_STAT_GETREF(ifp); 3640 if_statadd_ref(nsr, if_obytes, pktlen); 3641 if (mcast) 3642 if_statinc_ref(nsr, if_omcasts); 3643 IF_STAT_PUTREF(ifp); 3644 3645 if ((ifp->if_flags & IFF_OACTIVE) == 0) 3646 if_start_lock(ifp); 3647 out: 3648 splx(s); 3649 3650 return error; 3651 } 3652 3653 int 3654 if_transmit_lock(struct ifnet *ifp, struct mbuf *m) 3655 { 3656 int error; 3657 3658 kmsan_check_mbuf(m); 3659 3660 #ifdef ALTQ 3661 KERNEL_LOCK(1, NULL); 3662 if (ALTQ_IS_ENABLED(&ifp->if_snd)) { 3663 error = if_transmit(ifp, m); 3664 KERNEL_UNLOCK_ONE(NULL); 3665 } else { 3666 KERNEL_UNLOCK_ONE(NULL); 3667 error = (*ifp->if_transmit)(ifp, m); 3668 /* mbuf is alredy freed */ 3669 } 3670 #else /* !ALTQ */ 3671 error = (*ifp->if_transmit)(ifp, m); 3672 /* mbuf is alredy freed */ 3673 #endif /* !ALTQ */ 3674 3675 return error; 3676 } 3677 3678 /* 3679 * Queue message on interface, and start output if interface 3680 * not yet active. 3681 */ 3682 int 3683 ifq_enqueue(struct ifnet *ifp, struct mbuf *m) 3684 { 3685 3686 return if_transmit_lock(ifp, m); 3687 } 3688 3689 /* 3690 * Queue message on interface, possibly using a second fast queue 3691 */ 3692 int 3693 ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m) 3694 { 3695 int error = 0; 3696 3697 if (ifq != NULL 3698 #ifdef ALTQ 3699 && ALTQ_IS_ENABLED(&ifp->if_snd) == 0 3700 #endif 3701 ) { 3702 if (IF_QFULL(ifq)) { 3703 IF_DROP(&ifp->if_snd); 3704 m_freem(m); 3705 if (error == 0) 3706 error = ENOBUFS; 3707 } else 3708 IF_ENQUEUE(ifq, m); 3709 } else 3710 IFQ_ENQUEUE(&ifp->if_snd, m, error); 3711 if (error != 0) { 3712 if_statinc(ifp, if_oerrors); 3713 return error; 3714 } 3715 return 0; 3716 } 3717 3718 int 3719 if_addr_init(ifnet_t *ifp, struct ifaddr *ifa, const bool src) 3720 { 3721 int rc; 3722 3723 KASSERT(IFNET_LOCKED(ifp)); 3724 if (ifp->if_initaddr != NULL) 3725 rc = (*ifp->if_initaddr)(ifp, ifa, src); 3726 else if (src || 3727 (rc = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, ifa)) == ENOTTY) 3728 rc = (*ifp->if_ioctl)(ifp, SIOCINITIFADDR, ifa); 3729 3730 return rc; 3731 } 3732 3733 int 3734 if_do_dad(struct ifnet *ifp) 3735 { 3736 if ((ifp->if_flags & IFF_LOOPBACK) != 0) 3737 return 0; 3738 3739 switch (ifp->if_type) { 3740 case IFT_FAITH: 3741 /* 3742 * These interfaces do not have the IFF_LOOPBACK flag, 3743 * but loop packets back. We do not have to do DAD on such 3744 * interfaces. We should even omit it, because loop-backed 3745 * responses would confuse the DAD procedure. 3746 */ 3747 return 0; 3748 default: 3749 /* 3750 * Our DAD routine requires the interface up and running. 3751 * However, some interfaces can be up before the RUNNING 3752 * status. Additionaly, users may try to assign addresses 3753 * before the interface becomes up (or running). 3754 * We simply skip DAD in such a case as a work around. 3755 * XXX: we should rather mark "tentative" on such addresses, 3756 * and do DAD after the interface becomes ready. 3757 */ 3758 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != 3759 (IFF_UP | IFF_RUNNING)) 3760 return 0; 3761 3762 return 1; 3763 } 3764 } 3765 3766 int 3767 if_flags_set(ifnet_t *ifp, const u_short flags) 3768 { 3769 int rc; 3770 3771 KASSERT(IFNET_LOCKED(ifp)); 3772 3773 if (ifp->if_setflags != NULL) 3774 rc = (*ifp->if_setflags)(ifp, flags); 3775 else { 3776 u_short cantflags, chgdflags; 3777 struct ifreq ifr; 3778 3779 chgdflags = ifp->if_flags ^ flags; 3780 cantflags = chgdflags & IFF_CANTCHANGE; 3781 3782 if (cantflags != 0) 3783 ifp->if_flags ^= cantflags; 3784 3785 /* Traditionally, we do not call if_ioctl after 3786 * setting/clearing only IFF_PROMISC if the interface 3787 * isn't IFF_UP. Uphold that tradition. 3788 */ 3789 if (chgdflags == IFF_PROMISC && (ifp->if_flags & IFF_UP) == 0) 3790 return 0; 3791 3792 memset(&ifr, 0, sizeof(ifr)); 3793 3794 ifr.ifr_flags = flags & ~IFF_CANTCHANGE; 3795 rc = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, &ifr); 3796 3797 if (rc != 0 && cantflags != 0) 3798 ifp->if_flags ^= cantflags; 3799 } 3800 3801 return rc; 3802 } 3803 3804 int 3805 if_mcast_op(ifnet_t *ifp, const unsigned long cmd, const struct sockaddr *sa) 3806 { 3807 int rc; 3808 struct ifreq ifr; 3809 3810 /* 3811 * XXX NOMPSAFE - this calls if_ioctl without holding IFNET_LOCK() 3812 * in some cases - e.g. when called from vlan/netinet/netinet6 code 3813 * directly rather than via doifoictl() 3814 */ 3815 ifreq_setaddr(cmd, &ifr, sa); 3816 rc = (*ifp->if_ioctl)(ifp, cmd, &ifr); 3817 3818 return rc; 3819 } 3820 3821 static void 3822 sysctl_sndq_setup(struct sysctllog **clog, const char *ifname, 3823 struct ifaltq *ifq) 3824 { 3825 const struct sysctlnode *cnode, *rnode; 3826 3827 if (sysctl_createv(clog, 0, NULL, &rnode, 3828 CTLFLAG_PERMANENT, 3829 CTLTYPE_NODE, "interfaces", 3830 SYSCTL_DESCR("Per-interface controls"), 3831 NULL, 0, NULL, 0, 3832 CTL_NET, CTL_CREATE, CTL_EOL) != 0) 3833 goto bad; 3834 3835 if (sysctl_createv(clog, 0, &rnode, &rnode, 3836 CTLFLAG_PERMANENT, 3837 CTLTYPE_NODE, ifname, 3838 SYSCTL_DESCR("Interface controls"), 3839 NULL, 0, NULL, 0, 3840 CTL_CREATE, CTL_EOL) != 0) 3841 goto bad; 3842 3843 if (sysctl_createv(clog, 0, &rnode, &rnode, 3844 CTLFLAG_PERMANENT, 3845 CTLTYPE_NODE, "sndq", 3846 SYSCTL_DESCR("Interface output queue controls"), 3847 NULL, 0, NULL, 0, 3848 CTL_CREATE, CTL_EOL) != 0) 3849 goto bad; 3850 3851 if (sysctl_createv(clog, 0, &rnode, &cnode, 3852 CTLFLAG_PERMANENT, 3853 CTLTYPE_INT, "len", 3854 SYSCTL_DESCR("Current output queue length"), 3855 NULL, 0, &ifq->ifq_len, 0, 3856 CTL_CREATE, CTL_EOL) != 0) 3857 goto bad; 3858 3859 if (sysctl_createv(clog, 0, &rnode, &cnode, 3860 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 3861 CTLTYPE_INT, "maxlen", 3862 SYSCTL_DESCR("Maximum allowed output queue length"), 3863 NULL, 0, &ifq->ifq_maxlen, 0, 3864 CTL_CREATE, CTL_EOL) != 0) 3865 goto bad; 3866 3867 if (sysctl_createv(clog, 0, &rnode, &cnode, 3868 CTLFLAG_PERMANENT, 3869 CTLTYPE_INT, "drops", 3870 SYSCTL_DESCR("Packets dropped due to full output queue"), 3871 NULL, 0, &ifq->ifq_drops, 0, 3872 CTL_CREATE, CTL_EOL) != 0) 3873 goto bad; 3874 3875 return; 3876 bad: 3877 printf("%s: could not attach sysctl nodes\n", ifname); 3878 return; 3879 } 3880 3881 #if defined(INET) || defined(INET6) 3882 3883 #define SYSCTL_NET_PKTQ(q, cn, c) \ 3884 static int \ 3885 sysctl_net_##q##_##cn(SYSCTLFN_ARGS) \ 3886 { \ 3887 return sysctl_pktq_count(SYSCTLFN_CALL(rnode), q, c); \ 3888 } 3889 3890 #if defined(INET) 3891 static int 3892 sysctl_net_ip_pktq_maxlen(SYSCTLFN_ARGS) 3893 { 3894 return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip_pktq); 3895 } 3896 SYSCTL_NET_PKTQ(ip_pktq, items, PKTQ_NITEMS) 3897 SYSCTL_NET_PKTQ(ip_pktq, drops, PKTQ_DROPS) 3898 #endif 3899 3900 #if defined(INET6) 3901 static int 3902 sysctl_net_ip6_pktq_maxlen(SYSCTLFN_ARGS) 3903 { 3904 return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip6_pktq); 3905 } 3906 SYSCTL_NET_PKTQ(ip6_pktq, items, PKTQ_NITEMS) 3907 SYSCTL_NET_PKTQ(ip6_pktq, drops, PKTQ_DROPS) 3908 #endif 3909 3910 static void 3911 sysctl_net_pktq_setup(struct sysctllog **clog, int pf) 3912 { 3913 sysctlfn len_func = NULL, maxlen_func = NULL, drops_func = NULL; 3914 const char *pfname = NULL, *ipname = NULL; 3915 int ipn = 0, qid = 0; 3916 3917 switch (pf) { 3918 #if defined(INET) 3919 case PF_INET: 3920 len_func = sysctl_net_ip_pktq_items; 3921 maxlen_func = sysctl_net_ip_pktq_maxlen; 3922 drops_func = sysctl_net_ip_pktq_drops; 3923 pfname = "inet", ipn = IPPROTO_IP; 3924 ipname = "ip", qid = IPCTL_IFQ; 3925 break; 3926 #endif 3927 #if defined(INET6) 3928 case PF_INET6: 3929 len_func = sysctl_net_ip6_pktq_items; 3930 maxlen_func = sysctl_net_ip6_pktq_maxlen; 3931 drops_func = sysctl_net_ip6_pktq_drops; 3932 pfname = "inet6", ipn = IPPROTO_IPV6; 3933 ipname = "ip6", qid = IPV6CTL_IFQ; 3934 break; 3935 #endif 3936 default: 3937 KASSERT(false); 3938 } 3939 3940 sysctl_createv(clog, 0, NULL, NULL, 3941 CTLFLAG_PERMANENT, 3942 CTLTYPE_NODE, pfname, NULL, 3943 NULL, 0, NULL, 0, 3944 CTL_NET, pf, CTL_EOL); 3945 sysctl_createv(clog, 0, NULL, NULL, 3946 CTLFLAG_PERMANENT, 3947 CTLTYPE_NODE, ipname, NULL, 3948 NULL, 0, NULL, 0, 3949 CTL_NET, pf, ipn, CTL_EOL); 3950 sysctl_createv(clog, 0, NULL, NULL, 3951 CTLFLAG_PERMANENT, 3952 CTLTYPE_NODE, "ifq", 3953 SYSCTL_DESCR("Protocol input queue controls"), 3954 NULL, 0, NULL, 0, 3955 CTL_NET, pf, ipn, qid, CTL_EOL); 3956 3957 sysctl_createv(clog, 0, NULL, NULL, 3958 CTLFLAG_PERMANENT, 3959 CTLTYPE_QUAD, "len", 3960 SYSCTL_DESCR("Current input queue length"), 3961 len_func, 0, NULL, 0, 3962 CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL); 3963 sysctl_createv(clog, 0, NULL, NULL, 3964 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 3965 CTLTYPE_INT, "maxlen", 3966 SYSCTL_DESCR("Maximum allowed input queue length"), 3967 maxlen_func, 0, NULL, 0, 3968 CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL); 3969 sysctl_createv(clog, 0, NULL, NULL, 3970 CTLFLAG_PERMANENT, 3971 CTLTYPE_QUAD, "drops", 3972 SYSCTL_DESCR("Packets dropped due to full input queue"), 3973 drops_func, 0, NULL, 0, 3974 CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL); 3975 } 3976 #endif /* INET || INET6 */ 3977 3978 static int 3979 if_sdl_sysctl(SYSCTLFN_ARGS) 3980 { 3981 struct ifnet *ifp; 3982 const struct sockaddr_dl *sdl; 3983 struct psref psref; 3984 int error = 0; 3985 int bound; 3986 3987 if (namelen != 1) 3988 return EINVAL; 3989 3990 bound = curlwp_bind(); 3991 ifp = if_get_byindex(name[0], &psref); 3992 if (ifp == NULL) { 3993 error = ENODEV; 3994 goto out0; 3995 } 3996 3997 sdl = ifp->if_sadl; 3998 if (sdl == NULL) { 3999 *oldlenp = 0; 4000 goto out1; 4001 } 4002 4003 if (oldp == NULL) { 4004 *oldlenp = sdl->sdl_alen; 4005 goto out1; 4006 } 4007 4008 if (*oldlenp >= sdl->sdl_alen) 4009 *oldlenp = sdl->sdl_alen; 4010 error = sysctl_copyout(l, &sdl->sdl_data[sdl->sdl_nlen], oldp, *oldlenp); 4011 out1: 4012 if_put(ifp, &psref); 4013 out0: 4014 curlwp_bindx(bound); 4015 return error; 4016 } 4017 4018 static void 4019 if_sysctl_setup(struct sysctllog **clog) 4020 { 4021 const struct sysctlnode *rnode = NULL; 4022 4023 sysctl_createv(clog, 0, NULL, &rnode, 4024 CTLFLAG_PERMANENT, 4025 CTLTYPE_NODE, "sdl", 4026 SYSCTL_DESCR("Get active link-layer address"), 4027 if_sdl_sysctl, 0, NULL, 0, 4028 CTL_NET, CTL_CREATE, CTL_EOL); 4029 4030 #if defined(INET) 4031 sysctl_net_pktq_setup(NULL, PF_INET); 4032 #endif 4033 #ifdef INET6 4034 if (in6_present) 4035 sysctl_net_pktq_setup(NULL, PF_INET6); 4036 #endif 4037 } 4038