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