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