1c398230bSWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1980, 1986, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 2966afbd68SRuslan Ermilov * @(#)if.c 8.5 (Berkeley) 1/9/95 30c3aac50fSPeter Wemm * $FreeBSD$ 31df8bae1dSRodney W. Grimes */ 32df8bae1dSRodney W. Grimes 335591b823SEivind Eklund #include "opt_compat.h" 34cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h" 350d0f9d1eSYoshinobu Inoue #include "opt_inet.h" 36a9771948SGleb Smirnoff #include "opt_carp.h" 375591b823SEivind Eklund 38df8bae1dSRodney W. Grimes #include <sys/param.h> 394dcf2bbbSBrooks Davis #include <sys/types.h> 40f13ad206SJonathan Lemon #include <sys/conf.h> 414d1d4912SBruce Evans #include <sys/malloc.h> 424dcf2bbbSBrooks Davis #include <sys/sbuf.h> 43d2b4566aSJonathan Lemon #include <sys/bus.h> 44df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 45df8bae1dSRodney W. Grimes #include <sys/systm.h> 46acd3428bSRobert Watson #include <sys/priv.h> 47df8bae1dSRodney W. Grimes #include <sys/proc.h> 48df8bae1dSRodney W. Grimes #include <sys/socket.h> 49df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 50df8bae1dSRodney W. Grimes #include <sys/protosw.h> 51df8bae1dSRodney W. Grimes #include <sys/kernel.h> 52653735c4SBjoern A. Zeeb #include <sys/lock.h> 5327d37320SRobert Watson #include <sys/refcount.h> 5421ca7b57SMarko Zec #include <sys/module.h> 55653735c4SBjoern A. Zeeb #include <sys/rwlock.h> 5651a53488SBruce Evans #include <sys/sockio.h> 57963e4c2aSGarrett Wollman #include <sys/syslog.h> 58602d513cSGarrett Wollman #include <sys/sysctl.h> 59af5e59bfSRobert Watson #include <sys/taskqueue.h> 6031b1bfe1SHajimu UMEMOTO #include <sys/domain.h> 6191421ba2SRobert Watson #include <sys/jail.h> 62fa882e87SBrooks Davis #include <machine/stdarg.h> 636e6b3f7cSQing Li #include <vm/uma.h> 64df8bae1dSRodney W. Grimes 65df8bae1dSRodney W. Grimes #include <net/if.h> 665a97c9d4SBjoern A. Zeeb #include <net/if_arp.h> 67f889d2efSBrooks Davis #include <net/if_clone.h> 68df8bae1dSRodney W. Grimes #include <net/if_dl.h> 6966ce51ceSArchie Cobbs #include <net/if_types.h> 7030aad87dSBrooks Davis #include <net/if_var.h> 719448326fSPoul-Henning Kamp #include <net/radix.h> 725500d3beSWarner Losh #include <net/route.h> 734b79449eSBjoern A. Zeeb #include <net/vnet.h> 74df8bae1dSRodney W. Grimes 750d0f9d1eSYoshinobu Inoue #if defined(INET) || defined(INET6) 7682cd038dSYoshinobu Inoue /*XXX*/ 7782cd038dSYoshinobu Inoue #include <netinet/in.h> 780d0f9d1eSYoshinobu Inoue #include <netinet/in_var.h> 793411310dSYoshinobu Inoue #ifdef INET6 80978ee2edSJun-ichiro itojun Hagino #include <netinet6/in6_var.h> 81978ee2edSJun-ichiro itojun Hagino #include <netinet6/in6_ifattach.h> 823411310dSYoshinobu Inoue #endif 8382cd038dSYoshinobu Inoue #endif 84c0933269SPeter Wemm #ifdef INET 85c0933269SPeter Wemm #include <netinet/if_ether.h> 86c0933269SPeter Wemm #endif 87259d2d54SBjoern A. Zeeb #if defined(INET) || defined(INET6) 88a9771948SGleb Smirnoff #ifdef DEV_CARP 89a9771948SGleb Smirnoff #include <netinet/ip_carp.h> 90a9771948SGleb Smirnoff #endif 91259d2d54SBjoern A. Zeeb #endif 9282cd038dSYoshinobu Inoue 93aed55708SRobert Watson #include <security/mac/mac_framework.h> 94aed55708SRobert Watson 956cb7f168SBrooks Davis struct ifindex_entry { 966cb7f168SBrooks Davis struct ifnet *ife_ifnet; 976cb7f168SBrooks Davis }; 986cb7f168SBrooks Davis 99eb322a6fSJohn Baldwin static int slowtimo_started; 100eb322a6fSJohn Baldwin 1015515c2e7SGleb Smirnoff SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 1025515c2e7SGleb Smirnoff SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); 1035515c2e7SGleb Smirnoff 1045515c2e7SGleb Smirnoff /* Log link state change events */ 1055515c2e7SGleb Smirnoff static int log_link_state_change = 1; 1065515c2e7SGleb Smirnoff 1075515c2e7SGleb Smirnoff SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW, 1085515c2e7SGleb Smirnoff &log_link_state_change, 0, 1095515c2e7SGleb Smirnoff "log interface link state change events"); 1105515c2e7SGleb Smirnoff 1118f867517SAndrew Thompson void (*bstp_linkstate_p)(struct ifnet *ifp, int state); 1121c7899c7SGleb Smirnoff void (*ng_ether_link_state_p)(struct ifnet *ifp, int state); 11318242d3bSAndrew Thompson void (*lagg_linkstate_p)(struct ifnet *ifp, int state); 1141c7899c7SGleb Smirnoff 1154cb655c0SMax Laier struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL; 1164cb655c0SMax Laier 117ec002feeSBruce M Simpson /* 118ec002feeSBruce M Simpson * XXX: Style; these should be sorted alphabetically, and unprototyped 119ec002feeSBruce M Simpson * static functions should be prototyped. Currently they are sorted by 120ec002feeSBruce M Simpson * declaration order. 121ec002feeSBruce M Simpson */ 12231b1bfe1SHajimu UMEMOTO static void if_attachdomain(void *); 12331b1bfe1SHajimu UMEMOTO static void if_attachdomain1(struct ifnet *); 1240b59d917SJonathan Lemon static int ifconf(u_long, caddr_t); 125ec002feeSBruce M Simpson static void if_freemulti(struct ifmultiaddr *); 126f9132cebSJonathan Lemon static void if_init(void *); 127eb322a6fSJohn Baldwin static void if_check(void *); 1288614fb12SMax Laier static void if_route(struct ifnet *, int flag, int fam); 1291a3b6859SYaroslav Tykhiy static int if_setflag(struct ifnet *, int, int, int *, int); 1300b59d917SJonathan Lemon static void if_slowtimo(void *); 131db7f0b97SKip Macy static int if_transmit(struct ifnet *ifp, struct mbuf *m); 1328614fb12SMax Laier static void if_unroute(struct ifnet *, int flag, int fam); 1338071913dSRuslan Ermilov static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 1340b59d917SJonathan Lemon static int if_rtdel(struct radix_node *, void *); 135f13ad206SJonathan Lemon static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *); 136ec002feeSBruce M Simpson static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int); 13768a3482fSGleb Smirnoff static void do_link_state_change(void *, int); 1380dad3f0eSMax Laier static int if_getgroup(struct ifgroupreq *, struct ifnet *); 1390dad3f0eSMax Laier static int if_getgroupmembers(struct ifgroupreq *); 1408623f9fdSMax Laier static void if_delgroups(struct ifnet *); 141e0c14af9SMarko Zec static void if_attach_internal(struct ifnet *, int); 142e0c14af9SMarko Zec static void if_detach_internal(struct ifnet *, int); 143db7f0b97SKip Macy 14482cd038dSYoshinobu Inoue #ifdef INET6 14582cd038dSYoshinobu Inoue /* 14682cd038dSYoshinobu Inoue * XXX: declare here to avoid to include many inet6 related files.. 14782cd038dSYoshinobu Inoue * should be more generalized? 14882cd038dSYoshinobu Inoue */ 149929ddbbbSAlfred Perlstein extern void nd6_setmtu(struct ifnet *); 15082cd038dSYoshinobu Inoue #endif 15182cd038dSYoshinobu Inoue 152eddfbb76SRobert Watson VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */ 153eddfbb76SRobert Watson VNET_DEFINE(struct ifgrouphead, ifg_head); 154eddfbb76SRobert Watson VNET_DEFINE(int, if_index); 155eddfbb76SRobert Watson static VNET_DEFINE(int, if_indexlim) = 8; 156eddfbb76SRobert Watson 15777dfcdc4SRobert Watson /* Table of ifnet by index. */ 158eddfbb76SRobert Watson static VNET_DEFINE(struct ifindex_entry *, ifindex_table); 159eddfbb76SRobert Watson 1601e77c105SRobert Watson #define V_if_indexlim VNET(if_indexlim) 1611e77c105SRobert Watson #define V_ifindex_table VNET(ifindex_table) 16244e33a07SMarko Zec 16344e33a07SMarko Zec int ifqmaxlen = IFQ_MAXLEN; 16477dfcdc4SRobert Watson 16577dfcdc4SRobert Watson /* 16677dfcdc4SRobert Watson * The global network interface list (V_ifnet) and related state (such as 16777dfcdc4SRobert Watson * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and 16877dfcdc4SRobert Watson * an rwlock. Either may be acquired shared to stablize the list, but both 16977dfcdc4SRobert Watson * must be acquired writable to modify the list. This model allows us to 17077dfcdc4SRobert Watson * both stablize the interface list during interrupt thread processing, but 17177dfcdc4SRobert Watson * also to stablize it over long-running ioctls, without introducing priority 17277dfcdc4SRobert Watson * inversions and deadlocks. 17377dfcdc4SRobert Watson */ 17477dfcdc4SRobert Watson struct rwlock ifnet_rwlock; 17577dfcdc4SRobert Watson struct sx ifnet_sxlock; 17677dfcdc4SRobert Watson 177fc74a9f9SBrooks Davis static if_com_alloc_t *if_com_alloc[256]; 178fc74a9f9SBrooks Davis static if_com_free_t *if_com_free[256]; 1790b59d917SJonathan Lemon 1800b59d917SJonathan Lemon /* 1810b59d917SJonathan Lemon * System initialization 1820b59d917SJonathan Lemon */ 183eb322a6fSJohn Baldwin SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL); 1840b59d917SJonathan Lemon 185fc74a9f9SBrooks Davis MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals"); 1860b59d917SJonathan Lemon MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 1870b59d917SJonathan Lemon MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 18830aad87dSBrooks Davis 18921ca7b57SMarko Zec struct ifnet * 190d24c444cSKip Macy ifnet_byindex_locked(u_short idx) 19102f4879dSRobert Watson { 19202f4879dSRobert Watson 19327d37320SRobert Watson if (idx > V_if_index) 19427d37320SRobert Watson return (NULL); 19527d37320SRobert Watson return (V_ifindex_table[idx].ife_ifnet); 196d24c444cSKip Macy } 197d24c444cSKip Macy 198d24c444cSKip Macy struct ifnet * 199d24c444cSKip Macy ifnet_byindex(u_short idx) 200d24c444cSKip Macy { 201d24c444cSKip Macy struct ifnet *ifp; 202d24c444cSKip Macy 20377dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 204d24c444cSKip Macy ifp = ifnet_byindex_locked(idx); 20577dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 20602f4879dSRobert Watson return (ifp); 20702f4879dSRobert Watson } 20802f4879dSRobert Watson 20927d37320SRobert Watson struct ifnet * 21027d37320SRobert Watson ifnet_byindex_ref(u_short idx) 21127d37320SRobert Watson { 21227d37320SRobert Watson struct ifnet *ifp; 21327d37320SRobert Watson 21477dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 21527d37320SRobert Watson ifp = ifnet_byindex_locked(idx); 216242a8e72SRobert Watson if (ifp == NULL || (ifp->if_flags & IFF_DYING)) { 21777dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 21827d37320SRobert Watson return (NULL); 21927d37320SRobert Watson } 22027d37320SRobert Watson if_ref(ifp); 22177dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 22227d37320SRobert Watson return (ifp); 22327d37320SRobert Watson } 22427d37320SRobert Watson 225e0c14af9SMarko Zec static void 22677dfcdc4SRobert Watson ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp) 22702f4879dSRobert Watson { 22802f4879dSRobert Watson 22902f4879dSRobert Watson IFNET_WLOCK_ASSERT(); 23002f4879dSRobert Watson 231603724d3SBjoern A. Zeeb V_ifindex_table[idx].ife_ifnet = ifp; 23202f4879dSRobert Watson } 23302f4879dSRobert Watson 23477dfcdc4SRobert Watson static void 23577dfcdc4SRobert Watson ifnet_setbyindex(u_short idx, struct ifnet *ifp) 23677dfcdc4SRobert Watson { 23777dfcdc4SRobert Watson 23877dfcdc4SRobert Watson IFNET_WLOCK(); 23977dfcdc4SRobert Watson ifnet_setbyindex_locked(idx, ifp); 24077dfcdc4SRobert Watson IFNET_WUNLOCK(); 24177dfcdc4SRobert Watson } 24277dfcdc4SRobert Watson 24302f4879dSRobert Watson struct ifaddr * 24402f4879dSRobert Watson ifaddr_byindex(u_short idx) 24502f4879dSRobert Watson { 24602f4879dSRobert Watson struct ifaddr *ifa; 24702f4879dSRobert Watson 24877dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 249d24c444cSKip Macy ifa = ifnet_byindex_locked(idx)->if_addr; 2508c0fec80SRobert Watson if (ifa != NULL) 2518c0fec80SRobert Watson ifa_ref(ifa); 25277dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 25302f4879dSRobert Watson return (ifa); 25402f4879dSRobert Watson } 25502f4879dSRobert Watson 256df8bae1dSRodney W. Grimes /* 257df8bae1dSRodney W. Grimes * Network interface utility routines. 258df8bae1dSRodney W. Grimes * 259df8bae1dSRodney W. Grimes * Routines with ifa_ifwith* names take sockaddr *'s as 260df8bae1dSRodney W. Grimes * parameters. 261df8bae1dSRodney W. Grimes */ 262a45cbf12SBrooks Davis 263f9132cebSJonathan Lemon static void 264d0728d71SRobert Watson vnet_if_init(const void *unused __unused) 2651ed81b73SMarko Zec { 26644e33a07SMarko Zec 267603724d3SBjoern A. Zeeb TAILQ_INIT(&V_ifnet); 268603724d3SBjoern A. Zeeb TAILQ_INIT(&V_ifg_head); 269f9132cebSJonathan Lemon if_grow(); /* create initial table */ 270d0728d71SRobert Watson vnet_if_clone_init(); 271f9132cebSJonathan Lemon } 272d0728d71SRobert Watson VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_FIRST, vnet_if_init, 273d0728d71SRobert Watson NULL); 274d0728d71SRobert Watson 275d0728d71SRobert Watson /* ARGSUSED*/ 276d0728d71SRobert Watson static void 277d0728d71SRobert Watson if_init(void *dummy __unused) 278d0728d71SRobert Watson { 279d0728d71SRobert Watson 280d0728d71SRobert Watson IFNET_LOCK_INIT(); 281d0728d71SRobert Watson if_clone_init(); 282d0728d71SRobert Watson } 283d0728d71SRobert Watson SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_SECOND, if_init, NULL); 284d0728d71SRobert Watson 285f9132cebSJonathan Lemon 286bc29160dSMarko Zec #ifdef VIMAGE 287d0728d71SRobert Watson static void 288d0728d71SRobert Watson vnet_if_uninit(const void *unused __unused) 289bc29160dSMarko Zec { 290bc29160dSMarko Zec 291bc29160dSMarko Zec VNET_ASSERT(TAILQ_EMPTY(&V_ifnet)); 292bc29160dSMarko Zec VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head)); 293bc29160dSMarko Zec 294bc29160dSMarko Zec free((caddr_t)V_ifindex_table, M_IFNET); 295bc29160dSMarko Zec } 296d0728d71SRobert Watson VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST, 297d0728d71SRobert Watson vnet_if_uninit, NULL); 298bc29160dSMarko Zec #endif 299bc29160dSMarko Zec 30021ca7b57SMarko Zec void 301f9132cebSJonathan Lemon if_grow(void) 302f9132cebSJonathan Lemon { 303f9132cebSJonathan Lemon u_int n; 304f9132cebSJonathan Lemon struct ifindex_entry *e; 305f9132cebSJonathan Lemon 306603724d3SBjoern A. Zeeb V_if_indexlim <<= 1; 307603724d3SBjoern A. Zeeb n = V_if_indexlim * sizeof(*e); 308fc74a9f9SBrooks Davis e = malloc(n, M_IFNET, M_WAITOK | M_ZERO); 309603724d3SBjoern A. Zeeb if (V_ifindex_table != NULL) { 310603724d3SBjoern A. Zeeb memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2); 311603724d3SBjoern A. Zeeb free((caddr_t)V_ifindex_table, M_IFNET); 312f9132cebSJonathan Lemon } 313603724d3SBjoern A. Zeeb V_ifindex_table = e; 314f9132cebSJonathan Lemon } 315f9132cebSJonathan Lemon 316eb322a6fSJohn Baldwin static void 317eb322a6fSJohn Baldwin if_check(void *dummy __unused) 318eb322a6fSJohn Baldwin { 319eb322a6fSJohn Baldwin 320eb322a6fSJohn Baldwin /* 321eb322a6fSJohn Baldwin * If at least one interface added during boot uses 322eb322a6fSJohn Baldwin * if_watchdog then start the timer. 323eb322a6fSJohn Baldwin */ 324eb322a6fSJohn Baldwin if (slowtimo_started) 325eb322a6fSJohn Baldwin if_slowtimo(0); 326eb322a6fSJohn Baldwin } 327eb322a6fSJohn Baldwin 328df8bae1dSRodney W. Grimes /* 329a45cbf12SBrooks Davis * Allocate a struct ifnet and an index for an interface. A layer 2 330a45cbf12SBrooks Davis * common structure will also be allocated if an allocation routine is 331a45cbf12SBrooks Davis * registered for the passed type. 332fc74a9f9SBrooks Davis */ 333fc74a9f9SBrooks Davis struct ifnet * 334fc74a9f9SBrooks Davis if_alloc(u_char type) 335fc74a9f9SBrooks Davis { 336fc74a9f9SBrooks Davis struct ifnet *ifp; 337fc74a9f9SBrooks Davis 338fc74a9f9SBrooks Davis ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO); 339fc74a9f9SBrooks Davis 340dc7c539eSBrooks Davis /* 341dc7c539eSBrooks Davis * Try to find an empty slot below if_index. If we fail, take 342dc7c539eSBrooks Davis * the next slot. 343dc7c539eSBrooks Davis * 344dc7c539eSBrooks Davis * XXX: should be locked! 345dc7c539eSBrooks Davis */ 346603724d3SBjoern A. Zeeb for (ifp->if_index = 1; ifp->if_index <= V_if_index; ifp->if_index++) { 347dc7c539eSBrooks Davis if (ifnet_byindex(ifp->if_index) == NULL) 348dc7c539eSBrooks Davis break; 349dc7c539eSBrooks Davis } 350dc7c539eSBrooks Davis /* Catch if_index overflow. */ 351dc7c539eSBrooks Davis if (ifp->if_index < 1) { 352dc7c539eSBrooks Davis free(ifp, M_IFNET); 353dc7c539eSBrooks Davis return (NULL); 354dc7c539eSBrooks Davis } 355603724d3SBjoern A. Zeeb if (ifp->if_index > V_if_index) 356603724d3SBjoern A. Zeeb V_if_index = ifp->if_index; 357603724d3SBjoern A. Zeeb if (V_if_index >= V_if_indexlim) 358fc74a9f9SBrooks Davis if_grow(); 359fc74a9f9SBrooks Davis 360fc74a9f9SBrooks Davis ifp->if_type = type; 36127d37320SRobert Watson ifp->if_alloctype = type; 362fc74a9f9SBrooks Davis 363fc74a9f9SBrooks Davis if (if_com_alloc[type] != NULL) { 364fc74a9f9SBrooks Davis ifp->if_l2com = if_com_alloc[type](type, ifp); 36528ef2db4SBrooks Davis if (ifp->if_l2com == NULL) { 366fc74a9f9SBrooks Davis free(ifp, M_IFNET); 36728ef2db4SBrooks Davis return (NULL); 36828ef2db4SBrooks Davis } 369fc74a9f9SBrooks Davis } 37027d37320SRobert Watson 37127d37320SRobert Watson IF_ADDR_LOCK_INIT(ifp); 372d6f157eaSRobert Watson TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp); 373d6f157eaSRobert Watson ifp->if_afdata_initialized = 0; 374e0c14af9SMarko Zec IF_AFDATA_LOCK_INIT(ifp); 375d6f157eaSRobert Watson TAILQ_INIT(&ifp->if_addrhead); 376d6f157eaSRobert Watson TAILQ_INIT(&ifp->if_prefixhead); 377d6f157eaSRobert Watson TAILQ_INIT(&ifp->if_multiaddrs); 378d6f157eaSRobert Watson TAILQ_INIT(&ifp->if_groups); 379d6f157eaSRobert Watson #ifdef MAC 380d6f157eaSRobert Watson mac_ifnet_init(ifp); 381d6f157eaSRobert Watson #endif 382d659538fSSam Leffler ifq_init(&ifp->if_snd, ifp); 383d6f157eaSRobert Watson 38427d37320SRobert Watson refcount_init(&ifp->if_refcount, 1); /* Index reference. */ 38502f4879dSRobert Watson ifnet_setbyindex(ifp->if_index, ifp); 386fc74a9f9SBrooks Davis return (ifp); 387fc74a9f9SBrooks Davis } 388fc74a9f9SBrooks Davis 389a45cbf12SBrooks Davis /* 390242a8e72SRobert Watson * Do the actual work of freeing a struct ifnet, associated index, and layer 391242a8e72SRobert Watson * 2 common structure. This call is made when the last reference to an 392242a8e72SRobert Watson * interface is released. 393a45cbf12SBrooks Davis */ 394242a8e72SRobert Watson static void 395242a8e72SRobert Watson if_free_internal(struct ifnet *ifp) 396fc74a9f9SBrooks Davis { 397fc74a9f9SBrooks Davis 398242a8e72SRobert Watson KASSERT((ifp->if_flags & IFF_DYING), 399242a8e72SRobert Watson ("if_free_internal: interface not dying")); 400fc74a9f9SBrooks Davis 40102f4879dSRobert Watson IFNET_WLOCK(); 40227d37320SRobert Watson KASSERT(ifp == ifnet_byindex_locked(ifp->if_index), 40327d37320SRobert Watson ("%s: freeing unallocated ifnet", ifp->if_xname)); 404242a8e72SRobert Watson 40577dfcdc4SRobert Watson ifnet_setbyindex_locked(ifp->if_index, NULL); 406d24c444cSKip Macy while (V_if_index > 0 && ifnet_byindex_locked(V_if_index) == NULL) 407603724d3SBjoern A. Zeeb V_if_index--; 40802f4879dSRobert Watson IFNET_WUNLOCK(); 40928ef2db4SBrooks Davis 41027d37320SRobert Watson if (if_com_free[ifp->if_alloctype] != NULL) 41127d37320SRobert Watson if_com_free[ifp->if_alloctype](ifp->if_l2com, 41227d37320SRobert Watson ifp->if_alloctype); 413fc74a9f9SBrooks Davis 414d6f157eaSRobert Watson #ifdef MAC 415d6f157eaSRobert Watson mac_ifnet_destroy(ifp); 416d6f157eaSRobert Watson #endif /* MAC */ 417d6f157eaSRobert Watson IF_AFDATA_DESTROY(ifp); 41802f4879dSRobert Watson IF_ADDR_LOCK_DESTROY(ifp); 419d659538fSSam Leffler ifq_delete(&ifp->if_snd); 420fc74a9f9SBrooks Davis free(ifp, M_IFNET); 421c0c9ea90SSam Leffler } 422fc74a9f9SBrooks Davis 423242a8e72SRobert Watson /* 424242a8e72SRobert Watson * This version should only be called by intefaces that switch their type 425242a8e72SRobert Watson * after calling if_alloc(). if_free_type() will go away again now that we 426242a8e72SRobert Watson * have if_alloctype to cache the original allocation type. For now, assert 427242a8e72SRobert Watson * that they match, since we require that in practice. 428242a8e72SRobert Watson */ 429242a8e72SRobert Watson void 430242a8e72SRobert Watson if_free_type(struct ifnet *ifp, u_char type) 431242a8e72SRobert Watson { 432242a8e72SRobert Watson 433242a8e72SRobert Watson KASSERT(ifp->if_alloctype == type, 434242a8e72SRobert Watson ("if_free_type: type (%d) != alloctype (%d)", type, 435242a8e72SRobert Watson ifp->if_alloctype)); 436242a8e72SRobert Watson 437242a8e72SRobert Watson ifp->if_flags |= IFF_DYING; /* XXX: Locking */ 438242a8e72SRobert Watson if (!refcount_release(&ifp->if_refcount)) 439242a8e72SRobert Watson return; 440242a8e72SRobert Watson if_free_internal(ifp); 441242a8e72SRobert Watson } 442242a8e72SRobert Watson 443242a8e72SRobert Watson /* 444242a8e72SRobert Watson * This is the normal version of if_free(), used by device drivers to free a 445242a8e72SRobert Watson * detached network interface. The contents of if_free_type() will move into 446242a8e72SRobert Watson * here when if_free_type() goes away. 447242a8e72SRobert Watson */ 448242a8e72SRobert Watson void 449242a8e72SRobert Watson if_free(struct ifnet *ifp) 450242a8e72SRobert Watson { 451242a8e72SRobert Watson 452242a8e72SRobert Watson if_free_type(ifp, ifp->if_alloctype); 453242a8e72SRobert Watson } 454242a8e72SRobert Watson 455242a8e72SRobert Watson /* 456242a8e72SRobert Watson * Interfaces to keep an ifnet type-stable despite the possibility of the 457242a8e72SRobert Watson * driver calling if_free(). If there are additional references, we defer 458242a8e72SRobert Watson * freeing the underlying data structure. 459242a8e72SRobert Watson */ 460db7f0b97SKip Macy void 46127d37320SRobert Watson if_ref(struct ifnet *ifp) 46227d37320SRobert Watson { 46327d37320SRobert Watson 46427d37320SRobert Watson /* We don't assert the ifnet list lock here, but arguably should. */ 46527d37320SRobert Watson refcount_acquire(&ifp->if_refcount); 46627d37320SRobert Watson } 46727d37320SRobert Watson 46827d37320SRobert Watson void 46927d37320SRobert Watson if_rele(struct ifnet *ifp) 47027d37320SRobert Watson { 47127d37320SRobert Watson 472242a8e72SRobert Watson if (!refcount_release(&ifp->if_refcount)) 473242a8e72SRobert Watson return; 474242a8e72SRobert Watson if_free_internal(ifp); 47527d37320SRobert Watson } 47627d37320SRobert Watson 47727d37320SRobert Watson void 478d659538fSSam Leffler ifq_init(struct ifaltq *ifq, struct ifnet *ifp) 479db7f0b97SKip Macy { 480db7f0b97SKip Macy 481db7f0b97SKip Macy mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF); 482db7f0b97SKip Macy 483db7f0b97SKip Macy if (ifq->ifq_maxlen == 0) 484db7f0b97SKip Macy ifq->ifq_maxlen = ifqmaxlen; 485db7f0b97SKip Macy 486db7f0b97SKip Macy ifq->altq_type = 0; 487db7f0b97SKip Macy ifq->altq_disc = NULL; 488db7f0b97SKip Macy ifq->altq_flags &= ALTQF_CANTCHANGE; 489db7f0b97SKip Macy ifq->altq_tbr = NULL; 490db7f0b97SKip Macy ifq->altq_ifp = ifp; 491db7f0b97SKip Macy } 492db7f0b97SKip Macy 493db7f0b97SKip Macy void 494d659538fSSam Leffler ifq_delete(struct ifaltq *ifq) 495db7f0b97SKip Macy { 496db7f0b97SKip Macy mtx_destroy(&ifq->ifq_mtx); 497db7f0b97SKip Macy } 498db7f0b97SKip Macy 499fc74a9f9SBrooks Davis /* 500a45cbf12SBrooks Davis * Perform generic interface initalization tasks and attach the interface 501e0c14af9SMarko Zec * to the list of "active" interfaces. If vmove flag is set on entry 502e0c14af9SMarko Zec * to if_attach_internal(), perform only a limited subset of initialization 503e0c14af9SMarko Zec * tasks, given that we are moving from one vnet to another an ifnet which 504e0c14af9SMarko Zec * has already been fully initialized. 505a45cbf12SBrooks Davis * 506a45cbf12SBrooks Davis * XXX: 507a45cbf12SBrooks Davis * - The decision to return void and thus require this function to 508a45cbf12SBrooks Davis * succeed is questionable. 509a45cbf12SBrooks Davis * - We should probably do more sanity checking. For instance we don't 510a45cbf12SBrooks Davis * do anything to insure if_xname is unique or non-empty. 511df8bae1dSRodney W. Grimes */ 512df8bae1dSRodney W. Grimes void 51372fd1b6aSDag-Erling Smørgrav if_attach(struct ifnet *ifp) 514df8bae1dSRodney W. Grimes { 515e0c14af9SMarko Zec 516e0c14af9SMarko Zec if_attach_internal(ifp, 0); 517e0c14af9SMarko Zec } 518e0c14af9SMarko Zec 519e0c14af9SMarko Zec static void 520e0c14af9SMarko Zec if_attach_internal(struct ifnet *ifp, int vmove) 521e0c14af9SMarko Zec { 522df8bae1dSRodney W. Grimes unsigned socksize, ifasize; 5231ce9bf88SPoul-Henning Kamp int namelen, masklen; 52472fd1b6aSDag-Erling Smørgrav struct sockaddr_dl *sdl; 52572fd1b6aSDag-Erling Smørgrav struct ifaddr *ifa; 526df8bae1dSRodney W. Grimes 527fc74a9f9SBrooks Davis if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index)) 528fc74a9f9SBrooks Davis panic ("%s: BUG: if_attach called without if_alloc'd input()\n", 529fc74a9f9SBrooks Davis ifp->if_xname); 530fc74a9f9SBrooks Davis 531f6dfe47aSMarko Zec #ifdef VIMAGE 532f6dfe47aSMarko Zec ifp->if_vnet = curvnet; 533bc29160dSMarko Zec if (ifp->if_home_vnet == NULL) 534bc29160dSMarko Zec ifp->if_home_vnet = curvnet; 535f6dfe47aSMarko Zec #endif 536f6dfe47aSMarko Zec 5370dad3f0eSMax Laier if_addgroup(ifp, IFG_ALL); 5380dad3f0eSMax Laier 53998b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 540bc9d2991SBrooks Davis ifp->if_data.ifi_epoch = time_uptime; 541fc74a9f9SBrooks Davis ifp->if_data.ifi_datalen = sizeof(struct if_data); 542d6f157eaSRobert Watson 5437cc5b47fSKip Macy KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) || 5447cc5b47fSKip Macy (ifp->if_transmit != NULL && ifp->if_qflush != NULL), 5457cc5b47fSKip Macy ("transmit and qflush must both either be set or both be NULL")); 5467cc5b47fSKip Macy if (ifp->if_transmit == NULL) { 547db7f0b97SKip Macy ifp->if_transmit = if_transmit; 548db7f0b97SKip Macy ifp->if_qflush = if_qflush; 5497cc5b47fSKip Macy } 5507cc5b47fSKip Macy 551e0c14af9SMarko Zec if (!vmove) { 552e70cd263SRobert Watson #ifdef MAC 55330d239bcSRobert Watson mac_ifnet_create(ifp); 554e70cd263SRobert Watson #endif 555e70cd263SRobert Watson 556df8bae1dSRodney W. Grimes /* 557e0c14af9SMarko Zec * Create a Link Level name for this device. 558df8bae1dSRodney W. Grimes */ 5599bf40edeSBrooks Davis namelen = strlen(ifp->if_xname); 56036c19a57SBrooks Davis /* 561e0c14af9SMarko Zec * Always save enough space for any possiable name so we 562e0c14af9SMarko Zec * can do a rename in place later. 56336c19a57SBrooks Davis */ 56436c19a57SBrooks Davis masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ; 565df8bae1dSRodney W. Grimes socksize = masklen + ifp->if_addrlen; 566df8bae1dSRodney W. Grimes if (socksize < sizeof(*sdl)) 567df8bae1dSRodney W. Grimes socksize = sizeof(*sdl); 568ccb82468SBrooks Davis socksize = roundup2(socksize, sizeof(long)); 569df8bae1dSRodney W. Grimes ifasize = sizeof(*ifa) + 2 * socksize; 570a8773564SBrooks Davis ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO); 5711099f828SRobert Watson ifa_init(ifa); 572df8bae1dSRodney W. Grimes sdl = (struct sockaddr_dl *)(ifa + 1); 573df8bae1dSRodney W. Grimes sdl->sdl_len = socksize; 574df8bae1dSRodney W. Grimes sdl->sdl_family = AF_LINK; 5759bf40edeSBrooks Davis bcopy(ifp->if_xname, sdl->sdl_data, namelen); 5761ce9bf88SPoul-Henning Kamp sdl->sdl_nlen = namelen; 577df8bae1dSRodney W. Grimes sdl->sdl_index = ifp->if_index; 578df8bae1dSRodney W. Grimes sdl->sdl_type = ifp->if_type; 5794a0d6638SRuslan Ermilov ifp->if_addr = ifa; 580df8bae1dSRodney W. Grimes ifa->ifa_ifp = ifp; 581df8bae1dSRodney W. Grimes ifa->ifa_rtrequest = link_rtrequest; 582df8bae1dSRodney W. Grimes ifa->ifa_addr = (struct sockaddr *)sdl; 583df8bae1dSRodney W. Grimes sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 584df8bae1dSRodney W. Grimes ifa->ifa_netmask = (struct sockaddr *)sdl; 585df8bae1dSRodney W. Grimes sdl->sdl_len = masklen; 586df8bae1dSRodney W. Grimes while (namelen != 0) 587df8bae1dSRodney W. Grimes sdl->sdl_data[--namelen] = 0xff; 58859562606SGarrett Wollman TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link); 589e0c14af9SMarko Zec /* Reliably crash if used uninitialized. */ 590e0c14af9SMarko Zec ifp->if_broadcastaddr = NULL; 591e0c14af9SMarko Zec } 59252db6805SMarko Zec #ifdef VIMAGE 59352db6805SMarko Zec else { 59452db6805SMarko Zec /* 59552db6805SMarko Zec * Update the interface index in the link layer address 59652db6805SMarko Zec * of the interface. 59752db6805SMarko Zec */ 59852db6805SMarko Zec for (ifa = ifp->if_addr; ifa != NULL; 59952db6805SMarko Zec ifa = TAILQ_NEXT(ifa, ifa_link)) { 60052db6805SMarko Zec if (ifa->ifa_addr->sa_family == AF_LINK) { 60152db6805SMarko Zec sdl = (struct sockaddr_dl *)ifa->ifa_addr; 60252db6805SMarko Zec sdl->sdl_index = ifp->if_index; 60352db6805SMarko Zec } 60452db6805SMarko Zec } 60552db6805SMarko Zec } 60652db6805SMarko Zec #endif 607d94ccb09SBrooks Davis 608457f48e6SGleb Smirnoff IFNET_WLOCK(); 609603724d3SBjoern A. Zeeb TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link); 61021ca7b57SMarko Zec #ifdef VIMAGE 611006e9db4SRobert Watson curvnet->vnet_ifcnt++; 61221ca7b57SMarko Zec #endif 613457f48e6SGleb Smirnoff IFNET_WUNLOCK(); 614457f48e6SGleb Smirnoff 61569fb23b7SMax Laier if (domain_init_status >= 2) 61631b1bfe1SHajimu UMEMOTO if_attachdomain1(ifp); 61731b1bfe1SHajimu UMEMOTO 61825a4adceSMax Laier EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 61921ca7b57SMarko Zec if (IS_DEFAULT_VNET(curvnet)) 620f3b90d48SAndrew Thompson devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL); 62125a4adceSMax Laier 6227b6edd04SRuslan Ermilov /* Announce the interface. */ 6237b6edd04SRuslan Ermilov rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 624c18ffdc8SGleb Smirnoff 625e0c14af9SMarko Zec if (!vmove && ifp->if_watchdog != NULL) { 626b9175c45SRobert Watson if_printf(ifp, 627b9175c45SRobert Watson "WARNING: using obsoleted if_watchdog interface\n"); 628eb322a6fSJohn Baldwin 629eb322a6fSJohn Baldwin /* 630eb322a6fSJohn Baldwin * Note that we need if_slowtimo(). If this happens after 631eb322a6fSJohn Baldwin * boot, then call if_slowtimo() directly. 632eb322a6fSJohn Baldwin */ 633eb322a6fSJohn Baldwin if (atomic_cmpset_int(&slowtimo_started, 0, 1) && !cold) 634eb322a6fSJohn Baldwin if_slowtimo(0); 635eb322a6fSJohn Baldwin } 636df8bae1dSRodney W. Grimes } 6376182fdbdSPeter Wemm 63831b1bfe1SHajimu UMEMOTO static void 63972fd1b6aSDag-Erling Smørgrav if_attachdomain(void *dummy) 64031b1bfe1SHajimu UMEMOTO { 64131b1bfe1SHajimu UMEMOTO struct ifnet *ifp; 64231b1bfe1SHajimu UMEMOTO int s; 64331b1bfe1SHajimu UMEMOTO 64431b1bfe1SHajimu UMEMOTO s = splnet(); 645603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifp, &V_ifnet, if_link) 64631b1bfe1SHajimu UMEMOTO if_attachdomain1(ifp); 64731b1bfe1SHajimu UMEMOTO splx(s); 64831b1bfe1SHajimu UMEMOTO } 64969fb23b7SMax Laier SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND, 65031b1bfe1SHajimu UMEMOTO if_attachdomain, NULL); 65131b1bfe1SHajimu UMEMOTO 65231b1bfe1SHajimu UMEMOTO static void 65372fd1b6aSDag-Erling Smørgrav if_attachdomain1(struct ifnet *ifp) 65431b1bfe1SHajimu UMEMOTO { 65531b1bfe1SHajimu UMEMOTO struct domain *dp; 65631b1bfe1SHajimu UMEMOTO int s; 65731b1bfe1SHajimu UMEMOTO 65831b1bfe1SHajimu UMEMOTO s = splnet(); 65931b1bfe1SHajimu UMEMOTO 660234a35c7SHajimu UMEMOTO /* 661234a35c7SHajimu UMEMOTO * Since dp->dom_ifattach calls malloc() with M_WAITOK, we 662234a35c7SHajimu UMEMOTO * cannot lock ifp->if_afdata initialization, entirely. 663234a35c7SHajimu UMEMOTO */ 664234a35c7SHajimu UMEMOTO if (IF_AFDATA_TRYLOCK(ifp) == 0) { 665234a35c7SHajimu UMEMOTO splx(s); 666234a35c7SHajimu UMEMOTO return; 667234a35c7SHajimu UMEMOTO } 66869fb23b7SMax Laier if (ifp->if_afdata_initialized >= domain_init_status) { 669234a35c7SHajimu UMEMOTO IF_AFDATA_UNLOCK(ifp); 670234a35c7SHajimu UMEMOTO splx(s); 6716237419dSRobert Watson printf("if_attachdomain called more than once on %s\n", 6726237419dSRobert Watson ifp->if_xname); 673234a35c7SHajimu UMEMOTO return; 674234a35c7SHajimu UMEMOTO } 67569fb23b7SMax Laier ifp->if_afdata_initialized = domain_init_status; 676234a35c7SHajimu UMEMOTO IF_AFDATA_UNLOCK(ifp); 677234a35c7SHajimu UMEMOTO 67831b1bfe1SHajimu UMEMOTO /* address family dependent data region */ 67931b1bfe1SHajimu UMEMOTO bzero(ifp->if_afdata, sizeof(ifp->if_afdata)); 68031b1bfe1SHajimu UMEMOTO for (dp = domains; dp; dp = dp->dom_next) { 68131b1bfe1SHajimu UMEMOTO if (dp->dom_ifattach) 68231b1bfe1SHajimu UMEMOTO ifp->if_afdata[dp->dom_family] = 68331b1bfe1SHajimu UMEMOTO (*dp->dom_ifattach)(ifp); 68431b1bfe1SHajimu UMEMOTO } 68531b1bfe1SHajimu UMEMOTO 68631b1bfe1SHajimu UMEMOTO splx(s); 68731b1bfe1SHajimu UMEMOTO } 68831b1bfe1SHajimu UMEMOTO 6896182fdbdSPeter Wemm /* 690ec002feeSBruce M Simpson * Remove any unicast or broadcast network addresses from an interface. 69145778b37SPeter Edwards */ 69245778b37SPeter Edwards void 69345778b37SPeter Edwards if_purgeaddrs(struct ifnet *ifp) 69445778b37SPeter Edwards { 69545778b37SPeter Edwards struct ifaddr *ifa, *next; 69645778b37SPeter Edwards 69745778b37SPeter Edwards TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) { 6984b97d7afSYaroslav Tykhiy if (ifa->ifa_addr->sa_family == AF_LINK) 69945778b37SPeter Edwards continue; 70045778b37SPeter Edwards #ifdef INET 70145778b37SPeter Edwards /* XXX: Ugly!! ad hoc just for INET */ 7024b97d7afSYaroslav Tykhiy if (ifa->ifa_addr->sa_family == AF_INET) { 70345778b37SPeter Edwards struct ifaliasreq ifr; 70445778b37SPeter Edwards 70545778b37SPeter Edwards bzero(&ifr, sizeof(ifr)); 70645778b37SPeter Edwards ifr.ifra_addr = *ifa->ifa_addr; 70745778b37SPeter Edwards if (ifa->ifa_dstaddr) 70845778b37SPeter Edwards ifr.ifra_broadaddr = *ifa->ifa_dstaddr; 70945778b37SPeter Edwards if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp, 71045778b37SPeter Edwards NULL) == 0) 71145778b37SPeter Edwards continue; 71245778b37SPeter Edwards } 71345778b37SPeter Edwards #endif /* INET */ 71445778b37SPeter Edwards #ifdef INET6 7154b97d7afSYaroslav Tykhiy if (ifa->ifa_addr->sa_family == AF_INET6) { 71645778b37SPeter Edwards in6_purgeaddr(ifa); 71745778b37SPeter Edwards /* ifp_addrhead is already updated */ 71845778b37SPeter Edwards continue; 71945778b37SPeter Edwards } 72045778b37SPeter Edwards #endif /* INET6 */ 72145778b37SPeter Edwards TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 7221099f828SRobert Watson ifa_free(ifa); 72345778b37SPeter Edwards } 72445778b37SPeter Edwards } 72545778b37SPeter Edwards 72645778b37SPeter Edwards /* 727ec002feeSBruce M Simpson * Remove any multicast network addresses from an interface. 728ec002feeSBruce M Simpson */ 729fb27dd1dSSam Leffler void 730ec002feeSBruce M Simpson if_purgemaddrs(struct ifnet *ifp) 731ec002feeSBruce M Simpson { 732ec002feeSBruce M Simpson struct ifmultiaddr *ifma; 733ec002feeSBruce M Simpson struct ifmultiaddr *next; 734ec002feeSBruce M Simpson 735ec002feeSBruce M Simpson IF_ADDR_LOCK(ifp); 736ec002feeSBruce M Simpson TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) 737ec002feeSBruce M Simpson if_delmulti_locked(ifp, ifma, 1); 738ec002feeSBruce M Simpson IF_ADDR_UNLOCK(ifp); 739ec002feeSBruce M Simpson } 740ec002feeSBruce M Simpson 741ec002feeSBruce M Simpson /* 742e0c14af9SMarko Zec * Detach an interface, removing it from the list of "active" interfaces. 743e0c14af9SMarko Zec * If vmove flag is set on entry to if_detach_internal(), perform only a 744e0c14af9SMarko Zec * limited subset of cleanup tasks, given that we are moving an ifnet from 745e0c14af9SMarko Zec * one vnet to another, where it must be fully operational. 746b1c53bc9SRobert Watson * 747b1c53bc9SRobert Watson * XXXRW: There are some significant questions about event ordering, and 748b1c53bc9SRobert Watson * how to prevent things from starting to use the interface during detach. 7496182fdbdSPeter Wemm */ 7506182fdbdSPeter Wemm void 75172fd1b6aSDag-Erling Smørgrav if_detach(struct ifnet *ifp) 7526182fdbdSPeter Wemm { 753e0c14af9SMarko Zec 754e0c14af9SMarko Zec if_detach_internal(ifp, 0); 755e0c14af9SMarko Zec } 756e0c14af9SMarko Zec 757e0c14af9SMarko Zec static void 758e0c14af9SMarko Zec if_detach_internal(struct ifnet *ifp, int vmove) 759e0c14af9SMarko Zec { 76045778b37SPeter Edwards struct ifaddr *ifa; 7615500d3beSWarner Losh struct radix_node_head *rnh; 762e0c14af9SMarko Zec int i, j; 76331b1bfe1SHajimu UMEMOTO struct domain *dp; 7643f35d515SPeter Pentchev struct ifnet *iter; 765457f48e6SGleb Smirnoff int found = 0; 766457f48e6SGleb Smirnoff 767457f48e6SGleb Smirnoff IFNET_WLOCK(); 768603724d3SBjoern A. Zeeb TAILQ_FOREACH(iter, &V_ifnet, if_link) 769457f48e6SGleb Smirnoff if (iter == ifp) { 770603724d3SBjoern A. Zeeb TAILQ_REMOVE(&V_ifnet, ifp, if_link); 771457f48e6SGleb Smirnoff found = 1; 772457f48e6SGleb Smirnoff break; 773457f48e6SGleb Smirnoff } 77421ca7b57SMarko Zec #ifdef VIMAGE 77521ca7b57SMarko Zec if (found) 776006e9db4SRobert Watson curvnet->vnet_ifcnt--; 77721ca7b57SMarko Zec #endif 778457f48e6SGleb Smirnoff IFNET_WUNLOCK(); 779e0c14af9SMarko Zec if (!found) { 780e0c14af9SMarko Zec if (vmove) 781e0c14af9SMarko Zec panic("interface not in it's own ifnet list"); 782e0c14af9SMarko Zec else 783e0c14af9SMarko Zec return; /* XXX this should panic as well? */ 784e0c14af9SMarko Zec } 7856182fdbdSPeter Wemm 78668a3482fSGleb Smirnoff /* 78768a3482fSGleb Smirnoff * Remove/wait for pending events. 78868a3482fSGleb Smirnoff */ 78968a3482fSGleb Smirnoff taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 79068a3482fSGleb Smirnoff 7916182fdbdSPeter Wemm /* 7926182fdbdSPeter Wemm * Remove routes and flush queues. 7936182fdbdSPeter Wemm */ 7946182fdbdSPeter Wemm if_down(ifp); 79502b199f1SMax Laier #ifdef ALTQ 79602b199f1SMax Laier if (ALTQ_IS_ENABLED(&ifp->if_snd)) 79702b199f1SMax Laier altq_disable(&ifp->if_snd); 79802b199f1SMax Laier if (ALTQ_IS_ATTACHED(&ifp->if_snd)) 79902b199f1SMax Laier altq_detach(&ifp->if_snd); 80002b199f1SMax Laier #endif 8016182fdbdSPeter Wemm 80245778b37SPeter Edwards if_purgeaddrs(ifp); 8036182fdbdSPeter Wemm 804b1c53bc9SRobert Watson #ifdef INET 805b1c53bc9SRobert Watson in_ifdetach(ifp); 806b1c53bc9SRobert Watson #endif 807b1c53bc9SRobert Watson 80833841545SHajimu UMEMOTO #ifdef INET6 80933841545SHajimu UMEMOTO /* 81033841545SHajimu UMEMOTO * Remove all IPv6 kernel structs related to ifp. This should be done 81133841545SHajimu UMEMOTO * before removing routing entries below, since IPv6 interface direct 81233841545SHajimu UMEMOTO * routes are expected to be removed by the IPv6-specific kernel API. 81333841545SHajimu UMEMOTO * Otherwise, the kernel will detect some inconsistency and bark it. 81433841545SHajimu UMEMOTO */ 81533841545SHajimu UMEMOTO in6_ifdetach(ifp); 81633841545SHajimu UMEMOTO #endif 817ec002feeSBruce M Simpson if_purgemaddrs(ifp); 818ec002feeSBruce M Simpson 819e0c14af9SMarko Zec if (!vmove) { 820f4247b59SLuigi Rizzo /* 821111c6b61SRobert Watson * Prevent further calls into the device driver via ifnet. 822111c6b61SRobert Watson */ 823111c6b61SRobert Watson if_dead(ifp); 824111c6b61SRobert Watson 825111c6b61SRobert Watson /* 8264a0d6638SRuslan Ermilov * Remove link ifaddr pointer and maybe decrement if_index. 827f4247b59SLuigi Rizzo * Clean up all addresses. 828f4247b59SLuigi Rizzo */ 8294a0d6638SRuslan Ermilov ifp->if_addr = NULL; 830f4247b59SLuigi Rizzo 831212bd869SHajimu UMEMOTO /* We can now free link ifaddr. */ 8323f35d515SPeter Pentchev if (!TAILQ_EMPTY(&ifp->if_addrhead)) { 833212bd869SHajimu UMEMOTO ifa = TAILQ_FIRST(&ifp->if_addrhead); 834212bd869SHajimu UMEMOTO TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 8351099f828SRobert Watson ifa_free(ifa); 8363f35d515SPeter Pentchev } 837e0c14af9SMarko Zec } 838212bd869SHajimu UMEMOTO 8395500d3beSWarner Losh /* 8405500d3beSWarner Losh * Delete all remaining routes using this interface 8415500d3beSWarner Losh * Unfortuneatly the only way to do this is to slog through 8425500d3beSWarner Losh * the entire routing table looking for routes which point 8435500d3beSWarner Losh * to this interface...oh well... 8445500d3beSWarner Losh */ 8455500d3beSWarner Losh for (i = 1; i <= AF_MAX; i++) { 8468b07e49aSJulian Elischer for (j = 0; j < rt_numfibs; j++) { 847c2c2a7c1SBjoern A. Zeeb rnh = rt_tables_get_rnh(j, i); 848c2c2a7c1SBjoern A. Zeeb if (rnh == NULL) 8495500d3beSWarner Losh continue; 850956b0b65SJeffrey Hsu RADIX_NODE_HEAD_LOCK(rnh); 8515500d3beSWarner Losh (void) rnh->rnh_walktree(rnh, if_rtdel, ifp); 852956b0b65SJeffrey Hsu RADIX_NODE_HEAD_UNLOCK(rnh); 8535500d3beSWarner Losh } 8548b07e49aSJulian Elischer } 8555500d3beSWarner Losh 8567b6edd04SRuslan Ermilov /* Announce that the interface is gone. */ 8577b6edd04SRuslan Ermilov rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 85852023244SMax Laier EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 85921ca7b57SMarko Zec if (IS_DEFAULT_VNET(curvnet)) 860f3b90d48SAndrew Thompson devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL); 8618623f9fdSMax Laier if_delgroups(ifp); 8627b6edd04SRuslan Ermilov 863234a35c7SHajimu UMEMOTO IF_AFDATA_LOCK(ifp); 86431b1bfe1SHajimu UMEMOTO for (dp = domains; dp; dp = dp->dom_next) { 86531b1bfe1SHajimu UMEMOTO if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) 86631b1bfe1SHajimu UMEMOTO (*dp->dom_ifdetach)(ifp, 86731b1bfe1SHajimu UMEMOTO ifp->if_afdata[dp->dom_family]); 86831b1bfe1SHajimu UMEMOTO } 869e0c14af9SMarko Zec ifp->if_afdata_initialized = 0; 87067da1f3dSMarko Zec IF_AFDATA_UNLOCK(ifp); 8715500d3beSWarner Losh } 8725500d3beSWarner Losh 873e0c14af9SMarko Zec #ifdef VIMAGE 874e0c14af9SMarko Zec /* 875e0c14af9SMarko Zec * if_vmove() performs a limited version of if_detach() in current 876e0c14af9SMarko Zec * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg. 877e0c14af9SMarko Zec * An attempt is made to shrink if_index in current vnet, find an 878e0c14af9SMarko Zec * unused if_index in target vnet and calls if_grow() if necessary, 879e0c14af9SMarko Zec * and finally find an unused if_xname for the target vnet. 880e0c14af9SMarko Zec */ 881e0c14af9SMarko Zec void 882e0c14af9SMarko Zec if_vmove(struct ifnet *ifp, struct vnet *new_vnet) 883e0c14af9SMarko Zec { 884e0c14af9SMarko Zec 885e0c14af9SMarko Zec /* 886e0c14af9SMarko Zec * Detach from current vnet, but preserve LLADDR info, do not 887e0c14af9SMarko Zec * mark as dead etc. so that the ifnet can be reattached later. 888e0c14af9SMarko Zec */ 889e0c14af9SMarko Zec if_detach_internal(ifp, 1); 890e0c14af9SMarko Zec 891e0c14af9SMarko Zec /* 89277dfcdc4SRobert Watson * Unlink the ifnet from ifindex_table[] in current vnet, and shrink 89377dfcdc4SRobert Watson * the if_index for that vnet if possible. 89477dfcdc4SRobert Watson * 89577dfcdc4SRobert Watson * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized, 89677dfcdc4SRobert Watson * or we'd lock on one vnet and unlock on another. 897e0c14af9SMarko Zec */ 898e0c14af9SMarko Zec IFNET_WLOCK(); 89977dfcdc4SRobert Watson ifnet_setbyindex_locked(ifp->if_index, NULL); 900eddfbb76SRobert Watson while (V_if_index > 0 && ifnet_byindex_locked(V_if_index) == NULL) 901e0c14af9SMarko Zec V_if_index--; 902e0c14af9SMarko Zec IFNET_WUNLOCK(); 903e0c14af9SMarko Zec 904e0c14af9SMarko Zec /* 905e0c14af9SMarko Zec * Switch to the context of the target vnet. 906e0c14af9SMarko Zec */ 907e0c14af9SMarko Zec CURVNET_SET_QUIET(new_vnet); 908e0c14af9SMarko Zec 909e0c14af9SMarko Zec /* 910e0c14af9SMarko Zec * Try to find an empty slot below if_index. If we fail, take 911e0c14af9SMarko Zec * the next slot. 912e0c14af9SMarko Zec */ 913e0c14af9SMarko Zec IFNET_WLOCK(); 914e0c14af9SMarko Zec for (ifp->if_index = 1; ifp->if_index <= V_if_index; ifp->if_index++) { 915e0c14af9SMarko Zec if (ifnet_byindex_locked(ifp->if_index) == NULL) 916e0c14af9SMarko Zec break; 917e0c14af9SMarko Zec } 918e0c14af9SMarko Zec /* Catch if_index overflow. */ 919e0c14af9SMarko Zec if (ifp->if_index < 1) 920e0c14af9SMarko Zec panic("if_index overflow"); 921e0c14af9SMarko Zec 922e0c14af9SMarko Zec if (ifp->if_index > V_if_index) 923e0c14af9SMarko Zec V_if_index = ifp->if_index; 924e0c14af9SMarko Zec if (V_if_index >= V_if_indexlim) 925e0c14af9SMarko Zec if_grow(); 92677dfcdc4SRobert Watson ifnet_setbyindex_locked(ifp->if_index, ifp); 927e0c14af9SMarko Zec IFNET_WUNLOCK(); 928e0c14af9SMarko Zec 929e0c14af9SMarko Zec if_attach_internal(ifp, 1); 930e0c14af9SMarko Zec 931e0c14af9SMarko Zec CURVNET_RESTORE(); 932e0c14af9SMarko Zec } 933be31e5e7SBjoern A. Zeeb 934be31e5e7SBjoern A. Zeeb /* 935be31e5e7SBjoern A. Zeeb * Move an ifnet to or from another child prison/vnet, specified by the jail id. 936be31e5e7SBjoern A. Zeeb */ 937be31e5e7SBjoern A. Zeeb static int 938be31e5e7SBjoern A. Zeeb if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid) 939be31e5e7SBjoern A. Zeeb { 940be31e5e7SBjoern A. Zeeb struct prison *pr; 941be31e5e7SBjoern A. Zeeb struct ifnet *difp; 942be31e5e7SBjoern A. Zeeb 943be31e5e7SBjoern A. Zeeb /* Try to find the prison within our visibility. */ 944be31e5e7SBjoern A. Zeeb sx_slock(&allprison_lock); 945be31e5e7SBjoern A. Zeeb pr = prison_find_child(td->td_ucred->cr_prison, jid); 946be31e5e7SBjoern A. Zeeb sx_sunlock(&allprison_lock); 947be31e5e7SBjoern A. Zeeb if (pr == NULL) 948be31e5e7SBjoern A. Zeeb return (ENXIO); 949be31e5e7SBjoern A. Zeeb prison_hold_locked(pr); 950be31e5e7SBjoern A. Zeeb mtx_unlock(&pr->pr_mtx); 951be31e5e7SBjoern A. Zeeb 952be31e5e7SBjoern A. Zeeb /* Do not try to move the iface from and to the same prison. */ 953be31e5e7SBjoern A. Zeeb if (pr->pr_vnet == ifp->if_vnet) { 954be31e5e7SBjoern A. Zeeb prison_free(pr); 955be31e5e7SBjoern A. Zeeb return (EEXIST); 956be31e5e7SBjoern A. Zeeb } 957be31e5e7SBjoern A. Zeeb 958be31e5e7SBjoern A. Zeeb /* Make sure the named iface does not exists in the dst. prison/vnet. */ 959be31e5e7SBjoern A. Zeeb /* XXX Lock interfaces to avoid races. */ 9609abb4862SMarko Zec CURVNET_SET_QUIET(pr->pr_vnet); 961be31e5e7SBjoern A. Zeeb difp = ifunit(ifname); 962be31e5e7SBjoern A. Zeeb CURVNET_RESTORE(); 963be31e5e7SBjoern A. Zeeb if (difp != NULL) { 964be31e5e7SBjoern A. Zeeb prison_free(pr); 965be31e5e7SBjoern A. Zeeb return (EEXIST); 966be31e5e7SBjoern A. Zeeb } 967be31e5e7SBjoern A. Zeeb 968be31e5e7SBjoern A. Zeeb /* Move the interface into the child jail/vnet. */ 969be31e5e7SBjoern A. Zeeb if_vmove(ifp, pr->pr_vnet); 970be31e5e7SBjoern A. Zeeb 971be31e5e7SBjoern A. Zeeb /* Report the new if_xname back to the userland. */ 972be31e5e7SBjoern A. Zeeb sprintf(ifname, "%s", ifp->if_xname); 973be31e5e7SBjoern A. Zeeb 974be31e5e7SBjoern A. Zeeb prison_free(pr); 975be31e5e7SBjoern A. Zeeb return (0); 976be31e5e7SBjoern A. Zeeb } 977be31e5e7SBjoern A. Zeeb 978be31e5e7SBjoern A. Zeeb static int 979be31e5e7SBjoern A. Zeeb if_vmove_reclaim(struct thread *td, char *ifname, int jid) 980be31e5e7SBjoern A. Zeeb { 981be31e5e7SBjoern A. Zeeb struct prison *pr; 982be31e5e7SBjoern A. Zeeb struct vnet *vnet_dst; 983be31e5e7SBjoern A. Zeeb struct ifnet *ifp; 984be31e5e7SBjoern A. Zeeb 985be31e5e7SBjoern A. Zeeb /* Try to find the prison within our visibility. */ 986be31e5e7SBjoern A. Zeeb sx_slock(&allprison_lock); 987be31e5e7SBjoern A. Zeeb pr = prison_find_child(td->td_ucred->cr_prison, jid); 988be31e5e7SBjoern A. Zeeb sx_sunlock(&allprison_lock); 989be31e5e7SBjoern A. Zeeb if (pr == NULL) 990be31e5e7SBjoern A. Zeeb return (ENXIO); 991be31e5e7SBjoern A. Zeeb prison_hold_locked(pr); 992be31e5e7SBjoern A. Zeeb mtx_unlock(&pr->pr_mtx); 993be31e5e7SBjoern A. Zeeb 994be31e5e7SBjoern A. Zeeb /* Make sure the named iface exists in the source prison/vnet. */ 995be31e5e7SBjoern A. Zeeb CURVNET_SET(pr->pr_vnet); 996be31e5e7SBjoern A. Zeeb ifp = ifunit(ifname); /* XXX Lock to avoid races. */ 997be31e5e7SBjoern A. Zeeb if (ifp == NULL) { 998be31e5e7SBjoern A. Zeeb CURVNET_RESTORE(); 999be31e5e7SBjoern A. Zeeb prison_free(pr); 1000be31e5e7SBjoern A. Zeeb return (ENXIO); 1001be31e5e7SBjoern A. Zeeb } 1002be31e5e7SBjoern A. Zeeb 1003be31e5e7SBjoern A. Zeeb /* Do not try to move the iface from and to the same prison. */ 1004be31e5e7SBjoern A. Zeeb vnet_dst = TD_TO_VNET(td); 1005be31e5e7SBjoern A. Zeeb if (vnet_dst == ifp->if_vnet) { 1006be31e5e7SBjoern A. Zeeb CURVNET_RESTORE(); 1007be31e5e7SBjoern A. Zeeb prison_free(pr); 1008be31e5e7SBjoern A. Zeeb return (EEXIST); 1009be31e5e7SBjoern A. Zeeb } 1010be31e5e7SBjoern A. Zeeb 1011be31e5e7SBjoern A. Zeeb /* Get interface back from child jail/vnet. */ 1012be31e5e7SBjoern A. Zeeb if_vmove(ifp, vnet_dst); 1013be31e5e7SBjoern A. Zeeb CURVNET_RESTORE(); 1014be31e5e7SBjoern A. Zeeb 1015be31e5e7SBjoern A. Zeeb /* Report the new if_xname back to the userland. */ 1016be31e5e7SBjoern A. Zeeb sprintf(ifname, "%s", ifp->if_xname); 1017be31e5e7SBjoern A. Zeeb 1018be31e5e7SBjoern A. Zeeb prison_free(pr); 1019be31e5e7SBjoern A. Zeeb return (0); 1020be31e5e7SBjoern A. Zeeb } 1021e0c14af9SMarko Zec #endif /* VIMAGE */ 1022e0c14af9SMarko Zec 10235500d3beSWarner Losh /* 10240dad3f0eSMax Laier * Add a group to an interface 10250dad3f0eSMax Laier */ 10260dad3f0eSMax Laier int 10270dad3f0eSMax Laier if_addgroup(struct ifnet *ifp, const char *groupname) 10280dad3f0eSMax Laier { 10290dad3f0eSMax Laier struct ifg_list *ifgl; 10300dad3f0eSMax Laier struct ifg_group *ifg = NULL; 10310dad3f0eSMax Laier struct ifg_member *ifgm; 10320dad3f0eSMax Laier 10330dad3f0eSMax Laier if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' && 10340dad3f0eSMax Laier groupname[strlen(groupname) - 1] <= '9') 10350dad3f0eSMax Laier return (EINVAL); 10360dad3f0eSMax Laier 10370dad3f0eSMax Laier IFNET_WLOCK(); 10380dad3f0eSMax Laier TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 10390dad3f0eSMax Laier if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) { 10400dad3f0eSMax Laier IFNET_WUNLOCK(); 10410dad3f0eSMax Laier return (EEXIST); 10420dad3f0eSMax Laier } 10430dad3f0eSMax Laier 10440dad3f0eSMax Laier if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP, 10450dad3f0eSMax Laier M_NOWAIT)) == NULL) { 10460dad3f0eSMax Laier IFNET_WUNLOCK(); 10470dad3f0eSMax Laier return (ENOMEM); 10480dad3f0eSMax Laier } 10490dad3f0eSMax Laier 10500dad3f0eSMax Laier if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member), 10510dad3f0eSMax Laier M_TEMP, M_NOWAIT)) == NULL) { 10520dad3f0eSMax Laier free(ifgl, M_TEMP); 10530dad3f0eSMax Laier IFNET_WUNLOCK(); 10540dad3f0eSMax Laier return (ENOMEM); 10550dad3f0eSMax Laier } 10560dad3f0eSMax Laier 1057603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 10580dad3f0eSMax Laier if (!strcmp(ifg->ifg_group, groupname)) 10590dad3f0eSMax Laier break; 10600dad3f0eSMax Laier 10610dad3f0eSMax Laier if (ifg == NULL) { 10620dad3f0eSMax Laier if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group), 10630dad3f0eSMax Laier M_TEMP, M_NOWAIT)) == NULL) { 10640dad3f0eSMax Laier free(ifgl, M_TEMP); 10650dad3f0eSMax Laier free(ifgm, M_TEMP); 10660dad3f0eSMax Laier IFNET_WUNLOCK(); 10670dad3f0eSMax Laier return (ENOMEM); 10680dad3f0eSMax Laier } 10690dad3f0eSMax Laier strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group)); 10700dad3f0eSMax Laier ifg->ifg_refcnt = 0; 10710dad3f0eSMax Laier TAILQ_INIT(&ifg->ifg_members); 10720dad3f0eSMax Laier EVENTHANDLER_INVOKE(group_attach_event, ifg); 1073603724d3SBjoern A. Zeeb TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next); 10740dad3f0eSMax Laier } 10750dad3f0eSMax Laier 10760dad3f0eSMax Laier ifg->ifg_refcnt++; 10770dad3f0eSMax Laier ifgl->ifgl_group = ifg; 10780dad3f0eSMax Laier ifgm->ifgm_ifp = ifp; 10790dad3f0eSMax Laier 10800dad3f0eSMax Laier IF_ADDR_LOCK(ifp); 10810dad3f0eSMax Laier TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next); 10820dad3f0eSMax Laier TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next); 10830dad3f0eSMax Laier IF_ADDR_UNLOCK(ifp); 10840dad3f0eSMax Laier 10850dad3f0eSMax Laier IFNET_WUNLOCK(); 10860dad3f0eSMax Laier 10870dad3f0eSMax Laier EVENTHANDLER_INVOKE(group_change_event, groupname); 10880dad3f0eSMax Laier 10890dad3f0eSMax Laier return (0); 10900dad3f0eSMax Laier } 10910dad3f0eSMax Laier 10920dad3f0eSMax Laier /* 10930dad3f0eSMax Laier * Remove a group from an interface 10940dad3f0eSMax Laier */ 10950dad3f0eSMax Laier int 10960dad3f0eSMax Laier if_delgroup(struct ifnet *ifp, const char *groupname) 10970dad3f0eSMax Laier { 10980dad3f0eSMax Laier struct ifg_list *ifgl; 10990dad3f0eSMax Laier struct ifg_member *ifgm; 11000dad3f0eSMax Laier 11010dad3f0eSMax Laier IFNET_WLOCK(); 11020dad3f0eSMax Laier TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 11030dad3f0eSMax Laier if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) 11040dad3f0eSMax Laier break; 11050dad3f0eSMax Laier if (ifgl == NULL) { 11060dad3f0eSMax Laier IFNET_WUNLOCK(); 11070dad3f0eSMax Laier return (ENOENT); 11080dad3f0eSMax Laier } 11090dad3f0eSMax Laier 11100dad3f0eSMax Laier IF_ADDR_LOCK(ifp); 11110dad3f0eSMax Laier TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next); 11120dad3f0eSMax Laier IF_ADDR_UNLOCK(ifp); 11130dad3f0eSMax Laier 11140dad3f0eSMax Laier TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) 11150dad3f0eSMax Laier if (ifgm->ifgm_ifp == ifp) 11160dad3f0eSMax Laier break; 11170dad3f0eSMax Laier 11180dad3f0eSMax Laier if (ifgm != NULL) { 11190dad3f0eSMax Laier TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next); 11200dad3f0eSMax Laier free(ifgm, M_TEMP); 11210dad3f0eSMax Laier } 11220dad3f0eSMax Laier 11230dad3f0eSMax Laier if (--ifgl->ifgl_group->ifg_refcnt == 0) { 1124603724d3SBjoern A. Zeeb TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next); 11250dad3f0eSMax Laier EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group); 11260dad3f0eSMax Laier free(ifgl->ifgl_group, M_TEMP); 11270dad3f0eSMax Laier } 11280dad3f0eSMax Laier IFNET_WUNLOCK(); 11290dad3f0eSMax Laier 11300dad3f0eSMax Laier free(ifgl, M_TEMP); 11310dad3f0eSMax Laier 11320dad3f0eSMax Laier EVENTHANDLER_INVOKE(group_change_event, groupname); 11330dad3f0eSMax Laier 11340dad3f0eSMax Laier return (0); 11350dad3f0eSMax Laier } 11360dad3f0eSMax Laier 11370dad3f0eSMax Laier /* 11388623f9fdSMax Laier * Remove an interface from all groups 11398623f9fdSMax Laier */ 11408623f9fdSMax Laier static void 11418623f9fdSMax Laier if_delgroups(struct ifnet *ifp) 11428623f9fdSMax Laier { 11438623f9fdSMax Laier struct ifg_list *ifgl; 11448623f9fdSMax Laier struct ifg_member *ifgm; 11458623f9fdSMax Laier char groupname[IFNAMSIZ]; 11468623f9fdSMax Laier 11478623f9fdSMax Laier IFNET_WLOCK(); 11488623f9fdSMax Laier while (!TAILQ_EMPTY(&ifp->if_groups)) { 11498623f9fdSMax Laier ifgl = TAILQ_FIRST(&ifp->if_groups); 11508623f9fdSMax Laier 11518623f9fdSMax Laier strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ); 11528623f9fdSMax Laier 11538623f9fdSMax Laier IF_ADDR_LOCK(ifp); 11548623f9fdSMax Laier TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next); 11558623f9fdSMax Laier IF_ADDR_UNLOCK(ifp); 11568623f9fdSMax Laier 11578623f9fdSMax Laier TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) 11588623f9fdSMax Laier if (ifgm->ifgm_ifp == ifp) 11598623f9fdSMax Laier break; 11608623f9fdSMax Laier 11618623f9fdSMax Laier if (ifgm != NULL) { 11628623f9fdSMax Laier TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, 11638623f9fdSMax Laier ifgm_next); 11648623f9fdSMax Laier free(ifgm, M_TEMP); 11658623f9fdSMax Laier } 11668623f9fdSMax Laier 11678623f9fdSMax Laier if (--ifgl->ifgl_group->ifg_refcnt == 0) { 11688623f9fdSMax Laier TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next); 11698623f9fdSMax Laier EVENTHANDLER_INVOKE(group_detach_event, 11708623f9fdSMax Laier ifgl->ifgl_group); 11718623f9fdSMax Laier free(ifgl->ifgl_group, M_TEMP); 11728623f9fdSMax Laier } 11738623f9fdSMax Laier IFNET_WUNLOCK(); 11748623f9fdSMax Laier 11758623f9fdSMax Laier free(ifgl, M_TEMP); 11768623f9fdSMax Laier 11778623f9fdSMax Laier EVENTHANDLER_INVOKE(group_change_event, groupname); 11788623f9fdSMax Laier 11798623f9fdSMax Laier IFNET_WLOCK(); 11808623f9fdSMax Laier } 11818623f9fdSMax Laier IFNET_WUNLOCK(); 11828623f9fdSMax Laier } 11838623f9fdSMax Laier 11848623f9fdSMax Laier /* 11850dad3f0eSMax Laier * Stores all groups from an interface in memory pointed 11860dad3f0eSMax Laier * to by data 11870dad3f0eSMax Laier */ 11880dad3f0eSMax Laier static int 11890dad3f0eSMax Laier if_getgroup(struct ifgroupreq *data, struct ifnet *ifp) 11900dad3f0eSMax Laier { 11910dad3f0eSMax Laier int len, error; 11920dad3f0eSMax Laier struct ifg_list *ifgl; 11930dad3f0eSMax Laier struct ifg_req ifgrq, *ifgp; 11940dad3f0eSMax Laier struct ifgroupreq *ifgr = data; 11950dad3f0eSMax Laier 11960dad3f0eSMax Laier if (ifgr->ifgr_len == 0) { 11970dad3f0eSMax Laier IF_ADDR_LOCK(ifp); 11980dad3f0eSMax Laier TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 11990dad3f0eSMax Laier ifgr->ifgr_len += sizeof(struct ifg_req); 12000dad3f0eSMax Laier IF_ADDR_UNLOCK(ifp); 12010dad3f0eSMax Laier return (0); 12020dad3f0eSMax Laier } 12030dad3f0eSMax Laier 12040dad3f0eSMax Laier len = ifgr->ifgr_len; 12050dad3f0eSMax Laier ifgp = ifgr->ifgr_groups; 12060dad3f0eSMax Laier /* XXX: wire */ 12070dad3f0eSMax Laier IF_ADDR_LOCK(ifp); 12080dad3f0eSMax Laier TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) { 12090dad3f0eSMax Laier if (len < sizeof(ifgrq)) { 12100dad3f0eSMax Laier IF_ADDR_UNLOCK(ifp); 12110dad3f0eSMax Laier return (EINVAL); 12120dad3f0eSMax Laier } 12130dad3f0eSMax Laier bzero(&ifgrq, sizeof ifgrq); 12140dad3f0eSMax Laier strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group, 12150dad3f0eSMax Laier sizeof(ifgrq.ifgrq_group)); 12160dad3f0eSMax Laier if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 12170dad3f0eSMax Laier IF_ADDR_UNLOCK(ifp); 12180dad3f0eSMax Laier return (error); 12190dad3f0eSMax Laier } 12200dad3f0eSMax Laier len -= sizeof(ifgrq); 12210dad3f0eSMax Laier ifgp++; 12220dad3f0eSMax Laier } 12230dad3f0eSMax Laier IF_ADDR_UNLOCK(ifp); 12240dad3f0eSMax Laier 12250dad3f0eSMax Laier return (0); 12260dad3f0eSMax Laier } 12270dad3f0eSMax Laier 12280dad3f0eSMax Laier /* 12290dad3f0eSMax Laier * Stores all members of a group in memory pointed to by data 12300dad3f0eSMax Laier */ 12310dad3f0eSMax Laier static int 12320dad3f0eSMax Laier if_getgroupmembers(struct ifgroupreq *data) 12330dad3f0eSMax Laier { 12340dad3f0eSMax Laier struct ifgroupreq *ifgr = data; 12350dad3f0eSMax Laier struct ifg_group *ifg; 12360dad3f0eSMax Laier struct ifg_member *ifgm; 12370dad3f0eSMax Laier struct ifg_req ifgrq, *ifgp; 12380dad3f0eSMax Laier int len, error; 12390dad3f0eSMax Laier 12400dad3f0eSMax Laier IFNET_RLOCK(); 1241603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 12420dad3f0eSMax Laier if (!strcmp(ifg->ifg_group, ifgr->ifgr_name)) 12430dad3f0eSMax Laier break; 12440dad3f0eSMax Laier if (ifg == NULL) { 12450dad3f0eSMax Laier IFNET_RUNLOCK(); 12460dad3f0eSMax Laier return (ENOENT); 12470dad3f0eSMax Laier } 12480dad3f0eSMax Laier 12490dad3f0eSMax Laier if (ifgr->ifgr_len == 0) { 12500dad3f0eSMax Laier TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) 12510dad3f0eSMax Laier ifgr->ifgr_len += sizeof(ifgrq); 12520dad3f0eSMax Laier IFNET_RUNLOCK(); 12530dad3f0eSMax Laier return (0); 12540dad3f0eSMax Laier } 12550dad3f0eSMax Laier 12560dad3f0eSMax Laier len = ifgr->ifgr_len; 12570dad3f0eSMax Laier ifgp = ifgr->ifgr_groups; 12580dad3f0eSMax Laier TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) { 12590dad3f0eSMax Laier if (len < sizeof(ifgrq)) { 12600dad3f0eSMax Laier IFNET_RUNLOCK(); 12610dad3f0eSMax Laier return (EINVAL); 12620dad3f0eSMax Laier } 12630dad3f0eSMax Laier bzero(&ifgrq, sizeof ifgrq); 12640dad3f0eSMax Laier strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname, 12650dad3f0eSMax Laier sizeof(ifgrq.ifgrq_member)); 12660dad3f0eSMax Laier if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 12670dad3f0eSMax Laier IFNET_RUNLOCK(); 12680dad3f0eSMax Laier return (error); 12690dad3f0eSMax Laier } 12700dad3f0eSMax Laier len -= sizeof(ifgrq); 12710dad3f0eSMax Laier ifgp++; 12720dad3f0eSMax Laier } 12730dad3f0eSMax Laier IFNET_RUNLOCK(); 12740dad3f0eSMax Laier 12750dad3f0eSMax Laier return (0); 12760dad3f0eSMax Laier } 12770dad3f0eSMax Laier 12780dad3f0eSMax Laier /* 12795500d3beSWarner Losh * Delete Routes for a Network Interface 12805500d3beSWarner Losh * 12815500d3beSWarner Losh * Called for each routing entry via the rnh->rnh_walktree() call above 12825500d3beSWarner Losh * to delete all route entries referencing a detaching network interface. 12835500d3beSWarner Losh * 12845500d3beSWarner Losh * Arguments: 12855500d3beSWarner Losh * rn pointer to node in the routing table 12865500d3beSWarner Losh * arg argument passed to rnh->rnh_walktree() - detaching interface 12875500d3beSWarner Losh * 12885500d3beSWarner Losh * Returns: 12895500d3beSWarner Losh * 0 successful 12905500d3beSWarner Losh * errno failed - reason indicated 12915500d3beSWarner Losh * 12925500d3beSWarner Losh */ 12935500d3beSWarner Losh static int 129472fd1b6aSDag-Erling Smørgrav if_rtdel(struct radix_node *rn, void *arg) 12955500d3beSWarner Losh { 12965500d3beSWarner Losh struct rtentry *rt = (struct rtentry *)rn; 12975500d3beSWarner Losh struct ifnet *ifp = arg; 12985500d3beSWarner Losh int err; 12995500d3beSWarner Losh 13005500d3beSWarner Losh if (rt->rt_ifp == ifp) { 13015500d3beSWarner Losh 13025500d3beSWarner Losh /* 13035500d3beSWarner Losh * Protect (sorta) against walktree recursion problems 13045500d3beSWarner Losh * with cloned routes 13055500d3beSWarner Losh */ 13065500d3beSWarner Losh if ((rt->rt_flags & RTF_UP) == 0) 13075500d3beSWarner Losh return (0); 13085500d3beSWarner Losh 13098b07e49aSJulian Elischer err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway, 13106241d13aSKip Macy rt_mask(rt), rt->rt_flags|RTF_RNH_LOCKED, 13118b07e49aSJulian Elischer (struct rtentry **) NULL, rt->rt_fibnum); 13125500d3beSWarner Losh if (err) { 13135500d3beSWarner Losh log(LOG_WARNING, "if_rtdel: error %d\n", err); 13145500d3beSWarner Losh } 13155500d3beSWarner Losh } 13165500d3beSWarner Losh 13175500d3beSWarner Losh return (0); 13186182fdbdSPeter Wemm } 13196182fdbdSPeter Wemm 132040d8a302SBruce M Simpson /* 1321f9ef96caSRobert Watson * Wrapper functions for struct ifnet address list locking macros. These are 1322f9ef96caSRobert Watson * used by kernel modules to avoid encoding programming interface or binary 1323f9ef96caSRobert Watson * interface assumptions that may be violated when kernel-internal locking 1324f9ef96caSRobert Watson * approaches change. 1325f9ef96caSRobert Watson */ 1326f9ef96caSRobert Watson void 1327f9ef96caSRobert Watson if_addr_rlock(struct ifnet *ifp) 1328f9ef96caSRobert Watson { 1329f9ef96caSRobert Watson 1330f9ef96caSRobert Watson IF_ADDR_LOCK(ifp); 1331f9ef96caSRobert Watson } 1332f9ef96caSRobert Watson 1333f9ef96caSRobert Watson void 1334f9ef96caSRobert Watson if_addr_runlock(struct ifnet *ifp) 1335f9ef96caSRobert Watson { 1336f9ef96caSRobert Watson 1337f9ef96caSRobert Watson IF_ADDR_UNLOCK(ifp); 1338f9ef96caSRobert Watson } 1339f9ef96caSRobert Watson 1340f9ef96caSRobert Watson void 1341f9ef96caSRobert Watson if_maddr_rlock(struct ifnet *ifp) 1342f9ef96caSRobert Watson { 1343f9ef96caSRobert Watson 1344f9ef96caSRobert Watson IF_ADDR_LOCK(ifp); 1345f9ef96caSRobert Watson } 1346f9ef96caSRobert Watson 1347f9ef96caSRobert Watson void 1348f9ef96caSRobert Watson if_maddr_runlock(struct ifnet *ifp) 1349f9ef96caSRobert Watson { 1350f9ef96caSRobert Watson 1351f9ef96caSRobert Watson IF_ADDR_UNLOCK(ifp); 1352f9ef96caSRobert Watson } 1353f9ef96caSRobert Watson 1354f9ef96caSRobert Watson /* 13551099f828SRobert Watson * Reference count functions for ifaddrs. 13561099f828SRobert Watson */ 13571099f828SRobert Watson void 13581099f828SRobert Watson ifa_init(struct ifaddr *ifa) 13591099f828SRobert Watson { 13601099f828SRobert Watson 13611099f828SRobert Watson mtx_init(&ifa->ifa_mtx, "ifaddr", NULL, MTX_DEF); 13621099f828SRobert Watson refcount_init(&ifa->ifa_refcnt, 1); 13631099f828SRobert Watson } 13641099f828SRobert Watson 13651099f828SRobert Watson void 13661099f828SRobert Watson ifa_ref(struct ifaddr *ifa) 13671099f828SRobert Watson { 13681099f828SRobert Watson 13691099f828SRobert Watson refcount_acquire(&ifa->ifa_refcnt); 13701099f828SRobert Watson } 13711099f828SRobert Watson 13721099f828SRobert Watson void 13731099f828SRobert Watson ifa_free(struct ifaddr *ifa) 13741099f828SRobert Watson { 13751099f828SRobert Watson 13761099f828SRobert Watson if (refcount_release(&ifa->ifa_refcnt)) { 13771099f828SRobert Watson mtx_destroy(&ifa->ifa_mtx); 13781099f828SRobert Watson free(ifa, M_IFADDR); 13791099f828SRobert Watson } 13801099f828SRobert Watson } 13811099f828SRobert Watson 13821099f828SRobert Watson /* 138340d8a302SBruce M Simpson * XXX: Because sockaddr_dl has deeper structure than the sockaddr 138440d8a302SBruce M Simpson * structs used to represent other address families, it is necessary 138540d8a302SBruce M Simpson * to perform a different comparison. 138640d8a302SBruce M Simpson */ 138740d8a302SBruce M Simpson 138840d8a302SBruce M Simpson #define sa_equal(a1, a2) \ 138940d8a302SBruce M Simpson (bcmp((a1), (a2), ((a1))->sa_len) == 0) 139040d8a302SBruce M Simpson 139140d8a302SBruce M Simpson #define sa_dl_equal(a1, a2) \ 139240d8a302SBruce M Simpson ((((struct sockaddr_dl *)(a1))->sdl_len == \ 139340d8a302SBruce M Simpson ((struct sockaddr_dl *)(a2))->sdl_len) && \ 139440d8a302SBruce M Simpson (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \ 139540d8a302SBruce M Simpson LLADDR((struct sockaddr_dl *)(a2)), \ 139640d8a302SBruce M Simpson ((struct sockaddr_dl *)(a1))->sdl_alen) == 0)) 139719fc74fbSJeffrey Hsu 139830aad87dSBrooks Davis /* 1399df8bae1dSRodney W. Grimes * Locate an interface based on a complete address. 1400df8bae1dSRodney W. Grimes */ 1401df8bae1dSRodney W. Grimes /*ARGSUSED*/ 14028896f83aSRobert Watson static struct ifaddr * 14038c0fec80SRobert Watson ifa_ifwithaddr_internal(struct sockaddr *addr, int getref) 1404df8bae1dSRodney W. Grimes { 14050b59d917SJonathan Lemon struct ifnet *ifp; 14060b59d917SJonathan Lemon struct ifaddr *ifa; 1407df8bae1dSRodney W. Grimes 140877dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 1409ab5ed8a5SRobert Watson TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1410ab5ed8a5SRobert Watson IF_ADDR_LOCK(ifp); 141137d40066SPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1412df8bae1dSRodney W. Grimes if (ifa->ifa_addr->sa_family != addr->sa_family) 1413df8bae1dSRodney W. Grimes continue; 1414ab5ed8a5SRobert Watson if (sa_equal(addr, ifa->ifa_addr)) { 14158c0fec80SRobert Watson if (getref) 14168c0fec80SRobert Watson ifa_ref(ifa); 1417ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 14180b59d917SJonathan Lemon goto done; 1419ab5ed8a5SRobert Watson } 142082cd038dSYoshinobu Inoue /* IP6 doesn't have broadcast */ 14210b59d917SJonathan Lemon if ((ifp->if_flags & IFF_BROADCAST) && 14220b59d917SJonathan Lemon ifa->ifa_broadaddr && 142382cd038dSYoshinobu Inoue ifa->ifa_broadaddr->sa_len != 0 && 1424ab5ed8a5SRobert Watson sa_equal(ifa->ifa_broadaddr, addr)) { 14258c0fec80SRobert Watson if (getref) 14268c0fec80SRobert Watson ifa_ref(ifa); 1427ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 14280b59d917SJonathan Lemon goto done; 14290b59d917SJonathan Lemon } 1430ab5ed8a5SRobert Watson } 1431ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 1432ab5ed8a5SRobert Watson } 14330b59d917SJonathan Lemon ifa = NULL; 14340b59d917SJonathan Lemon done: 143577dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 1436df8bae1dSRodney W. Grimes return (ifa); 1437df8bae1dSRodney W. Grimes } 14380b59d917SJonathan Lemon 14398896f83aSRobert Watson struct ifaddr * 14408896f83aSRobert Watson ifa_ifwithaddr(struct sockaddr *addr) 14418896f83aSRobert Watson { 14428896f83aSRobert Watson 14438c0fec80SRobert Watson return (ifa_ifwithaddr_internal(addr, 1)); 14448896f83aSRobert Watson } 14458896f83aSRobert Watson 14468896f83aSRobert Watson int 14478896f83aSRobert Watson ifa_ifwithaddr_check(struct sockaddr *addr) 14488896f83aSRobert Watson { 14498896f83aSRobert Watson 14508c0fec80SRobert Watson return (ifa_ifwithaddr_internal(addr, 0) != NULL); 14518896f83aSRobert Watson } 14528896f83aSRobert Watson 1453df8bae1dSRodney W. Grimes /* 1454773725a2SAndre Oppermann * Locate an interface based on the broadcast address. 1455773725a2SAndre Oppermann */ 1456773725a2SAndre Oppermann /* ARGSUSED */ 1457773725a2SAndre Oppermann struct ifaddr * 1458773725a2SAndre Oppermann ifa_ifwithbroadaddr(struct sockaddr *addr) 1459773725a2SAndre Oppermann { 1460773725a2SAndre Oppermann struct ifnet *ifp; 1461773725a2SAndre Oppermann struct ifaddr *ifa; 1462773725a2SAndre Oppermann 146377dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 1464ab5ed8a5SRobert Watson TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1465ab5ed8a5SRobert Watson IF_ADDR_LOCK(ifp); 1466773725a2SAndre Oppermann TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1467773725a2SAndre Oppermann if (ifa->ifa_addr->sa_family != addr->sa_family) 1468773725a2SAndre Oppermann continue; 1469773725a2SAndre Oppermann if ((ifp->if_flags & IFF_BROADCAST) && 1470773725a2SAndre Oppermann ifa->ifa_broadaddr && 1471773725a2SAndre Oppermann ifa->ifa_broadaddr->sa_len != 0 && 1472ab5ed8a5SRobert Watson sa_equal(ifa->ifa_broadaddr, addr)) { 14738c0fec80SRobert Watson ifa_ref(ifa); 1474ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 1475773725a2SAndre Oppermann goto done; 1476773725a2SAndre Oppermann } 1477ab5ed8a5SRobert Watson } 1478ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 1479ab5ed8a5SRobert Watson } 1480773725a2SAndre Oppermann ifa = NULL; 1481773725a2SAndre Oppermann done: 148277dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 1483773725a2SAndre Oppermann return (ifa); 1484773725a2SAndre Oppermann } 1485773725a2SAndre Oppermann 1486773725a2SAndre Oppermann /* 1487df8bae1dSRodney W. Grimes * Locate the point to point interface with a given destination address. 1488df8bae1dSRodney W. Grimes */ 1489df8bae1dSRodney W. Grimes /*ARGSUSED*/ 1490df8bae1dSRodney W. Grimes struct ifaddr * 149172fd1b6aSDag-Erling Smørgrav ifa_ifwithdstaddr(struct sockaddr *addr) 1492df8bae1dSRodney W. Grimes { 14930b59d917SJonathan Lemon struct ifnet *ifp; 14940b59d917SJonathan Lemon struct ifaddr *ifa; 1495df8bae1dSRodney W. Grimes 149677dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 1497603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 14980b59d917SJonathan Lemon if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 14990b59d917SJonathan Lemon continue; 1500ab5ed8a5SRobert Watson IF_ADDR_LOCK(ifp); 150137d40066SPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1502df8bae1dSRodney W. Grimes if (ifa->ifa_addr->sa_family != addr->sa_family) 1503df8bae1dSRodney W. Grimes continue; 1504f0c04221SBjoern A. Zeeb if (ifa->ifa_dstaddr != NULL && 1505ab5ed8a5SRobert Watson sa_equal(addr, ifa->ifa_dstaddr)) { 15068c0fec80SRobert Watson ifa_ref(ifa); 1507ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 15080b59d917SJonathan Lemon goto done; 1509df8bae1dSRodney W. Grimes } 15100b59d917SJonathan Lemon } 1511ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 1512ab5ed8a5SRobert Watson } 15130b59d917SJonathan Lemon ifa = NULL; 15140b59d917SJonathan Lemon done: 151577dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 15160b59d917SJonathan Lemon return (ifa); 1517df8bae1dSRodney W. Grimes } 1518df8bae1dSRodney W. Grimes 1519df8bae1dSRodney W. Grimes /* 1520df8bae1dSRodney W. Grimes * Find an interface on a specific network. If many, choice 1521df8bae1dSRodney W. Grimes * is most specific found. 1522df8bae1dSRodney W. Grimes */ 1523df8bae1dSRodney W. Grimes struct ifaddr * 152472fd1b6aSDag-Erling Smørgrav ifa_ifwithnet(struct sockaddr *addr) 1525df8bae1dSRodney W. Grimes { 152672fd1b6aSDag-Erling Smørgrav struct ifnet *ifp; 152772fd1b6aSDag-Erling Smørgrav struct ifaddr *ifa; 15288c0fec80SRobert Watson struct ifaddr *ifa_maybe = NULL; 1529df8bae1dSRodney W. Grimes u_int af = addr->sa_family; 1530df8bae1dSRodney W. Grimes char *addr_data = addr->sa_data, *cplim; 1531df8bae1dSRodney W. Grimes 15327e2a6151SJulian Elischer /* 15337e2a6151SJulian Elischer * AF_LINK addresses can be looked up directly by their index number, 15347e2a6151SJulian Elischer * so do that if we can. 15357e2a6151SJulian Elischer */ 1536df8bae1dSRodney W. Grimes if (af == AF_LINK) { 1537d1dd20beSSam Leffler struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; 1538603724d3SBjoern A. Zeeb if (sdl->sdl_index && sdl->sdl_index <= V_if_index) 1539f9132cebSJonathan Lemon return (ifaddr_byindex(sdl->sdl_index)); 1540df8bae1dSRodney W. Grimes } 15417e2a6151SJulian Elischer 15427e2a6151SJulian Elischer /* 15438c0fec80SRobert Watson * Scan though each interface, looking for ones that have addresses 15448c0fec80SRobert Watson * in this address family. Maintain a reference on ifa_maybe once 15458c0fec80SRobert Watson * we find one, as we release the IF_ADDR_LOCK() that kept it stable 15468c0fec80SRobert Watson * when we move onto the next interface. 15477e2a6151SJulian Elischer */ 154877dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 1549603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1550ab5ed8a5SRobert Watson IF_ADDR_LOCK(ifp); 155137d40066SPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 155272fd1b6aSDag-Erling Smørgrav char *cp, *cp2, *cp3; 1553df8bae1dSRodney W. Grimes 1554523a02aaSDavid Greenman if (ifa->ifa_addr->sa_family != af) 1555df8bae1dSRodney W. Grimes next: continue; 1556c61cd599SHajimu UMEMOTO if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) { 15577e2a6151SJulian Elischer /* 15587e2a6151SJulian Elischer * This is a bit broken as it doesn't 15597e2a6151SJulian Elischer * take into account that the remote end may 15607e2a6151SJulian Elischer * be a single node in the network we are 15617e2a6151SJulian Elischer * looking for. 15627e2a6151SJulian Elischer * The trouble is that we don't know the 15637e2a6151SJulian Elischer * netmask for the remote end. 15647e2a6151SJulian Elischer */ 1565f0c04221SBjoern A. Zeeb if (ifa->ifa_dstaddr != NULL && 1566ab5ed8a5SRobert Watson sa_equal(addr, ifa->ifa_dstaddr)) { 15678c0fec80SRobert Watson ifa_ref(ifa); 1568ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 15690b59d917SJonathan Lemon goto done; 1570ab5ed8a5SRobert Watson } 15713740e2adSDavid Greenman } else { 15727e2a6151SJulian Elischer /* 15737ed8f465SJulian Elischer * if we have a special address handler, 15747ed8f465SJulian Elischer * then use it instead of the generic one. 15757ed8f465SJulian Elischer */ 15767ed8f465SJulian Elischer if (ifa->ifa_claim_addr) { 1577ab5ed8a5SRobert Watson if ((*ifa->ifa_claim_addr)(ifa, addr)) { 15788c0fec80SRobert Watson ifa_ref(ifa); 1579ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 15800b59d917SJonathan Lemon goto done; 1581ab5ed8a5SRobert Watson } 15827ed8f465SJulian Elischer continue; 15837ed8f465SJulian Elischer } 15847ed8f465SJulian Elischer 15857ed8f465SJulian Elischer /* 15867e2a6151SJulian Elischer * Scan all the bits in the ifa's address. 15877e2a6151SJulian Elischer * If a bit dissagrees with what we are 15887e2a6151SJulian Elischer * looking for, mask it with the netmask 15897e2a6151SJulian Elischer * to see if it really matters. 15907e2a6151SJulian Elischer * (A byte at a time) 15917e2a6151SJulian Elischer */ 1592523a02aaSDavid Greenman if (ifa->ifa_netmask == 0) 1593523a02aaSDavid Greenman continue; 1594df8bae1dSRodney W. Grimes cp = addr_data; 1595df8bae1dSRodney W. Grimes cp2 = ifa->ifa_addr->sa_data; 1596df8bae1dSRodney W. Grimes cp3 = ifa->ifa_netmask->sa_data; 15977e2a6151SJulian Elischer cplim = ifa->ifa_netmask->sa_len 15987e2a6151SJulian Elischer + (char *)ifa->ifa_netmask; 1599df8bae1dSRodney W. Grimes while (cp3 < cplim) 1600df8bae1dSRodney W. Grimes if ((*cp++ ^ *cp2++) & *cp3++) 16017e2a6151SJulian Elischer goto next; /* next address! */ 16027e2a6151SJulian Elischer /* 16037e2a6151SJulian Elischer * If the netmask of what we just found 16047e2a6151SJulian Elischer * is more specific than what we had before 16057e2a6151SJulian Elischer * (if we had one) then remember the new one 16067e2a6151SJulian Elischer * before continuing to search 16077e2a6151SJulian Elischer * for an even better one. 16087e2a6151SJulian Elischer */ 16098c0fec80SRobert Watson if (ifa_maybe == NULL || 1610df8bae1dSRodney W. Grimes rn_refines((caddr_t)ifa->ifa_netmask, 16118c0fec80SRobert Watson (caddr_t)ifa_maybe->ifa_netmask)) { 16128c0fec80SRobert Watson if (ifa_maybe != NULL) 16138c0fec80SRobert Watson ifa_free(ifa_maybe); 1614df8bae1dSRodney W. Grimes ifa_maybe = ifa; 16158c0fec80SRobert Watson ifa_ref(ifa_maybe); 16168c0fec80SRobert Watson } 1617df8bae1dSRodney W. Grimes } 1618b2af64fdSDavid Greenman } 1619ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 1620b2af64fdSDavid Greenman } 16210b59d917SJonathan Lemon ifa = ifa_maybe; 16228c0fec80SRobert Watson ifa_maybe = NULL; 16230b59d917SJonathan Lemon done: 162477dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 16258c0fec80SRobert Watson if (ifa_maybe != NULL) 16268c0fec80SRobert Watson ifa_free(ifa_maybe); 16270b59d917SJonathan Lemon return (ifa); 1628df8bae1dSRodney W. Grimes } 1629df8bae1dSRodney W. Grimes 1630df8bae1dSRodney W. Grimes /* 1631df8bae1dSRodney W. Grimes * Find an interface address specific to an interface best matching 1632df8bae1dSRodney W. Grimes * a given address. 1633df8bae1dSRodney W. Grimes */ 1634df8bae1dSRodney W. Grimes struct ifaddr * 163572fd1b6aSDag-Erling Smørgrav ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp) 1636df8bae1dSRodney W. Grimes { 163772fd1b6aSDag-Erling Smørgrav struct ifaddr *ifa; 163872fd1b6aSDag-Erling Smørgrav char *cp, *cp2, *cp3; 163972fd1b6aSDag-Erling Smørgrav char *cplim; 16408c0fec80SRobert Watson struct ifaddr *ifa_maybe = NULL; 1641df8bae1dSRodney W. Grimes u_int af = addr->sa_family; 1642df8bae1dSRodney W. Grimes 1643df8bae1dSRodney W. Grimes if (af >= AF_MAX) 1644df8bae1dSRodney W. Grimes return (0); 1645ab5ed8a5SRobert Watson IF_ADDR_LOCK(ifp); 164637d40066SPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1647df8bae1dSRodney W. Grimes if (ifa->ifa_addr->sa_family != af) 1648df8bae1dSRodney W. Grimes continue; 16498c0fec80SRobert Watson if (ifa_maybe == NULL) 1650df8bae1dSRodney W. Grimes ifa_maybe = ifa; 1651df8bae1dSRodney W. Grimes if (ifa->ifa_netmask == 0) { 1652d8d5b10eSRobert Watson if (sa_equal(addr, ifa->ifa_addr) || 1653d8d5b10eSRobert Watson (ifa->ifa_dstaddr && 1654d8d5b10eSRobert Watson sa_equal(addr, ifa->ifa_dstaddr))) 16552defe5cdSJonathan Lemon goto done; 1656df8bae1dSRodney W. Grimes continue; 1657df8bae1dSRodney W. Grimes } 1658b2af64fdSDavid Greenman if (ifp->if_flags & IFF_POINTOPOINT) { 1659d8d5b10eSRobert Watson if (sa_equal(addr, ifa->ifa_dstaddr)) 1660a8637146SJonathan Lemon goto done; 16613740e2adSDavid Greenman } else { 1662df8bae1dSRodney W. Grimes cp = addr->sa_data; 1663df8bae1dSRodney W. Grimes cp2 = ifa->ifa_addr->sa_data; 1664df8bae1dSRodney W. Grimes cp3 = ifa->ifa_netmask->sa_data; 1665df8bae1dSRodney W. Grimes cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 1666df8bae1dSRodney W. Grimes for (; cp3 < cplim; cp3++) 1667df8bae1dSRodney W. Grimes if ((*cp++ ^ *cp2++) & *cp3) 1668df8bae1dSRodney W. Grimes break; 1669df8bae1dSRodney W. Grimes if (cp3 == cplim) 16702defe5cdSJonathan Lemon goto done; 1671df8bae1dSRodney W. Grimes } 1672b2af64fdSDavid Greenman } 1673f9132cebSJonathan Lemon ifa = ifa_maybe; 1674f9132cebSJonathan Lemon done: 16758c0fec80SRobert Watson if (ifa != NULL) 16768c0fec80SRobert Watson ifa_ref(ifa); 1677ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 1678f9132cebSJonathan Lemon return (ifa); 1679df8bae1dSRodney W. Grimes } 1680df8bae1dSRodney W. Grimes 16816e6b3f7cSQing Li #include <net/if_llatbl.h> 16826e6b3f7cSQing Li 1683df8bae1dSRodney W. Grimes /* 1684df8bae1dSRodney W. Grimes * Default action when installing a route with a Link Level gateway. 1685df8bae1dSRodney W. Grimes * Lookup an appropriate real ifa to point to. 1686df8bae1dSRodney W. Grimes * This should be moved to /sys/net/link.c eventually. 1687df8bae1dSRodney W. Grimes */ 16883bda9f9bSPoul-Henning Kamp static void 168972fd1b6aSDag-Erling Smørgrav link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 1690df8bae1dSRodney W. Grimes { 169172fd1b6aSDag-Erling Smørgrav struct ifaddr *ifa, *oifa; 1692df8bae1dSRodney W. Grimes struct sockaddr *dst; 1693df8bae1dSRodney W. Grimes struct ifnet *ifp; 1694df8bae1dSRodney W. Grimes 1695d1dd20beSSam Leffler RT_LOCK_ASSERT(rt); 1696d1dd20beSSam Leffler 1697df8bae1dSRodney W. Grimes if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 1698df8bae1dSRodney W. Grimes ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 1699df8bae1dSRodney W. Grimes return; 17009448326fSPoul-Henning Kamp ifa = ifaof_ifpforaddr(dst, ifp); 17019448326fSPoul-Henning Kamp if (ifa) { 1702d1dd20beSSam Leffler oifa = rt->rt_ifa; 1703df8bae1dSRodney W. Grimes rt->rt_ifa = ifa; 17041099f828SRobert Watson ifa_free(oifa); 1705df8bae1dSRodney W. Grimes if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 17068071913dSRuslan Ermilov ifa->ifa_rtrequest(cmd, rt, info); 1707df8bae1dSRodney W. Grimes } 1708df8bae1dSRodney W. Grimes } 1709df8bae1dSRodney W. Grimes 1710df8bae1dSRodney W. Grimes /* 1711df8bae1dSRodney W. Grimes * Mark an interface down and notify protocols of 1712df8bae1dSRodney W. Grimes * the transition. 1713df8bae1dSRodney W. Grimes * NOTE: must be called at splnet or eqivalent. 1714df8bae1dSRodney W. Grimes */ 17158614fb12SMax Laier static void 171672fd1b6aSDag-Erling Smørgrav if_unroute(struct ifnet *ifp, int flag, int fam) 1717df8bae1dSRodney W. Grimes { 171872fd1b6aSDag-Erling Smørgrav struct ifaddr *ifa; 1719df8bae1dSRodney W. Grimes 1720292ee7beSRobert Watson KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP")); 1721292ee7beSRobert Watson 1722e8c2601dSPoul-Henning Kamp ifp->if_flags &= ~flag; 172398b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 1724e8c2601dSPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1725e8c2601dSPoul-Henning Kamp if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1726df8bae1dSRodney W. Grimes pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 1727db7f0b97SKip Macy ifp->if_qflush(ifp); 1728db7f0b97SKip Macy 1729259d2d54SBjoern A. Zeeb #if defined(INET) || defined(INET6) 1730a9771948SGleb Smirnoff #ifdef DEV_CARP 1731a9771948SGleb Smirnoff if (ifp->if_carp) 1732a9771948SGleb Smirnoff carp_carpdev_state(ifp->if_carp); 1733a9771948SGleb Smirnoff #endif 1734259d2d54SBjoern A. Zeeb #endif 1735df8bae1dSRodney W. Grimes rt_ifmsg(ifp); 1736df8bae1dSRodney W. Grimes } 1737df8bae1dSRodney W. Grimes 1738df8bae1dSRodney W. Grimes /* 1739df8bae1dSRodney W. Grimes * Mark an interface up and notify protocols of 1740df8bae1dSRodney W. Grimes * the transition. 1741df8bae1dSRodney W. Grimes * NOTE: must be called at splnet or eqivalent. 1742df8bae1dSRodney W. Grimes */ 17438614fb12SMax Laier static void 174472fd1b6aSDag-Erling Smørgrav if_route(struct ifnet *ifp, int flag, int fam) 1745df8bae1dSRodney W. Grimes { 174672fd1b6aSDag-Erling Smørgrav struct ifaddr *ifa; 1747df8bae1dSRodney W. Grimes 1748292ee7beSRobert Watson KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP")); 1749292ee7beSRobert Watson 1750e8c2601dSPoul-Henning Kamp ifp->if_flags |= flag; 175198b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 1752e8c2601dSPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1753e8c2601dSPoul-Henning Kamp if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1754df8bae1dSRodney W. Grimes pfctlinput(PRC_IFUP, ifa->ifa_addr); 1755259d2d54SBjoern A. Zeeb #if defined(INET) || defined(INET6) 1756a9771948SGleb Smirnoff #ifdef DEV_CARP 1757a9771948SGleb Smirnoff if (ifp->if_carp) 1758a9771948SGleb Smirnoff carp_carpdev_state(ifp->if_carp); 1759a9771948SGleb Smirnoff #endif 1760259d2d54SBjoern A. Zeeb #endif 1761df8bae1dSRodney W. Grimes rt_ifmsg(ifp); 176282cd038dSYoshinobu Inoue #ifdef INET6 176382cd038dSYoshinobu Inoue in6_if_up(ifp); 176482cd038dSYoshinobu Inoue #endif 1765df8bae1dSRodney W. Grimes } 1766df8bae1dSRodney W. Grimes 176794f5c9cfSSam Leffler void (*vlan_link_state_p)(struct ifnet *, int); /* XXX: private from if_vlan */ 176875ee267cSGleb Smirnoff void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */ 176994f5c9cfSSam Leffler 177094f5c9cfSSam Leffler /* 177168a3482fSGleb Smirnoff * Handle a change in the interface link state. To avoid LORs 177268a3482fSGleb Smirnoff * between driver lock and upper layer locks, as well as possible 177368a3482fSGleb Smirnoff * recursions, we post event to taskqueue, and all job 177468a3482fSGleb Smirnoff * is done in static do_link_state_change(). 177594f5c9cfSSam Leffler */ 177694f5c9cfSSam Leffler void 177794f5c9cfSSam Leffler if_link_state_change(struct ifnet *ifp, int link_state) 177894f5c9cfSSam Leffler { 17794d96314fSGleb Smirnoff /* Return if state hasn't changed. */ 17804d96314fSGleb Smirnoff if (ifp->if_link_state == link_state) 17814d96314fSGleb Smirnoff return; 17824d96314fSGleb Smirnoff 178394f5c9cfSSam Leffler ifp->if_link_state = link_state; 17844d96314fSGleb Smirnoff 178568a3482fSGleb Smirnoff taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask); 178668a3482fSGleb Smirnoff } 178768a3482fSGleb Smirnoff 178868a3482fSGleb Smirnoff static void 178968a3482fSGleb Smirnoff do_link_state_change(void *arg, int pending) 179068a3482fSGleb Smirnoff { 179168a3482fSGleb Smirnoff struct ifnet *ifp = (struct ifnet *)arg; 179268a3482fSGleb Smirnoff int link_state = ifp->if_link_state; 179368a3482fSGleb Smirnoff int link; 17948b615593SMarko Zec CURVNET_SET(ifp->if_vnet); 179568a3482fSGleb Smirnoff 17964d96314fSGleb Smirnoff /* Notify that the link state has changed. */ 179794f5c9cfSSam Leffler rt_ifmsg(ifp); 179894f5c9cfSSam Leffler if (link_state == LINK_STATE_UP) 179994f5c9cfSSam Leffler link = NOTE_LINKUP; 180094f5c9cfSSam Leffler else if (link_state == LINK_STATE_DOWN) 180194f5c9cfSSam Leffler link = NOTE_LINKDOWN; 180294f5c9cfSSam Leffler else 180394f5c9cfSSam Leffler link = NOTE_LINKINV; 180475ee267cSGleb Smirnoff if (ifp->if_vlantrunk != NULL) 180594f5c9cfSSam Leffler (*vlan_link_state_p)(ifp, link); 18061c7899c7SGleb Smirnoff 18071c7899c7SGleb Smirnoff if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) && 18081c7899c7SGleb Smirnoff IFP2AC(ifp)->ac_netgraph != NULL) 18091c7899c7SGleb Smirnoff (*ng_ether_link_state_p)(ifp, link_state); 1810259d2d54SBjoern A. Zeeb #if defined(INET) || defined(INET6) 18114d96314fSGleb Smirnoff #ifdef DEV_CARP 18124d96314fSGleb Smirnoff if (ifp->if_carp) 18134d96314fSGleb Smirnoff carp_carpdev_state(ifp->if_carp); 18144d96314fSGleb Smirnoff #endif 1815259d2d54SBjoern A. Zeeb #endif 18168f867517SAndrew Thompson if (ifp->if_bridge) { 18178f867517SAndrew Thompson KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!")); 18188f867517SAndrew Thompson (*bstp_linkstate_p)(ifp, link_state); 18198f867517SAndrew Thompson } 182018242d3bSAndrew Thompson if (ifp->if_lagg) { 182118242d3bSAndrew Thompson KASSERT(lagg_linkstate_p != NULL,("if_lagg not loaded!")); 182218242d3bSAndrew Thompson (*lagg_linkstate_p)(ifp, link_state); 1823b47888ceSAndrew Thompson } 18248f867517SAndrew Thompson 182521ca7b57SMarko Zec if (IS_DEFAULT_VNET(curvnet)) 18269d80a330SBrooks Davis devctl_notify("IFNET", ifp->if_xname, 182721ca7b57SMarko Zec (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", 182821ca7b57SMarko Zec NULL); 182968a3482fSGleb Smirnoff if (pending > 1) 183068a3482fSGleb Smirnoff if_printf(ifp, "%d link states coalesced\n", pending); 18315515c2e7SGleb Smirnoff if (log_link_state_change) 18328b02df24SGleb Smirnoff log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname, 18338b02df24SGleb Smirnoff (link_state == LINK_STATE_UP) ? "UP" : "DOWN" ); 18348b615593SMarko Zec CURVNET_RESTORE(); 183594f5c9cfSSam Leffler } 183694f5c9cfSSam Leffler 1837df8bae1dSRodney W. Grimes /* 1838e8c2601dSPoul-Henning Kamp * Mark an interface down and notify protocols of 1839e8c2601dSPoul-Henning Kamp * the transition. 1840e8c2601dSPoul-Henning Kamp * NOTE: must be called at splnet or eqivalent. 1841e8c2601dSPoul-Henning Kamp */ 1842e8c2601dSPoul-Henning Kamp void 184372fd1b6aSDag-Erling Smørgrav if_down(struct ifnet *ifp) 1844e8c2601dSPoul-Henning Kamp { 1845e8c2601dSPoul-Henning Kamp 1846e8c2601dSPoul-Henning Kamp if_unroute(ifp, IFF_UP, AF_UNSPEC); 1847e8c2601dSPoul-Henning Kamp } 1848e8c2601dSPoul-Henning Kamp 1849e8c2601dSPoul-Henning Kamp /* 1850e8c2601dSPoul-Henning Kamp * Mark an interface up and notify protocols of 1851e8c2601dSPoul-Henning Kamp * the transition. 1852e8c2601dSPoul-Henning Kamp * NOTE: must be called at splnet or eqivalent. 1853e8c2601dSPoul-Henning Kamp */ 1854e8c2601dSPoul-Henning Kamp void 185572fd1b6aSDag-Erling Smørgrav if_up(struct ifnet *ifp) 1856e8c2601dSPoul-Henning Kamp { 1857e8c2601dSPoul-Henning Kamp 1858e8c2601dSPoul-Henning Kamp if_route(ifp, IFF_UP, AF_UNSPEC); 1859e8c2601dSPoul-Henning Kamp } 1860e8c2601dSPoul-Henning Kamp 1861e8c2601dSPoul-Henning Kamp /* 1862df8bae1dSRodney W. Grimes * Flush an interface queue. 1863df8bae1dSRodney W. Grimes */ 18647cc5b47fSKip Macy void 1865db7f0b97SKip Macy if_qflush(struct ifnet *ifp) 1866df8bae1dSRodney W. Grimes { 186772fd1b6aSDag-Erling Smørgrav struct mbuf *m, *n; 1868db7f0b97SKip Macy struct ifaltq *ifq; 1869df8bae1dSRodney W. Grimes 1870db7f0b97SKip Macy ifq = &ifp->if_snd; 18717b21048cSMax Laier IFQ_LOCK(ifq); 187202b199f1SMax Laier #ifdef ALTQ 187302b199f1SMax Laier if (ALTQ_IS_ENABLED(ifq)) 187402b199f1SMax Laier ALTQ_PURGE(ifq); 187502b199f1SMax Laier #endif 1876df8bae1dSRodney W. Grimes n = ifq->ifq_head; 18779448326fSPoul-Henning Kamp while ((m = n) != 0) { 1878df8bae1dSRodney W. Grimes n = m->m_act; 1879df8bae1dSRodney W. Grimes m_freem(m); 1880df8bae1dSRodney W. Grimes } 1881df8bae1dSRodney W. Grimes ifq->ifq_head = 0; 1882df8bae1dSRodney W. Grimes ifq->ifq_tail = 0; 1883df8bae1dSRodney W. Grimes ifq->ifq_len = 0; 18847b21048cSMax Laier IFQ_UNLOCK(ifq); 1885df8bae1dSRodney W. Grimes } 1886df8bae1dSRodney W. Grimes 1887df8bae1dSRodney W. Grimes /* 1888df8bae1dSRodney W. Grimes * Handle interface watchdog timer routines. Called 1889df8bae1dSRodney W. Grimes * from softclock, we decrement timers (if set) and 1890df8bae1dSRodney W. Grimes * call the appropriate interface routine on expiration. 1891af5e59bfSRobert Watson * 1892af5e59bfSRobert Watson * XXXRW: Note that because timeouts run with Giant, if_watchdog() is called 1893e5adda3dSRobert Watson * holding Giant. 1894df8bae1dSRodney W. Grimes */ 18953bda9f9bSPoul-Henning Kamp static void 189672fd1b6aSDag-Erling Smørgrav if_slowtimo(void *arg) 1897df8bae1dSRodney W. Grimes { 18988b615593SMarko Zec VNET_ITERATOR_DECL(vnet_iter); 189972fd1b6aSDag-Erling Smørgrav struct ifnet *ifp; 1900df8bae1dSRodney W. Grimes int s = splimp(); 1901df8bae1dSRodney W. Grimes 19025ee847d3SRobert Watson VNET_LIST_RLOCK_NOSLEEP(); 190377dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 19048b615593SMarko Zec VNET_FOREACH(vnet_iter) { 19058b615593SMarko Zec CURVNET_SET(vnet_iter); 1906603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1907df8bae1dSRodney W. Grimes if (ifp->if_timer == 0 || --ifp->if_timer) 1908df8bae1dSRodney W. Grimes continue; 1909df8bae1dSRodney W. Grimes if (ifp->if_watchdog) 19104a5f1499SDavid Greenman (*ifp->if_watchdog)(ifp); 1911df8bae1dSRodney W. Grimes } 19128b615593SMarko Zec CURVNET_RESTORE(); 19138b615593SMarko Zec } 191477dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 19155ee847d3SRobert Watson VNET_LIST_RUNLOCK_NOSLEEP(); 1916df8bae1dSRodney W. Grimes splx(s); 1917df8bae1dSRodney W. Grimes timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ); 1918df8bae1dSRodney W. Grimes } 1919df8bae1dSRodney W. Grimes 1920df8bae1dSRodney W. Grimes /* 19216064c5d3SRobert Watson * Map interface name to interface structure pointer, with or without 19226064c5d3SRobert Watson * returning a reference. 1923df8bae1dSRodney W. Grimes */ 1924df8bae1dSRodney W. Grimes struct ifnet * 19256064c5d3SRobert Watson ifunit_ref(const char *name) 19266064c5d3SRobert Watson { 19276064c5d3SRobert Watson struct ifnet *ifp; 19286064c5d3SRobert Watson 192977dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 19306064c5d3SRobert Watson TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 19318bd015a1SRobert Watson if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 && 19328bd015a1SRobert Watson !(ifp->if_flags & IFF_DYING)) 19336064c5d3SRobert Watson break; 19346064c5d3SRobert Watson } 19356064c5d3SRobert Watson if (ifp != NULL) 19366064c5d3SRobert Watson if_ref(ifp); 193777dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 19386064c5d3SRobert Watson return (ifp); 19396064c5d3SRobert Watson } 19406064c5d3SRobert Watson 19416064c5d3SRobert Watson struct ifnet * 194230aad87dSBrooks Davis ifunit(const char *name) 1943df8bae1dSRodney W. Grimes { 19448b7805e4SBoris Popov struct ifnet *ifp; 1945df8bae1dSRodney W. Grimes 194677dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 1947603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 194836c19a57SBrooks Davis if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0) 1949df8bae1dSRodney W. Grimes break; 1950df8bae1dSRodney W. Grimes } 195177dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 1952df8bae1dSRodney W. Grimes return (ifp); 1953df8bae1dSRodney W. Grimes } 1954df8bae1dSRodney W. Grimes 195582cd038dSYoshinobu Inoue /* 1956f13ad206SJonathan Lemon * Hardware specific interface ioctls. 1957df8bae1dSRodney W. Grimes */ 1958f13ad206SJonathan Lemon static int 1959f13ad206SJonathan Lemon ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td) 1960df8bae1dSRodney W. Grimes { 1961f13ad206SJonathan Lemon struct ifreq *ifr; 1962413dd0baSPoul-Henning Kamp struct ifstat *ifs; 1963f13ad206SJonathan Lemon int error = 0; 1964292ee7beSRobert Watson int new_flags, temp_flags; 196536c19a57SBrooks Davis size_t namelen, onamelen; 196636c19a57SBrooks Davis char new_name[IFNAMSIZ]; 196736c19a57SBrooks Davis struct ifaddr *ifa; 196836c19a57SBrooks Davis struct sockaddr_dl *sdl; 1969df8bae1dSRodney W. Grimes 1970df8bae1dSRodney W. Grimes ifr = (struct ifreq *)data; 197130aad87dSBrooks Davis switch (cmd) { 1972de593450SJonathan Lemon case SIOCGIFINDEX: 1973de593450SJonathan Lemon ifr->ifr_index = ifp->if_index; 1974de593450SJonathan Lemon break; 1975de593450SJonathan Lemon 1976df8bae1dSRodney W. Grimes case SIOCGIFFLAGS: 1977292ee7beSRobert Watson temp_flags = ifp->if_flags | ifp->if_drv_flags; 1978292ee7beSRobert Watson ifr->ifr_flags = temp_flags & 0xffff; 1979292ee7beSRobert Watson ifr->ifr_flagshigh = temp_flags >> 16; 1980df8bae1dSRodney W. Grimes break; 1981df8bae1dSRodney W. Grimes 1982016da741SJonathan Lemon case SIOCGIFCAP: 1983016da741SJonathan Lemon ifr->ifr_reqcap = ifp->if_capabilities; 1984016da741SJonathan Lemon ifr->ifr_curcap = ifp->if_capenable; 1985016da741SJonathan Lemon break; 1986016da741SJonathan Lemon 19878f293a63SRobert Watson #ifdef MAC 19888f293a63SRobert Watson case SIOCGIFMAC: 198930d239bcSRobert Watson error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp); 19908f293a63SRobert Watson break; 19918f293a63SRobert Watson #endif 19928f293a63SRobert Watson 1993df8bae1dSRodney W. Grimes case SIOCGIFMETRIC: 1994df8bae1dSRodney W. Grimes ifr->ifr_metric = ifp->if_metric; 1995df8bae1dSRodney W. Grimes break; 1996df8bae1dSRodney W. Grimes 1997a7028af7SDavid Greenman case SIOCGIFMTU: 1998a7028af7SDavid Greenman ifr->ifr_mtu = ifp->if_mtu; 1999a7028af7SDavid Greenman break; 2000a7028af7SDavid Greenman 2001074c4a4eSGarrett Wollman case SIOCGIFPHYS: 2002074c4a4eSGarrett Wollman ifr->ifr_phys = ifp->if_physical; 2003074c4a4eSGarrett Wollman break; 2004074c4a4eSGarrett Wollman 2005df8bae1dSRodney W. Grimes case SIOCSIFFLAGS: 2006acd3428bSRobert Watson error = priv_check(td, PRIV_NET_SETIFFLAGS); 20079448326fSPoul-Henning Kamp if (error) 2008df8bae1dSRodney W. Grimes return (error); 2009292ee7beSRobert Watson /* 2010292ee7beSRobert Watson * Currently, no driver owned flags pass the IFF_CANTCHANGE 2011292ee7beSRobert Watson * check, so we don't need special handling here yet. 2012292ee7beSRobert Watson */ 201362f76486SMaxim Sobolev new_flags = (ifr->ifr_flags & 0xffff) | 201462f76486SMaxim Sobolev (ifr->ifr_flagshigh << 16); 2015cf4b9371SPoul-Henning Kamp if (ifp->if_flags & IFF_SMART) { 2016cf4b9371SPoul-Henning Kamp /* Smart drivers twiddle their own routes */ 20172f55ead7SPoul-Henning Kamp } else if (ifp->if_flags & IFF_UP && 201862f76486SMaxim Sobolev (new_flags & IFF_UP) == 0) { 2019df8bae1dSRodney W. Grimes int s = splimp(); 2020df8bae1dSRodney W. Grimes if_down(ifp); 2021df8bae1dSRodney W. Grimes splx(s); 202262f76486SMaxim Sobolev } else if (new_flags & IFF_UP && 2023cf4b9371SPoul-Henning Kamp (ifp->if_flags & IFF_UP) == 0) { 2024df8bae1dSRodney W. Grimes int s = splimp(); 2025df8bae1dSRodney W. Grimes if_up(ifp); 2026df8bae1dSRodney W. Grimes splx(s); 2027df8bae1dSRodney W. Grimes } 20287aebc5e8SYaroslav Tykhiy /* See if permanently promiscuous mode bit is about to flip */ 20297aebc5e8SYaroslav Tykhiy if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) { 20307aebc5e8SYaroslav Tykhiy if (new_flags & IFF_PPROMISC) 20317aebc5e8SYaroslav Tykhiy ifp->if_flags |= IFF_PROMISC; 20327aebc5e8SYaroslav Tykhiy else if (ifp->if_pcount == 0) 20337aebc5e8SYaroslav Tykhiy ifp->if_flags &= ~IFF_PROMISC; 20347aebc5e8SYaroslav Tykhiy log(LOG_INFO, "%s: permanently promiscuous mode %s\n", 20357aebc5e8SYaroslav Tykhiy ifp->if_xname, 20367aebc5e8SYaroslav Tykhiy (new_flags & IFF_PPROMISC) ? "enabled" : "disabled"); 20377aebc5e8SYaroslav Tykhiy } 2038df8bae1dSRodney W. Grimes ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 203962f76486SMaxim Sobolev (new_flags &~ IFF_CANTCHANGE); 204031302ebfSRobert Watson if (ifp->if_ioctl) { 2041df8bae1dSRodney W. Grimes (void) (*ifp->if_ioctl)(ifp, cmd, data); 204231302ebfSRobert Watson } 204398b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 2044df8bae1dSRodney W. Grimes break; 2045df8bae1dSRodney W. Grimes 2046016da741SJonathan Lemon case SIOCSIFCAP: 2047acd3428bSRobert Watson error = priv_check(td, PRIV_NET_SETIFCAP); 2048016da741SJonathan Lemon if (error) 2049016da741SJonathan Lemon return (error); 2050efb4018bSYaroslav Tykhiy if (ifp->if_ioctl == NULL) 2051efb4018bSYaroslav Tykhiy return (EOPNOTSUPP); 2052016da741SJonathan Lemon if (ifr->ifr_reqcap & ~ifp->if_capabilities) 2053016da741SJonathan Lemon return (EINVAL); 2054efb4018bSYaroslav Tykhiy error = (*ifp->if_ioctl)(ifp, cmd, data); 2055efb4018bSYaroslav Tykhiy if (error == 0) 2056efb4018bSYaroslav Tykhiy getmicrotime(&ifp->if_lastchange); 2057016da741SJonathan Lemon break; 2058016da741SJonathan Lemon 20598f293a63SRobert Watson #ifdef MAC 20608f293a63SRobert Watson case SIOCSIFMAC: 206130d239bcSRobert Watson error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp); 20628f293a63SRobert Watson break; 20638f293a63SRobert Watson #endif 20648f293a63SRobert Watson 206536c19a57SBrooks Davis case SIOCSIFNAME: 2066acd3428bSRobert Watson error = priv_check(td, PRIV_NET_SETIFNAME); 2067acd3428bSRobert Watson if (error) 206836c19a57SBrooks Davis return (error); 206936c19a57SBrooks Davis error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL); 2070bc1470f1SBrooks Davis if (error != 0) 207136c19a57SBrooks Davis return (error); 2072bc1470f1SBrooks Davis if (new_name[0] == '\0') 2073bc1470f1SBrooks Davis return (EINVAL); 207436c19a57SBrooks Davis if (ifunit(new_name) != NULL) 207536c19a57SBrooks Davis return (EEXIST); 207636c19a57SBrooks Davis 207736c19a57SBrooks Davis /* Announce the departure of the interface. */ 207836c19a57SBrooks Davis rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 207952023244SMax Laier EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 208036c19a57SBrooks Davis 208171672bb6SBrooks Davis log(LOG_INFO, "%s: changing name to '%s'\n", 208271672bb6SBrooks Davis ifp->if_xname, new_name); 208371672bb6SBrooks Davis 208436c19a57SBrooks Davis strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname)); 20854a0d6638SRuslan Ermilov ifa = ifp->if_addr; 208636c19a57SBrooks Davis IFA_LOCK(ifa); 208736c19a57SBrooks Davis sdl = (struct sockaddr_dl *)ifa->ifa_addr; 208836c19a57SBrooks Davis namelen = strlen(new_name); 208936c19a57SBrooks Davis onamelen = sdl->sdl_nlen; 209036c19a57SBrooks Davis /* 209136c19a57SBrooks Davis * Move the address if needed. This is safe because we 209236c19a57SBrooks Davis * allocate space for a name of length IFNAMSIZ when we 209336c19a57SBrooks Davis * create this in if_attach(). 209436c19a57SBrooks Davis */ 209536c19a57SBrooks Davis if (namelen != onamelen) { 209636c19a57SBrooks Davis bcopy(sdl->sdl_data + onamelen, 209736c19a57SBrooks Davis sdl->sdl_data + namelen, sdl->sdl_alen); 209836c19a57SBrooks Davis } 209936c19a57SBrooks Davis bcopy(new_name, sdl->sdl_data, namelen); 210036c19a57SBrooks Davis sdl->sdl_nlen = namelen; 210136c19a57SBrooks Davis sdl = (struct sockaddr_dl *)ifa->ifa_netmask; 210236c19a57SBrooks Davis bzero(sdl->sdl_data, onamelen); 210336c19a57SBrooks Davis while (namelen != 0) 210436c19a57SBrooks Davis sdl->sdl_data[--namelen] = 0xff; 210536c19a57SBrooks Davis IFA_UNLOCK(ifa); 210636c19a57SBrooks Davis 210725a4adceSMax Laier EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 210836c19a57SBrooks Davis /* Announce the return of the interface. */ 210936c19a57SBrooks Davis rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 211036c19a57SBrooks Davis break; 211136c19a57SBrooks Davis 2112679e1390SJamie Gritton #ifdef VIMAGE 2113679e1390SJamie Gritton case SIOCSIFVNET: 2114679e1390SJamie Gritton error = priv_check(td, PRIV_NET_SETIFVNET); 2115679e1390SJamie Gritton if (error) 2116679e1390SJamie Gritton return (error); 2117be31e5e7SBjoern A. Zeeb error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid); 2118679e1390SJamie Gritton break; 2119679e1390SJamie Gritton #endif 2120679e1390SJamie Gritton 2121df8bae1dSRodney W. Grimes case SIOCSIFMETRIC: 2122acd3428bSRobert Watson error = priv_check(td, PRIV_NET_SETIFMETRIC); 21239448326fSPoul-Henning Kamp if (error) 2124df8bae1dSRodney W. Grimes return (error); 2125df8bae1dSRodney W. Grimes ifp->if_metric = ifr->ifr_metric; 212698b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 2127df8bae1dSRodney W. Grimes break; 2128df8bae1dSRodney W. Grimes 2129074c4a4eSGarrett Wollman case SIOCSIFPHYS: 2130acd3428bSRobert Watson error = priv_check(td, PRIV_NET_SETIFPHYS); 2131e39a0280SGary Palmer if (error) 2132913e410eSYaroslav Tykhiy return (error); 2133913e410eSYaroslav Tykhiy if (ifp->if_ioctl == NULL) 2134913e410eSYaroslav Tykhiy return (EOPNOTSUPP); 2135e39a0280SGary Palmer error = (*ifp->if_ioctl)(ifp, cmd, data); 2136e39a0280SGary Palmer if (error == 0) 213798b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 2138913e410eSYaroslav Tykhiy break; 2139074c4a4eSGarrett Wollman 2140a7028af7SDavid Greenman case SIOCSIFMTU: 214182cd038dSYoshinobu Inoue { 214282cd038dSYoshinobu Inoue u_long oldmtu = ifp->if_mtu; 214382cd038dSYoshinobu Inoue 2144acd3428bSRobert Watson error = priv_check(td, PRIV_NET_SETIFMTU); 21459448326fSPoul-Henning Kamp if (error) 2146a7028af7SDavid Greenman return (error); 2147aab3beeeSBrian Somers if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) 214875ee03cbSDavid Greenman return (EINVAL); 2149f13ad206SJonathan Lemon if (ifp->if_ioctl == NULL) 2150f13ad206SJonathan Lemon return (EOPNOTSUPP); 2151e39a0280SGary Palmer error = (*ifp->if_ioctl)(ifp, cmd, data); 215248f71763SRuslan Ermilov if (error == 0) { 215398b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 215448f71763SRuslan Ermilov rt_ifmsg(ifp); 215548f71763SRuslan Ermilov } 215682cd038dSYoshinobu Inoue /* 215782cd038dSYoshinobu Inoue * If the link MTU changed, do network layer specific procedure. 215882cd038dSYoshinobu Inoue */ 215982cd038dSYoshinobu Inoue if (ifp->if_mtu != oldmtu) { 216082cd038dSYoshinobu Inoue #ifdef INET6 216182cd038dSYoshinobu Inoue nd6_setmtu(ifp); 216282cd038dSYoshinobu Inoue #endif 216382cd038dSYoshinobu Inoue } 2164f13ad206SJonathan Lemon break; 216582cd038dSYoshinobu Inoue } 2166a7028af7SDavid Greenman 2167df8bae1dSRodney W. Grimes case SIOCADDMULTI: 2168df8bae1dSRodney W. Grimes case SIOCDELMULTI: 2169acd3428bSRobert Watson if (cmd == SIOCADDMULTI) 2170acd3428bSRobert Watson error = priv_check(td, PRIV_NET_ADDMULTI); 2171acd3428bSRobert Watson else 2172acd3428bSRobert Watson error = priv_check(td, PRIV_NET_DELMULTI); 21739448326fSPoul-Henning Kamp if (error) 2174df8bae1dSRodney W. Grimes return (error); 2175477180fbSGarrett Wollman 2176477180fbSGarrett Wollman /* Don't allow group membership on non-multicast interfaces. */ 2177477180fbSGarrett Wollman if ((ifp->if_flags & IFF_MULTICAST) == 0) 2178f13ad206SJonathan Lemon return (EOPNOTSUPP); 2179477180fbSGarrett Wollman 2180477180fbSGarrett Wollman /* Don't let users screw up protocols' entries. */ 2181477180fbSGarrett Wollman if (ifr->ifr_addr.sa_family != AF_LINK) 2182f13ad206SJonathan Lemon return (EINVAL); 2183477180fbSGarrett Wollman 2184477180fbSGarrett Wollman if (cmd == SIOCADDMULTI) { 2185477180fbSGarrett Wollman struct ifmultiaddr *ifma; 2186ec002feeSBruce M Simpson 2187ec002feeSBruce M Simpson /* 2188ec002feeSBruce M Simpson * Userland is only permitted to join groups once 2189ec002feeSBruce M Simpson * via the if_addmulti() KPI, because it cannot hold 2190ec002feeSBruce M Simpson * struct ifmultiaddr * between calls. It may also 2191ec002feeSBruce M Simpson * lose a race while we check if the membership 2192ec002feeSBruce M Simpson * already exists. 2193ec002feeSBruce M Simpson */ 2194ec002feeSBruce M Simpson IF_ADDR_LOCK(ifp); 2195ec002feeSBruce M Simpson ifma = if_findmulti(ifp, &ifr->ifr_addr); 2196ec002feeSBruce M Simpson IF_ADDR_UNLOCK(ifp); 2197ec002feeSBruce M Simpson if (ifma != NULL) 2198ec002feeSBruce M Simpson error = EADDRINUSE; 2199ec002feeSBruce M Simpson else 2200477180fbSGarrett Wollman error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 2201477180fbSGarrett Wollman } else { 2202477180fbSGarrett Wollman error = if_delmulti(ifp, &ifr->ifr_addr); 2203477180fbSGarrett Wollman } 2204e39a0280SGary Palmer if (error == 0) 220598b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 2206f13ad206SJonathan Lemon break; 2207df8bae1dSRodney W. Grimes 220841b3e8e5SJun-ichiro itojun Hagino case SIOCSIFPHYADDR: 220941b3e8e5SJun-ichiro itojun Hagino case SIOCDIFPHYADDR: 221041b3e8e5SJun-ichiro itojun Hagino #ifdef INET6 221141b3e8e5SJun-ichiro itojun Hagino case SIOCSIFPHYADDR_IN6: 221241b3e8e5SJun-ichiro itojun Hagino #endif 221333841545SHajimu UMEMOTO case SIOCSLIFPHYADDR: 2214a912e453SPeter Wemm case SIOCSIFMEDIA: 2215d7189ec6SJoerg Wunsch case SIOCSIFGENERIC: 2216acd3428bSRobert Watson error = priv_check(td, PRIV_NET_HWIOCTL); 2217a912e453SPeter Wemm if (error) 2218a912e453SPeter Wemm return (error); 2219f13ad206SJonathan Lemon if (ifp->if_ioctl == NULL) 2220a912e453SPeter Wemm return (EOPNOTSUPP); 2221a912e453SPeter Wemm error = (*ifp->if_ioctl)(ifp, cmd, data); 2222a912e453SPeter Wemm if (error == 0) 222398b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 2224f13ad206SJonathan Lemon break; 2225a912e453SPeter Wemm 2226413dd0baSPoul-Henning Kamp case SIOCGIFSTATUS: 2227413dd0baSPoul-Henning Kamp ifs = (struct ifstat *)data; 2228413dd0baSPoul-Henning Kamp ifs->ascii[0] = '\0'; 2229413dd0baSPoul-Henning Kamp 223033841545SHajimu UMEMOTO case SIOCGIFPSRCADDR: 223133841545SHajimu UMEMOTO case SIOCGIFPDSTADDR: 223233841545SHajimu UMEMOTO case SIOCGLIFPHYADDR: 2233a912e453SPeter Wemm case SIOCGIFMEDIA: 2234d7189ec6SJoerg Wunsch case SIOCGIFGENERIC: 2235913e410eSYaroslav Tykhiy if (ifp->if_ioctl == NULL) 2236a912e453SPeter Wemm return (EOPNOTSUPP); 2237f13ad206SJonathan Lemon error = (*ifp->if_ioctl)(ifp, cmd, data); 2238f13ad206SJonathan Lemon break; 2239a912e453SPeter Wemm 2240b106252cSBill Paul case SIOCSIFLLADDR: 2241acd3428bSRobert Watson error = priv_check(td, PRIV_NET_SETLLADDR); 2242b106252cSBill Paul if (error) 2243b106252cSBill Paul return (error); 2244f13ad206SJonathan Lemon error = if_setlladdr(ifp, 224566ce51ceSArchie Cobbs ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); 2246f13ad206SJonathan Lemon break; 224766ce51ceSArchie Cobbs 22480dad3f0eSMax Laier case SIOCAIFGROUP: 22490dad3f0eSMax Laier { 22500dad3f0eSMax Laier struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; 22510dad3f0eSMax Laier 2252acd3428bSRobert Watson error = priv_check(td, PRIV_NET_ADDIFGROUP); 22530dad3f0eSMax Laier if (error) 22540dad3f0eSMax Laier return (error); 22550dad3f0eSMax Laier if ((error = if_addgroup(ifp, ifgr->ifgr_group))) 22560dad3f0eSMax Laier return (error); 22570dad3f0eSMax Laier break; 22580dad3f0eSMax Laier } 22590dad3f0eSMax Laier 22600dad3f0eSMax Laier case SIOCGIFGROUP: 22610dad3f0eSMax Laier if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp))) 22620dad3f0eSMax Laier return (error); 22630dad3f0eSMax Laier break; 22640dad3f0eSMax Laier 22650dad3f0eSMax Laier case SIOCDIFGROUP: 22660dad3f0eSMax Laier { 22670dad3f0eSMax Laier struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; 22680dad3f0eSMax Laier 2269acd3428bSRobert Watson error = priv_check(td, PRIV_NET_DELIFGROUP); 22700dad3f0eSMax Laier if (error) 22710dad3f0eSMax Laier return (error); 22720dad3f0eSMax Laier if ((error = if_delgroup(ifp, ifgr->ifgr_group))) 22730dad3f0eSMax Laier return (error); 22740dad3f0eSMax Laier break; 22750dad3f0eSMax Laier } 22760dad3f0eSMax Laier 2277df8bae1dSRodney W. Grimes default: 2278f13ad206SJonathan Lemon error = ENOIOCTL; 2279f13ad206SJonathan Lemon break; 2280f13ad206SJonathan Lemon } 2281f13ad206SJonathan Lemon return (error); 2282f13ad206SJonathan Lemon } 2283f13ad206SJonathan Lemon 2284f13ad206SJonathan Lemon /* 2285f13ad206SJonathan Lemon * Interface ioctls. 2286f13ad206SJonathan Lemon */ 2287f13ad206SJonathan Lemon int 228872fd1b6aSDag-Erling Smørgrav ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td) 2289f13ad206SJonathan Lemon { 2290f13ad206SJonathan Lemon struct ifnet *ifp; 2291f13ad206SJonathan Lemon struct ifreq *ifr; 2292f13ad206SJonathan Lemon int error; 229362f76486SMaxim Sobolev int oif_flags; 2294f13ad206SJonathan Lemon 2295f13ad206SJonathan Lemon switch (cmd) { 2296f13ad206SJonathan Lemon case SIOCGIFCONF: 2297f13ad206SJonathan Lemon case OSIOCGIFCONF: 229819cf0498SPaul Saab #ifdef __amd64__ 229919cf0498SPaul Saab case SIOCGIFCONF32: 230019cf0498SPaul Saab #endif 2301f13ad206SJonathan Lemon return (ifconf(cmd, data)); 2302f13ad206SJonathan Lemon } 2303f13ad206SJonathan Lemon ifr = (struct ifreq *)data; 2304f13ad206SJonathan Lemon 2305f13ad206SJonathan Lemon switch (cmd) { 2306feb08d06SMarko Zec #ifdef VIMAGE 2307679e1390SJamie Gritton case SIOCSIFRVNET: 2308679e1390SJamie Gritton error = priv_check(td, PRIV_NET_SETIFVNET); 2309679e1390SJamie Gritton if (error) 2310679e1390SJamie Gritton return (error); 2311be31e5e7SBjoern A. Zeeb return (if_vmove_reclaim(td, ifr->ifr_name, ifr->ifr_jid)); 2312feb08d06SMarko Zec #endif 2313f13ad206SJonathan Lemon case SIOCIFCREATE: 23146b7330e2SSam Leffler case SIOCIFCREATE2: 2315acd3428bSRobert Watson error = priv_check(td, PRIV_NET_IFCREATE); 2316acd3428bSRobert Watson if (error) 23176b7330e2SSam Leffler return (error); 23186b7330e2SSam Leffler return (if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name), 23196b7330e2SSam Leffler cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL)); 2320f13ad206SJonathan Lemon case SIOCIFDESTROY: 2321acd3428bSRobert Watson error = priv_check(td, PRIV_NET_IFDESTROY); 2322acd3428bSRobert Watson if (error) 2323f13ad206SJonathan Lemon return (error); 23246b7330e2SSam Leffler return if_clone_destroy(ifr->ifr_name); 2325f13ad206SJonathan Lemon 2326f13ad206SJonathan Lemon case SIOCIFGCLONERS: 2327f13ad206SJonathan Lemon return (if_clone_list((struct if_clonereq *)data)); 23280dad3f0eSMax Laier case SIOCGIFGMEMB: 23290dad3f0eSMax Laier return (if_getgroupmembers((struct ifgroupreq *)data)); 2330f13ad206SJonathan Lemon } 2331f13ad206SJonathan Lemon 23326064c5d3SRobert Watson ifp = ifunit_ref(ifr->ifr_name); 23336064c5d3SRobert Watson if (ifp == NULL) 2334f13ad206SJonathan Lemon return (ENXIO); 2335f13ad206SJonathan Lemon 2336f13ad206SJonathan Lemon error = ifhwioctl(cmd, ifp, data, td); 23376064c5d3SRobert Watson if (error != ENOIOCTL) { 23386064c5d3SRobert Watson if_rele(ifp); 2339f13ad206SJonathan Lemon return (error); 23406064c5d3SRobert Watson } 2341f13ad206SJonathan Lemon 234282cd038dSYoshinobu Inoue oif_flags = ifp->if_flags; 23436064c5d3SRobert Watson if (so->so_proto == NULL) { 23446064c5d3SRobert Watson if_rele(ifp); 2345df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 23466064c5d3SRobert Watson } 2347df8bae1dSRodney W. Grimes #ifndef COMPAT_43 234882cd038dSYoshinobu Inoue error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, 23492c37256eSGarrett Wollman data, 2350b40ce416SJulian Elischer ifp, td)); 2351bc3977f1SJamie Gritton if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL) 2352bc3977f1SJamie Gritton error = (*ifp->if_ioctl)(ifp, cmd, data); 2353df8bae1dSRodney W. Grimes #else 2354df8bae1dSRodney W. Grimes { 2355e40bae9aSRoman Divacky u_long ocmd = cmd; 2356df8bae1dSRodney W. Grimes 2357df8bae1dSRodney W. Grimes switch (cmd) { 2358df8bae1dSRodney W. Grimes 2359df8bae1dSRodney W. Grimes case SIOCSIFDSTADDR: 2360df8bae1dSRodney W. Grimes case SIOCSIFADDR: 2361df8bae1dSRodney W. Grimes case SIOCSIFBRDADDR: 2362df8bae1dSRodney W. Grimes case SIOCSIFNETMASK: 2363df8bae1dSRodney W. Grimes #if BYTE_ORDER != BIG_ENDIAN 2364df8bae1dSRodney W. Grimes if (ifr->ifr_addr.sa_family == 0 && 2365df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_len < 16) { 2366df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 2367df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_len = 16; 2368df8bae1dSRodney W. Grimes } 2369df8bae1dSRodney W. Grimes #else 2370df8bae1dSRodney W. Grimes if (ifr->ifr_addr.sa_len == 0) 2371df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_len = 16; 2372df8bae1dSRodney W. Grimes #endif 2373df8bae1dSRodney W. Grimes break; 2374df8bae1dSRodney W. Grimes 2375df8bae1dSRodney W. Grimes case OSIOCGIFADDR: 2376df8bae1dSRodney W. Grimes cmd = SIOCGIFADDR; 2377df8bae1dSRodney W. Grimes break; 2378df8bae1dSRodney W. Grimes 2379df8bae1dSRodney W. Grimes case OSIOCGIFDSTADDR: 2380df8bae1dSRodney W. Grimes cmd = SIOCGIFDSTADDR; 2381df8bae1dSRodney W. Grimes break; 2382df8bae1dSRodney W. Grimes 2383df8bae1dSRodney W. Grimes case OSIOCGIFBRDADDR: 2384df8bae1dSRodney W. Grimes cmd = SIOCGIFBRDADDR; 2385df8bae1dSRodney W. Grimes break; 2386df8bae1dSRodney W. Grimes 2387df8bae1dSRodney W. Grimes case OSIOCGIFNETMASK: 2388df8bae1dSRodney W. Grimes cmd = SIOCGIFNETMASK; 2389df8bae1dSRodney W. Grimes } 23902c37256eSGarrett Wollman error = ((*so->so_proto->pr_usrreqs->pru_control)(so, 23912c37256eSGarrett Wollman cmd, 23922c37256eSGarrett Wollman data, 2393b40ce416SJulian Elischer ifp, td)); 2394bc3977f1SJamie Gritton if (error == EOPNOTSUPP && ifp != NULL && 2395bc3977f1SJamie Gritton ifp->if_ioctl != NULL) 2396bc3977f1SJamie Gritton error = (*ifp->if_ioctl)(ifp, cmd, data); 2397df8bae1dSRodney W. Grimes switch (ocmd) { 2398df8bae1dSRodney W. Grimes 2399df8bae1dSRodney W. Grimes case OSIOCGIFADDR: 2400df8bae1dSRodney W. Grimes case OSIOCGIFDSTADDR: 2401df8bae1dSRodney W. Grimes case OSIOCGIFBRDADDR: 2402df8bae1dSRodney W. Grimes case OSIOCGIFNETMASK: 2403df8bae1dSRodney W. Grimes *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 240482cd038dSYoshinobu Inoue 240582cd038dSYoshinobu Inoue } 240682cd038dSYoshinobu Inoue } 240782cd038dSYoshinobu Inoue #endif /* COMPAT_43 */ 240882cd038dSYoshinobu Inoue 240982cd038dSYoshinobu Inoue if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 241082cd038dSYoshinobu Inoue #ifdef INET6 241182cd038dSYoshinobu Inoue if (ifp->if_flags & IFF_UP) { 241282cd038dSYoshinobu Inoue int s = splimp(); 241382cd038dSYoshinobu Inoue in6_if_up(ifp); 241482cd038dSYoshinobu Inoue splx(s); 241582cd038dSYoshinobu Inoue } 241682cd038dSYoshinobu Inoue #endif 2417df8bae1dSRodney W. Grimes } 24186064c5d3SRobert Watson if_rele(ifp); 2419df8bae1dSRodney W. Grimes return (error); 2420df8bae1dSRodney W. Grimes } 2421df8bae1dSRodney W. Grimes 2422df8bae1dSRodney W. Grimes /* 2423292ee7beSRobert Watson * The code common to handling reference counted flags, 24241a3b6859SYaroslav Tykhiy * e.g., in ifpromisc() and if_allmulti(). 2425b5c8bd59SYaroslav Tykhiy * The "pflag" argument can specify a permanent mode flag to check, 24261a3b6859SYaroslav Tykhiy * such as IFF_PPROMISC for promiscuous mode; should be 0 if none. 2427292ee7beSRobert Watson * 2428292ee7beSRobert Watson * Only to be used on stack-owned flags, not driver-owned flags. 24291a3b6859SYaroslav Tykhiy */ 24301a3b6859SYaroslav Tykhiy static int 24311a3b6859SYaroslav Tykhiy if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch) 24321a3b6859SYaroslav Tykhiy { 24331a3b6859SYaroslav Tykhiy struct ifreq ifr; 24341a3b6859SYaroslav Tykhiy int error; 24351a3b6859SYaroslav Tykhiy int oldflags, oldcount; 24361a3b6859SYaroslav Tykhiy 24371a3b6859SYaroslav Tykhiy /* Sanity checks to catch programming errors */ 2438b5c8bd59SYaroslav Tykhiy KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0, 2439b5c8bd59SYaroslav Tykhiy ("%s: setting driver-owned flag %d", __func__, flag)); 2440b5c8bd59SYaroslav Tykhiy 2441b5c8bd59SYaroslav Tykhiy if (onswitch) 2442b5c8bd59SYaroslav Tykhiy KASSERT(*refcount >= 0, 2443b5c8bd59SYaroslav Tykhiy ("%s: increment negative refcount %d for flag %d", 2444b5c8bd59SYaroslav Tykhiy __func__, *refcount, flag)); 2445b5c8bd59SYaroslav Tykhiy else 2446b5c8bd59SYaroslav Tykhiy KASSERT(*refcount > 0, 2447b5c8bd59SYaroslav Tykhiy ("%s: decrement non-positive refcount %d for flag %d", 2448b5c8bd59SYaroslav Tykhiy __func__, *refcount, flag)); 24491a3b6859SYaroslav Tykhiy 24501a3b6859SYaroslav Tykhiy /* In case this mode is permanent, just touch refcount */ 24511a3b6859SYaroslav Tykhiy if (ifp->if_flags & pflag) { 24521a3b6859SYaroslav Tykhiy *refcount += onswitch ? 1 : -1; 24531a3b6859SYaroslav Tykhiy return (0); 24541a3b6859SYaroslav Tykhiy } 24551a3b6859SYaroslav Tykhiy 24561a3b6859SYaroslav Tykhiy /* Save ifnet parameters for if_ioctl() may fail */ 24571a3b6859SYaroslav Tykhiy oldcount = *refcount; 24581a3b6859SYaroslav Tykhiy oldflags = ifp->if_flags; 24591a3b6859SYaroslav Tykhiy 24601a3b6859SYaroslav Tykhiy /* 24611a3b6859SYaroslav Tykhiy * See if we aren't the only and touching refcount is enough. 24621a3b6859SYaroslav Tykhiy * Actually toggle interface flag if we are the first or last. 24631a3b6859SYaroslav Tykhiy */ 24641a3b6859SYaroslav Tykhiy if (onswitch) { 24651a3b6859SYaroslav Tykhiy if ((*refcount)++) 24661a3b6859SYaroslav Tykhiy return (0); 24671a3b6859SYaroslav Tykhiy ifp->if_flags |= flag; 24681a3b6859SYaroslav Tykhiy } else { 24691a3b6859SYaroslav Tykhiy if (--(*refcount)) 24701a3b6859SYaroslav Tykhiy return (0); 24711a3b6859SYaroslav Tykhiy ifp->if_flags &= ~flag; 24721a3b6859SYaroslav Tykhiy } 24731a3b6859SYaroslav Tykhiy 24741a3b6859SYaroslav Tykhiy /* Call down the driver since we've changed interface flags */ 24751a3b6859SYaroslav Tykhiy if (ifp->if_ioctl == NULL) { 24761a3b6859SYaroslav Tykhiy error = EOPNOTSUPP; 24771a3b6859SYaroslav Tykhiy goto recover; 24781a3b6859SYaroslav Tykhiy } 24791a3b6859SYaroslav Tykhiy ifr.ifr_flags = ifp->if_flags & 0xffff; 24801a3b6859SYaroslav Tykhiy ifr.ifr_flagshigh = ifp->if_flags >> 16; 24811a3b6859SYaroslav Tykhiy error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 24821a3b6859SYaroslav Tykhiy if (error) 24831a3b6859SYaroslav Tykhiy goto recover; 24841a3b6859SYaroslav Tykhiy /* Notify userland that interface flags have changed */ 24851a3b6859SYaroslav Tykhiy rt_ifmsg(ifp); 24861a3b6859SYaroslav Tykhiy return (0); 24871a3b6859SYaroslav Tykhiy 24881a3b6859SYaroslav Tykhiy recover: 24891a3b6859SYaroslav Tykhiy /* Recover after driver error */ 24901a3b6859SYaroslav Tykhiy *refcount = oldcount; 24911a3b6859SYaroslav Tykhiy ifp->if_flags = oldflags; 24921a3b6859SYaroslav Tykhiy return (error); 24931a3b6859SYaroslav Tykhiy } 24941a3b6859SYaroslav Tykhiy 24951a3b6859SYaroslav Tykhiy /* 2496963e4c2aSGarrett Wollman * Set/clear promiscuous mode on interface ifp based on the truth value 2497963e4c2aSGarrett Wollman * of pswitch. The calls are reference counted so that only the first 2498963e4c2aSGarrett Wollman * "on" request actually has an effect, as does the final "off" request. 2499963e4c2aSGarrett Wollman * Results are undefined if the "off" and "on" requests are not matched. 2500963e4c2aSGarrett Wollman */ 2501963e4c2aSGarrett Wollman int 250272fd1b6aSDag-Erling Smørgrav ifpromisc(struct ifnet *ifp, int pswitch) 2503963e4c2aSGarrett Wollman { 25044a26224cSGarrett Wollman int error; 25051a3b6859SYaroslav Tykhiy int oldflags = ifp->if_flags; 2506963e4c2aSGarrett Wollman 25071a3b6859SYaroslav Tykhiy error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC, 25081a3b6859SYaroslav Tykhiy &ifp->if_pcount, pswitch); 25091a3b6859SYaroslav Tykhiy /* If promiscuous mode status has changed, log a message */ 25101a3b6859SYaroslav Tykhiy if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC)) 25119bf40edeSBrooks Davis log(LOG_INFO, "%s: promiscuous mode %s\n", 25129bf40edeSBrooks Davis ifp->if_xname, 25134f3c11a6SBill Fenner (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); 25141a3b6859SYaroslav Tykhiy return (error); 2515963e4c2aSGarrett Wollman } 2516963e4c2aSGarrett Wollman 2517963e4c2aSGarrett Wollman /* 2518df8bae1dSRodney W. Grimes * Return interface configuration 2519df8bae1dSRodney W. Grimes * of system. List may be used 2520df8bae1dSRodney W. Grimes * in later ioctl's (above) to get 2521df8bae1dSRodney W. Grimes * other information. 2522df8bae1dSRodney W. Grimes */ 2523df8bae1dSRodney W. Grimes /*ARGSUSED*/ 25243bda9f9bSPoul-Henning Kamp static int 252572fd1b6aSDag-Erling Smørgrav ifconf(u_long cmd, caddr_t data) 2526df8bae1dSRodney W. Grimes { 25270b59d917SJonathan Lemon struct ifconf *ifc = (struct ifconf *)data; 252819cf0498SPaul Saab #ifdef __amd64__ 252919cf0498SPaul Saab struct ifconf32 *ifc32 = (struct ifconf32 *)data; 253019cf0498SPaul Saab struct ifconf ifc_swab; 253119cf0498SPaul Saab #endif 25320b59d917SJonathan Lemon struct ifnet *ifp; 25330b59d917SJonathan Lemon struct ifaddr *ifa; 25344dcf2bbbSBrooks Davis struct ifreq ifr; 25354dcf2bbbSBrooks Davis struct sbuf *sb; 25364dcf2bbbSBrooks Davis int error, full = 0, valid_len, max_len; 2537df8bae1dSRodney W. Grimes 253819cf0498SPaul Saab #ifdef __amd64__ 253919cf0498SPaul Saab if (cmd == SIOCGIFCONF32) { 254019cf0498SPaul Saab ifc_swab.ifc_len = ifc32->ifc_len; 254119cf0498SPaul Saab ifc_swab.ifc_buf = (caddr_t)(uintptr_t)ifc32->ifc_buf; 254219cf0498SPaul Saab ifc = &ifc_swab; 254319cf0498SPaul Saab } 254419cf0498SPaul Saab #endif 25454dcf2bbbSBrooks Davis /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */ 25464dcf2bbbSBrooks Davis max_len = MAXPHYS - 1; 25474dcf2bbbSBrooks Davis 2548b0b4b28bSXin LI /* Prevent hostile input from being able to crash the system */ 2549b0b4b28bSXin LI if (ifc->ifc_len <= 0) 2550b0b4b28bSXin LI return (EINVAL); 2551b0b4b28bSXin LI 25524dcf2bbbSBrooks Davis again: 25534dcf2bbbSBrooks Davis if (ifc->ifc_len <= max_len) { 25544dcf2bbbSBrooks Davis max_len = ifc->ifc_len; 25554dcf2bbbSBrooks Davis full = 1; 25564dcf2bbbSBrooks Davis } 25574dcf2bbbSBrooks Davis sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN); 25584dcf2bbbSBrooks Davis max_len = 0; 25594dcf2bbbSBrooks Davis valid_len = 0; 25604dcf2bbbSBrooks Davis 256177dfcdc4SRobert Watson IFNET_RLOCK(); 2562603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 25639bf40edeSBrooks Davis int addrs; 25642624cf89SGarrett Wollman 2565fbd24c5eSColin Percival /* 2566fbd24c5eSColin Percival * Zero the ifr_name buffer to make sure we don't 2567fbd24c5eSColin Percival * disclose the contents of the stack. 2568fbd24c5eSColin Percival */ 2569fbd24c5eSColin Percival memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name)); 2570fbd24c5eSColin Percival 25719bf40edeSBrooks Davis if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)) 257262313e4cSSam Leffler >= sizeof(ifr.ifr_name)) { 257362313e4cSSam Leffler sbuf_delete(sb); 257462313e4cSSam Leffler IFNET_RUNLOCK(); 25754dcf2bbbSBrooks Davis return (ENAMETOOLONG); 257662313e4cSSam Leffler } 25772624cf89SGarrett Wollman 257875c13541SPoul-Henning Kamp addrs = 0; 2579ab5ed8a5SRobert Watson IF_ADDR_LOCK(ifp); 25802defe5cdSJonathan Lemon TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 25812defe5cdSJonathan Lemon struct sockaddr *sa = ifa->ifa_addr; 25822defe5cdSJonathan Lemon 2583b89e82ddSJamie Gritton if (prison_if(curthread->td_ucred, sa) != 0) 258475c13541SPoul-Henning Kamp continue; 258575c13541SPoul-Henning Kamp addrs++; 2586df8bae1dSRodney W. Grimes #ifdef COMPAT_43 2587df8bae1dSRodney W. Grimes if (cmd == OSIOCGIFCONF) { 2588df8bae1dSRodney W. Grimes struct osockaddr *osa = 2589df8bae1dSRodney W. Grimes (struct osockaddr *)&ifr.ifr_addr; 2590df8bae1dSRodney W. Grimes ifr.ifr_addr = *sa; 2591df8bae1dSRodney W. Grimes osa->sa_family = sa->sa_family; 25924dcf2bbbSBrooks Davis sbuf_bcat(sb, &ifr, sizeof(ifr)); 25934dcf2bbbSBrooks Davis max_len += sizeof(ifr); 2594df8bae1dSRodney W. Grimes } else 2595df8bae1dSRodney W. Grimes #endif 2596df8bae1dSRodney W. Grimes if (sa->sa_len <= sizeof(*sa)) { 2597df8bae1dSRodney W. Grimes ifr.ifr_addr = *sa; 25984dcf2bbbSBrooks Davis sbuf_bcat(sb, &ifr, sizeof(ifr)); 25994dcf2bbbSBrooks Davis max_len += sizeof(ifr); 2600df8bae1dSRodney W. Grimes } else { 26014dcf2bbbSBrooks Davis sbuf_bcat(sb, &ifr, 26024dcf2bbbSBrooks Davis offsetof(struct ifreq, ifr_addr)); 26034dcf2bbbSBrooks Davis max_len += offsetof(struct ifreq, ifr_addr); 26044dcf2bbbSBrooks Davis sbuf_bcat(sb, sa, sa->sa_len); 26054dcf2bbbSBrooks Davis max_len += sa->sa_len; 2606df8bae1dSRodney W. Grimes } 26074dcf2bbbSBrooks Davis 26084dcf2bbbSBrooks Davis if (!sbuf_overflowed(sb)) 26094dcf2bbbSBrooks Davis valid_len = sbuf_len(sb); 2610df8bae1dSRodney W. Grimes } 2611ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 26124dcf2bbbSBrooks Davis if (addrs == 0) { 261375c13541SPoul-Henning Kamp bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 26144dcf2bbbSBrooks Davis sbuf_bcat(sb, &ifr, sizeof(ifr)); 26154dcf2bbbSBrooks Davis max_len += sizeof(ifr); 26164dcf2bbbSBrooks Davis 26174dcf2bbbSBrooks Davis if (!sbuf_overflowed(sb)) 26184dcf2bbbSBrooks Davis valid_len = sbuf_len(sb); 261975c13541SPoul-Henning Kamp } 2620df8bae1dSRodney W. Grimes } 2621b30a244cSJeffrey Hsu IFNET_RUNLOCK(); 26224dcf2bbbSBrooks Davis 26234dcf2bbbSBrooks Davis /* 26244dcf2bbbSBrooks Davis * If we didn't allocate enough space (uncommon), try again. If 26254dcf2bbbSBrooks Davis * we have already allocated as much space as we are allowed, 26264dcf2bbbSBrooks Davis * return what we've got. 26274dcf2bbbSBrooks Davis */ 26284dcf2bbbSBrooks Davis if (valid_len != max_len && !full) { 26294dcf2bbbSBrooks Davis sbuf_delete(sb); 26304dcf2bbbSBrooks Davis goto again; 26314dcf2bbbSBrooks Davis } 26324dcf2bbbSBrooks Davis 26334dcf2bbbSBrooks Davis ifc->ifc_len = valid_len; 263419cf0498SPaul Saab #ifdef __amd64__ 263519cf0498SPaul Saab if (cmd == SIOCGIFCONF32) 263619cf0498SPaul Saab ifc32->ifc_len = valid_len; 263719cf0498SPaul Saab #endif 26385ed8cedcSBrian Feldman sbuf_finish(sb); 26394dcf2bbbSBrooks Davis error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len); 26404dcf2bbbSBrooks Davis sbuf_delete(sb); 2641df8bae1dSRodney W. Grimes return (error); 2642df8bae1dSRodney W. Grimes } 2643df8bae1dSRodney W. Grimes 26441158dfb7SGarrett Wollman /* 26458b25904eSGleb Smirnoff * Just like ifpromisc(), but for all-multicast-reception mode. 26461158dfb7SGarrett Wollman */ 26471158dfb7SGarrett Wollman int 264872fd1b6aSDag-Erling Smørgrav if_allmulti(struct ifnet *ifp, int onswitch) 26491158dfb7SGarrett Wollman { 26501158dfb7SGarrett Wollman 26511a3b6859SYaroslav Tykhiy return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch)); 26521158dfb7SGarrett Wollman } 26531158dfb7SGarrett Wollman 26545896d124SBruce M Simpson struct ifmultiaddr * 2655c3b31afdSRobert Watson if_findmulti(struct ifnet *ifp, struct sockaddr *sa) 26561158dfb7SGarrett Wollman { 26571158dfb7SGarrett Wollman struct ifmultiaddr *ifma; 26581158dfb7SGarrett Wollman 2659c3b31afdSRobert Watson IF_ADDR_LOCK_ASSERT(ifp); 2660c3b31afdSRobert Watson 26616817526dSPoul-Henning Kamp TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 266240d8a302SBruce M Simpson if (sa->sa_family == AF_LINK) { 266340d8a302SBruce M Simpson if (sa_dl_equal(ifma->ifma_addr, sa)) 266440d8a302SBruce M Simpson break; 266540d8a302SBruce M Simpson } else { 2666c3b31afdSRobert Watson if (sa_equal(ifma->ifma_addr, sa)) 2667c3b31afdSRobert Watson break; 26681158dfb7SGarrett Wollman } 266940d8a302SBruce M Simpson } 2670c3b31afdSRobert Watson 2671c3b31afdSRobert Watson return ifma; 267257af7922SJulian Elischer } 26731158dfb7SGarrett Wollman 26741158dfb7SGarrett Wollman /* 2675c3b31afdSRobert Watson * Allocate a new ifmultiaddr and initialize based on passed arguments. We 2676c3b31afdSRobert Watson * make copies of passed sockaddrs. The ifmultiaddr will not be added to 2677c3b31afdSRobert Watson * the ifnet multicast address list here, so the caller must do that and 2678c3b31afdSRobert Watson * other setup work (such as notifying the device driver). The reference 2679c3b31afdSRobert Watson * count is initialized to 1. 26801158dfb7SGarrett Wollman */ 2681c3b31afdSRobert Watson static struct ifmultiaddr * 2682c3b31afdSRobert Watson if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa, 2683c3b31afdSRobert Watson int mflags) 2684c3b31afdSRobert Watson { 2685c3b31afdSRobert Watson struct ifmultiaddr *ifma; 2686c3b31afdSRobert Watson struct sockaddr *dupsa; 2687c3b31afdSRobert Watson 26881ede983cSDag-Erling Smørgrav ifma = malloc(sizeof *ifma, M_IFMADDR, mflags | 2689c3b31afdSRobert Watson M_ZERO); 2690c3b31afdSRobert Watson if (ifma == NULL) 2691c3b31afdSRobert Watson return (NULL); 2692c3b31afdSRobert Watson 26931ede983cSDag-Erling Smørgrav dupsa = malloc(sa->sa_len, M_IFMADDR, mflags); 2694c3b31afdSRobert Watson if (dupsa == NULL) { 26951ede983cSDag-Erling Smørgrav free(ifma, M_IFMADDR); 2696c3b31afdSRobert Watson return (NULL); 26971158dfb7SGarrett Wollman } 26981158dfb7SGarrett Wollman bcopy(sa, dupsa, sa->sa_len); 26991158dfb7SGarrett Wollman ifma->ifma_addr = dupsa; 2700c3b31afdSRobert Watson 27011158dfb7SGarrett Wollman ifma->ifma_ifp = ifp; 27021158dfb7SGarrett Wollman ifma->ifma_refcount = 1; 2703d4d22970SGleb Smirnoff ifma->ifma_protospec = NULL; 2704c3b31afdSRobert Watson 2705c3b31afdSRobert Watson if (llsa == NULL) { 2706c3b31afdSRobert Watson ifma->ifma_lladdr = NULL; 2707c3b31afdSRobert Watson return (ifma); 2708c3b31afdSRobert Watson } 2709c3b31afdSRobert Watson 27101ede983cSDag-Erling Smørgrav dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags); 2711c3b31afdSRobert Watson if (dupsa == NULL) { 27121ede983cSDag-Erling Smørgrav free(ifma->ifma_addr, M_IFMADDR); 27131ede983cSDag-Erling Smørgrav free(ifma, M_IFMADDR); 2714c3b31afdSRobert Watson return (NULL); 2715c3b31afdSRobert Watson } 2716c3b31afdSRobert Watson bcopy(llsa, dupsa, llsa->sa_len); 2717c3b31afdSRobert Watson ifma->ifma_lladdr = dupsa; 2718c3b31afdSRobert Watson 2719c3b31afdSRobert Watson return (ifma); 2720c3b31afdSRobert Watson } 2721373f88edSGarrett Wollman 27221158dfb7SGarrett Wollman /* 2723c3b31afdSRobert Watson * if_freemulti: free ifmultiaddr structure and possibly attached related 2724c3b31afdSRobert Watson * addresses. The caller is responsible for implementing reference 2725c3b31afdSRobert Watson * counting, notifying the driver, handling routing messages, and releasing 2726c3b31afdSRobert Watson * any dependent link layer state. 27271158dfb7SGarrett Wollman */ 2728c3b31afdSRobert Watson static void 2729c3b31afdSRobert Watson if_freemulti(struct ifmultiaddr *ifma) 2730c3b31afdSRobert Watson { 2731c3b31afdSRobert Watson 2732ec002feeSBruce M Simpson KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d", 2733c3b31afdSRobert Watson ifma->ifma_refcount)); 2734c3b31afdSRobert Watson KASSERT(ifma->ifma_protospec == NULL, 2735c3b31afdSRobert Watson ("if_freemulti: protospec not NULL")); 2736c3b31afdSRobert Watson 2737c3b31afdSRobert Watson if (ifma->ifma_lladdr != NULL) 27381ede983cSDag-Erling Smørgrav free(ifma->ifma_lladdr, M_IFMADDR); 27391ede983cSDag-Erling Smørgrav free(ifma->ifma_addr, M_IFMADDR); 27401ede983cSDag-Erling Smørgrav free(ifma, M_IFMADDR); 2741c3b31afdSRobert Watson } 2742c3b31afdSRobert Watson 2743c3b31afdSRobert Watson /* 2744c3b31afdSRobert Watson * Register an additional multicast address with a network interface. 2745c3b31afdSRobert Watson * 2746c3b31afdSRobert Watson * - If the address is already present, bump the reference count on the 2747c3b31afdSRobert Watson * address and return. 2748c3b31afdSRobert Watson * - If the address is not link-layer, look up a link layer address. 2749c3b31afdSRobert Watson * - Allocate address structures for one or both addresses, and attach to the 2750c3b31afdSRobert Watson * multicast address list on the interface. If automatically adding a link 2751c3b31afdSRobert Watson * layer address, the protocol address will own a reference to the link 2752c3b31afdSRobert Watson * layer address, to be freed when it is freed. 2753c3b31afdSRobert Watson * - Notify the network device driver of an addition to the multicast address 2754c3b31afdSRobert Watson * list. 2755c3b31afdSRobert Watson * 2756c3b31afdSRobert Watson * 'sa' points to caller-owned memory with the desired multicast address. 2757c3b31afdSRobert Watson * 2758c3b31afdSRobert Watson * 'retifma' will be used to return a pointer to the resulting multicast 2759c3b31afdSRobert Watson * address reference, if desired. 2760c3b31afdSRobert Watson */ 2761c3b31afdSRobert Watson int 2762c3b31afdSRobert Watson if_addmulti(struct ifnet *ifp, struct sockaddr *sa, 2763c3b31afdSRobert Watson struct ifmultiaddr **retifma) 2764c3b31afdSRobert Watson { 2765c3b31afdSRobert Watson struct ifmultiaddr *ifma, *ll_ifma; 2766c3b31afdSRobert Watson struct sockaddr *llsa; 2767c3b31afdSRobert Watson int error; 2768c3b31afdSRobert Watson 2769c3b31afdSRobert Watson /* 2770c3b31afdSRobert Watson * If the address is already present, return a new reference to it; 2771c3b31afdSRobert Watson * otherwise, allocate storage and set up a new address. 2772c3b31afdSRobert Watson */ 2773c3b31afdSRobert Watson IF_ADDR_LOCK(ifp); 2774c3b31afdSRobert Watson ifma = if_findmulti(ifp, sa); 2775c3b31afdSRobert Watson if (ifma != NULL) { 2776c3b31afdSRobert Watson ifma->ifma_refcount++; 2777c3b31afdSRobert Watson if (retifma != NULL) 2778c3b31afdSRobert Watson *retifma = ifma; 2779c3b31afdSRobert Watson IF_ADDR_UNLOCK(ifp); 2780c3b31afdSRobert Watson return (0); 2781c3b31afdSRobert Watson } 2782c3b31afdSRobert Watson 2783c3b31afdSRobert Watson /* 2784c3b31afdSRobert Watson * The address isn't already present; resolve the protocol address 2785c3b31afdSRobert Watson * into a link layer address, and then look that up, bump its 2786c3b31afdSRobert Watson * refcount or allocate an ifma for that also. If 'llsa' was 2787c3b31afdSRobert Watson * returned, we will need to free it later. 2788c3b31afdSRobert Watson */ 2789c3b31afdSRobert Watson llsa = NULL; 2790c3b31afdSRobert Watson ll_ifma = NULL; 2791c3b31afdSRobert Watson if (ifp->if_resolvemulti != NULL) { 2792c3b31afdSRobert Watson error = ifp->if_resolvemulti(ifp, &llsa, sa); 2793c3b31afdSRobert Watson if (error) 2794c3b31afdSRobert Watson goto unlock_out; 2795c3b31afdSRobert Watson } 2796c3b31afdSRobert Watson 2797c3b31afdSRobert Watson /* 2798c3b31afdSRobert Watson * Allocate the new address. Don't hook it up yet, as we may also 2799c3b31afdSRobert Watson * need to allocate a link layer multicast address. 2800c3b31afdSRobert Watson */ 2801c3b31afdSRobert Watson ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT); 2802c3b31afdSRobert Watson if (ifma == NULL) { 2803c3b31afdSRobert Watson error = ENOMEM; 2804c3b31afdSRobert Watson goto free_llsa_out; 2805c3b31afdSRobert Watson } 2806c3b31afdSRobert Watson 2807c3b31afdSRobert Watson /* 2808c3b31afdSRobert Watson * If a link layer address is found, we'll need to see if it's 2809c3b31afdSRobert Watson * already present in the address list, or allocate is as well. 2810c3b31afdSRobert Watson * When this block finishes, the link layer address will be on the 2811c3b31afdSRobert Watson * list. 2812c3b31afdSRobert Watson */ 2813c3b31afdSRobert Watson if (llsa != NULL) { 2814c3b31afdSRobert Watson ll_ifma = if_findmulti(ifp, llsa); 2815c3b31afdSRobert Watson if (ll_ifma == NULL) { 2816c3b31afdSRobert Watson ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT); 2817c3b31afdSRobert Watson if (ll_ifma == NULL) { 2818ec002feeSBruce M Simpson --ifma->ifma_refcount; 2819c3b31afdSRobert Watson if_freemulti(ifma); 2820c3b31afdSRobert Watson error = ENOMEM; 2821c3b31afdSRobert Watson goto free_llsa_out; 2822c3b31afdSRobert Watson } 2823c3b31afdSRobert Watson TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma, 2824c3b31afdSRobert Watson ifma_link); 2825c3b31afdSRobert Watson } else 2826c3b31afdSRobert Watson ll_ifma->ifma_refcount++; 2827ec002feeSBruce M Simpson ifma->ifma_llifma = ll_ifma; 2828c3b31afdSRobert Watson } 2829c3b31afdSRobert Watson 2830c3b31afdSRobert Watson /* 2831c3b31afdSRobert Watson * We now have a new multicast address, ifma, and possibly a new or 2832c3b31afdSRobert Watson * referenced link layer address. Add the primary address to the 2833c3b31afdSRobert Watson * ifnet address list. 2834c3b31afdSRobert Watson */ 28356817526dSPoul-Henning Kamp TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 2836c3b31afdSRobert Watson 283713990766SJonathan Mini if (retifma != NULL) 2838373f88edSGarrett Wollman *retifma = ifma; 28391158dfb7SGarrett Wollman 2840c3b31afdSRobert Watson /* 2841c3b31afdSRobert Watson * Must generate the message while holding the lock so that 'ifma' 2842c3b31afdSRobert Watson * pointer is still valid. 2843c3b31afdSRobert Watson */ 2844c3b31afdSRobert Watson rt_newmaddrmsg(RTM_NEWMADDR, ifma); 2845c3b31afdSRobert Watson IF_ADDR_UNLOCK(ifp); 2846c3b31afdSRobert Watson 28471158dfb7SGarrett Wollman /* 28481158dfb7SGarrett Wollman * We are certain we have added something, so call down to the 28491158dfb7SGarrett Wollman * interface to let them know about it. 28501158dfb7SGarrett Wollman */ 28512432c31cSRobert Watson if (ifp->if_ioctl != NULL) { 28521a3b6859SYaroslav Tykhiy (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0); 28531a3b6859SYaroslav Tykhiy } 28541158dfb7SGarrett Wollman 2855c3b31afdSRobert Watson if (llsa != NULL) 28561ede983cSDag-Erling Smørgrav free(llsa, M_IFMADDR); 2857c3b31afdSRobert Watson 2858c3b31afdSRobert Watson return (0); 2859c3b31afdSRobert Watson 2860c3b31afdSRobert Watson free_llsa_out: 2861c3b31afdSRobert Watson if (llsa != NULL) 28621ede983cSDag-Erling Smørgrav free(llsa, M_IFMADDR); 2863c3b31afdSRobert Watson 2864c3b31afdSRobert Watson unlock_out: 2865c3b31afdSRobert Watson IF_ADDR_UNLOCK(ifp); 2866c3b31afdSRobert Watson return (error); 28671158dfb7SGarrett Wollman } 28681158dfb7SGarrett Wollman 28691158dfb7SGarrett Wollman /* 2870ec002feeSBruce M Simpson * Delete a multicast group membership by network-layer group address. 2871ec002feeSBruce M Simpson * 2872ec002feeSBruce M Simpson * Returns ENOENT if the entry could not be found. If ifp no longer 2873ec002feeSBruce M Simpson * exists, results are undefined. This entry point should only be used 2874ec002feeSBruce M Simpson * from subsystems which do appropriate locking to hold ifp for the 2875ec002feeSBruce M Simpson * duration of the call. 2876ec002feeSBruce M Simpson * Network-layer protocol domains must use if_delmulti_ifma(). 28771158dfb7SGarrett Wollman */ 28781158dfb7SGarrett Wollman int 287972fd1b6aSDag-Erling Smørgrav if_delmulti(struct ifnet *ifp, struct sockaddr *sa) 28801158dfb7SGarrett Wollman { 2881ec002feeSBruce M Simpson struct ifmultiaddr *ifma; 2882ec002feeSBruce M Simpson int lastref; 2883ec002feeSBruce M Simpson #ifdef INVARIANTS 2884ec002feeSBruce M Simpson struct ifnet *oifp; 2885ec002feeSBruce M Simpson 288677dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 2887603724d3SBjoern A. Zeeb TAILQ_FOREACH(oifp, &V_ifnet, if_link) 2888ec002feeSBruce M Simpson if (ifp == oifp) 2889ec002feeSBruce M Simpson break; 2890ec002feeSBruce M Simpson if (ifp != oifp) 2891ec002feeSBruce M Simpson ifp = NULL; 289277dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 2893ec002feeSBruce M Simpson 2894ec002feeSBruce M Simpson KASSERT(ifp != NULL, ("%s: ifnet went away", __func__)); 2895ec002feeSBruce M Simpson #endif 2896ec002feeSBruce M Simpson if (ifp == NULL) 2897ec002feeSBruce M Simpson return (ENOENT); 28981158dfb7SGarrett Wollman 2899c3b31afdSRobert Watson IF_ADDR_LOCK(ifp); 2900ec002feeSBruce M Simpson lastref = 0; 2901c3b31afdSRobert Watson ifma = if_findmulti(ifp, sa); 2902ec002feeSBruce M Simpson if (ifma != NULL) 2903ec002feeSBruce M Simpson lastref = if_delmulti_locked(ifp, ifma, 0); 2904c3b31afdSRobert Watson IF_ADDR_UNLOCK(ifp); 2905c3b31afdSRobert Watson 2906ec002feeSBruce M Simpson if (ifma == NULL) 2907ec002feeSBruce M Simpson return (ENOENT); 2908ec002feeSBruce M Simpson 2909ec002feeSBruce M Simpson if (lastref && ifp->if_ioctl != NULL) { 29101a3b6859SYaroslav Tykhiy (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 291131302ebfSRobert Watson } 29121158dfb7SGarrett Wollman 2913ec002feeSBruce M Simpson return (0); 2914ec002feeSBruce M Simpson } 2915ec002feeSBruce M Simpson 2916ec002feeSBruce M Simpson /* 2917ec002feeSBruce M Simpson * Delete a multicast group membership by group membership pointer. 2918ec002feeSBruce M Simpson * Network-layer protocol domains must use this routine. 2919ec002feeSBruce M Simpson * 2920e5adda3dSRobert Watson * It is safe to call this routine if the ifp disappeared. 2921ec002feeSBruce M Simpson */ 2922ec002feeSBruce M Simpson void 2923ec002feeSBruce M Simpson if_delmulti_ifma(struct ifmultiaddr *ifma) 2924ec002feeSBruce M Simpson { 2925ec002feeSBruce M Simpson struct ifnet *ifp; 2926ec002feeSBruce M Simpson int lastref; 2927ec002feeSBruce M Simpson 2928ec002feeSBruce M Simpson ifp = ifma->ifma_ifp; 2929ec002feeSBruce M Simpson #ifdef DIAGNOSTIC 2930ec002feeSBruce M Simpson if (ifp == NULL) { 2931ec002feeSBruce M Simpson printf("%s: ifma_ifp seems to be detached\n", __func__); 2932ec002feeSBruce M Simpson } else { 2933ec002feeSBruce M Simpson struct ifnet *oifp; 2934ec002feeSBruce M Simpson 293577dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 2936603724d3SBjoern A. Zeeb TAILQ_FOREACH(oifp, &V_ifnet, if_link) 2937ec002feeSBruce M Simpson if (ifp == oifp) 2938ec002feeSBruce M Simpson break; 2939ec002feeSBruce M Simpson if (ifp != oifp) { 2940ec002feeSBruce M Simpson printf("%s: ifnet %p disappeared\n", __func__, ifp); 2941ec002feeSBruce M Simpson ifp = NULL; 2942ec002feeSBruce M Simpson } 294377dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 2944ec002feeSBruce M Simpson } 2945ec002feeSBruce M Simpson #endif 2946ec002feeSBruce M Simpson /* 2947ec002feeSBruce M Simpson * If and only if the ifnet instance exists: Acquire the address lock. 2948ec002feeSBruce M Simpson */ 2949ec002feeSBruce M Simpson if (ifp != NULL) 2950ec002feeSBruce M Simpson IF_ADDR_LOCK(ifp); 2951ec002feeSBruce M Simpson 2952ec002feeSBruce M Simpson lastref = if_delmulti_locked(ifp, ifma, 0); 2953ec002feeSBruce M Simpson 2954ec002feeSBruce M Simpson if (ifp != NULL) { 2955ec002feeSBruce M Simpson /* 2956ec002feeSBruce M Simpson * If and only if the ifnet instance exists: 2957ec002feeSBruce M Simpson * Release the address lock. 2958ec002feeSBruce M Simpson * If the group was left: update the hardware hash filter. 2959ec002feeSBruce M Simpson */ 2960ec002feeSBruce M Simpson IF_ADDR_UNLOCK(ifp); 2961ec002feeSBruce M Simpson if (lastref && ifp->if_ioctl != NULL) { 2962ec002feeSBruce M Simpson (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 2963ec002feeSBruce M Simpson } 2964ec002feeSBruce M Simpson } 2965ec002feeSBruce M Simpson } 2966ec002feeSBruce M Simpson 2967ec002feeSBruce M Simpson /* 2968ec002feeSBruce M Simpson * Perform deletion of network-layer and/or link-layer multicast address. 2969ec002feeSBruce M Simpson * 2970ec002feeSBruce M Simpson * Return 0 if the reference count was decremented. 2971ec002feeSBruce M Simpson * Return 1 if the final reference was released, indicating that the 2972ec002feeSBruce M Simpson * hardware hash filter should be reprogrammed. 2973ec002feeSBruce M Simpson */ 2974ec002feeSBruce M Simpson static int 2975ec002feeSBruce M Simpson if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching) 2976ec002feeSBruce M Simpson { 2977ec002feeSBruce M Simpson struct ifmultiaddr *ll_ifma; 2978ec002feeSBruce M Simpson 2979ec002feeSBruce M Simpson if (ifp != NULL && ifma->ifma_ifp != NULL) { 2980ec002feeSBruce M Simpson KASSERT(ifma->ifma_ifp == ifp, 2981ec002feeSBruce M Simpson ("%s: inconsistent ifp %p", __func__, ifp)); 2982ec002feeSBruce M Simpson IF_ADDR_LOCK_ASSERT(ifp); 2983ec002feeSBruce M Simpson } 2984ec002feeSBruce M Simpson 2985ec002feeSBruce M Simpson ifp = ifma->ifma_ifp; 2986ec002feeSBruce M Simpson 2987ec002feeSBruce M Simpson /* 2988ec002feeSBruce M Simpson * If the ifnet is detaching, null out references to ifnet, 2989ec002feeSBruce M Simpson * so that upper protocol layers will notice, and not attempt 299075ae0c01SBruce M Simpson * to obtain locks for an ifnet which no longer exists. The 299175ae0c01SBruce M Simpson * routing socket announcement must happen before the ifnet 299275ae0c01SBruce M Simpson * instance is detached from the system. 2993ec002feeSBruce M Simpson */ 2994ec002feeSBruce M Simpson if (detaching) { 2995ec002feeSBruce M Simpson #ifdef DIAGNOSTIC 2996ec002feeSBruce M Simpson printf("%s: detaching ifnet instance %p\n", __func__, ifp); 2997ec002feeSBruce M Simpson #endif 299875ae0c01SBruce M Simpson /* 299975ae0c01SBruce M Simpson * ifp may already be nulled out if we are being reentered 300075ae0c01SBruce M Simpson * to delete the ll_ifma. 300175ae0c01SBruce M Simpson */ 300275ae0c01SBruce M Simpson if (ifp != NULL) { 300375ae0c01SBruce M Simpson rt_newmaddrmsg(RTM_DELMADDR, ifma); 3004ec002feeSBruce M Simpson ifma->ifma_ifp = NULL; 3005ec002feeSBruce M Simpson } 300675ae0c01SBruce M Simpson } 3007ec002feeSBruce M Simpson 3008ec002feeSBruce M Simpson if (--ifma->ifma_refcount > 0) 30091158dfb7SGarrett Wollman return 0; 3010ec002feeSBruce M Simpson 3011ec002feeSBruce M Simpson /* 3012ec002feeSBruce M Simpson * If this ifma is a network-layer ifma, a link-layer ifma may 3013ec002feeSBruce M Simpson * have been associated with it. Release it first if so. 3014ec002feeSBruce M Simpson */ 3015ec002feeSBruce M Simpson ll_ifma = ifma->ifma_llifma; 3016ec002feeSBruce M Simpson if (ll_ifma != NULL) { 3017ec002feeSBruce M Simpson KASSERT(ifma->ifma_lladdr != NULL, 3018ec002feeSBruce M Simpson ("%s: llifma w/o lladdr", __func__)); 3019ec002feeSBruce M Simpson if (detaching) 3020ec002feeSBruce M Simpson ll_ifma->ifma_ifp = NULL; /* XXX */ 3021ec002feeSBruce M Simpson if (--ll_ifma->ifma_refcount == 0) { 3022ec002feeSBruce M Simpson if (ifp != NULL) { 3023ec002feeSBruce M Simpson TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, 3024ec002feeSBruce M Simpson ifma_link); 3025ec002feeSBruce M Simpson } 3026ec002feeSBruce M Simpson if_freemulti(ll_ifma); 3027ec002feeSBruce M Simpson } 3028ec002feeSBruce M Simpson } 3029ec002feeSBruce M Simpson 3030ec002feeSBruce M Simpson if (ifp != NULL) 3031ec002feeSBruce M Simpson TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 3032ec002feeSBruce M Simpson 3033ec002feeSBruce M Simpson if_freemulti(ifma); 3034ec002feeSBruce M Simpson 3035ec002feeSBruce M Simpson /* 3036ec002feeSBruce M Simpson * The last reference to this instance of struct ifmultiaddr 3037ec002feeSBruce M Simpson * was released; the hardware should be notified of this change. 3038ec002feeSBruce M Simpson */ 3039ec002feeSBruce M Simpson return 1; 30401158dfb7SGarrett Wollman } 30411158dfb7SGarrett Wollman 304266ce51ceSArchie Cobbs /* 304366ce51ceSArchie Cobbs * Set the link layer address on an interface. 304466ce51ceSArchie Cobbs * 304566ce51ceSArchie Cobbs * At this time we only support certain types of interfaces, 304666ce51ceSArchie Cobbs * and we don't allow the length of the address to change. 304766ce51ceSArchie Cobbs */ 304866ce51ceSArchie Cobbs int 304966ce51ceSArchie Cobbs if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 305066ce51ceSArchie Cobbs { 305166ce51ceSArchie Cobbs struct sockaddr_dl *sdl; 305266ce51ceSArchie Cobbs struct ifaddr *ifa; 3053d637e989SPeter Wemm struct ifreq ifr; 305466ce51ceSArchie Cobbs 30553baaf297SRobert Watson IF_ADDR_LOCK(ifp); 30564a0d6638SRuslan Ermilov ifa = ifp->if_addr; 30573baaf297SRobert Watson if (ifa == NULL) { 30583baaf297SRobert Watson IF_ADDR_UNLOCK(ifp); 305966ce51ceSArchie Cobbs return (EINVAL); 30603baaf297SRobert Watson } 30613baaf297SRobert Watson ifa_ref(ifa); 30623baaf297SRobert Watson IF_ADDR_UNLOCK(ifp); 306366ce51ceSArchie Cobbs sdl = (struct sockaddr_dl *)ifa->ifa_addr; 30643baaf297SRobert Watson if (sdl == NULL) { 30653baaf297SRobert Watson ifa_free(ifa); 306666ce51ceSArchie Cobbs return (EINVAL); 30673baaf297SRobert Watson } 30683baaf297SRobert Watson if (len != sdl->sdl_alen) { /* don't allow length to change */ 30693baaf297SRobert Watson ifa_free(ifa); 307066ce51ceSArchie Cobbs return (EINVAL); 30713baaf297SRobert Watson } 307266ce51ceSArchie Cobbs switch (ifp->if_type) { 3073d09ed26fSRuslan Ermilov case IFT_ETHER: 307466ce51ceSArchie Cobbs case IFT_FDDI: 307566ce51ceSArchie Cobbs case IFT_XETHER: 307666ce51ceSArchie Cobbs case IFT_ISO88025: 3077b7bffa71SYaroslav Tykhiy case IFT_L2VLAN: 30788f867517SAndrew Thompson case IFT_BRIDGE: 30796cdcc159SMax Khon case IFT_ARCNET: 3080b47888ceSAndrew Thompson case IFT_IEEE8023ADLAG: 3081a51f44a7SSam Leffler case IFT_IEEE80211: 308266ce51ceSArchie Cobbs bcopy(lladdr, LLADDR(sdl), len); 30833baaf297SRobert Watson ifa_free(ifa); 308466ce51ceSArchie Cobbs break; 308566ce51ceSArchie Cobbs default: 30863baaf297SRobert Watson ifa_free(ifa); 308766ce51ceSArchie Cobbs return (ENODEV); 308866ce51ceSArchie Cobbs } 30893baaf297SRobert Watson 309066ce51ceSArchie Cobbs /* 309166ce51ceSArchie Cobbs * If the interface is already up, we need 309266ce51ceSArchie Cobbs * to re-init it in order to reprogram its 309366ce51ceSArchie Cobbs * address filter. 309466ce51ceSArchie Cobbs */ 309566ce51ceSArchie Cobbs if ((ifp->if_flags & IFF_UP) != 0) { 30961a3b6859SYaroslav Tykhiy if (ifp->if_ioctl) { 309766ce51ceSArchie Cobbs ifp->if_flags &= ~IFF_UP; 309862f76486SMaxim Sobolev ifr.ifr_flags = ifp->if_flags & 0xffff; 309962f76486SMaxim Sobolev ifr.ifr_flagshigh = ifp->if_flags >> 16; 3100ee0a4f7eSSUZUKI Shinsuke (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 310166ce51ceSArchie Cobbs ifp->if_flags |= IFF_UP; 310262f76486SMaxim Sobolev ifr.ifr_flags = ifp->if_flags & 0xffff; 310362f76486SMaxim Sobolev ifr.ifr_flagshigh = ifp->if_flags >> 16; 3104ee0a4f7eSSUZUKI Shinsuke (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 31051a3b6859SYaroslav Tykhiy } 3106b2c08f43SLuigi Rizzo #ifdef INET 3107b2c08f43SLuigi Rizzo /* 3108b2c08f43SLuigi Rizzo * Also send gratuitous ARPs to notify other nodes about 3109b2c08f43SLuigi Rizzo * the address change. 3110b2c08f43SLuigi Rizzo */ 3111b2c08f43SLuigi Rizzo TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 31124b97d7afSYaroslav Tykhiy if (ifa->ifa_addr->sa_family == AF_INET) 3113c0933269SPeter Wemm arp_ifinit(ifp, ifa); 3114b2c08f43SLuigi Rizzo } 3115b2c08f43SLuigi Rizzo #endif 311666ce51ceSArchie Cobbs } 311766ce51ceSArchie Cobbs return (0); 311866ce51ceSArchie Cobbs } 311966ce51ceSArchie Cobbs 31209bf40edeSBrooks Davis /* 31219bf40edeSBrooks Davis * The name argument must be a pointer to storage which will last as 31229bf40edeSBrooks Davis * long as the interface does. For physical devices, the result of 31239bf40edeSBrooks Davis * device_get_name(dev) is a good choice and for pseudo-devices a 31249bf40edeSBrooks Davis * static string works well. 31259bf40edeSBrooks Davis */ 31269bf40edeSBrooks Davis void 31279bf40edeSBrooks Davis if_initname(struct ifnet *ifp, const char *name, int unit) 31289bf40edeSBrooks Davis { 31299bf40edeSBrooks Davis ifp->if_dname = name; 31309bf40edeSBrooks Davis ifp->if_dunit = unit; 31319bf40edeSBrooks Davis if (unit != IF_DUNIT_NONE) 31329bf40edeSBrooks Davis snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit); 31339bf40edeSBrooks Davis else 31349bf40edeSBrooks Davis strlcpy(ifp->if_xname, name, IFNAMSIZ); 31359bf40edeSBrooks Davis } 31369bf40edeSBrooks Davis 3137fa882e87SBrooks Davis int 3138fa882e87SBrooks Davis if_printf(struct ifnet *ifp, const char * fmt, ...) 3139fa882e87SBrooks Davis { 3140fa882e87SBrooks Davis va_list ap; 3141fa882e87SBrooks Davis int retval; 3142fa882e87SBrooks Davis 31439bf40edeSBrooks Davis retval = printf("%s: ", ifp->if_xname); 3144fa882e87SBrooks Davis va_start(ap, fmt); 3145fa882e87SBrooks Davis retval += vprintf(fmt, ap); 3146fa882e87SBrooks Davis va_end(ap); 3147fa882e87SBrooks Davis return (retval); 3148fa882e87SBrooks Davis } 3149fa882e87SBrooks Davis 3150af5e59bfSRobert Watson void 3151af5e59bfSRobert Watson if_start(struct ifnet *ifp) 3152af5e59bfSRobert Watson { 3153af5e59bfSRobert Watson 3154af5e59bfSRobert Watson (*(ifp)->if_start)(ifp); 3155af5e59bfSRobert Watson } 3156af5e59bfSRobert Watson 3157db7f0b97SKip Macy /* 3158db7f0b97SKip Macy * Backwards compatibility interface for drivers 3159db7f0b97SKip Macy * that have not implemented it 3160db7f0b97SKip Macy */ 3161db7f0b97SKip Macy static int 3162db7f0b97SKip Macy if_transmit(struct ifnet *ifp, struct mbuf *m) 3163db7f0b97SKip Macy { 3164db7f0b97SKip Macy int error; 3165db7f0b97SKip Macy 3166db7f0b97SKip Macy IFQ_HANDOFF(ifp, m, error); 3167db7f0b97SKip Macy return (error); 3168db7f0b97SKip Macy } 3169db7f0b97SKip Macy 31700b762445SRobert Watson int 31710b762445SRobert Watson if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust) 31720b762445SRobert Watson { 31730b762445SRobert Watson int active = 0; 31740b762445SRobert Watson 31750b762445SRobert Watson IF_LOCK(ifq); 31760b762445SRobert Watson if (_IF_QFULL(ifq)) { 31770b762445SRobert Watson _IF_DROP(ifq); 31780b762445SRobert Watson IF_UNLOCK(ifq); 31790b762445SRobert Watson m_freem(m); 31800b762445SRobert Watson return (0); 31810b762445SRobert Watson } 31820b762445SRobert Watson if (ifp != NULL) { 31830b762445SRobert Watson ifp->if_obytes += m->m_pkthdr.len + adjust; 31840b762445SRobert Watson if (m->m_flags & (M_BCAST|M_MCAST)) 31850b762445SRobert Watson ifp->if_omcasts++; 3186292ee7beSRobert Watson active = ifp->if_drv_flags & IFF_DRV_OACTIVE; 31870b762445SRobert Watson } 31880b762445SRobert Watson _IF_ENQUEUE(ifq, m); 31890b762445SRobert Watson IF_UNLOCK(ifq); 31900b762445SRobert Watson if (ifp != NULL && !active) 3191e5adda3dSRobert Watson (*(ifp)->if_start)(ifp); 31920b762445SRobert Watson return (1); 31930b762445SRobert Watson } 3194fc74a9f9SBrooks Davis 3195fc74a9f9SBrooks Davis void 3196fc74a9f9SBrooks Davis if_register_com_alloc(u_char type, 3197fc74a9f9SBrooks Davis if_com_alloc_t *a, if_com_free_t *f) 3198fc74a9f9SBrooks Davis { 3199fc74a9f9SBrooks Davis 3200fc74a9f9SBrooks Davis KASSERT(if_com_alloc[type] == NULL, 3201fc74a9f9SBrooks Davis ("if_register_com_alloc: %d already registered", type)); 3202fc74a9f9SBrooks Davis KASSERT(if_com_free[type] == NULL, 3203fc74a9f9SBrooks Davis ("if_register_com_alloc: %d free already registered", type)); 3204fc74a9f9SBrooks Davis 3205fc74a9f9SBrooks Davis if_com_alloc[type] = a; 3206fc74a9f9SBrooks Davis if_com_free[type] = f; 3207fc74a9f9SBrooks Davis } 3208fc74a9f9SBrooks Davis 3209fc74a9f9SBrooks Davis void 3210fc74a9f9SBrooks Davis if_deregister_com_alloc(u_char type) 3211fc74a9f9SBrooks Davis { 3212fc74a9f9SBrooks Davis 3213affcaf78SMax Khon KASSERT(if_com_alloc[type] != NULL, 3214fc74a9f9SBrooks Davis ("if_deregister_com_alloc: %d not registered", type)); 3215affcaf78SMax Khon KASSERT(if_com_free[type] != NULL, 3216fc74a9f9SBrooks Davis ("if_deregister_com_alloc: %d free not registered", type)); 3217fc74a9f9SBrooks Davis if_com_alloc[type] = NULL; 3218fc74a9f9SBrooks Davis if_com_free[type] = NULL; 3219fc74a9f9SBrooks Davis } 3220