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