1 /* $NetBSD: if.c,v 1.322 2016/01/21 15:41:29 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 1999, 2000, 2001, 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by William Studenmund and Jason R. Thorpe. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 34 * All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. Neither the name of the project nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 */ 60 61 /* 62 * Copyright (c) 1980, 1986, 1993 63 * The Regents of the University of California. All rights reserved. 64 * 65 * Redistribution and use in source and binary forms, with or without 66 * modification, are permitted provided that the following conditions 67 * are met: 68 * 1. Redistributions of source code must retain the above copyright 69 * notice, this list of conditions and the following disclaimer. 70 * 2. Redistributions in binary form must reproduce the above copyright 71 * notice, this list of conditions and the following disclaimer in the 72 * documentation and/or other materials provided with the distribution. 73 * 3. Neither the name of the University nor the names of its contributors 74 * may be used to endorse or promote products derived from this software 75 * without specific prior written permission. 76 * 77 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 78 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 79 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 80 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 81 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 82 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 83 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 84 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 85 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 86 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 87 * SUCH DAMAGE. 88 * 89 * @(#)if.c 8.5 (Berkeley) 1/9/95 90 */ 91 92 #include <sys/cdefs.h> 93 __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.322 2016/01/21 15:41:29 riastradh Exp $"); 94 95 #if defined(_KERNEL_OPT) 96 #include "opt_inet.h" 97 98 #include "opt_atalk.h" 99 #include "opt_natm.h" 100 #include "opt_wlan.h" 101 #include "opt_net_mpsafe.h" 102 #endif 103 104 #include <sys/param.h> 105 #include <sys/mbuf.h> 106 #include <sys/systm.h> 107 #include <sys/callout.h> 108 #include <sys/proc.h> 109 #include <sys/socket.h> 110 #include <sys/socketvar.h> 111 #include <sys/domain.h> 112 #include <sys/protosw.h> 113 #include <sys/kernel.h> 114 #include <sys/ioctl.h> 115 #include <sys/sysctl.h> 116 #include <sys/syslog.h> 117 #include <sys/kauth.h> 118 #include <sys/kmem.h> 119 #include <sys/xcall.h> 120 121 #include <net/if.h> 122 #include <net/if_dl.h> 123 #include <net/if_ether.h> 124 #include <net/if_media.h> 125 #include <net80211/ieee80211.h> 126 #include <net80211/ieee80211_ioctl.h> 127 #include <net/if_types.h> 128 #include <net/radix.h> 129 #include <net/route.h> 130 #include <net/netisr.h> 131 #include <sys/module.h> 132 #ifdef NETATALK 133 #include <netatalk/at_extern.h> 134 #include <netatalk/at.h> 135 #endif 136 #include <net/pfil.h> 137 #include <netinet/in.h> 138 #include <netinet/in_var.h> 139 140 #ifdef INET6 141 #include <netinet6/in6_var.h> 142 #include <netinet6/nd6.h> 143 #endif 144 145 #include "ether.h" 146 #include "fddi.h" 147 #include "token.h" 148 149 #include "carp.h" 150 #if NCARP > 0 151 #include <netinet/ip_carp.h> 152 #endif 153 154 #include <compat/sys/sockio.h> 155 #include <compat/sys/socket.h> 156 157 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 158 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 159 160 /* 161 * Global list of interfaces. 162 */ 163 struct ifnet_head ifnet_list; 164 static ifnet_t ** ifindex2ifnet = NULL; 165 166 static u_int if_index = 1; 167 static size_t if_indexlim = 0; 168 static uint64_t index_gen; 169 static kmutex_t index_gen_mtx; 170 static kmutex_t if_clone_mtx; 171 172 struct ifnet *lo0ifp; 173 int ifqmaxlen = IFQ_MAXLEN; 174 175 static int if_rt_walktree(struct rtentry *, void *); 176 177 static struct if_clone *if_clone_lookup(const char *, int *); 178 179 static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners); 180 static int if_cloners_count; 181 182 /* Packet filtering hook for interfaces. */ 183 pfil_head_t * if_pfil; 184 185 static kauth_listener_t if_listener; 186 187 static int doifioctl(struct socket *, u_long, void *, struct lwp *); 188 static int ifioctl_attach(struct ifnet *); 189 static void ifioctl_detach(struct ifnet *); 190 static void ifnet_lock_enter(struct ifnet_lock *); 191 static void ifnet_lock_exit(struct ifnet_lock *); 192 static void if_detach_queues(struct ifnet *, struct ifqueue *); 193 static void sysctl_sndq_setup(struct sysctllog **, const char *, 194 struct ifaltq *); 195 static void if_slowtimo(void *); 196 static void if_free_sadl(struct ifnet *); 197 static void if_attachdomain1(struct ifnet *); 198 static int ifconf(u_long, void *); 199 static int if_clone_create(const char *); 200 static int if_clone_destroy(const char *); 201 202 #if defined(INET) || defined(INET6) 203 static void sysctl_net_pktq_setup(struct sysctllog **, int); 204 #endif 205 206 static int 207 if_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie, 208 void *arg0, void *arg1, void *arg2, void *arg3) 209 { 210 int result; 211 enum kauth_network_req req; 212 213 result = KAUTH_RESULT_DEFER; 214 req = (enum kauth_network_req)arg1; 215 216 if (action != KAUTH_NETWORK_INTERFACE) 217 return result; 218 219 if ((req == KAUTH_REQ_NETWORK_INTERFACE_GET) || 220 (req == KAUTH_REQ_NETWORK_INTERFACE_SET)) 221 result = KAUTH_RESULT_ALLOW; 222 223 return result; 224 } 225 226 /* 227 * Network interface utility routines. 228 * 229 * Routines with ifa_ifwith* names take sockaddr *'s as 230 * parameters. 231 */ 232 void 233 ifinit(void) 234 { 235 #if defined(INET) 236 sysctl_net_pktq_setup(NULL, PF_INET); 237 #endif 238 #ifdef INET6 239 if (in6_present) 240 sysctl_net_pktq_setup(NULL, PF_INET6); 241 #endif 242 243 if_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK, 244 if_listener_cb, NULL); 245 246 /* interfaces are available, inform socket code */ 247 ifioctl = doifioctl; 248 } 249 250 /* 251 * XXX Initialization before configure(). 252 * XXX hack to get pfil_add_hook working in autoconf. 253 */ 254 void 255 ifinit1(void) 256 { 257 mutex_init(&index_gen_mtx, MUTEX_DEFAULT, IPL_NONE); 258 mutex_init(&if_clone_mtx, MUTEX_DEFAULT, IPL_NONE); 259 TAILQ_INIT(&ifnet_list); 260 if_indexlim = 8; 261 262 if_pfil = pfil_head_create(PFIL_TYPE_IFNET, NULL); 263 KASSERT(if_pfil != NULL); 264 265 #if NETHER > 0 || NFDDI > 0 || defined(NETATALK) || NTOKEN > 0 || defined(WLAN) 266 etherinit(); 267 #endif 268 } 269 270 ifnet_t * 271 if_alloc(u_char type) 272 { 273 return kmem_zalloc(sizeof(ifnet_t), KM_SLEEP); 274 } 275 276 void 277 if_free(ifnet_t *ifp) 278 { 279 kmem_free(ifp, sizeof(ifnet_t)); 280 } 281 282 void 283 if_initname(struct ifnet *ifp, const char *name, int unit) 284 { 285 (void)snprintf(ifp->if_xname, sizeof(ifp->if_xname), 286 "%s%d", name, unit); 287 } 288 289 /* 290 * Null routines used while an interface is going away. These routines 291 * just return an error. 292 */ 293 294 int 295 if_nulloutput(struct ifnet *ifp, struct mbuf *m, 296 const struct sockaddr *so, struct rtentry *rt) 297 { 298 299 return ENXIO; 300 } 301 302 void 303 if_nullinput(struct ifnet *ifp, struct mbuf *m) 304 { 305 306 /* Nothing. */ 307 } 308 309 void 310 if_nullstart(struct ifnet *ifp) 311 { 312 313 /* Nothing. */ 314 } 315 316 int 317 if_nullioctl(struct ifnet *ifp, u_long cmd, void *data) 318 { 319 320 /* Wake ifioctl_detach(), who may wait for all threads to 321 * quit the critical section. 322 */ 323 cv_signal(&ifp->if_ioctl_lock->il_emptied); 324 return ENXIO; 325 } 326 327 int 328 if_nullinit(struct ifnet *ifp) 329 { 330 331 return ENXIO; 332 } 333 334 void 335 if_nullstop(struct ifnet *ifp, int disable) 336 { 337 338 /* Nothing. */ 339 } 340 341 void 342 if_nullslowtimo(struct ifnet *ifp) 343 { 344 345 /* Nothing. */ 346 } 347 348 void 349 if_nulldrain(struct ifnet *ifp) 350 { 351 352 /* Nothing. */ 353 } 354 355 void 356 if_set_sadl(struct ifnet *ifp, const void *lla, u_char addrlen, bool factory) 357 { 358 struct ifaddr *ifa; 359 struct sockaddr_dl *sdl; 360 361 ifp->if_addrlen = addrlen; 362 if_alloc_sadl(ifp); 363 ifa = ifp->if_dl; 364 sdl = satosdl(ifa->ifa_addr); 365 366 (void)sockaddr_dl_setaddr(sdl, sdl->sdl_len, lla, ifp->if_addrlen); 367 if (factory) { 368 ifp->if_hwdl = ifp->if_dl; 369 ifaref(ifp->if_hwdl); 370 } 371 /* TBD routing socket */ 372 } 373 374 struct ifaddr * 375 if_dl_create(const struct ifnet *ifp, const struct sockaddr_dl **sdlp) 376 { 377 unsigned socksize, ifasize; 378 int addrlen, namelen; 379 struct sockaddr_dl *mask, *sdl; 380 struct ifaddr *ifa; 381 382 namelen = strlen(ifp->if_xname); 383 addrlen = ifp->if_addrlen; 384 socksize = roundup(sockaddr_dl_measure(namelen, addrlen), sizeof(long)); 385 ifasize = sizeof(*ifa) + 2 * socksize; 386 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK|M_ZERO); 387 388 sdl = (struct sockaddr_dl *)(ifa + 1); 389 mask = (struct sockaddr_dl *)(socksize + (char *)sdl); 390 391 sockaddr_dl_init(sdl, socksize, ifp->if_index, ifp->if_type, 392 ifp->if_xname, namelen, NULL, addrlen); 393 mask->sdl_len = sockaddr_dl_measure(namelen, 0); 394 memset(&mask->sdl_data[0], 0xff, namelen); 395 ifa->ifa_rtrequest = link_rtrequest; 396 ifa->ifa_addr = (struct sockaddr *)sdl; 397 ifa->ifa_netmask = (struct sockaddr *)mask; 398 399 *sdlp = sdl; 400 401 return ifa; 402 } 403 404 static void 405 if_sadl_setrefs(struct ifnet *ifp, struct ifaddr *ifa) 406 { 407 const struct sockaddr_dl *sdl; 408 409 ifp->if_dl = ifa; 410 ifaref(ifa); 411 sdl = satosdl(ifa->ifa_addr); 412 ifp->if_sadl = sdl; 413 } 414 415 /* 416 * Allocate the link level name for the specified interface. This 417 * is an attachment helper. It must be called after ifp->if_addrlen 418 * is initialized, which may not be the case when if_attach() is 419 * called. 420 */ 421 void 422 if_alloc_sadl(struct ifnet *ifp) 423 { 424 struct ifaddr *ifa; 425 const struct sockaddr_dl *sdl; 426 427 /* 428 * If the interface already has a link name, release it 429 * now. This is useful for interfaces that can change 430 * link types, and thus switch link names often. 431 */ 432 if (ifp->if_sadl != NULL) 433 if_free_sadl(ifp); 434 435 ifa = if_dl_create(ifp, &sdl); 436 437 ifa_insert(ifp, ifa); 438 if_sadl_setrefs(ifp, ifa); 439 } 440 441 static void 442 if_deactivate_sadl(struct ifnet *ifp) 443 { 444 struct ifaddr *ifa; 445 446 KASSERT(ifp->if_dl != NULL); 447 448 ifa = ifp->if_dl; 449 450 ifp->if_sadl = NULL; 451 452 ifp->if_dl = NULL; 453 ifafree(ifa); 454 } 455 456 void 457 if_activate_sadl(struct ifnet *ifp, struct ifaddr *ifa, 458 const struct sockaddr_dl *sdl) 459 { 460 int s; 461 462 s = splnet(); 463 464 if_deactivate_sadl(ifp); 465 466 if_sadl_setrefs(ifp, ifa); 467 IFADDR_FOREACH(ifa, ifp) 468 rtinit(ifa, RTM_LLINFO_UPD, 0); 469 splx(s); 470 } 471 472 /* 473 * Free the link level name for the specified interface. This is 474 * a detach helper. This is called from if_detach(). 475 */ 476 static void 477 if_free_sadl(struct ifnet *ifp) 478 { 479 struct ifaddr *ifa; 480 int s; 481 482 ifa = ifp->if_dl; 483 if (ifa == NULL) { 484 KASSERT(ifp->if_sadl == NULL); 485 return; 486 } 487 488 KASSERT(ifp->if_sadl != NULL); 489 490 s = splnet(); 491 rtinit(ifa, RTM_DELETE, 0); 492 ifa_remove(ifp, ifa); 493 if_deactivate_sadl(ifp); 494 if (ifp->if_hwdl == ifa) { 495 ifafree(ifa); 496 ifp->if_hwdl = NULL; 497 } 498 splx(s); 499 } 500 501 static void 502 if_getindex(ifnet_t *ifp) 503 { 504 bool hitlimit = false; 505 506 mutex_enter(&index_gen_mtx); 507 ifp->if_index_gen = index_gen++; 508 mutex_exit(&index_gen_mtx); 509 510 ifp->if_index = if_index; 511 if (ifindex2ifnet == NULL) { 512 if_index++; 513 goto skip; 514 } 515 while (if_byindex(ifp->if_index)) { 516 /* 517 * If we hit USHRT_MAX, we skip back to 0 since 518 * there are a number of places where the value 519 * of if_index or if_index itself is compared 520 * to or stored in an unsigned short. By 521 * jumping back, we won't botch those assignments 522 * or comparisons. 523 */ 524 if (++if_index == 0) { 525 if_index = 1; 526 } else if (if_index == USHRT_MAX) { 527 /* 528 * However, if we have to jump back to 529 * zero *twice* without finding an empty 530 * slot in ifindex2ifnet[], then there 531 * there are too many (>65535) interfaces. 532 */ 533 if (hitlimit) { 534 panic("too many interfaces"); 535 } 536 hitlimit = true; 537 if_index = 1; 538 } 539 ifp->if_index = if_index; 540 } 541 skip: 542 /* 543 * ifindex2ifnet is indexed by if_index. Since if_index will 544 * grow dynamically, it should grow too. 545 */ 546 if (ifindex2ifnet == NULL || ifp->if_index >= if_indexlim) { 547 size_t m, n, oldlim; 548 void *q; 549 550 oldlim = if_indexlim; 551 while (ifp->if_index >= if_indexlim) 552 if_indexlim <<= 1; 553 554 /* grow ifindex2ifnet */ 555 m = oldlim * sizeof(struct ifnet *); 556 n = if_indexlim * sizeof(struct ifnet *); 557 q = malloc(n, M_IFADDR, M_WAITOK|M_ZERO); 558 if (ifindex2ifnet != NULL) { 559 memcpy(q, ifindex2ifnet, m); 560 free(ifindex2ifnet, M_IFADDR); 561 } 562 ifindex2ifnet = (struct ifnet **)q; 563 } 564 ifindex2ifnet[ifp->if_index] = ifp; 565 } 566 567 /* 568 * Initialize an interface and assign an index for it. 569 * 570 * It must be called prior to a device specific attach routine 571 * (e.g., ether_ifattach and ieee80211_ifattach) or if_alloc_sadl, 572 * and be followed by if_register: 573 * 574 * if_initialize(ifp); 575 * ether_ifattach(ifp, enaddr); 576 * if_register(ifp); 577 */ 578 void 579 if_initialize(ifnet_t *ifp) 580 { 581 KASSERT(if_indexlim > 0); 582 TAILQ_INIT(&ifp->if_addrlist); 583 584 /* 585 * Link level name is allocated later by a separate call to 586 * if_alloc_sadl(). 587 */ 588 589 if (ifp->if_snd.ifq_maxlen == 0) 590 ifp->if_snd.ifq_maxlen = ifqmaxlen; 591 592 ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */ 593 594 ifp->if_link_state = LINK_STATE_UNKNOWN; 595 596 ifp->if_capenable = 0; 597 ifp->if_csum_flags_tx = 0; 598 ifp->if_csum_flags_rx = 0; 599 600 #ifdef ALTQ 601 ifp->if_snd.altq_type = 0; 602 ifp->if_snd.altq_disc = NULL; 603 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE; 604 ifp->if_snd.altq_tbr = NULL; 605 ifp->if_snd.altq_ifp = ifp; 606 #endif 607 608 #ifdef NET_MPSAFE 609 ifp->if_snd.ifq_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET); 610 #else 611 ifp->if_snd.ifq_lock = NULL; 612 #endif 613 614 ifp->if_pfil = pfil_head_create(PFIL_TYPE_IFNET, ifp); 615 (void)pfil_run_hooks(if_pfil, 616 (struct mbuf **)PFIL_IFNET_ATTACH, ifp, PFIL_IFNET); 617 618 IF_AFDATA_LOCK_INIT(ifp); 619 620 if_getindex(ifp); 621 } 622 623 /* 624 * Register an interface to the list of "active" interfaces. 625 */ 626 void 627 if_register(ifnet_t *ifp) 628 { 629 if (ifioctl_attach(ifp) != 0) 630 panic("%s: ifioctl_attach() failed", __func__); 631 632 sysctl_sndq_setup(&ifp->if_sysctl_log, ifp->if_xname, &ifp->if_snd); 633 634 if (!STAILQ_EMPTY(&domains)) 635 if_attachdomain1(ifp); 636 637 /* Announce the interface. */ 638 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 639 640 if (ifp->if_slowtimo != NULL) { 641 ifp->if_slowtimo_ch = 642 kmem_zalloc(sizeof(*ifp->if_slowtimo_ch), KM_SLEEP); 643 callout_init(ifp->if_slowtimo_ch, 0); 644 callout_setfunc(ifp->if_slowtimo_ch, if_slowtimo, ifp); 645 if_slowtimo(ifp); 646 } 647 648 TAILQ_INSERT_TAIL(&ifnet_list, ifp, if_list); 649 } 650 651 /* 652 * Deprecated. Use if_initialize and if_register instead. 653 * See the above comment of if_initialize. 654 */ 655 void 656 if_attach(ifnet_t *ifp) 657 { 658 if_initialize(ifp); 659 if_register(ifp); 660 } 661 662 void 663 if_attachdomain(void) 664 { 665 struct ifnet *ifp; 666 int s; 667 668 s = splnet(); 669 IFNET_FOREACH(ifp) 670 if_attachdomain1(ifp); 671 splx(s); 672 } 673 674 static void 675 if_attachdomain1(struct ifnet *ifp) 676 { 677 struct domain *dp; 678 int s; 679 680 s = splnet(); 681 682 /* address family dependent data region */ 683 memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata)); 684 DOMAIN_FOREACH(dp) { 685 if (dp->dom_ifattach != NULL) 686 ifp->if_afdata[dp->dom_family] = 687 (*dp->dom_ifattach)(ifp); 688 } 689 690 splx(s); 691 } 692 693 /* 694 * Deactivate an interface. This points all of the procedure 695 * handles at error stubs. May be called from interrupt context. 696 */ 697 void 698 if_deactivate(struct ifnet *ifp) 699 { 700 int s; 701 702 s = splnet(); 703 704 ifp->if_output = if_nulloutput; 705 ifp->if_input = if_nullinput; 706 ifp->if_start = if_nullstart; 707 ifp->if_ioctl = if_nullioctl; 708 ifp->if_init = if_nullinit; 709 ifp->if_stop = if_nullstop; 710 ifp->if_slowtimo = if_nullslowtimo; 711 ifp->if_drain = if_nulldrain; 712 713 /* No more packets may be enqueued. */ 714 ifp->if_snd.ifq_maxlen = 0; 715 716 splx(s); 717 } 718 719 void 720 if_purgeaddrs(struct ifnet *ifp, int family, void (*purgeaddr)(struct ifaddr *)) 721 { 722 struct ifaddr *ifa, *nifa; 723 724 IFADDR_FOREACH_SAFE(ifa, ifp, nifa) { 725 if (ifa->ifa_addr->sa_family != family) 726 continue; 727 (*purgeaddr)(ifa); 728 } 729 } 730 731 /* 732 * Detach an interface from the list of "active" interfaces, 733 * freeing any resources as we go along. 734 * 735 * NOTE: This routine must be called with a valid thread context, 736 * as it may block. 737 */ 738 void 739 if_detach(struct ifnet *ifp) 740 { 741 struct socket so; 742 struct ifaddr *ifa; 743 #ifdef IFAREF_DEBUG 744 struct ifaddr *last_ifa = NULL; 745 #endif 746 struct domain *dp; 747 const struct protosw *pr; 748 int s, i, family, purged; 749 uint64_t xc; 750 751 /* 752 * XXX It's kind of lame that we have to have the 753 * XXX socket structure... 754 */ 755 memset(&so, 0, sizeof(so)); 756 757 s = splnet(); 758 759 ifindex2ifnet[ifp->if_index] = NULL; 760 TAILQ_REMOVE(&ifnet_list, ifp, if_list); 761 762 if (ifp->if_slowtimo != NULL) { 763 ifp->if_slowtimo = NULL; 764 callout_halt(ifp->if_slowtimo_ch, NULL); 765 callout_destroy(ifp->if_slowtimo_ch); 766 kmem_free(ifp->if_slowtimo_ch, sizeof(*ifp->if_slowtimo_ch)); 767 } 768 769 /* 770 * Do an if_down() to give protocols a chance to do something. 771 */ 772 if_down(ifp); 773 774 #ifdef ALTQ 775 if (ALTQ_IS_ENABLED(&ifp->if_snd)) 776 altq_disable(&ifp->if_snd); 777 if (ALTQ_IS_ATTACHED(&ifp->if_snd)) 778 altq_detach(&ifp->if_snd); 779 #endif 780 781 if (ifp->if_snd.ifq_lock) 782 mutex_obj_free(ifp->if_snd.ifq_lock); 783 784 sysctl_teardown(&ifp->if_sysctl_log); 785 786 #if NCARP > 0 787 /* Remove the interface from any carp group it is a part of. */ 788 if (ifp->if_carp != NULL && ifp->if_type != IFT_CARP) 789 carp_ifdetach(ifp); 790 #endif 791 792 /* 793 * Rip all the addresses off the interface. This should make 794 * all of the routes go away. 795 * 796 * pr_usrreq calls can remove an arbitrary number of ifaddrs 797 * from the list, including our "cursor", ifa. For safety, 798 * and to honor the TAILQ abstraction, I just restart the 799 * loop after each removal. Note that the loop will exit 800 * when all of the remaining ifaddrs belong to the AF_LINK 801 * family. I am counting on the historical fact that at 802 * least one pr_usrreq in each address domain removes at 803 * least one ifaddr. 804 */ 805 again: 806 IFADDR_FOREACH(ifa, ifp) { 807 family = ifa->ifa_addr->sa_family; 808 #ifdef IFAREF_DEBUG 809 printf("if_detach: ifaddr %p, family %d, refcnt %d\n", 810 ifa, family, ifa->ifa_refcnt); 811 if (last_ifa != NULL && ifa == last_ifa) 812 panic("if_detach: loop detected"); 813 last_ifa = ifa; 814 #endif 815 if (family == AF_LINK) 816 continue; 817 dp = pffinddomain(family); 818 #ifdef DIAGNOSTIC 819 if (dp == NULL) 820 panic("if_detach: no domain for AF %d", 821 family); 822 #endif 823 /* 824 * XXX These PURGEIF calls are redundant with the 825 * purge-all-families calls below, but are left in for 826 * now both to make a smaller change, and to avoid 827 * unplanned interactions with clearing of 828 * ifp->if_addrlist. 829 */ 830 purged = 0; 831 for (pr = dp->dom_protosw; 832 pr < dp->dom_protoswNPROTOSW; pr++) { 833 so.so_proto = pr; 834 if (pr->pr_usrreqs) { 835 (void) (*pr->pr_usrreqs->pr_purgeif)(&so, ifp); 836 purged = 1; 837 } 838 } 839 if (purged == 0) { 840 /* 841 * XXX What's really the best thing to do 842 * XXX here? --thorpej@NetBSD.org 843 */ 844 printf("if_detach: WARNING: AF %d not purged\n", 845 family); 846 ifa_remove(ifp, ifa); 847 } 848 goto again; 849 } 850 851 if_free_sadl(ifp); 852 853 /* Walk the routing table looking for stragglers. */ 854 for (i = 0; i <= AF_MAX; i++) { 855 while (rt_walktree(i, if_rt_walktree, ifp) == ERESTART) 856 continue; 857 } 858 859 DOMAIN_FOREACH(dp) { 860 if (dp->dom_ifdetach != NULL && ifp->if_afdata[dp->dom_family]) 861 { 862 void *p = ifp->if_afdata[dp->dom_family]; 863 if (p) { 864 ifp->if_afdata[dp->dom_family] = NULL; 865 (*dp->dom_ifdetach)(ifp, p); 866 } 867 } 868 869 /* 870 * One would expect multicast memberships (INET and 871 * INET6) on UDP sockets to be purged by the PURGEIF 872 * calls above, but if all addresses were removed from 873 * the interface prior to destruction, the calls will 874 * not be made (e.g. ppp, for which pppd(8) generally 875 * removes addresses before destroying the interface). 876 * Because there is no invariant that multicast 877 * memberships only exist for interfaces with IPv4 878 * addresses, we must call PURGEIF regardless of 879 * addresses. (Protocols which might store ifnet 880 * pointers are marked with PR_PURGEIF.) 881 */ 882 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) { 883 so.so_proto = pr; 884 if (pr->pr_usrreqs && pr->pr_flags & PR_PURGEIF) 885 (void)(*pr->pr_usrreqs->pr_purgeif)(&so, ifp); 886 } 887 } 888 889 (void)pfil_run_hooks(if_pfil, 890 (struct mbuf **)PFIL_IFNET_DETACH, ifp, PFIL_IFNET); 891 (void)pfil_head_destroy(ifp->if_pfil); 892 893 /* Announce that the interface is gone. */ 894 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 895 896 ifioctl_detach(ifp); 897 898 IF_AFDATA_LOCK_DESTROY(ifp); 899 900 /* 901 * remove packets that came from ifp, from software interrupt queues. 902 */ 903 DOMAIN_FOREACH(dp) { 904 for (i = 0; i < __arraycount(dp->dom_ifqueues); i++) { 905 struct ifqueue *iq = dp->dom_ifqueues[i]; 906 if (iq == NULL) 907 break; 908 dp->dom_ifqueues[i] = NULL; 909 if_detach_queues(ifp, iq); 910 } 911 } 912 913 /* 914 * IP queues have to be processed separately: net-queue barrier 915 * ensures that the packets are dequeued while a cross-call will 916 * ensure that the interrupts have completed. FIXME: not quite.. 917 */ 918 #ifdef INET 919 pktq_barrier(ip_pktq); 920 #endif 921 #ifdef INET6 922 if (in6_present) 923 pktq_barrier(ip6_pktq); 924 #endif 925 xc = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL); 926 xc_wait(xc); 927 928 splx(s); 929 } 930 931 static void 932 if_detach_queues(struct ifnet *ifp, struct ifqueue *q) 933 { 934 struct mbuf *m, *prev, *next; 935 936 prev = NULL; 937 for (m = q->ifq_head; m != NULL; m = next) { 938 KASSERT((m->m_flags & M_PKTHDR) != 0); 939 940 next = m->m_nextpkt; 941 if (m->m_pkthdr.rcvif != ifp) { 942 prev = m; 943 continue; 944 } 945 946 if (prev != NULL) 947 prev->m_nextpkt = m->m_nextpkt; 948 else 949 q->ifq_head = m->m_nextpkt; 950 if (q->ifq_tail == m) 951 q->ifq_tail = prev; 952 q->ifq_len--; 953 954 m->m_nextpkt = NULL; 955 m_freem(m); 956 IF_DROP(q); 957 } 958 } 959 960 /* 961 * Callback for a radix tree walk to delete all references to an 962 * ifnet. 963 */ 964 static int 965 if_rt_walktree(struct rtentry *rt, void *v) 966 { 967 struct ifnet *ifp = (struct ifnet *)v; 968 int error; 969 struct rtentry *retrt; 970 971 if (rt->rt_ifp != ifp) 972 return 0; 973 974 /* Delete the entry. */ 975 error = rtrequest(RTM_DELETE, rt_getkey(rt), rt->rt_gateway, 976 rt_mask(rt), rt->rt_flags, &retrt); 977 if (error == 0) { 978 KASSERT(retrt == rt); 979 KASSERT((retrt->rt_flags & RTF_UP) == 0); 980 retrt->rt_ifp = NULL; 981 rtfree(retrt); 982 } else { 983 printf("%s: warning: unable to delete rtentry @ %p, " 984 "error = %d\n", ifp->if_xname, rt, error); 985 } 986 return ERESTART; 987 } 988 989 /* 990 * Create a clone network interface. 991 */ 992 static int 993 if_clone_create(const char *name) 994 { 995 struct if_clone *ifc; 996 int unit; 997 998 ifc = if_clone_lookup(name, &unit); 999 if (ifc == NULL) 1000 return EINVAL; 1001 1002 if (ifunit(name) != NULL) 1003 return EEXIST; 1004 1005 return (*ifc->ifc_create)(ifc, unit); 1006 } 1007 1008 /* 1009 * Destroy a clone network interface. 1010 */ 1011 static int 1012 if_clone_destroy(const char *name) 1013 { 1014 struct if_clone *ifc; 1015 struct ifnet *ifp; 1016 1017 ifc = if_clone_lookup(name, NULL); 1018 if (ifc == NULL) 1019 return EINVAL; 1020 1021 ifp = ifunit(name); 1022 if (ifp == NULL) 1023 return ENXIO; 1024 1025 if (ifc->ifc_destroy == NULL) 1026 return EOPNOTSUPP; 1027 1028 return (*ifc->ifc_destroy)(ifp); 1029 } 1030 1031 /* 1032 * Look up a network interface cloner. 1033 */ 1034 static struct if_clone * 1035 if_clone_lookup(const char *name, int *unitp) 1036 { 1037 struct if_clone *ifc; 1038 const char *cp; 1039 char *dp, ifname[IFNAMSIZ + 3]; 1040 int unit; 1041 1042 strcpy(ifname, "if_"); 1043 /* separate interface name from unit */ 1044 for (dp = ifname + 3, cp = name; cp - name < IFNAMSIZ && 1045 *cp && (*cp < '0' || *cp > '9');) 1046 *dp++ = *cp++; 1047 1048 if (cp == name || cp - name == IFNAMSIZ || !*cp) 1049 return NULL; /* No name or unit number */ 1050 *dp++ = '\0'; 1051 1052 again: 1053 LIST_FOREACH(ifc, &if_cloners, ifc_list) { 1054 if (strcmp(ifname + 3, ifc->ifc_name) == 0) 1055 break; 1056 } 1057 1058 if (ifc == NULL) { 1059 if (*ifname == '\0' || 1060 module_autoload(ifname, MODULE_CLASS_DRIVER)) 1061 return NULL; 1062 *ifname = '\0'; 1063 goto again; 1064 } 1065 1066 unit = 0; 1067 while (cp - name < IFNAMSIZ && *cp) { 1068 if (*cp < '0' || *cp > '9' || unit >= INT_MAX / 10) { 1069 /* Bogus unit number. */ 1070 return NULL; 1071 } 1072 unit = (unit * 10) + (*cp++ - '0'); 1073 } 1074 1075 if (unitp != NULL) 1076 *unitp = unit; 1077 return ifc; 1078 } 1079 1080 /* 1081 * Register a network interface cloner. 1082 */ 1083 void 1084 if_clone_attach(struct if_clone *ifc) 1085 { 1086 1087 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list); 1088 if_cloners_count++; 1089 } 1090 1091 /* 1092 * Unregister a network interface cloner. 1093 */ 1094 void 1095 if_clone_detach(struct if_clone *ifc) 1096 { 1097 1098 LIST_REMOVE(ifc, ifc_list); 1099 if_cloners_count--; 1100 } 1101 1102 /* 1103 * Provide list of interface cloners to userspace. 1104 */ 1105 int 1106 if_clone_list(int buf_count, char *buffer, int *total) 1107 { 1108 char outbuf[IFNAMSIZ], *dst; 1109 struct if_clone *ifc; 1110 int count, error = 0; 1111 1112 *total = if_cloners_count; 1113 if ((dst = buffer) == NULL) { 1114 /* Just asking how many there are. */ 1115 return 0; 1116 } 1117 1118 if (buf_count < 0) 1119 return EINVAL; 1120 1121 count = (if_cloners_count < buf_count) ? 1122 if_cloners_count : buf_count; 1123 1124 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0; 1125 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) { 1126 (void)strncpy(outbuf, ifc->ifc_name, sizeof(outbuf)); 1127 if (outbuf[sizeof(outbuf) - 1] != '\0') 1128 return ENAMETOOLONG; 1129 error = copyout(outbuf, dst, sizeof(outbuf)); 1130 if (error != 0) 1131 break; 1132 } 1133 1134 return error; 1135 } 1136 1137 void 1138 ifaref(struct ifaddr *ifa) 1139 { 1140 ifa->ifa_refcnt++; 1141 } 1142 1143 void 1144 ifafree(struct ifaddr *ifa) 1145 { 1146 KASSERT(ifa != NULL); 1147 KASSERT(ifa->ifa_refcnt > 0); 1148 1149 if (--ifa->ifa_refcnt == 0) { 1150 free(ifa, M_IFADDR); 1151 } 1152 } 1153 1154 void 1155 ifa_insert(struct ifnet *ifp, struct ifaddr *ifa) 1156 { 1157 ifa->ifa_ifp = ifp; 1158 TAILQ_INSERT_TAIL(&ifp->if_addrlist, ifa, ifa_list); 1159 ifaref(ifa); 1160 } 1161 1162 void 1163 ifa_remove(struct ifnet *ifp, struct ifaddr *ifa) 1164 { 1165 KASSERT(ifa->ifa_ifp == ifp); 1166 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list); 1167 ifafree(ifa); 1168 } 1169 1170 static inline int 1171 equal(const struct sockaddr *sa1, const struct sockaddr *sa2) 1172 { 1173 return sockaddr_cmp(sa1, sa2) == 0; 1174 } 1175 1176 /* 1177 * Locate an interface based on a complete address. 1178 */ 1179 /*ARGSUSED*/ 1180 struct ifaddr * 1181 ifa_ifwithaddr(const struct sockaddr *addr) 1182 { 1183 struct ifnet *ifp; 1184 struct ifaddr *ifa; 1185 1186 IFNET_FOREACH(ifp) { 1187 if (ifp->if_output == if_nulloutput) 1188 continue; 1189 IFADDR_FOREACH(ifa, ifp) { 1190 if (ifa->ifa_addr->sa_family != addr->sa_family) 1191 continue; 1192 if (equal(addr, ifa->ifa_addr)) 1193 return ifa; 1194 if ((ifp->if_flags & IFF_BROADCAST) && 1195 ifa->ifa_broadaddr && 1196 /* IP6 doesn't have broadcast */ 1197 ifa->ifa_broadaddr->sa_len != 0 && 1198 equal(ifa->ifa_broadaddr, addr)) 1199 return ifa; 1200 } 1201 } 1202 return NULL; 1203 } 1204 1205 /* 1206 * Locate the point to point interface with a given destination address. 1207 */ 1208 /*ARGSUSED*/ 1209 struct ifaddr * 1210 ifa_ifwithdstaddr(const struct sockaddr *addr) 1211 { 1212 struct ifnet *ifp; 1213 struct ifaddr *ifa; 1214 1215 IFNET_FOREACH(ifp) { 1216 if (ifp->if_output == if_nulloutput) 1217 continue; 1218 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 1219 continue; 1220 IFADDR_FOREACH(ifa, ifp) { 1221 if (ifa->ifa_addr->sa_family != addr->sa_family || 1222 ifa->ifa_dstaddr == NULL) 1223 continue; 1224 if (equal(addr, ifa->ifa_dstaddr)) 1225 return ifa; 1226 } 1227 } 1228 return NULL; 1229 } 1230 1231 /* 1232 * Find an interface on a specific network. If many, choice 1233 * is most specific found. 1234 */ 1235 struct ifaddr * 1236 ifa_ifwithnet(const struct sockaddr *addr) 1237 { 1238 struct ifnet *ifp; 1239 struct ifaddr *ifa; 1240 const struct sockaddr_dl *sdl; 1241 struct ifaddr *ifa_maybe = 0; 1242 u_int af = addr->sa_family; 1243 const char *addr_data = addr->sa_data, *cplim; 1244 1245 if (af == AF_LINK) { 1246 sdl = satocsdl(addr); 1247 if (sdl->sdl_index && sdl->sdl_index < if_indexlim && 1248 ifindex2ifnet[sdl->sdl_index] && 1249 ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput) { 1250 return ifindex2ifnet[sdl->sdl_index]->if_dl; 1251 } 1252 } 1253 #ifdef NETATALK 1254 if (af == AF_APPLETALK) { 1255 const struct sockaddr_at *sat, *sat2; 1256 sat = (const struct sockaddr_at *)addr; 1257 IFNET_FOREACH(ifp) { 1258 if (ifp->if_output == if_nulloutput) 1259 continue; 1260 ifa = at_ifawithnet((const struct sockaddr_at *)addr, ifp); 1261 if (ifa == NULL) 1262 continue; 1263 sat2 = (struct sockaddr_at *)ifa->ifa_addr; 1264 if (sat2->sat_addr.s_net == sat->sat_addr.s_net) 1265 return ifa; /* exact match */ 1266 if (ifa_maybe == NULL) { 1267 /* else keep the if with the right range */ 1268 ifa_maybe = ifa; 1269 } 1270 } 1271 return ifa_maybe; 1272 } 1273 #endif 1274 IFNET_FOREACH(ifp) { 1275 if (ifp->if_output == if_nulloutput) 1276 continue; 1277 IFADDR_FOREACH(ifa, ifp) { 1278 const char *cp, *cp2, *cp3; 1279 1280 if (ifa->ifa_addr->sa_family != af || 1281 ifa->ifa_netmask == NULL) 1282 next: continue; 1283 cp = addr_data; 1284 cp2 = ifa->ifa_addr->sa_data; 1285 cp3 = ifa->ifa_netmask->sa_data; 1286 cplim = (const char *)ifa->ifa_netmask + 1287 ifa->ifa_netmask->sa_len; 1288 while (cp3 < cplim) { 1289 if ((*cp++ ^ *cp2++) & *cp3++) { 1290 /* want to continue for() loop */ 1291 goto next; 1292 } 1293 } 1294 if (ifa_maybe == NULL || 1295 rn_refines((void *)ifa->ifa_netmask, 1296 (void *)ifa_maybe->ifa_netmask)) 1297 ifa_maybe = ifa; 1298 } 1299 } 1300 return ifa_maybe; 1301 } 1302 1303 /* 1304 * Find the interface of the addresss. 1305 */ 1306 struct ifaddr * 1307 ifa_ifwithladdr(const struct sockaddr *addr) 1308 { 1309 struct ifaddr *ia; 1310 1311 if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) || 1312 (ia = ifa_ifwithnet(addr))) 1313 return ia; 1314 return NULL; 1315 } 1316 1317 /* 1318 * Find an interface using a specific address family 1319 */ 1320 struct ifaddr * 1321 ifa_ifwithaf(int af) 1322 { 1323 struct ifnet *ifp; 1324 struct ifaddr *ifa; 1325 1326 IFNET_FOREACH(ifp) { 1327 if (ifp->if_output == if_nulloutput) 1328 continue; 1329 IFADDR_FOREACH(ifa, ifp) { 1330 if (ifa->ifa_addr->sa_family == af) 1331 return ifa; 1332 } 1333 } 1334 return NULL; 1335 } 1336 1337 /* 1338 * Find an interface address specific to an interface best matching 1339 * a given address. 1340 */ 1341 struct ifaddr * 1342 ifaof_ifpforaddr(const struct sockaddr *addr, struct ifnet *ifp) 1343 { 1344 struct ifaddr *ifa; 1345 const char *cp, *cp2, *cp3; 1346 const char *cplim; 1347 struct ifaddr *ifa_maybe = 0; 1348 u_int af = addr->sa_family; 1349 1350 if (ifp->if_output == if_nulloutput) 1351 return NULL; 1352 1353 if (af >= AF_MAX) 1354 return NULL; 1355 1356 IFADDR_FOREACH(ifa, ifp) { 1357 if (ifa->ifa_addr->sa_family != af) 1358 continue; 1359 ifa_maybe = ifa; 1360 if (ifa->ifa_netmask == NULL) { 1361 if (equal(addr, ifa->ifa_addr) || 1362 (ifa->ifa_dstaddr && 1363 equal(addr, ifa->ifa_dstaddr))) 1364 return ifa; 1365 continue; 1366 } 1367 cp = addr->sa_data; 1368 cp2 = ifa->ifa_addr->sa_data; 1369 cp3 = ifa->ifa_netmask->sa_data; 1370 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 1371 for (; cp3 < cplim; cp3++) { 1372 if ((*cp++ ^ *cp2++) & *cp3) 1373 break; 1374 } 1375 if (cp3 == cplim) 1376 return ifa; 1377 } 1378 return ifa_maybe; 1379 } 1380 1381 /* 1382 * Default action when installing a route with a Link Level gateway. 1383 * Lookup an appropriate real ifa to point to. 1384 * This should be moved to /sys/net/link.c eventually. 1385 */ 1386 void 1387 link_rtrequest(int cmd, struct rtentry *rt, const struct rt_addrinfo *info) 1388 { 1389 struct ifaddr *ifa; 1390 const struct sockaddr *dst; 1391 struct ifnet *ifp; 1392 1393 if (cmd != RTM_ADD || (ifa = rt->rt_ifa) == NULL || 1394 (ifp = ifa->ifa_ifp) == NULL || (dst = rt_getkey(rt)) == NULL) 1395 return; 1396 if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) { 1397 rt_replace_ifa(rt, ifa); 1398 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 1399 ifa->ifa_rtrequest(cmd, rt, info); 1400 } 1401 } 1402 1403 /* 1404 * Handle a change in the interface link state. 1405 * XXX: We should listen to the routing socket in-kernel rather 1406 * than calling in6_if_link_* functions directly from here. 1407 */ 1408 void 1409 if_link_state_change(struct ifnet *ifp, int link_state) 1410 { 1411 int s; 1412 int old_link_state; 1413 struct domain *dp; 1414 1415 s = splnet(); 1416 if (ifp->if_link_state == link_state) { 1417 splx(s); 1418 return; 1419 } 1420 1421 old_link_state = ifp->if_link_state; 1422 ifp->if_link_state = link_state; 1423 #ifdef DEBUG 1424 log(LOG_DEBUG, "%s: link state %s (was %s)\n", ifp->if_xname, 1425 link_state == LINK_STATE_UP ? "UP" : 1426 link_state == LINK_STATE_DOWN ? "DOWN" : 1427 "UNKNOWN", 1428 old_link_state == LINK_STATE_UP ? "UP" : 1429 old_link_state == LINK_STATE_DOWN ? "DOWN" : 1430 "UNKNOWN"); 1431 #endif 1432 1433 /* 1434 * When going from UNKNOWN to UP, we need to mark existing 1435 * addresses as tentative and restart DAD as we may have 1436 * erroneously not found a duplicate. 1437 * 1438 * This needs to happen before rt_ifmsg to avoid a race where 1439 * listeners would have an address and expect it to work right 1440 * away. 1441 */ 1442 if (link_state == LINK_STATE_UP && 1443 old_link_state == LINK_STATE_UNKNOWN) 1444 { 1445 DOMAIN_FOREACH(dp) { 1446 if (dp->dom_if_link_state_change != NULL) 1447 dp->dom_if_link_state_change(ifp, 1448 LINK_STATE_DOWN); 1449 } 1450 } 1451 1452 /* Notify that the link state has changed. */ 1453 rt_ifmsg(ifp); 1454 1455 #if NCARP > 0 1456 if (ifp->if_carp) 1457 carp_carpdev_state(ifp); 1458 #endif 1459 1460 DOMAIN_FOREACH(dp) { 1461 if (dp->dom_if_link_state_change != NULL) 1462 dp->dom_if_link_state_change(ifp, link_state); 1463 } 1464 1465 splx(s); 1466 } 1467 1468 /* 1469 * Default action when installing a local route on a point-to-point 1470 * interface. 1471 */ 1472 void 1473 p2p_rtrequest(int req, struct rtentry *rt, 1474 __unused const struct rt_addrinfo *info) 1475 { 1476 struct ifnet *ifp = rt->rt_ifp; 1477 struct ifaddr *ifa, *lo0ifa; 1478 1479 switch (req) { 1480 case RTM_ADD: 1481 if ((rt->rt_flags & RTF_LOCAL) == 0) 1482 break; 1483 1484 IFADDR_FOREACH(ifa, ifp) { 1485 if (equal(rt_getkey(rt), ifa->ifa_addr)) 1486 break; 1487 } 1488 if (ifa == NULL) 1489 break; 1490 1491 /* 1492 * Ensure lo0 has an address of the same family. 1493 */ 1494 IFADDR_FOREACH(lo0ifa, lo0ifp) { 1495 if (lo0ifa->ifa_addr->sa_family == 1496 ifa->ifa_addr->sa_family) 1497 break; 1498 } 1499 if (lo0ifa == NULL) 1500 break; 1501 1502 rt->rt_ifp = lo0ifp; 1503 rt->rt_flags &= ~RTF_LLINFO; 1504 1505 /* 1506 * Make sure to set rt->rt_ifa to the interface 1507 * address we are using, otherwise we will have trouble 1508 * with source address selection. 1509 */ 1510 if (ifa != rt->rt_ifa) 1511 rt_replace_ifa(rt, ifa); 1512 break; 1513 case RTM_DELETE: 1514 case RTM_RESOLVE: 1515 default: 1516 break; 1517 } 1518 } 1519 1520 /* 1521 * Mark an interface down and notify protocols of 1522 * the transition. 1523 * NOTE: must be called at splsoftnet or equivalent. 1524 */ 1525 void 1526 if_down(struct ifnet *ifp) 1527 { 1528 struct ifaddr *ifa; 1529 struct domain *dp; 1530 1531 ifp->if_flags &= ~IFF_UP; 1532 nanotime(&ifp->if_lastchange); 1533 IFADDR_FOREACH(ifa, ifp) 1534 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 1535 IFQ_PURGE(&ifp->if_snd); 1536 #if NCARP > 0 1537 if (ifp->if_carp) 1538 carp_carpdev_state(ifp); 1539 #endif 1540 rt_ifmsg(ifp); 1541 DOMAIN_FOREACH(dp) { 1542 if (dp->dom_if_down) 1543 dp->dom_if_down(ifp); 1544 } 1545 } 1546 1547 /* 1548 * Mark an interface up and notify protocols of 1549 * the transition. 1550 * NOTE: must be called at splsoftnet or equivalent. 1551 */ 1552 void 1553 if_up(struct ifnet *ifp) 1554 { 1555 #ifdef notyet 1556 struct ifaddr *ifa; 1557 #endif 1558 struct domain *dp; 1559 1560 ifp->if_flags |= IFF_UP; 1561 nanotime(&ifp->if_lastchange); 1562 #ifdef notyet 1563 /* this has no effect on IP, and will kill all ISO connections XXX */ 1564 IFADDR_FOREACH(ifa, ifp) 1565 pfctlinput(PRC_IFUP, ifa->ifa_addr); 1566 #endif 1567 #if NCARP > 0 1568 if (ifp->if_carp) 1569 carp_carpdev_state(ifp); 1570 #endif 1571 rt_ifmsg(ifp); 1572 DOMAIN_FOREACH(dp) { 1573 if (dp->dom_if_up) 1574 dp->dom_if_up(ifp); 1575 } 1576 } 1577 1578 /* 1579 * Handle interface slowtimo timer routine. Called 1580 * from softclock, we decrement timer (if set) and 1581 * call the appropriate interface routine on expiration. 1582 */ 1583 static void 1584 if_slowtimo(void *arg) 1585 { 1586 void (*slowtimo)(struct ifnet *); 1587 struct ifnet *ifp = arg; 1588 int s; 1589 1590 slowtimo = ifp->if_slowtimo; 1591 if (__predict_false(slowtimo == NULL)) 1592 return; 1593 1594 s = splnet(); 1595 if (ifp->if_timer != 0 && --ifp->if_timer == 0) 1596 (*slowtimo)(ifp); 1597 1598 splx(s); 1599 1600 if (__predict_true(ifp->if_slowtimo != NULL)) 1601 callout_schedule(ifp->if_slowtimo_ch, hz / IFNET_SLOWHZ); 1602 } 1603 1604 /* 1605 * Set/clear promiscuous mode on interface ifp based on the truth value 1606 * of pswitch. The calls are reference counted so that only the first 1607 * "on" request actually has an effect, as does the final "off" request. 1608 * Results are undefined if the "off" and "on" requests are not matched. 1609 */ 1610 int 1611 ifpromisc(struct ifnet *ifp, int pswitch) 1612 { 1613 int pcount, ret; 1614 short nflags; 1615 1616 pcount = ifp->if_pcount; 1617 if (pswitch) { 1618 /* 1619 * Allow the device to be "placed" into promiscuous 1620 * mode even if it is not configured up. It will 1621 * consult IFF_PROMISC when it is brought up. 1622 */ 1623 if (ifp->if_pcount++ != 0) 1624 return 0; 1625 nflags = ifp->if_flags | IFF_PROMISC; 1626 } else { 1627 if (--ifp->if_pcount > 0) 1628 return 0; 1629 nflags = ifp->if_flags & ~IFF_PROMISC; 1630 } 1631 ret = if_flags_set(ifp, nflags); 1632 /* Restore interface state if not successful. */ 1633 if (ret != 0) { 1634 ifp->if_pcount = pcount; 1635 } 1636 return ret; 1637 } 1638 1639 /* 1640 * Map interface name to 1641 * interface structure pointer. 1642 */ 1643 struct ifnet * 1644 ifunit(const char *name) 1645 { 1646 struct ifnet *ifp; 1647 const char *cp = name; 1648 u_int unit = 0; 1649 u_int i; 1650 1651 /* 1652 * If the entire name is a number, treat it as an ifindex. 1653 */ 1654 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) { 1655 unit = unit * 10 + (*cp - '0'); 1656 } 1657 1658 /* 1659 * If the number took all of the name, then it's a valid ifindex. 1660 */ 1661 if (i == IFNAMSIZ || (cp != name && *cp == '\0')) { 1662 if (unit >= if_indexlim) 1663 return NULL; 1664 ifp = ifindex2ifnet[unit]; 1665 if (ifp == NULL || ifp->if_output == if_nulloutput) 1666 return NULL; 1667 return ifp; 1668 } 1669 1670 IFNET_FOREACH(ifp) { 1671 if (ifp->if_output == if_nulloutput) 1672 continue; 1673 if (strcmp(ifp->if_xname, name) == 0) 1674 return ifp; 1675 } 1676 return NULL; 1677 } 1678 1679 ifnet_t * 1680 if_byindex(u_int idx) 1681 { 1682 return (idx < if_indexlim) ? ifindex2ifnet[idx] : NULL; 1683 } 1684 1685 /* common */ 1686 int 1687 ifioctl_common(struct ifnet *ifp, u_long cmd, void *data) 1688 { 1689 int s; 1690 struct ifreq *ifr; 1691 struct ifcapreq *ifcr; 1692 struct ifdatareq *ifdr; 1693 1694 switch (cmd) { 1695 case SIOCSIFCAP: 1696 ifcr = data; 1697 if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0) 1698 return EINVAL; 1699 1700 if (ifcr->ifcr_capenable == ifp->if_capenable) 1701 return 0; 1702 1703 ifp->if_capenable = ifcr->ifcr_capenable; 1704 1705 /* Pre-compute the checksum flags mask. */ 1706 ifp->if_csum_flags_tx = 0; 1707 ifp->if_csum_flags_rx = 0; 1708 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Tx) { 1709 ifp->if_csum_flags_tx |= M_CSUM_IPv4; 1710 } 1711 if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) { 1712 ifp->if_csum_flags_rx |= M_CSUM_IPv4; 1713 } 1714 1715 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Tx) { 1716 ifp->if_csum_flags_tx |= M_CSUM_TCPv4; 1717 } 1718 if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) { 1719 ifp->if_csum_flags_rx |= M_CSUM_TCPv4; 1720 } 1721 1722 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Tx) { 1723 ifp->if_csum_flags_tx |= M_CSUM_UDPv4; 1724 } 1725 if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) { 1726 ifp->if_csum_flags_rx |= M_CSUM_UDPv4; 1727 } 1728 1729 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Tx) { 1730 ifp->if_csum_flags_tx |= M_CSUM_TCPv6; 1731 } 1732 if (ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) { 1733 ifp->if_csum_flags_rx |= M_CSUM_TCPv6; 1734 } 1735 1736 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Tx) { 1737 ifp->if_csum_flags_tx |= M_CSUM_UDPv6; 1738 } 1739 if (ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) { 1740 ifp->if_csum_flags_rx |= M_CSUM_UDPv6; 1741 } 1742 if (ifp->if_flags & IFF_UP) 1743 return ENETRESET; 1744 return 0; 1745 case SIOCSIFFLAGS: 1746 ifr = data; 1747 if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) { 1748 s = splnet(); 1749 if_down(ifp); 1750 splx(s); 1751 } 1752 if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) { 1753 s = splnet(); 1754 if_up(ifp); 1755 splx(s); 1756 } 1757 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 1758 (ifr->ifr_flags &~ IFF_CANTCHANGE); 1759 break; 1760 case SIOCGIFFLAGS: 1761 ifr = data; 1762 ifr->ifr_flags = ifp->if_flags; 1763 break; 1764 1765 case SIOCGIFMETRIC: 1766 ifr = data; 1767 ifr->ifr_metric = ifp->if_metric; 1768 break; 1769 1770 case SIOCGIFMTU: 1771 ifr = data; 1772 ifr->ifr_mtu = ifp->if_mtu; 1773 break; 1774 1775 case SIOCGIFDLT: 1776 ifr = data; 1777 ifr->ifr_dlt = ifp->if_dlt; 1778 break; 1779 1780 case SIOCGIFCAP: 1781 ifcr = data; 1782 ifcr->ifcr_capabilities = ifp->if_capabilities; 1783 ifcr->ifcr_capenable = ifp->if_capenable; 1784 break; 1785 1786 case SIOCSIFMETRIC: 1787 ifr = data; 1788 ifp->if_metric = ifr->ifr_metric; 1789 break; 1790 1791 case SIOCGIFDATA: 1792 ifdr = data; 1793 ifdr->ifdr_data = ifp->if_data; 1794 break; 1795 1796 case SIOCGIFINDEX: 1797 ifr = data; 1798 ifr->ifr_index = ifp->if_index; 1799 break; 1800 1801 case SIOCZIFDATA: 1802 ifdr = data; 1803 ifdr->ifdr_data = ifp->if_data; 1804 /* 1805 * Assumes that the volatile counters that can be 1806 * zero'ed are at the end of if_data. 1807 */ 1808 memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) - 1809 offsetof(struct if_data, ifi_ipackets)); 1810 /* 1811 * The memset() clears to the bottm of if_data. In the area, 1812 * if_lastchange is included. Please be careful if new entry 1813 * will be added into if_data or rewite this. 1814 * 1815 * And also, update if_lastchnage. 1816 */ 1817 getnanotime(&ifp->if_lastchange); 1818 break; 1819 case SIOCSIFMTU: 1820 ifr = data; 1821 if (ifp->if_mtu == ifr->ifr_mtu) 1822 break; 1823 ifp->if_mtu = ifr->ifr_mtu; 1824 /* 1825 * If the link MTU changed, do network layer specific procedure. 1826 */ 1827 #ifdef INET6 1828 if (in6_present) 1829 nd6_setmtu(ifp); 1830 #endif 1831 return ENETRESET; 1832 default: 1833 return ENOTTY; 1834 } 1835 return 0; 1836 } 1837 1838 int 1839 ifaddrpref_ioctl(struct socket *so, u_long cmd, void *data, struct ifnet *ifp) 1840 { 1841 struct if_addrprefreq *ifap = (struct if_addrprefreq *)data; 1842 struct ifaddr *ifa; 1843 const struct sockaddr *any, *sa; 1844 union { 1845 struct sockaddr sa; 1846 struct sockaddr_storage ss; 1847 } u, v; 1848 1849 switch (cmd) { 1850 case SIOCSIFADDRPREF: 1851 if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE, 1852 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd, 1853 NULL) != 0) 1854 return EPERM; 1855 case SIOCGIFADDRPREF: 1856 break; 1857 default: 1858 return EOPNOTSUPP; 1859 } 1860 1861 /* sanity checks */ 1862 if (data == NULL || ifp == NULL) { 1863 panic("invalid argument to %s", __func__); 1864 /*NOTREACHED*/ 1865 } 1866 1867 /* address must be specified on ADD and DELETE */ 1868 sa = sstocsa(&ifap->ifap_addr); 1869 if (sa->sa_family != sofamily(so)) 1870 return EINVAL; 1871 if ((any = sockaddr_any(sa)) == NULL || sa->sa_len != any->sa_len) 1872 return EINVAL; 1873 1874 sockaddr_externalize(&v.sa, sizeof(v.ss), sa); 1875 1876 IFADDR_FOREACH(ifa, ifp) { 1877 if (ifa->ifa_addr->sa_family != sa->sa_family) 1878 continue; 1879 sockaddr_externalize(&u.sa, sizeof(u.ss), ifa->ifa_addr); 1880 if (sockaddr_cmp(&u.sa, &v.sa) == 0) 1881 break; 1882 } 1883 if (ifa == NULL) 1884 return EADDRNOTAVAIL; 1885 1886 switch (cmd) { 1887 case SIOCSIFADDRPREF: 1888 ifa->ifa_preference = ifap->ifap_preference; 1889 return 0; 1890 case SIOCGIFADDRPREF: 1891 /* fill in the if_laddrreq structure */ 1892 (void)sockaddr_copy(sstosa(&ifap->ifap_addr), 1893 sizeof(ifap->ifap_addr), ifa->ifa_addr); 1894 ifap->ifap_preference = ifa->ifa_preference; 1895 return 0; 1896 default: 1897 return EOPNOTSUPP; 1898 } 1899 } 1900 1901 static void 1902 ifnet_lock_enter(struct ifnet_lock *il) 1903 { 1904 uint64_t *nenter; 1905 1906 /* Before trying to acquire the mutex, increase the count of threads 1907 * who have entered or who wait to enter the critical section. 1908 * Avoid one costly locked memory transaction by keeping a count for 1909 * each CPU. 1910 */ 1911 nenter = percpu_getref(il->il_nenter); 1912 (*nenter)++; 1913 percpu_putref(il->il_nenter); 1914 mutex_enter(&il->il_lock); 1915 } 1916 1917 static void 1918 ifnet_lock_exit(struct ifnet_lock *il) 1919 { 1920 /* Increase the count of threads who have exited the critical 1921 * section. Increase while we still hold the lock. 1922 */ 1923 il->il_nexit++; 1924 mutex_exit(&il->il_lock); 1925 } 1926 1927 /* 1928 * Interface ioctls. 1929 */ 1930 static int 1931 doifioctl(struct socket *so, u_long cmd, void *data, struct lwp *l) 1932 { 1933 struct ifnet *ifp; 1934 struct ifreq *ifr; 1935 int error = 0; 1936 #if defined(COMPAT_OSOCK) || defined(COMPAT_OIFREQ) 1937 u_long ocmd = cmd; 1938 #endif 1939 short oif_flags; 1940 #ifdef COMPAT_OIFREQ 1941 struct ifreq ifrb; 1942 struct oifreq *oifr = NULL; 1943 #endif 1944 int r; 1945 1946 switch (cmd) { 1947 #ifdef COMPAT_OIFREQ 1948 case OSIOCGIFCONF: 1949 case OOSIOCGIFCONF: 1950 return compat_ifconf(cmd, data); 1951 #endif 1952 #ifdef COMPAT_OIFDATA 1953 case OSIOCGIFDATA: 1954 case OSIOCZIFDATA: 1955 return compat_ifdatareq(l, cmd, data); 1956 #endif 1957 case SIOCGIFCONF: 1958 return ifconf(cmd, data); 1959 case SIOCINITIFADDR: 1960 return EPERM; 1961 } 1962 1963 #ifdef COMPAT_OIFREQ 1964 cmd = compat_cvtcmd(cmd); 1965 if (cmd != ocmd) { 1966 oifr = data; 1967 data = ifr = &ifrb; 1968 ifreqo2n(oifr, ifr); 1969 } else 1970 #endif 1971 ifr = data; 1972 1973 ifp = ifunit(ifr->ifr_name); 1974 1975 switch (cmd) { 1976 case SIOCIFCREATE: 1977 case SIOCIFDESTROY: 1978 if (l != NULL) { 1979 error = kauth_authorize_network(l->l_cred, 1980 KAUTH_NETWORK_INTERFACE, 1981 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, 1982 (void *)cmd, NULL); 1983 if (error != 0) 1984 return error; 1985 } 1986 mutex_enter(&if_clone_mtx); 1987 r = (cmd == SIOCIFCREATE) ? 1988 if_clone_create(ifr->ifr_name) : 1989 if_clone_destroy(ifr->ifr_name); 1990 mutex_exit(&if_clone_mtx); 1991 return r; 1992 1993 case SIOCIFGCLONERS: 1994 { 1995 struct if_clonereq *req = (struct if_clonereq *)data; 1996 return if_clone_list(req->ifcr_count, req->ifcr_buffer, 1997 &req->ifcr_total); 1998 } 1999 } 2000 2001 if (ifp == NULL) 2002 return ENXIO; 2003 2004 switch (cmd) { 2005 case SIOCALIFADDR: 2006 case SIOCDLIFADDR: 2007 case SIOCSIFADDRPREF: 2008 case SIOCSIFFLAGS: 2009 case SIOCSIFCAP: 2010 case SIOCSIFMETRIC: 2011 case SIOCZIFDATA: 2012 case SIOCSIFMTU: 2013 case SIOCSIFPHYADDR: 2014 case SIOCDIFPHYADDR: 2015 #ifdef INET6 2016 case SIOCSIFPHYADDR_IN6: 2017 #endif 2018 case SIOCSLIFPHYADDR: 2019 case SIOCADDMULTI: 2020 case SIOCDELMULTI: 2021 case SIOCSIFMEDIA: 2022 case SIOCSDRVSPEC: 2023 case SIOCG80211: 2024 case SIOCS80211: 2025 case SIOCS80211NWID: 2026 case SIOCS80211NWKEY: 2027 case SIOCS80211POWER: 2028 case SIOCS80211BSSID: 2029 case SIOCS80211CHANNEL: 2030 case SIOCSLINKSTR: 2031 if (l != NULL) { 2032 error = kauth_authorize_network(l->l_cred, 2033 KAUTH_NETWORK_INTERFACE, 2034 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, 2035 (void *)cmd, NULL); 2036 if (error != 0) 2037 return error; 2038 } 2039 } 2040 2041 oif_flags = ifp->if_flags; 2042 2043 ifnet_lock_enter(ifp->if_ioctl_lock); 2044 error = (*ifp->if_ioctl)(ifp, cmd, data); 2045 if (error != ENOTTY) 2046 ; 2047 else if (so->so_proto == NULL) 2048 error = EOPNOTSUPP; 2049 else { 2050 #ifdef COMPAT_OSOCK 2051 error = compat_ifioctl(so, ocmd, cmd, data, l); 2052 #else 2053 error = (*so->so_proto->pr_usrreqs->pr_ioctl)(so, 2054 cmd, data, ifp); 2055 #endif 2056 } 2057 2058 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) { 2059 if ((ifp->if_flags & IFF_UP) != 0) { 2060 int s = splnet(); 2061 if_up(ifp); 2062 splx(s); 2063 } 2064 } 2065 #ifdef COMPAT_OIFREQ 2066 if (cmd != ocmd) 2067 ifreqn2o(oifr, ifr); 2068 #endif 2069 2070 ifnet_lock_exit(ifp->if_ioctl_lock); 2071 return error; 2072 } 2073 2074 /* This callback adds to the sum in `arg' the number of 2075 * threads on `ci' who have entered or who wait to enter the 2076 * critical section. 2077 */ 2078 static void 2079 ifnet_lock_sum(void *p, void *arg, struct cpu_info *ci) 2080 { 2081 uint64_t *sum = arg, *nenter = p; 2082 2083 *sum += *nenter; 2084 } 2085 2086 /* Return the number of threads who have entered or who wait 2087 * to enter the critical section on all CPUs. 2088 */ 2089 static uint64_t 2090 ifnet_lock_entrances(struct ifnet_lock *il) 2091 { 2092 uint64_t sum = 0; 2093 2094 percpu_foreach(il->il_nenter, ifnet_lock_sum, &sum); 2095 2096 return sum; 2097 } 2098 2099 static int 2100 ifioctl_attach(struct ifnet *ifp) 2101 { 2102 struct ifnet_lock *il; 2103 2104 /* If the driver has not supplied its own if_ioctl, then 2105 * supply the default. 2106 */ 2107 if (ifp->if_ioctl == NULL) 2108 ifp->if_ioctl = ifioctl_common; 2109 2110 /* Create an ifnet_lock for synchronizing ifioctls. */ 2111 if ((il = kmem_zalloc(sizeof(*il), KM_SLEEP)) == NULL) 2112 return ENOMEM; 2113 2114 il->il_nenter = percpu_alloc(sizeof(uint64_t)); 2115 if (il->il_nenter == NULL) { 2116 kmem_free(il, sizeof(*il)); 2117 return ENOMEM; 2118 } 2119 2120 mutex_init(&il->il_lock, MUTEX_DEFAULT, IPL_NONE); 2121 cv_init(&il->il_emptied, ifp->if_xname); 2122 2123 ifp->if_ioctl_lock = il; 2124 2125 return 0; 2126 } 2127 2128 /* 2129 * This must not be called until after `ifp' has been withdrawn from the 2130 * ifnet tables so that ifioctl() cannot get a handle on it by calling 2131 * ifunit(). 2132 */ 2133 static void 2134 ifioctl_detach(struct ifnet *ifp) 2135 { 2136 struct ifnet_lock *il; 2137 2138 il = ifp->if_ioctl_lock; 2139 mutex_enter(&il->il_lock); 2140 /* Install if_nullioctl to make sure that any thread that 2141 * subsequently enters the critical section will quit it 2142 * immediately and signal the condition variable that we 2143 * wait on, below. 2144 */ 2145 ifp->if_ioctl = if_nullioctl; 2146 /* Sleep while threads are still in the critical section or 2147 * wait to enter it. 2148 */ 2149 while (ifnet_lock_entrances(il) != il->il_nexit) 2150 cv_wait(&il->il_emptied, &il->il_lock); 2151 /* At this point, we are the only thread still in the critical 2152 * section, and no new thread can get a handle on the ifioctl 2153 * lock, so it is safe to free its memory. 2154 */ 2155 mutex_exit(&il->il_lock); 2156 ifp->if_ioctl_lock = NULL; 2157 percpu_free(il->il_nenter, sizeof(uint64_t)); 2158 il->il_nenter = NULL; 2159 cv_destroy(&il->il_emptied); 2160 mutex_destroy(&il->il_lock); 2161 kmem_free(il, sizeof(*il)); 2162 } 2163 2164 /* 2165 * Return interface configuration 2166 * of system. List may be used 2167 * in later ioctl's (above) to get 2168 * other information. 2169 * 2170 * Each record is a struct ifreq. Before the addition of 2171 * sockaddr_storage, the API rule was that sockaddr flavors that did 2172 * not fit would extend beyond the struct ifreq, with the next struct 2173 * ifreq starting sa_len beyond the struct sockaddr. Because the 2174 * union in struct ifreq includes struct sockaddr_storage, every kind 2175 * of sockaddr must fit. Thus, there are no longer any overlength 2176 * records. 2177 * 2178 * Records are added to the user buffer if they fit, and ifc_len is 2179 * adjusted to the length that was written. Thus, the user is only 2180 * assured of getting the complete list if ifc_len on return is at 2181 * least sizeof(struct ifreq) less than it was on entry. 2182 * 2183 * If the user buffer pointer is NULL, this routine copies no data and 2184 * returns the amount of space that would be needed. 2185 * 2186 * Invariants: 2187 * ifrp points to the next part of the user's buffer to be used. If 2188 * ifrp != NULL, space holds the number of bytes remaining that we may 2189 * write at ifrp. Otherwise, space holds the number of bytes that 2190 * would have been written had there been adequate space. 2191 */ 2192 /*ARGSUSED*/ 2193 static int 2194 ifconf(u_long cmd, void *data) 2195 { 2196 struct ifconf *ifc = (struct ifconf *)data; 2197 struct ifnet *ifp; 2198 struct ifaddr *ifa; 2199 struct ifreq ifr, *ifrp = NULL; 2200 int space = 0, error = 0; 2201 const int sz = (int)sizeof(struct ifreq); 2202 const bool docopy = ifc->ifc_req != NULL; 2203 2204 if (docopy) { 2205 space = ifc->ifc_len; 2206 ifrp = ifc->ifc_req; 2207 } 2208 2209 IFNET_FOREACH(ifp) { 2210 (void)strncpy(ifr.ifr_name, ifp->if_xname, 2211 sizeof(ifr.ifr_name)); 2212 if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0') 2213 return ENAMETOOLONG; 2214 if (IFADDR_EMPTY(ifp)) { 2215 /* Interface with no addresses - send zero sockaddr. */ 2216 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr)); 2217 if (!docopy) { 2218 space += sz; 2219 continue; 2220 } 2221 if (space >= sz) { 2222 error = copyout(&ifr, ifrp, sz); 2223 if (error != 0) 2224 return error; 2225 ifrp++; 2226 space -= sz; 2227 } 2228 } 2229 2230 IFADDR_FOREACH(ifa, ifp) { 2231 struct sockaddr *sa = ifa->ifa_addr; 2232 /* all sockaddrs must fit in sockaddr_storage */ 2233 KASSERT(sa->sa_len <= sizeof(ifr.ifr_ifru)); 2234 2235 if (!docopy) { 2236 space += sz; 2237 continue; 2238 } 2239 memcpy(&ifr.ifr_space, sa, sa->sa_len); 2240 if (space >= sz) { 2241 error = copyout(&ifr, ifrp, sz); 2242 if (error != 0) 2243 return (error); 2244 ifrp++; space -= sz; 2245 } 2246 } 2247 } 2248 if (docopy) { 2249 KASSERT(0 <= space && space <= ifc->ifc_len); 2250 ifc->ifc_len -= space; 2251 } else { 2252 KASSERT(space >= 0); 2253 ifc->ifc_len = space; 2254 } 2255 return (0); 2256 } 2257 2258 int 2259 ifreq_setaddr(u_long cmd, struct ifreq *ifr, const struct sockaddr *sa) 2260 { 2261 uint8_t len; 2262 #ifdef COMPAT_OIFREQ 2263 struct ifreq ifrb; 2264 struct oifreq *oifr = NULL; 2265 u_long ocmd = cmd; 2266 cmd = compat_cvtcmd(cmd); 2267 if (cmd != ocmd) { 2268 oifr = (struct oifreq *)(void *)ifr; 2269 ifr = &ifrb; 2270 ifreqo2n(oifr, ifr); 2271 len = sizeof(oifr->ifr_addr); 2272 } else 2273 #endif 2274 len = sizeof(ifr->ifr_ifru.ifru_space); 2275 2276 if (len < sa->sa_len) 2277 return EFBIG; 2278 2279 memset(&ifr->ifr_addr, 0, len); 2280 sockaddr_copy(&ifr->ifr_addr, len, sa); 2281 2282 #ifdef COMPAT_OIFREQ 2283 if (cmd != ocmd) 2284 ifreqn2o(oifr, ifr); 2285 #endif 2286 return 0; 2287 } 2288 2289 /* 2290 * Queue message on interface, and start output if interface 2291 * not yet active. 2292 */ 2293 int 2294 ifq_enqueue(struct ifnet *ifp, struct mbuf *m 2295 ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr)) 2296 { 2297 int len = m->m_pkthdr.len; 2298 int mflags = m->m_flags; 2299 int s = splnet(); 2300 int error; 2301 2302 IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error); 2303 if (error != 0) 2304 goto out; 2305 ifp->if_obytes += len; 2306 if (mflags & M_MCAST) 2307 ifp->if_omcasts++; 2308 if ((ifp->if_flags & IFF_OACTIVE) == 0) 2309 (*ifp->if_start)(ifp); 2310 out: 2311 splx(s); 2312 return error; 2313 } 2314 2315 /* 2316 * Queue message on interface, possibly using a second fast queue 2317 */ 2318 int 2319 ifq_enqueue2(struct ifnet *ifp, struct ifqueue *ifq, struct mbuf *m 2320 ALTQ_COMMA ALTQ_DECL(struct altq_pktattr *pktattr)) 2321 { 2322 int error = 0; 2323 2324 if (ifq != NULL 2325 #ifdef ALTQ 2326 && ALTQ_IS_ENABLED(&ifp->if_snd) == 0 2327 #endif 2328 ) { 2329 if (IF_QFULL(ifq)) { 2330 IF_DROP(&ifp->if_snd); 2331 m_freem(m); 2332 if (error == 0) 2333 error = ENOBUFS; 2334 } else 2335 IF_ENQUEUE(ifq, m); 2336 } else 2337 IFQ_ENQUEUE(&ifp->if_snd, m, pktattr, error); 2338 if (error != 0) { 2339 ++ifp->if_oerrors; 2340 return error; 2341 } 2342 return 0; 2343 } 2344 2345 int 2346 if_addr_init(ifnet_t *ifp, struct ifaddr *ifa, const bool src) 2347 { 2348 int rc; 2349 2350 if (ifp->if_initaddr != NULL) 2351 rc = (*ifp->if_initaddr)(ifp, ifa, src); 2352 else if (src || 2353 (rc = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, ifa)) == ENOTTY) 2354 rc = (*ifp->if_ioctl)(ifp, SIOCINITIFADDR, ifa); 2355 2356 return rc; 2357 } 2358 2359 int 2360 if_do_dad(struct ifnet *ifp) 2361 { 2362 if ((ifp->if_flags & IFF_LOOPBACK) != 0) 2363 return 0; 2364 2365 switch (ifp->if_type) { 2366 case IFT_FAITH: 2367 /* 2368 * These interfaces do not have the IFF_LOOPBACK flag, 2369 * but loop packets back. We do not have to do DAD on such 2370 * interfaces. We should even omit it, because loop-backed 2371 * responses would confuse the DAD procedure. 2372 */ 2373 return 0; 2374 default: 2375 /* 2376 * Our DAD routine requires the interface up and running. 2377 * However, some interfaces can be up before the RUNNING 2378 * status. Additionaly, users may try to assign addresses 2379 * before the interface becomes up (or running). 2380 * We simply skip DAD in such a case as a work around. 2381 * XXX: we should rather mark "tentative" on such addresses, 2382 * and do DAD after the interface becomes ready. 2383 */ 2384 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != 2385 (IFF_UP|IFF_RUNNING)) 2386 return 0; 2387 2388 return 1; 2389 } 2390 } 2391 2392 int 2393 if_flags_set(ifnet_t *ifp, const short flags) 2394 { 2395 int rc; 2396 2397 if (ifp->if_setflags != NULL) 2398 rc = (*ifp->if_setflags)(ifp, flags); 2399 else { 2400 short cantflags, chgdflags; 2401 struct ifreq ifr; 2402 2403 chgdflags = ifp->if_flags ^ flags; 2404 cantflags = chgdflags & IFF_CANTCHANGE; 2405 2406 if (cantflags != 0) 2407 ifp->if_flags ^= cantflags; 2408 2409 /* Traditionally, we do not call if_ioctl after 2410 * setting/clearing only IFF_PROMISC if the interface 2411 * isn't IFF_UP. Uphold that tradition. 2412 */ 2413 if (chgdflags == IFF_PROMISC && (ifp->if_flags & IFF_UP) == 0) 2414 return 0; 2415 2416 memset(&ifr, 0, sizeof(ifr)); 2417 2418 ifr.ifr_flags = flags & ~IFF_CANTCHANGE; 2419 rc = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, &ifr); 2420 2421 if (rc != 0 && cantflags != 0) 2422 ifp->if_flags ^= cantflags; 2423 } 2424 2425 return rc; 2426 } 2427 2428 int 2429 if_mcast_op(ifnet_t *ifp, const unsigned long cmd, const struct sockaddr *sa) 2430 { 2431 int rc; 2432 struct ifreq ifr; 2433 2434 if (ifp->if_mcastop != NULL) 2435 rc = (*ifp->if_mcastop)(ifp, cmd, sa); 2436 else { 2437 ifreq_setaddr(cmd, &ifr, sa); 2438 rc = (*ifp->if_ioctl)(ifp, cmd, &ifr); 2439 } 2440 2441 return rc; 2442 } 2443 2444 static void 2445 sysctl_sndq_setup(struct sysctllog **clog, const char *ifname, 2446 struct ifaltq *ifq) 2447 { 2448 const struct sysctlnode *cnode, *rnode; 2449 2450 if (sysctl_createv(clog, 0, NULL, &rnode, 2451 CTLFLAG_PERMANENT, 2452 CTLTYPE_NODE, "interfaces", 2453 SYSCTL_DESCR("Per-interface controls"), 2454 NULL, 0, NULL, 0, 2455 CTL_NET, CTL_CREATE, CTL_EOL) != 0) 2456 goto bad; 2457 2458 if (sysctl_createv(clog, 0, &rnode, &rnode, 2459 CTLFLAG_PERMANENT, 2460 CTLTYPE_NODE, ifname, 2461 SYSCTL_DESCR("Interface controls"), 2462 NULL, 0, NULL, 0, 2463 CTL_CREATE, CTL_EOL) != 0) 2464 goto bad; 2465 2466 if (sysctl_createv(clog, 0, &rnode, &rnode, 2467 CTLFLAG_PERMANENT, 2468 CTLTYPE_NODE, "sndq", 2469 SYSCTL_DESCR("Interface output queue controls"), 2470 NULL, 0, NULL, 0, 2471 CTL_CREATE, CTL_EOL) != 0) 2472 goto bad; 2473 2474 if (sysctl_createv(clog, 0, &rnode, &cnode, 2475 CTLFLAG_PERMANENT, 2476 CTLTYPE_INT, "len", 2477 SYSCTL_DESCR("Current output queue length"), 2478 NULL, 0, &ifq->ifq_len, 0, 2479 CTL_CREATE, CTL_EOL) != 0) 2480 goto bad; 2481 2482 if (sysctl_createv(clog, 0, &rnode, &cnode, 2483 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2484 CTLTYPE_INT, "maxlen", 2485 SYSCTL_DESCR("Maximum allowed output queue length"), 2486 NULL, 0, &ifq->ifq_maxlen, 0, 2487 CTL_CREATE, CTL_EOL) != 0) 2488 goto bad; 2489 2490 if (sysctl_createv(clog, 0, &rnode, &cnode, 2491 CTLFLAG_PERMANENT, 2492 CTLTYPE_INT, "drops", 2493 SYSCTL_DESCR("Packets dropped due to full output queue"), 2494 NULL, 0, &ifq->ifq_drops, 0, 2495 CTL_CREATE, CTL_EOL) != 0) 2496 goto bad; 2497 2498 return; 2499 bad: 2500 printf("%s: could not attach sysctl nodes\n", ifname); 2501 return; 2502 } 2503 2504 #if defined(INET) || defined(INET6) 2505 2506 #define SYSCTL_NET_PKTQ(q, cn, c) \ 2507 static int \ 2508 sysctl_net_##q##_##cn(SYSCTLFN_ARGS) \ 2509 { \ 2510 return sysctl_pktq_count(SYSCTLFN_CALL(rnode), q, c); \ 2511 } 2512 2513 #if defined(INET) 2514 static int 2515 sysctl_net_ip_pktq_maxlen(SYSCTLFN_ARGS) 2516 { 2517 return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip_pktq); 2518 } 2519 SYSCTL_NET_PKTQ(ip_pktq, items, PKTQ_NITEMS) 2520 SYSCTL_NET_PKTQ(ip_pktq, drops, PKTQ_DROPS) 2521 #endif 2522 2523 #if defined(INET6) 2524 static int 2525 sysctl_net_ip6_pktq_maxlen(SYSCTLFN_ARGS) 2526 { 2527 return sysctl_pktq_maxlen(SYSCTLFN_CALL(rnode), ip6_pktq); 2528 } 2529 SYSCTL_NET_PKTQ(ip6_pktq, items, PKTQ_NITEMS) 2530 SYSCTL_NET_PKTQ(ip6_pktq, drops, PKTQ_DROPS) 2531 #endif 2532 2533 static void 2534 sysctl_net_pktq_setup(struct sysctllog **clog, int pf) 2535 { 2536 sysctlfn len_func = NULL, maxlen_func = NULL, drops_func = NULL; 2537 const char *pfname = NULL, *ipname = NULL; 2538 int ipn = 0, qid = 0; 2539 2540 switch (pf) { 2541 #if defined(INET) 2542 case PF_INET: 2543 len_func = sysctl_net_ip_pktq_items; 2544 maxlen_func = sysctl_net_ip_pktq_maxlen; 2545 drops_func = sysctl_net_ip_pktq_drops; 2546 pfname = "inet", ipn = IPPROTO_IP; 2547 ipname = "ip", qid = IPCTL_IFQ; 2548 break; 2549 #endif 2550 #if defined(INET6) 2551 case PF_INET6: 2552 len_func = sysctl_net_ip6_pktq_items; 2553 maxlen_func = sysctl_net_ip6_pktq_maxlen; 2554 drops_func = sysctl_net_ip6_pktq_drops; 2555 pfname = "inet6", ipn = IPPROTO_IPV6; 2556 ipname = "ip6", qid = IPV6CTL_IFQ; 2557 break; 2558 #endif 2559 default: 2560 KASSERT(false); 2561 } 2562 2563 sysctl_createv(clog, 0, NULL, NULL, 2564 CTLFLAG_PERMANENT, 2565 CTLTYPE_NODE, pfname, NULL, 2566 NULL, 0, NULL, 0, 2567 CTL_NET, pf, CTL_EOL); 2568 sysctl_createv(clog, 0, NULL, NULL, 2569 CTLFLAG_PERMANENT, 2570 CTLTYPE_NODE, ipname, NULL, 2571 NULL, 0, NULL, 0, 2572 CTL_NET, pf, ipn, CTL_EOL); 2573 sysctl_createv(clog, 0, NULL, NULL, 2574 CTLFLAG_PERMANENT, 2575 CTLTYPE_NODE, "ifq", 2576 SYSCTL_DESCR("Protocol input queue controls"), 2577 NULL, 0, NULL, 0, 2578 CTL_NET, pf, ipn, qid, CTL_EOL); 2579 2580 sysctl_createv(clog, 0, NULL, NULL, 2581 CTLFLAG_PERMANENT, 2582 CTLTYPE_INT, "len", 2583 SYSCTL_DESCR("Current input queue length"), 2584 len_func, 0, NULL, 0, 2585 CTL_NET, pf, ipn, qid, IFQCTL_LEN, CTL_EOL); 2586 sysctl_createv(clog, 0, NULL, NULL, 2587 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 2588 CTLTYPE_INT, "maxlen", 2589 SYSCTL_DESCR("Maximum allowed input queue length"), 2590 maxlen_func, 0, NULL, 0, 2591 CTL_NET, pf, ipn, qid, IFQCTL_MAXLEN, CTL_EOL); 2592 sysctl_createv(clog, 0, NULL, NULL, 2593 CTLFLAG_PERMANENT, 2594 CTLTYPE_INT, "drops", 2595 SYSCTL_DESCR("Packets dropped due to full input queue"), 2596 drops_func, 0, NULL, 0, 2597 CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL); 2598 } 2599 #endif /* INET || INET6 */ 2600 2601 static int 2602 if_sdl_sysctl(SYSCTLFN_ARGS) 2603 { 2604 struct ifnet *ifp; 2605 const struct sockaddr_dl *sdl; 2606 2607 if (namelen != 1) 2608 return EINVAL; 2609 2610 ifp = if_byindex(name[0]); 2611 if (ifp == NULL) 2612 return ENODEV; 2613 2614 sdl = ifp->if_sadl; 2615 if (sdl == NULL) { 2616 *oldlenp = 0; 2617 return 0; 2618 } 2619 2620 if (oldp == NULL) { 2621 *oldlenp = sdl->sdl_alen; 2622 return 0; 2623 } 2624 2625 if (*oldlenp >= sdl->sdl_alen) 2626 *oldlenp = sdl->sdl_alen; 2627 return sysctl_copyout(l, &sdl->sdl_data[sdl->sdl_nlen], oldp, *oldlenp); 2628 } 2629 2630 SYSCTL_SETUP(sysctl_net_sdl_setup, "sysctl net.sdl subtree setup") 2631 { 2632 const struct sysctlnode *rnode = NULL; 2633 2634 sysctl_createv(clog, 0, NULL, &rnode, 2635 CTLFLAG_PERMANENT, 2636 CTLTYPE_NODE, "sdl", 2637 SYSCTL_DESCR("Get active link-layer address"), 2638 if_sdl_sysctl, 0, NULL, 0, 2639 CTL_NET, CTL_CREATE, CTL_EOL); 2640 } 2641