1 /*- 2 * Copyright (c) 1980, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)if.c 8.5 (Berkeley) 1/9/95 30 * $FreeBSD$ 31 */ 32 33 #include "opt_compat.h" 34 #include "opt_inet6.h" 35 #include "opt_inet.h" 36 37 #include <sys/param.h> 38 #include <sys/types.h> 39 #include <sys/conf.h> 40 #include <sys/malloc.h> 41 #include <sys/sbuf.h> 42 #include <sys/bus.h> 43 #include <sys/mbuf.h> 44 #include <sys/systm.h> 45 #include <sys/priv.h> 46 #include <sys/proc.h> 47 #include <sys/socket.h> 48 #include <sys/socketvar.h> 49 #include <sys/protosw.h> 50 #include <sys/kernel.h> 51 #include <sys/lock.h> 52 #include <sys/refcount.h> 53 #include <sys/module.h> 54 #include <sys/rwlock.h> 55 #include <sys/sockio.h> 56 #include <sys/syslog.h> 57 #include <sys/sysctl.h> 58 #include <sys/taskqueue.h> 59 #include <sys/domain.h> 60 #include <sys/jail.h> 61 #include <sys/priv.h> 62 63 #include <machine/stdarg.h> 64 #include <vm/uma.h> 65 66 #include <net/if.h> 67 #include <net/if_arp.h> 68 #include <net/if_clone.h> 69 #include <net/if_dl.h> 70 #include <net/if_types.h> 71 #include <net/if_var.h> 72 #include <net/radix.h> 73 #include <net/route.h> 74 #include <net/vnet.h> 75 76 #if defined(INET) || defined(INET6) 77 /*XXX*/ 78 #include <netinet/in.h> 79 #include <netinet/in_var.h> 80 #include <netinet/ip_carp.h> 81 #ifdef INET6 82 #include <netinet6/in6_var.h> 83 #include <netinet6/in6_ifattach.h> 84 #endif 85 #endif 86 #ifdef INET 87 #include <netinet/if_ether.h> 88 #endif 89 90 #include <security/mac/mac_framework.h> 91 92 #ifdef COMPAT_FREEBSD32 93 #include <sys/mount.h> 94 #include <compat/freebsd32/freebsd32.h> 95 #endif 96 97 struct ifindex_entry { 98 struct ifnet *ife_ifnet; 99 }; 100 101 SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 102 SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); 103 104 TUNABLE_INT("net.link.ifqmaxlen", &ifqmaxlen); 105 SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN, 106 &ifqmaxlen, 0, "max send queue size"); 107 108 /* Log link state change events */ 109 static int log_link_state_change = 1; 110 111 SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW, 112 &log_link_state_change, 0, 113 "log interface link state change events"); 114 115 /* Interface description */ 116 static unsigned int ifdescr_maxlen = 1024; 117 SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW, 118 &ifdescr_maxlen, 0, 119 "administrative maximum length for interface description"); 120 121 static MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions"); 122 123 /* global sx for non-critical path ifdescr */ 124 static struct sx ifdescr_sx; 125 SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr"); 126 127 void (*bstp_linkstate_p)(struct ifnet *ifp, int state); 128 void (*ng_ether_link_state_p)(struct ifnet *ifp, int state); 129 void (*lagg_linkstate_p)(struct ifnet *ifp, int state); 130 /* These are external hooks for CARP. */ 131 void (*carp_linkstate_p)(struct ifnet *ifp); 132 void (*carp_demote_adj_p)(int, char *); 133 #if defined(INET) || defined(INET6) 134 int (*carp_forus_p)(struct ifnet *ifp, u_char *dhost); 135 int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m, 136 struct sockaddr *sa); 137 int (*carp_ioctl_p)(struct ifreq *, u_long, struct thread *); 138 int (*carp_attach_p)(struct ifaddr *, int); 139 void (*carp_detach_p)(struct ifaddr *); 140 #endif 141 #ifdef INET 142 int (*carp_iamatch_p)(struct ifaddr *, uint8_t **); 143 #endif 144 #ifdef INET6 145 struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6); 146 caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m, 147 const struct in6_addr *taddr); 148 #endif 149 150 struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL; 151 152 /* 153 * XXX: Style; these should be sorted alphabetically, and unprototyped 154 * static functions should be prototyped. Currently they are sorted by 155 * declaration order. 156 */ 157 static void if_attachdomain(void *); 158 static void if_attachdomain1(struct ifnet *); 159 static int ifconf(u_long, caddr_t); 160 static void if_freemulti(struct ifmultiaddr *); 161 static void if_init(void *); 162 static void if_grow(void); 163 static void if_route(struct ifnet *, int flag, int fam); 164 static int if_setflag(struct ifnet *, int, int, int *, int); 165 static int if_transmit(struct ifnet *ifp, struct mbuf *m); 166 static void if_unroute(struct ifnet *, int flag, int fam); 167 static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 168 static int if_rtdel(struct radix_node *, void *); 169 static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *); 170 static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int); 171 static void do_link_state_change(void *, int); 172 static int if_getgroup(struct ifgroupreq *, struct ifnet *); 173 static int if_getgroupmembers(struct ifgroupreq *); 174 static void if_delgroups(struct ifnet *); 175 static void if_attach_internal(struct ifnet *, int); 176 static void if_detach_internal(struct ifnet *, int); 177 178 #ifdef INET6 179 /* 180 * XXX: declare here to avoid to include many inet6 related files.. 181 * should be more generalized? 182 */ 183 extern void nd6_setmtu(struct ifnet *); 184 #endif 185 186 VNET_DEFINE(int, if_index); 187 int ifqmaxlen = IFQ_MAXLEN; 188 VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */ 189 VNET_DEFINE(struct ifgrouphead, ifg_head); 190 191 static VNET_DEFINE(int, if_indexlim) = 8; 192 193 /* Table of ifnet by index. */ 194 VNET_DEFINE(struct ifindex_entry *, ifindex_table); 195 196 #define V_if_indexlim VNET(if_indexlim) 197 #define V_ifindex_table VNET(ifindex_table) 198 199 /* 200 * The global network interface list (V_ifnet) and related state (such as 201 * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and 202 * an rwlock. Either may be acquired shared to stablize the list, but both 203 * must be acquired writable to modify the list. This model allows us to 204 * both stablize the interface list during interrupt thread processing, but 205 * also to stablize it over long-running ioctls, without introducing priority 206 * inversions and deadlocks. 207 */ 208 struct rwlock ifnet_rwlock; 209 struct sx ifnet_sxlock; 210 211 /* 212 * The allocation of network interfaces is a rather non-atomic affair; we 213 * need to select an index before we are ready to expose the interface for 214 * use, so will use this pointer value to indicate reservation. 215 */ 216 #define IFNET_HOLD (void *)(uintptr_t)(-1) 217 218 static if_com_alloc_t *if_com_alloc[256]; 219 static if_com_free_t *if_com_free[256]; 220 221 static MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals"); 222 MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 223 MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 224 225 struct ifnet * 226 ifnet_byindex_locked(u_short idx) 227 { 228 229 if (idx > V_if_index) 230 return (NULL); 231 if (V_ifindex_table[idx].ife_ifnet == IFNET_HOLD) 232 return (NULL); 233 return (V_ifindex_table[idx].ife_ifnet); 234 } 235 236 struct ifnet * 237 ifnet_byindex(u_short idx) 238 { 239 struct ifnet *ifp; 240 241 IFNET_RLOCK_NOSLEEP(); 242 ifp = ifnet_byindex_locked(idx); 243 IFNET_RUNLOCK_NOSLEEP(); 244 return (ifp); 245 } 246 247 struct ifnet * 248 ifnet_byindex_ref(u_short idx) 249 { 250 struct ifnet *ifp; 251 252 IFNET_RLOCK_NOSLEEP(); 253 ifp = ifnet_byindex_locked(idx); 254 if (ifp == NULL || (ifp->if_flags & IFF_DYING)) { 255 IFNET_RUNLOCK_NOSLEEP(); 256 return (NULL); 257 } 258 if_ref(ifp); 259 IFNET_RUNLOCK_NOSLEEP(); 260 return (ifp); 261 } 262 263 /* 264 * Allocate an ifindex array entry; return 0 on success or an error on 265 * failure. 266 */ 267 static int 268 ifindex_alloc_locked(u_short *idxp) 269 { 270 u_short idx; 271 272 IFNET_WLOCK_ASSERT(); 273 274 retry: 275 /* 276 * Try to find an empty slot below V_if_index. If we fail, take the 277 * next slot. 278 */ 279 for (idx = 1; idx <= V_if_index; idx++) { 280 if (V_ifindex_table[idx].ife_ifnet == NULL) 281 break; 282 } 283 284 /* Catch if_index overflow. */ 285 if (idx < 1) 286 return (ENOSPC); 287 if (idx >= V_if_indexlim) { 288 if_grow(); 289 goto retry; 290 } 291 if (idx > V_if_index) 292 V_if_index = idx; 293 *idxp = idx; 294 return (0); 295 } 296 297 static void 298 ifindex_free_locked(u_short idx) 299 { 300 301 IFNET_WLOCK_ASSERT(); 302 303 V_ifindex_table[idx].ife_ifnet = NULL; 304 while (V_if_index > 0 && 305 V_ifindex_table[V_if_index].ife_ifnet == NULL) 306 V_if_index--; 307 } 308 309 static void 310 ifindex_free(u_short idx) 311 { 312 313 IFNET_WLOCK(); 314 ifindex_free_locked(idx); 315 IFNET_WUNLOCK(); 316 } 317 318 static void 319 ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp) 320 { 321 322 IFNET_WLOCK_ASSERT(); 323 324 V_ifindex_table[idx].ife_ifnet = ifp; 325 } 326 327 static void 328 ifnet_setbyindex(u_short idx, struct ifnet *ifp) 329 { 330 331 IFNET_WLOCK(); 332 ifnet_setbyindex_locked(idx, ifp); 333 IFNET_WUNLOCK(); 334 } 335 336 struct ifaddr * 337 ifaddr_byindex(u_short idx) 338 { 339 struct ifaddr *ifa; 340 341 IFNET_RLOCK_NOSLEEP(); 342 ifa = ifnet_byindex_locked(idx)->if_addr; 343 if (ifa != NULL) 344 ifa_ref(ifa); 345 IFNET_RUNLOCK_NOSLEEP(); 346 return (ifa); 347 } 348 349 /* 350 * Network interface utility routines. 351 * 352 * Routines with ifa_ifwith* names take sockaddr *'s as 353 * parameters. 354 */ 355 356 static void 357 vnet_if_init(const void *unused __unused) 358 { 359 360 TAILQ_INIT(&V_ifnet); 361 TAILQ_INIT(&V_ifg_head); 362 IFNET_WLOCK(); 363 if_grow(); /* create initial table */ 364 IFNET_WUNLOCK(); 365 vnet_if_clone_init(); 366 } 367 VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init, 368 NULL); 369 370 /* ARGSUSED*/ 371 static void 372 if_init(void *dummy __unused) 373 { 374 375 IFNET_LOCK_INIT(); 376 if_clone_init(); 377 } 378 SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL); 379 380 381 #ifdef VIMAGE 382 static void 383 vnet_if_uninit(const void *unused __unused) 384 { 385 386 VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p " 387 "not empty", __func__, __LINE__, &V_ifnet)); 388 VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p " 389 "not empty", __func__, __LINE__, &V_ifg_head)); 390 391 free((caddr_t)V_ifindex_table, M_IFNET); 392 } 393 VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST, 394 vnet_if_uninit, NULL); 395 #endif 396 397 static void 398 if_grow(void) 399 { 400 int oldlim; 401 u_int n; 402 struct ifindex_entry *e; 403 404 IFNET_WLOCK_ASSERT(); 405 oldlim = V_if_indexlim; 406 IFNET_WUNLOCK(); 407 n = (oldlim << 1) * sizeof(*e); 408 e = malloc(n, M_IFNET, M_WAITOK | M_ZERO); 409 IFNET_WLOCK(); 410 if (V_if_indexlim != oldlim) { 411 free(e, M_IFNET); 412 return; 413 } 414 if (V_ifindex_table != NULL) { 415 memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2); 416 free((caddr_t)V_ifindex_table, M_IFNET); 417 } 418 V_if_indexlim <<= 1; 419 V_ifindex_table = e; 420 } 421 422 /* 423 * Allocate a struct ifnet and an index for an interface. A layer 2 424 * common structure will also be allocated if an allocation routine is 425 * registered for the passed type. 426 */ 427 struct ifnet * 428 if_alloc(u_char type) 429 { 430 struct ifnet *ifp; 431 u_short idx; 432 433 ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO); 434 IFNET_WLOCK(); 435 if (ifindex_alloc_locked(&idx) != 0) { 436 IFNET_WUNLOCK(); 437 free(ifp, M_IFNET); 438 return (NULL); 439 } 440 ifnet_setbyindex_locked(idx, IFNET_HOLD); 441 IFNET_WUNLOCK(); 442 ifp->if_index = idx; 443 ifp->if_type = type; 444 ifp->if_alloctype = type; 445 if (if_com_alloc[type] != NULL) { 446 ifp->if_l2com = if_com_alloc[type](type, ifp); 447 if (ifp->if_l2com == NULL) { 448 free(ifp, M_IFNET); 449 ifindex_free(idx); 450 return (NULL); 451 } 452 } 453 454 IF_ADDR_LOCK_INIT(ifp); 455 TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp); 456 ifp->if_afdata_initialized = 0; 457 IF_AFDATA_LOCK_INIT(ifp); 458 TAILQ_INIT(&ifp->if_addrhead); 459 TAILQ_INIT(&ifp->if_prefixhead); 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 int s; 696 697 s = splnet(); 698 TAILQ_FOREACH(ifp, &V_ifnet, if_link) 699 if_attachdomain1(ifp); 700 splx(s); 701 } 702 SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND, 703 if_attachdomain, NULL); 704 705 static void 706 if_attachdomain1(struct ifnet *ifp) 707 { 708 struct domain *dp; 709 int s; 710 711 s = splnet(); 712 713 /* 714 * Since dp->dom_ifattach calls malloc() with M_WAITOK, we 715 * cannot lock ifp->if_afdata initialization, entirely. 716 */ 717 if (IF_AFDATA_TRYLOCK(ifp) == 0) { 718 splx(s); 719 return; 720 } 721 if (ifp->if_afdata_initialized >= domain_init_status) { 722 IF_AFDATA_UNLOCK(ifp); 723 splx(s); 724 printf("if_attachdomain called more than once on %s\n", 725 ifp->if_xname); 726 return; 727 } 728 ifp->if_afdata_initialized = domain_init_status; 729 IF_AFDATA_UNLOCK(ifp); 730 731 /* address family dependent data region */ 732 bzero(ifp->if_afdata, sizeof(ifp->if_afdata)); 733 for (dp = domains; dp; dp = dp->dom_next) { 734 if (dp->dom_ifattach) 735 ifp->if_afdata[dp->dom_family] = 736 (*dp->dom_ifattach)(ifp); 737 } 738 739 splx(s); 740 } 741 742 /* 743 * Remove any unicast or broadcast network addresses from an interface. 744 */ 745 void 746 if_purgeaddrs(struct ifnet *ifp) 747 { 748 struct ifaddr *ifa, *next; 749 750 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) { 751 if (ifa->ifa_addr->sa_family == AF_LINK) 752 continue; 753 #ifdef INET 754 /* XXX: Ugly!! ad hoc just for INET */ 755 if (ifa->ifa_addr->sa_family == AF_INET) { 756 struct ifaliasreq ifr; 757 758 bzero(&ifr, sizeof(ifr)); 759 ifr.ifra_addr = *ifa->ifa_addr; 760 if (ifa->ifa_dstaddr) 761 ifr.ifra_broadaddr = *ifa->ifa_dstaddr; 762 if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp, 763 NULL) == 0) 764 continue; 765 } 766 #endif /* INET */ 767 #ifdef INET6 768 if (ifa->ifa_addr->sa_family == AF_INET6) { 769 in6_purgeaddr(ifa); 770 /* ifp_addrhead is already updated */ 771 continue; 772 } 773 #endif /* INET6 */ 774 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 775 ifa_free(ifa); 776 } 777 } 778 779 /* 780 * Remove any multicast network addresses from an interface when an ifnet 781 * is going away. 782 */ 783 static void 784 if_purgemaddrs(struct ifnet *ifp) 785 { 786 struct ifmultiaddr *ifma; 787 struct ifmultiaddr *next; 788 789 IF_ADDR_WLOCK(ifp); 790 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) 791 if_delmulti_locked(ifp, ifma, 1); 792 IF_ADDR_WUNLOCK(ifp); 793 } 794 795 /* 796 * Detach an interface, removing it from the list of "active" interfaces. 797 * If vmove flag is set on entry to if_detach_internal(), perform only a 798 * limited subset of cleanup tasks, given that we are moving an ifnet from 799 * one vnet to another, where it must be fully operational. 800 * 801 * XXXRW: There are some significant questions about event ordering, and 802 * how to prevent things from starting to use the interface during detach. 803 */ 804 void 805 if_detach(struct ifnet *ifp) 806 { 807 808 if_detach_internal(ifp, 0); 809 } 810 811 static void 812 if_detach_internal(struct ifnet *ifp, int vmove) 813 { 814 struct ifaddr *ifa; 815 struct radix_node_head *rnh; 816 int i, j; 817 struct domain *dp; 818 struct ifnet *iter; 819 int found = 0; 820 821 IFNET_WLOCK(); 822 TAILQ_FOREACH(iter, &V_ifnet, if_link) 823 if (iter == ifp) { 824 TAILQ_REMOVE(&V_ifnet, ifp, if_link); 825 found = 1; 826 break; 827 } 828 #ifdef VIMAGE 829 if (found) 830 curvnet->vnet_ifcnt--; 831 #endif 832 IFNET_WUNLOCK(); 833 if (!found) { 834 if (vmove) 835 panic("%s: ifp=%p not on the ifnet tailq %p", 836 __func__, ifp, &V_ifnet); 837 else 838 return; /* XXX this should panic as well? */ 839 } 840 841 /* 842 * Remove/wait for pending events. 843 */ 844 taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 845 846 /* 847 * Remove routes and flush queues. 848 */ 849 if_down(ifp); 850 #ifdef ALTQ 851 if (ALTQ_IS_ENABLED(&ifp->if_snd)) 852 altq_disable(&ifp->if_snd); 853 if (ALTQ_IS_ATTACHED(&ifp->if_snd)) 854 altq_detach(&ifp->if_snd); 855 #endif 856 857 if_purgeaddrs(ifp); 858 859 #ifdef INET 860 in_ifdetach(ifp); 861 #endif 862 863 #ifdef INET6 864 /* 865 * Remove all IPv6 kernel structs related to ifp. This should be done 866 * before removing routing entries below, since IPv6 interface direct 867 * routes are expected to be removed by the IPv6-specific kernel API. 868 * Otherwise, the kernel will detect some inconsistency and bark it. 869 */ 870 in6_ifdetach(ifp); 871 #endif 872 if_purgemaddrs(ifp); 873 874 if (!vmove) { 875 /* 876 * Prevent further calls into the device driver via ifnet. 877 */ 878 if_dead(ifp); 879 880 /* 881 * Remove link ifaddr pointer and maybe decrement if_index. 882 * Clean up all addresses. 883 */ 884 ifp->if_addr = NULL; 885 886 /* We can now free link ifaddr. */ 887 if (!TAILQ_EMPTY(&ifp->if_addrhead)) { 888 ifa = TAILQ_FIRST(&ifp->if_addrhead); 889 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 890 ifa_free(ifa); 891 } 892 } 893 894 /* 895 * Delete all remaining routes using this interface 896 * Unfortuneatly the only way to do this is to slog through 897 * the entire routing table looking for routes which point 898 * to this interface...oh well... 899 */ 900 for (i = 1; i <= AF_MAX; i++) { 901 for (j = 0; j < rt_numfibs; j++) { 902 rnh = rt_tables_get_rnh(j, i); 903 if (rnh == NULL) 904 continue; 905 RADIX_NODE_HEAD_LOCK(rnh); 906 (void) rnh->rnh_walktree(rnh, if_rtdel, ifp); 907 RADIX_NODE_HEAD_UNLOCK(rnh); 908 } 909 } 910 911 /* Announce that the interface is gone. */ 912 rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 913 EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 914 if (IS_DEFAULT_VNET(curvnet)) 915 devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL); 916 if_delgroups(ifp); 917 918 /* 919 * We cannot hold the lock over dom_ifdetach calls as they might 920 * sleep, for example trying to drain a callout, thus open up the 921 * theoretical race with re-attaching. 922 */ 923 IF_AFDATA_LOCK(ifp); 924 i = ifp->if_afdata_initialized; 925 ifp->if_afdata_initialized = 0; 926 IF_AFDATA_UNLOCK(ifp); 927 for (dp = domains; i > 0 && dp; dp = dp->dom_next) { 928 if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) 929 (*dp->dom_ifdetach)(ifp, 930 ifp->if_afdata[dp->dom_family]); 931 } 932 } 933 934 #ifdef VIMAGE 935 /* 936 * if_vmove() performs a limited version of if_detach() in current 937 * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg. 938 * An attempt is made to shrink if_index in current vnet, find an 939 * unused if_index in target vnet and calls if_grow() if necessary, 940 * and finally find an unused if_xname for the target vnet. 941 */ 942 void 943 if_vmove(struct ifnet *ifp, struct vnet *new_vnet) 944 { 945 u_short idx; 946 947 /* 948 * Detach from current vnet, but preserve LLADDR info, do not 949 * mark as dead etc. so that the ifnet can be reattached later. 950 */ 951 if_detach_internal(ifp, 1); 952 953 /* 954 * Unlink the ifnet from ifindex_table[] in current vnet, and shrink 955 * the if_index for that vnet if possible. 956 * 957 * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized, 958 * or we'd lock on one vnet and unlock on another. 959 */ 960 IFNET_WLOCK(); 961 ifindex_free_locked(ifp->if_index); 962 IFNET_WUNLOCK(); 963 964 /* 965 * Perform interface-specific reassignment tasks, if provided by 966 * the driver. 967 */ 968 if (ifp->if_reassign != NULL) 969 ifp->if_reassign(ifp, new_vnet, NULL); 970 971 /* 972 * Switch to the context of the target vnet. 973 */ 974 CURVNET_SET_QUIET(new_vnet); 975 976 IFNET_WLOCK(); 977 if (ifindex_alloc_locked(&idx) != 0) { 978 IFNET_WUNLOCK(); 979 panic("if_index overflow"); 980 } 981 ifp->if_index = idx; 982 ifnet_setbyindex_locked(ifp->if_index, ifp); 983 IFNET_WUNLOCK(); 984 985 if_attach_internal(ifp, 1); 986 987 CURVNET_RESTORE(); 988 } 989 990 /* 991 * Move an ifnet to or from another child prison/vnet, specified by the jail id. 992 */ 993 static int 994 if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid) 995 { 996 struct prison *pr; 997 struct ifnet *difp; 998 999 /* Try to find the prison within our visibility. */ 1000 sx_slock(&allprison_lock); 1001 pr = prison_find_child(td->td_ucred->cr_prison, jid); 1002 sx_sunlock(&allprison_lock); 1003 if (pr == NULL) 1004 return (ENXIO); 1005 prison_hold_locked(pr); 1006 mtx_unlock(&pr->pr_mtx); 1007 1008 /* Do not try to move the iface from and to the same prison. */ 1009 if (pr->pr_vnet == ifp->if_vnet) { 1010 prison_free(pr); 1011 return (EEXIST); 1012 } 1013 1014 /* Make sure the named iface does not exists in the dst. prison/vnet. */ 1015 /* XXX Lock interfaces to avoid races. */ 1016 CURVNET_SET_QUIET(pr->pr_vnet); 1017 difp = ifunit(ifname); 1018 CURVNET_RESTORE(); 1019 if (difp != NULL) { 1020 prison_free(pr); 1021 return (EEXIST); 1022 } 1023 1024 /* Move the interface into the child jail/vnet. */ 1025 if_vmove(ifp, pr->pr_vnet); 1026 1027 /* Report the new if_xname back to the userland. */ 1028 sprintf(ifname, "%s", ifp->if_xname); 1029 1030 prison_free(pr); 1031 return (0); 1032 } 1033 1034 static int 1035 if_vmove_reclaim(struct thread *td, char *ifname, int jid) 1036 { 1037 struct prison *pr; 1038 struct vnet *vnet_dst; 1039 struct ifnet *ifp; 1040 1041 /* Try to find the prison within our visibility. */ 1042 sx_slock(&allprison_lock); 1043 pr = prison_find_child(td->td_ucred->cr_prison, jid); 1044 sx_sunlock(&allprison_lock); 1045 if (pr == NULL) 1046 return (ENXIO); 1047 prison_hold_locked(pr); 1048 mtx_unlock(&pr->pr_mtx); 1049 1050 /* Make sure the named iface exists in the source prison/vnet. */ 1051 CURVNET_SET(pr->pr_vnet); 1052 ifp = ifunit(ifname); /* XXX Lock to avoid races. */ 1053 if (ifp == NULL) { 1054 CURVNET_RESTORE(); 1055 prison_free(pr); 1056 return (ENXIO); 1057 } 1058 1059 /* Do not try to move the iface from and to the same prison. */ 1060 vnet_dst = TD_TO_VNET(td); 1061 if (vnet_dst == ifp->if_vnet) { 1062 CURVNET_RESTORE(); 1063 prison_free(pr); 1064 return (EEXIST); 1065 } 1066 1067 /* Get interface back from child jail/vnet. */ 1068 if_vmove(ifp, vnet_dst); 1069 CURVNET_RESTORE(); 1070 1071 /* Report the new if_xname back to the userland. */ 1072 sprintf(ifname, "%s", ifp->if_xname); 1073 1074 prison_free(pr); 1075 return (0); 1076 } 1077 #endif /* VIMAGE */ 1078 1079 /* 1080 * Add a group to an interface 1081 */ 1082 int 1083 if_addgroup(struct ifnet *ifp, const char *groupname) 1084 { 1085 struct ifg_list *ifgl; 1086 struct ifg_group *ifg = NULL; 1087 struct ifg_member *ifgm; 1088 1089 if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' && 1090 groupname[strlen(groupname) - 1] <= '9') 1091 return (EINVAL); 1092 1093 IFNET_WLOCK(); 1094 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 1095 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) { 1096 IFNET_WUNLOCK(); 1097 return (EEXIST); 1098 } 1099 1100 if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP, 1101 M_NOWAIT)) == NULL) { 1102 IFNET_WUNLOCK(); 1103 return (ENOMEM); 1104 } 1105 1106 if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member), 1107 M_TEMP, M_NOWAIT)) == NULL) { 1108 free(ifgl, M_TEMP); 1109 IFNET_WUNLOCK(); 1110 return (ENOMEM); 1111 } 1112 1113 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 1114 if (!strcmp(ifg->ifg_group, groupname)) 1115 break; 1116 1117 if (ifg == NULL) { 1118 if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group), 1119 M_TEMP, M_NOWAIT)) == NULL) { 1120 free(ifgl, M_TEMP); 1121 free(ifgm, M_TEMP); 1122 IFNET_WUNLOCK(); 1123 return (ENOMEM); 1124 } 1125 strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group)); 1126 ifg->ifg_refcnt = 0; 1127 TAILQ_INIT(&ifg->ifg_members); 1128 EVENTHANDLER_INVOKE(group_attach_event, ifg); 1129 TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next); 1130 } 1131 1132 ifg->ifg_refcnt++; 1133 ifgl->ifgl_group = ifg; 1134 ifgm->ifgm_ifp = ifp; 1135 1136 IF_ADDR_WLOCK(ifp); 1137 TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next); 1138 TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next); 1139 IF_ADDR_WUNLOCK(ifp); 1140 1141 IFNET_WUNLOCK(); 1142 1143 EVENTHANDLER_INVOKE(group_change_event, groupname); 1144 1145 return (0); 1146 } 1147 1148 /* 1149 * Remove a group from an interface 1150 */ 1151 int 1152 if_delgroup(struct ifnet *ifp, const char *groupname) 1153 { 1154 struct ifg_list *ifgl; 1155 struct ifg_member *ifgm; 1156 1157 IFNET_WLOCK(); 1158 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 1159 if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) 1160 break; 1161 if (ifgl == NULL) { 1162 IFNET_WUNLOCK(); 1163 return (ENOENT); 1164 } 1165 1166 IF_ADDR_WLOCK(ifp); 1167 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next); 1168 IF_ADDR_WUNLOCK(ifp); 1169 1170 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) 1171 if (ifgm->ifgm_ifp == ifp) 1172 break; 1173 1174 if (ifgm != NULL) { 1175 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next); 1176 free(ifgm, M_TEMP); 1177 } 1178 1179 if (--ifgl->ifgl_group->ifg_refcnt == 0) { 1180 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next); 1181 EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group); 1182 free(ifgl->ifgl_group, M_TEMP); 1183 } 1184 IFNET_WUNLOCK(); 1185 1186 free(ifgl, M_TEMP); 1187 1188 EVENTHANDLER_INVOKE(group_change_event, groupname); 1189 1190 return (0); 1191 } 1192 1193 /* 1194 * Remove an interface from all groups 1195 */ 1196 static void 1197 if_delgroups(struct ifnet *ifp) 1198 { 1199 struct ifg_list *ifgl; 1200 struct ifg_member *ifgm; 1201 char groupname[IFNAMSIZ]; 1202 1203 IFNET_WLOCK(); 1204 while (!TAILQ_EMPTY(&ifp->if_groups)) { 1205 ifgl = TAILQ_FIRST(&ifp->if_groups); 1206 1207 strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ); 1208 1209 IF_ADDR_WLOCK(ifp); 1210 TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next); 1211 IF_ADDR_WUNLOCK(ifp); 1212 1213 TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) 1214 if (ifgm->ifgm_ifp == ifp) 1215 break; 1216 1217 if (ifgm != NULL) { 1218 TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, 1219 ifgm_next); 1220 free(ifgm, M_TEMP); 1221 } 1222 1223 if (--ifgl->ifgl_group->ifg_refcnt == 0) { 1224 TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next); 1225 EVENTHANDLER_INVOKE(group_detach_event, 1226 ifgl->ifgl_group); 1227 free(ifgl->ifgl_group, M_TEMP); 1228 } 1229 IFNET_WUNLOCK(); 1230 1231 free(ifgl, M_TEMP); 1232 1233 EVENTHANDLER_INVOKE(group_change_event, groupname); 1234 1235 IFNET_WLOCK(); 1236 } 1237 IFNET_WUNLOCK(); 1238 } 1239 1240 /* 1241 * Stores all groups from an interface in memory pointed 1242 * to by data 1243 */ 1244 static int 1245 if_getgroup(struct ifgroupreq *data, struct ifnet *ifp) 1246 { 1247 int len, error; 1248 struct ifg_list *ifgl; 1249 struct ifg_req ifgrq, *ifgp; 1250 struct ifgroupreq *ifgr = data; 1251 1252 if (ifgr->ifgr_len == 0) { 1253 IF_ADDR_RLOCK(ifp); 1254 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 1255 ifgr->ifgr_len += sizeof(struct ifg_req); 1256 IF_ADDR_RUNLOCK(ifp); 1257 return (0); 1258 } 1259 1260 len = ifgr->ifgr_len; 1261 ifgp = ifgr->ifgr_groups; 1262 /* XXX: wire */ 1263 IF_ADDR_RLOCK(ifp); 1264 TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) { 1265 if (len < sizeof(ifgrq)) { 1266 IF_ADDR_RUNLOCK(ifp); 1267 return (EINVAL); 1268 } 1269 bzero(&ifgrq, sizeof ifgrq); 1270 strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group, 1271 sizeof(ifgrq.ifgrq_group)); 1272 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 1273 IF_ADDR_RUNLOCK(ifp); 1274 return (error); 1275 } 1276 len -= sizeof(ifgrq); 1277 ifgp++; 1278 } 1279 IF_ADDR_RUNLOCK(ifp); 1280 1281 return (0); 1282 } 1283 1284 /* 1285 * Stores all members of a group in memory pointed to by data 1286 */ 1287 static int 1288 if_getgroupmembers(struct ifgroupreq *data) 1289 { 1290 struct ifgroupreq *ifgr = data; 1291 struct ifg_group *ifg; 1292 struct ifg_member *ifgm; 1293 struct ifg_req ifgrq, *ifgp; 1294 int len, error; 1295 1296 IFNET_RLOCK(); 1297 TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 1298 if (!strcmp(ifg->ifg_group, ifgr->ifgr_name)) 1299 break; 1300 if (ifg == NULL) { 1301 IFNET_RUNLOCK(); 1302 return (ENOENT); 1303 } 1304 1305 if (ifgr->ifgr_len == 0) { 1306 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) 1307 ifgr->ifgr_len += sizeof(ifgrq); 1308 IFNET_RUNLOCK(); 1309 return (0); 1310 } 1311 1312 len = ifgr->ifgr_len; 1313 ifgp = ifgr->ifgr_groups; 1314 TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) { 1315 if (len < sizeof(ifgrq)) { 1316 IFNET_RUNLOCK(); 1317 return (EINVAL); 1318 } 1319 bzero(&ifgrq, sizeof ifgrq); 1320 strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname, 1321 sizeof(ifgrq.ifgrq_member)); 1322 if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 1323 IFNET_RUNLOCK(); 1324 return (error); 1325 } 1326 len -= sizeof(ifgrq); 1327 ifgp++; 1328 } 1329 IFNET_RUNLOCK(); 1330 1331 return (0); 1332 } 1333 1334 /* 1335 * Delete Routes for a Network Interface 1336 * 1337 * Called for each routing entry via the rnh->rnh_walktree() call above 1338 * to delete all route entries referencing a detaching network interface. 1339 * 1340 * Arguments: 1341 * rn pointer to node in the routing table 1342 * arg argument passed to rnh->rnh_walktree() - detaching interface 1343 * 1344 * Returns: 1345 * 0 successful 1346 * errno failed - reason indicated 1347 * 1348 */ 1349 static int 1350 if_rtdel(struct radix_node *rn, void *arg) 1351 { 1352 struct rtentry *rt = (struct rtentry *)rn; 1353 struct ifnet *ifp = arg; 1354 int err; 1355 1356 if (rt->rt_ifp == ifp) { 1357 1358 /* 1359 * Protect (sorta) against walktree recursion problems 1360 * with cloned routes 1361 */ 1362 if ((rt->rt_flags & RTF_UP) == 0) 1363 return (0); 1364 1365 err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway, 1366 rt_mask(rt), rt->rt_flags|RTF_RNH_LOCKED, 1367 (struct rtentry **) NULL, rt->rt_fibnum); 1368 if (err) { 1369 log(LOG_WARNING, "if_rtdel: error %d\n", err); 1370 } 1371 } 1372 1373 return (0); 1374 } 1375 1376 /* 1377 * Wrapper functions for struct ifnet address list locking macros. These are 1378 * used by kernel modules to avoid encoding programming interface or binary 1379 * interface assumptions that may be violated when kernel-internal locking 1380 * approaches change. 1381 */ 1382 void 1383 if_addr_rlock(struct ifnet *ifp) 1384 { 1385 1386 IF_ADDR_RLOCK(ifp); 1387 } 1388 1389 void 1390 if_addr_runlock(struct ifnet *ifp) 1391 { 1392 1393 IF_ADDR_RUNLOCK(ifp); 1394 } 1395 1396 void 1397 if_maddr_rlock(struct ifnet *ifp) 1398 { 1399 1400 IF_ADDR_RLOCK(ifp); 1401 } 1402 1403 void 1404 if_maddr_runlock(struct ifnet *ifp) 1405 { 1406 1407 IF_ADDR_RUNLOCK(ifp); 1408 } 1409 1410 /* 1411 * Reference count functions for ifaddrs. 1412 */ 1413 void 1414 ifa_init(struct ifaddr *ifa) 1415 { 1416 1417 mtx_init(&ifa->ifa_mtx, "ifaddr", NULL, MTX_DEF); 1418 refcount_init(&ifa->ifa_refcnt, 1); 1419 } 1420 1421 void 1422 ifa_ref(struct ifaddr *ifa) 1423 { 1424 1425 refcount_acquire(&ifa->ifa_refcnt); 1426 } 1427 1428 void 1429 ifa_free(struct ifaddr *ifa) 1430 { 1431 1432 if (refcount_release(&ifa->ifa_refcnt)) { 1433 mtx_destroy(&ifa->ifa_mtx); 1434 free(ifa, M_IFADDR); 1435 } 1436 } 1437 1438 int 1439 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia) 1440 { 1441 int error = 0; 1442 struct rtentry *rt = NULL; 1443 struct rt_addrinfo info; 1444 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 1445 1446 bzero(&info, sizeof(info)); 1447 info.rti_ifp = V_loif; 1448 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC; 1449 info.rti_info[RTAX_DST] = ia; 1450 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl; 1451 error = rtrequest1_fib(RTM_ADD, &info, &rt, 0); 1452 1453 if (error == 0 && rt != NULL) { 1454 RT_LOCK(rt); 1455 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type = 1456 ifa->ifa_ifp->if_type; 1457 ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index = 1458 ifa->ifa_ifp->if_index; 1459 RT_REMREF(rt); 1460 RT_UNLOCK(rt); 1461 } else if (error != 0) 1462 log(LOG_DEBUG, "%s: insertion failed: %u\n", __func__, error); 1463 1464 return (error); 1465 } 1466 1467 int 1468 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia) 1469 { 1470 int error = 0; 1471 struct rt_addrinfo info; 1472 struct sockaddr_dl null_sdl; 1473 1474 bzero(&null_sdl, sizeof(null_sdl)); 1475 null_sdl.sdl_len = sizeof(null_sdl); 1476 null_sdl.sdl_family = AF_LINK; 1477 null_sdl.sdl_type = ifa->ifa_ifp->if_type; 1478 null_sdl.sdl_index = ifa->ifa_ifp->if_index; 1479 bzero(&info, sizeof(info)); 1480 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC; 1481 info.rti_info[RTAX_DST] = ia; 1482 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl; 1483 error = rtrequest1_fib(RTM_DELETE, &info, NULL, 0); 1484 1485 if (error != 0) 1486 log(LOG_DEBUG, "%s: deletion failed: %u\n", __func__, error); 1487 1488 return (error); 1489 } 1490 1491 /* 1492 * XXX: Because sockaddr_dl has deeper structure than the sockaddr 1493 * structs used to represent other address families, it is necessary 1494 * to perform a different comparison. 1495 */ 1496 1497 #define sa_equal(a1, a2) \ 1498 (bcmp((a1), (a2), ((a1))->sa_len) == 0) 1499 1500 #define sa_dl_equal(a1, a2) \ 1501 ((((struct sockaddr_dl *)(a1))->sdl_len == \ 1502 ((struct sockaddr_dl *)(a2))->sdl_len) && \ 1503 (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \ 1504 LLADDR((struct sockaddr_dl *)(a2)), \ 1505 ((struct sockaddr_dl *)(a1))->sdl_alen) == 0)) 1506 1507 /* 1508 * Locate an interface based on a complete address. 1509 */ 1510 /*ARGSUSED*/ 1511 static struct ifaddr * 1512 ifa_ifwithaddr_internal(struct sockaddr *addr, int getref) 1513 { 1514 struct ifnet *ifp; 1515 struct ifaddr *ifa; 1516 1517 IFNET_RLOCK_NOSLEEP(); 1518 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1519 IF_ADDR_RLOCK(ifp); 1520 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1521 if (ifa->ifa_addr->sa_family != addr->sa_family) 1522 continue; 1523 if (sa_equal(addr, ifa->ifa_addr)) { 1524 if (getref) 1525 ifa_ref(ifa); 1526 IF_ADDR_RUNLOCK(ifp); 1527 goto done; 1528 } 1529 /* IP6 doesn't have broadcast */ 1530 if ((ifp->if_flags & IFF_BROADCAST) && 1531 ifa->ifa_broadaddr && 1532 ifa->ifa_broadaddr->sa_len != 0 && 1533 sa_equal(ifa->ifa_broadaddr, addr)) { 1534 if (getref) 1535 ifa_ref(ifa); 1536 IF_ADDR_RUNLOCK(ifp); 1537 goto done; 1538 } 1539 } 1540 IF_ADDR_RUNLOCK(ifp); 1541 } 1542 ifa = NULL; 1543 done: 1544 IFNET_RUNLOCK_NOSLEEP(); 1545 return (ifa); 1546 } 1547 1548 struct ifaddr * 1549 ifa_ifwithaddr(struct sockaddr *addr) 1550 { 1551 1552 return (ifa_ifwithaddr_internal(addr, 1)); 1553 } 1554 1555 int 1556 ifa_ifwithaddr_check(struct sockaddr *addr) 1557 { 1558 1559 return (ifa_ifwithaddr_internal(addr, 0) != NULL); 1560 } 1561 1562 /* 1563 * Locate an interface based on the broadcast address. 1564 */ 1565 /* ARGSUSED */ 1566 struct ifaddr * 1567 ifa_ifwithbroadaddr(struct sockaddr *addr) 1568 { 1569 struct ifnet *ifp; 1570 struct ifaddr *ifa; 1571 1572 IFNET_RLOCK_NOSLEEP(); 1573 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1574 IF_ADDR_RLOCK(ifp); 1575 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1576 if (ifa->ifa_addr->sa_family != addr->sa_family) 1577 continue; 1578 if ((ifp->if_flags & IFF_BROADCAST) && 1579 ifa->ifa_broadaddr && 1580 ifa->ifa_broadaddr->sa_len != 0 && 1581 sa_equal(ifa->ifa_broadaddr, addr)) { 1582 ifa_ref(ifa); 1583 IF_ADDR_RUNLOCK(ifp); 1584 goto done; 1585 } 1586 } 1587 IF_ADDR_RUNLOCK(ifp); 1588 } 1589 ifa = NULL; 1590 done: 1591 IFNET_RUNLOCK_NOSLEEP(); 1592 return (ifa); 1593 } 1594 1595 /* 1596 * Locate the point to point interface with a given destination address. 1597 */ 1598 /*ARGSUSED*/ 1599 struct ifaddr * 1600 ifa_ifwithdstaddr(struct sockaddr *addr) 1601 { 1602 struct ifnet *ifp; 1603 struct ifaddr *ifa; 1604 1605 IFNET_RLOCK_NOSLEEP(); 1606 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1607 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 1608 continue; 1609 IF_ADDR_RLOCK(ifp); 1610 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1611 if (ifa->ifa_addr->sa_family != addr->sa_family) 1612 continue; 1613 if (ifa->ifa_dstaddr != NULL && 1614 sa_equal(addr, ifa->ifa_dstaddr)) { 1615 ifa_ref(ifa); 1616 IF_ADDR_RUNLOCK(ifp); 1617 goto done; 1618 } 1619 } 1620 IF_ADDR_RUNLOCK(ifp); 1621 } 1622 ifa = NULL; 1623 done: 1624 IFNET_RUNLOCK_NOSLEEP(); 1625 return (ifa); 1626 } 1627 1628 /* 1629 * Find an interface on a specific network. If many, choice 1630 * is most specific found. 1631 */ 1632 struct ifaddr * 1633 ifa_ifwithnet(struct sockaddr *addr, int ignore_ptp) 1634 { 1635 struct ifnet *ifp; 1636 struct ifaddr *ifa; 1637 struct ifaddr *ifa_maybe = NULL; 1638 u_int af = addr->sa_family; 1639 char *addr_data = addr->sa_data, *cplim; 1640 1641 /* 1642 * AF_LINK addresses can be looked up directly by their index number, 1643 * so do that if we can. 1644 */ 1645 if (af == AF_LINK) { 1646 struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; 1647 if (sdl->sdl_index && sdl->sdl_index <= V_if_index) 1648 return (ifaddr_byindex(sdl->sdl_index)); 1649 } 1650 1651 /* 1652 * Scan though each interface, looking for ones that have addresses 1653 * in this address family. Maintain a reference on ifa_maybe once 1654 * we find one, as we release the IF_ADDR_RLOCK() that kept it stable 1655 * when we move onto the next interface. 1656 */ 1657 IFNET_RLOCK_NOSLEEP(); 1658 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1659 IF_ADDR_RLOCK(ifp); 1660 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1661 char *cp, *cp2, *cp3; 1662 1663 if (ifa->ifa_addr->sa_family != af) 1664 next: continue; 1665 if (af == AF_INET && 1666 ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) { 1667 /* 1668 * This is a bit broken as it doesn't 1669 * take into account that the remote end may 1670 * be a single node in the network we are 1671 * looking for. 1672 * The trouble is that we don't know the 1673 * netmask for the remote end. 1674 */ 1675 if (ifa->ifa_dstaddr != NULL && 1676 sa_equal(addr, ifa->ifa_dstaddr)) { 1677 ifa_ref(ifa); 1678 IF_ADDR_RUNLOCK(ifp); 1679 goto done; 1680 } 1681 } else { 1682 /* 1683 * if we have a special address handler, 1684 * then use it instead of the generic one. 1685 */ 1686 if (ifa->ifa_claim_addr) { 1687 if ((*ifa->ifa_claim_addr)(ifa, addr)) { 1688 ifa_ref(ifa); 1689 IF_ADDR_RUNLOCK(ifp); 1690 goto done; 1691 } 1692 continue; 1693 } 1694 1695 /* 1696 * Scan all the bits in the ifa's address. 1697 * If a bit dissagrees with what we are 1698 * looking for, mask it with the netmask 1699 * to see if it really matters. 1700 * (A byte at a time) 1701 */ 1702 if (ifa->ifa_netmask == 0) 1703 continue; 1704 cp = addr_data; 1705 cp2 = ifa->ifa_addr->sa_data; 1706 cp3 = ifa->ifa_netmask->sa_data; 1707 cplim = ifa->ifa_netmask->sa_len 1708 + (char *)ifa->ifa_netmask; 1709 while (cp3 < cplim) 1710 if ((*cp++ ^ *cp2++) & *cp3++) 1711 goto next; /* next address! */ 1712 /* 1713 * If the netmask of what we just found 1714 * is more specific than what we had before 1715 * (if we had one) then remember the new one 1716 * before continuing to search 1717 * for an even better one. 1718 */ 1719 if (ifa_maybe == NULL || 1720 rn_refines((caddr_t)ifa->ifa_netmask, 1721 (caddr_t)ifa_maybe->ifa_netmask)) { 1722 if (ifa_maybe != NULL) 1723 ifa_free(ifa_maybe); 1724 ifa_maybe = ifa; 1725 ifa_ref(ifa_maybe); 1726 } 1727 } 1728 } 1729 IF_ADDR_RUNLOCK(ifp); 1730 } 1731 ifa = ifa_maybe; 1732 ifa_maybe = NULL; 1733 done: 1734 IFNET_RUNLOCK_NOSLEEP(); 1735 if (ifa_maybe != NULL) 1736 ifa_free(ifa_maybe); 1737 return (ifa); 1738 } 1739 1740 /* 1741 * Find an interface address specific to an interface best matching 1742 * a given address. 1743 */ 1744 struct ifaddr * 1745 ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp) 1746 { 1747 struct ifaddr *ifa; 1748 char *cp, *cp2, *cp3; 1749 char *cplim; 1750 struct ifaddr *ifa_maybe = NULL; 1751 u_int af = addr->sa_family; 1752 1753 if (af >= AF_MAX) 1754 return (NULL); 1755 IF_ADDR_RLOCK(ifp); 1756 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1757 if (ifa->ifa_addr->sa_family != af) 1758 continue; 1759 if (ifa_maybe == NULL) 1760 ifa_maybe = ifa; 1761 if (ifa->ifa_netmask == 0) { 1762 if (sa_equal(addr, ifa->ifa_addr) || 1763 (ifa->ifa_dstaddr && 1764 sa_equal(addr, ifa->ifa_dstaddr))) 1765 goto done; 1766 continue; 1767 } 1768 if (ifp->if_flags & IFF_POINTOPOINT) { 1769 if (sa_equal(addr, ifa->ifa_dstaddr)) 1770 goto done; 1771 } else { 1772 cp = addr->sa_data; 1773 cp2 = ifa->ifa_addr->sa_data; 1774 cp3 = ifa->ifa_netmask->sa_data; 1775 cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 1776 for (; cp3 < cplim; cp3++) 1777 if ((*cp++ ^ *cp2++) & *cp3) 1778 break; 1779 if (cp3 == cplim) 1780 goto done; 1781 } 1782 } 1783 ifa = ifa_maybe; 1784 done: 1785 if (ifa != NULL) 1786 ifa_ref(ifa); 1787 IF_ADDR_RUNLOCK(ifp); 1788 return (ifa); 1789 } 1790 1791 #include <net/if_llatbl.h> 1792 1793 /* 1794 * Default action when installing a route with a Link Level gateway. 1795 * Lookup an appropriate real ifa to point to. 1796 * This should be moved to /sys/net/link.c eventually. 1797 */ 1798 static void 1799 link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 1800 { 1801 struct ifaddr *ifa, *oifa; 1802 struct sockaddr *dst; 1803 struct ifnet *ifp; 1804 1805 RT_LOCK_ASSERT(rt); 1806 1807 if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 1808 ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 1809 return; 1810 ifa = ifaof_ifpforaddr(dst, ifp); 1811 if (ifa) { 1812 oifa = rt->rt_ifa; 1813 rt->rt_ifa = ifa; 1814 ifa_free(oifa); 1815 if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 1816 ifa->ifa_rtrequest(cmd, rt, info); 1817 } 1818 } 1819 1820 /* 1821 * Mark an interface down and notify protocols of 1822 * the transition. 1823 * NOTE: must be called at splnet or eqivalent. 1824 */ 1825 static void 1826 if_unroute(struct ifnet *ifp, int flag, int fam) 1827 { 1828 struct ifaddr *ifa; 1829 1830 KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP")); 1831 1832 ifp->if_flags &= ~flag; 1833 getmicrotime(&ifp->if_lastchange); 1834 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1835 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1836 pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 1837 ifp->if_qflush(ifp); 1838 1839 if (ifp->if_carp) 1840 (*carp_linkstate_p)(ifp); 1841 rt_ifmsg(ifp); 1842 } 1843 1844 /* 1845 * Mark an interface up and notify protocols of 1846 * the transition. 1847 * NOTE: must be called at splnet or eqivalent. 1848 */ 1849 static void 1850 if_route(struct ifnet *ifp, int flag, int fam) 1851 { 1852 struct ifaddr *ifa; 1853 1854 KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP")); 1855 1856 ifp->if_flags |= flag; 1857 getmicrotime(&ifp->if_lastchange); 1858 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1859 if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1860 pfctlinput(PRC_IFUP, ifa->ifa_addr); 1861 if (ifp->if_carp) 1862 (*carp_linkstate_p)(ifp); 1863 rt_ifmsg(ifp); 1864 #ifdef INET6 1865 in6_if_up(ifp); 1866 #endif 1867 } 1868 1869 void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */ 1870 void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */ 1871 struct ifnet *(*vlan_trunkdev_p)(struct ifnet *); 1872 struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t); 1873 int (*vlan_tag_p)(struct ifnet *, uint16_t *); 1874 int (*vlan_setcookie_p)(struct ifnet *, void *); 1875 void *(*vlan_cookie_p)(struct ifnet *); 1876 1877 /* 1878 * Handle a change in the interface link state. To avoid LORs 1879 * between driver lock and upper layer locks, as well as possible 1880 * recursions, we post event to taskqueue, and all job 1881 * is done in static do_link_state_change(). 1882 */ 1883 void 1884 if_link_state_change(struct ifnet *ifp, int link_state) 1885 { 1886 /* Return if state hasn't changed. */ 1887 if (ifp->if_link_state == link_state) 1888 return; 1889 1890 ifp->if_link_state = link_state; 1891 1892 taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask); 1893 } 1894 1895 static void 1896 do_link_state_change(void *arg, int pending) 1897 { 1898 struct ifnet *ifp = (struct ifnet *)arg; 1899 int link_state = ifp->if_link_state; 1900 CURVNET_SET(ifp->if_vnet); 1901 1902 /* Notify that the link state has changed. */ 1903 rt_ifmsg(ifp); 1904 if (ifp->if_vlantrunk != NULL) 1905 (*vlan_link_state_p)(ifp); 1906 1907 if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) && 1908 IFP2AC(ifp)->ac_netgraph != NULL) 1909 (*ng_ether_link_state_p)(ifp, link_state); 1910 if (ifp->if_carp) 1911 (*carp_linkstate_p)(ifp); 1912 if (ifp->if_bridge) { 1913 KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!")); 1914 (*bstp_linkstate_p)(ifp, link_state); 1915 } 1916 if (ifp->if_lagg) { 1917 KASSERT(lagg_linkstate_p != NULL,("if_lagg not loaded!")); 1918 (*lagg_linkstate_p)(ifp, link_state); 1919 } 1920 1921 if (IS_DEFAULT_VNET(curvnet)) 1922 devctl_notify("IFNET", ifp->if_xname, 1923 (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", 1924 NULL); 1925 if (pending > 1) 1926 if_printf(ifp, "%d link states coalesced\n", pending); 1927 if (log_link_state_change) 1928 log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname, 1929 (link_state == LINK_STATE_UP) ? "UP" : "DOWN" ); 1930 EVENTHANDLER_INVOKE(ifnet_link_event, ifp, ifp->if_link_state); 1931 CURVNET_RESTORE(); 1932 } 1933 1934 /* 1935 * Mark an interface down and notify protocols of 1936 * the transition. 1937 * NOTE: must be called at splnet or eqivalent. 1938 */ 1939 void 1940 if_down(struct ifnet *ifp) 1941 { 1942 1943 if_unroute(ifp, IFF_UP, AF_UNSPEC); 1944 } 1945 1946 /* 1947 * Mark an interface up and notify protocols of 1948 * the transition. 1949 * NOTE: must be called at splnet or eqivalent. 1950 */ 1951 void 1952 if_up(struct ifnet *ifp) 1953 { 1954 1955 if_route(ifp, IFF_UP, AF_UNSPEC); 1956 } 1957 1958 /* 1959 * Flush an interface queue. 1960 */ 1961 void 1962 if_qflush(struct ifnet *ifp) 1963 { 1964 struct mbuf *m, *n; 1965 struct ifaltq *ifq; 1966 1967 ifq = &ifp->if_snd; 1968 IFQ_LOCK(ifq); 1969 #ifdef ALTQ 1970 if (ALTQ_IS_ENABLED(ifq)) 1971 ALTQ_PURGE(ifq); 1972 #endif 1973 n = ifq->ifq_head; 1974 while ((m = n) != 0) { 1975 n = m->m_act; 1976 m_freem(m); 1977 } 1978 ifq->ifq_head = 0; 1979 ifq->ifq_tail = 0; 1980 ifq->ifq_len = 0; 1981 IFQ_UNLOCK(ifq); 1982 } 1983 1984 /* 1985 * Map interface name to interface structure pointer, with or without 1986 * returning a reference. 1987 */ 1988 struct ifnet * 1989 ifunit_ref(const char *name) 1990 { 1991 struct ifnet *ifp; 1992 1993 IFNET_RLOCK_NOSLEEP(); 1994 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1995 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 && 1996 !(ifp->if_flags & IFF_DYING)) 1997 break; 1998 } 1999 if (ifp != NULL) 2000 if_ref(ifp); 2001 IFNET_RUNLOCK_NOSLEEP(); 2002 return (ifp); 2003 } 2004 2005 struct ifnet * 2006 ifunit(const char *name) 2007 { 2008 struct ifnet *ifp; 2009 2010 IFNET_RLOCK_NOSLEEP(); 2011 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2012 if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0) 2013 break; 2014 } 2015 IFNET_RUNLOCK_NOSLEEP(); 2016 return (ifp); 2017 } 2018 2019 /* 2020 * Hardware specific interface ioctls. 2021 */ 2022 static int 2023 ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td) 2024 { 2025 struct ifreq *ifr; 2026 struct ifstat *ifs; 2027 int error = 0; 2028 int new_flags, temp_flags; 2029 size_t namelen, onamelen; 2030 size_t descrlen; 2031 char *descrbuf, *odescrbuf; 2032 char new_name[IFNAMSIZ]; 2033 struct ifaddr *ifa; 2034 struct sockaddr_dl *sdl; 2035 2036 ifr = (struct ifreq *)data; 2037 switch (cmd) { 2038 case SIOCGIFINDEX: 2039 ifr->ifr_index = ifp->if_index; 2040 break; 2041 2042 case SIOCGIFFLAGS: 2043 temp_flags = ifp->if_flags | ifp->if_drv_flags; 2044 ifr->ifr_flags = temp_flags & 0xffff; 2045 ifr->ifr_flagshigh = temp_flags >> 16; 2046 break; 2047 2048 case SIOCGIFCAP: 2049 ifr->ifr_reqcap = ifp->if_capabilities; 2050 ifr->ifr_curcap = ifp->if_capenable; 2051 break; 2052 2053 #ifdef MAC 2054 case SIOCGIFMAC: 2055 error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp); 2056 break; 2057 #endif 2058 2059 case SIOCGIFMETRIC: 2060 ifr->ifr_metric = ifp->if_metric; 2061 break; 2062 2063 case SIOCGIFMTU: 2064 ifr->ifr_mtu = ifp->if_mtu; 2065 break; 2066 2067 case SIOCGIFPHYS: 2068 ifr->ifr_phys = ifp->if_physical; 2069 break; 2070 2071 case SIOCGIFDESCR: 2072 error = 0; 2073 sx_slock(&ifdescr_sx); 2074 if (ifp->if_description == NULL) 2075 error = ENOMSG; 2076 else { 2077 /* space for terminating nul */ 2078 descrlen = strlen(ifp->if_description) + 1; 2079 if (ifr->ifr_buffer.length < descrlen) 2080 ifr->ifr_buffer.buffer = NULL; 2081 else 2082 error = copyout(ifp->if_description, 2083 ifr->ifr_buffer.buffer, descrlen); 2084 ifr->ifr_buffer.length = descrlen; 2085 } 2086 sx_sunlock(&ifdescr_sx); 2087 break; 2088 2089 case SIOCSIFDESCR: 2090 error = priv_check(td, PRIV_NET_SETIFDESCR); 2091 if (error) 2092 return (error); 2093 2094 /* 2095 * Copy only (length-1) bytes to make sure that 2096 * if_description is always nul terminated. The 2097 * length parameter is supposed to count the 2098 * terminating nul in. 2099 */ 2100 if (ifr->ifr_buffer.length > ifdescr_maxlen) 2101 return (ENAMETOOLONG); 2102 else if (ifr->ifr_buffer.length == 0) 2103 descrbuf = NULL; 2104 else { 2105 descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR, 2106 M_WAITOK | M_ZERO); 2107 error = copyin(ifr->ifr_buffer.buffer, descrbuf, 2108 ifr->ifr_buffer.length - 1); 2109 if (error) { 2110 free(descrbuf, M_IFDESCR); 2111 break; 2112 } 2113 } 2114 2115 sx_xlock(&ifdescr_sx); 2116 odescrbuf = ifp->if_description; 2117 ifp->if_description = descrbuf; 2118 sx_xunlock(&ifdescr_sx); 2119 2120 getmicrotime(&ifp->if_lastchange); 2121 free(odescrbuf, M_IFDESCR); 2122 break; 2123 2124 case SIOCGIFFIB: 2125 ifr->ifr_fib = ifp->if_fib; 2126 break; 2127 2128 case SIOCSIFFIB: 2129 error = priv_check(td, PRIV_NET_SETIFFIB); 2130 if (error) 2131 return (error); 2132 if (ifr->ifr_fib >= rt_numfibs) 2133 return (EINVAL); 2134 2135 ifp->if_fib = ifr->ifr_fib; 2136 break; 2137 2138 case SIOCSIFFLAGS: 2139 error = priv_check(td, PRIV_NET_SETIFFLAGS); 2140 if (error) 2141 return (error); 2142 /* 2143 * Currently, no driver owned flags pass the IFF_CANTCHANGE 2144 * check, so we don't need special handling here yet. 2145 */ 2146 new_flags = (ifr->ifr_flags & 0xffff) | 2147 (ifr->ifr_flagshigh << 16); 2148 if (ifp->if_flags & IFF_SMART) { 2149 /* Smart drivers twiddle their own routes */ 2150 } else if (ifp->if_flags & IFF_UP && 2151 (new_flags & IFF_UP) == 0) { 2152 int s = splimp(); 2153 if_down(ifp); 2154 splx(s); 2155 } else if (new_flags & IFF_UP && 2156 (ifp->if_flags & IFF_UP) == 0) { 2157 int s = splimp(); 2158 if_up(ifp); 2159 splx(s); 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 int s = splimp(); 2609 in6_if_up(ifp); 2610 splx(s); 2611 } 2612 #endif 2613 } 2614 if_rele(ifp); 2615 CURVNET_RESTORE(); 2616 return (error); 2617 } 2618 2619 /* 2620 * The code common to handling reference counted flags, 2621 * e.g., in ifpromisc() and if_allmulti(). 2622 * The "pflag" argument can specify a permanent mode flag to check, 2623 * such as IFF_PPROMISC for promiscuous mode; should be 0 if none. 2624 * 2625 * Only to be used on stack-owned flags, not driver-owned flags. 2626 */ 2627 static int 2628 if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch) 2629 { 2630 struct ifreq ifr; 2631 int error; 2632 int oldflags, oldcount; 2633 2634 /* Sanity checks to catch programming errors */ 2635 KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0, 2636 ("%s: setting driver-owned flag %d", __func__, flag)); 2637 2638 if (onswitch) 2639 KASSERT(*refcount >= 0, 2640 ("%s: increment negative refcount %d for flag %d", 2641 __func__, *refcount, flag)); 2642 else 2643 KASSERT(*refcount > 0, 2644 ("%s: decrement non-positive refcount %d for flag %d", 2645 __func__, *refcount, flag)); 2646 2647 /* In case this mode is permanent, just touch refcount */ 2648 if (ifp->if_flags & pflag) { 2649 *refcount += onswitch ? 1 : -1; 2650 return (0); 2651 } 2652 2653 /* Save ifnet parameters for if_ioctl() may fail */ 2654 oldcount = *refcount; 2655 oldflags = ifp->if_flags; 2656 2657 /* 2658 * See if we aren't the only and touching refcount is enough. 2659 * Actually toggle interface flag if we are the first or last. 2660 */ 2661 if (onswitch) { 2662 if ((*refcount)++) 2663 return (0); 2664 ifp->if_flags |= flag; 2665 } else { 2666 if (--(*refcount)) 2667 return (0); 2668 ifp->if_flags &= ~flag; 2669 } 2670 2671 /* Call down the driver since we've changed interface flags */ 2672 if (ifp->if_ioctl == NULL) { 2673 error = EOPNOTSUPP; 2674 goto recover; 2675 } 2676 ifr.ifr_flags = ifp->if_flags & 0xffff; 2677 ifr.ifr_flagshigh = ifp->if_flags >> 16; 2678 error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 2679 if (error) 2680 goto recover; 2681 /* Notify userland that interface flags have changed */ 2682 rt_ifmsg(ifp); 2683 return (0); 2684 2685 recover: 2686 /* Recover after driver error */ 2687 *refcount = oldcount; 2688 ifp->if_flags = oldflags; 2689 return (error); 2690 } 2691 2692 /* 2693 * Set/clear promiscuous mode on interface ifp based on the truth value 2694 * of pswitch. The calls are reference counted so that only the first 2695 * "on" request actually has an effect, as does the final "off" request. 2696 * Results are undefined if the "off" and "on" requests are not matched. 2697 */ 2698 int 2699 ifpromisc(struct ifnet *ifp, int pswitch) 2700 { 2701 int error; 2702 int oldflags = ifp->if_flags; 2703 2704 error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC, 2705 &ifp->if_pcount, pswitch); 2706 /* If promiscuous mode status has changed, log a message */ 2707 if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC)) 2708 log(LOG_INFO, "%s: promiscuous mode %s\n", 2709 ifp->if_xname, 2710 (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); 2711 return (error); 2712 } 2713 2714 /* 2715 * Return interface configuration 2716 * of system. List may be used 2717 * in later ioctl's (above) to get 2718 * other information. 2719 */ 2720 /*ARGSUSED*/ 2721 static int 2722 ifconf(u_long cmd, caddr_t data) 2723 { 2724 struct ifconf *ifc = (struct ifconf *)data; 2725 struct ifnet *ifp; 2726 struct ifaddr *ifa; 2727 struct ifreq ifr; 2728 struct sbuf *sb; 2729 int error, full = 0, valid_len, max_len; 2730 2731 /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */ 2732 max_len = MAXPHYS - 1; 2733 2734 /* Prevent hostile input from being able to crash the system */ 2735 if (ifc->ifc_len <= 0) 2736 return (EINVAL); 2737 2738 again: 2739 if (ifc->ifc_len <= max_len) { 2740 max_len = ifc->ifc_len; 2741 full = 1; 2742 } 2743 sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN); 2744 max_len = 0; 2745 valid_len = 0; 2746 2747 IFNET_RLOCK(); 2748 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2749 int addrs; 2750 2751 /* 2752 * Zero the ifr_name buffer to make sure we don't 2753 * disclose the contents of the stack. 2754 */ 2755 memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name)); 2756 2757 if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)) 2758 >= sizeof(ifr.ifr_name)) { 2759 sbuf_delete(sb); 2760 IFNET_RUNLOCK(); 2761 return (ENAMETOOLONG); 2762 } 2763 2764 addrs = 0; 2765 IF_ADDR_RLOCK(ifp); 2766 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2767 struct sockaddr *sa = ifa->ifa_addr; 2768 2769 if (prison_if(curthread->td_ucred, sa) != 0) 2770 continue; 2771 addrs++; 2772 #ifdef COMPAT_43 2773 if (cmd == OSIOCGIFCONF) { 2774 struct osockaddr *osa = 2775 (struct osockaddr *)&ifr.ifr_addr; 2776 ifr.ifr_addr = *sa; 2777 osa->sa_family = sa->sa_family; 2778 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2779 max_len += sizeof(ifr); 2780 } else 2781 #endif 2782 if (sa->sa_len <= sizeof(*sa)) { 2783 ifr.ifr_addr = *sa; 2784 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2785 max_len += sizeof(ifr); 2786 } else { 2787 sbuf_bcat(sb, &ifr, 2788 offsetof(struct ifreq, ifr_addr)); 2789 max_len += offsetof(struct ifreq, ifr_addr); 2790 sbuf_bcat(sb, sa, sa->sa_len); 2791 max_len += sa->sa_len; 2792 } 2793 2794 if (sbuf_error(sb) == 0) 2795 valid_len = sbuf_len(sb); 2796 } 2797 IF_ADDR_RUNLOCK(ifp); 2798 if (addrs == 0) { 2799 bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 2800 sbuf_bcat(sb, &ifr, sizeof(ifr)); 2801 max_len += sizeof(ifr); 2802 2803 if (sbuf_error(sb) == 0) 2804 valid_len = sbuf_len(sb); 2805 } 2806 } 2807 IFNET_RUNLOCK(); 2808 2809 /* 2810 * If we didn't allocate enough space (uncommon), try again. If 2811 * we have already allocated as much space as we are allowed, 2812 * return what we've got. 2813 */ 2814 if (valid_len != max_len && !full) { 2815 sbuf_delete(sb); 2816 goto again; 2817 } 2818 2819 ifc->ifc_len = valid_len; 2820 sbuf_finish(sb); 2821 error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len); 2822 sbuf_delete(sb); 2823 return (error); 2824 } 2825 2826 /* 2827 * Just like ifpromisc(), but for all-multicast-reception mode. 2828 */ 2829 int 2830 if_allmulti(struct ifnet *ifp, int onswitch) 2831 { 2832 2833 return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch)); 2834 } 2835 2836 struct ifmultiaddr * 2837 if_findmulti(struct ifnet *ifp, struct sockaddr *sa) 2838 { 2839 struct ifmultiaddr *ifma; 2840 2841 IF_ADDR_LOCK_ASSERT(ifp); 2842 2843 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 2844 if (sa->sa_family == AF_LINK) { 2845 if (sa_dl_equal(ifma->ifma_addr, sa)) 2846 break; 2847 } else { 2848 if (sa_equal(ifma->ifma_addr, sa)) 2849 break; 2850 } 2851 } 2852 2853 return ifma; 2854 } 2855 2856 /* 2857 * Allocate a new ifmultiaddr and initialize based on passed arguments. We 2858 * make copies of passed sockaddrs. The ifmultiaddr will not be added to 2859 * the ifnet multicast address list here, so the caller must do that and 2860 * other setup work (such as notifying the device driver). The reference 2861 * count is initialized to 1. 2862 */ 2863 static struct ifmultiaddr * 2864 if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa, 2865 int mflags) 2866 { 2867 struct ifmultiaddr *ifma; 2868 struct sockaddr *dupsa; 2869 2870 ifma = malloc(sizeof *ifma, M_IFMADDR, mflags | 2871 M_ZERO); 2872 if (ifma == NULL) 2873 return (NULL); 2874 2875 dupsa = malloc(sa->sa_len, M_IFMADDR, mflags); 2876 if (dupsa == NULL) { 2877 free(ifma, M_IFMADDR); 2878 return (NULL); 2879 } 2880 bcopy(sa, dupsa, sa->sa_len); 2881 ifma->ifma_addr = dupsa; 2882 2883 ifma->ifma_ifp = ifp; 2884 ifma->ifma_refcount = 1; 2885 ifma->ifma_protospec = NULL; 2886 2887 if (llsa == NULL) { 2888 ifma->ifma_lladdr = NULL; 2889 return (ifma); 2890 } 2891 2892 dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags); 2893 if (dupsa == NULL) { 2894 free(ifma->ifma_addr, M_IFMADDR); 2895 free(ifma, M_IFMADDR); 2896 return (NULL); 2897 } 2898 bcopy(llsa, dupsa, llsa->sa_len); 2899 ifma->ifma_lladdr = dupsa; 2900 2901 return (ifma); 2902 } 2903 2904 /* 2905 * if_freemulti: free ifmultiaddr structure and possibly attached related 2906 * addresses. The caller is responsible for implementing reference 2907 * counting, notifying the driver, handling routing messages, and releasing 2908 * any dependent link layer state. 2909 */ 2910 static void 2911 if_freemulti(struct ifmultiaddr *ifma) 2912 { 2913 2914 KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d", 2915 ifma->ifma_refcount)); 2916 KASSERT(ifma->ifma_protospec == NULL, 2917 ("if_freemulti: protospec not NULL")); 2918 2919 if (ifma->ifma_lladdr != NULL) 2920 free(ifma->ifma_lladdr, M_IFMADDR); 2921 free(ifma->ifma_addr, M_IFMADDR); 2922 free(ifma, M_IFMADDR); 2923 } 2924 2925 /* 2926 * Register an additional multicast address with a network interface. 2927 * 2928 * - If the address is already present, bump the reference count on the 2929 * address and return. 2930 * - If the address is not link-layer, look up a link layer address. 2931 * - Allocate address structures for one or both addresses, and attach to the 2932 * multicast address list on the interface. If automatically adding a link 2933 * layer address, the protocol address will own a reference to the link 2934 * layer address, to be freed when it is freed. 2935 * - Notify the network device driver of an addition to the multicast address 2936 * list. 2937 * 2938 * 'sa' points to caller-owned memory with the desired multicast address. 2939 * 2940 * 'retifma' will be used to return a pointer to the resulting multicast 2941 * address reference, if desired. 2942 */ 2943 int 2944 if_addmulti(struct ifnet *ifp, struct sockaddr *sa, 2945 struct ifmultiaddr **retifma) 2946 { 2947 struct ifmultiaddr *ifma, *ll_ifma; 2948 struct sockaddr *llsa; 2949 int error; 2950 2951 /* 2952 * If the address is already present, return a new reference to it; 2953 * otherwise, allocate storage and set up a new address. 2954 */ 2955 IF_ADDR_WLOCK(ifp); 2956 ifma = if_findmulti(ifp, sa); 2957 if (ifma != NULL) { 2958 ifma->ifma_refcount++; 2959 if (retifma != NULL) 2960 *retifma = ifma; 2961 IF_ADDR_WUNLOCK(ifp); 2962 return (0); 2963 } 2964 2965 /* 2966 * The address isn't already present; resolve the protocol address 2967 * into a link layer address, and then look that up, bump its 2968 * refcount or allocate an ifma for that also. If 'llsa' was 2969 * returned, we will need to free it later. 2970 */ 2971 llsa = NULL; 2972 ll_ifma = NULL; 2973 if (ifp->if_resolvemulti != NULL) { 2974 error = ifp->if_resolvemulti(ifp, &llsa, sa); 2975 if (error) 2976 goto unlock_out; 2977 } 2978 2979 /* 2980 * Allocate the new address. Don't hook it up yet, as we may also 2981 * need to allocate a link layer multicast address. 2982 */ 2983 ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT); 2984 if (ifma == NULL) { 2985 error = ENOMEM; 2986 goto free_llsa_out; 2987 } 2988 2989 /* 2990 * If a link layer address is found, we'll need to see if it's 2991 * already present in the address list, or allocate is as well. 2992 * When this block finishes, the link layer address will be on the 2993 * list. 2994 */ 2995 if (llsa != NULL) { 2996 ll_ifma = if_findmulti(ifp, llsa); 2997 if (ll_ifma == NULL) { 2998 ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT); 2999 if (ll_ifma == NULL) { 3000 --ifma->ifma_refcount; 3001 if_freemulti(ifma); 3002 error = ENOMEM; 3003 goto free_llsa_out; 3004 } 3005 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma, 3006 ifma_link); 3007 } else 3008 ll_ifma->ifma_refcount++; 3009 ifma->ifma_llifma = ll_ifma; 3010 } 3011 3012 /* 3013 * We now have a new multicast address, ifma, and possibly a new or 3014 * referenced link layer address. Add the primary address to the 3015 * ifnet address list. 3016 */ 3017 TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 3018 3019 if (retifma != NULL) 3020 *retifma = ifma; 3021 3022 /* 3023 * Must generate the message while holding the lock so that 'ifma' 3024 * pointer is still valid. 3025 */ 3026 rt_newmaddrmsg(RTM_NEWMADDR, ifma); 3027 IF_ADDR_WUNLOCK(ifp); 3028 3029 /* 3030 * We are certain we have added something, so call down to the 3031 * interface to let them know about it. 3032 */ 3033 if (ifp->if_ioctl != NULL) { 3034 (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0); 3035 } 3036 3037 if (llsa != NULL) 3038 free(llsa, M_IFMADDR); 3039 3040 return (0); 3041 3042 free_llsa_out: 3043 if (llsa != NULL) 3044 free(llsa, M_IFMADDR); 3045 3046 unlock_out: 3047 IF_ADDR_WUNLOCK(ifp); 3048 return (error); 3049 } 3050 3051 /* 3052 * Delete a multicast group membership by network-layer group address. 3053 * 3054 * Returns ENOENT if the entry could not be found. If ifp no longer 3055 * exists, results are undefined. This entry point should only be used 3056 * from subsystems which do appropriate locking to hold ifp for the 3057 * duration of the call. 3058 * Network-layer protocol domains must use if_delmulti_ifma(). 3059 */ 3060 int 3061 if_delmulti(struct ifnet *ifp, struct sockaddr *sa) 3062 { 3063 struct ifmultiaddr *ifma; 3064 int lastref; 3065 #ifdef INVARIANTS 3066 struct ifnet *oifp; 3067 3068 IFNET_RLOCK_NOSLEEP(); 3069 TAILQ_FOREACH(oifp, &V_ifnet, if_link) 3070 if (ifp == oifp) 3071 break; 3072 if (ifp != oifp) 3073 ifp = NULL; 3074 IFNET_RUNLOCK_NOSLEEP(); 3075 3076 KASSERT(ifp != NULL, ("%s: ifnet went away", __func__)); 3077 #endif 3078 if (ifp == NULL) 3079 return (ENOENT); 3080 3081 IF_ADDR_WLOCK(ifp); 3082 lastref = 0; 3083 ifma = if_findmulti(ifp, sa); 3084 if (ifma != NULL) 3085 lastref = if_delmulti_locked(ifp, ifma, 0); 3086 IF_ADDR_WUNLOCK(ifp); 3087 3088 if (ifma == NULL) 3089 return (ENOENT); 3090 3091 if (lastref && ifp->if_ioctl != NULL) { 3092 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 3093 } 3094 3095 return (0); 3096 } 3097 3098 /* 3099 * Delete all multicast group membership for an interface. 3100 * Should be used to quickly flush all multicast filters. 3101 */ 3102 void 3103 if_delallmulti(struct ifnet *ifp) 3104 { 3105 struct ifmultiaddr *ifma; 3106 struct ifmultiaddr *next; 3107 3108 IF_ADDR_WLOCK(ifp); 3109 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) 3110 if_delmulti_locked(ifp, ifma, 0); 3111 IF_ADDR_WUNLOCK(ifp); 3112 } 3113 3114 /* 3115 * Delete a multicast group membership by group membership pointer. 3116 * Network-layer protocol domains must use this routine. 3117 * 3118 * It is safe to call this routine if the ifp disappeared. 3119 */ 3120 void 3121 if_delmulti_ifma(struct ifmultiaddr *ifma) 3122 { 3123 struct ifnet *ifp; 3124 int lastref; 3125 3126 ifp = ifma->ifma_ifp; 3127 #ifdef DIAGNOSTIC 3128 if (ifp == NULL) { 3129 printf("%s: ifma_ifp seems to be detached\n", __func__); 3130 } else { 3131 struct ifnet *oifp; 3132 3133 IFNET_RLOCK_NOSLEEP(); 3134 TAILQ_FOREACH(oifp, &V_ifnet, if_link) 3135 if (ifp == oifp) 3136 break; 3137 if (ifp != oifp) { 3138 printf("%s: ifnet %p disappeared\n", __func__, ifp); 3139 ifp = NULL; 3140 } 3141 IFNET_RUNLOCK_NOSLEEP(); 3142 } 3143 #endif 3144 /* 3145 * If and only if the ifnet instance exists: Acquire the address lock. 3146 */ 3147 if (ifp != NULL) 3148 IF_ADDR_WLOCK(ifp); 3149 3150 lastref = if_delmulti_locked(ifp, ifma, 0); 3151 3152 if (ifp != NULL) { 3153 /* 3154 * If and only if the ifnet instance exists: 3155 * Release the address lock. 3156 * If the group was left: update the hardware hash filter. 3157 */ 3158 IF_ADDR_WUNLOCK(ifp); 3159 if (lastref && ifp->if_ioctl != NULL) { 3160 (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 3161 } 3162 } 3163 } 3164 3165 /* 3166 * Perform deletion of network-layer and/or link-layer multicast address. 3167 * 3168 * Return 0 if the reference count was decremented. 3169 * Return 1 if the final reference was released, indicating that the 3170 * hardware hash filter should be reprogrammed. 3171 */ 3172 static int 3173 if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching) 3174 { 3175 struct ifmultiaddr *ll_ifma; 3176 3177 if (ifp != NULL && ifma->ifma_ifp != NULL) { 3178 KASSERT(ifma->ifma_ifp == ifp, 3179 ("%s: inconsistent ifp %p", __func__, ifp)); 3180 IF_ADDR_WLOCK_ASSERT(ifp); 3181 } 3182 3183 ifp = ifma->ifma_ifp; 3184 3185 /* 3186 * If the ifnet is detaching, null out references to ifnet, 3187 * so that upper protocol layers will notice, and not attempt 3188 * to obtain locks for an ifnet which no longer exists. The 3189 * routing socket announcement must happen before the ifnet 3190 * instance is detached from the system. 3191 */ 3192 if (detaching) { 3193 #ifdef DIAGNOSTIC 3194 printf("%s: detaching ifnet instance %p\n", __func__, ifp); 3195 #endif 3196 /* 3197 * ifp may already be nulled out if we are being reentered 3198 * to delete the ll_ifma. 3199 */ 3200 if (ifp != NULL) { 3201 rt_newmaddrmsg(RTM_DELMADDR, ifma); 3202 ifma->ifma_ifp = NULL; 3203 } 3204 } 3205 3206 if (--ifma->ifma_refcount > 0) 3207 return 0; 3208 3209 /* 3210 * If this ifma is a network-layer ifma, a link-layer ifma may 3211 * have been associated with it. Release it first if so. 3212 */ 3213 ll_ifma = ifma->ifma_llifma; 3214 if (ll_ifma != NULL) { 3215 KASSERT(ifma->ifma_lladdr != NULL, 3216 ("%s: llifma w/o lladdr", __func__)); 3217 if (detaching) 3218 ll_ifma->ifma_ifp = NULL; /* XXX */ 3219 if (--ll_ifma->ifma_refcount == 0) { 3220 if (ifp != NULL) { 3221 TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, 3222 ifma_link); 3223 } 3224 if_freemulti(ll_ifma); 3225 } 3226 } 3227 3228 if (ifp != NULL) 3229 TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 3230 3231 if_freemulti(ifma); 3232 3233 /* 3234 * The last reference to this instance of struct ifmultiaddr 3235 * was released; the hardware should be notified of this change. 3236 */ 3237 return 1; 3238 } 3239 3240 /* 3241 * Set the link layer address on an interface. 3242 * 3243 * At this time we only support certain types of interfaces, 3244 * and we don't allow the length of the address to change. 3245 */ 3246 int 3247 if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 3248 { 3249 struct sockaddr_dl *sdl; 3250 struct ifaddr *ifa; 3251 struct ifreq ifr; 3252 3253 IF_ADDR_RLOCK(ifp); 3254 ifa = ifp->if_addr; 3255 if (ifa == NULL) { 3256 IF_ADDR_RUNLOCK(ifp); 3257 return (EINVAL); 3258 } 3259 ifa_ref(ifa); 3260 IF_ADDR_RUNLOCK(ifp); 3261 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 3262 if (sdl == NULL) { 3263 ifa_free(ifa); 3264 return (EINVAL); 3265 } 3266 if (len != sdl->sdl_alen) { /* don't allow length to change */ 3267 ifa_free(ifa); 3268 return (EINVAL); 3269 } 3270 switch (ifp->if_type) { 3271 case IFT_ETHER: 3272 case IFT_FDDI: 3273 case IFT_XETHER: 3274 case IFT_ISO88025: 3275 case IFT_L2VLAN: 3276 case IFT_BRIDGE: 3277 case IFT_ARCNET: 3278 case IFT_IEEE8023ADLAG: 3279 case IFT_IEEE80211: 3280 bcopy(lladdr, LLADDR(sdl), len); 3281 ifa_free(ifa); 3282 break; 3283 default: 3284 ifa_free(ifa); 3285 return (ENODEV); 3286 } 3287 3288 /* 3289 * If the interface is already up, we need 3290 * to re-init it in order to reprogram its 3291 * address filter. 3292 */ 3293 if ((ifp->if_flags & IFF_UP) != 0) { 3294 if (ifp->if_ioctl) { 3295 ifp->if_flags &= ~IFF_UP; 3296 ifr.ifr_flags = ifp->if_flags & 0xffff; 3297 ifr.ifr_flagshigh = ifp->if_flags >> 16; 3298 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 3299 ifp->if_flags |= IFF_UP; 3300 ifr.ifr_flags = ifp->if_flags & 0xffff; 3301 ifr.ifr_flagshigh = ifp->if_flags >> 16; 3302 (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 3303 } 3304 #ifdef INET 3305 /* 3306 * Also send gratuitous ARPs to notify other nodes about 3307 * the address change. 3308 */ 3309 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 3310 if (ifa->ifa_addr->sa_family == AF_INET) 3311 arp_ifinit(ifp, ifa); 3312 } 3313 #endif 3314 } 3315 return (0); 3316 } 3317 3318 /* 3319 * The name argument must be a pointer to storage which will last as 3320 * long as the interface does. For physical devices, the result of 3321 * device_get_name(dev) is a good choice and for pseudo-devices a 3322 * static string works well. 3323 */ 3324 void 3325 if_initname(struct ifnet *ifp, const char *name, int unit) 3326 { 3327 ifp->if_dname = name; 3328 ifp->if_dunit = unit; 3329 if (unit != IF_DUNIT_NONE) 3330 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit); 3331 else 3332 strlcpy(ifp->if_xname, name, IFNAMSIZ); 3333 } 3334 3335 int 3336 if_printf(struct ifnet *ifp, const char * fmt, ...) 3337 { 3338 va_list ap; 3339 int retval; 3340 3341 retval = printf("%s: ", ifp->if_xname); 3342 va_start(ap, fmt); 3343 retval += vprintf(fmt, ap); 3344 va_end(ap); 3345 return (retval); 3346 } 3347 3348 void 3349 if_start(struct ifnet *ifp) 3350 { 3351 3352 (*(ifp)->if_start)(ifp); 3353 } 3354 3355 /* 3356 * Backwards compatibility interface for drivers 3357 * that have not implemented it 3358 */ 3359 static int 3360 if_transmit(struct ifnet *ifp, struct mbuf *m) 3361 { 3362 int error; 3363 3364 IFQ_HANDOFF(ifp, m, error); 3365 return (error); 3366 } 3367 3368 int 3369 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust) 3370 { 3371 int active = 0; 3372 3373 IF_LOCK(ifq); 3374 if (_IF_QFULL(ifq)) { 3375 _IF_DROP(ifq); 3376 IF_UNLOCK(ifq); 3377 m_freem(m); 3378 return (0); 3379 } 3380 if (ifp != NULL) { 3381 ifp->if_obytes += m->m_pkthdr.len + adjust; 3382 if (m->m_flags & (M_BCAST|M_MCAST)) 3383 ifp->if_omcasts++; 3384 active = ifp->if_drv_flags & IFF_DRV_OACTIVE; 3385 } 3386 _IF_ENQUEUE(ifq, m); 3387 IF_UNLOCK(ifq); 3388 if (ifp != NULL && !active) 3389 (*(ifp)->if_start)(ifp); 3390 return (1); 3391 } 3392 3393 void 3394 if_register_com_alloc(u_char type, 3395 if_com_alloc_t *a, if_com_free_t *f) 3396 { 3397 3398 KASSERT(if_com_alloc[type] == NULL, 3399 ("if_register_com_alloc: %d already registered", type)); 3400 KASSERT(if_com_free[type] == NULL, 3401 ("if_register_com_alloc: %d free already registered", type)); 3402 3403 if_com_alloc[type] = a; 3404 if_com_free[type] = f; 3405 } 3406 3407 void 3408 if_deregister_com_alloc(u_char type) 3409 { 3410 3411 KASSERT(if_com_alloc[type] != NULL, 3412 ("if_deregister_com_alloc: %d not registered", type)); 3413 KASSERT(if_com_free[type] != NULL, 3414 ("if_deregister_com_alloc: %d free not registered", type)); 3415 if_com_alloc[type] = NULL; 3416 if_com_free[type] = NULL; 3417 } 3418