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$ 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 82 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL; 83 84 static void if_attachdomain(void *); 85 static void if_attachdomain1(struct ifnet *); 86 static int ifconf(u_long, caddr_t); 87 static void if_grow(void); 88 static void if_init(void *); 89 static void if_check(void *); 90 static int if_findindex(struct ifnet *); 91 static void if_qflush(struct ifaltq *); 92 static void if_route(struct ifnet *, int flag, int fam); 93 static void if_slowtimo(void *); 94 static void if_unroute(struct ifnet *, int flag, int fam); 95 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 96 static int if_rtdel(struct radix_node *, void *); 97 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *); 98 static 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 */ 104 extern void nd6_setmtu(struct ifnet *); 105 #endif 106 107 int if_index = 0; 108 struct ifindex_entry *ifindex_table = NULL; 109 int ifqmaxlen = IFQ_MAXLEN; 110 struct ifnethead ifnet; /* depend on static init XXX */ 111 struct mtx ifnet_lock; 112 113 static int if_indexlim = 8; 114 static struct knlist ifklist; 115 116 static void filt_netdetach(struct knote *kn); 117 static int filt_netdev(struct knote *kn, long hint); 118 119 static struct filterops netdev_filtops = 120 { 1, NULL, filt_netdetach, filt_netdev }; 121 122 /* 123 * System initialization 124 */ 125 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL) 126 SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL) 127 128 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 129 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 130 131 static d_open_t netopen; 132 static d_close_t netclose; 133 static d_ioctl_t netioctl; 134 static d_kqfilter_t netkqfilter; 135 136 static 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 146 static int 147 netopen(struct cdev *dev, int flag, int mode, struct thread *td) 148 { 149 return (0); 150 } 151 152 static int 153 netclose(struct cdev *dev, int flags, int fmt, struct thread *td) 154 { 155 return (0); 156 } 157 158 static int 159 netioctl(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 187 static int 188 netkqfilter(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 219 static void 220 filt_netdetach(struct knote *kn) 221 { 222 struct knlist *klist = (struct knlist *)kn->kn_hook; 223 224 knlist_remove(klist, kn, 0); 225 } 226 227 static int 228 filt_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*/ 255 static void 256 if_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 268 static void 269 if_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*/ 285 static void 286 if_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 310 static int 311 if_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; 341 found: 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 */ 363 void 364 if_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 (domain_init_status >= 2) 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 463 static void 464 if_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 } 474 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND, 475 if_attachdomain, NULL); 476 477 static void 478 if_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 >= domain_init_status) { 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 = domain_init_status; 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 */ 518 void 519 if_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 */ 662 static int 663 if_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*/ 695 struct ifaddr * 696 ifa_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; 716 done: 717 IFNET_RUNLOCK(); 718 return (ifa); 719 } 720 721 /* 722 * Locate the point to point interface with a given destination address. 723 */ 724 /*ARGSUSED*/ 725 struct ifaddr * 726 ifa_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; 743 done: 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 */ 752 struct ifaddr * 753 ifa_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) 781 next: 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; 837 done: 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 */ 846 struct ifaddr * 847 ifaof_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; 884 done: 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 */ 895 static void 896 link_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 */ 923 static void 924 if_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 */ 942 static void 943 if_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 void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */ 959 960 /* 961 * Handle a change in the interface link state. 962 */ 963 void 964 if_link_state_change(struct ifnet *ifp, int link_state) 965 { 966 int link; 967 968 /* Notify that the link state has changed. */ 969 if (ifp->if_link_state != link_state) { 970 ifp->if_link_state = link_state; 971 rt_ifmsg(ifp); 972 if (link_state == LINK_STATE_UP) 973 link = NOTE_LINKUP; 974 else if (link_state == LINK_STATE_DOWN) 975 link = NOTE_LINKDOWN; 976 else 977 link = NOTE_LINKINV; 978 KNOTE_UNLOCKED(&ifp->if_klist, link); 979 if (ifp->if_nvlans != 0) 980 (*vlan_link_state_p)(ifp, link); 981 } 982 } 983 984 /* 985 * Mark an interface down and notify protocols of 986 * the transition. 987 * NOTE: must be called at splnet or eqivalent. 988 */ 989 void 990 if_down(struct ifnet *ifp) 991 { 992 993 if_unroute(ifp, IFF_UP, AF_UNSPEC); 994 } 995 996 /* 997 * Mark an interface up and notify protocols of 998 * the transition. 999 * NOTE: must be called at splnet or eqivalent. 1000 */ 1001 void 1002 if_up(struct ifnet *ifp) 1003 { 1004 1005 if_route(ifp, IFF_UP, AF_UNSPEC); 1006 } 1007 1008 /* 1009 * Flush an interface queue. 1010 */ 1011 static void 1012 if_qflush(struct ifaltq *ifq) 1013 { 1014 struct mbuf *m, *n; 1015 1016 IFQ_LOCK(ifq); 1017 #ifdef ALTQ 1018 if (ALTQ_IS_ENABLED(ifq)) 1019 ALTQ_PURGE(ifq); 1020 #endif 1021 n = ifq->ifq_head; 1022 while ((m = n) != 0) { 1023 n = m->m_act; 1024 m_freem(m); 1025 } 1026 ifq->ifq_head = 0; 1027 ifq->ifq_tail = 0; 1028 ifq->ifq_len = 0; 1029 IFQ_UNLOCK(ifq); 1030 } 1031 1032 /* 1033 * Handle interface watchdog timer routines. Called 1034 * from softclock, we decrement timers (if set) and 1035 * call the appropriate interface routine on expiration. 1036 * 1037 * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called 1038 * holding Giant. If we switch to an MPSAFE callout, we likely need to grab 1039 * Giant before entering if_watchdog() on an IFF_NEEDSGIANT interface. 1040 */ 1041 static void 1042 if_slowtimo(void *arg) 1043 { 1044 struct ifnet *ifp; 1045 int s = splimp(); 1046 1047 IFNET_RLOCK(); 1048 TAILQ_FOREACH(ifp, &ifnet, if_link) { 1049 if (ifp->if_timer == 0 || --ifp->if_timer) 1050 continue; 1051 if (ifp->if_watchdog) 1052 (*ifp->if_watchdog)(ifp); 1053 } 1054 IFNET_RUNLOCK(); 1055 splx(s); 1056 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ); 1057 } 1058 1059 /* 1060 * Map interface name to 1061 * interface structure pointer. 1062 */ 1063 struct ifnet * 1064 ifunit(const char *name) 1065 { 1066 struct ifnet *ifp; 1067 1068 IFNET_RLOCK(); 1069 TAILQ_FOREACH(ifp, &ifnet, if_link) { 1070 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0) 1071 break; 1072 } 1073 IFNET_RUNLOCK(); 1074 return (ifp); 1075 } 1076 1077 /* 1078 * Hardware specific interface ioctls. 1079 */ 1080 static int 1081 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td) 1082 { 1083 struct ifreq *ifr; 1084 struct ifstat *ifs; 1085 int error = 0; 1086 int new_flags; 1087 size_t namelen, onamelen; 1088 char new_name[IFNAMSIZ]; 1089 struct ifaddr *ifa; 1090 struct sockaddr_dl *sdl; 1091 1092 ifr = (struct ifreq *)data; 1093 switch (cmd) { 1094 case SIOCGIFINDEX: 1095 ifr->ifr_index = ifp->if_index; 1096 break; 1097 1098 case SIOCGIFFLAGS: 1099 ifr->ifr_flags = ifp->if_flags & 0xffff; 1100 ifr->ifr_flagshigh = ifp->if_flags >> 16; 1101 break; 1102 1103 case SIOCGIFCAP: 1104 ifr->ifr_reqcap = ifp->if_capabilities; 1105 ifr->ifr_curcap = ifp->if_capenable; 1106 break; 1107 1108 #ifdef MAC 1109 case SIOCGIFMAC: 1110 error = mac_ioctl_ifnet_get(td->td_ucred, ifr, ifp); 1111 break; 1112 #endif 1113 1114 case SIOCGIFMETRIC: 1115 ifr->ifr_metric = ifp->if_metric; 1116 break; 1117 1118 case SIOCGIFMTU: 1119 ifr->ifr_mtu = ifp->if_mtu; 1120 break; 1121 1122 case SIOCGIFPHYS: 1123 ifr->ifr_phys = ifp->if_physical; 1124 break; 1125 1126 case SIOCSIFFLAGS: 1127 error = suser(td); 1128 if (error) 1129 return (error); 1130 new_flags = (ifr->ifr_flags & 0xffff) | 1131 (ifr->ifr_flagshigh << 16); 1132 if (ifp->if_flags & IFF_SMART) { 1133 /* Smart drivers twiddle their own routes */ 1134 } else if (ifp->if_flags & IFF_UP && 1135 (new_flags & IFF_UP) == 0) { 1136 int s = splimp(); 1137 if_down(ifp); 1138 splx(s); 1139 } else if (new_flags & IFF_UP && 1140 (ifp->if_flags & IFF_UP) == 0) { 1141 int s = splimp(); 1142 if_up(ifp); 1143 splx(s); 1144 } 1145 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 1146 (new_flags &~ IFF_CANTCHANGE); 1147 if (new_flags & IFF_PPROMISC) { 1148 /* Permanently promiscuous mode requested */ 1149 ifp->if_flags |= IFF_PROMISC; 1150 } else if (ifp->if_pcount == 0) { 1151 ifp->if_flags &= ~IFF_PROMISC; 1152 } 1153 if (ifp->if_ioctl) { 1154 IFF_LOCKGIANT(ifp); 1155 (void) (*ifp->if_ioctl)(ifp, cmd, data); 1156 IFF_UNLOCKGIANT(ifp); 1157 } 1158 getmicrotime(&ifp->if_lastchange); 1159 break; 1160 1161 case SIOCSIFCAP: 1162 error = suser(td); 1163 if (error) 1164 return (error); 1165 if (ifp->if_ioctl == NULL) 1166 return (EOPNOTSUPP); 1167 if (ifr->ifr_reqcap & ~ifp->if_capabilities) 1168 return (EINVAL); 1169 IFF_LOCKGIANT(ifp); 1170 error = (*ifp->if_ioctl)(ifp, cmd, data); 1171 IFF_UNLOCKGIANT(ifp); 1172 if (error == 0) 1173 getmicrotime(&ifp->if_lastchange); 1174 break; 1175 1176 #ifdef MAC 1177 case SIOCSIFMAC: 1178 error = mac_ioctl_ifnet_set(td->td_ucred, ifr, ifp); 1179 break; 1180 #endif 1181 1182 case SIOCSIFNAME: 1183 error = suser(td); 1184 if (error != 0) 1185 return (error); 1186 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL); 1187 if (error != 0) 1188 return (error); 1189 if (new_name[0] == '\0') 1190 return (EINVAL); 1191 if (ifunit(new_name) != NULL) 1192 return (EEXIST); 1193 1194 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 1195 /* Announce the departure of the interface. */ 1196 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 1197 1198 log(LOG_INFO, "%s: changing name to '%s'\n", 1199 ifp->if_xname, new_name); 1200 1201 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname)); 1202 ifa = ifaddr_byindex(ifp->if_index); 1203 IFA_LOCK(ifa); 1204 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 1205 namelen = strlen(new_name); 1206 onamelen = sdl->sdl_nlen; 1207 /* 1208 * Move the address if needed. This is safe because we 1209 * allocate space for a name of length IFNAMSIZ when we 1210 * create this in if_attach(). 1211 */ 1212 if (namelen != onamelen) { 1213 bcopy(sdl->sdl_data + onamelen, 1214 sdl->sdl_data + namelen, sdl->sdl_alen); 1215 } 1216 bcopy(new_name, sdl->sdl_data, namelen); 1217 sdl->sdl_nlen = namelen; 1218 sdl = (struct sockaddr_dl *)ifa->ifa_netmask; 1219 bzero(sdl->sdl_data, onamelen); 1220 while (namelen != 0) 1221 sdl->sdl_data[--namelen] = 0xff; 1222 IFA_UNLOCK(ifa); 1223 1224 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 1225 /* Announce the return of the interface. */ 1226 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 1227 break; 1228 1229 case SIOCSIFMETRIC: 1230 error = suser(td); 1231 if (error) 1232 return (error); 1233 ifp->if_metric = ifr->ifr_metric; 1234 getmicrotime(&ifp->if_lastchange); 1235 break; 1236 1237 case SIOCSIFPHYS: 1238 error = suser(td); 1239 if (error) 1240 return (error); 1241 if (ifp->if_ioctl == NULL) 1242 return (EOPNOTSUPP); 1243 IFF_LOCKGIANT(ifp); 1244 error = (*ifp->if_ioctl)(ifp, cmd, data); 1245 IFF_UNLOCKGIANT(ifp); 1246 if (error == 0) 1247 getmicrotime(&ifp->if_lastchange); 1248 break; 1249 1250 case SIOCSIFMTU: 1251 { 1252 u_long oldmtu = ifp->if_mtu; 1253 1254 error = suser(td); 1255 if (error) 1256 return (error); 1257 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) 1258 return (EINVAL); 1259 if (ifp->if_ioctl == NULL) 1260 return (EOPNOTSUPP); 1261 IFF_LOCKGIANT(ifp); 1262 error = (*ifp->if_ioctl)(ifp, cmd, data); 1263 IFF_UNLOCKGIANT(ifp); 1264 if (error == 0) { 1265 getmicrotime(&ifp->if_lastchange); 1266 rt_ifmsg(ifp); 1267 } 1268 /* 1269 * If the link MTU changed, do network layer specific procedure. 1270 */ 1271 if (ifp->if_mtu != oldmtu) { 1272 #ifdef INET6 1273 nd6_setmtu(ifp); 1274 #endif 1275 } 1276 break; 1277 } 1278 1279 case SIOCADDMULTI: 1280 case SIOCDELMULTI: 1281 error = suser(td); 1282 if (error) 1283 return (error); 1284 1285 /* Don't allow group membership on non-multicast interfaces. */ 1286 if ((ifp->if_flags & IFF_MULTICAST) == 0) 1287 return (EOPNOTSUPP); 1288 1289 /* Don't let users screw up protocols' entries. */ 1290 if (ifr->ifr_addr.sa_family != AF_LINK) 1291 return (EINVAL); 1292 1293 if (cmd == SIOCADDMULTI) { 1294 struct ifmultiaddr *ifma; 1295 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 1296 } else { 1297 error = if_delmulti(ifp, &ifr->ifr_addr); 1298 } 1299 if (error == 0) 1300 getmicrotime(&ifp->if_lastchange); 1301 break; 1302 1303 case SIOCSIFPHYADDR: 1304 case SIOCDIFPHYADDR: 1305 #ifdef INET6 1306 case SIOCSIFPHYADDR_IN6: 1307 #endif 1308 case SIOCSLIFPHYADDR: 1309 case SIOCSIFMEDIA: 1310 case SIOCSIFGENERIC: 1311 error = suser(td); 1312 if (error) 1313 return (error); 1314 if (ifp->if_ioctl == NULL) 1315 return (EOPNOTSUPP); 1316 IFF_LOCKGIANT(ifp); 1317 error = (*ifp->if_ioctl)(ifp, cmd, data); 1318 IFF_UNLOCKGIANT(ifp); 1319 if (error == 0) 1320 getmicrotime(&ifp->if_lastchange); 1321 break; 1322 1323 case SIOCGIFSTATUS: 1324 ifs = (struct ifstat *)data; 1325 ifs->ascii[0] = '\0'; 1326 1327 case SIOCGIFPSRCADDR: 1328 case SIOCGIFPDSTADDR: 1329 case SIOCGLIFPHYADDR: 1330 case SIOCGIFMEDIA: 1331 case SIOCGIFGENERIC: 1332 if (ifp->if_ioctl == NULL) 1333 return (EOPNOTSUPP); 1334 IFF_LOCKGIANT(ifp); 1335 error = (*ifp->if_ioctl)(ifp, cmd, data); 1336 IFF_UNLOCKGIANT(ifp); 1337 break; 1338 1339 case SIOCSIFLLADDR: 1340 error = suser(td); 1341 if (error) 1342 return (error); 1343 error = if_setlladdr(ifp, 1344 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); 1345 break; 1346 1347 default: 1348 error = ENOIOCTL; 1349 break; 1350 } 1351 return (error); 1352 } 1353 1354 /* 1355 * Interface ioctls. 1356 */ 1357 int 1358 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td) 1359 { 1360 struct ifnet *ifp; 1361 struct ifreq *ifr; 1362 int error; 1363 int oif_flags; 1364 1365 switch (cmd) { 1366 case SIOCGIFCONF: 1367 case OSIOCGIFCONF: 1368 return (ifconf(cmd, data)); 1369 } 1370 ifr = (struct ifreq *)data; 1371 1372 switch (cmd) { 1373 case SIOCIFCREATE: 1374 case SIOCIFDESTROY: 1375 if ((error = suser(td)) != 0) 1376 return (error); 1377 return ((cmd == SIOCIFCREATE) ? 1378 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) : 1379 if_clone_destroy(ifr->ifr_name)); 1380 1381 case SIOCIFGCLONERS: 1382 return (if_clone_list((struct if_clonereq *)data)); 1383 } 1384 1385 ifp = ifunit(ifr->ifr_name); 1386 if (ifp == 0) 1387 return (ENXIO); 1388 1389 error = ifhwioctl(cmd, ifp, data, td); 1390 if (error != ENOIOCTL) 1391 return (error); 1392 1393 oif_flags = ifp->if_flags; 1394 if (so->so_proto == 0) 1395 return (EOPNOTSUPP); 1396 #ifndef COMPAT_43 1397 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, 1398 data, 1399 ifp, td)); 1400 #else 1401 { 1402 int ocmd = cmd; 1403 1404 switch (cmd) { 1405 1406 case SIOCSIFDSTADDR: 1407 case SIOCSIFADDR: 1408 case SIOCSIFBRDADDR: 1409 case SIOCSIFNETMASK: 1410 #if BYTE_ORDER != BIG_ENDIAN 1411 if (ifr->ifr_addr.sa_family == 0 && 1412 ifr->ifr_addr.sa_len < 16) { 1413 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 1414 ifr->ifr_addr.sa_len = 16; 1415 } 1416 #else 1417 if (ifr->ifr_addr.sa_len == 0) 1418 ifr->ifr_addr.sa_len = 16; 1419 #endif 1420 break; 1421 1422 case OSIOCGIFADDR: 1423 cmd = SIOCGIFADDR; 1424 break; 1425 1426 case OSIOCGIFDSTADDR: 1427 cmd = SIOCGIFDSTADDR; 1428 break; 1429 1430 case OSIOCGIFBRDADDR: 1431 cmd = SIOCGIFBRDADDR; 1432 break; 1433 1434 case OSIOCGIFNETMASK: 1435 cmd = SIOCGIFNETMASK; 1436 } 1437 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, 1438 cmd, 1439 data, 1440 ifp, td)); 1441 switch (ocmd) { 1442 1443 case OSIOCGIFADDR: 1444 case OSIOCGIFDSTADDR: 1445 case OSIOCGIFBRDADDR: 1446 case OSIOCGIFNETMASK: 1447 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 1448 1449 } 1450 } 1451 #endif /* COMPAT_43 */ 1452 1453 if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 1454 #ifdef INET6 1455 DELAY(100);/* XXX: temporary workaround for fxp issue*/ 1456 if (ifp->if_flags & IFF_UP) { 1457 int s = splimp(); 1458 in6_if_up(ifp); 1459 splx(s); 1460 } 1461 #endif 1462 } 1463 return (error); 1464 } 1465 1466 /* 1467 * Set/clear promiscuous mode on interface ifp based on the truth value 1468 * of pswitch. The calls are reference counted so that only the first 1469 * "on" request actually has an effect, as does the final "off" request. 1470 * Results are undefined if the "off" and "on" requests are not matched. 1471 */ 1472 int 1473 ifpromisc(struct ifnet *ifp, int pswitch) 1474 { 1475 struct ifreq ifr; 1476 int error; 1477 int oldflags, oldpcount; 1478 1479 oldpcount = ifp->if_pcount; 1480 oldflags = ifp->if_flags; 1481 if (ifp->if_flags & IFF_PPROMISC) { 1482 /* Do nothing if device is in permanently promiscuous mode */ 1483 ifp->if_pcount += pswitch ? 1 : -1; 1484 return (0); 1485 } 1486 if (pswitch) { 1487 /* 1488 * If the device is not configured up, we cannot put it in 1489 * promiscuous mode. 1490 */ 1491 if ((ifp->if_flags & IFF_UP) == 0) 1492 return (ENETDOWN); 1493 if (ifp->if_pcount++ != 0) 1494 return (0); 1495 ifp->if_flags |= IFF_PROMISC; 1496 } else { 1497 if (--ifp->if_pcount > 0) 1498 return (0); 1499 ifp->if_flags &= ~IFF_PROMISC; 1500 } 1501 ifr.ifr_flags = ifp->if_flags & 0xffff; 1502 ifr.ifr_flagshigh = ifp->if_flags >> 16; 1503 IFF_LOCKGIANT(ifp); 1504 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 1505 IFF_UNLOCKGIANT(ifp); 1506 if (error == 0) { 1507 log(LOG_INFO, "%s: promiscuous mode %s\n", 1508 ifp->if_xname, 1509 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); 1510 rt_ifmsg(ifp); 1511 } else { 1512 ifp->if_pcount = oldpcount; 1513 ifp->if_flags = oldflags; 1514 } 1515 return error; 1516 } 1517 1518 /* 1519 * Return interface configuration 1520 * of system. List may be used 1521 * in later ioctl's (above) to get 1522 * other information. 1523 */ 1524 /*ARGSUSED*/ 1525 static int 1526 ifconf(u_long cmd, caddr_t data) 1527 { 1528 struct ifconf *ifc = (struct ifconf *)data; 1529 struct ifnet *ifp; 1530 struct ifaddr *ifa; 1531 struct ifreq ifr; 1532 struct sbuf *sb; 1533 int error, full = 0, valid_len, max_len; 1534 1535 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */ 1536 max_len = MAXPHYS - 1; 1537 1538 again: 1539 if (ifc->ifc_len <= max_len) { 1540 max_len = ifc->ifc_len; 1541 full = 1; 1542 } 1543 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN); 1544 max_len = 0; 1545 valid_len = 0; 1546 1547 IFNET_RLOCK(); /* could sleep XXX */ 1548 TAILQ_FOREACH(ifp, &ifnet, if_link) { 1549 int addrs; 1550 1551 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)) 1552 >= sizeof(ifr.ifr_name)) 1553 return (ENAMETOOLONG); 1554 1555 addrs = 0; 1556 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1557 struct sockaddr *sa = ifa->ifa_addr; 1558 1559 if (jailed(curthread->td_ucred) && 1560 prison_if(curthread->td_ucred, sa)) 1561 continue; 1562 addrs++; 1563 #ifdef COMPAT_43 1564 if (cmd == OSIOCGIFCONF) { 1565 struct osockaddr *osa = 1566 (struct osockaddr *)&ifr.ifr_addr; 1567 ifr.ifr_addr = *sa; 1568 osa->sa_family = sa->sa_family; 1569 sbuf_bcat(sb, &ifr, sizeof(ifr)); 1570 max_len += sizeof(ifr); 1571 } else 1572 #endif 1573 if (sa->sa_len <= sizeof(*sa)) { 1574 ifr.ifr_addr = *sa; 1575 sbuf_bcat(sb, &ifr, sizeof(ifr)); 1576 max_len += sizeof(ifr); 1577 } else { 1578 sbuf_bcat(sb, &ifr, 1579 offsetof(struct ifreq, ifr_addr)); 1580 max_len += offsetof(struct ifreq, ifr_addr); 1581 sbuf_bcat(sb, sa, sa->sa_len); 1582 max_len += sa->sa_len; 1583 } 1584 1585 if (!sbuf_overflowed(sb)) 1586 valid_len = sbuf_len(sb); 1587 } 1588 if (addrs == 0) { 1589 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 1590 sbuf_bcat(sb, &ifr, sizeof(ifr)); 1591 max_len += sizeof(ifr); 1592 1593 if (!sbuf_overflowed(sb)) 1594 valid_len = sbuf_len(sb); 1595 } 1596 } 1597 IFNET_RUNLOCK(); 1598 1599 /* 1600 * If we didn't allocate enough space (uncommon), try again. If 1601 * we have already allocated as much space as we are allowed, 1602 * return what we've got. 1603 */ 1604 if (valid_len != max_len && !full) { 1605 sbuf_delete(sb); 1606 goto again; 1607 } 1608 1609 ifc->ifc_len = valid_len; 1610 sbuf_finish(sb); 1611 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len); 1612 sbuf_delete(sb); 1613 return (error); 1614 } 1615 1616 /* 1617 * Just like if_promisc(), but for all-multicast-reception mode. 1618 */ 1619 int 1620 if_allmulti(struct ifnet *ifp, int onswitch) 1621 { 1622 int error = 0; 1623 int s = splimp(); 1624 struct ifreq ifr; 1625 1626 if (onswitch) { 1627 if (ifp->if_amcount++ == 0) { 1628 ifp->if_flags |= IFF_ALLMULTI; 1629 ifr.ifr_flags = ifp->if_flags & 0xffff; 1630 ifr.ifr_flagshigh = ifp->if_flags >> 16; 1631 IFF_LOCKGIANT(ifp); 1632 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 1633 IFF_UNLOCKGIANT(ifp); 1634 } 1635 } else { 1636 if (ifp->if_amcount > 1) { 1637 ifp->if_amcount--; 1638 } else { 1639 ifp->if_amcount = 0; 1640 ifp->if_flags &= ~IFF_ALLMULTI; 1641 ifr.ifr_flags = ifp->if_flags & 0xffff;; 1642 ifr.ifr_flagshigh = ifp->if_flags >> 16; 1643 IFF_LOCKGIANT(ifp); 1644 error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 1645 IFF_UNLOCKGIANT(ifp); 1646 } 1647 } 1648 splx(s); 1649 1650 if (error == 0) 1651 rt_ifmsg(ifp); 1652 return error; 1653 } 1654 1655 /* 1656 * Add a multicast listenership to the interface in question. 1657 * The link layer provides a routine which converts 1658 */ 1659 int 1660 if_addmulti(struct ifnet *ifp, struct sockaddr *sa, struct ifmultiaddr **retifma) 1661 { 1662 struct sockaddr *llsa, *dupsa; 1663 int error, s; 1664 struct ifmultiaddr *ifma; 1665 1666 /* 1667 * If the matching multicast address already exists 1668 * then don't add a new one, just add a reference 1669 */ 1670 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1671 if (equal(sa, ifma->ifma_addr)) { 1672 ifma->ifma_refcount++; 1673 if (retifma) 1674 *retifma = ifma; 1675 return 0; 1676 } 1677 } 1678 1679 /* 1680 * Give the link layer a chance to accept/reject it, and also 1681 * find out which AF_LINK address this maps to, if it isn't one 1682 * already. 1683 */ 1684 if (ifp->if_resolvemulti) { 1685 error = ifp->if_resolvemulti(ifp, &llsa, sa); 1686 if (error) return error; 1687 } else { 1688 llsa = 0; 1689 } 1690 1691 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK); 1692 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK); 1693 bcopy(sa, dupsa, sa->sa_len); 1694 1695 ifma->ifma_addr = dupsa; 1696 ifma->ifma_lladdr = llsa; 1697 ifma->ifma_ifp = ifp; 1698 ifma->ifma_refcount = 1; 1699 ifma->ifma_protospec = 0; 1700 rt_newmaddrmsg(RTM_NEWMADDR, ifma); 1701 1702 /* 1703 * Some network interfaces can scan the address list at 1704 * interrupt time; lock them out. 1705 */ 1706 s = splimp(); 1707 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 1708 splx(s); 1709 if (retifma != NULL) 1710 *retifma = ifma; 1711 1712 if (llsa != 0) { 1713 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1714 if (equal(ifma->ifma_addr, llsa)) 1715 break; 1716 } 1717 if (ifma) { 1718 ifma->ifma_refcount++; 1719 } else { 1720 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, 1721 M_IFMADDR, M_WAITOK); 1722 MALLOC(dupsa, struct sockaddr *, llsa->sa_len, 1723 M_IFMADDR, M_WAITOK); 1724 bcopy(llsa, dupsa, llsa->sa_len); 1725 ifma->ifma_addr = dupsa; 1726 ifma->ifma_lladdr = NULL; 1727 ifma->ifma_ifp = ifp; 1728 ifma->ifma_refcount = 1; 1729 ifma->ifma_protospec = 0; 1730 s = splimp(); 1731 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 1732 splx(s); 1733 } 1734 } 1735 /* 1736 * We are certain we have added something, so call down to the 1737 * interface to let them know about it. 1738 */ 1739 s = splimp(); 1740 IFF_LOCKGIANT(ifp); 1741 ifp->if_ioctl(ifp, SIOCADDMULTI, 0); 1742 IFF_UNLOCKGIANT(ifp); 1743 splx(s); 1744 1745 return 0; 1746 } 1747 1748 /* 1749 * Remove a reference to a multicast address on this interface. Yell 1750 * if the request does not match an existing membership. 1751 */ 1752 int 1753 if_delmulti(struct ifnet *ifp, struct sockaddr *sa) 1754 { 1755 struct ifmultiaddr *ifma; 1756 int s; 1757 1758 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 1759 if (equal(sa, ifma->ifma_addr)) 1760 break; 1761 if (ifma == 0) 1762 return ENOENT; 1763 1764 if (ifma->ifma_refcount > 1) { 1765 ifma->ifma_refcount--; 1766 return 0; 1767 } 1768 1769 rt_newmaddrmsg(RTM_DELMADDR, ifma); 1770 sa = ifma->ifma_lladdr; 1771 s = splimp(); 1772 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 1773 /* 1774 * Make sure the interface driver is notified 1775 * in the case of a link layer mcast group being left. 1776 */ 1777 if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0) { 1778 IFF_LOCKGIANT(ifp); 1779 ifp->if_ioctl(ifp, SIOCDELMULTI, 0); 1780 IFF_UNLOCKGIANT(ifp); 1781 } 1782 splx(s); 1783 free(ifma->ifma_addr, M_IFMADDR); 1784 free(ifma, M_IFMADDR); 1785 if (sa == 0) 1786 return 0; 1787 1788 /* 1789 * Now look for the link-layer address which corresponds to 1790 * this network address. It had been squirreled away in 1791 * ifma->ifma_lladdr for this purpose (so we don't have 1792 * to call ifp->if_resolvemulti() again), and we saved that 1793 * value in sa above. If some nasty deleted the 1794 * link-layer address out from underneath us, we can deal because 1795 * the address we stored was is not the same as the one which was 1796 * in the record for the link-layer address. (So we don't complain 1797 * in that case.) 1798 */ 1799 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 1800 if (equal(sa, ifma->ifma_addr)) 1801 break; 1802 if (ifma == 0) 1803 return 0; 1804 1805 if (ifma->ifma_refcount > 1) { 1806 ifma->ifma_refcount--; 1807 return 0; 1808 } 1809 1810 s = splimp(); 1811 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 1812 IFF_LOCKGIANT(ifp); 1813 ifp->if_ioctl(ifp, SIOCDELMULTI, 0); 1814 IFF_UNLOCKGIANT(ifp); 1815 splx(s); 1816 free(ifma->ifma_addr, M_IFMADDR); 1817 free(sa, M_IFMADDR); 1818 free(ifma, M_IFMADDR); 1819 1820 return 0; 1821 } 1822 1823 /* 1824 * Set the link layer address on an interface. 1825 * 1826 * At this time we only support certain types of interfaces, 1827 * and we don't allow the length of the address to change. 1828 */ 1829 int 1830 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 1831 { 1832 struct sockaddr_dl *sdl; 1833 struct ifaddr *ifa; 1834 struct ifreq ifr; 1835 1836 ifa = ifaddr_byindex(ifp->if_index); 1837 if (ifa == NULL) 1838 return (EINVAL); 1839 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 1840 if (sdl == NULL) 1841 return (EINVAL); 1842 if (len != sdl->sdl_alen) /* don't allow length to change */ 1843 return (EINVAL); 1844 switch (ifp->if_type) { 1845 case IFT_ETHER: /* these types use struct arpcom */ 1846 case IFT_FDDI: 1847 case IFT_XETHER: 1848 case IFT_ISO88025: 1849 case IFT_L2VLAN: 1850 bcopy(lladdr, IFP2AC(ifp)->ac_enaddr, len); 1851 /* 1852 * XXX We also need to store the lladdr in LLADDR(sdl), 1853 * which is done below. This is a pain because we must 1854 * remember to keep the info in sync. 1855 */ 1856 /* FALLTHROUGH */ 1857 case IFT_ARCNET: 1858 bcopy(lladdr, LLADDR(sdl), len); 1859 break; 1860 default: 1861 return (ENODEV); 1862 } 1863 /* 1864 * If the interface is already up, we need 1865 * to re-init it in order to reprogram its 1866 * address filter. 1867 */ 1868 if ((ifp->if_flags & IFF_UP) != 0) { 1869 IFF_LOCKGIANT(ifp); 1870 ifp->if_flags &= ~IFF_UP; 1871 ifr.ifr_flags = ifp->if_flags & 0xffff; 1872 ifr.ifr_flagshigh = ifp->if_flags >> 16; 1873 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 1874 ifp->if_flags |= IFF_UP; 1875 ifr.ifr_flags = ifp->if_flags & 0xffff; 1876 ifr.ifr_flagshigh = ifp->if_flags >> 16; 1877 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 1878 IFF_UNLOCKGIANT(ifp); 1879 #ifdef INET 1880 /* 1881 * Also send gratuitous ARPs to notify other nodes about 1882 * the address change. 1883 */ 1884 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1885 if (ifa->ifa_addr != NULL && 1886 ifa->ifa_addr->sa_family == AF_INET) 1887 arp_ifinit(ifp, ifa); 1888 } 1889 #endif 1890 } 1891 return (0); 1892 } 1893 1894 struct ifmultiaddr * 1895 ifmaof_ifpforaddr(struct sockaddr *sa, struct ifnet *ifp) 1896 { 1897 struct ifmultiaddr *ifma; 1898 1899 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) 1900 if (equal(ifma->ifma_addr, sa)) 1901 break; 1902 1903 return ifma; 1904 } 1905 1906 /* 1907 * The name argument must be a pointer to storage which will last as 1908 * long as the interface does. For physical devices, the result of 1909 * device_get_name(dev) is a good choice and for pseudo-devices a 1910 * static string works well. 1911 */ 1912 void 1913 if_initname(struct ifnet *ifp, const char *name, int unit) 1914 { 1915 ifp->if_dname = name; 1916 ifp->if_dunit = unit; 1917 if (unit != IF_DUNIT_NONE) 1918 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit); 1919 else 1920 strlcpy(ifp->if_xname, name, IFNAMSIZ); 1921 } 1922 1923 int 1924 if_printf(struct ifnet *ifp, const char * fmt, ...) 1925 { 1926 va_list ap; 1927 int retval; 1928 1929 retval = printf("%s: ", ifp->if_xname); 1930 va_start(ap, fmt); 1931 retval += vprintf(fmt, ap); 1932 va_end(ap); 1933 return (retval); 1934 } 1935 1936 /* 1937 * When an interface is marked IFF_NEEDSGIANT, its if_start() routine cannot 1938 * be called without Giant. However, we often can't acquire the Giant lock 1939 * at those points; instead, we run it via a task queue that holds Giant via 1940 * if_start_deferred. 1941 * 1942 * XXXRW: We need to make sure that the ifnet isn't fully detached until any 1943 * outstanding if_start_deferred() tasks that will run after the free. This 1944 * probably means waiting in if_detach(). 1945 */ 1946 void 1947 if_start(struct ifnet *ifp) 1948 { 1949 1950 NET_ASSERT_GIANT(); 1951 1952 if ((ifp->if_flags & IFF_NEEDSGIANT) != 0 && debug_mpsafenet != 0) { 1953 if (mtx_owned(&Giant)) 1954 (*(ifp)->if_start)(ifp); 1955 else 1956 taskqueue_enqueue(taskqueue_swi_giant, 1957 &ifp->if_starttask); 1958 } else 1959 (*(ifp)->if_start)(ifp); 1960 } 1961 1962 static void 1963 if_start_deferred(void *context, int pending) 1964 { 1965 struct ifnet *ifp; 1966 1967 /* 1968 * This code must be entered with Giant, and should never run if 1969 * we're not running with debug.mpsafenet. 1970 */ 1971 KASSERT(debug_mpsafenet != 0, ("if_start_deferred: debug.mpsafenet")); 1972 GIANT_REQUIRED; 1973 1974 ifp = (struct ifnet *)context; 1975 (ifp->if_start)(ifp); 1976 } 1977 1978 int 1979 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust) 1980 { 1981 int active = 0; 1982 1983 IF_LOCK(ifq); 1984 if (_IF_QFULL(ifq)) { 1985 _IF_DROP(ifq); 1986 IF_UNLOCK(ifq); 1987 m_freem(m); 1988 return (0); 1989 } 1990 if (ifp != NULL) { 1991 ifp->if_obytes += m->m_pkthdr.len + adjust; 1992 if (m->m_flags & (M_BCAST|M_MCAST)) 1993 ifp->if_omcasts++; 1994 active = ifp->if_flags & IFF_OACTIVE; 1995 } 1996 _IF_ENQUEUE(ifq, m); 1997 IF_UNLOCK(ifq); 1998 if (ifp != NULL && !active) 1999 if_start(ifp); 2000 return (1); 2001 } 2002 2003 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 2004 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); 2005