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" 365591b823SEivind Eklund 37df8bae1dSRodney W. Grimes #include <sys/param.h> 384dcf2bbbSBrooks Davis #include <sys/types.h> 39f13ad206SJonathan Lemon #include <sys/conf.h> 404d1d4912SBruce Evans #include <sys/malloc.h> 414dcf2bbbSBrooks Davis #include <sys/sbuf.h> 42d2b4566aSJonathan Lemon #include <sys/bus.h> 43df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 44df8bae1dSRodney W. Grimes #include <sys/systm.h> 45acd3428bSRobert Watson #include <sys/priv.h> 46df8bae1dSRodney W. Grimes #include <sys/proc.h> 47df8bae1dSRodney W. Grimes #include <sys/socket.h> 48df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 49df8bae1dSRodney W. Grimes #include <sys/protosw.h> 50df8bae1dSRodney W. Grimes #include <sys/kernel.h> 51653735c4SBjoern A. Zeeb #include <sys/lock.h> 5227d37320SRobert Watson #include <sys/refcount.h> 5321ca7b57SMarko Zec #include <sys/module.h> 54653735c4SBjoern A. Zeeb #include <sys/rwlock.h> 5551a53488SBruce Evans #include <sys/sockio.h> 56963e4c2aSGarrett Wollman #include <sys/syslog.h> 57602d513cSGarrett Wollman #include <sys/sysctl.h> 58af5e59bfSRobert Watson #include <sys/taskqueue.h> 5931b1bfe1SHajimu UMEMOTO #include <sys/domain.h> 6091421ba2SRobert Watson #include <sys/jail.h> 61fa882e87SBrooks Davis #include <machine/stdarg.h> 626e6b3f7cSQing Li #include <vm/uma.h> 63df8bae1dSRodney W. Grimes 64df8bae1dSRodney W. Grimes #include <net/if.h> 655a97c9d4SBjoern A. Zeeb #include <net/if_arp.h> 66f889d2efSBrooks Davis #include <net/if_clone.h> 67df8bae1dSRodney W. Grimes #include <net/if_dl.h> 6866ce51ceSArchie Cobbs #include <net/if_types.h> 6930aad87dSBrooks Davis #include <net/if_var.h> 709448326fSPoul-Henning Kamp #include <net/radix.h> 715500d3beSWarner Losh #include <net/route.h> 724b79449eSBjoern A. Zeeb #include <net/vnet.h> 73df8bae1dSRodney W. Grimes 740d0f9d1eSYoshinobu Inoue #if defined(INET) || defined(INET6) 7582cd038dSYoshinobu Inoue /*XXX*/ 7682cd038dSYoshinobu Inoue #include <netinet/in.h> 770d0f9d1eSYoshinobu Inoue #include <netinet/in_var.h> 789963e8a5SWill Andrews #include <netinet/ip_carp.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 8782cd038dSYoshinobu Inoue 88aed55708SRobert Watson #include <security/mac/mac_framework.h> 89aed55708SRobert Watson 909af74f3dSSergey Kandaurov #ifdef COMPAT_FREEBSD32 919af74f3dSSergey Kandaurov #include <sys/mount.h> 929af74f3dSSergey Kandaurov #include <compat/freebsd32/freebsd32.h> 939af74f3dSSergey Kandaurov #endif 949af74f3dSSergey Kandaurov 956cb7f168SBrooks Davis struct ifindex_entry { 966cb7f168SBrooks Davis struct ifnet *ife_ifnet; 976cb7f168SBrooks Davis }; 986cb7f168SBrooks Davis 995515c2e7SGleb Smirnoff SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers"); 1005515c2e7SGleb Smirnoff SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management"); 1015515c2e7SGleb Smirnoff 102e50d35e6SMaxim Sobolev TUNABLE_INT("net.link.ifqmaxlen", &ifqmaxlen); 103f88910cdSMatthew D Fleming SYSCTL_INT(_net_link, OID_AUTO, ifqmaxlen, CTLFLAG_RDTUN, 104e50d35e6SMaxim Sobolev &ifqmaxlen, 0, "max send queue size"); 105e50d35e6SMaxim Sobolev 1065515c2e7SGleb Smirnoff /* Log link state change events */ 1075515c2e7SGleb Smirnoff static int log_link_state_change = 1; 1085515c2e7SGleb Smirnoff 1095515c2e7SGleb Smirnoff SYSCTL_INT(_net_link, OID_AUTO, log_link_state_change, CTLFLAG_RW, 1105515c2e7SGleb Smirnoff &log_link_state_change, 0, 1115515c2e7SGleb Smirnoff "log interface link state change events"); 1125515c2e7SGleb Smirnoff 113215940b3SXin LI /* Interface description */ 114215940b3SXin LI static unsigned int ifdescr_maxlen = 1024; 115215940b3SXin LI SYSCTL_UINT(_net, OID_AUTO, ifdescr_maxlen, CTLFLAG_RW, 116215940b3SXin LI &ifdescr_maxlen, 0, 117215940b3SXin LI "administrative maximum length for interface description"); 118215940b3SXin LI 119215940b3SXin LI MALLOC_DEFINE(M_IFDESCR, "ifdescr", "ifnet descriptions"); 120215940b3SXin LI 121215940b3SXin LI /* global sx for non-critical path ifdescr */ 122215940b3SXin LI static struct sx ifdescr_sx; 123215940b3SXin LI SX_SYSINIT(ifdescr_sx, &ifdescr_sx, "ifnet descr"); 124215940b3SXin LI 1258f867517SAndrew Thompson void (*bstp_linkstate_p)(struct ifnet *ifp, int state); 1261c7899c7SGleb Smirnoff void (*ng_ether_link_state_p)(struct ifnet *ifp, int state); 12718242d3bSAndrew Thompson void (*lagg_linkstate_p)(struct ifnet *ifp, int state); 1289963e8a5SWill Andrews /* These are external hooks for CARP. */ 12954bfbd51SWill Andrews void (*carp_linkstate_p)(struct ifnet *ifp); 1309963e8a5SWill Andrews #if defined(INET) || defined(INET6) 1319963e8a5SWill Andrews struct ifnet *(*carp_forus_p)(struct ifnet *ifp, u_char *dhost); 1329963e8a5SWill Andrews int (*carp_output_p)(struct ifnet *ifp, struct mbuf *m, 1339963e8a5SWill Andrews struct sockaddr *sa, struct rtentry *rt); 1349963e8a5SWill Andrews #endif 1359963e8a5SWill Andrews #ifdef INET 1369963e8a5SWill Andrews int (*carp_iamatch_p)(struct ifnet *, struct in_ifaddr *, struct in_addr *, 1379963e8a5SWill Andrews u_int8_t **); 1389963e8a5SWill Andrews #endif 1399963e8a5SWill Andrews #ifdef INET6 1409963e8a5SWill Andrews struct ifaddr *(*carp_iamatch6_p)(struct ifnet *ifp, struct in6_addr *taddr6); 1419963e8a5SWill Andrews caddr_t (*carp_macmatch6_p)(struct ifnet *ifp, struct mbuf *m, 1429963e8a5SWill Andrews const struct in6_addr *taddr); 1439963e8a5SWill Andrews #endif 1441c7899c7SGleb Smirnoff 1454cb655c0SMax Laier struct mbuf *(*tbr_dequeue_ptr)(struct ifaltq *, int) = NULL; 1464cb655c0SMax Laier 147ec002feeSBruce M Simpson /* 148ec002feeSBruce M Simpson * XXX: Style; these should be sorted alphabetically, and unprototyped 149ec002feeSBruce M Simpson * static functions should be prototyped. Currently they are sorted by 150ec002feeSBruce M Simpson * declaration order. 151ec002feeSBruce M Simpson */ 15231b1bfe1SHajimu UMEMOTO static void if_attachdomain(void *); 15331b1bfe1SHajimu UMEMOTO static void if_attachdomain1(struct ifnet *); 1540b59d917SJonathan Lemon static int ifconf(u_long, caddr_t); 155ec002feeSBruce M Simpson static void if_freemulti(struct ifmultiaddr *); 156f9132cebSJonathan Lemon static void if_init(void *); 1578e937462SRobert Watson static void if_grow(void); 1588614fb12SMax Laier static void if_route(struct ifnet *, int flag, int fam); 1591a3b6859SYaroslav Tykhiy static int if_setflag(struct ifnet *, int, int, int *, int); 160db7f0b97SKip Macy static int if_transmit(struct ifnet *ifp, struct mbuf *m); 1618614fb12SMax Laier static void if_unroute(struct ifnet *, int flag, int fam); 1628071913dSRuslan Ermilov static void link_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 1630b59d917SJonathan Lemon static int if_rtdel(struct radix_node *, void *); 164f13ad206SJonathan Lemon static int ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *); 165ec002feeSBruce M Simpson static int if_delmulti_locked(struct ifnet *, struct ifmultiaddr *, int); 16668a3482fSGleb Smirnoff static void do_link_state_change(void *, int); 1670dad3f0eSMax Laier static int if_getgroup(struct ifgroupreq *, struct ifnet *); 1680dad3f0eSMax Laier static int if_getgroupmembers(struct ifgroupreq *); 1698623f9fdSMax Laier static void if_delgroups(struct ifnet *); 170e0c14af9SMarko Zec static void if_attach_internal(struct ifnet *, int); 171e0c14af9SMarko Zec static void if_detach_internal(struct ifnet *, int); 172db7f0b97SKip Macy 17382cd038dSYoshinobu Inoue #ifdef INET6 17482cd038dSYoshinobu Inoue /* 17582cd038dSYoshinobu Inoue * XXX: declare here to avoid to include many inet6 related files.. 17682cd038dSYoshinobu Inoue * should be more generalized? 17782cd038dSYoshinobu Inoue */ 178929ddbbbSAlfred Perlstein extern void nd6_setmtu(struct ifnet *); 17982cd038dSYoshinobu Inoue #endif 18082cd038dSYoshinobu Inoue 18182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, if_index); 18282cea7e6SBjoern A. Zeeb int ifqmaxlen = IFQ_MAXLEN; 183eddfbb76SRobert Watson VNET_DEFINE(struct ifnethead, ifnet); /* depend on static init XXX */ 184eddfbb76SRobert Watson VNET_DEFINE(struct ifgrouphead, ifg_head); 18582cea7e6SBjoern A. Zeeb 1863e288e62SDimitry Andric static VNET_DEFINE(int, if_indexlim) = 8; 187eddfbb76SRobert Watson 18877dfcdc4SRobert Watson /* Table of ifnet by index. */ 189a38de013SBjoern A. Zeeb VNET_DEFINE(struct ifindex_entry *, ifindex_table); 190eddfbb76SRobert Watson 1911e77c105SRobert Watson #define V_if_indexlim VNET(if_indexlim) 1921e77c105SRobert Watson #define V_ifindex_table VNET(ifindex_table) 19344e33a07SMarko Zec 19477dfcdc4SRobert Watson /* 19577dfcdc4SRobert Watson * The global network interface list (V_ifnet) and related state (such as 19677dfcdc4SRobert Watson * if_index, if_indexlim, and ifindex_table) are protected by an sxlock and 19777dfcdc4SRobert Watson * an rwlock. Either may be acquired shared to stablize the list, but both 19877dfcdc4SRobert Watson * must be acquired writable to modify the list. This model allows us to 19977dfcdc4SRobert Watson * both stablize the interface list during interrupt thread processing, but 20077dfcdc4SRobert Watson * also to stablize it over long-running ioctls, without introducing priority 20177dfcdc4SRobert Watson * inversions and deadlocks. 20277dfcdc4SRobert Watson */ 20377dfcdc4SRobert Watson struct rwlock ifnet_rwlock; 20477dfcdc4SRobert Watson struct sx ifnet_sxlock; 20577dfcdc4SRobert Watson 206ed2dabfcSRobert Watson /* 207ed2dabfcSRobert Watson * The allocation of network interfaces is a rather non-atomic affair; we 208ed2dabfcSRobert Watson * need to select an index before we are ready to expose the interface for 209ed2dabfcSRobert Watson * use, so will use this pointer value to indicate reservation. 210ed2dabfcSRobert Watson */ 211ed2dabfcSRobert Watson #define IFNET_HOLD (void *)(uintptr_t)(-1) 212ed2dabfcSRobert Watson 213fc74a9f9SBrooks Davis static if_com_alloc_t *if_com_alloc[256]; 214fc74a9f9SBrooks Davis static if_com_free_t *if_com_free[256]; 2150b59d917SJonathan Lemon 216fc74a9f9SBrooks Davis MALLOC_DEFINE(M_IFNET, "ifnet", "interface internals"); 2170b59d917SJonathan Lemon MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address"); 2180b59d917SJonathan Lemon MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address"); 21930aad87dSBrooks Davis 22021ca7b57SMarko Zec struct ifnet * 221d24c444cSKip Macy ifnet_byindex_locked(u_short idx) 22202f4879dSRobert Watson { 22302f4879dSRobert Watson 22427d37320SRobert Watson if (idx > V_if_index) 22527d37320SRobert Watson return (NULL); 226ed2dabfcSRobert Watson if (V_ifindex_table[idx].ife_ifnet == IFNET_HOLD) 227ed2dabfcSRobert Watson return (NULL); 22827d37320SRobert Watson return (V_ifindex_table[idx].ife_ifnet); 229d24c444cSKip Macy } 230d24c444cSKip Macy 231d24c444cSKip Macy struct ifnet * 232d24c444cSKip Macy ifnet_byindex(u_short idx) 233d24c444cSKip Macy { 234d24c444cSKip Macy struct ifnet *ifp; 235d24c444cSKip Macy 23677dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 237d24c444cSKip Macy ifp = ifnet_byindex_locked(idx); 23877dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 23902f4879dSRobert Watson return (ifp); 24002f4879dSRobert Watson } 24102f4879dSRobert Watson 24227d37320SRobert Watson struct ifnet * 24327d37320SRobert Watson ifnet_byindex_ref(u_short idx) 24427d37320SRobert Watson { 24527d37320SRobert Watson struct ifnet *ifp; 24627d37320SRobert Watson 24777dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 24827d37320SRobert Watson ifp = ifnet_byindex_locked(idx); 249242a8e72SRobert Watson if (ifp == NULL || (ifp->if_flags & IFF_DYING)) { 25077dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 25127d37320SRobert Watson return (NULL); 25227d37320SRobert Watson } 25327d37320SRobert Watson if_ref(ifp); 25477dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 25527d37320SRobert Watson return (ifp); 25627d37320SRobert Watson } 25727d37320SRobert Watson 25861f6986bSRobert Watson /* 25961f6986bSRobert Watson * Allocate an ifindex array entry; return 0 on success or an error on 26061f6986bSRobert Watson * failure. 26161f6986bSRobert Watson */ 26261f6986bSRobert Watson static int 263ed2dabfcSRobert Watson ifindex_alloc_locked(u_short *idxp) 26461f6986bSRobert Watson { 26561f6986bSRobert Watson u_short idx; 26661f6986bSRobert Watson 26761f6986bSRobert Watson IFNET_WLOCK_ASSERT(); 26861f6986bSRobert Watson 2695f3b301aSJohn Baldwin retry: 27061f6986bSRobert Watson /* 271ed2dabfcSRobert Watson * Try to find an empty slot below V_if_index. If we fail, take the 27261f6986bSRobert Watson * next slot. 27361f6986bSRobert Watson */ 27461f6986bSRobert Watson for (idx = 1; idx <= V_if_index; idx++) { 275ed2dabfcSRobert Watson if (V_ifindex_table[idx].ife_ifnet == NULL) 27661f6986bSRobert Watson break; 27761f6986bSRobert Watson } 27861f6986bSRobert Watson 27961f6986bSRobert Watson /* Catch if_index overflow. */ 28061f6986bSRobert Watson if (idx < 1) 28161f6986bSRobert Watson return (ENOSPC); 2825f3b301aSJohn Baldwin if (idx >= V_if_indexlim) { 2835f3b301aSJohn Baldwin if_grow(); 2845f3b301aSJohn Baldwin goto retry; 2855f3b301aSJohn Baldwin } 28661f6986bSRobert Watson if (idx > V_if_index) 28761f6986bSRobert Watson V_if_index = idx; 28861f6986bSRobert Watson *idxp = idx; 28961f6986bSRobert Watson return (0); 29061f6986bSRobert Watson } 29161f6986bSRobert Watson 292e0c14af9SMarko Zec static void 293ed2dabfcSRobert Watson ifindex_free_locked(u_short idx) 294ed2dabfcSRobert Watson { 295ed2dabfcSRobert Watson 296ed2dabfcSRobert Watson IFNET_WLOCK_ASSERT(); 297ed2dabfcSRobert Watson 298ed2dabfcSRobert Watson V_ifindex_table[idx].ife_ifnet = NULL; 299ed2dabfcSRobert Watson while (V_if_index > 0 && 300ed2dabfcSRobert Watson V_ifindex_table[V_if_index].ife_ifnet == NULL) 301ed2dabfcSRobert Watson V_if_index--; 302ed2dabfcSRobert Watson } 303ed2dabfcSRobert Watson 304ed2dabfcSRobert Watson static void 305ed2dabfcSRobert Watson ifindex_free(u_short idx) 306ed2dabfcSRobert Watson { 307ed2dabfcSRobert Watson 308ed2dabfcSRobert Watson IFNET_WLOCK(); 309ed2dabfcSRobert Watson ifindex_free_locked(idx); 310ed2dabfcSRobert Watson IFNET_WUNLOCK(); 311ed2dabfcSRobert Watson } 312ed2dabfcSRobert Watson 313ed2dabfcSRobert Watson static void 31477dfcdc4SRobert Watson ifnet_setbyindex_locked(u_short idx, struct ifnet *ifp) 31502f4879dSRobert Watson { 31602f4879dSRobert Watson 31702f4879dSRobert Watson IFNET_WLOCK_ASSERT(); 31802f4879dSRobert Watson 319603724d3SBjoern A. Zeeb V_ifindex_table[idx].ife_ifnet = ifp; 32002f4879dSRobert Watson } 32102f4879dSRobert Watson 32277dfcdc4SRobert Watson static void 32377dfcdc4SRobert Watson ifnet_setbyindex(u_short idx, struct ifnet *ifp) 32477dfcdc4SRobert Watson { 32577dfcdc4SRobert Watson 32677dfcdc4SRobert Watson IFNET_WLOCK(); 32777dfcdc4SRobert Watson ifnet_setbyindex_locked(idx, ifp); 32877dfcdc4SRobert Watson IFNET_WUNLOCK(); 32977dfcdc4SRobert Watson } 33077dfcdc4SRobert Watson 33102f4879dSRobert Watson struct ifaddr * 33202f4879dSRobert Watson ifaddr_byindex(u_short idx) 33302f4879dSRobert Watson { 33402f4879dSRobert Watson struct ifaddr *ifa; 33502f4879dSRobert Watson 33677dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 337d24c444cSKip Macy ifa = ifnet_byindex_locked(idx)->if_addr; 3388c0fec80SRobert Watson if (ifa != NULL) 3398c0fec80SRobert Watson ifa_ref(ifa); 34077dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 34102f4879dSRobert Watson return (ifa); 34202f4879dSRobert Watson } 34302f4879dSRobert Watson 344df8bae1dSRodney W. Grimes /* 345df8bae1dSRodney W. Grimes * Network interface utility routines. 346df8bae1dSRodney W. Grimes * 347df8bae1dSRodney W. Grimes * Routines with ifa_ifwith* names take sockaddr *'s as 348df8bae1dSRodney W. Grimes * parameters. 349df8bae1dSRodney W. Grimes */ 350a45cbf12SBrooks Davis 351f9132cebSJonathan Lemon static void 352d0728d71SRobert Watson vnet_if_init(const void *unused __unused) 3531ed81b73SMarko Zec { 35444e33a07SMarko Zec 355603724d3SBjoern A. Zeeb TAILQ_INIT(&V_ifnet); 356603724d3SBjoern A. Zeeb TAILQ_INIT(&V_ifg_head); 3575f3b301aSJohn Baldwin IFNET_WLOCK(); 358f9132cebSJonathan Lemon if_grow(); /* create initial table */ 3595f3b301aSJohn Baldwin IFNET_WUNLOCK(); 360d0728d71SRobert Watson vnet_if_clone_init(); 361f9132cebSJonathan Lemon } 3625f3b301aSJohn Baldwin VNET_SYSINIT(vnet_if_init, SI_SUB_INIT_IF, SI_ORDER_SECOND, vnet_if_init, 363d0728d71SRobert Watson NULL); 364d0728d71SRobert Watson 365d0728d71SRobert Watson /* ARGSUSED*/ 366d0728d71SRobert Watson static void 367d0728d71SRobert Watson if_init(void *dummy __unused) 368d0728d71SRobert Watson { 369d0728d71SRobert Watson 370d0728d71SRobert Watson IFNET_LOCK_INIT(); 371d0728d71SRobert Watson if_clone_init(); 372d0728d71SRobert Watson } 3735f3b301aSJohn Baldwin SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL); 374d0728d71SRobert Watson 375f9132cebSJonathan Lemon 376bc29160dSMarko Zec #ifdef VIMAGE 377d0728d71SRobert Watson static void 378d0728d71SRobert Watson vnet_if_uninit(const void *unused __unused) 379bc29160dSMarko Zec { 380bc29160dSMarko Zec 3810028e524SBjoern A. Zeeb VNET_ASSERT(TAILQ_EMPTY(&V_ifnet), ("%s:%d tailq &V_ifnet=%p " 3820028e524SBjoern A. Zeeb "not empty", __func__, __LINE__, &V_ifnet)); 3830028e524SBjoern A. Zeeb VNET_ASSERT(TAILQ_EMPTY(&V_ifg_head), ("%s:%d tailq &V_ifg_head=%p " 3840028e524SBjoern A. Zeeb "not empty", __func__, __LINE__, &V_ifg_head)); 385bc29160dSMarko Zec 386bc29160dSMarko Zec free((caddr_t)V_ifindex_table, M_IFNET); 387bc29160dSMarko Zec } 388d0728d71SRobert Watson VNET_SYSUNINIT(vnet_if_uninit, SI_SUB_INIT_IF, SI_ORDER_FIRST, 389d0728d71SRobert Watson vnet_if_uninit, NULL); 390bc29160dSMarko Zec #endif 391bc29160dSMarko Zec 3928e937462SRobert Watson static void 393f9132cebSJonathan Lemon if_grow(void) 394f9132cebSJonathan Lemon { 3955f3b301aSJohn Baldwin int oldlim; 396f9132cebSJonathan Lemon u_int n; 397f9132cebSJonathan Lemon struct ifindex_entry *e; 398f9132cebSJonathan Lemon 3995f3b301aSJohn Baldwin IFNET_WLOCK_ASSERT(); 4005f3b301aSJohn Baldwin oldlim = V_if_indexlim; 4015f3b301aSJohn Baldwin IFNET_WUNLOCK(); 4025f3b301aSJohn Baldwin n = (oldlim << 1) * sizeof(*e); 403fc74a9f9SBrooks Davis e = malloc(n, M_IFNET, M_WAITOK | M_ZERO); 4045f3b301aSJohn Baldwin IFNET_WLOCK(); 4055f3b301aSJohn Baldwin if (V_if_indexlim != oldlim) { 4065f3b301aSJohn Baldwin free(e, M_IFNET); 4075f3b301aSJohn Baldwin return; 4085f3b301aSJohn Baldwin } 409603724d3SBjoern A. Zeeb if (V_ifindex_table != NULL) { 410603724d3SBjoern A. Zeeb memcpy((caddr_t)e, (caddr_t)V_ifindex_table, n/2); 411603724d3SBjoern A. Zeeb free((caddr_t)V_ifindex_table, M_IFNET); 412f9132cebSJonathan Lemon } 4135f3b301aSJohn Baldwin V_if_indexlim <<= 1; 414603724d3SBjoern A. Zeeb V_ifindex_table = e; 415f9132cebSJonathan Lemon } 416f9132cebSJonathan Lemon 417df8bae1dSRodney W. Grimes /* 418a45cbf12SBrooks Davis * Allocate a struct ifnet and an index for an interface. A layer 2 419a45cbf12SBrooks Davis * common structure will also be allocated if an allocation routine is 420a45cbf12SBrooks Davis * registered for the passed type. 421fc74a9f9SBrooks Davis */ 422fc74a9f9SBrooks Davis struct ifnet * 423fc74a9f9SBrooks Davis if_alloc(u_char type) 424fc74a9f9SBrooks Davis { 425fc74a9f9SBrooks Davis struct ifnet *ifp; 42661f6986bSRobert Watson u_short idx; 427fc74a9f9SBrooks Davis 428fc74a9f9SBrooks Davis ifp = malloc(sizeof(struct ifnet), M_IFNET, M_WAITOK|M_ZERO); 42961f6986bSRobert Watson IFNET_WLOCK(); 430ed2dabfcSRobert Watson if (ifindex_alloc_locked(&idx) != 0) { 43161f6986bSRobert Watson IFNET_WUNLOCK(); 432dc7c539eSBrooks Davis free(ifp, M_IFNET); 433dc7c539eSBrooks Davis return (NULL); 434dc7c539eSBrooks Davis } 435ed2dabfcSRobert Watson ifnet_setbyindex_locked(idx, IFNET_HOLD); 43661f6986bSRobert Watson IFNET_WUNLOCK(); 43761f6986bSRobert Watson ifp->if_index = idx; 438fc74a9f9SBrooks Davis ifp->if_type = type; 43927d37320SRobert Watson ifp->if_alloctype = type; 440fc74a9f9SBrooks Davis if (if_com_alloc[type] != NULL) { 441fc74a9f9SBrooks Davis ifp->if_l2com = if_com_alloc[type](type, ifp); 44228ef2db4SBrooks Davis if (ifp->if_l2com == NULL) { 443fc74a9f9SBrooks Davis free(ifp, M_IFNET); 444ed2dabfcSRobert Watson ifindex_free(idx); 44528ef2db4SBrooks Davis return (NULL); 44628ef2db4SBrooks Davis } 447fc74a9f9SBrooks Davis } 44827d37320SRobert Watson 44927d37320SRobert Watson IF_ADDR_LOCK_INIT(ifp); 450d6f157eaSRobert Watson TASK_INIT(&ifp->if_linktask, 0, do_link_state_change, ifp); 451d6f157eaSRobert Watson ifp->if_afdata_initialized = 0; 452e0c14af9SMarko Zec IF_AFDATA_LOCK_INIT(ifp); 453d6f157eaSRobert Watson TAILQ_INIT(&ifp->if_addrhead); 454d6f157eaSRobert Watson TAILQ_INIT(&ifp->if_prefixhead); 455d6f157eaSRobert Watson TAILQ_INIT(&ifp->if_multiaddrs); 456d6f157eaSRobert Watson TAILQ_INIT(&ifp->if_groups); 457d6f157eaSRobert Watson #ifdef MAC 458d6f157eaSRobert Watson mac_ifnet_init(ifp); 459d6f157eaSRobert Watson #endif 460d659538fSSam Leffler ifq_init(&ifp->if_snd, ifp); 461d6f157eaSRobert Watson 46227d37320SRobert Watson refcount_init(&ifp->if_refcount, 1); /* Index reference. */ 46302f4879dSRobert Watson ifnet_setbyindex(ifp->if_index, ifp); 464fc74a9f9SBrooks Davis return (ifp); 465fc74a9f9SBrooks Davis } 466fc74a9f9SBrooks Davis 467a45cbf12SBrooks Davis /* 468242a8e72SRobert Watson * Do the actual work of freeing a struct ifnet, associated index, and layer 469242a8e72SRobert Watson * 2 common structure. This call is made when the last reference to an 470242a8e72SRobert Watson * interface is released. 471a45cbf12SBrooks Davis */ 472242a8e72SRobert Watson static void 473242a8e72SRobert Watson if_free_internal(struct ifnet *ifp) 474fc74a9f9SBrooks Davis { 475fc74a9f9SBrooks Davis 476242a8e72SRobert Watson KASSERT((ifp->if_flags & IFF_DYING), 477242a8e72SRobert Watson ("if_free_internal: interface not dying")); 478fc74a9f9SBrooks Davis 47902f4879dSRobert Watson IFNET_WLOCK(); 48027d37320SRobert Watson KASSERT(ifp == ifnet_byindex_locked(ifp->if_index), 48127d37320SRobert Watson ("%s: freeing unallocated ifnet", ifp->if_xname)); 482242a8e72SRobert Watson 483ed2dabfcSRobert Watson ifindex_free_locked(ifp->if_index); 48402f4879dSRobert Watson IFNET_WUNLOCK(); 48528ef2db4SBrooks Davis 48627d37320SRobert Watson if (if_com_free[ifp->if_alloctype] != NULL) 48727d37320SRobert Watson if_com_free[ifp->if_alloctype](ifp->if_l2com, 48827d37320SRobert Watson ifp->if_alloctype); 489fc74a9f9SBrooks Davis 490d6f157eaSRobert Watson #ifdef MAC 491d6f157eaSRobert Watson mac_ifnet_destroy(ifp); 492d6f157eaSRobert Watson #endif /* MAC */ 493215940b3SXin LI if (ifp->if_description != NULL) 494215940b3SXin LI free(ifp->if_description, M_IFDESCR); 495d6f157eaSRobert Watson IF_AFDATA_DESTROY(ifp); 49602f4879dSRobert Watson IF_ADDR_LOCK_DESTROY(ifp); 497d659538fSSam Leffler ifq_delete(&ifp->if_snd); 498fc74a9f9SBrooks Davis free(ifp, M_IFNET); 499c0c9ea90SSam Leffler } 500fc74a9f9SBrooks Davis 501242a8e72SRobert Watson /* 502242a8e72SRobert Watson * This version should only be called by intefaces that switch their type 503242a8e72SRobert Watson * after calling if_alloc(). if_free_type() will go away again now that we 504242a8e72SRobert Watson * have if_alloctype to cache the original allocation type. For now, assert 505242a8e72SRobert Watson * that they match, since we require that in practice. 506242a8e72SRobert Watson */ 507242a8e72SRobert Watson void 508242a8e72SRobert Watson if_free_type(struct ifnet *ifp, u_char type) 509242a8e72SRobert Watson { 510242a8e72SRobert Watson 511242a8e72SRobert Watson KASSERT(ifp->if_alloctype == type, 512242a8e72SRobert Watson ("if_free_type: type (%d) != alloctype (%d)", type, 513242a8e72SRobert Watson ifp->if_alloctype)); 514242a8e72SRobert Watson 515242a8e72SRobert Watson ifp->if_flags |= IFF_DYING; /* XXX: Locking */ 516242a8e72SRobert Watson if (!refcount_release(&ifp->if_refcount)) 517242a8e72SRobert Watson return; 518242a8e72SRobert Watson if_free_internal(ifp); 519242a8e72SRobert Watson } 520242a8e72SRobert Watson 521242a8e72SRobert Watson /* 522242a8e72SRobert Watson * This is the normal version of if_free(), used by device drivers to free a 523242a8e72SRobert Watson * detached network interface. The contents of if_free_type() will move into 524242a8e72SRobert Watson * here when if_free_type() goes away. 525242a8e72SRobert Watson */ 526242a8e72SRobert Watson void 527242a8e72SRobert Watson if_free(struct ifnet *ifp) 528242a8e72SRobert Watson { 529242a8e72SRobert Watson 530242a8e72SRobert Watson if_free_type(ifp, ifp->if_alloctype); 531242a8e72SRobert Watson } 532242a8e72SRobert Watson 533242a8e72SRobert Watson /* 534242a8e72SRobert Watson * Interfaces to keep an ifnet type-stable despite the possibility of the 535242a8e72SRobert Watson * driver calling if_free(). If there are additional references, we defer 536242a8e72SRobert Watson * freeing the underlying data structure. 537242a8e72SRobert Watson */ 538db7f0b97SKip Macy void 53927d37320SRobert Watson if_ref(struct ifnet *ifp) 54027d37320SRobert Watson { 54127d37320SRobert Watson 54227d37320SRobert Watson /* We don't assert the ifnet list lock here, but arguably should. */ 54327d37320SRobert Watson refcount_acquire(&ifp->if_refcount); 54427d37320SRobert Watson } 54527d37320SRobert Watson 54627d37320SRobert Watson void 54727d37320SRobert Watson if_rele(struct ifnet *ifp) 54827d37320SRobert Watson { 54927d37320SRobert Watson 550242a8e72SRobert Watson if (!refcount_release(&ifp->if_refcount)) 551242a8e72SRobert Watson return; 552242a8e72SRobert Watson if_free_internal(ifp); 55327d37320SRobert Watson } 55427d37320SRobert Watson 55527d37320SRobert Watson void 556d659538fSSam Leffler ifq_init(struct ifaltq *ifq, struct ifnet *ifp) 557db7f0b97SKip Macy { 558db7f0b97SKip Macy 559db7f0b97SKip Macy mtx_init(&ifq->ifq_mtx, ifp->if_xname, "if send queue", MTX_DEF); 560db7f0b97SKip Macy 561db7f0b97SKip Macy if (ifq->ifq_maxlen == 0) 562db7f0b97SKip Macy ifq->ifq_maxlen = ifqmaxlen; 563db7f0b97SKip Macy 564db7f0b97SKip Macy ifq->altq_type = 0; 565db7f0b97SKip Macy ifq->altq_disc = NULL; 566db7f0b97SKip Macy ifq->altq_flags &= ALTQF_CANTCHANGE; 567db7f0b97SKip Macy ifq->altq_tbr = NULL; 568db7f0b97SKip Macy ifq->altq_ifp = ifp; 569db7f0b97SKip Macy } 570db7f0b97SKip Macy 571db7f0b97SKip Macy void 572d659538fSSam Leffler ifq_delete(struct ifaltq *ifq) 573db7f0b97SKip Macy { 574db7f0b97SKip Macy mtx_destroy(&ifq->ifq_mtx); 575db7f0b97SKip Macy } 576db7f0b97SKip Macy 577fc74a9f9SBrooks Davis /* 578a45cbf12SBrooks Davis * Perform generic interface initalization tasks and attach the interface 579e0c14af9SMarko Zec * to the list of "active" interfaces. If vmove flag is set on entry 580e0c14af9SMarko Zec * to if_attach_internal(), perform only a limited subset of initialization 581e0c14af9SMarko Zec * tasks, given that we are moving from one vnet to another an ifnet which 582e0c14af9SMarko Zec * has already been fully initialized. 583a45cbf12SBrooks Davis * 584a45cbf12SBrooks Davis * XXX: 585a45cbf12SBrooks Davis * - The decision to return void and thus require this function to 586a45cbf12SBrooks Davis * succeed is questionable. 587a45cbf12SBrooks Davis * - We should probably do more sanity checking. For instance we don't 588a45cbf12SBrooks Davis * do anything to insure if_xname is unique or non-empty. 589df8bae1dSRodney W. Grimes */ 590df8bae1dSRodney W. Grimes void 59172fd1b6aSDag-Erling Smørgrav if_attach(struct ifnet *ifp) 592df8bae1dSRodney W. Grimes { 593e0c14af9SMarko Zec 594e0c14af9SMarko Zec if_attach_internal(ifp, 0); 595e0c14af9SMarko Zec } 596e0c14af9SMarko Zec 597e0c14af9SMarko Zec static void 598e0c14af9SMarko Zec if_attach_internal(struct ifnet *ifp, int vmove) 599e0c14af9SMarko Zec { 600df8bae1dSRodney W. Grimes unsigned socksize, ifasize; 6011ce9bf88SPoul-Henning Kamp int namelen, masklen; 60272fd1b6aSDag-Erling Smørgrav struct sockaddr_dl *sdl; 60372fd1b6aSDag-Erling Smørgrav struct ifaddr *ifa; 604df8bae1dSRodney W. Grimes 605fc74a9f9SBrooks Davis if (ifp->if_index == 0 || ifp != ifnet_byindex(ifp->if_index)) 606fc74a9f9SBrooks Davis panic ("%s: BUG: if_attach called without if_alloc'd input()\n", 607fc74a9f9SBrooks Davis ifp->if_xname); 608fc74a9f9SBrooks Davis 609f6dfe47aSMarko Zec #ifdef VIMAGE 610f6dfe47aSMarko Zec ifp->if_vnet = curvnet; 611bc29160dSMarko Zec if (ifp->if_home_vnet == NULL) 612bc29160dSMarko Zec ifp->if_home_vnet = curvnet; 613f6dfe47aSMarko Zec #endif 614f6dfe47aSMarko Zec 6150dad3f0eSMax Laier if_addgroup(ifp, IFG_ALL); 6160dad3f0eSMax Laier 61798b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 618bc9d2991SBrooks Davis ifp->if_data.ifi_epoch = time_uptime; 619fc74a9f9SBrooks Davis ifp->if_data.ifi_datalen = sizeof(struct if_data); 620d6f157eaSRobert Watson 6217cc5b47fSKip Macy KASSERT((ifp->if_transmit == NULL && ifp->if_qflush == NULL) || 6227cc5b47fSKip Macy (ifp->if_transmit != NULL && ifp->if_qflush != NULL), 6237cc5b47fSKip Macy ("transmit and qflush must both either be set or both be NULL")); 6247cc5b47fSKip Macy if (ifp->if_transmit == NULL) { 625db7f0b97SKip Macy ifp->if_transmit = if_transmit; 626db7f0b97SKip Macy ifp->if_qflush = if_qflush; 6277cc5b47fSKip Macy } 6287cc5b47fSKip Macy 629e0c14af9SMarko Zec if (!vmove) { 630e70cd263SRobert Watson #ifdef MAC 63130d239bcSRobert Watson mac_ifnet_create(ifp); 632e70cd263SRobert Watson #endif 633e70cd263SRobert Watson 634df8bae1dSRodney W. Grimes /* 635e0c14af9SMarko Zec * Create a Link Level name for this device. 636df8bae1dSRodney W. Grimes */ 6379bf40edeSBrooks Davis namelen = strlen(ifp->if_xname); 63836c19a57SBrooks Davis /* 639e0c14af9SMarko Zec * Always save enough space for any possiable name so we 640e0c14af9SMarko Zec * can do a rename in place later. 64136c19a57SBrooks Davis */ 64236c19a57SBrooks Davis masklen = offsetof(struct sockaddr_dl, sdl_data[0]) + IFNAMSIZ; 643df8bae1dSRodney W. Grimes socksize = masklen + ifp->if_addrlen; 644df8bae1dSRodney W. Grimes if (socksize < sizeof(*sdl)) 645df8bae1dSRodney W. Grimes socksize = sizeof(*sdl); 646ccb82468SBrooks Davis socksize = roundup2(socksize, sizeof(long)); 647df8bae1dSRodney W. Grimes ifasize = sizeof(*ifa) + 2 * socksize; 648a8773564SBrooks Davis ifa = malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO); 6491099f828SRobert Watson ifa_init(ifa); 650df8bae1dSRodney W. Grimes sdl = (struct sockaddr_dl *)(ifa + 1); 651df8bae1dSRodney W. Grimes sdl->sdl_len = socksize; 652df8bae1dSRodney W. Grimes sdl->sdl_family = AF_LINK; 6539bf40edeSBrooks Davis bcopy(ifp->if_xname, sdl->sdl_data, namelen); 6541ce9bf88SPoul-Henning Kamp sdl->sdl_nlen = namelen; 655df8bae1dSRodney W. Grimes sdl->sdl_index = ifp->if_index; 656df8bae1dSRodney W. Grimes sdl->sdl_type = ifp->if_type; 6574a0d6638SRuslan Ermilov ifp->if_addr = ifa; 658df8bae1dSRodney W. Grimes ifa->ifa_ifp = ifp; 659df8bae1dSRodney W. Grimes ifa->ifa_rtrequest = link_rtrequest; 660df8bae1dSRodney W. Grimes ifa->ifa_addr = (struct sockaddr *)sdl; 661df8bae1dSRodney W. Grimes sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl); 662df8bae1dSRodney W. Grimes ifa->ifa_netmask = (struct sockaddr *)sdl; 663df8bae1dSRodney W. Grimes sdl->sdl_len = masklen; 664df8bae1dSRodney W. Grimes while (namelen != 0) 665df8bae1dSRodney W. Grimes sdl->sdl_data[--namelen] = 0xff; 66659562606SGarrett Wollman TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link); 667e0c14af9SMarko Zec /* Reliably crash if used uninitialized. */ 668e0c14af9SMarko Zec ifp->if_broadcastaddr = NULL; 669e0c14af9SMarko Zec } 67052db6805SMarko Zec #ifdef VIMAGE 67152db6805SMarko Zec else { 67252db6805SMarko Zec /* 67352db6805SMarko Zec * Update the interface index in the link layer address 67452db6805SMarko Zec * of the interface. 67552db6805SMarko Zec */ 67652db6805SMarko Zec for (ifa = ifp->if_addr; ifa != NULL; 67752db6805SMarko Zec ifa = TAILQ_NEXT(ifa, ifa_link)) { 67852db6805SMarko Zec if (ifa->ifa_addr->sa_family == AF_LINK) { 67952db6805SMarko Zec sdl = (struct sockaddr_dl *)ifa->ifa_addr; 68052db6805SMarko Zec sdl->sdl_index = ifp->if_index; 68152db6805SMarko Zec } 68252db6805SMarko Zec } 68352db6805SMarko Zec } 68452db6805SMarko Zec #endif 685d94ccb09SBrooks Davis 686457f48e6SGleb Smirnoff IFNET_WLOCK(); 687603724d3SBjoern A. Zeeb TAILQ_INSERT_TAIL(&V_ifnet, ifp, if_link); 68821ca7b57SMarko Zec #ifdef VIMAGE 689006e9db4SRobert Watson curvnet->vnet_ifcnt++; 69021ca7b57SMarko Zec #endif 691457f48e6SGleb Smirnoff IFNET_WUNLOCK(); 692457f48e6SGleb Smirnoff 69369fb23b7SMax Laier if (domain_init_status >= 2) 69431b1bfe1SHajimu UMEMOTO if_attachdomain1(ifp); 69531b1bfe1SHajimu UMEMOTO 69625a4adceSMax Laier EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 69721ca7b57SMarko Zec if (IS_DEFAULT_VNET(curvnet)) 698f3b90d48SAndrew Thompson devctl_notify("IFNET", ifp->if_xname, "ATTACH", NULL); 69925a4adceSMax Laier 7007b6edd04SRuslan Ermilov /* Announce the interface. */ 7017b6edd04SRuslan Ermilov rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 702df8bae1dSRodney W. Grimes } 7036182fdbdSPeter Wemm 70431b1bfe1SHajimu UMEMOTO static void 70572fd1b6aSDag-Erling Smørgrav if_attachdomain(void *dummy) 70631b1bfe1SHajimu UMEMOTO { 70731b1bfe1SHajimu UMEMOTO struct ifnet *ifp; 70831b1bfe1SHajimu UMEMOTO int s; 70931b1bfe1SHajimu UMEMOTO 71031b1bfe1SHajimu UMEMOTO s = splnet(); 711603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifp, &V_ifnet, if_link) 71231b1bfe1SHajimu UMEMOTO if_attachdomain1(ifp); 71331b1bfe1SHajimu UMEMOTO splx(s); 71431b1bfe1SHajimu UMEMOTO } 71569fb23b7SMax Laier SYSINIT(domainifattach, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_SECOND, 71631b1bfe1SHajimu UMEMOTO if_attachdomain, NULL); 71731b1bfe1SHajimu UMEMOTO 71831b1bfe1SHajimu UMEMOTO static void 71972fd1b6aSDag-Erling Smørgrav if_attachdomain1(struct ifnet *ifp) 72031b1bfe1SHajimu UMEMOTO { 72131b1bfe1SHajimu UMEMOTO struct domain *dp; 72231b1bfe1SHajimu UMEMOTO int s; 72331b1bfe1SHajimu UMEMOTO 72431b1bfe1SHajimu UMEMOTO s = splnet(); 72531b1bfe1SHajimu UMEMOTO 726234a35c7SHajimu UMEMOTO /* 727234a35c7SHajimu UMEMOTO * Since dp->dom_ifattach calls malloc() with M_WAITOK, we 728234a35c7SHajimu UMEMOTO * cannot lock ifp->if_afdata initialization, entirely. 729234a35c7SHajimu UMEMOTO */ 730234a35c7SHajimu UMEMOTO if (IF_AFDATA_TRYLOCK(ifp) == 0) { 731234a35c7SHajimu UMEMOTO splx(s); 732234a35c7SHajimu UMEMOTO return; 733234a35c7SHajimu UMEMOTO } 73469fb23b7SMax Laier if (ifp->if_afdata_initialized >= domain_init_status) { 735234a35c7SHajimu UMEMOTO IF_AFDATA_UNLOCK(ifp); 736234a35c7SHajimu UMEMOTO splx(s); 7376237419dSRobert Watson printf("if_attachdomain called more than once on %s\n", 7386237419dSRobert Watson ifp->if_xname); 739234a35c7SHajimu UMEMOTO return; 740234a35c7SHajimu UMEMOTO } 74169fb23b7SMax Laier ifp->if_afdata_initialized = domain_init_status; 742234a35c7SHajimu UMEMOTO IF_AFDATA_UNLOCK(ifp); 743234a35c7SHajimu UMEMOTO 74431b1bfe1SHajimu UMEMOTO /* address family dependent data region */ 74531b1bfe1SHajimu UMEMOTO bzero(ifp->if_afdata, sizeof(ifp->if_afdata)); 74631b1bfe1SHajimu UMEMOTO for (dp = domains; dp; dp = dp->dom_next) { 74731b1bfe1SHajimu UMEMOTO if (dp->dom_ifattach) 74831b1bfe1SHajimu UMEMOTO ifp->if_afdata[dp->dom_family] = 74931b1bfe1SHajimu UMEMOTO (*dp->dom_ifattach)(ifp); 75031b1bfe1SHajimu UMEMOTO } 75131b1bfe1SHajimu UMEMOTO 75231b1bfe1SHajimu UMEMOTO splx(s); 75331b1bfe1SHajimu UMEMOTO } 75431b1bfe1SHajimu UMEMOTO 7556182fdbdSPeter Wemm /* 756ec002feeSBruce M Simpson * Remove any unicast or broadcast network addresses from an interface. 75745778b37SPeter Edwards */ 75845778b37SPeter Edwards void 75945778b37SPeter Edwards if_purgeaddrs(struct ifnet *ifp) 76045778b37SPeter Edwards { 76145778b37SPeter Edwards struct ifaddr *ifa, *next; 76245778b37SPeter Edwards 76345778b37SPeter Edwards TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) { 7644b97d7afSYaroslav Tykhiy if (ifa->ifa_addr->sa_family == AF_LINK) 76545778b37SPeter Edwards continue; 76645778b37SPeter Edwards #ifdef INET 76745778b37SPeter Edwards /* XXX: Ugly!! ad hoc just for INET */ 7684b97d7afSYaroslav Tykhiy if (ifa->ifa_addr->sa_family == AF_INET) { 76945778b37SPeter Edwards struct ifaliasreq ifr; 77045778b37SPeter Edwards 77145778b37SPeter Edwards bzero(&ifr, sizeof(ifr)); 77245778b37SPeter Edwards ifr.ifra_addr = *ifa->ifa_addr; 77345778b37SPeter Edwards if (ifa->ifa_dstaddr) 77445778b37SPeter Edwards ifr.ifra_broadaddr = *ifa->ifa_dstaddr; 77545778b37SPeter Edwards if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp, 77645778b37SPeter Edwards NULL) == 0) 77745778b37SPeter Edwards continue; 77845778b37SPeter Edwards } 77945778b37SPeter Edwards #endif /* INET */ 78045778b37SPeter Edwards #ifdef INET6 7814b97d7afSYaroslav Tykhiy if (ifa->ifa_addr->sa_family == AF_INET6) { 78245778b37SPeter Edwards in6_purgeaddr(ifa); 78345778b37SPeter Edwards /* ifp_addrhead is already updated */ 78445778b37SPeter Edwards continue; 78545778b37SPeter Edwards } 78645778b37SPeter Edwards #endif /* INET6 */ 78745778b37SPeter Edwards TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 7881099f828SRobert Watson ifa_free(ifa); 78945778b37SPeter Edwards } 79045778b37SPeter Edwards } 79145778b37SPeter Edwards 79245778b37SPeter Edwards /* 79393ec7edcSShteryana Shopova * Remove any multicast network addresses from an interface when an ifnet 79493ec7edcSShteryana Shopova * is going away. 795ec002feeSBruce M Simpson */ 79693ec7edcSShteryana Shopova static void 797ec002feeSBruce M Simpson if_purgemaddrs(struct ifnet *ifp) 798ec002feeSBruce M Simpson { 799ec002feeSBruce M Simpson struct ifmultiaddr *ifma; 800ec002feeSBruce M Simpson struct ifmultiaddr *next; 801ec002feeSBruce M Simpson 802ec002feeSBruce M Simpson IF_ADDR_LOCK(ifp); 803ec002feeSBruce M Simpson TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) 804ec002feeSBruce M Simpson if_delmulti_locked(ifp, ifma, 1); 805ec002feeSBruce M Simpson IF_ADDR_UNLOCK(ifp); 806ec002feeSBruce M Simpson } 807ec002feeSBruce M Simpson 808ec002feeSBruce M Simpson /* 809e0c14af9SMarko Zec * Detach an interface, removing it from the list of "active" interfaces. 810e0c14af9SMarko Zec * If vmove flag is set on entry to if_detach_internal(), perform only a 811e0c14af9SMarko Zec * limited subset of cleanup tasks, given that we are moving an ifnet from 812e0c14af9SMarko Zec * one vnet to another, where it must be fully operational. 813b1c53bc9SRobert Watson * 814b1c53bc9SRobert Watson * XXXRW: There are some significant questions about event ordering, and 815b1c53bc9SRobert Watson * how to prevent things from starting to use the interface during detach. 8166182fdbdSPeter Wemm */ 8176182fdbdSPeter Wemm void 81872fd1b6aSDag-Erling Smørgrav if_detach(struct ifnet *ifp) 8196182fdbdSPeter Wemm { 820e0c14af9SMarko Zec 821e0c14af9SMarko Zec if_detach_internal(ifp, 0); 822e0c14af9SMarko Zec } 823e0c14af9SMarko Zec 824e0c14af9SMarko Zec static void 825e0c14af9SMarko Zec if_detach_internal(struct ifnet *ifp, int vmove) 826e0c14af9SMarko Zec { 82745778b37SPeter Edwards struct ifaddr *ifa; 8285500d3beSWarner Losh struct radix_node_head *rnh; 829e0c14af9SMarko Zec int i, j; 83031b1bfe1SHajimu UMEMOTO struct domain *dp; 8313f35d515SPeter Pentchev struct ifnet *iter; 832457f48e6SGleb Smirnoff int found = 0; 833457f48e6SGleb Smirnoff 834457f48e6SGleb Smirnoff IFNET_WLOCK(); 835603724d3SBjoern A. Zeeb TAILQ_FOREACH(iter, &V_ifnet, if_link) 836457f48e6SGleb Smirnoff if (iter == ifp) { 837603724d3SBjoern A. Zeeb TAILQ_REMOVE(&V_ifnet, ifp, if_link); 838457f48e6SGleb Smirnoff found = 1; 839457f48e6SGleb Smirnoff break; 840457f48e6SGleb Smirnoff } 84121ca7b57SMarko Zec #ifdef VIMAGE 84221ca7b57SMarko Zec if (found) 843006e9db4SRobert Watson curvnet->vnet_ifcnt--; 84421ca7b57SMarko Zec #endif 845457f48e6SGleb Smirnoff IFNET_WUNLOCK(); 846e0c14af9SMarko Zec if (!found) { 847e0c14af9SMarko Zec if (vmove) 84858606037SBjoern A. Zeeb panic("%s: ifp=%p not on the ifnet tailq %p", 84958606037SBjoern A. Zeeb __func__, ifp, &V_ifnet); 850e0c14af9SMarko Zec else 851e0c14af9SMarko Zec return; /* XXX this should panic as well? */ 852e0c14af9SMarko Zec } 8536182fdbdSPeter Wemm 85468a3482fSGleb Smirnoff /* 85568a3482fSGleb Smirnoff * Remove/wait for pending events. 85668a3482fSGleb Smirnoff */ 85768a3482fSGleb Smirnoff taskqueue_drain(taskqueue_swi, &ifp->if_linktask); 85868a3482fSGleb Smirnoff 8596182fdbdSPeter Wemm /* 8606182fdbdSPeter Wemm * Remove routes and flush queues. 8616182fdbdSPeter Wemm */ 8626182fdbdSPeter Wemm if_down(ifp); 86302b199f1SMax Laier #ifdef ALTQ 86402b199f1SMax Laier if (ALTQ_IS_ENABLED(&ifp->if_snd)) 86502b199f1SMax Laier altq_disable(&ifp->if_snd); 86602b199f1SMax Laier if (ALTQ_IS_ATTACHED(&ifp->if_snd)) 86702b199f1SMax Laier altq_detach(&ifp->if_snd); 86802b199f1SMax Laier #endif 8696182fdbdSPeter Wemm 87045778b37SPeter Edwards if_purgeaddrs(ifp); 8716182fdbdSPeter Wemm 872b1c53bc9SRobert Watson #ifdef INET 873b1c53bc9SRobert Watson in_ifdetach(ifp); 874b1c53bc9SRobert Watson #endif 875b1c53bc9SRobert Watson 87633841545SHajimu UMEMOTO #ifdef INET6 87733841545SHajimu UMEMOTO /* 87833841545SHajimu UMEMOTO * Remove all IPv6 kernel structs related to ifp. This should be done 87933841545SHajimu UMEMOTO * before removing routing entries below, since IPv6 interface direct 88033841545SHajimu UMEMOTO * routes are expected to be removed by the IPv6-specific kernel API. 88133841545SHajimu UMEMOTO * Otherwise, the kernel will detect some inconsistency and bark it. 88233841545SHajimu UMEMOTO */ 88333841545SHajimu UMEMOTO in6_ifdetach(ifp); 88433841545SHajimu UMEMOTO #endif 885ec002feeSBruce M Simpson if_purgemaddrs(ifp); 886ec002feeSBruce M Simpson 887e0c14af9SMarko Zec if (!vmove) { 888f4247b59SLuigi Rizzo /* 889111c6b61SRobert Watson * Prevent further calls into the device driver via ifnet. 890111c6b61SRobert Watson */ 891111c6b61SRobert Watson if_dead(ifp); 892111c6b61SRobert Watson 893111c6b61SRobert Watson /* 8944a0d6638SRuslan Ermilov * Remove link ifaddr pointer and maybe decrement if_index. 895f4247b59SLuigi Rizzo * Clean up all addresses. 896f4247b59SLuigi Rizzo */ 8974a0d6638SRuslan Ermilov ifp->if_addr = NULL; 898f4247b59SLuigi Rizzo 899212bd869SHajimu UMEMOTO /* We can now free link ifaddr. */ 9003f35d515SPeter Pentchev if (!TAILQ_EMPTY(&ifp->if_addrhead)) { 901212bd869SHajimu UMEMOTO ifa = TAILQ_FIRST(&ifp->if_addrhead); 902212bd869SHajimu UMEMOTO TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 9031099f828SRobert Watson ifa_free(ifa); 9043f35d515SPeter Pentchev } 905e0c14af9SMarko Zec } 906212bd869SHajimu UMEMOTO 9075500d3beSWarner Losh /* 9085500d3beSWarner Losh * Delete all remaining routes using this interface 9095500d3beSWarner Losh * Unfortuneatly the only way to do this is to slog through 9105500d3beSWarner Losh * the entire routing table looking for routes which point 9115500d3beSWarner Losh * to this interface...oh well... 9125500d3beSWarner Losh */ 9135500d3beSWarner Losh for (i = 1; i <= AF_MAX; i++) { 9148b07e49aSJulian Elischer for (j = 0; j < rt_numfibs; j++) { 915c2c2a7c1SBjoern A. Zeeb rnh = rt_tables_get_rnh(j, i); 916c2c2a7c1SBjoern A. Zeeb if (rnh == NULL) 9175500d3beSWarner Losh continue; 918956b0b65SJeffrey Hsu RADIX_NODE_HEAD_LOCK(rnh); 9195500d3beSWarner Losh (void) rnh->rnh_walktree(rnh, if_rtdel, ifp); 920956b0b65SJeffrey Hsu RADIX_NODE_HEAD_UNLOCK(rnh); 9215500d3beSWarner Losh } 9228b07e49aSJulian Elischer } 9235500d3beSWarner Losh 9247b6edd04SRuslan Ermilov /* Announce that the interface is gone. */ 9257b6edd04SRuslan Ermilov rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 92652023244SMax Laier EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 92721ca7b57SMarko Zec if (IS_DEFAULT_VNET(curvnet)) 928f3b90d48SAndrew Thompson devctl_notify("IFNET", ifp->if_xname, "DETACH", NULL); 9298623f9fdSMax Laier if_delgroups(ifp); 9307b6edd04SRuslan Ermilov 931d8c13659SBjoern A. Zeeb /* 932d8c13659SBjoern A. Zeeb * We cannot hold the lock over dom_ifdetach calls as they might 933d8c13659SBjoern A. Zeeb * sleep, for example trying to drain a callout, thus open up the 934d8c13659SBjoern A. Zeeb * theoretical race with re-attaching. 935d8c13659SBjoern A. Zeeb */ 936234a35c7SHajimu UMEMOTO IF_AFDATA_LOCK(ifp); 937d8c13659SBjoern A. Zeeb i = ifp->if_afdata_initialized; 938d8c13659SBjoern A. Zeeb ifp->if_afdata_initialized = 0; 939d8c13659SBjoern A. Zeeb IF_AFDATA_UNLOCK(ifp); 940d8c13659SBjoern A. Zeeb for (dp = domains; i > 0 && dp; dp = dp->dom_next) { 94131b1bfe1SHajimu UMEMOTO if (dp->dom_ifdetach && ifp->if_afdata[dp->dom_family]) 94231b1bfe1SHajimu UMEMOTO (*dp->dom_ifdetach)(ifp, 94331b1bfe1SHajimu UMEMOTO ifp->if_afdata[dp->dom_family]); 94431b1bfe1SHajimu UMEMOTO } 9455500d3beSWarner Losh } 9465500d3beSWarner Losh 947e0c14af9SMarko Zec #ifdef VIMAGE 948e0c14af9SMarko Zec /* 949e0c14af9SMarko Zec * if_vmove() performs a limited version of if_detach() in current 950e0c14af9SMarko Zec * vnet and if_attach()es the ifnet to the vnet specified as 2nd arg. 951e0c14af9SMarko Zec * An attempt is made to shrink if_index in current vnet, find an 952e0c14af9SMarko Zec * unused if_index in target vnet and calls if_grow() if necessary, 953e0c14af9SMarko Zec * and finally find an unused if_xname for the target vnet. 954e0c14af9SMarko Zec */ 955e0c14af9SMarko Zec void 956e0c14af9SMarko Zec if_vmove(struct ifnet *ifp, struct vnet *new_vnet) 957e0c14af9SMarko Zec { 95861f6986bSRobert Watson u_short idx; 959e0c14af9SMarko Zec 960e0c14af9SMarko Zec /* 961e0c14af9SMarko Zec * Detach from current vnet, but preserve LLADDR info, do not 962e0c14af9SMarko Zec * mark as dead etc. so that the ifnet can be reattached later. 963e0c14af9SMarko Zec */ 964e0c14af9SMarko Zec if_detach_internal(ifp, 1); 965e0c14af9SMarko Zec 966e0c14af9SMarko Zec /* 96777dfcdc4SRobert Watson * Unlink the ifnet from ifindex_table[] in current vnet, and shrink 96877dfcdc4SRobert Watson * the if_index for that vnet if possible. 96977dfcdc4SRobert Watson * 97077dfcdc4SRobert Watson * NOTE: IFNET_WLOCK/IFNET_WUNLOCK() are assumed to be unvirtualized, 97177dfcdc4SRobert Watson * or we'd lock on one vnet and unlock on another. 972e0c14af9SMarko Zec */ 973e0c14af9SMarko Zec IFNET_WLOCK(); 974ed2dabfcSRobert Watson ifindex_free_locked(ifp->if_index); 975d3c351c5SMarko Zec IFNET_WUNLOCK(); 976d3c351c5SMarko Zec 977d3c351c5SMarko Zec /* 978d3c351c5SMarko Zec * Perform interface-specific reassignment tasks, if provided by 979d3c351c5SMarko Zec * the driver. 980d3c351c5SMarko Zec */ 981d3c351c5SMarko Zec if (ifp->if_reassign != NULL) 982d3c351c5SMarko Zec ifp->if_reassign(ifp, new_vnet, NULL); 983e0c14af9SMarko Zec 984e0c14af9SMarko Zec /* 985e0c14af9SMarko Zec * Switch to the context of the target vnet. 986e0c14af9SMarko Zec */ 987e0c14af9SMarko Zec CURVNET_SET_QUIET(new_vnet); 988e0c14af9SMarko Zec 989d3c351c5SMarko Zec IFNET_WLOCK(); 990ed2dabfcSRobert Watson if (ifindex_alloc_locked(&idx) != 0) { 99161f6986bSRobert Watson IFNET_WUNLOCK(); 992e0c14af9SMarko Zec panic("if_index overflow"); 99361f6986bSRobert Watson } 99461f6986bSRobert Watson ifp->if_index = idx; 99577dfcdc4SRobert Watson ifnet_setbyindex_locked(ifp->if_index, ifp); 996e0c14af9SMarko Zec IFNET_WUNLOCK(); 997e0c14af9SMarko Zec 998e0c14af9SMarko Zec if_attach_internal(ifp, 1); 999e0c14af9SMarko Zec 1000e0c14af9SMarko Zec CURVNET_RESTORE(); 1001e0c14af9SMarko Zec } 1002be31e5e7SBjoern A. Zeeb 1003be31e5e7SBjoern A. Zeeb /* 1004be31e5e7SBjoern A. Zeeb * Move an ifnet to or from another child prison/vnet, specified by the jail id. 1005be31e5e7SBjoern A. Zeeb */ 1006be31e5e7SBjoern A. Zeeb static int 1007be31e5e7SBjoern A. Zeeb if_vmove_loan(struct thread *td, struct ifnet *ifp, char *ifname, int jid) 1008be31e5e7SBjoern A. Zeeb { 1009be31e5e7SBjoern A. Zeeb struct prison *pr; 1010be31e5e7SBjoern A. Zeeb struct ifnet *difp; 1011be31e5e7SBjoern A. Zeeb 1012be31e5e7SBjoern A. Zeeb /* Try to find the prison within our visibility. */ 1013be31e5e7SBjoern A. Zeeb sx_slock(&allprison_lock); 1014be31e5e7SBjoern A. Zeeb pr = prison_find_child(td->td_ucred->cr_prison, jid); 1015be31e5e7SBjoern A. Zeeb sx_sunlock(&allprison_lock); 1016be31e5e7SBjoern A. Zeeb if (pr == NULL) 1017be31e5e7SBjoern A. Zeeb return (ENXIO); 1018be31e5e7SBjoern A. Zeeb prison_hold_locked(pr); 1019be31e5e7SBjoern A. Zeeb mtx_unlock(&pr->pr_mtx); 1020be31e5e7SBjoern A. Zeeb 1021be31e5e7SBjoern A. Zeeb /* Do not try to move the iface from and to the same prison. */ 1022be31e5e7SBjoern A. Zeeb if (pr->pr_vnet == ifp->if_vnet) { 1023be31e5e7SBjoern A. Zeeb prison_free(pr); 1024be31e5e7SBjoern A. Zeeb return (EEXIST); 1025be31e5e7SBjoern A. Zeeb } 1026be31e5e7SBjoern A. Zeeb 1027be31e5e7SBjoern A. Zeeb /* Make sure the named iface does not exists in the dst. prison/vnet. */ 1028be31e5e7SBjoern A. Zeeb /* XXX Lock interfaces to avoid races. */ 10299abb4862SMarko Zec CURVNET_SET_QUIET(pr->pr_vnet); 1030be31e5e7SBjoern A. Zeeb difp = ifunit(ifname); 1031be31e5e7SBjoern A. Zeeb CURVNET_RESTORE(); 1032be31e5e7SBjoern A. Zeeb if (difp != NULL) { 1033be31e5e7SBjoern A. Zeeb prison_free(pr); 1034be31e5e7SBjoern A. Zeeb return (EEXIST); 1035be31e5e7SBjoern A. Zeeb } 1036be31e5e7SBjoern A. Zeeb 1037be31e5e7SBjoern A. Zeeb /* Move the interface into the child jail/vnet. */ 1038be31e5e7SBjoern A. Zeeb if_vmove(ifp, pr->pr_vnet); 1039be31e5e7SBjoern A. Zeeb 1040be31e5e7SBjoern A. Zeeb /* Report the new if_xname back to the userland. */ 1041be31e5e7SBjoern A. Zeeb sprintf(ifname, "%s", ifp->if_xname); 1042be31e5e7SBjoern A. Zeeb 1043be31e5e7SBjoern A. Zeeb prison_free(pr); 1044be31e5e7SBjoern A. Zeeb return (0); 1045be31e5e7SBjoern A. Zeeb } 1046be31e5e7SBjoern A. Zeeb 1047be31e5e7SBjoern A. Zeeb static int 1048be31e5e7SBjoern A. Zeeb if_vmove_reclaim(struct thread *td, char *ifname, int jid) 1049be31e5e7SBjoern A. Zeeb { 1050be31e5e7SBjoern A. Zeeb struct prison *pr; 1051be31e5e7SBjoern A. Zeeb struct vnet *vnet_dst; 1052be31e5e7SBjoern A. Zeeb struct ifnet *ifp; 1053be31e5e7SBjoern A. Zeeb 1054be31e5e7SBjoern A. Zeeb /* Try to find the prison within our visibility. */ 1055be31e5e7SBjoern A. Zeeb sx_slock(&allprison_lock); 1056be31e5e7SBjoern A. Zeeb pr = prison_find_child(td->td_ucred->cr_prison, jid); 1057be31e5e7SBjoern A. Zeeb sx_sunlock(&allprison_lock); 1058be31e5e7SBjoern A. Zeeb if (pr == NULL) 1059be31e5e7SBjoern A. Zeeb return (ENXIO); 1060be31e5e7SBjoern A. Zeeb prison_hold_locked(pr); 1061be31e5e7SBjoern A. Zeeb mtx_unlock(&pr->pr_mtx); 1062be31e5e7SBjoern A. Zeeb 1063be31e5e7SBjoern A. Zeeb /* Make sure the named iface exists in the source prison/vnet. */ 1064be31e5e7SBjoern A. Zeeb CURVNET_SET(pr->pr_vnet); 1065be31e5e7SBjoern A. Zeeb ifp = ifunit(ifname); /* XXX Lock to avoid races. */ 1066be31e5e7SBjoern A. Zeeb if (ifp == NULL) { 1067be31e5e7SBjoern A. Zeeb CURVNET_RESTORE(); 1068be31e5e7SBjoern A. Zeeb prison_free(pr); 1069be31e5e7SBjoern A. Zeeb return (ENXIO); 1070be31e5e7SBjoern A. Zeeb } 1071be31e5e7SBjoern A. Zeeb 1072be31e5e7SBjoern A. Zeeb /* Do not try to move the iface from and to the same prison. */ 1073be31e5e7SBjoern A. Zeeb vnet_dst = TD_TO_VNET(td); 1074be31e5e7SBjoern A. Zeeb if (vnet_dst == ifp->if_vnet) { 1075be31e5e7SBjoern A. Zeeb CURVNET_RESTORE(); 1076be31e5e7SBjoern A. Zeeb prison_free(pr); 1077be31e5e7SBjoern A. Zeeb return (EEXIST); 1078be31e5e7SBjoern A. Zeeb } 1079be31e5e7SBjoern A. Zeeb 1080be31e5e7SBjoern A. Zeeb /* Get interface back from child jail/vnet. */ 1081be31e5e7SBjoern A. Zeeb if_vmove(ifp, vnet_dst); 1082be31e5e7SBjoern A. Zeeb CURVNET_RESTORE(); 1083be31e5e7SBjoern A. Zeeb 1084be31e5e7SBjoern A. Zeeb /* Report the new if_xname back to the userland. */ 1085be31e5e7SBjoern A. Zeeb sprintf(ifname, "%s", ifp->if_xname); 1086be31e5e7SBjoern A. Zeeb 1087be31e5e7SBjoern A. Zeeb prison_free(pr); 1088be31e5e7SBjoern A. Zeeb return (0); 1089be31e5e7SBjoern A. Zeeb } 1090e0c14af9SMarko Zec #endif /* VIMAGE */ 1091e0c14af9SMarko Zec 10925500d3beSWarner Losh /* 10930dad3f0eSMax Laier * Add a group to an interface 10940dad3f0eSMax Laier */ 10950dad3f0eSMax Laier int 10960dad3f0eSMax Laier if_addgroup(struct ifnet *ifp, const char *groupname) 10970dad3f0eSMax Laier { 10980dad3f0eSMax Laier struct ifg_list *ifgl; 10990dad3f0eSMax Laier struct ifg_group *ifg = NULL; 11000dad3f0eSMax Laier struct ifg_member *ifgm; 11010dad3f0eSMax Laier 11020dad3f0eSMax Laier if (groupname[0] && groupname[strlen(groupname) - 1] >= '0' && 11030dad3f0eSMax Laier groupname[strlen(groupname) - 1] <= '9') 11040dad3f0eSMax Laier return (EINVAL); 11050dad3f0eSMax Laier 11060dad3f0eSMax Laier IFNET_WLOCK(); 11070dad3f0eSMax Laier TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 11080dad3f0eSMax Laier if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) { 11090dad3f0eSMax Laier IFNET_WUNLOCK(); 11100dad3f0eSMax Laier return (EEXIST); 11110dad3f0eSMax Laier } 11120dad3f0eSMax Laier 11130dad3f0eSMax Laier if ((ifgl = (struct ifg_list *)malloc(sizeof(struct ifg_list), M_TEMP, 11140dad3f0eSMax Laier M_NOWAIT)) == NULL) { 11150dad3f0eSMax Laier IFNET_WUNLOCK(); 11160dad3f0eSMax Laier return (ENOMEM); 11170dad3f0eSMax Laier } 11180dad3f0eSMax Laier 11190dad3f0eSMax Laier if ((ifgm = (struct ifg_member *)malloc(sizeof(struct ifg_member), 11200dad3f0eSMax Laier M_TEMP, M_NOWAIT)) == NULL) { 11210dad3f0eSMax Laier free(ifgl, M_TEMP); 11220dad3f0eSMax Laier IFNET_WUNLOCK(); 11230dad3f0eSMax Laier return (ENOMEM); 11240dad3f0eSMax Laier } 11250dad3f0eSMax Laier 1126603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 11270dad3f0eSMax Laier if (!strcmp(ifg->ifg_group, groupname)) 11280dad3f0eSMax Laier break; 11290dad3f0eSMax Laier 11300dad3f0eSMax Laier if (ifg == NULL) { 11310dad3f0eSMax Laier if ((ifg = (struct ifg_group *)malloc(sizeof(struct ifg_group), 11320dad3f0eSMax Laier M_TEMP, M_NOWAIT)) == NULL) { 11330dad3f0eSMax Laier free(ifgl, M_TEMP); 11340dad3f0eSMax Laier free(ifgm, M_TEMP); 11350dad3f0eSMax Laier IFNET_WUNLOCK(); 11360dad3f0eSMax Laier return (ENOMEM); 11370dad3f0eSMax Laier } 11380dad3f0eSMax Laier strlcpy(ifg->ifg_group, groupname, sizeof(ifg->ifg_group)); 11390dad3f0eSMax Laier ifg->ifg_refcnt = 0; 11400dad3f0eSMax Laier TAILQ_INIT(&ifg->ifg_members); 11410dad3f0eSMax Laier EVENTHANDLER_INVOKE(group_attach_event, ifg); 1142603724d3SBjoern A. Zeeb TAILQ_INSERT_TAIL(&V_ifg_head, ifg, ifg_next); 11430dad3f0eSMax Laier } 11440dad3f0eSMax Laier 11450dad3f0eSMax Laier ifg->ifg_refcnt++; 11460dad3f0eSMax Laier ifgl->ifgl_group = ifg; 11470dad3f0eSMax Laier ifgm->ifgm_ifp = ifp; 11480dad3f0eSMax Laier 11490dad3f0eSMax Laier IF_ADDR_LOCK(ifp); 11500dad3f0eSMax Laier TAILQ_INSERT_TAIL(&ifg->ifg_members, ifgm, ifgm_next); 11510dad3f0eSMax Laier TAILQ_INSERT_TAIL(&ifp->if_groups, ifgl, ifgl_next); 11520dad3f0eSMax Laier IF_ADDR_UNLOCK(ifp); 11530dad3f0eSMax Laier 11540dad3f0eSMax Laier IFNET_WUNLOCK(); 11550dad3f0eSMax Laier 11560dad3f0eSMax Laier EVENTHANDLER_INVOKE(group_change_event, groupname); 11570dad3f0eSMax Laier 11580dad3f0eSMax Laier return (0); 11590dad3f0eSMax Laier } 11600dad3f0eSMax Laier 11610dad3f0eSMax Laier /* 11620dad3f0eSMax Laier * Remove a group from an interface 11630dad3f0eSMax Laier */ 11640dad3f0eSMax Laier int 11650dad3f0eSMax Laier if_delgroup(struct ifnet *ifp, const char *groupname) 11660dad3f0eSMax Laier { 11670dad3f0eSMax Laier struct ifg_list *ifgl; 11680dad3f0eSMax Laier struct ifg_member *ifgm; 11690dad3f0eSMax Laier 11700dad3f0eSMax Laier IFNET_WLOCK(); 11710dad3f0eSMax Laier TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 11720dad3f0eSMax Laier if (!strcmp(ifgl->ifgl_group->ifg_group, groupname)) 11730dad3f0eSMax Laier break; 11740dad3f0eSMax Laier if (ifgl == NULL) { 11750dad3f0eSMax Laier IFNET_WUNLOCK(); 11760dad3f0eSMax Laier return (ENOENT); 11770dad3f0eSMax Laier } 11780dad3f0eSMax Laier 11790dad3f0eSMax Laier IF_ADDR_LOCK(ifp); 11800dad3f0eSMax Laier TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next); 11810dad3f0eSMax Laier IF_ADDR_UNLOCK(ifp); 11820dad3f0eSMax Laier 11830dad3f0eSMax Laier TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) 11840dad3f0eSMax Laier if (ifgm->ifgm_ifp == ifp) 11850dad3f0eSMax Laier break; 11860dad3f0eSMax Laier 11870dad3f0eSMax Laier if (ifgm != NULL) { 11880dad3f0eSMax Laier TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, ifgm_next); 11890dad3f0eSMax Laier free(ifgm, M_TEMP); 11900dad3f0eSMax Laier } 11910dad3f0eSMax Laier 11920dad3f0eSMax Laier if (--ifgl->ifgl_group->ifg_refcnt == 0) { 1193603724d3SBjoern A. Zeeb TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next); 11940dad3f0eSMax Laier EVENTHANDLER_INVOKE(group_detach_event, ifgl->ifgl_group); 11950dad3f0eSMax Laier free(ifgl->ifgl_group, M_TEMP); 11960dad3f0eSMax Laier } 11970dad3f0eSMax Laier IFNET_WUNLOCK(); 11980dad3f0eSMax Laier 11990dad3f0eSMax Laier free(ifgl, M_TEMP); 12000dad3f0eSMax Laier 12010dad3f0eSMax Laier EVENTHANDLER_INVOKE(group_change_event, groupname); 12020dad3f0eSMax Laier 12030dad3f0eSMax Laier return (0); 12040dad3f0eSMax Laier } 12050dad3f0eSMax Laier 12060dad3f0eSMax Laier /* 12078623f9fdSMax Laier * Remove an interface from all groups 12088623f9fdSMax Laier */ 12098623f9fdSMax Laier static void 12108623f9fdSMax Laier if_delgroups(struct ifnet *ifp) 12118623f9fdSMax Laier { 12128623f9fdSMax Laier struct ifg_list *ifgl; 12138623f9fdSMax Laier struct ifg_member *ifgm; 12148623f9fdSMax Laier char groupname[IFNAMSIZ]; 12158623f9fdSMax Laier 12168623f9fdSMax Laier IFNET_WLOCK(); 12178623f9fdSMax Laier while (!TAILQ_EMPTY(&ifp->if_groups)) { 12188623f9fdSMax Laier ifgl = TAILQ_FIRST(&ifp->if_groups); 12198623f9fdSMax Laier 12208623f9fdSMax Laier strlcpy(groupname, ifgl->ifgl_group->ifg_group, IFNAMSIZ); 12218623f9fdSMax Laier 12228623f9fdSMax Laier IF_ADDR_LOCK(ifp); 12238623f9fdSMax Laier TAILQ_REMOVE(&ifp->if_groups, ifgl, ifgl_next); 12248623f9fdSMax Laier IF_ADDR_UNLOCK(ifp); 12258623f9fdSMax Laier 12268623f9fdSMax Laier TAILQ_FOREACH(ifgm, &ifgl->ifgl_group->ifg_members, ifgm_next) 12278623f9fdSMax Laier if (ifgm->ifgm_ifp == ifp) 12288623f9fdSMax Laier break; 12298623f9fdSMax Laier 12308623f9fdSMax Laier if (ifgm != NULL) { 12318623f9fdSMax Laier TAILQ_REMOVE(&ifgl->ifgl_group->ifg_members, ifgm, 12328623f9fdSMax Laier ifgm_next); 12338623f9fdSMax Laier free(ifgm, M_TEMP); 12348623f9fdSMax Laier } 12358623f9fdSMax Laier 12368623f9fdSMax Laier if (--ifgl->ifgl_group->ifg_refcnt == 0) { 12378623f9fdSMax Laier TAILQ_REMOVE(&V_ifg_head, ifgl->ifgl_group, ifg_next); 12388623f9fdSMax Laier EVENTHANDLER_INVOKE(group_detach_event, 12398623f9fdSMax Laier ifgl->ifgl_group); 12408623f9fdSMax Laier free(ifgl->ifgl_group, M_TEMP); 12418623f9fdSMax Laier } 12428623f9fdSMax Laier IFNET_WUNLOCK(); 12438623f9fdSMax Laier 12448623f9fdSMax Laier free(ifgl, M_TEMP); 12458623f9fdSMax Laier 12468623f9fdSMax Laier EVENTHANDLER_INVOKE(group_change_event, groupname); 12478623f9fdSMax Laier 12488623f9fdSMax Laier IFNET_WLOCK(); 12498623f9fdSMax Laier } 12508623f9fdSMax Laier IFNET_WUNLOCK(); 12518623f9fdSMax Laier } 12528623f9fdSMax Laier 12538623f9fdSMax Laier /* 12540dad3f0eSMax Laier * Stores all groups from an interface in memory pointed 12550dad3f0eSMax Laier * to by data 12560dad3f0eSMax Laier */ 12570dad3f0eSMax Laier static int 12580dad3f0eSMax Laier if_getgroup(struct ifgroupreq *data, struct ifnet *ifp) 12590dad3f0eSMax Laier { 12600dad3f0eSMax Laier int len, error; 12610dad3f0eSMax Laier struct ifg_list *ifgl; 12620dad3f0eSMax Laier struct ifg_req ifgrq, *ifgp; 12630dad3f0eSMax Laier struct ifgroupreq *ifgr = data; 12640dad3f0eSMax Laier 12650dad3f0eSMax Laier if (ifgr->ifgr_len == 0) { 12660dad3f0eSMax Laier IF_ADDR_LOCK(ifp); 12670dad3f0eSMax Laier TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) 12680dad3f0eSMax Laier ifgr->ifgr_len += sizeof(struct ifg_req); 12690dad3f0eSMax Laier IF_ADDR_UNLOCK(ifp); 12700dad3f0eSMax Laier return (0); 12710dad3f0eSMax Laier } 12720dad3f0eSMax Laier 12730dad3f0eSMax Laier len = ifgr->ifgr_len; 12740dad3f0eSMax Laier ifgp = ifgr->ifgr_groups; 12750dad3f0eSMax Laier /* XXX: wire */ 12760dad3f0eSMax Laier IF_ADDR_LOCK(ifp); 12770dad3f0eSMax Laier TAILQ_FOREACH(ifgl, &ifp->if_groups, ifgl_next) { 12780dad3f0eSMax Laier if (len < sizeof(ifgrq)) { 12790dad3f0eSMax Laier IF_ADDR_UNLOCK(ifp); 12800dad3f0eSMax Laier return (EINVAL); 12810dad3f0eSMax Laier } 12820dad3f0eSMax Laier bzero(&ifgrq, sizeof ifgrq); 12830dad3f0eSMax Laier strlcpy(ifgrq.ifgrq_group, ifgl->ifgl_group->ifg_group, 12840dad3f0eSMax Laier sizeof(ifgrq.ifgrq_group)); 12850dad3f0eSMax Laier if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 12860dad3f0eSMax Laier IF_ADDR_UNLOCK(ifp); 12870dad3f0eSMax Laier return (error); 12880dad3f0eSMax Laier } 12890dad3f0eSMax Laier len -= sizeof(ifgrq); 12900dad3f0eSMax Laier ifgp++; 12910dad3f0eSMax Laier } 12920dad3f0eSMax Laier IF_ADDR_UNLOCK(ifp); 12930dad3f0eSMax Laier 12940dad3f0eSMax Laier return (0); 12950dad3f0eSMax Laier } 12960dad3f0eSMax Laier 12970dad3f0eSMax Laier /* 12980dad3f0eSMax Laier * Stores all members of a group in memory pointed to by data 12990dad3f0eSMax Laier */ 13000dad3f0eSMax Laier static int 13010dad3f0eSMax Laier if_getgroupmembers(struct ifgroupreq *data) 13020dad3f0eSMax Laier { 13030dad3f0eSMax Laier struct ifgroupreq *ifgr = data; 13040dad3f0eSMax Laier struct ifg_group *ifg; 13050dad3f0eSMax Laier struct ifg_member *ifgm; 13060dad3f0eSMax Laier struct ifg_req ifgrq, *ifgp; 13070dad3f0eSMax Laier int len, error; 13080dad3f0eSMax Laier 13090dad3f0eSMax Laier IFNET_RLOCK(); 1310603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifg, &V_ifg_head, ifg_next) 13110dad3f0eSMax Laier if (!strcmp(ifg->ifg_group, ifgr->ifgr_name)) 13120dad3f0eSMax Laier break; 13130dad3f0eSMax Laier if (ifg == NULL) { 13140dad3f0eSMax Laier IFNET_RUNLOCK(); 13150dad3f0eSMax Laier return (ENOENT); 13160dad3f0eSMax Laier } 13170dad3f0eSMax Laier 13180dad3f0eSMax Laier if (ifgr->ifgr_len == 0) { 13190dad3f0eSMax Laier TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) 13200dad3f0eSMax Laier ifgr->ifgr_len += sizeof(ifgrq); 13210dad3f0eSMax Laier IFNET_RUNLOCK(); 13220dad3f0eSMax Laier return (0); 13230dad3f0eSMax Laier } 13240dad3f0eSMax Laier 13250dad3f0eSMax Laier len = ifgr->ifgr_len; 13260dad3f0eSMax Laier ifgp = ifgr->ifgr_groups; 13270dad3f0eSMax Laier TAILQ_FOREACH(ifgm, &ifg->ifg_members, ifgm_next) { 13280dad3f0eSMax Laier if (len < sizeof(ifgrq)) { 13290dad3f0eSMax Laier IFNET_RUNLOCK(); 13300dad3f0eSMax Laier return (EINVAL); 13310dad3f0eSMax Laier } 13320dad3f0eSMax Laier bzero(&ifgrq, sizeof ifgrq); 13330dad3f0eSMax Laier strlcpy(ifgrq.ifgrq_member, ifgm->ifgm_ifp->if_xname, 13340dad3f0eSMax Laier sizeof(ifgrq.ifgrq_member)); 13350dad3f0eSMax Laier if ((error = copyout(&ifgrq, ifgp, sizeof(struct ifg_req)))) { 13360dad3f0eSMax Laier IFNET_RUNLOCK(); 13370dad3f0eSMax Laier return (error); 13380dad3f0eSMax Laier } 13390dad3f0eSMax Laier len -= sizeof(ifgrq); 13400dad3f0eSMax Laier ifgp++; 13410dad3f0eSMax Laier } 13420dad3f0eSMax Laier IFNET_RUNLOCK(); 13430dad3f0eSMax Laier 13440dad3f0eSMax Laier return (0); 13450dad3f0eSMax Laier } 13460dad3f0eSMax Laier 13470dad3f0eSMax Laier /* 13485500d3beSWarner Losh * Delete Routes for a Network Interface 13495500d3beSWarner Losh * 13505500d3beSWarner Losh * Called for each routing entry via the rnh->rnh_walktree() call above 13515500d3beSWarner Losh * to delete all route entries referencing a detaching network interface. 13525500d3beSWarner Losh * 13535500d3beSWarner Losh * Arguments: 13545500d3beSWarner Losh * rn pointer to node in the routing table 13555500d3beSWarner Losh * arg argument passed to rnh->rnh_walktree() - detaching interface 13565500d3beSWarner Losh * 13575500d3beSWarner Losh * Returns: 13585500d3beSWarner Losh * 0 successful 13595500d3beSWarner Losh * errno failed - reason indicated 13605500d3beSWarner Losh * 13615500d3beSWarner Losh */ 13625500d3beSWarner Losh static int 136372fd1b6aSDag-Erling Smørgrav if_rtdel(struct radix_node *rn, void *arg) 13645500d3beSWarner Losh { 13655500d3beSWarner Losh struct rtentry *rt = (struct rtentry *)rn; 13665500d3beSWarner Losh struct ifnet *ifp = arg; 13675500d3beSWarner Losh int err; 13685500d3beSWarner Losh 13695500d3beSWarner Losh if (rt->rt_ifp == ifp) { 13705500d3beSWarner Losh 13715500d3beSWarner Losh /* 13725500d3beSWarner Losh * Protect (sorta) against walktree recursion problems 13735500d3beSWarner Losh * with cloned routes 13745500d3beSWarner Losh */ 13755500d3beSWarner Losh if ((rt->rt_flags & RTF_UP) == 0) 13765500d3beSWarner Losh return (0); 13775500d3beSWarner Losh 13788b07e49aSJulian Elischer err = rtrequest_fib(RTM_DELETE, rt_key(rt), rt->rt_gateway, 13796241d13aSKip Macy rt_mask(rt), rt->rt_flags|RTF_RNH_LOCKED, 13808b07e49aSJulian Elischer (struct rtentry **) NULL, rt->rt_fibnum); 13815500d3beSWarner Losh if (err) { 13825500d3beSWarner Losh log(LOG_WARNING, "if_rtdel: error %d\n", err); 13835500d3beSWarner Losh } 13845500d3beSWarner Losh } 13855500d3beSWarner Losh 13865500d3beSWarner Losh return (0); 13876182fdbdSPeter Wemm } 13886182fdbdSPeter Wemm 138940d8a302SBruce M Simpson /* 1390f9ef96caSRobert Watson * Wrapper functions for struct ifnet address list locking macros. These are 1391f9ef96caSRobert Watson * used by kernel modules to avoid encoding programming interface or binary 1392f9ef96caSRobert Watson * interface assumptions that may be violated when kernel-internal locking 1393f9ef96caSRobert Watson * approaches change. 1394f9ef96caSRobert Watson */ 1395f9ef96caSRobert Watson void 1396f9ef96caSRobert Watson if_addr_rlock(struct ifnet *ifp) 1397f9ef96caSRobert Watson { 1398f9ef96caSRobert Watson 1399f9ef96caSRobert Watson IF_ADDR_LOCK(ifp); 1400f9ef96caSRobert Watson } 1401f9ef96caSRobert Watson 1402f9ef96caSRobert Watson void 1403f9ef96caSRobert Watson if_addr_runlock(struct ifnet *ifp) 1404f9ef96caSRobert Watson { 1405f9ef96caSRobert Watson 1406f9ef96caSRobert Watson IF_ADDR_UNLOCK(ifp); 1407f9ef96caSRobert Watson } 1408f9ef96caSRobert Watson 1409f9ef96caSRobert Watson void 1410f9ef96caSRobert Watson if_maddr_rlock(struct ifnet *ifp) 1411f9ef96caSRobert Watson { 1412f9ef96caSRobert Watson 1413f9ef96caSRobert Watson IF_ADDR_LOCK(ifp); 1414f9ef96caSRobert Watson } 1415f9ef96caSRobert Watson 1416f9ef96caSRobert Watson void 1417f9ef96caSRobert Watson if_maddr_runlock(struct ifnet *ifp) 1418f9ef96caSRobert Watson { 1419f9ef96caSRobert Watson 1420f9ef96caSRobert Watson IF_ADDR_UNLOCK(ifp); 1421f9ef96caSRobert Watson } 1422f9ef96caSRobert Watson 1423f9ef96caSRobert Watson /* 14241099f828SRobert Watson * Reference count functions for ifaddrs. 14251099f828SRobert Watson */ 14261099f828SRobert Watson void 14271099f828SRobert Watson ifa_init(struct ifaddr *ifa) 14281099f828SRobert Watson { 14291099f828SRobert Watson 14301099f828SRobert Watson mtx_init(&ifa->ifa_mtx, "ifaddr", NULL, MTX_DEF); 14311099f828SRobert Watson refcount_init(&ifa->ifa_refcnt, 1); 14321099f828SRobert Watson } 14331099f828SRobert Watson 14341099f828SRobert Watson void 14351099f828SRobert Watson ifa_ref(struct ifaddr *ifa) 14361099f828SRobert Watson { 14371099f828SRobert Watson 14381099f828SRobert Watson refcount_acquire(&ifa->ifa_refcnt); 14391099f828SRobert Watson } 14401099f828SRobert Watson 14411099f828SRobert Watson void 14421099f828SRobert Watson ifa_free(struct ifaddr *ifa) 14431099f828SRobert Watson { 14441099f828SRobert Watson 14451099f828SRobert Watson if (refcount_release(&ifa->ifa_refcnt)) { 14461099f828SRobert Watson mtx_destroy(&ifa->ifa_mtx); 14471099f828SRobert Watson free(ifa, M_IFADDR); 14481099f828SRobert Watson } 14491099f828SRobert Watson } 14501099f828SRobert Watson 14519bb7d0f4SQing Li int 14529bb7d0f4SQing Li ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia) 14539bb7d0f4SQing Li { 14549bb7d0f4SQing Li int error = 0; 14559bb7d0f4SQing Li struct rtentry *rt = NULL; 14569bb7d0f4SQing Li struct rt_addrinfo info; 14579bb7d0f4SQing Li static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 14589bb7d0f4SQing Li 14599bb7d0f4SQing Li bzero(&info, sizeof(info)); 14609bb7d0f4SQing Li info.rti_ifp = V_loif; 14619bb7d0f4SQing Li info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC; 14629bb7d0f4SQing Li info.rti_info[RTAX_DST] = ia; 14639bb7d0f4SQing Li info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl; 14649bb7d0f4SQing Li error = rtrequest1_fib(RTM_ADD, &info, &rt, 0); 14659bb7d0f4SQing Li 14669bb7d0f4SQing Li if (error == 0 && rt != NULL) { 14679bb7d0f4SQing Li RT_LOCK(rt); 14689bb7d0f4SQing Li ((struct sockaddr_dl *)rt->rt_gateway)->sdl_type = 146946e7f983SQing Li ifa->ifa_ifp->if_type; 14709bb7d0f4SQing Li ((struct sockaddr_dl *)rt->rt_gateway)->sdl_index = 147146e7f983SQing Li ifa->ifa_ifp->if_index; 14729bb7d0f4SQing Li RT_REMREF(rt); 14739bb7d0f4SQing Li RT_UNLOCK(rt); 14749bb7d0f4SQing Li } else if (error != 0) 14759bb7d0f4SQing Li log(LOG_INFO, "ifa_add_loopback_route: insertion failed\n"); 14769bb7d0f4SQing Li 14779bb7d0f4SQing Li return (error); 14789bb7d0f4SQing Li } 14799bb7d0f4SQing Li 14809bb7d0f4SQing Li int 14819bb7d0f4SQing Li ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia) 14829bb7d0f4SQing Li { 14839bb7d0f4SQing Li int error = 0; 14849bb7d0f4SQing Li struct rt_addrinfo info; 14859bb7d0f4SQing Li struct sockaddr_dl null_sdl; 14869bb7d0f4SQing Li 14879bb7d0f4SQing Li bzero(&null_sdl, sizeof(null_sdl)); 14889bb7d0f4SQing Li null_sdl.sdl_len = sizeof(null_sdl); 14899bb7d0f4SQing Li null_sdl.sdl_family = AF_LINK; 14909bb7d0f4SQing Li null_sdl.sdl_type = ifa->ifa_ifp->if_type; 14919bb7d0f4SQing Li null_sdl.sdl_index = ifa->ifa_ifp->if_index; 14929bb7d0f4SQing Li bzero(&info, sizeof(info)); 14939bb7d0f4SQing Li info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC; 14949bb7d0f4SQing Li info.rti_info[RTAX_DST] = ia; 14959bb7d0f4SQing Li info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl; 14969bb7d0f4SQing Li error = rtrequest1_fib(RTM_DELETE, &info, NULL, 0); 14979bb7d0f4SQing Li 14989bb7d0f4SQing Li if (error != 0) 14999bb7d0f4SQing Li log(LOG_INFO, "ifa_del_loopback_route: deletion failed\n"); 15009bb7d0f4SQing Li 15019bb7d0f4SQing Li return (error); 15029bb7d0f4SQing Li } 15039bb7d0f4SQing Li 15041099f828SRobert Watson /* 150540d8a302SBruce M Simpson * XXX: Because sockaddr_dl has deeper structure than the sockaddr 150640d8a302SBruce M Simpson * structs used to represent other address families, it is necessary 150740d8a302SBruce M Simpson * to perform a different comparison. 150840d8a302SBruce M Simpson */ 150940d8a302SBruce M Simpson 151040d8a302SBruce M Simpson #define sa_equal(a1, a2) \ 151140d8a302SBruce M Simpson (bcmp((a1), (a2), ((a1))->sa_len) == 0) 151240d8a302SBruce M Simpson 151340d8a302SBruce M Simpson #define sa_dl_equal(a1, a2) \ 151440d8a302SBruce M Simpson ((((struct sockaddr_dl *)(a1))->sdl_len == \ 151540d8a302SBruce M Simpson ((struct sockaddr_dl *)(a2))->sdl_len) && \ 151640d8a302SBruce M Simpson (bcmp(LLADDR((struct sockaddr_dl *)(a1)), \ 151740d8a302SBruce M Simpson LLADDR((struct sockaddr_dl *)(a2)), \ 151840d8a302SBruce M Simpson ((struct sockaddr_dl *)(a1))->sdl_alen) == 0)) 151919fc74fbSJeffrey Hsu 152030aad87dSBrooks Davis /* 1521df8bae1dSRodney W. Grimes * Locate an interface based on a complete address. 1522df8bae1dSRodney W. Grimes */ 1523df8bae1dSRodney W. Grimes /*ARGSUSED*/ 15248896f83aSRobert Watson static struct ifaddr * 15258c0fec80SRobert Watson ifa_ifwithaddr_internal(struct sockaddr *addr, int getref) 1526df8bae1dSRodney W. Grimes { 15270b59d917SJonathan Lemon struct ifnet *ifp; 15280b59d917SJonathan Lemon struct ifaddr *ifa; 1529df8bae1dSRodney W. Grimes 153077dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 1531ab5ed8a5SRobert Watson TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1532ab5ed8a5SRobert Watson IF_ADDR_LOCK(ifp); 153337d40066SPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1534df8bae1dSRodney W. Grimes if (ifa->ifa_addr->sa_family != addr->sa_family) 1535df8bae1dSRodney W. Grimes continue; 1536ab5ed8a5SRobert Watson if (sa_equal(addr, ifa->ifa_addr)) { 15378c0fec80SRobert Watson if (getref) 15388c0fec80SRobert Watson ifa_ref(ifa); 1539ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 15400b59d917SJonathan Lemon goto done; 1541ab5ed8a5SRobert Watson } 154282cd038dSYoshinobu Inoue /* IP6 doesn't have broadcast */ 15430b59d917SJonathan Lemon if ((ifp->if_flags & IFF_BROADCAST) && 15440b59d917SJonathan Lemon ifa->ifa_broadaddr && 154582cd038dSYoshinobu Inoue ifa->ifa_broadaddr->sa_len != 0 && 1546ab5ed8a5SRobert Watson sa_equal(ifa->ifa_broadaddr, addr)) { 15478c0fec80SRobert Watson if (getref) 15488c0fec80SRobert Watson ifa_ref(ifa); 1549ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 15500b59d917SJonathan Lemon goto done; 15510b59d917SJonathan Lemon } 1552ab5ed8a5SRobert Watson } 1553ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 1554ab5ed8a5SRobert Watson } 15550b59d917SJonathan Lemon ifa = NULL; 15560b59d917SJonathan Lemon done: 155777dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 1558df8bae1dSRodney W. Grimes return (ifa); 1559df8bae1dSRodney W. Grimes } 15600b59d917SJonathan Lemon 15618896f83aSRobert Watson struct ifaddr * 15628896f83aSRobert Watson ifa_ifwithaddr(struct sockaddr *addr) 15638896f83aSRobert Watson { 15648896f83aSRobert Watson 15658c0fec80SRobert Watson return (ifa_ifwithaddr_internal(addr, 1)); 15668896f83aSRobert Watson } 15678896f83aSRobert Watson 15688896f83aSRobert Watson int 15698896f83aSRobert Watson ifa_ifwithaddr_check(struct sockaddr *addr) 15708896f83aSRobert Watson { 15718896f83aSRobert Watson 15728c0fec80SRobert Watson return (ifa_ifwithaddr_internal(addr, 0) != NULL); 15738896f83aSRobert Watson } 15748896f83aSRobert Watson 1575df8bae1dSRodney W. Grimes /* 1576773725a2SAndre Oppermann * Locate an interface based on the broadcast address. 1577773725a2SAndre Oppermann */ 1578773725a2SAndre Oppermann /* ARGSUSED */ 1579773725a2SAndre Oppermann struct ifaddr * 1580773725a2SAndre Oppermann ifa_ifwithbroadaddr(struct sockaddr *addr) 1581773725a2SAndre Oppermann { 1582773725a2SAndre Oppermann struct ifnet *ifp; 1583773725a2SAndre Oppermann struct ifaddr *ifa; 1584773725a2SAndre Oppermann 158577dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 1586ab5ed8a5SRobert Watson TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1587ab5ed8a5SRobert Watson IF_ADDR_LOCK(ifp); 1588773725a2SAndre Oppermann TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1589773725a2SAndre Oppermann if (ifa->ifa_addr->sa_family != addr->sa_family) 1590773725a2SAndre Oppermann continue; 1591773725a2SAndre Oppermann if ((ifp->if_flags & IFF_BROADCAST) && 1592773725a2SAndre Oppermann ifa->ifa_broadaddr && 1593773725a2SAndre Oppermann ifa->ifa_broadaddr->sa_len != 0 && 1594ab5ed8a5SRobert Watson sa_equal(ifa->ifa_broadaddr, addr)) { 15958c0fec80SRobert Watson ifa_ref(ifa); 1596ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 1597773725a2SAndre Oppermann goto done; 1598773725a2SAndre Oppermann } 1599ab5ed8a5SRobert Watson } 1600ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 1601ab5ed8a5SRobert Watson } 1602773725a2SAndre Oppermann ifa = NULL; 1603773725a2SAndre Oppermann done: 160477dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 1605773725a2SAndre Oppermann return (ifa); 1606773725a2SAndre Oppermann } 1607773725a2SAndre Oppermann 1608773725a2SAndre Oppermann /* 1609df8bae1dSRodney W. Grimes * Locate the point to point interface with a given destination address. 1610df8bae1dSRodney W. Grimes */ 1611df8bae1dSRodney W. Grimes /*ARGSUSED*/ 1612df8bae1dSRodney W. Grimes struct ifaddr * 161372fd1b6aSDag-Erling Smørgrav ifa_ifwithdstaddr(struct sockaddr *addr) 1614df8bae1dSRodney W. Grimes { 16150b59d917SJonathan Lemon struct ifnet *ifp; 16160b59d917SJonathan Lemon struct ifaddr *ifa; 1617df8bae1dSRodney W. Grimes 161877dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 1619603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 16200b59d917SJonathan Lemon if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 16210b59d917SJonathan Lemon continue; 1622ab5ed8a5SRobert Watson IF_ADDR_LOCK(ifp); 162337d40066SPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1624df8bae1dSRodney W. Grimes if (ifa->ifa_addr->sa_family != addr->sa_family) 1625df8bae1dSRodney W. Grimes continue; 1626f0c04221SBjoern A. Zeeb if (ifa->ifa_dstaddr != NULL && 1627ab5ed8a5SRobert Watson sa_equal(addr, ifa->ifa_dstaddr)) { 16288c0fec80SRobert Watson ifa_ref(ifa); 1629ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 16300b59d917SJonathan Lemon goto done; 1631df8bae1dSRodney W. Grimes } 16320b59d917SJonathan Lemon } 1633ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 1634ab5ed8a5SRobert Watson } 16350b59d917SJonathan Lemon ifa = NULL; 16360b59d917SJonathan Lemon done: 163777dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 16380b59d917SJonathan Lemon return (ifa); 1639df8bae1dSRodney W. Grimes } 1640df8bae1dSRodney W. Grimes 1641df8bae1dSRodney W. Grimes /* 1642df8bae1dSRodney W. Grimes * Find an interface on a specific network. If many, choice 1643df8bae1dSRodney W. Grimes * is most specific found. 1644df8bae1dSRodney W. Grimes */ 1645df8bae1dSRodney W. Grimes struct ifaddr * 16460ed6142bSQing Li ifa_ifwithnet(struct sockaddr *addr, int ignore_ptp) 1647df8bae1dSRodney W. Grimes { 164872fd1b6aSDag-Erling Smørgrav struct ifnet *ifp; 164972fd1b6aSDag-Erling Smørgrav struct ifaddr *ifa; 16508c0fec80SRobert Watson struct ifaddr *ifa_maybe = NULL; 1651df8bae1dSRodney W. Grimes u_int af = addr->sa_family; 1652df8bae1dSRodney W. Grimes char *addr_data = addr->sa_data, *cplim; 1653df8bae1dSRodney W. Grimes 16547e2a6151SJulian Elischer /* 16557e2a6151SJulian Elischer * AF_LINK addresses can be looked up directly by their index number, 16567e2a6151SJulian Elischer * so do that if we can. 16577e2a6151SJulian Elischer */ 1658df8bae1dSRodney W. Grimes if (af == AF_LINK) { 1659d1dd20beSSam Leffler struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr; 1660603724d3SBjoern A. Zeeb if (sdl->sdl_index && sdl->sdl_index <= V_if_index) 1661f9132cebSJonathan Lemon return (ifaddr_byindex(sdl->sdl_index)); 1662df8bae1dSRodney W. Grimes } 16637e2a6151SJulian Elischer 16647e2a6151SJulian Elischer /* 16658c0fec80SRobert Watson * Scan though each interface, looking for ones that have addresses 16668c0fec80SRobert Watson * in this address family. Maintain a reference on ifa_maybe once 16678c0fec80SRobert Watson * we find one, as we release the IF_ADDR_LOCK() that kept it stable 16688c0fec80SRobert Watson * when we move onto the next interface. 16697e2a6151SJulian Elischer */ 167077dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 1671603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1672ab5ed8a5SRobert Watson IF_ADDR_LOCK(ifp); 167337d40066SPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 167472fd1b6aSDag-Erling Smørgrav char *cp, *cp2, *cp3; 1675df8bae1dSRodney W. Grimes 1676523a02aaSDavid Greenman if (ifa->ifa_addr->sa_family != af) 1677df8bae1dSRodney W. Grimes next: continue; 16780ed6142bSQing Li if (af == AF_INET && 16790ed6142bSQing Li ifp->if_flags & IFF_POINTOPOINT && !ignore_ptp) { 16807e2a6151SJulian Elischer /* 16817e2a6151SJulian Elischer * This is a bit broken as it doesn't 16827e2a6151SJulian Elischer * take into account that the remote end may 16837e2a6151SJulian Elischer * be a single node in the network we are 16847e2a6151SJulian Elischer * looking for. 16857e2a6151SJulian Elischer * The trouble is that we don't know the 16867e2a6151SJulian Elischer * netmask for the remote end. 16877e2a6151SJulian Elischer */ 1688f0c04221SBjoern A. Zeeb if (ifa->ifa_dstaddr != NULL && 1689ab5ed8a5SRobert Watson sa_equal(addr, ifa->ifa_dstaddr)) { 16908c0fec80SRobert Watson ifa_ref(ifa); 1691ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 16920b59d917SJonathan Lemon goto done; 1693ab5ed8a5SRobert Watson } 16943740e2adSDavid Greenman } else { 16957e2a6151SJulian Elischer /* 16967ed8f465SJulian Elischer * if we have a special address handler, 16977ed8f465SJulian Elischer * then use it instead of the generic one. 16987ed8f465SJulian Elischer */ 16997ed8f465SJulian Elischer if (ifa->ifa_claim_addr) { 1700ab5ed8a5SRobert Watson if ((*ifa->ifa_claim_addr)(ifa, addr)) { 17018c0fec80SRobert Watson ifa_ref(ifa); 1702ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 17030b59d917SJonathan Lemon goto done; 1704ab5ed8a5SRobert Watson } 17057ed8f465SJulian Elischer continue; 17067ed8f465SJulian Elischer } 17077ed8f465SJulian Elischer 17087ed8f465SJulian Elischer /* 17097e2a6151SJulian Elischer * Scan all the bits in the ifa's address. 17107e2a6151SJulian Elischer * If a bit dissagrees with what we are 17117e2a6151SJulian Elischer * looking for, mask it with the netmask 17127e2a6151SJulian Elischer * to see if it really matters. 17137e2a6151SJulian Elischer * (A byte at a time) 17147e2a6151SJulian Elischer */ 1715523a02aaSDavid Greenman if (ifa->ifa_netmask == 0) 1716523a02aaSDavid Greenman continue; 1717df8bae1dSRodney W. Grimes cp = addr_data; 1718df8bae1dSRodney W. Grimes cp2 = ifa->ifa_addr->sa_data; 1719df8bae1dSRodney W. Grimes cp3 = ifa->ifa_netmask->sa_data; 17207e2a6151SJulian Elischer cplim = ifa->ifa_netmask->sa_len 17217e2a6151SJulian Elischer + (char *)ifa->ifa_netmask; 1722df8bae1dSRodney W. Grimes while (cp3 < cplim) 1723df8bae1dSRodney W. Grimes if ((*cp++ ^ *cp2++) & *cp3++) 17247e2a6151SJulian Elischer goto next; /* next address! */ 17257e2a6151SJulian Elischer /* 17267e2a6151SJulian Elischer * If the netmask of what we just found 17277e2a6151SJulian Elischer * is more specific than what we had before 17287e2a6151SJulian Elischer * (if we had one) then remember the new one 17297e2a6151SJulian Elischer * before continuing to search 17307e2a6151SJulian Elischer * for an even better one. 17317e2a6151SJulian Elischer */ 17328c0fec80SRobert Watson if (ifa_maybe == NULL || 1733df8bae1dSRodney W. Grimes rn_refines((caddr_t)ifa->ifa_netmask, 17348c0fec80SRobert Watson (caddr_t)ifa_maybe->ifa_netmask)) { 17358c0fec80SRobert Watson if (ifa_maybe != NULL) 17368c0fec80SRobert Watson ifa_free(ifa_maybe); 1737df8bae1dSRodney W. Grimes ifa_maybe = ifa; 17388c0fec80SRobert Watson ifa_ref(ifa_maybe); 17398c0fec80SRobert Watson } 1740df8bae1dSRodney W. Grimes } 1741b2af64fdSDavid Greenman } 1742ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 1743b2af64fdSDavid Greenman } 17440b59d917SJonathan Lemon ifa = ifa_maybe; 17458c0fec80SRobert Watson ifa_maybe = NULL; 17460b59d917SJonathan Lemon done: 174777dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 17488c0fec80SRobert Watson if (ifa_maybe != NULL) 17498c0fec80SRobert Watson ifa_free(ifa_maybe); 17500b59d917SJonathan Lemon return (ifa); 1751df8bae1dSRodney W. Grimes } 1752df8bae1dSRodney W. Grimes 1753df8bae1dSRodney W. Grimes /* 1754df8bae1dSRodney W. Grimes * Find an interface address specific to an interface best matching 1755df8bae1dSRodney W. Grimes * a given address. 1756df8bae1dSRodney W. Grimes */ 1757df8bae1dSRodney W. Grimes struct ifaddr * 175872fd1b6aSDag-Erling Smørgrav ifaof_ifpforaddr(struct sockaddr *addr, struct ifnet *ifp) 1759df8bae1dSRodney W. Grimes { 176072fd1b6aSDag-Erling Smørgrav struct ifaddr *ifa; 176172fd1b6aSDag-Erling Smørgrav char *cp, *cp2, *cp3; 176272fd1b6aSDag-Erling Smørgrav char *cplim; 17638c0fec80SRobert Watson struct ifaddr *ifa_maybe = NULL; 1764df8bae1dSRodney W. Grimes u_int af = addr->sa_family; 1765df8bae1dSRodney W. Grimes 1766df8bae1dSRodney W. Grimes if (af >= AF_MAX) 1767cd292f12SBjoern A. Zeeb return (NULL); 1768ab5ed8a5SRobert Watson IF_ADDR_LOCK(ifp); 176937d40066SPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1770df8bae1dSRodney W. Grimes if (ifa->ifa_addr->sa_family != af) 1771df8bae1dSRodney W. Grimes continue; 17728c0fec80SRobert Watson if (ifa_maybe == NULL) 1773df8bae1dSRodney W. Grimes ifa_maybe = ifa; 1774df8bae1dSRodney W. Grimes if (ifa->ifa_netmask == 0) { 1775d8d5b10eSRobert Watson if (sa_equal(addr, ifa->ifa_addr) || 1776d8d5b10eSRobert Watson (ifa->ifa_dstaddr && 1777d8d5b10eSRobert Watson sa_equal(addr, ifa->ifa_dstaddr))) 17782defe5cdSJonathan Lemon goto done; 1779df8bae1dSRodney W. Grimes continue; 1780df8bae1dSRodney W. Grimes } 1781b2af64fdSDavid Greenman if (ifp->if_flags & IFF_POINTOPOINT) { 1782d8d5b10eSRobert Watson if (sa_equal(addr, ifa->ifa_dstaddr)) 1783a8637146SJonathan Lemon goto done; 17843740e2adSDavid Greenman } else { 1785df8bae1dSRodney W. Grimes cp = addr->sa_data; 1786df8bae1dSRodney W. Grimes cp2 = ifa->ifa_addr->sa_data; 1787df8bae1dSRodney W. Grimes cp3 = ifa->ifa_netmask->sa_data; 1788df8bae1dSRodney W. Grimes cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask; 1789df8bae1dSRodney W. Grimes for (; cp3 < cplim; cp3++) 1790df8bae1dSRodney W. Grimes if ((*cp++ ^ *cp2++) & *cp3) 1791df8bae1dSRodney W. Grimes break; 1792df8bae1dSRodney W. Grimes if (cp3 == cplim) 17932defe5cdSJonathan Lemon goto done; 1794df8bae1dSRodney W. Grimes } 1795b2af64fdSDavid Greenman } 1796f9132cebSJonathan Lemon ifa = ifa_maybe; 1797f9132cebSJonathan Lemon done: 17988c0fec80SRobert Watson if (ifa != NULL) 17998c0fec80SRobert Watson ifa_ref(ifa); 1800ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 1801f9132cebSJonathan Lemon return (ifa); 1802df8bae1dSRodney W. Grimes } 1803df8bae1dSRodney W. Grimes 18046e6b3f7cSQing Li #include <net/if_llatbl.h> 18056e6b3f7cSQing Li 1806df8bae1dSRodney W. Grimes /* 1807df8bae1dSRodney W. Grimes * Default action when installing a route with a Link Level gateway. 1808df8bae1dSRodney W. Grimes * Lookup an appropriate real ifa to point to. 1809df8bae1dSRodney W. Grimes * This should be moved to /sys/net/link.c eventually. 1810df8bae1dSRodney W. Grimes */ 18113bda9f9bSPoul-Henning Kamp static void 181272fd1b6aSDag-Erling Smørgrav link_rtrequest(int cmd, struct rtentry *rt, struct rt_addrinfo *info) 1813df8bae1dSRodney W. Grimes { 181472fd1b6aSDag-Erling Smørgrav struct ifaddr *ifa, *oifa; 1815df8bae1dSRodney W. Grimes struct sockaddr *dst; 1816df8bae1dSRodney W. Grimes struct ifnet *ifp; 1817df8bae1dSRodney W. Grimes 1818d1dd20beSSam Leffler RT_LOCK_ASSERT(rt); 1819d1dd20beSSam Leffler 1820df8bae1dSRodney W. Grimes if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) || 1821df8bae1dSRodney W. Grimes ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0)) 1822df8bae1dSRodney W. Grimes return; 18239448326fSPoul-Henning Kamp ifa = ifaof_ifpforaddr(dst, ifp); 18249448326fSPoul-Henning Kamp if (ifa) { 1825d1dd20beSSam Leffler oifa = rt->rt_ifa; 1826df8bae1dSRodney W. Grimes rt->rt_ifa = ifa; 18271099f828SRobert Watson ifa_free(oifa); 1828df8bae1dSRodney W. Grimes if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest) 18298071913dSRuslan Ermilov ifa->ifa_rtrequest(cmd, rt, info); 1830df8bae1dSRodney W. Grimes } 1831df8bae1dSRodney W. Grimes } 1832df8bae1dSRodney W. Grimes 1833df8bae1dSRodney W. Grimes /* 1834df8bae1dSRodney W. Grimes * Mark an interface down and notify protocols of 1835df8bae1dSRodney W. Grimes * the transition. 1836df8bae1dSRodney W. Grimes * NOTE: must be called at splnet or eqivalent. 1837df8bae1dSRodney W. Grimes */ 18388614fb12SMax Laier static void 183972fd1b6aSDag-Erling Smørgrav if_unroute(struct ifnet *ifp, int flag, int fam) 1840df8bae1dSRodney W. Grimes { 184172fd1b6aSDag-Erling Smørgrav struct ifaddr *ifa; 1842df8bae1dSRodney W. Grimes 1843292ee7beSRobert Watson KASSERT(flag == IFF_UP, ("if_unroute: flag != IFF_UP")); 1844292ee7beSRobert Watson 1845e8c2601dSPoul-Henning Kamp ifp->if_flags &= ~flag; 184698b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 1847e8c2601dSPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1848e8c2601dSPoul-Henning Kamp if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1849df8bae1dSRodney W. Grimes pfctlinput(PRC_IFDOWN, ifa->ifa_addr); 1850db7f0b97SKip Macy ifp->if_qflush(ifp); 1851db7f0b97SKip Macy 1852a9771948SGleb Smirnoff if (ifp->if_carp) 185354bfbd51SWill Andrews (*carp_linkstate_p)(ifp); 1854df8bae1dSRodney W. Grimes rt_ifmsg(ifp); 1855df8bae1dSRodney W. Grimes } 1856df8bae1dSRodney W. Grimes 1857df8bae1dSRodney W. Grimes /* 1858df8bae1dSRodney W. Grimes * Mark an interface up and notify protocols of 1859df8bae1dSRodney W. Grimes * the transition. 1860df8bae1dSRodney W. Grimes * NOTE: must be called at splnet or eqivalent. 1861df8bae1dSRodney W. Grimes */ 18628614fb12SMax Laier static void 186372fd1b6aSDag-Erling Smørgrav if_route(struct ifnet *ifp, int flag, int fam) 1864df8bae1dSRodney W. Grimes { 186572fd1b6aSDag-Erling Smørgrav struct ifaddr *ifa; 1866df8bae1dSRodney W. Grimes 1867292ee7beSRobert Watson KASSERT(flag == IFF_UP, ("if_route: flag != IFF_UP")); 1868292ee7beSRobert Watson 1869e8c2601dSPoul-Henning Kamp ifp->if_flags |= flag; 187098b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 1871e8c2601dSPoul-Henning Kamp TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1872e8c2601dSPoul-Henning Kamp if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family)) 1873df8bae1dSRodney W. Grimes pfctlinput(PRC_IFUP, ifa->ifa_addr); 1874a9771948SGleb Smirnoff if (ifp->if_carp) 187554bfbd51SWill Andrews (*carp_linkstate_p)(ifp); 1876df8bae1dSRodney W. Grimes rt_ifmsg(ifp); 187782cd038dSYoshinobu Inoue #ifdef INET6 187882cd038dSYoshinobu Inoue in6_if_up(ifp); 187982cd038dSYoshinobu Inoue #endif 1880df8bae1dSRodney W. Grimes } 1881df8bae1dSRodney W. Grimes 1882a6fffd6cSBrooks Davis void (*vlan_link_state_p)(struct ifnet *); /* XXX: private from if_vlan */ 188375ee267cSGleb Smirnoff void (*vlan_trunk_cap_p)(struct ifnet *); /* XXX: private from if_vlan */ 1884*e4cd31ddSJeff Roberson struct ifnet *(*vlan_trunkdev_p)(struct ifnet *); 1885*e4cd31ddSJeff Roberson struct ifnet *(*vlan_devat_p)(struct ifnet *, uint16_t); 1886*e4cd31ddSJeff Roberson int (*vlan_tag_p)(struct ifnet *, uint16_t *); 1887*e4cd31ddSJeff Roberson int (*vlan_setcookie_p)(struct ifnet *, void *); 1888*e4cd31ddSJeff Roberson void *(*vlan_cookie_p)(struct ifnet *); 188994f5c9cfSSam Leffler 189094f5c9cfSSam Leffler /* 189168a3482fSGleb Smirnoff * Handle a change in the interface link state. To avoid LORs 189268a3482fSGleb Smirnoff * between driver lock and upper layer locks, as well as possible 189368a3482fSGleb Smirnoff * recursions, we post event to taskqueue, and all job 189468a3482fSGleb Smirnoff * is done in static do_link_state_change(). 189594f5c9cfSSam Leffler */ 189694f5c9cfSSam Leffler void 189794f5c9cfSSam Leffler if_link_state_change(struct ifnet *ifp, int link_state) 189894f5c9cfSSam Leffler { 18994d96314fSGleb Smirnoff /* Return if state hasn't changed. */ 19004d96314fSGleb Smirnoff if (ifp->if_link_state == link_state) 19014d96314fSGleb Smirnoff return; 19024d96314fSGleb Smirnoff 190394f5c9cfSSam Leffler ifp->if_link_state = link_state; 19044d96314fSGleb Smirnoff 190568a3482fSGleb Smirnoff taskqueue_enqueue(taskqueue_swi, &ifp->if_linktask); 190668a3482fSGleb Smirnoff } 190768a3482fSGleb Smirnoff 190868a3482fSGleb Smirnoff static void 190968a3482fSGleb Smirnoff do_link_state_change(void *arg, int pending) 191068a3482fSGleb Smirnoff { 191168a3482fSGleb Smirnoff struct ifnet *ifp = (struct ifnet *)arg; 191268a3482fSGleb Smirnoff int link_state = ifp->if_link_state; 19138b615593SMarko Zec CURVNET_SET(ifp->if_vnet); 191468a3482fSGleb Smirnoff 19154d96314fSGleb Smirnoff /* Notify that the link state has changed. */ 191694f5c9cfSSam Leffler rt_ifmsg(ifp); 191775ee267cSGleb Smirnoff if (ifp->if_vlantrunk != NULL) 1918a6fffd6cSBrooks Davis (*vlan_link_state_p)(ifp); 19191c7899c7SGleb Smirnoff 19201c7899c7SGleb Smirnoff if ((ifp->if_type == IFT_ETHER || ifp->if_type == IFT_L2VLAN) && 19211c7899c7SGleb Smirnoff IFP2AC(ifp)->ac_netgraph != NULL) 19221c7899c7SGleb Smirnoff (*ng_ether_link_state_p)(ifp, link_state); 19234d96314fSGleb Smirnoff if (ifp->if_carp) 192454bfbd51SWill Andrews (*carp_linkstate_p)(ifp); 19258f867517SAndrew Thompson if (ifp->if_bridge) { 19268f867517SAndrew Thompson KASSERT(bstp_linkstate_p != NULL,("if_bridge bstp not loaded!")); 19278f867517SAndrew Thompson (*bstp_linkstate_p)(ifp, link_state); 19288f867517SAndrew Thompson } 192918242d3bSAndrew Thompson if (ifp->if_lagg) { 193018242d3bSAndrew Thompson KASSERT(lagg_linkstate_p != NULL,("if_lagg not loaded!")); 193118242d3bSAndrew Thompson (*lagg_linkstate_p)(ifp, link_state); 1932b47888ceSAndrew Thompson } 19338f867517SAndrew Thompson 193421ca7b57SMarko Zec if (IS_DEFAULT_VNET(curvnet)) 19359d80a330SBrooks Davis devctl_notify("IFNET", ifp->if_xname, 193621ca7b57SMarko Zec (link_state == LINK_STATE_UP) ? "LINK_UP" : "LINK_DOWN", 193721ca7b57SMarko Zec NULL); 193868a3482fSGleb Smirnoff if (pending > 1) 193968a3482fSGleb Smirnoff if_printf(ifp, "%d link states coalesced\n", pending); 19405515c2e7SGleb Smirnoff if (log_link_state_change) 19418b02df24SGleb Smirnoff log(LOG_NOTICE, "%s: link state changed to %s\n", ifp->if_xname, 19428b02df24SGleb Smirnoff (link_state == LINK_STATE_UP) ? "UP" : "DOWN" ); 1943*e4cd31ddSJeff Roberson EVENTHANDLER_INVOKE(ifnet_link_event, ifp, ifp->if_link_state); 19448b615593SMarko Zec CURVNET_RESTORE(); 194594f5c9cfSSam Leffler } 194694f5c9cfSSam Leffler 1947df8bae1dSRodney W. Grimes /* 1948e8c2601dSPoul-Henning Kamp * Mark an interface down and notify protocols of 1949e8c2601dSPoul-Henning Kamp * the transition. 1950e8c2601dSPoul-Henning Kamp * NOTE: must be called at splnet or eqivalent. 1951e8c2601dSPoul-Henning Kamp */ 1952e8c2601dSPoul-Henning Kamp void 195372fd1b6aSDag-Erling Smørgrav if_down(struct ifnet *ifp) 1954e8c2601dSPoul-Henning Kamp { 1955e8c2601dSPoul-Henning Kamp 1956e8c2601dSPoul-Henning Kamp if_unroute(ifp, IFF_UP, AF_UNSPEC); 1957e8c2601dSPoul-Henning Kamp } 1958e8c2601dSPoul-Henning Kamp 1959e8c2601dSPoul-Henning Kamp /* 1960e8c2601dSPoul-Henning Kamp * Mark an interface up and notify protocols of 1961e8c2601dSPoul-Henning Kamp * the transition. 1962e8c2601dSPoul-Henning Kamp * NOTE: must be called at splnet or eqivalent. 1963e8c2601dSPoul-Henning Kamp */ 1964e8c2601dSPoul-Henning Kamp void 196572fd1b6aSDag-Erling Smørgrav if_up(struct ifnet *ifp) 1966e8c2601dSPoul-Henning Kamp { 1967e8c2601dSPoul-Henning Kamp 1968e8c2601dSPoul-Henning Kamp if_route(ifp, IFF_UP, AF_UNSPEC); 1969e8c2601dSPoul-Henning Kamp } 1970e8c2601dSPoul-Henning Kamp 1971e8c2601dSPoul-Henning Kamp /* 1972df8bae1dSRodney W. Grimes * Flush an interface queue. 1973df8bae1dSRodney W. Grimes */ 19747cc5b47fSKip Macy void 1975db7f0b97SKip Macy if_qflush(struct ifnet *ifp) 1976df8bae1dSRodney W. Grimes { 197772fd1b6aSDag-Erling Smørgrav struct mbuf *m, *n; 1978db7f0b97SKip Macy struct ifaltq *ifq; 1979df8bae1dSRodney W. Grimes 1980db7f0b97SKip Macy ifq = &ifp->if_snd; 19817b21048cSMax Laier IFQ_LOCK(ifq); 198202b199f1SMax Laier #ifdef ALTQ 198302b199f1SMax Laier if (ALTQ_IS_ENABLED(ifq)) 198402b199f1SMax Laier ALTQ_PURGE(ifq); 198502b199f1SMax Laier #endif 1986df8bae1dSRodney W. Grimes n = ifq->ifq_head; 19879448326fSPoul-Henning Kamp while ((m = n) != 0) { 1988df8bae1dSRodney W. Grimes n = m->m_act; 1989df8bae1dSRodney W. Grimes m_freem(m); 1990df8bae1dSRodney W. Grimes } 1991df8bae1dSRodney W. Grimes ifq->ifq_head = 0; 1992df8bae1dSRodney W. Grimes ifq->ifq_tail = 0; 1993df8bae1dSRodney W. Grimes ifq->ifq_len = 0; 19947b21048cSMax Laier IFQ_UNLOCK(ifq); 1995df8bae1dSRodney W. Grimes } 1996df8bae1dSRodney W. Grimes 1997df8bae1dSRodney W. Grimes /* 19986064c5d3SRobert Watson * Map interface name to interface structure pointer, with or without 19996064c5d3SRobert Watson * returning a reference. 2000df8bae1dSRodney W. Grimes */ 2001df8bae1dSRodney W. Grimes struct ifnet * 20026064c5d3SRobert Watson ifunit_ref(const char *name) 20036064c5d3SRobert Watson { 20046064c5d3SRobert Watson struct ifnet *ifp; 20056064c5d3SRobert Watson 200677dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 20076064c5d3SRobert Watson TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 20088bd015a1SRobert Watson if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0 && 20098bd015a1SRobert Watson !(ifp->if_flags & IFF_DYING)) 20106064c5d3SRobert Watson break; 20116064c5d3SRobert Watson } 20126064c5d3SRobert Watson if (ifp != NULL) 20136064c5d3SRobert Watson if_ref(ifp); 201477dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 20156064c5d3SRobert Watson return (ifp); 20166064c5d3SRobert Watson } 20176064c5d3SRobert Watson 20186064c5d3SRobert Watson struct ifnet * 201930aad87dSBrooks Davis ifunit(const char *name) 2020df8bae1dSRodney W. Grimes { 20218b7805e4SBoris Popov struct ifnet *ifp; 2022df8bae1dSRodney W. Grimes 202377dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 2024603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 202536c19a57SBrooks Davis if (strncmp(name, ifp->if_xname, IFNAMSIZ) == 0) 2026df8bae1dSRodney W. Grimes break; 2027df8bae1dSRodney W. Grimes } 202877dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 2029df8bae1dSRodney W. Grimes return (ifp); 2030df8bae1dSRodney W. Grimes } 2031df8bae1dSRodney W. Grimes 203282cd038dSYoshinobu Inoue /* 2033f13ad206SJonathan Lemon * Hardware specific interface ioctls. 2034df8bae1dSRodney W. Grimes */ 2035f13ad206SJonathan Lemon static int 2036f13ad206SJonathan Lemon ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td) 2037df8bae1dSRodney W. Grimes { 2038f13ad206SJonathan Lemon struct ifreq *ifr; 2039413dd0baSPoul-Henning Kamp struct ifstat *ifs; 2040f13ad206SJonathan Lemon int error = 0; 2041292ee7beSRobert Watson int new_flags, temp_flags; 204236c19a57SBrooks Davis size_t namelen, onamelen; 2043215940b3SXin LI size_t descrlen; 2044215940b3SXin LI char *descrbuf, *odescrbuf; 204536c19a57SBrooks Davis char new_name[IFNAMSIZ]; 204636c19a57SBrooks Davis struct ifaddr *ifa; 204736c19a57SBrooks Davis struct sockaddr_dl *sdl; 2048df8bae1dSRodney W. Grimes 2049df8bae1dSRodney W. Grimes ifr = (struct ifreq *)data; 205030aad87dSBrooks Davis switch (cmd) { 2051de593450SJonathan Lemon case SIOCGIFINDEX: 2052de593450SJonathan Lemon ifr->ifr_index = ifp->if_index; 2053de593450SJonathan Lemon break; 2054de593450SJonathan Lemon 2055df8bae1dSRodney W. Grimes case SIOCGIFFLAGS: 2056292ee7beSRobert Watson temp_flags = ifp->if_flags | ifp->if_drv_flags; 2057292ee7beSRobert Watson ifr->ifr_flags = temp_flags & 0xffff; 2058292ee7beSRobert Watson ifr->ifr_flagshigh = temp_flags >> 16; 2059df8bae1dSRodney W. Grimes break; 2060df8bae1dSRodney W. Grimes 2061016da741SJonathan Lemon case SIOCGIFCAP: 2062016da741SJonathan Lemon ifr->ifr_reqcap = ifp->if_capabilities; 2063016da741SJonathan Lemon ifr->ifr_curcap = ifp->if_capenable; 2064016da741SJonathan Lemon break; 2065016da741SJonathan Lemon 20668f293a63SRobert Watson #ifdef MAC 20678f293a63SRobert Watson case SIOCGIFMAC: 206830d239bcSRobert Watson error = mac_ifnet_ioctl_get(td->td_ucred, ifr, ifp); 20698f293a63SRobert Watson break; 20708f293a63SRobert Watson #endif 20718f293a63SRobert Watson 2072df8bae1dSRodney W. Grimes case SIOCGIFMETRIC: 2073df8bae1dSRodney W. Grimes ifr->ifr_metric = ifp->if_metric; 2074df8bae1dSRodney W. Grimes break; 2075df8bae1dSRodney W. Grimes 2076a7028af7SDavid Greenman case SIOCGIFMTU: 2077a7028af7SDavid Greenman ifr->ifr_mtu = ifp->if_mtu; 2078a7028af7SDavid Greenman break; 2079a7028af7SDavid Greenman 2080074c4a4eSGarrett Wollman case SIOCGIFPHYS: 2081074c4a4eSGarrett Wollman ifr->ifr_phys = ifp->if_physical; 2082074c4a4eSGarrett Wollman break; 2083074c4a4eSGarrett Wollman 2084215940b3SXin LI case SIOCGIFDESCR: 2085215940b3SXin LI error = 0; 2086215940b3SXin LI sx_slock(&ifdescr_sx); 208757d84848SXin LI if (ifp->if_description == NULL) 2088215940b3SXin LI error = ENOMSG; 208957d84848SXin LI else { 2090215940b3SXin LI /* space for terminating nul */ 2091215940b3SXin LI descrlen = strlen(ifp->if_description) + 1; 2092215940b3SXin LI if (ifr->ifr_buffer.length < descrlen) 209357d84848SXin LI ifr->ifr_buffer.buffer = NULL; 2094215940b3SXin LI else 2095215940b3SXin LI error = copyout(ifp->if_description, 2096215940b3SXin LI ifr->ifr_buffer.buffer, descrlen); 2097215940b3SXin LI ifr->ifr_buffer.length = descrlen; 2098215940b3SXin LI } 2099215940b3SXin LI sx_sunlock(&ifdescr_sx); 2100215940b3SXin LI break; 2101215940b3SXin LI 2102215940b3SXin LI case SIOCSIFDESCR: 2103215940b3SXin LI error = priv_check(td, PRIV_NET_SETIFDESCR); 2104215940b3SXin LI if (error) 2105215940b3SXin LI return (error); 2106215940b3SXin LI 2107215940b3SXin LI /* 2108215940b3SXin LI * Copy only (length-1) bytes to make sure that 2109215940b3SXin LI * if_description is always nul terminated. The 2110215940b3SXin LI * length parameter is supposed to count the 2111215940b3SXin LI * terminating nul in. 2112215940b3SXin LI */ 2113215940b3SXin LI if (ifr->ifr_buffer.length > ifdescr_maxlen) 2114215940b3SXin LI return (ENAMETOOLONG); 2115215940b3SXin LI else if (ifr->ifr_buffer.length == 0) 2116215940b3SXin LI descrbuf = NULL; 2117215940b3SXin LI else { 2118215940b3SXin LI descrbuf = malloc(ifr->ifr_buffer.length, M_IFDESCR, 2119215940b3SXin LI M_WAITOK | M_ZERO); 2120215940b3SXin LI error = copyin(ifr->ifr_buffer.buffer, descrbuf, 2121215940b3SXin LI ifr->ifr_buffer.length - 1); 2122215940b3SXin LI if (error) { 2123215940b3SXin LI free(descrbuf, M_IFDESCR); 2124215940b3SXin LI break; 2125215940b3SXin LI } 2126215940b3SXin LI } 2127215940b3SXin LI 2128215940b3SXin LI sx_xlock(&ifdescr_sx); 2129215940b3SXin LI odescrbuf = ifp->if_description; 2130215940b3SXin LI ifp->if_description = descrbuf; 2131215940b3SXin LI sx_xunlock(&ifdescr_sx); 2132215940b3SXin LI 2133215940b3SXin LI getmicrotime(&ifp->if_lastchange); 2134215940b3SXin LI free(odescrbuf, M_IFDESCR); 2135215940b3SXin LI break; 2136215940b3SXin LI 2137df8bae1dSRodney W. Grimes case SIOCSIFFLAGS: 2138acd3428bSRobert Watson error = priv_check(td, PRIV_NET_SETIFFLAGS); 21399448326fSPoul-Henning Kamp if (error) 2140df8bae1dSRodney W. Grimes return (error); 2141292ee7beSRobert Watson /* 2142292ee7beSRobert Watson * Currently, no driver owned flags pass the IFF_CANTCHANGE 2143292ee7beSRobert Watson * check, so we don't need special handling here yet. 2144292ee7beSRobert Watson */ 214562f76486SMaxim Sobolev new_flags = (ifr->ifr_flags & 0xffff) | 214662f76486SMaxim Sobolev (ifr->ifr_flagshigh << 16); 2147cf4b9371SPoul-Henning Kamp if (ifp->if_flags & IFF_SMART) { 2148cf4b9371SPoul-Henning Kamp /* Smart drivers twiddle their own routes */ 21492f55ead7SPoul-Henning Kamp } else if (ifp->if_flags & IFF_UP && 215062f76486SMaxim Sobolev (new_flags & IFF_UP) == 0) { 2151df8bae1dSRodney W. Grimes int s = splimp(); 2152df8bae1dSRodney W. Grimes if_down(ifp); 2153df8bae1dSRodney W. Grimes splx(s); 215462f76486SMaxim Sobolev } else if (new_flags & IFF_UP && 2155cf4b9371SPoul-Henning Kamp (ifp->if_flags & IFF_UP) == 0) { 2156df8bae1dSRodney W. Grimes int s = splimp(); 2157df8bae1dSRodney W. Grimes if_up(ifp); 2158df8bae1dSRodney W. Grimes splx(s); 2159df8bae1dSRodney W. Grimes } 21607aebc5e8SYaroslav Tykhiy /* See if permanently promiscuous mode bit is about to flip */ 21617aebc5e8SYaroslav Tykhiy if ((ifp->if_flags ^ new_flags) & IFF_PPROMISC) { 21627aebc5e8SYaroslav Tykhiy if (new_flags & IFF_PPROMISC) 21637aebc5e8SYaroslav Tykhiy ifp->if_flags |= IFF_PROMISC; 21647aebc5e8SYaroslav Tykhiy else if (ifp->if_pcount == 0) 21657aebc5e8SYaroslav Tykhiy ifp->if_flags &= ~IFF_PROMISC; 21667aebc5e8SYaroslav Tykhiy log(LOG_INFO, "%s: permanently promiscuous mode %s\n", 21677aebc5e8SYaroslav Tykhiy ifp->if_xname, 21687aebc5e8SYaroslav Tykhiy (new_flags & IFF_PPROMISC) ? "enabled" : "disabled"); 21697aebc5e8SYaroslav Tykhiy } 2170df8bae1dSRodney W. Grimes ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | 217162f76486SMaxim Sobolev (new_flags &~ IFF_CANTCHANGE); 217231302ebfSRobert Watson if (ifp->if_ioctl) { 2173df8bae1dSRodney W. Grimes (void) (*ifp->if_ioctl)(ifp, cmd, data); 217431302ebfSRobert Watson } 217598b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 2176df8bae1dSRodney W. Grimes break; 2177df8bae1dSRodney W. Grimes 2178016da741SJonathan Lemon case SIOCSIFCAP: 2179acd3428bSRobert Watson error = priv_check(td, PRIV_NET_SETIFCAP); 2180016da741SJonathan Lemon if (error) 2181016da741SJonathan Lemon return (error); 2182efb4018bSYaroslav Tykhiy if (ifp->if_ioctl == NULL) 2183efb4018bSYaroslav Tykhiy return (EOPNOTSUPP); 2184016da741SJonathan Lemon if (ifr->ifr_reqcap & ~ifp->if_capabilities) 2185016da741SJonathan Lemon return (EINVAL); 2186efb4018bSYaroslav Tykhiy error = (*ifp->if_ioctl)(ifp, cmd, data); 2187efb4018bSYaroslav Tykhiy if (error == 0) 2188efb4018bSYaroslav Tykhiy getmicrotime(&ifp->if_lastchange); 2189016da741SJonathan Lemon break; 2190016da741SJonathan Lemon 21918f293a63SRobert Watson #ifdef MAC 21928f293a63SRobert Watson case SIOCSIFMAC: 219330d239bcSRobert Watson error = mac_ifnet_ioctl_set(td->td_ucred, ifr, ifp); 21948f293a63SRobert Watson break; 21958f293a63SRobert Watson #endif 21968f293a63SRobert Watson 219736c19a57SBrooks Davis case SIOCSIFNAME: 2198acd3428bSRobert Watson error = priv_check(td, PRIV_NET_SETIFNAME); 2199acd3428bSRobert Watson if (error) 220036c19a57SBrooks Davis return (error); 220136c19a57SBrooks Davis error = copyinstr(ifr->ifr_data, new_name, IFNAMSIZ, NULL); 2202bc1470f1SBrooks Davis if (error != 0) 220336c19a57SBrooks Davis return (error); 2204bc1470f1SBrooks Davis if (new_name[0] == '\0') 2205bc1470f1SBrooks Davis return (EINVAL); 220636c19a57SBrooks Davis if (ifunit(new_name) != NULL) 220736c19a57SBrooks Davis return (EEXIST); 220836c19a57SBrooks Davis 22095428776eSJohn Baldwin /* 22105428776eSJohn Baldwin * XXX: Locking. Nothing else seems to lock if_flags, 22115428776eSJohn Baldwin * and there are numerous other races with the 22125428776eSJohn Baldwin * ifunit() checks not being atomic with namespace 22135428776eSJohn Baldwin * changes (renames, vmoves, if_attach, etc). 22145428776eSJohn Baldwin */ 22155428776eSJohn Baldwin ifp->if_flags |= IFF_RENAMING; 22165428776eSJohn Baldwin 221736c19a57SBrooks Davis /* Announce the departure of the interface. */ 221836c19a57SBrooks Davis rt_ifannouncemsg(ifp, IFAN_DEPARTURE); 221952023244SMax Laier EVENTHANDLER_INVOKE(ifnet_departure_event, ifp); 222036c19a57SBrooks Davis 222171672bb6SBrooks Davis log(LOG_INFO, "%s: changing name to '%s'\n", 222271672bb6SBrooks Davis ifp->if_xname, new_name); 222371672bb6SBrooks Davis 222436c19a57SBrooks Davis strlcpy(ifp->if_xname, new_name, sizeof(ifp->if_xname)); 22254a0d6638SRuslan Ermilov ifa = ifp->if_addr; 222636c19a57SBrooks Davis IFA_LOCK(ifa); 222736c19a57SBrooks Davis sdl = (struct sockaddr_dl *)ifa->ifa_addr; 222836c19a57SBrooks Davis namelen = strlen(new_name); 222936c19a57SBrooks Davis onamelen = sdl->sdl_nlen; 223036c19a57SBrooks Davis /* 223136c19a57SBrooks Davis * Move the address if needed. This is safe because we 223236c19a57SBrooks Davis * allocate space for a name of length IFNAMSIZ when we 223336c19a57SBrooks Davis * create this in if_attach(). 223436c19a57SBrooks Davis */ 223536c19a57SBrooks Davis if (namelen != onamelen) { 223636c19a57SBrooks Davis bcopy(sdl->sdl_data + onamelen, 223736c19a57SBrooks Davis sdl->sdl_data + namelen, sdl->sdl_alen); 223836c19a57SBrooks Davis } 223936c19a57SBrooks Davis bcopy(new_name, sdl->sdl_data, namelen); 224036c19a57SBrooks Davis sdl->sdl_nlen = namelen; 224136c19a57SBrooks Davis sdl = (struct sockaddr_dl *)ifa->ifa_netmask; 224236c19a57SBrooks Davis bzero(sdl->sdl_data, onamelen); 224336c19a57SBrooks Davis while (namelen != 0) 224436c19a57SBrooks Davis sdl->sdl_data[--namelen] = 0xff; 224536c19a57SBrooks Davis IFA_UNLOCK(ifa); 224636c19a57SBrooks Davis 224725a4adceSMax Laier EVENTHANDLER_INVOKE(ifnet_arrival_event, ifp); 224836c19a57SBrooks Davis /* Announce the return of the interface. */ 224936c19a57SBrooks Davis rt_ifannouncemsg(ifp, IFAN_ARRIVAL); 22505428776eSJohn Baldwin 22515428776eSJohn Baldwin ifp->if_flags &= ~IFF_RENAMING; 225236c19a57SBrooks Davis break; 225336c19a57SBrooks Davis 2254679e1390SJamie Gritton #ifdef VIMAGE 2255679e1390SJamie Gritton case SIOCSIFVNET: 2256679e1390SJamie Gritton error = priv_check(td, PRIV_NET_SETIFVNET); 2257679e1390SJamie Gritton if (error) 2258679e1390SJamie Gritton return (error); 2259be31e5e7SBjoern A. Zeeb error = if_vmove_loan(td, ifp, ifr->ifr_name, ifr->ifr_jid); 2260679e1390SJamie Gritton break; 2261679e1390SJamie Gritton #endif 2262679e1390SJamie Gritton 2263df8bae1dSRodney W. Grimes case SIOCSIFMETRIC: 2264acd3428bSRobert Watson error = priv_check(td, PRIV_NET_SETIFMETRIC); 22659448326fSPoul-Henning Kamp if (error) 2266df8bae1dSRodney W. Grimes return (error); 2267df8bae1dSRodney W. Grimes ifp->if_metric = ifr->ifr_metric; 226898b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 2269df8bae1dSRodney W. Grimes break; 2270df8bae1dSRodney W. Grimes 2271074c4a4eSGarrett Wollman case SIOCSIFPHYS: 2272acd3428bSRobert Watson error = priv_check(td, PRIV_NET_SETIFPHYS); 2273e39a0280SGary Palmer if (error) 2274913e410eSYaroslav Tykhiy return (error); 2275913e410eSYaroslav Tykhiy if (ifp->if_ioctl == NULL) 2276913e410eSYaroslav Tykhiy return (EOPNOTSUPP); 2277e39a0280SGary Palmer error = (*ifp->if_ioctl)(ifp, cmd, data); 2278e39a0280SGary Palmer if (error == 0) 227998b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 2280913e410eSYaroslav Tykhiy break; 2281074c4a4eSGarrett Wollman 2282a7028af7SDavid Greenman case SIOCSIFMTU: 228382cd038dSYoshinobu Inoue { 228482cd038dSYoshinobu Inoue u_long oldmtu = ifp->if_mtu; 228582cd038dSYoshinobu Inoue 2286acd3428bSRobert Watson error = priv_check(td, PRIV_NET_SETIFMTU); 22879448326fSPoul-Henning Kamp if (error) 2288a7028af7SDavid Greenman return (error); 2289aab3beeeSBrian Somers if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU) 229075ee03cbSDavid Greenman return (EINVAL); 2291f13ad206SJonathan Lemon if (ifp->if_ioctl == NULL) 2292f13ad206SJonathan Lemon return (EOPNOTSUPP); 2293e39a0280SGary Palmer error = (*ifp->if_ioctl)(ifp, cmd, data); 229448f71763SRuslan Ermilov if (error == 0) { 229598b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 229648f71763SRuslan Ermilov rt_ifmsg(ifp); 229748f71763SRuslan Ermilov } 229882cd038dSYoshinobu Inoue /* 229982cd038dSYoshinobu Inoue * If the link MTU changed, do network layer specific procedure. 230082cd038dSYoshinobu Inoue */ 230182cd038dSYoshinobu Inoue if (ifp->if_mtu != oldmtu) { 230282cd038dSYoshinobu Inoue #ifdef INET6 230382cd038dSYoshinobu Inoue nd6_setmtu(ifp); 230482cd038dSYoshinobu Inoue #endif 230582cd038dSYoshinobu Inoue } 2306f13ad206SJonathan Lemon break; 230782cd038dSYoshinobu Inoue } 2308a7028af7SDavid Greenman 2309df8bae1dSRodney W. Grimes case SIOCADDMULTI: 2310df8bae1dSRodney W. Grimes case SIOCDELMULTI: 2311acd3428bSRobert Watson if (cmd == SIOCADDMULTI) 2312acd3428bSRobert Watson error = priv_check(td, PRIV_NET_ADDMULTI); 2313acd3428bSRobert Watson else 2314acd3428bSRobert Watson error = priv_check(td, PRIV_NET_DELMULTI); 23159448326fSPoul-Henning Kamp if (error) 2316df8bae1dSRodney W. Grimes return (error); 2317477180fbSGarrett Wollman 2318477180fbSGarrett Wollman /* Don't allow group membership on non-multicast interfaces. */ 2319477180fbSGarrett Wollman if ((ifp->if_flags & IFF_MULTICAST) == 0) 2320f13ad206SJonathan Lemon return (EOPNOTSUPP); 2321477180fbSGarrett Wollman 2322477180fbSGarrett Wollman /* Don't let users screw up protocols' entries. */ 2323477180fbSGarrett Wollman if (ifr->ifr_addr.sa_family != AF_LINK) 2324f13ad206SJonathan Lemon return (EINVAL); 2325477180fbSGarrett Wollman 2326477180fbSGarrett Wollman if (cmd == SIOCADDMULTI) { 2327477180fbSGarrett Wollman struct ifmultiaddr *ifma; 2328ec002feeSBruce M Simpson 2329ec002feeSBruce M Simpson /* 2330ec002feeSBruce M Simpson * Userland is only permitted to join groups once 2331ec002feeSBruce M Simpson * via the if_addmulti() KPI, because it cannot hold 2332ec002feeSBruce M Simpson * struct ifmultiaddr * between calls. It may also 2333ec002feeSBruce M Simpson * lose a race while we check if the membership 2334ec002feeSBruce M Simpson * already exists. 2335ec002feeSBruce M Simpson */ 2336ec002feeSBruce M Simpson IF_ADDR_LOCK(ifp); 2337ec002feeSBruce M Simpson ifma = if_findmulti(ifp, &ifr->ifr_addr); 2338ec002feeSBruce M Simpson IF_ADDR_UNLOCK(ifp); 2339ec002feeSBruce M Simpson if (ifma != NULL) 2340ec002feeSBruce M Simpson error = EADDRINUSE; 2341ec002feeSBruce M Simpson else 2342477180fbSGarrett Wollman error = if_addmulti(ifp, &ifr->ifr_addr, &ifma); 2343477180fbSGarrett Wollman } else { 2344477180fbSGarrett Wollman error = if_delmulti(ifp, &ifr->ifr_addr); 2345477180fbSGarrett Wollman } 2346e39a0280SGary Palmer if (error == 0) 234798b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 2348f13ad206SJonathan Lemon break; 2349df8bae1dSRodney W. Grimes 235041b3e8e5SJun-ichiro itojun Hagino case SIOCSIFPHYADDR: 235141b3e8e5SJun-ichiro itojun Hagino case SIOCDIFPHYADDR: 235241b3e8e5SJun-ichiro itojun Hagino #ifdef INET6 235341b3e8e5SJun-ichiro itojun Hagino case SIOCSIFPHYADDR_IN6: 235441b3e8e5SJun-ichiro itojun Hagino #endif 235533841545SHajimu UMEMOTO case SIOCSLIFPHYADDR: 2356a912e453SPeter Wemm case SIOCSIFMEDIA: 2357d7189ec6SJoerg Wunsch case SIOCSIFGENERIC: 2358acd3428bSRobert Watson error = priv_check(td, PRIV_NET_HWIOCTL); 2359a912e453SPeter Wemm if (error) 2360a912e453SPeter Wemm return (error); 2361f13ad206SJonathan Lemon if (ifp->if_ioctl == NULL) 2362a912e453SPeter Wemm return (EOPNOTSUPP); 2363a912e453SPeter Wemm error = (*ifp->if_ioctl)(ifp, cmd, data); 2364a912e453SPeter Wemm if (error == 0) 236598b9590eSPoul-Henning Kamp getmicrotime(&ifp->if_lastchange); 2366f13ad206SJonathan Lemon break; 2367a912e453SPeter Wemm 2368413dd0baSPoul-Henning Kamp case SIOCGIFSTATUS: 2369413dd0baSPoul-Henning Kamp ifs = (struct ifstat *)data; 2370413dd0baSPoul-Henning Kamp ifs->ascii[0] = '\0'; 2371413dd0baSPoul-Henning Kamp 237233841545SHajimu UMEMOTO case SIOCGIFPSRCADDR: 237333841545SHajimu UMEMOTO case SIOCGIFPDSTADDR: 237433841545SHajimu UMEMOTO case SIOCGLIFPHYADDR: 2375a912e453SPeter Wemm case SIOCGIFMEDIA: 2376d7189ec6SJoerg Wunsch case SIOCGIFGENERIC: 2377913e410eSYaroslav Tykhiy if (ifp->if_ioctl == NULL) 2378a912e453SPeter Wemm return (EOPNOTSUPP); 2379f13ad206SJonathan Lemon error = (*ifp->if_ioctl)(ifp, cmd, data); 2380f13ad206SJonathan Lemon break; 2381a912e453SPeter Wemm 2382b106252cSBill Paul case SIOCSIFLLADDR: 2383acd3428bSRobert Watson error = priv_check(td, PRIV_NET_SETLLADDR); 2384b106252cSBill Paul if (error) 2385b106252cSBill Paul return (error); 2386f13ad206SJonathan Lemon error = if_setlladdr(ifp, 238766ce51ceSArchie Cobbs ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len); 2388ea4ca115SAndrew Thompson EVENTHANDLER_INVOKE(iflladdr_event, ifp); 2389f13ad206SJonathan Lemon break; 239066ce51ceSArchie Cobbs 23910dad3f0eSMax Laier case SIOCAIFGROUP: 23920dad3f0eSMax Laier { 23930dad3f0eSMax Laier struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; 23940dad3f0eSMax Laier 2395acd3428bSRobert Watson error = priv_check(td, PRIV_NET_ADDIFGROUP); 23960dad3f0eSMax Laier if (error) 23970dad3f0eSMax Laier return (error); 23980dad3f0eSMax Laier if ((error = if_addgroup(ifp, ifgr->ifgr_group))) 23990dad3f0eSMax Laier return (error); 24000dad3f0eSMax Laier break; 24010dad3f0eSMax Laier } 24020dad3f0eSMax Laier 24030dad3f0eSMax Laier case SIOCGIFGROUP: 24040dad3f0eSMax Laier if ((error = if_getgroup((struct ifgroupreq *)ifr, ifp))) 24050dad3f0eSMax Laier return (error); 24060dad3f0eSMax Laier break; 24070dad3f0eSMax Laier 24080dad3f0eSMax Laier case SIOCDIFGROUP: 24090dad3f0eSMax Laier { 24100dad3f0eSMax Laier struct ifgroupreq *ifgr = (struct ifgroupreq *)ifr; 24110dad3f0eSMax Laier 2412acd3428bSRobert Watson error = priv_check(td, PRIV_NET_DELIFGROUP); 24130dad3f0eSMax Laier if (error) 24140dad3f0eSMax Laier return (error); 24150dad3f0eSMax Laier if ((error = if_delgroup(ifp, ifgr->ifgr_group))) 24160dad3f0eSMax Laier return (error); 24170dad3f0eSMax Laier break; 24180dad3f0eSMax Laier } 24190dad3f0eSMax Laier 2420df8bae1dSRodney W. Grimes default: 2421f13ad206SJonathan Lemon error = ENOIOCTL; 2422f13ad206SJonathan Lemon break; 2423f13ad206SJonathan Lemon } 2424f13ad206SJonathan Lemon return (error); 2425f13ad206SJonathan Lemon } 2426f13ad206SJonathan Lemon 24279af74f3dSSergey Kandaurov #ifdef COMPAT_FREEBSD32 24289af74f3dSSergey Kandaurov struct ifconf32 { 24299af74f3dSSergey Kandaurov int32_t ifc_len; 24309af74f3dSSergey Kandaurov union { 24319af74f3dSSergey Kandaurov uint32_t ifcu_buf; 24329af74f3dSSergey Kandaurov uint32_t ifcu_req; 24339af74f3dSSergey Kandaurov } ifc_ifcu; 24349af74f3dSSergey Kandaurov }; 24359af74f3dSSergey Kandaurov #define SIOCGIFCONF32 _IOWR('i', 36, struct ifconf32) 24369af74f3dSSergey Kandaurov #endif 24379af74f3dSSergey Kandaurov 2438f13ad206SJonathan Lemon /* 2439f13ad206SJonathan Lemon * Interface ioctls. 2440f13ad206SJonathan Lemon */ 2441f13ad206SJonathan Lemon int 244272fd1b6aSDag-Erling Smørgrav ifioctl(struct socket *so, u_long cmd, caddr_t data, struct thread *td) 2443f13ad206SJonathan Lemon { 2444f13ad206SJonathan Lemon struct ifnet *ifp; 2445f13ad206SJonathan Lemon struct ifreq *ifr; 2446f13ad206SJonathan Lemon int error; 244762f76486SMaxim Sobolev int oif_flags; 2448f13ad206SJonathan Lemon 24491fb51a12SBjoern A. Zeeb CURVNET_SET(so->so_vnet); 2450f13ad206SJonathan Lemon switch (cmd) { 2451f13ad206SJonathan Lemon case SIOCGIFCONF: 2452f13ad206SJonathan Lemon case OSIOCGIFCONF: 24531fb51a12SBjoern A. Zeeb error = ifconf(cmd, data); 24541fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 24551fb51a12SBjoern A. Zeeb return (error); 24569af74f3dSSergey Kandaurov 24579af74f3dSSergey Kandaurov #ifdef COMPAT_FREEBSD32 24589af74f3dSSergey Kandaurov case SIOCGIFCONF32: 24599af74f3dSSergey Kandaurov { 24609af74f3dSSergey Kandaurov struct ifconf32 *ifc32; 24619af74f3dSSergey Kandaurov struct ifconf ifc; 24629af74f3dSSergey Kandaurov 24639af74f3dSSergey Kandaurov ifc32 = (struct ifconf32 *)data; 24649af74f3dSSergey Kandaurov ifc.ifc_len = ifc32->ifc_len; 24659af74f3dSSergey Kandaurov ifc.ifc_buf = PTRIN(ifc32->ifc_buf); 24669af74f3dSSergey Kandaurov 24671fb51a12SBjoern A. Zeeb error = ifconf(SIOCGIFCONF, (void *)&ifc); 24681fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 24691fb51a12SBjoern A. Zeeb return (error); 24709af74f3dSSergey Kandaurov } 24719af74f3dSSergey Kandaurov #endif 2472f13ad206SJonathan Lemon } 2473f13ad206SJonathan Lemon ifr = (struct ifreq *)data; 2474f13ad206SJonathan Lemon 2475f13ad206SJonathan Lemon switch (cmd) { 2476feb08d06SMarko Zec #ifdef VIMAGE 2477679e1390SJamie Gritton case SIOCSIFRVNET: 2478679e1390SJamie Gritton error = priv_check(td, PRIV_NET_SETIFVNET); 24791fb51a12SBjoern A. Zeeb if (error == 0) 24801fb51a12SBjoern A. Zeeb error = if_vmove_reclaim(td, ifr->ifr_name, 24811fb51a12SBjoern A. Zeeb ifr->ifr_jid); 24821fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 2483679e1390SJamie Gritton return (error); 2484feb08d06SMarko Zec #endif 2485f13ad206SJonathan Lemon case SIOCIFCREATE: 24866b7330e2SSam Leffler case SIOCIFCREATE2: 2487acd3428bSRobert Watson error = priv_check(td, PRIV_NET_IFCREATE); 24881fb51a12SBjoern A. Zeeb if (error == 0) 24891fb51a12SBjoern A. Zeeb error = if_clone_create(ifr->ifr_name, 24901fb51a12SBjoern A. Zeeb sizeof(ifr->ifr_name), 24911fb51a12SBjoern A. Zeeb cmd == SIOCIFCREATE2 ? ifr->ifr_data : NULL); 24921fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 24936b7330e2SSam Leffler return (error); 2494f13ad206SJonathan Lemon case SIOCIFDESTROY: 2495acd3428bSRobert Watson error = priv_check(td, PRIV_NET_IFDESTROY); 24961fb51a12SBjoern A. Zeeb if (error == 0) 24971fb51a12SBjoern A. Zeeb error = if_clone_destroy(ifr->ifr_name); 24981fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 2499f13ad206SJonathan Lemon return (error); 2500f13ad206SJonathan Lemon 2501f13ad206SJonathan Lemon case SIOCIFGCLONERS: 25021fb51a12SBjoern A. Zeeb error = if_clone_list((struct if_clonereq *)data); 25031fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 25041fb51a12SBjoern A. Zeeb return (error); 25050dad3f0eSMax Laier case SIOCGIFGMEMB: 25061fb51a12SBjoern A. Zeeb error = if_getgroupmembers((struct ifgroupreq *)data); 25071fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 25081fb51a12SBjoern A. Zeeb return (error); 2509f13ad206SJonathan Lemon } 2510f13ad206SJonathan Lemon 25116064c5d3SRobert Watson ifp = ifunit_ref(ifr->ifr_name); 25121fb51a12SBjoern A. Zeeb if (ifp == NULL) { 25131fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 2514f13ad206SJonathan Lemon return (ENXIO); 25151fb51a12SBjoern A. Zeeb } 2516f13ad206SJonathan Lemon 2517f13ad206SJonathan Lemon error = ifhwioctl(cmd, ifp, data, td); 25186064c5d3SRobert Watson if (error != ENOIOCTL) { 25196064c5d3SRobert Watson if_rele(ifp); 25201fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 2521f13ad206SJonathan Lemon return (error); 25226064c5d3SRobert Watson } 2523f13ad206SJonathan Lemon 252482cd038dSYoshinobu Inoue oif_flags = ifp->if_flags; 25256064c5d3SRobert Watson if (so->so_proto == NULL) { 25266064c5d3SRobert Watson if_rele(ifp); 25271fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 2528df8bae1dSRodney W. Grimes return (EOPNOTSUPP); 25296064c5d3SRobert Watson } 2530df8bae1dSRodney W. Grimes #ifndef COMPAT_43 253182cd038dSYoshinobu Inoue error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd, 25322c37256eSGarrett Wollman data, 2533b40ce416SJulian Elischer ifp, td)); 2534bc3977f1SJamie Gritton if (error == EOPNOTSUPP && ifp != NULL && ifp->if_ioctl != NULL) 2535bc3977f1SJamie Gritton error = (*ifp->if_ioctl)(ifp, cmd, data); 2536df8bae1dSRodney W. Grimes #else 2537df8bae1dSRodney W. Grimes { 2538e40bae9aSRoman Divacky u_long ocmd = cmd; 2539df8bae1dSRodney W. Grimes 2540df8bae1dSRodney W. Grimes switch (cmd) { 2541df8bae1dSRodney W. Grimes 2542df8bae1dSRodney W. Grimes case SIOCSIFDSTADDR: 2543df8bae1dSRodney W. Grimes case SIOCSIFADDR: 2544df8bae1dSRodney W. Grimes case SIOCSIFBRDADDR: 2545df8bae1dSRodney W. Grimes case SIOCSIFNETMASK: 2546df8bae1dSRodney W. Grimes #if BYTE_ORDER != BIG_ENDIAN 2547df8bae1dSRodney W. Grimes if (ifr->ifr_addr.sa_family == 0 && 2548df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_len < 16) { 2549df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len; 2550df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_len = 16; 2551df8bae1dSRodney W. Grimes } 2552df8bae1dSRodney W. Grimes #else 2553df8bae1dSRodney W. Grimes if (ifr->ifr_addr.sa_len == 0) 2554df8bae1dSRodney W. Grimes ifr->ifr_addr.sa_len = 16; 2555df8bae1dSRodney W. Grimes #endif 2556df8bae1dSRodney W. Grimes break; 2557df8bae1dSRodney W. Grimes 2558df8bae1dSRodney W. Grimes case OSIOCGIFADDR: 2559df8bae1dSRodney W. Grimes cmd = SIOCGIFADDR; 2560df8bae1dSRodney W. Grimes break; 2561df8bae1dSRodney W. Grimes 2562df8bae1dSRodney W. Grimes case OSIOCGIFDSTADDR: 2563df8bae1dSRodney W. Grimes cmd = SIOCGIFDSTADDR; 2564df8bae1dSRodney W. Grimes break; 2565df8bae1dSRodney W. Grimes 2566df8bae1dSRodney W. Grimes case OSIOCGIFBRDADDR: 2567df8bae1dSRodney W. Grimes cmd = SIOCGIFBRDADDR; 2568df8bae1dSRodney W. Grimes break; 2569df8bae1dSRodney W. Grimes 2570df8bae1dSRodney W. Grimes case OSIOCGIFNETMASK: 2571df8bae1dSRodney W. Grimes cmd = SIOCGIFNETMASK; 2572df8bae1dSRodney W. Grimes } 25732c37256eSGarrett Wollman error = ((*so->so_proto->pr_usrreqs->pru_control)(so, 25742c37256eSGarrett Wollman cmd, 25752c37256eSGarrett Wollman data, 2576b40ce416SJulian Elischer ifp, td)); 2577bc3977f1SJamie Gritton if (error == EOPNOTSUPP && ifp != NULL && 2578bc3977f1SJamie Gritton ifp->if_ioctl != NULL) 2579bc3977f1SJamie Gritton error = (*ifp->if_ioctl)(ifp, cmd, data); 2580df8bae1dSRodney W. Grimes switch (ocmd) { 2581df8bae1dSRodney W. Grimes 2582df8bae1dSRodney W. Grimes case OSIOCGIFADDR: 2583df8bae1dSRodney W. Grimes case OSIOCGIFDSTADDR: 2584df8bae1dSRodney W. Grimes case OSIOCGIFBRDADDR: 2585df8bae1dSRodney W. Grimes case OSIOCGIFNETMASK: 2586df8bae1dSRodney W. Grimes *(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family; 258782cd038dSYoshinobu Inoue 258882cd038dSYoshinobu Inoue } 258982cd038dSYoshinobu Inoue } 259082cd038dSYoshinobu Inoue #endif /* COMPAT_43 */ 259182cd038dSYoshinobu Inoue 259282cd038dSYoshinobu Inoue if ((oif_flags ^ ifp->if_flags) & IFF_UP) { 259382cd038dSYoshinobu Inoue #ifdef INET6 259482cd038dSYoshinobu Inoue if (ifp->if_flags & IFF_UP) { 259582cd038dSYoshinobu Inoue int s = splimp(); 259682cd038dSYoshinobu Inoue in6_if_up(ifp); 259782cd038dSYoshinobu Inoue splx(s); 259882cd038dSYoshinobu Inoue } 259982cd038dSYoshinobu Inoue #endif 2600df8bae1dSRodney W. Grimes } 26016064c5d3SRobert Watson if_rele(ifp); 26021fb51a12SBjoern A. Zeeb CURVNET_RESTORE(); 2603df8bae1dSRodney W. Grimes return (error); 2604df8bae1dSRodney W. Grimes } 2605df8bae1dSRodney W. Grimes 2606df8bae1dSRodney W. Grimes /* 2607292ee7beSRobert Watson * The code common to handling reference counted flags, 26081a3b6859SYaroslav Tykhiy * e.g., in ifpromisc() and if_allmulti(). 2609b5c8bd59SYaroslav Tykhiy * The "pflag" argument can specify a permanent mode flag to check, 26101a3b6859SYaroslav Tykhiy * such as IFF_PPROMISC for promiscuous mode; should be 0 if none. 2611292ee7beSRobert Watson * 2612292ee7beSRobert Watson * Only to be used on stack-owned flags, not driver-owned flags. 26131a3b6859SYaroslav Tykhiy */ 26141a3b6859SYaroslav Tykhiy static int 26151a3b6859SYaroslav Tykhiy if_setflag(struct ifnet *ifp, int flag, int pflag, int *refcount, int onswitch) 26161a3b6859SYaroslav Tykhiy { 26171a3b6859SYaroslav Tykhiy struct ifreq ifr; 26181a3b6859SYaroslav Tykhiy int error; 26191a3b6859SYaroslav Tykhiy int oldflags, oldcount; 26201a3b6859SYaroslav Tykhiy 26211a3b6859SYaroslav Tykhiy /* Sanity checks to catch programming errors */ 2622b5c8bd59SYaroslav Tykhiy KASSERT((flag & (IFF_DRV_OACTIVE|IFF_DRV_RUNNING)) == 0, 2623b5c8bd59SYaroslav Tykhiy ("%s: setting driver-owned flag %d", __func__, flag)); 2624b5c8bd59SYaroslav Tykhiy 2625b5c8bd59SYaroslav Tykhiy if (onswitch) 2626b5c8bd59SYaroslav Tykhiy KASSERT(*refcount >= 0, 2627b5c8bd59SYaroslav Tykhiy ("%s: increment negative refcount %d for flag %d", 2628b5c8bd59SYaroslav Tykhiy __func__, *refcount, flag)); 2629b5c8bd59SYaroslav Tykhiy else 2630b5c8bd59SYaroslav Tykhiy KASSERT(*refcount > 0, 2631b5c8bd59SYaroslav Tykhiy ("%s: decrement non-positive refcount %d for flag %d", 2632b5c8bd59SYaroslav Tykhiy __func__, *refcount, flag)); 26331a3b6859SYaroslav Tykhiy 26341a3b6859SYaroslav Tykhiy /* In case this mode is permanent, just touch refcount */ 26351a3b6859SYaroslav Tykhiy if (ifp->if_flags & pflag) { 26361a3b6859SYaroslav Tykhiy *refcount += onswitch ? 1 : -1; 26371a3b6859SYaroslav Tykhiy return (0); 26381a3b6859SYaroslav Tykhiy } 26391a3b6859SYaroslav Tykhiy 26401a3b6859SYaroslav Tykhiy /* Save ifnet parameters for if_ioctl() may fail */ 26411a3b6859SYaroslav Tykhiy oldcount = *refcount; 26421a3b6859SYaroslav Tykhiy oldflags = ifp->if_flags; 26431a3b6859SYaroslav Tykhiy 26441a3b6859SYaroslav Tykhiy /* 26451a3b6859SYaroslav Tykhiy * See if we aren't the only and touching refcount is enough. 26461a3b6859SYaroslav Tykhiy * Actually toggle interface flag if we are the first or last. 26471a3b6859SYaroslav Tykhiy */ 26481a3b6859SYaroslav Tykhiy if (onswitch) { 26491a3b6859SYaroslav Tykhiy if ((*refcount)++) 26501a3b6859SYaroslav Tykhiy return (0); 26511a3b6859SYaroslav Tykhiy ifp->if_flags |= flag; 26521a3b6859SYaroslav Tykhiy } else { 26531a3b6859SYaroslav Tykhiy if (--(*refcount)) 26541a3b6859SYaroslav Tykhiy return (0); 26551a3b6859SYaroslav Tykhiy ifp->if_flags &= ~flag; 26561a3b6859SYaroslav Tykhiy } 26571a3b6859SYaroslav Tykhiy 26581a3b6859SYaroslav Tykhiy /* Call down the driver since we've changed interface flags */ 26591a3b6859SYaroslav Tykhiy if (ifp->if_ioctl == NULL) { 26601a3b6859SYaroslav Tykhiy error = EOPNOTSUPP; 26611a3b6859SYaroslav Tykhiy goto recover; 26621a3b6859SYaroslav Tykhiy } 26631a3b6859SYaroslav Tykhiy ifr.ifr_flags = ifp->if_flags & 0xffff; 26641a3b6859SYaroslav Tykhiy ifr.ifr_flagshigh = ifp->if_flags >> 16; 26651a3b6859SYaroslav Tykhiy error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 26661a3b6859SYaroslav Tykhiy if (error) 26671a3b6859SYaroslav Tykhiy goto recover; 26681a3b6859SYaroslav Tykhiy /* Notify userland that interface flags have changed */ 26691a3b6859SYaroslav Tykhiy rt_ifmsg(ifp); 26701a3b6859SYaroslav Tykhiy return (0); 26711a3b6859SYaroslav Tykhiy 26721a3b6859SYaroslav Tykhiy recover: 26731a3b6859SYaroslav Tykhiy /* Recover after driver error */ 26741a3b6859SYaroslav Tykhiy *refcount = oldcount; 26751a3b6859SYaroslav Tykhiy ifp->if_flags = oldflags; 26761a3b6859SYaroslav Tykhiy return (error); 26771a3b6859SYaroslav Tykhiy } 26781a3b6859SYaroslav Tykhiy 26791a3b6859SYaroslav Tykhiy /* 2680963e4c2aSGarrett Wollman * Set/clear promiscuous mode on interface ifp based on the truth value 2681963e4c2aSGarrett Wollman * of pswitch. The calls are reference counted so that only the first 2682963e4c2aSGarrett Wollman * "on" request actually has an effect, as does the final "off" request. 2683963e4c2aSGarrett Wollman * Results are undefined if the "off" and "on" requests are not matched. 2684963e4c2aSGarrett Wollman */ 2685963e4c2aSGarrett Wollman int 268672fd1b6aSDag-Erling Smørgrav ifpromisc(struct ifnet *ifp, int pswitch) 2687963e4c2aSGarrett Wollman { 26884a26224cSGarrett Wollman int error; 26891a3b6859SYaroslav Tykhiy int oldflags = ifp->if_flags; 2690963e4c2aSGarrett Wollman 26911a3b6859SYaroslav Tykhiy error = if_setflag(ifp, IFF_PROMISC, IFF_PPROMISC, 26921a3b6859SYaroslav Tykhiy &ifp->if_pcount, pswitch); 26931a3b6859SYaroslav Tykhiy /* If promiscuous mode status has changed, log a message */ 26941a3b6859SYaroslav Tykhiy if (error == 0 && ((ifp->if_flags ^ oldflags) & IFF_PROMISC)) 26959bf40edeSBrooks Davis log(LOG_INFO, "%s: promiscuous mode %s\n", 26969bf40edeSBrooks Davis ifp->if_xname, 26974f3c11a6SBill Fenner (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled"); 26981a3b6859SYaroslav Tykhiy return (error); 2699963e4c2aSGarrett Wollman } 2700963e4c2aSGarrett Wollman 2701963e4c2aSGarrett Wollman /* 2702df8bae1dSRodney W. Grimes * Return interface configuration 2703df8bae1dSRodney W. Grimes * of system. List may be used 2704df8bae1dSRodney W. Grimes * in later ioctl's (above) to get 2705df8bae1dSRodney W. Grimes * other information. 2706df8bae1dSRodney W. Grimes */ 2707df8bae1dSRodney W. Grimes /*ARGSUSED*/ 27083bda9f9bSPoul-Henning Kamp static int 270972fd1b6aSDag-Erling Smørgrav ifconf(u_long cmd, caddr_t data) 2710df8bae1dSRodney W. Grimes { 27110b59d917SJonathan Lemon struct ifconf *ifc = (struct ifconf *)data; 27120b59d917SJonathan Lemon struct ifnet *ifp; 27130b59d917SJonathan Lemon struct ifaddr *ifa; 27144dcf2bbbSBrooks Davis struct ifreq ifr; 27154dcf2bbbSBrooks Davis struct sbuf *sb; 27164dcf2bbbSBrooks Davis int error, full = 0, valid_len, max_len; 2717df8bae1dSRodney W. Grimes 27184dcf2bbbSBrooks Davis /* Limit initial buffer size to MAXPHYS to avoid DoS from userspace. */ 27194dcf2bbbSBrooks Davis max_len = MAXPHYS - 1; 27204dcf2bbbSBrooks Davis 2721b0b4b28bSXin LI /* Prevent hostile input from being able to crash the system */ 2722b0b4b28bSXin LI if (ifc->ifc_len <= 0) 2723b0b4b28bSXin LI return (EINVAL); 2724b0b4b28bSXin LI 27254dcf2bbbSBrooks Davis again: 27264dcf2bbbSBrooks Davis if (ifc->ifc_len <= max_len) { 27274dcf2bbbSBrooks Davis max_len = ifc->ifc_len; 27284dcf2bbbSBrooks Davis full = 1; 27294dcf2bbbSBrooks Davis } 27304dcf2bbbSBrooks Davis sb = sbuf_new(NULL, NULL, max_len + 1, SBUF_FIXEDLEN); 27314dcf2bbbSBrooks Davis max_len = 0; 27324dcf2bbbSBrooks Davis valid_len = 0; 27334dcf2bbbSBrooks Davis 273477dfcdc4SRobert Watson IFNET_RLOCK(); 2735603724d3SBjoern A. Zeeb TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 27369bf40edeSBrooks Davis int addrs; 27372624cf89SGarrett Wollman 2738fbd24c5eSColin Percival /* 2739fbd24c5eSColin Percival * Zero the ifr_name buffer to make sure we don't 2740fbd24c5eSColin Percival * disclose the contents of the stack. 2741fbd24c5eSColin Percival */ 2742fbd24c5eSColin Percival memset(ifr.ifr_name, 0, sizeof(ifr.ifr_name)); 2743fbd24c5eSColin Percival 27449bf40edeSBrooks Davis if (strlcpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name)) 274562313e4cSSam Leffler >= sizeof(ifr.ifr_name)) { 274662313e4cSSam Leffler sbuf_delete(sb); 274762313e4cSSam Leffler IFNET_RUNLOCK(); 27484dcf2bbbSBrooks Davis return (ENAMETOOLONG); 274962313e4cSSam Leffler } 27502624cf89SGarrett Wollman 275175c13541SPoul-Henning Kamp addrs = 0; 2752ab5ed8a5SRobert Watson IF_ADDR_LOCK(ifp); 27532defe5cdSJonathan Lemon TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 27542defe5cdSJonathan Lemon struct sockaddr *sa = ifa->ifa_addr; 27552defe5cdSJonathan Lemon 2756b89e82ddSJamie Gritton if (prison_if(curthread->td_ucred, sa) != 0) 275775c13541SPoul-Henning Kamp continue; 275875c13541SPoul-Henning Kamp addrs++; 2759df8bae1dSRodney W. Grimes #ifdef COMPAT_43 2760df8bae1dSRodney W. Grimes if (cmd == OSIOCGIFCONF) { 2761df8bae1dSRodney W. Grimes struct osockaddr *osa = 2762df8bae1dSRodney W. Grimes (struct osockaddr *)&ifr.ifr_addr; 2763df8bae1dSRodney W. Grimes ifr.ifr_addr = *sa; 2764df8bae1dSRodney W. Grimes osa->sa_family = sa->sa_family; 27654dcf2bbbSBrooks Davis sbuf_bcat(sb, &ifr, sizeof(ifr)); 27664dcf2bbbSBrooks Davis max_len += sizeof(ifr); 2767df8bae1dSRodney W. Grimes } else 2768df8bae1dSRodney W. Grimes #endif 2769df8bae1dSRodney W. Grimes if (sa->sa_len <= sizeof(*sa)) { 2770df8bae1dSRodney W. Grimes ifr.ifr_addr = *sa; 27714dcf2bbbSBrooks Davis sbuf_bcat(sb, &ifr, sizeof(ifr)); 27724dcf2bbbSBrooks Davis max_len += sizeof(ifr); 2773df8bae1dSRodney W. Grimes } else { 27744dcf2bbbSBrooks Davis sbuf_bcat(sb, &ifr, 27754dcf2bbbSBrooks Davis offsetof(struct ifreq, ifr_addr)); 27764dcf2bbbSBrooks Davis max_len += offsetof(struct ifreq, ifr_addr); 27774dcf2bbbSBrooks Davis sbuf_bcat(sb, sa, sa->sa_len); 27784dcf2bbbSBrooks Davis max_len += sa->sa_len; 2779df8bae1dSRodney W. Grimes } 27804dcf2bbbSBrooks Davis 27814d369413SMatthew D Fleming if (sbuf_error(sb) == 0) 27824dcf2bbbSBrooks Davis valid_len = sbuf_len(sb); 2783df8bae1dSRodney W. Grimes } 2784ab5ed8a5SRobert Watson IF_ADDR_UNLOCK(ifp); 27854dcf2bbbSBrooks Davis if (addrs == 0) { 278675c13541SPoul-Henning Kamp bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); 27874dcf2bbbSBrooks Davis sbuf_bcat(sb, &ifr, sizeof(ifr)); 27884dcf2bbbSBrooks Davis max_len += sizeof(ifr); 27894dcf2bbbSBrooks Davis 27904d369413SMatthew D Fleming if (sbuf_error(sb) == 0) 27914dcf2bbbSBrooks Davis valid_len = sbuf_len(sb); 279275c13541SPoul-Henning Kamp } 2793df8bae1dSRodney W. Grimes } 2794b30a244cSJeffrey Hsu IFNET_RUNLOCK(); 27954dcf2bbbSBrooks Davis 27964dcf2bbbSBrooks Davis /* 27974dcf2bbbSBrooks Davis * If we didn't allocate enough space (uncommon), try again. If 27984dcf2bbbSBrooks Davis * we have already allocated as much space as we are allowed, 27994dcf2bbbSBrooks Davis * return what we've got. 28004dcf2bbbSBrooks Davis */ 28014dcf2bbbSBrooks Davis if (valid_len != max_len && !full) { 28024dcf2bbbSBrooks Davis sbuf_delete(sb); 28034dcf2bbbSBrooks Davis goto again; 28044dcf2bbbSBrooks Davis } 28054dcf2bbbSBrooks Davis 28064dcf2bbbSBrooks Davis ifc->ifc_len = valid_len; 28075ed8cedcSBrian Feldman sbuf_finish(sb); 28084dcf2bbbSBrooks Davis error = copyout(sbuf_data(sb), ifc->ifc_req, ifc->ifc_len); 28094dcf2bbbSBrooks Davis sbuf_delete(sb); 2810df8bae1dSRodney W. Grimes return (error); 2811df8bae1dSRodney W. Grimes } 2812df8bae1dSRodney W. Grimes 28131158dfb7SGarrett Wollman /* 28148b25904eSGleb Smirnoff * Just like ifpromisc(), but for all-multicast-reception mode. 28151158dfb7SGarrett Wollman */ 28161158dfb7SGarrett Wollman int 281772fd1b6aSDag-Erling Smørgrav if_allmulti(struct ifnet *ifp, int onswitch) 28181158dfb7SGarrett Wollman { 28191158dfb7SGarrett Wollman 28201a3b6859SYaroslav Tykhiy return (if_setflag(ifp, IFF_ALLMULTI, 0, &ifp->if_amcount, onswitch)); 28211158dfb7SGarrett Wollman } 28221158dfb7SGarrett Wollman 28235896d124SBruce M Simpson struct ifmultiaddr * 2824c3b31afdSRobert Watson if_findmulti(struct ifnet *ifp, struct sockaddr *sa) 28251158dfb7SGarrett Wollman { 28261158dfb7SGarrett Wollman struct ifmultiaddr *ifma; 28271158dfb7SGarrett Wollman 2828c3b31afdSRobert Watson IF_ADDR_LOCK_ASSERT(ifp); 2829c3b31afdSRobert Watson 28306817526dSPoul-Henning Kamp TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 283140d8a302SBruce M Simpson if (sa->sa_family == AF_LINK) { 283240d8a302SBruce M Simpson if (sa_dl_equal(ifma->ifma_addr, sa)) 283340d8a302SBruce M Simpson break; 283440d8a302SBruce M Simpson } else { 2835c3b31afdSRobert Watson if (sa_equal(ifma->ifma_addr, sa)) 2836c3b31afdSRobert Watson break; 28371158dfb7SGarrett Wollman } 283840d8a302SBruce M Simpson } 2839c3b31afdSRobert Watson 2840c3b31afdSRobert Watson return ifma; 284157af7922SJulian Elischer } 28421158dfb7SGarrett Wollman 28431158dfb7SGarrett Wollman /* 2844c3b31afdSRobert Watson * Allocate a new ifmultiaddr and initialize based on passed arguments. We 2845c3b31afdSRobert Watson * make copies of passed sockaddrs. The ifmultiaddr will not be added to 2846c3b31afdSRobert Watson * the ifnet multicast address list here, so the caller must do that and 2847c3b31afdSRobert Watson * other setup work (such as notifying the device driver). The reference 2848c3b31afdSRobert Watson * count is initialized to 1. 28491158dfb7SGarrett Wollman */ 2850c3b31afdSRobert Watson static struct ifmultiaddr * 2851c3b31afdSRobert Watson if_allocmulti(struct ifnet *ifp, struct sockaddr *sa, struct sockaddr *llsa, 2852c3b31afdSRobert Watson int mflags) 2853c3b31afdSRobert Watson { 2854c3b31afdSRobert Watson struct ifmultiaddr *ifma; 2855c3b31afdSRobert Watson struct sockaddr *dupsa; 2856c3b31afdSRobert Watson 28571ede983cSDag-Erling Smørgrav ifma = malloc(sizeof *ifma, M_IFMADDR, mflags | 2858c3b31afdSRobert Watson M_ZERO); 2859c3b31afdSRobert Watson if (ifma == NULL) 2860c3b31afdSRobert Watson return (NULL); 2861c3b31afdSRobert Watson 28621ede983cSDag-Erling Smørgrav dupsa = malloc(sa->sa_len, M_IFMADDR, mflags); 2863c3b31afdSRobert Watson if (dupsa == NULL) { 28641ede983cSDag-Erling Smørgrav free(ifma, M_IFMADDR); 2865c3b31afdSRobert Watson return (NULL); 28661158dfb7SGarrett Wollman } 28671158dfb7SGarrett Wollman bcopy(sa, dupsa, sa->sa_len); 28681158dfb7SGarrett Wollman ifma->ifma_addr = dupsa; 2869c3b31afdSRobert Watson 28701158dfb7SGarrett Wollman ifma->ifma_ifp = ifp; 28711158dfb7SGarrett Wollman ifma->ifma_refcount = 1; 2872d4d22970SGleb Smirnoff ifma->ifma_protospec = NULL; 2873c3b31afdSRobert Watson 2874c3b31afdSRobert Watson if (llsa == NULL) { 2875c3b31afdSRobert Watson ifma->ifma_lladdr = NULL; 2876c3b31afdSRobert Watson return (ifma); 2877c3b31afdSRobert Watson } 2878c3b31afdSRobert Watson 28791ede983cSDag-Erling Smørgrav dupsa = malloc(llsa->sa_len, M_IFMADDR, mflags); 2880c3b31afdSRobert Watson if (dupsa == NULL) { 28811ede983cSDag-Erling Smørgrav free(ifma->ifma_addr, M_IFMADDR); 28821ede983cSDag-Erling Smørgrav free(ifma, M_IFMADDR); 2883c3b31afdSRobert Watson return (NULL); 2884c3b31afdSRobert Watson } 2885c3b31afdSRobert Watson bcopy(llsa, dupsa, llsa->sa_len); 2886c3b31afdSRobert Watson ifma->ifma_lladdr = dupsa; 2887c3b31afdSRobert Watson 2888c3b31afdSRobert Watson return (ifma); 2889c3b31afdSRobert Watson } 2890373f88edSGarrett Wollman 28911158dfb7SGarrett Wollman /* 2892c3b31afdSRobert Watson * if_freemulti: free ifmultiaddr structure and possibly attached related 2893c3b31afdSRobert Watson * addresses. The caller is responsible for implementing reference 2894c3b31afdSRobert Watson * counting, notifying the driver, handling routing messages, and releasing 2895c3b31afdSRobert Watson * any dependent link layer state. 28961158dfb7SGarrett Wollman */ 2897c3b31afdSRobert Watson static void 2898c3b31afdSRobert Watson if_freemulti(struct ifmultiaddr *ifma) 2899c3b31afdSRobert Watson { 2900c3b31afdSRobert Watson 2901ec002feeSBruce M Simpson KASSERT(ifma->ifma_refcount == 0, ("if_freemulti: refcount %d", 2902c3b31afdSRobert Watson ifma->ifma_refcount)); 2903c3b31afdSRobert Watson KASSERT(ifma->ifma_protospec == NULL, 2904c3b31afdSRobert Watson ("if_freemulti: protospec not NULL")); 2905c3b31afdSRobert Watson 2906c3b31afdSRobert Watson if (ifma->ifma_lladdr != NULL) 29071ede983cSDag-Erling Smørgrav free(ifma->ifma_lladdr, M_IFMADDR); 29081ede983cSDag-Erling Smørgrav free(ifma->ifma_addr, M_IFMADDR); 29091ede983cSDag-Erling Smørgrav free(ifma, M_IFMADDR); 2910c3b31afdSRobert Watson } 2911c3b31afdSRobert Watson 2912c3b31afdSRobert Watson /* 2913c3b31afdSRobert Watson * Register an additional multicast address with a network interface. 2914c3b31afdSRobert Watson * 2915c3b31afdSRobert Watson * - If the address is already present, bump the reference count on the 2916c3b31afdSRobert Watson * address and return. 2917c3b31afdSRobert Watson * - If the address is not link-layer, look up a link layer address. 2918c3b31afdSRobert Watson * - Allocate address structures for one or both addresses, and attach to the 2919c3b31afdSRobert Watson * multicast address list on the interface. If automatically adding a link 2920c3b31afdSRobert Watson * layer address, the protocol address will own a reference to the link 2921c3b31afdSRobert Watson * layer address, to be freed when it is freed. 2922c3b31afdSRobert Watson * - Notify the network device driver of an addition to the multicast address 2923c3b31afdSRobert Watson * list. 2924c3b31afdSRobert Watson * 2925c3b31afdSRobert Watson * 'sa' points to caller-owned memory with the desired multicast address. 2926c3b31afdSRobert Watson * 2927c3b31afdSRobert Watson * 'retifma' will be used to return a pointer to the resulting multicast 2928c3b31afdSRobert Watson * address reference, if desired. 2929c3b31afdSRobert Watson */ 2930c3b31afdSRobert Watson int 2931c3b31afdSRobert Watson if_addmulti(struct ifnet *ifp, struct sockaddr *sa, 2932c3b31afdSRobert Watson struct ifmultiaddr **retifma) 2933c3b31afdSRobert Watson { 2934c3b31afdSRobert Watson struct ifmultiaddr *ifma, *ll_ifma; 2935c3b31afdSRobert Watson struct sockaddr *llsa; 2936c3b31afdSRobert Watson int error; 2937c3b31afdSRobert Watson 2938c3b31afdSRobert Watson /* 2939c3b31afdSRobert Watson * If the address is already present, return a new reference to it; 2940c3b31afdSRobert Watson * otherwise, allocate storage and set up a new address. 2941c3b31afdSRobert Watson */ 2942c3b31afdSRobert Watson IF_ADDR_LOCK(ifp); 2943c3b31afdSRobert Watson ifma = if_findmulti(ifp, sa); 2944c3b31afdSRobert Watson if (ifma != NULL) { 2945c3b31afdSRobert Watson ifma->ifma_refcount++; 2946c3b31afdSRobert Watson if (retifma != NULL) 2947c3b31afdSRobert Watson *retifma = ifma; 2948c3b31afdSRobert Watson IF_ADDR_UNLOCK(ifp); 2949c3b31afdSRobert Watson return (0); 2950c3b31afdSRobert Watson } 2951c3b31afdSRobert Watson 2952c3b31afdSRobert Watson /* 2953c3b31afdSRobert Watson * The address isn't already present; resolve the protocol address 2954c3b31afdSRobert Watson * into a link layer address, and then look that up, bump its 2955c3b31afdSRobert Watson * refcount or allocate an ifma for that also. If 'llsa' was 2956c3b31afdSRobert Watson * returned, we will need to free it later. 2957c3b31afdSRobert Watson */ 2958c3b31afdSRobert Watson llsa = NULL; 2959c3b31afdSRobert Watson ll_ifma = NULL; 2960c3b31afdSRobert Watson if (ifp->if_resolvemulti != NULL) { 2961c3b31afdSRobert Watson error = ifp->if_resolvemulti(ifp, &llsa, sa); 2962c3b31afdSRobert Watson if (error) 2963c3b31afdSRobert Watson goto unlock_out; 2964c3b31afdSRobert Watson } 2965c3b31afdSRobert Watson 2966c3b31afdSRobert Watson /* 2967c3b31afdSRobert Watson * Allocate the new address. Don't hook it up yet, as we may also 2968c3b31afdSRobert Watson * need to allocate a link layer multicast address. 2969c3b31afdSRobert Watson */ 2970c3b31afdSRobert Watson ifma = if_allocmulti(ifp, sa, llsa, M_NOWAIT); 2971c3b31afdSRobert Watson if (ifma == NULL) { 2972c3b31afdSRobert Watson error = ENOMEM; 2973c3b31afdSRobert Watson goto free_llsa_out; 2974c3b31afdSRobert Watson } 2975c3b31afdSRobert Watson 2976c3b31afdSRobert Watson /* 2977c3b31afdSRobert Watson * If a link layer address is found, we'll need to see if it's 2978c3b31afdSRobert Watson * already present in the address list, or allocate is as well. 2979c3b31afdSRobert Watson * When this block finishes, the link layer address will be on the 2980c3b31afdSRobert Watson * list. 2981c3b31afdSRobert Watson */ 2982c3b31afdSRobert Watson if (llsa != NULL) { 2983c3b31afdSRobert Watson ll_ifma = if_findmulti(ifp, llsa); 2984c3b31afdSRobert Watson if (ll_ifma == NULL) { 2985c3b31afdSRobert Watson ll_ifma = if_allocmulti(ifp, llsa, NULL, M_NOWAIT); 2986c3b31afdSRobert Watson if (ll_ifma == NULL) { 2987ec002feeSBruce M Simpson --ifma->ifma_refcount; 2988c3b31afdSRobert Watson if_freemulti(ifma); 2989c3b31afdSRobert Watson error = ENOMEM; 2990c3b31afdSRobert Watson goto free_llsa_out; 2991c3b31afdSRobert Watson } 2992c3b31afdSRobert Watson TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ll_ifma, 2993c3b31afdSRobert Watson ifma_link); 2994c3b31afdSRobert Watson } else 2995c3b31afdSRobert Watson ll_ifma->ifma_refcount++; 2996ec002feeSBruce M Simpson ifma->ifma_llifma = ll_ifma; 2997c3b31afdSRobert Watson } 2998c3b31afdSRobert Watson 2999c3b31afdSRobert Watson /* 3000c3b31afdSRobert Watson * We now have a new multicast address, ifma, and possibly a new or 3001c3b31afdSRobert Watson * referenced link layer address. Add the primary address to the 3002c3b31afdSRobert Watson * ifnet address list. 3003c3b31afdSRobert Watson */ 30046817526dSPoul-Henning Kamp TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link); 3005c3b31afdSRobert Watson 300613990766SJonathan Mini if (retifma != NULL) 3007373f88edSGarrett Wollman *retifma = ifma; 30081158dfb7SGarrett Wollman 3009c3b31afdSRobert Watson /* 3010c3b31afdSRobert Watson * Must generate the message while holding the lock so that 'ifma' 3011c3b31afdSRobert Watson * pointer is still valid. 3012c3b31afdSRobert Watson */ 3013c3b31afdSRobert Watson rt_newmaddrmsg(RTM_NEWMADDR, ifma); 3014c3b31afdSRobert Watson IF_ADDR_UNLOCK(ifp); 3015c3b31afdSRobert Watson 30161158dfb7SGarrett Wollman /* 30171158dfb7SGarrett Wollman * We are certain we have added something, so call down to the 30181158dfb7SGarrett Wollman * interface to let them know about it. 30191158dfb7SGarrett Wollman */ 30202432c31cSRobert Watson if (ifp->if_ioctl != NULL) { 30211a3b6859SYaroslav Tykhiy (void) (*ifp->if_ioctl)(ifp, SIOCADDMULTI, 0); 30221a3b6859SYaroslav Tykhiy } 30231158dfb7SGarrett Wollman 3024c3b31afdSRobert Watson if (llsa != NULL) 30251ede983cSDag-Erling Smørgrav free(llsa, M_IFMADDR); 3026c3b31afdSRobert Watson 3027c3b31afdSRobert Watson return (0); 3028c3b31afdSRobert Watson 3029c3b31afdSRobert Watson free_llsa_out: 3030c3b31afdSRobert Watson if (llsa != NULL) 30311ede983cSDag-Erling Smørgrav free(llsa, M_IFMADDR); 3032c3b31afdSRobert Watson 3033c3b31afdSRobert Watson unlock_out: 3034c3b31afdSRobert Watson IF_ADDR_UNLOCK(ifp); 3035c3b31afdSRobert Watson return (error); 30361158dfb7SGarrett Wollman } 30371158dfb7SGarrett Wollman 30381158dfb7SGarrett Wollman /* 3039ec002feeSBruce M Simpson * Delete a multicast group membership by network-layer group address. 3040ec002feeSBruce M Simpson * 3041ec002feeSBruce M Simpson * Returns ENOENT if the entry could not be found. If ifp no longer 3042ec002feeSBruce M Simpson * exists, results are undefined. This entry point should only be used 3043ec002feeSBruce M Simpson * from subsystems which do appropriate locking to hold ifp for the 3044ec002feeSBruce M Simpson * duration of the call. 3045ec002feeSBruce M Simpson * Network-layer protocol domains must use if_delmulti_ifma(). 30461158dfb7SGarrett Wollman */ 30471158dfb7SGarrett Wollman int 304872fd1b6aSDag-Erling Smørgrav if_delmulti(struct ifnet *ifp, struct sockaddr *sa) 30491158dfb7SGarrett Wollman { 3050ec002feeSBruce M Simpson struct ifmultiaddr *ifma; 3051ec002feeSBruce M Simpson int lastref; 3052ec002feeSBruce M Simpson #ifdef INVARIANTS 3053ec002feeSBruce M Simpson struct ifnet *oifp; 3054ec002feeSBruce M Simpson 305577dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 3056603724d3SBjoern A. Zeeb TAILQ_FOREACH(oifp, &V_ifnet, if_link) 3057ec002feeSBruce M Simpson if (ifp == oifp) 3058ec002feeSBruce M Simpson break; 3059ec002feeSBruce M Simpson if (ifp != oifp) 3060ec002feeSBruce M Simpson ifp = NULL; 306177dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 3062ec002feeSBruce M Simpson 3063ec002feeSBruce M Simpson KASSERT(ifp != NULL, ("%s: ifnet went away", __func__)); 3064ec002feeSBruce M Simpson #endif 3065ec002feeSBruce M Simpson if (ifp == NULL) 3066ec002feeSBruce M Simpson return (ENOENT); 30671158dfb7SGarrett Wollman 3068c3b31afdSRobert Watson IF_ADDR_LOCK(ifp); 3069ec002feeSBruce M Simpson lastref = 0; 3070c3b31afdSRobert Watson ifma = if_findmulti(ifp, sa); 3071ec002feeSBruce M Simpson if (ifma != NULL) 3072ec002feeSBruce M Simpson lastref = if_delmulti_locked(ifp, ifma, 0); 3073c3b31afdSRobert Watson IF_ADDR_UNLOCK(ifp); 3074c3b31afdSRobert Watson 3075ec002feeSBruce M Simpson if (ifma == NULL) 3076ec002feeSBruce M Simpson return (ENOENT); 3077ec002feeSBruce M Simpson 3078ec002feeSBruce M Simpson if (lastref && ifp->if_ioctl != NULL) { 30791a3b6859SYaroslav Tykhiy (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 308031302ebfSRobert Watson } 30811158dfb7SGarrett Wollman 3082ec002feeSBruce M Simpson return (0); 3083ec002feeSBruce M Simpson } 3084ec002feeSBruce M Simpson 3085ec002feeSBruce M Simpson /* 308693ec7edcSShteryana Shopova * Delete all multicast group membership for an interface. 308793ec7edcSShteryana Shopova * Should be used to quickly flush all multicast filters. 308893ec7edcSShteryana Shopova */ 308993ec7edcSShteryana Shopova void 309093ec7edcSShteryana Shopova if_delallmulti(struct ifnet *ifp) 309193ec7edcSShteryana Shopova { 309293ec7edcSShteryana Shopova struct ifmultiaddr *ifma; 309393ec7edcSShteryana Shopova struct ifmultiaddr *next; 309493ec7edcSShteryana Shopova 309593ec7edcSShteryana Shopova IF_ADDR_LOCK(ifp); 309693ec7edcSShteryana Shopova TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) 309793ec7edcSShteryana Shopova if_delmulti_locked(ifp, ifma, 0); 309893ec7edcSShteryana Shopova IF_ADDR_UNLOCK(ifp); 309993ec7edcSShteryana Shopova } 310093ec7edcSShteryana Shopova 310193ec7edcSShteryana Shopova /* 3102ec002feeSBruce M Simpson * Delete a multicast group membership by group membership pointer. 3103ec002feeSBruce M Simpson * Network-layer protocol domains must use this routine. 3104ec002feeSBruce M Simpson * 3105e5adda3dSRobert Watson * It is safe to call this routine if the ifp disappeared. 3106ec002feeSBruce M Simpson */ 3107ec002feeSBruce M Simpson void 3108ec002feeSBruce M Simpson if_delmulti_ifma(struct ifmultiaddr *ifma) 3109ec002feeSBruce M Simpson { 3110ec002feeSBruce M Simpson struct ifnet *ifp; 3111ec002feeSBruce M Simpson int lastref; 3112ec002feeSBruce M Simpson 3113ec002feeSBruce M Simpson ifp = ifma->ifma_ifp; 3114ec002feeSBruce M Simpson #ifdef DIAGNOSTIC 3115ec002feeSBruce M Simpson if (ifp == NULL) { 3116ec002feeSBruce M Simpson printf("%s: ifma_ifp seems to be detached\n", __func__); 3117ec002feeSBruce M Simpson } else { 3118ec002feeSBruce M Simpson struct ifnet *oifp; 3119ec002feeSBruce M Simpson 312077dfcdc4SRobert Watson IFNET_RLOCK_NOSLEEP(); 3121603724d3SBjoern A. Zeeb TAILQ_FOREACH(oifp, &V_ifnet, if_link) 3122ec002feeSBruce M Simpson if (ifp == oifp) 3123ec002feeSBruce M Simpson break; 3124ec002feeSBruce M Simpson if (ifp != oifp) { 3125ec002feeSBruce M Simpson printf("%s: ifnet %p disappeared\n", __func__, ifp); 3126ec002feeSBruce M Simpson ifp = NULL; 3127ec002feeSBruce M Simpson } 312877dfcdc4SRobert Watson IFNET_RUNLOCK_NOSLEEP(); 3129ec002feeSBruce M Simpson } 3130ec002feeSBruce M Simpson #endif 3131ec002feeSBruce M Simpson /* 3132ec002feeSBruce M Simpson * If and only if the ifnet instance exists: Acquire the address lock. 3133ec002feeSBruce M Simpson */ 3134ec002feeSBruce M Simpson if (ifp != NULL) 3135ec002feeSBruce M Simpson IF_ADDR_LOCK(ifp); 3136ec002feeSBruce M Simpson 3137ec002feeSBruce M Simpson lastref = if_delmulti_locked(ifp, ifma, 0); 3138ec002feeSBruce M Simpson 3139ec002feeSBruce M Simpson if (ifp != NULL) { 3140ec002feeSBruce M Simpson /* 3141ec002feeSBruce M Simpson * If and only if the ifnet instance exists: 3142ec002feeSBruce M Simpson * Release the address lock. 3143ec002feeSBruce M Simpson * If the group was left: update the hardware hash filter. 3144ec002feeSBruce M Simpson */ 3145ec002feeSBruce M Simpson IF_ADDR_UNLOCK(ifp); 3146ec002feeSBruce M Simpson if (lastref && ifp->if_ioctl != NULL) { 3147ec002feeSBruce M Simpson (void)(*ifp->if_ioctl)(ifp, SIOCDELMULTI, 0); 3148ec002feeSBruce M Simpson } 3149ec002feeSBruce M Simpson } 3150ec002feeSBruce M Simpson } 3151ec002feeSBruce M Simpson 3152ec002feeSBruce M Simpson /* 3153ec002feeSBruce M Simpson * Perform deletion of network-layer and/or link-layer multicast address. 3154ec002feeSBruce M Simpson * 3155ec002feeSBruce M Simpson * Return 0 if the reference count was decremented. 3156ec002feeSBruce M Simpson * Return 1 if the final reference was released, indicating that the 3157ec002feeSBruce M Simpson * hardware hash filter should be reprogrammed. 3158ec002feeSBruce M Simpson */ 3159ec002feeSBruce M Simpson static int 3160ec002feeSBruce M Simpson if_delmulti_locked(struct ifnet *ifp, struct ifmultiaddr *ifma, int detaching) 3161ec002feeSBruce M Simpson { 3162ec002feeSBruce M Simpson struct ifmultiaddr *ll_ifma; 3163ec002feeSBruce M Simpson 3164ec002feeSBruce M Simpson if (ifp != NULL && ifma->ifma_ifp != NULL) { 3165ec002feeSBruce M Simpson KASSERT(ifma->ifma_ifp == ifp, 3166ec002feeSBruce M Simpson ("%s: inconsistent ifp %p", __func__, ifp)); 3167ec002feeSBruce M Simpson IF_ADDR_LOCK_ASSERT(ifp); 3168ec002feeSBruce M Simpson } 3169ec002feeSBruce M Simpson 3170ec002feeSBruce M Simpson ifp = ifma->ifma_ifp; 3171ec002feeSBruce M Simpson 3172ec002feeSBruce M Simpson /* 3173ec002feeSBruce M Simpson * If the ifnet is detaching, null out references to ifnet, 3174ec002feeSBruce M Simpson * so that upper protocol layers will notice, and not attempt 317575ae0c01SBruce M Simpson * to obtain locks for an ifnet which no longer exists. The 317675ae0c01SBruce M Simpson * routing socket announcement must happen before the ifnet 317775ae0c01SBruce M Simpson * instance is detached from the system. 3178ec002feeSBruce M Simpson */ 3179ec002feeSBruce M Simpson if (detaching) { 3180ec002feeSBruce M Simpson #ifdef DIAGNOSTIC 3181ec002feeSBruce M Simpson printf("%s: detaching ifnet instance %p\n", __func__, ifp); 3182ec002feeSBruce M Simpson #endif 318375ae0c01SBruce M Simpson /* 318475ae0c01SBruce M Simpson * ifp may already be nulled out if we are being reentered 318575ae0c01SBruce M Simpson * to delete the ll_ifma. 318675ae0c01SBruce M Simpson */ 318775ae0c01SBruce M Simpson if (ifp != NULL) { 318875ae0c01SBruce M Simpson rt_newmaddrmsg(RTM_DELMADDR, ifma); 3189ec002feeSBruce M Simpson ifma->ifma_ifp = NULL; 3190ec002feeSBruce M Simpson } 319175ae0c01SBruce M Simpson } 3192ec002feeSBruce M Simpson 3193ec002feeSBruce M Simpson if (--ifma->ifma_refcount > 0) 31941158dfb7SGarrett Wollman return 0; 3195ec002feeSBruce M Simpson 3196ec002feeSBruce M Simpson /* 3197ec002feeSBruce M Simpson * If this ifma is a network-layer ifma, a link-layer ifma may 3198ec002feeSBruce M Simpson * have been associated with it. Release it first if so. 3199ec002feeSBruce M Simpson */ 3200ec002feeSBruce M Simpson ll_ifma = ifma->ifma_llifma; 3201ec002feeSBruce M Simpson if (ll_ifma != NULL) { 3202ec002feeSBruce M Simpson KASSERT(ifma->ifma_lladdr != NULL, 3203ec002feeSBruce M Simpson ("%s: llifma w/o lladdr", __func__)); 3204ec002feeSBruce M Simpson if (detaching) 3205ec002feeSBruce M Simpson ll_ifma->ifma_ifp = NULL; /* XXX */ 3206ec002feeSBruce M Simpson if (--ll_ifma->ifma_refcount == 0) { 3207ec002feeSBruce M Simpson if (ifp != NULL) { 3208ec002feeSBruce M Simpson TAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, 3209ec002feeSBruce M Simpson ifma_link); 3210ec002feeSBruce M Simpson } 3211ec002feeSBruce M Simpson if_freemulti(ll_ifma); 3212ec002feeSBruce M Simpson } 3213ec002feeSBruce M Simpson } 3214ec002feeSBruce M Simpson 3215ec002feeSBruce M Simpson if (ifp != NULL) 3216ec002feeSBruce M Simpson TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link); 3217ec002feeSBruce M Simpson 3218ec002feeSBruce M Simpson if_freemulti(ifma); 3219ec002feeSBruce M Simpson 3220ec002feeSBruce M Simpson /* 3221ec002feeSBruce M Simpson * The last reference to this instance of struct ifmultiaddr 3222ec002feeSBruce M Simpson * was released; the hardware should be notified of this change. 3223ec002feeSBruce M Simpson */ 3224ec002feeSBruce M Simpson return 1; 32251158dfb7SGarrett Wollman } 32261158dfb7SGarrett Wollman 322766ce51ceSArchie Cobbs /* 322866ce51ceSArchie Cobbs * Set the link layer address on an interface. 322966ce51ceSArchie Cobbs * 323066ce51ceSArchie Cobbs * At this time we only support certain types of interfaces, 323166ce51ceSArchie Cobbs * and we don't allow the length of the address to change. 323266ce51ceSArchie Cobbs */ 323366ce51ceSArchie Cobbs int 323466ce51ceSArchie Cobbs if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len) 323566ce51ceSArchie Cobbs { 323666ce51ceSArchie Cobbs struct sockaddr_dl *sdl; 323766ce51ceSArchie Cobbs struct ifaddr *ifa; 3238d637e989SPeter Wemm struct ifreq ifr; 323966ce51ceSArchie Cobbs 32403baaf297SRobert Watson IF_ADDR_LOCK(ifp); 32414a0d6638SRuslan Ermilov ifa = ifp->if_addr; 32423baaf297SRobert Watson if (ifa == NULL) { 32433baaf297SRobert Watson IF_ADDR_UNLOCK(ifp); 324466ce51ceSArchie Cobbs return (EINVAL); 32453baaf297SRobert Watson } 32463baaf297SRobert Watson ifa_ref(ifa); 32473baaf297SRobert Watson IF_ADDR_UNLOCK(ifp); 324866ce51ceSArchie Cobbs sdl = (struct sockaddr_dl *)ifa->ifa_addr; 32493baaf297SRobert Watson if (sdl == NULL) { 32503baaf297SRobert Watson ifa_free(ifa); 325166ce51ceSArchie Cobbs return (EINVAL); 32523baaf297SRobert Watson } 32533baaf297SRobert Watson if (len != sdl->sdl_alen) { /* don't allow length to change */ 32543baaf297SRobert Watson ifa_free(ifa); 325566ce51ceSArchie Cobbs return (EINVAL); 32563baaf297SRobert Watson } 325766ce51ceSArchie Cobbs switch (ifp->if_type) { 3258d09ed26fSRuslan Ermilov case IFT_ETHER: 325966ce51ceSArchie Cobbs case IFT_FDDI: 326066ce51ceSArchie Cobbs case IFT_XETHER: 326166ce51ceSArchie Cobbs case IFT_ISO88025: 3262b7bffa71SYaroslav Tykhiy case IFT_L2VLAN: 32638f867517SAndrew Thompson case IFT_BRIDGE: 32646cdcc159SMax Khon case IFT_ARCNET: 3265b47888ceSAndrew Thompson case IFT_IEEE8023ADLAG: 3266a51f44a7SSam Leffler case IFT_IEEE80211: 326766ce51ceSArchie Cobbs bcopy(lladdr, LLADDR(sdl), len); 32683baaf297SRobert Watson ifa_free(ifa); 326966ce51ceSArchie Cobbs break; 327066ce51ceSArchie Cobbs default: 32713baaf297SRobert Watson ifa_free(ifa); 327266ce51ceSArchie Cobbs return (ENODEV); 327366ce51ceSArchie Cobbs } 32743baaf297SRobert Watson 327566ce51ceSArchie Cobbs /* 327666ce51ceSArchie Cobbs * If the interface is already up, we need 327766ce51ceSArchie Cobbs * to re-init it in order to reprogram its 327866ce51ceSArchie Cobbs * address filter. 327966ce51ceSArchie Cobbs */ 328066ce51ceSArchie Cobbs if ((ifp->if_flags & IFF_UP) != 0) { 32811a3b6859SYaroslav Tykhiy if (ifp->if_ioctl) { 328266ce51ceSArchie Cobbs ifp->if_flags &= ~IFF_UP; 328362f76486SMaxim Sobolev ifr.ifr_flags = ifp->if_flags & 0xffff; 328462f76486SMaxim Sobolev ifr.ifr_flagshigh = ifp->if_flags >> 16; 3285ee0a4f7eSSUZUKI Shinsuke (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 328666ce51ceSArchie Cobbs ifp->if_flags |= IFF_UP; 328762f76486SMaxim Sobolev ifr.ifr_flags = ifp->if_flags & 0xffff; 328862f76486SMaxim Sobolev ifr.ifr_flagshigh = ifp->if_flags >> 16; 3289ee0a4f7eSSUZUKI Shinsuke (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr); 32901a3b6859SYaroslav Tykhiy } 3291b2c08f43SLuigi Rizzo #ifdef INET 3292b2c08f43SLuigi Rizzo /* 3293b2c08f43SLuigi Rizzo * Also send gratuitous ARPs to notify other nodes about 3294b2c08f43SLuigi Rizzo * the address change. 3295b2c08f43SLuigi Rizzo */ 3296b2c08f43SLuigi Rizzo TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 32974b97d7afSYaroslav Tykhiy if (ifa->ifa_addr->sa_family == AF_INET) 3298c0933269SPeter Wemm arp_ifinit(ifp, ifa); 3299b2c08f43SLuigi Rizzo } 3300b2c08f43SLuigi Rizzo #endif 330166ce51ceSArchie Cobbs } 330266ce51ceSArchie Cobbs return (0); 330366ce51ceSArchie Cobbs } 330466ce51ceSArchie Cobbs 33059bf40edeSBrooks Davis /* 33069bf40edeSBrooks Davis * The name argument must be a pointer to storage which will last as 33079bf40edeSBrooks Davis * long as the interface does. For physical devices, the result of 33089bf40edeSBrooks Davis * device_get_name(dev) is a good choice and for pseudo-devices a 33099bf40edeSBrooks Davis * static string works well. 33109bf40edeSBrooks Davis */ 33119bf40edeSBrooks Davis void 33129bf40edeSBrooks Davis if_initname(struct ifnet *ifp, const char *name, int unit) 33139bf40edeSBrooks Davis { 33149bf40edeSBrooks Davis ifp->if_dname = name; 33159bf40edeSBrooks Davis ifp->if_dunit = unit; 33169bf40edeSBrooks Davis if (unit != IF_DUNIT_NONE) 33179bf40edeSBrooks Davis snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", name, unit); 33189bf40edeSBrooks Davis else 33199bf40edeSBrooks Davis strlcpy(ifp->if_xname, name, IFNAMSIZ); 33209bf40edeSBrooks Davis } 33219bf40edeSBrooks Davis 3322fa882e87SBrooks Davis int 3323fa882e87SBrooks Davis if_printf(struct ifnet *ifp, const char * fmt, ...) 3324fa882e87SBrooks Davis { 3325fa882e87SBrooks Davis va_list ap; 3326fa882e87SBrooks Davis int retval; 3327fa882e87SBrooks Davis 33289bf40edeSBrooks Davis retval = printf("%s: ", ifp->if_xname); 3329fa882e87SBrooks Davis va_start(ap, fmt); 3330fa882e87SBrooks Davis retval += vprintf(fmt, ap); 3331fa882e87SBrooks Davis va_end(ap); 3332fa882e87SBrooks Davis return (retval); 3333fa882e87SBrooks Davis } 3334fa882e87SBrooks Davis 3335af5e59bfSRobert Watson void 3336af5e59bfSRobert Watson if_start(struct ifnet *ifp) 3337af5e59bfSRobert Watson { 3338af5e59bfSRobert Watson 3339af5e59bfSRobert Watson (*(ifp)->if_start)(ifp); 3340af5e59bfSRobert Watson } 3341af5e59bfSRobert Watson 3342db7f0b97SKip Macy /* 3343db7f0b97SKip Macy * Backwards compatibility interface for drivers 3344db7f0b97SKip Macy * that have not implemented it 3345db7f0b97SKip Macy */ 3346db7f0b97SKip Macy static int 3347db7f0b97SKip Macy if_transmit(struct ifnet *ifp, struct mbuf *m) 3348db7f0b97SKip Macy { 3349db7f0b97SKip Macy int error; 3350db7f0b97SKip Macy 3351db7f0b97SKip Macy IFQ_HANDOFF(ifp, m, error); 3352db7f0b97SKip Macy return (error); 3353db7f0b97SKip Macy } 3354db7f0b97SKip Macy 33550b762445SRobert Watson int 33560b762445SRobert Watson if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust) 33570b762445SRobert Watson { 33580b762445SRobert Watson int active = 0; 33590b762445SRobert Watson 33600b762445SRobert Watson IF_LOCK(ifq); 33610b762445SRobert Watson if (_IF_QFULL(ifq)) { 33620b762445SRobert Watson _IF_DROP(ifq); 33630b762445SRobert Watson IF_UNLOCK(ifq); 33640b762445SRobert Watson m_freem(m); 33650b762445SRobert Watson return (0); 33660b762445SRobert Watson } 33670b762445SRobert Watson if (ifp != NULL) { 33680b762445SRobert Watson ifp->if_obytes += m->m_pkthdr.len + adjust; 33690b762445SRobert Watson if (m->m_flags & (M_BCAST|M_MCAST)) 33700b762445SRobert Watson ifp->if_omcasts++; 3371292ee7beSRobert Watson active = ifp->if_drv_flags & IFF_DRV_OACTIVE; 33720b762445SRobert Watson } 33730b762445SRobert Watson _IF_ENQUEUE(ifq, m); 33740b762445SRobert Watson IF_UNLOCK(ifq); 33750b762445SRobert Watson if (ifp != NULL && !active) 3376e5adda3dSRobert Watson (*(ifp)->if_start)(ifp); 33770b762445SRobert Watson return (1); 33780b762445SRobert Watson } 3379fc74a9f9SBrooks Davis 3380fc74a9f9SBrooks Davis void 3381fc74a9f9SBrooks Davis if_register_com_alloc(u_char type, 3382fc74a9f9SBrooks Davis if_com_alloc_t *a, if_com_free_t *f) 3383fc74a9f9SBrooks Davis { 3384fc74a9f9SBrooks Davis 3385fc74a9f9SBrooks Davis KASSERT(if_com_alloc[type] == NULL, 3386fc74a9f9SBrooks Davis ("if_register_com_alloc: %d already registered", type)); 3387fc74a9f9SBrooks Davis KASSERT(if_com_free[type] == NULL, 3388fc74a9f9SBrooks Davis ("if_register_com_alloc: %d free already registered", type)); 3389fc74a9f9SBrooks Davis 3390fc74a9f9SBrooks Davis if_com_alloc[type] = a; 3391fc74a9f9SBrooks Davis if_com_free[type] = f; 3392fc74a9f9SBrooks Davis } 3393fc74a9f9SBrooks Davis 3394fc74a9f9SBrooks Davis void 3395fc74a9f9SBrooks Davis if_deregister_com_alloc(u_char type) 3396fc74a9f9SBrooks Davis { 3397fc74a9f9SBrooks Davis 3398affcaf78SMax Khon KASSERT(if_com_alloc[type] != NULL, 3399fc74a9f9SBrooks Davis ("if_deregister_com_alloc: %d not registered", type)); 3400affcaf78SMax Khon KASSERT(if_com_free[type] != NULL, 3401fc74a9f9SBrooks Davis ("if_deregister_com_alloc: %d free not registered", type)); 3402fc74a9f9SBrooks Davis if_com_alloc[type] = NULL; 3403fc74a9f9SBrooks Davis if_com_free[type] = NULL; 3404fc74a9f9SBrooks Davis } 3405