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 #include "opt_carp.h" 38 39 #include <sys/param.h> 40 #include <sys/types.h> 41 #include <sys/conf.h> 42 #include <sys/mac.h> 43 #include <sys/malloc.h> 44 #include <sys/sbuf.h> 45 #include <sys/bus.h> 46 #include <sys/mbuf.h> 47 #include <sys/systm.h> 48 #include <sys/proc.h> 49 #include <sys/socket.h> 50 #include <sys/socketvar.h> 51 #include <sys/protosw.h> 52 #include <sys/kernel.h> 53 #include <sys/sockio.h> 54 #include <sys/syslog.h> 55 #include <sys/sysctl.h> 56 #include <sys/taskqueue.h> 57 #include <sys/domain.h> 58 #include <sys/jail.h> 59 #include <machine/stdarg.h> 60 61 #include <net/if.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 #ifdef DEV_CARP 82 #include <netinet/ip_carp.h> 83 #endif 84 85 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 86 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); 87 88 /* Log link state change events */ 89 static int log_link_state_change = 1; 90 91 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW, 92 &log_link_state_change, 0, 93 "log interface link state change events"); 94 95 void (*bstp_linkstate_p)(struct ifnet *ifp, int state); 96 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state); 97 98 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL; 99 100 static void if_attachdomain(void *); 101 static void if_attachdomain1(struct ifnet *); 102 static int ifconf(u_long, caddr_t); 103 static void if_grow(void); 104 static void if_init(void *); 105 static void if_check(void *); 106 static void if_qflush(struct ifaltq *); 107 static void if_route(struct ifnet *, int flag, int fam); 108 static int if_setflag(struct ifnet *, int, int, int *, int); 109 static void if_slowtimo(void *); 110 static void if_unroute(struct ifnet *, int flag, int fam); 111 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 112 static int if_rtdel(struct radix_node *, void *); 113 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *); 114 static void if_start_deferred(void *context, int pending); 115 static void do_link_state_change(void *, int); 116 #ifdef INET6 117 /* 118 * XXX: declare here to avoid to include many inet6 related files.. 119 * should be more generalized? 120 */ 121 extern void nd6_setmtu(struct ifnet *); 122 #endif 123 124 int if_index = 0; 125 struct ifindex_entry *ifindex_table = NULL; 126 int ifqmaxlen = IFQ_MAXLEN; 127 struct ifnethead ifnet; /* depend on static init XXX */ 128 struct mtx ifnet_lock; 129 static if_com_alloc_t *if_com_alloc[256]; 130 static if_com_free_t *if_com_free[256]; 131 132 static int if_indexlim = 8; 133 static struct knlist ifklist; 134 135 static void filt_netdetach(struct knote *kn); 136 static int filt_netdev(struct knote *kn, long hint); 137 138 static struct filterops netdev_filtops = 139 { 1, NULL, filt_netdetach, filt_netdev }; 140 141 /* 142 * System initialization 143 */ 144 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL) 145 SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL) 146 147 MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals"); 148 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 149 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 150 151 static d_open_t netopen; 152 static d_close_t netclose; 153 static d_ioctl_t netioctl; 154 static d_kqfilter_t netkqfilter; 155 156 static struct cdevsw net_cdevsw = { 157 .d_version = D_VERSION, 158 .d_flags = D_NEEDGIANT, 159 .d_open = netopen, 160 .d_close = netclose, 161 .d_ioctl = netioctl, 162 .d_name = "net", 163 .d_kqfilter = netkqfilter, 164 }; 165 166 static int 167 netopen(struct cdev *dev, int flag, int mode, struct thread *td) 168 { 169 return (0); 170 } 171 172 static int 173 netclose(struct cdev *dev, int flags, int fmt, struct thread *td) 174 { 175 return (0); 176 } 177 178 static int 179 netioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 180 { 181 struct ifnet *ifp; 182 int error, idx; 183 184 /* only support interface specific ioctls */ 185 if (IOCGROUP(cmd) != 'i') 186 return (EOPNOTSUPP); 187 idx = minor(dev); 188 if (idx == 0) { 189 /* 190 * special network device, not interface. 191 */ 192 if (cmd == SIOCGIFCONF) 193 return (ifconf(cmd, data)); /* XXX remove cmd */ 194 #ifdef __amd64__ 195 if (cmd == SIOCGIFCONF32) 196 return (ifconf(cmd, data)); /* XXX remove cmd */ 197 #endif 198 return (EOPNOTSUPP); 199 } 200 201 ifp = ifnet_byindex(idx); 202 if (ifp == NULL) 203 return (ENXIO); 204 205 error = ifhwioctl(cmd, ifp, data, td); 206 if (error == ENOIOCTL) 207 error = EOPNOTSUPP; 208 return (error); 209 } 210 211 static int 212 netkqfilter(struct cdev *dev, struct knote *kn) 213 { 214 struct knlist *klist; 215 struct ifnet *ifp; 216 int idx; 217 218 switch (kn->kn_filter) { 219 case EVFILT_NETDEV: 220 kn->kn_fop = &netdev_filtops; 221 break; 222 default: 223 return (EINVAL); 224 } 225 226 idx = minor(dev); 227 if (idx == 0) { 228 klist = &ifklist; 229 } else { 230 ifp = ifnet_byindex(idx); 231 if (ifp == NULL) 232 return (1); 233 klist = &ifp->if_klist; 234 } 235 236 kn->kn_hook = (caddr_t)klist; 237 238 knlist_add(klist, kn, 0); 239 240 return (0); 241 } 242 243 static void 244 filt_netdetach(struct knote *kn) 245 { 246 struct knlist *klist = (struct knlist *)kn->kn_hook; 247 248 knlist_remove(klist, kn, 0); 249 } 250 251 static int 252 filt_netdev(struct knote *kn, long hint) 253 { 254 struct knlist *klist = (struct knlist *)kn->kn_hook; 255 256 /* 257 * Currently NOTE_EXIT is abused to indicate device detach. 258 */ 259 if (hint == NOTE_EXIT) { 260 kn->kn_data = NOTE_LINKINV; 261 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 262 knlist_remove_inevent(klist, kn); 263 return (1); 264 } 265 if (hint != 0) 266 kn->kn_data = hint; /* current status */ 267 if (kn->kn_sfflags & hint) 268 kn->kn_fflags |= hint; 269 return (kn->kn_fflags != 0); 270 } 271 272 /* 273 * Network interface utility routines. 274 * 275 * Routines with ifa_ifwith* names take sockaddr *'s as 276 * parameters. 277 */ 278 /* ARGSUSED*/ 279 static void 280 if_init(void *dummy __unused) 281 { 282 283 IFNET_LOCK_INIT(); 284 TAILQ_INIT(&ifnet); 285 knlist_init(&ifklist, NULL, NULL, NULL, NULL); 286 if_grow(); /* create initial table */ 287 ifdev_byindex(0) = make_dev(&net_cdevsw, 0, 288 UID_ROOT, GID_WHEEL, 0600, "network"); 289 if_clone_init(); 290 } 291 292 static void 293 if_grow(void) 294 { 295 u_int n; 296 struct ifindex_entry *e; 297 298 if_indexlim <<= 1; 299 n = if_indexlim * sizeof(*e); 300 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO); 301 if (ifindex_table != NULL) { 302 memcpy((caddr_t)e, (caddr_t)ifindex_table, n/2); 303 free((caddr_t)ifindex_table, M_IFNET); 304 } 305 ifindex_table = e; 306 } 307 308 /* ARGSUSED*/ 309 static void 310 if_check(void *dummy __unused) 311 { 312 struct ifnet *ifp; 313 int s; 314 315 s = splimp(); 316 IFNET_RLOCK(); /* could sleep on rare error; mostly okay XXX */ 317 TAILQ_FOREACH(ifp, &ifnet, if_link) { 318 if (ifp->if_snd.ifq_maxlen == 0) { 319 if_printf(ifp, "XXX: driver didn't set ifq_maxlen\n"); 320 ifp->if_snd.ifq_maxlen = ifqmaxlen; 321 } 322 if (!mtx_initialized(&ifp->if_snd.ifq_mtx)) { 323 if_printf(ifp, 324 "XXX: driver didn't initialize queue mtx\n"); 325 mtx_init(&ifp->if_snd.ifq_mtx, "unknown", 326 MTX_NETWORK_LOCK, MTX_DEF); 327 } 328 } 329 IFNET_RUNLOCK(); 330 splx(s); 331 if_slowtimo(0); 332 } 333 334 /* 335 * Allocate a struct ifnet and in index for an interface. 336 */ 337 struct ifnet* 338 if_alloc(u_char type) 339 { 340 struct ifnet *ifp; 341 342 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO); 343 344 /* 345 * Try to find an empty slot below if_index. If we fail, take 346 * the next slot. 347 * 348 * XXX: should be locked! 349 */ 350 for (ifp->if_index = 1; ifp->if_index <= if_index; ifp->if_index++) { 351 if (ifnet_byindex(ifp->if_index) == NULL) 352 break; 353 } 354 /* Catch if_index overflow. */ 355 if (ifp->if_index < 1) { 356 free(ifp, M_IFNET); 357 return (NULL); 358 } 359 if (ifp->if_index > if_index) 360 if_index = ifp->if_index; 361 if (if_index >= if_indexlim) 362 if_grow(); 363 ifnet_byindex(ifp->if_index) = ifp; 364 365 ifp->if_type = type; 366 367 if (if_com_alloc[type] != NULL) { 368 ifp->if_l2com = if_com_alloc[type](type, ifp); 369 if (ifp->if_l2com == NULL) { 370 free(ifp, M_IFNET); 371 return (NULL); 372 } 373 } 374 IF_ADDR_LOCK_INIT(ifp); 375 376 return (ifp); 377 } 378 379 void 380 if_free(struct ifnet *ifp) 381 { 382 383 /* Do not add code to this function! Add it to if_free_type(). */ 384 if_free_type(ifp, ifp->if_type); 385 } 386 387 void 388 if_free_type(struct ifnet *ifp, u_char type) 389 { 390 391 if (ifp != ifnet_byindex(ifp->if_index)) { 392 if_printf(ifp, "%s: value was not if_alloced, skipping\n", 393 __func__); 394 return; 395 } 396 397 IF_ADDR_LOCK_DESTROY(ifp); 398 399 ifnet_byindex(ifp->if_index) = NULL; 400 401 /* XXX: should be locked with if_findindex() */ 402 while (if_index > 0 && ifnet_byindex(if_index) == NULL) 403 if_index--; 404 405 if (if_com_free[type] != NULL) 406 if_com_free[type](ifp->if_l2com, type); 407 408 free(ifp, M_IFNET); 409 }; 410 411 /* 412 * Attach an interface to the 413 * list of "active" interfaces. 414 */ 415 void 416 if_attach(struct ifnet *ifp) 417 { 418 unsigned socksize, ifasize; 419 int namelen, masklen; 420 struct sockaddr_dl *sdl; 421 struct ifaddr *ifa; 422 423 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index)) 424 panic ("%s: BUG: if_attach called without if_alloc'd input()\n", 425 ifp->if_xname); 426 427 TASK_INIT(&ifp->if_starttask, 0, if_start_deferred, ifp); 428 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp); 429 IF_AFDATA_LOCK_INIT(ifp); 430 ifp->if_afdata_initialized = 0; 431 IFNET_WLOCK(); 432 TAILQ_INSERT_TAIL(&ifnet, ifp, if_link); 433 IFNET_WUNLOCK(); 434 /* 435 * XXX - 436 * The old code would work if the interface passed a pre-existing 437 * chain of ifaddrs to this code. We don't trust our callers to 438 * properly initialize the tailq, however, so we no longer allow 439 * this unlikely case. 440 */ 441 TAILQ_INIT(&ifp->if_addrhead); 442 TAILQ_INIT(&ifp->if_prefixhead); 443 TAILQ_INIT(&ifp->if_multiaddrs); 444 knlist_init(&ifp->if_klist, NULL, NULL, NULL, NULL); 445 getmicrotime(&ifp->if_lastchange); 446 ifp->if_data.ifi_epoch = time_uptime; 447 ifp->if_data.ifi_datalen = sizeof(struct if_data); 448 449 #ifdef MAC 450 mac_init_ifnet(ifp); 451 mac_create_ifnet(ifp); 452 #endif 453 454 ifdev_byindex(ifp->if_index) = make_dev(&net_cdevsw, 455 unit2minor(ifp->if_index), 456 UID_ROOT, GID_WHEEL, 0600, "%s/%s", 457 net_cdevsw.d_name, ifp->if_xname); 458 make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d", 459 net_cdevsw.d_name, ifp->if_index); 460 461 mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF); 462 463 /* 464 * create a Link Level name for this device 465 */ 466 namelen = strlen(ifp->if_xname); 467 /* 468 * Always save enough space for any possiable name so we can do 469 * a rename in place later. 470 */ 471 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ; 472 socksize = masklen + ifp->if_addrlen; 473 if (socksize < sizeof(*sdl)) 474 socksize = sizeof(*sdl); 475 socksize = roundup2(socksize, sizeof(long)); 476 ifasize = sizeof(*ifa) + 2 * socksize; 477 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO); 478 IFA_LOCK_INIT(ifa); 479 sdl = (struct sockaddr_dl *)(ifa + 1); 480 sdl->sdl_len = socksize; 481 sdl->sdl_family = AF_LINK; 482 bcopy(ifp->if_xname, sdl->sdl_data, namelen); 483 sdl->sdl_nlen = namelen; 484 sdl->sdl_index = ifp->if_index; 485 sdl->sdl_type = ifp->if_type; 486 ifp->if_addr = ifa; 487 ifa->ifa_ifp = ifp; 488 ifa->ifa_rtrequest = link_rtrequest; 489 ifa->ifa_addr = (struct sockaddr *)sdl; 490 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 491 ifa->ifa_netmask = (struct sockaddr *)sdl; 492 sdl->sdl_len = masklen; 493 while (namelen != 0) 494 sdl->sdl_data[--namelen] = 0xff; 495 ifa->ifa_refcnt = 1; 496 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link); 497 ifp->if_broadcastaddr = NULL; /* reliably crash if used uninitialized */ 498 ifp->if_snd.altq_type = 0; 499 ifp->if_snd.altq_disc = NULL; 500 ifp->if_snd.altq_flags &= ALTQF_CANTCHANGE; 501 ifp->if_snd.altq_tbr = NULL; 502 ifp->if_snd.altq_ifp = ifp; 503 504 if (domain_init_status >= 2) 505 if_attachdomain1(ifp); 506 507 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 508 509 /* Announce the interface. */ 510 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 511 } 512 513 static void 514 if_attachdomain(void *dummy) 515 { 516 struct ifnet *ifp; 517 int s; 518 519 s = splnet(); 520 TAILQ_FOREACH(ifp, &ifnet, if_link) 521 if_attachdomain1(ifp); 522 splx(s); 523 } 524 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND, 525 if_attachdomain, NULL); 526 527 static void 528 if_attachdomain1(struct ifnet *ifp) 529 { 530 struct domain *dp; 531 int s; 532 533 s = splnet(); 534 535 /* 536 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we 537 * cannot lock ifp->if_afdata initialization, entirely. 538 */ 539 if (IF_AFDATA_TRYLOCK(ifp) == 0) { 540 splx(s); 541 return; 542 } 543 if (ifp->if_afdata_initialized >= domain_init_status) { 544 IF_AFDATA_UNLOCK(ifp); 545 splx(s); 546 printf("if_attachdomain called more than once on %s\n", 547 ifp->if_xname); 548 return; 549 } 550 ifp->if_afdata_initialized = domain_init_status; 551 IF_AFDATA_UNLOCK(ifp); 552 553 /* address family dependent data region */ 554 bzero(ifp->if_afdata, sizeof(ifp->if_afdata)); 555 for (dp = domains; dp; dp = dp->dom_next) { 556 if (dp->dom_ifattach) 557 ifp->if_afdata[dp->dom_family] = 558 (*dp->dom_ifattach)(ifp); 559 } 560 561 splx(s); 562 } 563 564 /* 565 * Remove any network addresses from an interface. 566 */ 567 568 void 569 if_purgeaddrs(struct ifnet *ifp) 570 { 571 struct ifaddr *ifa, *next; 572 573 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) { 574 575 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_LINK) 576 continue; 577 #ifdef INET 578 /* XXX: Ugly!! ad hoc just for INET */ 579 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { 580 struct ifaliasreq ifr; 581 582 bzero(&ifr, sizeof(ifr)); 583 ifr.ifra_addr = *ifa->ifa_addr; 584 if (ifa->ifa_dstaddr) 585 ifr.ifra_broadaddr = *ifa->ifa_dstaddr; 586 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp, 587 NULL) == 0) 588 continue; 589 } 590 #endif /* INET */ 591 #ifdef INET6 592 if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) { 593 in6_purgeaddr(ifa); 594 /* ifp_addrhead is already updated */ 595 continue; 596 } 597 #endif /* INET6 */ 598 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 599 IFAFREE(ifa); 600 } 601 } 602 603 /* 604 * Detach an interface, removing it from the 605 * list of "active" interfaces. 606 * 607 * XXXRW: There are some significant questions about event ordering, and 608 * how to prevent things from starting to use the interface during detach. 609 */ 610 void 611 if_detach(struct ifnet *ifp) 612 { 613 struct ifaddr *ifa; 614 struct radix_node_head *rnh; 615 int s; 616 int i; 617 struct domain *dp; 618 struct ifnet *iter; 619 int found; 620 621 /* 622 * Remove/wait for pending events. 623 */ 624 taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 625 626 #ifdef DEV_CARP 627 /* Maybe hook to the generalized departure handler above?!? */ 628 if (ifp->if_carp) 629 carp_ifdetach(ifp); 630 #endif 631 632 /* 633 * Remove routes and flush queues. 634 */ 635 s = splnet(); 636 if_down(ifp); 637 #ifdef ALTQ 638 if (ALTQ_IS_ENABLED(&ifp->if_snd)) 639 altq_disable(&ifp->if_snd); 640 if (ALTQ_IS_ATTACHED(&ifp->if_snd)) 641 altq_detach(&ifp->if_snd); 642 #endif 643 644 if_purgeaddrs(ifp); 645 646 #ifdef INET 647 in_ifdetach(ifp); 648 #endif 649 650 #ifdef INET6 651 /* 652 * Remove all IPv6 kernel structs related to ifp. This should be done 653 * before removing routing entries below, since IPv6 interface direct 654 * routes are expected to be removed by the IPv6-specific kernel API. 655 * Otherwise, the kernel will detect some inconsistency and bark it. 656 */ 657 in6_ifdetach(ifp); 658 #endif 659 /* 660 * Remove link ifaddr pointer and maybe decrement if_index. 661 * Clean up all addresses. 662 */ 663 ifp->if_addr = NULL; 664 destroy_dev(ifdev_byindex(ifp->if_index)); 665 ifdev_byindex(ifp->if_index) = NULL; 666 667 /* We can now free link ifaddr. */ 668 if (!TAILQ_EMPTY(&ifp->if_addrhead)) { 669 ifa = TAILQ_FIRST(&ifp->if_addrhead); 670 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 671 IFAFREE(ifa); 672 } 673 674 /* 675 * Delete all remaining routes using this interface 676 * Unfortuneatly the only way to do this is to slog through 677 * the entire routing table looking for routes which point 678 * to this interface...oh well... 679 */ 680 for (i = 1; i <= AF_MAX; i++) { 681 if ((rnh = rt_tables[i]) == NULL) 682 continue; 683 RADIX_NODE_HEAD_LOCK(rnh); 684 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp); 685 RADIX_NODE_HEAD_UNLOCK(rnh); 686 } 687 688 /* Announce that the interface is gone. */ 689 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 690 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 691 692 IF_AFDATA_LOCK(ifp); 693 for (dp = domains; dp; dp = dp->dom_next) { 694 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) 695 (*dp->dom_ifdetach)(ifp, 696 ifp->if_afdata[dp->dom_family]); 697 } 698 IF_AFDATA_UNLOCK(ifp); 699 700 #ifdef MAC 701 mac_destroy_ifnet(ifp); 702 #endif /* MAC */ 703 KNOTE_UNLOCKED(&ifp->if_klist, NOTE_EXIT); 704 knlist_clear(&ifp->if_klist, 0); 705 knlist_destroy(&ifp->if_klist); 706 IFNET_WLOCK(); 707 found = 0; 708 TAILQ_FOREACH(iter, &ifnet, if_link) 709 if (iter == ifp) { 710 found = 1; 711 break; 712 } 713 if (found) 714 TAILQ_REMOVE(&ifnet, ifp, if_link); 715 IFNET_WUNLOCK(); 716 mtx_destroy(&ifp->if_snd.ifq_mtx); 717 IF_AFDATA_DESTROY(ifp); 718 splx(s); 719 } 720 721 /* 722 * Delete Routes for a Network Interface 723 * 724 * Called for each routing entry via the rnh->rnh_walktree() call above 725 * to delete all route entries referencing a detaching network interface. 726 * 727 * Arguments: 728 * rn pointer to node in the routing table 729 * arg argument passed to rnh->rnh_walktree() - detaching interface 730 * 731 * Returns: 732 * 0 successful 733 * errno failed - reason indicated 734 * 735 */ 736 static int 737 if_rtdel(struct radix_node *rn, void *arg) 738 { 739 struct rtentry *rt = (struct rtentry *)rn; 740 struct ifnet *ifp = arg; 741 int err; 742 743 if (rt->rt_ifp == ifp) { 744 745 /* 746 * Protect (sorta) against walktree recursion problems 747 * with cloned routes 748 */ 749 if ((rt->rt_flags & RTF_UP) == 0) 750 return (0); 751 752 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, 753 rt_mask(rt), rt->rt_flags, 754 (struct rtentry **) NULL); 755 if (err) { 756 log(LOG_WARNING, "if_rtdel: error %d\n", err); 757 } 758 } 759 760 return (0); 761 } 762 763 #define sa_equal(a1, a2) (bcmp((a1), (a2), ((a1))->sa_len) == 0) 764 765 /* 766 * Locate an interface based on a complete address. 767 */ 768 /*ARGSUSED*/ 769 struct ifaddr * 770 ifa_ifwithaddr(struct sockaddr *addr) 771 { 772 struct ifnet *ifp; 773 struct ifaddr *ifa; 774 775 IFNET_RLOCK(); 776 TAILQ_FOREACH(ifp, &ifnet, if_link) 777 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 778 if (ifa->ifa_addr->sa_family != addr->sa_family) 779 continue; 780 if (sa_equal(addr, ifa->ifa_addr)) 781 goto done; 782 /* IP6 doesn't have broadcast */ 783 if ((ifp->if_flags & IFF_BROADCAST) && 784 ifa->ifa_broadaddr && 785 ifa->ifa_broadaddr->sa_len != 0 && 786 sa_equal(ifa->ifa_broadaddr, addr)) 787 goto done; 788 } 789 ifa = NULL; 790 done: 791 IFNET_RUNLOCK(); 792 return (ifa); 793 } 794 795 /* 796 * Locate the point to point interface with a given destination address. 797 */ 798 /*ARGSUSED*/ 799 struct ifaddr * 800 ifa_ifwithdstaddr(struct sockaddr *addr) 801 { 802 struct ifnet *ifp; 803 struct ifaddr *ifa; 804 805 IFNET_RLOCK(); 806 TAILQ_FOREACH(ifp, &ifnet, if_link) { 807 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 808 continue; 809 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 810 if (ifa->ifa_addr->sa_family != addr->sa_family) 811 continue; 812 if (ifa->ifa_dstaddr && 813 sa_equal(addr, ifa->ifa_dstaddr)) 814 goto done; 815 } 816 } 817 ifa = NULL; 818 done: 819 IFNET_RUNLOCK(); 820 return (ifa); 821 } 822 823 /* 824 * Find an interface on a specific network. If many, choice 825 * is most specific found. 826 */ 827 struct ifaddr * 828 ifa_ifwithnet(struct sockaddr *addr) 829 { 830 struct ifnet *ifp; 831 struct ifaddr *ifa; 832 struct ifaddr *ifa_maybe = (struct ifaddr *) 0; 833 u_int af = addr->sa_family; 834 char *addr_data = addr->sa_data, *cplim; 835 836 /* 837 * AF_LINK addresses can be looked up directly by their index number, 838 * so do that if we can. 839 */ 840 if (af == AF_LINK) { 841 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; 842 if (sdl->sdl_index && sdl->sdl_index <= if_index) 843 return (ifaddr_byindex(sdl->sdl_index)); 844 } 845 846 /* 847 * Scan though each interface, looking for ones that have 848 * addresses in this address family. 849 */ 850 IFNET_RLOCK(); 851 TAILQ_FOREACH(ifp, &ifnet, if_link) { 852 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 853 char *cp, *cp2, *cp3; 854 855 if (ifa->ifa_addr->sa_family != af) 856 next: continue; 857 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) { 858 /* 859 * This is a bit broken as it doesn't 860 * take into account that the remote end may 861 * be a single node in the network we are 862 * looking for. 863 * The trouble is that we don't know the 864 * netmask for the remote end. 865 */ 866 if (ifa->ifa_dstaddr != 0 && 867 sa_equal(addr, ifa->ifa_dstaddr)) 868 goto done; 869 } else { 870 /* 871 * if we have a special address handler, 872 * then use it instead of the generic one. 873 */ 874 if (ifa->ifa_claim_addr) { 875 if ((*ifa->ifa_claim_addr)(ifa, addr)) 876 goto done; 877 continue; 878 } 879 880 /* 881 * Scan all the bits in the ifa's address. 882 * If a bit dissagrees with what we are 883 * looking for, mask it with the netmask 884 * to see if it really matters. 885 * (A byte at a time) 886 */ 887 if (ifa->ifa_netmask == 0) 888 continue; 889 cp = addr_data; 890 cp2 = ifa->ifa_addr->sa_data; 891 cp3 = ifa->ifa_netmask->sa_data; 892 cplim = ifa->ifa_netmask->sa_len 893 + (char *)ifa->ifa_netmask; 894 while (cp3 < cplim) 895 if ((*cp++ ^ *cp2++) & *cp3++) 896 goto next; /* next address! */ 897 /* 898 * If the netmask of what we just found 899 * is more specific than what we had before 900 * (if we had one) then remember the new one 901 * before continuing to search 902 * for an even better one. 903 */ 904 if (ifa_maybe == 0 || 905 rn_refines((caddr_t)ifa->ifa_netmask, 906 (caddr_t)ifa_maybe->ifa_netmask)) 907 ifa_maybe = ifa; 908 } 909 } 910 } 911 ifa = ifa_maybe; 912 done: 913 IFNET_RUNLOCK(); 914 return (ifa); 915 } 916 917 /* 918 * Find an interface address specific to an interface best matching 919 * a given address. 920 */ 921 struct ifaddr * 922 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp) 923 { 924 struct ifaddr *ifa; 925 char *cp, *cp2, *cp3; 926 char *cplim; 927 struct ifaddr *ifa_maybe = 0; 928 u_int af = addr->sa_family; 929 930 if (af >= AF_MAX) 931 return (0); 932 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 933 if (ifa->ifa_addr->sa_family != af) 934 continue; 935 if (ifa_maybe == 0) 936 ifa_maybe = ifa; 937 if (ifa->ifa_netmask == 0) { 938 if (sa_equal(addr, ifa->ifa_addr) || 939 (ifa->ifa_dstaddr && 940 sa_equal(addr, ifa->ifa_dstaddr))) 941 goto done; 942 continue; 943 } 944 if (ifp->if_flags & IFF_POINTOPOINT) { 945 if (sa_equal(addr, ifa->ifa_dstaddr)) 946 goto done; 947 } else { 948 cp = addr->sa_data; 949 cp2 = ifa->ifa_addr->sa_data; 950 cp3 = ifa->ifa_netmask->sa_data; 951 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 952 for (; cp3 < cplim; cp3++) 953 if ((*cp++ ^ *cp2++) & *cp3) 954 break; 955 if (cp3 == cplim) 956 goto done; 957 } 958 } 959 ifa = ifa_maybe; 960 done: 961 return (ifa); 962 } 963 964 #include <net/route.h> 965 966 /* 967 * Default action when installing a route with a Link Level gateway. 968 * Lookup an appropriate real ifa to point to. 969 * This should be moved to /sys/net/link.c eventually. 970 */ 971 static void 972 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 973 { 974 struct ifaddr *ifa, *oifa; 975 struct sockaddr *dst; 976 struct ifnet *ifp; 977 978 RT_LOCK_ASSERT(rt); 979 980 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 981 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 982 return; 983 ifa = ifaof_ifpforaddr(dst, ifp); 984 if (ifa) { 985 IFAREF(ifa); /* XXX */ 986 oifa = rt->rt_ifa; 987 rt->rt_ifa = ifa; 988 IFAFREE(oifa); 989 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 990 ifa->ifa_rtrequest(cmd, rt, info); 991 } 992 } 993 994 /* 995 * Mark an interface down and notify protocols of 996 * the transition. 997 * NOTE: must be called at splnet or eqivalent. 998 */ 999 static void 1000 if_unroute(struct ifnet *ifp, int flag, int fam) 1001 { 1002 struct ifaddr *ifa; 1003 1004 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP")); 1005 1006 ifp->if_flags &= ~flag; 1007 getmicrotime(&ifp->if_lastchange); 1008 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1009 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1010 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 1011 if_qflush(&ifp->if_snd); 1012 #ifdef DEV_CARP 1013 if (ifp->if_carp) 1014 carp_carpdev_state(ifp->if_carp); 1015 #endif 1016 rt_ifmsg(ifp); 1017 } 1018 1019 /* 1020 * Mark an interface up and notify protocols of 1021 * the transition. 1022 * NOTE: must be called at splnet or eqivalent. 1023 */ 1024 static void 1025 if_route(struct ifnet *ifp, int flag, int fam) 1026 { 1027 struct ifaddr *ifa; 1028 1029 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP")); 1030 1031 ifp->if_flags |= flag; 1032 getmicrotime(&ifp->if_lastchange); 1033 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1034 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1035 pfctlinput(PRC_IFUP, ifa->ifa_addr); 1036 #ifdef DEV_CARP 1037 if (ifp->if_carp) 1038 carp_carpdev_state(ifp->if_carp); 1039 #endif 1040 rt_ifmsg(ifp); 1041 #ifdef INET6 1042 in6_if_up(ifp); 1043 #endif 1044 } 1045 1046 void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */ 1047 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */ 1048 1049 /* 1050 * Handle a change in the interface link state. To avoid LORs 1051 * between driver lock and upper layer locks, as well as possible 1052 * recursions, we post event to taskqueue, and all job 1053 * is done in static do_link_state_change(). 1054 */ 1055 void 1056 if_link_state_change(struct ifnet *ifp, int link_state) 1057 { 1058 /* Return if state hasn't changed. */ 1059 if (ifp->if_link_state == link_state) 1060 return; 1061 1062 ifp->if_link_state = link_state; 1063 1064 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask); 1065 } 1066 1067 static void 1068 do_link_state_change(void *arg, int pending) 1069 { 1070 struct ifnet *ifp = (struct ifnet *)arg; 1071 int link_state = ifp->if_link_state; 1072 int link; 1073 1074 /* Notify that the link state has changed. */ 1075 rt_ifmsg(ifp); 1076 if (link_state == LINK_STATE_UP) 1077 link = NOTE_LINKUP; 1078 else if (link_state == LINK_STATE_DOWN) 1079 link = NOTE_LINKDOWN; 1080 else 1081 link = NOTE_LINKINV; 1082 KNOTE_UNLOCKED(&ifp->if_klist, link); 1083 if (ifp->if_vlantrunk != NULL) 1084 (*vlan_link_state_p)(ifp, link); 1085 1086 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) && 1087 IFP2AC(ifp)->ac_netgraph != NULL) 1088 (*ng_ether_link_state_p)(ifp, link_state); 1089 #ifdef DEV_CARP 1090 if (ifp->if_carp) 1091 carp_carpdev_state(ifp->if_carp); 1092 #endif 1093 if (ifp->if_bridge) { 1094 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!")); 1095 (*bstp_linkstate_p)(ifp, link_state); 1096 } 1097 1098 devctl_notify("IFNET", ifp->if_xname, 1099 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL); 1100 if (pending > 1) 1101 if_printf(ifp, "%d link states coalesced\n", pending); 1102 if (log_link_state_change) 1103 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname, 1104 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" ); 1105 } 1106 1107 /* 1108 * Mark an interface down and notify protocols of 1109 * the transition. 1110 * NOTE: must be called at splnet or eqivalent. 1111 */ 1112 void 1113 if_down(struct ifnet *ifp) 1114 { 1115 1116 if_unroute(ifp, IFF_UP, AF_UNSPEC); 1117 } 1118 1119 /* 1120 * Mark an interface up and notify protocols of 1121 * the transition. 1122 * NOTE: must be called at splnet or eqivalent. 1123 */ 1124 void 1125 if_up(struct ifnet *ifp) 1126 { 1127 1128 if_route(ifp, IFF_UP, AF_UNSPEC); 1129 } 1130 1131 /* 1132 * Flush an interface queue. 1133 */ 1134 static void 1135 if_qflush(struct ifaltq *ifq) 1136 { 1137 struct mbuf *m, *n; 1138 1139 IFQ_LOCK(ifq); 1140 #ifdef ALTQ 1141 if (ALTQ_IS_ENABLED(ifq)) 1142 ALTQ_PURGE(ifq); 1143 #endif 1144 n = ifq->ifq_head; 1145 while ((m = n) != 0) { 1146 n = m->m_act; 1147 m_freem(m); 1148 } 1149 ifq->ifq_head = 0; 1150 ifq->ifq_tail = 0; 1151 ifq->ifq_len = 0; 1152 IFQ_UNLOCK(ifq); 1153 } 1154 1155 /* 1156 * Handle interface watchdog timer routines. Called 1157 * from softclock, we decrement timers (if set) and 1158 * call the appropriate interface routine on expiration. 1159 * 1160 * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called 1161 * holding Giant. If we switch to an MPSAFE callout, we likely need to grab 1162 * Giant before entering if_watchdog() on an IFF_NEEDSGIANT interface. 1163 */ 1164 static void 1165 if_slowtimo(void *arg) 1166 { 1167 struct ifnet *ifp; 1168 int s = splimp(); 1169 1170 IFNET_RLOCK(); 1171 TAILQ_FOREACH(ifp, &ifnet, if_link) { 1172 if (ifp->if_timer == 0 || --ifp->if_timer) 1173 continue; 1174 if (ifp->if_watchdog) 1175 (*ifp->if_watchdog)(ifp); 1176 } 1177 IFNET_RUNLOCK(); 1178 splx(s); 1179 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ); 1180 } 1181 1182 /* 1183 * Map interface name to 1184 * interface structure pointer. 1185 */ 1186 struct ifnet * 1187 ifunit(const char *name) 1188 { 1189 struct ifnet *ifp; 1190 1191 IFNET_RLOCK(); 1192 TAILQ_FOREACH(ifp, &ifnet, if_link) { 1193 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0) 1194 break; 1195 } 1196 IFNET_RUNLOCK(); 1197 return (ifp); 1198 } 1199 1200 /* 1201 * Hardware specific interface ioctls. 1202 */ 1203 static int 1204 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td) 1205 { 1206 struct ifreq *ifr; 1207 struct ifstat *ifs; 1208 int error = 0; 1209 int new_flags, temp_flags; 1210 size_t namelen, onamelen; 1211 char new_name[IFNAMSIZ]; 1212 struct ifaddr *ifa; 1213 struct sockaddr_dl *sdl; 1214 1215 ifr = (struct ifreq *)data; 1216 switch (cmd) { 1217 case SIOCGIFINDEX: 1218 ifr->ifr_index = ifp->if_index; 1219 break; 1220 1221 case SIOCGIFFLAGS: 1222 temp_flags = ifp->if_flags | ifp->if_drv_flags; 1223 ifr->ifr_flags = temp_flags & 0xffff; 1224 ifr->ifr_flagshigh = temp_flags >> 16; 1225 break; 1226 1227 case SIOCGIFCAP: 1228 ifr->ifr_reqcap = ifp->if_capabilities; 1229 ifr->ifr_curcap = ifp->if_capenable; 1230 break; 1231 1232 #ifdef MAC 1233 case SIOCGIFMAC: 1234 error = mac_ioctl_ifnet_get(td->td_ucred, ifr, ifp); 1235 break; 1236 #endif 1237 1238 case SIOCGIFMETRIC: 1239 ifr->ifr_metric = ifp->if_metric; 1240 break; 1241 1242 case SIOCGIFMTU: 1243 ifr->ifr_mtu = ifp->if_mtu; 1244 break; 1245 1246 case SIOCGIFPHYS: 1247 ifr->ifr_phys = ifp->if_physical; 1248 break; 1249 1250 case SIOCSIFFLAGS: 1251 error = suser(td); 1252 if (error) 1253 return (error); 1254 /* 1255 * Currently, no driver owned flags pass the IFF_CANTCHANGE 1256 * check, so we don't need special handling here yet. 1257 */ 1258 new_flags = (ifr->ifr_flags & 0xffff) | 1259 (ifr->ifr_flagshigh << 16); 1260 if (ifp->if_flags & IFF_SMART) { 1261 /* Smart drivers twiddle their own routes */ 1262 } else if (ifp->if_flags & IFF_UP && 1263 (new_flags & IFF_UP) == 0) { 1264 int s = splimp(); 1265 if_down(ifp); 1266 splx(s); 1267 } else if (new_flags & IFF_UP && 1268 (ifp->if_flags & IFF_UP) == 0) { 1269 int s = splimp(); 1270 if_up(ifp); 1271 splx(s); 1272 } 1273 /* See if permanently promiscuous mode bit is about to flip */ 1274 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) { 1275 if (new_flags & IFF_PPROMISC) 1276 ifp->if_flags |= IFF_PROMISC; 1277 else if (ifp->if_pcount == 0) 1278 ifp->if_flags &= ~IFF_PROMISC; 1279 log(LOG_INFO, "%s: permanently promiscuous mode %s\n", 1280 ifp->if_xname, 1281 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled"); 1282 } 1283 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 1284 (new_flags &~ IFF_CANTCHANGE); 1285 if (ifp->if_ioctl) { 1286 IFF_LOCKGIANT(ifp); 1287 (void) (*ifp->if_ioctl)(ifp, cmd, data); 1288 IFF_UNLOCKGIANT(ifp); 1289 } 1290 getmicrotime(&ifp->if_lastchange); 1291 break; 1292 1293 case SIOCSIFCAP: 1294 error = suser(td); 1295 if (error) 1296 return (error); 1297 if (ifp->if_ioctl == NULL) 1298 return (EOPNOTSUPP); 1299 if (ifr->ifr_reqcap & ~ifp->if_capabilities) 1300 return (EINVAL); 1301 IFF_LOCKGIANT(ifp); 1302 error = (*ifp->if_ioctl)(ifp, cmd, data); 1303 IFF_UNLOCKGIANT(ifp); 1304 if (error == 0) 1305 getmicrotime(&ifp->if_lastchange); 1306 break; 1307 1308 #ifdef MAC 1309 case SIOCSIFMAC: 1310 error = mac_ioctl_ifnet_set(td->td_ucred, ifr, ifp); 1311 break; 1312 #endif 1313 1314 case SIOCSIFNAME: 1315 error = suser(td); 1316 if (error != 0) 1317 return (error); 1318 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL); 1319 if (error != 0) 1320 return (error); 1321 if (new_name[0] == '\0') 1322 return (EINVAL); 1323 if (ifunit(new_name) != NULL) 1324 return (EEXIST); 1325 1326 /* Announce the departure of the interface. */ 1327 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 1328 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 1329 1330 log(LOG_INFO, "%s: changing name to '%s'\n", 1331 ifp->if_xname, new_name); 1332 1333 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname)); 1334 ifa = ifp->if_addr; 1335 IFA_LOCK(ifa); 1336 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 1337 namelen = strlen(new_name); 1338 onamelen = sdl->sdl_nlen; 1339 /* 1340 * Move the address if needed. This is safe because we 1341 * allocate space for a name of length IFNAMSIZ when we 1342 * create this in if_attach(). 1343 */ 1344 if (namelen != onamelen) { 1345 bcopy(sdl->sdl_data + onamelen, 1346 sdl->sdl_data + namelen, sdl->sdl_alen); 1347 } 1348 bcopy(new_name, sdl->sdl_data, namelen); 1349 sdl->sdl_nlen = namelen; 1350 sdl = (struct sockaddr_dl *)ifa->ifa_netmask; 1351 bzero(sdl->sdl_data, onamelen); 1352 while (namelen != 0) 1353 sdl->sdl_data[--namelen] = 0xff; 1354 IFA_UNLOCK(ifa); 1355 1356 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 1357 /* Announce the return of the interface. */ 1358 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 1359 break; 1360 1361 case SIOCSIFMETRIC: 1362 error = suser(td); 1363 if (error) 1364 return (error); 1365 ifp->if_metric = ifr->ifr_metric; 1366 getmicrotime(&ifp->if_lastchange); 1367 break; 1368 1369 case SIOCSIFPHYS: 1370 error = suser(td); 1371 if (error) 1372 return (error); 1373 if (ifp->if_ioctl == NULL) 1374 return (EOPNOTSUPP); 1375 IFF_LOCKGIANT(ifp); 1376 error = (*ifp->if_ioctl)(ifp, cmd, data); 1377 IFF_UNLOCKGIANT(ifp); 1378 if (error == 0) 1379 getmicrotime(&ifp->if_lastchange); 1380 break; 1381 1382 case SIOCSIFMTU: 1383 { 1384 u_long oldmtu = ifp->if_mtu; 1385 1386 error = suser(td); 1387 if (error) 1388 return (error); 1389 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) 1390 return (EINVAL); 1391 if (ifp->if_ioctl == NULL) 1392 return (EOPNOTSUPP); 1393 IFF_LOCKGIANT(ifp); 1394 error = (*ifp->if_ioctl)(ifp, cmd, data); 1395 IFF_UNLOCKGIANT(ifp); 1396 if (error == 0) { 1397 getmicrotime(&ifp->if_lastchange); 1398 rt_ifmsg(ifp); 1399 } 1400 /* 1401 * If the link MTU changed, do network layer specific procedure. 1402 */ 1403 if (ifp->if_mtu != oldmtu) { 1404 #ifdef INET6 1405 nd6_setmtu(ifp); 1406 #endif 1407 } 1408 break; 1409 } 1410 1411 case SIOCADDMULTI: 1412 case SIOCDELMULTI: 1413 error = suser(td); 1414 if (error) 1415 return (error); 1416 1417 /* Don't allow group membership on non-multicast interfaces. */ 1418 if ((ifp->if_flags & IFF_MULTICAST) == 0) 1419 return (EOPNOTSUPP); 1420 1421 /* Don't let users screw up protocols' entries. */ 1422 if (ifr->ifr_addr.sa_family != AF_LINK) 1423 return (EINVAL); 1424 1425 if (cmd == SIOCADDMULTI) { 1426 struct ifmultiaddr *ifma; 1427 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 1428 } else { 1429 error = if_delmulti(ifp, &ifr->ifr_addr); 1430 } 1431 if (error == 0) 1432 getmicrotime(&ifp->if_lastchange); 1433 break; 1434 1435 case SIOCSIFPHYADDR: 1436 case SIOCDIFPHYADDR: 1437 #ifdef INET6 1438 case SIOCSIFPHYADDR_IN6: 1439 #endif 1440 case SIOCSLIFPHYADDR: 1441 case SIOCSIFMEDIA: 1442 case SIOCSIFGENERIC: 1443 error = suser(td); 1444 if (error) 1445 return (error); 1446 if (ifp->if_ioctl == NULL) 1447 return (EOPNOTSUPP); 1448 IFF_LOCKGIANT(ifp); 1449 error = (*ifp->if_ioctl)(ifp, cmd, data); 1450 IFF_UNLOCKGIANT(ifp); 1451 if (error == 0) 1452 getmicrotime(&ifp->if_lastchange); 1453 break; 1454 1455 case SIOCGIFSTATUS: 1456 ifs = (struct ifstat *)data; 1457 ifs->ascii[0] = '\0'; 1458 1459 case SIOCGIFPSRCADDR: 1460 case SIOCGIFPDSTADDR: 1461 case SIOCGLIFPHYADDR: 1462 case SIOCGIFMEDIA: 1463 case SIOCGIFGENERIC: 1464 if (ifp->if_ioctl == NULL) 1465 return (EOPNOTSUPP); 1466 IFF_LOCKGIANT(ifp); 1467 error = (*ifp->if_ioctl)(ifp, cmd, data); 1468 IFF_UNLOCKGIANT(ifp); 1469 break; 1470 1471 case SIOCSIFLLADDR: 1472 error = suser(td); 1473 if (error) 1474 return (error); 1475 error = if_setlladdr(ifp, 1476 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); 1477 break; 1478 1479 default: 1480 error = ENOIOCTL; 1481 break; 1482 } 1483 return (error); 1484 } 1485 1486 /* 1487 * Interface ioctls. 1488 */ 1489 int 1490 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td) 1491 { 1492 struct ifnet *ifp; 1493 struct ifreq *ifr; 1494 int error; 1495 int oif_flags; 1496 1497 switch (cmd) { 1498 case SIOCGIFCONF: 1499 case OSIOCGIFCONF: 1500 #ifdef __amd64__ 1501 case SIOCGIFCONF32: 1502 #endif 1503 return (ifconf(cmd, data)); 1504 } 1505 ifr = (struct ifreq *)data; 1506 1507 switch (cmd) { 1508 case SIOCIFCREATE: 1509 case SIOCIFDESTROY: 1510 if ((error = suser(td)) != 0) 1511 return (error); 1512 return ((cmd == SIOCIFCREATE) ? 1513 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) : 1514 if_clone_destroy(ifr->ifr_name)); 1515 1516 case SIOCIFGCLONERS: 1517 return (if_clone_list((struct if_clonereq *)data)); 1518 } 1519 1520 ifp = ifunit(ifr->ifr_name); 1521 if (ifp == 0) 1522 return (ENXIO); 1523 1524 error = ifhwioctl(cmd, ifp, data, td); 1525 if (error != ENOIOCTL) 1526 return (error); 1527 1528 oif_flags = ifp->if_flags; 1529 if (so->so_proto == 0) 1530 return (EOPNOTSUPP); 1531 #ifndef COMPAT_43 1532 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, 1533 data, 1534 ifp, td)); 1535 #else 1536 { 1537 int ocmd = cmd; 1538 1539 switch (cmd) { 1540 1541 case SIOCSIFDSTADDR: 1542 case SIOCSIFADDR: 1543 case SIOCSIFBRDADDR: 1544 case SIOCSIFNETMASK: 1545 #if BYTE_ORDER != BIG_ENDIAN 1546 if (ifr->ifr_addr.sa_family == 0 && 1547 ifr->ifr_addr.sa_len < 16) { 1548 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 1549 ifr->ifr_addr.sa_len = 16; 1550 } 1551 #else 1552 if (ifr->ifr_addr.sa_len == 0) 1553 ifr->ifr_addr.sa_len = 16; 1554 #endif 1555 break; 1556 1557 case OSIOCGIFADDR: 1558 cmd = SIOCGIFADDR; 1559 break; 1560 1561 case OSIOCGIFDSTADDR: 1562 cmd = SIOCGIFDSTADDR; 1563 break; 1564 1565 case OSIOCGIFBRDADDR: 1566 cmd = SIOCGIFBRDADDR; 1567 break; 1568 1569 case OSIOCGIFNETMASK: 1570 cmd = SIOCGIFNETMASK; 1571 } 1572 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, 1573 cmd, 1574 data, 1575 ifp, td)); 1576 switch (ocmd) { 1577 1578 case OSIOCGIFADDR: 1579 case OSIOCGIFDSTADDR: 1580 case OSIOCGIFBRDADDR: 1581 case OSIOCGIFNETMASK: 1582 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 1583 1584 } 1585 } 1586 #endif /* COMPAT_43 */ 1587 1588 if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 1589 #ifdef INET6 1590 DELAY(100);/* XXX: temporary workaround for fxp issue*/ 1591 if (ifp->if_flags & IFF_UP) { 1592 int s = splimp(); 1593 in6_if_up(ifp); 1594 splx(s); 1595 } 1596 #endif 1597 } 1598 return (error); 1599 } 1600 1601 /* 1602 * The code common to handling reference counted flags, 1603 * e.g., in ifpromisc() and if_allmulti(). 1604 * The "pflag" argument can specify a permanent mode flag to check, 1605 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none. 1606 * 1607 * Only to be used on stack-owned flags, not driver-owned flags. 1608 */ 1609 static int 1610 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch) 1611 { 1612 struct ifreq ifr; 1613 int error; 1614 int oldflags, oldcount; 1615 1616 /* Sanity checks to catch programming errors */ 1617 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0, 1618 ("%s: setting driver-owned flag %d", __func__, flag)); 1619 1620 if (onswitch) 1621 KASSERT(*refcount >= 0, 1622 ("%s: increment negative refcount %d for flag %d", 1623 __func__, *refcount, flag)); 1624 else 1625 KASSERT(*refcount > 0, 1626 ("%s: decrement non-positive refcount %d for flag %d", 1627 __func__, *refcount, flag)); 1628 1629 /* In case this mode is permanent, just touch refcount */ 1630 if (ifp->if_flags & pflag) { 1631 *refcount += onswitch ? 1 : -1; 1632 return (0); 1633 } 1634 1635 /* Save ifnet parameters for if_ioctl() may fail */ 1636 oldcount = *refcount; 1637 oldflags = ifp->if_flags; 1638 1639 /* 1640 * See if we aren't the only and touching refcount is enough. 1641 * Actually toggle interface flag if we are the first or last. 1642 */ 1643 if (onswitch) { 1644 if ((*refcount)++) 1645 return (0); 1646 ifp->if_flags |= flag; 1647 } else { 1648 if (--(*refcount)) 1649 return (0); 1650 ifp->if_flags &= ~flag; 1651 } 1652 1653 /* Call down the driver since we've changed interface flags */ 1654 if (ifp->if_ioctl == NULL) { 1655 error = EOPNOTSUPP; 1656 goto recover; 1657 } 1658 ifr.ifr_flags = ifp->if_flags & 0xffff; 1659 ifr.ifr_flagshigh = ifp->if_flags >> 16; 1660 IFF_LOCKGIANT(ifp); 1661 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 1662 IFF_UNLOCKGIANT(ifp); 1663 if (error) 1664 goto recover; 1665 /* Notify userland that interface flags have changed */ 1666 rt_ifmsg(ifp); 1667 return (0); 1668 1669 recover: 1670 /* Recover after driver error */ 1671 *refcount = oldcount; 1672 ifp->if_flags = oldflags; 1673 return (error); 1674 } 1675 1676 /* 1677 * Set/clear promiscuous mode on interface ifp based on the truth value 1678 * of pswitch. The calls are reference counted so that only the first 1679 * "on" request actually has an effect, as does the final "off" request. 1680 * Results are undefined if the "off" and "on" requests are not matched. 1681 */ 1682 int 1683 ifpromisc(struct ifnet *ifp, int pswitch) 1684 { 1685 int error; 1686 int oldflags = ifp->if_flags; 1687 1688 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC, 1689 &ifp->if_pcount, pswitch); 1690 /* If promiscuous mode status has changed, log a message */ 1691 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC)) 1692 log(LOG_INFO, "%s: promiscuous mode %s\n", 1693 ifp->if_xname, 1694 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); 1695 return (error); 1696 } 1697 1698 /* 1699 * Return interface configuration 1700 * of system. List may be used 1701 * in later ioctl's (above) to get 1702 * other information. 1703 */ 1704 /*ARGSUSED*/ 1705 static int 1706 ifconf(u_long cmd, caddr_t data) 1707 { 1708 struct ifconf *ifc = (struct ifconf *)data; 1709 #ifdef __amd64__ 1710 struct ifconf32 *ifc32 = (struct ifconf32 *)data; 1711 struct ifconf ifc_swab; 1712 #endif 1713 struct ifnet *ifp; 1714 struct ifaddr *ifa; 1715 struct ifreq ifr; 1716 struct sbuf *sb; 1717 int error, full = 0, valid_len, max_len; 1718 1719 #ifdef __amd64__ 1720 if (cmd == SIOCGIFCONF32) { 1721 ifc_swab.ifc_len = ifc32->ifc_len; 1722 ifc_swab.ifc_buf = (caddr_t)(uintptr_t)ifc32->ifc_buf; 1723 ifc = &ifc_swab; 1724 } 1725 #endif 1726 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */ 1727 max_len = MAXPHYS - 1; 1728 1729 /* Prevent hostile input from being able to crash the system */ 1730 if (ifc->ifc_len <= 0) 1731 return (EINVAL); 1732 1733 again: 1734 if (ifc->ifc_len <= max_len) { 1735 max_len = ifc->ifc_len; 1736 full = 1; 1737 } 1738 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN); 1739 max_len = 0; 1740 valid_len = 0; 1741 1742 IFNET_RLOCK(); /* could sleep XXX */ 1743 TAILQ_FOREACH(ifp, &ifnet, if_link) { 1744 int addrs; 1745 1746 /* 1747 * Zero the ifr_name buffer to make sure we don't 1748 * disclose the contents of the stack. 1749 */ 1750 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name)); 1751 1752 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)) 1753 >= sizeof(ifr.ifr_name)) { 1754 sbuf_delete(sb); 1755 IFNET_RUNLOCK(); 1756 return (ENAMETOOLONG); 1757 } 1758 1759 addrs = 0; 1760 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1761 struct sockaddr *sa = ifa->ifa_addr; 1762 1763 if (jailed(curthread->td_ucred) && 1764 prison_if(curthread->td_ucred, sa)) 1765 continue; 1766 addrs++; 1767 #ifdef COMPAT_43 1768 if (cmd == OSIOCGIFCONF) { 1769 struct osockaddr *osa = 1770 (struct osockaddr *)&ifr.ifr_addr; 1771 ifr.ifr_addr = *sa; 1772 osa->sa_family = sa->sa_family; 1773 sbuf_bcat(sb, &ifr, sizeof(ifr)); 1774 max_len += sizeof(ifr); 1775 } else 1776 #endif 1777 if (sa->sa_len <= sizeof(*sa)) { 1778 ifr.ifr_addr = *sa; 1779 sbuf_bcat(sb, &ifr, sizeof(ifr)); 1780 max_len += sizeof(ifr); 1781 } else { 1782 sbuf_bcat(sb, &ifr, 1783 offsetof(struct ifreq, ifr_addr)); 1784 max_len += offsetof(struct ifreq, ifr_addr); 1785 sbuf_bcat(sb, sa, sa->sa_len); 1786 max_len += sa->sa_len; 1787 } 1788 1789 if (!sbuf_overflowed(sb)) 1790 valid_len = sbuf_len(sb); 1791 } 1792 if (addrs == 0) { 1793 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 1794 sbuf_bcat(sb, &ifr, sizeof(ifr)); 1795 max_len += sizeof(ifr); 1796 1797 if (!sbuf_overflowed(sb)) 1798 valid_len = sbuf_len(sb); 1799 } 1800 } 1801 IFNET_RUNLOCK(); 1802 1803 /* 1804 * If we didn't allocate enough space (uncommon), try again. If 1805 * we have already allocated as much space as we are allowed, 1806 * return what we've got. 1807 */ 1808 if (valid_len != max_len && !full) { 1809 sbuf_delete(sb); 1810 goto again; 1811 } 1812 1813 ifc->ifc_len = valid_len; 1814 #ifdef __amd64__ 1815 if (cmd == SIOCGIFCONF32) 1816 ifc32->ifc_len = valid_len; 1817 #endif 1818 sbuf_finish(sb); 1819 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len); 1820 sbuf_delete(sb); 1821 return (error); 1822 } 1823 1824 /* 1825 * Just like ifpromisc(), but for all-multicast-reception mode. 1826 */ 1827 int 1828 if_allmulti(struct ifnet *ifp, int onswitch) 1829 { 1830 1831 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch)); 1832 } 1833 1834 static struct ifmultiaddr * 1835 if_findmulti(struct ifnet *ifp, struct sockaddr *sa) 1836 { 1837 struct ifmultiaddr *ifma; 1838 1839 IF_ADDR_LOCK_ASSERT(ifp); 1840 1841 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1842 if (sa_equal(ifma->ifma_addr, sa)) 1843 break; 1844 } 1845 1846 return ifma; 1847 } 1848 1849 /* 1850 * Allocate a new ifmultiaddr and initialize based on passed arguments. We 1851 * make copies of passed sockaddrs. The ifmultiaddr will not be added to 1852 * the ifnet multicast address list here, so the caller must do that and 1853 * other setup work (such as notifying the device driver). The reference 1854 * count is initialized to 1. 1855 */ 1856 static struct ifmultiaddr * 1857 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa, 1858 int mflags) 1859 { 1860 struct ifmultiaddr *ifma; 1861 struct sockaddr *dupsa; 1862 1863 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, mflags | 1864 M_ZERO); 1865 if (ifma == NULL) 1866 return (NULL); 1867 1868 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, mflags); 1869 if (dupsa == NULL) { 1870 FREE(ifma, M_IFMADDR); 1871 return (NULL); 1872 } 1873 bcopy(sa, dupsa, sa->sa_len); 1874 ifma->ifma_addr = dupsa; 1875 1876 ifma->ifma_ifp = ifp; 1877 ifma->ifma_refcount = 1; 1878 ifma->ifma_protospec = NULL; 1879 1880 if (llsa == NULL) { 1881 ifma->ifma_lladdr = NULL; 1882 return (ifma); 1883 } 1884 1885 MALLOC(dupsa, struct sockaddr *, llsa->sa_len, M_IFMADDR, mflags); 1886 if (dupsa == NULL) { 1887 FREE(ifma->ifma_addr, M_IFMADDR); 1888 FREE(ifma, M_IFMADDR); 1889 return (NULL); 1890 } 1891 bcopy(llsa, dupsa, llsa->sa_len); 1892 ifma->ifma_lladdr = dupsa; 1893 1894 return (ifma); 1895 } 1896 1897 /* 1898 * if_freemulti: free ifmultiaddr structure and possibly attached related 1899 * addresses. The caller is responsible for implementing reference 1900 * counting, notifying the driver, handling routing messages, and releasing 1901 * any dependent link layer state. 1902 */ 1903 static void 1904 if_freemulti(struct ifmultiaddr *ifma) 1905 { 1906 1907 KASSERT(ifma->ifma_refcount == 1, ("if_freemulti: refcount %d", 1908 ifma->ifma_refcount)); 1909 KASSERT(ifma->ifma_protospec == NULL, 1910 ("if_freemulti: protospec not NULL")); 1911 1912 if (ifma->ifma_lladdr != NULL) 1913 FREE(ifma->ifma_lladdr, M_IFMADDR); 1914 FREE(ifma->ifma_addr, M_IFMADDR); 1915 FREE(ifma, M_IFMADDR); 1916 } 1917 1918 /* 1919 * Register an additional multicast address with a network interface. 1920 * 1921 * - If the address is already present, bump the reference count on the 1922 * address and return. 1923 * - If the address is not link-layer, look up a link layer address. 1924 * - Allocate address structures for one or both addresses, and attach to the 1925 * multicast address list on the interface. If automatically adding a link 1926 * layer address, the protocol address will own a reference to the link 1927 * layer address, to be freed when it is freed. 1928 * - Notify the network device driver of an addition to the multicast address 1929 * list. 1930 * 1931 * 'sa' points to caller-owned memory with the desired multicast address. 1932 * 1933 * 'retifma' will be used to return a pointer to the resulting multicast 1934 * address reference, if desired. 1935 */ 1936 int 1937 if_addmulti(struct ifnet *ifp, struct sockaddr *sa, 1938 struct ifmultiaddr **retifma) 1939 { 1940 struct ifmultiaddr *ifma, *ll_ifma; 1941 struct sockaddr *llsa; 1942 int error; 1943 1944 /* 1945 * If the address is already present, return a new reference to it; 1946 * otherwise, allocate storage and set up a new address. 1947 */ 1948 IF_ADDR_LOCK(ifp); 1949 ifma = if_findmulti(ifp, sa); 1950 if (ifma != NULL) { 1951 ifma->ifma_refcount++; 1952 if (retifma != NULL) 1953 *retifma = ifma; 1954 IF_ADDR_UNLOCK(ifp); 1955 return (0); 1956 } 1957 1958 /* 1959 * The address isn't already present; resolve the protocol address 1960 * into a link layer address, and then look that up, bump its 1961 * refcount or allocate an ifma for that also. If 'llsa' was 1962 * returned, we will need to free it later. 1963 */ 1964 llsa = NULL; 1965 ll_ifma = NULL; 1966 if (ifp->if_resolvemulti != NULL) { 1967 error = ifp->if_resolvemulti(ifp, &llsa, sa); 1968 if (error) 1969 goto unlock_out; 1970 } 1971 1972 /* 1973 * Allocate the new address. Don't hook it up yet, as we may also 1974 * need to allocate a link layer multicast address. 1975 */ 1976 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT); 1977 if (ifma == NULL) { 1978 error = ENOMEM; 1979 goto free_llsa_out; 1980 } 1981 1982 /* 1983 * If a link layer address is found, we'll need to see if it's 1984 * already present in the address list, or allocate is as well. 1985 * When this block finishes, the link layer address will be on the 1986 * list. 1987 */ 1988 if (llsa != NULL) { 1989 ll_ifma = if_findmulti(ifp, llsa); 1990 if (ll_ifma == NULL) { 1991 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT); 1992 if (ll_ifma == NULL) { 1993 if_freemulti(ifma); 1994 error = ENOMEM; 1995 goto free_llsa_out; 1996 } 1997 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma, 1998 ifma_link); 1999 } else 2000 ll_ifma->ifma_refcount++; 2001 } 2002 2003 /* 2004 * We now have a new multicast address, ifma, and possibly a new or 2005 * referenced link layer address. Add the primary address to the 2006 * ifnet address list. 2007 */ 2008 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 2009 2010 if (retifma != NULL) 2011 *retifma = ifma; 2012 2013 /* 2014 * Must generate the message while holding the lock so that 'ifma' 2015 * pointer is still valid. 2016 * 2017 * XXXRW: How come we don't announce ll_ifma? 2018 */ 2019 rt_newmaddrmsg(RTM_NEWMADDR, ifma); 2020 IF_ADDR_UNLOCK(ifp); 2021 2022 /* 2023 * We are certain we have added something, so call down to the 2024 * interface to let them know about it. 2025 */ 2026 if (ifp->if_ioctl != NULL) { 2027 IFF_LOCKGIANT(ifp); 2028 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0); 2029 IFF_UNLOCKGIANT(ifp); 2030 } 2031 2032 if (llsa != NULL) 2033 FREE(llsa, M_IFMADDR); 2034 2035 return (0); 2036 2037 free_llsa_out: 2038 if (llsa != NULL) 2039 FREE(llsa, M_IFMADDR); 2040 2041 unlock_out: 2042 IF_ADDR_UNLOCK(ifp); 2043 return (error); 2044 } 2045 2046 /* 2047 * Remove a reference to a multicast address on this interface. Yell 2048 * if the request does not match an existing membership. 2049 */ 2050 int 2051 if_delmulti(struct ifnet *ifp, struct sockaddr *sa) 2052 { 2053 struct ifmultiaddr *ifma, *ll_ifma; 2054 2055 IF_ADDR_LOCK(ifp); 2056 ifma = if_findmulti(ifp, sa); 2057 if (ifma == NULL) { 2058 IF_ADDR_UNLOCK(ifp); 2059 return ENOENT; 2060 } 2061 2062 if (ifma->ifma_refcount > 1) { 2063 ifma->ifma_refcount--; 2064 IF_ADDR_UNLOCK(ifp); 2065 return 0; 2066 } 2067 2068 sa = ifma->ifma_lladdr; 2069 if (sa != NULL) 2070 ll_ifma = if_findmulti(ifp, sa); 2071 else 2072 ll_ifma = NULL; 2073 2074 /* 2075 * XXXRW: How come we don't announce ll_ifma? 2076 */ 2077 rt_newmaddrmsg(RTM_DELMADDR, ifma); 2078 2079 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 2080 if_freemulti(ifma); 2081 2082 if (ll_ifma != NULL) { 2083 if (ll_ifma->ifma_refcount == 1) { 2084 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifma_link); 2085 if_freemulti(ll_ifma); 2086 } else 2087 ll_ifma->ifma_refcount--; 2088 } 2089 IF_ADDR_UNLOCK(ifp); 2090 2091 /* 2092 * Make sure the interface driver is notified 2093 * in the case of a link layer mcast group being left. 2094 */ 2095 if (ifp->if_ioctl) { 2096 IFF_LOCKGIANT(ifp); 2097 (void) (*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 2098 IFF_UNLOCKGIANT(ifp); 2099 } 2100 2101 return 0; 2102 } 2103 2104 /* 2105 * Set the link layer address on an interface. 2106 * 2107 * At this time we only support certain types of interfaces, 2108 * and we don't allow the length of the address to change. 2109 */ 2110 int 2111 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 2112 { 2113 struct sockaddr_dl *sdl; 2114 struct ifaddr *ifa; 2115 struct ifreq ifr; 2116 2117 ifa = ifp->if_addr; 2118 if (ifa == NULL) 2119 return (EINVAL); 2120 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 2121 if (sdl == NULL) 2122 return (EINVAL); 2123 if (len != sdl->sdl_alen) /* don't allow length to change */ 2124 return (EINVAL); 2125 switch (ifp->if_type) { 2126 case IFT_ETHER: 2127 case IFT_FDDI: 2128 case IFT_XETHER: 2129 case IFT_ISO88025: 2130 case IFT_L2VLAN: 2131 case IFT_BRIDGE: 2132 case IFT_ARCNET: 2133 bcopy(lladdr, LLADDR(sdl), len); 2134 break; 2135 default: 2136 return (ENODEV); 2137 } 2138 /* 2139 * If the interface is already up, we need 2140 * to re-init it in order to reprogram its 2141 * address filter. 2142 */ 2143 if ((ifp->if_flags & IFF_UP) != 0) { 2144 if (ifp->if_ioctl) { 2145 IFF_LOCKGIANT(ifp); 2146 ifp->if_flags &= ~IFF_UP; 2147 ifr.ifr_flags = ifp->if_flags & 0xffff; 2148 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2149 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2150 ifp->if_flags |= IFF_UP; 2151 ifr.ifr_flags = ifp->if_flags & 0xffff; 2152 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2153 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2154 IFF_UNLOCKGIANT(ifp); 2155 } 2156 #ifdef INET 2157 /* 2158 * Also send gratuitous ARPs to notify other nodes about 2159 * the address change. 2160 */ 2161 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2162 if (ifa->ifa_addr != NULL && 2163 ifa->ifa_addr->sa_family == AF_INET) 2164 arp_ifinit(ifp, ifa); 2165 } 2166 #endif 2167 } 2168 return (0); 2169 } 2170 2171 /* 2172 * The name argument must be a pointer to storage which will last as 2173 * long as the interface does. For physical devices, the result of 2174 * device_get_name(dev) is a good choice and for pseudo-devices a 2175 * static string works well. 2176 */ 2177 void 2178 if_initname(struct ifnet *ifp, const char *name, int unit) 2179 { 2180 ifp->if_dname = name; 2181 ifp->if_dunit = unit; 2182 if (unit != IF_DUNIT_NONE) 2183 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit); 2184 else 2185 strlcpy(ifp->if_xname, name, IFNAMSIZ); 2186 } 2187 2188 int 2189 if_printf(struct ifnet *ifp, const char * fmt, ...) 2190 { 2191 va_list ap; 2192 int retval; 2193 2194 retval = printf("%s: ", ifp->if_xname); 2195 va_start(ap, fmt); 2196 retval += vprintf(fmt, ap); 2197 va_end(ap); 2198 return (retval); 2199 } 2200 2201 /* 2202 * When an interface is marked IFF_NEEDSGIANT, its if_start() routine cannot 2203 * be called without Giant. However, we often can't acquire the Giant lock 2204 * at those points; instead, we run it via a task queue that holds Giant via 2205 * if_start_deferred. 2206 * 2207 * XXXRW: We need to make sure that the ifnet isn't fully detached until any 2208 * outstanding if_start_deferred() tasks that will run after the free. This 2209 * probably means waiting in if_detach(). 2210 */ 2211 void 2212 if_start(struct ifnet *ifp) 2213 { 2214 2215 NET_ASSERT_GIANT(); 2216 2217 if ((ifp->if_flags & IFF_NEEDSGIANT) != 0 && debug_mpsafenet != 0) { 2218 if (mtx_owned(&Giant)) 2219 (*(ifp)->if_start)(ifp); 2220 else 2221 taskqueue_enqueue(taskqueue_swi_giant, 2222 &ifp->if_starttask); 2223 } else 2224 (*(ifp)->if_start)(ifp); 2225 } 2226 2227 static void 2228 if_start_deferred(void *context, int pending) 2229 { 2230 struct ifnet *ifp; 2231 2232 /* 2233 * This code must be entered with Giant, and should never run if 2234 * we're not running with debug.mpsafenet. 2235 */ 2236 KASSERT(debug_mpsafenet != 0, ("if_start_deferred: debug.mpsafenet")); 2237 GIANT_REQUIRED; 2238 2239 ifp = context; 2240 (ifp->if_start)(ifp); 2241 } 2242 2243 int 2244 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust) 2245 { 2246 int active = 0; 2247 2248 IF_LOCK(ifq); 2249 if (_IF_QFULL(ifq)) { 2250 _IF_DROP(ifq); 2251 IF_UNLOCK(ifq); 2252 m_freem(m); 2253 return (0); 2254 } 2255 if (ifp != NULL) { 2256 ifp->if_obytes += m->m_pkthdr.len + adjust; 2257 if (m->m_flags & (M_BCAST|M_MCAST)) 2258 ifp->if_omcasts++; 2259 active = ifp->if_drv_flags & IFF_DRV_OACTIVE; 2260 } 2261 _IF_ENQUEUE(ifq, m); 2262 IF_UNLOCK(ifq); 2263 if (ifp != NULL && !active) 2264 if_start(ifp); 2265 return (1); 2266 } 2267 2268 void 2269 if_register_com_alloc(u_char type, 2270 if_com_alloc_t *a, if_com_free_t *f) 2271 { 2272 2273 KASSERT(if_com_alloc[type] == NULL, 2274 ("if_register_com_alloc: %d already registered", type)); 2275 KASSERT(if_com_free[type] == NULL, 2276 ("if_register_com_alloc: %d free already registered", type)); 2277 2278 if_com_alloc[type] = a; 2279 if_com_free[type] = f; 2280 } 2281 2282 void 2283 if_deregister_com_alloc(u_char type) 2284 { 2285 2286 KASSERT(if_com_alloc[type] == NULL, 2287 ("if_deregister_com_alloc: %d not registered", type)); 2288 KASSERT(if_com_free[type] == NULL, 2289 ("if_deregister_com_alloc: %d free not registered", type)); 2290 if_com_alloc[type] = NULL; 2291 if_com_free[type] = NULL; 2292 } 2293