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