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