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