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