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