1 /* $NetBSD: if.c,v 1.455 2019/05/21 09:18:37 msaitoh 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.455 2019/05/21 09:18:37 msaitoh 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 ifp->if_transmit == NULL || ifp->if_transmit == if_nulltransmit || 1047 ifp->if_transmit == if_transmit || 1048 ALTQ_IS_ENABLED(&ifp->if_snd); 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 uint64_t xc; 1311 1312 #ifdef IFAREF_DEBUG 1313 if_build_ifa_list(ifp); 1314 #endif 1315 /* 1316 * XXX It's kind of lame that we have to have the 1317 * XXX socket structure... 1318 */ 1319 memset(&so, 0, sizeof(so)); 1320 1321 s = splnet(); 1322 1323 sysctl_teardown(&ifp->if_sysctl_log); 1324 IFNET_LOCK(ifp); 1325 if_deactivate(ifp); 1326 IFNET_UNLOCK(ifp); 1327 1328 /* 1329 * Unlink from the list and wait for all readers to leave 1330 * from pserialize read sections. Note that we can't do 1331 * psref_target_destroy here. See below. 1332 */ 1333 IFNET_GLOBAL_LOCK(); 1334 ifindex2ifnet[ifp->if_index] = NULL; 1335 TAILQ_REMOVE(&ifnet_list, ifp, if_list); 1336 IFNET_WRITER_REMOVE(ifp); 1337 pserialize_perform(ifnet_psz); 1338 IFNET_GLOBAL_UNLOCK(); 1339 1340 if (ifp->if_slowtimo != NULL && ifp->if_slowtimo_ch != NULL) { 1341 ifp->if_slowtimo = NULL; 1342 callout_halt(ifp->if_slowtimo_ch, NULL); 1343 callout_destroy(ifp->if_slowtimo_ch); 1344 kmem_free(ifp->if_slowtimo_ch, sizeof(*ifp->if_slowtimo_ch)); 1345 } 1346 if_deferred_start_destroy(ifp); 1347 1348 /* 1349 * Do an if_down() to give protocols a chance to do something. 1350 */ 1351 if_down_deactivated(ifp); 1352 1353 #ifdef ALTQ 1354 if (ALTQ_IS_ENABLED(&ifp->if_snd)) 1355 altq_disable(&ifp->if_snd); 1356 if (ALTQ_IS_ATTACHED(&ifp->if_snd)) 1357 altq_detach(&ifp->if_snd); 1358 #endif 1359 1360 #if NCARP > 0 1361 /* Remove the interface from any carp group it is a part of. */ 1362 if (ifp->if_carp != NULL && ifp->if_type != IFT_CARP) 1363 carp_ifdetach(ifp); 1364 #endif 1365 1366 /* 1367 * Rip all the addresses off the interface. This should make 1368 * all of the routes go away. 1369 * 1370 * pr_usrreq calls can remove an arbitrary number of ifaddrs 1371 * from the list, including our "cursor", ifa. For safety, 1372 * and to honor the TAILQ abstraction, I just restart the 1373 * loop after each removal. Note that the loop will exit 1374 * when all of the remaining ifaddrs belong to the AF_LINK 1375 * family. I am counting on the historical fact that at 1376 * least one pr_usrreq in each address domain removes at 1377 * least one ifaddr. 1378 */ 1379 again: 1380 /* 1381 * At this point, no other one tries to remove ifa in the list, 1382 * so we don't need to take a lock or psref. Avoid using 1383 * IFADDR_READER_FOREACH to pass over an inspection of contract 1384 * violations of pserialize. 1385 */ 1386 IFADDR_WRITER_FOREACH(ifa, ifp) { 1387 family = ifa->ifa_addr->sa_family; 1388 #ifdef IFAREF_DEBUG 1389 printf("if_detach: ifaddr %p, family %d, refcnt %d\n", 1390 ifa, family, ifa->ifa_refcnt); 1391 if (last_ifa != NULL && ifa == last_ifa) 1392 panic("if_detach: loop detected"); 1393 last_ifa = ifa; 1394 #endif 1395 if (family == AF_LINK) 1396 continue; 1397 dp = pffinddomain(family); 1398 KASSERTMSG(dp != NULL, "no domain for AF %d", family); 1399 /* 1400 * XXX These PURGEIF calls are redundant with the 1401 * purge-all-families calls below, but are left in for 1402 * now both to make a smaller change, and to avoid 1403 * unplanned interactions with clearing of 1404 * ifp->if_addrlist. 1405 */ 1406 purged = 0; 1407 for (pr = dp->dom_protosw; 1408 pr < dp->dom_protoswNPROTOSW; pr++) { 1409 so.so_proto = pr; 1410 if (pr->pr_usrreqs) { 1411 (void) (*pr->pr_usrreqs->pr_purgeif)(&so, ifp); 1412 purged = 1; 1413 } 1414 } 1415 if (purged == 0) { 1416 /* 1417 * XXX What's really the best thing to do 1418 * XXX here? --thorpej@NetBSD.org 1419 */ 1420 printf("if_detach: WARNING: AF %d not purged\n", 1421 family); 1422 ifa_remove(ifp, ifa); 1423 } 1424 goto again; 1425 } 1426 1427 if_free_sadl(ifp, 1); 1428 1429 restart: 1430 IFADDR_WRITER_FOREACH(ifa, ifp) { 1431 family = ifa->ifa_addr->sa_family; 1432 KASSERT(family == AF_LINK); 1433 ifa_remove(ifp, ifa); 1434 goto restart; 1435 } 1436 1437 /* Delete stray routes from the routing table. */ 1438 for (i = 0; i <= AF_MAX; i++) 1439 rt_delete_matched_entries(i, if_delroute_matcher, ifp); 1440 1441 DOMAIN_FOREACH(dp) { 1442 if (dp->dom_ifdetach != NULL && ifp->if_afdata[dp->dom_family]) 1443 { 1444 void *p = ifp->if_afdata[dp->dom_family]; 1445 if (p) { 1446 ifp->if_afdata[dp->dom_family] = NULL; 1447 (*dp->dom_ifdetach)(ifp, p); 1448 } 1449 } 1450 1451 /* 1452 * One would expect multicast memberships (INET and 1453 * INET6) on UDP sockets to be purged by the PURGEIF 1454 * calls above, but if all addresses were removed from 1455 * the interface prior to destruction, the calls will 1456 * not be made (e.g. ppp, for which pppd(8) generally 1457 * removes addresses before destroying the interface). 1458 * Because there is no invariant that multicast 1459 * memberships only exist for interfaces with IPv4 1460 * addresses, we must call PURGEIF regardless of 1461 * addresses. (Protocols which might store ifnet 1462 * pointers are marked with PR_PURGEIF.) 1463 */ 1464 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) { 1465 so.so_proto = pr; 1466 if (pr->pr_usrreqs && pr->pr_flags & PR_PURGEIF) 1467 (void)(*pr->pr_usrreqs->pr_purgeif)(&so, ifp); 1468 } 1469 } 1470 1471 /* 1472 * Must be done after the above pr_purgeif because if_psref may be 1473 * still used in pr_purgeif. 1474 */ 1475 psref_target_destroy(&ifp->if_psref, ifnet_psref_class); 1476 PSLIST_ENTRY_DESTROY(ifp, if_pslist_entry); 1477 1478 pfil_run_ifhooks(if_pfil, PFIL_IFNET_DETACH, ifp); 1479 (void)pfil_head_destroy(ifp->if_pfil); 1480 1481 /* Announce that the interface is gone. */ 1482 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 1483 1484 IF_AFDATA_LOCK_DESTROY(ifp); 1485 1486 if (if_is_link_state_changeable(ifp)) { 1487 softint_disestablish(ifp->if_link_si); 1488 ifp->if_link_si = NULL; 1489 } 1490 1491 /* 1492 * remove packets that came from ifp, from software interrupt queues. 1493 */ 1494 DOMAIN_FOREACH(dp) { 1495 for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) { 1496 struct ifqueue *iq = dp->dom_ifqueues[i]; 1497 if (iq == NULL) 1498 break; 1499 dp->dom_ifqueues[i] = NULL; 1500 if_detach_queues(ifp, iq); 1501 } 1502 } 1503 1504 /* 1505 * IP queues have to be processed separately: net-queue barrier 1506 * ensures that the packets are dequeued while a cross-call will 1507 * ensure that the interrupts have completed. FIXME: not quite.. 1508 */ 1509 #ifdef INET 1510 pktq_barrier(ip_pktq); 1511 #endif 1512 #ifdef INET6 1513 if (in6_present) 1514 pktq_barrier(ip6_pktq); 1515 #endif 1516 xc = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL); 1517 xc_wait(xc); 1518 1519 if (ifp->if_percpuq != NULL) { 1520 if_percpuq_destroy(ifp->if_percpuq); 1521 ifp->if_percpuq = NULL; 1522 } 1523 1524 mutex_obj_free(ifp->if_ioctl_lock); 1525 ifp->if_ioctl_lock = NULL; 1526 mutex_obj_free(ifp->if_snd.ifq_lock); 1527 1528 splx(s); 1529 1530 #ifdef IFAREF_DEBUG 1531 if_check_and_free_ifa_list(ifp); 1532 #endif 1533 } 1534 1535 static void 1536 if_detach_queues(struct ifnet *ifp, struct ifqueue *q) 1537 { 1538 struct mbuf *m, *prev, *next; 1539 1540 prev = NULL; 1541 for (m = q->ifq_head; m != NULL; m = next) { 1542 KASSERT((m->m_flags & M_PKTHDR) != 0); 1543 1544 next = m->m_nextpkt; 1545 if (m->m_pkthdr.rcvif_index != ifp->if_index) { 1546 prev = m; 1547 continue; 1548 } 1549 1550 if (prev != NULL) 1551 prev->m_nextpkt = m->m_nextpkt; 1552 else 1553 q->ifq_head = m->m_nextpkt; 1554 if (q->ifq_tail == m) 1555 q->ifq_tail = prev; 1556 q->ifq_len--; 1557 1558 m->m_nextpkt = NULL; 1559 m_freem(m); 1560 IF_DROP(q); 1561 } 1562 } 1563 1564 /* 1565 * Callback for a radix tree walk to delete all references to an 1566 * ifnet. 1567 */ 1568 static int 1569 if_delroute_matcher(struct rtentry *rt, void *v) 1570 { 1571 struct ifnet *ifp = (struct ifnet *)v; 1572 1573 if (rt->rt_ifp == ifp) 1574 return 1; 1575 else 1576 return 0; 1577 } 1578 1579 /* 1580 * Create a clone network interface. 1581 */ 1582 static int 1583 if_clone_create(const char *name) 1584 { 1585 struct if_clone *ifc; 1586 int unit; 1587 struct ifnet *ifp; 1588 struct psref psref; 1589 1590 KASSERT(mutex_owned(&if_clone_mtx)); 1591 1592 ifc = if_clone_lookup(name, &unit); 1593 if (ifc == NULL) 1594 return EINVAL; 1595 1596 ifp = if_get(name, &psref); 1597 if (ifp != NULL) { 1598 if_put(ifp, &psref); 1599 return EEXIST; 1600 } 1601 1602 return (*ifc->ifc_create)(ifc, unit); 1603 } 1604 1605 /* 1606 * Destroy a clone network interface. 1607 */ 1608 static int 1609 if_clone_destroy(const char *name) 1610 { 1611 struct if_clone *ifc; 1612 struct ifnet *ifp; 1613 struct psref psref; 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 ifp->if_ioctl = if_nullioctl; 1631 IFNET_UNLOCK(ifp); 1632 1633 /* 1634 * We cannot call ifc_destroy with holding ifp. 1635 * Releasing ifp here is safe thanks to if_clone_mtx. 1636 */ 1637 if_put(ifp, &psref); 1638 1639 return (*ifc->ifc_destroy)(ifp); 1640 } 1641 1642 static bool 1643 if_is_unit(const char *name) 1644 { 1645 1646 while (*name != '\0') { 1647 if (*name < '0' || *name > '9') 1648 return false; 1649 name++; 1650 } 1651 1652 return true; 1653 } 1654 1655 /* 1656 * Look up a network interface cloner. 1657 */ 1658 static struct if_clone * 1659 if_clone_lookup(const char *name, int *unitp) 1660 { 1661 struct if_clone *ifc; 1662 const char *cp; 1663 char *dp, ifname[IFNAMSIZ + 3]; 1664 int unit; 1665 1666 KASSERT(mutex_owned(&if_clone_mtx)); 1667 1668 strcpy(ifname, "if_"); 1669 /* separate interface name from unit */ 1670 /* TODO: search unit number from backward */ 1671 for (dp = ifname + 3, cp = name; cp - name < IFNAMSIZ && 1672 *cp && !if_is_unit(cp);) 1673 *dp++ = *cp++; 1674 1675 if (cp == name || cp - name == IFNAMSIZ || !*cp) 1676 return NULL; /* No name or unit number */ 1677 *dp++ = '\0'; 1678 1679 again: 1680 LIST_FOREACH(ifc, &if_cloners, ifc_list) { 1681 if (strcmp(ifname + 3, ifc->ifc_name) == 0) 1682 break; 1683 } 1684 1685 if (ifc == NULL) { 1686 int error; 1687 if (*ifname == '\0') 1688 return NULL; 1689 mutex_exit(&if_clone_mtx); 1690 error = module_autoload(ifname, MODULE_CLASS_DRIVER); 1691 mutex_enter(&if_clone_mtx); 1692 if (error) 1693 return NULL; 1694 *ifname = '\0'; 1695 goto again; 1696 } 1697 1698 unit = 0; 1699 while (cp - name < IFNAMSIZ && *cp) { 1700 if (*cp < '0' || *cp > '9' || unit >= INT_MAX / 10) { 1701 /* Bogus unit number. */ 1702 return NULL; 1703 } 1704 unit = (unit * 10) + (*cp++ - '0'); 1705 } 1706 1707 if (unitp != NULL) 1708 *unitp = unit; 1709 return ifc; 1710 } 1711 1712 /* 1713 * Register a network interface cloner. 1714 */ 1715 void 1716 if_clone_attach(struct if_clone *ifc) 1717 { 1718 1719 mutex_enter(&if_clone_mtx); 1720 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list); 1721 if_cloners_count++; 1722 mutex_exit(&if_clone_mtx); 1723 } 1724 1725 /* 1726 * Unregister a network interface cloner. 1727 */ 1728 void 1729 if_clone_detach(struct if_clone *ifc) 1730 { 1731 1732 mutex_enter(&if_clone_mtx); 1733 LIST_REMOVE(ifc, ifc_list); 1734 if_cloners_count--; 1735 mutex_exit(&if_clone_mtx); 1736 } 1737 1738 /* 1739 * Provide list of interface cloners to userspace. 1740 */ 1741 int 1742 if_clone_list(int buf_count, char *buffer, int *total) 1743 { 1744 char outbuf[IFNAMSIZ], *dst; 1745 struct if_clone *ifc; 1746 int count, error = 0; 1747 1748 mutex_enter(&if_clone_mtx); 1749 *total = if_cloners_count; 1750 if ((dst = buffer) == NULL) { 1751 /* Just asking how many there are. */ 1752 goto out; 1753 } 1754 1755 if (buf_count < 0) { 1756 error = EINVAL; 1757 goto out; 1758 } 1759 1760 count = (if_cloners_count < buf_count) ? 1761 if_cloners_count : buf_count; 1762 1763 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0; 1764 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) { 1765 (void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf)); 1766 if (outbuf[sizeof(outbuf) - 1] != '\0') { 1767 error = ENAMETOOLONG; 1768 goto out; 1769 } 1770 error = copyout(outbuf, dst, sizeof(outbuf)); 1771 if (error != 0) 1772 break; 1773 } 1774 1775 out: 1776 mutex_exit(&if_clone_mtx); 1777 return error; 1778 } 1779 1780 void 1781 ifa_psref_init(struct ifaddr *ifa) 1782 { 1783 1784 psref_target_init(&ifa->ifa_psref, ifa_psref_class); 1785 } 1786 1787 void 1788 ifaref(struct ifaddr *ifa) 1789 { 1790 1791 atomic_inc_uint(&ifa->ifa_refcnt); 1792 } 1793 1794 void 1795 ifafree(struct ifaddr *ifa) 1796 { 1797 KASSERT(ifa != NULL); 1798 KASSERT(ifa->ifa_refcnt > 0); 1799 1800 if (atomic_dec_uint_nv(&ifa->ifa_refcnt) == 0) { 1801 free(ifa, M_IFADDR); 1802 } 1803 } 1804 1805 bool 1806 ifa_is_destroying(struct ifaddr *ifa) 1807 { 1808 1809 return ISSET(ifa->ifa_flags, IFA_DESTROYING); 1810 } 1811 1812 void 1813 ifa_insert(struct ifnet *ifp, struct ifaddr *ifa) 1814 { 1815 1816 ifa->ifa_ifp = ifp; 1817 1818 /* 1819 * Check MP-safety for IFEF_MPSAFE drivers. 1820 * Check !IFF_RUNNING for initialization routines that normally don't 1821 * take IFNET_LOCK but it's safe because there is no competitor. 1822 * XXX there are false positive cases because IFF_RUNNING can be off on 1823 * if_stop. 1824 */ 1825 KASSERT(!if_is_mpsafe(ifp) || !ISSET(ifp->if_flags, IFF_RUNNING) || 1826 IFNET_LOCKED(ifp)); 1827 1828 TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list); 1829 IFADDR_ENTRY_INIT(ifa); 1830 IFADDR_WRITER_INSERT_TAIL(ifp, ifa); 1831 1832 ifaref(ifa); 1833 } 1834 1835 void 1836 ifa_remove(struct ifnet *ifp, struct ifaddr *ifa) 1837 { 1838 1839 KASSERT(ifa->ifa_ifp == ifp); 1840 /* 1841 * Check MP-safety for IFEF_MPSAFE drivers. 1842 * if_is_deactivated indicates ifa_remove is called form if_detach 1843 * where is safe even if IFNET_LOCK isn't held. 1844 */ 1845 KASSERT(!if_is_mpsafe(ifp) || if_is_deactivated(ifp) || IFNET_LOCKED(ifp)); 1846 1847 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list); 1848 IFADDR_WRITER_REMOVE(ifa); 1849 #ifdef NET_MPSAFE 1850 IFNET_GLOBAL_LOCK(); 1851 pserialize_perform(ifnet_psz); 1852 IFNET_GLOBAL_UNLOCK(); 1853 #endif 1854 1855 #ifdef NET_MPSAFE 1856 psref_target_destroy(&ifa->ifa_psref, ifa_psref_class); 1857 #endif 1858 IFADDR_ENTRY_DESTROY(ifa); 1859 ifafree(ifa); 1860 } 1861 1862 void 1863 ifa_acquire(struct ifaddr *ifa, struct psref *psref) 1864 { 1865 1866 PSREF_DEBUG_FILL_RETURN_ADDRESS(psref); 1867 psref_acquire(psref, &ifa->ifa_psref, ifa_psref_class); 1868 } 1869 1870 void 1871 ifa_release(struct ifaddr *ifa, struct psref *psref) 1872 { 1873 1874 if (ifa == NULL) 1875 return; 1876 1877 psref_release(psref, &ifa->ifa_psref, ifa_psref_class); 1878 } 1879 1880 bool 1881 ifa_held(struct ifaddr *ifa) 1882 { 1883 1884 return psref_held(&ifa->ifa_psref, ifa_psref_class); 1885 } 1886 1887 static inline int 1888 equal(const struct sockaddr *sa1, const struct sockaddr *sa2) 1889 { 1890 return sockaddr_cmp(sa1, sa2) == 0; 1891 } 1892 1893 /* 1894 * Locate an interface based on a complete address. 1895 */ 1896 /*ARGSUSED*/ 1897 struct ifaddr * 1898 ifa_ifwithaddr(const struct sockaddr *addr) 1899 { 1900 struct ifnet *ifp; 1901 struct ifaddr *ifa; 1902 1903 IFNET_READER_FOREACH(ifp) { 1904 if (if_is_deactivated(ifp)) 1905 continue; 1906 IFADDR_READER_FOREACH(ifa, ifp) { 1907 if (ifa->ifa_addr->sa_family != addr->sa_family) 1908 continue; 1909 if (equal(addr, ifa->ifa_addr)) 1910 return ifa; 1911 if ((ifp->if_flags & IFF_BROADCAST) && 1912 ifa->ifa_broadaddr && 1913 /* IP6 doesn't have broadcast */ 1914 ifa->ifa_broadaddr->sa_len != 0 && 1915 equal(ifa->ifa_broadaddr, addr)) 1916 return ifa; 1917 } 1918 } 1919 return NULL; 1920 } 1921 1922 struct ifaddr * 1923 ifa_ifwithaddr_psref(const struct sockaddr *addr, struct psref *psref) 1924 { 1925 struct ifaddr *ifa; 1926 int s = pserialize_read_enter(); 1927 1928 ifa = ifa_ifwithaddr(addr); 1929 if (ifa != NULL) 1930 ifa_acquire(ifa, psref); 1931 pserialize_read_exit(s); 1932 1933 return ifa; 1934 } 1935 1936 /* 1937 * Locate the point to point interface with a given destination address. 1938 */ 1939 /*ARGSUSED*/ 1940 struct ifaddr * 1941 ifa_ifwithdstaddr(const struct sockaddr *addr) 1942 { 1943 struct ifnet *ifp; 1944 struct ifaddr *ifa; 1945 1946 IFNET_READER_FOREACH(ifp) { 1947 if (if_is_deactivated(ifp)) 1948 continue; 1949 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 1950 continue; 1951 IFADDR_READER_FOREACH(ifa, ifp) { 1952 if (ifa->ifa_addr->sa_family != addr->sa_family || 1953 ifa->ifa_dstaddr == NULL) 1954 continue; 1955 if (equal(addr, ifa->ifa_dstaddr)) 1956 return ifa; 1957 } 1958 } 1959 1960 return NULL; 1961 } 1962 1963 struct ifaddr * 1964 ifa_ifwithdstaddr_psref(const struct sockaddr *addr, struct psref *psref) 1965 { 1966 struct ifaddr *ifa; 1967 int s; 1968 1969 s = pserialize_read_enter(); 1970 ifa = ifa_ifwithdstaddr(addr); 1971 if (ifa != NULL) 1972 ifa_acquire(ifa, psref); 1973 pserialize_read_exit(s); 1974 1975 return ifa; 1976 } 1977 1978 /* 1979 * Find an interface on a specific network. If many, choice 1980 * is most specific found. 1981 */ 1982 struct ifaddr * 1983 ifa_ifwithnet(const struct sockaddr *addr) 1984 { 1985 struct ifnet *ifp; 1986 struct ifaddr *ifa, *ifa_maybe = NULL; 1987 const struct sockaddr_dl *sdl; 1988 u_int af = addr->sa_family; 1989 const char *addr_data = addr->sa_data, *cplim; 1990 1991 if (af == AF_LINK) { 1992 sdl = satocsdl(addr); 1993 if (sdl->sdl_index && sdl->sdl_index < if_indexlim && 1994 ifindex2ifnet[sdl->sdl_index] && 1995 !if_is_deactivated(ifindex2ifnet[sdl->sdl_index])) { 1996 return ifindex2ifnet[sdl->sdl_index]->if_dl; 1997 } 1998 } 1999 #ifdef NETATALK 2000 if (af == AF_APPLETALK) { 2001 const struct sockaddr_at *sat, *sat2; 2002 sat = (const struct sockaddr_at *)addr; 2003 IFNET_READER_FOREACH(ifp) { 2004 if (if_is_deactivated(ifp)) 2005 continue; 2006 ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp); 2007 if (ifa == NULL) 2008 continue; 2009 sat2 = (struct sockaddr_at *)ifa->ifa_addr; 2010 if (sat2->sat_addr.s_net == sat->sat_addr.s_net) 2011 return ifa; /* exact match */ 2012 if (ifa_maybe == NULL) { 2013 /* else keep the if with the right range */ 2014 ifa_maybe = ifa; 2015 } 2016 } 2017 return ifa_maybe; 2018 } 2019 #endif 2020 IFNET_READER_FOREACH(ifp) { 2021 if (if_is_deactivated(ifp)) 2022 continue; 2023 IFADDR_READER_FOREACH(ifa, ifp) { 2024 const char *cp, *cp2, *cp3; 2025 2026 if (ifa->ifa_addr->sa_family != af || 2027 ifa->ifa_netmask == NULL) 2028 next: continue; 2029 cp = addr_data; 2030 cp2 = ifa->ifa_addr->sa_data; 2031 cp3 = ifa->ifa_netmask->sa_data; 2032 cplim = (const char *)ifa->ifa_netmask + 2033 ifa->ifa_netmask->sa_len; 2034 while (cp3 < cplim) { 2035 if ((*cp++ ^ *cp2++) & *cp3++) { 2036 /* want to continue for() loop */ 2037 goto next; 2038 } 2039 } 2040 if (ifa_maybe == NULL || 2041 rt_refines(ifa->ifa_netmask, 2042 ifa_maybe->ifa_netmask)) 2043 ifa_maybe = ifa; 2044 } 2045 } 2046 return ifa_maybe; 2047 } 2048 2049 struct ifaddr * 2050 ifa_ifwithnet_psref(const struct sockaddr *addr, struct psref *psref) 2051 { 2052 struct ifaddr *ifa; 2053 int s; 2054 2055 s = pserialize_read_enter(); 2056 ifa = ifa_ifwithnet(addr); 2057 if (ifa != NULL) 2058 ifa_acquire(ifa, psref); 2059 pserialize_read_exit(s); 2060 2061 return ifa; 2062 } 2063 2064 /* 2065 * Find the interface of the addresss. 2066 */ 2067 struct ifaddr * 2068 ifa_ifwithladdr(const struct sockaddr *addr) 2069 { 2070 struct ifaddr *ia; 2071 2072 if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) || 2073 (ia = ifa_ifwithnet(addr))) 2074 return ia; 2075 return NULL; 2076 } 2077 2078 struct ifaddr * 2079 ifa_ifwithladdr_psref(const struct sockaddr *addr, struct psref *psref) 2080 { 2081 struct ifaddr *ifa; 2082 int s; 2083 2084 s = pserialize_read_enter(); 2085 ifa = ifa_ifwithladdr(addr); 2086 if (ifa != NULL) 2087 ifa_acquire(ifa, psref); 2088 pserialize_read_exit(s); 2089 2090 return ifa; 2091 } 2092 2093 /* 2094 * Find an interface using a specific address family 2095 */ 2096 struct ifaddr * 2097 ifa_ifwithaf(int af) 2098 { 2099 struct ifnet *ifp; 2100 struct ifaddr *ifa = NULL; 2101 int s; 2102 2103 s = pserialize_read_enter(); 2104 IFNET_READER_FOREACH(ifp) { 2105 if (if_is_deactivated(ifp)) 2106 continue; 2107 IFADDR_READER_FOREACH(ifa, ifp) { 2108 if (ifa->ifa_addr->sa_family == af) 2109 goto out; 2110 } 2111 } 2112 out: 2113 pserialize_read_exit(s); 2114 return ifa; 2115 } 2116 2117 /* 2118 * Find an interface address specific to an interface best matching 2119 * a given address. 2120 */ 2121 struct ifaddr * 2122 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp) 2123 { 2124 struct ifaddr *ifa; 2125 const char *cp, *cp2, *cp3; 2126 const char *cplim; 2127 struct ifaddr *ifa_maybe = 0; 2128 u_int af = addr->sa_family; 2129 2130 if (if_is_deactivated(ifp)) 2131 return NULL; 2132 2133 if (af >= AF_MAX) 2134 return NULL; 2135 2136 IFADDR_READER_FOREACH(ifa, ifp) { 2137 if (ifa->ifa_addr->sa_family != af) 2138 continue; 2139 ifa_maybe = ifa; 2140 if (ifa->ifa_netmask == NULL) { 2141 if (equal(addr, ifa->ifa_addr) || 2142 (ifa->ifa_dstaddr && 2143 equal(addr, ifa->ifa_dstaddr))) 2144 return ifa; 2145 continue; 2146 } 2147 cp = addr->sa_data; 2148 cp2 = ifa->ifa_addr->sa_data; 2149 cp3 = ifa->ifa_netmask->sa_data; 2150 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 2151 for (; cp3 < cplim; cp3++) { 2152 if ((*cp++ ^ *cp2++) & *cp3) 2153 break; 2154 } 2155 if (cp3 == cplim) 2156 return ifa; 2157 } 2158 return ifa_maybe; 2159 } 2160 2161 struct ifaddr * 2162 ifaof_ifpforaddr_psref(const struct sockaddr *addr, struct ifnet *ifp, 2163 struct psref *psref) 2164 { 2165 struct ifaddr *ifa; 2166 int s; 2167 2168 s = pserialize_read_enter(); 2169 ifa = ifaof_ifpforaddr(addr, ifp); 2170 if (ifa != NULL) 2171 ifa_acquire(ifa, psref); 2172 pserialize_read_exit(s); 2173 2174 return ifa; 2175 } 2176 2177 /* 2178 * Default action when installing a route with a Link Level gateway. 2179 * Lookup an appropriate real ifa to point to. 2180 * This should be moved to /sys/net/link.c eventually. 2181 */ 2182 void 2183 link_rtrequest(int cmd, struct rtentry *rt, const struct rt_addrinfo *info) 2184 { 2185 struct ifaddr *ifa; 2186 const struct sockaddr *dst; 2187 struct ifnet *ifp; 2188 struct psref psref; 2189 2190 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL || 2191 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_getkey(rt)) == NULL || 2192 ISSET(info->rti_flags, RTF_DONTCHANGEIFA)) 2193 return; 2194 if ((ifa = ifaof_ifpforaddr_psref(dst, ifp, &psref)) != NULL) { 2195 rt_replace_ifa(rt, ifa); 2196 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 2197 ifa->ifa_rtrequest(cmd, rt, info); 2198 ifa_release(ifa, &psref); 2199 } 2200 } 2201 2202 /* 2203 * bitmask macros to manage a densely packed link_state change queue. 2204 * Because we need to store LINK_STATE_UNKNOWN(0), LINK_STATE_DOWN(1) and 2205 * LINK_STATE_UP(2) we need 2 bits for each state change. 2206 * As a state change to store is 0, treat all bits set as an unset item. 2207 */ 2208 #define LQ_ITEM_BITS 2 2209 #define LQ_ITEM_MASK ((1 << LQ_ITEM_BITS) - 1) 2210 #define LQ_MASK(i) (LQ_ITEM_MASK << (i) * LQ_ITEM_BITS) 2211 #define LINK_STATE_UNSET LQ_ITEM_MASK 2212 #define LQ_ITEM(q, i) (((q) & LQ_MASK((i))) >> (i) * LQ_ITEM_BITS) 2213 #define LQ_STORE(q, i, v) \ 2214 do { \ 2215 (q) &= ~LQ_MASK((i)); \ 2216 (q) |= (v) << (i) * LQ_ITEM_BITS; \ 2217 } while (0 /* CONSTCOND */) 2218 #define LQ_MAX(q) ((sizeof((q)) * NBBY) / LQ_ITEM_BITS) 2219 #define LQ_POP(q, v) \ 2220 do { \ 2221 (v) = LQ_ITEM((q), 0); \ 2222 (q) >>= LQ_ITEM_BITS; \ 2223 (q) |= LINK_STATE_UNSET << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS; \ 2224 } while (0 /* CONSTCOND */) 2225 #define LQ_PUSH(q, v) \ 2226 do { \ 2227 (q) >>= LQ_ITEM_BITS; \ 2228 (q) |= (v) << (LQ_MAX((q)) - 1) * LQ_ITEM_BITS; \ 2229 } while (0 /* CONSTCOND */) 2230 #define LQ_FIND_UNSET(q, i) \ 2231 for ((i) = 0; i < LQ_MAX((q)); (i)++) { \ 2232 if (LQ_ITEM((q), (i)) == LINK_STATE_UNSET) \ 2233 break; \ 2234 } 2235 2236 /* 2237 * XXX reusing (ifp)->if_snd->ifq_lock rather than having another spin mutex 2238 * for each ifnet. It doesn't matter because: 2239 * - if IFEF_MPSAFE is enabled, if_snd isn't used and lock contentions on 2240 * ifq_lock don't happen 2241 * - if IFEF_MPSAFE is disabled, there is no lock contention on ifq_lock 2242 * because if_snd, if_link_state_change and if_link_state_change_softint 2243 * are all called with KERNEL_LOCK 2244 */ 2245 #define IF_LINK_STATE_CHANGE_LOCK(ifp) \ 2246 mutex_enter((ifp)->if_snd.ifq_lock) 2247 #define IF_LINK_STATE_CHANGE_UNLOCK(ifp) \ 2248 mutex_exit((ifp)->if_snd.ifq_lock) 2249 2250 /* 2251 * Handle a change in the interface link state and 2252 * queue notifications. 2253 */ 2254 void 2255 if_link_state_change(struct ifnet *ifp, int link_state) 2256 { 2257 int idx; 2258 2259 KASSERTMSG(if_is_link_state_changeable(ifp), 2260 "%s: IFEF_NO_LINK_STATE_CHANGE must not be set, but if_extflags=0x%x", 2261 ifp->if_xname, ifp->if_extflags); 2262 2263 /* Ensure change is to a valid state */ 2264 switch (link_state) { 2265 case LINK_STATE_UNKNOWN: /* FALLTHROUGH */ 2266 case LINK_STATE_DOWN: /* FALLTHROUGH */ 2267 case LINK_STATE_UP: 2268 break; 2269 default: 2270 #ifdef DEBUG 2271 printf("%s: invalid link state %d\n", 2272 ifp->if_xname, link_state); 2273 #endif 2274 return; 2275 } 2276 2277 IF_LINK_STATE_CHANGE_LOCK(ifp); 2278 2279 /* Find the last unset event in the queue. */ 2280 LQ_FIND_UNSET(ifp->if_link_queue, idx); 2281 2282 /* 2283 * Ensure link_state doesn't match the last event in the queue. 2284 * ifp->if_link_state is not checked and set here because 2285 * that would present an inconsistent picture to the system. 2286 */ 2287 if (idx != 0 && 2288 LQ_ITEM(ifp->if_link_queue, idx - 1) == (uint8_t)link_state) 2289 goto out; 2290 2291 /* Handle queue overflow. */ 2292 if (idx == LQ_MAX(ifp->if_link_queue)) { 2293 uint8_t lost; 2294 2295 /* 2296 * The DOWN state must be protected from being pushed off 2297 * the queue to ensure that userland will always be 2298 * in a sane state. 2299 * Because DOWN is protected, there is no need to protect 2300 * UNKNOWN. 2301 * It should be invalid to change from any other state to 2302 * UNKNOWN anyway ... 2303 */ 2304 lost = LQ_ITEM(ifp->if_link_queue, 0); 2305 LQ_PUSH(ifp->if_link_queue, (uint8_t)link_state); 2306 if (lost == LINK_STATE_DOWN) { 2307 lost = LQ_ITEM(ifp->if_link_queue, 0); 2308 LQ_STORE(ifp->if_link_queue, 0, LINK_STATE_DOWN); 2309 } 2310 printf("%s: lost link state change %s\n", 2311 ifp->if_xname, 2312 lost == LINK_STATE_UP ? "UP" : 2313 lost == LINK_STATE_DOWN ? "DOWN" : 2314 "UNKNOWN"); 2315 } else 2316 LQ_STORE(ifp->if_link_queue, idx, (uint8_t)link_state); 2317 2318 softint_schedule(ifp->if_link_si); 2319 2320 out: 2321 IF_LINK_STATE_CHANGE_UNLOCK(ifp); 2322 } 2323 2324 /* 2325 * Handle interface link state change notifications. 2326 */ 2327 void 2328 if_link_state_change_softint(struct ifnet *ifp, int link_state) 2329 { 2330 struct domain *dp; 2331 int s = splnet(); 2332 bool notify; 2333 2334 KASSERT(!cpu_intr_p()); 2335 2336 IF_LINK_STATE_CHANGE_LOCK(ifp); 2337 2338 /* Ensure the change is still valid. */ 2339 if (ifp->if_link_state == link_state) { 2340 IF_LINK_STATE_CHANGE_UNLOCK(ifp); 2341 splx(s); 2342 return; 2343 } 2344 2345 #ifdef DEBUG 2346 log(LOG_DEBUG, "%s: link state %s (was %s)\n", ifp->if_xname, 2347 link_state == LINK_STATE_UP ? "UP" : 2348 link_state == LINK_STATE_DOWN ? "DOWN" : 2349 "UNKNOWN", 2350 ifp->if_link_state == LINK_STATE_UP ? "UP" : 2351 ifp->if_link_state == LINK_STATE_DOWN ? "DOWN" : 2352 "UNKNOWN"); 2353 #endif 2354 2355 /* 2356 * When going from UNKNOWN to UP, we need to mark existing 2357 * addresses as tentative and restart DAD as we may have 2358 * erroneously not found a duplicate. 2359 * 2360 * This needs to happen before rt_ifmsg to avoid a race where 2361 * listeners would have an address and expect it to work right 2362 * away. 2363 */ 2364 notify = (link_state == LINK_STATE_UP && 2365 ifp->if_link_state == LINK_STATE_UNKNOWN); 2366 ifp->if_link_state = link_state; 2367 /* The following routines may sleep so release the spin mutex */ 2368 IF_LINK_STATE_CHANGE_UNLOCK(ifp); 2369 2370 KERNEL_LOCK_UNLESS_NET_MPSAFE(); 2371 if (notify) { 2372 DOMAIN_FOREACH(dp) { 2373 if (dp->dom_if_link_state_change != NULL) 2374 dp->dom_if_link_state_change(ifp, 2375 LINK_STATE_DOWN); 2376 } 2377 } 2378 2379 /* Notify that the link state has changed. */ 2380 rt_ifmsg(ifp); 2381 2382 #if NCARP > 0 2383 if (ifp->if_carp) 2384 carp_carpdev_state(ifp); 2385 #endif 2386 2387 DOMAIN_FOREACH(dp) { 2388 if (dp->dom_if_link_state_change != NULL) 2389 dp->dom_if_link_state_change(ifp, link_state); 2390 } 2391 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 2392 splx(s); 2393 } 2394 2395 /* 2396 * Process the interface link state change queue. 2397 */ 2398 static void 2399 if_link_state_change_si(void *arg) 2400 { 2401 struct ifnet *ifp = arg; 2402 int s; 2403 uint8_t state; 2404 bool schedule; 2405 2406 SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE(); 2407 s = splnet(); 2408 2409 /* Pop a link state change from the queue and process it. */ 2410 IF_LINK_STATE_CHANGE_LOCK(ifp); 2411 LQ_POP(ifp->if_link_queue, state); 2412 IF_LINK_STATE_CHANGE_UNLOCK(ifp); 2413 2414 if_link_state_change_softint(ifp, state); 2415 2416 /* If there is a link state change to come, schedule it. */ 2417 IF_LINK_STATE_CHANGE_LOCK(ifp); 2418 schedule = (LQ_ITEM(ifp->if_link_queue, 0) != LINK_STATE_UNSET); 2419 IF_LINK_STATE_CHANGE_UNLOCK(ifp); 2420 if (schedule) 2421 softint_schedule(ifp->if_link_si); 2422 2423 splx(s); 2424 SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 2425 } 2426 2427 /* 2428 * Default action when installing a local route on a point-to-point 2429 * interface. 2430 */ 2431 void 2432 p2p_rtrequest(int req, struct rtentry *rt, 2433 __unused const struct rt_addrinfo *info) 2434 { 2435 struct ifnet *ifp = rt->rt_ifp; 2436 struct ifaddr *ifa, *lo0ifa; 2437 int s = pserialize_read_enter(); 2438 2439 switch (req) { 2440 case RTM_ADD: 2441 if ((rt->rt_flags & RTF_LOCAL) == 0) 2442 break; 2443 2444 rt->rt_ifp = lo0ifp; 2445 2446 if (ISSET(info->rti_flags, RTF_DONTCHANGEIFA)) 2447 break; 2448 2449 IFADDR_READER_FOREACH(ifa, ifp) { 2450 if (equal(rt_getkey(rt), ifa->ifa_addr)) 2451 break; 2452 } 2453 if (ifa == NULL) 2454 break; 2455 2456 /* 2457 * Ensure lo0 has an address of the same family. 2458 */ 2459 IFADDR_READER_FOREACH(lo0ifa, lo0ifp) { 2460 if (lo0ifa->ifa_addr->sa_family == 2461 ifa->ifa_addr->sa_family) 2462 break; 2463 } 2464 if (lo0ifa == NULL) 2465 break; 2466 2467 /* 2468 * Make sure to set rt->rt_ifa to the interface 2469 * address we are using, otherwise we will have trouble 2470 * with source address selection. 2471 */ 2472 if (ifa != rt->rt_ifa) 2473 rt_replace_ifa(rt, ifa); 2474 break; 2475 case RTM_DELETE: 2476 default: 2477 break; 2478 } 2479 pserialize_read_exit(s); 2480 } 2481 2482 static void 2483 _if_down(struct ifnet *ifp) 2484 { 2485 struct ifaddr *ifa; 2486 struct domain *dp; 2487 int s, bound; 2488 struct psref psref; 2489 2490 ifp->if_flags &= ~IFF_UP; 2491 nanotime(&ifp->if_lastchange); 2492 2493 bound = curlwp_bind(); 2494 s = pserialize_read_enter(); 2495 IFADDR_READER_FOREACH(ifa, ifp) { 2496 ifa_acquire(ifa, &psref); 2497 pserialize_read_exit(s); 2498 2499 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 2500 2501 s = pserialize_read_enter(); 2502 ifa_release(ifa, &psref); 2503 } 2504 pserialize_read_exit(s); 2505 curlwp_bindx(bound); 2506 2507 IFQ_PURGE(&ifp->if_snd); 2508 #if NCARP > 0 2509 if (ifp->if_carp) 2510 carp_carpdev_state(ifp); 2511 #endif 2512 rt_ifmsg(ifp); 2513 DOMAIN_FOREACH(dp) { 2514 if (dp->dom_if_down) 2515 dp->dom_if_down(ifp); 2516 } 2517 } 2518 2519 static void 2520 if_down_deactivated(struct ifnet *ifp) 2521 { 2522 2523 KASSERT(if_is_deactivated(ifp)); 2524 _if_down(ifp); 2525 } 2526 2527 void 2528 if_down_locked(struct ifnet *ifp) 2529 { 2530 2531 KASSERT(IFNET_LOCKED(ifp)); 2532 _if_down(ifp); 2533 } 2534 2535 /* 2536 * Mark an interface down and notify protocols of 2537 * the transition. 2538 * NOTE: must be called at splsoftnet or equivalent. 2539 */ 2540 void 2541 if_down(struct ifnet *ifp) 2542 { 2543 2544 IFNET_LOCK(ifp); 2545 if_down_locked(ifp); 2546 IFNET_UNLOCK(ifp); 2547 } 2548 2549 /* 2550 * Must be called with holding if_ioctl_lock. 2551 */ 2552 static void 2553 if_up_locked(struct ifnet *ifp) 2554 { 2555 #ifdef notyet 2556 struct ifaddr *ifa; 2557 #endif 2558 struct domain *dp; 2559 2560 KASSERT(IFNET_LOCKED(ifp)); 2561 2562 KASSERT(!if_is_deactivated(ifp)); 2563 ifp->if_flags |= IFF_UP; 2564 nanotime(&ifp->if_lastchange); 2565 #ifdef notyet 2566 /* this has no effect on IP, and will kill all ISO connections XXX */ 2567 IFADDR_READER_FOREACH(ifa, ifp) 2568 pfctlinput(PRC_IFUP, ifa->ifa_addr); 2569 #endif 2570 #if NCARP > 0 2571 if (ifp->if_carp) 2572 carp_carpdev_state(ifp); 2573 #endif 2574 rt_ifmsg(ifp); 2575 DOMAIN_FOREACH(dp) { 2576 if (dp->dom_if_up) 2577 dp->dom_if_up(ifp); 2578 } 2579 } 2580 2581 /* 2582 * Handle interface slowtimo timer routine. Called 2583 * from softclock, we decrement timer (if set) and 2584 * call the appropriate interface routine on expiration. 2585 */ 2586 static void 2587 if_slowtimo(void *arg) 2588 { 2589 void (*slowtimo)(struct ifnet *); 2590 struct ifnet *ifp = arg; 2591 int s; 2592 2593 slowtimo = ifp->if_slowtimo; 2594 if (__predict_false(slowtimo == NULL)) 2595 return; 2596 2597 s = splnet(); 2598 if (ifp->if_timer != 0 && --ifp->if_timer == 0) 2599 (*slowtimo)(ifp); 2600 2601 splx(s); 2602 2603 if (__predict_true(ifp->if_slowtimo != NULL)) 2604 callout_schedule(ifp->if_slowtimo_ch, hz / IFNET_SLOWHZ); 2605 } 2606 2607 /* 2608 * Mark an interface up and notify protocols of 2609 * the transition. 2610 * NOTE: must be called at splsoftnet or equivalent. 2611 */ 2612 void 2613 if_up(struct ifnet *ifp) 2614 { 2615 2616 IFNET_LOCK(ifp); 2617 if_up_locked(ifp); 2618 IFNET_UNLOCK(ifp); 2619 } 2620 2621 /* 2622 * Set/clear promiscuous mode on interface ifp based on the truth value 2623 * of pswitch. The calls are reference counted so that only the first 2624 * "on" request actually has an effect, as does the final "off" request. 2625 * Results are undefined if the "off" and "on" requests are not matched. 2626 */ 2627 int 2628 ifpromisc_locked(struct ifnet *ifp, int pswitch) 2629 { 2630 int pcount, ret = 0; 2631 short nflags; 2632 2633 KASSERT(IFNET_LOCKED(ifp)); 2634 2635 pcount = ifp->if_pcount; 2636 if (pswitch) { 2637 /* 2638 * Allow the device to be "placed" into promiscuous 2639 * mode even if it is not configured up. It will 2640 * consult IFF_PROMISC when it is brought up. 2641 */ 2642 if (ifp->if_pcount++ != 0) 2643 goto out; 2644 nflags = ifp->if_flags | IFF_PROMISC; 2645 } else { 2646 if (--ifp->if_pcount > 0) 2647 goto out; 2648 nflags = ifp->if_flags & ~IFF_PROMISC; 2649 } 2650 ret = if_flags_set(ifp, nflags); 2651 /* Restore interface state if not successful. */ 2652 if (ret != 0) { 2653 ifp->if_pcount = pcount; 2654 } 2655 out: 2656 return ret; 2657 } 2658 2659 int 2660 ifpromisc(struct ifnet *ifp, int pswitch) 2661 { 2662 int e; 2663 2664 IFNET_LOCK(ifp); 2665 e = ifpromisc_locked(ifp, pswitch); 2666 IFNET_UNLOCK(ifp); 2667 2668 return e; 2669 } 2670 2671 /* 2672 * Map interface name to 2673 * interface structure pointer. 2674 */ 2675 struct ifnet * 2676 ifunit(const char *name) 2677 { 2678 struct ifnet *ifp; 2679 const char *cp = name; 2680 u_int unit = 0; 2681 u_int i; 2682 int s; 2683 2684 /* 2685 * If the entire name is a number, treat it as an ifindex. 2686 */ 2687 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) { 2688 unit = unit * 10 + (*cp - '0'); 2689 } 2690 2691 /* 2692 * If the number took all of the name, then it's a valid ifindex. 2693 */ 2694 if (i == IFNAMSIZ || (cp != name && *cp == '\0')) 2695 return if_byindex(unit); 2696 2697 ifp = NULL; 2698 s = pserialize_read_enter(); 2699 IFNET_READER_FOREACH(ifp) { 2700 if (if_is_deactivated(ifp)) 2701 continue; 2702 if (strcmp(ifp->if_xname, name) == 0) 2703 goto out; 2704 } 2705 out: 2706 pserialize_read_exit(s); 2707 return ifp; 2708 } 2709 2710 /* 2711 * Get a reference of an ifnet object by an interface name. 2712 * The returned reference is protected by psref(9). The caller 2713 * must release a returned reference by if_put after use. 2714 */ 2715 struct ifnet * 2716 if_get(const char *name, struct psref *psref) 2717 { 2718 struct ifnet *ifp; 2719 const char *cp = name; 2720 u_int unit = 0; 2721 u_int i; 2722 int s; 2723 2724 /* 2725 * If the entire name is a number, treat it as an ifindex. 2726 */ 2727 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) { 2728 unit = unit * 10 + (*cp - '0'); 2729 } 2730 2731 /* 2732 * If the number took all of the name, then it's a valid ifindex. 2733 */ 2734 if (i == IFNAMSIZ || (cp != name && *cp == '\0')) 2735 return if_get_byindex(unit, psref); 2736 2737 ifp = NULL; 2738 s = pserialize_read_enter(); 2739 IFNET_READER_FOREACH(ifp) { 2740 if (if_is_deactivated(ifp)) 2741 continue; 2742 if (strcmp(ifp->if_xname, name) == 0) { 2743 PSREF_DEBUG_FILL_RETURN_ADDRESS(psref); 2744 psref_acquire(psref, &ifp->if_psref, 2745 ifnet_psref_class); 2746 goto out; 2747 } 2748 } 2749 out: 2750 pserialize_read_exit(s); 2751 return ifp; 2752 } 2753 2754 /* 2755 * Release a reference of an ifnet object given by if_get, if_get_byindex 2756 * or if_get_bylla. 2757 */ 2758 void 2759 if_put(const struct ifnet *ifp, struct psref *psref) 2760 { 2761 2762 if (ifp == NULL) 2763 return; 2764 2765 psref_release(psref, &ifp->if_psref, ifnet_psref_class); 2766 } 2767 2768 /* 2769 * Return ifp having idx. Return NULL if not found. Normally if_byindex 2770 * should be used. 2771 */ 2772 ifnet_t * 2773 _if_byindex(u_int idx) 2774 { 2775 2776 return (__predict_true(idx < if_indexlim)) ? ifindex2ifnet[idx] : NULL; 2777 } 2778 2779 /* 2780 * Return ifp having idx. Return NULL if not found or the found ifp is 2781 * already deactivated. 2782 */ 2783 ifnet_t * 2784 if_byindex(u_int idx) 2785 { 2786 ifnet_t *ifp; 2787 2788 ifp = _if_byindex(idx); 2789 if (ifp != NULL && if_is_deactivated(ifp)) 2790 ifp = NULL; 2791 return ifp; 2792 } 2793 2794 /* 2795 * Get a reference of an ifnet object by an interface index. 2796 * The returned reference is protected by psref(9). The caller 2797 * must release a returned reference by if_put after use. 2798 */ 2799 ifnet_t * 2800 if_get_byindex(u_int idx, struct psref *psref) 2801 { 2802 ifnet_t *ifp; 2803 int s; 2804 2805 s = pserialize_read_enter(); 2806 ifp = if_byindex(idx); 2807 if (__predict_true(ifp != NULL)) { 2808 PSREF_DEBUG_FILL_RETURN_ADDRESS(psref); 2809 psref_acquire(psref, &ifp->if_psref, ifnet_psref_class); 2810 } 2811 pserialize_read_exit(s); 2812 2813 return ifp; 2814 } 2815 2816 ifnet_t * 2817 if_get_bylla(const void *lla, unsigned char lla_len, struct psref *psref) 2818 { 2819 ifnet_t *ifp; 2820 int s; 2821 2822 s = pserialize_read_enter(); 2823 IFNET_READER_FOREACH(ifp) { 2824 if (if_is_deactivated(ifp)) 2825 continue; 2826 if (ifp->if_addrlen != lla_len) 2827 continue; 2828 if (memcmp(lla, CLLADDR(ifp->if_sadl), lla_len) == 0) { 2829 psref_acquire(psref, &ifp->if_psref, 2830 ifnet_psref_class); 2831 break; 2832 } 2833 } 2834 pserialize_read_exit(s); 2835 2836 return ifp; 2837 } 2838 2839 /* 2840 * Note that it's safe only if the passed ifp is guaranteed to not be freed, 2841 * for example using pserialize or the ifp is already held or some other 2842 * object is held which guarantes the ifp to not be freed indirectly. 2843 */ 2844 void 2845 if_acquire(struct ifnet *ifp, struct psref *psref) 2846 { 2847 2848 KASSERT(ifp->if_index != 0); 2849 psref_acquire(psref, &ifp->if_psref, ifnet_psref_class); 2850 } 2851 2852 bool 2853 if_held(struct ifnet *ifp) 2854 { 2855 2856 return psref_held(&ifp->if_psref, ifnet_psref_class); 2857 } 2858 2859 /* 2860 * Some tunnel interfaces can nest, e.g. IPv4 over IPv4 gif(4) tunnel over IPv4. 2861 * Check the tunnel nesting count. 2862 * Return > 0, if tunnel nesting count is more than limit. 2863 * Return 0, if tunnel nesting count is equal or less than limit. 2864 */ 2865 int 2866 if_tunnel_check_nesting(struct ifnet *ifp, struct mbuf *m, int limit) 2867 { 2868 struct m_tag *mtag; 2869 int *count; 2870 2871 mtag = m_tag_find(m, PACKET_TAG_TUNNEL_INFO); 2872 if (mtag != NULL) { 2873 count = (int *)(mtag + 1); 2874 if (++(*count) > limit) { 2875 log(LOG_NOTICE, 2876 "%s: recursively called too many times(%d)\n", 2877 ifp->if_xname, *count); 2878 return EIO; 2879 } 2880 } else { 2881 mtag = m_tag_get(PACKET_TAG_TUNNEL_INFO, sizeof(*count), 2882 M_NOWAIT); 2883 if (mtag != NULL) { 2884 m_tag_prepend(m, mtag); 2885 count = (int *)(mtag + 1); 2886 *count = 0; 2887 } else { 2888 log(LOG_DEBUG, 2889 "%s: m_tag_get() failed, recursion calls are not prevented.\n", 2890 ifp->if_xname); 2891 } 2892 } 2893 2894 return 0; 2895 } 2896 2897 /* common */ 2898 int 2899 ifioctl_common(struct ifnet *ifp, u_long cmd, void *data) 2900 { 2901 int s; 2902 struct ifreq *ifr; 2903 struct ifcapreq *ifcr; 2904 struct ifdatareq *ifdr; 2905 2906 switch (cmd) { 2907 case SIOCSIFCAP: 2908 ifcr = data; 2909 if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0) 2910 return EINVAL; 2911 2912 if (ifcr->ifcr_capenable == ifp->if_capenable) 2913 return 0; 2914 2915 ifp->if_capenable = ifcr->ifcr_capenable; 2916 2917 /* Pre-compute the checksum flags mask. */ 2918 ifp->if_csum_flags_tx = 0; 2919 ifp->if_csum_flags_rx = 0; 2920 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) 2921 ifp->if_csum_flags_tx |= M_CSUM_IPv4; 2922 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) 2923 ifp->if_csum_flags_rx |= M_CSUM_IPv4; 2924 2925 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) 2926 ifp->if_csum_flags_tx |= M_CSUM_TCPv4; 2927 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) 2928 ifp->if_csum_flags_rx |= M_CSUM_TCPv4; 2929 2930 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) 2931 ifp->if_csum_flags_tx |= M_CSUM_UDPv4; 2932 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) 2933 ifp->if_csum_flags_rx |= M_CSUM_UDPv4; 2934 2935 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) 2936 ifp->if_csum_flags_tx |= M_CSUM_TCPv6; 2937 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) 2938 ifp->if_csum_flags_rx |= M_CSUM_TCPv6; 2939 2940 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) 2941 ifp->if_csum_flags_tx |= M_CSUM_UDPv6; 2942 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) 2943 ifp->if_csum_flags_rx |= M_CSUM_UDPv6; 2944 2945 if (ifp->if_capenable & IFCAP_TSOv4) 2946 ifp->if_csum_flags_tx |= M_CSUM_TSOv4; 2947 if (ifp->if_capenable & IFCAP_TSOv6) 2948 ifp->if_csum_flags_tx |= M_CSUM_TSOv6; 2949 2950 #if NBRIDGE > 0 2951 if (ifp->if_bridge != NULL) 2952 bridge_calc_csum_flags(ifp->if_bridge); 2953 #endif 2954 2955 if (ifp->if_flags & IFF_UP) 2956 return ENETRESET; 2957 return 0; 2958 case SIOCSIFFLAGS: 2959 ifr = data; 2960 /* 2961 * If if_is_mpsafe(ifp), KERNEL_LOCK isn't held here, but if_up 2962 * and if_down aren't MP-safe yet, so we must hold the lock. 2963 */ 2964 KERNEL_LOCK_IF_IFP_MPSAFE(ifp); 2965 if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) { 2966 s = splsoftnet(); 2967 if_down_locked(ifp); 2968 splx(s); 2969 } 2970 if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) { 2971 s = splsoftnet(); 2972 if_up_locked(ifp); 2973 splx(s); 2974 } 2975 KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp); 2976 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 2977 (ifr->ifr_flags &~ IFF_CANTCHANGE); 2978 break; 2979 case SIOCGIFFLAGS: 2980 ifr = data; 2981 ifr->ifr_flags = ifp->if_flags; 2982 break; 2983 2984 case SIOCGIFMETRIC: 2985 ifr = data; 2986 ifr->ifr_metric = ifp->if_metric; 2987 break; 2988 2989 case SIOCGIFMTU: 2990 ifr = data; 2991 ifr->ifr_mtu = ifp->if_mtu; 2992 break; 2993 2994 case SIOCGIFDLT: 2995 ifr = data; 2996 ifr->ifr_dlt = ifp->if_dlt; 2997 break; 2998 2999 case SIOCGIFCAP: 3000 ifcr = data; 3001 ifcr->ifcr_capabilities = ifp->if_capabilities; 3002 ifcr->ifcr_capenable = ifp->if_capenable; 3003 break; 3004 3005 case SIOCSIFMETRIC: 3006 ifr = data; 3007 ifp->if_metric = ifr->ifr_metric; 3008 break; 3009 3010 case SIOCGIFDATA: 3011 ifdr = data; 3012 ifdr->ifdr_data = ifp->if_data; 3013 break; 3014 3015 case SIOCGIFINDEX: 3016 ifr = data; 3017 ifr->ifr_index = ifp->if_index; 3018 break; 3019 3020 case SIOCZIFDATA: 3021 ifdr = data; 3022 ifdr->ifdr_data = ifp->if_data; 3023 /* 3024 * Assumes that the volatile counters that can be 3025 * zero'ed are at the end of if_data. 3026 */ 3027 memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) - 3028 offsetof(struct if_data, ifi_ipackets)); 3029 /* 3030 * The memset() clears to the bottm of if_data. In the area, 3031 * if_lastchange is included. Please be careful if new entry 3032 * will be added into if_data or rewite this. 3033 * 3034 * And also, update if_lastchnage. 3035 */ 3036 getnanotime(&ifp->if_lastchange); 3037 break; 3038 case SIOCSIFMTU: 3039 ifr = data; 3040 if (ifp->if_mtu == ifr->ifr_mtu) 3041 break; 3042 ifp->if_mtu = ifr->ifr_mtu; 3043 /* 3044 * If the link MTU changed, do network layer specific procedure. 3045 */ 3046 #ifdef INET6 3047 KERNEL_LOCK_UNLESS_NET_MPSAFE(); 3048 if (in6_present) 3049 nd6_setmtu(ifp); 3050 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 3051 #endif 3052 return ENETRESET; 3053 default: 3054 return ENOTTY; 3055 } 3056 return 0; 3057 } 3058 3059 int 3060 ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data, struct ifnet *ifp) 3061 { 3062 struct if_addrprefreq *ifap = (struct if_addrprefreq *)data; 3063 struct ifaddr *ifa; 3064 const struct sockaddr *any, *sa; 3065 union { 3066 struct sockaddr sa; 3067 struct sockaddr_storage ss; 3068 } u, v; 3069 int s, error = 0; 3070 3071 switch (cmd) { 3072 case SIOCSIFADDRPREF: 3073 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE, 3074 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd, 3075 NULL) != 0) 3076 return EPERM; 3077 case SIOCGIFADDRPREF: 3078 break; 3079 default: 3080 return EOPNOTSUPP; 3081 } 3082 3083 /* sanity checks */ 3084 if (data == NULL || ifp == NULL) { 3085 panic("invalid argument to %s", __func__); 3086 /*NOTREACHED*/ 3087 } 3088 3089 /* address must be specified on ADD and DELETE */ 3090 sa = sstocsa(&ifap->ifap_addr); 3091 if (sa->sa_family != sofamily(so)) 3092 return EINVAL; 3093 if ((any = sockaddr_any(sa)) == NULL || sa->sa_len != any->sa_len) 3094 return EINVAL; 3095 3096 sockaddr_externalize(&v.sa, sizeof(v.ss), sa); 3097 3098 s = pserialize_read_enter(); 3099 IFADDR_READER_FOREACH(ifa, ifp) { 3100 if (ifa->ifa_addr->sa_family != sa->sa_family) 3101 continue; 3102 sockaddr_externalize(&u.sa, sizeof(u.ss), ifa->ifa_addr); 3103 if (sockaddr_cmp(&u.sa, &v.sa) == 0) 3104 break; 3105 } 3106 if (ifa == NULL) { 3107 error = EADDRNOTAVAIL; 3108 goto out; 3109 } 3110 3111 switch (cmd) { 3112 case SIOCSIFADDRPREF: 3113 ifa->ifa_preference = ifap->ifap_preference; 3114 goto out; 3115 case SIOCGIFADDRPREF: 3116 /* fill in the if_laddrreq structure */ 3117 (void)sockaddr_copy(sstosa(&ifap->ifap_addr), 3118 sizeof(ifap->ifap_addr), ifa->ifa_addr); 3119 ifap->ifap_preference = ifa->ifa_preference; 3120 goto out; 3121 default: 3122 error = EOPNOTSUPP; 3123 } 3124 out: 3125 pserialize_read_exit(s); 3126 return error; 3127 } 3128 3129 /* 3130 * Interface ioctls. 3131 */ 3132 static int 3133 doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l) 3134 { 3135 struct ifnet *ifp; 3136 struct ifreq *ifr; 3137 int error = 0; 3138 u_long ocmd = cmd; 3139 short oif_flags; 3140 struct ifreq ifrb; 3141 struct oifreq *oifr = NULL; 3142 int r; 3143 struct psref psref; 3144 int bound; 3145 bool do_if43_post = false; 3146 bool do_ifm80_post = false; 3147 3148 switch (cmd) { 3149 case SIOCGIFCONF: 3150 return ifconf(cmd, data); 3151 case SIOCINITIFADDR: 3152 return EPERM; 3153 default: 3154 MODULE_HOOK_CALL(uipc_syscalls_40_hook, (cmd, data), enosys(), 3155 error); 3156 if (error != ENOSYS) 3157 return error; 3158 MODULE_HOOK_CALL(uipc_syscalls_50_hook, (l, cmd, data), 3159 enosys(), error); 3160 if (error != ENOSYS) 3161 return error; 3162 error = 0; 3163 break; 3164 } 3165 3166 ifr = data; 3167 /* Pre-conversion */ 3168 MODULE_HOOK_CALL(if_cvtcmd_43_hook, (&cmd, ocmd), enosys(), error); 3169 if (cmd != ocmd) { 3170 oifr = data; 3171 data = ifr = &ifrb; 3172 IFREQO2N_43(oifr, ifr); 3173 do_if43_post = true; 3174 } 3175 MODULE_HOOK_CALL(ifmedia_80_pre_hook, (ifr, &cmd, &do_ifm80_post), 3176 enosys(), error); 3177 3178 switch (cmd) { 3179 case SIOCIFCREATE: 3180 case SIOCIFDESTROY: 3181 bound = curlwp_bind(); 3182 if (l != NULL) { 3183 ifp = if_get(ifr->ifr_name, &psref); 3184 error = kauth_authorize_network(l->l_cred, 3185 KAUTH_NETWORK_INTERFACE, 3186 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, 3187 (void *)cmd, NULL); 3188 if (ifp != NULL) 3189 if_put(ifp, &psref); 3190 if (error != 0) { 3191 curlwp_bindx(bound); 3192 return error; 3193 } 3194 } 3195 KERNEL_LOCK_UNLESS_NET_MPSAFE(); 3196 mutex_enter(&if_clone_mtx); 3197 r = (cmd == SIOCIFCREATE) ? 3198 if_clone_create(ifr->ifr_name) : 3199 if_clone_destroy(ifr->ifr_name); 3200 mutex_exit(&if_clone_mtx); 3201 KERNEL_UNLOCK_UNLESS_NET_MPSAFE(); 3202 curlwp_bindx(bound); 3203 return r; 3204 3205 case SIOCIFGCLONERS: 3206 { 3207 struct if_clonereq *req = (struct if_clonereq *)data; 3208 return if_clone_list(req->ifcr_count, req->ifcr_buffer, 3209 &req->ifcr_total); 3210 } 3211 } 3212 3213 bound = curlwp_bind(); 3214 ifp = if_get(ifr->ifr_name, &psref); 3215 if (ifp == NULL) { 3216 curlwp_bindx(bound); 3217 return ENXIO; 3218 } 3219 3220 switch (cmd) { 3221 case SIOCALIFADDR: 3222 case SIOCDLIFADDR: 3223 case SIOCSIFADDRPREF: 3224 case SIOCSIFFLAGS: 3225 case SIOCSIFCAP: 3226 case SIOCSIFMETRIC: 3227 case SIOCZIFDATA: 3228 case SIOCSIFMTU: 3229 case SIOCSIFPHYADDR: 3230 case SIOCDIFPHYADDR: 3231 #ifdef INET6 3232 case SIOCSIFPHYADDR_IN6: 3233 #endif 3234 case SIOCSLIFPHYADDR: 3235 case SIOCADDMULTI: 3236 case SIOCDELMULTI: 3237 case SIOCSETHERCAP: 3238 case SIOCSIFMEDIA: 3239 case SIOCSDRVSPEC: 3240 case SIOCG80211: 3241 case SIOCS80211: 3242 case SIOCS80211NWID: 3243 case SIOCS80211NWKEY: 3244 case SIOCS80211POWER: 3245 case SIOCS80211BSSID: 3246 case SIOCS80211CHANNEL: 3247 case SIOCSLINKSTR: 3248 if (l != NULL) { 3249 error = kauth_authorize_network(l->l_cred, 3250 KAUTH_NETWORK_INTERFACE, 3251 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, 3252 (void *)cmd, NULL); 3253 if (error != 0) 3254 goto out; 3255 } 3256 } 3257 3258 oif_flags = ifp->if_flags; 3259 3260 KERNEL_LOCK_UNLESS_IFP_MPSAFE(ifp); 3261 IFNET_LOCK(ifp); 3262 3263 error = (*ifp->if_ioctl)(ifp, cmd, data); 3264 if (error != ENOTTY) 3265 ; 3266 else if (so->so_proto == NULL) 3267 error = EOPNOTSUPP; 3268 else { 3269 KERNEL_LOCK_IF_IFP_MPSAFE(ifp); 3270 MODULE_HOOK_CALL(if_ifioctl_43_hook, 3271 (so, ocmd, cmd, data, l), enosys(), error); 3272 if (error == ENOSYS) 3273 error = (*so->so_proto->pr_usrreqs->pr_ioctl)(so, 3274 cmd, data, ifp); 3275 KERNEL_UNLOCK_IF_IFP_MPSAFE(ifp); 3276 } 3277 3278 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) { 3279 if ((ifp->if_flags & IFF_UP) != 0) { 3280 int s = splsoftnet(); 3281 if_up_locked(ifp); 3282 splx(s); 3283 } 3284 } 3285 3286 /* Post-conversion */ 3287 if (do_ifm80_post && (error == 0)) 3288 MODULE_HOOK_CALL(ifmedia_80_post_hook, (ifr, cmd), 3289 enosys(), error); 3290 if (do_if43_post) 3291 IFREQN2O_43(oifr, ifr); 3292 3293 IFNET_UNLOCK(ifp); 3294 KERNEL_UNLOCK_UNLESS_IFP_MPSAFE(ifp); 3295 out: 3296 if_put(ifp, &psref); 3297 curlwp_bindx(bound); 3298 return error; 3299 } 3300 3301 /* 3302 * Return interface configuration 3303 * of system. List may be used 3304 * in later ioctl's (above) to get 3305 * other information. 3306 * 3307 * Each record is a struct ifreq. Before the addition of 3308 * sockaddr_storage, the API rule was that sockaddr flavors that did 3309 * not fit would extend beyond the struct ifreq, with the next struct 3310 * ifreq starting sa_len beyond the struct sockaddr. Because the 3311 * union in struct ifreq includes struct sockaddr_storage, every kind 3312 * of sockaddr must fit. Thus, there are no longer any overlength 3313 * records. 3314 * 3315 * Records are added to the user buffer if they fit, and ifc_len is 3316 * adjusted to the length that was written. Thus, the user is only 3317 * assured of getting the complete list if ifc_len on return is at 3318 * least sizeof(struct ifreq) less than it was on entry. 3319 * 3320 * If the user buffer pointer is NULL, this routine copies no data and 3321 * returns the amount of space that would be needed. 3322 * 3323 * Invariants: 3324 * ifrp points to the next part of the user's buffer to be used. If 3325 * ifrp != NULL, space holds the number of bytes remaining that we may 3326 * write at ifrp. Otherwise, space holds the number of bytes that 3327 * would have been written had there been adequate space. 3328 */ 3329 /*ARGSUSED*/ 3330 static int 3331 ifconf(u_long cmd, void *data) 3332 { 3333 struct ifconf *ifc = (struct ifconf *)data; 3334 struct ifnet *ifp; 3335 struct ifaddr *ifa; 3336 struct ifreq ifr, *ifrp = NULL; 3337 int space = 0, error = 0; 3338 const int sz = (int)sizeof(struct ifreq); 3339 const bool docopy = ifc->ifc_req != NULL; 3340 int s; 3341 int bound; 3342 struct psref psref; 3343 3344 memset(&ifr, 0, sizeof(ifr)); 3345 if (docopy) { 3346 space = ifc->ifc_len; 3347 ifrp = ifc->ifc_req; 3348 } 3349 3350 bound = curlwp_bind(); 3351 s = pserialize_read_enter(); 3352 IFNET_READER_FOREACH(ifp) { 3353 psref_acquire(&psref, &ifp->if_psref, ifnet_psref_class); 3354 pserialize_read_exit(s); 3355 3356 (void)strncpy(ifr.ifr_name, ifp->if_xname, 3357 sizeof(ifr.ifr_name)); 3358 if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0') { 3359 error = ENAMETOOLONG; 3360 goto release_exit; 3361 } 3362 if (IFADDR_READER_EMPTY(ifp)) { 3363 /* Interface with no addresses - send zero sockaddr. */ 3364 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr)); 3365 if (!docopy) { 3366 space += sz; 3367 goto next; 3368 } 3369 if (space >= sz) { 3370 error = copyout(&ifr, ifrp, sz); 3371 if (error != 0) 3372 goto release_exit; 3373 ifrp++; 3374 space -= sz; 3375 } 3376 } 3377 3378 s = pserialize_read_enter(); 3379 IFADDR_READER_FOREACH(ifa, ifp) { 3380 struct sockaddr *sa = ifa->ifa_addr; 3381 /* all sockaddrs must fit in sockaddr_storage */ 3382 KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru)); 3383 3384 if (!docopy) { 3385 space += sz; 3386 continue; 3387 } 3388 memcpy(&ifr.ifr_space, sa, sa->sa_len); 3389 pserialize_read_exit(s); 3390 3391 if (space >= sz) { 3392 error = copyout(&ifr, ifrp, sz); 3393 if (error != 0) 3394 goto release_exit; 3395 ifrp++; space -= sz; 3396 } 3397 s = pserialize_read_enter(); 3398 } 3399 pserialize_read_exit(s); 3400 3401 next: 3402 s = pserialize_read_enter(); 3403 psref_release(&psref, &ifp->if_psref, ifnet_psref_class); 3404 } 3405 pserialize_read_exit(s); 3406 curlwp_bindx(bound); 3407 3408 if (docopy) { 3409 KASSERT(0 <= space && space <= ifc->ifc_len); 3410 ifc->ifc_len -= space; 3411 } else { 3412 KASSERT(space >= 0); 3413 ifc->ifc_len = space; 3414 } 3415 return (0); 3416 3417 release_exit: 3418 psref_release(&psref, &ifp->if_psref, ifnet_psref_class); 3419 curlwp_bindx(bound); 3420 return error; 3421 } 3422 3423 int 3424 ifreq_setaddr(u_long cmd, struct ifreq *ifr, const struct sockaddr *sa) 3425 { 3426 uint8_t len = sizeof(ifr->ifr_ifru.ifru_space); 3427 struct ifreq ifrb; 3428 struct oifreq *oifr = NULL; 3429 u_long ocmd = cmd; 3430 int hook; 3431 3432 MODULE_HOOK_CALL(if_cvtcmd_43_hook, (&cmd, ocmd), enosys(), hook); 3433 if (hook != ENOSYS) { 3434 if (cmd != ocmd) { 3435 oifr = (struct oifreq *)(void *)ifr; 3436 ifr = &ifrb; 3437 IFREQO2N_43(oifr, ifr); 3438 len = sizeof(oifr->ifr_addr); 3439 } 3440 } 3441 3442 if (len < sa->sa_len) 3443 return EFBIG; 3444 3445 memset(&ifr->ifr_addr, 0, len); 3446 sockaddr_copy(&ifr->ifr_addr, len, sa); 3447 3448 if (cmd != ocmd) 3449 IFREQN2O_43(oifr, ifr); 3450 return 0; 3451 } 3452 3453 /* 3454 * wrapper function for the drivers which doesn't have if_transmit(). 3455 */ 3456 static int 3457 if_transmit(struct ifnet *ifp, struct mbuf *m) 3458 { 3459 int s, error; 3460 size_t pktlen = m->m_pkthdr.len; 3461 bool mcast = (m->m_flags & M_MCAST) != 0; 3462 3463 s = splnet(); 3464 3465 IFQ_ENQUEUE(&ifp->if_snd, m, error); 3466 if (error != 0) { 3467 /* mbuf is already freed */ 3468 goto out; 3469 } 3470 3471 ifp->if_obytes += pktlen; 3472 if (mcast) 3473 ifp->if_omcasts++; 3474 3475 if ((ifp->if_flags & IFF_OACTIVE) == 0) 3476 if_start_lock(ifp); 3477 out: 3478 splx(s); 3479 3480 return error; 3481 } 3482 3483 int 3484 if_transmit_lock(struct ifnet *ifp, struct mbuf *m) 3485 { 3486 int error; 3487 3488 #ifdef ALTQ 3489 KERNEL_LOCK(1, NULL); 3490 if (ALTQ_IS_ENABLED(&ifp->if_snd)) { 3491 error = if_transmit(ifp, m); 3492 KERNEL_UNLOCK_ONE(NULL); 3493 } else { 3494 KERNEL_UNLOCK_ONE(NULL); 3495 error = (*ifp->if_transmit)(ifp, m); 3496 /* mbuf is alredy freed */ 3497 } 3498 #else /* !ALTQ */ 3499 error = (*ifp->if_transmit)(ifp, m); 3500 /* mbuf is alredy freed */ 3501 #endif /* !ALTQ */ 3502 3503 return error; 3504 } 3505 3506 /* 3507 * Queue message on interface, and start output if interface 3508 * not yet active. 3509 */ 3510 int 3511 ifq_enqueue(struct ifnet *ifp, struct mbuf *m) 3512 { 3513 3514 return if_transmit_lock(ifp, m); 3515 } 3516 3517 /* 3518 * Queue message on interface, possibly using a second fast queue 3519 */ 3520 int 3521 ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m) 3522 { 3523 int error = 0; 3524 3525 if (ifq != NULL 3526 #ifdef ALTQ 3527 && ALTQ_IS_ENABLED(&ifp->if_snd) == 0 3528 #endif 3529 ) { 3530 if (IF_QFULL(ifq)) { 3531 IF_DROP(&ifp->if_snd); 3532 m_freem(m); 3533 if (error == 0) 3534 error = ENOBUFS; 3535 } else 3536 IF_ENQUEUE(ifq, m); 3537 } else 3538 IFQ_ENQUEUE(&ifp->if_snd, m, error); 3539 if (error != 0) { 3540 ++ifp->if_oerrors; 3541 return error; 3542 } 3543 return 0; 3544 } 3545 3546 int 3547 if_addr_init(ifnet_t *ifp, struct ifaddr *ifa, const bool src) 3548 { 3549 int rc; 3550 3551 KASSERT(IFNET_LOCKED(ifp)); 3552 if (ifp->if_initaddr != NULL) 3553 rc = (*ifp->if_initaddr)(ifp, ifa, src); 3554 else if (src || 3555 (rc = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, ifa)) == ENOTTY) 3556 rc = (*ifp->if_ioctl)(ifp, SIOCINITIFADDR, ifa); 3557 3558 return rc; 3559 } 3560 3561 int 3562 if_do_dad(struct ifnet *ifp) 3563 { 3564 if ((ifp->if_flags & IFF_LOOPBACK) != 0) 3565 return 0; 3566 3567 switch (ifp->if_type) { 3568 case IFT_FAITH: 3569 /* 3570 * These interfaces do not have the IFF_LOOPBACK flag, 3571 * but loop packets back. We do not have to do DAD on such 3572 * interfaces. We should even omit it, because loop-backed 3573 * responses would confuse the DAD procedure. 3574 */ 3575 return 0; 3576 default: 3577 /* 3578 * Our DAD routine requires the interface up and running. 3579 * However, some interfaces can be up before the RUNNING 3580 * status. Additionaly, users may try to assign addresses 3581 * before the interface becomes up (or running). 3582 * We simply skip DAD in such a case as a work around. 3583 * XXX: we should rather mark "tentative" on such addresses, 3584 * and do DAD after the interface becomes ready. 3585 */ 3586 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != 3587 (IFF_UP | IFF_RUNNING)) 3588 return 0; 3589 3590 return 1; 3591 } 3592 } 3593 3594 int 3595 if_flags_set(ifnet_t *ifp, const short flags) 3596 { 3597 int rc; 3598 3599 KASSERT(IFNET_LOCKED(ifp)); 3600 3601 if (ifp->if_setflags != NULL) 3602 rc = (*ifp->if_setflags)(ifp, flags); 3603 else { 3604 short cantflags, chgdflags; 3605 struct ifreq ifr; 3606 3607 chgdflags = ifp->if_flags ^ flags; 3608 cantflags = chgdflags & IFF_CANTCHANGE; 3609 3610 if (cantflags != 0) 3611 ifp->if_flags ^= cantflags; 3612 3613 /* Traditionally, we do not call if_ioctl after 3614 * setting/clearing only IFF_PROMISC if the interface 3615 * isn't IFF_UP. Uphold that tradition. 3616 */ 3617 if (chgdflags == IFF_PROMISC && (ifp->if_flags & IFF_UP) == 0) 3618 return 0; 3619 3620 memset(&ifr, 0, sizeof(ifr)); 3621 3622 ifr.ifr_flags = flags & ~IFF_CANTCHANGE; 3623 rc = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, &ifr); 3624 3625 if (rc != 0 && cantflags != 0) 3626 ifp->if_flags ^= cantflags; 3627 } 3628 3629 return rc; 3630 } 3631 3632 int 3633 if_mcast_op(ifnet_t *ifp, const unsigned long cmd, const struct sockaddr *sa) 3634 { 3635 int rc; 3636 struct ifreq ifr; 3637 3638 if (ifp->if_mcastop != NULL) 3639 rc = (*ifp->if_mcastop)(ifp, cmd, sa); 3640 else { 3641 ifreq_setaddr(cmd, &ifr, sa); 3642 rc = (*ifp->if_ioctl)(ifp, cmd, &ifr); 3643 } 3644 3645 return rc; 3646 } 3647 3648 static void 3649 sysctl_sndq_setup(struct sysctllog **clog, const char *ifname, 3650 struct ifaltq *ifq) 3651 { 3652 const struct sysctlnode *cnode, *rnode; 3653 3654 if (sysctl_createv(clog, 0, NULL, &rnode, 3655 CTLFLAG_PERMANENT, 3656 CTLTYPE_NODE, "interfaces", 3657 SYSCTL_DESCR("Per-interface controls"), 3658 NULL, 0, NULL, 0, 3659 CTL_NET, CTL_CREATE, CTL_EOL) != 0) 3660 goto bad; 3661 3662 if (sysctl_createv(clog, 0, &rnode, &rnode, 3663 CTLFLAG_PERMANENT, 3664 CTLTYPE_NODE, ifname, 3665 SYSCTL_DESCR("Interface controls"), 3666 NULL, 0, NULL, 0, 3667 CTL_CREATE, CTL_EOL) != 0) 3668 goto bad; 3669 3670 if (sysctl_createv(clog, 0, &rnode, &rnode, 3671 CTLFLAG_PERMANENT, 3672 CTLTYPE_NODE, "sndq", 3673 SYSCTL_DESCR("Interface output queue controls"), 3674 NULL, 0, NULL, 0, 3675 CTL_CREATE, CTL_EOL) != 0) 3676 goto bad; 3677 3678 if (sysctl_createv(clog, 0, &rnode, &cnode, 3679 CTLFLAG_PERMANENT, 3680 CTLTYPE_INT, "len", 3681 SYSCTL_DESCR("Current output queue length"), 3682 NULL, 0, &ifq->ifq_len, 0, 3683 CTL_CREATE, CTL_EOL) != 0) 3684 goto bad; 3685 3686 if (sysctl_createv(clog, 0, &rnode, &cnode, 3687 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 3688 CTLTYPE_INT, "maxlen", 3689 SYSCTL_DESCR("Maximum allowed output queue length"), 3690 NULL, 0, &ifq->ifq_maxlen, 0, 3691 CTL_CREATE, CTL_EOL) != 0) 3692 goto bad; 3693 3694 if (sysctl_createv(clog, 0, &rnode, &cnode, 3695 CTLFLAG_PERMANENT, 3696 CTLTYPE_INT, "drops", 3697 SYSCTL_DESCR("Packets dropped due to full output queue"), 3698 NULL, 0, &ifq->ifq_drops, 0, 3699 CTL_CREATE, CTL_EOL) != 0) 3700 goto bad; 3701 3702 return; 3703 bad: 3704 printf("%s: could not attach sysctl nodes\n", ifname); 3705 return; 3706 } 3707 3708 #if defined(INET) || defined(INET6) 3709 3710 #define SYSCTL_NET_PKTQ(q, cn, c) \ 3711 static int \ 3712 sysctl_net_##q##_##cn(SYSCTLFN_ARGS) \ 3713 { \ 3714 return sysctl_pktq_count(SYSCTLFN_CALL(rnode), q, c); \ 3715 } 3716 3717 #if defined(INET) 3718 static int 3719 sysctl_net_ip_pktq_maxlen(SYSCTLFN_ARGS) 3720 { 3721 return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip_pktq); 3722 } 3723 SYSCTL_NET_PKTQ(ip_pktq, items, PKTQ_NITEMS) 3724 SYSCTL_NET_PKTQ(ip_pktq, drops, PKTQ_DROPS) 3725 #endif 3726 3727 #if defined(INET6) 3728 static int 3729 sysctl_net_ip6_pktq_maxlen(SYSCTLFN_ARGS) 3730 { 3731 return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip6_pktq); 3732 } 3733 SYSCTL_NET_PKTQ(ip6_pktq, items, PKTQ_NITEMS) 3734 SYSCTL_NET_PKTQ(ip6_pktq, drops, PKTQ_DROPS) 3735 #endif 3736 3737 static void 3738 sysctl_net_pktq_setup(struct sysctllog **clog, int pf) 3739 { 3740 sysctlfn len_func = NULL, maxlen_func = NULL, drops_func = NULL; 3741 const char *pfname = NULL, *ipname = NULL; 3742 int ipn = 0, qid = 0; 3743 3744 switch (pf) { 3745 #if defined(INET) 3746 case PF_INET: 3747 len_func = sysctl_net_ip_pktq_items; 3748 maxlen_func = sysctl_net_ip_pktq_maxlen; 3749 drops_func = sysctl_net_ip_pktq_drops; 3750 pfname = "inet", ipn = IPPROTO_IP; 3751 ipname = "ip", qid = IPCTL_IFQ; 3752 break; 3753 #endif 3754 #if defined(INET6) 3755 case PF_INET6: 3756 len_func = sysctl_net_ip6_pktq_items; 3757 maxlen_func = sysctl_net_ip6_pktq_maxlen; 3758 drops_func = sysctl_net_ip6_pktq_drops; 3759 pfname = "inet6", ipn = IPPROTO_IPV6; 3760 ipname = "ip6", qid = IPV6CTL_IFQ; 3761 break; 3762 #endif 3763 default: 3764 KASSERT(false); 3765 } 3766 3767 sysctl_createv(clog, 0, NULL, NULL, 3768 CTLFLAG_PERMANENT, 3769 CTLTYPE_NODE, pfname, NULL, 3770 NULL, 0, NULL, 0, 3771 CTL_NET, pf, CTL_EOL); 3772 sysctl_createv(clog, 0, NULL, NULL, 3773 CTLFLAG_PERMANENT, 3774 CTLTYPE_NODE, ipname, NULL, 3775 NULL, 0, NULL, 0, 3776 CTL_NET, pf, ipn, CTL_EOL); 3777 sysctl_createv(clog, 0, NULL, NULL, 3778 CTLFLAG_PERMANENT, 3779 CTLTYPE_NODE, "ifq", 3780 SYSCTL_DESCR("Protocol input queue controls"), 3781 NULL, 0, NULL, 0, 3782 CTL_NET, pf, ipn, qid, CTL_EOL); 3783 3784 sysctl_createv(clog, 0, NULL, NULL, 3785 CTLFLAG_PERMANENT, 3786 CTLTYPE_QUAD, "len", 3787 SYSCTL_DESCR("Current input queue length"), 3788 len_func, 0, NULL, 0, 3789 CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL); 3790 sysctl_createv(clog, 0, NULL, NULL, 3791 CTLFLAG_PERMANENT | CTLFLAG_READWRITE, 3792 CTLTYPE_INT, "maxlen", 3793 SYSCTL_DESCR("Maximum allowed input queue length"), 3794 maxlen_func, 0, NULL, 0, 3795 CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL); 3796 sysctl_createv(clog, 0, NULL, NULL, 3797 CTLFLAG_PERMANENT, 3798 CTLTYPE_QUAD, "drops", 3799 SYSCTL_DESCR("Packets dropped due to full input queue"), 3800 drops_func, 0, NULL, 0, 3801 CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL); 3802 } 3803 #endif /* INET || INET6 */ 3804 3805 static int 3806 if_sdl_sysctl(SYSCTLFN_ARGS) 3807 { 3808 struct ifnet *ifp; 3809 const struct sockaddr_dl *sdl; 3810 struct psref psref; 3811 int error = 0; 3812 int bound; 3813 3814 if (namelen != 1) 3815 return EINVAL; 3816 3817 bound = curlwp_bind(); 3818 ifp = if_get_byindex(name[0], &psref); 3819 if (ifp == NULL) { 3820 error = ENODEV; 3821 goto out0; 3822 } 3823 3824 sdl = ifp->if_sadl; 3825 if (sdl == NULL) { 3826 *oldlenp = 0; 3827 goto out1; 3828 } 3829 3830 if (oldp == NULL) { 3831 *oldlenp = sdl->sdl_alen; 3832 goto out1; 3833 } 3834 3835 if (*oldlenp >= sdl->sdl_alen) 3836 *oldlenp = sdl->sdl_alen; 3837 error = sysctl_copyout(l, &sdl->sdl_data[sdl->sdl_nlen], oldp, *oldlenp); 3838 out1: 3839 if_put(ifp, &psref); 3840 out0: 3841 curlwp_bindx(bound); 3842 return error; 3843 } 3844 3845 static void 3846 if_sysctl_setup(struct sysctllog **clog) 3847 { 3848 const struct sysctlnode *rnode = NULL; 3849 3850 sysctl_createv(clog, 0, NULL, &rnode, 3851 CTLFLAG_PERMANENT, 3852 CTLTYPE_NODE, "sdl", 3853 SYSCTL_DESCR("Get active link-layer address"), 3854 if_sdl_sysctl, 0, NULL, 0, 3855 CTL_NET, CTL_CREATE, CTL_EOL); 3856 3857 #if defined(INET) 3858 sysctl_net_pktq_setup(NULL, PF_INET); 3859 #endif 3860 #ifdef INET6 3861 if (in6_present) 3862 sysctl_net_pktq_setup(NULL, PF_INET6); 3863 #endif 3864 } 3865