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