1 /* 2 * Copyright (c) 1980, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)if.c 8.3 (Berkeley) 1/4/94 34 * $FreeBSD$ 35 */ 36 37 #include "opt_compat.h" 38 #include "opt_inet6.h" 39 #include "opt_inet.h" 40 41 #include <sys/param.h> 42 #include <sys/malloc.h> 43 #include <sys/mbuf.h> 44 #include <sys/systm.h> 45 #include <sys/proc.h> 46 #include <sys/socket.h> 47 #include <sys/socketvar.h> 48 #include <sys/protosw.h> 49 #include <sys/kernel.h> 50 #include <sys/sockio.h> 51 #include <sys/syslog.h> 52 #include <sys/sysctl.h> 53 #include <sys/jail.h> 54 55 #include <net/if.h> 56 #include <net/if_arp.h> 57 #include <net/if_dl.h> 58 #include <net/if_types.h> 59 #include <net/if_var.h> 60 #include <net/radix.h> 61 #include <net/route.h> 62 63 #if defined(INET) || defined(INET6) 64 /*XXX*/ 65 #include <netinet/in.h> 66 #include <netinet/in_var.h> 67 #ifdef INET6 68 #include <netinet6/in6_var.h> 69 #include <netinet6/in6_ifattach.h> 70 #endif 71 #endif 72 73 static int ifconf(u_long, caddr_t); 74 static void if_grow(void); 75 static void if_init(void *); 76 static void if_check(void *); 77 static void if_qflush(struct ifqueue *); 78 static void if_slowtimo(void *); 79 static void link_rtrequest(int, struct rtentry *, struct sockaddr *); 80 static int if_rtdel(struct radix_node *, void *); 81 static struct if_clone *if_clone_lookup(const char *, int *); 82 static int if_clone_list(struct if_clonereq *); 83 #ifdef INET6 84 /* 85 * XXX: declare here to avoid to include many inet6 related files.. 86 * should be more generalized? 87 */ 88 extern void nd6_setmtu __P((struct ifnet *)); 89 #endif 90 91 int if_index = 0; 92 struct ifindex_entry *ifindex_table = NULL; 93 int ifqmaxlen = IFQ_MAXLEN; 94 struct ifnethead ifnet; /* depend on static init XXX */ 95 int if_cloners_count; 96 LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners); 97 98 static int if_indexlim = 8; 99 100 /* 101 * System initialization 102 */ 103 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL) 104 SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL) 105 106 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 107 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 108 109 /* 110 * Network interface utility routines. 111 * 112 * Routines with ifa_ifwith* names take sockaddr *'s as 113 * parameters. 114 */ 115 /* ARGSUSED*/ 116 static void 117 if_init(dummy) 118 void *dummy; 119 { 120 121 TAILQ_INIT(&ifnet); 122 if_grow(); /* create initial table */ 123 } 124 125 static void 126 if_grow(void) 127 { 128 u_int n; 129 struct ifindex_entry *e; 130 131 if_indexlim <<= 1; 132 n = if_indexlim * sizeof(*e); 133 e = malloc(n, M_IFADDR, M_WAITOK | M_ZERO); 134 if (ifindex_table != NULL) { 135 memcpy((caddr_t)e, (caddr_t)ifindex_table, n/2); 136 free((caddr_t)ifindex_table, M_IFADDR); 137 } 138 ifindex_table = e; 139 } 140 141 /* ARGSUSED*/ 142 static void 143 if_check(dummy) 144 void *dummy; 145 { 146 struct ifnet *ifp; 147 int s; 148 149 s = splimp(); 150 TAILQ_FOREACH(ifp, &ifnet, if_link) { 151 if (ifp->if_snd.ifq_maxlen == 0) { 152 printf("%s%d XXX: driver didn't set ifq_maxlen\n", 153 ifp->if_name, ifp->if_unit); 154 ifp->if_snd.ifq_maxlen = ifqmaxlen; 155 } 156 if (!mtx_initialized(&ifp->if_snd.ifq_mtx)) { 157 printf("%s%d XXX: driver didn't initialize queue mtx\n", 158 ifp->if_name, ifp->if_unit); 159 mtx_init(&ifp->if_snd.ifq_mtx, "unknown", MTX_DEF); 160 } 161 } 162 splx(s); 163 if_slowtimo(0); 164 } 165 166 /* 167 * Attach an interface to the 168 * list of "active" interfaces. 169 */ 170 void 171 if_attach(ifp) 172 struct ifnet *ifp; 173 { 174 unsigned socksize, ifasize; 175 int namelen, masklen; 176 char workbuf[64]; 177 register struct sockaddr_dl *sdl; 178 register struct ifaddr *ifa; 179 180 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link); 181 ifp->if_index = ++if_index; 182 /* 183 * XXX - 184 * The old code would work if the interface passed a pre-existing 185 * chain of ifaddrs to this code. We don't trust our callers to 186 * properly initialize the tailq, however, so we no longer allow 187 * this unlikely case. 188 */ 189 TAILQ_INIT(&ifp->if_addrhead); 190 TAILQ_INIT(&ifp->if_prefixhead); 191 TAILQ_INIT(&ifp->if_multiaddrs); 192 getmicrotime(&ifp->if_lastchange); 193 if (if_index >= if_indexlim) 194 if_grow(); 195 196 ifnet_byindex(if_index) = ifp; 197 mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_name, MTX_DEF); 198 199 /* 200 * create a Link Level name for this device 201 */ 202 namelen = snprintf(workbuf, sizeof(workbuf), 203 "%s%d", ifp->if_name, ifp->if_unit); 204 #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m)) 205 masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen; 206 socksize = masklen + ifp->if_addrlen; 207 #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1))) 208 if (socksize < sizeof(*sdl)) 209 socksize = sizeof(*sdl); 210 socksize = ROUNDUP(socksize); 211 ifasize = sizeof(*ifa) + 2 * socksize; 212 ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO); 213 if (ifa) { 214 sdl = (struct sockaddr_dl *)(ifa + 1); 215 sdl->sdl_len = socksize; 216 sdl->sdl_family = AF_LINK; 217 bcopy(workbuf, sdl->sdl_data, namelen); 218 sdl->sdl_nlen = namelen; 219 sdl->sdl_index = ifp->if_index; 220 sdl->sdl_type = ifp->if_type; 221 ifaddr_byindex(if_index) = ifa; 222 ifa->ifa_ifp = ifp; 223 ifa->ifa_rtrequest = link_rtrequest; 224 ifa->ifa_addr = (struct sockaddr *)sdl; 225 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 226 ifa->ifa_netmask = (struct sockaddr *)sdl; 227 sdl->sdl_len = masklen; 228 while (namelen != 0) 229 sdl->sdl_data[--namelen] = 0xff; 230 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link); 231 } 232 } 233 234 /* 235 * Detach an interface, removing it from the 236 * list of "active" interfaces. 237 */ 238 void 239 if_detach(ifp) 240 struct ifnet *ifp; 241 { 242 struct ifaddr *ifa; 243 struct radix_node_head *rnh; 244 int s; 245 int i; 246 247 /* 248 * Remove routes and flush queues. 249 */ 250 s = splnet(); 251 if_down(ifp); 252 253 /* 254 * Remove address from ifindex_table[] and maybe decrement if_index. 255 * Clean up all addresses. 256 */ 257 ifaddr_byindex(ifp->if_index) = NULL; 258 259 while (if_index > 0 && ifaddr_byindex(if_index) == NULL) 260 if_index--; 261 262 for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa; 263 ifa = TAILQ_FIRST(&ifp->if_addrhead)) { 264 #ifdef INET 265 /* XXX: Ugly!! ad hoc just for INET */ 266 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { 267 struct ifaliasreq ifr; 268 269 bzero(&ifr, sizeof(ifr)); 270 ifr.ifra_addr = *ifa->ifa_addr; 271 if (ifa->ifa_dstaddr) 272 ifr.ifra_broadaddr = *ifa->ifa_dstaddr; 273 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp, 274 NULL) == 0) 275 continue; 276 } 277 #endif /* INET */ 278 #ifdef INET6 279 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) { 280 in6_purgeaddr(ifa); 281 /* ifp_addrhead is already updated */ 282 continue; 283 } 284 #endif /* INET6 */ 285 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 286 IFAFREE(ifa); 287 } 288 289 #ifdef INET6 290 /* 291 * Remove all IPv6 kernel structs related to ifp. This should be done 292 * before removing routing entries below, since IPv6 interface direct 293 * routes are expected to be removed by the IPv6-specific kernel API. 294 * Otherwise, the kernel will detect some inconsistency and bark it. 295 */ 296 in6_ifdetach(ifp); 297 #endif 298 299 /* 300 * Delete all remaining routes using this interface 301 * Unfortuneatly the only way to do this is to slog through 302 * the entire routing table looking for routes which point 303 * to this interface...oh well... 304 */ 305 for (i = 1; i <= AF_MAX; i++) { 306 if ((rnh = rt_tables[i]) == NULL) 307 continue; 308 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp); 309 } 310 311 TAILQ_REMOVE(&ifnet, ifp, if_link); 312 mtx_destroy(&ifp->if_snd.ifq_mtx); 313 splx(s); 314 } 315 316 /* 317 * Delete Routes for a Network Interface 318 * 319 * Called for each routing entry via the rnh->rnh_walktree() call above 320 * to delete all route entries referencing a detaching network interface. 321 * 322 * Arguments: 323 * rn pointer to node in the routing table 324 * arg argument passed to rnh->rnh_walktree() - detaching interface 325 * 326 * Returns: 327 * 0 successful 328 * errno failed - reason indicated 329 * 330 */ 331 static int 332 if_rtdel(rn, arg) 333 struct radix_node *rn; 334 void *arg; 335 { 336 struct rtentry *rt = (struct rtentry *)rn; 337 struct ifnet *ifp = arg; 338 int err; 339 340 if (rt->rt_ifp == ifp) { 341 342 /* 343 * Protect (sorta) against walktree recursion problems 344 * with cloned routes 345 */ 346 if ((rt->rt_flags & RTF_UP) == 0) 347 return (0); 348 349 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, 350 rt_mask(rt), rt->rt_flags, 351 (struct rtentry **) NULL); 352 if (err) { 353 log(LOG_WARNING, "if_rtdel: error %d\n", err); 354 } 355 } 356 357 return (0); 358 } 359 360 /* 361 * Create a clone network interface. 362 */ 363 int 364 if_clone_create(name, len) 365 char *name; 366 int len; 367 { 368 struct if_clone *ifc; 369 char *dp; 370 int wildcard; 371 int unit; 372 int err; 373 374 ifc = if_clone_lookup(name, &unit); 375 if (ifc == NULL) 376 return (EINVAL); 377 378 if (ifunit(name) != NULL) 379 return (EEXIST); 380 381 wildcard = (unit < 0); 382 383 err = (*ifc->ifc_create)(ifc, &unit); 384 if (err != 0) 385 return (err); 386 387 /* In the wildcard case, we need to update the name. */ 388 if (wildcard) { 389 for (dp = name; *dp != '\0'; dp++); 390 if (snprintf(dp, len - (dp-name), "%d", unit) > 391 len - (dp-name) - 1) { 392 /* 393 * This can only be a programmer error and 394 * there's no straightforward way to recover if 395 * it happens. 396 */ 397 panic("if_clone_create(): interface name too long"); 398 } 399 400 } 401 402 return (0); 403 } 404 405 /* 406 * Destroy a clone network interface. 407 */ 408 int 409 if_clone_destroy(name) 410 const char *name; 411 { 412 struct if_clone *ifc; 413 struct ifnet *ifp; 414 415 ifc = if_clone_lookup(name, NULL); 416 if (ifc == NULL) 417 return (EINVAL); 418 419 ifp = ifunit(name); 420 if (ifp == NULL) 421 return (ENXIO); 422 423 if (ifc->ifc_destroy == NULL) 424 return (EOPNOTSUPP); 425 426 (*ifc->ifc_destroy)(ifp); 427 return (0); 428 } 429 430 /* 431 * Look up a network interface cloner. 432 */ 433 static struct if_clone * 434 if_clone_lookup(name, unitp) 435 const char *name; 436 int *unitp; 437 { 438 struct if_clone *ifc; 439 const char *cp; 440 int i; 441 442 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) { 443 for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) { 444 if (ifc->ifc_name[i] != *cp) 445 goto next_ifc; 446 } 447 goto found_name; 448 next_ifc: 449 ifc = LIST_NEXT(ifc, ifc_list); 450 } 451 452 /* No match. */ 453 return ((struct if_clone *)NULL); 454 455 found_name: 456 if (*cp == '\0') { 457 i = -1; 458 } else { 459 for (i = 0; *cp != '\0'; cp++) { 460 if (*cp < '0' || *cp > '9') { 461 /* Bogus unit number. */ 462 return (NULL); 463 } 464 i = (i * 10) + (*cp - '0'); 465 } 466 } 467 468 if (unitp != NULL) 469 *unitp = i; 470 return (ifc); 471 } 472 473 /* 474 * Register a network interface cloner. 475 */ 476 void 477 if_clone_attach(ifc) 478 struct if_clone *ifc; 479 { 480 481 LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list); 482 if_cloners_count++; 483 } 484 485 /* 486 * Unregister a network interface cloner. 487 */ 488 void 489 if_clone_detach(ifc) 490 struct if_clone *ifc; 491 { 492 493 LIST_REMOVE(ifc, ifc_list); 494 if_cloners_count--; 495 } 496 497 /* 498 * Provide list of interface cloners to userspace. 499 */ 500 static int 501 if_clone_list(ifcr) 502 struct if_clonereq *ifcr; 503 { 504 char outbuf[IFNAMSIZ], *dst; 505 struct if_clone *ifc; 506 int count, error = 0; 507 508 ifcr->ifcr_total = if_cloners_count; 509 if ((dst = ifcr->ifcr_buffer) == NULL) { 510 /* Just asking how many there are. */ 511 return (0); 512 } 513 514 if (ifcr->ifcr_count < 0) 515 return (EINVAL); 516 517 count = (if_cloners_count < ifcr->ifcr_count) ? 518 if_cloners_count : ifcr->ifcr_count; 519 520 for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0; 521 ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) { 522 strncpy(outbuf, ifc->ifc_name, IFNAMSIZ); 523 outbuf[IFNAMSIZ - 1] = '\0'; /* sanity */ 524 error = copyout(outbuf, dst, IFNAMSIZ); 525 if (error) 526 break; 527 } 528 529 return (error); 530 } 531 532 /* 533 * Locate an interface based on a complete address. 534 */ 535 /*ARGSUSED*/ 536 struct ifaddr * 537 ifa_ifwithaddr(addr) 538 struct sockaddr *addr; 539 { 540 struct ifnet *ifp; 541 struct ifaddr *ifa; 542 543 #define equal(a1, a2) \ 544 (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0) 545 TAILQ_FOREACH(ifp, &ifnet, if_link) 546 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 547 if (ifa->ifa_addr->sa_family != addr->sa_family) 548 continue; 549 if (equal(addr, ifa->ifa_addr)) 550 goto done; 551 /* IP6 doesn't have broadcast */ 552 if ((ifp->if_flags & IFF_BROADCAST) && 553 ifa->ifa_broadaddr && 554 ifa->ifa_broadaddr->sa_len != 0 && 555 equal(ifa->ifa_broadaddr, addr)) 556 goto done; 557 } 558 ifa = NULL; 559 done: 560 return (ifa); 561 } 562 563 /* 564 * Locate the point to point interface with a given destination address. 565 */ 566 /*ARGSUSED*/ 567 struct ifaddr * 568 ifa_ifwithdstaddr(addr) 569 struct sockaddr *addr; 570 { 571 struct ifnet *ifp; 572 struct ifaddr *ifa; 573 574 TAILQ_FOREACH(ifp, &ifnet, if_link) { 575 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 576 continue; 577 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 578 if (ifa->ifa_addr->sa_family != addr->sa_family) 579 continue; 580 if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)) 581 goto done; 582 } 583 } 584 ifa = NULL; 585 done: 586 return (ifa); 587 } 588 589 /* 590 * Find an interface on a specific network. If many, choice 591 * is most specific found. 592 */ 593 struct ifaddr * 594 ifa_ifwithnet(addr) 595 struct sockaddr *addr; 596 { 597 register struct ifnet *ifp; 598 register struct ifaddr *ifa; 599 struct ifaddr *ifa_maybe = (struct ifaddr *) 0; 600 u_int af = addr->sa_family; 601 char *addr_data = addr->sa_data, *cplim; 602 603 /* 604 * AF_LINK addresses can be looked up directly by their index number, 605 * so do that if we can. 606 */ 607 if (af == AF_LINK) { 608 register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; 609 if (sdl->sdl_index && sdl->sdl_index <= if_index) 610 return (ifaddr_byindex(sdl->sdl_index)); 611 } 612 613 /* 614 * Scan though each interface, looking for ones that have 615 * addresses in this address family. 616 */ 617 TAILQ_FOREACH(ifp, &ifnet, if_link) { 618 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 619 register char *cp, *cp2, *cp3; 620 621 if (ifa->ifa_addr->sa_family != af) 622 next: continue; 623 if ( 624 #ifdef INET6 /* XXX: for maching gif tunnel dst as routing entry gateway */ 625 addr->sa_family != AF_INET6 && 626 #endif 627 ifp->if_flags & IFF_POINTOPOINT) { 628 /* 629 * This is a bit broken as it doesn't 630 * take into account that the remote end may 631 * be a single node in the network we are 632 * looking for. 633 * The trouble is that we don't know the 634 * netmask for the remote end. 635 */ 636 if (ifa->ifa_dstaddr != 0 637 && equal(addr, ifa->ifa_dstaddr)) 638 goto done; 639 } else { 640 /* 641 * if we have a special address handler, 642 * then use it instead of the generic one. 643 */ 644 if (ifa->ifa_claim_addr) { 645 if ((*ifa->ifa_claim_addr)(ifa, addr)) 646 goto done; 647 continue; 648 } 649 650 /* 651 * Scan all the bits in the ifa's address. 652 * If a bit dissagrees with what we are 653 * looking for, mask it with the netmask 654 * to see if it really matters. 655 * (A byte at a time) 656 */ 657 if (ifa->ifa_netmask == 0) 658 continue; 659 cp = addr_data; 660 cp2 = ifa->ifa_addr->sa_data; 661 cp3 = ifa->ifa_netmask->sa_data; 662 cplim = ifa->ifa_netmask->sa_len 663 + (char *)ifa->ifa_netmask; 664 while (cp3 < cplim) 665 if ((*cp++ ^ *cp2++) & *cp3++) 666 goto next; /* next address! */ 667 /* 668 * If the netmask of what we just found 669 * is more specific than what we had before 670 * (if we had one) then remember the new one 671 * before continuing to search 672 * for an even better one. 673 */ 674 if (ifa_maybe == 0 || 675 rn_refines((caddr_t)ifa->ifa_netmask, 676 (caddr_t)ifa_maybe->ifa_netmask)) 677 ifa_maybe = ifa; 678 } 679 } 680 } 681 ifa = ifa_maybe; 682 done: 683 return (ifa); 684 } 685 686 /* 687 * Find an interface address specific to an interface best matching 688 * a given address. 689 */ 690 struct ifaddr * 691 ifaof_ifpforaddr(addr, ifp) 692 struct sockaddr *addr; 693 register struct ifnet *ifp; 694 { 695 register struct ifaddr *ifa; 696 register char *cp, *cp2, *cp3; 697 register char *cplim; 698 struct ifaddr *ifa_maybe = 0; 699 u_int af = addr->sa_family; 700 701 if (af >= AF_MAX) 702 return (0); 703 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 704 if (ifa->ifa_addr->sa_family != af) 705 continue; 706 if (ifa_maybe == 0) 707 ifa_maybe = ifa; 708 if (ifa->ifa_netmask == 0) { 709 if (equal(addr, ifa->ifa_addr) || 710 (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))) 711 goto done; 712 continue; 713 } 714 if (ifp->if_flags & IFF_POINTOPOINT) { 715 if (equal(addr, ifa->ifa_dstaddr)) 716 goto done; 717 } else { 718 cp = addr->sa_data; 719 cp2 = ifa->ifa_addr->sa_data; 720 cp3 = ifa->ifa_netmask->sa_data; 721 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 722 for (; cp3 < cplim; cp3++) 723 if ((*cp++ ^ *cp2++) & *cp3) 724 break; 725 if (cp3 == cplim) 726 goto done; 727 } 728 } 729 ifa = ifa_maybe; 730 done: 731 return (ifa); 732 } 733 734 #include <net/route.h> 735 736 /* 737 * Default action when installing a route with a Link Level gateway. 738 * Lookup an appropriate real ifa to point to. 739 * This should be moved to /sys/net/link.c eventually. 740 */ 741 static void 742 link_rtrequest(cmd, rt, sa) 743 int cmd; 744 register struct rtentry *rt; 745 struct sockaddr *sa; 746 { 747 register struct ifaddr *ifa; 748 struct sockaddr *dst; 749 struct ifnet *ifp; 750 751 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 752 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 753 return; 754 ifa = ifaof_ifpforaddr(dst, ifp); 755 if (ifa) { 756 IFAFREE(rt->rt_ifa); 757 rt->rt_ifa = ifa; 758 ifa->ifa_refcnt++; 759 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 760 ifa->ifa_rtrequest(cmd, rt, sa); 761 } 762 } 763 764 /* 765 * Mark an interface down and notify protocols of 766 * the transition. 767 * NOTE: must be called at splnet or eqivalent. 768 */ 769 void 770 if_unroute(ifp, flag, fam) 771 register struct ifnet *ifp; 772 int flag, fam; 773 { 774 register struct ifaddr *ifa; 775 776 ifp->if_flags &= ~flag; 777 getmicrotime(&ifp->if_lastchange); 778 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 779 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 780 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 781 if_qflush(&ifp->if_snd); 782 rt_ifmsg(ifp); 783 } 784 785 /* 786 * Mark an interface up and notify protocols of 787 * the transition. 788 * NOTE: must be called at splnet or eqivalent. 789 */ 790 void 791 if_route(ifp, flag, fam) 792 register struct ifnet *ifp; 793 int flag, fam; 794 { 795 register struct ifaddr *ifa; 796 797 ifp->if_flags |= flag; 798 getmicrotime(&ifp->if_lastchange); 799 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 800 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 801 pfctlinput(PRC_IFUP, ifa->ifa_addr); 802 rt_ifmsg(ifp); 803 #ifdef INET6 804 in6_if_up(ifp); 805 #endif 806 } 807 808 /* 809 * Mark an interface down and notify protocols of 810 * the transition. 811 * NOTE: must be called at splnet or eqivalent. 812 */ 813 void 814 if_down(ifp) 815 register struct ifnet *ifp; 816 { 817 818 if_unroute(ifp, IFF_UP, AF_UNSPEC); 819 } 820 821 /* 822 * Mark an interface up and notify protocols of 823 * the transition. 824 * NOTE: must be called at splnet or eqivalent. 825 */ 826 void 827 if_up(ifp) 828 register struct ifnet *ifp; 829 { 830 831 if_route(ifp, IFF_UP, AF_UNSPEC); 832 } 833 834 /* 835 * Flush an interface queue. 836 */ 837 static void 838 if_qflush(ifq) 839 register struct ifqueue *ifq; 840 { 841 register struct mbuf *m, *n; 842 843 n = ifq->ifq_head; 844 while ((m = n) != 0) { 845 n = m->m_act; 846 m_freem(m); 847 } 848 ifq->ifq_head = 0; 849 ifq->ifq_tail = 0; 850 ifq->ifq_len = 0; 851 } 852 853 /* 854 * Handle interface watchdog timer routines. Called 855 * from softclock, we decrement timers (if set) and 856 * call the appropriate interface routine on expiration. 857 */ 858 static void 859 if_slowtimo(arg) 860 void *arg; 861 { 862 register struct ifnet *ifp; 863 int s = splimp(); 864 865 TAILQ_FOREACH(ifp, &ifnet, if_link) { 866 if (ifp->if_timer == 0 || --ifp->if_timer) 867 continue; 868 if (ifp->if_watchdog) 869 (*ifp->if_watchdog)(ifp); 870 } 871 splx(s); 872 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ); 873 } 874 875 /* 876 * Map interface name to 877 * interface structure pointer. 878 */ 879 struct ifnet * 880 ifunit(const char *name) 881 { 882 char namebuf[IFNAMSIZ + 1]; 883 const char *cp; 884 struct ifnet *ifp; 885 int unit; 886 unsigned len, m; 887 char c; 888 889 len = strlen(name); 890 if (len < 2 || len > IFNAMSIZ) 891 return NULL; 892 cp = name + len - 1; 893 c = *cp; 894 if (c < '0' || c > '9') 895 return NULL; /* trailing garbage */ 896 unit = 0; 897 m = 1; 898 do { 899 if (cp == name) 900 return NULL; /* no interface name */ 901 unit += (c - '0') * m; 902 if (unit > 1000000) 903 return NULL; /* number is unreasonable */ 904 m *= 10; 905 c = *--cp; 906 } while (c >= '0' && c <= '9'); 907 len = cp - name + 1; 908 bcopy(name, namebuf, len); 909 namebuf[len] = '\0'; 910 /* 911 * Now search all the interfaces for this name/number 912 */ 913 TAILQ_FOREACH(ifp, &ifnet, if_link) { 914 if (strcmp(ifp->if_name, namebuf)) 915 continue; 916 if (unit == ifp->if_unit) 917 break; 918 } 919 return (ifp); 920 } 921 922 923 /* 924 * Map interface name in a sockaddr_dl to 925 * interface structure pointer. 926 */ 927 struct ifnet * 928 if_withname(sa) 929 struct sockaddr *sa; 930 { 931 char ifname[IFNAMSIZ+1]; 932 struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa; 933 934 if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) || 935 (sdl->sdl_nlen > IFNAMSIZ) ) 936 return NULL; 937 938 /* 939 * ifunit wants a null-terminated name. It may not be null-terminated 940 * in the sockaddr. We don't want to change the caller's sockaddr, 941 * and there might not be room to put the trailing null anyway, so we 942 * make a local copy that we know we can null terminate safely. 943 */ 944 945 bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen); 946 ifname[sdl->sdl_nlen] = '\0'; 947 return ifunit(ifname); 948 } 949 950 951 /* 952 * Interface ioctls. 953 */ 954 int 955 ifioctl(so, cmd, data, td) 956 struct socket *so; 957 u_long cmd; 958 caddr_t data; 959 struct thread *td; 960 { 961 register struct ifnet *ifp; 962 register struct ifreq *ifr; 963 struct ifstat *ifs; 964 int error; 965 short oif_flags; 966 967 switch (cmd) { 968 969 case SIOCGIFCONF: 970 case OSIOCGIFCONF: 971 return (ifconf(cmd, data)); 972 } 973 ifr = (struct ifreq *)data; 974 975 switch (cmd) { 976 case SIOCIFCREATE: 977 case SIOCIFDESTROY: 978 if ((error = suser_td(td)) != 0) 979 return (error); 980 return ((cmd == SIOCIFCREATE) ? 981 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) : 982 if_clone_destroy(ifr->ifr_name)); 983 984 case SIOCIFGCLONERS: 985 return (if_clone_list((struct if_clonereq *)data)); 986 } 987 988 ifp = ifunit(ifr->ifr_name); 989 if (ifp == 0) 990 return (ENXIO); 991 switch (cmd) { 992 993 case SIOCGIFFLAGS: 994 ifr->ifr_flags = ifp->if_flags; 995 break; 996 997 case SIOCGIFMETRIC: 998 ifr->ifr_metric = ifp->if_metric; 999 break; 1000 1001 case SIOCGIFMTU: 1002 ifr->ifr_mtu = ifp->if_mtu; 1003 break; 1004 1005 case SIOCGIFPHYS: 1006 ifr->ifr_phys = ifp->if_physical; 1007 break; 1008 1009 case SIOCSIFFLAGS: 1010 error = suser_td(td); 1011 if (error) 1012 return (error); 1013 ifr->ifr_prevflags = ifp->if_flags; 1014 if (ifp->if_flags & IFF_SMART) { 1015 /* Smart drivers twiddle their own routes */ 1016 } else if (ifp->if_flags & IFF_UP && 1017 (ifr->ifr_flags & IFF_UP) == 0) { 1018 int s = splimp(); 1019 if_down(ifp); 1020 splx(s); 1021 } else if (ifr->ifr_flags & IFF_UP && 1022 (ifp->if_flags & IFF_UP) == 0) { 1023 int s = splimp(); 1024 if_up(ifp); 1025 splx(s); 1026 } 1027 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 1028 (ifr->ifr_flags &~ IFF_CANTCHANGE); 1029 if (ifp->if_ioctl) 1030 (void) (*ifp->if_ioctl)(ifp, cmd, data); 1031 getmicrotime(&ifp->if_lastchange); 1032 break; 1033 1034 case SIOCSIFMETRIC: 1035 error = suser_td(td); 1036 if (error) 1037 return (error); 1038 ifp->if_metric = ifr->ifr_metric; 1039 getmicrotime(&ifp->if_lastchange); 1040 break; 1041 1042 case SIOCSIFPHYS: 1043 error = suser_td(td); 1044 if (error) 1045 return error; 1046 if (!ifp->if_ioctl) 1047 return EOPNOTSUPP; 1048 error = (*ifp->if_ioctl)(ifp, cmd, data); 1049 if (error == 0) 1050 getmicrotime(&ifp->if_lastchange); 1051 return(error); 1052 1053 case SIOCSIFMTU: 1054 { 1055 u_long oldmtu = ifp->if_mtu; 1056 1057 error = suser_td(td); 1058 if (error) 1059 return (error); 1060 if (ifp->if_ioctl == NULL) 1061 return (EOPNOTSUPP); 1062 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) 1063 return (EINVAL); 1064 error = (*ifp->if_ioctl)(ifp, cmd, data); 1065 if (error == 0) { 1066 getmicrotime(&ifp->if_lastchange); 1067 rt_ifmsg(ifp); 1068 } 1069 /* 1070 * If the link MTU changed, do network layer specific procedure. 1071 */ 1072 if (ifp->if_mtu != oldmtu) { 1073 #ifdef INET6 1074 nd6_setmtu(ifp); 1075 #endif 1076 } 1077 return (error); 1078 } 1079 1080 case SIOCADDMULTI: 1081 case SIOCDELMULTI: 1082 error = suser_td(td); 1083 if (error) 1084 return (error); 1085 1086 /* Don't allow group membership on non-multicast interfaces. */ 1087 if ((ifp->if_flags & IFF_MULTICAST) == 0) 1088 return EOPNOTSUPP; 1089 1090 /* Don't let users screw up protocols' entries. */ 1091 if (ifr->ifr_addr.sa_family != AF_LINK) 1092 return EINVAL; 1093 1094 if (cmd == SIOCADDMULTI) { 1095 struct ifmultiaddr *ifma; 1096 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 1097 } else { 1098 error = if_delmulti(ifp, &ifr->ifr_addr); 1099 } 1100 if (error == 0) 1101 getmicrotime(&ifp->if_lastchange); 1102 return error; 1103 1104 case SIOCSIFPHYADDR: 1105 case SIOCDIFPHYADDR: 1106 #ifdef INET6 1107 case SIOCSIFPHYADDR_IN6: 1108 #endif 1109 case SIOCSLIFPHYADDR: 1110 case SIOCSIFMEDIA: 1111 case SIOCSIFGENERIC: 1112 error = suser_td(td); 1113 if (error) 1114 return (error); 1115 if (ifp->if_ioctl == 0) 1116 return (EOPNOTSUPP); 1117 error = (*ifp->if_ioctl)(ifp, cmd, data); 1118 if (error == 0) 1119 getmicrotime(&ifp->if_lastchange); 1120 return error; 1121 1122 case SIOCGIFSTATUS: 1123 ifs = (struct ifstat *)data; 1124 ifs->ascii[0] = '\0'; 1125 1126 case SIOCGIFPSRCADDR: 1127 case SIOCGIFPDSTADDR: 1128 case SIOCGLIFPHYADDR: 1129 case SIOCGIFMEDIA: 1130 case SIOCGIFGENERIC: 1131 if (ifp->if_ioctl == 0) 1132 return (EOPNOTSUPP); 1133 return ((*ifp->if_ioctl)(ifp, cmd, data)); 1134 1135 case SIOCSIFLLADDR: 1136 error = suser_td(td); 1137 if (error) 1138 return (error); 1139 return if_setlladdr(ifp, 1140 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); 1141 1142 default: 1143 oif_flags = ifp->if_flags; 1144 if (so->so_proto == 0) 1145 return (EOPNOTSUPP); 1146 #ifndef COMPAT_43 1147 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, 1148 data, 1149 ifp, td)); 1150 #else 1151 { 1152 int ocmd = cmd; 1153 1154 switch (cmd) { 1155 1156 case SIOCSIFDSTADDR: 1157 case SIOCSIFADDR: 1158 case SIOCSIFBRDADDR: 1159 case SIOCSIFNETMASK: 1160 #if BYTE_ORDER != BIG_ENDIAN 1161 if (ifr->ifr_addr.sa_family == 0 && 1162 ifr->ifr_addr.sa_len < 16) { 1163 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 1164 ifr->ifr_addr.sa_len = 16; 1165 } 1166 #else 1167 if (ifr->ifr_addr.sa_len == 0) 1168 ifr->ifr_addr.sa_len = 16; 1169 #endif 1170 break; 1171 1172 case OSIOCGIFADDR: 1173 cmd = SIOCGIFADDR; 1174 break; 1175 1176 case OSIOCGIFDSTADDR: 1177 cmd = SIOCGIFDSTADDR; 1178 break; 1179 1180 case OSIOCGIFBRDADDR: 1181 cmd = SIOCGIFBRDADDR; 1182 break; 1183 1184 case OSIOCGIFNETMASK: 1185 cmd = SIOCGIFNETMASK; 1186 } 1187 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, 1188 cmd, 1189 data, 1190 ifp, td)); 1191 switch (ocmd) { 1192 1193 case OSIOCGIFADDR: 1194 case OSIOCGIFDSTADDR: 1195 case OSIOCGIFBRDADDR: 1196 case OSIOCGIFNETMASK: 1197 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 1198 1199 } 1200 } 1201 #endif /* COMPAT_43 */ 1202 1203 if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 1204 #ifdef INET6 1205 DELAY(100);/* XXX: temporal workaround for fxp issue*/ 1206 if (ifp->if_flags & IFF_UP) { 1207 int s = splimp(); 1208 in6_if_up(ifp); 1209 splx(s); 1210 } 1211 #endif 1212 } 1213 return (error); 1214 1215 } 1216 return (0); 1217 } 1218 1219 /* 1220 * Set/clear promiscuous mode on interface ifp based on the truth value 1221 * of pswitch. The calls are reference counted so that only the first 1222 * "on" request actually has an effect, as does the final "off" request. 1223 * Results are undefined if the "off" and "on" requests are not matched. 1224 */ 1225 int 1226 ifpromisc(ifp, pswitch) 1227 struct ifnet *ifp; 1228 int pswitch; 1229 { 1230 struct ifreq ifr; 1231 int error; 1232 int oldflags, oldpcount; 1233 1234 oldpcount = ifp->if_pcount; 1235 oldflags = ifp->if_flags; 1236 if (pswitch) { 1237 /* 1238 * If the device is not configured up, we cannot put it in 1239 * promiscuous mode. 1240 */ 1241 if ((ifp->if_flags & IFF_UP) == 0) 1242 return (ENETDOWN); 1243 if (ifp->if_pcount++ != 0) 1244 return (0); 1245 ifp->if_flags |= IFF_PROMISC; 1246 } else { 1247 if (--ifp->if_pcount > 0) 1248 return (0); 1249 ifp->if_flags &= ~IFF_PROMISC; 1250 } 1251 ifr.ifr_flags = ifp->if_flags; 1252 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 1253 if (error == 0) { 1254 log(LOG_INFO, "%s%d: promiscuous mode %s\n", 1255 ifp->if_name, ifp->if_unit, 1256 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); 1257 rt_ifmsg(ifp); 1258 } else { 1259 ifp->if_pcount = oldpcount; 1260 ifp->if_flags = oldflags; 1261 } 1262 return error; 1263 } 1264 1265 /* 1266 * Return interface configuration 1267 * of system. List may be used 1268 * in later ioctl's (above) to get 1269 * other information. 1270 */ 1271 /*ARGSUSED*/ 1272 static int 1273 ifconf(cmd, data) 1274 u_long cmd; 1275 caddr_t data; 1276 { 1277 struct ifconf *ifc = (struct ifconf *)data; 1278 struct ifnet *ifp; 1279 struct ifaddr *ifa; 1280 struct ifreq ifr, *ifrp; 1281 int space = ifc->ifc_len, error = 0; 1282 1283 ifrp = ifc->ifc_req; 1284 TAILQ_FOREACH(ifp, &ifnet, if_link) { 1285 char workbuf[64]; 1286 int ifnlen, addrs; 1287 1288 if (space < sizeof(ifr)) 1289 break; 1290 ifnlen = snprintf(workbuf, sizeof(workbuf), 1291 "%s%d", ifp->if_name, ifp->if_unit); 1292 if(ifnlen + 1 > sizeof ifr.ifr_name) { 1293 error = ENAMETOOLONG; 1294 break; 1295 } else { 1296 strcpy(ifr.ifr_name, workbuf); 1297 } 1298 1299 addrs = 0; 1300 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1301 struct sockaddr *sa = ifa->ifa_addr; 1302 1303 if (space < sizeof(ifr)) 1304 break; 1305 if (jailed(curproc->p_ucred) && 1306 prison_if(curproc->p_ucred, sa)) 1307 continue; 1308 addrs++; 1309 #ifdef COMPAT_43 1310 if (cmd == OSIOCGIFCONF) { 1311 struct osockaddr *osa = 1312 (struct osockaddr *)&ifr.ifr_addr; 1313 ifr.ifr_addr = *sa; 1314 osa->sa_family = sa->sa_family; 1315 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 1316 sizeof (ifr)); 1317 ifrp++; 1318 } else 1319 #endif 1320 if (sa->sa_len <= sizeof(*sa)) { 1321 ifr.ifr_addr = *sa; 1322 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 1323 sizeof (ifr)); 1324 ifrp++; 1325 } else { 1326 if (space < sizeof (ifr) + sa->sa_len - 1327 sizeof(*sa)) 1328 break; 1329 space -= sa->sa_len - sizeof(*sa); 1330 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 1331 sizeof (ifr.ifr_name)); 1332 if (error == 0) 1333 error = copyout((caddr_t)sa, 1334 (caddr_t)&ifrp->ifr_addr, sa->sa_len); 1335 ifrp = (struct ifreq *) 1336 (sa->sa_len + (caddr_t)&ifrp->ifr_addr); 1337 } 1338 if (error) 1339 break; 1340 space -= sizeof (ifr); 1341 } 1342 if (error) 1343 break; 1344 if (!addrs) { 1345 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 1346 error = copyout((caddr_t)&ifr, (caddr_t)ifrp, 1347 sizeof (ifr)); 1348 if (error) 1349 break; 1350 space -= sizeof (ifr); 1351 ifrp++; 1352 } 1353 } 1354 ifc->ifc_len -= space; 1355 return (error); 1356 } 1357 1358 /* 1359 * Just like if_promisc(), but for all-multicast-reception mode. 1360 */ 1361 int 1362 if_allmulti(ifp, onswitch) 1363 struct ifnet *ifp; 1364 int onswitch; 1365 { 1366 int error = 0; 1367 int s = splimp(); 1368 1369 if (onswitch) { 1370 if (ifp->if_amcount++ == 0) { 1371 ifp->if_flags |= IFF_ALLMULTI; 1372 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0); 1373 } 1374 } else { 1375 if (ifp->if_amcount > 1) { 1376 ifp->if_amcount--; 1377 } else { 1378 ifp->if_amcount = 0; 1379 ifp->if_flags &= ~IFF_ALLMULTI; 1380 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, 0); 1381 } 1382 } 1383 splx(s); 1384 1385 if (error == 0) 1386 rt_ifmsg(ifp); 1387 return error; 1388 } 1389 1390 /* 1391 * Add a multicast listenership to the interface in question. 1392 * The link layer provides a routine which converts 1393 */ 1394 int 1395 if_addmulti(ifp, sa, retifma) 1396 struct ifnet *ifp; /* interface to manipulate */ 1397 struct sockaddr *sa; /* address to add */ 1398 struct ifmultiaddr **retifma; 1399 { 1400 struct sockaddr *llsa, *dupsa; 1401 int error, s; 1402 struct ifmultiaddr *ifma; 1403 1404 /* 1405 * If the matching multicast address already exists 1406 * then don't add a new one, just add a reference 1407 */ 1408 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1409 if (equal(sa, ifma->ifma_addr)) { 1410 ifma->ifma_refcount++; 1411 if (retifma) 1412 *retifma = ifma; 1413 return 0; 1414 } 1415 } 1416 1417 /* 1418 * Give the link layer a chance to accept/reject it, and also 1419 * find out which AF_LINK address this maps to, if it isn't one 1420 * already. 1421 */ 1422 if (ifp->if_resolvemulti) { 1423 error = ifp->if_resolvemulti(ifp, &llsa, sa); 1424 if (error) return error; 1425 } else { 1426 llsa = 0; 1427 } 1428 1429 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK); 1430 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK); 1431 bcopy(sa, dupsa, sa->sa_len); 1432 1433 ifma->ifma_addr = dupsa; 1434 ifma->ifma_lladdr = llsa; 1435 ifma->ifma_ifp = ifp; 1436 ifma->ifma_refcount = 1; 1437 ifma->ifma_protospec = 0; 1438 rt_newmaddrmsg(RTM_NEWMADDR, ifma); 1439 1440 /* 1441 * Some network interfaces can scan the address list at 1442 * interrupt time; lock them out. 1443 */ 1444 s = splimp(); 1445 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 1446 splx(s); 1447 *retifma = ifma; 1448 1449 if (llsa != 0) { 1450 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1451 if (equal(ifma->ifma_addr, llsa)) 1452 break; 1453 } 1454 if (ifma) { 1455 ifma->ifma_refcount++; 1456 } else { 1457 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, 1458 M_IFMADDR, M_WAITOK); 1459 MALLOC(dupsa, struct sockaddr *, llsa->sa_len, 1460 M_IFMADDR, M_WAITOK); 1461 bcopy(llsa, dupsa, llsa->sa_len); 1462 ifma->ifma_addr = dupsa; 1463 ifma->ifma_ifp = ifp; 1464 ifma->ifma_refcount = 1; 1465 s = splimp(); 1466 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 1467 splx(s); 1468 } 1469 } 1470 /* 1471 * We are certain we have added something, so call down to the 1472 * interface to let them know about it. 1473 */ 1474 s = splimp(); 1475 ifp->if_ioctl(ifp, SIOCADDMULTI, 0); 1476 splx(s); 1477 1478 return 0; 1479 } 1480 1481 /* 1482 * Remove a reference to a multicast address on this interface. Yell 1483 * if the request does not match an existing membership. 1484 */ 1485 int 1486 if_delmulti(ifp, sa) 1487 struct ifnet *ifp; 1488 struct sockaddr *sa; 1489 { 1490 struct ifmultiaddr *ifma; 1491 int s; 1492 1493 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 1494 if (equal(sa, ifma->ifma_addr)) 1495 break; 1496 if (ifma == 0) 1497 return ENOENT; 1498 1499 if (ifma->ifma_refcount > 1) { 1500 ifma->ifma_refcount--; 1501 return 0; 1502 } 1503 1504 rt_newmaddrmsg(RTM_DELMADDR, ifma); 1505 sa = ifma->ifma_lladdr; 1506 s = splimp(); 1507 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 1508 /* 1509 * Make sure the interface driver is notified 1510 * in the case of a link layer mcast group being left. 1511 */ 1512 if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0) 1513 ifp->if_ioctl(ifp, SIOCDELMULTI, 0); 1514 splx(s); 1515 free(ifma->ifma_addr, M_IFMADDR); 1516 free(ifma, M_IFMADDR); 1517 if (sa == 0) 1518 return 0; 1519 1520 /* 1521 * Now look for the link-layer address which corresponds to 1522 * this network address. It had been squirreled away in 1523 * ifma->ifma_lladdr for this purpose (so we don't have 1524 * to call ifp->if_resolvemulti() again), and we saved that 1525 * value in sa above. If some nasty deleted the 1526 * link-layer address out from underneath us, we can deal because 1527 * the address we stored was is not the same as the one which was 1528 * in the record for the link-layer address. (So we don't complain 1529 * in that case.) 1530 */ 1531 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 1532 if (equal(sa, ifma->ifma_addr)) 1533 break; 1534 if (ifma == 0) 1535 return 0; 1536 1537 if (ifma->ifma_refcount > 1) { 1538 ifma->ifma_refcount--; 1539 return 0; 1540 } 1541 1542 s = splimp(); 1543 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 1544 ifp->if_ioctl(ifp, SIOCDELMULTI, 0); 1545 splx(s); 1546 free(ifma->ifma_addr, M_IFMADDR); 1547 free(sa, M_IFMADDR); 1548 free(ifma, M_IFMADDR); 1549 1550 return 0; 1551 } 1552 1553 /* 1554 * Set the link layer address on an interface. 1555 * 1556 * At this time we only support certain types of interfaces, 1557 * and we don't allow the length of the address to change. 1558 */ 1559 int 1560 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 1561 { 1562 struct sockaddr_dl *sdl; 1563 struct ifaddr *ifa; 1564 1565 ifa = ifaddr_byindex(ifp->if_index); 1566 if (ifa == NULL) 1567 return (EINVAL); 1568 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 1569 if (sdl == NULL) 1570 return (EINVAL); 1571 if (len != sdl->sdl_alen) /* don't allow length to change */ 1572 return (EINVAL); 1573 switch (ifp->if_type) { 1574 case IFT_ETHER: /* these types use struct arpcom */ 1575 case IFT_FDDI: 1576 case IFT_XETHER: 1577 case IFT_ISO88025: 1578 case IFT_L2VLAN: 1579 bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len); 1580 bcopy(lladdr, LLADDR(sdl), len); 1581 break; 1582 default: 1583 return (ENODEV); 1584 } 1585 /* 1586 * If the interface is already up, we need 1587 * to re-init it in order to reprogram its 1588 * address filter. 1589 */ 1590 if ((ifp->if_flags & IFF_UP) != 0) { 1591 ifp->if_flags &= ~IFF_UP; 1592 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, NULL); 1593 ifp->if_flags |= IFF_UP; 1594 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, NULL); 1595 } 1596 return (0); 1597 } 1598 1599 struct ifmultiaddr * 1600 ifmaof_ifpforaddr(sa, ifp) 1601 struct sockaddr *sa; 1602 struct ifnet *ifp; 1603 { 1604 struct ifmultiaddr *ifma; 1605 1606 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 1607 if (equal(ifma->ifma_addr, sa)) 1608 break; 1609 1610 return ifma; 1611 } 1612 1613 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 1614 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); 1615