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