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