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