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