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