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