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