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