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