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