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