1 /* $OpenBSD: if.c,v 1.67 2003/07/23 22:48:00 itojun Exp $ */ 2 /* $NetBSD: if.c,v 1.35 1996/05/07 05:26:04 thorpej Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1980, 1986, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * @(#)if.c 8.3 (Berkeley) 1/4/94 62 */ 63 64 #include "bpfilter.h" 65 #include "bridge.h" 66 67 #include <sys/param.h> 68 #include <sys/systm.h> 69 #include <sys/mbuf.h> 70 #include <sys/proc.h> 71 #include <sys/socket.h> 72 #include <sys/socketvar.h> 73 #include <sys/protosw.h> 74 #include <sys/kernel.h> 75 #include <sys/ioctl.h> 76 #include <sys/domain.h> 77 78 #include <net/if.h> 79 #include <net/if_dl.h> 80 #include <net/route.h> 81 82 #ifdef INET 83 #include <netinet/in.h> 84 #include <netinet/in_var.h> 85 #include <netinet/if_ether.h> 86 #include <netinet/igmp.h> 87 #ifdef MROUTING 88 #include <netinet/ip_mroute.h> 89 #endif 90 #endif 91 92 #ifdef INET6 93 #ifndef INET 94 #include <netinet/in.h> 95 #endif 96 #include <netinet6/in6_ifattach.h> 97 #include <netinet6/nd6.h> 98 #endif 99 100 #if NBPFILTER > 0 101 #include <net/bpf.h> 102 #endif 103 104 #if NBRIDGE > 0 105 #include <net/if_bridge.h> 106 #endif 107 108 void if_attachsetup(struct ifnet *); 109 void if_attachdomain1(struct ifnet *); 110 int if_detach_rtdelete(struct radix_node *, void *); 111 int if_mark_ignore(struct radix_node *, void *); 112 int if_mark_unignore(struct radix_node *, void *); 113 114 int ifqmaxlen = IFQ_MAXLEN; 115 int netisr; 116 117 void if_detach_queues(struct ifnet *, struct ifqueue *); 118 void if_detached_start(struct ifnet *); 119 int if_detached_ioctl(struct ifnet *, u_long, caddr_t); 120 void if_detached_watchdog(struct ifnet *); 121 122 /* 123 * Network interface utility routines. 124 * 125 * Routines with ifa_ifwith* names take sockaddr *'s as 126 * parameters. 127 */ 128 void 129 ifinit() 130 { 131 static struct timeout if_slowtim; 132 133 timeout_set(&if_slowtim, if_slowtimo, &if_slowtim); 134 135 if_slowtimo(&if_slowtim); 136 } 137 138 int if_index = 0; 139 struct ifaddr **ifnet_addrs = NULL; 140 struct ifnet **ifindex2ifnet = NULL; 141 struct ifnet_head ifnet; 142 struct ifnet *lo0ifp; 143 144 /* 145 * Attach an interface to the 146 * list of "active" interfaces. 147 */ 148 void 149 if_attachsetup(ifp) 150 struct ifnet *ifp; 151 { 152 struct ifaddr *ifa; 153 static int if_indexlim = 8; 154 155 ifp->if_index = ++if_index; 156 157 /* 158 * We have some arrays that should be indexed by if_index. 159 * since if_index will grow dynamically, they should grow too. 160 * struct ifadd **ifnet_addrs 161 * struct ifnet **ifindex2ifnet 162 */ 163 if (ifnet_addrs == 0 || ifindex2ifnet == 0 || if_index >= if_indexlim) { 164 size_t n; 165 caddr_t q; 166 167 while (if_index >= if_indexlim) 168 if_indexlim <<= 1; 169 170 /* grow ifnet_addrs */ 171 n = if_indexlim * sizeof(ifa); 172 q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK); 173 bzero(q, n); 174 if (ifnet_addrs) { 175 bcopy((caddr_t)ifnet_addrs, q, n/2); 176 free((caddr_t)ifnet_addrs, M_IFADDR); 177 } 178 ifnet_addrs = (struct ifaddr **)q; 179 180 /* grow ifindex2ifnet */ 181 n = if_indexlim * sizeof(struct ifnet *); 182 q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK); 183 bzero(q, n); 184 if (ifindex2ifnet) { 185 bcopy((caddr_t)ifindex2ifnet, q, n/2); 186 free((caddr_t)ifindex2ifnet, M_IFADDR); 187 } 188 ifindex2ifnet = (struct ifnet **)q; 189 } 190 191 ifindex2ifnet[if_index] = ifp; 192 193 if (ifp->if_snd.ifq_maxlen == 0) 194 ifp->if_snd.ifq_maxlen = ifqmaxlen; 195 #ifdef ALTQ 196 ifp->if_snd.altq_type = 0; 197 ifp->if_snd.altq_disc = NULL; 198 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE; 199 ifp->if_snd.altq_tbr = NULL; 200 ifp->if_snd.altq_ifp = ifp; 201 #endif 202 203 if (domains) 204 if_attachdomain1(ifp); 205 } 206 207 /* 208 * Allocate the link level name for the specified interface. This 209 * is an attachment helper. It must be called after ifp->if_addrlen 210 * is initialized, which may not be the case when if_attach() is 211 * called. 212 */ 213 void 214 if_alloc_sadl(ifp) 215 struct ifnet *ifp; 216 { 217 unsigned socksize, ifasize; 218 int namelen, masklen; 219 struct sockaddr_dl *sdl; 220 struct ifaddr *ifa; 221 222 /* 223 * If the interface already has a link name, release it 224 * now. This is useful for interfaces that can change 225 * link types, and thus switch link names often. 226 */ 227 if (ifp->if_sadl != NULL) 228 if_free_sadl(ifp); 229 230 namelen = strlen(ifp->if_xname); 231 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m)) 232 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen; 233 socksize = masklen + ifp->if_addrlen; 234 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1))) 235 if (socksize < sizeof(*sdl)) 236 socksize = sizeof(*sdl); 237 socksize = ROUNDUP(socksize); 238 ifasize = sizeof(*ifa) + 2 * socksize; 239 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK); 240 bzero((caddr_t)ifa, ifasize); 241 sdl = (struct sockaddr_dl *)(ifa + 1); 242 sdl->sdl_len = socksize; 243 sdl->sdl_family = AF_LINK; 244 bcopy(ifp->if_xname, sdl->sdl_data, namelen); 245 sdl->sdl_nlen = namelen; 246 sdl->sdl_alen = ifp->if_addrlen; 247 sdl->sdl_index = ifp->if_index; 248 sdl->sdl_type = ifp->if_type; 249 ifnet_addrs[if_index] = ifa; 250 ifa->ifa_ifp = ifp; 251 ifa->ifa_rtrequest = link_rtrequest; 252 TAILQ_INSERT_HEAD(&ifp->if_addrlist, ifa, ifa_list); 253 ifa->ifa_addr = (struct sockaddr *)sdl; 254 ifp->if_sadl = sdl; 255 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 256 ifa->ifa_netmask = (struct sockaddr *)sdl; 257 sdl->sdl_len = masklen; 258 while (namelen != 0) 259 sdl->sdl_data[--namelen] = 0xff; 260 } 261 262 /* 263 * Free the link level name for the specified interface. This is 264 * a detach helper. This is called from if_detach() or from 265 * link layer type specific detach functions. 266 */ 267 void 268 if_free_sadl(ifp) 269 struct ifnet *ifp; 270 { 271 struct ifaddr *ifa; 272 int s; 273 274 ifa = ifnet_addrs[ifp->if_index]; 275 if (ifa == NULL) 276 return; 277 278 s = splnet(); 279 rtinit(ifa, RTM_DELETE, 0); 280 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list); 281 282 ifp->if_sadl = NULL; 283 284 ifnet_addrs[ifp->if_index] = NULL; 285 splx(s); 286 } 287 288 void 289 if_attachdomain() 290 { 291 struct ifnet *ifp; 292 int s; 293 294 s = splnet(); 295 for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) 296 if_attachdomain1(ifp); 297 splx(s); 298 } 299 300 void 301 if_attachdomain1(ifp) 302 struct ifnet *ifp; 303 { 304 struct domain *dp; 305 int s; 306 307 s = splnet(); 308 309 /* address family dependent data region */ 310 bzero(ifp->if_afdata, sizeof(ifp->if_afdata)); 311 for (dp = domains; dp; dp = dp->dom_next) { 312 if (dp->dom_ifattach) 313 ifp->if_afdata[dp->dom_family] = 314 (*dp->dom_ifattach)(ifp); 315 } 316 317 splx(s); 318 } 319 320 void 321 if_attachhead(ifp) 322 struct ifnet *ifp; 323 { 324 if (if_index == 0) 325 TAILQ_INIT(&ifnet); 326 TAILQ_INIT(&ifp->if_addrlist); 327 ifp->if_addrhooks = malloc(sizeof(*ifp->if_addrhooks), M_TEMP, M_NOWAIT); 328 if (ifp->if_addrhooks == NULL) 329 panic("if_attachhead: malloc"); 330 TAILQ_INIT(ifp->if_addrhooks); 331 TAILQ_INSERT_HEAD(&ifnet, ifp, if_list); 332 if_attachsetup(ifp); 333 } 334 335 void 336 if_attach(ifp) 337 struct ifnet *ifp; 338 { 339 if (if_index == 0) 340 TAILQ_INIT(&ifnet); 341 TAILQ_INIT(&ifp->if_addrlist); 342 ifp->if_addrhooks = malloc(sizeof(*ifp->if_addrhooks), M_TEMP, M_NOWAIT); 343 if (ifp->if_addrhooks == NULL) 344 panic("if_attach: malloc"); 345 TAILQ_INIT(ifp->if_addrhooks); 346 TAILQ_INSERT_TAIL(&ifnet, ifp, if_list); 347 if_attachsetup(ifp); 348 } 349 350 /* 351 * Delete a route if it has a specific interface for output. 352 * This function complies to the rn_walktree callback API. 353 */ 354 int 355 if_detach_rtdelete(rn, vifp) 356 struct radix_node *rn; 357 void *vifp; 358 { 359 struct ifnet *ifp = vifp; 360 struct rtentry *rt = (struct rtentry *)rn; 361 362 if (rt->rt_ifp == ifp) 363 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, rt_mask(rt), 364 0, NULL); 365 366 /* 367 * XXX There should be no need to check for rt_ifa belonging to this 368 * interface, because then rt_ifp is set, right? 369 */ 370 371 return (0); 372 } 373 374 int 375 if_mark_ignore(rn, vifp) 376 struct radix_node *rn; 377 void *vifp; 378 { 379 struct ifnet *ifp = vifp; 380 struct rtentry *rt = (struct rtentry *)rn; 381 382 if (rt->rt_ifp == ifp) 383 rn->rn_flags |= RNF_IGNORE; 384 385 return (0); 386 } 387 388 int 389 if_mark_unignore(rn, vifp) 390 struct radix_node *rn; 391 void *vifp; 392 { 393 struct ifnet *ifp = vifp; 394 struct rtentry *rt = (struct rtentry *)rn; 395 396 if (rt->rt_ifp == ifp) 397 rn->rn_flags &= ~RNF_IGNORE; 398 399 return (0); 400 } 401 402 /* 403 * Detach an interface from everything in the kernel. Also deallocate 404 * private resources. 405 * XXX So far only the INET protocol family has been looked over 406 * wrt resource usage that needs to be decoupled. 407 */ 408 void 409 if_detach(ifp) 410 struct ifnet *ifp; 411 { 412 struct ifaddr *ifa; 413 int i, s = splimp(); 414 struct radix_node_head *rnh; 415 struct domain *dp; 416 417 ifp->if_flags &= ~IFF_OACTIVE; 418 ifp->if_start = if_detached_start; 419 ifp->if_ioctl = if_detached_ioctl; 420 ifp->if_watchdog = if_detached_watchdog; 421 422 #if NBRIDGE > 0 423 /* Remove the interface from any bridge it is part of. */ 424 if (ifp->if_bridge) 425 bridge_ifdetach(ifp); 426 #endif 427 428 #if NBPFILTER > 0 429 /* If there is a bpf device attached, detach from it. */ 430 if (ifp->if_bpf) 431 bpfdetach(ifp); 432 #endif 433 #ifdef ALTQ 434 if (ALTQ_IS_ENABLED(&ifp->if_snd)) 435 altq_disable(&ifp->if_snd); 436 if (ALTQ_IS_ATTACHED(&ifp->if_snd)) 437 altq_detach(&ifp->if_snd); 438 #endif 439 440 /* 441 * Find and remove all routes which is using this interface. 442 * XXX Factor out into a route.c function? 443 */ 444 for (i = 1; i <= AF_MAX; i++) { 445 rnh = rt_tables[i]; 446 if (rnh) 447 (*rnh->rnh_walktree)(rnh, if_detach_rtdelete, ifp); 448 } 449 450 #ifdef INET 451 rti_delete(ifp); 452 #if NETHER > 0 453 myip_ifp = NULL; 454 #endif 455 #ifdef MROUTING 456 vif_delete(ifp); 457 #endif 458 #endif 459 #ifdef INET6 460 in6_ifdetach(ifp); 461 #endif 462 463 /* 464 * remove packets came from ifp, from software interrupt queues. 465 * net/netisr_dispatch.h is not usable, as some of them use 466 * strange queue names. 467 */ 468 #define IF_DETACH_QUEUES(x) \ 469 do { \ 470 extern struct ifqueue x; \ 471 if_detach_queues(ifp, & x); \ 472 } while (0) 473 #ifdef INET 474 IF_DETACH_QUEUES(arpintrq); 475 IF_DETACH_QUEUES(ipintrq); 476 #endif 477 #ifdef INET6 478 IF_DETACH_QUEUES(ip6intrq); 479 #endif 480 #ifdef IPX 481 IF_DETACH_QUEUES(ipxintrq); 482 #endif 483 #ifdef NS 484 IF_DETACH_QUEUES(nsintrq); 485 #endif 486 #ifdef NETATALK 487 IF_DETACH_QUEUES(atintrq1); 488 IF_DETACH_QUEUES(atintrq2); 489 #endif 490 #ifdef ISO 491 IF_DETACH_QUEUES(clnlintrq); 492 #endif 493 #ifdef CCITT 494 IF_DETACH_QUEUES(llcintrq); 495 #endif 496 #ifdef NATM 497 IF_DETACH_QUEUES(natmintrq); 498 #endif 499 #ifdef DECNET 500 IF_DETACH_QUEUES(decnetintrq); 501 #endif 502 #undef IF_DETACH_QUEUES 503 504 /* 505 * XXX transient ifp refs? inpcb.ip_moptions.imo_multicast_ifp? 506 * Other network stacks than INET? 507 */ 508 509 /* Remove the interface from the list of all interfaces. */ 510 TAILQ_REMOVE(&ifnet, ifp, if_list); 511 512 /* 513 * Deallocate private resources. 514 * XXX should consult refcnt and use IFAFREE 515 */ 516 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); ifa; 517 ifa = TAILQ_FIRST(&ifp->if_addrlist)) { 518 TAILQ_REMOVE(&ifp->if_addrlist, ifa, ifa_list); 519 #ifdef INET 520 if (ifa->ifa_addr->sa_family == AF_INET) 521 TAILQ_REMOVE(&in_ifaddr, (struct in_ifaddr *)ifa, 522 ia_list); 523 #endif 524 free(ifa, M_IFADDR); 525 } 526 ifp->if_sadl = NULL; 527 ifnet_addrs[ifp->if_index] = NULL; 528 529 free(ifp->if_addrhooks, M_TEMP); 530 531 for (dp = domains; dp; dp = dp->dom_next) { 532 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) 533 (*dp->dom_ifdetach)(ifp, 534 ifp->if_afdata[dp->dom_family]); 535 } 536 537 splx(s); 538 } 539 540 void 541 if_detach_queues(ifp, q) 542 struct ifnet *ifp; 543 struct ifqueue *q; 544 { 545 struct mbuf *m, *prev, *next; 546 547 prev = NULL; 548 for (m = q->ifq_head; m; m = next) { 549 next = m->m_nextpkt; 550 #ifdef DIAGNOSTIC 551 if ((m->m_flags & M_PKTHDR) == 0) { 552 prev = m; 553 continue; 554 } 555 #endif 556 if (m->m_pkthdr.rcvif != ifp) { 557 prev = m; 558 continue; 559 } 560 561 if (prev) 562 prev->m_nextpkt = m->m_nextpkt; 563 else 564 q->ifq_head = m->m_nextpkt; 565 if (q->ifq_tail == m) 566 q->ifq_tail = prev; 567 q->ifq_len--; 568 569 m->m_nextpkt = NULL; 570 m_freem(m); 571 IF_DROP(q); 572 } 573 } 574 575 /* 576 * Locate an interface based on a complete address. 577 */ 578 /*ARGSUSED*/ 579 struct ifaddr * 580 ifa_ifwithaddr(addr) 581 register struct sockaddr *addr; 582 { 583 register struct ifnet *ifp; 584 register struct ifaddr *ifa; 585 586 #define equal(a1, a2) \ 587 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0) 588 TAILQ_FOREACH(ifp, &ifnet, if_list) { 589 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 590 if (ifa->ifa_addr->sa_family != addr->sa_family) 591 continue; 592 if (equal(addr, ifa->ifa_addr)) 593 return (ifa); 594 if ((ifp->if_flags & IFF_BROADCAST) && ifa->ifa_broadaddr && 595 /* IP6 doesn't have broadcast */ 596 ifa->ifa_broadaddr->sa_len != 0 && 597 equal(ifa->ifa_broadaddr, addr)) 598 return (ifa); 599 } 600 } 601 return (NULL); 602 } 603 /* 604 * Locate the point to point interface with a given destination address. 605 */ 606 /*ARGSUSED*/ 607 struct ifaddr * 608 ifa_ifwithdstaddr(addr) 609 register struct sockaddr *addr; 610 { 611 register struct ifnet *ifp; 612 register struct ifaddr *ifa; 613 614 TAILQ_FOREACH(ifp, &ifnet, if_list) { 615 if (ifp->if_flags & IFF_POINTOPOINT) 616 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 617 if (ifa->ifa_addr->sa_family != addr->sa_family || 618 ifa->ifa_dstaddr == NULL) 619 continue; 620 if (equal(addr, ifa->ifa_dstaddr)) 621 return (ifa); 622 } 623 } 624 return (NULL); 625 } 626 627 /* 628 * Find an interface on a specific network. If many, choice 629 * is most specific found. 630 */ 631 struct ifaddr * 632 ifa_ifwithnet(addr) 633 struct sockaddr *addr; 634 { 635 register struct ifnet *ifp; 636 register struct ifaddr *ifa; 637 struct ifaddr *ifa_maybe = 0; 638 u_int af = addr->sa_family; 639 char *addr_data = addr->sa_data, *cplim; 640 641 if (af == AF_LINK) { 642 register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; 643 if (sdl->sdl_index && sdl->sdl_index <= if_index) 644 return (ifnet_addrs[sdl->sdl_index]); 645 } 646 TAILQ_FOREACH(ifp, &ifnet, if_list) { 647 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 648 register char *cp, *cp2, *cp3; 649 650 if (ifa->ifa_addr->sa_family != af || 651 ifa->ifa_netmask == 0) 652 next: continue; 653 cp = addr_data; 654 cp2 = ifa->ifa_addr->sa_data; 655 cp3 = ifa->ifa_netmask->sa_data; 656 cplim = (char *)ifa->ifa_netmask + 657 ifa->ifa_netmask->sa_len; 658 while (cp3 < cplim) 659 if ((*cp++ ^ *cp2++) & *cp3++) 660 /* want to continue for() loop */ 661 goto next; 662 if (ifa_maybe == 0 || 663 rn_refines((caddr_t)ifa->ifa_netmask, 664 (caddr_t)ifa_maybe->ifa_netmask)) 665 ifa_maybe = ifa; 666 } 667 } 668 return (ifa_maybe); 669 } 670 671 /* 672 * Find an interface using a specific address family 673 */ 674 struct ifaddr * 675 ifa_ifwithaf(af) 676 register int af; 677 { 678 register struct ifnet *ifp; 679 register struct ifaddr *ifa; 680 681 TAILQ_FOREACH(ifp, &ifnet, if_list) { 682 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 683 if (ifa->ifa_addr->sa_family == af) 684 return (ifa); 685 } 686 } 687 return (NULL); 688 } 689 690 /* 691 * Find an interface address specific to an interface best matching 692 * a given address. 693 */ 694 struct ifaddr * 695 ifaof_ifpforaddr(addr, ifp) 696 struct sockaddr *addr; 697 register struct ifnet *ifp; 698 { 699 register struct ifaddr *ifa; 700 register char *cp, *cp2, *cp3; 701 register char *cplim; 702 struct ifaddr *ifa_maybe = 0; 703 u_int af = addr->sa_family; 704 705 if (af >= AF_MAX) 706 return (NULL); 707 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 708 if (ifa->ifa_addr->sa_family != af) 709 continue; 710 ifa_maybe = ifa; 711 if (ifa->ifa_netmask == 0) { 712 if (equal(addr, ifa->ifa_addr) || 713 (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))) 714 return (ifa); 715 continue; 716 } 717 cp = addr->sa_data; 718 cp2 = ifa->ifa_addr->sa_data; 719 cp3 = ifa->ifa_netmask->sa_data; 720 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 721 for (; cp3 < cplim; cp3++) 722 if ((*cp++ ^ *cp2++) & *cp3) 723 break; 724 if (cp3 == cplim) 725 return (ifa); 726 } 727 return (ifa_maybe); 728 } 729 730 /* 731 * Default action when installing a route with a Link Level gateway. 732 * Lookup an appropriate real ifa to point to. 733 * This should be moved to /sys/net/link.c eventually. 734 */ 735 void 736 link_rtrequest(cmd, rt, info) 737 int cmd; 738 register struct rtentry *rt; 739 struct rt_addrinfo *info; 740 { 741 register struct ifaddr *ifa; 742 struct sockaddr *dst; 743 struct ifnet *ifp; 744 745 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 746 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 747 return; 748 if ((ifa = ifaof_ifpforaddr(dst, ifp)) != NULL) { 749 IFAFREE(rt->rt_ifa); 750 rt->rt_ifa = ifa; 751 ifa->ifa_refcnt++; 752 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 753 ifa->ifa_rtrequest(cmd, rt, info); 754 } 755 } 756 757 /* 758 * Mark an interface down and notify protocols of 759 * the transition. 760 * NOTE: must be called at splsoftnet or equivalent. 761 */ 762 void 763 if_down(struct ifnet *ifp) 764 { 765 struct ifaddr *ifa; 766 struct radix_node_head *rnh; 767 int i; 768 769 splassert(IPL_SOFTNET); 770 771 ifp->if_flags &= ~IFF_UP; 772 microtime(&ifp->if_lastchange); 773 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 774 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 775 } 776 IFQ_PURGE(&ifp->if_snd); 777 rt_ifmsg(ifp); 778 779 /* 780 * Find and mark as ignore all routes which are using this interface. 781 * XXX Factor out into a route.c function? 782 */ 783 for (i = 1; i <= AF_MAX; i++) { 784 rnh = rt_tables[i]; 785 if (rnh) 786 (*rnh->rnh_walktree)(rnh, if_mark_ignore, ifp); 787 } 788 } 789 790 /* 791 * Mark an interface up and notify protocols of 792 * the transition. 793 * NOTE: must be called at splsoftnet or equivalent. 794 */ 795 void 796 if_up(struct ifnet *ifp) 797 { 798 #ifdef notyet 799 struct ifaddr *ifa; 800 #endif 801 struct radix_node_head *rnh; 802 int i; 803 804 splassert(IPL_SOFTNET); 805 806 ifp->if_flags |= IFF_UP; 807 microtime(&ifp->if_lastchange); 808 #ifdef notyet 809 /* this has no effect on IP, and will kill all ISO connections XXX */ 810 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 811 pfctlinput(PRC_IFUP, ifa->ifa_addr); 812 } 813 #endif 814 rt_ifmsg(ifp); 815 #ifdef INET6 816 in6_if_up(ifp); 817 #endif 818 819 /* 820 * Find and unignore all routes which are using this interface. 821 * XXX Factor out into a route.c function? 822 */ 823 for (i = 1; i <= AF_MAX; i++) { 824 rnh = rt_tables[i]; 825 if (rnh) 826 (*rnh->rnh_walktree)(rnh, if_mark_unignore, ifp); 827 } 828 } 829 830 /* 831 * Flush an interface queue. 832 */ 833 void 834 if_qflush(ifq) 835 register struct ifqueue *ifq; 836 { 837 register struct mbuf *m, *n; 838 839 n = ifq->ifq_head; 840 while ((m = n) != NULL) { 841 n = m->m_act; 842 m_freem(m); 843 } 844 ifq->ifq_head = 0; 845 ifq->ifq_tail = 0; 846 ifq->ifq_len = 0; 847 } 848 849 /* 850 * Handle interface watchdog timer routines. Called 851 * from softclock, we decrement timers (if set) and 852 * call the appropriate interface routine on expiration. 853 */ 854 void 855 if_slowtimo(arg) 856 void *arg; 857 { 858 struct timeout *to = (struct timeout *)arg; 859 struct ifnet *ifp; 860 int s = splimp(); 861 862 TAILQ_FOREACH(ifp, &ifnet, if_list) { 863 if (ifp->if_timer == 0 || --ifp->if_timer) 864 continue; 865 if (ifp->if_watchdog) 866 (*ifp->if_watchdog)(ifp); 867 } 868 splx(s); 869 timeout_add(to, hz / IFNET_SLOWHZ); 870 } 871 872 /* 873 * Map interface name to 874 * interface structure pointer. 875 */ 876 struct ifnet * 877 ifunit(name) 878 register char *name; 879 { 880 register struct ifnet *ifp; 881 882 TAILQ_FOREACH(ifp, &ifnet, if_list) { 883 if (strcmp(ifp->if_xname, name) == 0) 884 return (ifp); 885 } 886 return (NULL); 887 } 888 889 /* 890 * Interface ioctls. 891 */ 892 int 893 ifioctl(so, cmd, data, p) 894 struct socket *so; 895 u_long cmd; 896 caddr_t data; 897 struct proc *p; 898 { 899 register struct ifnet *ifp; 900 register struct ifreq *ifr; 901 int error = 0; 902 short oif_flags; 903 904 switch (cmd) { 905 906 case SIOCGIFCONF: 907 case OSIOCGIFCONF: 908 return (ifconf(cmd, data)); 909 } 910 ifr = (struct ifreq *)data; 911 ifp = ifunit(ifr->ifr_name); 912 if (ifp == 0) 913 return (ENXIO); 914 oif_flags = ifp->if_flags; 915 switch (cmd) { 916 917 case SIOCGIFFLAGS: 918 ifr->ifr_flags = ifp->if_flags; 919 break; 920 921 case SIOCGIFMETRIC: 922 ifr->ifr_metric = ifp->if_metric; 923 break; 924 925 case SIOCGIFMTU: 926 ifr->ifr_mtu = ifp->if_mtu; 927 break; 928 929 case SIOCGIFDATA: 930 error = copyout((caddr_t)&ifp->if_data, ifr->ifr_data, 931 sizeof(ifp->if_data)); 932 break; 933 934 case SIOCSIFFLAGS: 935 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 936 return (error); 937 if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) { 938 int s = splimp(); 939 if_down(ifp); 940 splx(s); 941 } 942 if (ifr->ifr_flags & IFF_UP && (ifp->if_flags & IFF_UP) == 0) { 943 int s = splimp(); 944 if_up(ifp); 945 splx(s); 946 } 947 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 948 (ifr->ifr_flags &~ IFF_CANTCHANGE); 949 if (ifp->if_ioctl) 950 (void) (*ifp->if_ioctl)(ifp, cmd, data); 951 break; 952 953 case SIOCSIFMETRIC: 954 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 955 return (error); 956 ifp->if_metric = ifr->ifr_metric; 957 break; 958 959 case SIOCSIFMTU: 960 { 961 #ifdef INET6 962 int oldmtu = ifp->if_mtu; 963 #endif 964 965 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 966 return (error); 967 if (ifp->if_ioctl == NULL) 968 return (EOPNOTSUPP); 969 error = (*ifp->if_ioctl)(ifp, cmd, data); 970 971 /* 972 * If the link MTU changed, do network layer specific procedure. 973 */ 974 #ifdef INET6 975 if (ifp->if_mtu != oldmtu) 976 nd6_setmtu(ifp); 977 #endif 978 break; 979 } 980 981 case SIOCSIFPHYADDR: 982 case SIOCDIFPHYADDR: 983 #ifdef INET6 984 case SIOCSIFPHYADDR_IN6: 985 #endif 986 case SIOCSLIFPHYADDR: 987 case SIOCADDMULTI: 988 case SIOCDELMULTI: 989 case SIOCSIFMEDIA: 990 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) 991 return (error); 992 /* FALLTHROUGH */ 993 case SIOCGIFPSRCADDR: 994 case SIOCGIFPDSTADDR: 995 case SIOCGLIFPHYADDR: 996 case SIOCGIFMEDIA: 997 if (ifp->if_ioctl == 0) 998 return (EOPNOTSUPP); 999 error = (*ifp->if_ioctl)(ifp, cmd, data); 1000 break; 1001 1002 default: 1003 if (so->so_proto == 0) 1004 return (EOPNOTSUPP); 1005 #if !defined(COMPAT_43) && !defined(COMPAT_LINUX) && !defined(COMPAT_SVR4) 1006 error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL, 1007 (struct mbuf *) cmd, (struct mbuf *) data, 1008 (struct mbuf *) ifp)); 1009 #else 1010 { 1011 u_long ocmd = cmd; 1012 1013 switch (cmd) { 1014 1015 case SIOCSIFADDR: 1016 case SIOCSIFDSTADDR: 1017 case SIOCSIFBRDADDR: 1018 case SIOCSIFNETMASK: 1019 #if BYTE_ORDER != BIG_ENDIAN 1020 if (ifr->ifr_addr.sa_family == 0 && 1021 ifr->ifr_addr.sa_len < 16) { 1022 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 1023 ifr->ifr_addr.sa_len = 16; 1024 } 1025 #else 1026 if (ifr->ifr_addr.sa_len == 0) 1027 ifr->ifr_addr.sa_len = 16; 1028 #endif 1029 break; 1030 1031 case OSIOCGIFADDR: 1032 cmd = SIOCGIFADDR; 1033 break; 1034 1035 case OSIOCGIFDSTADDR: 1036 cmd = SIOCGIFDSTADDR; 1037 break; 1038 1039 case OSIOCGIFBRDADDR: 1040 cmd = SIOCGIFBRDADDR; 1041 break; 1042 1043 case OSIOCGIFNETMASK: 1044 cmd = SIOCGIFNETMASK; 1045 } 1046 error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL, 1047 (struct mbuf *) cmd, (struct mbuf *) data, 1048 (struct mbuf *) ifp)); 1049 switch (ocmd) { 1050 1051 case OSIOCGIFADDR: 1052 case OSIOCGIFDSTADDR: 1053 case OSIOCGIFBRDADDR: 1054 case OSIOCGIFNETMASK: 1055 *(u_int16_t *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 1056 } 1057 1058 } 1059 #endif 1060 break; 1061 } 1062 1063 if (((oif_flags ^ ifp->if_flags) & IFF_UP) != 0) { 1064 #ifdef INET6 1065 if ((ifp->if_flags & IFF_UP) != 0) { 1066 int s = splnet(); 1067 in6_if_up(ifp); 1068 splx(s); 1069 } 1070 #endif 1071 } 1072 return (error); 1073 } 1074 1075 /* 1076 * Return interface configuration 1077 * of system. List may be used 1078 * in later ioctl's (above) to get 1079 * other information. 1080 */ 1081 /*ARGSUSED*/ 1082 int 1083 ifconf(cmd, data) 1084 u_long cmd; 1085 caddr_t data; 1086 { 1087 register struct ifconf *ifc = (struct ifconf *)data; 1088 register struct ifnet *ifp; 1089 register struct ifaddr *ifa; 1090 struct ifreq ifr, *ifrp; 1091 int space = ifc->ifc_len, error = 0; 1092 1093 /* If ifc->ifc_len is 0, fill it in with the needed size and return. */ 1094 if (space == 0) { 1095 TAILQ_FOREACH(ifp, &ifnet, if_list) { 1096 register struct sockaddr *sa; 1097 1098 if (TAILQ_EMPTY(&ifp->if_addrlist)) 1099 space += sizeof (ifr); 1100 else 1101 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 1102 sa = ifa->ifa_addr; 1103 #if defined(COMPAT_43) || defined(COMPAT_LINUX) || defined(COMPAT_SVR4) 1104 if (cmd != OSIOCGIFCONF) 1105 #endif 1106 if (sa->sa_len > sizeof(*sa)) 1107 space += sa->sa_len - 1108 sizeof (*sa); 1109 space += sizeof (ifr); 1110 } 1111 } 1112 ifc->ifc_len = space; 1113 return (0); 1114 } 1115 1116 ifrp = ifc->ifc_req; 1117 for (ifp = TAILQ_FIRST(&ifnet); space >= sizeof(ifr) && 1118 ifp != TAILQ_END(&ifnet); ifp = TAILQ_NEXT(ifp, if_list)) { 1119 bcopy(ifp->if_xname, ifr.ifr_name, IFNAMSIZ); 1120 if (TAILQ_EMPTY(&ifp->if_addrlist)) { 1121 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 1122 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 1123 sizeof(ifr)); 1124 if (error) 1125 break; 1126 space -= sizeof (ifr), ifrp++; 1127 } else 1128 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); 1129 space >= sizeof (ifr) && 1130 ifa != TAILQ_END(&ifp->if_addrlist); 1131 ifa = TAILQ_NEXT(ifa, ifa_list)) { 1132 register struct sockaddr *sa = ifa->ifa_addr; 1133 #if defined(COMPAT_43) || defined(COMPAT_LINUX) || defined(COMPAT_SVR4) 1134 if (cmd == OSIOCGIFCONF) { 1135 struct osockaddr *osa = 1136 (struct osockaddr *)&ifr.ifr_addr; 1137 ifr.ifr_addr = *sa; 1138 osa->sa_family = sa->sa_family; 1139 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 1140 sizeof (ifr)); 1141 ifrp++; 1142 } else 1143 #endif 1144 if (sa->sa_len <= sizeof(*sa)) { 1145 ifr.ifr_addr = *sa; 1146 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 1147 sizeof (ifr)); 1148 ifrp++; 1149 } else { 1150 space -= sa->sa_len - sizeof(*sa); 1151 if (space < sizeof (ifr)) 1152 break; 1153 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 1154 sizeof (ifr.ifr_name)); 1155 if (error == 0) 1156 error = copyout((caddr_t)sa, 1157 (caddr_t)&ifrp->ifr_addr, 1158 sa->sa_len); 1159 ifrp = (struct ifreq *)(sa->sa_len + 1160 (caddr_t)&ifrp->ifr_addr); 1161 } 1162 if (error) 1163 break; 1164 space -= sizeof (ifr); 1165 } 1166 } 1167 ifc->ifc_len -= space; 1168 return (error); 1169 } 1170 1171 /* 1172 * Dummy functions replaced in ifnet during detach (if protocols decide to 1173 * fiddle with the if during detach. 1174 */ 1175 void 1176 if_detached_start(struct ifnet *ifp) 1177 { 1178 struct mbuf *m; 1179 1180 while (1) { 1181 IF_DEQUEUE(&ifp->if_snd, m); 1182 1183 if (m == NULL) 1184 return; 1185 m_freem(m); 1186 } 1187 } 1188 1189 int 1190 if_detached_ioctl(struct ifnet *ifp, u_long a, caddr_t b) 1191 { 1192 return ENODEV; 1193 } 1194 1195 void 1196 if_detached_watchdog(struct ifnet *ifp) 1197 { 1198 /* nothing */ 1199 } 1200 1201 /* 1202 * Set/clear promiscuous mode on interface ifp based on the truth value 1203 * of pswitch. The calls are reference counted so that only the first 1204 * "on" request actually has an effect, as does the final "off" request. 1205 * Results are undefined if the "off" and "on" requests are not matched. 1206 */ 1207 int 1208 ifpromisc(ifp, pswitch) 1209 struct ifnet *ifp; 1210 int pswitch; 1211 { 1212 struct ifreq ifr; 1213 1214 if (pswitch) { 1215 /* 1216 * If the device is not configured up, we cannot put it in 1217 * promiscuous mode. 1218 */ 1219 if ((ifp->if_flags & IFF_UP) == 0) 1220 return (ENETDOWN); 1221 if (ifp->if_pcount++ != 0) 1222 return (0); 1223 ifp->if_flags |= IFF_PROMISC; 1224 } else { 1225 if (--ifp->if_pcount > 0) 1226 return (0); 1227 ifp->if_flags &= ~IFF_PROMISC; 1228 /* 1229 * If the device is not configured up, we should not need to 1230 * turn off promiscuous mode (device should have turned it 1231 * off when interface went down; and will look at IFF_PROMISC 1232 * again next time interface comes up). 1233 */ 1234 if ((ifp->if_flags & IFF_UP) == 0) 1235 return (0); 1236 } 1237 ifr.ifr_flags = ifp->if_flags; 1238 return ((*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr)); 1239 } 1240