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