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