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/malloc.h> 43 #include <sys/sbuf.h> 44 #include <sys/bus.h> 45 #include <sys/mbuf.h> 46 #include <sys/systm.h> 47 #include <sys/priv.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/lock.h> 54 #include <sys/rwlock.h> 55 #include <sys/sockio.h> 56 #include <sys/syslog.h> 57 #include <sys/sysctl.h> 58 #include <sys/taskqueue.h> 59 #include <sys/domain.h> 60 #include <sys/jail.h> 61 #include <sys/vimage.h> 62 #include <machine/stdarg.h> 63 #include <vm/uma.h> 64 65 #include <net/if.h> 66 #include <net/if_arp.h> 67 #include <net/if_clone.h> 68 #include <net/if_dl.h> 69 #include <net/if_types.h> 70 #include <net/if_var.h> 71 #include <net/radix.h> 72 #include <net/route.h> 73 #include <net/vnet.h> 74 75 #if defined(INET) || defined(INET6) 76 /*XXX*/ 77 #include <netinet/in.h> 78 #include <netinet/in_var.h> 79 #ifdef INET6 80 #include <netinet6/in6_var.h> 81 #include <netinet6/in6_ifattach.h> 82 #endif 83 #endif 84 #ifdef INET 85 #include <netinet/if_ether.h> 86 #include <netinet/vinet.h> 87 #endif 88 #ifdef DEV_CARP 89 #include <netinet/ip_carp.h> 90 #endif 91 92 #include <security/mac/mac_framework.h> 93 94 #ifndef VIMAGE 95 #ifndef VIMAGE_GLOBALS 96 struct vnet_net vnet_net_0; 97 #endif 98 #endif 99 100 static int slowtimo_started; 101 102 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 103 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); 104 105 /* Log link state change events */ 106 static int log_link_state_change = 1; 107 108 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW, 109 &log_link_state_change, 0, 110 "log interface link state change events"); 111 112 void (*bstp_linkstate_p)(struct ifnet *ifp, int state); 113 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state); 114 void (*lagg_linkstate_p)(struct ifnet *ifp, int state); 115 116 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL; 117 118 /* 119 * XXX: Style; these should be sorted alphabetically, and unprototyped 120 * static functions should be prototyped. Currently they are sorted by 121 * declaration order. 122 */ 123 static void if_attachdomain(void *); 124 static void if_attachdomain1(struct ifnet *); 125 static int ifconf(u_long, caddr_t); 126 static void if_freemulti(struct ifmultiaddr *); 127 static void if_grow(void); 128 static void if_init(void *); 129 static void if_check(void *); 130 static void if_qflush(struct ifnet *); 131 static void if_route(struct ifnet *, int flag, int fam); 132 static int if_setflag(struct ifnet *, int, int, int *, int); 133 static void if_slowtimo(void *); 134 static int if_transmit(struct ifnet *ifp, struct mbuf *m); 135 static void if_unroute(struct ifnet *, int flag, int fam); 136 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 137 static int if_rtdel(struct radix_node *, void *); 138 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *); 139 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int); 140 static void if_start_deferred(void *context, int pending); 141 static void do_link_state_change(void *, int); 142 static int if_getgroup(struct ifgroupreq *, struct ifnet *); 143 static int if_getgroupmembers(struct ifgroupreq *); 144 145 #ifdef INET6 146 /* 147 * XXX: declare here to avoid to include many inet6 related files.. 148 * should be more generalized? 149 */ 150 extern void nd6_setmtu(struct ifnet *); 151 #endif 152 153 #ifdef VIMAGE_GLOBALS 154 struct ifnethead ifnet; /* depend on static init XXX */ 155 struct ifgrouphead ifg_head; 156 int if_index; 157 static int if_indexlim; 158 /* Table of ifnet/cdev by index. Locked with ifnet_lock. */ 159 static struct ifindex_entry *ifindex_table; 160 static struct knlist ifklist; 161 #endif 162 163 int ifqmaxlen = IFQ_MAXLEN; 164 struct rwlock ifnet_lock; 165 static if_com_alloc_t *if_com_alloc[256]; 166 static if_com_free_t *if_com_free[256]; 167 168 static void filt_netdetach(struct knote *kn); 169 static int filt_netdev(struct knote *kn, long hint); 170 171 static struct filterops netdev_filtops = 172 { 1, NULL, filt_netdetach, filt_netdev }; 173 174 #ifndef VIMAGE_GLOBALS 175 static struct vnet_symmap vnet_net_symmap[] = { 176 VNET_SYMMAP(net, ifnet), 177 VNET_SYMMAP(net, rt_tables), 178 VNET_SYMMAP(net, rtstat), 179 VNET_SYMMAP(net, rttrash), 180 VNET_SYMMAP_END 181 }; 182 183 VNET_MOD_DECLARE(NET, net, vnet_net_iattach, vnet_net_idetach, 184 NONE, vnet_net_symmap) 185 #endif 186 187 /* 188 * System initialization 189 */ 190 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL); 191 SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL); 192 193 MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals"); 194 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 195 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 196 197 static struct ifnet * 198 ifnet_byindex_locked(u_short idx) 199 { 200 INIT_VNET_NET(curvnet); 201 struct ifnet *ifp; 202 203 ifp = V_ifindex_table[idx].ife_ifnet; 204 return (ifp); 205 } 206 207 struct ifnet * 208 ifnet_byindex(u_short idx) 209 { 210 struct ifnet *ifp; 211 212 IFNET_RLOCK(); 213 ifp = ifnet_byindex_locked(idx); 214 IFNET_RUNLOCK(); 215 return (ifp); 216 } 217 218 static void 219 ifnet_setbyindex(u_short idx, struct ifnet *ifp) 220 { 221 INIT_VNET_NET(curvnet); 222 223 IFNET_WLOCK_ASSERT(); 224 225 V_ifindex_table[idx].ife_ifnet = ifp; 226 } 227 228 struct ifaddr * 229 ifaddr_byindex(u_short idx) 230 { 231 struct ifaddr *ifa; 232 233 IFNET_RLOCK(); 234 ifa = ifnet_byindex_locked(idx)->if_addr; 235 IFNET_RUNLOCK(); 236 return (ifa); 237 } 238 239 struct cdev * 240 ifdev_byindex(u_short idx) 241 { 242 INIT_VNET_NET(curvnet); 243 struct cdev *cdev; 244 245 IFNET_RLOCK(); 246 cdev = V_ifindex_table[idx].ife_dev; 247 IFNET_RUNLOCK(); 248 return (cdev); 249 } 250 251 static void 252 ifdev_setbyindex(u_short idx, struct cdev *cdev) 253 { 254 INIT_VNET_NET(curvnet); 255 256 IFNET_WLOCK(); 257 V_ifindex_table[idx].ife_dev = cdev; 258 IFNET_WUNLOCK(); 259 } 260 261 static d_open_t netopen; 262 static d_close_t netclose; 263 static d_ioctl_t netioctl; 264 static d_kqfilter_t netkqfilter; 265 266 static struct cdevsw net_cdevsw = { 267 .d_version = D_VERSION, 268 .d_flags = D_NEEDGIANT, 269 .d_open = netopen, 270 .d_close = netclose, 271 .d_ioctl = netioctl, 272 .d_name = "net", 273 .d_kqfilter = netkqfilter, 274 }; 275 276 static int 277 netopen(struct cdev *dev, int flag, int mode, struct thread *td) 278 { 279 return (0); 280 } 281 282 static int 283 netclose(struct cdev *dev, int flags, int fmt, struct thread *td) 284 { 285 return (0); 286 } 287 288 static int 289 netioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 290 { 291 struct ifnet *ifp; 292 int error, idx; 293 294 /* only support interface specific ioctls */ 295 if (IOCGROUP(cmd) != 'i') 296 return (EOPNOTSUPP); 297 idx = dev2unit(dev); 298 if (idx == 0) { 299 /* 300 * special network device, not interface. 301 */ 302 if (cmd == SIOCGIFCONF) 303 return (ifconf(cmd, data)); /* XXX remove cmd */ 304 #ifdef __amd64__ 305 if (cmd == SIOCGIFCONF32) 306 return (ifconf(cmd, data)); /* XXX remove cmd */ 307 #endif 308 return (EOPNOTSUPP); 309 } 310 311 ifp = ifnet_byindex(idx); 312 if (ifp == NULL) 313 return (ENXIO); 314 315 error = ifhwioctl(cmd, ifp, data, td); 316 if (error == ENOIOCTL) 317 error = EOPNOTSUPP; 318 return (error); 319 } 320 321 static int 322 netkqfilter(struct cdev *dev, struct knote *kn) 323 { 324 INIT_VNET_NET(curvnet); 325 struct knlist *klist; 326 struct ifnet *ifp; 327 int idx; 328 329 switch (kn->kn_filter) { 330 case EVFILT_NETDEV: 331 kn->kn_fop = &netdev_filtops; 332 break; 333 default: 334 return (EINVAL); 335 } 336 337 idx = dev2unit(dev); 338 if (idx == 0) { 339 klist = &V_ifklist; 340 } else { 341 ifp = ifnet_byindex(idx); 342 if (ifp == NULL) 343 return (1); 344 klist = &ifp->if_klist; 345 } 346 347 kn->kn_hook = (caddr_t)klist; 348 349 knlist_add(klist, kn, 0); 350 351 return (0); 352 } 353 354 static void 355 filt_netdetach(struct knote *kn) 356 { 357 struct knlist *klist = (struct knlist *)kn->kn_hook; 358 359 knlist_remove(klist, kn, 0); 360 } 361 362 static int 363 filt_netdev(struct knote *kn, long hint) 364 { 365 struct knlist *klist = (struct knlist *)kn->kn_hook; 366 367 /* 368 * Currently NOTE_EXIT is abused to indicate device detach. 369 */ 370 if (hint == NOTE_EXIT) { 371 kn->kn_data = NOTE_LINKINV; 372 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 373 knlist_remove_inevent(klist, kn); 374 return (1); 375 } 376 if (hint != 0) 377 kn->kn_data = hint; /* current status */ 378 if (kn->kn_sfflags & hint) 379 kn->kn_fflags |= hint; 380 return (kn->kn_fflags != 0); 381 } 382 383 /* 384 * Network interface utility routines. 385 * 386 * Routines with ifa_ifwith* names take sockaddr *'s as 387 * parameters. 388 */ 389 390 /* ARGSUSED*/ 391 static void 392 if_init(void *dummy __unused) 393 { 394 INIT_VNET_NET(curvnet); 395 396 #ifndef VIMAGE_GLOBALS 397 vnet_mod_register(&vnet_net_modinfo); 398 #endif 399 400 V_if_index = 0; 401 V_ifindex_table = NULL; 402 V_if_indexlim = 8; 403 404 IFNET_LOCK_INIT(); 405 TAILQ_INIT(&V_ifnet); 406 TAILQ_INIT(&V_ifg_head); 407 knlist_init(&V_ifklist, NULL, NULL, NULL, NULL); 408 if_grow(); /* create initial table */ 409 ifdev_setbyindex(0, make_dev(&net_cdevsw, 0, UID_ROOT, GID_WHEEL, 410 0600, "network")); 411 if_clone_init(); 412 } 413 414 static void 415 if_grow(void) 416 { 417 INIT_VNET_NET(curvnet); 418 u_int n; 419 struct ifindex_entry *e; 420 421 V_if_indexlim <<= 1; 422 n = V_if_indexlim * sizeof(*e); 423 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO); 424 if (V_ifindex_table != NULL) { 425 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2); 426 free((caddr_t)V_ifindex_table, M_IFNET); 427 } 428 V_ifindex_table = e; 429 } 430 431 static void 432 if_check(void *dummy __unused) 433 { 434 435 /* 436 * If at least one interface added during boot uses 437 * if_watchdog then start the timer. 438 */ 439 if (slowtimo_started) 440 if_slowtimo(0); 441 } 442 443 /* 444 * Allocate a struct ifnet and an index for an interface. A layer 2 445 * common structure will also be allocated if an allocation routine is 446 * registered for the passed type. 447 */ 448 struct ifnet* 449 if_alloc(u_char type) 450 { 451 INIT_VNET_NET(curvnet); 452 struct ifnet *ifp; 453 454 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO); 455 456 /* 457 * Try to find an empty slot below if_index. If we fail, take 458 * the next slot. 459 * 460 * XXX: should be locked! 461 */ 462 for (ifp->if_index = 1; ifp->if_index <= V_if_index; ifp->if_index++) { 463 if (ifnet_byindex(ifp->if_index) == NULL) 464 break; 465 } 466 /* Catch if_index overflow. */ 467 if (ifp->if_index < 1) { 468 free(ifp, M_IFNET); 469 return (NULL); 470 } 471 if (ifp->if_index > V_if_index) 472 V_if_index = ifp->if_index; 473 if (V_if_index >= V_if_indexlim) 474 if_grow(); 475 476 ifp->if_type = type; 477 478 if (if_com_alloc[type] != NULL) { 479 ifp->if_l2com = if_com_alloc[type](type, ifp); 480 if (ifp->if_l2com == NULL) { 481 free(ifp, M_IFNET); 482 return (NULL); 483 } 484 } 485 IFNET_WLOCK(); 486 ifnet_setbyindex(ifp->if_index, ifp); 487 IFNET_WUNLOCK(); 488 IF_ADDR_LOCK_INIT(ifp); 489 490 return (ifp); 491 } 492 493 /* 494 * Free the struct ifnet, the associated index, and the layer 2 common 495 * structure if needed. All the work is done in if_free_type(). 496 * 497 * Do not add code to this function! Add it to if_free_type(). 498 */ 499 void 500 if_free(struct ifnet *ifp) 501 { 502 503 if_free_type(ifp, ifp->if_type); 504 } 505 506 /* 507 * Do the actual work of freeing a struct ifnet, associated index, and 508 * layer 2 common structure. This version should only be called by 509 * intefaces that switch their type after calling if_alloc(). 510 */ 511 void 512 if_free_type(struct ifnet *ifp, u_char type) 513 { 514 INIT_VNET_NET(curvnet); /* ifp->if_vnet can be NULL here ! */ 515 516 if (ifp != ifnet_byindex(ifp->if_index)) { 517 if_printf(ifp, "%s: value was not if_alloced, skipping\n", 518 __func__); 519 return; 520 } 521 522 IFNET_WLOCK(); 523 ifnet_setbyindex(ifp->if_index, NULL); 524 525 /* XXX: should be locked with if_findindex() */ 526 while (V_if_index > 0 && ifnet_byindex_locked(V_if_index) == NULL) 527 V_if_index--; 528 IFNET_WUNLOCK(); 529 530 if (if_com_free[type] != NULL) 531 if_com_free[type](ifp->if_l2com, type); 532 533 IF_ADDR_LOCK_DESTROY(ifp); 534 free(ifp, M_IFNET); 535 }; 536 537 void 538 ifq_attach(struct ifaltq *ifq, struct ifnet *ifp) 539 { 540 541 mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF); 542 543 if (ifq->ifq_maxlen == 0) 544 ifq->ifq_maxlen = ifqmaxlen; 545 546 ifq->altq_type = 0; 547 ifq->altq_disc = NULL; 548 ifq->altq_flags &= ALTQF_CANTCHANGE; 549 ifq->altq_tbr = NULL; 550 ifq->altq_ifp = ifp; 551 } 552 553 void 554 ifq_detach(struct ifaltq *ifq) 555 { 556 mtx_destroy(&ifq->ifq_mtx); 557 } 558 559 /* 560 * Perform generic interface initalization tasks and attach the interface 561 * to the list of "active" interfaces. 562 * 563 * XXX: 564 * - The decision to return void and thus require this function to 565 * succeed is questionable. 566 * - We do more initialization here then is probably a good idea. 567 * Some of this should probably move to if_alloc(). 568 * - We should probably do more sanity checking. For instance we don't 569 * do anything to insure if_xname is unique or non-empty. 570 */ 571 void 572 if_attach(struct ifnet *ifp) 573 { 574 INIT_VNET_NET(curvnet); 575 unsigned socksize, ifasize; 576 int namelen, masklen; 577 struct sockaddr_dl *sdl; 578 struct ifaddr *ifa; 579 580 if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index)) 581 panic ("%s: BUG: if_attach called without if_alloc'd input()\n", 582 ifp->if_xname); 583 584 TASK_INIT(&ifp->if_starttask, 0, if_start_deferred, ifp); 585 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp); 586 IF_AFDATA_LOCK_INIT(ifp); 587 ifp->if_afdata_initialized = 0; 588 589 TAILQ_INIT(&ifp->if_addrhead); 590 TAILQ_INIT(&ifp->if_prefixhead); 591 TAILQ_INIT(&ifp->if_multiaddrs); 592 TAILQ_INIT(&ifp->if_groups); 593 594 if_addgroup(ifp, IFG_ALL); 595 596 knlist_init(&ifp->if_klist, NULL, NULL, NULL, NULL); 597 getmicrotime(&ifp->if_lastchange); 598 ifp->if_data.ifi_epoch = time_uptime; 599 ifp->if_data.ifi_datalen = sizeof(struct if_data); 600 ifp->if_transmit = if_transmit; 601 ifp->if_qflush = if_qflush; 602 #ifdef MAC 603 mac_ifnet_init(ifp); 604 mac_ifnet_create(ifp); 605 #endif 606 607 ifdev_setbyindex(ifp->if_index, make_dev(&net_cdevsw, 608 ifp->if_index, UID_ROOT, GID_WHEEL, 0600, "%s/%s", 609 net_cdevsw.d_name, ifp->if_xname)); 610 make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d", 611 net_cdevsw.d_name, ifp->if_index); 612 613 ifq_attach(&ifp->if_snd, ifp); 614 615 /* 616 * create a Link Level name for this device 617 */ 618 namelen = strlen(ifp->if_xname); 619 /* 620 * Always save enough space for any possiable name so we can do 621 * a rename in place later. 622 */ 623 masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ; 624 socksize = masklen + ifp->if_addrlen; 625 if (socksize < sizeof(*sdl)) 626 socksize = sizeof(*sdl); 627 socksize = roundup2(socksize, sizeof(long)); 628 ifasize = sizeof(*ifa) + 2 * socksize; 629 ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO); 630 IFA_LOCK_INIT(ifa); 631 sdl = (struct sockaddr_dl *)(ifa + 1); 632 sdl->sdl_len = socksize; 633 sdl->sdl_family = AF_LINK; 634 bcopy(ifp->if_xname, sdl->sdl_data, namelen); 635 sdl->sdl_nlen = namelen; 636 sdl->sdl_index = ifp->if_index; 637 sdl->sdl_type = ifp->if_type; 638 ifp->if_addr = ifa; 639 ifa->ifa_ifp = ifp; 640 ifa->ifa_rtrequest = link_rtrequest; 641 ifa->ifa_addr = (struct sockaddr *)sdl; 642 sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 643 ifa->ifa_netmask = (struct sockaddr *)sdl; 644 sdl->sdl_len = masklen; 645 while (namelen != 0) 646 sdl->sdl_data[--namelen] = 0xff; 647 ifa->ifa_refcnt = 1; 648 TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link); 649 ifp->if_broadcastaddr = NULL; /* reliably crash if used uninitialized */ 650 651 652 IFNET_WLOCK(); 653 TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link); 654 IFNET_WUNLOCK(); 655 656 if (domain_init_status >= 2) 657 if_attachdomain1(ifp); 658 659 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 660 devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL); 661 662 /* Announce the interface. */ 663 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 664 665 if (ifp->if_watchdog != NULL) { 666 if_printf(ifp, 667 "WARNING: using obsoleted if_watchdog interface\n"); 668 669 /* 670 * Note that we need if_slowtimo(). If this happens after 671 * boot, then call if_slowtimo() directly. 672 */ 673 if (atomic_cmpset_int(&slowtimo_started, 0, 1) && !cold) 674 if_slowtimo(0); 675 } 676 if (ifp->if_flags & IFF_NEEDSGIANT) 677 if_printf(ifp, 678 "WARNING: using obsoleted IFF_NEEDSGIANT flag\n"); 679 } 680 681 static void 682 if_attachdomain(void *dummy) 683 { 684 INIT_VNET_NET(curvnet); 685 struct ifnet *ifp; 686 int s; 687 688 s = splnet(); 689 TAILQ_FOREACH(ifp, &V_ifnet, if_link) 690 if_attachdomain1(ifp); 691 splx(s); 692 } 693 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND, 694 if_attachdomain, NULL); 695 696 static void 697 if_attachdomain1(struct ifnet *ifp) 698 { 699 struct domain *dp; 700 int s; 701 702 s = splnet(); 703 704 /* 705 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we 706 * cannot lock ifp->if_afdata initialization, entirely. 707 */ 708 if (IF_AFDATA_TRYLOCK(ifp) == 0) { 709 splx(s); 710 return; 711 } 712 if (ifp->if_afdata_initialized >= domain_init_status) { 713 IF_AFDATA_UNLOCK(ifp); 714 splx(s); 715 printf("if_attachdomain called more than once on %s\n", 716 ifp->if_xname); 717 return; 718 } 719 ifp->if_afdata_initialized = domain_init_status; 720 IF_AFDATA_UNLOCK(ifp); 721 722 /* address family dependent data region */ 723 bzero(ifp->if_afdata, sizeof(ifp->if_afdata)); 724 for (dp = domains; dp; dp = dp->dom_next) { 725 if (dp->dom_ifattach) 726 ifp->if_afdata[dp->dom_family] = 727 (*dp->dom_ifattach)(ifp); 728 } 729 730 splx(s); 731 } 732 733 /* 734 * Remove any unicast or broadcast network addresses from an interface. 735 */ 736 void 737 if_purgeaddrs(struct ifnet *ifp) 738 { 739 struct ifaddr *ifa, *next; 740 741 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) { 742 if (ifa->ifa_addr->sa_family == AF_LINK) 743 continue; 744 #ifdef INET 745 /* XXX: Ugly!! ad hoc just for INET */ 746 if (ifa->ifa_addr->sa_family == AF_INET) { 747 struct ifaliasreq ifr; 748 749 bzero(&ifr, sizeof(ifr)); 750 ifr.ifra_addr = *ifa->ifa_addr; 751 if (ifa->ifa_dstaddr) 752 ifr.ifra_broadaddr = *ifa->ifa_dstaddr; 753 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp, 754 NULL) == 0) 755 continue; 756 } 757 #endif /* INET */ 758 #ifdef INET6 759 if (ifa->ifa_addr->sa_family == AF_INET6) { 760 in6_purgeaddr(ifa); 761 /* ifp_addrhead is already updated */ 762 continue; 763 } 764 #endif /* INET6 */ 765 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 766 IFAFREE(ifa); 767 } 768 } 769 770 /* 771 * Remove any multicast network addresses from an interface. 772 */ 773 void 774 if_purgemaddrs(struct ifnet *ifp) 775 { 776 struct ifmultiaddr *ifma; 777 struct ifmultiaddr *next; 778 779 IF_ADDR_LOCK(ifp); 780 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) 781 if_delmulti_locked(ifp, ifma, 1); 782 IF_ADDR_UNLOCK(ifp); 783 } 784 785 /* 786 * Detach an interface, removing it from the 787 * list of "active" interfaces. 788 * 789 * XXXRW: There are some significant questions about event ordering, and 790 * how to prevent things from starting to use the interface during detach. 791 */ 792 void 793 if_detach(struct ifnet *ifp) 794 { 795 INIT_VNET_NET(ifp->if_vnet); 796 struct ifaddr *ifa; 797 struct radix_node_head *rnh; 798 int s, i, j; 799 struct domain *dp; 800 struct ifnet *iter; 801 int found = 0; 802 803 IFNET_WLOCK(); 804 TAILQ_FOREACH(iter, &V_ifnet, if_link) 805 if (iter == ifp) { 806 TAILQ_REMOVE(&V_ifnet, ifp, if_link); 807 found = 1; 808 break; 809 } 810 IFNET_WUNLOCK(); 811 if (!found) 812 return; 813 814 /* 815 * Remove/wait for pending events. 816 */ 817 taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 818 819 /* 820 * Remove routes and flush queues. 821 */ 822 s = splnet(); 823 if_down(ifp); 824 #ifdef ALTQ 825 if (ALTQ_IS_ENABLED(&ifp->if_snd)) 826 altq_disable(&ifp->if_snd); 827 if (ALTQ_IS_ATTACHED(&ifp->if_snd)) 828 altq_detach(&ifp->if_snd); 829 #endif 830 831 if_purgeaddrs(ifp); 832 833 #ifdef INET 834 in_ifdetach(ifp); 835 #endif 836 837 #ifdef INET6 838 /* 839 * Remove all IPv6 kernel structs related to ifp. This should be done 840 * before removing routing entries below, since IPv6 interface direct 841 * routes are expected to be removed by the IPv6-specific kernel API. 842 * Otherwise, the kernel will detect some inconsistency and bark it. 843 */ 844 in6_ifdetach(ifp); 845 #endif 846 if_purgemaddrs(ifp); 847 848 /* 849 * Remove link ifaddr pointer and maybe decrement if_index. 850 * Clean up all addresses. 851 */ 852 ifp->if_addr = NULL; 853 destroy_dev(ifdev_byindex(ifp->if_index)); 854 ifdev_setbyindex(ifp->if_index, NULL); 855 856 /* We can now free link ifaddr. */ 857 if (!TAILQ_EMPTY(&ifp->if_addrhead)) { 858 ifa = TAILQ_FIRST(&ifp->if_addrhead); 859 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 860 IFAFREE(ifa); 861 } 862 863 /* 864 * Delete all remaining routes using this interface 865 * Unfortuneatly the only way to do this is to slog through 866 * the entire routing table looking for routes which point 867 * to this interface...oh well... 868 */ 869 for (i = 1; i <= AF_MAX; i++) { 870 for (j = 0; j < rt_numfibs; j++) { 871 if ((rnh = V_rt_tables[j][i]) == NULL) 872 continue; 873 RADIX_NODE_HEAD_LOCK(rnh); 874 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp); 875 RADIX_NODE_HEAD_UNLOCK(rnh); 876 } 877 } 878 879 /* Announce that the interface is gone. */ 880 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 881 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 882 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL); 883 884 IF_AFDATA_LOCK(ifp); 885 for (dp = domains; dp; dp = dp->dom_next) { 886 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) 887 (*dp->dom_ifdetach)(ifp, 888 ifp->if_afdata[dp->dom_family]); 889 } 890 IF_AFDATA_UNLOCK(ifp); 891 892 #ifdef MAC 893 mac_ifnet_destroy(ifp); 894 #endif /* MAC */ 895 KNOTE_UNLOCKED(&ifp->if_klist, NOTE_EXIT); 896 knlist_clear(&ifp->if_klist, 0); 897 knlist_destroy(&ifp->if_klist); 898 ifq_detach(&ifp->if_snd); 899 IF_AFDATA_DESTROY(ifp); 900 splx(s); 901 } 902 903 /* 904 * Add a group to an interface 905 */ 906 int 907 if_addgroup(struct ifnet *ifp, const char *groupname) 908 { 909 INIT_VNET_NET(ifp->if_vnet); 910 struct ifg_list *ifgl; 911 struct ifg_group *ifg = NULL; 912 struct ifg_member *ifgm; 913 914 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' && 915 groupname[strlen(groupname) - 1] <= '9') 916 return (EINVAL); 917 918 IFNET_WLOCK(); 919 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 920 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) { 921 IFNET_WUNLOCK(); 922 return (EEXIST); 923 } 924 925 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP, 926 M_NOWAIT)) == NULL) { 927 IFNET_WUNLOCK(); 928 return (ENOMEM); 929 } 930 931 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member), 932 M_TEMP, M_NOWAIT)) == NULL) { 933 free(ifgl, M_TEMP); 934 IFNET_WUNLOCK(); 935 return (ENOMEM); 936 } 937 938 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 939 if (!strcmp(ifg->ifg_group, groupname)) 940 break; 941 942 if (ifg == NULL) { 943 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group), 944 M_TEMP, M_NOWAIT)) == NULL) { 945 free(ifgl, M_TEMP); 946 free(ifgm, M_TEMP); 947 IFNET_WUNLOCK(); 948 return (ENOMEM); 949 } 950 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group)); 951 ifg->ifg_refcnt = 0; 952 TAILQ_INIT(&ifg->ifg_members); 953 EVENTHANDLER_INVOKE(group_attach_event, ifg); 954 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next); 955 } 956 957 ifg->ifg_refcnt++; 958 ifgl->ifgl_group = ifg; 959 ifgm->ifgm_ifp = ifp; 960 961 IF_ADDR_LOCK(ifp); 962 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next); 963 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next); 964 IF_ADDR_UNLOCK(ifp); 965 966 IFNET_WUNLOCK(); 967 968 EVENTHANDLER_INVOKE(group_change_event, groupname); 969 970 return (0); 971 } 972 973 /* 974 * Remove a group from an interface 975 */ 976 int 977 if_delgroup(struct ifnet *ifp, const char *groupname) 978 { 979 INIT_VNET_NET(ifp->if_vnet); 980 struct ifg_list *ifgl; 981 struct ifg_member *ifgm; 982 983 IFNET_WLOCK(); 984 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 985 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) 986 break; 987 if (ifgl == NULL) { 988 IFNET_WUNLOCK(); 989 return (ENOENT); 990 } 991 992 IF_ADDR_LOCK(ifp); 993 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next); 994 IF_ADDR_UNLOCK(ifp); 995 996 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) 997 if (ifgm->ifgm_ifp == ifp) 998 break; 999 1000 if (ifgm != NULL) { 1001 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next); 1002 free(ifgm, M_TEMP); 1003 } 1004 1005 if (--ifgl->ifgl_group->ifg_refcnt == 0) { 1006 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next); 1007 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group); 1008 free(ifgl->ifgl_group, M_TEMP); 1009 } 1010 IFNET_WUNLOCK(); 1011 1012 free(ifgl, M_TEMP); 1013 1014 EVENTHANDLER_INVOKE(group_change_event, groupname); 1015 1016 return (0); 1017 } 1018 1019 /* 1020 * Stores all groups from an interface in memory pointed 1021 * to by data 1022 */ 1023 static int 1024 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp) 1025 { 1026 int len, error; 1027 struct ifg_list *ifgl; 1028 struct ifg_req ifgrq, *ifgp; 1029 struct ifgroupreq *ifgr = data; 1030 1031 if (ifgr->ifgr_len == 0) { 1032 IF_ADDR_LOCK(ifp); 1033 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 1034 ifgr->ifgr_len += sizeof(struct ifg_req); 1035 IF_ADDR_UNLOCK(ifp); 1036 return (0); 1037 } 1038 1039 len = ifgr->ifgr_len; 1040 ifgp = ifgr->ifgr_groups; 1041 /* XXX: wire */ 1042 IF_ADDR_LOCK(ifp); 1043 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) { 1044 if (len < sizeof(ifgrq)) { 1045 IF_ADDR_UNLOCK(ifp); 1046 return (EINVAL); 1047 } 1048 bzero(&ifgrq, sizeof ifgrq); 1049 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group, 1050 sizeof(ifgrq.ifgrq_group)); 1051 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 1052 IF_ADDR_UNLOCK(ifp); 1053 return (error); 1054 } 1055 len -= sizeof(ifgrq); 1056 ifgp++; 1057 } 1058 IF_ADDR_UNLOCK(ifp); 1059 1060 return (0); 1061 } 1062 1063 /* 1064 * Stores all members of a group in memory pointed to by data 1065 */ 1066 static int 1067 if_getgroupmembers(struct ifgroupreq *data) 1068 { 1069 INIT_VNET_NET(curvnet); 1070 struct ifgroupreq *ifgr = data; 1071 struct ifg_group *ifg; 1072 struct ifg_member *ifgm; 1073 struct ifg_req ifgrq, *ifgp; 1074 int len, error; 1075 1076 IFNET_RLOCK(); 1077 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 1078 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name)) 1079 break; 1080 if (ifg == NULL) { 1081 IFNET_RUNLOCK(); 1082 return (ENOENT); 1083 } 1084 1085 if (ifgr->ifgr_len == 0) { 1086 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) 1087 ifgr->ifgr_len += sizeof(ifgrq); 1088 IFNET_RUNLOCK(); 1089 return (0); 1090 } 1091 1092 len = ifgr->ifgr_len; 1093 ifgp = ifgr->ifgr_groups; 1094 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) { 1095 if (len < sizeof(ifgrq)) { 1096 IFNET_RUNLOCK(); 1097 return (EINVAL); 1098 } 1099 bzero(&ifgrq, sizeof ifgrq); 1100 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname, 1101 sizeof(ifgrq.ifgrq_member)); 1102 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 1103 IFNET_RUNLOCK(); 1104 return (error); 1105 } 1106 len -= sizeof(ifgrq); 1107 ifgp++; 1108 } 1109 IFNET_RUNLOCK(); 1110 1111 return (0); 1112 } 1113 1114 /* 1115 * Delete Routes for a Network Interface 1116 * 1117 * Called for each routing entry via the rnh->rnh_walktree() call above 1118 * to delete all route entries referencing a detaching network interface. 1119 * 1120 * Arguments: 1121 * rn pointer to node in the routing table 1122 * arg argument passed to rnh->rnh_walktree() - detaching interface 1123 * 1124 * Returns: 1125 * 0 successful 1126 * errno failed - reason indicated 1127 * 1128 */ 1129 static int 1130 if_rtdel(struct radix_node *rn, void *arg) 1131 { 1132 struct rtentry *rt = (struct rtentry *)rn; 1133 struct ifnet *ifp = arg; 1134 int err; 1135 1136 if (rt->rt_ifp == ifp) { 1137 1138 /* 1139 * Protect (sorta) against walktree recursion problems 1140 * with cloned routes 1141 */ 1142 if ((rt->rt_flags & RTF_UP) == 0) 1143 return (0); 1144 1145 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway, 1146 rt_mask(rt), rt->rt_flags|RTF_RNH_LOCKED, 1147 (struct rtentry **) NULL, rt->rt_fibnum); 1148 if (err) { 1149 log(LOG_WARNING, "if_rtdel: error %d\n", err); 1150 } 1151 } 1152 1153 return (0); 1154 } 1155 1156 /* 1157 * XXX: Because sockaddr_dl has deeper structure than the sockaddr 1158 * structs used to represent other address families, it is necessary 1159 * to perform a different comparison. 1160 */ 1161 1162 #define sa_equal(a1, a2) \ 1163 (bcmp((a1), (a2), ((a1))->sa_len) == 0) 1164 1165 #define sa_dl_equal(a1, a2) \ 1166 ((((struct sockaddr_dl *)(a1))->sdl_len == \ 1167 ((struct sockaddr_dl *)(a2))->sdl_len) && \ 1168 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \ 1169 LLADDR((struct sockaddr_dl *)(a2)), \ 1170 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0)) 1171 1172 /* 1173 * Locate an interface based on a complete address. 1174 */ 1175 /*ARGSUSED*/ 1176 struct ifaddr * 1177 ifa_ifwithaddr(struct sockaddr *addr) 1178 { 1179 INIT_VNET_NET(curvnet); 1180 struct ifnet *ifp; 1181 struct ifaddr *ifa; 1182 1183 IFNET_RLOCK(); 1184 TAILQ_FOREACH(ifp, &V_ifnet, if_link) 1185 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1186 if (ifa->ifa_addr->sa_family != addr->sa_family) 1187 continue; 1188 if (sa_equal(addr, ifa->ifa_addr)) 1189 goto done; 1190 /* IP6 doesn't have broadcast */ 1191 if ((ifp->if_flags & IFF_BROADCAST) && 1192 ifa->ifa_broadaddr && 1193 ifa->ifa_broadaddr->sa_len != 0 && 1194 sa_equal(ifa->ifa_broadaddr, addr)) 1195 goto done; 1196 } 1197 ifa = NULL; 1198 done: 1199 IFNET_RUNLOCK(); 1200 return (ifa); 1201 } 1202 1203 /* 1204 * Locate an interface based on the broadcast address. 1205 */ 1206 /* ARGSUSED */ 1207 struct ifaddr * 1208 ifa_ifwithbroadaddr(struct sockaddr *addr) 1209 { 1210 INIT_VNET_NET(curvnet); 1211 struct ifnet *ifp; 1212 struct ifaddr *ifa; 1213 1214 IFNET_RLOCK(); 1215 TAILQ_FOREACH(ifp, &V_ifnet, if_link) 1216 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1217 if (ifa->ifa_addr->sa_family != addr->sa_family) 1218 continue; 1219 if ((ifp->if_flags & IFF_BROADCAST) && 1220 ifa->ifa_broadaddr && 1221 ifa->ifa_broadaddr->sa_len != 0 && 1222 sa_equal(ifa->ifa_broadaddr, addr)) 1223 goto done; 1224 } 1225 ifa = NULL; 1226 done: 1227 IFNET_RUNLOCK(); 1228 return (ifa); 1229 } 1230 1231 /* 1232 * Locate the point to point interface with a given destination address. 1233 */ 1234 /*ARGSUSED*/ 1235 struct ifaddr * 1236 ifa_ifwithdstaddr(struct sockaddr *addr) 1237 { 1238 INIT_VNET_NET(curvnet); 1239 struct ifnet *ifp; 1240 struct ifaddr *ifa; 1241 1242 IFNET_RLOCK(); 1243 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1244 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 1245 continue; 1246 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1247 if (ifa->ifa_addr->sa_family != addr->sa_family) 1248 continue; 1249 if (ifa->ifa_dstaddr != NULL && 1250 sa_equal(addr, ifa->ifa_dstaddr)) 1251 goto done; 1252 } 1253 } 1254 ifa = NULL; 1255 done: 1256 IFNET_RUNLOCK(); 1257 return (ifa); 1258 } 1259 1260 /* 1261 * Find an interface on a specific network. If many, choice 1262 * is most specific found. 1263 */ 1264 struct ifaddr * 1265 ifa_ifwithnet(struct sockaddr *addr) 1266 { 1267 INIT_VNET_NET(curvnet); 1268 struct ifnet *ifp; 1269 struct ifaddr *ifa; 1270 struct ifaddr *ifa_maybe = (struct ifaddr *) 0; 1271 u_int af = addr->sa_family; 1272 char *addr_data = addr->sa_data, *cplim; 1273 1274 /* 1275 * AF_LINK addresses can be looked up directly by their index number, 1276 * so do that if we can. 1277 */ 1278 if (af == AF_LINK) { 1279 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; 1280 if (sdl->sdl_index && sdl->sdl_index <= V_if_index) 1281 return (ifaddr_byindex(sdl->sdl_index)); 1282 } 1283 1284 /* 1285 * Scan though each interface, looking for ones that have 1286 * addresses in this address family. 1287 */ 1288 IFNET_RLOCK(); 1289 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1290 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1291 char *cp, *cp2, *cp3; 1292 1293 if (ifa->ifa_addr->sa_family != af) 1294 next: continue; 1295 if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) { 1296 /* 1297 * This is a bit broken as it doesn't 1298 * take into account that the remote end may 1299 * be a single node in the network we are 1300 * looking for. 1301 * The trouble is that we don't know the 1302 * netmask for the remote end. 1303 */ 1304 if (ifa->ifa_dstaddr != NULL && 1305 sa_equal(addr, ifa->ifa_dstaddr)) 1306 goto done; 1307 } else { 1308 /* 1309 * if we have a special address handler, 1310 * then use it instead of the generic one. 1311 */ 1312 if (ifa->ifa_claim_addr) { 1313 if ((*ifa->ifa_claim_addr)(ifa, addr)) 1314 goto done; 1315 continue; 1316 } 1317 1318 /* 1319 * Scan all the bits in the ifa's address. 1320 * If a bit dissagrees with what we are 1321 * looking for, mask it with the netmask 1322 * to see if it really matters. 1323 * (A byte at a time) 1324 */ 1325 if (ifa->ifa_netmask == 0) 1326 continue; 1327 cp = addr_data; 1328 cp2 = ifa->ifa_addr->sa_data; 1329 cp3 = ifa->ifa_netmask->sa_data; 1330 cplim = ifa->ifa_netmask->sa_len 1331 + (char *)ifa->ifa_netmask; 1332 while (cp3 < cplim) 1333 if ((*cp++ ^ *cp2++) & *cp3++) 1334 goto next; /* next address! */ 1335 /* 1336 * If the netmask of what we just found 1337 * is more specific than what we had before 1338 * (if we had one) then remember the new one 1339 * before continuing to search 1340 * for an even better one. 1341 */ 1342 if (ifa_maybe == 0 || 1343 rn_refines((caddr_t)ifa->ifa_netmask, 1344 (caddr_t)ifa_maybe->ifa_netmask)) 1345 ifa_maybe = ifa; 1346 } 1347 } 1348 } 1349 ifa = ifa_maybe; 1350 done: 1351 IFNET_RUNLOCK(); 1352 return (ifa); 1353 } 1354 1355 /* 1356 * Find an interface address specific to an interface best matching 1357 * a given address. 1358 */ 1359 struct ifaddr * 1360 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp) 1361 { 1362 struct ifaddr *ifa; 1363 char *cp, *cp2, *cp3; 1364 char *cplim; 1365 struct ifaddr *ifa_maybe = 0; 1366 u_int af = addr->sa_family; 1367 1368 if (af >= AF_MAX) 1369 return (0); 1370 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1371 if (ifa->ifa_addr->sa_family != af) 1372 continue; 1373 if (ifa_maybe == 0) 1374 ifa_maybe = ifa; 1375 if (ifa->ifa_netmask == 0) { 1376 if (sa_equal(addr, ifa->ifa_addr) || 1377 (ifa->ifa_dstaddr && 1378 sa_equal(addr, ifa->ifa_dstaddr))) 1379 goto done; 1380 continue; 1381 } 1382 if (ifp->if_flags & IFF_POINTOPOINT) { 1383 if (sa_equal(addr, ifa->ifa_dstaddr)) 1384 goto done; 1385 } else { 1386 cp = addr->sa_data; 1387 cp2 = ifa->ifa_addr->sa_data; 1388 cp3 = ifa->ifa_netmask->sa_data; 1389 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 1390 for (; cp3 < cplim; cp3++) 1391 if ((*cp++ ^ *cp2++) & *cp3) 1392 break; 1393 if (cp3 == cplim) 1394 goto done; 1395 } 1396 } 1397 ifa = ifa_maybe; 1398 done: 1399 return (ifa); 1400 } 1401 1402 #include <net/route.h> 1403 #include <net/if_llatbl.h> 1404 1405 /* 1406 * Default action when installing a route with a Link Level gateway. 1407 * Lookup an appropriate real ifa to point to. 1408 * This should be moved to /sys/net/link.c eventually. 1409 */ 1410 static void 1411 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 1412 { 1413 struct ifaddr *ifa, *oifa; 1414 struct sockaddr *dst; 1415 struct ifnet *ifp; 1416 1417 RT_LOCK_ASSERT(rt); 1418 1419 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 1420 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 1421 return; 1422 ifa = ifaof_ifpforaddr(dst, ifp); 1423 if (ifa) { 1424 IFAREF(ifa); /* XXX */ 1425 oifa = rt->rt_ifa; 1426 rt->rt_ifa = ifa; 1427 IFAFREE(oifa); 1428 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 1429 ifa->ifa_rtrequest(cmd, rt, info); 1430 } 1431 } 1432 1433 /* 1434 * Mark an interface down and notify protocols of 1435 * the transition. 1436 * NOTE: must be called at splnet or eqivalent. 1437 */ 1438 static void 1439 if_unroute(struct ifnet *ifp, int flag, int fam) 1440 { 1441 struct ifaddr *ifa; 1442 1443 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP")); 1444 1445 ifp->if_flags &= ~flag; 1446 getmicrotime(&ifp->if_lastchange); 1447 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1448 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1449 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 1450 ifp->if_qflush(ifp); 1451 1452 #ifdef DEV_CARP 1453 if (ifp->if_carp) 1454 carp_carpdev_state(ifp->if_carp); 1455 #endif 1456 rt_ifmsg(ifp); 1457 } 1458 1459 /* 1460 * Mark an interface up and notify protocols of 1461 * the transition. 1462 * NOTE: must be called at splnet or eqivalent. 1463 */ 1464 static void 1465 if_route(struct ifnet *ifp, int flag, int fam) 1466 { 1467 struct ifaddr *ifa; 1468 1469 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP")); 1470 1471 ifp->if_flags |= flag; 1472 getmicrotime(&ifp->if_lastchange); 1473 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1474 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1475 pfctlinput(PRC_IFUP, ifa->ifa_addr); 1476 #ifdef DEV_CARP 1477 if (ifp->if_carp) 1478 carp_carpdev_state(ifp->if_carp); 1479 #endif 1480 rt_ifmsg(ifp); 1481 #ifdef INET6 1482 in6_if_up(ifp); 1483 #endif 1484 } 1485 1486 void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */ 1487 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */ 1488 1489 /* 1490 * Handle a change in the interface link state. To avoid LORs 1491 * between driver lock and upper layer locks, as well as possible 1492 * recursions, we post event to taskqueue, and all job 1493 * is done in static do_link_state_change(). 1494 */ 1495 void 1496 if_link_state_change(struct ifnet *ifp, int link_state) 1497 { 1498 /* Return if state hasn't changed. */ 1499 if (ifp->if_link_state == link_state) 1500 return; 1501 1502 ifp->if_link_state = link_state; 1503 1504 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask); 1505 } 1506 1507 static void 1508 do_link_state_change(void *arg, int pending) 1509 { 1510 struct ifnet *ifp = (struct ifnet *)arg; 1511 int link_state = ifp->if_link_state; 1512 int link; 1513 CURVNET_SET(ifp->if_vnet); 1514 1515 /* Notify that the link state has changed. */ 1516 rt_ifmsg(ifp); 1517 if (link_state == LINK_STATE_UP) 1518 link = NOTE_LINKUP; 1519 else if (link_state == LINK_STATE_DOWN) 1520 link = NOTE_LINKDOWN; 1521 else 1522 link = NOTE_LINKINV; 1523 KNOTE_UNLOCKED(&ifp->if_klist, link); 1524 if (ifp->if_vlantrunk != NULL) 1525 (*vlan_link_state_p)(ifp, link); 1526 1527 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) && 1528 IFP2AC(ifp)->ac_netgraph != NULL) 1529 (*ng_ether_link_state_p)(ifp, link_state); 1530 #ifdef DEV_CARP 1531 if (ifp->if_carp) 1532 carp_carpdev_state(ifp->if_carp); 1533 #endif 1534 if (ifp->if_bridge) { 1535 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!")); 1536 (*bstp_linkstate_p)(ifp, link_state); 1537 } 1538 if (ifp->if_lagg) { 1539 KASSERT(lagg_linkstate_p != NULL,("if_lagg not loaded!")); 1540 (*lagg_linkstate_p)(ifp, link_state); 1541 } 1542 1543 devctl_notify("IFNET", ifp->if_xname, 1544 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", NULL); 1545 if (pending > 1) 1546 if_printf(ifp, "%d link states coalesced\n", pending); 1547 if (log_link_state_change) 1548 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname, 1549 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" ); 1550 CURVNET_RESTORE(); 1551 } 1552 1553 /* 1554 * Mark an interface down and notify protocols of 1555 * the transition. 1556 * NOTE: must be called at splnet or eqivalent. 1557 */ 1558 void 1559 if_down(struct ifnet *ifp) 1560 { 1561 1562 if_unroute(ifp, IFF_UP, AF_UNSPEC); 1563 } 1564 1565 /* 1566 * Mark an interface up and notify protocols of 1567 * the transition. 1568 * NOTE: must be called at splnet or eqivalent. 1569 */ 1570 void 1571 if_up(struct ifnet *ifp) 1572 { 1573 1574 if_route(ifp, IFF_UP, AF_UNSPEC); 1575 } 1576 1577 /* 1578 * Flush an interface queue. 1579 */ 1580 static void 1581 if_qflush(struct ifnet *ifp) 1582 { 1583 struct mbuf *m, *n; 1584 struct ifaltq *ifq; 1585 1586 ifq = &ifp->if_snd; 1587 IFQ_LOCK(ifq); 1588 #ifdef ALTQ 1589 if (ALTQ_IS_ENABLED(ifq)) 1590 ALTQ_PURGE(ifq); 1591 #endif 1592 n = ifq->ifq_head; 1593 while ((m = n) != 0) { 1594 n = m->m_act; 1595 m_freem(m); 1596 } 1597 ifq->ifq_head = 0; 1598 ifq->ifq_tail = 0; 1599 ifq->ifq_len = 0; 1600 IFQ_UNLOCK(ifq); 1601 } 1602 1603 /* 1604 * Handle interface watchdog timer routines. Called 1605 * from softclock, we decrement timers (if set) and 1606 * call the appropriate interface routine on expiration. 1607 * 1608 * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called 1609 * holding Giant. If we switch to an MPSAFE callout, we likely need to grab 1610 * Giant before entering if_watchdog() on an IFF_NEEDSGIANT interface. 1611 */ 1612 static void 1613 if_slowtimo(void *arg) 1614 { 1615 VNET_ITERATOR_DECL(vnet_iter); 1616 struct ifnet *ifp; 1617 int s = splimp(); 1618 1619 IFNET_RLOCK(); 1620 VNET_LIST_RLOCK(); 1621 VNET_FOREACH(vnet_iter) { 1622 CURVNET_SET(vnet_iter); 1623 INIT_VNET_NET(vnet_iter); 1624 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1625 if (ifp->if_timer == 0 || --ifp->if_timer) 1626 continue; 1627 if (ifp->if_watchdog) 1628 (*ifp->if_watchdog)(ifp); 1629 } 1630 CURVNET_RESTORE(); 1631 } 1632 VNET_LIST_RUNLOCK(); 1633 IFNET_RUNLOCK(); 1634 splx(s); 1635 timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ); 1636 } 1637 1638 /* 1639 * Map interface name to 1640 * interface structure pointer. 1641 */ 1642 struct ifnet * 1643 ifunit(const char *name) 1644 { 1645 INIT_VNET_NET(curvnet); 1646 struct ifnet *ifp; 1647 1648 IFNET_RLOCK(); 1649 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1650 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0) 1651 break; 1652 } 1653 IFNET_RUNLOCK(); 1654 return (ifp); 1655 } 1656 1657 /* 1658 * Hardware specific interface ioctls. 1659 */ 1660 static int 1661 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td) 1662 { 1663 struct ifreq *ifr; 1664 struct ifstat *ifs; 1665 int error = 0; 1666 int new_flags, temp_flags; 1667 size_t namelen, onamelen; 1668 char new_name[IFNAMSIZ]; 1669 struct ifaddr *ifa; 1670 struct sockaddr_dl *sdl; 1671 1672 ifr = (struct ifreq *)data; 1673 switch (cmd) { 1674 case SIOCGIFINDEX: 1675 ifr->ifr_index = ifp->if_index; 1676 break; 1677 1678 case SIOCGIFFLAGS: 1679 temp_flags = ifp->if_flags | ifp->if_drv_flags; 1680 ifr->ifr_flags = temp_flags & 0xffff; 1681 ifr->ifr_flagshigh = temp_flags >> 16; 1682 break; 1683 1684 case SIOCGIFCAP: 1685 ifr->ifr_reqcap = ifp->if_capabilities; 1686 ifr->ifr_curcap = ifp->if_capenable; 1687 break; 1688 1689 #ifdef MAC 1690 case SIOCGIFMAC: 1691 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp); 1692 break; 1693 #endif 1694 1695 case SIOCGIFMETRIC: 1696 ifr->ifr_metric = ifp->if_metric; 1697 break; 1698 1699 case SIOCGIFMTU: 1700 ifr->ifr_mtu = ifp->if_mtu; 1701 break; 1702 1703 case SIOCGIFPHYS: 1704 ifr->ifr_phys = ifp->if_physical; 1705 break; 1706 1707 case SIOCSIFFLAGS: 1708 error = priv_check(td, PRIV_NET_SETIFFLAGS); 1709 if (error) 1710 return (error); 1711 /* 1712 * Currently, no driver owned flags pass the IFF_CANTCHANGE 1713 * check, so we don't need special handling here yet. 1714 */ 1715 new_flags = (ifr->ifr_flags & 0xffff) | 1716 (ifr->ifr_flagshigh << 16); 1717 if (ifp->if_flags & IFF_SMART) { 1718 /* Smart drivers twiddle their own routes */ 1719 } else if (ifp->if_flags & IFF_UP && 1720 (new_flags & IFF_UP) == 0) { 1721 int s = splimp(); 1722 if_down(ifp); 1723 splx(s); 1724 } else if (new_flags & IFF_UP && 1725 (ifp->if_flags & IFF_UP) == 0) { 1726 int s = splimp(); 1727 if_up(ifp); 1728 splx(s); 1729 } 1730 /* See if permanently promiscuous mode bit is about to flip */ 1731 if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) { 1732 if (new_flags & IFF_PPROMISC) 1733 ifp->if_flags |= IFF_PROMISC; 1734 else if (ifp->if_pcount == 0) 1735 ifp->if_flags &= ~IFF_PROMISC; 1736 log(LOG_INFO, "%s: permanently promiscuous mode %s\n", 1737 ifp->if_xname, 1738 (new_flags & IFF_PPROMISC) ? "enabled" : "disabled"); 1739 } 1740 ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 1741 (new_flags &~ IFF_CANTCHANGE); 1742 if (ifp->if_ioctl) { 1743 IFF_LOCKGIANT(ifp); 1744 (void) (*ifp->if_ioctl)(ifp, cmd, data); 1745 IFF_UNLOCKGIANT(ifp); 1746 } 1747 getmicrotime(&ifp->if_lastchange); 1748 break; 1749 1750 case SIOCSIFCAP: 1751 error = priv_check(td, PRIV_NET_SETIFCAP); 1752 if (error) 1753 return (error); 1754 if (ifp->if_ioctl == NULL) 1755 return (EOPNOTSUPP); 1756 if (ifr->ifr_reqcap & ~ifp->if_capabilities) 1757 return (EINVAL); 1758 IFF_LOCKGIANT(ifp); 1759 error = (*ifp->if_ioctl)(ifp, cmd, data); 1760 IFF_UNLOCKGIANT(ifp); 1761 if (error == 0) 1762 getmicrotime(&ifp->if_lastchange); 1763 break; 1764 1765 #ifdef MAC 1766 case SIOCSIFMAC: 1767 error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp); 1768 break; 1769 #endif 1770 1771 case SIOCSIFNAME: 1772 error = priv_check(td, PRIV_NET_SETIFNAME); 1773 if (error) 1774 return (error); 1775 error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL); 1776 if (error != 0) 1777 return (error); 1778 if (new_name[0] == '\0') 1779 return (EINVAL); 1780 if (ifunit(new_name) != NULL) 1781 return (EEXIST); 1782 1783 /* Announce the departure of the interface. */ 1784 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 1785 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 1786 1787 log(LOG_INFO, "%s: changing name to '%s'\n", 1788 ifp->if_xname, new_name); 1789 1790 strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname)); 1791 ifa = ifp->if_addr; 1792 IFA_LOCK(ifa); 1793 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 1794 namelen = strlen(new_name); 1795 onamelen = sdl->sdl_nlen; 1796 /* 1797 * Move the address if needed. This is safe because we 1798 * allocate space for a name of length IFNAMSIZ when we 1799 * create this in if_attach(). 1800 */ 1801 if (namelen != onamelen) { 1802 bcopy(sdl->sdl_data + onamelen, 1803 sdl->sdl_data + namelen, sdl->sdl_alen); 1804 } 1805 bcopy(new_name, sdl->sdl_data, namelen); 1806 sdl->sdl_nlen = namelen; 1807 sdl = (struct sockaddr_dl *)ifa->ifa_netmask; 1808 bzero(sdl->sdl_data, onamelen); 1809 while (namelen != 0) 1810 sdl->sdl_data[--namelen] = 0xff; 1811 IFA_UNLOCK(ifa); 1812 1813 EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 1814 /* Announce the return of the interface. */ 1815 rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 1816 break; 1817 1818 case SIOCSIFMETRIC: 1819 error = priv_check(td, PRIV_NET_SETIFMETRIC); 1820 if (error) 1821 return (error); 1822 ifp->if_metric = ifr->ifr_metric; 1823 getmicrotime(&ifp->if_lastchange); 1824 break; 1825 1826 case SIOCSIFPHYS: 1827 error = priv_check(td, PRIV_NET_SETIFPHYS); 1828 if (error) 1829 return (error); 1830 if (ifp->if_ioctl == NULL) 1831 return (EOPNOTSUPP); 1832 IFF_LOCKGIANT(ifp); 1833 error = (*ifp->if_ioctl)(ifp, cmd, data); 1834 IFF_UNLOCKGIANT(ifp); 1835 if (error == 0) 1836 getmicrotime(&ifp->if_lastchange); 1837 break; 1838 1839 case SIOCSIFMTU: 1840 { 1841 u_long oldmtu = ifp->if_mtu; 1842 1843 error = priv_check(td, PRIV_NET_SETIFMTU); 1844 if (error) 1845 return (error); 1846 if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) 1847 return (EINVAL); 1848 if (ifp->if_ioctl == NULL) 1849 return (EOPNOTSUPP); 1850 IFF_LOCKGIANT(ifp); 1851 error = (*ifp->if_ioctl)(ifp, cmd, data); 1852 IFF_UNLOCKGIANT(ifp); 1853 if (error == 0) { 1854 getmicrotime(&ifp->if_lastchange); 1855 rt_ifmsg(ifp); 1856 } 1857 /* 1858 * If the link MTU changed, do network layer specific procedure. 1859 */ 1860 if (ifp->if_mtu != oldmtu) { 1861 #ifdef INET6 1862 nd6_setmtu(ifp); 1863 #endif 1864 } 1865 break; 1866 } 1867 1868 case SIOCADDMULTI: 1869 case SIOCDELMULTI: 1870 if (cmd == SIOCADDMULTI) 1871 error = priv_check(td, PRIV_NET_ADDMULTI); 1872 else 1873 error = priv_check(td, PRIV_NET_DELMULTI); 1874 if (error) 1875 return (error); 1876 1877 /* Don't allow group membership on non-multicast interfaces. */ 1878 if ((ifp->if_flags & IFF_MULTICAST) == 0) 1879 return (EOPNOTSUPP); 1880 1881 /* Don't let users screw up protocols' entries. */ 1882 if (ifr->ifr_addr.sa_family != AF_LINK) 1883 return (EINVAL); 1884 1885 if (cmd == SIOCADDMULTI) { 1886 struct ifmultiaddr *ifma; 1887 1888 /* 1889 * Userland is only permitted to join groups once 1890 * via the if_addmulti() KPI, because it cannot hold 1891 * struct ifmultiaddr * between calls. It may also 1892 * lose a race while we check if the membership 1893 * already exists. 1894 */ 1895 IF_ADDR_LOCK(ifp); 1896 ifma = if_findmulti(ifp, &ifr->ifr_addr); 1897 IF_ADDR_UNLOCK(ifp); 1898 if (ifma != NULL) 1899 error = EADDRINUSE; 1900 else 1901 error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 1902 } else { 1903 error = if_delmulti(ifp, &ifr->ifr_addr); 1904 } 1905 if (error == 0) 1906 getmicrotime(&ifp->if_lastchange); 1907 break; 1908 1909 case SIOCSIFPHYADDR: 1910 case SIOCDIFPHYADDR: 1911 #ifdef INET6 1912 case SIOCSIFPHYADDR_IN6: 1913 #endif 1914 case SIOCSLIFPHYADDR: 1915 case SIOCSIFMEDIA: 1916 case SIOCSIFGENERIC: 1917 error = priv_check(td, PRIV_NET_HWIOCTL); 1918 if (error) 1919 return (error); 1920 if (ifp->if_ioctl == NULL) 1921 return (EOPNOTSUPP); 1922 IFF_LOCKGIANT(ifp); 1923 error = (*ifp->if_ioctl)(ifp, cmd, data); 1924 IFF_UNLOCKGIANT(ifp); 1925 if (error == 0) 1926 getmicrotime(&ifp->if_lastchange); 1927 break; 1928 1929 case SIOCGIFSTATUS: 1930 ifs = (struct ifstat *)data; 1931 ifs->ascii[0] = '\0'; 1932 1933 case SIOCGIFPSRCADDR: 1934 case SIOCGIFPDSTADDR: 1935 case SIOCGLIFPHYADDR: 1936 case SIOCGIFMEDIA: 1937 case SIOCGIFGENERIC: 1938 if (ifp->if_ioctl == NULL) 1939 return (EOPNOTSUPP); 1940 IFF_LOCKGIANT(ifp); 1941 error = (*ifp->if_ioctl)(ifp, cmd, data); 1942 IFF_UNLOCKGIANT(ifp); 1943 break; 1944 1945 case SIOCSIFLLADDR: 1946 error = priv_check(td, PRIV_NET_SETLLADDR); 1947 if (error) 1948 return (error); 1949 error = if_setlladdr(ifp, 1950 ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); 1951 break; 1952 1953 case SIOCAIFGROUP: 1954 { 1955 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; 1956 1957 error = priv_check(td, PRIV_NET_ADDIFGROUP); 1958 if (error) 1959 return (error); 1960 if ((error = if_addgroup(ifp, ifgr->ifgr_group))) 1961 return (error); 1962 break; 1963 } 1964 1965 case SIOCGIFGROUP: 1966 if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp))) 1967 return (error); 1968 break; 1969 1970 case SIOCDIFGROUP: 1971 { 1972 struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; 1973 1974 error = priv_check(td, PRIV_NET_DELIFGROUP); 1975 if (error) 1976 return (error); 1977 if ((error = if_delgroup(ifp, ifgr->ifgr_group))) 1978 return (error); 1979 break; 1980 } 1981 1982 default: 1983 error = ENOIOCTL; 1984 break; 1985 } 1986 return (error); 1987 } 1988 1989 /* 1990 * Interface ioctls. 1991 */ 1992 int 1993 ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td) 1994 { 1995 struct ifnet *ifp; 1996 struct ifreq *ifr; 1997 int error; 1998 int oif_flags; 1999 2000 switch (cmd) { 2001 case SIOCGIFCONF: 2002 case OSIOCGIFCONF: 2003 #ifdef __amd64__ 2004 case SIOCGIFCONF32: 2005 #endif 2006 return (ifconf(cmd, data)); 2007 } 2008 ifr = (struct ifreq *)data; 2009 2010 switch (cmd) { 2011 case SIOCIFCREATE: 2012 case SIOCIFCREATE2: 2013 error = priv_check(td, PRIV_NET_IFCREATE); 2014 if (error) 2015 return (error); 2016 return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name), 2017 cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL)); 2018 case SIOCIFDESTROY: 2019 error = priv_check(td, PRIV_NET_IFDESTROY); 2020 if (error) 2021 return (error); 2022 return if_clone_destroy(ifr->ifr_name); 2023 2024 case SIOCIFGCLONERS: 2025 return (if_clone_list((struct if_clonereq *)data)); 2026 case SIOCGIFGMEMB: 2027 return (if_getgroupmembers((struct ifgroupreq *)data)); 2028 } 2029 2030 ifp = ifunit(ifr->ifr_name); 2031 if (ifp == 0) 2032 return (ENXIO); 2033 2034 error = ifhwioctl(cmd, ifp, data, td); 2035 if (error != ENOIOCTL) 2036 return (error); 2037 2038 oif_flags = ifp->if_flags; 2039 if (so->so_proto == 0) 2040 return (EOPNOTSUPP); 2041 #ifndef COMPAT_43 2042 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, 2043 data, 2044 ifp, td)); 2045 #else 2046 { 2047 int ocmd = cmd; 2048 2049 switch (cmd) { 2050 2051 case SIOCSIFDSTADDR: 2052 case SIOCSIFADDR: 2053 case SIOCSIFBRDADDR: 2054 case SIOCSIFNETMASK: 2055 #if BYTE_ORDER != BIG_ENDIAN 2056 if (ifr->ifr_addr.sa_family == 0 && 2057 ifr->ifr_addr.sa_len < 16) { 2058 ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 2059 ifr->ifr_addr.sa_len = 16; 2060 } 2061 #else 2062 if (ifr->ifr_addr.sa_len == 0) 2063 ifr->ifr_addr.sa_len = 16; 2064 #endif 2065 break; 2066 2067 case OSIOCGIFADDR: 2068 cmd = SIOCGIFADDR; 2069 break; 2070 2071 case OSIOCGIFDSTADDR: 2072 cmd = SIOCGIFDSTADDR; 2073 break; 2074 2075 case OSIOCGIFBRDADDR: 2076 cmd = SIOCGIFBRDADDR; 2077 break; 2078 2079 case OSIOCGIFNETMASK: 2080 cmd = SIOCGIFNETMASK; 2081 } 2082 error = ((*so->so_proto->pr_usrreqs->pru_control)(so, 2083 cmd, 2084 data, 2085 ifp, td)); 2086 switch (ocmd) { 2087 2088 case OSIOCGIFADDR: 2089 case OSIOCGIFDSTADDR: 2090 case OSIOCGIFBRDADDR: 2091 case OSIOCGIFNETMASK: 2092 *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 2093 2094 } 2095 } 2096 #endif /* COMPAT_43 */ 2097 2098 if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 2099 #ifdef INET6 2100 DELAY(100);/* XXX: temporary workaround for fxp issue*/ 2101 if (ifp->if_flags & IFF_UP) { 2102 int s = splimp(); 2103 in6_if_up(ifp); 2104 splx(s); 2105 } 2106 #endif 2107 } 2108 return (error); 2109 } 2110 2111 /* 2112 * The code common to handling reference counted flags, 2113 * e.g., in ifpromisc() and if_allmulti(). 2114 * The "pflag" argument can specify a permanent mode flag to check, 2115 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none. 2116 * 2117 * Only to be used on stack-owned flags, not driver-owned flags. 2118 */ 2119 static int 2120 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch) 2121 { 2122 struct ifreq ifr; 2123 int error; 2124 int oldflags, oldcount; 2125 2126 /* Sanity checks to catch programming errors */ 2127 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0, 2128 ("%s: setting driver-owned flag %d", __func__, flag)); 2129 2130 if (onswitch) 2131 KASSERT(*refcount >= 0, 2132 ("%s: increment negative refcount %d for flag %d", 2133 __func__, *refcount, flag)); 2134 else 2135 KASSERT(*refcount > 0, 2136 ("%s: decrement non-positive refcount %d for flag %d", 2137 __func__, *refcount, flag)); 2138 2139 /* In case this mode is permanent, just touch refcount */ 2140 if (ifp->if_flags & pflag) { 2141 *refcount += onswitch ? 1 : -1; 2142 return (0); 2143 } 2144 2145 /* Save ifnet parameters for if_ioctl() may fail */ 2146 oldcount = *refcount; 2147 oldflags = ifp->if_flags; 2148 2149 /* 2150 * See if we aren't the only and touching refcount is enough. 2151 * Actually toggle interface flag if we are the first or last. 2152 */ 2153 if (onswitch) { 2154 if ((*refcount)++) 2155 return (0); 2156 ifp->if_flags |= flag; 2157 } else { 2158 if (--(*refcount)) 2159 return (0); 2160 ifp->if_flags &= ~flag; 2161 } 2162 2163 /* Call down the driver since we've changed interface flags */ 2164 if (ifp->if_ioctl == NULL) { 2165 error = EOPNOTSUPP; 2166 goto recover; 2167 } 2168 ifr.ifr_flags = ifp->if_flags & 0xffff; 2169 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2170 IFF_LOCKGIANT(ifp); 2171 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2172 IFF_UNLOCKGIANT(ifp); 2173 if (error) 2174 goto recover; 2175 /* Notify userland that interface flags have changed */ 2176 rt_ifmsg(ifp); 2177 return (0); 2178 2179 recover: 2180 /* Recover after driver error */ 2181 *refcount = oldcount; 2182 ifp->if_flags = oldflags; 2183 return (error); 2184 } 2185 2186 /* 2187 * Set/clear promiscuous mode on interface ifp based on the truth value 2188 * of pswitch. The calls are reference counted so that only the first 2189 * "on" request actually has an effect, as does the final "off" request. 2190 * Results are undefined if the "off" and "on" requests are not matched. 2191 */ 2192 int 2193 ifpromisc(struct ifnet *ifp, int pswitch) 2194 { 2195 int error; 2196 int oldflags = ifp->if_flags; 2197 2198 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC, 2199 &ifp->if_pcount, pswitch); 2200 /* If promiscuous mode status has changed, log a message */ 2201 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC)) 2202 log(LOG_INFO, "%s: promiscuous mode %s\n", 2203 ifp->if_xname, 2204 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); 2205 return (error); 2206 } 2207 2208 /* 2209 * Return interface configuration 2210 * of system. List may be used 2211 * in later ioctl's (above) to get 2212 * other information. 2213 */ 2214 /*ARGSUSED*/ 2215 static int 2216 ifconf(u_long cmd, caddr_t data) 2217 { 2218 INIT_VNET_NET(curvnet); 2219 struct ifconf *ifc = (struct ifconf *)data; 2220 #ifdef __amd64__ 2221 struct ifconf32 *ifc32 = (struct ifconf32 *)data; 2222 struct ifconf ifc_swab; 2223 #endif 2224 struct ifnet *ifp; 2225 struct ifaddr *ifa; 2226 struct ifreq ifr; 2227 struct sbuf *sb; 2228 int error, full = 0, valid_len, max_len; 2229 2230 #ifdef __amd64__ 2231 if (cmd == SIOCGIFCONF32) { 2232 ifc_swab.ifc_len = ifc32->ifc_len; 2233 ifc_swab.ifc_buf = (caddr_t)(uintptr_t)ifc32->ifc_buf; 2234 ifc = &ifc_swab; 2235 } 2236 #endif 2237 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */ 2238 max_len = MAXPHYS - 1; 2239 2240 /* Prevent hostile input from being able to crash the system */ 2241 if (ifc->ifc_len <= 0) 2242 return (EINVAL); 2243 2244 again: 2245 if (ifc->ifc_len <= max_len) { 2246 max_len = ifc->ifc_len; 2247 full = 1; 2248 } 2249 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN); 2250 max_len = 0; 2251 valid_len = 0; 2252 2253 IFNET_RLOCK(); /* could sleep XXX */ 2254 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2255 int addrs; 2256 2257 /* 2258 * Zero the ifr_name buffer to make sure we don't 2259 * disclose the contents of the stack. 2260 */ 2261 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name)); 2262 2263 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)) 2264 >= sizeof(ifr.ifr_name)) { 2265 sbuf_delete(sb); 2266 IFNET_RUNLOCK(); 2267 return (ENAMETOOLONG); 2268 } 2269 2270 addrs = 0; 2271 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2272 struct sockaddr *sa = ifa->ifa_addr; 2273 2274 if (prison_if(curthread->td_ucred, sa) != 0) 2275 continue; 2276 addrs++; 2277 #ifdef COMPAT_43 2278 if (cmd == OSIOCGIFCONF) { 2279 struct osockaddr *osa = 2280 (struct osockaddr *)&ifr.ifr_addr; 2281 ifr.ifr_addr = *sa; 2282 osa->sa_family = sa->sa_family; 2283 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2284 max_len += sizeof(ifr); 2285 } else 2286 #endif 2287 if (sa->sa_len <= sizeof(*sa)) { 2288 ifr.ifr_addr = *sa; 2289 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2290 max_len += sizeof(ifr); 2291 } else { 2292 sbuf_bcat(sb, &ifr, 2293 offsetof(struct ifreq, ifr_addr)); 2294 max_len += offsetof(struct ifreq, ifr_addr); 2295 sbuf_bcat(sb, sa, sa->sa_len); 2296 max_len += sa->sa_len; 2297 } 2298 2299 if (!sbuf_overflowed(sb)) 2300 valid_len = sbuf_len(sb); 2301 } 2302 if (addrs == 0) { 2303 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 2304 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2305 max_len += sizeof(ifr); 2306 2307 if (!sbuf_overflowed(sb)) 2308 valid_len = sbuf_len(sb); 2309 } 2310 } 2311 IFNET_RUNLOCK(); 2312 2313 /* 2314 * If we didn't allocate enough space (uncommon), try again. If 2315 * we have already allocated as much space as we are allowed, 2316 * return what we've got. 2317 */ 2318 if (valid_len != max_len && !full) { 2319 sbuf_delete(sb); 2320 goto again; 2321 } 2322 2323 ifc->ifc_len = valid_len; 2324 #ifdef __amd64__ 2325 if (cmd == SIOCGIFCONF32) 2326 ifc32->ifc_len = valid_len; 2327 #endif 2328 sbuf_finish(sb); 2329 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len); 2330 sbuf_delete(sb); 2331 return (error); 2332 } 2333 2334 /* 2335 * Just like ifpromisc(), but for all-multicast-reception mode. 2336 */ 2337 int 2338 if_allmulti(struct ifnet *ifp, int onswitch) 2339 { 2340 2341 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch)); 2342 } 2343 2344 struct ifmultiaddr * 2345 if_findmulti(struct ifnet *ifp, struct sockaddr *sa) 2346 { 2347 struct ifmultiaddr *ifma; 2348 2349 IF_ADDR_LOCK_ASSERT(ifp); 2350 2351 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 2352 if (sa->sa_family == AF_LINK) { 2353 if (sa_dl_equal(ifma->ifma_addr, sa)) 2354 break; 2355 } else { 2356 if (sa_equal(ifma->ifma_addr, sa)) 2357 break; 2358 } 2359 } 2360 2361 return ifma; 2362 } 2363 2364 /* 2365 * Allocate a new ifmultiaddr and initialize based on passed arguments. We 2366 * make copies of passed sockaddrs. The ifmultiaddr will not be added to 2367 * the ifnet multicast address list here, so the caller must do that and 2368 * other setup work (such as notifying the device driver). The reference 2369 * count is initialized to 1. 2370 */ 2371 static struct ifmultiaddr * 2372 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa, 2373 int mflags) 2374 { 2375 struct ifmultiaddr *ifma; 2376 struct sockaddr *dupsa; 2377 2378 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags | 2379 M_ZERO); 2380 if (ifma == NULL) 2381 return (NULL); 2382 2383 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags); 2384 if (dupsa == NULL) { 2385 free(ifma, M_IFMADDR); 2386 return (NULL); 2387 } 2388 bcopy(sa, dupsa, sa->sa_len); 2389 ifma->ifma_addr = dupsa; 2390 2391 ifma->ifma_ifp = ifp; 2392 ifma->ifma_refcount = 1; 2393 ifma->ifma_protospec = NULL; 2394 2395 if (llsa == NULL) { 2396 ifma->ifma_lladdr = NULL; 2397 return (ifma); 2398 } 2399 2400 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags); 2401 if (dupsa == NULL) { 2402 free(ifma->ifma_addr, M_IFMADDR); 2403 free(ifma, M_IFMADDR); 2404 return (NULL); 2405 } 2406 bcopy(llsa, dupsa, llsa->sa_len); 2407 ifma->ifma_lladdr = dupsa; 2408 2409 return (ifma); 2410 } 2411 2412 /* 2413 * if_freemulti: free ifmultiaddr structure and possibly attached related 2414 * addresses. The caller is responsible for implementing reference 2415 * counting, notifying the driver, handling routing messages, and releasing 2416 * any dependent link layer state. 2417 */ 2418 static void 2419 if_freemulti(struct ifmultiaddr *ifma) 2420 { 2421 2422 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d", 2423 ifma->ifma_refcount)); 2424 KASSERT(ifma->ifma_protospec == NULL, 2425 ("if_freemulti: protospec not NULL")); 2426 2427 if (ifma->ifma_lladdr != NULL) 2428 free(ifma->ifma_lladdr, M_IFMADDR); 2429 free(ifma->ifma_addr, M_IFMADDR); 2430 free(ifma, M_IFMADDR); 2431 } 2432 2433 /* 2434 * Register an additional multicast address with a network interface. 2435 * 2436 * - If the address is already present, bump the reference count on the 2437 * address and return. 2438 * - If the address is not link-layer, look up a link layer address. 2439 * - Allocate address structures for one or both addresses, and attach to the 2440 * multicast address list on the interface. If automatically adding a link 2441 * layer address, the protocol address will own a reference to the link 2442 * layer address, to be freed when it is freed. 2443 * - Notify the network device driver of an addition to the multicast address 2444 * list. 2445 * 2446 * 'sa' points to caller-owned memory with the desired multicast address. 2447 * 2448 * 'retifma' will be used to return a pointer to the resulting multicast 2449 * address reference, if desired. 2450 */ 2451 int 2452 if_addmulti(struct ifnet *ifp, struct sockaddr *sa, 2453 struct ifmultiaddr **retifma) 2454 { 2455 struct ifmultiaddr *ifma, *ll_ifma; 2456 struct sockaddr *llsa; 2457 int error; 2458 2459 /* 2460 * If the address is already present, return a new reference to it; 2461 * otherwise, allocate storage and set up a new address. 2462 */ 2463 IF_ADDR_LOCK(ifp); 2464 ifma = if_findmulti(ifp, sa); 2465 if (ifma != NULL) { 2466 ifma->ifma_refcount++; 2467 if (retifma != NULL) 2468 *retifma = ifma; 2469 IF_ADDR_UNLOCK(ifp); 2470 return (0); 2471 } 2472 2473 /* 2474 * The address isn't already present; resolve the protocol address 2475 * into a link layer address, and then look that up, bump its 2476 * refcount or allocate an ifma for that also. If 'llsa' was 2477 * returned, we will need to free it later. 2478 */ 2479 llsa = NULL; 2480 ll_ifma = NULL; 2481 if (ifp->if_resolvemulti != NULL) { 2482 error = ifp->if_resolvemulti(ifp, &llsa, sa); 2483 if (error) 2484 goto unlock_out; 2485 } 2486 2487 /* 2488 * Allocate the new address. Don't hook it up yet, as we may also 2489 * need to allocate a link layer multicast address. 2490 */ 2491 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT); 2492 if (ifma == NULL) { 2493 error = ENOMEM; 2494 goto free_llsa_out; 2495 } 2496 2497 /* 2498 * If a link layer address is found, we'll need to see if it's 2499 * already present in the address list, or allocate is as well. 2500 * When this block finishes, the link layer address will be on the 2501 * list. 2502 */ 2503 if (llsa != NULL) { 2504 ll_ifma = if_findmulti(ifp, llsa); 2505 if (ll_ifma == NULL) { 2506 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT); 2507 if (ll_ifma == NULL) { 2508 --ifma->ifma_refcount; 2509 if_freemulti(ifma); 2510 error = ENOMEM; 2511 goto free_llsa_out; 2512 } 2513 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma, 2514 ifma_link); 2515 } else 2516 ll_ifma->ifma_refcount++; 2517 ifma->ifma_llifma = ll_ifma; 2518 } 2519 2520 /* 2521 * We now have a new multicast address, ifma, and possibly a new or 2522 * referenced link layer address. Add the primary address to the 2523 * ifnet address list. 2524 */ 2525 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 2526 2527 if (retifma != NULL) 2528 *retifma = ifma; 2529 2530 /* 2531 * Must generate the message while holding the lock so that 'ifma' 2532 * pointer is still valid. 2533 */ 2534 rt_newmaddrmsg(RTM_NEWMADDR, ifma); 2535 IF_ADDR_UNLOCK(ifp); 2536 2537 /* 2538 * We are certain we have added something, so call down to the 2539 * interface to let them know about it. 2540 */ 2541 if (ifp->if_ioctl != NULL) { 2542 IFF_LOCKGIANT(ifp); 2543 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0); 2544 IFF_UNLOCKGIANT(ifp); 2545 } 2546 2547 if (llsa != NULL) 2548 free(llsa, M_IFMADDR); 2549 2550 return (0); 2551 2552 free_llsa_out: 2553 if (llsa != NULL) 2554 free(llsa, M_IFMADDR); 2555 2556 unlock_out: 2557 IF_ADDR_UNLOCK(ifp); 2558 return (error); 2559 } 2560 2561 /* 2562 * Delete a multicast group membership by network-layer group address. 2563 * 2564 * Returns ENOENT if the entry could not be found. If ifp no longer 2565 * exists, results are undefined. This entry point should only be used 2566 * from subsystems which do appropriate locking to hold ifp for the 2567 * duration of the call. 2568 * Network-layer protocol domains must use if_delmulti_ifma(). 2569 */ 2570 int 2571 if_delmulti(struct ifnet *ifp, struct sockaddr *sa) 2572 { 2573 struct ifmultiaddr *ifma; 2574 int lastref; 2575 #ifdef INVARIANTS 2576 struct ifnet *oifp; 2577 INIT_VNET_NET(ifp->if_vnet); 2578 2579 IFNET_RLOCK(); 2580 TAILQ_FOREACH(oifp, &V_ifnet, if_link) 2581 if (ifp == oifp) 2582 break; 2583 if (ifp != oifp) 2584 ifp = NULL; 2585 IFNET_RUNLOCK(); 2586 2587 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__)); 2588 #endif 2589 if (ifp == NULL) 2590 return (ENOENT); 2591 2592 IF_ADDR_LOCK(ifp); 2593 lastref = 0; 2594 ifma = if_findmulti(ifp, sa); 2595 if (ifma != NULL) 2596 lastref = if_delmulti_locked(ifp, ifma, 0); 2597 IF_ADDR_UNLOCK(ifp); 2598 2599 if (ifma == NULL) 2600 return (ENOENT); 2601 2602 if (lastref && ifp->if_ioctl != NULL) { 2603 IFF_LOCKGIANT(ifp); 2604 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 2605 IFF_UNLOCKGIANT(ifp); 2606 } 2607 2608 return (0); 2609 } 2610 2611 /* 2612 * Delete a multicast group membership by group membership pointer. 2613 * Network-layer protocol domains must use this routine. 2614 * 2615 * It is safe to call this routine if the ifp disappeared. Callers should 2616 * hold IFF_LOCKGIANT() to avoid a LOR in case the hardware needs to be 2617 * reconfigured. 2618 */ 2619 void 2620 if_delmulti_ifma(struct ifmultiaddr *ifma) 2621 { 2622 #ifdef DIAGNOSTIC 2623 INIT_VNET_NET(curvnet); 2624 #endif 2625 struct ifnet *ifp; 2626 int lastref; 2627 2628 ifp = ifma->ifma_ifp; 2629 #ifdef DIAGNOSTIC 2630 if (ifp == NULL) { 2631 printf("%s: ifma_ifp seems to be detached\n", __func__); 2632 } else { 2633 struct ifnet *oifp; 2634 2635 IFNET_RLOCK(); 2636 TAILQ_FOREACH(oifp, &V_ifnet, if_link) 2637 if (ifp == oifp) 2638 break; 2639 if (ifp != oifp) { 2640 printf("%s: ifnet %p disappeared\n", __func__, ifp); 2641 ifp = NULL; 2642 } 2643 IFNET_RUNLOCK(); 2644 } 2645 #endif 2646 /* 2647 * If and only if the ifnet instance exists: Acquire the address lock. 2648 */ 2649 if (ifp != NULL) 2650 IF_ADDR_LOCK(ifp); 2651 2652 lastref = if_delmulti_locked(ifp, ifma, 0); 2653 2654 if (ifp != NULL) { 2655 /* 2656 * If and only if the ifnet instance exists: 2657 * Release the address lock. 2658 * If the group was left: update the hardware hash filter. 2659 */ 2660 IF_ADDR_UNLOCK(ifp); 2661 if (lastref && ifp->if_ioctl != NULL) { 2662 IFF_LOCKGIANT(ifp); 2663 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 2664 IFF_UNLOCKGIANT(ifp); 2665 } 2666 } 2667 } 2668 2669 /* 2670 * Perform deletion of network-layer and/or link-layer multicast address. 2671 * 2672 * Return 0 if the reference count was decremented. 2673 * Return 1 if the final reference was released, indicating that the 2674 * hardware hash filter should be reprogrammed. 2675 */ 2676 static int 2677 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching) 2678 { 2679 struct ifmultiaddr *ll_ifma; 2680 2681 if (ifp != NULL && ifma->ifma_ifp != NULL) { 2682 KASSERT(ifma->ifma_ifp == ifp, 2683 ("%s: inconsistent ifp %p", __func__, ifp)); 2684 IF_ADDR_LOCK_ASSERT(ifp); 2685 } 2686 2687 ifp = ifma->ifma_ifp; 2688 2689 /* 2690 * If the ifnet is detaching, null out references to ifnet, 2691 * so that upper protocol layers will notice, and not attempt 2692 * to obtain locks for an ifnet which no longer exists. The 2693 * routing socket announcement must happen before the ifnet 2694 * instance is detached from the system. 2695 */ 2696 if (detaching) { 2697 #ifdef DIAGNOSTIC 2698 printf("%s: detaching ifnet instance %p\n", __func__, ifp); 2699 #endif 2700 /* 2701 * ifp may already be nulled out if we are being reentered 2702 * to delete the ll_ifma. 2703 */ 2704 if (ifp != NULL) { 2705 rt_newmaddrmsg(RTM_DELMADDR, ifma); 2706 ifma->ifma_ifp = NULL; 2707 } 2708 } 2709 2710 if (--ifma->ifma_refcount > 0) 2711 return 0; 2712 2713 /* 2714 * If this ifma is a network-layer ifma, a link-layer ifma may 2715 * have been associated with it. Release it first if so. 2716 */ 2717 ll_ifma = ifma->ifma_llifma; 2718 if (ll_ifma != NULL) { 2719 KASSERT(ifma->ifma_lladdr != NULL, 2720 ("%s: llifma w/o lladdr", __func__)); 2721 if (detaching) 2722 ll_ifma->ifma_ifp = NULL; /* XXX */ 2723 if (--ll_ifma->ifma_refcount == 0) { 2724 if (ifp != NULL) { 2725 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, 2726 ifma_link); 2727 } 2728 if_freemulti(ll_ifma); 2729 } 2730 } 2731 2732 if (ifp != NULL) 2733 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 2734 2735 if_freemulti(ifma); 2736 2737 /* 2738 * The last reference to this instance of struct ifmultiaddr 2739 * was released; the hardware should be notified of this change. 2740 */ 2741 return 1; 2742 } 2743 2744 /* 2745 * Set the link layer address on an interface. 2746 * 2747 * At this time we only support certain types of interfaces, 2748 * and we don't allow the length of the address to change. 2749 */ 2750 int 2751 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 2752 { 2753 struct sockaddr_dl *sdl; 2754 struct ifaddr *ifa; 2755 struct ifreq ifr; 2756 2757 ifa = ifp->if_addr; 2758 if (ifa == NULL) 2759 return (EINVAL); 2760 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 2761 if (sdl == NULL) 2762 return (EINVAL); 2763 if (len != sdl->sdl_alen) /* don't allow length to change */ 2764 return (EINVAL); 2765 switch (ifp->if_type) { 2766 case IFT_ETHER: 2767 case IFT_FDDI: 2768 case IFT_XETHER: 2769 case IFT_ISO88025: 2770 case IFT_L2VLAN: 2771 case IFT_BRIDGE: 2772 case IFT_ARCNET: 2773 case IFT_IEEE8023ADLAG: 2774 bcopy(lladdr, LLADDR(sdl), len); 2775 break; 2776 default: 2777 return (ENODEV); 2778 } 2779 /* 2780 * If the interface is already up, we need 2781 * to re-init it in order to reprogram its 2782 * address filter. 2783 */ 2784 if ((ifp->if_flags & IFF_UP) != 0) { 2785 if (ifp->if_ioctl) { 2786 IFF_LOCKGIANT(ifp); 2787 ifp->if_flags &= ~IFF_UP; 2788 ifr.ifr_flags = ifp->if_flags & 0xffff; 2789 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2790 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2791 ifp->if_flags |= IFF_UP; 2792 ifr.ifr_flags = ifp->if_flags & 0xffff; 2793 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2794 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2795 IFF_UNLOCKGIANT(ifp); 2796 } 2797 #ifdef INET 2798 /* 2799 * Also send gratuitous ARPs to notify other nodes about 2800 * the address change. 2801 */ 2802 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2803 if (ifa->ifa_addr->sa_family == AF_INET) 2804 arp_ifinit(ifp, ifa); 2805 } 2806 #endif 2807 } 2808 return (0); 2809 } 2810 2811 /* 2812 * The name argument must be a pointer to storage which will last as 2813 * long as the interface does. For physical devices, the result of 2814 * device_get_name(dev) is a good choice and for pseudo-devices a 2815 * static string works well. 2816 */ 2817 void 2818 if_initname(struct ifnet *ifp, const char *name, int unit) 2819 { 2820 ifp->if_dname = name; 2821 ifp->if_dunit = unit; 2822 if (unit != IF_DUNIT_NONE) 2823 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit); 2824 else 2825 strlcpy(ifp->if_xname, name, IFNAMSIZ); 2826 } 2827 2828 int 2829 if_printf(struct ifnet *ifp, const char * fmt, ...) 2830 { 2831 va_list ap; 2832 int retval; 2833 2834 retval = printf("%s: ", ifp->if_xname); 2835 va_start(ap, fmt); 2836 retval += vprintf(fmt, ap); 2837 va_end(ap); 2838 return (retval); 2839 } 2840 2841 /* 2842 * When an interface is marked IFF_NEEDSGIANT, its if_start() routine cannot 2843 * be called without Giant. However, we often can't acquire the Giant lock 2844 * at those points; instead, we run it via a task queue that holds Giant via 2845 * if_start_deferred. 2846 * 2847 * XXXRW: We need to make sure that the ifnet isn't fully detached until any 2848 * outstanding if_start_deferred() tasks that will run after the free. This 2849 * probably means waiting in if_detach(). 2850 */ 2851 void 2852 if_start(struct ifnet *ifp) 2853 { 2854 2855 if (ifp->if_flags & IFF_NEEDSGIANT) { 2856 if (mtx_owned(&Giant)) 2857 (*(ifp)->if_start)(ifp); 2858 else 2859 taskqueue_enqueue(taskqueue_swi_giant, 2860 &ifp->if_starttask); 2861 } else 2862 (*(ifp)->if_start)(ifp); 2863 } 2864 2865 static void 2866 if_start_deferred(void *context, int pending) 2867 { 2868 struct ifnet *ifp; 2869 2870 GIANT_REQUIRED; 2871 2872 ifp = context; 2873 (ifp->if_start)(ifp); 2874 } 2875 2876 /* 2877 * Backwards compatibility interface for drivers 2878 * that have not implemented it 2879 */ 2880 static int 2881 if_transmit(struct ifnet *ifp, struct mbuf *m) 2882 { 2883 int error; 2884 2885 IFQ_HANDOFF(ifp, m, error); 2886 return (error); 2887 } 2888 2889 int 2890 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust) 2891 { 2892 int active = 0; 2893 2894 IF_LOCK(ifq); 2895 if (_IF_QFULL(ifq)) { 2896 _IF_DROP(ifq); 2897 IF_UNLOCK(ifq); 2898 m_freem(m); 2899 return (0); 2900 } 2901 if (ifp != NULL) { 2902 ifp->if_obytes += m->m_pkthdr.len + adjust; 2903 if (m->m_flags & (M_BCAST|M_MCAST)) 2904 ifp->if_omcasts++; 2905 active = ifp->if_drv_flags & IFF_DRV_OACTIVE; 2906 } 2907 _IF_ENQUEUE(ifq, m); 2908 IF_UNLOCK(ifq); 2909 if (ifp != NULL && !active) 2910 if_start(ifp); 2911 return (1); 2912 } 2913 2914 void 2915 if_register_com_alloc(u_char type, 2916 if_com_alloc_t *a, if_com_free_t *f) 2917 { 2918 2919 KASSERT(if_com_alloc[type] == NULL, 2920 ("if_register_com_alloc: %d already registered", type)); 2921 KASSERT(if_com_free[type] == NULL, 2922 ("if_register_com_alloc: %d free already registered", type)); 2923 2924 if_com_alloc[type] = a; 2925 if_com_free[type] = f; 2926 } 2927 2928 void 2929 if_deregister_com_alloc(u_char type) 2930 { 2931 2932 KASSERT(if_com_alloc[type] != NULL, 2933 ("if_deregister_com_alloc: %d not registered", type)); 2934 KASSERT(if_com_free[type] != NULL, 2935 ("if_deregister_com_alloc: %d free not registered", type)); 2936 if_com_alloc[type] = NULL; 2937 if_com_free[type] = NULL; 2938 } 2939