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