1 /* $NetBSD: if.c,v 1.117 2003/02/01 06:23:46 thorpej 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 Studnemund 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. All advertising materials mentioning features or use of this software 81 * must display the following acknowledgement: 82 * This product includes software developed by the University of 83 * California, Berkeley and its contributors. 84 * 4. Neither the name of the University nor the names of its contributors 85 * may be used to endorse or promote products derived from this software 86 * without specific prior written permission. 87 * 88 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 89 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 90 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 91 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 92 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 93 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 94 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 95 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 96 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 97 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 98 * SUCH DAMAGE. 99 * 100 * @(#)if.c 8.5 (Berkeley) 1/9/95 101 */ 102 103 #include <sys/cdefs.h> 104 __KERNEL_RCSID(0, "$NetBSD: if.c,v 1.117 2003/02/01 06:23:46 thorpej Exp $"); 105 106 #include "opt_inet.h" 107 108 #include "opt_compat_linux.h" 109 #include "opt_compat_svr4.h" 110 #include "opt_compat_43.h" 111 #include "opt_atalk.h" 112 #include "opt_pfil_hooks.h" 113 114 #include <sys/param.h> 115 #include <sys/mbuf.h> 116 #include <sys/systm.h> 117 #include <sys/callout.h> 118 #include <sys/proc.h> 119 #include <sys/socket.h> 120 #include <sys/socketvar.h> 121 #include <sys/domain.h> 122 #include <sys/protosw.h> 123 #include <sys/kernel.h> 124 #include <sys/ioctl.h> 125 126 #include <net/if.h> 127 #include <net/if_dl.h> 128 #include <net/if_ether.h> 129 #include <net/if_ieee80211.h> 130 #include <net/if_types.h> 131 #include <net/radix.h> 132 #include <net/route.h> 133 #include <net/netisr.h> 134 #ifdef NETATALK 135 #include <netatalk/at_extern.h> 136 #include <netatalk/at.h> 137 #endif 138 139 #ifdef INET6 140 #include <netinet/in.h> 141 #include <netinet6/in6_var.h> 142 #include <netinet6/nd6.h> 143 #endif 144 145 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 146 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 147 148 int ifqmaxlen = IFQ_MAXLEN; 149 struct callout if_slowtimo_ch; 150 151 int netisr; /* scheduling bits for network */ 152 153 int if_rt_walktree __P((struct radix_node *, void *)); 154 155 struct if_clone *if_clone_lookup __P((const char *, int *)); 156 int if_clone_list __P((struct if_clonereq *)); 157 158 LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners); 159 int if_cloners_count; 160 161 #if defined(INET) || defined(INET6) || defined(NETATALK) || defined(NS) || \ 162 defined(ISO) || defined(CCITT) || defined(NATM) 163 static void if_detach_queues __P((struct ifnet *, struct ifqueue *)); 164 #endif 165 166 /* 167 * Network interface utility routines. 168 * 169 * Routines with ifa_ifwith* names take sockaddr *'s as 170 * parameters. 171 */ 172 void 173 ifinit() 174 { 175 176 callout_init(&if_slowtimo_ch); 177 if_slowtimo(NULL); 178 } 179 180 /* 181 * Null routines used while an interface is going away. These routines 182 * just return an error. 183 */ 184 185 int 186 if_nulloutput(ifp, m, so, rt) 187 struct ifnet *ifp; 188 struct mbuf *m; 189 struct sockaddr *so; 190 struct rtentry *rt; 191 { 192 193 return (ENXIO); 194 } 195 196 void 197 if_nullinput(ifp, m) 198 struct ifnet *ifp; 199 struct mbuf *m; 200 { 201 202 /* Nothing. */ 203 } 204 205 void 206 if_nullstart(ifp) 207 struct ifnet *ifp; 208 { 209 210 /* Nothing. */ 211 } 212 213 int 214 if_nullioctl(ifp, cmd, data) 215 struct ifnet *ifp; 216 u_long cmd; 217 caddr_t data; 218 { 219 220 return (ENXIO); 221 } 222 223 int 224 if_nullinit(ifp) 225 struct ifnet *ifp; 226 { 227 228 return (ENXIO); 229 } 230 231 void 232 if_nullstop(ifp, disable) 233 struct ifnet *ifp; 234 int disable; 235 { 236 237 /* Nothing. */ 238 } 239 240 void 241 if_nullwatchdog(ifp) 242 struct ifnet *ifp; 243 { 244 245 /* Nothing. */ 246 } 247 248 void 249 if_nulldrain(ifp) 250 struct ifnet *ifp; 251 { 252 253 /* Nothing. */ 254 } 255 256 u_int if_index = 1; 257 struct ifnet_head ifnet; 258 struct ifaddr **ifnet_addrs = NULL; 259 struct ifnet **ifindex2ifnet = NULL; 260 261 /* 262 * Allocate the link level name for the specified interface. This 263 * is an attachment helper. It must be called after ifp->if_addrlen 264 * is initialized, which may not be the case when if_attach() is 265 * called. 266 */ 267 void 268 if_alloc_sadl(struct ifnet *ifp) 269 { 270 unsigned socksize, ifasize; 271 int namelen, masklen; 272 struct sockaddr_dl *sdl; 273 struct ifaddr *ifa; 274 275 /* 276 * If the interface already has a link name, release it 277 * now. This is useful for interfaces that can change 278 * link types, and thus switch link names often. 279 */ 280 if (ifp->if_sadl != NULL) 281 if_free_sadl(ifp); 282 283 namelen = strlen(ifp->if_xname); 284 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + namelen; 285 socksize = masklen + ifp->if_addrlen; 286 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1))) 287 if (socksize < sizeof(*sdl)) 288 socksize = sizeof(*sdl); 289 socksize = ROUNDUP(socksize); 290 ifasize = sizeof(*ifa) + 2 * socksize; 291 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK); 292 memset((caddr_t)ifa, 0, ifasize); 293 sdl = (struct sockaddr_dl *)(ifa + 1); 294 sdl->sdl_len = socksize; 295 sdl->sdl_family = AF_LINK; 296 bcopy(ifp->if_xname, sdl->sdl_data, namelen); 297 sdl->sdl_nlen = namelen; 298 sdl->sdl_alen = ifp->if_addrlen; 299 sdl->sdl_index = ifp->if_index; 300 sdl->sdl_type = ifp->if_type; 301 ifnet_addrs[ifp->if_index] = ifa; 302 IFAREF(ifa); 303 ifa->ifa_ifp = ifp; 304 ifa->ifa_rtrequest = link_rtrequest; 305 TAILQ_INSERT_HEAD(&ifp->if_addrlist, ifa, ifa_list); 306 IFAREF(ifa); 307 ifa->ifa_addr = (struct sockaddr *)sdl; 308 ifp->if_sadl = sdl; 309 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 310 ifa->ifa_netmask = (struct sockaddr *)sdl; 311 sdl->sdl_len = masklen; 312 while (namelen != 0) 313 sdl->sdl_data[--namelen] = 0xff; 314 } 315 316 /* 317 * Free the link level name for the specified interface. This is 318 * a detach helper. This is called from if_detach() or from 319 * link layer type specific detach functions. 320 */ 321 void 322 if_free_sadl(struct ifnet *ifp) 323 { 324 struct ifaddr *ifa; 325 int s; 326 327 ifa = ifnet_addrs[ifp->if_index]; 328 if (ifa == NULL) { 329 KASSERT(ifp->if_sadl == NULL); 330 return; 331 } 332 333 KASSERT(ifp->if_sadl != NULL); 334 335 s = splnet(); 336 rtinit(ifa, RTM_DELETE, 0); 337 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list); 338 IFAFREE(ifa); 339 340 ifp->if_sadl = NULL; 341 342 ifnet_addrs[ifp->if_index] = NULL; 343 IFAFREE(ifa); 344 splx(s); 345 } 346 347 /* 348 * Attach an interface to the 349 * list of "active" interfaces. 350 */ 351 void 352 if_attach(ifp) 353 struct ifnet *ifp; 354 { 355 static size_t if_indexlim = 0; 356 int indexlim = 0; 357 358 if (if_indexlim == 0) { 359 TAILQ_INIT(&ifnet); 360 if_indexlim = 8; 361 } 362 TAILQ_INIT(&ifp->if_addrlist); 363 TAILQ_INSERT_TAIL(&ifnet, ifp, if_list); 364 ifp->if_index = if_index; 365 if (ifindex2ifnet == 0) 366 if_index++; 367 else 368 while (ifindex2ifnet[ifp->if_index] != NULL) { 369 ++if_index; 370 if (if_index == 0) 371 if_index = 1; 372 /* 373 * If we hit USHRT_MAX, we skip back to 0 since 374 * there are a number of places where the value 375 * of if_index or if_index itself is compared 376 * to or stored in an unsigned short. By 377 * jumping back, we won't botch those assignments 378 * or comparisons. 379 */ 380 else if (if_index == USHRT_MAX) { 381 /* 382 * However, if we have to jump back to 383 * zero *twice* without finding an empty 384 * slot in ifindex2ifnet[], then there 385 * there are too many (>65535) interfaces. 386 */ 387 if (indexlim++) 388 panic("too many interfaces"); 389 else 390 if_index = 1; 391 } 392 ifp->if_index = if_index; 393 } 394 395 /* 396 * We have some arrays that should be indexed by if_index. 397 * since if_index will grow dynamically, they should grow too. 398 * struct ifadd **ifnet_addrs 399 * struct ifnet **ifindex2ifnet 400 */ 401 if (ifnet_addrs == 0 || ifindex2ifnet == 0 || 402 ifp->if_index >= if_indexlim) { 403 size_t n; 404 caddr_t q; 405 406 while (ifp->if_index >= if_indexlim) 407 if_indexlim <<= 1; 408 409 /* grow ifnet_addrs */ 410 n = if_indexlim * sizeof(struct ifaddr *); 411 q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK); 412 memset(q, 0, n); 413 if (ifnet_addrs) { 414 bcopy((caddr_t)ifnet_addrs, q, n/2); 415 free((caddr_t)ifnet_addrs, M_IFADDR); 416 } 417 ifnet_addrs = (struct ifaddr **)q; 418 419 /* grow ifindex2ifnet */ 420 n = if_indexlim * sizeof(struct ifnet *); 421 q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK); 422 memset(q, 0, n); 423 if (ifindex2ifnet) { 424 bcopy((caddr_t)ifindex2ifnet, q, n/2); 425 free((caddr_t)ifindex2ifnet, M_IFADDR); 426 } 427 ifindex2ifnet = (struct ifnet **)q; 428 } 429 430 ifindex2ifnet[ifp->if_index] = ifp; 431 432 /* 433 * Link level name is allocated later by a separate call to 434 * if_alloc_sadl(). 435 */ 436 437 if (ifp->if_snd.ifq_maxlen == 0) 438 ifp->if_snd.ifq_maxlen = ifqmaxlen; 439 ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */ 440 441 ifp->if_link_state = LINK_STATE_UNKNOWN; 442 443 ifp->if_capenable = 0; 444 ifp->if_csum_flags_tx = 0; 445 ifp->if_csum_flags_rx = 0; 446 447 #ifdef ALTQ 448 ifp->if_snd.altq_type = 0; 449 ifp->if_snd.altq_disc = NULL; 450 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE; 451 ifp->if_snd.altq_tbr = NULL; 452 ifp->if_snd.altq_ifp = ifp; 453 #endif 454 455 #ifdef PFIL_HOOKS 456 ifp->if_pfil.ph_type = PFIL_TYPE_IFNET; 457 ifp->if_pfil.ph_ifnet = ifp; 458 if (pfil_head_register(&ifp->if_pfil) != 0) 459 printf("%s: WARNING: unable to register pfil hook\n", 460 ifp->if_xname); 461 #endif 462 463 if (domains) 464 if_attachdomain1(ifp); 465 466 /* Announce the interface. */ 467 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 468 } 469 470 void 471 if_attachdomain() 472 { 473 struct ifnet *ifp; 474 int s; 475 476 s = splnet(); 477 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) 478 if_attachdomain1(ifp); 479 splx(s); 480 } 481 482 void 483 if_attachdomain1(ifp) 484 struct ifnet *ifp; 485 { 486 struct domain *dp; 487 int s; 488 489 s = splnet(); 490 491 /* address family dependent data region */ 492 memset(ifp->if_afdata, 0, sizeof(ifp->if_afdata)); 493 for (dp = domains; dp; dp = dp->dom_next) { 494 if (dp->dom_ifattach) 495 ifp->if_afdata[dp->dom_family] = 496 (*dp->dom_ifattach)(ifp); 497 } 498 499 splx(s); 500 } 501 502 /* 503 * Deactivate an interface. This points all of the procedure 504 * handles at error stubs. May be called from interrupt context. 505 */ 506 void 507 if_deactivate(ifp) 508 struct ifnet *ifp; 509 { 510 int s; 511 512 s = splnet(); 513 514 ifp->if_output = if_nulloutput; 515 ifp->if_input = if_nullinput; 516 ifp->if_start = if_nullstart; 517 ifp->if_ioctl = if_nullioctl; 518 ifp->if_init = if_nullinit; 519 ifp->if_stop = if_nullstop; 520 ifp->if_watchdog = if_nullwatchdog; 521 ifp->if_drain = if_nulldrain; 522 523 /* No more packets may be enqueued. */ 524 ifp->if_snd.ifq_maxlen = 0; 525 526 splx(s); 527 } 528 529 /* 530 * Detach an interface from the list of "active" interfaces, 531 * freeing any resources as we go along. 532 * 533 * NOTE: This routine must be called with a valid thread context, 534 * as it may block. 535 */ 536 void 537 if_detach(ifp) 538 struct ifnet *ifp; 539 { 540 struct socket so; 541 struct ifaddr *ifa; 542 #ifdef IFAREF_DEBUG 543 struct ifaddr *last_ifa = NULL; 544 #endif 545 struct domain *dp; 546 struct protosw *pr; 547 struct radix_node_head *rnh; 548 int s, i, family, purged; 549 550 /* 551 * XXX It's kind of lame that we have to have the 552 * XXX socket structure... 553 */ 554 memset(&so, 0, sizeof(so)); 555 556 s = splnet(); 557 558 /* 559 * Do an if_down() to give protocols a chance to do something. 560 */ 561 if_down(ifp); 562 563 #ifdef ALTQ 564 if (ALTQ_IS_ENABLED(&ifp->if_snd)) 565 altq_disable(&ifp->if_snd); 566 if (ALTQ_IS_ATTACHED(&ifp->if_snd)) 567 altq_detach(&ifp->if_snd); 568 #endif 569 570 #ifdef PFIL_HOOKS 571 (void) pfil_head_unregister(&ifp->if_pfil); 572 #endif 573 574 if_free_sadl(ifp); 575 576 /* 577 * Rip all the addresses off the interface. This should make 578 * all of the routes go away. 579 */ 580 while ((ifa = TAILQ_FIRST(&ifp->if_addrlist)) != NULL) { 581 family = ifa->ifa_addr->sa_family; 582 #ifdef IFAREF_DEBUG 583 printf("if_detach: ifaddr %p, family %d, refcnt %d\n", 584 ifa, family, ifa->ifa_refcnt); 585 if (last_ifa != NULL && ifa == last_ifa) 586 panic("if_detach: loop detected"); 587 last_ifa = ifa; 588 #endif 589 if (family == AF_LINK) { 590 /* 591 * XXX This case may now be obsolete by 592 * XXX the call to if_free_sadl(). 593 */ 594 rtinit(ifa, RTM_DELETE, 0); 595 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list); 596 IFAFREE(ifa); 597 } else { 598 dp = pffinddomain(family); 599 #ifdef DIAGNOSTIC 600 if (dp == NULL) 601 panic("if_detach: no domain for AF %d", 602 family); 603 #endif 604 purged = 0; 605 for (pr = dp->dom_protosw; 606 pr < dp->dom_protoswNPROTOSW; pr++) { 607 so.so_proto = pr; 608 if (pr->pr_usrreq != NULL) { 609 (void) (*pr->pr_usrreq)(&so, 610 PRU_PURGEIF, NULL, NULL, 611 (struct mbuf *) ifp, curproc); 612 purged = 1; 613 } 614 } 615 if (purged == 0) { 616 /* 617 * XXX What's really the best thing to do 618 * XXX here? --thorpej@netbsd.org 619 */ 620 printf("if_detach: WARNING: AF %d not purged\n", 621 family); 622 } 623 } 624 } 625 626 /* Walk the routing table looking for straglers. */ 627 for (i = 0; i <= AF_MAX; i++) { 628 if ((rnh = rt_tables[i]) != NULL) 629 (void) (*rnh->rnh_walktree)(rnh, if_rt_walktree, ifp); 630 } 631 632 for (dp = domains; dp; dp = dp->dom_next) { 633 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) 634 (*dp->dom_ifdetach)(ifp, 635 ifp->if_afdata[dp->dom_family]); 636 } 637 638 /* Announce that the interface is gone. */ 639 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 640 641 ifindex2ifnet[ifp->if_index] = NULL; 642 643 TAILQ_REMOVE(&ifnet, ifp, if_list); 644 645 /* 646 * remove packets came from ifp, from software interrupt queues. 647 * net/netisr_dispatch.h is not usable, as some of them use 648 * strange queue names. 649 */ 650 #define IF_DETACH_QUEUES(x) \ 651 do { \ 652 extern struct ifqueue x; \ 653 if_detach_queues(ifp, & x); \ 654 } while (/*CONSTCOND*/ 0) 655 #ifdef INET 656 #if NARP > 0 657 IF_DETACH_QUEUES(arpintrq); 658 #endif 659 IF_DETACH_QUEUES(ipintrq); 660 #endif 661 #ifdef INET6 662 IF_DETACH_QUEUES(ip6intrq); 663 #endif 664 #ifdef NETATALK 665 IF_DETACH_QUEUES(atintrq1); 666 IF_DETACH_QUEUES(atintrq2); 667 #endif 668 #ifdef NS 669 IF_DETACH_QUEUES(nsintrq); 670 #endif 671 #ifdef ISO 672 IF_DETACH_QUEUES(clnlintrq); 673 #endif 674 #ifdef CCITT 675 IF_DETACH_QUEUES(llcintrq); 676 IF_DETACH_QUEUES(hdintrq); 677 #endif 678 #ifdef NATM 679 IF_DETACH_QUEUES(natmintrq); 680 #endif 681 #undef IF_DETACH_QUEUES 682 683 splx(s); 684 } 685 686 #if defined(INET) || defined(INET6) || defined(NETATALK) || defined(NS) || \ 687 defined(ISO) || defined(CCITT) || defined(NATM) 688 static void 689 if_detach_queues(ifp, q) 690 struct ifnet *ifp; 691 struct ifqueue *q; 692 { 693 struct mbuf *m, *prev, *next; 694 695 prev = NULL; 696 for (m = q->ifq_head; m; m = next) { 697 next = m->m_nextpkt; 698 #ifdef DIAGNOSTIC 699 if ((m->m_flags & M_PKTHDR) == 0) { 700 prev = m; 701 continue; 702 } 703 #endif 704 if (m->m_pkthdr.rcvif != ifp) { 705 prev = m; 706 continue; 707 } 708 709 if (prev) 710 prev->m_nextpkt = m->m_nextpkt; 711 else 712 q->ifq_head = m->m_nextpkt; 713 if (q->ifq_tail == m) 714 q->ifq_tail = prev; 715 q->ifq_len--; 716 717 m->m_nextpkt = NULL; 718 m_freem(m); 719 IF_DROP(q); 720 } 721 } 722 #endif /* defined(INET) || ... */ 723 724 /* 725 * Callback for a radix tree walk to delete all references to an 726 * ifnet. 727 */ 728 int 729 if_rt_walktree(rn, v) 730 struct radix_node *rn; 731 void *v; 732 { 733 struct ifnet *ifp = (struct ifnet *)v; 734 struct rtentry *rt = (struct rtentry *)rn; 735 int error; 736 737 if (rt->rt_ifp == ifp) { 738 /* Delete the entry. */ 739 error = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, 740 rt_mask(rt), rt->rt_flags, NULL); 741 if (error) 742 printf("%s: warning: unable to delete rtentry @ %p, " 743 "error = %d\n", ifp->if_xname, rt, error); 744 } 745 return (0); 746 } 747 748 /* 749 * Create a clone network interface. 750 */ 751 int 752 if_clone_create(name) 753 const char *name; 754 { 755 struct if_clone *ifc; 756 int unit; 757 758 ifc = if_clone_lookup(name, &unit); 759 if (ifc == NULL) 760 return (EINVAL); 761 762 if (ifunit(name) != NULL) 763 return (EEXIST); 764 765 return ((*ifc->ifc_create)(ifc, unit)); 766 } 767 768 /* 769 * Destroy a clone network interface. 770 */ 771 int 772 if_clone_destroy(name) 773 const char *name; 774 { 775 struct if_clone *ifc; 776 struct ifnet *ifp; 777 778 ifc = if_clone_lookup(name, NULL); 779 if (ifc == NULL) 780 return (EINVAL); 781 782 ifp = ifunit(name); 783 if (ifp == NULL) 784 return (ENXIO); 785 786 if (ifc->ifc_destroy == NULL) 787 return (EOPNOTSUPP); 788 789 (*ifc->ifc_destroy)(ifp); 790 return (0); 791 } 792 793 /* 794 * Look up a network interface cloner. 795 */ 796 struct if_clone * 797 if_clone_lookup(name, unitp) 798 const char *name; 799 int *unitp; 800 { 801 struct if_clone *ifc; 802 const char *cp; 803 size_t i; 804 805 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) { 806 for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) { 807 if (ifc->ifc_name[i] != *cp) 808 goto next_ifc; 809 } 810 goto found_name; 811 next_ifc: 812 ifc = LIST_NEXT(ifc, ifc_list); 813 } 814 815 /* No match. */ 816 return (NULL); 817 818 found_name: 819 for (i = 0; *cp != '\0'; cp++) { 820 if (*cp < '0' || *cp > '9') { 821 /* Bogus unit number. */ 822 return (NULL); 823 } 824 i = (i * 10) + (*cp - '0'); 825 } 826 827 if (unitp != NULL) 828 *unitp = i; 829 return (ifc); 830 } 831 832 /* 833 * Register a network interface cloner. 834 */ 835 void 836 if_clone_attach(ifc) 837 struct if_clone *ifc; 838 { 839 840 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list); 841 if_cloners_count++; 842 } 843 844 /* 845 * Unregister a network interface cloner. 846 */ 847 void 848 if_clone_detach(ifc) 849 struct if_clone *ifc; 850 { 851 852 LIST_REMOVE(ifc, ifc_list); 853 if_cloners_count--; 854 } 855 856 /* 857 * Provide list of interface cloners to userspace. 858 */ 859 int 860 if_clone_list(ifcr) 861 struct if_clonereq *ifcr; 862 { 863 char outbuf[IFNAMSIZ], *dst; 864 struct if_clone *ifc; 865 int count, error = 0; 866 867 ifcr->ifcr_total = if_cloners_count; 868 if ((dst = ifcr->ifcr_buffer) == NULL) { 869 /* Just asking how many there are. */ 870 return (0); 871 } 872 873 if (ifcr->ifcr_count < 0) 874 return (EINVAL); 875 876 count = (if_cloners_count < ifcr->ifcr_count) ? 877 if_cloners_count : ifcr->ifcr_count; 878 879 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0; 880 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) { 881 strncpy(outbuf, ifc->ifc_name, IFNAMSIZ); 882 outbuf[IFNAMSIZ - 1] = '\0'; /* sanity */ 883 error = copyout(outbuf, dst, IFNAMSIZ); 884 if (error) 885 break; 886 } 887 888 return (error); 889 } 890 891 /* 892 * Locate an interface based on a complete address. 893 */ 894 /*ARGSUSED*/ 895 struct ifaddr * 896 ifa_ifwithaddr(addr) 897 struct sockaddr *addr; 898 { 899 struct ifnet *ifp; 900 struct ifaddr *ifa; 901 902 #define equal(a1, a2) \ 903 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0) 904 905 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL; 906 ifp = TAILQ_NEXT(ifp, if_list)) { 907 if (ifp->if_output == if_nulloutput) 908 continue; 909 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; 910 ifa = TAILQ_NEXT(ifa, ifa_list)) { 911 if (ifa->ifa_addr->sa_family != addr->sa_family) 912 continue; 913 if (equal(addr, ifa->ifa_addr)) 914 return (ifa); 915 if ((ifp->if_flags & IFF_BROADCAST) && 916 ifa->ifa_broadaddr && 917 /* IP6 doesn't have broadcast */ 918 ifa->ifa_broadaddr->sa_len != 0 && 919 equal(ifa->ifa_broadaddr, addr)) 920 return (ifa); 921 } 922 } 923 return (NULL); 924 } 925 926 /* 927 * Locate the point to point interface with a given destination address. 928 */ 929 /*ARGSUSED*/ 930 struct ifaddr * 931 ifa_ifwithdstaddr(addr) 932 struct sockaddr *addr; 933 { 934 struct ifnet *ifp; 935 struct ifaddr *ifa; 936 937 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL; 938 ifp = TAILQ_NEXT(ifp, if_list)) { 939 if (ifp->if_output == if_nulloutput) 940 continue; 941 if (ifp->if_flags & IFF_POINTOPOINT) { 942 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; 943 ifa = TAILQ_NEXT(ifa, ifa_list)) { 944 if (ifa->ifa_addr->sa_family != 945 addr->sa_family || 946 ifa->ifa_dstaddr == NULL) 947 continue; 948 if (equal(addr, ifa->ifa_dstaddr)) 949 return (ifa); 950 } 951 } 952 } 953 return (NULL); 954 } 955 956 /* 957 * Find an interface on a specific network. If many, choice 958 * is most specific found. 959 */ 960 struct ifaddr * 961 ifa_ifwithnet(addr) 962 struct sockaddr *addr; 963 { 964 struct ifnet *ifp; 965 struct ifaddr *ifa; 966 struct sockaddr_dl *sdl; 967 struct ifaddr *ifa_maybe = 0; 968 u_int af = addr->sa_family; 969 char *addr_data = addr->sa_data, *cplim; 970 971 if (af == AF_LINK) { 972 sdl = (struct sockaddr_dl *)addr; 973 if (sdl->sdl_index && sdl->sdl_index <= if_index && 974 ifindex2ifnet[sdl->sdl_index]->if_output != if_nulloutput) 975 return (ifnet_addrs[sdl->sdl_index]); 976 } 977 #ifdef NETATALK 978 if (af == AF_APPLETALK) { 979 struct sockaddr_at *sat, *sat2; 980 sat = (struct sockaddr_at *)addr; 981 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL; 982 ifp = TAILQ_NEXT(ifp, if_list)) { 983 if (ifp->if_output == if_nulloutput) 984 continue; 985 ifa = at_ifawithnet((struct sockaddr_at *)addr, ifp); 986 if (ifa == NULL) 987 continue; 988 sat2 = (struct sockaddr_at *)ifa->ifa_addr; 989 if (sat2->sat_addr.s_net == sat->sat_addr.s_net) 990 return (ifa); /* exact match */ 991 if (ifa_maybe == NULL) { 992 /* else keep the if with the right range */ 993 ifa_maybe = ifa; 994 } 995 } 996 return (ifa_maybe); 997 } 998 #endif 999 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL; 1000 ifp = TAILQ_NEXT(ifp, if_list)) { 1001 if (ifp->if_output == if_nulloutput) 1002 continue; 1003 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; 1004 ifa = TAILQ_NEXT(ifa, ifa_list)) { 1005 char *cp, *cp2, *cp3; 1006 1007 if (ifa->ifa_addr->sa_family != af || 1008 ifa->ifa_netmask == 0) 1009 next: continue; 1010 cp = addr_data; 1011 cp2 = ifa->ifa_addr->sa_data; 1012 cp3 = ifa->ifa_netmask->sa_data; 1013 cplim = (char *)ifa->ifa_netmask + 1014 ifa->ifa_netmask->sa_len; 1015 while (cp3 < cplim) { 1016 if ((*cp++ ^ *cp2++) & *cp3++) { 1017 /* want to continue for() loop */ 1018 goto next; 1019 } 1020 } 1021 if (ifa_maybe == 0 || 1022 rn_refines((caddr_t)ifa->ifa_netmask, 1023 (caddr_t)ifa_maybe->ifa_netmask)) 1024 ifa_maybe = ifa; 1025 } 1026 } 1027 return (ifa_maybe); 1028 } 1029 1030 /* 1031 * Find the interface of the addresss. 1032 */ 1033 struct ifaddr * 1034 ifa_ifwithladdr(addr) 1035 struct sockaddr *addr; 1036 { 1037 struct ifaddr *ia; 1038 1039 if ((ia = ifa_ifwithaddr(addr)) || (ia = ifa_ifwithdstaddr(addr)) || 1040 (ia = ifa_ifwithnet(addr))) 1041 return (ia); 1042 return (NULL); 1043 } 1044 1045 /* 1046 * Find an interface using a specific address family 1047 */ 1048 struct ifaddr * 1049 ifa_ifwithaf(af) 1050 int af; 1051 { 1052 struct ifnet *ifp; 1053 struct ifaddr *ifa; 1054 1055 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL; 1056 ifp = TAILQ_NEXT(ifp, if_list)) { 1057 if (ifp->if_output == if_nulloutput) 1058 continue; 1059 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; 1060 ifa = TAILQ_NEXT(ifa, ifa_list)) { 1061 if (ifa->ifa_addr->sa_family == af) 1062 return (ifa); 1063 } 1064 } 1065 return (NULL); 1066 } 1067 1068 /* 1069 * Find an interface address specific to an interface best matching 1070 * a given address. 1071 */ 1072 struct ifaddr * 1073 ifaof_ifpforaddr(addr, ifp) 1074 struct sockaddr *addr; 1075 struct ifnet *ifp; 1076 { 1077 struct ifaddr *ifa; 1078 char *cp, *cp2, *cp3; 1079 char *cplim; 1080 struct ifaddr *ifa_maybe = 0; 1081 u_int af = addr->sa_family; 1082 1083 if (ifp->if_output == if_nulloutput) 1084 return (NULL); 1085 1086 if (af >= AF_MAX) 1087 return (NULL); 1088 1089 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; 1090 ifa = TAILQ_NEXT(ifa, ifa_list)) { 1091 if (ifa->ifa_addr->sa_family != af) 1092 continue; 1093 ifa_maybe = ifa; 1094 if (ifa->ifa_netmask == 0) { 1095 if (equal(addr, ifa->ifa_addr) || 1096 (ifa->ifa_dstaddr && 1097 equal(addr, ifa->ifa_dstaddr))) 1098 return (ifa); 1099 continue; 1100 } 1101 cp = addr->sa_data; 1102 cp2 = ifa->ifa_addr->sa_data; 1103 cp3 = ifa->ifa_netmask->sa_data; 1104 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 1105 for (; cp3 < cplim; cp3++) { 1106 if ((*cp++ ^ *cp2++) & *cp3) 1107 break; 1108 } 1109 if (cp3 == cplim) 1110 return (ifa); 1111 } 1112 return (ifa_maybe); 1113 } 1114 1115 /* 1116 * Default action when installing a route with a Link Level gateway. 1117 * Lookup an appropriate real ifa to point to. 1118 * This should be moved to /sys/net/link.c eventually. 1119 */ 1120 void 1121 link_rtrequest(cmd, rt, info) 1122 int cmd; 1123 struct rtentry *rt; 1124 struct rt_addrinfo *info; 1125 { 1126 struct ifaddr *ifa; 1127 struct sockaddr *dst; 1128 struct ifnet *ifp; 1129 1130 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 1131 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 1132 return; 1133 if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) { 1134 IFAFREE(rt->rt_ifa); 1135 rt->rt_ifa = ifa; 1136 IFAREF(ifa); 1137 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 1138 ifa->ifa_rtrequest(cmd, rt, info); 1139 } 1140 } 1141 1142 /* 1143 * Mark an interface down and notify protocols of 1144 * the transition. 1145 * NOTE: must be called at splsoftnet or equivalent. 1146 */ 1147 void 1148 if_down(ifp) 1149 struct ifnet *ifp; 1150 { 1151 struct ifaddr *ifa; 1152 1153 ifp->if_flags &= ~IFF_UP; 1154 microtime(&ifp->if_lastchange); 1155 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; 1156 ifa = TAILQ_NEXT(ifa, ifa_list)) 1157 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 1158 IFQ_PURGE(&ifp->if_snd); 1159 rt_ifmsg(ifp); 1160 } 1161 1162 /* 1163 * Mark an interface up and notify protocols of 1164 * the transition. 1165 * NOTE: must be called at splsoftnet or equivalent. 1166 */ 1167 void 1168 if_up(ifp) 1169 struct ifnet *ifp; 1170 { 1171 #ifdef notyet 1172 struct ifaddr *ifa; 1173 #endif 1174 1175 ifp->if_flags |= IFF_UP; 1176 microtime(&ifp->if_lastchange); 1177 #ifdef notyet 1178 /* this has no effect on IP, and will kill all ISO connections XXX */ 1179 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa != NULL; 1180 ifa = TAILQ_NEXT(ifa, ifa_list)) 1181 pfctlinput(PRC_IFUP, ifa->ifa_addr); 1182 #endif 1183 rt_ifmsg(ifp); 1184 #ifdef INET6 1185 in6_if_up(ifp); 1186 #endif 1187 } 1188 1189 /* 1190 * Handle interface watchdog timer routines. Called 1191 * from softclock, we decrement timers (if set) and 1192 * call the appropriate interface routine on expiration. 1193 */ 1194 void 1195 if_slowtimo(arg) 1196 void *arg; 1197 { 1198 struct ifnet *ifp; 1199 int s = splnet(); 1200 1201 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL; 1202 ifp = TAILQ_NEXT(ifp, if_list)) { 1203 if (ifp->if_timer == 0 || --ifp->if_timer) 1204 continue; 1205 if (ifp->if_watchdog) 1206 (*ifp->if_watchdog)(ifp); 1207 } 1208 splx(s); 1209 callout_reset(&if_slowtimo_ch, hz / IFNET_SLOWHZ, 1210 if_slowtimo, NULL); 1211 } 1212 1213 /* 1214 * Set/clear promiscuous mode on interface ifp based on the truth value 1215 * of pswitch. The calls are reference counted so that only the first 1216 * "on" request actually has an effect, as does the final "off" request. 1217 * Results are undefined if the "off" and "on" requests are not matched. 1218 */ 1219 int 1220 ifpromisc(ifp, pswitch) 1221 struct ifnet *ifp; 1222 int pswitch; 1223 { 1224 int pcount, ret; 1225 short flags; 1226 struct ifreq ifr; 1227 1228 pcount = ifp->if_pcount; 1229 flags = ifp->if_flags; 1230 if (pswitch) { 1231 /* 1232 * Allow the device to be "placed" into promiscuous 1233 * mode even if it is not configured up. It will 1234 * consult IFF_PROMISC when it is is brought up. 1235 */ 1236 if (ifp->if_pcount++ != 0) 1237 return (0); 1238 ifp->if_flags |= IFF_PROMISC; 1239 if ((ifp->if_flags & IFF_UP) == 0) 1240 return (0); 1241 } else { 1242 if (--ifp->if_pcount > 0) 1243 return (0); 1244 ifp->if_flags &= ~IFF_PROMISC; 1245 /* 1246 * If the device is not configured up, we should not need to 1247 * turn off promiscuous mode (device should have turned it 1248 * off when interface went down; and will look at IFF_PROMISC 1249 * again next time interface comes up). 1250 */ 1251 if ((ifp->if_flags & IFF_UP) == 0) 1252 return (0); 1253 } 1254 memset(&ifr, 0, sizeof(ifr)); 1255 ifr.ifr_flags = ifp->if_flags; 1256 ret = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t) &ifr); 1257 /* Restore interface state if not successful. */ 1258 if (ret != 0) { 1259 ifp->if_pcount = pcount; 1260 ifp->if_flags = flags; 1261 } 1262 return (ret); 1263 } 1264 1265 /* 1266 * Map interface name to 1267 * interface structure pointer. 1268 */ 1269 struct ifnet * 1270 ifunit(name) 1271 const char *name; 1272 { 1273 struct ifnet *ifp; 1274 const char *cp = name; 1275 u_int unit = 0; 1276 u_int i; 1277 1278 /* 1279 * If the entire name is a number, treat it as an ifindex. 1280 */ 1281 for (i = 0; i < IFNAMSIZ && *cp >= '0' && *cp <= '9'; i++, cp++) { 1282 unit = unit * 10 + (*cp - '0'); 1283 } 1284 1285 /* 1286 * If the number took all of the name, then it's a valid ifindex. 1287 */ 1288 if (i == IFNAMSIZ || (cp != name && *cp == '\0')) { 1289 if (unit >= if_index) 1290 return (NULL); 1291 ifp = ifindex2ifnet[unit]; 1292 if (ifp == NULL || ifp->if_output == if_nulloutput) 1293 return (NULL); 1294 return (ifp); 1295 } 1296 1297 for (ifp = TAILQ_FIRST(&ifnet); ifp != NULL; 1298 ifp = TAILQ_NEXT(ifp, if_list)) { 1299 if (ifp->if_output == if_nulloutput) 1300 continue; 1301 if (strcmp(ifp->if_xname, name) == 0) 1302 return (ifp); 1303 } 1304 return (NULL); 1305 } 1306 1307 /* 1308 * Interface ioctls. 1309 */ 1310 int 1311 ifioctl(so, cmd, data, p) 1312 struct socket *so; 1313 u_long cmd; 1314 caddr_t data; 1315 struct proc *p; 1316 { 1317 struct ifnet *ifp; 1318 struct ifreq *ifr; 1319 struct ifcapreq *ifcr; 1320 struct ifdatareq *ifdr; 1321 int s, error = 0; 1322 short oif_flags; 1323 1324 switch (cmd) { 1325 1326 case SIOCGIFCONF: 1327 case OSIOCGIFCONF: 1328 return (ifconf(cmd, data)); 1329 } 1330 ifr = (struct ifreq *)data; 1331 ifcr = (struct ifcapreq *)data; 1332 ifdr = (struct ifdatareq *)data; 1333 1334 switch (cmd) { 1335 case SIOCIFCREATE: 1336 case SIOCIFDESTROY: 1337 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 1338 return (error); 1339 return ((cmd == SIOCIFCREATE) ? 1340 if_clone_create(ifr->ifr_name) : 1341 if_clone_destroy(ifr->ifr_name)); 1342 1343 case SIOCIFGCLONERS: 1344 return (if_clone_list((struct if_clonereq *)data)); 1345 } 1346 1347 ifp = ifunit(ifr->ifr_name); 1348 if (ifp == 0) 1349 return (ENXIO); 1350 oif_flags = ifp->if_flags; 1351 switch (cmd) { 1352 1353 case SIOCGIFFLAGS: 1354 ifr->ifr_flags = ifp->if_flags; 1355 break; 1356 1357 case SIOCGIFMETRIC: 1358 ifr->ifr_metric = ifp->if_metric; 1359 break; 1360 1361 case SIOCGIFMTU: 1362 ifr->ifr_mtu = ifp->if_mtu; 1363 break; 1364 1365 case SIOCGIFDLT: 1366 ifr->ifr_dlt = ifp->if_dlt; 1367 break; 1368 1369 case SIOCSIFFLAGS: 1370 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 1371 return (error); 1372 if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) { 1373 s = splnet(); 1374 if_down(ifp); 1375 splx(s); 1376 } 1377 if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) { 1378 s = splnet(); 1379 if_up(ifp); 1380 splx(s); 1381 } 1382 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 1383 (ifr->ifr_flags &~ IFF_CANTCHANGE); 1384 if (ifp->if_ioctl) 1385 (void) (*ifp->if_ioctl)(ifp, cmd, data); 1386 break; 1387 1388 case SIOCGIFCAP: 1389 ifcr->ifcr_capabilities = ifp->if_capabilities; 1390 ifcr->ifcr_capenable = ifp->if_capenable; 1391 break; 1392 1393 case SIOCSIFCAP: 1394 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 1395 return (error); 1396 if ((ifcr->ifcr_capenable & ~ifp->if_capabilities) != 0) 1397 return (EINVAL); 1398 if (ifp->if_ioctl == NULL) 1399 return (EOPNOTSUPP); 1400 1401 /* Must prevent race with packet reception here. */ 1402 s = splnet(); 1403 if (ifcr->ifcr_capenable != ifp->if_capenable) { 1404 struct ifreq ifrq; 1405 1406 ifrq.ifr_flags = ifp->if_flags; 1407 ifp->if_capenable = ifcr->ifcr_capenable; 1408 1409 /* Pre-compute the checksum flags mask. */ 1410 ifp->if_csum_flags_tx = 0; 1411 ifp->if_csum_flags_rx = 0; 1412 if (ifp->if_capenable & IFCAP_CSUM_IPv4) { 1413 ifp->if_csum_flags_tx |= M_CSUM_IPv4; 1414 ifp->if_csum_flags_rx |= M_CSUM_IPv4; 1415 } 1416 1417 if (ifp->if_capenable & IFCAP_CSUM_TCPv4) { 1418 ifp->if_csum_flags_tx |= M_CSUM_TCPv4; 1419 ifp->if_csum_flags_rx |= M_CSUM_TCPv4; 1420 } else if (ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) 1421 ifp->if_csum_flags_rx |= M_CSUM_TCPv4; 1422 1423 if (ifp->if_capenable & IFCAP_CSUM_UDPv4) { 1424 ifp->if_csum_flags_tx |= M_CSUM_UDPv4; 1425 ifp->if_csum_flags_rx |= M_CSUM_UDPv4; 1426 } else if (ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) 1427 ifp->if_csum_flags_rx |= M_CSUM_UDPv4; 1428 1429 if (ifp->if_capenable & IFCAP_CSUM_TCPv6) { 1430 ifp->if_csum_flags_tx |= M_CSUM_TCPv6; 1431 ifp->if_csum_flags_rx |= M_CSUM_TCPv6; 1432 } 1433 1434 if (ifp->if_capenable & IFCAP_CSUM_UDPv6) { 1435 ifp->if_csum_flags_tx |= M_CSUM_UDPv6; 1436 ifp->if_csum_flags_rx |= M_CSUM_UDPv6; 1437 } 1438 1439 /* 1440 * Only kick the interface if it's up. If it's 1441 * not up now, it will notice the cap enables 1442 * when it is brought up later. 1443 */ 1444 if (ifp->if_flags & IFF_UP) 1445 (void) (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, 1446 (caddr_t) &ifrq); 1447 } 1448 splx(s); 1449 break; 1450 1451 case SIOCSIFMETRIC: 1452 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 1453 return (error); 1454 ifp->if_metric = ifr->ifr_metric; 1455 break; 1456 1457 case SIOCGIFDATA: 1458 ifdr->ifdr_data = ifp->if_data; 1459 break; 1460 1461 case SIOCZIFDATA: 1462 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 1463 return (error); 1464 ifdr->ifdr_data = ifp->if_data; 1465 /* 1466 * Assumes that the volatile counters that can be 1467 * zero'ed are at the end of if_data. 1468 */ 1469 memset(&ifp->if_data.ifi_ipackets, 0, sizeof(ifp->if_data) - 1470 offsetof(struct if_data, ifi_ipackets)); 1471 break; 1472 1473 case SIOCSIFMTU: 1474 { 1475 u_long oldmtu = ifp->if_mtu; 1476 1477 error = suser(p->p_ucred, &p->p_acflag); 1478 if (error) 1479 return (error); 1480 if (ifp->if_ioctl == NULL) 1481 return (EOPNOTSUPP); 1482 error = (*ifp->if_ioctl)(ifp, cmd, data); 1483 1484 /* 1485 * If the link MTU changed, do network layer specific procedure. 1486 */ 1487 if (ifp->if_mtu != oldmtu) { 1488 #ifdef INET6 1489 nd6_setmtu(ifp); 1490 #endif 1491 } 1492 break; 1493 } 1494 case SIOCSIFPHYADDR: 1495 case SIOCDIFPHYADDR: 1496 #ifdef INET6 1497 case SIOCSIFPHYADDR_IN6: 1498 #endif 1499 case SIOCSLIFPHYADDR: 1500 case SIOCADDMULTI: 1501 case SIOCDELMULTI: 1502 case SIOCSIFMEDIA: 1503 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 1504 return (error); 1505 /* FALLTHROUGH */ 1506 case SIOCGIFPSRCADDR: 1507 case SIOCGIFPDSTADDR: 1508 case SIOCGLIFPHYADDR: 1509 case SIOCGIFMEDIA: 1510 if (ifp->if_ioctl == 0) 1511 return (EOPNOTSUPP); 1512 error = (*ifp->if_ioctl)(ifp, cmd, data); 1513 break; 1514 1515 case SIOCSDRVSPEC: 1516 case SIOCS80211NWID: 1517 case SIOCS80211NWKEY: 1518 case SIOCS80211POWER: 1519 case SIOCS80211BSSID: 1520 case SIOCS80211CHANNEL: 1521 /* XXX: need to pass proc pointer through to driver... */ 1522 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 1523 return (error); 1524 /* FALLTHROUGH */ 1525 default: 1526 if (so->so_proto == 0) 1527 return (EOPNOTSUPP); 1528 #if !defined(COMPAT_43) && !defined(COMPAT_LINUX) && !defined(COMPAT_SVR4) && !defined(LKM) 1529 error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL, 1530 (struct mbuf *)cmd, (struct mbuf *)data, 1531 (struct mbuf *)ifp, p)); 1532 #else 1533 { 1534 int ocmd = cmd; 1535 1536 switch (cmd) { 1537 1538 case SIOCSIFADDR: 1539 case SIOCSIFDSTADDR: 1540 case SIOCSIFBRDADDR: 1541 case SIOCSIFNETMASK: 1542 #if BYTE_ORDER != BIG_ENDIAN 1543 if (ifr->ifr_addr.sa_family == 0 && 1544 ifr->ifr_addr.sa_len < 16) { 1545 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 1546 ifr->ifr_addr.sa_len = 16; 1547 } 1548 #else 1549 if (ifr->ifr_addr.sa_len == 0) 1550 ifr->ifr_addr.sa_len = 16; 1551 #endif 1552 break; 1553 1554 case OSIOCGIFADDR: 1555 cmd = SIOCGIFADDR; 1556 break; 1557 1558 case OSIOCGIFDSTADDR: 1559 cmd = SIOCGIFDSTADDR; 1560 break; 1561 1562 case OSIOCGIFBRDADDR: 1563 cmd = SIOCGIFBRDADDR; 1564 break; 1565 1566 case OSIOCGIFNETMASK: 1567 cmd = SIOCGIFNETMASK; 1568 } 1569 1570 error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL, 1571 (struct mbuf *)cmd, (struct mbuf *)data, 1572 (struct mbuf *)ifp, p)); 1573 1574 switch (ocmd) { 1575 case OSIOCGIFADDR: 1576 case OSIOCGIFDSTADDR: 1577 case OSIOCGIFBRDADDR: 1578 case OSIOCGIFNETMASK: 1579 *(u_int16_t *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 1580 } 1581 } 1582 #endif /* COMPAT_43 */ 1583 break; 1584 } 1585 1586 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) { 1587 #ifdef INET6 1588 if ((ifp->if_flags & IFF_UP) != 0) { 1589 s = splnet(); 1590 in6_if_up(ifp); 1591 splx(s); 1592 } 1593 #endif 1594 } 1595 1596 return (error); 1597 } 1598 1599 /* 1600 * Return interface configuration 1601 * of system. List may be used 1602 * in later ioctl's (above) to get 1603 * other information. 1604 */ 1605 /*ARGSUSED*/ 1606 int 1607 ifconf(cmd, data) 1608 u_long cmd; 1609 caddr_t data; 1610 { 1611 struct ifconf *ifc = (struct ifconf *)data; 1612 struct ifnet *ifp; 1613 struct ifaddr *ifa; 1614 struct ifreq ifr, *ifrp; 1615 int space = ifc->ifc_len, error = 0; 1616 1617 ifrp = ifc->ifc_req; 1618 TAILQ_FOREACH(ifp, &ifnet, if_list) { 1619 bcopy(ifp->if_xname, ifr.ifr_name, IFNAMSIZ); 1620 if ((ifa = TAILQ_FIRST(&ifp->if_addrlist)) == 0) { 1621 memset((caddr_t)&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr)); 1622 if (space >= (int)sizeof (ifr)) { 1623 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 1624 sizeof(ifr)); 1625 if (error) 1626 break; 1627 } 1628 space -= sizeof (ifr), ifrp++; 1629 } else 1630 for (; ifa != 0; ifa = TAILQ_NEXT(ifa, ifa_list)) { 1631 struct sockaddr *sa = ifa->ifa_addr; 1632 #if defined(COMPAT_43) || defined(COMPAT_LINUX) || defined(COMPAT_SVR4) 1633 if (cmd == OSIOCGIFCONF) { 1634 struct osockaddr *osa = 1635 (struct osockaddr *)&ifr.ifr_addr; 1636 ifr.ifr_addr = *sa; 1637 osa->sa_family = sa->sa_family; 1638 if (space >= (int)sizeof (ifr)) { 1639 error = copyout((caddr_t)&ifr, 1640 (caddr_t)ifrp, 1641 sizeof (ifr)); 1642 ifrp++; 1643 } 1644 } else 1645 #endif 1646 if (sa->sa_len <= sizeof(*sa)) { 1647 ifr.ifr_addr = *sa; 1648 if (space >= (int)sizeof (ifr)) { 1649 error = copyout((caddr_t)&ifr, 1650 (caddr_t)ifrp, 1651 sizeof (ifr)); 1652 ifrp++; 1653 } 1654 } else { 1655 space -= sa->sa_len - sizeof(*sa); 1656 if (space >= (int)sizeof (ifr)) { 1657 error = copyout((caddr_t)&ifr, 1658 (caddr_t)ifrp, 1659 sizeof (ifr.ifr_name)); 1660 if (error == 0) { 1661 error = copyout((caddr_t)sa, 1662 (caddr_t)&ifrp->ifr_addr, 1663 sa->sa_len); 1664 } 1665 ifrp = (struct ifreq *) 1666 (sa->sa_len + 1667 (caddr_t)&ifrp->ifr_addr); 1668 } 1669 } 1670 if (error) 1671 break; 1672 space -= sizeof (ifr); 1673 } 1674 } 1675 ifc->ifc_len -= space; 1676 return (error); 1677 } 1678