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