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