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