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