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