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