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 /* 627 * Remove routes and flush queues. 628 */ 629 s = splnet(); 630 if_down(ifp); 631 #ifdef ALTQ 632 if (ALTQ_IS_ENABLED(&ifp->if_snd)) 633 altq_disable(&ifp->if_snd); 634 if (ALTQ_IS_ATTACHED(&ifp->if_snd)) 635 altq_detach(&ifp->if_snd); 636 #endif 637 638 if_purgeaddrs(ifp); 639 640 #ifdef INET 641 in_ifdetach(ifp); 642 #endif 643 644 #ifdef INET6 645 /* 646 * Remove all IPv6 kernel structs related to ifp. This should be done 647 * before removing routing entries below, since IPv6 interface direct 648 * routes are expected to be removed by the IPv6-specific kernel API. 649 * Otherwise, the kernel will detect some inconsistency and bark it. 650 */ 651 in6_ifdetach(ifp); 652 #endif 653 /* 654 * Remove link ifaddr pointer and maybe decrement if_index. 655 * Clean up all addresses. 656 */ 657 ifp->if_addr = NULL; 658 destroy_dev(ifdev_byindex(ifp->if_index)); 659 ifdev_byindex(ifp->if_index) = NULL; 660 661 /* We can now free link ifaddr. */ 662 if (!TAILQ_EMPTY(&ifp->if_addrhead)) { 663 ifa = TAILQ_FIRST(&ifp->if_addrhead); 664 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 665 IFAFREE(ifa); 666 } 667 668 /* 669 * Delete all remaining routes using this interface 670 * Unfortuneatly the only way to do this is to slog through 671 * the entire routing table looking for routes which point 672 * to this interface...oh well... 673 */ 674 for (i = 1; i <= AF_MAX; i++) { 675 if ((rnh = rt_tables[i]) == NULL) 676 continue; 677 RADIX_NODE_HEAD_LOCK(rnh); 678 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp); 679 RADIX_NODE_HEAD_UNLOCK(rnh); 680 } 681 682 /* Announce that the interface is gone. */ 683 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 684 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 685 686 IF_AFDATA_LOCK(ifp); 687 for (dp = domains; dp; dp = dp->dom_next) { 688 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) 689 (*dp->dom_ifdetach)(ifp, 690 ifp->if_afdata[dp->dom_family]); 691 } 692 IF_AFDATA_UNLOCK(ifp); 693 694 #ifdef MAC 695 mac_destroy_ifnet(ifp); 696 #endif /* MAC */ 697 KNOTE_UNLOCKED(&ifp->if_klist, NOTE_EXIT); 698 knlist_clear(&ifp->if_klist, 0); 699 knlist_destroy(&ifp->if_klist); 700 IFNET_WLOCK(); 701 found = 0; 702 TAILQ_FOREACH(iter, &ifnet, if_link) 703 if (iter == ifp) { 704 found = 1; 705 break; 706 } 707 if (found) 708 TAILQ_REMOVE(&ifnet, ifp, if_link); 709 IFNET_WUNLOCK(); 710 mtx_destroy(&ifp->if_snd.ifq_mtx); 711 IF_AFDATA_DESTROY(ifp); 712 splx(s); 713 } 714 715 /* 716 * Delete Routes for a Network Interface 717 * 718 * Called for each routing entry via the rnh->rnh_walktree() call above 719 * to delete all route entries referencing a detaching network interface. 720 * 721 * Arguments: 722 * rn pointer to node in the routing table 723 * arg argument passed to rnh->rnh_walktree() - detaching interface 724 * 725 * Returns: 726 * 0 successful 727 * errno failed - reason indicated 728 * 729 */ 730 static int 731 if_rtdel(struct radix_node *rn, void *arg) 732 { 733 struct rtentry *rt = (struct rtentry *)rn; 734 struct ifnet *ifp = arg; 735 int err; 736 737 if (rt->rt_ifp == ifp) { 738 739 /* 740 * Protect (sorta) against walktree recursion problems 741 * with cloned routes 742 */ 743 if ((rt->rt_flags & RTF_UP) == 0) 744 return (0); 745 746 err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, 747 rt_mask(rt), rt->rt_flags, 748 (struct rtentry **) NULL); 749 if (err) { 750 log(LOG_WARNING, "if_rtdel: error %d\n", err); 751 } 752 } 753 754 return (0); 755 } 756 757 #define sa_equal(a1, a2) (bcmp((a1), (a2), ((a1))->sa_len) == 0) 758 759 /* 760 * Locate an interface based on a complete address. 761 */ 762 /*ARGSUSED*/ 763 struct ifaddr * 764 ifa_ifwithaddr(struct sockaddr *addr) 765 { 766 struct ifnet *ifp; 767 struct ifaddr *ifa; 768 769 IFNET_RLOCK(); 770 TAILQ_FOREACH(ifp, &ifnet, if_link) 771 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 772 if (ifa->ifa_addr->sa_family != addr->sa_family) 773 continue; 774 if (sa_equal(addr, ifa->ifa_addr)) 775 goto done; 776 /* IP6 doesn't have broadcast */ 777 if ((ifp->if_flags & IFF_BROADCAST) && 778 ifa->ifa_broadaddr && 779 ifa->ifa_broadaddr->sa_len != 0 && 780 sa_equal(ifa->ifa_broadaddr, addr)) 781 goto done; 782 } 783 ifa = NULL; 784 done: 785 IFNET_RUNLOCK(); 786 return (ifa); 787 } 788 789 /* 790 * Locate the point to point interface with a given destination address. 791 */ 792 /*ARGSUSED*/ 793 struct ifaddr * 794 ifa_ifwithdstaddr(struct sockaddr *addr) 795 { 796 struct ifnet *ifp; 797 struct ifaddr *ifa; 798 799 IFNET_RLOCK(); 800 TAILQ_FOREACH(ifp, &ifnet, if_link) { 801 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 802 continue; 803 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 804 if (ifa->ifa_addr->sa_family != addr->sa_family) 805 continue; 806 if (ifa->ifa_dstaddr && 807 sa_equal(addr, ifa->ifa_dstaddr)) 808 goto done; 809 } 810 } 811 ifa = NULL; 812 done: 813 IFNET_RUNLOCK(); 814 return (ifa); 815 } 816 817 /* 818 * Find an interface on a specific network. If many, choice 819 * is most specific found. 820 */ 821 struct ifaddr * 822 ifa_ifwithnet(struct sockaddr *addr) 823 { 824 struct ifnet *ifp; 825 struct ifaddr *ifa; 826 struct ifaddr *ifa_maybe = (struct ifaddr *) 0; 827 u_int af = addr->sa_family; 828 char *addr_data = addr->sa_data, *cplim; 829 830 /* 831 * AF_LINK addresses can be looked up directly by their index number, 832 * so do that if we can. 833 */ 834 if (af == AF_LINK) { 835 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; 836 if (sdl->sdl_index && sdl->sdl_index <= if_index) 837 return (ifaddr_byindex(sdl->sdl_index)); 838 } 839 840 /* 841 * Scan though each interface, looking for ones that have 842 * addresses in this address family. 843 */ 844 IFNET_RLOCK(); 845 TAILQ_FOREACH(ifp, &ifnet, if_link) { 846 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 847 char *cp, *cp2, *cp3; 848 849 if (ifa->ifa_addr->sa_family != af) 850 next: continue; 851 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) { 852 /* 853 * This is a bit broken as it doesn't 854 * take into account that the remote end may 855 * be a single node in the network we are 856 * looking for. 857 * The trouble is that we don't know the 858 * netmask for the remote end. 859 */ 860 if (ifa->ifa_dstaddr != 0 && 861 sa_equal(addr, ifa->ifa_dstaddr)) 862 goto done; 863 } else { 864 /* 865 * if we have a special address handler, 866 * then use it instead of the generic one. 867 */ 868 if (ifa->ifa_claim_addr) { 869 if ((*ifa->ifa_claim_addr)(ifa, addr)) 870 goto done; 871 continue; 872 } 873 874 /* 875 * Scan all the bits in the ifa's address. 876 * If a bit dissagrees with what we are 877 * looking for, mask it with the netmask 878 * to see if it really matters. 879 * (A byte at a time) 880 */ 881 if (ifa->ifa_netmask == 0) 882 continue; 883 cp = addr_data; 884 cp2 = ifa->ifa_addr->sa_data; 885 cp3 = ifa->ifa_netmask->sa_data; 886 cplim = ifa->ifa_netmask->sa_len 887 + (char *)ifa->ifa_netmask; 888 while (cp3 < cplim) 889 if ((*cp++ ^ *cp2++) & *cp3++) 890 goto next; /* next address! */ 891 /* 892 * If the netmask of what we just found 893 * is more specific than what we had before 894 * (if we had one) then remember the new one 895 * before continuing to search 896 * for an even better one. 897 */ 898 if (ifa_maybe == 0 || 899 rn_refines((caddr_t)ifa->ifa_netmask, 900 (caddr_t)ifa_maybe->ifa_netmask)) 901 ifa_maybe = ifa; 902 } 903 } 904 } 905 ifa = ifa_maybe; 906 done: 907 IFNET_RUNLOCK(); 908 return (ifa); 909 } 910 911 /* 912 * Find an interface address specific to an interface best matching 913 * a given address. 914 */ 915 struct ifaddr * 916 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp) 917 { 918 struct ifaddr *ifa; 919 char *cp, *cp2, *cp3; 920 char *cplim; 921 struct ifaddr *ifa_maybe = 0; 922 u_int af = addr->sa_family; 923 924 if (af >= AF_MAX) 925 return (0); 926 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 927 if (ifa->ifa_addr->sa_family != af) 928 continue; 929 if (ifa_maybe == 0) 930 ifa_maybe = ifa; 931 if (ifa->ifa_netmask == 0) { 932 if (sa_equal(addr, ifa->ifa_addr) || 933 (ifa->ifa_dstaddr && 934 sa_equal(addr, ifa->ifa_dstaddr))) 935 goto done; 936 continue; 937 } 938 if (ifp->if_flags & IFF_POINTOPOINT) { 939 if (sa_equal(addr, ifa->ifa_dstaddr)) 940 goto done; 941 } else { 942 cp = addr->sa_data; 943 cp2 = ifa->ifa_addr->sa_data; 944 cp3 = ifa->ifa_netmask->sa_data; 945 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 946 for (; cp3 < cplim; cp3++) 947 if ((*cp++ ^ *cp2++) & *cp3) 948 break; 949 if (cp3 == cplim) 950 goto done; 951 } 952 } 953 ifa = ifa_maybe; 954 done: 955 return (ifa); 956 } 957 958 #include <net/route.h> 959 960 /* 961 * Default action when installing a route with a Link Level gateway. 962 * Lookup an appropriate real ifa to point to. 963 * This should be moved to /sys/net/link.c eventually. 964 */ 965 static void 966 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 967 { 968 struct ifaddr *ifa, *oifa; 969 struct sockaddr *dst; 970 struct ifnet *ifp; 971 972 RT_LOCK_ASSERT(rt); 973 974 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 975 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 976 return; 977 ifa = ifaof_ifpforaddr(dst, ifp); 978 if (ifa) { 979 IFAREF(ifa); /* XXX */ 980 oifa = rt->rt_ifa; 981 rt->rt_ifa = ifa; 982 IFAFREE(oifa); 983 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 984 ifa->ifa_rtrequest(cmd, rt, info); 985 } 986 } 987 988 /* 989 * Mark an interface down and notify protocols of 990 * the transition. 991 * NOTE: must be called at splnet or eqivalent. 992 */ 993 static void 994 if_unroute(struct ifnet *ifp, int flag, int fam) 995 { 996 struct ifaddr *ifa; 997 998 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP")); 999 1000 ifp->if_flags &= ~flag; 1001 getmicrotime(&ifp->if_lastchange); 1002 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1003 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1004 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 1005 if_qflush(&ifp->if_snd); 1006 #ifdef DEV_CARP 1007 if (ifp->if_carp) 1008 carp_carpdev_state(ifp->if_carp); 1009 #endif 1010 rt_ifmsg(ifp); 1011 } 1012 1013 /* 1014 * Mark an interface up and notify protocols of 1015 * the transition. 1016 * NOTE: must be called at splnet or eqivalent. 1017 */ 1018 static void 1019 if_route(struct ifnet *ifp, int flag, int fam) 1020 { 1021 struct ifaddr *ifa; 1022 1023 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP")); 1024 1025 ifp->if_flags |= flag; 1026 getmicrotime(&ifp->if_lastchange); 1027 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1028 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1029 pfctlinput(PRC_IFUP, ifa->ifa_addr); 1030 #ifdef DEV_CARP 1031 if (ifp->if_carp) 1032 carp_carpdev_state(ifp->if_carp); 1033 #endif 1034 rt_ifmsg(ifp); 1035 #ifdef INET6 1036 in6_if_up(ifp); 1037 #endif 1038 } 1039 1040 void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */ 1041 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */ 1042 1043 /* 1044 * Handle a change in the interface link state. To avoid LORs 1045 * between driver lock and upper layer locks, as well as possible 1046 * recursions, we post event to taskqueue, and all job 1047 * is done in static do_link_state_change(). 1048 */ 1049 void 1050 if_link_state_change(struct ifnet *ifp, int link_state) 1051 { 1052 /* Return if state hasn't changed. */ 1053 if (ifp->if_link_state == link_state) 1054 return; 1055 1056 ifp->if_link_state = link_state; 1057 1058 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask); 1059 } 1060 1061 static void 1062 do_link_state_change(void *arg, int pending) 1063 { 1064 struct ifnet *ifp = (struct ifnet *)arg; 1065 int link_state = ifp->if_link_state; 1066 int link; 1067 1068 /* Notify that the link state has changed. */ 1069 rt_ifmsg(ifp); 1070 if (link_state == LINK_STATE_UP) 1071 link = NOTE_LINKUP; 1072 else if (link_state == LINK_STATE_DOWN) 1073 link = NOTE_LINKDOWN; 1074 else 1075 link = NOTE_LINKINV; 1076 KNOTE_UNLOCKED(&ifp->if_klist, link); 1077 if (ifp->if_vlantrunk != NULL) 1078 (*vlan_link_state_p)(ifp, link); 1079 1080 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) && 1081 IFP2AC(ifp)->ac_netgraph != NULL) 1082 (*ng_ether_link_state_p)(ifp, link_state); 1083 #ifdef DEV_CARP 1084 if (ifp->if_carp) 1085 carp_carpdev_state(ifp->if_carp); 1086 #endif 1087 if (ifp->if_bridge) { 1088 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!")); 1089 (*bstp_linkstate_p)(ifp, link_state); 1090 } 1091 1092 devctl_notify("IFNET", ifp->if_xname, 1093 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL); 1094 if (pending > 1) 1095 if_printf(ifp, "%d link states coalesced\n", pending); 1096 if (log_link_state_change) 1097 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname, 1098 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" ); 1099 } 1100 1101 /* 1102 * Mark an interface down and notify protocols of 1103 * the transition. 1104 * NOTE: must be called at splnet or eqivalent. 1105 */ 1106 void 1107 if_down(struct ifnet *ifp) 1108 { 1109 1110 if_unroute(ifp, IFF_UP, AF_UNSPEC); 1111 } 1112 1113 /* 1114 * Mark an interface up and notify protocols of 1115 * the transition. 1116 * NOTE: must be called at splnet or eqivalent. 1117 */ 1118 void 1119 if_up(struct ifnet *ifp) 1120 { 1121 1122 if_route(ifp, IFF_UP, AF_UNSPEC); 1123 } 1124 1125 /* 1126 * Flush an interface queue. 1127 */ 1128 static void 1129 if_qflush(struct ifaltq *ifq) 1130 { 1131 struct mbuf *m, *n; 1132 1133 IFQ_LOCK(ifq); 1134 #ifdef ALTQ 1135 if (ALTQ_IS_ENABLED(ifq)) 1136 ALTQ_PURGE(ifq); 1137 #endif 1138 n = ifq->ifq_head; 1139 while ((m = n) != 0) { 1140 n = m->m_act; 1141 m_freem(m); 1142 } 1143 ifq->ifq_head = 0; 1144 ifq->ifq_tail = 0; 1145 ifq->ifq_len = 0; 1146 IFQ_UNLOCK(ifq); 1147 } 1148 1149 /* 1150 * Handle interface watchdog timer routines. Called 1151 * from softclock, we decrement timers (if set) and 1152 * call the appropriate interface routine on expiration. 1153 * 1154 * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called 1155 * holding Giant. If we switch to an MPSAFE callout, we likely need to grab 1156 * Giant before entering if_watchdog() on an IFF_NEEDSGIANT interface. 1157 */ 1158 static void 1159 if_slowtimo(void *arg) 1160 { 1161 struct ifnet *ifp; 1162 int s = splimp(); 1163 1164 IFNET_RLOCK(); 1165 TAILQ_FOREACH(ifp, &ifnet, if_link) { 1166 if (ifp->if_timer == 0 || --ifp->if_timer) 1167 continue; 1168 if (ifp->if_watchdog) 1169 (*ifp->if_watchdog)(ifp); 1170 } 1171 IFNET_RUNLOCK(); 1172 splx(s); 1173 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ); 1174 } 1175 1176 /* 1177 * Map interface name to 1178 * interface structure pointer. 1179 */ 1180 struct ifnet * 1181 ifunit(const char *name) 1182 { 1183 struct ifnet *ifp; 1184 1185 IFNET_RLOCK(); 1186 TAILQ_FOREACH(ifp, &ifnet, if_link) { 1187 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0) 1188 break; 1189 } 1190 IFNET_RUNLOCK(); 1191 return (ifp); 1192 } 1193 1194 /* 1195 * Hardware specific interface ioctls. 1196 */ 1197 static int 1198 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td) 1199 { 1200 struct ifreq *ifr; 1201 struct ifstat *ifs; 1202 int error = 0; 1203 int new_flags, temp_flags; 1204 size_t namelen, onamelen; 1205 char new_name[IFNAMSIZ]; 1206 struct ifaddr *ifa; 1207 struct sockaddr_dl *sdl; 1208 1209 ifr = (struct ifreq *)data; 1210 switch (cmd) { 1211 case SIOCGIFINDEX: 1212 ifr->ifr_index = ifp->if_index; 1213 break; 1214 1215 case SIOCGIFFLAGS: 1216 temp_flags = ifp->if_flags | ifp->if_drv_flags; 1217 ifr->ifr_flags = temp_flags & 0xffff; 1218 ifr->ifr_flagshigh = temp_flags >> 16; 1219 break; 1220 1221 case SIOCGIFCAP: 1222 ifr->ifr_reqcap = ifp->if_capabilities; 1223 ifr->ifr_curcap = ifp->if_capenable; 1224 break; 1225 1226 #ifdef MAC 1227 case SIOCGIFMAC: 1228 error = mac_ioctl_ifnet_get(td->td_ucred, ifr, ifp); 1229 break; 1230 #endif 1231 1232 case SIOCGIFMETRIC: 1233 ifr->ifr_metric = ifp->if_metric; 1234 break; 1235 1236 case SIOCGIFMTU: 1237 ifr->ifr_mtu = ifp->if_mtu; 1238 break; 1239 1240 case SIOCGIFPHYS: 1241 ifr->ifr_phys = ifp->if_physical; 1242 break; 1243 1244 case SIOCSIFFLAGS: 1245 error = suser(td); 1246 if (error) 1247 return (error); 1248 /* 1249 * Currently, no driver owned flags pass the IFF_CANTCHANGE 1250 * check, so we don't need special handling here yet. 1251 */ 1252 new_flags = (ifr->ifr_flags & 0xffff) | 1253 (ifr->ifr_flagshigh << 16); 1254 if (ifp->if_flags & IFF_SMART) { 1255 /* Smart drivers twiddle their own routes */ 1256 } else if (ifp->if_flags & IFF_UP && 1257 (new_flags & IFF_UP) == 0) { 1258 int s = splimp(); 1259 if_down(ifp); 1260 splx(s); 1261 } else if (new_flags & IFF_UP && 1262 (ifp->if_flags & IFF_UP) == 0) { 1263 int s = splimp(); 1264 if_up(ifp); 1265 splx(s); 1266 } 1267 /* See if permanently promiscuous mode bit is about to flip */ 1268 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) { 1269 if (new_flags & IFF_PPROMISC) 1270 ifp->if_flags |= IFF_PROMISC; 1271 else if (ifp->if_pcount == 0) 1272 ifp->if_flags &= ~IFF_PROMISC; 1273 log(LOG_INFO, "%s: permanently promiscuous mode %s\n", 1274 ifp->if_xname, 1275 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled"); 1276 } 1277 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 1278 (new_flags &~ IFF_CANTCHANGE); 1279 if (ifp->if_ioctl) { 1280 IFF_LOCKGIANT(ifp); 1281 (void) (*ifp->if_ioctl)(ifp, cmd, data); 1282 IFF_UNLOCKGIANT(ifp); 1283 } 1284 getmicrotime(&ifp->if_lastchange); 1285 break; 1286 1287 case SIOCSIFCAP: 1288 error = suser(td); 1289 if (error) 1290 return (error); 1291 if (ifp->if_ioctl == NULL) 1292 return (EOPNOTSUPP); 1293 if (ifr->ifr_reqcap & ~ifp->if_capabilities) 1294 return (EINVAL); 1295 IFF_LOCKGIANT(ifp); 1296 error = (*ifp->if_ioctl)(ifp, cmd, data); 1297 IFF_UNLOCKGIANT(ifp); 1298 if (error == 0) 1299 getmicrotime(&ifp->if_lastchange); 1300 break; 1301 1302 #ifdef MAC 1303 case SIOCSIFMAC: 1304 error = mac_ioctl_ifnet_set(td->td_ucred, ifr, ifp); 1305 break; 1306 #endif 1307 1308 case SIOCSIFNAME: 1309 error = suser(td); 1310 if (error != 0) 1311 return (error); 1312 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL); 1313 if (error != 0) 1314 return (error); 1315 if (new_name[0] == '\0') 1316 return (EINVAL); 1317 if (ifunit(new_name) != NULL) 1318 return (EEXIST); 1319 1320 /* Announce the departure of the interface. */ 1321 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 1322 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 1323 1324 log(LOG_INFO, "%s: changing name to '%s'\n", 1325 ifp->if_xname, new_name); 1326 1327 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname)); 1328 ifa = ifp->if_addr; 1329 IFA_LOCK(ifa); 1330 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 1331 namelen = strlen(new_name); 1332 onamelen = sdl->sdl_nlen; 1333 /* 1334 * Move the address if needed. This is safe because we 1335 * allocate space for a name of length IFNAMSIZ when we 1336 * create this in if_attach(). 1337 */ 1338 if (namelen != onamelen) { 1339 bcopy(sdl->sdl_data + onamelen, 1340 sdl->sdl_data + namelen, sdl->sdl_alen); 1341 } 1342 bcopy(new_name, sdl->sdl_data, namelen); 1343 sdl->sdl_nlen = namelen; 1344 sdl = (struct sockaddr_dl *)ifa->ifa_netmask; 1345 bzero(sdl->sdl_data, onamelen); 1346 while (namelen != 0) 1347 sdl->sdl_data[--namelen] = 0xff; 1348 IFA_UNLOCK(ifa); 1349 1350 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 1351 /* Announce the return of the interface. */ 1352 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 1353 break; 1354 1355 case SIOCSIFMETRIC: 1356 error = suser(td); 1357 if (error) 1358 return (error); 1359 ifp->if_metric = ifr->ifr_metric; 1360 getmicrotime(&ifp->if_lastchange); 1361 break; 1362 1363 case SIOCSIFPHYS: 1364 error = suser(td); 1365 if (error) 1366 return (error); 1367 if (ifp->if_ioctl == NULL) 1368 return (EOPNOTSUPP); 1369 IFF_LOCKGIANT(ifp); 1370 error = (*ifp->if_ioctl)(ifp, cmd, data); 1371 IFF_UNLOCKGIANT(ifp); 1372 if (error == 0) 1373 getmicrotime(&ifp->if_lastchange); 1374 break; 1375 1376 case SIOCSIFMTU: 1377 { 1378 u_long oldmtu = ifp->if_mtu; 1379 1380 error = suser(td); 1381 if (error) 1382 return (error); 1383 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) 1384 return (EINVAL); 1385 if (ifp->if_ioctl == NULL) 1386 return (EOPNOTSUPP); 1387 IFF_LOCKGIANT(ifp); 1388 error = (*ifp->if_ioctl)(ifp, cmd, data); 1389 IFF_UNLOCKGIANT(ifp); 1390 if (error == 0) { 1391 getmicrotime(&ifp->if_lastchange); 1392 rt_ifmsg(ifp); 1393 } 1394 /* 1395 * If the link MTU changed, do network layer specific procedure. 1396 */ 1397 if (ifp->if_mtu != oldmtu) { 1398 #ifdef INET6 1399 nd6_setmtu(ifp); 1400 #endif 1401 } 1402 break; 1403 } 1404 1405 case SIOCADDMULTI: 1406 case SIOCDELMULTI: 1407 error = suser(td); 1408 if (error) 1409 return (error); 1410 1411 /* Don't allow group membership on non-multicast interfaces. */ 1412 if ((ifp->if_flags & IFF_MULTICAST) == 0) 1413 return (EOPNOTSUPP); 1414 1415 /* Don't let users screw up protocols' entries. */ 1416 if (ifr->ifr_addr.sa_family != AF_LINK) 1417 return (EINVAL); 1418 1419 if (cmd == SIOCADDMULTI) { 1420 struct ifmultiaddr *ifma; 1421 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 1422 } else { 1423 error = if_delmulti(ifp, &ifr->ifr_addr); 1424 } 1425 if (error == 0) 1426 getmicrotime(&ifp->if_lastchange); 1427 break; 1428 1429 case SIOCSIFPHYADDR: 1430 case SIOCDIFPHYADDR: 1431 #ifdef INET6 1432 case SIOCSIFPHYADDR_IN6: 1433 #endif 1434 case SIOCSLIFPHYADDR: 1435 case SIOCSIFMEDIA: 1436 case SIOCSIFGENERIC: 1437 error = suser(td); 1438 if (error) 1439 return (error); 1440 if (ifp->if_ioctl == NULL) 1441 return (EOPNOTSUPP); 1442 IFF_LOCKGIANT(ifp); 1443 error = (*ifp->if_ioctl)(ifp, cmd, data); 1444 IFF_UNLOCKGIANT(ifp); 1445 if (error == 0) 1446 getmicrotime(&ifp->if_lastchange); 1447 break; 1448 1449 case SIOCGIFSTATUS: 1450 ifs = (struct ifstat *)data; 1451 ifs->ascii[0] = '\0'; 1452 1453 case SIOCGIFPSRCADDR: 1454 case SIOCGIFPDSTADDR: 1455 case SIOCGLIFPHYADDR: 1456 case SIOCGIFMEDIA: 1457 case SIOCGIFGENERIC: 1458 if (ifp->if_ioctl == NULL) 1459 return (EOPNOTSUPP); 1460 IFF_LOCKGIANT(ifp); 1461 error = (*ifp->if_ioctl)(ifp, cmd, data); 1462 IFF_UNLOCKGIANT(ifp); 1463 break; 1464 1465 case SIOCSIFLLADDR: 1466 error = suser(td); 1467 if (error) 1468 return (error); 1469 error = if_setlladdr(ifp, 1470 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); 1471 break; 1472 1473 default: 1474 error = ENOIOCTL; 1475 break; 1476 } 1477 return (error); 1478 } 1479 1480 /* 1481 * Interface ioctls. 1482 */ 1483 int 1484 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td) 1485 { 1486 struct ifnet *ifp; 1487 struct ifreq *ifr; 1488 int error; 1489 int oif_flags; 1490 1491 switch (cmd) { 1492 case SIOCGIFCONF: 1493 case OSIOCGIFCONF: 1494 #ifdef __amd64__ 1495 case SIOCGIFCONF32: 1496 #endif 1497 return (ifconf(cmd, data)); 1498 } 1499 ifr = (struct ifreq *)data; 1500 1501 switch (cmd) { 1502 case SIOCIFCREATE: 1503 case SIOCIFDESTROY: 1504 if ((error = suser(td)) != 0) 1505 return (error); 1506 return ((cmd == SIOCIFCREATE) ? 1507 if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) : 1508 if_clone_destroy(ifr->ifr_name)); 1509 1510 case SIOCIFGCLONERS: 1511 return (if_clone_list((struct if_clonereq *)data)); 1512 } 1513 1514 ifp = ifunit(ifr->ifr_name); 1515 if (ifp == 0) 1516 return (ENXIO); 1517 1518 error = ifhwioctl(cmd, ifp, data, td); 1519 if (error != ENOIOCTL) 1520 return (error); 1521 1522 oif_flags = ifp->if_flags; 1523 if (so->so_proto == 0) 1524 return (EOPNOTSUPP); 1525 #ifndef COMPAT_43 1526 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, 1527 data, 1528 ifp, td)); 1529 #else 1530 { 1531 int ocmd = cmd; 1532 1533 switch (cmd) { 1534 1535 case SIOCSIFDSTADDR: 1536 case SIOCSIFADDR: 1537 case SIOCSIFBRDADDR: 1538 case SIOCSIFNETMASK: 1539 #if BYTE_ORDER != BIG_ENDIAN 1540 if (ifr->ifr_addr.sa_family == 0 && 1541 ifr->ifr_addr.sa_len < 16) { 1542 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 1543 ifr->ifr_addr.sa_len = 16; 1544 } 1545 #else 1546 if (ifr->ifr_addr.sa_len == 0) 1547 ifr->ifr_addr.sa_len = 16; 1548 #endif 1549 break; 1550 1551 case OSIOCGIFADDR: 1552 cmd = SIOCGIFADDR; 1553 break; 1554 1555 case OSIOCGIFDSTADDR: 1556 cmd = SIOCGIFDSTADDR; 1557 break; 1558 1559 case OSIOCGIFBRDADDR: 1560 cmd = SIOCGIFBRDADDR; 1561 break; 1562 1563 case OSIOCGIFNETMASK: 1564 cmd = SIOCGIFNETMASK; 1565 } 1566 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, 1567 cmd, 1568 data, 1569 ifp, td)); 1570 switch (ocmd) { 1571 1572 case OSIOCGIFADDR: 1573 case OSIOCGIFDSTADDR: 1574 case OSIOCGIFBRDADDR: 1575 case OSIOCGIFNETMASK: 1576 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 1577 1578 } 1579 } 1580 #endif /* COMPAT_43 */ 1581 1582 if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 1583 #ifdef INET6 1584 DELAY(100);/* XXX: temporary workaround for fxp issue*/ 1585 if (ifp->if_flags & IFF_UP) { 1586 int s = splimp(); 1587 in6_if_up(ifp); 1588 splx(s); 1589 } 1590 #endif 1591 } 1592 return (error); 1593 } 1594 1595 /* 1596 * The code common to handling reference counted flags, 1597 * e.g., in ifpromisc() and if_allmulti(). 1598 * The "pflag" argument can specify a permanent mode flag to check, 1599 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none. 1600 * 1601 * Only to be used on stack-owned flags, not driver-owned flags. 1602 */ 1603 static int 1604 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch) 1605 { 1606 struct ifreq ifr; 1607 int error; 1608 int oldflags, oldcount; 1609 1610 /* Sanity checks to catch programming errors */ 1611 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0, 1612 ("%s: setting driver-owned flag %d", __func__, flag)); 1613 1614 if (onswitch) 1615 KASSERT(*refcount >= 0, 1616 ("%s: increment negative refcount %d for flag %d", 1617 __func__, *refcount, flag)); 1618 else 1619 KASSERT(*refcount > 0, 1620 ("%s: decrement non-positive refcount %d for flag %d", 1621 __func__, *refcount, flag)); 1622 1623 /* In case this mode is permanent, just touch refcount */ 1624 if (ifp->if_flags & pflag) { 1625 *refcount += onswitch ? 1 : -1; 1626 return (0); 1627 } 1628 1629 /* Save ifnet parameters for if_ioctl() may fail */ 1630 oldcount = *refcount; 1631 oldflags = ifp->if_flags; 1632 1633 /* 1634 * See if we aren't the only and touching refcount is enough. 1635 * Actually toggle interface flag if we are the first or last. 1636 */ 1637 if (onswitch) { 1638 if ((*refcount)++) 1639 return (0); 1640 ifp->if_flags |= flag; 1641 } else { 1642 if (--(*refcount)) 1643 return (0); 1644 ifp->if_flags &= ~flag; 1645 } 1646 1647 /* Call down the driver since we've changed interface flags */ 1648 if (ifp->if_ioctl == NULL) { 1649 error = EOPNOTSUPP; 1650 goto recover; 1651 } 1652 ifr.ifr_flags = ifp->if_flags & 0xffff; 1653 ifr.ifr_flagshigh = ifp->if_flags >> 16; 1654 IFF_LOCKGIANT(ifp); 1655 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 1656 IFF_UNLOCKGIANT(ifp); 1657 if (error) 1658 goto recover; 1659 /* Notify userland that interface flags have changed */ 1660 rt_ifmsg(ifp); 1661 return (0); 1662 1663 recover: 1664 /* Recover after driver error */ 1665 *refcount = oldcount; 1666 ifp->if_flags = oldflags; 1667 return (error); 1668 } 1669 1670 /* 1671 * Set/clear promiscuous mode on interface ifp based on the truth value 1672 * of pswitch. The calls are reference counted so that only the first 1673 * "on" request actually has an effect, as does the final "off" request. 1674 * Results are undefined if the "off" and "on" requests are not matched. 1675 */ 1676 int 1677 ifpromisc(struct ifnet *ifp, int pswitch) 1678 { 1679 int error; 1680 int oldflags = ifp->if_flags; 1681 1682 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC, 1683 &ifp->if_pcount, pswitch); 1684 /* If promiscuous mode status has changed, log a message */ 1685 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC)) 1686 log(LOG_INFO, "%s: promiscuous mode %s\n", 1687 ifp->if_xname, 1688 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); 1689 return (error); 1690 } 1691 1692 /* 1693 * Return interface configuration 1694 * of system. List may be used 1695 * in later ioctl's (above) to get 1696 * other information. 1697 */ 1698 /*ARGSUSED*/ 1699 static int 1700 ifconf(u_long cmd, caddr_t data) 1701 { 1702 struct ifconf *ifc = (struct ifconf *)data; 1703 #ifdef __amd64__ 1704 struct ifconf32 *ifc32 = (struct ifconf32 *)data; 1705 struct ifconf ifc_swab; 1706 #endif 1707 struct ifnet *ifp; 1708 struct ifaddr *ifa; 1709 struct ifreq ifr; 1710 struct sbuf *sb; 1711 int error, full = 0, valid_len, max_len; 1712 1713 #ifdef __amd64__ 1714 if (cmd == SIOCGIFCONF32) { 1715 ifc_swab.ifc_len = ifc32->ifc_len; 1716 ifc_swab.ifc_buf = (caddr_t)(uintptr_t)ifc32->ifc_buf; 1717 ifc = &ifc_swab; 1718 } 1719 #endif 1720 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */ 1721 max_len = MAXPHYS - 1; 1722 1723 /* Prevent hostile input from being able to crash the system */ 1724 if (ifc->ifc_len <= 0) 1725 return (EINVAL); 1726 1727 again: 1728 if (ifc->ifc_len <= max_len) { 1729 max_len = ifc->ifc_len; 1730 full = 1; 1731 } 1732 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN); 1733 max_len = 0; 1734 valid_len = 0; 1735 1736 IFNET_RLOCK(); /* could sleep XXX */ 1737 TAILQ_FOREACH(ifp, &ifnet, if_link) { 1738 int addrs; 1739 1740 /* 1741 * Zero the ifr_name buffer to make sure we don't 1742 * disclose the contents of the stack. 1743 */ 1744 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name)); 1745 1746 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)) 1747 >= sizeof(ifr.ifr_name)) { 1748 sbuf_delete(sb); 1749 IFNET_RUNLOCK(); 1750 return (ENAMETOOLONG); 1751 } 1752 1753 addrs = 0; 1754 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1755 struct sockaddr *sa = ifa->ifa_addr; 1756 1757 if (jailed(curthread->td_ucred) && 1758 prison_if(curthread->td_ucred, sa)) 1759 continue; 1760 addrs++; 1761 #ifdef COMPAT_43 1762 if (cmd == OSIOCGIFCONF) { 1763 struct osockaddr *osa = 1764 (struct osockaddr *)&ifr.ifr_addr; 1765 ifr.ifr_addr = *sa; 1766 osa->sa_family = sa->sa_family; 1767 sbuf_bcat(sb, &ifr, sizeof(ifr)); 1768 max_len += sizeof(ifr); 1769 } else 1770 #endif 1771 if (sa->sa_len <= sizeof(*sa)) { 1772 ifr.ifr_addr = *sa; 1773 sbuf_bcat(sb, &ifr, sizeof(ifr)); 1774 max_len += sizeof(ifr); 1775 } else { 1776 sbuf_bcat(sb, &ifr, 1777 offsetof(struct ifreq, ifr_addr)); 1778 max_len += offsetof(struct ifreq, ifr_addr); 1779 sbuf_bcat(sb, sa, sa->sa_len); 1780 max_len += sa->sa_len; 1781 } 1782 1783 if (!sbuf_overflowed(sb)) 1784 valid_len = sbuf_len(sb); 1785 } 1786 if (addrs == 0) { 1787 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 1788 sbuf_bcat(sb, &ifr, sizeof(ifr)); 1789 max_len += sizeof(ifr); 1790 1791 if (!sbuf_overflowed(sb)) 1792 valid_len = sbuf_len(sb); 1793 } 1794 } 1795 IFNET_RUNLOCK(); 1796 1797 /* 1798 * If we didn't allocate enough space (uncommon), try again. If 1799 * we have already allocated as much space as we are allowed, 1800 * return what we've got. 1801 */ 1802 if (valid_len != max_len && !full) { 1803 sbuf_delete(sb); 1804 goto again; 1805 } 1806 1807 ifc->ifc_len = valid_len; 1808 #ifdef __amd64__ 1809 if (cmd == SIOCGIFCONF32) 1810 ifc32->ifc_len = valid_len; 1811 #endif 1812 sbuf_finish(sb); 1813 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len); 1814 sbuf_delete(sb); 1815 return (error); 1816 } 1817 1818 /* 1819 * Just like ifpromisc(), but for all-multicast-reception mode. 1820 */ 1821 int 1822 if_allmulti(struct ifnet *ifp, int onswitch) 1823 { 1824 1825 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch)); 1826 } 1827 1828 static struct ifmultiaddr * 1829 if_findmulti(struct ifnet *ifp, struct sockaddr *sa) 1830 { 1831 struct ifmultiaddr *ifma; 1832 1833 IF_ADDR_LOCK_ASSERT(ifp); 1834 1835 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1836 if (sa_equal(ifma->ifma_addr, sa)) 1837 break; 1838 } 1839 1840 return ifma; 1841 } 1842 1843 /* 1844 * Allocate a new ifmultiaddr and initialize based on passed arguments. We 1845 * make copies of passed sockaddrs. The ifmultiaddr will not be added to 1846 * the ifnet multicast address list here, so the caller must do that and 1847 * other setup work (such as notifying the device driver). The reference 1848 * count is initialized to 1. 1849 */ 1850 static struct ifmultiaddr * 1851 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa, 1852 int mflags) 1853 { 1854 struct ifmultiaddr *ifma; 1855 struct sockaddr *dupsa; 1856 1857 MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, mflags | 1858 M_ZERO); 1859 if (ifma == NULL) 1860 return (NULL); 1861 1862 MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, mflags); 1863 if (dupsa == NULL) { 1864 FREE(ifma, M_IFMADDR); 1865 return (NULL); 1866 } 1867 bcopy(sa, dupsa, sa->sa_len); 1868 ifma->ifma_addr = dupsa; 1869 1870 ifma->ifma_ifp = ifp; 1871 ifma->ifma_refcount = 1; 1872 ifma->ifma_protospec = NULL; 1873 1874 if (llsa == NULL) { 1875 ifma->ifma_lladdr = NULL; 1876 return (ifma); 1877 } 1878 1879 MALLOC(dupsa, struct sockaddr *, llsa->sa_len, M_IFMADDR, mflags); 1880 if (dupsa == NULL) { 1881 FREE(ifma->ifma_addr, M_IFMADDR); 1882 FREE(ifma, M_IFMADDR); 1883 return (NULL); 1884 } 1885 bcopy(llsa, dupsa, llsa->sa_len); 1886 ifma->ifma_lladdr = dupsa; 1887 1888 return (ifma); 1889 } 1890 1891 /* 1892 * if_freemulti: free ifmultiaddr structure and possibly attached related 1893 * addresses. The caller is responsible for implementing reference 1894 * counting, notifying the driver, handling routing messages, and releasing 1895 * any dependent link layer state. 1896 */ 1897 static void 1898 if_freemulti(struct ifmultiaddr *ifma) 1899 { 1900 1901 KASSERT(ifma->ifma_refcount == 1, ("if_freemulti: refcount %d", 1902 ifma->ifma_refcount)); 1903 KASSERT(ifma->ifma_protospec == NULL, 1904 ("if_freemulti: protospec not NULL")); 1905 1906 if (ifma->ifma_lladdr != NULL) 1907 FREE(ifma->ifma_lladdr, M_IFMADDR); 1908 FREE(ifma->ifma_addr, M_IFMADDR); 1909 FREE(ifma, M_IFMADDR); 1910 } 1911 1912 /* 1913 * Register an additional multicast address with a network interface. 1914 * 1915 * - If the address is already present, bump the reference count on the 1916 * address and return. 1917 * - If the address is not link-layer, look up a link layer address. 1918 * - Allocate address structures for one or both addresses, and attach to the 1919 * multicast address list on the interface. If automatically adding a link 1920 * layer address, the protocol address will own a reference to the link 1921 * layer address, to be freed when it is freed. 1922 * - Notify the network device driver of an addition to the multicast address 1923 * list. 1924 * 1925 * 'sa' points to caller-owned memory with the desired multicast address. 1926 * 1927 * 'retifma' will be used to return a pointer to the resulting multicast 1928 * address reference, if desired. 1929 */ 1930 int 1931 if_addmulti(struct ifnet *ifp, struct sockaddr *sa, 1932 struct ifmultiaddr **retifma) 1933 { 1934 struct ifmultiaddr *ifma, *ll_ifma; 1935 struct sockaddr *llsa; 1936 int error; 1937 1938 /* 1939 * If the address is already present, return a new reference to it; 1940 * otherwise, allocate storage and set up a new address. 1941 */ 1942 IF_ADDR_LOCK(ifp); 1943 ifma = if_findmulti(ifp, sa); 1944 if (ifma != NULL) { 1945 ifma->ifma_refcount++; 1946 if (retifma != NULL) 1947 *retifma = ifma; 1948 IF_ADDR_UNLOCK(ifp); 1949 return (0); 1950 } 1951 1952 /* 1953 * The address isn't already present; resolve the protocol address 1954 * into a link layer address, and then look that up, bump its 1955 * refcount or allocate an ifma for that also. If 'llsa' was 1956 * returned, we will need to free it later. 1957 */ 1958 llsa = NULL; 1959 ll_ifma = NULL; 1960 if (ifp->if_resolvemulti != NULL) { 1961 error = ifp->if_resolvemulti(ifp, &llsa, sa); 1962 if (error) 1963 goto unlock_out; 1964 } 1965 1966 /* 1967 * Allocate the new address. Don't hook it up yet, as we may also 1968 * need to allocate a link layer multicast address. 1969 */ 1970 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT); 1971 if (ifma == NULL) { 1972 error = ENOMEM; 1973 goto free_llsa_out; 1974 } 1975 1976 /* 1977 * If a link layer address is found, we'll need to see if it's 1978 * already present in the address list, or allocate is as well. 1979 * When this block finishes, the link layer address will be on the 1980 * list. 1981 */ 1982 if (llsa != NULL) { 1983 ll_ifma = if_findmulti(ifp, llsa); 1984 if (ll_ifma == NULL) { 1985 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT); 1986 if (ll_ifma == NULL) { 1987 if_freemulti(ifma); 1988 error = ENOMEM; 1989 goto free_llsa_out; 1990 } 1991 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma, 1992 ifma_link); 1993 } else 1994 ll_ifma->ifma_refcount++; 1995 } 1996 1997 /* 1998 * We now have a new multicast address, ifma, and possibly a new or 1999 * referenced link layer address. Add the primary address to the 2000 * ifnet address list. 2001 */ 2002 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 2003 2004 if (retifma != NULL) 2005 *retifma = ifma; 2006 2007 /* 2008 * Must generate the message while holding the lock so that 'ifma' 2009 * pointer is still valid. 2010 * 2011 * XXXRW: How come we don't announce ll_ifma? 2012 */ 2013 rt_newmaddrmsg(RTM_NEWMADDR, ifma); 2014 IF_ADDR_UNLOCK(ifp); 2015 2016 /* 2017 * We are certain we have added something, so call down to the 2018 * interface to let them know about it. 2019 */ 2020 if (ifp->if_ioctl != NULL) { 2021 IFF_LOCKGIANT(ifp); 2022 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0); 2023 IFF_UNLOCKGIANT(ifp); 2024 } 2025 2026 if (llsa != NULL) 2027 FREE(llsa, M_IFMADDR); 2028 2029 return (0); 2030 2031 free_llsa_out: 2032 if (llsa != NULL) 2033 FREE(llsa, M_IFMADDR); 2034 2035 unlock_out: 2036 IF_ADDR_UNLOCK(ifp); 2037 return (error); 2038 } 2039 2040 /* 2041 * Remove a reference to a multicast address on this interface. Yell 2042 * if the request does not match an existing membership. 2043 */ 2044 int 2045 if_delmulti(struct ifnet *ifp, struct sockaddr *sa) 2046 { 2047 struct ifmultiaddr *ifma, *ll_ifma; 2048 2049 IF_ADDR_LOCK(ifp); 2050 ifma = if_findmulti(ifp, sa); 2051 if (ifma == NULL) { 2052 IF_ADDR_UNLOCK(ifp); 2053 return ENOENT; 2054 } 2055 2056 if (ifma->ifma_refcount > 1) { 2057 ifma->ifma_refcount--; 2058 IF_ADDR_UNLOCK(ifp); 2059 return 0; 2060 } 2061 2062 sa = ifma->ifma_lladdr; 2063 if (sa != NULL) 2064 ll_ifma = if_findmulti(ifp, sa); 2065 else 2066 ll_ifma = NULL; 2067 2068 /* 2069 * XXXRW: How come we don't announce ll_ifma? 2070 */ 2071 rt_newmaddrmsg(RTM_DELMADDR, ifma); 2072 2073 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 2074 if_freemulti(ifma); 2075 2076 if (ll_ifma != NULL) { 2077 if (ll_ifma->ifma_refcount == 1) { 2078 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifma_link); 2079 if_freemulti(ll_ifma); 2080 } else 2081 ll_ifma->ifma_refcount--; 2082 } 2083 IF_ADDR_UNLOCK(ifp); 2084 2085 /* 2086 * Make sure the interface driver is notified 2087 * in the case of a link layer mcast group being left. 2088 */ 2089 if (ifp->if_ioctl) { 2090 IFF_LOCKGIANT(ifp); 2091 (void) (*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 2092 IFF_UNLOCKGIANT(ifp); 2093 } 2094 2095 return 0; 2096 } 2097 2098 /* 2099 * Set the link layer address on an interface. 2100 * 2101 * At this time we only support certain types of interfaces, 2102 * and we don't allow the length of the address to change. 2103 */ 2104 int 2105 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 2106 { 2107 struct sockaddr_dl *sdl; 2108 struct ifaddr *ifa; 2109 struct ifreq ifr; 2110 2111 ifa = ifp->if_addr; 2112 if (ifa == NULL) 2113 return (EINVAL); 2114 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 2115 if (sdl == NULL) 2116 return (EINVAL); 2117 if (len != sdl->sdl_alen) /* don't allow length to change */ 2118 return (EINVAL); 2119 switch (ifp->if_type) { 2120 case IFT_ETHER: 2121 case IFT_FDDI: 2122 case IFT_XETHER: 2123 case IFT_ISO88025: 2124 case IFT_L2VLAN: 2125 case IFT_BRIDGE: 2126 case IFT_ARCNET: 2127 bcopy(lladdr, LLADDR(sdl), len); 2128 break; 2129 default: 2130 return (ENODEV); 2131 } 2132 /* 2133 * If the interface is already up, we need 2134 * to re-init it in order to reprogram its 2135 * address filter. 2136 */ 2137 if ((ifp->if_flags & IFF_UP) != 0) { 2138 if (ifp->if_ioctl) { 2139 IFF_LOCKGIANT(ifp); 2140 ifp->if_flags &= ~IFF_UP; 2141 ifr.ifr_flags = ifp->if_flags & 0xffff; 2142 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2143 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2144 ifp->if_flags |= IFF_UP; 2145 ifr.ifr_flags = ifp->if_flags & 0xffff; 2146 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2147 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2148 IFF_UNLOCKGIANT(ifp); 2149 } 2150 #ifdef INET 2151 /* 2152 * Also send gratuitous ARPs to notify other nodes about 2153 * the address change. 2154 */ 2155 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2156 if (ifa->ifa_addr != NULL && 2157 ifa->ifa_addr->sa_family == AF_INET) 2158 arp_ifinit(ifp, ifa); 2159 } 2160 #endif 2161 } 2162 return (0); 2163 } 2164 2165 /* 2166 * The name argument must be a pointer to storage which will last as 2167 * long as the interface does. For physical devices, the result of 2168 * device_get_name(dev) is a good choice and for pseudo-devices a 2169 * static string works well. 2170 */ 2171 void 2172 if_initname(struct ifnet *ifp, const char *name, int unit) 2173 { 2174 ifp->if_dname = name; 2175 ifp->if_dunit = unit; 2176 if (unit != IF_DUNIT_NONE) 2177 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit); 2178 else 2179 strlcpy(ifp->if_xname, name, IFNAMSIZ); 2180 } 2181 2182 int 2183 if_printf(struct ifnet *ifp, const char * fmt, ...) 2184 { 2185 va_list ap; 2186 int retval; 2187 2188 retval = printf("%s: ", ifp->if_xname); 2189 va_start(ap, fmt); 2190 retval += vprintf(fmt, ap); 2191 va_end(ap); 2192 return (retval); 2193 } 2194 2195 /* 2196 * When an interface is marked IFF_NEEDSGIANT, its if_start() routine cannot 2197 * be called without Giant. However, we often can't acquire the Giant lock 2198 * at those points; instead, we run it via a task queue that holds Giant via 2199 * if_start_deferred. 2200 * 2201 * XXXRW: We need to make sure that the ifnet isn't fully detached until any 2202 * outstanding if_start_deferred() tasks that will run after the free. This 2203 * probably means waiting in if_detach(). 2204 */ 2205 void 2206 if_start(struct ifnet *ifp) 2207 { 2208 2209 NET_ASSERT_GIANT(); 2210 2211 if ((ifp->if_flags & IFF_NEEDSGIANT) != 0 && debug_mpsafenet != 0) { 2212 if (mtx_owned(&Giant)) 2213 (*(ifp)->if_start)(ifp); 2214 else 2215 taskqueue_enqueue(taskqueue_swi_giant, 2216 &ifp->if_starttask); 2217 } else 2218 (*(ifp)->if_start)(ifp); 2219 } 2220 2221 static void 2222 if_start_deferred(void *context, int pending) 2223 { 2224 struct ifnet *ifp; 2225 2226 /* 2227 * This code must be entered with Giant, and should never run if 2228 * we're not running with debug.mpsafenet. 2229 */ 2230 KASSERT(debug_mpsafenet != 0, ("if_start_deferred: debug.mpsafenet")); 2231 GIANT_REQUIRED; 2232 2233 ifp = context; 2234 (ifp->if_start)(ifp); 2235 } 2236 2237 int 2238 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust) 2239 { 2240 int active = 0; 2241 2242 IF_LOCK(ifq); 2243 if (_IF_QFULL(ifq)) { 2244 _IF_DROP(ifq); 2245 IF_UNLOCK(ifq); 2246 m_freem(m); 2247 return (0); 2248 } 2249 if (ifp != NULL) { 2250 ifp->if_obytes += m->m_pkthdr.len + adjust; 2251 if (m->m_flags & (M_BCAST|M_MCAST)) 2252 ifp->if_omcasts++; 2253 active = ifp->if_drv_flags & IFF_DRV_OACTIVE; 2254 } 2255 _IF_ENQUEUE(ifq, m); 2256 IF_UNLOCK(ifq); 2257 if (ifp != NULL && !active) 2258 if_start(ifp); 2259 return (1); 2260 } 2261 2262 void 2263 if_register_com_alloc(u_char type, 2264 if_com_alloc_t *a, if_com_free_t *f) 2265 { 2266 2267 KASSERT(if_com_alloc[type] == NULL, 2268 ("if_register_com_alloc: %d already registered", type)); 2269 KASSERT(if_com_free[type] == NULL, 2270 ("if_register_com_alloc: %d free already registered", type)); 2271 2272 if_com_alloc[type] = a; 2273 if_com_free[type] = f; 2274 } 2275 2276 void 2277 if_deregister_com_alloc(u_char type) 2278 { 2279 2280 KASSERT(if_com_alloc[type] == NULL, 2281 ("if_deregister_com_alloc: %d not registered", type)); 2282 KASSERT(if_com_free[type] == NULL, 2283 ("if_deregister_com_alloc: %d free not registered", type)); 2284 if_com_alloc[type] = NULL; 2285 if_com_free[type] = NULL; 2286 } 2287