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